Skip to contentSkip to conversation

Fix mas_alloc_cyclic retry

PATCH In review Liam R. Howlett 2 participants 5 messages Dec 16, 2024 – Dec 17, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Fix mas_alloc_cyclic retry

This is a follow-up patch and test for Yang Erkun. lore seems to have eaten the original email [1], so I'll add akpm's link as well [2].

Patch 1 applies on top of Yang Erkun's fix, so feel free to squash it in.

Patch 2 adds a test to the maple tree infrastructure that will catch the issue if it comes back.

[1] https://lore.kernel.org/all/[email protected]/T/#u
[2] https://lore.kernel.org/all/[email protected]/

Liam R. Howlett (2):
maple_tree: Fix mas_alloc_cyclic() second search
test_maple_tree: Test exhausted upper limit of mtree_alloc_cyclic()

 lib/maple_tree.c      |  3 +--
 lib/test_maple_tree.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 maple_tree: Fix mas_alloc_cyclic() second search

From: "Liam R. Howlett" <[email protected]>

The first search may leave the maple state in an error state. Reset the maple state before the second search so that the search has a chance of executing correctly after an exhausted first search.

Fixes 9b6713cc7522 ("maple_tree: Add mtree_alloc_cyclic()")
Cc Yang Erkun <[email protected]>
---
 lib/maple_tree.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/maple_tree.c +1 -2
index 940d30653407b..f7153ade1be5f 100644
@@ -4335,7 +4335,6 @@ int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp,
 {
 	unsigned long min = range_lo;
 	int ret = 0;
-	struct ma_state m = *mas;
 
 	range_lo = max(min, *next);
 	ret = mas_empty_area(mas, range_lo, range_hi, 1);
@@ -4344,7 +4343,7 @@ int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp,
 		ret = 1;
 	}
 	if (ret < 0 && range_lo > min) {
-		*mas = m;
+		mas_reset(mas);
 		ret = mas_empty_area(mas, min, range_hi, 1);
 		if (ret == 0)
 			ret = 1;
Signature · 1 line
2.43.0
Reviewed-by Yang Erkun on v1 1/2
#2 lore raw

Re: [PATCH 1/2] maple_tree: Fix mas_alloc_cyclic() second search

Quoted 12 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

The first search may leave the maple state in an error state. Reset the maple state before the second search so that the search has a chance of executing correctly after an exhausted first search.

Fixes 9b6713cc7522 ("maple_tree: Add mtree_alloc_cyclic()")
Cc Yang Erkun <[email protected]>
Quoted 3 lines
---
  lib/maple_tree.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)

This looks more clear!

Quoted 22 lines
lib/maple_tree.c +1 -2
index 940d30653407b..f7153ade1be5f 100644
@@ -4335,7 +4335,6 @@ int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp,
  {
  	unsigned long min = range_lo;
  	int ret = 0;
-	struct ma_state m = *mas;
 
  	range_lo = max(min, *next);
  	ret = mas_empty_area(mas, range_lo, range_hi, 1);
@@ -4344,7 +4343,7 @@ int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp,
  		ret = 1;
  	}
  	if (ret < 0 && range_lo > min) {
-		*mas = m;
+		mas_reset(mas);
  		ret = mas_empty_area(mas, min, range_hi, 1);
  		if (ret == 0)
  			ret = 1;
#3 lore raw

PATCH 2/2 test_maple_tree: Test exhausted upper limit of mtree_alloc_cyclic()

In reply to Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

When the upper bound of the search is exhausted, the maple state may be returned in an error state of -EBUSY. This means maple state needs to be reset before the second search in mas_alloc_cylic() to ensure the search happens. This test ensures the issue is not recreated.

---
 lib/test_maple_tree.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
lib/test_maple_tree.c +28 -0
index 72bda304b5952..13e2a10d7554d 100644
@@ -3738,6 +3738,34 @@ static noinline void __init alloc_cyclic_testing(struct maple_tree *mt)
 	}
 
 	mtree_destroy(mt);
+
+	/*
+	 * Issue with reverse search was discovered
+	 * https://lore.kernel.org/all/[email protected]/
+	 * Exhausting the allocation area and forcing the search to wrap needs a
+	 * mas_reset() in mas_alloc_cyclic().
+	 */
+	next = 0;
+	mt_init_flags(mt, MT_FLAGS_ALLOC_RANGE);
+	for (int i = 0; i < 1023; i++) {
+		mtree_alloc_cyclic(mt, &location, mt, 2, 1024, &next, GFP_KERNEL);
+		MT_BUG_ON(mt, i != location - 2);
+		MT_BUG_ON(mt, i != next - 3);
+		MT_BUG_ON(mt, mtree_load(mt, location) != mt);
+	}
+	mtree_erase(mt, 123);
+	MT_BUG_ON(mt, mtree_load(mt, 123) != NULL);
+	mtree_alloc_cyclic(mt, &location, mt, 2, 1024, &next, GFP_KERNEL);
+	MT_BUG_ON(mt, 123 != location);
+	MT_BUG_ON(mt, 124 != next);
+	MT_BUG_ON(mt, mtree_load(mt, location) != mt);
+	mtree_erase(mt, 100);
+	mtree_alloc_cyclic(mt, &location, mt, 2, 1024, &next, GFP_KERNEL);
+	MT_BUG_ON(mt, 100 != location);
+	MT_BUG_ON(mt, 101 != next);
+	MT_BUG_ON(mt, mtree_load(mt, location) != mt);
+	mtree_destroy(mt);
+
 	/* Overflow test */
 	next = ULONG_MAX - 1;
 	ret = mtree_alloc_cyclic(mt, &location, mt, 2, ULONG_MAX, &next, GFP_KERNEL);
Signature · 1 line
2.43.0
Reviewed-by Yang Erkun on v1 2/2
#4 lore raw

Re: [PATCH 2/2] test_maple_tree: Test exhausted upper limit of mtree_alloc_cyclic()

Quoted 7 lines · Liam R. Howlett

From: "Liam R. Howlett" <[email protected]>

When the upper bound of the search is exhausted, the maple state may be returned in an error state of -EBUSY. This means maple state needs to be reset before the second search in mas_alloc_cylic() to ensure the search happens. This test ensures the issue is not recreated.

Quoted 8 lines
---
  lib/test_maple_tree.c | 28 ++++++++++++++++++++++++++++
  1 file changed, 28 insertions(+)

Thanks for the testcase!

Quoted 39 lines
lib/test_maple_tree.c +28 -0
index 72bda304b5952..13e2a10d7554d 100644
@@ -3738,6 +3738,34 @@ static noinline void __init alloc_cyclic_testing(struct maple_tree *mt)
  	}
 
  	mtree_destroy(mt);
+
+	/*
+	 * Issue with reverse search was discovered
+	 * https://lore.kernel.org/all/[email protected]/
+	 * Exhausting the allocation area and forcing the search to wrap needs a
+	 * mas_reset() in mas_alloc_cyclic().
+	 */
+	next = 0;
+	mt_init_flags(mt, MT_FLAGS_ALLOC_RANGE);
+	for (int i = 0; i < 1023; i++) {
+		mtree_alloc_cyclic(mt, &location, mt, 2, 1024, &next, GFP_KERNEL);
+		MT_BUG_ON(mt, i != location - 2);
+		MT_BUG_ON(mt, i != next - 3);
+		MT_BUG_ON(mt, mtree_load(mt, location) != mt);
+	}
+	mtree_erase(mt, 123);
+	MT_BUG_ON(mt, mtree_load(mt, 123) != NULL);
+	mtree_alloc_cyclic(mt, &location, mt, 2, 1024, &next, GFP_KERNEL);
+	MT_BUG_ON(mt, 123 != location);
+	MT_BUG_ON(mt, 124 != next);
+	MT_BUG_ON(mt, mtree_load(mt, location) != mt);
+	mtree_erase(mt, 100);
+	mtree_alloc_cyclic(mt, &location, mt, 2, 1024, &next, GFP_KERNEL);
+	MT_BUG_ON(mt, 100 != location);
+	MT_BUG_ON(mt, 101 != next);
+	MT_BUG_ON(mt, mtree_load(mt, location) != mt);
+	mtree_destroy(mt);
+
  	/* Overflow test */
  	next = ULONG_MAX - 1;
  	ret = mtree_alloc_cyclic(mt, &location, mt, 2, ULONG_MAX, &next, GFP_KERNEL);