DF-5183 / lwqdemo.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | /* * lwqdemo.c - runtime PoC harness for the DragonFly drm LinuxKPI wait shim * (sys/dev/drm/linux_wait.c + sys/dev/drm/include/linux/wait.h). * * The module compiles the REAL, UNMODIFIED sys/dev/drm/linux_wait.c into the * KLD (the guest kernel does not include drm), and drives the real * wait_event_timeout() macro from <linux/wait.h> against it. * * Experiment 1 (DF-5182): lost wakeup in the wait_event family. * The macro evaluates the condition BEFORE calling tsleep_interlock(): * * __wait_event_prefix(&wq, flags); (set state) * if (condition) break; (condition eval) * tsleep_interlock(current, flags); (interlock) * tsleep(current, PINTERLOCKED|flags, ...); * * A wake_up() landing between the condition evaluation and the interlock * is dropped (nobody is interlocked yet), and autoremove_wake_function() * unconditionally unlinks the wait entry (wake_up_process() always * returns 1), so no later wake_up() can reach the stranded sleeper * either: it burns its full timeout and wait_event_timeout() returns 0 * ("condition never became true") even though the condition WAS set * true before the wake. * * A pure timing race could not hit this in 1.67M rounds on this guest * (an external waker's store->wakeup() delivery pipeline is slower than * the waiter's condition->interlock path, see VERDICT.md). The window * is realistically entered when (a) the condition expression itself has * width between reading the awaited state and returning - production * drm conditions do (e.g. radeon_fence_wait_seq_timeout() evaluates * radeon_fence_any_seq_signaled() which walks RADEON_NUM_BITS atomic64 * counters), or (b) the waiter is preempted between check and * interlock. The harness models (a) deterministically: the condition * helper reads `gen' and then completes only when the waker pokes it, * giving the waker a precisely controlled in-window slot: * * WINDOWED round: * waiter: gen_snap = gen; window_open = 1; spin until poke * waker: (sees window_open) gen++; wake_up() [FIRST wake: * delivered between the condition's state read and the * interlock -> lost + entry auto-removed] * poke = 1 [release the condition] * ... waiter interlocks, tsleeps, burns its FULL timeout * ~100us later: wake_up() again [RESCUE wake: entry was * unlinked, cannot reach the sleeper] * outcome: ret == 0 (timeout) while gen != snap => MISREPORT * * CONTROL round (proves the harness works when the wake does NOT * land in the window): the waker bumps + wakes only after the * condition has completed and the waiter is interlocked-asleep; * the waiter returns immediately with ret > 0. * * Experiment 2 (DF-5183): timeout parameter truncation. * wait_on_bit()/wait_on_bit_timeout(..., MAX_SCHEDULE_TIMEOUT) pass * (unsigned long)LONG_MAX into tsleep()'s `int timo', truncating to -1; * _callout_setup_quick() clamps negative expiry to the next tick, so the * "infinite" sleep actually returns EWOULDBLOCK after ~1 tick. * Demonstrated directly at the tsleep() boundary with no waker. * * Results are printed with the "LWQ:" prefix; unload refused while running. */ #include <sys/param.h> #include <sys/kernel.h> #include <sys/systm.h> #include <sys/malloc.h> #include <sys/proc.h> #include <sys/kthread.h> #include <sys/spinlock.h> #include <sys/module.h> #include <machine/cpufunc.h> #include <linux/wait.h> #include <linux/wait_bit.h> #include <linux/sched.h> MALLOC_DEFINE(M_LWQDEMO, "lwqdemo", "lwqdemo task_structs"); /* * local linux_task_alloc() - trimmed copy of sys/dev/drm/linux_sched.c:59 * (the guest kernel has no drm, so we provide the allocator the `current' * macro lazily calls; kthreads have no proc/mm which is fine here). */ struct task_struct * linux_task_alloc(struct thread *td) { struct task_struct *task; task = kmalloc(sizeof(*task), M_LWQDEMO, M_WAITOK | M_ZERO); task->dfly_td = td; task->pid = -1; spin_init(&task->kt_spin, "lwqtsp"); td->td_linux_task = task; return task; } static wait_queue_head_t demo_wq; static volatile u_int gen; /* condition generation */ static volatile u_int rounds_started; static volatile u_int rounds_done; static volatile u_int fast_rounds; static volatile u_int misreports; static volatile u_int control_ok; static volatile u_int rescues_fired; static volatile int round_windowed; /* current round mode */ static volatile int window_open; /* condition read done, not returned */ static volatile int poke; /* waker -> condition: complete */ static volatile int stopping; static volatile int done; static volatile u_int gen_snap; /* condition's view of gen */ static volatile u_int waiter_g0; /* this round's g0 */ #define TIMEOUT_TICKS (2 * hz) #define MAX_MISREPORTS 8 #define RUN_SECONDS 90 static __inline void pause1(void) { __asm __volatile("pause"); } static __inline void spin_until(uint64_t target) { while (rdtsc() < target) pause1(); } /* * The wait condition. Models a condition with non-trivial width between * its read of the awaited state (gen) and its return, as production drm * conditions have (radeon_fence_any_seq_signaled, i915_sw_finish_query, * ...). In WINDOWED rounds the waker is allowed into that slot. */ static int cond_expr(void) { gen_snap = gen; /* the state read */ if (round_windowed) { window_open = 1; while (!poke && !stopping) pause1(); window_open = 0; poke = 0; } return (gen_snap != waiter_g0); } /* * ------------------------------------------------------------------ * Experiment 1: waiter thread * ------------------------------------------------------------------ */ static void waiter_thread(void *arg) { long ret; int start, dt, windowed; kprintf("LWQ: waiter started (tid %p)\n", curthread); while (!stopping) { windowed = (rounds_started & 1) == 0; /* alternate */ round_windowed = windowed; waiter_g0 = gen; rounds_started++; start = ticks; ret = wait_event_timeout(demo_wq, cond_expr(), TIMEOUT_TICKS); dt = ticks - start; rounds_done++; if (ret > 0) { fast_rounds++; if (!windowed) control_ok++; else kprintf("LWQ: windowed round returned fast " "(unexpected - wake NOT lost?)\n"); } else if (ret == 0) { if (gen != waiter_g0) { misreports++; kprintf("LWQ: MISREPORT #%u (windowed) " "dt=%d ticks: wait_event_timeout " "returned 0 (timeout) but condition " "gen %u != %u is TRUE - the in-window " "wake was lost and the rescue wake " "could not find the auto-removed " "entry\n", misreports, dt, gen, waiter_g0); } else if (windowed) { kprintf("LWQ: windowed round true-timeout " "(waker failed to bump?)\n"); } else { kprintf("LWQ: NATURAL RACE HIT in a control " "round (no widened condition)! " "dt=%d\n", dt); } } } kprintf("LWQ: waiter exiting\n"); wakeup(&done); kthread_exit(); } /* * ------------------------------------------------------------------ * Experiment 1: waker thread * ------------------------------------------------------------------ */ static void waker_thread(void *arg) { u_int served; kprintf("LWQ: waker started (tid %p)\n", curthread); while (!stopping) { /* wait for a round to be in flight */ while (rounds_done + 1 != rounds_started && !stopping) pause1(); if (stopping) break; served = rounds_started; if (round_windowed) { /* wait until the waiter is INSIDE the condition * window (state read done, not yet returned) */ while (!window_open && !stopping) pause1(); if (stopping) break; gen++; /* condition becomes true */ wake_up(&demo_wq); /* FIRST (in-window) wake */ /* let the waiter finish the condition and go to * sleep (interlocked) */ poke = 1; /* ~100 us: waiter now stranded asleep with its * wait entry unlinked */ spin_until(rdtsc() + 300000); rescues_fired++; wake_up(&demo_wq); /* RESCUE wake - misses */ /* round completes at full timeout */ } else { /* CONTROL round: bump + wake AFTER the condition * has completed and the waiter is interlocked and * asleep - this is the path that works. */ spin_until(rdtsc() + 20000); /* ~7 us */ gen++; wake_up(&demo_wq); } /* wait for the round to complete */ while (rounds_done != served && !stopping) pause1(); } kprintf("LWQ: waker exiting\n"); wakeup(&done); kthread_exit(); } /* * ------------------------------------------------------------------ * Supervisor * ------------------------------------------------------------------ */ static void ctrl_thread(void *arg) { int i; for (i = 0; i < RUN_SECONDS; i++) { tsleep(&ctrl_thread, 0, "lwqctl", hz); if (misreports >= MAX_MISREPORTS) break; } stopping = 1; poke = 1; gen++; wake_up(&demo_wq); tsleep(&ctrl_thread, 0, "lwqfin", TIMEOUT_TICKS + hz); kprintf("LWQ: ================= SUMMARY =================\n"); kprintf("LWQ: rounds_done=%u fast=%u control_ok=%u " "misreports=%u rescues_fired=%u\n", rounds_done, fast_rounds, control_ok, misreports, rescues_fired); kprintf("LWQ: verdict: %s\n", misreports ? "REPRODUCED - windowed rounds: in-window wake LOST + auto-removed " "entry strands the sleeper; wait_event_timeout() reports timeout " "although the condition was set true before the wake; rescue " "wake cannot reach it; control rounds behave correctly" : "no misreport observed this run"); kprintf("LWQ: ===========================================\n"); done = 1; wakeup(&done); kthread_exit(); } /* * ------------------------------------------------------------------ * Experiment 2 (DF-5183): MAX_SCHEDULE_TIMEOUT truncation * ------------------------------------------------------------------ */ static volatile unsigned long f3_word = 1; static void f3_thread(void *arg) { int start, dt, rv, timo; tsleep(&f3_thread, 0, "lwqf3a", hz); timo = (int)MAX_SCHEDULE_TIMEOUT; /* what tsleep() receives */ start = ticks; rv = tsleep((void *)&f3_word, 0, "lwqf3", timo); dt = ticks - start; kprintf("LWQ-F3: tsleep(timo=(int)MAX_SCHEDULE_TIMEOUT=%d) returned " "rv=%d after %d ticks with NO waker - 'infinite' timeout actually " "expires every tick (LONG_MAX truncates to int -1, callout clamps " "to next tick)\n", timo, rv, dt); start = ticks; rv = wait_on_bit_timeout((unsigned long *)&f3_word, 0, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); dt = ticks - start; kprintf("LWQ-F3: wait_on_bit_timeout(MAX_SCHEDULE_TIMEOUT) on a set " "bit returned rv=%d after %d ticks (only exits because we clear " "the bit below; internally it polls at 1 tick granularity)\n", rv, dt); wakeup(&done); kthread_exit(); } static void f3_clearer(void *arg) { tsleep(&f3_clearer, 0, "lwqf3c", 5 * hz); f3_word = 0; wake_up_bit((void *)&f3_word, 0); wakeup(&done); kthread_exit(); } /* * ------------------------------------------------------------------ * module glue * ------------------------------------------------------------------ */ static int lwqdemo_load(module_t mod, int what, void *arg) { int error; switch (what) { case MOD_LOAD: init_waitqueue_head(&demo_wq); stopping = 0; done = 0; rounds_started = 0; rounds_done = 0; error = kthread_create(waiter_thread, NULL, NULL, "lwq_waiter"); if (error) return (error); error = kthread_create(waker_thread, NULL, NULL, "lwq_waker"); if (error) return (error); error = kthread_create(ctrl_thread, NULL, NULL, "lwq_ctrl"); if (error) return (error); error = kthread_create(f3_thread, NULL, NULL, "lwq_f3"); if (error) return (error); error = kthread_create(f3_clearer, NULL, NULL, "lwq_f3clr"); if (error) return (error); kprintf("LWQ: module loaded, experiments running " "(check dmesg for LWQ:/LWQ-F3: lines)\n"); return (0); case MOD_UNLOAD: if (!done) { kprintf("LWQ: still running, refusing unload\n"); return (EBUSY); } return (0); } return (EOPNOTSUPP); } static moduledata_t lwqdemo_mod = { "lwqdemo", lwqdemo_load, NULL }; DECLARE_MODULE(lwqdemo, lwqdemo_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); |