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

From: Andy Shevchenko

Date: Wed May 06 2026 - 05:10:51 EST


On Tue, May 05, 2026 at 05:07:19PM -0500, Maxwell Doose 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. This will help
> modernize the driver with up-to-date functions/macros.
>
> Remove now redundant gotos and ret variables, as the new RAII macros
> make them unneeded.

...

> - case IIO_CHAN_INFO_SAMP_FREQ:
> + case IIO_CHAN_INFO_SAMP_FREQ: {
> if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
> return -EINVAL;
>
> - mutex_lock(&data->lock);
> - ret = kmx61_get_odr(data, val, val2, chan->address);
> - mutex_unlock(&data->lock);
> + scoped_guard(mutex, &data->lock)
> + ret = kmx61_get_odr(data, val, val2, chan->address);
> if (ret)
> return -EINVAL;
> return IIO_VAL_INT_PLUS_MICRO;
> }
> + }
> return -EINVAL;

For making the above less ambiguous, add default case here instead of the
standalone return.

}
default:
return -EINVAL;
}

> }

...

> static int kmx61_runtime_suspend(struct device *dev)
> {
> struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
> - int ret;
>
> - mutex_lock(&data->lock);
> - ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
> - mutex_unlock(&data->lock);
> -
> - return ret;
> + guard(mutex)(&data->lock);

Leave a blank line here. Same applies to all guard()() cases like this.

> + return kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
> }

--
With Best Regards,
Andy Shevchenko