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 27B15C04A6A for ; Fri, 4 Aug 2023 16:30:26 +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-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=XzZAr6vB8C4SKu3bboeiA6gettvwpbeG1fGTLJSklhk=; b=k6nS3FusJAEuA1 AKRzRmRn5fxDdSD/4Wcu7EACWQGXA19MozJOg0uO8wHgjMFdeuy5LHjs+syNDTc1BeFeSKR8QllPO FLiJA6yExNYl7reCgEDzuME2z67WD69zzjx0bcKyaYShqgdusIE0fVoNXWWZ5UTQcR8nFxZZY6DvT OQU+wzbNOykNyud0dbQ0svlWNbY+zITDY5Hmzkl2XY/VrN/k7e21JPcFacxfJXafXqOhkn5wVHJUm Y6lNk1JueV7FXWn2QkP3d1FMsrs5hpKcrOjIT86Im3EaXRdMXwQ4zeWuMRSWb+TMVF8tV09/ojy7H TVyXs3uAZPao8uszsPrQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qRxgi-00CnVo-0x; Fri, 04 Aug 2023 16:30:04 +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 1qRxge-00CnUs-2W for linux-arm-kernel@lists.infradead.org; Fri, 04 Aug 2023 16:30:02 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4657A62086; Fri, 4 Aug 2023 16:30:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C0E8C433C8; Fri, 4 Aug 2023 16:29:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691166599; bh=XL+TUm/cXgohgwGkYWj9XkK8SBBw8h6QNYCVPvifeQM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=HNYerY+yUIUJBz5YL0oOZ8vw55MUccK+08B8znHM0LONs65dW2ic1rSzHwx6ajgGc jl8uKCPWoGfqD8EjEMbf+rNUt7T+3nQDcqS3IGBKWABl3TdNTDlw882ZsHF7kQwCoI 2qoMaO7kMSp5bTblLT4kc6DTVUKYZ0v30bXDBPA2jRgAdzbWrfw+WnulloXXwoGEIf BF3J9iFyFgeFFJadXSVez5dSmS3LXXD3590tJ3Gx2XtgTb4+B27oYXtLUiIqKroYck AStuNSxw8qcTa6UdfdRf0YzlEswgjSeyxBib3OyMByOdUZ5ZJfjPsEVTN4jfGAeQi0 KUZ4/JeUBZOaA== Date: Fri, 4 Aug 2023 17:29:55 +0100 From: Will Deacon To: Waiman Long Cc: Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Robin Murphy Subject: Re: [PATCH v3] perf/arm-dmc620: Fix dmc620_pmu_irqs_lock/cpu_hotplug_lock circular lock dependency Message-ID: <20230804162954.GD30679@willie-the-truck> References: <20230722031729.3913953-1-longman@redhat.com> <20230728150614.GF21718@willie-the-truck> <62d4b353-0237-9ec6-a63e-8a7a6764aba5@redhat.com> <3e6a9ca3-3be5-8207-4923-8ecd141c04eb@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <3e6a9ca3-3be5-8207-4923-8ecd141c04eb@redhat.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230804_093000_911212_9FBE5222 X-CRM114-Status: GOOD ( 43.05 ) 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-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Aug 02, 2023 at 09:44:58PM -0400, Waiman Long wrote: > = > On 8/2/23 21:37, Waiman Long wrote: > > = > > On 7/28/23 11:06, Will Deacon wrote: > > > On Fri, Jul 21, 2023 at 11:17:28PM -0400, Waiman Long wrote: > > > > The following circular locking dependency was reported when running > > > > cpus online/offline test on an arm64 system. > > > > = > > > > [=A0=A0 84.195923] Chain exists of: > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 dmc620_pmu_irqs= _lock --> cpu_hotplug_lock --> > > > > cpuhp_state-down > > > > = > > > > [=A0=A0 84.207305]=A0 Possible unsafe locking scenario: > > > > = > > > > [=A0=A0 84.213212]=A0=A0=A0=A0=A0=A0=A0 CPU0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 CPU1 > > > > [=A0=A0 84.217729]=A0=A0=A0=A0=A0=A0=A0 ----=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 ---- > > > > [=A0=A0 84.222247]=A0=A0 lock(cpuhp_state-down); > > > > [=A0=A0 84.225899] lock(cpu_hotplug_lock); > > > > [=A0=A0 84.232068] lock(cpuhp_state-down); > > > > [=A0=A0 84.238237]=A0=A0 lock(dmc620_pmu_irqs_lock); > > > > [=A0=A0 84.242236] > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 *** DEADLOCK *** > > > > = > > > > The problematic locking order seems to be > > > > = > > > > =A0=A0=A0=A0lock(dmc620_pmu_irqs_lock) --> lock(cpu_hotplug_lock) > > > > = > > > > This locking order happens when dmc620_pmu_get_irq() is called from > > > > dmc620_pmu_device_probe(). Since dmc620_pmu_irqs_lock is used for > > > > protecting the dmc620_pmu_irqs structure only, we don't actually ne= ed > > > > to hold the lock when adding a new instance to the CPU hotplug > > > > subsystem. > > > > = > > > > Fix this possible deadlock scenario by releasing the lock before > > > > calling cpuhp_state_add_instance_nocalls() and reacquiring it > > > > afterward. > > > > To avoid the possibility of 2 racing dmc620_pmu_get_irq() calls > > > > inserting > > > > duplicated dmc620_pmu_irq structures with the same irq number, a du= mmy > > > > entry is inserted before releasing the lock which will block a > > > > competing > > > > thread from inserting another irq structure of the same irq number. > > > > = > > > > Suggested-by: Robin Murphy > > > > Signed-off-by: Waiman Long > > > > --- > > > > =A0 drivers/perf/arm_dmc620_pmu.c | 28 ++++++++++++++++++++++------ > > > > =A0 1 file changed, 22 insertions(+), 6 deletions(-) > > > > = > > > > diff --git a/drivers/perf/arm_dmc620_pmu.c > > > > b/drivers/perf/arm_dmc620_pmu.c > > > > index 9d0f01c4455a..7cafd4dd4522 100644 > > > > --- a/drivers/perf/arm_dmc620_pmu.c > > > > +++ b/drivers/perf/arm_dmc620_pmu.c > > > > @@ -76,6 +76,7 @@ struct dmc620_pmu_irq { > > > > =A0=A0=A0=A0=A0 refcount_t refcount; > > > > =A0=A0=A0=A0=A0 unsigned int irq_num; > > > > =A0=A0=A0=A0=A0 unsigned int cpu; > > > > +=A0=A0=A0 unsigned int valid; > > > > =A0 }; > > > > =A0 =A0 struct dmc620_pmu { > > > > @@ -423,9 +424,14 @@ static struct dmc620_pmu_irq > > > > *__dmc620_pmu_get_irq(int irq_num) > > > > =A0=A0=A0=A0=A0 struct dmc620_pmu_irq *irq; > > > > =A0=A0=A0=A0=A0 int ret; > > > > =A0 -=A0=A0=A0 list_for_each_entry(irq, &dmc620_pmu_irqs, irqs_node) > > > > -=A0=A0=A0=A0=A0=A0=A0 if (irq->irq_num =3D=3D irq_num && > > > > refcount_inc_not_zero(&irq->refcount)) > > > > +=A0=A0=A0 list_for_each_entry(irq, &dmc620_pmu_irqs, irqs_node) { > > > > +=A0=A0=A0=A0=A0=A0=A0 if (irq->irq_num !=3D irq_num) > > > > +=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 continue; > > > > +=A0=A0=A0=A0=A0=A0=A0 if (!irq->valid) > > > > +=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 return ERR_PTR(-EAGAIN);=A0=A0= =A0 /* Try again later */ > > > It looks like this can bubble up to the probe() routine. Does the dri= ver > > > core handle -EAGAIN coming back from a probe routine? > > Right, I should add code to handle this error condition. I think it can > > be handled in dmc620_pmu_get_irq(). The important thing is to release > > the mutex, wait a few ms and try again. What do you think? > > > = > > > > +=A0=A0=A0=A0=A0=A0=A0 if (refcount_inc_not_zero(&irq->refcount)) > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 return irq; > > > > +=A0=A0=A0 } > > > > =A0 =A0=A0=A0=A0=A0 irq =3D kzalloc(sizeof(*irq), GFP_KERNEL); > > > > =A0=A0=A0=A0=A0 if (!irq) > > > > @@ -447,13 +453,23 @@ static struct dmc620_pmu_irq > > > > *__dmc620_pmu_get_irq(int irq_num) > > > > =A0=A0=A0=A0=A0 if (ret) > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0 goto out_free_irq; > > > > =A0 -=A0=A0=A0 ret =3D cpuhp_state_add_instance_nocalls(cpuhp_state= _num, > > > > &irq->node); > > > > -=A0=A0=A0 if (ret) > > > > -=A0=A0=A0=A0=A0=A0=A0 goto out_free_irq; > > > > - > > > > =A0=A0=A0=A0=A0 irq->irq_num =3D irq_num; > > > > =A0=A0=A0=A0=A0 list_add(&irq->irqs_node, &dmc620_pmu_irqs); > > > > =A0 +=A0=A0=A0 /* > > > > +=A0=A0=A0=A0 * Release dmc620_pmu_irqs_lock before calling > > > > +=A0=A0=A0=A0 * cpuhp_state_add_instance_nocalls() and reacquire it= afterward. > > > > +=A0=A0=A0=A0 */ > > > > +=A0=A0=A0 mutex_unlock(&dmc620_pmu_irqs_lock); > > > > +=A0=A0=A0 ret =3D cpuhp_state_add_instance_nocalls(cpuhp_state_num, > > > > &irq->node); > > > > +=A0=A0=A0 mutex_lock(&dmc620_pmu_irqs_lock); > > > > + > > > > +=A0=A0=A0 if (ret) { > > > > +=A0=A0=A0=A0=A0=A0=A0 list_del(&irq->irqs_node); > > > > +=A0=A0=A0=A0=A0=A0=A0 goto out_free_irq; > > > > +=A0=A0=A0 } > > > > + > > > > +=A0=A0=A0 irq->valid =3D true; > > > Do you actually need a new flag here, or could we use a refcount of z= ero > > > to indicate that the irq descriptor is still being constructed? > > = > > A refcount of zero can also mean that an existing irq is about to be > > removed. Right? So I don't think we can use that for this purpose. > > Besides, there is a 4-byte hole in the structure anyway for arm64. > = > Alternatively, I can use a special reference count value, say -1, to sign= al > that the irq is not valid yet. What do you think? If the device is being removed, we should teardown the irq handler first, so I don't see why the refcount isn't the right thing. Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel