The BPF_F_KPROBE_MULTI_INLINE flag attaches inlined instances found in vmlinux inline BTF. Add support to attach also inlined instances in kernel modules. The module inline BTF has LOCSEC for each section with inlined instances, named as inline
, with offsets relative to the section start. Sections that are not in module text, like freed init sections, are skipped. The function is matched by name in module BTF and its base vmlinux BTF, so kernel function inlined in module code is attached as well. Modules with inlined instances are stored in the link, so they stay loaded as long as the link exists. The link info inline_ids value of instance in module is the BTF id from the inline BTF of that module. Signed-off-by: Jiri Olsa --- kernel/trace/bpf_trace.c | 70 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index bd413d45ef2c..71da54a6ea20 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2873,8 +2873,8 @@ static bool has_module(struct modules_array *arr, struct module *mod) } /* - * Add modules for @addrs to @arr. On error all the modules in @arr - * are released. + * Add modules for @addrs to @arr, which might already have modules + * with inlined instances. On error all the modules in @arr are released. */ static int get_modules_for_addrs(struct modules_array *arr, unsigned long *addrs, u32 addrs_cnt) { @@ -2959,6 +2959,7 @@ struct inline_lookup { /* Output */ struct inline_sites *inlines; + struct modules_array *mods; u32 addrs_cnt; /* Lookup state */ @@ -3015,6 +3016,58 @@ static int inline_lookup_locsec(struct inline_lookup *lookup, const struct btf * return 0; } +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +static int inline_lookup_module(struct module *mod, const struct btf *btf, void *data) +{ + u32 id, nr_types = btf_nr_types(btf); + struct inline_lookup *lookup = data; + const struct btf_type *t; + u32 cnt = lookup->cnt; + unsigned long base; + const char *name; + int err; + + /* + * Each section with inlined instances has its own LOCSEC named + * as inline
with offsets relative to the section start. + */ + for (id = btf_nr_types(btf_base_btf(btf)); id < nr_types; id++) { + t = btf_type_by_id(btf, id); + if (btf_kind(t) != BTF_KIND_LOCSEC) + continue; + name = btf_name_by_offset(btf, t->name_off); + if (!name || !str_has_prefix(name, "inline")) + continue; + /* Skip sections that are not loaded or are freed already (init). */ + base = module_sect_addr(mod, name + strlen("inline")); + if (!base || !within_module_mem_type(base, mod, MOD_TEXT)) + continue; + err = inline_lookup_locsec(lookup, btf, t, base); + if (err) + return err; + } + + if (lookup->cnt == cnt) + return 0; + + /* Keep the module we have inlined instances in. */ + err = add_module(lookup->mods, mod); + if (!err) + __module_get(mod); + return err; +} + +static int inline_lookup_modules(struct inline_lookup *lookup) +{ + return btf_for_each_module_inline(inline_lookup_module, lookup); +} +#else +static int inline_lookup_modules(struct inline_lookup *lookup) +{ + return 0; +} +#endif + /* * Sort instances by symbol name and address, so instances of each * symbol are next to each other, and get rid of duplicates. @@ -3051,9 +3104,11 @@ static bool inline_site_traceable(unsigned long addr) } /* - * Find inlined instances for functions in @lookup->syms. Each function - * must have an entry address or at least one inlined instance, functions - * without an entry address are dropped from @lookup->addrs. + * Find inlined instances for functions in @lookup->syms in kernel and + * modules, modules with instances are stored in @lookup->mods with + * reference taken. Each function must have an entry address or at least + * one inlined instance, functions without an entry address are dropped + * from @lookup->addrs. */ static int inline_lookup_symbols(struct inline_lookup *lookup) { @@ -3075,6 +3130,10 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) if (id < 0) return -EOPNOTSUPP; + err = inline_lookup_modules(lookup); + if (err) + goto error; + err = inline_lookup_locsec(lookup, btf, btf_type_by_id(btf, id), (unsigned long)_stext); if (err) goto error; @@ -3298,6 +3357,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr if ((flags & BPF_F_KPROBE_MULTI_INLINE) && (!err || err == -ESRCH)) { struct inline_lookup lookup = { .inlines = &inlines, + .mods = &mods, .syms = us.syms, .syms_cnt = cnt, .addrs = addrs, -- 2.56.1