Skip to contentSkip to conversation

dmaengine: loongson1-apb-dma: fix residue calculation for queued descriptors

PATCH In review Mahad Ibrahim 5 participants 14 messages Jul 20 – Sep 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dmaengine: loongson1-apb-dma: fix residue calculation for queued descriptors

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 internally expands to the container_of macro to find the current ls1x_dma_lli's next-descriptor pointer to compare against. However a problem occurs when the loop reaches the final lli list element and wraps around to the head. The head is a different struct ls1x_dma_desc. It goes down to the offset the node member would be at in ls1x_dma_lli inside of ls1x_dma_desc.

This leaves the lli pointer somewhere inside of ls1x_dma_desc at the end of the loop. The subsequent phys debug value read therefore would print garbage as a valid phys address.

The primary bug occurs in the residue calculation list_for_each_entry_from macro. The lli pointer still points to somewhere inside of ls1x_dma_desc, when the list_for_each_entry_from performs the same wrong offset calculation on ls1x_dma_desc to determine the stop condition of the loop, it lands at the same offset that lli pointer is on, and the loop immediately stops.

This produces a 0 residue value, while it should be the entire length of the lli list chain. The driver advertises DMA_RESIDUE_GRANULARITY_SEGMENT, so a 0 residue would mean the transfer has completed.

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

Note the check still reports this line after the fix; the remaining use is safe (the cursor is only dereferenced on the match path, or reset to the first entry otherwise).

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.

---
 drivers/dma/loongson/loongson1-apb-dma.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/dma/loongson/loongson1-apb-dma.c +11 -4
index 89786cbd20ab..8bb6e6e5719d 100644
@@ -439,6 +439,7 @@ static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan,
 			struct ls1x_dma_desc *desc = to_ls1x_dma_desc(vd);
 			struct ls1x_dma_lli *lli;
 			dma_addr_t next_phys;
+			bool found = false;
 
 			/* get the current lli */
 			if (ls1x_dma_query(chan, &chan->curr_lli->phys))
@@ -447,11 +448,17 @@ 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)
+				if (lli->hw[LS1X_DMADESC_NEXT] == next_phys) {
+					found = true;
 					break;
-
-			dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
-				&lli->phys);
+				}
+
+			if (!found)
+				lli = list_first_entry(&desc->lli_list,
+						       struct ls1x_dma_lli, node);
+			else
+				dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
+					&lli->phys);
 
 			/* count the residues */
 			list_for_each_entry_from(lli, &desc->lli_list, node)
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:

Pre-existing issues:
- [High] Calling issue_pending while a transfer is active forcefully restarts the active transfer from the beginning.
- [High] The DMA IRQ handler fails to start the next pending descriptor when the current transfer completes.
- [High] Zero-length scatterlists or cyclic configurations with buf_len < period_len cause NULL pointer dereferences and memory corruption.
- [High] IRQ and devres leak on resource allocation failure.
- [High] Lockless read-modify-write race on the shared global LS1X_DMA_CTRL register corrupts commands and stops wrong channels.
- [High] Idle hardware causes false-positive LLI matches, reporting partial residue for pending transfers.
- [Medium] The driver stalls the CPU for up to 3ms inside a spinlock_irqsave critical section.
- [Medium] The driver tracks bus_width globally per channel rather than per descriptor, corrupting residue calculations. --

--- Patch [1]: [PATCH] dmaengine: loongson1-apb-dma: fix residue calculation for queued descriptors --- commit 62e04cef060bef1de9eb6ca4a2d6840c563a954f
Author: Mahad Ibrahim <[email protected]>

dmaengine: loongson1-apb-dma: fix residue calculation for queued descriptors

This commit fixes a bug in ls1x_dma_tx_status() where the loop locating the current descriptor could wrap around to the list head. The wrapped pointer caused the subsequent list_for_each_entry_from() to stop immediately, resulting in a residue of 0 being reported.

Quoted 5 lines
drivers/dma/loongson/loongson1-apb-dma.c +0 -0
index 89786cbd20ab5..8bb6e6e5719df 100644
@@ -439,6 +439,7 @@ static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan,

[ ... ]

drivers/dma/loongson/loongson1-apb-dma.c static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan, · line 454

