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