Skip to contentSkip to conversation

lib/vsprintf: Fix %pfwf when current node refcount == 0

PATCH In review Herve Codina 2 participants 2 messages Nov 14, 2023 – Dec 6, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 lib/vsprintf: Fix %pfwf when current node refcount == 0

A refcount issue can appeared in __fwnode_link_del() due to the pr_debug() call:

  WARNING: CPU: 0 PID: 901 at lib/refcount.c:25 refcount_warn_saturate+0xe5/0x110
  Call Trace:
  <TASK>
  ...
  of_node_get+0x1e/0x30
  of_fwnode_get+0x28/0x40
  fwnode_full_name_string+0x34/0x90
  fwnode_string+0xdb/0x140
  ...
  vsnprintf+0x17b/0x630
  ...
  __fwnode_link_del+0x25/0xa0
  fwnode_links_purge+0x39/0xb0
  of_node_release+0xd9/0x180
  ...

Indeed, an fwnode (of_node) is being destroyed and so, of_node_release() is called because the of_node refcount reached 0.
From of_node_release() several function calls are done and lead to a pr_debug() calls with %pfwf to print the fwnode full name. The issue is not present if we change %pfwf to %pfwP.

To print the full name, %pfwf iterates over the current node and its parents and obtain/drop a reference to all nodes involved.

In order to allow to print the full name (%pfwf) of a node while it is being destroyed, do not obtain/drop a reference to this current node.

Fixes a92eb7621b9f ("lib/vsprintf: Make use of fwnode API to obtain node names and separators")

---
Changes v2 -> v3
- Fix typo in comment ("ie parents node" -> "i.e. parent nodes")
- Add 'Reviewed-by: Sakari Ailus <[email protected]>'
- Add 'Reviewed-by: Andy Shevchenko <[email protected]>'

Changes v1 -> v2
- Avoid handling current node out of the loop. Instead obtain/drop references in the loop based on the depth value.
- Remove some of the backtrace lines in the commit log.

 lib/vsprintf.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
lib/vsprintf.c +8 -3
index afb88b24fa74..2aa408441cd3 100644
@@ -2110,15 +2110,20 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 
 	/* Loop starting from the root node to the current node. */
 	for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
-		struct fwnode_handle *__fwnode =
-			fwnode_get_nth_parent(fwnode, depth);
+		/*
+		 * Only get a reference for other nodes (i.e. parent nodes).
+		 * fwnode refcount may be 0 here.
+		 */
+		struct fwnode_handle *__fwnode = depth ?
+			fwnode_get_nth_parent(fwnode, depth) : fwnode;
 
 		buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
 			     default_str_spec);
 		buf = string(buf, end, fwnode_get_name(__fwnode),
 			     default_str_spec);
 
-		fwnode_handle_put(__fwnode);
+		if (depth)
+			fwnode_handle_put(__fwnode);
 	}
 
 	return buf;
Signature · 1 line
2.41.0
maintainer
#1 lore raw
Quoted 27 lines · Herve Codina

A refcount issue can appeared in __fwnode_link_del() due to the pr_debug() call:

  WARNING: CPU: 0 PID: 901 at lib/refcount.c:25 refcount_warn_saturate+0xe5/0x110
  Call Trace:
  <TASK>
  ...
  of_node_get+0x1e/0x30
  of_fwnode_get+0x28/0x40
  fwnode_full_name_string+0x34/0x90
  fwnode_string+0xdb/0x140
  ...
  vsnprintf+0x17b/0x630
  ...
  __fwnode_link_del+0x25/0xa0
  fwnode_links_purge+0x39/0xb0
  of_node_release+0xd9/0x180
  ...

Indeed, an fwnode (of_node) is being destroyed and so, of_node_release() is called because the of_node refcount reached 0.

Quoted 1 line

From of_node_release() several function calls are done and lead to

a pr_debug() calls with %pfwf to print the fwnode full name. The issue is not present if we change %pfwf to %pfwP.

To print the full name, %pfwf iterates over the current node and its parents and obtain/drop a reference to all nodes involved.

Quoted 8 lines

In order to allow to print the full name (%pfwf) of a node while it is being destroyed, do not obtain/drop a reference to this current node.

Fixes a92eb7621b9f ("lib/vsprintf: Make use of fwnode API to obtain node names and separators")

I have pushed the fix into printk/linux.git, branch for-6.8.

Please, let me know if you would prefer to push this into 6.7.

The "Fixes:" tag points to a pretty old commit from v5.5 so it does not look like a super urgent fix. And I always prefer when fixes spend some time in linux-next.

Best Regards,
Petr