BPF List
 help / color / mirror / Atom feed
From: Alan Maguire <almagui@kernel.org>
To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org
Cc: andrey.grodzovsky@crowdstrike.com, 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,
	mason@kernel.org, bpf@vger.kernel.org,
	Alan Maguire <almagui@kernel.org>
Subject: [PATCH v2 bpf-next 0/5] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering
Date: Wed, 25 Nov 2026 16:58:40 +0000	[thread overview]
Message-ID: <20261125165845.45806-1-almagui@kernel.org> (raw)

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 larger than 8 bytes taking more than
one slot.

When an attach target gains, loses or reorders parameters
across kernels, programs silently read the wrong argument
or fail verification, and they have no way to check if an
argument actually exists.

To bring a Compile Once - Run Everywhere (CO-RE) experience
to parameters for programs, this series lets programs describe
the parameters they need and any variants via a
struct bpf_func_args___<flavor> which can contain

- parameters in any order, to support cases where a user might
  only use a subset of parameters in the associated program
- parameters that may exist for some kernels, where use is
  guarded by bpf_core_field_exists(ctx->field)
- renamed parameters, allowing the old and new names both to
  be present
- parameters whose type varies across kernels: a pointer can be read
  through the same struct either way, and bpf_core_type_matches() on
  a flavor with just that parameter tells which type it is
- __ret for the return value, for fexit, fmod_ret, fsession and
  LSM programs

For example, a program tracing a function foo() can declare just the
parameters it needs, a, b and c, in any order, irrespective of their
position in foo()'s signature. It can also declare a parameter "notsure"
that only some kernels have, and check whether it exists, and both names
of a parameter renamed from "old" to "new". Parameter "d" is a struct
file * on some kernels and a void * on others: as any two pointers are
compatible, it can be read through the same struct either way, and a
flavor with just that parameter tells which it is. __ret is the return
value.

        struct bpf_func_args___foo {
                __u64 c;
                int b;
                char a;
                long notsure;
                int old;
                int new;
                void *d;
                int __ret;
        } __attribute__((preserve_access_index));

        struct bpf_func_args___foo_d_file {
                struct file *d;
        } __attribute__((preserve_access_index));

        SEC("fexit/foo")
        int fexit_foo(struct bpf_func_args___foo *ctx)
        {
                a = ctx->a;
                b = ctx->b;
                c = ctx->c;
                if (bpf_core_field_exists(ctx->notsure))
                        notsure = ctx->notsure;
                if (bpf_core_field_exists(ctx->new))
                        val = ctx->new;
                else
                        val = ctx->old;
                if (bpf_core_type_matches(struct bpf_func_args___foo_d_file))
                        file = (struct file *)ctx->d;
                ret = ctx->__ret;
                return 0;
        }

Other changes, e.g. a parameter of another integer width or a struct
passed by value becoming a pointer, are covered in the libbpf overview.
See also tools/testing/selftests/bpf/progs/func_args_core.c for more
examples of usage.

Patch 1 allows loads of the low-order bytes of context slots on
big-endian, where arguments smaller than 8 bytes are at the end of their
slot.
Patch 2 adds the libbpf side.
Patch 3 adds the kernel side.
Patch 4 adds selftests.
Patch 5 adds documentation for libbpf/llvm reloc.

Changes since v1 [1]:

- Reworked as suggested by Alexei: rather than matching struct members
  against the attach target's parameters in relo_core.c, libbpf (and,
  for light skeletons, the kernel) builds a struct describing the
  program's context in a split BTF on top of the attach target's BTF,
  with one member per parameter at its context offset, plus __ret. This
  struct is the only CO-RE candidate, so relocations against it are
  ordinary CO-RE, and relo_core.c is unchanged.

- Structs are recognised by name, bpf_func_args___<flavor>, rather than
  by a BTF decl tag. This works the same with clang and GCC, so
  __bpf_func_args, __bpf_func_args_marker and __bpf_func_ret are gone.
  The return value is the member __ret. The flavor is everything after
  the first "___", so any flavor works.

