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 8311EC5320E for ; Mon, 19 Aug 2024 10:54:59 +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=vvXc9HAGHJgTZ9DLzYLaXhUGqgXKwp/2pHakQlwGPEs=; b=4MgVjFPxru9ELRiRUFL5DFXbQc hAxxdaElXog++MuxpQYDkC7F5z0XU7pYx1PUDQJQRkpDAseoG1N0cShWLVzomqPy1tpTbjIhjKd9t NGCrK8OAki3fR8FpcclF6MYST+gTjKrjX7XzfWQFs5mA3+CG7HV8LuBU7aDx6pKnT6hstZssFLWIX eLsGwzYEn+EevS8omkrJ5qRYAG/295RHhLiuOYdO5EqhfwVuAx6WTyy+57syF0BTUsqzmZwJQU62a dzVEDG9zGsUUjvCKFDvSb5X39gsjKu4vewQe+Mqr32kcdYBHiIPjrwtk0LPRpIE7OoNpkj96sv9n6 pUVmRA/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sg02A-000000019OU-0h2Y; Mon, 19 Aug 2024 10:54:46 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sg01U-000000019NV-20LV for linux-arm-kernel@lists.infradead.org; Mon, 19 Aug 2024 10:54:06 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id B3A1960BB7; Mon, 19 Aug 2024 10:54:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 649E9C32782; Mon, 19 Aug 2024 10:54:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724064843; bh=hfi2xWUQDHNzoOIDKBnqZk6fSJrAUllso94P3ypE0GY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=f+zEvglK+0ypG7S6lMy6tMUEZpH3BYeGaWjfMZ6fgjIv816gfbwt4PrGb0Y3Ph3or 6HfAKbu63DGiFRg4m0WX8hg0O07kMuebvxLhW8al/1xFZmbyoQuMkJg2IQX3uths/B r6vRlKZjnJ1N3XqrOKoJLBvy74kZV72jWNWyFmXOIvX0JOdUsLsDessHrGT3VpIcLT ssqsMLeEzFKOdQQETf02i35Xbmojchml9zh5NeGgRNFN4DnTMl+l0k5CNzMYYA1538 585KGto2LD/EO074Sxi/L6uhOIfVsQSqaAhwHHPZ1nOwReIMMpsF1idIw8Rq3ZjRnl jBEjXh846vblQ== 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.95) (envelope-from ) id 1sg01R-004shy-6Z; Mon, 19 Aug 2024 11:54:01 +0100 Date: Mon, 19 Aug 2024 11:53:59 +0100 Message-ID: <8634n0zsu0.wl-maz@kernel.org> From: Marc Zyngier To: Zenghui Yu Cc: , , James Morse , Suzuki K Poulose , Oliver Upton , Alexander Potapenko Subject: Re: [PATCH] KVM: arm64: vgic: Hold config_lock while tearing down a CPU interface In-Reply-To: References: <20240808091546.3262111-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/29.4 (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: yuzenghui@huawei.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, glider@google.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240819_035404_658262_59E1BA5B X-CRM114-Status: GOOD ( 28.55 ) 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, 19 Aug 2024 10:39:50 +0100, Zenghui Yu wrote: > > Hi Marc, > > On 2024/8/8 17:15, Marc Zyngier wrote: > > Tearing down a vcpu CPU interface involves freeing the private interrupt > > array. If we don't hold the lock, we may race against another thread > > trying to configure it. Yeah, fuzzers do wonderful things... > > > > Taking the lock early solves this particular problem. > > > > Fixes: 03b3d00a70b5 ("KVM: arm64: vgic: Allocate private interrupts on demand") > > Reported-by: Alexander Potapenko > > Tested-by: Alexander Potapenko > > Signed-off-by: Marc Zyngier > > --- > > arch/arm64/kvm/vgic/vgic-init.c | 3 +-- > > 1 file changed, 1 insertion(+), 2 deletions(-) > > > > diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c > > index 7f68cf58b978..41feb858ff9a 100644 > > --- a/arch/arm64/kvm/vgic/vgic-init.c > > +++ b/arch/arm64/kvm/vgic/vgic-init.c > > @@ -438,14 +438,13 @@ void kvm_vgic_destroy(struct kvm *kvm) > > unsigned long i; > > > > mutex_lock(&kvm->slots_lock); > > + mutex_lock(&kvm->arch.config_lock); > > > > vgic_debug_destroy(kvm); > > > > kvm_for_each_vcpu(i, vcpu, kvm) > > __kvm_vgic_vcpu_destroy(vcpu); > > > > - mutex_lock(&kvm->arch.config_lock); > > - > > kvm_vgic_dist_destroy(kvm); > > > > mutex_unlock(&kvm->arch.config_lock); > > The following splat was triggered when running the vgic_init selftest on > a lockdep kernel (I use rc4, with defconfig + PROVE_LOCKING). > > I'm not entirely sure that the splat is related to this change. Just > haven't got time to dig further so post it out early for record. Arghh. Thanks for reporting this. Can you try the following patch? It does the trick for me, but I don't trust myself anymore... M. diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c index 41feb858ff9a..e7c53e8af3d1 100644 --- a/arch/arm64/kvm/vgic/vgic-init.c +++ b/arch/arm64/kvm/vgic/vgic-init.c @@ -417,10 +417,8 @@ static void __kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) kfree(vgic_cpu->private_irqs); vgic_cpu->private_irqs = NULL; - if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) { - vgic_unregister_redist_iodev(vcpu); + if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF; - } } void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) @@ -448,6 +446,11 @@ void kvm_vgic_destroy(struct kvm *kvm) kvm_vgic_dist_destroy(kvm); mutex_unlock(&kvm->arch.config_lock); + + if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) + kvm_for_each_vcpu(i, vcpu, kvm) + vgic_unregister_redist_iodev(vcpu); + mutex_unlock(&kvm->slots_lock); } diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c index 2caa64415ff3..f50274fd5581 100644 --- a/arch/arm64/kvm/vgic/vgic.c +++ b/arch/arm64/kvm/vgic/vgic.c @@ -36,6 +36,11 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = { * we have to disable IRQs before taking this lock and everything lower * than it. * + * The config_lock has additional ordering requirements: + * kvm->slots_lock + * kvm->srcu + * kvm->arch.config_lock + * * If you need to take multiple locks, always take the upper lock first, * then the lower ones, e.g. first take the its_lock, then the irq_lock. * If you are already holding a lock and need to take a higher one, you -- Without deviation from the norm, progress is not possible.