Re: [PATCH v7] iio: imu: kmx61: Use guard(mutex)() over manual locking

From: Maxwell Doose

Date: Thu May 21 2026 - 18:35:13 EST


On Thu, May 21, 2026 at 5:30 PM Maxwell Doose <m32285159@xxxxxxxxx> wrote:
>
> Include linux/cleanup.h to take advantage of new macros.
>
> Replace manual mutex_lock() and mutex_unlock() calls across the file
> with guard(mutex)() and scoped_guard() where appropriate to simplify
> error paths and eliminate manual locking calls.
>
> Add new helper function kmx61_read_for_each_active_channel() to mitigate
> certain style issues and to prevent notifying that the IRQ is finished
> whilst holding the lock.
>
> Update certain returns, and add default case to return -EINVAL in
> kmx61_read_raw().
>
> Remove now-redundant gotos and ret variables, as the new RAII macros
> make them unneeded.
>
> Signed-off-by: Maxwell Doose <m32285159@xxxxxxxxx>
> ---
> drivers/iio/imu/kmx61.c | 129 +++++++++++++++++++++-------------------
> 1 file changed, 67 insertions(+), 62 deletions(-)
>
[snip]

Crap, forgot the whole changelog.

v2:
- Remove redundant blank line per Andy.
- Put kmx61_set_mode() function call in kmx61_runtime_suspend() on one
line per Andy.

v3:
- Add dedicated scope for guards.

v4:
- Fix certain scoping in switch-case blocks.
- Remove stray ret variable.
- Used scoped_guard() in kmx61_trigger_handler() instead of guard()().

v5:
- Added new helper function kmx61_read_for_each_active_channel() per
Jonathan's suggestion.
- Add default case to return -EINVAL in kmx61_read_raw() per Andy's
suggestion.
- Add blank lines between guard(mutex)() calls and regular code per
Andy's suggestion.
- Change last return statement of kmx61_write_event_config() to be 0
per Jonathan's suggestion.

v6:
- Rebased onto TOCTOU fix per Jonathan's suggestion.
- (note: now dependent on said commit, link below)
- link: https://lore.kernel.org/linux-iio/CAKqfh0GHVtAS6Uw3Bjo+B0PCpcfT8FogqEPCmt2x59zhmmh1Kg@xxxxxxxxxxxxxx/T/#t

v7:
- Remove some unneeded parentheses.
- Remove stray whitespace.
- Fix up return value (from return ret; to return 0;) since we won't
ever return anything other than 0 from that return statement now.

Note:
Will save the dev_get_drvdata() patch for later.

best regards,
max