iio: accel: convert to guard(mutex)
Subsystems (4)
v4 cover iio: accel: convert to guard(mutex)
This series converts manual mutex_lock/mutex_unlock pairs to guard(mutex) in two ADXL accelerometer drivers. Each conversion also simplifies error handling by removing goto labels and returning directly on error paths.
adxl355 was considered but dropped as guard(mutex) leads to code duplication due to the STANDBY/MEASUREMENT op_mode pattern, making it less readable than the original code.
Ref to v3: [PATCH v3 0/3] iio: accel: convert to guard(mutex)
Rajveer Chaudhari (2):
iio: accel: adxl313: convert to guard(mutex)
iio: accel: adxl372: convert to guard(mutex)
drivers/iio/accel/adxl313_core.c | 11 ++++------- drivers/iio/accel/adxl372.c | 13 +++++-------- 2 files changed, 9 insertions(+), 15 deletions(-)
Signature · 1 line
2.53.0
PATCH v4 1/2 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.
---
v4: Keep ret declaration at top of function.
v3: Return directly from regmap_bulk_read error path.
v2: Split into separate patch per driver.
--- drivers/iio/accel/adxl313_core.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/iio/accel/adxl313_core.c +4 -7
@@ -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,19 +357,15 @@ 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; - - 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
PATCH v4 2/2 iio: accel: adxl372: convert to guard(mutex)
In reply to Rajveer ChaudhariReplace manual mutex_lock/mutex_unlock pair with guard(mutex) in adxl372_write_threshold_value(). This ensures the mutex is released on all return paths and allows returning directly without a goto label.
---
v4: Changelog moved below --- as requested by Jonathan Cameron.
v3: Return directly from error path without goto.
v2: Split into separate patch per driver.
--- drivers/iio/accel/adxl372.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/iio/accel/adxl372.c +5 -8
@@ -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,18 +337,14 @@ static ssize_t adxl372_write_threshold_value(struct iio_dev *indio_dev, unsignedstruct 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 ), +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
Jonathan Cameron
maintainerRe: [PATCH v4 2/2] iio: accel: adxl372: convert to guard(mutex)
Quoted 5 lines · On Sat, 7 Mar 2026 17:19:12 +0530 Rajveer Chaudhari…
Replace manual mutex_lock/mutex_unlock pair with guard(mutex) in
adxl372_write_threshold_value(). This ensures the mutex is released on all return paths and allows returning directly without a goto label.Signed-off-by Rajveer Chaudhari <[email protected]>
Series applied. One small tweak mentioned below.
Applied to the testing branch of iio.git where the bots poke at it briefly before I push it out as togreg which linux-next picks up.
Thanks,
Jonathan
Quoted 25 lines
---
v4: Changelog moved below --- as requested by Jonathan Cameron.
v3: Return directly from error path without goto.
v2: Split into separate patch per driver.--- drivers/iio/accel/adxl372.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)drivers/iio/accel/adxl372.c +1
-1@@ -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,18 +337,14 @@ static ssize_t adxl372_write_threshold_value(struct iio_dev *indio_dev, unsignedstruct adxl372_state *st =iio_priv (indio_dev);int ret; -mutex_lock (&st->threshold_m);
drivers/iio/accel/adxl372.c static ssize_t adxl372_write_threshold_value(struct iio_dev *indio_dev, unsigned · line 347
Quoted 10 lines
drivers/iio/accel/adxl372.c +4
-2+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 ), +return regmap_update_bits (st->regmap, addr +1 ,GENMASK (7 ,5 ),ADXL372_THRESH_VAL_L_SEL (threshold) <<5 );
I'd expect a minor indent update to be needed here. 1 more space.
Quoted 8 lines
drivers/iio/accel/adxl372.c +0
-5- -unlock: -mutex_unlock (&st->threshold_m); - -return ret; }static int adxl372_read_axis (struct adxl372_state *st,u8 addr)