Re: [PATCH v2 2/2] drm/panthor: Fix OPP refcnt leaks in devfreq initialisation

From: Boris Brezillon
Date: Thu Oct 03 2024 - 10:58:49 EST


On Thu, 3 Oct 2024 14:30:29 +0100
Adrián Larumbe <adrian.larumbe@xxxxxxxxxxxxx> wrote:

> Make sure in case of errors between the first fetch of an OPP in
> panthor_devfreq_init and its successive put, the error path decrements its
> reference count to avoid OPP object leaks when removing the device.
>
> Signed-off-by: Adrián Larumbe <adrian.larumbe@xxxxxxxxxxxxx>
> Fixes: fac9b22df4b1 ("drm/panthor: Add the devfreq logical block")
> ---
> drivers/gpu/drm/panthor/panthor_devfreq.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/panthor/panthor_devfreq.c b/drivers/gpu/drm/panthor/panthor_devfreq.c
> index 9d0f891b9b53..ce0ac4563f65 100644
> --- a/drivers/gpu/drm/panthor/panthor_devfreq.c
> +++ b/drivers/gpu/drm/panthor/panthor_devfreq.c
> @@ -197,7 +197,7 @@ int panthor_devfreq_init(struct panthor_device *ptdev)
> if (ret && ret != -ENODEV) {
> if (ret != -EPROBE_DEFER)
> DRM_DEV_ERROR(dev, "Couldn't retrieve/enable sram supply\n");
> - return ret;
> + goto opp_err;
> }
>
> /*
> @@ -207,7 +207,7 @@ int panthor_devfreq_init(struct panthor_device *ptdev)
> ret = dev_pm_opp_set_opp(dev, opp);
> if (ret) {
> DRM_DEV_ERROR(dev, "Couldn't set recommended OPP\n");
> - return ret;
> + goto opp_err;
> }
>
> dev_pm_opp_put(opp);
> @@ -242,6 +242,10 @@ int panthor_devfreq_init(struct panthor_device *ptdev)
> DRM_DEV_INFO(dev, "Failed to register cooling device\n");
>
> return 0;
> +
> +opp_err:
> + dev_pm_opp_put(opp);
> + return ret;

If you re-order things (see the following diff), you shouldn't need
this error path.

--->8---

diff --git a/drivers/gpu/drm/panthor/panthor_devfreq.c b/drivers/gpu/drm/panthor/panthor_devfreq.c
index 9d0f891b9b53..4f1a30f29c06 100644
--- a/drivers/gpu/drm/panthor/panthor_devfreq.c
+++ b/drivers/gpu/drm/panthor/panthor_devfreq.c
@@ -163,13 +163,6 @@ int panthor_devfreq_init(struct panthor_device *ptdev)

cur_freq = clk_get_rate(ptdev->clks.core);

- opp = devfreq_recommended_opp(dev, &cur_freq, 0);
- if (IS_ERR(opp))
- return PTR_ERR(opp);
-
- panthor_devfreq_profile.initial_freq = cur_freq;
- ptdev->current_frequency = cur_freq;
-
/* Regulator coupling only takes care of synchronizing/balancing voltage
* updates, but the coupled regulator needs to be enabled manually.
*
@@ -200,17 +193,24 @@ int panthor_devfreq_init(struct panthor_device *ptdev)
return ret;
}

+ opp = devfreq_recommended_opp(dev, &cur_freq, 0);
+ if (IS_ERR(opp))
+ return PTR_ERR(opp);
+
+ panthor_devfreq_profile.initial_freq = cur_freq;
+ ptdev->current_frequency = cur_freq;
+
/*
* Set the recommend OPP this will enable and configure the regulator
* if any and will avoid a switch off by regulator_late_cleanup()
*/
ret = dev_pm_opp_set_opp(dev, opp);
+ dev_pm_opp_put(opp);
if (ret) {
DRM_DEV_ERROR(dev, "Couldn't set recommended OPP\n");
return ret;
}

- dev_pm_opp_put(opp);

/* Find the fastest defined rate */
opp = dev_pm_opp_find_freq_floor(dev, &freq);