Re: [PATCH] iio: magnetometer: bmc150_magn: Used guard() for mutex protection

From: David Lechner

Date: Tue Feb 03 2026 - 19:49:56 EST


On 2/3/26 6:03 PM, Neel Bullywon wrote:
> Replaced the manual mutex lock/unlock calls using guard() to simplify
> the error handling and reduce goto statement usage.
>
> By using RAII-style cleanup, it ensures the mutex is always released
> when exiting the function, regardless of the return path taken
>
> This was compiled test only
>
> Signed-off-by: Neel Bullywon <neelb2403@xxxxxxxxx>
> ---
> drivers/iio/magnetometer/bmc150_magn.c | 17 +++++------------
> 1 file changed, 5 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c
> index 6a73f6e2f1f0..73d9183a354b 100644
> --- a/drivers/iio/magnetometer/bmc150_magn.c
> +++ b/drivers/iio/magnetometer/bmc150_magn.c
> @@ -796,29 +796,22 @@ static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
> struct bmc150_magn_data *data = iio_priv(indio_dev);
> int ret = 0;

We can take this a bit farther and drop the init here.

>
> - mutex_lock(&data->mutex);
> + guard(mutex)(&data->mutex);
> +
> if (state == data->dready_trigger_on)
> - goto err_unlock;
> + return 0;
>
> ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
> BMC150_MAGN_MASK_DRDY_EN,
> state << BMC150_MAGN_SHIFT_DRDY_EN);
> if (ret < 0)
> - goto err_unlock;
> + return ret;
>
> data->dready_trigger_on = state;
>
> - if (state) {
> + if (state)
> ret = bmc150_magn_reset_intr(data);

return directly here

> - if (ret < 0)
> - goto err_unlock;
> - }
> - mutex_unlock(&data->mutex);
>
> - return 0;
> -
> -err_unlock:
> - mutex_unlock(&data->mutex);
> return ret;

and return 0 here.

> }
>