blob: 1ef6e25d031c6ba44be8e4115bfc25b60eee35b4 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 Red Black Trees
4 (C) 1999 Andrea Arcangeli <andrea@suse.de>
5 (C) 2002 David Woodhouse <dwmw2@infradead.org>
Michel Lespinasse46b61352012-10-08 16:31:11 -07006 (C) 2012 Michel Lespinasse <walken@google.com>
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9 linux/lib/rbtree.c
10*/
11
Michel Lespinasse9c079ad2012-10-08 16:31:33 -070012#include <linux/rbtree_augmented.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Michel Lespinasse5bc91882012-10-08 16:30:47 -070015/*
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
Michel Lespinasse6280d232012-10-08 16:30:57 -070031 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
32 * parentheses and have some accompanying text comment.
Michel Lespinasse5bc91882012-10-08 16:30:47 -070033 */
34
Peter Zijlstrad72da4a2015-05-27 11:09:36 +093035/*
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
Michel Lespinasse46b61352012-10-08 16:31:11 -070059static inline void rb_set_black(struct rb_node *rb)
60{
61 rb->__rb_parent_color |= RB_BLACK;
62}
63
Michel Lespinasse5bc91882012-10-08 16:30:47 -070064static inline struct rb_node *rb_red_parent(struct rb_node *red)
65{
66 return (struct rb_node *)red->__rb_parent_color;
67}
68
Michel Lespinasse5bc91882012-10-08 16:30:47 -070069/*
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 */
74static 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);
Michel Lespinasse7abc7042012-10-08 16:31:07 -070081 __rb_change_child(old, new, parent, root);
Michel Lespinasse5bc91882012-10-08 16:30:47 -070082}
83
Michel Lespinasse14b94af2012-10-08 16:31:17 -070084static __always_inline void
85__rb_insert(struct rb_node *node, struct rb_root *root,
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -070086 bool newleft, struct rb_node **leftmost,
Michel Lespinasse14b94af2012-10-08 16:31:17 -070087 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Michel Lespinasse5bc91882012-10-08 16:30:47 -070089 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -070091 if (newleft)
92 *leftmost = node;
93
Michel Lespinasse6d584522012-10-08 16:30:44 -070094 while (true) {
95 /*
Davidlohr Bueso2aadf7f2017-09-08 16:14:39 -070096 * Loop invariant: node is red.
Michel Lespinasse6d584522012-10-08 16:30:44 -070097 */
Davidlohr Bueso2aadf7f2017-09-08 16:14:39 -070098 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 */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700104 rb_set_parent_color(node, NULL, RB_BLACK);
Michel Lespinasse6d584522012-10-08 16:30:44 -0700105 break;
Davidlohr Bueso2aadf7f2017-09-08 16:14:39 -0700106 }
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))
Michel Lespinasse6d584522012-10-08 16:30:44 -0700115 break;
116
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700117 gparent = rb_red_parent(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700119 tmp = gparent->rb_right;
120 if (parent != tmp) { /* parent == gparent->rb_left */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700121 if (tmp && rb_is_red(tmp)) {
122 /*
Davidlohr Bueso35dc67d2017-09-08 16:14:42 -0700123 * Case 1 - node's uncle is red (color flips).
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700124 *
125 * G g
126 * / \ / \
127 * p u --> P U
128 * / /
Wei Yang1b9c53e2014-08-08 14:22:14 -0700129 * n n
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700130 *
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700143 tmp = parent->rb_right;
144 if (node == tmp) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700145 /*
Davidlohr Bueso35dc67d2017-09-08 16:14:42 -0700146 * Case 2 - node's uncle is black and node is
147 * the parent's right child (left rotate at parent).
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700148 *
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 */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930158 tmp = node->rb_left;
159 WRITE_ONCE(parent->rb_right, tmp);
160 WRITE_ONCE(node->rb_left, parent);
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700161 if (tmp)
162 rb_set_parent_color(tmp, parent,
163 RB_BLACK);
164 rb_set_parent_color(parent, node, RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700165 augment_rotate(parent, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 parent = node;
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700167 tmp = node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700170 /*
Davidlohr Bueso35dc67d2017-09-08 16:14:42 -0700171 * Case 3 - node's uncle is black and node is
172 * the parent's left child (right rotate at gparent).
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700173 *
174 * G P
175 * / \ / \
176 * p U --> n g
177 * / \
178 * n U
179 */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930180 WRITE_ONCE(gparent->rb_left, tmp); /* == parent->rb_right */
181 WRITE_ONCE(parent->rb_right, gparent);
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700182 if (tmp)
183 rb_set_parent_color(tmp, gparent, RB_BLACK);
184 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700185 augment_rotate(gparent, parent);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 } else {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700188 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700199 tmp = parent->rb_left;
200 if (node == tmp) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700201 /* Case 2 - right rotate at parent */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930202 tmp = node->rb_right;
203 WRITE_ONCE(parent->rb_left, tmp);
204 WRITE_ONCE(node->rb_right, parent);
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700205 if (tmp)
206 rb_set_parent_color(tmp, parent,
207 RB_BLACK);
208 rb_set_parent_color(parent, node, RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700209 augment_rotate(parent, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 parent = node;
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700211 tmp = node->rb_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700214 /* Case 3 - left rotate at gparent */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930215 WRITE_ONCE(gparent->rb_right, tmp); /* == parent->rb_left */
216 WRITE_ONCE(parent->rb_left, gparent);
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700217 if (tmp)
218 rb_set_parent_color(tmp, gparent, RB_BLACK);
219 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700220 augment_rotate(gparent, parent);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Michel Lespinasse3cb7a562013-01-11 14:32:20 -0800226/*
227 * Inline version for rb_erase() use - we want to be able to inline
228 * and eliminate the dummy_rotate callback there
229 */
230static __always_inline void
231____rb_erase_color(struct rb_node *parent, struct rb_root *root,
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700232 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Michel Lespinasse46b61352012-10-08 16:31:11 -0700234 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700236 while (true) {
237 /*
Michel Lespinasse46b61352012-10-08 16:31:11 -0700238 * 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.
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700243 */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700244 sibling = parent->rb_right;
245 if (node != sibling) { /* node == parent->rb_left */
Michel Lespinasse6280d232012-10-08 16:30:57 -0700246 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 */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930256 tmp1 = sibling->rb_left;
257 WRITE_ONCE(parent->rb_right, tmp1);
258 WRITE_ONCE(sibling->rb_left, parent);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700259 rb_set_parent_color(tmp1, parent, RB_BLACK);
260 __rb_rotate_set_parents(parent, sibling, root,
261 RB_RED);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700262 augment_rotate(parent, sibling);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700263 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700265 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 *
Michel Lespinasse46b61352012-10-08 16:31:11 -0700279 * 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.
Michel Lespinasse6280d232012-10-08 16:30:57 -0700283 */
284 rb_set_parent_color(sibling, parent,
285 RB_RED);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700286 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700296 /*
297 * Case 3 - right rotate at sibling
298 * (p could be either color here)
299 *
300 * (p) (p)
301 * / \ / \
Jie Chence093a02016-12-12 16:46:17 -0800302 * N S --> N sl
Michel Lespinasse6280d232012-10-08 16:30:57 -0700303 * / \ \
Jie Chence093a02016-12-12 16:46:17 -0800304 * sl Sr S
Michel Lespinasse6280d232012-10-08 16:30:57 -0700305 * \
306 * Sr
Jie Chence093a02016-12-12 16:46:17 -0800307 *
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
Michel Lespinasse6280d232012-10-08 16:30:57 -0700322 */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930323 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);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700327 if (tmp1)
328 rb_set_parent_color(tmp1, sibling,
329 RB_BLACK);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700330 augment_rotate(sibling, tmp2);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700331 tmp1 = sibling;
332 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700334 /*
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 */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930346 tmp2 = sibling->rb_left;
347 WRITE_ONCE(parent->rb_right, tmp2);
348 WRITE_ONCE(sibling->rb_left, parent);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700349 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);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700354 augment_rotate(parent, sibling);
Michel Lespinassee125d142012-10-08 16:30:54 -0700355 break;
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700356 } else {
Michel Lespinasse6280d232012-10-08 16:30:57 -0700357 sibling = parent->rb_left;
358 if (rb_is_red(sibling)) {
359 /* Case 1 - right rotate at parent */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930360 tmp1 = sibling->rb_right;
361 WRITE_ONCE(parent->rb_left, tmp1);
362 WRITE_ONCE(sibling->rb_right, parent);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700363 rb_set_parent_color(tmp1, parent, RB_BLACK);
364 __rb_rotate_set_parents(parent, sibling, root,
365 RB_RED);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700366 augment_rotate(parent, sibling);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700367 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700369 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);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700376 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Jie Chence093a02016-12-12 16:46:17 -0800386 /* Case 3 - left rotate at sibling */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930387 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);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700391 if (tmp1)
392 rb_set_parent_color(tmp1, sibling,
393 RB_BLACK);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700394 augment_rotate(sibling, tmp2);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700395 tmp1 = sibling;
396 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Jie Chence093a02016-12-12 16:46:17 -0800398 /* Case 4 - right rotate at parent + color flips */
Peter Zijlstrad72da4a2015-05-27 11:09:36 +0930399 tmp2 = sibling->rb_right;
400 WRITE_ONCE(parent->rb_left, tmp2);
401 WRITE_ONCE(sibling->rb_right, parent);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700402 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);
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700407 augment_rotate(parent, sibling);
Michel Lespinassee125d142012-10-08 16:30:54 -0700408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
Michel Lespinasse3cb7a562013-01-11 14:32:20 -0800412
413/* Non-inline version for rb_erase_augmented() use */
414void __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}
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700419EXPORT_SYMBOL(__rb_erase_color);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700420
421/*
422 * Non-augmented rbtree manipulation functions.
423 *
424 * We use dummy augmented callbacks here, and have the compiler optimize them
425 * out of the rb_insert_color() and rb_erase() function definitions.
426 */
427
428static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
429static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
430static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
431
432static const struct rb_augment_callbacks dummy_callbacks = {
Kees Cookf231aeb2017-02-24 15:01:04 -0800433 .propagate = dummy_propagate,
434 .copy = dummy_copy,
435 .rotate = dummy_rotate
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700436};
437
438void rb_insert_color(struct rb_node *node, struct rb_root *root)
439{
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700440 __rb_insert(node, root, false, NULL, dummy_rotate);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700441}
442EXPORT_SYMBOL(rb_insert_color);
443
444void rb_erase(struct rb_node *node, struct rb_root *root)
445{
Michel Lespinasse3cb7a562013-01-11 14:32:20 -0800446 struct rb_node *rebalance;
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700447 rebalance = __rb_erase_augmented(node, root,
448 NULL, &dummy_callbacks);
Michel Lespinasse3cb7a562013-01-11 14:32:20 -0800449 if (rebalance)
450 ____rb_erase_color(rebalance, root, dummy_rotate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452EXPORT_SYMBOL(rb_erase);
453
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700454void rb_insert_color_cached(struct rb_node *node,
455 struct rb_root_cached *root, bool leftmost)
456{
457 __rb_insert(node, &root->rb_root, leftmost,
458 &root->rb_leftmost, dummy_rotate);
459}
460EXPORT_SYMBOL(rb_insert_color_cached);
461
462void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root)
463{
464 struct rb_node *rebalance;
465 rebalance = __rb_erase_augmented(node, &root->rb_root,
466 &root->rb_leftmost, &dummy_callbacks);
467 if (rebalance)
468 ____rb_erase_color(rebalance, &root->rb_root, dummy_rotate);
469}
470EXPORT_SYMBOL(rb_erase_cached);
471
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700472/*
473 * Augmented rbtree manipulation functions.
474 *
475 * This instantiates the same __always_inline functions as in the non-augmented
476 * case, but this time with user-defined callbacks.
477 */
478
479void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700480 bool newleft, struct rb_node **leftmost,
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700481 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
482{
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700483 __rb_insert(node, root, newleft, leftmost, augment_rotate);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700484}
485EXPORT_SYMBOL(__rb_insert_augmented);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * This function returns the first node (in sort order) of the tree.
489 */
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000490struct rb_node *rb_first(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct rb_node *n;
493
494 n = root->rb_node;
495 if (!n)
496 return NULL;
497 while (n->rb_left)
498 n = n->rb_left;
499 return n;
500}
501EXPORT_SYMBOL(rb_first);
502
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000503struct rb_node *rb_last(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct rb_node *n;
506
507 n = root->rb_node;
508 if (!n)
509 return NULL;
510 while (n->rb_right)
511 n = n->rb_right;
512 return n;
513}
514EXPORT_SYMBOL(rb_last);
515
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000516struct rb_node *rb_next(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
David Woodhouse55a98102006-04-21 13:35:51 +0100518 struct rb_node *parent;
519
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700520 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200521 return NULL;
522
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700523 /*
524 * If we have a right-hand child, go down and then left as far
525 * as we can.
526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (node->rb_right) {
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700528 node = node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 while (node->rb_left)
530 node=node->rb_left;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000531 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700534 /*
535 * No right-hand children. Everything down and left is smaller than us,
536 * so any 'next' node must be in the general direction of our parent.
537 * Go up the tree; any time the ancestor is a right-hand child of its
538 * parent, keep going up. First time it's a left-hand child of its
539 * parent, said parent is our 'next' node.
540 */
David Woodhouse55a98102006-04-21 13:35:51 +0100541 while ((parent = rb_parent(node)) && node == parent->rb_right)
542 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
David Woodhouse55a98102006-04-21 13:35:51 +0100544 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546EXPORT_SYMBOL(rb_next);
547
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000548struct rb_node *rb_prev(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
David Woodhouse55a98102006-04-21 13:35:51 +0100550 struct rb_node *parent;
551
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700552 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200553 return NULL;
554
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700555 /*
556 * If we have a left-hand child, go down and then right as far
557 * as we can.
558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (node->rb_left) {
Davidlohr Buesocd9e61e2017-09-08 16:14:36 -0700560 node = node->rb_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 while (node->rb_right)
562 node=node->rb_right;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000563 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700566 /*
567 * No left-hand children. Go up till we find an ancestor which
568 * is a right-hand child of its parent.
569 */
David Woodhouse55a98102006-04-21 13:35:51 +0100570 while ((parent = rb_parent(node)) && node == parent->rb_left)
571 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
David Woodhouse55a98102006-04-21 13:35:51 +0100573 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575EXPORT_SYMBOL(rb_prev);
576
577void rb_replace_node(struct rb_node *victim, struct rb_node *new,
578 struct rb_root *root)
579{
David Woodhouse55a98102006-04-21 13:35:51 +0100580 struct rb_node *parent = rb_parent(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
David Howellsc1adf202016-07-01 07:53:51 +0100582 /* Copy the pointers/colour from the victim to the replacement */
583 *new = *victim;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Set the surrounding nodes to point to the replacement */
David Howellsc1adf202016-07-01 07:53:51 +0100586 if (victim->rb_left)
587 rb_set_parent(victim->rb_left, new);
588 if (victim->rb_right)
589 rb_set_parent(victim->rb_right, new);
Michel Lespinasse7abc7042012-10-08 16:31:07 -0700590 __rb_change_child(victim, new, parent, root);
David Howellsc1adf202016-07-01 07:53:51 +0100591}
592EXPORT_SYMBOL(rb_replace_node);
593
Chris Wilson338f1d92017-12-14 15:32:28 -0800594void rb_replace_node_cached(struct rb_node *victim, struct rb_node *new,
595 struct rb_root_cached *root)
596{
597 rb_replace_node(victim, new, &root->rb_root);
598
599 if (root->rb_leftmost == victim)
600 root->rb_leftmost = new;
601}
602EXPORT_SYMBOL(rb_replace_node_cached);
603
David Howellsc1adf202016-07-01 07:53:51 +0100604void rb_replace_node_rcu(struct rb_node *victim, struct rb_node *new,
605 struct rb_root *root)
606{
607 struct rb_node *parent = rb_parent(victim);
608
609 /* Copy the pointers/colour from the victim to the replacement */
610 *new = *victim;
611
612 /* Set the surrounding nodes to point to the replacement */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (victim->rb_left)
David Woodhouse55a98102006-04-21 13:35:51 +0100614 rb_set_parent(victim->rb_left, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (victim->rb_right)
David Woodhouse55a98102006-04-21 13:35:51 +0100616 rb_set_parent(victim->rb_right, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
David Howellsc1adf202016-07-01 07:53:51 +0100618 /* Set the parent's pointer to the new node last after an RCU barrier
619 * so that the pointers onwards are seen to be set correctly when doing
620 * an RCU walk over the tree.
621 */
622 __rb_change_child_rcu(victim, new, parent, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
David Howellsc1adf202016-07-01 07:53:51 +0100624EXPORT_SYMBOL(rb_replace_node_rcu);
Cody P Schafer9dee5c52013-09-11 14:25:10 -0700625
626static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
627{
628 for (;;) {
629 if (node->rb_left)
630 node = node->rb_left;
631 else if (node->rb_right)
632 node = node->rb_right;
633 else
634 return (struct rb_node *)node;
635 }
636}
637
638struct rb_node *rb_next_postorder(const struct rb_node *node)
639{
640 const struct rb_node *parent;
641 if (!node)
642 return NULL;
643 parent = rb_parent(node);
644
645 /* If we're sitting on node, we've already seen our children */
646 if (parent && node == parent->rb_left && parent->rb_right) {
647 /* If we are the parent's left node, go to the parent's right
648 * node then all the way down to the left */
649 return rb_left_deepest_node(parent->rb_right);
650 } else
651 /* Otherwise we are the parent's right node, and the parent
652 * should be next */
653 return (struct rb_node *)parent;
654}
655EXPORT_SYMBOL(rb_next_postorder);
656
657struct rb_node *rb_first_postorder(const struct rb_root *root)
658{
659 if (!root->rb_node)
660 return NULL;
661
662 return rb_left_deepest_node(root->rb_node);
663}
664EXPORT_SYMBOL(rb_first_postorder);