From: Nathan Chen <nathanc@nvidia.com>
To: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
linux-kernel@vger.kernel.org
Cc: Mark Rutland <mark.rutland@arm.com>,
Will Deacon <will@kernel.org>,
Catalin Marinas <catalin.marinas@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Joey Gouly <joey.gouly@arm.com>,
Fuad Tabba <fuad.tabba@linux.dev>,
Oliver Upton <oupton@kernel.org>, Marc Zyngier <maz@kernel.org>,
Tom Matthias <tmatthias@nvidia.com>,
Shameer Kolothum Thodi <skolothumtho@nvidia.com>,
Jason Gunthorpe <jgg@nvidia.com>,
Julian Miller <julianmiller@nvidia.com>
Subject: [RFC PATCH 2/6] KVM: arm64: nv: Pick the shadow S2 MMU to recycle with a clock
Date: Tue, 24 Nov 2026 13:12:14 -0800 [thread overview]
Message-ID: <20261124211218.2751086-3-nathanc@nvidia.com> (raw)
In-Reply-To: <20261124211218.2751086-1-nathanc@nvidia.com>
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@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
next prev parent reply other threads:[~2026-11-24 21:17 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 21:12 [RFC PATCH 0/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 1/6] KVM: arm64: nv: Allocate shadow S2 MMUs in blocks Nathan Chen
2026-11-24 21:12 ` Nathan Chen [this message]
2026-11-24 21:12 ` [RFC PATCH 3/6] KVM: arm64: Add kvm_arm_vmid_nr_usable() Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 4/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 5/6] KVM: arm64: nv: Park grown MMUs on ALLE1 and reactivate them " Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 6/6] KVM: arm64: nv: Park shadow S2 MMUs emptied by a guest range invalidation Nathan Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124211218.2751086-3-nathanc@nvidia.com \
--to=nathanc@nvidia.com \
--cc=catalin.marinas@arm.com \
--cc=fuad.tabba@linux.dev \
--cc=jgg@nvidia.com \
--cc=joey.gouly@arm.com \
--cc=julianmiller@nvidia.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=skolothumtho@nvidia.com \
--cc=suzuki.poulose@arm.com \
--cc=tmatthias@nvidia.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox