summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMykyta Yatsenko <yatsenko@meta.com>2025-09-24 15:29:54 +0100
committerAndrii Nakryiko <andrii@kernel.org>2025-09-25 11:00:01 -0700
commit5730dacb3f172858ca47b8b1aeab083b5713f24b (patch)
tree088d7cb3167d4bfa995f9bfa8a343cde1b34d187
parentdd948aa63ee48e3032804bd10c87a0f4edaa3515 (diff)
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 <yatsenko@meta.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250924142954.129519-2-mykyta.yatsenko5@gmail.com
-rw-r--r--tools/testing/selftests/bpf/prog_tests/test_task_work.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/tools/testing/selftests/bpf/prog_tests/test_task_work.c b/tools/testing/selftests/bpf/prog_tests/test_task_work.c
index 666585270fbf..774b31a5f6ca 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_task_work.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_task_work.c
@@ -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"))
@@ -112,6 +116,8 @@ static void task_work_run(const char *prog_name, const char *map_name)
if (!ASSERT_OK_PTR(link, "attach_perf_event"))
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 +132,9 @@ static void task_work_run(const char *prog_name, const char *map_name)
cleanup:
if (pe_fd >= 0)
close(pe_fd);
+ 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]);