Skip to contentSkip to conversation

iio: magnetometer: bmc150_magn: Used guard() for mutex protection

PATCH Superseded Neel Bullywon 2 participants 2 messages Feb 4 IIO

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: magnetometer: bmc150_magn: Used guard() for mutex protection

Replaced the manual mutex lock/unlock calls using guard() to simplify the error handling and reduce goto statement usage.

By using RAII-style cleanup, it ensures the mutex is always released when exiting the function, regardless of the return path taken

This was compiled test only

---
 drivers/iio/magnetometer/bmc150_magn.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +5 -12
index 6a73f6e2f1f0..73d9183a354b 100644
@@ -796,29 +796,22 @@ static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
 	struct bmc150_magn_data *data = iio_priv(indio_dev);
 	int ret = 0;
 
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
+
 	if (state == data->dready_trigger_on)
-		goto err_unlock;
+		return 0;
 
 	ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
 				 BMC150_MAGN_MASK_DRDY_EN,
 				 state << BMC150_MAGN_SHIFT_DRDY_EN);
 	if (ret < 0)
-		goto err_unlock;
+		return ret;
 
 	data->dready_trigger_on = state;
 
-	if (state) {
+	if (state)
 		ret = bmc150_magn_reset_intr(data);
-		if (ret < 0)
-			goto err_unlock;
-	}
-	mutex_unlock(&data->mutex);
 
-	return 0;
-
-err_unlock:
-	mutex_unlock(&data->mutex);
 	return ret;
 }
 
Signature · 1 line
2.44.0
reviewer
#1 lore raw
Quoted 18 lines · Neel Bullywon

Replaced the manual mutex lock/unlock calls using guard() to simplify the error handling and reduce goto statement usage.

By using RAII-style cleanup, it ensures the mutex is always released when exiting the function, regardless of the return path taken

This was compiled test only

---
 drivers/iio/magnetometer/bmc150_magn.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/iio/magnetometer/bmc150_magn.c +0 -0
index 6a73f6e2f1f0..73d9183a354b 100644

drivers/iio/magnetometer/bmc150_magn.c static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig, · line 797

Quoted 2 lines
drivers/iio/magnetometer/bmc150_magn.c +0 -0
@@ -796,29 +796,22 @@ static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig,
 	struct bmc150_magn_data *data = iio_priv(indio_dev);
 	int ret = 0;

We can take this a bit farther and drop the init here.

Quoted 10 lines
drivers/iio/magnetometer/bmc150_magn.c +3 -2
-	mutex_lock(&data->mutex);
+	guard(mutex)(&data->mutex);
+
 	if (state == data->dready_trigger_on)
-		goto err_unlock;
+		return 0;
 
 	ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY,
 				 BMC150_MAGN_MASK_DRDY_EN,

drivers/iio/magnetometer/bmc150_magn.c static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig, · line 813

Quoted 10 lines
drivers/iio/magnetometer/bmc150_magn.c +2 -2
 				 state << BMC150_MAGN_SHIFT_DRDY_EN);
 	if (ret < 0)
-		goto err_unlock;
+		return ret;
 
 	data->dready_trigger_on = state;
 
-	if (state) {
+	if (state)
 		ret = bmc150_magn_reset_intr(data);

return directly here

drivers/iio/magnetometer/bmc150_magn.c static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig, · line 815

Quoted 10 lines
drivers/iio/magnetometer/bmc150_magn.c +0 -8
-		if (ret < 0)
-			goto err_unlock;
-	}
-	mutex_unlock(&data->mutex);
 
-	return 0;
-
-err_unlock:
-	mutex_unlock(&data->mutex);
 	return ret;

and return 0 here.

Quoted 2 lines
drivers/iio/magnetometer/bmc150_magn.c +0 -0
 }