2023-05-05 15:12:47

by Peng Zhang

[permalink] [raw]
Subject: [PATCH v2] maple_tree: Make maple state reusable after mas_empty_area()

Make mas->min and mas->max point to a node range instead of a leaf entry
range. This allows mas to still be usable after mas_empty_area() returns.
Users would get unexpected results from other operations on the maple
state after calling the affected function.

Reported-by: "Edgecombe, Rick P" <[email protected]>
Reported-by: Tad <[email protected]>
Reported-by: Michael Keyes <[email protected]>
Link: https://lore.kernel.org/linux-mm/[email protected]/
Link: https://lore.kernel.org/linux-mm/[email protected]/
Fixes: 54a611b60590 ("Maple Tree: add new data structure")
Cc: <[email protected]>
Signed-off-by: Peng Zhang <[email protected]>
---
lib/maple_tree.c | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index 110a36479dced..8ebc43d4cc8c5 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -5317,15 +5317,9 @@ int mas_empty_area(struct ma_state *mas, unsigned long min,

mt = mte_node_type(mas->node);
pivots = ma_pivots(mas_mn(mas), mt);
- if (offset)
- mas->min = pivots[offset - 1] + 1;
-
- if (offset < mt_pivots[mt])
- mas->max = pivots[offset];
-
- if (mas->index < mas->min)
- mas->index = mas->min;
-
+ min = mas_safe_min(mas, pivots, offset);
+ if (mas->index < min)
+ mas->index = min;
mas->last = mas->index + size - 1;
return 0;
}
--
2.20.1


2023-05-05 16:13:11

by Liam R. Howlett

[permalink] [raw]
Subject: Re: [PATCH v2] maple_tree: Make maple state reusable after mas_empty_area()

* Peng Zhang <[email protected]> [230505 10:59]:
> Make mas->min and mas->max point to a node range instead of a leaf entry
> range. This allows mas to still be usable after mas_empty_area() returns.
> Users would get unexpected results from other operations on the maple
> state after calling the affected function.
>
> Reported-by: "Edgecombe, Rick P" <[email protected]>
> Reported-by: Tad <[email protected]>
> Reported-by: Michael Keyes <[email protected]>
> Link: https://lore.kernel.org/linux-mm/[email protected]/
> Link: https://lore.kernel.org/linux-mm/[email protected]/
> Fixes: 54a611b60590 ("Maple Tree: add new data structure")
> Cc: <[email protected]>
> Signed-off-by: Peng Zhang <[email protected]>

Reviewed-by: Liam R. Howlett <[email protected]>

> ---
> lib/maple_tree.c | 12 +++---------
> 1 file changed, 3 insertions(+), 9 deletions(-)
>
> diff --git a/lib/maple_tree.c b/lib/maple_tree.c
> index 110a36479dced..8ebc43d4cc8c5 100644
> --- a/lib/maple_tree.c
> +++ b/lib/maple_tree.c
> @@ -5317,15 +5317,9 @@ int mas_empty_area(struct ma_state *mas, unsigned long min,
>
> mt = mte_node_type(mas->node);
> pivots = ma_pivots(mas_mn(mas), mt);
> - if (offset)
> - mas->min = pivots[offset - 1] + 1;
> -
> - if (offset < mt_pivots[mt])
> - mas->max = pivots[offset];
> -
> - if (mas->index < mas->min)
> - mas->index = mas->min;
> -
> + min = mas_safe_min(mas, pivots, offset);
> + if (mas->index < min)
> + mas->index = min;
> mas->last = mas->index + size - 1;
> return 0;
> }
> --
> 2.20.1
>

2023-05-05 17:54:04

by Edgecombe, Rick P

[permalink] [raw]
Subject: Re: [PATCH v2] maple_tree: Make maple state reusable after mas_empty_area()

On Fri, 2023-05-05 at 22:58 +0800, Peng Zhang wrote:
> Make mas->min and mas->max point to a node range instead of a leaf
> entry
> range. This allows mas to still be usable after mas_empty_area()
> returns.
> Users would get unexpected results from other operations on the maple
> state after calling the affected function.
>
> Reported-by: "Edgecombe, Rick P" <[email protected]>
> Reported-by: Tad <[email protected]>
> Reported-by: Michael Keyes <[email protected]>
> Link:
> https://lore.kernel.org/linux-mm/[email protected]/
> Link:
> https://lore.kernel.org/linux-mm/[email protected]/
> Fixes: 54a611b60590 ("Maple Tree: add new data structure")
> Cc: <[email protected]>
> Signed-off-by: Peng Zhang <[email protected]>
> ---

Yep, this also works for me. Thanks.