- Dropped BPF_PROG_CORE(): it couldn't express the variants that are
  the main reason to relocate arguments (Alexei). Programs use the
  struct directly instead.

- Dropped bpf_func_args_supported() and the type-existence special
  case: as the candidate is named after the program's struct,
  type-based relocations, including bpf_core_type_exists() and
  bpf_core_type_matches(), work as usual.

- Pointer parameters follow the usual CO-RE rules, where any two
  pointers are compatible, instead of a stricter pointer check.
  bpf_core_type_matches() on a flavor containing just the parameter
  tells which type it points to.

- Big-endian: arguments smaller than 8 bytes are placed at the end of
  their slot, and a new verifier patch (patch 1) allows loads of the
  low-order bytes of context slots there, rather than widening loads
  and poisoning signed ones. The s390x selftest restriction is gone.

- libbpf: an attach target that can't be found no longer fails
  bpf_object__prepare(); the struct just has no candidate, and the
  program fails to load if it is loaded. Once the object is prepared,
  bpf_program__set_type(), bpf_program__set_expected_attach_type() and
  bpf_program__set_attach_target() fail with -EBUSY for programs whose
  relocations depend on them (Andrey).

- Selftests: test the libbpf and light skeleton paths, the LSM test
  folded in, plus renamed and missing parameters, struct and type
  variants, integer resizing, and the prepare-time behaviour above.

- Docs: llvm_reloc.rst describes the relocation for loader
  implementers; the libbpf overview covers common cases (a subset of
  parameters in any order; parameters that are missing, renamed or of
  another type on some kernels) and limitations.

[1] https://lore.kernel.org/bpf/20261123223227.745345-1-almagui@kernel.org/

Alan Maguire (5):
  bpf: Allow loads of the low-order bytes of context slots on big-endian
  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
  Documentation/bpf: Describe CO-RE relocation of function arguments

 Documentation/bpf/libbpf/libbpf_overview.rst  | 123 +++++++
 Documentation/bpf/llvm_reloc.rst              |  72 ++++
 include/linux/bpf.h                           |   2 +
 kernel/bpf/btf.c                              | 253 ++++++++++++-
 kernel/bpf/check_btf.c                        |   1 +
 tools/lib/bpf/libbpf.c                        | 333 ++++++++++++++++++
 tools/lib/bpf/libbpf.h                        |  13 +-
 tools/testing/selftests/bpf/Makefile.skel     |   2 +-
 .../selftests/bpf/prog_tests/func_args_core.c | 129 +++++++
 .../selftests/bpf/progs/func_args_core.c      | 178 ++++++++++
 10 files changed, 1099 insertions(+), 7 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

-- 
2.56.0


             reply	other threads:[~2026-11-25 16:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 16:58 Alan Maguire [this message]
2026-11-25 16:58 ` [PATCH v2 bpf-next 1/5] bpf: Allow loads of the low-order bytes of context slots on big-endian Alan Maguire
2026-11-25 16:58 ` [PATCH v2 bpf-next 2/5] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
2026-11-25 17:16   ` sashiko-bot
2026-11-25 16:58 ` [PATCH v2 bpf-next 3/5] bpf: " Alan Maguire
2026-11-25 16:58 ` [PATCH v2 bpf-next 4/5] selftests/bpf: Test " Alan Maguire
2026-11-25 16:58 ` [PATCH v2 bpf-next 5/5] Documentation/bpf: Describe CO-RE relocation of function arguments Alan Maguire
2026-11-25 17:37   ` bot+bpf-ci
2026-11-25 22:49 ` [PATCH v2 bpf-next 0/5] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Andrii Nakryiko
2026-11-26  4:02   ` Alexei Starovoitov

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=20261125165845.45806-1-almagui@kernel.org \
    --to=almagui@kernel.org \
    --cc=andrey.grodzovsky@crowdstrike.com \
    --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=mason@kernel.org \
    --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