wait.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. void init_wait_entry(wait_queue_t *wait, int flags)
  174. {
  175. wait->flags = flags;
  176. wait->private = current;
  177. wait->func = autoremove_wake_function;
  178. INIT_LIST_HEAD(&wait->task_list);
  179. }
  180. EXPORT_SYMBOL(init_wait_entry);
  181. long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state)
  182. {
  183. unsigned long flags;
  184. long ret = 0;
  185. spin_lock_irqsave(&q->lock, flags);
  186. if (unlikely(signal_pending_state(state, current))) {
  187. /*
  188. * Exclusive waiter must not fail if it was selected by wakeup,
  189. * it should "consume" the condition we were waiting for.
  190. *
  191. * The caller will recheck the condition and return success if
  192. * we were already woken up, we can not miss the event because
  193. * wakeup locks/unlocks the same q->lock.
  194. *
  195. * But we need to ensure that set-condition + wakeup after that
  196. * can't see us, it should wake up another exclusive waiter if
  197. * we fail.
  198. */
  199. list_del_init(&wait->task_list);
  200. ret = -ERESTARTSYS;
  201. } else {
  202. if (list_empty(&wait->task_list)) {
  203. if (wait->flags & WQ_FLAG_EXCLUSIVE)
  204. __add_wait_queue_tail(q, wait);
  205. else
  206. __add_wait_queue(q, wait);
  207. }
  208. set_current_state(state);
  209. }
  210. spin_unlock_irqrestore(&q->lock, flags);
  211. return ret;
  212. }
  213. EXPORT_SYMBOL(prepare_to_wait_event);
  214. /**
  215. * finish_wait - clean up after waiting in a queue
  216. * @q: waitqueue waited on
  217. * @wait: wait descriptor
  218. *
  219. * Sets current thread back to running state and removes
  220. * the wait descriptor from the given waitqueue if still
  221. * queued.
  222. */
  223. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  224. {
  225. unsigned long flags;
  226. __set_current_state(TASK_RUNNING);
  227. /*
  228. * We can check for list emptiness outside the lock
  229. * IFF:
  230. * - we use the "careful" check that verifies both
  231. * the next and prev pointers, so that there cannot
  232. * be any half-pending updates in progress on other
  233. * CPU's that we haven't seen yet (and that might
  234. * still change the stack area.
  235. * and
  236. * - all other users take the lock (ie we can only
  237. * have _one_ other CPU that looks at or modifies
  238. * the list).
  239. */
  240. if (!list_empty_careful(&wait->task_list)) {
  241. spin_lock_irqsave(&q->lock, flags);
  242. list_del_init(&wait->task_list);
  243. spin_unlock_irqrestore(&q->lock, flags);
  244. }
  245. }
  246. EXPORT_SYMBOL(finish_wait);
  247. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  248. {
  249. int ret = default_wake_function(wait, mode, sync, key);
  250. if (ret)
  251. list_del_init(&wait->task_list);
  252. return ret;
  253. }
  254. EXPORT_SYMBOL(autoremove_wake_function);
  255. static inline bool is_kthread_should_stop(void)
  256. {
  257. return (current->flags & PF_KTHREAD) && kthread_should_stop();
  258. }
  259. /*
  260. * DEFINE_WAIT_FUNC(wait, woken_wake_func);
  261. *
  262. * add_wait_queue(&wq, &wait);
  263. * for (;;) {
  264. * if (condition)
  265. * break;
  266. *
  267. * p->state = mode; condition = true;
  268. * smp_mb(); // A smp_wmb(); // C
  269. * if (!wait->flags & WQ_FLAG_WOKEN) wait->flags |= WQ_FLAG_WOKEN;
  270. * schedule() try_to_wake_up();
  271. * p->state = TASK_RUNNING; ~~~~~~~~~~~~~~~~~~
  272. * wait->flags &= ~WQ_FLAG_WOKEN; condition = true;
  273. * smp_mb() // B smp_wmb(); // C
  274. * wait->flags |= WQ_FLAG_WOKEN;
  275. * }
  276. * remove_wait_queue(&wq, &wait);
  277. *
  278. */
  279. long wait_woken(wait_queue_t *wait, unsigned mode, long timeout)
  280. {
  281. set_current_state(mode); /* A */
  282. /*
  283. * The above implies an smp_mb(), which matches with the smp_wmb() from
  284. * woken_wake_function() such that if we observe WQ_FLAG_WOKEN we must
  285. * also observe all state before the wakeup.
  286. */
  287. if (!(wait->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
  288. timeout = schedule_timeout(timeout);
  289. __set_current_state(TASK_RUNNING);
  290. /*
  291. * The below implies an smp_mb(), it too pairs with the smp_wmb() from
  292. * woken_wake_function() such that we must either observe the wait
  293. * condition being true _OR_ WQ_FLAG_WOKEN such that we will not miss
  294. * an event.
  295. */
  296. smp_store_mb(wait->flags, wait->flags & ~WQ_FLAG_WOKEN); /* B */
  297. return timeout;
  298. }
  299. EXPORT_SYMBOL(wait_woken);
  300. int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  301. {
  302. /*
  303. * Although this function is called under waitqueue lock, LOCK
  304. * doesn't imply write barrier and the users expects write
  305. * barrier semantics on wakeup functions. The following
  306. * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up()
  307. * and is paired with smp_store_mb() in wait_woken().
  308. */
  309. smp_wmb(); /* C */
  310. wait->flags |= WQ_FLAG_WOKEN;
  311. return default_wake_function(wait, mode, sync, key);
  312. }
  313. EXPORT_SYMBOL(woken_wake_function);
  314. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
  315. {
  316. struct wait_bit_key *key = arg;
  317. struct wait_bit_queue *wait_bit
  318. = container_of(wait, struct wait_bit_queue, wait);
  319. if (wait_bit->key.flags != key->flags ||
  320. wait_bit->key.bit_nr != key->bit_nr ||
  321. test_bit(key->bit_nr, key->flags))
  322. return 0;
  323. else
  324. return autoremove_wake_function(wait, mode, sync, key);
  325. }
  326. EXPORT_SYMBOL(wake_bit_function);
  327. /*
  328. * To allow interruptible waiting and asynchronous (i.e. nonblocking)
  329. * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
  330. * permitted return codes. Nonzero return codes halt waiting and return.
  331. */
  332. int __sched
  333. __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
  334. wait_bit_action_f *action, unsigned mode)
  335. {
  336. int ret = 0;
  337. do {
  338. prepare_to_wait(wq, &q->wait, mode);
  339. if (test_bit(q->key.bit_nr, q->key.flags))
  340. ret = (*action)(&q->key, mode);
  341. } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
  342. finish_wait(wq, &q->wait);
  343. return ret;
  344. }
  345. EXPORT_SYMBOL(__wait_on_bit);
  346. int __sched out_of_line_wait_on_bit(void *word, int bit,
  347. wait_bit_action_f *action, unsigned mode)
  348. {
  349. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  350. DEFINE_WAIT_BIT(wait, word, bit);
  351. return __wait_on_bit(wq, &wait, action, mode);
  352. }
  353. EXPORT_SYMBOL(out_of_line_wait_on_bit);
  354. int __sched out_of_line_wait_on_bit_timeout(
  355. void *word, int bit, wait_bit_action_f *action,
  356. unsigned mode, unsigned long timeout)
  357. {
  358. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  359. DEFINE_WAIT_BIT(wait, word, bit);
  360. wait.key.timeout = jiffies + timeout;
  361. return __wait_on_bit(wq, &wait, action, mode);
  362. }
  363. EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
  364. int __sched
  365. __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
  366. wait_bit_action_f *action, unsigned mode)
  367. {
  368. int ret = 0;
  369. for (;;) {
  370. prepare_to_wait_exclusive(wq, &q->wait, mode);
  371. if (test_bit(q->key.bit_nr, q->key.flags)) {
  372. ret = action(&q->key, mode);
  373. /*
  374. * See the comment in prepare_to_wait_event().
  375. * finish_wait() does not necessarily takes wq->lock,
  376. * but test_and_set_bit() implies mb() which pairs with
  377. * smp_mb__after_atomic() before wake_up_page().
  378. */
  379. if (ret)
  380. finish_wait(wq, &q->wait);
  381. }
  382. if (!test_and_set_bit(q->key.bit_nr, q->key.flags)) {
  383. if (!ret)
  384. finish_wait(wq, &q->wait);
  385. return 0;
  386. } else if (ret) {
  387. return ret;
  388. }
  389. }
  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. /*
  430. * Manipulate the atomic_t address to produce a better bit waitqueue table hash
  431. * index (we're keying off bit -1, but that would produce a horrible hash
  432. * value).
  433. */
  434. static inline wait_queue_head_t *atomic_t_waitqueue(atomic_t *p)
  435. {
  436. if (BITS_PER_LONG == 64) {
  437. unsigned long q = (unsigned long)p;
  438. return bit_waitqueue((void *)(q & ~1), q & 1);
  439. }
  440. return bit_waitqueue(p, 0);
  441. }
  442. static int wake_atomic_t_function(wait_queue_t *wait, unsigned mode, int sync,
  443. void *arg)
  444. {
  445. struct wait_bit_key *key = arg;
  446. struct wait_bit_queue *wait_bit
  447. = container_of(wait, struct wait_bit_queue, wait);
  448. atomic_t *val = key->flags;
  449. if (wait_bit->key.flags != key->flags ||
  450. wait_bit->key.bit_nr != key->bit_nr ||
  451. atomic_read(val) != 0)
  452. return 0;
  453. return autoremove_wake_function(wait, mode, sync, key);
  454. }
  455. /*
  456. * To allow interruptible waiting and asynchronous (i.e. nonblocking) waiting,
  457. * the actions of __wait_on_atomic_t() are permitted return codes. Nonzero
  458. * return codes halt waiting and return.
  459. */
  460. static __sched
  461. int __wait_on_atomic_t(wait_queue_head_t *wq, struct wait_bit_queue *q,
  462. int (*action)(atomic_t *), unsigned mode)
  463. {
  464. atomic_t *val;
  465. int ret = 0;
  466. do {
  467. prepare_to_wait(wq, &q->wait, mode);
  468. val = q->key.flags;
  469. if (atomic_read(val) == 0)
  470. break;
  471. ret = (*action)(val);
  472. } while (!ret && atomic_read(val) != 0);
  473. finish_wait(wq, &q->wait);
  474. return ret;
  475. }
  476. #define DEFINE_WAIT_ATOMIC_T(name, p) \
  477. struct wait_bit_queue name = { \
  478. .key = __WAIT_ATOMIC_T_KEY_INITIALIZER(p), \
  479. .wait = { \
  480. .private = current, \
  481. .func = wake_atomic_t_function, \
  482. .task_list = \
  483. LIST_HEAD_INIT((name).wait.task_list), \
  484. }, \
  485. }
  486. __sched int out_of_line_wait_on_atomic_t(atomic_t *p, int (*action)(atomic_t *),
  487. unsigned mode)
  488. {
  489. wait_queue_head_t *wq = atomic_t_waitqueue(p);
  490. DEFINE_WAIT_ATOMIC_T(wait, p);
  491. return __wait_on_atomic_t(wq, &wait, action, mode);
  492. }
  493. EXPORT_SYMBOL(out_of_line_wait_on_atomic_t);
  494. /**
  495. * wake_up_atomic_t - Wake up a waiter on a atomic_t
  496. * @p: The atomic_t being waited on, a kernel virtual address
  497. *
  498. * Wake up anyone waiting for the atomic_t to go to zero.
  499. *
  500. * Abuse the bit-waker function and its waitqueue hash table set (the atomic_t
  501. * check is done by the waiter's wake function, not the by the waker itself).
  502. */
  503. void wake_up_atomic_t(atomic_t *p)
  504. {
  505. __wake_up_bit(atomic_t_waitqueue(p), p, WAIT_ATOMIC_T_BIT_NR);
  506. }
  507. EXPORT_SYMBOL(wake_up_atomic_t);
  508. __sched int bit_wait(struct wait_bit_key *word, int mode)
  509. {
  510. schedule();
  511. if (signal_pending_state(mode, current))
  512. return -EINTR;
  513. return 0;
  514. }
  515. EXPORT_SYMBOL(bit_wait);
  516. __sched int bit_wait_io(struct wait_bit_key *word, int mode)
  517. {
  518. io_schedule();
  519. if (signal_pending_state(mode, current))
  520. return -EINTR;
  521. return 0;
  522. }
  523. EXPORT_SYMBOL(bit_wait_io);
  524. __sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
  525. {
  526. unsigned long now = READ_ONCE(jiffies);
  527. if (time_after_eq(now, word->timeout))
  528. return -EAGAIN;
  529. schedule_timeout(word->timeout - now);
  530. if (signal_pending_state(mode, current))
  531. return -EINTR;
  532. return 0;
  533. }
  534. EXPORT_SYMBOL_GPL(bit_wait_timeout);
  535. __sched int bit_wait_io_timeout(struct wait_bit_key *word, int mode)
  536. {
  537. unsigned long now = READ_ONCE(jiffies);
  538. if (time_after_eq(now, word->timeout))
  539. return -EAGAIN;
  540. io_schedule_timeout(word->timeout - now);
  541. if (signal_pending_state(mode, current))
  542. return -EINTR;
  543. return 0;
  544. }
  545. EXPORT_SYMBOL_GPL(bit_wait_io_timeout);