All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Jiri Olsa" <jolsa@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: Re: [PATCH RFC bpf-next 00/31] bpf: Attach kprobe multi link to inlined functions
Date: Thu, 03 Dec 2026 01:57:14 +0000	[thread overview]
Message-ID: <DNAQ3Q8ARTHT.OWF2VDJ3X7T8@gmail.com> (raw)
In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org>

On Wed, Dec 02, 2026 at 04:30 AM Jiri Olsa <jolsa@kernel.org> wrote:
> This patchset adds BPF_F_KPROBE_MULTI_INLINE flag that attaches also
> the inlined instances of the functions in the syms array. The
> instances are resolved from inline BTF (CONFIG_DEBUG_INFO_BTF_INLINE)
> of vmlinux and modules and each gets its own kprobe.

I like the idea. kprobes behind the kprobe multi link is the piece
that was missing.
But the lookup by name in the kernel is kinda heavy.

In Alan's RFC thread I leaned towards the kernel doing the whole thing.
The reason: kprobe multi could not attach to arbitrary IPs.
Patch 7 shows that it can. That changes the trade off.

Lets start with the mechanism only.
addrs[] takes any address that kprobes accept.
fprobe gets the ftrace locations. Every other address gets a kprobe.
Such addrs fail with -ENOENT in fprobe_init() today, so no need for
a flag.
User space reads vmlinux.inline from sysfs and passes the sites.

libbpf works this way already. It resolves "tcp_*" in user space and
passes addrs. In this series wildcards can't use inline (patch 19).
To match fully inlined functions libbpf has to read vmlinux.inline
anyway.

                     this series              addrs[]
  -----------------  -----------------------  -------------------------
  name -> sites      kernel, exact names      user space, wildcards too
  subset of sites    no                       yes
  vmlinux.inline     2nd copy, ~10 Mbytes,    mmap in user space
                     never freed
  cookie             per function             per site
  args               bpf_get_func_arg()       cookie -> arg spec, like
                                              usdt.bpf.h
  arch               x86-64                   any with kprobes
  link info          5 new fields             as is
  fdinfo             2 new lines              as is, %pS
  bpftool            new table, BTF lookup    print sym+off
  new flags          2                        0

Your use case from Alan's v2 thread works too: trace foo() only where
it is inlined in bar(). User space picks the sites.

Then there is no need for BTF parsing in the kernel, module_sect_addr(),
regs_get_dwarf_register(), inline_* in bpf_link_info, STRICT, etc.
Patches 2 and 3 stay.

If tracers show that the lookup has to be in the kernel we add it
on top of addrs[]. uapi doesn't go the other way.

What the diff doesn't do:
It is all or none. imo that's the right default.
It doesn't say which address kprobes refuse. The link_create log can
name it. A flag to skip such addresses can come later. link info shows
what is attached.
It calls register_kprobes(). enable_kprobes() goes on top.
Two functions inlined at one address share one cookie.

The kernel side is below. It compiles, I didn't attach anything with it:

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 2031c37..57c6e91 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2360,6 +2360,16 @@ struct bpf_session_run_ctx {
 };

 #ifdef CONFIG_FPROBE
+struct bpf_kprobe_multi_link;
+
+/* For an address in link->addrs that is not an ftrace location. */
+struct bpf_kprobe_multi_kprobe {
+	struct kprobe kp;
+	struct bpf_kprobe_multi_link *link;
+	/* As in link->addrs. register_kprobe() can move kp.addr behind endbr. */
+	unsigned long addr;
+};
+
 struct bpf_kprobe_multi_link {
 	struct bpf_link link;
 	struct fprobe fp;
@@ -2368,6 +2378,9 @@ struct bpf_kprobe_multi_link {
 	u32 cnt;
 	u32 mods_cnt;
 	struct module **mods;
+	struct bpf_kprobe_multi_kprobe *kprobes;
+	struct kprobe **kps;
+	u32 kps_cnt;
 };

 struct bpf_kprobe_multi_run_ctx {
@@ -2459,6 +2472,7 @@ static void bpf_kprobe_multi_link_release(struct bpf_link *link)
 	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
 	/* Don't wait for RCU GP here. */
 	unregister_fprobe_async(&kmulti_link->fp);
+	unregister_kprobes(kmulti_link->kps, kmulti_link->kps_cnt);
 	kprobe_multi_put_modules(kmulti_link->mods, kmulti_link->mods_cnt);
 }

@@ -2467,12 +2481,24 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)
 	struct bpf_kprobe_multi_link *kmulti_link;

 	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
+	kvfree(kmulti_link->kprobes);
+	kvfree(kmulti_link->kps);
 	kvfree(kmulti_link->addrs);
 	kvfree(kmulti_link->cookies);
 	kfree(kmulti_link->mods);
 	kfree(kmulti_link);
 }

+static unsigned long kprobe_multi_missed(const struct bpf_kprobe_multi_link *link)
+{
+	unsigned long missed = link->fp.nmissed;
+	u32 i;
+
+	for (i = 0; i < link->kps_cnt; i++)
+		missed += link->kprobes[i].kp.nmissed;
+	return missed;
+}
+
 static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
 						struct bpf_link_info *info)
 {
@@ -2490,7 +2516,7 @@ static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link,
 	kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);
 	info->kprobe_multi.count = kmulti_link->cnt;
 	info->kprobe_multi.flags = kmulti_link->link.flags;
-	info->kprobe_multi.missed = kmulti_link->fp.nmissed;
+	info->kprobe_multi.missed = kprobe_multi_missed(kmulti_link);

 	if (!uaddrs)
 		return 0;
@@ -2537,7 +2563,7 @@ static void bpf_kprobe_multi_show_fdinfo(const struct bpf_link *link,
 		   "kprobe_cnt:\t%u\n"
 		   "missed:\t%lu\n",
 		   kmulti_link->cnt,
-		   kmulti_link->fp.nmissed);
+		   kprobe_multi_missed(kmulti_link));

 	seq_printf(seq, "%s\t %s\n", "cookie", "func");
 	for (int i = 0; i < kmulti_link->cnt; i++) {
@@ -2621,7 +2647,7 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
 static __always_inline int
 kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
 			   unsigned long entry_ip, struct ftrace_regs *fregs,
-			   bool is_return, void *data)
+			   struct pt_regs *regs, bool is_return, void *data)
 {
 	struct bpf_kprobe_multi_run_ctx run_ctx = {
 		.session_ctx = {
@@ -2632,11 +2658,10 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
 		.entry_ip = entry_ip,
 	};
 	struct bpf_run_ctx *old_run_ctx;
-	struct pt_regs *regs;
 	int err;

 	/*
-	 * graph tracer framework ensures we won't migrate, so there is no need
+	 * fprobe and kprobe handlers don't migrate, so there is no need
 	 * to use migrate_disable for bpf_prog_run again. The check here just for
 	 * __this_cpu_inc_return.
 	 */
@@ -2649,11 +2674,14 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link,
 	}

 	rcu_read_lock();
-	regs = ftrace_partial_regs(fregs, bpf_kprobe_multi_pt_regs_ptr());
+	/* fprobe has fregs, kprobe has regs */
+	if (fregs)
+		regs = ftrace_partial_regs(fregs, bpf_kprobe_multi_pt_regs_ptr());
 	old_run_ctx = bpf_set_run_ctx(&run_ctx.session_ctx.run_ctx);
 	err = bpf_prog_run(link->link.prog, regs);
 	bpf_reset_run_ctx(old_run_ctx);
-	ftrace_partial_regs_update(fregs, bpf_kprobe_multi_pt_regs_ptr());
+	if (fregs)
+		ftrace_partial_regs_update(fregs, bpf_kprobe_multi_pt_regs_ptr());
 	rcu_read_unlock();

  out:
@@ -2671,7 +2699,7 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,

 	link = container_of(fp, struct bpf_kprobe_multi_link, fp);
 	err = kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip),
-					 fregs, false, data);
+					 fregs, NULL, false, data);
 	return is_kprobe_session(link->link.prog) ? err : 0;
 }

@@ -2684,8 +2712,18 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,

 	link = container_of(fp, struct bpf_kprobe_multi_link, fp);
 	kprobe_multi_link_prog_run(link, ftrace_get_entry_ip(fentry_ip),
-				   fregs, true, data);
+				   fregs, NULL, true, data);
+}
+
+static int kprobe_multi_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
+{
+	struct bpf_kprobe_multi_kprobe *mkp;
+
+	mkp = container_of(kp, struct bpf_kprobe_multi_kprobe, kp);
+	kprobe_multi_link_prog_run(mkp->link, mkp->addr, NULL, regs, false, NULL);
+	return 0;
 }
