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 1/6] KVM: arm64: nv: Allocate shadow S2 MMUs in blocks
Date: Tue, 24 Nov 2026 13:12:13 -0800 [thread overview]
Message-ID: <20261124211218.2751086-2-nathanc@nvidia.com> (raw)
In-Reply-To: <20261124211218.2751086-1-nathanc@nvidia.com>
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
next prev parent reply other threads:[~2026-11-24 21:12 UTC|newest]
Thread overview: 12+ 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 ` Nathan Chen [this message]
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:34 ` sashiko-bot
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:36 ` sashiko-bot
2026-11-24 21:12 ` [RFC PATCH 6/6] KVM: arm64: nv: Park shadow S2 MMUs emptied by a guest range invalidation Nathan Chen
2026-11-24 21:42 ` sashiko-bot
2026-11-24 22:29 ` [RFC PATCH 0/6] KVM: arm64: nv: Grow the shadow S2 MMU pool on demand Oliver Upton
2026-11-25 22:36 ` 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-2-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.