Fix a few BPF selftests
Subsystems (4)
Song Liu
reviewercover Fix a few BPF selftests
Fix a few BPF selftests. Please see each patch for details of these fixes.
Song Liu (3):
selftests/bpf: Use read_perf_max_sample_freq() in perf_event_stackmap selftests/bpf: Fix leaked bpf_link in get_stackid_cannot_attach selftests/bpf: Keep the loop in bpf_testmod_loop_test
.../selftests/bpf/bpf_testmod/bpf_testmod.c | 6 +++++- .../prog_tests/get_stackid_cannot_attach.c | 1 + .../bpf/prog_tests/perf_event_stackmap.c | 3 ++- .../bpf/prog_tests/stacktrace_build_id_nmi.c | 15 -------------- tools/testing/selftests/bpf/testing_helpers.c | 20 +++++++++++++++++++ tools/testing/selftests/bpf/testing_helpers.h | 2 ++ 6 files changed, 30 insertions(+), 17 deletions(-)
--
2.34.1
Song Liu
reviewerPATCH 1/3 selftests/bpf: Use read_perf_max_sample_freq() in perf_event_stackmap
Currently, perf_event sample period in perf_event_stackmap is set too low that the test fails randomly. Fix this by using the max sample frequency, from read_perf_max_sample_freq().
Move read_perf_max_sample_freq() to testing_helpers.c. Replace the CHECK() with if-printf, as CHECK is not available in testing_helpers.c.
--- .../bpf/prog_tests/perf_event_stackmap.c | 3 ++- .../bpf/prog_tests/stacktrace_build_id_nmi.c | 15 -------------- tools/testing/selftests/bpf/testing_helpers.c | 20 +++++++++++++++++++ tools/testing/selftests/bpf/testing_helpers.h | 2 ++ 4 files changed, 24 insertions(+), 16 deletions(-)
tools/testing/selftests/bpf/prog_tests/perf_event_stackmap.c +2 -1
@@ -63,7 +63,8 @@ void test_perf_event_stackmap(void)PERF_SAMPLE_BRANCH_NO_FLAGS |PERF_SAMPLE_BRANCH_NO_CYCLES | PERF_SAMPLE_BRANCH_CALL_STACK, - .sample_period = 5000, + .freq = 1, + .sample_freq = read_perf_max_sample_freq(), .size = sizeof(struct perf_event_attr), };struct perf_event_stackmap *skel;
tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c +0 -15
@@ -2,21 +2,6 @@#include <test_progs.h> #include "test_stacktrace_build_id.skel.h" -static __u64 read_perf_max_sample_freq (void ) -{ -__u64 sample_freq =5000 ;/* fallback to 5000 on error */ -FILE *f; -__u32 duration =0 ; - - f =fopen ("/proc/sys/kernel/perf_event_max_sample_rate" ,"r" ); -if (f ==NULL ) -return sample_freq; -CHECK (fscanf (f,"%llu" , &sample_freq) !=1 ,"Get max sample rate" , -"return default value: 5000,err %d\n" , -errno); -fclose (f); -return sample_freq; -} -void test_stacktrace_build_id_nmi (void ) {int control_map_fd, stackid_hmap_fd, stackmap_fd;
tools/testing/selftests/bpf/testing_helpers.c +20 -0
@@ -229,3 +229,23 @@ int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,return bpf_prog_load (type,NULL , license, insns, insns_cnt, &opts); } + +__u64 read_perf_max_sample_freq (void ) +{ +__u64 sample_freq =5000 ;/* fallback to 5000 on error */ +FILE *f; + + f =fopen ("/proc/sys/kernel/perf_event_max_sample_rate" ,"r" ); +if (f ==NULL ) { +printf ("Failed to open /proc/sys/kernel/perf_event_max_sample_rate: err %d\n" +"return default value: 5000\n" , -errno); +return sample_freq; + } +if (fscanf (f,"%llu" , &sample_freq) !=1 ) { +printf ("Failed to parse /proc/sys/kernel/perf_event_max_sample_rate: err %d\n" +"return default value: 5000\n" , -errno); + } + +fclose (f); +return sample_freq; +}
tools/testing/selftests/bpf/testing_helpers.h +2 -0
@@ -20,3 +20,5 @@ struct test_filter_set;int parse_test_list (const char *s,struct test_filter_set *test_set,bool is_glob_pattern); + +__u64 read_perf_max_sample_freq (void );
Signature · 1 line
2.34.1
Song Liu
reviewerPATCH 2/3 selftests/bpf: Fix leaked bpf_link in get_stackid_cannot_attach
In reply to Song Liuskel->links.oncpu is leaked in one case. This causes test perf_branches fails when it runs after get_stackid_cannot_attach:
./test_progs -t get_stackid_cannot_attach,perf_branches
84 get_stackid_cannot_attach:OK
test_perf_branches_common:PASS:test_perf_branches_load 0 nsec test_perf_branches_common:PASS:attach_perf_event 0 nsec test_perf_branches_common:PASS:set_affinity 0 nsec check_good_sample:FAIL:output not valid no valid sample from prog
146/1 perf_branches/perf_branches_hw:FAIL 146/2 perf_branches/perf_branches_no_hw:OK 146 perf_branches:FAIL
All error logs:
test_perf_branches_common:PASS:test_perf_branches_load 0 nsec test_perf_branches_common:PASS:attach_perf_event 0 nsec test_perf_branches_common:PASS:set_affinity 0 nsec check_good_sample:FAIL:output not valid no valid sample from prog
146/1 perf_branches/perf_branches_hw:FAIL
146 perf_branches:FAIL
Summary: 1/1 PASSED, 0 SKIPPED, 1 FAILED
Fix this by adding the missing bpf_link__destroy().
--- .../testing/selftests/bpf/prog_tests/get_stackid_cannot_attach.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/prog_tests/get_stackid_cannot_attach.c +1 -0
@@ -65,6 +65,7 @@ void test_get_stackid_cannot_attach(void) skel->links.oncpu =bpf_program__attach_perf_event (skel->progs.oncpu, pmu_fd);ASSERT_OK_PTR (skel->links.oncpu,"attach_perf_event_callchain" ); +bpf_link__destroy (skel->links.oncpu);close (pmu_fd);/* add exclude_callchain_kernel, attach should fail */
Signature · 1 line
2.34.1
Song Liu
reviewerPATCH 3/3 selftests/bpf: Keep the loop in bpf_testmod_loop_test
In reply to Song LiuSome 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.
--- tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +5 -1
@@ -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.
Signature · 1 line
2.34.1
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 8 lines · On Wed, 12 Apr 2023 14:04:20 -0700 you wrote:
Fix a few BPF selftests. Please see each patch for details of these fixes.
Song Liu (3):
selftests/bpf: Useread_perf_max_sample_freq()inperf_event_stackmapselftests/bpf: Fix leakedbpf_linkinget_stackid_cannot_attachselftests/bpf: Keep the loop inbpf_testmod_loop_test[...]
Here is the summary with links:
- [bpf-next,1/3] selftests/bpf: Use read_perf_max_sample_freq() in perf_event_stackmap
https://git.kernel.org/bpf/bpf-next/c/de6d014a09bf - [bpf-next,2/3] selftests/bpf: Fix leaked bpf_link in get_stackid_cannot_attach https://git.kernel.org/bpf/bpf-next/c/c1e07a80cf23 - [bpf-next,3/3] selftests/bpf: Keep the loop in bpf_testmod_loop_test https://git.kernel.org/bpf/bpf-next/c/2995f9a8d427
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html