Quoted 11 lines
drivers/dma/loongson/loongson1-apb-dma.c +3 -4
 			/* 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)
+				if (lli->hw[LS1X_DMADESC_NEXT] == next_phys) {
+					found = true;
 					break;
-
-			dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
-				&lli->phys);
+				}

[Severity: High]
This is a pre-existing issue, but might idle hardware cause false-positive LLI matches and bypass the newly added fallback logic?

The final LLI of all non-cyclic descriptors is initialized with NEXT == 0. When the hardware is idle after a previous transfer, its next_phys register remains 0.

If a client queries the status of a pending, unstarted descriptor, will the driver incorrectly match next_phys = 0 to the descriptor's final LLI, setting found = true, and report only the length of the last segment as the residue instead of the full size?

drivers/dma/loongson/loongson1-apb-dma.c static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan, · line 464

Quoted 10 lines
drivers/dma/loongson/loongson1-apb-dma.c +7 -0
+
+			if (!found)
+				lli = list_first_entry(&desc->lli_list,
+						       struct ls1x_dma_lli, node);
+			else
+				dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
+					&lli->phys);
 
 			/* count the residues */
 			list_for_each_entry_from(lli, &desc->lli_list, node)

[Severity: Medium]
This isn't a bug introduced by this patch, but will tracking bus_width globally per channel rather than per descriptor corrupt residue calculations?

In ls1x_dma_prep_lli(), the bus width for a specific transfer is cached globally in chan->bus_width. Later, ls1x_dma_tx_status() calculates the residue:

	bytes += lli->hw[LS1X_DMADESC_LENGTH] * chan->bus_width;

If a client queues multiple transfers with different configurations or directions on the same channel, will querying the status of older transfers use the width of the most recently prepared transfer and result in an incorrect byte count?

[Severity: High]
This is a pre-existing issue, but does calling vchan_issue_pending() while a transfer is active forcefully restart the active transfer from the beginning?

When vchan_issue_pending() is called, it moves submitted descriptors to the issued list and returns true if the list is not empty.

In ls1x_dma_issue_pending(), it unconditionally fetches the first descriptor:

	if (vchan_issue_pending(&chan->vc)) {
		struct virt_dma_desc *vd = vchan_next_desc(&chan->vc);
		...
		ls1x_dma_start(chan, &lli->phys);
	}

Because vchan_next_desc() returns the currently running transfer (it is only removed upon completion), this immediately calls ls1x_dma_start() and instructs the hardware to begin a new transfer at the active descriptor's start address.

Can this destroy the current hardware state machine and restart the ongoing transfer, leading to data corruption?

[Severity: High]
This isn't a bug introduced by this patch, but is it possible that the DMA IRQ handler fails to start the next pending descriptor when the current transfer completes?

When a non-cyclic transfer completes, ls1x_dma_irq_handler() removes the completed descriptor from the issued list and marks it complete:

	} else {
		list_del(&vd->node);
		vchan_cookie_complete(vd);
	}

It does not fetch the next queued descriptor or call ls1x_dma_start() to initiate the next transfer.

If a client submits multiple descriptors, will the channel stall indefinitely after finishing the first one?

[Severity: High]
This is a pre-existing issue, but can zero-length scatterlists or cyclic configurations with buf_len < period_len cause NULL pointer dereferences and memory corruption?

In ls1x_dma_prep_dma_cyclic(), if buf_len < period_len, sg_len becomes 0. ls1x_dma_prep_lli() is then called with sg_len == 0, bypassing the iteration loop, leaving lli and first pointers uninitialized.

If is_cyclic is true, the driver immediately dereferences the NULL first pointer:

	if (is_cyclic) {
		lli->hw[LS1X_DMADESC_NEXT] = first->phys | LS1X_DMA_NEXT_VALID;
		chan->is_cyclic = is_cyclic;
	}

If is_cyclic is false, it returns success with an empty lli_list. When ls1x_dma_issue_pending() processes it:

	lli = list_first_entry(&desc->lli_list, struct ls1x_dma_lli, node);
	ls1x_dma_start(chan, &lli->phys);

Can this force list_first_entry() to return a garbage offset and instruct the hardware to execute from random memory addresses?

[Severity: High]
This isn't a bug introduced by this patch, but is there an IRQ and devres leak on resource allocation failure?

ls1x_dma_alloc_chan_resources() dynamically registers an IRQ handler using devm_request_irq().

If subsequent allocations like dma_pool_create() fail:

	if (!chan->lli_pool)
		return -ENOMEM;

It returns an error without calling devm_free_irq(). The DMA engine core handles the failure but does not call the corresponding free_chan_resources() callback on allocation failure.

