mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] iio: convert to guard(mutex)
@ 2026-03-06 20:26 Rajveer Chaudhari
  2026-03-06 20:37 ` David Lechner
  2026-03-06 21:12 ` [PATCH] iio: " Andy Shevchenko
  0 siblings, 2 replies; 21+ messages in thread
From: Rajveer Chaudhari @ 2026-03-06 20:26 UTC (permalink / raw)
  To: lucas.p.stankus, lars, Michael.Hennerich, jic23, dlechner,
	nuno.sa, andy, puranjay
  Cc: linux-iio, linux-kernel, Rajveer Chaudhari

Replace manual mutex_lock/mutex_unlock pairs with guard(mutex)
in several IIO drivers. This ensures the mutex is released on
every return path, preventing missed unlocks on error paths.

Signed-off-by: Rajveer Chaudhari <rajveer.chaudhari.linux@gmail.com>
---
 drivers/iio/accel/adxl313_core.c      |  7 +++----
 drivers/iio/accel/adxl355_core.c      | 27 ++++++++-------------------
 drivers/iio/accel/adxl372.c           |  8 +++-----
 drivers/iio/industrialio-sw-device.c  |  4 ++--
 drivers/iio/industrialio-sw-trigger.c |  4 ++--
 5 files changed, 18 insertions(+), 32 deletions(-)

diff --git a/drivers/iio/accel/adxl313_core.c b/drivers/iio/accel/adxl313_core.c
index 9f5d4d2cb325..94c6023af487 100644
--- a/drivers/iio/accel/adxl313_core.c
+++ b/drivers/iio/accel/adxl313_core.c
@@ -8,6 +8,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/overflow.h>
@@ -356,18 +357,16 @@ static int adxl313_read_axis(struct adxl313_data *data,
 {
 	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	ret = regmap_bulk_read(data->regmap,
 			       ADXL313_REG_DATA_AXIS(chan->address),
 			       &data->transf_buf, sizeof(data->transf_buf));
 	if (ret)
-		goto unlock_ret;
+		return ret;
 
 	ret = le16_to_cpu(data->transf_buf);
 
-unlock_ret:
-	mutex_unlock(&data->lock);
 	return ret;
 }
 
diff --git a/drivers/iio/accel/adxl355_core.c b/drivers/iio/accel/adxl355_core.c
index 1c1d64d5cbcb..ef05178bff00 100644
--- a/drivers/iio/accel/adxl355_core.c
+++ b/drivers/iio/accel/adxl355_core.c
@@ -9,6 +9,7 @@
 
 #include <linux/bits.h>
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/iio/buffer.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/trigger.h>
@@ -263,12 +264,11 @@ static int adxl355_data_rdy_trigger_set_state(struct iio_trigger *trig,
 	struct adxl355_data *data = iio_priv(indio_dev);
 	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	ret = regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG,
 				 ADXL355_POWER_CTL_DRDY_MSK,
 				 FIELD_PREP(ADXL355_POWER_CTL_DRDY_MSK,
 					    state ? 0 : 1));
-	mutex_unlock(&data->lock);
 
 	return ret;
 }
