From: Marc Zyngier <maz@kernel.org>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
Steffen Eiden <seiden@linux.ibm.com>,
Joey Gouly <joey.gouly@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Oliver Upton <oupton@kernel.org>,
Zenghui Yu <yuzenghui@huawei.com>,
Fuad Tabba <fuad.tabba@linux.dev>,
Shen Yongchao <grayhat@foxmail.com>,
Karl Mehltretter <kmehltretter@gmail.com>,
stable@vger.kernel.org
Subject: Re: [PATCH] KVM: arm64: nv: Fix life cycle of the nested_mmus array
Date: Thu, 13 Aug 2026 16:08:54 +0100 [thread overview]
Message-ID: <86zeyqys55.wl-maz@kernel.org> (raw)
In-Reply-To: <anx6Wa5i1M2hidVK@gremlin>
On Wed, 12 Aug 2026 15:05:21 +0100,
"Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
>
> On Tue, Aug 11, 2026 at 01:20:57PM +0100, Marc Zyngier wrote:
> > The nested_mmus array holds the shadow page tables that are used when
> > a guest is running a nested context. These structures are allocated on
> > VCPU_INIT for whole guest, which implies that they may have to be
> > relocated as the array grows.
> >
> > Should a VCPU_INIT occur whilst a vcpu is actively running an L2 and
> > that the allocation requires relocation, that vcpu will still be
> > running with a pointer to the previous structure, which will have been
> > freed.
> >
> > Fix this by turning the array of structures to an array of pointers,
> > which is now allocated at VM creation, sized to the absolute maximum
> > that KVM can handle.
> >
> > In turn, each VCPU_INIT contributes S2_MMU_PER_VCPU to the pool. No
> > reallocation is ever performed, and the life cycle of each object is
> > much clearer:
> >
> > - the nested_mmus array is allocated in kvm_init_nested(), and freed
> > in kvm_arch_destroy_vm()
> >
> > - s2_mmu structures are allocated in kvm_vcpu_init_nested(), and freed
> > on kvm_arch_flush_shadow_all()
> >
> > Finally, the freeing of vcpu->arch.vncr_array is made consistent
> > rather than being done on some failure paths, but not others.
> >
> > Fixes: 4f128f8e1aaa ("KVM: arm64: nv: Support multiple nested Stage-2 mmu structures")
> > Reported-by: Shen Yongchao <grayhat@foxmail.com>
> > Reported-by: Karl Mehltretter <kmehltretter@gmail.com>
> > Suggested-by: Karl Mehltretter <kmehltretter@gmail.com>
> > Link: https://lore.kernel.org/r/20260803224405.41468-1-kmehltretter@gmail.com
> > Signed-off-by: Marc Zyngier <maz@kernel.org>
>
> This addresses the same kind of stuff I had a couple of patches in my
> series for :>)
>
> I think there are still some problems with it, see below.
>
> Also I attach my original patch for the UAF below in case it's useful! I
> had another for the init stuff, will reply with that separately also :)
>
> > Cc: stable@vger.kernel.org
> > ---
> >
> > Notes:
> > Sending this as a first class patch, since the other approaches were even
> > uglier than this one. I'm still displeased with kvm_arch_flush_shadow_all(),
> > but that's a step in the direction of tightening it:
>
[...]
> > @@ -1316,16 +1308,15 @@ void kvm_nested_s2_flush(struct kvm *kvm)
> >
> > void kvm_arch_flush_shadow_all(struct kvm *kvm)
> > {
> > - int i;
> > -
> > - for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
> > - struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
> > + for (int i = kvm->arch.nested_mmus_size - 1; i >= 0; i--) {
> > + struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
> >
> > if (!WARN_ON(atomic_read(&mmu->refcnt)))
> > kvm_free_stage2_pgd(mmu);
> > +
> > + if ((i % S2_MMU_PER_VCPU) == 0)
> > + kvfree(mmu);
>
> Hmm I think that this can still be referenced if a concurrent e.g. mmu notifier
> thread doing S2 nested teardown is referencing it?
Yes, but that's a separate fix, as per the notes above.
> Maybe defer this to kvm_arch_destroy_vm() also?
Sort off. I think we need two separate things here:
- In this particular callback, keep the tearing down of the shadow
S2s. They need to be emptied (after all, that's what the callback is
about), but the s2_mmu structures still need to exist, just as the
one that is embedded in the kvm structure doesn't disappear from
under our feet.
- in kvm_arch_destroy_vm(), free the suckers, because that's the only
safe point to do so.
>
> > }
> > - kvfree(kvm->arch.nested_mmus);
> > - kvm->arch.nested_mmus = NULL;
> > kvm->arch.nested_mmus_size = 0;
>
> This is racey (concurrent S2 teardown again) and should be
> done with the kvm->mmu_lock held I think.
>
> In my original patch (see below) I simply did:
>
> /* We may be raced by concurrent S2 teardown. */
> scoped_guard(write_lock, &kvm->mmu_lock)
> kvm->arch.nested_mmus_size = 0;
As explained above, I don't think this is the place to free the S2
MMUs at all.
I'll prepare some additional fixes for that.
Thanks,
M.
--
Without deviation from the norm, progress is not possible.
next prev parent reply other threads:[~2026-08-13 15:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-11 12:20 [PATCH] KVM: arm64: nv: Fix life cycle of the nested_mmus array Marc Zyngier
2026-08-12 11:21 ` Joey Gouly
2026-08-12 14:02 ` Marc Zyngier
2026-08-12 14:25 ` Lorenzo Stoakes (ARM)
2026-08-12 14:05 ` Lorenzo Stoakes (ARM)
2026-08-13 15:08 ` Marc Zyngier [this message]
2026-08-13 15:16 ` Lorenzo Stoakes (ARM)
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=86zeyqys55.wl-maz@kernel.org \
--to=maz@kernel.org \
--cc=fuad.tabba@linux.dev \
--cc=grayhat@foxmail.com \
--cc=joey.gouly@arm.com \
--cc=kmehltretter@gmail.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=ljs@kernel.org \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=stable@vger.kernel.org \
--cc=suzuki.poulose@arm.com \
--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