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

rbtree, uprobes: Use rbtree helpers

Reduce rbtree boilerplate by using the new helpers.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Davidlohr Bueso <dbueso@suse.de>

authored by

Peter Zijlstra and committed by
Ingo Molnar
a905e84e a3b89864

+39 -41
+39 -41
kernel/events/uprobes.c
··· 613 613 } 614 614 } 615 615 616 - static int match_uprobe(struct uprobe *l, struct uprobe *r) 616 + static __always_inline 617 + int uprobe_cmp(const struct inode *l_inode, const loff_t l_offset, 618 + const struct uprobe *r) 617 619 { 618 - if (l->inode < r->inode) 620 + if (l_inode < r->inode) 619 621 return -1; 620 622 621 - if (l->inode > r->inode) 623 + if (l_inode > r->inode) 622 624 return 1; 623 625 624 - if (l->offset < r->offset) 626 + if (l_offset < r->offset) 625 627 return -1; 626 628 627 - if (l->offset > r->offset) 629 + if (l_offset > r->offset) 628 630 return 1; 629 631 630 632 return 0; 631 633 } 632 634 635 + #define __node_2_uprobe(node) \ 636 + rb_entry((node), struct uprobe, rb_node) 637 + 638 + struct __uprobe_key { 639 + struct inode *inode; 640 + loff_t offset; 641 + }; 642 + 643 + static inline int __uprobe_cmp_key(const void *key, const struct rb_node *b) 644 + { 645 + const struct __uprobe_key *a = key; 646 + return uprobe_cmp(a->inode, a->offset, __node_2_uprobe(b)); 647 + } 648 + 649 + static inline int __uprobe_cmp(struct rb_node *a, const struct rb_node *b) 650 + { 651 + struct uprobe *u = __node_2_uprobe(a); 652 + return uprobe_cmp(u->inode, u->offset, __node_2_uprobe(b)); 653 + } 654 + 633 655 static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset) 634 656 { 635 - struct uprobe u = { .inode = inode, .offset = offset }; 636 - struct rb_node *n = uprobes_tree.rb_node; 637 - struct uprobe *uprobe; 638 - int match; 657 + struct __uprobe_key key = { 658 + .inode = inode, 659 + .offset = offset, 660 + }; 661 + struct rb_node *node = rb_find(&key, &uprobes_tree, __uprobe_cmp_key); 639 662 640 - while (n) { 641 - uprobe = rb_entry(n, struct uprobe, rb_node); 642 - match = match_uprobe(&u, uprobe); 643 - if (!match) 644 - return get_uprobe(uprobe); 663 + if (node) 664 + return __node_2_uprobe(node); 645 665 646 - if (match < 0) 647 - n = n->rb_left; 648 - else 649 - n = n->rb_right; 650 - } 651 666 return NULL; 652 667 } 653 668 ··· 683 668 684 669 static struct uprobe *__insert_uprobe(struct uprobe *uprobe) 685 670 { 686 - struct rb_node **p = &uprobes_tree.rb_node; 687 - struct rb_node *parent = NULL; 688 - struct uprobe *u; 689 - int match; 671 + struct rb_node *node; 690 672 691 - while (*p) { 692 - parent = *p; 693 - u = rb_entry(parent, struct uprobe, rb_node); 694 - match = match_uprobe(uprobe, u); 695 - if (!match) 696 - return get_uprobe(u); 673 + node = rb_find_add(&uprobe->rb_node, &uprobes_tree, __uprobe_cmp); 674 + if (node) 675 + return get_uprobe(__node_2_uprobe(node)); 697 676 698 - if (match < 0) 699 - p = &parent->rb_left; 700 - else 701 - p = &parent->rb_right; 702 - 703 - } 704 - 705 - u = NULL; 706 - rb_link_node(&uprobe->rb_node, parent, p); 707 - rb_insert_color(&uprobe->rb_node, &uprobes_tree); 708 677 /* get access + creation ref */ 709 678 refcount_set(&uprobe->ref, 2); 710 - 711 - return u; 679 + return NULL; 712 680 } 713 681 714 682 /*