Skip to contentSkip to conversation

iio: light: pa12203001: Use guard() to simplify lock handling.

PATCH In review Jonathan Cameron 3 participants 4 messages Oct 26 – Nov 2 IIO

lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

PATCH 1/1 iio: light: pa12203001: Use guard() to simplify lock handling.

From: Jonathan Cameron <[email protected]>

It became apparent in reviewing [1] that the use of guard() would remove some ugly error paths.

---
 drivers/iio/light/pa12203001.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
drivers/iio/light/pa12203001.c +9 -20
index 3f5717fb1276..3056ce076239 100644
@@ -10,6 +10,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/acpi.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/iio/iio.h>
@@ -160,36 +161,29 @@ static int pa12203001_runtime_resume_and_get(struct pa12203001_data *data, u8 ma
 	int ret;
 
 	if (mask & PA12203001_ALS_EN_MASK) {
-		mutex_lock(&data->lock);
+		guard(mutex)(&data->lock);
 		if (data->px_enabled) {
 			ret = pa12203001_als_enable(data,
 						    PA12203001_ALS_EN_MASK);
 			if (ret < 0)
-				goto err;
+				return ret;
 		} else {
 			data->als_needs_enable = true;
 		}
-		mutex_unlock(&data->lock);
 	}
 
 	if (mask & PA12203001_PX_EN_MASK) {
-		mutex_lock(&data->lock);
+		guard(mutex)(&data->lock);
 		if (data->als_enabled) {
 			ret = pa12203001_px_enable(data, PA12203001_PX_EN_MASK);
 			if (ret < 0)
-				goto err;
+				return ret;
 		} else {
 			data->px_needs_enable = true;
 		}
-		mutex_unlock(&data->lock);
 	}
 
 	return pm_runtime_resume_and_get(&data->client->dev);
-
-err:
-	mutex_unlock(&data->lock);
-	return ret;
-
 #endif
 	return 0;
 }
@@ -313,16 +307,12 @@ static int pa12203001_power_chip(struct iio_dev *indio_dev, u8 state)
 	struct pa12203001_data *data = iio_priv(indio_dev);
 	int ret;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	ret = pa12203001_als_enable(data, state);
 	if (ret < 0)
-		goto out;
-
-	ret = pa12203001_px_enable(data, state);
+		return ret;
 
-out:
-	mutex_unlock(&data->lock);
-	return ret;
+	return pa12203001_px_enable(data, state);
 }
 
 static int pa12203001_probe(struct i2c_client *client)
@@ -421,7 +411,7 @@ static int pa12203001_runtime_resume(struct device *dev)
 
 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	if (data->als_needs_enable) {
 		pa12203001_als_enable(data, PA12203001_ALS_EN_MASK);
 		data->als_needs_enable = false;
@@ -430,7 +420,6 @@ static int pa12203001_runtime_resume(struct device *dev)
 		pa12203001_px_enable(data, PA12203001_PX_EN_MASK);
 		data->px_needs_enable = false;
 	}
-	mutex_unlock(&data->lock);
 
 	return 0;
 }
Signature · 1 line
2.56.0
#1 lore raw
Quoted 2 lines · Jonathan Cameron

It became apparent in reviewing [1] that the use of guard() would remove some ugly error paths.

...

drivers/iio/light/pa12203001.c static int pa12203001_runtime_resume_and_get(struct pa12203001_data *data, u8 ma · line 164

Quoted 3 lines
drivers/iio/light/pa12203001.c +1 -1
 	if (mask & PA12203001_ALS_EN_MASK) {
-		mutex_lock(&data->lock);
+		guard(mutex)(&data->lock);

+ Blank line. Let's make guard()() to be standalone to increase the chances to catch it on a glance.

Ditto for all similar cases.

Quoted 11 lines
drivers/iio/light/pa12203001.c +1 -2
 		if (data->px_enabled) {
 			ret = pa12203001_als_enable(data,
 						    PA12203001_ALS_EN_MASK);
 			if (ret < 0)
-				goto err;
+				return ret;
 		} else {
 			data->als_needs_enable = true;
 		}
-		mutex_unlock(&data->lock);
 	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#2 lore raw
Quoted 7 lines · On Mon, 26 Oct 2026 11:10:19 +0200 Andy Shevchenko…
Quoted 2 lines · Jonathan Cameron

It became apparent in reviewing [1] that the use of guard() would remove some ugly error paths.

...

Quoted 8 lines

drivers/iio/light/pa12203001.c static int pa12203001_runtime_resume_and_get(struct pa12203001_data *data, u8 ma · line 164

Quoted 3 lines
drivers/iio/light/pa12203001.c +1 -1
 	if (mask & PA12203001_ALS_EN_MASK) {
-		mutex_lock(&data->lock);
+		guard(mutex)(&data->lock);  

+ Blank line. Let's make guard()() to be standalone to increase the chances to catch it on a glance.

Ditto for all similar cases.

Tweaked and applied

Quoted 13 lines
Quoted 12 lines
drivers/iio/light/pa12203001.c +1 -2
 		if (data->px_enabled) {
 			ret = pa12203001_als_enable(data,
 						    PA12203001_ALS_EN_MASK);
 			if (ret < 0)
-				goto err;
+				return ret;
 		} else {
 			data->als_needs_enable = true;
 		}
-		mutex_unlock(&data->lock);
 	}