From: Mahad Ibrahim <mahad.ibrahim.dev@gmail.com>
To: Keguang Zhang <keguang.zhang@gmail.com>, Vinod Koul <vkoul@kernel.org>
Cc: Frank Li <Frank.Li@kernel.org>,
linux-mips@vger.kernel.org, dmaengine@vger.kernel.org,
linux-kernel@vger.kernel.org,
Mahad Ibrahim <mahad.ibrahim.dev@gmail.com>
Subject: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()
Date: Wed, 29 Jul 2026 14:32:47 +0000 [thread overview]
Message-ID: <20260729143247.6111-1-mahad.ibrahim.dev@gmail.com> (raw)
In-Reply-To: <20260720142538.2766-1-mahad.ibrahim.dev@gmail.com>
ls1x_dma_tx_status() locates the descriptor actively being processed by
walking the LLI list and comparing the hardware reported next descriptor
pointer against each element's next-descriptor pointer.
A list_for_each_entry macro is used in the comparison phase. Which at
the end of the loop leaves the lli pointer at the currently executing LLI.
However this also subsequently runs for a non-match lli, in which it
points at the head. This causes a type confusion bug which treats the
head, which is a ls1x_dma_desc, as a ls1x_dma_lli object. Additionally it
goes forwards and prints garbage via the dev_dbg.
Fix the type confusion bug by only allowing matched LLI descriptor chains
to print the current LLI and residue calculation, as failing to match
should be treated as an unexpected condition.
Found by the following Coccinelle check:
scripts/coccinelle/iterators/use_after_iter.cocci
drivers/dma/loongson/loongson1-apb-dma.c:461:6-9: ERROR: invalid
reference to the index variable of the iterator on line 450
I did not see a bug upstream detailing this error, nor do I have the
hardware to confirm this bug or error, all this is from a pure code
examination.
As I do not possess the hardware, I cannot test the patch. Compile tested
only with mips64-linux-gnu-gcc.
Signed-off-by: Mahad Ibrahim <mahad.ibrahim.dev@gmail.com>
---
v2:
- encapsulate residue calculation and dev_dbg inside the
list_for_each_entry() macro. Treat non-matching LLI as an unexpected
case.
drivers/dma/loongson/loongson1-apb-dma.c | 27 ++++++++++++++----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/drivers/dma/loongson/loongson1-apb-dma.c b/drivers/dma/loongson/loongson1-apb-dma.c
index 89786cbd20ab..8658d5377795 100644
--- a/drivers/dma/loongson/loongson1-apb-dma.c
+++ b/drivers/dma/loongson/loongson1-apb-dma.c
@@ -446,22 +446,27 @@ static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan,
/* locate the current lli */
next_phys = chan->curr_lli->hw[LS1X_DMADESC_NEXT];
- list_for_each_entry(lli, &desc->lli_list, node)
- if (lli->hw[LS1X_DMADESC_NEXT] == next_phys)
- break;
+ list_for_each_entry(lli, &desc->lli_list, node) {
+ if (lli->hw[LS1X_DMADESC_NEXT] != next_phys)
+ continue;
- dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
- &lli->phys);
+ dev_dbg(chan2dev(dchan), "current lli_phys=%pad\n",
+ &lli->phys);
- /* count the residues */
- list_for_each_entry_from(lli, &desc->lli_list, node)
- bytes += lli->hw[LS1X_DMADESC_LENGTH] *
- chan->bus_width;
+ /* count the residues */
+ list_for_each_entry_from(lli, &desc->lli_list, node)
+ bytes += lli->hw[LS1X_DMADESC_LENGTH] *
+ chan->bus_width;
+
+ dma_set_residue(state, bytes);
+ return status;
+ }
+
+ dev_warn(chan2dev(dchan),
+ "unable to locate current lli.\n");
}
}
- dma_set_residue(state, bytes);
-
return status;
}
--
2.54.0
next prev parent reply other threads:[~2026-07-29 14:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-20 14:25 [PATCH] dmaengine: loongson1-apb-dma: fix residue calculation for queued descriptors Mahad Ibrahim
2026-07-20 14:40 ` sashiko-bot
2026-07-21 7:32 ` Keguang Zhang
2026-07-21 16:14 ` Mahad Ibrahim
2026-07-29 14:32 ` Mahad Ibrahim [this message]
2026-07-29 14:46 ` [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry() sashiko-bot
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=20260729143247.6111-1-mahad.ibrahim.dev@gmail.com \
--to=mahad.ibrahim.dev@gmail.com \
--cc=Frank.Li@kernel.org \
--cc=dmaengine@vger.kernel.org \
--cc=keguang.zhang@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=vkoul@kernel.org \
/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