3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
5 (C) 2012 Michel Lespinasse <walken@google.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/atomic.h>
25 #include <linux/rbtree_augmented.h>
26 #include <linux/export.h>
29 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
31 * 1) A node is either red or black
32 * 2) The root is black
33 * 3) All leaves (NULL) are black
34 * 4) Both children of every red node are black
35 * 5) Every simple path from root to leaves contains the same number
38 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
39 * consecutive red nodes in a path and every red node is therefore followed by
40 * a black. So if B is the number of black nodes on every simple path (as per
41 * 5), then the longest possible path due to 4 is 2B.
43 * We shall indicate color with case, where black nodes are uppercase and red
44 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
45 * parentheses and have some accompanying text comment.
49 * Notes on lockless lookups:
51 * All stores to the tree structure (rb_left and rb_right) must be done using
52 * WRITE_ONCE(). And we must not inadvertently cause (temporary) loops in the
53 * tree structure as seen in program order.
55 * These two requirements will allow lockless iteration of the tree -- not
56 * correct iteration mind you, tree rotations are not atomic so a lookup might
57 * miss entire subtrees.
59 * But they do guarantee that any such traversal will only see valid elements
60 * and that it will indeed complete -- does not get stuck in a loop.
62 * It also guarantees that if the lookup returns an element it is the 'correct'
63 * one. But not returning an element does _NOT_ mean it's not present.
67 * Stores to __rb_parent_color are not important for simple lookups so those
68 * are left undone as of now. Nor did I check for loops involving parent
72 static inline void rb_set_black(struct rb_node *rb)
74 rb->__rb_parent_color |= RB_BLACK;
77 static inline struct rb_node *rb_red_parent(struct rb_node *red)
79 return (struct rb_node *)red->__rb_parent_color;
83 * Helper function for rotations:
84 * - old's parent and color get assigned to new
85 * - old gets assigned new as a parent and 'color' as a color.
88 __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
89 struct rb_root *root, int color)
91 struct rb_node *parent = rb_parent(old);
92 new->__rb_parent_color = old->__rb_parent_color;
93 rb_set_parent_color(old, new, color);
94 __rb_change_child(old, new, parent, root);
97 static __always_inline void
98 __rb_insert(struct rb_node *node, struct rb_root *root,
99 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
101 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
105 * Loop invariant: node is red
107 * If there is a black parent, we are done.
108 * Otherwise, take some corrective action as we don't
109 * want a red root or two consecutive red nodes.
112 rb_set_parent_color(node, NULL, RB_BLACK);
114 } else if (rb_is_black(parent))
117 gparent = rb_red_parent(parent);
119 tmp = gparent->rb_right;
120 if (parent != tmp) { /* parent == gparent->rb_left */
121 if (tmp && rb_is_red(tmp)) {
123 * Case 1 - color flips
131 * However, since g's parent might be red, and
132 * 4) does not allow this, we need to recurse
135 rb_set_parent_color(tmp, gparent, RB_BLACK);
136 rb_set_parent_color(parent, gparent, RB_BLACK);
138 parent = rb_parent(node);
139 rb_set_parent_color(node, parent, RB_RED);
143 tmp = parent->rb_right;
146 * Case 2 - left rotate at parent
154 * This still leaves us in violation of 4), the
155 * continuation into Case 3 will fix that.
158 WRITE_ONCE(parent->rb_right, tmp);
159 WRITE_ONCE(node->rb_left, parent);
161 rb_set_parent_color(tmp, parent,
163 rb_set_parent_color(parent, node, RB_RED);
164 augment_rotate(parent, node);
166 tmp = node->rb_right;
170 * Case 3 - right rotate at gparent
178 WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */
179 WRITE_ONCE(parent->rb_right, gparent);
181 rb_set_parent_color(tmp, gparent, RB_BLACK);
182 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
183 augment_rotate(gparent, parent);
186 tmp = gparent->rb_left;
187 if (tmp && rb_is_red(tmp)) {
188 /* Case 1 - color flips */
189 rb_set_parent_color(tmp, gparent, RB_BLACK);
190 rb_set_parent_color(parent, gparent, RB_BLACK);
192 parent = rb_parent(node);
193 rb_set_parent_color(node, parent, RB_RED);
197 tmp = parent->rb_left;
199 /* Case 2 - right rotate at parent */
200 tmp = node->rb_right;
201 WRITE_ONCE(parent->rb_left, tmp);
202 WRITE_ONCE(node->rb_right, parent);
204 rb_set_parent_color(tmp, parent,
206 rb_set_parent_color(parent, node, RB_RED);
207 augment_rotate(parent, node);
212 /* Case 3 - left rotate at gparent */
213 WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */
214 WRITE_ONCE(parent->rb_left, gparent);
216 rb_set_parent_color(tmp, gparent, RB_BLACK);
217 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
218 augment_rotate(gparent, parent);
225 * Inline version for rb_erase() use - we want to be able to inline
226 * and eliminate the dummy_rotate callback there
228 static __always_inline void
229 ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
230 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
232 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
237 * - node is black (or NULL on first iteration)
238 * - node is not the root (parent is not NULL)
239 * - All leaf paths going through parent and node have a
240 * black node count that is 1 lower than other leaf paths.
242 sibling = parent->rb_right;
243 if (node != sibling) { /* node == parent->rb_left */
244 if (rb_is_red(sibling)) {
246 * Case 1 - left rotate at parent
254 tmp1 = sibling->rb_left;
255 WRITE_ONCE(parent->rb_right, tmp1);
256 WRITE_ONCE(sibling->rb_left, parent);
257 rb_set_parent_color(tmp1, parent, RB_BLACK);
258 __rb_rotate_set_parents(parent, sibling, root,
260 augment_rotate(parent, sibling);
263 tmp1 = sibling->rb_right;
264 if (!tmp1 || rb_is_black(tmp1)) {
265 tmp2 = sibling->rb_left;
266 if (!tmp2 || rb_is_black(tmp2)) {
268 * Case 2 - sibling color flip
269 * (p could be either color here)
277 * This leaves us violating 5) which
278 * can be fixed by flipping p to black
279 * if it was red, or by recursing at p.
280 * p is red when coming from Case 1.
282 rb_set_parent_color(sibling, parent,
284 if (rb_is_red(parent))
285 rb_set_black(parent);
288 parent = rb_parent(node);
295 * Case 3 - right rotate at sibling
296 * (p could be either color here)
306 tmp1 = tmp2->rb_right;
307 WRITE_ONCE(sibling->rb_left, tmp1);
308 WRITE_ONCE(tmp2->rb_right, sibling);
309 WRITE_ONCE(parent->rb_right, tmp2);
311 rb_set_parent_color(tmp1, sibling,
313 augment_rotate(sibling, tmp2);
318 * Case 4 - left rotate at parent + color flips
319 * (p and sl could be either color here.
320 * After rotation, p becomes black, s acquires
321 * p's color, and sl keeps its color)
329 tmp2 = sibling->rb_left;
330 WRITE_ONCE(parent->rb_right, tmp2);
331 WRITE_ONCE(sibling->rb_left, parent);
332 rb_set_parent_color(tmp1, sibling, RB_BLACK);
334 rb_set_parent(tmp2, parent);
335 __rb_rotate_set_parents(parent, sibling, root,
337 augment_rotate(parent, sibling);
340 sibling = parent->rb_left;
341 if (rb_is_red(sibling)) {
342 /* Case 1 - right rotate at parent */
343 tmp1 = sibling->rb_right;
344 WRITE_ONCE(parent->rb_left, tmp1);
345 WRITE_ONCE(sibling->rb_right, parent);
346 rb_set_parent_color(tmp1, parent, RB_BLACK);
347 __rb_rotate_set_parents(parent, sibling, root,
349 augment_rotate(parent, sibling);
352 tmp1 = sibling->rb_left;
353 if (!tmp1 || rb_is_black(tmp1)) {
354 tmp2 = sibling->rb_right;
355 if (!tmp2 || rb_is_black(tmp2)) {
356 /* Case 2 - sibling color flip */
357 rb_set_parent_color(sibling, parent,
359 if (rb_is_red(parent))
360 rb_set_black(parent);
363 parent = rb_parent(node);
369 /* Case 3 - right rotate at sibling */
370 tmp1 = tmp2->rb_left;
371 WRITE_ONCE(sibling->rb_right, tmp1);
372 WRITE_ONCE(tmp2->rb_left, sibling);
373 WRITE_ONCE(parent->rb_left, tmp2);
375 rb_set_parent_color(tmp1, sibling,
377 augment_rotate(sibling, tmp2);
381 /* Case 4 - left rotate at parent + color flips */
382 tmp2 = sibling->rb_right;
383 WRITE_ONCE(parent->rb_left, tmp2);
384 WRITE_ONCE(sibling->rb_right, parent);
385 rb_set_parent_color(tmp1, sibling, RB_BLACK);
387 rb_set_parent(tmp2, parent);
388 __rb_rotate_set_parents(parent, sibling, root,
390 augment_rotate(parent, sibling);
396 /* Non-inline version for rb_erase_augmented() use */
397 void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
398 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
400 ____rb_erase_color(parent, root, augment_rotate);
402 EXPORT_SYMBOL(__rb_erase_color);
405 * Non-augmented rbtree manipulation functions.
407 * We use dummy augmented callbacks here, and have the compiler optimize them
408 * out of the rb_insert_color() and rb_erase() function definitions.
411 static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
412 static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
413 static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
415 static const struct rb_augment_callbacks dummy_callbacks = {
416 dummy_propagate, dummy_copy, dummy_rotate
419 void rb_insert_color(struct rb_node *node, struct rb_root *root)
421 __rb_insert(node, root, dummy_rotate);
423 EXPORT_SYMBOL(rb_insert_color);
425 void rb_erase(struct rb_node *node, struct rb_root *root)
427 struct rb_node *rebalance;
428 rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
430 ____rb_erase_color(rebalance, root, dummy_rotate);
432 EXPORT_SYMBOL(rb_erase);
435 * Augmented rbtree manipulation functions.
437 * This instantiates the same __always_inline functions as in the non-augmented
438 * case, but this time with user-defined callbacks.
441 void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
442 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
444 __rb_insert(node, root, augment_rotate);
446 EXPORT_SYMBOL(__rb_insert_augmented);
449 * This function returns the first node (in sort order) of the tree.
451 struct rb_node *rb_first(const struct rb_root *root)
462 EXPORT_SYMBOL(rb_first);
464 struct rb_node *rb_last(const struct rb_root *root)
475 EXPORT_SYMBOL(rb_last);
477 struct rb_node *rb_next(const struct rb_node *node)
479 struct rb_node *parent;
481 if (RB_EMPTY_NODE(node))
485 * If we have a right-hand child, go down and then left as far
488 if (node->rb_right) {
489 node = node->rb_right;
490 while (node->rb_left)
492 return (struct rb_node *)node;
496 * No right-hand children. Everything down and left is smaller than us,
497 * so any 'next' node must be in the general direction of our parent.
498 * Go up the tree; any time the ancestor is a right-hand child of its
499 * parent, keep going up. First time it's a left-hand child of its
500 * parent, said parent is our 'next' node.
502 while ((parent = rb_parent(node)) && node == parent->rb_right)
507 EXPORT_SYMBOL(rb_next);
509 struct rb_node *rb_prev(const struct rb_node *node)
511 struct rb_node *parent;
513 if (RB_EMPTY_NODE(node))
517 * If we have a left-hand child, go down and then right as far
521 node = node->rb_left;
522 while (node->rb_right)
524 return (struct rb_node *)node;
528 * No left-hand children. Go up till we find an ancestor which
529 * is a right-hand child of its parent.
531 while ((parent = rb_parent(node)) && node == parent->rb_left)
536 EXPORT_SYMBOL(rb_prev);
538 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
539 struct rb_root *root)
541 struct rb_node *parent = rb_parent(victim);
543 /* Copy the pointers/colour from the victim to the replacement */
546 /* Set the surrounding nodes to point to the replacement */
548 rb_set_parent(victim->rb_left, new);
549 if (victim->rb_right)
550 rb_set_parent(victim->rb_right, new);
551 __rb_change_child(victim, new, parent, root);
553 EXPORT_SYMBOL(rb_replace_node);
555 void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
556 struct rb_root *root)
558 struct rb_node *parent = rb_parent(victim);
560 /* Copy the pointers/colour from the victim to the replacement */
563 /* Set the surrounding nodes to point to the replacement */
565 rb_set_parent(victim->rb_left, new);
566 if (victim->rb_right)
567 rb_set_parent(victim->rb_right, new);
569 /* Set the parent's pointer to the new node last after an RCU barrier
570 * so that the pointers onwards are seen to be set correctly when doing
571 * an RCU walk over the tree.
573 __rb_change_child_rcu(victim, new, parent, root);
575 EXPORT_SYMBOL(rb_replace_node_rcu);
577 static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
581 node = node->rb_left;
582 else if (node->rb_right)
583 node = node->rb_right;
585 return (struct rb_node *)node;
589 struct rb_node *rb_next_postorder(const struct rb_node *node)
591 const struct rb_node *parent;
594 parent = rb_parent(node);
596 /* If we're sitting on node, we've already seen our children */
597 if (parent && node == parent->rb_left && parent->rb_right) {
598 /* If we are the parent's left node, go to the parent's right
599 * node then all the way down to the left */
600 return rb_left_deepest_node(parent->rb_right);
602 /* Otherwise we are the parent's right node, and the parent
604 return (struct rb_node *)parent;
606 EXPORT_SYMBOL(rb_next_postorder);
608 struct rb_node *rb_first_postorder(const struct rb_root *root)
613 return rb_left_deepest_node(root->rb_node);
615 EXPORT_SYMBOL(rb_first_postorder);