* [RFC PATCH 1/6] KVM: arm64: nv: Allocate shadow S2 MMUs in blocks
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 ` 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
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Nathan Chen @ 2026-11-24 21:12 UTC (permalink / raw)
To: linux-arm-kernel, kvmarm, linux-kernel
Cc: Mark Rutland, Will Deacon, Catalin Marinas, Zenghui Yu,
Suzuki K Poulose, Steffen Eiden, Joey Gouly, Fuad Tabba,
Oliver Upton, Marc Zyngier, Tom Matthias, Shameer Kolothum Thodi,
Jason Gunthorpe, Julian Miller
Shadow S2 MMUs are allocated S2_MMU_PER_VCPU at a time at vcpu init,
and kvm_destroy_nested() frees them by walking the pointer table with
that stride, as it assumes every allocation has the same size.
Allocate them through a helper that adds a block of any size to the
pool, and track the blocks on a list that kvm_destroy_nested() frees,
so that the pool can later grow by blocks of a different size.
The ptdump files of the new MMUs are created once they are published
in the table, rather than before.
No functional change otherwise.
Signed-off-by: Nathan Chen <nathanc@nvidia.com>
---
arch/arm64/include/asm/kvm_host.h | 4 +-
arch/arm64/kvm/nested.c | 84 ++++++++++++++++++++-----------
2 files changed, 57 insertions(+), 31 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 97332ffcb130..ff5073b13338 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -430,10 +430,12 @@ struct kvm_arch {
/*
* Stage 2 paging state for VMs with nested S2 using a virtual
- * VMID.
+ * VMID. The MMUs are allocated in blocks tracked on
+ * nested_mmu_blocks.
*/
struct kvm_s2_mmu **nested_mmus;
size_t nested_mmus_size;
+ struct list_head nested_mmu_blocks;
int nested_mmus_next;
/*
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 4d83281a7a28..7d78efee5296 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -47,8 +47,14 @@ struct vncr_tlb {
#define S2_MMU_PER_VCPU 2
#define S2_MMU_PER_VM (KVM_MAX_VCPUS * S2_MMU_PER_VCPU)
+struct nested_s2_mmu_block {
+ struct list_head list;
+ struct kvm_s2_mmu mmus[];
+};
+
int kvm_init_nested(struct kvm *kvm)
{
+ INIT_LIST_HEAD(&kvm->arch.nested_mmu_blocks);
kvm->arch.nested_mmus = kvmalloc_objs(struct kvm_s2_mmu *,
S2_MMU_PER_VM,
GFP_KERNEL_ACCOUNT);
@@ -61,8 +67,10 @@ int kvm_init_nested(struct kvm *kvm)
void kvm_destroy_nested(struct kvm *kvm)
{
- for (int i = 0; i < kvm->arch.nested_mmus_size; i+= S2_MMU_PER_VCPU)
- kvfree(kvm->arch.nested_mmus[i]);
+ struct nested_s2_mmu_block *blk, *tmp;
+
+ list_for_each_entry_safe(blk, tmp, &kvm->arch.nested_mmu_blocks, list)
+ kvfree(blk);
kvm->arch.nested_mmus_size = 0;
kvfree(kvm->arch.nested_mmus);
@@ -83,6 +91,49 @@ static int init_nested_s2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)
return kvm_init_stage2_mmu(kvm, mmu, kvm_get_pa_bits(kvm));
}
+/* Add a block of @nr shadow S2 MMUs to the pool */
+static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr)
+{
+ size_t size = kvm->arch.nested_mmus_size;
+ struct nested_s2_mmu_block *blk;
+ int i, ret = 0;
+
+ lockdep_assert_held(&kvm->arch.config_lock);
+
+ blk = kvzalloc_flex(*blk, mmus, nr, GFP_KERNEL_ACCOUNT);
+ if (!blk)
+ return -ENOMEM;
+
+ for (i = 0; i < nr; i++) {
+ ret = init_nested_s2_mmu(kvm, &blk->mmus[i]);
+ if (ret)
+ goto out_free;
+
+ blk->mmus[i].s2_mmu_idx = size + i;
+ }
+
+ scoped_guard(write_lock, &kvm->mmu_lock) {
+ for (i = 0; i < nr; i++)
+ kvm->arch.nested_mmus[size + i] = &blk->mmus[i];
+
+ kvm->arch.nested_mmus_size = size + nr;
+ }
+
+ list_add(&blk->list, &kvm->arch.nested_mmu_blocks);
+
+ for (i = 0; i < nr; i++)
+ kvm_nested_s2_ptdump_create_debugfs(&blk->mmus[i], size + i);
+
+ return 0;
+
+out_free:
+ while (i--)
+ kvm_free_stage2_pgd(&blk->mmus[i]);
+
+ kvfree(blk);
+ return ret;
+}
+
int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
{
struct kvm *kvm = vcpu->kvm;
@@ -104,40 +155,13 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
num_mmus = atomic_read(&kvm->online_vcpus) * S2_MMU_PER_VCPU;
if (num_mmus > kvm->arch.nested_mmus_size) {
- struct kvm_s2_mmu *tmp;
- int i, ret = 0;
-
- tmp = kvzalloc_objs(*tmp, S2_MMU_PER_VCPU, GFP_KERNEL_ACCOUNT);
- if (!tmp)
- ret = -ENOMEM;
-
- for (i = 0; !ret && i < S2_MMU_PER_VCPU; i++) {
- ret = init_nested_s2_mmu(kvm, &tmp[i]);
- if (ret)
- break;
- }
+ int ret = kvm_nested_add_s2_mmus(kvm, S2_MMU_PER_VCPU);
if (ret) {
- while (--i >= 0)
- kvm_free_stage2_pgd(&tmp[i]);
-
- kvfree(tmp);
free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
vcpu->arch.ctxt.vncr_array = NULL;
return ret;
}
-
- for (i = 0; i < S2_MMU_PER_VCPU; i++)
- kvm_nested_s2_ptdump_create_debugfs(&tmp[i], i + kvm->arch.nested_mmus_size);
-
- guard(write_lock)(&kvm->mmu_lock);
-
- for (i = 0; i < S2_MMU_PER_VCPU; i++) {
- tmp[i].s2_mmu_idx = i + kvm->arch.nested_mmus_size;
- kvm->arch.nested_mmus[i + kvm->arch.nested_mmus_size] = &tmp[i];
- }
-
- kvm->arch.nested_mmus_size += S2_MMU_PER_VCPU;
}
return 0;
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [RFC PATCH 2/6] KVM: arm64: nv: Pick the shadow S2 MMU to recycle with a clock
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
2026-11-24 21:12 ` [RFC PATCH 3/6] KVM: arm64: Add kvm_arm_vmid_nr_usable() Nathan Chen
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Nathan Chen @ 2026-11-24 21:12 UTC (permalink / raw)
To: linux-arm-kernel, kvmarm, linux-kernel
Cc: Mark Rutland, Will Deacon, Catalin Marinas, Zenghui Yu,
Suzuki K Poulose, Steffen Eiden, Joey Gouly, Fuad Tabba,
Oliver Upton, Marc Zyngier, Tom Matthias, Shameer Kolothum Thodi,
Jason Gunthorpe, Julian Miller
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [RFC PATCH 3/6] KVM: arm64: Add kvm_arm_vmid_nr_usable()
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 ` Nathan Chen
2026-11-24 21:12 ` [RFC PATCH 4/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Nathan Chen
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Nathan Chen @ 2026-11-24 21:12 UTC (permalink / raw)
To: linux-arm-kernel, kvmarm, linux-kernel
Cc: Mark Rutland, Will Deacon, Catalin Marinas, Zenghui Yu,
Suzuki K Poulose, Steffen Eiden, Joey Gouly, Fuad Tabba,
Oliver Upton, Marc Zyngier, Tom Matthias, Shameer Kolothum Thodi,
Jason Gunthorpe, Julian Miller
Growing the shadow stage-2 MMU pool of nested VMs needs a host-wide
limit derived from how many VMIDs the allocator can hand out within a
generation, before a rollover.
That is fewer than NUM_USER_VMIDS: VMID #0 is never allocated, and a
rollover can keep one VMID reserved per CPU for the VMID active on it.
Add kvm_arm_vmid_nr_usable() to return that number, so that the limit
is computed next to the allocator that sets it.
Signed-off-by: Nathan Chen <nathanc@nvidia.com>
---
arch/arm64/include/asm/kvm_host.h | 1 +
arch/arm64/kvm/vmid.c | 12 ++++++++++++
2 files changed, 13 insertions(+)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index c7133999296f..6ae86bc96600 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -1431,6 +1431,7 @@ int __init kvm_arm_vmid_alloc_init(void);
void __init kvm_arm_vmid_alloc_free(void);
void kvm_arm_vmid_update(struct kvm_vmid *kvm_vmid);
void kvm_arm_vmid_clear_active(void);
+unsigned int kvm_arm_vmid_nr_usable(void);
static inline void kvm_arm_pvtime_vcpu_init(struct kvm_vcpu_arch *vcpu_arch)
{
diff --git a/arch/arm64/kvm/vmid.c b/arch/arm64/kvm/vmid.c
index 7fe8ba1a2851..cf044f434531 100644
--- a/arch/arm64/kvm/vmid.c
+++ b/arch/arm64/kvm/vmid.c
@@ -129,6 +129,18 @@ static u64 new_vmid(struct kvm_vmid *kvm_vmid)
return vmid;
}
+/*
+ * Number of VMIDs that can be handed out within a generation: VMID #0
+ * is never allocated, and a rollover can keep one VMID reserved per CPU.
+ */
+unsigned int kvm_arm_vmid_nr_usable(void)
+{
+ if (NUM_USER_VMIDS - 1 <= num_possible_cpus())
+ return 0;
+
+ return NUM_USER_VMIDS - 1 - num_possible_cpus();
+}
+
/* Called from vCPU sched out with preemption disabled */
void kvm_arm_vmid_clear_active(void)
{
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [RFC PATCH 4/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand
2026-11-24 21:12 [RFC PATCH 0/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Nathan Chen
` (2 preceding siblings ...)
2026-11-24 21:12 ` [RFC PATCH 3/6] KVM: arm64: Add kvm_arm_vmid_nr_usable() Nathan Chen
@ 2026-11-24 21:12 ` 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
5 siblings, 0 replies; 7+ messages in thread
From: Nathan Chen @ 2026-11-24 21:12 UTC (permalink / raw)
To: linux-arm-kernel, kvmarm, linux-kernel
Cc: Mark Rutland, Will Deacon, Catalin Marinas, Zenghui Yu,
Suzuki K Poulose, Steffen Eiden, Joey Gouly, Fuad Tabba,
Oliver Upton, Marc Zyngier, Tom Matthias, Shameer Kolothum Thodi,
Jason Gunthorpe, Julian Miller
A nested-capable VM gets online_vcpus * S2_MMU_PER_VCPU shadow stage-2
MMUs, with S2_MMU_PER_VCPU currently hardcoded to 2. When the guest
hypervisor runs more than that number of concurrent nested VMs,
get_s2_mmu_nested() recycles live MMUs on nearly every nested context
switch, tearing down and rebuilding shadow stage-2 tables and issuing
VMID TLB invalidations each time. This causes a sharp spike in host CPU
usage.
Keep S2_MMU_PER_VCPU as the initial allocation, and grow the VM's pool
when the guest's working set outgrows it. To keep growth from exhausting
the VMID space, grown MMUs are counted across all VMs on the host and
limited to half of what kvm_arm_vmid_nr_usable() returns.
If an allocation fails, growth stops for good, so that a VM at its
memory limit doesn't retry on every recycle.
The nested_mmus pointer table is reallocated when full. Only the table
moves, not the MMUs, so cached hw_mmu pointers stay valid.
Signed-off-by: Nathan Chen <nathanc@nvidia.com>
---
arch/arm64/include/asm/kvm_host.h | 8 +-
arch/arm64/kvm/nested.c | 153 +++++++++++++++++++++++++++---
2 files changed, 148 insertions(+), 13 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 6ae86bc96600..9307b86285a5 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -62,6 +62,7 @@
#define KVM_REQ_MAP_L1_VNCR_EL2 KVM_ARCH_REQ(10)
#define KVM_REQ_VGIC_PROCESS_UPDATE KVM_ARCH_REQ(11)
#define KVM_REQ_RELOAD_GICv5 KVM_ARCH_REQ(12)
+#define KVM_REQ_NESTED_S2_GROW KVM_ARCH_REQ(13)
/* Bit indices for kvm_arch::flags */
enum {
@@ -434,12 +435,17 @@ struct kvm_arch {
/*
* Stage 2 paging state for VMs with nested S2 using a virtual
* VMID. The MMUs are allocated in blocks tracked on
- * nested_mmu_blocks.
+ * nested_mmu_blocks, and the pool grows on demand.
*/
struct kvm_s2_mmu **nested_mmus;
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 */
+ unsigned int nested_mmus_grown;
int nested_mmus_next;
+ bool nested_mmus_grow;
+ bool nested_mmus_no_grow;
/*
* Serializes guest s2 tracking trees access when the mmu_lock
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 44dc6780fe6f..d530a37716be 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -39,14 +39,58 @@ struct vncr_tlb {
};
/*
- * Ratio of live shadow S2 MMU per vcpu. This is a trade-off between
- * memory usage and potential number of different sets of S2 PTs in
- * the guests. Running out of S2 MMUs only affects performance (we
- * will invalidate them more often).
+ * Initial ratio of shadow S2 MMU per vcpu. When a vcpu has to recycle an
+ * MMU that is still in concurrent use, the VM's pool, which all its vcpus
+ * share, grows by one MMU per vcpu within the host-wide budget below.
+ * The pool then follows the number of different sets of S2 PTs the guest
+ * uses at the same time.
*/
#define S2_MMU_PER_VCPU 2
#define S2_MMU_PER_VM (KVM_MAX_VCPUS * S2_MMU_PER_VCPU)
+/* The MMU index is kept in the low bits of a tracked nested IPA */
+#define S2_MMU_MAX_PER_VM SZ_4K
+
+/*
+ * Each shadow S2 MMU that runs takes a host VMID, and VMIDs are shared
+ * by all VMs on the host. Running out of them causes a rollover, which
+ * invalidates the TLBs for every VM. MMUs added beyond the initial
+ * S2_MMU_PER_VCPU are therefore counted host-wide and limited to half
+ * of the VMIDs the allocator can hand out. Hosts with 8-bit VMIDs don't
+ * have enough to spare, so they never grow.
+ */
+static atomic_t nested_s2_mmus_grown;
+
+static int nested_s2_mmus_budget(void)
+{
+ if (kvm_arm_vmid_bits <= 8)
+ return 0;
+
+ return kvm_arm_vmid_nr_usable() / 2;
+}
+
+/* Lockless hint, checked on recycle before asking for a grow */
+static bool nested_s2_mmus_budget_left(void)
+{
+ return atomic_read(&nested_s2_mmus_grown) < nested_s2_mmus_budget();
+}
+
+/* Take up to @nr MMUs from the budget, returning how many were taken */
+static int nested_s2_mmus_budget_take(int nr)
+{
+ int budget = nested_s2_mmus_budget();
+ int old = atomic_read(&nested_s2_mmus_grown);
+ int take;
+
+ do {
+ take = min(nr, budget - old);
+ if (take <= 0)
+ return 0;
+ } while (!atomic_try_cmpxchg(&nested_s2_mmus_grown, &old, old + take));
+
+ return take;
+}
+
struct nested_s2_mmu_block {
struct list_head list;
struct kvm_s2_mmu mmus[];
@@ -55,10 +99,12 @@ struct nested_s2_mmu_block {
int kvm_init_nested(struct kvm *kvm)
{
INIT_LIST_HEAD(&kvm->arch.nested_mmu_blocks);
+ kvm->arch.nested_mmus_capacity = S2_MMU_PER_VM;
kvm->arch.nested_mmus = kvmalloc_objs(struct kvm_s2_mmu *,
- S2_MMU_PER_VM,
+ kvm->arch.nested_mmus_capacity,
GFP_KERNEL_ACCOUNT);
kvm->arch.nested_mmus_size = 0;
+ kvm->arch.nested_mmus_no_grow = !nested_s2_mmus_budget();
atomic_set(&kvm->arch.vncr_tlb_count, 0);
spin_lock_init(&kvm->arch.guest_s2_tracking_lock);
@@ -72,6 +118,8 @@ void kvm_destroy_nested(struct kvm *kvm)
list_for_each_entry_safe(blk, tmp, &kvm->arch.nested_mmu_blocks, list)
kvfree(blk);
+ atomic_sub(kvm->arch.nested_mmus_grown, &nested_s2_mmus_grown);
+ kvm->arch.nested_mmus_grown = 0;
kvm->arch.nested_mmus_size = 0;
kvfree(kvm->arch.nested_mmus);
}
@@ -91,16 +139,25 @@ static int init_nested_s2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu)
return kvm_init_stage2_mmu(kvm, mmu, kvm_get_pa_bits(kvm));
}
-/* Add a block of @nr shadow S2 MMUs to the pool */
-static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr)
+/*
+ * Add a block of @nr shadow S2 MMUs to the pool, reallocating the
+ * pointer table if it is full. Only the table moves, never the MMUs, so
+ * cached hw_mmu pointers stay valid. Runtime walkers of the table hold
+ * mmu_lock, and kvm_arch_flush_shadow_all() only runs once no vcpu can
+ * enter the guest, so it can't race with a grow.
+ */
+static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr, bool grow)
{
+ gfp_t gfp = GFP_KERNEL_ACCOUNT | (grow ? __GFP_NORETRY : 0);
size_t size = kvm->arch.nested_mmus_size;
+ size_t cap = kvm->arch.nested_mmus_capacity;
+ struct kvm_s2_mmu **table = NULL;
struct nested_s2_mmu_block *blk;
int i, ret = 0;
lockdep_assert_held(&kvm->arch.config_lock);
- blk = kvzalloc_flex(*blk, mmus, nr, GFP_KERNEL_ACCOUNT);
+ blk = kvzalloc_flex(*blk, mmus, nr, gfp);
if (!blk)
return -ENOMEM;
@@ -112,14 +169,36 @@ static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr)
blk->mmus[i].s2_mmu_idx = size + i;
}
+ if (size + nr > cap) {
+ cap = max(cap * 2, size + nr);
+ table = kvmalloc_objs(struct kvm_s2_mmu *, cap, gfp);
+ if (!table) {
+ ret = -ENOMEM;
+ goto out_free;
+ }
+ }
+
scoped_guard(write_lock, &kvm->mmu_lock) {
+ if (table) {
+ memcpy(table, kvm->arch.nested_mmus,
+ size_mul(size, sizeof(*table)));
+ swap(kvm->arch.nested_mmus, table);
+ kvm->arch.nested_mmus_capacity = cap;
+ }
+
for (i = 0; i < nr; i++)
kvm->arch.nested_mmus[size + i] = &blk->mmus[i];
kvm->arch.nested_mmus_size = size + nr;
+ if (grow)
+ kvm->arch.nested_mmus_grown += nr;
+
+ /* Hand out the new, unused MMUs before recycling live ones */
+ kvm->arch.nested_mmus_next = size;
}
list_add(&blk->list, &kvm->arch.nested_mmu_blocks);
+ kvfree(table);
for (i = 0; i < nr; i++)
kvm_nested_s2_ptdump_create_debugfs(&blk->mmus[i], size + i);
@@ -134,6 +213,42 @@ static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr)
return ret;
}
+/*
+ * 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
+ * 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;
+
+ guard(mutex)(&kvm->arch.config_lock);
+
+ /* Other vcpus may have asked too, before a grow or a failure */
+ if (!READ_ONCE(kvm->arch.nested_mmus_grow) ||
+ READ_ONCE(kvm->arch.nested_mmus_no_grow))
+ return;
+
+ nr = nested_s2_mmus_budget_take(atomic_read(&kvm->online_vcpus));
+ if (!nr) {
+ WRITE_ONCE(kvm->arch.nested_mmus_grow, false);
+ return;
+ }
+
+ add = min_t(int, nr, 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);
+}
+
int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
{
struct kvm *kvm = vcpu->kvm;
@@ -155,7 +270,7 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
num_mmus = atomic_read(&kvm->online_vcpus) * S2_MMU_PER_VCPU;
if (num_mmus > kvm->arch.nested_mmus_size) {
- int ret = kvm_nested_add_s2_mmus(kvm, S2_MMU_PER_VCPU);
+ int ret = kvm_nested_add_s2_mmus(kvm, S2_MMU_PER_VCPU, false);
if (ret) {
free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
@@ -842,6 +957,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;
+ bool in_use = true;
int i, victim = -1;
lockdep_assert_held_write(&vcpu->kvm->mmu_lock);
@@ -859,8 +975,8 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
* 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.
+ * chance. If every free MMU was used since the last pass, they
+ * are all in concurrent use and the pool is too small.
*/
for (i = kvm->arch.nested_mmus_next;
i < (kvm->arch.nested_mmus_size + kvm->arch.nested_mmus_next);
@@ -873,6 +989,7 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
if (!kvm_s2_mmu_valid(mmu) || !mmu->accessed) {
victim = i;
+ in_use = false;
break;
}
@@ -891,6 +1008,14 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
if (kvm_s2_mmu_valid(s2_mmu))
s2_mmu->pending_unmap = true;
+ /* 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 &&
+ nested_s2_mmus_budget_left()) {
+ WRITE_ONCE(kvm->arch.nested_mmus_grow, true);
+ kvm_make_request(KVM_REQ_NESTED_S2_GROW, vcpu);
+ }
+
/*
* The virtual VMID (modulo CnP) will be used as a key when matching
* an existing kvm_s2_mmu.
@@ -924,7 +1049,8 @@ static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu)
static void tag_s2_mapping_mmu(struct kvm_guest_s2_mapping *mapping,
struct kvm_s2_mmu *mmu)
{
- BUILD_BUG_ON(S2_MMU_PER_VM > SZ_4K);
+ BUILD_BUG_ON(S2_MMU_PER_VM > S2_MMU_MAX_PER_VM);
+ BUILD_BUG_ON(S2_MMU_MAX_PER_VM - 1 > S2_MMU_IDX_MASK);
mapping->nested.start &= ~S2_MMU_IDX_MASK;
mapping->nested.start |= mmu->s2_mmu_idx;
}
@@ -2210,6 +2336,9 @@ void check_nested_vcpu_requests(struct kvm_vcpu *vcpu)
write_unlock(&vcpu->kvm->mmu_lock);
}
+ if (kvm_check_request(KVM_REQ_NESTED_S2_GROW, vcpu))
+ kvm_nested_s2_grow(vcpu->kvm);
+
if (kvm_check_request(KVM_REQ_MAP_L1_VNCR_EL2, vcpu))
kvm_map_l1_vncr(vcpu);
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [RFC PATCH 5/6] KVM: arm64: nv: Park grown MMUs on ALLE1 and reactivate them on demand
2026-11-24 21:12 [RFC PATCH 0/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Nathan Chen
` (3 preceding siblings ...)
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
2026-11-24 21:12 ` [RFC PATCH 6/6] KVM: arm64: nv: Park shadow S2 MMUs emptied by a guest range invalidation Nathan Chen
5 siblings, 0 replies; 7+ messages in thread
From: Nathan Chen @ 2026-11-24 21:12 UTC (permalink / raw)
To: linux-arm-kernel, kvmarm, linux-kernel
Cc: Mark Rutland, Will Deacon, Catalin Marinas, Zenghui Yu,
Suzuki K Poulose, Steffen Eiden, Joey Gouly, Fuad Tabba,
Oliver Upton, Marc Zyngier, Tom Matthias, Shameer Kolothum Thodi,
Jason Gunthorpe, Julian Miller
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [RFC PATCH 6/6] KVM: arm64: nv: Park shadow S2 MMUs emptied by a guest range invalidation
2026-11-24 21:12 [RFC PATCH 0/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Nathan Chen
` (4 preceding siblings ...)
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 ` Nathan Chen
5 siblings, 0 replies; 7+ messages in thread
From: Nathan Chen @ 2026-11-24 21:12 UTC (permalink / raw)
To: linux-arm-kernel, kvmarm, linux-kernel
Cc: Mark Rutland, Will Deacon, Catalin Marinas, Zenghui Yu,
Suzuki K Poulose, Steffen Eiden, Joey Gouly, Fuad Tabba,
Oliver Upton, Marc Zyngier, Tom Matthias, Shameer Kolothum Thodi,
Jason Gunthorpe, Julian Miller
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@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
^ permalink raw reply related [flat|nested] 7+ messages in thread