Skip to contentSkip to conversation

iio: Fix runtime PM leaks across multiple drivers

PATCH Applied Biren Pandya 4 participants 14 messages Jun 14 – Jul 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover iio: Fix runtime PM leaks across multiple drivers

Hi Jonathan,

While auditing the IIO subsystem, I noticed a recurring pattern in several drivers where early returns or error paths fail to drop their pm_runtime_get_sync() references. This permanently leaves the device active and prevents autosuspension.

This series provides four independent fixes for these imbalances across the kxsd9, mpu3050, mpl115, and gp2ap002 drivers.

Because these drivers are independent, these patches do not depend on each other and can be merged individually.

Best regards,
Biren Pandya

 drivers/iio/accel/kxsd9.c       |  5 +++--
 drivers/iio/gyro/mpu3050-core.c | 16 +++++++++++-----
 drivers/iio/light/gp2ap002.c    |  2 +-
 drivers/iio/pressure/mpl115.c   |  4 ++--
 4 files changed, 17 insertions(+), 10 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/4 iio: accel: kxsd9: fix runtime PM imbalance on write_raw() error

kxsd9_write_raw() takes a runtime PM reference with pm_runtime_get_sync() but returns -EINVAL directly when a scale with a non-zero integer part is requested, skipping the matching pm_runtime_put_autosuspend(). This leaks a runtime PM usage-counter reference on every such write, after which the device can no longer autosuspend.

Set the error code and fall through to the existing put instead of returning early.

Assisted-by Claude:claude-opus-4-8 coccinelle
---
 drivers/iio/accel/kxsd9.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iio/accel/kxsd9.c +3 -2
index 4717d80fc24a..7ac885d94d7f 100644
@@ -147,8 +147,9 @@ static int kxsd9_write_raw(struct iio_dev *indio_dev,
 	if (mask == IIO_CHAN_INFO_SCALE) {
 		/* Check no integer component */
 		if (val)
-			return -EINVAL;
-		ret = kxsd9_write_scale(indio_dev, val2);
+			ret = -EINVAL;
+		else
+			ret = kxsd9_write_scale(indio_dev, val2);
 	}
 
 	pm_runtime_put_autosuspend(st->dev);
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer Applied
#2 lore raw

Re: [PATCH v1 1/4] iio: accel: kxsd9: fix runtime PM imbalance on write_raw() error

Quoted 11 lines · On Sun, 14 Jun 2026 12:45:46 +0530 Biren Pandya <[email protected]> wrote:

kxsd9_write_raw() takes a runtime PM reference with pm_runtime_get_sync() but returns -EINVAL directly when a scale with a non-zero integer part is requested, skipping the matching pm_runtime_put_autosuspend(). This leaks a runtime PM usage-counter reference on every such write, after which the device can no longer autosuspend.

Set the error code and fall through to the existing put instead of returning early.

Assisted-by Claude:claude-opus-4-8 coccinelle

Good find. This one is nasty as it is easily triggered from userspace. So I'm going to pick this up as it stands. However, if you are interested in a doing a follow up series that cleans things up further:

- The pm_runtime_get_sync() return value is not checked anywhere in this driver. So good to do that.
- The various places in this driver (including this one) where runtime_pm is involved would be nicer handled using the relatively new PM_RUNTIME_ACQUIRE_AUTOSUSPEND() / PM_RUNTIME_ACQUIRE_ERR()

Probably makes sense to do both of those in one go given they'll be touching the same code.

Applied to the fixes-togreg branch of iio.git and marked for stable. Note I'll be rebasing that branch on rc1 before doing a pull request.

Thanks,

Jonathan

Quoted 20 lines
---
 drivers/iio/accel/kxsd9.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iio/accel/kxsd9.c +3 -2
index 4717d80fc24a..7ac885d94d7f 100644
@@ -147,8 +147,9 @@ static int kxsd9_write_raw(struct iio_dev *indio_dev,
 	if (mask == IIO_CHAN_INFO_SCALE) {
 		/* Check no integer component */
 		if (val)
-			return -EINVAL;
-		ret = kxsd9_write_scale(indio_dev, val2);
+			ret = -EINVAL;
+		else
+			ret = kxsd9_write_scale(indio_dev, val2);
 	}
 
 	pm_runtime_put_autosuspend(st->dev);
#3 lore raw

PATCH 2/4 iio: gyro: mpu3050: fix runtime PM leak when enabling trigger fails

In reply to Biren Pandya

mpu3050_drdy_trigger_set_state() calls pm_runtime_get_sync() when the trigger is enabled, but several error paths in the enable branch return directly without dropping the usage counter again. pm_runtime_get_sync() increments the usage counter, so every failed enable leaks a runtime PM reference and the device can no longer autosuspend. The driver state flag hw_irq_trigger is also left set after a failed enable.

Jump to a common error label that drops the reference and clears hw_irq_trigger, mirroring the disable path. The success path deliberately keeps the reference, as the device must stay resumed while the trigger is active.

Assisted-by Claude:claude-opus-4-8 coccinelle
---
 drivers/iio/gyro/mpu3050-core.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iio/gyro/mpu3050-core.c +11 -5
index d84e04e4b431..783be9a786df 100644
@@ -994,14 +994,14 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 		/* Disable all things in the FIFO */
 		ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Reset and enable the FIFO */
 		ret = regmap_set_bits(mpu3050->map, MPU3050_USR_CTRL,
 				      MPU3050_USR_CTRL_FIFO_EN |
 				      MPU3050_USR_CTRL_FIFO_RST);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		mpu3050->pending_fifo_footer = false;
 
@@ -1013,12 +1013,12 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 				   MPU3050_FIFO_EN_GYRO_ZOUT |
 				   MPU3050_FIFO_EN_FOOTER);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Configure the sample engine */
 		ret = mpu3050_start_sampling(mpu3050);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Clear IRQ flag */
 		ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
@@ -1037,10 +1037,16 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 
 		ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val);
 		if (ret)
