From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 09396C959 for ; Thu, 23 Mar 2023 19:43:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AEF7CC433D2; Thu, 23 Mar 2023 19:43:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679600588; bh=f49JOMDkbt72qpChULG4UI3cnzeMUvCYd9RDzyBm0ho=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=umYQVhP5I8x+G0Vs96JviabDAZ1ZYmiURyrNBUQNZndhTdna73seyI5rThIQlt2Dz r6AgaAsCc8ybakZ3Hzt80hZZ4OETT2GhjsoodNUVhBZ+YqCP77Hod78EnNOYEYr/5e Y0x0ZIh0oUadq2iLN/0uFniDsxgFOOLYbyTqYaCh8JMQ8QVYSB8oMOprK+jQvKYjVp R7hRDF1Gz6/MfI1ctZETLJDapefhtkrGguDM0T9jEg7Hu7PrFTRwOc7oBEEN9jad6T YFK14+YDGxWhT38hINqcM+6vf3ExdOBgo21xYQ7GFAOBIL90H8fujhu2KM4B2vfBKU zB6pYs2fchCWA== Received: from disco-boy.misterjones.org ([51.254.78.96] helo=www.loen.fr) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pfQq1-002dCz-0a; Thu, 23 Mar 2023 19:43:06 +0000 Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 23 Mar 2023 19:43:04 +0000 From: Marc Zyngier To: Oliver Upton Cc: James Morse , Suzuki K Poulose , kvmarm@lists.linux.dev, Zenghui Yu , linux-arm-kernel@lists.infradead.org, Sean Christopherson , Jeremy Linton Subject: Re: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width In-Reply-To: References: <20230316211412.2651555-1-oliver.upton@linux.dev> <20230316211412.2651555-3-oliver.upton@linux.dev> <87lejpgfj3.wl-maz@kernel.org> User-Agent: Roundcube Webmail/1.4.13 Message-ID: <4f18d5264e4213f5a94a26bd8539eb0c@kernel.org> X-Sender: maz@kernel.org Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: 51.254.78.96 X-SA-Exim-Rcpt-To: oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, kvmarm@lists.linux.dev, yuzenghui@huawei.com, linux-arm-kernel@lists.infradead.org, seanjc@google.com, jeremy.linton@arm.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On 2023-03-23 19:20, Oliver Upton wrote: > On Wed, Mar 22, 2023 at 12:02:40PM +0000, Marc Zyngier wrote: >> On Thu, 16 Mar 2023 21:14:10 +0000, >> Oliver Upton wrote: >> > >> > kvm->lock must be taken outside of the vcpu->mutex. Of course, the >> > locking documentation for KVM makes this abundantly clear. Nonetheless, >> > the locking order in KVM/arm64 has been wrong for quite a while; we >> > acquire the kvm->lock while holding the vcpu->mutex all over the shop. >> > >> > All was seemingly fine until commit 42a90008f890 ("KVM: Ensure lockdep >> > knows about kvm->lock vs. vcpu->mutex ordering rule") caught us with our >> > pants down, leading to lockdep barfing: >> > >> > ====================================================== >> > WARNING: possible circular locking dependency detected >> > 6.2.0-rc7+ #19 Not tainted >> > ------------------------------------------------------ >> > qemu-system-aar/859 is trying to acquire lock: >> > ffff5aa69269eba0 (&host_kvm->lock){+.+.}-{3:3}, at: kvm_reset_vcpu+0x34/0x274 >> > >> > but task is already holding lock: >> > ffff5aa68768c0b8 (&vcpu->mutex){+.+.}-{3:3}, at: kvm_vcpu_ioctl+0x8c/0xba0 >> > >> > which lock already depends on the new lock. >> > >> > Add a dedicated lock to serialize writes to VM-scoped configuration from >> > the context of a vCPU. Protect the register width flags with the new >> > lock, thus avoiding the need to grab the kvm->lock while holding >> > vcpu->mutex in kvm_reset_vcpu(). >> > >> > Reported-by: Jeremy Linton >> > Link: https://lore.kernel.org/kvmarm/f6452cdd-65ff-34b8-bab0-5c06416da5f6@arm.com/ >> > Signed-off-by: Oliver Upton >> > --- >> > arch/arm64/include/asm/kvm_host.h | 3 +++ >> > arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ >> > arch/arm64/kvm/reset.c | 6 +++--- >> > 3 files changed, 24 insertions(+), 3 deletions(-) >> > >> > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h >> > index 917586237a4d..1f4b9708a775 100644 >> > --- a/arch/arm64/include/asm/kvm_host.h >> > +++ b/arch/arm64/include/asm/kvm_host.h >> > @@ -185,6 +185,9 @@ struct kvm_protected_vm { >> > }; >> > >> > struct kvm_arch { >> > + /* Protects VM-scoped configuration data */ >> > + struct mutex config_lock; >> > + >> >> nit: can we move this down into the structure and keep the MM stuff on >> its own at the top? Placing it next to the flags would make some >> sense, as these flags are definitely related to configuration matters. > > Sure thing! > >> > struct kvm_s2_mmu mmu; >> > >> > /* VTCR_EL2 value for this VM */ >> > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c >> > index 731a78f85915..1478bec52767 100644 >> > --- a/arch/arm64/kvm/arm.c >> > +++ b/arch/arm64/kvm/arm.c >> > @@ -128,6 +128,16 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) >> > { >> > int ret; >> > >> > + mutex_init(&kvm->arch.config_lock); >> > + >> > +#ifdef CONFIG_LOCKDEP >> > + /* Clue in lockdep that the config_lock must be taken inside kvm->lock */ >> > + mutex_lock(&kvm->lock); >> > + mutex_lock(&kvm->arch.config_lock); >> > + mutex_unlock(&kvm->arch.config_lock); >> > + mutex_unlock(&kvm->lock); >> > +#endif >> > + >> > ret = kvm_share_hyp(kvm, kvm + 1); >> > if (ret) >> > return ret; >> > @@ -328,6 +338,14 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) >> > >> > spin_lock_init(&vcpu->arch.mp_state_lock); >> > >> > +#ifdef CONFIG_LOCKDEP >> > + /* Inform lockdep that the config_lock is acquired after vcpu->mutex */ >> > + mutex_lock(&vcpu->mutex); >> > + mutex_lock(&vcpu->kvm->arch.config_lock); >> > + mutex_unlock(&vcpu->kvm->arch.config_lock); >> > + mutex_unlock(&vcpu->mutex); >> > +#endif >> >> Shouldn't this hunk be moved to the previous patch? > > Uh, I don't believe so since this is the patch that actually introduces > kvm_arch::config_lock. The last patch was aimed at a separate lock for > mp state. Nah, you're obviously right. I reviewed this at 4am being jet-lagged. Not the brightest comment... :-/ Sorry for the noise. M. -- Jazz is not dead. It just smells funny... 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 92EADC74A5B for ; Thu, 23 Mar 2023 19:44:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Message-ID:References:In-Reply-To:Subject:Cc:To:From :Date:MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=OFOBqF9Y+GMyRuCPsBEoOb2G2yX4Yi9h8fYWxMK/DMU=; b=VdU0LwixsPupuHtXobYrripe98 jnHzjQLqivgfSLq37LE7ApCTO8BhAXOJThVZIB+UQNn8MfzbYwwaTL/u1N0KR5FVXau73f9fHFfpe 6nxhgVHCYZXNmR/wNnCRLAdh/j/vvZRRGwQguiWbdK0qi8b64Yw4tTh4L8vk53XJQS+xdGEATYQn1 KCh03jphFLg1eaVlwLuRJf9zRBpbi0G0rAgxmi3gKXaZgK15EGn6buaM/R7sC+FOh84y+QWEB/OMX 98SY/YhWlWww+cOkdFUn5mP3XVY6pBkDDh1snKj9WKVBCiyM4Gx98/NprMX9Ssny1DC4/Klhm7JDX Cuu3z8hA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfQq9-002qvu-1i; Thu, 23 Mar 2023 19:43:13 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfQq6-002qvL-0A for linux-arm-kernel@lists.infradead.org; Thu, 23 Mar 2023 19:43:11 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4C3046287C; Thu, 23 Mar 2023 19:43:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AEF7CC433D2; Thu, 23 Mar 2023 19:43:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679600588; bh=f49JOMDkbt72qpChULG4UI3cnzeMUvCYd9RDzyBm0ho=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=umYQVhP5I8x+G0Vs96JviabDAZ1ZYmiURyrNBUQNZndhTdna73seyI5rThIQlt2Dz r6AgaAsCc8ybakZ3Hzt80hZZ4OETT2GhjsoodNUVhBZ+YqCP77Hod78EnNOYEYr/5e Y0x0ZIh0oUadq2iLN/0uFniDsxgFOOLYbyTqYaCh8JMQ8QVYSB8oMOprK+jQvKYjVp R7hRDF1Gz6/MfI1ctZETLJDapefhtkrGguDM0T9jEg7Hu7PrFTRwOc7oBEEN9jad6T YFK14+YDGxWhT38hINqcM+6vf3ExdOBgo21xYQ7GFAOBIL90H8fujhu2KM4B2vfBKU zB6pYs2fchCWA== Received: from disco-boy.misterjones.org ([51.254.78.96] helo=www.loen.fr) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pfQq1-002dCz-0a; Thu, 23 Mar 2023 19:43:06 +0000 MIME-Version: 1.0 Date: Thu, 23 Mar 2023 19:43:04 +0000 From: Marc Zyngier To: Oliver Upton Cc: James Morse , Suzuki K Poulose , kvmarm@lists.linux.dev, Zenghui Yu , linux-arm-kernel@lists.infradead.org, Sean Christopherson , Jeremy Linton Subject: Re: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width In-Reply-To: References: <20230316211412.2651555-1-oliver.upton@linux.dev> <20230316211412.2651555-3-oliver.upton@linux.dev> <87lejpgfj3.wl-maz@kernel.org> User-Agent: Roundcube Webmail/1.4.13 Message-ID: <4f18d5264e4213f5a94a26bd8539eb0c@kernel.org> X-Sender: maz@kernel.org X-SA-Exim-Connect-IP: 51.254.78.96 X-SA-Exim-Rcpt-To: oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, kvmarm@lists.linux.dev, yuzenghui@huawei.com, linux-arm-kernel@lists.infradead.org, seanjc@google.com, jeremy.linton@arm.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-20230323_124310_167797_7A489D8B X-CRM114-Status: GOOD ( 29.81 ) 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: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 2023-03-23 19:20, Oliver Upton wrote: > On Wed, Mar 22, 2023 at 12:02:40PM +0000, Marc Zyngier wrote: >> On Thu, 16 Mar 2023 21:14:10 +0000, >> Oliver Upton wrote: >> > >> > kvm->lock must be taken outside of the vcpu->mutex. Of course, the >> > locking documentation for KVM makes this abundantly clear. Nonetheless, >> > the locking order in KVM/arm64 has been wrong for quite a while; we >> > acquire the kvm->lock while holding the vcpu->mutex all over the shop. >> > >> > All was seemingly fine until commit 42a90008f890 ("KVM: Ensure lockdep >> > knows about kvm->lock vs. vcpu->mutex ordering rule") caught us with our >> > pants down, leading to lockdep barfing: >> > >> > ====================================================== >> > WARNING: possible circular locking dependency detected >> > 6.2.0-rc7+ #19 Not tainted >> > ------------------------------------------------------ >> > qemu-system-aar/859 is trying to acquire lock: >> > ffff5aa69269eba0 (&host_kvm->lock){+.+.}-{3:3}, at: kvm_reset_vcpu+0x34/0x274 >> > >> > but task is already holding lock: >> > ffff5aa68768c0b8 (&vcpu->mutex){+.+.}-{3:3}, at: kvm_vcpu_ioctl+0x8c/0xba0 >> > >> > which lock already depends on the new lock. >> > >> > Add a dedicated lock to serialize writes to VM-scoped configuration from >> > the context of a vCPU. Protect the register width flags with the new >> > lock, thus avoiding the need to grab the kvm->lock while holding >> > vcpu->mutex in kvm_reset_vcpu(). >> > >> > Reported-by: Jeremy Linton >> > Link: https://lore.kernel.org/kvmarm/f6452cdd-65ff-34b8-bab0-5c06416da5f6@arm.com/ >> > Signed-off-by: Oliver Upton >> > --- >> > arch/arm64/include/asm/kvm_host.h | 3 +++ >> > arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ >> > arch/arm64/kvm/reset.c | 6 +++--- >> > 3 files changed, 24 insertions(+), 3 deletions(-) >> > >> > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h >> > index 917586237a4d..1f4b9708a775 100644 >> > --- a/arch/arm64/include/asm/kvm_host.h >> > +++ b/arch/arm64/include/asm/kvm_host.h >> > @@ -185,6 +185,9 @@ struct kvm_protected_vm { >> > }; >> > >> > struct kvm_arch { >> > + /* Protects VM-scoped configuration data */ >> > + struct mutex config_lock; >> > + >> >> nit: can we move this down into the structure and keep the MM stuff on >> its own at the top? Placing it next to the flags would make some >> sense, as these flags are definitely related to configuration matters. > > Sure thing! > >> > struct kvm_s2_mmu mmu; >> > >> > /* VTCR_EL2 value for this VM */ >> > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c >> > index 731a78f85915..1478bec52767 100644 >> > --- a/arch/arm64/kvm/arm.c >> > +++ b/arch/arm64/kvm/arm.c >> > @@ -128,6 +128,16 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) >> > { >> > int ret; >> > >> > + mutex_init(&kvm->arch.config_lock); >> > + >> > +#ifdef CONFIG_LOCKDEP >> > + /* Clue in lockdep that the config_lock must be taken inside kvm->lock */ >> > + mutex_lock(&kvm->lock); >> > + mutex_lock(&kvm->arch.config_lock); >> > + mutex_unlock(&kvm->arch.config_lock); >> > + mutex_unlock(&kvm->lock); >> > +#endif >> > + >> > ret = kvm_share_hyp(kvm, kvm + 1); >> > if (ret) >> > return ret; >> > @@ -328,6 +338,14 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) >> > >> > spin_lock_init(&vcpu->arch.mp_state_lock); >> > >> > +#ifdef CONFIG_LOCKDEP >> > + /* Inform lockdep that the config_lock is acquired after vcpu->mutex */ >> > + mutex_lock(&vcpu->mutex); >> > + mutex_lock(&vcpu->kvm->arch.config_lock); >> > + mutex_unlock(&vcpu->kvm->arch.config_lock); >> > + mutex_unlock(&vcpu->mutex); >> > +#endif >> >> Shouldn't this hunk be moved to the previous patch? > > Uh, I don't believe so since this is the patch that actually introduces > kvm_arch::config_lock. The last patch was aimed at a separate lock for > mp state. Nah, you're obviously right. I reviewed this at 4am being jet-lagged. Not the brightest comment... :-/ Sorry for the noise. M. -- Jazz is not dead. It just smells funny... _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel