closure.c 4.9 KB

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