Skip to contentSkip to conversation

iio: temperature: tsys02d: Use guard(mutex) in tsys02d_write_raw()

PATCH In review lauraarakaki 3 participants 4 messages Apr 21 – Apr 22 1 message not in the archive IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: temperature: tsys02d: Use guard(mutex) in tsys02d_write_raw()

Replace the manual mutex_lock()/mutex_unlock() pair in tsys02d_write_raw() with guard(mutex)() from the scope-based cleanup helpers (include/linux/cleanup.h).

The previous code stored the return value of ms_sensors_write_resolution() in a local variable solely to bridge the gap between the manual mutex_unlock() call and the return statement. Using guard(mutex)() removes the need for both the intermediate variable and the explicit unlock call, since the mutex is released automatically when the function goes out of scope.

No functional change intended.

---
 drivers/iio/temperature/tsys02d.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/iio/temperature/tsys02d.c +4 -7
index 0cad27205667..55a55f2f4663 100644
@@ -10,6 +10,7 @@
  *  http://www.meas-spec.com/downloads/Digital_Sensor_TSYS02D.pdf
  */
 
+#include <linux/cleanup.h>
 #include <linux/init.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
@@ -62,7 +63,7 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
 			     int val, int val2, long mask)
 {
 	struct ms_ht_dev *dev_data = iio_priv(indio_dev);
-	int i, ret;
+	int i;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -72,17 +73,13 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
 				break;
 		if (i < 0)
 			return -EINVAL;
-		mutex_lock(&dev_data->lock);
+		guard(mutex)(&dev_data->lock);
 		dev_data->res_index = i;
-		ret = ms_sensors_write_resolution(dev_data, i);
-		mutex_unlock(&dev_data->lock);
-
-		return ret;
+		return ms_sensors_write_resolution(dev_data, i);
 	default:
 		return -EINVAL;
 	}
 }
-
 static const struct iio_chan_spec tsys02d_channels[] = {
 	{
 		.type = IIO_TEMP,
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 4 lines · On Tue, 21 Apr 2026 12:40:01 -0300 lauraarakaki <[email protected]>…

Replace the manual mutex_lock()/mutex_unlock() pair in tsys02d_write_raw() with guard(mutex)() from the scope-based cleanup helpers (include/linux/cleanup.h).

Hi and welcome to IIO.

The wrap of this message is rather short. Normally we go for 75 characters for a commit description.

Quoted 7 lines

The previous code stored the return value of ms_sensors_write_resolution() in a local variable solely to bridge the gap between the manual mutex_unlock() call and the return statement. Using guard(mutex)() removes the need for both the intermediate variable and the explicit unlock call, since the mutex is released automatically when the function goes out of scope.

This paragraph is unnecessary detail.

Quoted 31 lines

No functional change intended.

---
 drivers/iio/temperature/tsys02d.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/iio/temperature/tsys02d.c +2 -1
index 0cad27205667..55a55f2f4663 100644
@@ -10,6 +10,7 @@
  *  http://www.meas-spec.com/downloads/Digital_Sensor_TSYS02D.pdf
  */
 
+#include <linux/cleanup.h>
 #include <linux/init.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
@@ -62,7 +63,7 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
 			     int val, int val2, long mask)
 {
 	struct ms_ht_dev *dev_data = iio_priv(indio_dev);
-	int i, ret;
+	int i;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 76

Quoted 5 lines
drivers/iio/temperature/tsys02d.c +1 -1
@@ -72,17 +73,13 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
 				break;
 		if (i < 0)
 			return -EINVAL;
-		mutex_lock(&dev_data->lock);
+		guard(mutex)(&dev_data->lock);

Unfortunately this broken because the non obvious definition of scope in switch case statements. You need to add {} in appropriate places.

See some similar threads from last few days.

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 85

Quoted 11 lines
drivers/iio/temperature/tsys02d.c +1 -5
 		dev_data->res_index = i;
-		ret = ms_sensors_write_resolution(dev_data, i);
-		mutex_unlock(&dev_data->lock);
-
-		return ret;
+		return ms_sensors_write_resolution(dev_data, i);
 	default:
 		return -EINVAL;
 	}
 }
-

Unrelated change. Always check for these and clean them up before posting.

Quoted 3 lines
drivers/iio/temperature/tsys02d.c +0 -0
 static const struct iio_chan_spec tsys02d_channels[] = {
 	{
 		.type = IIO_TEMP,

A message that is not in the archive (sent off-list or not yet received): CAJN_CScSHkrXGsF9pjWna5eYnRsPiwj47+7NbVYSVBGkwL3AJw@mail.gmail.com

#3 lore raw
Quoted 11 lines · Laura Arakaki

Hi,

Thanks for the review.

I'll address the issues:

- Fix scope in switch case by adding proper braces for guard(mutex)
- Simplify the commit message
- Remove unrelated changes

I'll send a v2 shortly.

No, please slow down. Take your time to review patches from others in this area (IIO subsystem), read documentation, et cetera.

There are too many patches now that overload the capacity of the human reviewers. Also by reviewing others' patches you will learn a lot without even submitting anything.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
In reply to lauraarakaki
Quoted 12 lines · lauraarakaki

Replace the manual mutex_lock()/mutex_unlock() pair in tsys02d_write_raw() with guard(mutex)() from the scope-based cleanup helpers (include/linux/cleanup.h).

The previous code stored the return value of ms_sensors_write_resolution() in a local variable solely to bridge the gap between the manual mutex_unlock() call and the return statement. Using guard(mutex)() removes the need for both the intermediate variable and the explicit unlock call, since the mutex is released automatically when the function goes out of scope.

Quoted 3 lines

No functional change intended.

Read documentation, you need to use real name (as in documents).

Quoted 6 lines

---

+#include <linux/cleanup.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/kernel.h>

Preferable to get a prerequisite that sorts headers alphabetically and (optionally) another patch to move to follow IWYU principle.

...

Now the most important point of this review. All of the above you may get by helping in reviewing the patches in Linux IIO mailing list (lore.kernel.org/linux-iio).

TL;DR: I will be glad to see a newcomer to participate in the reviewing first.

Signature · 2 lines
With Best Regards,
Andy Shevchenko