Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bcache: kill closure locking code

Also flesh out the documentation a bit

Signed-off-by: Kent Overstreet <kmo@daterainc.com>

+128 -318
+18 -72
drivers/md/bcache/closure.c
··· 11 11 12 12 #include "closure.h" 13 13 14 - #define CL_FIELD(type, field) \ 15 - case TYPE_ ## type: \ 16 - return &container_of(cl, struct type, cl)->field 17 - 18 - static struct closure_waitlist *closure_waitlist(struct closure *cl) 19 - { 20 - switch (cl->type) { 21 - CL_FIELD(closure_with_waitlist, wait); 22 - default: 23 - return NULL; 24 - } 25 - } 26 - 27 14 static inline void closure_put_after_sub(struct closure *cl, int flags) 28 15 { 29 16 int r = flags & CLOSURE_REMAINING_MASK; ··· 29 42 closure_queue(cl); 30 43 } else { 31 44 struct closure *parent = cl->parent; 32 - struct closure_waitlist *wait = closure_waitlist(cl); 33 45 closure_fn *destructor = cl->fn; 34 46 35 47 closure_debug_destroy(cl); 36 - 37 - smp_mb(); 38 - atomic_set(&cl->remaining, -1); 39 - 40 - if (wait) 41 - closure_wake_up(wait); 42 48 43 49 if (destructor) 44 50 destructor(cl); ··· 49 69 } 50 70 EXPORT_SYMBOL(closure_sub); 51 71 72 + /** 73 + * closure_put - decrement a closure's refcount 74 + */ 52 75 void closure_put(struct closure *cl) 53 76 { 54 77 closure_put_after_sub(cl, atomic_dec_return(&cl->remaining)); 55 78 } 56 79 EXPORT_SYMBOL(closure_put); 57 80 58 - static void set_waiting(struct closure *cl, unsigned long f) 59 - { 60 - #ifdef CONFIG_BCACHE_CLOSURES_DEBUG 61 - cl->waiting_on = f; 62 - #endif 63 - } 64 - 81 + /** 82 + * closure_wake_up - wake up all closures on a wait list, without memory barrier 83 + */ 65 84 void __closure_wake_up(struct closure_waitlist *wait_list) 66 85 { 67 86 struct llist_node *list; ··· 85 106 cl = container_of(reverse, struct closure, list); 86 107 reverse = llist_next(reverse); 87 108 88 - set_waiting(cl, 0); 109 + closure_set_waiting(cl, 0); 89 110 closure_sub(cl, CLOSURE_WAITING + 1); 90 111 } 91 112 } 92 113 EXPORT_SYMBOL(__closure_wake_up); 93 114 94 - bool closure_wait(struct closure_waitlist *list, struct closure *cl) 115 + /** 116 + * closure_wait - add a closure to a waitlist 117 + * 118 + * @waitlist will own a ref on @cl, which will be released when 119 + * closure_wake_up() is called on @waitlist. 120 + * 121 + */ 122 + bool closure_wait(struct closure_waitlist *waitlist, struct closure *cl) 95 123 { 96 124 if (atomic_read(&cl->remaining) & CLOSURE_WAITING) 97 125 return false; 98 126 99 - set_waiting(cl, _RET_IP_); 127 + closure_set_waiting(cl, _RET_IP_); 100 128 atomic_add(CLOSURE_WAITING + 1, &cl->remaining); 101 - llist_add(&cl->list, &list->list); 129 + llist_add(&cl->list, &waitlist->list); 102 130 103 131 return true; 104 132 } 105 133 EXPORT_SYMBOL(closure_wait); 106 134 107 135 /** 108 - * closure_sync() - sleep until a closure a closure has nothing left to wait on 136 + * closure_sync - sleep until a closure a closure has nothing left to wait on 109 137 * 110 138 * Sleeps until the refcount hits 1 - the thread that's running the closure owns 111 139 * the last refcount. ··· 133 147 __closure_end_sleep(cl); 134 148 } 135 149 EXPORT_SYMBOL(closure_sync); 136 - 137 - /** 138 - * closure_trylock() - try to acquire the closure, without waiting 139 - * @cl: closure to lock 140 - * 141 - * Returns true if the closure was succesfully locked. 142 - */ 143 - bool closure_trylock(struct closure *cl, struct closure *parent) 144 - { 145 - if (atomic_cmpxchg(&cl->remaining, -1, 146 - CLOSURE_REMAINING_INITIALIZER) != -1) 147 - return false; 148 - 149 - smp_mb(); 150 - 151 - cl->parent = parent; 152 - if (parent) 153 - closure_get(parent); 154 - 155 - closure_set_ret_ip(cl); 156 - closure_debug_create(cl); 157 - return true; 158 - } 159 - EXPORT_SYMBOL(closure_trylock); 160 - 161 - void __closure_lock(struct closure *cl, struct closure *parent, 162 - struct closure_waitlist *wait_list) 163 - { 164 - struct closure wait; 165 - closure_init_stack(&wait); 166 - 167 - while (1) { 168 - if (closure_trylock(cl, parent)) 169 - return; 170 - 171 - closure_wait_event(wait_list, &wait, 172 - atomic_read(&cl->remaining) == -1); 173 - } 174 - } 175 - EXPORT_SYMBOL(__closure_lock); 176 150 177 151 #ifdef CONFIG_BCACHE_CLOSURES_DEBUG 178 152
+110 -246
drivers/md/bcache/closure.h
··· 72 72 * closure - _always_ use continue_at(). Doing so consistently will help 73 73 * eliminate an entire class of particularly pernicious races. 74 74 * 75 - * For a closure to wait on an arbitrary event, we need to introduce waitlists: 76 - * 77 - * struct closure_waitlist list; 78 - * closure_wait_event(list, cl, condition); 79 - * closure_wake_up(wait_list); 80 - * 81 - * These work analagously to wait_event() and wake_up() - except that instead of 82 - * operating on the current thread (for wait_event()) and lists of threads, they 83 - * operate on an explicit closure and lists of closures. 84 - * 85 - * Because it's a closure we can now wait either synchronously or 86 - * asynchronously. closure_wait_event() returns the current value of the 87 - * condition, and if it returned false continue_at() or closure_sync() can be 88 - * used to wait for it to become true. 89 - * 90 - * It's useful for waiting on things when you can't sleep in the context in 91 - * which you must check the condition (perhaps a spinlock held, or you might be 92 - * beneath generic_make_request() - in which case you can't sleep on IO). 93 - * 94 - * closure_wait_event() will wait either synchronously or asynchronously, 95 - * depending on whether the closure is in blocking mode or not. You can pick a 96 - * mode explicitly with closure_wait_event_sync() and 97 - * closure_wait_event_async(), which do just what you might expect. 98 - * 99 75 * Lastly, you might have a wait list dedicated to a specific event, and have no 100 76 * need for specifying the condition - you just want to wait until someone runs 101 77 * closure_wake_up() on the appropriate wait list. In that case, just use ··· 97 121 * All this implies that a closure should typically be embedded in a particular 98 122 * struct (which its refcount will normally control the lifetime of), and that 99 123 * struct can very much be thought of as a stack frame. 100 - * 101 - * Locking: 102 - * 103 - * Closures are based on work items but they can be thought of as more like 104 - * threads - in that like threads and unlike work items they have a well 105 - * defined lifetime; they are created (with closure_init()) and eventually 106 - * complete after a continue_at(cl, NULL, NULL). 107 - * 108 - * Suppose you've got some larger structure with a closure embedded in it that's 109 - * used for periodically doing garbage collection. You only want one garbage 110 - * collection happening at a time, so the natural thing to do is protect it with 111 - * a lock. However, it's difficult to use a lock protecting a closure correctly 112 - * because the unlock should come after the last continue_to() (additionally, if 113 - * you're using the closure asynchronously a mutex won't work since a mutex has 114 - * to be unlocked by the same process that locked it). 115 - * 116 - * So to make it less error prone and more efficient, we also have the ability 117 - * to use closures as locks: 118 - * 119 - * closure_init_unlocked(); 120 - * closure_trylock(); 121 - * 122 - * That's all we need for trylock() - the last closure_put() implicitly unlocks 123 - * it for you. But for closure_lock(), we also need a wait list: 124 - * 125 - * struct closure_with_waitlist frobnicator_cl; 126 - * 127 - * closure_init_unlocked(&frobnicator_cl); 128 - * closure_lock(&frobnicator_cl); 129 - * 130 - * A closure_with_waitlist embeds a closure and a wait list - much like struct 131 - * delayed_work embeds a work item and a timer_list. The important thing is, use 132 - * it exactly like you would a regular closure and closure_put() will magically 133 - * handle everything for you. 134 124 */ 135 125 136 126 struct closure; ··· 104 162 105 163 struct closure_waitlist { 106 164 struct llist_head list; 107 - }; 108 - 109 - enum closure_type { 110 - TYPE_closure = 0, 111 - TYPE_closure_with_waitlist = 1, 112 - MAX_CLOSURE_TYPE = 1, 113 165 }; 114 166 115 167 enum closure_state { ··· 160 224 161 225 atomic_t remaining; 162 226 163 - enum closure_type type; 164 - 165 227 #ifdef CONFIG_BCACHE_CLOSURES_DEBUG 166 228 #define CLOSURE_MAGIC_DEAD 0xc054dead 167 229 #define CLOSURE_MAGIC_ALIVE 0xc054a11e ··· 171 237 #endif 172 238 }; 173 239 174 - struct closure_with_waitlist { 175 - struct closure cl; 176 - struct closure_waitlist wait; 177 - }; 178 - 179 - extern unsigned invalid_closure_type(void); 180 - 181 - #define __CLOSURE_TYPE(cl, _t) \ 182 - __builtin_types_compatible_p(typeof(cl), struct _t) \ 183 - ? TYPE_ ## _t : \ 184 - 185 - #define __closure_type(cl) \ 186 - ( \ 187 - __CLOSURE_TYPE(cl, closure) \ 188 - __CLOSURE_TYPE(cl, closure_with_waitlist) \ 189 - invalid_closure_type() \ 190 - ) 191 - 192 240 void closure_sub(struct closure *cl, int v); 193 241 void closure_put(struct closure *cl); 194 242 void __closure_wake_up(struct closure_waitlist *list); 195 243 bool closure_wait(struct closure_waitlist *list, struct closure *cl); 196 244 void closure_sync(struct closure *cl); 197 - 198 - bool closure_trylock(struct closure *cl, struct closure *parent); 199 - void __closure_lock(struct closure *cl, struct closure *parent, 200 - struct closure_waitlist *wait_list); 201 245 202 246 #ifdef CONFIG_BCACHE_CLOSURES_DEBUG 203 247 ··· 205 293 #endif 206 294 } 207 295 208 - static inline void closure_get(struct closure *cl) 296 + static inline void closure_set_waiting(struct closure *cl, unsigned long f) 209 297 { 210 298 #ifdef CONFIG_BCACHE_CLOSURES_DEBUG 211 - BUG_ON((atomic_inc_return(&cl->remaining) & 212 - CLOSURE_REMAINING_MASK) <= 1); 213 - #else 214 - atomic_inc(&cl->remaining); 299 + cl->waiting_on = f; 215 300 #endif 216 301 } 217 - 218 - static inline void closure_set_stopped(struct closure *cl) 219 - { 220 - atomic_sub(CLOSURE_RUNNING, &cl->remaining); 221 - } 222 - 223 - static inline bool closure_is_unlocked(struct closure *cl) 224 - { 225 - return atomic_read(&cl->remaining) == -1; 226 - } 227 - 228 - static inline void do_closure_init(struct closure *cl, struct closure *parent, 229 - bool running) 230 - { 231 - cl->parent = parent; 232 - if (parent) 233 - closure_get(parent); 234 - 235 - if (running) { 236 - closure_debug_create(cl); 237 - atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER); 238 - } else 239 - atomic_set(&cl->remaining, -1); 240 - 241 - closure_set_ip(cl); 242 - } 243 - 244 - /* 245 - * Hack to get at the embedded closure if there is one, by doing an unsafe cast: 246 - * the result of __closure_type() is thrown away, it's used merely for type 247 - * checking. 248 - */ 249 - #define __to_internal_closure(cl) \ 250 - ({ \ 251 - BUILD_BUG_ON(__closure_type(*cl) > MAX_CLOSURE_TYPE); \ 252 - (struct closure *) cl; \ 253 - }) 254 - 255 - #define closure_init_type(cl, parent, running) \ 256 - do { \ 257 - struct closure *_cl = __to_internal_closure(cl); \ 258 - _cl->type = __closure_type(*(cl)); \ 259 - do_closure_init(_cl, parent, running); \ 260 - } while (0) 261 - 262 - /** 263 - * closure_init() - Initialize a closure, setting the refcount to 1 264 - * @cl: closure to initialize 265 - * @parent: parent of the new closure. cl will take a refcount on it for its 266 - * lifetime; may be NULL. 267 - */ 268 - #define closure_init(cl, parent) \ 269 - closure_init_type(cl, parent, true) 270 - 271 - static inline void closure_init_stack(struct closure *cl) 272 - { 273 - memset(cl, 0, sizeof(struct closure)); 274 - atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER|CLOSURE_STACK); 275 - } 276 - 277 - /** 278 - * closure_init_unlocked() - Initialize a closure but leave it unlocked. 279 - * @cl: closure to initialize 280 - * 281 - * For when the closure will be used as a lock. The closure may not be used 282 - * until after a closure_lock() or closure_trylock(). 283 - */ 284 - #define closure_init_unlocked(cl) \ 285 - do { \ 286 - memset((cl), 0, sizeof(*(cl))); \ 287 - closure_init_type(cl, NULL, false); \ 288 - } while (0) 289 - 290 - /** 291 - * closure_lock() - lock and initialize a closure. 292 - * @cl: the closure to lock 293 - * @parent: the new parent for this closure 294 - * 295 - * The closure must be of one of the types that has a waitlist (otherwise we 296 - * wouldn't be able to sleep on contention). 297 - * 298 - * @parent has exactly the same meaning as in closure_init(); if non null, the 299 - * closure will take a reference on @parent which will be released when it is 300 - * unlocked. 301 - */ 302 - #define closure_lock(cl, parent) \ 303 - __closure_lock(__to_internal_closure(cl), parent, &(cl)->wait) 304 302 305 303 static inline void __closure_end_sleep(struct closure *cl) 306 304 { ··· 230 408 atomic_add(CLOSURE_SLEEPING, &cl->remaining); 231 409 } 232 410 233 - /** 234 - * closure_wake_up() - wake up all closures on a wait list. 235 - */ 236 - static inline void closure_wake_up(struct closure_waitlist *list) 411 + static inline void closure_set_stopped(struct closure *cl) 237 412 { 238 - smp_mb(); 239 - __closure_wake_up(list); 240 - } 241 - 242 - /* 243 - * Wait on an event, synchronously or asynchronously - analogous to wait_event() 244 - * but for closures. 245 - * 246 - * The loop is oddly structured so as to avoid a race; we must check the 247 - * condition again after we've added ourself to the waitlist. We know if we were 248 - * already on the waitlist because closure_wait() returns false; thus, we only 249 - * schedule or break if closure_wait() returns false. If it returns true, we 250 - * just loop again - rechecking the condition. 251 - * 252 - * The __closure_wake_up() is necessary because we may race with the event 253 - * becoming true; i.e. we see event false -> wait -> recheck condition, but the 254 - * thread that made the event true may have called closure_wake_up() before we 255 - * added ourself to the wait list. 256 - * 257 - * We have to call closure_sync() at the end instead of just 258 - * __closure_end_sleep() because a different thread might've called 259 - * closure_wake_up() before us and gotten preempted before they dropped the 260 - * refcount on our closure. If this was a stack allocated closure, that would be 261 - * bad. 262 - */ 263 - #define closure_wait_event(list, cl, condition) \ 264 - ({ \ 265 - typeof(condition) ret; \ 266 - \ 267 - while (1) { \ 268 - ret = (condition); \ 269 - if (ret) { \ 270 - __closure_wake_up(list); \ 271 - closure_sync(cl); \ 272 - break; \ 273 - } \ 274 - \ 275 - __closure_start_sleep(cl); \ 276 - \ 277 - if (!closure_wait(list, cl)) \ 278 - schedule(); \ 279 - } \ 280 - \ 281 - ret; \ 282 - }) 283 - 284 - static inline void closure_queue(struct closure *cl) 285 - { 286 - struct workqueue_struct *wq = cl->wq; 287 - if (wq) { 288 - INIT_WORK(&cl->work, cl->work.func); 289 - BUG_ON(!queue_work(wq, &cl->work)); 290 - } else 291 - cl->fn(cl); 413 + atomic_sub(CLOSURE_RUNNING, &cl->remaining); 292 414 } 293 415 294 416 static inline void set_closure_fn(struct closure *cl, closure_fn *fn, ··· 246 480 smp_mb__before_atomic_dec(); 247 481 } 248 482 483 + static inline void closure_queue(struct closure *cl) 484 + { 485 + struct workqueue_struct *wq = cl->wq; 486 + if (wq) { 487 + INIT_WORK(&cl->work, cl->work.func); 488 + BUG_ON(!queue_work(wq, &cl->work)); 489 + } else 490 + cl->fn(cl); 491 + } 492 + 493 + /** 494 + * closure_get - increment a closure's refcount 495 + */ 496 + static inline void closure_get(struct closure *cl) 497 + { 498 + #ifdef CONFIG_BCACHE_CLOSURES_DEBUG 499 + BUG_ON((atomic_inc_return(&cl->remaining) & 500 + CLOSURE_REMAINING_MASK) <= 1); 501 + #else 502 + atomic_inc(&cl->remaining); 503 + #endif 504 + } 505 + 506 + /** 507 + * closure_init - Initialize a closure, setting the refcount to 1 508 + * @cl: closure to initialize 509 + * @parent: parent of the new closure. cl will take a refcount on it for its 510 + * lifetime; may be NULL. 511 + */ 512 + static inline void closure_init(struct closure *cl, struct closure *parent) 513 + { 514 + memset(cl, 0, sizeof(struct closure)); 515 + cl->parent = parent; 516 + if (parent) 517 + closure_get(parent); 518 + 519 + atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER); 520 + 521 + closure_debug_create(cl); 522 + closure_set_ip(cl); 523 + } 524 + 525 + static inline void closure_init_stack(struct closure *cl) 526 + { 527 + memset(cl, 0, sizeof(struct closure)); 528 + atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER|CLOSURE_STACK); 529 + } 530 + 531 + /** 532 + * closure_wake_up - wake up all closures on a wait list. 533 + */ 534 + static inline void closure_wake_up(struct closure_waitlist *list) 535 + { 536 + smp_mb(); 537 + __closure_wake_up(list); 538 + } 539 + 540 + /** 541 + * continue_at - jump to another function with barrier 542 + * 543 + * After @cl is no longer waiting on anything (i.e. all outstanding refs have 544 + * been dropped with closure_put()), it will resume execution at @fn running out 545 + * of @wq (or, if @wq is NULL, @fn will be called by closure_put() directly). 546 + * 547 + * NOTE: This macro expands to a return in the calling function! 548 + * 549 + * This is because after calling continue_at() you no longer have a ref on @cl, 550 + * and whatever @cl owns may be freed out from under you - a running closure fn 551 + * has a ref on its own closure which continue_at() drops. 552 + */ 249 553 #define continue_at(_cl, _fn, _wq) \ 250 554 do { \ 251 555 set_closure_fn(_cl, _fn, _wq); \ ··· 323 487 return; \ 324 488 } while (0) 325 489 490 + /** 491 + * closure_return - finish execution of a closure 492 + * 493 + * This is used to indicate that @cl is finished: when all outstanding refs on 494 + * @cl have been dropped @cl's ref on its parent closure (as passed to 495 + * closure_init()) will be dropped, if one was specified - thus this can be 496 + * thought of as returning to the parent closure. 497 + */ 326 498 #define closure_return(_cl) continue_at((_cl), NULL, NULL) 327 499 500 + /** 501 + * continue_at_nobarrier - jump to another function without barrier 502 + * 503 + * Causes @fn to be executed out of @cl, in @wq context (or called directly if 504 + * @wq is NULL). 505 + * 506 + * NOTE: like continue_at(), this macro expands to a return in the caller! 507 + * 508 + * The ref the caller of continue_at_nobarrier() had on @cl is now owned by @fn, 509 + * thus it's not safe to touch anything protected by @cl after a 510 + * continue_at_nobarrier(). 511 + */ 328 512 #define continue_at_nobarrier(_cl, _fn, _wq) \ 329 513 do { \ 330 514 set_closure_fn(_cl, _fn, _wq); \ ··· 352 496 return; \ 353 497 } while (0) 354 498 499 + /** 500 + * closure_return - finish execution of a closure, with destructor 501 + * 502 + * Works like closure_return(), except @destructor will be called when all 503 + * outstanding refs on @cl have been dropped; @destructor may be used to safely 504 + * free the memory occupied by @cl, and it is called with the ref on the parent 505 + * closure still held - so @destructor could safely return an item to a 506 + * freelist protected by @cl's parent. 507 + */ 355 508 #define closure_return_with_destructor(_cl, _destructor) \ 356 509 do { \ 357 510 set_closure_fn(_cl, _destructor, NULL); \ ··· 368 503 return; \ 369 504 } while (0) 370 505 506 + /** 507 + * closure_call - execute @fn out of a new, uninitialized closure 508 + * 509 + * Typically used when running out of one closure, and we want to run @fn 510 + * asynchronously out of a new closure - @parent will then wait for @cl to 511 + * finish. 512 + */ 371 513 static inline void closure_call(struct closure *cl, closure_fn fn, 372 514 struct workqueue_struct *wq, 373 515 struct closure *parent) 374 516 { 375 517 closure_init(cl, parent); 376 518 continue_at_nobarrier(cl, fn, wq); 377 - } 378 - 379 - static inline void closure_trylock_call(struct closure *cl, closure_fn fn, 380 - struct workqueue_struct *wq, 381 - struct closure *parent) 382 - { 383 - if (closure_trylock(cl, parent)) 384 - continue_at_nobarrier(cl, fn, wq); 385 519 } 386 520 387 521 #endif /* _LINUX_CLOSURE_H */