closure.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Asynchronous refcounty things
  3. *
  4. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  5. * Copyright 2012 Google, Inc.
  6. */
  7. #include <linux/debugfs.h>
  8. #include <linux/module.h>
  9. #include <linux/seq_file.h>
  10. #include "closure.h"
  11. void closure_queue(struct closure *cl)
  12. {
  13. struct workqueue_struct *wq = cl->wq;
  14. if (wq) {
  15. INIT_WORK(&cl->work, cl->work.func);
  16. BUG_ON(!queue_work(wq, &cl->work));
  17. } else
  18. cl->fn(cl);
  19. }
  20. EXPORT_SYMBOL_GPL(closure_queue);
  21. #define CL_FIELD(type, field) \
  22. case TYPE_ ## type: \
  23. return &container_of(cl, struct type, cl)->field
  24. static struct closure_waitlist *closure_waitlist(struct closure *cl)
  25. {
  26. switch (cl->type) {
  27. CL_FIELD(closure_with_waitlist, wait);
  28. CL_FIELD(closure_with_waitlist_and_timer, wait);
  29. default:
  30. return NULL;
  31. }
  32. }
  33. static struct timer_list *closure_timer(struct closure *cl)
  34. {
  35. switch (cl->type) {
  36. CL_FIELD(closure_with_timer, timer);
  37. CL_FIELD(closure_with_waitlist_and_timer, timer);
  38. default:
  39. return NULL;
  40. }
  41. }
  42. static inline void closure_put_after_sub(struct closure *cl, int flags)
  43. {
  44. int r = flags & CLOSURE_REMAINING_MASK;
  45. BUG_ON(flags & CLOSURE_GUARD_MASK);
  46. BUG_ON(!r && (flags & ~(CLOSURE_DESTRUCTOR|CLOSURE_BLOCKING)));
  47. /* Must deliver precisely one wakeup */
  48. if (r == 1 && (flags & CLOSURE_SLEEPING))
  49. wake_up_process(cl->task);
  50. if (!r) {
  51. if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
  52. /* CLOSURE_BLOCKING might be set - clear it */
  53. atomic_set(&cl->remaining,
  54. CLOSURE_REMAINING_INITIALIZER);
  55. closure_queue(cl);
  56. } else {
  57. struct closure *parent = cl->parent;
  58. struct closure_waitlist *wait = closure_waitlist(cl);
  59. closure_debug_destroy(cl);
  60. atomic_set(&cl->remaining, -1);
  61. if (wait)
  62. closure_wake_up(wait);
  63. if (cl->fn)
  64. cl->fn(cl);
  65. if (parent)
  66. closure_put(parent);
  67. }
  68. }
  69. }
  70. /* For clearing flags with the same atomic op as a put */
  71. void closure_sub(struct closure *cl, int v)
  72. {
  73. closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
  74. }
  75. EXPORT_SYMBOL_GPL(closure_sub);
  76. void closure_put(struct closure *cl)
  77. {
  78. closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
  79. }
  80. EXPORT_SYMBOL_GPL(closure_put);
  81. static void set_waiting(struct closure *cl, unsigned long f)
  82. {
  83. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  84. cl->waiting_on = f;
  85. #endif
  86. }
  87. void __closure_wake_up(struct closure_waitlist *wait_list)
  88. {
  89. struct llist_node *list;
  90. struct closure *cl;
  91. struct llist_node *reverse = NULL;
  92. list = llist_del_all(&wait_list->list);
  93. /* We first reverse the list to preserve FIFO ordering and fairness */
  94. while (list) {
  95. struct llist_node *t = list;
  96. list = llist_next(list);
  97. t->next = reverse;
  98. reverse = t;
  99. }
  100. /* Then do the wakeups */
  101. while (reverse) {
  102. cl = container_of(reverse, struct closure, list);
  103. reverse = llist_next(reverse);
  104. set_waiting(cl, 0);
  105. closure_sub(cl, CLOSURE_WAITING + 1);
  106. }
  107. }
  108. EXPORT_SYMBOL_GPL(__closure_wake_up);
  109. bool closure_wait(struct closure_waitlist *list, struct closure *cl)
  110. {
  111. if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
  112. return false;
  113. set_waiting(cl, _RET_IP_);
  114. atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
  115. llist_add(&cl->list, &list->list);
  116. return true;
  117. }
  118. EXPORT_SYMBOL_GPL(closure_wait);
  119. /**
  120. * closure_sync() - sleep until a closure a closure has nothing left to wait on
  121. *
  122. * Sleeps until the refcount hits 1 - the thread that's running the closure owns
  123. * the last refcount.
  124. */
  125. void closure_sync(struct closure *cl)
  126. {
  127. while (1) {
  128. __closure_start_sleep(cl);
  129. closure_set_ret_ip(cl);
  130. if ((atomic_read(&cl->remaining) &
  131. CLOSURE_REMAINING_MASK) == 1)
  132. break;
  133. schedule();
  134. }
  135. __closure_end_sleep(cl);
  136. }
  137. EXPORT_SYMBOL_GPL(closure_sync);
  138. /**
  139. * closure_trylock() - try to acquire the closure, without waiting
  140. * @cl: closure to lock
  141. *
  142. * Returns true if the closure was succesfully locked.
  143. */
  144. bool closure_trylock(struct closure *cl, struct closure *parent)
  145. {
  146. if (atomic_cmpxchg(&cl->remaining, -1,
  147. CLOSURE_REMAINING_INITIALIZER) != -1)
  148. return false;
  149. closure_set_ret_ip(cl);
  150. smp_mb();
  151. cl->parent = parent;
  152. if (parent)
  153. closure_get(parent);
  154. closure_debug_create(cl);
  155. return true;
  156. }
  157. EXPORT_SYMBOL_GPL(closure_trylock);
  158. void __closure_lock(struct closure *cl, struct closure *parent,
  159. struct closure_waitlist *wait_list)
  160. {
  161. struct closure wait;
  162. closure_init_stack(&wait);
  163. while (1) {
  164. if (closure_trylock(cl, parent))
  165. return;
  166. closure_wait_event_sync(wait_list, &wait,
  167. atomic_read(&cl->remaining) == -1);
  168. }
  169. }
  170. EXPORT_SYMBOL_GPL(__closure_lock);
  171. static void closure_delay_timer_fn(unsigned long data)
  172. {
  173. struct closure *cl = (struct closure *) data;
  174. closure_sub(cl, CLOSURE_TIMER + 1);
  175. }
  176. void do_closure_timer_init(struct closure *cl)
  177. {
  178. struct timer_list *timer = closure_timer(cl);
  179. init_timer(timer);
  180. timer->data = (unsigned long) cl;
  181. timer->function = closure_delay_timer_fn;
  182. }
  183. EXPORT_SYMBOL_GPL(do_closure_timer_init);
  184. bool __closure_delay(struct closure *cl, unsigned long delay,
  185. struct timer_list *timer)
  186. {
  187. if (atomic_read(&cl->remaining) & CLOSURE_TIMER)
  188. return false;
  189. BUG_ON(timer_pending(timer));
  190. timer->expires = jiffies + delay;
  191. atomic_add(CLOSURE_TIMER + 1, &cl->remaining);
  192. add_timer(timer);
  193. return true;
  194. }
  195. EXPORT_SYMBOL_GPL(__closure_delay);
  196. void __closure_flush(struct closure *cl, struct timer_list *timer)
  197. {
  198. if (del_timer(timer))
  199. closure_sub(cl, CLOSURE_TIMER + 1);
  200. }
  201. EXPORT_SYMBOL_GPL(__closure_flush);
  202. void __closure_flush_sync(struct closure *cl, struct timer_list *timer)
  203. {
  204. if (del_timer_sync(timer))
  205. closure_sub(cl, CLOSURE_TIMER + 1);
  206. }
  207. EXPORT_SYMBOL_GPL(__closure_flush_sync);
  208. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  209. static LIST_HEAD(closure_list);
  210. static DEFINE_SPINLOCK(closure_list_lock);
  211. void closure_debug_create(struct closure *cl)
  212. {
  213. unsigned long flags;
  214. BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
  215. cl->magic = CLOSURE_MAGIC_ALIVE;
  216. spin_lock_irqsave(&closure_list_lock, flags);
  217. list_add(&cl->all, &closure_list);
  218. spin_unlock_irqrestore(&closure_list_lock, flags);
  219. }
  220. EXPORT_SYMBOL_GPL(closure_debug_create);
  221. void closure_debug_destroy(struct closure *cl)
  222. {
  223. unsigned long flags;
  224. BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
  225. cl->magic = CLOSURE_MAGIC_DEAD;
  226. spin_lock_irqsave(&closure_list_lock, flags);
  227. list_del(&cl->all);
  228. spin_unlock_irqrestore(&closure_list_lock, flags);
  229. }
  230. EXPORT_SYMBOL_GPL(closure_debug_destroy);
  231. static struct dentry *debug;
  232. #define work_data_bits(work) ((unsigned long *)(&(work)->data))
  233. static int debug_seq_show(struct seq_file *f, void *data)
  234. {
  235. struct closure *cl;
  236. spin_lock_irq(&closure_list_lock);
  237. list_for_each_entry(cl, &closure_list, all) {
  238. int r = atomic_read(&cl->remaining);
  239. seq_printf(f, "%p: %pF -> %pf p %p r %i ",
  240. cl, (void *) cl->ip, cl->fn, cl->parent,
  241. r & CLOSURE_REMAINING_MASK);
  242. seq_printf(f, "%s%s%s%s%s%s\n",
  243. test_bit(WORK_STRUCT_PENDING,
  244. work_data_bits(&cl->work)) ? "Q" : "",
  245. r & CLOSURE_RUNNING ? "R" : "",
  246. r & CLOSURE_BLOCKING ? "B" : "",
  247. r & CLOSURE_STACK ? "S" : "",
  248. r & CLOSURE_SLEEPING ? "Sl" : "",
  249. r & CLOSURE_TIMER ? "T" : "");
  250. if (r & CLOSURE_WAITING)
  251. seq_printf(f, " W %pF\n",
  252. (void *) cl->waiting_on);
  253. seq_printf(f, "\n");
  254. }
  255. spin_unlock_irq(&closure_list_lock);
  256. return 0;
  257. }
  258. static int debug_seq_open(struct inode *inode, struct file *file)
  259. {
  260. return single_open(file, debug_seq_show, NULL);
  261. }
  262. static const struct file_operations debug_ops = {
  263. .owner = THIS_MODULE,
  264. .open = debug_seq_open,
  265. .read = seq_read,
  266. .release = single_release
  267. };
  268. void __init closure_debug_init(void)
  269. {
  270. debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);
  271. }
  272. #endif
  273. MODULE_AUTHOR("Kent Overstreet <koverstreet@google.com>");
  274. MODULE_LICENSE("GPL");