DMA Engine development
 help / color / mirror / Atom feed
* [PATCH v2] dmaengine: mediatek: mtk-cqdma: fix list entry type, channel check, and locking in tx_status
@ 2026-11-30 13:42 Ziheng Wei
  2026-12-01 20:17 ` Frank Li
  0 siblings, 1 reply; 2+ messages in thread
From: Ziheng Wei @ 2026-11-30 13:42 UTC (permalink / raw)
  To: Sean Wang, Vinod Koul, Frank Li, Matthias Brugger,
	AngeloGioacchino Del Regno
  Cc: dmaengine, linux-arm-kernel, linux-mediatek, linux-kernel, stable

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@vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03@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@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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] dmaengine: mediatek: mtk-cqdma: fix list entry type, channel check, and locking in tx_status
  2026-11-30 13:42 [PATCH v2] dmaengine: mediatek: mtk-cqdma: fix list entry type, channel check, and locking in tx_status Ziheng Wei
@ 2026-12-01 20:17 ` Frank Li
  0 siblings, 0 replies; 2+ messages in thread
From: Frank Li @ 2026-12-01 20:17 UTC (permalink / raw)
  To: Ziheng Wei
  Cc: Sean Wang, Vinod Koul, Frank Li, Matthias Brugger,
	AngeloGioacchino Del Regno, dmaengine, linux-arm-kernel,
	linux-mediatek, linux-kernel, stable

On Mon, Nov 30, 2026 at 09:42:45PM +0800, Ziheng Wei wrote:
> 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.

how it work previously?

>
> 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@vger.kernel.org
> Signed-off-by: Ziheng Wei <zihengw03@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@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);

Use two patches to fix it. one patch only fix one problem.

Frank

>
>  	dma_set_residue(txstate, bytes);
>
>
> base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 20:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 13:42 [PATCH v2] dmaengine: mediatek: mtk-cqdma: fix list entry type, channel check, and locking in tx_status Ziheng Wei
2026-12-01 20:17 ` Frank Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox