mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	linux-kernel@vger.kernel.org, x86@kernel.org,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Quentin Monnet <qmo@kernel.org>,
	Alan Maguire <almagui@kernel.org>
Subject: [PATCH RFC bpf-next 13/31] tbpf: Add support to attach inlined functions in modules with kprobe multi link
Date: Wed,  2 Dec 2026 13:30:17 +0100	[thread overview]
Message-ID: <20261202123035.10335-14-jolsa@kernel.org> (raw)
In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org>

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<section>, 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 <jolsa@kernel.org>
---
 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<section> 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


  parent reply	other threads:[~2026-12-02 12:32 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:30 [PATCH RFC bpf-next 00/31] bpf: Attach kprobe multi link to inlined functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 01/31] x86/ptrace: Add regs_get_dwarf_register function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 02/31] kprobes: Make the kprobe hash table size configurable Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 03/31] kprobes: Add enable_kprobes to enable multiple kprobes at once Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 04/31] module: Add module_sect_addr function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 05/31] bpf: Add bpf_get_btf_vmlinux_inline function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 06/31] bpf: Use goto labels for error path in bpf_kprobe_multi_link_attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 07/31] bpf: Add support to attach inlined functions with kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 08/31] bpf: Add cookie support for kprobe multi inlined functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 09/31] bpf: Add link info " Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 10/31] bpf: Add fdinfo " Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 11/31] bpf: Add btf_for_each_module_inline function Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 12/31] bpf: Change get_modules_for_addrs to use struct modules_array Jiri Olsa
2026-12-02 12:30 ` Jiri Olsa [this message]
2026-12-02 12:30 ` [PATCH RFC bpf-next 14/31] bpf: Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 15/31] bpf: Add get_func_arg[_cnt] support for kprobe multi functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 16/31] bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 17/31] bpf: Add log messages for kprobe multi inline attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 18/31] bpf: Add inlined functions for kprobe multi inline tests Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 19/31] libbpf: Add inline options to bpf_kprobe_multi_opts Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 20/31] libbpf: Print kernel log for failed kprobe multi attach Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 21/31] bpftool: Display inlined functions for kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 22/31] selftests/bpf: Add inlined functions to bpf_testmod Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 23/31] selftests/bpf: Add kprobe multi inline attach test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 24/31] selftests/bpf: Add kprobe multi inline mixed test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 25/31] selftests/bpf: Add kprobe multi inline skip test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 26/31] selftests/bpf: Add kprobe multi inline fails test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 27/31] selftests/bpf: Add kprobe multi inline module test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 28/31] selftests/bpf: Add kprobe multi inline strict test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 29/31] selftests/bpf: Add kprobe multi inline args test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 30/31] selftests/bpf: Add kprobe multi inline bench attach test Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 31/31] selftests/bpf: Add kprobe multi inline link info test Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202123035.10335-14-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=almagui@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=qmo@kernel.org \
    --cc=song@kernel.org \
    --cc=x86@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®