mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Amir Atef via B4 Relay
	<devnull+amiratefabdallah1122.gmail.com@kernel.org>
Cc: amiratefabdallah1122@gmail.com,
	Lars-Peter Clausen <lars@metafoo.de>,
	Michael Hennerich <Michael.Hennerich@analog.com>,
	David Lechner <dlechner@baylibre.com>,
	Nuno Sa <nuno.sa@analog.com>, Andy Shevchenko <andy@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-iio@vger.kernel.org, linux-staging@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] staging: iio: frequency: ad9834: use guard(mutex)
Date: Sat, 17 Oct 2026 20:54:54 +0100	[thread overview]
Message-ID: <20261017205454.0dda6c10@jic23-hlaptop> (raw)
In-Reply-To: <179219435216.322.13409717511306957730.20261017-ad9834-guard-mutex-v2@gmail.com>

On Sat, 17 Oct 2026 02:45:52 +0300
Amir Atef via B4 Relay <devnull+amiratefabdallah1122.gmail.com@kernel.org> wrote:

> From: Amir Atef <amiratefabdallah1122@gmail.com>
> 
> Use guard(mutex) to manage the state lock in ad9834_write() and
> ad9834_store_wavetype(). This simplifies error handling by allowing
> early returns instead of setting the return code and breaking out of the
> switch statement to manually unlock the mutex.
> 
> Signed-off-by: Amir Atef <amiratefabdallah1122@gmail.com>
Hi Amir,

One small additional thing inline.

Thanks,

Jonathan

> ---
> Changes in v2:
> - Add blank line after guard(mutex) in ad9834_write().
> - Move 'int ret;' to the end of the variable declaration block in
>   ad9834_store_wavetype().
> - Drop redundant 'else' in ad9834_store_wavetype() case 1 by inverting
>   the condition (kept 'else' in ad9834_write() and case 0 where they are
>   part of if-else-if-else chains).
> - Replace ternary return with explicit 'if (ret) return ret;' in
>   ad9834_store_wavetype().
> - Link to v1: https://lore.kernel.org/r/179214943270.333.5921549521929682753.20261016-ad9834-guard-mutex@gmail.com
> 
>  drivers/staging/iio/frequency/ad9834.c | 50 ++++++++++++--------------
>  1 file changed, 22 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/staging/iio/frequency/ad9834.c b/drivers/staging/iio/frequency/ad9834.c
> index 0d20f796e0f0..c45bb388c998 100644
> --- a/drivers/staging/iio/frequency/ad9834.c
> +++ b/drivers/staging/iio/frequency/ad9834.c
> @@ -6,6 +6,7 @@
>   */
>  
>  #include <linux/bits.h>
> +#include <linux/cleanup.h>
>  #include <linux/clk.h>
>  #include <linux/dev_printk.h>
>  #include <linux/err.h>
> @@ -154,7 +155,8 @@ static ssize_t ad9834_write(struct device *dev,
>  	if (ret)
>  		return ret;
>  
> -	mutex_lock(&st->lock);
> +	guard(mutex)(&st->lock);
> +
>  	switch ((u32)this_attr->address) {
>  	case AD9834_REG_FREQ0:
>  	case AD9834_REG_FREQ1:
> @@ -165,10 +167,8 @@ static ssize_t ad9834_write(struct device *dev,
>  		ret = ad9834_write_phase(st, this_attr->address, val);
>  		break;
>  	case AD9834_OPBITEN:
> -		if (st->control & AD9834_MODE) {
> -			ret = -EINVAL;  /* AD9834 reserved mode */
> -			break;
> -		}
> +		if (st->control & AD9834_MODE)
> +			return -EINVAL;  /* AD9834 reserved mode */
>  
>  		if (val)
>  			st->control |= AD9834_OPBITEN;
> @@ -194,8 +194,7 @@ static ssize_t ad9834_write(struct device *dev,
>  			st->control |= this_attr->address;
>  			st->control &= ~AD9834_PIN_SW;
>  		} else {
> -			ret = -EINVAL;
> -			break;
> +			return -EINVAL;
>  		}
>  		st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
>  		ret = spi_sync(st->spi, &st->msg);
> @@ -210,9 +209,8 @@ static ssize_t ad9834_write(struct device *dev,
>  		ret = spi_sync(st->spi, &st->msg);

I'd rather see all error handled at point where they originate, rather
than only some (with special case for the last call) so here and similar places, do 
		if (ret)
			return ret;

		break;

Or you could even do a return len; here if you prefer.

>  		break;
>  	default:
> -		ret = -ENODEV;
> +		return -ENODEV;
>  	}
> -	mutex_unlock(&st->lock);
>  
>  	return ret ? ret : len;
>  }


  reply	other threads:[~2026-10-17 19:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16 23:45 Amir Atef via B4 Relay
2026-10-17 19:54 ` Jonathan Cameron [this message]
2026-10-19  7:43   ` 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=20261017205454.0dda6c10@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=Michael.Hennerich@analog.com \
    --cc=amiratefabdallah1122@gmail.com \
    --cc=andy@kernel.org \
    --cc=devnull+amiratefabdallah1122.gmail.com@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --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

all inboxes | Powered by JetHome®