Skip to contentSkip to conversation

iio: convert to guard(mutex)

PATCH Superseded Rajveer Chaudhari 4 participants 21 messages Mar 6 – Mar 7

Subsystems (5)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: convert to guard(mutex)

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.

---
 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(-)
drivers/iio/accel/adxl313_core.c +3 -4
index 9f5d4d2cb325..94c6023af487 100644
@@ -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;
 }
 
drivers/iio/accel/adxl355_core.c +8 -19
index 1c1d64d5cbcb..ef05178bff00 100644
@@ -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;
 }
 
drivers/iio/accel/adxl372.c +3 -5
index 28a8793a53b6..24a79f61900d 100644
@@ -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;
 }
 
drivers/iio/industrialio-sw-device.c +2 -2
index 3582524de0b8..a2a3e3a67d79 100644
@@ -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;
 }
drivers/iio/industrialio-sw-trigger.c +2 -2
index 334b6b10a784..4af601ae286c 100644
@@ -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;
 }
Signature · 1 line
2.53.0
reviewer
#1 lore raw
Quoted 3 lines · 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.

If there are any actual bugs this is fixing, they need to be split out and have a clear explanation of the bug and have a Fixes: tag.

Quoted 8 lines
---
 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 ++--

Please split these up as one patch per driver. It makes it easier to review and backport if needed.

Quoted 23 lines
 5 files changed, 18 insertions(+), 32 deletions(-)
drivers/iio/accel/adxl313_core.c +2 -1
index 9f5d4d2cb325..94c6023af487 100644
@@ -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),

drivers/iio/accel/adxl313_core.c static int adxl313_read_axis(struct adxl313_data *data, · line 370

Quoted 10 lines
drivers/iio/accel/adxl313_core.c +1 -3
 			       &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;

Watch out for places like this where we can return directly now. If we aren't doing additional improvements like that or fixing actual bugs, there isn't much point in spending the time to convert to guard().

The same applies to all of the other changes in this patch.

Quoted 2 lines
drivers/iio/accel/adxl313_core.c +0 -0
 }
 
#2 lore raw

v2 cover iio: accel: convert to guard(mutex)

This series converts manual mutex_lock/mutex_unlock pairs to guard(mutex) in three ADXL accelerometer drivers. Each conversion also simplifies error handling by removing goto labels and returning directly.

v2: Split into one patch per driver as requested by David Lechner.
Ensured each conversion also simplifies code flow.

Rajveer Chaudhari (3):
iio: accel: adxl313: convert to guard(mutex)
iio: accel: adxl355: convert to guard(mutex)
iio: accel: adxl372: convert to guard(mutex)

 drivers/iio/accel/adxl313_core.c |  7 +++----
 drivers/iio/accel/adxl355_core.c | 27 ++++++++-------------------
 drivers/iio/accel/adxl372.c      |  8 +++-----
 3 files changed, 14 insertions(+), 28 deletions(-)
Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 1/3 iio: accel: adxl313: convert to guard(mutex)

---
 drivers/iio/accel/adxl313_core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/accel/adxl313_core.c +3 -4
index 9f5d4d2cb325..94c6023af487 100644
@@ -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;
 }
 
Signature · 1 line
2.53.0
reviewer
#4 lore raw

Re: [PATCH v2 1/3] iio: accel: adxl313: convert to guard(mutex)

Quoted 21 lines · Rajveer Chaudhari
---
 drivers/iio/accel/adxl313_core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/iio/accel/adxl313_core.c +1 -1
index 9f5d4d2cb325..94c6023af487 100644
@@ -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);

drivers/iio/accel/adxl313_core.c static int adxl313_read_axis(struct adxl313_data *data, · line 368

Quoted 10 lines
drivers/iio/accel/adxl313_core.c +2 -1
+	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);

This can be simplifed:

	return le16_to_cpu(data->transf_buf);
Quoted 6 lines
drivers/iio/accel/adxl313_core.c +0 -2
-unlock_ret:
-	mutex_unlock(&data->lock);
 	return ret;
 }
 
#5 lore raw

PATCH v2 2/3 iio: accel: adxl355: convert to guard(mutex)

In reply to Rajveer Chaudhari
---
 drivers/iio/accel/adxl355_core.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
