Re: [PATCH v2 2/2] dmaengine: stm32-dma3: defer clock cleanup to managed teardown

From: Frank Li

Date: Mon Nov 16 2026 - 16:35:46 EST


On Tue, Nov 10, 2026 at 01:05:14AM +0000, Myeonghun Pak wrote:
> Probe enables the controller clock, but remove only disables runtime PM.
> Disabling runtime PM does not invoke runtime suspend, so removal of an
> active controller leaves its clock prepare and enable references held.
> Probe error paths also disable the clock before managed DMA registration
> and IRQ resources have been released.
>
> Register a clock cleanup action before acquiring those managed resources.
> Keep the existing remove-time runtime PM disable barrier, and let devres
> release the remaining clock reference after the DMA and IRQ resources.
> Skip the clock disable if runtime suspend has already released it, then
> mark the device suspended. The PM stubs also cover CONFIG_PM=n.
>
> Set the initial runtime PM state next to clock enable so that probe unwind
> can distinguish that reference from a runtime-suspended device. If setting
> the state fails, release the clock immediately and return the error.
> Use direct error returns once devres owns the remaining cleanup.
>
> Fixes: f561ec8b2b33 ("dmaengine: Add STM32 DMA3 support")
>
> Co-developed-by: Ijae Kim <ae878000@xxxxxxxxx>
> Signed-off-by: Ijae Kim <ae878000@xxxxxxxxx>
> Signed-off-by: Myeonghun Pak <mhun512@xxxxxxxxx>
> ---
> No hardware runtime testing.
>
> diff --git a/drivers/dma/stm32/stm32-dma3.c b/drivers/dma/stm32/stm32-dma3.c
> --- a/drivers/dma/stm32/stm32-dma3.c
> +++ b/drivers/dma/stm32/stm32-dma3.c
> @@ -1721,6 +1721,16 @@
> };
> MODULE_DEVICE_TABLE(of, stm32_dma3_of_match);
>
> +static void stm32_dma3_clk_disable(void *data)
> +{
> + struct device *dev = data;
> + struct stm32_dma3_ddata *ddata = dev_get_drvdata(dev);
> +
> + if (!pm_runtime_status_suspended(dev))
> + clk_disable_unprepare(ddata->clk);
> + pm_runtime_set_suspended(dev);
> +}
> +
> static int stm32_dma3_probe(struct platform_device *pdev)
> {
> struct device_node *np = pdev->dev.of_node;
> @@ -1755,6 +1765,16 @@
> ret = clk_prepare_enable(ddata->clk);
> if (ret)
> return dev_err_probe(&pdev->dev, ret, "Failed to enable clk\n");
> +
> + ret = pm_runtime_set_active(&pdev->dev);
> + if (ret) {
> + clk_disable_unprepare(ddata->clk);
> + return ret;
> + }
> + /* Release the clock after the managed DMA device and channel IRQs. */
> + ret = devm_add_action_or_reset(&pdev->dev, stm32_dma3_clk_disable,
> &pdev->dev);
> + if (ret)
> + return ret;

devm_clk_bulk_get_enable()

if (devm_pm_runtime_set_active_enabled()) {
...
}

At remove function

{
pm_runtime_get_sync(&pdev->dev);

...
pm_runtime_put_noidle(&pdev->dev); //return pm count drop to 0,
devm_clk*() will disable clock for you
}


Frank

>
> reset_control_reset(reset);
>
> @@ -1831,17 +1851,15 @@
>
> ddata->chans = devm_kcalloc(&pdev->dev, ddata->dma_channels,
> sizeof(*ddata->chans),
> GFP_KERNEL);
> - if (!ddata->chans) {
> - ret = -ENOMEM;
> - goto err_clk_disable;
> - }
> + if (!ddata->chans)
> + return -ENOMEM;
>
> chan_reserved = stm32_dma3_check_rif(ddata);
>
> if (chan_reserved == GENMASK(ddata->dma_channels - 1, 0)) {
> ret = -ENODEV;
> dev_err_probe(&pdev->dev, ret, "No channel available, abort registration\n");
> - goto err_clk_disable;
> + return ret;
> }
>
> /* G_FIFO_SIZE x=0..7 in HWCFGR3 and G_FIFO_SIZE x=8..15 in HWCFGR4 */
> @@ -1861,7 +1879,7 @@
>
> ret = dmaenginem_async_device_register(dma_dev);
> if (ret)
> - goto err_clk_disable;
> + return ret;
>
> for (i = 0; i < ddata->dma_channels; i++) {
> char name[12];
> @@ -1878,12 +1896,12 @@
> ret = dma_async_device_channel_register(&ddata->dma_dev,
> &chan->vchan.chan, name);
> if (ret) {
> dev_err_probe(&pdev->dev, ret, "Failed to register channel %s\n", name);
> - goto err_clk_disable;
> + return ret;
> }
>
> ret = platform_get_irq(pdev, i);
> if (ret < 0)
> - goto err_clk_disable;
> + return ret;
> chan->irq = ret;
>
> ret = devm_request_irq(&pdev->dev, chan->irq, stm32_dma3_chan_irq, 0,
> @@ -1891,19 +1909,18 @@
> if (ret) {
> dev_err_probe(&pdev->dev, ret, "Failed to request channel %s IRQ\n",
> vchan_chan_name(&chan->vchan));
> - goto err_clk_disable;
> + return ret;
> }
> }
>
> ret = of_dma_controller_register(np, stm32_dma3_of_xlate, ddata);
> if (ret) {
> dev_err_probe(&pdev->dev, ret, "Failed to register controller\n");
> - goto err_clk_disable;
> + return ret;
> }
>
> verr = readl_relaxed(ddata->base + STM32_DMA3_VERR);
>
> - pm_runtime_set_active(&pdev->dev);
> pm_runtime_enable(&pdev->dev);
> pm_runtime_get_noresume(&pdev->dev);
> pm_runtime_put(&pdev->dev);
> @@ -1912,11 +1929,6 @@
> FIELD_GET(VERR_MAJREV, verr), FIELD_GET(VERR_MINREV, verr));
>
> return 0;
> -
> -err_clk_disable:
> - clk_disable_unprepare(ddata->clk);
> -
> - return ret;
> }
>
> static void stm32_dma3_remove(struct platform_device *pdev)