[PATCH v2 2/2] dmaengine: bcm2835-dma: Convert to managed resources

From: Tomasz Unger

Date: Thu Nov 19 2026 - 15:39:48 EST


bcm2835_dma_probe() registers the OF DMA controller before the DMA
device. A client probing on another CPU can therefore reach
bcm2835_dma_xlate() and dma_get_any_slave_channel() while the device
is not registered yet, and if dma_async_device_register() then fails,
the driver data is freed while that client may still hold a channel.

Convert the driver to managed resources and register the OF controller
last, after the DMA device. As devres releases resources in reverse
order, the OF controller is now also freed first, before the DMA
device is unregistered, both on remove and on the probe error path.
This makes bcm2835_dma_remove() and the error labels unnecessary.

Channel teardown and the zero page unmap become two separate devres
actions. The channels are devm-allocated after the zero page is
mapped, so a single action registered at that point would run after
the channel memory has already been freed and walk freed list entries.
The tasklet teardown is therefore registered after the channels are
initialized.

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

Suggested-by: Frank Li <Frank.Li@xxxxxxxxxx>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Tomasz Unger <tomasz.unger@xxxxxxxx>
---
drivers/dma/bcm2835-dma.c | 56 +++++++++++++++++++++++------------------------
1 file changed, 27 insertions(+), 29 deletions(-)

diff --git a/drivers/dma/bcm2835-dma.c b/drivers/dma/bcm2835-dma.c
index cf29d897450e..b2da706d2e16 100644
--- a/drivers/dma/bcm2835-dma.c
+++ b/drivers/dma/bcm2835-dma.c
@@ -822,8 +822,9 @@ static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id,
return 0;
}

-static void bcm2835_dma_free(struct bcm2835_dmadev *od)
+static void bcm2835_dma_free(void *data)
{
+ struct bcm2835_dmadev *od = data;
struct bcm2835_chan *c, *next;

list_for_each_entry_safe(c, next, &od->ddev.channels,
@@ -831,6 +832,11 @@ static void bcm2835_dma_free(struct bcm2835_dmadev *od)
list_del(&c->vc.chan.device_node);
tasklet_kill(&c->vc.task);
}
+}
+
+static void bcm2835_dma_unmap_zero_page(void *data)
+{
+ struct bcm2835_dmadev *od = data;

dma_unmap_page_attrs(od->ddev.dev, od->zero_page, PAGE_SIZE,
DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
@@ -944,13 +950,17 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
return -ENOMEM;
}

+ rc = devm_add_action_or_reset(&pdev->dev, bcm2835_dma_unmap_zero_page,
+ od);
+ if (rc)
+ return rc;
+
/* Request DMA channel mask from device tree */
if (of_property_read_u32(pdev->dev.of_node,
"brcm,dma-channel-mask",
&chans_available)) {
dev_err(&pdev->dev, "Failed to get channel mask\n");
- rc = -EINVAL;
- goto err_no_dma;
+ return -EINVAL;
}

/* get irqs for each channel that we support */
@@ -994,49 +1004,37 @@ static int bcm2835_dma_probe(struct platform_device *pdev)
/* initialize the channel */
rc = bcm2835_dma_chan_init(od, i, irq[i], irq_flags);
if (rc)
- goto err_no_dma;
+ return rc;
}

dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);

- /* Device-tree DMA controller registration */
- rc = of_dma_controller_register(pdev->dev.of_node,
- bcm2835_dma_xlate, od);
- if (rc) {
- dev_err(&pdev->dev, "Failed to register DMA controller\n");
- goto err_no_dma;
- }
+ rc = devm_add_action_or_reset(&pdev->dev, bcm2835_dma_free, od);
+ if (rc)
+ return rc;

- rc = dma_async_device_register(&od->ddev);
+ rc = dmaenginem_async_device_register(&od->ddev);
if (rc) {
dev_err(&pdev->dev,
"Failed to register slave DMA engine device: %d\n", rc);
- goto err_of_dma;
+ return rc;
+ }
+
+ /* Device-tree DMA controller registration */
+ rc = devm_of_dma_controller_register(&pdev->dev, pdev->dev.of_node,
+ bcm2835_dma_xlate, od);
+ if (rc) {
+ dev_err(&pdev->dev, "Failed to register DMA controller\n");
+ return rc;
}

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;
-}
-
-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);
}

static struct platform_driver bcm2835_dma_driver = {
.probe = bcm2835_dma_probe,
- .remove = bcm2835_dma_remove,
.driver = {
.name = "bcm2835-dma",
.of_match_table = of_match_ptr(bcm2835_dma_of_match),

--
2.56.1