drivers/iio/accel/adxl355_core.c +8 -19
index 1c1d64d5cbcb..ef05178bff00 100644
@@ -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;
 }
 
Signature · 1 line
2.53.0
reviewer
#6 lore raw

Re: [PATCH v2 2/3] iio: accel: adxl355: convert to guard(mutex)

Quoted 17 lines · Rajveer Chaudhari
---
 drivers/iio/accel/adxl355_core.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
drivers/iio/accel/adxl355_core.c +1 -0
index 1c1d64d5cbcb..ef05178bff00 100644
@@ -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>

drivers/iio/accel/adxl355_core.c static int adxl355_data_rdy_trigger_set_state(struct iio_trigger *trig, · line 271

Quoted 9 lines
drivers/iio/accel/adxl355_core.c +1 -1
@@ -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));

Same principal applies here:

return regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG,
ADXL355_POWER_CTL_DRDY_MSK, FIELD_PREP(ADXL355_POWER_CTL_DRDY_MSK,
state ? 0 : 1));

Then we can remove ret entirely.

Similar changes can be made in most other functions as well.

Quoted 4 lines
drivers/iio/accel/adxl355_core.c +0 -1
-	mutex_unlock(&data->lock);
 
 	return ret;
 }

...

Quoted 20 lines
drivers/iio/accel/adxl355_core.c +2 -3
@@ -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,

drivers/iio/accel/adxl355_core.c static int adxl355_set_hpf_3db(struct adxl355_data *data, · line 467

Quoted 2 lines
drivers/iio/accel/adxl355_core.c +0 -0
@@ -471,13 +466,10 @@ static int adxl355_set_hpf_3db(struct adxl355_data *data,
 	if (ret)
 		goto err_set_opmode;

If we aren't getting rid of all of the gotos, we are doing it wrong.

Same applies in other functions as well.

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +0 -3
-	mutex_unlock(&data->lock);
 	return 0;
 
 err_set_opmode:
 	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
 	return ret;
 }
#7 lore raw

PATCH v2 3/3 iio: accel: adxl372: convert to guard(mutex)

In reply to Rajveer Chaudhari
---
 drivers/iio/accel/adxl372.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/iio/accel/adxl372.c +3 -5
index 28a8793a53b6..24a79f61900d 100644
@@ -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;
 }
 
Signature · 1 line
2.53.0
#8 lore raw

v3 cover iio: accel: convert to guard(mutex)

In reply to Rajveer Chaudhari

This series converts manual mutex_lock/mutex_unlock pairs to guard(mutex) in three ADXL accelerometer drivers. Each conversion also simplifies error handling by removing goto labels and returning directly where possible.

v3: Remove remaining goto labels completely as requested by David Lechner.
Return directly from functions where ret variable is not needed.
v2: Split into one patch per driver as requested by David Lechner.
Ensured each conversion also simplifies code flow.

Rajveer Chaudhari (3):
iio: accel: adxl313: convert to guard(mutex)
iio: accel: adxl355: convert to guard(mutex)
iio: accel: adxl372: convert to guard(mutex)

 drivers/iio/accel/adxl313_core.c | 15 ++----
 drivers/iio/accel/adxl355_core.c | 81 ++++++++++++++------------------
 drivers/iio/accel/adxl372.c      | 16 +++----
 3 files changed, 45 insertions(+), 67 deletions(-)
Signature · 1 line
2.53.0
#9 lore raw

PATCH v3 1/3 iio: accel: adxl313: convert to guard(mutex)

Replace manual mutex_lock/mutex_unlock pair with guard(mutex) in adxl313_read_axis(). This ensures the mutex is released on all return paths and allows returning directly without a goto label.

v3: Return directly from regmap_bulk_read error path.
v2: Split into separate patch per driver.

---
 drivers/iio/accel/adxl313_core.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/iio/accel/adxl313_core.c +5 -10
