Commit 6d58452d authored by Michel Lespinasse's avatar Michel Lespinasse Committed by Linus Torvalds

rbtree: adjust root color in rb_insert_color() only when necessary

The root node of an rbtree must always be black.  However,
rb_insert_color() only needs to maintain this invariant when it has been
broken - that is, when it exits the loop due to the current (red) node
being the root.  In all other cases (exiting after tree rotations, or
exiting due to an existing black parent) the invariant is already
satisfied, so there is no need to adjust the root node color.
Signed-off-by: default avatarMichel Lespinasse <>
Cc: Andrea Arcangeli <>
Acked-by: default avatarDavid Woodhouse <>
Cc: Rik van Riel <>
Cc: Peter Zijlstra <>
Cc: Daniel Santos <>
Cc: Jens Axboe <>
Cc: "Eric W. Biederman" <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent 1f052865
......@@ -91,8 +91,21 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
struct rb_node *parent, *gparent;
while ((parent = rb_parent(node)) && rb_is_red(parent))
while (true) {
* Loop invariant: node is red
* If there is a black parent, we are done.
* Otherwise, take some corrective action as we don't
* want a red root or two consecutive red nodes.
parent = rb_parent(node);
if (!parent) {
} else if (rb_is_black(parent))
gparent = rb_parent(parent);
if (parent == gparent->rb_left)
......@@ -142,8 +155,6 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment