Skip to content

Commit 34af455

Browse files
jic23robherring
authored andcommitted
of: Introduce for_each_*_child_of_node_scoped() to automate of_node_put() handling
To avoid issues with out of order cleanup, or ambiguity about when the auto freed data is first instantiated, do it within the for loop definition. The disadvantage is that the struct device_node *child variable creation is not immediately obvious where this is used. However, in many cases, if there is another definition of struct device_node *child; the compiler / static analysers will notify us that it is unused, or uninitialized. Note that, in the vast majority of cases, the _available_ form should be used and as code is converted to these scoped handers, we should confirm that any cases that do not check for available have a good reason not to. Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Rob Herring <robh@kernel.org> Link: https://lore.kernel.org/r/20240225142714.286440-3-jic23@kernel.org Signed-off-by: Rob Herring <robh@kernel.org>
1 parent 9448e55 commit 34af455

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

include/linux/of.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,10 +1420,23 @@ static inline int of_property_read_s32(const struct device_node *np,
14201420
#define for_each_child_of_node(parent, child) \
14211421
for (child = of_get_next_child(parent, NULL); child != NULL; \
14221422
child = of_get_next_child(parent, child))
1423+
1424+
#define for_each_child_of_node_scoped(parent, child) \
1425+
for (struct device_node *child __free(device_node) = \
1426+
of_get_next_child(parent, NULL); \
1427+
child != NULL; \
1428+
child = of_get_next_child(parent, child))
1429+
14231430
#define for_each_available_child_of_node(parent, child) \
14241431
for (child = of_get_next_available_child(parent, NULL); child != NULL; \
14251432
child = of_get_next_available_child(parent, child))
14261433

1434+
#define for_each_available_child_of_node_scoped(parent, child) \
1435+
for (struct device_node *child __free(device_node) = \
1436+
of_get_next_available_child(parent, NULL); \
1437+
child != NULL; \
1438+
child = of_get_next_available_child(parent, child))
1439+
14271440
#define for_each_of_cpu_node(cpu) \
14281441
for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
14291442
cpu = of_get_next_cpu_node(cpu))

0 commit comments

Comments
 (0)