linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] device property: Add scoped fwnode child node iterators
@ 2025-09-02 19:04 Jean-François Lessard
  2025-09-02 19:04 ` [PATCH v4 1/2] " Jean-François Lessard
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Jean-François Lessard @ 2025-09-02 19:04 UTC (permalink / raw)
  To: Wolfram Sang, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich
  Cc: linux-i2c, linux-kernel, linux-acpi

This series adds scoped versions of fwnode iterator macros and converts
existing manual implementation to use them.

The first patch adds the infrastructure macros following existing patterns
for scoped iterators in the kernel. The second patch demonstrates
fwnode_for_each_child_node_scoped() usage by converting existing manual
__free() usage in i2c-core-slave.c.

This series introduces infrastructure for the TM16XX driver series,
being the first user of fwnode_for_each_available_child_node_scoped().
See the related patch series:
  auxdisplay: Add TM16xx 7-segment LED matrix display controllers driver

Changes in v4:
- drop the fwnode_for_each_named_child_node_scoped() variant (no user)
- add Reviewed-by tag to commit message of patch 2/2

Changes in v3:
- Split into separate patches as requested
- Infrastructure addition in patch 1/2
- Usage example in patch 2/2

Changes in v2:
- replace manual __free(fwnode_handle) of i2c-core-slave.c with
  fwnode_for_each_child_node_scoped

Jean-François Lessard (2):
  device property: Add scoped fwnode child node iterators
  i2c: core: Use fwnode_for_each_child_node_scoped()

 drivers/i2c/i2c-core-slave.c |  3 +--
 include/linux/property.h     | 10 ++++++++++
 2 files changed, 11 insertions(+), 2 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v4 1/2] device property: Add scoped fwnode child node iterators
  2025-09-02 19:04 [PATCH v4 0/2] device property: Add scoped fwnode child node iterators Jean-François Lessard
@ 2025-09-02 19:04 ` Jean-François Lessard
  2025-09-02 19:11   ` Danilo Krummrich
                     ` (2 more replies)
  2025-09-02 19:04 ` [PATCH v4 2/2] i2c: core: Use fwnode_for_each_child_node_scoped() Jean-François Lessard
  2025-09-03 10:30 ` [PATCH v4 0/2] device property: Add scoped fwnode child node iterators Andy Shevchenko
  2 siblings, 3 replies; 10+ messages in thread
From: Jean-François Lessard @ 2025-09-02 19:04 UTC (permalink / raw)
  To: Wolfram Sang, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich
  Cc: linux-i2c, linux-kernel, linux-acpi

Add scoped versions of fwnode child node iterators that automatically
handle reference counting cleanup using the __free() attribute:

- fwnode_for_each_child_node_scoped()
- fwnode_for_each_available_child_node_scoped()

These macros follow the same pattern as existing scoped iterators in the
kernel, ensuring fwnode references are automatically released when the
iterator variable goes out of scope. This prevents resource leaks and
eliminates the need for manual cleanup in error paths.

The implementation mirrors the non-scoped variants but uses
__free(fwnode_handle) for automatic resource management, providing a
safer and more convenient interface for drivers iterating over firmware
node children.

Signed-off-by: Jean-François Lessard <jefflessard3@gmail.com>
---

Notes:
    checkpatch reports false positives that are intentionally ignored:
    MACRO_ARG_REUSE, MACRO_ARG_PRECEDENCE
    This is a standard iterator pattern following kernel conventions.

 include/linux/property.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/include/linux/property.h b/include/linux/property.h
index 82f0cb3ab..862e20813 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -176,6 +176,16 @@ struct fwnode_handle *fwnode_get_next_available_child_node(
 	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
 	     child = fwnode_get_next_available_child_node(fwnode, child))
 
