completion.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic wait-for-completion handler;
  4. *
  5. * It differs from semaphores in that their default case is the opposite,
  6. * wait_for_completion default blocks whereas semaphore default non-block. The
  7. * interface also makes it easy to 'complete' multiple waiting threads,
  8. * something which isn't entirely natural for semaphores.
  9. *
  10. * But more importantly, the primitive documents the usage. Semaphores would
  11. * typically be used for exclusion which gives rise to priority inversion.
  12. * Waiting for completion is a typically sync point, but not an exclusion point.
  13. */
  14. static void complete_with_flags(struct completion *x, int wake_flags)
  15. {
  16. unsigned long flags;
  17. raw_spin_lock_irqsave(&x->wait.lock, flags);
  18. if (x->done != UINT_MAX)
  19. x->done++;
  20. swake_up_locked(&x->wait, wake_flags);
  21. raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  22. }
  23. void complete_on_current_cpu(struct completion *x)
  24. {
  25. return complete_with_flags(x, WF_CURRENT_CPU);
  26. }
  27. /**
  28. * complete: - signals a single thread waiting on this completion
  29. * @x: holds the state of this particular completion
  30. *
  31. * This will wake up a single thread waiting on this completion. Threads will be
  32. * awakened in the same order in which they were queued.
  33. *
  34. * See also complete_all(), wait_for_completion() and related routines.
  35. *
  36. * If this function wakes up a task, it executes a full memory barrier before
  37. * accessing the task state.
  38. */
  39. void complete(struct completion *x)
  40. {
  41. complete_with_flags(x, 0);
  42. }
  43. EXPORT_SYMBOL(complete);
  44. /**
  45. * complete_all: - signals all threads waiting on this completion
  46. * @x: holds the state of this particular completion
  47. *
  48. * This will wake up all threads waiting on this particular completion event.
  49. *
  50. * If this function wakes up a task, it executes a full memory barrier before
  51. * accessing the task state.
  52. *
  53. * Since complete_all() sets the completion of @x permanently to done
  54. * to allow multiple waiters to finish, a call to reinit_completion()
  55. * must be used on @x if @x is to be used again. The code must make
  56. * sure that all waiters have woken and finished before reinitializing
  57. * @x. Also note that the function completion_done() can not be used
  58. * to know if there are still waiters after complete_all() has been called.
  59. */
  60. void complete_all(struct completion *x)
  61. {
  62. unsigned long flags;
  63. lockdep_assert_RT_in_threaded_ctx();
  64. raw_spin_lock_irqsave(&x->wait.lock, flags);
  65. x->done = UINT_MAX;
  66. swake_up_all_locked(&x->wait);
  67. raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  68. }
  69. EXPORT_SYMBOL(complete_all);
  70. static inline long __sched
  71. do_wait_for_common(struct completion *x,
  72. long (*action)(long), long timeout, int state)
  73. {
  74. if (!x->done) {
  75. DECLARE_SWAITQUEUE(wait);
  76. do {
  77. if (signal_pending_state(state, current)) {
  78. timeout = -ERESTARTSYS;
  79. break;
  80. }
  81. __prepare_to_swait(&x->wait, &wait);
  82. __set_current_state(state);
  83. raw_spin_unlock_irq(&x->wait.lock);
  84. timeout = action(timeout);
  85. raw_spin_lock_irq(&x->wait.lock);
  86. } while (!x->done && timeout);
  87. __finish_swait(&x->wait, &wait);
  88. if (!x->done)
  89. return timeout;
  90. }
  91. if (x->done != UINT_MAX)
  92. x->done--;
  93. return timeout ?: 1;
  94. }
  95. static inline long __sched
  96. __wait_for_common(struct completion *x,
  97. long (*action)(long), long timeout, int state)
  98. {
  99. might_sleep();
  100. complete_acquire(x);
  101. raw_spin_lock_irq(&x->wait.lock);
  102. timeout = do_wait_for_common(x, action, timeout, state);
  103. raw_spin_unlock_irq(&x->wait.lock);
  104. complete_release(x);
  105. return timeout;
  106. }
  107. static long __sched
  108. wait_for_common(struct completion *x, long timeout, int state)
  109. {
  110. return __wait_for_common(x, schedule_timeout, timeout, state);
  111. }
  112. static long __sched
  113. wait_for_common_io(struct completion *x, long timeout, int state)
  114. {
  115. return __wait_for_common(x, io_schedule_timeout, timeout, state);
  116. }
  117. /**
  118. * wait_for_completion: - waits for completion of a task
  119. * @x: holds the state of this particular completion
  120. *
  121. * This waits to be signaled for completion of a specific task. It is NOT
  122. * interruptible and there is no timeout.
  123. *
  124. * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
  125. * and interrupt capability. Also see complete().
  126. */
  127. void __sched wait_for_completion(struct completion *x)
  128. {
  129. wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  130. }
  131. EXPORT_SYMBOL(wait_for_completion);
  132. /**
  133. * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
  134. * @x: holds the state of this particular completion
  135. * @timeout: timeout value in jiffies
  136. *
  137. * This waits for either a completion of a specific task to be signaled or for a
  138. * specified timeout to expire. The timeout is in jiffies. It is not
  139. * interruptible.
  140. *
  141. * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
  142. * till timeout) if completed.
  143. */
  144. unsigned long __sched
  145. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  146. {
  147. return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
  148. }
  149. EXPORT_SYMBOL(wait_for_completion_timeout);
  150. /**
  151. * wait_for_completion_io: - waits for completion of a task
  152. * @x: holds the state of this particular completion
  153. *
  154. * This waits to be signaled for completion of a specific task. It is NOT
  155. * interruptible and there is no timeout. The caller is accounted as waiting
  156. * for IO (which traditionally means blkio only).
  157. */
  158. void __sched wait_for_completion_io(struct completion *x)
  159. {
  160. wait_for_common_io(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  161. }
  162. EXPORT_SYMBOL(wait_for_completion_io);
  163. /**
  164. * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
  165. * @x: holds the state of this particular completion
  166. * @timeout: timeout value in jiffies
  167. *
  168. * This waits for either a completion of a specific task to be signaled or for a
  169. * specified timeout to expire. The timeout is in jiffies. It is not
  170. * interruptible. The caller is accounted as waiting for IO (which traditionally
  171. * means blkio only).
  172. *
  173. * Return: 0 if timed out, and positive (at least 1, or number of jiffies left
  174. * till timeout) if completed.
  175. */
  176. unsigned long __sched
  177. wait_for_completion_io_timeout(struct completion *x, unsigned long timeout)
  178. {
  179. return wait_for_common_io(x, timeout, TASK_UNINTERRUPTIBLE);
  180. }
  181. EXPORT_SYMBOL(wait_for_completion_io_timeout);
  182. /**
  183. * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
  184. * @x: holds the state of this particular completion
  185. *
  186. * This waits for completion of a specific task to be signaled. It is
  187. * interruptible.
  188. *
  189. * Return: -ERESTARTSYS if interrupted, 0 if completed.
  190. */
  191. int __sched wait_for_completion_interruptible(struct completion *x)
  192. {
  193. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
  194. if (t == -ERESTARTSYS)
  195. return t;
  196. return 0;
  197. }
  198. EXPORT_SYMBOL(wait_for_completion_interruptible);
  199. /**
  200. * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
  201. * @x: holds the state of this particular completion
  202. * @timeout: timeout value in jiffies
  203. *
  204. * This waits for either a completion of a specific task to be signaled or for a
  205. * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  206. *
  207. * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
  208. * or number of jiffies left till timeout) if completed.
  209. */
  210. long __sched
  211. wait_for_completion_interruptible_timeout(struct completion *x,
  212. unsigned long timeout)
  213. {
  214. return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
  215. }
  216. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  217. /**
  218. * wait_for_completion_killable: - waits for completion of a task (killable)
  219. * @x: holds the state of this particular completion
  220. *
  221. * This waits to be signaled for completion of a specific task. It can be
  222. * interrupted by a kill signal.
  223. *
  224. * Return: -ERESTARTSYS if interrupted, 0 if completed.
  225. */
  226. int __sched wait_for_completion_killable(struct completion *x)
  227. {
  228. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
  229. if (t == -ERESTARTSYS)
  230. return t;
  231. return 0;
  232. }
  233. EXPORT_SYMBOL(wait_for_completion_killable);
  234. int __sched wait_for_completion_state(struct completion *x, unsigned int state)
  235. {
  236. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, state);
  237. if (t == -ERESTARTSYS)
  238. return t;
  239. return 0;
  240. }
  241. EXPORT_SYMBOL(wait_for_completion_state);
  242. /**
  243. * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
  244. * @x: holds the state of this particular completion
  245. * @timeout: timeout value in jiffies
  246. *
  247. * This waits for either a completion of a specific task to be
  248. * signaled or for a specified timeout to expire. It can be
  249. * interrupted by a kill signal. The timeout is in jiffies.
  250. *
  251. * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1,
  252. * or number of jiffies left till timeout) if completed.
  253. */
  254. long __sched
  255. wait_for_completion_killable_timeout(struct completion *x,
  256. unsigned long timeout)
  257. {
  258. return wait_for_common(x, timeout, TASK_KILLABLE);
  259. }
  260. EXPORT_SYMBOL(wait_for_completion_killable_timeout);
  261. /**
  262. * try_wait_for_completion - try to decrement a completion without blocking
  263. * @x: completion structure
  264. *
  265. * Return: 0 if a decrement cannot be done without blocking
  266. * 1 if a decrement succeeded.
  267. *
  268. * If a completion is being used as a counting completion,
  269. * attempt to decrement the counter without blocking. This
  270. * enables us to avoid waiting if the resource the completion
  271. * is protecting is not available.
  272. */
  273. bool try_wait_for_completion(struct completion *x)
  274. {
  275. unsigned long flags;
  276. bool ret = true;
  277. /*
  278. * Since x->done will need to be locked only
  279. * in the non-blocking case, we check x->done
  280. * first without taking the lock so we can
  281. * return early in the blocking case.
  282. */
  283. if (!READ_ONCE(x->done))
  284. return false;
  285. raw_spin_lock_irqsave(&x->wait.lock, flags);
  286. if (!x->done)
  287. ret = false;
  288. else if (x->done != UINT_MAX)
  289. x->done--;
  290. raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  291. return ret;
  292. }
  293. EXPORT_SYMBOL(try_wait_for_completion);
  294. /**
  295. * completion_done - Test to see if a completion has any waiters
  296. * @x: completion structure
  297. *
  298. * Return: 0 if there are waiters (wait_for_completion() in progress)
  299. * 1 if there are no waiters.
  300. *
  301. * Note, this will always return true if complete_all() was called on @X.
  302. */
  303. bool completion_done(struct completion *x)
  304. {
  305. unsigned long flags;
  306. if (!READ_ONCE(x->done))
  307. return false;
  308. /*
  309. * If ->done, we need to wait for complete() to release ->wait.lock
  310. * otherwise we can end up freeing the completion before complete()
  311. * is done referencing it.
  312. */
  313. raw_spin_lock_irqsave(&x->wait.lock, flags);
  314. raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  315. return true;
  316. }
  317. EXPORT_SYMBOL(completion_done);