dmaengine: Kill channel tasklets in remove again
cover dmaengine: Kill channel tasklets in remove again
at_xdmac and idma64 kill their channel tasklets in remove() with a loop over dma.chancnt after dma_async_device_unregister(). Since commit e81274cd6b52, dma_async_device_unregister() unregisters each channel and decrements dma.chancnt for it, so chancnt is 0 when the loops run, they do nothing, and a tasklet scheduled by an interrupt can run after the devm-allocated driver data is gone. Both patches read the count before unregistering.
These loops are also touched by Allen's "dmaengine: migrate channel tasklets to WQ_BH" series (v4, 01/33 and 04/33), which keeps the chancnt bound. The two fixes don't depend on that series; they make the current tasklet cleanup work again. Whichever goes in second needs a trivial rebase.
Found with a static analysis tool and checked by hand. Compile tested on x86_64 with COMPILE_TEST (W=1); I don't have the hardware.
---
Suyuan Wang (2):
dmaengine: at_xdmac: Kill the channel tasklets in remove again
dmaengine: idma64: Kill the channel tasklets in remove again
drivers/dma/at_xdmac.c | 4 +++- drivers/dma/idma64.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)
PATCH 1/2 dmaengine: at_xdmac: Kill the channel tasklets in remove again
From: Suyuan Wang <[email protected]>
at_xdmac_remove() kills the channel tasklets and frees the channel resources in a loop after free_irq():
for (i = 0; i < atxdmac->dma.chancnt; i++) {
...
tasklet_kill(&atchan->tasklet);
at_xdmac_free_chan_resources(&atchan->chan);
}Since commit e81274cd6b52 ("dmaengine: add support to dynamic register/unregister of channels"), dma_async_device_unregister() unregisters each channel and decrements dma.chancnt for it, so the count is 0 by the time the loop runs and the loop does nothing. A tasklet scheduled by an interrupt can then run after atxdmac, which is devm-allocated, has been freed.
Read the channel count before unregistering the device.
Found with a static analysis tool and checked by hand.
--- drivers/dma/at_xdmac.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/dma/at_xdmac.c +3 -1
@@ -2436,6 +2436,8 @@ static int at_xdmac_probe(struct platform_device *pdev)static void at_xdmac_remove (struct platform_device *pdev) {struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata (pdev); +/* dma_async_device_unregister() decrements dma.chancnt to 0 */ +int nr_channels = atxdmac->dma.chancnt;int i;at_xdmac_off (atxdmac, true);
@@ -2448,7 +2450,7 @@ static void at_xdmac_remove(struct platform_device *pdev)free_irq (atxdmac->irq, atxdmac); -for (i =0 ; i < atxdmac->dma.chancnt; i++) { +for (i =0 ; i < nr_channels; i++) {struct at_xdmac_chan *atchan = &atxdmac->chan[i];tasklet_kill (&atchan->tasklet);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 1/2] dmaengine: at_xdmac: Kill the channel tasklets in remove again
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dmaengine: at_xdmac: chan_dev UAF and unclocked MMIO in at_xdmac_remove() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] dmaengine: at_xdmac: chan_dev UAF and unclocked MMIO in at_xdmac_remove()
--
commit d1b41bac9406538e5524be5f60b8b97177c4c118
Author: Suyuan Wang <[email protected]>
dmaengine: at_xdmac: Kill the channel tasklets in remove again
This commit saves atxdmac->dma.chancnt before dma_async_device_unregister() decrements it to zero so the per-channel tasklet cleanup loop in at_xdmac_remove() runs again.
Quoted 14 lines
drivers/dma/at_xdmac.c +2
-0@@ -2436,6 +2436,8 @@ static int at_xdmac_probe(struct platform_device *pdev)static void at_xdmac_remove (struct platform_device *pdev) {struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata (pdev); +/* dma_async_device_unregister() decrements dma.chancnt to 0 */ +int nr_channels = atxdmac->dma.chancnt;int i;at_xdmac_off (atxdmac, true);
drivers/dma/at_xdmac.c static void at_xdmac_remove(struct platform_device *pdev) · line 2456
Quoted 8 lines
drivers/dma/at_xdmac.c +1
-1@@ -2448,7 +2450,7 @@ static void at_xdmac_remove(struct platform_device *pdev)free_irq (atxdmac->irq, atxdmac); -for (i =0 ; i < atxdmac->dma.chancnt; i++) { +for (i =0 ; i < nr_channels; i++) {struct at_xdmac_chan *atchan = &atxdmac->chan[i];tasklet_kill (&atchan->tasklet);
[Severity: Medium]
Does re-enabling this loop after dma_async_device_unregister() cause at_xdmac_free_chan_resources() to dereference a freed chan->chan_dev?
dma_async_device_unregister() unregisters each channel device, triggering chan_dev_release() -> kfree(chan_dev) and leaving chan->chan_dev dangling. When the loop below runs at_xdmac_free_chan_resources(&atchan->chan) with a non-empty free_descs_list, it passes dmaengine_chan_dev(chan) (&chan->chan_dev->device) to dev_dbg(), which is a use-after-free:
drivers/dma/at_xdmac.c:at_xdmac_free_chan_resources() {
...
list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
dev_dbg(dmaengine_chan_dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
list_del(&desc->desc_node);
dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
}
...
}In addition, because free_irq() and tasklet_kill() run after dma_async_device_unregister() and clk_disable_unprepare(), an in-flight interrupt or scheduled tasklet can also dereference the freed chan->chan_dev and access XDMAC MMIO registers after the controller clock has been disabled. Should free_irq(), tasklet_kill(), and at_xdmac_free_chan_resources() run before dma_async_device_unregister() and clk_disable_unprepare()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/2 dmaengine: idma64: Kill the channel tasklets in remove again
In reply to Suyuan WangFrom: Suyuan Wang <[email protected]>
idma64_remove() frees the IRQ and then kills the channel tasklets, as the comment there says, so that no tasklet scheduled by the interrupt runs after removal:
for (i = 0; i < idma64->dma.chancnt; i++) {
...
tasklet_kill(&idma64c->vchan.task);
}Since commit e81274cd6b52 ("dmaengine: add support to dynamic register/unregister of channels"), dma_async_device_unregister() unregisters each channel and decrements dma.chancnt for it, so the count is 0 when this loop runs and no tasklet is killed. A tasklet scheduled by an interrupt can then run after the devm-allocated idma64 and its channels have been freed.
Read the channel count before unregistering the device.
Found with a static analysis tool and checked by hand.
--- drivers/dma/idma64.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/dma/idma64.c +3 -1
@@ -604,6 +604,8 @@ static int idma64_probe(struct idma64_chip *chip)static void idma64_remove (struct idma64_chip *chip) {struct idma64 *idma64 = chip->idma64; +/* dma_async_device_unregister() decrements dma.chancnt to 0 */ +unsigned short nr_chan = idma64->dma.chancnt;unsigned short i;dma_async_device_unregister (&idma64->dma);
@@ -614,7 +616,7 @@ static void idma64_remove(struct idma64_chip *chip)*/ devm_free_irq (chip->dev, chip->irq, idma64); -for (i =0 ; i < idma64->dma.chancnt; i++) { +for (i =0 ; i < nr_chan; i++) {struct idma64_chan *idma64c = &idma64->chan[i];tasklet_kill (&idma64c->vchan.task);
Signature · 1 line
2.34.1
Re: [PATCH 2/2] dmaengine: idma64: Kill the channel tasklets in remove again
Quoted 17 lines · On Mon, Nov 23, 2026 at 12:53:07AM +0000, Suyuan Wang via B4 Relay wrote:
idma64_remove()frees the IRQ and then kills the channel tasklets, as the comment there says, so that no tasklet scheduled by the interrupt runs after removal:for (i = 0; i < idma64->dma.chancnt; i++) { ... tasklet_kill(&idma64c->vchan.task); }Since commit
e81274cd6b52("dmaengine: add support to dynamic register/unregister of channels"),dma_async_device_unregister()unregisters each channel and decrements dma.chancnt for it, so the count is 0 when this loop runs and no tasklet is killed. A tasklet scheduled by an interrupt can then run after the devm-allocated idma64 and its channels have been freed.
Quoted 3 lines
Read the channel count before unregistering the device.
Found with a static analysis tool and checked by hand.
...
drivers/dma/idma64.c static int idma64_probe(struct idma64_chip *chip) · line 608
Quoted 2 lines
drivers/dma/idma64.c +2
-0+/* dma_async_device_unregister() decrements dma.chancnt to 0 */ +unsigned short nr_chan = idma64->dma.chancnt;
Which makes me think that we need to draw a line between private and non-private members in the respective DMA data types. With this field it's probably better to mark it with __private, or add another one with that annotation and use it internally in DMAengine core.
Signature · 2 lines
With Best Regards, Andy Shevchenko