closure.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Asynchronous refcounty things
  4. *
  5. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  6. * Copyright 2012 Google, Inc.
  7. */
  8. #include <linux/closure.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/export.h>
  11. #include <linux/rcupdate.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/sched/debug.h>
  14. static inline void closure_put_after_sub_checks(int flags)
  15. {
  16. int r = flags & CLOSURE_REMAINING_MASK;
  17. if (WARN(flags & CLOSURE_GUARD_MASK,
  18. "closure has guard bits set: %x (%u)",
  19. flags & CLOSURE_GUARD_MASK, (unsigned) __fls(r)))
  20. r &= ~CLOSURE_GUARD_MASK;
  21. WARN(!r && (flags & ~CLOSURE_DESTRUCTOR),
  22. "closure ref hit 0 with incorrect flags set: %x (%u)",
  23. flags & ~CLOSURE_DESTRUCTOR, (unsigned) __fls(flags));
  24. }
  25. static inline void closure_put_after_sub(struct closure *cl, int flags)
  26. {
  27. closure_put_after_sub_checks(flags);
  28. if (!(flags & CLOSURE_REMAINING_MASK)) {
  29. smp_acquire__after_ctrl_dep();
  30. cl->closure_get_happened = false;
  31. if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
  32. atomic_set(&cl->remaining,
  33. CLOSURE_REMAINING_INITIALIZER);
  34. closure_queue(cl);
  35. } else {
  36. struct closure *parent = cl->parent;
  37. closure_fn *destructor = cl->fn;
  38. closure_debug_destroy(cl);
  39. if (destructor)
  40. destructor(&cl->work);
  41. if (parent)
  42. closure_put(parent);
  43. }
  44. }
  45. }
  46. /* For clearing flags with the same atomic op as a put */
  47. void closure_sub(struct closure *cl, int v)
  48. {
  49. closure_put_after_sub(cl, atomic_sub_return_release(v, &cl->remaining));
  50. }
  51. EXPORT_SYMBOL(closure_sub);
  52. /*
  53. * closure_put - decrement a closure's refcount
  54. */
  55. void closure_put(struct closure *cl)
  56. {
  57. closure_put_after_sub(cl, atomic_dec_return_release(&cl->remaining));
  58. }
  59. EXPORT_SYMBOL(closure_put);
  60. /*
  61. * closure_wake_up - wake up all closures on a wait list, without memory barrier
  62. */
  63. void __closure_wake_up(struct closure_waitlist *wait_list)
  64. {
  65. struct llist_node *list;
  66. struct closure *cl, *t;
  67. struct llist_node *reverse = NULL;
  68. list = llist_del_all(&wait_list->list);
  69. /* We first reverse the list to preserve FIFO ordering and fairness */
  70. reverse = llist_reverse_order(list);
  71. /* Then do the wakeups */
  72. llist_for_each_entry_safe(cl, t, reverse, list) {
  73. closure_set_waiting(cl, 0);
  74. closure_sub(cl, CLOSURE_WAITING + 1);
  75. }
  76. }
  77. EXPORT_SYMBOL(__closure_wake_up);
  78. /**
  79. * closure_wait - add a closure to a waitlist
  80. * @waitlist: will own a ref on @cl, which will be released when
  81. * closure_wake_up() is called on @waitlist.
  82. * @cl: closure pointer.
  83. *
  84. */
  85. bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
  86. {
  87. if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
  88. return false;
  89. cl->closure_get_happened = true;
  90. closure_set_waiting(cl, _RET_IP_);
  91. atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
  92. llist_add(&cl->list, &waitlist->list);
  93. return true;
  94. }
  95. EXPORT_SYMBOL(closure_wait);
  96. struct closure_syncer {
  97. struct task_struct *task;
  98. int done;
  99. };
  100. static CLOSURE_CALLBACK(closure_sync_fn)
  101. {
  102. struct closure *cl = container_of(ws, struct closure, work);
  103. struct closure_syncer *s = cl->s;
  104. struct task_struct *p;
  105. rcu_read_lock();
  106. p = READ_ONCE(s->task);
  107. s->done = 1;
  108. wake_up_process(p);
  109. rcu_read_unlock();
  110. }
  111. void __sched __closure_sync(struct closure *cl)
  112. {
  113. struct closure_syncer s = { .task = current };
  114. cl->s = &s;
  115. continue_at(cl, closure_sync_fn, NULL);
  116. while (1) {
  117. set_current_state(TASK_UNINTERRUPTIBLE);
  118. if (s.done)
  119. break;
  120. schedule();
  121. }
  122. __set_current_state(TASK_RUNNING);
  123. }
  124. EXPORT_SYMBOL(__closure_sync);
  125. /*
  126. * closure_return_sync - finish running a closure, synchronously (i.e. waiting
  127. * for outstanding get()s to finish) and returning once closure refcount is 0.
  128. *
  129. * Unlike closure_sync() this doesn't reinit the ref to 1; subsequent
  130. * closure_get_not_zero() calls waill fail.
  131. */
  132. void __sched closure_return_sync(struct closure *cl)
  133. {
  134. struct closure_syncer s = { .task = current };
  135. cl->s = &s;
  136. set_closure_fn(cl, closure_sync_fn, NULL);
  137. unsigned flags = atomic_sub_return_release(1 + CLOSURE_RUNNING - CLOSURE_DESTRUCTOR,
  138. &cl->remaining);
  139. closure_put_after_sub_checks(flags);
  140. if (unlikely(flags & CLOSURE_REMAINING_MASK)) {
  141. while (1) {
  142. set_current_state(TASK_UNINTERRUPTIBLE);
  143. if (s.done)
  144. break;
  145. schedule();
  146. }
  147. __set_current_state(TASK_RUNNING);
  148. }
  149. if (cl->parent)
  150. closure_put(cl->parent);
  151. }
  152. EXPORT_SYMBOL(closure_return_sync);
  153. int __sched __closure_sync_timeout(struct closure *cl, unsigned long timeout)
  154. {
  155. struct closure_syncer s = { .task = current };
  156. int ret = 0;
  157. cl->s = &s;
  158. continue_at(cl, closure_sync_fn, NULL);
  159. while (1) {
  160. set_current_state(TASK_UNINTERRUPTIBLE);
  161. if (s.done)
  162. break;
  163. if (!timeout) {
  164. /*
  165. * Carefully undo the continue_at() - but only if it
  166. * hasn't completed, i.e. the final closure_put() hasn't
  167. * happened yet:
  168. */
  169. unsigned old, new, v = atomic_read(&cl->remaining);
  170. do {
  171. old = v;
  172. if (!old || (old & CLOSURE_RUNNING))
  173. goto success;
  174. new = old + CLOSURE_REMAINING_INITIALIZER;
  175. } while ((v = atomic_cmpxchg(&cl->remaining, old, new)) != old);
  176. ret = -ETIME;
  177. }
  178. timeout = schedule_timeout(timeout);
  179. }
  180. success:
  181. __set_current_state(TASK_RUNNING);
  182. return ret;
  183. }
  184. EXPORT_SYMBOL(__closure_sync_timeout);
  185. #ifdef CONFIG_DEBUG_CLOSURES
  186. static LIST_HEAD(closure_list);
  187. static DEFINE_SPINLOCK(closure_list_lock);
  188. void closure_debug_create(struct closure *cl)
  189. {
  190. unsigned long flags;
  191. BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
  192. cl->magic = CLOSURE_MAGIC_ALIVE;
  193. spin_lock_irqsave(&closure_list_lock, flags);
  194. list_add(&cl->all, &closure_list);
  195. spin_unlock_irqrestore(&closure_list_lock, flags);
  196. }
  197. EXPORT_SYMBOL(closure_debug_create);
  198. void closure_debug_destroy(struct closure *cl)
  199. {
  200. unsigned long flags;
  201. if (cl->magic == CLOSURE_MAGIC_STACK)
  202. return;
  203. BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
  204. cl->magic = CLOSURE_MAGIC_DEAD;
  205. spin_lock_irqsave(&closure_list_lock, flags);
  206. list_del(&cl->all);
  207. spin_unlock_irqrestore(&closure_list_lock, flags);
  208. }
  209. EXPORT_SYMBOL(closure_debug_destroy);
  210. static int debug_show(struct seq_file *f, void *data)
  211. {
  212. struct closure *cl;
  213. spin_lock_irq(&closure_list_lock);
  214. list_for_each_entry(cl, &closure_list, all) {
  215. int r = atomic_read(&cl->remaining);
  216. seq_printf(f, "%p: %pS -> %pS p %p r %i ",
  217. cl, (void *) cl->ip, cl->fn, cl->parent,
  218. r & CLOSURE_REMAINING_MASK);
  219. seq_printf(f, "%s%s\n",
  220. test_bit(WORK_STRUCT_PENDING_BIT,
  221. work_data_bits(&cl->work)) ? "Q" : "",
  222. r & CLOSURE_RUNNING ? "R" : "");
  223. if (r & CLOSURE_WAITING)
  224. seq_printf(f, " W %pS\n",
  225. (void *) cl->waiting_on);
  226. seq_putc(f, '\n');
  227. }
  228. spin_unlock_irq(&closure_list_lock);
  229. return 0;
  230. }
  231. DEFINE_SHOW_ATTRIBUTE(debug);
  232. static int __init closure_debug_init(void)
  233. {
  234. debugfs_create_file("closures", 0400, NULL, NULL, &debug_fops);
  235. return 0;
  236. }
  237. late_initcall(closure_debug_init)
  238. #endif