iio: light: ltr501: Use automatic cleanup of locks
PATCH 1/1 iio: light: ltr501: Use automatic cleanup of locks
From: "Rafael B. Sales" <[email protected]>
Replace mutex_lock and mutex_unlock with guard(mutex) and scoped_guard(mutex)` to reduce boilerplate and allow for easy-to-read code blocks.
--- drivers/iio/light/ltr501.c | 44 +++++++++++++------------------------- 1 file changed, 15 insertions(+), 29 deletions(-)
drivers/iio/light/ltr501.c +15 -29
@@ -242,9 +242,8 @@ static int ltr501_als_write_samp_freq(struct ltr501_data *data,if (i <0 )return i; -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als); ret =regmap_field_write (data->reg_als_rate, i); -mutex_unlock (&data->lock_als);return ret; }
@@ -261,9 +260,8 @@ static int ltr501_ps_write_samp_freq(struct ltr501_data *data,if (i <0 )return i; -mutex_lock (&data->lock_ps); +guard (mutex)(&data->lock_ps); ret =regmap_field_write (data->reg_ps_rate, i); -mutex_unlock (&data->lock_ps);return ret; }
@@ -487,9 +485,8 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,if (new_val <0 || new_val >0x0f )return -EINVAL ; -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als); ret =regmap_field_write (data->reg_als_prst, new_val); -mutex_unlock (&data->lock_als);if (ret >=0 ) data->als_period = period;
@@ -507,9 +504,8 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,if (new_val <0 || new_val >0x0f )return -EINVAL ; -mutex_lock (&data->lock_ps); +guard (mutex)(&data->lock_ps); ret =regmap_field_write (data->reg_ps_prst, new_val); -mutex_unlock (&data->lock_ps);if (ret >=0 ) data->ps_period = period;
@@ -651,18 +647,16 @@ static int ltr501_read_info_raw(struct ltr501_data *data,switch (chan->type) {case IIO_INTENSITY : -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als); ret =ltr501_read_als (data, buf); -mutex_unlock (&data->lock_als);if (ret <0 )return ret; *val =le16_to_cpu (chan->address ==LTR501_ALS_DATA1 ? buf[0 ] : buf[1 ]);return IIO_VAL_INT ;case IIO_PROXIMITY : -mutex_lock (&data->lock_ps); +guard (mutex)(&data->lock_ps); ret =ltr501_read_ps (data); -mutex_unlock (&data->lock_ps);if (ret <0 )return ret; *val = ret <R501_PS_DATA_MASK ;
@@ -687,9 +681,8 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,if (!iio_device_claim_direct (indio_dev))return -EBUSY ; -mutex_lock (&data->lock_als); - ret =ltr501_read_als (data, buf); -mutex_unlock (&data->lock_als); +scoped_guard (mutex, &data->lock_als) + ret = ltr501_read_als(data, buf);iio_device_release_direct (indio_dev);if (ret <0 )return ret;
@@ -802,9 +795,8 @@ static int __ltr501_write_raw(struct iio_dev *indio_dev,if (val !=0 )return -EINVAL ; -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als); ret =ltr501_set_it_time (data, val2); -mutex_unlock (&data->lock_als);return ret;default :return -EINVAL ;
@@ -953,18 +945,16 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,return -EINVAL ;switch (dir) {case IIO_EV_DIR_RISING : -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als); ret =regmap_bulk_write (data->regmap,LTR501_ALS_THRESH_UP , &val,2 ); -mutex_unlock (&data->lock_als);return ret;case IIO_EV_DIR_FALLING : -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als); ret =regmap_bulk_write (data->regmap,LTR501_ALS_THRESH_LOW , &val,2 ); -mutex_unlock (&data->lock_als);return ret;default :return -EINVAL ;
@@ -974,18 +964,16 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,return -EINVAL ;switch (dir) {case IIO_EV_DIR_RISING : -mutex_lock (&data->lock_ps); +guard (mutex)(&data->lock_ps); ret =regmap_bulk_write (data->regmap,LTR501_PS_THRESH_UP , &val,2 ); -mutex_unlock (&data->lock_ps);return ret;case IIO_EV_DIR_FALLING : -mutex_lock (&data->lock_ps); +guard (mutex)(&data->lock_ps); ret =regmap_bulk_write (data->regmap,LTR501_PS_THRESH_LOW , &val,2 ); -mutex_unlock (&data->lock_ps);return ret;default :return -EINVAL ;
@@ -1082,14 +1070,12 @@ static int ltr501_write_event_config(struct iio_dev *indio_dev,switch (chan->type) {case IIO_INTENSITY : -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als); ret =regmap_field_write (data->reg_als_intr, state); -mutex_unlock (&data->lock_als);return ret;case IIO_PROXIMITY : -mutex_lock (&data->lock_ps); +guard (mutex)(&data->lock_ps); ret =regmap_field_write (data->reg_ps_intr, state); -mutex_unlock (&data->lock_ps);return ret;default :return -EINVAL ;
Signature · 1 line
2.53.0
Quoted 3 lines · On Wed, Apr 15, 2026 at 05:33:30PM -0300, [email protected] wrote:
Replace
mutex_lockandmutex_unlockwithguard(mutex)and scoped_guard(mutex)` to reduce boilerplate and allow for easy-to-read code blocks.
We refer to the functions as func().
...
Review
Quoted 2 lines
(quoted diff) +1
-1- mutex_lock(&data->lock_als); + guard(mutex)(&data->lock_als);
This requires a header. But looking at the driver, please add a new patch to make header inclusion block sorted and followed IWYU principle.
drivers/iio/light/ltr501.c static int ltr501_als_write_samp_freq(struct ltr501_data *data, · line 248
Quoted 4 lines
drivers/iio/light/ltr501.c +0
-1ret =regmap_field_write (data->reg_als_rate, i); -mutex_unlock (&data->lock_als);return ret;
Now this is simple
return regmap_field_write(...);
Same comment to other similar cases below.
...
Review
Quoted 2 lines
(quoted diff) +1
-1- mutex_lock(&data->lock_als); + guard(mutex)(&data->lock_als);
This is not an equivalent. Please, care to explain why does the critical section need now additional stuff?
drivers/iio/light/ltr501.c static int ltr501_write_intr_prst(struct ltr501_data *data, · line 491
Quoted 4 lines
drivers/iio/light/ltr501.c +0
-1ret =regmap_field_write (data->reg_als_prst, new_val); -mutex_unlock (&data->lock_als);if (ret >=0 ) data->als_period = period;
Ditto for other similar cases below.
...
drivers/iio/light/ltr501.c static int ltr501_read_info_raw(struct ltr501_data *data, · line 650
Quoted 4 lines
drivers/iio/light/ltr501.c +1
-1switch (chan->type) {case IIO_INTENSITY : -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als);
I don't think you can use guard()() in this scope. Please, double check that this compiles and behaves well.
drivers/iio/light/ltr501.c static int ltr501_read_info_raw(struct ltr501_data *data, · line 655
Quoted 7 lines
drivers/iio/light/ltr501.c +0
-1ret =ltr501_read_als (data, buf); -mutex_unlock (&data->lock_als);if (ret <0 )return ret; *val =le16_to_cpu (chan->address ==LTR501_ALS_DATA1 ? buf[0 ] : buf[1 ]);
In any case, why is this now part of the critical section?
Quoted 1 line
return
IIO_VAL_INT;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines
This requires a header. But looking at the driver, please add a new patch to make header inclusion block sorted and followed IWYU principle.
Will add a separate patch to the set that ensures the driver follows IWYU.
...
Quoted 5 lines
Now this is simple
return regmap_field_write(...);Same comment to other similar cases below.
I will apply this where possible. This doesn’t work in switch cases with scoped_guard() due to implicit fall-through warnings, so those cases will need to remain unchanged.
...
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +1
-1- mutex_lock(&data->lock_als); + guard(mutex)(&data->lock_als);This is not an equivalent. Please, care to explain why does the critical section need now additional stuff?
Quoted 6 lines
drivers/iio/light/ltr501.c static int ltr501_write_intr_prst(struct ltr501_data *data, · line 491
Quoted 4 lines
drivers/iio/light/ltr501.c +0
-1ret =regmap_field_write (data->reg_als_prst, new_val); -mutex_unlock (&data->lock_als);if (ret >=0 ) data->als_period = period;Ditto for other similar cases below.
Agreed, changing the original lock boundaries like this is just bad practice.
I'll revert this on v2 keeping the original scope (ditto for similar cases below).
...
Quoted 7 lines
drivers/iio/light/ltr501.c static int ltr501_read_info_raw(struct ltr501_data *data, · line 650
Quoted 4 lines
drivers/iio/light/ltr501.c +1
-1switch (chan->type) {case IIO_INTENSITY : -mutex_lock (&data->lock_als); +guard (mutex)(&data->lock_als);I don't think you can use guard()() in this scope. Please, double check that this compiles and behaves well.
You are right that guards in switch cases can lead to undesirable behavior. I will switch to using scoped_guard() here on v2 which will address this.