Skip to contentSkip to conversation

iio: core: Simplify IIO core managed APIs

PATCH Superseded Andy Shevchenko 4 participants 10 messages Feb 16 – Feb 20 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: core: Simplify IIO core managed APIs

Use devm_add_action_or_reset() instead of devres_alloc() and devres_add(), which works the same. This will simplify the code. There is no functional changes.

---
 drivers/iio/buffer/kfifo_buf.c     | 29 +++++++++++++----------------
 drivers/iio/industrialio-trigger.c | 24 ++++++++++--------------
 2 files changed, 23 insertions(+), 30 deletions(-)
drivers/iio/buffer/kfifo_buf.c +13 -16
index 38034c8bcc04..a126cc05fb38 100644
@@ -224,9 +224,9 @@ void iio_kfifo_free(struct iio_buffer *r)
 }
 EXPORT_SYMBOL(iio_kfifo_free);
 
-static void devm_iio_kfifo_release(struct device *dev, void *res)
+static void devm_iio_kfifo_release(void *r)
 {
-	iio_kfifo_free(*(struct iio_buffer **)res);
+	iio_kfifo_free(r);
 }
 
 /**
@@ -234,23 +234,20 @@ static void devm_iio_kfifo_release(struct device *dev, void *res)
  * @dev:		Device to allocate kfifo buffer for
  *
  * RETURNS:
- * Pointer to allocated iio_buffer on success, NULL on failure.
+ * Pointer to allocated iio_buffer on success, error pointer on failure.
  */
 static struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
 {
-	struct iio_buffer **ptr, *r;
-
-	ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
-	if (!ptr)
-		return NULL;
+	struct iio_buffer *r;
+	int ret;
 
 	r = iio_kfifo_allocate();
-	if (r) {
-		*ptr = r;
-		devres_add(dev, ptr);
-	} else {
-		devres_free(ptr);
-	}
+	if (!r)
+		return ERR_PTR(-ENOMEM);
+
+	ret = devm_add_action_or_reset(dev, devm_iio_kfifo_release, r);
+	if (ret)
+		return ERR_PTR(ret);
 
 	return r;
 }
@@ -275,8 +272,8 @@ int devm_iio_kfifo_buffer_setup_ext(struct device *dev,
 	struct iio_buffer *buffer;
 
 	buffer = devm_iio_kfifo_allocate(dev);
-	if (!buffer)
-		return -ENOMEM;
+	if (IS_ERR(buffer))
+		return PTR_ERR(buffer);
 
 	indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
 	indio_dev->setup_ops = setup_ops;
drivers/iio/industrialio-trigger.c +10 -14
index 54416a384232..00c8b7e5c8b9 100644
@@ -634,9 +634,9 @@ void iio_trigger_free(struct iio_trigger *trig)
 }
 EXPORT_SYMBOL(iio_trigger_free);
 
-static void devm_iio_trigger_release(struct device *dev, void *res)
+static void devm_iio_trigger_release(void *trig)
 {
-	iio_trigger_free(*(struct iio_trigger **)res);
+	iio_trigger_free(trig);
 }
 
 /**
@@ -658,24 +658,20 @@ struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent,
 					     struct module *this_mod,
 					     const char *fmt, ...)
 {
-	struct iio_trigger **ptr, *trig;
+	struct iio_trigger *trig;
 	va_list vargs;
-
-	ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
-			   GFP_KERNEL);
-	if (!ptr)
-		return NULL;
+	int ret;
 
 	/* use raw alloc_dr for kmalloc caller tracing */
 	va_start(vargs, fmt);
 	trig = viio_trigger_alloc(parent, this_mod, fmt, vargs);
 	va_end(vargs);
-	if (trig) {
-		*ptr = trig;
-		devres_add(parent, ptr);
-	} else {
-		devres_free(ptr);
-	}
+	if (!trig)
+		return NULL;
+
+	ret = devm_add_action_or_reset(parent, devm_iio_trigger_release, trig);
+	if (ret)
+		return NULL;
 
 	return trig;
 }
Signature · 1 line
2.50.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Andy Shevchenko