Will the IRQ handler remain permanently registered, allowing devm allocations to accumulate and potentially exhausting memory or registering duplicate handlers upon repeated client requests?

[Severity: High]
This is a pre-existing issue, but can a lockless read-modify-write race on the shared global LS1X_DMA_CTRL register corrupt commands and stop wrong channels?

The LS1X_DMA_CTRL register is a single global register mapped to chan->reg_base and shared across all DMA channels. ls1x_dma_stop() performs a lockless read-modify-write:

	int val = readl(chan->reg_base + LS1X_DMA_CTRL);

	writel(val | LS1X_DMA_STOP, chan->reg_base + LS1X_DMA_CTRL);

Because it does not clear the previous channel ID field in val, does it send the STOP command to whichever channel was last written to the register, even if ls1x_dma_stop() was called for a different channel?

Will concurrent operations using per-channel locks in ls1x_dma_start() and ls1x_dma_query() also silently race and corrupt the register?

[Severity: Medium]
This isn't a bug introduced by this patch, but could stalling the CPU for up to 3ms inside a spinlock_irqsave() critical section be problematic?

ls1x_dma_tx_status() acquires a per-channel spinlock and disables interrupts. It then calls ls1x_dma_query(), which loops polling the hardware:

	ret = readl_poll_timeout_atomic(chan->reg_base + LS1X_DMA_CTRL, val,
					!(val & LS1X_DMA_ASK_VALID), 0, 3000);

Can stalling a CPU for 3000 microseconds with interrupts masked degrade system latency and trigger soft lockup detectors under worst-case hardware delays?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Mahad Ibrahim

Hi Mahad,

Thanks for catching this use-after-iterator issue.

Quoted 69 lines · Mahad Ibrahim

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 internally expands to the container_of macro to find the current ls1x_dma_lli's next-descriptor pointer to compare against. However a problem occurs when the loop reaches the final lli list element and wraps around to the head. The head is a different struct ls1x_dma_desc. It goes down to the offset the node member would be at in ls1x_dma_lli inside of ls1x_dma_desc.

This leaves the lli pointer somewhere inside of ls1x_dma_desc at the end of the loop. The subsequent phys debug value read therefore would print garbage as a valid phys address.

The primary bug occurs in the residue calculation list_for_each_entry_from macro. The lli pointer still points to somewhere inside of ls1x_dma_desc, when the list_for_each_entry_from performs the same wrong offset calculation on ls1x_dma_desc to determine the stop condition of the loop, it lands at the same offset that lli pointer is on, and the loop immediately stops.

This produces a 0 residue value, while it should be the entire length of the lli list chain. The driver advertises DMA_RESIDUE_GRANULARITY_SEGMENT, so a 0 residue would mean the transfer has completed.

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

Note the check still reports this line after the fix; the remaining use is safe (the cursor is only dereferenced on the match path, or reset to the first entry otherwise).

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.

---
 drivers/dma/loongson/loongson1-apb-dma.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/dma/loongson/loongson1-apb-dma.c +2 -1
index 89786cbd20ab..8bb6e6e5719d 100644
@@ -439,6 +439,7 @@ static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan,
                        struct ls1x_dma_desc *desc = to_ls1x_dma_desc(vd);
                        struct ls1x_dma_lli *lli;
                        dma_addr_t next_phys;
+                       bool found = false;
 
                        /* get the current lli */
                        if (ls1x_dma_query(chan, &chan->curr_lli->phys))
@@ -447,11 +448,17 @@ 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)
+                               if (lli->hw[LS1X_DMADESC_NEXT] == next_phys) {

drivers/dma/loongson/loongson1-apb-dma.c static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan, · line 458

Quoted 10 lines
drivers/dma/loongson/loongson1-apb-dma.c +6 -3
+                                       found = true;
                                        break;
-
-                       dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
-                               &lli->phys);
+                               }
+
+                       if (!found)
+                               lli = list_first_entry(&desc->lli_list,
+                                                      struct ls1x_dma_lli, node);

