Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Raffael Raiel Trindade <raffaelraiel@usp.br>
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	Kim Carvalho <kim.ca@usp.br>,
	linux-iio@vger.kernel.org
Subject: Re: [PATCH v4] iio: light: vcnl4000: use lock guard()
Date: Thu, 7 May 2026 17:36:54 +0100	[thread overview]
Message-ID: <20260507173654.3c52f58f@jic23-huawei> (raw)
In-Reply-To: <20260506210616.313636-1-raffaelraiel@usp.br>

On Wed,  6 May 2026 18:05:33 -0300
Raffael Raiel Trindade <raffaelraiel@usp.br> wrote:

> From: Raffael Raiel Trindade <raffaelraiel@usp.br>
> 
> Use guard() and scoped_guard() for handling mutex lock instead of manually
> locking and unlocking. Remove gotos in error handling logic. This prevents
> forgotten locks on early exits.
> 
> Signed-off-by: Raffael Raiel Trindade <raffaelraiel@usp.br>
> Co-developed-by: Kim Carvalho <kim.ca@usp.br>
> Signed-off-by: Kim Carvalho <kim.ca@usp.br>

Hi All,

One remaining minor point and a suggestion for where there may be
some more useful cleanup to be done on this driver

Thanks,

Jonathan

> ---
> v4:
>  - use guard() and scoped_guard() instead of
>    lock() and unlock() in all functions
>  - drop ret = -EINVAL initialization
> 
> v3:
>  - fix indentation problems on line breaks
> 
> v2:
>  - remove unnecessary ret attributions on non-error logic.
>  - remove breaks on switch-case after return
> 
>  drivers/iio/light/vcnl4000.c | 148 +++++++++++------------------------
>  1 file changed, 45 insertions(+), 103 deletions(-)
> 
> diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
> index c08927e34b4e..d87a95a05329 100644
> --- a/drivers/iio/light/vcnl4000.c
> +++ b/drivers/iio/light/vcnl4000.c
> @@ -18,6 +18,7 @@
>   */
>  
>  #include <linux/bitfield.h>
> +#include <linux/cleanup.h>
>  #include <linux/delay.h>
>  #include <linux/err.h>
>  #include <linux/i2c.h>

>  
>  static int vcnl4200_set_power_state(struct vcnl4000_data *data, bool on)
> @@ -442,18 +433,18 @@ static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
>  	int tries = 20;
>  	int ret;
>  
> -	mutex_lock(&data->vcnl4000_lock);
> +	guard(mutex)(&data->vcnl4000_lock);
>  
>  	ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
>  					req_mask);
>  	if (ret < 0)
> -		goto fail;
> +		return ret;
>  
>  	/* wait for data to become ready */
>  	while (tries--) {
>  		ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
>  		if (ret < 0)
> -			goto fail;
> +			return ret;
>  		if (ret & rdy_mask)
>  			break;
>  		msleep(20); /* measurement takes up to 100 ms */
> @@ -462,20 +453,13 @@ static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
>  	if (tries < 0) {
>  		dev_err(&data->client->dev,
>  			"vcnl4000_measure() failed, data not ready\n");
> -		ret = -EIO;
> -		goto fail;
> +		return -EIO;
>  	}
>  
>  	ret = vcnl4000_read_data(data, data_reg, val);
>  	if (ret < 0)
> -		goto fail;
> -
> -	mutex_unlock(&data->vcnl4000_lock);
> -
> -	return 0;
> +		return ret;
>  
> -fail:
> -	mutex_unlock(&data->vcnl4000_lock);
>  	return ret;
	
	return 0;

Check for any other cases that have been introduced where the final return
is always success (e.g. 0)

>  }

>  static ssize_t vcnl4040_read_ps_calibbias(struct vcnl4000_data *data, int *val, int *val2)
> @@ -878,20 +834,15 @@ static ssize_t vcnl4040_write_ps_calibbias(struct vcnl4000_data *data, int val)
>  	if (i >= ARRAY_SIZE(vcnl4040_ps_calibbias_ua))
>  		return -EINVAL;
>  
> -	mutex_lock(&data->vcnl4000_lock);
> +	guard(mutex)(&data->vcnl4000_lock);
>  
>  	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF3);
>  	if (ret < 0)
> -		goto out_unlock;
> +		return ret;
>  
>  	regval = (ret & ~VCNL4040_PS_MS_LED_I);
>  	regval |= FIELD_PREP(VCNL4040_PS_MS_LED_I, i);

If you want more stuff to do on this driver, look at FIELD_MODIFY() and where
it can be usefully applied.

> -	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF3,
> -					regval);
> -
> -out_unlock:
> -	mutex_unlock(&data->vcnl4000_lock);
> -	return ret;
> +	return i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF3, regval);
>  }

      reply	other threads:[~2026-05-07 16:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-06 21:05 [PATCH v4] iio: light: vcnl4000: use lock guard() Raffael Raiel Trindade
2026-05-07 16:36 ` Jonathan Cameron [this message]

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=20260507173654.3c52f58f@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=kim.ca@usp.br \
    --cc=linux-iio@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=raffaelraiel@usp.br \
    /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