+#define fwnode_for_each_child_node_scoped(fwnode, child)		\
+	for (struct fwnode_handle *child __free(fwnode_handle) =	\
+		fwnode_get_next_child_node(fwnode, NULL);		\
+	     child; child = fwnode_get_next_child_node(fwnode, child))
+
+#define fwnode_for_each_available_child_node_scoped(fwnode, child)	\
+	for (struct fwnode_handle *child __free(fwnode_handle) =	\
+		fwnode_get_next_available_child_node(fwnode, NULL);	\
+	     child; child = fwnode_get_next_available_child_node(fwnode, child))
+
 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
 						 struct fwnode_handle *child);
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v4 2/2] i2c: core: Use fwnode_for_each_child_node_scoped()
  2025-09-02 19:04 [PATCH v4 0/2] device property: Add scoped fwnode child node iterators Jean-François Lessard
  2025-09-02 19:04 ` [PATCH v4 1/2] " Jean-François Lessard
@ 2025-09-02 19:04 ` Jean-François Lessard
  2025-09-03 13:19   ` Sakari Ailus
  2025-09-03 10:30 ` [PATCH v4 0/2] device property: Add scoped fwnode child node iterators Andy Shevchenko
  2 siblings, 1 reply; 10+ messages in thread
From: Jean-François Lessard @ 2025-09-02 19:04 UTC (permalink / raw)
  To: Wolfram Sang, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich
  Cc: linux-i2c, linux-kernel, linux-acpi

Replace the manual __free(fwnode_handle) iterator declaration with the
new scoped iterator macro for cleaner, less error-prone code.

This eliminates the need for explicit iterator variable declaration with
the cleanup attribute, making the code more consistent with other scoped
iterator usage patterns in the kernel.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Jean-François Lessard <jefflessard3@gmail.com>
---
 drivers/i2c/i2c-core-slave.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/i2c/i2c-core-slave.c b/drivers/i2c/i2c-core-slave.c
index 7ee6b992b..02ca55c22 100644
--- a/drivers/i2c/i2c-core-slave.c
+++ b/drivers/i2c/i2c-core-slave.c
@@ -112,10 +112,9 @@ bool i2c_detect_slave_mode(struct device *dev)
 	struct fwnode_handle *fwnode = dev_fwnode(dev);
 
 	if (is_of_node(fwnode)) {
-		struct fwnode_handle *child __free(fwnode_handle) = NULL;
 		u32 reg;
 
-		fwnode_for_each_child_node(fwnode, child) {
+		fwnode_for_each_child_node_scoped(fwnode, child) {
 			fwnode_property_read_u32(child, "reg", &reg);
 			if (reg & I2C_OWN_SLAVE_ADDRESS)
 				return true;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 1/2] device property: Add scoped fwnode child node iterators
  2025-09-02 19:04 ` [PATCH v4 1/2] " Jean-François Lessard
@ 2025-09-02 19:11   ` Danilo Krummrich
  2025-09-03 10:29   ` Andy Shevchenko
  2025-09-03 13:18   ` Sakari Ailus
  2 siblings, 0 replies; 10+ messages in thread
From: Danilo Krummrich @ 2025-09-02 19:11 UTC (permalink / raw)
  To: Jean-François Lessard
  Cc: Wolfram Sang, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Sakari Ailus, Greg Kroah-Hartman, Rafael J. Wysocki, linux-i2c,
	linux-kernel, linux-acpi



On 9/2/25 9:04 PM, Jean-François Lessard wrote:
> Add scoped versions of fwnode child node iterators that automatically
> handle reference counting cleanup using the __free() attribute:
> 
> - fwnode_for_each_child_node_scoped()
> - fwnode_for_each_available_child_node_scoped()
> 
> These macros follow the same pattern as existing scoped iterators in the
> kernel, ensuring fwnode references are automatically released when the
> iterator variable goes out of scope. This prevents resource leaks and
> eliminates the need for manual cleanup in error paths.
> 
> The implementation mirrors the non-scoped variants but uses
> __free(fwnode_handle) for automatic resource management, providing a
> safer and more convenient interface for drivers iterating over firmware
> node children.
> 
> Signed-off-by: Jean-François Lessard <jefflessard3@gmail.com>

Acked-by: Danilo Krummrich <dakr@kernel.org>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 1/2] device property: Add scoped fwnode child node iterators
  2025-09-02 19:04 ` [PATCH v4 1/2] " Jean-François Lessard
  2025-09-02 19:11   ` Danilo Krummrich
@ 2025-09-03 10:29   ` Andy Shevchenko
  2025-09-03 13:18   ` Sakari Ailus
  2 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2025-09-03 10:29 UTC (permalink / raw)
  To: Jean-François Lessard
  Cc: Wolfram Sang, Daniel Scally, Heikki Krogerus, Sakari Ailus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	linux-i2c, linux-kernel, linux-acpi

On Tue, Sep 02, 2025 at 03:04:39PM -0400, Jean-François Lessard wrote:
> Add scoped versions of fwnode child node iterators that automatically
> handle reference counting cleanup using the __free() attribute:
> 
> - fwnode_for_each_child_node_scoped()
> - fwnode_for_each_available_child_node_scoped()
> 
> These macros follow the same pattern as existing scoped iterators in the
> kernel, ensuring fwnode references are automatically released when the
> iterator variable goes out of scope. This prevents resource leaks and
> eliminates the need for manual cleanup in error paths.
> 
> The implementation mirrors the non-scoped variants but uses
> __free(fwnode_handle) for automatic resource management, providing a
> safer and more convenient interface for drivers iterating over firmware
> node children.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 0/2] device property: Add scoped fwnode child node iterators
  2025-09-02 19:04 [PATCH v4 0/2] device property: Add scoped fwnode child node iterators Jean-François Lessard
  2025-09-02 19:04 ` [PATCH v4 1/2] " Jean-François Lessard
  2025-09-02 19:04 ` [PATCH v4 2/2] i2c: core: Use fwnode_for_each_child_node_scoped() Jean-François Lessard
