devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Guillaume Stols <gstols@baylibre.com>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
	Michael Hennerich <Michael.Hennerich@analog.com>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Beniamin Bia <beniamin.bia@analog.com>,
	Stefan Popa <stefan.popa@analog.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-fbdev@vger.kernel.org, devicetree@vger.kernel.org,
	Jonathan Cameron <Jonathan.Cameron@huawei.com>,
	jstephan@baylibre.com, dlechner@baylibre.com
Subject: Re: [PATCH v2 10/10] iio: adc: ad7606: switch mutexes to scoped_guard
Date: Sat, 29 Jun 2024 16:36:59 +0100	[thread overview]
Message-ID: <20240629163659.322954c3@jic23-huawei> (raw)
In-Reply-To: <20240628-cleanup-ad7606-v2-10-96e02f90256d@baylibre.com>

On Fri, 28 Jun 2024 14:48:28 +0000
Guillaume Stols <gstols@baylibre.com> wrote:

> Switching to scoped_guard simplifies the code and avoids to take care to
> unlock the mutex in case of premature return.
> 
> Signed-off-by: Guillaume Stols <gstols@baylibre.com>
> ---
>  drivers/iio/adc/ad7606.c | 60 ++++++++++++++++++++++--------------------------
>  1 file changed, 27 insertions(+), 33 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c
> index 50ccc245e314..3c439787d130 100644
> --- a/drivers/iio/adc/ad7606.c
> +++ b/drivers/iio/adc/ad7606.c
> @@ -69,19 +69,17 @@ static int ad7606_reg_access(struct iio_dev *indio_dev,
>  	struct ad7606_state *st = iio_priv(indio_dev);
>  	int ret;
>  
> -	mutex_lock(&st->lock);
> +	guard(mutex)(&st->lock);
> +
>  	if (readval) {
>  		ret = st->bops->reg_read(st, reg);
>  		if (ret < 0)
> -			goto err_unlock;
> +			return ret;
>  		*readval = ret;
> -		ret = 0;
> +		return 0;
>  	} else {
> -		ret = st->bops->reg_write(st, reg, writeval);
> +		return st->bops->reg_write(st, reg, writeval);
>  	}
> -err_unlock:
> -	mutex_unlock(&st->lock);
> -	return ret;
>  }
>  
>  static int ad7606_read_samples(struct ad7606_state *st)
> @@ -124,18 +122,18 @@ static irqreturn_t ad7606_trigger_handler(int irq, void *p)
>  	struct ad7606_state *st = iio_priv(indio_dev);
>  	int ret;
>  
> -	mutex_lock(&st->lock);

Why not simply a guard(mutex)(&st->lock) ?

Then we avoid the somewhat nasty label in an nested block of code.

