Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rbtree: cache leftmost node internally

Patch series "rbtree: Cache leftmost node internally", v4.

A series to extending rbtrees to internally cache the leftmost node such
that we can have fast overlap check optimization for all interval tree
users[1]. The benefits of this series are that:

(i) Unify users that do internal leftmost node caching.
(ii) Optimize all interval tree users.
(iii) Convert at least two new users (epoll and procfs) to the new interface.

This patch (of 16):

Red-black tree semantics imply that nodes with smaller or greater (or
equal for duplicates) keys always be to the left and right,
respectively. For the kernel this is extremely evident when considering
our rb_first() semantics. Enabling lookups for the smallest node in the
tree in O(1) can save a good chunk of cycles in not having to walk down
the tree each time. To this end there are a few core users that
explicitly do this, such as the scheduler and rtmutexes. There is also
the desire for interval trees to have this optimization allowing faster
overlap checking.

This patch introduces a new 'struct rb_root_cached' which is just the
root with a cached pointer to the leftmost node. The reason why the
regular rb_root was not extended instead of adding a new structure was
that this allows the user to have the choice between memory footprint
and actual tree performance. The new wrappers on top of the regular
rb_root calls are:

- rb_first_cached(cached_root) -- which is a fast replacement
for rb_first.

- rb_insert_color_cached(node, cached_root, new)

- rb_erase_cached(node, cached_root)

In addition, augmented cached interfaces are also added for basic
insertion and deletion operations; which becomes important for the
interval tree changes.

With the exception of the inserts, which adds a bool for updating the
new leftmost, the interfaces are kept the same. To this end, porting rb
users to the cached version becomes really trivial, and keeping current
rbtree semantics for users that don't care about the optimization
requires zero overhead.

Link: http://lkml.kernel.org/r/20170719014603.19029-2-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Reviewed-by: Jan Kara <jack@suse.cz>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Davidlohr Bueso and committed by
Linus Torvalds
cd9e61ed c32ee3d9

