wait.c 20 KB

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