* [PATCH] iio: magnetometer: bmc150_magn: Used guard() for mutex protection
@ 2026-02-04 0:03 Neel Bullywon
2026-02-04 0:49 ` David Lechner
0 siblings, 1 reply; 2+ messages in thread
From: Neel Bullywon @ 2026-02-04 0:03 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel, Neel Bullywon
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@gmail.com>
---
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;
- 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);
- if (ret < 0)
- goto err_unlock;
- }
- mutex_unlock(&data->mutex);
- return 0;
-
-err_unlock:
- mutex_unlock(&data->mutex);
return ret;
}
--
2.44.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] iio: magnetometer: bmc150_magn: Used guard() for mutex protection
2026-02-04 0:03 [PATCH] iio: magnetometer: bmc150_magn: Used guard() for mutex protection Neel Bullywon
@ 2026-02-04 0:49 ` David Lechner
0 siblings, 0 replies; 2+ messages in thread
From: David Lechner @ 2026-02-04 0:49 UTC (permalink / raw)
To: Neel Bullywon, Jonathan Cameron
Cc: Nuno Sá, Andy Shevchenko, linux-iio, linux-kernel
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@gmail.com>
> ---
> 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.
> }
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-02-04 0:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-04 0:03 [PATCH] iio: magnetometer: bmc150_magn: Used guard() for mutex protection Neel Bullywon
2026-02-04 0:49 ` David Lechner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®