iio: chemical: sps30: Replace manual locking with RAII locking
Subsystems (2)
PATCH v3 1/1 iio: chemical: sps30: Replace manual locking with RAII locking
Replace manual mutex_lock() and mutex_unlock() calls with the much newer guard(mutex)() and scoped_guard() macros to enable RAII patterns, modernize the driver, and to increase readability.
---
v2:
- Switch over some scoped_guard()s to guard(mutex)() per David's
suggestion.
- Remove redundant whitespace per Andy's suggestion.
- Add new wrapper sps30_do_meas() per Andy's suggestion (see commit
message).
- Add Joshua's RB
(link: https://lore.kernel.org/linux-iio/CAKqfh0FWig8mRR-xhvnfcFeSinR6RySyPaf9Gbpb6WU+diiiUQ@mail.gmail.com/T/#t)
v3:
- Remove sps30_do_meas() wrapper per Andy's suggestion.
- Remove Joshua's RB (major changes).
drivers/iio/chemical/sps30.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/iio/chemical/sps30.c +13 -16
@@ -5,6 +5,7 @@* Copyright (c) Tomasz Duszynski <[email protected]> */ +#include <linux/cleanup.h> #include <linux/crc8.h> #include <linux/delay.h> #include <linux/i2c.h>
@@ -111,9 +112,8 @@ static irqreturn_t sps30_trigger_handler(int irq, void *p)aligned_s64 ts; } scan; -mutex_lock (&state->lock); - ret =sps30_do_meas (state, scan.data,ARRAY_SIZE (scan.data)); -mutex_unlock (&state->lock); +scoped_guard (mutex, &state->lock) + ret = sps30_do_meas(state, scan.data, ARRAY_SIZE(scan.data));if (ret) goto err;
@@ -135,8 +135,8 @@ static int sps30_read_raw(struct iio_dev *indio_dev,switch (mask) {case IIO_CHAN_INFO_PROCESSED :switch (chan->type) { -case IIO_MASSCONCENTRATION : -mutex_lock (&state->lock); +case IIO_MASSCONCENTRATION : { +guard (mutex)(&state->lock);/* read up to the number of bytes actually needed */ switch (chan->channel2) {case IIO_MOD_PM1 :
@@ -152,7 +152,6 @@ static int sps30_read_raw(struct iio_dev *indio_dev, ret =sps30_do_meas (state, data,4 );break ; } -mutex_unlock (&state->lock);if (ret)return ret;
@@ -160,6 +159,7 @@ static int sps30_read_raw(struct iio_dev *indio_dev, *val2 = (data[chan->address] %100 ) *10000 ;return IIO_VAL_INT_PLUS_MICRO ; + }default :return -EINVAL ; }
@@ -197,9 +197,9 @@ static ssize_t start_cleaning_store(struct device *dev,if (kstrtoint (buf,0 , &val) || val !=1 )return -EINVAL ; -mutex_lock (&state->lock); +guard (mutex)(&state->lock); + ret = state->ops->clean_fan (state); -mutex_unlock (&state->lock);if (ret)return ret;
@@ -215,9 +215,9 @@ static ssize_t cleaning_period_show(struct device *dev,__be32 val;int ret; -mutex_lock (&state->lock); +guard (mutex)(&state->lock); + ret = state->ops->read_cleaning_period (state, &val); -mutex_unlock (&state->lock);if (ret)return ret;
@@ -238,12 +238,11 @@ static ssize_t cleaning_period_store(struct device *dev, struct device_attribute (val > SPS30_AUTO_CLEANING_PERIOD_MAX))return -EINVAL ; -mutex_lock (&state->lock); +guard (mutex)(&state->lock); + ret = state->ops->write_cleaning_period (state,cpu_to_be32 (val)); -if (ret) { -mutex_unlock (&state->lock); +if (ret)return ret; - }msleep (20 );
@@ -256,8 +255,6 @@ static ssize_t cleaning_period_store(struct device *dev, struct device_attributedev_warn (dev,"period changed but reads will return the old value\n" ); -mutex_unlock (&state->lock); -return len; }
Signature · 1 line
2.54.0
Jonathan Cameron
maintainerQuoted 15 lines · On Thu, 14 May 2026 22:13:42 -0500 Maxwell Doose <[email protected]> wrote:
Replace manual
mutex_lock()andmutex_unlock()calls with the much newer guard(mutex)() andscoped_guard()macros to enable RAII patterns, modernize the driver, and to increase readability.Signed-off-by Maxwell Doose <[email protected]>---
v2:
- Switch over some scoped_guard()s to guard(mutex)() per David's
suggestion.
- Remove redundant whitespace per Andy's suggestion.
- Add new wrappersps30_do_meas()per Andy's suggestion (see commit
message).
- Add Joshua's RB
(link: https://lore.kernel.org/linux-iio/CAKqfh0FWig8mRR-xhvnfcFeSinR6RySyPaf9Gbpb6WU+diiiUQ@mail.gmail.com/T/#t)
Quoted 2 lines
v3:
- Removesps30_do_meas()wrapper per Andy's suggestion.
I think this wasn't what Andy meant. I think he meant just push the guard into the the function you were wrapping.
Quoted 18 lines
- Remove Joshua's RB (major changes).
drivers/iio/chemical/sps30.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-)drivers/iio/chemical/sps30.c +1
-0@@ -5,6 +5,7 @@* Copyright (c) Tomasz Duszynski <[email protected]> */ +#include <linux/cleanup.h> #include <linux/crc8.h> #include <linux/delay.h> #include <linux/i2c.h>
drivers/iio/chemical/sps30.c static irqreturn_t sps30_trigger_handler(int irq, void *p) · line 116
Quoted 8 lines
drivers/iio/chemical/sps30.c +2
-3@@ -111,9 +112,8 @@ static irqreturn_t sps30_trigger_handler(int irq, void *p)aligned_s64 ts; } scan; -mutex_lock (&state->lock); - ret =sps30_do_meas (state, scan.data,ARRAY_SIZE (scan.data)); -mutex_unlock (&state->lock); +scoped_guard (mutex, &state->lock) + ret = sps30_do_meas(state, scan.data, ARRAY_SIZE(scan.data));
Every call to sps30_do_meas() is done with the lock held just over that call.
So just move the lock (as a guard) into sps30_do_meas().
I think that's what Andy was suggesting in v2 review, but maybe not!
Quoted 3 lines
drivers/iio/chemical/sps30.c +0
-0if (ret) goto err;
Quoted 19 lines · Jonathan Cameron
Quoted 15 lines · On Thu, 14 May 2026 22:13:42 -0500 Maxwell Doose <[email protected]> wrote:
Replace manual
mutex_lock()andmutex_unlock()calls with the much newer guard(mutex)() andscoped_guard()macros to enable RAII patterns, modernize the driver, and to increase readability.Signed-off-by Maxwell Doose <[email protected]>---
v2:
- Switch over some scoped_guard()s to guard(mutex)() per David's
suggestion.
- Remove redundant whitespace per Andy's suggestion.
- Add new wrappersps30_do_meas()per Andy's suggestion (see commit
message).
- Add Joshua's RB
(link: https://lore.kernel.org/linux-iio/CAKqfh0FWig8mRR-xhvnfcFeSinR6RySyPaf9Gbpb6WU+diiiUQ@mail.gmail.com/T/#t)
Quoted 5 lines
Quoted 2 lines
v3:
- Removesps30_do_meas()wrapper per Andy's suggestion.I think this wasn't what Andy meant. I think he meant just push the guard into the the function you were wrapping.
TBH I'm fairly confused on what Andy wanted in terms of "what do I wrap". If he wants me to put the guard()() into the function call itself I can do that but see the (ps) in my other email.
Quoted 28 lines
Quoted 18 lines
- Remove Joshua's RB (major changes).
drivers/iio/chemical/sps30.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-)drivers/iio/chemical/sps30.c +1
-0@@ -5,6 +5,7 @@* Copyright (c) Tomasz Duszynski <[email protected]> */ +#include <linux/cleanup.h> #include <linux/crc8.h> #include <linux/delay.h> #include <linux/i2c.h> drivers/iio/chemical/sps30.c static irqreturn_t sps30_trigger_handler(int irq, void *p) · line 116
Quoted 8 lines
drivers/iio/chemical/sps30.c +2
-3@@ -111,9 +112,8 @@ static irqreturn_t sps30_trigger_handler(int irq, void *p)aligned_s64 ts; } scan; -mutex_lock (&state->lock); - ret =sps30_do_meas (state, scan.data,ARRAY_SIZE (scan.data)); -mutex_unlock (&state->lock); +scoped_guard (mutex, &state->lock) + ret = sps30_do_meas(state, scan.data, ARRAY_SIZE(scan.data));Every call to
sps30_do_meas()is done with the lock held just over that call.
Quoted 4 lines
So just move the lock (as a guard) into
sps30_do_meas().I think that's what Andy was suggesting in v2 review, but maybe not!
Perhaps, we'll see I suppose.
best regards,
max
Quoted 4 lines
Quoted 4 lines
drivers/iio/chemical/sps30.c +0
-0if (ret) goto err;
Jonathan Cameron
maintainerQuoted 22 lines · On Fri, 15 May 2026 09:35:03 -0500 Maxwell Doose <[email protected]> wrote:
Quoted 21 lines · Jonathan Cameron
Quoted 17 lines · On Thu, 14 May 2026 22:13:42 -0500 Maxwell Doose <[email protected]> wrote:
Replace manual
mutex_lock()andmutex_unlock()calls with the much newer guard(mutex)() andscoped_guard()macros to enable RAII patterns, modernize the driver, and to increase readability.Signed-off-by Maxwell Doose <[email protected]>---
v2:
- Switch over some scoped_guard()s to guard(mutex)() per David's
suggestion.
- Remove redundant whitespace per Andy's suggestion.
- Add new wrappersps30_do_meas()per Andy's suggestion (see commit
message).
- Add Joshua's RB
(link: https://lore.kernel.org/linux-iio/CAKqfh0FWig8mRR-xhvnfcFeSinR6RySyPaf9Gbpb6WU+diiiUQ@mail.gmail.com/T/#t)v3:
- Removesps30_do_meas()wrapper per Andy's suggestion.
Quoted 7 lines
Quoted 3 lines
I think this wasn't what Andy meant. I think he meant just push the guard into the the function you were wrapping.
TBH I'm fairly confused on what Andy wanted in terms of "what do I wrap". If he wants me to put the guard()() into the function call itself I can do that but see the (ps) in my other email.
I guess you mean:
Andy:
"Ah, I was under impression that this is (unlocked version) is used somewhere else. Since it's not the case, the wrapper is not needed, just use guard()() in the original code."
Could be read either way but I think he'll be fine with it in sps30_do_meas() even if the other direction was what he was thinking of.
J
Quoted 45 lines
Quoted 29 lines
Quoted 19 lines
- Remove Joshua's RB (major changes).
drivers/iio/chemical/sps30.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-)drivers/iio/chemical/sps30.c +1
-0@@ -5,6 +5,7 @@* Copyright (c) Tomasz Duszynski <[email protected]> */ +#include <linux/cleanup.h> #include <linux/crc8.h> #include <linux/delay.h> #include <linux/i2c.h> drivers/iio/chemical/sps30.c static irqreturn_t sps30_trigger_handler(int irq, void *p) · line 116
Quoted 8 lines
drivers/iio/chemical/sps30.c +2
-3@@ -111,9 +112,8 @@ static irqreturn_t sps30_trigger_handler(int irq, void *p)aligned_s64 ts; } scan; -mutex_lock (&state->lock); - ret =sps30_do_meas (state, scan.data,ARRAY_SIZE (scan.data)); -mutex_unlock (&state->lock); +scoped_guard (mutex, &state->lock) + ret = sps30_do_meas(state, scan.data, ARRAY_SIZE(scan.data));Every call to
sps30_do_meas()is done with the lock held just over that call.Quoted 4 lines
So just move the lock (as a guard) into
sps30_do_meas().I think that's what Andy was suggesting in v2 review, but maybe not!
Perhaps, we'll see I suppose.
best regards,
maxQuoted 4 lines
Quoted 4 lines
drivers/iio/chemical/sps30.c +0
-0if (ret) goto err;
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Fri, 15 May 2026 09:35:03 -0500 Maxwell Doose <[email protected]> wrote:
On Fri, May 15, 2026 at 9:18 AM Jonathan Cameron <[email protected]> wrote:
...
Quoted 4 lines
Quoted 3 lines
TBH I'm fairly confused on what Andy wanted in terms of "what do I wrap". If he wants me to put the guard()() into the function call itself I can do that but see the (ps) in my other email.
Quoted 8 lines
I guess you mean:
Andy:
"Ah, I was under impression that this is (unlocked version) is used somewhere else. Since it's not the case, the wrapper is not needed, just use guard()() in the original code."Could be read either way but I think he'll be fine with it in
sps30_do_meas()even if the other direction was what he was thinking of.
Sorry for the confusion. Either just put a guard()() into the original function (whatever its name is) or into a wrapper. I.o.w. as Jonathan said I'm fine with both approaches.
Signature · 2 lines
With Best Regards, Andy Shevchenko