Re: [PATCH RFC 3/6] iio: core: Add cleanup.h support for iio_device_claim_*()

From: Jonathan Cameron

Date: Sat Dec 06 2025 - 13:21:07 EST


On Wed, 03 Dec 2025 14:18:17 -0500
Kurt Borja <kuurtb@xxxxxxxxx> wrote:

> Add guard() and ACQUIRE() support for iio_device_claim_*() lock.
>
> This involves exporting iio_device_{claim, release}() wrappers to define
> a general GUARD class, and then defining the _direct and _buffer
> conditional ones.
>
> Suggested-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>
> Signed-off-by: Kurt Borja <kuurtb@xxxxxxxxx>

I'd like some documentation (alongside the DEFINE_GUARD() stuff)
- particularly warn people that using the non conditional variant
is very much the unusual one.

Perhaps add examples of usage for the other two and ignore that one
on basis alarm bells will ring whenever we see it in code.

Jonathan

> ---
> drivers/iio/industrialio-core.c | 12 ++++++++++++
> include/linux/iio/iio.h | 20 ++++++++++++++++++++
> 2 files changed, 32 insertions(+)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index adf0142d0300..da090c993fe8 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -2171,6 +2171,18 @@ int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
> }
> EXPORT_SYMBOL_GPL(__devm_iio_device_register);
>
> +void __iio_device_claim(struct iio_dev *indio_dev)
> +{
> + mutex_lock(&to_iio_dev_opaque(indio_dev)->mlock);
> +}
> +EXPORT_SYMBOL_GPL(__iio_device_claim);
> +
> +void __iio_device_release(struct iio_dev *indio_dev)
> +{
> + mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock);
> +}
> +EXPORT_SYMBOL_GPL(__iio_device_release);
> +
> /**
> * __iio_device_claim_direct - Keep device in direct mode
> * @indio_dev: the iio_dev associated with the device
> diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
> index 27da9af67c47..472b13ec28d3 100644
> --- a/include/linux/iio/iio.h
> +++ b/include/linux/iio/iio.h
> @@ -10,6 +10,7 @@
> #include <linux/align.h>
> #include <linux/device.h>
> #include <linux/cdev.h>
> +#include <linux/cleanup.h>
> #include <linux/compiler_types.h>
> #include <linux/minmax.h>
> #include <linux/slab.h>
> @@ -661,9 +662,23 @@ void iio_device_unregister(struct iio_dev *indio_dev);
> int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
> struct module *this_mod);
> int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
> +void __iio_device_claim(struct iio_dev *indio_dev);
> +void __iio_device_release(struct iio_dev *indio_dev);
> bool __iio_device_claim_direct(struct iio_dev *indio_dev);
> void __iio_device_release_direct(struct iio_dev *indio_dev);
>
> +static inline void iio_device_claim(struct iio_dev *indio_dev)
> + __acquires(indio_dev)
> +{
> + __iio_device_claim(indio_dev);
> +}
> +
> +static inline void iio_device_release(struct iio_dev *indio_dev)
> + __releases(indio_dev)
> +{
> + __iio_device_release(indio_dev);
> +}
> +
> /*
> * Helper functions that allow claim and release of direct mode
> * in a fashion that doesn't generate many false positives from sparse.
> @@ -690,6 +705,11 @@ static inline void iio_device_release_direct(struct iio_dev *indio_dev)
> bool iio_device_claim_buffer(struct iio_dev *indio_dev);
> void iio_device_release_buffer(struct iio_dev *indio_dev);
>
> +DEFINE_GUARD(iio_device_claim, struct iio_dev *, iio_device_claim(_T),
> + iio_device_release(_T));
> +DEFINE_GUARD_COND(iio_device_claim, _buffer, iio_device_claim_buffer(_T));
> +DEFINE_GUARD_COND(iio_device_claim, _direct, iio_device_claim_direct(_T));
> +
> extern const struct bus_type iio_bus_type;
>
> /**
>