closure.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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, *t;
  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. reverse = llist_reverse_order(list);
  60. /* Then do the wakeups */
  61. llist_for_each_entry_safe(cl, t, reverse, list) {
  62. closure_set_waiting(cl, 0);
  63. closure_sub(cl, CLOSURE_WAITING + 1);
  64. }
  65. }
  66. EXPORT_SYMBOL(__closure_wake_up);
  67. /**
  68. * closure_wait - add a closure to a waitlist
  69. *
  70. * @waitlist will own a ref on @cl, which will be released when
  71. * closure_wake_up() is called on @waitlist.
  72. *
  73. */
  74. bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
  75. {
  76. if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
  77. return false;
  78. closure_set_waiting(cl, _RET_IP_);
  79. atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
  80. llist_add(&cl->list, &waitlist->list);
  81. return true;
  82. }
  83. EXPORT_SYMBOL(closure_wait);
  84. /**
  85. * closure_sync - sleep until a closure has nothing left to wait on
  86. *
  87. * Sleeps until the refcount hits 1 - the thread that's running the closure owns
  88. * the last refcount.
  89. */
  90. void closure_sync(struct closure *cl)
  91. {
  92. while (1) {
  93. __closure_start_sleep(cl);
  94. closure_set_ret_ip(cl);
  95. if ((atomic_read(&cl->remaining) &
  96. CLOSURE_REMAINING_MASK) == 1)
  97. break;
  98. schedule();
  99. }
  100. __closure_end_sleep(cl);
  101. }
  102. EXPORT_SYMBOL(closure_sync);
  103. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  104. static LIST_HEAD(closure_list);
  105. static DEFINE_SPINLOCK(closure_list_lock);
  106. void closure_debug_create(struct closure *cl)
  107. {
  108. unsigned long flags;
  109. BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
  110. cl->magic = CLOSURE_MAGIC_ALIVE;
  111. spin_lock_irqsave(&closure_list_lock, flags);
  112. list_add(&cl->all, &closure_list);
  113. spin_unlock_irqrestore(&closure_list_lock, flags);
  114. }
  115. EXPORT_SYMBOL(closure_debug_create);
  116. void closure_debug_destroy(struct closure *cl)
  117. {
  118. unsigned long flags;
  119. BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
  120. cl->magic = CLOSURE_MAGIC_DEAD;
  121. spin_lock_irqsave(&closure_list_lock, flags);
  122. list_del(&cl->all);
  123. spin_unlock_irqrestore(&closure_list_lock, flags);
  124. }
  125. EXPORT_SYMBOL(closure_debug_destroy);
  126. static struct dentry *debug;
  127. static int debug_seq_show(struct seq_file *f, void *data)
  128. {
  129. struct closure *cl;
  130. spin_lock_irq(&closure_list_lock);
  131. list_for_each_entry(cl, &closure_list, all) {
  132. int r = atomic_read(&cl->remaining);
  133. seq_printf(f, "%p: %pF -> %pf p %p r %i ",
  134. cl, (void *) cl->ip, cl->fn, cl->parent,
  135. r & CLOSURE_REMAINING_MASK);
  136. seq_printf(f, "%s%s%s%s\n",
  137. test_bit(WORK_STRUCT_PENDING_BIT,
  138. work_data_bits(&cl->work)) ? "Q" : "",
  139. r & CLOSURE_RUNNING ? "R" : "",
  140. r & CLOSURE_STACK ? "S" : "",
  141. r & CLOSURE_SLEEPING ? "Sl" : "");
  142. if (r & CLOSURE_WAITING)
  143. seq_printf(f, " W %pF\n",
  144. (void *) cl->waiting_on);
  145. seq_printf(f, "\n");
  146. }
  147. spin_unlock_irq(&closure_list_lock);
  148. return 0;
  149. }
  150. static int debug_seq_open(struct inode *inode, struct file *file)
  151. {
  152. return single_open(file, debug_seq_show, NULL);
  153. }
  154. static const struct file_operations debug_ops = {
  155. .owner = THIS_MODULE,
  156. .open = debug_seq_open,
  157. .read = seq_read,
  158. .release = single_release
  159. };
  160. void __init closure_debug_init(void)
  161. {
  162. debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);
  163. }
  164. #endif
  165. MODULE_AUTHOR("Kent Overstreet <koverstreet@google.com>");
  166. MODULE_LICENSE("GPL");