wait.c 14 KB

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