Skip to contentSkip to conversation

spanning write related cleanup

PATCH In review Wei Yang 2 participants 17 messages Nov 27, 2024 – Jan 31, 2025

Subsystems (3)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover spanning write related cleanup

Here is some cleanup related to spanning write.

Wei Yang (7):
maple_tree: not necessary to check ahead if !content
maple_tree: validate we won't split on NULL
maple_tree: check mid_split only may have
maple_tree: the return value of mast_spanning_rebalance() is not used
maple_tree: the type of left subtree is already saved in bnode->type
maple_tree: always need to update max of new left node
maple_tree: only ascend left subtree to get the old node for
replacement

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

PATCH 1/7 maple_tree: not necessary to check ahead if !content

Just like mas_wr_extend_null(), only when content is not NULL, we may have a NULL slot ahead.

So we can wrap it in an else.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
CC Lorenzo Stoakes <[email protected]>
---
 lib/maple_tree.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
lib/maple_tree.c +12 -10
index 7efb1520f9bd..98692704d773 100644
@@ -3568,18 +3568,20 @@ static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,
 	unsigned char l_slot;
 
 	l_slot = l_mas->offset;
-	if (!l_wr_mas->content)
+	if (!l_wr_mas->content) {
+		/* If this one is null, the next and prev are not */
 		l_mas->index = l_wr_mas->r_min;
+	} else {
+		if ((l_mas->index == l_wr_mas->r_min) &&
+			 (l_slot &&
+			  !mas_slot_locked(l_mas, l_wr_mas->slots, l_slot - 1))) {
+			if (l_slot > 1)
+				l_mas->index = l_wr_mas->pivots[l_slot - 2] + 1;
+			else
+				l_mas->index = l_mas->min;
 
-	if ((l_mas->index == l_wr_mas->r_min) &&
-		 (l_slot &&
-		  !mas_slot_locked(l_mas, l_wr_mas->slots, l_slot - 1))) {
-		if (l_slot > 1)
-			l_mas->index = l_wr_mas->pivots[l_slot - 2] + 1;
-		else
-			l_mas->index = l_mas->min;
-
-		l_mas->offset = l_slot - 1;
+			l_mas->offset = l_slot - 1;
+		}
 	}
 
 	if (!r_wr_mas->content) {
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/7 maple_tree: validate we won't split on NULL

In reply to Wei Yang

If this is not the right most node, we don't expect its last slot to be NULL.

Let's validate this.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
CC Lorenzo Stoakes <[email protected]>
---
 lib/maple_tree.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/maple_tree.c +6 -0
index 98692704d773..2a00441130ee 100644
@@ -7522,6 +7522,12 @@ static void mt_validate_nulls(struct maple_tree *mt)
 		MT_BUG_ON(mt, !last && !entry);
 		last = entry;
 		if (offset == mas_data_end(&mas)) {
+			if ((mas.max != ULONG_MAX) && !entry) {
+				pr_err("Last slot %p end with NULL\n",
+					mas_mn(&mas));
+				MT_BUG_ON(mas.tree, 1);
+			}
+
 			mas_next_node(&mas, mas_mn(&mas), ULONG_MAX);
 			if (mas_is_overflow(&mas))
 				return;
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/7 maple_tree: check mid_split only may have

In reply to Wei Yang

We only may set mid_split in the else clause, let's move the check in it.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
CC Lorenzo Stoakes <[email protected]>
---
 lib/maple_tree.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/maple_tree.c +3 -2
index 2a00441130ee..f5606b4d0dba 100644
@@ -2381,10 +2381,11 @@ static inline unsigned char mas_mab_to_node(struct ma_state *mas,
 	} else {
 		split = mab_calc_split(mas, b_node, mid_split);
 		*right = mas_new_ma_node(mas, b_node);
+
+		if (*mid_split)
+			*middle = mas_new_ma_node(mas, b_node);
 	}
 
-	if (*mid_split)
-		*middle = mas_new_ma_node(mas, b_node);
 
 	return split;
 
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 4/7 maple_tree: the return value of mast_spanning_rebalance() is not used

In reply to Wei Yang

The return value is not used, change it to a void function.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
CC Lorenzo Stoakes <[email protected]>
---
 lib/maple_tree.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/maple_tree.c +4 -4
index f5606b4d0dba..6cabee1371ec 100644
@@ -2265,7 +2265,7 @@ static inline void mast_rebalance_prev(struct maple_subtree_state *mast)
  * @mast: The maple_subtree_state.
  */
 static inline
-bool mast_spanning_rebalance(struct maple_subtree_state *mast)
+void mast_spanning_rebalance(struct maple_subtree_state *mast)
 {
 	struct ma_state r_tmp = *mast->orig_r;
 	struct ma_state l_tmp = *mast->orig_l;
@@ -2284,7 +2284,7 @@ bool mast_spanning_rebalance(struct maple_subtree_state *mast)
 
 			mast_rebalance_next(mast);
 			*mast->orig_l = l_tmp;
-			return true;
+			return;
 		} else if (mast->orig_l->offset != 0) {
 			mast->orig_l->offset--;
 			do {
@@ -2295,13 +2295,13 @@ bool mast_spanning_rebalance(struct maple_subtree_state *mast)
 
 			mast_rebalance_prev(mast);
 			*mast->orig_r = r_tmp;
-			return true;
+			return;
 		}
 	} while (!mte_is_root(mast->orig_r->node));
 
 	*mast->orig_r = r_tmp;
 	*mast->orig_l = l_tmp;
-	return false;
+	return;
 }
 
 /*
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 5/7 maple_tree: the type of left subtree is already saved in bnode->type

In reply to Wei Yang

mast_sufficient()/mast_overflow() are only used in mas_spanning_rebalance(). Before doing the check, mast_ascend() has retrieved the left subtree type and stored in bnode->type.

So we can use the type in bnode->type directly to get minimum slot count.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
CC Lorenzo Stoakes <[email protected]>
---
 lib/maple_tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/maple_tree.c +2 -2
index 6cabee1371ec..56e9857ce681 100644
@@ -2729,7 +2729,7 @@ static inline void mast_combine_cp_right(struct maple_subtree_state *mast)
  */
 static inline bool mast_sufficient(struct maple_subtree_state *mast)
 {
-	if (mast->bn->b_end > mt_min_slot_count(mast->orig_l->node))
+	if (mast->bn->b_end > mt_min_slots[mast->bn->type])
 		return true;
 
 	return false;
@@ -2742,7 +2742,7 @@ static inline bool mast_sufficient(struct maple_subtree_state *mast)
  */
 static inline bool mast_overflow(struct maple_subtree_state *mast)
 {
-	if (mast->bn->b_end >= mt_slot_count(mast->orig_l->node))
+	if (mast->bn->b_end >= mt_slots[mast->bn->type])
 		return true;
 
 	return false;
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH 6/7 maple_tree: always need to update max of new left node

In reply to Wei Yang

If (split == mast->bn->b_end), this means we would put all data from bnode to new left. And the original data source of bnode is orig_l + orig_r, whose range is [orig_l->min, orig_r->max]

So we can always update max of new left node.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
CC Lorenzo Stoakes <[email protected]>
---
 lib/maple_tree.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
lib/maple_tree.c +1 -8
index 56e9857ce681..4ba9ae68c0a9 100644
@@ -2664,19 +2664,12 @@ static inline void mast_cp_to_nodes(struct maple_subtree_state *mast,
 	struct maple_enode *left, struct maple_enode *middle,
 	struct maple_enode *right, unsigned char split, unsigned char mid_split)
 {
-	bool new_lmax = true;
-
 	mas_node_or_none(mast->l, left);
 	mas_node_or_none(mast->m, middle);
 	mas_node_or_none(mast->r, right);
 
 	mast->l->min = mast->orig_l->min;
-	if (split == mast->bn->b_end) {
-		mast->l->max = mast->orig_r->max;
-		new_lmax = false;
-	}
-
-	mab_mas_cp(mast->bn, 0, split, mast->l, new_lmax);
+	mab_mas_cp(mast->bn, 0, split, mast->l, true);
 
 	if (middle) {
 		mab_mas_cp(mast->bn, 1 + split, mid_split, mast->m, true);
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH 7/7 maple_tree: only ascend left subtree to get the old node for replacement

In reply to Wei Yang

When we come to a new_root, we should go up the tree to get the old root for replacement.

Instead of ascend both left/right subtree, we only ascend one of it is enough. Let's use the left subtree as it dose now.

CC Liam R. Howlett <[email protected]>
CC Sidhartha Kumar <[email protected]>
CC Lorenzo Stoakes <[email protected]>
---
 lib/maple_tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/maple_tree.c +1 -1
index 4ba9ae68c0a9..2c05919be168 100644
@@ -2920,7 +2920,7 @@ static void mas_spanning_rebalance(struct ma_state *mas,
 new_root:
 		mas_mn(mast->l)->parent = ma_parent_ptr(mas_tree_parent(mas));
 		while (!mte_is_root(mast->orig_l->node))
-			mast_ascend(mast);
+			mas_ascend(mast->orig_l);
 	} else {
 		mas_mn(&l_mas)->parent = mas_mn(mast->orig_l)->parent;
 	}
Signature · 1 line
2.34.1
#8 lore raw
In reply to Wei Yang
Quoted 1 line · Wei Yang

Here is some cleanup related to spanning write.

None of these fix anything, but do fiddle with code that's pretty critical to the kernel. Most of the changes will be immeasurable in change but carry risk to causing subtle changes.

Some are simple removal of returns that aren't used while others change things because you think they are probably the equivalent. This seems like unnecessary chrun at this point. I'm all for efficient code but this is getting a bit much, some of these are just preference of what to use that will already exist in the cpu cache.

I'll get back to you when I dig through them, as some need a deeper look for sure.

Liam

Quoted 17 lines

Wei Yang (7):
maple_tree: not necessary to check ahead if !content
maple_tree: validate we won't split on NULL
maple_tree: check mid_split only may have
maple_tree: the return value of mast_spanning_rebalance() is not used
maple_tree: the type of left subtree is already saved in bnode->type
maple_tree: always need to update max of new left node
maple_tree: only ascend left subtree to get the old node for
replacement

 lib/maple_tree.c | 56 +++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 27 deletions(-)
Signature · 1 line
2.34.1
author
#9 lore raw
Quoted 7 lines · Liam R. Howlett
Quoted 1 line · Wei Yang

Here is some cleanup related to spanning write.

None of these fix anything, but do fiddle with code that's pretty critical to the kernel. Most of the changes will be immeasurable in change but carry risk to causing subtle changes.

Quoted 8 lines

Some are simple removal of returns that aren't used while others change things because you think they are probably the equivalent. This seems like unnecessary chrun at this point. I'm all for efficient code but this is getting a bit much, some of these are just preference of what to use that will already exist in the cpu cache.

I'll get back to you when I dig through them, as some need a deeper look for sure.

Thanks

Quoted 20 lines

Liam

Quoted 17 lines

Wei Yang (7):
maple_tree: not necessary to check ahead if !content
maple_tree: validate we won't split on NULL
maple_tree: check mid_split only may have
maple_tree: the return value of mast_spanning_rebalance() is not used
maple_tree: the type of left subtree is already saved in bnode->type
maple_tree: always need to update max of new left node
maple_tree: only ascend left subtree to get the old node for
replacement

 lib/maple_tree.c | 56 +++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 27 deletions(-)
Signature · 1 line
2.34.1
Signature · 2 lines
Wei Yang
Help you, Help me
author
#10 lore raw
In reply to Liam R. Howlett
Quoted 13 lines · Liam R. Howlett
Quoted 1 line · Wei Yang

Here is some cleanup related to spanning write.

None of these fix anything, but do fiddle with code that's pretty critical to the kernel. Most of the changes will be immeasurable in change but carry risk to causing subtle changes.

Some are simple removal of returns that aren't used while others change things because you think they are probably the equivalent. This seems like unnecessary chrun at this point. I'm all for efficient code but this is getting a bit much, some of these are just preference of what to use that will already exist in the cpu cache.

Quoted 5 lines

I'll get back to you when I dig through them, as some need a deeper look for sure.

Liam

Hi, Liam

Would you mind taking a look when you have time?

Signature · 2 lines
Wei Yang
Help you, Help me
#11 lore raw
Quoted 17 lines · Wei Yang
Quoted 16 lines · Liam R. Howlett
Quoted 1 line · Wei Yang

Here is some cleanup related to spanning write.

None of these fix anything, but do fiddle with code that's pretty critical to the kernel. Most of the changes will be immeasurable in change but carry risk to causing subtle changes.

Some are simple removal of returns that aren't used while others change things because you think they are probably the equivalent. This seems like unnecessary chrun at this point. I'm all for efficient code but this is getting a bit much, some of these are just preference of what to use that will already exist in the cpu cache.

I'll get back to you when I dig through them, as some need a deeper look for sure.

Quoted 6 lines
Quoted 2 lines

Liam

Hi, Liam

Would you mind taking a look when you have time?

Yes, I'll have a look soon. I don't love changes that dive deep into complex code that results in no gains (performance or feature wise).

It's also odd to have simple "this return isn't use" and things moving code blocks to be executed only in certain scenarios, as the difficulty to verify the latter is much higher.

Can we please limit changes to areas where there is a performance change or coupled with a change that is needed? ie: stop sending patches that change things unless it's with a feature or improvement (performance or otherwise). I'm just not convinced some of these are worth the cost vs risk.

Thanks,
Liam

author
#12 lore raw
Quoted 32 lines · Liam R. Howlett
Quoted 17 lines · Wei Yang
Quoted 16 lines · Liam R. Howlett
Quoted 1 line · Wei Yang

Here is some cleanup related to spanning write.

None of these fix anything, but do fiddle with code that's pretty critical to the kernel. Most of the changes will be immeasurable in change but carry risk to causing subtle changes.

Some are simple removal of returns that aren't used while others change things because you think they are probably the equivalent. This seems like unnecessary chrun at this point. I'm all for efficient code but this is getting a bit much, some of these are just preference of what to use that will already exist in the cpu cache.

I'll get back to you when I dig through them, as some need a deeper look for sure.

Quoted 6 lines
Quoted 2 lines

Liam

Hi, Liam

Would you mind taking a look when you have time?

Yes, I'll have a look soon. I don't love changes that dive deep into complex code that results in no gains (performance or feature wise).

It's also odd to have simple "this return isn't use" and things moving code blocks to be executed only in certain scenarios, as the difficulty to verify the latter is much higher.

Quoted 6 lines

Can we please limit changes to areas where there is a performance change or coupled with a change that is needed? ie: stop sending patches that change things unless it's with a feature or improvement (performance or otherwise). I'm just not convinced some of these are worth the cost vs risk.

Ok.

So you would drop this patch set or still want to take a look?

Quoted 2 lines

Thanks,
Liam

Signature · 2 lines
Wei Yang
Help you, Help me
#13 lore raw
Quoted 40 lines · Wei Yang
Quoted 32 lines · Liam R. Howlett
Quoted 17 lines · Wei Yang
Quoted 16 lines · Liam R. Howlett
Quoted 1 line · Wei Yang

Here is some cleanup related to spanning write.

None of these fix anything, but do fiddle with code that's pretty critical to the kernel. Most of the changes will be immeasurable in change but carry risk to causing subtle changes.

Some are simple removal of returns that aren't used while others change things because you think they are probably the equivalent. This seems like unnecessary chrun at this point. I'm all for efficient code but this is getting a bit much, some of these are just preference of what to use that will already exist in the cpu cache.

I'll get back to you when I dig through them, as some need a deeper look for sure.

Quoted 6 lines
Quoted 2 lines

Liam

Hi, Liam

Would you mind taking a look when you have time?

Yes, I'll have a look soon. I don't love changes that dive deep into complex code that results in no gains (performance or feature wise).

It's also odd to have simple "this return isn't use" and things moving code blocks to be executed only in certain scenarios, as the difficulty to verify the latter is much higher.

Quoted 6 lines

Can we please limit changes to areas where there is a performance change or coupled with a change that is needed? ie: stop sending patches that change things unless it's with a feature or improvement (performance or otherwise). I'm just not convinced some of these are worth the cost vs risk.

Quoted 3 lines

Ok.

So you would drop this patch set or still want to take a look?

I was going to look at it, but after I send my reply, I received a report of an issue caused in a certain configuration that caused the stack frame to grow out of the configured 1024 limit, which was tracked to a patch you added to simplify a previous function.

So, I think we should drop these patches since they don't make a measurable difference and are not without risk.

Thanks,
Liam

author
#14 lore raw
Quoted 41 lines · Liam R. Howlett
Quoted 40 lines · Wei Yang
Quoted 38 lines · Liam R. Howlett
Quoted 17 lines · Wei Yang
Quoted 16 lines · Liam R. Howlett
Quoted 1 line · Wei Yang

Here is some cleanup related to spanning write.

None of these fix anything, but do fiddle with code that's pretty critical to the kernel. Most of the changes will be immeasurable in change but carry risk to causing subtle changes.

Some are simple removal of returns that aren't used while others change things because you think they are probably the equivalent. This seems like unnecessary chrun at this point. I'm all for efficient code but this is getting a bit much, some of these are just preference of what to use that will already exist in the cpu cache.

I'll get back to you when I dig through them, as some need a deeper look for sure.

Quoted 6 lines
Quoted 2 lines

Liam

Hi, Liam

Would you mind taking a look when you have time?

Yes, I'll have a look soon. I don't love changes that dive deep into complex code that results in no gains (performance or feature wise).

It's also odd to have simple "this return isn't use" and things moving code blocks to be executed only in certain scenarios, as the difficulty to verify the latter is much higher.

Can we please limit changes to areas where there is a performance change or coupled with a change that is needed? ie: stop sending patches that change things unless it's with a feature or improvement (performance or otherwise). I'm just not convinced some of these are worth the cost vs risk.

Quoted 8 lines
Quoted 3 lines

Ok.

So you would drop this patch set or still want to take a look?

I was going to look at it, but after I send my reply, I received a report of an issue caused in a certain configuration that caused the stack frame to grow out of the configured 1024 limit, which was tracked to a patch you added to simplify a previous function.

Sorry for that. Would you mind letting me know what is the problem? or cc me in case you will fix it?

Quoted 6 lines

So, I think we should drop these patches since they don't make a measurable difference and are not without risk.

Thanks,
Liam

Signature · 2 lines
Wei Yang
Help you, Help me
author
#15 lore raw
In reply to Liam R. Howlett
Quoted 45 lines · Liam R. Howlett
Quoted 40 lines · Wei Yang
Quoted 32 lines · Liam R. Howlett
Quoted 17 lines · Wei Yang
Quoted 16 lines · Liam R. Howlett
Quoted 1 line · Wei Yang

Here is some cleanup related to spanning write.

None of these fix anything, but do fiddle with code that's pretty critical to the kernel. Most of the changes will be immeasurable in change but carry risk to causing subtle changes.

Some are simple removal of returns that aren't used while others change things because you think they are probably the equivalent. This seems like unnecessary chrun at this point. I'm all for efficient code but this is getting a bit much, some of these are just preference of what to use that will already exist in the cpu cache.

I'll get back to you when I dig through them, as some need a deeper look for sure.

Quoted 6 lines
Quoted 2 lines

Liam

Hi, Liam

Would you mind taking a look when you have time?

Yes, I'll have a look soon. I don't love changes that dive deep into complex code that results in no gains (performance or feature wise).

It's also odd to have simple "this return isn't use" and things moving code blocks to be executed only in certain scenarios, as the difficulty to verify the latter is much higher.

Quoted 6 lines

Can we please limit changes to areas where there is a performance change or coupled with a change that is needed? ie: stop sending patches that change things unless it's with a feature or improvement (performance or otherwise). I'm just not convinced some of these are worth the cost vs risk.

Quoted 3 lines

Ok.

So you would drop this patch set or still want to take a look?

Quoted 8 lines

I was going to look at it, but after I send my reply, I received a report of an issue caused in a certain configuration that caused the stack frame to grow out of the configured 1024 limit, which was tracked to a patch you added to simplify a previous function.

So, I think we should drop these patches since they don't make a measurable difference and are not without risk.

Hi, Liam

Maybe I found one issue in current code that we miss to set the root node dead. Before sending a fix, I want to check with you first.

Current behavior:

mte_destroy_walk()

    mt_destroy_walk()
      if (mte_is_leaf())
        goto free_leaf;
      mte_destroy_descend()
        mte_set_node_dead()    <-- set node dead here

Usually we set all node dead in mte_destroy_descend(). But if the maple tree has just one root node, it jump to free_leaf without set it dead.

It seems not correct to leave a non-dead node. Is my understanding correct?

Quoted 2 lines

Thanks,
Liam

Signature · 2 lines
Wei Yang
Help you, Help me
author
#16 lore raw
In reply to Liam R. Howlett
Quoted 1 line · Liam R. Howlett

* Wei Yang <[email protected]> [250123 20:44]:

[...]

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 3 lines

Hi, Liam

Would you mind taking a look when you have time?

Yes, I'll have a look soon. I don't love changes that dive deep into complex code that results in no gains (performance or feature wise).

It's also odd to have simple "this return isn't use" and things moving code blocks to be executed only in certain scenarios, as the difficulty to verify the latter is much higher.

Can we please limit changes to areas where there is a performance change or coupled with a change that is needed? ie: stop sending patches that change things unless it's with a feature or improvement (performance or otherwise). I'm just not convinced some of these are worth the cost vs risk.

Quoted 9 lines
Quoted 3 lines

Ok.

So you would drop this patch set or still want to take a look?

I was going to look at it, but after I send my reply, I received a report of an issue caused in a certain configuration that caused the stack frame to grow out of the configured 1024 limit, which was tracked to a patch you added to simplify a previous function.

Liam,

I tried to locate which of my patch introduce the problem, but not succeed. This is the issue of -Wframe-larger-than=, right?

Would you mind letting me know which one did something bad? So that I can avoid such thing later.

Quoted 5 lines

So, I think we should drop these patches since they don't make a measurable difference and are not without risk.

Thanks,
Liam

Signature · 2 lines
Wei Yang
Help you, Help me