closure.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. static inline void closure_put_after_sub(struct closure *cl, int flags)
  12. {
  13. int r = flags & CLOSURE_REMAINING_MASK;
  14. BUG_ON(flags & CLOSURE_GUARD_MASK);
  15. BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR));
  16. /* Must deliver precisely one wakeup */
  17. if (r == 1 && (flags & CLOSURE_SLEEPING))
  18. wake_up_process(cl->task);
  19. if (!r) {
  20. if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
  21. atomic_set(&cl->remaining,
  22. CLOSURE_REMAINING_INITIALIZER);
  23. closure_queue(cl);
  24. } else {
  25. struct closure *parent = cl->parent;
  26. closure_fn *destructor = cl->fn;
  27. closure_debug_destroy(cl);
  28. if (destructor)
  29. destructor(cl);
  30. if (parent)
  31. closure_put(parent);
  32. }
  33. }
  34. }
  35. /* For clearing flags with the same atomic op as a put */
  36. void closure_sub(struct closure *cl, int v)
  37. {
  38. closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
  39. }
  40. EXPORT_SYMBOL(closure_sub);
  41. /**
  42. * closure_put - decrement a closure's refcount
  43. */
  44. void closure_put(struct closure *cl)
  45. {
  46. closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
  47. }
  48. EXPORT_SYMBOL(closure_put);
  49. /**
  50. * closure_wake_up - wake up all closures on a wait list, without memory barrier
  51. */
  52. void __closure_wake_up(struct closure_waitlist *wait_list)
  53. {
  54. struct llist_node *list;
  55. struct closure *cl;
  56. struct llist_node *reverse = NULL;
  57. list = llist_del_all(&wait_list->list);
  58. /* We first reverse the list to preserve FIFO ordering and fairness */
  59. while (list) {
  60. struct llist_node *t = list;
  61. list = llist_next(list);
  62. t->next = reverse;
  63. reverse = t;
  64. }
  65. /* Then do the wakeups */
  66. while (reverse) {
  67. cl = container_of(reverse, struct closure, list);
  68. reverse = llist_next(reverse);
  69. closure_set_waiting(cl, 0);
  70. closure_sub(cl, CLOSURE_WAITING + 1);
  71. }
  72. }
  73. EXPORT_SYMBOL(__closure_wake_up);
  74. /**
  75. * closure_wait - add a closure to a waitlist
  76. *
  77. * @waitlist will own a ref on @cl, which will be released when
  78. * closure_wake_up() is called on @waitlist.
  79. *
  80. */
  81. bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
  82. {
  83. if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
  84. return false;
  85. closure_set_waiting(cl, _RET_IP_);
  86. atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
  87. llist_add(&cl->list, &waitlist->list);
  88. return true;
  89. }
  90. EXPORT_SYMBOL(closure_wait);
  91. /**
  92. * closure_sync - sleep until a closure a closure has nothing left to wait on
  93. *
  94. * Sleeps until the refcount hits 1 - the thread that's running the closure owns
  95. * the last refcount.
  96. */
  97. void closure_sync(struct closure *cl)
  98. {
  99. while (1) {
  100. __closure_start_sleep(cl);
  101. closure_set_ret_ip(cl);
  102. if ((atomic_read(&cl->remaining) &
  103. CLOSURE_REMAINING_MASK) == 1)
  104. break;
  105. schedule();
  106. }
  107. __closure_end_sleep(cl);
  108. }
  109. EXPORT_SYMBOL(closure_sync);
  110. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  111. static LIST_HEAD(closure_list);
  112. static DEFINE_SPINLOCK(closure_list_lock);
  113. void closure_debug_create(struct closure *cl)
  114. {
  115. unsigned long flags;
  116. BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
  117. cl->magic = CLOSURE_MAGIC_ALIVE;
  118. spin_lock_irqsave(&closure_list_lock, flags);
  119. list_add(&cl->all, &closure_list);
  120. spin_unlock_irqrestore(&closure_list_lock, flags);
  121. }
  122. EXPORT_SYMBOL(closure_debug_create);
  123. void closure_debug_destroy(struct closure *cl)
  124. {
  125. unsigned long flags;
  126. BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
  127. cl->magic = CLOSURE_MAGIC_DEAD;
  128. spin_lock_irqsave(&closure_list_lock, flags);
  129. list_del(&cl->all);
  130. spin_unlock_irqrestore(&closure_list_lock, flags);
  131. }
  132. EXPORT_SYMBOL(closure_debug_destroy);
  133. static struct dentry *debug;
  134. static int debug_seq_show(struct seq_file *f, void *data)
  135. {
  136. struct closure *cl;
  137. spin_lock_irq(&closure_list_lock);
  138. list_for_each_entry(cl, &closure_list, all) {
  139. int r = atomic_read(&cl->remaining);
  140. seq_printf(f, "%p: %pF -> %pf p %p r %i ",
  141. cl, (void *) cl->ip, cl->fn, cl->parent,
  142. r & CLOSURE_REMAINING_MASK);
  143. seq_printf(f, "%s%s%s%s\n",
  144. test_bit(WORK_STRUCT_PENDING_BIT,
  145. work_data_bits(&cl->work)) ? "Q" : "",
  146. r & CLOSURE_RUNNING ? "R" : "",
  147. r & CLOSURE_STACK ? "S" : "",
  148. r & CLOSURE_SLEEPING ? "Sl" : "");
  149. if (r & CLOSURE_WAITING)
  150. seq_printf(f, " W %pF\n",
  151. (void *) cl->waiting_on);
  152. seq_printf(f, "\n");
  153. }
  154. spin_unlock_irq(&closure_list_lock);
  155. return 0;
  156. }
  157. static int debug_seq_open(struct inode *inode, struct file *file)
  158. {
  159. return single_open(file, debug_seq_show, NULL);
  160. }
  161. static const struct file_operations debug_ops = {
  162. .owner = THIS_MODULE,
  163. .open = debug_seq_open,
  164. .read = seq_read,
  165. .release = single_release
  166. };
  167. void __init closure_debug_init(void)
  168. {
  169. debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);
  170. }
  171. #endif
  172. MODULE_AUTHOR("Kent Overstreet <koverstreet@google.com>");
  173. MODULE_LICENSE("GPL");