Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/tests/test_spinlock.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tests / test_spinlock.c` 338 lines ยท 10780 bytes Raw Blame History 1 /* test_spinlock.c -- host tests for core/spinlock.h (DRA-556). 2 * 3 * Mutual exclusion under contention, FIFO order (fairness), counter wrap, and the interrupt mask: on a host a 4 * thread's interrupt is CK_SPIN_HOST_IRQ_SIGNAL (SIGUSR1). A handler that takes a lock its own thread holds must 5 * be deferred until the unlock; without the mask it spins on its own ticket forever. A watchdog thread turns that 6 * hang into "test_spinlock: FAIL" (an alarm, not a cap: a passing run never waits on it). 7 * Ordering is checked deterministically: CK_SPIN_TEST_STEP raises the interrupt between the two steps of acquire 8 * (mask, ticket) and of release (release, restore). The TEST-ONLY mutants -DCK_SPIN_TEST_NO_IRQ_MASK, 9 * -DCK_SPIN_TEST_TICKET_BEFORE_MASK and -DCK_SPIN_TEST_RESTORE_BEFORE_RELEASE must each FAIL here 10 * (make spinlock-mutant). The _nomask forms: mutual exclusion, trylock never jumps the queue, mask untouched. 11 */ 12 #define _GNU_SOURCE 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 22 struct ck_spinlock; 23 static void step_hook(struct ck_spinlock *l); 24 #define CK_SPIN_TEST_STEP(l) step_hook(l) 25 #include "spinlock.h" 26 27 /* Armed with a lock: the next acquire or release step on that lock raises the interrupt once. */ 28 static struct ck_spinlock *volatile step_armed; 29 static void step_hook(struct ck_spinlock *l) 30 { 31 if (l && l == step_armed) { 32 step_armed = 0; 33 pthread_kill(pthread_self(), CK_SPIN_HOST_IRQ_SIGNAL); 34 } 35 } 36 37 static int failures; 38 #define CHECK(c) \ 39 do { \ 40 if (!(c)) { \ 41 printf("test_spinlock: CHECK failed %s:%d: %s\n", __FILE__, __LINE__, #c); \ 42 failures++; \ 43 } \ 44 } while (0) 45 46 /* ---- watchdog: no progress for STALL_S seconds means a deadlock ---- */ 47 #define STALL_S 5 48 static volatile uint64_t progress; 49 static volatile int done; 50 static const char *volatile phase = "start"; 51 52 static void *watchdog(void *arg) 53 { 54 (void)arg; 55 uint64_t last = (uint64_t)-1; 56 int still = 0; 57 while (!__atomic_load_n(&done, __ATOMIC_ACQUIRE)) { 58 sleep(1); 59 uint64_t now = __atomic_load_n(&progress, __ATOMIC_RELAXED); 60 still = now == last ? still + 1 : 0; 61 last = now; 62 if (still >= STALL_S) { 63 printf("test_spinlock: FAIL deadlock (no progress for %d s) in phase %s\n", STALL_S, phase); 64 fflush(stdout); 65 _exit(1); 66 } 67 } 68 return 0; 69 } 70 static void tick(void) { __atomic_add_fetch(&progress, 1, __ATOMIC_RELAXED); } 71 72 /* ---- 1. mutual exclusion ---- */ 73 #define MX_THREADS 4 74 #define MX_ITERS 200000 75 static struct ck_spinlock mx = CK_SPINLOCK_INIT; 76 static uint64_t mx_count; /* plain, only under mx */ 77 78 static void *mx_worker(void *arg) 79 { 80 for (int i = 0; i < MX_ITERS; i++) { 81 int open = (int)(intptr_t)arg; /* half the threads take the same lock with the _nomask forms */ 82 if (open) 83 ck_spin_lock_nomask(&mx); 84 else 85 ck_spin_lock(&mx); 86 uint64_t v = mx_count; 87 __asm__ volatile("" ::: "memory"); 88 mx_count = v + 1; 89 if (open) 90 ck_spin_unlock_nomask(&mx); 91 else 92 ck_spin_unlock(&mx); 93 tick(); 94 } 95 return 0; 96 } 97 98 static void test_mutex(void) 99 { 100 phase = "mutex"; 101 pthread_t th[MX_THREADS]; 102 for (int i = 0; i < MX_THREADS; i++) 103 CHECK(pthread_create(&th[i], 0, mx_worker, (void *)(intptr_t)(i & 1)) == 0); 104 for (int i = 0; i < MX_THREADS; i++) 105 pthread_join(th[i], 0); 106 CHECK(mx_count == (uint64_t)MX_THREADS * MX_ITERS); 107 CHECK(!ck_spin_busy(&mx)); 108 } 109 110 /* ---- 2. FIFO: B queues before C, so B enters before C ---- */ 111 static struct ck_spinlock ff; 112 static int ff_order[2], ff_n; 113 114 static void *ff_worker(void *arg) 115 { 116 int id = (int)(intptr_t)arg; 117 ck_spin_lock(&ff); 118 ff_order[ff_n++] = id; 119 ck_spin_unlock(&ff); 120 return 0; 121 } 122 123 static void wait_ticket(const struct ck_spinlock *l, uint32_t want) 124 { 125 while (__atomic_load_n(&l->ticket, __ATOMIC_ACQUIRE) != want) 126 sched_yield(); 127 } 128 129 static void fifo_round(uint32_t start) 130 { 131 ff.ticket = ff.serving = start; 132 ff.irq = 0; 133 ff_n = 0; 134 ck_spin_lock(&ff); /* A holds: ticket start */ 135 pthread_t b, c; 136 CHECK(pthread_create(&b, 0, ff_worker, (void *)(intptr_t)1) == 0); 137 wait_ticket(&ff, start + 2u); /* B took ticket start+1 */ 138 CHECK(pthread_create(&c, 0, ff_worker, (void *)(intptr_t)2) == 0); 139 wait_ticket(&ff, start + 3u); /* C took ticket start+2 */ 140 ck_spin_unlock(&ff); 141 pthread_join(b, 0); 142 pthread_join(c, 0); 143 CHECK(ff_n == 2 && ff_order[0] == 1 && ff_order[1] == 2); 144 CHECK(ff.ticket == start + 3u && ff.serving == start + 3u); 145 tick(); 146 } 147 148 static void test_fifo(void) 149 { 150 phase = "fifo"; 151 for (int r = 0; r < 200; r++) 152 fifo_round((uint32_t)r * 7919u); 153 phase = "fifo-wrap"; 154 for (uint32_t s = 0xFFFFFFFCu; s != 2u; s++) /* rounds that cross 2^32 */ 155 fifo_round(s); 156 } 157 158 /* ---- 3. interrupt mask ---- */ 159 static struct ck_spinlock ia = CK_SPINLOCK_INIT, ib = CK_SPINLOCK_INIT; 160 static volatile sig_atomic_t irq_ran, irq_saw_busy; 161 162 static void on_irq(int sig) 163 { 164 (void)sig; 165 if (ck_spin_busy(&ia)) 166 irq_saw_busy = 1; 167 ck_spin_lock(&ia); /* the interrupt takes the lock its own CPU (thread) may hold */ 168 irq_ran++; 169 ck_spin_unlock(&ia); 170 } 171 172 static int irq_blocked(void) 173 { 174 sigset_t cur; 175 pthread_sigmask(SIG_SETMASK, 0, &cur); 176 return sigismember(&cur, CK_SPIN_HOST_IRQ_SIGNAL) == 1; 177 } 178 179 static void step_acquire(void) 180 { 181 /* Interrupt raised exactly between mask and ticket: it must wait (mutant ticket-before-mask: it lands after the 182 * ticket and before the mask, and the handler spins on its own CPU's ticket). */ 183 phase = "irq-step-acquire"; 184 irq_ran = 0; 185 step_armed = &ia; 186 ck_spin_lock(&ia); 187 tick(); 188 CHECK(irq_ran == 0); 189 ck_spin_unlock(&ia); 190 CHECK(irq_ran == 1); 191 CHECK(step_armed == 0); 192 tick(); 193 } 194 195 static void step_release(void) 196 { 197 /* Interrupt raised exactly between release and restore: the lock is already free when it runs (mutant 198 * restore-before-release: it runs before the release, on a held lock). */ 199 phase = "irq-step-release"; 200 irq_ran = 0; 201 ck_spin_lock(&ia); 202 step_armed = &ia; 203 ck_spin_unlock(&ia); 204 CHECK(irq_ran == 1); 205 CHECK(step_armed == 0); 206 CHECK(!ck_spin_busy(&ia)); 207 tick(); 208 } 209 210 static void irq_setup(void) 211 { 212 struct sigaction sa; 213 memset(&sa, 0, sizeof sa); 214 sa.sa_handler = on_irq; 215 sigemptyset(&sa.sa_mask); 216 CHECK(sigaction(CK_SPIN_HOST_IRQ_SIGNAL, &sa, 0) == 0); 217 } 218 219 static void test_irq(void) 220 { 221 irq_setup(); 222 223 phase = "irq-open"; 224 irq_ran = 0; 225 pthread_kill(pthread_self(), CK_SPIN_HOST_IRQ_SIGNAL); /* lock free: the handler runs at once */ 226 CHECK(irq_ran == 1); 227 tick(); 228 229 phase = "irq-held"; 230 irq_ran = irq_saw_busy = 0; 231 ck_spin_lock(&ia); 232 CHECK(irq_blocked()); 233 pthread_kill(pthread_self(), CK_SPIN_HOST_IRQ_SIGNAL); /* raised while held: must wait */ 234 tick(); 235 CHECK(irq_ran == 0); 236 ck_spin_unlock(&ia); /* delivered here, after the release */ 237 CHECK(irq_ran == 1); 238 CHECK(irq_saw_busy == 0); 239 CHECK(!irq_blocked()); 240 tick(); 241 242 phase = "irq-nested"; 243 irq_ran = 0; 244 ck_irqflags_t fa = ck_spin_lock_irqsave(&ib); 245 ck_spin_lock(&ia); 246 pthread_kill(pthread_self(), CK_SPIN_HOST_IRQ_SIGNAL); 247 tick(); 248 ck_spin_unlock(&ia); /* inner restore keeps the outer mask */ 249 CHECK(irq_ran == 0); 250 CHECK(irq_blocked()); 251 ck_spin_unlock_irqrestore(&ib, fa); 252 CHECK(irq_ran == 1); 253 tick(); 254 255 step_acquire(); 256 step_release(); 257 258 phase = "irq-premasked"; 259 sigset_t one; 260 sigemptyset(&one); 261 sigaddset(&one, CK_SPIN_HOST_IRQ_SIGNAL); 262 pthread_sigmask(SIG_BLOCK, &one, 0); 263 ck_spin_lock(&ia); 264 ck_spin_unlock(&ia); 265 CHECK(irq_blocked()); /* a caller that had interrupts off gets them back off */ 266 pthread_sigmask(SIG_UNBLOCK, &one, 0); 267 tick(); 268 } 269 270 /* ---- 4. _nomask forms ---- */ 271 static struct ck_spinlock nm = CK_SPINLOCK_INIT; 272 static void *nm_waiter(void *arg) 273 { 274 (void)arg; 275 ck_spin_lock_nomask(&nm); 276 ck_spin_unlock_nomask(&nm); 277 return 0; 278 } 279 280 static void test_nomask(void) 281 { 282 phase = "nomask"; 283 CHECK(ck_spin_trylock_nomask(&nm) == 1); /* free */ 284 CHECK(!irq_blocked()); /* the mask is untouched */ 285 CHECK(ck_spin_trylock_nomask(&nm) == 0); /* held */ 286 CHECK(nm.ticket == 1u && nm.serving == 0u); 287 pthread_t w; 288 CHECK(pthread_create(&w, 0, nm_waiter, 0) == 0); 289 wait_ticket(&nm, 2u); /* a waiter queued behind the holder */ 290 ck_spin_unlock_nomask(&nm); /* handed to the waiter */ 291 pthread_join(w, 0); 292 CHECK(nm.ticket == 2u && nm.serving == 2u); 293 ck_spin_lock_nomask(&nm); 294 CHECK(pthread_create(&w, 0, nm_waiter, 0) == 0); 295 wait_ticket(&nm, 4u); 296 CHECK(ck_spin_trylock_nomask(&nm) == 0); /* never jumps the queue */ 297 CHECK(nm.ticket == 4u); 298 ck_spin_unlock_nomask(&nm); 299 pthread_join(w, 0); 300 CHECK(ck_spin_trylock_nomask(&nm) == 1); 301 ck_spin_unlock_nomask(&nm); 302 CHECK(!ck_spin_busy(&nm)); 303 nm.ticket = nm.serving = 0xFFFFFFFFu; /* trylock across the wrap */ 304 CHECK(ck_spin_trylock_nomask(&nm) == 1 && nm.ticket == 0u); 305 ck_spin_unlock_nomask(&nm); 306 CHECK(nm.serving == 0u && !ck_spin_busy(&nm)); 307 tick(); 308 } 309 310 int main(void) 311 { 312 pthread_t wd; 313 if (pthread_create(&wd, 0, watchdog, 0) != 0) 314 return 2; 315 ck_spin_init(&ff); 316 /* TEST_SPINLOCK_ONLY=irq-step-acquire|irq-step-release runs that phase alone (make spinlock-mutant uses it to 317 * show each ordering mutant caught by its own phase). */ 318 const char *only = getenv("TEST_SPINLOCK_ONLY"); 319 if (only && !strcmp(only, "irq-step-acquire")) { 320 irq_setup(); 321 step_acquire(); 322 } else if (only && !strcmp(only, "irq-step-release")) { 323 irq_setup(); 324 step_release(); 325 } else if (only) { 326 printf("test_spinlock: FAIL unknown TEST_SPINLOCK_ONLY=%s\n", only); 327 return 2; 328 } else { 329 test_mutex(); 330 test_fifo(); 331 test_irq(); 332 test_nomask(); 333 } 334 __atomic_store_n(&done, 1, __ATOMIC_RELEASE); 335 pthread_join(wd, 0); 336 printf("test_spinlock: %s (%d failures)\n", failures ? "FAIL" : "PASS", failures); 337 return failures ? 1 : 0; 338 }