Merge tag 'rtc-6.7' of git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux
[linux-block.git] / include / linux / closure.h
CommitLineData
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
105struct closure;
e4bf7919 106struct closure_syncer;
cafe5635 107typedef void (closure_fn) (struct closure *);
df2b9431 108extern struct dentry *bcache_debug;
cafe5635
KO
109
110struct closure_waitlist {
111 struct llist_head list;
112};
113
cafe5635
KO
114enum closure_state {
115 /*
cafe5635
KO
116 * CLOSURE_WAITING: Set iff the closure is on a waitlist. Must be set by
117 * the thread that owns the closure, and cleared by the thread that's
118 * waking up the closure.
119 *
cafe5635
KO
120 * The rest are for debugging and don't affect behaviour:
121 *
122 * CLOSURE_RUNNING: Set when a closure is running (i.e. by
123 * closure_init() and when closure_put() runs then next function), and
124 * must be cleared before remaining hits 0. Primarily to help guard
125 * against incorrect usage and accidentally transferring references.
126 * continue_at() and closure_return() clear it for you, if you're doing
127 * something unusual you can use closure_set_dead() which also helps
128 * annotate where references are being transferred.
cafe5635
KO
129 */
130
3609c471
ML
131 CLOSURE_BITS_START = (1U << 26),
132 CLOSURE_DESTRUCTOR = (1U << 26),
133 CLOSURE_WAITING = (1U << 28),
134 CLOSURE_RUNNING = (1U << 30),
cafe5635
KO
135};
136
137#define CLOSURE_GUARD_MASK \
e4bf7919 138 ((CLOSURE_DESTRUCTOR|CLOSURE_WAITING|CLOSURE_RUNNING) << 1)
cafe5635
KO
139
140#define CLOSURE_REMAINING_MASK (CLOSURE_BITS_START - 1)
141#define CLOSURE_REMAINING_INITIALIZER (1|CLOSURE_RUNNING)
142
143struct closure {
144 union {
145 struct {
146 struct workqueue_struct *wq;
e4bf7919 147 struct closure_syncer *s;
cafe5635
KO
148 struct llist_node list;
149 closure_fn *fn;
150 };
151 struct work_struct work;
152 };
153
154 struct closure *parent;
155
156 atomic_t remaining;
157
8c8d2d96 158#ifdef CONFIG_DEBUG_CLOSURES
cafe5635
KO
159#define CLOSURE_MAGIC_DEAD 0xc054dead
160#define CLOSURE_MAGIC_ALIVE 0xc054a11e
161
6f10f7d1 162 unsigned int magic;
cafe5635
KO
163 struct list_head all;
164 unsigned long ip;
165 unsigned long waiting_on;
166#endif
167};
168
cafe5635
KO
169void closure_sub(struct closure *cl, int v);
170void closure_put(struct closure *cl);
cafe5635
KO
171void __closure_wake_up(struct closure_waitlist *list);
172bool closure_wait(struct closure_waitlist *list, struct closure *cl);
e4bf7919
KO
173void __closure_sync(struct closure *cl);
174
48b79357
KO
175static inline unsigned closure_nr_remaining(struct closure *cl)
176{
177 return atomic_read(&cl->remaining) & CLOSURE_REMAINING_MASK;
178}
179
e4bf7919
KO
180/**
181 * closure_sync - sleep until a closure a closure has nothing left to wait on
182 *
183 * Sleeps until the refcount hits 1 - the thread that's running the closure owns
184 * the last refcount.
185 */
186static inline void closure_sync(struct closure *cl)
187{
48b79357 188 if (closure_nr_remaining(cl) != 1)
e4bf7919
KO
189 __closure_sync(cl);
190}
cafe5635 191
8c8d2d96 192#ifdef CONFIG_DEBUG_CLOSURES
cafe5635
KO
193
194void closure_debug_create(struct closure *cl);
195void closure_debug_destroy(struct closure *cl);
196
197#else
198
199static inline void closure_debug_create(struct closure *cl) {}
200static inline void closure_debug_destroy(struct closure *cl) {}
201
202#endif
203
204static inline void closure_set_ip(struct closure *cl)
205{
8c8d2d96 206#ifdef CONFIG_DEBUG_CLOSURES
cafe5635
KO
207 cl->ip = _THIS_IP_;
208#endif
209}
210
211static inline void closure_set_ret_ip(struct closure *cl)
212{
8c8d2d96 213#ifdef CONFIG_DEBUG_CLOSURES
cafe5635
KO
214 cl->ip = _RET_IP_;
215#endif
216}
217
1dd13c8d 218static inline void closure_set_waiting(struct closure *cl, unsigned long f)
cafe5635 219{
8c8d2d96 220#ifdef CONFIG_DEBUG_CLOSURES
1dd13c8d 221 cl->waiting_on = f;
cafe5635
KO
222#endif
223}
224
225static inline void closure_set_stopped(struct closure *cl)
226{
227 atomic_sub(CLOSURE_RUNNING, &cl->remaining);
228}
229
1dd13c8d
KO
230static inline void set_closure_fn(struct closure *cl, closure_fn *fn,
231 struct workqueue_struct *wq)
cafe5635 232{
1dd13c8d
KO
233 closure_set_ip(cl);
234 cl->fn = fn;
235 cl->wq = wq;
236 /* between atomic_dec() in closure_put() */
4e857c58 237 smp_mb__before_atomic();
cafe5635
KO
238}
239
1dd13c8d 240static inline void closure_queue(struct closure *cl)
cafe5635 241{
1dd13c8d 242 struct workqueue_struct *wq = cl->wq;
6446c684
LC
243 /**
244 * Changes made to closure, work_struct, or a couple of other structs
245 * may cause work.func not pointing to the right location.
246 */
247 BUILD_BUG_ON(offsetof(struct closure, fn)
248 != offsetof(struct work_struct, func));
8c8d2d96 249
1dd13c8d
KO
250 if (wq) {
251 INIT_WORK(&cl->work, cl->work.func);
252 BUG_ON(!queue_work(wq, &cl->work));
cafe5635 253 } else
1dd13c8d 254 cl->fn(cl);
cafe5635
KO
255}
256
1dd13c8d
KO
257/**
258 * closure_get - increment a closure's refcount
cafe5635 259 */
1dd13c8d
KO
260static inline void closure_get(struct closure *cl)
261{
8c8d2d96 262#ifdef CONFIG_DEBUG_CLOSURES
1dd13c8d
KO
263 BUG_ON((atomic_inc_return(&cl->remaining) &
264 CLOSURE_REMAINING_MASK) <= 1);
265#else
266 atomic_inc(&cl->remaining);
267#endif
268}
cafe5635 269
cafe5635 270/**
1dd13c8d 271 * closure_init - Initialize a closure, setting the refcount to 1
cafe5635
KO
272 * @cl: closure to initialize
273 * @parent: parent of the new closure. cl will take a refcount on it for its
274 * lifetime; may be NULL.
275 */
1dd13c8d 276static inline void closure_init(struct closure *cl, struct closure *parent)
cafe5635 277{
8c8d2d96 278 cl->fn = NULL;
1dd13c8d
KO
279 cl->parent = parent;
280 if (parent)
281 closure_get(parent);
cafe5635 282
1dd13c8d 283 atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER);
cafe5635 284
1dd13c8d
KO
285 closure_debug_create(cl);
286 closure_set_ip(cl);
cafe5635
KO
287}
288
1dd13c8d 289static inline void closure_init_stack(struct closure *cl)
cafe5635 290{
1dd13c8d 291 memset(cl, 0, sizeof(struct closure));
e4bf7919 292 atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER);
cafe5635
KO
293}
294
cafe5635 295/**
eb2b3d03
CL
296 * closure_wake_up - wake up all closures on a wait list,
297 * with memory barrier
cafe5635
KO
298 */
299static inline void closure_wake_up(struct closure_waitlist *list)
300{
eb2b3d03 301 /* Memory barrier for the wait list */
cafe5635
KO
302 smp_mb();
303 __closure_wake_up(list);
304}
305
1dd13c8d
KO
306/**
307 * continue_at - jump to another function with barrier
308 *
309 * After @cl is no longer waiting on anything (i.e. all outstanding refs have
310 * been dropped with closure_put()), it will resume execution at @fn running out
311 * of @wq (or, if @wq is NULL, @fn will be called by closure_put() directly).
312 *
1dd13c8d
KO
313 * This is because after calling continue_at() you no longer have a ref on @cl,
314 * and whatever @cl owns may be freed out from under you - a running closure fn
315 * has a ref on its own closure which continue_at() drops.
e4bf7919
KO
316 *
317 * Note you are expected to immediately return after using this macro.
cafe5635 318 */
cafe5635
KO
319#define continue_at(_cl, _fn, _wq) \
320do { \
321 set_closure_fn(_cl, _fn, _wq); \
322 closure_sub(_cl, CLOSURE_RUNNING + 1); \
cafe5635
KO
323} while (0)
324
1dd13c8d
KO
325/**
326 * closure_return - finish execution of a closure
327 *
328 * This is used to indicate that @cl is finished: when all outstanding refs on
329 * @cl have been dropped @cl's ref on its parent closure (as passed to
330 * closure_init()) will be dropped, if one was specified - thus this can be
331 * thought of as returning to the parent closure.
332 */
cafe5635
KO
333#define closure_return(_cl) continue_at((_cl), NULL, NULL)
334
1dd13c8d
KO
335/**
336 * continue_at_nobarrier - jump to another function without barrier
337 *
338 * Causes @fn to be executed out of @cl, in @wq context (or called directly if
339 * @wq is NULL).
340 *
1dd13c8d
KO
341 * The ref the caller of continue_at_nobarrier() had on @cl is now owned by @fn,
342 * thus it's not safe to touch anything protected by @cl after a
343 * continue_at_nobarrier().
344 */
cafe5635
KO
345#define continue_at_nobarrier(_cl, _fn, _wq) \
346do { \
347 set_closure_fn(_cl, _fn, _wq); \
a34a8bfd 348 closure_queue(_cl); \
cafe5635
KO
349} while (0)
350
1dd13c8d 351/**
4516da42
CL
352 * closure_return_with_destructor - finish execution of a closure,
353 * with destructor
1dd13c8d
KO
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) \
362do { \
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
374static 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
ced58fc7
KO
382#define __closure_wait_event(waitlist, _cond) \
383do { \
384 struct closure cl; \
385 \
386 closure_init_stack(&cl); \
387 \
388 while (1) { \
389 closure_wait(waitlist, &cl); \
390 if (_cond) \
391 break; \
392 closure_sync(&cl); \
393 } \
394 closure_wake_up(waitlist); \
395 closure_sync(&cl); \
396} while (0)
397
398#define closure_wait_event(waitlist, _cond) \
399do { \
400 if (!(_cond)) \
401 __closure_wait_event(waitlist, _cond); \
402} while (0)
403
cafe5635 404#endif /* _LINUX_CLOSURE_H */