[PATCH v3 next 6/6] iio: sca3000: use guard(mutex) to simplify return paths

Harshit Mogalapalli posted 6 patches 6 days, 13 hours ago
There is a newer version of this series
[PATCH v3 next 6/6] iio: sca3000: use guard(mutex) to simplify return paths
Posted by Harshit Mogalapalli 6 days, 13 hours ago
Switch sca3000_stop_all_interrupts() to use guard(mutex) to simplify the
error paths without needing a goto.

Suggested-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
v2->v3: Had to rewrite this patch as we have changes in patch3, Also
mention using guard() based auto cleanup simplifies error path( Thanks
to David for the suggestion ) -- didn't include RB of David from v2 as
the code is slightly different now.
---
 drivers/iio/accel/sca3000.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
index 419c30453805..307e51279cb6 100644
--- a/drivers/iio/accel/sca3000.c
+++ b/drivers/iio/accel/sca3000.c
@@ -7,6 +7,7 @@
  * See industrialio/accels/sca3000.h for comments.
  */
 
+#include <linux/cleanup.h>
 #include <linux/interrupt.h>
 #include <linux/fs.h>
 #include <linux/device.h>
@@ -1443,17 +1444,15 @@ static void sca3000_stop_all_interrupts(void *data)
 	struct sca3000_state *st = iio_priv(indio_dev);
 	int ret;
 
-	mutex_lock(&st->lock);
+	guard(mutex)(&st->lock);
+
 	ret = sca3000_read_data_short(st, SCA3000_REG_INT_MASK_ADDR, 1);
-	if (ret)
-		goto out_unlock;
-	sca3000_write_reg(st, SCA3000_REG_INT_MASK_ADDR,
-			  (st->rx[0] &
-			   ~(SCA3000_REG_INT_MASK_RING_THREE_QUARTER |
-			     SCA3000_REG_INT_MASK_RING_HALF |
-			     SCA3000_REG_INT_MASK_ALL_INTS)));
-out_unlock:
-	mutex_unlock(&st->lock);
+	if (!ret)
+		sca3000_write_reg(st, SCA3000_REG_INT_MASK_ADDR,
+				  (st->rx[0] &
+				   ~(SCA3000_REG_INT_MASK_RING_THREE_QUARTER |
+				     SCA3000_REG_INT_MASK_RING_HALF |
+				     SCA3000_REG_INT_MASK_ALL_INTS)));
 }
 
 static int sca3000_probe(struct spi_device *spi)
-- 
2.47.3
Re: [PATCH v3 next 6/6] iio: sca3000: use guard(mutex) to simplify return paths
Posted by Andy Shevchenko 6 days, 10 hours ago
On Tue, Feb 03, 2026 at 04:20:50AM -0800, Harshit Mogalapalli wrote:
> Switch sca3000_stop_all_interrupts() to use guard(mutex) to simplify the
> error paths without needing a goto.

...

> -	if (ret)

> +	if (!ret)

Leave the standard pattern to check for errors first.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v3 next 6/6] iio: sca3000: use guard(mutex) to simplify return paths
Posted by Harshit Mogalapalli 6 days, 9 hours ago
Hi Andy,

On 03/02/26 20:53, Andy Shevchenko wrote:
> On Tue, Feb 03, 2026 at 04:20:50AM -0800, Harshit Mogalapalli wrote:
>> Switch sca3000_stop_all_interrupts() to use guard(mutex) to simplify the
>> error paths without needing a goto.
> 
> ...
> 
>> -	if (ret)
> 
>> +	if (!ret)
> 
> Leave the standard pattern to check for errors first.
> 

Thanks, I will do that, I agree it would look simpler that way.

Regards,
Harshit