Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
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 4/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand
Date: Tue, 24 Nov 2026 13:12:16 -0800	[thread overview]
Message-ID: <20261124211218.2751086-5-nathanc@nvidia.com> (raw)
In-Reply-To: <20261124211218.2751086-1-nathanc@nvidia.com>

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



  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 ` Nathan Chen [this message]
2026-11-24 21:12 ` [RFC PATCH 5/6] KVM: arm64: nv: Park grown MMUs on ALLE1 and reactivate them on demand 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-5-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