[RFC PATCH 2/6] KVM: arm64: nv: Pick the shadow S2 MMU to recycle with a clock

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


On a lookup miss, get_s2_mmu_nested() recycles the first free MMU after
the last one it picked. That MMU may hold the shadow stage-2 of a nested
VM that is still running and about to be loaded again, while the pool
has MMUs nobody has used in a long time, such as those of torn down
nested VMs. Recycling a live MMU throws away its shadow tables and
makes its nested VM fault them all back in.

Track use with an 'accessed' flag, set whenever a vcpu takes a reference
on the MMU, and pick the victim with a clock (second chance):

- A free MMU that is invalid, or wasn't used since the search last went
  past it, is recycled straight away.

- Other free MMUs have their flag cleared as the search goes past them.

- If every free MMU was used since the last pass, the first one found
  is recycled, as before.

Signed-off-by: Nathan Chen <nathanc at nvidia.com>
---
 arch/arm64/include/asm/kvm_host.h |  3 +++
 arch/arm64/kvm/nested.c           | 30 +++++++++++++++++++++++++-----
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index ff5073b13338..c7133999296f 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -325,6 +325,9 @@ struct kvm_s2_mmu {
 	/* Index in the S2 MMU array, only valid for a shadow S2 */
 	u16	s2_mmu_idx;
 
+	/* Used since the victim search last went past it, under mmu_lock */
+	bool	accessed;
+
 	/*
 	 *  0: Nobody is currently using this, check vttbr for validity
 	 * >0: Somebody is actively using this.
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 7d78efee5296..44dc6780fe6f 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -842,7 +842,7 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
 {
 	struct kvm *kvm = vcpu->kvm;
 	struct kvm_s2_mmu *s2_mmu;
-	int i;
+	int i, victim = -1;
 
 	lockdep_assert_held_write(&vcpu->kvm->mmu_lock);
 
@@ -854,19 +854,38 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
 	 * Make sure we don't always search from the same point, or we
 	 * will always reuse a potentially active context, leaving
 	 * free contexts unused.
+	 *
+	 * Pick the victim with a clock: a free MMU that is invalid, or
+	 * hasn't been used since the search last went past it (such as
+	 * one left behind by a torn down guest), is recycled straight
+	 * away. Other free MMUs lose their accessed bit and get a second
+	 * chance. If every free MMU was used since the last pass, the
+	 * first one found is recycled.
 	 */
 	for (i = kvm->arch.nested_mmus_next;
 	     i < (kvm->arch.nested_mmus_size + kvm->arch.nested_mmus_next);
 	     i++) {
-		s2_mmu = kvm->arch.nested_mmus[i % kvm->arch.nested_mmus_size];
+		struct kvm_s2_mmu *mmu;
+
+		mmu = kvm->arch.nested_mmus[i % kvm->arch.nested_mmus_size];
+		if (atomic_read(&mmu->refcnt))
+			continue;
 
-		if (atomic_read(&s2_mmu->refcnt) == 0)
+		if (!kvm_s2_mmu_valid(mmu) || !mmu->accessed) {
+			victim = i;
 			break;
+		}
+
+		if (victim < 0)
+			victim = i;
+
+		mmu->accessed = false;
 	}
-	BUG_ON(atomic_read(&s2_mmu->refcnt)); /* We have struct MMUs to spare */
+	BUG_ON(victim < 0); /* We have struct MMUs to spare */
+	s2_mmu = kvm->arch.nested_mmus[victim % kvm->arch.nested_mmus_size];
 
 	/* Set the scene for the next search */
-	kvm->arch.nested_mmus_next = (i + 1) % kvm->arch.nested_mmus_size;
+	kvm->arch.nested_mmus_next = (victim + 1) % kvm->arch.nested_mmus_size;
 
 	/* Make sure we don't forget to do the laundry */
 	if (kvm_s2_mmu_valid(s2_mmu))
@@ -886,6 +905,7 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
 
 out:
 	atomic_inc(&s2_mmu->refcnt);
+	s2_mmu->accessed = true;
 
 	/*
 	 * Set the vCPU request to perform an unmap, even if the pending unmap
-- 
2.43.0




More information about the linux-arm-kernel mailing list