closure.h 12 KB

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