+113 -8
+33
Documentation/rbtree.txt
··· 193 193 for (node = rb_first(&mytree); node; node = rb_next(node)) 194 194 printk("key=%s\n", rb_entry(node, struct mytype, node)->keystring); 195 195 196 + Cached rbtrees 197 + -------------- 198 + 199 + Computing the leftmost (smallest) node is quite a common task for binary 200 + search trees, such as for traversals or users relying on a the particular 201 + order for their own logic. To this end, users can use 'struct rb_root_cached' 202 + to optimize O(logN) rb_first() calls to a simple pointer fetch avoiding 203 + potentially expensive tree iterations. This is done at negligible runtime 204 + overhead for maintanence; albeit larger memory footprint. 205 + 206 + Similar to the rb_root structure, cached rbtrees are initialized to be 207 + empty via: 208 + 209 + struct rb_root_cached mytree = RB_ROOT_CACHED; 210 + 211 + Cached rbtree is simply a regular rb_root with an extra pointer to cache the 212 + leftmost node. This allows rb_root_cached to exist wherever rb_root does, 213 + which permits augmented trees to be supported as well as only a few extra 214 + interfaces: 215 + 216 + struct rb_node *rb_first_cached(struct rb_root_cached *tree); 217 + void rb_insert_color_cached(struct rb_node *, struct rb_root_cached *, bool); 218 + void rb_erase_cached(struct rb_node *node, struct rb_root_cached *); 219 + 220 + Both insert and erase calls have their respective counterpart of augmented 221 + trees: 222 + 223 + void rb_insert_augmented_cached(struct rb_node *node, struct rb_root_cached *, 224 + bool, struct rb_augment_callbacks *); 225 + void rb_erase_augmented_cached(struct rb_node *, struct rb_root_cached *, 226 + struct rb_augment_callbacks *); 227 + 228 + 196 229 Support for Augmented rbtrees 197 230 ----------------------------- 198 231
+21
include/linux/rbtree.h
··· 44 44 struct rb_node *rb_node; 45 45 }; 46 46 47 + /* 48 + * Leftmost-cached rbtrees. 49 + * 50 + * We do not cache the rightmost node based on footprint 51 + * size vs number of potential users that could benefit 52 + * from O(1) rb_last(). Just not worth it, users that want 53 + * this feature can always implement the logic explicitly. 54 + * Furthermore, users that want to cache both pointers may 55 + * find it a bit asymmetric, but that's ok. 56 + */ 57 + struct rb_root_cached { 58 + struct rb_root rb_root; 59 + struct rb_node *rb_leftmost; 60 + }; 47 61 48 62 #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3)) 49 63 50 64 #define RB_ROOT (struct rb_root) { NULL, } 65 + #define RB_ROOT_CACHED (struct rb_root_cached) { {NULL, }, NULL } 51 66 #define rb_entry(ptr, type, member) container_of(ptr, type, member) 52 67 53 68 #define RB_EMPTY_ROOT(root) (READ_ONCE((root)->rb_node) == NULL) ··· 83 68 extern struct rb_node *rb_prev(const struct rb_node *); 84 69 extern struct rb_node *rb_first(const struct rb_root *); 85 70 extern struct rb_node *rb_last(const struct rb_root *); 71 + 72 + extern void rb_insert_color_cached(struct rb_node *, 73 + struct rb_root_cached *, bool); 74 + extern void rb_erase_cached(struct rb_node *node, struct rb_root_cached *); 75 + /* Same as rb_first(), but O(1) */ 76 + #define rb_first_cached(root) (root)->rb_leftmost 86 77 87 78 /* Postorder iteration - always visit the parent after its children */ 88 79 extern struct rb_node *rb_first_postorder(const struct rb_root *);
+30 -3
include/linux/rbtree_augmented.h
··· 41 41 void (*rotate)(struct rb_node *old, struct rb_node *new); 42 42 }; 43 43 44 - extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, 44 + extern void __rb_insert_augmented(struct rb_node *node, 45 + struct rb_root *root, 46 + bool newleft, struct rb_node **leftmost, 45 47 void (*augment_rotate)(struct rb_node *old, struct rb_node *new)); 46 48 /* 47 49 * Fixup the rbtree and update the augmented information when rebalancing. ··· 59 57 rb_insert_augmented(struct rb_node *node, struct rb_root *root, 60 58 const struct rb_augment_callbacks *augment) 61 59 { 62 - __rb_insert_augmented(node, root, augment->rotate); 60 + __rb_insert_augmented(node, root, false, NULL, augment->rotate); 61 + } 62 + 63 + static inline void 64 + rb_insert_augmented_cached(struct rb_node *node, 65 + struct rb_root_cached *root, bool newleft, 66 + const struct rb_augment_callbacks *augment) 67 + { 68 + __rb_insert_augmented(node, &root->rb_root, 69 + newleft, &root->rb_leftmost, augment->rotate); 63 70 } 64 71 65 72 #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \ ··· 161 150 162 151 static __always_inline struct rb_node * 163 152 __rb_erase_augmented(struct rb_node *node, struct rb_root *root, 153 + struct rb_node **leftmost, 164 154 const struct rb_augment_callbacks *augment) 165 155 { 166 156 struct rb_node *child = node->rb_right; 167 157 struct rb_node *tmp = node->rb_left; 168 158 struct rb_node *parent, *rebalance; 169 159 unsigned long pc; 160 + 161 + if (leftmost && node == *leftmost) 162 + *leftmost = rb_next(node); 170 163 171 164 if (!tmp) { 172 165 /* ··· 271 256 rb_erase_augmented(struct rb_node *node, struct rb_root *root, 272 257 const struct rb_augment_callbacks *augment) 273 258 { 274 - struct rb_node *rebalance = __rb_erase_augmented(node, root, augment); 259 + struct rb_node *rebalance = __rb_erase_augmented(node, root, 260 + NULL, augment); 275 261 if (rebalance) 276 262 __rb_erase_color(rebalance, root, augment->rotate); 263 + } 264 + 265 + static __always_inline void 266 + rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root, 267 + const struct rb_augment_callbacks *augment) 268 + { 269 + struct rb_node *rebalance = __rb_erase_augmented(node, &root->rb_root, 270 + &root->rb_leftmost, 271 + augment); 272 + if (rebalance) 273 + __rb_erase_color(rebalance, &root->rb_root, augment->rotate); 277 274 } 278 275 279 276 #endif /* _LINUX_RBTREE_AUGMENTED_H */
+29 -5
lib/rbtree.c
··· 95 95 96 96 static __always_inline void 97 97 __rb_insert(struct rb_node *node, struct rb_root *root, 98 + bool newleft, struct rb_node **leftmost, 98 99 void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 99 100 { 100 101 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp; 102 + 103 + if (newleft) 104 + *leftmost = node; 101 105 102 106 while (true) { 103 107 /* ··· 438 434 439 435 void rb_insert_color(struct rb_node *node, struct rb_root *root) 440 436 { 441 - __rb_insert(node, root, dummy_rotate); 437 + __rb_insert(node, root, false, NULL, dummy_rotate); 442 438 } 443 439 EXPORT_SYMBOL(rb_insert_color); 444 440 445 441 void rb_erase(struct rb_node *node, struct rb_root *root) 446 442 { 447 443 struct rb_node *rebalance; 448 - rebalance = __rb_erase_augmented(node, root, &dummy_callbacks); 444 + rebalance = __rb_erase_augmented(node, root, 445 + NULL, &dummy_callbacks); 449 446 if (rebalance) 450 447 ____rb_erase_color(rebalance, root, dummy_rotate); 451 448 } 452 449 EXPORT_SYMBOL(rb_erase); 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 + EXPORT_SYMBOL(rb_insert_color_cached); 458 + 459 + void rb_erase_cached(struct rb_node *node, struct rb_root_cached *root) 460 + { 461 + struct rb_node *rebalance; 462 + rebalance = __rb_erase_augmented(node, &root->rb_root, 463 + &root->rb_leftmost, &dummy_callbacks); 464 + if (rebalance) 465 + ____rb_erase_color(rebalance, &root->rb_root, dummy_rotate); 466 + } 467 + EXPORT_SYMBOL(rb_erase_cached); 453 468 454 469 /* 455 470 * Augmented rbtree manipulation functions. ··· 478 455 */ 479 456 480 457 void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, 458 + bool newleft, struct rb_node **leftmost, 481 459 void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 482 460 { 483 - __rb_insert(node, root, augment_rotate); 461 + __rb_insert(node, root, newleft, leftmost, augment_rotate); 484 462 } 485 463 EXPORT_SYMBOL(__rb_insert_augmented); 486 464 ··· 526 502 * as we can. 527 503 */ 528 504 if (node->rb_right) { 529 - node = node->rb_right; 505 + node = node->rb_right; 530 506 while (node->rb_left) 531 507 node=node->rb_left; 532 508 return (struct rb_node *)node; ··· 558 534 * as we can. 559 535 */ 560 536 if (node->rb_left) { 561 - node = node->rb_left; 537 + node = node->rb_left; 562 538 while (node->rb_right) 563 539 node=node->rb_right; 564 540 return (struct rb_node *)node;