selftests/bpf: fix flaky bpf_cookie selftest
Subsystems (3)
PATCH v2 1/2 selftests/bpf: fix flaky bpf_cookie selftest
From: Mykyta Yatsenko <[email protected]>
Problem:bpf_cookie selftest fails if it runs after task_work selftest:perf_event_open fails with errno=EINVAL.
EINVAL indicates incorrect/invalid input argument, which in case of bpf_cookie can only point to sample_freq attribute.
Possible root cause:
When running task_work test, we can see that perf subsystem lowers kernel.perf_event_max_sample_rate which probably is the side-effect of the test that make bpf_cookie fail.
Solution:
Set perf_event_open sampling rate attribute for bpf_cookie the same as task_work - this is the most reliable solution for this, changing task_work sampling rate resulted in task_work test becoming flaky.
--- tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1 -2
@@ -450,8 +450,7 @@ static void pe_subtest(struct test_bpf_cookie *skel) attr.size =sizeof (attr); attr.type =PERF_TYPE_SOFTWARE ; attr.config =PERF_COUNT_SW_CPU_CLOCK ; - attr.freq =1 ; - attr.sample_freq =10000 ; + attr.sample_period =100000 ; pfd =syscall (__NR_perf_event_open, &attr,-1 ,0 ,-1 ,PERF_FLAG_FD_CLOEXEC );if (!ASSERT_GE (pfd,0 ,"perf_fd" )) goto cleanup;
Signature · 1 line
2.51.0
PATCH v2 2/2 selftests/bpf: task_work selftest cleanup fixes
From: Mykyta Yatsenko <[email protected]>
task_work selftest does not properly handle cleanup during failures:
* destroy bpf_link
* perf event fd is passed to bpf_link, no need to close it if link was created successfully
* goto cleanup if fork() failed, close pipe.
--- .../selftests/bpf/prog_tests/test_task_work.c | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_task_work.c +15 -6
@@ -55,8 +55,8 @@ static void task_work_run(const char *prog_name, const char *map_name)struct task_work *skel;struct bpf_program *prog;struct bpf_map *map; -struct bpf_link *link; -int err, pe_fd =0 , pid, status, pipefd[2 ]; +struct bpf_link *link =NULL ; +int err, pe_fd =-1 , pid, status, pipefd[2 ];char user_string[] ="hello world" ;if (!ASSERT_NEQ (pipe (pipefd),-1 ,"pipe" ))
@@ -77,7 +77,11 @@ static void task_work_run(const char *prog_name, const char *map_name) (void )num;exit (0 ); } -ASSERT_GT (pid,0 ,"fork() failed" ); +if (!ASSERT_GT (pid,0 ,"fork() failed" )) { +close (pipefd[0 ]); +close (pipefd[1 ]); +return ; + } skel =task_work__open ();if (!ASSERT_OK_PTR (skel,"task_work__open" ))
@@ -109,9 +113,12 @@ static void task_work_run(const char *prog_name, const char *map_name) } link =bpf_program__attach_perf_event (prog, pe_fd); -if (!ASSERT_OK_PTR (link,"attach_perf_event" )) +if (!ASSERT_OK_PTR (link,"attach_perf_event" )) { + link =NULL ; goto cleanup; - + } +/* perf event fd ownership is passed to bpf_link */ + pe_fd =-1 ;close (pipefd[0 ]);write (pipefd[1 ], user_string,1 );close (pipefd[1 ]);
@@ -126,8 +133,10 @@ static void task_work_run(const char *prog_name, const char *map_name) cleanup:if (pe_fd >=0 )close (pe_fd); +if (link) +bpf_link__destroy (link);task_work__destroy (skel); -if (pid) { +if (pid >0 ) {close (pipefd[0 ]);write (pipefd[1 ], user_string,1 );close (pipefd[1 ]);
Signature · 1 line
2.51.0
Andrii Nakryiko
AppliedRe: [PATCH bpf-next v2 2/2] selftests/bpf: task_work selftest cleanup fixes
Quoted 43 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
task_workselftest does not properly handle cleanup during failures:
* destroybpf_link
* perf event fd is passed tobpf_link, no need to close it if link was created successfully
* goto cleanup iffork()failed, close pipe.Signed-off-by Mykyta Yatsenko <[email protected]>--- .../selftests/bpf/prog_tests/test_task_work.c | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)tools/testing/selftests/bpf/prog_tests/test_task_work.c +7
-3@@ -55,8 +55,8 @@ static void task_work_run(const char *prog_name, const char *map_name)struct task_work *skel;struct bpf_program *prog;struct bpf_map *map; -struct bpf_link *link; -int err, pe_fd =0 , pid, status, pipefd[2 ]; +struct bpf_link *link =NULL ; +int err, pe_fd =-1 , pid, status, pipefd[2 ];char user_string[] ="hello world" ;if (!ASSERT_NEQ (pipe (pipefd),-1 ,"pipe" ))@@ -77,7 +77,11 @@ static void task_work_run(const char *prog_name, const char *map_name) (void )num;exit (0 ); } -ASSERT_GT (pid,0 ,"fork() failed" ); +if (!ASSERT_GT (pid,0 ,"fork() failed" )) { +close (pipefd[0 ]); +close (pipefd[1 ]); +return ; + } skel =task_work__open ();if (!ASSERT_OK_PTR (skel,"task_work__open" ))
tools/testing/selftests/bpf/prog_tests/test_task_work.c static void task_work_run(const char *prog_name, const char *map_name) · line 117
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_task_work.c +2
-1@@ -109,9 +113,12 @@ static void task_work_run(const char *prog_name, const char *map_name) } link =bpf_program__attach_perf_event (prog, pe_fd); -if (!ASSERT_OK_PTR (link,"attach_perf_event" )) +if (!ASSERT_OK_PTR (link,"attach_perf_event" )) { + link =NULL ;
no need, removed, link will be NULL on error
tools/testing/selftests/bpf/prog_tests/test_task_work.c static void task_work_run(const char *prog_name, const char *map_name) · line 136
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/test_task_work.c +4
-1goto cleanup; - + } +/* perf event fd ownership is passed to bpf_link */ + pe_fd =-1 ;close (pipefd[0 ]);write (pipefd[1 ], user_string,1 );close (pipefd[1 ]);@@ -126,8 +133,10 @@ static void task_work_run(const char *prog_name, const char *map_name) cleanup:if (pe_fd >=0 )close (pe_fd); +if (link)
no need to check, bpf_link__destroy() handles NULLs just fine, removed
applied to bpf-next, thanks
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_task_work.c +2
-2+bpf_link__destroy (link);task_work__destroy (skel); -if (pid) { +if (pid >0 ) {close (pipefd[0 ]);write (pipefd[1 ], user_string,1 );close (pipefd[1 ]); --2.51.0
Quoted 14 lines · Mykyta Yatsenko
From: Mykyta Yatsenko <[email protected]>
Problem:
bpf_cookieselftest fails if it runs aftertask_workselftest:perf_event_openfails with errno=EINVAL.
EINVAL indicates incorrect/invalid input argument, which in case ofbpf_cookiecan only point tosample_freqattribute.Possible root cause:
When runningtask_worktest, we can see that perf subsystem lowers kernel.perf_event_max_sample_rate which probably is the side-effect of the test that makebpf_cookiefail.
Quoted 5 lines
Solution:
Setperf_event_opensampling rate attribute forbpf_cookiethe same astask_work- this is the most reliable solution for this, changingtask_worksampling rate resulted intask_worktest becoming flaky.
All of the above is not very convincing, tbh. (and in parts just makes no sense at all)
A better fix would be to change all those perf_event triggering dependent tests from just doing some fixed amount of work (like 1 million loop iterations) to time-based waiting for BPF program to be triggered. And if it's not triggered within a reasonable amount of time -- only then erroring out.
I dropped this patch for now, and applied the second one only.
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void pe_subtest(struct test_bpf_cookie *skel) · line 452
Quoted 13 lines
Signed-off-by Mykyta Yatsenko <[email protected]>--- tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +0
-0@@ -450,8 +450,7 @@ static void pe_subtest(struct test_bpf_cookie *skel) attr.size =sizeof (attr); attr.type =PERF_TYPE_SOFTWARE ; attr.config =PERF_COUNT_SW_CPU_CLOCK ;
test_task_work test is using HARDWARE/HW_CPU_CYCLES, what does these two independent perf events have to do with each other? This "fix" makes no sense to me, it's just hoping for the better.
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-3- attr.freq =1 ; - attr.sample_freq =10000 ; + attr.sample_period =100000 ; pfd =syscall (__NR_perf_event_open, &attr,-1 ,0 ,-1 ,PERF_FLAG_FD_CLOEXEC );if (!ASSERT_GE (pfd,0 ,"perf_fd" )) goto cleanup; --2.51.0