Skip to contentSkip to conversation

cleanup maple_alloc related functions

PATCH Superseded Wei Yang 3 participants 14 messages Sep 24, 2024 – Oct 15, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover cleanup maple_alloc related functions

Patch 1/2: some fields of the maple_alloc is not necessary to change, so we
can skip some operations
Patch 3: a valid alloc check could be hide in current code, so we don't need
to do a separate check
Patch 4: found on rare case where allocation would fail even has enough
memory

Wei Yang (4):
maple_tree: clear request_count for new allocated one
maple_tree: total is not changed for nomem_one case
maple_tree: simplify mas_push_node()
maple_tree: fix potential allocation failure even has memory

 lib/maple_tree.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/4 maple_tree: clear request_count for new allocated one

If this is not a new allocated one, the request_count has already been cleared in mas_set_alloc_req().

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
---
 lib/maple_tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/maple_tree.c +1 -1
index fab1610dc800..75be2c81f0e2 100644
@@ -1250,11 +1250,11 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 
 		mas->alloc = node;
 		node->total = ++allocated;
+		node->request_count = 0;
 		requested--;
 	}
 
 	node = mas->alloc;
-	node->request_count = 0;
 	while (requested) {
 		max_req = MAPLE_ALLOC_SLOTS - node->node_count;
 		slots = (void **)&node->slot[node->node_count];
Signature · 1 line
2.34.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] maple_tree: clear request_count for new allocated one

Quoted 6 lines · Wei Yang

If this is not a new allocated one, the request_count has already been cleared in mas_set_alloc_req().

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
Quoted 24 lines
---
 lib/maple_tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/maple_tree.c +1 -1
index fab1610dc800..75be2c81f0e2 100644
@@ -1250,11 +1250,11 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 
 		mas->alloc = node;
 		node->total = ++allocated;
+		node->request_count = 0;
 		requested--;
 	}
 
 	node = mas->alloc;
-	node->request_count = 0;
 	while (requested) {
 		max_req = MAPLE_ALLOC_SLOTS - node->node_count;
 		slots = (void **)&node->slot[node->node_count];
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 2/4 maple_tree: total is not changed for nomem_one case

In reply to Wei Yang

If it jumps to nomem_one, the total allocated number is not changed. So we don't need to adjust it.

For the nomem_bulk case, we know there is a valid mas->alloc. So we don't need to do the check.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
---
 lib/maple_tree.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/maple_tree.c +1 -2
index 75be2c81f0e2..c1fb67540cc9 100644
@@ -1279,10 +1279,9 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 nomem_bulk:
 	/* Clean up potential freed allocations on bulk failure */
 	memset(slots, 0, max_req * sizeof(unsigned long));
+	mas->alloc->total = allocated;
 nomem_one:
 	mas_set_alloc_req(mas, requested);
-	if (mas->alloc && !(((unsigned long)mas->alloc & 0x1)))
-		mas->alloc->total = allocated;
 	mas_set_err(mas, -ENOMEM);
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] maple_tree: total is not changed for nomem_one case

Quoted 9 lines · Wei Yang

If it jumps to nomem_one, the total allocated number is not changed. So we don't need to adjust it.

For the nomem_bulk case, we know there is a valid mas->alloc. So we don't need to do the check.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
Quoted 23 lines
---
 lib/maple_tree.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/maple_tree.c +1 -2
index 75be2c81f0e2..c1fb67540cc9 100644
@@ -1279,10 +1279,9 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 nomem_bulk:
 	/* Clean up potential freed allocations on bulk failure */
 	memset(slots, 0, max_req * sizeof(unsigned long));
+	mas->alloc->total = allocated;
 nomem_one:
 	mas_set_alloc_req(mas, requested);
-	if (mas->alloc && !(((unsigned long)mas->alloc & 0x1)))
-		mas->alloc->total = allocated;
 	mas_set_err(mas, -ENOMEM);
 }
 
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 3/4 maple_tree: simplify mas_push_node()

In reply to Wei Yang

When count is not 0, we know head is valid. So we can put the assignment in if(count) instead of check head again.

Also count represents current total, we can assign the new total by increasing count at last.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
---
 lib/maple_tree.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
lib/maple_tree.c +7 -9
index c1fb67540cc9..1cbc5f7ca40d 100644
@@ -1192,19 +1192,17 @@ static inline void mas_push_node(struct ma_state *mas, struct maple_node *used)
 
 	reuse->request_count = 0;
 	reuse->node_count = 0;
-	if (count && (head->node_count < MAPLE_ALLOC_SLOTS)) {
-		head->slot[head->node_count++] = reuse;
-		head->total++;
-		goto done;
-	}
-
-	reuse->total = 1;
-	if ((head) && !((unsigned long)head & 0x1)) {
+	if (count) {
+		if (head->node_count < MAPLE_ALLOC_SLOTS) {
+			head->slot[head->node_count++] = reuse;
+			head->total++;
+			goto done;
+		}
 		reuse->slot[0] = head;
 		reuse->node_count = 1;
-		reuse->total += head->total;
 	}
 
+	reuse->total = count + 1;
 	mas->alloc = reuse;
 done:
 	if (requested > 1)
Signature · 1 line
2.34.1
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] maple_tree: simplify mas_push_node()

Quoted 2 lines · Wei Yang

When count is not 0, we know head is valid. So we can put the assignment in if(count) in stead of check head again.

" in if (count) instead of checking the head pointer again."

Quoted 3 lines

Also count represents current total, we can assign the new total by increasing count at last.

" increasing the count by one."

This message isn't clear, but the rest is good. Thanks!

Quoted 4 lines
CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
Quoted 38 lines
---
 lib/maple_tree.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
lib/maple_tree.c +7 -9
index c1fb67540cc9..1cbc5f7ca40d 100644
@@ -1192,19 +1192,17 @@ static inline void mas_push_node(struct ma_state *mas, struct maple_node *used)
 
 	reuse->request_count = 0;
 	reuse->node_count = 0;
-	if (count && (head->node_count < MAPLE_ALLOC_SLOTS)) {
-		head->slot[head->node_count++] = reuse;
-		head->total++;
-		goto done;
-	}
-
-	reuse->total = 1;
-	if ((head) && !((unsigned long)head & 0x1)) {
+	if (count) {
+		if (head->node_count < MAPLE_ALLOC_SLOTS) {
+			head->slot[head->node_count++] = reuse;
+			head->total++;
+			goto done;
+		}
 		reuse->slot[0] = head;
 		reuse->node_count = 1;
-		reuse->total += head->total;
 	}
 
+	reuse->total = count + 1;
 	mas->alloc = reuse;
 done:
 	if (requested > 1)
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH 4/4 maple_tree: fix potential allocation failure even has memory

In reply to Wei Yang

We got an rare case when mas_node_count() would fail even there is enough memory.

The reason is the maple_alloc grows downward. And when hit a full maple_alloc, the max_req would be 0. This leads to mt_alloc_bulk() return 0, which means failure here.

For example, here is the test code:

	expect = MAPLE_ALLOC_SLOTS + 1;
	mas_node_count(&ms, expect);
	pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms));
	expect = MAPLE_ALLOC_SLOTS * 2 + 2;
	mas_node_count(&ms, expect);
	pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms));

We will get the following output, which shows we fail to allocate the required number of nodes.

expect 31 allocated 31
expect 62 allocated 61

The straight forward way to fix it is go down one level more.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
---
 lib/maple_tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/maple_tree.c +3 -2
index 1cbc5f7ca40d..dd33d0793dd1 100644
@@ -1253,8 +1253,10 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 	}
 
 	node = mas->alloc;
-	while (requested) {
+	for (; requested; node = node->slot[0]) {
 		max_req = MAPLE_ALLOC_SLOTS - node->node_count;
+		if (unlikely(!max_req))
+			continue;
 		slots = (void **)&node->slot[node->node_count];
 		max_req = min(requested, max_req);
 		count = mt_alloc_bulk(gfp, max_req, slots);
@@ -1268,7 +1270,6 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 
 		node->node_count += count;
 		allocated += count;
-		node = node->slot[0];
 		requested -= count;
 	}
 	mas->alloc->total = allocated;
Signature · 1 line
2.34.1
author
#8 lore raw

Re: [PATCH 4/4] maple_tree: fix potential allocation failure even has memory

Related fix has been posted, but not merged yet.

lkml.kernel.org/r/[email protected]

May drop this one.

Quoted 57 lines · Wei Yang

We got an rare case when mas_node_count() would fail even there is enough memory.

The reason is the maple_alloc grows downward. And when hit a full maple_alloc, the max_req would be 0. This leads to mt_alloc_bulk() return 0, which means failure here.

For example, here is the test code:

	expect = MAPLE_ALLOC_SLOTS + 1;
	mas_node_count(&ms, expect);
	pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms));
	expect = MAPLE_ALLOC_SLOTS * 2 + 2;
	mas_node_count(&ms, expect);
	pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms));

We will get the following output, which shows we fail to allocate the required number of nodes.

expect 31 allocated 31
expect 62 allocated 61

The straight forward way to fix it is go down one level more.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>

---
lib/maple_tree.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
lib/maple_tree.c +3 -2
index 1cbc5f7ca40d..dd33d0793dd1 100644
@@ -1253,8 +1253,10 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 	}
 
 	node = mas->alloc;
-	while (requested) {
+	for (; requested; node = node->slot[0]) {
 		max_req = MAPLE_ALLOC_SLOTS - node->node_count;
+		if (unlikely(!max_req))
+			continue;
 		slots = (void **)&node->slot[node->node_count];
 		max_req = min(requested, max_req);
 		count = mt_alloc_bulk(gfp, max_req, slots);
@@ -1268,7 +1270,6 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 
 		node->node_count += count;
 		allocated += count;
-		node = node->slot[0];
 		requested -= count;
 	}
 	mas->alloc->total = allocated;
Signature · 1 line
2.34.1
Signature · 2 lines
Wei Yang
Help you, Help me
#9 lore raw

Re: [PATCH 4/4] maple_tree: fix potential allocation failure even has memory

Quoted 6 lines · Wei Yang

Related fix has been posted, but not merged yet.

lkml.kernel.org/r/[email protected]

May drop this one.

Yes, I thought we had fixed this already.

Quoted 64 lines · Wei Yang
Quoted 57 lines

We got an rare case when mas_node_count() would fail even there is enough memory.

The reason is the maple_alloc grows downward. And when hit a full maple_alloc, the max_req would be 0. This leads to mt_alloc_bulk() return 0, which means failure here.

For example, here is the test code:

	expect = MAPLE_ALLOC_SLOTS + 1;
	mas_node_count(&ms, expect);
	pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms));
	expect = MAPLE_ALLOC_SLOTS * 2 + 2;
	mas_node_count(&ms, expect);
	pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms));

We will get the following output, which shows we fail to allocate the required number of nodes.

expect 31 allocated 31
expect 62 allocated 61

The straight forward way to fix it is go down one level more.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>

---
lib/maple_tree.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
lib/maple_tree.c +3 -2
index 1cbc5f7ca40d..dd33d0793dd1 100644
@@ -1253,8 +1253,10 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 	}
 
 	node = mas->alloc;
-	while (requested) {
+	for (; requested; node = node->slot[0]) {
 		max_req = MAPLE_ALLOC_SLOTS - node->node_count;
+		if (unlikely(!max_req))
+			continue;
 		slots = (void **)&node->slot[node->node_count];
 		max_req = min(requested, max_req);
 		count = mt_alloc_bulk(gfp, max_req, slots);
@@ -1268,7 +1270,6 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 
 		node->node_count += count;
 		allocated += count;
-		node = node->slot[0];
 		requested -= count;
 	}
 	mas->alloc->total = allocated;
Signature · 1 line
2.34.1
Signature · 2 lines
Wei Yang
Help you, Help me
#10 lore raw

Re: [PATCH 4/4] maple_tree: fix potential allocation failure even has memory

In reply to Wei Yang
Quoted 6 lines · Wei Yang

Related fix has been posted, but not merged yet.

lkml.kernel.org/r/[email protected]

May drop this one.

Yes, thanks. This can be dropped in favour of the other commit.

Quoted 64 lines · Wei Yang
Quoted 57 lines

We got an rare case when mas_node_count() would fail even there is enough memory.

The reason is the maple_alloc grows downward. And when hit a full maple_alloc, the max_req would be 0. This leads to mt_alloc_bulk() return 0, which means failure here.

For example, here is the test code:

	expect = MAPLE_ALLOC_SLOTS + 1;
	mas_node_count(&ms, expect);
	pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms));
	expect = MAPLE_ALLOC_SLOTS * 2 + 2;
	mas_node_count(&ms, expect);
	pr_info("expect %d allocated %lu\n", expect, mas_allocated(&ms));

We will get the following output, which shows we fail to allocate the required number of nodes.

expect 31 allocated 31
expect 62 allocated 61

The straight forward way to fix it is go down one level more.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>

