Add btf_for_each_module_inline() that calls the provided callback for each live module with inline BTF, the split BTF on top of module BTF that describes inline sites in the module. At the moment we keep just relocated inline BTF data for the module, which is exposed in sysfs. Parse it on first use and keep the parsed inline BTF cached until the module is gone, so there's no cost unless something asks for it. The module reference is held just for the callback, so the callback needs to get its own reference to keep using the module. Signed-off-by: Jiri Olsa --- include/linux/btf.h | 11 ++++++++ kernel/bpf/btf.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/linux/btf.h b/include/linux/btf.h index 9007da300ca0..dcc1cb4a93bf 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -256,6 +256,17 @@ bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t); s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind); s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p); struct btf *btf_get_module_btf(const struct module *module); + +typedef int (*btf_module_inline_fn)(struct module *mod, const struct btf *btf, void *data); + +#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE) +int btf_for_each_module_inline(btf_module_inline_fn fn, void *data); +#else +static inline int btf_for_each_module_inline(btf_module_inline_fn fn, void *data) +{ + return 0; +} +#endif __u32 btf_relocate_id(const struct btf *btf, __u32 id); const struct btf_type *btf_type_skip_modifiers(const struct btf *btf, u32 id, u32 *res_id); diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 72eba56e9a5e..683539f8ea6b 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -9412,6 +9412,9 @@ struct btf_module { struct bin_attribute *sysfs_attr; struct bin_attribute *sysfs_inline_attr; void *btf_inline_data; + u32 btf_inline_data_size; + /* Parsed on first use, check btf_for_each_module_inline. */ + struct btf *inline_btf; int flags; }; @@ -9587,6 +9590,7 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op, btf_mod->module = module; btf_mod->btf = btf; btf_mod->btf_inline_data = relocated_inline_data; + btf_mod->btf_inline_data_size = inline_data_size; list_add(&btf_mod->list, &btf_modules); mutex_unlock(&btf_module_mutex); @@ -9646,6 +9650,8 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op, if (btf_mod->sysfs_inline_attr) sysfs_btf_remove(btf_mod->sysfs_inline_attr); kvfree(btf_mod->btf_inline_data); + if (btf_mod->inline_btf) + btf_free(btf_mod->inline_btf); purge_cand_cache(btf_mod->btf); btf_put(btf_mod->btf); kfree(btf_mod); @@ -9670,6 +9676,64 @@ static int __init btf_module_init(void) } fs_initcall(btf_module_init); + +#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE) +static struct btf *btf_module_inline_btf(struct btf_module *btf_mod) +{ + struct btf_verifier_env *env; + struct btf *btf; + + lockdep_assert_held(&btf_module_mutex); + + if (btf_mod->inline_btf) + return btf_mod->inline_btf; + + env = kzalloc_obj(*env, GFP_KERNEL | __GFP_NOWARN); + if (!env) + return ERR_PTR(-ENOMEM); + + env->log.level = BPF_LOG_KERNEL; + btf = btf_parse_split(env, btf_mod->module->name, btf_mod->btf_inline_data, + btf_mod->btf_inline_data_size, btf_mod->btf); + btf_verifier_env_free(env); + if (!IS_ERR(btf)) + btf_mod->inline_btf = btf; + return btf; +} + +/* + * Call @fn for each live module with inline BTF, the split BTF on top + * of module BTF that describes inline sites in the module. + * + * The inline BTF is parsed on first use and stays cached until the + * module is gone. The module reference is held just for the @fn call, + * so @fn needs to get its own to keep using the module or its BTF. + * + * The iteration stops when @fn returns non zero value, which is then + * returned. + */ +int btf_for_each_module_inline(btf_module_inline_fn fn, void *data) +{ + struct btf_module *btf_mod; + struct btf *btf; + int err = 0; + + mutex_lock(&btf_module_mutex); + list_for_each_entry(btf_mod, &btf_modules, list) { + if (!btf_mod->btf_inline_data || !(btf_mod->flags & BTF_MODULE_F_LIVE)) + continue; + if (!try_module_get(btf_mod->module)) + continue; + btf = btf_module_inline_btf(btf_mod); + err = IS_ERR(btf) ? PTR_ERR(btf) : fn(btf_mod->module, btf, data); + module_put(btf_mod->module); + if (err) + break; + } + mutex_unlock(&btf_module_mutex); + return err; +} +#endif /* IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE) */ #endif /* CONFIG_DEBUG_INFO_BTF_MODULES */ #if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE) -- 2.56.1