-			return ret;
+			goto err_power_down;
 	}
 
 	return 0;
+
+err_power_down:
+	pm_runtime_put_autosuspend(mpu3050->dev);
+	mpu3050->hw_irq_trigger = false;
+
+	return ret;
 }
 
 static const struct iio_trigger_ops mpu3050_trigger_ops = {
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#4 lore raw

Re: [PATCH v1 2/4] iio: gyro: mpu3050: fix runtime PM leak when enabling trigger fails

Quoted 7 lines · On Sun, 14 Jun 2026 12:45:47 +0530 Biren Pandya <[email protected]> wrote:

mpu3050_drdy_trigger_set_state() calls pm_runtime_get_sync() when the trigger is enabled, but several error paths in the enable branch return directly without dropping the usage counter again. pm_runtime_get_sync() increments the usage counter, so every failed enable leaks a runtime PM reference and the device can no longer autosuspend. The driver state flag hw_irq_trigger is also left set after a failed enable.

Quoted 7 lines

Jump to a common error label that drops the reference and clears hw_irq_trigger, mirroring the disable path. The success path deliberately keeps the reference, as the device must stay resumed while the trigger is active.

Assisted-by Claude:claude-opus-4-8 coccinelle

This looks more or less fine to me, but I'd like to leave it on list for a little while so hopefully someone who is more familiar with the device can take a look.

A suggestion below for a less minimal but perhaps nicer way of doing this.

Quoted 41 lines
---
 drivers/iio/gyro/mpu3050-core.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iio/gyro/mpu3050-core.c +4 -4
index d84e04e4b431..783be9a786df 100644
@@ -994,14 +994,14 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 		/* Disable all things in the FIFO */
 		ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Reset and enable the FIFO */
 		ret = regmap_set_bits(mpu3050->map, MPU3050_USR_CTRL,
 				      MPU3050_USR_CTRL_FIFO_EN |
 				      MPU3050_USR_CTRL_FIFO_RST);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		mpu3050->pending_fifo_footer = false;
 
@@ -1013,12 +1013,12 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 				   MPU3050_FIFO_EN_GYRO_ZOUT |
 				   MPU3050_FIFO_EN_FOOTER);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Configure the sample engine */
 		ret = mpu3050_start_sampling(mpu3050);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Clear IRQ flag */
 		ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);

drivers/iio/gyro/mpu3050-core.c static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig, · line 1045

Quoted 10 lines
drivers/iio/gyro/mpu3050-core.c +3 -1
@@ -1037,10 +1037,16 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 
 		ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val);
 		if (ret)
