Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Eduardo Augusto <eduardoaugustoabc@ime.usp.br>
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	Gustavo Pagnotta Faria <gustavo.pagnotta@ime.usp.br>,
	Christian Barry <christian.barry@ime.usp.br>,
	linux-iio@vger.kernel.org
Subject: Re: [PATCH] iio: adc: mcp320x: Use guard(mutex) for lock handling
Date: Fri, 24 Apr 2026 12:48:23 +0100	[thread overview]
Message-ID: <20260424124823.443b3c23@jic23-huawei> (raw)
In-Reply-To: <20260422211105.48158-1-eduardoaugustoabc@ime.usp.br>

On Wed, 22 Apr 2026 18:11:05 -0300
Eduardo Augusto <eduardoaugustoabc@ime.usp.br> wrote:

> Replace the explicit mutex_lock() and mutex_unlock() calls in
> mcp320x_read_raw() with guard(mutex)().
> 
> This simplifies the error handling paths by removing the need for
> the explicit unlock label while keeping the locking semantics
> unchanged.
> 
> Signed-off-by: Eduardo Augusto <eduardoaugustoabc@ime.usp.br>
> Co-developed-by: Gustavo Pagnotta Faria <gustavo.pagnotta@ime.usp.br>
> Signed-off-by: Gustavo Pagnotta Faria <gustavo.pagnotta@ime.usp.br>
> Co-developed-by: Christian Barry <christian.barry@ime.usp.br>
> Signed-off-by: Christian Barry <christian.barry@ime.usp.br>
> ---
>  drivers/iio/adc/mcp320x.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/iio/adc/mcp320x.c b/drivers/iio/adc/mcp320x.c
> index 57cff3772..799756f4c 100644
> --- a/drivers/iio/adc/mcp320x.c
> +++ b/drivers/iio/adc/mcp320x.c
> @@ -44,6 +44,7 @@
>  #include <linux/mod_devicetable.h>
>  #include <linux/iio/iio.h>
>  #include <linux/regulator/consumer.h>
> +#include <linux/cleanup.h>
>  
>  enum {
>  	mcp3001,
> @@ -198,8 +199,7 @@ static int mcp320x_read_raw(struct iio_dev *indio_dev,
>  	struct mcp320x *adc = iio_priv(indio_dev);
>  	int ret = -EINVAL;
>  	int device_index = 0;
> -
> -	mutex_lock(&adc->lock);
> +	guard(mutex)(&adc->lock);
>  
>  	device_index = spi_get_device_id(adc->spi)->driver_data;
>  
> @@ -208,7 +208,7 @@ static int mcp320x_read_raw(struct iio_dev *indio_dev,
>  		ret = mcp320x_adc_conversion(adc, channel->address,
>  			channel->differential, device_index, val);
>  		if (ret < 0)
> -			goto out;
> +			return ret;
>  
>  		ret = IIO_VAL_INT;
>  		break;
> @@ -216,7 +216,7 @@ static int mcp320x_read_raw(struct iio_dev *indio_dev,
>  	case IIO_CHAN_INFO_SCALE:
>  		ret = regulator_get_voltage(adc->reg);
>  		if (ret < 0)
> -			goto out;
> +			return ret;
>  
>  		/* convert regulator output voltage to mV */
>  		*val = ret / 1000;
> @@ -225,9 +225,6 @@ static int mcp320x_read_raw(struct iio_dev *indio_dev,
>  		break;
>  	}
>  
> -out:
> -	mutex_unlock(&adc->lock);
> -
>  	return ret;
Can do early returns now in all branches.  As Andy said, check for
reviews of similar code that has been posted recently.

If anyone who is doing this sort of work feels like taking the
combined feedback from those reviewing and writing a doc with examples
etc that would be great.  Maybe a blog or similar or we could see
if we could find a home for it in tree.

Too many patches to review for Andy or I to have the time, though
maybe when things calm down a bit...

Jonathan

>  }
>  


      parent reply	other threads:[~2026-04-24 11:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-22 21:11 [PATCH] iio: adc: mcp320x: Use guard(mutex) for lock handling Eduardo Augusto
2026-04-23 11:56 ` Andy Shevchenko
2026-04-24 11:48 ` Jonathan Cameron [this message]

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=20260424124823.443b3c23@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=christian.barry@ime.usp.br \
    --cc=dlechner@baylibre.com \
    --cc=eduardoaugustoabc@ime.usp.br \
    --cc=gustavo.pagnotta@ime.usp.br \
    --cc=linux-iio@vger.kernel.org \
    --cc=nuno.sa@analog.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