Re: [PATCH] iio: proximity: srf04: fix runtime PM autosuspend cleanup
From: Jonathan Cameron
Date: Fri Aug 07 2026 - 19:43:12 EST
On Fri, 7 Aug 2026 22:19:37 +0800
Guangshuo Li <lgs201920130244@xxxxxxxxx> wrote:
> srf04_probe() calls pm_runtime_use_autosuspend() when the optional
> power GPIO is present, but srf04_remove() does not call the matching
> pm_runtime_dont_use_autosuspend() before disabling runtime PM.
>
> The runtime PM documentation requires pm_runtime_use_autosuspend() to
> be balanced with pm_runtime_dont_use_autosuspend() when the driver is
> removed. Failing to do so can leave the autosuspend usage state
> unbalanced and may result in a usage_count leak when the autosuspend
> delay is negative.
>
> Add the missing pm_runtime_dont_use_autosuspend() call in the remove
> path before disabling runtime PM.
>
> This issue was found by manual code inspection.
A cleaner solution may be to move to devm_pm_runtime_set_active_enabled()
though I am slightly concerned by the ordering here where in probe
we do iio_device_register() then runtime pm setup.
Remove would generally be the reverse order but instead it
does iio_device_unregister() the the runtime pm teardown.
That complicates matters and I can't see why we need to do that.
Do take a close look to see if you can see any reason this might be done.
I think easiest is move runtime pm registration before iio_device_register(),
using devm_pm_runtime_set_active_enabled() and ripping out all the calls
that effectively replaces.
Then a follow up patch to move to devm_iio_device_register() and drop
remove() entirely.
Looks like I missed this odd ordering when reviewing back in 2020.
oops.
Thanks,
Jonathan
>
> Fixes: 2251157b335b4 ("iio: srf04: add power management feature")
> Signed-off-by: Guangshuo Li <lgs201920130244@xxxxxxxxx>
> ---
> drivers/iio/proximity/srf04.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/iio/proximity/srf04.c b/drivers/iio/proximity/srf04.c
> index 7be50bdebfcb..5f270f506803 100644
> --- a/drivers/iio/proximity/srf04.c
> +++ b/drivers/iio/proximity/srf04.c
> @@ -347,6 +347,7 @@ static void srf04_remove(struct platform_device *pdev)
> iio_device_unregister(indio_dev);
>
> if (data->gpiod_power) {
> + pm_runtime_dont_use_autosuspend(data->dev);
> pm_runtime_disable(data->dev);
> pm_runtime_set_suspended(data->dev);
> }