@@ -409,16 +409,15 @@ static int adxl355_set_odr(struct adxl355_data *data,
 {
 	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	if (data->odr == odr) {
-		mutex_unlock(&data->lock);
 		return 0;
 	}
 
 	ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
 	if (ret)
-		goto err_unlock;
+		return ret;
 
 	ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
 				 ADXL355_FILTER_ODR_MSK,
@@ -433,13 +432,10 @@ static int adxl355_set_odr(struct adxl355_data *data,
 	if (ret)
 		goto err_set_opmode;
 
-	mutex_unlock(&data->lock);
 	return 0;
 
 err_set_opmode:
 	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
 	return ret;
 }
 
@@ -448,16 +444,15 @@ static int adxl355_set_hpf_3db(struct adxl355_data *data,
 {
 	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	if (data->hpf_3db == hpf) {
-		mutex_unlock(&data->lock);
 		return 0;
 	}
 
 	ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
 	if (ret)
-		goto err_unlock;
+		return ret;
 
 	ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
 				 ADXL355_FILTER_HPF_MSK,
@@ -471,13 +466,10 @@ static int adxl355_set_hpf_3db(struct adxl355_data *data,
 	if (ret)
 		goto err_set_opmode;
 
-	mutex_unlock(&data->lock);
 	return 0;
 
 err_set_opmode:
 	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
 	return ret;
 }
 
@@ -486,11 +478,11 @@ static int adxl355_set_calibbias(struct adxl355_data *data,
 {
 	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
 	ret = adxl355_set_op_mode(data, ADXL355_STANDBY);
 	if (ret)
-		goto err_unlock;
+		return ret;
 
 	put_unaligned_be16(calibbias, data->transf_buf);
 	ret = regmap_bulk_write(data->regmap,
@@ -505,13 +497,10 @@ static int adxl355_set_calibbias(struct adxl355_data *data,
 	if (ret)
 		goto err_set_opmode;
 
-	mutex_unlock(&data->lock);
 	return 0;
 
 err_set_opmode:
 	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
 	return ret;
 }
 
diff --git a/drivers/iio/accel/adxl372.c b/drivers/iio/accel/adxl372.c
index 28a8793a53b6..24a79f61900d 100644
--- a/drivers/iio/accel/adxl372.c
+++ b/drivers/iio/accel/adxl372.c
@@ -7,6 +7,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/module.h>
@@ -336,17 +337,14 @@ static ssize_t adxl372_write_threshold_value(struct iio_dev *indio_dev, unsigned
 	struct adxl372_state *st = iio_priv(indio_dev);
 	int ret;
 
-	mutex_lock(&st->threshold_m);
+	guard(mutex)(&st->threshold_m);
 	ret = regmap_write(st->regmap, addr, ADXL372_THRESH_VAL_H_SEL(threshold));
 	if (ret < 0)
-		goto unlock;
+		return ret;
 
 	ret = regmap_update_bits(st->regmap, addr + 1, GENMASK(7, 5),
 				 ADXL372_THRESH_VAL_L_SEL(threshold) << 5);
 
-unlock:
-	mutex_unlock(&st->threshold_m);
-
 	return ret;
 }
 
diff --git a/drivers/iio/industrialio-sw-device.c b/drivers/iio/industrialio-sw-device.c
index 3582524de0b8..a2a3e3a67d79 100644
--- a/drivers/iio/industrialio-sw-device.c
+++ b/drivers/iio/industrialio-sw-device.c
@@ -10,6 +10,7 @@
 #include <linux/kmod.h>
 #include <linux/list.h>
 #include <linux/slab.h>
+#include <linux/cleanup.h>
 
 #include <linux/iio/sw_device.h>
 #include <linux/iio/configfs.h>
@@ -84,11 +85,10 @@ struct iio_sw_device_type *iio_get_sw_device_type(const char *name)
 {
 	struct iio_sw_device_type *dt;
 
-	mutex_lock(&iio_device_types_lock);
+	guard(mutex)(&iio_device_types_lock);
 	dt = __iio_find_sw_device_type(name, strlen(name));
 	if (dt && !try_module_get(dt->owner))
 		dt = NULL;
-	mutex_unlock(&iio_device_types_lock);
 
 	return dt;
 }
diff --git a/drivers/iio/industrialio-sw-trigger.c b/drivers/iio/industrialio-sw-trigger.c
index 334b6b10a784..4af601ae286c 100644
--- a/drivers/iio/industrialio-sw-trigger.c
+++ b/drivers/iio/industrialio-sw-trigger.c
@@ -10,6 +10,7 @@
 #include <linux/kmod.h>
 #include <linux/list.h>
 #include <linux/slab.h>
+#include <linux/cleanup.h>
 
 #include <linux/iio/sw_trigger.h>
 #include <linux/iio/configfs.h>
@@ -88,11 +89,10 @@ struct iio_sw_trigger_type *iio_get_sw_trigger_type(const char *name)
 {
 	struct iio_sw_trigger_type *t;
 
-	mutex_lock(&iio_trigger_types_lock);
+	guard(mutex)(&iio_trigger_types_lock);
 	t = __iio_find_sw_trigger_type(name, strlen(name));
 	if (t && !try_module_get(t->owner))
 		t = NULL;
-	mutex_unlock(&iio_trigger_types_lock);
 
 	return t;
 }
-- 
2.53.0


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

end of thread, other threads:[~2026-03-07 17:09 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-06 20:26 [PATCH] iio: convert to guard(mutex) Rajveer Chaudhari
2026-03-06 20:37 ` David Lechner
2026-03-06 21:11   ` [PATCH v2 0/3] iio: accel: " Rajveer Chaudhari
2026-03-06 21:11     ` [PATCH v2 1/3] iio: accel: adxl313: " Rajveer Chaudhari
2026-03-06 22:10       ` David Lechner
2026-03-06 21:11     ` [PATCH v2 2/3] iio: accel: adxl355: " Rajveer Chaudhari
2026-03-06 22:12       ` David Lechner
2026-03-06 21:11     ` [PATCH v2 3/3] iio: accel: adxl372: " Rajveer Chaudhari
2026-03-07 10:17     ` [PATCH v3 0/3] iio: accel: " Rajveer Chaudhari
2026-03-07 10:17       ` [PATCH v3 1/3] iio: accel: adxl313: " Rajveer Chaudhari
2026-03-07 10:31         ` Jonathan Cameron
2026-03-07 10:17       ` [PATCH v3 2/3] iio: accel: adxl355: " Rajveer Chaudhari
2026-03-07 10:39         ` Jonathan Cameron
2026-03-07 10:43           ` Jonathan Cameron
2026-03-07 10:17       ` [PATCH v3 3/3] iio: accel: adxl372: " Rajveer Chaudhari
2026-03-07 10:41         ` Jonathan Cameron
2026-03-07 10:26       ` [PATCH v3 0/3] iio: accel: " Jonathan Cameron
2026-03-07 10:33         ` Rajveer Chaudhari
2026-03-07 17:08           ` Jonathan Cameron
2026-03-06 21:12 ` [PATCH] iio: " Andy Shevchenko
2026-03-06 21:23   ` Rajveer Chaudhari

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

all inboxes | Powered by JetHome®