-			return ret;
+			goto err_power_down;
 	}
 
 	return 0;
+
+err_power_down:

I'm not particularly keen on a goto error block that only applies to one leg of an if / else. Given there is effectively not sharing of code between the enable and disable paths, I'd rather see this whole thing broken into two helpers. mpu3050_drdy_trigger_enable() and mpu3050_drdy_trigger_disable()

Then the error unwind would not have the issue of only applying to some code paths as it would be in the helper. Obviously it is a more invasive change but I think it is worth doing rather that having a minimal fix then sweeping around later to replace all that code.

Quoted 7 lines
drivers/iio/gyro/mpu3050-core.c +4 -0
+	pm_runtime_put_autosuspend(mpu3050->dev);
+	mpu3050->hw_irq_trigger = false;
+
+	return ret;
 }
 
 static const struct iio_trigger_ops mpu3050_trigger_ops = {
#5 lore raw

PATCH v3 1/1 iio: gyro: mpu3050: fix runtime PM leak when enabling trigger fails

In reply to Biren Pandya

mpu3050_drdy_trigger_set_state() calls pm_runtime_get_sync() when the trigger is enabled, but several error paths in the enable branch return directly without dropping the usage counter again. pm_runtime_get_sync() increments the usage counter, so every failed enable leaks a runtime PM reference and the device can no longer autosuspend. The driver state flag hw_irq_trigger is also left set after a failed enable.

Jump to a common error label that drops the reference and clears hw_irq_trigger, mirroring the disable path. The success path deliberately keeps the reference, as the device must stay resumed while the trigger is active.

Assisted-by Claude:claude-opus-4-8 coccinelle
---
 drivers/iio/gyro/mpu3050-core.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/iio/gyro/mpu3050-core.c +12 -6
index d84e04e4b4314..1f23e700a0dae 100644
@@ -761,7 +761,7 @@ static const struct iio_info mpu3050_info = {
 	.attrs = &mpu3050_attribute_group,
 };
 
-/**
+/*
  * mpu3050_read_mem() - read MPU-3050 internal memory
  * @mpu3050: device to read from
  * @bank: target bank
@@ -994,14 +994,14 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 		/* Disable all things in the FIFO */
 		ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Reset and enable the FIFO */
 		ret = regmap_set_bits(mpu3050->map, MPU3050_USR_CTRL,
 				      MPU3050_USR_CTRL_FIFO_EN |
 				      MPU3050_USR_CTRL_FIFO_RST);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		mpu3050->pending_fifo_footer = false;
 
@@ -1013,12 +1013,12 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 				   MPU3050_FIFO_EN_GYRO_ZOUT |
 				   MPU3050_FIFO_EN_FOOTER);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Configure the sample engine */
 		ret = mpu3050_start_sampling(mpu3050);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Clear IRQ flag */
 		ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
@@ -1037,10 +1037,16 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 
 		ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val);
 		if (ret)
-			return ret;
+			goto err_power_down;
 	}
 
 	return 0;
