Skip to content

Commit a108f7d

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
bpf: Add trampolines to kallsyms
Adding trampolines to kallsyms. It's displayed as bpf_trampoline_<ID> [bpf] where ID is the BTF id of the trampoline function. Adding bpf_image_ksym_add/del functions that setup the start/end values and call KSYMBOL perf events handlers. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200312195610.346362-11-jolsa@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent dba122f commit a108f7d

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ struct bpf_trampoline {
513513
/* Executable image of trampoline */
514514
void *image;
515515
u64 selector;
516+
struct bpf_ksym ksym;
516517
};
517518

518519
#define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
@@ -585,6 +586,8 @@ struct bpf_image {
585586
bool is_bpf_image_address(unsigned long address);
586587
void *bpf_image_alloc(void);
587588
/* Called only from JIT-enabled code, so there's no need for stubs. */
589+
void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym);
590+
void bpf_image_ksym_del(struct bpf_ksym *ksym);
588591
void bpf_ksym_add(struct bpf_ksym *ksym);
589592
void bpf_ksym_del(struct bpf_ksym *ksym);
590593
#else

‎kernel/bpf/trampoline.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/filter.h>
66
#include <linux/ftrace.h>
77
#include <linux/rbtree_latch.h>
8+
#include <linux/perf_event.h>
89

910
/* dummy _ops. The verifier will operate on target program's ops. */
1011
const struct bpf_verifier_ops bpf_extension_verifier_ops = {
@@ -96,6 +97,30 @@ bool is_bpf_image_address(unsigned long addr)
9697
return ret;
9798
}
9899

100+
void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
101+
{
102+
ksym->start = (unsigned long) data;
103+
ksym->end = ksym->start + BPF_IMAGE_SIZE;
104+
bpf_ksym_add(ksym);
105+
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
106+
BPF_IMAGE_SIZE, false, ksym->name);
107+
}
108+
109+
void bpf_image_ksym_del(struct bpf_ksym *ksym)
110+
{
111+
bpf_ksym_del(ksym);
112+
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
113+
BPF_IMAGE_SIZE, true, ksym->name);
114+
}
115+
116+
static void bpf_trampoline_ksym_add(struct bpf_trampoline *tr)
117+
{
118+
struct bpf_ksym *ksym = &tr->ksym;
119+
120+
snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", tr->key);
121+
bpf_image_ksym_add(tr->image, ksym);
122+
}
123+
99124
struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
100125
{
101126
struct bpf_trampoline *tr;
@@ -131,6 +156,8 @@ struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
131156
for (i = 0; i < BPF_TRAMP_MAX; i++)
132157
INIT_HLIST_HEAD(&tr->progs_hlist[i]);
133158
tr->image = image;
159+
INIT_LIST_HEAD_RCU(&tr->ksym.lnode);
160+
bpf_trampoline_ksym_add(tr);
134161
out:
135162
mutex_unlock(&trampoline_mutex);
136163
return tr;
@@ -368,6 +395,7 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
368395
goto out;
369396
if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT])))
370397
goto out;
398+
bpf_image_ksym_del(&tr->ksym);
371399
image = container_of(tr->image, struct bpf_image, data);
372400
latch_tree_erase(&image->tnode, &image_tree, &image_tree_ops);
373401
/* wait for tasks to get out of trampoline before freeing it */

0 commit comments

Comments
 (0)