Skip to contentSkip to conversation

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

PATCH Superseded Herve Codina 3 participants 8 messages Nov 14, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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>
  ? refcount_warn_saturate+0xe5/0x110
  ? __warn+0x81/0x130
  ? refcount_warn_saturate+0xe5/0x110
  ? report_bug+0x191/0x1c0
  ? srso_alias_return_thunk+0x5/0x7f
  ? prb_read_valid+0x1b/0x30
  ? handle_bug+0x3c/0x80
  ? exc_invalid_op+0x17/0x70
  ? asm_exc_invalid_op+0x1a/0x20
  ? refcount_warn_saturate+0xe5/0x110
  kobject_get+0x68/0x70
  of_node_get+0x1e/0x30
  of_fwnode_get+0x28/0x40
  fwnode_full_name_string+0x34/0x90
  fwnode_string+0xdb/0x140
  vsnprintf+0x17b/0x630
  va_format.isra.0+0x71/0x130
  vsnprintf+0x17b/0x630
  vprintk_store+0x162/0x4d0
  ? srso_alias_return_thunk+0x5/0x7f
  ? srso_alias_return_thunk+0x5/0x7f
  ? srso_alias_return_thunk+0x5/0x7f
  ? try_to_wake_up+0x9c/0x620
  ? rwsem_mark_wake+0x1b2/0x310
  vprintk_emit+0xe4/0x2b0
  _printk+0x5c/0x80
  __dynamic_pr_debug+0x131/0x160
  ? srso_alias_return_thunk+0x5/0x7f
  __fwnode_link_del+0x25/0xa0
  fwnode_links_purge+0x39/0xb0
  of_node_release+0xd9/0x180
  kobject_put+0x7b/0x190
  ...

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")
---
 lib/vsprintf.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/vsprintf.c +12 -2
index afb88b24fa74..74ef229c2783 100644
@@ -2108,8 +2108,8 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 {
 	int depth;
 
-	/* Loop starting from the root node to the current node. */
-	for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
+	/* Loop starting from the root node to the parent of current node. */
+	for (depth = fwnode_count_parents(fwnode); depth > 0; depth--) {
 		struct fwnode_handle *__fwnode =
 			fwnode_get_nth_parent(fwnode, depth);
 
@@ -2121,6 +2121,16 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 		fwnode_handle_put(__fwnode);
 	}
 
+	/* Handle current node without calling fwnode_handle_{get,put}().
+	 * This allows to print the full node name while the current node is
+	 * being destroyed (ie print from a function called because of
+	 * refcount == 0) without any refcount issues.
+	 */
+	buf = string(buf, end, fwnode_get_name_prefix(fwnode),
+		     default_str_spec);
+	buf = string(buf, end, fwnode_get_name(fwnode),
+		     default_str_spec);
+
 	return buf;
 }
 
Signature · 1 line
2.41.0
#1 lore raw

Hi Herve,

Thanks for the patch.

Quoted 63 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>
  ? refcount_warn_saturate+0xe5/0x110
  ? __warn+0x81/0x130
  ? refcount_warn_saturate+0xe5/0x110
  ? report_bug+0x191/0x1c0
  ? srso_alias_return_thunk+0x5/0x7f
  ? prb_read_valid+0x1b/0x30
  ? handle_bug+0x3c/0x80
  ? exc_invalid_op+0x17/0x70
  ? asm_exc_invalid_op+0x1a/0x20
  ? refcount_warn_saturate+0xe5/0x110
  kobject_get+0x68/0x70
  of_node_get+0x1e/0x30
  of_fwnode_get+0x28/0x40
  fwnode_full_name_string+0x34/0x90
  fwnode_string+0xdb/0x140
  vsnprintf+0x17b/0x630
  va_format.isra.0+0x71/0x130
  vsnprintf+0x17b/0x630
  vprintk_store+0x162/0x4d0
  ? srso_alias_return_thunk+0x5/0x7f
  ? srso_alias_return_thunk+0x5/0x7f
  ? srso_alias_return_thunk+0x5/0x7f
  ? try_to_wake_up+0x9c/0x620
  ? rwsem_mark_wake+0x1b2/0x310
  vprintk_emit+0xe4/0x2b0
  _printk+0x5c/0x80
  __dynamic_pr_debug+0x131/0x160
  ? srso_alias_return_thunk+0x5/0x7f
  __fwnode_link_del+0x25/0xa0
  fwnode_links_purge+0x39/0xb0
  of_node_release+0xd9/0x180
  kobject_put+0x7b/0x190
  ...

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")
---
 lib/vsprintf.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/vsprintf.c +0 -0
index afb88b24fa74..74ef229c2783 100644

lib/vsprintf.c char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, · line 2114

Quoted 9 lines
lib/vsprintf.c +2 -2
@@ -2108,8 +2108,8 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 {
 	int depth;
 
-	/* Loop starting from the root node to the current node. */
-	for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
+	/* Loop starting from the root node to the parent of current node. */
+	for (depth = fwnode_count_parents(fwnode); depth > 0; depth--) {
 		struct fwnode_handle *__fwnode =
 			fwnode_get_nth_parent(fwnode, depth);

How about, without changing the loop:

		/*
		 * Only get a reference for other nodes, fwnode refcount
		 * may be 0 here.
		 */
		struct fwnode_handle *__fwnode =
			depth ? fwnode_get_nth_parent(fwnode, depth) : fwnode;

lib/vsprintf.c char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, · line 2121

Quoted 3 lines
lib/vsprintf.c +0 -0
@@ -2121,6 +2121,16 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 		fwnode_handle_put(__fwnode);

And:

		if (__fwnode != fwnode)
			fwnode_handle_put(__fwnode);

lib/vsprintf.c char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, · line 2132

Quoted 11 lines
lib/vsprintf.c +9 -0
 	}
 
+	/* Handle current node without calling fwnode_handle_{get,put}().
+	 * This allows to print the full node name while the current node is
+	 * being destroyed (ie print from a function called because of
+	 * refcount == 0) without any refcount issues.
+	 */
+	buf = string(buf, end, fwnode_get_name_prefix(fwnode),
+		     default_str_spec);
+	buf = string(buf, end, fwnode_get_name(fwnode),
+		     default_str_spec);

It'd avoid duplicating this part, too, which I find worth the while.

Quoted 4 lines
lib/vsprintf.c +1 -0
+
 	return buf;
 }
 
Signature · 3 lines
Kind regards,

Sakari Ailus
#2 lore raw

Hi Sakari,

Quoted 23 lines · On Tue, 14 Nov 2023 11:28:43 +0000 Sakari Ailus <[email protected]>…

lib/vsprintf.c char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, · line 2114

Quoted 12 lines
lib/vsprintf.c +2 -2
@@ -2108,8 +2108,8 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 {
 	int depth;
 
-	/* Loop starting from the root node to the current node. */
-	for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
+	/* Loop starting from the root node to the parent of current node. */
+	for (depth = fwnode_count_parents(fwnode); depth > 0; depth--) {
 		struct fwnode_handle *__fwnode =
 			fwnode_get_nth_parent(fwnode, depth);  

How about, without changing the loop:

		/*
		 * Only get a reference for other nodes, fwnode refcount
		 * may be 0 here.
		 */
		struct fwnode_handle *__fwnode =
			depth ? fwnode_get_nth_parent(fwnode, depth) : fwnode;
Quoted 1 line
Quoted 8 lines

lib/vsprintf.c char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, · line 2121

Quoted 2 lines
lib/vsprintf.c +0 -0
@@ -2121,6 +2121,16 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 		fwnode_handle_put(__fwnode);  

And:

		if (__fwnode != fwnode)
			fwnode_handle_put(__fwnode);

Sure.
I will just change to keep the both tests consistent. I mean test with depth or test with __fwnode != fwnode but avoid mixing them.

What do you think about testing using depth in all cases and so:

	if (depth)
		fwnode_handle_put(__fwnode);

Best regards,
Hervé

#3 lore raw

Hi Herve,

Quoted 37 lines · Herve Codina

Hi Sakari,

Quoted 23 lines · On Tue, 14 Nov 2023 11:28:43 +0000 Sakari Ailus <[email protected]>…

lib/vsprintf.c char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, · line 2114

Quoted 12 lines
lib/vsprintf.c +2 -2
@@ -2108,8 +2108,8 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 {
 	int depth;
 
-	/* Loop starting from the root node to the current node. */
-	for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
+	/* Loop starting from the root node to the parent of current node. */
+	for (depth = fwnode_count_parents(fwnode); depth > 0; depth--) {
 		struct fwnode_handle *__fwnode =
 			fwnode_get_nth_parent(fwnode, depth);  

How about, without changing the loop:

		/*
		 * Only get a reference for other nodes, fwnode refcount
		 * may be 0 here.
		 */
		struct fwnode_handle *__fwnode =
			depth ? fwnode_get_nth_parent(fwnode, depth) : fwnode;
Quoted 1 line
Quoted 8 lines

lib/vsprintf.c char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, · line 2121

Quoted 2 lines
lib/vsprintf.c +0 -0
@@ -2121,6 +2121,16 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 		fwnode_handle_put(__fwnode);  

And:

		if (__fwnode != fwnode)
			fwnode_handle_put(__fwnode);
Quoted 8 lines

Sure.
I will just change to keep the both tests consistent. I mean test with depth or test with __fwnode != fwnode but avoid mixing them.

What do you think about testing using depth in all cases and so:

	if (depth)
		fwnode_handle_put(__fwnode);

I'd compare fwnodes as we're putting __fwnode since we've gotten a reference to fwnodes different from fwnode. I don't have a strong opinion on this though, up to you.

Signature · 3 lines
Regards,

Sakari Ailus
#4 lore raw
In reply to Herve Codina
Quoted 31 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>
  ? refcount_warn_saturate+0xe5/0x110
  ? __warn+0x81/0x130
  ? refcount_warn_saturate+0xe5/0x110
  ? report_bug+0x191/0x1c0
  ? srso_alias_return_thunk+0x5/0x7f
  ? prb_read_valid+0x1b/0x30
  ? handle_bug+0x3c/0x80
  ? exc_invalid_op+0x17/0x70
  ? asm_exc_invalid_op+0x1a/0x20
  ? refcount_warn_saturate+0xe5/0x110
  kobject_get+0x68/0x70
  of_node_get+0x1e/0x30
  of_fwnode_get+0x28/0x40
  fwnode_full_name_string+0x34/0x90
  fwnode_string+0xdb/0x140
  vsnprintf+0x17b/0x630
  va_format.isra.0+0x71/0x130
  vsnprintf+0x17b/0x630
  vprintk_store+0x162/0x4d0
  ? srso_alias_return_thunk+0x5/0x7f
  ? srso_alias_return_thunk+0x5/0x7f
  ? srso_alias_return_thunk+0x5/0x7f
  ? try_to_wake_up+0x9c/0x620
  ? rwsem_mark_wake+0x1b2/0x310
  vprintk_emit+0xe4/0x2b0
Quoted 8 lines
  _printk+0x5c/0x80
  __dynamic_pr_debug+0x131/0x160
  ? srso_alias_return_thunk+0x5/0x7f
  __fwnode_link_del+0x25/0xa0
  fwnode_links_purge+0x39/0xb0
  of_node_release+0xd9/0x180
  kobject_put+0x7b/0x190
  ...

Please, do not put so many unrelated lines of backtrace in the commit message. Leave only the important ones (the Submitting Patches document suggests some like ~3-5 lines only).

Quoted 11 lines

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 41 lines · On Tue, 14 Nov 2023 15:12:47 +0200 Andy Shevchenko…
Quoted 31 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>
  ? refcount_warn_saturate+0xe5/0x110
  ? __warn+0x81/0x130
  ? refcount_warn_saturate+0xe5/0x110
  ? report_bug+0x191/0x1c0
  ? srso_alias_return_thunk+0x5/0x7f
  ? prb_read_valid+0x1b/0x30
  ? handle_bug+0x3c/0x80
  ? exc_invalid_op+0x17/0x70
  ? asm_exc_invalid_op+0x1a/0x20
  ? refcount_warn_saturate+0xe5/0x110
  kobject_get+0x68/0x70
  of_node_get+0x1e/0x30
  of_fwnode_get+0x28/0x40
  fwnode_full_name_string+0x34/0x90
  fwnode_string+0xdb/0x140
  vsnprintf+0x17b/0x630
  va_format.isra.0+0x71/0x130
  vsnprintf+0x17b/0x630
  vprintk_store+0x162/0x4d0
  ? srso_alias_return_thunk+0x5/0x7f
  ? srso_alias_return_thunk+0x5/0x7f
  ? srso_alias_return_thunk+0x5/0x7f
  ? try_to_wake_up+0x9c/0x620
  ? rwsem_mark_wake+0x1b2/0x310
  vprintk_emit+0xe4/0x2b0
Quoted 8 lines
  _printk+0x5c/0x80
  __dynamic_pr_debug+0x131/0x160
  ? srso_alias_return_thunk+0x5/0x7f
  __fwnode_link_del+0x25/0xa0
  fwnode_links_purge+0x39/0xb0
  of_node_release+0xd9/0x180
  kobject_put+0x7b/0x190
  ...  
Quoted 3 lines

Please, do not put so many unrelated lines of backtrace in the commit message. Leave only the important ones (the Submitting Patches document suggests some like ~3-5 lines only).

Ok, I will remove some of them.

Best regards,
Hervé

#6 lore raw
Quoted 5 lines · Herve Codina
Quoted 3 lines · On Tue, 14 Nov 2023 15:12:47 +0200 Andy Shevchenko…
Quoted 2 lines · Herve Codina

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

---8<---

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
  WARNING: CPU: 0 PID: 901 at lib/refcount.c:25 refcount_warn_saturate+0xe5/0x110
  Call Trace:

  ? refcount_warn_saturate+0xe5/0x110
	...
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
  of_node_get+0x1e/0x30
  of_fwnode_get+0x28/0x40
  fwnode_full_name_string+0x34/0x90
  fwnode_string+0xdb/0x140
	...
Quoted 1 line
Quoted 1 line
Quoted 1 line

vsnprintf+0x17b/0x630

	...
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
  __fwnode_link_del+0x25/0xa0
  fwnode_links_purge+0x39/0xb0
  of_node_release+0xd9/0x180
	...

---8<---

Quoted 5 lines
Quoted 3 lines

Please, do not put so many unrelated lines of backtrace in the commit message. Leave only the important ones (the Submitting Patches document suggests some like ~3-5 lines only).

Ok, I will remove some of them.

Thanks (my suggestion is above).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Herve Codina
Quoted 3 lines · On Tue, 14 Nov 2023 15:12:47 +0200 Andy Shevchenko…
Quoted 2 lines · Herve Codina

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

---8<---

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
  WARNING: CPU: 0 PID: 901 at lib/refcount.c:25 refcount_warn_saturate+0xe5/0x110
  Call Trace:

  ? refcount_warn_saturate+0xe5/0x110
	...

These are actually not needed as duplicating WARNING above.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
  of_node_get+0x1e/0x30
  of_fwnode_get+0x28/0x40
  fwnode_full_name_string+0x34/0x90
  fwnode_string+0xdb/0x140
Quoted 8 lines
	...
Quoted 1 line
Quoted 1 line
Quoted 1 line

vsnprintf+0x17b/0x630

	...
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
  __fwnode_link_del+0x25/0xa0
  fwnode_links_purge+0x39/0xb0
  of_node_release+0xd9/0x180
	...

---8<---

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Please, do not put so many unrelated lines of backtrace in the commit message. Leave only the important ones (the Submitting Patches document suggests some like ~3-5 lines only).

Ok, I will remove some of them.

Thanks (my suggestion is above).

Signature · 2 lines
With Best Regards,
Andy Shevchenko