wait.c 13 KB

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