However, I don't think falling back to list_first_entry() when no match is found is appropriate. Since next_phys should always match one of the LLIs in the descriptor chain, failing to find a match should be treated as an unexpected condition rather than silently falling back to the first LLI.
I would suggest something like:

       scoped_guard(spinlock_irqsave, &chan->vc.lock) {
               vd = vchan_find_desc(&chan->vc, cookie);
               if (vd) {
                       struct ls1x_dma_desc *desc = to_ls1x_dma_desc(vd);
                       struct ls1x_dma_lli *lli;
                       dma_addr_t next_phys;

                       /* get the current lli */
                       if (ls1x_dma_query(chan, &chan->curr_lli->phys))
                               return status;

                       /* 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)
                                       continue;

                               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;

                               dma_set_residue(state, bytes);
                               return status;
                       }

                       dev_warn(chan2dev(dchan), "unable to locate
current LLI\n");
               }
       }

       return status;
Quoted 9 lines
drivers/dma/loongson/loongson1-apb-dma.c +3 -1
+                       else
+                               dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
+                                       &lli->phys);
 
                        /* count the residues */
                        list_for_each_entry_from(lli, &desc->lli_list, node)
--

2.54.0

Signature · 3 lines
Best regards,

Keguang Zhang
#3 lore raw

Hello Keguang,

Thank you for the quick response and feedback. I'll adopt the single-loop structure.

Additionally, Sashiko pointed out another possible flaw, which occurs when the hardware is idle. The hw[LS1X_DMADESC_NEXT] of the last terminator LLI in a chain is 0 and the current next_phys the hardware returns with

    next_phys = chan->curr_lli->hw[LS1X_DMADESC_NEXT];

is also 0. Which would match, however I do not think it's the preferred behavior, the residue would only be the length of the last LLI. If you wish I can add it as an additional patch on top of the single-loop structure.

Best regards,

Mahad Ibrahim

Quoted 146 lines · Keguang Zhang

Hi Mahad,

Thanks for catching this use-after-iterator issue.

Quoted 69 lines · Mahad Ibrahim

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 internally expands to the container_of macro to find the current ls1x_dma_lli's next-descriptor pointer to compare against. However a problem occurs when the loop reaches the final lli list element and wraps around to the head. The head is a different struct ls1x_dma_desc. It goes down to the offset the node member would be at in ls1x_dma_lli inside of ls1x_dma_desc.

This leaves the lli pointer somewhere inside of ls1x_dma_desc at the end of the loop. The subsequent phys debug value read therefore would print garbage as a valid phys address.

The primary bug occurs in the residue calculation list_for_each_entry_from macro. The lli pointer still points to somewhere inside of ls1x_dma_desc, when the list_for_each_entry_from performs the same wrong offset calculation on ls1x_dma_desc to determine the stop condition of the loop, it lands at the same offset that lli pointer is on, and the loop immediately stops.

This produces a 0 residue value, while it should be the entire length of the lli list chain. The driver advertises DMA_RESIDUE_GRANULARITY_SEGMENT, so a 0 residue would mean the transfer has completed.

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

Note the check still reports this line after the fix; the remaining use is safe (the cursor is only dereferenced on the match path, or reset to the first entry otherwise).

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.

---
 drivers/dma/loongson/loongson1-apb-dma.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/dma/loongson/loongson1-apb-dma.c +2 -1
index 89786cbd20ab..8bb6e6e5719d 100644
@@ -439,6 +439,7 @@ static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan,
                        struct ls1x_dma_desc *desc = to_ls1x_dma_desc(vd);
                        struct ls1x_dma_lli *lli;
                        dma_addr_t next_phys;
+                       bool found = false;
 
                        /* get the current lli */
                        if (ls1x_dma_query(chan, &chan->curr_lli->phys))
@@ -447,11 +448,17 @@ 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)
+                               if (lli->hw[LS1X_DMADESC_NEXT] == next_phys) {

drivers/dma/loongson/loongson1-apb-dma.c static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan, · line 458

Quoted 10 lines
drivers/dma/loongson/loongson1-apb-dma.c +6 -3
+                                       found = true;
                                        break;
-
-                       dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
-                               &lli->phys);
+                               }
+
+                       if (!found)
+                               lli = list_first_entry(&desc->lli_list,
+                                                      struct ls1x_dma_lli, node);

