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