wait.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Generic waiting primitives.
  3. *
  4. * (C) 2004 Nadia Yvette Chambers, Oracle
  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. void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *key)
  14. {
  15. spin_lock_init(&q->lock);
  16. lockdep_set_class_and_name(&q->lock, key, name);
  17. INIT_LIST_HEAD(&q->task_list);
  18. }
  19. EXPORT_SYMBOL(__init_waitqueue_head);
  20. void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  21. {
  22. unsigned long flags;
  23. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  24. spin_lock_irqsave(&q->lock, flags);
  25. __add_wait_queue(q, wait);
  26. spin_unlock_irqrestore(&q->lock, flags);
  27. }
  28. EXPORT_SYMBOL(add_wait_queue);
  29. void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
  30. {
  31. unsigned long flags;
  32. wait->flags |= WQ_FLAG_EXCLUSIVE;
  33. spin_lock_irqsave(&q->lock, flags);
  34. __add_wait_queue_tail(q, wait);
  35. spin_unlock_irqrestore(&q->lock, flags);
  36. }
  37. EXPORT_SYMBOL(add_wait_queue_exclusive);
  38. void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  39. {
  40. unsigned long flags;
  41. spin_lock_irqsave(&q->lock, flags);
  42. __remove_wait_queue(q, wait);
  43. spin_unlock_irqrestore(&q->lock, flags);
  44. }
  45. EXPORT_SYMBOL(remove_wait_queue);
  46. /*
  47. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  48. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  49. * number) then we wake all the non-exclusive tasks and one exclusive task.
  50. *
  51. * There are circumstances in which we can try to wake a task which has already
  52. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  53. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  54. */
  55. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  56. int nr_exclusive, int wake_flags, void *key)
  57. {
  58. wait_queue_t *curr, *next;
  59. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  60. unsigned flags = curr->flags;
  61. if (curr->func(curr, mode, wake_flags, key) &&
  62. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  63. break;
  64. }
  65. }
  66. /**
  67. * __wake_up - wake up threads blocked on a waitqueue.
  68. * @q: the waitqueue
  69. * @mode: which threads
  70. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  71. * @key: is directly passed to the wakeup function
  72. *
  73. * It may be assumed that this function implies a write memory barrier before
  74. * changing the task state if and only if any tasks are woken up.
  75. */
  76. void __wake_up(wait_queue_head_t *q, unsigned int mode,
  77. int nr_exclusive, void *key)
  78. {
  79. unsigned long flags;
  80. spin_lock_irqsave(&q->lock, flags);
  81. __wake_up_common(q, mode, nr_exclusive, 0, key);
  82. spin_unlock_irqrestore(&q->lock, flags);
  83. }
  84. EXPORT_SYMBOL(__wake_up);
  85. /*
  86. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  87. */
  88. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr)
  89. {
  90. __wake_up_common(q, mode, nr, 0, NULL);
  91. }
  92. EXPORT_SYMBOL_GPL(__wake_up_locked);
  93. void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
  94. {
  95. __wake_up_common(q, mode, 1, 0, key);
  96. }
  97. EXPORT_SYMBOL_GPL(__wake_up_locked_key);
  98. /**
  99. * __wake_up_sync_key - wake up threads blocked on a waitqueue.
  100. * @q: the waitqueue
  101. * @mode: which threads
  102. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  103. * @key: opaque value to be passed to wakeup targets
  104. *
  105. * The sync wakeup differs that the waker knows that it will schedule
  106. * away soon, so while the target thread will be woken up, it will not
  107. * be migrated to another CPU - ie. the two threads are 'synchronized'
  108. * with each other. This can prevent needless bouncing between CPUs.
  109. *
  110. * On UP it can prevent extra preemption.
  111. *
  112. * It may be assumed that this function implies a write memory barrier before
  113. * changing the task state if and only if any tasks are woken up.
  114. */
  115. void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
  116. int nr_exclusive, void *key)
  117. {
  118. unsigned long flags;
  119. int wake_flags = 1; /* XXX WF_SYNC */
  120. if (unlikely(!q))
  121. return;
  122. if (unlikely(nr_exclusive != 1))
  123. wake_flags = 0;
  124. spin_lock_irqsave(&q->lock, flags);
  125. __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
  126. spin_unlock_irqrestore(&q->lock, flags);
  127. }
  128. EXPORT_SYMBOL_GPL(__wake_up_sync_key);
  129. /*
  130. * __wake_up_sync - see __wake_up_sync_key()
  131. */
  132. void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  133. {
  134. __wake_up_sync_key(q, mode, nr_exclusive, NULL);
  135. }
  136. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  137. /*
  138. * Note: we use "set_current_state()" _after_ the wait-queue add,
  139. * because we need a memory barrier there on SMP, so that any
  140. * wake-function that tests for the wait-queue being active
  141. * will be guaranteed to see waitqueue addition _or_ subsequent
  142. * tests in this thread will see the wakeup having taken place.
  143. *
  144. * The spin_unlock() itself is semi-permeable and only protects
  145. * one way (it only protects stuff inside the critical region and
  146. * stops them from bleeding out - it would still allow subsequent
  147. * loads to move into the critical region).
  148. */
  149. void
  150. prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
  151. {
  152. unsigned long flags;
  153. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  154. spin_lock_irqsave(&q->lock, flags);
  155. if (list_empty(&wait->task_list))
  156. __add_wait_queue(q, wait);
  157. set_current_state(state);
  158. spin_unlock_irqrestore(&q->lock, flags);
  159. }
  160. EXPORT_SYMBOL(prepare_to_wait);
  161. void
  162. prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
  163. {
  164. unsigned long flags;
  165. wait->flags |= WQ_FLAG_EXCLUSIVE;
  166. spin_lock_irqsave(&q->lock, flags);
  167. if (list_empty(&wait->task_list))
  168. __add_wait_queue_tail(q, wait);
  169. set_current_state(state);
  170. spin_unlock_irqrestore(&q->lock, flags);
  171. }
  172. EXPORT_SYMBOL(prepare_to_wait_exclusive);
  173. long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state)
  174. {
  175. unsigned long flags;
  176. if (signal_pending_state(state, current))
  177. return -ERESTARTSYS;
  178. wait->private = current;
  179. wait->func = autoremove_wake_function;
  180. spin_lock_irqsave(&q->lock, flags);
  181. if (list_empty(&wait->task_list)) {
  182. if (wait->flags & WQ_FLAG_EXCLUSIVE)
  183. __add_wait_queue_tail(q, wait);
  184. else
  185. __add_wait_queue(q, wait);
  186. }
  187. set_current_state(state);
  188. spin_unlock_irqrestore(&q->lock, flags);
  189. return 0;
  190. }
  191. EXPORT_SYMBOL(prepare_to_wait_event);
  192. /**
  193. * finish_wait - clean up after waiting in a queue
  194. * @q: waitqueue waited on
  195. * @wait: wait descriptor
  196. *
  197. * Sets current thread back to running state and removes
  198. * the wait descriptor from the given waitqueue if still
  199. * queued.
  200. */
  201. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  202. {
  203. unsigned long flags;
  204. __set_current_state(TASK_RUNNING);
  205. /*
  206. * We can check for list emptiness outside the lock
  207. * IFF:
  208. * - we use the "careful" check that verifies both
  209. * the next and prev pointers, so that there cannot
  210. * be any half-pending updates in progress on other
  211. * CPU's that we haven't seen yet (and that might
  212. * still change the stack area.
  213. * and
  214. * - all other users take the lock (ie we can only
  215. * have _one_ other CPU that looks at or modifies
  216. * the list).
  217. */
  218. if (!list_empty_careful(&wait->task_list)) {
  219. spin_lock_irqsave(&q->lock, flags);
  220. list_del_init(&wait->task_list);
  221. spin_unlock_irqrestore(&q->lock, flags);
  222. }
  223. }
  224. EXPORT_SYMBOL(finish_wait);
  225. /**
  226. * abort_exclusive_wait - abort exclusive waiting in a queue
  227. * @q: waitqueue waited on
  228. * @wait: wait descriptor
  229. * @mode: runstate of the waiter to be woken
  230. * @key: key to identify a wait bit queue or %NULL
  231. *
  232. * Sets current thread back to running state and removes
  233. * the wait descriptor from the given waitqueue if still
  234. * queued.
  235. *
  236. * Wakes up the next waiter if the caller is concurrently
  237. * woken up through the queue.
  238. *
  239. * This prevents waiter starvation where an exclusive waiter
  240. * aborts and is woken up concurrently and no one wakes up
  241. * the next waiter.
  242. */
  243. void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
  244. unsigned int mode, void *key)
  245. {
  246. unsigned long flags;
  247. __set_current_state(TASK_RUNNING);
  248. spin_lock_irqsave(&q->lock, flags);
  249. if (!list_empty(&wait->task_list))
  250. list_del_init(&wait->task_list);
  251. else if (waitqueue_active(q))
  252. __wake_up_locked_key(q, mode, key);
  253. spin_unlock_irqrestore(&q->lock, flags);
  254. }
  255. EXPORT_SYMBOL(abort_exclusive_wait);
  256. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  257. {
  258. int ret = default_wake_function(wait, mode, sync, key);
  259. if (ret)
  260. list_del_init(&wait->task_list);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL(autoremove_wake_function);
  264. static inline bool is_kthread_should_stop(void)
  265. {
  266. return (current->flags & PF_KTHREAD) && kthread_should_stop();
  267. }
  268. /*
  269. * DEFINE_WAIT_FUNC(wait, woken_wake_func);
  270. *
  271. * add_wait_queue(&wq, &wait);
  272. * for (;;) {
  273. * if (condition)
  274. * break;
  275. *
  276. * p->state = mode; condition = true;
  277. * smp_mb(); // A smp_wmb(); // C
  278. * if (!wait->flags & WQ_FLAG_WOKEN) wait->flags |= WQ_FLAG_WOKEN;
  279. * schedule() try_to_wake_up();
  280. * p->state = TASK_RUNNING; ~~~~~~~~~~~~~~~~~~
  281. * wait->flags &= ~WQ_FLAG_WOKEN; condition = true;
  282. * smp_mb() // B smp_wmb(); // C
  283. * wait->flags |= WQ_FLAG_WOKEN;
  284. * }
  285. * remove_wait_queue(&wq, &wait);
  286. *
  287. */
  288. long wait_woken(wait_queue_t *wait, unsigned mode, long timeout)
  289. {
  290. set_current_state(mode); /* A */
  291. /*
  292. * The above implies an smp_mb(), which matches with the smp_wmb() from
  293. * woken_wake_function() such that if we observe WQ_FLAG_WOKEN we must
  294. * also observe all state before the wakeup.
  295. */
  296. if (!(wait->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
  297. timeout = schedule_timeout(timeout);
  298. __set_current_state(TASK_RUNNING);
  299. /*
  300. * The below implies an smp_mb(), it too pairs with the smp_wmb() from
  301. * woken_wake_function() such that we must either observe the wait
  302. * condition being true _OR_ WQ_FLAG_WOKEN such that we will not miss
  303. * an event.
  304. */
  305. smp_store_mb(wait->flags, wait->flags & ~WQ_FLAG_WOKEN); /* B */
  306. return timeout;
  307. }
  308. EXPORT_SYMBOL(wait_woken);
  309. int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  310. {
  311. /*
  312. * Although this function is called under waitqueue lock, LOCK
  313. * doesn't imply write barrier and the users expects write
  314. * barrier semantics on wakeup functions. The following
  315. * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up()
  316. * and is paired with smp_store_mb() in wait_woken().
  317. */
  318. smp_wmb(); /* C */
  319. wait->flags |= WQ_FLAG_WOKEN;
  320. return default_wake_function(wait, mode, sync, key);
  321. }
  322. EXPORT_SYMBOL(woken_wake_function);
  323. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
  324. {
  325. struct wait_bit_key *key = arg;
  326. struct wait_bit_queue *wait_bit
  327. = container_of(wait, struct wait_bit_queue, wait);
  328. if (wait_bit->key.flags != key->flags ||
  329. wait_bit->key.bit_nr != key->bit_nr ||
  330. test_bit(key->bit_nr, key->flags))
  331. return 0;
  332. else
  333. return autoremove_wake_function(wait, mode, sync, key);
  334. }
  335. EXPORT_SYMBOL(wake_bit_function);
  336. /*
  337. * To allow interruptible waiting and asynchronous (i.e. nonblocking)
  338. * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
  339. * permitted return codes. Nonzero return codes halt waiting and return.
  340. */
  341. int __sched
  342. __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
  343. wait_bit_action_f *action, unsigned mode)
  344. {
  345. int ret = 0;
  346. do {
  347. prepare_to_wait(wq, &q->wait, mode);
  348. if (test_bit(q->key.bit_nr, q->key.flags))
  349. ret = (*action)(&q->key, mode);
  350. } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
  351. finish_wait(wq, &q->wait);
  352. return ret;
  353. }
  354. EXPORT_SYMBOL(__wait_on_bit);
  355. int __sched out_of_line_wait_on_bit(void *word, int bit,
  356. wait_bit_action_f *action, unsigned mode)
  357. {
  358. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  359. DEFINE_WAIT_BIT(wait, word, bit);
  360. return __wait_on_bit(wq, &wait, action, mode);
  361. }
  362. EXPORT_SYMBOL(out_of_line_wait_on_bit);
  363. int __sched out_of_line_wait_on_bit_timeout(
  364. void *word, int bit, wait_bit_action_f *action,
  365. unsigned mode, unsigned long timeout)
  366. {
  367. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  368. DEFINE_WAIT_BIT(wait, word, bit);
  369. wait.key.timeout = jiffies + timeout;
  370. return __wait_on_bit(wq, &wait, action, mode);
  371. }
  372. EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
  373. int __sched
  374. __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
  375. wait_bit_action_f *action, unsigned mode)
  376. {
  377. do {
  378. int ret;
  379. prepare_to_wait_exclusive(wq, &q->wait, mode);
  380. if (!test_bit(q->key.bit_nr, q->key.flags))
  381. continue;
  382. ret = action(&q->key, mode);
  383. if (!ret)
  384. continue;
  385. abort_exclusive_wait(wq, &q->wait, mode, &q->key);
  386. return ret;
  387. } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
  388. finish_wait(wq, &q->wait);
  389. return 0;
  390. }
  391. EXPORT_SYMBOL(__wait_on_bit_lock);
  392. int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
  393. wait_bit_action_f *action, unsigned mode)
  394. {
  395. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  396. DEFINE_WAIT_BIT(wait, word, bit);
  397. return __wait_on_bit_lock(wq, &wait, action, mode);
  398. }
  399. EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
  400. void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
  401. {
  402. struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
  403. if (waitqueue_active(wq))
  404. __wake_up(wq, TASK_NORMAL, 1, &key);
  405. }
  406. EXPORT_SYMBOL(__wake_up_bit);
  407. /**
  408. * wake_up_bit - wake up a waiter on a bit
  409. * @word: the word being waited on, a kernel virtual address
  410. * @bit: the bit of the word being waited on
  411. *
  412. * There is a standard hashed waitqueue table for generic use. This
  413. * is the part of the hashtable's accessor API that wakes up waiters
  414. * on a bit. For instance, if one were to have waiters on a bitflag,
  415. * one would call wake_up_bit() after clearing the bit.
  416. *
  417. * In order for this to function properly, as it uses waitqueue_active()
  418. * internally, some kind of memory barrier must be done prior to calling
  419. * this. Typically, this will be smp_mb__after_atomic(), but in some
  420. * cases where bitflags are manipulated non-atomically under a lock, one
  421. * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
  422. * because spin_unlock() does not guarantee a memory barrier.
  423. */
  424. void wake_up_bit(void *word, int bit)
  425. {
  426. __wake_up_bit(bit_waitqueue(word, bit), word, bit);
  427. }
  428. EXPORT_SYMBOL(wake_up_bit);
  429. wait_queue_head_t *bit_waitqueue(void *word, int bit)
  430. {
  431. const int shift = BITS_PER_LONG == 32 ? 5 : 6;
  432. const struct zone *zone = page_zone(virt_to_page(word));
  433. unsigned long val = (unsigned long)word << shift | bit;
  434. return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
  435. }
  436. EXPORT_SYMBOL(bit_waitqueue);
  437. /*
  438. * Manipulate the atomic_t address to produce a better bit waitqueue table hash
  439. * index (we're keying off bit -1, but that would produce a horrible hash
  440. * value).
  441. */
  442. static inline wait_queue_head_t *atomic_t_waitqueue(atomic_t *p)
  443. {
  444. if (BITS_PER_LONG == 64) {
  445. unsigned long q = (unsigned long)p;
  446. return bit_waitqueue((void *)(q & ~1), q & 1);
  447. }
  448. return bit_waitqueue(p, 0);
  449. }
  450. static int wake_atomic_t_function(wait_queue_t *wait, unsigned mode, int sync,
  451. void *arg)
  452. {
  453. struct wait_bit_key *key = arg;
  454. struct wait_bit_queue *wait_bit
  455. = container_of(wait, struct wait_bit_queue, wait);
  456. atomic_t *val = key->flags;
  457. if (wait_bit->key.flags != key->flags ||
  458. wait_bit->key.bit_nr != key->bit_nr ||
  459. atomic_read(val) != 0)
  460. return 0;
  461. return autoremove_wake_function(wait, mode, sync, key);
  462. }
  463. /*
  464. * To allow interruptible waiting and asynchronous (i.e. nonblocking) waiting,
  465. * the actions of __wait_on_atomic_t() are permitted return codes. Nonzero
  466. * return codes halt waiting and return.
  467. */
  468. static __sched
  469. int __wait_on_atomic_t(wait_queue_head_t *wq, struct wait_bit_queue *q,
  470. int (*action)(atomic_t *), unsigned mode)
  471. {
  472. atomic_t *val;
  473. int ret = 0;
  474. do {
  475. prepare_to_wait(wq, &q->wait, mode);
  476. val = q->key.flags;
  477. if (atomic_read(val) == 0)
  478. break;
  479. ret = (*action)(val);
  480. } while (!ret && atomic_read(val) != 0);
  481. finish_wait(wq, &q->wait);
  482. return ret;
  483. }
  484. #define DEFINE_WAIT_ATOMIC_T(name, p) \
  485. struct wait_bit_queue name = { \
  486. .key = __WAIT_ATOMIC_T_KEY_INITIALIZER(p), \
  487. .wait = { \
  488. .private = current, \
  489. .func = wake_atomic_t_function, \
  490. .task_list = \
  491. LIST_HEAD_INIT((name).wait.task_list), \
  492. }, \
  493. }
  494. __sched int out_of_line_wait_on_atomic_t(atomic_t *p, int (*action)(atomic_t *),
  495. unsigned mode)
  496. {
  497. wait_queue_head_t *wq = atomic_t_waitqueue(p);
  498. DEFINE_WAIT_ATOMIC_T(wait, p);
  499. return __wait_on_atomic_t(wq, &wait, action, mode);
  500. }
  501. EXPORT_SYMBOL(out_of_line_wait_on_atomic_t);
  502. /**
  503. * wake_up_atomic_t - Wake up a waiter on a atomic_t
  504. * @p: The atomic_t being waited on, a kernel virtual address
  505. *
  506. * Wake up anyone waiting for the atomic_t to go to zero.
  507. *
  508. * Abuse the bit-waker function and its waitqueue hash table set (the atomic_t
  509. * check is done by the waiter's wake function, not the by the waker itself).
  510. */
  511. void wake_up_atomic_t(atomic_t *p)
  512. {
  513. __wake_up_bit(atomic_t_waitqueue(p), p, WAIT_ATOMIC_T_BIT_NR);
  514. }
  515. EXPORT_SYMBOL(wake_up_atomic_t);
  516. __sched int bit_wait(struct wait_bit_key *word, int mode)
  517. {
  518. schedule();
  519. if (signal_pending_state(mode, current))
  520. return -EINTR;
  521. return 0;
  522. }
  523. EXPORT_SYMBOL(bit_wait);
  524. __sched int bit_wait_io(struct wait_bit_key *word, int mode)
  525. {
  526. io_schedule();
  527. if (signal_pending_state(mode, current))
  528. return -EINTR;
  529. return 0;
  530. }
  531. EXPORT_SYMBOL(bit_wait_io);
  532. __sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
  533. {
  534. unsigned long now = READ_ONCE(jiffies);
  535. if (time_after_eq(now, word->timeout))
  536. return -EAGAIN;
  537. schedule_timeout(word->timeout - now);
  538. if (signal_pending_state(mode, current))
  539. return -EINTR;
  540. return 0;
  541. }
  542. EXPORT_SYMBOL_GPL(bit_wait_timeout);
  543. __sched int bit_wait_io_timeout(struct wait_bit_key *word, int mode)
  544. {
  545. unsigned long now = READ_ONCE(jiffies);
  546. if (time_after_eq(now, word->timeout))
  547. return -EAGAIN;
  548. io_schedule_timeout(word->timeout - now);
  549. if (signal_pending_state(mode, current))
  550. return -EINTR;
  551. return 0;
  552. }
  553. EXPORT_SYMBOL_GPL(bit_wait_io_timeout);