From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 89A9BC5CFEB for ; Thu, 13 Aug 2026 15:09:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=4s3K07ZR4jTg3+OEzdvHrniz3ZCVHL+rT3dEEJTt4Dg=; b=IUzNUB98C0G29a/V8sY9TkQa8A c61pFMKMioSUk7ZT/pn7LpP2MrajYD+n9uS0fVd9G1SD+C9UP71bnUlww3MKRVItflarvYDmzZ6iv lt2WbU/liY8RjiHA5KpwrU32byyNuwBQ5MHmpw3Lg5DOiVjZEbednfB4Ds3i+g2ee0BXQ9P0nIZGQ buZcD1Vy74MjuGoi/7W2usBLtZrSFb6Zzw66gEp2D9ujDw1wbeUNBwhgV7rBVAGaV+EXqVscQ600x N93Oz3GyL3AWsiY0qPb6f1VsxVZPi7rbJgA9+jUtI+Ks8ggrsKVF6xNRiFiX72dR9RZAAwCHdKWJA nCXVGTRg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wuX3C-00000000vQj-0tG6; Thu, 13 Aug 2026 15:08:58 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wuX3A-00000000vQT-489S for linux-arm-kernel@lists.infradead.org; Thu, 13 Aug 2026 15:08:57 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 8EF6A42BA8; Thu, 13 Aug 2026 15:08:56 +0000 (UTC) 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) 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 X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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.