dmaengine: Kill channel tasklets in remove again
v2 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.
For at_xdmac, just making the loop run again is not enough, as the Sashiko review of v1 pointed out: the tasklets and at_xdmac_free_chan_resources() would then run after the channel devices are freed and the clock is disabled. v2 moves the IRQ, tasklet and channel cleanup in front of dma_async_device_unregister() and clk_disable_unprepare() instead. idma64 has no clock to disable and its virt-dma tasklet uses neither the registers nor the channel device, so patch 2 is unchanged.
These loops are also touched by Allen's "dmaengine: migrate channel tasklets to WQ_BH" series (v4, 01/33 and 04/33). The two fixes don't depend on that series. 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.
Changes in v2:
- 1/2: move free_irq(), the tasklet kills and the channel cleanup before dma_async_device_unregister() and clk_disable_unprepare(), instead of saving chancnt; add the Fixes tag for the original driver (Sashiko).
- 2/2: unchanged.
v1: https://lore.kernel.org/all/[email protected]/
---
Suyuan Wang (2):
dmaengine: at_xdmac: Kill the channel tasklets before unregistering
dmaengine: idma64: Kill the channel tasklets in remove again
drivers/dma/at_xdmac.c | 19 ++++++++++++------- drivers/dma/idma64.c | 4 +++- 2 files changed, 15 insertions(+), 8 deletions(-)
PATCH v2 1/2 dmaengine: at_xdmac: Kill the channel tasklets before unregistering
From: Suyuan Wang <[email protected]>
at_xdmac_remove() unregisters the DMA device and disables the clock, and only then frees the IRQ and kills the channel tasklets in a loop over dma.chancnt:
dma_async_device_unregister(&atxdmac->dma);
...
clk_disable_unprepare(atxdmac->clk);
free_irq(atxdmac->irq, atxdmac);
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 loop does nothing and a tasklet scheduled by an interrupt can run after the devm-allocated atxdmac has been freed. Even when the loop ran, tasklet_kill() let a pending tasklet run after the channel devices were freed and the clock was disabled, and the tasklet reads the channel registers and logs through the channel device.
Free the IRQ, kill the tasklets and free the channel resources right after at_xdmac_off(), before the channels are unregistered and the clock is disabled.
Found with a static analysis tool and checked by hand.
--- drivers/dma/at_xdmac.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/dma/at_xdmac.c +12 -7
@@ -2439,21 +2439,26 @@ static void at_xdmac_remove(struct platform_device *pdev)int i;at_xdmac_off (atxdmac, true); -of_dma_controller_free (pdev->dev.of_node); -dma_async_device_unregister (&atxdmac->dma); -pm_runtime_disable (atxdmac->dev); -pm_runtime_set_suspended (&pdev->dev); -pm_runtime_dont_use_autosuspend (&pdev->dev); -clk_disable_unprepare (atxdmac->clk); -free_irq (atxdmac->irq, atxdmac); +/* +* Do this before dma_async_device_unregister(): it frees the channel +* devices and decrements dma.chancnt, and the tasklets also need the +* clock. +*/ for (i =0 ; i < atxdmac->dma.chancnt; i++) {struct at_xdmac_chan *atchan = &atxdmac->chan[i];tasklet_kill (&atchan->tasklet);at_xdmac_free_chan_resources (&atchan->chan); } + +of_dma_controller_free (pdev->dev.of_node); +dma_async_device_unregister (&atxdmac->dma); +pm_runtime_disable (atxdmac->dev); +pm_runtime_set_suspended (&pdev->dev); +pm_runtime_dont_use_autosuspend (&pdev->dev); +clk_disable_unprepare (atxdmac->clk); }static const struct dev_pm_ops __maybe_unused atmel_xdmac_dev_pm_ops = {
Signature · 1 line
2.34.1
PATCH v2 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
Andy Shevchenko
reviewerRe: [PATCH v2 2/2] dmaengine: idma64: Kill the channel tasklets in remove again
Quoted 17 lines · On Mon, Nov 23, 2026 at 01:17:38AM +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.
You have to wait a sensible time before resubmission. Look at the v1 and act accordingly.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Wong Steve
Acked-by Andy ShevchenkoRe: [PATCH v2 2/2] dmaengine: idma64: Kill the channel tasklets in remove again
Quoted 2 lines
You have to wait a sensible time before resubmission. Look at the v1 and act accordingly.
Sorry, I sent v2 too quickly. Sashiko found a real problem in the at_xdmac patch (1/2), and I resent the series without waiting for other replies, so your Acked-by on v1 is missing.
This idma64 patch (2/2) is the same in v1 and v2. Vinod, could you take Andy's tag from v1 when applying:
About marking chancnt __private: that makes sense to me, but it is a change to the dmaengine core, so I'd keep it out of this fix.
I'll wait for more review before sending anything else.
Thanks,
Suyuan
Quoted 31 lines · Andy Shevchenko <[email protected]> 于2026年11月23日周一 17:49写道:
Quoted 17 lines · On Mon, Nov 23, 2026 at 01:17:38AM +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.
You have to wait a sensible time before resubmission. Look at the v1 and act accordingly.
--
With Best Regards,
Andy Shevchenko