> +	scoped_guard(mutex, &st->lock) {
> +		ret = ad7606_read_samples(st);
> +		if (ret)
> +			goto error_ret;
>  
> -	ret = ad7606_read_samples(st);
> -	if (ret == 0)
>  		iio_push_to_buffers_with_timestamp(indio_dev, st->data,
>  						   iio_get_time_ns(indio_dev));
> -
> -	iio_trigger_notify_done(indio_dev->trig);
> -	/* The rising edge of the CONVST signal starts a new conversion. */
> -	gpiod_set_value(st->gpio_convst, 1);
> -
> -	mutex_unlock(&st->lock);
> +error_ret:
> +		iio_trigger_notify_done(indio_dev->trig);
> +		/* The rising edge of the CONVST signal starts a new conversion. */
> +		gpiod_set_value(st->gpio_convst, 1);
> +	}
>  
>  	return IRQ_HANDLED;
>  }
> @@ -259,17 +257,15 @@ static int ad7606_write_raw(struct iio_dev *indio_dev,
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_SCALE:
> -		mutex_lock(&st->lock);
> -		i = find_closest(val2, st->scale_avail, st->num_scales);
> -		if (st->sw_mode_en)
> -			ch = chan->address;
> -		ret = st->write_scale(indio_dev, ch, i);
> -		if (ret < 0) {
> -			mutex_unlock(&st->lock);
> -			return ret;
> +		scoped_guard(mutex, &st->lock) {

The mutex is grabbed in all paths that actually do anything.
Pull it out of the switch and use
guard(mutex)(&st->lock);

That will reduce the changes needed and give the same cleanups.
I doubt we care about potentially slowing down the path that returns an error
as we are writing something unwriteable.

> +			i = find_closest(val2, st->scale_avail, st->num_scales);
> +			if (st->sw_mode_en)
> +				ch = chan->address;
> +			ret = st->write_scale(indio_dev, ch, i);
> +			if (ret < 0)
> +				return ret;
> +			st->range[ch] = i;
>  		}
> -		st->range[ch] = i;
> -		mutex_unlock(&st->lock);
>  
>  		return 0;
>  	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> @@ -277,14 +273,12 @@ static int ad7606_write_raw(struct iio_dev *indio_dev,
>  			return -EINVAL;
>  		i = find_closest(val, st->oversampling_avail,
>  				 st->num_os_ratios);
> -		mutex_lock(&st->lock);
> -		ret = st->write_os(indio_dev, i);
> -		if (ret < 0) {
> -			mutex_unlock(&st->lock);
> -			return ret;
> +		scoped_guard(mutex, &st->lock) {
> +			ret = st->write_os(indio_dev, i);
> +			if (ret < 0)
> +				return ret;
> +			st->oversampling = st->oversampling_avail[i];
>  		}
> -		st->oversampling = st->oversampling_avail[i];
> -		mutex_unlock(&st->lock);
>  
>  		return 0;
>  	default:
> 


  reply	other threads:[~2024-06-29 15:37 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-28 14:48 [PATCH v2 00/10] iio: adc: ad7606: Improvements Guillaume Stols
2024-06-28 14:48 ` [PATCH v2 01/10] dt-bindings: iio: adc: adi,ad7606: add missing datasheet link Guillaume Stols
2024-06-29 15:18   ` Jonathan Cameron
2024-06-28 14:48 ` [PATCH v2 02/10] dt-bindings: iio: adc: adi,ad7606: comment and sort the compatible names Guillaume Stols
2024-06-29 15:22   ` Jonathan Cameron
2024-06-30 14:00     ` Conor Dooley
2024-06-28 14:48 ` [PATCH v2 03/10] dt-bindings: iio: adc: adi,ad7606: normalize textwidth Guillaume Stols
2024-07-01 17:13   ` Rob Herring (Arm)
2024-06-28 14:48 ` [PATCH v2 04/10] dt-bindings: iio: adc: adi,ad7606: improve descriptions Guillaume Stols
2024-06-29 15:29   ` Jonathan Cameron
2024-07-01 17:19   ` Rob Herring (Arm)
2024-06-28 14:48 ` [PATCH v2 05/10] dt-bindings: iio: adc: adi,ad7606: add supply properties Guillaume Stols
2024-06-29 15:30   ` Jonathan Cameron
2024-06-28 14:48 ` [PATCH v2 06/10] dt-bindings: iio: adc: adi,ad7606: fix example Guillaume Stols
2024-06-28 14:48 ` [PATCH v2 07/10] dt-bindings: iio: adc: adi,ad7606: add conditions Guillaume Stols
2024-06-28 15:15   ` Conor Dooley
2024-06-28 14:48 ` [PATCH v2 08/10] iio: adc: ad7606: fix oversampling gpio array Guillaume Stols
2024-06-28 14:48 ` [PATCH v2 09/10] iio: adc: ad7606: fix standby gpio state to match the documentation Guillaume Stols
2024-06-28 14:48 ` [PATCH v2 10/10] iio: adc: ad7606: switch mutexes to scoped_guard Guillaume Stols
2024-06-29 15:36   ` Jonathan Cameron [this message]
2024-06-28 15:53 ` [PATCH v2 00/10] iio: adc: ad7606: Improvements Conor Dooley
2024-06-28 15:55   ` Conor Dooley
2024-06-29 15:38     ` Jonathan Cameron

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=20240629163659.322954c3@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=beniamin.bia@analog.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gstols@baylibre.com \
    --cc=jstephan@baylibre.com \
    --cc=krzk+dt@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=stefan.popa@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;
as well as URLs for NNTP newsgroup(s).