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

selftests/bpf: Stress test attaching a BPF prog to another BPF prog

Add a test that invokes a BPF prog in a loop, while concurrently
attaching and detaching another BPF prog to and from it. This helps
identifying race conditions in bpf_arch_text_poke().

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Link: https://lore.kernel.org/r/20250716194524.48109-3-iii@linux.ibm.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Ilya Leoshkevich and committed by
Alexei Starovoitov
d459dbbb 6a5abf8c

+67
+67
tools/testing/selftests/bpf/prog_tests/recursive_attach.c
··· 149 149 fentry_recursive_target__destroy(target_skel); 150 150 fentry_recursive__destroy(tracing_skel); 151 151 } 152 + 153 + static void *fentry_target_test_run(void *arg) 154 + { 155 + for (;;) { 156 + int prog_fd = __atomic_load_n((int *)arg, __ATOMIC_SEQ_CST); 157 + LIBBPF_OPTS(bpf_test_run_opts, topts); 158 + int err; 159 + 160 + if (prog_fd == -1) 161 + break; 162 + err = bpf_prog_test_run_opts(prog_fd, &topts); 163 + if (!ASSERT_OK(err, "fentry_target test_run")) 164 + break; 165 + } 166 + 167 + return NULL; 168 + } 169 + 170 + void test_fentry_attach_stress(void) 171 + { 172 + struct fentry_recursive_target *target_skel = NULL; 173 + struct fentry_recursive *tracing_skel = NULL; 174 + struct bpf_program *prog; 175 + int err, i, tgt_prog_fd; 176 + pthread_t thread; 177 + 178 + target_skel = fentry_recursive_target__open_and_load(); 179 + if (!ASSERT_OK_PTR(target_skel, 180 + "fentry_recursive_target__open_and_load")) 181 + goto close_prog; 182 + tgt_prog_fd = bpf_program__fd(target_skel->progs.fentry_target); 183 + err = pthread_create(&thread, NULL, 184 + fentry_target_test_run, &tgt_prog_fd); 185 + if (!ASSERT_OK(err, "bpf_program__set_attach_target")) 186 + goto close_prog; 187 + 188 + for (i = 0; i < 1000; i++) { 189 + tracing_skel = fentry_recursive__open(); 190 + if (!ASSERT_OK_PTR(tracing_skel, "fentry_recursive__open")) 191 + goto stop_thread; 192 + 193 + prog = tracing_skel->progs.recursive_attach; 194 + err = bpf_program__set_attach_target(prog, tgt_prog_fd, 195 + "fentry_target"); 196 + if (!ASSERT_OK(err, "bpf_program__set_attach_target")) 197 + goto stop_thread; 198 + 199 + err = fentry_recursive__load(tracing_skel); 200 + if (!ASSERT_OK(err, "fentry_recursive__load")) 201 + goto stop_thread; 202 + 203 + err = fentry_recursive__attach(tracing_skel); 204 + if (!ASSERT_OK(err, "fentry_recursive__attach")) 205 + goto stop_thread; 206 + 207 + fentry_recursive__destroy(tracing_skel); 208 + tracing_skel = NULL; 209 + } 210 + 211 + stop_thread: 212 + __atomic_store_n(&tgt_prog_fd, -1, __ATOMIC_SEQ_CST); 213 + err = pthread_join(thread, NULL); 214 + ASSERT_OK(err, "pthread_join"); 215 + close_prog: 216 + fentry_recursive__destroy(tracing_skel); 217 + fentry_recursive_target__destroy(target_skel); 218 + }