[RFC PATCH 6/6] KVM: arm64: nv: Park shadow S2 MMUs emptied by a guest range invalidation

Nathan Chen nathanc at nvidia.com
Tue Nov 24 13:12:18 PST 2026


If a guest TLBI RIPAS2E1IS or VMALLS12E1IS leaves a grown, unreferenced
MMU empty, park it there rather than leave it valid until a recycle
unmaps the whole IPA space of an already empty MMU.

Signed-off-by: Nathan Chen <nathanc at nvidia.com>
---
 arch/arm64/include/asm/kvm_nested.h | 1 +
 arch/arm64/kvm/nested.c             | 2 +-
 arch/arm64/kvm/sys_regs.c           | 8 ++++++++
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h
index 952853ab245a..164e42376296 100644
--- a/arch/arm64/include/asm/kvm_nested.h
+++ b/arch/arm64/include/asm/kvm_nested.h
@@ -175,6 +175,7 @@ extern void kvm_nested_unmap_cipa_range(struct kvm *kvm, gpa_t cipa,
 					size_t unmap_size, bool may_block);
 extern void kvm_nested_s2_unmap(struct kvm *kvm, bool may_block);
 extern void kvm_nested_s2_shrink(struct kvm *kvm);
+extern bool kvm_nested_s2_park(struct kvm *kvm, struct kvm_s2_mmu *mmu);
 extern void kvm_nested_s2_flush(struct kvm *kvm);
 
 unsigned long compute_tlb_inval_range(struct kvm_s2_mmu *mmu, u64 val);
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 9251f3fd4840..e9a2eaef9eda 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -121,7 +121,7 @@ static bool kvm_s2_mmu_empty(struct kvm_s2_mmu *mmu)
  * mmu_lock never see it go away. Parking always leaves S2_MMU_PER_VCPU
  * unparked MMUs per vcpu, so the victim search always has a candidate.
  */
-static bool kvm_nested_s2_park(struct kvm *kvm, struct kvm_s2_mmu *mmu)
+bool kvm_nested_s2_park(struct kvm *kvm, struct kvm_s2_mmu *mmu)
 {
 	unsigned int keep = atomic_read(&kvm->online_vcpus) * S2_MMU_PER_VCPU;
 
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index df81adf96259..99dcd2549b08 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -4123,6 +4123,14 @@ static void s2_mmu_unmap_range(struct kvm_s2_mmu *mmu,
 	 * the TLBI.
 	 */
 	kvm_stage2_unmap_range(mmu, info->range.start, info->range.size, true);
+
+	/*
+	 * The guest hypervisor may have unmapped everything this MMU held,
+	 * e.g. all memory of a nested VM being torn down. If it is grown and
+	 * now empty, return it to the budget rather than leave it for a
+	 * recycle to unmap again.
+	 */
+	kvm_nested_s2_park(kvm_s2_mmu_to_kvm(mmu), mmu);
 }
 
 static bool handle_vmalls12e1is(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
-- 
2.43.0




More information about the linux-arm-kernel mailing list