|
3 | 3 | #include <test_progs.h> |
4 | 4 | #include "struct_ops_assoc.skel.h" |
5 | 5 | #include "struct_ops_assoc_reuse.skel.h" |
| 6 | +#include "struct_ops_assoc_in_timer.skel.h" |
6 | 7 |
|
7 | 8 | static void test_st_ops_assoc(void) |
8 | 9 | { |
@@ -101,10 +102,93 @@ static void test_st_ops_assoc_reuse(void) |
101 | 102 | struct_ops_assoc_reuse__destroy(skel); |
102 | 103 | } |
103 | 104 |
|
| 105 | +static void test_st_ops_assoc_in_timer(void) |
| 106 | +{ |
| 107 | + struct struct_ops_assoc_in_timer *skel = NULL; |
| 108 | + int err; |
| 109 | + |
| 110 | + skel = struct_ops_assoc_in_timer__open_and_load(); |
| 111 | + if (!ASSERT_OK_PTR(skel, "struct_ops_assoc_reuse__open")) |
| 112 | + goto out; |
| 113 | + |
| 114 | + err = bpf_program__assoc_struct_ops(skel->progs.syscall_prog, |
| 115 | + skel->maps.st_ops_map, NULL); |
| 116 | + ASSERT_OK(err, "bpf_program__assoc_struct_ops"); |
| 117 | + |
| 118 | + err = struct_ops_assoc_in_timer__attach(skel); |
| 119 | + if (!ASSERT_OK(err, "struct_ops_assoc__attach")) |
| 120 | + goto out; |
| 121 | + |
| 122 | + /* |
| 123 | + * Run .test_1 by calling kfunc bpf_kfunc_multi_st_ops_test_1_prog_arg() and checks |
| 124 | + * the return value. .test_1 will also schedule timer_cb that runs .test_1 again |
| 125 | + * immediately. |
| 126 | + */ |
| 127 | + err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.syscall_prog), NULL); |
| 128 | + ASSERT_OK(err, "bpf_prog_test_run_opts"); |
| 129 | + |
| 130 | + /* Check the return of the kfunc after timer_cb runs */ |
| 131 | + while (!READ_ONCE(skel->bss->timer_cb_run)) |
| 132 | + sched_yield(); |
| 133 | + ASSERT_EQ(skel->bss->timer_test_1_ret, 1234, "skel->bss->timer_test_1_ret"); |
| 134 | + ASSERT_EQ(skel->bss->test_err, 0, "skel->bss->test_err_a"); |
| 135 | +out: |
| 136 | + struct_ops_assoc_in_timer__destroy(skel); |
| 137 | +} |
| 138 | + |
| 139 | +static void test_st_ops_assoc_in_timer_no_uref(void) |
| 140 | +{ |
| 141 | + struct struct_ops_assoc_in_timer *skel = NULL; |
| 142 | + struct bpf_link *link; |
| 143 | + int err; |
| 144 | + |
| 145 | + skel = struct_ops_assoc_in_timer__open_and_load(); |
| 146 | + if (!ASSERT_OK_PTR(skel, "struct_ops_assoc_reuse__open")) |
| 147 | + goto out; |
| 148 | + |
| 149 | + err = bpf_program__assoc_struct_ops(skel->progs.syscall_prog, |
| 150 | + skel->maps.st_ops_map, NULL); |
| 151 | + ASSERT_OK(err, "bpf_program__assoc_struct_ops"); |
| 152 | + |
| 153 | + link = bpf_map__attach_struct_ops(skel->maps.st_ops_map); |
| 154 | + if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) |
| 155 | + goto out; |
| 156 | + |
| 157 | + /* |
| 158 | + * Run .test_1 by calling kfunc bpf_kfunc_multi_st_ops_test_1_prog_arg() and checks |
| 159 | + * the return value. .test_1 will also schedule timer_cb that runs .test_1 again. |
| 160 | + * timer_cb will run 500ms after syscall_prog runs, when the user space no longer |
| 161 | + * holds a reference to st_ops_map. |
| 162 | + */ |
| 163 | + skel->bss->timer_ns = 500000000; |
| 164 | + err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.syscall_prog), NULL); |
| 165 | + ASSERT_OK(err, "bpf_prog_test_run_opts"); |
| 166 | + |
| 167 | + /* |
| 168 | + * Detach and close struct_ops map. timer_cb holding a reference to the map should |
| 169 | + * prevent it from being freed. |
| 170 | + */ |
| 171 | + bpf_link__destroy(link); |
| 172 | + close(bpf_program__fd(skel->progs.syscall_prog)); |
| 173 | + close(bpf_map__fd(skel->maps.st_ops_map)); |
| 174 | + |
| 175 | + /* Check the return of the kfunc after timer_cb runs */ |
| 176 | + while (!READ_ONCE(skel->bss->timer_cb_run)) |
| 177 | + sched_yield(); |
| 178 | + ASSERT_EQ(skel->bss->timer_test_1_ret, 1234, "skel->bss->timer_test_1_ret"); |
| 179 | + ASSERT_EQ(skel->bss->test_err, 0, "skel->bss->test_err_a"); |
| 180 | +out: |
| 181 | + struct_ops_assoc_in_timer__destroy(skel); |
| 182 | +} |
| 183 | + |
104 | 184 | void test_struct_ops_assoc(void) |
105 | 185 | { |
106 | 186 | if (test__start_subtest("st_ops_assoc")) |
107 | 187 | test_st_ops_assoc(); |
108 | 188 | if (test__start_subtest("st_ops_assoc_reuse")) |
109 | 189 | test_st_ops_assoc_reuse(); |
| 190 | + if (test__start_subtest("st_ops_assoc_in_timer")) |
| 191 | + test_st_ops_assoc_in_timer(); |
| 192 | + if (test__start_subtest("st_ops_assoc_in_timer_no_uref")) |
| 193 | + test_st_ops_assoc_in_timer_no_uref(); |
110 | 194 | } |
0 commit comments