@ 2025-09-03 10:30 ` Andy Shevchenko
  2 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2025-09-03 10:30 UTC (permalink / raw)
  To: Jean-François Lessard
  Cc: Wolfram Sang, Daniel Scally, Heikki Krogerus, Sakari Ailus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	linux-i2c, linux-kernel, linux-acpi

On Tue, Sep 02, 2025 at 03:04:38PM -0400, Jean-François Lessard wrote:
> This series adds scoped versions of fwnode iterator macros and converts
> existing manual implementation to use them.
> 
> The first patch adds the infrastructure macros following existing patterns
> for scoped iterators in the kernel. The second patch demonstrates
> fwnode_for_each_child_node_scoped() usage by converting existing manual
> __free() usage in i2c-core-slave.c.
> 
> This series introduces infrastructure for the TM16XX driver series,
> being the first user of fwnode_for_each_available_child_node_scoped().
> See the related patch series:
>   auxdisplay: Add TM16xx 7-segment LED matrix display controllers driver

It might be good to have an immutable branch for me from i2c core.
Wolfram, can you provide a such if no objections?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 1/2] device property: Add scoped fwnode child node iterators
  2025-09-02 19:04 ` [PATCH v4 1/2] " Jean-François Lessard
  2025-09-02 19:11   ` Danilo Krummrich
  2025-09-03 10:29   ` Andy Shevchenko
@ 2025-09-03 13:18   ` Sakari Ailus
  2025-09-03 16:43     ` Jean-François Lessard
  2025-09-03 17:22     ` Danilo Krummrich
  2 siblings, 2 replies; 10+ messages in thread
From: Sakari Ailus @ 2025-09-03 13:18 UTC (permalink / raw)
  To: Jean-François Lessard
  Cc: Wolfram Sang, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	linux-i2c, linux-kernel, linux-acpi

Hi Jean-François,

On Tue, Sep 02, 2025 at 03:04:39PM -0400, Jean-François Lessard wrote:
> Add scoped versions of fwnode child node iterators that automatically
> handle reference counting cleanup using the __free() attribute:
> 
> - fwnode_for_each_child_node_scoped()
> - fwnode_for_each_available_child_node_scoped()
> 
> These macros follow the same pattern as existing scoped iterators in the
> kernel, ensuring fwnode references are automatically released when the
> iterator variable goes out of scope. This prevents resource leaks and
> eliminates the need for manual cleanup in error paths.
> 
> The implementation mirrors the non-scoped variants but uses
> __free(fwnode_handle) for automatic resource management, providing a
> safer and more convenient interface for drivers iterating over firmware
> node children.
> 
> Signed-off-by: Jean-François Lessard <jefflessard3@gmail.com>
> ---
> 
> Notes:
>     checkpatch reports false positives that are intentionally ignored:
>     MACRO_ARG_REUSE, MACRO_ARG_PRECEDENCE
>     This is a standard iterator pattern following kernel conventions.
> 
>  include/linux/property.h | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/include/linux/property.h b/include/linux/property.h
> index 82f0cb3ab..862e20813 100644
> --- a/include/linux/property.h
> +++ b/include/linux/property.h
> @@ -176,6 +176,16 @@ struct fwnode_handle *fwnode_get_next_available_child_node(
>  	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
>  	     child = fwnode_get_next_available_child_node(fwnode, child))
>  
> +#define fwnode_for_each_child_node_scoped(fwnode, child)		\
> +	for (struct fwnode_handle *child __free(fwnode_handle) =	\
> +		fwnode_get_next_child_node(fwnode, NULL);		\
> +	     child; child = fwnode_get_next_child_node(fwnode, child))
> +
> +#define fwnode_for_each_available_child_node_scoped(fwnode, child)	\
> +	for (struct fwnode_handle *child __free(fwnode_handle) =	\
> +		fwnode_get_next_available_child_node(fwnode, NULL);	\
> +	     child; child = fwnode_get_next_available_child_node(fwnode, child))
> +

Do we really need the available variant?

Please see
<URL:https://lore.kernel.org/linux-acpi/Zwj12J5bTNUEnxA0@kekkonen.localdomain/>.

I'll post a patch to remove fwnode_get_next_available_child_node(), too.

>  struct fwnode_handle *device_get_next_child_node(const struct device *dev,
>  						 struct fwnode_handle *child);
>  

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 2/2] i2c: core: Use fwnode_for_each_child_node_scoped()
  2025-09-02 19:04 ` [PATCH v4 2/2] i2c: core: Use fwnode_for_each_child_node_scoped() Jean-François Lessard
