From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796214756; b=AA0DsiiQLTygESy0/8yCt7eDQVVJVM8hWLPtJv0gMjv39+HeE4tGcJNMLebqcbzryLJ3 udBil2qKwWsrn+fmTEom8J1DjVnEIdAY+TqY28FeJqasUtSaMdd3oJyyksh6aNXBMmLB6 ZQoAIvTBSM4WfB8oMLb286xM8NvmxsvW9sEpgx/4hbYq1BZA369wmp9l3CoyMTzrGww23 d9LhYflmdE9Vbi7g8U1i4WskbUhdDqy4TIl2eBVvy+rekZyYwr+4KVD4Q1bUf4PMrqs7D wAW0nyx2YcDXrb51hNv7ng5K0F7Jo8ulhm1MFBAH26OqxgjKUdrhP9jJc0N6WcmcZPQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214756; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=ZzTxmQ0DUVlTegHLmnwP5biOJ3e8L9oXvVc71NUep0U=; b=nX3V+o/PP1+VR4zQeUT7NZ02dkS91PLSJFrC6tE9SFYF49BDzxJnY0rFOzdYPyuNX/69 T+RXh1KFSsBIz8d5jUeT3PLKoYRP3yPP3NUsyvMpFYd4kHo2ia7FOQTxIOuZEQAP9v9ah Yj6yXXYb/8voDZDNZNLDdImQ+GuyLeKv/b2jYuQdGsG/RO33Wl4Y+5cEiRHTvsW5RG2TH pgwbpVila7al5gEFxBrpWYCCKr/78IqR2HH1luw7xUk9HRHOy0bsLai0qMlU+SGIEICfB FJTqrR7TBfmlZ2j41epGF7Cm1jfB9ZKBwKKR2a190yfrlaSCniXisOW28C/4PRTsuVg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=m3h6mc0K; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15C91606A32D; Wed, 2 Dec 2026 12:32:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=m3h6mc0K Received: by smtp.kernel.org (Postfix) with ESMTPSA id A64731F00897; Wed, 2 Dec 2026 12:32:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796214755; bh=ZzTxmQ0DUVlTegHLmnwP5biOJ3e8L9oXvVc71NUep0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=m3h6mc0KMQoekONEu+KF3QAAL2VNbo3F4fqowzz3QcFQbggEisa1cJmbMP+/mmXS2 jtTP+EmdI0UYeSp+9j6/cQvyvo/ioOYXue8koqUy7SCYpBh3M6IY+WZvEslCVcRn9s bibierTQuTk92AWAVBs4JZffBR8r1M4GJe7EOpLa+qcWZ1nOMx5gQj9iSzxSTZcCot ZdRSWCDAcl9WaQH7kc+ykrP8TzJOBrZgSfH4a3KZzEbYpHPj2oc0ufXoLPaB6u0+8v dc6wQo1eX2sH35Gmsr7hadtxtKChgQWvDu45rfKypleDZHVcivdqJf7vCQUKq9uhCh WdX8Q/b/d5zWQ== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , Quentin Monnet , Alan Maguire Subject: [PATCH RFC bpf-next 11/31] bpf: Add btf_for_each_module_inline function Date: Wed, 2 Dec 2026 13:30:15 +0100 Message-ID: <20261202123035.10335-12-jolsa@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org> References: <20261202123035.10335-1-jolsa@kernel.org> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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