index 9f5d4d2cb325..40f62c6f89b4 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/overflow.h>
@@ -354,21 +355,15 @@ static int adxl313_set_odr(struct adxl313_data *data,
 static int adxl313_read_axis(struct adxl313_data *data,
 			     struct iio_chan_spec const *chan)
 {
-	int ret;
-
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
-	ret = regmap_bulk_read(data->regmap,
+	int ret = regmap_bulk_read(data->regmap,
 			       ADXL313_REG_DATA_AXIS(chan->address),
 			       &data->transf_buf, sizeof(data->transf_buf));
 	if (ret)
-		goto unlock_ret;
-
-	ret = le16_to_cpu(data->transf_buf);
+		return ret;
 
-unlock_ret:
-	mutex_unlock(&data->lock);
-	return ret;
+	return le16_to_cpu(data->transf_buf);
 }
 
 static int adxl313_read_freq_avail(struct iio_dev *indio_dev,
Signature · 1 line
2.53.0
maintainer
#10 lore raw

Re: [PATCH v3 1/3] iio: accel: adxl313: convert to guard(mutex)

Quoted 6 lines · On Sat, 7 Mar 2026 15:47:56 +0530 Rajveer Chaudhari…

Replace manual mutex_lock/mutex_unlock pair with guard(mutex) in adxl313_read_axis(). This ensures the mutex is released on all return paths and allows returning directly without a goto label.

v3: Return directly from regmap_bulk_read error path.
v2: Split into separate patch per driver.

This change log belongs..

Quoted 3 lines

---

.. here so that it doesn't get picked up when the patch is applied. The history of how a patch was modified isn't normally something we want in the git log.

One minor comment inline.

Quoted 16 lines
 drivers/iio/accel/adxl313_core.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/iio/accel/adxl313_core.c +1 -0
index 9f5d4d2cb325..40f62c6f89b4 100644
@@ -8,6 +8,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/overflow.h>

drivers/iio/accel/adxl313_core.c static int adxl313_set_odr(struct adxl313_data *data, · line 360

Quoted 10 lines
drivers/iio/accel/adxl313_core.c +2 -4
@@ -354,21 +355,15 @@ static int adxl313_set_odr(struct adxl313_data *data,
 static int adxl313_read_axis(struct adxl313_data *data,
 			     struct iio_chan_spec const *chan)
 {
-	int ret;
-
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 
-	ret = regmap_bulk_read(data->regmap,
+	int ret = regmap_bulk_read(data->regmap,

This is not a style used in the kernel. There are exceptions where we declare variables inline, but those are mainly about __free() so where a destructor and
constructor want to be visible together. That doesn't apply here so please
leave the declaration of ret at the top.

Thanks,

Jonathan

Quoted 15 lines
drivers/iio/accel/adxl313_core.c +2 -6
 			       ADXL313_REG_DATA_AXIS(chan->address),
 			       &data->transf_buf, sizeof(data->transf_buf));
 	if (ret)
-		goto unlock_ret;
-
-	ret = le16_to_cpu(data->transf_buf);
+		return ret;
 
-unlock_ret:
-	mutex_unlock(&data->lock);
-	return ret;
+	return le16_to_cpu(data->transf_buf);
 }
 
 static int adxl313_read_freq_avail(struct iio_dev *indio_dev,
#11 lore raw

PATCH v3 2/3 iio: accel: adxl355: convert to guard(mutex)

In reply to Rajveer Chaudhari

Replace manual mutex_lock/mutex_unlock pairs with guard(mutex) in adxl355_data_rdy_trigger_set_state(), adxl355_set_odr(), adxl355_set_hpf_3db() and adxl355_set_calibbias(). Remove all goto labels and return directly on error paths.

v3: Remove all remaining gotos and return directly where possible.
v2: Split into separate patch per driver.

---
 drivers/iio/accel/adxl355_core.c | 81 ++++++++++++++------------------
 1 file changed, 34 insertions(+), 47 deletions(-)
drivers/iio/accel/adxl355_core.c +34 -47
index 1c1d64d5cbcb..af606e2ab8d4 100644
@@ -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>
@@ -261,16 +262,13 @@ static int adxl355_data_rdy_trigger_set_state(struct iio_trigger *trig,
 {
 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
 	struct adxl355_data *data = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&data->lock);
-	ret = regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG,
+	guard(mutex)(&data->lock);
+	
+	return 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;
 }
 
 static void adxl355_fill_3db_frequency_table(struct adxl355_data *data)
@@ -409,38 +407,34 @@ 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,
 				 FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr));
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
 	data->odr = odr;
 	adxl355_fill_3db_frequency_table(data);
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_set_hpf_3db(struct adxl355_data *data,
@@ -448,37 +442,33 @@ 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,
 				 FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf));
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
 	data->hpf_3db = hpf;
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_set_calibbias(struct adxl355_data *data,
@@ -486,33 +476,30 @@ 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,
 				adxl355_chans[chan].offset_reg,
 				data->transf_buf, 2);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
 	data->calibbias[chan] = calibbias;
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_read_raw(struct iio_dev *indio_dev,
Signature · 1 line
2.53.0
maintainer
#12 lore raw

Re: [PATCH v3 2/3] iio: accel: adxl355: convert to guard(mutex)

Quoted 7 lines · On Sat, 7 Mar 2026 15:47:57 +0530 Rajveer Chaudhari…

Replace manual mutex_lock/mutex_unlock pairs with guard(mutex) in adxl355_data_rdy_trigger_set_state(), adxl355_set_odr(), adxl355_set_hpf_3db() and adxl355_set_calibbias(). Remove all goto labels and return directly on error paths.

v3: Remove all remaining gotos and return directly where possible.
v2: Split into separate patch per driver.

Same issue for the change log needing to be below the ---

Quoted 11 lines
---
 drivers/iio/accel/adxl355_core.c | 81 ++++++++++++++------------------
 1 file changed, 34 insertions(+), 47 deletions(-)
drivers/iio/accel/adxl355_core.c +0 -0
index 1c1d64d5cbcb..af606e2ab8d4 100644

...

Quoted 16 lines

static void adxl355_fill_3db_frequency_table(struct adxl355_data *data)

drivers/iio/accel/adxl355_core.c +1 -2
@@ -409,38 +407,34 @@ 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)

drivers/iio/accel/adxl355_core.c static int adxl355_set_odr(struct adxl355_data *data, · line 424

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +3 -3
-		goto err_unlock;
+		return ret;
 
 	ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
 				 ADXL355_FILTER_ODR_MSK,
 				 FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr));
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);

See below.

Quoted 39 lines
drivers/iio/accel/adxl355_core.c +7 -11
+		return ret;
+	}
 
 	data->odr = odr;
 	adxl355_fill_3db_frequency_table(data);
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_set_hpf_3db(struct adxl355_data *data,
@@ -448,37 +442,33 @@ 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)

drivers/iio/accel/adxl355_core.c static int adxl355_set_hpf_3db(struct adxl355_data *data, · line 459

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +3 -3
-		goto err_unlock;
+		return ret;
 
 	ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
 				 ADXL355_FILTER_HPF_MSK,
 				 FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf));
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);

