Re: [PATCH] iio: adc: ad7280a: replace mutex_lock() with guard(mutex)
From: Jonathan Cameron
Date: Wed Mar 25 2026 - 16:39:34 EST
On Wed, 25 Mar 2026 16:21:04 -0300
Matheus Giarola <matheustpgiarola@xxxxxxxxx> wrote:
> On Sat, Mar 21, 2026 at 12:55 PM Jonathan Cameron <jic23@xxxxxxxxxx> wrote:
> >
> > On Thu, 19 Mar 2026 15:46:15 -0300
> > Matheus Giarola <matheustpgiarola@xxxxxxxxx> 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.
> > >
> > > Signed-off-by: Matheus Giarola <matheusgiarola@xxxxxx>
> > Hi Matheus.
> >
> >
> > > @@ -885,13 +875,12 @@ static int ad7280_read_raw(struct iio_dev *indio_dev,
> > >
> > > switch (m) {
> > > case IIO_CHAN_INFO_RAW:
> > > - mutex_lock(&st->lock);
> > > + guard(mutex)(&st->lock);
> >
> > Scope missing...
> >
> > Thanks,
> >
> > Jonathan
>
> Hi Jonathan,
>
> Thank you for the review and pointing out the missing scope, I'll fix
> it by using
> scoped_guard() to ensure the lock is released at the right time.
nope. Whilst that would be correct it's not the nicest solution.
Instead do what you'd do if there was a variable declared in a case
block..
case IIO_CHAN_INFO_RAW: {
guard(...)(..);
...
}
Under the hood guard is declaring a local variable.
>
> I'm a bit busy with university work this week, so at most by next week I'll send
> a v2 of this patch.
>
> Best regards,
>
> Matheus
> >
> > > if (chan->address == AD7280A_ALL_CELLS)
> > > ret = ad7280_read_all_channels(st, st->scan_cnt, NULL);
> > > else
> > > ret = ad7280_read_channel(st, chan->address >> 8,
> > > chan->address & 0xFF);
> > > - mutex_unlock(&st->lock);
> > >
> > > if (ret < 0)
> > > return ret;
> >