1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic waiting primitives.
4 *
5 * (C) 2004 Nadia Yvette Chambers, Oracle
6 */
7 #include "sched.h"
8 #include <trace/hooks/sched.h>
9
__init_waitqueue_head(struct wait_queue_head * wq_head,const char * name,struct lock_class_key * key)10 void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *key)
11 {
12 spin_lock_init(&wq_head->lock);
13 lockdep_set_class_and_name(&wq_head->lock, key, name);
14 INIT_LIST_HEAD(&wq_head->head);
15 }
16
17 EXPORT_SYMBOL(__init_waitqueue_head);
18
add_wait_queue(struct wait_queue_head * wq_head,struct wait_queue_entry * wq_entry)19 void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
20 {
21 unsigned long flags;
22
23 wq_entry->flags &= ~WQ_FLAG_EXCLUSIVE;
24 spin_lock_irqsave(&wq_head->lock, flags);
25 __add_wait_queue(wq_head, wq_entry);
26 spin_unlock_irqrestore(&wq_head->lock, flags);
27 }
28 EXPORT_SYMBOL(add_wait_queue);
29
add_wait_queue_exclusive(struct wait_queue_head * wq_head,struct wait_queue_entry * wq_entry)30 void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
31 {
32 unsigned long flags;
33
34 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
35 spin_lock_irqsave(&wq_head->lock, flags);
36 __add_wait_queue_entry_tail(wq_head, wq_entry);
37 spin_unlock_irqrestore(&wq_head->lock, flags);
38 }
39 EXPORT_SYMBOL(add_wait_queue_exclusive);
40
add_wait_queue_priority(struct wait_queue_head * wq_head,struct wait_queue_entry * wq_entry)41 void add_wait_queue_priority(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
42 {
43 unsigned long flags;
44
45 wq_entry->flags |= WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY;
46 spin_lock_irqsave(&wq_head->lock, flags);
47 __add_wait_queue(wq_head, wq_entry);
48 spin_unlock_irqrestore(&wq_head->lock, flags);
49 }
50 EXPORT_SYMBOL_GPL(add_wait_queue_priority);
51
remove_wait_queue(struct wait_queue_head * wq_head,struct wait_queue_entry * wq_entry)52 void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
53 {
54 unsigned long flags;
55
56 spin_lock_irqsave(&wq_head->lock, flags);
57 __remove_wait_queue(wq_head, wq_entry);
58 spin_unlock_irqrestore(&wq_head->lock, flags);
59 }
60 EXPORT_SYMBOL(remove_wait_queue);
61
62 /*
63 * Scan threshold to break wait queue walk.
64 * This allows a waker to take a break from holding the
65 * wait queue lock during the wait queue walk.
66 */
67 #define WAITQUEUE_WALK_BREAK_CNT 64
68
69 /*
70 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
71 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
72 * number) then we wake that number of exclusive tasks, and potentially all
73 * the non-exclusive tasks. Normally, exclusive tasks will be at the end of
74 * the list and any non-exclusive tasks will be woken first. A priority task
75 * may be at the head of the list, and can consume the event without any other
76 * tasks being woken.
77 *
78 * There are circumstances in which we can try to wake a task which has already
79 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
80 * zero in this (rare) case, and we handle it by continuing to scan the queue.
81 */
__wake_up_common(struct wait_queue_head * wq_head,unsigned int mode,int nr_exclusive,int wake_flags,void * key,wait_queue_entry_t * bookmark)82 static int __wake_up_common(struct wait_queue_head *wq_head, unsigned int mode,
83 int nr_exclusive, int wake_flags, void *key,
84 wait_queue_entry_t *bookmark)
85 {
86 wait_queue_entry_t *curr, *next;
87 int cnt = 0;
88
89 lockdep_assert_held(&wq_head->lock);
90
91 if (bookmark && (bookmark->flags & WQ_FLAG_BOOKMARK)) {
92 curr = list_next_entry(bookmark, entry);
93
94 list_del(&bookmark->entry);
95 bookmark->flags = 0;
96 } else
97 curr = list_first_entry(&wq_head->head, wait_queue_entry_t, entry);
98
99 if (&curr->entry == &wq_head->head)
100 return nr_exclusive;
101
102 list_for_each_entry_safe_from(curr, next, &wq_head->head, entry) {
103 unsigned flags = curr->flags;
104 int ret;
105
106 if (flags & WQ_FLAG_BOOKMARK)
107 continue;
108
109 ret = curr->func(curr, mode, wake_flags, key);
110 if (ret < 0)
111 break;
112 if (ret && (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
113 break;
114
115 if (bookmark && (++cnt > WAITQUEUE_WALK_BREAK_CNT) &&
116 (&next->entry != &wq_head->head)) {
117 bookmark->flags = WQ_FLAG_BOOKMARK;
118 list_add_tail(&bookmark->entry, &next->entry);
119 break;
120 }
121 }
122
123 return nr_exclusive;
124 }
125
__wake_up_common_lock(struct wait_queue_head * wq_head,unsigned int mode,int nr_exclusive,int wake_flags,void * key)126 static void __wake_up_common_lock(struct wait_queue_head *wq_head, unsigned int mode,
127 int nr_exclusive, int wake_flags, void *key)
128 {
129 unsigned long flags;
130 wait_queue_entry_t bookmark;
131
132 bookmark.flags = 0;
133 bookmark.private = NULL;
134 bookmark.func = NULL;
135 INIT_LIST_HEAD(&bookmark.entry);
136
137 do {
138 spin_lock_irqsave(&wq_head->lock, flags);
139 nr_exclusive = __wake_up_common(wq_head, mode, nr_exclusive,
140 wake_flags, key, &bookmark);
141 spin_unlock_irqrestore(&wq_head->lock, flags);
142 } while (bookmark.flags & WQ_FLAG_BOOKMARK);
143 }
144
145 /**
146 * __wake_up - wake up threads blocked on a waitqueue.
147 * @wq_head: the waitqueue
148 * @mode: which threads
149 * @nr_exclusive: how many wake-one or wake-many threads to wake up
150 * @key: is directly passed to the wakeup function
151 *
152 * If this function wakes up a task, it executes a full memory barrier before
153 * accessing the task state.
154 */
__wake_up(struct wait_queue_head * wq_head,unsigned int mode,int nr_exclusive,void * key)155 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode,
156 int nr_exclusive, void *key)
157 {
158 __wake_up_common_lock(wq_head, mode, nr_exclusive, 0, key);
159 }
160 EXPORT_SYMBOL(__wake_up);
161
162 /*
163 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
164 */
__wake_up_locked(struct wait_queue_head * wq_head,unsigned int mode,int nr)165 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr)
166 {
167 __wake_up_common(wq_head, mode, nr, 0, NULL, NULL);
168 }
169 EXPORT_SYMBOL_GPL(__wake_up_locked);
170
__wake_up_locked_key(struct wait_queue_head * wq_head,unsigned int mode,void * key)171 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key)
172 {
173 __wake_up_common(wq_head, mode, 1, 0, key, NULL);
174 }
175 EXPORT_SYMBOL_GPL(__wake_up_locked_key);
176
__wake_up_locked_key_bookmark(struct wait_queue_head * wq_head,unsigned int mode,void * key,wait_queue_entry_t * bookmark)177 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
178 unsigned int mode, void *key, wait_queue_entry_t *bookmark)
179 {
180 __wake_up_common(wq_head, mode, 1, 0, key, bookmark);
181 }
182 EXPORT_SYMBOL_GPL(__wake_up_locked_key_bookmark);
183
184 /**
185 * __wake_up_sync_key - wake up threads blocked on a waitqueue.
186 * @wq_head: the waitqueue
187 * @mode: which threads
188 * @key: opaque value to be passed to wakeup targets
189 *
190 * The sync wakeup differs that the waker knows that it will schedule
191 * away soon, so while the target thread will be woken up, it will not
192 * be migrated to another CPU - ie. the two threads are 'synchronized'
193 * with each other. This can prevent needless bouncing between CPUs.
194 *
195 * On UP it can prevent extra preemption.
196 *
197 * If this function wakes up a task, it executes a full memory barrier before
198 * accessing the task state.
199 */
__wake_up_sync_key(struct wait_queue_head * wq_head,unsigned int mode,void * key)200 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode,
201 void *key)
202 {
203 int wake_flags = WF_SYNC;
204
205 if (unlikely(!wq_head))
206 return;
207
208 trace_android_vh_set_wake_flags(&wake_flags, &mode);
209 __wake_up_common_lock(wq_head, mode, 1, wake_flags, key);
210 }
211 EXPORT_SYMBOL_GPL(__wake_up_sync_key);
212
213 /**
214 * __wake_up_locked_sync_key - wake up a thread blocked on a locked waitqueue.
215 * @wq_head: the waitqueue
216 * @mode: which threads
217 * @key: opaque value to be passed to wakeup targets
218 *
219 * The sync wakeup differs in that the waker knows that it will schedule
220 * away soon, so while the target thread will be woken up, it will not
221 * be migrated to another CPU - ie. the two threads are 'synchronized'
222 * with each other. This can prevent needless bouncing between CPUs.
223 *
224 * On UP it can prevent extra preemption.
225 *
226 * If this function wakes up a task, it executes a full memory barrier before
227 * accessing the task state.
228 */
__wake_up_locked_sync_key(struct wait_queue_head * wq_head,unsigned int mode,void * key)229 void __wake_up_locked_sync_key(struct wait_queue_head *wq_head,
230 unsigned int mode, void *key)
231 {
232 __wake_up_common(wq_head, mode, 1, WF_SYNC, key, NULL);
233 }
234 EXPORT_SYMBOL_GPL(__wake_up_locked_sync_key);
235
236 /*
237 * __wake_up_sync - see __wake_up_sync_key()
238 */
__wake_up_sync(struct wait_queue_head * wq_head,unsigned int mode)239 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode)
240 {
241 __wake_up_sync_key(wq_head, mode, NULL);
242 }
243 EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
244
__wake_up_pollfree(struct wait_queue_head * wq_head)245 void __wake_up_pollfree(struct wait_queue_head *wq_head)
246 {
247 __wake_up(wq_head, TASK_NORMAL, 0, poll_to_key(EPOLLHUP | POLLFREE));
248 /* POLLFREE must have cleared the queue. */
249 WARN_ON_ONCE(waitqueue_active(wq_head));
250 }
251
252 /*
253 * Note: we use "set_current_state()" _after_ the wait-queue add,
254 * because we need a memory barrier there on SMP, so that any
255 * wake-function that tests for the wait-queue being active
256 * will be guaranteed to see waitqueue addition _or_ subsequent
257 * tests in this thread will see the wakeup having taken place.
258 *
259 * The spin_unlock() itself is semi-permeable and only protects
260 * one way (it only protects stuff inside the critical region and
261 * stops them from bleeding out - it would still allow subsequent
262 * loads to move into the critical region).
263 */
264 void
prepare_to_wait(struct wait_queue_head * wq_head,struct wait_queue_entry * wq_entry,int state)265 prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
266 {
267 unsigned long flags;
268
269 wq_entry->flags &= ~WQ_FLAG_EXCLUSIVE;
270 spin_lock_irqsave(&wq_head->lock, flags);
271 if (list_empty(&wq_entry->entry))
272 __add_wait_queue(wq_head, wq_entry);
273 set_current_state(state);
274 spin_unlock_irqrestore(&wq_head->lock, flags);
275 }
276 EXPORT_SYMBOL(prepare_to_wait);
277
278 /* Returns true if we are the first waiter in the queue, false otherwise. */
279 bool
prepare_to_wait_exclusive(struct wait_queue_head * wq_head,struct wait_queue_entry * wq_entry,int state)280 prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
281 {
282 unsigned long flags;
283 bool was_empty = false;
284
285 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
286 spin_lock_irqsave(&wq_head->lock, flags);
287 if (list_empty(&wq_entry->entry)) {
288 was_empty = list_empty(&wq_head->head);
289 __add_wait_queue_entry_tail(wq_head, wq_entry);
290 }
291 set_current_state(state);
292 spin_unlock_irqrestore(&wq_head->lock, flags);
293 return was_empty;
294 }
295 EXPORT_SYMBOL(prepare_to_wait_exclusive);
296
init_wait_entry(struct wait_queue_entry * wq_entry,int flags)297 void init_wait_entry(struct wait_queue_entry *wq_entry, int flags)
298 {
299 wq_entry->flags = flags;
300 wq_entry->private = current;
301 wq_entry->func = autoremove_wake_function;
302 INIT_LIST_HEAD(&wq_entry->entry);
303 }
304 EXPORT_SYMBOL(init_wait_entry);
305
prepare_to_wait_event(struct wait_queue_head * wq_head,struct wait_queue_entry * wq_entry,int state)306 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
307 {
308 unsigned long flags;
309 long ret = 0;
310
311 spin_lock_irqsave(&wq_head->lock, flags);
312 if (signal_pending_state(state, current)) {
313 /*
314 * Exclusive waiter must not fail if it was selected by wakeup,
315 * it should "consume" the condition we were waiting for.
316 *
317 * The caller will recheck the condition and return success if
318 * we were already woken up, we can not miss the event because
319 * wakeup locks/unlocks the same wq_head->lock.
320 *
321 * But we need to ensure that set-condition + wakeup after that
322 * can't see us, it should wake up another exclusive waiter if
323 * we fail.
324 */
325 list_del_init(&wq_entry->entry);
326 ret = -ERESTARTSYS;
327 } else {
328 if (list_empty(&wq_entry->entry)) {
329 if (wq_entry->flags & WQ_FLAG_EXCLUSIVE)
330 __add_wait_queue_entry_tail(wq_head, wq_entry);
331 else
332 __add_wait_queue(wq_head, wq_entry);
333 }
334 set_current_state(state);
335 }
336 spin_unlock_irqrestore(&wq_head->lock, flags);
337
338 return ret;
339 }
340 EXPORT_SYMBOL(prepare_to_wait_event);
341
342 /*
343 * Note! These two wait functions are entered with the
344 * wait-queue lock held (and interrupts off in the _irq
345 * case), so there is no race with testing the wakeup
346 * condition in the caller before they add the wait
347 * entry to the wake queue.
348 */
do_wait_intr(wait_queue_head_t * wq,wait_queue_entry_t * wait)349 int do_wait_intr(wait_queue_head_t *wq, wait_queue_entry_t *wait)
350 {
351 if (likely(list_empty(&wait->entry)))
352 __add_wait_queue_entry_tail(wq, wait);
353
354 set_current_state(TASK_INTERRUPTIBLE);
355 if (signal_pending(current))
356 return -ERESTARTSYS;
357
358 spin_unlock(&wq->lock);
359 schedule();
360 spin_lock(&wq->lock);
361
362 return 0;
363 }
364 EXPORT_SYMBOL(do_wait_intr);
365
do_wait_intr_irq(wait_queue_head_t * wq,wait_queue_entry_t * wait)366 int do_wait_intr_irq(wait_queue_head_t *wq, wait_queue_entry_t *wait)
367 {
368 if (likely(list_empty(&wait->entry)))
369 __add_wait_queue_entry_tail(wq, wait);
370
371 set_current_state(TASK_INTERRUPTIBLE);
372 if (signal_pending(current))
373 return -ERESTARTSYS;
374
375 spin_unlock_irq(&wq->lock);
376 schedule();
377 spin_lock_irq(&wq->lock);
378
379 return 0;
380 }
381 EXPORT_SYMBOL(do_wait_intr_irq);
382
383 /**
384 * finish_wait - clean up after waiting in a queue
385 * @wq_head: waitqueue waited on
386 * @wq_entry: wait descriptor
387 *
388 * Sets current thread back to running state and removes
389 * the wait descriptor from the given waitqueue if still
390 * queued.
391 */
finish_wait(struct wait_queue_head * wq_head,struct wait_queue_entry * wq_entry)392 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
393 {
394 unsigned long flags;
395
396 __set_current_state(TASK_RUNNING);
397 /*
398 * We can check for list emptiness outside the lock
399 * IFF:
400 * - we use the "careful" check that verifies both
401 * the next and prev pointers, so that there cannot
402 * be any half-pending updates in progress on other
403 * CPU's that we haven't seen yet (and that might
404 * still change the stack area.
405 * and
406 * - all other users take the lock (ie we can only
407 * have _one_ other CPU that looks at or modifies
408 * the list).
409 */
410 if (!list_empty_careful(&wq_entry->entry)) {
411 spin_lock_irqsave(&wq_head->lock, flags);
412 list_del_init(&wq_entry->entry);
413 spin_unlock_irqrestore(&wq_head->lock, flags);
414 }
415 }
416 EXPORT_SYMBOL(finish_wait);
417
autoremove_wake_function(struct wait_queue_entry * wq_entry,unsigned int mode,int sync,void * key)418 __sched int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
419 int sync, void *key)
420 {
421 int ret = default_wake_function(wq_entry, mode, sync, key);
422
423 if (ret)
424 list_del_init_careful(&wq_entry->entry);
425
426 return ret;
427 }
428 EXPORT_SYMBOL(autoremove_wake_function);
429
is_kthread_should_stop_or_park(void)430 static inline bool is_kthread_should_stop_or_park(void)
431 {
432 return (current->flags & PF_KTHREAD) && (kthread_should_stop() || kthread_should_park());
433 }
434
435 /*
436 * DEFINE_WAIT_FUNC(wait, woken_wake_func);
437 *
438 * add_wait_queue(&wq_head, &wait);
439 * for (;;) {
440 * if (condition)
441 * break;
442 *
443 * // in wait_woken() // in woken_wake_function()
444 *
445 * p->state = mode; wq_entry->flags |= WQ_FLAG_WOKEN;
446 * smp_mb(); // A try_to_wake_up():
447 * if (!(wq_entry->flags & WQ_FLAG_WOKEN)) <full barrier>
448 * schedule() if (p->state & mode)
449 * p->state = TASK_RUNNING; p->state = TASK_RUNNING;
450 * wq_entry->flags &= ~WQ_FLAG_WOKEN; ~~~~~~~~~~~~~~~~~~
451 * smp_mb(); // B condition = true;
452 * } smp_mb(); // C
453 * remove_wait_queue(&wq_head, &wait); wq_entry->flags |= WQ_FLAG_WOKEN;
454 */
wait_woken(struct wait_queue_entry * wq_entry,unsigned int mode,long timeout)455 __sched long wait_woken(struct wait_queue_entry *wq_entry, unsigned int mode, long timeout)
456 {
457 /*
458 * The below executes an smp_mb(), which matches with the full barrier
459 * executed by the try_to_wake_up() in woken_wake_function() such that
460 * either we see the store to wq_entry->flags in woken_wake_function()
461 * or woken_wake_function() sees our store to current->state.
462 */
463 set_current_state(mode); /* A */
464 if (!(wq_entry->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop_or_park())
465 timeout = schedule_timeout(timeout);
466 __set_current_state(TASK_RUNNING);
467
468 /*
469 * The below executes an smp_mb(), which matches with the smp_mb() (C)
470 * in woken_wake_function() such that either we see the wait condition
471 * being true or the store to wq_entry->flags in woken_wake_function()
472 * follows ours in the coherence order.
473 */
474 smp_store_mb(wq_entry->flags, wq_entry->flags & ~WQ_FLAG_WOKEN); /* B */
475
476 return timeout;
477 }
478 EXPORT_SYMBOL(wait_woken);
479
woken_wake_function(struct wait_queue_entry * wq_entry,unsigned int mode,int sync,void * key)480 __sched int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned int mode,
481 int sync, void *key)
482 {
483 /* Pairs with the smp_store_mb() in wait_woken(). */
484 smp_mb(); /* C */
485 wq_entry->flags |= WQ_FLAG_WOKEN;
486
487 return default_wake_function(wq_entry, mode, sync, key);
488 }
489 EXPORT_SYMBOL(woken_wake_function);
490