wait.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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->head);
  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->head, entry) {
  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->entry))
  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->entry))
  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->entry);
  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->entry);
  201. ret = -ERESTARTSYS;
  202. } else {
  203. if (list_empty(&wq_entry->entry)) {
  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->entry)))
  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->entry)))
  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->entry)) {
  275. spin_lock_irqsave(&wq_head->lock, flags);
  276. list_del_init(&wq_entry->entry);
  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->entry);
  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);