Re: [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup in tx_status

From: Frank Li

Date: Wed Dec 02 2026 - 15:32:09 EST


On Wed, Dec 02, 2026 at 02:45:36PM +0800, Ziheng Wei wrote:
> mtk_cqdma_issue_vchan_pending() links active descriptors onto pc->queue
> via struct mtk_cqdma_vdesc::node, whereas mtk_cqdma_find_active_desc()
> walks pc->queue using struct virt_dma_desc::node.
>
> Because the recovered pointer is shifted inside struct mtk_cqdma_vdesc,
> vd->tx.cookie reads an unrelated field instead of the descriptor cookie.
> In typical runs this fails to match cookie so mtk_cqdma_find_active_desc()
> returns NULL and mtk_cqdma_tx_status() silently reports zero residue,
> which hid the bug; if the misread field happens to match cookie,
> to_cqdma_vdesc(vd)->residue reads past the end of the allocation.
>
> In addition, multiple virtual channels share pc->queue and maintain
> independent per-channel cookies, so scanning pc->queue without checking
> cvd->ch == c can match another virtual channel's active descriptor.
>
> Walk pc->queue as struct mtk_cqdma_vdesc and check cvd->ch == c.
>
> 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@xxxxxxxxxxxxxxx
> Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
> ---

Reviewed-by: Frank Li <Frank.Li@xxxxxxx>

> v2 -> v3:
> - Split the lookup fix and locking fix into two patches (Frank Li).
> - Explain why the wrong list entry type went unnoticed previously
> (Frank Li).
>
> v2: https://lore.kernel.org/r/20261130134245.1412154-1-zihengw03@xxxxxxx
> v1: https://lore.kernel.org/r/20261130103317.1383850-1-zihengw03@xxxxxxx
>
> drivers/dma/mediatek/mtk-cqdma.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/dma/mediatek/mtk-cqdma.c b/drivers/dma/mediatek/mtk-cqdma.c
> index a7000608d..758452c5d 100644
> --- a/drivers/dma/mediatek/mtk-cqdma.c
> +++ b/drivers/dma/mediatek/mtk-cqdma.c
> @@ -419,12 +419,12 @@ static struct virt_dma_desc *mtk_cqdma_find_active_desc(struct dma_chan *c,
> 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)
>
> base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
> --
> 2.43.0
>