Re: [PATCH] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex
From: Joshua Crofts
Date: Mon Nov 23 2026 - 10:22:35 EST
On 23-Nov-26 8:08, Vaibhav Chahar wrote:
> Adopting cleanup.h helper: Replaced mutex_lock() and mutex_unlock() calls with guard() and scoped_guard(). Ensures locks are automatically released, simplifying the code.
>
> scoped_guard(){} was handled in mcp3564_read_raw switch statement (case: IIO_CHAN_INFO_SCALE)- giving it proper
> scope.
I'd reword the commit message; something along the lines of "Use guard(mutex)
instead of manual mutex_lock()/mutex_unlock() calls to modernize driver and improve
mutex handling."
> No goto statements were present inside older mutex blocks - so it was a
> 1 to 1 substitution.
>
> Tested:
> - compiled with `make W=1` with zero warnings.
> - Verified using Sparse (`make C=1`)
> - Verified styling via 'scripts/checkpatch.pl --strict' (0 errors, 0 warnings)
Move the above paragraphs under the ---, you should mention testing only if
done on the affected hardware (one would expect a patch to be at least compile
tested before being sent to the mailing list).
> Signed-off-by: Vaibhav Chahar <vaibhav.chahar26@xxxxxxxxx>
> ---
> Q: If i have similar cleanups for other drivers/files in this directory.
> Should I combine them into a single patch or submit a patch series?
I'd do a series. One patch can be harder to review.
> drivers/iio/adc/mcp3564.c | 38 ++++++++++++++++----------------------
> 1 file changed, 16 insertions(+), 22 deletions(-)
>
> diff --git a/drivers/iio/adc/mcp3564.c b/drivers/iio/adc/mcp3564.c
> index 259ac4ebd..252c3886e 100644
> --- a/drivers/iio/adc/mcp3564.c
> +++ b/drivers/iio/adc/mcp3564.c
> @@ -17,6 +17,7 @@
> */
>
> #include <linux/bitfield.h>
> +#include <linux/cleanup.h>
> #include <linux/iopoll.h>
> #include <linux/regulator/consumer.h>
> #include <linux/spi/spi.h>
> @@ -498,7 +499,7 @@ static int mcp3564_set_current_boost_mode(struct iio_dev *indio_dev,
>
> dev_dbg(&indio_dev->dev, "%s: %d\n", __func__, mode);
>
> - mutex_lock(&adc->lock);
> + guard(mutex)(&adc->lock);
Blank lines around guard() please.
...
> @@ -840,10 +837,10 @@ static int mcp3564_read_raw(struct iio_dev *indio_dev,
> return -EINVAL;
> return IIO_VAL_INT;
> case IIO_CHAN_INFO_SCALE:
> - mutex_lock(&adc->lock);
> - *val = adc->scale_tbls[adc->hwgain][0];
> - *val2 = adc->scale_tbls[adc->hwgain][1];
> - mutex_unlock(&adc->lock);
> + scoped_guard(mutex, &adc->lock) {
> + *val = adc->scale_tbls[adc->hwgain][0];
> + *val2 = adc->scale_tbls[adc->hwgain][1];
> + }
> return IIO_VAL_INT_PLUS_NANO;
> case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> *val = mcp3564_oversampling_avail[adc->oversampling];
> @@ -903,7 +900,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
> if (burnout == adc->burnout_mode)
> return ret;
>
> - mutex_lock(&adc->lock);
> + guard(mutex)(&adc->lock);
If you're adding guard() in a switch case, make sure to wrap the case
with brackets, i.e.
case IIO_CHAN_INFO_RAW: {
foo();
}
...
The cleanup.h magic works thanks to local variables, which need some
sort of scope.
--
Kind regards,
Joshua Crofts