2 * Asynchronous refcounty things
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
8 #include <linux/closure.h>
9 #include <linux/debugfs.h>
10 #include <linux/export.h>
11 #include <linux/seq_file.h>
13 static inline void closure_put_after_sub(struct closure *cl, int flags)
15 int r = flags & CLOSURE_REMAINING_MASK;
17 BUG_ON(flags & CLOSURE_GUARD_MASK);
18 BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR));
21 if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
22 atomic_set(&cl->remaining,
23 CLOSURE_REMAINING_INITIALIZER);
26 struct closure *parent = cl->parent;
27 closure_fn *destructor = cl->fn;
29 closure_debug_destroy(cl);
40 /* For clearing flags with the same atomic op as a put */
41 void closure_sub(struct closure *cl, int v)
43 closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
45 EXPORT_SYMBOL(closure_sub);
48 * closure_put - decrement a closure's refcount
50 void closure_put(struct closure *cl)
52 closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
54 EXPORT_SYMBOL(closure_put);
57 * closure_wake_up - wake up all closures on a wait list, without memory barrier
59 void __closure_wake_up(struct closure_waitlist *wait_list)
61 struct llist_node *list, *next;
65 * Grab entire list, reverse order to preserve FIFO ordering, and wake
68 for (list = llist_reverse_order(llist_del_all(&wait_list->list));
71 next = llist_next(list);
72 cl = container_of(list, struct closure, list);
74 closure_set_waiting(cl, 0);
75 closure_sub(cl, CLOSURE_WAITING + 1);
78 EXPORT_SYMBOL(__closure_wake_up);
81 * closure_wait - add a closure to a waitlist
83 * @waitlist will own a ref on @cl, which will be released when
84 * closure_wake_up() is called on @waitlist.
87 bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
89 if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
92 closure_set_waiting(cl, _RET_IP_);
93 atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
94 llist_add(&cl->list, &waitlist->list);
98 EXPORT_SYMBOL(closure_wait);
100 struct closure_syncer {
101 struct task_struct *task;
105 static void closure_sync_fn(struct closure *cl)
108 wake_up_process(cl->s->task);
111 void __sched __closure_sync(struct closure *cl)
113 struct closure_syncer s = { .task = current };
116 continue_at_noreturn(cl, closure_sync_fn, NULL);
119 __set_current_state(TASK_UNINTERRUPTIBLE);
126 __set_current_state(TASK_RUNNING);
128 EXPORT_SYMBOL(__closure_sync);
130 #ifdef CONFIG_DEBUG_CLOSURES
132 static LIST_HEAD(closure_list);
133 static DEFINE_SPINLOCK(closure_list_lock);
135 void closure_debug_create(struct closure *cl)
139 BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
140 cl->magic = CLOSURE_MAGIC_ALIVE;
142 spin_lock_irqsave(&closure_list_lock, flags);
143 list_add(&cl->all, &closure_list);
144 spin_unlock_irqrestore(&closure_list_lock, flags);
146 EXPORT_SYMBOL(closure_debug_create);
148 void closure_debug_destroy(struct closure *cl)
152 BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
153 cl->magic = CLOSURE_MAGIC_DEAD;
155 spin_lock_irqsave(&closure_list_lock, flags);
157 spin_unlock_irqrestore(&closure_list_lock, flags);
159 EXPORT_SYMBOL(closure_debug_destroy);
161 static struct dentry *debug;
163 static int debug_seq_show(struct seq_file *f, void *data)
167 spin_lock_irq(&closure_list_lock);
169 list_for_each_entry(cl, &closure_list, all) {
170 int r = atomic_read(&cl->remaining);
172 seq_printf(f, "%p: %pF -> %pf p %p r %i ",
173 cl, (void *) cl->ip, cl->fn, cl->parent,
174 r & CLOSURE_REMAINING_MASK);
176 seq_printf(f, "%s%s\n",
177 test_bit(WORK_STRUCT_PENDING_BIT,
178 work_data_bits(&cl->work)) ? "Q" : "",
179 r & CLOSURE_RUNNING ? "R" : "");
181 if (r & CLOSURE_WAITING)
182 seq_printf(f, " W %pF\n",
183 (void *) cl->waiting_on);
188 spin_unlock_irq(&closure_list_lock);
192 static int debug_seq_open(struct inode *inode, struct file *file)
194 return single_open(file, debug_seq_show, NULL);
197 static const struct file_operations debug_ops = {
198 .owner = THIS_MODULE,
199 .open = debug_seq_open,
201 .release = single_release
204 static int __init closure_debug_init(void)
206 debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);
209 late_initcall(closure_debug_init)