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