Skip to content

Commit 5730dac

Browse files
mykyta5anakryiko
authored andcommitted
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]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent dd948aa commit 5730dac

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

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

Lines changed: 11 additions & 4 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"))
@@ -112,6 +116,8 @@ static void task_work_run(const char *prog_name, const char *map_name)
112116
if (!ASSERT_OK_PTR(link, "attach_perf_event"))
113117
goto cleanup;
114118

119+
/* perf event fd ownership is passed to bpf_link */
120+
pe_fd = -1;
115121
close(pipefd[0]);
116122
write(pipefd[1], user_string, 1);
117123
close(pipefd[1]);
@@ -126,8 +132,9 @@ static void task_work_run(const char *prog_name, const char *map_name)
126132
cleanup:
127133
if (pe_fd >= 0)
128134
close(pe_fd);
135+
bpf_link__destroy(link);
129136
task_work__destroy(skel);
130-
if (pid) {
137+
if (pid > 0) {
131138
close(pipefd[0]);
132139
write(pipefd[1], user_string, 1);
133140
close(pipefd[1]);

0 commit comments

Comments
 (0)