Skip to contentSkip to conversation

mm: memcontrol-v1: use nofail allocations for soft limit trees

PATCH In review Ruoyu Wang 3 participants 5 messages Jun 8 – Jun 10

Subsystems (3)
lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH 1/1 mm: memcontrol-v1: use nofail allocations for soft limit trees

memcg1_init() allocates one soft-limit tree node per NUMA node and then initializes the returned object. If kzalloc_node() fails, the rb_root and lock initialization dereference NULL.

The per-node soft-limit tree is required by memcg v1. Use nofail GFP_KERNEL allocations for these init-time objects so the init path does not continue without the required tree nodes.

---
 mm/memcontrol-v1.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol-v1.c +2 -1
index 433bba9dfe715..3f41a15d8a8cf 100644
@@ -2246,7 +2246,8 @@ static int __init memcg1_init(void)
 	for_each_node(node) {
 		struct mem_cgroup_tree_per_node *rtpn;
 
-		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
+		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL | __GFP_NOFAIL,
+				    node);
 
 		rtpn->rb_root = RB_ROOT;
 		rtpn->rb_rightmost = NULL;
Signature · 1 line
2.51.0
#1 lore raw
Quoted 7 lines · Ruoyu Wang

memcg1_init() allocates one soft-limit tree node per NUMA node and then initializes the returned object. If kzalloc_node() fails, the rb_root and lock initialization dereference NULL.

The per-node soft-limit tree is required by memcg v1. Use nofail GFP_KERNEL allocations for these init-time objects so the init path does not continue without the required tree nodes.

This is an early init code executing in during boot. Have you really seen this allocation failing?

Quoted 21 lines
---
 mm/memcontrol-v1.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol-v1.c +2 -1
index 433bba9dfe715..3f41a15d8a8cf 100644
@@ -2246,7 +2246,8 @@ static int __init memcg1_init(void)
 	for_each_node(node) {
 		struct mem_cgroup_tree_per_node *rtpn;
 
-		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
+		rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL | __GFP_NOFAIL,
+				    node);
 
 		rtpn->rb_root = RB_ROOT;
 		rtpn->rb_rightmost = NULL;
Signature · 1 line
2.51.0
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#2 lore raw

No, I have not observed this allocation failing in practice.

This was found by static analysis and then checked by reading the code:
memcg1_init() dereferences rtpn unconditionally after kzalloc_node(). I treated the soft-limit tree as mandatory memcg v1 init state and used __GFP_NOFAIL because continuing without it would not be useful.

I agree this is early boot init code, and I do not have a runtime failure report or fault-injection reproduction for it. If such allocations are considered not worth handling in this path, feel free to drop the patch.

Thanks,
Ruoyu Wang

Quoted 40 lines · Michal Hocko
Quoted 2 lines · Ruoyu Wang

memcg1_init() allocates one soft-limit tree node per NUMA node and then initializes the returned object. If kzalloc_node() fails, the

rb_root

Quoted 5 lines

and lock initialization dereference NULL.

The per-node soft-limit tree is required by memcg v1. Use nofail GFP_KERNEL allocations for these init-time objects so the init path does not continue without the required tree nodes.

This is an early init code executing in during boot. Have you really seen this allocation failing?

Quoted 10 lines
---
 mm/memcontrol-v1.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol-v1.c +0 -0
index 433bba9dfe715..3f41a15d8a8cf 100644

mm/memcontrol-v1.c static int __init memcg1_init(void) · line 2249

Quoted 5 lines
mm/memcontrol-v1.c +1 -1
@@ -2246,7 +2246,8 @@ static int __init memcg1_init(void)
      for_each_node(node) {
              struct mem_cgroup_tree_per_node *rtpn;
 
-             rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node);
+             rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL |

__GFP_NOFAIL,

Quoted 6 lines
mm/memcontrol-v1.c +1 -1
+                                 node);
 
              rtpn->rb_root = RB_ROOT;
              rtpn->rb_rightmost = NULL;
--

2.51.0

--
Michal Hocko
SUSE Labs

#3 lore raw
Quoted 6 lines · Ruoyu Wang

No, I have not observed this allocation failing in practice.

This was found by static analysis and then checked by reading the code:
memcg1_init() dereferences rtpn unconditionally after kzalloc_node(). I treated the soft-limit tree as mandatory memcg v1 init state and used __GFP_NOFAIL because continuing without it would not be useful.

This should have been part of the changelog because it provides an insight into how have you reached your conclusion.

Quoted 4 lines

I agree this is early boot init code, and I do not have a runtime failure report or fault-injection reproduction for it. If such allocations are considered not worth handling in this path, feel free to drop the patch.

Yes, there is simply no point in handling these failures because early allocation failure like this one would very likely lead to massive failure before userspace is brought up anyway so there is no practical way to trigger the NULL ptr.

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#4 lore raw
In reply to Ruoyu Wang
Quoted 7 lines · On Mon, 8 Jun 2026 16:34:48 +0800 Ruoyu Wang <[email protected]> wrote:

This was found by static analysis and then checked by reading the code:
memcg1_init() dereferences rtpn unconditionally after kzalloc_node(). I treated the soft-limit tree as mandatory memcg v1 init state and used __GFP_NOFAIL because continuing without it would not be useful.

I agree this is early boot init code, and I do not have a runtime failure report or fault-injection reproduction for it.

Thanks.

Please teach the static analyzer that kernel practice is to ignore allocation failures in __init code.