tools/bpf: do not use pahole if clang/llvm can generate BTF sections
Subsystems (7)
PATCH v3 10/13 tools/bpf: do not use pahole if clang/llvm can generate BTF sections
Add additional checks in tools/testing/selftests/bpf and samples/bpf such that if clang/llvm compiler can generate BTF sections, do not use pahole.
--- samples/bpf/Makefile | 8 ++++++++ tools/testing/selftests/bpf/Makefile | 8 ++++++++ 2 files changed, 16 insertions(+)
samples/bpf/Makefile +8 -0
@@ -208,12 +208,20 @@ endif BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm') +BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ + clang -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \ + readelf -S ./llvm_btf_verify.o | grep BTF; \ + /bin/rm -f ./llvm_btf_verify.o) +ifneq ($(BTF_LLVM_PROBE),) + EXTRA_CFLAGS += -g +else ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),) EXTRA_CFLAGS += -g LLC_FLAGS += -mattr=dwarfris DWARF2BTF = y endif +endif # Trick to allow make to be run from this directory all:
tools/testing/selftests/bpf/Makefile +8 -0
@@ -121,7 +121,14 @@ $(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm') +BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ + clang -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \ + readelf -S ./llvm_btf_verify.o | grep BTF; \ + /bin/rm -f ./llvm_btf_verify.o) +ifneq ($(BTF_LLVM_PROBE),) + CLANG_FLAGS += -g +else ifneq ($(BTF_LLC_PROBE),) ifneq ($(BTF_PAHOLE_PROBE),) ifneq ($(BTF_OBJCOPY_PROBE),)
@@ -131,6 +138,7 @@ ifneq ($(BTF_OBJCOPY_PROBE),) endif endif endif +endif $(OUTPUT)/%.o: %.c $(CLANG) $(CLANG_FLAGS) \
Signature · 1 line
2.17.1
PATCH v3 11/13 tools/bpf: refactor to implement btf_get_from_id() in lib/bpf
The function get_btf() is implemented in tools/bpf/bpftool/map.c to get a btf structure given a map_info. This patch refactored this function to be function btf_get_from_id() in tools/lib/bpf so that it can be used later.
--- tools/bpf/bpftool/map.c | 68 ++-------------------------------------- tools/lib/bpf/btf.c | 69 +++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 18 ++++++----- 3 files changed, 81 insertions(+), 74 deletions(-)
tools/bpf/bpftool/map.c +2 -66
@@ -215,70 +215,6 @@ static int do_dump_btf(const struct btf_dumper *d,return ret; } -static int get_btf (struct bpf_map_info *map_info,struct btf **btf) -{ -struct bpf_btf_info btf_info = {0 }; -__u32 len =sizeof (btf_info); -__u32 last_size; -int btf_fd; -void *ptr; -int err; - - err =0 ; - *btf =NULL ; - btf_fd =bpf_btf_get_fd_by_id (map_info->btf_id); -if (btf_fd <0 ) -return 0 ; - -/* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so -* let's start with a sane default - 4KiB here - and resize it only if -* bpf_obj_get_info_by_fd() needs a bigger buffer. -*/ - btf_info.btf_size =4096 ; - last_size = btf_info.btf_size; - ptr =malloc (last_size); -if (!ptr) { - err = -ENOMEM ; - goto exit_free; - } - -bzero (ptr, last_size); - btf_info.btf =ptr_to_u64 (ptr); - err =bpf_obj_get_info_by_fd (btf_fd, &btf_info, &len); - -if (!err && btf_info.btf_size > last_size) { -void *temp_ptr; - - last_size = btf_info.btf_size; - temp_ptr =realloc (ptr, last_size); -if (!temp_ptr) { - err = -ENOMEM ; - goto exit_free; - } - ptr = temp_ptr; -bzero (ptr, last_size); - btf_info.btf =ptr_to_u64 (ptr); - err =bpf_obj_get_info_by_fd (btf_fd, &btf_info, &len); - } - -if (err || btf_info.btf_size > last_size) { - err = errno; - goto exit_free; - } - - *btf =btf__new ((__u8 *)btf_info.btf, btf_info.btf_size,NULL ); -if (IS_ERR (*btf)) { - err =PTR_ERR (*btf); - *btf =NULL ; - } - -exit_free: -close (btf_fd); -free (ptr); - -return err; -} -static json_writer_t *get_btf_writer (void ) {json_writer_t *jw =jsonw_new (stdout);
@@ -765,7 +701,7 @@ static int do_dump(int argc, char **argv) prev_key =NULL ; - err =get_btf (&info, &btf); + err =btf_get_from_id (info.btf_id, &btf);if (err) {p_err ("failed to get btf" ); goto exit_free;
@@ -909,7 +845,7 @@ static int do_lookup(int argc, char **argv) }/* here means bpf_map_lookup_elem() succeeded */ - err =get_btf (&info, &btf); + err =btf_get_from_id (info.btf_id, &btf);if (err) {p_err ("failed to get btf" ); goto exit_free;
tools/lib/bpf/btf.c +69 -0
81 lines not on this page · raw message
tools/lib/bpf/btf.h +10 -8
@@ -67,14 +67,16 @@ LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__fd(const struct btf *btf); LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); +LIBBPF_API int btf_get_from_id(__u32 id, struct btf **btf); -struct btf_ext *btf_ext__new (__u8 *data,__u32 size,btf_print_fn_t err_log); -void btf_ext__free (struct btf_ext *btf_ext); -int btf_ext_reloc_init (struct btf *btf,struct btf_ext *btf_ext, -const char *sec_name,__u32 *btf_fd, -void **func_info,__u32 *func_info_len); -int btf_ext_reloc (struct btf *btf,struct btf_ext *btf_ext, -const char *sec_name,__u32 insns_cnt,void **func_info, -__u32 *func_info_len); +LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size, +btf_print_fn_t err_log); +LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext); +LIBBPF_API int btf_ext_reloc_init(struct btf *btf, struct btf_ext *btf_ext, +const char *sec_name,__u32 *btf_fd, +void **func_info,__u32 *func_info_len); +LIBBPF_API int btf_ext_reloc(struct btf *btf, struct btf_ext *btf_ext, +const char *sec_name,__u32 insns_cnt, +void **func_info,__u32 *func_info_len);#endif /* __LIBBPF_BTF_H */
Signature · 1 line
2.17.1
PATCH v3 12/13 tools/bpf: enhance test_btf file testing to test func info
In reply to Yonghong SongChange the bpf programs test_btf_haskv.c and test_btf_nokv.c to have two sections, and enhance test_btf.c test_file feature to test btf func_info returned by the kernel.
--- tools/testing/selftests/bpf/test_btf.c | 87 ++++++++++++++++++-- tools/testing/selftests/bpf/test_btf_haskv.c | 16 +++- tools/testing/selftests/bpf/test_btf_nokv.c | 16 +++- 3 files changed, 106 insertions(+), 13 deletions(-)
tools/testing/selftests/bpf/test_btf.c +78 -9
@@ -2180,13 +2180,13 @@ static struct btf_file_test file_tests[] = { }, }; -static int file_has_btf_elf (const char *fn) +static int file_has_btf_elf (const char *fn,bool *has_btf_ext) {Elf_Scn *scn =NULL ;GElf_Ehdr ehdr; +int ret =0 ;int elf_fd;Elf *elf; -int ret;if (CHECK (elf_version (EV_CURRENT ) ==EV_NONE ,"elf_version(EV_CURRENT) == EV_NONE" ))
@@ -2218,14 +2218,12 @@ static int file_has_btf_elf(const char *fn) } sh_name =elf_strptr (elf, ehdr.e_shstrndx, sh.sh_name); -if (!strcmp (sh_name,BTF_ELF_SEC )) { +if (!strcmp (sh_name,BTF_ELF_SEC )) ret =1 ; - goto done; - } +if (!strcmp (sh_name,BTF_EXT_ELF_SEC )) + *has_btf_ext = true; } - ret =0 ; - done:close (elf_fd);elf_end (elf);
@@ -2235,15 +2233,22 @@ static int file_has_btf_elf(const char *fn)static int do_test_file (unsigned int test_num) {const struct btf_file_test *test = &file_tests[test_num -1 ]; +const char *expected_fnames[] = {"_dummy_tracepoint" , +"test_long_fname_1" , +"test_long_fname_2" }; +__u32 func_lens[10 ], func_types[10 ], info_len; +struct bpf_prog_info info = {};struct bpf_object *obj =NULL ;struct bpf_program *prog; +bool has_btf_ext = false; +struct btf *btf =NULL ;struct bpf_map *map; -int err; +int i, err, prog_fd;fprintf (stderr,"BTF libbpf test[%u] (%s): " , test_num, test->file); - err =file_has_btf_elf (test->file); + err =file_has_btf_elf (test->file, &has_btf_ext);if (err ==-1 )return err;
@@ -2271,6 +2276,7 @@ static int do_test_file(unsigned int test_num) err =bpf_object__load (obj);if (CHECK (err <0 ,"bpf_object__load: %d" , err)) goto done; + prog_fd =bpf_program__fd (prog); map =bpf_object__find_map_by_name (obj,"btf_map" );if (CHECK (!map,"btf_map not found" )) {
@@ -2285,6 +2291,69 @@ static int do_test_file(unsigned int test_num) test->btf_kv_notfound)) goto done; +if (!jit_enabled || !has_btf_ext) + goto skip_jit; + + info_len =sizeof (struct bpf_prog_info ); + info.nr_jited_func_types =ARRAY_SIZE (func_types); + info.nr_jited_func_lens =ARRAY_SIZE (func_lens); + info.jited_func_types =ptr_to_u64 (&func_types[0 ]); + info.jited_func_lens =ptr_to_u64 (&func_lens[0 ]); + + err =bpf_obj_get_info_by_fd (prog_fd, &info, &info_len); + +if (CHECK (err ==-1 ,"invalid get info errno:%d" , errno)) { +fprintf (stderr,"%s\n" , btf_log_buf); + err =-1 ; + goto done; + } +if (CHECK (info.nr_jited_func_lens !=3 , +"incorrect info.nr_jited_func_lens %d" , + info.nr_jited_func_lens)) { + err =-1 ; + goto done; + } +if (CHECK (info.nr_jited_func_types !=3 , +"incorrect info.nr_jited_func_types %d" , + info.nr_jited_func_types)) { + err =-1 ; + goto done; + } +if (CHECK (info.btf_id ==0 ,"incorrect btf_id = 0" )) { + err =-1 ; + goto done; + } + + err =btf_get_from_id (info.btf_id, &btf); +if (CHECK (err,"cannot get btf from kernel, err: %d" , err)) + goto done; + +/* check three functions */ +for (i =0 ; i <3 ; i++) { +const struct btf_type *t; +const char *fname; + + t =btf__type_by_id (btf, func_types[i]); +if (CHECK (!t,"btf__type_by_id failure: id %u" , + func_types[i])) { + err =-1 ; + goto done; + } + + fname =btf__name_by_offset (btf, t->name_off); + err =strcmp (fname, expected_fnames[i]); +/* for the second and third functions in .text section, +* the compiler may order them either way. +*/ +if (i && err) + err =strcmp (fname, expected_fnames[3 - i]); +if (CHECK (err,"incorrect fname %s" , fname ? :"" )) { + err =-1 ; + goto done; + } + } + +skip_jit:fprintf (stderr,"OK" ); done:
tools/testing/selftests/bpf/test_btf_haskv.c +14 -2
@@ -24,8 +24,8 @@ struct dummy_tracepoint_args {struct sock *sock; }; -SEC("dummy_tracepoint") -int _dummy_tracepoint (struct dummy_tracepoint_args *arg) +__attribute__((noinline)) +static int test_long_fname_2 (struct dummy_tracepoint_args *arg) {struct ipv_counts *counts;int key =0 ;
@@ -42,4 +42,16 @@ int _dummy_tracepoint(struct dummy_tracepoint_args *arg)return 0 ; } +__attribute__((noinline)) +static int test_long_fname_1 (struct dummy_tracepoint_args *arg) +{ +return test_long_fname_2 (arg); +} + +SEC("dummy_tracepoint") +int _dummy_tracepoint (struct dummy_tracepoint_args *arg) +{ +return test_long_fname_1 (arg); +} + char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_btf_nokv.c +14 -2
@@ -22,8 +22,8 @@ struct dummy_tracepoint_args {struct sock *sock; }; -SEC("dummy_tracepoint") -int _dummy_tracepoint (struct dummy_tracepoint_args *arg) +__attribute__((noinline)) +static int test_long_fname_2 (struct dummy_tracepoint_args *arg) {struct ipv_counts *counts;int key =0 ;
@@ -40,4 +40,16 @@ int _dummy_tracepoint(struct dummy_tracepoint_args *arg)return 0 ; } +__attribute__((noinline)) +static int test_long_fname_1 (struct dummy_tracepoint_args *arg) +{ +return test_long_fname_2 (arg); +} + +SEC("dummy_tracepoint") +int _dummy_tracepoint (struct dummy_tracepoint_args *arg) +{ +return test_long_fname_1 (arg); +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.17.1
PATCH v3 13/13 tools/bpf: bpftool: add support for jited func types
In reply to Yonghong SongThis patch added support to print function signature if btf func_info is available. Note that ksym now uses function name instead of prog_name as prog_name has a limit of 16 bytes including ending '\0'.
The following is a sample output for selftests test_btf with file test_btf_haskv.o:
$ bpftool prog dump jited id 1
int _dummy_tracepoint(struct dummy_tracepoint_args * ):
bpf_prog_b07ccb89267cf242__dummy_tracepoint:
0: push %rbp
1: mov %rsp,%rbp
......
3c: add $0x28,%rbp
40: leaveq
41: retq int test_long_fname_1(struct dummy_tracepoint_args * ):
bpf_prog_2dcecc18072623fc_test_long_fname_1:
0: push %rbp
1: mov %rsp,%rbp
......
3a: add $0x28,%rbp
3e: leaveq
3f: retq int test_long_fname_2(struct dummy_tracepoint_args * ):
bpf_prog_89d64e4abf0f0126_test_long_fname_2:
0: push %rbp
1: mov %rsp,%rbp
......
80: add $0x28,%rbp
84: leaveq
85: retq--- tools/bpf/bpftool/btf_dumper.c | 90 ++++++++++++++++++++++++++++++++++ tools/bpf/bpftool/main.h | 2 + tools/bpf/bpftool/prog.c | 54 ++++++++++++++++++++ 3 files changed, 146 insertions(+)
tools/bpf/bpftool/btf_dumper.c +90 -0
@@ -249,3 +249,93 @@ int btf_dumper_type(const struct btf_dumper *d, __u32 type_id, {return btf_dumper_do_type (d, type_id,0 , data); } + +#define BTF_PRINT_ARG (...) \ + do { \ + pos += snprintf(func_sig + pos, size - pos, \ + __VA_ARGS__); \ + if (pos >= size) \ + return -1; \ + } while (0) +#define BTF_PRINT_TYPE (type) \ + do { \ + pos = __btf_dumper_type_only(btf, type, func_sig, \ + pos, size); \ + if (pos == -1) \ + return -1; \ + } while (0) + +static int __btf_dumper_type_only (struct btf *btf,__u32 type_id, +char *func_sig,int pos,int size) +{ +const struct btf_type *t =btf__type_by_id (btf, type_id); +const struct btf_array *array; +int i, vlen; + +switch (BTF_INFO_KIND (t->info)) { +case BTF_KIND_INT : +BTF_PRINT_ARG ("%s " ,btf__name_by_offset (btf, t->name_off)); +break ; +case BTF_KIND_STRUCT : +BTF_PRINT_ARG ("struct %s " , +btf__name_by_offset (btf, t->name_off)); +break ; +case BTF_KIND_UNION : +BTF_PRINT_ARG ("union %s " , +btf__name_by_offset (btf, t->name_off)); +break ; +case BTF_KIND_ENUM : +BTF_PRINT_ARG ("enum %s " , +btf__name_by_offset (btf, t->name_off)); +break ; +case BTF_KIND_ARRAY : + array = (struct btf_array *)(t +1 ); +BTF_PRINT_TYPE (array->type); +BTF_PRINT_ARG ("[%d]" , array->nelems); +break ; +case BTF_KIND_PTR : +BTF_PRINT_TYPE (t->type); +BTF_PRINT_ARG ("* " ); +break ; +case BTF_KIND_UNKN : +case BTF_KIND_FWD : +case BTF_KIND_TYPEDEF : +return -1 ; +case BTF_KIND_VOLATILE : +BTF_PRINT_ARG ("volatile " ); +BTF_PRINT_TYPE (t->type); +break ; +case BTF_KIND_CONST : +BTF_PRINT_ARG ("const " ); +BTF_PRINT_TYPE (t->type); +break ; +case BTF_KIND_RESTRICT : +BTF_PRINT_ARG ("restrict " ); +BTF_PRINT_TYPE (t->type); +break ; +case BTF_KIND_FUNC : +case BTF_KIND_FUNC_PROTO : +BTF_PRINT_TYPE (t->type); +BTF_PRINT_ARG ("%s(" ,btf__name_by_offset (btf, t->name_off)); + vlen =BTF_INFO_VLEN (t->info); +for (i =0 ; i < vlen; i++) { +__u32 arg_type = ((__u32 *)(t +1 ))[i]; + +if (i) +BTF_PRINT_ARG (", " ); +BTF_PRINT_TYPE (arg_type); + } +BTF_PRINT_ARG (")" ); +break ; +default : +return -1 ; + } + +return pos; +} + +int btf_dumper_type_only (struct btf *btf,__u32 type_id,char *func_sig, +int size) +{ +return __btf_dumper_type_only (btf, type_id, func_sig,0 , size); +}
tools/bpf/bpftool/main.h +2 -0
@@ -168,6 +168,8 @@ struct btf_dumper {*/ int btf_dumper_type (const struct btf_dumper *d,__u32 type_id,const void *data); +int btf_dumper_type_only (struct btf *btf,__u32 func_type_id,char *func_only, +int size);struct nlattr ;struct ifinfomsg ;
tools/bpf/bpftool/prog.c +54 -0
130 lines not on this page · raw message
Signature · 1 line
2.17.1