[PATCH v2] dmaengine: mediatek: mtk-cqdma: fix list entry type, channel check, and locking in tx_status

Ziheng Wei zihengw03 at 163.com
Mon Nov 30 05:42:45 PST 2026


mtk_cqdma_issue_vchan_pending() links active descriptors onto pc->queue via
struct mtk_cqdma_vdesc::node, but mtk_cqdma_find_active_desc() walks
pc->queue using struct virt_dma_desc::node. This shifts each recovered
descriptor pointer so vd->tx.cookie reads the wrong field; if it matches
cookie, to_cqdma_vdesc(vd)->residue reads past the end of the allocation,
and otherwise active transfers falsely report zero residue.

Because multiple virtual channels share pc->queue and maintain independent
per-channel cookies, matching pc->queue without checking cvd->ch == c can
also match another virtual channel's descriptor. Finally,
mtk_cqdma_tx_status() reads cvd->residue after dropping vc.lock and
pc->lock, racing with tasklet completion and descriptor free.

Walk pc->queue as struct mtk_cqdma_vdesc, check cvd->ch == c, and read
cvd->residue while holding vc.lock and pc->lock.

Found by LLM-assisted static analysis and runtime-tested in QEMU+KASAN
(slab-out-of-bounds before the fix; 0 KASAN findings after).

Fixes: b1f01e48df5a ("dmaengine: mediatek: Add MediaTek Command-Queue DMA controller for MT6765 SoC")
Cc: stable at vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03 at 163.com>
---
v1 -> v2:
- Also check cvd->ch == c when scanning the shared pc->queue so active
  descriptors from other virtual channels with the same cookie are not
  matched (thanks to Sashiko AI review).
- Add Cc: stable at vger.kernel.org.

diff --git a/drivers/dma/mediatek/mtk-cqdma.c b/drivers/dma/mediatek/mtk-cqdma.c
--- a/drivers/dma/mediatek/mtk-cqdma.c
+++ b/drivers/dma/mediatek/mtk-cqdma.c
@@ -419,12 +419,12 @@
 							dma_cookie_t cookie)
 {
 	struct mtk_cqdma_vchan *cvc = to_cqdma_vchan(c);
+	struct mtk_cqdma_vdesc *cvd;
 	struct virt_dma_desc *vd;
 
-	list_for_each_entry(vd, &cvc->pc->queue, node)
-		if (vd->tx.cookie == cookie) {
-			return vd;
-		}
+	list_for_each_entry(cvd, &cvc->pc->queue, node)
+		if (cvd->ch == c && cvd->vd.tx.cookie == cookie)
+			return &cvd->vd;
 
 	list_for_each_entry(vd, &cvc->vc.desc_issued, node)
 		if (vd->tx.cookie == cookie)
@@ -451,13 +451,12 @@
 	spin_lock_irqsave(&cvc->pc->lock, flags);
 	spin_lock(&cvc->vc.lock);
 	vd = mtk_cqdma_find_active_desc(c, cookie);
-	spin_unlock(&cvc->vc.lock);
-	spin_unlock_irqrestore(&cvc->pc->lock, flags);
-
 	if (vd) {
 		cvd = to_cqdma_vdesc(vd);
 		bytes = cvd->residue;
 	}
+	spin_unlock(&cvc->vc.lock);
+	spin_unlock_irqrestore(&cvc->pc->lock, flags);
 
 	dma_set_residue(txstate, bytes);
 

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa




More information about the linux-arm-kernel mailing list