Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] dmaengine: bcm2835-dma: Add missing of_dma_controller_free()
       [not found] <20261115-bcm2835-dma-of-dma-free-v1-1-d539234bfec5.ref@yahoo.pl>
@ 2026-11-15 22:15 ` Tomasz Unger
  2026-11-18 19:43   ` Frank Li
  0 siblings, 1 reply; 4+ messages in thread
From: Tomasz Unger @ 2026-11-15 22:15 UTC (permalink / raw)
  To: Vinod Koul, Florian Fainelli
  Cc: Frank Li, Broadcom internal kernel review list, Ray Jui,
	Scott Branden, dmaengine, linux-rpi-kernel, linux-arm-kernel,
	linux-kernel, stable, Tomasz Unger

bcm2835_dma_probe() registers the controller with
of_dma_controller_register(), but of_dma_controller_free() is never
called, neither when dma_async_device_register() fails nor in
bcm2835_dma_remove().

The entry stays on the global of_dma_list and keeps a pointer to the
driver data, which is devm-allocated and freed when the device is
unbound. The next DMA channel request for this node is passed to
bcm2835_dma_xlate() with the freed structure and, depending on what
that memory holds by then, either fails or crashes. As the stale entry
is found first, this still happens after the controller has been bound
again.

On a Raspberry Pi 4 Model B, unbinding vc4 and the DMA controller and
then binding vc4 again results in:

  Unable to handle kernel NULL pointer dereference at virtual address
  0000000000000018
  Call trace:
   find_candidate+0x64/0x210 (P)
   dma_get_any_slave_channel+0x64/0xd0
   bcm2835_dma_xlate+0x24/0x48
   of_dma_request_slave_channel+0x178/0x2a8
   dma_request_chan+0x20c/0x380
   snd_dmaengine_pcm_register+0xa4/0x2a0 [snd_soc_core]
   devm_snd_dmaengine_pcm_register+0x60/0xc0 [snd_soc_core]
   vc4_hdmi_bind+0x4d4/0x8e8 [vc4]

Free the controller on the probe error path and in remove. In remove,
do it before unregistering the DMA device, so that no channel can be
requested through the node while the device is going away.

The bug was found and the fix drafted with the help of an AI coding
assistant (Claude).

Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Tomasz Unger <tomasz.unger@yahoo.pl>
---
Verified on a Raspberry Pi 4 Model B: without the patch the sequence
described in the commit message oopses; with it, binding vc4 while the
controller is unbound is deferred and HDMI audio gets its DMA channels
back once the controller is bound again. Both paths (remove and a forced
dma_async_device_register() failure) were also run under KASAN in QEMU
raspi4b: slab-use-after-free in find_candidate() without the patch, no
report with it.
---
 drivers/dma/bcm2835-dma.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/dma/bcm2835-dma.c b/drivers/dma/bcm2835-dma.c
index 06d830d36882..cf29d897450e 100644
--- a/drivers/dma/bcm2835-dma.c
+++ b/drivers/dma/bcm2835-dma.c
@@ -1011,13 +1011,15 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
 	if (rc) {
 		dev_err(&pdev->dev,
 			"Failed to register slave DMA engine device: %d\n", rc);
-		goto err_no_dma;
+		goto err_of_dma;
 	}
 
 	dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
 
 	return 0;
 
+err_of_dma:
+	of_dma_controller_free(pdev->dev.of_node);
 err_no_dma:
 	bcm2835_dma_free(od);
 	return rc;
@@ -1027,6 +1029,7 @@ static void bcm2835_dma_remove(struct platform_device *pdev)
 {
 	struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
 
+	of_dma_controller_free(pdev->dev.of_node);
 	dma_async_device_unregister(&od->ddev);
 	bcm2835_dma_free(od);
 }

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261115-bcm2835-dma-of-dma-free-ef85776c0914

Best regards,
--  
Tomasz Unger <tomasz.unger@yahoo.pl>



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

