]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
rbtree: adjust root color in rb_insert_color() only when necessary
authorMichel Lespinasse <walken@google.com>
Wed, 26 Sep 2012 01:33:31 +0000 (11:33 +1000)
committerStephen Rothwell <sfr@canb.auug.org.au>
Wed, 26 Sep 2012 05:44:55 +0000 (15:44 +1000)
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: Michel Lespinasse <walken@google.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Acked-by: David Woodhouse <David.Woodhouse@intel.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Daniel Santos <daniel.santos@pobox.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
lib/rbtree.c

index 12abb8abf442244be5b459253259fe6e8ee6135f..d0be5fcaafe8e919cd46c4624146656948fbdba9 100644 (file)
@@ -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) {
+                       rb_set_black(node);
+                       break;
+               } else if (rb_is_black(parent))
+                       break;
+
                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)
                        break;
                }
        }
-
-       rb_set_black(root->rb_node);
 }
 EXPORT_SYMBOL(rb_insert_color);