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 5/6] KVM: arm64: nv: Park grown MMUs on ALLE1 and reactivate them on demand
Date: Tue, 24 Nov 2026 13:12:17 -0800 [thread overview]
Message-ID: <20261124211218.2751086-6-nathanc@nvidia.com> (raw)
In-Reply-To: <20261124211218.2751086-1-nathanc@nvidia.com>
Grown shadow S2 MMUs only return to the host budget when their VM is
destroyed, so a guest hypervisor that once ran many nested VMs keeps
holding the budget after they are gone.
Park the grown MMUs that are unreferenced and still empty after a guest
TLBI ALLE1, returning them to the budget. A later grow reactivates
parked MMUs before allocating new ones.
Signed-off-by: Nathan Chen <nathanc@nvidia.com>
---
arch/arm64/include/asm/kvm_host.h | 13 +++-
arch/arm64/include/asm/kvm_nested.h | 1 +
arch/arm64/kvm/nested.c | 113 +++++++++++++++++++++++++---
arch/arm64/kvm/sys_regs.c | 1 +
4 files changed, 118 insertions(+), 10 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 9307b86285a5..c0b9cd60dbe6 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -329,6 +329,15 @@ struct kvm_s2_mmu {
/* Used since the victim search last went past it, under mmu_lock */
bool accessed;
+ /*
+ * Added by growing the pool, and returned to the host budget by
+ * parking it (invalid and unmapped) once unused. A parked MMU is
+ * never picked by the victim search until a grow reactivates it.
+ * Both under mmu_lock.
+ */
+ bool grown;
+ bool parked;
+
/*
* 0: Nobody is currently using this, check vttbr for validity
* >0: Somebody is actively using this.
@@ -441,8 +450,10 @@ struct kvm_arch {
size_t nested_mmus_size;
size_t nested_mmus_capacity;
struct list_head nested_mmu_blocks;
- /* Grown MMUs, counted in the host budget, under mmu_lock */
+ /* Grown MMUs not parked, counted in the host budget, under mmu_lock */
unsigned int nested_mmus_grown;
+ /* Parked MMUs, under mmu_lock */
+ unsigned int nested_mmus_parked;
int nested_mmus_next;
bool nested_mmus_grow;
bool nested_mmus_no_grow;
diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h
index 0f899addafa8..952853ab245a 100644
--- a/arch/arm64/include/asm/kvm_nested.h
+++ b/arch/arm64/include/asm/kvm_nested.h
@@ -174,6 +174,7 @@ extern void kvm_nested_s2_wp(struct kvm *kvm);
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 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 d530a37716be..9251f3fd4840 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -96,6 +96,55 @@ struct nested_s2_mmu_block {
struct kvm_s2_mmu mmus[];
};
+static int s2_mmu_nonempty_walker(const struct kvm_pgtable_visit_ctx *ctx,
+ enum kvm_pgtable_walk_flags visit)
+{
+ return ctx->old ? -EEXIST : 0;
+}
+
+/* Only looks at the stage-2 root unless it finds an entry in use */
+static bool kvm_s2_mmu_empty(struct kvm_s2_mmu *mmu)
+{
+ struct kvm_pgtable_walker walker = {
+ .cb = s2_mmu_nonempty_walker,
+ .flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_PRE,
+ };
+
+ return !kvm_pgtable_walk(mmu->pgt, 0, kvm_phys_size(mmu), &walker);
+}
+
+/*
+ * Park @mmu if it is grown, unreferenced and empty, returning it to the
+ * budget. It is invalidated, so no lookup can hand it out, and skipped
+ * by the victim search. It keeps its slot, its struct and its stage-2
+ * root until the VM is destroyed, so walkers of the table that drop
+ * 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)
+{
+ unsigned int keep = atomic_read(&kvm->online_vcpus) * S2_MMU_PER_VCPU;
+
+ lockdep_assert_held_write(&kvm->mmu_lock);
+
+ if (kvm->arch.nested_mmus_size - kvm->arch.nested_mmus_parked <= keep)
+ return false;
+
+ if (!mmu->grown || mmu->parked || atomic_read(&mmu->refcnt) ||
+ !mmu->pgt || !kvm_s2_mmu_empty(mmu))
+ return false;
+
+ if (kvm_s2_mmu_valid(mmu))
+ kvm_init_nested_s2_mmu(mmu);
+ mmu->parked = true;
+ mmu->pending_unmap = false;
+
+ kvm->arch.nested_mmus_grown--;
+ kvm->arch.nested_mmus_parked++;
+ atomic_dec(&nested_s2_mmus_grown);
+ return true;
+}
+
int kvm_init_nested(struct kvm *kvm)
{
INIT_LIST_HEAD(&kvm->arch.nested_mmu_blocks);
@@ -167,6 +216,7 @@ static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr, bool grow)
goto out_free;
blk->mmus[i].s2_mmu_idx = size + i;
+ blk->mmus[i].grown = grow;
}
if (size + nr > cap) {
@@ -216,14 +266,15 @@ static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr, bool grow)
/*
* A vcpu had to recycle a shadow S2 MMU in concurrent use. The request
* is per vcpu, but the pool belongs to the VM: grow it by one MMU per
- * vcpu, within the budget and S2_MMU_MAX_PER_VM. If the budget is used
- * up, the pool keeps recycling until another VM's destruction returns
+ * vcpu, within the budget and S2_MMU_MAX_PER_VM, reactivating parked
+ * MMUs before allocating new ones. If the budget is used up, the pool
+ * keeps recycling until parking or another VM's destruction returns
* some. Stop growing for good if an allocation fails, so that a VM at
* its memory limit doesn't retry on every recycle.
*/
static void kvm_nested_s2_grow(struct kvm *kvm)
{
- int nr, add;
+ int nr, add = 0, reactivated = 0, first = -1;
guard(mutex)(&kvm->arch.config_lock);
@@ -238,15 +289,57 @@ static void kvm_nested_s2_grow(struct kvm *kvm)
return;
}
- add = min_t(int, nr, S2_MMU_MAX_PER_VM - kvm->arch.nested_mmus_size);
+ scoped_guard(write_lock, &kvm->mmu_lock) {
+ for (int i = 0; i < kvm->arch.nested_mmus_size; i++) {
+ struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
+
+ if (reactivated == nr)
+ break;
+ if (!mmu->parked)
+ continue;
+
+ mmu->parked = false;
+ reactivated++;
+ if (first < 0)
+ first = i;
+ }
+
+ if (reactivated) {
+ kvm->arch.nested_mmus_grown += reactivated;
+ kvm->arch.nested_mmus_parked -= reactivated;
+ kvm->arch.nested_mmus_next = first;
+ }
+
+ add = min_t(int, nr - reactivated,
+ S2_MMU_MAX_PER_VM - kvm->arch.nested_mmus_size);
+ }
+
if (add && kvm_nested_add_s2_mmus(kvm, add, true)) {
WRITE_ONCE(kvm->arch.nested_mmus_no_grow, true);
add = 0;
}
WRITE_ONCE(kvm->arch.nested_mmus_grow, false);
- if (nr - add)
- atomic_sub(nr - add, &nested_s2_mmus_grown);
+ if (nr - reactivated - add)
+ atomic_sub(nr - reactivated - add, &nested_s2_mmus_grown);
+}
+
+/*
+ * The guest hypervisor invalidated all of its stage-2 TLBs, and the
+ * caller has just unmapped every shadow S2 MMU. Any MMU of a torn down
+ * nested VM is now empty and unreferenced, but so may be those of live
+ * nested VMs that aren't running right now. Park every grown MMU that
+ * is unreferenced and still empty. Parking doesn't unmap or invalidate
+ * TLBs again, as every removal of its mappings was followed by a TLB
+ * invalidation. An MMU that was refilled since the caller's unmap is in
+ * use, and is left alone.
+ */
+void kvm_nested_s2_shrink(struct kvm *kvm)
+{
+ lockdep_assert_held_write(&kvm->mmu_lock);
+
+ for (int i = 0; i < kvm->arch.nested_mmus_size; i++)
+ kvm_nested_s2_park(kvm, kvm->arch.nested_mmus[i]);
}
int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
@@ -976,7 +1069,8 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
* 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, they
- * are all in concurrent use and the pool is too small.
+ * are all in concurrent use and the pool is too small. Parked
+ * MMUs are skipped until a grow reactivates them.
*/
for (i = kvm->arch.nested_mmus_next;
i < (kvm->arch.nested_mmus_size + kvm->arch.nested_mmus_next);
@@ -984,7 +1078,7 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
struct kvm_s2_mmu *mmu;
mmu = kvm->arch.nested_mmus[i % kvm->arch.nested_mmus_size];
- if (atomic_read(&mmu->refcnt))
+ if (atomic_read(&mmu->refcnt) || mmu->parked)
continue;
if (!kvm_s2_mmu_valid(mmu) || !mmu->accessed) {
@@ -1010,7 +1104,8 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
/* Every free MMU is in concurrent use: the pool is too small */
if (in_use && !READ_ONCE(kvm->arch.nested_mmus_no_grow) &&
- kvm->arch.nested_mmus_size < S2_MMU_MAX_PER_VM &&
+ (kvm->arch.nested_mmus_size < S2_MMU_MAX_PER_VM ||
+ kvm->arch.nested_mmus_parked) &&
nested_s2_mmus_budget_left()) {
WRITE_ONCE(kvm->arch.nested_mmus_grow, true);
kvm_make_request(KVM_REQ_NESTED_S2_GROW, vcpu);
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index c7d91138a6d4..df81adf96259 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -4047,6 +4047,7 @@ static bool handle_alle1is(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
* corresponding VMIDs.
*/
kvm_nested_s2_unmap(vcpu->kvm, true);
+ kvm_nested_s2_shrink(vcpu->kvm);
write_unlock(&vcpu->kvm->mmu_lock);
--
2.43.0
next prev parent reply other threads:[~2026-11-24 21:13 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 ` [RFC PATCH 2/6] KVM: arm64: nv: Pick the shadow S2 MMU to recycle with a clock Nathan Chen
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 ` Nathan Chen [this message]
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-6-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