Skip to contentSkip to conversation

drivers/base/node: Handle error properly in register_one_node()

PATCH Superseded Donet Tom 3 participants 5 messages Jul 2, 2025 – Jul 7, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Donet Tom

author
#0 lore raw

PATCH 1/1 drivers/base/node: Handle error properly in register_one_node()

If register_node() returns an error, it is not handled correctly. The function will proceed further and try to register CPUs under the node, which is not correct.

So, in this patch, if register_node() returns an error, we return immediately from the function.

---

This patch is based on the mm-unstable branch.

Fixes 76b67ed9dce6 ("[PATCH] node hotplug: register cpu: remove node struct")

The issue has been present since the above commit, which is quite old. Should I add a Fixes: tag and backport it to all kernels that have this commit?

---
 drivers/base/node.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/base/node.c +2 -0
index bef84f01712f..aec991b4c0b2 100644
@@ -885,6 +885,8 @@ int register_one_node(int nid)
 	node_devices[nid] = node;
 
 	error = register_node(node_devices[nid], nid);
+	if (error)
+		return error;
 
 	/* link cpu under this node */
 	for_each_present_cpu(cpu) {
Signature · 1 line
2.47.1
#1 lore raw
Quoted 10 lines · Donet Tom

If register_node() returns an error, it is not handled correctly. The function will proceed further and try to register CPUs under the node, which is not correct.

So, in this patch, if register_node() returns an error, we return immediately from the function.

---

...

drivers/base/node.c int register_one_node(int nid) · line 889

Quoted 10 lines
drivers/base/node.c +2 -0
index bef84f01712f..aec991b4c0b2 100644
@@ -885,6 +885,8 @@ int register_one_node(int nid)
 	node_devices[nid] = node;
 
 	error = register_node(node_devices[nid], nid);
+	if (error)
+		return error;

Ok, all current callers (based on mm-unstable) panic or BUG() if this fails, but powerpc, in init_phb_dynamic(), which keeps on going.
Unless it panics somewhere down the road as well.

So I think we need to:

node_devices[nid] = NULL
kfree(node)

?

Also, once Hannes fix lands, we might need that as well.

Anyway, I'd suggest you hold on until Hannes fix lands, so we can later rebase all your mem-hotplug on top of that [1].

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#mbf392eb390b8053f96be50da3b40dfd9b62dd389

Signature · 2 lines
Oscar Salvador
SUSE Labs
author
#2 lore raw
Quoted 26 lines · Oscar Salvador
Quoted 10 lines · Donet Tom

If register_node() returns an error, it is not handled correctly. The function will proceed further and try to register CPUs under the node, which is not correct.

So, in this patch, if register_node() returns an error, we return immediately from the function.

---

...

drivers/base/node.c int register_one_node(int nid) · line 889

Quoted 10 lines
drivers/base/node.c +2 -0
index bef84f01712f..aec991b4c0b2 100644
@@ -885,6 +885,8 @@ int register_one_node(int nid)
  	node_devices[nid] = node;
 
  	error = register_node(node_devices[nid], nid);
+	if (error)
+		return error;

Ok, all current callers (based on mm-unstable) panic or BUG() if this fails, but powerpc, in init_phb_dynamic(), which keeps on going.
Unless it panics somewhere down the road as well.

Quoted 6 lines

So I think we need to:

node_devices[nid] = NULL
kfree(node)

?

Yes, I will add this too.

But one question: if register_node() fails, is it okay to continue, or should we panic?

What is the correct way to handle this?

Quoted 4 lines

Also, once Hannes fix lands, we might need that as well.

Anyway, I'd suggest you hold on until Hannes fix lands, so we can later rebase all your mem-hotplug on top of that [1].

Sure

Quoted 4 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#mbf392eb390b8053f96be50da3b40dfd9b62dd389

#3 lore raw
Quoted 36 lines · Donet Tom
Quoted 26 lines · Oscar Salvador
Quoted 10 lines · Donet Tom

If register_node() returns an error, it is not handled correctly. The function will proceed further and try to register CPUs under the node, which is not correct.

So, in this patch, if register_node() returns an error, we return immediately from the function.

---

...

drivers/base/node.c int register_one_node(int nid) · line 889

Quoted 10 lines
drivers/base/node.c +2 -0
index bef84f01712f..aec991b4c0b2 100644
@@ -885,6 +885,8 @@ int register_one_node(int nid)
   	node_devices[nid] = node;
 
   	error = register_node(node_devices[nid], nid);
+	if (error)
+		return error;

Ok, all current callers (based on mm-unstable) panic or BUG() if this fails, but powerpc, in init_phb_dynamic(), which keeps on going.
Unless it panics somewhere down the road as well.

Quoted 6 lines

So I think we need to:

node_devices[nid] = NULL
kfree(node)

   ?
Quoted 6 lines

Yes, I will add this too.

But one question: if register_node() fails, is it okay to continue, or should we panic?

What is the correct way to handle this?

panic() or BUG() is not the answer :)

Try to recover ...

Signature · 3 lines
Cheers,

David / dhildenb
author
#4 lore raw
Quoted 38 lines · David Hildenbrand
Quoted 37 lines · Donet Tom
Quoted 25 lines · Oscar Salvador
Quoted 10 lines · Donet Tom

If register_node() returns an error, it is not handled correctly. The function will proceed further and try to register CPUs under the node, which is not correct.

So, in this patch, if register_node() returns an error, we return immediately from the function.

---

...

drivers/base/node.c int register_one_node(int nid) · line 888

Quoted 9 lines
drivers/base/node.c +2 -0
index bef84f01712f..aec991b4c0b2 100644
@@ -885,6 +885,8 @@ int register_one_node(int nid)
        node_devices[nid] = node;
           error = register_node(node_devices[nid], nid);
+    if (error)
+        return error;

Ok, all current callers (based on mm-unstable) panic or BUG() if this fails, but powerpc, in init_phb_dynamic(), which keeps on going.
Unless it panics somewhere down the road as well.

Quoted 6 lines

So I think we need to:

   node_devices[nid] = NULL
   kfree(node)

   ?

Yes, I will add this too.

Quoted 8 lines
Quoted 4 lines

But one question: if register_node() fails, is it okay to continue, or should we panic?

What is the correct way to handle this?

panic() or BUG() is not the answer :)

Try to recover ...

Got it, thank you very much, David.