dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()
Subsystems (3)
PATCH v3 1/1 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.
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
Compile test only; Hardware testing by Keguang Zhang.
---
v2:
- encapsulate residue calculation and dev_dbg inside the list_for_each_entry() macro. Treat non-matching LLI as an unexpected case.
- link: https://lore.kernel.org/all/[email protected]/
v3:
- Collect tags from Keguang Zhang.
- Repost patch in a new thread.
- Change chan2dev to the newer dmaengine_chan_dev.
drivers/dma/loongson/loongson1-apb-dma.c | 27 ++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-)
drivers/dma/loongson/loongson1-apb-dma.c +16 -11
@@ -441,22 +441,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 (dmaengine_chan_dev (dchan),"current lli_phys=%pad" , - &lli->phys); +dev_dbg (dmaengine_chan_dev (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 (dmaengine_chan_dev (dchan), +"unable to locate current lli.\n" ); } } -dma_set_residue (state, bytes); -return status; }
Signature · 1 line
2.54.0
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_entrymacro 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 als1x_dma_desc, as als1x_dma_lliobject. Additionally it goes forwards and prints garbage via thedev_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.coccidrivers/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
Compile test only; Hardware testing by Keguang Zhang.
Signed-off-by Mahad Ibrahim <[email protected]>Reviewed-by Keguang Zhang <[email protected]>---
Quoted 63 lines
v2:
- encapsulate residue calculation anddev_dbginside thelist_for_each_entry()macro. Treat non-matching LLI as an unexpected case.
- link: https://lore.kernel.org/all/[email protected]/v3:
- Collect tags from Keguang Zhang.
- Repost patch in a new thread.
- Change chan2dev to the newerdmaengine_chan_dev.drivers/dma/loongson/loongson1-apb-dma.c | 27 ++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-)drivers/dma/loongson/loongson1-apb-dma.c +16
-12@@ -441,22 +441,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 (dmaengine_chan_dev (dchan),"current lli_phys=%pad" , - &lli->phys); +dev_dbg (dmaengine_chan_dev (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 (dmaengine_chan_dev (dchan), +"unable to locate current lli.\n" ); } } -dma_set_residue (state, bytes); -return status; } --2.54.0
Quoted 4 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.
Quoted 8 lines
A
list_for_each_entrymacro 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 als1x_dma_desc, as als1x_dma_lliobject. Additionally it goes forwards and prints garbage via thedev_dbg.[...]
Applied, thanks!
[1/1] dmaengine: loongson1-apb-dma: avoid using iterator variable after list_for_each_entry()
commit: 5d4f5cdad22b181559c0bc8088f3ae9572ae4362
Best regards,
Signature · 1 line
~Vinod