···11111212#include "closure.h"13131414-#define CL_FIELD(type, field) \1515- case TYPE_ ## type: \1616- return &container_of(cl, struct type, cl)->field1717-1818-static struct closure_waitlist *closure_waitlist(struct closure *cl)1919-{2020- switch (cl->type) {2121- CL_FIELD(closure_with_waitlist, wait);2222- default:2323- return NULL;2424- }2525-}2626-2714static inline void closure_put_after_sub(struct closure *cl, int flags)2815{2916 int r = flags & CLOSURE_REMAINING_MASK;···2942 closure_queue(cl);3043 } else {3144 struct closure *parent = cl->parent;3232- struct closure_waitlist *wait = closure_waitlist(cl);3345 closure_fn *destructor = cl->fn;34463547 closure_debug_destroy(cl);3636-3737- smp_mb();3838- atomic_set(&cl->remaining, -1);3939-4040- if (wait)4141- closure_wake_up(wait);42484349 if (destructor)4450 destructor(cl);···4969}5070EXPORT_SYMBOL(closure_sub);51717272+/**7373+ * closure_put - decrement a closure's refcount7474+ */5275void closure_put(struct closure *cl)5376{5477 closure_put_after_sub(cl, atomic_dec_return(&cl->remaining));5578}5679EXPORT_SYMBOL(closure_put);57805858-static void set_waiting(struct closure *cl, unsigned long f)5959-{6060-#ifdef CONFIG_BCACHE_CLOSURES_DEBUG6161- cl->waiting_on = f;6262-#endif6363-}6464-8181+/**8282+ * closure_wake_up - wake up all closures on a wait list, without memory barrier8383+ */6584void __closure_wake_up(struct closure_waitlist *wait_list)6685{6786 struct llist_node *list;···85106 cl = container_of(reverse, struct closure, list);86107 reverse = llist_next(reverse);871088888- set_waiting(cl, 0);109109+ closure_set_waiting(cl, 0);89110 closure_sub(cl, CLOSURE_WAITING + 1);90111 }91112}92113EXPORT_SYMBOL(__closure_wake_up);931149494-bool closure_wait(struct closure_waitlist *list, struct closure *cl)115115+/**116116+ * closure_wait - add a closure to a waitlist117117+ *118118+ * @waitlist will own a ref on @cl, which will be released when119119+ * closure_wake_up() is called on @waitlist.120120+ *121121+ */122122+bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl)95123{96124 if (atomic_read(&cl->remaining) & CLOSURE_WAITING)97125 return false;981269999- set_waiting(cl, _RET_IP_);127127+ closure_set_waiting(cl, _RET_IP_);100128 atomic_add(CLOSURE_WAITING + 1, &cl->remaining);101101- llist_add(&cl->list, &list->list);129129+ llist_add(&cl->list, &waitlist->list);102130103131 return true;104132}105133EXPORT_SYMBOL(closure_wait);106134107135/**108108- * closure_sync() - sleep until a closure a closure has nothing left to wait on136136+ * closure_sync - sleep until a closure a closure has nothing left to wait on109137 *110138 * Sleeps until the refcount hits 1 - the thread that's running the closure owns111139 * the last refcount.···133147 __closure_end_sleep(cl);134148}135149EXPORT_SYMBOL(closure_sync);136136-137137-/**138138- * closure_trylock() - try to acquire the closure, without waiting139139- * @cl: closure to lock140140- *141141- * Returns true if the closure was succesfully locked.142142- */143143-bool closure_trylock(struct closure *cl, struct closure *parent)144144-{145145- if (atomic_cmpxchg(&cl->remaining, -1,146146- CLOSURE_REMAINING_INITIALIZER) != -1)147147- return false;148148-149149- smp_mb();150150-151151- cl->parent = parent;152152- if (parent)153153- closure_get(parent);154154-155155- closure_set_ret_ip(cl);156156- closure_debug_create(cl);157157- return true;158158-}159159-EXPORT_SYMBOL(closure_trylock);160160-161161-void __closure_lock(struct closure *cl, struct closure *parent,162162- struct closure_waitlist *wait_list)163163-{164164- struct closure wait;165165- closure_init_stack(&wait);166166-167167- while (1) {168168- if (closure_trylock(cl, parent))169169- return;170170-171171- closure_wait_event(wait_list, &wait,172172- atomic_read(&cl->remaining) == -1);173173- }174174-}175175-EXPORT_SYMBOL(__closure_lock);176150177151#ifdef CONFIG_BCACHE_CLOSURES_DEBUG178152
+110-246
drivers/md/bcache/closure.h
···7272 * closure - _always_ use continue_at(). Doing so consistently will help7373 * eliminate an entire class of particularly pernicious races.7474 *7575- * For a closure to wait on an arbitrary event, we need to introduce waitlists:7676- *7777- * struct closure_waitlist list;7878- * closure_wait_event(list, cl, condition);7979- * closure_wake_up(wait_list);8080- *8181- * These work analagously to wait_event() and wake_up() - except that instead of8282- * operating on the current thread (for wait_event()) and lists of threads, they8383- * operate on an explicit closure and lists of closures.8484- *8585- * Because it's a closure we can now wait either synchronously or8686- * asynchronously. closure_wait_event() returns the current value of the8787- * condition, and if it returned false continue_at() or closure_sync() can be8888- * used to wait for it to become true.8989- *9090- * It's useful for waiting on things when you can't sleep in the context in9191- * which you must check the condition (perhaps a spinlock held, or you might be9292- * beneath generic_make_request() - in which case you can't sleep on IO).9393- *9494- * closure_wait_event() will wait either synchronously or asynchronously,9595- * depending on whether the closure is in blocking mode or not. You can pick a9696- * mode explicitly with closure_wait_event_sync() and9797- * closure_wait_event_async(), which do just what you might expect.9898- *9975 * Lastly, you might have a wait list dedicated to a specific event, and have no10076 * need for specifying the condition - you just want to wait until someone runs10177 * closure_wake_up() on the appropriate wait list. In that case, just use···97121 * All this implies that a closure should typically be embedded in a particular98122 * struct (which its refcount will normally control the lifetime of), and that99123 * struct can very much be thought of as a stack frame.100100- *101101- * Locking:102102- *103103- * Closures are based on work items but they can be thought of as more like104104- * threads - in that like threads and unlike work items they have a well105105- * defined lifetime; they are created (with closure_init()) and eventually106106- * complete after a continue_at(cl, NULL, NULL).107107- *108108- * Suppose you've got some larger structure with a closure embedded in it that's109109- * used for periodically doing garbage collection. You only want one garbage110110- * collection happening at a time, so the natural thing to do is protect it with111111- * a lock. However, it's difficult to use a lock protecting a closure correctly112112- * because the unlock should come after the last continue_to() (additionally, if113113- * you're using the closure asynchronously a mutex won't work since a mutex has114114- * to be unlocked by the same process that locked it).115115- *116116- * So to make it less error prone and more efficient, we also have the ability117117- * to use closures as locks:118118- *119119- * closure_init_unlocked();120120- * closure_trylock();121121- *122122- * That's all we need for trylock() - the last closure_put() implicitly unlocks123123- * it for you. But for closure_lock(), we also need a wait list:124124- *125125- * struct closure_with_waitlist frobnicator_cl;126126- *127127- * closure_init_unlocked(&frobnicator_cl);128128- * closure_lock(&frobnicator_cl);129129- *130130- * A closure_with_waitlist embeds a closure and a wait list - much like struct131131- * delayed_work embeds a work item and a timer_list. The important thing is, use132132- * it exactly like you would a regular closure and closure_put() will magically133133- * handle everything for you.134124 */135125136126struct closure;···104162105163struct closure_waitlist {106164 struct llist_head list;107107-};108108-109109-enum closure_type {110110- TYPE_closure = 0,111111- TYPE_closure_with_waitlist = 1,112112- MAX_CLOSURE_TYPE = 1,113165};114166115167enum closure_state {···160224161225 atomic_t remaining;162226163163- enum closure_type type;164164-165227#ifdef CONFIG_BCACHE_CLOSURES_DEBUG166228#define CLOSURE_MAGIC_DEAD 0xc054dead167229#define CLOSURE_MAGIC_ALIVE 0xc054a11e···171237#endif172238};173239174174-struct closure_with_waitlist {175175- struct closure cl;176176- struct closure_waitlist wait;177177-};178178-179179-extern unsigned invalid_closure_type(void);180180-181181-#define __CLOSURE_TYPE(cl, _t) \182182- __builtin_types_compatible_p(typeof(cl), struct _t) \183183- ? TYPE_ ## _t : \184184-185185-#define __closure_type(cl) \186186-( \187187- __CLOSURE_TYPE(cl, closure) \188188- __CLOSURE_TYPE(cl, closure_with_waitlist) \189189- invalid_closure_type() \190190-)191191-192240void closure_sub(struct closure *cl, int v);193241void closure_put(struct closure *cl);194242void __closure_wake_up(struct closure_waitlist *list);195243bool closure_wait(struct closure_waitlist *list, struct closure *cl);196244void closure_sync(struct closure *cl);197197-198198-bool closure_trylock(struct closure *cl, struct closure *parent);199199-void __closure_lock(struct closure *cl, struct closure *parent,200200- struct closure_waitlist *wait_list);201245202246#ifdef CONFIG_BCACHE_CLOSURES_DEBUG203247···205293#endif206294}207295208208-static inline void closure_get(struct closure *cl)296296+static inline void closure_set_waiting(struct closure *cl, unsigned long f)209297{210298#ifdef CONFIG_BCACHE_CLOSURES_DEBUG211211- BUG_ON((atomic_inc_return(&cl->remaining) &212212- CLOSURE_REMAINING_MASK) <= 1);213213-#else214214- atomic_inc(&cl->remaining);299299+ cl->waiting_on = f;215300#endif216301}217217-218218-static inline void closure_set_stopped(struct closure *cl)219219-{220220- atomic_sub(CLOSURE_RUNNING, &cl->remaining);221221-}222222-223223-static inline bool closure_is_unlocked(struct closure *cl)224224-{225225- return atomic_read(&cl->remaining) == -1;226226-}227227-228228-static inline void do_closure_init(struct closure *cl, struct closure *parent,229229- bool running)230230-{231231- cl->parent = parent;232232- if (parent)233233- closure_get(parent);234234-235235- if (running) {236236- closure_debug_create(cl);237237- atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER);238238- } else239239- atomic_set(&cl->remaining, -1);240240-241241- closure_set_ip(cl);242242-}243243-244244-/*245245- * Hack to get at the embedded closure if there is one, by doing an unsafe cast:246246- * the result of __closure_type() is thrown away, it's used merely for type247247- * checking.248248- */249249-#define __to_internal_closure(cl) \250250-({ \251251- BUILD_BUG_ON(__closure_type(*cl) > MAX_CLOSURE_TYPE); \252252- (struct closure *) cl; \253253-})254254-255255-#define closure_init_type(cl, parent, running) \256256-do { \257257- struct closure *_cl = __to_internal_closure(cl); \258258- _cl->type = __closure_type(*(cl)); \259259- do_closure_init(_cl, parent, running); \260260-} while (0)261261-262262-/**263263- * closure_init() - Initialize a closure, setting the refcount to 1264264- * @cl: closure to initialize265265- * @parent: parent of the new closure. cl will take a refcount on it for its266266- * lifetime; may be NULL.267267- */268268-#define closure_init(cl, parent) \269269- closure_init_type(cl, parent, true)270270-271271-static inline void closure_init_stack(struct closure *cl)272272-{273273- memset(cl, 0, sizeof(struct closure));274274- atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER|CLOSURE_STACK);275275-}276276-277277-/**278278- * closure_init_unlocked() - Initialize a closure but leave it unlocked.279279- * @cl: closure to initialize280280- *281281- * For when the closure will be used as a lock. The closure may not be used282282- * until after a closure_lock() or closure_trylock().283283- */284284-#define closure_init_unlocked(cl) \285285-do { \286286- memset((cl), 0, sizeof(*(cl))); \287287- closure_init_type(cl, NULL, false); \288288-} while (0)289289-290290-/**291291- * closure_lock() - lock and initialize a closure.292292- * @cl: the closure to lock293293- * @parent: the new parent for this closure294294- *295295- * The closure must be of one of the types that has a waitlist (otherwise we296296- * wouldn't be able to sleep on contention).297297- *298298- * @parent has exactly the same meaning as in closure_init(); if non null, the299299- * closure will take a reference on @parent which will be released when it is300300- * unlocked.301301- */302302-#define closure_lock(cl, parent) \303303- __closure_lock(__to_internal_closure(cl), parent, &(cl)->wait)304302305303static inline void __closure_end_sleep(struct closure *cl)306304{···230408 atomic_add(CLOSURE_SLEEPING, &cl->remaining);231409}232410233233-/**234234- * closure_wake_up() - wake up all closures on a wait list.235235- */236236-static inline void closure_wake_up(struct closure_waitlist *list)411411+static inline void closure_set_stopped(struct closure *cl)237412{238238- smp_mb();239239- __closure_wake_up(list);240240-}241241-242242-/*243243- * Wait on an event, synchronously or asynchronously - analogous to wait_event()244244- * but for closures.245245- *246246- * The loop is oddly structured so as to avoid a race; we must check the247247- * condition again after we've added ourself to the waitlist. We know if we were248248- * already on the waitlist because closure_wait() returns false; thus, we only249249- * schedule or break if closure_wait() returns false. If it returns true, we250250- * just loop again - rechecking the condition.251251- *252252- * The __closure_wake_up() is necessary because we may race with the event253253- * becoming true; i.e. we see event false -> wait -> recheck condition, but the254254- * thread that made the event true may have called closure_wake_up() before we255255- * added ourself to the wait list.256256- *257257- * We have to call closure_sync() at the end instead of just258258- * __closure_end_sleep() because a different thread might've called259259- * closure_wake_up() before us and gotten preempted before they dropped the260260- * refcount on our closure. If this was a stack allocated closure, that would be261261- * bad.262262- */263263-#define closure_wait_event(list, cl, condition) \264264-({ \265265- typeof(condition) ret; \266266- \267267- while (1) { \268268- ret = (condition); \269269- if (ret) { \270270- __closure_wake_up(list); \271271- closure_sync(cl); \272272- break; \273273- } \274274- \275275- __closure_start_sleep(cl); \276276- \277277- if (!closure_wait(list, cl)) \278278- schedule(); \279279- } \280280- \281281- ret; \282282-})283283-284284-static inline void closure_queue(struct closure *cl)285285-{286286- struct workqueue_struct *wq = cl->wq;287287- if (wq) {288288- INIT_WORK(&cl->work, cl->work.func);289289- BUG_ON(!queue_work(wq, &cl->work));290290- } else291291- cl->fn(cl);413413+ atomic_sub(CLOSURE_RUNNING, &cl->remaining);292414}293415294416static inline void set_closure_fn(struct closure *cl, closure_fn *fn,···246480 smp_mb__before_atomic_dec();247481}248482483483+static inline void closure_queue(struct closure *cl)484484+{485485+ struct workqueue_struct *wq = cl->wq;486486+ if (wq) {487487+ INIT_WORK(&cl->work, cl->work.func);488488+ BUG_ON(!queue_work(wq, &cl->work));489489+ } else490490+ cl->fn(cl);491491+}492492+493493+/**494494+ * closure_get - increment a closure's refcount495495+ */496496+static inline void closure_get(struct closure *cl)497497+{498498+#ifdef CONFIG_BCACHE_CLOSURES_DEBUG499499+ BUG_ON((atomic_inc_return(&cl->remaining) &500500+ CLOSURE_REMAINING_MASK) <= 1);501501+#else502502+ atomic_inc(&cl->remaining);503503+#endif504504+}505505+506506+/**507507+ * closure_init - Initialize a closure, setting the refcount to 1508508+ * @cl: closure to initialize509509+ * @parent: parent of the new closure. cl will take a refcount on it for its510510+ * lifetime; may be NULL.511511+ */512512+static inline void closure_init(struct closure *cl, struct closure *parent)513513+{514514+ memset(cl, 0, sizeof(struct closure));515515+ cl->parent = parent;516516+ if (parent)517517+ closure_get(parent);518518+519519+ atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER);520520+521521+ closure_debug_create(cl);522522+ closure_set_ip(cl);523523+}524524+525525+static inline void closure_init_stack(struct closure *cl)526526+{527527+ memset(cl, 0, sizeof(struct closure));528528+ atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER|CLOSURE_STACK);529529+}530530+531531+/**532532+ * closure_wake_up - wake up all closures on a wait list.533533+ */534534+static inline void closure_wake_up(struct closure_waitlist *list)535535+{536536+ smp_mb();537537+ __closure_wake_up(list);538538+}539539+540540+/**541541+ * continue_at - jump to another function with barrier542542+ *543543+ * After @cl is no longer waiting on anything (i.e. all outstanding refs have544544+ * been dropped with closure_put()), it will resume execution at @fn running out545545+ * of @wq (or, if @wq is NULL, @fn will be called by closure_put() directly).546546+ *547547+ * NOTE: This macro expands to a return in the calling function!548548+ *549549+ * This is because after calling continue_at() you no longer have a ref on @cl,550550+ * and whatever @cl owns may be freed out from under you - a running closure fn551551+ * has a ref on its own closure which continue_at() drops.552552+ */249553#define continue_at(_cl, _fn, _wq) \250554do { \251555 set_closure_fn(_cl, _fn, _wq); \···323487 return; \324488} while (0)325489490490+/**491491+ * closure_return - finish execution of a closure492492+ *493493+ * This is used to indicate that @cl is finished: when all outstanding refs on494494+ * @cl have been dropped @cl's ref on its parent closure (as passed to495495+ * closure_init()) will be dropped, if one was specified - thus this can be496496+ * thought of as returning to the parent closure.497497+ */326498#define closure_return(_cl) continue_at((_cl), NULL, NULL)327499500500+/**501501+ * continue_at_nobarrier - jump to another function without barrier502502+ *503503+ * Causes @fn to be executed out of @cl, in @wq context (or called directly if504504+ * @wq is NULL).505505+ *506506+ * NOTE: like continue_at(), this macro expands to a return in the caller!507507+ *508508+ * The ref the caller of continue_at_nobarrier() had on @cl is now owned by @fn,509509+ * thus it's not safe to touch anything protected by @cl after a510510+ * continue_at_nobarrier().511511+ */328512#define continue_at_nobarrier(_cl, _fn, _wq) \329513do { \330514 set_closure_fn(_cl, _fn, _wq); \···352496 return; \353497} while (0)354498499499+/**500500+ * closure_return - finish execution of a closure, with destructor501501+ *502502+ * Works like closure_return(), except @destructor will be called when all503503+ * outstanding refs on @cl have been dropped; @destructor may be used to safely504504+ * free the memory occupied by @cl, and it is called with the ref on the parent505505+ * closure still held - so @destructor could safely return an item to a506506+ * freelist protected by @cl's parent.507507+ */355508#define closure_return_with_destructor(_cl, _destructor) \356509do { \357510 set_closure_fn(_cl, _destructor, NULL); \···368503 return; \369504} while (0)370505506506+/**507507+ * closure_call - execute @fn out of a new, uninitialized closure508508+ *509509+ * Typically used when running out of one closure, and we want to run @fn510510+ * asynchronously out of a new closure - @parent will then wait for @cl to511511+ * finish.512512+ */371513static inline void closure_call(struct closure *cl, closure_fn fn,372514 struct workqueue_struct *wq,373515 struct closure *parent)374516{375517 closure_init(cl, parent);376518 continue_at_nobarrier(cl, fn, wq);377377-}378378-379379-static inline void closure_trylock_call(struct closure *cl, closure_fn fn,380380- struct workqueue_struct *wq,381381- struct closure *parent)382382-{383383- if (closure_trylock(cl, parent))384384- continue_at_nobarrier(cl, fn, wq);385519}386520387521#endif /* _LINUX_CLOSURE_H */