lib/maple_tree: fix potential NULL dereference in mas_pop_node()
Subsystems (3)
PATCH 1/3 lib/maple_tree: fix potential NULL dereference in mas_pop_node()
If kmem_cache_alloc_from_sheaf() returns NULL (possible under GFP_NOWAIT pressure), mas_pop_node() falls through to the out label and dereferences the NULL pointer in memset(ret, 0, sizeof(*ret)).
Add a WARN_ON_ONCE NULL check after the sheaf allocation to bail out early, matching the existing pattern for the !mas->sheaf case above.
--- lib/maple_tree.c | 2 ++ 1 file changed, 2 insertions(+)
lib/maple_tree.c +2 -0
@@ -1063,6 +1063,8 @@ static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas)return NULL ; ret =kmem_cache_alloc_from_sheaf (maple_node_cache,GFP_NOWAIT , mas->sheaf); +if (WARN_ON_ONCE (!ret)) +return NULL ; out:memset (ret,0 ,sizeof (*ret));
Signature · 1 line
2.34.1
PATCH 2/3 lib/maple_tree: fix always-true condition in mas_erase()
The condition (!mas_is_active(mas) || !mas_is_start(mas)) is always true because ma_active and ma_start are distinct enum values -- a state can never be both simultaneously. This causes mas_erase() to unconditionally reset the status to ma_start, discarding a valid active walk position.
Change || to && so the reset only fires when the state is neither active nor start, matching the equivalent guard in mas_walk().
--- lib/maple_tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/maple_tree.c +1 -1
@@ -5574,7 +5574,7 @@ void *mas_erase(struct ma_state *mas)unsigned long index = mas->index;MA_WR_STATE (wr_mas, mas,NULL ); -if (!mas_is_active (mas) || !mas_is_start (mas)) +if (!mas_is_active (mas) && !mas_is_start (mas)) mas->status = ma_start; write_retry:
Signature · 1 line
2.34.1
Re: [PATCH 2/3] lib/maple_tree: fix always-true condition in mas_erase()
Quoted 8 lines · Josh Law
The condition (!mas_is_active(mas) || !mas_is_start(mas)) is always true because
ma_activeandma_startare distinct enum values -- a state can never be both simultaneously. This causesmas_erase()to unconditionally reset the status toma_start, discarding a valid active walk position.Change || to && so the reset only fires when the state is neither active nor start, matching the equivalent guard in
mas_walk().
This should be a function, then.
With a test case, if possible.
Quoted 24 lines
Signed-off-by Josh Law <[email protected]>--- lib/maple_tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/maple_tree.c +1
-1@@ -5574,7 +5574,7 @@ void *mas_erase(struct ma_state *mas)unsigned long index = mas->index;MA_WR_STATE (wr_mas, mas,NULL ); -if (!mas_is_active (mas) || !mas_is_start (mas)) +if (!mas_is_active (mas) && !mas_is_start (mas)) mas->status = ma_start; write_retry:Signature · 1 line
2.34.1
Andrew Morton
maintainerQuoted 3 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).
This is such a glaring bug that I wonder if we're missing something.
Quoted 13 lines
Add a
WARN_ON_ONCENULL check after the sheaf allocation to bail out early, matching the existing pattern for the !mas->sheaf case above.Signed-off-by Josh Law <[email protected]>--- lib/maple_tree.c | 2 ++ 1 file changed, 2 insertions(+)lib/maple_tree.c +0
-0
lib/maple_tree.c static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) · line 1067
Quoted 5 lines
lib/maple_tree.c +2
-0@@ -1063,6 +1063,8 @@ static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas)return NULL ; ret =kmem_cache_alloc_from_sheaf (maple_node_cache,GFP_NOWAIT , mas->sheaf); +if (WARN_ON_ONCE (!ret)) +return NULL ;
If we're going to do this then we may as well restore !__GFP_NOWARN, get more relevant information.
But a GFP_NOWAIT allocation attempt can fail relatively easily so callers must be equipped to handle it - perhaps no need for any warning.
12 Mar 2026 20:45:32 Andrew Morton <[email protected]>:
Quoted 30 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
Quoted 4 lines
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).This is such a glaring bug that I wonder if we're missing something.
Quoted 13 lines
Add a
WARN_ON_ONCENULL check after the sheaf allocation to bail out early, matching the existing pattern for the !mas->sheaf case above.Signed-off-by Josh Law <[email protected]>---
lib/maple_tree.c| 2 ++1 file changed, 2 insertions(+)lib/maple_tree.c +0
-0lib/maple_tree.c static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) · line 1067
Quoted 5 lines
lib/maple_tree.c +2
-0@@ -1063,6 +1063,8 @@ static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) return NULL; ret = kmem_cache_alloc_from_sheaf(maple_node_cache, GFP_NOWAIT, mas->sheaf); + if (WARN_ON_ONCE(!ret)) + return NULL;If we're going to do this then we may as well restore !__GFP_NOWARN, get more relevant information.
Quoted 3 lines
But a
GFP_NOWAITallocation attempt can fail relatively easily so callers must be equipped to handle it - perhaps no need for any warning.
Well, fair enough, but WARN_ON is equivalent to a "oops! Something went wrong! We will continue anyway", NOWARN is quite bad for logging that that went wrong, usually it's BUG_ON that causes said kernel panics and that, which is a bit overkill, that's why I didn't add it, and it warns once, then bails, that's why I'm a bit on the iffy side about adding NOWARN, what's your opinion on this, do you think a NOWARN is better then warn on once?
V/R
Josh law
12 Mar 2026 20:49:21 Josh Law <[email protected]>:
Quoted 36 lines
12 Mar 2026 20:45:32 Andrew Morton <[email protected]>:
Quoted 30 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
Quoted 4 lines
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).This is such a glaring bug that I wonder if we're missing something.
Quoted 13 lines
Add a
WARN_ON_ONCENULL check after the sheaf allocation to bail out early, matching the existing pattern for the !mas->sheaf case above.Signed-off-by Josh Law <[email protected]>---
lib/maple_tree.c| 2 ++1 file changed, 2 insertions(+)lib/maple_tree.c +0
-0lib/maple_tree.c static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) · line 1067
Quoted 5 lines
lib/maple_tree.c +2
-0@@ -1063,6 +1063,8 @@ static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) return NULL; ret = kmem_cache_alloc_from_sheaf(maple_node_cache, GFP_NOWAIT, mas->sheaf); + if (WARN_ON_ONCE(!ret)) + return NULL;If we're going to do this then we may as well restore !__GFP_NOWARN, get more relevant information.
Quoted 3 lines
But a
GFP_NOWAITallocation attempt can fail relatively easily so callers must be equipped to handle it - perhaps no need for any warning.
Quoted 8 lines
Well, fair enough, but
WARN_ONis equivalent to a "oops! Something went wrong! We will continue anyway", NOWARN is quite bad for logging that that went wrong, usually it'sBUG_ONthat causes said kernel panics and that, which is a bit overkill, that's why I didn't add it, and it warns once, then bails, that's why I'm a bit on the iffy side about adding NOWARN, what's your opinion on this, do you think a NOWARN is better then warn on once?V/R
Josh law
I checked the callers as you suggested. In lib/maple_tree.c at lines 2352 and 6039, mas_pop_node() is called inside loops where the return value is used immediately (passed to ma_mnode_ptr or bitwise-ORed) without any NULL validation. If kmem_cache_alloc_from_sheaf() fails under GFP_NOWAIT pressure, these callers will trigger a kernel panic.
12 Mar 2026 20:56:16 Josh Law <[email protected]>:
Quoted 41 lines
12 Mar 2026 20:49:21 Josh Law <[email protected]>:
Quoted 39 lines
12 Mar 2026 20:45:32 Andrew Morton <[email protected]>:
Quoted 30 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
Quoted 4 lines
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).This is such a glaring bug that I wonder if we're missing something.
Quoted 13 lines
Add a
WARN_ON_ONCENULL check after the sheaf allocation to bail out early, matching the existing pattern for the !mas->sheaf case above.Signed-off-by Josh Law <[email protected]>---
lib/maple_tree.c| 2 ++1 file changed, 2 insertions(+)lib/maple_tree.c +0
-0lib/maple_tree.c static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) · line 1067
Quoted 5 lines
lib/maple_tree.c +2
-0@@ -1063,6 +1063,8 @@ static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) return NULL; ret = kmem_cache_alloc_from_sheaf(maple_node_cache, GFP_NOWAIT, mas->sheaf); + if (WARN_ON_ONCE(!ret)) + return NULL;If we're going to do this then we may as well restore !__GFP_NOWARN, get more relevant information.
Quoted 3 lines
But a
GFP_NOWAITallocation attempt can fail relatively easily so callers must be equipped to handle it - perhaps no need for any warning.Well, fair enough, but
WARN_ONis equivalent to a "oops! Something went wrong! We will continue anyway", NOWARN is quite bad for logging that that went wrong, usually it'sBUG_ONthat causes said kernel panics and that, which is a bit overkill, that's why I didn't add it, and it warns once, then bails, that's why I'm a bit on the iffy side about adding NOWARN, what's your opinion on this, do you think a NOWARN is better then warn on once?
Quoted 8 lines
Quoted 5 lines
V/R
Josh law
I checked the callers as you suggested. In
lib/maple_tree.cat lines 2352 and 6039,mas_pop_node()is called inside loops where the return value is used immediately (passed toma_mnode_ptror bitwise-ORed) without any NULL validation. Ifkmem_cache_alloc_from_sheaf()fails underGFP_NOWAITpressure, these callers will trigger a kernel panic.
Yeah in my opinion I think this may need to be merged.. if you would like I can add the NOWARN
V/R
Alice Ryhl
reviewerQuoted 36 lines · Josh Law
12 Mar 2026 20:45:32 Andrew Morton <[email protected]>:
Quoted 30 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
Quoted 4 lines
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).This is such a glaring bug that I wonder if we're missing something.
Quoted 13 lines
Add a
WARN_ON_ONCENULL check after the sheaf allocation to bail out early, matching the existing pattern for the !mas->sheaf case above.Signed-off-by Josh Law <[email protected]>---
lib/maple_tree.c| 2 ++1 file changed, 2 insertions(+)lib/maple_tree.c +0
-0lib/maple_tree.c static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) · line 1067
Quoted 5 lines
lib/maple_tree.c +2
-0@@ -1063,6 +1063,8 @@ static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas) return NULL; ret = kmem_cache_alloc_from_sheaf(maple_node_cache, GFP_NOWAIT, mas->sheaf); + if (WARN_ON_ONCE(!ret)) + return NULL;If we're going to do this then we may as well restore !__GFP_NOWARN, get more relevant information.
Quoted 3 lines
But a
GFP_NOWAITallocation attempt can fail relatively easily so callers must be equipped to handle it - perhaps no need for any warning.
Quoted 7 lines
Well, fair enough, but
WARN_ONis equivalent to a "oops! Something went wrong! We will continue anyway", NOWARN is quite bad for logging that that went wrong, usually it'sBUG_ONthat causes said kernel panics and that, which is a bit overkill, that's why I didn't add it, and it warns once, then bails, that's why I'm a bit on the iffy side about adding NOWARN, what's your opinion on this, do you think a NOWARN is better then warn on once?
The WARN_ON option must only be used for conditions that indicate a kernel bug. Memory pressure is not a kernel bug, so WARN_ON is wrong here. In fact, depending on kernel configuration, WARN_ON_ONCE may crash the kernel.
Alice
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).This is such a glaring bug that I wonder if we're missing something.
According to my local copy of lib/maple_tree.c:
mas_pop_node() - Get a previously allocated maple node from the maple state.
Note the "previously" :) kmem_cache_alloc_from_sheaf() can only fail if you run out of objects in the sheaf.
So yeah, this "bug" looks bogus.
Signature · 1 line
Pedro
12 Mar 2026 23:22:48 Pedro Falcato <[email protected]>:
Quoted 13 lines · Andrew Morton
Quoted 7 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
Quoted 4 lines
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).This is such a glaring bug that I wonder if we're missing something.
According to my local copy of
lib/maple_tree.c:
mas_pop_node()- Get a previously allocated maple node from the maple state.
Quoted 7 lines
Note the "previously" :)
kmem_cache_alloc_from_sheaf()can only fail if you run out of objects in the sheaf.So yeah, this "bug" looks bogus.
--
Pedro
Hi Pedro,
I see the comment regarding 'previously allocated' nodes. However, mas_pop_node() explicitly calls kmem_cache_alloc_from_sheaf() with GFP_NOWAIT. If there is any path—even an unexpected one—where the sheaf is exhausted or the allocator fails, the code immediately performs a memset on the NULL pointer.
Even if this is a 'should never happen' scenario, returning NULL is safer than a kernel panic. As Andrew noted, the current structure allows a fall-through directly into a dereference. My patch ensures we handle that edge case safely.
Quoted 23 lines · Josh Law
12 Mar 2026 23:22:48 Pedro Falcato <[email protected]>:
Quoted 13 lines · Andrew Morton
Quoted 7 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
Quoted 4 lines
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).This is such a glaring bug that I wonder if we're missing something.
According to my local copy of
lib/maple_tree.c:
mas_pop_node()- Get a previously allocated maple node from the maple state.Quoted 7 lines
Note the "previously" :)
kmem_cache_alloc_from_sheaf()can only fail if you run out of objects in the sheaf.So yeah, this "bug" looks bogus.
--
Pedro
Quoted 7 lines
Hi Pedro,
I see the comment regarding 'previously allocated' nodes. However,
mas_pop_node()explicitly callskmem_cache_alloc_from_sheaf()withGFP_NOWAIT. If there is any path—even an unexpected one—where the sheaf is exhausted or the allocator fails, the code immediately performs a memset on the NULL pointer.
And? This does not happen, simply. If it does, your maple tree is hosed and, really, you're not recovering from it.
Quoted 5 lines
Even if this is a 'should never happen' scenario, returning NULL is safer than a kernel panic. As Andrew noted, the current structure allows a fall-through directly into a dereference. My patch ensures we handle that edge case safely.
... and now because none of the mas_pop_node() callers ever checks for NULL (why would they, they preallocated those same nodes before), you safely dereference NULL away from mas_pop_node!.
Signature · 1 line
Pedro
13 Mar 2026 09:05:37 Pedro Falcato <[email protected]>:
Quoted 41 lines · Josh Law
Quoted 23 lines
12 Mar 2026 23:22:48 Pedro Falcato <[email protected]>:
Quoted 13 lines · Andrew Morton
Quoted 7 lines · On Thu, 12 Mar 2026 18:40:53 +0000 Josh Law <[email protected]> wrote:
Quoted 4 lines
If
kmem_cache_alloc_from_sheaf()returns NULL (possible underGFP_NOWAITpressure),mas_pop_node()falls through to the out label and dereferences the NULL pointer inmemset(ret, 0, sizeof(*ret)).This is such a glaring bug that I wonder if we're missing something.
According to my local copy of
lib/maple_tree.c:
mas_pop_node()- Get a previously allocated maple node from the maple state.Quoted 7 lines
Note the "previously" :)
kmem_cache_alloc_from_sheaf()can only fail if you run out of objects in the sheaf.So yeah, this "bug" looks bogus.
--
PedroQuoted 7 lines
Hi Pedro,
I see the comment regarding 'previously allocated' nodes. However,
mas_pop_node()explicitly callskmem_cache_alloc_from_sheaf()withGFP_NOWAIT. If there is any path—even an unexpected one—where the sheaf is exhausted or the allocator fails, the code immediately performs a memset on the NULL pointer.And? This does not happen, simply. If it does, your maple tree is hosed and, really, you're not recovering from it.
Quoted 5 lines
Even if this is a 'should never happen' scenario, returning NULL is safer than a kernel panic. As Andrew noted, the current structure allows a fall-through directly into a dereference. My patch ensures we handle that edge case safely.
Quoted 6 lines
... and now because none of the
mas_pop_node()callers ever checks for NULL (why would they, they preallocated those same nodes before), you safely dereference NULL away frommas_pop_node!.--
Pedro
Hi Pedro,
I see your point regarding the invariants of the sheaf. If the pre-allocation logic is guaranteed, then a NULL return here implies a fundamental corruption of the maple state.
My concern was primarily the 'fall-through' structure which makes the dereference look accidental rather than intentional. However, if the callers aren't prepared to handle a NULL return anyway, simply returning NULL doesn't solve the underlying panic.
I'll take this as a lesson in understanding function invariants before jumping to defensive checks. Thanks for the technical explanation.
V/R
Josh law