The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] dma: loongson2-apb-cmc: fix NULL deref in residue computation
@ 2026-05-07  2:31 Stepan Ionichev
  2026-05-07 17:50 ` [PATCH v2] " Stepan Ionichev
  2026-05-07 18:30 ` [PATCH] " Frank Li
  0 siblings, 2 replies; 4+ messages in thread
From: Stepan Ionichev @ 2026-05-07  2:31 UTC (permalink / raw)
  To: zhoubinbin; +Cc: vkoul, Frank.Li, dmaengine, linux-kernel, Stepan Ionichev

loongson2_cmc_dma_desc_residue() takes a "desc" parameter that is the
descriptor whose residue should be computed. The body uses it
correctly via "desc->num_sgs" and "desc->sg_req[i].len", but the
cyclic check incorrectly looks at the channel's stale current
descriptor instead:

	if (lchan->desc->cyclic && next_sg == 0)
		return residue;

This breaks when the function is called from the vdesc fallback path
of loongson2_cmc_dma_tx_status():

	if (lchan->desc && cookie == lchan->desc->vdesc.tx.cookie)
		state->residue = ...desc_residue(lchan, lchan->desc, ...);
	else if (vdesc)
		state->residue = ...desc_residue(lchan, to_lmdma_desc(vdesc), 0);

The else-if branch is taken precisely when "lchan->desc" is NULL or
points to a different descriptor than the one being queried, so
dereferencing "lchan->desc->cyclic" inside the helper either NULL-
derefs or reads the wrong descriptor's flag.

smatch flags this:

  drivers/dma/loongson/loongson2-apb-cmc-dma.c:516
  loongson2_cmc_dma_tx_status() error: we previously assumed
  'lchan->desc' could be null (see line 512)

Use the "desc" parameter, matching how the rest of the function
already accesses fields of the descriptor under inspection.

Signed-off-by: Stepan Ionichev <sozdayvek@gmail.com>
---
 drivers/dma/loongson/loongson2-apb-cmc-dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/dma/loongson/loongson2-apb-cmc-dma.c b/drivers/dma/loongson/loongson2-apb-cmc-dma.c
index 1c9a542ed..3b02bcd75 100644
--- a/drivers/dma/loongson/loongson2-apb-cmc-dma.c
+++ b/drivers/dma/loongson/loongson2-apb-cmc-dma.c
@@ -487,7 +487,7 @@ static size_t loongson2_cmc_dma_desc_residue(struct loongson2_cmc_dma_chan *lcha
 	ndtr = loongson2_cmc_dma_read(lddev, LOONGSON2_CMCDMA_CNDTR, lchan->id);
 	residue = ndtr << width;
 
-	if (lchan->desc->cyclic && next_sg == 0)
+	if (desc->cyclic && next_sg == 0)
 		return residue;
 
 	for (i = next_sg; i < desc->num_sgs; i++)
-- 
2.43.0


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

end of thread, other threads:[~2026-05-08  7:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-07  2:31 [PATCH] dma: loongson2-apb-cmc: fix NULL deref in residue computation Stepan Ionichev
2026-05-07 17:50 ` [PATCH v2] " Stepan Ionichev
2026-05-08  7:25   ` Binbin Zhou
2026-05-07 18:30 ` [PATCH] " Frank Li

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