wait.c 18 KB

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