public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: adc: ti-ads1015: Use scoped_guard() in trigger handler
@ 2026-02-21 14:25 Tabrez Ahmed
  2026-02-22 15:58 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Tabrez Ahmed @ 2026-02-21 14:25 UTC (permalink / raw)
  To: jic23; +Cc: dlechner, nuno.sa, andy, linux-iio, linux-kernel, Tabrez Ahmed

Replace the manual mutex_lock() and mutex_unlock() calls with the
scoped_guard() macro in ads1015_trigger_handler(). This simplifies
the locking logic by ensuring the mutex is always properly released
out of scope, while maintaining the trigger notification on the
error path.

Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
---
Compile-tested only.
 drivers/iio/adc/ti-ads1015.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/adc/ti-ads1015.c b/drivers/iio/adc/ti-ads1015.c
index f2a93c63ca14..582d30c07874 100644
--- a/drivers/iio/adc/ti-ads1015.c
+++ b/drivers/iio/adc/ti-ads1015.c
@@ -451,18 +451,16 @@ static irqreturn_t ads1015_trigger_handler(int irq, void *p)
 	} scan = { };
 	int chan, ret, res;
 
-	mutex_lock(&data->lock);
-	chan = find_first_bit(indio_dev->active_scan_mask,
-			      iio_get_masklength(indio_dev));
-	ret = ads1015_get_adc_result(data, chan, &res);
-	if (ret < 0) {
-		mutex_unlock(&data->lock);
-		goto err;
+	scoped_guard(mutex, &data->lock) {
+		chan = find_first_bit(indio_dev->active_scan_mask,
+				      iio_get_masklength(indio_dev));
+		ret = ads1015_get_adc_result(data, chan, &res);
+		if (ret < 0)
+			goto err;
+
+		scan.chan = res;
 	}
 
-	scan.chan = res;
-	mutex_unlock(&data->lock);
-
 	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan),
 				    iio_get_time_ns(indio_dev));
 
-- 
2.43.0


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

* Re: [PATCH] iio: adc: ti-ads1015: Use scoped_guard() in trigger handler
  2026-02-21 14:25 [PATCH] iio: adc: ti-ads1015: Use scoped_guard() in trigger handler Tabrez Ahmed
@ 2026-02-22 15:58 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2026-02-22 15:58 UTC (permalink / raw)
  To: Tabrez Ahmed; +Cc: dlechner, nuno.sa, andy, linux-iio, linux-kernel

On Sat, 21 Feb 2026 19:55:00 +0530
Tabrez Ahmed <tabreztalks@gmail.com> wrote:

> Replace the manual mutex_lock() and mutex_unlock() calls with the
> scoped_guard() macro in ads1015_trigger_handler(). This simplifies
> the locking logic by ensuring the mutex is always properly released
> out of scope, while maintaining the trigger notification on the
> error path.
> 
> Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
Hi Tabrez,

Take a look at the comments in cleanup.h 

Basic rule is don't combine guard() or __free() with
gotos.  The actual bugs that avoids aren't present in your patch
but to keep things simple for review, that rule stands anyway.

See below for alternative.

> ---
> Compile-tested only.
>  drivers/iio/adc/ti-ads1015.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/iio/adc/ti-ads1015.c b/drivers/iio/adc/ti-ads1015.c
> index f2a93c63ca14..582d30c07874 100644
> --- a/drivers/iio/adc/ti-ads1015.c
> +++ b/drivers/iio/adc/ti-ads1015.c
> @@ -451,18 +451,16 @@ static irqreturn_t ads1015_trigger_handler(int irq, void *p)
>  	} scan = { };
>  	int chan, ret, res;
>  
> -	mutex_lock(&data->lock);
> -	chan = find_first_bit(indio_dev->active_scan_mask,
> -			      iio_get_masklength(indio_dev));
> -	ret = ads1015_get_adc_result(data, chan, &res);
> -	if (ret < 0) {
> -		mutex_unlock(&data->lock);
> -		goto err;
> +	scoped_guard(mutex, &data->lock) {
> +		chan = find_first_bit(indio_dev->active_scan_mask,
> +				      iio_get_masklength(indio_dev));
> +		ret = ads1015_get_adc_result(data, chan, &res);
> +		if (ret < 0)
> +			goto err;
> +
> +		scan.chan = res;
>  	}
>  
> -	scan.chan = res;

There was never any reason to hold the lock over this assignment as
scan.chan and res are both local variables.

Hence, this can become something like

	mutex_lock(&data->lock);
	chan = find_first_bit(indio_dev->active_scan_mask,
			      iio_get_masklength(indio_dev));
	ret = ads1015_get_adc_result(data, chan, &res);
	mutex_unlock(data->lock);
	if (ret < 0)
		goto err;

	scan_chan = res;
...

Which gets you most of the advantages of using scoped_guard()
(no need to unlock in multiple paths) without running into problems
due to the goto.

Jonathan

> -	mutex_unlock(&data->lock);
> -
>  	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan),
>  				    iio_get_time_ns(indio_dev));
>  


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

end of thread, other threads:[~2026-02-22 15:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-21 14:25 [PATCH] iio: adc: ti-ads1015: Use scoped_guard() in trigger handler Tabrez Ahmed
2026-02-22 15:58 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox