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 X-Spam-Level: X-Spam-Status: No, score=-15.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C503C1B0E3 for ; Mon, 14 Dec 2020 12:56:05 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id EE9B92074B for ; Mon, 14 Dec 2020 12:56:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EE9B92074B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvmarm-bounces@lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 249464B5E8; Mon, 14 Dec 2020 07:56:04 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@redhat.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id V0kGMuLbPfUf; Mon, 14 Dec 2020 07:56:02 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id EC7824B5F0; Mon, 14 Dec 2020 07:56:02 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id AE5074B5C6 for ; Mon, 14 Dec 2020 07:56:01 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aQfKnO35qt5U for ; Mon, 14 Dec 2020 07:56:00 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mm01.cs.columbia.edu (Postfix) with ESMTP id BF8774B598 for ; Mon, 14 Dec 2020 07:56:00 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1607950560; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LStcWnrYVBS4YMoB7Kz3AAAbFjNBL/YMZWlSVmkr5VY=; b=KtzblyTQGe0HDZdNbmA8i4EIC3ir85y7hIcm8RWOmL1b740ozYDr8txIokI+6XrwiN6QqI UvCkLCT7jWrxB0lxbQOGYLqFaGnVkpkfSdUWnbbi1PGF3cs4h0CqKdlobI6DR95MrblJgJ 9H031MgOwJ8X+cxmoMOkc/egwRFMIWM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-397-gPWeCsjMOZq4HpO3VoR8Zg-1; Mon, 14 Dec 2020 07:55:56 -0500 X-MC-Unique: gPWeCsjMOZq4HpO3VoR8Zg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 34A6659; Mon, 14 Dec 2020 12:55:55 +0000 (UTC) Received: from [10.36.115.41] (ovpn-115-41.ams2.redhat.com [10.36.115.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 85A935C1BB; Mon, 14 Dec 2020 12:55:53 +0000 (UTC) Subject: Re: [PATCH 3/5] KVM: arm64: Move double-checked lock to kvm_vgic_map_resources() To: Alexandru Elisei , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com References: <20201201150157.223625-1-alexandru.elisei@arm.com> <20201201150157.223625-4-alexandru.elisei@arm.com> From: Auger Eric Message-ID: Date: Mon, 14 Dec 2020 13:55:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <20201201150157.223625-4-alexandru.elisei@arm.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eric.auger@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu Hi Alexandru, On 12/1/20 4:01 PM, Alexandru Elisei wrote: > kvm_vgic_map_resources() is called when a VCPU if first run and it maps all > the VGIC MMIO regions. To prevent double-initialization, the VGIC uses the > ready variable to keep track of the state of resources and the global KVM > mutex to protect against concurrent accesses. After the lock is taken, the > variable is checked again in case another VCPU took the lock between the > current VCPU reading ready equals false and taking the lock. > > The double-checked lock pattern is spread across four different functions: > in kvm_vcpu_first_run_init(), in kvm_vgic_map_resource() and in > vgic_{v2,v3}_map_resources(), which makes it hard to reason about and > introduces minor code duplication. Consolidate the checks in > kvm_vgic_map_resources(), where the lock is taken. > > No functional change intended. > > Signed-off-by: Alexandru Elisei > --- > arch/arm64/kvm/arm.c | 8 +++----- > arch/arm64/kvm/vgic/vgic-init.c | 6 ++++++ > arch/arm64/kvm/vgic/vgic-v2.c | 3 --- > arch/arm64/kvm/vgic/vgic-v3.c | 3 --- > 4 files changed, 9 insertions(+), 11 deletions(-) > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 9d69d2bf6943..65a5e89f5133 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -530,11 +530,9 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) > * Map the VGIC hardware resources before running a vcpu the > * first time on this VM. > */ > - if (unlikely(!vgic_ready(kvm))) { > - ret = kvm_vgic_map_resources(kvm); > - if (ret) > - return ret; > - } > + ret = kvm_vgic_map_resources(kvm); > + if (ret) > + return ret; > } else { > /* > * Tell the rest of the code that there are userspace irqchip > diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c > index 32e32d67a127..a2f4d1c85f00 100644 > --- a/arch/arm64/kvm/vgic/vgic-init.c > +++ b/arch/arm64/kvm/vgic/vgic-init.c > @@ -428,7 +428,13 @@ int kvm_vgic_map_resources(struct kvm *kvm) > struct vgic_dist *dist = &kvm->arch.vgic; > int ret = 0; > > + if (likely(vgic_ready(kvm))) > + return 0; > + > mutex_lock(&kvm->lock); > + if (vgic_ready(kvm)) > + goto out; > + While we are at it, the setting of dist->ready may be moved in kvm_vgic_map_resources() too. Thanks Eric > if (!irqchip_in_kernel(kvm)) > goto out; > > diff --git a/arch/arm64/kvm/vgic/vgic-v2.c b/arch/arm64/kvm/vgic/vgic-v2.c > index ebf53a4e1296..7f38c1a93639 100644 > --- a/arch/arm64/kvm/vgic/vgic-v2.c > +++ b/arch/arm64/kvm/vgic/vgic-v2.c > @@ -306,9 +306,6 @@ int vgic_v2_map_resources(struct kvm *kvm) > struct vgic_dist *dist = &kvm->arch.vgic; > int ret = 0; > > - if (vgic_ready(kvm)) > - goto out; > - > if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) || > IS_VGIC_ADDR_UNDEF(dist->vgic_cpu_base)) { > kvm_err("Need to set vgic cpu and dist addresses first\n"); > diff --git a/arch/arm64/kvm/vgic/vgic-v3.c b/arch/arm64/kvm/vgic/vgic-v3.c > index 9cdf39a94a63..35029c5cb0f1 100644 > --- a/arch/arm64/kvm/vgic/vgic-v3.c > +++ b/arch/arm64/kvm/vgic/vgic-v3.c > @@ -500,9 +500,6 @@ int vgic_v3_map_resources(struct kvm *kvm) > int ret = 0; > int c; > > - if (vgic_ready(kvm)) > - goto out; > - > kvm_for_each_vcpu(c, vcpu, kvm) { > struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; > > _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm 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 X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B00A9C4361B for ; Mon, 14 Dec 2020 12:57:22 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 57FB0206C0 for ; Mon, 14 Dec 2020 12:57:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 57FB0206C0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe :List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From:References:To:Subject: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Ravco3Vl+bh9L1/AXFwQi7rrCgEaWxiNNrqD6kZc77I=; b=C8aoJJ0V6k49Hda1i7SJfJaCed f7riedJ5P/OfO2pFGJJ+Kpz4ajPnxCx81whecBguTaWYbR2xvUQvTZ6+kSW6xmZ6DWVbLP34EaIU/ fWlOklhY4uC8RdAicQfjxrculs3XPiXcXnWxhwVRyHj/dJVCidBjwomg6ca3k5vOkg07wGDlfS0Tv JMhzAoAhPCp1QoABQrtCA/1zIG7UV6jcxO5jr7W0Ct8u5Zw8pqE3f1bnTW7Gs9hGqOTC+1i47XocK hCDkYFxD0iOurJ+/dnU2j/TNU0nx0poNz0L6RbWSTyntHM6qcNz0hJsTCr+1HcKfUOCQ4ufF2GnNy F+L71gSw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1konOU-0000NU-W5; Mon, 14 Dec 2020 12:56:03 +0000 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1konOT-0000Mk-2p for linux-arm-kernel@lists.infradead.org; Mon, 14 Dec 2020 12:56:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1607950560; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LStcWnrYVBS4YMoB7Kz3AAAbFjNBL/YMZWlSVmkr5VY=; b=KtzblyTQGe0HDZdNbmA8i4EIC3ir85y7hIcm8RWOmL1b740ozYDr8txIokI+6XrwiN6QqI UvCkLCT7jWrxB0lxbQOGYLqFaGnVkpkfSdUWnbbi1PGF3cs4h0CqKdlobI6DR95MrblJgJ 9H031MgOwJ8X+cxmoMOkc/egwRFMIWM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-397-gPWeCsjMOZq4HpO3VoR8Zg-1; Mon, 14 Dec 2020 07:55:56 -0500 X-MC-Unique: gPWeCsjMOZq4HpO3VoR8Zg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 34A6659; Mon, 14 Dec 2020 12:55:55 +0000 (UTC) Received: from [10.36.115.41] (ovpn-115-41.ams2.redhat.com [10.36.115.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 85A935C1BB; Mon, 14 Dec 2020 12:55:53 +0000 (UTC) Subject: Re: [PATCH 3/5] KVM: arm64: Move double-checked lock to kvm_vgic_map_resources() To: Alexandru Elisei , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com References: <20201201150157.223625-1-alexandru.elisei@arm.com> <20201201150157.223625-4-alexandru.elisei@arm.com> From: Auger Eric Message-ID: Date: Mon, 14 Dec 2020 13:55:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <20201201150157.223625-4-alexandru.elisei@arm.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eric.auger@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201214_075601_219446_F60D1F76 X-CRM114-Status: GOOD ( 27.77 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Alexandru, On 12/1/20 4:01 PM, Alexandru Elisei wrote: > kvm_vgic_map_resources() is called when a VCPU if first run and it maps all > the VGIC MMIO regions. To prevent double-initialization, the VGIC uses the > ready variable to keep track of the state of resources and the global KVM > mutex to protect against concurrent accesses. After the lock is taken, the > variable is checked again in case another VCPU took the lock between the > current VCPU reading ready equals false and taking the lock. > > The double-checked lock pattern is spread across four different functions: > in kvm_vcpu_first_run_init(), in kvm_vgic_map_resource() and in > vgic_{v2,v3}_map_resources(), which makes it hard to reason about and > introduces minor code duplication. Consolidate the checks in > kvm_vgic_map_resources(), where the lock is taken. > > No functional change intended. > > Signed-off-by: Alexandru Elisei > --- > arch/arm64/kvm/arm.c | 8 +++----- > arch/arm64/kvm/vgic/vgic-init.c | 6 ++++++ > arch/arm64/kvm/vgic/vgic-v2.c | 3 --- > arch/arm64/kvm/vgic/vgic-v3.c | 3 --- > 4 files changed, 9 insertions(+), 11 deletions(-) > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 9d69d2bf6943..65a5e89f5133 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -530,11 +530,9 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) > * Map the VGIC hardware resources before running a vcpu the > * first time on this VM. > */ > - if (unlikely(!vgic_ready(kvm))) { > - ret = kvm_vgic_map_resources(kvm); > - if (ret) > - return ret; > - } > + ret = kvm_vgic_map_resources(kvm); > + if (ret) > + return ret; > } else { > /* > * Tell the rest of the code that there are userspace irqchip > diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c > index 32e32d67a127..a2f4d1c85f00 100644 > --- a/arch/arm64/kvm/vgic/vgic-init.c > +++ b/arch/arm64/kvm/vgic/vgic-init.c > @@ -428,7 +428,13 @@ int kvm_vgic_map_resources(struct kvm *kvm) > struct vgic_dist *dist = &kvm->arch.vgic; > int ret = 0; > > + if (likely(vgic_ready(kvm))) > + return 0; > + > mutex_lock(&kvm->lock); > + if (vgic_ready(kvm)) > + goto out; > + While we are at it, the setting of dist->ready may be moved in kvm_vgic_map_resources() too. Thanks Eric > if (!irqchip_in_kernel(kvm)) > goto out; > > diff --git a/arch/arm64/kvm/vgic/vgic-v2.c b/arch/arm64/kvm/vgic/vgic-v2.c > index ebf53a4e1296..7f38c1a93639 100644 > --- a/arch/arm64/kvm/vgic/vgic-v2.c > +++ b/arch/arm64/kvm/vgic/vgic-v2.c > @@ -306,9 +306,6 @@ int vgic_v2_map_resources(struct kvm *kvm) > struct vgic_dist *dist = &kvm->arch.vgic; > int ret = 0; > > - if (vgic_ready(kvm)) > - goto out; > - > if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) || > IS_VGIC_ADDR_UNDEF(dist->vgic_cpu_base)) { > kvm_err("Need to set vgic cpu and dist addresses first\n"); > diff --git a/arch/arm64/kvm/vgic/vgic-v3.c b/arch/arm64/kvm/vgic/vgic-v3.c > index 9cdf39a94a63..35029c5cb0f1 100644 > --- a/arch/arm64/kvm/vgic/vgic-v3.c > +++ b/arch/arm64/kvm/vgic/vgic-v3.c > @@ -500,9 +500,6 @@ int vgic_v3_map_resources(struct kvm *kvm) > int ret = 0; > int c; > > - if (vgic_ready(kvm)) > - goto out; > - > kvm_for_each_vcpu(c, vcpu, kvm) { > struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; > > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel