* [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering
@ 2026-11-23 22:32 Alan Maguire
2026-11-23 22:32 ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence Alan Maguire
` (7 more replies)
0 siblings, 8 replies; 12+ messages in thread
From: Alan Maguire @ 2026-11-23 22:32 UTC (permalink / raw)
To: ast, daniel, andrii
Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, john.fastabend, bpf, Alan Maguire
Currently the BPF_PROG() family of macros greatly facilitates
BPF tracing/LSM program writing by allowing specification
of function, tracepoint or LSM hook arguments directly by
name and type. These are then mapped to a context struct
consisting of a u64 array, with one value for each argument,
with by-value structs > 8 bytes taking two slots.
By instead creating a structure with named members, we can
bring a Compile Once, Run Everywhere experience to parameter
specifications via a new BPF_PROG_CORE() macro. This will
better handle cases like the following:
Problem: A tracing program only needs to use one parameter
from a multi-parameter kernel function signature, but verification
can fail if the function signature changes due to added or removed
parameters which are irrelevant to the case in hand.
Solution: Only specify the needed parameter in BPF_PROG_CORE()
Example:
/*
* Prototype for filename_unlinkat is:
* int filename_unlinkat(int dfd, struct filename * name);
*/
SEC("fentry/filename_unlinkat")
int BPF_PROG_CORE(handle, struct filename *, name)
{
if (bpf_func_arg_exists(name))
...
Problem: Ordering of parameters changes for a BPF LSM program
Solution: Again use BPF_PROG_CORE() which is resilient to
ordering changes as these become CO-RE relocations in the
context structure. Return values should be tagged with
__bpf_func_ret .
Problem: A parameter type changes
Solution: Use ___<new> named variants with old and new
types:
int BPF_PROG_CORE(handle, struct vm_area_struct *vma,
struct mm_area *vma___new)
{
if (bpf_func_arg_exists(vma___new))
...
else if (bpf_func_arg_exists(vma)
Because the solution is purely in libbpf, the support needs
no kernel changes, aside from the light skeleton case,
where CO-RE relocation is done in-kernel (support for the
latter is added in patch 2). Patch 5 provides a test
that programs can use to see if support is present.
Support covers fentry, fexit, fmod_ret, fsession, tp_btf,
lsm_cgroup and BPF LSM programs.
In short, we are making use of BPF CO-RE to extend a CO-RE
experience to program parameter ordering, presence and
handling of parameter type changes.
Limitations: tp_btf programs require >= 5.10 kernel since
it is the first kernel that makes tp_btf parameter names
available, while on big-endian (s390x) platforms, signed values
smaller than 8 bytes cannot be read.
This support requires BTF declaration tag support in clang/gcc
to work; because gcc does not support declaration tags on types
we add a marker within the struct itself.
Patch 1 adds libbpf support.
Patch 2 adds kernel-based lskel support.
Patch 3 tests tracing program support.
Patch 4 tests BPF LSM support.
Patch 5 provides a test BPF program writers can use -
bpf_func_args_supported() - to probe for support.
Patch 6 tests that probe.
Patch 7 documents the support.
Alan Maguire (7):
libbpf: Support CO-RE relocation of tracing program arguments
bpf: Support CO-RE relocation of tracing program arguments
selftests/bpf: Test CO-RE relocation of tracing program arguments
selftests/bpf: Test CO-RE relocation of BPF LSM program arguments
libbpf: Report function argument relocation support via type existence
selftests/bpf: Test detection of function argument relocation support
Documentation/bpf: Describe CO-RE relocation of function arguments
Documentation/bpf/libbpf/libbpf_overview.rst | 190 ++++++++
Documentation/bpf/llvm_reloc.rst | 95 ++++
include/linux/bpf.h | 2 +
kernel/bpf/btf.c | 38 ++
kernel/bpf/check_btf.c | 1 +
tools/lib/bpf/bpf_tracing.h | 195 ++++++++
tools/lib/bpf/libbpf.c | 142 +++++-
tools/lib/bpf/relo_core.c | 458 +++++++++++++++++-
tools/lib/bpf/relo_core.h | 29 ++
tools/testing/selftests/bpf/Makefile.skel | 3 +-
.../selftests/bpf/prog_tests/func_args_core.c | 325 +++++++++++++
.../selftests/bpf/progs/func_args_core.c | 241 +++++++++
.../selftests/bpf/progs/func_args_core_lsm.c | 156 ++++++
.../bpf/progs/func_args_core_lsm_cgroup.c | 40 ++
14 files changed, 1905 insertions(+), 10 deletions(-)
create mode 100644 tools/testing/selftests/bpf/prog_tests/func_args_core.c
create mode 100644 tools/testing/selftests/bpf/progs/func_args_core.c
create mode 100644 tools/testing/selftests/bpf/progs/func_args_core_lsm.c
create mode 100644 tools/testing/selftests/bpf/progs/func_args_core_lsm_cgroup.c
--
2.56.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence
2026-11-23 22:32 [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
@ 2026-11-23 22:32 ` Alan Maguire
2026-11-23 22:38 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 6/7] selftests/bpf: Test detection of function argument relocation support Alan Maguire
` (6 subsequent siblings)
7 siblings, 1 reply; 12+ messages in thread
From: Alan Maguire @ 2026-11-23 22:32 UTC (permalink / raw)
To: ast, daniel, andrii
Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, john.fastabend, bpf, Alan Maguire
Function argument structs (see __bpf_func_args) are relocated against the
attach target by whatever applies the program's CO-RE relocations at load
time, which needn't match the bpf_tracing.h the program was built with.
A loader without support for function argument relocations treats such a
struct as an ordinary local type, for which a same-named kernel type is
looked up, so all of its members appear to be missing. As BPF_PROG_CORE()
guards each argument read with a field existence check, such programs
still load, but all arguments silently read as zero, which can't be told
apart from the arguments not existing in the attach target.
When the program is loaded by the libbpf version whose headers it was built
with (e.g., a vendored or statically linked libbpf), support is a given.
It isn't with:
- light skeletons, as the kernel applies the relocations, so support
depends on the running kernel;
- objects built against newer headers than the libbpf loading them, e.g.,
an older shared libbpf provided by the distribution;
- loaders other than libbpf that implement CO-RE relocations themselves.
Additionally, explicitly declared structs lose their "bpf_func_args" BTF
decl tag when built with GCC without __bpf_func_args_marker (which
BPF_PROG_CORE() always adds), so they are treated as ordinary local types
as well. And for tp_btf programs on kernels before 5.10, whose BTF has no
parameter names for tracepoints, there is no candidate to match against.
Allow programs to detect this, by having BPF_CORE_TYPE_EXISTS relocations
against function argument structs resolve to 1 when they are matched
against the attach target (otherwise, the lookup by name finds nothing,
resolving to 0), and add bpf_func_args_supported() to bpf_tracing.h to
check it, using bpf_core_type_exists(), which handles the differences
between clang's and GCC's __builtin_preserve_type_info(). It tells
whether the struct is relocated against the attach target, not which of
its members exist. Other type-based relocations against such structs
remain unsupported.
As relo_core.c is shared with the kernel, this also applies to light
skeletons on kernels with this change, while on kernels without support
for function argument relocations, bpf_func_args_supported() resolves to
0, as intended.
Assisted-by: LLM
Signed-off-by: Alan Maguire <almagui@kernel.org>
---
tools/lib/bpf/bpf_tracing.h | 19 +++++++++++++++++++
tools/lib/bpf/relo_core.c | 11 +++++++++--
2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index 24182b48fa4d..f08164ce4f3c 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -721,6 +721,25 @@ struct pt_regs;
#else
#define __bpf_func_args __attribute__((preserve_access_index))
#endif
+/*
+ * bpf_func_args_supported() tells whether the __bpf_func_args struct that
+ * args points to is relocated against the parameters of the program's attach
+ * target. That is done by whatever applies the program's CO-RE relocations,
+ * which needn't match this header. With the libbpf version providing this
+ * header, it is 1, except for tp_btf programs on kernels before Linux 5.10,
+ * whose BTF has no parameter names for tracepoints. It is also 0 for light
+ * skeletons on kernels without that support, with older libbpf versions
+ * (e.g., a distribution's shared libbpf), with other loaders lacking that
+ * support, and if the struct lost its BTF decl tag (e.g., when built with GCC
+ * without __bpf_func_args_marker). Whenever it is 0, all members appear to be
+ * missing (BPF_PROG_CORE() arguments read as zero). It doesn't tell which
+ * members exist. Note that loaders without that support look for a same-named
+ * kernel type instead, so the struct's name shouldn't be that of a kernel
+ * type. Within BPF_PROG_CORE() programs, use bpf_func_args_supported(ctx).
+ * Requires bpf_core_read.h.
+ */
+#define bpf_func_args_supported(args) bpf_core_type_exists(typeof(*(args)))
+
#define __bpf_func_args_marker \
char ___bpf_func_args[0] __attribute__((btf_decl_tag("bpf_func_args")))
#define __bpf_func_ret __attribute__((btf_decl_tag("bpf_func_ret")))
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 2f6627952220..ed566a55cb06 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -907,8 +907,15 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
targ_spec->relo_kind = local_spec->relo_kind;
if (func_args) {
- /* only field accesses (args->x...) of the arguments struct
- * have a meaning for function argument candidates
+ /* the existence of an arguments struct indicates that it is
+ * matched against the attach target, as only libbpf and
+ * kernel versions supporting that find a candidate for it,
+ * allowing programs to detect that support
+ */
+ if (local_spec->relo_kind == BPF_CORE_TYPE_EXISTS)
+ return 1;
+ /* otherwise, only field accesses (args->x...) have a meaning
+ * for function argument candidates
*/
if (!core_relo_is_field_based(local_spec->relo_kind) ||
local_spec->len < 2 || local_spec->spec[0].idx != 0)
--
2.56.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH bpf-next 6/7] selftests/bpf: Test detection of function argument relocation support
2026-11-23 22:32 [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
2026-11-23 22:32 ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence Alan Maguire
@ 2026-11-23 22:32 ` Alan Maguire
2026-11-23 22:36 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 7/7] Documentation/bpf: Describe CO-RE relocation of function arguments Alan Maguire
` (5 subsequent siblings)
7 siblings, 1 reply; 12+ messages in thread
From: Alan Maguire @ 2026-11-23 22:32 UTC (permalink / raw)
To: ast, daniel, andrii
Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, john.fastabend, bpf, Alan Maguire
Check that bpf_func_args_supported() returns 1 for function argument
structs, both explicitly declared (including one relying only on the
struct-level BTF decl tag when built with clang) and declared by
BPF_PROG_CORE(), in fentry, fexit, LSM and LSM cgroup programs, with
both skeletons and light skeletons, i.e., when relocated by libbpf and
by the kernel.
Also check that it returns 0 for a struct lacking the "bpf_func_args"
tag, as all structs would appear to libbpf or kernel versions without
support for function argument relocations, or when built with GCC
without __bpf_func_args_marker.
Assisted-by: LLM
Signed-off-by: Alan Maguire <almagui@kernel.org>
---
.../selftests/bpf/prog_tests/func_args_core.c | 6 ++++++
.../testing/selftests/bpf/progs/func_args_core.c | 15 ++++++++++++++-
.../selftests/bpf/progs/func_args_core_lsm.c | 8 ++++++--
.../bpf/progs/func_args_core_lsm_cgroup.c | 3 ++-
4 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/func_args_core.c b/tools/testing/selftests/bpf/prog_tests/func_args_core.c
index f4cc859f20cd..e63591b6ccf5 100644
--- a/tools/testing/selftests/bpf/prog_tests/func_args_core.c
+++ b/tools/testing/selftests/bpf/prog_tests/func_args_core.c
@@ -35,6 +35,9 @@ static void trigger(int fentry_prog_fd, int fmod_ret_prog_fd)
ASSERT_EQ(skel->data->t3_fentry_ret_exists, 0, "t3_fentry_ret_exists"); \
ASSERT_EQ(skel->data->t3_ret_exists, 1, "t3_ret_exists"); \
ASSERT_EQ(skel->bss->t3_ret, 15, "t3_ret"); \
+ ASSERT_EQ(skel->data->t3_supported, 1, "t3_supported"); \
+ ASSERT_EQ(skel->data->t3_untagged_supported, 0, "t3_untagged_supported"); \
+ ASSERT_EQ(skel->data->m3_supported, 1, "m3_supported"); \
\
ASSERT_EQ(skel->bss->fm_calls, 1, "fm_calls"); \
ASSERT_EQ(skel->bss->fm_a, 1, "fm_a"); \
@@ -160,6 +163,8 @@ static __u64 lsm_ioctl(int *monitored_pid)
ASSERT_EQ(skel->data->s_ret, 0, "s_ret"); \
ASSERT_EQ(skel->data->s_ret_exists, 1, "s_ret_exists"); \
ASSERT_EQ(skel->data->s_added_exists, 0, "s_added_exists"); \
+ ASSERT_EQ(skel->data->s_supported, 1, "s_supported"); \
+ ASSERT_EQ(skel->data->m_supported, 1, "m_supported"); \
\
/* file___new (struct vfs_file *) doesn't match, file is used instead */ \
ASSERT_EQ(skel->data->v_new_exists, 0, "v_new_exists"); \
@@ -230,6 +235,7 @@ static void test_lsm_lskel(void)
ASSERT_EQ(skel->data->lc_kern, 0, "lc_kern"); \
ASSERT_EQ(skel->data->lc_ret_exists, 1, "lc_ret_exists"); \
ASSERT_EQ(skel->data->lc_added_exists, 0, "lc_added_exists"); \
+ ASSERT_EQ(skel->data->lc_supported, 1, "lc_supported"); \
})
static void trigger_lsm_cgroup(void)
diff --git a/tools/testing/selftests/bpf/progs/func_args_core.c b/tools/testing/selftests/bpf/progs/func_args_core.c
index 4656a6c642d1..cf2785d9349f 100644
--- a/tools/testing/selftests/bpf/progs/func_args_core.c
+++ b/tools/testing/selftests/bpf/progs/func_args_core.c
@@ -32,11 +32,23 @@ struct fentry_test3_args {
__u64 t3_a, t3_b, t3_c, t3_b_alias, t3_ret;
int t3_missing_exists = -1, t3_bad_type_exists = -1, t3_missing_flavor_exists = -1;
+int t3_supported = -1, t3_untagged_supported = -1;
+
+/* without the bpf_func_args tag, as for libbpf or kernel versions without
+ * support for function argument relocations; no kernel type has this name
+ */
+struct func_args_core_untagged {
+ int a;
+} __attribute__((preserve_access_index));
int t3_fentry_ret_exists = -1, t3_ret_exists = -1;
SEC("fentry/bpf_fentry_test3")
int fentry3(struct fentry_test3_args *args)
{
+ struct func_args_core_untagged *untagged = (void *)args;
+
+ t3_supported = bpf_func_args_supported(args);
+ t3_untagged_supported = bpf_func_args_supported(untagged);
t3_a = args->a;
t3_b = args->b;
t3_c = args->c;
@@ -185,12 +197,13 @@ int tp_read(struct test_read_args *args)
/* BPF_PROG_CORE() versions, with the arguments struct declared implicitly */
__u64 m3_a, m3_b, m3_c, m3_missing = -1, m3_ret;
-int m3_missing_exists = -1, m3_ret_exists = -1;
+int m3_missing_exists = -1, m3_ret_exists = -1, m3_supported = -1;
SEC("fexit/bpf_fentry_test3")
int BPF_PROG_CORE(fexit3_macro, __u64, c, int, b, char, a, long, missing,
__bpf_func_ret int, ret)
{
+ m3_supported = bpf_func_args_supported(ctx);
m3_a = a;
m3_b = b;
m3_c = c;
diff --git a/tools/testing/selftests/bpf/progs/func_args_core_lsm.c b/tools/testing/selftests/bpf/progs/func_args_core_lsm.c
index f4b9e68e4578..53f39275614a 100644
--- a/tools/testing/selftests/bpf/progs/func_args_core_lsm.c
+++ b/tools/testing/selftests/bpf/progs/func_args_core_lsm.c
@@ -22,7 +22,7 @@ struct file_ioctl_args {
} __bpf_func_args;
__u64 s_arg, s_ino;
-int s_ret = -1, s_ret_exists = -1, s_added_exists = -1, s_calls;
+int s_ret = -1, s_ret_exists = -1, s_added_exists = -1, s_calls, s_supported = -1;
SEC("lsm/file_ioctl")
int ioctl_struct(struct file_ioctl_args *args)
@@ -30,6 +30,8 @@ int ioctl_struct(struct file_ioctl_args *args)
if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
return 0;
+ s_supported = bpf_func_args_supported(args);
+
s_calls++;
s_arg = args->arg;
s_ino = args->file->f_inode->i_ino;
@@ -88,7 +90,7 @@ int BPF_PROG_CORE(ioctl_macro_variants, struct vfs_file *, file___new,
}
__u64 m_arg, m_cmd, m_added = -1;
-int m_ret = -1, m_ret_exists = -1, m_added_exists = -1, m_calls;
+int m_ret = -1, m_ret_exists = -1, m_added_exists = -1, m_calls, m_supported = -1;
SEC("lsm/file_ioctl")
int BPF_PROG_CORE(ioctl_macro, unsigned long, arg, __bpf_func_ret int, ret,
@@ -97,6 +99,8 @@ int BPF_PROG_CORE(ioctl_macro, unsigned long, arg, __bpf_func_ret int, ret,
if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
return 0;
+ m_supported = bpf_func_args_supported(ctx);
+
m_calls++;
m_arg = arg;
m_cmd = cmd;
diff --git a/tools/testing/selftests/bpf/progs/func_args_core_lsm_cgroup.c b/tools/testing/selftests/bpf/progs/func_args_core_lsm_cgroup.c
index 2743c58f13aa..e2b149272556 100644
--- a/tools/testing/selftests/bpf/progs/func_args_core_lsm_cgroup.c
+++ b/tools/testing/selftests/bpf/progs/func_args_core_lsm_cgroup.c
@@ -20,7 +20,7 @@ struct socket_create_args {
} __bpf_func_args;
int lc_calls, lc_family = -1, lc_protocol = -1, lc_kern = -1;
-int lc_ret_exists = -1, lc_added_exists = -1;
+int lc_ret_exists = -1, lc_added_exists = -1, lc_supported = -1;
SEC("lsm_cgroup/socket_create")
int socket_create_struct(struct socket_create_args *args)
@@ -29,6 +29,7 @@ int socket_create_struct(struct socket_create_args *args)
return 1;
lc_calls++;
+ lc_supported = bpf_func_args_supported(args);
lc_family = args->family;
lc_protocol = args->protocol;
lc_kern = args->kern;
--
2.56.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH bpf-next 7/7] Documentation/bpf: Describe CO-RE relocation of function arguments
2026-11-23 22:32 [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
2026-11-23 22:32 ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence Alan Maguire
2026-11-23 22:32 ` [PATCH bpf-next 6/7] selftests/bpf: Test detection of function argument relocation support Alan Maguire
@ 2026-11-23 22:32 ` Alan Maguire
[not found] ` <20261123223227.745345-5-almagui@kernel.org>
` (4 subsequent siblings)
7 siblings, 0 replies; 12+ messages in thread
From: Alan Maguire @ 2026-11-23 22:32 UTC (permalink / raw)
To: ast, daniel, andrii
Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, john.fastabend, bpf, Alan Maguire
Document how BPF programs attached via BTF to kernel functions, LSM hooks
and tracepoints can access the arguments of their attach target in a
CO-RE-relocatable way:
- in the libbpf overview, for program authors: __bpf_func_args and
__bpf_func_ret structs (and __bpf_func_args_marker, needed with GCC),
BPF_PROG_CORE() and bpf_func_arg_exists(), how members are matched to
parameters (by name, ignoring flavors), how parameter type changes are
handled, including the stricter rules for pointers, the use of
__probestub_*() or __traceiter_*() for tracepoints (and kernels
before 5.10 having neither), the behaviour of light skeletons on
older kernels, and the limitations, including that signed arguments
smaller than 8 bytes generally can't be read on big-endian
architectures; also how programs can use
bpf_func_args_supported() to detect whether the arguments struct is
relocated against the attach target, which it isn't with light
skeletons on older kernels, with older libbpf versions than the
headers the program was built with, with other loaders lacking that
support, for GCC-built structs lacking __bpf_func_args_marker, or for
tracepoints on kernels before 5.10;
- in the description of CO-RE relocations, for authors of other BPF
loaders: the "bpf_func_args" (on the struct or one of its members) and
"bpf_func_ret" decl tags, how relocations against such structs are to
be resolved against the attach target's parameters and their context
slots, and that BPF_CORE_TYPE_EXISTS relocations against them resolve
to 1, which programs use to detect that support.
Signed-off-by: Alan Maguire <almagui@kernel.org>
Assisted-by: LLM
---
Documentation/bpf/libbpf/libbpf_overview.rst | 190 +++++++++++++++++++
Documentation/bpf/llvm_reloc.rst | 95 ++++++++++
2 files changed, 285 insertions(+)
diff --git a/Documentation/bpf/libbpf/libbpf_overview.rst b/Documentation/bpf/libbpf/libbpf_overview.rst
index f4d22f0c62b0..cdc489cb27f2 100644
--- a/Documentation/bpf/libbpf/libbpf_overview.rst
+++ b/Documentation/bpf/libbpf/libbpf_overview.rst
@@ -198,6 +198,196 @@ about the field that should be relocated on the target kernel. i.e, if the
``struct task_struct`` due to some new field added in front of it, libbpf will
automatically adjust the actual offset to the proper value.
+CO-RE for function arguments
+----------------------------
+
+BPF programs attached via BTF to kernel functions, LSM hooks and tracepoints
+(``fentry``, ``fexit``, ``fmod_ret``, ``fsession``, ``tp_btf``, ``lsm`` and
+``lsm_cgroup`` programs) receive the arguments of their attach target in their
+context, as an array of 64-bit slots. ``BPF_PROG()`` and ``BPF_PROG2()`` access
+these by position, so if parameters are added, removed or reordered in another
+kernel version, a program reads the wrong arguments.
+
+libbpf supports CO-RE relocation of these arguments instead. A struct marked
+with ``__bpf_func_args`` (from ``bpf_tracing.h``) describes the arguments a
+program needs, with members named after the target's parameters, in any order.
+The program takes a pointer to such a struct as its context:
+
+.. code-block:: C
+
+ struct unlinkat_args {
+ struct filename *name;
+ int dfd;
+ int ret __bpf_func_ret;
+ } __bpf_func_args;
+
+ SEC("fexit/filename_unlinkat")
+ int handle(struct unlinkat_args *args)
+ {
+ if (bpf_core_field_exists(args->dfd))
+ bpf_printk("dfd %d", args->dfd);
+ bpf_printk("ret %d", args->ret);
+ return 0;
+ }
+
+When the program is loaded, each member is matched by name against the
+parameters of the attach target, and accesses to it are relocated to the context
+slot of that parameter. A member marked ``__bpf_func_ret`` refers to the return
+value, which exists for ``fexit``, ``fmod_ret``, ``fsession``, ``lsm`` and
+``lsm_cgroup`` programs.
+
+GCC doesn't support BTF decl tags on types, so with GCC such a struct also
+needs to contain ``__bpf_func_args_marker``, a zero-sized member that carries
+the tag instead. Structs with both work with either compiler:
+
+.. code-block:: C
+
+ struct unlinkat_args {
+ __bpf_func_args_marker;
+ struct filename *name;
+ int dfd;
+ int ret __bpf_func_ret;
+ } __bpf_func_args;
+
+``BPF_PROG_CORE()`` offers the same with a ``BPF_PROG2()``-like interface,
+declaring the struct from ``(type, name)`` pairs (using
+``__bpf_func_args_marker``, so that it works with both clang and GCC) and
+passing the arguments to the program body as variables:
+
+.. code-block:: C
+
+ SEC("fexit/filename_unlinkat")
+ int BPF_PROG_CORE(handle, struct filename *, name, int, dfd,
+ __bpf_func_ret int, ret)
+ {
+ if (bpf_func_arg_exists(dfd))
+ bpf_printk("dfd %d", dfd);
+ bpf_printk("ret %d", ret);
+ return 0;
+ }
+
+Arguments that don't exist in the target read as zero, and
+``bpf_func_arg_exists()`` tells whether an argument exists. Within the program
+body, ``ctx`` points to the arguments struct.
+
+Members are matched to parameters as follows:
+
+* Names are compared ignoring any suffix starting with three underscores (a
+ "flavor", as for CO-RE types), so that several members can describe a
+ parameter that is renamed or changes type across kernel versions.
+
+* Members without a matching parameter don't exist: ``bpf_core_field_exists()``
+ returns 0 for them and accesses that aren't guarded by such a check fail to
+ load. Parameters without a matching member are ignored.
+
+* Member and parameter types have to be compatible following the usual CO-RE
+ rules for struct fields. Members of a different kind than their parameter
+ (e.g., an integer for a pointer, or for an enum) don't match. Integers match
+ regardless of size and signedness: when an unsigned integer changes size the
+ load is adjusted accordingly, while size changes involving signed integers
+ make accesses fail to load. Structs passed by value match, with accesses to
+ their members relocated like those of any other struct.
+
+* Unlike struct fields, pointers only match if the types they point to (ignoring
+ typedefs and qualifiers) are compatible: structs, unions and enums need the
+ same kind and name (forward declarations match structs and unions), integers
+ the same size and other types the same kind, recursing through pointers to
+ pointers, while ``void *`` matches any pointer. A parameter changing from,
+ e.g., ``struct file *`` to ``struct inode *`` thus appears as missing rather
+ than having accesses through it relocated against the wrong type, and flavors
+ can describe both variants:
+
+ .. code-block:: C
+
+ struct my_args {
+ struct vm_area_struct *vma;
+ struct mm_area *vma___new;
+ } __bpf_func_args;
+
+For ``tp_btf`` programs, as the ``btf_trace_<tracepoint>`` type has no parameter
+names, these are taken from the tracepoint's ``__probestub_<tracepoint>()``
+function (Linux 6.5 and later) or ``__traceiter_<tracepoint>()`` function
+(Linux 5.10 and later). On older kernels, there are no parameter names to
+match, so the program loads but all arguments appear to be missing, and
+``bpf_func_args_supported()`` (see below) returns 0.
+
+Light skeletons rely on the kernel to apply CO-RE relocations. Kernels that
+don't support function argument relocations find no match for any member, so
+all arguments appear to be missing.
+
+Function argument relocations are applied by whatever loads the program,
+which needn't match the ``bpf_tracing.h`` it was built with. When it is the
+libbpf version whose headers the program was built with (e.g., a vendored or
+statically linked libbpf), they are supported, except for ``tp_btf`` programs
+on kernels before Linux 5.10 (see above). Otherwise, all arguments appear to be
+missing (and read as zero in ``BPF_PROG_CORE()`` programs) with:
+
+* light skeletons on kernels that don't support function argument
+ relocations;
+* an older libbpf than the headers the program was built with, e.g., a shared
+ libbpf provided by the distribution;
+* loaders other than libbpf that apply CO-RE relocations themselves but don't
+ support function argument relocations.
+
+The same happens for explicitly declared structs built with GCC without
+``__bpf_func_args_marker`` (``BPF_PROG_CORE()`` always adds it). Programs can
+detect whether the arguments struct is relocated against the attach target with
+``bpf_func_args_supported()`` (``bpf_func_args_supported(ctx)`` in
+``BPF_PROG_CORE()`` programs; it uses ``bpf_core_type_exists()`` from
+``bpf_core_read.h``), and report or handle a lack of support:
+
+.. code-block:: C
+
+ SEC("fexit/filename_unlinkat")
+ int handle(struct unlinkat_args *args)
+ {
+ if (!bpf_func_args_supported(args)) {
+ unsupported = true;
+ return 0;
+ }
+ ...
+ }
+
+This tells whether the struct is relocated against the attach target at all,
+not which of its members exist. Without that support, a kernel type with the
+same name as the struct would be looked up instead, so arguments structs
+shouldn't be named like kernel types.
+
+Limitations:
+
+* Structs passed by value that are larger than 8 bytes can't be copied out of
+ the context as a whole, so they can't be ``BPF_PROG_CORE()`` arguments; use a
+ ``__bpf_func_args`` struct and access their members instead.
+
+* Compilers may access members of structs passed by value by adding a
+ (relocated) offset to the context pointer, rather than relocating the load
+ itself. The verifier only accepts such accesses since Linux 7.1.
+
+* When libbpf applies the relocations, accesses have to be made from the
+ program itself (or from inlined functions) rather than from subprograms, which
+ may be shared by programs with different attach targets.
+
+* Multi-attach programs and programs attached to other BPF programs (e.g.
+ ``freplace``) are not supported.
+
+* The verifier only allows context accesses at offsets that are a multiple of
+ 8 bytes. Members of structs passed by value can thus only be accessed if their
+ offset within the struct is a multiple of 8 bytes.
+
+* On big-endian architectures, arguments smaller than 8 bytes are in the
+ low-order bytes of their slot. Loads of integer and enum arguments are widened
+ to the whole slot instead, relying on the calling convention extending them to
+ 64 bits, but only zero-extending loads of unsigned arguments and
+ sign-extending loads (``-mcpu=v4``) of signed ones can be widened this way.
+ Other loads, e.g., zero-extending loads of signed integers, and accesses to
+ structs and unions passed by value fail to load; ``BPF_PROG()`` and
+ ``BPF_PROG2()`` can access such arguments. In practice, signed arguments and
+ return values smaller than 8 bytes, such as ``int``, can generally not be
+ read on big-endian architectures: compilers only use sign-extending loads
+ when the value is converted to a wider type, and ``BPF_PROG_CORE()`` always
+ loads arguments into variables of their own type, so it can't be used with
+ such arguments there at all.
+
Getting Started with libbpf
===========================
diff --git a/Documentation/bpf/llvm_reloc.rst b/Documentation/bpf/llvm_reloc.rst
index 44188e219d32..147cca6e45be 100644
--- a/Documentation/bpf/llvm_reloc.rst
+++ b/Documentation/bpf/llvm_reloc.rst
@@ -544,3 +544,98 @@ Enum-based relocations:
108: CO-RE <enumval_value> [16] enum bar::V = 1
35: *(u64 *)(r2 + 0x0) = r1
36: exit
+
+CO-RE Relocations of Function Arguments
+=======================================
+
+Programs attached via BTF to kernel functions and tracepoints
+(``BPF_PROG_TYPE_TRACING`` and ``BPF_PROG_TYPE_LSM`` programs with expected
+attach types ``BPF_TRACE_FENTRY``, ``BPF_TRACE_FEXIT``, ``BPF_TRACE_FSESSION``,
+``BPF_MODIFY_RETURN``, ``BPF_TRACE_RAW_TP``, ``BPF_LSM_MAC`` and
+``BPF_LSM_CGROUP``) can access their context through a struct describing the
+arguments of their attach target, with members named after its parameters.
+Such a struct is marked with a ``btf_decl_tag("bpf_func_args")`` BTF decl tag,
+either on the struct itself (``component_idx`` -1) or, as compilers such as GCC
+don't support decl tags on types, on one of its members (typically a
+zero-sized ``char ___bpf_func_args[0]`` member, which never matches a
+parameter). It is otherwise an ordinary struct accessed with
+``preserve_access_index``, so the compiler emits the usual field-based CO-RE
+relocations on the context loads:
+
+.. code-block:: c
+
+ struct args {
+ int b;
+ char a;
+ int ret __attribute__((btf_decl_tag("bpf_func_ret")));
+ } __attribute__((preserve_access_index, btf_decl_tag("bpf_func_args")));
+
+ /* int bpf_fentry_test3(char a, int b, u64 c) */
+ __attribute__((section("fexit/bpf_fentry_test3")))
+ int prog(struct args *args) {
+ return args->a + args->b + args->ret;
+ }
+
+The relocation's root type may refer to such a struct through typedefs and
+modifiers (e.g., clang uses the typedef as the root type of relocations against
+a typedef'd anonymous struct), so loaders skip these before looking for the
+tag. Rather than with same-named types, loaders resolve relocations against
+such a struct with the single candidate given by the program's attach target:
+
+* For ``BPF_TRACE_RAW_TP`` programs, whose attach target is the
+ ``btf_trace_<tracepoint>`` typedef, the parameters are those of the
+ ``__probestub_<tracepoint>`` function or, if there is none (before Linux
+ 6.5), of the ``__traceiter_<tracepoint>`` function, ignoring the first
+ (``void *__data``) one. If neither exists (before Linux 5.10), there is no
+ candidate, so no member matches.
+
+* For other programs, the parameters are those of the attach target function.
+
+* The first accessor of the relocation's access string has to be ``0``, and the
+ second one, a member of the struct, is matched by name against the
+ parameters, ignoring the ``___`` flavor suffix of the member's name. The
+ matching parameter is at the byte offset of its slot in the context, which
+ is an array of 8-byte slots, one per parameter, except for parameters larger
+ than 8 bytes passed by value (structs, unions or integers), which take as many
+ slots as their size rounded up to 8 bytes requires. A pointer always takes a
+ single slot. A parameter smaller than 8 bytes, including a struct or union
+ passed by value, is in the low-order bytes of its slot.
+
+* A member tagged with ``btf_decl_tag("bpf_func_ret")`` (``component_idx`` being
+ the member's index) refers to the return value, in the slot following those
+ of the parameters, but only for ``BPF_TRACE_FEXIT``, ``BPF_TRACE_FSESSION``,
+ ``BPF_MODIFY_RETURN``, ``BPF_LSM_MAC`` and ``BPF_LSM_CGROUP`` programs, and
+ for functions that don't return ``void``.
+
+* The member's and parameter's types have to be compatible as for struct
+ fields, except that pointers are only compatible if the types they point to,
+ after skipping typedefs and modifiers, are: structs, unions and enums need the
+ same kind and essential name (forward declarations being compatible with
+ structs and unions, and enums with 64-bit enums), integers the same size, and
+ other types the same kind, recursing through pointers to pointers, with
+ ``void *`` on either side compatible with any pointer.
+
+* Further accessors (members of structs passed by value) are resolved as for
+ any other struct.
+
+* On big-endian targets, as the verifier only allows 8-byte aligned context
+ accesses, a ``byte_off`` relocation of an integer or enum parameter (or return
+ value) smaller than 8 bytes that is applied to a load is resolved to the
+ start of its slot, with the load turned into an 8-byte ``BPF_MEM`` load. This
+ relies on the calling convention extending such values to 64 bits, so it is
+ only done for zero-extending (``BPF_MEM``) loads of unsigned and
+ sign-extending (``BPF_MEMSX``) loads of signed values of the parameter's size;
+ other such loads are poisoned.
+
+Members that have no match don't exist, as for missing struct fields. A
+``BPF_CORE_TYPE_EXISTS`` relocation against such a struct resolves to 1 if
+there is a candidate, so that programs can detect that the struct is relocated
+against the attach target, and to 0 otherwise (for ``BPF_TRACE_RAW_TP``
+programs without a ``__probestub_<tracepoint>`` or
+``__traceiter_<tracepoint>`` function). Other type-based relocations against
+it are not supported. In the example above, the ``byte_off`` relocations for
+``a``, ``b`` and ``ret`` are resolved to 0, 8 and 24 respectively.
+
+Both libbpf and the kernel (for relocations passed to ``BPF_PROG_LOAD``, as
+used by light skeletons) resolve these relocations, using the attach target
+passed to ``BPF_PROG_LOAD`` in the kernel's case.
--
2.56.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 6/7] selftests/bpf: Test detection of function argument relocation support
2026-11-23 22:32 ` [PATCH bpf-next 6/7] selftests/bpf: Test detection of function argument relocation support Alan Maguire
@ 2026-11-23 22:36 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-23 22:36 UTC (permalink / raw)
To: Alan Maguire; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/bpf: non-standard multi-line comment style in
func_args_core.c
--
commit 636af5484ed5c26ea73ebeb02b695ba9f266ad9c
Author: Alan Maguire <almagui@kernel.org>
selftests/bpf: Test detection of function argument relocation support
This commit adds selftest checks verifying that bpf_func_args_supported()
returns 1 for function argument structs in fentry, fexit, LSM and LSM
cgroup programs, and 0 for structs lacking the bpf_func_args tag.
> diff --git a/tools/testing/selftests/bpf/progs/func_args_core.c b/tools/testing/selftests/bpf/progs/func_args_core.c
> index 4656a6c642d10..cf2785d9349f9 100644
> --- a/tools/testing/selftests/bpf/progs/func_args_core.c
> +++ b/tools/testing/selftests/bpf/progs/func_args_core.c
> @@ -32,11 +32,23 @@ struct fentry_test3_args {
>
> __u64 t3_a, t3_b, t3_c, t3_b_alias, t3_ret;
> int t3_missing_exists = -1, t3_bad_type_exists = -1, t3_missing_flavor_exists = -1;
> +int t3_supported = -1, t3_untagged_supported = -1;
> +
> +/* without the bpf_func_args tag, as for libbpf or kernel versions without
> + * support for function argument relocations; no kernel type has this name
> + */
[Severity: Low]
This isn't a bug, but BPF coding style puts the opening /* of a
multi-line comment on its own line with the comment text starting on the
next line.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123223227.745345-1-almagui@kernel.org?part=6
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence
2026-11-23 22:32 ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence Alan Maguire
@ 2026-11-23 22:38 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-23 22:38 UTC (permalink / raw)
To: Alan Maguire; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] libbpf: multi-line comment style violation in
bpf_core_spec_match()
--
commit 4236a42c871ff888d9de43484f575fb4195ce34b
Author: Alan Maguire <almagui@kernel.org>
libbpf: Report function argument relocation support via type existence
This commit resolves BPF_CORE_TYPE_EXISTS relocations against function
argument structs to 1 when matched against the attach target, and adds
bpf_func_args_supported() to bpf_tracing.h so programs can detect whether
function argument relocation is supported by the loader.
> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> index 2f6627952220b..ed566a55cb068 100644
> --- a/tools/lib/bpf/relo_core.c
> +++ b/tools/lib/bpf/relo_core.c
> @@ -907,8 +907,15 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
> targ_spec->relo_kind = local_spec->relo_kind;
>
> if (func_args) {
> - /* only field accesses (args->x...) of the arguments struct
> - * have a meaning for function argument candidates
> + /* the existence of an arguments struct indicates that it is
> + * matched against the attach target, as only libbpf and
> + * kernel versions supporting that find a candidate for it,
> + * allowing programs to detect that support
> + */
> + if (local_spec->relo_kind == BPF_CORE_TYPE_EXISTS)
> + return 1;
> + /* otherwise, only field accesses (args->x...) have a meaning
> + * for function argument candidates
> */
[Severity: Low]
This isn't a bug, but BPF coding style puts the opening /* of multi-line
comments on its own line rather than placing comment text on the first
line.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123223227.745345-1-almagui@kernel.org?part=5
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM program arguments
[not found] ` <20261123223227.745345-5-almagui@kernel.org>
@ 2026-11-23 22:41 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-23 22:41 UTC (permalink / raw)
To: Alan Maguire; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] selftests/bpf: non-standard multi-line comment style in
func_args_core tests
--
commit d8c54394186071b26020206d3532b117ff6287ba
Author: Alan Maguire <almagui@kernel.org>
selftests/bpf: Test CO-RE relocation of BPF LSM program arguments
This commit adds BPF selftests covering CO-RE relocation of function
argument structs and BPF_PROG_CORE() macros for BPF_LSM and BPF_LSM_CGROUP
programs using both standard and light skeletons.
> diff --git a/tools/testing/selftests/bpf/prog_tests/func_args_core.c b/tools/testing/selftests/bpf/prog_tests/func_args_core.c
> index 1516ad13eaa26..f4cc859f20cdc 100644
> --- a/tools/testing/selftests/bpf/prog_tests/func_args_core.c
> +++ b/tools/testing/selftests/bpf/prog_tests/func_args_core.c
[ ... ]
> @@ -110,6 +118,179 @@ static void test_lskel(void)
> func_args_core_lskel__destroy(skel);
> }
>
> +/* An ioctl command that no file implements: the call fails after the
> + * file_ioctl hook ran, so whatever the programs return has no effect on the
> + * process (unlike, e.g., an mprotect() that they allow). Its top bit is set
> + * to check that the unsigned int argument isn't sign-extended.
> + */
> +#define LSM_IOCTL_CMD 0xb1f0c0deU
> +#define LSM_IOCTL_ARG 0x0123456789abcdefULL
> +
> +/* Run file_ioctl(file, cmd, arg) for an ioctl() on a memfd, only monitoring
> + * that call; returns the memfd's inode number, or 0 on failure.
> + */
> +static __u64 lsm_ioctl(int *monitored_pid)
[ ... ]
> diff --git a/tools/testing/selftests/bpf/progs/func_args_core_lsm.c b/tools/testing/selftests/bpf/progs/func_args_core_lsm.c
> new file mode 100644
> index 0000000000000..f4b9e68e45786
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/func_args_core_lsm.c
> @@ -0,0 +1,152 @@
[ ... ]
> +/* A hypothetical future kernel in which file_ioctl()'s file parameter
> + * points to a renamed struct: both variants are described with flavors,
> + * and only one of them can match in a given kernel.
> + */
> +struct vfs_file {
[ ... ]
> +/* The reverse: a program written for the hypothetical kernel, describing
> + * file with the new type and the current one with a flavor, so that only
> + * the flavored member matches.
> + */
> +struct file_ioctl_old_variant_args {
[Severity: Low]
This isn't a bug, but BPF coding style requires multi-line comments to
place the opening "/*" on its own line with the comment text starting on
the next line.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123223227.745345-1-almagui@kernel.org?part=4
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 3/7] selftests/bpf: Test CO-RE relocation of tracing program arguments
[not found] ` <20261123223227.745345-4-almagui@kernel.org>
@ 2026-11-23 22:43 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-23 22:43 UTC (permalink / raw)
To: Alan Maguire; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] selftests/bpf: unfiltered tracing hooks in
test_func_args_core() race under -j
- [Low] selftests/bpf: multi-line comments in func_args_core.c violate
BPF comment style
--
commit 0bbc8fc67b8eff90ed517069d0dfc1334363d6c3
Author: Alan Maguire <almagui@kernel.org>
selftests/bpf: Test CO-RE relocation of tracing program arguments
This commit adds BPF selftests covering CO-RE relocation of tracing program
arguments using __bpf_func_args structs and BPF_PROG_CORE() with both libbpf
and light skeletons.
> diff --git a/tools/testing/selftests/bpf/prog_tests/func_args_core.c b/tools/testing/selftests/bpf/prog_tests/func_args_core.c
> new file mode 100644
> index 0000000000000..1516ad13eaa26
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/func_args_core.c
> @@ -0,0 +1,130 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <test_progs.h>
> +#include "func_args_core.skel.h"
> +#include "func_args_core.lskel.h"
> +
> +/* runs bpf_fentry_test1..N() (via a fentry prog), bpf_modify_return_test()
> + * (via a fmod_ret prog) and bpf_testmod's test read
> + */
[Severity: Low]
This isn't a bug, but BPF coding style places the opening /* of a multi-line
comment on its own line, with the comment text starting on the next line.
The same applies to the other multi-line comments in this file and in
progs/func_args_core.c.
> diff --git a/tools/testing/selftests/bpf/progs/func_args_core.c b/tools/testing/selftests/bpf/progs/func_args_core.c
> new file mode 100644
> index 0000000000000..4656a6c642d10
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/func_args_core.c
[ ... ]
> +SEC("fmod_ret/bpf_modify_return_test")
> +int fmod_ret_test(struct modify_return_args *args)
> +{
> + fm_calls++;
> + fm_a = args->a;
> + fm_b_set = args->b != NULL;
> + fm_ret = args->ret;
> + fm_ret_exists = bpf_core_field_exists(args->ret);
> + fm_missing_exists = bpf_core_field_exists(args->missing);
> + /* don't override the return value */
> + return 0;
> +}
> +
> +/* fsession on bpf_fentry_test3(char a, int b, u64 c), called as (4, 5, 6) */
> +__u64 fs_entry_c, fs_exit_c;
> +int fs_entry_ret = -1, fs_exit_ret = -1, fs_ret_exists = -1;
> +int fs_entry_calls, fs_exit_calls;
> +
> +SEC("fsession/bpf_fentry_test3")
> +int BPF_PROG_CORE(fsession3, __u64, c, __bpf_func_ret int, ret)
> +{
> + fs_ret_exists = bpf_func_arg_exists(ret);
> + if (!bpf_session_is_return(ctx)) {
> + fs_entry_calls++;
> + fs_entry_c = c;
> + fs_entry_ret = ret;
> + } else {
> + fs_exit_calls++;
> + fs_exit_c = c;
> + fs_exit_ret = ret;
> + }
> + return 0;
> +}
[ ... ]
> +SEC("tp_btf/bpf_testmod_test_read")
> +int tp_read(struct test_read_args *args)
> +{
> + tp_pid = args->task->pid;
> + tp_len = args->ctx->len;
> + tp_bad_ptr_exists = bpf_core_field_exists(args->task___bad_ptr);
> + tp_fwd_exists = bpf_core_field_exists(args->task___fwd);
> + tp_void_exists = bpf_core_field_exists(args->ctx___void);
> + tp_void_same = args->ctx___void == (void *)args->ctx;
> + return 0;
> +}
[ ... ]
> +SEC("tp_btf/bpf_testmod_test_read")
> +int BPF_PROG_CORE(tp_read_macro, struct task_struct *, task)
> +{
> + mtp_pid = task->pid;
> + return 0;
> +}
[Severity: Medium]
Can concurrent test_progs workers cause check_results() assertions to fail
when test_progs runs with -j?
test_func_args_core() runs in parallel with other selftests that call
trigger_module_test_read() or invoke bpf_fentry_test3() and
bpf_modify_return_test() via bpf_prog_test_run_opts(). Because these programs
do not filter by the test worker's PID, another worker can increment
fm_calls, fs_entry_calls, and fs_exit_calls past 1, or overwrite tp_pid,
mtp_pid, and tp_len with its own PID and read length before check_results()
runs. In addition, if test_get_func_args_test() runs concurrently, its
fmod_ret program on bpf_modify_return_test() returns 1234 without PID
filtering, causing the trampoline to short-circuit and skip fmod_ret_test().
Filtering events by a target PID (and making the test serial if needed to
avoid fmod_ret short-circuiting from other tests) would avoid this race.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123223227.745345-1-almagui@kernel.org?part=3
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 2/7] bpf: Support CO-RE relocation of tracing program arguments
[not found] ` <20261123223227.745345-3-almagui@kernel.org>
@ 2026-11-23 22:52 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-23 22:52 UTC (permalink / raw)
To: Alan Maguire; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] bpf: quadratic BTF type scan in bpf_core_apply() via
bpf_core_is_func_args_type()
- [Low] bpf: non-standard multi-line comment above
bpf_core_func_args_cand_prog()
--
commit fb7674aa7cef4ed7e344622061c7c9391f5b51c3
Author: Alan Maguire <almagui@kernel.org>
bpf: Support CO-RE relocation of tracing program arguments
This commit passes the BPF program being loaded to bpf_core_apply() so
that CO-RE relocations against function argument structs can be resolved
against the program's attach target.
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index c4386a9a1ad41..f93ed8908d655 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -10709,11 +10709,39 @@ bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id)
> return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
> }
>
> +/* Function argument structs (see __bpf_func_args in libbpf's bpf_tracing.h)
> + * are matched against the parameters of the program's attach target.
> + */
[Severity: Low]
This isn't a bug, but BPF coding style puts the opening /* of a
multi-line comment on its own line.
[ ... ]
> @@ -10734,6 +10762,16 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
> return -EINVAL;
> }
>
> + if (need_cands && ctx->prog && bpf_core_is_func_args_type(ctx->btf, relo->type_id)) {
> + /* no locking needed, prog holds a reference to attach_btf */
> + need_cands = false;
> + err = bpf_core_func_args_cand_prog(ctx, relo_idx, &func_args_cand);
> + if (err)
> + goto out;
> + cands.cands = &func_args_cand;
> + cands.len = 1;
> + }
[Severity: Medium]
Does calling bpf_core_is_func_args_type() here introduce a quadratic scan
of ctx->btf across CO-RE relocations?
bpf_check_core_relo() sets ctx.prog for all program types, so
bpf_core_apply() now runs bpf_core_is_func_args_type() on every relocation
where relo->kind != BPF_CORE_TYPE_ID_LOCAL before consulting
vmlinux_cand_cache:
tools/lib/bpf/relo_core.c:bpf_core_is_func_args_type() {
...
n = btf__type_cnt(btf);
for (i = 1; i < n; i++) {
t = btf_type_by_id(btf, i);
if (btf_kind(t) == BTF_KIND_DECL_TAG && t->type == type_id &&
strcmp(btf__name_by_offset(btf, t->name_off), BPF_CORE_FUNC_ARGS_TAG) == 0)
return true;
}
...
}
Unlike libbpf's bpf_core_resolve_relo(), which checks its candidate cache
by local type ID before calling bpf_core_is_func_args_type(), this scans
all types in the program's BTF on every relocation (and btf_has_decl_tag()
does the same per matched argument), turning in-kernel CO-RE relocation
into O(nr_core_relo * btf_nr_types(ctx->btf)). In the kernel, program BTFs
already pre-link decl tags in btf->next_tag via d01bec4218c7 ("bpf: Link
the decl tags of a type"), so walking that chain (or using
btf_find_decl_tag_value()) would keep tag lookups O(1).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123223227.745345-1-almagui@kernel.org?part=2
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments
[not found] ` <20261123223227.745345-2-almagui@kernel.org>
@ 2026-11-23 22:53 ` sashiko-bot
2026-11-23 23:23 ` bot+bpf-ci
1 sibling, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-23 22:53 UTC (permalink / raw)
To: Alan Maguire; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] libbpf: wrong big-endian relocation for signed sub-8-byte
tp_btf args
- [Medium] libbpf: pointer argument size and offset mismatch on 32-bit
target BTF
- [Low] libbpf: multi-line comment style and misplaced comment in
relo_core.c
--
commit 966987a90447045d3ff50e58c43a513399cdbc27
Author: Alan Maguire <almagui@kernel.org>
libbpf: Support CO-RE relocation of tracing program arguments
This commit adds CO-RE relocation support for tracing program arguments
described by structs tagged with __bpf_func_args and introduces the
BPF_PROG_CORE() convenience macro.
> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> index 2672623a41989..2f6627952220b 100644
> --- a/tools/lib/bpf/relo_core.c
> +++ b/tools/lib/bpf/relo_core.c
[ ... ]
> + /* sub-8-byte arguments live in the low-order bytes of their slot,
> + * including structs and unions passed by value, which, e.g., s390
> + * passes in registers like integers of the same size
> + */
> + sz = btf__resolve_size(targ_btf, type_id);
> + if (btf_is_big_endian(targ_btf) && sz > 0 && sz < 8)
> + slot_off += 8 - sz;
[ ... ]
> @@ -734,10 +1092,17 @@ static int bpf_core_calc_field_relo(const char *prog_name,
> return 0;
> }
>
> - m = btf_members(t) + acc->idx;
> - mt = skip_mods_and_typedefs(spec->btf, m->type, &field_type_id);
> + if (btf_kind(t) == BTF_KIND_FUNC) {
> + /* function argument (or return value), never a bitfield */
> + mt = skip_mods_and_typedefs(spec->btf, func_arg_type(spec->btf, t, acc->idx),
> + &field_type_id);
> + bit_sz = 0;
> + } else {
> + m = btf_members(t) + acc->idx;
> + mt = skip_mods_and_typedefs(spec->btf, m->type, &field_type_id);
> + bit_sz = btf_member_bitfield_size(t, acc->idx);
> + }
[Severity: Medium]
Does relocating a pointer argument against 32-bit target BTF downsize the
context load to 4 bytes and misalign the offset on 32-bit big-endian
targets?
Unlike func_arg_slot_sz(), which checks btf_is_ptr(t) and returns 8,
bpf_core_calc_field_relo() falls through to btf__resolve_size() for
BTF_KIND_FUNC arguments. On 32-bit target BTF, btf__resolve_size()
returns 4 for BTF_KIND_PTR, so bpf_core_patch_insn() shrinks the 8-byte
context load to BPF_W, and bpf_core_match_func_arg() adds 4 to slot_off on
big-endian targets. However, context slots are always u64 and
btf_ctx_access() requires pointer loads from the context array to be
8-byte aligned with size == sizeof(u64), so the verifier will reject the
program.
[ ... ]
> @@ -893,6 +1258,38 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
> * with each other. Otherwise, libbpf will refuse to proceed due to ambiguity.
> * If instruction has to be poisoned, *poison will be set to true.
> */
> +/* Whether a target spec refers to an integer or enum function argument (or
> + * return value), rather than to a member of a struct passed by value, that is
> + * smaller than 8 bytes on a big-endian target. Such arguments are in the
> + * low-order bytes of their context slot, where the verifier doesn't allow
> + * loads, but the calling conventions of big-endian architectures with BPF
> + * trampolines (s390x, powerpc64) extend them to 64 bits.
> + */
[Severity: Low]
This isn't a bug, but bpf_core_is_be_small_func_arg() is inserted between
the function comment for bpf_core_calc_relo() and bpf_core_calc_relo()
itself. Also, the new multi-line comments in tools/lib/bpf/libbpf.c,
tools/lib/bpf/relo_core.c, and tools/lib/bpf/relo_core.h place text on the
opening /* line rather than putting /* on its own line per BPF comment
style.
> +static bool bpf_core_is_be_small_func_arg(const struct bpf_core_spec *spec,
> + bool *is_signed)
> +{
> + const struct bpf_core_accessor *acc;
> + const struct btf_type *t;
> + __u32 id;
> + __s64 sz;
> +
> + if (spec->len != 2 || !btf_is_big_endian(spec->btf))
> + return false;
> + acc = &spec->spec[1];
> + t = btf_type_by_id(spec->btf, acc->type_id);
> + if (!t || btf_kind(t) != BTF_KIND_FUNC)
> + return false;
> + t = skip_mods_and_typedefs(spec->btf, func_arg_type(spec->btf, t, acc->idx), &id);
> + if (!t || (!btf_is_int(t) && !btf_is_any_enum(t)))
> + return false;
> + sz = btf__resolve_size(spec->btf, id);
> + if (sz <= 0 || sz >= 8)
> + return false;
> + *is_signed = (btf_is_any_enum(t) && BTF_INFO_KFLAG(t->info)) ||
> + (btf_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED));
> + return true;
> +}
[ ... ]
> @@ -1100,6 +1506,38 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
> return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
> }
>
> + if (res->widen_func_arg && class == BPF_LDX) {
> + __u8 mode = BPF_MODE(insn->code);
> +
> + if (insn_bpf_size_to_bytes(insn) != res->orig_sz) {
> + pr_warn("prog '%s': relo #%d: insn #%d (LDX) unexpected mem size: got %d, exp %u\n",
> + prog_name, relo_idx, insn_idx,
> + insn_bpf_size_to_bytes(insn), res->orig_sz);
> + return -EINVAL;
> + }
> + /* An 8-byte load of the whole slot yields the argument
> + * as extended to 64 bits by the calling convention,
> + * which is what the original load yields only if it
> + * extends it the same way.
> + */
> + if (!(mode == BPF_MEM && !res->func_arg_signed) &&
> + !(mode == BPF_MEMSX && res->func_arg_signed &&
> + res->orig_sz == res->new_sz)) {
> + pr_warn("prog '%s': relo #%d: insn #%d (LDX) can't load function argument smaller than 8 bytes on big-endian target. "
> + "Make sure to use zero-extending loads of unsigned integers or sign-extending loads of signed ones.\n",
> + prog_name, relo_idx, insn_idx);
> + return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
> + }
> +
> + orig_val = insn->off;
> + insn->off = res->func_arg_slot_off;
> + insn->code = BPF_LDX | BPF_MEM | BPF_DW;
[Severity: Medium]
For tp_btf programs on big-endian targets, does widening a BPF_MEMSX load
of a signed sub-8-byte argument to BPF_DW corrupt negative values?
Unlike BPF trampolines, raw tracepoint probes populate context slots via
__CAST_TO_U64() in include/trace/bpf_probe.h:
include/trace/bpf_probe.h:__CAST_TO_U64() {
#define __CAST_TO_U64(x) ({ \
typeof(x) __src = (x); \
UINTTYPE(sizeof(x)) __dst; \
memcpy(&__dst, &__src, sizeof(__dst)); \
(u64)__dst; })
}
Because __CAST_TO_U64() copies the argument into an unsigned integer of
the same width before casting to u64, sub-8-byte signed tracepoint
arguments are always zero-extended in their 64-bit context slot rather
than sign-extended. Treating res->func_arg_signed as true for tp_btf
candidates causes bpf_core_patch_insn() to rewrite BPF_MEMSX loads into
BPF_DW loads that leave zeros in the upper 32/48/56 bits of negative
values, while poisoning BPF_MEM loads that would have read the
zero-extended slot value as expected.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123223227.745345-1-almagui@kernel.org?part=1
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments
[not found] ` <20261123223227.745345-2-almagui@kernel.org>
2026-11-23 22:53 ` [PATCH bpf-next 1/7] libbpf: " sashiko-bot
@ 2026-11-23 23:23 ` bot+bpf-ci
1 sibling, 0 replies; 12+ messages in thread
From: bot+bpf-ci @ 2026-11-23 23:23 UTC (permalink / raw)
To: almagui, ast, daniel, andrii
Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, john.fastabend, bpf, almagui, ast, andrii, daniel,
martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 4857 bytes --]
> libbpf: Support CO-RE relocation of tracing program arguments
TL;DR:
- Changing attach target or type after bpf_object__prepare() may silently load
stale argument offsets, reading the wrong arguments
- bpf_tracing.h comments advise by-value struct member access that can fail to
load (non-8-byte-aligned offsets)
> diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
> --- a/tools/lib/bpf/bpf_tracing.h
> +++ b/tools/lib/bpf/bpf_tracing.h
[ ... ]
> + * On big-endian architectures (e.g., s390x), the verifier only allows loads
> + * of whole 8-byte context slots, so loads of integer and enum arguments (and
> + * return values) smaller than 8 bytes are widened to their slot. That only
[ ... ]
> + * Structs passed by value that are larger than 8 bytes can't be copied out
> + * of the program context as a whole, so they can't be used as arguments of
> + * BPF_PROG_CORE; use an explicit __bpf_func_args struct and access their
> + * members individually instead.
Is this advice accurate for every struct?
btf_ctx_access() rejects any context offset that is not a multiple of 8, on
all architectures:
if (off % 8) {
bpf_log(log, "func '%s' offset %d is not multiple of 8\n", tname, off);
return false;
}
Nested member matching produces slot_offset + member_offset, so a member of
a by-value struct argument at a non-8-byte-aligned offset can never be read.
For example, with bpf_testmod_test_struct_arg_9(..., struct
bpf_testmod_struct_arg_5 h, long i), where the struct is { char a; short b;
int c; long d; }:
struct args9 { struct bpf_testmod_struct_arg_5 h; } __bpf_func_args;
... return args->h.c; /* ctx offset 7*8 + 4 = 60 */
This fails to load with "offset 60 is not multiple of 8", yet it is what the
comment recommends. BPF_PROG2() handles this struct by loading whole u64
slots. The same applies to by-value structs of 8 bytes or less with members
at a non-zero offset, such as { int a; int b; }.
Also, the big-endian paragraph says "the verifier only allows loads of whole
8-byte context slots". bpf_tracing_ctx_access() only requires off % size ==
0, so narrow loads at the start of a slot are accepted. The actual
constraint is 8-byte-aligned offsets, which is how the commit message
describes it ("only allows 8-byte-aligned context accesses").
Could both comments describe the 8-byte-aligned offset rule instead?
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
[ ... ]
> +/* Find the BTF type of the attach target of a BTF-attached tracing program */
> +static int bpf_prog_attach_target(const struct bpf_program *prog,
> + const struct btf **btf, __u32 *btf_id)
> +{
[ ... ]
> + if (prog->attach_btf_id) {
> + /* set via bpf_program__set_attach_target() */
> + btf_obj_fd = prog->attach_btf_obj_fd;
> + btf_type_id = prog->attach_btf_id;
> + } else {
> + const char *attach_name = strchr(prog->sec_name, '/');
Function-argument relocations are now resolved during bpf_object__prepare():
bpf_object__prepare() -> bpf_object__relocate() -> bpf_object__relocate_core()
-> bpf_core_resolve_relo() -> bpf_core_args_find_cands()
-> bpf_prog_attach_target()
bpf_program__set_attach_target() and bpf_program__set_expected_attach_type()
only reject obj->state >= OBJ_LOADED, so both still succeed in OBJ_PREPARED.
Before this patch nothing in prepare read the attach target, so open ->
bpf_object__prepare() -> bpf_program__set_attach_target() ->
bpf_object__load() worked.
With __bpf_func_args or BPF_PROG_CORE() in use, that sequence now loads
instructions whose context offsets and sizes were computed for the old
target. At load time libbpf_prepare_prog_load() skips resolution because
prog->attach_btf_id is non-zero, and the new attach_btf_id is passed to the
kernel, so nothing is redone and nothing reports an error.
For example, SEC("fentry/foo") with struct args { int a; int b; } and foo(int
a, int b). After prepare, bpf_program__set_attach_target(prog, 0, "bar")
with bar(int b, int a) loads fine, and args->a reads bar's slot 0, which is
b. The verifier accepts it because both slots are scalars. Changing fentry
to fexit via set_expected_attach_type() after prepare similarly leaves a
__bpf_func_ret member resolved as missing.
Can these setters be refused in OBJ_PREPARED for programs with function
argument relocations, or can the target used during relocation be recorded
and compared at load/clone time? The commit message does not mention this
limitation, and no later commit in the series changes it.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45283084474
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering
2026-11-23 22:32 [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
` (6 preceding siblings ...)
[not found] ` <20261123223227.745345-2-almagui@kernel.org>
@ 2026-11-24 0:50 ` Alexei Starovoitov
7 siblings, 0 replies; 12+ messages in thread
From: Alexei Starovoitov @ 2026-11-24 0:50 UTC (permalink / raw)
To: Alan Maguire, daniel, andrii
Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, john.fastabend, bpf
On Mon, Nov 23, 2026 at 02:32 PM Alan Maguire <almagui@kernel.org> wrote:
> By instead creating a structure with named members, we can
> bring a Compile Once, Run Everywhere experience to parameter
> specifications via a new BPF_PROG_CORE() macro. This will
> better handle cases like the following:
I like the idea. It sounds similar to what Ihor propopsed in the other
thread.
But implementation is kinda heavy.
Here is an alternative
and it cover tp_btf, fentry, fexit, fmod_ret, fsession and lsm progs.
Let bpf prog describe the context as
struct bpf_func_args {
int dfd;
struct filename *name;
int __ret;
};
in a split BTF on top of the BTF of the target.
It is the only candidate for 'struct bpf_func_args___xyz' of the prog.
The rest is ordinary CO-RE.
No changes in relo_core.[ch] and in bpf_tracing.h.
The kernel does the same for lskel.
The prog:
struct bpf_func_args___unlink {
struct filename *name;
int dfd;
int __ret;
} __attribute__((preserve_access_index));
SEC("fexit/filename_unlinkat")
int handle(struct bpf_func_args___unlink *ctx)
{
if (bpf_core_field_exists(ctx->dfd))
bpf_printk("dfd %d", ctx->dfd);
bpf_printk("%s %d", ctx->name->name, ctx->__ret);
return 0;
}
What goes away:
the series with the real struct
------------------------------------ -------------------------------
FUNC as a candidate, 3 cand flags a struct candidate
2 decl tags, marker member for GCC the name of the struct
___flavor on members ___flavor on the struct
compat rules for pointers bpf_core_type_matches()
TYPE_EXISTS special case bpf_core_type_exists()
scan of all types for every relo strncmp()
BPF_PROG_CORE() and friends ctx->arg
bpf_core_type_matches() compares the names of the pointed-to structs.
It covers vma vs vma___new:
struct bpf_func_args___new {
struct mm_area *vma;
} __attribute__((preserve_access_index));
if (bpf_core_type_matches(struct bpf_func_args___new))
BPF_PROG_CORE() reads a missing arg as zero.
An old libbpf finds no candidate. Every arg is zero and the prog loads.
Patches 5 and 6 exist because of that.
ctx->arg without a guard is poisoned there and the load fails.
Big endian: a small arg is in the low bytes of its slot.
btf_ctx_access() rejects such offset.
I think the 'off % 8' check can go for scalars.
Then there is no need to rewrite loads in libbpf, and int args work.
Not in the diff: that verifier change and the selftest.
tp_btf on kernels older than 5.10 fails to load.
btf.c compiles with this. I didn't compile the libbpf half and didn't
run anything:
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4e9a4d1..fb29ebf 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3446,6 +3446,7 @@ int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
struct bpf_core_ctx {
struct bpf_verifier_log *log;
const struct btf *btf;
+ const struct bpf_prog *prog;
};
bool btf_nested_type_is_trusted(struct bpf_verifier_log *log,
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index c4386a9..c86ad9a 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -10709,11 +10709,124 @@ check_modules:
return populate_cand_cache(cands, module_cand_cache, MODULE_CAND_CACHE_SIZE);
}
+#define BPF_FUNC_ARGS "bpf_func_args"
+
+/*
+ * The context of tp_btf, fentry, fexit, fmod_ret, fsession and lsm progs is
+ * an array of u64 slots: the arguments of the attach target, then its return
+ * value, see btf_ctx_arg_idx(). Describe it as
+ *
+ * struct bpf_func_args {
+ * <type of the parameter> <name of the parameter>;
+ * ...
+ * <return type> __ret;
+ * };
+ *
+ * in a split BTF on top of the BTF of the target. Then the relocations
+ * against 'struct bpf_func_args___xyz' of the prog are ordinary CO-RE.
+ */
+struct func_args_btf {
+ struct btf btf;
+ struct btf_type *types[1];
+ struct bpf_core_cand cand;
+ struct btf_type t;
+ struct btf_member m[MAX_BPF_FUNC_ARGS + 1];
+};
+
+static const char func_args_strs[] = BPF_FUNC_ARGS "\0__ret";
+
+static bool bpf_core_is_func_args(const struct btf *btf, const struct btf_type *t)
+{
+ const char *name = btf_name_by_offset(btf, t->name_off);
+ size_t len = sizeof(BPF_FUNC_ARGS) - 1;
+
+ return __btf_type_is_struct(t) && name &&
+ bpf_core_essential_name_len(name) == len &&
+ !strncmp(name, BPF_FUNC_ARGS, len);
+}
+
+static int func_args_add(struct func_args_btf *a, u32 name_off, u32 type_id)
+{
+ const struct btf_type *t = btf_type_skip_modifiers(&a->btf, type_id, NULL);
+ u32 sz = btf_type_is_ptr(t) ? 8 : t->size;
+ u32 n = btf_type_vlen(&a->t);
+
+ if (n == ARRAY_SIZE(a->m))
+ return -E2BIG;
+
+ a->m[n].name_off = name_off;
+ a->m[n].type = type_id;
+ a->m[n].offset = a->t.size * 8;
+ /* a small argument is in the low bytes of its slot */
+ if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && sz < 8)
+ a->m[n].offset += (8 - sz) * 8;
+ a->t.size += roundup(sz, 8);
+ a->t.info = BTF_KIND_STRUCT << 24 | (n + 1);
+ return 0;
+}
+
+static struct func_args_btf *bpf_core_func_args_btf(const struct bpf_prog *prog)
+{
+ struct btf *targ_btf = prog->aux->attach_btf;
+ const struct btf_type *t = NULL;
+ const struct btf_param *p;
+ struct func_args_btf *a;
+ int i, first = 0, err = 0;
+ const char *tname;
+ char name[128];
+ s32 id;
+
+ if (targ_btf)
+ t = btf_type_by_id(targ_btf, prog->aux->attach_btf_id);
+ if (t && btf_type_is_typedef(t)) {
+ /* btf_trace_xyz has no names of parameters, __probestub_xyz() has */
+ tname = btf_name_by_offset(targ_btf, t->name_off);
+ if (strncmp(tname, "btf_trace_", 10))
+ return ERR_PTR(-EINVAL);
+ snprintf(name, sizeof(name), "__probestub_%s", tname + 10);
+ id = btf_find_by_name_kind(targ_btf, name, BTF_KIND_FUNC);
+ t = id > 0 ? btf_type_by_id(targ_btf, id) : NULL;
+ first = 1; /* void *__data is not in the context */
+ }
+ if (!t || !btf_type_is_func(t))
+ return ERR_PTR(-EINVAL);
+ t = btf_type_by_id(targ_btf, t->type);
+
+ a = kzalloc_obj(*a, GFP_KERNEL_ACCOUNT);
+ if (!a)
+ return ERR_PTR(-ENOMEM);
+
+ a->btf.base_btf = targ_btf;
+ a->btf.start_id = btf_nr_types(targ_btf);
+ a->btf.start_str_off = targ_btf->start_str_off + targ_btf->hdr.str_len;
+ a->btf.strings = func_args_strs;
+ a->btf.hdr.str_len = sizeof(func_args_strs);
+ a->btf.types = a->types;
+ a->btf.nr_types = 1;
+ a->types[0] = &a->t;
+ a->t.name_off = a->btf.start_str_off;
+ a->t.info = BTF_KIND_STRUCT << 24;
+ a->cand.btf = &a->btf;
+ a->cand.id = a->btf.start_id;
+
+ p = btf_params(t);
+ for (i = first; !err && i < btf_type_vlen(t); i++)
+ err = func_args_add(a, p[i].name_off, p[i].type);
+ if (!err && !first && t->type)
+ err = func_args_add(a, a->t.name_off + sizeof(BPF_FUNC_ARGS), t->type);
+ if (err) {
+ kfree(a);
+ return ERR_PTR(err);
+ }
+ return a;
+}
+
int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
int relo_idx, void *insn)
{
bool need_cands = relo->kind != BPF_CORE_TYPE_ID_LOCAL;
struct bpf_core_cand_list cands = {};
+ struct func_args_btf *args = NULL;
struct bpf_core_relo_res targ_res;
struct bpf_core_spec *specs;
const struct btf_type *type;
@@ -10734,6 +10847,19 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
return -EINVAL;
}
+ if (need_cands && bpf_core_is_func_args(ctx->btf, type)) {
+ /* the prog holds attach_btf, no need for cand_cache_mutex */
+ need_cands = false;
+ args = bpf_core_func_args_btf(ctx->prog);
+ if (IS_ERR(args)) {
+ err = PTR_ERR(args);
+ args = NULL;
+ goto out;
+ }
+ cands.cands = &args->cand;
+ cands.len = 1;
+ }
+
if (need_cands) {
struct bpf_cand_cache *cc;
int i;
@@ -10779,6 +10905,7 @@ int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
out:
kfree(specs);
+ kfree(args);
if (need_cands) {
kfree(cands.cands);
mutex_unlock(&cand_cache_mutex);
diff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c
index 79e079e..e302fee 100644
--- a/kernel/bpf/check_btf.c
+++ b/kernel/bpf/check_btf.c
@@ -352,6 +352,7 @@ int bpf_check_core_relo(struct bpf_verifier_env *env,
struct bpf_core_ctx ctx = {
.log = &env->log,
.btf = btf,
+ .prog = prog,
};
bpfptr_t u_core_relo;
int err;
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1efc0dd..82a532a 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -6256,6 +6256,18 @@ static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int
return NULL;
}
+#define BPF_FUNC_ARGS "bpf_func_args"
+
+static struct btf *bpf_program_func_args_btf(struct bpf_program *prog);
+
+static bool bpf_core_is_func_args(const struct btf_type *t, const char *name)
+{
+ size_t len = sizeof(BPF_FUNC_ARGS) - 1;
+
+ return btf_is_struct(t) && bpf_core_essential_name_len(name) == len &&
+ strncmp(name, BPF_FUNC_ARGS, len) == 0;
+}
+
static int bpf_core_resolve_relo(struct bpf_program *prog,
const struct bpf_core_relo *relo,
int relo_idx,
@@ -6279,6 +6291,25 @@ static int bpf_core_resolve_relo(struct bpf_program *prog,
if (!local_name)
return -EINVAL;
+ if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
+ bpf_core_is_func_args(local_type, local_name)) {
+ /* the candidate depends on the prog, keep it out of cand_cache */
+ struct bpf_core_cand_list args_cands = { .len = 1 };
+ struct bpf_core_cand args_cand;
+ struct btf *args_btf;
+
+ args_btf = bpf_program_func_args_btf(prog);
+ if (IS_ERR(args_btf))
+ return PTR_ERR(args_btf);
+ args_cand.btf = args_btf;
+ args_cand.id = btf__type_cnt(args_btf) - 1;
+ args_cands.cands = &args_cand;
+ err = bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, &args_cands,
+ specs_scratch, targ_res);
+ btf__free(args_btf);
+ return err;
+ }
+
if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
!hashmap__find(cand_cache, local_id, &cands)) {
cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
@@ -11641,6 +11672,110 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attac
return 0;
}
+static int func_args_add(struct btf *btf, const char *name, int type_id, __u32 *off)
+{
+ __s64 sz = btf__resolve_size(btf, type_id);
+ __u32 pad = 0;
+ int err;
+
+ if (sz < 0)
+ return sz;
+ /* a small argument is in the low bytes of its slot */
+ if (sz < 8 && btf__endianness(btf) == BTF_BIG_ENDIAN)
+ pad = 8 - sz;
+ err = btf__add_field(btf, name, type_id, (*off + pad) * 8, 0);
+ *off += roundup(sz, 8);
+ return err;
+}
+
+/*
+ * The context of tp_btf, fentry, fexit, fmod_ret, fsession and lsm progs is
+ * an array of u64 slots: the arguments of the attach target, then its return
+ * value, see btf_ctx_arg_idx() in the kernel. Describe it as
+ *
+ * struct bpf_func_args {
+ * <type of the parameter> <name of the parameter>;
+ * ...
+ * <return type> __ret;
+ * };
+ *
+ * in a split BTF on top of the BTF of the target. Then the relocations
+ * against 'struct bpf_func_args___xyz' of the prog are ordinary CO-RE.
+ */
+static struct btf *bpf_program_func_args_btf(struct bpf_program *prog)
+{
+ int btf_obj_fd = 0, id = 0, i, first = 0, err;
+ struct bpf_object *obj = prog->obj;
+ struct btf *targ_btf = obj->btf_vmlinux, *btf;
+ const struct btf_param *p;
+ const struct btf_type *t;
+ const char *name;
+ __u32 off = 0;
+
+ /* subprogs are shared and have no type */
+ if ((prog->type != BPF_PROG_TYPE_TRACING && prog->type != BPF_PROG_TYPE_LSM) ||
+ prog->attach_prog_fd) {
+ pr_warn("prog '%s': struct %s needs an attach target in kernel BTF\n",
+ prog->name, BPF_FUNC_ARGS);
+ return ERR_PTR(-EOPNOTSUPP);
+ }
+
+ if (!prog->attach_btf_id) {
+ name = strchr(prog->sec_name, '/');
+ if (!name)
+ return ERR_PTR(-EINVAL);
+ err = libbpf_find_attach_btf_id(prog, name + 1, &btf_obj_fd, &id);
+ if (err)
+ return ERR_PTR(err);
+ /* libbpf_prepare_prog_load() doesn't search again */
+ prog->attach_btf_obj_fd = btf_obj_fd;
+ prog->attach_btf_id = id;
+ }
+
+ for (i = 0; prog->attach_btf_obj_fd && i < obj->btf_module_cnt; i++) {
+ if (obj->btf_modules[i].fd == prog->attach_btf_obj_fd)
+ targ_btf = obj->btf_modules[i].btf;
+ }
+
+ t = btf__type_by_id(targ_btf, prog->attach_btf_id);
+ if (t && btf_is_typedef(t)) {
+ /*
+ * btf_trace_xyz has no names of parameters. __probestub_xyz()
+ * has (6.5+), and __traceiter_xyz() has (5.10+).
+ */
+ name = btf__name_by_offset(targ_btf, t->name_off) + sizeof(BTF_TRACE_PREFIX) - 1;
+ id = find_btf_by_prefix_kind(targ_btf, "__probestub_", name, BTF_KIND_FUNC);
+ if (id == -ENOENT)
+ id = find_btf_by_prefix_kind(targ_btf, "__traceiter_", name, BTF_KIND_FUNC);
+ if (id < 0)
+ return ERR_PTR(id);
+ t = btf__type_by_id(targ_btf, id);
+ first = 1; /* void *__data is not in the context */
+ }
+ if (!t || !btf_is_func(t))
+ return ERR_PTR(-EINVAL);
+ t = btf__type_by_id(targ_btf, t->type);
+
+ btf = btf__new_empty_split(targ_btf);
+ if (!btf)
+ return ERR_PTR(-errno);
+
+ id = btf__add_struct(btf, BPF_FUNC_ARGS, 0);
+ err = id < 0 ? id : 0;
+ p = btf_params(t);
+ for (i = first; !err && i < btf_vlen(t); i++)
+ err = func_args_add(btf, btf__name_by_offset(targ_btf, p[i].name_off),
+ p[i].type, &off);
+ if (!err && !first && t->type)
+ err = func_args_add(btf, "__ret", t->type, &off);
+ if (err) {
+ btf__free(btf);
+ return ERR_PTR(err);
+ }
+ btf_type_by_id(btf, id)->size = off;
+ return btf;
+}
+
int libbpf_attach_type_by_name(const char *name,
enum bpf_attach_type *attach_type)
{
> .../selftests/bpf/prog_tests/func_args_core.c | 325 +++++++++++++
> .../selftests/bpf/progs/func_args_core.c | 241 +++++++++
> .../selftests/bpf/progs/func_args_core_lsm.c | 156 ++++++
> .../bpf/progs/func_args_core_lsm_cgroup.c | 40 ++
Also 762 lines of selftests are too much.
Pls make all of it much shorter.
pw-bot: cr
^ permalink raw reply related [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-11-24 0:50 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 22:32 [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
2026-11-23 22:32 ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence Alan Maguire
2026-11-23 22:38 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 6/7] selftests/bpf: Test detection of function argument relocation support Alan Maguire
2026-11-23 22:36 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 7/7] Documentation/bpf: Describe CO-RE relocation of function arguments Alan Maguire
[not found] ` <20261123223227.745345-5-almagui@kernel.org>
2026-11-23 22:41 ` [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM program arguments sashiko-bot
[not found] ` <20261123223227.745345-4-almagui@kernel.org>
2026-11-23 22:43 ` [PATCH bpf-next 3/7] selftests/bpf: Test CO-RE relocation of tracing " sashiko-bot
[not found] ` <20261123223227.745345-3-almagui@kernel.org>
2026-11-23 22:52 ` [PATCH bpf-next 2/7] bpf: Support " sashiko-bot
[not found] ` <20261123223227.745345-2-almagui@kernel.org>
2026-11-23 22:53 ` [PATCH bpf-next 1/7] libbpf: " sashiko-bot
2026-11-23 23:23 ` bot+bpf-ci
2026-11-24 0:50 ` [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox