wait.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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(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. * Scan threshold to break wait queue walk.
  49. * This allows a waker to take a break from holding the
  50. * wait queue lock during the wait queue walk.
  51. */
  52. #define WAITQUEUE_WALK_BREAK_CNT 64
  53. /*
  54. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  55. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  56. * number) then we wake all the non-exclusive tasks and one exclusive task.
  57. *
  58. * There are circumstances in which we can try to wake a task which has already
  59. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  60. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  61. */
  62. static int __wake_up_common(struct wait_queue_head *wq_head, unsigned int mode,
  63. int nr_exclusive, int wake_flags, void *key,
  64. wait_queue_entry_t *bookmark)
  65. {
  66. wait_queue_entry_t *curr, *next;
  67. int cnt = 0;
  68. if (bookmark && (bookmark->flags & WQ_FLAG_BOOKMARK)) {
  69. curr = list_next_entry(bookmark, entry);
  70. list_del(&bookmark->entry);
  71. bookmark->flags = 0;
  72. } else
  73. curr = list_first_entry(&wq_head->head, wait_queue_entry_t, entry);
  74. if (&curr->entry == &wq_head->head)
  75. return nr_exclusive;
  76. list_for_each_entry_safe_from(curr, next, &wq_head->head, entry) {
  77. unsigned flags = curr->flags;
  78. int ret;
  79. if (flags & WQ_FLAG_BOOKMARK)
  80. continue;
  81. ret = curr->func(curr, mode, wake_flags, key);
  82. if (ret < 0)
  83. break;
  84. if (ret && (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  85. break;
  86. if (bookmark && (++cnt > WAITQUEUE_WALK_BREAK_CNT) &&
  87. (&next->entry != &wq_head->head)) {
  88. bookmark->flags = WQ_FLAG_BOOKMARK;
  89. list_add_tail(&bookmark->entry, &next->entry);
  90. break;
  91. }
  92. }
  93. return nr_exclusive;
  94. }
  95. static void __wake_up_common_lock(struct wait_queue_head *wq_head, unsigned int mode,
  96. int nr_exclusive, int wake_flags, void *key)
  97. {
  98. unsigned long flags;
  99. wait_queue_entry_t bookmark;
  100. bookmark.flags = 0;
  101. bookmark.private = NULL;
  102. bookmark.func = NULL;
  103. INIT_LIST_HEAD(&bookmark.entry);
  104. spin_lock_irqsave(&wq_head->lock, flags);
  105. nr_exclusive = __wake_up_common(wq_head, mode, nr_exclusive, wake_flags, key, &bookmark);
  106. spin_unlock_irqrestore(&wq_head->lock, flags);
  107. while (bookmark.flags & WQ_FLAG_BOOKMARK) {
  108. spin_lock_irqsave(&wq_head->lock, flags);
  109. nr_exclusive = __wake_up_common(wq_head, mode, nr_exclusive,
  110. wake_flags, key, &bookmark);
  111. spin_unlock_irqrestore(&wq_head->lock, flags);
  112. }
  113. }
  114. /**
  115. * __wake_up - wake up threads blocked on a waitqueue.
  116. * @wq_head: the waitqueue
  117. * @mode: which threads
  118. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  119. * @key: is directly passed to the wakeup function
  120. *
  121. * It may be assumed that this function implies a write memory barrier before
  122. * changing the task state if and only if any tasks are woken up.
  123. */
  124. void __wake_up(struct wait_queue_head *wq_head, unsigned int mode,
  125. int nr_exclusive, void *key)
  126. {
  127. __wake_up_common_lock(wq_head, mode, nr_exclusive, 0, key);
  128. }
  129. EXPORT_SYMBOL(__wake_up);
  130. /*
  131. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  132. */
  133. void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr)
  134. {
  135. __wake_up_common(wq_head, mode, nr, 0, NULL, NULL);
  136. }
  137. EXPORT_SYMBOL_GPL(__wake_up_locked);
  138. void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key)
  139. {
  140. __wake_up_common(wq_head, mode, 1, 0, key, NULL);
  141. }
  142. EXPORT_SYMBOL_GPL(__wake_up_locked_key);
  143. void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
  144. unsigned int mode, void *key, wait_queue_entry_t *bookmark)
  145. {
  146. __wake_up_common(wq_head, mode, 1, 0, key, bookmark);
  147. }
  148. EXPORT_SYMBOL_GPL(__wake_up_locked_key_bookmark);
  149. /**
  150. * __wake_up_sync_key - wake up threads blocked on a waitqueue.
  151. * @wq_head: the waitqueue
  152. * @mode: which threads
  153. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  154. * @key: opaque value to be passed to wakeup targets
  155. *
  156. * The sync wakeup differs that the waker knows that it will schedule
  157. * away soon, so while the target thread will be woken up, it will not
  158. * be migrated to another CPU - ie. the two threads are 'synchronized'
  159. * with each other. This can prevent needless bouncing between CPUs.
  160. *
  161. * On UP it can prevent extra preemption.
  162. *
  163. * It may be assumed that this function implies a write memory barrier before
  164. * changing the task state if and only if any tasks are woken up.
  165. */
  166. void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode,
  167. int nr_exclusive, void *key)
  168. {
  169. int wake_flags = 1; /* XXX WF_SYNC */
  170. if (unlikely(!wq_head))
  171. return;
  172. if (unlikely(nr_exclusive != 1))
  173. wake_flags = 0;
  174. __wake_up_common_lock(wq_head, mode, nr_exclusive, wake_flags, key);
  175. }
  176. EXPORT_SYMBOL_GPL(__wake_up_sync_key);
  177. /*
  178. * __wake_up_sync - see __wake_up_sync_key()
  179. */
  180. void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr_exclusive)
  181. {
  182. __wake_up_sync_key(wq_head, mode, nr_exclusive, NULL);
  183. }
  184. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  185. /*
  186. * Note: we use "set_current_state()" _after_ the wait-queue add,
  187. * because we need a memory barrier there on SMP, so that any
  188. * wake-function that tests for the wait-queue being active
  189. * will be guaranteed to see waitqueue addition _or_ subsequent
  190. * tests in this thread will see the wakeup having taken place.
  191. *
  192. * The spin_unlock() itself is semi-permeable and only protects
  193. * one way (it only protects stuff inside the critical region and
  194. * stops them from bleeding out - it would still allow subsequent
  195. * loads to move into the critical region).
  196. */
  197. void
  198. prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
  199. {
  200. unsigned long flags;
  201. wq_entry->flags &= ~WQ_FLAG_EXCLUSIVE;
  202. spin_lock_irqsave(&wq_head->lock, flags);
  203. if (list_empty(&wq_entry->entry))
  204. __add_wait_queue(wq_head, wq_entry);
  205. set_current_state(state);
  206. spin_unlock_irqrestore(&wq_head->lock, flags);
  207. }
  208. EXPORT_SYMBOL(prepare_to_wait);
  209. void
  210. prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
  211. {
  212. unsigned long flags;
  213. wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
  214. spin_lock_irqsave(&wq_head->lock, flags);
  215. if (list_empty(&wq_entry->entry))
  216. __add_wait_queue_entry_tail(wq_head, wq_entry);
  217. set_current_state(state);
  218. spin_unlock_irqrestore(&wq_head->lock, flags);
  219. }
  220. EXPORT_SYMBOL(prepare_to_wait_exclusive);
  221. void init_wait_entry(struct wait_queue_entry *wq_entry, int flags)
  222. {
  223. wq_entry->flags = flags;
  224. wq_entry->private = current;
  225. wq_entry->func = autoremove_wake_function;
  226. INIT_LIST_HEAD(&wq_entry->entry);
  227. }
  228. EXPORT_SYMBOL(init_wait_entry);
  229. long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state)
  230. {
  231. unsigned long flags;
  232. long ret = 0;
  233. spin_lock_irqsave(&wq_head->lock, flags);
  234. if (unlikely(signal_pending_state(state, current))) {
  235. /*
  236. * Exclusive waiter must not fail if it was selected by wakeup,
  237. * it should "consume" the condition we were waiting for.
  238. *
  239. * The caller will recheck the condition and return success if
  240. * we were already woken up, we can not miss the event because
  241. * wakeup locks/unlocks the same wq_head->lock.
  242. *
  243. * But we need to ensure that set-condition + wakeup after that
  244. * can't see us, it should wake up another exclusive waiter if
  245. * we fail.
  246. */
  247. list_del_init(&wq_entry->entry);
  248. ret = -ERESTARTSYS;
  249. } else {
  250. if (list_empty(&wq_entry->entry)) {
  251. if (wq_entry->flags & WQ_FLAG_EXCLUSIVE)
  252. __add_wait_queue_entry_tail(wq_head, wq_entry);
  253. else
  254. __add_wait_queue(wq_head, wq_entry);
  255. }
  256. set_current_state(state);
  257. }
  258. spin_unlock_irqrestore(&wq_head->lock, flags);
  259. return ret;
  260. }
  261. EXPORT_SYMBOL(prepare_to_wait_event);
  262. /*
  263. * Note! These two wait functions are entered with the
  264. * wait-queue lock held (and interrupts off in the _irq
  265. * case), so there is no race with testing the wakeup
  266. * condition in the caller before they add the wait
  267. * entry to the wake queue.
  268. */
  269. int do_wait_intr(wait_queue_head_t *wq, wait_queue_entry_t *wait)
  270. {
  271. if (likely(list_empty(&wait->entry)))
  272. __add_wait_queue_entry_tail(wq, wait);
  273. set_current_state(TASK_INTERRUPTIBLE);
  274. if (signal_pending(current))
  275. return -ERESTARTSYS;
  276. spin_unlock(&wq->lock);
  277. schedule();
  278. spin_lock(&wq->lock);
  279. return 0;
  280. }
  281. EXPORT_SYMBOL(do_wait_intr);
  282. int do_wait_intr_irq(wait_queue_head_t *wq, wait_queue_entry_t *wait)
  283. {
  284. if (likely(list_empty(&wait->entry)))
  285. __add_wait_queue_entry_tail(wq, wait);
  286. set_current_state(TASK_INTERRUPTIBLE);
  287. if (signal_pending(current))
  288. return -ERESTARTSYS;
  289. spin_unlock_irq(&wq->lock);
  290. schedule();
  291. spin_lock_irq(&wq->lock);
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(do_wait_intr_irq);
  295. /**
  296. * finish_wait - clean up after waiting in a queue
  297. * @wq_head: waitqueue waited on
  298. * @wq_entry: wait descriptor
  299. *
  300. * Sets current thread back to running state and removes
  301. * the wait descriptor from the given waitqueue if still
  302. * queued.
  303. */
  304. void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
  305. {
  306. unsigned long flags;
  307. __set_current_state(TASK_RUNNING);
  308. /*
  309. * We can check for list emptiness outside the lock
  310. * IFF:
  311. * - we use the "careful" check that verifies both
  312. * the next and prev pointers, so that there cannot
  313. * be any half-pending updates in progress on other
  314. * CPU's that we haven't seen yet (and that might
  315. * still change the stack area.
  316. * and
  317. * - all other users take the lock (ie we can only
  318. * have _one_ other CPU that looks at or modifies
  319. * the list).
  320. */
  321. if (!list_empty_careful(&wq_entry->entry)) {
  322. spin_lock_irqsave(&wq_head->lock, flags);
  323. list_del_init(&wq_entry->entry);
  324. spin_unlock_irqrestore(&wq_head->lock, flags);
  325. }
  326. }
  327. EXPORT_SYMBOL(finish_wait);
  328. int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key)
  329. {
  330. int ret = default_wake_function(wq_entry, mode, sync, key);
  331. if (ret)
  332. list_del_init(&wq_entry->entry);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL(autoremove_wake_function);
  336. static inline bool is_kthread_should_stop(void)
  337. {
  338. return (current->flags & PF_KTHREAD) && kthread_should_stop();
  339. }
  340. /*
  341. * DEFINE_WAIT_FUNC(wait, woken_wake_func);
  342. *
  343. * add_wait_queue(&wq_head, &wait);
  344. * for (;;) {
  345. * if (condition)
  346. * break;
  347. *
  348. * // in wait_woken() // in woken_wake_function()
  349. *
  350. * p->state = mode; wq_entry->flags |= WQ_FLAG_WOKEN;
  351. * smp_mb(); // A try_to_wake_up():
  352. * if (!(wq_entry->flags & WQ_FLAG_WOKEN)) <full barrier>
  353. * schedule() if (p->state & mode)
  354. * p->state = TASK_RUNNING; p->state = TASK_RUNNING;
  355. * wq_entry->flags &= ~WQ_FLAG_WOKEN; ~~~~~~~~~~~~~~~~~~
  356. * smp_mb(); // B condition = true;
  357. * } smp_mb(); // C
  358. * remove_wait_queue(&wq_head, &wait); wq_entry->flags |= WQ_FLAG_WOKEN;
  359. */
  360. long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout)
  361. {
  362. /*
  363. * The below executes an smp_mb(), which matches with the full barrier
  364. * executed by the try_to_wake_up() in woken_wake_function() such that
  365. * either we see the store to wq_entry->flags in woken_wake_function()
  366. * or woken_wake_function() sees our store to current->state.
  367. */
  368. set_current_state(mode); /* A */
  369. if (!(wq_entry->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop())
  370. timeout = schedule_timeout(timeout);
  371. __set_current_state(TASK_RUNNING);
  372. /*
  373. * The below executes an smp_mb(), which matches with the smp_mb() (C)
  374. * in woken_wake_function() such that either we see the wait condition
  375. * being true or the store to wq_entry->flags in woken_wake_function()
  376. * follows ours in the coherence order.
  377. */
  378. smp_store_mb(wq_entry->flags, wq_entry->flags & ~WQ_FLAG_WOKEN); /* B */
  379. return timeout;
  380. }
  381. EXPORT_SYMBOL(wait_woken);
  382. int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key)
  383. {
  384. /* Pairs with the smp_store_mb() in wait_woken(). */
  385. smp_mb(); /* C */
  386. wq_entry->flags |= WQ_FLAG_WOKEN;
  387. return default_wake_function(wq_entry, mode, sync, key);
  388. }
  389. EXPORT_SYMBOL(woken_wake_function);