maple_tree fixes for 7.3
Subsystems (5)
Liam R. Howlett (Google)
maintainerv2 cover maple_tree fixes for 7.3
Two bugs found by an LLM review, and one problem Lorenzo Stoakes reported off list.
An RCU reader that restarts after its node dies can dereference NULL if the tree was emptied, or holds a single entry in the root pointer. Patch 1 fixes it and patch 2 adds a test.
With CONFIG_DEBUG_MAPLE_TREE, MT_WARN_ON() and the other WARN macros print the tree and call dump_stack() rather than WARN(), so syzbot never sees them. Patch 3 makes them WARN once per call site, after the dump, and makes the BUG macros BUG() after theirs. The userspace tests make WARN() fatal to keep failing as they did on dump_stack().
Changes since v1:
- Drop rhashtable patch. It is still undecided and will go to linux-crypto
- A reverse walk from index 0 returned the new root entry a second time; fixed and tested (Andrew's review bot)
- Fixes tags point at the commits that introduced the problems
- The WARN macros' once state is reset by clear_warn_once
- WARN() is fatal in maple-shared.h (Lorenzo); the maple test exits non-zero on a failure
Liam R. Howlett (Google) (3):
maple_tree: Fix rcu reader NULL dereference after the tree is emptied
maple_tree: Test emptying tree while RCU reading
maple_tree: Update internal warning and bug_on code
include/linux/maple_tree.h | 48 ++++---- lib/maple_tree.c | 112 ++++++++++++++----- lib/test_maple_tree.c | 165 ++++++++++++++++++++++++++++ tools/testing/radix-tree/maple.c | 13 ++- tools/testing/shared/maple-shared.h | 33 ++++++ 5 files changed, 316 insertions(+), 55 deletions(-)
Signature · 1 line
2.47.3
Liam R. Howlett (Google)
maintainerPATCH v2 1/3 maple_tree: Fix rcu reader NULL dereference after the tree is emptied
RCU readers may hit a NULL dereference if the tree is emptied while the walker is restarted from a dead node. Avoid the NULL dereference by checking for an emptied/pointer tree (0->ptr, 1-ULONG_MAX -> NULL) and set the maple state limits correctly.
A reader of /proc/<pid>/maps racing with an munmap() of the whole address space can hit this. The first implementation has the same problem in both directions.
Initial fix was generated by LLM and sent by Chris Mason in private.
--- lib/maple_tree.c | 112 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 85 insertions(+), 27 deletions(-)
lib/maple_tree.c +85 -27
@@ -3891,14 +3891,56 @@ static __always_inline void mas_rewalk(struct ma_state *mas, unsigned long index goto retry; } -static __always_inline bool mas_rewalk_if_dead(struct ma_state *mas, -struct maple_node *node,const unsigned long index) +/* +* mas_next_no_node() - Finish a next walk that restarted into a tree without +* nodes. +* @mas: The inactive maple state (ma_none or ma_root) +* +* The arrival here is either because there is a root value or nothing. Either +* way, the next entry is nothing. So set the limits and overwrite the status. +* +* Return: NULL +*/ +static inline void *mas_next_no_node (struct ma_state *mas) { -if (unlikely (ma_dead_node (node))) { -mas_rewalk (mas, index); -return true; +if (mas_is_ptr (mas)) + mas->index =1 ; +else + mas->index =0 ; + + mas->last =ULONG_MAX ; + mas->status = ma_none; +return NULL ; +} + +/* +* mas_prev_no_node() - Finish a prev walk that restarted into a tree without +* nodes. +* @mas: The inactive maple state (ma_none or ma_root) +* @index: The start of the range the walk left +* @min: The lowest index to return +* +* The arrival here is either because there is a root value or nothing. With +* nothing, leave the limits mas_walk() sets for an empty tree. A root value +* sits at 0: stop on it as mas_prev_setup() does, unless the walk was at 0. +* +* Return: The root entry if @index and @min allow it, NULL otherwise. A node +* if the root changed again since the walk; the caller restarts. +*/ +static inline void *mas_prev_no_node (struct ma_state *mas,unsigned long index, +unsigned long min) +{ + mas->index =0 ; +if (mas_is_none (mas)) { + mas->last =ULONG_MAX ; +return NULL ; } -return false; + + mas->last =0 ; +if (!index || min) +return NULL ; + +return mas_root (mas); }/*
@@ -4006,14 +4048,14 @@ static void *mas_prev_slot(struct ma_state *mas, unsigned long min, bool empty) node =mas_mn (mas); type =mte_node_type (mas->node); pivots =ma_pivots (node, type); -if (unlikely (mas_rewalk_if_dead (mas, node, save_point))) - goto retry; +if (unlikely (ma_dead_node (node))) + goto dead;if (mas->min <= min) { pivot =mas_safe_min (mas, pivots, mas->offset); -if (unlikely (mas_rewalk_if_dead (mas, node, save_point))) - goto retry; +if (unlikely (ma_dead_node (node))) + goto dead;if (pivot <= min) goto underflow;
@@ -4028,10 +4070,8 @@ static void *mas_prev_slot(struct ma_state *mas, unsigned long min, bool empty)if (mas->index <= min) goto underflow; -if (mas_prev_node (mas, min)) { -mas_rewalk (mas, save_point); - goto retry; - } +if (mas_prev_node (mas, min)) + goto dead;if (WARN_ON_ONCE (mas_is_underflow (mas)))return NULL ;
@@ -4045,8 +4085,8 @@ static void *mas_prev_slot(struct ma_state *mas, unsigned long min, bool empty) slots =ma_slots (node, type); entry =mas_slot (mas, slots, mas->offset); -if (unlikely (mas_rewalk_if_dead (mas, node, save_point))) - goto retry; +if (unlikely (ma_dead_node (node))) + goto dead;if (likely (entry))return entry;
@@ -4063,6 +4103,17 @@ static void *mas_prev_slot(struct ma_state *mas, unsigned long min, bool empty) underflow: mas->status = ma_underflow;return NULL ; + +dead: +mas_rewalk (mas, save_point); +if (likely (mas_is_active (mas))) + goto retry; + + entry =mas_prev_no_node (mas, save_point, min); +if (unlikely (xa_is_node (entry)))/* The root changed again */ + goto dead; + +return entry; }/*
@@ -4176,8 +4227,8 @@ static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty) node =mas_mn (mas); type =mte_node_type (mas->node); pivots =ma_pivots (node, type); -if (unlikely (mas_rewalk_if_dead (mas, node, save_point))) - goto retry; +if (unlikely (ma_dead_node (node))) + goto dead;if (mas->max >= max) {if (likely (mas->offset < mas->end))
@@ -4185,8 +4236,8 @@ static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty)else pivot = mas->max; -if (unlikely (mas_rewalk_if_dead (mas, node, save_point))) - goto retry; +if (unlikely (ma_dead_node (node))) + goto dead;if (pivot >= max) {/* Was at the limit, next will extend beyond */ mas->status = ma_overflow;
@@ -4208,10 +4259,8 @@ static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty)return NULL ; } -if (mas_next_node (mas, node, max)) { -mas_rewalk (mas, save_point); - goto retry; - } +if (mas_next_node (mas, node, max)) + goto dead;if (WARN_ON_ONCE (mas_is_overflow (mas)))return NULL ;
@@ -4226,8 +4275,8 @@ static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty) slots =ma_slots (node, type); entry =mt_slot (mas->tree, slots, mas->offset); -if (unlikely (mas_rewalk_if_dead (mas, node, save_point))) - goto retry; +if (unlikely (ma_dead_node (node))) + goto dead;if (entry)return entry;
@@ -4244,6 +4293,13 @@ static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty) }return entry; + +dead: +mas_rewalk (mas, save_point); +if (likely (mas_is_active (mas))) + goto retry; + +return mas_next_no_node (mas); }/*
@@ -5450,7 +5506,9 @@ void *mas_find(struct ma_state *mas, unsigned long max)/* Retries on dead nodes handled by mas_next_slot */ entry =mas_next_slot (mas, max, false);/* Ignore overflow */ - mas->status = ma_active; +if (mas_is_overflow (mas)) + mas->status = ma_active; +return entry; }EXPORT_SYMBOL_GPL (mas_find);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v2 1/3] maple_tree: Fix rcu reader NULL dereference after the tree is emptied
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] maple_tree: race in mas_prev_slot() when root is emptied after mas_rewalk() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] maple_tree: race in mas_prev_slot() when root is emptied after mas_rewalk() --
commit edc2da191bb5cf8aae2a20e02f12ca0a680681ac
Author: Liam R. Howlett (Google) <[email protected]>
maple_tree: Fix rcu reader NULL dereference after the tree is emptied
This commit fixes a NULL dereference when RCU readers restart a forward or reverse walk from a dead node after the maple tree has been emptied or reduced to a single root pointer.
Quoted 5 lines
lib/maple_tree.c +0
-0@@ -3891,14 +3891,56 @@ static __always_inline void mas_rewalk(struct ma_state *mas, unsigned long index
[ ... ]
Quoted 15 lines
lib/maple_tree.c +14
-0+static inline void *mas_prev_no_node (struct ma_state *mas,unsigned long index, +unsigned long min) +{ + mas->index =0 ; +if (mas_is_none (mas)) { + mas->last =ULONG_MAX ; +return NULL ; + } + + mas->last =0 ; +if (!index || min) +return NULL ; + +return mas_root (mas); }
[ ... ]
Quoted 6 lines
lib/maple_tree.c +2
-0@@ -4063,6 +4103,17 @@ static void *mas_prev_slot(struct ma_state *mas, unsigned long min, bool empty) underflow: mas->status = ma_underflow;return NULL ; + +dead:
lib/maple_tree.c static void *mas_prev_slot(struct ma_state *mas, unsigned long min, bool empty) · line 4117
Quoted 10 lines
lib/maple_tree.c +9
-0+mas_rewalk (mas, save_point); +if (likely (mas_is_active (mas))) + goto retry; + + entry =mas_prev_no_node (mas, save_point, min); +if (unlikely (xa_is_node (entry)))/* The root changed again */ + goto dead; + +return entry; }
[Severity: Medium]
Can this leave mas in an inconsistent state if the root is concurrently emptied between mas_rewalk() and the mas_root() read in mas_prev_no_node()?
When an RCU reader in mas_prev_slot() hits a dead node with save_point > 0 and min == 0, it jumps to dead: and runs:
mas_rewalk(mas, save_point)
-> mas_state_walk(mas)
-> mas_start(mas)If the tree holds a single pointer at index 0 at that moment, mas_start() sets mas->status = ma_root and returns NULL because mas->index > 0.
Back in mas_prev_slot(), mas_prev_no_node() sees mas->status == ma_root, sets mas->index = 0 and mas->last = 0, and performs a second RCU read via mas_root(mas).
If a concurrent writer finishes emptying the tree (setting ma_root to NULL) before that second mas_root(mas) read, mas_prev_no_node() returns NULL. Because mas_prev_slot() only checks xa_is_node(entry) to detect a concurrent root change, it returns NULL with mas->status == ma_root, mas->index == 0, and mas->last == 0 instead of mas->status == ma_none and mas->last == ULONG_MAX.
Could this leave callers like mas_prev_range() or mas_find_range_rev() with a bogus [0, 0] range, or cause a subsequent forward walk on the same state to skip index 0?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2 1/3] maple_tree: Fix rcu reader NULL dereference after the tree is emptied
Quoted 29 lines · On Tue, 03 Nov 2026 03:24:58 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium]maple_tree: race inmas_prev_slot()when root is emptied aftermas_rewalk()--[Severity: Medium]
Can this leave mas in an inconsistent state if the root is concurrently emptied betweenmas_rewalk()and themas_root()read inmas_prev_no_node()?When an RCU reader in
mas_prev_slot()hits a dead node withsave_point> 0 and min == 0, it jumps to dead: and runs:mas_rewalk(mas, save_point) -> mas_state_walk(mas) -> mas_start(mas)If the tree holds a single pointer at index 0 at that moment,
mas_start()setsmas->status=ma_rootand returns NULL becausemas->index> 0.Back in
mas_prev_slot(),mas_prev_no_node()seesmas->status==ma_root, setsmas->index= 0 andmas->last= 0, and performs a second RCU read viamas_root(mas).If a concurrent writer finishes emptying the tree (setting
ma_rootto NULL) before that secondmas_root(mas)read,mas_prev_no_node()returns NULL. Becausemas_prev_slot()only checksxa_is_node(entry)to detect a concurrent root change, it returns NULL withmas->status==ma_root,mas->index== 0, andmas->last== 0 instead ofmas->status==ma_noneandmas->last==ULONG_MAX.
Quoted 3 lines
Could this leave callers like
mas_prev_range()ormas_find_range_rev()with a bogus [0, 0] range, or cause a subsequent forward walk on the same state to skip index 0?
I plan to squash this fix into the base patch ("maple_tree: Fix rcu reader NULL dereference after the tree is emptied"):
From: Andrew Morton <[email protected]>
Subject: maple_tree: fix reverse iterator state when the root disappears
Date: Tue, 03 Nov 2026 04:28:56 +0000
A reverse walk restarting from a dead node can observe a direct root entry in mas_rewalk() and then an empty root in mas_prev_no_node(). The second root read returns NULL, but leaves the iterator in ma_root with index and last both zero.
Consequently, mas_prev_range() and mas_find_range_rev() can report an empty range of [0, 0] instead of [0, ULONG_MAX]. A subsequent forward range walk starts at index 1, omitting index 0 from the empty range.
Set ma_none and extend last to ULONG_MAX when the second root read returns NULL. Keep the existing encoded-node retry in mas_prev_slot() for a root that has grown into a node again.
A deterministic userspace Maple test reproduces the stale state through both reverse range APIs before this change and checks the full empty range afterwards, including a subsequent forward walk.
---
lib/maple_tree.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
lib/maple_tree.c renamed from lib/maple_tree.c~maple_tree-fix-reverse-iterator-state-when-the-root-disappears +9 -1
@@ -3930,6 +3930,8 @@ static inline void *mas_next_no_node(strstatic inline void *mas_prev_no_node (struct ma_state *mas,unsigned long index,unsigned long min) { +void *entry; + mas->index =0 ;if (mas_is_none (mas)) { mas->last =ULONG_MAX ;
@@ -3940,7 +3942,13 @@ static inline void *mas_prev_no_node(strif (!index || min)return NULL ; -return mas_root (mas); + entry =mas_root (mas); +if (!entry) { + mas->status = ma_none; + mas->last =ULONG_MAX ; + } + +return entry; }/*
_
Liam R. Howlett (Google)
maintainerPATCH v2 2/3 maple_tree: Test emptying tree while RCU reading
In reply to Liam R. Howlett (Google)Add a test case for the rcu reader being restarted by emptying the tree. A reverse walk from index 0 must not return the new root entry.
--- lib/test_maple_tree.c | 165 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+)
lib/test_maple_tree.c +165 -0
@@ -3740,6 +3740,170 @@ static noinline void __init check_range64_in_rcu(struct maple_tree *mt)rcu_barrier (); } +/* +* A reader that restarts after its leaf died may find a tree without nodes: +* emptied, or emptied and then given a single entry at 0. Each case starts +* from a tree with nodes, built by check_seq(). The stores run while the +* reader holds rcu_read_lock, so they use GFP_ATOMIC. +*/ +static noinline void __init check_restart_no_node(struct maple_tree *mt) +{ +void *ptr =xa_mk_value (1234 ); +void *entry; +MA_STATE (mas, mt,0 ,0 ); + +MT_BUG_ON (mt, !mtree_empty (mt)); +mt_set_non_kernel (99999 ); + +/* find: the tree is emptied */ +mt_init_flags (mt,MT_FLAGS_USE_RCU ); +check_seq (mt,100 , false); +rcu_read_lock (); +mas_set (&mas,0 ); + entry =mas_find (&mas,ULONG_MAX ); +MT_BUG_ON (mt, entry !=xa_mk_value (0 )); +MT_BUG_ON (mt,mtree_store_range (mt,0 ,ULONG_MAX ,NULL ,GFP_ATOMIC )); + entry =mas_find (&mas,ULONG_MAX ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_none); +MT_BUG_ON (mt, mas.index !=0 ); +MT_BUG_ON (mt, mas.last !=ULONG_MAX ); + entry =mas_find (&mas,ULONG_MAX ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_none); +rcu_read_unlock (); +mtree_destroy (mt); +rcu_barrier (); + +/* next: the tree is emptied */ +mt_init_flags (mt,MT_FLAGS_USE_RCU ); +check_seq (mt,100 , false); +rcu_read_lock (); +mas_set (&mas,0 ); + entry =mas_find (&mas,ULONG_MAX ); +MT_BUG_ON (mt, entry !=xa_mk_value (0 )); +MT_BUG_ON (mt,mtree_store_range (mt,0 ,ULONG_MAX ,NULL ,GFP_ATOMIC )); + entry =mas_next (&mas,ULONG_MAX ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_none); +MT_BUG_ON (mt, mas.index !=0 ); +MT_BUG_ON (mt, mas.last !=ULONG_MAX ); +rcu_read_unlock (); +mtree_destroy (mt); +rcu_barrier (); + +/* find: the tree is emptied, then holds one entry at 0 */ +mt_init_flags (mt,MT_FLAGS_USE_RCU ); +check_seq (mt,100 , false); +rcu_read_lock (); +mas_set (&mas,0 ); + entry =mas_find (&mas,ULONG_MAX ); +MT_BUG_ON (mt, entry !=xa_mk_value (0 )); +MT_BUG_ON (mt,mtree_store_range (mt,0 ,ULONG_MAX ,NULL ,GFP_ATOMIC )); +MT_BUG_ON (mt,mtree_store (mt,0 , ptr,GFP_ATOMIC )); + entry =mas_find (&mas,ULONG_MAX ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_none); +MT_BUG_ON (mt, mas.index !=1 ); +MT_BUG_ON (mt, mas.last !=ULONG_MAX ); +rcu_read_unlock (); +mtree_destroy (mt); +rcu_barrier (); + +/* find_rev: the tree is emptied */ +mt_init_flags (mt,MT_FLAGS_USE_RCU ); +check_seq (mt,100 , false); +rcu_read_lock (); +mas_set (&mas,ULONG_MAX ); + entry =mas_find_rev (&mas,0 ); +MT_BUG_ON (mt, entry !=xa_mk_value (100 )); +MT_BUG_ON (mt,mtree_store_range (mt,0 ,ULONG_MAX ,NULL ,GFP_ATOMIC )); + entry =mas_find_rev (&mas,0 ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_none); +MT_BUG_ON (mt, mas.index !=0 ); +MT_BUG_ON (mt, mas.last !=ULONG_MAX ); + entry =mas_find_rev (&mas,0 ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_none); +rcu_read_unlock (); +mtree_destroy (mt); +rcu_barrier (); + +/* prev: the tree is emptied, then holds one entry at 0 */ +mt_init_flags (mt,MT_FLAGS_USE_RCU ); +check_seq (mt,100 , false); +rcu_read_lock (); +mas_set (&mas,ULONG_MAX ); + entry =mas_find_rev (&mas,0 ); +MT_BUG_ON (mt, entry !=xa_mk_value (100 )); +MT_BUG_ON (mt,mtree_store_range (mt,0 ,ULONG_MAX ,NULL ,GFP_ATOMIC )); +MT_BUG_ON (mt,mtree_store (mt,0 , ptr,GFP_ATOMIC )); + entry =mas_prev (&mas,0 ); +MT_BUG_ON (mt, entry != ptr); +MT_BUG_ON (mt, mas.status != ma_root); +MT_BUG_ON (mt, mas.index !=0 ); +MT_BUG_ON (mt, mas.last !=0 ); +rcu_read_unlock (); +mtree_destroy (mt); +rcu_barrier (); + +/* find_rev: the entry at 0 is below the minimum */ +mt_init_flags (mt,MT_FLAGS_USE_RCU ); +check_seq (mt,100 , false); +rcu_read_lock (); +mas_set (&mas,ULONG_MAX ); + entry =mas_find_rev (&mas,1 ); +MT_BUG_ON (mt, entry !=xa_mk_value (100 )); +MT_BUG_ON (mt,mtree_store_range (mt,0 ,ULONG_MAX ,NULL ,GFP_ATOMIC )); +MT_BUG_ON (mt,mtree_store (mt,0 , ptr,GFP_ATOMIC )); + entry =mas_find_rev (&mas,1 ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_root); +MT_BUG_ON (mt, mas.index !=0 ); +MT_BUG_ON (mt, mas.last !=0 ); +rcu_read_unlock (); +mtree_destroy (mt); +rcu_barrier (); + +/* find_rev: the reader is at 0 when the tree gets one entry at 0 */ +mt_init_flags (mt,MT_FLAGS_USE_RCU ); +check_seq (mt,100 , false); +rcu_read_lock (); +mas_set (&mas,0 ); + entry =mas_find_rev (&mas,0 ); +MT_BUG_ON (mt, entry !=xa_mk_value (0 )); +MT_BUG_ON (mt,mtree_store_range (mt,0 ,ULONG_MAX ,NULL ,GFP_ATOMIC )); +MT_BUG_ON (mt,mtree_store (mt,0 , ptr,GFP_ATOMIC )); + entry =mas_find_rev (&mas,0 ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_root); +MT_BUG_ON (mt, mas.index !=0 ); +MT_BUG_ON (mt, mas.last !=0 ); +rcu_read_unlock (); +mtree_destroy (mt); +rcu_barrier (); + +/* find_range_rev: the reader is at 0 when the tree gets one entry at 0 */ +mt_init_flags (mt,MT_FLAGS_USE_RCU ); +check_seq (mt,100 , false); +rcu_read_lock (); +mas_set (&mas,0 ); + entry =mas_find_range_rev (&mas,0 ); +MT_BUG_ON (mt, entry !=xa_mk_value (0 )); +MT_BUG_ON (mt,mtree_store_range (mt,0 ,ULONG_MAX ,NULL ,GFP_ATOMIC )); +MT_BUG_ON (mt,mtree_store (mt,0 , ptr,GFP_ATOMIC )); + entry =mas_find_range_rev (&mas,0 ); +MT_BUG_ON (mt, entry !=NULL ); +MT_BUG_ON (mt, mas.status != ma_root); +MT_BUG_ON (mt, mas.index !=0 ); +MT_BUG_ON (mt, mas.last !=0 ); +rcu_read_unlock (); +mtree_destroy (mt); +rcu_barrier (); +mt_set_non_kernel (0 ); +} +static DEFINE_MTREE (tree);static int __init maple_tree_seed (void ) {
@@ -4031,6 +4195,7 @@ static int __init maple_tree_seed(void)check_range64_in_rcu (&tree); +check_restart_no_node (&tree);#if defined(BENCH ) skip:
Signature · 1 line
2.47.3
Liam R. Howlett (Google)
maintainerPATCH v2 3/3 maple_tree: Update internal warning and bug_on code
In reply to Liam R. Howlett (Google)When CONFIG_DEBUG_MAPLE_TREE is set, call WARN_ON() once per site after the custom information is dumped about the tree. Without using the kernel's WARN_ON calls, syzbot will not catch issues detected by this code. The BUG_ON variants now stop instead of dumping and continuing.
Update the non-debug code to call WARN_ON_ONCE() as well.
The userspace tests asserted on dump_stack() but only print on WARN_ON(), so make WARN() fatal in maple-shared.h and have the maple test exit with the result of maple_tree_seed().
--- include/linux/maple_tree.h | 48 ++++++++++++++--------------- tools/testing/radix-tree/maple.c | 13 +++++--- tools/testing/shared/maple-shared.h | 33 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 28 deletions(-)
include/linux/maple_tree.h +24 -24
@@ -680,7 +680,7 @@ void mt_cache_shrink(void);pr_info ("Pass: %u Run:%u\n" , \atomic_read (&maple_tree_tests_passed), \atomic_read (&maple_tree_tests_run)); \ -dump_stack (); \ +BUG_ON (1 ); \ }else { \atomic_inc (&maple_tree_tests_passed); \ } \
@@ -696,7 +696,7 @@ void mt_cache_shrink(void);pr_info ("Pass: %u Run:%u\n" , \atomic_read (&maple_tree_tests_passed), \atomic_read (&maple_tree_tests_run)); \ -dump_stack (); \ +BUG_ON (1 ); \ }else { \atomic_inc (&maple_tree_tests_passed); \ } \
@@ -713,7 +713,7 @@ void mt_cache_shrink(void);pr_info ("Pass: %u Run:%u\n" , \atomic_read (&maple_tree_tests_passed), \atomic_read (&maple_tree_tests_run)); \ -dump_stack (); \ +BUG_ON (1 ); \ }else { \atomic_inc (&maple_tree_tests_passed); \ } \
@@ -722,16 +722,16 @@ void mt_cache_shrink(void);#define MT_WARN_ON (__tree, __x) ({ \ int ret = !!(__x); \ atomic_inc(&maple_tree_tests_run); \ - if (ret) { \ - pr_info("WARN at %s:%d (%u)\n", \ - __func__, __LINE__, __x); \ + if (!ret) { \ + atomic_inc(&maple_tree_tests_passed); \ + } else if (__ONCE_LITE_IF(ret)) { \ + pr_info("WARN at %s:%d\n", \ + __func__, __LINE__); \ mt_dump(__tree, mt_dump_hex); \ pr_info("Pass: %u Run:%u\n", \ atomic_read(&maple_tree_tests_passed), \ atomic_read(&maple_tree_tests_run)); \ - dump_stack(); \ - } else { \ - atomic_inc(&maple_tree_tests_passed); \ + WARN_ON(1); \ } \ unlikely(ret); \ })
@@ -739,17 +739,17 @@ void mt_cache_shrink(void);#define MAS_WARN_ON (__mas, __x) ({ \ int ret = !!(__x); \ atomic_inc(&maple_tree_tests_run); \ - if (ret) { \ - pr_info("WARN at %s:%d (%u)\n", \ - __func__, __LINE__, __x); \ + if (!ret) { \ + atomic_inc(&maple_tree_tests_passed); \ + } else if (__ONCE_LITE_IF(ret)) { \ + pr_info("WARN at %s:%d\n", \ + __func__, __LINE__); \ mas_dump(__mas); \ mt_dump((__mas)->tree, mt_dump_hex); \ pr_info("Pass: %u Run:%u\n", \ atomic_read(&maple_tree_tests_passed), \ atomic_read(&maple_tree_tests_run)); \ - dump_stack(); \ - } else { \ - atomic_inc(&maple_tree_tests_passed); \ + WARN_ON(1); \ } \ unlikely(ret); \ })
@@ -757,18 +757,18 @@ void mt_cache_shrink(void);#define MAS_WR_WARN_ON (__wrmas, __x) ({ \ int ret = !!(__x); \ atomic_inc(&maple_tree_tests_run); \ - if (ret) { \ - pr_info("WARN at %s:%d (%u)\n", \ - __func__, __LINE__, __x); \ + if (!ret) { \ + atomic_inc(&maple_tree_tests_passed); \ + } else if (__ONCE_LITE_IF(ret)) { \ + pr_info("WARN at %s:%d\n", \ + __func__, __LINE__); \ mas_wr_dump(__wrmas); \ mas_dump((__wrmas)->mas); \ mt_dump((__wrmas)->mas->tree, mt_dump_hex); \ pr_info("Pass: %u Run:%u\n", \ atomic_read(&maple_tree_tests_passed), \ atomic_read(&maple_tree_tests_run)); \ - dump_stack(); \ - } else { \ - atomic_inc(&maple_tree_tests_passed); \ + WARN_ON(1); \ } \ unlikely(ret); \ })
@@ -776,9 +776,9 @@ void mt_cache_shrink(void);#define MT_BUG_ON (__tree, __x) BUG_ON(__x)#define MAS_BUG_ON (__mas, __x) BUG_ON(__x)#define MAS_WR_BUG_ON (__mas, __x) BUG_ON(__x) -#define MT_WARN_ON (__tree, __x) WARN_ON(__x) -#define MAS_WARN_ON (__mas, __x) WARN_ON(__x) -#define MAS_WR_WARN_ON (__mas, __x) WARN_ON(__x) +#define MT_WARN_ON (__tree, __x) WARN_ON_ONCE(__x) +#define MAS_WARN_ON (__mas, __x) WARN_ON_ONCE(__x) +#define MAS_WR_WARN_ON (__mas, __x) WARN_ON_ONCE(__x)#endif /* CONFIG_DEBUG_MAPLE_TREE */ /**
tools/testing/radix-tree/maple.c +9 -4
@@ -36502,22 +36502,27 @@ static void regression_tests(void)test_spanning_store_regression (); } -void maple_tree_tests (void ) +int maple_tree_tests (void ) { +int ret; +#if !defined(BENCH )regression_tests ();farmer_tests ();#endif -maple_tree_seed (); + ret =maple_tree_seed ();maple_tree_harvest (); +return ret; }int __weak main (void ) { +int ret; +maple_tree_init (); -maple_tree_tests (); + ret =maple_tree_tests ();rcu_barrier ();if (nr_allocated)printf ("nr_allocated = %d\n" , nr_allocated); -return 0 ; +return ret ?EXIT_FAILURE :EXIT_SUCCESS ; }
tools/testing/shared/maple-shared.h +33 -0
@@ -8,6 +8,7 @@#include "shared.h" #include <stdlib.h> #include <time.h> +#include <assert.h> #include "linux/init.h" void maple_rcu_cb (struct rcu_head *head);
@@ -20,5 +21,37 @@ do { \call_rcu (&((_p_struct)->_memb), rcu_cb); \ } while(0); +/* Make WARN() fatal */ +#undef WARN +#undef WARN_ON +#undef WARN_ONCE +#undef WARN_ON_ONCE +#define WARN(condition, format...) ({ \ + int __ret_warn_on = !!(condition); \ + if (unlikely(__ret_warn_on)) { \ + __WARN_printf(format); \ + assert(0); \ + } \ + unlikely(__ret_warn_on); \ +}) +#define WARN_ON (condition) \ + WARN(condition, "assertion failed at %s:%d\n", __FILE__, __LINE__) +#define WARN_ONCE(condition, format...) WARN(condition, format) +#define WARN_ON_ONCE (condition) WARN_ON(condition) + +/* Once state for the debug macros */ +#ifndef __ONCE_LITE_IF +#define __ONCE_LITE_IF (condition) \ + ({ \ + static bool __already_done; \ + bool __ret_once = false; \ + \ + if ((condition) && !__already_done) { \ + __already_done = true; \ + __ret_once = true; \ + } \ + __ret_once; \ + }) +#endif #endif /* __MAPLE_SHARED_H__ */
Signature · 1 line
2.47.3
Re: [PATCH v2 3/3] maple_tree: Update internal warning and bug_on code
Quoted 7 lines · Liam R. Howlett (Google)
When
CONFIG_DEBUG_MAPLE_TREEis set, callWARN_ON()once per site after the custom information is dumped about the tree. Without using the kernel'sWARN_ONcalls, syzbot will not catch issues detected by this code. TheBUG_ONvariants now stop instead of dumping and continuing.Update the non-debug code to call
WARN_ON_ONCE()as well.
Quoted 8 lines
The userspace tests asserted on
dump_stack()but only print onWARN_ON(), so makeWARN()fatal inmaple-shared.hand have the maple test exit with the result ofmaple_tree_seed().Cc <[email protected]>Reported-by Lorenzo Stoakes <[email protected]>Fixes f0a1f866aba1 ("maple_tree: add debug BUG_ON and WARN_ON variants")Signed-off-by Liam R. Howlett (Google) <[email protected]>
Thanks for doing this! This covers off my concerns with the userland testing changes so all LGTM and:
Quoted 209 lines
--- include/linux/maple_tree.h | 48 ++++++++++++++--------------- tools/testing/radix-tree/maple.c | 13 +++++--- tools/testing/shared/maple-shared.h | 33 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 28 deletions(-)include/linux/maple_tree.h +24
-24@@ -680,7 +680,7 @@ void mt_cache_shrink(void);pr_info ("Pass: %u Run:%u\n" , \atomic_read (&maple_tree_tests_passed), \atomic_read (&maple_tree_tests_run)); \ -dump_stack (); \ +BUG_ON (1 ); \ }else { \atomic_inc (&maple_tree_tests_passed); \ } \@@ -696,7 +696,7 @@ void mt_cache_shrink(void);pr_info ("Pass: %u Run:%u\n" , \atomic_read (&maple_tree_tests_passed), \atomic_read (&maple_tree_tests_run)); \ -dump_stack (); \ +BUG_ON (1 ); \ }else { \atomic_inc (&maple_tree_tests_passed); \ } \@@ -713,7 +713,7 @@ void mt_cache_shrink(void);pr_info ("Pass: %u Run:%u\n" , \atomic_read (&maple_tree_tests_passed), \atomic_read (&maple_tree_tests_run)); \ -dump_stack (); \ +BUG_ON (1 ); \ }else { \atomic_inc (&maple_tree_tests_passed); \ } \@@ -722,16 +722,16 @@ void mt_cache_shrink(void);#define MT_WARN_ON (__tree, __x) ({ \ int ret = !!(__x); \ atomic_inc(&maple_tree_tests_run); \ - if (ret) { \ - pr_info("WARN at %s:%d (%u)\n", \ - __func__, __LINE__, __x); \ + if (!ret) { \ + atomic_inc(&maple_tree_tests_passed); \ + } else if (__ONCE_LITE_IF(ret)) { \ + pr_info("WARN at %s:%d\n", \ + __func__, __LINE__); \ mt_dump(__tree, mt_dump_hex); \ pr_info("Pass: %u Run:%u\n", \ atomic_read(&maple_tree_tests_passed), \ atomic_read(&maple_tree_tests_run)); \ - dump_stack(); \ - } else { \ - atomic_inc(&maple_tree_tests_passed); \ + WARN_ON(1); \ } \ unlikely(ret); \ })@@ -739,17 +739,17 @@ void mt_cache_shrink(void);#define MAS_WARN_ON (__mas, __x) ({ \ int ret = !!(__x); \ atomic_inc(&maple_tree_tests_run); \ - if (ret) { \ - pr_info("WARN at %s:%d (%u)\n", \ - __func__, __LINE__, __x); \ + if (!ret) { \ + atomic_inc(&maple_tree_tests_passed); \ + } else if (__ONCE_LITE_IF(ret)) { \ + pr_info("WARN at %s:%d\n", \ + __func__, __LINE__); \ mas_dump(__mas); \ mt_dump((__mas)->tree, mt_dump_hex); \ pr_info("Pass: %u Run:%u\n", \ atomic_read(&maple_tree_tests_passed), \ atomic_read(&maple_tree_tests_run)); \ - dump_stack(); \ - } else { \ - atomic_inc(&maple_tree_tests_passed); \ + WARN_ON(1); \ } \ unlikely(ret); \ })@@ -757,18 +757,18 @@ void mt_cache_shrink(void);#define MAS_WR_WARN_ON (__wrmas, __x) ({ \ int ret = !!(__x); \ atomic_inc(&maple_tree_tests_run); \ - if (ret) { \ - pr_info("WARN at %s:%d (%u)\n", \ - __func__, __LINE__, __x); \ + if (!ret) { \ + atomic_inc(&maple_tree_tests_passed); \ + } else if (__ONCE_LITE_IF(ret)) { \ + pr_info("WARN at %s:%d\n", \ + __func__, __LINE__); \ mas_wr_dump(__wrmas); \ mas_dump((__wrmas)->mas); \ mt_dump((__wrmas)->mas->tree, mt_dump_hex); \ pr_info("Pass: %u Run:%u\n", \ atomic_read(&maple_tree_tests_passed), \ atomic_read(&maple_tree_tests_run)); \ - dump_stack(); \ - } else { \ - atomic_inc(&maple_tree_tests_passed); \ + WARN_ON(1); \ } \ unlikely(ret); \ })@@ -776,9 +776,9 @@ void mt_cache_shrink(void);#define MT_BUG_ON (__tree, __x) BUG_ON(__x)#define MAS_BUG_ON (__mas, __x) BUG_ON(__x)#define MAS_WR_BUG_ON (__mas, __x) BUG_ON(__x) -#define MT_WARN_ON (__tree, __x) WARN_ON(__x) -#define MAS_WARN_ON (__mas, __x) WARN_ON(__x) -#define MAS_WR_WARN_ON (__mas, __x) WARN_ON(__x) +#define MT_WARN_ON (__tree, __x) WARN_ON_ONCE(__x) +#define MAS_WARN_ON (__mas, __x) WARN_ON_ONCE(__x) +#define MAS_WR_WARN_ON (__mas, __x) WARN_ON_ONCE(__x)#endif /* CONFIG_DEBUG_MAPLE_TREE */ /** tools/testing/radix-tree/maple.c +9
-4@@ -36502,22 +36502,27 @@ static void regression_tests(void)test_spanning_store_regression (); } -void maple_tree_tests (void ) +int maple_tree_tests (void ) { +int ret; +#if !defined(BENCH )regression_tests ();farmer_tests ();#endif -maple_tree_seed (); + ret =maple_tree_seed ();maple_tree_harvest (); +return ret; }int __weak main (void ) { +int ret; +maple_tree_init (); -maple_tree_tests (); + ret =maple_tree_tests ();rcu_barrier ();if (nr_allocated)printf ("nr_allocated = %d\n" , nr_allocated); -return 0 ; +return ret ?EXIT_FAILURE :EXIT_SUCCESS ; }tools/testing/shared/maple-shared.h +33
-1@@ -8,6 +8,7 @@#include "shared.h" #include <stdlib.h> #include <time.h> +#include <assert.h> #include "linux/init.h" void maple_rcu_cb (struct rcu_head *head);@@ -20,5 +21,37 @@ do { \call_rcu (&((_p_struct)->_memb), rcu_cb); \ } while(0); +/* Make WARN() fatal */ +#undef WARN +#undef WARN_ON +#undef WARN_ONCE +#undef WARN_ON_ONCE +#define WARN(condition, format...) ({ \ + int __ret_warn_on = !!(condition); \ + if (unlikely(__ret_warn_on)) { \ + __WARN_printf(format); \ + assert(0); \ + } \ + unlikely(__ret_warn_on); \ +}) +#define WARN_ON (condition) \ + WARN(condition, "assertion failed at %s:%d\n", __FILE__, __LINE__) +#define WARN_ONCE(condition, format...) WARN(condition, format) +#define WARN_ON_ONCE (condition) WARN_ON(condition) + +/* Once state for the debug macros */ +#ifndef __ONCE_LITE_IF +#define __ONCE_LITE_IF (condition) \ + ({ \ + static bool __already_done; \ + bool __ret_once = false; \ + \ + if ((condition) && !__already_done) { \ + __already_done = true; \ + __ret_once = true; \ + } \ + __ret_once; \ + }) +#endif #endif /* __MAPLE_SHARED_H__ */ --
Quoted 2 lines
2.47.3
--
Cheers, Lorenzo