public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Neel Bullywon <neelb2403@gmail.com>
Cc: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
	andy@kernel.org, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v6 1/3] iio: magnetometer: bmc150_magn: use automated cleanup for mutex
Date: Wed, 11 Feb 2026 10:39:35 +0200	[thread overview]
Message-ID: <aYxARz8CXoLRQtBC@smile.fi.intel.com> (raw)
In-Reply-To: <20260210233945.40975-2-neelb2403@gmail.com>

On Tue, Feb 10, 2026 at 06:39:43PM -0500, Neel Bullywon wrote:
> Use guard() and scoped_guard() to replace manual mutex lock/unlock
> calls. This simplifies error handling and ensures RAII-style cleanup.
> 
> scoped_guard() is used in read_raw IIO_CHAN_INFO_RAW case for a
> multi-statement mutex-protected block, as well as in remove,
> runtime_suspend, and suspend where a short mutex-protected scope
> is needed for a single function call.
> 
> guard() is used in write_raw and trig_reen where the mutex scope
> extends to the end of the function or case block. Case blocks in
> write_raw are wrapped in braces to ensure clear scope for the
> cleanup guards.
> 
> The trigger_handler function is left unchanged as mixing guard()
> with goto error paths can be fragile.

...

>  #include <linux/module.h>
>  #include <linux/i2c.h>
> +#include <linux/cleanup.h>
>  #include <linux/interrupt.h>
>  #include <linux/delay.h>
>  #include <linux/slab.h>

With a given context I would place the new one just before the delay.h.


...

>  	case IIO_CHAN_INFO_RAW:
>  		if (iio_buffer_enabled(indio_dev))
>  			return -EBUSY;
> -		mutex_lock(&data->mutex);
> -
> -		ret = bmc150_magn_set_power_state(data, true);
> -		if (ret < 0) {
> -			mutex_unlock(&data->mutex);
> -			return ret;
> -		}
> +		scoped_guard(mutex, &data->mutex) {
> +			ret = bmc150_magn_set_power_state(data, true);
> +			if (ret < 0)
> +				return ret;
>  
> -		ret = bmc150_magn_read_xyz(data, values);
> -		if (ret < 0) {
> -			bmc150_magn_set_power_state(data, false);
> -			mutex_unlock(&data->mutex);
> -			return ret;
> -		}
> -		*val = values[chan->scan_index];
> +			ret = bmc150_magn_read_xyz(data, values);
> +			if (ret < 0) {
> +				bmc150_magn_set_power_state(data, false);
> +				return ret;
> +			}
> +			*val = values[chan->scan_index];
>  
> -		ret = bmc150_magn_set_power_state(data, false);
> -		if (ret < 0) {
> -			mutex_unlock(&data->mutex);
> -			return ret;
> +			ret = bmc150_magn_set_power_state(data, false);
> +			if (ret < 0)
> +				return ret;
>  		}
> -
> -		mutex_unlock(&data->mutex);
>  		return IIO_VAL_INT;

Actually looking again at this, I think we may use guard()(), but move the {}
to the whole case:

	case IIO_CHAN_INFO_RAW: {
		if (iio_buffer_enabled(indio_dev))
			return -EBUSY;

		guard(mutex)(&data->mutex);
		...
	}

This will make the change much more cleaner.

...

>  		case IIO_MOD_Z:
> +			{
> +				guard(mutex)(&data->mutex);

Why do you change only one case?! If the comment is given, it applies
to all similar places. But see also above.

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-02-11  8:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-10 23:39 [PATCH v6 0/3] iio: magnetometer: bmc150_magn: cleanup and formatting Neel Bullywon
2026-02-10 23:39 ` [PATCH v6 1/3] iio: magnetometer: bmc150_magn: use automated cleanup for mutex Neel Bullywon
2026-02-11  8:39   ` Andy Shevchenko [this message]
2026-02-13 23:31   ` kernel test robot
2026-02-14 16:34     ` Jonathan Cameron
2026-02-14 16:28   ` David Lechner
2026-02-10 23:39 ` [PATCH v6 2/3] iio: magnetometer: bmc150_magn: replace msleep with fsleep Neel Bullywon
2026-02-11  8:40   ` Andy Shevchenko
2026-02-10 23:39 ` [PATCH v6 3/3] iio: magnetometer: bmc150_magn: minor formatting cleanup Neel Bullywon
2026-02-11  8:41   ` Andy Shevchenko
2026-02-14 16:31   ` David Lechner
2026-02-14 18:00     ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aYxARz8CXoLRQtBC@smile.fi.intel.com \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=neelb2403@gmail.com \
    --cc=nuno.sa@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox