Skip to content

Commit fad8e42

Browse files
howlettakpm00
authored andcommitted
maple_tree: make maple state reusable after mas_empty_area_rev()
Stop using maple state min/max for the range by passing through pointers for those values. This will allow the maple state to be reused without resetting. Also add some logic to fail out early on searching with invalid arguments. Link: https://lkml.kernel.org/r/[email protected] Fixes: 54a611b ("Maple Tree: add new data structure") Signed-off-by: Liam R. Howlett <[email protected]> Reported-by: Rick Edgecombe <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent fdea03e commit fad8e42

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

lib/maple_tree.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4965,7 +4965,8 @@ static inline void *mas_prev_entry(struct ma_state *mas, unsigned long min)
49654965
* Return: True if found in a leaf, false otherwise.
49664966
*
49674967
*/
4968-
static bool mas_rev_awalk(struct ma_state *mas, unsigned long size)
4968+
static bool mas_rev_awalk(struct ma_state *mas, unsigned long size,
4969+
unsigned long *gap_min, unsigned long *gap_max)
49694970
{
49704971
enum maple_type type = mte_node_type(mas->node);
49714972
struct maple_node *node = mas_mn(mas);
@@ -5030,8 +5031,8 @@ static bool mas_rev_awalk(struct ma_state *mas, unsigned long size)
50305031

50315032
if (unlikely(ma_is_leaf(type))) {
50325033
mas->offset = offset;
5033-
mas->min = min;
5034-
mas->max = min + gap - 1;
5034+
*gap_min = min;
5035+
*gap_max = min + gap - 1;
50355036
return true;
50365037
}
50375038

@@ -5307,6 +5308,9 @@ int mas_empty_area(struct ma_state *mas, unsigned long min,
53075308
unsigned long *pivots;
53085309
enum maple_type mt;
53095310

5311+
if (min >= max)
5312+
return -EINVAL;
5313+
53105314
if (mas_is_start(mas))
53115315
mas_start(mas);
53125316
else if (mas->offset >= 2)
@@ -5361,6 +5365,9 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
53615365
{
53625366
struct maple_enode *last = mas->node;
53635367

5368+
if (min >= max)
5369+
return -EINVAL;
5370+
53645371
if (mas_is_start(mas)) {
53655372
mas_start(mas);
53665373
mas->offset = mas_data_end(mas);
@@ -5380,7 +5387,7 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
53805387
mas->index = min;
53815388
mas->last = max;
53825389

5383-
while (!mas_rev_awalk(mas, size)) {
5390+
while (!mas_rev_awalk(mas, size, &min, &max)) {
53845391
if (last == mas->node) {
53855392
if (!mas_rewind_node(mas))
53865393
return -EBUSY;
@@ -5395,17 +5402,9 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
53955402
if (unlikely(mas->offset == MAPLE_NODE_SLOTS))
53965403
return -EBUSY;
53975404

5398-
/*
5399-
* mas_rev_awalk() has set mas->min and mas->max to the gap values. If
5400-
* the maximum is outside the window we are searching, then use the last
5401-
* location in the search.
5402-
* mas->max and mas->min is the range of the gap.
5403-
* mas->index and mas->last are currently set to the search range.
5404-
*/
5405-
54065405
/* Trim the upper limit to the max. */
5407-
if (mas->max <= mas->last)
5408-
mas->last = mas->max;
5406+
if (max <= mas->last)
5407+
mas->last = max;
54095408

54105409
mas->index = mas->last - size + 1;
54115410
return 0;

0 commit comments

Comments
 (0)