* Re: [PATCH] dmaengine: bcm2835-dma: Add missing of_dma_controller_free()
  2026-11-15 22:15 ` [PATCH] dmaengine: bcm2835-dma: Add missing of_dma_controller_free() Tomasz Unger
@ 2026-11-18 19:43   ` Frank Li
  2026-11-18 22:22     ` Tomasz Unger
  0 siblings, 1 reply; 4+ messages in thread
From: Frank Li @ 2026-11-18 19:43 UTC (permalink / raw)
  To: Tomasz Unger
  Cc: Vinod Koul, Florian Fainelli, Frank Li,
	Broadcom internal kernel review list, Ray Jui, Scott Branden,
	dmaengine, linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	stable

On Sun, Nov 15, 2026 at 11:15:15PM +0100, Tomasz Unger wrote:
> [You don't often get email from tomasz.unger@yahoo.pl. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> bcm2835_dma_probe() registers the controller with
> of_dma_controller_register(), but of_dma_controller_free() is never
> called, neither when dma_async_device_register() fails nor in
> bcm2835_dma_remove().
>
> The entry stays on the global of_dma_list and keeps a pointer to the
> driver data, which is devm-allocated and freed when the device is
> unbound. The next DMA channel request for this node is passed to
> bcm2835_dma_xlate() with the freed structure and, depending on what
> that memory holds by then, either fails or crashes. As the stale entry
> is found first, this still happens after the controller has been bound
> again.
>
> On a Raspberry Pi 4 Model B, unbinding vc4 and the DMA controller and
> then binding vc4 again results in:
>
>   Unable to handle kernel NULL pointer dereference at virtual address
>   0000000000000018
>   Call trace:
>    find_candidate+0x64/0x210 (P)
>    dma_get_any_slave_channel+0x64/0xd0
>    bcm2835_dma_xlate+0x24/0x48
>    of_dma_request_slave_channel+0x178/0x2a8
>    dma_request_chan+0x20c/0x380
>    snd_dmaengine_pcm_register+0xa4/0x2a0 [snd_soc_core]
>    devm_snd_dmaengine_pcm_register+0x60/0xc0 [snd_soc_core]
>    vc4_hdmi_bind+0x4d4/0x8e8 [vc4]
>
> Free the controller on the probe error path and in remove. In remove,
> do it before unregistering the DMA device, so that no channel can be
> requested through the node while the device is going away.
>
> The bug was found and the fix drafted with the help of an AI coding
> assistant (Claude).
>
> Fixes: 96286b576690 ("dmaengine: Add support for BCM2835")
> Cc: stable@vger.kernel.org
> Assisted-by: Claude:claude-fable-5-2
> Signed-off-by: Tomasz Unger <tomasz.unger@yahoo.pl>
> ---
> Verified on a Raspberry Pi 4 Model B: without the patch the sequence
> described in the commit message oopses; with it, binding vc4 while the
> controller is unbound is deferred and HDMI audio gets its DMA channels
> back once the controller is bound again. Both paths (remove and a forced
> dma_async_device_register() failure) were also run under KASAN in QEMU
> raspi4b: slab-use-after-free in find_candidate() without the patch, no
> report with it.
> ---
>  drivers/dma/bcm2835-dma.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/dma/bcm2835-dma.c b/drivers/dma/bcm2835-dma.c
> index 06d830d36882..cf29d897450e 100644
> --- a/drivers/dma/bcm2835-dma.c
> +++ b/drivers/dma/bcm2835-dma.c
> @@ -1011,13 +1011,15 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
>         if (rc) {
>                 dev_err(&pdev->dev,
>                         "Failed to register slave DMA engine device: %d\n", rc);
> -               goto err_no_dma;
> +               goto err_of_dma;
>         }
>
>         dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
>
>         return 0;
>
> +err_of_dma:
> +       of_dma_controller_free(pdev->dev.of_node);
>  err_no_dma:
>         bcm2835_dma_free(od);
>         return rc;
> @@ -1027,6 +1029,7 @@ static void bcm2835_dma_remove(struct platform_device *pdev)
>  {
>         struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
>
> +       of_dma_controller_free(pdev->dev.of_node);

use devm_of_dma_controller_register() to fix this problem

Frank

>         dma_async_device_unregister(&od->ddev);
>         bcm2835_dma_free(od);
>  }
>
> ---
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> change-id: 20261115-bcm2835-dma-of-dma-free-ef85776c0914
>
> Best regards,
> --
> Tomasz Unger <tomasz.unger@yahoo.pl>
>


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

* Re: [PATCH] dmaengine: bcm2835-dma: Add missing of_dma_controller_free()
  2026-11-18 19:43   ` Frank Li
@ 2026-11-18 22:22     ` Tomasz Unger
  2026-11-19  2:48       ` Frank Li
  0 siblings, 1 reply; 4+ messages in thread
From: Tomasz Unger @ 2026-11-18 22:22 UTC (permalink / raw)
  To: Frank Li
  Cc: Vinod Koul, Florian Fainelli, Frank Li,
	Broadcom internal kernel review list, Ray Jui, Scott Branden,
	dmaengine, linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	stable

