From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8CD6D483BC6; Thu, 13 Aug 2026 15:08:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786633737; cv=none; b=lhzPU+KbGKs8cppg1TKksBOtpe8ffU6KxY2E8p9IW2XVtEjoTdzsPW4nhNhojvlBr8z/BaZenswc4OXgnVm+S8J/uCi2CL2OUtMqXNa+GfQmnsuhvUCCQRCbSvKbwtVk7fSg++r1J44wZ84knegxe+AkR5H6F52520IlYWJgopU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786633737; c=relaxed/simple; bh=X/hoEMXmHicwjxg83ioroK4qXFBC2KkAYZtWR7Wfpv4=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=Y8rtcNW2xFxubbCRiUzRJu4cFb87n1OkiNbshW7rOgHdPIJq03XBMZZrxhE74Vosr8s5rbzSYW3esGyg7FgsXsHoTjdy4v0eT+lLqcwY3+3lSxT/kde9WVQ1/FCIxnFDdSrq2ErlUNOz0qJ5ieI4CsHtgQF9qi4PjmGyGz5OBtw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iu6zTI+7; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iu6zTI+7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7016D1F00A3A; Thu, 13 Aug 2026 15:08:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1786633736; bh=4s3K07ZR4jTg3+OEzdvHrniz3ZCVHL+rT3dEEJTt4Dg=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=iu6zTI+7xfJmVT111dFPsjfpICN2Yx9tbNufZUl1BhMSFCkBqI5rri3uah/FSJl+X YxsIF59K7x1iOW6rdLfpqRAeh1OURGDb7wOL7al409HSTs8MbQUKtb6+kzdoforFeu QJnnpIpn6ff9gru4+ewMgeDHOq9VyXk/aCmkfRahhht+E8DyBf1ef+eWR2pcy0kQsB moYAYpayztiVeIY95EOl03CHU7G3WvoCthS2bWWLHipuXp2F2jgZtMJX9VqgO5oyH0 R/yDkzypOndluS+TUj2Gf/NpQL3pisqGPgmbOLaUXJjqYznsDk03RsHbUPPXImQEl+ +Z+SwCu/kjCAA== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wuX38-0000000FMuq-20Q4; Thu, 13 Aug 2026 15:08:54 +0000 Date: Thu, 13 Aug 2026 16:08:54 +0100 Message-ID: <86zeyqys55.wl-maz@kernel.org> From: Marc Zyngier To: "Lorenzo Stoakes (ARM)" Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, Steffen Eiden , Joey Gouly , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Fuad Tabba , Shen Yongchao , Karl Mehltretter , stable@vger.kernel.org Subject: Re: [PATCH] KVM: arm64: nv: Fix life cycle of the nested_mmus array In-Reply-To: References: <20260811122057.754772-1-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: ljs@kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, seiden@linux.ibm.com, joey.gouly@arm.com, suzuki.poulose@arm.com, oupton@kernel.org, yuzenghui@huawei.com, fuad.tabba@linux.dev, grayhat@foxmail.com, kmehltretter@gmail.com, stable@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Wed, 12 Aug 2026 15:05:21 +0100, "Lorenzo Stoakes (ARM)" 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 > > Reported-by: Karl Mehltretter > > Suggested-by: Karl Mehltretter > > Link: https://lore.kernel.org/r/20260803224405.41468-1-kmehltretter@gmail.com > > Signed-off-by: Marc Zyngier > > 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.