mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Lechner <dlechner@baylibre.com>
To: Maxwell Doose <m32285159@gmail.com>,
	tduszyns@gmail.com, jic23@kernel.org
Cc: "Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"open list:IIO SUBSYSTEM AND DRIVERS" <linux-iio@vger.kernel.org>,
	"open list" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] iio: chemical: sps30: Replace manual locking with RAII locking
Date: Sat, 9 May 2026 16:22:13 -0500	[thread overview]
Message-ID: <f9c1a25b-456f-4ba9-b1c4-7958f2fd2aa8@baylibre.com> (raw)
In-Reply-To: <20260509125201.29872-1-m32285159@gmail.com>

On 5/9/26 7:52 AM, Maxwell Doose wrote:
> Replace manual mutex_lock() and mutex_unlock() calls with the much newer
> guard(mutex)() and scoped_guard() macros to enable RAII patterns,
> modernize the driver, and to increase readability.
> 
> Signed-off-by: Maxwell Doose <m32285159@gmail.com>
> ---
>  drivers/iio/chemical/sps30.c | 60 +++++++++++++++++-------------------
>  1 file changed, 29 insertions(+), 31 deletions(-)
> 
> diff --git a/drivers/iio/chemical/sps30.c b/drivers/iio/chemical/sps30.c
> index a934bf0298dd..186dec4cfd78 100644
> --- a/drivers/iio/chemical/sps30.c
> +++ b/drivers/iio/chemical/sps30.c
> @@ -5,6 +5,7 @@
>   * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
>   */
>  
> +#include <linux/cleanup.h>
>  #include <linux/crc8.h>
>  #include <linux/delay.h>
>  #include <linux/i2c.h>
> @@ -111,9 +112,9 @@ static irqreturn_t sps30_trigger_handler(int irq, void *p)
>  		aligned_s64 ts;
>  	} scan;
>  
> -	mutex_lock(&state->lock);
> -	ret = sps30_do_meas(state, scan.data, ARRAY_SIZE(scan.data));
> -	mutex_unlock(&state->lock);
> +	scoped_guard(mutex, &state->lock)
> +		ret = sps30_do_meas(state, scan.data, ARRAY_SIZE(scan.data));
> +
>  	if (ret)
>  		goto err;
>  
> @@ -136,23 +137,23 @@ static int sps30_read_raw(struct iio_dev *indio_dev,
>  	case IIO_CHAN_INFO_PROCESSED:
>  		switch (chan->type) {
>  		case IIO_MASSCONCENTRATION:
> -			mutex_lock(&state->lock);
> -			/* read up to the number of bytes actually needed */
> -			switch (chan->channel2) {
> -			case IIO_MOD_PM1:
> -				ret = sps30_do_meas(state, data, 1);
> -				break;
> -			case IIO_MOD_PM2P5:
> -				ret = sps30_do_meas(state, data, 2);
> -				break;
> -			case IIO_MOD_PM4:
> -				ret = sps30_do_meas(state, data, 3);
> -				break;
> -			case IIO_MOD_PM10:
> -				ret = sps30_do_meas(state, data, 4);
> -				break;

We can do it like this:

  		case IIO_MASSCONCENTRATION: {
			guard(mutex)(&state->lock);

			/* read up to the number of bytes actually needed */
			switch (chan->channel2) {
			case IIO_MOD_PM1:
				ret = sps30_do_meas(state, data, 1);
				break;
			case IIO_MOD_PM2P5:
				ret = sps30_do_meas(state, data, 2);
				break;
			case IIO_MOD_PM4:
				ret = sps30_do_meas(state, data, 3);
				break;
			case IIO_MOD_PM10:
				ret = sps30_do_meas(state, data, 4);
				break;
			default:
				return -EINVAL;
			}
			if (ret)
				return ret;

		
			*val = data[chan->address] / 100;
			*val2 = (data[chan->address] % 100) * 10000;

			return IIO_VAL_INT_PLUS_MICRO;
		}
		default:
			return -EINVAL;

Make less indent and we don't have to initialize ret at the start of
the function anymore.

> +			scoped_guard(mutex, &state->lock) {
> +				/* read up to the number of bytes actually needed */
> +				switch (chan->channel2) {
> +				case IIO_MOD_PM1:
> +					ret = sps30_do_meas(state, data, 1);
> +					break;
> +				case IIO_MOD_PM2P5:
> +					ret = sps30_do_meas(state, data, 2);
> +					break;
> +				case IIO_MOD_PM4:
> +					ret = sps30_do_meas(state, data, 3);
> +					break;
> +				case IIO_MOD_PM10:
> +					ret = sps30_do_meas(state, data, 4);
> +					break;
> +				}
>  			}
> -			mutex_unlock(&state->lock);
>  			if (ret)
>  				return ret;
>  
> @@ -197,9 +198,9 @@ static ssize_t start_cleaning_store(struct device *dev,
>  	if (kstrtoint(buf, 0, &val) || val != 1)
>  		return -EINVAL;
>  
> -	mutex_lock(&state->lock);
> -	ret = state->ops->clean_fan(state);
> -	mutex_unlock(&state->lock);
> +	scoped_guard(mutex, &state->lock)

Doesn't need to be scoped. there is just return after this.

> +		ret = state->ops->clean_fan(state);
> +
>  	if (ret)
>  		return ret;
>  
> @@ -215,9 +216,9 @@ static ssize_t cleaning_period_show(struct device *dev,
>  	__be32 val;
>  	int ret;
>  
> -	mutex_lock(&state->lock);
> -	ret = state->ops->read_cleaning_period(state, &val);
> -	mutex_unlock(&state->lock);
> +	scoped_guard(mutex, &state->lock)
> +		ret = state->ops->read_cleaning_period(state, &val);
> +

IMHO, this one just make the code uglier, not really an improvement.

>  	if (ret)
>  		return ret;
>  

  parent reply	other threads:[~2026-05-09 21:22 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-09 12:52 Maxwell Doose
2026-05-09 13:04 ` Joshua Crofts
2026-05-09 23:55   ` Maxwell Doose
2026-05-10  9:32     ` Joshua Crofts
2026-05-09 21:22 ` David Lechner [this message]
2026-05-09 21:44   ` Maxwell Doose
2026-05-10  6:53 ` Andy Shevchenko
2026-05-10  6:57   ` Maxwell Doose

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=f9c1a25b-456f-4ba9-b1c4-7958f2fd2aa8@baylibre.com \
    --to=dlechner@baylibre.com \
    --cc=andy@kernel.org \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m32285159@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=tduszyns@gmail.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

all inboxes | Powered by JetHome®