On Wed, Nov 18, 2026 at 01:43:05PM -0600, Frank Li wrote:
> On Sun, Nov 15, 2026 at 11:15:15PM +0100, Tomasz Unger wrote:
>> @@ -1027,6 +1029,7 @@ static void bcm2835_dma_remove(struct platform_device *pdev)
>>  {
>>        struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
>>
>> +      of_dma_controller_free(pdev->dev.of_node);
>
> use devm_of_dma_controller_register() to fix this problem

Hi Frank,

Thanks for the review. I tried that, but with only the OF registration
moved to devres, the controller entry is removed after
bcm2835_dma_remove() has returned, i.e. after
dma_async_device_unregister() and bcm2835_dma_free().

In that window bcm2835_dma_xlate() can still be reached through the
node. dma_async_device_unregister() has already freed chan->local and
dropped the last device reference (dma_device_release() does
list_del_rcu(&device->global_node)), while the channels stay on
od->ddev.channels until bcm2835_dma_free(). A client requesting a
channel at that point goes through find_candidate(), dma_chan_get()
fails with -ENODEV and global_node is deleted a second time. The same
applies to the dma_async_device_register() error path in probe.

Doing it right with devres would mean converting the whole teardown:
a devm action for bcm2835_dma_free(), dmaenginem_async_device_register(),
and devm_of_dma_controller_register() last, so that the OF entry is
removed first. That is a larger change, and
devm_of_dma_controller_register() only exists since v7.1, while this
fixes a bug from the original driver and is tagged for stable.

So I would prefer to keep this patch as a minimal fix that backports
cleanly, and send the devres conversion as a separate follow-up.
Would that work for you?

Thanks,
Tomasz


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

* Re: [PATCH] dmaengine: bcm2835-dma: Add missing of_dma_controller_free()
  2026-11-18 22:22     ` Tomasz Unger
@ 2026-11-19  2:48       ` Frank Li
  0 siblings, 0 replies; 4+ messages in thread
From: Frank Li @ 2026-11-19  2:48 UTC (permalink / raw)
  To: Tomasz Unger
  Cc: Vinod Koul, Florian Fainelli, Frank Li,
	Broadcom internal kernel review list, Ray Jui, Scott Branden,
	dmaengine, linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	stable

On Wed, Nov 18, 2026 at 11:22:30PM +0100, Tomasz Unger wrote:
> [You don't often get email from tomasz.unger@yahoo.pl. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> On Wed, Nov 18, 2026 at 01:43:05PM -0600, Frank Li wrote:
> > On Sun, Nov 15, 2026 at 11:15:15PM +0100, Tomasz Unger wrote:
> >> @@ -1027,6 +1029,7 @@ static void bcm2835_dma_remove(struct platform_device *pdev)
> >>  {
> >>        struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
> >>
> >> +      of_dma_controller_free(pdev->dev.of_node);
> >
> > use devm_of_dma_controller_register() to fix this problem
>
> Hi Frank,
>
> Thanks for the review. I tried that, but with only the OF registration
> moved to devres, the controller entry is removed after
> bcm2835_dma_remove() has returned, i.e. after
> dma_async_device_unregister() and bcm2835_dma_free().
>
> In that window bcm2835_dma_xlate() can still be reached through the
> node. dma_async_device_unregister() has already freed chan->local and
> dropped the last device reference (dma_device_release() does
> list_del_rcu(&device->global_node)), while the channels stay on
> od->ddev.channels until bcm2835_dma_free(). A client requesting a
> channel at that point goes through find_candidate(), dma_chan_get()
> fails with -ENODEV and global_node is deleted a second time. The same
> applies to the dma_async_device_register() error path in probe.
>
> Doing it right with devres would mean converting the whole teardown:
> a devm action for bcm2835_dma_free(), dmaenginem_async_device_register(),
> and devm_of_dma_controller_register() last, so that the OF entry is
> removed first. That is a larger change, and
> devm_of_dma_controller_register() only exists since v7.1, while this
> fixes a bug from the original driver and is tagged for stable.
>
> So I would prefer to keep this patch as a minimal fix that backports
> cleanly, and send the devres conversion as a separate follow-up.
> Would that work for you?

Please send out follow up patch also because this is not urgent. DMA engine
driver was seldom removed.

Frank

>
> Thanks,
> Tomasz


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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261115-bcm2835-dma-of-dma-free-v1-1-d539234bfec5.ref@yahoo.pl>
2026-11-15 22:15 ` [PATCH] dmaengine: bcm2835-dma: Add missing of_dma_controller_free() Tomasz Unger
2026-11-18 19:43   ` Frank Li
2026-11-18 22:22     ` Tomasz Unger
2026-11-19  2:48       ` Frank Li

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