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
65
66
67
68void
69prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
70{
71 unsigned long flags;
72
73 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
74 spin_lock_irqsave(&q->lock, flags);
75 if (list_empty(&wait->task_list))
76 __add_wait_queue(q, wait);
77 set_current_state(state);
78 spin_unlock_irqrestore(&q->lock, flags);
79}
80EXPORT_SYMBOL(prepare_to_wait);
81
82void
83prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
84{
85 unsigned long flags;
86
87 wait->flags |= WQ_FLAG_EXCLUSIVE;
88 spin_lock_irqsave(&q->lock, flags);
89 if (list_empty(&wait->task_list))
90 __add_wait_queue_tail(q, wait);
91 set_current_state(state);
92 spin_unlock_irqrestore(&q->lock, flags);
93}
94EXPORT_SYMBOL(prepare_to_wait_exclusive);
95
96
97
98
99
100
101
102
103
104
105void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
106{
107 unsigned long flags;
108
109 __set_current_state(TASK_RUNNING);
110
111
112
113
114
115
116
117
118
119
120
121
122
123 if (!list_empty_careful(&wait->task_list)) {
124 spin_lock_irqsave(&q->lock, flags);
125 list_del_init(&wait->task_list);
126 spin_unlock_irqrestore(&q->lock, flags);
127 }
128}
129EXPORT_SYMBOL(finish_wait);
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
150 unsigned int mode, void *key)
151{
152 unsigned long flags;
153
154 __set_current_state(TASK_RUNNING);
155 spin_lock_irqsave(&q->lock, flags);
156 if (!list_empty(&wait->task_list))
157 list_del_init(&wait->task_list);
158 else if (waitqueue_active(q))
159 __wake_up_locked_key(q, mode, key);
160 spin_unlock_irqrestore(&q->lock, flags);
161}
162EXPORT_SYMBOL(abort_exclusive_wait);
163
164int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
165{
166 int ret = default_wake_function(wait, mode, sync, key);
167
168 if (ret)
169 list_del_init(&wait->task_list);
170 return ret;
171}
172EXPORT_SYMBOL(autoremove_wake_function);
173
174static inline bool is_kthread_should_stop(void)
175{
176 return (current->flags & PF_KTHREAD) && kthread_should_stop();
177}
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199long wait_woken(wait_queue_t *wait, unsigned mode, long timeout)
200{
201 set_current_state(mode);
202
203
204
205
206
207 if (!(wait->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
208 timeout = schedule_timeout(timeout);
209 __set_current_state(TASK_RUNNING);
210
211
212
213
214
215
216
217 set_mb(wait->flags, wait->flags & ~WQ_FLAG_WOKEN);
218
219 return timeout;
220}
221EXPORT_SYMBOL(wait_woken);
222
223int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
224{
225
226
227
228
229
230
231
232 smp_wmb();
233 wait->flags |= WQ_FLAG_WOKEN;
234
235 return default_wake_function(wait, mode, sync, key);
236}
237EXPORT_SYMBOL(woken_wake_function);
238
239
240
241
242
243
244int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
245{
246 struct wait_bit_key_deprecated *key = arg;
247 struct wait_bit_queue_deprecated *wait_bit
248 = container_of(wait, struct wait_bit_queue_deprecated, wait);
249
250 if (wait_bit->key.flags != key->flags ||
251 wait_bit->key.bit_nr != key->bit_nr ||
252 test_bit(key->bit_nr, key->flags))
253 return 0;
254 else
255 return autoremove_wake_function(wait, mode, sync, key);
256}
257EXPORT_SYMBOL(wake_bit_function);
258
259int wake_bit_function_rh(wait_queue_t *wait, unsigned mode, int sync, void *arg)
260{
261 struct wait_bit_key *key = arg;
262 struct wait_bit_queue *wait_bit
263 = container_of(wait, struct wait_bit_queue, wait);
264
265 if (wait_bit->key.flags != key->flags ||
266 wait_bit->key.bit_nr != key->bit_nr ||
267 test_bit(key->bit_nr, key->flags))
268 return 0;
269 else
270 return autoremove_wake_function(wait, mode, sync, key);
271}
272EXPORT_SYMBOL(wake_bit_function_rh);
273
274
275
276
277
278
279int __sched
280__wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
281 wait_bit_action_f *action, unsigned mode)
282{
283 int ret = 0;
284
285 do {
286 prepare_to_wait(wq, &q->wait, mode);
287 if (test_bit(q->key.bit_nr, q->key.flags))
288 ret = (*action)(&q->key, mode);
289 } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
290 finish_wait(wq, &q->wait);
291 return ret;
292}
293EXPORT_SYMBOL(__wait_on_bit);
294
295int __sched out_of_line_wait_on_bit(void *word, int bit,
296 wait_bit_action_f *action, unsigned mode)
297{
298 wait_queue_head_t *wq = bit_waitqueue(word, bit);
299 DEFINE_WAIT_BIT(wait, word, bit);
300
301 return __wait_on_bit(wq, &wait, action, mode);
302}
303EXPORT_SYMBOL(out_of_line_wait_on_bit);
304
305int __sched out_of_line_wait_on_bit_timeout(
306 void *word, int bit, wait_bit_action_f *action,
307 unsigned mode, unsigned long timeout)
308{
309 wait_queue_head_t *wq = bit_waitqueue(word, bit);
310 DEFINE_WAIT_BIT(wait, word, bit);
311
312 wait.key.timeout = jiffies + timeout;
313 return __wait_on_bit(wq, &wait, action, mode);
314}
315EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
316
317int __sched
318__wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
319 wait_bit_action_f *action, unsigned mode)
320{
321 do {
322 int ret;
323
324 prepare_to_wait_exclusive(wq, &q->wait, mode);
325 if (!test_bit(q->key.bit_nr, q->key.flags))
326 continue;
327 ret = action(&q->key, mode);
328 if (!ret)
329 continue;
330 abort_exclusive_wait(wq, &q->wait, mode, &q->key);
331 return ret;
332 } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
333 finish_wait(wq, &q->wait);
334 return 0;
335}
336EXPORT_SYMBOL(__wait_on_bit_lock);
337
338int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
339 wait_bit_action_f *action, unsigned mode)
340{
341 wait_queue_head_t *wq = bit_waitqueue(word, bit);
342 DEFINE_WAIT_BIT(wait, word, bit);
343
344 return __wait_on_bit_lock(wq, &wait, action, mode);
345}
346EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
347
348void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
349{
350 struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
351 if (waitqueue_active(wq))
352 __wake_up(wq, TASK_NORMAL, 1, &key);
353}
354EXPORT_SYMBOL(__wake_up_bit);
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373void wake_up_bit(void *word, int bit)
374{
375 __wake_up_bit(bit_waitqueue(word, bit), word, bit);
376}
377EXPORT_SYMBOL(wake_up_bit);
378
379wait_queue_head_t *bit_waitqueue(void *word, int bit)
380{
381 const int shift = BITS_PER_LONG == 32 ? 5 : 6;
382 struct page *page = is_vmalloc_addr(word) ?
383 vmalloc_to_page(word) : virt_to_page(word);
384 const struct zone *zone = page_zone(page);
385 unsigned long val = (unsigned long)word << shift | bit;
386
387 return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
388}
389EXPORT_SYMBOL(bit_waitqueue);
390
391
392
393
394
395
396static inline wait_queue_head_t *atomic_t_waitqueue(atomic_t *p)
397{
398 if (BITS_PER_LONG == 64) {
399 unsigned long q = (unsigned long)p;
400 return bit_waitqueue((void *)(q & ~1), q & 1);
401 }
402 return bit_waitqueue(p, 0);
403}
404
405static int wake_atomic_t_function(wait_queue_t *wait, unsigned mode, int sync,
406 void *arg)
407{
408 struct wait_bit_key *key = arg;
409 struct wait_bit_queue *wait_bit
410 = container_of(wait, struct wait_bit_queue, wait);
411 atomic_t *val = key->flags;
412
413 if (wait_bit->key.flags != key->flags ||
414 wait_bit->key.bit_nr != key->bit_nr ||
415 atomic_read(val) != 0)
416 return 0;
417 return autoremove_wake_function(wait, mode, sync, key);
418}
419
420
421
422
423
424
425static __sched
426int __wait_on_atomic_t(wait_queue_head_t *wq, struct wait_bit_queue *q,
427 int (*action)(atomic_t *), unsigned mode)
428{
429 atomic_t *val;
430 int ret = 0;
431
432 do {
433 prepare_to_wait(wq, &q->wait, mode);
434 val = q->key.flags;
435 if (atomic_read(val) == 0)
436 break;
437 ret = (*action)(val);
438 } while (!ret && atomic_read(val) != 0);
439 finish_wait(wq, &q->wait);
440 return ret;
441}
442
443#define DEFINE_WAIT_ATOMIC_T(name, p) \
444 struct wait_bit_queue name = { \
445 .key = __WAIT_ATOMIC_T_KEY_INITIALIZER(p), \
446 .wait = { \
447 .private = current, \
448 .func = wake_atomic_t_function, \
449 .task_list = \
450 LIST_HEAD_INIT((name).wait.task_list), \
451 }, \
452 }
453
454__sched int out_of_line_wait_on_atomic_t(atomic_t *p, int (*action)(atomic_t *),
455 unsigned mode)
456{
457 wait_queue_head_t *wq = atomic_t_waitqueue(p);
458 DEFINE_WAIT_ATOMIC_T(wait, p);
459
460 return __wait_on_atomic_t(wq, &wait, action, mode);
461}
462EXPORT_SYMBOL(out_of_line_wait_on_atomic_t);
463
464
465
466
467
468
469
470
471
472
473void wake_up_atomic_t(atomic_t *p)
474{
475 __wake_up_bit(atomic_t_waitqueue(p), p, WAIT_ATOMIC_T_BIT_NR);
476}
477EXPORT_SYMBOL(wake_up_atomic_t);
478
479__sched int bit_wait(struct wait_bit_key *word, int mode)
480{
481 schedule();
482 if (signal_pending_state(mode, current))
483 return -EINTR;
484 return 0;
485}
486EXPORT_SYMBOL(bit_wait);
487
488__sched int bit_wait_io(struct wait_bit_key *word, int mode)
489{
490 io_schedule();
491 if (signal_pending_state(mode, current))
492 return -EINTR;
493 return 0;
494}
495EXPORT_SYMBOL(bit_wait_io);
496
497__sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
498{
499 unsigned long now = ACCESS_ONCE(jiffies);
500 if (time_after_eq(now, word->timeout))
501 return -EAGAIN;
502 schedule_timeout(word->timeout - now);
503 if (signal_pending_state(mode, current))
504 return -EINTR;
505 return 0;
506}
507EXPORT_SYMBOL_GPL(bit_wait_timeout);
508
509__sched int bit_wait_io_timeout(struct wait_bit_key *word, int mode)
510{
511 unsigned long now = ACCESS_ONCE(jiffies);
512 if (time_after_eq(now, word->timeout))
513 return -EAGAIN;
514 io_schedule_timeout(word->timeout - now);
515 if (signal_pending_state(mode, current))
516 return -EINTR;
517 return 0;
518}
519EXPORT_SYMBOL_GPL(bit_wait_io_timeout);
520