Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] dmaengine: Kill channel tasklets in remove again
@ 2026-11-23  1:17 Suyuan Wang via B4 Relay
  2026-11-23  1:17 ` [PATCH v2 1/2] dmaengine: at_xdmac: Kill the channel tasklets before unregistering Suyuan Wang via B4 Relay
  2026-11-23  1:17 ` [PATCH v2 2/2] dmaengine: idma64: Kill the channel tasklets in remove again Suyuan Wang via B4 Relay
  0 siblings, 2 replies; 4+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-23  1:17 UTC (permalink / raw)
  To: ludovic.desroches, vkoul
  Cc: Frank.Li, dave.jiang, linux-kernel, andriy.shevchenko, dmaengine,
	allen.lkml, linux-arm-kernel

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/20261123-dma-chancnt-v1-0-f0eea0a4bb63@gmail.com/
---
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(-)




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] dmaengine: at_xdmac: Kill the channel tasklets before unregistering
  2026-11-23  1:17 [PATCH v2 0/2] dmaengine: Kill channel tasklets in remove again Suyuan Wang via B4 Relay
@ 2026-11-23  1:17 ` Suyuan Wang via B4 Relay
  2026-11-23  1:17 ` [PATCH v2 2/2] dmaengine: idma64: Kill the channel tasklets in remove again Suyuan Wang via B4 Relay
  1 sibling, 0 replies; 4+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-23  1:17 UTC (permalink / raw)
  To: ludovic.desroches, vkoul
  Cc: Frank.Li, dave.jiang, linux-kernel, andriy.shevchenko, dmaengine,
	allen.lkml, linux-arm-kernel

From: Suyuan Wang <stevewong@hust.edu.cn>

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.

Fixes: e1f7c9eee707 ("dmaengine: at_xdmac: creation of the atmel eXtended DMA Controller driver")
Fixes: e81274cd6b52 ("dmaengine: add support to dynamic register/unregister of channels")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
 drivers/dma/at_xdmac.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/drivers/dma/at_xdmac.c b/drivers/dma/at_xdmac.c
index ef0dfe9d37ba..d5d60f3d3543 100644
--- a/drivers/dma/at_xdmac.c
+++ b/drivers/dma/at_xdmac.c
@@ -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 = {

-- 
2.34.1




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] dmaengine: idma64: Kill the channel tasklets in remove again
  2026-11-23  1:17 [PATCH v2 0/2] dmaengine: Kill channel tasklets in remove again Suyuan Wang via B4 Relay
  2026-11-23  1:17 ` [PATCH v2 1/2] dmaengine: at_xdmac: Kill the channel tasklets before unregistering Suyuan Wang via B4 Relay
@ 2026-11-23  1:17 ` Suyuan Wang via B4 Relay
  2026-11-23  9:48   ` Andy Shevchenko
  1 sibling, 1 reply; 4+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-23  1:17 UTC (permalink / raw)
  To: ludovic.desroches, vkoul
  Cc: Frank.Li, dave.jiang, linux-kernel, andriy.shevchenko, dmaengine,
	allen.lkml, linux-arm-kernel

From: Suyuan Wang <stevewong@hust.edu.cn>

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.

Fixes: e81274cd6b52 ("dmaengine: add support to dynamic register/unregister of channels")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
 drivers/dma/idma64.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/dma/idma64.c b/drivers/dma/idma64.c
index e3db213e564b..5d0df1defb14 100644
--- a/drivers/dma/idma64.c
+++ b/drivers/dma/idma64.c
@@ -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);

-- 
2.34.1




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] dmaengine: idma64: Kill the channel tasklets in remove again
  2026-11-23  1:17 ` [PATCH v2 2/2] dmaengine: idma64: Kill the channel tasklets in remove again Suyuan Wang via B4 Relay
@ 2026-11-23  9:48   ` Andy Shevchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Andy Shevchenko @ 2026-11-23  9:48 UTC (permalink / raw)
  To: shyckless
  Cc: ludovic.desroches, vkoul, Frank.Li, dave.jiang, linux-kernel,
	dmaengine, allen.lkml, linux-arm-kernel

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.
> 
> 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




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-23  9:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  1:17 [PATCH v2 0/2] dmaengine: Kill channel tasklets in remove again Suyuan Wang via B4 Relay
2026-11-23  1:17 ` [PATCH v2 1/2] dmaengine: at_xdmac: Kill the channel tasklets before unregistering Suyuan Wang via B4 Relay
2026-11-23  1:17 ` [PATCH v2 2/2] dmaengine: idma64: Kill the channel tasklets in remove again Suyuan Wang via B4 Relay
2026-11-23  9:48   ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox