[PATCH 5/7] of: do not use "%pOF" printk format on node with refcount of zero

From: Frank Rowand
Date: Mon Feb 13 2023 - 13:58:04 EST


of_node_release() can not use the "%pOF" printk format to report
the node name of a node when the node reference count is zero.
This is because the formatter device_node_string() calls
fwnode_full_name_string() which indirectly calls of_node_get().
Calling of_node_get() on the node with a zero reference count
results in a WARNING and stack trace.

When the reference count has been decremented to zero, this function
is in the subsequent call path which frees memory related to the node.

This commit resolves the unittest EXPECT errors that were created in
the previous commmit.

Signed-off-by: Frank Rowand <frowand.list@xxxxxxxxx>
---

The scripts/dtc/of_unittest_expect summary statistics before this commit:

** EXPECT statistics:
**
** non-zero values expected:
**
** EXPECT found : 50
** EXPECT_NOT not found : 4
**
** zero values expected:
**
** EXPECT not found : 0
** missing EXPECT begin : 5
** missing EXPECT end : 0
**
** EXPECT_NOT found : 0
** missing EXPECT_NOT begin : 0
** missing EXPECT_NOT end : 0
**
** unittest FAIL : 0
** internal error : 0

The scripts/dtc/of_unittest_expect summary statistics after this commit:

** EXPECT statistics:
**
** non-zero values expected:
**
** EXPECT found : 55
** EXPECT_NOT not found : 4
**
** zero values expected:
**
** EXPECT not found : 0
** missing EXPECT begin : 0
** missing EXPECT end : 0
**
** EXPECT_NOT found : 0
** missing EXPECT_NOT begin : 0
** missing EXPECT_NOT end : 0
**
** unittest FAIL : 0
** internal error : 0

drivers/of/dynamic.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index becb80f762c8..dbcbc41f3465 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -329,9 +329,17 @@ void of_node_release(struct kobject *kobj)
{
struct device_node *node = kobj_to_device_node(kobj);

+ /*
+ * can not use '"%pOF", node' in pr_err() calls from this function
+ * because an of_node_get(node) when refcount is already zero
+ * will result in an error and a stack dump
+ */
+
/* We should never be releasing nodes that haven't been detached. */
if (!of_node_check_flag(node, OF_DETACHED)) {
- pr_err("ERROR: Bad of_node_put() on %pOF\n", node);
+
+ pr_err("ERROR: %s() detected bad of_node_put() on %pOF/%s\n",
+ __func__, node->parent, node->full_name);

/*
* of unittests will test this path. Do not print the stack
--
Frank Rowand <frowand.list@xxxxxxxxx>