swait_active(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

okay so bestie, welcome to swait_active(9) from the Kernel API Manual β€” this one's a tiny little lockless vibe check for waitqueues, and she's giving main character energy: small, iconic, and not to be underestimated πŸ’…βœ¨

πŸ’– NAME πŸ’–

swait_active - - locklessly test for waiters on the queue

✨ SYNOPSIS ✨

int swait_active (struct swait_queue_head *wq );

πŸŽ€ ARGUMENTS πŸŽ€

  • wq β€” the waitqueue to test for waiters 🫢

πŸ¦‹ DESCRIPTION πŸ¦‹

returns true if the wait list is not empty. lowkey that's the whole plot, and honestly? obsessed ✨

πŸ’… NOTE πŸ’…

okay bestie, this function is lockless and requires care β€” like, incorrect usage _will_ lead to sporadic and non-obvious failure. no cap, fr fr, do not treat this like a cute little shortcut without reading the fine print 😭 iykyk

NOTE2: this function has the same above implications as regular waitqueues.

Use either while holding swait_queue_head::lock or when used for wakeups with an extra smp_mb like:

CPU0 - waker                    CPU1 - waiter

for (;;) { cond = true;
prepare_to_swait_exclusive(wq_head, wait, state); smp_mb;
// smp_mb from set_current_state if (swait_active(wq_head))        if
(cond) wake_up(wq_head);                      break; schedule; }
finish_swait(wq_head, wait);

Because without the explicit smp_mb it's possible for the swait_active load to get hoisted over the cond store such that we'll observe an empty wait list while the waiter might not observe cond. This, in turn, can trigger missing wakeups. missing wakeups are not a slay, periodt πŸ’€

Also note that this 'optimization' trades a spin_lock for an smp_mb, which (when the lock is uncontended) are of roughly equal cost. ngl, that's the trade-off, bestie πŸ’…

🌈 SEE ALSO 🌈

Kernel file ./include/linux/swait.h β€” plus swq_has_sleeper(9), swait_event_idle_exclusive(9), and swait_event_idle_timeout_exclusive(9) πŸ¦‹ iykyk, go read those too.


that's the whole page, bestie 🫢 source path/version: ./include/linux, dated October 2026, and the page itself is swait_active(9) β€” periodt πŸ’–βœ¨

πŸ“¦ where this page comes from