iio: accel: kxsd9: validate input before acquiring runtime PM in write_raw runtime PM in write_raw
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/3 iio: accel: kxsd9: validate input before acquiring runtime PM in write_raw runtime PM in write_raw
Currently, kxsd9_write_raw() acquires a runtime PM reference at the very top of the function, before checking if the requested mask or value are actually supported. This causes unnecessary hardware wakeups for invalid inputs.
Refactor write_raw to validate inputs first. Additionally, modernize the PM runtime call by replacing pm_runtime_get_sync() with pm_runtime_resume_and_get() and adding the missing error check to prevent proceeding if power-on fails.
--- drivers/iio/accel/kxsd9.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/iio/accel/kxsd9.c +12 -9
@@ -139,18 +139,21 @@ 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); +int ret; -pm_runtime_get_sync (st->dev); +if (mask !=IIO_CHAN_INFO_SCALE ) +return -EINVAL ; -if (mask ==IIO_CHAN_INFO_SCALE ) { -/* Check no integer component */ -if (val) - ret = -EINVAL ; -else - ret =kxsd9_write_scale (indio_dev, val2); - } +/* Check no integer component */ +if (val) +return -EINVAL ; + + ret =pm_runtime_resume_and_get (st->dev); +if (ret <0 ) +return ret; + + ret =kxsd9_write_scale (indio_dev, val2);pm_runtime_put_autosuspend (st->dev);
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 5 lines · On Tue, 14 Jul 2026 17:55:21 +0530 Biren Pandya <[email protected]> wrote:
Currently,
kxsd9_write_raw()acquires a runtime PM reference at the very top of the function, before checking if the requested mask or value are actually supported. This causes unnecessary hardware wakeups for invalid inputs.
Quoted 7 lines
Refactor
write_rawto validate inputs first. Additionally, modernize the PM runtime call by replacingpm_runtime_get_sync()withpm_runtime_resume_and_get()and adding the missing error check to prevent proceeding if power-on fails.Suggested-by Jonathan Cameron <[email protected]>Signed-off-by Biren Pandya <[email protected]>
Hi Biren,
For a series there should always be a cover letter.
This patch looks fine but you may want to take the approaches I'll suggest in review of patch 2 and apply them here for consistency.
Thanks,
Jonathan
Quoted 39 lines
--- drivers/iio/accel/kxsd9.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)drivers/iio/accel/kxsd9.c +12
-9@@ -139,18 +139,21 @@ 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); +int ret; -pm_runtime_get_sync (st->dev); +if (mask !=IIO_CHAN_INFO_SCALE ) +return -EINVAL ; -if (mask ==IIO_CHAN_INFO_SCALE ) { -/* Check no integer component */ -if (val) - ret = -EINVAL ; -else - ret =kxsd9_write_scale (indio_dev, val2); - } +/* Check no integer component */ +if (val) +return -EINVAL ; + + ret =pm_runtime_resume_and_get (st->dev); +if (ret <0 ) +return ret; + + ret =kxsd9_write_scale (indio_dev, val2);pm_runtime_put_autosuspend (st->dev);
PATCH 2/3 iio: accel: kxsd9: modernize runtime PM in read_raw
In reply to a message not in the archiveReplace the legacy pm_runtime_get_sync() call with the modern pm_runtime_resume_and_get() in kxsd9_read_raw().
If the resume fails, we now correctly abort the read instead of carrying on and reading garbage data from the sleeping hardware.
--- drivers/iio/accel/kxsd9.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/iio/accel/kxsd9.c +7 -2
@@ -164,13 +164,15 @@ 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); + ret =pm_runtime_resume_and_get (st->dev); +if (ret <0 ) +return ret;switch (mask) {case IIO_CHAN_INFO_RAW :
@@ -199,6 +201,9 @@ static int kxsd9_read_raw(struct iio_dev *indio_dev, *val2 = kxsd9_micro_scales[regval &KXSD9_CTRL_C_FS_MASK ]; ret =IIO_VAL_INT_PLUS_MICRO ;break ; +default : + ret = -EINVAL ; +break ; } error_ret:
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH 2/3] iio: accel: kxsd9: modernize runtime PM in read_raw
Quoted 19 lines · On Tue, 14 Jul 2026 17:55:22 +0530 Biren Pandya <[email protected]> wrote:
Replace the legacy
pm_runtime_get_sync()call with the modernpm_runtime_resume_and_get()inkxsd9_read_raw().If the resume fails, we now correctly abort the read instead of carrying on and reading garbage data from the sleeping hardware.
Suggested-by Jonathan Cameron <[email protected]>Signed-off-by Biren Pandya <[email protected]>--- drivers/iio/accel/kxsd9.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)drivers/iio/accel/kxsd9.c +0
-0@@ -164,13 +164,15 @@ static int kxsd9_read_raw(struct iio_dev *indio_dev,struct iio_chan_spec const *chan, int *val, int *val2, long mask)
drivers/iio/accel/kxsd9.c static int kxsd9_read_raw(struct iio_dev *indio_dev, · line 173
Quoted 10 lines
drivers/iio/accel/kxsd9.c +2
-2{ -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); + ret =pm_runtime_resume_and_get (st->dev);
Look at PM_RUNTIME_ACQUIRE_AUTOSUSPEND() and consider how it would improve this code - particularly with respect to getting rid of the gotos. This function is almost a text book case of why that stuff is useful!
Thanks,
Jonathan
Quoted 15 lines
drivers/iio/accel/kxsd9.c +5
-0+if (ret <0 ) +return ret;switch (mask) {case IIO_CHAN_INFO_RAW :@@ -199,6 +201,9 @@ static int kxsd9_read_raw(struct iio_dev *indio_dev, *val2 = kxsd9_micro_scales[regval &KXSD9_CTRL_C_FS_MASK ]; ret =IIO_VAL_INT_PLUS_MICRO ;break ; +default : + ret = -EINVAL ; +break ; } error_ret:
PATCH 3/3 iio: accel: kxsd9: use dev_err_probe()
In reply to a message not in the archiveModernize the error paths in kxsd9_common_probe() by using dev_err_probe() instead of dev_err() when handling failures from regulators and buffer setup.
This simplifies the error handling and properly suppresses annoying and unnecessary log spam during -EPROBE_DEFER cases when regulators are not yet available during boot.
--- drivers/iio/accel/kxsd9.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/iio/accel/kxsd9.c +7 -6
@@ -433,10 +433,9 @@ int kxsd9_common_probe(struct device *dev, ret =devm_regulator_bulk_get (dev,ARRAY_SIZE (st->regs), st->regs); -if (ret) { -dev_err (dev,"Cannot get regulators\n" ); -return ret; - } +if (ret) +return dev_err_probe (dev, ret,"Cannot get regulators\n" ); +/* Default scaling */ st->scale =KXSD9_CTRL_C_FS_2G ;
@@ -447,13 +446,15 @@ int kxsd9_common_probe(struct device *dev, kxsd9_trigger_handler, &kxsd9_buffer_setup_ops);if (ret) { -dev_err (dev,"triggered buffer setup failed\n" ); +dev_err_probe (dev, ret,"triggered buffer setup failed\n" ); goto err_power_down; } ret =iio_device_register (indio_dev); -if (ret) +if (ret) { +dev_err_probe (dev, ret,"device register failed\n" ); goto err_cleanup_buffer; + }dev_set_drvdata (dev, indio_dev);
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH 3/3] iio: accel: kxsd9: use dev_err_probe()
Quoted 1 line · On Tue, 14 Jul 2026 17:55:23 +0530 Biren Pandya <[email protected]> wrote:
Modernize the error paths in
kxsd9_common_probe()by using
Not sure we need the modernise bit of this.
Quoted 6 lines
dev_err_probe()instead ofdev_err()when handling failures from regulators and buffer setup.This simplifies the error handling and properly suppresses annoying and unnecessary log spam during -EPROBE_DEFER cases when regulators are not yet available during boot.
Not sure we need to state too much about it here, but it does some more useful stuff than suppressing the log. Take a look at debug stuff for deferred probing.
Anyhow, this one looks fine to me though the true benefit only comes when these all become direct returns with a full move to devm based cleanup.
Jonathan
Quoted 42 lines
Signed-off-by Biren Pandya <[email protected]>--- drivers/iio/accel/kxsd9.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)drivers/iio/accel/kxsd9.c +7
-6@@ -433,10 +433,9 @@ int kxsd9_common_probe(struct device *dev, ret =devm_regulator_bulk_get (dev,ARRAY_SIZE (st->regs), st->regs); -if (ret) { -dev_err (dev,"Cannot get regulators\n" ); -return ret; - } +if (ret) +return dev_err_probe (dev, ret,"Cannot get regulators\n" ); +/* Default scaling */ st->scale =KXSD9_CTRL_C_FS_2G ;@@ -447,13 +446,15 @@ int kxsd9_common_probe(struct device *dev, kxsd9_trigger_handler, &kxsd9_buffer_setup_ops);if (ret) { -dev_err (dev,"triggered buffer setup failed\n" ); +dev_err_probe (dev, ret,"triggered buffer setup failed\n" ); goto err_power_down; } ret =iio_device_register (indio_dev); -if (ret) +if (ret) { +dev_err_probe (dev, ret,"device register failed\n" ); goto err_cleanup_buffer; + }dev_set_drvdata (dev, indio_dev);