[PATCH v2] iio: light: apds9306: fix PM reference leak in apds9306_read_data()

Moksh Panicker posted 1 patch 2 months ago
drivers/iio/light/apds9306.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
[PATCH v2] iio: light: apds9306: fix PM reference leak in apds9306_read_data()
Posted by Moksh Panicker 2 months ago
apds9306_read_data() calls pm_runtime_resume_and_get() but several
error paths return directly without calling pm_runtime_put_autosuspend(),
leaking the runtime PM reference and preventing the device from
autosuspending.

Use PM_RUNTIME_ACQUIRE_AUTOSUSPEND() and PM_RUNTIME_ACQUIRE_ERR() to
automatically handle runtime PM reference release on all return paths.

Fixes: 620d1e6c7a3f ("iio: light: Add support for APDS9306 Light Sensor")
Cc: stable@vger.kernel.org
Signed-off-by: Moksh Panicker <mokshpanicker.7@gmail.com>
---
Changes in v2:
 - Use PM_RUNTIME_ACQUIRE_AUTOSUSPEND() and PM_RUNTIME_ACQUIRE_ERR()
   instead of goto pattern as suggested by Jonathan Cameron

 drivers/iio/light/apds9306.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
index 5ca4c87524fe..d582bda4d847 100644
--- a/drivers/iio/light/apds9306.c
+++ b/drivers/iio/light/apds9306.c
@@ -469,9 +469,9 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
 	int status = 0;
 	u8 buff[3];
 
-	ret = pm_runtime_resume_and_get(data->dev);
-	if (ret)
-		return ret;
+	PM_RUNTIME_ACQUIRE_AUTOSUSPEND(data->dev, pm);
+	if (PM_RUNTIME_ACQUIRE_ERR(&pm))
+		return PM_RUNTIME_ACQUIRE_ERR(&pm);
 
 	ret = regmap_field_read(rf->intg_time, &intg_time_idx);
 	if (ret)
@@ -535,8 +535,6 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
 
 	*val = get_unaligned_le24(&buff);
 
-	pm_runtime_put_autosuspend(data->dev);
-
 	return 0;
 }
 
-- 
2.34.1
Re: [PATCH v2] iio: light: apds9306: fix PM reference leak in apds9306_read_data()
Posted by Jonathan Cameron 2 months ago
On Sun,  2 Aug 2026 19:07:01 +0000
Moksh Panicker <mokshpanicker.7@gmail.com> wrote:

> apds9306_read_data() calls pm_runtime_resume_and_get() but several
> error paths return directly without calling pm_runtime_put_autosuspend(),
> leaking the runtime PM reference and preventing the device from
> autosuspending.
> 
> Use PM_RUNTIME_ACQUIRE_AUTOSUSPEND() and PM_RUNTIME_ACQUIRE_ERR() to
> automatically handle runtime PM reference release on all return paths.
> 
> Fixes: 620d1e6c7a3f ("iio: light: Add support for APDS9306 Light Sensor")
> Cc: stable@vger.kernel.org
> Signed-off-by: Moksh Panicker <mokshpanicker.7@gmail.com>
Applied to the fixes-togreg branch of iio.git.

Note however that I'm not planning to do another fixes pull request for this
cycle so this will go upstream during the coming merge window

Thanks,

Jonathan

> ---
> Changes in v2:
>  - Use PM_RUNTIME_ACQUIRE_AUTOSUSPEND() and PM_RUNTIME_ACQUIRE_ERR()
>    instead of goto pattern as suggested by Jonathan Cameron
> 
>  drivers/iio/light/apds9306.c | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
> index 5ca4c87524fe..d582bda4d847 100644
> --- a/drivers/iio/light/apds9306.c
> +++ b/drivers/iio/light/apds9306.c
> @@ -469,9 +469,9 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
>  	int status = 0;
>  	u8 buff[3];
>  
> -	ret = pm_runtime_resume_and_get(data->dev);
> -	if (ret)
> -		return ret;
> +	PM_RUNTIME_ACQUIRE_AUTOSUSPEND(data->dev, pm);
> +	if (PM_RUNTIME_ACQUIRE_ERR(&pm))
> +		return PM_RUNTIME_ACQUIRE_ERR(&pm);
>  
>  	ret = regmap_field_read(rf->intg_time, &intg_time_idx);
>  	if (ret)
> @@ -535,8 +535,6 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
>  
>  	*val = get_unaligned_le24(&buff);
>  
> -	pm_runtime_put_autosuspend(data->dev);
> -
>  	return 0;
>  }
>