Fix various issues with 32-bit libbpf
Subsystems (7)
v3 cover Fix various issues with 32-bit libbpf
This patch set contains fixes to libbpf, bpftool, and selftests that were found while testing libbpf and selftests built in 32-bit mode. 64-bit nature of BPF target and 32-bit host environment don't always mix together well without extra care, so there were a bunch of problems discovered and fixed.
Each individual patch contains additional explanations, where necessary.
v2->v3:
- don't give up if failed to determine ELF class;v1->v2:
- guess_ptr_sz -> determine_ptr_sz as per Alexei;
- added pointer size determination by ELF class.
Andrii Nakryiko (9):
tools/bpftool: fix compilation warnings in 32-bit mode selftest/bpf: fix compilation warnings in 32-bit mode
libbpf: fix BTF-defined map-in-map initialization on 32-bit host
arches
libbpf: handle BTF pointer sizes more carefully selftests/bpf: fix btf_dump test cases on 32-bit arches
libbpf: enforce 64-bitness of BTF for BPF object files selftests/bpf: correct various core_reloc 64-bit assumptions tools/bpftool: generate data section struct with conservative
alignment
selftests/bpf: make test_varlen work with 32-bit user-space arch
tools/bpf/bpftool/btf_dumper.c | 2 +- tools/bpf/bpftool/gen.c | 14 ++++ tools/bpf/bpftool/link.c | 4 +- tools/bpf/bpftool/main.h | 10 ++- tools/bpf/bpftool/prog.c | 16 ++-- tools/lib/bpf/btf.c | 83 ++++++++++++++++++- tools/lib/bpf/btf.h | 2 + tools/lib/bpf/btf_dump.c | 4 +- tools/lib/bpf/libbpf.c | 20 +++-- tools/lib/bpf/libbpf.map | 2 + .../selftests/bpf/prog_tests/bpf_obj_id.c | 8 +- .../selftests/bpf/prog_tests/btf_dump.c | 27 ++++-- .../selftests/bpf/prog_tests/core_extern.c | 4 +- .../selftests/bpf/prog_tests/core_reloc.c | 20 ++--- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 6 +- .../selftests/bpf/prog_tests/flow_dissector.c | 2 +- .../selftests/bpf/prog_tests/global_data.c | 6 +- .../selftests/bpf/prog_tests/prog_run_xattr.c | 2 +- .../selftests/bpf/prog_tests/skb_ctx.c | 2 +- .../testing/selftests/bpf/prog_tests/varlen.c | 8 +- .../selftests/bpf/progs/core_reloc_types.h | 69 ++++++++------- .../testing/selftests/bpf/progs/test_varlen.c | 6 +- tools/testing/selftests/bpf/test_btf.c | 8 +- tools/testing/selftests/bpf/test_progs.h | 5 ++ 24 files changed, 233 insertions(+), 97 deletions(-)
Signature · 1 line
2.24.1
PATCH v3 1/9 tools/bpftool: fix compilation warnings in 32-bit mode
Fix few compilation warnings in bpftool when compiling in 32-bit mode. Abstract away u64 to pointer conversion into a helper function.
--- tools/bpf/bpftool/btf_dumper.c | 2 +- tools/bpf/bpftool/link.c | 4 ++-- tools/bpf/bpftool/main.h | 10 +++++++++- tools/bpf/bpftool/prog.c | 16 ++++++++-------- 4 files changed, 20 insertions(+), 12 deletions(-)
tools/bpf/bpftool/btf_dumper.c +1 -1
@@ -67,7 +67,7 @@ static int dump_prog_id_as_func_ptr(const struct btf_dumper *d,if (!info->btf_id || !info->nr_func_info ||btf__get_from_id (info->btf_id, &prog_btf)) goto print; - finfo = (struct bpf_func_info *)info->func_info; + finfo =u64_to_ptr (info->func_info); func_type =btf__type_by_id (prog_btf, finfo->type_id);if (!func_type || !btf_is_func (func_type)) goto print;
tools/bpf/bpftool/link.c +2 -2
@@ -106,7 +106,7 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)switch (info->type) {case BPF_LINK_TYPE_RAW_TRACEPOINT :jsonw_string_field (json_wtr,"tp_name" , - (const char *)info->raw_tracepoint.tp_name); +u64_to_ptr (info->raw_tracepoint.tp_name));break ;case BPF_LINK_TYPE_TRACING : err =get_prog_info (info->prog_id, &prog_info);
@@ -185,7 +185,7 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)switch (info->type) {case BPF_LINK_TYPE_RAW_TRACEPOINT :printf ("\n\ttp '%s' " , - (const char *)info->raw_tracepoint.tp_name); + (const char *)u64_to_ptr (info->raw_tracepoint.tp_name));break ;case BPF_LINK_TYPE_TRACING : err =get_prog_info (info->prog_id, &prog_info);
tools/bpf/bpftool/main.h +9 -1
@@ -21,7 +21,15 @@/* Make sure we do not use kernel-only integer typedefs */ #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 -#define ptr_to_u64 (ptr) ((__u64)(unsigned long)(ptr)) +static inline __u64 ptr_to_u64 (const void *ptr) +{ +return (__u64 )(unsigned long )ptr; +} + +static inline void *u64_to_ptr (__u64 ptr) +{ +return (void *)(unsigned long )ptr; +}#define NEXT_ARG () ({ argc--; argv++; if (argc < 0) usage(); })#define NEXT_ARGP () ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
tools/bpf/bpftool/prog.c +8 -8
@@ -428,14 +428,14 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,p_info ("no instructions returned" );return -1 ; } - buf = (unsigned char *)(info->jited_prog_insns); + buf =u64_to_ptr (info->jited_prog_insns); member_len = info->jited_prog_len; }else {/* DUMP_XLATED */ if (info->xlated_prog_len ==0 || !info->xlated_prog_insns) {p_err ("error retrieving insn dump: kernel.kptr_restrict set?" );return -1 ; } - buf = (unsigned char *)info->xlated_prog_insns; + buf =u64_to_ptr (info->xlated_prog_insns); member_len = info->xlated_prog_len; }
@@ -444,7 +444,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,return -1 ; } - func_info = (void *)info->func_info; + func_info =u64_to_ptr (info->func_info);if (info->nr_line_info) { prog_linfo =bpf_prog_linfo__new (info);
@@ -462,7 +462,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, n =write (fd, buf, member_len);close (fd); -if (n != member_len) { +if (n != (ssize_t )member_len) {p_err ("error writing output file: %s" , n <0 ?strerror (errno) :"short write" );return -1 ;
@@ -492,13 +492,13 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,__u32 i;if (info->nr_jited_ksyms) {kernel_syms_load (&dd); - ksyms = (__u64 *) info->jited_ksyms; + ksyms =u64_to_ptr (info->jited_ksyms); }if (json_output)jsonw_start_array (json_wtr); - lens = (__u32 *) info->jited_func_lens; + lens =u64_to_ptr (info->jited_func_lens);for (i =0 ; i < info->nr_jited_func_lens; i++) {if (ksyms) { sym =kernel_syms_search (&dd, ksyms[i]);
@@ -559,7 +559,7 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode, }else {kernel_syms_load (&dd); dd.nr_jited_ksyms = info->nr_jited_ksyms; - dd.jited_ksyms = (__u64 *) info->jited_ksyms; + dd.jited_ksyms =u64_to_ptr (info->jited_ksyms); dd.btf = btf; dd.func_info = func_info; dd.finfo_rec_size = info->func_info_rec_size;
@@ -1681,7 +1681,7 @@ static char *profile_target_name(int tgt_fd) goto out; } - func_info = (struct bpf_func_info *)(info_linear->info.func_info); + func_info =u64_to_ptr (info_linear->info.func_info); t =btf__type_by_id (btf, func_info[0 ].type_id);if (!t) { p_err("btf %d doesn't have type %d",
Signature · 1 line
2.24.1
PATCH v3 2/9 selftest/bpf: fix compilation warnings in 32-bit mode
In reply to Andrii NakryikoFix compilation warnings emitted when compiling selftests for 32-bit platform (x86 in my case).
--- tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c | 8 ++++---- tools/testing/selftests/bpf/prog_tests/core_extern.c | 4 ++-- tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c | 6 +++--- tools/testing/selftests/bpf/prog_tests/flow_dissector.c | 2 +- tools/testing/selftests/bpf/prog_tests/global_data.c | 6 +++--- tools/testing/selftests/bpf/prog_tests/prog_run_xattr.c | 2 +- tools/testing/selftests/bpf/prog_tests/skb_ctx.c | 2 +- tools/testing/selftests/bpf/test_btf.c | 8 ++++---- tools/testing/selftests/bpf/test_progs.h | 5 +++++ 9 files changed, 24 insertions(+), 19 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c +4 -4
@@ -159,15 +159,15 @@ void test_bpf_obj_id(void)/* Check getting link info */ info_len =sizeof (struct bpf_link_info ) *2 ;bzero (&link_infos[i], info_len); - link_infos[i].raw_tracepoint.tp_name = (__u64)&tp_name; + link_infos[i].raw_tracepoint.tp_name =ptr_to_u64 (&tp_name); link_infos[i].raw_tracepoint.tp_name_len =sizeof (tp_name); err =bpf_obj_get_info_by_fd (bpf_link__fd (links[i]), &link_infos[i], &info_len); if (CHECK(err || link_infos[i].type != BPF_LINK_TYPE_RAW_TRACEPOINT || link_infos[i].prog_id != prog_infos[i].id || - link_infos[i].raw_tracepoint.tp_name != (__u64)&tp_name || - strcmp((char *)link_infos[i].raw_tracepoint.tp_name, + link_infos[i].raw_tracepoint.tp_name != ptr_to_u64(&tp_name) || + strcmp(u64_to_ptr(link_infos[i].raw_tracepoint.tp_name), "sys_enter") || info_len != sizeof(struct bpf_link_info), "get-link-info(fd)",
@@ -178,7 +178,7 @@ void test_bpf_obj_id(void) link_infos[i].type,BPF_LINK_TYPE_RAW_TRACEPOINT , link_infos[i].id, link_infos[i].prog_id, prog_infos[i].id, - (char *)link_infos[i].raw_tracepoint.tp_name, + (const char *)u64_to_ptr (link_infos[i].raw_tracepoint.tp_name), "sys_enter")) goto done;
tools/testing/selftests/bpf/prog_tests/core_extern.c +2 -2
@@ -159,8 +159,8 @@ void test_core_extern(void) exp = (uint64_t *)&t->data;for (j =0 ; j < n; j++) {CHECK (got[j] != exp[j],"check_res" , -"result #%d: expected %lx, but got %lx\n" , - j, exp[j], got[j]); +"result #%d: expected %llx, but got %llx\n" , + j, (__u64 )exp[j], (__u64 )got[j]); } cleanup:test_core_extern__destroy (skel);
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +3 -3
@@ -16,7 +16,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,__u32 duration =0 , retval;struct bpf_map *data_map;const int zero =0 ; -u64 *result =NULL ; +__u64 *result =NULL ; err =bpf_prog_load (target_obj_file,BPF_PROG_TYPE_UNSPEC , &pkt_obj, &pkt_fd);
@@ -29,7 +29,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, link =calloc (sizeof (struct bpf_link *), prog_cnt); prog =calloc (sizeof (struct bpf_program *), prog_cnt); - result =malloc ((prog_cnt +32 /* spare */ ) *sizeof (u64)); + result =malloc ((prog_cnt +32 /* spare */ ) *sizeof (__u64));if (CHECK (!link || !prog || !result,"alloc_memory" ,"failed to alloc memory" )) goto close_prog;
@@ -72,7 +72,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, goto close_prog;for (i =0 ; i < prog_cnt; i++) -if (CHECK (result[i] !=1 ,"result" ,"fexit_bpf2bpf failed err %ld\n" , +if (CHECK (result[i] !=1 ,"result" ,"fexit_bpf2bpf failed err %llu\n" , result[i])) goto close_prog;
tools/testing/selftests/bpf/prog_tests/flow_dissector.c +1 -1
@@ -591,7 +591,7 @@ void test_flow_dissector(void)CHECK_ATTR (tattr.data_size_out !=sizeof (flow_keys) || err || tattr.retval !=1 , tests[i].name, -"err %d errno %d retval %d duration %d size %u/%lu\n" , +"err %d errno %d retval %d duration %d size %u/%zu\n" , err, errno, tattr.retval, tattr.duration, tattr.data_size_out,sizeof (flow_keys));CHECK_FLOW_KEYS (tests[i].name, flow_keys, tests[i].keys);
tools/testing/selftests/bpf/prog_tests/global_data.c +3 -3
@@ -5,7 +5,7 @@static void test_global_data_number (struct bpf_object *obj,__u32 duration) {int i, err, map_fd; -uint64_t num; +__u64 num; map_fd =bpf_find_map (__func__, obj,"result_number" );if (CHECK_FAIL (map_fd <0 ))
@@ -14,7 +14,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)struct {char *name;uint32_t key; -uint64_t num; +__u64 num; } tests[] = { {"relocate .bss reference" ,0 ,0 }, {"relocate .data reference" ,1 ,42 },
@@ -32,7 +32,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)for (i =0 ; i <sizeof (tests) /sizeof (tests[0 ]); i++) { err =bpf_map_lookup_elem (map_fd, &tests[i].key, &num);CHECK (err || num != tests[i].num, tests[i].name, -"err %d result %lx expected %lx\n" , +"err %d result %llx expected %llx\n" , err, num, tests[i].num); } }
tools/testing/selftests/bpf/prog_tests/prog_run_xattr.c +1 -1
@@ -28,7 +28,7 @@ void test_prog_run_xattr(void) "err %d errno %d retval %d\n", err, errno, tattr.retval);CHECK_ATTR (tattr.data_size_out !=sizeof (pkt_v4),"data_size_out" , -"incorrect output size, want %lu have %u\n" , +"incorrect output size, want %zu have %u\n" ,sizeof (pkt_v4), tattr.data_size_out); CHECK_ATTR(buf[5] != 0, "overflow",
tools/testing/selftests/bpf/prog_tests/skb_ctx.c +1 -1
@@ -81,7 +81,7 @@ void test_skb_ctx(void)CHECK_ATTR (tattr.ctx_size_out !=sizeof (skb),"ctx_size_out" , -"incorrect output size, want %lu have %u\n" , +"incorrect output size, want %zu have %u\n" ,sizeof (skb), tattr.ctx_size_out);for (i =0 ; i <5 ; i++)
tools/testing/selftests/bpf/test_btf.c +4 -4
@@ -3883,7 +3883,7 @@ static int test_big_btf_info(unsigned int test_num) info_garbage.garbage =0 ; err =bpf_obj_get_info_by_fd (btf_fd, info, &info_len);if (CHECK (err || info_len !=sizeof (*info), -"err:%d errno:%d info_len:%u sizeof(*info):%lu" , +"err:%d errno:%d info_len:%u sizeof(*info):%zu" , err, errno, info_len,sizeof (*info))) { err =-1 ; goto done;
@@ -4094,7 +4094,7 @@ static int do_test_get_info(unsigned int test_num)if (CHECK (err || !info.id || info_len !=sizeof (info) || info.btf_size != raw_btf_size || (ret =memcmp (raw_btf, user_btf, expected_nbytes)), -"err:%d errno:%d info.id:%u info_len:%u sizeof(info):%lu raw_btf_size:%u info.btf_size:%u expected_nbytes:%u memcmp:%d" , +"err:%d errno:%d info.id:%u info_len:%u sizeof(info):%zu raw_btf_size:%u info.btf_size:%u expected_nbytes:%u memcmp:%d" , err, errno, info.id, info_len,sizeof (info), raw_btf_size, info.btf_size, expected_nbytes, ret)) { err =-1 ;
@@ -4730,7 +4730,7 @@ ssize_t get_pprint_expected_line(enum pprint_mapv_kind_t mapv_kind, nexpected_line = snprintf(expected_line, line_size, "%s%u: {%u,0,%d,0x%x,0x%x,0x%x," - "{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s," + "{%llu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s," "%u,0x%x,[[%d,%d],[%d,%d]]}\n", percpu_map ? "\tcpu" : "", percpu_map ? cpu : next_key,
@@ -4738,7 +4738,7 @@ ssize_t get_pprint_expected_line(enum pprint_mapv_kind_t mapv_kind, v->unused_bits2a, v->bits28, v->unused_bits2b, - v->ui64, + (__u64 )v->ui64, v->ui8a[0 ], v->ui8a[1 ], v->ui8a[2 ], v->ui8a[3 ], v->ui8a[4], v->ui8a[5],
tools/testing/selftests/bpf/test_progs.h +5 -0
@@ -135,6 +135,11 @@ static inline __u64 ptr_to_u64(const void *ptr)return (__u64 ) (unsigned long ) ptr; } +static inline void *u64_to_ptr (__u64 ptr) +{ +return (void *) (unsigned long ) ptr; +} +int bpf_find_map (const char *test,struct bpf_object *obj,const char *name);int compare_map_keys (int map1_fd,int map2_fd);int compare_stack_ips (int smap_fd,int amap_fd,int stack_trace_len);
Signature · 1 line
2.24.1
PATCH v3 3/9 libbpf: fix BTF-defined map-in-map initialization on 32-bit host arches
In reply to Andrii NakryikoLibbpf built in 32-bit mode should be careful about not conflating 64-bit BPF pointers in BPF ELF file and host architecture pointers. This patch fixes issue of incorrect initializating of map-in-map inner map slots due to such difference.
--- tools/lib/bpf/libbpf.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
tools/lib/bpf/libbpf.c +10 -6
@@ -5194,7 +5194,8 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,static int bpf_object__collect_map_relos (struct bpf_object *obj,GElf_Shdr *shdr,Elf_Data *data) { -int i, j, nrels, new_sz, ptr_sz =sizeof (void *); +const int bpf_ptr_sz =8 , host_ptr_sz =sizeof (void *); +int i, j, nrels, new_sz;const struct btf_var_secinfo *vi =NULL ;const struct btf_type *sec, *var, *def;const struct btf_member *member;
@@ -5243,7 +5244,7 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj, vi =btf_var_secinfos (sec) + map->btf_var_idx;if (vi->offset <= rel.r_offset && - rel.r_offset +sizeof (void *) <= vi->offset + vi->size) + rel.r_offset + bpf_ptr_sz <= vi->offset + vi->size)break ; }if (j == obj->nr_maps) {
@@ -5279,17 +5280,20 @@ static int bpf_object__collect_map_relos(struct bpf_object *obj,return -EINVAL ; moff = rel.r_offset - vi->offset - moff; -if (moff % ptr_sz) +/* here we use BPF pointer size, which is always 64 bit, as we +* are parsing ELF that was built for BPF target +*/ +if (moff % bpf_ptr_sz)return -EINVAL ; - moff /= ptr_sz; + moff /= bpf_ptr_sz;if (moff >= map->init_slots_sz) { new_sz = moff +1 ; - tmp =realloc (map->init_slots, new_sz * ptr_sz); + tmp =realloc (map->init_slots, new_sz * host_ptr_sz);if (!tmp)return -ENOMEM ; map->init_slots = tmp;memset (map->init_slots + map->init_slots_sz,0 , - (new_sz - map->init_slots_sz) * ptr_sz); + (new_sz - map->init_slots_sz) * host_ptr_sz); map->init_slots_sz = new_sz; } map->init_slots[moff] = targ_map;
Signature · 1 line
2.24.1
PATCH v3 4/9 libbpf: handle BTF pointer sizes more carefully
In reply to Andrii NakryikoWith libbpf and BTF it is pretty common to have libbpf built for one architecture, while BTF information was generated for a different architecture (typically, but not always, BPF). In such case, the size of a pointer might differ betweem architectures. libbpf previously was always making an assumption that pointer size for BTF is the same as native architecture pointer size, but that breaks for cases where libbpf is built as 32-bit library, while BTF is for 64-bit architecture.
To solve this, add heuristic to determine pointer size by searching for long or unsigned long integer type and using its size as a pointer size. Also, allow to override the pointer size with a new API btf__set_pointer_size(), for cases where application knows which pointer size should be used. User application can check what libbpf "guessed" by looking at the result of btf__pointer_size(). If it's not 0, then libbpf successfully determined a pointer size, otherwise native arch pointer size will be used.
For cases where BTF is parsed from ELF file, use ELF's class (32-bit or 64-bit) to determine pointer size.
--- tools/lib/bpf/btf.c | 83 ++++++++++++++++++++++++++++++++++++++-- tools/lib/bpf/btf.h | 2 + tools/lib/bpf/btf_dump.c | 4 +- tools/lib/bpf/libbpf.map | 2 + 4 files changed, 87 insertions(+), 4 deletions(-)
tools/lib/bpf/btf.c +80 -3
@@ -41,6 +41,7 @@ struct btf {__u32 types_size;__u32 data_size;int fd; +int ptr_sz; };static inline __u64 ptr_to_u64 (const void *ptr)
@@ -221,6 +222,70 @@ const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)return btf->types[type_id]; } +static int determine_ptr_size (const struct btf *btf) +{ +const struct btf_type *t; +const char *name; +int i; + +for (i =1 ; i <= btf->nr_types; i++) { + t =btf__type_by_id (btf, i); +if (!btf_is_int (t)) +continue ; + + name =btf__name_by_offset (btf, t->name_off); +if (!name) +continue ; + +if (strcmp (name,"long int" ) ==0 || +strcmp (name,"long unsigned int" ) ==0 ) { +if (t->size !=4 && t->size !=8 ) +continue ; +return t->size; + } + } + +return -1 ; +} + +static size_t btf_ptr_sz (const struct btf *btf) +{ +if (!btf->ptr_sz) + ((struct btf *)btf)->ptr_sz =determine_ptr_size (btf); +return btf->ptr_sz <0 ?sizeof (void *) : btf->ptr_sz; +} + +/* Return pointer size this BTF instance assumes. The size is heuristically +* determined by looking for 'long' or 'unsigned long' integer type and +* recording its size in bytes. If BTF type information doesn't have any such +* type, this function returns 0. In the latter case, native architecture's +* pointer size is assumed, so will be either 4 or 8, depending on +* architecture that libbpf was compiled for. It's possible to override +* guessed value by using btf__set_pointer_size() API. +*/ +size_t btf__pointer_size (const struct btf *btf) +{ +if (!btf->ptr_sz) + ((struct btf *)btf)->ptr_sz =determine_ptr_size (btf); + +if (btf->ptr_sz <0 ) +/* not enough BTF type info to guess */ +return 0 ; + +return btf->ptr_sz; +} + +/* Override or set pointer size in bytes. Only values of 4 and 8 are +* supported. +*/ +int btf__set_pointer_size (struct btf *btf,size_t ptr_sz) +{ +if (ptr_sz !=4 && ptr_sz !=8 ) +return -EINVAL ; + btf->ptr_sz = ptr_sz; +return 0 ; +} +static bool btf_type_is_void (const struct btf_type *t) {return t == &btf_void ||btf_is_fwd (t);
@@ -253,7 +318,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id) size = t->size; goto done;case BTF_KIND_PTR : - size =sizeof (void *); + size =btf_ptr_sz (btf); goto done;case BTF_KIND_TYPEDEF :case BTF_KIND_VOLATILE :
@@ -293,9 +358,9 @@ int btf__align_of(const struct btf *btf, __u32 id)switch (kind) {case BTF_KIND_INT :case BTF_KIND_ENUM : -return min (sizeof (void *), (size_t )t->size); +return min (btf_ptr_sz (btf), (size_t )t->size);case BTF_KIND_PTR : -return sizeof (void *); +return btf_ptr_sz (btf);case BTF_KIND_TYPEDEF :case BTF_KIND_VOLATILE :case BTF_KIND_CONST :
@@ -533,6 +598,18 @@ struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)if (IS_ERR (btf)) goto done; +switch (gelf_getclass (elf)) { +case ELFCLASS32 : +btf__set_pointer_size (btf,4 ); +break ; +case ELFCLASS64 : +btf__set_pointer_size (btf,8 ); +break ; +default : +pr_warn ("failed to get ELF class (bitness) for %s\n" , path); +break ; + } +if (btf_ext && btf_ext_data) { *btf_ext =btf_ext__new (btf_ext_data->d_buf, btf_ext_data->d_size);
tools/lib/bpf/btf.h +2 -0
@@ -76,6 +76,8 @@ LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf, LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf); LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,__u32 id); +LIBBPF_API size_t btf__pointer_size(const struct btf *btf); +LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz); 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__align_of(const struct btf *btf, __u32 id);
tools/lib/bpf/btf_dump.c +3 -1
@@ -61,6 +61,7 @@ struct btf_dump {const struct btf_ext *btf_ext;btf_dump_printf_fn_t printf_fn;struct btf_dump_opts opts; +int ptr_sz;bool strip_mods;/* per-type auxiliary state */
@@ -139,6 +140,7 @@ struct btf_dump *btf_dump__new(const struct btf *btf, d->btf_ext = btf_ext; d->printf_fn = printf_fn; d->opts.ctx = opts ? opts->ctx :NULL ; + d->ptr_sz =btf__pointer_size (btf) ? :sizeof (void *); d->type_names =hashmap__new (str_hash_fn, str_equal_fn,NULL );if (IS_ERR (d->type_names)) {
@@ -804,7 +806,7 @@ static void btf_dump_emit_bit_padding(const struct btf_dump *d, int align, int lvl) {int off_diff = m_off - cur_off; -int ptr_bits =sizeof (void *) *8 ; +int ptr_bits = d->ptr_sz *8 ;if (off_diff <=0 )/* no gap */
tools/lib/bpf/libbpf.map +2 -0
@@ -295,5 +295,7 @@ LIBBPF_0.1.0 { bpf_program__set_sk_lookup; btf__parse; btf__parse_raw; + btf__pointer_size; btf__set_fd; + btf__set_pointer_size; } LIBBPF_0.0.9;
Signature · 1 line
2.24.1
PATCH v3 5/9 selftests/bpf: fix btf_dump test cases on 32-bit arches
In reply to Andrii NakryikoFix btf_dump test cases by hard-coding BPF's pointer size of 8 bytes for cases where it's impossible to deterimne the pointer size (no long type in BTF). In cases where it's known, validate libbpf correctly determines it as 8.
--- .../selftests/bpf/prog_tests/btf_dump.c | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf_dump.c +20 -7
@@ -12,15 +12,16 @@ void btf_dump_printf(void *ctx, const char *fmt, va_list args)static struct btf_dump_test_case {const char *name;const char *file; +bool known_ptr_sz;struct btf_dump_opts opts; } btf_dump_test_cases[] = { - {"btf_dump: syntax" ,"btf_dump_test_case_syntax" , {}}, - {"btf_dump: ordering" ,"btf_dump_test_case_ordering" , {}}, - {"btf_dump: padding" ,"btf_dump_test_case_padding" , {}}, - {"btf_dump: packing" ,"btf_dump_test_case_packing" , {}}, - {"btf_dump: bitfields" ,"btf_dump_test_case_bitfields" , {}}, - {"btf_dump: multidim" ,"btf_dump_test_case_multidim" , {}}, - {"btf_dump: namespacing" ,"btf_dump_test_case_namespacing" , {}}, + {"btf_dump: syntax" ,"btf_dump_test_case_syntax" , true, {}}, + {"btf_dump: ordering" ,"btf_dump_test_case_ordering" , false, {}}, + {"btf_dump: padding" ,"btf_dump_test_case_padding" , true, {}}, + {"btf_dump: packing" ,"btf_dump_test_case_packing" , true, {}}, + {"btf_dump: bitfields" ,"btf_dump_test_case_bitfields" , true, {}}, + {"btf_dump: multidim" ,"btf_dump_test_case_multidim" , false, {}}, + {"btf_dump: namespacing" ,"btf_dump_test_case_namespacing" , false, {}}, }; static int btf_dump_all_types(const struct btf *btf,
@@ -62,6 +63,18 @@ static int test_btf_dump_case(int n, struct btf_dump_test_case *t) goto done; } +/* tests with t->known_ptr_sz have no "long" or "unsigned long" type, +* so it's impossible to determine correct pointer size; but if they +* do, it should be 8 regardless of host architecture, becaues BPF +* target is always 64-bit +*/ +if (!t->known_ptr_sz) { +btf__set_pointer_size (btf,8 ); + }else { +CHECK (btf__pointer_size (btf) !=8 ,"ptr_sz" ,"exp %d, got %zu\n" , +8 ,btf__pointer_size (btf)); + } +snprintf (out_file,sizeof (out_file),"/tmp/%s.output.XXXXXX" , t->file); fd =mkstemp (out_file);if (CHECK (fd <0 ,"create_tmp" ,"failed to create file: %d\n" , fd)) {
Signature · 1 line
2.24.1
PATCH v3 6/9 libbpf: enforce 64-bitness of BTF for BPF object files
In reply to Andrii NakryikoBPF object files are always targeting 64-bit BPF target architecture, so enforce that at BTF level as well.
--- tools/lib/bpf/libbpf.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/lib/bpf/libbpf.c +4 -0
@@ -2434,6 +2434,8 @@ static int bpf_object__init_btf(struct bpf_object *obj, BTF_ELF_SEC, err); goto out; } +/* enforce 8-byte pointers for BPF-targeted BTFs */ +btf__set_pointer_size (obj->btf,8 ); err =0 ; }if (btf_ext_data) {
@@ -2542,6 +2544,8 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)if (IS_ERR (kern_btf))return PTR_ERR (kern_btf); +/* enforce 8-byte pointers for BPF-targeted BTFs */ +btf__set_pointer_size (obj->btf,8 );bpf_object__sanitize_btf (obj, kern_btf); }
Signature · 1 line
2.24.1
PATCH v3 7/9 selftests/bpf: correct various core_reloc 64-bit assumptions
In reply to Andrii NakryikoEnsure that types are memory layout- and field alignment-compatible regardless of 32/64-bitness mix of libbpf and BPF architecture.
--- .../selftests/bpf/prog_tests/core_reloc.c | 20 +++--- .../selftests/bpf/progs/core_reloc_types.h | 69 ++++++++++--------- 2 files changed, 47 insertions(+), 42 deletions(-)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +10 -10
@@ -237,8 +237,8 @@ .union_sz = sizeof(((type *)0)->union_field), \ .arr_sz = sizeof(((type *)0)->arr_field), \ .arr_elem_sz = sizeof(((type *)0)->arr_field[0]), \ - .ptr_sz = sizeof(((type *)0)->ptr_field), \ - .enum_sz = sizeof(((type *)0)->enum_field), \ + .ptr_sz = 8, /* always 8-byte pointer for BPF */ \ + .enum_sz = sizeof(((type *)0)->enum_field), \ }#define SIZE_CASE (name) { \
@@ -432,20 +432,20 @@ static struct core_reloc_test_case test_cases[] = { .sb4 = -1, .sb20 = -0x17654321, .u32 = 0xBEEF, - .s32 =-0x3FEDCBA987654321 , + .s32 =-0x3FEDCBA987654321LL , }),BITFIELDS_CASE (bitfields___bitfield_vs_int, { - .ub1 = 0xFEDCBA9876543210, + .ub1 = 0xFEDCBA9876543210LL, .ub2 = 0xA6, - .ub7 = -0x7EDCBA987654321, - .sb4 = -0x6123456789ABCDE, - .sb20 = 0xD00D, + .ub7 = -0x7EDCBA987654321LL, + .sb4 = -0x6123456789ABCDELL, + .sb20 = 0xD00DLL, .u32 = -0x76543, - .s32 = 0x0ADEADBEEFBADB0B, + .s32 = 0x0ADEADBEEFBADB0BLL, }),BITFIELDS_CASE (bitfields___just_big_enough, { - .ub1 = 0xF, - .ub2 = 0x0812345678FEDCBA, + .ub1 = 0xFLL, + .ub2 = 0x0812345678FEDCBALL, }), BITFIELDS_ERR_CASE(bitfields___err_too_big_bitfield),
tools/testing/selftests/bpf/progs/core_reloc_types.h +37 -32
158 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH v3 8/9 tools/bpftool: generate data section struct with conservative alignment
In reply to Andrii NakryikoThe comment in the code describes this in good details. Generate such a memory layout that would work both on 32-bit and 64-bit architectures for user-space.
--- tools/bpf/bpftool/gen.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
tools/bpf/bpftool/gen.c +14 -0
@@ -143,6 +143,20 @@ static int codegen_datasec_def(struct bpf_object *obj, var_name, align);return -EINVAL ; } +/* Assume 32-bit architectures when generating data section +* struct memory layout. Given bpftool can't know which target +* host architecture it's emitting skeleton for, we need to be +* conservative and assume 32-bit one to ensure enough padding +* bytes are generated for pointer and long types. This will +* still work correctly for 64-bit architectures, because in +* the worst case we'll generate unnecessary padding field, +* which on 64-bit architectures is not strictly necessary and +* would be handled by natural 8-byte alignment. But it still +* will be a correct memory layout, based on recorded offsets +* in BTF. +*/ +if (align >4 ) + align =4 ; align_off = (off + align -1 ) / align * align;if (align_off != need_off) {
Signature · 1 line
2.24.1
PATCH v3 9/9 selftests/bpf: make test_varlen work with 32-bit user-space arch
In reply to Andrii NakryikoDespite bpftool generating data section memory layout that will work for 32-bit architectures on user-space side, BPF programs should be careful to not use ambiguous types like long, which have different size in 32-bit and 64-bit environments. Fix that in test by using __u64 explicitly, which is a recommended approach anyway.
--- tools/testing/selftests/bpf/prog_tests/varlen.c | 8 ++++---- tools/testing/selftests/bpf/progs/test_varlen.c | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/varlen.c +4 -4
@@ -44,25 +44,25 @@ void test_varlen(void)CHECK_VAL (bss->payload1_len2, size2);CHECK_VAL (bss->total1, size1 + size2);CHECK (memcmp (bss->payload1, exp_str, size1 + size2),"content_check" , -"doesn't match!" ); +"doesn't match!\n" );CHECK_VAL (data->payload2_len1, size1);CHECK_VAL (data->payload2_len2, size2);CHECK_VAL (data->total2, size1 + size2);CHECK (memcmp (data->payload2, exp_str, size1 + size2),"content_check" , -"doesn't match!" ); +"doesn't match!\n" );CHECK_VAL (data->payload3_len1, size1);CHECK_VAL (data->payload3_len2, size2);CHECK_VAL (data->total3, size1 + size2);CHECK (memcmp (data->payload3, exp_str, size1 + size2),"content_check" , -"doesn't match!" ); +"doesn't match!\n" );CHECK_VAL (data->payload4_len1, size1);CHECK_VAL (data->payload4_len2, size2);CHECK_VAL (data->total4, size1 + size2);CHECK (memcmp (data->payload4, exp_str, size1 + size2),"content_check" , -"doesn't match!" ); +"doesn't match!\n" ); cleanup:test_varlen__destroy (skel); }
tools/testing/selftests/bpf/progs/test_varlen.c +3 -3
@@ -15,9 +15,9 @@ int test_pid = 0;bool capture = false;/* .bss */ -long payload1_len1 =0 ; -long payload1_len2 =0 ; -long total1 =0 ; +__u64 payload1_len1 =0 ; +__u64 payload1_len2 =0 ; +__u64 total1 =0 ;char payload1[MAX_LEN +MAX_LEN ] = {};/* .data */
Signature · 1 line
2.24.1
Quoted 5 lines · Andrii Nakryiko
This patch set contains fixes to libbpf, bpftool, and selftests that were found while testing libbpf and selftests built in 32-bit mode. 64-bit nature of BPF target and 32-bit host environment don't always mix together well without extra care, so there were a bunch of problems discovered and fixed.
Quoted 7 lines
Each individual patch contains additional explanations, where necessary.
v2->v3:
- don't give up if failed to determine ELF class;v1->v2:
-guess_ptr_sz->determine_ptr_szas per Alexei;
- added pointer size determination by ELF class.
lgtm
Applied, Thanks