iio: magnetometer: rm3100: Modernize locking and control flow
Subsystems (2)
cover iio: magnetometer: rm3100: Modernize locking and control flow
The goal of this patch series is to replace the manual mutex_lock() and mutex_unlock() calls in rm3100-core.c with their more modern counterparts, guard(mutex)() and scoped_guard(). I've also done some minor cleanups, removing what are now redundant gotos, and enabling direct returns.
Following feedback on recent commits, I've learned that it's a better idea to split changes to be more atomic, and I've done that here in case any particular change *somehow* causes a build regression. The changes have been test-compiled however, and according to make, smatch, and sparse, should be sound.
=== Changelog ===
v2:
- Added small style fixes per Andy's suggestions (Adding blank lines, moving an if statement in a scoped_guard block).
- Switched out scoped_guard() for guard(mutex)() in certain commits.
- Fixed error in commit 4 where deadlocks could occur, as goto ignores __attribute__((cleanup)). This has been fixed by the above.
Maxwell Doose (4):
iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_read_mag()
iio: magnetometer: rm3100: Use scoped_guard() in
rm3100_get_samp_freq()
iio: magnetometer: rm3100: Use guard(mutex)() in
rm3100_set_samp_freq()
iio: magnetometer: rm3100: Use guard(mutex)() in
rm3100_trigger_handler()drivers/iio/magnetometer/rm3100-core.c | 63 ++++++++++---------------- 1 file changed, 24 insertions(+), 39 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/4 iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_read_mag()
Replace mutex_lock() and mutex_unlock() calls in rm3100_read_mag() with the more modern guard(mutex)(). This will help modernize the driver and bring it up-to-date with modern available macros/functions.
While at it, remove the now unnecessary "unlock_return" goto and directly return in if statements.
---
v2:
- Switch out scoped_guard() for guard(mutex)() per Andy.
drivers/iio/magnetometer/rm3100-core.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/iio/magnetometer/rm3100-core.c +5 -9
@@ -204,27 +204,23 @@ static int rm3100_read_mag(struct rm3100_data *data, int idx, int *val)u8 buffer[3 ];int ret; -mutex_lock (&data->lock); +guard (mutex)(&data->lock); + ret =regmap_write (regmap,RM3100_REG_POLL ,BIT (4 + idx));if (ret <0 ) - goto unlock_return; +return ret; ret =rm3100_wait_measurement (data);if (ret <0 ) - goto unlock_return; +return ret; ret =regmap_bulk_read (regmap,RM3100_REG_MX2 +3 * idx, buffer,3 );if (ret <0 ) - goto unlock_return; -mutex_unlock (&data->lock); +return ret; *val =sign_extend32 (get_unaligned_be24 (&buffer[0 ]),23 );return IIO_VAL_INT ; - -unlock_return: -mutex_unlock (&data->lock); -return ret; }#define RM3100_CHANNEL (axis, idx) \
Signature · 1 line
2.53.0
Re: [PATCH v2 1/4] iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_read_mag()
Quoted 6 lines · Maxwell Doose
Replace
mutex_lock()andmutex_unlock()calls inrm3100_read_mag()with the more modern guard(mutex)(). This will help modernize the driver and bring it up-to-date with modern available macros/functions.While at it, remove the now unnecessary "
unlock_return" goto and directly return in if statements.
Perhaps we need to also put a summary of the extended critical section, so that it will be clear that we thought about it and decided to cover that small memory access + some bitops.
Otherwise LGTM.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/4] iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_read_mag()
Quoted 7 lines · Andy Shevchenko
Perhaps we need to also put a summary of the extended critical section, so that it will be clear that we thought about it and decided to cover that small memory access + some bitops.
Otherwise LGTM.
Sounds good, but I'm going to wait 1 or 2 days to see if a maintainer wants to comment on whether or not we *need* the summary about the mutex scope. If they don't say anything I'll probably just send a v3 with the summary added in the commit message.
best regards,
maxwell
Jonathan Cameron
maintainerRe: [PATCH v2 1/4] iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_read_mag()
Quoted 7 lines · On Tue, 28 Apr 2026 09:59:55 -0500 Maxwell Doose <[email protected]> wrote:
Quoted 5 lines · Andy Shevchenko
Perhaps we need to also put a summary of the extended critical section, so that it will be clear that we thought about it and decided to cover that small memory access + some bitops.
Quoted 7 lines
Quoted 2 lines
Otherwise LGTM.
Sounds good, but I'm going to wait 1 or 2 days to see if a maintainer wants to comment on whether or not we *need* the summary about the mutex scope. If they don't say anything I'll probably just send a v3 with the summary added in the commit message.
Where scope changes, it is indeed good to add a brief note on what ends up incorporated in the expanded scope and whether that is worth thinking about or not when reviewing the patch.
Here I think it's easy to conclude we don't care. The last patch not so much!
J
Quoted 3 lines
best regards,
maxwell
PATCH v2 2/4 iio: magnetometer: rm3100: Use scoped_guard() in rm3100_get_samp_freq()
In reply to Maxwell DooseReplace mutex_lock() and mutex_unlock() calls in rm3100_get_samp_freq() with the more modern scoped_guard(). This will help modernize the driver and bring it up-to-date with modern available macros/functions.
---
v2:
- Move if statement into scoped_guard body per Andy.
drivers/iio/magnetometer/rm3100-core.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/iio/magnetometer/rm3100-core.c +6 -5
@@ -280,11 +280,12 @@ static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)unsigned int tmp;int ret; -mutex_lock (&data->lock); - ret =regmap_read (data->regmap,RM3100_REG_TMRC , &tmp); -mutex_unlock (&data->lock); -if (ret <0 ) -return ret; + scoped_guard(mutex, &data->lock) { + ret =regmap_read (data->regmap,RM3100_REG_TMRC , &tmp); + +if (ret <0 ) +return ret; + } *val = rm3100_samp_rates[tmp -RM3100_TMRC_OFFSET ][0 ]; *val2 = rm3100_samp_rates[tmp -RM3100_TMRC_OFFSET ][1 ];
Signature · 1 line
2.53.0
Re: [PATCH v2 2/4] iio: magnetometer: rm3100: Use scoped_guard() in rm3100_get_samp_freq()
Quoted 3 lines · Maxwell Doose
Replace
mutex_lock()andmutex_unlock()calls inrm3100_get_samp_freq()with the more modernscoped_guard(). This will help modernize the driver and bring it up-to-date with modern available macros/functions.
...
drivers/iio/magnetometer/rm3100-core.c static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)
Quoted 9 lines
drivers/iio/magnetometer/rm3100-core.c +3
-5-mutex_lock (&data->lock); - ret =regmap_read (data->regmap,RM3100_REG_TMRC , &tmp); -mutex_unlock (&data->lock); -if (ret <0 ) -return ret; + scoped_guard(mutex, &data->lock) { + ret =regmap_read (data->regmap,RM3100_REG_TMRC , &tmp); +
Redundant blank line as ret is defined elsewhere.
Quoted 3 lines
drivers/iio/magnetometer/rm3100-core.c +3
-0+if (ret <0 ) +return ret; + }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/4] iio: magnetometer: rm3100: Use scoped_guard() in rm3100_get_samp_freq()
Quoted 11 lines · Andy Shevchenko
drivers/iio/magnetometer/rm3100-core.c static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)
Quoted 9 lines
drivers/iio/magnetometer/rm3100-core.c +3
-5-mutex_lock (&data->lock); - ret =regmap_read (data->regmap,RM3100_REG_TMRC , &tmp); -mutex_unlock (&data->lock); -if (ret <0 ) -return ret; + scoped_guard(mutex, &data->lock) { + ret =regmap_read (data->regmap,RM3100_REG_TMRC , &tmp); +Redundant blank line as ret is defined elsewhere.
Shoot. I'm away right now but once I get back I can fix that.
best regards,
maxwell
Jonathan Cameron
maintainerRe: [PATCH v2 2/4] iio: magnetometer: rm3100: Use scoped_guard() in rm3100_get_samp_freq()
Quoted 11 lines · On Tue, 28 Apr 2026 08:59:48 -0500 Maxwell Doose <[email protected]> wrote:
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines
drivers/iio/magnetometer/rm3100-core.c +2
-5-mutex_lock (&data->lock); - ret =regmap_read (data->regmap,RM3100_REG_TMRC , &tmp); -mutex_unlock (&data->lock); -if (ret <0 ) -return ret; + scoped_guard(mutex, &data->lock) { + ret =regmap_read (data->regmap,RM3100_REG_TMRC , &tmp);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
+
Redundant blank line as ret is defined elsewhere.
Shoot. I'm away right now but once I get back I can fix that.
Given the function is:
static int rm3100_get_samp_freq(struct rm3100_data *data, int *val, int *val2)
{
unsigned int tmp;
int ret;
mutex_lock(&data->lock);
ret = regmap_read(data->regmap, RM3100_REG_TMRC, &tmp);
mutex_unlock(&data->lock);
if (ret < 0)
return ret;
*val = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][0];
*val2 = rm3100_samp_rates[tmp - RM3100_TMRC_OFFSET][1];
return IIO_VAL_INT_PLUS_MICRO;
}I'd just use a guard and not worry about two value lookups from static const data being under the guard()
Why is this series broken up into lots of patches doing same type of change in different places? This could definitely be combined with the previous patch.
Thanks,
Jonathan
Quoted 3 lines
best regards,
maxwell
Re: [PATCH v2 2/4] iio: magnetometer: rm3100: Use scoped_guard() in rm3100_get_samp_freq()
Quoted 3 lines · Jonathan Cameron
I'd just use a guard and not worry about two value lookups from static const data being under the
guard()
We'd probably want to keep the scope as close as possible to the original code, but it's your call, I can use guard(mutex)() instead.
Quoted 2 lines
Why is this series broken up into lots of patches doing same type of change in different places? This could definitely be combined with the previous patch.
I've had some suggestions on other patches to split some changes, and thus, I've been erring on the side of atomicity. I can see now how this probably should be combined, and I'll merge these for a v3 and add the suggested fixes for when I have free time.
best regards,
maxwell
PATCH v2 3/4 iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_set_samp_freq()
In reply to Maxwell DooseReplace mutex_lock() and mutex_unlock() calls in rm3100_set_samp_freq with the more modern guard(mutex)(). This will help modernize the driver and bring it up-to-date with modern available macros/functions.
While at it, remove unlock_return goto and use direct returns instead.
---
v2:
- Added blank line below guard(mutex)() per Andy.
drivers/iio/magnetometer/rm3100-core.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-)
drivers/iio/magnetometer/rm3100-core.c +10 -16
@@ -335,56 +335,50 @@ static int rm3100_set_samp_freq(struct iio_dev *indio_dev, int val, int val2)int ret;int i; -mutex_lock (&data->lock); +guard (mutex)(&data->lock); +/* All cycle count registers use the same value. */ ret =regmap_read (regmap,RM3100_REG_CC_X , &cycle_count);if (ret <0 ) - goto unlock_return; +return ret;for (i =0 ; i <RM3100_SAMP_NUM ; i++) {if (val == rm3100_samp_rates[i][0 ] && val2 == rm3100_samp_rates[i][1 ])break ; } -if (i ==RM3100_SAMP_NUM ) { - ret = -EINVAL ; - goto unlock_return; - } +if (i ==RM3100_SAMP_NUM ) +return -EINVAL ; ret =regmap_write (regmap,RM3100_REG_TMRC , i +RM3100_TMRC_OFFSET );if (ret <0 ) - goto unlock_return; +return ret;/* Checking if cycle count registers need changing. */ if (val ==600 && cycle_count ==200 ) { ret =rm3100_set_cycle_count (data,100 );if (ret <0 ) - goto unlock_return; +return ret; }else if (val !=600 && cycle_count ==100 ) { ret =rm3100_set_cycle_count (data,200 );if (ret <0 ) - goto unlock_return; +return ret; }if (iio_buffer_enabled (indio_dev)) {/* Writing TMRC registers requires CMM reset. */ ret =regmap_write (regmap,RM3100_REG_CMM ,0 );if (ret <0 ) - goto unlock_return; +return ret; ret =regmap_write (data->regmap,RM3100_REG_CMM , (*indio_dev->active_scan_mask &0x7 ) <<RM3100_CMM_AXIS_SHIFT |RM3100_CMM_START );if (ret <0 ) - goto unlock_return; +return ret; } -mutex_unlock (&data->lock); data->conversion_time = rm3100_samp_rates[i][2 ] *2 ;return 0 ; - -unlock_return: -mutex_unlock (&data->lock); -return ret; } static int rm3100_read_raw(struct iio_dev *indio_dev,
Signature · 1 line
2.53.0
PATCH v2 4/4 iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_trigger_handler()
In reply to Maxwell DooseReplace mutex_lock() and mutex_unlock() calls in rm3100_trigger_handler() with the more modern guard(mutex)(). This will help modernize the driver and bring it up-to-date with modern available macros/functions.
---
v2:
- Switched out scoped_guard() for guard(mutex)().
- Because of the replacement of scoped_guard(), fixed an error due to gotos bypassing __attribute__((cleanup)).
drivers/iio/magnetometer/rm3100-core.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/iio/magnetometer/rm3100-core.c +3 -9
@@ -459,11 +459,11 @@ static irqreturn_t rm3100_trigger_handler(int irq, void *p)struct regmap *regmap = data->regmap;int ret, i, bit; -mutex_lock (&data->lock); +guard (mutex)(&data->lock); +switch (scan_mask) {case BIT (0 ) |BIT (1 ) |BIT (2 ): ret =regmap_bulk_read (regmap,RM3100_REG_MX2 , data->buffer,9 ); -mutex_unlock (&data->lock);if (ret <0 ) goto done;/* Convert XXXYYYZZZxxx to XXXxYYYxZZZx. x for paddings. */
@@ -472,21 +472,18 @@ static irqreturn_t rm3100_trigger_handler(int irq, void *p)break ;case BIT (0 ) |BIT (1 ): ret =regmap_bulk_read (regmap,RM3100_REG_MX2 , data->buffer,6 ); -mutex_unlock (&data->lock);if (ret <0 ) goto done;memmove (data->buffer +4 , data->buffer +3 ,3 );break ;case BIT (1 ) |BIT (2 ): ret =regmap_bulk_read (regmap,RM3100_REG_MY2 , data->buffer,6 ); -mutex_unlock (&data->lock);if (ret <0 ) goto done;memmove (data->buffer +4 , data->buffer +3 ,3 );break ;case BIT (0 ) |BIT (2 ): ret =regmap_bulk_read (regmap,RM3100_REG_MX2 , data->buffer,9 ); -mutex_unlock (&data->lock);if (ret <0 ) goto done;memmove (data->buffer +4 , data->buffer +6 ,3 );
@@ -495,12 +492,9 @@ static irqreturn_t rm3100_trigger_handler(int irq, void *p)for_each_set_bit (bit, &scan_mask, mask_len) { ret =regmap_bulk_read (regmap,RM3100_REG_MX2 +3 * bit, data->buffer,3 ); -if (ret <0 ) { -mutex_unlock (&data->lock); +if (ret <0 ) goto done; - } } -mutex_unlock (&data->lock); }/* * Always using the same buffer so that we wouldn't need to set the
Signature · 1 line
2.53.0
Jonathan Cameron
maintainerRe: [PATCH v2 4/4] iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_trigger_handler()
Quoted 11 lines · On Tue, 28 Apr 2026 07:46:44 -0500 Maxwell Doose <[email protected]> wrote:
Replace
mutex_lock()andmutex_unlock()calls inrm3100_trigger_handler()with the more modern guard(mutex)(). This will help modernize the driver and bring it up-to-date with modern available macros/functions.Signed-off-by Maxwell Doose <[email protected]>---
v2:
- Switched outscoped_guard()for guard(mutex)().
- Because of the replacement ofscoped_guard(), fixed an error due to gotos bypassing __attribute__((cleanup)).
Read the guidance in cleanup.h It's pretty clear that nothing from that file should ever be used in functions that use gotos.
Whilst there might not be a bug here, it ends up being fragile as people changing the code may not noticed that can't use an existing label and hence jump the hidden __free() in that guard() call.
The cleanest way to use guard() here is to factor out the code under the guard. Don't increase the scope as the final stuff in here is not trivial.
There is even an outside chance of deadlock if you hold a local lock when calling iio_trigger_notify_done() as that can call back into the driver. Here you are ok because no trigger_ops are set so there isn't a reenable() callback.
Be careful to ensure any expanded scope only contains calls where you can easily see they don't deadlock or do anything time consuming.
Jonathan
Quoted 58 lines
drivers/iio/magnetometer/rm3100-core.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)drivers/iio/magnetometer/rm3100-core.c +3
-9@@ -459,11 +459,11 @@ static irqreturn_t rm3100_trigger_handler(int irq, void *p)struct regmap *regmap = data->regmap;int ret, i, bit; -mutex_lock (&data->lock); +guard (mutex)(&data->lock); +switch (scan_mask) {case BIT (0 ) |BIT (1 ) |BIT (2 ): ret =regmap_bulk_read (regmap,RM3100_REG_MX2 , data->buffer,9 ); -mutex_unlock (&data->lock);if (ret <0 ) goto done;/* Convert XXXYYYZZZxxx to XXXxYYYxZZZx. x for paddings. */ @@ -472,21 +472,18 @@ static irqreturn_t rm3100_trigger_handler(int irq, void *p)break ;case BIT (0 ) |BIT (1 ): ret =regmap_bulk_read (regmap,RM3100_REG_MX2 , data->buffer,6 ); -mutex_unlock (&data->lock);if (ret <0 ) goto done;memmove (data->buffer +4 , data->buffer +3 ,3 );break ;case BIT (1 ) |BIT (2 ): ret =regmap_bulk_read (regmap,RM3100_REG_MY2 , data->buffer,6 ); -mutex_unlock (&data->lock);if (ret <0 ) goto done;memmove (data->buffer +4 , data->buffer +3 ,3 );break ;case BIT (0 ) |BIT (2 ): ret =regmap_bulk_read (regmap,RM3100_REG_MX2 , data->buffer,9 ); -mutex_unlock (&data->lock);if (ret <0 ) goto done;memmove (data->buffer +4 , data->buffer +6 ,3 );@@ -495,12 +492,9 @@ static irqreturn_t rm3100_trigger_handler(int irq, void *p)for_each_set_bit (bit, &scan_mask, mask_len) { ret =regmap_bulk_read (regmap,RM3100_REG_MX2 +3 * bit, data->buffer,3 ); -if (ret <0 ) { -mutex_unlock (&data->lock); +if (ret <0 ) goto done; - } } -mutex_unlock (&data->lock); }/* * Always using the same buffer so that we wouldn't need to set the
Re: [PATCH v2 4/4] iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_trigger_handler()
Quoted 8 lines · Jonathan Cameron
Read the guidance in
cleanup.hIt's pretty clear that nothing from that file should ever be used in functions that use gotos.Whilst there might not be a bug here, it ends up being fragile as people changing the code may not noticed that can't use an existing label and hence jump the hidden
__free()in thatguard()call.
Then I guess if we really want to do this then we should probably get rid of the gotos? Because I can certainly do that. Either that or we just keep the manual locks and unlocks.
Quoted 2 lines
Be careful to ensure any expanded scope only contains calls where you can easily see they don't deadlock or do anything time consuming.
I'll also double-check that.
best regards,
maxwell
Re: [PATCH v2 4/4] iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_trigger_handler()
In reply to Jonathan CameronQuoted 1 line · Jonathan Cameron
[snip]
Quoted 6 lines
There is even an outside chance of deadlock if you hold a local lock when calling
iio_trigger_notify_done()as that can call back into the driver. Here you are ok because notrigger_opsare set so there isn't areenable()callback.
Given this, I'll also likely go back to scoped_guard as well once we remove the gotos, that way we'll be able to unlock it before we get to a potential callback.
best regards,
maxwell
Jonathan Cameron
maintainerRe: [PATCH v2 4/4] iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_trigger_handler()
Quoted 10 lines · On Tue, 28 Apr 2026 12:27:18 -0500 Maxwell Doose <[email protected]> wrote:
Quoted 1 line · Jonathan Cameron
[snip]
Quoted 6 lines
There is even an outside chance of deadlock if you hold a local lock when calling
iio_trigger_notify_done()as that can call back into the driver. Here you are ok because notrigger_opsare set so there isn't areenable()callback.
Quoted 4 lines
Given this, I'll also likely go back to
scoped_guardas well once we remove the gotos, that way we'll be able to unlock it before we get to a potential callback.
I'd prefer a helper function with straight forward guard() + returns. Then you can have a single exit point from the outer function.
ret = helper()
if (ret)
return ret;
other stuff.
Quoted 2 lines
best regards,
maxwell
Re: [PATCH v2 4/4] iio: magnetometer: rm3100: Use guard(mutex)() in rm3100_trigger_handler()
Quoted 10 lines · Jonathan Cameron
I'd prefer a helper function with straight forward
guard()+ returns. Then you can have a single exit point from the outer function.ret =
helper()
if (ret)return ret;other stuff.
Sounds like a great idea, I'm eager to start when I've got time.
best regards,
maxwell