* [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 1/7] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
` (6 more replies)
0 siblings, 7 replies; 15+ 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] 15+ messages in thread
* [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program 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 ` Alan Maguire
2026-11-23 22:53 ` sashiko-bot
2026-11-23 23:23 ` bot+bpf-ci
2026-11-23 22:32 ` [PATCH bpf-next 2/7] bpf: " Alan Maguire
` (5 subsequent siblings)
6 siblings, 2 replies; 15+ 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
BPF_PROG() and friends access the arguments of fentry/fexit/tp_btf/LSM
programs by fixed position in the u64 context array. When a traced
function or tracepoint gains, loses or reorders parameters, programs
silently read the wrong argument (or fail verification), and there is
no way to check whether a given argument exists.
Provide a CO-RE-like experience for these arguments instead. A struct
marked __bpf_func_args (preserve_access_index plus a "bpf_func_args"
decl tag) describes the arguments of interest, with members named after
the target's parameters, in any order:
struct my_args {
struct filename *name;
int dfd;
int ret __bpf_func_ret;
} __bpf_func_args;
SEC("fexit/filename_unlinkat")
int prog(struct my_args *args)
{
if (bpf_core_field_exists(args->dfd))
...
}
Clang emits field-based CO-RE relocations directly on the context loads.
For such structs, rather than searching for same-named target types, the
single CO-RE candidate is the program's attach target FUNC, flagged as a
function arguments candidate. Typedefs and modifiers are skipped when
looking for the tag, as clang uses the typedef as the root type of
relocations against typedef'd anonymous structs. relo_core then matches
the struct's members by essential name (ignoring ___flavor suffixes)
against the FUNC's parameters, placing each at its context slot as laid
out by the kernel's btf_ctx_arg_idx() (by-value structs > 8 bytes take
multiple slots), with the parameter's type. A member tagged
__bpf_func_ret maps to the return value slot for fexit-like programs.
Everything else (offset patching, bpf_core_field_exists(), type
compatibility checks and poisoning of unguarded accesses) works
unchanged, and nested accesses into by-value struct arguments continue
as normal member matching.
Parameter type changes are handled by CO-RE's usual field compatibility
rules, applied between each member and its parameter, plus a stricter
check for pointers:
- a member of a different kind than its parameter (e.g. integer vs
pointer, integer vs enum, scalar vs struct) doesn't match, so the
argument appears to be missing: bpf_core_field_exists() returns 0 and
unguarded accesses fail to load;
- integers match regardless of size and signedness; when an unsigned
integer changes size, the load size is adjusted and the value is read
correctly, while a size change involving signed integers poisons the
load (it exists, but can't be read correctly), as for struct fields;
- enums match if their essential names do; structs passed by value
always match, with accesses to their members relocated as usual;
- unlike struct fields, where any two pointers are compatible, pointer
arguments only match if the pointed-to types (ignoring typedefs and
modifiers) are compatible: structs, unions and enums need the same
kind and essential name (forward declarations matching structs and
unions), integers the same size and other types the same kind,
recursing through multi-level pointers, with void * on either side
matching any pointer. Otherwise, a parameter changing from, e.g.,
struct file * to struct inode * would silently have accesses through
it relocated against the wrong type. Instead such an argument is seen
as missing, and ___flavor members can describe both variants:
struct my_args {
struct vm_area_struct *vma;
struct mm_area *vma___new;
} __bpf_func_args;
btf_trace_xxx prototypes carry no parameter names, so for tp_btf
programs the candidate is the identically-typed __probestub_xxx()
function (Linux 6.5+) or, failing that, __traceiter_xxx() (Linux 5.10+),
ignoring its first ("void *__data") parameter. On older kernels, which
have neither, there is no candidate: the program still loads, with all
arguments appearing to be missing, rather than failing to load.
The matching lives in relo_core.c, which is shared with the kernel, so
that kernel-side CO-RE (used by light skeletons) can support it too.
BPF_PROG_CORE() wraps this in a BPF_PROG2()-like interface, declaring
the arguments struct from (type, name) pairs and passing the arguments
to the program body as typed variables:
SEC("fexit/filename_unlinkat")
int BPF_PROG_CORE(prog, struct filename *, name, int, dfd,
__bpf_func_ret int, ret)
Arguments that don't exist in the target read as zero, and
bpf_func_arg_exists() tells whether they exist. Structs larger than 8
bytes passed by value can't be copied out of the context as a whole, so
they need an explicit __bpf_func_args struct instead. The zero value of a
missing argument is hidden from the compiler, as otherwise GCC can prove
that, e.g., dereferencing a missing pointer argument is a NULL dereference
and replace that dead code path with a call to abort(), which can't be
linked.
GCC doesn't support BTF decl tags on types (it warns that the attribute
doesn't apply to types, which builds with -Wno-attributes don't show),
so with GCC a struct-level "bpf_func_args" tag would silently be dropped
and all arguments would appear to be missing. Hence also recognize the
tag on a member of the struct, which both compilers support, and provide
__bpf_func_args_marker, a zero-sized member carrying it. BPF_PROG_CORE()
always uses the marker, so it works the same with clang and GCC, while
structs declared explicitly need the marker for GCC; with GCC,
__bpf_func_args only applies preserve_access_index, to avoid the warning.
With libbpf applying relocations, accesses must be made from the entry
program (or inlined code), since a shared subprogram has no single
attach target. BPF program targets (freplace, fentry on BPF progs) and
multi-attach programs are rejected explicitly. On big-endian targets
sub-8-byte arguments, including structs and unions passed by value
(which, e.g., s390 passes in registers like integers of the same
size), are placed at the correct offset within their slot, but the
verifier currently only allows 8-byte-aligned context accesses for
these programs. Loads of integer and enum arguments (and return values)
are therefore widened to 8-byte loads of their whole slot: the calling
conventions of the big-endian architectures with BPF trampolines
(s390x, powerpc64) extend these to 64 bits, so that this yields what
the original load would for zero-extending loads of unsigned and
sign-extending loads of signed arguments, as BPF_PROG()'s conversion
from the slot's value does. Other loads of such arguments are poisoned,
and other accesses (including to members of structs passed by value)
fail to load, rather than reading the wrong bytes.
This is a significant limitation on big-endian targets: signed integer
arguments and return values smaller than 8 bytes, such as int, can
generally not be read there. Compilers use zero-extending loads for
them, unless the value is converted to a wider type when building with
-mcpu=v4 or later, and BPF_PROG_CORE() always loads arguments into
variables of their own type, so it can't be used with such arguments at
all. Programs reading them fail to load and need to use BPF_PROG() or
BPF_PROG2() instead. Lifting this requires the verifier to allow narrow
loads at the offset of such arguments within their slot.
Signed-off-by: Alan Maguire <almagui@kernel.org>
Assisted-by: LLM
---
tools/lib/bpf/bpf_tracing.h | 176 ++++++++++++++
tools/lib/bpf/libbpf.c | 142 +++++++++++-
tools/lib/bpf/relo_core.c | 451 +++++++++++++++++++++++++++++++++++-
tools/lib/bpf/relo_core.h | 29 +++
4 files changed, 789 insertions(+), 9 deletions(-)
diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h
index dbe32a5d02cd..24182b48fa4d 100644
--- a/tools/lib/bpf/bpf_tracing.h
+++ b/tools/lib/bpf/bpf_tracing.h
@@ -654,6 +654,77 @@ struct pt_regs;
#define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), ctx[11]
#define ___bpf_ctx_cast(args...) ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)
+/*
+ * __bpf_func_args marks a struct describing (a subset of) the arguments of
+ * the attach target of a tp_btf/fentry/fexit/fmod_ret/LSM program, giving a
+ * CO-RE-relocatable alternative to BPF_PROG()'s fixed argument positions.
+ * Members are matched to the target's parameters by name (ignoring any
+ * ___flavor suffix) when the program is loaded, regardless of their order or
+ * position in the struct, so an argument being added, removed or reordered
+ * in the target function doesn't break the program. A member marked with
+ * __bpf_func_ret refers to the return value (for fexit-like programs).
+ *
+ * The program takes a pointer to such a struct as its context and uses
+ * plain member accesses; members that have no match in the target can be
+ * checked for with bpf_core_field_exists(). The usual CO-RE field
+ * compatibility rules apply between member and parameter types, except that
+ * 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 pointers match any pointer. A parameter changing to
+ * point to a different type thus appears as missing; ___flavor members can
+ * describe the different variants. For example:
+ *
+ * struct my_args {
+ * struct filename *name;
+ * int dfd;
+ * int ret __bpf_func_ret;
+ * } __bpf_func_args;
+ *
+ * SEC("fexit/filename_unlinkat")
+ * int handle(struct my_args *args)
+ * {
+ * if (bpf_core_field_exists(args->dfd))
+ * bpf_printk("dfd %d", args->dfd);
+ * bpf_printk("%s: %d", BPF_CORE_READ(args->name, name), args->ret);
+ * return 0;
+ * }
+ *
+ * Accesses have to be made from the program itself (or __always_inline
+ * functions), not from subprograms. Compilers may access members of struct
+ * arguments passed by value by adding a (relocated) offset to the context
+ * pointer, which the verifier only accepts since Linux 7.1.
+ *
+ * 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
+ * yields the right value for zero-extending loads of unsigned and
+ * sign-extending loads of signed arguments; other loads make the program
+ * fail to load. As compilers only use sign-extending loads when the value is
+ * converted to a wider type, and only with -mcpu=v4 or later, signed
+ * arguments smaller than 8 bytes, such as int, generally can't be read there;
+ * use BPF_PROG() or BPF_PROG2() for them instead.
+ *
+ * GCC doesn't support BTF decl tags on types, so with GCC the struct also
+ * needs to contain __bpf_func_args_marker, a zero-sized member carrying the
+ * tag instead; structs having both work with either compiler:
+ *
+ * struct my_args {
+ * __bpf_func_args_marker;
+ * struct filename *name;
+ * ...
+ * } __bpf_func_args;
+ */
+#if defined(__clang__)
+#define __bpf_func_args __attribute__((preserve_access_index, btf_decl_tag("bpf_func_args")))
+#else
+#define __bpf_func_args __attribute__((preserve_access_index))
+#endif
+#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")))
+
/*
* BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and
* similar kinds of BPF programs, that accept input arguments as a single
@@ -790,6 +861,111 @@ typeof(name(0)) name(unsigned long long *ctx) \
static __always_inline typeof(name(0)) \
____##name(unsigned long long *ctx ___bpf_ctx_decl(args))
+#define ___bpf_core_mem0()
+#define ___bpf_core_mem1(t, x) t x;
+#define ___bpf_core_mem2(t, x, args...) t x; ___bpf_core_mem1(args)
+#define ___bpf_core_mem3(t, x, args...) t x; ___bpf_core_mem2(args)
+#define ___bpf_core_mem4(t, x, args...) t x; ___bpf_core_mem3(args)
+#define ___bpf_core_mem5(t, x, args...) t x; ___bpf_core_mem4(args)
+#define ___bpf_core_mem6(t, x, args...) t x; ___bpf_core_mem5(args)
+#define ___bpf_core_mem7(t, x, args...) t x; ___bpf_core_mem6(args)
+#define ___bpf_core_mem8(t, x, args...) t x; ___bpf_core_mem7(args)
+#define ___bpf_core_mem9(t, x, args...) t x; ___bpf_core_mem8(args)
+#define ___bpf_core_mem10(t, x, args...) t x; ___bpf_core_mem9(args)
+#define ___bpf_core_mem11(t, x, args...) t x; ___bpf_core_mem10(args)
+#define ___bpf_core_mem12(t, x, args...) t x; ___bpf_core_mem11(args)
+#define ___bpf_core_mem(args...) ___bpf_apply(___bpf_core_mem, ___bpf_narg2(args))(args)
+
+#define ___bpf_core_ld0()
+#define ___bpf_core_ld1(t, x) , ___bpf_core_arg(x)
+#define ___bpf_core_ld2(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld1(args)
+#define ___bpf_core_ld3(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld2(args)
+#define ___bpf_core_ld4(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld3(args)
+#define ___bpf_core_ld5(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld4(args)
+#define ___bpf_core_ld6(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld5(args)
+#define ___bpf_core_ld7(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld6(args)
+#define ___bpf_core_ld8(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld7(args)
+#define ___bpf_core_ld9(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld8(args)
+#define ___bpf_core_ld10(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld9(args)
+#define ___bpf_core_ld11(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld10(args)
+#define ___bpf_core_ld12(t, x, args...) , ___bpf_core_arg(x) ___bpf_core_ld11(args)
+#define ___bpf_core_ld(args...) ___bpf_apply(___bpf_core_ld, ___bpf_narg2(args))(args)
+
+/* 2 == BPF_FIELD_EXISTS from bpf_core_read.h */
+#define ___bpf_core_arg_exists(p, x) __builtin_preserve_field_info((p)->x, 2)
+
+/*
+ * Missing arguments read as zero. The zero is hidden from the compiler, as
+ * otherwise it could prove that, e.g., dereferencing a missing pointer
+ * argument is a NULL dereference and replace that (dead) code path with a
+ * trap, which GCC turns into a call to abort() that can't be linked.
+ */
+#define ___bpf_core_arg(x) ({ \
+ typeof(___args->x) ___v; \
+ _Static_assert(sizeof(___v) <= sizeof(unsigned long), \
+ "BPF_PROG_CORE() arguments can't be larger than 8 bytes"); \
+ if (___bpf_core_arg_exists(___args, x)) { \
+ ___v = ___args->x; \
+ } else { \
+ unsigned long ___zero = 0; \
+ \
+ asm volatile("" : "+r"(___zero)); \
+ __builtin_memcpy(&___v, &___zero, sizeof(___v)); \
+ } \
+ ___v; \
+})
+
+/*
+ * BPF_PROG_CORE is a CO-RE-relocatable variant of BPF_PROG2: arguments are
+ * specified as (type, name) pairs naming (a subset of) the parameters of the
+ * attach target, in any order, and are matched to the target's parameters by
+ * name when the program is loaded (see __bpf_func_args; BPF_PROG_CORE
+ * declares such a struct internally, using __bpf_func_args_marker so that it
+ * works with both clang and GCC). Specifying __bpf_func_ret as part of the
+ * type of an argument makes it refer to the return value instead.
+ *
+ * Arguments that don't exist in the target (or whose type isn't compatible)
+ * read as zero; bpf_func_arg_exists() tells whether an argument exists.
+ * Within the program, 'ctx' points to the arguments struct.
+ *
+ * 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);
+ * ...
+ * }
+ *
+ * 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.
+ *
+ * On big-endian architectures, signed integer and enum arguments (and return
+ * values) smaller than 8 bytes can't be used either, as they are loaded into
+ * variables of their own type with zero-extending loads, which can't be
+ * relocated there (see __bpf_func_args); use BPF_PROG() or BPF_PROG2().
+ */
+#define BPF_PROG_CORE(name, args...) \
+name(void *ctx); \
+struct ___##name##_args { \
+ __bpf_func_args_marker; \
+ ___bpf_core_mem(args) \
+} __attribute__((preserve_access_index)); \
+static __always_inline typeof(name(0)) \
+____##name(struct ___##name##_args *ctx ___bpf_ctx_decl(args)); \
+typeof(name(0)) name(void *ctx) \
+{ \
+ struct ___##name##_args *___args = ctx; \
+ \
+ return ____##name(___args ___bpf_core_ld(args)); \
+} \
+static __always_inline typeof(name(0)) \
+____##name(struct ___##name##_args *ctx ___bpf_ctx_decl(args))
+
+#define bpf_func_arg_exists(x) ___bpf_core_arg_exists(ctx, x)
+
struct pt_regs;
#define ___bpf_kprobe_args0() ctx
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1efc0dd37b2e..82408e3eafc4 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5990,6 +5990,7 @@ int bpf_core_add_cands(struct bpf_core_cand *local_cand,
cand = &new_cands[cands->len];
cand->btf = targ_btf;
cand->id = i;
+ cand->flags = 0;
cands->cands = new_cands;
cands->len++;
@@ -6256,11 +6257,134 @@ static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int
return NULL;
}
+static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,
+ enum bpf_attach_type attach_type,
+ int *btf_obj_fd, int *btf_type_id);
+
+/* Single-entry cache of the CO-RE candidate for function argument structs
+ * (see __bpf_func_args) of a given program, i.e., of its attach target.
+ */
+struct bpf_core_args_cache {
+ const struct bpf_program *prog;
+ struct bpf_core_cand cand;
+ /* 0 if the attach target has no named parameters */
+ int nr_cands;
+ struct bpf_core_cand_list cands;
+};
+
+/* 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)
+{
+ struct bpf_object *obj = prog->obj;
+ int btf_obj_fd = 0, btf_type_id = 0, i, err;
+
+ if (prog->attach_prog_fd) {
+ pr_warn("prog '%s': function argument relocations are not supported for BPF program targets\n",
+ prog->name);
+ return -EOPNOTSUPP;
+ }
+
+ 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, '/');
+
+ if (!prog->sec_def || !(prog->sec_def->cookie & SEC_ATTACH_BTF) || !attach_name) {
+ pr_warn("prog '%s': function argument relocations require a BTF attach target\n",
+ prog->name);
+ return -EINVAL;
+ }
+ err = find_kernel_btf_id(obj, attach_name + 1, prog->expected_attach_type,
+ &btf_obj_fd, &btf_type_id);
+ if (err) {
+ pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %s\n",
+ prog->name, attach_name + 1, errstr(err));
+ return err;
+ }
+ }
+
+ *btf = NULL;
+ if (!btf_obj_fd) {
+ *btf = obj->btf_vmlinux;
+ } else {
+ for (i = 0; i < obj->btf_module_cnt; i++) {
+ if (obj->btf_modules[i].fd == btf_obj_fd) {
+ *btf = obj->btf_modules[i].btf;
+ break;
+ }
+ }
+ }
+ if (!*btf)
+ return -ESRCH;
+ *btf_id = btf_type_id;
+ return 0;
+}
+
+static struct bpf_core_cand_list *
+bpf_core_args_find_cands(const struct bpf_program *prog, struct bpf_core_args_cache *cache)
+{
+ const struct btf *btf;
+ __u32 btf_id;
+ int err;
+
+ if (cache->prog == prog)
+ goto out;
+ cache->prog = NULL;
+
+ /* there is no single attach target to relocate against in (shared)
+ * subprograms; accesses need to be in the entry prog itself (or in
+ * __always_inline functions)
+ */
+ if (prog_is_subprog(prog->obj, prog)) {
+ pr_warn("prog '%s': function argument relocations are only supported in entry programs\n",
+ prog->name);
+ return ERR_PTR(-EOPNOTSUPP);
+ }
+ if (prog->type != BPF_PROG_TYPE_TRACING && prog->type != BPF_PROG_TYPE_LSM) {
+ pr_warn("prog '%s': function argument relocations are not supported for this program type\n",
+ prog->name);
+ return ERR_PTR(-EOPNOTSUPP);
+ }
+
+ err = bpf_prog_attach_target(prog, &btf, &btf_id);
+ if (err)
+ return ERR_PTR(err);
+
+ err = bpf_core_func_args_cand(btf, btf_id, prog->expected_attach_type, &cache->cand);
+ if (err == -ENOENT) {
+ /* tracepoints on old kernels have no function with named
+ * parameters to match against; rather than failing to load,
+ * leave all arguments without a match, so that they appear
+ * to be missing (and the struct, to bpf_core_type_exists(),
+ * to be unsupported)
+ */
+ pr_warn("prog '%s': attach target [%u] has no parameter names, function arguments will appear to be missing\n",
+ prog->name, btf_id);
+ cache->nr_cands = 0;
+ } else if (err) {
+ pr_warn("prog '%s': can't relocate function arguments against attach target [%u]: %s\n",
+ prog->name, btf_id, errstr(err));
+ return ERR_PTR(err);
+ } else {
+ cache->nr_cands = 1;
+ }
+ cache->prog = prog;
+out:
+ /* bpf_core_calc_relo_insn() may shrink the list, so (re)set it */
+ cache->cands.cands = &cache->cand;
+ cache->cands.len = cache->nr_cands;
+ return &cache->cands;
+}
+
static int bpf_core_resolve_relo(struct bpf_program *prog,
const struct bpf_core_relo *relo,
int relo_idx,
const struct btf *local_btf,
struct hashmap *cand_cache,
+ struct bpf_core_args_cache *args_cache,
struct bpf_core_relo_res *targ_res)
{
struct bpf_core_spec specs_scratch[3] = {};
@@ -6280,7 +6404,19 @@ static int bpf_core_resolve_relo(struct bpf_program *prog,
return -EINVAL;
if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
- !hashmap__find(cand_cache, local_id, &cands)) {
+ !hashmap__find(cand_cache, local_id, &cands) &&
+ bpf_core_is_func_args_type(local_btf, local_id)) {
+ /* candidate depends on the program's attach target, so it
+ * can't go into the object-wide candidate cache
+ */
+ cands = bpf_core_args_find_cands(prog, args_cache);
+ if (IS_ERR(cands)) {
+ pr_warn("prog '%s': relo #%d: failed to resolve function arguments for [%u] %s %s: %ld\n",
+ prog_name, relo_idx, local_id, btf_kind_str(local_type),
+ local_name, PTR_ERR(cands));
+ return PTR_ERR(cands);
+ }
+ } else if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && !cands) {
cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
if (IS_ERR(cands)) {
pr_warn("prog '%s': relo #%d: target candidate search failed for [%u] %s %s: %ld\n",
@@ -6307,6 +6443,7 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
const struct bpf_core_relo *rec;
const struct btf_ext_info *seg;
struct hashmap_entry *entry;
+ struct bpf_core_args_cache args_cache = {};
struct hashmap *cand_cache = NULL;
struct bpf_program *prog;
struct bpf_insn *insn;
@@ -6400,7 +6537,8 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
if (prog->obj->gen_loader)
continue;
- err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res);
+ err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache,
+ &args_cache, &targ_res);
if (err) {
pr_warn("prog '%s': relo #%d: failed to relocate: %s\n",
prog->name, i, errstr(err));
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 2672623a4198..2f6627952220 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -41,6 +41,21 @@ static s64 btf__resolve_size(const struct btf *btf, u32 type_id)
return size;
}
+static u32 btf__type_cnt(const struct btf *btf)
+{
+ return btf_nr_types(btf);
+}
+
+static s32 btf__find_by_name_kind(const struct btf *btf, const char *name, u32 kind)
+{
+ return btf_find_by_name_kind(btf, name, kind);
+}
+
+static bool btf_is_big_endian(const struct btf *btf)
+{
+ return IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
+}
+
enum libbpf_print_level {
LIBBPF_WARN,
LIBBPF_INFO,
@@ -65,6 +80,11 @@ enum libbpf_print_level {
#include "bpf.h"
#include "btf.h"
#include "libbpf_internal.h"
+
+static bool btf_is_big_endian(const struct btf *btf)
+{
+ return btf__endianness(btf) == BTF_BIG_ENDIAN;
+}
#endif
static bool is_flex_arr(const struct btf *btf,
@@ -463,6 +483,319 @@ static int bpf_core_fields_are_compat(const struct btf *local_btf,
}
}
+static bool btf_has_decl_tag(const struct btf *btf, __u32 type_id, int comp_idx,
+ const char *tag)
+{
+ __u32 i, n = btf__type_cnt(btf);
+ const struct btf_type *t;
+
+ 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 ||
+ btf_decl_tag(t)->component_idx != comp_idx)
+ continue;
+ if (strcmp(btf__name_by_offset(btf, t->name_off), tag) == 0)
+ return true;
+ }
+ return false;
+}
+
+/* A struct tagged with btf_decl_tag("bpf_func_args") describes (a subset of)
+ * the arguments of the attach target of a tracing program; its members are
+ * matched by name against the target's parameters rather than against
+ * members of same-named target types. The tag can be on the struct itself or,
+ * for compilers that don't support decl tags on types (e.g., GCC), on one of
+ * its members (see __bpf_func_args_marker). The type may refer to the struct
+ * through typedefs and modifiers, e.g., as the root type of relocations
+ * against a typedef'd anonymous struct.
+ */
+bool bpf_core_is_func_args_type(const struct btf *btf, __u32 type_id)
+{
+ __u32 i, n = btf__type_cnt(btf);
+ const struct btf_type *t;
+
+ t = skip_mods_and_typedefs(btf, type_id, &type_id);
+ if (!t || btf_kind(t) != BTF_KIND_STRUCT)
+ return false;
+
+ 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;
+ }
+ return false;
+}
+
+#define BTF_TRACE_PFX "btf_trace_"
+#define BTF_PROBESTUB_PFX "__probestub_"
+#define BTF_TRACEITER_PFX "__traceiter_"
+
+static int find_tp_func(const struct btf *btf, const char *pfx, const char *tp_name)
+{
+ char func_name[128];
+
+ if (snprintf(func_name, sizeof(func_name), "%s%s", pfx, tp_name) >= sizeof(func_name))
+ return -ENAMETOOLONG;
+ return btf__find_by_name_kind(btf, func_name, BTF_KIND_FUNC);
+}
+
+/* Construct the CO-RE candidate for function argument structs of programs
+ * attached to attach_btf_id in btf, with the given attach type. Returns
+ * -ENOENT if the attach target is a tracepoint without a function having its
+ * parameters (with names) in btf, i.e., on kernels before Linux 5.10.
+ */
+int bpf_core_func_args_cand(const struct btf *btf, __u32 attach_btf_id,
+ enum bpf_attach_type attach_type,
+ struct bpf_core_cand *cand)
+{
+ const struct btf_type *t, *proto;
+ __u32 flags = BPF_CORE_CAND_FUNC_ARGS;
+ __u32 id = attach_btf_id;
+
+ switch (attach_type) {
+ case BPF_TRACE_FEXIT:
+ case BPF_TRACE_FSESSION:
+ case BPF_MODIFY_RETURN:
+ case BPF_LSM_MAC:
+ case BPF_LSM_CGROUP:
+ flags |= BPF_CORE_CAND_RETVAL;
+ break;
+ case BPF_TRACE_FENTRY:
+ break;
+ case BPF_TRACE_RAW_TP:
+ flags |= BPF_CORE_CAND_SKIP_FIRST;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ t = btf_type_by_id(btf, id);
+ if (!t)
+ return -EINVAL;
+ /* the btf_trace_xxx typedef's FUNC_PROTO has no parameter names, but
+ * __probestub_xxx(void *__data, proto) (since Linux 6.5) and
+ * __traceiter_xxx(void *__data, proto) (since Linux 5.10) have the
+ * same parameters, with names; only the parameters are used, as there
+ * is no return value in the context of raw tracepoint programs
+ */
+ if (btf_kind(t) == BTF_KIND_TYPEDEF) {
+ const char *name = btf__name_by_offset(btf, t->name_off);
+ size_t pfx_len = sizeof(BTF_TRACE_PFX) - 1;
+ int func_id;
+
+ if (!name || strncmp(name, BTF_TRACE_PFX, pfx_len) != 0)
+ return -EINVAL;
+ func_id = find_tp_func(btf, BTF_PROBESTUB_PFX, name + pfx_len);
+ if (func_id == -ENOENT)
+ func_id = find_tp_func(btf, BTF_TRACEITER_PFX, name + pfx_len);
+ if (func_id < 0)
+ return func_id;
+ id = func_id;
+ t = btf_type_by_id(btf, id);
+ }
+ if (btf_kind(t) != BTF_KIND_FUNC)
+ return -EINVAL;
+ proto = btf_type_by_id(btf, t->type);
+ if (!proto || btf_kind(proto) != BTF_KIND_FUNC_PROTO)
+ return -EINVAL;
+ if ((flags & BPF_CORE_CAND_SKIP_FIRST) && btf_vlen(proto) < 1)
+ return -EINVAL;
+
+ cand->btf = btf;
+ cand->id = id;
+ cand->flags = flags;
+ return 0;
+}
+
+/* Size of the program context slot(s) taken by a value of the given type;
+ * mirrors btf_ctx_arg_idx() in the kernel.
+ */
+static int func_arg_slot_sz(const struct btf *btf, __u32 type_id)
+{
+ const struct btf_type *t = skip_mods_and_typedefs(btf, type_id, NULL);
+ __s64 sz;
+
+ if (!t)
+ return -EINVAL;
+ if (btf_is_ptr(t))
+ return 8;
+ sz = btf__resolve_size(btf, type_id);
+ if (sz < 0)
+ return sz;
+ return (sz + 7) / 8 * 8;
+}
+
+/* Type of parameter idx of a FUNC, or of its return value if idx is one
+ * past the last parameter.
+ */
+static __u32 func_arg_type(const struct btf *btf, const struct btf_type *func, __u32 idx)
+{
+ const struct btf_type *proto = btf_type_by_id(btf, func->type);
+
+ return idx < btf_vlen(proto) ? btf_params(proto)[idx].type : proto->type;
+}
+
+/* Stricter compatibility check for pointer arguments than the one CO-RE
+ * applies to fields, where any two pointers are compatible: after skipping
+ * typedefs and modifiers, the types pointed to need to be structs, unions or
+ * enums of the same kind and essential name (forward declarations matching
+ * structs and unions, enums matching 64-bit enums), integers of the same
+ * size, or other types of the same kind, recursing through pointers to
+ * pointers, so that a parameter changing to point to a different type is seen
+ * as a missing argument (and different ___flavor members can describe
+ * different pointer types). void pointers on either side act as wildcards.
+ */
+static int bpf_core_func_arg_ptr_compat(const struct btf *local_btf, __u32 local_id,
+ const struct btf *targ_btf, __u32 targ_id)
+{
+ const struct btf_type *local_t, *targ_t;
+ const char *local_name, *targ_name;
+ size_t local_len, targ_len;
+ int depth = 32;
+
+ local_t = skip_mods_and_typedefs(local_btf, local_id, &local_id);
+ targ_t = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
+ if (!local_t || !targ_t)
+ return -EINVAL;
+ if (!btf_is_ptr(local_t) || !btf_is_ptr(targ_t))
+ return 1;
+
+ while (btf_is_ptr(local_t) && btf_is_ptr(targ_t)) {
+ if (--depth < 0)
+ return -EINVAL;
+ local_t = skip_mods_and_typedefs(local_btf, local_t->type, &local_id);
+ targ_t = skip_mods_and_typedefs(targ_btf, targ_t->type, &targ_id);
+ if (!local_t || !targ_t)
+ return -EINVAL;
+ /* void * */
+ if (local_id == 0 || targ_id == 0)
+ return 1;
+ }
+
+ switch (btf_kind(local_t)) {
+ case BTF_KIND_STRUCT:
+ case BTF_KIND_UNION:
+ case BTF_KIND_FWD:
+ /* forward declarations match structs and unions */
+ if (btf_kind(targ_t) != BTF_KIND_STRUCT && btf_kind(targ_t) != BTF_KIND_UNION &&
+ btf_kind(targ_t) != BTF_KIND_FWD)
+ return 0;
+ if (btf_kind(local_t) != BTF_KIND_FWD && btf_kind(targ_t) != BTF_KIND_FWD &&
+ btf_kind(local_t) != btf_kind(targ_t))
+ return 0;
+ break;
+ case BTF_KIND_ENUM:
+ case BTF_KIND_ENUM64:
+ if (!btf_is_any_enum(targ_t))
+ return 0;
+ break;
+ case BTF_KIND_INT:
+ return btf_is_int(targ_t) && local_t->size == targ_t->size;
+ default:
+ return btf_kind(local_t) == btf_kind(targ_t);
+ }
+
+ local_name = btf__name_by_offset(local_btf, local_t->name_off);
+ targ_name = btf__name_by_offset(targ_btf, targ_t->name_off);
+ local_len = bpf_core_essential_name_len(local_name);
+ targ_len = bpf_core_essential_name_len(targ_name);
+ return local_len == targ_len &&
+ (local_len == 0 || strncmp(local_name, targ_name, local_len) == 0);
+}
+
+/* Match a named field accessor of a function arguments struct against the
+ * parameters (or the return value) of the FUNC of a function arguments
+ * candidate, using their location in the program context, which is an
+ * array of u64 slots, one per argument (two for structs passed by value
+ * that are larger than 8 bytes), followed by the return value.
+ */
+static int bpf_core_match_func_arg(const struct btf *local_btf,
+ const struct bpf_core_accessor *local_acc,
+ const struct bpf_core_cand *cand,
+ struct bpf_core_spec *spec,
+ __u32 *next_targ_id)
+{
+ const struct btf *targ_btf = cand->btf;
+ const struct btf_type *local_type, *func, *proto;
+ const struct btf_member *local_member;
+ const struct btf_param *params;
+ struct bpf_core_accessor *targ_acc;
+ __u32 i, idx, n, first, type_id, slot_off = 0;
+ const char *targ_name = NULL;
+ size_t local_len;
+ int sz, found;
+
+ if (!local_acc->name)
+ return 0;
+
+ local_type = btf_type_by_id(local_btf, local_acc->type_id);
+ local_member = btf_members(local_type) + local_acc->idx;
+ local_len = bpf_core_essential_name_len(local_acc->name);
+
+ func = btf_type_by_id(targ_btf, cand->id);
+ proto = btf_type_by_id(targ_btf, func->type);
+ params = btf_params(proto);
+ n = btf_vlen(proto);
+ /* variadic functions can't be traced, but be safe */
+ if (n && !params[n - 1].type)
+ n--;
+ first = (cand->flags & BPF_CORE_CAND_SKIP_FIRST) ? 1 : 0;
+
+ if (btf_has_decl_tag(local_btf, local_acc->type_id, local_acc->idx,
+ BPF_CORE_FUNC_RET_TAG)) {
+ if (!(cand->flags & BPF_CORE_CAND_RETVAL) || !proto->type)
+ return 0;
+ idx = btf_vlen(proto);
+ targ_name = local_acc->name;
+ } else {
+ for (idx = first; idx < n; idx++) {
+ targ_name = btf__name_by_offset(targ_btf, params[idx].name_off);
+ if (targ_name && strlen(targ_name) == local_len &&
+ strncmp(targ_name, local_acc->name, local_len) == 0)
+ break;
+ }
+ if (idx == n)
+ return 0;
+ }
+
+ for (i = first; i < idx && i < n; i++) {
+ sz = func_arg_slot_sz(targ_btf, params[i].type);
+ if (sz < 0)
+ return sz;
+ slot_off += sz;
+ }
+
+ type_id = func_arg_type(targ_btf, func, idx);
+ found = bpf_core_fields_are_compat(local_btf, local_member->type,
+ targ_btf, type_id);
+ if (found > 0)
+ found = bpf_core_func_arg_ptr_compat(local_btf, local_member->type,
+ targ_btf, type_id);
+ if (found <= 0)
+ return found;
+
+ if (spec->raw_len == BPF_CORE_SPEC_MAX_LEN)
+ return -E2BIG;
+
+ /* 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;
+
+ spec->bit_offset += slot_off * 8;
+ spec->raw_spec[spec->raw_len++] = idx;
+ targ_acc = &spec->spec[spec->len++];
+ targ_acc->type_id = cand->id;
+ targ_acc->idx = idx;
+ targ_acc->name = targ_name;
+ *next_targ_id = type_id;
+ return 1;
+}
+
/*
* Given single high-level named field accessor in local type, find
* corresponding high-level accessor for a target type. Along the way,
@@ -556,12 +889,15 @@ static int bpf_core_match_member(const struct btf *local_btf,
* target spec (high-level, low-level + bit offset).
*/
static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
- const struct btf *targ_btf, __u32 targ_id,
+ const struct bpf_core_cand *cand,
struct bpf_core_spec *targ_spec)
{
+ const struct btf *targ_btf = cand->btf;
+ bool func_args = cand->flags & BPF_CORE_CAND_FUNC_ARGS;
const struct btf_type *targ_type;
const struct bpf_core_accessor *local_acc;
struct bpf_core_accessor *targ_acc;
+ __u32 targ_id = cand->id;
int i, sz, matched;
__u32 name_off;
@@ -570,6 +906,15 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
targ_spec->root_type_id = targ_id;
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
+ */
+ if (!core_relo_is_field_based(local_spec->relo_kind) ||
+ local_spec->len < 2 || local_spec->spec[0].idx != 0)
+ return 0;
+ }
+
if (core_relo_is_type_based(local_spec->relo_kind)) {
if (local_spec->relo_kind == BPF_CORE_TYPE_MATCHES)
return bpf_core_types_match(local_spec->btf,
@@ -627,7 +972,20 @@ static int bpf_core_spec_match(struct bpf_core_spec *local_spec,
if (!targ_type)
return -EINVAL;
- if (local_acc->name) {
+ if (func_args && i == 0) {
+ /* args[0], i.e., the context itself */
+ targ_acc->type_id = targ_id;
+ targ_acc->idx = 0;
+ targ_acc->name = NULL;
+ targ_spec->len++;
+ targ_spec->raw_spec[targ_spec->raw_len++] = 0;
+ } else if (func_args && i == 1) {
+ matched = bpf_core_match_func_arg(local_spec->btf,
+ local_acc, cand,
+ targ_spec, &targ_id);
+ if (matched <= 0)
+ return matched;
+ } else if (local_acc->name) {
matched = bpf_core_match_member(local_spec->btf,
local_acc,
targ_btf, targ_id,
@@ -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);
+ }
bit_off = spec->bit_offset;
- bit_sz = btf_member_bitfield_size(t, acc->idx);
bitfield = bit_sz > 0;
if (bitfield) {
@@ -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.
+ */
+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;
+}
+
static int bpf_core_calc_relo(const char *prog_name,
const struct bpf_core_relo *relo,
int relo_idx,
@@ -909,6 +1306,9 @@ static int bpf_core_calc_relo(const char *prog_name,
res->fail_memsz_adjust = false;
res->orig_sz = res->new_sz = 0;
res->orig_type_id = res->new_type_id = 0;
+ res->widen_func_arg = false;
+ res->func_arg_signed = false;
+ res->func_arg_slot_off = 0;
if (core_relo_is_field_based(relo->kind)) {
err = bpf_core_calc_field_relo(prog_name, relo, local_spec,
@@ -919,6 +1319,12 @@ static int bpf_core_calc_relo(const char *prog_name,
&res->new_type_id, NULL);
if (err)
goto done;
+ if (relo->kind == BPF_CORE_FIELD_BYTE_OFFSET && targ_spec &&
+ bpf_core_is_be_small_func_arg(targ_spec, &res->func_arg_signed)) {
+ /* context slots are 8-byte aligned */
+ res->widen_func_arg = true;
+ res->func_arg_slot_off = res->new_val & ~7ULL;
+ }
/* Validate if it's safe to adjust load/store memory size.
* Adjustments are performed only if original and new memory
* sizes differ.
@@ -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;
+ pr_debug("prog '%s': relo #%d: patched insn #%d (LDX) off %llu -> %u, mem_sz %u -> 8 (big-endian function argument)\n",
+ prog_name, relo_idx, insn_idx, (unsigned long long)orig_val,
+ res->func_arg_slot_off, res->orig_sz);
+ break;
+ }
+
orig_val = insn->off;
insn->off = new_val;
pr_debug("prog '%s': relo #%d: patched insn #%d (LDX/ST/STX) off %llu -> %llu\n",
@@ -1356,8 +1794,7 @@ int bpf_core_calc_relo_insn(const char *prog_name,
}
for (i = 0, j = 0; i < cands->len; i++) {
- err = bpf_core_spec_match(local_spec, cands->cands[i].btf,
- cands->cands[i].id, cand_spec);
+ err = bpf_core_spec_match(local_spec, &cands->cands[i], cand_spec);
if (err < 0) {
bpf_core_format_spec(spec_buf, sizeof(spec_buf), cand_spec);
pr_warn("prog '%s': relo #%d: error matching candidate #%d %s: %d\n",
diff --git a/tools/lib/bpf/relo_core.h b/tools/lib/bpf/relo_core.h
index 1c0566daf8e8..e51d33ba2d7f 100644
--- a/tools/lib/bpf/relo_core.h
+++ b/tools/lib/bpf/relo_core.h
@@ -6,9 +6,26 @@
#include <linux/bpf.h>
+/* names of BTF decl tags marking function argument structs (and members
+ * referring to the return value) whose fields are relocated against the
+ * parameters of a tracing program's attach target, see __bpf_func_args
+ */
+#define BPF_CORE_FUNC_ARGS_TAG "bpf_func_args"
+#define BPF_CORE_FUNC_RET_TAG "bpf_func_ret"
+
+enum bpf_core_cand_flags {
+ /* id is a FUNC whose parameters are matched by name */
+ BPF_CORE_CAND_FUNC_ARGS = 1 << 0,
+ /* first parameter isn't in the program context (raw tracepoints) */
+ BPF_CORE_CAND_SKIP_FIRST = 1 << 1,
+ /* return value follows the arguments in the program context */
+ BPF_CORE_CAND_RETVAL = 1 << 2,
+};
+
struct bpf_core_cand {
const struct btf *btf;
__u32 id;
+ __u32 flags;
};
/* dynamically sized list of type IDs and its associated struct btf */
@@ -66,6 +83,13 @@ struct bpf_core_relo_res {
__u32 orig_type_id;
__u32 new_sz;
__u32 new_type_id;
+ /* loads of integer or enum function arguments smaller than 8 bytes on
+ * big-endian targets, which are in the low-order bytes of their
+ * context slot, are widened to the whole slot at func_arg_slot_off
+ */
+ bool widen_func_arg;
+ bool func_arg_signed;
+ __u32 func_arg_slot_off;
};
int __bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
@@ -79,6 +103,11 @@ int bpf_core_types_match(const struct btf *local_btf, __u32 local_id, const stru
size_t bpf_core_essential_name_len(const char *name);
+bool bpf_core_is_func_args_type(const struct btf *btf, __u32 type_id);
+int bpf_core_func_args_cand(const struct btf *btf, __u32 attach_btf_id,
+ enum bpf_attach_type attach_type,
+ struct bpf_core_cand *cand);
+
int bpf_core_calc_relo_insn(const char *prog_name,
const struct bpf_core_relo *relo, int relo_idx,
const struct btf *local_btf,
--
2.56.0
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH bpf-next 2/7] bpf: Support CO-RE relocation of tracing program 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 1/7] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
@ 2026-11-23 22:32 ` Alan Maguire
2026-11-23 22:52 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 3/7] selftests/bpf: Test " Alan Maguire
` (4 subsequent siblings)
6 siblings, 1 reply; 15+ 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
Light skeletons rely on the kernel to apply CO-RE relocations, which
only knows how to find candidates by type name, so the function
arguments structs supported by libbpf (__bpf_func_args) find no
candidate: all arguments appear to be missing.
Pass the program being loaded to bpf_core_apply() and, for relocations
against a function arguments struct, use the program's attach target
as the (single) candidate, as built by bpf_core_func_args_cand() in the
shared relo_core.c. prog->aux->attach_btf and attach_btf_id are already
set when bpf_check_core_relo() runs, and the program holds a reference
to attach_btf, so no candidate cache or locking is needed.
As the matching is shared with libbpf, parameter type changes are
handled the same way as when libbpf applies the relocations: members of
a different kind than their parameter, and pointers to a type with a
different kind or name than the parameter's (other than void *), don't
match and so appear as missing arguments; integer size changes are
handled as for struct fields. Likewise, on big-endian targets, loads of
signed arguments smaller than 8 bytes that aren't sign-extending are
poisoned, as the verifier only allows loads of whole context slots.
Unlike in libbpf, all subprograms belong to the program being loaded,
so relocations in subprograms work too.
Signed-off-by: Alan Maguire <almagui@kernel.org>
Assisted-by: LLM
---
include/linux/bpf.h | 2 ++
kernel/bpf/btf.c | 38 ++++++++++++++++++++++++++++++++++++++
kernel/bpf/check_btf.c | 1 +
3 files changed, 41 insertions(+)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 971e584c6efc..f814f4e8fd81 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3445,6 +3445,8 @@ 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;
+ /* program being loaded, if any, for function argument relocations */
+ 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 2f28b418a7fc..e37a77dec8b8 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -10697,11 +10697,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.
+ */
+static int bpf_core_func_args_cand_prog(struct bpf_core_ctx *ctx, int relo_idx,
+ struct bpf_core_cand *cand)
+{
+ const struct bpf_prog *prog = ctx->prog;
+ int err;
+
+ if ((prog->type != BPF_PROG_TYPE_TRACING && prog->type != BPF_PROG_TYPE_LSM) ||
+ !prog->aux->attach_btf || prog->aux->attach_btf_id == 0 ||
+ prog->aux->dst_prog) {
+ bpf_log(ctx->log,
+ "relo #%u: function argument relocations need a kernel attach target\n",
+ relo_idx);
+ return -EOPNOTSUPP;
+ }
+
+ err = bpf_core_func_args_cand(prog->aux->attach_btf, prog->aux->attach_btf_id,
+ prog->expected_attach_type, cand);
+ if (err)
+ bpf_log(ctx->log,
+ "relo #%u: can't relocate function arguments against attach target [%u]: %d\n",
+ relo_idx, prog->aux->attach_btf_id, err);
+ return err;
+}
+
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 bpf_core_cand func_args_cand;
struct bpf_core_relo_res targ_res;
struct bpf_core_spec *specs;
const struct btf_type *type;
@@ -10722,6 +10750,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;
+ }
+
if (need_cands) {
struct bpf_cand_cache *cc;
int i;
diff --git a/kernel/bpf/check_btf.c b/kernel/bpf/check_btf.c
index 79e079e907c9..e302feea9ca9 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;
--
2.56.0
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH bpf-next 3/7] selftests/bpf: Test CO-RE relocation of tracing program 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 1/7] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
2026-11-23 22:32 ` [PATCH bpf-next 2/7] bpf: " Alan Maguire
@ 2026-11-23 22:32 ` Alan Maguire
2026-11-23 22:43 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM " Alan Maguire
` (3 subsequent siblings)
6 siblings, 1 reply; 15+ 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
Exercise __bpf_func_args structs with:
- fentry/fexit on bpf_fentry_test3() with members in a different order
than the parameters, a member with no matching parameter (both with
and without a ___flavor suffix), a member whose type is incompatible
with the matching parameter, a ___flavor alias, and a __bpf_func_ret
member that only exists for fexit;
- fexit on a bpf_testmod function taking a 16-byte struct by value,
which occupies two context slots and shifts the following arguments;
- tp_btf on a bpf_testmod tracepoint, where parameter names come from
__probestub_bpf_testmod_test_read(), including pointer members whose
pointed-to type differs from the parameter's (no match), is a
forward declaration of the same struct (match), or is void (match);
- fmod_ret on bpf_modify_return_test(), with members out of order, a
missing member and the return value slot (holding the return value of
previous fmod_ret programs);
- fentry on bpf_fentry_test3() with a typedef'd anonymous struct, whose
relocations have the typedef rather than the struct as their root type.
Also exercise BPF_PROG_CORE() on the same targets: reordered arguments,
a missing argument reading as zero with bpf_func_arg_exists() reporting
it as absent, an argument whose type is incompatible with the matching
parameter, which also reads as zero and is reported as absent, the
return value on fexit (and its absence on fentry), a tp_btf program,
and an fsession program on bpf_fentry_test3(), which calls
bpf_session_is_return() on the ctx and checks that the return value
exists on both entry and exit, reading 0 and 15 respectively.
Argument structs use __bpf_func_args_marker, so that they also work when
built with GCC, except for the one used by the fentry/fexit programs on
bpf_fentry_test3(), which only has the struct-level tag when built with
clang, to also cover that form.
Build the object both as a skeleton and a light skeleton, so that both
libbpf's and the kernel's CO-RE relocation are covered.
Skip the test on big-endian targets: the programs read signed int
arguments and return values with zero-extending loads (directly and via
BPF_PROG_CORE()), which can't be relocated there, as the verifier only
allows loads of whole context slots, so they would fail to load.
Signed-off-by: Alan Maguire <almagui@kernel.org>
Assisted-by: LLM
---
tools/testing/selftests/bpf/Makefile.skel | 2 +-
.../selftests/bpf/prog_tests/func_args_core.c | 130 ++++++++++
.../selftests/bpf/progs/func_args_core.c | 228 ++++++++++++++++++
3 files changed, 359 insertions(+), 1 deletion(-)
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
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 45e4ce5dca4d..69808d962fc7 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -40,7 +40,7 @@ LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
# Generate both light skeleton and libbpf skeleton for these
LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
- kfunc_call_test_subprog.c test_global_percpu_data.c
+ kfunc_call_test_subprog.c test_global_percpu_data.c func_args_core.c
SKEL_BLACKLIST += $(LSKELS) $(LSKELS_SIGNED)
test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o
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 000000000000..1516ad13eaa2
--- /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
+ */
+static void trigger(int fentry_prog_fd, int fmod_ret_prog_fd)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, topts);
+
+ ASSERT_OK(bpf_prog_test_run_opts(fentry_prog_fd, &topts), "test_run");
+ ASSERT_OK(bpf_prog_test_run_opts(fmod_ret_prog_fd, &topts), "test_run_fmod_ret");
+ ASSERT_OK(trigger_module_test_read(256), "trigger_read");
+}
+
+/* the skeleton and light skeleton have identically named bss/data members */
+#define check_results(skel) ({ \
+ ASSERT_EQ(skel->bss->t3_a, 4, "t3_a"); \
+ ASSERT_EQ(skel->bss->t3_b, 5, "t3_b"); \
+ ASSERT_EQ(skel->bss->t3_c, 6, "t3_c"); \
+ ASSERT_EQ(skel->bss->t3_b_alias, 5, "t3_b_alias"); \
+ ASSERT_EQ(skel->data->t3_missing_exists, 0, "t3_missing_exists"); \
+ ASSERT_EQ(skel->data->t3_bad_type_exists, 0, "t3_bad_type_exists"); \
+ ASSERT_EQ(skel->data->t3_missing_flavor_exists, 0, "t3_missing_flavor_exists"); \
+ 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->bss->fm_calls, 1, "fm_calls"); \
+ ASSERT_EQ(skel->bss->fm_a, 1, "fm_a"); \
+ ASSERT_TRUE(skel->bss->fm_b_set, "fm_b_set"); \
+ ASSERT_EQ(skel->data->fm_ret, 0, "fm_ret"); \
+ ASSERT_EQ(skel->data->fm_ret_exists, 1, "fm_ret_exists"); \
+ ASSERT_EQ(skel->data->fm_missing_exists, 0, "fm_missing_exists"); \
+ \
+ ASSERT_EQ(skel->data->fs_ret_exists, 1, "fs_ret_exists"); \
+ ASSERT_EQ(skel->bss->fs_entry_calls, 1, "fs_entry_calls"); \
+ ASSERT_EQ(skel->bss->fs_exit_calls, 1, "fs_exit_calls"); \
+ ASSERT_EQ(skel->bss->fs_entry_c, 6, "fs_entry_c"); \
+ ASSERT_EQ(skel->bss->fs_exit_c, 6, "fs_exit_c"); \
+ ASSERT_EQ(skel->data->fs_entry_ret, 0, "fs_entry_ret"); \
+ ASSERT_EQ(skel->data->fs_exit_ret, 15, "fs_exit_ret"); \
+ \
+ ASSERT_EQ(skel->bss->m3_a, 4, "m3_a"); \
+ ASSERT_EQ(skel->bss->m3_b, 5, "m3_b"); \
+ ASSERT_EQ(skel->bss->m3_c, 6, "m3_c"); \
+ ASSERT_EQ(skel->data->m3_missing, 0, "m3_missing"); \
+ ASSERT_EQ(skel->data->m3_missing_exists, 0, "m3_missing_exists"); \
+ ASSERT_EQ(skel->bss->m3_ret, 15, "m3_ret"); \
+ ASSERT_EQ(skel->data->m3_ret_exists, 1, "m3_ret_exists"); \
+ \
+ ASSERT_EQ(skel->bss->mf_b, 5, "mf_b"); \
+ ASSERT_EQ(skel->data->mf_ret, 0, "mf_ret"); \
+ ASSERT_EQ(skel->data->mf_ret_exists, 0, "mf_ret_exists"); \
+ ASSERT_EQ(skel->data->mf_a, 0, "mf_a"); \
+ ASSERT_EQ(skel->data->mf_a_exists, 0, "mf_a_exists"); \
+ \
+ ASSERT_EQ(skel->bss->td_c, 6, "td_c"); \
+ ASSERT_EQ(skel->data->td_missing_exists, 0, "td_missing_exists"); \
+ \
+ ASSERT_EQ(skel->bss->s1_a_a, 2, "s1_a_a"); \
+ ASSERT_EQ(skel->bss->s1_a_b, 3, "s1_a_b"); \
+ ASSERT_EQ(skel->bss->s1_b, 1, "s1_b"); \
+ ASSERT_EQ(skel->bss->s1_c, 4, "s1_c"); \
+ ASSERT_EQ(skel->bss->s1_ret, 10, "s1_ret"); \
+ \
+ ASSERT_EQ(skel->bss->tp_pid, getpid(), "tp_pid"); \
+ ASSERT_EQ(skel->bss->tp_len, 256, "tp_len"); \
+ ASSERT_EQ(skel->data->tp_bad_ptr_exists, 0, "tp_bad_ptr_exists"); \
+ ASSERT_EQ(skel->data->tp_fwd_exists, 1, "tp_fwd_exists"); \
+ ASSERT_EQ(skel->data->tp_void_exists, 1, "tp_void_exists"); \
+ ASSERT_EQ(skel->data->tp_void_same, 1, "tp_void_same"); \
+ ASSERT_EQ(skel->bss->mtp_pid, getpid(), "mtp_pid"); \
+})
+
+/* CO-RE relocations applied by libbpf */
+static void test_skel(void)
+{
+ struct func_args_core *skel;
+
+ skel = func_args_core__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+ return;
+ if (!ASSERT_OK(func_args_core__attach(skel), "skel_attach"))
+ goto cleanup;
+
+ trigger(bpf_program__fd(skel->progs.fentry3),
+ bpf_program__fd(skel->progs.fmod_ret_test));
+ check_results(skel);
+cleanup:
+ func_args_core__destroy(skel);
+}
+
+/* CO-RE relocations applied by the kernel */
+static void test_lskel(void)
+{
+ struct func_args_core_lskel *skel;
+
+ skel = func_args_core_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "lskel_open_and_load"))
+ return;
+ if (!ASSERT_OK(func_args_core_lskel__attach(skel), "lskel_attach"))
+ goto cleanup;
+
+ trigger(skel->progs.fentry3.prog_fd, skel->progs.fmod_ret_test.prog_fd);
+ check_results(skel);
+cleanup:
+ func_args_core_lskel__destroy(skel);
+}
+
+void test_func_args_core(void)
+{
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ /* On big-endian targets, the verifier only allows loads of whole
+ * context slots, and loads of signed arguments smaller than 8 bytes
+ * can only be widened to their slot if they are sign-extending. The
+ * programs read signed int arguments and return values with
+ * zero-extending loads (directly and via BPF_PROG_CORE()), which
+ * are poisoned there, so they would fail to load.
+ */
+ test__skip();
+ return;
+#endif
+ if (test__start_subtest("skel"))
+ test_skel();
+ if (test__start_subtest("lskel"))
+ test_lskel();
+}
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 000000000000..4656a6c642d1
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/func_args_core.c
@@ -0,0 +1,228 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+#include "../test_kmods/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+/* int bpf_fentry_test3(char a, int b, u64 c), called as (4, 5, 6) */
+struct fentry_test3_args {
+#ifndef __clang__
+ /* GCC doesn't support BTF decl tags on types, see __bpf_func_args;
+ * with clang, this struct relies on the struct-level tag only
+ */
+ __bpf_func_args_marker;
+#endif
+ /* deliberately in a different order to the target */
+ __u64 c;
+ int b;
+ char a;
+ /* no such parameter */
+ long missing;
+ /* flavor of a parameter that doesn't exist */
+ long missing___v2;
+ /* parameter exists, but the type isn't compatible */
+ struct task_struct *a___bad_type;
+ /* flavor of an existing parameter */
+ int b___alias;
+ int ret __bpf_func_ret;
+} __bpf_func_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_fentry_ret_exists = -1, t3_ret_exists = -1;
+
+SEC("fentry/bpf_fentry_test3")
+int fentry3(struct fentry_test3_args *args)
+{
+ t3_a = args->a;
+ t3_b = args->b;
+ t3_c = args->c;
+ t3_b_alias = args->b___alias;
+ t3_missing_exists = bpf_core_field_exists(args->missing);
+ t3_bad_type_exists = bpf_core_field_exists(args->a___bad_type);
+ t3_missing_flavor_exists = bpf_core_field_exists(args->missing___v2);
+ /* the return value isn't available on entry */
+ t3_fentry_ret_exists = bpf_core_field_exists(args->ret);
+ return 0;
+}
+
+SEC("fexit/bpf_fentry_test3")
+int fexit3(struct fentry_test3_args *args)
+{
+ t3_ret_exists = bpf_core_field_exists(args->ret);
+ t3_ret = args->ret;
+ return 0;
+}
+
+/* typedef'd anonymous struct, for which relocations have the typedef rather
+ * than the (tagged) struct as their root type
+ */
+typedef struct {
+ __bpf_func_args_marker;
+ __u64 c;
+ long missing;
+} __bpf_func_args fentry_test3_td_args_t;
+
+__u64 td_c;
+int td_missing_exists = -1;
+
+SEC("fentry/bpf_fentry_test3")
+int fentry3_typedef(fentry_test3_td_args_t *args)
+{
+ td_c = args->c;
+ td_missing_exists = bpf_core_field_exists(args->missing);
+ return 0;
+}
+
+/* int bpf_modify_return_test(int a, int *b), called as (1, &b) */
+struct modify_return_args {
+ __bpf_func_args_marker;
+ int *b;
+ int a;
+ long missing;
+ int ret __bpf_func_ret;
+} __bpf_func_args;
+
+int fm_a, fm_calls, fm_ret = -1, fm_ret_exists = -1, fm_missing_exists = -1;
+bool fm_b_set;
+
+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;
+}
+
+/* int bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a,
+ * int b, int c), called as ({2, 3}, 1, 4);
+ * 'a' is passed by value and occupies two context slots.
+ */
+struct bpf_testmod_struct_arg_2 {
+ long a;
+ long b;
+};
+
+struct struct_arg_1_args {
+ __bpf_func_args_marker;
+ int c;
+ int b;
+ struct bpf_testmod_struct_arg_2 a;
+ int ret __bpf_func_ret;
+} __bpf_func_args;
+
+long s1_a_a, s1_a_b, s1_b, s1_c, s1_ret;
+
+SEC("fexit/bpf_testmod_test_struct_arg_1")
+int fexit_struct_arg(struct struct_arg_1_args *args)
+{
+ s1_a_a = args->a.a;
+ s1_a_b = args->a.b;
+ s1_b = args->b;
+ s1_c = args->c;
+ s1_ret = args->ret;
+ return 0;
+}
+
+/* TP_PROTO(struct task_struct *task, struct bpf_testmod_test_read_ctx *ctx) */
+struct task_struct___fwd;
+
+struct test_read_args {
+ __bpf_func_args_marker;
+ struct bpf_testmod_test_read_ctx *ctx;
+ struct task_struct *task;
+ /* pointer to a different type than the parameter's: no match */
+ struct file *task___bad_ptr;
+ /* forward-declared pointee with the same essential name: match */
+ struct task_struct___fwd *task___fwd;
+ /* void pointers match any pointer */
+ void *ctx___void;
+} __bpf_func_args;
+
+int tp_pid, tp_len;
+int tp_bad_ptr_exists = -1, tp_fwd_exists = -1, tp_void_exists = -1, tp_void_same = -1;
+
+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;
+}
+
+/* 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;
+
+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_a = a;
+ m3_b = b;
+ m3_c = c;
+ m3_missing = missing;
+ m3_missing_exists = bpf_func_arg_exists(missing);
+ m3_ret = ret;
+ m3_ret_exists = bpf_func_arg_exists(ret);
+ return 0;
+}
+
+__u64 mf_b, mf_ret = -1, mf_a = -1;
+int mf_ret_exists = -1, mf_a_exists = -1;
+
+SEC("fentry/bpf_fentry_test3")
+int BPF_PROG_CORE(fentry3_macro, int, b, __bpf_func_ret int, ret,
+ struct task_struct *, a)
+{
+ mf_b = b;
+ /* 'a' is a char, so a pointer isn't compatible: it reads as zero */
+ mf_a = (__u64)a;
+ mf_a_exists = bpf_func_arg_exists(a);
+ /* no return value on entry, so it reads as zero */
+ mf_ret = ret;
+ mf_ret_exists = bpf_func_arg_exists(ret);
+ return 0;
+}
+
+int mtp_pid;
+
+SEC("tp_btf/bpf_testmod_test_read")
+int BPF_PROG_CORE(tp_read_macro, struct task_struct *, task)
+{
+ mtp_pid = task->pid;
+ return 0;
+}
--
2.56.0
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM program arguments
2026-11-23 22:32 [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
` (2 preceding siblings ...)
2026-11-23 22:32 ` [PATCH bpf-next 3/7] selftests/bpf: Test " Alan Maguire
@ 2026-11-23 22:32 ` Alan Maguire
2026-11-23 22:41 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence Alan Maguire
` (2 subsequent siblings)
6 siblings, 1 reply; 15+ 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 function argument structs work for BPF LSM programs, where
the attach target is the bpf_lsm_<hook>() function, against the
file_ioctl hook (file, cmd, arg), triggered by an ioctl() on a memfd
with a command that no file implements. That fails with ENOTTY after
the hook ran, so whatever the programs return has no effect on the test
process, and no other selftest attaches to that hook:
- a __bpf_func_args struct describing the arguments out of order,
omitting cmd, adding a member with no matching parameter, and
reading the return value of the previous hook via __bpf_func_ret;
- BPF_PROG_CORE() with arguments out of order, the return value, and
an added argument which reads as zero and is reported as absent by
bpf_func_arg_exists(); the cmd passed has its top bit set, to check
that an unsigned int argument isn't sign-extended;
- both of these describing the file parameter with two flavors, as for
a (hypothetical) kernel in which it pointed to a renamed struct
vfs_file: 'struct vfs_file *file___new' doesn't match, and the
program reads the inode number through whichever variant exists,
which also checks that the poisoned accesses through the missing one
are dead code;
- the reverse, as for a program written for that kernel: both of these
describing the file parameter as 'struct vfs_file *file', which
doesn't match, and 'struct file *file___old', which does, checking
that a flavored member matches when the unflavored one doesn't.
The inode numbers read are checked against fstat() on the memfd.
Also cover BPF_LSM_CGROUP programs with a __bpf_func_args struct for the
socket_create hook (family, type, protocol, kern), attached to a test
cgroup and triggered via socket(AF_UNIX, SOCK_STREAM, 0), describing
the arguments out of order, omitting type, adding a member with no
matching parameter and checking that the return value slot exists.
As with the tracing tests, run these with both a skeleton and a light
skeleton to cover libbpf's and the kernel's CO-RE relocation. They are
also skipped on big-endian targets, as they read signed int arguments
and return values as well.
Signed-off-by: Alan Maguire <almagui@kernel.org>
Assisted-by: LLM
---
tools/testing/selftests/bpf/Makefile.skel | 3 +-
.../selftests/bpf/prog_tests/func_args_core.c | 189 ++++++++++++++++++
.../selftests/bpf/progs/func_args_core_lsm.c | 152 ++++++++++++++
.../bpf/progs/func_args_core_lsm_cgroup.c | 39 ++++
4 files changed, 382 insertions(+), 1 deletion(-)
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
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 69808d962fc7..3c48251ae5b7 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -40,7 +40,8 @@ LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
# Generate both light skeleton and libbpf skeleton for these
LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
- kfunc_call_test_subprog.c test_global_percpu_data.c func_args_core.c
+ kfunc_call_test_subprog.c test_global_percpu_data.c func_args_core.c \
+ func_args_core_lsm.c func_args_core_lsm_cgroup.c
SKEL_BLACKLIST += $(LSKELS) $(LSKELS_SIGNED)
test_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o
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 1516ad13eaa2..f4cc859f20cd 100644
--- a/tools/testing/selftests/bpf/prog_tests/func_args_core.c
+++ b/tools/testing/selftests/bpf/prog_tests/func_args_core.c
@@ -2,6 +2,14 @@
#include <test_progs.h>
#include "func_args_core.skel.h"
#include "func_args_core.lskel.h"
+#include "func_args_core_lsm.skel.h"
+#include "func_args_core_lsm.lskel.h"
+#include "func_args_core_lsm_cgroup.skel.h"
+#include "func_args_core_lsm_cgroup.lskel.h"
+#include "cgroup_helpers.h"
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/stat.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
@@ -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)
+{
+ struct stat st;
+ __u64 ino = 0;
+ int fd, err;
+
+ fd = memfd_create("func_args_core", MFD_CLOEXEC);
+ if (!ASSERT_OK_FD(fd, "memfd_create"))
+ return 0;
+ if (!ASSERT_OK(fstat(fd, &st), "fstat"))
+ goto out;
+
+ *monitored_pid = getpid();
+ err = ioctl(fd, LSM_IOCTL_CMD, LSM_IOCTL_ARG) ? -errno : 0;
+ *monitored_pid = 0;
+
+ if (ASSERT_EQ(err, -ENOTTY, "ioctl"))
+ ino = st.st_ino;
+out:
+ close(fd);
+ return ino;
+}
+
+/* file_ioctl(file, cmd, arg) via lsm_ioctl() */
+#define check_lsm_results(skel, ino) ({ \
+ ASSERT_EQ(skel->bss->s_calls, 1, "s_calls"); \
+ ASSERT_EQ(skel->bss->s_arg, LSM_IOCTL_ARG, "s_arg"); \
+ ASSERT_EQ(skel->bss->s_ino, ino, "s_ino"); \
+ 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"); \
+ \
+ /* file___new (struct vfs_file *) doesn't match, file is used instead */ \
+ ASSERT_EQ(skel->data->v_new_exists, 0, "v_new_exists"); \
+ ASSERT_EQ(skel->data->v_old_exists, 1, "v_old_exists"); \
+ ASSERT_EQ(skel->bss->v_ino, ino, "v_ino"); \
+ ASSERT_EQ(skel->data->vm_new_exists, 0, "vm_new_exists"); \
+ ASSERT_EQ(skel->data->vm_old_exists, 1, "vm_old_exists"); \
+ ASSERT_EQ(skel->bss->vm_ino, ino, "vm_ino"); \
+ \
+ /* only the flavored file___old matches */ \
+ ASSERT_EQ(skel->data->o_new_exists, 0, "o_new_exists"); \
+ ASSERT_EQ(skel->data->o_old_exists, 1, "o_old_exists"); \
+ ASSERT_EQ(skel->bss->o_ino, ino, "o_ino"); \
+ ASSERT_EQ(skel->data->om_new_exists, 0, "om_new_exists"); \
+ ASSERT_EQ(skel->data->om_old_exists, 1, "om_old_exists"); \
+ ASSERT_EQ(skel->bss->om_ino, ino, "om_ino"); \
+ \
+ ASSERT_EQ(skel->bss->m_calls, 1, "m_calls"); \
+ ASSERT_EQ(skel->bss->m_arg, LSM_IOCTL_ARG, "m_arg"); \
+ ASSERT_EQ(skel->bss->m_cmd, LSM_IOCTL_CMD, "m_cmd"); \
+ ASSERT_EQ(skel->data->m_added, 0, "m_added"); \
+ ASSERT_EQ(skel->data->m_added_exists, 0, "m_added_exists"); \
+ ASSERT_EQ(skel->data->m_ret, 0, "m_ret"); \
+ ASSERT_EQ(skel->data->m_ret_exists, 1, "m_ret_exists"); \
+})
+
+static void test_lsm_skel(void)
+{
+ struct func_args_core_lsm *skel;
+ __u64 ino;
+
+ skel = func_args_core_lsm__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+ return;
+ if (!ASSERT_OK(func_args_core_lsm__attach(skel), "skel_attach"))
+ goto cleanup;
+
+ ino = lsm_ioctl(&skel->bss->monitored_pid);
+ if (ASSERT_NEQ(ino, 0, "lsm_ioctl"))
+ check_lsm_results(skel, ino);
+cleanup:
+ func_args_core_lsm__destroy(skel);
+}
+
+static void test_lsm_lskel(void)
+{
+ struct func_args_core_lsm_lskel *skel;
+ __u64 ino;
+
+ skel = func_args_core_lsm_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "lskel_open_and_load"))
+ return;
+ if (!ASSERT_OK(func_args_core_lsm_lskel__attach(skel), "lskel_attach"))
+ goto cleanup;
+
+ ino = lsm_ioctl(&skel->bss->monitored_pid);
+ if (ASSERT_NEQ(ino, 0, "lsm_ioctl"))
+ check_lsm_results(skel, ino);
+cleanup:
+ func_args_core_lsm_lskel__destroy(skel);
+}
+
+/* socket_create(family, type, protocol, kern) via socket(AF_UNIX, ...) */
+#define check_lsm_cgroup_results(skel) ({ \
+ ASSERT_EQ(skel->bss->lc_calls, 1, "lc_calls"); \
+ ASSERT_EQ(skel->data->lc_family, AF_UNIX, "lc_family"); \
+ ASSERT_EQ(skel->data->lc_protocol, 0, "lc_protocol"); \
+ 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"); \
+})
+
+static void trigger_lsm_cgroup(void)
+{
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+
+ if (ASSERT_OK_FD(fd, "socket"))
+ close(fd);
+}
+
+static void test_lsm_cgroup_skel(void)
+{
+ struct func_args_core_lsm_cgroup *skel;
+ struct bpf_link *link = NULL;
+ int cgroup_fd;
+
+ cgroup_fd = test__join_cgroup("/func_args_core");
+ if (!ASSERT_OK_FD(cgroup_fd, "join_cgroup"))
+ return;
+
+ skel = func_args_core_lsm_cgroup__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+ goto out;
+ link = bpf_program__attach_cgroup(skel->progs.socket_create_struct, cgroup_fd);
+ if (!ASSERT_OK_PTR(link, "attach_cgroup"))
+ goto out;
+
+ trigger_lsm_cgroup();
+ check_lsm_cgroup_results(skel);
+out:
+ bpf_link__destroy(link);
+ func_args_core_lsm_cgroup__destroy(skel);
+ close(cgroup_fd);
+}
+
+static void test_lsm_cgroup_lskel(void)
+{
+ struct func_args_core_lsm_cgroup_lskel *skel;
+ int cgroup_fd, link_fd = -1;
+
+ cgroup_fd = test__join_cgroup("/func_args_core");
+ if (!ASSERT_OK_FD(cgroup_fd, "join_cgroup"))
+ return;
+
+ skel = func_args_core_lsm_cgroup_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "lskel_open_and_load"))
+ goto out;
+ link_fd = bpf_link_create(skel->progs.socket_create_struct.prog_fd, cgroup_fd,
+ BPF_LSM_CGROUP, NULL);
+ if (!ASSERT_OK_FD(link_fd, "link_create"))
+ goto out;
+
+ trigger_lsm_cgroup();
+ check_lsm_cgroup_results(skel);
+out:
+ if (link_fd >= 0)
+ close(link_fd);
+ func_args_core_lsm_cgroup_lskel__destroy(skel);
+ close(cgroup_fd);
+}
+
void test_func_args_core(void)
{
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
@@ -127,4 +308,12 @@ void test_func_args_core(void)
test_skel();
if (test__start_subtest("lskel"))
test_lskel();
+ if (test__start_subtest("lsm_skel"))
+ test_lsm_skel();
+ if (test__start_subtest("lsm_lskel"))
+ test_lsm_lskel();
+ if (test__start_subtest("lsm_cgroup_skel"))
+ test_lsm_cgroup_skel();
+ if (test__start_subtest("lsm_cgroup_lskel"))
+ test_lsm_cgroup_lskel();
}
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 000000000000..f4b9e68e4578
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/func_args_core_lsm.c
@@ -0,0 +1,152 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char _license[] SEC("license") = "GPL";
+
+int monitored_pid;
+
+/* int file_ioctl(struct file *file, unsigned int cmd, unsigned long arg) */
+struct file_ioctl_args {
+ __bpf_func_args_marker;
+ /* out of order */
+ unsigned long arg;
+
+ int ret __bpf_func_ret;
+ struct file *file;
+ /* added: no such parameter */
+ unsigned long added;
+ /* missing: cmd isn't described */
+} __bpf_func_args;
+
+__u64 s_arg, s_ino;
+int s_ret = -1, s_ret_exists = -1, s_added_exists = -1, s_calls;
+
+SEC("lsm/file_ioctl")
+int ioctl_struct(struct file_ioctl_args *args)
+{
+ if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
+ return 0;
+
+ s_calls++;
+ s_arg = args->arg;
+ s_ino = args->file->f_inode->i_ino;
+ s_ret = args->ret;
+ s_ret_exists = bpf_core_field_exists(args->ret);
+ s_added_exists = bpf_core_field_exists(args->added);
+ return 0;
+}
+
+/* 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 {
+ struct inode *f_inode;
+} __attribute__((preserve_access_index));
+
+struct file_ioctl_variant_args {
+ __bpf_func_args_marker;
+ struct vfs_file *file___new;
+ struct file *file;
+} __bpf_func_args;
+
+__u64 v_ino, vm_ino;
+int v_new_exists = -1, v_old_exists = -1, vm_new_exists = -1, vm_old_exists = -1;
+
+SEC("lsm/file_ioctl")
+int ioctl_variants(struct file_ioctl_variant_args *args)
+{
+ if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
+ return 0;
+
+ v_new_exists = bpf_core_field_exists(args->file___new);
+ v_old_exists = bpf_core_field_exists(args->file);
+ if (bpf_core_field_exists(args->file___new))
+ v_ino = args->file___new->f_inode->i_ino;
+ else if (bpf_core_field_exists(args->file))
+ v_ino = args->file->f_inode->i_ino;
+ return 0;
+}
+
+SEC("lsm/file_ioctl")
+int BPF_PROG_CORE(ioctl_macro_variants, struct vfs_file *, file___new,
+ struct file *, file)
+{
+ if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
+ return 0;
+
+ vm_new_exists = bpf_func_arg_exists(file___new);
+ vm_old_exists = bpf_func_arg_exists(file);
+ if (bpf_func_arg_exists(file___new))
+ vm_ino = file___new->f_inode->i_ino;
+ else if (bpf_func_arg_exists(file))
+ vm_ino = file->f_inode->i_ino;
+ return 0;
+}
+
+__u64 m_arg, m_cmd, m_added = -1;
+int m_ret = -1, m_ret_exists = -1, m_added_exists = -1, m_calls;
+
+SEC("lsm/file_ioctl")
+int BPF_PROG_CORE(ioctl_macro, unsigned long, arg, __bpf_func_ret int, ret,
+ unsigned long, added, unsigned int, cmd)
+{
+ if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
+ return 0;
+
+ m_calls++;
+ m_arg = arg;
+ m_cmd = cmd;
+ m_added = added;
+ m_added_exists = bpf_func_arg_exists(added);
+ m_ret = ret;
+ m_ret_exists = bpf_func_arg_exists(ret);
+ return 0;
+}
+
+/* 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 {
+ __bpf_func_args_marker;
+ struct vfs_file *file;
+ struct file *file___old;
+} __bpf_func_args;
+
+__u64 o_ino, om_ino;
+int o_new_exists = -1, o_old_exists = -1, om_new_exists = -1, om_old_exists = -1;
+
+SEC("lsm/file_ioctl")
+int ioctl_old_variant(struct file_ioctl_old_variant_args *args)
+{
+ if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
+ return 0;
+
+ o_new_exists = bpf_core_field_exists(args->file);
+ o_old_exists = bpf_core_field_exists(args->file___old);
+ if (bpf_core_field_exists(args->file))
+ o_ino = args->file->f_inode->i_ino;
+ else if (bpf_core_field_exists(args->file___old))
+ o_ino = args->file___old->f_inode->i_ino;
+ return 0;
+}
+
+SEC("lsm/file_ioctl")
+int BPF_PROG_CORE(ioctl_macro_old_variant, struct vfs_file *, file,
+ struct file *, file___old)
+{
+ if ((bpf_get_current_pid_tgid() >> 32) != monitored_pid)
+ return 0;
+
+ om_new_exists = bpf_func_arg_exists(file);
+ om_old_exists = bpf_func_arg_exists(file___old);
+ if (bpf_func_arg_exists(file))
+ om_ino = file->f_inode->i_ino;
+ else if (bpf_func_arg_exists(file___old))
+ om_ino = file___old->f_inode->i_ino;
+ return 0;
+}
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
new file mode 100644
index 000000000000..2743c58f13aa
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/func_args_core_lsm_cgroup.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char _license[] SEC("license") = "GPL";
+
+/* int socket_create(int family, int type, int protocol, int kern) */
+struct socket_create_args {
+ __bpf_func_args_marker;
+ /* out of order */
+ int kern;
+ int protocol;
+ int ret __bpf_func_ret;
+ int family;
+ /* added: no such parameter */
+ long added;
+ /* missing: type isn't described */
+} __bpf_func_args;
+
+int lc_calls, lc_family = -1, lc_protocol = -1, lc_kern = -1;
+int lc_ret_exists = -1, lc_added_exists = -1;
+
+SEC("lsm_cgroup/socket_create")
+int socket_create_struct(struct socket_create_args *args)
+{
+ if (args->family != 1 /* AF_UNIX */)
+ return 1;
+
+ lc_calls++;
+ lc_family = args->family;
+ lc_protocol = args->protocol;
+ lc_kern = args->kern;
+ lc_ret_exists = bpf_core_field_exists(args->ret);
+ lc_added_exists = bpf_core_field_exists(args->added);
+ /* allow */
+ return 1;
+}
--
2.56.0
^ permalink raw reply related [flat|nested] 15+ 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
` (3 preceding siblings ...)
2026-11-23 22:32 ` [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM " 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
2026-11-23 22:32 ` [PATCH bpf-next 7/7] Documentation/bpf: Describe CO-RE relocation of function arguments Alan Maguire
6 siblings, 1 reply; 15+ 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] 15+ 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
` (4 preceding siblings ...)
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
6 siblings, 1 reply; 15+ 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] 15+ 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
` (5 preceding siblings ...)
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
6 siblings, 0 replies; 15+ 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] 15+ 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; 15+ 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] 15+ 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; 15+ 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] 15+ messages in thread
* Re: [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM program arguments
2026-11-23 22:32 ` [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM " Alan Maguire
@ 2026-11-23 22:41 ` sashiko-bot
0 siblings, 0 replies; 15+ 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] 15+ messages in thread
* Re: [PATCH bpf-next 3/7] selftests/bpf: Test CO-RE relocation of tracing program arguments
2026-11-23 22:32 ` [PATCH bpf-next 3/7] selftests/bpf: Test " Alan Maguire
@ 2026-11-23 22:43 ` sashiko-bot
0 siblings, 0 replies; 15+ 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] 15+ messages in thread
* Re: [PATCH bpf-next 2/7] bpf: Support CO-RE relocation of tracing program arguments
2026-11-23 22:32 ` [PATCH bpf-next 2/7] bpf: " Alan Maguire
@ 2026-11-23 22:52 ` sashiko-bot
0 siblings, 0 replies; 15+ 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] 15+ messages in thread
* Re: [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments
2026-11-23 22:32 ` [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
@ 2026-11-23 22:53 ` sashiko-bot
2026-11-23 23:23 ` bot+bpf-ci
1 sibling, 0 replies; 15+ 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] 15+ messages in thread
* Re: [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments
2026-11-23 22:32 ` [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
2026-11-23 22:53 ` sashiko-bot
@ 2026-11-23 23:23 ` bot+bpf-ci
1 sibling, 0 replies; 15+ 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] 15+ messages in thread
end of thread, other threads:[~2026-11-23 23:23 UTC | newest]
Thread overview: 15+ 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 1/7] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
2026-11-23 22:53 ` sashiko-bot
2026-11-23 23:23 ` bot+bpf-ci
2026-11-23 22:32 ` [PATCH bpf-next 2/7] bpf: " Alan Maguire
2026-11-23 22:52 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 3/7] selftests/bpf: Test " Alan Maguire
2026-11-23 22:43 ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM " Alan Maguire
2026-11-23 22:41 ` sashiko-bot
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox