completion.c 9.6 KB

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