* [PATCH] iio: light: apds9306: fix PM reference leak in apds9306_read_data()
@ 2026-07-25 5:10 Moksh Panicker
2026-07-27 2:31 ` Jonathan Cameron
0 siblings, 1 reply; 3+ messages in thread
From: Moksh Panicker @ 2026-07-25 5:10 UTC (permalink / raw)
To: jic23; +Cc: dlechner, linux-iio, linux-kernel, stable, skhan, Moksh Panicker
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")
Cc: stable@vger.kernel.org
Signed-off-by: Moksh Panicker <mokshpanicker.7@gmail.com>
---
drivers/iio/light/apds9306.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
index 5ca4c87524fe..014db7a1787e 100644
--- a/drivers/iio/light/apds9306.c
+++ b/drivers/iio/light/apds9306.c
@@ -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)
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] iio: light: apds9306: fix PM reference leak in apds9306_read_data()
2026-07-25 5:10 [PATCH] iio: light: apds9306: fix PM reference leak in apds9306_read_data() Moksh Panicker
@ 2026-07-27 2:31 ` Jonathan Cameron
2026-08-01 8:53 ` Subhajit Ghosh
0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Cameron @ 2026-07-27 2:31 UTC (permalink / raw)
To: Moksh Panicker; +Cc: dlechner, linux-iio, linux-kernel, stable, skhan
On Sat, 25 Jul 2026 05:10:48 +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.
>
> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Moksh Panicker <mokshpanicker.7@gmail.com>
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.
> ---
> drivers/iio/light/apds9306.c | 19 +++++++++++--------
> 1 file changed, 11 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/iio/light/apds9306.c b/drivers/iio/light/apds9306.c
> index 5ca4c87524fe..014db7a1787e 100644
> --- a/drivers/iio/light/apds9306.c
> +++ b/drivers/iio/light/apds9306.c
> @@ -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)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] iio: light: apds9306: fix PM reference leak in apds9306_read_data()
2026-07-27 2:31 ` Jonathan Cameron
@ 2026-08-01 8:53 ` Subhajit Ghosh
0 siblings, 0 replies; 3+ messages in thread
From: Subhajit Ghosh @ 2026-08-01 8:53 UTC (permalink / raw)
To: Jonathan Cameron, Moksh Panicker
Cc: dlechner, linux-iio, linux-kernel, stable, skhan
On 27/7/26 12:01 pm, Jonathan Cameron wrote:
> On Sat, 25 Jul 2026 05:10:48 +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.
>>
>> 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")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Moksh Panicker <mokshpanicker.7@gmail.com>
> 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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-01 8:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-25 5:10 [PATCH] iio: light: apds9306: fix PM reference leak in apds9306_read_data() Moksh Panicker
2026-07-27 2:31 ` Jonathan Cameron
2026-08-01 8:53 ` Subhajit Ghosh
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®