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

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(-)



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

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

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

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.

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 | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/dma/at_xdmac.c b/drivers/dma/at_xdmac.c
index ef0dfe9d37ba..ef4012c22573 100644
--- a/drivers/dma/at_xdmac.c
+++ b/drivers/dma/at_xdmac.c
@@ -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);

-- 
2.34.1



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

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

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 1/2] dmaengine: at_xdmac: Kill the channel tasklets in remove again
  2026-11-23  0:53 ` [PATCH 1/2] dmaengine: at_xdmac: Kill the " Suyuan Wang via B4 Relay
@ 2026-11-23  1:09   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-23  1:09 UTC (permalink / raw)
  To: Suyuan Wang; +Cc: dmaengine, vkoul, Frank.Li

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 <stevewong@hust.edu.cn>

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.

> diff --git a/drivers/dma/at_xdmac.c b/drivers/dma/at_xdmac.c
> index ef0dfe9d37ba3..ef4012c225730 100644
> --- a/drivers/dma/at_xdmac.c
> +++ b/drivers/dma/at_xdmac.c
> @@ -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);

[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()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-dma-chancnt-v1-0-f0eea0a4bb63@gmail.com?part=1

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  0:53 [PATCH 0/2] dmaengine: Kill channel tasklets in remove again Suyuan Wang via B4 Relay
2026-11-23  0:53 ` [PATCH 1/2] dmaengine: at_xdmac: Kill the " Suyuan Wang via B4 Relay
2026-11-23  1:09   ` sashiko-bot
2026-11-23  0:53 ` [PATCH 2/2] dmaengine: idma64: " Suyuan Wang via B4 Relay

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