However, I don't think falling back to list_first_entry() when no match is found is appropriate. Since next_phys should always match one of the LLIs in the descriptor chain, failing to find a match should be treated as an unexpected condition rather than silently falling back to the first LLI.
I would suggest something like:

       scoped_guard(spinlock_irqsave, &chan->vc.lock) {
               vd = vchan_find_desc(&chan->vc, cookie);
               if (vd) {
                       struct ls1x_dma_desc *desc = to_ls1x_dma_desc(vd);
                       struct ls1x_dma_lli *lli;
                       dma_addr_t next_phys;

                       /* get the current lli */
                       if (ls1x_dma_query(chan, &chan->curr_lli->phys))
                               return status;

                       /* 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)
                                       continue;

                               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;

                               dma_set_residue(state, bytes);
                               return status;
                       }

                       dev_warn(chan2dev(dchan), "unable to locate
current LLI\n");
               }
       }

       return status;
Quoted 9 lines
drivers/dma/loongson/loongson1-apb-dma.c +3 -1
+                       else
+                               dev_dbg(chan2dev(dchan), "current lli_phys=%pad",
+                                       &lli->phys);
 
                        /* count the residues */
                        list_for_each_entry_from(lli, &desc->lli_list, node)
--

2.54.0

--
Best regards,

Keguang Zhang

#4 lore raw

PATCH v2 1/1 dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

In reply to Mahad Ibrahim

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.

---

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(-)
drivers/dma/loongson/loongson1-apb-dma.c +16 -11
index 89786cbd20ab..8658d5377795 100644
@@ -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;
 }
 
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] The patch introduces an unconditional dev_warn() when a matching LLI is not found, leading to severe kernel log spam when querying queued or recently completed DMA descriptors.

Pre-existing issues:
- [High] The driver fails to start the next queued descriptor in the interrupt handler, breaking queued non-cyclic DMA transfers.
- [High] Missing validation of src_addr_width and dst_addr_width allows a divide-by-zero kernel panic. --

--- Patch [1]: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry() --- commit 4909625fdc8e10245c908a5b3d250dd01aa7c238
Author: Mahad Ibrahim <[email protected]>

dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

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.

Quoted 25 lines
drivers/dma/loongson/loongson1-apb-dma.c +6 -9
index 89786cbd20ab5..8658d53777957 100644
@@ -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 */

drivers/dma/loongson/loongson1-apb-dma.c static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan, · line 466

Quoted 10 lines
drivers/dma/loongson/loongson1-apb-dma.c +10 -0
+				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");

[Severity: High]
Does this new unconditional warning cause severe kernel log spam during normal operation?

If a client submits a descriptor, calls dma_async_issue_pending(), and then polls its status via dmaengine_tx_status(), the hardware might be executing a different descriptor (or sitting idle).

In this case, ls1x_dma_query() fetches a physical address that does not match any LLI in the queried descriptor's list, leading to this warning triggering repeatedly for queued or recently completed descriptors.

[Severity: High]
This is a pre-existing issue, but does the driver fail to start the next queued descriptor in the interrupt handler, breaking queued non-cyclic DMA transfers?

Looking at the interrupt handler ls1x_dma_irq_handler():

	if (chan->is_cyclic) {
		vchan_cyclic_callback(vd);
	} else {
		list_del(&vd->node);
		vchan_cookie_complete(vd);
	}

It removes the completed descriptor and marks it as complete, but appears to lack the logic to initiate the next pending transfer in the virt-dma queue (e.g., using vchan_next_desc() and ls1x_dma_start()). Can this cause subsequent queued descriptors to wait indefinitely?

[Severity: High]
This is a pre-existing issue, but does the missing validation of user-supplied bus widths allow a divide-by-zero kernel panic?

In ls1x_dma_slave_config(), the user-supplied bus widths are saved without validation:

	chan->src_addr = config->src_addr;
	chan->src_addr_width = config->src_addr_width;
	chan->dst_addr = config->dst_addr;
	chan->dst_addr_width = config->dst_addr_width;

Later, in ls1x_dma_prep_lli(), this value is assigned to chan->bus_width and directly used as a divisor:

	lli->hw[LS1X_DMADESC_LENGTH] = buf_len / chan->bus_width;

If a client driver configures the DMA channel but leaves src_addr_width or dst_addr_width undefined (0), could this lead to an immediate system crash?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by Tested-by on v2 1/1
#6 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

In reply to Mahad Ibrahim
Quoted 88 lines · Mahad Ibrahim

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.

---

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(-)
drivers/dma/loongson/loongson1-apb-dma.c +16 -12
index 89786cbd20ab..8658d5377795 100644
@@ -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

Signature · 3 lines
Best regards,

Keguang Zhang
#7 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

In reply to Mahad Ibrahim

Gentle ping.

v2 has been Reviwed-by and Tested-by Keguang Zhang. The patch has no outstanding review comments.

Thank you,
Mahad Ibrahim

#8 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

In reply to Mahad Ibrahim
Quoted 22 lines · Mahad Ibrahim

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