@ 2025-09-03 13:19   ` Sakari Ailus
  0 siblings, 0 replies; 10+ messages in thread
From: Sakari Ailus @ 2025-09-03 13:19 UTC (permalink / raw)
  To: Jean-François Lessard
  Cc: Wolfram Sang, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	linux-i2c, linux-kernel, linux-acpi

On Tue, Sep 02, 2025 at 03:04:40PM -0400, Jean-François Lessard wrote:
> Replace the manual __free(fwnode_handle) iterator declaration with the
> new scoped iterator macro for cleaner, less error-prone code.
> 
> This eliminates the need for explicit iterator variable declaration with
> the cleanup attribute, making the code more consistent with other scoped
> iterator usage patterns in the kernel.
> 
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Signed-off-by: Jean-François Lessard <jefflessard3@gmail.com>

Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>

-- 
Sakari Ailus

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 1/2] device property: Add scoped fwnode child node iterators
  2025-09-03 13:18   ` Sakari Ailus
@ 2025-09-03 16:43     ` Jean-François Lessard
  2025-09-03 17:22     ` Danilo Krummrich
  1 sibling, 0 replies; 10+ messages in thread
From: Jean-François Lessard @ 2025-09-03 16:43 UTC (permalink / raw)
  To: Sakari Ailus
  Cc: Wolfram Sang, Andy Shevchenko, Daniel Scally, Heikki Krogerus,
	Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	linux-i2c, linux-kernel, linux-acpi

Hi Sakari,

