Skip to contentSkip to conversation

iio: light: apds9306: fix PM reference leak in apds9306_read_data()

PATCH Superseded Moksh Panicker 3 participants 3 messages Jul 25 – Aug 1

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: light: apds9306: fix PM reference leak in apds9306_read_data()

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.

Add a goto label before pm_runtime_put_autosuspend() and use it on all error paths after a successful pm_runtime_resume_and_get().

Fixes 620d1e6c7a3f ("iio: light: Add support for APDS9306 Light Sensor")
---
 drivers/iio/light/apds9306.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/iio/light/apds9306.c +11 -8
index 5ca4c87524fe..014db7a1787e 100644
@@ -475,19 +475,21 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
 
 	ret = regmap_field_read(rf->intg_time, &intg_time_idx);
 	if (ret)
-		return ret;
+		goto out_pm_put;
 
 	ret = regmap_field_read(rf->repeat_rate, &repeat_rate_idx);
 	if (ret)
-		return ret;
+		goto out_pm_put;
 
 	ret = regmap_field_read(rf->int_src, &int_src);
 	if (ret)
-		return ret;
+		goto out_pm_put;
 
 	intg_time = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx);
-	if (intg_time < 0)
-		return intg_time;
+	if (intg_time < 0) {
+		ret = intg_time;
+		goto out_pm_put;
+	}
 
 	/* Whichever is greater - integration time period or sampling period. */
 	delay = max(intg_time, apds9306_repeat_rate_period[repeat_rate_idx]);
@@ -510,7 +512,7 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
 						  APDS9306_ALS_INT_STAT_MASK)),
 				       APDS9306_ALS_READ_DATA_DELAY_US, delay * 2);
 	if (ret)
-		return ret;
+		goto out_pm_put;
 
 	/* If we reach here before the interrupt handler we push an event */
 	if ((status & APDS9306_ALS_INT_STAT_MASK)) {
@@ -530,14 +532,15 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
 	ret = regmap_bulk_read(data->regmap, reg, buff, sizeof(buff));
 	if (ret) {
 		dev_err_ratelimited(dev, "read data failed\n");
-		return ret;
+		goto out_pm_put;
 	}
 
 	*val = get_unaligned_le24(&buff);
 
+out_pm_put:
 	pm_runtime_put_autosuspend(data->dev);
 
-	return 0;
+	return ret;
 }
 
 static int apds9306_intg_time_get(struct apds9306_data *data, int *val2)
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 11 lines · On Sat, 25 Jul 2026 05:10:48 +0000 Moksh Panicker <[email protected]>…

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.

Add a goto label before pm_runtime_put_autosuspend() and use it on all error paths after a successful pm_runtime_resume_and_get().

Fixes 620d1e6c7a3f ("iio: light: Add support for APDS9306 Light Sensor")

Good find.
Please look at PM_RUNTIME_ACQUIRE_AUTOSUSPEND() and matching ERR macro.

Using that should simplify this fix quite a bit and generally give us nicer code.

Quoted 62 lines
---
 drivers/iio/light/apds9306.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/iio/light/apds9306.c +11 -8
index 5ca4c87524fe..014db7a1787e 100644
@@ -475,19 +475,21 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
 
 	ret = regmap_field_read(rf->intg_time, &intg_time_idx);
 	if (ret)
-		return ret;
+		goto out_pm_put;
 
 	ret = regmap_field_read(rf->repeat_rate, &repeat_rate_idx);
 	if (ret)
-		return ret;
+		goto out_pm_put;
 
 	ret = regmap_field_read(rf->int_src, &int_src);
 	if (ret)
-		return ret;
+		goto out_pm_put;
 
 	intg_time = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx);
-	if (intg_time < 0)
-		return intg_time;
+	if (intg_time < 0) {
+		ret = intg_time;
+		goto out_pm_put;
+	}
 
 	/* Whichever is greater - integration time period or sampling period. */
 	delay = max(intg_time, apds9306_repeat_rate_period[repeat_rate_idx]);
@@ -510,7 +512,7 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
 						  APDS9306_ALS_INT_STAT_MASK)),
 				       APDS9306_ALS_READ_DATA_DELAY_US, delay * 2);
 	if (ret)
-		return ret;
+		goto out_pm_put;
 
 	/* If we reach here before the interrupt handler we push an event */
 	if ((status & APDS9306_ALS_INT_STAT_MASK)) {
@@ -530,14 +532,15 @@ static int apds9306_read_data(struct apds9306_data *data, int *val, int reg)
 	ret = regmap_bulk_read(data->regmap, reg, buff, sizeof(buff));
 	if (ret) {
 		dev_err_ratelimited(dev, "read data failed\n");
-		return ret;
+		goto out_pm_put;
 	}
 
 	*val = get_unaligned_le24(&buff);
 
+out_pm_put:
 	pm_runtime_put_autosuspend(data->dev);
 
-	return 0;
+	return ret;
 }
 
 static int apds9306_intg_time_get(struct apds9306_data *data, int *val2)
maintainer
#2 lore raw
Quoted 11 lines · Jonathan Cameron
Quoted 8 lines · On Sat, 25 Jul 2026 05:10:48 +0000 Moksh Panicker <[email protected]>…

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.

Add a goto label before pm_runtime_put_autosuspend() and use it on all error paths after a successful pm_runtime_resume_and_get().

Quoted 7 lines
Quoted 3 lines
Fixes 620d1e6c7a3f ("iio: light: Add support for APDS9306 Light Sensor")

Good find.
Please look at PM_RUNTIME_ACQUIRE_AUTOSUSPEND() and matching ERR macro.

Using that should simplify this fix quite a bit and generally give us nicer code.

Nice. Ditto. I have to read about it too. Thanks Jonathan.