Quoted 6 lines

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.

Avoid post new patch on old email thread.

Just simple said

Compile test only.

Quoted 38 lines

---

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(-)
drivers/dma/loongson/loongson1-apb-dma.c +6 -9
index 89786cbd20ab..8658d5377795 100644
@@ -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 */

drivers/dma/loongson/loongson1-apb-dma.c static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan, · line 466

Quoted 10 lines
drivers/dma/loongson/loongson1-apb-dma.c +10 -0
+				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");

most likely logic is correct, it is not obvious, hope some one who have hardware help test it.

Binbin Zhou just add add this driver recently, he should have hardware to test it.

Frank

Quoted 11 lines
drivers/dma/loongson/loongson1-apb-dma.c +0 -3
 		}
 	}
 
-	dma_set_residue(state, bytes);
-
 	return status;
 }
 
--

2.54.0

#9 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

Hi Frank:

Sorry for the late reply.

Quoted 88 lines · Frank Li
Quoted 22 lines · Mahad Ibrahim

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

Quoted 6 lines

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.

Avoid post new patch on old email thread.

Just simple said

Compile test only.

Quoted 38 lines

---

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(-)
drivers/dma/loongson/loongson1-apb-dma.c +6 -9
index 89786cbd20ab..8658d5377795 100644
@@ -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 */

drivers/dma/loongson/loongson1-apb-dma.c static enum dma_status ls1x_dma_tx_status(struct dma_chan *dchan, · line 466

Quoted 10 lines
drivers/dma/loongson/loongson1-apb-dma.c +10 -0
+				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");

most likely logic is correct, it is not obvious, hope some one who have hardware help test it.

Quoted 2 lines

Binbin Zhou just add add this driver recently, he should have hardware to test it.

Emm, sorry, I don't have any test equipment available right now. As far as I know, Keguang is the maintainer of this driver, and he has the necessary test equipment.

Quoted 13 lines

Frank

Quoted 11 lines
drivers/dma/loongson/loongson1-apb-dma.c +0 -3
  		}
  	}
 
-	dma_set_residue(state, bytes);
-
  	return status;
  }
 
--

2.54.0

Signature · 2 lines
Thanks.
Binbin
#10 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

Quoted 3 lines · Binbin Zhou

Emm, sorry, I don't have any test equipment available right now. As far as I know, Keguang is the maintainer of this driver, and he has the necessary test equipment.

I apologize for the late reply Frank and Binbin.

This patch has already been tested and reviewed by Keguang Zhang who is the maintainer for this driver. v2 was based on his suggestion.

Link:
https://lore.kernel.org/all/CAJhJPsUmpie6NkrzuTK-8C4NP2FFKQMG3nY8626_C9Qjdu4M0g@mail.gmail.com/

Thank you for your time,
Mahad Ibrahim

#11 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

Quoted 7 lines · Mahad Ibrahim

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 3 lines · Binbin Zhou

Emm, sorry, I don't have any test equipment available right now. As far as I know, Keguang is the maintainer of this driver, and he has the necessary test equipment.

Quoted 7 lines

I apologize for the late reply Frank and Binbin.

This patch has already been tested and reviewed by Keguang Zhang who is the maintainer for this driver. v2 was based on his suggestion.

Link:
https://lore.kernel.org/all/CAJhJPsUmpie6NkrzuTK-8C4NP2FFKQMG3nY8626_C9Qjdu4M0g@mail.gmail.com/

You missed their tags in this version. And avoid post new patch at old thread

Frank

Quoted 3 lines

Thank you for your time,
Mahad Ibrahim

#12 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

Quoted 6 lines · Frank Li

You missed their tags in this version. And avoid post new patch at old thread

Frank

I don't quite follow what you mean by the missing tags; Keguang sent his Reviewed-by and Tested-by tags two days after my v2. I think sending v2 on old v1 thread is causing issues about the sequence, sorry about that.

If you wish I can send a v3 with Keguang's tags and a fresh thread.

Thank you for your time,
Mahad Ibrahim

#13 lore raw

Re: [PATCH v2] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()

In reply to Frank Li
Quoted 27 lines · Frank Li
Quoted 26 lines · Mahad Ibrahim

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.

Quoted 8 lines
Quoted 2 lines

As I do not possess the hardware, I cannot test the patch. Compile tested only with mips64-linux-gnu-gcc.

Avoid post new patch on old email thread.

Just simple said

Compile test only.

Please updated commit message at v3

Frank