summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorSong Liu <song@kernel.org>2023-04-12 14:04:23 -0700
committerDaniel Borkmann <daniel@iogearbox.net>2023-04-13 14:32:05 +0200
commit2995f9a8d427b9ff6f3cf4e85c0f9d4456ef324d (patch)
tree854d6a32ef40291b939816a376136f775aaba94c /tools
parentc1e07a80cf23d3a6e96172bc9a73bfa912a9fcbc (diff)
downloadlinux-2995f9a8d427b9ff6f3cf4e85c0f9d4456ef324d.tar.gz
linux-2995f9a8d427b9ff6f3cf4e85c0f9d4456ef324d.tar.bz2
linux-2995f9a8d427b9ff6f3cf4e85c0f9d4456ef324d.zip
selftests/bpf: Keep the loop in bpf_testmod_loop_test
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: Song Liu <song@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20230412210423.900851-4-song@kernel.org
Diffstat (limited to 'tools')
-rw-r--r--tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
index 7999476b9446..c5ad39bbe9af 100644
--- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
@@ -130,7 +130,11 @@ bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
noinline int bpf_testmod_loop_test(int n)
{
- int i, sum = 0;
+ /* Make sum volatile, so smart compilers, such as clang, will not
+ * optimize the code by removing the loop.
+ */
+ volatile int sum = 0;
+ int i;
/* the primary goal of this test is to test LBR. Create a lot of
* branches in the function, so we can catch it easily.