]> Git Repo - J-linux.git/blob - tools/testing/selftests/bpf/prog_tests/task_kfunc.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / tools / testing / selftests / bpf / prog_tests / task_kfunc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3
4 #define _GNU_SOURCE
5 #include <sys/wait.h>
6 #include <test_progs.h>
7 #include <unistd.h>
8
9 #include "task_kfunc_failure.skel.h"
10 #include "task_kfunc_success.skel.h"
11
12 static struct task_kfunc_success *open_load_task_kfunc_skel(void)
13 {
14         struct task_kfunc_success *skel;
15         int err;
16
17         skel = task_kfunc_success__open();
18         if (!ASSERT_OK_PTR(skel, "skel_open"))
19                 return NULL;
20
21         skel->bss->pid = getpid();
22
23         err = task_kfunc_success__load(skel);
24         if (!ASSERT_OK(err, "skel_load"))
25                 goto cleanup;
26
27         return skel;
28
29 cleanup:
30         task_kfunc_success__destroy(skel);
31         return NULL;
32 }
33
34 static void run_success_test(const char *prog_name)
35 {
36         struct task_kfunc_success *skel;
37         int status;
38         pid_t child_pid;
39         struct bpf_program *prog;
40         struct bpf_link *link = NULL;
41
42         skel = open_load_task_kfunc_skel();
43         if (!ASSERT_OK_PTR(skel, "open_load_skel"))
44                 return;
45
46         if (!ASSERT_OK(skel->bss->err, "pre_spawn_err"))
47                 goto cleanup;
48
49         prog = bpf_object__find_program_by_name(skel->obj, prog_name);
50         if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
51                 goto cleanup;
52
53         link = bpf_program__attach(prog);
54         if (!ASSERT_OK_PTR(link, "attached_link"))
55                 goto cleanup;
56
57         child_pid = fork();
58         if (!ASSERT_GT(child_pid, -1, "child_pid"))
59                 goto cleanup;
60         if (child_pid == 0)
61                 _exit(0);
62         waitpid(child_pid, &status, 0);
63
64         ASSERT_OK(skel->bss->err, "post_wait_err");
65
66 cleanup:
67         bpf_link__destroy(link);
68         task_kfunc_success__destroy(skel);
69 }
70
71 static int run_vpid_test(void *prog_name)
72 {
73         struct task_kfunc_success *skel;
74         struct bpf_program *prog;
75         int prog_fd, err = 0;
76
77         if (getpid() != 1)
78                 return 1;
79
80         skel = open_load_task_kfunc_skel();
81         if (!skel)
82                 return 2;
83
84         if (skel->bss->err) {
85                 err = 3;
86                 goto cleanup;
87         }
88
89         prog = bpf_object__find_program_by_name(skel->obj, prog_name);
90         if (!prog) {
91                 err = 4;
92                 goto cleanup;
93         }
94
95         prog_fd = bpf_program__fd(prog);
96         if (prog_fd < 0) {
97                 err = 5;
98                 goto cleanup;
99         }
100
101         if (bpf_prog_test_run_opts(prog_fd, NULL)) {
102                 err = 6;
103                 goto cleanup;
104         }
105
106         if (skel->bss->err)
107                 err = 7 + skel->bss->err;
108 cleanup:
109         task_kfunc_success__destroy(skel);
110         return err;
111 }
112
113 static void run_vpid_success_test(const char *prog_name)
114 {
115         const int stack_size = 1024 * 1024;
116         int child_pid, wstatus;
117         char *stack;
118
119         stack = (char *)malloc(stack_size);
120         if (!ASSERT_OK_PTR(stack, "clone_stack"))
121                 return;
122
123         child_pid = clone(run_vpid_test, stack + stack_size,
124                           CLONE_NEWPID | SIGCHLD, (void *)prog_name);
125         if (!ASSERT_GT(child_pid, -1, "child_pid"))
126                 goto cleanup;
127
128         if (!ASSERT_GT(waitpid(child_pid, &wstatus, 0), -1, "waitpid"))
129                 goto cleanup;
130
131         if (WEXITSTATUS(wstatus) > 7)
132                 ASSERT_OK(WEXITSTATUS(wstatus) - 7, "vpid_test_failure");
133         else
134                 ASSERT_OK(WEXITSTATUS(wstatus), "run_vpid_test_err");
135 cleanup:
136         free(stack);
137 }
138
139 static const char * const success_tests[] = {
140         "test_task_acquire_release_argument",
141         "test_task_acquire_release_current",
142         "test_task_acquire_leave_in_map",
143         "test_task_xchg_release",
144         "test_task_map_acquire_release",
145         "test_task_current_acquire_release",
146         "test_task_from_pid_arg",
147         "test_task_from_pid_current",
148         "test_task_from_pid_invalid",
149         "task_kfunc_acquire_trusted_walked",
150         "test_task_kfunc_flavor_relo",
151         "test_task_kfunc_flavor_relo_not_found",
152 };
153
154 static const char * const vpid_success_tests[] = {
155         "test_task_from_vpid_current",
156         "test_task_from_vpid_invalid",
157 };
158
159 void test_task_kfunc(void)
160 {
161         int i;
162
163         for (i = 0; i < ARRAY_SIZE(success_tests); i++) {
164                 if (!test__start_subtest(success_tests[i]))
165                         continue;
166
167                 run_success_test(success_tests[i]);
168         }
169
170         for (i = 0; i < ARRAY_SIZE(vpid_success_tests); i++) {
171                 if (!test__start_subtest(vpid_success_tests[i]))
172                         continue;
173
174                 run_vpid_success_test(vpid_success_tests[i]);
175         }
176
177         RUN_TESTS(task_kfunc_failure);
178 }
This page took 0.036211 seconds and 4 git commands to generate.