Skip to contentSkip to conversation

Question regarding Linux implementation of rbtrees

PATCH Alexandru Moise 2 participants 3 messages Apr 13, 2017 – Apr 14, 2017 Library code

lore
Share & tools
lore
#0 lore raw

Seeing as RB_RED is defined to be 0 in include/linux/rbtree_augmented.h A call of this form: rb_set_parent_color(node, parent, RB_RED);
as seen in __rb_insert would only end up reassigning the parent "color" (which is the parent pointer value cast to unsigned long) OR'd with 0. Which would mean that nothing would really change regarding the parent's "color". So, that would lead one to think that the diagram at case 2 showing the grandparent's color going from black to red could not be completely accurate as the Linux implementation presently stands.

Could the maintainers provide an answer as to why the below patch is the __wrong__ thing to do? Apart from the obvious "the values of the macros might change in the future".

Thanks,
../Alex

---
 lib/rbtree.c | 4 ----
 1 file changed, 4 deletions(-)
lib/rbtree.c +0 -4
index 4ba2828a67c0..6b540be4dda4 100644
@@ -135,7 +135,6 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
 				rb_set_parent_color(parent, gparent, RB_BLACK);
 				node = gparent;
 				parent = rb_parent(node);
-				rb_set_parent_color(node, parent, RB_RED);
 				continue;
 			}
 
@@ -159,7 +158,6 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
 				if (tmp)
 					rb_set_parent_color(tmp, parent,
 							    RB_BLACK);
-				rb_set_parent_color(parent, node, RB_RED);
 				augment_rotate(parent, node);
 				parent = node;
 				tmp = node->rb_right;
@@ -189,7 +187,6 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
 				rb_set_parent_color(parent, gparent, RB_BLACK);
 				node = gparent;
 				parent = rb_parent(node);
-				rb_set_parent_color(node, parent, RB_RED);
 				continue;
 			}
 
@@ -202,7 +199,6 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
 				if (tmp)
 					rb_set_parent_color(tmp, parent,
 							    RB_BLACK);
-				rb_set_parent_color(parent, node, RB_RED);
 				augment_rotate(parent, node);
 				parent = node;
 				tmp = node->rb_left;
Signature · 1 line
2.12.2
#1 lore raw
Quoted 24 lines · Alexandru Moise

Seeing as RB_RED is defined to be 0 in include/linux/rbtree_augmented.h A call of this form: rb_set_parent_color(node, parent, RB_RED);
as seen in __rb_insert would only end up reassigning the parent "color" (which is the parent pointer value cast to unsigned long) OR'd with 0. Which would mean that nothing would really change regarding the parent's "color". So, that would lead one to think that the diagram at case 2 showing the grandparent's color going from black to red could not be completely accurate as the Linux implementation presently stands.

Could the maintainers provide an answer as to why the below patch is the __wrong__ thing to do? Apart from the obvious "the values of the macros might change in the future".

Thanks,
../Alex

---
 lib/rbtree.c | 4 ----
 1 file changed, 4 deletions(-)
lib/rbtree.c +0 -0
index 4ba2828a67c0..6b540be4dda4 100644

lib/rbtree.c __rb_insert(struct rb_node *node, struct rb_root *root, · line 140

Quoted 7 lines
lib/rbtree.c +0 -1
@@ -135,7 +135,6 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
 				rb_set_parent_color(parent, gparent, RB_BLACK);
 				node = gparent;
 				parent = rb_parent(node);
-				rb_set_parent_color(node, parent, RB_RED);
 				continue;
 			}
 

So who would clear the bit then? The point here is (IIRC) that node is black and needs to become red.

#2 lore raw
Quoted 20 lines · Peter Zijlstra
Quoted 19 lines · Alexandru Moise

Seeing as RB_RED is defined to be 0 in include/linux/rbtree_augmented.h A call of this form: rb_set_parent_color(node, parent, RB_RED);
as seen in __rb_insert would only end up reassigning the parent "color" (which is the parent pointer value cast to unsigned long) OR'd with 0. Which would mean that nothing would really change regarding the parent's "color". So, that would lead one to think that the diagram at case 2 showing the grandparent's color going from black to red could not be completely accurate as the Linux implementation presently stands.

Could the maintainers provide an answer as to why the below patch is the __wrong__ thing to do? Apart from the obvious "the values of the macros might change in the future".

Thanks,
../Alex

---
 lib/rbtree.c | 4 ----
 1 file changed, 4 deletions(-)
Quoted 15 lines

lib/rbtree.c __rb_insert(struct rb_node *node, struct rb_root *root, · line 140

Quoted 12 lines
lib/rbtree.c +0 -1
index 4ba2828a67c0..6b540be4dda4 100644
@@ -135,7 +135,6 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
 				rb_set_parent_color(parent, gparent, RB_BLACK);
 				node = gparent;
 				parent = rb_parent(node);
-				rb_set_parent_color(node, parent, RB_RED);
 				continue;
 			}
 

So who would clear the bit then? The point here is (IIRC) that node is black and needs to become red.

Now I've read it again and realized that it's actually in rb_parent() that the bit gets cleared and all rb_set_parent_color() does is assign the new pointer cast to ulong to the node's color. I was expecting that the bit would be cleared in rb_set_parent_color().

Sorry for the noise.

../Alex