Skip to contentSkip to conversation

iio: adc: ti-ads1015: Use scoped_guard() in trigger handler

PATCH In review Tabrez Ahmed 2 participants 2 messages Feb 21 – Feb 22 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: adc: ti-ads1015: Use scoped_guard() in trigger handler

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.

---
Compile-tested only.

 drivers/iio/adc/ti-ads1015.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/iio/adc/ti-ads1015.c +8 -10
index f2a93c63ca14..582d30c07874 100644
@@ -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));
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 7 lines · On Sat, 21 Feb 2026 19:55:00 +0530 Tabrez Ahmed <[email protected]> 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.

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.

Quoted 21 lines

---
Compile-tested only.

 drivers/iio/adc/ti-ads1015.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/iio/adc/ti-ads1015.c +1 -7
index f2a93c63ca14..582d30c07874 100644
@@ -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) {

drivers/iio/adc/ti-ads1015.c static irqreturn_t ads1015_trigger_handler(int irq, void *p) · line 463

Quoted 10 lines
drivers/iio/adc/ti-ads1015.c +7 -1
+		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

Quoted 5 lines
drivers/iio/adc/ti-ads1015.c +0 -2
-	mutex_unlock(&data->lock);
-
 	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan),
 				    iio_get_time_ns(indio_dev));