+
+err_power_down:
+	pm_runtime_put_autosuspend(mpu3050->dev);
+	mpu3050->hw_irq_trigger = false;
+
+	return ret;
 }
 
 static const struct iio_trigger_ops mpu3050_trigger_ops = {
Signature · 1 line
2.50.1 (Apple Git-155)
#6 lore raw

Re: [PATCH v3] iio: gyro: mpu3050: fix runtime PM leak when enabling trigger fails

Quoted 11 lines · Biren Pandya

mpu3050_drdy_trigger_set_state() calls pm_runtime_get_sync() when the trigger is enabled, but several error paths in the enable branch return directly without dropping the usage counter again. pm_runtime_get_sync() increments the usage counter, so every failed enable leaks a runtime PM reference and the device can no longer autosuspend. The driver state flag hw_irq_trigger is also left set after a failed enable.

Jump to a common error label that drops the reference and clears hw_irq_trigger, mirroring the disable path. The success path deliberately keeps the reference, as the device must stay resumed while the trigger is active.

...

drivers/iio/gyro/mpu3050-core.c static const struct iio_info mpu3050_info = { · line 767

Quoted 5 lines
drivers/iio/gyro/mpu3050-core.c +1 -1
-/**
+/*
  * mpu3050_read_mem() - read MPU-3050 internal memory
  * @mpu3050: device to read from
  * @bank: target bank

Stray and actually wrong change. Please, use more brains and less AI.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

PATCH v4 1/1 iio: gyro: mpu3050: fix runtime PM leak when enabling trigger fails

In reply to Biren Pandya

mpu3050_drdy_trigger_set_state() calls pm_runtime_get_sync() when the trigger is enabled, but several error paths in the enable branch return directly without dropping the usage counter again. pm_runtime_get_sync() increments the usage counter, so every failed enable leaks a runtime PM reference and the device can no longer autosuspend. The driver state flag hw_irq_trigger is also left set after a failed enable.

Jump to a common error label that drops the reference and clears hw_irq_trigger, mirroring the disable path. The success path deliberately keeps the reference, as the device must stay resumed while the trigger is active.

---
Changes in v4:
- Reverted stray change converting mpu3050_read_mem() kernel-doc to a regular comment.
- Dropped AI assistance tag from the commit message.
Changes in v3:
- Changed to a unified error label instead of individual drops.
Changes in v2:
- Fixed a missed PM release in the first error branch.

 drivers/iio/gyro/mpu3050-core.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iio/gyro/mpu3050-core.c +11 -5
index d84e04e4b4314..783be9a786df0 100644
@@ -994,14 +994,14 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 		/* Disable all things in the FIFO */
 		ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Reset and enable the FIFO */
 		ret = regmap_set_bits(mpu3050->map, MPU3050_USR_CTRL,
 				      MPU3050_USR_CTRL_FIFO_EN |
 				      MPU3050_USR_CTRL_FIFO_RST);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		mpu3050->pending_fifo_footer = false;
 
@@ -1013,12 +1013,12 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 				   MPU3050_FIFO_EN_GYRO_ZOUT |
 				   MPU3050_FIFO_EN_FOOTER);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Configure the sample engine */
 		ret = mpu3050_start_sampling(mpu3050);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Clear IRQ flag */
 		ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
@@ -1037,10 +1037,16 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 
 		ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val);
 		if (ret)
-			return ret;
+			goto err_power_down;
 	}
 
 	return 0;
+
+err_power_down:
+	pm_runtime_put_autosuspend(mpu3050->dev);
+	mpu3050->hw_irq_trigger = false;
+
+	return ret;
 }
 
 static const struct iio_trigger_ops mpu3050_trigger_ops = {
Signature · 1 line
2.50.1 (Apple Git-155)
#8 lore raw

Re: [PATCH v4] iio: gyro: mpu3050: fix runtime PM leak when enabling trigger fails

Quoted 7 lines · On Fri, 17 Jul 2026 23:40:48 +0530 Biren Pandya <[email protected]> wrote:

mpu3050_drdy_trigger_set_state() calls pm_runtime_get_sync() when the trigger is enabled, but several error paths in the enable branch return directly without dropping the usage counter again. pm_runtime_get_sync() increments the usage counter, so every failed enable leaks a runtime PM reference and the device can no longer autosuspend. The driver state flag hw_irq_trigger is also left set after a failed enable.

Quoted 6 lines

Jump to a common error label that drops the reference and clears hw_irq_trigger, mirroring the disable path. The success path deliberately keeps the reference, as the device must stay resumed while the trigger is active.

Never send a new version in reply to an old one. It both leads to confusing threads and also generally increases the time before a reviewer or maintainer will look at it.

We mostly work backwards through our inboxes because old stuff is often resolved in later threads!

drivers/iio/gyro/mpu3050-core.c static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,

Quoted 17 lines

---
Changes in v4:
- Reverted stray change converting mpu3050_read_mem() kernel-doc to a regular comment.
- Dropped AI assistance tag from the commit message.
Changes in v3:
- Changed to a unified error label instead of individual drops.
Changes in v2:
- Fixed a missed PM release in the first error branch.

 drivers/iio/gyro/mpu3050-core.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iio/gyro/mpu3050-core.c +0 -0
index d84e04e4b4314..783be9a786df0 100644
@@ -994,14 +994,14 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,

If touching this code, can we also check for errors from the pm_runtime_get_sync().

A number of paths were cleaned up in
acc3949aab3e ("iio: gyro: mpu3050-core: fix pm_runtime error handling") but oddly not this one. Antoniu any idea why you didn't address this one?

Quoted 49 lines
drivers/iio/gyro/mpu3050-core.c +11 -5
 		/* Disable all things in the FIFO */
 		ret = regmap_write(mpu3050->map, MPU3050_FIFO_EN, 0);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Reset and enable the FIFO */
 		ret = regmap_set_bits(mpu3050->map, MPU3050_USR_CTRL,
 				      MPU3050_USR_CTRL_FIFO_EN |
 				      MPU3050_USR_CTRL_FIFO_RST);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		mpu3050->pending_fifo_footer = false;
 
@@ -1013,12 +1013,12 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 				   MPU3050_FIFO_EN_GYRO_ZOUT |
 				   MPU3050_FIFO_EN_FOOTER);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Configure the sample engine */
 		ret = mpu3050_start_sampling(mpu3050);
 		if (ret)
