[PATCH 3/4] dmaengine: sh: rz-dmac: Refactor runtime PM handling

Tommaso Merciai posted 4 patches 5 months, 1 week ago
[PATCH 3/4] dmaengine: sh: rz-dmac: Refactor runtime PM handling
Posted by Tommaso Merciai 5 months, 1 week ago
Refactor runtime PM handling to ensure correct power management and prevent
resource leaks.  Invoke pm_runtime_get_sync() when allocating DMA channel
resources and pm_runtime_put() when freeing them.  Add pm_runtime_put() in
rz_dmac_probe() to balance the usage count during device initialization,
and remove the unnecessary pm_runtime_put() from rz_dmac_remove() to avoid
PM inconsistencies.

Signed-off-by: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>
---
 drivers/dma/sh/rz-dmac.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/dma/sh/rz-dmac.c b/drivers/dma/sh/rz-dmac.c
index 0bc11a6038383..4ab6076f5499e 100644
--- a/drivers/dma/sh/rz-dmac.c
+++ b/drivers/dma/sh/rz-dmac.c
@@ -455,7 +455,7 @@ static int rz_dmac_alloc_chan_resources(struct dma_chan *chan)
 	if (!channel->descs_allocated)
 		return -ENOMEM;
 
-	return channel->descs_allocated;
+	return pm_runtime_get_sync(chan->device->dev);
 }
 
 static void rz_dmac_free_chan_resources(struct dma_chan *chan)
@@ -490,6 +490,8 @@ static void rz_dmac_free_chan_resources(struct dma_chan *chan)
 
 	INIT_LIST_HEAD(&channel->ld_free);
 	vchan_free_chan_resources(&channel->vc);
+
+	pm_runtime_put(chan->device->dev);
 }
 
 static struct dma_async_tx_descriptor *
@@ -1027,6 +1029,7 @@ static int rz_dmac_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev, "unable to register\n");
 		goto dma_register_err;
 	}
+	pm_runtime_put(&pdev->dev);
 	return 0;
 
 dma_register_err:
@@ -1063,7 +1066,6 @@ static void rz_dmac_remove(struct platform_device *pdev)
 				  channel->lmdesc.base,
 				  channel->lmdesc.base_dma);
 	}
-	pm_runtime_put(&pdev->dev);
 
 	platform_device_put(dmac->icu.pdev);
 }
-- 
2.43.0
Re: [PATCH 3/4] dmaengine: sh: rz-dmac: Refactor runtime PM handling
Posted by Claudiu Beznea 4 weeks, 1 day ago
Hi, Tommaso,

On 9/5/25 17:44, Tommaso Merciai wrote:
> Refactor runtime PM handling to ensure correct power management and prevent
> resource leaks.  Invoke pm_runtime_get_sync() when allocating DMA channel
> resources and pm_runtime_put() when freeing them.  Add pm_runtime_put() in
> rz_dmac_probe() to balance the usage count during device initialization,
> and remove the unnecessary pm_runtime_put() from rz_dmac_remove() to avoid
> PM inconsistencies.
> 
> Signed-off-by: Tommaso Merciai <tommaso.merciai.xr@bp.renesas.com>
> ---
>   drivers/dma/sh/rz-dmac.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/dma/sh/rz-dmac.c b/drivers/dma/sh/rz-dmac.c
> index 0bc11a6038383..4ab6076f5499e 100644
> --- a/drivers/dma/sh/rz-dmac.c
> +++ b/drivers/dma/sh/rz-dmac.c
> @@ -455,7 +455,7 @@ static int rz_dmac_alloc_chan_resources(struct dma_chan *chan)
>   	if (!channel->descs_allocated)
>   		return -ENOMEM;
>   
> -	return channel->descs_allocated;
> +	return pm_runtime_get_sync(chan->device->dev);

This looks wrong to me as, rz_dmac_alloc_chan_resources() could be 
called from the following path:

dma_request_chan() ->
   find_candidate() ->
     dma_chan_get() ->
       rz_dmac_alloc_chan_resources()

With this, the runtime PM reference counter is incremented on 
dma_request_chan() calls, when DMA consumers request DMA channels, and 
remains different than zero until the DMA channel is put. But, on 
suspend path, the runtime PM reference counter is not touched even when 
there are DMA channels requested.

Am I missing something?

Thank you,
Claudiu