Skip to contentSkip to conversation

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

PATCH Applied Vidhu Sarwal 4 participants 4 messages Jul 13 – Jul 19 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: light: ltrf216a: fix runtime PM reference leak in error path

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")
---
 drivers/iio/light/ltrf216a.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/light/ltrf216a.c +3 -1
index 3f34ddc911b4..3ec239eb5831 100644
@@ -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

Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · On Mon, 13 Jul 2026 07:58:29 +0530 Vidhu Sarwal <[email protected]> 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")
---
 drivers/iio/light/ltrf216a.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/light/ltrf216a.c +0 -0
index 3f34ddc911b4..3ec239eb5831 100644

drivers/iio/light/ltrf216a.c static int ltrf216a_get_lux(struct ltrf216a_data *data) · line 255

Quoted 10 lines
drivers/iio/light/ltrf216a.c +3 -1
@@ -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.

Signature · 3 lines
Kind regards

CJD
#2 lore raw
In reply to Vidhu Sarwal
Quoted 10 lines · Vidhu Sarwal

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.

...

drivers/iio/light/ltrf216a.c static int ltrf216a_get_lux(struct ltrf216a_data *data) · line 255

Quoted 10 lines

static int ltrf216a_get_lux(struct ltrf216a_data *data)

drivers/iio/light/ltrf216a.c +3 -1
 	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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 34 lines · On Mon, 13 Jul 2026 15:36:51 +0300 Andy Shevchenko…
Quoted 10 lines · Vidhu Sarwal

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.

...

Review

Quoted 10 lines

static int ltrf216a_get_lux(struct ltrf216a_data *data)

(quoted diff) +3 -1
 	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;
Quoted 3 lines

?

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.

drivers/iio/light/ltrf216a.c +1 -2
index aad96fc91565..dd8f3260b1d8 100644
@@ -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;
Quoted 2 lines