Skip to contentSkip to conversation

iio: temperature: tsys02d: Fix state desynchronization on resolution write error

PATCH In review Salah Triki 3 participants 3 messages Oct 5 – Oct 18 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: temperature: tsys02d: Fix state desynchronization on resolution write error

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 handling the error path first, updating dev_data->res_index only on success, and using guard(mutex) to clean up locking.

This was found through manual code review.

Fixes 53bf4d067d51 ("Add tsys02d meas-spec driver support")

---
v2 -> v3:
- Include <linux/cleanup.h>.
- Enclose case block in scope brackets.
- Add missing return 0 on success path to prevent falling through to default case.

v1 -> v2:
- Use error-first pattern (if (ret) return ret) instead of if (!ret).
- Use guard(mutex) for clean locking and simplified return path.
- Add note stating the issue was found through manual code review.
- Add proper blank lines.

 drivers/iio/temperature/tsys02d.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/iio/temperature/tsys02d.c +11 -5
index 2c67faebd90c..3dd33eb3c310 100644
@@ -12,6 +12,7 @@
 
 #include <linux/array_size.h>
 #include <linux/bits.h>
+#include <linux/cleanup.h>
 #include <linux/dev_printk.h>
 #include <linux/device.h>
 #include <linux/i2c.h>
@@ -71,19 +72,24 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
 	int i, ret;
 
 	switch (mask) {
-	case IIO_CHAN_INFO_SAMP_FREQ:
+	case IIO_CHAN_INFO_SAMP_FREQ: {
 		i = ARRAY_SIZE(tsys02d_samp_freq);
 		while (i-- > 0)
 			if (val == tsys02d_samp_freq[i])
 				break;
 		if (i < 0)
 			return -EINVAL;
-		mutex_lock(&dev_data->lock);
-		dev_data->res_index = i;
+
+		guard(mutex)(&dev_data->lock);
+
 		ret = ms_sensors_write_resolution(dev_data, i);
-		mutex_unlock(&dev_data->lock);
+		if (ret)
+			return ret;
 
-		return ret;
+		dev_data->res_index = i;
+
+		return 0;
+	}
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.43.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 8 lines · On Mon, 5 Oct 2026 14:20:02 +0100 Salah Triki <[email protected]> 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.

Quoted 8 lines

Fix this by handling the error path first, updating dev_data->res_index only on success, and using guard(mutex) to clean up locking.

This was found through manual code review.

Fixes 53bf4d067d51 ("Add tsys02d meas-spec driver support")

---

Makes sense.

Signature · 2 lines
Kind regards,
Joshua Crofts
maintainer
#2 lore raw
Quoted 16 lines · On Mon, 5 Oct 2026 16:19:50 +0200 Joshua Crofts <[email protected]>…
Quoted 13 lines · On Mon, 5 Oct 2026 14:20:02 +0100 Salah Triki <[email protected]> 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 handling the error path first, updating dev_data->res_index only on success, and using guard(mutex) to clean up locking.

This was found through manual code review.

Quoted 8 lines
Quoted 3 lines
Fixes 53bf4d067d51 ("Add tsys02d meas-spec driver support")

---

Makes sense.

I'll take this one the slow path given it is a fix for an error case we don't really expect to see.

Applied to the testing branch of iio.git.

Thanks,

Jonathan