Re: [PATCH 10/18] iio: adc: ad4170-4: Fix lock order inversion with IIO direct mode

From: Andy Shevchenko

Date: Thu Dec 03 2026 - 12:16:16 EST


On Thu, Dec 03, 2026 at 04:19:50PM +0100, Janani Sunil wrote:
> ad4170_read_raw() and ad4170_set_filter_type() take st->lock before
> claiming direct mode, while ad4170_write_raw() and the IIO core claim
> the mode lock first and then take st->lock. A raw read or filter_type
> write racing with a sampling_frequency, calibbias or calibscale write
> can deadlock, each holding one of the two locks.
>
> Claim direct mode before taking st->lock in both functions.

...

> + scoped_guard(mutex, &st->lock) {
> if (val == AD4170_SINC5_AVG || val == AD4170_SINC3)
> setup->filter_fs = clamp(setup->filter_fs,
> AD4170_SINC3_MIN_FS,

>
> ret = ad4170_write_channel_setup(st, chan->address, false);
> - iio_device_release_direct(indio_dev);

You may return directly from the scoped_guard(). Or rather

if (ret)
return ret;

> }
> + iio_device_release_direct(indio_dev);
>
> return ret;

return 0;

But I haven't looked at the full function to be sure.

...

> - guard(mutex)(&st->lock);
> - switch (info) {
> - case IIO_CHAN_INFO_RAW:
> + if (info == IIO_CHAN_INFO_RAW) {
> if (!iio_device_claim_direct(indio_dev))
> return -EBUSY;
>
> - ret = ad4170_read_sample(indio_dev, chan, val);
> + scoped_guard(mutex, &st->lock)
> + ret = ad4170_read_sample(indio_dev, chan, val);
> iio_device_release_direct(indio_dev);
> return ret;

Same here.

> + }
> +
> + guard(mutex)(&st->lock);

+ blank line.

> + switch (info) {

--
With Best Regards,
Andy Shevchenko