Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Asynchronous refcounty things |
| 3 | * |
| 4 | * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> |
| 5 | * Copyright 2012 Google, Inc. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/seq_file.h> |
| 11 | |
| 12 | #include "closure.h" |
| 13 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 14 | static inline void closure_put_after_sub(struct closure *cl, int flags) |
| 15 | { |
| 16 | int r = flags & CLOSURE_REMAINING_MASK; |
| 17 | |
| 18 | BUG_ON(flags & CLOSURE_GUARD_MASK); |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 19 | BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 20 | |
| 21 | /* Must deliver precisely one wakeup */ |
| 22 | if (r == 1 && (flags & CLOSURE_SLEEPING)) |
| 23 | wake_up_process(cl->task); |
| 24 | |
| 25 | if (!r) { |
| 26 | if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 27 | atomic_set(&cl->remaining, |
| 28 | CLOSURE_REMAINING_INITIALIZER); |
| 29 | closure_queue(cl); |
| 30 | } else { |
| 31 | struct closure *parent = cl->parent; |
Kent Overstreet | 6aa8f1a | 2013-07-10 18:04:21 -0700 | [diff] [blame] | 32 | closure_fn *destructor = cl->fn; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 33 | |
| 34 | closure_debug_destroy(cl); |
| 35 | |
Kent Overstreet | 6aa8f1a | 2013-07-10 18:04:21 -0700 | [diff] [blame] | 36 | if (destructor) |
| 37 | destructor(cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 38 | |
| 39 | if (parent) |
| 40 | closure_put(parent); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /* For clearing flags with the same atomic op as a put */ |
| 46 | void closure_sub(struct closure *cl, int v) |
| 47 | { |
| 48 | closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining)); |
| 49 | } |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 50 | EXPORT_SYMBOL(closure_sub); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 51 | |
Kent Overstreet | 1dd13c8 | 2013-12-20 15:55:23 -0800 | [diff] [blame] | 52 | /** |
| 53 | * closure_put - decrement a closure's refcount |
| 54 | */ |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 55 | void closure_put(struct closure *cl) |
| 56 | { |
| 57 | closure_put_after_sub(cl, atomic_dec_return(&cl->remaining)); |
| 58 | } |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 59 | EXPORT_SYMBOL(closure_put); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 60 | |
Kent Overstreet | 1dd13c8 | 2013-12-20 15:55:23 -0800 | [diff] [blame] | 61 | /** |
| 62 | * closure_wake_up - wake up all closures on a wait list, without memory barrier |
| 63 | */ |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 64 | void __closure_wake_up(struct closure_waitlist *wait_list) |
| 65 | { |
| 66 | struct llist_node *list; |
| 67 | struct closure *cl; |
| 68 | struct llist_node *reverse = NULL; |
| 69 | |
| 70 | list = llist_del_all(&wait_list->list); |
| 71 | |
| 72 | /* We first reverse the list to preserve FIFO ordering and fairness */ |
Byungchul Park | 09b3efe | 2017-09-06 14:25:54 +0800 | [diff] [blame] | 73 | reverse = llist_reverse_order(list); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 74 | |
| 75 | /* Then do the wakeups */ |
Byungchul Park | 09b3efe | 2017-09-06 14:25:54 +0800 | [diff] [blame] | 76 | llist_for_each_entry(cl, reverse, list) { |
Kent Overstreet | 1dd13c8 | 2013-12-20 15:55:23 -0800 | [diff] [blame] | 77 | closure_set_waiting(cl, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 78 | closure_sub(cl, CLOSURE_WAITING + 1); |
| 79 | } |
| 80 | } |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 81 | EXPORT_SYMBOL(__closure_wake_up); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 82 | |
Kent Overstreet | 1dd13c8 | 2013-12-20 15:55:23 -0800 | [diff] [blame] | 83 | /** |
| 84 | * closure_wait - add a closure to a waitlist |
| 85 | * |
| 86 | * @waitlist will own a ref on @cl, which will be released when |
| 87 | * closure_wake_up() is called on @waitlist. |
| 88 | * |
| 89 | */ |
| 90 | bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 91 | { |
| 92 | if (atomic_read(&cl->remaining) & CLOSURE_WAITING) |
| 93 | return false; |
| 94 | |
Kent Overstreet | 1dd13c8 | 2013-12-20 15:55:23 -0800 | [diff] [blame] | 95 | closure_set_waiting(cl, _RET_IP_); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 96 | atomic_add(CLOSURE_WAITING + 1, &cl->remaining); |
Kent Overstreet | 1dd13c8 | 2013-12-20 15:55:23 -0800 | [diff] [blame] | 97 | llist_add(&cl->list, &waitlist->list); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 98 | |
| 99 | return true; |
| 100 | } |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 101 | EXPORT_SYMBOL(closure_wait); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
Yijing Wang | 7abc70d | 2016-07-04 09:23:34 +0800 | [diff] [blame] | 104 | * closure_sync - sleep until a closure has nothing left to wait on |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 105 | * |
| 106 | * Sleeps until the refcount hits 1 - the thread that's running the closure owns |
| 107 | * the last refcount. |
| 108 | */ |
| 109 | void closure_sync(struct closure *cl) |
| 110 | { |
| 111 | while (1) { |
| 112 | __closure_start_sleep(cl); |
| 113 | closure_set_ret_ip(cl); |
| 114 | |
| 115 | if ((atomic_read(&cl->remaining) & |
| 116 | CLOSURE_REMAINING_MASK) == 1) |
| 117 | break; |
| 118 | |
| 119 | schedule(); |
| 120 | } |
| 121 | |
| 122 | __closure_end_sleep(cl); |
| 123 | } |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 124 | EXPORT_SYMBOL(closure_sync); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 125 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 126 | #ifdef CONFIG_BCACHE_CLOSURES_DEBUG |
| 127 | |
| 128 | static LIST_HEAD(closure_list); |
| 129 | static DEFINE_SPINLOCK(closure_list_lock); |
| 130 | |
| 131 | void closure_debug_create(struct closure *cl) |
| 132 | { |
| 133 | unsigned long flags; |
| 134 | |
| 135 | BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE); |
| 136 | cl->magic = CLOSURE_MAGIC_ALIVE; |
| 137 | |
| 138 | spin_lock_irqsave(&closure_list_lock, flags); |
| 139 | list_add(&cl->all, &closure_list); |
| 140 | spin_unlock_irqrestore(&closure_list_lock, flags); |
| 141 | } |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 142 | EXPORT_SYMBOL(closure_debug_create); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 143 | |
| 144 | void closure_debug_destroy(struct closure *cl) |
| 145 | { |
| 146 | unsigned long flags; |
| 147 | |
| 148 | BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE); |
| 149 | cl->magic = CLOSURE_MAGIC_DEAD; |
| 150 | |
| 151 | spin_lock_irqsave(&closure_list_lock, flags); |
| 152 | list_del(&cl->all); |
| 153 | spin_unlock_irqrestore(&closure_list_lock, flags); |
| 154 | } |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 155 | EXPORT_SYMBOL(closure_debug_destroy); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 156 | |
| 157 | static struct dentry *debug; |
| 158 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 159 | static int debug_seq_show(struct seq_file *f, void *data) |
| 160 | { |
| 161 | struct closure *cl; |
| 162 | spin_lock_irq(&closure_list_lock); |
| 163 | |
| 164 | list_for_each_entry(cl, &closure_list, all) { |
| 165 | int r = atomic_read(&cl->remaining); |
| 166 | |
| 167 | seq_printf(f, "%p: %pF -> %pf p %p r %i ", |
| 168 | cl, (void *) cl->ip, cl->fn, cl->parent, |
| 169 | r & CLOSURE_REMAINING_MASK); |
| 170 | |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 171 | seq_printf(f, "%s%s%s%s\n", |
Petr Mladek | 8d090f4 | 2015-10-05 14:39:52 +0200 | [diff] [blame] | 172 | test_bit(WORK_STRUCT_PENDING_BIT, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 173 | work_data_bits(&cl->work)) ? "Q" : "", |
| 174 | r & CLOSURE_RUNNING ? "R" : "", |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 175 | r & CLOSURE_STACK ? "S" : "", |
Kent Overstreet | faadf0c | 2013-11-01 18:03:08 -0700 | [diff] [blame] | 176 | r & CLOSURE_SLEEPING ? "Sl" : ""); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 177 | |
| 178 | if (r & CLOSURE_WAITING) |
| 179 | seq_printf(f, " W %pF\n", |
| 180 | (void *) cl->waiting_on); |
| 181 | |
| 182 | seq_printf(f, "\n"); |
| 183 | } |
| 184 | |
| 185 | spin_unlock_irq(&closure_list_lock); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int debug_seq_open(struct inode *inode, struct file *file) |
| 190 | { |
| 191 | return single_open(file, debug_seq_show, NULL); |
| 192 | } |
| 193 | |
| 194 | static const struct file_operations debug_ops = { |
| 195 | .owner = THIS_MODULE, |
| 196 | .open = debug_seq_open, |
| 197 | .read = seq_read, |
| 198 | .release = single_release |
| 199 | }; |
| 200 | |
Kent Overstreet | 07e86cc | 2013-03-25 11:46:43 -0700 | [diff] [blame] | 201 | void __init closure_debug_init(void) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 202 | { |
| 203 | debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 206 | #endif |
| 207 | |
| 208 | MODULE_AUTHOR("Kent Overstreet <koverstreet@google.com>"); |
| 209 | MODULE_LICENSE("GPL"); |