]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/closure.c
Add upstream files
[bcachefs-tools-debian] / linux / closure.c
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/closure.h>
9 #include <linux/debugfs.h>
10 #include <linux/export.h>
11 #include <linux/seq_file.h>
12
13 static inline void closure_put_after_sub(struct closure *cl, int flags)
14 {
15         int r = flags & CLOSURE_REMAINING_MASK;
16
17         BUG_ON(flags & CLOSURE_GUARD_MASK);
18         BUG_ON(!r && (flags & ~CLOSURE_DESTRUCTOR));
19
20         if (!r) {
21                 if (cl->fn && !(flags & CLOSURE_DESTRUCTOR)) {
22                         atomic_set(&cl->remaining,
23                                    CLOSURE_REMAINING_INITIALIZER);
24                         closure_queue(cl);
25                 } else {
26                         struct closure *parent = cl->parent;
27                         closure_fn *destructor = cl->fn;
28
29                         closure_debug_destroy(cl);
30
31                         if (destructor)
32                                 destructor(cl);
33
34                         if (parent)
35                                 closure_put(parent);
36                 }
37         }
38 }
39
40 /* For clearing flags with the same atomic op as a put */
41 void closure_sub(struct closure *cl, int v)
42 {
43         closure_put_after_sub(cl, atomic_sub_return(v, &cl->remaining));
44 }
45 EXPORT_SYMBOL(closure_sub);
46
47 /**
48  * closure_put - decrement a closure's refcount
49  */
50 void closure_put(struct closure *cl)
51 {
52         closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));
53 }
54 EXPORT_SYMBOL(closure_put);
55
56 /**
57  * closure_wake_up - wake up all closures on a wait list, without memory barrier
58  */
59 void __closure_wake_up(struct closure_waitlist *wait_list)
60 {
61         struct llist_node *list, *next;
62         struct closure *cl;
63
64         /*
65          * Grab entire list, reverse order to preserve FIFO ordering, and wake
66          * everything up
67          */
68         for (list = llist_reverse_order(llist_del_all(&wait_list->list));
69              list;
70              list = next) {
71                 next = llist_next(list);
72                 cl = container_of(list, struct closure, list);
73
74                 closure_set_waiting(cl, 0);
75                 closure_sub(cl, CLOSURE_WAITING + 1);
76         }
77 }
78 EXPORT_SYMBOL(__closure_wake_up);
79
80 /**
81  * closure_wait - add a closure to a waitlist
82  *
83  * @waitlist will own a ref on @cl, which will be released when
84  * closure_wake_up() is called on @waitlist.
85  *
86  */
87 bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)
88 {
89         if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
90                 return false;
91
92         closure_set_waiting(cl, _RET_IP_);
93         atomic_add(CLOSURE_WAITING + 1, &cl->remaining);
94         llist_add(&cl->list, &waitlist->list);
95
96         return true;
97 }
98 EXPORT_SYMBOL(closure_wait);
99
100 struct closure_syncer {
101         struct task_struct      *task;
102         int                     done;
103 };
104
105 static void closure_sync_fn(struct closure *cl)
106 {
107         cl->s->done = 1;
108         wake_up_process(cl->s->task);
109 }
110
111 void __sched __closure_sync(struct closure *cl)
112 {
113         struct closure_syncer s = { .task = current };
114
115         cl->s = &s;
116         continue_at_noreturn(cl, closure_sync_fn, NULL);
117
118         while (1) {
119                 __set_current_state(TASK_UNINTERRUPTIBLE);
120                 smp_mb();
121                 if (s.done)
122                         break;
123                 schedule();
124         }
125
126         __set_current_state(TASK_RUNNING);
127 }
128 EXPORT_SYMBOL(__closure_sync);
129
130 #ifdef CONFIG_DEBUG_CLOSURES
131
132 static LIST_HEAD(closure_list);
133 static DEFINE_SPINLOCK(closure_list_lock);
134
135 void closure_debug_create(struct closure *cl)
136 {
137         unsigned long flags;
138
139         BUG_ON(cl->magic == CLOSURE_MAGIC_ALIVE);
140         cl->magic = CLOSURE_MAGIC_ALIVE;
141
142         spin_lock_irqsave(&closure_list_lock, flags);
143         list_add(&cl->all, &closure_list);
144         spin_unlock_irqrestore(&closure_list_lock, flags);
145 }
146 EXPORT_SYMBOL(closure_debug_create);
147
148 void closure_debug_destroy(struct closure *cl)
149 {
150         unsigned long flags;
151
152         BUG_ON(cl->magic != CLOSURE_MAGIC_ALIVE);
153         cl->magic = CLOSURE_MAGIC_DEAD;
154
155         spin_lock_irqsave(&closure_list_lock, flags);
156         list_del(&cl->all);
157         spin_unlock_irqrestore(&closure_list_lock, flags);
158 }
159 EXPORT_SYMBOL(closure_debug_destroy);
160
161 static struct dentry *debug;
162
163 static int debug_seq_show(struct seq_file *f, void *data)
164 {
165         struct closure *cl;
166
167         spin_lock_irq(&closure_list_lock);
168
169         list_for_each_entry(cl, &closure_list, all) {
170                 int r = atomic_read(&cl->remaining);
171
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);
175
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" : "");
180
181                 if (r & CLOSURE_WAITING)
182                         seq_printf(f, " W %pF\n",
183                                    (void *) cl->waiting_on);
184
185                 seq_puts(f, "\n");
186         }
187
188         spin_unlock_irq(&closure_list_lock);
189         return 0;
190 }
191
192 static int debug_seq_open(struct inode *inode, struct file *file)
193 {
194         return single_open(file, debug_seq_show, NULL);
195 }
196
197 static const struct file_operations debug_ops = {
198         .owner          = THIS_MODULE,
199         .open           = debug_seq_open,
200         .read           = seq_read,
201         .release        = single_release
202 };
203
204 static int __init closure_debug_init(void)
205 {
206         debug = debugfs_create_file("closures", 0400, NULL, NULL, &debug_ops);
207         return 0;
208 }
209 late_initcall(closure_debug_init)
210
211 #endif