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