Use devm_add_action_or_reset() instead of devres_alloc() and devres_add(), which works the same. This will simplify the code. There is no functional changes.

---

LGTM. Just one minor "complain". Anyways:

Quoted 57 lines

 drivers/iio/buffer/kfifo_buf.c     | 29 +++++++++++++---------------- drivers/iio/industrialio-trigger.c | 24 ++++++++++--------------

 2 files changed, 23 insertions(+), 30 deletions(-)
drivers/iio/buffer/kfifo_buf.c +0 -0
index 38034c8bcc04..a126cc05fb38 100644
@@ -224,9 +224,9 @@ void iio_kfifo_free(struct iio_buffer *r)
  }
  EXPORT_SYMBOL(iio_kfifo_free);

drivers/iio/buffer/kfifo_buf.c +1 -1
-static void devm_iio_kfifo_release(struct device *dev, void *res)
+static void devm_iio_kfifo_release(void *r)

 {

drivers/iio/buffer/kfifo_buf.c +1 -1
-	iio_kfifo_free(*(struct iio_buffer **)res);
+	iio_kfifo_free(r);

 }

 /**

drivers/iio/buffer/kfifo_buf.c +3 -6
@@ -234,23 +234,20 @@ static void devm_iio_kfifo_release(struct device *dev, void *res)
   * @dev:		Device to allocate kfifo buffer for
   *
   * RETURNS:
- * Pointer to allocated iio_buffer on success, NULL on failure.
+ * Pointer to allocated iio_buffer on success, error pointer on failure.
   */
  static struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
  {
-	struct iio_buffer **ptr, *r;
-
-	ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
-	if (!ptr)
-		return NULL;
+	struct iio_buffer *r;
+	int ret;


  r = iio_kfifo_allocate();

drivers/iio/buffer/kfifo_buf.c +6 -6
-	if (r) {
-		*ptr = r;
-		devres_add(dev, ptr);
-	} else {
-		devres_free(ptr);
-	}
+	if (!r)
+		return ERR_PTR(-ENOMEM);
+
+	ret = devm_add_action_or_reset(dev, devm_iio_kfifo_release, r);
+	if (ret)
+		return ERR_PTR(ret);


  return r;
 }

