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