drivers/base/node: Handle error properly in register_one_node()
Subsystems (2)
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.
---
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
@@ -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
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")Signed-off-by Donet Tom <[email protected]>---
Signature · 3 lines
Cheers David / dhildenb
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")Signed-off-by Donet Tom <[email protected]>---
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
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
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")Signed-off-by Donet Tom <[email protected]>---
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
-0drivers/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(), whendevice_register()fails, it callsput_device(&node->dev). Theput_device()call triggersnode_device_release()which does kfree(to_node(dev)), freeing the entire node structure. So whenregister_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
Andrew Morton
maintainerQuoted 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(), whendevice_register()fails, it callsput_device(&node->dev). Theput_device()call triggersnode_device_release()which does kfree(to_node(dev)), freeing the entire node structure. So whenregister_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
is the way to go.
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(), whendevice_register()fails, it callsput_device(&node->dev). Theput_device()call triggersnode_device_release()which does kfree(to_node(dev)), freeing the entire node structure. So whenregister_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.