[PATCH] iio: light: ltrf216a: fix runtime PM reference leak in error path

Vidhu Sarwal posted 1 patch 1 week, 5 days ago
drivers/iio/light/ltrf216a.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] iio: light: ltrf216a: fix runtime PM reference leak in error path
Posted by Vidhu Sarwal 1 week, 5 days ago
ltrf216a_get_lux() acquires a runtime PM reference by calling
ltrf216a_set_power_state(data, true). However, if
ltrf216a_read_data() fails, the function returns immediately without
dropping the reference.

This leaves the runtime PM usage count unbalanced, preventing the device
from autosuspending after a failed read.

Fix this by releasing the runtime PM reference before returning from the
error path.

Fixes: 83f0bcd40d5c ("iio: light: Add support for ltrf216a sensor")
Signed-off-by: Vidhu Sarwal <vidhu.linux@gmail.com>
---
 drivers/iio/light/ltrf216a.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/light/ltrf216a.c b/drivers/iio/light/ltrf216a.c
index 3f34ddc911b4..3ec239eb5831 100644
--- a/drivers/iio/light/ltrf216a.c
+++ b/drivers/iio/light/ltrf216a.c
@@ -247,8 +247,10 @@ static int ltrf216a_get_lux(struct ltrf216a_data *data)
 		return ret;
 
 	greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);
-	if (greendata < 0)
+	if (greendata < 0) {
+		ltrf216a_set_power_state(data, false);
 		return greendata;
+	}
 
 	ltrf216a_set_power_state(data, false);
 

base-commit: a13c140cc289c0b7b3770bce5b3ad42ab35074aa
-- 
2.53.0
Re: [PATCH] iio: light: ltrf216a: fix runtime PM reference leak in error path
Posted by Andy Shevchenko 1 week, 5 days ago
On Mon, Jul 13, 2026 at 07:58:29AM +0530, Vidhu Sarwal wrote:
> ltrf216a_get_lux() acquires a runtime PM reference by calling
> ltrf216a_set_power_state(data, true). However, if
> ltrf216a_read_data() fails, the function returns immediately without
> dropping the reference.
> 
> This leaves the runtime PM usage count unbalanced, preventing the device
> from autosuspending after a failed read.
> 
> Fix this by releasing the runtime PM reference before returning from the
> error path.

...

> static int ltrf216a_get_lux(struct ltrf216a_data *data)

>  	greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);
> -	if (greendata < 0)
> +	if (greendata < 0) {
> +		ltrf216a_set_power_state(data, false);
>  		return greendata;
> +	}
>  
>  	ltrf216a_set_power_state(data, false);

Wouldn't be simpler to do

	greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);

	ltrf216a_set_power_state(data, false);

	if (greendata < 0)
		return greendata;

?

Not insisting on this. Up to Jonathan and others to decide.


-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] iio: light: ltrf216a: fix runtime PM reference leak in error path
Posted by Jonathan Cameron 5 days, 23 hours ago
On Mon, 13 Jul 2026 15:36:51 +0300
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Mon, Jul 13, 2026 at 07:58:29AM +0530, Vidhu Sarwal wrote:
> > ltrf216a_get_lux() acquires a runtime PM reference by calling
> > ltrf216a_set_power_state(data, true). However, if
> > ltrf216a_read_data() fails, the function returns immediately without
> > dropping the reference.
> > 
> > This leaves the runtime PM usage count unbalanced, preventing the device
> > from autosuspending after a failed read.
> > 
> > Fix this by releasing the runtime PM reference before returning from the
> > error path.  
> 
> ...
> 
> > static int ltrf216a_get_lux(struct ltrf216a_data *data)  
> 
> >  	greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);
> > -	if (greendata < 0)
> > +	if (greendata < 0) {
> > +		ltrf216a_set_power_state(data, false);
> >  		return greendata;
> > +	}
> >  
> >  	ltrf216a_set_power_state(data, false);  
> 
> Wouldn't be simpler to do
> 
> 	greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);
> 
> 	ltrf216a_set_power_state(data, false);
> 
> 	if (greendata < 0)
> 		return greendata;
> 
> ?
> 
> Not insisting on this. Up to Jonathan and others to decide.
I merged a slight variant on what Andy has here to keep the greendata
assignment and check closer together.

diff --git a/drivers/iio/light/ltrf216a.c b/drivers/iio/light/ltrf216a.c
index aad96fc91565..dd8f3260b1d8 100644
--- a/drivers/iio/light/ltrf216a.c
+++ b/drivers/iio/light/ltrf216a.c
@@ -248,11 +248,10 @@ static int ltrf216a_get_lux(struct ltrf216a_data *data)
                return ret;
 
        greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);
+       ltrf216a_set_power_state(data, false);
        if (greendata < 0)
                return greendata;
 
-       ltrf216a_set_power_state(data, false);
-
        lux = greendata * data->info->lux_multiplier * LTRF216A_WIN_FAC;
 
        return lux;




> 
>
Re: [PATCH] iio: light: ltrf216a: fix runtime PM reference leak in error path
Posted by Joshua Crofts 1 week, 5 days ago
On Mon, 13 Jul 2026 07:58:29 +0530
Vidhu Sarwal <vidhu.linux@gmail.com> wrote:

> ltrf216a_get_lux() acquires a runtime PM reference by calling
> ltrf216a_set_power_state(data, true). However, if
> ltrf216a_read_data() fails, the function returns immediately without
> dropping the reference.
> 
> This leaves the runtime PM usage count unbalanced, preventing the device
> from autosuspending after a failed read.
> 
> Fix this by releasing the runtime PM reference before returning from the
> error path.
> 
> Fixes: 83f0bcd40d5c ("iio: light: Add support for ltrf216a sensor")
> Signed-off-by: Vidhu Sarwal <vidhu.linux@gmail.com>
> ---
>  drivers/iio/light/ltrf216a.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/light/ltrf216a.c b/drivers/iio/light/ltrf216a.c
> index 3f34ddc911b4..3ec239eb5831 100644
> --- a/drivers/iio/light/ltrf216a.c
> +++ b/drivers/iio/light/ltrf216a.c
> @@ -247,8 +247,10 @@ static int ltrf216a_get_lux(struct ltrf216a_data *data)
>  		return ret;
>  
>  	greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);
> -	if (greendata < 0)
> +	if (greendata < 0) {
> +		ltrf216a_set_power_state(data, false);
>  		return greendata;
> +	}
>  
>  	ltrf216a_set_power_state(data, false);

LGTM, Sashiko had an issue with the fact that pm_runtime_mark_last_busy() isn't
called before autosuspending, however pm_runtime_put_autosuspend() already calls
this function in its body.

Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>

-- 
Kind regards

CJD