* [PATCH v2] iio: magnetometer: bmc150_magn: Used guard() for mutex protection
@ 2026-02-04 1:05 Neel Bullywon
2026-02-04 1:09 ` David Lechner
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Neel Bullywon @ 2026-02-04 1:05 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 | 21 +++++++--------------
1 file changed, 7 insertions(+), 14 deletions(-)
diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c
index 6a73f6e2f1f0..bd2706c2078d 100644
--- a/drivers/iio/magnetometer/bmc150_magn.c
+++ b/drivers/iio/magnetometer/bmc150_magn.c
@@ -794,32 +794,25 @@ static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
{
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
struct bmc150_magn_data *data = iio_priv(indio_dev);
- int ret = 0;
+ int ret;
+
+ guard(mutex)(&data->mutex);
- mutex_lock(&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) {
- ret = bmc150_magn_reset_intr(data);
- if (ret < 0)
- goto err_unlock;
- }
- mutex_unlock(&data->mutex);
+ if (state)
+ return bmc150_magn_reset_intr(data);
return 0;
-
-err_unlock:
- mutex_unlock(&data->mutex);
- return ret;
}
static const struct iio_trigger_ops bmc150_magn_trigger_ops = {
--
2.44.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] iio: magnetometer: bmc150_magn: Used guard() for mutex protection
2026-02-04 1:05 [PATCH v2] iio: magnetometer: bmc150_magn: Used guard() for mutex protection Neel Bullywon
@ 2026-02-04 1:09 ` David Lechner
2026-02-04 1:15 ` Andy Shevchenko
2026-02-04 2:12 ` [PATCH v3] " Neel Bullywon
2 siblings, 0 replies; 5+ messages in thread
From: David Lechner @ 2026-02-04 1:09 UTC (permalink / raw)
To: Neel Bullywon, Jonathan Cameron
Cc: Nuno Sá, Andy Shevchenko, linux-iio, linux-kernel
On 2/3/26 7:05 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>
> ---
Reviewed-by: David Lechner <dlechner@baylibre.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] iio: magnetometer: bmc150_magn: Used guard() for mutex protection
2026-02-04 1:05 [PATCH v2] iio: magnetometer: bmc150_magn: Used guard() for mutex protection Neel Bullywon
2026-02-04 1:09 ` David Lechner
@ 2026-02-04 1:15 ` Andy Shevchenko
2026-02-04 2:12 ` [PATCH v3] " Neel Bullywon
2 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2026-02-04 1:15 UTC (permalink / raw)
To: Neel Bullywon
Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
linux-iio, linux-kernel
On Tue, Feb 03, 2026 at 08:05:29PM -0500, 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
Missed period.
> This was compiled test only
Ditto.
No need to put this into commit message.
Code wise looks good.
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3] iio: magnetometer: bmc150_magn: Used guard() for mutex protection
2026-02-04 1:05 [PATCH v2] iio: magnetometer: bmc150_magn: Used guard() for mutex protection Neel Bullywon
2026-02-04 1:09 ` David Lechner
2026-02-04 1:15 ` Andy Shevchenko
@ 2026-02-04 2:12 ` Neel Bullywon
2026-02-05 20:25 ` Jonathan Cameron
2 siblings, 1 reply; 5+ messages in thread
From: Neel Bullywon @ 2026-02-04 2:12 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
linux-kernel, Neel Bullywon, Andy Shevchenko
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.
Signed-off-by: Neel Bullywon <neelb2403@gmail.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
---
drivers/iio/magnetometer/bmc150_magn.c | 21 +++++++--------------
1 file changed, 7 insertions(+), 14 deletions(-)
diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c
index 6a73f6e2f1f0..bd2706c2078d 100644
--- a/drivers/iio/magnetometer/bmc150_magn.c
+++ b/drivers/iio/magnetometer/bmc150_magn.c
@@ -794,32 +794,25 @@ static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
{
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
struct bmc150_magn_data *data = iio_priv(indio_dev);
- int ret = 0;
+ int ret;
+
+ guard(mutex)(&data->mutex);
- mutex_lock(&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) {
- ret = bmc150_magn_reset_intr(data);
- if (ret < 0)
- goto err_unlock;
- }
- mutex_unlock(&data->mutex);
+ if (state)
+ return bmc150_magn_reset_intr(data);
return 0;
-
-err_unlock:
- mutex_unlock(&data->mutex);
- return ret;
}
static const struct iio_trigger_ops bmc150_magn_trigger_ops = {
--
2.44.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v3] iio: magnetometer: bmc150_magn: Used guard() for mutex protection
2026-02-04 2:12 ` [PATCH v3] " Neel Bullywon
@ 2026-02-05 20:25 ` Jonathan Cameron
0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2026-02-05 20:25 UTC (permalink / raw)
To: Neel Bullywon
Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
linux-kernel, Andy Shevchenko
On Tue, 3 Feb 2026 21:12:41 -0500
Neel Bullywon <neelb2403@gmail.com> 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.
>
> Signed-off-by: Neel Bullywon <neelb2403@gmail.com>
> Reviewed-by: David Lechner <dlechner@baylibre.com>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Looks like this needs to have
#include <linux/cleanup.h>
as first use of this stuff in this particular file.
From a quick look it seems to me that there might be other potential
similar cases in this file that would make sense to include as part of this
patch. Perhaps you could take a look at those?
In some places it is in a switch statement so you'd have to
add {} to define the scope, or use scoped_guard() as appropriate.
Thanks,
Jonathan
> ---
> drivers/iio/magnetometer/bmc150_magn.c | 21 +++++++--------------
> 1 file changed, 7 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c
> index 6a73f6e2f1f0..bd2706c2078d 100644
> --- a/drivers/iio/magnetometer/bmc150_magn.c
> +++ b/drivers/iio/magnetometer/bmc150_magn.c
> @@ -794,32 +794,25 @@ static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
> {
> struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
> struct bmc150_magn_data *data = iio_priv(indio_dev);
> - int ret = 0;
> + int ret;
> +
> + guard(mutex)(&data->mutex);
>
> - mutex_lock(&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) {
> - ret = bmc150_magn_reset_intr(data);
> - if (ret < 0)
> - goto err_unlock;
> - }
> - mutex_unlock(&data->mutex);
> + if (state)
> + return bmc150_magn_reset_intr(data);
>
> return 0;
> -
> -err_unlock:
> - mutex_unlock(&data->mutex);
> - return ret;
> }
>
> static const struct iio_trigger_ops bmc150_magn_trigger_ops = {
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-02-05 20:25 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-04 1:05 [PATCH v2] iio: magnetometer: bmc150_magn: Used guard() for mutex protection Neel Bullywon
2026-02-04 1:09 ` David Lechner
2026-02-04 1:15 ` Andy Shevchenko
2026-02-04 2:12 ` [PATCH v3] " Neel Bullywon
2026-02-05 20:25 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox