[PATCH 01/11] of: Add for_each_compatible_node_scoped() helper

Krzysztof Kozlowski posted 11 patches 1 month ago
There is a newer version of this series
[PATCH 01/11] of: Add for_each_compatible_node_scoped() helper
Posted by Krzysztof Kozlowski 1 month ago
Just like looping through children and available children, add a scoped
helper for for_each_compatible_node() so error paths can drop
of_node_put() leading to simpler code.

Suggested-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>

---

Prerequisite for all further patches.
---
 .clang-format      | 1 +
 include/linux/of.h | 7 +++++++
 2 files changed, 8 insertions(+)

diff --git a/.clang-format b/.clang-format
index c7060124a47a..1cc151e2adcc 100644
--- a/.clang-format
+++ b/.clang-format
@@ -259,6 +259,7 @@ ForEachMacros:
   - 'for_each_collection'
   - 'for_each_comp_order'
   - 'for_each_compatible_node'
+  - 'for_each_compatible_node_scoped'
   - 'for_each_component_dais'
   - 'for_each_component_dais_safe'
   - 'for_each_conduit'
diff --git a/include/linux/of.h b/include/linux/of.h
index 9bbdcf25a2b4..be6ec4916adf 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1485,6 +1485,13 @@ static inline int of_property_read_s32(const struct device_node *np,
 #define for_each_compatible_node(dn, type, compatible) \
 	for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
 	     dn = of_find_compatible_node(dn, type, compatible))
+
+#define for_each_compatible_node_scoped(dn, type, compatible) \
+	for (struct device_node *dn __free(device_node) =		\
+	     of_find_compatible_node(NULL, type, compatible);		\
+	     dn;							\
+	     dn = of_find_compatible_node(dn, type, compatible))
+
 #define for_each_matching_node(dn, matches) \
 	for (dn = of_find_matching_node(NULL, matches); dn; \
 	     dn = of_find_matching_node(dn, matches))

-- 
2.51.0
Re: [PATCH 01/11] of: Add for_each_compatible_node_scoped() helper
Posted by Jonathan Cameron 1 month ago
On Mon, 05 Jan 2026 14:33:39 +0100
Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com> wrote:

> Just like looping through children and available children, add a scoped
> helper for for_each_compatible_node() so error paths can drop
> of_node_put() leading to simpler code.
> 
> Suggested-by: Jonathan Cameron <jonathan.cameron@huawei.com>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>

Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Re: [PATCH 01/11] of: Add for_each_compatible_node_scoped() helper
Posted by Rob Herring 1 month ago
On Mon, Jan 05, 2026 at 02:33:39PM +0100, Krzysztof Kozlowski wrote:
> Just like looping through children and available children, add a scoped
> helper for for_each_compatible_node() so error paths can drop
> of_node_put() leading to simpler code.
> 
> Suggested-by: Jonathan Cameron <jonathan.cameron@huawei.com>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
> 
> ---
> 
> Prerequisite for all further patches.
> ---
>  .clang-format      | 1 +
>  include/linux/of.h | 7 +++++++
>  2 files changed, 8 insertions(+)

You need to update scripts/dtc/dt-extract-compatibles

> 
> diff --git a/.clang-format b/.clang-format
> index c7060124a47a..1cc151e2adcc 100644
> --- a/.clang-format
> +++ b/.clang-format
> @@ -259,6 +259,7 @@ ForEachMacros:
>    - 'for_each_collection'
>    - 'for_each_comp_order'
>    - 'for_each_compatible_node'
> +  - 'for_each_compatible_node_scoped'
>    - 'for_each_component_dais'
>    - 'for_each_component_dais_safe'
>    - 'for_each_conduit'
> diff --git a/include/linux/of.h b/include/linux/of.h
> index 9bbdcf25a2b4..be6ec4916adf 100644
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -1485,6 +1485,13 @@ static inline int of_property_read_s32(const struct device_node *np,
>  #define for_each_compatible_node(dn, type, compatible) \
>  	for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
>  	     dn = of_find_compatible_node(dn, type, compatible))
> +
> +#define for_each_compatible_node_scoped(dn, type, compatible) \
> +	for (struct device_node *dn __free(device_node) =		\
> +	     of_find_compatible_node(NULL, type, compatible);		\
> +	     dn;							\
> +	     dn = of_find_compatible_node(dn, type, compatible))
> +
>  #define for_each_matching_node(dn, matches) \
>  	for (dn = of_find_matching_node(NULL, matches); dn; \
>  	     dn = of_find_matching_node(dn, matches))
> 
> -- 
> 2.51.0
>
Re: [PATCH 01/11] of: Add for_each_compatible_node_scoped() helper
Posted by Krzysztof Kozlowski 1 month ago
On 05/01/2026 23:36, Rob Herring wrote:
> On Mon, Jan 05, 2026 at 02:33:39PM +0100, Krzysztof Kozlowski wrote:
>> Just like looping through children and available children, add a scoped
>> helper for for_each_compatible_node() so error paths can drop
>> of_node_put() leading to simpler code.
>>
>> Suggested-by: Jonathan Cameron <jonathan.cameron@huawei.com>
>> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
>>
>> ---
>>
>> Prerequisite for all further patches.
>> ---
>>  .clang-format      | 1 +
>>  include/linux/of.h | 7 +++++++
>>  2 files changed, 8 insertions(+)
> 
> You need to update scripts/dtc/dt-extract-compatibles

Right, ack.

Best regards,
Krzysztof