History

bpf: Add bpf_ksym_add/del functions

Separating /proc/kallsyms add/del code and adding bpf_ksym_add/del functions for that. Moving bpf_prog_ksym_node_add/del functions to __bpf_ksym_add/del and changing their argument to 'struct bpf_ksym' object. This way we can call them for other bpf objects types like trampoline and dispatcher.

Jiri Olsa committed 6 years ago

Commit dba122f1 parent cbd76f82 files · +22 −14Browse files

Unified · Split

2 files
  1. include/linux/bpf.h +3 −0
  2. kernel/bpf/core.c +19 −14
include/linux/bpf.h+3−0
@@ -584,6 +584,9 @@ struct bpf_image { · 583 unmodified lines
584#define BPF_IMAGE_SIZE (PAGE_SIZE - sizeof(struct bpf_image))584#define BPF_IMAGE_SIZE (PAGE_SIZE - sizeof(struct bpf_image))
585bool is_bpf_image_address(unsigned long address);585bool is_bpf_image_address(unsigned long address);
586void *bpf_image_alloc(void);586void *bpf_image_alloc(void);
587/* Called only from JIT-enabled code, so there's no need for stubs. */
588void bpf_ksym_add(struct bpf_ksym *ksym);
589void bpf_ksym_del(struct bpf_ksym *ksym);
587#else590#else
588static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)591static inline struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
589{592{
kernel/bpf/core.c+19−14
@@ -607,20 +607,29 @@ static DEFINE_SPINLOCK(bpf_lock); · 606 unmodified lines
607static LIST_HEAD(bpf_kallsyms);607static LIST_HEAD(bpf_kallsyms);
608static struct latch_tree_root bpf_tree __cacheline_aligned;608static struct latch_tree_root bpf_tree __cacheline_aligned;
609 609
610static void bpf_prog_ksym_node_add(struct bpf_prog_aux *aux)610void bpf_ksym_add(struct bpf_ksym *ksym)
611{611{
612 WARN_ON_ONCE(!list_empty(&aux->ksym.lnode));612 spin_lock_bh(&bpf_lock);
613 list_add_tail_rcu(&aux->ksym.lnode, &bpf_kallsyms);613 WARN_ON_ONCE(!list_empty(&ksym->lnode));
614 latch_tree_insert(&aux->ksym.tnode, &bpf_tree, &bpf_tree_ops);614 list_add_tail_rcu(&ksym->lnode, &bpf_kallsyms);
615 latch_tree_insert(&ksym->tnode, &bpf_tree, &bpf_tree_ops);
616 spin_unlock_bh(&bpf_lock);
615}617}
616 618
617static void bpf_prog_ksym_node_del(struct bpf_prog_aux *aux)619static void __bpf_ksym_del(struct bpf_ksym *ksym)
618{620{
619 if (list_empty(&aux->ksym.lnode))621 if (list_empty(&ksym->lnode))
620 return;622 return;
621 623
622 latch_tree_erase(&aux->ksym.tnode, &bpf_tree, &bpf_tree_ops);624 latch_tree_erase(&ksym->tnode, &bpf_tree, &bpf_tree_ops);
623 list_del_rcu(&aux->ksym.lnode);625 list_del_rcu(&ksym->lnode);
626}
627
628void bpf_ksym_del(struct bpf_ksym *ksym)
629{
630 spin_lock_bh(&bpf_lock);
631 __bpf_ksym_del(ksym);
632 spin_unlock_bh(&bpf_lock);
624}633}
625 634
626static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)635static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
@@ -644,9 +653,7 @@ void bpf_prog_kallsyms_add(struct bpf_prog *fp) · 17 unmodified lines
644 bpf_prog_ksym_set_name(fp);653 bpf_prog_ksym_set_name(fp);
645 fp->aux->ksym.prog = true;654 fp->aux->ksym.prog = true;
646 655
647 spin_lock_bh(&bpf_lock);656 bpf_ksym_add(&fp->aux->ksym);
648 bpf_prog_ksym_node_add(fp->aux);
649 spin_unlock_bh(&bpf_lock);
650}657}
651 658
652void bpf_prog_kallsyms_del(struct bpf_prog *fp)659void bpf_prog_kallsyms_del(struct bpf_prog *fp)
@@ -654,9 +661,7 @@ void bpf_prog_kallsyms_del(struct bpf_prog *fp) · 1 unmodified line
654 if (!bpf_prog_kallsyms_candidate(fp))661 if (!bpf_prog_kallsyms_candidate(fp))
655 return;662 return;
656 663
657 spin_lock_bh(&bpf_lock);664 bpf_ksym_del(&fp->aux->ksym);
658 bpf_prog_ksym_node_del(fp->aux);
659 spin_unlock_bh(&bpf_lock);
660}665}
661 666
662static struct bpf_ksym *bpf_ksym_find(unsigned long addr)667static struct bpf_ksym *bpf_ksym_find(unsigned long addr)