Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: BPF test_prog selftests for bpf_loop inlining

Two new test BPF programs for test_prog selftests checking bpf_loop
behavior. Both are corner cases for bpf_loop inlinig transformation:
- check that bpf_loop behaves correctly when callback function is not
a compile time constant
- check that local function variables are not affected by allocating
additional stack storage for registers spilled by loop inlining

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/r/20220620235344.569325-6-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Eduard Zingerman and committed by
Alexei Starovoitov
0e1bf9ed f8acfdd0

+176
+62
tools/testing/selftests/bpf/prog_tests/bpf_loop.c
··· 120 120 bpf_link__destroy(link); 121 121 } 122 122 123 + static void check_non_constant_callback(struct bpf_loop *skel) 124 + { 125 + struct bpf_link *link = 126 + bpf_program__attach(skel->progs.prog_non_constant_callback); 127 + 128 + if (!ASSERT_OK_PTR(link, "link")) 129 + return; 130 + 131 + skel->bss->callback_selector = 0x0F; 132 + usleep(1); 133 + ASSERT_EQ(skel->bss->g_output, 0x0F, "g_output #1"); 134 + 135 + skel->bss->callback_selector = 0xF0; 136 + usleep(1); 137 + ASSERT_EQ(skel->bss->g_output, 0xF0, "g_output #2"); 138 + 139 + bpf_link__destroy(link); 140 + } 141 + 142 + static void check_stack(struct bpf_loop *skel) 143 + { 144 + struct bpf_link *link = bpf_program__attach(skel->progs.stack_check); 145 + const int max_key = 12; 146 + int key; 147 + int map_fd; 148 + 149 + if (!ASSERT_OK_PTR(link, "link")) 150 + return; 151 + 152 + map_fd = bpf_map__fd(skel->maps.map1); 153 + 154 + if (!ASSERT_GE(map_fd, 0, "bpf_map__fd")) 155 + goto out; 156 + 157 + for (key = 1; key <= max_key; ++key) { 158 + int val = key; 159 + int err = bpf_map_update_elem(map_fd, &key, &val, BPF_NOEXIST); 160 + 161 + if (!ASSERT_OK(err, "bpf_map_update_elem")) 162 + goto out; 163 + } 164 + 165 + usleep(1); 166 + 167 + for (key = 1; key <= max_key; ++key) { 168 + int val; 169 + int err = bpf_map_lookup_elem(map_fd, &key, &val); 170 + 171 + if (!ASSERT_OK(err, "bpf_map_lookup_elem")) 172 + goto out; 173 + if (!ASSERT_EQ(val, key + 1, "bad value in the map")) 174 + goto out; 175 + } 176 + 177 + out: 178 + bpf_link__destroy(link); 179 + } 180 + 123 181 void test_bpf_loop(void) 124 182 { 125 183 struct bpf_loop *skel; ··· 198 140 check_invalid_flags(skel); 199 141 if (test__start_subtest("check_nested_calls")) 200 142 check_nested_calls(skel); 143 + if (test__start_subtest("check_non_constant_callback")) 144 + check_non_constant_callback(skel); 145 + if (test__start_subtest("check_stack")) 146 + check_stack(skel); 201 147 202 148 bpf_loop__destroy(skel); 203 149 }
+114
tools/testing/selftests/bpf/progs/bpf_loop.c
··· 11 11 int output; 12 12 }; 13 13 14 + struct { 15 + __uint(type, BPF_MAP_TYPE_HASH); 16 + __uint(max_entries, 32); 17 + __type(key, int); 18 + __type(value, int); 19 + } map1 SEC(".maps"); 20 + 14 21 /* These should be set by the user program */ 15 22 u32 nested_callback_nr_loops; 16 23 u32 stop_index = -1; 17 24 u32 nr_loops; 18 25 int pid; 26 + int callback_selector; 19 27 20 28 /* Making these global variables so that the userspace program 21 29 * can verify the output through the skeleton ··· 116 108 bpf_loop(nr_loops, nested_callback1, &data, 0); 117 109 118 110 g_output = data.output; 111 + 112 + return 0; 113 + } 114 + 115 + static int callback_set_f0(int i, void *ctx) 116 + { 117 + g_output = 0xF0; 118 + return 0; 119 + } 120 + 121 + static int callback_set_0f(int i, void *ctx) 122 + { 123 + g_output = 0x0F; 124 + return 0; 125 + } 126 + 127 + /* 128 + * non-constant callback is a corner case for bpf_loop inline logic 129 + */ 130 + SEC("fentry/" SYS_PREFIX "sys_nanosleep") 131 + int prog_non_constant_callback(void *ctx) 132 + { 133 + struct callback_ctx data = {}; 134 + 135 + if (bpf_get_current_pid_tgid() >> 32 != pid) 136 + return 0; 137 + 138 + int (*callback)(int i, void *ctx); 139 + 140 + g_output = 0; 141 + 142 + if (callback_selector == 0x0F) 143 + callback = callback_set_0f; 144 + else 145 + callback = callback_set_f0; 146 + 147 + bpf_loop(1, callback, NULL, 0); 148 + 149 + return 0; 150 + } 151 + 152 + static int stack_check_inner_callback(void *ctx) 153 + { 154 + return 0; 155 + } 156 + 157 + static int map1_lookup_elem(int key) 158 + { 159 + int *val = bpf_map_lookup_elem(&map1, &key); 160 + 161 + return val ? *val : -1; 162 + } 163 + 164 + static void map1_update_elem(int key, int val) 165 + { 166 + bpf_map_update_elem(&map1, &key, &val, BPF_ANY); 167 + } 168 + 169 + static int stack_check_outer_callback(void *ctx) 170 + { 171 + int a = map1_lookup_elem(1); 172 + int b = map1_lookup_elem(2); 173 + int c = map1_lookup_elem(3); 174 + int d = map1_lookup_elem(4); 175 + int e = map1_lookup_elem(5); 176 + int f = map1_lookup_elem(6); 177 + 178 + bpf_loop(1, stack_check_inner_callback, NULL, 0); 179 + 180 + map1_update_elem(1, a + 1); 181 + map1_update_elem(2, b + 1); 182 + map1_update_elem(3, c + 1); 183 + map1_update_elem(4, d + 1); 184 + map1_update_elem(5, e + 1); 185 + map1_update_elem(6, f + 1); 186 + 187 + return 0; 188 + } 189 + 190 + /* Some of the local variables in stack_check and 191 + * stack_check_outer_callback would be allocated on stack by 192 + * compiler. This test should verify that stack content for these 193 + * variables is preserved between calls to bpf_loop (might be an issue 194 + * if loop inlining allocates stack slots incorrectly). 195 + */ 196 + SEC("fentry/" SYS_PREFIX "sys_nanosleep") 197 + int stack_check(void *ctx) 198 + { 199 + if (bpf_get_current_pid_tgid() >> 32 != pid) 200 + return 0; 201 + 202 + int a = map1_lookup_elem(7); 203 + int b = map1_lookup_elem(8); 204 + int c = map1_lookup_elem(9); 205 + int d = map1_lookup_elem(10); 206 + int e = map1_lookup_elem(11); 207 + int f = map1_lookup_elem(12); 208 + 209 + bpf_loop(1, stack_check_outer_callback, NULL, 0); 210 + 211 + map1_update_elem(7, a + 1); 212 + map1_update_elem(8, b + 1); 213 + map1_update_elem(9, c + 1); 214 + map1_update_elem(10, d + 1); 215 + map1_update_elem(11, e + 1); 216 + map1_update_elem(12, f + 1); 119 217 120 218 return 0; 121 219 }