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 70124CD8CAD for ; Tue, 9 Jun 2026 07:25:11 +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=Qpz3SdkUYISithjygHc1jib5Zu/rO+fgThVHu853SKI=; b=H3F3Hjt4BY1ks7Y1vOzax3eMNi HA76RRzIOSgP9oHdCzv0LqhqNRuH/BmAzpHmEge3+7iRJazP7axc5/fj4NtK+SwwSf2ShcB3qcn6O Zq/nRj/zjROQA0fnTrPq11Hm1Q+zx57Wrq7RQ45uuOcYR0YMImagpkmMYCgjqJ31T/laXHuaskhtz FfuYYCh5Js4zEovJc/lDtfTO65nHwhHAkZNE2vFXds4OmnjU9rvaYZSjxeOM0iV2g7Sly2YJt94fU DF/wT5jYB+FlIvW2QH/kaHt+BOhxYe2yWmyQkhpsb+eOt9yzd34Qof/x8u/S857a32riiPQkI1IKr xoBeKLlw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wWqpc-00000004xfa-3d7o; Tue, 09 Jun 2026 07:25:04 +0000 Received: from tor.source.kernel.org ([2600:3c04:e001:324:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wWqpb-00000004xfM-2zDz for linux-arm-kernel@lists.infradead.org; Tue, 09 Jun 2026 07:25:03 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 930E6601E6; Tue, 9 Jun 2026 07:25:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 48E8A1F00893; Tue, 9 Jun 2026 07:25:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780989902; bh=Qpz3SdkUYISithjygHc1jib5Zu/rO+fgThVHu853SKI=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=diCh9CroxRr1M/NLX82W9xpSc2vmG5xzr4jz6sRjKRyV3XXh9NeqKnYzFQfOH4Nwh sFr3o9p4StESVETMkgR1gEBFAB45lfCaAI1WOjXOS0XN0WonIcAZ/P14Ir0l03f2p3 4iGOvT+GtroGBJ9LiW/LCDivHr//MiOKARWqAn2j6YLXyJVXdmiMUs2+rrH9E7vy4i 6M38Tw6ftE8Mp2WVYv8dVbRFOz+StYiGze+nQRvPeTA7SI1n6Freqhf+yOckfJflyg XccN8eXWgnp5ZHfnoGW+C19PsttbJcQmNlj6LGr75guvbdyGcRUqNbWq/BIIreoerN ofDyyzCfYkyeg== 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 1wWqpY-0000000AnMP-1UFX; Tue, 09 Jun 2026 07:25:00 +0000 Date: Tue, 09 Jun 2026 08:25:00 +0100 Message-ID: <86ldcotbub.wl-maz@kernel.org> From: Marc Zyngier To: Yosry Ahmed Cc: kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Steffen Eiden , Joey Gouly , Suzuki K Poulose , Oliver Upton , Zenghui Yu Subject: Re: [PATCH] KVM: arm64: Hold kvm->mmu_lock while initialising vcpu->arch.vncr_tlb In-Reply-To: References: <20260608081108.2244133-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: yosry@kernel.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, seiden@linux.ibm.com, joey.gouly@arm.com, suzuki.poulose@arm.com, oupton@kernel.org, yuzenghui@huawei.com 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 Mon, 08 Jun 2026 21:55:25 +0100, Yosry Ahmed wrote: > > On Mon, Jun 08, 2026 at 09:11:08AM +0100, Marc Zyngier wrote: > > Sashiko reports that there is a race between initialising vncr_tlb > > and making use of it, as we don't hold the mmu_lock at this point. > > > > Additionally, it identifies a memory leak, should userspace repeatedly > > invokes the KVM_RUN ioctl after a failure of kvm_arch_vcpu_run_pid_change(), > > as we assign vncr_tlb blindly on first run, irrespective of prior > > allocations. > > > > Slap the two bugs in one go by taking the kvm->mmu_lock on assigning > > vncr_tlb, preventing the race for good, and by checking that vncr_tlb > > is indeed NULL prior to allocation. > > > > Reported-by: Sashiko > > Signed-off-by: Marc Zyngier > > Link: https://lore.kernel.org/r/20260607180815.85FBC1F00893@smtp.kernel.org > > --- > > arch/arm64/kvm/nested.c | 10 ++++++++-- > > 1 file changed, 8 insertions(+), 2 deletions(-) > > > > diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c > > index 690b8e8564166..d11e36b3cfcc2 100644 > > --- a/arch/arm64/kvm/nested.c > > +++ b/arch/arm64/kvm/nested.c > > @@ -1253,8 +1253,14 @@ int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu) > > if (!kvm_has_feat(vcpu->kvm, ID_AA64MMFR4_EL1, NV_frac, NV2_ONLY)) > > return 0; > > > > - vcpu->arch.vncr_tlb = kzalloc_obj(*vcpu->arch.vncr_tlb, > > - GFP_KERNEL_ACCOUNT); > > + if (!vcpu->arch.vncr_tlb) { > > + struct vncr_tlb *vt = kzalloc_obj(*vcpu->arch.vncr_tlb, > > + GFP_KERNEL_ACCOUNT); > > + > > + scoped_guard(write_lock, &vcpu->kvm->mmu_lock) > > + vcpu->arch.vncr_tlb = vt; > > + } > > (I am not familiar with this code at all, so apologies in advance if I > am making an idiot out of myself here) > > IIUC, the point of holding the lock here is *not* to protect against > concurrent initialization, as in this case the NULL check needs to be > done under the lock. > > Rather, the goal is to prevent re-ordering of zeroing from kzalloc and > the assignment to vcpu->arch.vncr_tlb, by depending on the barriers > provided by the lock. The lock is held by the readers so holding it here > conviently means we do not need to add any barriers to the readers. > > Is my understanding correct? It is correct. > > If yes, I think the code looks confusing, at least to a layman like > myself. It initially seems like the lock protects against concurrent > initializations, but then the NULL check is not done again under the > lock. The goal of the lock is not clear without the original report. > > Mayeb it's clearer to explicitly use barriers if the goal is preventing > reordering? This would require both the initialisation of vncr_tlb to use a store release, *and* all the other call sites to use a load acquire. I really don't think it is worth the churn, nor the (very small) burden on the readers. Thanks, M. -- Without deviation from the norm, progress is not possible.