BPF List
 help / color / mirror / Atom feed
From: Alan Maguire <almagui@kernel.org>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org
Cc: eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev,
	song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org,
	emil@etsalapatis.com, ihor.solodrai@linux.dev,
	john.fastabend@gmail.com, bpf@vger.kernel.org,
	Alan Maguire <almagui@kernel.org>
Subject: [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence
Date: Mon, 23 Nov 2026 22:32:25 +0000	[thread overview]
Message-ID: <20261123223227.745345-6-almagui@kernel.org> (raw)
In-Reply-To: <20261123223227.745345-1-almagui@kernel.org>

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


  parent reply	other threads:[~2026-11-23 22:32 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Alan Maguire [this message]
2026-11-23 22:38   ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence 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

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20261123223227.745345-6-almagui@kernel.org \
    --to=almagui@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

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

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