+NOKPROBE_SYMBOL(kprobe_multi_kprobe_handler);

 static int symbols_cmp_r(const void *a, const void *b, const void *priv)
 {
@@ -2804,6 +2842,73 @@ static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)
 	return 0;
 }

+/* fprobe takes the ftrace locations. Every other address gets a kprobe. */
+static int kprobe_multi_register(struct bpf_kprobe_multi_link *link)
+{
+	u32 i, cnt = link->cnt, fp_cnt = 0, kp_cnt = 0;
+	struct bpf_kprobe_multi_kprobe *mkp;
+	unsigned long *addrs;
+	int err = 0;
+
+	addrs = kvmalloc_objs(*addrs, cnt);
+	if (!addrs)
+		return -ENOMEM;
+
+	/* ftrace locations fill addrs from the start, the rest from the end. */
+	for (i = 0; i < cnt; i++) {
+		if (ftrace_location(link->addrs[i]))
+			addrs[fp_cnt++] = link->addrs[i];
+		else
+			addrs[cnt - ++kp_cnt] = link->addrs[i];
+	}
+
+	if (kp_cnt) {
+		/*
+		 * No return probe, session and override in the middle of
+		 * a function. Fail the way fprobe_init() does today.
+		 */
+		if (link->fp.exit_handler || link->link.prog->kprobe_override) {
+			err = -ENOENT;
+			goto out;
+		}
+
+		/* bpf_kprobe_multi_link_dealloc() frees both. */
+		link->kprobes = kvcalloc(kp_cnt, sizeof(*link->kprobes), GFP_KERNEL);
+		link->kps = kvmalloc_objs(*link->kps, kp_cnt);
+		if (!link->kprobes || !link->kps) {
+			err = -ENOMEM;
+			goto out;
+		}
+
+		for (i = 0; i < kp_cnt; i++) {
+			mkp = &link->kprobes[i];
+			mkp->link = link;
+			mkp->addr = addrs[fp_cnt + i];
+			mkp->kp.addr = (kprobe_opcode_t *)mkp->addr;
+			mkp->kp.pre_handler = kprobe_multi_kprobe_handler;
+			link->kps[i] = &mkp->kp;
+		}
+
+		/* All or none. */
+		err = register_kprobes(link->kps, kp_cnt);
+		if (err)
+			goto out;
+	}
+
+	if (fp_cnt) {
+		err = register_fprobe_ips(&link->fp, addrs, fp_cnt);
+		if (err) {
+			unregister_kprobes(link->kps, kp_cnt);
+			goto out;
+		}
+	}
+
+	link->kps_cnt = kp_cnt;
+out:
+	kvfree(addrs);
+	return err;
+}
+
 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
 {
 	struct bpf_kprobe_multi_link *link = NULL;
@@ -2944,7 +3049,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr
 	}
 	link->mods_cnt = err;

-	err = register_fprobe_ips(&link->fp, addrs, cnt);
+	err = kprobe_multi_register(link);
 	if (err) {
 		kprobe_multi_put_modules(link->mods, link->mods_cnt);
 		bpf_link_cleanup(&link_primer);

pw-bot: cr

      parent reply	other threads:[~2026-12-03  1:57 UTC|newest]

Thread overview: 41+ 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-03  1:50   ` Alexei Starovoitov
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-03  1:51   ` Alexei Starovoitov
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 ` [PATCH RFC bpf-next 13/31] tbpf: Add support to attach inlined functions in modules with kprobe multi link Jiri Olsa
2026-12-02 12:30 ` [PATCH RFC bpf-next 14/31] bpf: Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for " 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 13:06   ` sashiko-bot
2026-12-03  1:53   ` Alexei Starovoitov
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:55   ` sashiko-bot
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:51   ` sashiko-bot
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 13:00   ` sashiko-bot
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:50   ` sashiko-bot
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
2026-12-03  1:57 ` Alexei Starovoitov [this message]

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=DNAQ3Q8ARTHT.OWF2VDJ3X7T8@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=almagui@kernel.org \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jolsa@kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.