diff mbox series

[01/10] iio: locking: introduce __cleanup() based direct mode claiming infrastructure

Message ID 20240128150537.44592-2-jic23@kernel.org (mailing list archive)
State Accepted
Headers show
Series IIO: Use the new cleanup.h magic | expand

Commit Message

Jonathan Cameron Jan. 28, 2024, 3:05 p.m. UTC
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Allows use of:

       iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
       }

to automatically call iio_device_release_direct_mode() based on scope.
Typically seen in combination with local device specific locks which
are already have automated cleanup options via guard(mutex)(&st->lock)
and scoped_guard(). Using both together allows most error handling to
be automated.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
Since RFC:
 - Drop the IS_ERR() protection in iio_device_release_direct_mode()
   as nothing in this new infrastructure results in this function
   being passed an error pointer.
 - Add some blank lines to make the header slightly easier to read.
---
 include/linux/iio/iio.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

Comments

Andy Shevchenko Feb. 4, 2024, 4:26 p.m. UTC | #1
Sun, Jan 28, 2024 at 03:05:28PM +0000, Jonathan Cameron kirjoitti:
> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> 
> Allows use of:
> 
>        iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
>        }
> 
> to automatically call iio_device_release_direct_mode() based on scope.
> Typically seen in combination with local device specific locks which
> are already have automated cleanup options via guard(mutex)(&st->lock)
> and scoped_guard(). Using both together allows most error handling to
> be automated.

...

> +/* This autocleanup logic is normally used via iio_claim_direct_scoped */

Wrong name? And missing parentheses.
Jonathan Cameron Feb. 4, 2024, 5:42 p.m. UTC | #2
On Sun, 4 Feb 2024 18:26:16 +0200
andy.shevchenko@gmail.com wrote:

> Sun, Jan 28, 2024 at 03:05:28PM +0000, Jonathan Cameron kirjoitti:
> > From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> > 
> > Allows use of:
> > 
> >        iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
> >        }
> > 
> > to automatically call iio_device_release_direct_mode() based on scope.
> > Typically seen in combination with local device specific locks which
> > are already have automated cleanup options via guard(mutex)(&st->lock)
> > and scoped_guard(). Using both together allows most error handling to
> > be automated.  
> 
> ...
> 
> > +/* This autocleanup logic is normally used via iio_claim_direct_scoped */  
> 
> Wrong name? And missing parentheses.
> 
Oops. I renamed it and missed the comment. Will fix up.
diff mbox series

Patch

diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
index c5b36d2c1e73..28e78af3614b 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -9,6 +9,7 @@ 
 
 #include <linux/device.h>
 #include <linux/cdev.h>
+#include <linux/cleanup.h>
 #include <linux/slab.h>
 #include <linux/iio/types.h>
 /* IIO TODO LIST */
@@ -638,6 +639,30 @@  int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
 int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
 void iio_device_release_direct_mode(struct iio_dev *indio_dev);
+
+/* This autocleanup logic is normally used via iio_claim_direct_scoped */
+DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
+	     iio_device_release_direct_mode(_T))
+
+DEFINE_GUARD_COND(iio_claim_direct, _try, ({
+			struct iio_dev *dev;
+			int d = iio_device_claim_direct_mode(_T);
+
+			if (d < 0)
+				dev = NULL;
+			else
+				dev = _T;
+			dev;
+		}))
+
+/**
+ * iio_device_claim_direct_scoped() - Scoped call to iio_device_claim_direct.
+ * @fail: What to do on failure to claim device.
+ * @iio_dev: Pointer to the IIO devices structure
+ */
+#define iio_device_claim_direct_scoped(fail, iio_dev) \
+	scoped_cond_guard(iio_claim_direct_try, fail, iio_dev)
+
 int iio_device_claim_buffer_mode(struct iio_dev *indio_dev);
 void iio_device_release_buffer_mode(struct iio_dev *indio_dev);