These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / lib / rbtree.c
1 /*
2   Red Black Trees
3   (C) 1999  Andrea Arcangeli <andrea@suse.de>
4   (C) 2002  David Woodhouse <dwmw2@infradead.org>
5   (C) 2012  Michel Lespinasse <walken@google.com>
6
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.
11
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.
16
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
20
21   linux/lib/rbtree.c
22 */
23
24 #include <linux/rbtree_augmented.h>
25 #include <linux/export.h>
26 #include <linux/rcupdate.h>
27
28 /*
29  * red-black trees properties:  http://en.wikipedia.org/wiki/Rbtree
30  *
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
36  *     of black nodes.
37  *
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.
42  *
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.
46  */
47
48 /*
49  * Notes on lockless lookups:
50  *
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.
54  *
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.
58  *
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.
61  *
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.
64  *
65  * NOTE:
66  *
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
69  * pointers.
70  */
71
72 static inline void rb_set_black(struct rb_node *rb)
73 {
74         rb->__rb_parent_color |= RB_BLACK;
75 }
76
77 static inline struct rb_node *rb_red_parent(struct rb_node *red)
78 {
79         return (struct rb_node *)red->__rb_parent_color;
80 }
81
82 /*
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.
86  */
87 static inline void
88 __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
89                         struct rb_root *root, int color)
90 {
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);
95 }
96
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))
100 {
101         struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
102
103         while (true) {
104                 /*
105                  * Loop invariant: node is red
106                  *
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.
110                  */
111                 if (!parent) {
112                         rb_set_parent_color(node, NULL, RB_BLACK);
113                         break;
114                 } else if (rb_is_black(parent))
115                         break;
116
117                 gparent = rb_red_parent(parent);
118
119                 tmp = gparent->rb_right;
120                 if (parent != tmp) {    /* parent == gparent->rb_left */
121                         if (tmp && rb_is_red(tmp)) {
122                                 /*
123                                  * Case 1 - color flips
124                                  *
125                                  *       G            g
126                                  *      / \          / \
127                                  *     p   u  -->   P   U
128                                  *    /            /
129                                  *   n            n
130                                  *
131                                  * However, since g's parent might be red, and
132                                  * 4) does not allow this, we need to recurse
133                                  * at g.
134                                  */
135                                 rb_set_parent_color(tmp, gparent, RB_BLACK);
136                                 rb_set_parent_color(parent, gparent, RB_BLACK);
137                                 node = gparent;
138                                 parent = rb_parent(node);
139                                 rb_set_parent_color(node, parent, RB_RED);
140                                 continue;
141                         }
142
143                         tmp = parent->rb_right;
144                         if (node == tmp) {
145                                 /*
146                                  * Case 2 - left rotate at parent
147                                  *
148                                  *      G             G
149                                  *     / \           / \
150                                  *    p   U  -->    n   U
151                                  *     \           /
152                                  *      n         p
153                                  *
154                                  * This still leaves us in violation of 4), the
155                                  * continuation into Case 3 will fix that.
156                                  */
157                                 tmp = node->rb_left;
158                                 WRITE_ONCE(parent->rb_right, tmp);
159                                 WRITE_ONCE(node->rb_left, parent);
160                                 if (tmp)
161                                         rb_set_parent_color(tmp, parent,
162                                                             RB_BLACK);
163                                 rb_set_parent_color(parent, node, RB_RED);
164                                 augment_rotate(parent, node);
165                                 parent = node;
166                                 tmp = node->rb_right;
167                         }
168
169                         /*
170                          * Case 3 - right rotate at gparent
171                          *
172                          *        G           P
173                          *       / \         / \
174                          *      p   U  -->  n   g
175                          *     /                 \
176                          *    n                   U
177                          */
178                         WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */
179                         WRITE_ONCE(parent->rb_right, gparent);
180                         if (tmp)
181                                 rb_set_parent_color(tmp, gparent, RB_BLACK);
182                         __rb_rotate_set_parents(gparent, parent, root, RB_RED);
183                         augment_rotate(gparent, parent);
184                         break;
185                 } else {
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);
191                                 node = gparent;
192                                 parent = rb_parent(node);
193                                 rb_set_parent_color(node, parent, RB_RED);
194                                 continue;
195                         }
196
197                         tmp = parent->rb_left;
198                         if (node == tmp) {
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);
203                                 if (tmp)
204                                         rb_set_parent_color(tmp, parent,
205                                                             RB_BLACK);
206                                 rb_set_parent_color(parent, node, RB_RED);
207                                 augment_rotate(parent, node);
208                                 parent = node;
209                                 tmp = node->rb_left;
210                         }
211
212                         /* Case 3 - left rotate at gparent */
213                         WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */
214                         WRITE_ONCE(parent->rb_left, gparent);
215                         if (tmp)
216                                 rb_set_parent_color(tmp, gparent, RB_BLACK);
217                         __rb_rotate_set_parents(gparent, parent, root, RB_RED);
218                         augment_rotate(gparent, parent);
219                         break;
220                 }
221         }
222 }
223
224 /*
225  * Inline version for rb_erase() use - we want to be able to inline
226  * and eliminate the dummy_rotate callback there
227  */
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))
231 {
232         struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
233
234         while (true) {
235                 /*
236                  * Loop invariants:
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.
241                  */
242                 sibling = parent->rb_right;
243                 if (node != sibling) {  /* node == parent->rb_left */
244                         if (rb_is_red(sibling)) {
245                                 /*
246                                  * Case 1 - left rotate at parent
247                                  *
248                                  *     P               S
249                                  *    / \             / \
250                                  *   N   s    -->    p   Sr
251                                  *      / \         / \
252                                  *     Sl  Sr      N   Sl
253                                  */
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,
259                                                         RB_RED);
260                                 augment_rotate(parent, sibling);
261                                 sibling = tmp1;
262                         }
263                         tmp1 = sibling->rb_right;
264                         if (!tmp1 || rb_is_black(tmp1)) {
265                                 tmp2 = sibling->rb_left;
266                                 if (!tmp2 || rb_is_black(tmp2)) {
267                                         /*
268                                          * Case 2 - sibling color flip
269                                          * (p could be either color here)
270                                          *
271                                          *    (p)           (p)
272                                          *    / \           / \
273                                          *   N   S    -->  N   s
274                                          *      / \           / \
275                                          *     Sl  Sr        Sl  Sr
276                                          *
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.
281                                          */
282                                         rb_set_parent_color(sibling, parent,
283                                                             RB_RED);
284                                         if (rb_is_red(parent))
285                                                 rb_set_black(parent);
286                                         else {
287                                                 node = parent;
288                                                 parent = rb_parent(node);
289                                                 if (parent)
290                                                         continue;
291                                         }
292                                         break;
293                                 }
294                                 /*
295                                  * Case 3 - right rotate at sibling
296                                  * (p could be either color here)
297                                  *
298                                  *   (p)           (p)
299                                  *   / \           / \
300                                  *  N   S    -->  N   Sl
301                                  *     / \             \
302                                  *    sl  Sr            s
303                                  *                       \
304                                  *                        Sr
305                                  */
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);
310                                 if (tmp1)
311                                         rb_set_parent_color(tmp1, sibling,
312                                                             RB_BLACK);
313                                 augment_rotate(sibling, tmp2);
314                                 tmp1 = sibling;
315                                 sibling = tmp2;
316                         }
317                         /*
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)
322                          *
323                          *      (p)             (s)
324                          *      / \             / \
325                          *     N   S     -->   P   Sr
326                          *        / \         / \
327                          *      (sl) sr      N  (sl)
328                          */
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);
333                         if (tmp2)
334                                 rb_set_parent(tmp2, parent);
335                         __rb_rotate_set_parents(parent, sibling, root,
336                                                 RB_BLACK);
337                         augment_rotate(parent, sibling);
338                         break;
339                 } else {
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,
348                                                         RB_RED);
349                                 augment_rotate(parent, sibling);
350                                 sibling = tmp1;
351                         }
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,
358                                                             RB_RED);
359                                         if (rb_is_red(parent))
360                                                 rb_set_black(parent);
361                                         else {
362                                                 node = parent;
363                                                 parent = rb_parent(node);
364                                                 if (parent)
365                                                         continue;
366                                         }
367                                         break;
368                                 }
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);
374                                 if (tmp1)
375                                         rb_set_parent_color(tmp1, sibling,
376                                                             RB_BLACK);
377                                 augment_rotate(sibling, tmp2);
378                                 tmp1 = sibling;
379                                 sibling = tmp2;
380                         }
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);
386                         if (tmp2)
387                                 rb_set_parent(tmp2, parent);
388                         __rb_rotate_set_parents(parent, sibling, root,
389                                                 RB_BLACK);
390                         augment_rotate(parent, sibling);
391                         break;
392                 }
393         }
394 }
395
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))
399 {
400         ____rb_erase_color(parent, root, augment_rotate);
401 }
402 EXPORT_SYMBOL(__rb_erase_color);
403
404 /*
405  * Non-augmented rbtree manipulation functions.
406  *
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.
409  */
410
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) {}
414
415 static const struct rb_augment_callbacks dummy_callbacks = {
416         dummy_propagate, dummy_copy, dummy_rotate
417 };
418
419 void rb_insert_color(struct rb_node *node, struct rb_root *root)
420 {
421         __rb_insert(node, root, dummy_rotate);
422 }
423 EXPORT_SYMBOL(rb_insert_color);
424
425 void rb_erase(struct rb_node *node, struct rb_root *root)
426 {
427         struct rb_node *rebalance;
428         rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
429         if (rebalance)
430                 ____rb_erase_color(rebalance, root, dummy_rotate);
431 }
432 EXPORT_SYMBOL(rb_erase);
433
434 /*
435  * Augmented rbtree manipulation functions.
436  *
437  * This instantiates the same __always_inline functions as in the non-augmented
438  * case, but this time with user-defined callbacks.
439  */
440
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))
443 {
444         __rb_insert(node, root, augment_rotate);
445 }
446 EXPORT_SYMBOL(__rb_insert_augmented);
447
448 /*
449  * This function returns the first node (in sort order) of the tree.
450  */
451 struct rb_node *rb_first(const struct rb_root *root)
452 {
453         struct rb_node  *n;
454
455         n = root->rb_node;
456         if (!n)
457                 return NULL;
458         while (n->rb_left)
459                 n = n->rb_left;
460         return n;
461 }
462 EXPORT_SYMBOL(rb_first);
463
464 struct rb_node *rb_last(const struct rb_root *root)
465 {
466         struct rb_node  *n;
467
468         n = root->rb_node;
469         if (!n)
470                 return NULL;
471         while (n->rb_right)
472                 n = n->rb_right;
473         return n;
474 }
475 EXPORT_SYMBOL(rb_last);
476
477 struct rb_node *rb_next(const struct rb_node *node)
478 {
479         struct rb_node *parent;
480
481         if (RB_EMPTY_NODE(node))
482                 return NULL;
483
484         /*
485          * If we have a right-hand child, go down and then left as far
486          * as we can.
487          */
488         if (node->rb_right) {
489                 node = node->rb_right; 
490                 while (node->rb_left)
491                         node=node->rb_left;
492                 return (struct rb_node *)node;
493         }
494
495         /*
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.
501          */
502         while ((parent = rb_parent(node)) && node == parent->rb_right)
503                 node = parent;
504
505         return parent;
506 }
507 EXPORT_SYMBOL(rb_next);
508
509 struct rb_node *rb_prev(const struct rb_node *node)
510 {
511         struct rb_node *parent;
512
513         if (RB_EMPTY_NODE(node))
514                 return NULL;
515
516         /*
517          * If we have a left-hand child, go down and then right as far
518          * as we can.
519          */
520         if (node->rb_left) {
521                 node = node->rb_left; 
522                 while (node->rb_right)
523                         node=node->rb_right;
524                 return (struct rb_node *)node;
525         }
526
527         /*
528          * No left-hand children. Go up till we find an ancestor which
529          * is a right-hand child of its parent.
530          */
531         while ((parent = rb_parent(node)) && node == parent->rb_left)
532                 node = parent;
533
534         return parent;
535 }
536 EXPORT_SYMBOL(rb_prev);
537
538 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
539                      struct rb_root *root)
540 {
541         struct rb_node *parent = rb_parent(victim);
542
543         /* Set the surrounding nodes to point to the replacement */
544         __rb_change_child(victim, new, parent, root);
545         if (victim->rb_left)
546                 rb_set_parent(victim->rb_left, new);
547         if (victim->rb_right)
548                 rb_set_parent(victim->rb_right, new);
549
550         /* Copy the pointers/colour from the victim to the replacement */
551         *new = *victim;
552 }
553 EXPORT_SYMBOL(rb_replace_node);
554
555 static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
556 {
557         for (;;) {
558                 if (node->rb_left)
559                         node = node->rb_left;
560                 else if (node->rb_right)
561                         node = node->rb_right;
562                 else
563                         return (struct rb_node *)node;
564         }
565 }
566
567 struct rb_node *rb_next_postorder(const struct rb_node *node)
568 {
569         const struct rb_node *parent;
570         if (!node)
571                 return NULL;
572         parent = rb_parent(node);
573
574         /* If we're sitting on node, we've already seen our children */
575         if (parent && node == parent->rb_left && parent->rb_right) {
576                 /* If we are the parent's left node, go to the parent's right
577                  * node then all the way down to the left */
578                 return rb_left_deepest_node(parent->rb_right);
579         } else
580                 /* Otherwise we are the parent's right node, and the parent
581                  * should be next */
582                 return (struct rb_node *)parent;
583 }
584 EXPORT_SYMBOL(rb_next_postorder);
585
586 struct rb_node *rb_first_postorder(const struct rb_root *root)
587 {
588         if (!root->rb_node)
589                 return NULL;
590
591         return rb_left_deepest_node(root->rb_node);
592 }
593 EXPORT_SYMBOL(rb_first_postorder);
594
595 void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
596                                     struct rb_node **rb_link)
597 {
598         node->__rb_parent_color = (unsigned long)parent;
599         node->rb_left = node->rb_right = NULL;
600
601         rcu_assign_pointer(*rb_link, node);
602 }
603 EXPORT_SYMBOL(rb_link_node_rcu);