llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

bpf __builtin_memcpy from large aligned global spills unnecessarily

Open dramforever opened this issue 1 year ago • 5 comments

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
        [...]

dramforever avatar Oct 23 '25 10:10 dramforever

Perhaps you need to increase bpf stack size per instruction? https://godbolt.org/z/sca99YWxE

realqhc avatar Oct 24 '25 08:10 realqhc

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.

dramforever avatar Oct 24 '25 08:10 dramforever

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.

yonghong-song avatar Jan 05 '26 21:01 yonghong-song

I created a new issue https://github.com/llvm/llvm-project/issues/175030 to debug this problem. Thanks!

yonghong-song avatar Jan 08 '26 17:01 yonghong-song

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.

yonghong-song avatar Jan 17 '26 04:01 yonghong-song