closure.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_fn *destructor = cl->fn;
  60. closure_debug_destroy(cl);
  61. smp_mb();
  62. atomic_set(&cl->remaining, -1);
  63. if (wait)
  64. closure_wake_up(wait);
  65. if (destructor)
  66. destructor(cl);
  67. if (parent)
  68. closure_put(parent);
  69. }
  70. }
  71. }
  72. /* For clearing flags with the same atomic op as a put */
  73. void closure_sub(struct closure *cl, int v)
  74. {
  75. closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
  76. }
  77. EXPORT_SYMBOL_GPL(closure_sub);
  78. void closure_put(struct closure *cl)
  79. {
  80. closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
  81. }
  82. EXPORT_SYMBOL_GPL(closure_put);
  83. static void set_waiting(struct closure *cl, unsigned long f)
  84. {
  85. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  86. cl->waiting_on = f;
  87. #endif
  88. }
  89. void __closure_wake_up(struct closure_waitlist *wait_list)
  90. {
  91. struct llist_node *list;
  92. struct closure *cl;
  93. struct llist_node *reverse = NULL;
  94. list = llist_del_all(&wait_list->list);
  95. /* We first reverse the list to preserve FIFO ordering and fairness */
  96. while (list) {
  97. struct llist_node *t = list;
  98. list = llist_next(list);
  99. t->next = reverse;
  100. reverse = t;
  101. }
  102. /* Then do the wakeups */
  103. while (reverse) {
  104. cl = container_of(reverse, struct closure, list);
  105. reverse = llist_next(reverse);
  106. set_waiting(cl, 0);
  107. closure_sub(cl, CLOSURE_WAITING + 1);
  108. }
  109. }
  110. EXPORT_SYMBOL_GPL(__closure_wake_up);
  111. bool closure_wait(struct closure_waitlist *list, struct closure *cl)
  112. {
  113. if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
  114. return false;
  115. set_waiting(cl, _RET_IP_);
  116. atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
  117. llist_add(&cl->list, &list->list);
  118. return true;
  119. }
  120. EXPORT_SYMBOL_GPL(closure_wait);
  121. /**
  122. * closure_sync() - sleep until a closure a closure has nothing left to wait on
  123. *
  124. * Sleeps until the refcount hits 1 - the thread that's running the closure owns
  125. * the last refcount.
  126. */
  127. void closure_sync(struct closure *cl)
  128. {
  129. while (1) {
  130. __closure_start_sleep(cl);
  131. closure_set_ret_ip(cl);
  132. if ((atomic_read(&cl->remaining) &
  133. CLOSURE_REMAINING_MASK) == 1)
  134. break;
  135. schedule();
  136. }
  137. __closure_end_sleep(cl);
  138. }
  139. EXPORT_SYMBOL_GPL(closure_sync);
  140. /**
  141. * closure_trylock() - try to acquire the closure, without waiting
  142. * @cl: closure to lock
  143. *
  144. * Returns true if the closure was succesfully locked.
  145. */
  146. bool closure_trylock(struct closure *cl, struct closure *parent)
  147. {
  148. if (atomic_cmpxchg(&cl->remaining, -1,
  149. CLOSURE_REMAINING_INITIALIZER) != -1)
  150. return false;
  151. closure_set_ret_ip(cl);
  152. smp_mb();
  153. cl->parent = parent;
  154. if (parent)
  155. closure_get(parent);
  156. closure_debug_create(cl);
  157. return true;
  158. }
  159. EXPORT_SYMBOL_GPL(closure_trylock);
  160. void __closure_lock(struct closure *cl, struct closure *parent,
  161. struct closure_waitlist *wait_list)
  162. {
  163. struct closure wait;
  164. closure_init_stack(&wait);
  165. while (1) {
  166. if (closure_trylock(cl, parent))
  167. return;
  168. closure_wait_event_sync(wait_list, &wait,
  169. atomic_read(&cl->remaining) == -1);
  170. }
  171. }
  172. EXPORT_SYMBOL_GPL(__closure_lock);
  173. static void closure_delay_timer_fn(unsigned long data)
  174. {
  175. struct closure *cl = (struct closure *) data;
  176. closure_sub(cl, CLOSURE_TIMER + 1);
  177. }
  178. void do_closure_timer_init(struct closure *cl)
  179. {
  180. struct timer_list *timer = closure_timer(cl);
  181. init_timer(timer);
  182. timer->data = (unsigned long) cl;
  183. timer->function = closure_delay_timer_fn;
  184. }
  185. EXPORT_SYMBOL_GPL(do_closure_timer_init);
  186. bool __closure_delay(struct closure *cl, unsigned long delay,
  187. struct timer_list *timer)
  188. {
  189. if (atomic_read(&cl->remaining) & CLOSURE_TIMER)
  190. return false;
  191. BUG_ON(timer_pending(timer));
  192. timer->expires = jiffies + delay;
  193. atomic_add(CLOSURE_TIMER + 1, &cl->remaining);
  194. add_timer(timer);
  195. return true;
  196. }
  197. EXPORT_SYMBOL_GPL(__closure_delay);
  198. void __closure_flush(struct closure *cl, struct timer_list *timer)
  199. {
  200. if (del_timer(timer))
  201. closure_sub(cl, CLOSURE_TIMER + 1);
  202. }
  203. EXPORT_SYMBOL_GPL(__closure_flush);
  204. void __closure_flush_sync(struct closure *cl, struct timer_list *timer)
  205. {
  206. if (del_timer_sync(timer))
  207. closure_sub(cl, CLOSURE_TIMER + 1);
  208. }
  209. EXPORT_SYMBOL_GPL(__closure_flush_sync);
  210. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  211. static LIST_HEAD(closure_list);
  212. static DEFINE_SPINLOCK(closure_list_lock);
  213. void closure_debug_create(struct closure *cl)
  214. {
  215. unsigned long flags;
  216. BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
  217. cl->magic = CLOSURE_MAGIC_ALIVE;
  218. spin_lock_irqsave(&closure_list_lock, flags);
  219. list_add(&cl->all, &closure_list);
  220. spin_unlock_irqrestore(&closure_list_lock, flags);
  221. }
  222. EXPORT_SYMBOL_GPL(closure_debug_create);
  223. void closure_debug_destroy(struct closure *cl)
  224. {
  225. unsigned long flags;
  226. BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
  227. cl->magic = CLOSURE_MAGIC_DEAD;
  228. spin_lock_irqsave(&closure_list_lock, flags);
  229. list_del(&cl->all);
  230. spin_unlock_irqrestore(&closure_list_lock, flags);
  231. }
  232. EXPORT_SYMBOL_GPL(closure_debug_destroy);
  233. static struct dentry *debug;
  234. #define work_data_bits(work) ((unsigned long *)(&(work)->data))
  235. static int debug_seq_show(struct seq_file *f, void *data)
  236. {
  237. struct closure *cl;
  238. spin_lock_irq(&closure_list_lock);
  239. list_for_each_entry(cl, &closure_list, all) {
  240. int r = atomic_read(&cl->remaining);
  241. seq_printf(f, "%p: %pF -> %pf p %p r %i ",
  242. cl, (void *) cl->ip, cl->fn, cl->parent,
  243. r & CLOSURE_REMAINING_MASK);
  244. seq_printf(f, "%s%s%s%s%s%s\n",
  245. test_bit(WORK_STRUCT_PENDING,
  246. work_data_bits(&cl->work)) ? "Q" : "",
  247. r & CLOSURE_RUNNING ? "R" : "",
  248. r & CLOSURE_BLOCKING ? "B" : "",
  249. r & CLOSURE_STACK ? "S" : "",
  250. r & CLOSURE_SLEEPING ? "Sl" : "",
  251. r & CLOSURE_TIMER ? "T" : "");
  252. if (r & CLOSURE_WAITING)
  253. seq_printf(f, " W %pF\n",
  254. (void *) cl->waiting_on);
  255. seq_printf(f, "\n");
  256. }
  257. spin_unlock_irq(&closure_list_lock);
  258. return 0;
  259. }
  260. static int debug_seq_open(struct inode *inode, struct file *file)
  261. {
  262. return single_open(file, debug_seq_show, NULL);
  263. }
  264. static const struct file_operations debug_ops = {
  265. .owner = THIS_MODULE,
  266. .open = debug_seq_open,
  267. .read = seq_read,
  268. .release = single_release
  269. };
  270. void __init closure_debug_init(void)
  271. {
  272. debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);
  273. }
  274. #endif
  275. MODULE_AUTHOR("Kent Overstreet <koverstreet@google.com>");
  276. MODULE_LICENSE("GPL");