Skip to contentSkip to conversation

of: fix of_node leak caused in of_find_node_opts_by_path

PATCH In review Qi Hou 2 participants 3 messages Feb 6, 2017 – Feb 10, 2017

Subsystems (3)
lore
Share & tools
lore

Qi Hou

author
#0 lore raw

PATCH 1/3 of: fix of_node leak caused in of_find_node_opts_by_path

During stepping down the tree, parent node is gotten first and its refcount is increased with of_node_get() in __of_get_next_child(). Since it just being used as tmp node, its refcount must be decreased with of_node_put() after traversing its child nodes.

Or, its refcount will never be descreased to ZERO, then it will never be freed, as well as other related memory blocks.

To fix this, decrease refcount of parent with of_node_put() after __of_find_node_by_path().

---
 drivers/of/base.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/of/base.c +3 -0
index d4bea3c..ce8206f 100644
@@ -842,8 +842,11 @@ struct device_node *of_find_node_opts_by_path(const char *path, const char **opt
 	if (!np)
 		np = of_node_get(of_root);
 	while (np && *path == '/') {
+		struct device_node *tmp = np;
+
 		path++; /* Increment past '/' delimiter */
 		np = __of_find_node_by_path(np, path);
+		of_node_put(tmp);
 		path = strchrnul(path, '/');
 		if (separator && separator < path)
 			break;
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 8 lines · Qi Hou

During stepping down the tree, parent node is gotten first and its refcount is increased with of_node_get() in __of_get_next_child(). Since it just being used as tmp node, its refcount must be decreased with of_node_put() after traversing its child nodes.

Or, its refcount will never be descreased to ZERO, then it will never be freed, as well as other related memory blocks.

Quoted 8 lines

To fix this, decrease refcount of parent with of_node_put() after __of_find_node_by_path().

---
 drivers/of/base.c | 3 +++
 1 file changed, 3 insertions(+)

It doesn't look like there is any inter-dependency with this patch and the other 2, so I'll apply this one.

Rob

author
#2 lore raw
Quoted 12 lines · Rob Herring
Quoted 11 lines · Qi Hou

During stepping down the tree, parent node is gotten first and its refcount is increased with of_node_get() in __of_get_next_child(). Since it just being used as tmp node, its refcount must be decreased with of_node_put() after traversing its child nodes.

Or, its refcount will never be descreased to ZERO, then it will never be freed, as well as other related memory blocks.

To fix this, decrease refcount of parent with of_node_put() after __of_find_node_by_path().

Quoted 7 lines
Quoted 5 lines
---
  drivers/of/base.c | 3 +++
  1 file changed, 3 insertions(+)

It doesn't look like there is any inter-dependency with this patch and the other 2, so I'll apply this one.

No, there is no inter-dependency in these three patches.

Just since they are all related with of_node_put and reported by kmemleak and debugged by the similar means, I sent them together.

-- Best regards,
Qi Hou

Quoted 2 lines

Rob