Return-Path: Received: from mail-sn1nam01on0134.outbound.protection.outlook.com ([104.47.32.134]:63889 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753525AbeARVCw (ORCPT ); Thu, 18 Jan 2018 16:02:52 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Iago Abal , Vinod Koul , "Sasha Levin" Subject: [added to the 4.1 stable tree] dmaengine: pl330: fix double lock Date: Thu, 18 Jan 2018 21:00:34 +0000 Message-ID: <20180118205908.3220-131-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Iago Abal This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit 91539eb1fda2d530d3b268eef542c5414e54bf1a ] The static bug finder EBA (http://www.iagoabal.eu/eba/) reported the following double-lock bug: Double lock: 1. spin_lock_irqsave(pch->lock, flags) at pl330_free_chan_resources:223= 6; 2. call to function `pl330_release_channel' immediately after; 3. call to function `dma_pl330_rqcb' in line 1753; 4. spin_lock_irqsave(pch->lock, flags) at dma_pl330_rqcb:1505. I have fixed it as suggested by Marek Szyprowski. First, I have replaced `pch->lock' with `pl330->lock' in functions `pl330_alloc_chan_resources' and `pl330_free_chan_resources'. This avoids the double-lock by acquiring a different lock than `dma_pl330_rqcb'. NOTE that, as a result, `pl330_free_chan_resources' executes `list_splice_tail_init' on `pch->work_list' under lock `pl330->lock', whereas in the rest of the code `pch->work_list' is protected by `pch->lock'. I don't know if this may cause race conditions. Similarly `pch->cyclic' is written by `pl330_alloc_chan_resources' under `pl330->lock' but read by `pl330_tx_submit' under `pch->lock'. Second, I have removed locking from `pl330_request_channel' and `pl330_release_channel' functions. Function `pl330_request_channel' is only called from `pl330_alloc_chan_resources', so the lock is already held. Function `pl330_release_channel' is called from `pl330_free_chan_resources', which already holds the lock, and from `pl330_del'. Function `pl330_del' is called in an error path of `pl330_probe' and at the end of `pl330_remove', but I assume that there cannot be concurrent accesses to the protected data at those points. Signed-off-by: Iago Abal Reviewed-by: Marek Szyprowski Signed-off-by: Vinod Koul Signed-off-by: Sasha Levin --- drivers/dma/pl330.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/drivers/dma/pl330.c b/drivers/dma/pl330.c index 3dabc52b9615..cb731749205a 100644 --- a/drivers/dma/pl330.c +++ b/drivers/dma/pl330.c @@ -1651,7 +1651,6 @@ static bool _chan_ns(const struct pl330_dmac *pl330, = int i) static struct pl330_thread *pl330_request_channel(struct pl330_dmac *pl330= ) { struct pl330_thread *thrd =3D NULL; - unsigned long flags; int chans, i; =20 if (pl330->state =3D=3D DYING) @@ -1659,8 +1658,6 @@ static struct pl330_thread *pl330_request_channel(str= uct pl330_dmac *pl330) =20 chans =3D pl330->pcfg.num_chan; =20 - spin_lock_irqsave(&pl330->lock, flags); - for (i =3D 0; i < chans; i++) { thrd =3D &pl330->channels[i]; if ((thrd->free) && (!_manager_ns(thrd) || @@ -1678,8 +1675,6 @@ static struct pl330_thread *pl330_request_channel(str= uct pl330_dmac *pl330) thrd =3D NULL; } =20 - spin_unlock_irqrestore(&pl330->lock, flags); - return thrd; } =20 @@ -1697,7 +1692,6 @@ static inline void _free_event(struct pl330_thread *t= hrd, int ev) static void pl330_release_channel(struct pl330_thread *thrd) { struct pl330_dmac *pl330; - unsigned long flags; =20 if (!thrd || thrd->free) return; @@ -1709,10 +1703,8 @@ static void pl330_release_channel(struct pl330_threa= d *thrd) =20 pl330 =3D thrd->dmac; =20 - spin_lock_irqsave(&pl330->lock, flags); _free_event(thrd, thrd->ev); thrd->free =3D true; - spin_unlock_irqrestore(&pl330->lock, flags); } =20 /* Initialize the structure for PL330 configuration, that can be used @@ -2077,20 +2069,20 @@ static int pl330_alloc_chan_resources(struct dma_ch= an *chan) struct pl330_dmac *pl330 =3D pch->dmac; unsigned long flags; =20 - spin_lock_irqsave(&pch->lock, flags); + spin_lock_irqsave(&pl330->lock, flags); =20 dma_cookie_init(chan); pch->cyclic =3D false; =20 pch->thread =3D pl330_request_channel(pl330); if (!pch->thread) { - spin_unlock_irqrestore(&pch->lock, flags); + spin_unlock_irqrestore(&pl330->lock, flags); return -ENOMEM; } =20 tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch); =20 - spin_unlock_irqrestore(&pch->lock, flags); + spin_unlock_irqrestore(&pl330->lock, flags); =20 return 1; } @@ -2188,12 +2180,13 @@ static int pl330_pause(struct dma_chan *chan) static void pl330_free_chan_resources(struct dma_chan *chan) { struct dma_pl330_chan *pch =3D to_pchan(chan); + struct pl330_dmac *pl330 =3D pch->dmac; unsigned long flags; =20 tasklet_kill(&pch->task); =20 pm_runtime_get_sync(pch->dmac->ddma.dev); - spin_lock_irqsave(&pch->lock, flags); + spin_lock_irqsave(&pl330->lock, flags); =20 pl330_release_channel(pch->thread); pch->thread =3D NULL; @@ -2201,7 +2194,7 @@ static void pl330_free_chan_resources(struct dma_chan= *chan) if (pch->cyclic) list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool); =20 - spin_unlock_irqrestore(&pch->lock, flags); + spin_unlock_irqrestore(&pl330->lock, flags); pm_runtime_mark_last_busy(pch->dmac->ddma.dev); pm_runtime_put_autosuspend(pch->dmac->ddma.dev); } --=20 2.11.0