[PATCH] iio: temperature: tsys02d: Fix state desynchronization on resolution write error

Salah Triki posted 1 patch 2 weeks, 4 days ago
There is a newer version of this series
drivers/iio/temperature/tsys02d.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] iio: temperature: tsys02d: Fix state desynchronization on resolution write error
Posted by Salah Triki 2 weeks, 4 days ago
In tsys02d_write_raw(), dev_data->res_index is updated before checking
the return value of ms_sensors_write_resolution(). If the hardware I2C
write operation fails, dev_data->res_index remains updated with the
new index despite the physical sensor remaining in its previous state.

This leads to a state desynchronization where subsequent reads via
tsys02d_read_raw() return an incorrect sampling frequency.

Fix this by performing the I2C write first and updating dev_data->res_index
only if ms_sensors_write_resolution() succeeds.

Fixes: 53bf4d067d51 ("Add tsys02d meas-spec driver support")
Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
 drivers/iio/temperature/tsys02d.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/temperature/tsys02d.c b/drivers/iio/temperature/tsys02d.c
index 3ef72347456e..f50426beacdd 100644
--- a/drivers/iio/temperature/tsys02d.c
+++ b/drivers/iio/temperature/tsys02d.c
@@ -73,8 +73,9 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
 		if (i < 0)
 			return -EINVAL;
 		mutex_lock(&dev_data->lock);
-		dev_data->res_index = i;
 		ret = ms_sensors_write_resolution(dev_data, i);
+		if (!ret)
+			dev_data->res_index = i;
 		mutex_unlock(&dev_data->lock);
 
 		return ret;
-- 
2.43.0
Re: [PATCH] iio: temperature: tsys02d: Fix state desynchronization on resolution write error
Posted by Joshua Crofts 2 weeks, 4 days ago
On Mon,  7 Sep 2026 09:53:53 +0100
Salah Triki <salah.triki@gmail.com> wrote:

> In tsys02d_write_raw(), dev_data->res_index is updated before checking
> the return value of ms_sensors_write_resolution(). If the hardware I2C
> write operation fails, dev_data->res_index remains updated with the
> new index despite the physical sensor remaining in its previous state.
> 
> This leads to a state desynchronization where subsequent reads via
> tsys02d_read_raw() return an incorrect sampling frequency.
> 
> Fix this by performing the I2C write first and updating dev_data->res_index
> only if ms_sensors_write_resolution() succeeds.
> 
> Fixes: 53bf4d067d51 ("Add tsys02d meas-spec driver support")
> Signed-off-by: Salah Triki <salah.triki@gmail.com>

Was this found using an LLM or manually?

> ---
>  drivers/iio/temperature/tsys02d.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/temperature/tsys02d.c b/drivers/iio/temperature/tsys02d.c
> index 3ef72347456e..f50426beacdd 100644
> --- a/drivers/iio/temperature/tsys02d.c
> +++ b/drivers/iio/temperature/tsys02d.c
> @@ -73,8 +73,9 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
>  		if (i < 0)
>  			return -EINVAL;
>  		mutex_lock(&dev_data->lock);
> -		dev_data->res_index = i;

+ blank line

>  		ret = ms_sensors_write_resolution(dev_data, i);
> +		if (!ret)
> +			dev_data->res_index = i;

+ blank line

>  		mutex_unlock(&dev_data->lock);
>  
>  		return ret;

Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>

-- 
Kind regards,
Joshua Crofts
Re: [PATCH] iio: temperature: tsys02d: Fix state desynchronization on resolution write error
Posted by Andy Shevchenko 2 weeks, 3 days ago
On Mon, Sep 07, 2026 at 04:50:09PM +0200, Joshua Crofts wrote:
> On Mon,  7 Sep 2026 09:53:53 +0100
> Salah Triki <salah.triki@gmail.com> wrote:

...

> >  		ret = ms_sensors_write_resolution(dev_data, i);
> > +		if (!ret)
> > +			dev_data->res_index = i;
> 
> + blank line
> 
> >  		mutex_unlock(&dev_data->lock);
> >  
> >  		return ret;
> 
> Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>

Actually we prefer usual pattern — "check for error first".
This 'if (!ret)' is confusing.

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH] iio: temperature: tsys02d: Fix state desynchronization on resolution write error
Posted by Jonathan Cameron 1 week, 5 days ago
On Tue, 8 Sep 2026 13:32:44 +0300
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Mon, Sep 07, 2026 at 04:50:09PM +0200, Joshua Crofts wrote:
> > On Mon,  7 Sep 2026 09:53:53 +0100
> > Salah Triki <salah.triki@gmail.com> wrote:  
> 
> ...
> 
> > >  		ret = ms_sensors_write_resolution(dev_data, i);
> > > +		if (!ret)
> > > +			dev_data->res_index = i;  
> > 
> > + blank line
> >   
> > >  		mutex_unlock(&dev_data->lock);
> > >  
> > >  		return ret;  
> > 
> > Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>  
> 
> Actually we prefer usual pattern — "check for error first".
> This 'if (!ret)' is confusing.
> 

I'd use a guard so we can just return when the error is seen.