1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (c) 2024 Meta Platforms, Inc. and affiliates. 4 * Copyright (c) 2024 David Vernet <dvernet@meta.com> 5 */ 6 #include <bpf/bpf.h> 7 #include <errno.h> 8 #include <pthread.h> 9 #include <scx/common.h> 10 #include <sys/wait.h> 11 #include <unistd.h> 12 #include "maximal.bpf.skel.h" 13 #include "scx_test.h" 14 15 enum { 16 RELOAD_ITERS = 1024, 17 RELOAD_THREADS = 2, 18 }; 19 20 static struct maximal *skel; 21 static pthread_t threads[RELOAD_THREADS]; 22 static int unexpected_errno; 23 24 bool force_exit = false; 25 26 static enum scx_test_status setup(void **ctx) 27 { 28 skel = maximal__open(); 29 SCX_FAIL_IF(!skel, "Failed to open"); 30 SCX_ENUM_INIT(skel); 31 SCX_FAIL_IF(maximal__load(skel), "Failed to load skel"); 32 33 bpf_map__set_autoattach(skel->maps.maximal_ops, false); 34 SCX_FAIL_IF(maximal__attach(skel), "Failed to attach skel"); 35 36 return SCX_TEST_PASS; 37 } 38 39 static void *do_reload_loop(void *arg) 40 { 41 u32 i, failures = 0; 42 43 for (i = 0; i < RELOAD_ITERS && !force_exit; i++) { 44 struct bpf_link *link; 45 46 errno = 0; 47 link = bpf_map__attach_struct_ops(skel->maps.maximal_ops); 48 if (!link) { 49 int e = errno; 50 51 if (e != EBUSY) 52 __atomic_store_n(&unexpected_errno, e, 53 __ATOMIC_RELAXED); 54 failures++; 55 continue; 56 } 57 58 bpf_link__destroy(link); 59 } 60 61 return (void *)(unsigned long)failures; 62 } 63 64 static enum scx_test_status run(void *ctx) 65 { 66 unsigned long failures[RELOAD_THREADS] = {}; 67 int err, i; 68 void *ret; 69 70 err = pthread_create(&threads[0], NULL, do_reload_loop, NULL); 71 SCX_FAIL_IF(err, "Failed to create thread 0"); 72 73 err = pthread_create(&threads[1], NULL, do_reload_loop, NULL); 74 if (err) { 75 force_exit = true; 76 pthread_join(threads[0], NULL); 77 SCX_FAIL("Failed to create thread 1"); 78 } 79 80 SCX_FAIL_IF(pthread_join(threads[0], &ret), "thread 0 failed"); 81 failures[0] = (unsigned long)ret; 82 83 SCX_FAIL_IF(pthread_join(threads[1], &ret), "thread 1 failed"); 84 failures[1] = (unsigned long)ret; 85 86 SCX_FAIL_IF(unexpected_errno, "unexpected attach errno %d", 87 unexpected_errno); 88 89 for (i = 0; i < RELOAD_THREADS; i++) 90 SCX_FAIL_IF(failures[i] == RELOAD_ITERS, 91 "thread %d: every attach failed (%lu EBUSY)", 92 i, failures[i]); 93 94 return SCX_TEST_PASS; 95 } 96 97 static void cleanup(void *ctx) 98 { 99 force_exit = true; 100 maximal__destroy(skel); 101 } 102 103 struct scx_test reload_loop = { 104 .name = "reload_loop", 105 .description = "Stress test loading and unloading schedulers repeatedly in a tight loop", 106 .setup = setup, 107 .run = run, 108 .cleanup = cleanup, 109 }; 110 REGISTER_SCX_TEST(&reload_loop) 111