Similar to below.

Quoted 33 lines
drivers/iio/accel/adxl355_core.c +7 -10
+		return ret;
+	}
 
 	data->hpf_3db = hpf;
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_set_calibbias(struct adxl355_data *data,
@@ -486,33 +476,30 @@ 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)

drivers/iio/accel/adxl355_core.c static int adxl355_set_calibbias(struct adxl355_data *data, · line 489

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +2 -3
-		goto err_unlock;
+		return ret;
 
 	put_unaligned_be16(calibbias, data->transf_buf);
 	ret = regmap_bulk_write(data->regmap,
 				adxl355_chans[chan].offset_reg,
 				data->transf_buf, 2);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){

Whilst this does answer the feedback you got on v2 wrt to not mixing gotos (see comments on this in cleanup.h) it leads to inelegant code due to the duplication.

There are a couple of techniques to avoid this. The most appropriate one here is to use a helper function called something like do_adxl355_set_calibbias() which has all the code
done between set_op_mode and unsetting it.

Then you can unconditionally unset the op_mode before checking if there as an error. You will need to be a little clever with ret code handling though.

drivers/iio/accel/adxl355_core.c static int adxl355_set_calibbias(struct adxl355_data *data, · line 497

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +4 -2
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
 	data->calibbias[chan] = calibbias;
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){

Run checkpatch.pl over your patch. Should be a space before that {

Quoted 15 lines
drivers/iio/accel/adxl355_core.c +3 -7
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_read_raw(struct iio_dev *indio_dev,
maintainer
#13 lore raw

Re: [PATCH v3 2/3] iio: accel: adxl355: convert to guard(mutex)

Quoted 8 lines · On Sat, 7 Mar 2026 10:39:49 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 5 lines · On Sat, 7 Mar 2026 15:47:57 +0530 Rajveer Chaudhari…

Replace manual mutex_lock/mutex_unlock pairs with guard(mutex) in adxl355_data_rdy_trigger_set_state(), adxl355_set_odr(), adxl355_set_hpf_3db() and adxl355_set_calibbias(). Remove all goto labels and return directly on error paths.

Quoted 4 lines
Quoted 2 lines

v3: Remove all remaining gotos and return directly where possible.
v2: Split into separate patch per driver.

Same issue for the change log needing to be below the ---

One more thing. Sometimes guard() brings enough extra complexity it isn't the right tool for the job. Whilst you can explore the suggestions for helper functions I make below, I'm not sure they will lead to easier to read and less error prone code. So it might not be a good idea to use them in this driver at all!

Jonathan

Quoted 182 lines
Quoted 13 lines
---
 drivers/iio/accel/adxl355_core.c | 81 ++++++++++++++------------------
 1 file changed, 34 insertions(+), 47 deletions(-)
drivers/iio/accel/adxl355_core.c +0 -0
index 1c1d64d5cbcb..af606e2ab8d4 100644

...

Quoted 16 lines

static void adxl355_fill_3db_frequency_table(struct adxl355_data *data)

drivers/iio/accel/adxl355_core.c +1 -2
@@ -409,38 +407,34 @@ 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)

drivers/iio/accel/adxl355_core.c static int adxl355_set_odr(struct adxl355_data *data, · line 424

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +3 -3
-		goto err_unlock;
+		return ret;
 
 	ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
 				 ADXL355_FILTER_ODR_MSK,
 				 FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr));
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);  

See below.

Quoted 39 lines
drivers/iio/accel/adxl355_core.c +7 -11
+		return ret;
+	}
 
 	data->odr = odr;
 	adxl355_fill_3db_frequency_table(data);
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_set_hpf_3db(struct adxl355_data *data,
@@ -448,37 +442,33 @@ 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)