drivers/iio/buffer/kfifo_buf.c +0 -0
@@ -275,8 +272,8 @@ int devm_iio_kfifo_buffer_setup_ext(struct device *dev,
  	struct iio_buffer *buffer;


  buffer = devm_iio_kfifo_allocate(dev);

drivers/iio/buffer/kfifo_buf.c int devm_iio_kfifo_buffer_setup_ext(struct device *dev, · line 276

Quoted 4 lines
drivers/iio/buffer/kfifo_buf.c +2 -2
-	if (!buffer)
-		return -ENOMEM;
+	if (IS_ERR(buffer))
+		return PTR_ERR(buffer);

Subtle change that could have been mentioned in the commit message.

- Nuno Sá

#2 lore raw
Quoted 6 lines · Nuno Sá
Quoted 3 lines · Andy Shevchenko

Use devm_add_action_or_reset() instead of devres_alloc() and devres_add(), which works the same. This will simplify the code. There is no functional changes.

LGTM. Just one minor "complain". Anyways:

My answer below.

Quoted 1 line

Thanks!

...

Quoted 6 lines

drivers/iio/buffer/kfifo_buf.c int devm_iio_kfifo_buffer_setup_ext(struct device *dev, · line 276

Quoted 4 lines
drivers/iio/buffer/kfifo_buf.c +2 -2
-	if (!buffer)
-		return -ENOMEM;
+	if (IS_ERR(buffer))
+		return PTR_ERR(buffer);

Subtle change that could have been mentioned in the commit message.

Actually not really a change. Currently the error code is shadowed to -ENOMEM, but again currently the callee can't return anything else. So, it's just a use of the regular pattern. TL;DR: there is no functional change currently. If anything in the future comes returning different error codes here, that will bring the difference.

Anyways, if Jonathan asks to elaborate and send a new version, I will do for sure.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 16 lines · Andy Shevchenko
Quoted 6 lines · Nuno Sá
Quoted 3 lines · Andy Shevchenko

Use devm_add_action_or_reset() instead of devres_alloc() and devres_add(), which works the same. This will simplify the code. There is no functional changes.

LGTM. Just one minor "complain". Anyways:

My answer below.

Quoted 1 line

Thanks!

...

Quoted 8 lines
Quoted 6 lines

drivers/iio/buffer/kfifo_buf.c int devm_iio_kfifo_buffer_setup_ext(struct device *dev, · line 276

Quoted 4 lines
drivers/iio/buffer/kfifo_buf.c +2 -2
-	if (!buffer)
-		return -ENOMEM;
+	if (IS_ERR(buffer))
+		return PTR_ERR(buffer);

Subtle change that could have been mentioned in the commit message.

Actually not really a change. Currently the error code is shadowed to -ENOMEM,

Well before that was explicit. Now it also depends on the internals of __devm_add_action(). That is why I said it was subtle. But yes, no functional change.

- Nuno Sá

Quoted 1 line
#4 lore raw
Quoted 3 lines · Nuno Sá
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá

On Mon, 2026-02-16 at 09:14 +0100, Andy Shevchenko wrote:

...

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

drivers/iio/buffer/kfifo_buf.c int devm_iio_kfifo_buffer_setup_ext(struct device *dev, · line 276

Quoted 4 lines
drivers/iio/buffer/kfifo_buf.c +2 -2
-	if (!buffer)
-		return -ENOMEM;
+	if (IS_ERR(buffer))
+		return PTR_ERR(buffer);

Subtle change that could have been mentioned in the commit message.

Actually not really a change. Currently the error code is shadowed to -ENOMEM,

Well before that was explicit. Now it also depends on the internals of __devm_add_action().

True, but if that ever changed, we will have a new error code coming from ->probe() and that's fine. ->probe() is not stricted to the certain error codes.

Quoted 1 line

That is why I said it was subtle. But yes, no functional change.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 14 lines · Andy Shevchenko
Quoted 3 lines · Nuno Sá
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá

On Mon, 2026-02-16 at 09:14 +0100, Andy Shevchenko wrote:

...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

drivers/iio/buffer/kfifo_buf.c int devm_iio_kfifo_buffer_setup_ext(struct device *dev, · line 276

Quoted 4 lines
drivers/iio/buffer/kfifo_buf.c +2 -2
-	if (!buffer)
-		return -ENOMEM;
+	if (IS_ERR(buffer))
+		return PTR_ERR(buffer);

Subtle change that could have been mentioned in the commit message.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Actually not really a change. Currently the error code is shadowed to -ENOMEM,

Well before that was explicit. Now it also depends on the internals of  __devm_add_action().

True, but if that ever changed, we will have a new error code coming from ->probe() and that's fine. ->probe() is not stricted to the certain error codes.

Note that I'm not complaining about the change. Just that it could have been mentioned in the commit :)

- Nuno Sá

Quoted 1 line
maintainer
#6 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · On Mon, 16 Feb 2026 09:14:12 +0100 Andy Shevchenko…

Use devm_add_action_or_reset() instead of devres_alloc() and devres_add(), which works the same. This will simplify the code. There is no functional changes.

Nice. There was a mass sweep up of these cases a few years back but I guess these instances slipped through the scripting that was used to find those.

Quoted 10 lines
---
 drivers/iio/buffer/kfifo_buf.c     | 29 +++++++++++++----------------
 drivers/iio/industrialio-trigger.c | 24 ++++++++++--------------
 2 files changed, 23 insertions(+), 30 deletions(-)
drivers/iio/buffer/kfifo_buf.c +0 -0
index 38034c8bcc04..a126cc05fb38 100644

drivers/iio/buffer/kfifo_buf.c static void devm_iio_kfifo_release(struct device *dev, void *res) · line 237

Quoted 5 lines
drivers/iio/buffer/kfifo_buf.c +1 -1
@@ -234,23 +234,20 @@ static void devm_iio_kfifo_release(struct device *dev, void *res)
  * @dev:		Device to allocate kfifo buffer for
  *
  * RETURNS:
- * Pointer to allocated iio_buffer on success, NULL on failure.
+ * Pointer to allocated iio_buffer on success, error pointer on failure.

This had me briefly confused as I failed to notice it was static (no idea why I gave such a simple internal function kernel-doc!)

However that brings the obvious follow up question. Given your simplification does having a helper make sense given only a single caller?

Let's just squash it and have the two calls inline + some resulting docs updates to get rid of the references to this function.

Thanks,

Jonathan

Quoted 38 lines
drivers/iio/buffer/kfifo_buf.c +10 -13
  */
 static struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
 {
-	struct iio_buffer **ptr, *r;
-
-	ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
-	if (!ptr)
-		return NULL;
+	struct iio_buffer *r;
+	int ret;
 
 	r = iio_kfifo_allocate();
-	if (r) {
-		*ptr = r;
-		devres_add(dev, ptr);
-	} else {
-		devres_free(ptr);
-	}
+	if (!r)
+		return ERR_PTR(-ENOMEM);
+
+	ret = devm_add_action_or_reset(dev, devm_iio_kfifo_release, r);
+	if (ret)
+		return ERR_PTR(ret);
 
 	return r;
 }
@@ -275,8 +272,8 @@ int devm_iio_kfifo_buffer_setup_ext(struct device *dev,
 	struct iio_buffer *buffer;
 
 	buffer = devm_iio_kfifo_allocate(dev);
-	if (!buffer)
-		return -ENOMEM;
+	if (IS_ERR(buffer))
+		return PTR_ERR(buffer);
 
 	indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
 	indio_dev->setup_ops = setup_ops;
#7 lore raw
Quoted 2 lines · Jonathan Cameron

On Mon, 16 Feb 2026 09:14:12 +0100
Andy Shevchenko <[email protected]> wrote:

...

Quoted 8 lines

This had me briefly confused as I failed to notice it was static (no idea why I gave such a simple internal function kernel-doc!)

However that brings the obvious follow up question. Given your simplification does having a helper make sense given only a single caller?

Let's just squash it and have the two calls inline + some resulting docs updates to get rid of the references to this function.

Do you want to have this change inside the proposed patch? Or in a separate (pre/post) one?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#8 lore raw
Quoted 9 lines · On Fri, 20 Feb 2026 12:27:40 +0200 Andy Shevchenko <[email protected]>…
Quoted 2 lines · Jonathan Cameron

On Mon, 16 Feb 2026 09:14:12 +0100
Andy Shevchenko <[email protected]> wrote:

...

Quoted 3 lines

This had me briefly confused as I failed to notice it was static (no idea why I gave such a simple internal function kernel-doc!)

Quoted 8 lines
Quoted 5 lines

However that brings the obvious follow up question. Given your simplification does having a helper make sense given only a single caller?

Let's just squash it and have the two calls inline + some resulting docs updates to get rid of the references to this function.

Do you want to have this change inside the proposed patch? Or in a separate (pre/post) one?

Same patch would be fine I think. It's small and coupled to the code getting simpler because of the main change here. Seems like a pointless dance to refactor same code twice.

Jonathan

Quoted 1 line
#9 lore raw
Quoted 5 lines · Jonathan Cameron
Quoted 3 lines · On Fri, 20 Feb 2026 12:27:40 +0200 Andy Shevchenko <[email protected]>…
Quoted 2 lines · Jonathan Cameron

On Mon, 16 Feb 2026 09:14:12 +0100
Andy Shevchenko <[email protected]> wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

This had me briefly confused as I failed to notice it was static (no idea why I gave such a simple internal function kernel-doc!)

However that brings the obvious follow up question. Given your simplification does having a helper make sense given only a single caller?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Let's just squash it and have the two calls inline + some resulting docs updates to get rid of the references to this function.

Do you want to have this change inside the proposed patch? Or in a separate (pre/post) one?

Same patch would be fine I think. It's small and coupled to the code getting simpler because of the main change here. Seems like a pointless dance to refactor same code twice.

v2 has just been sent.

Signature · 2 lines
With Best Regards,
Andy Shevchenko