device property: Add scoped fwnode child node iterators
Subsystems (5)
v3 cover device property: Add scoped fwnode child node iterators
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 their usage by converting existing manual __free() usage in i2c-core-slave.c.
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 | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 1/2 device property: Add scoped fwnode child node iterators
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_named_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.
---
Notes:
checkpatch reports false positives that are intentionally ignored:
COMPLEX_MACRO, MACRO_ARG_REUSE, MACRO_ARG_PRECEDENCE This is a standard iterator pattern following kernel conventions.
include/linux/property.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/property.h +14 -0
@@ -176,6 +176,20 @@ 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_named_child_node_scoped (fwnode, child, name) \ + fwnode_for_each_child_node_scoped(fwnode, child) \ + for_each_if(fwnode_name_eq(child, name)) + +#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);
Signature · 1 line
2.43.0
Danilo Krummrich
maintainerQuoted 12 lines · Jean-François Lessard
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_named_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.
Quoted 6 lines
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 <[email protected]>
Thanks for adding a user and splitting it up (Andy was a bit faster than me :).
Quoted 6 lines
include/linux/property.h +0
-0@@ -176,6 +176,20 @@ struct fwnode_handle *fwnode_get_next_available_child_node( for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
include/linux/property.h struct fwnode_handle *fwnode_get_next_available_child_node( · line 186
Quoted 10 lines
include/linux/property.h +8
-0child = 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_named_child_node_scoped (fwnode, child, name) \ + fwnode_for_each_child_node_scoped(fwnode, child) \ + for_each_if(fwnode_name_eq(child, name))
IIRC, your first patch mentioned that your driver series would only use fwnode_for_each_available_child_node_scoped().
And this series adds a user for fwnode_for_each_child_node_scoped(); do you also have a user for fwnode_for_each_named_child_node_scoped()?
Le 1 septembre 2025 13 h 48 min 14 s HAE, Danilo Krummrich <[email protected]> a écrit :
Quoted 13 lines · Jean-François Lessard
Quoted 12 lines
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_named_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.
Quoted 9 lines
Quoted 6 lines
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 <[email protected]>Thanks for adding a user and splitting it up (Andy was a bit faster than me :).
Very welcome! Thanks for reviewing.
Quoted 10 lines
Quoted 10 lines
include/linux/property.h +2
-0@@ -176,6 +176,20 @@ 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); \
Quoted 8 lines
include/linux/property.h struct fwnode_handle *fwnode_get_next_available_child_node( · line 186
Quoted 5 lines
include/linux/property.h +5
-0+ child; child = fwnode_get_next_child_node(fwnode, child)) + +#define fwnode_for_each_named_child_node_scoped (fwnode, child, name) \ + fwnode_for_each_child_node_scoped(fwnode, child) \ + for_each_if(fwnode_name_eq(child, name))IIRC, your first patch mentioned that your driver series would only use
fwnode_for_each_available_child_node_scoped().
You are correct. Next version of TM16XX driver patch series will use fwnode_for_each_available_child_node_scoped()
Quoted 2 lines
And this series adds a user for
fwnode_for_each_child_node_scoped(); do you also have a user forfwnode_for_each_named_child_node_scoped()?
No, I haven't found an existing user that requires the scoped version. The only usage I found of the non-scoped fwnode_for_each_named_child_node() is in drivers/base/property.c in fwnode_get_named_child_node_count(), which doesn't need to put the fwnode.
I included it for consistency since the header defines all three non-scoped variants, but I understand the "no dead code" policy concern.
Would you prefer I drop the fwnode_for_each_named_child_node_scoped() variant and submit a v4 with only the two variants that have real users?
Regards,
Jean-François Lessard
Greg Kroah-Hartman
maintainerQuoted 56 lines · Jean-François Lessard
Le 1 septembre 2025 13 h 48 min 14 s HAE, Danilo Krummrich <[email protected]> a écrit :
Quoted 13 lines · Jean-François Lessard
Quoted 12 lines
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_named_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.
Quoted 9 lines
Quoted 6 lines
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 <[email protected]>Thanks for adding a user and splitting it up (Andy was a bit faster than me :).
Very welcome! Thanks for reviewing.
Quoted 10 lines
Quoted 10 lines
include/linux/property.h +2
-0@@ -176,6 +176,20 @@ 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); \Quoted 8 lines
include/linux/property.h struct fwnode_handle *fwnode_get_next_available_child_node( · line 186
Quoted 5 lines
include/linux/property.h +5
-0+ child; child = fwnode_get_next_child_node(fwnode, child)) + +#define fwnode_for_each_named_child_node_scoped (fwnode, child, name) \ + fwnode_for_each_child_node_scoped(fwnode, child) \ + for_each_if(fwnode_name_eq(child, name))IIRC, your first patch mentioned that your driver series would only use
fwnode_for_each_available_child_node_scoped().You are correct. Next version of TM16XX driver patch series will use
fwnode_for_each_available_child_node_scoped()Quoted 2 lines
And this series adds a user for
fwnode_for_each_child_node_scoped(); do you also have a user forfwnode_for_each_named_child_node_scoped()?No, I haven't found an existing user that requires the scoped version. The only usage I found of the non-scoped
fwnode_for_each_named_child_node()is indrivers/base/property.cinfwnode_get_named_child_node_count(), which doesn't need to put the fwnode.
Quoted 5 lines
I included it for consistency since the header defines all three non-scoped variants, but I understand the "no dead code" policy concern.
Would you prefer I drop the
fwnode_for_each_named_child_node_scoped()variant and submit a v4 with only the two variants that have real users?
Yes please.
Le 2 septembre 2025 00 h 50 min 08 s HAE, Greg Kroah-Hartman <[email protected]> a écrit :
Quoted 57 lines · Jean-François Lessard
Quoted 56 lines
Le 1 septembre 2025 13 h 48 min 14 s HAE, Danilo Krummrich <[email protected]> a écrit :
Quoted 13 lines · Jean-François Lessard
Quoted 12 lines
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_named_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.
Quoted 9 lines
Quoted 6 lines
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 <[email protected]>Thanks for adding a user and splitting it up (Andy was a bit faster than me :).
Very welcome! Thanks for reviewing.
Quoted 10 lines
Quoted 10 lines
include/linux/property.h +2
-0@@ -176,6 +176,20 @@ 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); \Quoted 8 lines
include/linux/property.h struct fwnode_handle *fwnode_get_next_available_child_node( · line 186
Quoted 5 lines
include/linux/property.h +5
-0+ child; child = fwnode_get_next_child_node(fwnode, child)) + +#define fwnode_for_each_named_child_node_scoped (fwnode, child, name) \ + fwnode_for_each_child_node_scoped(fwnode, child) \ + for_each_if(fwnode_name_eq(child, name))IIRC, your first patch mentioned that your driver series would only use
fwnode_for_each_available_child_node_scoped().You are correct. Next version of TM16XX driver patch series will use
fwnode_for_each_available_child_node_scoped()Quoted 2 lines
And this series adds a user for
fwnode_for_each_child_node_scoped(); do you also have a user forfwnode_for_each_named_child_node_scoped()?No, I haven't found an existing user that requires the scoped version. The only usage I found of the non-scoped
fwnode_for_each_named_child_node()is indrivers/base/property.cinfwnode_get_named_child_node_count(), which doesn't need to put the fwnode.
Quoted 7 lines
Quoted 5 lines
I included it for consistency since the header defines all three non-scoped variants, but I understand the "no dead code" policy concern.
Would you prefer I drop the
fwnode_for_each_named_child_node_scoped()variant and submit a v4 with only the two variants that have real users?Yes please.
Understood. I will do so.
PATCH v3 2/2 i2c: core: Use fwnode_for_each_child_node_scoped()
In reply to Jean-François LessardReplace 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.
--- drivers/i2c/i2c-core-slave.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/i2c/i2c-core-slave.c +1 -2
@@ -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" , ®);if (reg &I2C_OWN_SLAVE_ADDRESS )return true;
Signature · 1 line
2.43.0
Re: [PATCH v3 2/2] i2c: core: Use fwnode_for_each_child_node_scoped()
Quoted 6 lines · Jean-François Lessard
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.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 2/2] i2c: core: Use fwnode_for_each_child_node_scoped()
Le 2 septembre 2025 05 h 44 min 52 s HAE, Andy Shevchenko <[email protected]> a écrit :
Quoted 10 lines · Jean-François Lessard
Quoted 6 lines
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 <[email protected]>
Acknowledged with thanks. Will add this tag in the next submission.