Support BTF-powered BPF tracing programs for kernel modules
Subsystems (11)
Andrii Nakryiko
maintainerv6 cover Support BTF-powered BPF tracing programs for kernel modules
This patch sets extends kernel and libbpf with support for attaching BTF-powered raw tracepoint (tp_btf) and tracing (fentry/fexit/fmod_ret/lsm) BPF programs to BPF hooks defined in kernel modules. As part of that, libbpf now supports performing CO-RE relocations against types in kernel module BTFs, in addition to existing vmlinux BTF support.
Kernel UAPI for BPF_PROG_LOAD now allows to specify kernel module (or vmlinux) BTF object FD in attach_btf_obj_fd field, aliased to attach_prog_fd. This is used to identify which BTF object needs to be used for finding BTF type by provided attach_btf_id.
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. Currently pahole doesn't generate BTF_KIND_FUNC info for ftrace-able static functions in kernel modules, so expose traced function in bpf_sidecar.ko. Once pahole is enhanced, we can go back to static function.
From end user perspective there are no extra actions that need to happen. Libbpf will continue searching across all kernel module BTFs, if desired attach BTF type is not found in vmlinux. That way it doesn't matter if BPF hook that user is trying to attach to is built into vmlinux image or is loaded in kernel module.
v5->v6:
- move btf_put() back to syscall.c (kernel test robot);
- added close(fd) in patch #5 (John);v4->v5:
- use FD to specify BTF object (Alexei);
- move prog->aux->attach_btf putting into bpf_prog_free() for consistency
with putting prog->aux->dst_prog;
- fix BTF FD leak(s) in libbpf;v3->v4:
- merge together patch sets [0] and [1];
- avoid increasing bpf_reg_state by reordering fields (Alexei);
- preserve btf_data_size in struct module;v2->v3:
- fix subtle uninitialized variable use in BTF ID iteration code;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.
[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=393677&state=*
[1] https://patchwork.kernel.org/project/netdevbpf/list/?series=393679&state=*
Andrii Nakryiko (14):
bpf: fix bpf_put_raw_tracepoint()'s use of __module_address()
bpf: keep module's btf_data_size intact after load
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
bpf: remove hard-coded btf_vmlinux assumption from BPF verifier
bpf: allow to specify kernel module BTFs when attaching BPF programs
libbpf: factor out low-level BPF program loading helper
libbpf: support attachment of BPF tracing programs to kernel modules selftests/bpf: add tp_btf CO-RE reloc test for modules selftests/bpf: add fentry/fexit/fmod_ret selftest for kernel module
include/linux/bpf.h | 13 +- include/linux/bpf_verifier.h | 28 +- include/linux/btf.h | 6 +- include/uapi/linux/bpf.h | 7 +- kernel/bpf/btf.c | 70 ++- kernel/bpf/syscall.c | 78 ++- kernel/bpf/verifier.c | 77 +-- kernel/module.c | 1 - kernel/trace/bpf_trace.c | 8 +- net/ipv4/bpf_tcp_ca.c | 3 +- tools/include/uapi/linux/bpf.h | 7 +- tools/lib/bpf/bpf.c | 101 ++-- tools/lib/bpf/btf.c | 61 ++- tools/lib/bpf/libbpf.c | 500 ++++++++++++++---- tools/lib/bpf/libbpf_internal.h | 31 ++ 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 | 52 ++ .../selftests/bpf/bpf_testmod/bpf_testmod.h | 14 + .../selftests/bpf/prog_tests/core_reloc.c | 80 ++- .../selftests/bpf/prog_tests/module_attach.c | 53 ++ .../selftests/bpf/progs/core_reloc_types.h | 17 + .../bpf/progs/test_core_reloc_module.c | 96 ++++ .../selftests/bpf/progs/test_module_attach.c | 66 +++ tools/testing/selftests/bpf/test_progs.c | 65 ++- tools/testing/selftests/bpf/test_progs.h | 1 + 29 files changed, 1230 insertions(+), 280 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/prog_tests/module_attach.c create mode 100644 tools/testing/selftests/bpf/progs/test_core_reloc_module.c create mode 100644 tools/testing/selftests/bpf/progs/test_module_attach.c
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v6 1/14 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 v6 2/14 bpf: keep module's btf_data_size intact after load
In reply to Andrii NakryikoHaving real btf_data_size stored in struct module is benefitial to quickly determine which kernel modules have associated BTF object and which don't. There is no harm in keeping this info, as opposed to keeping invalid pointer.
--- kernel/module.c | 1 - 1 file changed, 1 deletion(-)
kernel/module.c +0 -1
@@ -3712,7 +3712,6 @@ static noinline int do_init_module(struct module *mod)#ifdef CONFIG_DEBUG_INFO_BTF_MODULES /* .BTF is not SHF_ALLOC and will get removed, so sanitize pointer */ mod->btf_data =NULL ; - mod->btf_data_size =0 ;#endif /* * We want to free module_init, but be aware that kallsyms may be
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v6 3/14 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
@@ -1323,35 +1323,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) {
@@ -1360,31 +1352,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 v6 4/14 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 v6 5/14 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 | 179 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 169 insertions(+), 10 deletions(-)
tools/lib/bpf/libbpf.c +169 -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,96 @@ 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 =0 , 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); +close (fd); +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 +4825,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 +5907,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 +6095,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 ;
@@ -7316,6 +7467,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 v6 6/14 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.
Pahole currently isn't able to generate BTF for static functions in kernel modules, so make sure traced function is global.
--- 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 | 52 ++++++++++++++++ .../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, 198 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 +52 -0
52 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
Andrii Nakryiko
maintainerPATCH v6 7/14 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 v6 8/14 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 v6 9/14 bpf: remove hard-coded btf_vmlinux assumption from BPF verifier
In reply to Andrii NakryikoRemove a permeating assumption thoughout BPF verifier of vmlinux BTF. Instead, wherever BTF type IDs are involved, also track the instance of struct btf that goes along with the type ID. This allows to gradually add support for kernel module BTFs and using/tracking module types across BPF helper calls and registers.
This patch also renames btf_id() function to btf_obj_id() to minimize naming clash with using btf_id to denote BTF *type* ID, rather than BTF *object*'s ID.
Also, altough btf_vmlinux can't get destructed and thus doesn't need refcounting, module BTFs need that, so apply BTF refcounting universally when BPF program is using BTF-powered attachment (tp_btf, fentry/fexit, etc). This makes for simpler clean up code.
Now that BTF type ID is not enough to uniquely identify a BTF type, extend BPF trampoline key to include BTF object ID. To differentiate that from target program BPF ID, set 31st bit of type ID. BTF type IDs (at least currently) are not allowed to take full 32 bits, so there is no danger of confusing that bit with a valid BTF type ID.
--- include/linux/bpf.h | 13 ++++-- include/linux/bpf_verifier.h | 28 +++++++++---- include/linux/btf.h | 5 ++- kernel/bpf/btf.c | 65 ++++++++++++++++++++---------- kernel/bpf/syscall.c | 24 +++++++++-- kernel/bpf/verifier.c | 77 ++++++++++++++++++++++-------------- net/ipv4/bpf_tcp_ca.c | 3 +- 7 files changed, 148 insertions(+), 67 deletions(-)
include/linux/bpf.h +9 -4
@@ -421,7 +421,10 @@ struct bpf_insn_access_aux {enum bpf_reg_type reg_type;union {int ctx_field_size; -u32 btf_id; +struct { +struct btf *btf; +u32 btf_id; + }; };struct bpf_verifier_log *log;/* for verbose logs */ };
@@ -458,6 +461,7 @@ struct bpf_verifier_ops {struct bpf_insn *dst, struct bpf_prog *prog, u32 *target_size);int (*btf_struct_access)(struct bpf_verifier_log *log, +const struct btf *btf,const struct btf_type *t,int off,int size,enum bpf_access_type atype,u32 *next_btf_id);
@@ -771,6 +775,7 @@ struct bpf_prog_aux {u32 ctx_arg_info_size;u32 max_rdonly_access;u32 max_rdwr_access; +struct btf *attach_btf;const struct bpf_ctx_arg_aux *ctx_arg_info;struct mutex dst_mutex;/* protects dst_* pointers below, *after* prog becomes visible */ struct bpf_prog *dst_prog;
@@ -1005,7 +1010,6 @@ struct bpf_event_entry {bool bpf_prog_array_compatible (struct bpf_array *array,const struct bpf_prog *fp);int bpf_prog_calc_tag (struct bpf_prog *fp); -const char *kernel_type_name (u32 btf_type_id);const struct bpf_func_proto *bpf_get_trace_printk_proto (void );
@@ -1450,12 +1454,13 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,bool btf_ctx_access (int off,int size,enum bpf_access_type type,const struct bpf_prog *prog,struct bpf_insn_access_aux *info); -int btf_struct_access (struct bpf_verifier_log *log, +int btf_struct_access (struct bpf_verifier_log *log,const struct btf *btf,const struct btf_type *t,int off,int size,enum bpf_access_type atype,u32 *next_btf_id);bool btf_struct_ids_match (struct bpf_verifier_log *log, -int off,u32 id,u32 need_type_id); +const struct btf *btf,u32 id,int off, +const struct btf *need_btf,u32 need_type_id); int btf_distill_func_proto(struct bpf_verifier_log *log, struct btf *btf,
include/linux/bpf_verifier.h +21 -7
@@ -5,6 +5,7 @@#define _LINUX_BPF_VERIFIER_H 1#include <linux/bpf.h> /* for enum bpf_reg_type */ +#include <linux/btf.h> /* for struct btf and btf_id() */ #include <linux/filter.h> /* for MAX_BPF_STACK */ #include <linux/tnum.h>
@@ -43,6 +44,8 @@ enum bpf_reg_liveness {struct bpf_reg_state {/* Ordering of fields matters. See states_equal() */ enum bpf_reg_type type; +/* Fixed part of pointer offset, pointer types only */ +s32 off;union {/* valid when type == PTR_TO_PACKET */ int range;
@@ -52,15 +55,20 @@ struct bpf_reg_state {*/ struct bpf_map *map_ptr; -u32 btf_id;/* for PTR_TO_BTF_ID */ +/* for PTR_TO_BTF_ID */ +struct { +struct btf *btf; +u32 btf_id; + };u32 mem_size;/* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */ /* Max size from any of the above. */ -unsigned long raw; +struct { +unsigned long raw1; +unsigned long raw2; + } raw; }; -/* Fixed part of pointer offset, pointer types only */ -s32 off;/* For PTR_TO_PACKET, used to find other pointers with the same variable * offset, so they can share range knowledge. * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we
@@ -311,7 +319,10 @@ struct bpf_insn_aux_data {struct {enum bpf_reg_type reg_type;/* type of pseudo_btf_id */ union { -u32 btf_id;/* btf_id for struct typed var */ +struct { +struct btf *btf; +u32 btf_id;/* btf_id for struct typed var */ + };u32 mem_size;/* mem_size for non-struct typed var */ }; } btf_var;
@@ -459,9 +470,12 @@ int check_ctx_reg(struct bpf_verifier_env *env,/* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ static inline u64 bpf_trampoline_compute_key (const struct bpf_prog *tgt_prog, -u32 btf_id) +struct btf *btf,u32 btf_id) { -return tgt_prog ? (((u64 )tgt_prog->aux->id) <<32 | btf_id) : btf_id; +if (tgt_prog) +return ((u64 )tgt_prog->aux->id <<32 ) | btf_id; +else +return ((u64 )btf_obj_id (btf) <<32 ) |0x80000000 | btf_id; } int bpf_check_attach_target(struct bpf_verifier_log *log,
include/linux/btf.h +4 -1
@@ -18,6 +18,7 @@ struct btf_show;extern const struct file_operations btf_fops; +void btf_get (struct btf *btf);void btf_put (struct btf *btf);int btf_new_fd (const union bpf_attr *attr);struct btf *btf_get_by_fd (int fd);
@@ -88,7 +89,7 @@ int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj, char *buf, int len, u64 flags);int btf_get_fd_by_id (u32 id); -u32 btf_id (const struct btf *btf); +u32 btf_obj_id (const struct btf *btf);bool btf_member_is_reg_int (const struct btf *btf,const struct btf_type *s,const struct btf_member *m,u32 expected_offset,u32 expected_size);
@@ -206,6 +207,8 @@ static inline const struct btf_var_secinfo *btf_type_var_secinfo( }#ifdef CONFIG_BPF_SYSCALL +struct bpf_prog ; +const struct btf_type *btf_type_by_id (const struct btf *btf,u32 type_id);const char *btf_name_by_offset (const struct btf *btf,u32 offset);struct btf *btf_parse_vmlinux (void );
kernel/bpf/btf.c +44 -21
180 lines not on this page · raw message
kernel/bpf/syscall.c +21 -3
@@ -1691,6 +1691,8 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)bpf_prog_kallsyms_del_all (prog);btf_put (prog->aux->btf);bpf_prog_free_linfo (prog); +if (prog->aux->attach_btf) +btf_put (prog->aux->attach_btf);if (deferred) {if (prog->aux->sleepable)
@@ -2113,6 +2115,20 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) prog->expected_attach_type = attr->expected_attach_type; prog->aux->attach_btf_id = attr->attach_btf_id; + +if (attr->attach_btf_id && !attr->attach_prog_fd) { +struct btf *btf; + + btf =bpf_get_btf_vmlinux (); +if (IS_ERR (btf)) +return PTR_ERR (btf); +if (!btf) +return -EINVAL ; + +btf_get (btf); + prog->aux->attach_btf = btf; + } +if (attr->attach_prog_fd) {struct bpf_prog *dst_prog;
@@ -2209,6 +2225,8 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)free_uid (prog->aux->user);security_bpf_prog_free (prog->aux); free_prog: +if (prog->aux->attach_btf) +btf_put (prog->aux->attach_btf);bpf_prog_free (prog);return err; }
@@ -2566,7 +2584,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_put_prog; } - key =bpf_trampoline_compute_key (tgt_prog, btf_id); + key =bpf_trampoline_compute_key (tgt_prog,NULL , btf_id); } link =kzalloc (sizeof (*link),GFP_USER );
@@ -3543,7 +3561,7 @@ static int bpf_prog_get_info_by_fd(struct file *file, }if (prog->aux->btf) - info.btf_id =btf_id (prog->aux->btf); + info.btf_id =btf_obj_id (prog->aux->btf); ulen = info.nr_func_info; info.nr_func_info = prog->aux->func_info_cnt;
@@ -3646,7 +3664,7 @@ static int bpf_map_get_info_by_fd(struct file *file,memcpy (info.name, map->name,sizeof (map->name));if (map->btf) { - info.btf_id =btf_id (map->btf); + info.btf_id =btf_obj_id (map->btf); info.btf_key_type_id = map->btf_key_type_id; info.btf_value_type_id = map->btf_value_type_id; }
kernel/bpf/verifier.c +47 -30
256 lines not on this page · raw message
net/ipv4/bpf_tcp_ca.c +2 -1
@@ -95,6 +95,7 @@ static bool bpf_tcp_ca_is_valid_access(int off, int size, }static int bpf_tcp_ca_btf_struct_access (struct bpf_verifier_log *log, +const struct btf *btf,const struct btf_type *t,int off,int size,enum bpf_access_type atype,u32 *next_btf_id)
@@ -102,7 +103,7 @@ static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,size_t end;if (atype ==BPF_READ ) -return btf_struct_access (log, t, off, size, atype, next_btf_id); +return btf_struct_access (log, btf, t, off, size, atype, next_btf_id);if (t != tcp_sock_type) {bpf_log (log,"only read is supported\n" );
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v6 10/14 bpf: allow to specify kernel module BTFs when attaching BPF programs
In reply to Andrii NakryikoAdd ability for user-space programs to specify non-vmlinux BTF when attaching BTF-powered BPF programs: raw_tp, fentry/fexit/fmod_ret, LSM, etc. For this, attach_prog_fd (now with the alias name attach_btf_obj_fd) should specify FD of a module or vmlinux BTF object. For backwards compatibility reasons, 0 denotes vmlinux BTF. Only kernel BTF (vmlinux or module) can be specified.
--- include/linux/btf.h | 1 + include/uapi/linux/bpf.h | 7 ++- kernel/bpf/btf.c | 5 +++ kernel/bpf/syscall.c | 82 +++++++++++++++++++++------------- tools/include/uapi/linux/bpf.h | 7 ++- 5 files changed, 69 insertions(+), 33 deletions(-)
include/linux/btf.h +1 -0
@@ -90,6 +90,7 @@ int btf_type_snprintf_show(const struct btf *btf, u32 type_id, void *obj,int btf_get_fd_by_id (u32 id);u32 btf_obj_id (const struct btf *btf); +bool btf_is_kernel (const struct btf *btf);bool btf_member_is_reg_int (const struct btf *btf,const struct btf_type *s,const struct btf_member *m,u32 expected_offset,u32 expected_size);
include/uapi/linux/bpf.h +6 -1
@@ -557,7 +557,12 @@ union bpf_attr {__aligned_u64 line_info;/* line info */ __u32 line_info_cnt;/* number of bpf_line_info records */ __u32 attach_btf_id;/* in-kernel BTF type id to attach to */ -__u32 attach_prog_fd;/* 0 to attach to vmlinux */ +union { +/* valid prog_fd to attach to bpf prog */ +__u32 attach_prog_fd; +/* or valid module BTF object fd or 0 to attach to vmlinux */ +__u32 attach_btf_obj_fd; + }; };struct {/* anonymous struct used by BPF_OBJ_* commands */
kernel/bpf/btf.c +5 -0
@@ -5738,6 +5738,11 @@ u32 btf_obj_id(const struct btf *btf)return btf->id; } +bool btf_is_kernel (const struct btf *btf) +{ +return btf->kernel_btf; +} +static int btf_id_cmp_func (const void *a,const void *b) {const int *pa = a, *pb = b;
kernel/bpf/syscall.c +51 -31
121 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +6 -1
@@ -557,7 +557,12 @@ union bpf_attr {__aligned_u64 line_info;/* line info */ __u32 line_info_cnt;/* number of bpf_line_info records */ __u32 attach_btf_id;/* in-kernel BTF type id to attach to */ -__u32 attach_prog_fd;/* 0 to attach to vmlinux */ +union { +/* valid prog_fd to attach to bpf prog */ +__u32 attach_prog_fd; +/* or valid module BTF object fd or 0 to attach to vmlinux */ +__u32 attach_btf_obj_fd; + }; };struct {/* anonymous struct used by BPF_OBJ_* commands */
Signature · 1 line
2.24.1
Re: [PATCH v6 bpf-next 10/14] bpf: allow to specify kernel module BTFs when attaching BPF programs
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) · line 2125
Quoted 10 lines · Andrii Nakryiko
kernel/bpf/syscall.c +10
-0+if (attr->attach_prog_fd) { + dst_prog =bpf_prog_get (attr->attach_prog_fd); +if (IS_ERR (dst_prog)) { + dst_prog =NULL ; + attach_btf =btf_get_by_fd (attr->attach_btf_obj_fd); +if (IS_ERR (attach_btf)) +return -EINVAL ; +if (!btf_is_kernel (attach_btf)) { +btf_put (attach_btf); +return -EINVAL ;
Applied, but please consider follow up with different err code here. I think we might support this case in the future.
Specifying prog's BTF as a base and attach_btf_id within it might make user space simpler in some cases. prog's btf covers the whole elf file. Where prog_fd is a specific prog. That narrow scope isn't really necessary. So may be return ENOTSUPP here for now? With a hint that this might work in the future?
Re: [PATCH v6 bpf-next 10/14] bpf: allow to specify kernel module BTFs when attaching BPF programs
Quoted 13 lines · Alexei Starovoitov
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) · line 2125
Quoted 10 lines · Andrii Nakryiko
kernel/bpf/syscall.c +10
-0+if (attr->attach_prog_fd) { + dst_prog =bpf_prog_get (attr->attach_prog_fd); +if (IS_ERR (dst_prog)) { + dst_prog =NULL ; + attach_btf =btf_get_by_fd (attr->attach_btf_obj_fd); +if (IS_ERR (attach_btf)) +return -EINVAL ; +if (!btf_is_kernel (attach_btf)) { +btf_put (attach_btf); +return -EINVAL ;
Quoted 4 lines
Applied, but please consider follow up with different err code here. I think we might support this case in the future.
Specifying prog's BTF as a base andattach_btf_idwithin it might make user space simpler in some cases. prog's btf covers the whole elf file.
The problem is that there is no link from BTF to bpf_prog. And multiple instances of bpf_progs can re-use the same BTF object and the same BTF type ID. That would need to be resolved somehow.
But keeping our options open is a good idea either way. So I'll send a patch to switch this to -EOPNOTSUPP (I think that's the one we need to use for user-space).
Quoted 3 lines
Where
prog_fdis a specific prog. That narrow scope isn't really necessary. So may be return ENOTSUPP here for now? With a hint that this might work in the future?
Andrii Nakryiko
maintainerPATCH v6 11/14 libbpf: factor out low-level BPF program loading helper
In reply to Andrii NakryikoRefactor low-level API for BPF program loading to not rely on public API types. This allows painless extension without constant efforts to cleverly not break backwards compatibility.
--- tools/lib/bpf/bpf.c | 100 ++++++++++++++++++++++---------- tools/lib/bpf/libbpf.c | 34 +++++------ tools/lib/bpf/libbpf_internal.h | 29 +++++++++ 3 files changed, 113 insertions(+), 50 deletions(-)
tools/lib/bpf/bpf.c +68 -32
@@ -215,59 +215,52 @@ alloc_zero_tailing_info(const void *orecord, __u32 cnt,return info; } -int bpf_load_program_xattr (const struct bpf_load_program_attr *load_attr, -char *log_buf,size_t log_buf_sz) +int libbpf__bpf_prog_load (const struct bpf_prog_load_params *load_attr) {void *finfo =NULL , *linfo =NULL ;union bpf_attr attr; -__u32 log_level;int fd; -if (!load_attr || !log_buf != !log_buf_sz) +if (!load_attr->log_buf != !load_attr->log_buf_sz)return -EINVAL ; - log_level = load_attr->log_level; -if (log_level > (4 |2 |1 ) || (log_level && !log_buf)) +if (load_attr->log_level > (4 |2 |1 ) || (load_attr->log_level && !load_attr->log_buf))return -EINVAL ;memset (&attr,0 ,sizeof (attr)); attr.prog_type = load_attr->prog_type; attr.expected_attach_type = load_attr->expected_attach_type; -if (attr.prog_type ==BPF_PROG_TYPE_STRUCT_OPS || - attr.prog_type ==BPF_PROG_TYPE_LSM ) { - attr.attach_btf_id = load_attr->attach_btf_id; - }else if (attr.prog_type ==BPF_PROG_TYPE_TRACING || - attr.prog_type ==BPF_PROG_TYPE_EXT ) { - attr.attach_btf_id = load_attr->attach_btf_id; - attr.attach_prog_fd = load_attr->attach_prog_fd; - }else { - attr.prog_ifindex = load_attr->prog_ifindex; - attr.kern_version = load_attr->kern_version; - } - attr.insn_cnt = (__u32 )load_attr->insns_cnt; + + attr.attach_btf_id = load_attr->attach_btf_id; + attr.attach_prog_fd = load_attr->attach_prog_fd; + + attr.prog_ifindex = load_attr->prog_ifindex; + attr.kern_version = load_attr->kern_version; + + attr.insn_cnt = (__u32 )load_attr->insn_cnt; attr.insns =ptr_to_u64 (load_attr->insns); attr.license =ptr_to_u64 (load_attr->license); - attr.log_level = log_level; -if (log_level) { - attr.log_buf =ptr_to_u64 (log_buf); - attr.log_size = log_buf_sz; - }else { - attr.log_buf =ptr_to_u64 (NULL ); - attr.log_size =0 ; + attr.log_level = load_attr->log_level; +if (attr.log_level) { + attr.log_buf =ptr_to_u64 (load_attr->log_buf); + attr.log_size = load_attr->log_buf_sz; } attr.prog_btf_fd = load_attr->prog_btf_fd; + attr.prog_flags = load_attr->prog_flags; + attr.func_info_rec_size = load_attr->func_info_rec_size; attr.func_info_cnt = load_attr->func_info_cnt; attr.func_info =ptr_to_u64 (load_attr->func_info); + attr.line_info_rec_size = load_attr->line_info_rec_size; attr.line_info_cnt = load_attr->line_info_cnt; attr.line_info =ptr_to_u64 (load_attr->line_info); +if (load_attr->name)memcpy (attr.prog_name, load_attr->name, -min (strlen (load_attr->name),BPF_OBJ_NAME_LEN -1 )); - attr.prog_flags = load_attr->prog_flags; +min (strlen (load_attr->name), (size_t )BPF_OBJ_NAME_LEN -1 )); fd =sys_bpf_prog_load (&attr,sizeof (attr));if (fd >=0 )
@@ -307,19 +300,19 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, } fd =sys_bpf_prog_load (&attr,sizeof (attr)); -if (fd >=0 ) goto done; } -if (log_level || !log_buf) +if (load_attr->log_level || !load_attr->log_buf) goto done;/* Try again with log */ - attr.log_buf =ptr_to_u64 (log_buf); - attr.log_size = log_buf_sz; + attr.log_buf =ptr_to_u64 (load_attr->log_buf); + attr.log_size = load_attr->log_buf_sz; attr.log_level =1 ; - log_buf[0 ] =0 ; + load_attr->log_buf[0 ] =0 ; + fd =sys_bpf_prog_load (&attr,sizeof (attr)); done:free (finfo);
@@ -327,6 +320,49 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,return fd; } +int bpf_load_program_xattr (const struct bpf_load_program_attr *load_attr, +char *log_buf,size_t log_buf_sz) +{ +struct bpf_prog_load_params p = {}; + +if (!load_attr || !log_buf != !log_buf_sz) +return -EINVAL ; + + p.prog_type = load_attr->prog_type; + p.expected_attach_type = load_attr->expected_attach_type; +switch (p.prog_type) { +case BPF_PROG_TYPE_STRUCT_OPS : +case BPF_PROG_TYPE_LSM : + p.attach_btf_id = load_attr->attach_btf_id; +break ; +case BPF_PROG_TYPE_TRACING : +case BPF_PROG_TYPE_EXT : + p.attach_btf_id = load_attr->attach_btf_id; + p.attach_prog_fd = load_attr->attach_prog_fd; +break ; +default : + p.prog_ifindex = load_attr->prog_ifindex; + p.kern_version = load_attr->kern_version; + } + p.insn_cnt = load_attr->insns_cnt; + p.insns = load_attr->insns; + p.license = load_attr->license; + p.log_level = load_attr->log_level; + p.log_buf = log_buf; + p.log_buf_sz = log_buf_sz; + p.prog_btf_fd = load_attr->prog_btf_fd; + p.func_info_rec_size = load_attr->func_info_rec_size; + p.func_info_cnt = load_attr->func_info_cnt; + p.func_info = load_attr->func_info; + p.line_info_rec_size = load_attr->line_info_rec_size; + p.line_info_cnt = load_attr->line_info_cnt; + p.line_info = load_attr->line_info; + p.name = load_attr->name; + p.prog_flags = load_attr->prog_flags; + +return libbpf__bpf_prog_load (&p); +} + int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns, size_t insns_cnt, const char *license, __u32 kern_version, char *log_buf,
tools/lib/bpf/libbpf.c +16 -18
@@ -6809,7 +6809,7 @@ static int load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, char *license, __u32 kern_version, int *pfd) { - struct bpf_load_program_attr load_attr; + struct bpf_prog_load_params load_attr = {};char *cp, errmsg[STRERR_BUFSIZE ];size_t log_buf_size =0 ;char *log_buf =NULL ;
@@ -6828,7 +6828,6 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,if (!insns || !insns_cnt)return -EINVAL ; -memset (&load_attr,0 ,sizeof (struct bpf_load_program_attr )); load_attr.prog_type = prog->type;/* old kernels might not support specifying expected_attach_type */ if (!kernel_supports(FEAT_EXP_ATTACH_TYPE) && prog->sec_def &&
@@ -6839,19 +6838,14 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,if (kernel_supports (FEAT_PROG_NAME )) load_attr.name = prog->name; load_attr.insns = insns; - load_attr.insns_cnt = insns_cnt; + load_attr.insn_cnt = insns_cnt; load_attr.license = license; -if (prog->type ==BPF_PROG_TYPE_STRUCT_OPS || - prog->type ==BPF_PROG_TYPE_LSM ) { - load_attr.attach_btf_id = prog->attach_btf_id; - }else if (prog->type ==BPF_PROG_TYPE_TRACING || - prog->type ==BPF_PROG_TYPE_EXT ) { - load_attr.attach_prog_fd = prog->attach_prog_fd; - load_attr.attach_btf_id = prog->attach_btf_id; - }else { - load_attr.kern_version = kern_version; - load_attr.prog_ifindex = prog->prog_ifindex; - } + load_attr.attach_btf_id = prog->attach_btf_id; + load_attr.attach_prog_fd = prog->attach_prog_fd; + load_attr.attach_btf_id = prog->attach_btf_id; + load_attr.kern_version = kern_version; + load_attr.prog_ifindex = prog->prog_ifindex; +/* specify func_info/line_info only if kernel supports them */ btf_fd =bpf_object__btf_fd (prog->obj);if (btf_fd >=0 &&kernel_supports (FEAT_BTF_FUNC )) {
@@ -6875,7 +6869,9 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, *log_buf =0 ; } - ret =bpf_load_program_xattr (&load_attr, log_buf, log_buf_size); + load_attr.log_buf = log_buf; + load_attr.log_buf_sz = log_buf_size; + ret =libbpf__bpf_prog_load (&load_attr);if (ret >=0 ) {if (log_buf && load_attr.log_level)
@@ -6916,9 +6912,9 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,pr_warn ("-- BEGIN DUMP LOG ---\n" );pr_warn ("\n%s\n" , log_buf);pr_warn ("-- END LOG --\n" ); - }else if (load_attr.insns_cnt >=BPF_MAXINSNS ) { + }else if (load_attr.insn_cnt >=BPF_MAXINSNS ) {pr_warn ("Program too large (%zu insns), at most %d insns\n" , - load_attr.insns_cnt,BPF_MAXINSNS ); + load_attr.insn_cnt,BPF_MAXINSNS ); ret = -LIBBPF_ERRNO__PROG2BIG ; }else if (load_attr.prog_type !=BPF_PROG_TYPE_KPROBE ) {/* Wrong program type? */
@@ -6926,7 +6922,9 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, load_attr.prog_type =BPF_PROG_TYPE_KPROBE ; load_attr.expected_attach_type =0 ; - fd =bpf_load_program_xattr (&load_attr,NULL ,0 ); + load_attr.log_buf =NULL ; + load_attr.log_buf_sz =0 ; + fd =libbpf__bpf_prog_load (&load_attr);if (fd >=0 ) {close (fd); ret = -LIBBPF_ERRNO__PROGTYPE ;
tools/lib/bpf/libbpf_internal.h +29 -0
@@ -151,6 +151,35 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);int libbpf__load_raw_btf (const char *raw_types,size_t types_len,const char *str_sec,size_t str_len); +struct bpf_prog_load_params { +enum bpf_prog_type prog_type; +enum bpf_attach_type expected_attach_type; +const char *name; +const struct bpf_insn *insns; +size_t insn_cnt; +const char *license; +__u32 kern_version; +__u32 attach_prog_fd; +__u32 attach_btf_id; +__u32 prog_ifindex; +__u32 prog_btf_fd; +__u32 prog_flags; + +__u32 func_info_rec_size; +const void *func_info; +__u32 func_info_cnt; + +__u32 line_info_rec_size; +const void *line_info; +__u32 line_info_cnt; + +__u32 log_level; +char *log_buf; +size_t log_buf_sz; +}; + +int libbpf__bpf_prog_load (const struct bpf_prog_load_params *load_attr); +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,
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v6 12/14 libbpf: support attachment of BPF tracing programs to kernel modules
In reply to Andrii NakryikoTeach libbpf to search for BTF types in kernel modules for tracing BPF programs. This allows attachment of raw_tp/fentry/fexit/fmod_ret/etc BPF program types to tracepoints and functions in kernel modules.
--- tools/lib/bpf/bpf.c | 5 +- tools/lib/bpf/libbpf.c | 138 +++++++++++++++++++++++++------- tools/lib/bpf/libbpf_internal.h | 1 + 3 files changed, 112 insertions(+), 32 deletions(-)
tools/lib/bpf/bpf.c +4 -1
@@ -231,8 +231,11 @@ int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr) attr.prog_type = load_attr->prog_type; attr.expected_attach_type = load_attr->expected_attach_type; +if (load_attr->attach_prog_fd) + attr.attach_prog_fd = load_attr->attach_prog_fd; +else + attr.attach_btf_obj_fd = load_attr->attach_btf_obj_fd; attr.attach_btf_id = load_attr->attach_btf_id; - attr.attach_prog_fd = load_attr->attach_prog_fd; attr.prog_ifindex = load_attr->prog_ifindex; attr.kern_version = load_attr->kern_version;
tools/lib/bpf/libbpf.c +107 -31
244 lines not on this page · raw message
tools/lib/bpf/libbpf_internal.h +1 -0
@@ -160,6 +160,7 @@ struct bpf_prog_load_params {const char *license;__u32 kern_version;__u32 attach_prog_fd; +__u32 attach_btf_obj_fd;__u32 attach_btf_id;__u32 prog_ifindex;__u32 prog_btf_fd;
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v6 13/14 selftests/bpf: add tp_btf CO-RE reloc test for modules
In reply to Andrii NakryikoAdd another CO-RE relocation test for kernel module relocations. This time for tp_btf with direct memory reads.
--- .../selftests/bpf/prog_tests/core_reloc.c | 3 +- .../bpf/progs/test_core_reloc_module.c | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +2 -1
@@ -514,7 +514,8 @@ static struct core_reloc_test_case test_cases[] = { },/* validate we can find kernel module BTF types for relocs/attach */ - MODULES_CASE("module", "raw_tp/bpf_testmod_test_read", "bpf_testmod_test_read"), +MODULES_CASE ("module_probed" ,"raw_tp/bpf_testmod_test_read" ,"bpf_testmod_test_read" ), + MODULES_CASE("module_direct", "tp_btf/bpf_testmod_test_read", NULL),/* validate BPF program can use multiple flavors to match against * single target BTF type
tools/testing/selftests/bpf/progs/test_core_reloc_module.c +31 -1
@@ -36,7 +36,7 @@ struct core_reloc_module_output { }; SEC("raw_tp/bpf_testmod_test_read") -int BPF_PROG (test_core_module , +int BPF_PROG (test_core_module_probed ,struct task_struct *task,struct bpf_testmod_test_read_ctx *read_ctx) {
@@ -64,3 +64,33 @@ int BPF_PROG(test_core_module,return 0 ; } + +SEC("tp_btf/bpf_testmod_test_read") +int BPF_PROG (test_core_module_direct , +struct task_struct *task, +struct bpf_testmod_test_read_ctx *read_ctx) +{ +struct core_reloc_module_output *out = (void *)&data.out; +__u64 pid_tgid =bpf_get_current_pid_tgid (); +__u32 real_tgid = (__u32)(pid_tgid >>32 ); +__u32 real_pid = (__u32 )pid_tgid; + +if (data.my_pid_tgid != pid_tgid) +return 0 ; + +if (task->pid != real_pid || task->tgid != real_tgid) +return 0 ; + + out->len = read_ctx->len; + out->off = read_ctx->off; + + out->read_ctx_sz = bpf_core_type_size(struct bpf_testmod_test_read_ctx); + out->read_ctx_exists = bpf_core_type_exists(struct bpf_testmod_test_read_ctx); + out->buf_exists =bpf_core_field_exists (read_ctx->buf); + out->off_exists =bpf_core_field_exists (read_ctx->off); + out->len_exists =bpf_core_field_exists (read_ctx->len); + + out->comm_len =BPF_CORE_READ_STR_INTO (&out->comm, task, comm); + +return 0 ; +}
Signature · 1 line
2.24.1
Andrii Nakryiko
maintainerPATCH v6 14/14 selftests/bpf: add fentry/fexit/fmod_ret selftest for kernel module
In reply to Andrii NakryikoAdd new selftest checking attachment of fentry/fexit/fmod_ret (and raw tracepoint ones for completeness) BPF programs to kernel module function.
--- .../selftests/bpf/prog_tests/module_attach.c | 53 +++++++++++++++ .../selftests/bpf/progs/test_module_attach.c | 66 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/module_attach.c create mode 100644 tools/testing/selftests/bpf/progs/test_module_attach.c
tools/testing/selftests/bpf/prog_tests/module_attach.c new file +53 -0
@@ -0,0 +1,53 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ + +#include <test_progs.h> +#include "test_module_attach.skel.h" + +static int duration; + +static int trigger_module_test_read (int read_sz) +{ +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 , read_sz); +close (fd); + +return 0 ; +} + +void test_module_attach (void ) +{ +const int READ_SZ =456 ; +struct test_module_attach * skel; +struct test_module_attach__bss *bss; +int err; + + skel =test_module_attach__open_and_load (); +if (CHECK (!skel,"skel_open" ,"failed to open skeleton\n" )) +return ; + + bss = skel->bss; + + err =test_module_attach__attach (skel); +if (CHECK (err,"skel_attach" ,"skeleton attach failed: %d\n" , err)) + goto cleanup; + +/* trigger tracepoint */ +ASSERT_OK (trigger_module_test_read (READ_SZ ),"trigger_read" ); + +ASSERT_EQ (bss->raw_tp_read_sz,READ_SZ ,"raw_tp" ); +ASSERT_EQ (bss->tp_btf_read_sz,READ_SZ ,"tp_btf" ); +ASSERT_EQ (bss->fentry_read_sz,READ_SZ ,"fentry" ); +ASSERT_EQ (bss->fexit_read_sz,READ_SZ ,"fexit" ); +ASSERT_EQ (bss->fexit_ret, -EIO ,"fexit_tet" ); +ASSERT_EQ (bss->fmod_ret_read_sz,READ_SZ ,"fmod_ret" ); + +cleanup: +test_module_attach__destroy (skel); +}
tools/testing/selftests/bpf/progs/test_module_attach.c new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
2.24.1