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

Rajveer Chaudhari posted 3 patches 1 month ago
There is a newer version of this series
[PATCH v3 1/3] iio: accel: adxl313: convert to guard(mutex)
Posted by Rajveer Chaudhari 1 month ago
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.

Signed-off-by: Rajveer Chaudhari <rajveer.chaudhari.linux@gmail.com>
---
 drivers/iio/accel/adxl313_core.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/accel/adxl313_core.c b/drivers/iio/accel/adxl313_core.c
index 9f5d4d2cb325..40f62c6f89b4 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>
@@ -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,
-- 
2.53.0
Re: [PATCH v3 1/3] iio: accel: adxl313: convert to guard(mutex)
Posted by Jonathan Cameron 1 month ago
On Sat,  7 Mar 2026 15:47:56 +0530
Rajveer Chaudhari <rajveer.chaudhari.linux@gmail.com> wrote:

> 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..
> 
> Signed-off-by: Rajveer Chaudhari <rajveer.chaudhari.linux@gmail.com>
> ---
.. 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.

>  drivers/iio/accel/adxl313_core.c | 15 +++++----------
>  1 file changed, 5 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/iio/accel/adxl313_core.c b/drivers/iio/accel/adxl313_core.c
> index 9f5d4d2cb325..40f62c6f89b4 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>
> @@ -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


>  			       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,