closure.h 12 KB

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