Skip to content

Commit 585a593

Browse files
TomRita999gregkh
authored andcommitted
dmaengine: mediatek: Fix a possible deadlock error in mtk_cqdma_tx_status()
[ Upstream commit 157ae5f ] Fix a potential deadlock bug. Observe that in the mtk-cqdma.c file, functions like mtk_cqdma_issue_pending() and mtk_cqdma_free_active_desc() properly acquire the pc lock before the vc lock when handling pc and vc fields. However, mtk_cqdma_tx_status() violates this order by first acquiring the vc lock before invoking mtk_cqdma_find_active_desc(), which subsequently takes the pc lock. This reversed locking sequence (vc → pc) contradicts the established pc → vc order and creates deadlock risks. Fix the issue by moving the vc lock acquisition code from mtk_cqdma_find_active_desc() to mtk_cqdma_tx_status(). Ensure the pc lock is acquired before the vc lock in the calling function to maintain correct locking hierarchy. Note that since mtk_cqdma_find_active_desc() is a static function with only one caller (mtk_cqdma_tx_status()), this modification safely eliminates the deadlock possibility without affecting other components. This possible bug is found by an experimental static analysis tool developed by our team. This tool analyzes the locking APIs to extract function pairs that can be concurrently executed, and then analyzes the instructions in the paired functions to identify possible concurrency bugs including deadlocks, data races and atomicity violations. Fixes: b1f01e4 ("dmaengine: mediatek: Add MediaTek Command-Queue DMA controller for MT6765 SoC") Cc: [email protected] Signed-off-by: Qiu-ji Chen <[email protected]> Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 523aefb commit 585a593

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

drivers/dma/mediatek/mtk-cqdma.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,11 @@ static struct virt_dma_desc *mtk_cqdma_find_active_desc(struct dma_chan *c,
420420
{
421421
struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c);
422422
struct virt_dma_desc *vd;
423-
unsigned long flags;
424423

425-
spin_lock_irqsave(&cvc->pc->lock, flags);
426424
list_for_each_entry(vd, &cvc->pc->queue, node)
427425
if (vd->tx.cookie == cookie) {
428-
spin_unlock_irqrestore(&cvc->pc->lock, flags);
429426
return vd;
430427
}
431-
spin_unlock_irqrestore(&cvc->pc->lock, flags);
432428

433429
list_for_each_entry(vd, &cvc->vc.desc_issued, node)
434430
if (vd->tx.cookie == cookie)
@@ -452,9 +448,11 @@ static enum dma_status mtk_cqdma_tx_status(struct dma_chan *c,
452448
if (ret == DMA_COMPLETE || !txstate)
453449
return ret;
454450

451+
spin_lock_irqsave(&cvc->pc->lock, flags);
455452
spin_lock_irqsave(&cvc->vc.lock, flags);
456453
vd = mtk_cqdma_find_active_desc(c, cookie);
457454
spin_unlock_irqrestore(&cvc->vc.lock, flags);
455+
spin_unlock_irqrestore(&cvc->pc->lock, flags);
458456

459457
if (vd) {
460458
cvd = to_cqdma_vdesc(vd);

0 commit comments

Comments
 (0)