XenevaOS
Loading...
Searching...
No Matches
wait.h
Go to the documentation of this file.
1#ifndef __LINUX_WAIT_H__
2#define __LINUX_WAIT_H__
3
4/*
5 * DCL <linux/wait.h> -- wait queues, with an honest answer to the one thing
6 * this port cannot do: sleep.
7 *
8 * There is no native equivalent in Xeneva and no scheduler to block in, so
9 * the queue is built for real -- the lists, the entries, the wake functions
10 * and the bookkeeping are all genuine -- while the *waiting* is done by
11 * polling, bounded. The distinction matters: what must not happen is a driver
12 * that believes it slept and then loops on a condition that only ever changes
13 * while it is spinning.
14 *
15 * Three call sites decide the design (the measured blocking surface of the
16 * nine files: 7 wait-queue ops, 5 schedule()):
17 *
18 * n_tty.c:2232 / :2354 -- blocking read and write, via
19 * DEFINE_WAIT_FUNC(wait, woken_wake_function) + add_wait_queue + a loop
20 * that ends in wait_woken(). Both are stage 5's EAGAIN-first cut; until
21 * that lands, an O_NONBLOCK fd returns -EAGAIN before reaching here
22 * (n_tty checks tty_io_nonblock() first), and a blocking fd with no
23 * data spins in wait_woken() rather than sleeping. The queue itself
24 * must therefore be correct -- it is what the cut will hang off.
25 *
26 * serial_core.c:3194 -- wait_event(state->remove_wait,
27 * !atomic_read(&state->refcount)) during port removal. The condition is
28 * normally already true (refcount drops to zero on the line above), so
29 * the bounded poll sees it on the first test and returns.
30 *
31 * serial_core.c:1205 / tty_io.c:3118 -- DECLARE_WAITQUEUE and
32 * init_waitqueue_head at construction, no waiting at all.
33 *
34 * wait_event() polls for DCL_WAIT_EVENT_MS and, if the condition still has
35 * not come true, says so through dcl_wait_event_timeout() before falling
36 * through. Mainline would block indefinitely there; falling through is the
37 * wrong answer dressed as the right one, which is exactly why it is logged
38 * instead of being quiet -- an unconditional block is a hung kernel and an
39 * unconditional fall-through is a corrupt one, and neither should look like
40 * a normal boot.
41 *
42 * add_wait_queue/remove_wait_queue are real list operations with no lock
43 * held across them (DCL's spinlocks are no-ops -- see <linux/spinlock.h>),
44 * so a wake arriving concurrently with an add is not excluded. That matches
45 * the locking contract of everything else here.
46 */
47
48#include <linux/kernel.h> /* mdelay (bounded polling) */
49#include <linux/list.h>
50#include <linux/spinlock.h>
51#include <linux/poll.h> /* EPOLL* keys passed to wake_up_*_poll */
52#include <linux/sched.h> /* current, TASK_*, task_struct */
53
54/*
55 * Forward-declared before its first use in a parameter list: a struct tag
56 * first named inside a function-pointer typedef's parameters has scope no
57 * wider than that declarator, so the definition below would have become a
58 * *different* struct and every assignment of a wake function to ->func would
59 * have been a type mismatch. The compiler said so; this is the fix, not a
60 * cast.
61 */
62struct wait_queue_entry;
63struct wait_queue_head;
64
65typedef int (*wait_queue_func_t)(struct wait_queue_entry* wq_entry,
66 unsigned mode, int flags, void* key);
67
69 unsigned int flags;
70 void* private;
73};
74
79
80/*
81 * wait_queue_head_t -- mainline's spelling. struct tty_struct carries two of
82 * them (write_wait, read_wait) by value, and every ported header that names
83 * a tty field has to see the typedef, not just the tag.
84 */
86
87#define WQ_FLAG_EXCLUSIVE 0x01
88#define WQ_FLAG_WOKEN 0x02
89
90/* Poll budget for wait_event(): long enough that a device settling during
91 * probe is caught, short enough that a condition that will never come cannot
92 * hold the kernel. */
93#define DCL_WAIT_EVENT_MS 5000
94
95static inline void init_waitqueue_head(struct wait_queue_head* wq_head)
96{
97 spin_lock_init(&wq_head->lock);
98 INIT_LIST_HEAD(&wq_head->head);
99}
100
101#define DECLARE_WAIT_QUEUE_HEAD(name) \
102 struct wait_queue_head name = { 0, LIST_HEAD_INIT(name.head) }
103
104#define INIT_WAIT_QUEUE_HEAD(name) init_waitqueue_head(&(name))
105
107 unsigned mode, int flags, void* key);
109 unsigned mode, int flags, void* key);
110
111#define __WAITQUEUE_INITIALIZER(name, tsk) { \
112 .private = (tsk), \
113 .func = default_wake_function, \
114 .entry = LIST_HEAD_INIT((name).entry), \
115}
116
117#define DECLARE_WAITQUEUE(name, tsk) \
118 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
119
120/* n_tty.c:2189 and :2339 -- the form the tty read/write loops use. */
121#define DEFINE_WAIT_FUNC(name, function) \
122 struct wait_queue_entry name = { \
123 .private = current, \
124 .func = (function), \
125 .entry = LIST_HEAD_INIT((name).entry), \
126 }
127
128static inline void init_waitqueue_entry(struct wait_queue_entry* wq_entry,
129 struct task_struct* tsk)
130{
131 wq_entry->flags = 0;
132 wq_entry->private = tsk;
133 wq_entry->func = default_wake_function;
134 INIT_LIST_HEAD(&wq_entry->entry);
135}
136
137static inline void init_waitqueue_func_entry(struct wait_queue_entry* wq_entry,
139{
140 wq_entry->flags = 0;
141 wq_entry->private = 0;
142 wq_entry->func = func;
143 INIT_LIST_HEAD(&wq_entry->entry);
144}
145
146void add_wait_queue(struct wait_queue_head* wq_head,
147 struct wait_queue_entry* wq_entry);
149 struct wait_queue_entry* wq_entry);
150
151/* nr_exclusive == 0 wakes everyone; >0 stops after that many. */
152void __wake_up(struct wait_queue_head* wq_head, unsigned int mode,
153 int nr_exclusive, void* key);
154
155#define wake_up(x) __wake_up((x), TASK_NORMAL, 1, 0)
156#define wake_up_all(x) __wake_up((x), TASK_NORMAL, 0, 0)
157#define wake_up_interruptible(x) __wake_up((x), TASK_INTERRUPTIBLE, 1, 0)
158#define wake_up_interruptible_all(x) __wake_up((x), TASK_INTERRUPTIBLE, 0, 0)
159#define wake_up_interruptible_sync(x) __wake_up((x), TASK_INTERRUPTIBLE, 1, 0)
160#define wake_up_poll(x, m) \
161 __wake_up((x), TASK_NORMAL, 1, (void*)(uintptr_t)(m))
162#define wake_up_interruptible_poll(x, m) \
163 __wake_up((x), TASK_INTERRUPTIBLE, 1, (void*)(uintptr_t)(m))
164#define wake_up_interruptible_sync_poll(x, m) \
165 __wake_up((x), TASK_INTERRUPTIBLE, 1, (void*)(uintptr_t)(m))
166
167/*
168 * Returns the entry's flags; n_tty discards the result (n_tty.c:2407) and
169 * mainline's contract is "waited up to timeout, returns when woken or the
170 * timeout expires". Here the timeout expires immediately, so the caller's
171 * surrounding loop re-tests its own condition on the next pass -- which is
172 * why that loop must be bounded elsewhere (see <linux/sched.h> on TIOCMWAIT,
173 * and stage 5's cut on the two n_tty sites).
174 */
175unsigned int wait_woken(struct wait_queue_entry* wq_entry,
176 unsigned int state, long timeout);
177
178/* Called when a bounded wait_event() gives up, so falling through is visible
179 * in the log instead of looking like a normal boot. */
180void dcl_wait_event_timeout(const char* func);
181
182/*
183 * dcl_wait_pump() -- run pending device completions while the bounded poll
184 * spins. Declared here, called from _dcl_wait_core below, defined in
185 * DCL/virtio_shim.c beside virtio_poll_vqs().
186 *
187 * Why a wait has to pump at all: completions on this port are reaped by
188 * polling -- the cdev read loop, or the caller's own loop -- and a driver's
189 * *probe-time* waits have no such loop above them. virtio_gpu_init()'s
190 * display-info wait is the first time it bites: the reply only moves when
191 * the control queue is polled, nothing else polls before userspace runs,
192 * and without this the condition could never come true inside the wait. The
193 * call is idempotent and cheap (one used->idx read per live queue), so
194 * running it under every wait_event is safe on the same terms the cdev
195 * paths already pump.
196 */
197void dcl_wait_pump(void);
198
199/*
200 * _dcl_wait_core(wq_head, condition) -- the one polling loop every
201 * wait_event* macro below is built from: DCL_WAIT_EVENT_MS passes of
202 * mdelay(1) while the condition is false, dcl_wait_event_timeout() on the
203 * way out, then the condition read one last time. Each pass first runs
204 * dcl_wait_pump() (declared above), so a condition fed by a device
205 * completion can arrive during the wait rather than only being re-tested.
206 *
207 * Spelled once because the four macros that use it differ only in what they
208 * make of an expiry -- nothing, -ERESTARTSYS, 0, or the caller's own
209 * timeout -- which is what each of their comments is about. Four copies of
210 * the loop would let a change to the budget or the tick reach one variant
211 * and not the others, and a caller cannot see that until a port stops
212 * draining.
213 *
214 * Evaluates to 1 when the condition held and 0 when the budget ran out, so
215 * the caller picks its own result instead of re-testing the condition: it is
216 * read once per millisecond and once here, which is the side-effect-free
217 * contract the note on wait_event() describes.
218 */
219#define _dcl_wait_core(wq_head, condition) \
220 ({ \
221 unsigned int _dcl_i = DCL_WAIT_EVENT_MS; \
222 int _dcl_ok; \
223 (void)(wq_head); \
224 while (!(condition) && _dcl_i) { \
225 _dcl_i--; \
226 dcl_wait_pump(); \
227 mdelay(1); \
228 } \
229 _dcl_ok = !!(condition); \
230 if (!_dcl_ok) \
231 dcl_wait_event_timeout(__func__); \
232 _dcl_ok; \
233 })
234
235/*
236 * wait_event(wq, condition) -- poll, bounded.
237 *
238 * The condition is evaluated once per millisecond and once at the end; it
239 * must therefore be side-effect free, which mainline's own wait_event()
240 * requires too (it evaluates it on every wake as well).
241 */
242#define wait_event(wq_head, condition) \
243 do { \
244 (void)_dcl_wait_core(wq_head, condition); \
245 } while (0)
246
247
248/*
249 * wait_event_interruptible(wq, condition) -- tty_ioctl.c:484, which does
250 *
251 * retval = wait_event_interruptible(tty->write_wait, !tty_chars_in_buffer(tty));
252 * if (retval < 0)
253 * return retval;
254 *
255 * so the value is the whole contract: 0 means "the buffer drained", negative
256 * means "give up and unwind", and a 0 on timeout would let the caller proceed
257 * to write with bytes still queued -- the exact thing the wait was for.
258 *
259 * DCL has no signal delivery to make a wait *interruptible* (signal_pending()
260 * is 0, kernel.h), so the failure it can report is the one wait_event() above
261 * already reports: DCL_WAIT_EVENT_MS expired with the condition still false.
262 * That is spelled -ERESTARTSYS because that is the value this call site's
263 * `retval < 0` branch is written against and the value mainline returns when
264 * an interrupt does what a timeout does here. The polling shape -- one
265 * millisecond per pass, a bounded number of them, dcl_wait_event_timeout() on
266 * the way out -- is wait_event()'s, deliberately: two different wait loops in
267 * one header that disagreed about when they gave up would be worse than two
268 * that agreed and differed only in their result type.
269 */
270#define wait_event_interruptible(wq_head, condition) \
271 ({ \
272 _dcl_wait_core(wq_head, condition) \
273 ? 0 : -ERESTARTSYS; \
274 })
275
276/*
277 * wait_event_interruptible_timeout(wq, condition, timeout) --
278 * tty_ioctl.c:184 in tty_wait_until_sent(), and its result is read three
279 * different ways on the lines after:
280 *
281 * timeout = wait_event_interruptible_timeout(tty->write_wait,
282 * !tty_chars_in_buffer(tty), timeout);
283 * if (timeout <= 0) return; -- gave up
284 * if (timeout == MAX_SCHEDULE_TIMEOUT)
285 * timeout = 0; -- the caller passed "wait forever"
286 *
287 * Hence returning the *original* timeout on success rather than a remaining
288 * one. mainline returns jiffies left, and here that would matter only if a
289 * caller compared for equality -- which this one does, against
290 * MAX_SCHEDULE_TIMEOUT, where the true remaining time is so close to the
291 * original that mainline's own arithmetic lands on the same value. Returning
292 * the input preserves that equality for the infinite case, and for the finite
293 * case the caller only tests `<= 0`, so an optimistic remainder cannot be
294 * observed. 0 is the timeout, and -ERESTARTSYS never happens: there is no
295 * signal, so the two exits are "condition met" and "bounded wait expired".
296 *
297 * The `timeout` argument is in jiffies on mainline; DCL has no jiffy clock on
298 * this path and mdelay() is what wait_event() already spends, so the bounded
299 * wait is DCL_WAIT_EVENT_MS like every other one here, and timeout's value
300 * only ever travels through to the caller's own comparison.
301 */
302#define wait_event_interruptible_timeout(wq_head, condition, timeout) \
303 ({ \
304 long _dcl_t = (timeout); \
305 _dcl_wait_core(wq_head, condition) \
306 ? _dcl_t : 0; \
307 })
308
309/*
310 * wait_event_timeout(wq_head, condition, timeout) -- the two drm vblank
311 * waits, drm_atomic_helper.c:1916 and drm_vblank.c:1325.
312 *
313 * Same bounded poll and the same result convention as
314 * wait_event_interruptible_timeout() above: the caller's timeout back when
315 * the condition came true, 0 when the budget expired first. Both call
316 * sites test only `!ret` / `ret == 0` -- drm_crtc_wait_one_vblank() maps the
317 * 0 onto -ETIMEDOUT -- so no remainder is ever read. mainline floors a
318 * success at 1 (`timeout ?: 1`); returning the input unchanged is what the
319 * sibling macro above chose, so this one follows it rather than giving two
320 * answers for one argument in the same header.
321 */
322#define wait_event_timeout(wq_head, condition, timeout) \
323 ({ \
324 long _dcl_t = (timeout); \
325 _dcl_wait_core(wq_head, condition) \
326 ? _dcl_t : 0; \
327 })
328
329/*
330 * wait_event_lock_irq(wq_head, condition, lock) -- drm_vblank_work.c:229
331 * and :250, which hold dev->event_lock across the wait and pass it to the
332 * macro so mainline's __wait_event_lock_irq() can drop it around each
333 * schedule() and take it again on wake. DCL's spinlocks are no-ops with
334 * nothing to drop (see <linux/spinlock.h>), so `lock` is unused; what
335 * remains is wait_event()'s bounded poll. The contract mainline states --
336 * the lock is held on entry and still held on return -- is then trivially
337 * kept, because the lock was never actually released.
338 */
339#define wait_event_lock_irq(wq_head, condition, lock) \
340 do { \
341 (void)(lock); \
342 (void)_dcl_wait_core(wq_head, condition); \
343 } while (0)
344
345
346/*
347 * DEFINE_WAIT(name) -- mainline's spelling for the waiter declared *inside*
348 * the function that waits, as opposed to DECLARE_WAITQUEUE's (which takes the
349 * task as an argument). One user so far: tty_port.c:504, the `wait` that
350 * tty_port_block_til_ready() brackets its whole loop with.
351 *
352 * The initialiser is __WAITQUEUE_INITIALIZER with `current` filled in, which
353 * is the entire difference from DECLARE_WAITQUEUE -- and it is safe to write
354 * as a static initialiser only because the declaration is a local one: a
355 * block-scope struct may be initialised with a non-constant expression, and
356 * `current` is dcl_current(), a call. The same macro used at file scope
357 * would not compile; nothing does.
358 *
359 * mainline: include/linux/wait.h.
360 */
361#define DEFINE_WAIT(name) \
362 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, current)
363
364/*
365 * prepare_to_wait() / finish_wait() -- the two calls that bracket a wait
366 * loop, at tty_port.c:542 and :569.
367 *
368 * The queue work is the part that has to be real, and it is: the entry goes
369 * on the queue only if it is not already there, which is what mainline does
370 * and what stops a loop that re-prepares each pass from queueing itself a
371 * second time -- and it comes off in finish_wait(), which is what stops a
372 * later wake_up_interruptible() from walking an entry whose stack frame has
373 * already been returned through. Both reuse the add/remove this header
374 * already declares (bodies in DCL/linux_irq_shim.c) rather than open-coding
375 * list operations, so there is one answer for where on the queue an entry
376 * goes instead of two that could disagree.
377 *
378 * The state work is the no-op set_current_state from <linux/sched.h>, and
379 * that is the same bargain wait_event() above strikes: there is no task state
380 * to change because no task blocks here. The queue is genuine so the wake-up
381 * is genuine; the *waiting* is the bounded poll the caller does after this
382 * returns. What would be wrong is an entry left queued and a caller that
383 * believed it had slept.
384 *
385 * mainline: kernel/sched/wait.c.
386 */
387static inline void prepare_to_wait(struct wait_queue_head* wq_head,
388 struct wait_queue_entry* wq_entry,
389 int state)
390{
391 if (list_empty(&wq_entry->entry))
392 add_wait_queue(wq_head, wq_entry);
394}
395
396static inline void finish_wait(struct wait_queue_head* wq_head,
397 struct wait_queue_entry* wq_entry)
398{
399 if (!list_empty(&wq_entry->entry))
400 remove_wait_queue(wq_head, wq_entry);
402}
403
404/*
405 * wait_event_freezable(wq_head, condition) -- the freezer-aware wait.
406 *
407 * virtio_console.c:752 and :784 both write it, inside wait_port_readable()
408 * and wait_port_writable(), where mainline's version additionally lets a
409 * suspend in progress abort the wait with -ERESTARTSYS.
410 *
411 * DCL has no refrigerator: nothing freezes a task, so the half of mainline's
412 * contract that responds to freezing has nothing to respond *to*. The other
413 * half -- poll the condition, give up after a bounded budget -- is what
414 * wait_event() above already does, and this is that loop and nothing else,
415 * deliberately: two waits in one header that gave up at different times would
416 * be a difference a caller could not see until a port stopped draining.
417 *
418 * The shape (one millisecond per pass, DCL_WAIT_EVENT_MS of them,
419 * dcl_wait_event_timeout() on the way out) is wait_event()'s, so a reader who
420 * knows one of them knows both.
421 *
422 * It returns an int, which is the part the previous note here got wrong when
423 * it said "returns nothing, because mainline's does not either" -- mainline's
424 * __wait_event_freezable() does return one, and both call sites treat the
425 * value as the whole contract:
426 *
427 * ret = wait_event_freezable(port->waitqueue, !will_read_block(port));
428 * if (ret < 0)
429 * return ret;
430 *
431 * A `do {} while (0)` there is not a value to assign, so the call would not
432 * compile at all, which is how the mistake surfaced. The two arms are the
433 * ones wait_event_interruptible() above already chose and for the same
434 * reason: 0 when the condition came true, -ERESTARTSYS when the bounded
435 * budget expired first. A 0 on the second arm would walk past the wait with
436 * the port still empty and hand read() a success with nothing in it; the
437 * callers' `ret < 0` branch is written for exactly the case that produces.
438 */
439#define wait_event_freezable(wq_head, condition) \
440 ({ \
441 _dcl_wait_core(wq_head, condition) \
442 ? 0 : -ERESTARTSYS; \
443 })
444#endif /* __LINUX_WAIT_H__ */
uint8_t flags
Definition virtio.h:0
#define spin_lock_init(l)
Definition kernel.h:225
int spinlock_t
Definition kernel.h:44
#define set_current_state(s)
Definition sched.h:241
#define TASK_RUNNING
Definition sched.h:84
#define __set_current_state(s)
Definition sched.h:242
int const struct list_head const struct list_head struct list_head * head
Definition list_sort.h:13
Definition list.h:11
Definition sched.h:121
Definition wait.h:68
wait_queue_func_t func
Definition wait.h:71
unsigned int flags
Definition wait.h:69
void * private
Definition wait.h:70
struct list_head entry
Definition wait.h:72
Definition wait.h:75
spinlock_t lock
Definition wait.h:76
struct list_head head
Definition wait.h:77
#define state(x)
#define mode(x)
void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr_exclusive, void *key)
unsigned int wait_woken(struct wait_queue_entry *wq_entry, unsigned int state, long timeout)
void dcl_wait_pump(void)
void dcl_wait_event_timeout(const char *func)
void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
int default_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key)
int(* wait_queue_func_t)(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key)
Definition wait.h:65
int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key)