For "mod:func" libbpf looks func up in the BTF of the module, and that lookup searches the types of vmlinux first. If vmlinux has a function of that name too, libbpf takes its ID, and the program attaches to the module function with the vmlinux prototype. The kernel now refuses this when the module has a function of its own of that name. A glob of a tracing multi program for a module walks the types of vmlinux too, and takes both IDs for such a name. Look up the function of the module first, and take the one of vmlinux only if the module has no function of that name. The dedup of BTF merges a module function of the same prototype into the vmlinux one, which is then the type of the module function. Fixes: 8f8a024272f3 ("libbpf: support "module: Function" syntax for tracing programs") Fixes: f2aa370dfe57 ("libbpf: Add support to create tracing multi link") Signed-off-by: Anton Protopopov --- tools/lib/bpf/libbpf.c | 73 +++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8ed4786e0e51..b75b94add227 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11869,6 +11869,13 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, */ if (ret < 0 || ret >= sizeof(btf_type_name)) return -ENAMETOOLONG; + /* + * A function of a module first, then the one of vmlinux, which the + * dedup of BTF merges a module function of the same prototype into + */ + ret = btf__find_by_name_kind_own(btf, btf_type_name, kind); + if (ret > 0 || !btf__base_btf(btf)) + return ret; return btf__find_by_name_kind(btf, btf_type_name, kind); } @@ -13887,40 +13894,60 @@ bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t) return true; } +static int add_func_id_by_glob(const struct btf *btf, __u32 type_id, const char *pattern, + bool skip_own, __u32 **ids, size_t *cap, size_t *cnt) +{ + const struct btf_type *t = btf__type_by_id(btf, type_id); + const char *name; + + if (btf_kind(t) != BTF_KIND_FUNC) + return 0; + name = btf__name_by_offset(btf, t->name_off); + if (!name || !glob_match(name, pattern)) + return 0; + if (!btf_type_is_traceable_func(btf, t)) + return 0; + if (skip_own && btf__find_by_name_kind_own(btf, name, BTF_KIND_FUNC) > 0) + return 0; + + if (libbpf_ensure_mem((void **) ids, cap, sizeof(**ids), *cnt + 1)) + return -ENOMEM; + (*ids)[(*cnt)++] = type_id; + return 0; +} + static int collect_btf_func_ids_by_glob(const struct btf *btf, const char *pattern, __u32 **ids) { - __u32 type_id, nr_types = btf__type_cnt(btf); + const struct btf *base_btf = btf__base_btf(btf); + __u32 type_id, start_id, nr_types = btf__type_cnt(btf); size_t cap = 0, cnt = 0; + int err; if (!pattern) return -EINVAL; - for (type_id = 1; type_id < nr_types; type_id++) { - const struct btf_type *t = btf__type_by_id(btf, type_id); - const char *name; - int err; - - if (btf_kind(t) != BTF_KIND_FUNC) - continue; - name = btf__name_by_offset(btf, t->name_off); - if (!name) - continue; - - if (!glob_match(name, pattern)) - continue; - if (!btf_type_is_traceable_func(btf, t)) - continue; - - err = libbpf_ensure_mem((void **) ids, &cap, sizeof(**ids), cnt + 1); - if (err) { - free(*ids); - return -ENOMEM; - } - (*ids)[cnt++] = type_id; + /* + * The functions of a module first, then the ones of vmlinux, which the + * dedup of BTF merges module functions of the same prototype into + */ + start_id = base_btf ? btf__type_cnt(base_btf) : 1; + for (type_id = start_id; type_id < nr_types; type_id++) { + err = add_func_id_by_glob(btf, type_id, pattern, false, ids, &cap, &cnt); + if (err) + goto err_out; + } + for (type_id = 1; type_id < start_id; type_id++) { + err = add_func_id_by_glob(btf, type_id, pattern, true, ids, &cap, &cnt); + if (err) + goto err_out; } return cnt; + +err_out: + free(*ids); + return err; } static int collect_func_ids_by_glob(const struct bpf_program *prog, const char *pattern, __u32 **ids) -- 2.43.0