drivers/iio/accel/adxl355_core.c static int adxl355_set_hpf_3db(struct adxl355_data *data, · line 459

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +3 -3
-		goto err_unlock;
+		return ret;
 
 	ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG,
 				 ADXL355_FILTER_HPF_MSK,
 				 FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf));
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);  

Similar to below.

Quoted 33 lines
drivers/iio/accel/adxl355_core.c +7 -10
+		return ret;
+	}
 
 	data->hpf_3db = hpf;
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_set_calibbias(struct adxl355_data *data,
@@ -486,33 +476,30 @@ 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)

drivers/iio/accel/adxl355_core.c static int adxl355_set_calibbias(struct adxl355_data *data, · line 489

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +2 -3
-		goto err_unlock;
+		return ret;
 
 	put_unaligned_be16(calibbias, data->transf_buf);
 	ret = regmap_bulk_write(data->regmap,
 				adxl355_chans[chan].offset_reg,
 				data->transf_buf, 2);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){  

Whilst this does answer the feedback you got on v2 wrt to not mixing gotos (see comments on this in cleanup.h) it leads to inelegant code due to the duplication.

There are a couple of techniques to avoid this. The most appropriate one here is to use a helper function called something like do_adxl355_set_calibbias() which has all the code
done between set_op_mode and unsetting it.

Then you can unconditionally unset the op_mode before checking if there as an error. You will need to be a little clever with ret code handling though.

drivers/iio/accel/adxl355_core.c static int adxl355_set_calibbias(struct adxl355_data *data, · line 497

Quoted 10 lines
drivers/iio/accel/adxl355_core.c +4 -2
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
 	data->calibbias[chan] = calibbias;
 
 	ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-	if (ret)
-		goto err_set_opmode;
+	if (ret){  

Run checkpatch.pl over your patch. Should be a space before that {

Quoted 15 lines
drivers/iio/accel/adxl355_core.c +3 -7
+		adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
+		return ret;
+	}
 
-	mutex_unlock(&data->lock);
 	return 0;
-
-err_set_opmode:
-	adxl355_set_op_mode(data, ADXL355_MEASUREMENT);
-err_unlock:
-	mutex_unlock(&data->lock);
-	return ret;
 }
 
 static int adxl355_read_raw(struct iio_dev *indio_dev,  
#14 lore raw

PATCH v3 3/3 iio: accel: adxl372: convert to guard(mutex)

In reply to Rajveer Chaudhari

Replace manual mutex_lock/mutex_unlock pair with guard(mutex) in adxl372_write_threshold_value(). Remove goto label and return directly on error path.

v3: Return directly from error path without goto.
v2: Split into separate patch per driver.

---
 drivers/iio/accel/adxl372.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
drivers/iio/accel/adxl372.c +6 -10
index 28a8793a53b6..340433782cf2 100644
@@ -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>
@@ -334,20 +335,15 @@ static ssize_t adxl372_write_threshold_value(struct iio_dev *indio_dev, unsigned
 					     u16 threshold)
 {
 	struct adxl372_state *st = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&st->threshold_m);
-	ret = regmap_write(st->regmap, addr, ADXL372_THRESH_VAL_H_SEL(threshold));
+	guard(mutex)(&st->threshold_m);
+	
+	int 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),
+	return 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;
 }
 
 static int adxl372_read_axis(struct adxl372_state *st, u8 addr)
Signature · 1 line
2.53.0
maintainer
#15 lore raw

Re: [PATCH v3 3/3] iio: accel: adxl372: convert to guard(mutex)

Quoted 6 lines · On Sat, 7 Mar 2026 15:47:58 +0530 Rajveer Chaudhari…

Replace manual mutex_lock/mutex_unlock pair with guard(mutex) in adxl372_write_threshold_value(). Remove goto label and return directly on error path.

v3: Return directly from error path without goto.
v2: Split into separate patch per driver.

As in previous, this needs to move down.
Otherwise this patch is fine.

Thanks,

Jonathan

Quoted 44 lines
---
 drivers/iio/accel/adxl372.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
drivers/iio/accel/adxl372.c +6 -10
index 28a8793a53b6..340433782cf2 100644
@@ -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>
@@ -334,20 +335,15 @@ static ssize_t adxl372_write_threshold_value(struct iio_dev *indio_dev, unsigned
 					     u16 threshold)
 {
 	struct adxl372_state *st = iio_priv(indio_dev);
-	int ret;
 
-	mutex_lock(&st->threshold_m);
-	ret = regmap_write(st->regmap, addr, ADXL372_THRESH_VAL_H_SEL(threshold));
+	guard(mutex)(&st->threshold_m);
+	
+	int 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),
+	return 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;
 }
 
 static int adxl372_read_axis(struct adxl372_state *st, u8 addr)
