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 void closure_queue(struct closure *cl)
16 struct workqueue_struct *wq = cl->wq;
18 INIT_WORK(&cl->work, cl->work.func);
19 BUG_ON(!queue_work(wq, &cl->work));
23 EXPORT_SYMBOL_GPL(closure_queue);
25 #define CL_FIELD(type, field) \
27 return &container_of(cl, struct type, cl)->field
29 static struct closure_waitlist *closure_waitlist(struct closure *cl)
32 CL_FIELD(closure_with_waitlist, wait);
33 CL_FIELD(closure_with_waitlist_and_timer, wait);
39 static struct timer_list *closure_timer(struct closure *cl)
42 CL_FIELD(closure_with_timer, timer);
43 CL_FIELD(closure_with_waitlist_and_timer, timer);
49 static inline void closure_put_after_sub(struct closure *cl, int flags)
51 int r = flags & CLOSURE_REMAINING_MASK;
53 BUG_ON(flags & CLOSURE_GUARD_MASK);
54 BUG_ON(!r && (flags & ~(CLOSURE_DESTRUCTOR|CLOSURE_BLOCKING)));
56 /* Must deliver precisely one wakeup */
57 if (r == 1 && (flags & CLOSURE_SLEEPING))
58 wake_up_process(cl->task);
61 if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
62 /* CLOSURE_BLOCKING might be set - clear it */
63 atomic_set(&cl->remaining,
64 CLOSURE_REMAINING_INITIALIZER);
67 struct closure *parent = cl->parent;
68 struct closure_waitlist *wait = closure_waitlist(cl);
69 closure_fn *destructor = cl->fn;
71 closure_debug_destroy(cl);
74 atomic_set(&cl->remaining, -1);
77 closure_wake_up(wait);
88 /* For clearing flags with the same atomic op as a put */
89 void closure_sub(struct closure *cl, int v)
91 closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
93 EXPORT_SYMBOL_GPL(closure_sub);
95 void closure_put(struct closure *cl)
97 closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
99 EXPORT_SYMBOL_GPL(closure_put);
101 static void set_waiting(struct closure *cl, unsigned long f)
103 #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
108 void __closure_wake_up(struct closure_waitlist *wait_list)
110 struct llist_node *list;
112 struct llist_node *reverse = NULL;
114 list = llist_del_all(&wait_list->list);
116 /* We first reverse the list to preserve FIFO ordering and fairness */
119 struct llist_node *t = list;
120 list = llist_next(list);
126 /* Then do the wakeups */
129 cl = container_of(reverse, struct closure, list);
130 reverse = llist_next(reverse);
133 closure_sub(cl, CLOSURE_WAITING + 1);
136 EXPORT_SYMBOL_GPL(__closure_wake_up);
138 bool closure_wait(struct closure_waitlist *list, struct closure *cl)
140 if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
143 set_waiting(cl, _RET_IP_);
144 atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
145 llist_add(&cl->list, &list->list);
149 EXPORT_SYMBOL_GPL(closure_wait);
152 * closure_sync() - sleep until a closure a closure has nothing left to wait on
154 * Sleeps until the refcount hits 1 - the thread that's running the closure owns
157 void closure_sync(struct closure *cl)
160 __closure_start_sleep(cl);
161 closure_set_ret_ip(cl);
163 if ((atomic_read(&cl->remaining) &
164 CLOSURE_REMAINING_MASK) == 1)
170 __closure_end_sleep(cl);
172 EXPORT_SYMBOL_GPL(closure_sync);
175 * closure_trylock() - try to acquire the closure, without waiting
176 * @cl: closure to lock
178 * Returns true if the closure was succesfully locked.
180 bool closure_trylock(struct closure *cl, struct closure *parent)
182 if (atomic_cmpxchg(&cl->remaining, -1,
183 CLOSURE_REMAINING_INITIALIZER) != -1)
186 closure_set_ret_ip(cl);
193 closure_debug_create(cl);
196 EXPORT_SYMBOL_GPL(closure_trylock);
198 void __closure_lock(struct closure *cl, struct closure *parent,
199 struct closure_waitlist *wait_list)
202 closure_init_stack(&wait);
205 if (closure_trylock(cl, parent))
208 closure_wait_event_sync(wait_list, &wait,
209 atomic_read(&cl->remaining) == -1);
212 EXPORT_SYMBOL_GPL(__closure_lock);
214 static void closure_delay_timer_fn(unsigned long data)
216 struct closure *cl = (struct closure *) data;
217 closure_sub(cl, CLOSURE_TIMER + 1);
220 void do_closure_timer_init(struct closure *cl)
222 struct timer_list *timer = closure_timer(cl);
225 timer->data = (unsigned long) cl;
226 timer->function = closure_delay_timer_fn;
228 EXPORT_SYMBOL_GPL(do_closure_timer_init);
230 bool __closure_delay(struct closure *cl, unsigned long delay,
231 struct timer_list *timer)
233 if (atomic_read(&cl->remaining) & CLOSURE_TIMER)
236 BUG_ON(timer_pending(timer));
238 timer->expires = jiffies + delay;
240 atomic_add(CLOSURE_TIMER + 1, &cl->remaining);
244 EXPORT_SYMBOL_GPL(__closure_delay);
246 void __closure_flush(struct closure *cl, struct timer_list *timer)
248 if (del_timer(timer))
249 closure_sub(cl, CLOSURE_TIMER + 1);
251 EXPORT_SYMBOL_GPL(__closure_flush);
253 void __closure_flush_sync(struct closure *cl, struct timer_list *timer)
255 if (del_timer_sync(timer))
256 closure_sub(cl, CLOSURE_TIMER + 1);
258 EXPORT_SYMBOL_GPL(__closure_flush_sync);
260 #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
262 static LIST_HEAD(closure_list);
263 static DEFINE_SPINLOCK(closure_list_lock);
265 void closure_debug_create(struct closure *cl)
269 BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
270 cl->magic = CLOSURE_MAGIC_ALIVE;
272 spin_lock_irqsave(&closure_list_lock, flags);
273 list_add(&cl->all, &closure_list);
274 spin_unlock_irqrestore(&closure_list_lock, flags);
276 EXPORT_SYMBOL_GPL(closure_debug_create);
278 void closure_debug_destroy(struct closure *cl)
282 BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
283 cl->magic = CLOSURE_MAGIC_DEAD;
285 spin_lock_irqsave(&closure_list_lock, flags);
287 spin_unlock_irqrestore(&closure_list_lock, flags);
289 EXPORT_SYMBOL_GPL(closure_debug_destroy);
291 static struct dentry *debug;
293 #define work_data_bits(work) ((unsigned long *)(&(work)->data))
295 static int debug_seq_show(struct seq_file *f, void *data)
298 spin_lock_irq(&closure_list_lock);
300 list_for_each_entry(cl, &closure_list, all) {
301 int r = atomic_read(&cl->remaining);
303 seq_printf(f, "%p: %pF -> %pf p %p r %i ",
304 cl, (void *) cl->ip, cl->fn, cl->parent,
305 r & CLOSURE_REMAINING_MASK);
307 seq_printf(f, "%s%s%s%s%s%s\n",
308 test_bit(WORK_STRUCT_PENDING,
309 work_data_bits(&cl->work)) ? "Q" : "",
310 r & CLOSURE_RUNNING ? "R" : "",
311 r & CLOSURE_BLOCKING ? "B" : "",
312 r & CLOSURE_STACK ? "S" : "",
313 r & CLOSURE_SLEEPING ? "Sl" : "",
314 r & CLOSURE_TIMER ? "T" : "");
316 if (r & CLOSURE_WAITING)
317 seq_printf(f, " W %pF\n",
318 (void *) cl->waiting_on);
323 spin_unlock_irq(&closure_list_lock);
327 static int debug_seq_open(struct inode *inode, struct file *file)
329 return single_open(file, debug_seq_show, NULL);
332 static const struct file_operations debug_ops = {
333 .owner = THIS_MODULE,
334 .open = debug_seq_open,
336 .release = single_release
339 void __init closure_debug_init(void)
341 debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);
346 MODULE_AUTHOR("Kent Overstreet <koverstreet@google.com>");
347 MODULE_LICENSE("GPL");