From: sashiko-bot@kernel.org
To: "Nathan Chen" <nathanc@nvidia.com>, kvmarm@lists.linux.dev
Cc: "Oliver Upton" <oupton@kernel.org>, "Marc Zyngier" <maz@kernel.org>
Subject: Re: [RFC PATCH 5/6] KVM: arm64: nv: Park grown MMUs on ALLE1 and reactivate them on demand
Date: Tue, 24 Nov 2026 21:36:19 +0000 [thread overview]
Message-ID: <sashiko-outbox-232052@kernel.org> (raw)
In-Reply-To: <20261124211218.2751086-6-nathanc@nvidia.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] KVM: arm64: nv: kvm_vcpu_init_nested() counts grown and parked
MMUs as base
- [Medium] KVM: arm64: nv: nested_mmus_no_grow blocks reactivating
parked MMUs
- [Medium] KVM: arm64: nv: kvm_nested_add_s2_mmus() clobbers reactivated
clock hand
- [Medium] KVM: arm64: nv: stale guest_s2_mappings left behind by
kvm_nested_s2_park()
--
commit 9c4a135b779b29bdcbc1e942f3c2d4d37849957b
Author: Nathan Chen <nathanc@nvidia.com>
KVM: arm64: nv: Park grown MMUs on ALLE1 and reactivate them on demand
This commit parks unreferenced and empty grown shadow stage-2 MMUs on a
guest TLBI ALLE1 to return them to the host budget, and reactivates
parked MMUs before allocating new ones when growing the pool.
> diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
> index d530a37716be6..9251f3fd48405 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;
[Severity: Medium]
Can kvm_nested_s2_park() leave stale entries in mmu->guest_s2_mappings
when parking an MMU?
kvm_s2_mmu_empty() only checks mmu->pgt, which can be empty while
mmu->guest_s2_mappings still holds nodes (for example, if a guest stage-2
TLBI unmapped a subrange of a block mapping: stage2_unmap_walker() breaks
the entire block PTE in mmu->pgt, while kvm_remove_guest_s2_mappings()
keeps nodes larger than the unmap range).
Because kvm_nested_s2_park() marks the MMU invalid via
kvm_init_nested_s2_mmu() and clears mmu->pending_unmap, a later
reactivation and reuse in get_s2_mmu_nested() will not request
KVM_REQ_NESTED_S2_UNMAP, leaving the old tracking nodes attached to the
new nested context until a full unmap or VM teardown. Calling
kvm_remove_guest_s2_mappings(mmu, 0, kvm_phys_size(mmu)) before parking
would clear any remaining tracking entries.
[ ... ]
> @@ -216,14 +266,15 @@ static int kvm_nested_add_s2_mmus(struct kvm *kvm, int nr, bool grow)
> 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)) {
[Severity: Medium]
When kvm_nested_s2_grow() both reactivates parked MMUs (0 < reactivated <
nr) and allocates new ones (add > 0), does kvm_nested_add_s2_mmus()
clobber kvm->arch.nested_mmus_next = first?
kvm_nested_add_s2_mmus() unconditionally sets kvm->arch.nested_mmus_next
to the old nested_mmus_size. After get_s2_mmu_nested() hands out the new
MMUs at the tail, the clock hand wraps to index 0 and recycles live MMUs
in [0, first) whose accessed bits were just cleared by the pass that
triggered the grow, before reaching the empty reactivated MMUs at
[first, size).
[ ... ]
> @@ -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;
[Severity: High]
Can skipping parked MMUs here trigger BUG_ON(victim < 0) in
get_s2_mmu_nested() when vCPUs are initialized after the pool has grown?
arch/arm64/kvm/nested.c:kvm_vcpu_init_nested() {
...
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, false);
...
}
kvm_vcpu_init_nested() compares num_mmus against nested_mmus_size, which
includes grown and parked MMUs rather than just the base non-grown pool
(nested_mmus_size - nested_mmus_grown - nested_mmus_parked).
If vCPU 0 is created, initialized (nested_mmus_size == 2), and runs in
nested mode to grow the pool (for example by 4 MMUs to nested_mmus_size
== 6), and then executes TLBI ALLE1, kvm_nested_s2_park() parks all 4
grown MMUs (keep == 2, leaving 2 unparked MMUs). If userspace then
creates and initializes vCPU 1 and vCPU 2 (allowed before VGIC init or
when !irqchip_in_kernel(kvm)), num_mmus > kvm->arch.nested_mmus_size is
false, so no base MMUs are added and the 4 grown MMUs remain parked. The
VM now has 3 online vCPUs and only 2 unparked MMUs; when 2 vCPUs hold
references to those 2 MMUs and the 3rd calls get_s2_mmu_nested(), every
entry is either referenced or parked and the host hits BUG_ON(victim < 0).
Even when all vCPUs are created upfront, if vCPU 0 grows the pool before
the remaining vCPUs call KVM_ARM_VCPU_INIT, kvm_vcpu_init_nested() skips
allocating their base MMUs, and kvm_nested_s2_park() permanently retains
grown MMUs against the host budget to satisfy keep.
> @@ -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);
[Severity: Medium]
Does nested_mmus_no_grow prevent reactivating already-allocated parked
MMUs?
If a grow allocation in kvm_nested_add_s2_mmus() fails after the VM has
already accumulated grown MMUs, kvm_nested_s2_grow() sets
nested_mmus_no_grow = true. Once a later guest TLBI parks those earlier
grown MMUs, checking !READ_ONCE(kvm->arch.nested_mmus_no_grow) here and
at the start of kvm_nested_s2_grow() blocks reactivating them even though
reactivation needs no memory allocation, leaving their memory and stage-2
PGDs allocated but unusable until VM teardown.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124211218.2751086-1-nathanc@nvidia.com?part=5
next prev parent reply other threads:[~2026-11-24 21:36 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 ` [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: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 [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
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=sashiko-outbox-232052@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=maz@kernel.org \
--cc=nathanc@nvidia.com \
--cc=oupton@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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.