Skip to content

Commit bfea9a8

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
bpf: Add name to struct bpf_ksym
Adding name to 'struct bpf_ksym' object to carry the name of the symbol for bpf_prog, bpf_trampoline, bpf_dispatcher objects. The current benefit is that name is now generated only when the symbol is added to the list, so we don't need to generate it every time it's accessed. The future benefit is that we will have all the bpf objects symbols represented by struct bpf_ksym. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Song Liu <songliubraving@fb.com> Link: https://lore.kernel.org/bpf/20200312195610.346362-5-jolsa@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 535911c commit bfea9a8

4 files changed

Lines changed: 12 additions & 14 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/refcount.h>
1919
#include <linux/mutex.h>
2020
#include <linux/module.h>
21+
#include <linux/kallsyms.h>
2122

2223
struct bpf_verifier_env;
2324
struct bpf_verifier_log;
@@ -474,6 +475,7 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start);
474475
struct bpf_ksym {
475476
unsigned long start;
476477
unsigned long end;
478+
char name[KSYM_NAME_LEN];
477479
};
478480

479481
enum bpf_tramp_prog_type {

‎include/linux/filter.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,6 @@ bpf_address_lookup(unsigned long addr, unsigned long *size,
10831083

10841084
void bpf_prog_kallsyms_add(struct bpf_prog *fp);
10851085
void bpf_prog_kallsyms_del(struct bpf_prog *fp);
1086-
void bpf_get_prog_name(const struct bpf_prog *prog, char *sym);
10871086

10881087
#else /* CONFIG_BPF_JIT */
10891088

@@ -1152,11 +1151,6 @@ static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
11521151
{
11531152
}
11541153

1155-
static inline void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
1156-
{
1157-
sym[0] = '\0';
1158-
}
1159-
11601154
#endif /* CONFIG_BPF_JIT */
11611155

11621156
void bpf_prog_kallsyms_del_all(struct bpf_prog *fp);

‎kernel/bpf/core.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,10 @@ bpf_prog_ksym_set_addr(struct bpf_prog *prog)
535535
prog->aux->ksym.end = addr + hdr->pages * PAGE_SIZE;
536536
}
537537

538-
void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
538+
static void
539+
bpf_prog_ksym_set_name(struct bpf_prog *prog)
539540
{
541+
char *sym = prog->aux->ksym.name;
540542
const char *end = sym + KSYM_NAME_LEN;
541543
const struct btf_type *type;
542544
const char *func_name;
@@ -643,6 +645,7 @@ void bpf_prog_kallsyms_add(struct bpf_prog *fp)
643645
return;
644646

645647
bpf_prog_ksym_set_addr(fp);
648+
bpf_prog_ksym_set_name(fp);
646649

647650
spin_lock_bh(&bpf_lock);
648651
bpf_prog_ksym_node_add(fp->aux);
@@ -681,7 +684,7 @@ const char *__bpf_address_lookup(unsigned long addr, unsigned long *size,
681684
unsigned long symbol_start = prog->aux->ksym.start;
682685
unsigned long symbol_end = prog->aux->ksym.end;
683686

684-
bpf_get_prog_name(prog, sym);
687+
strncpy(sym, prog->aux->ksym.name, KSYM_NAME_LEN);
685688

686689
ret = sym;
687690
if (size)
@@ -738,7 +741,7 @@ int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
738741
if (it++ != symnum)
739742
continue;
740743

741-
bpf_get_prog_name(aux->prog, sym);
744+
strncpy(sym, aux->ksym.name, KSYM_NAME_LEN);
742745

743746
*value = (unsigned long)aux->prog->bpf_func;
744747
*type = BPF_SYM_ELF_TYPE;

‎kernel/events/core.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8255,23 +8255,22 @@ static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
82558255
enum perf_bpf_event_type type)
82568256
{
82578257
bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
8258-
char sym[KSYM_NAME_LEN];
82598258
int i;
82608259

82618260
if (prog->aux->func_cnt == 0) {
8262-
bpf_get_prog_name(prog, sym);
82638261
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
82648262
(u64)(unsigned long)prog->bpf_func,
8265-
prog->jited_len, unregister, sym);
8263+
prog->jited_len, unregister,
8264+
prog->aux->ksym.name);
82668265
} else {
82678266
for (i = 0; i < prog->aux->func_cnt; i++) {
82688267
struct bpf_prog *subprog = prog->aux->func[i];
82698268

8270-
bpf_get_prog_name(subprog, sym);
82718269
perf_event_ksymbol(
82728270
PERF_RECORD_KSYMBOL_TYPE_BPF,
82738271
(u64)(unsigned long)subprog->bpf_func,
8274-
subprog->jited_len, unregister, sym);
8272+
subprog->jited_len, unregister,
8273+
prog->aux->ksym.name);
82758274
}
82768275
}
82778276
}

0 commit comments

Comments
 (0)