Re: [PATCH v3 2/2] iio: accel: kxsd9: fix runtime PM leaks and unchecked returns
From: Andy Shevchenko
Date: Mon Jul 06 2026 - 16:04:32 EST
On Mon, Jul 06, 2026 at 01:16:52PM +0530, Biren Pandya wrote:
> The kxsd9 driver previously used manual pm_runtime_get_sync() and
> pm_runtime_put_autosuspend() calls in various return paths, which is
> prone to memory leaks if a return path is missed. Specifically, in
> kxsd9_write_raw(), if the user provides a non-zero integer component
> for scale, the function previously returned -EINVAL directly, leaking
> the runtime PM usage counter.
>
> Switch to using the new scoped-based PM_RUNTIME_ACQUIRE_AUTOSUSPEND()
> and PM_RUNTIME_ACQUIRE_ERR() macros to automatically handle dropping
> the PM usage counter upon return. This simplifies the logic by allowing
> early returns in error paths.
>
> In remove(), use pm_runtime_resume_and_get() with an error check to
> prevent unchecked return issues, and perform a best-effort power down.
...
> 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_device_unregister(indio_dev);
> iio_triggered_buffer_cleanup(indio_dev);
> - pm_runtime_get_sync(dev);
> - pm_runtime_put_noidle(dev);
> +
> + ret = pm_runtime_resume_and_get(dev);
> + if (ret < 0)
> + dev_warn(dev, "Failed to resume device (%d)\n", ret);
> +
> pm_runtime_disable(dev);
> + if (ret >= 0)
> + pm_runtime_put_noidle(dev);
Has this been discussed already? Because it looks to me a bit strange to have
_disable() be called before _put_noidle(). Also the commit message is unclear
about the flow change.
> kxsd9_power_down(st);
> }
--
With Best Regards,
Andy Shevchenko