Re: [PATCH v3 2/2] dmaengine: mediatek: mtk-cqdma: read residue under locks in tx_status
From: Frank Li
Date: Wed Dec 02 2026 - 15:33:59 EST
On Wed, Dec 02, 2026 at 02:45:37PM +0800, Ziheng Wei wrote:
> mtk_cqdma_tx_status() looks up the active descriptor while holding
> cvc->pc->lock and cvc->vc.lock, then drops both locks before reading
> to_cqdma_vdesc(vd)->residue.
>
> Once the locks are released, mtk_cqdma_tasklet_cb() and vchan_complete()
> (or mtk_cqdma_terminate_all()) can concurrently update residue or free
> the descriptor, causing a use-after-free when reading cvd->residue.
>
> Read cvd->residue before releasing cvc->vc.lock and cvc->pc->lock.
>
> Found by LLM-assisted static analysis and compile-tested on x86_64.
>
> 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 out from the lookup fix into a separate patch (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 | 5 ++---
> 1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/dma/mediatek/mtk-cqdma.c b/drivers/dma/mediatek/mtk-cqdma.c
> index 758452c5d..1b4fb373f 100644
> --- a/drivers/dma/mediatek/mtk-cqdma.c
> +++ b/drivers/dma/mediatek/mtk-cqdma.c
> @@ -451,13 +451,12 @@ static enum dma_status mtk_cqdma_tx_status(struct dma_chan *c,
> 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);
>
> --
> 2.43.0
>