11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds Red Black Trees 31da177e4SLinus Torvalds (C) 1999 Andrea Arcangeli <andrea@suse.de> 41da177e4SLinus Torvalds (C) 2002 David Woodhouse <dwmw2@infradead.org> 546b6135aSMichel Lespinasse (C) 2012 Michel Lespinasse <walken@google.com> 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds it under the terms of the GNU General Public License as published by 91da177e4SLinus Torvalds the Free Software Foundation; either version 2 of the License, or 101da177e4SLinus Torvalds (at your option) any later version. 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds This program is distributed in the hope that it will be useful, 131da177e4SLinus Torvalds but WITHOUT ANY WARRANTY; without even the implied warranty of 141da177e4SLinus Torvalds MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 151da177e4SLinus Torvalds GNU General Public License for more details. 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds You should have received a copy of the GNU General Public License 181da177e4SLinus Torvalds along with this program; if not, write to the Free Software 191da177e4SLinus Torvalds Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds linux/lib/rbtree.c 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 249c079addSMichel Lespinasse #include <linux/rbtree_augmented.h> 258bc3bcc9SPaul Gortmaker #include <linux/export.h> 261da177e4SLinus Torvalds 275bc9188aSMichel Lespinasse /* 285bc9188aSMichel Lespinasse * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree 295bc9188aSMichel Lespinasse * 305bc9188aSMichel Lespinasse * 1) A node is either red or black 315bc9188aSMichel Lespinasse * 2) The root is black 325bc9188aSMichel Lespinasse * 3) All leaves (NULL) are black 335bc9188aSMichel Lespinasse * 4) Both children of every red node are black 345bc9188aSMichel Lespinasse * 5) Every simple path from root to leaves contains the same number 355bc9188aSMichel Lespinasse * of black nodes. 365bc9188aSMichel Lespinasse * 375bc9188aSMichel Lespinasse * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two 385bc9188aSMichel Lespinasse * consecutive red nodes in a path and every red node is therefore followed by 395bc9188aSMichel Lespinasse * a black. So if B is the number of black nodes on every simple path (as per 405bc9188aSMichel Lespinasse * 5), then the longest possible path due to 4 is 2B. 415bc9188aSMichel Lespinasse * 425bc9188aSMichel Lespinasse * We shall indicate color with case, where black nodes are uppercase and red 436280d235SMichel Lespinasse * nodes will be lowercase. Unknown color nodes shall be drawn as red within 446280d235SMichel Lespinasse * parentheses and have some accompanying text comment. 455bc9188aSMichel Lespinasse */ 465bc9188aSMichel Lespinasse 47d72da4a4SPeter Zijlstra /* 48d72da4a4SPeter Zijlstra * Notes on lockless lookups: 49d72da4a4SPeter Zijlstra * 50d72da4a4SPeter Zijlstra * All stores to the tree structure (rb_left and rb_right) must be done using 51d72da4a4SPeter Zijlstra * WRITE_ONCE(). And we must not inadvertently cause (temporary) loops in the 52d72da4a4SPeter Zijlstra * tree structure as seen in program order. 53d72da4a4SPeter Zijlstra * 54d72da4a4SPeter Zijlstra * These two requirements will allow lockless iteration of the tree -- not 55d72da4a4SPeter Zijlstra * correct iteration mind you, tree rotations are not atomic so a lookup might 56d72da4a4SPeter Zijlstra * miss entire subtrees. 57d72da4a4SPeter Zijlstra * 58d72da4a4SPeter Zijlstra * But they do guarantee that any such traversal will only see valid elements 59d72da4a4SPeter Zijlstra * and that it will indeed complete -- does not get stuck in a loop. 60d72da4a4SPeter Zijlstra * 61d72da4a4SPeter Zijlstra * It also guarantees that if the lookup returns an element it is the 'correct' 62d72da4a4SPeter Zijlstra * one. But not returning an element does _NOT_ mean it's not present. 63d72da4a4SPeter Zijlstra * 64d72da4a4SPeter Zijlstra * NOTE: 65d72da4a4SPeter Zijlstra * 66d72da4a4SPeter Zijlstra * Stores to __rb_parent_color are not important for simple lookups so those 67d72da4a4SPeter Zijlstra * are left undone as of now. Nor did I check for loops involving parent 68d72da4a4SPeter Zijlstra * pointers. 69d72da4a4SPeter Zijlstra */ 70d72da4a4SPeter Zijlstra 7146b6135aSMichel Lespinasse static inline void rb_set_black(struct rb_node *rb) 7246b6135aSMichel Lespinasse { 7346b6135aSMichel Lespinasse rb->__rb_parent_color |= RB_BLACK; 7446b6135aSMichel Lespinasse } 7546b6135aSMichel Lespinasse 765bc9188aSMichel Lespinasse static inline struct rb_node *rb_red_parent(struct rb_node *red) 775bc9188aSMichel Lespinasse { 785bc9188aSMichel Lespinasse return (struct rb_node *)red->__rb_parent_color; 795bc9188aSMichel Lespinasse } 805bc9188aSMichel Lespinasse 815bc9188aSMichel Lespinasse /* 825bc9188aSMichel Lespinasse * Helper function for rotations: 835bc9188aSMichel Lespinasse * - old's parent and color get assigned to new 845bc9188aSMichel Lespinasse * - old gets assigned new as a parent and 'color' as a color. 855bc9188aSMichel Lespinasse */ 865bc9188aSMichel Lespinasse static inline void 875bc9188aSMichel Lespinasse __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new, 885bc9188aSMichel Lespinasse struct rb_root *root, int color) 895bc9188aSMichel Lespinasse { 905bc9188aSMichel Lespinasse struct rb_node *parent = rb_parent(old); 915bc9188aSMichel Lespinasse new->__rb_parent_color = old->__rb_parent_color; 925bc9188aSMichel Lespinasse rb_set_parent_color(old, new, color); 937abc704aSMichel Lespinasse __rb_change_child(old, new, parent, root); 945bc9188aSMichel Lespinasse } 955bc9188aSMichel Lespinasse 9614b94af0SMichel Lespinasse static __always_inline void 9714b94af0SMichel Lespinasse __rb_insert(struct rb_node *node, struct rb_root *root, 98cd9e61edSDavidlohr Bueso bool newleft, struct rb_node **leftmost, 9914b94af0SMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 1001da177e4SLinus Torvalds { 1015bc9188aSMichel Lespinasse struct rb_node *parent = rb_red_parent(node), *gparent, *tmp; 1021da177e4SLinus Torvalds 103cd9e61edSDavidlohr Bueso if (newleft) 104cd9e61edSDavidlohr Bueso *leftmost = node; 105cd9e61edSDavidlohr Bueso 1066d58452dSMichel Lespinasse while (true) { 1076d58452dSMichel Lespinasse /* 108*2aadf7fcSDavidlohr Bueso * Loop invariant: node is red. 1096d58452dSMichel Lespinasse */ 110*2aadf7fcSDavidlohr Bueso if (unlikely(!parent)) { 111*2aadf7fcSDavidlohr Bueso /* 112*2aadf7fcSDavidlohr Bueso * The inserted node is root. Either this is the 113*2aadf7fcSDavidlohr Bueso * first node, or we recursed at Case 1 below and 114*2aadf7fcSDavidlohr Bueso * are no longer violating 4). 115*2aadf7fcSDavidlohr Bueso */ 1165bc9188aSMichel Lespinasse rb_set_parent_color(node, NULL, RB_BLACK); 1176d58452dSMichel Lespinasse break; 118*2aadf7fcSDavidlohr Bueso } 119*2aadf7fcSDavidlohr Bueso 120*2aadf7fcSDavidlohr Bueso /* 121*2aadf7fcSDavidlohr Bueso * If there is a black parent, we are done. 122*2aadf7fcSDavidlohr Bueso * Otherwise, take some corrective action as, 123*2aadf7fcSDavidlohr Bueso * per 4), we don't want a red root or two 124*2aadf7fcSDavidlohr Bueso * consecutive red nodes. 125*2aadf7fcSDavidlohr Bueso */ 126*2aadf7fcSDavidlohr Bueso if(rb_is_black(parent)) 1276d58452dSMichel Lespinasse break; 1286d58452dSMichel Lespinasse 1295bc9188aSMichel Lespinasse gparent = rb_red_parent(parent); 1301da177e4SLinus Torvalds 1315bc9188aSMichel Lespinasse tmp = gparent->rb_right; 13259633abfSMichel Lespinasse if (parent != tmp) { /* parent == gparent->rb_left */ 1335bc9188aSMichel Lespinasse if (tmp && rb_is_red(tmp)) { 1345bc9188aSMichel Lespinasse /* 1355bc9188aSMichel Lespinasse * Case 1 - color flips 1365bc9188aSMichel Lespinasse * 1375bc9188aSMichel Lespinasse * G g 1385bc9188aSMichel Lespinasse * / \ / \ 1395bc9188aSMichel Lespinasse * p u --> P U 1405bc9188aSMichel Lespinasse * / / 1411b9c53e8SWei Yang * n n 1425bc9188aSMichel Lespinasse * 1435bc9188aSMichel Lespinasse * However, since g's parent might be red, and 1445bc9188aSMichel Lespinasse * 4) does not allow this, we need to recurse 1455bc9188aSMichel Lespinasse * at g. 1465bc9188aSMichel Lespinasse */ 1475bc9188aSMichel Lespinasse rb_set_parent_color(tmp, gparent, RB_BLACK); 1485bc9188aSMichel Lespinasse rb_set_parent_color(parent, gparent, RB_BLACK); 1491da177e4SLinus Torvalds node = gparent; 1505bc9188aSMichel Lespinasse parent = rb_parent(node); 1515bc9188aSMichel Lespinasse rb_set_parent_color(node, parent, RB_RED); 1521da177e4SLinus Torvalds continue; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 15559633abfSMichel Lespinasse tmp = parent->rb_right; 15659633abfSMichel Lespinasse if (node == tmp) { 1575bc9188aSMichel Lespinasse /* 1585bc9188aSMichel Lespinasse * Case 2 - left rotate at parent 1595bc9188aSMichel Lespinasse * 1605bc9188aSMichel Lespinasse * G G 1615bc9188aSMichel Lespinasse * / \ / \ 1625bc9188aSMichel Lespinasse * p U --> n U 1635bc9188aSMichel Lespinasse * \ / 1645bc9188aSMichel Lespinasse * n p 1655bc9188aSMichel Lespinasse * 1665bc9188aSMichel Lespinasse * This still leaves us in violation of 4), the 1675bc9188aSMichel Lespinasse * continuation into Case 3 will fix that. 1685bc9188aSMichel Lespinasse */ 169d72da4a4SPeter Zijlstra tmp = node->rb_left; 170d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_right, tmp); 171d72da4a4SPeter Zijlstra WRITE_ONCE(node->rb_left, parent); 1725bc9188aSMichel Lespinasse if (tmp) 1735bc9188aSMichel Lespinasse rb_set_parent_color(tmp, parent, 1745bc9188aSMichel Lespinasse RB_BLACK); 1755bc9188aSMichel Lespinasse rb_set_parent_color(parent, node, RB_RED); 17614b94af0SMichel Lespinasse augment_rotate(parent, node); 1771da177e4SLinus Torvalds parent = node; 17859633abfSMichel Lespinasse tmp = node->rb_right; 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1815bc9188aSMichel Lespinasse /* 1825bc9188aSMichel Lespinasse * Case 3 - right rotate at gparent 1835bc9188aSMichel Lespinasse * 1845bc9188aSMichel Lespinasse * G P 1855bc9188aSMichel Lespinasse * / \ / \ 1865bc9188aSMichel Lespinasse * p U --> n g 1875bc9188aSMichel Lespinasse * / \ 1885bc9188aSMichel Lespinasse * n U 1895bc9188aSMichel Lespinasse */ 190d72da4a4SPeter Zijlstra WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */ 191d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_right, gparent); 1925bc9188aSMichel Lespinasse if (tmp) 1935bc9188aSMichel Lespinasse rb_set_parent_color(tmp, gparent, RB_BLACK); 1945bc9188aSMichel Lespinasse __rb_rotate_set_parents(gparent, parent, root, RB_RED); 19514b94af0SMichel Lespinasse augment_rotate(gparent, parent); 1961f052865SMichel Lespinasse break; 1971da177e4SLinus Torvalds } else { 1985bc9188aSMichel Lespinasse tmp = gparent->rb_left; 1995bc9188aSMichel Lespinasse if (tmp && rb_is_red(tmp)) { 2005bc9188aSMichel Lespinasse /* Case 1 - color flips */ 2015bc9188aSMichel Lespinasse rb_set_parent_color(tmp, gparent, RB_BLACK); 2025bc9188aSMichel Lespinasse rb_set_parent_color(parent, gparent, RB_BLACK); 2031da177e4SLinus Torvalds node = gparent; 2045bc9188aSMichel Lespinasse parent = rb_parent(node); 2055bc9188aSMichel Lespinasse rb_set_parent_color(node, parent, RB_RED); 2061da177e4SLinus Torvalds continue; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 20959633abfSMichel Lespinasse tmp = parent->rb_left; 21059633abfSMichel Lespinasse if (node == tmp) { 2115bc9188aSMichel Lespinasse /* Case 2 - right rotate at parent */ 212d72da4a4SPeter Zijlstra tmp = node->rb_right; 213d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_left, tmp); 214d72da4a4SPeter Zijlstra WRITE_ONCE(node->rb_right, parent); 2155bc9188aSMichel Lespinasse if (tmp) 2165bc9188aSMichel Lespinasse rb_set_parent_color(tmp, parent, 2175bc9188aSMichel Lespinasse RB_BLACK); 2185bc9188aSMichel Lespinasse rb_set_parent_color(parent, node, RB_RED); 21914b94af0SMichel Lespinasse augment_rotate(parent, node); 2201da177e4SLinus Torvalds parent = node; 22159633abfSMichel Lespinasse tmp = node->rb_left; 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2245bc9188aSMichel Lespinasse /* Case 3 - left rotate at gparent */ 225d72da4a4SPeter Zijlstra WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */ 226d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_left, gparent); 2275bc9188aSMichel Lespinasse if (tmp) 2285bc9188aSMichel Lespinasse rb_set_parent_color(tmp, gparent, RB_BLACK); 2295bc9188aSMichel Lespinasse __rb_rotate_set_parents(gparent, parent, root, RB_RED); 23014b94af0SMichel Lespinasse augment_rotate(gparent, parent); 2311f052865SMichel Lespinasse break; 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2363cb7a563SMichel Lespinasse /* 2373cb7a563SMichel Lespinasse * Inline version for rb_erase() use - we want to be able to inline 2383cb7a563SMichel Lespinasse * and eliminate the dummy_rotate callback there 2393cb7a563SMichel Lespinasse */ 2403cb7a563SMichel Lespinasse static __always_inline void 2413cb7a563SMichel Lespinasse ____rb_erase_color(struct rb_node *parent, struct rb_root *root, 2429c079addSMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 2431da177e4SLinus Torvalds { 24446b6135aSMichel Lespinasse struct rb_node *node = NULL, *sibling, *tmp1, *tmp2; 2451da177e4SLinus Torvalds 246d6ff1273SMichel Lespinasse while (true) { 247d6ff1273SMichel Lespinasse /* 24846b6135aSMichel Lespinasse * Loop invariants: 24946b6135aSMichel Lespinasse * - node is black (or NULL on first iteration) 25046b6135aSMichel Lespinasse * - node is not the root (parent is not NULL) 25146b6135aSMichel Lespinasse * - All leaf paths going through parent and node have a 252d6ff1273SMichel Lespinasse * black node count that is 1 lower than other leaf paths. 253d6ff1273SMichel Lespinasse */ 2546280d235SMichel Lespinasse sibling = parent->rb_right; 25559633abfSMichel Lespinasse if (node != sibling) { /* node == parent->rb_left */ 2566280d235SMichel Lespinasse if (rb_is_red(sibling)) { 2576280d235SMichel Lespinasse /* 2586280d235SMichel Lespinasse * Case 1 - left rotate at parent 2596280d235SMichel Lespinasse * 2606280d235SMichel Lespinasse * P S 2616280d235SMichel Lespinasse * / \ / \ 2626280d235SMichel Lespinasse * N s --> p Sr 2636280d235SMichel Lespinasse * / \ / \ 2646280d235SMichel Lespinasse * Sl Sr N Sl 2656280d235SMichel Lespinasse */ 266d72da4a4SPeter Zijlstra tmp1 = sibling->rb_left; 267d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_right, tmp1); 268d72da4a4SPeter Zijlstra WRITE_ONCE(sibling->rb_left, parent); 2696280d235SMichel Lespinasse rb_set_parent_color(tmp1, parent, RB_BLACK); 2706280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 2716280d235SMichel Lespinasse RB_RED); 2729c079addSMichel Lespinasse augment_rotate(parent, sibling); 2736280d235SMichel Lespinasse sibling = tmp1; 2741da177e4SLinus Torvalds } 2756280d235SMichel Lespinasse tmp1 = sibling->rb_right; 2766280d235SMichel Lespinasse if (!tmp1 || rb_is_black(tmp1)) { 2776280d235SMichel Lespinasse tmp2 = sibling->rb_left; 2786280d235SMichel Lespinasse if (!tmp2 || rb_is_black(tmp2)) { 2796280d235SMichel Lespinasse /* 2806280d235SMichel Lespinasse * Case 2 - sibling color flip 2816280d235SMichel Lespinasse * (p could be either color here) 2826280d235SMichel Lespinasse * 2836280d235SMichel Lespinasse * (p) (p) 2846280d235SMichel Lespinasse * / \ / \ 2856280d235SMichel Lespinasse * N S --> N s 2866280d235SMichel Lespinasse * / \ / \ 2876280d235SMichel Lespinasse * Sl Sr Sl Sr 2886280d235SMichel Lespinasse * 28946b6135aSMichel Lespinasse * This leaves us violating 5) which 29046b6135aSMichel Lespinasse * can be fixed by flipping p to black 29146b6135aSMichel Lespinasse * if it was red, or by recursing at p. 29246b6135aSMichel Lespinasse * p is red when coming from Case 1. 2936280d235SMichel Lespinasse */ 2946280d235SMichel Lespinasse rb_set_parent_color(sibling, parent, 2956280d235SMichel Lespinasse RB_RED); 29646b6135aSMichel Lespinasse if (rb_is_red(parent)) 29746b6135aSMichel Lespinasse rb_set_black(parent); 29846b6135aSMichel Lespinasse else { 2991da177e4SLinus Torvalds node = parent; 30055a98102SDavid Woodhouse parent = rb_parent(node); 30146b6135aSMichel Lespinasse if (parent) 302e125d147SMichel Lespinasse continue; 3031da177e4SLinus Torvalds } 30446b6135aSMichel Lespinasse break; 30546b6135aSMichel Lespinasse } 3066280d235SMichel Lespinasse /* 3076280d235SMichel Lespinasse * Case 3 - right rotate at sibling 3086280d235SMichel Lespinasse * (p could be either color here) 3096280d235SMichel Lespinasse * 3106280d235SMichel Lespinasse * (p) (p) 3116280d235SMichel Lespinasse * / \ / \ 312ce093a04SJie Chen * N S --> N sl 3136280d235SMichel Lespinasse * / \ \ 314ce093a04SJie Chen * sl Sr S 315ce093a04SJie Chen * \ 316ce093a04SJie Chen * Sr 317ce093a04SJie Chen * 318ce093a04SJie Chen * Note: p might be red, and then both 319ce093a04SJie Chen * p and sl are red after rotation(which 320ce093a04SJie Chen * breaks property 4). This is fixed in 321ce093a04SJie Chen * Case 4 (in __rb_rotate_set_parents() 322ce093a04SJie Chen * which set sl the color of p 323ce093a04SJie Chen * and set p RB_BLACK) 324ce093a04SJie Chen * 325ce093a04SJie Chen * (p) (sl) 326ce093a04SJie Chen * / \ / \ 327ce093a04SJie Chen * N sl --> P S 328ce093a04SJie Chen * \ / \ 329ce093a04SJie Chen * S N Sr 3306280d235SMichel Lespinasse * \ 3316280d235SMichel Lespinasse * Sr 3326280d235SMichel Lespinasse */ 333d72da4a4SPeter Zijlstra tmp1 = tmp2->rb_right; 334d72da4a4SPeter Zijlstra WRITE_ONCE(sibling->rb_left, tmp1); 335d72da4a4SPeter Zijlstra WRITE_ONCE(tmp2->rb_right, sibling); 336d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_right, tmp2); 3376280d235SMichel Lespinasse if (tmp1) 3386280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, 3396280d235SMichel Lespinasse RB_BLACK); 3409c079addSMichel Lespinasse augment_rotate(sibling, tmp2); 3416280d235SMichel Lespinasse tmp1 = sibling; 3426280d235SMichel Lespinasse sibling = tmp2; 3431da177e4SLinus Torvalds } 3446280d235SMichel Lespinasse /* 3456280d235SMichel Lespinasse * Case 4 - left rotate at parent + color flips 3466280d235SMichel Lespinasse * (p and sl could be either color here. 3476280d235SMichel Lespinasse * After rotation, p becomes black, s acquires 3486280d235SMichel Lespinasse * p's color, and sl keeps its color) 3496280d235SMichel Lespinasse * 3506280d235SMichel Lespinasse * (p) (s) 3516280d235SMichel Lespinasse * / \ / \ 3526280d235SMichel Lespinasse * N S --> P Sr 3536280d235SMichel Lespinasse * / \ / \ 3546280d235SMichel Lespinasse * (sl) sr N (sl) 3556280d235SMichel Lespinasse */ 356d72da4a4SPeter Zijlstra tmp2 = sibling->rb_left; 357d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_right, tmp2); 358d72da4a4SPeter Zijlstra WRITE_ONCE(sibling->rb_left, parent); 3596280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, RB_BLACK); 3606280d235SMichel Lespinasse if (tmp2) 3616280d235SMichel Lespinasse rb_set_parent(tmp2, parent); 3626280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 3636280d235SMichel Lespinasse RB_BLACK); 3649c079addSMichel Lespinasse augment_rotate(parent, sibling); 3651da177e4SLinus Torvalds break; 366d6ff1273SMichel Lespinasse } else { 3676280d235SMichel Lespinasse sibling = parent->rb_left; 3686280d235SMichel Lespinasse if (rb_is_red(sibling)) { 3696280d235SMichel Lespinasse /* Case 1 - right rotate at parent */ 370d72da4a4SPeter Zijlstra tmp1 = sibling->rb_right; 371d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_left, tmp1); 372d72da4a4SPeter Zijlstra WRITE_ONCE(sibling->rb_right, parent); 3736280d235SMichel Lespinasse rb_set_parent_color(tmp1, parent, RB_BLACK); 3746280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 3756280d235SMichel Lespinasse RB_RED); 3769c079addSMichel Lespinasse augment_rotate(parent, sibling); 3776280d235SMichel Lespinasse sibling = tmp1; 3781da177e4SLinus Torvalds } 3796280d235SMichel Lespinasse tmp1 = sibling->rb_left; 3806280d235SMichel Lespinasse if (!tmp1 || rb_is_black(tmp1)) { 3816280d235SMichel Lespinasse tmp2 = sibling->rb_right; 3826280d235SMichel Lespinasse if (!tmp2 || rb_is_black(tmp2)) { 3836280d235SMichel Lespinasse /* Case 2 - sibling color flip */ 3846280d235SMichel Lespinasse rb_set_parent_color(sibling, parent, 3856280d235SMichel Lespinasse RB_RED); 38646b6135aSMichel Lespinasse if (rb_is_red(parent)) 38746b6135aSMichel Lespinasse rb_set_black(parent); 38846b6135aSMichel Lespinasse else { 3891da177e4SLinus Torvalds node = parent; 39055a98102SDavid Woodhouse parent = rb_parent(node); 39146b6135aSMichel Lespinasse if (parent) 392e125d147SMichel Lespinasse continue; 3931da177e4SLinus Torvalds } 39446b6135aSMichel Lespinasse break; 39546b6135aSMichel Lespinasse } 396ce093a04SJie Chen /* Case 3 - left rotate at sibling */ 397d72da4a4SPeter Zijlstra tmp1 = tmp2->rb_left; 398d72da4a4SPeter Zijlstra WRITE_ONCE(sibling->rb_right, tmp1); 399d72da4a4SPeter Zijlstra WRITE_ONCE(tmp2->rb_left, sibling); 400d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_left, tmp2); 4016280d235SMichel Lespinasse if (tmp1) 4026280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, 4036280d235SMichel Lespinasse RB_BLACK); 4049c079addSMichel Lespinasse augment_rotate(sibling, tmp2); 4056280d235SMichel Lespinasse tmp1 = sibling; 4066280d235SMichel Lespinasse sibling = tmp2; 4071da177e4SLinus Torvalds } 408ce093a04SJie Chen /* Case 4 - right rotate at parent + color flips */ 409d72da4a4SPeter Zijlstra tmp2 = sibling->rb_right; 410d72da4a4SPeter Zijlstra WRITE_ONCE(parent->rb_left, tmp2); 411d72da4a4SPeter Zijlstra WRITE_ONCE(sibling->rb_right, parent); 4126280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, RB_BLACK); 4136280d235SMichel Lespinasse if (tmp2) 4146280d235SMichel Lespinasse rb_set_parent(tmp2, parent); 4156280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 4166280d235SMichel Lespinasse RB_BLACK); 4179c079addSMichel Lespinasse augment_rotate(parent, sibling); 4181da177e4SLinus Torvalds break; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds } 4223cb7a563SMichel Lespinasse 4233cb7a563SMichel Lespinasse /* Non-inline version for rb_erase_augmented() use */ 4243cb7a563SMichel Lespinasse void __rb_erase_color(struct rb_node *parent, struct rb_root *root, 4253cb7a563SMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 4263cb7a563SMichel Lespinasse { 4273cb7a563SMichel Lespinasse ____rb_erase_color(parent, root, augment_rotate); 4283cb7a563SMichel Lespinasse } 4299c079addSMichel Lespinasse EXPORT_SYMBOL(__rb_erase_color); 43014b94af0SMichel Lespinasse 43114b94af0SMichel Lespinasse /* 43214b94af0SMichel Lespinasse * Non-augmented rbtree manipulation functions. 43314b94af0SMichel Lespinasse * 43414b94af0SMichel Lespinasse * We use dummy augmented callbacks here, and have the compiler optimize them 43514b94af0SMichel Lespinasse * out of the rb_insert_color() and rb_erase() function definitions. 43614b94af0SMichel Lespinasse */ 43714b94af0SMichel Lespinasse 43814b94af0SMichel Lespinasse static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {} 43914b94af0SMichel Lespinasse static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {} 44014b94af0SMichel Lespinasse static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {} 44114b94af0SMichel Lespinasse 44214b94af0SMichel Lespinasse static const struct rb_augment_callbacks dummy_callbacks = { 443f231aebfSKees Cook .propagate = dummy_propagate, 444f231aebfSKees Cook .copy = dummy_copy, 445f231aebfSKees Cook .rotate = dummy_rotate 44614b94af0SMichel Lespinasse }; 44714b94af0SMichel Lespinasse 44814b94af0SMichel Lespinasse void rb_insert_color(struct rb_node *node, struct rb_root *root) 44914b94af0SMichel Lespinasse { 450cd9e61edSDavidlohr Bueso __rb_insert(node, root, false, NULL, dummy_rotate); 45114b94af0SMichel Lespinasse } 45214b94af0SMichel Lespinasse EXPORT_SYMBOL(rb_insert_color); 45314b94af0SMichel Lespinasse 45414b94af0SMichel Lespinasse void rb_erase(struct rb_node *node, struct rb_root *root) 45514b94af0SMichel Lespinasse { 4563cb7a563SMichel Lespinasse struct rb_node *rebalance; 457cd9e61edSDavidlohr Bueso rebalance = __rb_erase_augmented(node, root, 458cd9e61edSDavidlohr Bueso NULL, &dummy_callbacks); 4593cb7a563SMichel Lespinasse if (rebalance) 4603cb7a563SMichel Lespinasse ____rb_erase_color(rebalance, root, dummy_rotate); 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds EXPORT_SYMBOL(rb_erase); 4631da177e4SLinus Torvalds 464cd9e61edSDavidlohr Bueso void rb_insert_color_cached(struct rb_node *node, 465cd9e61edSDavidlohr Bueso struct rb_root_cached *root, bool leftmost) 466cd9e61edSDavidlohr Bueso { 467cd9e61edSDavidlohr Bueso __rb_insert(node, &root->rb_root, leftmost, 468cd9e61edSDavidlohr Bueso &root->rb_leftmost, dummy_rotate); 469cd9e61edSDavidlohr Bueso } 470cd9e61edSDavidlohr Bueso EXPORT_SYMBOL(rb_insert_color_cached); 471cd9e61edSDavidlohr Bueso 472cd9e61edSDavidlohr Bueso void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root) 473cd9e61edSDavidlohr Bueso { 474cd9e61edSDavidlohr Bueso struct rb_node *rebalance; 475cd9e61edSDavidlohr Bueso rebalance = __rb_erase_augmented(node, &root->rb_root, 476cd9e61edSDavidlohr Bueso &root->rb_leftmost, &dummy_callbacks); 477cd9e61edSDavidlohr Bueso if (rebalance) 478cd9e61edSDavidlohr Bueso ____rb_erase_color(rebalance, &root->rb_root, dummy_rotate); 479cd9e61edSDavidlohr Bueso } 480cd9e61edSDavidlohr Bueso EXPORT_SYMBOL(rb_erase_cached); 481cd9e61edSDavidlohr Bueso 48214b94af0SMichel Lespinasse /* 48314b94af0SMichel Lespinasse * Augmented rbtree manipulation functions. 48414b94af0SMichel Lespinasse * 48514b94af0SMichel Lespinasse * This instantiates the same __always_inline functions as in the non-augmented 48614b94af0SMichel Lespinasse * case, but this time with user-defined callbacks. 48714b94af0SMichel Lespinasse */ 48814b94af0SMichel Lespinasse 48914b94af0SMichel Lespinasse void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, 490cd9e61edSDavidlohr Bueso bool newleft, struct rb_node **leftmost, 49114b94af0SMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 49214b94af0SMichel Lespinasse { 493cd9e61edSDavidlohr Bueso __rb_insert(node, root, newleft, leftmost, augment_rotate); 49414b94af0SMichel Lespinasse } 49514b94af0SMichel Lespinasse EXPORT_SYMBOL(__rb_insert_augmented); 49614b94af0SMichel Lespinasse 4971da177e4SLinus Torvalds /* 4981da177e4SLinus Torvalds * This function returns the first node (in sort order) of the tree. 4991da177e4SLinus Torvalds */ 500f4b477c4SArtem Bityutskiy struct rb_node *rb_first(const struct rb_root *root) 5011da177e4SLinus Torvalds { 5021da177e4SLinus Torvalds struct rb_node *n; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds n = root->rb_node; 5051da177e4SLinus Torvalds if (!n) 5061da177e4SLinus Torvalds return NULL; 5071da177e4SLinus Torvalds while (n->rb_left) 5081da177e4SLinus Torvalds n = n->rb_left; 5091da177e4SLinus Torvalds return n; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds EXPORT_SYMBOL(rb_first); 5121da177e4SLinus Torvalds 513f4b477c4SArtem Bityutskiy struct rb_node *rb_last(const struct rb_root *root) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds struct rb_node *n; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds n = root->rb_node; 5181da177e4SLinus Torvalds if (!n) 5191da177e4SLinus Torvalds return NULL; 5201da177e4SLinus Torvalds while (n->rb_right) 5211da177e4SLinus Torvalds n = n->rb_right; 5221da177e4SLinus Torvalds return n; 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds EXPORT_SYMBOL(rb_last); 5251da177e4SLinus Torvalds 526f4b477c4SArtem Bityutskiy struct rb_node *rb_next(const struct rb_node *node) 5271da177e4SLinus Torvalds { 52855a98102SDavid Woodhouse struct rb_node *parent; 52955a98102SDavid Woodhouse 5304c199a93SMichel Lespinasse if (RB_EMPTY_NODE(node)) 53110fd48f2SJens Axboe return NULL; 53210fd48f2SJens Axboe 5337ce6ff9eSMichel Lespinasse /* 5347ce6ff9eSMichel Lespinasse * If we have a right-hand child, go down and then left as far 5357ce6ff9eSMichel Lespinasse * as we can. 5367ce6ff9eSMichel Lespinasse */ 5371da177e4SLinus Torvalds if (node->rb_right) { 5381da177e4SLinus Torvalds node = node->rb_right; 5391da177e4SLinus Torvalds while (node->rb_left) 5401da177e4SLinus Torvalds node=node->rb_left; 541f4b477c4SArtem Bityutskiy return (struct rb_node *)node; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 5447ce6ff9eSMichel Lespinasse /* 5457ce6ff9eSMichel Lespinasse * No right-hand children. Everything down and left is smaller than us, 5467ce6ff9eSMichel Lespinasse * so any 'next' node must be in the general direction of our parent. 5477ce6ff9eSMichel Lespinasse * Go up the tree; any time the ancestor is a right-hand child of its 5487ce6ff9eSMichel Lespinasse * parent, keep going up. First time it's a left-hand child of its 5497ce6ff9eSMichel Lespinasse * parent, said parent is our 'next' node. 5507ce6ff9eSMichel Lespinasse */ 55155a98102SDavid Woodhouse while ((parent = rb_parent(node)) && node == parent->rb_right) 55255a98102SDavid Woodhouse node = parent; 5531da177e4SLinus Torvalds 55455a98102SDavid Woodhouse return parent; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds EXPORT_SYMBOL(rb_next); 5571da177e4SLinus Torvalds 558f4b477c4SArtem Bityutskiy struct rb_node *rb_prev(const struct rb_node *node) 5591da177e4SLinus Torvalds { 56055a98102SDavid Woodhouse struct rb_node *parent; 56155a98102SDavid Woodhouse 5624c199a93SMichel Lespinasse if (RB_EMPTY_NODE(node)) 56310fd48f2SJens Axboe return NULL; 56410fd48f2SJens Axboe 5657ce6ff9eSMichel Lespinasse /* 5667ce6ff9eSMichel Lespinasse * If we have a left-hand child, go down and then right as far 5677ce6ff9eSMichel Lespinasse * as we can. 5687ce6ff9eSMichel Lespinasse */ 5691da177e4SLinus Torvalds if (node->rb_left) { 5701da177e4SLinus Torvalds node = node->rb_left; 5711da177e4SLinus Torvalds while (node->rb_right) 5721da177e4SLinus Torvalds node=node->rb_right; 573f4b477c4SArtem Bityutskiy return (struct rb_node *)node; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5767ce6ff9eSMichel Lespinasse /* 5777ce6ff9eSMichel Lespinasse * No left-hand children. Go up till we find an ancestor which 5787ce6ff9eSMichel Lespinasse * is a right-hand child of its parent. 5797ce6ff9eSMichel Lespinasse */ 58055a98102SDavid Woodhouse while ((parent = rb_parent(node)) && node == parent->rb_left) 58155a98102SDavid Woodhouse node = parent; 5821da177e4SLinus Torvalds 58355a98102SDavid Woodhouse return parent; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds EXPORT_SYMBOL(rb_prev); 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds void rb_replace_node(struct rb_node *victim, struct rb_node *new, 5881da177e4SLinus Torvalds struct rb_root *root) 5891da177e4SLinus Torvalds { 59055a98102SDavid Woodhouse struct rb_node *parent = rb_parent(victim); 5911da177e4SLinus Torvalds 592c1adf200SDavid Howells /* Copy the pointers/colour from the victim to the replacement */ 593c1adf200SDavid Howells *new = *victim; 594c1adf200SDavid Howells 5951da177e4SLinus Torvalds /* Set the surrounding nodes to point to the replacement */ 596c1adf200SDavid Howells if (victim->rb_left) 597c1adf200SDavid Howells rb_set_parent(victim->rb_left, new); 598c1adf200SDavid Howells if (victim->rb_right) 599c1adf200SDavid Howells rb_set_parent(victim->rb_right, new); 6007abc704aSMichel Lespinasse __rb_change_child(victim, new, parent, root); 601c1adf200SDavid Howells } 602c1adf200SDavid Howells EXPORT_SYMBOL(rb_replace_node); 603c1adf200SDavid Howells 604c1adf200SDavid Howells void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new, 605c1adf200SDavid Howells struct rb_root *root) 606c1adf200SDavid Howells { 607c1adf200SDavid Howells struct rb_node *parent = rb_parent(victim); 608c1adf200SDavid Howells 609c1adf200SDavid Howells /* Copy the pointers/colour from the victim to the replacement */ 610c1adf200SDavid Howells *new = *victim; 611c1adf200SDavid Howells 612c1adf200SDavid Howells /* Set the surrounding nodes to point to the replacement */ 6131da177e4SLinus Torvalds if (victim->rb_left) 61455a98102SDavid Woodhouse rb_set_parent(victim->rb_left, new); 6151da177e4SLinus Torvalds if (victim->rb_right) 61655a98102SDavid Woodhouse rb_set_parent(victim->rb_right, new); 6171da177e4SLinus Torvalds 618c1adf200SDavid Howells /* Set the parent's pointer to the new node last after an RCU barrier 619c1adf200SDavid Howells * so that the pointers onwards are seen to be set correctly when doing 620c1adf200SDavid Howells * an RCU walk over the tree. 621c1adf200SDavid Howells */ 622c1adf200SDavid Howells __rb_change_child_rcu(victim, new, parent, root); 6231da177e4SLinus Torvalds } 624c1adf200SDavid Howells EXPORT_SYMBOL(rb_replace_node_rcu); 6259dee5c51SCody P Schafer 6269dee5c51SCody P Schafer static struct rb_node *rb_left_deepest_node(const struct rb_node *node) 6279dee5c51SCody P Schafer { 6289dee5c51SCody P Schafer for (;;) { 6299dee5c51SCody P Schafer if (node->rb_left) 6309dee5c51SCody P Schafer node = node->rb_left; 6319dee5c51SCody P Schafer else if (node->rb_right) 6329dee5c51SCody P Schafer node = node->rb_right; 6339dee5c51SCody P Schafer else 6349dee5c51SCody P Schafer return (struct rb_node *)node; 6359dee5c51SCody P Schafer } 6369dee5c51SCody P Schafer } 6379dee5c51SCody P Schafer 6389dee5c51SCody P Schafer struct rb_node *rb_next_postorder(const struct rb_node *node) 6399dee5c51SCody P Schafer { 6409dee5c51SCody P Schafer const struct rb_node *parent; 6419dee5c51SCody P Schafer if (!node) 6429dee5c51SCody P Schafer return NULL; 6439dee5c51SCody P Schafer parent = rb_parent(node); 6449dee5c51SCody P Schafer 6459dee5c51SCody P Schafer /* If we're sitting on node, we've already seen our children */ 6469dee5c51SCody P Schafer if (parent && node == parent->rb_left && parent->rb_right) { 6479dee5c51SCody P Schafer /* If we are the parent's left node, go to the parent's right 6489dee5c51SCody P Schafer * node then all the way down to the left */ 6499dee5c51SCody P Schafer return rb_left_deepest_node(parent->rb_right); 6509dee5c51SCody P Schafer } else 6519dee5c51SCody P Schafer /* Otherwise we are the parent's right node, and the parent 6529dee5c51SCody P Schafer * should be next */ 6539dee5c51SCody P Schafer return (struct rb_node *)parent; 6549dee5c51SCody P Schafer } 6559dee5c51SCody P Schafer EXPORT_SYMBOL(rb_next_postorder); 6569dee5c51SCody P Schafer 6579dee5c51SCody P Schafer struct rb_node *rb_first_postorder(const struct rb_root *root) 6589dee5c51SCody P Schafer { 6599dee5c51SCody P Schafer if (!root->rb_node) 6609dee5c51SCody P Schafer return NULL; 6619dee5c51SCody P Schafer 6629dee5c51SCody P Schafer return rb_left_deepest_node(root->rb_node); 6639dee5c51SCody P Schafer } 6649dee5c51SCody P Schafer EXPORT_SYMBOL(rb_first_postorder); 665