bpf __builtin_memcpy from large aligned global spills unnecessarily
int meow[512 / sizeof(int)];
void cpy(char *dest)
{
__builtin_memcpy(dest, meow, 512);
}
https://godbolt.org/z/9czYacMfT
Compiles with LLVM 19, Fails with this error on >= 20:
<source>:3:6: error: Looks like the BPF stack limit is exceeded. Please move large on stack variables into BPF per-cpu array map. For non-kernel uses, the stack can be increased using -mllvm -bpf-stack-size.
3 | void cpy(char *dest)
| ^
<source>:3:6: note: could not determine the original source location for example.c:0:0
<source>:3:6: note: could not determine the original source location for example.c:0:0
Reducing the memcpy size to 40 or similar reveals that the code now spills to stack, for seemingly no reason:
cpy:
r3 = meow ll
[...]
w2 = *(u32 *)(r3 + 8)
*(u64 *)(r10 - 24) = r2
*(u8 *)(r1 + 8) = w2
w2 = *(u32 *)(r3 + 4)
*(u64 *)(r10 - 16) = r2
*(u8 *)(r1 + 4) = w2
w3 = *(u32 *)(r3 + 0)
*(u64 *)(r10 - 8) = r3
*(u8 *)(r1 + 0) = w3
[...]
Perhaps you need to increase bpf stack size per instruction? https://godbolt.org/z/sca99YWxE
Perhaps you need to increase bpf stack size per instruction? https://godbolt.org/z/sca99YWxE
@realqhc I cannot do that. My BPF program will be loaded into the kernel and thus cannot override the stack size restriction.
In any case it still doesn't make sense to require a seemingly unbounded amount of stack space for __builtin_memcpy.
I've currently worked around this in hid-bpf-uclogic by making make the source pointer volatile to hide it from optimization. Not ideal, but it fixes the problem for now: https://github.com/dramforever/hid-bpf-uclogic/commit/d9e943850e859404073281f915a7ae8551fb5aea
I can also change the descriptor so that it's a separate "global"/map, but that's annoying from the perspective of the interface between userspace and the BPF program.
I can reproduce the issue.
$ cat run.sh
clang --version
echo "==========="
clang --target=bpf -O2 -c t.c -mcpu=v1
echo "==========="
clang --target=bpf -O2 -c t.c -mcpu=v3
$ ./run.sh
clang version 22.0.0git (https://github.com/llvm/llvm-project.git 990883a6908202f5bbd8ada9d1d106d3db271622)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/yhs/work/llvm-project/llvm/build.22/Release/bin
===========
===========
t.c:5:6: error: Looks like the BPF stack limit is exceeded. Please move large on stack variables into BPF per-cpu
array map. For non-kernel uses, the stack can be increased using -mllvm -bpf-stack-size.
5 | void cpy(char *dest)
| ^
t.c:5:6: error: Looks like the BPF stack limit is exceeded. Please move large on stack variables into BPF per-cpu
array map. For non-kernel uses, the stack can be increased using -mllvm -bpf-stack-size.
t.c:5:6: error: Looks like the BPF stack limit is exceeded. Please move large on stack variables into BPF per-cpu
array map. For non-kernel uses, the stack can be increased using -mllvm -bpf-stack-size.
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
The reason is due to the default cpu version from v1 to v3. https://github.com/llvm/llvm-project/pull/107008
I will do some further investigation on this.
I created a new issue https://github.com/llvm/llvm-project/issues/175030 to debug this problem. Thanks!
I have not found a solution yet. But you can add '-mllvm -bpf-expand-memcpy-in-order' to your compilation flag and it should resolve the issue.