From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "David Faust" <david.faust@oracle.com>, <bpf@vger.kernel.org>
Cc: <ast@kernel.org>, <andrii@kernel.org>, <yonghong.song@linux.dev>,
<eddyz87@gmail.com>, <jose.marchesi@oracle.com>,
<cupertino.miranda@oracle.com>, <vineet.gupta@linux.dev>
Subject: Re: [PATCH bpf-next] selftests/bpf: remove string.h includes in bpf progs
Date: Tue, 21 Apr 2026 17:57:14 -0700 [thread overview]
Message-ID: <DHZ9X7I3N8ZI.36L0KXXPX39I4@gmail.com> (raw)
In-Reply-To: <20a84543-8e42-4d54-96da-29754ed1cd39@oracle.com>
On Tue Apr 21, 2026 at 5:15 PM PDT, David Faust wrote:
>
>
> On 4/21/26 14:07, Alexei Starovoitov wrote:
>> On Tue Apr 21, 2026 at 1:22 PM PDT, David Faust wrote:
>>> Sources for some BPF test programs currently include string.h, which
>>> is a glibc header and not a toolchain header. Since there is no BPF
>>> glibc, this means it is the native system glibc string.h which gets
>>> included when building the test programs.
>>>
>>> In all cases the include is only necessary for the prototypes of
>>> non-builtin versions of memset, memcpy, etc., and in every case both
>>> clang and gcc already replace these with the compiler built-in versions
>>> and expand them inline.
>>>
>>> In the case of gcc this replacement happens after initial debuginfo is
>>> generated, which includes the calls to the glibc routines. This means a
>>> BTF record for e.g. 'extern memset' is emitted, resulting in load
>>> failures like:
>>>
>>> libbpf: BTF loading error: -22
>>> ...
>>> [26] FUNC memset type_id=1 Invalid func linkage
>>
>> ...
>>
>>> - memcpy(&val, keys, sizeof(val));
>>> + __builtin_memcpy(&val, keys, sizeof(val));
>>
>> This is not an option. It works for llvm because it inlines it for small sizes.
>> GCC should do the same.
>
> GCC does the same inlining for small sizes.
> It is a bug in GCC that the FUNC record for the original is still emitted
> in this case, that shall be fixed.
>
>> For large sizes llvm will emit libcall to memset
>> that libbpf will error with "FUNC memset type_id=1 Invalid func linkage"
>> and that's a separate issue to deal with.
>
> It works now in both compilers (modulo above gcc bug) because at -O1 and above
> they recognize the memset/memcpy/etc. calls and attempt to replace them with
> builtin versions and expand inline.
> If the size is too big or non-constant, they will both fall back on libcalls.
>
> My point is that it is essentially working by chance, only because the compilers
> do this optimization. The progs as written are including a libc header, using
> routines from that header, but not linking to any actual implementation of
> those routines that they use.
No. It's not "by chance". It's working, because compilers have to do this optimization.
Just like
static void *(* const bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
hack.
It "works" because compilers perform a set of optimizations.
There is no luck here. Compilers have to transform the code in a certain way
that the verifier expects.
BPF doesn't work with -O0. Just like kernel won't boot if compiled with -O0.
One can argue that kernel is working at -O2 "by chance" as well.
> Changing to __builtin versions makes the current behavior explicit.
Nope. Not doing gcc's helper_call extension either.
next prev parent reply other threads:[~2026-04-22 0:57 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-21 20:22 [PATCH bpf-next] selftests/bpf: remove string.h includes in bpf progs David Faust
2026-04-21 21:07 ` Alexei Starovoitov
2026-04-22 0:15 ` David Faust
2026-04-22 0:57 ` Alexei Starovoitov [this message]
2026-04-21 21:08 ` Vineet Gupta
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=DHZ9X7I3N8ZI.36L0KXXPX39I4@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cupertino.miranda@oracle.com \
--cc=david.faust@oracle.com \
--cc=eddyz87@gmail.com \
--cc=jose.marchesi@oracle.com \
--cc=vineet.gupta@linux.dev \
--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