]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
cafe5635 KO |
2 | #ifndef _LINUX_CLOSURE_H |
3 | #define _LINUX_CLOSURE_H | |
4 | ||
5 | #include <linux/llist.h> | |
6 | #include <linux/sched.h> | |
68db0cf1 | 7 | #include <linux/sched/task_stack.h> |
cafe5635 KO |
8 | #include <linux/workqueue.h> |
9 | ||
10 | /* | |
11 | * Closure is perhaps the most overused and abused term in computer science, but | |
12 | * since I've been unable to come up with anything better you're stuck with it | |
13 | * again. | |
14 | * | |
15 | * What are closures? | |
16 | * | |
17 | * They embed a refcount. The basic idea is they count "things that are in | |
18 | * progress" - in flight bios, some other thread that's doing something else - | |
19 | * anything you might want to wait on. | |
20 | * | |
21 | * The refcount may be manipulated with closure_get() and closure_put(). | |
22 | * closure_put() is where many of the interesting things happen, when it causes | |
23 | * the refcount to go to 0. | |
24 | * | |
25 | * Closures can be used to wait on things both synchronously and asynchronously, | |
26 | * and synchronous and asynchronous use can be mixed without restriction. To | |
27 | * wait synchronously, use closure_sync() - you will sleep until your closure's | |
28 | * refcount hits 1. | |
29 | * | |
30 | * To wait asynchronously, use | |
31 | * continue_at(cl, next_function, workqueue); | |
32 | * | |
33 | * passing it, as you might expect, the function to run when nothing is pending | |
34 | * and the workqueue to run that function out of. | |
35 | * | |
7abc70d7 YW |
36 | * continue_at() also, critically, requires a 'return' immediately following the |
37 | * location where this macro is referenced, to return to the calling function. | |
cafe5635 KO |
38 | * There's good reason for this. |
39 | * | |
40 | * To use safely closures asynchronously, they must always have a refcount while | |
41 | * they are running owned by the thread that is running them. Otherwise, suppose | |
42 | * you submit some bios and wish to have a function run when they all complete: | |
43 | * | |
4246a0b6 | 44 | * foo_endio(struct bio *bio) |
cafe5635 KO |
45 | * { |
46 | * closure_put(cl); | |
47 | * } | |
48 | * | |
49 | * closure_init(cl); | |
50 | * | |
51 | * do_stuff(); | |
52 | * closure_get(cl); | |
53 | * bio1->bi_endio = foo_endio; | |
54 | * bio_submit(bio1); | |
55 | * | |
56 | * do_more_stuff(); | |
57 | * closure_get(cl); | |
58 | * bio2->bi_endio = foo_endio; | |
59 | * bio_submit(bio2); | |
60 | * | |
61 | * continue_at(cl, complete_some_read, system_wq); | |
62 | * | |
63 | * If closure's refcount started at 0, complete_some_read() could run before the | |
64 | * second bio was submitted - which is almost always not what you want! More | |
65 | * importantly, it wouldn't be possible to say whether the original thread or | |
66 | * complete_some_read()'s thread owned the closure - and whatever state it was | |
67 | * associated with! | |
68 | * | |
69 | * So, closure_init() initializes a closure's refcount to 1 - and when a | |
70 | * closure_fn is run, the refcount will be reset to 1 first. | |
71 | * | |
72 | * Then, the rule is - if you got the refcount with closure_get(), release it | |
73 | * with closure_put() (i.e, in a bio->bi_endio function). If you have a refcount | |
74 | * on a closure because you called closure_init() or you were run out of a | |
75 | * closure - _always_ use continue_at(). Doing so consistently will help | |
76 | * eliminate an entire class of particularly pernicious races. | |
77 | * | |
cafe5635 KO |
78 | * Lastly, you might have a wait list dedicated to a specific event, and have no |
79 | * need for specifying the condition - you just want to wait until someone runs | |
80 | * closure_wake_up() on the appropriate wait list. In that case, just use | |
81 | * closure_wait(). It will return either true or false, depending on whether the | |
82 | * closure was already on a wait list or not - a closure can only be on one wait | |
83 | * list at a time. | |
84 | * | |
85 | * Parents: | |
86 | * | |
87 | * closure_init() takes two arguments - it takes the closure to initialize, and | |
88 | * a (possibly null) parent. | |
89 | * | |
90 | * If parent is non null, the new closure will have a refcount for its lifetime; | |
91 | * a closure is considered to be "finished" when its refcount hits 0 and the | |
92 | * function to run is null. Hence | |
93 | * | |
94 | * continue_at(cl, NULL, NULL); | |
95 | * | |
96 | * returns up the (spaghetti) stack of closures, precisely like normal return | |
97 | * returns up the C stack. continue_at() with non null fn is better thought of | |
98 | * as doing a tail call. | |
99 | * | |
100 | * All this implies that a closure should typically be embedded in a particular | |
101 | * struct (which its refcount will normally control the lifetime of), and that | |
102 | * struct can very much be thought of as a stack frame. | |
cafe5635 KO |
103 | */ |
104 | ||
105 | struct closure; | |
106 | typedef void (closure_fn) (struct closure *); | |
107 | ||
108 | struct closure_waitlist { | |
109 | struct llist_head list; | |
110 | }; | |
111 | ||
cafe5635 KO |
112 | enum closure_state { |
113 | /* | |
cafe5635 KO |
114 | * CLOSURE_WAITING: Set iff the closure is on a waitlist. Must be set by |
115 | * the thread that owns the closure, and cleared by the thread that's | |
116 | * waking up the closure. | |
117 | * | |
118 | * CLOSURE_SLEEPING: Must be set before a thread uses a closure to sleep | |
119 | * - indicates that cl->task is valid and closure_put() may wake it up. | |
120 | * Only set or cleared by the thread that owns the closure. | |
121 | * | |
cafe5635 KO |
122 | * The rest are for debugging and don't affect behaviour: |
123 | * | |
124 | * CLOSURE_RUNNING: Set when a closure is running (i.e. by | |
125 | * closure_init() and when closure_put() runs then next function), and | |
126 | * must be cleared before remaining hits 0. Primarily to help guard | |
127 | * against incorrect usage and accidentally transferring references. | |
128 | * continue_at() and closure_return() clear it for you, if you're doing | |
129 | * something unusual you can use closure_set_dead() which also helps | |
130 | * annotate where references are being transferred. | |
131 | * | |
132 | * CLOSURE_STACK: Sanity check - remaining should never hit 0 on a | |
133 | * closure with this flag set | |
134 | */ | |
135 | ||
faadf0c9 KO |
136 | CLOSURE_BITS_START = (1 << 23), |
137 | CLOSURE_DESTRUCTOR = (1 << 23), | |
138 | CLOSURE_WAITING = (1 << 25), | |
139 | CLOSURE_SLEEPING = (1 << 27), | |
cafe5635 KO |
140 | CLOSURE_RUNNING = (1 << 29), |
141 | CLOSURE_STACK = (1 << 31), | |
142 | }; | |
143 | ||
144 | #define CLOSURE_GUARD_MASK \ | |
faadf0c9 KO |
145 | ((CLOSURE_DESTRUCTOR|CLOSURE_WAITING|CLOSURE_SLEEPING| \ |
146 | CLOSURE_RUNNING|CLOSURE_STACK) << 1) | |
cafe5635 KO |
147 | |
148 | #define CLOSURE_REMAINING_MASK (CLOSURE_BITS_START - 1) | |
149 | #define CLOSURE_REMAINING_INITIALIZER (1|CLOSURE_RUNNING) | |
150 | ||
151 | struct closure { | |
152 | union { | |
153 | struct { | |
154 | struct workqueue_struct *wq; | |
155 | struct task_struct *task; | |
156 | struct llist_node list; | |
157 | closure_fn *fn; | |
158 | }; | |
159 | struct work_struct work; | |
160 | }; | |
161 | ||
162 | struct closure *parent; | |
163 | ||
164 | atomic_t remaining; | |
165 | ||
cafe5635 KO |
166 | #ifdef CONFIG_BCACHE_CLOSURES_DEBUG |
167 | #define CLOSURE_MAGIC_DEAD 0xc054dead | |
168 | #define CLOSURE_MAGIC_ALIVE 0xc054a11e | |
169 | ||
170 | unsigned magic; | |
171 | struct list_head all; | |
172 | unsigned long ip; | |
173 | unsigned long waiting_on; | |
174 | #endif | |
175 | }; | |
176 | ||
cafe5635 KO |
177 | void closure_sub(struct closure *cl, int v); |
178 | void closure_put(struct closure *cl); | |
cafe5635 KO |
179 | void __closure_wake_up(struct closure_waitlist *list); |
180 | bool closure_wait(struct closure_waitlist *list, struct closure *cl); | |
181 | void closure_sync(struct closure *cl); | |
182 | ||
cafe5635 KO |
183 | #ifdef CONFIG_BCACHE_CLOSURES_DEBUG |
184 | ||
07e86ccb | 185 | void closure_debug_init(void); |
cafe5635 KO |
186 | void closure_debug_create(struct closure *cl); |
187 | void closure_debug_destroy(struct closure *cl); | |
188 | ||
189 | #else | |
190 | ||
07e86ccb | 191 | static inline void closure_debug_init(void) {} |
cafe5635 KO |
192 | static inline void closure_debug_create(struct closure *cl) {} |
193 | static inline void closure_debug_destroy(struct closure *cl) {} | |
194 | ||
195 | #endif | |
196 | ||
197 | static inline void closure_set_ip(struct closure *cl) | |
198 | { | |
199 | #ifdef CONFIG_BCACHE_CLOSURES_DEBUG | |
200 | cl->ip = _THIS_IP_; | |
201 | #endif | |
202 | } | |
203 | ||
204 | static inline void closure_set_ret_ip(struct closure *cl) | |
205 | { | |
206 | #ifdef CONFIG_BCACHE_CLOSURES_DEBUG | |
207 | cl->ip = _RET_IP_; | |
208 | #endif | |
209 | } | |
210 | ||
1dd13c8d | 211 | static inline void closure_set_waiting(struct closure *cl, unsigned long f) |
cafe5635 KO |
212 | { |
213 | #ifdef CONFIG_BCACHE_CLOSURES_DEBUG | |
1dd13c8d | 214 | cl->waiting_on = f; |
cafe5635 KO |
215 | #endif |
216 | } | |
217 | ||
1dd13c8d KO |
218 | static inline void __closure_end_sleep(struct closure *cl) |
219 | { | |
220 | __set_current_state(TASK_RUNNING); | |
221 | ||
222 | if (atomic_read(&cl->remaining) & CLOSURE_SLEEPING) | |
223 | atomic_sub(CLOSURE_SLEEPING, &cl->remaining); | |
224 | } | |
225 | ||
226 | static inline void __closure_start_sleep(struct closure *cl) | |
227 | { | |
228 | closure_set_ip(cl); | |
229 | cl->task = current; | |
230 | set_current_state(TASK_UNINTERRUPTIBLE); | |
231 | ||
232 | if (!(atomic_read(&cl->remaining) & CLOSURE_SLEEPING)) | |
233 | atomic_add(CLOSURE_SLEEPING, &cl->remaining); | |
234 | } | |
235 | ||
cafe5635 KO |
236 | static inline void closure_set_stopped(struct closure *cl) |
237 | { | |
238 | atomic_sub(CLOSURE_RUNNING, &cl->remaining); | |
239 | } | |
240 | ||
1dd13c8d KO |
241 | static inline void set_closure_fn(struct closure *cl, closure_fn *fn, |
242 | struct workqueue_struct *wq) | |
cafe5635 | 243 | { |
1dd13c8d KO |
244 | BUG_ON(object_is_on_stack(cl)); |
245 | closure_set_ip(cl); | |
246 | cl->fn = fn; | |
247 | cl->wq = wq; | |
248 | /* between atomic_dec() in closure_put() */ | |
4e857c58 | 249 | smp_mb__before_atomic(); |
cafe5635 KO |
250 | } |
251 | ||
1dd13c8d | 252 | static inline void closure_queue(struct closure *cl) |
cafe5635 | 253 | { |
1dd13c8d KO |
254 | struct workqueue_struct *wq = cl->wq; |
255 | if (wq) { | |
256 | INIT_WORK(&cl->work, cl->work.func); | |
257 | BUG_ON(!queue_work(wq, &cl->work)); | |
cafe5635 | 258 | } else |
1dd13c8d | 259 | cl->fn(cl); |
cafe5635 KO |
260 | } |
261 | ||
1dd13c8d KO |
262 | /** |
263 | * closure_get - increment a closure's refcount | |
cafe5635 | 264 | */ |
1dd13c8d KO |
265 | static inline void closure_get(struct closure *cl) |
266 | { | |
267 | #ifdef CONFIG_BCACHE_CLOSURES_DEBUG | |
268 | BUG_ON((atomic_inc_return(&cl->remaining) & | |
269 | CLOSURE_REMAINING_MASK) <= 1); | |
270 | #else | |
271 | atomic_inc(&cl->remaining); | |
272 | #endif | |
273 | } | |
cafe5635 | 274 | |
cafe5635 | 275 | /** |
1dd13c8d | 276 | * closure_init - Initialize a closure, setting the refcount to 1 |
cafe5635 KO |
277 | * @cl: closure to initialize |
278 | * @parent: parent of the new closure. cl will take a refcount on it for its | |
279 | * lifetime; may be NULL. | |
280 | */ | |
1dd13c8d | 281 | static inline void closure_init(struct closure *cl, struct closure *parent) |
cafe5635 KO |
282 | { |
283 | memset(cl, 0, sizeof(struct closure)); | |
1dd13c8d KO |
284 | cl->parent = parent; |
285 | if (parent) | |
286 | closure_get(parent); | |
cafe5635 | 287 | |
1dd13c8d | 288 | atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER); |
cafe5635 | 289 | |
1dd13c8d KO |
290 | closure_debug_create(cl); |
291 | closure_set_ip(cl); | |
cafe5635 KO |
292 | } |
293 | ||
1dd13c8d | 294 | static inline void closure_init_stack(struct closure *cl) |
cafe5635 | 295 | { |
1dd13c8d KO |
296 | memset(cl, 0, sizeof(struct closure)); |
297 | atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER|CLOSURE_STACK); | |
cafe5635 KO |
298 | } |
299 | ||
cafe5635 | 300 | /** |
1dd13c8d | 301 | * closure_wake_up - wake up all closures on a wait list. |
cafe5635 KO |
302 | */ |
303 | static inline void closure_wake_up(struct closure_waitlist *list) | |
304 | { | |
305 | smp_mb(); | |
306 | __closure_wake_up(list); | |
307 | } | |
308 | ||
1dd13c8d KO |
309 | /** |
310 | * continue_at - jump to another function with barrier | |
311 | * | |
312 | * After @cl is no longer waiting on anything (i.e. all outstanding refs have | |
313 | * been dropped with closure_put()), it will resume execution at @fn running out | |
314 | * of @wq (or, if @wq is NULL, @fn will be called by closure_put() directly). | |
315 | * | |
1dd13c8d KO |
316 | * This is because after calling continue_at() you no longer have a ref on @cl, |
317 | * and whatever @cl owns may be freed out from under you - a running closure fn | |
318 | * has a ref on its own closure which continue_at() drops. | |
cafe5635 | 319 | */ |
cafe5635 KO |
320 | #define continue_at(_cl, _fn, _wq) \ |
321 | do { \ | |
322 | set_closure_fn(_cl, _fn, _wq); \ | |
323 | closure_sub(_cl, CLOSURE_RUNNING + 1); \ | |
cafe5635 KO |
324 | } while (0) |
325 | ||
1dd13c8d KO |
326 | /** |
327 | * closure_return - finish execution of a closure | |
328 | * | |
329 | * This is used to indicate that @cl is finished: when all outstanding refs on | |
330 | * @cl have been dropped @cl's ref on its parent closure (as passed to | |
331 | * closure_init()) will be dropped, if one was specified - thus this can be | |
332 | * thought of as returning to the parent closure. | |
333 | */ | |
cafe5635 KO |
334 | #define closure_return(_cl) continue_at((_cl), NULL, NULL) |
335 | ||
1dd13c8d KO |
336 | /** |
337 | * continue_at_nobarrier - jump to another function without barrier | |
338 | * | |
339 | * Causes @fn to be executed out of @cl, in @wq context (or called directly if | |
340 | * @wq is NULL). | |
341 | * | |
1dd13c8d KO |
342 | * The ref the caller of continue_at_nobarrier() had on @cl is now owned by @fn, |
343 | * thus it's not safe to touch anything protected by @cl after a | |
344 | * continue_at_nobarrier(). | |
345 | */ | |
cafe5635 KO |
346 | #define continue_at_nobarrier(_cl, _fn, _wq) \ |
347 | do { \ | |
348 | set_closure_fn(_cl, _fn, _wq); \ | |
a34a8bfd | 349 | closure_queue(_cl); \ |
cafe5635 KO |
350 | } while (0) |
351 | ||
1dd13c8d KO |
352 | /** |
353 | * closure_return - finish execution of a closure, with destructor | |
354 | * | |
355 | * Works like closure_return(), except @destructor will be called when all | |
356 | * outstanding refs on @cl have been dropped; @destructor may be used to safely | |
357 | * free the memory occupied by @cl, and it is called with the ref on the parent | |
358 | * closure still held - so @destructor could safely return an item to a | |
359 | * freelist protected by @cl's parent. | |
360 | */ | |
cafe5635 KO |
361 | #define closure_return_with_destructor(_cl, _destructor) \ |
362 | do { \ | |
363 | set_closure_fn(_cl, _destructor, NULL); \ | |
364 | closure_sub(_cl, CLOSURE_RUNNING - CLOSURE_DESTRUCTOR + 1); \ | |
cafe5635 KO |
365 | } while (0) |
366 | ||
1dd13c8d KO |
367 | /** |
368 | * closure_call - execute @fn out of a new, uninitialized closure | |
369 | * | |
370 | * Typically used when running out of one closure, and we want to run @fn | |
371 | * asynchronously out of a new closure - @parent will then wait for @cl to | |
372 | * finish. | |
373 | */ | |
cafe5635 KO |
374 | static inline void closure_call(struct closure *cl, closure_fn fn, |
375 | struct workqueue_struct *wq, | |
376 | struct closure *parent) | |
377 | { | |
378 | closure_init(cl, parent); | |
379 | continue_at_nobarrier(cl, fn, wq); | |
380 | } | |
381 | ||
cafe5635 | 382 | #endif /* _LINUX_CLOSURE_H */ |