Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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