closure.h 12 KB

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