2 * Asynchronous refcounty things
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
8 #include <linux/debugfs.h>
9 #include <linux/module.h>
10 #include <linux/seq_file.h>
14 static inline void closure_put_after_sub(struct closure *cl, int flags)
16 int r = flags & CLOSURE_REMAINING_MASK;
18 BUG_ON(flags & CLOSURE_GUARD_MASK);
19 BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR));
22 if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
23 atomic_set(&cl->remaining,
24 CLOSURE_REMAINING_INITIALIZER);
27 struct closure *parent = cl->parent;
28 closure_fn *destructor = cl->fn;
30 closure_debug_destroy(cl);
41 /* For clearing flags with the same atomic op as a put */
42 void closure_sub(struct closure *cl, int v)
44 closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
46 EXPORT_SYMBOL(closure_sub);
49 * closure_put - decrement a closure's refcount
51 void closure_put(struct closure *cl)
53 closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
55 EXPORT_SYMBOL(closure_put);
58 * closure_wake_up - wake up all closures on a wait list, without memory barrier
60 void __closure_wake_up(struct closure_waitlist *wait_list)
62 struct llist_node *list, *next;
66 * Grab entire list, reverse order to preserve FIFO ordering, and wake
69 for (list = llist_reverse_order(llist_del_all(&wait_list->list));
72 next = llist_next(list);
73 cl = container_of(list, struct closure, list);
75 closure_set_waiting(cl, 0);
76 closure_sub(cl, CLOSURE_WAITING + 1);
79 EXPORT_SYMBOL(__closure_wake_up);
82 * closure_wait - add a closure to a waitlist
84 * @waitlist will own a ref on @cl, which will be released when
85 * closure_wake_up() is called on @waitlist.
88 bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
90 if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
93 closure_set_waiting(cl, _RET_IP_);
94 atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
95 llist_add(&cl->list, &waitlist->list);
99 EXPORT_SYMBOL(closure_wait);
101 struct closure_syncer {
102 struct task_struct *task;
106 static void closure_sync_fn(struct closure *cl)
109 wake_up_process(cl->s->task);
112 void __sched __closure_sync(struct closure *cl)
114 struct closure_syncer s = { .task = current };
117 continue_at_noreturn(cl, closure_sync_fn, NULL);
120 __set_current_state(TASK_UNINTERRUPTIBLE);
127 __set_current_state(TASK_RUNNING);
129 EXPORT_SYMBOL(__closure_sync);
131 #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
133 static LIST_HEAD(closure_list);
134 static DEFINE_SPINLOCK(closure_list_lock);
136 void closure_debug_create(struct closure *cl)
140 BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
141 cl->magic = CLOSURE_MAGIC_ALIVE;
143 spin_lock_irqsave(&closure_list_lock, flags);
144 list_add(&cl->all, &closure_list);
145 spin_unlock_irqrestore(&closure_list_lock, flags);
147 EXPORT_SYMBOL(closure_debug_create);
149 void closure_debug_destroy(struct closure *cl)
153 BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
154 cl->magic = CLOSURE_MAGIC_DEAD;
156 spin_lock_irqsave(&closure_list_lock, flags);
158 spin_unlock_irqrestore(&closure_list_lock, flags);
160 EXPORT_SYMBOL(closure_debug_destroy);
162 static struct dentry *debug;
164 static int debug_seq_show(struct seq_file *f, void *data)
168 spin_lock_irq(&closure_list_lock);
170 list_for_each_entry(cl, &closure_list, all) {
171 int r = atomic_read(&cl->remaining);
173 seq_printf(f, "%p: %pF -> %pf p %p r %i ",
174 cl, (void *) cl->ip, cl->fn, cl->parent,
175 r & CLOSURE_REMAINING_MASK);
177 seq_printf(f, "%s%s\n",
178 test_bit(WORK_STRUCT_PENDING_BIT,
179 work_data_bits(&cl->work)) ? "Q" : "",
180 r & CLOSURE_RUNNING ? "R" : "");
182 if (r & CLOSURE_WAITING)
183 seq_printf(f, " W %pF\n",
184 (void *) cl->waiting_on);
189 spin_unlock_irq(&closure_list_lock);
193 static int debug_seq_open(struct inode *inode, struct file *file)
195 return single_open(file, debug_seq_show, NULL);
198 static const struct file_operations debug_ops = {
199 .owner = THIS_MODULE,
200 .open = debug_seq_open,
202 .release = single_release
205 void __init closure_debug_init(void)
207 debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);