iio: accel: kxsd9: Fix runtime PM error handling and scoping
PATCH 1/1 iio: accel: kxsd9: Fix runtime PM error handling and scoping
The kxsd9 driver uses pm_runtime_get_sync() in multiple places without checking its return value, which can lead to silent failures. It also relies on manual pm_runtime_put_autosuspend() calls in various return paths, which is prone to memory leaks if a return path is missed.
Fix the runtime PM handling by using the new scoped guards and proper error checking:
- In kxsd9_write_raw() and kxsd9_read_raw(), use the scoped PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND() macro to safely automate cleanup on function return, while maintaining the behavior of waking up a suspended device via pm_runtime_resume_and_get(). This allows us to cleanly remove all manual puts and error gotos.
- In kxsd9_buffer_preenable(), replace pm_runtime_get_sync() with pm_runtime_resume_and_get() to safely check for errors and handle usage-counter unwinding on failure.
- In kxsd9_common_remove(), replace pm_runtime_get_sync() with pm_runtime_resume_and_get() and only invoke kxsd9_power_down() if the resume succeeds. This prevents unbalanced regulator disable warnings and bus timeouts if the device is already suspended.
--- drivers/iio/accel/kxsd9.c | 50 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 26 deletions(-)
drivers/iio/accel/kxsd9.c +24 -26
@@ -139,68 +139,63 @@ static int kxsd9_write_raw(struct iio_dev *indio_dev, int val2, long mask) { - int ret = -EINVAL;struct kxsd9_state *st =iio_priv (indio_dev); -pm_runtime_get_sync (st->dev); +PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND (st->dev, pm); +if (PM_RUNTIME_ACQUIRE_ERR (&pm)) +return PM_RUNTIME_ACQUIRE_ERR (&pm);if (mask ==IIO_CHAN_INFO_SCALE ) {/* Check no integer component */ if (val)return -EINVAL ; - ret =kxsd9_write_scale (indio_dev, val2); +return kxsd9_write_scale (indio_dev, val2); } -pm_runtime_put_autosuspend (st->dev); - -return ret; +return -EINVAL ; }static int kxsd9_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan,int *val,int *val2,long mask) { -int ret = -EINVAL ;struct kxsd9_state *st =iio_priv (indio_dev);unsigned int regval;__be16 raw_val;u16 nval; +int ret; -pm_runtime_get_sync (st->dev); +PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND (st->dev, pm); +if (PM_RUNTIME_ACQUIRE_ERR (&pm)) +return PM_RUNTIME_ACQUIRE_ERR (&pm);switch (mask) {case IIO_CHAN_INFO_RAW : ret =regmap_bulk_read (st->map, chan->address, &raw_val,sizeof (raw_val));if (ret) - goto error_ret; +return ret; nval =be16_to_cpu (raw_val);/* Only 12 bits are valid */ nval >>=4 ; *val = nval; - ret =IIO_VAL_INT ; -break ; +return IIO_VAL_INT ;case IIO_CHAN_INFO_OFFSET :/* This has a bias of -2048 */ *val =KXSD9_ZERO_G_OFFSET ; - ret =IIO_VAL_INT ; -break ; +return IIO_VAL_INT ;case IIO_CHAN_INFO_SCALE : ret =regmap_read (st->map,KXSD9_REG_CTRL_C , ®val);if (ret <0 ) - goto error_ret; +return ret; *val =0 ; *val2 = kxsd9_micro_scales[regval &KXSD9_CTRL_C_FS_MASK ]; - ret =IIO_VAL_INT_PLUS_MICRO ; -break ; +return IIO_VAL_INT_PLUS_MICRO ; } -error_ret: -pm_runtime_put_autosuspend (st->dev); - -return ret; +return -EINVAL ; };static irqreturn_t kxsd9_trigger_handler (int irq,void *p)
@@ -239,9 +234,7 @@ static int kxsd9_buffer_preenable(struct iio_dev *indio_dev) {struct kxsd9_state *st =iio_priv (indio_dev); -pm_runtime_get_sync (st->dev); - -return 0 ; +return pm_runtime_resume_and_get (st->dev); }static int kxsd9_buffer_postdisable (struct iio_dev *indio_dev)
@@ -476,13 +469,18 @@ void kxsd9_common_remove(struct device *dev) {struct iio_dev *indio_dev =dev_get_drvdata (dev);struct kxsd9_state *st =iio_priv (indio_dev); +int ret;iio_triggered_buffer_cleanup (indio_dev);iio_device_unregister (indio_dev); -pm_runtime_get_sync (dev); -pm_runtime_put_noidle (dev); + + ret =pm_runtime_resume_and_get (dev);pm_runtime_disable (dev); -kxsd9_power_down (st); + +if (ret >=0 ) { +kxsd9_power_down (st); +pm_runtime_put_noidle (dev); + } }EXPORT_SYMBOL_NS (kxsd9_common_remove,"IIO_KXSD9" );
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 19 lines · Biren Pandya
The kxsd9 driver uses
pm_runtime_get_sync()in multiple places without checking its return value, which can lead to silent failures. It also relies on manualpm_runtime_put_autosuspend()calls in various return paths, which is prone to memory leaks if a return path is missed.Fix the runtime PM handling by using the new scoped guards and proper error checking:
- Inkxsd9_write_raw()andkxsd9_read_raw(), use the scopedPM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND()macro to safely automate cleanup on function return, while maintaining the behavior of waking up a suspended device viapm_runtime_resume_and_get(). This allows us to cleanly remove all manual puts and error gotos.
- Inkxsd9_buffer_preenable(), replacepm_runtime_get_sync()withpm_runtime_resume_and_get()to safely check for errors and handle usage-counter unwinding on failure.
- Inkxsd9_common_remove(), replacepm_runtime_get_sync()withpm_runtime_resume_and_get()and only invokekxsd9_power_down()if the resume succeeds. This prevents unbalanced regulator disable warnings and bus timeouts if the device is already suspended.
...
Quoted 6 lines
void
kxsd9_common_remove(struct device *dev)
{drivers/iio/accel/kxsd9.c +1
-0struct iio_dev *indio_dev =dev_get_drvdata (dev);struct kxsd9_state *st =iio_priv (indio_dev); +int ret;
drivers/iio/accel/kxsd9.c void kxsd9_common_remove(struct device *dev)
Quoted 10 lines
drivers/iio/accel/kxsd9.c +3
-3iio_triggered_buffer_cleanup (indio_dev);iio_device_unregister (indio_dev); -pm_runtime_get_sync (dev); -pm_runtime_put_noidle (dev); + + ret =pm_runtime_resume_and_get (dev);pm_runtime_disable (dev); -kxsd9_power_down (st); +if (ret >=0 ) {
Use standard pattern.
Quoted 4 lines
drivers/iio/accel/kxsd9.c +3
-0+kxsd9_power_down (st); +pm_runtime_put_noidle (dev); + } }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerQuoted 5 lines · On Tue, 16 Jun 2026 01:33:30 +0530 Biren Pandya <[email protected]> wrote:
The kxsd9 driver uses
pm_runtime_get_sync()in multiple places without checking its return value, which can lead to silent failures. It also relies on manualpm_runtime_put_autosuspend()calls in various return paths, which is prone to memory leaks if a return path is missed.
Quoted 7 lines
Fix the runtime PM handling by using the new scoped guards and proper error checking:
- Inkxsd9_write_raw()andkxsd9_read_raw(), use the scopedPM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND()macro to safely automate cleanup on function return, while maintaining the behavior of waking up a suspended device viapm_runtime_resume_and_get(). This allows us to cleanly remove all manual puts and error gotos.
Somewhere here mention that one of the puts was missing.
Quoted 9 lines
- In
kxsd9_buffer_preenable(), replacepm_runtime_get_sync()withpm_runtime_resume_and_get()to safely check for errors and handle usage-counter unwinding on failure.
- Inkxsd9_common_remove(), replacepm_runtime_get_sync()withpm_runtime_resume_and_get()and only invokekxsd9_power_down()if the resume succeeds. This prevents unbalanced regulator disable warnings and bus timeouts if the device is already suspended.Signed-off-by Biren Pandya <[email protected]>
Given this fixes a path, needs a Fixes tag.
Otherwise, what Andy said.
Quoted 14 lines
--- drivers/iio/accel/kxsd9.c | 50 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 26 deletions(-)drivers/iio/accel/kxsd9.c +0
-1@@ -139,68 +139,63 @@ static int kxsd9_write_raw(struct iio_dev *indio_dev, int val2, long mask) { - int ret = -EINVAL;struct kxsd9_state *st =iio_priv (indio_dev);
drivers/iio/accel/kxsd9.c static int kxsd9_write_raw(struct iio_dev *indio_dev, · line 151
Quoted 10 lines
drivers/iio/accel/kxsd9.c +3
-1-pm_runtime_get_sync (st->dev); +PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND (st->dev, pm); +if (PM_RUNTIME_ACQUIRE_ERR (&pm)) +return PM_RUNTIME_ACQUIRE_ERR (&pm);if (mask ==IIO_CHAN_INFO_SCALE ) {/* Check no integer component */ if (val)return -EINVAL ;
This one is a real fix that needed addressing as it exits with an error code leaving the power on.
Quoted 9 lines
drivers/iio/accel/kxsd9.c +2
-4- ret =kxsd9_write_scale (indio_dev, val2); +return kxsd9_write_scale (indio_dev, val2); } -pm_runtime_put_autosuspend (st->dev); - -return ret; +return -EINVAL ; }