Re: [PATCH v5] iio: adc: ad7280a: replace mutex_lock() with guard(mutex)
From: Andy Shevchenko
Date: Wed Apr 22 2026 - 03:51:28 EST
On Tue, Apr 21, 2026 at 04:08:55PM -0300, Matheus Giarola wrote:
> Use guard(mutex) instead of mutex_lock()/mutex_unlock(),
> ensuring the mutex is released automatically when leaving
> the function scope. The change improves error handling and
> avoids issues such as missing unlocks. It also simplifies the
> code by removing 'err_unlock' label and several mutex_unlock()
> calls.
>
> As suggested, in ad7280_read_raw(), wrap the IIO_CHAN_INFO_RAW
> case in braces, providing a scope for the implicit variable
> declared by guard(mutex).
>
> Also, limit guard(mutex) scope in ad7280_show_balance_timer()
> to keep it locked just when necessary.
>
> In ad7280a_write_thresh(), replace break with return on error.
...
> static ssize_t ad7280_show_balance_timer(struct iio_dev *indio_dev,
> unsigned int msecs;
> int ret;
>
> - mutex_lock(&st->lock);
> - ret = ad7280_read_reg(st, chan->address >> 8,
> + scoped_guard(mutex, &st->lock) {
> + ret = ad7280_read_reg(st, chan->address >> 8,
> (chan->address & 0xFF) + AD7280A_CB1_TIMER_REG);
> - mutex_unlock(&st->lock);
> + }
Not sure if we need {} here (as it's one statement body), but the indentation
now got broken of the second line there.
What about
u8 devaddr = chan->address >> 8;
u8 addr = chan->address;
// ...or use existing names in the driver for the same things
scoped_guard(mutex, &st->lock)
ret = ad7280_read_reg(st, devaddr, addr + AD7280A_CB1_TIMER_REG);
?
--
With Best Regards,
Andy Shevchenko