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

From: Janani Sunil

Date: Thu Dec 03 2026 - 10:21:22 EST


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.

Fixes: 5731f2a06c0f ("iio: adc: Add basic support for AD4170-4")
Fixes: a770f70b4f5b ("iio: adc: ad4170-4: Add digital filter and sample frequency config support")
Signed-off-by: Janani Sunil <janani.sunil@xxxxxxxxxx>
---
drivers/iio/adc/ad4170-4.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/adc/ad4170-4.c b/drivers/iio/adc/ad4170-4.c
index 25a47fae9ebb..419bef6e16e7 100644
--- a/drivers/iio/adc/ad4170-4.c
+++ b/drivers/iio/adc/ad4170-4.c
@@ -882,10 +882,10 @@ static int ad4170_set_filter_type(struct iio_dev *indio_dev,
* filter_fs value affects the ODR (sampling_frequency), changing the
* filter may lead to a change in the sampling frequency.
*/
- scoped_guard(mutex, &st->lock) {
- if (!iio_device_claim_direct(indio_dev))
- return -EBUSY;
+ if (!iio_device_claim_direct(indio_dev))
+ return -EBUSY;

+ scoped_guard(mutex, &st->lock) {
if (val == AD4170_SINC5_AVG || val == AD4170_SINC3)
setup->filter_fs = clamp(setup->filter_fs,
AD4170_SINC3_MIN_FS,
@@ -900,8 +900,8 @@ static int ad4170_set_filter_type(struct iio_dev *indio_dev,
filter_type_conf);

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

return ret;
}
@@ -1290,15 +1290,18 @@ static int ad4170_read_raw(struct iio_dev *indio_dev,
unsigned int pga, fs_idx;
int ret;

- 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;
+ }
+
+ guard(mutex)(&st->lock);
+ switch (info) {
case IIO_CHAN_INFO_SCALE:
pga = FIELD_GET(AD4170_AFE_PGA_GAIN_MSK, setup->afe);
switch (chan->type) {

--
2.43.0