---
lib/maple_tree.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
lib/maple_tree.c +3 -2
index 1cbc5f7ca40d..dd33d0793dd1 100644
@@ -1253,8 +1253,10 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 	}
 
 	node = mas->alloc;
-	while (requested) {
+	for (; requested; node = node->slot[0]) {
 		max_req = MAPLE_ALLOC_SLOTS - node->node_count;
+		if (unlikely(!max_req))
+			continue;
 		slots = (void **)&node->slot[node->node_count];
 		max_req = min(requested, max_req);
 		count = mt_alloc_bulk(gfp, max_req, slots);
@@ -1268,7 +1270,6 @@ static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
 
 		node->node_count += count;
 		allocated += count;
-		node = node->slot[0];
 		requested -= count;
 	}
 	mas->alloc->total = allocated;
Signature · 1 line
2.34.1
Signature · 2 lines
Wei Yang
Help you, Help me
#11 lore raw
In reply to Wei Yang
Quoted 13 lines · Wei Yang

Patch 1/2: some fields of the maple_alloc is not necessary to change, so we
can skip some operations
Patch 3: a valid alloc check could be hide in current code, so we don't need
to do a separate check
Patch 4: found on rare case where allocation would fail even has enough
memory

Wei Yang (4):
maple_tree: clear request_count for new allocated one
maple_tree: total is not changed for nomem_one case
maple_tree: simplify mas_push_node()
maple_tree: fix potential allocation failure even has memory

Quoted 7 lines
 lib/maple_tree.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

--
2.34.1

Andrew - Liam is away at the moment - could we hold off on taking this series to unstable until he's back and had a chance to review please?

It may be best to avoid taking maple tree changes other than urgent bug fixes or the like until his return also.

Wei - please hold off on series like this until Liam's had a chance to reply to this thread. Thanks!

author
#12 lore raw
Quoted 22 lines · Lorenzo Stoakes
Quoted 13 lines · Wei Yang

Patch 1/2: some fields of the maple_alloc is not necessary to change, so we
can skip some operations
Patch 3: a valid alloc check could be hide in current code, so we don't need
to do a separate check
Patch 4: found on rare case where allocation would fail even has enough
memory

Wei Yang (4):
maple_tree: clear request_count for new allocated one
maple_tree: total is not changed for nomem_one case
maple_tree: simplify mas_push_node()
maple_tree: fix potential allocation failure even has memory

Quoted 7 lines
 lib/maple_tree.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

--
2.34.1

Quoted 8 lines

Andrew - Liam is away at the moment - could we hold off on taking this series to unstable until he's back and had a chance to review please?

It may be best to avoid taking maple tree changes other than urgent bug fixes or the like until his return also.

Wei - please hold off on series like this until Liam's had a chance to reply to this thread. Thanks!

Sure, let me know when it is ok.

BTW, I don't see Andrew has taken it in unstable. Do I miss the mail?

Signature · 2 lines
Wei Yang
Help you, Help me
#13 lore raw
Quoted 26 lines · Wei Yang
Quoted 25 lines · Lorenzo Stoakes
Quoted 13 lines · Wei Yang

Patch 1/2: some fields of the maple_alloc is not necessary to change, so we
can skip some operations
Patch 3: a valid alloc check could be hide in current code, so we don't need
to do a separate check
Patch 4: found on rare case where allocation would fail even has enough
memory

Wei Yang (4):
maple_tree: clear request_count for new allocated one
maple_tree: total is not changed for nomem_one case
maple_tree: simplify mas_push_node()
maple_tree: fix potential allocation failure even has memory

Quoted 7 lines
 lib/maple_tree.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

--
2.34.1

Andrew - Liam is away at the moment - could we hold off on taking this series to unstable until he's back and had a chance to review please?

Quoted 7 lines
Quoted 5 lines

It may be best to avoid taking maple tree changes other than urgent bug fixes or the like until his return also.

Wei - please hold off on series like this until Liam's had a chance to reply to this thread. Thanks!

Sure, let me know when it is ok.

Thanks, Liam should reply to the thread once he's back then we're good to go :)

Quoted 2 lines

BTW, I don't see Andrew has taken it in unstable. Do I miss the mail?

Yeah he's not taken yet, kind of getting ahead of things here - not a comment on your series - more an act of time/resource management :)

Cheers!

Quoted 5 lines

--
Wei Yang
Help you, Help me