maintainer
#16 lore raw

Re: [PATCH v3 0/3] iio: accel: convert to guard(mutex)

In reply to Rajveer Chaudhari
Quoted 15 lines · On Sat, 7 Mar 2026 15:47:55 +0530 Rajveer Chaudhari…

This series converts manual mutex_lock/mutex_unlock pairs to guard(mutex) in three ADXL accelerometer drivers. Each conversion also simplifies error handling by removing goto labels and returning directly where possible.

v3: Remove remaining goto labels completely as requested by David Lechner.
Return directly from functions where ret variable is not needed.
v2: Split into one patch per driver as requested by David Lechner.
Ensured each conversion also simplifies code flow.

Rajveer Chaudhari (3):
iio: accel: adxl313: convert to guard(mutex)
iio: accel: adxl355: convert to guard(mutex)
iio: accel: adxl372: convert to guard(mutex)

Quoted 5 lines
 drivers/iio/accel/adxl313_core.c | 15 ++----
 drivers/iio/accel/adxl355_core.c | 81 ++++++++++++++------------------
 drivers/iio/accel/adxl372.c      | 16 +++----
 3 files changed, 45 insertions(+), 67 deletions(-)

Hi Rajveer,

Small process thing. Please send each new version as a separate email thread. It can get very hard to follow when we end up with deeply nested threads so most areas of the kernel prefer to just associate them via name of the thread and possibly a link in the cover letter to the previous version.

Jonathan

#17 lore raw

Re: [PATCH v3 0/3] iio: accel: convert to guard(mutex)

Hello Jonathan,

I am sorry for making things confusing, do you want me to re-send V3 on a new separate email thread?
Or I should keep this in mind for future patches.

Thankyou,
Rajveer

Quoted 34 lines · Jonathan Cameron
Quoted 15 lines · On Sat, 7 Mar 2026 15:47:55 +0530 Rajveer Chaudhari…

