Skip to content

Commit 8b559e4

Browse files
committed
selftests/bpf: task_work selftest cleanup fixes
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. Signed-off-by: Mykyta Yatsenko <[email protected]>
1 parent 34a5669 commit 8b559e4

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

tools/testing/selftests/bpf/prog_tests/test_task_work.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ static void task_work_run(const char *prog_name, const char *map_name)
5555
struct task_work *skel;
5656
struct bpf_program *prog;
5757
struct bpf_map *map;
58-
struct bpf_link *link;
59-
int err, pe_fd = 0, pid, status, pipefd[2];
58+
struct bpf_link *link = NULL;
59+
int err, pe_fd = -1, pid, status, pipefd[2];
6060
char user_string[] = "hello world";
6161

6262
if (!ASSERT_NEQ(pipe(pipefd), -1, "pipe"))
@@ -77,7 +77,11 @@ static void task_work_run(const char *prog_name, const char *map_name)
7777
(void)num;
7878
exit(0);
7979
}
80-
ASSERT_GT(pid, 0, "fork() failed");
80+
if (!ASSERT_GT(pid, 0, "fork() failed")) {
81+
close(pipefd[0]);
82+
close(pipefd[1]);
83+
return;
84+
}
8185

8286
skel = task_work__open();
8387
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)
109113
}
110114

111115
link = bpf_program__attach_perf_event(prog, pe_fd);
112-
if (!ASSERT_OK_PTR(link, "attach_perf_event"))
116+
if (!ASSERT_OK_PTR(link, "attach_perf_event")) {
117+
link = NULL;
113118
goto cleanup;
114-
119+
}
120+
/* perf event fd ownership is passed to bpf_link */
121+
pe_fd = -1;
115122
close(pipefd[0]);
116123
write(pipefd[1], user_string, 1);
117124
close(pipefd[1]);
@@ -126,8 +133,10 @@ static void task_work_run(const char *prog_name, const char *map_name)
126133
cleanup:
127134
if (pe_fd >= 0)
128135
close(pe_fd);
136+
if (link)
137+
bpf_link__destroy(link);
129138
task_work__destroy(skel);
130-
if (pid) {
139+
if (pid > 0) {
131140
close(pipefd[0]);
132141
write(pipefd[1], user_string, 1);
133142
close(pipefd[1]);

0 commit comments

Comments
 (0)