Le 3 septembre 2025 09 h 18 min 32 s HAE, Sakari Ailus <sakari.ailus@linux.intel.com> a écrit :
>Hi Jean-François,
>
>On Tue, Sep 02, 2025 at 03:04:39PM -0400, Jean-François Lessard wrote:
>> Add scoped versions of fwnode child node iterators that automatically
>> handle reference counting cleanup using the __free() attribute:
>> 
>> - fwnode_for_each_child_node_scoped()
>> - fwnode_for_each_available_child_node_scoped()
>> 
>> These macros follow the same pattern as existing scoped iterators in the
>> kernel, ensuring fwnode references are automatically released when the
>> iterator variable goes out of scope. This prevents resource leaks and
>> eliminates the need for manual cleanup in error paths.
>> 
>> The implementation mirrors the non-scoped variants but uses
>> __free(fwnode_handle) for automatic resource management, providing a
>> safer and more convenient interface for drivers iterating over firmware
>> node children.
>> 
>> Signed-off-by: Jean-François Lessard <jefflessard3@gmail.com>
>> ---
>> 
>> Notes:
>>     checkpatch reports false positives that are intentionally ignored:
>>     MACRO_ARG_REUSE, MACRO_ARG_PRECEDENCE
>>     This is a standard iterator pattern following kernel conventions.
>> 
>>  include/linux/property.h | 10 ++++++++++
>>  1 file changed, 10 insertions(+)
>> 
>> diff --git a/include/linux/property.h b/include/linux/property.h
>> index 82f0cb3ab..862e20813 100644
>> --- a/include/linux/property.h
>> +++ b/include/linux/property.h
>> @@ -176,6 +176,16 @@ struct fwnode_handle *fwnode_get_next_available_child_node(
>>  	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
>>  	     child = fwnode_get_next_available_child_node(fwnode, child))
>>  
>> +#define fwnode_for_each_child_node_scoped(fwnode, child)		\
>> +	for (struct fwnode_handle *child __free(fwnode_handle) =	\
>> +		fwnode_get_next_child_node(fwnode, NULL);		\
>> +	     child; child = fwnode_get_next_child_node(fwnode, child))
>> +
>> +#define fwnode_for_each_available_child_node_scoped(fwnode, child)	\
>> +	for (struct fwnode_handle *child __free(fwnode_handle) =	\
>> +		fwnode_get_next_available_child_node(fwnode, NULL);	\
>> +	     child; child = fwnode_get_next_available_child_node(fwnode, child))
>> +
>
>Do we really need the available variant?
>
>Please see
><URL:https://lore.kernel.org/linux-acpi/Zwj12J5bTNUEnxA0@kekkonen.localdomain/>.
>
>I'll post a patch to remove fwnode_get_next_available_child_node(), too.
>

Thanks for the link to the discussion.

I see you're planning to remove fwnode_get_next_available_child_node() 
entirely. In that context, adding a scoped version doesn't make sense.

For my driver use case, I can handle the status checking manually if 
the _available_ variant is being deprecated.

Should I drop the _available_ variant and submit v5 with only 
fwnode_for_each_child_node_scoped()?

>>  struct fwnode_handle *device_get_next_child_node(const struct device *dev,
>>  						 struct fwnode_handle *child);
>>  
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v4 1/2] device property: Add scoped fwnode child node iterators
  2025-09-03 13:18   ` Sakari Ailus
  2025-09-03 16:43     ` Jean-François Lessard
@ 2025-09-03 17:22     ` Danilo Krummrich
  1 sibling, 0 replies; 10+ messages in thread
From: Danilo Krummrich @ 2025-09-03 17:22 UTC (permalink / raw)
  To: Sakari Ailus
  Cc: Jean-François Lessard, Wolfram Sang, Andy Shevchenko,
	Daniel Scally, Heikki Krogerus, Greg Kroah-Hartman,
	Rafael J. Wysocki, Javier Carrasco, linux-i2c, linux-kernel,
	linux-acpi

(Cc: Javier)

On Wed Sep 3, 2025 at 3:18 PM CEST, Sakari Ailus wrote:
> Do we really need the available variant?
>
> Please see
> <URL:https://lore.kernel.org/linux-acpi/Zwj12J5bTNUEnxA0@kekkonen.localdomain/>.
>
> I'll post a patch to remove fwnode_get_next_available_child_node(), too.

Either I'm missing something substantial or the link does indeed not provide an
obvious justification of why you want to send a patch to remove
fwnode_get_next_available_child_node().

Do you mean to say that all fwnode backends always return true for
device_is_available() and hence the fwnode API should not make this distinction?

I.e. are you referring to the fact that of_fwnode_get_next_child_node() always
calls of_get_next_available_child() and swnode has no device_is_available()
callback and hence is always available? What about ACPI?

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-09-03 17:22 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-02 19:04 [PATCH v4 0/2] device property: Add scoped fwnode child node iterators Jean-François Lessard
2025-09-02 19:04 ` [PATCH v4 1/2] " Jean-François Lessard
2025-09-02 19:11   ` Danilo Krummrich
2025-09-03 10:29   ` Andy Shevchenko
2025-09-03 13:18   ` Sakari Ailus
2025-09-03 16:43     ` Jean-François Lessard
2025-09-03 17:22     ` Danilo Krummrich
2025-09-02 19:04 ` [PATCH v4 2/2] i2c: core: Use fwnode_for_each_child_node_scoped() Jean-François Lessard
2025-09-03 13:19   ` Sakari Ailus
2025-09-03 10:30 ` [PATCH v4 0/2] device property: Add scoped fwnode child node iterators Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).