libbpf: add support for kernel module BTF CO-RE relocations
Subsystems (10)
Andrii Nakryiko
maintainerv2 cover libbpf: add support for kernel module BTF CO-RE relocations
Implement libbpf support for performing CO-RE relocations against types in kernel module BTFs, in addition to existing vmlinux BTF support.
This is a first step towards fully supporting kernel module BTFs. Subsequent patch sets will expand kernel and libbpf sides to allow using other BTF-powered capabilities (fentry/fexit, struct_ops, ksym externs, etc). For CO-RE relocations support, though, no extra kernel changes are necessary.
This patch set also sets up a convenient and fully-controlled custom kernel module (called "bpf_testmod"), that is a predictable playground for all the BPF selftests, that rely on module BTFs.
v1->v2:
- module_put() inside preempt_disable() region (Alexei);
- bpf_sidecar -> bpf_testmod rename (Alexei);
- test_progs more relaxed handling of bpf_testmod;
- test_progs marks skipped sub-tests properly as SKIP now.
Andrii Nakryiko (7):
bpf: fix bpf_put_raw_tracepoint()'s use of __module_address()
libbpf: add internal helper to load BTF data by FD
libbpf: refactor CO-RE relocs to not assume a single BTF object
libbpf: add kernel module BTF support for CO-RE relocations selftests/bpf: add bpf_testmod kernel module for testing selftests/bpf: add support for marking sub-tests as skipped selftests/bpf: add CO-RE relocs selftest relying on kernel module BTF
kernel/trace/bpf_trace.c | 8 +- tools/lib/bpf/btf.c | 61 ++-- tools/lib/bpf/libbpf.c | 345 ++++++++++++++---- tools/lib/bpf/libbpf_internal.h | 1 + tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 12 +- .../selftests/bpf/bpf_testmod/.gitignore | 6 + .../selftests/bpf/bpf_testmod/Makefile | 20 + .../bpf/bpf_testmod/bpf_testmod-events.h | 36 ++ .../selftests/bpf/bpf_testmod/bpf_testmod.c | 51 +++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 14 + .../selftests/bpf/prog_tests/core_reloc.c | 79 +++- .../selftests/bpf/progs/core_reloc_types.h | 17 + .../bpf/progs/test_core_reloc_module.c | 66 ++++ tools/testing/selftests/bpf/test_progs.c | 65 +++- tools/testing/selftests/bpf/test_progs.h | 1 + 16 files changed, 661 insertions(+), 122 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_testmod/.gitignore create mode 100644 tools/testing/selftests/bpf/bpf_testmod/Makefile create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_module.c
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v2 1/7 bpf: fix bpf_put_raw_tracepoint()'s use of __module_address()
__module_address() needs to be called with preemption disabled or with module_mutex taken. preempt_disable() is enough for read-only uses, which is what this fix does. Also, module_put() does internal check for NULL, so drop it as well.
--- kernel/trace/bpf_trace.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
kernel/trace/bpf_trace.c +5 -3
@@ -2060,10 +2060,12 @@ struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)void bpf_put_raw_tracepoint (struct bpf_raw_event_map *btp) { -struct module *mod =__module_address ((unsigned long )btp); +struct module *mod; -if (mod) -module_put (mod); +preempt_disable (); + mod =__module_address ((unsigned long )btp); +module_put (mod); +preempt_enable (); } static __always_inline
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v2 2/7 libbpf: add internal helper to load BTF data by FD
In reply to Andrii NakryikoAdd a btf_get_from_fd() helper, which constructs struct btf from in-kernel BTF data by FD. This is used for loading module BTFs.
--- tools/lib/bpf/btf.c | 61 +++++++++++++++++++-------------- tools/lib/bpf/libbpf_internal.h | 1 + 2 files changed, 36 insertions(+), 26 deletions(-)
tools/lib/bpf/btf.c +35 -26
@@ -1318,35 +1318,27 @@ const char *btf__name_by_offset(const struct btf *btf, __u32 offset)return btf__str_by_offset (btf, offset); } -int btf__get_from_id (__u32 id,struct btf **btf) +struct btf *btf_get_from_fd (int btf_fd,struct btf *base_btf) { -struct bpf_btf_info btf_info = {0 }; +struct bpf_btf_info btf_info;__u32 len =sizeof (btf_info);__u32 last_size; -int btf_fd; +struct btf *btf;void *ptr;int err; - err =0 ; - *btf =NULL ; - btf_fd =bpf_btf_get_fd_by_id (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; + last_size =4096 ; ptr =malloc (last_size); -if (!ptr) { - err = -ENOMEM ; - goto exit_free; - } +if (!ptr) +return ERR_PTR (-ENOMEM ); -memset (ptr,0 , last_size); +memset (&btf_info,0 ,sizeof (btf_info)); btf_info.btf =ptr_to_u64 (ptr); + btf_info.btf_size = last_size; err =bpf_obj_get_info_by_fd (btf_fd, &btf_info, &len);if (!err && btf_info.btf_size > last_size) {
@@ -1355,31 +1347,48 @@ int btf__get_from_id(__u32 id, struct btf **btf) last_size = btf_info.btf_size; temp_ptr =realloc (ptr, last_size);if (!temp_ptr) { - err = -ENOMEM ; + btf =ERR_PTR (-ENOMEM ); goto exit_free; } ptr = temp_ptr; -memset (ptr,0 , last_size); + + len =sizeof (btf_info); +memset (&btf_info,0 ,sizeof (btf_info)); btf_info.btf =ptr_to_u64 (ptr); + btf_info.btf_size = last_size; + err =bpf_obj_get_info_by_fd (btf_fd, &btf_info, &len); }if (err || btf_info.btf_size > last_size) { - err = errno; + btf = err ?ERR_PTR (-errno) :ERR_PTR (-E2BIG ); goto exit_free; } - *btf =btf__new ((__u8 *)(long )btf_info.btf, btf_info.btf_size); -if (IS_ERR (*btf)) { - err =PTR_ERR (*btf); - *btf =NULL ; - } + btf =btf_new (ptr, btf_info.btf_size, base_btf); exit_free: -close (btf_fd);free (ptr); +return btf; +} -return err; +int btf__get_from_id (__u32 id,struct btf **btf) +{ +struct btf *res; +int btf_fd; + + *btf =NULL ; + btf_fd =bpf_btf_get_fd_by_id (id); +if (btf_fd <0 ) +return -errno; + + res =btf_get_from_fd (btf_fd,NULL ); +close (btf_fd); +if (IS_ERR (res)) +return PTR_ERR (res); + + *btf = res; +return 0 ; } int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
tools/lib/bpf/libbpf_internal.h +1 -0
@@ -155,6 +155,7 @@ int bpf_object__section_size(const struct bpf_object *obj, const char *name, __u32 *size);int bpf_object__variable_offset (const struct bpf_object *obj,const char *name,__u32 *off); +struct btf *btf_get_from_fd (int btf_fd,struct btf *base_btf);struct btf_ext_info {/*
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v2 4/7 libbpf: add kernel module BTF support for CO-RE relocations
In reply to Andrii NakryikoTeach libbpf to search for candidate types for CO-RE relocations across kernel modules BTFs, in addition to vmlinux BTF. If at least one candidate type is found in vmlinux BTF, kernel module BTFs are not iterated. If vmlinux BTF has no matching candidates, then find all kernel module BTFs and search for all matching candidates across all of them.
Kernel's support for module BTFs are inferred from the support for BTF name pointer in BPF UAPI.
--- tools/lib/bpf/libbpf.c | 178 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 168 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf.c +168 -10
@@ -176,6 +176,8 @@ enum kern_feature_id {FEAT_PROBE_READ_KERN ,/* BPF_PROG_BIND_MAP is supported */ FEAT_PROG_BIND_MAP , +/* Kernel support for module BTFs */ +FEAT_MODULE_BTF , __FEAT_CNT, };
@@ -402,6 +404,12 @@ struct extern_desc {static LIST_HEAD (bpf_objects_list); +struct module_btf { +struct btf *btf; +char *name; +__u32 id; +}; +struct bpf_object {char name[BPF_OBJ_NAME_LEN ];char license[64 ];
@@ -470,6 +478,11 @@ struct bpf_object {struct btf *btf_vmlinux;/* vmlinux BTF override for CO-RE relocations */ struct btf *btf_vmlinux_override; +/* Lazily initialized kernel module BTFs */ +struct module_btf *btf_modules; +bool btf_modules_loaded; +size_t btf_module_cnt; +size_t btf_module_cap;void *priv;bpf_object_clear_priv_t clear_priv;
@@ -3960,6 +3973,35 @@ static int probe_prog_bind_map(void)return ret >=0 ; } +static int probe_module_btf (void ) +{ +static const char strs[] ="\0int" ; +__u32 types[] = { +/* int */ +BTF_TYPE_INT_ENC (1 ,BTF_INT_SIGNED ,0 ,32 ,4 ), + }; +struct bpf_btf_info info; +__u32 len =sizeof (info); +char name[16 ]; +int fd, err; + + fd =libbpf__load_raw_btf ((char *)types,sizeof (types), strs,sizeof (strs)); +if (fd <0 ) +return 0 ;/* BTF not supported at all */ + +memset (&info,0 ,sizeof (info)); + info.name =ptr_to_u64 (name); + info.name_len =sizeof (name); + +/* check that BPF_OBJ_GET_INFO_BY_FD supports specifying name pointer; +* kernel's module BTF support coincides with support for +* name/name_len fields in struct bpf_btf_info. +*/ + err =bpf_obj_get_info_by_fd (fd, &info, &len); +close (fd); +return !err; +} +enum kern_feature_result {FEAT_UNKNOWN =0 ,FEAT_SUPPORTED =1 ,
@@ -4003,7 +4045,10 @@ static struct kern_feature_desc { }, [FEAT_PROG_BIND_MAP ] = {"BPF_PROG_BIND_MAP support" , probe_prog_bind_map, - } + }, + [FEAT_MODULE_BTF ] = { +"module BTF support" , probe_module_btf, + }, };static bool kernel_supports (enum kern_feature_id feat_id)
@@ -4674,13 +4719,95 @@ static int bpf_core_add_cands(struct core_cand *local_cand,return 0 ; } +static int load_module_btfs (struct bpf_object *obj) +{ +struct bpf_btf_info info; +struct module_btf *mod_btf; +struct btf *btf; +char name[64 ]; +__u32 id, len; +int err, fd; + +if (obj->btf_modules_loaded) +return 0 ; + +/* don't do this again, even if we find no module BTFs */ + obj->btf_modules_loaded = true; + +/* kernel too old to support module BTFs */ +if (!kernel_supports (FEAT_MODULE_BTF )) +return 0 ; + +while (true) { + err =bpf_btf_get_next_id (id, &id); +if (err && errno ==ENOENT ) +return 0 ; +if (err) { + err = -errno; +pr_warn ("failed to iterate BTF objects: %d\n" , err); +return err; + } + + fd =bpf_btf_get_fd_by_id (id); +if (fd <0 ) { +if (errno ==ENOENT ) +continue ;/* expected race: BTF was unloaded */ + err = -errno; +pr_warn ("failed to get BTF object #%d FD: %d\n" , id, err); +return err; + } + + len =sizeof (info); +memset (&info,0 ,sizeof (info)); + info.name =ptr_to_u64 (name); + info.name_len =sizeof (name); + + err =bpf_obj_get_info_by_fd (fd, &info, &len); +if (err) { + err = -errno; +pr_warn ("failed to get BTF object #%d info: %d\n" , id, err); +return err; + } + +/* ignore non-module BTFs */ +if (!info.kernel_btf ||strcmp (name,"vmlinux" ) ==0 ) { +close (fd); +continue ; + } + + btf =btf_get_from_fd (fd, obj->btf_vmlinux); +close (fd); +if (IS_ERR (btf)) { +pr_warn ("failed to load module [%s]'s BTF object #%d: %ld\n" , + name, id,PTR_ERR (btf)); +return PTR_ERR (btf); + } + + err =btf_ensure_mem ((void **)&obj->btf_modules, &obj->btf_module_cap, +sizeof (*obj->btf_modules), obj->btf_module_cnt +1 ); +if (err) +return err; + + mod_btf = &obj->btf_modules[obj->btf_module_cnt++]; + + mod_btf->btf = btf; + mod_btf->id = id; + mod_btf->name =strdup (name); +if (!mod_btf->name) +return -ENOMEM ; + } + +return 0 ; +} +static struct core_cand_list *bpf_core_find_cands (struct bpf_object *obj,const struct btf *local_btf,__u32 local_type_id) {struct core_cand local_cand = {};struct core_cand_list *cands; +const struct btf *main_btf;size_t local_essent_len; -int err; +int err, i; local_cand.btf = local_btf; local_cand.t =btf__type_by_id (local_btf, local_type_id);
@@ -4697,15 +4824,38 @@ bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 lreturn ERR_PTR (-ENOMEM );/* Attempt to find target candidates in vmlinux BTF first */ - err =bpf_core_add_cands (&local_cand, local_essent_len, - obj->btf_vmlinux_override ?: obj->btf_vmlinux, -"vmlinux" ,1 , cands); -if (err) { -bpf_core_free_cands (cands); -return ERR_PTR (err); + main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux; + err =bpf_core_add_cands (&local_cand, local_essent_len, main_btf,"vmlinux" ,1 , cands); +if (err) + goto err_out; + +/* if vmlinux BTF has any candidate, don't got for module BTFs */ +if (cands->len) +return cands; + +/* if vmlinux BTF was overridden, don't attempt to load module BTFs */ +if (obj->btf_vmlinux_override) +return cands; + +/* now look through module BTFs, trying to still find candidates */ + err =load_module_btfs (obj); +if (err) + goto err_out; + +for (i =0 ; i < obj->btf_module_cnt; i++) { + err =bpf_core_add_cands (&local_cand, local_essent_len, + obj->btf_modules[i].btf, + obj->btf_modules[i].name, +btf__get_nr_types (obj->btf_vmlinux) +1 , + cands); +if (err) + goto err_out; }return cands; +err_out: +bpf_core_free_cands (cands); +return ERR_PTR (err); }/* Check two types for compatibility for the purpose of field access
@@ -5756,7 +5906,7 @@ static int bpf_core_apply_relo(struct bpf_program *prog,if (!hashmap__find (cand_cache, type_key, (void **)&cands)) { cands =bpf_core_find_cands (prog->obj, local_btf, local_id);if (IS_ERR (cands)) { -pr_warn ("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld" , +pr_warn ("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n" , prog->name, relo_idx, local_id,btf_kind_str (local_type), local_name,PTR_ERR (cands));return PTR_ERR (cands);
@@ -5944,7 +6094,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) } out: -/* obj->btf_vmlinux is freed at the end of object load phase */ +/* obj->btf_vmlinux and module BTFs are freed after object load */ btf__free (obj->btf_vmlinux_override); obj->btf_vmlinux_override =NULL ;
@@ -7303,6 +7453,14 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) err = err ? :bpf_object__relocate (obj, attr->target_btf_path); err = err ? :bpf_object__load_progs (obj, attr->log_level); +/* clean up module BTFs */ +for (i =0 ; i < obj->btf_module_cnt; i++) { +btf__free (obj->btf_modules[i].btf); +free (obj->btf_modules[i].name); + } +free (obj->btf_modules); + +/* clean up vmlinux BTF */ btf__free (obj->btf_vmlinux); obj->btf_vmlinux =NULL ;
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v2 3/7 libbpf: refactor CO-RE relocs to not assume a single BTF object
In reply to Andrii NakryikoRefactor CO-RE relocation candidate search to not expect a single BTF, rather return all candidate types with their corresponding BTF objects. This will allow to extend CO-RE relocations to accommodate kernel module BTFs.
--- tools/lib/bpf/libbpf.c | 187 ++++++++++++++++++++++++----------------- 1 file changed, 111 insertions(+), 76 deletions(-)
tools/lib/bpf/libbpf.c +111 -76
@@ -462,11 +462,14 @@ struct bpf_object {struct list_head list;struct btf *btf; +struct btf_ext *btf_ext; +/* Parse and load BTF vmlinux if any of the programs in the object need * it at load time. */ struct btf *btf_vmlinux; -struct btf_ext *btf_ext; +/* vmlinux BTF override for CO-RE relocations */ +struct btf *btf_vmlinux_override;void *priv;bpf_object_clear_priv_t clear_priv;
@@ -4600,46 +4603,43 @@ static size_t bpf_core_essential_name_len(const char *name)return n; } -/* dynamically sized list of type IDs */ -struct ids_vec { -__u32 *data; +struct core_cand +{ +const struct btf *btf; +const struct btf_type *t; +const char *name; +__u32 id; +}; + +/* dynamically sized list of type IDs and its associated struct btf */ +struct core_cand_list { +struct core_cand *cands;int len; }; -static void bpf_core_free_cands (struct ids_vec *cand_ids) +static void bpf_core_free_cands (struct core_cand_list *cands) { -free (cand_ids->data); -free (cand_ids); +free (cands->cands); +free (cands); } -static struct ids_vec *bpf_core_find_cands (const struct btf *local_btf, -__u32 local_type_id, -const struct btf *targ_btf) +static int bpf_core_add_cands (struct core_cand *local_cand, +size_t local_essent_len, +const struct btf *targ_btf, +const char *targ_btf_name, +int targ_start_id, +struct core_cand_list *cands) { -size_t local_essent_len, targ_essent_len; -const char *local_name, *targ_name; -const struct btf_type *t, *local_t; -struct ids_vec *cand_ids; -__u32 *new_ids; -int i, err, n; - - local_t =btf__type_by_id (local_btf, local_type_id); -if (!local_t) -return ERR_PTR (-EINVAL ); - - local_name =btf__name_by_offset (local_btf, local_t->name_off); -if (str_is_empty (local_name)) -return ERR_PTR (-EINVAL ); - local_essent_len =bpf_core_essential_name_len (local_name); - - cand_ids =calloc (1 ,sizeof (*cand_ids)); -if (!cand_ids) -return ERR_PTR (-ENOMEM ); +struct core_cand *new_cands, *cand; +const struct btf_type *t; +const char *targ_name; +size_t targ_essent_len; +int n, i; n =btf__get_nr_types (targ_btf); -for (i =1 ; i <= n; i++) { +for (i = targ_start_id; i <= n; i++) { t =btf__type_by_id (targ_btf, i); -if (btf_kind (t) !=btf_kind (local_t)) +if (btf_kind (t) !=btf_kind (local_cand->t))continue ; targ_name =btf__name_by_offset (targ_btf, t->name_off);
@@ -4650,25 +4650,62 @@ static struct ids_vec *bpf_core_find_cands(const struct btf *local_btf,if (targ_essent_len != local_essent_len)continue ; -if (strncmp (local_name, targ_name, local_essent_len) ==0 ) { -pr_debug ("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s\n" , - local_type_id,btf_kind_str (local_t), - local_name, i,btf_kind_str (t), targ_name); - new_ids =libbpf_reallocarray (cand_ids->data, - cand_ids->len +1 , -sizeof (*cand_ids->data)); -if (!new_ids) { - err = -ENOMEM ; - goto err_out; - } - cand_ids->data = new_ids; - cand_ids->data[cand_ids->len++] = i; - } +if (strncmp (local_cand->name, targ_name, local_essent_len) !=0 ) +continue ; + +pr_debug ("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n" , + local_cand->id,btf_kind_str (local_cand->t), + local_cand->name, i,btf_kind_str (t), targ_name, + targ_btf_name); + new_cands =libbpf_reallocarray (cands->cands, cands->len +1 , +sizeof (*cands->cands)); +if (!new_cands) +return -ENOMEM ; + + cand = &new_cands[cands->len]; + cand->btf = targ_btf; + cand->t = t; + cand->name = targ_name; + cand->id = i; + + cands->cands = new_cands; + cands->len++; } -return cand_ids; -err_out: -bpf_core_free_cands (cand_ids); -return ERR_PTR (err); +return 0 ; +} + +static struct core_cand_list * +bpf_core_find_cands (struct bpf_object *obj,const struct btf *local_btf,__u32 local_type_id) +{ +struct core_cand local_cand = {}; +struct core_cand_list *cands; +size_t local_essent_len; +int err; + + local_cand.btf = local_btf; + local_cand.t =btf__type_by_id (local_btf, local_type_id); +if (!local_cand.t) +return ERR_PTR (-EINVAL ); + + local_cand.name =btf__name_by_offset (local_btf, local_cand.t->name_off); +if (str_is_empty (local_cand.name)) +return ERR_PTR (-EINVAL ); + local_essent_len =bpf_core_essential_name_len (local_cand.name); + + cands =calloc (1 ,sizeof (*cands)); +if (!cands) +return ERR_PTR (-ENOMEM ); + +/* Attempt to find target candidates in vmlinux BTF first */ + err =bpf_core_add_cands (&local_cand, local_essent_len, + obj->btf_vmlinux_override ?: obj->btf_vmlinux, +"vmlinux" ,1 , cands); +if (err) { +bpf_core_free_cands (cands); +return ERR_PTR (err); + } + +return cands; }/* Check two types for compatibility for the purpose of field access
@@ -5661,7 +5698,6 @@ static int bpf_core_apply_relo(struct bpf_program *prog,const struct bpf_core_relo *relo, int relo_idx, const struct btf *local_btf, - const struct btf *targ_btf, struct hashmap *cand_cache) { struct bpf_core_spec local_spec, cand_spec, targ_spec = {};
@@ -5669,8 +5705,8 @@ static int bpf_core_apply_relo(struct bpf_program *prog,struct bpf_core_relo_res cand_res, targ_res;const struct btf_type *local_type;const char *local_name; -struct ids_vec *cand_ids; -__u32 local_id, cand_id; +struct core_cand_list *cands =NULL ; +__u32 local_id;const char *spec_str;int i, j, err;
@@ -5717,24 +5753,24 @@ static int bpf_core_apply_relo(struct bpf_program *prog,return -EOPNOTSUPP ; } -if (!hashmap__find (cand_cache, type_key, (void **)&cand_ids)) { - cand_ids =bpf_core_find_cands (local_btf, local_id, targ_btf); -if (IS_ERR (cand_ids)) { +if (!hashmap__find (cand_cache, type_key, (void **)&cands)) { + cands =bpf_core_find_cands (prog->obj, local_btf, local_id); +if (IS_ERR (cands)) {pr_warn ("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld" , prog->name, relo_idx, local_id,btf_kind_str (local_type), - local_name,PTR_ERR (cand_ids)); -return PTR_ERR (cand_ids); + local_name,PTR_ERR (cands)); +return PTR_ERR (cands); } - err =hashmap__set (cand_cache, type_key, cand_ids,NULL ,NULL ); + err =hashmap__set (cand_cache, type_key, cands,NULL ,NULL );if (err) { -bpf_core_free_cands (cand_ids); +bpf_core_free_cands (cands);return err; } } -for (i =0 , j =0 ; i < cand_ids->len; i++) { - cand_id = cand_ids->data[i]; - err =bpf_core_spec_match (&local_spec, targ_btf, cand_id, &cand_spec); +for (i =0 , j =0 ; i < cands->len; i++) { + err =bpf_core_spec_match (&local_spec, cands->cands[i].btf, + cands->cands[i].id, &cand_spec);if (err <0 ) {pr_warn ("prog '%s': relo #%d: error matching candidate #%d " , prog->name, relo_idx, i);
@@ -5778,7 +5814,7 @@ static int bpf_core_apply_relo(struct bpf_program *prog,return -EINVAL ; } - cand_ids->data[j++] = cand_spec.root_type_id; + cands->cands[j++] = cands->cands[i]; }/*
@@ -5790,7 +5826,7 @@ static int bpf_core_apply_relo(struct bpf_program *prog,* depending on relo's kind. */ if (j >0 ) - cand_ids->len = j; + cands->len = j;/* * If no candidates were found, it might be both a programmer error,
@@ -5834,20 +5870,19 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)struct hashmap_entry *entry;struct hashmap *cand_cache =NULL ;struct bpf_program *prog; -struct btf *targ_btf;const char *sec_name;int i, err =0 , insn_idx, sec_idx;if (obj->btf_ext->core_relo_info.len ==0 )return 0 ; -if (targ_btf_path) - targ_btf =btf__parse (targ_btf_path,NULL ); -else - targ_btf = obj->btf_vmlinux; -if (IS_ERR_OR_NULL (targ_btf)) { -pr_warn ("failed to get target BTF: %ld\n" ,PTR_ERR (targ_btf)); -return PTR_ERR (targ_btf); +if (targ_btf_path) { + obj->btf_vmlinux_override =btf__parse (targ_btf_path,NULL ); +if (IS_ERR_OR_NULL (obj->btf_vmlinux_override)) { + err =PTR_ERR (obj->btf_vmlinux_override); +pr_warn ("failed to parse target BTF: %d\n" , err); +return err; + } } cand_cache =hashmap__new (bpf_core_hash_fn, bpf_core_equal_fn,NULL );
@@ -5899,8 +5934,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)if (!prog->load)continue ; - err =bpf_core_apply_relo (prog, rec, i, obj->btf, - targ_btf, cand_cache); + err =bpf_core_apply_relo (prog, rec, i, obj->btf, cand_cache);if (err) {pr_warn ("prog '%s': relo #%d: failed to relocate: %d\n" , prog->name, i, err);
@@ -5911,8 +5945,9 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path) out:/* obj->btf_vmlinux is freed at the end of object load phase */ -if (targ_btf != obj->btf_vmlinux) -btf__free (targ_btf); +btf__free (obj->btf_vmlinux_override); + obj->btf_vmlinux_override =NULL ; +if (!IS_ERR_OR_NULL (cand_cache)) {hashmap__for_each_entry (cand_cache, entry, i) {bpf_core_free_cands (entry->value);
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v2 6/7 selftests/bpf: add support for marking sub-tests as skipped
In reply to Andrii NakryikoPreviously skipped sub-tests would be counted as passing with ":OK" appened in the log. Change that to be accounted as ":SKIP".
--- tools/testing/selftests/bpf/test_progs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/test_progs.c +3 -3
@@ -149,15 +149,15 @@ void test__end_subtest()if (sub_error_cnt) env.fail_cnt++; -else +else if (test->skip_cnt ==0 ) env.sub_succ_cnt++;skip_account ();dump_test_log (test, sub_error_cnt);fprintf (env.stdout,"#%d/%d %s:%s\n" , - test->test_num, test->subtest_num, - test->subtest_name, sub_error_cnt ?"FAIL" :"OK" ); + test->test_num, test->subtest_num, test->subtest_name, + sub_error_cnt ?"FAIL" : (test->skip_cnt ?"SKIP" :"OK" ));free (test->subtest_name); test->subtest_name =NULL ;
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v2 7/7 selftests/bpf: add CO-RE relocs selftest relying on kernel module BTF
In reply to Andrii NakryikoAdd a self-tests validating libbpf is able to perform CO-RE relocations against the type defined in kernel module BTF. if bpf_testmod.o is not supported by the kernel (e.g., due to version mismatch), skip tests, instead of failing.
--- .../selftests/bpf/prog_tests/core_reloc.c | 79 ++++++++++++++++--- .../selftests/bpf/progs/core_reloc_types.h | 17 ++++ .../bpf/progs/test_core_reloc_module.c | 66 ++++++++++++++++ 3 files changed, 151 insertions(+), 11 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_module.c
tools/testing/selftests/bpf/prog_tests/core_reloc.c +68 -11
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> #include "progs/core_reloc_types.h" +#include "bpf_testmod/bpf_testmod.h" #include <sys/mman.h> #include <sys/syscall.h> #include <bpf/btf.h>
@@ -9,6 +10,30 @@ static int duration = 0;#define STRUCT_TO_CHAR_PTR (struct_name) (const char *)&(struct struct_name) +#define MODULES_CASE(name, sec_name, tp_name) { \ + .case_name = name, \ + .bpf_obj_file = "test_core_reloc_module.o", \ + .btf_src_file = NULL, /* find in kernel module BTFs */ \ + .input = "", \ + .input_len = 0, \ + .output = STRUCT_TO_CHAR_PTR(core_reloc_module_output) { \ + .read_ctx_sz = sizeof(struct bpf_testmod_test_read_ctx),\ + .read_ctx_exists = true, \ + .buf_exists = true, \ + .len_exists = true, \ + .off_exists = true, \ + .len = 123, \ + .off = 0, \ + .comm = "test_progs", \ + .comm_len = sizeof("test_progs"), \ + }, \ + .output_len = sizeof(struct core_reloc_module_output), \ + .prog_sec_name = sec_name, \ + .raw_tp_name = tp_name, \ + .trigger = trigger_module_test_read, \ + .needs_testmod = true, \ +} +#define FLAVORS_DATA (struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \ .a = 42, \ .b = 0xc001, \
@@ -211,7 +236,7 @@ static int duration = 0; .output = STRUCT_TO_CHAR_PTR(core_reloc_bitfields_output) \ __VA_ARGS__, \ .output_len = sizeof(struct core_reloc_bitfields_output), \ - .direct_raw_tp = true, \ + .prog_sec_name = "tp_btf/sys_enter", \ }
@@ -222,7 +247,7 @@ static int duration = 0; }, { \ BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_direct.o", \ "direct:", name), \ - .direct_raw_tp = true, \ + .prog_sec_name = "tp_btf/sys_enter", \ .fails = true, \ }
@@ -309,6 +334,7 @@ static int duration = 0;struct core_reloc_test_case ;typedef int (*setup_test_fn )(struct core_reloc_test_case *test); +typedef int (*trigger_test_fn )(const struct core_reloc_test_case *test);struct core_reloc_test_case {const char *case_name;
@@ -319,9 +345,12 @@ struct core_reloc_test_case {const char *output;int output_len;bool fails; +bool needs_testmod;bool relaxed_core_relocs; -bool direct_raw_tp; +const char *prog_sec_name; +const char *raw_tp_name;setup_test_fn setup; +trigger_test_fn trigger; };static int find_btf_type (const struct btf *btf,const char *name,__u32 kind)
@@ -451,6 +480,23 @@ static int setup_type_id_case_failure(struct core_reloc_test_case *test)return 0 ; } +static int trigger_module_test_read (const struct core_reloc_test_case *test) +{ +struct core_reloc_module_output *exp = (void *)test->output; +int fd, err; + + fd =open ("/sys/kernel/bpf_testmod" ,O_RDONLY ); + err = -errno; +if (CHECK (fd <0 ,"testmod_file_open" ,"failed: %d\n" , err)) +return err; + +read (fd,NULL , exp->len);/* request expected number of bytes */ +close (fd); + +return 0 ; +} + +static struct core_reloc_test_case test_cases[] = {/* validate we can find kernel image and use its BTF for relocs */ {
@@ -467,6 +513,9 @@ static struct core_reloc_test_case test_cases[] = { .output_len = sizeof(struct core_reloc_kernel_output), }, +/* validate we can find kernel module BTF types for relocs/attach */ + MODULES_CASE("module", "raw_tp/bpf_testmod_test_read", "bpf_testmod_test_read"), +/* validate BPF program can use multiple flavors to match against * single target BTF type */
@@ -779,6 +828,11 @@ void test_core_reloc(void)if (!test__start_subtest (test_case->case_name))continue ; +if (test_case->needs_testmod && !env.has_testmod) { +test__skip (); +continue ; + } +if (test_case->setup) { err = test_case->setup (test_case);if (CHECK (err,"test_setup" ,"test #%d setup failed: %d\n" , i, err))
@@ -790,13 +844,11 @@ void test_core_reloc(void) test_case->bpf_obj_file, PTR_ERR(obj)))continue ; -/* for typed raw tracepoints, NULL should be specified */ -if (test_case->direct_raw_tp) { - probe_name ="tp_btf/sys_enter" ; - tp_name =NULL ; - }else { - probe_name ="raw_tracepoint/sys_enter" ; - tp_name ="sys_enter" ; + probe_name ="raw_tracepoint/sys_enter" ; + tp_name ="sys_enter" ; +if (test_case->prog_sec_name) { + probe_name = test_case->prog_sec_name; + tp_name = test_case->raw_tp_name;/* NULL for tp_btf */ } prog =bpf_object__find_program_by_title (obj, probe_name);
@@ -837,7 +889,12 @@ void test_core_reloc(void) goto cleanup;/* trigger test run */ -usleep (1 ); +if (test_case->trigger) { +if (!ASSERT_OK (test_case->trigger (test_case),"test_trigger" )) + goto cleanup; + }else { +usleep (1 ); + }if (data->skip) {test__skip ();
tools/testing/selftests/bpf/progs/core_reloc_types.h +17 -0
@@ -15,6 +15,23 @@ struct core_reloc_kernel_output {int comm_len; }; +/* +* MODULE +*/ + +struct core_reloc_module_output { +long long len; +long long off; +int read_ctx_sz; +bool read_ctx_exists; +bool buf_exists; +bool len_exists; +bool off_exists; +/* we have test_progs[-flavor], so cut flavor part */ +char comm[sizeof ("test_progs" )]; +int comm_len; +}; +/* * FLAVORS */
tools/testing/selftests/bpf/progs/test_core_reloc_module.c new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v2 5/7 selftests/bpf: add bpf_testmod kernel module for testing
In reply to Andrii NakryikoAdd bpf_testmod module, which is conceptually out-of-tree module and provides ways for selftests/bpf to test various kernel module-related functionality:
raw tracepoint, fentry/fexit/fmod_ret, etc. This module will be auto-loaded by test_progs test runner and expected by some of selftests to be present and loaded.
--- tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 12 +++- .../selftests/bpf/bpf_testmod/.gitignore | 6 ++ .../selftests/bpf/bpf_testmod/Makefile | 20 +++++++ .../bpf/bpf_testmod/bpf_testmod-events.h | 36 +++++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.c | 51 ++++++++++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 14 +++++ tools/testing/selftests/bpf/test_progs.c | 59 +++++++++++++++++++ tools/testing/selftests/bpf/test_progs.h | 1 + 9 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_testmod/.gitignore create mode 100644 tools/testing/selftests/bpf/bpf_testmod/Makefile create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
tools/testing/selftests/bpf/.gitignore +1 -0
@@ -35,3 +35,4 @@ test_cpp /tools /runqslower /bench +*.ko
tools/testing/selftests/bpf/Makefile +9 -3
@@ -80,7 +80,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \ # Compile but not part of 'make run_tests' TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ - test_lirc_mode2_user xdping test_cpp runqslower bench + test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko TEST_CUSTOM_PROGS = urandom_read
@@ -104,6 +104,7 @@ OVERRIDE_TARGETS := 1 override define CLEAN $(call msg,CLEAN) $(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN) + $(Q)$(MAKE) -C bpf_testmod clean endef include ../lib.mk
@@ -136,6 +137,11 @@ $(OUTPUT)/urandom_read: urandom_read.c $(call msg,BINARY,,$@) $(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1 +$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch]) + $(call msg,MOD,,$@) + $(Q)$(MAKE) $(submake_extras) -C bpf_testmod + $(Q)cp bpf_testmod/bpf_testmod.ko $@ + $(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ) $(call msg,CC,,$@) $(Q)$(CC) -c $(CFLAGS) -o $@ $<
@@ -388,7 +394,7 @@ TRUNNER_BPF_PROGS_DIR := progs TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ network_helpers.c testing_helpers.c \ btf_helpers.c flow_dissector_load.h -TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ +TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ ima_setup.sh \ $(wildcard progs/btf_dump_test_case_*.c) TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
@@ -460,4 +466,4 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \ EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \ prog_tests/tests.h map_tests/tests.h verifier/tests.h \ feature \ - $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc) + $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc bpf_testmod.ko)
tools/testing/selftests/bpf/bpf_testmod/.gitignore new file +6 -0
@@ -0,0 +1,6 @@ +*.mod +*.mod.c +*.o +.ko +/Module.symvers +/modules.order
tools/testing/selftests/bpf/bpf_testmod/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +BPF_TESTMOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= $(abspath $(BPF_TESTMOD_DIR)/../../../../..) + +ifeq ($(V),1) +Q = +else +Q = @ +endif + +MODULES = bpf_testmod.ko + +obj-m += bpf_testmod.o +CFLAGS_bpf_testmod.o = -I$(src) + +all: + +$(Q)make -C $(KDIR) M=$(BPF_TESTMOD_DIR) modules + +clean: + +$(Q)make -C $(KDIR) M=$(BPF_TESTMOD_DIR) clean +
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2020 Facebook */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM bpf_testmod + +#if !defined(_BPF_TESTMOD_EVENTS_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _BPF_TESTMOD_EVENTS_H + +#include <linux/tracepoint.h> +#include "bpf_testmod.h" + +TRACE_EVENT (bpf_testmod_test_read, + TP_PROTO(struct task_struct *task, struct bpf_testmod_test_read_ctx *ctx), +TP_ARGS (task, ctx), +TP_STRUCT__entry ( + __field(pid_t, pid) + __array(char, comm, TASK_COMM_LEN) + __field(loff_t, off) +__field (size_t, len) + ), +TP_fast_assign ( + __entry->pid = task->pid; + memcpy(__entry->comm, task->comm, TASK_COMM_LEN); + __entry->off = ctx->off; + __entry->len = ctx->len; + ), +TP_printk ("pid=%d comm=%s off=%llu len=%zu" , + __entry->pid, __entry->comm, __entry->off, __entry->len) +); + +#endif /* _BPF_TESTMOD_EVENTS_H */ + +#undef TRACE_INCLUDE_PATH +#define TRACE_INCLUDE_PATH . +#define TRACE_INCLUDE_FILE bpf_testmod-events +#include <trace/define_trace.h>
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c new file +51 -0
51 lines not on this page · raw message
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2020 Facebook */ +#ifndef _BPF_TESTMOD_H +#define _BPF_TESTMOD_H + +#include <linux/types.h> + +struct bpf_testmod_test_read_ctx { +char *buf; +loff_t off; +size_t len; +}; + +#endif /* _BPF_TESTMOD_H */
tools/testing/selftests/bpf/test_progs.c +59 -0
77 lines not on this page · raw message
tools/testing/selftests/bpf/test_progs.h +1 -0
@@ -66,6 +66,7 @@ struct test_env {enum verbosity verbosity;bool jit_enabled; +bool has_testmod;bool get_test_cnt;bool list_test_names;
Signature · 1 line
2.24.1