1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * background writeback - scan btree for dirty data and write it to the backing 4 * device 5 * 6 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> 7 * Copyright 2012 Google, Inc. 8 */ 9 10 #include "bcache.h" 11 #include "btree.h" 12 #include "debug.h" 13 #include "writeback.h" 14 15 #include <linux/delay.h> 16 #include <linux/kthread.h> 17 #include <linux/sched/clock.h> 18 #include <trace/events/bcache.h> 19 20 static void update_gc_after_writeback(struct cache_set *c) 21 { 22 if (c->gc_after_writeback != (BCH_ENABLE_AUTO_GC) || 23 c->gc_stats.in_use < BCH_AUTO_GC_DIRTY_THRESHOLD) 24 return; 25 26 c->gc_after_writeback |= BCH_DO_AUTO_GC; 27 } 28 29 /* Rate limiting */ 30 static uint64_t __calc_target_rate(struct cached_dev *dc) 31 { 32 struct cache_set *c = dc->disk.c; 33 34 /* 35 * This is the size of the cache, minus the amount used for 36 * flash-only devices 37 */ 38 uint64_t cache_sectors = c->nbuckets * c->cache->sb.bucket_size - 39 atomic_long_read(&c->flash_dev_dirty_sectors); 40 41 /* 42 * Unfortunately there is no control of global dirty data. If the 43 * user states that they want 10% dirty data in the cache, and has, 44 * e.g., 5 backing volumes of equal size, we try and ensure each 45 * backing volume uses about 2% of the cache for dirty data. 46 */ 47 uint32_t bdev_share = 48 div64_u64(bdev_sectors(dc->bdev) << WRITEBACK_SHARE_SHIFT, 49 c->cached_dev_sectors); 50 51 uint64_t cache_dirty_target = 52 div_u64(cache_sectors * dc->writeback_percent, 100); 53 54 /* Ensure each backing dev gets at least one dirty share */ 55 if (bdev_share < 1) 56 bdev_share = 1; 57 58 return (cache_dirty_target * bdev_share) >> WRITEBACK_SHARE_SHIFT; 59 } 60 61 static void __update_writeback_rate(struct cached_dev *dc) 62 { 63 /* 64 * PI controller: 65 * Figures out the amount that should be written per second. 66 * 67 * First, the error (number of sectors that are dirty beyond our 68 * target) is calculated. The error is accumulated (numerically 69 * integrated). 70 * 71 * Then, the proportional value and integral value are scaled 72 * based on configured values. These are stored as inverses to 73 * avoid fixed point math and to make configuration easy-- e.g. 74 * the default value of 40 for writeback_rate_p_term_inverse 75 * attempts to write at a rate that would retire all the dirty 76 * blocks in 40 seconds. 77 * 78 * The writeback_rate_i_inverse value of 10000 means that 1/10000th 79 * of the error is accumulated in the integral term per second. 80 * This acts as a slow, long-term average that is not subject to 81 * variations in usage like the p term. 82 */ 83 int64_t target = __calc_target_rate(dc); 84 int64_t dirty = bcache_dev_sectors_dirty(&dc->disk); 85 int64_t error = dirty - target; 86 int64_t proportional_scaled = 87 div_s64(error, dc->writeback_rate_p_term_inverse); 88 int64_t integral_scaled; 89 uint32_t new_rate; 90 91 /* 92 * We need to consider the number of dirty buckets as well 93 * when calculating the proportional_scaled, Otherwise we might 94 * have an unreasonable small writeback rate at a highly fragmented situation 95 * when very few dirty sectors consumed a lot dirty buckets, the 96 * worst case is when dirty buckets reached cutoff_writeback_sync and 97 * dirty data is still not even reached to writeback percent, so the rate 98 * still will be at the minimum value, which will cause the write 99 * stuck at a non-writeback mode. 100 */ 101 struct cache_set *c = dc->disk.c; 102 103 int64_t dirty_buckets = c->nbuckets - c->avail_nbuckets; 104 105 if (dc->writeback_consider_fragment && 106 c->gc_stats.in_use > BCH_WRITEBACK_FRAGMENT_THRESHOLD_LOW && dirty > 0) { 107 int64_t fragment = 108 div_s64((dirty_buckets * c->cache->sb.bucket_size), dirty); 109 int64_t fp_term; 110 int64_t fps; 111 112 if (c->gc_stats.in_use <= BCH_WRITEBACK_FRAGMENT_THRESHOLD_MID) { 113 fp_term = dc->writeback_rate_fp_term_low * 114 (c->gc_stats.in_use - BCH_WRITEBACK_FRAGMENT_THRESHOLD_LOW); 115 } else if (c->gc_stats.in_use <= BCH_WRITEBACK_FRAGMENT_THRESHOLD_HIGH) { 116 fp_term = dc->writeback_rate_fp_term_mid * 117 (c->gc_stats.in_use - BCH_WRITEBACK_FRAGMENT_THRESHOLD_MID); 118 } else { 119 fp_term = dc->writeback_rate_fp_term_high * 120 (c->gc_stats.in_use - BCH_WRITEBACK_FRAGMENT_THRESHOLD_HIGH); 121 } 122 fps = div_s64(dirty, dirty_buckets) * fp_term; 123 if (fragment > 3 && fps > proportional_scaled) { 124 /* Only overrite the p when fragment > 3 */ 125 proportional_scaled = fps; 126 } 127 } 128 129 if ((error < 0 && dc->writeback_rate_integral > 0) || 130 (error > 0 && time_before64(local_clock(), 131 dc->writeback_rate.next + NSEC_PER_MSEC))) { 132 /* 133 * Only decrease the integral term if it's more than 134 * zero. Only increase the integral term if the device 135 * is keeping up. (Don't wind up the integral 136 * ineffectively in either case). 137 * 138 * It's necessary to scale this by 139 * writeback_rate_update_seconds to keep the integral 140 * term dimensioned properly. 141 */ 142 dc->writeback_rate_integral += error * 143 dc->writeback_rate_update_seconds; 144 } 145 146 integral_scaled = div_s64(dc->writeback_rate_integral, 147 dc->writeback_rate_i_term_inverse); 148 149 new_rate = clamp_t(int32_t, (proportional_scaled + integral_scaled), 150 dc->writeback_rate_minimum, NSEC_PER_SEC); 151 152 dc->writeback_rate_proportional = proportional_scaled; 153 dc->writeback_rate_integral_scaled = integral_scaled; 154 dc->writeback_rate_change = new_rate - 155 atomic_long_read(&dc->writeback_rate.rate); 156 atomic_long_set(&dc->writeback_rate.rate, new_rate); 157 dc->writeback_rate_target = target; 158 } 159 160 static bool set_at_max_writeback_rate(struct cache_set *c, 161 struct cached_dev *dc) 162 { 163 /* Don't sst max writeback rate if it is disabled */ 164 if (!c->idle_max_writeback_rate_enabled) 165 return false; 166 167 /* Don't set max writeback rate if gc is running */ 168 if (!c->gc_mark_valid) 169 return false; 170 /* 171 * Idle_counter is increased everytime when update_writeback_rate() is 172 * called. If all backing devices attached to the same cache set have 173 * identical dc->writeback_rate_update_seconds values, it is about 6 174 * rounds of update_writeback_rate() on each backing device before 175 * c->at_max_writeback_rate is set to 1, and then max wrteback rate set 176 * to each dc->writeback_rate.rate. 177 * In order to avoid extra locking cost for counting exact dirty cached 178 * devices number, c->attached_dev_nr is used to calculate the idle 179 * throushold. It might be bigger if not all cached device are in write- 180 * back mode, but it still works well with limited extra rounds of 181 * update_writeback_rate(). 182 */ 183 if (atomic_inc_return(&c->idle_counter) < 184 atomic_read(&c->attached_dev_nr) * 6) 185 return false; 186 187 if (atomic_read(&c->at_max_writeback_rate) != 1) 188 atomic_set(&c->at_max_writeback_rate, 1); 189 190 atomic_long_set(&dc->writeback_rate.rate, INT_MAX); 191 192 /* keep writeback_rate_target as existing value */ 193 dc->writeback_rate_proportional = 0; 194 dc->writeback_rate_integral_scaled = 0; 195 dc->writeback_rate_change = 0; 196 197 /* 198 * Check c->idle_counter and c->at_max_writeback_rate agagain in case 199 * new I/O arrives during before set_at_max_writeback_rate() returns. 200 * Then the writeback rate is set to 1, and its new value should be 201 * decided via __update_writeback_rate(). 202 */ 203 if ((atomic_read(&c->idle_counter) < 204 atomic_read(&c->attached_dev_nr) * 6) || 205 !atomic_read(&c->at_max_writeback_rate)) 206 return false; 207 208 return true; 209 } 210 211 static void update_writeback_rate(struct work_struct *work) 212 { 213 struct cached_dev *dc = container_of(to_delayed_work(work), 214 struct cached_dev, 215 writeback_rate_update); 216 struct cache_set *c = dc->disk.c; 217 218 /* 219 * should check BCACHE_DEV_RATE_DW_RUNNING before calling 220 * cancel_delayed_work_sync(). 221 */ 222 set_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags); 223 /* paired with where BCACHE_DEV_RATE_DW_RUNNING is tested */ 224 smp_mb__after_atomic(); 225 226 /* 227 * CACHE_SET_IO_DISABLE might be set via sysfs interface, 228 * check it here too. 229 */ 230 if (!test_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags) || 231 test_bit(CACHE_SET_IO_DISABLE, &c->flags)) { 232 clear_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags); 233 /* paired with where BCACHE_DEV_RATE_DW_RUNNING is tested */ 234 smp_mb__after_atomic(); 235 return; 236 } 237 238 if (atomic_read(&dc->has_dirty) && dc->writeback_percent) { 239 /* 240 * If the whole cache set is idle, set_at_max_writeback_rate() 241 * will set writeback rate to a max number. Then it is 242 * unncessary to update writeback rate for an idle cache set 243 * in maximum writeback rate number(s). 244 */ 245 if (!set_at_max_writeback_rate(c, dc)) { 246 down_read(&dc->writeback_lock); 247 __update_writeback_rate(dc); 248 update_gc_after_writeback(c); 249 up_read(&dc->writeback_lock); 250 } 251 } 252 253 254 /* 255 * CACHE_SET_IO_DISABLE might be set via sysfs interface, 256 * check it here too. 257 */ 258 if (test_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags) && 259 !test_bit(CACHE_SET_IO_DISABLE, &c->flags)) { 260 schedule_delayed_work(&dc->writeback_rate_update, 261 dc->writeback_rate_update_seconds * HZ); 262 } 263 264 /* 265 * should check BCACHE_DEV_RATE_DW_RUNNING before calling 266 * cancel_delayed_work_sync(). 267 */ 268 clear_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags); 269 /* paired with where BCACHE_DEV_RATE_DW_RUNNING is tested */ 270 smp_mb__after_atomic(); 271 } 272 273 static unsigned int writeback_delay(struct cached_dev *dc, 274 unsigned int sectors) 275 { 276 if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) || 277 !dc->writeback_percent) 278 return 0; 279 280 return bch_next_delay(&dc->writeback_rate, sectors); 281 } 282 283 struct dirty_io { 284 struct closure cl; 285 struct cached_dev *dc; 286 uint16_t sequence; 287 struct bio bio; 288 }; 289 290 static void dirty_init(struct keybuf_key *w) 291 { 292 struct dirty_io *io = w->private; 293 struct bio *bio = &io->bio; 294 295 bio_init(bio, bio->bi_inline_vecs, 296 DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS)); 297 if (!io->dc->writeback_percent) 298 bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); 299 300 bio->bi_iter.bi_size = KEY_SIZE(&w->key) << 9; 301 bio->bi_private = w; 302 bch_bio_map(bio, NULL); 303 } 304 305 static void dirty_io_destructor(struct closure *cl) 306 { 307 struct dirty_io *io = container_of(cl, struct dirty_io, cl); 308 309 kfree(io); 310 } 311 312 static void write_dirty_finish(struct closure *cl) 313 { 314 struct dirty_io *io = container_of(cl, struct dirty_io, cl); 315 struct keybuf_key *w = io->bio.bi_private; 316 struct cached_dev *dc = io->dc; 317 318 bio_free_pages(&io->bio); 319 320 /* This is kind of a dumb way of signalling errors. */ 321 if (KEY_DIRTY(&w->key)) { 322 int ret; 323 unsigned int i; 324 struct keylist keys; 325 326 bch_keylist_init(&keys); 327 328 bkey_copy(keys.top, &w->key); 329 SET_KEY_DIRTY(keys.top, false); 330 bch_keylist_push(&keys); 331 332 for (i = 0; i < KEY_PTRS(&w->key); i++) 333 atomic_inc(&PTR_BUCKET(dc->disk.c, &w->key, i)->pin); 334 335 ret = bch_btree_insert(dc->disk.c, &keys, NULL, &w->key); 336 337 if (ret) 338 trace_bcache_writeback_collision(&w->key); 339 340 atomic_long_inc(ret 341 ? &dc->disk.c->writeback_keys_failed 342 : &dc->disk.c->writeback_keys_done); 343 } 344 345 bch_keybuf_del(&dc->writeback_keys, w); 346 up(&dc->in_flight); 347 348 closure_return_with_destructor(cl, dirty_io_destructor); 349 } 350 351 static void dirty_endio(struct bio *bio) 352 { 353 struct keybuf_key *w = bio->bi_private; 354 struct dirty_io *io = w->private; 355 356 if (bio->bi_status) { 357 SET_KEY_DIRTY(&w->key, false); 358 bch_count_backing_io_errors(io->dc, bio); 359 } 360 361 closure_put(&io->cl); 362 } 363 364 static void write_dirty(struct closure *cl) 365 { 366 struct dirty_io *io = container_of(cl, struct dirty_io, cl); 367 struct keybuf_key *w = io->bio.bi_private; 368 struct cached_dev *dc = io->dc; 369 370 uint16_t next_sequence; 371 372 if (atomic_read(&dc->writeback_sequence_next) != io->sequence) { 373 /* Not our turn to write; wait for a write to complete */ 374 closure_wait(&dc->writeback_ordering_wait, cl); 375 376 if (atomic_read(&dc->writeback_sequence_next) == io->sequence) { 377 /* 378 * Edge case-- it happened in indeterminate order 379 * relative to when we were added to wait list.. 380 */ 381 closure_wake_up(&dc->writeback_ordering_wait); 382 } 383 384 continue_at(cl, write_dirty, io->dc->writeback_write_wq); 385 return; 386 } 387 388 next_sequence = io->sequence + 1; 389 390 /* 391 * IO errors are signalled using the dirty bit on the key. 392 * If we failed to read, we should not attempt to write to the 393 * backing device. Instead, immediately go to write_dirty_finish 394 * to clean up. 395 */ 396 if (KEY_DIRTY(&w->key)) { 397 dirty_init(w); 398 bio_set_op_attrs(&io->bio, REQ_OP_WRITE, 0); 399 io->bio.bi_iter.bi_sector = KEY_START(&w->key); 400 bio_set_dev(&io->bio, io->dc->bdev); 401 io->bio.bi_end_io = dirty_endio; 402 403 /* I/O request sent to backing device */ 404 closure_bio_submit(io->dc->disk.c, &io->bio, cl); 405 } 406 407 atomic_set(&dc->writeback_sequence_next, next_sequence); 408 closure_wake_up(&dc->writeback_ordering_wait); 409 410 continue_at(cl, write_dirty_finish, io->dc->writeback_write_wq); 411 } 412 413 static void read_dirty_endio(struct bio *bio) 414 { 415 struct keybuf_key *w = bio->bi_private; 416 struct dirty_io *io = w->private; 417 418 /* is_read = 1 */ 419 bch_count_io_errors(PTR_CACHE(io->dc->disk.c, &w->key, 0), 420 bio->bi_status, 1, 421 "reading dirty data from cache"); 422 423 dirty_endio(bio); 424 } 425 426 static void read_dirty_submit(struct closure *cl) 427 { 428 struct dirty_io *io = container_of(cl, struct dirty_io, cl); 429 430 closure_bio_submit(io->dc->disk.c, &io->bio, cl); 431 432 continue_at(cl, write_dirty, io->dc->writeback_write_wq); 433 } 434 435 static void read_dirty(struct cached_dev *dc) 436 { 437 unsigned int delay = 0; 438 struct keybuf_key *next, *keys[MAX_WRITEBACKS_IN_PASS], *w; 439 size_t size; 440 int nk, i; 441 struct dirty_io *io; 442 struct closure cl; 443 uint16_t sequence = 0; 444 445 BUG_ON(!llist_empty(&dc->writeback_ordering_wait.list)); 446 atomic_set(&dc->writeback_sequence_next, sequence); 447 closure_init_stack(&cl); 448 449 /* 450 * XXX: if we error, background writeback just spins. Should use some 451 * mempools. 452 */ 453 454 next = bch_keybuf_next(&dc->writeback_keys); 455 456 while (!kthread_should_stop() && 457 !test_bit(CACHE_SET_IO_DISABLE, &dc->disk.c->flags) && 458 next) { 459 size = 0; 460 nk = 0; 461 462 do { 463 BUG_ON(ptr_stale(dc->disk.c, &next->key, 0)); 464 465 /* 466 * Don't combine too many operations, even if they 467 * are all small. 468 */ 469 if (nk >= MAX_WRITEBACKS_IN_PASS) 470 break; 471 472 /* 473 * If the current operation is very large, don't 474 * further combine operations. 475 */ 476 if (size >= MAX_WRITESIZE_IN_PASS) 477 break; 478 479 /* 480 * Operations are only eligible to be combined 481 * if they are contiguous. 482 * 483 * TODO: add a heuristic willing to fire a 484 * certain amount of non-contiguous IO per pass, 485 * so that we can benefit from backing device 486 * command queueing. 487 */ 488 if ((nk != 0) && bkey_cmp(&keys[nk-1]->key, 489 &START_KEY(&next->key))) 490 break; 491 492 size += KEY_SIZE(&next->key); 493 keys[nk++] = next; 494 } while ((next = bch_keybuf_next(&dc->writeback_keys))); 495 496 /* Now we have gathered a set of 1..5 keys to write back. */ 497 for (i = 0; i < nk; i++) { 498 w = keys[i]; 499 500 io = kzalloc(struct_size(io, bio.bi_inline_vecs, 501 DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS)), 502 GFP_KERNEL); 503 if (!io) 504 goto err; 505 506 w->private = io; 507 io->dc = dc; 508 io->sequence = sequence++; 509 510 dirty_init(w); 511 bio_set_op_attrs(&io->bio, REQ_OP_READ, 0); 512 io->bio.bi_iter.bi_sector = PTR_OFFSET(&w->key, 0); 513 bio_set_dev(&io->bio, 514 PTR_CACHE(dc->disk.c, &w->key, 0)->bdev); 515 io->bio.bi_end_io = read_dirty_endio; 516 517 if (bch_bio_alloc_pages(&io->bio, GFP_KERNEL)) 518 goto err_free; 519 520 trace_bcache_writeback(&w->key); 521 522 down(&dc->in_flight); 523 524 /* 525 * We've acquired a semaphore for the maximum 526 * simultaneous number of writebacks; from here 527 * everything happens asynchronously. 528 */ 529 closure_call(&io->cl, read_dirty_submit, NULL, &cl); 530 } 531 532 delay = writeback_delay(dc, size); 533 534 while (!kthread_should_stop() && 535 !test_bit(CACHE_SET_IO_DISABLE, &dc->disk.c->flags) && 536 delay) { 537 schedule_timeout_interruptible(delay); 538 delay = writeback_delay(dc, 0); 539 } 540 } 541 542 if (0) { 543 err_free: 544 kfree(w->private); 545 err: 546 bch_keybuf_del(&dc->writeback_keys, w); 547 } 548 549 /* 550 * Wait for outstanding writeback IOs to finish (and keybuf slots to be 551 * freed) before refilling again 552 */ 553 closure_sync(&cl); 554 } 555 556 /* Scan for dirty data */ 557 558 void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned int inode, 559 uint64_t offset, int nr_sectors) 560 { 561 struct bcache_device *d = c->devices[inode]; 562 unsigned int stripe_offset, sectors_dirty; 563 int stripe; 564 565 if (!d) 566 return; 567 568 stripe = offset_to_stripe(d, offset); 569 if (stripe < 0) 570 return; 571 572 if (UUID_FLASH_ONLY(&c->uuids[inode])) 573 atomic_long_add(nr_sectors, &c->flash_dev_dirty_sectors); 574 575 stripe_offset = offset & (d->stripe_size - 1); 576 577 while (nr_sectors) { 578 int s = min_t(unsigned int, abs(nr_sectors), 579 d->stripe_size - stripe_offset); 580 581 if (nr_sectors < 0) 582 s = -s; 583 584 if (stripe >= d->nr_stripes) 585 return; 586 587 sectors_dirty = atomic_add_return(s, 588 d->stripe_sectors_dirty + stripe); 589 if (sectors_dirty == d->stripe_size) 590 set_bit(stripe, d->full_dirty_stripes); 591 else 592 clear_bit(stripe, d->full_dirty_stripes); 593 594 nr_sectors -= s; 595 stripe_offset = 0; 596 stripe++; 597 } 598 } 599 600 static bool dirty_pred(struct keybuf *buf, struct bkey *k) 601 { 602 struct cached_dev *dc = container_of(buf, 603 struct cached_dev, 604 writeback_keys); 605 606 BUG_ON(KEY_INODE(k) != dc->disk.id); 607 608 return KEY_DIRTY(k); 609 } 610 611 static void refill_full_stripes(struct cached_dev *dc) 612 { 613 struct keybuf *buf = &dc->writeback_keys; 614 unsigned int start_stripe, next_stripe; 615 int stripe; 616 bool wrapped = false; 617 618 stripe = offset_to_stripe(&dc->disk, KEY_OFFSET(&buf->last_scanned)); 619 if (stripe < 0) 620 stripe = 0; 621 622 start_stripe = stripe; 623 624 while (1) { 625 stripe = find_next_bit(dc->disk.full_dirty_stripes, 626 dc->disk.nr_stripes, stripe); 627 628 if (stripe == dc->disk.nr_stripes) 629 goto next; 630 631 next_stripe = find_next_zero_bit(dc->disk.full_dirty_stripes, 632 dc->disk.nr_stripes, stripe); 633 634 buf->last_scanned = KEY(dc->disk.id, 635 stripe * dc->disk.stripe_size, 0); 636 637 bch_refill_keybuf(dc->disk.c, buf, 638 &KEY(dc->disk.id, 639 next_stripe * dc->disk.stripe_size, 0), 640 dirty_pred); 641 642 if (array_freelist_empty(&buf->freelist)) 643 return; 644 645 stripe = next_stripe; 646 next: 647 if (wrapped && stripe > start_stripe) 648 return; 649 650 if (stripe == dc->disk.nr_stripes) { 651 stripe = 0; 652 wrapped = true; 653 } 654 } 655 } 656 657 /* 658 * Returns true if we scanned the entire disk 659 */ 660 static bool refill_dirty(struct cached_dev *dc) 661 { 662 struct keybuf *buf = &dc->writeback_keys; 663 struct bkey start = KEY(dc->disk.id, 0, 0); 664 struct bkey end = KEY(dc->disk.id, MAX_KEY_OFFSET, 0); 665 struct bkey start_pos; 666 667 /* 668 * make sure keybuf pos is inside the range for this disk - at bringup 669 * we might not be attached yet so this disk's inode nr isn't 670 * initialized then 671 */ 672 if (bkey_cmp(&buf->last_scanned, &start) < 0 || 673 bkey_cmp(&buf->last_scanned, &end) > 0) 674 buf->last_scanned = start; 675 676 if (dc->partial_stripes_expensive) { 677 refill_full_stripes(dc); 678 if (array_freelist_empty(&buf->freelist)) 679 return false; 680 } 681 682 start_pos = buf->last_scanned; 683 bch_refill_keybuf(dc->disk.c, buf, &end, dirty_pred); 684 685 if (bkey_cmp(&buf->last_scanned, &end) < 0) 686 return false; 687 688 /* 689 * If we get to the end start scanning again from the beginning, and 690 * only scan up to where we initially started scanning from: 691 */ 692 buf->last_scanned = start; 693 bch_refill_keybuf(dc->disk.c, buf, &start_pos, dirty_pred); 694 695 return bkey_cmp(&buf->last_scanned, &start_pos) >= 0; 696 } 697 698 static int bch_writeback_thread(void *arg) 699 { 700 struct cached_dev *dc = arg; 701 struct cache_set *c = dc->disk.c; 702 bool searched_full_index; 703 704 bch_ratelimit_reset(&dc->writeback_rate); 705 706 while (!kthread_should_stop() && 707 !test_bit(CACHE_SET_IO_DISABLE, &c->flags)) { 708 down_write(&dc->writeback_lock); 709 set_current_state(TASK_INTERRUPTIBLE); 710 /* 711 * If the bache device is detaching, skip here and continue 712 * to perform writeback. Otherwise, if no dirty data on cache, 713 * or there is dirty data on cache but writeback is disabled, 714 * the writeback thread should sleep here and wait for others 715 * to wake up it. 716 */ 717 if (!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) && 718 (!atomic_read(&dc->has_dirty) || !dc->writeback_running)) { 719 up_write(&dc->writeback_lock); 720 721 if (kthread_should_stop() || 722 test_bit(CACHE_SET_IO_DISABLE, &c->flags)) { 723 set_current_state(TASK_RUNNING); 724 break; 725 } 726 727 schedule(); 728 continue; 729 } 730 set_current_state(TASK_RUNNING); 731 732 searched_full_index = refill_dirty(dc); 733 734 if (searched_full_index && 735 RB_EMPTY_ROOT(&dc->writeback_keys.keys)) { 736 atomic_set(&dc->has_dirty, 0); 737 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN); 738 bch_write_bdev_super(dc, NULL); 739 /* 740 * If bcache device is detaching via sysfs interface, 741 * writeback thread should stop after there is no dirty 742 * data on cache. BCACHE_DEV_DETACHING flag is set in 743 * bch_cached_dev_detach(). 744 */ 745 if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) { 746 struct closure cl; 747 748 closure_init_stack(&cl); 749 memset(&dc->sb.set_uuid, 0, 16); 750 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE); 751 752 bch_write_bdev_super(dc, &cl); 753 closure_sync(&cl); 754 755 up_write(&dc->writeback_lock); 756 break; 757 } 758 759 /* 760 * When dirty data rate is high (e.g. 50%+), there might 761 * be heavy buckets fragmentation after writeback 762 * finished, which hurts following write performance. 763 * If users really care about write performance they 764 * may set BCH_ENABLE_AUTO_GC via sysfs, then when 765 * BCH_DO_AUTO_GC is set, garbage collection thread 766 * will be wake up here. After moving gc, the shrunk 767 * btree and discarded free buckets SSD space may be 768 * helpful for following write requests. 769 */ 770 if (c->gc_after_writeback == 771 (BCH_ENABLE_AUTO_GC|BCH_DO_AUTO_GC)) { 772 c->gc_after_writeback &= ~BCH_DO_AUTO_GC; 773 force_wake_up_gc(c); 774 } 775 } 776 777 up_write(&dc->writeback_lock); 778 779 read_dirty(dc); 780 781 if (searched_full_index) { 782 unsigned int delay = dc->writeback_delay * HZ; 783 784 while (delay && 785 !kthread_should_stop() && 786 !test_bit(CACHE_SET_IO_DISABLE, &c->flags) && 787 !test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) 788 delay = schedule_timeout_interruptible(delay); 789 790 bch_ratelimit_reset(&dc->writeback_rate); 791 } 792 } 793 794 if (dc->writeback_write_wq) { 795 flush_workqueue(dc->writeback_write_wq); 796 destroy_workqueue(dc->writeback_write_wq); 797 } 798 cached_dev_put(dc); 799 wait_for_kthread_stop(); 800 801 return 0; 802 } 803 804 /* Init */ 805 #define INIT_KEYS_EACH_TIME 500000 806 #define INIT_KEYS_SLEEP_MS 100 807 808 struct sectors_dirty_init { 809 struct btree_op op; 810 unsigned int inode; 811 size_t count; 812 struct bkey start; 813 }; 814 815 static int sectors_dirty_init_fn(struct btree_op *_op, struct btree *b, 816 struct bkey *k) 817 { 818 struct sectors_dirty_init *op = container_of(_op, 819 struct sectors_dirty_init, op); 820 if (KEY_INODE(k) > op->inode) 821 return MAP_DONE; 822 823 if (KEY_DIRTY(k)) 824 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k), 825 KEY_START(k), KEY_SIZE(k)); 826 827 op->count++; 828 if (atomic_read(&b->c->search_inflight) && 829 !(op->count % INIT_KEYS_EACH_TIME)) { 830 bkey_copy_key(&op->start, k); 831 return -EAGAIN; 832 } 833 834 return MAP_CONTINUE; 835 } 836 837 static int bch_root_node_dirty_init(struct cache_set *c, 838 struct bcache_device *d, 839 struct bkey *k) 840 { 841 struct sectors_dirty_init op; 842 int ret; 843 844 bch_btree_op_init(&op.op, -1); 845 op.inode = d->id; 846 op.count = 0; 847 op.start = KEY(op.inode, 0, 0); 848 849 do { 850 ret = bcache_btree(map_keys_recurse, 851 k, 852 c->root, 853 &op.op, 854 &op.start, 855 sectors_dirty_init_fn, 856 0); 857 if (ret == -EAGAIN) 858 schedule_timeout_interruptible( 859 msecs_to_jiffies(INIT_KEYS_SLEEP_MS)); 860 else if (ret < 0) { 861 pr_warn("sectors dirty init failed, ret=%d!\n", ret); 862 break; 863 } 864 } while (ret == -EAGAIN); 865 866 return ret; 867 } 868 869 static int bch_dirty_init_thread(void *arg) 870 { 871 struct dirty_init_thrd_info *info = arg; 872 struct bch_dirty_init_state *state = info->state; 873 struct cache_set *c = state->c; 874 struct btree_iter iter; 875 struct bkey *k, *p; 876 int cur_idx, prev_idx, skip_nr; 877 878 k = p = NULL; 879 cur_idx = prev_idx = 0; 880 881 bch_btree_iter_init(&c->root->keys, &iter, NULL); 882 k = bch_btree_iter_next_filter(&iter, &c->root->keys, bch_ptr_bad); 883 BUG_ON(!k); 884 885 p = k; 886 887 while (k) { 888 spin_lock(&state->idx_lock); 889 cur_idx = state->key_idx; 890 state->key_idx++; 891 spin_unlock(&state->idx_lock); 892 893 skip_nr = cur_idx - prev_idx; 894 895 while (skip_nr) { 896 k = bch_btree_iter_next_filter(&iter, 897 &c->root->keys, 898 bch_ptr_bad); 899 if (k) 900 p = k; 901 else { 902 atomic_set(&state->enough, 1); 903 /* Update state->enough earlier */ 904 smp_mb__after_atomic(); 905 goto out; 906 } 907 skip_nr--; 908 cond_resched(); 909 } 910 911 if (p) { 912 if (bch_root_node_dirty_init(c, state->d, p) < 0) 913 goto out; 914 } 915 916 p = NULL; 917 prev_idx = cur_idx; 918 cond_resched(); 919 } 920 921 out: 922 /* In order to wake up state->wait in time */ 923 smp_mb__before_atomic(); 924 if (atomic_dec_and_test(&state->started)) 925 wake_up(&state->wait); 926 927 return 0; 928 } 929 930 static int bch_btre_dirty_init_thread_nr(void) 931 { 932 int n = num_online_cpus()/2; 933 934 if (n == 0) 935 n = 1; 936 else if (n > BCH_DIRTY_INIT_THRD_MAX) 937 n = BCH_DIRTY_INIT_THRD_MAX; 938 939 return n; 940 } 941 942 void bch_sectors_dirty_init(struct bcache_device *d) 943 { 944 int i; 945 struct bkey *k = NULL; 946 struct btree_iter iter; 947 struct sectors_dirty_init op; 948 struct cache_set *c = d->c; 949 struct bch_dirty_init_state *state; 950 char name[32]; 951 952 /* Just count root keys if no leaf node */ 953 if (c->root->level == 0) { 954 bch_btree_op_init(&op.op, -1); 955 op.inode = d->id; 956 op.count = 0; 957 op.start = KEY(op.inode, 0, 0); 958 959 for_each_key_filter(&c->root->keys, 960 k, &iter, bch_ptr_invalid) 961 sectors_dirty_init_fn(&op.op, c->root, k); 962 return; 963 } 964 965 state = kzalloc(sizeof(struct bch_dirty_init_state), GFP_KERNEL); 966 if (!state) { 967 pr_warn("sectors dirty init failed: cannot allocate memory\n"); 968 return; 969 } 970 971 state->c = c; 972 state->d = d; 973 state->total_threads = bch_btre_dirty_init_thread_nr(); 974 state->key_idx = 0; 975 spin_lock_init(&state->idx_lock); 976 atomic_set(&state->started, 0); 977 atomic_set(&state->enough, 0); 978 init_waitqueue_head(&state->wait); 979 980 for (i = 0; i < state->total_threads; i++) { 981 /* Fetch latest state->enough earlier */ 982 smp_mb__before_atomic(); 983 if (atomic_read(&state->enough)) 984 break; 985 986 state->infos[i].state = state; 987 atomic_inc(&state->started); 988 snprintf(name, sizeof(name), "bch_dirty_init[%d]", i); 989 990 state->infos[i].thread = 991 kthread_run(bch_dirty_init_thread, 992 &state->infos[i], 993 name); 994 if (IS_ERR(state->infos[i].thread)) { 995 pr_err("fails to run thread bch_dirty_init[%d]\n", i); 996 for (--i; i >= 0; i--) 997 kthread_stop(state->infos[i].thread); 998 goto out; 999 } 1000 } 1001 1002 wait_event_interruptible(state->wait, 1003 atomic_read(&state->started) == 0 || 1004 test_bit(CACHE_SET_IO_DISABLE, &c->flags)); 1005 1006 out: 1007 kfree(state); 1008 } 1009 1010 void bch_cached_dev_writeback_init(struct cached_dev *dc) 1011 { 1012 sema_init(&dc->in_flight, 64); 1013 init_rwsem(&dc->writeback_lock); 1014 bch_keybuf_init(&dc->writeback_keys); 1015 1016 dc->writeback_metadata = true; 1017 dc->writeback_running = false; 1018 dc->writeback_consider_fragment = true; 1019 dc->writeback_percent = 10; 1020 dc->writeback_delay = 30; 1021 atomic_long_set(&dc->writeback_rate.rate, 1024); 1022 dc->writeback_rate_minimum = 8; 1023 1024 dc->writeback_rate_update_seconds = WRITEBACK_RATE_UPDATE_SECS_DEFAULT; 1025 dc->writeback_rate_p_term_inverse = 40; 1026 dc->writeback_rate_fp_term_low = 1; 1027 dc->writeback_rate_fp_term_mid = 10; 1028 dc->writeback_rate_fp_term_high = 1000; 1029 dc->writeback_rate_i_term_inverse = 10000; 1030 1031 WARN_ON(test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)); 1032 INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate); 1033 } 1034 1035 int bch_cached_dev_writeback_start(struct cached_dev *dc) 1036 { 1037 dc->writeback_write_wq = alloc_workqueue("bcache_writeback_wq", 1038 WQ_MEM_RECLAIM, 0); 1039 if (!dc->writeback_write_wq) 1040 return -ENOMEM; 1041 1042 cached_dev_get(dc); 1043 dc->writeback_thread = kthread_create(bch_writeback_thread, dc, 1044 "bcache_writeback"); 1045 if (IS_ERR(dc->writeback_thread)) { 1046 cached_dev_put(dc); 1047 destroy_workqueue(dc->writeback_write_wq); 1048 return PTR_ERR(dc->writeback_thread); 1049 } 1050 dc->writeback_running = true; 1051 1052 WARN_ON(test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)); 1053 schedule_delayed_work(&dc->writeback_rate_update, 1054 dc->writeback_rate_update_seconds * HZ); 1055 1056 bch_writeback_queue(dc); 1057 1058 return 0; 1059 } 1060