Skip to contentSkip to conversation

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

PATCH In review Donet Tom 4 participants 6 messages Aug 22, 2025 – Sep 18, 2025

Subsystems (2)
lore
Share & tools
lore

Donet Tom

author
#0 lore raw

PATCH v2 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.

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

---
v1 -> v2
Made the changes based on Oscar’s review comments.

v1 - https://lore.kernel.org/all/[email protected]/

---
 drivers/base/node.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/base/node.c +5 -0
index c65b4917794e..1608816de67f 100644
@@ -883,6 +883,11 @@ int register_one_node(int nid)
 	node_devices[nid] = node;
 
 	error = register_node(node_devices[nid], nid);
+	if (error) {
+		node_devices[nid] = NULL;
+		kfree(node);
+		return error;
+	}
 
 	/* link cpu under this node */
 	for_each_present_cpu(cpu) {
Signature · 1 line
2.50.1
Acked-by on v2 1/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.

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

---

Signature · 3 lines
Cheers

David / dhildenb
#2 lore raw
In reply to Donet Tom
Quoted 23 lines · On Fri, 22 Aug 2025 14:18:45 +0530 Donet Tom <[email protected]> wrote:

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.

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

---
v1 -> v2
Made the changes based on Oscar’s review comments.

v1 - https://lore.kernel.org/all/[email protected]/

---
 drivers/base/node.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/base/node.c +0 -0
index c65b4917794e..1608816de67f 100644

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

Quoted 8 lines
drivers/base/node.c +5 -0
@@ -883,6 +883,11 @@ int register_one_node(int nid)
 	node_devices[nid] = node;
 
 	error = register_node(node_devices[nid], nid);
+	if (error) {
+		node_devices[nid] = NULL;
+		kfree(node);
+		return error;
+	}

Can this cause a double-free? Looking at register_node(), when device_register() fails, it calls put_device(&node->dev). The put_device() call triggers node_device_release() which does kfree(to_node(dev)), freeing the entire node structure. So when register_node() returns an error, the node memory is already freed, but this code calls kfree(node) again on the same memory.

The call chain is: register_node()->device_register() fails-> put_device()->node_device_release()->kfree(to_node(dev)).

[ This came from automated patch review, but it looks real to me ]

-chris

author
#3 lore raw
Quoted 40 lines · Chris Mason
Quoted 23 lines · On Fri, 22 Aug 2025 14:18:45 +0530 Donet Tom <[email protected]> wrote:

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.

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

---
v1 -> v2
Made the changes based on Oscar’s review comments.

v1 - https://lore.kernel.org/all/[email protected]/

---
  drivers/base/node.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/base/node.c +0 -0
index c65b4917794e..1608816de67f 100644

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

Quoted 8 lines
drivers/base/node.c +5 -0
@@ -883,6 +883,11 @@ int register_one_node(int nid)
  	node_devices[nid] = node;
 
  	error = register_node(node_devices[nid], nid);
+	if (error) {
+		node_devices[nid] = NULL;
+		kfree(node);
+		return error;
+	}

Can this cause a double-free? Looking at register_node(), when device_register() fails, it calls put_device(&node->dev). The put_device() call triggers node_device_release() which does kfree(to_node(dev)), freeing the entire node structure. So when register_node() returns an error, the node memory is already freed, but this code calls kfree(node) again on the same memory.

Quoted 2 lines

The call chain is: register_node()->device_register() fails-> put_device()->node_device_release()->kfree(to_node(dev)).

Thank you for pointing this out. I will address it and send a v3.

Quoted 5 lines

[ This came from automated patch review, but it looks real to me ]

-chris

maintainer
#4 lore raw
Quoted 7 lines · On Wed, 17 Sep 2025 20:25:48 +0530 Donet Tom <[email protected]> wrote:
Quoted 7 lines

Can this cause a double-free? Looking at register_node(), when device_register() fails, it calls put_device(&node->dev). The put_device() call triggers node_device_release() which does kfree(to_node(dev)), freeing the entire node structure. So when register_node() returns an error, the node memory is already freed, but this code calls kfree(node) again on the same memory.

Quoted 5 lines
Quoted 2 lines

The call chain is: register_node()->device_register() fails-> put_device()->node_device_release()->kfree(to_node(dev)).

Thank you for pointing this out. I will address it and send a v3.

This patch is now in mm.git's non-rebasing mm-stable branch, so no replacements, please.

A standalone patch with

Fixes 786eb990cfb7 ("drivers/base/node: handle error properly in register_one_node()")

is the way to go.

author
#5 lore raw
Quoted 13 lines · Andrew Morton
Quoted 11 lines · On Wed, 17 Sep 2025 20:25:48 +0530 Donet Tom <[email protected]> wrote:
Quoted 9 lines

Can this cause a double-free? Looking at register_node(), when device_register() fails, it calls put_device(&node->dev). The put_device() call triggers node_device_release() which does kfree(to_node(dev)), freeing the entire node structure. So when register_node() returns an error, the node memory is already freed, but this code calls kfree(node) again on the same memory.

The call chain is: register_node()->device_register() fails-> put_device()->node_device_release()->kfree(to_node(dev)).

Thank you for pointing this out. I will address it and send a v3.

Quoted 8 lines

This patch is now in mm.git's non-rebasing mm-stable branch, so no replacements, please.

A standalone patch with

Fixes 786eb990cfb7 ("drivers/base/node: handle error properly in register_one_node()")

is the way to go.

Sure Andrew .I will send it today.

Quoted 2 lines