Linux-mediatek Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Frank Li <Frank.li@oss.nxp.com>
To: Ziheng Wei <zihengw03@163.com>
Cc: Sean Wang <sean.wang@mediatek.com>, Vinod Koul <vkoul@kernel.org>,
	Frank Li <Frank.Li@kernel.org>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	dmaengine@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH v2] dmaengine: mediatek: mtk-cqdma: fix list entry type, channel check, and locking in tx_status
Date: Tue, 1 Dec 2026 14:17:44 -0600	[thread overview]
Message-ID: <aw8raE-BcEerThlO@SMW015318> (raw)
In-Reply-To: <20261130134245.1412154-1-zihengw03@163.com>

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
>


      reply	other threads:[~2026-12-01 20:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aw8raE-BcEerThlO@SMW015318 \
    --to=frank.li@oss.nxp.com \
    --cc=Frank.Li@kernel.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=sean.wang@mediatek.com \
    --cc=stable@vger.kernel.org \
    --cc=vkoul@kernel.org \
    --cc=zihengw03@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox