BPF List
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	kernel-team@fb.com
Subject: Re: [PATCH bpf-next v4 12/12] docs/bpf: Document arena pointers in a by-value return
Date: Sun, 30 Aug 2026 19:42:48 -0700	[thread overview]
Message-ID: <86697482-9edf-4b78-8fcb-6cea1bdf7db6@linux.dev> (raw)
In-Reply-To: <DL1W44NTIQ1U.1BH6B0V44EFSA@gmail.com>



On 8/29/26 6:33 PM, Kumar Kartikeya Dwivedi wrote:
> On Sat Aug 29, 2026 at 8:16 AM CEST, Yonghong Song wrote:
>> Section 2.9 describes the by-value return contract as scalars only, which
>> no longer holds: a kfunc and a global subprogram may now return a struct
>> or union whose members are scalars or arena pointers. Update it.
>>
>> Note that an arena pointer member is handed back as a scalar like every
>> other member. That costs nothing: the program has to cast_kern() the value
>> before it can be used, and the verifier allows that cast on any scalar, so
>> the member gives the program no reach it did not already have.
>>
>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>> ---
>>   Documentation/bpf/kfuncs.rst | 39 ++++++++++++++++++++++--------------
>>   1 file changed, 24 insertions(+), 15 deletions(-)
>>
>> diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
>> index 89dea6b0b024..ebe37c1fe9d2 100644
>> --- a/Documentation/bpf/kfuncs.rst
>> +++ b/Documentation/bpf/kfuncs.rst
>> @@ -581,20 +581,29 @@ against the arena. Larger accesses must verify the range explicitly.
>>   A kfunc may return a scalar, a pointer, or a small struct or union by
>>   value. A scalar or pointer of up to 8 bytes is returned in R0, as usual.
>>
>> -A struct or union returned by value must be composed only of scalars
>> -(recursively), where a scalar is an integer or an enum; arrays of scalars are
>> -allowed as members. Its bytes are handed back to the program as the raw
>> -contents of R0 (and R2), so a pointer field would be laundered into a scalar
>> -and escape the verifier's pointer provenance and reference tracking. A struct
>> -or union with a pointer member is therefore rejected at load time, and so is
>> -one with a floating-point member, which the ABI may not return in R0:R2 at
>> +A struct or union returned by value must be composed only of scalars and arena
>> +pointers, where a scalar is an integer or an enum and an arena pointer is one
>> +carrying the ``btf_type_tag("arena")`` attribute. Those may be nested in
>> +structs and unions and in arrays of any number of dimensions, in any
>> +combination, as long as what the nesting bottoms out in is a scalar or an arena
>> +pointer. Its bytes are handed back to the program as the raw contents of R0
>> +(and R2), so a member of any other pointer type would be laundered into a
>> +scalar and escape the verifier's pointer provenance and reference tracking. A
>> +struct or union with such a member is therefore rejected at load time, and so
>> +is one with a floating-point member, which the ABI may not return in R0:R2 at
>>   all.
>>
>> +An arena pointer member is handed back as a scalar too, but nothing is lost by
>> +that. The program must ``cast_kern()`` the value before it can be used, and the
>> +verifier allows that cast on any scalar, so a laundered arena address gives the
>> +program no reach it did not already have. The result is confined to the
>> +program's arena in either case.
>> +
> I think it would make more sense to support translation for returned arena
> pointers as well, i.e. KF_ARENA_RET + the case you describe above. Like, anyone
> who would use this tag in a struct being returned to the user would probably we
> working with the kernel pointer into the arena.
>
> I think permitting them in the verifier is a good first step, but it isn't all
> that useful unless the translation is supported as well. Otherwise, the kfunc
> has to do it manually, which is a bit of a pain, esp. without access to the
> program's arena's base addresses, which isn't always easily possible across
> various program types.

Probably you mean something like below for input argument 'addr'.

+__bpf_kfunc struct prog_test_ret_arena bpf_kfunc_call_test_ret_arena(u64 addr)
+{
+	struct prog_test_ret_arena r = { .a = (void *)addr, .b = (void *)(addr + 4) };
+
+	return r;
+}

Yes, if kfunc itself wants to do something (esp. dereference of the addr), it cannot
do it.

>
> I can look into doing this in case you don't have cycles, but overall should be
> fairly simple to plumb support.

Sure. Please do it. Thanks!


>
>> [...]


  reply	other threads:[~2026-08-31  2:42 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-29  6:15 [PATCH bpf-next v4 00/12] bpf: Allow arena pointers in by-value returns Yonghong Song
2026-08-29  6:15 ` [PATCH bpf-next v4 01/12] bpf: Record each half of a paired return value in verifier diagnostics Yonghong Song
2026-08-29  6:15 ` [PATCH bpf-next v4 02/12] bpf: Drop the recursion depth argument of btf_type_is_scalar_struct() Yonghong Song
2026-08-29  6:15 ` [PATCH bpf-next v4 03/12] bpf: Add btf_type_is_arena_ptr() Yonghong Song
2026-08-29  6:15 ` [PATCH bpf-next v4 04/12] bpf: Let the by-value struct walk take the kinds of member it accepts Yonghong Song
2026-08-29  6:15 ` [PATCH bpf-next v4 05/12] bpf: Let a by-value struct nest arrays and structs freely Yonghong Song
2026-08-29  7:11   ` bot+bpf-ci
2026-08-29  6:15 ` [PATCH bpf-next v4 06/12] bpf: Report which member makes a kfunc return type unsupported Yonghong Song
2026-08-29  7:11   ` bot+bpf-ci
2026-08-29  6:15 ` [PATCH bpf-next v4 07/12] bpf: Allow a global function to return arena pointers by value Yonghong Song
2026-08-29  6:15 ` [PATCH bpf-next v4 08/12] bpf: Allow arena pointers in a by-value kfunc return Yonghong Song
2026-08-29  6:16 ` [PATCH bpf-next v4 09/12] selftests/bpf: Check the member named for an unsupported kfunc return type Yonghong Song
2026-08-29  6:16 ` [PATCH bpf-next v4 10/12] selftests/bpf: Test global functions returning arena pointers by value Yonghong Song
2026-08-29  6:16 ` [PATCH bpf-next v4 11/12] selftests/bpf: Test kfuncs " Yonghong Song
2026-08-29  6:16 ` [PATCH bpf-next v4 12/12] docs/bpf: Document arena pointers in a by-value return Yonghong Song
2026-08-30  1:33   ` Kumar Kartikeya Dwivedi
2026-08-31  2:42     ` Yonghong Song [this message]
2026-08-30  1:20 ` [PATCH bpf-next v4 00/12] bpf: Allow arena pointers in by-value returns patchwork-bot+netdevbpf

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=86697482-9edf-4b78-8fcb-6cea1bdf7db6@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@fb.com \
    --cc=memxor@gmail.com \
    /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