DMA Engine development
 help / color / mirror / Atom feed
From: Chengfeng Ye <dg573847474@gmail.com>
To: logang@deltatee.com, vkoul@kernel.org
Cc: dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org,
	Chengfeng Ye <dg573847474@gmail.com>
Subject: [PATCH] dmaengine: plx_dma: Fix potential deadlock on &plxdev->ring_lock
Date: Wed, 26 Jul 2023 10:48:27 +0000	[thread overview]
Message-ID: <20230726104827.60382-1-dg573847474@gmail.com> (raw)

As plx_dma_process_desc() is invoked by both tasklet plx_dma_desc_task()
under softirq context and plx_dma_tx_status() callback that executed under
process context, the lock aquicision of &plxdev->ring_lock inside
plx_dma_process_desc() should disable irq otherwise deadlock could happen
if the irq preempts the execution of process context code while the lock
is held in process context on the same CPU.

Possible deadlock scenario:
plx_dma_tx_status()
    -> plx_dma_process_desc()
    -> spin_lock(&plxdev->ring_lock)
        <tasklet softirq>
        -> plx_dma_desc_task()
        -> plx_dma_process_desc()
        -> spin_lock(&plxdev->ring_lock) (deadlock here)

This flaw was found by an experimental static analysis tool I am developing
for irq-related deadlock.

The tentative patch fixes the potential deadlock by spin_lock_irqsave() in
plx_dma_process_desc() to disable irq while lock is held.

Signed-off-by: Chengfeng Ye <dg573847474@gmail.com>
---
 drivers/dma/plx_dma.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/dma/plx_dma.c b/drivers/dma/plx_dma.c
index 34b6416c3287..46068b8d83f9 100644
--- a/drivers/dma/plx_dma.c
+++ b/drivers/dma/plx_dma.c
@@ -135,9 +135,10 @@ static void plx_dma_process_desc(struct plx_dma_dev *plxdev)
 {
 	struct dmaengine_result res;
 	struct plx_dma_desc *desc;
+	unsigned long lock_flags;
 	u32 flags;
 
-	spin_lock(&plxdev->ring_lock);
+	spin_lock_irqsave(&plxdev->ring_lock, lock_flags);
 
 	while (plxdev->tail != plxdev->head) {
 		desc = plx_dma_get_desc(plxdev, plxdev->tail);
@@ -165,7 +166,7 @@ static void plx_dma_process_desc(struct plx_dma_dev *plxdev)
 		plxdev->tail++;
 	}
 
-	spin_unlock(&plxdev->ring_lock);
+	spin_unlock_irqrestore(&plxdev->ring_lock, lock_flags);
 }
 
 static void plx_dma_abort_desc(struct plx_dma_dev *plxdev)
-- 
2.17.1


             reply	other threads:[~2023-07-26 10:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-26 10:48 Chengfeng Ye [this message]
2023-07-26 10:58 ` [PATCH] dmaengine: plx_dma: Fix potential deadlock on &plxdev->ring_lock Chengfeng Ye
2023-07-26 15:57 ` Logan Gunthorpe
2023-07-26 21:00   ` Christophe JAILLET
2023-07-26 21:10     ` Logan Gunthorpe
2023-07-27  6:48       ` Chengfeng Ye
2023-07-27  9:45         ` Eric Schwarz
2023-07-27 14:34           ` Chengfeng Ye
2023-07-27 15:31         ` Logan Gunthorpe
2023-07-29 18:01           ` Chengfeng Ye

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230726104827.60382-1-dg573847474@gmail.com \
    --to=dg573847474@gmail.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=logang@deltatee.com \
    --cc=vkoul@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox