wait.c 13 KB

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