• Song Liu's avatar
    selftests/bpf: Keep the loop in bpf_testmod_loop_test · 2995f9a8
    Song Liu authored
    Some compilers (for example clang-15) optimize bpf_testmod_loop_test and
    remove the loop:
    
    gcc version
    (gdb) disassemble bpf_testmod_loop_test
    Dump of assembler code for function bpf_testmod_loop_test:
       0x0000000000000570 <+0>:     callq  0x575 <bpf_testmod_loop_test+5>
       0x0000000000000575 <+5>:     xor    %eax,%eax
       0x0000000000000577 <+7>:     test   %edi,%edi
       0x0000000000000579 <+9>:     jle    0x587 <bpf_testmod_loop_test+23>
       0x000000000000057b <+11>:    xor    %edx,%edx
       0x000000000000057d <+13>:    add    %edx,%eax
       0x000000000000057f <+15>:    add    $0x1,%edx
       0x0000000000000582 <+18>:    cmp    %edx,%edi
       0x0000000000000584 <+20>:    jne    0x57d <bpf_testmod_loop_test+13>
       0x0000000000000586 <+22>:    retq
       0x0000000000000587 <+23>:    retq
    
    clang-15 version
    (gdb) disassemble bpf_testmod_loop_test
    Dump of assembler code for function bpf_testmod_loop_test:
       0x0000000000000450 <+0>:     nopl   0x0(%rax,%rax,1)
       0x0000000000000455 <+5>:     test   %edi,%edi
       0x0000000000000457 <+7>:     jle    0x46b <bpf_testmod_loop_test+27>
       0x0000000000000459 <+9>:     lea    -0x1(%rdi),%eax
       0x000000000000045c <+12>:    lea    -0x2(%rdi),%ecx
       0x000000000000045f <+15>:    imul   %rax,%rcx
       0x0000000000000463 <+19>:    shr    %rcx
       0x0000000000000466 <+22>:    lea    -0x1(%rdi,%rcx,1),%eax
       0x000000000000046a <+26>:    retq
       0x000000000000046b <+27>:    xor    %eax,%eax
       0x000000000000046d <+29>:    retq
    
    Note: The jne instruction is removed in clang-15 version.
    
    Force the compile to keep the loop by making sum volatile.
    Signed-off-by: default avatarSong Liu <song@kernel.org>
    Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20230412210423.900851-4-song@kernel.org
    2995f9a8
bpf_testmod.c 7.67 KB