1
2
3
4
5
6#include <linux/init.h>
7#include <linux/export.h>
8#include <linux/sched.h>
9#include <linux/mm.h>
10#include <linux/wait.h>
11#include <linux/hash.h>
12#include <linux/kthread.h>
13
14void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *key)
15{
16 spin_lock_init(&q->lock);
17 lockdep_set_class_and_name(&q->lock, key, name);
18 INIT_LIST_HEAD(&q->task_list);
19}
20
21EXPORT_SYMBOL(__init_waitqueue_head);
22
23void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
24{
25 unsigned long flags;
26
27 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
28 spin_lock_irqsave(&q->lock, flags);
29 __add_wait_queue(q, wait);
30 spin_unlock_irqrestore(&q->lock, flags);
31}
32EXPORT_SYMBOL(add_wait_queue);
33
34void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
35{
36 unsigned long flags;
37
38 wait->flags |= WQ_FLAG_EXCLUSIVE;
39 spin_lock_irqsave(&q->lock, flags);
40 __add_wait_queue_tail(q, wait);
41 spin_unlock_irqrestore(&q->lock, flags);
42}
43EXPORT_SYMBOL(add_wait_queue_exclusive);
44
45void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
46{
47 unsigned long flags;
48
49 spin_lock_irqsave(&q->lock, flags);
50 __remove_wait_queue(q, wait);
51 spin_unlock_irqrestore(&q->lock, flags);
52}
53EXPORT_SYMBOL(remove_wait_queue);
54
55
56
57
58
59
60
61
62
63
64
65static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
66 int nr_exclusive, int wake_flags, void *key)
67{
68 wait_queue_t *curr, *next;
69
70 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
71 unsigned flags = curr->flags;
72
73 if (curr->func(curr, mode, wake_flags, key) &&
74 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
75 break;
76 }
77}
78
79
80
81
82
83
84
85
86
87
88
89void __wake_up(wait_queue_head_t *q, unsigned int mode,
90 int nr_exclusive, void *key)
91{
92 unsigned long flags;
93
94 spin_lock_irqsave(&q->lock, flags);
95 __wake_up_common(q, mode, nr_exclusive, 0, key);
96 spin_unlock_irqrestore(&q->lock, flags);
97}
98EXPORT_SYMBOL(__wake_up);
99
100
101
102
103void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr)
104{
105 __wake_up_common(q, mode, nr, 0, NULL);
106}
107EXPORT_SYMBOL_GPL(__wake_up_locked);
108
109void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
110{
111 __wake_up_common(q, mode, 1, 0, key);
112}
113EXPORT_SYMBOL_GPL(__wake_up_locked_key);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
133 int nr_exclusive, void *key)
134{
135 unsigned long flags;
136 int wake_flags = 1;
137
138 if (unlikely(!q))
139 return;
140
141 if (unlikely(nr_exclusive != 1))
142 wake_flags = 0;
143
144 spin_lock_irqsave(&q->lock, flags);
145 __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
146 spin_unlock_irqrestore(&q->lock, flags);
147}
148EXPORT_SYMBOL_GPL(__wake_up_sync_key);
149
150
151
152
153void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
154{
155 __wake_up_sync_key(q, mode, nr_exclusive, NULL);
156}
157EXPORT_SYMBOL_GPL(__wake_up_sync);
158
159
160
161
162
163
164
165
166
167
168
169
170
171void
172prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
173{
174 unsigned long flags;
175
176 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
177 spin_lock_irqsave(&q->lock, flags);
178 if (list_empty(&wait->task_list))
179 __add_wait_queue(q, wait);
180 set_current_state(state);
181 spin_unlock_irqrestore(&q->lock, flags);
182}
183EXPORT_SYMBOL(prepare_to_wait);
184
185void
186prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
187{
188 unsigned long flags;
189
190 wait->flags |= WQ_FLAG_EXCLUSIVE;
191 spin_lock_irqsave(&q->lock, flags);
192 if (list_empty(&wait->task_list))
193 __add_wait_queue_tail(q, wait);
194 set_current_state(state);
195 spin_unlock_irqrestore(&q->lock, flags);
196}
197EXPORT_SYMBOL(prepare_to_wait_exclusive);
198
199void init_wait_entry(wait_queue_t *wait, int flags)
200{
201 wait->flags = flags;
202 wait->private = current;
203 wait->func = autoremove_wake_function;
204 INIT_LIST_HEAD(&wait->task_list);
205}
206EXPORT_SYMBOL(init_wait_entry);
207
208long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state)
209{
210 unsigned long flags;
211 long ret = 0;
212
213 spin_lock_irqsave(&q->lock, flags);
214 if (unlikely(signal_pending_state(state, current))) {
215
216
217
218
219
220
221
222
223
224
225
226
227 list_del_init(&wait->task_list);
228 ret = -ERESTARTSYS;
229 } else {
230 if (list_empty(&wait->task_list)) {
231 if (wait->flags & WQ_FLAG_EXCLUSIVE)
232 __add_wait_queue_tail(q, wait);
233 else
234 __add_wait_queue(q, wait);
235 }
236 set_current_state(state);
237 }
238 spin_unlock_irqrestore(&q->lock, flags);
239
240 return ret;
241}
242EXPORT_SYMBOL(prepare_to_wait_event);
243
244
245
246
247
248
249
250
251
252
253void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
254{
255 unsigned long flags;
256
257 __set_current_state(TASK_RUNNING);
258
259
260
261
262
263
264
265
266
267
268
269
270
271 if (!list_empty_careful(&wait->task_list)) {
272 spin_lock_irqsave(&q->lock, flags);
273 list_del_init(&wait->task_list);
274 spin_unlock_irqrestore(&q->lock, flags);
275 }
276}
277EXPORT_SYMBOL(finish_wait);
278
279int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
280{
281 int ret = default_wake_function(wait, mode, sync, key);
282
283 if (ret)
284 list_del_init(&wait->task_list);
285 return ret;
286}
287EXPORT_SYMBOL(autoremove_wake_function);
288
289static inline bool is_kthread_should_stop(void)
290{
291 return (current->flags & PF_KTHREAD) && kthread_should_stop();
292}
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314long wait_woken(wait_queue_t *wait, unsigned mode, long timeout)
315{
316 set_current_state(mode);
317
318
319
320
321
322 if (!(wait->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
323 timeout = schedule_timeout(timeout);
324 __set_current_state(TASK_RUNNING);
325
326
327
328
329
330
331
332 smp_store_mb(wait->flags, wait->flags & ~WQ_FLAG_WOKEN);
333
334 return timeout;
335}
336EXPORT_SYMBOL(wait_woken);
337
338int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
339{
340
341
342
343
344
345
346
347 smp_wmb();
348 wait->flags |= WQ_FLAG_WOKEN;
349
350 return default_wake_function(wait, mode, sync, key);
351}
352EXPORT_SYMBOL(woken_wake_function);
353
354int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
355{
356 struct wait_bit_key *key = arg;
357 struct wait_bit_queue *wait_bit
358 = container_of(wait, struct wait_bit_queue, wait);
359
360 if (wait_bit->key.flags != key->flags ||
361 wait_bit->key.bit_nr != key->bit_nr ||
362 test_bit(key->bit_nr, key->flags))
363 return 0;
364 else
365 return autoremove_wake_function(wait, mode, sync, key);
366}
367EXPORT_SYMBOL(wake_bit_function);
368
369
370
371
372
373
374int __sched
375__wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
376 wait_bit_action_f *action, unsigned mode)
377{
378 int ret = 0;
379
380 do {
381 prepare_to_wait(wq, &q->wait, mode);
382 if (test_bit(q->key.bit_nr, q->key.flags))
383 ret = (*action)(&q->key, mode);
384 } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
385 finish_wait(wq, &q->wait);
386 return ret;
387}
388EXPORT_SYMBOL(__wait_on_bit);
389
390int __sched out_of_line_wait_on_bit(void *word, int bit,
391 wait_bit_action_f *action, unsigned mode)
392{
393 wait_queue_head_t *wq = bit_waitqueue(word, bit);
394 DEFINE_WAIT_BIT(wait, word, bit);
395
396 return __wait_on_bit(wq, &wait, action, mode);
397}
398EXPORT_SYMBOL(out_of_line_wait_on_bit);
399
400int __sched out_of_line_wait_on_bit_timeout(
401 void *word, int bit, wait_bit_action_f *action,
402 unsigned mode, unsigned long timeout)
403{
404 wait_queue_head_t *wq = bit_waitqueue(word, bit);
405 DEFINE_WAIT_BIT(wait, word, bit);
406
407 wait.key.timeout = jiffies + timeout;
408 return __wait_on_bit(wq, &wait, action, mode);
409}
410EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
411
412int __sched
413__wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
414 wait_bit_action_f *action, unsigned mode)
415{
416 int ret = 0;
417
418 for (;;) {
419 prepare_to_wait_exclusive(wq, &q->wait, mode);
420 if (test_bit(q->key.bit_nr, q->key.flags)) {
421 ret = action(&q->key, mode);
422
423
424
425
426
427
428 if (ret)
429 finish_wait(wq, &q->wait);
430 }
431 if (!test_and_set_bit(q->key.bit_nr, q->key.flags)) {
432 if (!ret)
433 finish_wait(wq, &q->wait);
434 return 0;
435 } else if (ret) {
436 return ret;
437 }
438 }
439}
440EXPORT_SYMBOL(__wait_on_bit_lock);
441
442int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
443 wait_bit_action_f *action, unsigned mode)
444{
445 wait_queue_head_t *wq = bit_waitqueue(word, bit);
446 DEFINE_WAIT_BIT(wait, word, bit);
447
448 return __wait_on_bit_lock(wq, &wait, action, mode);
449}
450EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
451
452void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
453{
454 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
455 if (waitqueue_active(wq))
456 __wake_up(wq, TASK_NORMAL, 1, &key);
457}
458EXPORT_SYMBOL(__wake_up_bit);
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477void wake_up_bit(void *word, int bit)
478{
479 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
480}
481EXPORT_SYMBOL(wake_up_bit);
482
483
484
485
486
487
488static inline wait_queue_head_t *atomic_t_waitqueue(atomic_t *p)
489{
490 if (BITS_PER_LONG == 64) {
491 unsigned long q = (unsigned long)p;
492 return bit_waitqueue((void *)(q & ~1), q & 1);
493 }
494 return bit_waitqueue(p, 0);
495}
496
497static int wake_atomic_t_function(wait_queue_t *wait, unsigned mode, int sync,
498 void *arg)
499{
500 struct wait_bit_key *key = arg;
501 struct wait_bit_queue *wait_bit
502 = container_of(wait, struct wait_bit_queue, wait);
503 atomic_t *val = key->flags;
504
505 if (wait_bit->key.flags != key->flags ||
506 wait_bit->key.bit_nr != key->bit_nr ||
507 atomic_read(val) != 0)
508 return 0;
509 return autoremove_wake_function(wait, mode, sync, key);
510}
511
512
513
514
515
516
517static __sched
518int __wait_on_atomic_t(wait_queue_head_t *wq, struct wait_bit_queue *q,
519 int (*action)(atomic_t *), unsigned mode)
520{
521 atomic_t *val;
522 int ret = 0;
523
524 do {
525 prepare_to_wait(wq, &q->wait, mode);
526 val = q->key.flags;
527 if (atomic_read(val) == 0)
528 break;
529 ret = (*action)(val);
530 } while (!ret && atomic_read(val) != 0);
531 finish_wait(wq, &q->wait);
532 return ret;
533}
534
535#define DEFINE_WAIT_ATOMIC_T(name, p) \
536 struct wait_bit_queue name = { \
537 .key = __WAIT_ATOMIC_T_KEY_INITIALIZER(p), \
538 .wait = { \
539 .private = current, \
540 .func = wake_atomic_t_function, \
541 .task_list = \
542 LIST_HEAD_INIT((name).wait.task_list), \
543 }, \
544 }
545
546__sched int out_of_line_wait_on_atomic_t(atomic_t *p, int (*action)(atomic_t *),
547 unsigned mode)
548{
549 wait_queue_head_t *wq = atomic_t_waitqueue(p);
550 DEFINE_WAIT_ATOMIC_T(wait, p);
551
552 return __wait_on_atomic_t(wq, &wait, action, mode);
553}
554EXPORT_SYMBOL(out_of_line_wait_on_atomic_t);
555
556
557
558
559
560
561
562
563
564
565void wake_up_atomic_t(atomic_t *p)
566{
567 __wake_up_bit(atomic_t_waitqueue(p), p, WAIT_ATOMIC_T_BIT_NR);
568}
569EXPORT_SYMBOL(wake_up_atomic_t);
570
571__sched int bit_wait(struct wait_bit_key *word, int mode)
572{
573 schedule();
574 if (signal_pending_state(mode, current))
575 return -EINTR;
576 return 0;
577}
578EXPORT_SYMBOL(bit_wait);
579
580__sched int bit_wait_io(struct wait_bit_key *word, int mode)
581{
582 io_schedule();
583 if (signal_pending_state(mode, current))
584 return -EINTR;
585 return 0;
586}
587EXPORT_SYMBOL(bit_wait_io);
588
589__sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
590{
591 unsigned long now = READ_ONCE(jiffies);
592 if (time_after_eq(now, word->timeout))
593 return -EAGAIN;
594 schedule_timeout(word->timeout - now);
595 if (signal_pending_state(mode, current))
596 return -EINTR;
597 return 0;
598}
599EXPORT_SYMBOL_GPL(bit_wait_timeout);
600
601__sched int bit_wait_io_timeout(struct wait_bit_key *word, int mode)
602{
603 unsigned long now = READ_ONCE(jiffies);
604 if (time_after_eq(now, word->timeout))
605 return -EAGAIN;
606 io_schedule_timeout(word->timeout - now);
607 if (signal_pending_state(mode, current))
608 return -EINTR;
609 return 0;
610}
611EXPORT_SYMBOL_GPL(bit_wait_io_timeout);
612