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

bcache: kill closure locking usage

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

+98 -55
+6 -3
drivers/md/bcache/bcache.h
··· 309 309 struct cache_sb sb; 310 310 struct bio sb_bio; 311 311 struct bio_vec sb_bv[1]; 312 - struct closure_with_waitlist sb_write; 312 + struct closure sb_write; 313 + struct semaphore sb_write_mutex; 313 314 314 315 /* Refcount on the cache set. Always nonzero when we're caching. */ 315 316 atomic_t count; ··· 515 514 uint64_t cached_dev_sectors; 516 515 struct closure caching; 517 516 518 - struct closure_with_waitlist sb_write; 517 + struct closure sb_write; 518 + struct semaphore sb_write_mutex; 519 519 520 520 mempool_t *search; 521 521 mempool_t *bio_meta; ··· 637 635 unsigned nr_uuids; 638 636 struct uuid_entry *uuids; 639 637 BKEY_PADDED(uuid_bucket); 640 - struct closure_with_waitlist uuid_write; 638 + struct closure uuid_write; 639 + struct semaphore uuid_write_mutex; 641 640 642 641 /* 643 642 * A btree node on disk could have too many bsets for an iterator to fit
+34 -18
drivers/md/bcache/btree.c
··· 340 340 w->journal = NULL; 341 341 } 342 342 343 + static void btree_node_write_unlock(struct closure *cl) 344 + { 345 + struct btree *b = container_of(cl, struct btree, io); 346 + 347 + up(&b->io_mutex); 348 + } 349 + 343 350 static void __btree_node_write_done(struct closure *cl) 344 351 { 345 - struct btree *b = container_of(cl, struct btree, io.cl); 352 + struct btree *b = container_of(cl, struct btree, io); 346 353 struct btree_write *w = btree_prev_write(b); 347 354 348 355 bch_bbio_free(b->bio, b->c); ··· 360 353 queue_delayed_work(btree_io_wq, &b->work, 361 354 msecs_to_jiffies(30000)); 362 355 363 - closure_return(cl); 356 + closure_return_with_destructor(cl, btree_node_write_unlock); 364 357 } 365 358 366 359 static void btree_node_write_done(struct closure *cl) 367 360 { 368 - struct btree *b = container_of(cl, struct btree, io.cl); 361 + struct btree *b = container_of(cl, struct btree, io); 369 362 struct bio_vec *bv; 370 363 int n; 371 364 ··· 378 371 static void btree_node_write_endio(struct bio *bio, int error) 379 372 { 380 373 struct closure *cl = bio->bi_private; 381 - struct btree *b = container_of(cl, struct btree, io.cl); 374 + struct btree *b = container_of(cl, struct btree, io); 382 375 383 376 if (error) 384 377 set_btree_node_io_error(b); ··· 389 382 390 383 static void do_btree_node_write(struct btree *b) 391 384 { 392 - struct closure *cl = &b->io.cl; 385 + struct closure *cl = &b->io; 393 386 struct bset *i = b->sets[b->nsets].data; 394 387 BKEY_PADDED(key) k; 395 388 ··· 442 435 bch_submit_bbio(b->bio, b->c, &k.key, 0); 443 436 444 437 closure_sync(cl); 445 - __btree_node_write_done(cl); 438 + continue_at_nobarrier(cl, __btree_node_write_done, NULL); 446 439 } 447 440 } 448 441 ··· 461 454 cancel_delayed_work(&b->work); 462 455 463 456 /* If caller isn't waiting for write, parent refcount is cache set */ 464 - closure_lock(&b->io, parent ?: &b->c->cl); 457 + down(&b->io_mutex); 458 + closure_init(&b->io, parent ?: &b->c->cl); 465 459 466 460 clear_bit(BTREE_NODE_dirty, &b->flags); 467 461 change_bit(BTREE_NODE_write_idx, &b->flags); ··· 562 554 static void mca_data_free(struct btree *b) 563 555 { 564 556 struct bset_tree *t = b->sets; 565 - BUG_ON(!closure_is_unlocked(&b->io.cl)); 557 + 558 + BUG_ON(b->io_mutex.count != 1); 566 559 567 560 if (bset_prev_bytes(b) < PAGE_SIZE) 568 561 kfree(t->prev); ··· 644 635 INIT_LIST_HEAD(&b->list); 645 636 INIT_DELAYED_WORK(&b->work, btree_node_write_work); 646 637 b->c = c; 647 - closure_init_unlocked(&b->io); 638 + sema_init(&b->io_mutex, 1); 648 639 649 640 mca_data_alloc(b, k, gfp); 650 641 return b; ··· 662 653 663 654 BUG_ON(btree_node_dirty(b) && !b->sets[0].data); 664 655 665 - if (b->page_order < min_order || 666 - (!flush && 667 - (btree_node_dirty(b) || 668 - atomic_read(&b->io.cl.remaining) != -1))) { 669 - rw_unlock(true, b); 670 - return -ENOMEM; 656 + if (b->page_order < min_order) 657 + goto out_unlock; 658 + 659 + if (!flush) { 660 + if (btree_node_dirty(b)) 661 + goto out_unlock; 662 + 663 + if (down_trylock(&b->io_mutex)) 664 + goto out_unlock; 665 + up(&b->io_mutex); 671 666 } 672 667 673 668 if (btree_node_dirty(b)) 674 669 bch_btree_node_write_sync(b); 675 670 676 671 /* wait for any in flight btree write */ 677 - closure_wait_event(&b->io.wait, &cl, 678 - atomic_read(&b->io.cl.remaining) == -1); 672 + down(&b->io_mutex); 673 + up(&b->io_mutex); 679 674 680 675 return 0; 676 + out_unlock: 677 + rw_unlock(true, b); 678 + return -ENOMEM; 681 679 } 682 680 683 681 static unsigned long bch_mca_scan(struct shrinker *shrink, ··· 934 918 if (!b->sets->data) 935 919 goto err; 936 920 out: 937 - BUG_ON(!closure_is_unlocked(&b->io.cl)); 921 + BUG_ON(b->io_mutex.count != 1); 938 922 939 923 bkey_copy(&b->key, k); 940 924 list_move(&b->list, &c->btree_cache);
+2 -1
drivers/md/bcache/btree.h
··· 143 143 struct bset_tree sets[MAX_BSETS]; 144 144 145 145 /* For outstanding btree writes, used as a lock - protects write_idx */ 146 - struct closure_with_waitlist io; 146 + struct closure io; 147 + struct semaphore io_mutex; 147 148 148 149 struct list_head list; 149 150 struct delayed_work work;
+2 -5
drivers/md/bcache/debug.c
··· 127 127 if (!b->c->verify) 128 128 return; 129 129 130 - closure_wait_event(&b->io.wait, &cl, 131 - atomic_read(&b->io.cl.remaining) == -1); 132 - 130 + down(&b->io_mutex); 133 131 mutex_lock(&b->c->verify_lock); 134 132 135 133 bkey_copy(&v->key, &b->key); ··· 135 137 v->level = b->level; 136 138 137 139 bch_btree_node_read(v); 138 - closure_wait_event(&v->io.wait, &cl, 139 - atomic_read(&b->io.cl.remaining) == -1); 140 140 141 141 if (new->keys != v->sets[0].data->keys || 142 142 memcmp(new->start, ··· 163 167 } 164 168 165 169 mutex_unlock(&b->c->verify_lock); 170 + up(&b->io_mutex); 166 171 } 167 172 168 173 void bch_data_verify(struct cached_dev *dc, struct bio *bio)
+14 -13
drivers/md/bcache/journal.c
··· 564 564 continue_at_nobarrier(cl, journal_write, system_wq); 565 565 } 566 566 567 + static void journal_write_unlock(struct closure *cl) 568 + { 569 + struct cache_set *c = container_of(cl, struct cache_set, journal.io); 570 + 571 + c->journal.io_in_flight = 0; 572 + spin_unlock(&c->journal.lock); 573 + } 574 + 567 575 static void journal_write_unlocked(struct closure *cl) 568 576 __releases(c->journal.lock) 569 577 { ··· 586 578 bio_list_init(&list); 587 579 588 580 if (!w->need_write) { 589 - /* 590 - * XXX: have to unlock closure before we unlock journal lock, 591 - * else we race with bch_journal(). But this way we race 592 - * against cache set unregister. Doh. 593 - */ 594 - set_closure_fn(cl, NULL, NULL); 595 - closure_sub(cl, CLOSURE_RUNNING + 1); 596 - spin_unlock(&c->journal.lock); 597 - return; 581 + closure_return_with_destructor(cl, journal_write_unlock); 598 582 } else if (journal_full(&c->journal)) { 599 583 journal_reclaim(c); 600 584 spin_unlock(&c->journal.lock); ··· 662 662 663 663 w->need_write = true; 664 664 665 - if (closure_trylock(cl, &c->cl)) 666 - journal_write_unlocked(cl); 667 - else 665 + if (!c->journal.io_in_flight) { 666 + c->journal.io_in_flight = 1; 667 + closure_call(cl, journal_write_unlocked, NULL, &c->cl); 668 + } else { 668 669 spin_unlock(&c->journal.lock); 670 + } 669 671 } 670 672 671 673 static struct journal_write *journal_wait_for_write(struct cache_set *c, ··· 795 793 { 796 794 struct journal *j = &c->journal; 797 795 798 - closure_init_unlocked(&j->io); 799 796 spin_lock_init(&j->lock); 800 797 INIT_DELAYED_WORK(&j->work, journal_write_work); 801 798
+1
drivers/md/bcache/journal.h
··· 104 104 /* used when waiting because the journal was full */ 105 105 struct closure_waitlist wait; 106 106 struct closure io; 107 + int io_in_flight; 107 108 struct delayed_work work; 108 109 109 110 /* Number of blocks free in the bucket(s) we're currently writing to */
+39 -15
drivers/md/bcache/super.c
··· 225 225 struct cached_dev *dc = bio->bi_private; 226 226 /* XXX: error checking */ 227 227 228 - closure_put(&dc->sb_write.cl); 228 + closure_put(&dc->sb_write); 229 229 } 230 230 231 231 static void __write_super(struct cache_sb *sb, struct bio *bio) ··· 263 263 submit_bio(REQ_WRITE, bio); 264 264 } 265 265 266 + static void bch_write_bdev_super_unlock(struct closure *cl) 267 + { 268 + struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write); 269 + 270 + up(&dc->sb_write_mutex); 271 + } 272 + 266 273 void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent) 267 274 { 268 - struct closure *cl = &dc->sb_write.cl; 275 + struct closure *cl = &dc->sb_write; 269 276 struct bio *bio = &dc->sb_bio; 270 277 271 - closure_lock(&dc->sb_write, parent); 278 + down(&dc->sb_write_mutex); 279 + closure_init(cl, parent); 272 280 273 281 bio_reset(bio); 274 282 bio->bi_bdev = dc->bdev; ··· 286 278 closure_get(cl); 287 279 __write_super(&dc->sb, bio); 288 280 289 - closure_return(cl); 281 + closure_return_with_destructor(cl, bch_write_bdev_super_unlock); 290 282 } 291 283 292 284 static void write_super_endio(struct bio *bio, int error) ··· 294 286 struct cache *ca = bio->bi_private; 295 287 296 288 bch_count_io_errors(ca, error, "writing superblock"); 297 - closure_put(&ca->set->sb_write.cl); 289 + closure_put(&ca->set->sb_write); 290 + } 291 + 292 + static void bcache_write_super_unlock(struct closure *cl) 293 + { 294 + struct cache_set *c = container_of(cl, struct cache_set, sb_write); 295 + 296 + up(&c->sb_write_mutex); 298 297 } 299 298 300 299 void bcache_write_super(struct cache_set *c) 301 300 { 302 - struct closure *cl = &c->sb_write.cl; 301 + struct closure *cl = &c->sb_write; 303 302 struct cache *ca; 304 303 unsigned i; 305 304 306 - closure_lock(&c->sb_write, &c->cl); 305 + down(&c->sb_write_mutex); 306 + closure_init(cl, &c->cl); 307 307 308 308 c->sb.seq++; 309 309 ··· 333 317 __write_super(&ca->sb, bio); 334 318 } 335 319 336 - closure_return(cl); 320 + closure_return_with_destructor(cl, bcache_write_super_unlock); 337 321 } 338 322 339 323 /* UUID io */ ··· 341 325 static void uuid_endio(struct bio *bio, int error) 342 326 { 343 327 struct closure *cl = bio->bi_private; 344 - struct cache_set *c = container_of(cl, struct cache_set, uuid_write.cl); 328 + struct cache_set *c = container_of(cl, struct cache_set, uuid_write); 345 329 346 330 cache_set_err_on(error, c, "accessing uuids"); 347 331 bch_bbio_free(bio, c); 348 332 closure_put(cl); 349 333 } 350 334 335 + static void uuid_io_unlock(struct closure *cl) 336 + { 337 + struct cache_set *c = container_of(cl, struct cache_set, uuid_write); 338 + 339 + up(&c->uuid_write_mutex); 340 + } 341 + 351 342 static void uuid_io(struct cache_set *c, unsigned long rw, 352 343 struct bkey *k, struct closure *parent) 353 344 { 354 - struct closure *cl = &c->uuid_write.cl; 345 + struct closure *cl = &c->uuid_write; 355 346 struct uuid_entry *u; 356 347 unsigned i; 357 348 char buf[80]; 358 349 359 350 BUG_ON(!parent); 360 - closure_lock(&c->uuid_write, parent); 351 + down(&c->uuid_write_mutex); 352 + closure_init(cl, parent); 361 353 362 354 for (i = 0; i < KEY_PTRS(k); i++) { 363 355 struct bio *bio = bch_bbio_alloc(c); ··· 392 368 u - c->uuids, u->uuid, u->label, 393 369 u->first_reg, u->last_reg, u->invalidated); 394 370 395 - closure_return(cl); 371 + closure_return_with_destructor(cl, uuid_io_unlock); 396 372 } 397 373 398 374 static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl) ··· 1122 1098 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq); 1123 1099 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype); 1124 1100 INIT_WORK(&dc->detach, cached_dev_detach_finish); 1125 - closure_init_unlocked(&dc->sb_write); 1101 + sema_init(&dc->sb_write_mutex, 1); 1126 1102 INIT_LIST_HEAD(&dc->io_lru); 1127 1103 spin_lock_init(&dc->io_lock); 1128 1104 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl); ··· 1478 1454 1479 1455 c->sort_crit_factor = int_sqrt(c->btree_pages); 1480 1456 1481 - closure_init_unlocked(&c->sb_write); 1457 + sema_init(&c->sb_write_mutex, 1); 1482 1458 mutex_init(&c->bucket_lock); 1483 1459 init_waitqueue_head(&c->try_wait); 1484 1460 init_waitqueue_head(&c->bucket_wait); 1485 - closure_init_unlocked(&c->uuid_write); 1461 + sema_init(&c->uuid_write_mutex, 1); 1486 1462 mutex_init(&c->sort_lock); 1487 1463 1488 1464 spin_lock_init(&c->sort_time.lock);