completion.c 10 KB

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