BPF List
 help / color / mirror / Atom feed
From: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
To: "Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	<bpf@vger.kernel.org>
Cc: "Tejun Heo" <tj@kernel.org>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Emil Tsalapatis" <emil@etsalapatis.com>, <kkd@meta.com>,
	<kernel-team@meta.com>
Subject: Re: [PATCH bpf-next v1 3/9] bpf, x86: JIT __arena kfunc argument rebasing
Date: Sat, 18 Jul 2026 09:16:25 +0200	[thread overview]
Message-ID: <DK1IGXGCR7R7.2ZVCIOYH3JAPL@gmail.com> (raw)
In-Reply-To: <bbf82d80eb0dc307d09a6c7cc33c23063a55a90f.camel@gmail.com>

On Sat Jul 18, 2026 at 3:33 AM CEST, Eduard Zingerman wrote:
> On Thu, 2026-07-16 at 00:00 +0200, Kumar Kartikeya Dwivedi wrote:
>> From: Tejun Heo <tj@kernel.org>
>>
>> Implement arena argument rebasing for kfunc calls on x86. R12 already
>> holds kern_vm_start whenever the prog has an arena, so each tagged
>> argument costs two instructions emitted right before the call:
>>
>>   movl %eN, %eN         /* truncate, clear the upper 32 bits */
>>   addq %r12, %rN
>>
>> A nullable argument tests the truncated value and jumps over the add:
>>
>>   movl  %eN, %eN
>>   testl %eN, %eN
>>   jz    1f
>>   addq  %r12, %rN
>> 1:
>>
>> addq carries a REX prefix for every argument register and is always
>> three bytes, so the jz displacement is constant. The sequence is native
>> code generated after constant blinding has run on the BPF instruction
>> stream, so blinding never sees the rebase and needs no special handling.
>>
>> bpf_jit_supports_arena_args() is not flipped yet; that happens when the
>> struct_ops trampoline side is in place as well.
>>
>> Signed-off-by: Tejun Heo <tj@kernel.org>
>> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
>> ---
>>  arch/x86/net/bpf_jit_comp.c | 49 +++++++++++++++++++++++++++++++++++++
>>  1 file changed, 49 insertions(+)
>
> Hi Kumar, Tejun,
>
> Sorry for the delayed response.
>
> I'd like to push back on the JIT based approach.
> As far as I understand, it saves a 64-immediate move per callsite,
> for jits that have dedicated arena registers (all but s390),
> but it puts implementation burden on each jit.
>
> There is an easy workaround regarding 64-immediate move:
> use R0 as in the original patch-set by default,
> add a jit_has_dedicated_arena_register() predicate +
> and encoding to address this register from __internal__
> bpf instruction, say BPF_REG_12.
>
> I'd prefer to keep the bulk of the implementation in one place instead
> of verifier.c + 6 jits.
>

I considered some of these options.

I think this is reasonable for kfuncs. We can retain move to R0 for JITs without
that predicate. But what do we do for struct_ops? Earlier prototype had to reload,
translate the arg, and store it back into ctx. IMO that's way uglier than just
translating while storing into the ctx during save_args().

At that point, the argument for avoiding JIT-specific code in favor of generic
sequences becomes a lot weaker.

Now we can argue for having some way to inject generic bytecode sequence in the
trampoline before BPF program is called, but that requires more conversation.
This case would be the only user. If we have more, it is probably worth a
consideration.

> ===
>
> Tangential to this, I think that __arena vs __arena_nullable is a footgun.
> Is it really so performance critical to avoid this null check?
>

Just imagine multiple arena arguments. Let's think beyond the immediate use case
for a moment (in scx). With the test+branch, that's way too many instructions
before each function call to translate each argument. In scx I think one of the
kfunc / struct_ops callback already did not need __nullable. In general it also
stays more consistent with us opting into __nullable for arguments, than having
it by default.

Yes, __arena means different things in different places, the meaning is now
context-dependent. In the program, it just indicates global function takes arena
pointer. NULL-ness or not distinction is erased, just like for PTR_TO_ARENA. For
kfuncs, the translation happens from program to kernel, for struct_ops, from
kernel to program. Better naming can probably avoid some of the overloading, but
anything else seems uglier to spell out.

> ===
>
> On more tangential, these flags would represent a third way to encode
> arena arguments we already have:
> - KF_ARENA_ARG{1,2} for kfuncs
> - ARG_TAG_ARENA for global functions.
>
> Any ideas on how to unify these?

We can gate this new one behind type tag support in GCC, and begin using those
instead. That still won't allow unification with KF_ARENA_* ones.

  reply	other threads:[~2026-07-18  7:16 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-15 22:00 [PATCH bpf-next v1 0/9] Add arena argument support to kfuncs and struct_ops Kumar Kartikeya Dwivedi
2026-07-15 22:00 ` [PATCH bpf-next v1 1/9] bpf: Support __arena and __arena_nullable kfunc argument suffixes Kumar Kartikeya Dwivedi
2026-07-15 23:05   ` bot+bpf-ci
2026-07-16 11:31     ` Kumar Kartikeya Dwivedi
2026-07-15 22:00 ` [PATCH bpf-next v1 2/9] bpf: Support __arena and __arena_nullable on struct_ops stub arguments Kumar Kartikeya Dwivedi
2026-07-15 22:22   ` sashiko-bot
2026-07-15 22:00 ` [PATCH bpf-next v1 3/9] bpf, x86: JIT __arena kfunc argument rebasing Kumar Kartikeya Dwivedi
2026-07-18  1:33   ` Eduard Zingerman
2026-07-18  7:16     ` Kumar Kartikeya Dwivedi [this message]
2026-07-15 22:00 ` [PATCH bpf-next v1 4/9] bpf, x86: Convert struct_ops arena arguments in the trampoline Kumar Kartikeya Dwivedi
2026-07-15 22:00 ` [PATCH bpf-next v1 5/9] selftests/bpf: Add kfunc __arena and __arena_nullable argument tests Kumar Kartikeya Dwivedi
2026-07-15 22:00 ` [PATCH bpf-next v1 6/9] selftests/bpf: Add JIT-sequence tests for __arena kfunc arguments Kumar Kartikeya Dwivedi
2026-07-15 22:00 ` [PATCH bpf-next v1 7/9] selftests/bpf: Add struct_ops __arena and __arena_nullable argument tests Kumar Kartikeya Dwivedi
2026-07-15 22:00 ` [PATCH bpf-next v1 8/9] bpf, x86: Fix stack-passed arguments for indirect trampolines Kumar Kartikeya Dwivedi
2026-07-15 22:14   ` sashiko-bot
2026-07-16 11:32     ` Kumar Kartikeya Dwivedi
2026-07-15 22:51   ` bot+bpf-ci
2026-07-15 22:00 ` [PATCH bpf-next v1 9/9] selftests/bpf: Test stack-passed struct_ops arena arguments Kumar Kartikeya Dwivedi

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=DK1IGXGCR7R7.2ZVCIOYH3JAPL@gmail.com \
    --to=memxor@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=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=tj@kernel.org \
    /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