Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: adc: mcp320x: Use guard(mutex) for lock handling
@ 2026-04-22 21:11 Eduardo Augusto
  2026-04-23 11:56 ` Andy Shevchenko
  2026-04-24 11:48 ` Jonathan Cameron
  0 siblings, 2 replies; 3+ messages in thread
From: Eduardo Augusto @ 2026-04-22 21:11 UTC (permalink / raw)
  To: jic23, dlechner, nuno.sa, andy
  Cc: Eduardo Augusto, Gustavo Pagnotta Faria, Christian Barry,
	linux-iio

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;
 }
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: adc: mcp320x: Use guard(mutex) for lock handling
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2026-04-23 11:56 UTC (permalink / raw)
  To: Eduardo Augusto
  Cc: jic23, dlechner, nuno.sa, andy, Gustavo Pagnotta Faria,
	Christian Barry, linux-iio

On Wed, Apr 22, 2026 at 06:11:05PM -0300, Eduardo Augusto 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.

...

>  #include <linux/mod_devicetable.h>
>  #include <linux/iio/iio.h>
>  #include <linux/regulator/consumer.h>
> +#include <linux/cleanup.h>

Please, start reviewing others' contribution first before making your own.
Also learn from other reviews.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: adc: mcp320x: Use guard(mutex) for lock handling
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2026-04-24 11:48 UTC (permalink / raw)
  To: Eduardo Augusto
  Cc: dlechner, nuno.sa, andy, Gustavo Pagnotta Faria, Christian Barry,
	linux-iio

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

>  }
>  


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-24 11:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox