From: Joshua Crofts <crofts@kernel.org>
To: Vaibhav Chahar <vaibhav.chahar26@gmail.com>,
marius.cristea@microchip.com, jic23@kernel.org
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex
Date: Mon, 23 Nov 2026 16:22:28 +0100 [thread overview]
Message-ID: <9e1b6e08-4cd9-4c0e-a009-16330843ce3c@kernel.org> (raw)
In-Reply-To: <20261123070802.14487-1-vaibhav.chahar26@gmail.com>
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@gmail.com>
> ---
> 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
next prev parent reply other threads:[~2026-11-23 15:22 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 7:08 [PATCH] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex Vaibhav Chahar
2026-11-23 15:22 ` Joshua Crofts [this message]
2026-11-23 17:28 ` [PATCH v2 0/2] iio: adc: Use cleanup.h guards for mutex locking Vaibhav Chahar
2026-11-23 17:28 ` [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex Vaibhav Chahar
2026-11-23 19:05 ` Andy Shevchenko
2026-11-23 17:28 ` [PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Vaibhav Chahar
2026-11-23 19:10 ` Andy Shevchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9e1b6e08-4cd9-4c0e-a009-16330843ce3c@kernel.org \
--to=crofts@kernel.org \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marius.cristea@microchip.com \
--cc=nuno.sa@analog.com \
--cc=vaibhav.chahar26@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox