[PATCH v5 2/7] iio: core: Refactor iio_device_claim_direct() implementation

Kurt Borja posted 7 patches 2 weeks, 5 days ago
[PATCH v5 2/7] iio: core: Refactor iio_device_claim_direct() implementation
Posted by Kurt Borja 2 weeks, 5 days ago
In order to eventually unify the locking API, implement
iio_device_claim_direct() fully inline, with the use of
__iio_dev_mode_lock(), which takes care of sparse annotations.

Reviewed-by: David Lechner <dlechner@baylibre.com>
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
Signed-off-by: Kurt Borja <kuurtb@gmail.com>
---
 drivers/iio/industrialio-core.c | 44 -----------------------------------------
 include/linux/iio/iio.h         | 40 ++++++++++++++++++++++++++-----------
 2 files changed, 28 insertions(+), 56 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index db803267df6e..0f8e3aa98b72 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -2201,50 +2201,6 @@ void __iio_dev_mode_unlock(struct iio_dev *indio_dev)
 }
 EXPORT_SYMBOL_GPL(__iio_dev_mode_unlock);
 
-/**
- * __iio_device_claim_direct - Keep device in direct mode
- * @indio_dev:	the iio_dev associated with the device
- *
- * If the device is in direct mode it is guaranteed to stay
- * that way until __iio_device_release_direct() is called.
- *
- * Use with __iio_device_release_direct().
- *
- * Drivers should only call iio_device_claim_direct().
- *
- * Returns: true on success, false on failure.
- */
-bool __iio_device_claim_direct(struct iio_dev *indio_dev)
-{
-	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
-
-	mutex_lock(&iio_dev_opaque->mlock);
-
-	if (iio_buffer_enabled(indio_dev)) {
-		mutex_unlock(&iio_dev_opaque->mlock);
-		return false;
-	}
-	return true;
-}
-EXPORT_SYMBOL_GPL(__iio_device_claim_direct);
-
-/**
- * __iio_device_release_direct - releases claim on direct mode
- * @indio_dev:	the iio_dev associated with the device
- *
- * Release the claim. Device is no longer guaranteed to stay
- * in direct mode.
- *
- * Drivers should only call iio_device_release_direct().
- *
- * Use with __iio_device_claim_direct()
- */
-void __iio_device_release_direct(struct iio_dev *indio_dev)
-{
-	mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock);
-}
-EXPORT_SYMBOL_GPL(__iio_device_release_direct);
-
 /**
  * iio_device_claim_buffer_mode - Keep device in buffer 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 aecda887d833..e263ab5eeccf 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -664,31 +664,47 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
 
 void __iio_dev_mode_lock(struct iio_dev *indio_dev) __acquires(indio_dev);
 void __iio_dev_mode_unlock(struct iio_dev *indio_dev) __releases(indio_dev);
-bool __iio_device_claim_direct(struct iio_dev *indio_dev);
-void __iio_device_release_direct(struct iio_dev *indio_dev);
 
 /*
  * Helper functions that allow claim and release of direct mode
  * in a fashion that doesn't generate many false positives from sparse.
  * Note this must remain static inline in the header so that sparse
- * can see the __acquire() marking. Revisit when sparse supports
- * __cond_acquires()
+ * can see the __acquires() and __releases() annotations.
+ */
+
+/**
+ * iio_device_claim_direct() - Keep device in direct mode
+ * @indio_dev:	the iio_dev associated with the device
+ *
+ * If the device is in direct mode it is guaranteed to stay
+ * that way until iio_device_release_direct() is called.
+ *
+ * Use with iio_device_release_direct().
+ *
+ * Returns: true on success, false on failure.
  */
 static inline bool iio_device_claim_direct(struct iio_dev *indio_dev)
 {
-	if (!__iio_device_claim_direct(indio_dev))
-		return false;
+	__iio_dev_mode_lock(indio_dev);
 
-	__acquire(iio_dev);
+	if (iio_buffer_enabled(indio_dev)) {
+		__iio_dev_mode_unlock(indio_dev);
+		return false;
+	}
 
 	return true;
 }
 
-static inline void iio_device_release_direct(struct iio_dev *indio_dev)
-{
-	__iio_device_release_direct(indio_dev);
-	__release(indio_dev);
-}
+/**
+ * iio_device_release_direct() - Releases claim on direct mode
+ * @indio_dev:	the iio_dev associated with the device
+ *
+ * Release the claim. Device is no longer guaranteed to stay
+ * in direct mode.
+ *
+ * Use with iio_device_claim_direct().
+ */
+#define iio_device_release_direct(indio_dev) __iio_dev_mode_unlock(indio_dev)
 
 int iio_device_claim_buffer_mode(struct iio_dev *indio_dev);
 void iio_device_release_buffer_mode(struct iio_dev *indio_dev);

-- 
2.52.0

Re: [PATCH v5 2/7] iio: core: Refactor iio_device_claim_direct() implementation
Posted by Jonathan Cameron 2 weeks, 2 days ago
On Tue, 20 Jan 2026 01:20:42 -0500
Kurt Borja <kuurtb@gmail.com> wrote:

> In order to eventually unify the locking API, implement
> iio_device_claim_direct() fully inline, with the use of
> __iio_dev_mode_lock(), which takes care of sparse annotations.
> 
> Reviewed-by: David Lechner <dlechner@baylibre.com>
> Reviewed-by: Nuno Sá <nuno.sa@analog.com>
> Signed-off-by: Kurt Borja <kuurtb@gmail.com>

So sparse does change what it moans about with this one in precisely one instance
There are 9 false positives with Al Viro's updated sparse - I haven't checked the
original one recently.

Anyhow, 
drivers/iio/adc/ad7173.c:416:12: warning: context imbalance in 'ad7173_set_filter_type' - different lock contexts for basic block
becomes

drivers/iio/adc/ad7173.c:425:9: warning: context imbalance in 'ad7173_set_filter_type' - unexpected unlock                                                          

Lets go with a bit meh for that and move on...

> ---
>  drivers/iio/industrialio-core.c | 44 -----------------------------------------
>  include/linux/iio/iio.h         | 40 ++++++++++++++++++++++++++-----------
>  2 files changed, 28 insertions(+), 56 deletions(-)
> 
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index db803267df6e..0f8e3aa98b72 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -2201,50 +2201,6 @@ void __iio_dev_mode_unlock(struct iio_dev *indio_dev)
>  }
>  EXPORT_SYMBOL_GPL(__iio_dev_mode_unlock);
>  
> -/**
> - * __iio_device_claim_direct - Keep device in direct mode
> - * @indio_dev:	the iio_dev associated with the device
> - *
> - * If the device is in direct mode it is guaranteed to stay
> - * that way until __iio_device_release_direct() is called.
> - *
> - * Use with __iio_device_release_direct().
> - *
> - * Drivers should only call iio_device_claim_direct().
> - *
> - * Returns: true on success, false on failure.
> - */
> -bool __iio_device_claim_direct(struct iio_dev *indio_dev)
> -{
> -	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
> -
> -	mutex_lock(&iio_dev_opaque->mlock);
> -
> -	if (iio_buffer_enabled(indio_dev)) {
> -		mutex_unlock(&iio_dev_opaque->mlock);
> -		return false;
> -	}
> -	return true;
> -}
> -EXPORT_SYMBOL_GPL(__iio_device_claim_direct);
> -
> -/**
> - * __iio_device_release_direct - releases claim on direct mode
> - * @indio_dev:	the iio_dev associated with the device
> - *
> - * Release the claim. Device is no longer guaranteed to stay
> - * in direct mode.
> - *
> - * Drivers should only call iio_device_release_direct().
> - *
> - * Use with __iio_device_claim_direct()
> - */
> -void __iio_device_release_direct(struct iio_dev *indio_dev)
> -{
> -	mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock);
> -}
> -EXPORT_SYMBOL_GPL(__iio_device_release_direct);
> -
>  /**
>   * iio_device_claim_buffer_mode - Keep device in buffer 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 aecda887d833..e263ab5eeccf 100644
> --- a/include/linux/iio/iio.h
> +++ b/include/linux/iio/iio.h
> @@ -664,31 +664,47 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
>  
>  void __iio_dev_mode_lock(struct iio_dev *indio_dev) __acquires(indio_dev);
>  void __iio_dev_mode_unlock(struct iio_dev *indio_dev) __releases(indio_dev);
> -bool __iio_device_claim_direct(struct iio_dev *indio_dev);
> -void __iio_device_release_direct(struct iio_dev *indio_dev);
>  
>  /*
>   * Helper functions that allow claim and release of direct mode
>   * in a fashion that doesn't generate many false positives from sparse.
>   * Note this must remain static inline in the header so that sparse
> - * can see the __acquire() marking. Revisit when sparse supports
> - * __cond_acquires()
> + * can see the __acquires() and __releases() annotations.
> + */
> +
> +/**
> + * iio_device_claim_direct() - Keep device in direct mode
> + * @indio_dev:	the iio_dev associated with the device
> + *
> + * If the device is in direct mode it is guaranteed to stay
> + * that way until iio_device_release_direct() is called.
> + *
> + * Use with iio_device_release_direct().
> + *
> + * Returns: true on success, false on failure.
>   */
>  static inline bool iio_device_claim_direct(struct iio_dev *indio_dev)
>  {
> -	if (!__iio_device_claim_direct(indio_dev))
> -		return false;
> +	__iio_dev_mode_lock(indio_dev);
>  
> -	__acquire(iio_dev);
> +	if (iio_buffer_enabled(indio_dev)) {
> +		__iio_dev_mode_unlock(indio_dev);
> +		return false;
> +	}
>  
>  	return true;
>  }
>  
> -static inline void iio_device_release_direct(struct iio_dev *indio_dev)
> -{
> -	__iio_device_release_direct(indio_dev);
> -	__release(indio_dev);
> -}
> +/**
> + * iio_device_release_direct() - Releases claim on direct mode
> + * @indio_dev:	the iio_dev associated with the device
> + *
> + * Release the claim. Device is no longer guaranteed to stay
> + * in direct mode.
> + *
> + * Use with iio_device_claim_direct().
> + */
> +#define iio_device_release_direct(indio_dev) __iio_dev_mode_unlock(indio_dev)
>  
>  int iio_device_claim_buffer_mode(struct iio_dev *indio_dev);
>  void iio_device_release_buffer_mode(struct iio_dev *indio_dev);
> 
Re: [PATCH v5 2/7] iio: core: Refactor iio_device_claim_direct() implementation
Posted by Andy Shevchenko 2 weeks, 2 days ago
On Thu, Jan 22, 2026 at 07:04:01PM +0000, Jonathan Cameron wrote:
> On Tue, 20 Jan 2026 01:20:42 -0500
> Kurt Borja <kuurtb@gmail.com> wrote:

...

> So sparse does change what it moans about with this one in precisely one instance
> There are 9 false positives with Al Viro's updated sparse - I haven't checked the
> original one recently.

FWIW, I also switched to use Al's branch of sparse and recommended LKP to use
that as well.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v5 2/7] iio: core: Refactor iio_device_claim_direct() implementation
Posted by Jonathan Cameron 2 weeks, 2 days ago
On Fri, 23 Jan 2026 10:01:41 +0200
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Thu, Jan 22, 2026 at 07:04:01PM +0000, Jonathan Cameron wrote:
> > On Tue, 20 Jan 2026 01:20:42 -0500
> > Kurt Borja <kuurtb@gmail.com> wrote:  
> 
> ...
> 
> > So sparse does change what it moans about with this one in precisely one instance
> > There are 9 false positives with Al Viro's updated sparse - I haven't checked the
> > original one recently.  
> 
> FWIW, I also switched to use Al's branch of sparse and recommended LKP to use
> that as well.
> 
Agreed, that will help with most of the false positives but not all :(
Re: [PATCH v5 2/7] iio: core: Refactor iio_device_claim_direct() implementation
Posted by Kurt Borja 2 weeks, 2 days ago
On Thu Jan 22, 2026 at 2:04 PM -05, Jonathan Cameron wrote:
> On Tue, 20 Jan 2026 01:20:42 -0500
> Kurt Borja <kuurtb@gmail.com> wrote:
>
>> In order to eventually unify the locking API, implement
>> iio_device_claim_direct() fully inline, with the use of
>> __iio_dev_mode_lock(), which takes care of sparse annotations.
>> 
>> Reviewed-by: David Lechner <dlechner@baylibre.com>
>> Reviewed-by: Nuno Sá <nuno.sa@analog.com>
>> Signed-off-by: Kurt Borja <kuurtb@gmail.com>
>
> So sparse does change what it moans about with this one in precisely one instance
> There are 9 false positives with Al Viro's updated sparse - I haven't checked the
> original one recently.

Hi Jonathan,

Upstream sparse is kinda broken right now.

I'm not getting any false positives on the locking stuff but a BUNCH of
"error: bad constant expresion" on MODULE_*() macros so I don't fully
trust it.

I know at least one subsystem maintainer is getting these too so I guess
it's an upstream problem.

>
> Anyhow, 
> drivers/iio/adc/ad7173.c:416:12: warning: context imbalance in 'ad7173_set_filter_type' - different lock contexts for basic block
> becomes
>
> drivers/iio/adc/ad7173.c:425:9: warning: context imbalance in 'ad7173_set_filter_type' - unexpected unlock                                                          

This one is interesting because I can't figure out why sparse hates it.

>
> Lets go with a bit meh for that and move on...
>

-- 
Thanks,
 ~ Kurt
Re: [PATCH v5 2/7] iio: core: Refactor iio_device_claim_direct() implementation
Posted by Andy Shevchenko 2 weeks, 2 days ago
On Thu, Jan 22, 2026 at 05:20:55PM -0500, Kurt Borja wrote:
> On Thu Jan 22, 2026 at 2:04 PM -05, Jonathan Cameron wrote:
> > On Tue, 20 Jan 2026 01:20:42 -0500
> > Kurt Borja <kuurtb@gmail.com> wrote:

...

> > So sparse does change what it moans about with this one in precisely one instance
> > There are 9 false positives with Al Viro's updated sparse - I haven't checked the
> > original one recently.
> 
> Hi Jonathan,
> 
> Upstream sparse is kinda broken right now.

Yep, please switch to Al's version.

> I'm not getting any false positives on the locking stuff but a BUNCH of
> "error: bad constant expresion" on MODULE_*() macros so I don't fully
> trust it.
> 
> I know at least one subsystem maintainer is getting these too so I guess
> it's an upstream problem.

Yes, it's fixed in Al's version.

-- 
With Best Regards,
Andy Shevchenko