Linux-mediatek Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup in tx_status
@ 2026-12-02  6:45 Ziheng Wei
  2026-12-02  6:45 ` [PATCH v3 2/2] dmaengine: mediatek: mtk-cqdma: read residue under locks " Ziheng Wei
  2026-12-02 20:31 ` [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup " Frank Li
  0 siblings, 2 replies; 4+ messages in thread
From: Ziheng Wei @ 2026-12-02  6:45 UTC (permalink / raw)
  To: Sean Wang, Vinod Koul
  Cc: Frank Li, Frank Li, Matthias Brugger, AngeloGioacchino Del Regno,
	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, 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@vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---
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@163.com
v1: https://lore.kernel.org/r/20261130103317.1383850-1-zihengw03@163.com

 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



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

* [PATCH v3 2/2] dmaengine: mediatek: mtk-cqdma: read residue under locks in tx_status
  2026-12-02  6:45 [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup in tx_status Ziheng Wei
@ 2026-12-02  6:45 ` Ziheng Wei
  2026-12-02 20:33   ` Frank Li
  2026-12-02 20:31 ` [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup " Frank Li
  1 sibling, 1 reply; 4+ messages in thread
From: Ziheng Wei @ 2026-12-02  6:45 UTC (permalink / raw)
  To: Sean Wang, Vinod Koul
  Cc: Frank Li, Frank Li, Matthias Brugger, AngeloGioacchino Del Regno,
	dmaengine, linux-arm-kernel, linux-mediatek, linux-kernel, stable

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@vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---
v2 -> v3:
- Split out from the lookup fix into a separate patch (Frank Li).

v2: https://lore.kernel.org/r/20261130134245.1412154-1-zihengw03@163.com
v1: https://lore.kernel.org/r/20261130103317.1383850-1-zihengw03@163.com

 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



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

* Re: [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup in tx_status
  2026-12-02  6:45 [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup in tx_status Ziheng Wei
  2026-12-02  6:45 ` [PATCH v3 2/2] dmaengine: mediatek: mtk-cqdma: read residue under locks " Ziheng Wei
@ 2026-12-02 20:31 ` Frank Li
  1 sibling, 0 replies; 4+ messages in thread
From: Frank Li @ 2026-12-02 20:31 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 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@vger.kernel.org
> Signed-off-by: Ziheng Wei <zihengw03@163.com>
> ---

Reviewed-by: Frank Li <Frank.Li@nxp.com>

> 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@163.com
> v1: https://lore.kernel.org/r/20261130103317.1383850-1-zihengw03@163.com
>
>  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
>


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

* Re: [PATCH v3 2/2] dmaengine: mediatek: mtk-cqdma: read residue under locks in tx_status
  2026-12-02  6:45 ` [PATCH v3 2/2] dmaengine: mediatek: mtk-cqdma: read residue under locks " Ziheng Wei
@ 2026-12-02 20:33   ` Frank Li
  0 siblings, 0 replies; 4+ messages in thread
From: Frank Li @ 2026-12-02 20:33 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 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@vger.kernel.org
> Signed-off-by: Ziheng Wei <zihengw03@163.com>
> ---

Reviewed-by: Frank Li <Frank.Li@nxp.com>

> v2 -> v3:
> - Split out from the lookup fix into a separate patch (Frank Li).
>
> v2: https://lore.kernel.org/r/20261130134245.1412154-1-zihengw03@163.com
> v1: https://lore.kernel.org/r/20261130103317.1383850-1-zihengw03@163.com
>
>  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
>


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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  6:45 [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup in tx_status Ziheng Wei
2026-12-02  6:45 ` [PATCH v3 2/2] dmaengine: mediatek: mtk-cqdma: read residue under locks " Ziheng Wei
2026-12-02 20:33   ` Frank Li
2026-12-02 20:31 ` [PATCH v3 1/2] dmaengine: mediatek: mtk-cqdma: fix active descriptor lookup " Frank Li

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