VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
1a59d1b8e05ea (Thomas Gleixner     2019-05-27 08:55:05 +0200   1) // SPDX-License-Identifier: GPL-2.0-or-later
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   2) /*
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   3)   Red Black Trees
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   4)   (C) 1999  Andrea Arcangeli <andrea@suse.de>
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   5)   (C) 2002  David Woodhouse <dwmw2@infradead.org>
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700   6)   (C) 2012  Michel Lespinasse <walken@google.com>
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700   7) 
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   8) 
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700   9)   linux/lib/rbtree.c
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700  10) */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700  11) 
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700  12) #include <linux/rbtree_augmented.h>
8bc3bcc93a2b4 (Paul Gortmaker      2011-11-16 21:29:17 -0500  13) #include <linux/export.h>
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700  14) 
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  15) /*
d89775fc929c5 (Alexander A. Klimov 2020-08-11 18:34:50 -0700  16)  * red-black trees properties:  https://en.wikipedia.org/wiki/Rbtree
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  17)  *
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  18)  *  1) A node is either red or black
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  19)  *  2) The root is black
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  20)  *  3) All leaves (NULL) are black
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  21)  *  4) Both children of every red node are black
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  22)  *  5) Every simple path from root to leaves contains the same number
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  23)  *     of black nodes.
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  24)  *
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  25)  *  4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  26)  *  consecutive red nodes in a path and every red node is therefore followed by
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  27)  *  a black. So if B is the number of black nodes on every simple path (as per
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  28)  *  5), then the longest possible path due to 4 is 2B.
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  29)  *
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  30)  *  We shall indicate color with case, where black nodes are uppercase and red
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700  31)  *  nodes will be lowercase. Unknown color nodes shall be drawn as red within
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700  32)  *  parentheses and have some accompanying text comment.
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  33)  */
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  34) 
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  35) /*
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  36)  * Notes on lockless lookups:
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  37)  *
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  38)  * All stores to the tree structure (rb_left and rb_right) must be done using
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  39)  * WRITE_ONCE(). And we must not inadvertently cause (temporary) loops in the
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  40)  * tree structure as seen in program order.
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  41)  *
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  42)  * These two requirements will allow lockless iteration of the tree -- not
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  43)  * correct iteration mind you, tree rotations are not atomic so a lookup might
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  44)  * miss entire subtrees.
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  45)  *
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  46)  * But they do guarantee that any such traversal will only see valid elements
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  47)  * and that it will indeed complete -- does not get stuck in a loop.
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  48)  *
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  49)  * It also guarantees that if the lookup returns an element it is the 'correct'
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  50)  * one. But not returning an element does _NOT_ mean it's not present.
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  51)  *
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  52)  * NOTE:
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  53)  *
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  54)  * Stores to __rb_parent_color are not important for simple lookups so those
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  55)  * are left undone as of now. Nor did I check for loops involving parent
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  56)  * pointers.
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  57)  */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930  58) 
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700  59) static inline void rb_set_black(struct rb_node *rb)
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700  60) {
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700  61) 	rb->__rb_parent_color |= RB_BLACK;
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700  62) }
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700  63) 
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  64) static inline struct rb_node *rb_red_parent(struct rb_node *red)
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  65) {
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  66) 	return (struct rb_node *)red->__rb_parent_color;
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  67) }
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  68) 
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  69) /*
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  70)  * Helper function for rotations:
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  71)  * - old's parent and color get assigned to new
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  72)  * - old gets assigned new as a parent and 'color' as a color.
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  73)  */
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  74) static inline void
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  75) __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  76) 			struct rb_root *root, int color)
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  77) {
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  78) 	struct rb_node *parent = rb_parent(old);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  79) 	new->__rb_parent_color = old->__rb_parent_color;
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  80) 	rb_set_parent_color(old, new, color);
7abc704ae399f (Michel Lespinasse   2012-10-08 16:31:07 -0700  81) 	__rb_change_child(old, new, parent, root);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  82) }
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  83) 
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700  84) static __always_inline void
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700  85) __rb_insert(struct rb_node *node, struct rb_root *root,
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700  86) 	    void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700  87) {
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700  88) 	struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700  89) 
6d58452dc066d (Michel Lespinasse   2012-10-08 16:30:44 -0700  90) 	while (true) {
6d58452dc066d (Michel Lespinasse   2012-10-08 16:30:44 -0700  91) 		/*
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700  92) 		 * Loop invariant: node is red.
6d58452dc066d (Michel Lespinasse   2012-10-08 16:30:44 -0700  93) 		 */
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700  94) 		if (unlikely(!parent)) {
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700  95) 			/*
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700  96) 			 * The inserted node is root. Either this is the
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700  97) 			 * first node, or we recursed at Case 1 below and
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700  98) 			 * are no longer violating 4).
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700  99) 			 */
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 100) 			rb_set_parent_color(node, NULL, RB_BLACK);
6d58452dc066d (Michel Lespinasse   2012-10-08 16:30:44 -0700 101) 			break;
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 102) 		}
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 103) 
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 104) 		/*
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 105) 		 * If there is a black parent, we are done.
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 106) 		 * Otherwise, take some corrective action as,
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 107) 		 * per 4), we don't want a red root or two
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 108) 		 * consecutive red nodes.
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 109) 		 */
2aadf7fc7df9e (Davidlohr Bueso     2017-09-08 16:14:39 -0700 110) 		if(rb_is_black(parent))
6d58452dc066d (Michel Lespinasse   2012-10-08 16:30:44 -0700 111) 			break;
6d58452dc066d (Michel Lespinasse   2012-10-08 16:30:44 -0700 112) 
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 113) 		gparent = rb_red_parent(parent);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 114) 
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 115) 		tmp = gparent->rb_right;
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 116) 		if (parent != tmp) {	/* parent == gparent->rb_left */
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 117) 			if (tmp && rb_is_red(tmp)) {
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 118) 				/*
35dc67d7d922b (Davidlohr Bueso     2017-09-08 16:14:42 -0700 119) 				 * Case 1 - node's uncle is red (color flips).
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 120) 				 *
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 121) 				 *       G            g
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 122) 				 *      / \          / \
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 123) 				 *     p   u  -->   P   U
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 124) 				 *    /            /
1b9c53e849aa6 (Wei Yang            2014-08-08 14:22:14 -0700 125) 				 *   n            n
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 126) 				 *
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 127) 				 * However, since g's parent might be red, and
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 128) 				 * 4) does not allow this, we need to recurse
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 129) 				 * at g.
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 130) 				 */
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 131) 				rb_set_parent_color(tmp, gparent, RB_BLACK);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 132) 				rb_set_parent_color(parent, gparent, RB_BLACK);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 133) 				node = gparent;
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 134) 				parent = rb_parent(node);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 135) 				rb_set_parent_color(node, parent, RB_RED);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 136) 				continue;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 137) 			}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 138) 
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 139) 			tmp = parent->rb_right;
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 140) 			if (node == tmp) {
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 141) 				/*
35dc67d7d922b (Davidlohr Bueso     2017-09-08 16:14:42 -0700 142) 				 * Case 2 - node's uncle is black and node is
35dc67d7d922b (Davidlohr Bueso     2017-09-08 16:14:42 -0700 143) 				 * the parent's right child (left rotate at parent).
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 144) 				 *
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 145) 				 *      G             G
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 146) 				 *     / \           / \
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 147) 				 *    p   U  -->    n   U
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 148) 				 *     \           /
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 149) 				 *      n         p
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 150) 				 *
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 151) 				 * This still leaves us in violation of 4), the
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 152) 				 * continuation into Case 3 will fix that.
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 153) 				 */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 154) 				tmp = node->rb_left;
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 155) 				WRITE_ONCE(parent->rb_right, tmp);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 156) 				WRITE_ONCE(node->rb_left, parent);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 157) 				if (tmp)
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 158) 					rb_set_parent_color(tmp, parent,
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 159) 							    RB_BLACK);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 160) 				rb_set_parent_color(parent, node, RB_RED);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 161) 				augment_rotate(parent, node);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 162) 				parent = node;
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 163) 				tmp = node->rb_right;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 164) 			}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 165) 
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 166) 			/*
35dc67d7d922b (Davidlohr Bueso     2017-09-08 16:14:42 -0700 167) 			 * Case 3 - node's uncle is black and node is
35dc67d7d922b (Davidlohr Bueso     2017-09-08 16:14:42 -0700 168) 			 * the parent's left child (right rotate at gparent).
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 169) 			 *
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 170) 			 *        G           P
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 171) 			 *       / \         / \
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 172) 			 *      p   U  -->  n   g
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 173) 			 *     /                 \
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 174) 			 *    n                   U
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 175) 			 */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 176) 			WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 177) 			WRITE_ONCE(parent->rb_right, gparent);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 178) 			if (tmp)
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 179) 				rb_set_parent_color(tmp, gparent, RB_BLACK);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 180) 			__rb_rotate_set_parents(gparent, parent, root, RB_RED);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 181) 			augment_rotate(gparent, parent);
1f0528653e41e (Michel Lespinasse   2012-10-08 16:30:42 -0700 182) 			break;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 183) 		} else {
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 184) 			tmp = gparent->rb_left;
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 185) 			if (tmp && rb_is_red(tmp)) {
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 186) 				/* Case 1 - color flips */
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 187) 				rb_set_parent_color(tmp, gparent, RB_BLACK);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 188) 				rb_set_parent_color(parent, gparent, RB_BLACK);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 189) 				node = gparent;
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 190) 				parent = rb_parent(node);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 191) 				rb_set_parent_color(node, parent, RB_RED);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 192) 				continue;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 193) 			}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 194) 
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 195) 			tmp = parent->rb_left;
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 196) 			if (node == tmp) {
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 197) 				/* Case 2 - right rotate at parent */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 198) 				tmp = node->rb_right;
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 199) 				WRITE_ONCE(parent->rb_left, tmp);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 200) 				WRITE_ONCE(node->rb_right, parent);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 201) 				if (tmp)
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 202) 					rb_set_parent_color(tmp, parent,
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 203) 							    RB_BLACK);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 204) 				rb_set_parent_color(parent, node, RB_RED);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 205) 				augment_rotate(parent, node);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 206) 				parent = node;
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 207) 				tmp = node->rb_left;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 208) 			}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 209) 
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 210) 			/* Case 3 - left rotate at gparent */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 211) 			WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 212) 			WRITE_ONCE(parent->rb_left, gparent);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 213) 			if (tmp)
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 214) 				rb_set_parent_color(tmp, gparent, RB_BLACK);
5bc9188aa207d (Michel Lespinasse   2012-10-08 16:30:47 -0700 215) 			__rb_rotate_set_parents(gparent, parent, root, RB_RED);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 216) 			augment_rotate(gparent, parent);
1f0528653e41e (Michel Lespinasse   2012-10-08 16:30:42 -0700 217) 			break;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 218) 		}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 219) 	}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 220) }
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 221) 
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 222) /*
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 223)  * Inline version for rb_erase() use - we want to be able to inline
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 224)  * and eliminate the dummy_rotate callback there
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 225)  */
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 226) static __always_inline void
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 227) ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700 228) 	void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 229) {
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 230) 	struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 231) 
d6ff1273928eb (Michel Lespinasse   2012-10-08 16:30:50 -0700 232) 	while (true) {
d6ff1273928eb (Michel Lespinasse   2012-10-08 16:30:50 -0700 233) 		/*
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 234) 		 * Loop invariants:
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 235) 		 * - node is black (or NULL on first iteration)
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 236) 		 * - node is not the root (parent is not NULL)
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 237) 		 * - All leaf paths going through parent and node have a
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 238) 		 *   black node count that is 1 lower than other leaf paths.
d6ff1273928eb (Michel Lespinasse   2012-10-08 16:30:50 -0700 239) 		 */
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 240) 		sibling = parent->rb_right;
59633abf34e2f (Michel Lespinasse   2012-10-08 16:31:02 -0700 241) 		if (node != sibling) {	/* node == parent->rb_left */
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 242) 			if (rb_is_red(sibling)) {
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 243) 				/*
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 244) 				 * Case 1 - left rotate at parent
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 245) 				 *
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 246) 				 *     P               S
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 247) 				 *    / \             / \
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 248) 				 *   N   s    -->    p   Sr
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 249) 				 *      / \         / \
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 250) 				 *     Sl  Sr      N   Sl
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 251) 				 */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 252) 				tmp1 = sibling->rb_left;
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 253) 				WRITE_ONCE(parent->rb_right, tmp1);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 254) 				WRITE_ONCE(sibling->rb_left, parent);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 255) 				rb_set_parent_color(tmp1, parent, RB_BLACK);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 256) 				__rb_rotate_set_parents(parent, sibling, root,
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 257) 							RB_RED);
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700 258) 				augment_rotate(parent, sibling);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 259) 				sibling = tmp1;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 260) 			}
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 261) 			tmp1 = sibling->rb_right;
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 262) 			if (!tmp1 || rb_is_black(tmp1)) {
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 263) 				tmp2 = sibling->rb_left;
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 264) 				if (!tmp2 || rb_is_black(tmp2)) {
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 265) 					/*
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 266) 					 * Case 2 - sibling color flip
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 267) 					 * (p could be either color here)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 268) 					 *
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 269) 					 *    (p)           (p)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 270) 					 *    / \           / \
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 271) 					 *   N   S    -->  N   s
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 272) 					 *      / \           / \
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 273) 					 *     Sl  Sr        Sl  Sr
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 274) 					 *
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 275) 					 * This leaves us violating 5) which
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 276) 					 * can be fixed by flipping p to black
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 277) 					 * if it was red, or by recursing at p.
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 278) 					 * p is red when coming from Case 1.
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 279) 					 */
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 280) 					rb_set_parent_color(sibling, parent,
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 281) 							    RB_RED);
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 282) 					if (rb_is_red(parent))
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 283) 						rb_set_black(parent);
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 284) 					else {
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 285) 						node = parent;
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 286) 						parent = rb_parent(node);
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 287) 						if (parent)
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 288) 							continue;
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 289) 					}
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 290) 					break;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 291) 				}
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 292) 				/*
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 293) 				 * Case 3 - right rotate at sibling
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 294) 				 * (p could be either color here)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 295) 				 *
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 296) 				 *   (p)           (p)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 297) 				 *   / \           / \
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 298) 				 *  N   S    -->  N   sl
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 299) 				 *     / \             \
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 300) 				 *    sl  Sr            S
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 301) 				 *                       \
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 302) 				 *                        Sr
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 303) 				 *
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 304) 				 * Note: p might be red, and then both
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 305) 				 * p and sl are red after rotation(which
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 306) 				 * breaks property 4). This is fixed in
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 307) 				 * Case 4 (in __rb_rotate_set_parents()
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 308) 				 *         which set sl the color of p
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 309) 				 *         and set p RB_BLACK)
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 310) 				 *
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 311) 				 *   (p)            (sl)
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 312) 				 *   / \            /  \
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 313) 				 *  N   sl   -->   P    S
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 314) 				 *       \        /      \
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 315) 				 *        S      N        Sr
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 316) 				 *         \
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 317) 				 *          Sr
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 318) 				 */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 319) 				tmp1 = tmp2->rb_right;
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 320) 				WRITE_ONCE(sibling->rb_left, tmp1);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 321) 				WRITE_ONCE(tmp2->rb_right, sibling);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 322) 				WRITE_ONCE(parent->rb_right, tmp2);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 323) 				if (tmp1)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 324) 					rb_set_parent_color(tmp1, sibling,
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 325) 							    RB_BLACK);
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700 326) 				augment_rotate(sibling, tmp2);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 327) 				tmp1 = sibling;
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 328) 				sibling = tmp2;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 329) 			}
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 330) 			/*
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 331) 			 * Case 4 - left rotate at parent + color flips
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 332) 			 * (p and sl could be either color here.
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 333) 			 *  After rotation, p becomes black, s acquires
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 334) 			 *  p's color, and sl keeps its color)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 335) 			 *
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 336) 			 *      (p)             (s)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 337) 			 *      / \             / \
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 338) 			 *     N   S     -->   P   Sr
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 339) 			 *        / \         / \
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 340) 			 *      (sl) sr      N  (sl)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 341) 			 */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 342) 			tmp2 = sibling->rb_left;
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 343) 			WRITE_ONCE(parent->rb_right, tmp2);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 344) 			WRITE_ONCE(sibling->rb_left, parent);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 345) 			rb_set_parent_color(tmp1, sibling, RB_BLACK);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 346) 			if (tmp2)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 347) 				rb_set_parent(tmp2, parent);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 348) 			__rb_rotate_set_parents(parent, sibling, root,
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 349) 						RB_BLACK);
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700 350) 			augment_rotate(parent, sibling);
e125d1471a4f8 (Michel Lespinasse   2012-10-08 16:30:54 -0700 351) 			break;
d6ff1273928eb (Michel Lespinasse   2012-10-08 16:30:50 -0700 352) 		} else {
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 353) 			sibling = parent->rb_left;
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 354) 			if (rb_is_red(sibling)) {
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 355) 				/* Case 1 - right rotate at parent */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 356) 				tmp1 = sibling->rb_right;
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 357) 				WRITE_ONCE(parent->rb_left, tmp1);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 358) 				WRITE_ONCE(sibling->rb_right, parent);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 359) 				rb_set_parent_color(tmp1, parent, RB_BLACK);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 360) 				__rb_rotate_set_parents(parent, sibling, root,
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 361) 							RB_RED);
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700 362) 				augment_rotate(parent, sibling);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 363) 				sibling = tmp1;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 364) 			}
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 365) 			tmp1 = sibling->rb_left;
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 366) 			if (!tmp1 || rb_is_black(tmp1)) {
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 367) 				tmp2 = sibling->rb_right;
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 368) 				if (!tmp2 || rb_is_black(tmp2)) {
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 369) 					/* Case 2 - sibling color flip */
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 370) 					rb_set_parent_color(sibling, parent,
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 371) 							    RB_RED);
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 372) 					if (rb_is_red(parent))
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 373) 						rb_set_black(parent);
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 374) 					else {
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 375) 						node = parent;
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 376) 						parent = rb_parent(node);
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 377) 						if (parent)
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 378) 							continue;
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 379) 					}
46b6135a7402a (Michel Lespinasse   2012-10-08 16:31:11 -0700 380) 					break;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 381) 				}
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 382) 				/* Case 3 - left rotate at sibling */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 383) 				tmp1 = tmp2->rb_left;
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 384) 				WRITE_ONCE(sibling->rb_right, tmp1);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 385) 				WRITE_ONCE(tmp2->rb_left, sibling);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 386) 				WRITE_ONCE(parent->rb_left, tmp2);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 387) 				if (tmp1)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 388) 					rb_set_parent_color(tmp1, sibling,
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 389) 							    RB_BLACK);
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700 390) 				augment_rotate(sibling, tmp2);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 391) 				tmp1 = sibling;
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 392) 				sibling = tmp2;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 393) 			}
ce093a04543c4 (Jie Chen            2016-12-12 16:46:17 -0800 394) 			/* Case 4 - right rotate at parent + color flips */
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 395) 			tmp2 = sibling->rb_right;
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 396) 			WRITE_ONCE(parent->rb_left, tmp2);
d72da4a4d973d (Peter Zijlstra      2015-05-27 11:09:36 +0930 397) 			WRITE_ONCE(sibling->rb_right, parent);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 398) 			rb_set_parent_color(tmp1, sibling, RB_BLACK);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 399) 			if (tmp2)
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 400) 				rb_set_parent(tmp2, parent);
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 401) 			__rb_rotate_set_parents(parent, sibling, root,
6280d2356fd8a (Michel Lespinasse   2012-10-08 16:30:57 -0700 402) 						RB_BLACK);
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700 403) 			augment_rotate(parent, sibling);
e125d1471a4f8 (Michel Lespinasse   2012-10-08 16:30:54 -0700 404) 			break;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 405) 		}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 406) 	}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 407) }
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 408) 
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 409) /* Non-inline version for rb_erase_augmented() use */
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 410) void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 411) 	void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 412) {
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 413) 	____rb_erase_color(parent, root, augment_rotate);
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 414) }
9c079add0d0f4 (Michel Lespinasse   2012-10-08 16:31:33 -0700 415) EXPORT_SYMBOL(__rb_erase_color);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 416) 
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 417) /*
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 418)  * Non-augmented rbtree manipulation functions.
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 419)  *
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 420)  * We use dummy augmented callbacks here, and have the compiler optimize them
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 421)  * out of the rb_insert_color() and rb_erase() function definitions.
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 422)  */
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 423) 
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 424) static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 425) static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 426) static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 427) 
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 428) static const struct rb_augment_callbacks dummy_callbacks = {
f231aebfc4cae (Kees Cook           2017-02-24 15:01:04 -0800 429) 	.propagate = dummy_propagate,
f231aebfc4cae (Kees Cook           2017-02-24 15:01:04 -0800 430) 	.copy = dummy_copy,
f231aebfc4cae (Kees Cook           2017-02-24 15:01:04 -0800 431) 	.rotate = dummy_rotate
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 432) };
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 433) 
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 434) void rb_insert_color(struct rb_node *node, struct rb_root *root)
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 435) {
9f973cb38088e (Michel Lespinasse   2019-07-16 16:27:45 -0700 436) 	__rb_insert(node, root, dummy_rotate);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 437) }
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 438) EXPORT_SYMBOL(rb_insert_color);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 439) 
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 440) void rb_erase(struct rb_node *node, struct rb_root *root)
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 441) {
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 442) 	struct rb_node *rebalance;
9f973cb38088e (Michel Lespinasse   2019-07-16 16:27:45 -0700 443) 	rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 444) 	if (rebalance)
3cb7a56344ca4 (Michel Lespinasse   2013-01-11 14:32:20 -0800 445) 		____rb_erase_color(rebalance, root, dummy_rotate);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 446) }
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 447) EXPORT_SYMBOL(rb_erase);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 448) 
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 449) /*
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 450)  * Augmented rbtree manipulation functions.
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 451)  *
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 452)  * This instantiates the same __always_inline functions as in the non-augmented
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 453)  * case, but this time with user-defined callbacks.
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 454)  */
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 455) 
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 456) void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 457) 	void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 458) {
9f973cb38088e (Michel Lespinasse   2019-07-16 16:27:45 -0700 459) 	__rb_insert(node, root, augment_rotate);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 460) }
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 461) EXPORT_SYMBOL(__rb_insert_augmented);
14b94af0b251a (Michel Lespinasse   2012-10-08 16:31:17 -0700 462) 
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 463) /*
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 464)  * This function returns the first node (in sort order) of the tree.
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 465)  */
f4b477c473323 (Artem Bityutskiy    2009-01-10 11:12:09 +0000 466) struct rb_node *rb_first(const struct rb_root *root)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 467) {
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 468) 	struct rb_node	*n;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 469) 
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 470) 	n = root->rb_node;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 471) 	if (!n)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 472) 		return NULL;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 473) 	while (n->rb_left)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 474) 		n = n->rb_left;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 475) 	return n;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 476) }
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 477) EXPORT_SYMBOL(rb_first);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 478) 
f4b477c473323 (Artem Bityutskiy    2009-01-10 11:12:09 +0000 479) struct rb_node *rb_last(const struct rb_root *root)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 480) {
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 481) 	struct rb_node	*n;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 482) 
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 483) 	n = root->rb_node;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 484) 	if (!n)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 485) 		return NULL;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 486) 	while (n->rb_right)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 487) 		n = n->rb_right;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 488) 	return n;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 489) }
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 490) EXPORT_SYMBOL(rb_last);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 491) 
f4b477c473323 (Artem Bityutskiy    2009-01-10 11:12:09 +0000 492) struct rb_node *rb_next(const struct rb_node *node)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 493) {
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 494) 	struct rb_node *parent;
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 495) 
4c199a93a2d36 (Michel Lespinasse   2012-10-08 16:30:32 -0700 496) 	if (RB_EMPTY_NODE(node))
10fd48f2376db (Jens Axboe          2006-07-11 21:15:52 +0200 497) 		return NULL;
10fd48f2376db (Jens Axboe          2006-07-11 21:15:52 +0200 498) 
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 499) 	/*
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 500) 	 * If we have a right-hand child, go down and then left as far
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 501) 	 * as we can.
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 502) 	 */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 503) 	if (node->rb_right) {
cd9e61ed1eebb (Davidlohr Bueso     2017-09-08 16:14:36 -0700 504) 		node = node->rb_right;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 505) 		while (node->rb_left)
8d994cada27c5 (chenqiwu            2020-04-06 20:10:31 -0700 506) 			node = node->rb_left;
f4b477c473323 (Artem Bityutskiy    2009-01-10 11:12:09 +0000 507) 		return (struct rb_node *)node;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 508) 	}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 509) 
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 510) 	/*
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 511) 	 * No right-hand children. Everything down and left is smaller than us,
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 512) 	 * so any 'next' node must be in the general direction of our parent.
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 513) 	 * Go up the tree; any time the ancestor is a right-hand child of its
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 514) 	 * parent, keep going up. First time it's a left-hand child of its
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 515) 	 * parent, said parent is our 'next' node.
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 516) 	 */
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 517) 	while ((parent = rb_parent(node)) && node == parent->rb_right)
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 518) 		node = parent;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 519) 
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 520) 	return parent;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 521) }
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 522) EXPORT_SYMBOL(rb_next);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 523) 
f4b477c473323 (Artem Bityutskiy    2009-01-10 11:12:09 +0000 524) struct rb_node *rb_prev(const struct rb_node *node)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 525) {
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 526) 	struct rb_node *parent;
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 527) 
4c199a93a2d36 (Michel Lespinasse   2012-10-08 16:30:32 -0700 528) 	if (RB_EMPTY_NODE(node))
10fd48f2376db (Jens Axboe          2006-07-11 21:15:52 +0200 529) 		return NULL;
10fd48f2376db (Jens Axboe          2006-07-11 21:15:52 +0200 530) 
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 531) 	/*
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 532) 	 * If we have a left-hand child, go down and then right as far
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 533) 	 * as we can.
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 534) 	 */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 535) 	if (node->rb_left) {
cd9e61ed1eebb (Davidlohr Bueso     2017-09-08 16:14:36 -0700 536) 		node = node->rb_left;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 537) 		while (node->rb_right)
8d994cada27c5 (chenqiwu            2020-04-06 20:10:31 -0700 538) 			node = node->rb_right;
f4b477c473323 (Artem Bityutskiy    2009-01-10 11:12:09 +0000 539) 		return (struct rb_node *)node;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 540) 	}
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 541) 
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 542) 	/*
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 543) 	 * No left-hand children. Go up till we find an ancestor which
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 544) 	 * is a right-hand child of its parent.
7ce6ff9e5de99 (Michel Lespinasse   2012-10-08 16:31:01 -0700 545) 	 */
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 546) 	while ((parent = rb_parent(node)) && node == parent->rb_left)
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 547) 		node = parent;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 548) 
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 549) 	return parent;
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 550) }
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 551) EXPORT_SYMBOL(rb_prev);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 552) 
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 553) void rb_replace_node(struct rb_node *victim, struct rb_node *new,
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 554) 		     struct rb_root *root)
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 555) {
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 556) 	struct rb_node *parent = rb_parent(victim);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 557) 
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 558) 	/* Copy the pointers/colour from the victim to the replacement */
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 559) 	*new = *victim;
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 560) 
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 561) 	/* Set the surrounding nodes to point to the replacement */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 562) 	if (victim->rb_left)
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 563) 		rb_set_parent(victim->rb_left, new);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 564) 	if (victim->rb_right)
55a981027fc39 (David Woodhouse     2006-04-21 13:35:51 +0100 565) 		rb_set_parent(victim->rb_right, new);
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 566) 	__rb_change_child(victim, new, parent, root);
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 567) }
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 568) EXPORT_SYMBOL(rb_replace_node);
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 569) 
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 570) void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 571) 			 struct rb_root *root)
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 572) {
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 573) 	struct rb_node *parent = rb_parent(victim);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 574) 
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 575) 	/* Copy the pointers/colour from the victim to the replacement */
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 576) 	*new = *victim;
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 577) 
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 578) 	/* Set the surrounding nodes to point to the replacement */
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 579) 	if (victim->rb_left)
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 580) 		rb_set_parent(victim->rb_left, new);
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 581) 	if (victim->rb_right)
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 582) 		rb_set_parent(victim->rb_right, new);
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 583) 
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 584) 	/* Set the parent's pointer to the new node last after an RCU barrier
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 585) 	 * so that the pointers onwards are seen to be set correctly when doing
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 586) 	 * an RCU walk over the tree.
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 587) 	 */
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 588) 	__rb_change_child_rcu(victim, new, parent, root);
^1da177e4c3f4 (Linus Torvalds      2005-04-16 15:20:36 -0700 589) }
c1adf20052d80 (David Howells       2016-07-01 07:53:51 +0100 590) EXPORT_SYMBOL(rb_replace_node_rcu);
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 591) 
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 592) static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 593) {
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 594) 	for (;;) {
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 595) 		if (node->rb_left)
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 596) 			node = node->rb_left;
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 597) 		else if (node->rb_right)
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 598) 			node = node->rb_right;
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 599) 		else
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 600) 			return (struct rb_node *)node;
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 601) 	}
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 602) }
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 603) 
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 604) struct rb_node *rb_next_postorder(const struct rb_node *node)
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 605) {
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 606) 	const struct rb_node *parent;
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 607) 	if (!node)
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 608) 		return NULL;
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 609) 	parent = rb_parent(node);
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 610) 
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 611) 	/* If we're sitting on node, we've already seen our children */
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 612) 	if (parent && node == parent->rb_left && parent->rb_right) {
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 613) 		/* If we are the parent's left node, go to the parent's right
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 614) 		 * node then all the way down to the left */
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 615) 		return rb_left_deepest_node(parent->rb_right);
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 616) 	} else
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 617) 		/* Otherwise we are the parent's right node, and the parent
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 618) 		 * should be next */
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 619) 		return (struct rb_node *)parent;
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 620) }
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 621) EXPORT_SYMBOL(rb_next_postorder);
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 622) 
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 623) struct rb_node *rb_first_postorder(const struct rb_root *root)
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 624) {
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 625) 	if (!root->rb_node)
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 626) 		return NULL;
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 627) 
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 628) 	return rb_left_deepest_node(root->rb_node);
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 629) }
9dee5c51516d2 (Cody P Schafer      2013-09-11 14:25:10 -0700 630) EXPORT_SYMBOL(rb_first_postorder);