diff mbox series

[2/3] of: Add missing locking to of_(bus_)?n_(size|addr)_cells()

Message ID 20240530-dt-interrupt-map-fix-v1-2-2331d8732f08@kernel.org
State Changes Requested
Headers show
Series of: Reimplement of_(bus_)?n_(size|addr)_cells() | expand

Checks

Context Check Description
robh/checkpatch success
robh/patch-applied fail build log

Commit Message

Rob Herring May 31, 2024, 1:03 a.m. UTC
When accessing parent/child/sibling pointers the DT spinlock needs to
be held. The of_(bus_)?n_(size|addr)_cells() functions are missing that
when walking up the parent nodes. In reality, it rarely matters as most
nodes are static.

Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
 drivers/of/base.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

Comments

Rob Herring June 4, 2024, 4:08 p.m. UTC | #1
On Thu, May 30, 2024 at 08:03:28PM -0500, Rob Herring (Arm) wrote:
> When accessing parent/child/sibling pointers the DT spinlock needs to
> be held. The of_(bus_)?n_(size|addr)_cells() functions are missing that
> when walking up the parent nodes. In reality, it rarely matters as most
> nodes are static.
> 
> Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
> ---
>  drivers/of/base.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index 20603d3c9931..61fff13bbee5 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -91,8 +91,8 @@ int of_bus_n_addr_cells(struct device_node *np)
>  {
>  	u32 cells;
>  
> -	for (; np; np = np->parent)
> -		if (!of_property_read_u32(np, "#address-cells", &cells))
> +	for_each_parent_of_node_scoped(parent, np)
> +		if (!of_property_read_u32(parent, "#address-cells", &cells))
>  			return cells;
>  
>  	/* No #address-cells property for the root node */
> @@ -101,10 +101,9 @@ int of_bus_n_addr_cells(struct device_node *np)
>  
>  int of_n_addr_cells(struct device_node *np)
>  {
> -	if (np->parent)
> -		np = np->parent;

This isn't going to work... This drops of_n_addr_cells working for the 
root node. Callers wanting to get root node's properties need to use the 
of_bus_n variant instead, so those callers will have to be checked and 
fixed first.

Rob
diff mbox series

Patch

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 20603d3c9931..61fff13bbee5 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -91,8 +91,8 @@  int of_bus_n_addr_cells(struct device_node *np)
 {
 	u32 cells;
 
-	for (; np; np = np->parent)
-		if (!of_property_read_u32(np, "#address-cells", &cells))
+	for_each_parent_of_node_scoped(parent, np)
+		if (!of_property_read_u32(parent, "#address-cells", &cells))
 			return cells;
 
 	/* No #address-cells property for the root node */
@@ -101,10 +101,9 @@  int of_bus_n_addr_cells(struct device_node *np)
 
 int of_n_addr_cells(struct device_node *np)
 {
-	if (np->parent)
-		np = np->parent;
+	struct device_node *parent __free(device_node) = of_get_parent(np);
 
-	return of_bus_n_addr_cells(np);
+	return of_bus_n_addr_cells(parent);
 }
 EXPORT_SYMBOL(of_n_addr_cells);
 
@@ -112,8 +111,8 @@  int of_bus_n_size_cells(struct device_node *np)
 {
 	u32 cells;
 
-	for (; np; np = np->parent)
-		if (!of_property_read_u32(np, "#size-cells", &cells))
+	for_each_parent_of_node_scoped(parent, np)
+		if (!of_property_read_u32(parent, "#size-cells", &cells))
 			return cells;
 
 	/* No #size-cells property for the root node */
@@ -122,10 +121,9 @@  int of_bus_n_size_cells(struct device_node *np)
 
 int of_n_size_cells(struct device_node *np)
 {
-	if (np->parent)
-		np = np->parent;
+	struct device_node *parent __free(device_node) = of_get_parent(np);
 
-	return of_bus_n_size_cells(np);
+	return of_bus_n_size_cells(parent);
 }
 EXPORT_SYMBOL(of_n_size_cells);