closure.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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, int error)
  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. * For a closure to wait on an arbitrary event, we need to introduce waitlists:
  74. *
  75. * struct closure_waitlist list;
  76. * closure_wait_event(list, cl, condition);
  77. * closure_wake_up(wait_list);
  78. *
  79. * These work analagously to wait_event() and wake_up() - except that instead of
  80. * operating on the current thread (for wait_event()) and lists of threads, they
  81. * operate on an explicit closure and lists of closures.
  82. *
  83. * Because it's a closure we can now wait either synchronously or
  84. * asynchronously. closure_wait_event() returns the current value of the
  85. * condition, and if it returned false continue_at() or closure_sync() can be
  86. * used to wait for it to become true.
  87. *
  88. * It's useful for waiting on things when you can't sleep in the context in
  89. * which you must check the condition (perhaps a spinlock held, or you might be
  90. * beneath generic_make_request() - in which case you can't sleep on IO).
  91. *
  92. * closure_wait_event() will wait either synchronously or asynchronously,
  93. * depending on whether the closure is in blocking mode or not. You can pick a
  94. * mode explicitly with closure_wait_event_sync() and
  95. * closure_wait_event_async(), which do just what you might expect.
  96. *
  97. * Lastly, you might have a wait list dedicated to a specific event, and have no
  98. * need for specifying the condition - you just want to wait until someone runs
  99. * closure_wake_up() on the appropriate wait list. In that case, just use
  100. * closure_wait(). It will return either true or false, depending on whether the
  101. * closure was already on a wait list or not - a closure can only be on one wait
  102. * list at a time.
  103. *
  104. * Parents:
  105. *
  106. * closure_init() takes two arguments - it takes the closure to initialize, and
  107. * a (possibly null) parent.
  108. *
  109. * If parent is non null, the new closure will have a refcount for its lifetime;
  110. * a closure is considered to be "finished" when its refcount hits 0 and the
  111. * function to run is null. Hence
  112. *
  113. * continue_at(cl, NULL, NULL);
  114. *
  115. * returns up the (spaghetti) stack of closures, precisely like normal return
  116. * returns up the C stack. continue_at() with non null fn is better thought of
  117. * as doing a tail call.
  118. *
  119. * All this implies that a closure should typically be embedded in a particular
  120. * struct (which its refcount will normally control the lifetime of), and that
  121. * struct can very much be thought of as a stack frame.
  122. *
  123. * Locking:
  124. *
  125. * Closures are based on work items but they can be thought of as more like
  126. * threads - in that like threads and unlike work items they have a well
  127. * defined lifetime; they are created (with closure_init()) and eventually
  128. * complete after a continue_at(cl, NULL, NULL).
  129. *
  130. * Suppose you've got some larger structure with a closure embedded in it that's
  131. * used for periodically doing garbage collection. You only want one garbage
  132. * collection happening at a time, so the natural thing to do is protect it with
  133. * a lock. However, it's difficult to use a lock protecting a closure correctly
  134. * because the unlock should come after the last continue_to() (additionally, if
  135. * you're using the closure asynchronously a mutex won't work since a mutex has
  136. * to be unlocked by the same process that locked it).
  137. *
  138. * So to make it less error prone and more efficient, we also have the ability
  139. * to use closures as locks:
  140. *
  141. * closure_init_unlocked();
  142. * closure_trylock();
  143. *
  144. * That's all we need for trylock() - the last closure_put() implicitly unlocks
  145. * it for you. But for closure_lock(), we also need a wait list:
  146. *
  147. * struct closure_with_waitlist frobnicator_cl;
  148. *
  149. * closure_init_unlocked(&frobnicator_cl);
  150. * closure_lock(&frobnicator_cl);
  151. *
  152. * A closure_with_waitlist embeds a closure and a wait list - much like struct
  153. * delayed_work embeds a work item and a timer_list. The important thing is, use
  154. * it exactly like you would a regular closure and closure_put() will magically
  155. * handle everything for you.
  156. */
  157. struct closure;
  158. typedef void (closure_fn) (struct closure *);
  159. struct closure_waitlist {
  160. struct llist_head list;
  161. };
  162. enum closure_type {
  163. TYPE_closure = 0,
  164. TYPE_closure_with_waitlist = 1,
  165. MAX_CLOSURE_TYPE = 1,
  166. };
  167. enum closure_state {
  168. /*
  169. * CLOSURE_WAITING: Set iff the closure is on a waitlist. Must be set by
  170. * the thread that owns the closure, and cleared by the thread that's
  171. * waking up the closure.
  172. *
  173. * CLOSURE_SLEEPING: Must be set before a thread uses a closure to sleep
  174. * - indicates that cl->task is valid and closure_put() may wake it up.
  175. * Only set or cleared by the thread that owns the closure.
  176. *
  177. * The rest are for debugging and don't affect behaviour:
  178. *
  179. * CLOSURE_RUNNING: Set when a closure is running (i.e. by
  180. * closure_init() and when closure_put() runs then next function), and
  181. * must be cleared before remaining hits 0. Primarily to help guard
  182. * against incorrect usage and accidentally transferring references.
  183. * continue_at() and closure_return() clear it for you, if you're doing
  184. * something unusual you can use closure_set_dead() which also helps
  185. * annotate where references are being transferred.
  186. *
  187. * CLOSURE_STACK: Sanity check - remaining should never hit 0 on a
  188. * closure with this flag set
  189. */
  190. CLOSURE_BITS_START = (1 << 23),
  191. CLOSURE_DESTRUCTOR = (1 << 23),
  192. CLOSURE_WAITING = (1 << 25),
  193. CLOSURE_SLEEPING = (1 << 27),
  194. CLOSURE_RUNNING = (1 << 29),
  195. CLOSURE_STACK = (1 << 31),
  196. };
  197. #define CLOSURE_GUARD_MASK \
  198. ((CLOSURE_DESTRUCTOR|CLOSURE_WAITING|CLOSURE_SLEEPING| \
  199. CLOSURE_RUNNING|CLOSURE_STACK) << 1)
  200. #define CLOSURE_REMAINING_MASK (CLOSURE_BITS_START - 1)
  201. #define CLOSURE_REMAINING_INITIALIZER (1|CLOSURE_RUNNING)
  202. struct closure {
  203. union {
  204. struct {
  205. struct workqueue_struct *wq;
  206. struct task_struct *task;
  207. struct llist_node list;
  208. closure_fn *fn;
  209. };
  210. struct work_struct work;
  211. };
  212. struct closure *parent;
  213. atomic_t remaining;
  214. enum closure_type type;
  215. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  216. #define CLOSURE_MAGIC_DEAD 0xc054dead
  217. #define CLOSURE_MAGIC_ALIVE 0xc054a11e
  218. unsigned magic;
  219. struct list_head all;
  220. unsigned long ip;
  221. unsigned long waiting_on;
  222. #endif
  223. };
  224. struct closure_with_waitlist {
  225. struct closure cl;
  226. struct closure_waitlist wait;
  227. };
  228. extern unsigned invalid_closure_type(void);
  229. #define __CLOSURE_TYPE(cl, _t) \
  230. __builtin_types_compatible_p(typeof(cl), struct _t) \
  231. ? TYPE_ ## _t : \
  232. #define __closure_type(cl) \
  233. ( \
  234. __CLOSURE_TYPE(cl, closure) \
  235. __CLOSURE_TYPE(cl, closure_with_waitlist) \
  236. invalid_closure_type() \
  237. )
  238. void closure_sub(struct closure *cl, int v);
  239. void closure_put(struct closure *cl);
  240. void __closure_wake_up(struct closure_waitlist *list);
  241. bool closure_wait(struct closure_waitlist *list, struct closure *cl);
  242. void closure_sync(struct closure *cl);
  243. bool closure_trylock(struct closure *cl, struct closure *parent);
  244. void __closure_lock(struct closure *cl, struct closure *parent,
  245. struct closure_waitlist *wait_list);
  246. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  247. void closure_debug_init(void);
  248. void closure_debug_create(struct closure *cl);
  249. void closure_debug_destroy(struct closure *cl);
  250. #else
  251. static inline void closure_debug_init(void) {}
  252. static inline void closure_debug_create(struct closure *cl) {}
  253. static inline void closure_debug_destroy(struct closure *cl) {}
  254. #endif
  255. static inline void closure_set_ip(struct closure *cl)
  256. {
  257. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  258. cl->ip = _THIS_IP_;
  259. #endif
  260. }
  261. static inline void closure_set_ret_ip(struct closure *cl)
  262. {
  263. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  264. cl->ip = _RET_IP_;
  265. #endif
  266. }
  267. static inline void closure_get(struct closure *cl)
  268. {
  269. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  270. BUG_ON((atomic_inc_return(&cl->remaining) &
  271. CLOSURE_REMAINING_MASK) <= 1);
  272. #else
  273. atomic_inc(&cl->remaining);
  274. #endif
  275. }
  276. static inline void closure_set_stopped(struct closure *cl)
  277. {
  278. atomic_sub(CLOSURE_RUNNING, &cl->remaining);
  279. }
  280. static inline bool closure_is_unlocked(struct closure *cl)
  281. {
  282. return atomic_read(&cl->remaining) == -1;
  283. }
  284. static inline void do_closure_init(struct closure *cl, struct closure *parent,
  285. bool running)
  286. {
  287. cl->parent = parent;
  288. if (parent)
  289. closure_get(parent);
  290. if (running) {
  291. closure_debug_create(cl);
  292. atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER);
  293. } else
  294. atomic_set(&cl->remaining, -1);
  295. closure_set_ip(cl);
  296. }
  297. /*
  298. * Hack to get at the embedded closure if there is one, by doing an unsafe cast:
  299. * the result of __closure_type() is thrown away, it's used merely for type
  300. * checking.
  301. */
  302. #define __to_internal_closure(cl) \
  303. ({ \
  304. BUILD_BUG_ON(__closure_type(*cl) > MAX_CLOSURE_TYPE); \
  305. (struct closure *) cl; \
  306. })
  307. #define closure_init_type(cl, parent, running) \
  308. do { \
  309. struct closure *_cl = __to_internal_closure(cl); \
  310. _cl->type = __closure_type(*(cl)); \
  311. do_closure_init(_cl, parent, running); \
  312. } while (0)
  313. /**
  314. * __closure_init() - Initialize a closure, skipping the memset()
  315. *
  316. * May be used instead of closure_init() when memory has already been zeroed.
  317. */
  318. #define __closure_init(cl, parent) \
  319. closure_init_type(cl, parent, true)
  320. /**
  321. * closure_init() - Initialize a closure, setting the refcount to 1
  322. * @cl: closure to initialize
  323. * @parent: parent of the new closure. cl will take a refcount on it for its
  324. * lifetime; may be NULL.
  325. */
  326. #define closure_init(cl, parent) \
  327. do { \
  328. memset((cl), 0, sizeof(*(cl))); \
  329. __closure_init(cl, parent); \
  330. } while (0)
  331. static inline void closure_init_stack(struct closure *cl)
  332. {
  333. memset(cl, 0, sizeof(struct closure));
  334. atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER|CLOSURE_STACK);
  335. }
  336. /**
  337. * closure_init_unlocked() - Initialize a closure but leave it unlocked.
  338. * @cl: closure to initialize
  339. *
  340. * For when the closure will be used as a lock. The closure may not be used
  341. * until after a closure_lock() or closure_trylock().
  342. */
  343. #define closure_init_unlocked(cl) \
  344. do { \
  345. memset((cl), 0, sizeof(*(cl))); \
  346. closure_init_type(cl, NULL, false); \
  347. } while (0)
  348. /**
  349. * closure_lock() - lock and initialize a closure.
  350. * @cl: the closure to lock
  351. * @parent: the new parent for this closure
  352. *
  353. * The closure must be of one of the types that has a waitlist (otherwise we
  354. * wouldn't be able to sleep on contention).
  355. *
  356. * @parent has exactly the same meaning as in closure_init(); if non null, the
  357. * closure will take a reference on @parent which will be released when it is
  358. * unlocked.
  359. */
  360. #define closure_lock(cl, parent) \
  361. __closure_lock(__to_internal_closure(cl), parent, &(cl)->wait)
  362. static inline void __closure_end_sleep(struct closure *cl)
  363. {
  364. __set_current_state(TASK_RUNNING);
  365. if (atomic_read(&cl->remaining) & CLOSURE_SLEEPING)
  366. atomic_sub(CLOSURE_SLEEPING, &cl->remaining);
  367. }
  368. static inline void __closure_start_sleep(struct closure *cl)
  369. {
  370. closure_set_ip(cl);
  371. cl->task = current;
  372. set_current_state(TASK_UNINTERRUPTIBLE);
  373. if (!(atomic_read(&cl->remaining) & CLOSURE_SLEEPING))
  374. atomic_add(CLOSURE_SLEEPING, &cl->remaining);
  375. }
  376. /**
  377. * closure_wake_up() - wake up all closures on a wait list.
  378. */
  379. static inline void closure_wake_up(struct closure_waitlist *list)
  380. {
  381. smp_mb();
  382. __closure_wake_up(list);
  383. }
  384. /*
  385. * Wait on an event, synchronously or asynchronously - analogous to wait_event()
  386. * but for closures.
  387. *
  388. * The loop is oddly structured so as to avoid a race; we must check the
  389. * condition again after we've added ourself to the waitlist. We know if we were
  390. * already on the waitlist because closure_wait() returns false; thus, we only
  391. * schedule or break if closure_wait() returns false. If it returns true, we
  392. * just loop again - rechecking the condition.
  393. *
  394. * The __closure_wake_up() is necessary because we may race with the event
  395. * becoming true; i.e. we see event false -> wait -> recheck condition, but the
  396. * thread that made the event true may have called closure_wake_up() before we
  397. * added ourself to the wait list.
  398. *
  399. * We have to call closure_sync() at the end instead of just
  400. * __closure_end_sleep() because a different thread might've called
  401. * closure_wake_up() before us and gotten preempted before they dropped the
  402. * refcount on our closure. If this was a stack allocated closure, that would be
  403. * bad.
  404. */
  405. #define closure_wait_event(list, cl, condition) \
  406. ({ \
  407. typeof(condition) ret; \
  408. \
  409. while (1) { \
  410. ret = (condition); \
  411. if (ret) { \
  412. __closure_wake_up(list); \
  413. closure_sync(cl); \
  414. break; \
  415. } \
  416. \
  417. __closure_start_sleep(cl); \
  418. \
  419. if (!closure_wait(list, cl)) \
  420. schedule(); \
  421. } \
  422. \
  423. ret; \
  424. })
  425. static inline void closure_queue(struct closure *cl)
  426. {
  427. struct workqueue_struct *wq = cl->wq;
  428. if (wq) {
  429. INIT_WORK(&cl->work, cl->work.func);
  430. BUG_ON(!queue_work(wq, &cl->work));
  431. } else
  432. cl->fn(cl);
  433. }
  434. static inline void set_closure_fn(struct closure *cl, closure_fn *fn,
  435. struct workqueue_struct *wq)
  436. {
  437. BUG_ON(object_is_on_stack(cl));
  438. closure_set_ip(cl);
  439. cl->fn = fn;
  440. cl->wq = wq;
  441. /* between atomic_dec() in closure_put() */
  442. smp_mb__before_atomic_dec();
  443. }
  444. #define continue_at(_cl, _fn, _wq) \
  445. do { \
  446. set_closure_fn(_cl, _fn, _wq); \
  447. closure_sub(_cl, CLOSURE_RUNNING + 1); \
  448. return; \
  449. } while (0)
  450. #define closure_return(_cl) continue_at((_cl), NULL, NULL)
  451. #define continue_at_nobarrier(_cl, _fn, _wq) \
  452. do { \
  453. set_closure_fn(_cl, _fn, _wq); \
  454. closure_queue(_cl); \
  455. return; \
  456. } while (0)
  457. #define closure_return_with_destructor(_cl, _destructor) \
  458. do { \
  459. set_closure_fn(_cl, _destructor, NULL); \
  460. closure_sub(_cl, CLOSURE_RUNNING - CLOSURE_DESTRUCTOR + 1); \
  461. return; \
  462. } while (0)
  463. static inline void closure_call(struct closure *cl, closure_fn fn,
  464. struct workqueue_struct *wq,
  465. struct closure *parent)
  466. {
  467. closure_init(cl, parent);
  468. continue_at_nobarrier(cl, fn, wq);
  469. }
  470. static inline void closure_trylock_call(struct closure *cl, closure_fn fn,
  471. struct workqueue_struct *wq,
  472. struct closure *parent)
  473. {
  474. if (closure_trylock(cl, parent))
  475. continue_at_nobarrier(cl, fn, wq);
  476. }
  477. #endif /* _LINUX_CLOSURE_H */