Re: [PATCH] iio: convert to guard(mutex)
From: David Lechner
Date: Fri Mar 06 2026 - 15:37:31 EST
On 3/6/26 2:26 PM, Rajveer Chaudhari wrote:
> 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.
>
> Signed-off-by: Rajveer Chaudhari <rajveer.chaudhari.linux@xxxxxxxxx>
> ---
> 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.
> 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;
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.
> }
>