Re: [PATCH] staging: iio: frequency: ad9832: use guard(mutex) for locking
From: Jonathan Cameron
Date: Mon Apr 20 2026 - 09:06:30 EST
On Sat, 11 Apr 2026 12:55:04 +0530
Advaith Neelacantan <advaith.neelacantan@xxxxxxxxx> wrote:
> Replace mutex lock/unlock pairs with
>
> guard(mutex) for automatic scope-based locking.
>
> Simplifies code by removing need to manually unlock.
>
> Signed-off-by: Advaith Neelacantan <advaith.neelacantan@xxxxxxxxx>
What everyone else has stated indeed applies, though I'm more flexible
if you are doing this part of driver cleanup that is more substantial
and touches nearby code.
However, it's buggy. Read the guidance in cleanup.h and make sure
you understand why. Note that LLVM will warn about the problem,
GCC currently does not.
> ---
> drivers/staging/iio/frequency/ad9832.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
> index b87ea1781..73c47a049 100644
> --- a/drivers/staging/iio/frequency/ad9832.c
> +++ b/drivers/staging/iio/frequency/ad9832.c
> @@ -181,7 +181,7 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
> if (ret)
> goto error_ret;
>
> - mutex_lock(&st->lock);
> + guard(mutex)(&st->lock);
> switch ((u32)this_attr->address) {
> case AD9832_FREQ0HM:
> case AD9832_FREQ1HM:
> @@ -239,7 +239,6 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
> default:
> ret = -ENODEV;
> }
> - mutex_unlock(&st->lock);
>
> error_ret:
> return ret ? ret : len;