The Linux Kernel Mailing List
 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 16/31] bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions
Date: Wed,  2 Dec 2026 13:30:20 +0100	[thread overview]
Message-ID: <20261202123035.10335-17-jolsa@kernel.org> (raw)
In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org>

Adding bpf_get_func_arg and bpf_get_func_arg_cnt helpers support for
inlined instances attached with BPF_F_KPROBE_MULTI_INLINE flag.

The program attached to inlined instance gets pt_regs of the probed
instruction, where the arguments are not in the argument registers.
Their locations are described in inline BTF, so store the location
prototype of each instance and use it in the helpers.

The register value is read with regs_get_dwarf_register function, so
the support is at the moment limited to x86_64.

The bpf_get_func_arg helper fails with:

  -EINVAL     for argument index out of the number of arguments
  -ENOENT     for argument without location (likely optimized out)
  -EFAULT     if the argument can't be read from its location
  -EOPNOTSUPP for unsupported location

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 include/uapi/linux/bpf.h       |  13 ++--
 kernel/trace/bpf_trace.c       | 132 ++++++++++++++++++++++++++++++---
 tools/include/uapi/linux/bpf.h |  13 ++--
 3 files changed, 137 insertions(+), 21 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 863b911c4bc5..eab777547afd 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -5681,13 +5681,16 @@ union bpf_attr {
  *		returned in **value**.
  *
  *		For kprobe multi programs get **n**-th argument (zero based) of
- *		the traced function.
+ *		the traced function or of its inlined instance.
  *
  *	Return
  *		0 on success.
  *		**-EINVAL** if n >= argument register count of traced function.
- *		**-EOPNOTSUPP** for kprobe multi programs executed in return
- *		probe or for inlined instances.
+ *		**-ENOENT** if the argument of inlined instance has no location.
+ *		**-EFAULT** if the argument of inlined instance can't be read.
+ *		**-EOPNOTSUPP** if the location of inlined instance argument is
+ *		not supported or for kprobe multi programs executed in return
+ *		probe.
  *
  * long bpf_get_func_ret(void *ctx, u64 *value)
  *	Description
@@ -5704,12 +5707,12 @@ union bpf_attr {
  *		function arguments are stored in these registers.
  *
  *		For kprobe multi programs get number of arguments of the traced
- *		function.
+ *		function or of its inlined instance.
  *
  *	Return
  *		The number of argument registers of the traced function.
  *		0 for kprobe multi programs attached with
- *		**BPF_F_KPROBE_MULTI_RETURN** flag or for inlined instances.
+ *		**BPF_F_KPROBE_MULTI_RETURN** flag.
  *
  * int bpf_get_retval(void)
  *	Description
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index f9b63cf5f66f..af7d258d7357 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2382,10 +2382,13 @@ struct bpf_kprobe_multi_link;
 struct inline_site {
 	struct kprobe kp;
 	struct bpf_kprobe_multi_link *link;
+	const struct btf *btf;
+	const struct btf_type *loc_proto;
 	u64 cookie;
 	u32 sym_idx;
 	u32 func_id;
 	bool has_entry;
+	bool is_vmlinux;
 	bool traceable;
 };
 
@@ -2733,6 +2736,109 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)
 	return run_ctx->entry_ip;
 }
 
+/*
+ * Get @n-th argument of the inlined instance @site from its location
+ * described in inline BTF, @regs are registers of the probed instruction.
+ */
+static int inline_site_arg(const struct inline_site *site, struct pt_regs *regs,
+			   u32 n, u64 *value)
+{
+	const struct btf_loc_param *p;
+	const struct btf_type *t;
+	u32 id, flags, vlen;
+	unsigned long regval;
+	u64 val, mem = 0;
+	int err;
+
+	if (n >= btf_type_vlen(site->loc_proto))
+		return -EINVAL;
+
+	/* There's no location for the argument, it's likely optimized out. */
+	id = btf_loc_proto_params(site->loc_proto)[n];
+	if (!id)
+		return -ENOENT;
+
+	/*
+	 * The location is BTF_KIND_LOC_PARAM type, its size is the size
+	 * of the argument and its flags say how to get the argument value
+	 * from the vlen number of 32-bit values that follow.
+	 */
+	t = btf_type_by_id(site->btf, id);
+	p = btf_loc_param(t);
+	vlen = btf_type_vlen(t);
+	flags = p->flags;
+
+	/* The value needs to fit in u64. */
+	if (t->size > sizeof(u64))
+		return -EOPNOTSUPP;
+
+	/*
+	 * The argument is constant, stored in the first value, 64-bit
+	 * constant is stored in two values in {lo, hi} order.
+	 */
+	if (flags & BTF_LOC_PARAM_CONST) {
+		val = p->values[0];
+		if (vlen > 1)
+			val |= (u64)p->values[1] << 32;
+		if (flags & BTF_LOC_PARAM_ADDR) {
+			/* The address is relative to the kernel image start. */
+			if (!site->is_vmlinux)
+				return -EOPNOTSUPP;
+			val += (unsigned long)_text;
+		} else if (flags & BTF_LOC_PARAM_SIGNED) {
+			/* Sign extend the constant from the argument size. */
+			val = sign_extend64(val, t->size * 8 - 1);
+		}
+		*value = val;
+		return 0;
+	}
+
+	/*
+	 * The rest of the locations is based on register, the first value
+	 * is its DWARF number.
+	 */
+
+	/* Value stored in two registers. */
+	if (flags == BTF_LOC_PARAM_REG && vlen > 1)
+		return -EOPNOTSUPP;
+
+	/*
+	 * Fails for registers we don't have value for, which includes
+	 * frame base register (BTF_LOC_PARAM_FBREG).
+	 */
+	err = regs_get_dwarf_register(regs, p->values[0], &regval);
+	if (err)
+		return err;
+	val = regval;
+
+	/*
+	 * The offset follows the register number, it's either signed
+	 * 32-bit value or 64-bit value in {lo, hi} order.
+	 */
+	if (flags & BTF_LOC_PARAM_OFFSET) {
+		if (vlen > 2)
+			val += (u64)p->values[2] << 32 | p->values[1];
+		else
+			val += (s32)p->values[1];
+	}
+
+	/*
+	 * Without dereference the argument is the register value (plus
+	 * offset) itself, otherwise it's address the argument is read from.
+	 */
+	if (flags & BTF_LOC_PARAM_DEREF) {
+		if (copy_from_kernel_nofault(&mem, (void *)(unsigned long)val, t->size))
+			return -EFAULT;
+		/* On big endian the argument was read to the high bytes of mem. */
+		if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
+			mem >>= (sizeof(mem) - t->size) * 8;
+		val = mem;
+	}
+
+	*value = val;
+	return 0;
+}
+
 /*
  * Get number of arguments of the function we got executed for
  * through its entry probe.
@@ -2759,9 +2865,8 @@ BPF_CALL_3(bpf_get_func_arg_kprobe_multi, struct pt_regs *, regs, u32, n, u64 *,
 
 	run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx,
 			       session_ctx.run_ctx);
-	/* Arguments of inlined instances are not supported. */
 	if (run_ctx->site)
-		return -EOPNOTSUPP;
+		return inline_site_arg(run_ctx->site, regs, n, value);
 	/* The arguments are not available in return probe. */
 	if (run_ctx->session_ctx.is_return)
 		return -EOPNOTSUPP;
@@ -2791,9 +2896,8 @@ BPF_CALL_1(bpf_get_func_arg_cnt_kprobe_multi, struct pt_regs *, regs)
 
 	run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx,
 			       session_ctx.run_ctx);
-	/* Arguments of inlined instances are not supported. */
 	if (run_ctx->site)
-		return 0;
+		return btf_type_vlen(run_ctx->site->loc_proto);
 
 	cnt = kprobe_multi_entry_args_cnt(run_ctx);
 	return cnt < 0 ? 0 : cnt;
@@ -3057,8 +3161,9 @@ struct inline_lookup {
 	u32 cap;
 };
 
-static int inline_lookup_add(struct inline_lookup *lookup, unsigned long addr,
-			     u32 sym_idx, u32 func_id)
+static int inline_lookup_add(struct inline_lookup *lookup, const struct btf *btf,
+			     const struct btf_loc *loc, unsigned long addr,
+			     u32 sym_idx, bool is_vmlinux)
 {
 	struct inline_site *sites;
 	u32 cap;
@@ -3077,8 +3182,11 @@ static int inline_lookup_add(struct inline_lookup *lookup, unsigned long addr,
 
 	lookup->sites[lookup->cnt++] = (struct inline_site) {
 		.kp.addr = (kprobe_opcode_t *)addr,
+		.btf = btf,
+		.loc_proto = btf_type_by_id(btf, loc->loc_proto),
 		.sym_idx = sym_idx,
-		.func_id = func_id,
+		.func_id = loc->func,
+		.is_vmlinux = is_vmlinux,
 	};
 	return 0;
 }
@@ -3088,7 +3196,8 @@ static int inline_lookup_add(struct inline_lookup *lookup, unsigned long addr,
  * the offsets of its locations are relative to @base address.
  */
 static int inline_lookup_locsec(struct inline_lookup *lookup, const struct btf *btf,
-				const struct btf_type *locsec, unsigned long base)
+				const struct btf_type *locsec, unsigned long base,
+				bool is_vmlinux)
 {
 	struct btf_loc *loc;
 	int idx, err;
@@ -3098,7 +3207,7 @@ static int inline_lookup_locsec(struct inline_lookup *lookup, const struct btf *
 		idx = inline_sym_idx(btf, loc, lookup->syms, lookup->syms_cnt);
 		if (idx < 0)
 			continue;
-		err = inline_lookup_add(lookup, base + loc->offset, idx, loc->func);
+		err = inline_lookup_add(lookup, btf, loc, base + loc->offset, idx, is_vmlinux);
 		if (err)
 			return err;
 	}
@@ -3131,7 +3240,7 @@ static int inline_lookup_module(struct module *mod, const struct btf *btf, void
 		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);
+		err = inline_lookup_locsec(lookup, btf, t, base, false);
 		if (err)
 			return err;
 	}
@@ -3266,7 +3375,8 @@ static int inline_lookup_symbols(struct inline_lookup *lookup)
 	if (err)
 		goto error;
 
-	err = inline_lookup_locsec(lookup, btf, btf_type_by_id(btf, id), (unsigned long)_stext);
+	err = inline_lookup_locsec(lookup, btf, btf_type_by_id(btf, id),
+				   (unsigned long)_stext, true);
 	if (err)
 		goto error;
 
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 863b911c4bc5..eab777547afd 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -5681,13 +5681,16 @@ union bpf_attr {
  *		returned in **value**.
  *
  *		For kprobe multi programs get **n**-th argument (zero based) of
- *		the traced function.
+ *		the traced function or of its inlined instance.
  *
  *	Return
  *		0 on success.
  *		**-EINVAL** if n >= argument register count of traced function.
- *		**-EOPNOTSUPP** for kprobe multi programs executed in return
- *		probe or for inlined instances.
+ *		**-ENOENT** if the argument of inlined instance has no location.
+ *		**-EFAULT** if the argument of inlined instance can't be read.
+ *		**-EOPNOTSUPP** if the location of inlined instance argument is
+ *		not supported or for kprobe multi programs executed in return
+ *		probe.
  *
  * long bpf_get_func_ret(void *ctx, u64 *value)
  *	Description
@@ -5704,12 +5707,12 @@ union bpf_attr {
  *		function arguments are stored in these registers.
  *
  *		For kprobe multi programs get number of arguments of the traced
- *		function.
+ *		function or of its inlined instance.
  *
  *	Return
  *		The number of argument registers of the traced function.
  *		0 for kprobe multi programs attached with
- *		**BPF_F_KPROBE_MULTI_RETURN** flag or for inlined instances.
+ *		**BPF_F_KPROBE_MULTI_RETURN** flag.
  *
  * int bpf_get_retval(void)
  *	Description
-- 
2.56.1


  parent reply	other threads:[~2026-12-02 12:33 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 ` [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 12:30 ` Jiri Olsa [this message]
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-17-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