maple_tree: Fix the replacement of a root leaf node
Subsystems (5)
v3 cover maple_tree: Fix the replacement of a root leaf node
On destroy we should set each node dead. But current code miss this when the maple tree has only the root node.
The reason is mt_destroy_walk() leverage mte_destroy_descend() to set node dead, but this is skipped since the only root node is a leaf.
Patch 1 fixes this.
When adding a test case, I found we always get the new value even we leave the old root node not dead. It turns out we always re-walk the tree in mas_walk(). It looks like a typo on the status check of mas_walk().
Patch 2 fixes this.
Patch 3 add a test case to assert retrieving new value when overwriting the whole range to a tree with only root node.
V3 rebase on latest mm-unstable with base commit eba4438e2296.
Wei Yang (3):
maple_tree: Fix mt_destroy_walk() on root leaf node
maple_tree: restart walk on correct status
maple_tree: assert retrieving new value on a tree containing just a
leaf node
lib/maple_tree.c | 3 ++- tools/testing/radix-tree/maple.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
PATCH v3 1/3 maple_tree: Fix mt_destroy_walk() on root leaf node
On destroy, we should set each node dead. But current code miss this when the maple tree has only the root node.
The reason is mt_destroy_walk() leverage mte_destroy_descend() to set node dead, but this is skipped since the only root node is a leaf.
Fixes this by setting the node dead if it is a leaf.
---
v2:
* move the operation into mt_destroy_walk()
* adjust the title accordingly
--- lib/maple_tree.c | 1 + 1 file changed, 1 insertion(+)
lib/maple_tree.c +1 -0
@@ -5319,6 +5319,7 @@ static void mt_destroy_walk(struct maple_enode *enode, struct maple_tree *mt,struct maple_enode *start;if (mte_is_leaf (enode)) { +mte_set_node_dead (enode); node->type =mte_node_type (enode); goto free_leaf; }
Signature · 1 line
2.34.1
PATCH v3 2/3 maple_tree: restart walk on correct status
In reply to Wei YangCommit a8091f039c1e ("maple_tree: add MAS_UNDERFLOW and MAS_OVERFLOW states") adds more status during maple tree walk. But it introduce a typo on the status check during walk.
It expects to mean neither active nor start, we would restart the walk, while current code means we would always restart the walk.
--- lib/maple_tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/maple_tree.c +1 -1
@@ -4930,7 +4930,7 @@ void *mas_walk(struct ma_state *mas) {void *entry; -if (!mas_is_active (mas) || !mas_is_start (mas)) +if (!mas_is_active (mas) && !mas_is_start (mas)) mas->status = ma_start; retry: entry =mas_state_walk (mas);
Signature · 1 line
2.34.1
PATCH v3 3/3 maple_tree: assert retrieving new value on a tree containing just a leaf node
In reply to Wei YangOriginal code may not get the new value after overwriting the whole range on a maple tree containing just a leaf node. The reason is we didn't set the only root node dead during destroy.
Add a test case to ensure the new value is returned when overwriting a tree containing just a leaf node.
---
v2: adjust the changelog according to Liam's suggestion
--- tools/testing/radix-tree/maple.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
tools/testing/radix-tree/maple.c +24 -0
@@ -35256,6 +35256,30 @@ static noinline void __init check_rcu_simulated(struct maple_tree *mt)MT_BUG_ON (mt,mas_prev (&mas_reader,0 ) !=xa_mk_value (val));rcu_read_unlock (); +/* Clear out tree & create one with only root node */ +mas_lock (&mas_writer); +mas_set_range (&mas_writer,0 ,ULONG_MAX ); +mas_store_gfp (&mas_writer,NULL ,GFP_KERNEL ); +mas_set_range (&mas_writer,0 ,0 ); +for (i =0 ; i <=5 ; i++) { + mas_writer.index = i *10 ; + mas_writer.last = i *10 +5 ; +mas_store_gfp (&mas_writer,xa_mk_value (i),GFP_KERNEL ); + } +mas_unlock (&mas_writer); + target =10 ; +mas_set_range (&mas_reader, target, target); +rcu_read_lock (); +MT_BUG_ON (mt,mas_walk (&mas_reader) !=xa_mk_value (target/10 )); + +/* Overwrite the whole range */ +mas_lock (&mas_writer); +mas_set_range (&mas_writer,0 ,ULONG_MAX ); +mas_store_gfp (&mas_writer,xa_mk_value (val),GFP_KERNEL ); +mas_unlock (&mas_writer); +MT_BUG_ON (mt,mas_walk (&mas_reader) !=xa_mk_value (val)); +rcu_read_unlock (); +rcu_unregister_thread (); }
Signature · 1 line
2.34.1
Andrew Morton
maintainerQuoted 5 lines · On Wed, 11 Jun 2025 01:12:50 +0000 Wei Yang <[email protected]> wrote:
On destroy we should set each node dead. But current code miss this when the maple tree has only the root node.
The reason is
mt_destroy_walk()leveragemte_destroy_descend()to set node dead, but this is skipped since the only root node is a leaf.
Thanks. You added cc:stable to two of the patches and that's great, thanks for remembering. But the changelogs didn't tell us why you made this choice.
So, as always, please always describe the userspace-visible impact of a bug when fixing that bug!
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Wed, 11 Jun 2025 01:12:50 +0000 Wei Yang <[email protected]> wrote:
On destroy we should set each node dead. But current code miss this when the maple tree has only the root node.
The reason is
mt_destroy_walk()leveragemte_destroy_descend()to set node dead, but this is skipped since the only root node is a leaf.
Quoted 6 lines
Thanks. You added cc:stable to two of the patches and that's great, thanks for remembering. But the changelogs didn't tell us why you made this choice.
So, as always, please always describe the userspace-visible impact of a bug when fixing that bug!
Thanks for reminding.
Do you prefer to have a new version with the description in changelog?
Signature · 2 lines
Wei Yang Help you, Help me
Andrew Morton
maintainerQuoted 13 lines · On Wed, 11 Jun 2025 02:54:36 +0000 Wei Yang <[email protected]> wrote:
Quoted 12 lines · Andrew Morton
Quoted 5 lines · On Wed, 11 Jun 2025 01:12:50 +0000 Wei Yang <[email protected]> wrote:
On destroy we should set each node dead. But current code miss this when the maple tree has only the root node.
The reason is
mt_destroy_walk()leveragemte_destroy_descend()to set node dead, but this is skipped since the only root node is a leaf.Thanks. You added cc:stable to two of the patches and that's great, thanks for remembering. But the changelogs didn't tell us why you made this choice.
Quoted 6 lines
Quoted 2 lines
So, as always, please always describe the userspace-visible impact of a bug when fixing that bug!
Thanks for reminding.
Do you prefer to have a new version with the description in changelog?
No, that's fine - please just send it in reply to this email.
Quoted 15 lines · Andrew Morton
Quoted 13 lines · On Wed, 11 Jun 2025 02:54:36 +0000 Wei Yang <[email protected]> wrote:
Quoted 12 lines · Andrew Morton
Quoted 5 lines · On Wed, 11 Jun 2025 01:12:50 +0000 Wei Yang <[email protected]> wrote:
On destroy we should set each node dead. But current code miss this when the maple tree has only the root node.
The reason is
mt_destroy_walk()leveragemte_destroy_descend()to set node dead, but this is skipped since the only root node is a leaf.Thanks. You added cc:stable to two of the patches and that's great, thanks for remembering. But the changelogs didn't tell us why you made this choice.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
So, as always, please always describe the userspace-visible impact of a bug when fixing that bug!
Thanks for reminding.
Do you prefer to have a new version with the description in changelog?
No, that's fine - please just send it in reply to this email.
Below is my understanding about the impact of the bug.
---
Without the fix, maple tree user may see old value after overwriting the whole range [0, ULONG_MAX] on a tree containing just one leaf node. If it happens, kernel may access some invalid data and be crashed.
Since we rarely overwrite the whole range of a maple tree, we don't expect it to have userspace-visible impact in practise.
Signature · 2 lines
Wei Yang Help you, Help me