BPF List
 help / color / mirror / Atom feed
From: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>
To: <sashiko-reviews@lists.linux.dev>
Cc: <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v2 1/9] bpf: Support __arena and __arena_nullable kfunc argument suffixes
Date: Sun, 26 Jul 2026 04:01:59 +0200	[thread overview]
Message-ID: <DK84SJFF77GL.1VGZ5Q21VUPXC@gmail.com> (raw)
In-Reply-To: <20260726014755.2DA7D1F000E9@smtp.kernel.org>

On Sun Jul 26, 2026 at 3:47 AM CEST, sashiko-bot wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [High] The `__arena_nullable` kfunc argument design uses a 32-bit truncated value of 0 as a sentinel for NULL, which collides with valid memory allocations from `bpf_arena_alloc_pages()`.
> --
>
> --- Patch [1]: [PATCH bpf-next v2 1/9] bpf: Support __arena and __arena_nullable kfunc argument suffixes ---
> commit fb759ca6f84030ccde9ee83f1e83634342bdae0d
> Author: Tejun Heo <tj@kernel.org>
>
> bpf: Support __arena and __arena_nullable kfunc argument suffixes
>
> This patch adds support for passing BPF arena pointers to kfuncs by introducing
> __arena and __arena_nullable argument suffixes. The verifier processes these
> suffixes and allows JITs to handle the translation (rebasing) at the call site.
> __arena rebases unconditionally, while __arena_nullable preserves NULL by skipping
> the rebase when the arena offset is zero.
>
>> diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
>> index c801a330aece0..9809680bee43e 100644
>> --- a/Documentation/bpf/kfuncs.rst
>> +++ b/Documentation/bpf/kfuncs.rst
> [ ... ]
>> @@ -250,6 +250,33 @@ Or::
>>                  ...
>>          }
>>
>> +2.3.7 __arena and __arena_nullable Annotations
>> +----------------------------------------------
>> +
>> +Both annotations indicate that the pointer argument points into the
>> +calling program's arena. The JIT rebases the value at the call site so
>> +the kfunc receives a directly dereferenceable kernel address, subject to
>> +the access rules described in :ref:`BPF_kfunc_arena_access` (at most
>> +``GUARD_SZ / 2``, 32 KiB, past the pointer in a single unchecked access).
>> +
>> +With ``__arena`` the rebase is unconditional and the argument is never
>> +NULL: a value whose lower 32 bits are zero arrives as the arena base
>> +address (arena offset 0). The kfunc must not check the argument for NULL.
>> +With ``__arena_nullable`` such a value arrives as NULL instead and the
>> +kfunc must check before dereferencing.
>
> [Severity: High]
> Does this __arena_nullable design cause a collision with valid allocations?
>
> If an arena is mapped at a 4GB-aligned boundary, the user_vm_start address will
> have its lower 32 bits as 0. When bpf_arena_alloc_pages() returns the first
> free page at offset 0, the resulting user pointer also has its lower 32 bits
> equal to 0.
>
> If a BPF program passes this valid pointer to an __arena_nullable kfunc, won't
> the JIT see the 0 value in the lower 32 bits and incorrectly pass NULL to the
> kfunc instead of the valid arena base address?

Yes, but it is intentional. Referencing the first page is equivalent to NULL, or
start of the arena "address space".

>
>> +
>> +An example is given below::
> [ ... ]
>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
>> index 641c3c62c1ecf..f3e6190f639e4 100644
>> --- a/kernel/bpf/verifier.c
>> +++ b/kernel/bpf/verifier.c
> [ ... ]
>> @@ -12056,6 +12066,35 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me
>>
>>  		t = btf_type_skip_modifiers(btf, args[i].type, NULL);
>>
>> +		if (is_kfunc_arg_arena(btf, &args[i]) || is_kfunc_arg_arena_nullable(btf, &args[i])) {
>
> [Severity: High]
> Should check_kfunc_args() or the arena allocator ensure that offset 0 is
> reserved?
>
> If offset 0 is not reserved, the verifier will allow programs to pass a valid
> offset 0 allocation to __arena_nullable arguments. The JIT will then silently
> translate this to NULL, causing unexpected behavior in the kfunc since it will
> assume the optional argument was not provided.

It will always be left unmapped in practice.

>
>> +			if (verifier_bug_if(!btf_type_is_ptr(t), env,
>> +					    "kfunc %s arg#%d has __arena tag on non-pointer",
>> +					    func_name, i))
>> +				return -EFAULT;


  reply	other threads:[~2026-07-26  2:02 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-26  1:30 [PATCH bpf-next v2 0/9] Add arena argument support to kfuncs and struct_ops Kumar Kartikeya Dwivedi
2026-07-26  1:30 ` [PATCH bpf-next v2 1/9] bpf: Support __arena and __arena_nullable kfunc argument suffixes Kumar Kartikeya Dwivedi
2026-07-26  1:47   ` sashiko-bot
2026-07-26  2:01     ` Kumar Kartikeya Dwivedi [this message]
2026-07-26  1:30 ` [PATCH bpf-next v2 2/9] bpf: Support __arena and __arena_nullable on struct_ops arguments Kumar Kartikeya Dwivedi
2026-07-26  1:30 ` [PATCH bpf-next v2 3/9] bpf, x86: JIT __arena kfunc argument rebasing Kumar Kartikeya Dwivedi
2026-07-26  2:22   ` bot+bpf-ci
2026-07-26  2:25     ` Kumar Kartikeya Dwivedi
2026-07-26  1:30 ` [PATCH bpf-next v2 4/9] bpf, x86: Convert struct_ops arena arguments in the trampoline Kumar Kartikeya Dwivedi
2026-07-26  1:30 ` [PATCH bpf-next v2 5/9] selftests/bpf: Add kfunc __arena and __arena_nullable argument tests Kumar Kartikeya Dwivedi
2026-07-26  1:41   ` sashiko-bot
2026-07-26  2:02     ` Kumar Kartikeya Dwivedi
2026-07-26  1:30 ` [PATCH bpf-next v2 6/9] selftests/bpf: Add JIT-sequence tests for __arena kfunc arguments Kumar Kartikeya Dwivedi
2026-07-26  1:44   ` sashiko-bot
2026-07-26  1:31 ` [PATCH bpf-next v2 7/9] selftests/bpf: Add struct_ops __arena and __arena_nullable argument tests Kumar Kartikeya Dwivedi
2026-07-26  1:31 ` [PATCH bpf-next v2 8/9] bpf, x86: Fix stack-passed arguments for indirect trampolines Kumar Kartikeya Dwivedi
2026-07-26  1:51   ` sashiko-bot
2026-07-26  2:05     ` Kumar Kartikeya Dwivedi
2026-07-26  1:31 ` [PATCH bpf-next v2 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=DK84SJFF77GL.1VGZ5Q21VUPXC@gmail.com \
    --to=memxor@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.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