BPF List
 help / color / mirror / Atom feed
From: Alan Maguire <almagui@kernel.org>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
	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
Subject: Re: [PATCH v2 bpf-next 0/5] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering
Date: Thu, 26 Nov 2026 16:41:27 +0000	[thread overview]
Message-ID: <21d38d84-0520-4fd2-ad98-04253d0405f2@kernel.org> (raw)
In-Reply-To: <CAEf4BzbkrVyCSW1aRCo_s=2pxK7NRfUnznk21CY4-txqveRcnQ@mail.gmail.com>

On 11/25/26 10:49 PM, Andrii Nakryiko wrote:
> On Wed, Nov 25, 2026 at 8:58 AM Alan Maguire <almagui@kernel.org> wrote:
>>
>> 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));
>>
> 
> to make this work reliably and not get screwed over by compiler
> generating context access like this:
> 
> r2 = <offset> ; CO-RE relocated
> r1 += r2      ; verifier doesn't like this
> r3 = *(u64 *)(r1 + 0); /* ctx access */
> 
> we need to make sure that we can use
> __attribute__((preserve_static_offset)) together with
> __attribute__((preserve_access_index))
> 
> This was pointed out in [0] (which, coincidentally perhaps, also
> mentioned explicit bpf_func_args___xxx struct idea), we might not need
> to block on that support given user defines this struct and bears
> responsibility for this to work, but still, should be addressed
> soon-ish in Clang/GCC.
> 
>

Thanks for the pointer to the thread, I didn't see that message though
Alexei mentioned it (and the name). Ihor, I'll add a Suggested-by 
for you in the next round if that works?

I guess the challenge will be detecting cases where both
preserve_static_offset and preserve_access_index _do_ work together
(once they do)? If we had a __BPF_FEATURE as Alexei suggested I guess
we could do something like this:


#if defined(__BPF_FEATURE_PSO_CORE)
#define __bpf_func_args_attrs __attribute__((preserve_access_index, preserve_static_offset))
#else
#define __bpf_func_args_attrs __attribute__((preserve_access_index))
#endif

?

I couldn't find an LLVM issue for this problem specifically, but
looks like [1] describes things from the gcc side.

> And I'm actually wondering if the better name for this would be
> "bpf_ctx___<flavor>" or "bpf_ctx_args___<flavor>", because it's not
> always *function* arguments (tracepoint, BPF program, etc). Keeping it
> more generic as "context arguments" or just "context" seems more
> future-proof.
> 
> 
> Also (I only skimmed patches, haven't gone through them completely,
> will get back to this next week after holidays, sorry), I saw you
> mentioned the limitation that this context access can be only done
> from entry program code, right?  I think this can be lifted if we
> apply these relocations after subprogs are appended to their
> respective main programs. Not sure how ugly that will be, but you can
> try that and see how it works (please do it as a separate patch to
> distract less from the main logic change).
> 

Thanks for the suggestion, I'll see if I can get this working.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117068


      parent reply	other threads:[~2026-11-26 16:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 16:58 [PATCH v2 bpf-next 0/5] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
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
2026-11-26 16:41   ` Alan Maguire [this message]

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=21d38d84-0520-4fd2-ad98-04253d0405f2@kernel.org \
    --to=almagui@kernel.org \
    --cc=andrey.grodzovsky@crowdstrike.com \
    --cc=andrii.nakryiko@gmail.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