This series converts manual mutex_lock/mutex_unlock pairs to guard(mutex) in three ADXL accelerometer drivers. Each conversion also simplifies error handling by removing goto labels and returning directly where possible.

v3: Remove remaining goto labels completely as requested by David Lechner.
Return directly from functions where ret variable is not needed.
v2: Split into one patch per driver as requested by David Lechner.
Ensured each conversion also simplifies code flow.

Rajveer Chaudhari (3):
iio: accel: adxl313: convert to guard(mutex)
iio: accel: adxl355: convert to guard(mutex)
iio: accel: adxl372: convert to guard(mutex)

Quoted 5 lines
 drivers/iio/accel/adxl313_core.c | 15 ++----
 drivers/iio/accel/adxl355_core.c | 81 ++++++++++++++------------------
 drivers/iio/accel/adxl372.c      | 16 +++----
 3 files changed, 45 insertions(+), 67 deletions(-)

Hi Rajveer,

Small process thing. Please send each new version as a separate email thread. It can get very hard to follow when we end up with deeply nested threads so most areas of the kernel prefer to just associate them via name of the thread and possibly a link in the cover letter to the previous version.

Jonathan

maintainer
#18 lore raw

Re: [PATCH v3 0/3] iio: accel: convert to guard(mutex)

Quoted 5 lines · On Sat, 7 Mar 2026 16:03:58 +0530 Rajveer Chaudhari…

Hello Jonathan,

I am sorry for making things confusing, do you want me to re-send V3 on a new separate email thread?
Or I should keep this in mind for future patches.

Keep in mind for future patch sets / v4 of this one

Thanks,

J

Quoted 39 lines

Thankyou,
Rajveer

Quoted 34 lines · Jonathan Cameron
Quoted 15 lines · On Sat, 7 Mar 2026 15:47:55 +0530 Rajveer Chaudhari…

This series converts manual mutex_lock/mutex_unlock pairs to guard(mutex) in three ADXL accelerometer drivers. Each conversion also simplifies error handling by removing goto labels and returning directly where possible.

v3: Remove remaining goto labels completely as requested by David Lechner.
Return directly from functions where ret variable is not needed.
v2: Split into one patch per driver as requested by David Lechner.
Ensured each conversion also simplifies code flow.

Rajveer Chaudhari (3):
iio: accel: adxl313: convert to guard(mutex)
iio: accel: adxl355: convert to guard(mutex)
iio: accel: adxl372: convert to guard(mutex)

Quoted 5 lines
 drivers/iio/accel/adxl313_core.c | 15 ++----
 drivers/iio/accel/adxl355_core.c | 81 ++++++++++++++------------------
 drivers/iio/accel/adxl372.c      | 16 +++----
 3 files changed, 45 insertions(+), 67 deletions(-)

Hi Rajveer,

Small process thing. Please send each new version as a separate email thread. It can get very hard to follow when we end up with deeply nested threads so most areas of the kernel prefer to just associate them via name of the thread and possibly a link in the cover letter to the previous version.

Jonathan

#19 lore raw
In reply to Rajveer Chaudhari
Quoted 3 lines · 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.

...

Quoted 4 lines

#include <linux/kmod.h>
#include <linux/list.h>
#include <linux/slab.h>
+#include <linux/cleanup.h>

Move it to the better place to keep more lines ordered.

Quoted 4 lines

#include <linux/kmod.h>
#include <linux/list.h>
#include <linux/slab.h>
+#include <linux/cleanup.h>

Ditto.,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#20 lore raw

Thanks for the review Andy.

Those files have been dropped in v2 of this series which was just sent. The v2 focuses only on the ADXL accelerometer drivers where the conversion also simplifies the error handling.

With Best Regards,
Rajveer Chaudhari

Quoted 27 lines · Andy Shevchenko
Quoted 3 lines · 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.

...

Quoted 4 lines

#include <linux/kmod.h>
#include <linux/list.h>
#include <linux/slab.h>
+#include <linux/cleanup.h>

Move it to the better place to keep more lines ordered.

Quoted 4 lines

#include <linux/kmod.h>
#include <linux/list.h>
#include <linux/slab.h>
+#include <linux/cleanup.h>

Ditto.,

--
With Best Regards,
Andy Shevchenko