-			return ret;
+			goto err_power_down;
 
 		/* Clear IRQ flag */
 		ret = regmap_read(mpu3050->map, MPU3050_INT_STATUS, &val);
@@ -1037,10 +1037,16 @@ static int mpu3050_drdy_trigger_set_state(struct iio_trigger *trig,
 
 		ret = regmap_write(mpu3050->map, MPU3050_INT_CFG, val);
 		if (ret)
-			return ret;
+			goto err_power_down;
 	}
 
 	return 0;
+
+err_power_down:
+	pm_runtime_put_autosuspend(mpu3050->dev);
+	mpu3050->hw_irq_trigger = false;
+
+	return ret;
 }
 
 static const struct iio_trigger_ops mpu3050_trigger_ops = {
#9 lore raw

PATCH 3/4 iio: pressure: mpl115: fix runtime PM leak on read error

In reply to Biren Pandya

mpl115_read_raw() takes a runtime PM reference with pm_runtime_get_sync() before reading the processed pressure or raw temperature, but on the read error path it returns without calling pm_runtime_put_autosuspend(). Each failed read therefore leaks a runtime PM reference and prevents the device from autosuspending.

Drop the reference before checking the return value so both the success and error paths are balanced.

Assisted-by Claude:claude-opus-4-8 coccinelle
---
 drivers/iio/pressure/mpl115.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/pressure/mpl115.c +2 -2
index 830a5065c008..16e112b796ba 100644
@@ -106,18 +106,18 @@ static int mpl115_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_PROCESSED:
 		pm_runtime_get_sync(data->dev);
 		ret = mpl115_comp_pressure(data, val, val2);
+		pm_runtime_put_autosuspend(data->dev);
 		if (ret < 0)
 			return ret;
-		pm_runtime_put_autosuspend(data->dev);
 
 		return IIO_VAL_INT_PLUS_MICRO;
 	case IIO_CHAN_INFO_RAW:
 		pm_runtime_get_sync(data->dev);
 		/* temperature -5.35 C / LSB, 472 LSB is 25 C */
 		ret = mpl115_read_temp(data);
+		pm_runtime_put_autosuspend(data->dev);
 		if (ret < 0)
 			return ret;
-		pm_runtime_put_autosuspend(data->dev);
 		*val = ret >> 6;
 
 		return IIO_VAL_INT;
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#10 lore raw

Re: [PATCH v1 3/4] iio: pressure: mpl115: fix runtime PM leak on read error

Quoted 11 lines · On Sun, 14 Jun 2026 12:45:48 +0530 Biren Pandya <[email protected]> wrote:

mpl115_read_raw() takes a runtime PM reference with pm_runtime_get_sync() before reading the processed pressure or raw temperature, but on the read error path it returns without calling pm_runtime_put_autosuspend(). Each failed read therefore leaks a runtime PM reference and prevents the device from autosuspending.

Drop the reference before checking the return value so both the success and error paths are balanced.

Assisted-by Claude:claude-opus-4-8 coccinelle

Applied and marked for stable.
Thanks,

Jonathan

Quoted 29 lines
---
 drivers/iio/pressure/mpl115.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/pressure/mpl115.c +2 -2
index 830a5065c008..16e112b796ba 100644
@@ -106,18 +106,18 @@ static int mpl115_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_PROCESSED:
 		pm_runtime_get_sync(data->dev);
 		ret = mpl115_comp_pressure(data, val, val2);
+		pm_runtime_put_autosuspend(data->dev);
 		if (ret < 0)
 			return ret;
-		pm_runtime_put_autosuspend(data->dev);
 
 		return IIO_VAL_INT_PLUS_MICRO;
 	case IIO_CHAN_INFO_RAW:
 		pm_runtime_get_sync(data->dev);
 		/* temperature -5.35 C / LSB, 472 LSB is 25 C */
 		ret = mpl115_read_temp(data);
+		pm_runtime_put_autosuspend(data->dev);
 		if (ret < 0)
 			return ret;
-		pm_runtime_put_autosuspend(data->dev);
 		*val = ret >> 6;
 
 		return IIO_VAL_INT;
#11 lore raw

PATCH 4/4 iio: light: gp2ap002: fix runtime PM leak on read error

In reply to Biren Pandya

gp2ap002_read_raw() calls pm_runtime_get_sync() before reading the lux value, but if gp2ap002_get_lux() fails, it returns directly. This skips the pm_runtime_put_autosuspend() call at the "out" label, permanently leaking a runtime PM reference and preventing the device from autosuspending.

Replace the direct return with a "goto out" to ensure the reference is properly dropped on the error path.

---
 drivers/iio/light/gp2ap002.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/light/gp2ap002.c +1 -1
index a0d8a58f2704..886accf5e859 100644
@@ -258,7 +258,7 @@ static int gp2ap002_read_raw(struct iio_dev *indio_dev,
 		case IIO_LIGHT:
 			ret = gp2ap002_get_lux(gp2ap002);
 			if (ret < 0)
-				return ret;
+				goto out;
 			*val = ret;
 			ret = IIO_VAL_INT;
 			goto out;
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#12 lore raw

Re: [PATCH v1 4/4] iio: light: gp2ap002: fix runtime PM leak on read error

Quoted 10 lines · On Sun, 14 Jun 2026 12:45:49 +0530 Biren Pandya <[email protected]> wrote:

gp2ap002_read_raw() calls pm_runtime_get_sync() before reading the lux value, but if gp2ap002_get_lux() fails, it returns directly. This skips the pm_runtime_put_autosuspend() call at the "out" label, permanently leaking a runtime PM reference and preventing the device from autosuspending.

Replace the direct return with a "goto out" to ensure the reference is properly dropped on the error path.

Similar to patch one this would benefit from a follow up cleanup to make use of the ACQUIRE macros.

This is good though, so applied to the fixes-togreg branch of iio.git and marked for stable.

Thanks

Jonathan

Quoted 17 lines
---
 drivers/iio/light/gp2ap002.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/light/gp2ap002.c +1 -1
index a0d8a58f2704..886accf5e859 100644
@@ -258,7 +258,7 @@ static int gp2ap002_read_raw(struct iio_dev *indio_dev,
 		case IIO_LIGHT:
 			ret = gp2ap002_get_lux(gp2ap002);
 			if (ret < 0)
-				return ret;
+				goto out;
 			*val = ret;
 			ret = IIO_VAL_INT;
 			goto out;
maintainer
#13 lore raw
In reply to Biren Pandya
Quoted 7 lines · On Sun, 14 Jun 2026 12:45:45 +0530 Biren Pandya <[email protected]> wrote:

Hi Jonathan,

While auditing the IIO subsystem, I noticed a recurring pattern in several drivers where early returns or error paths fail to drop their pm_runtime_get_sync() references. This permanently leaves the device active and prevents autosuspension.

Quoted 5 lines

This series provides four independent fixes for these imbalances across the kxsd9, mpu3050, mpl115, and gp2ap002 drivers.

Because these drivers are independent, these patches do not depend on each other and can be merged individually.

Oops. Too me until just after I'd replied to patch 4 to notice that these don't have the necessary Fixes tags.

I've added them, but keep that in mind for next time you send a fix.

Thanks,

Jonathan

Quoted 11 lines

Best regards,
Biren Pandya

 drivers/iio/accel/kxsd9.c       |  5 +++--
 drivers/iio/gyro/mpu3050-core.c | 16 +++++++++++-----
 drivers/iio/light/gp2ap002.c    |  2 +-
 drivers/iio/pressure/mpl115.c   |  4 ++--
 4 files changed, 17 insertions(+), 10 deletions(-)