1
2
3
4
5
6
7
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
20static 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
30static uint64_t __calc_target_rate(struct cached_dev *dc)
31{
32 struct cache_set *c = dc->disk.c;
33
34
35
36
37
38 uint64_t cache_sectors = c->nbuckets * c->sb.bucket_size -
39 atomic_long_read(&c->flash_dev_dirty_sectors);
40
41
42
43
44
45
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
55 if (bdev_share < 1)
56 bdev_share = 1;
57
58 return (cache_dirty_target * bdev_share) >> WRITEBACK_SHARE_SHIFT;
59}
60
61static void __update_writeback_rate(struct cached_dev *dc)
62{
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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 if ((error < 0 && dc->writeback_rate_integral > 0) ||
92 (error > 0 && time_before64(local_clock(),
93 dc->writeback_rate.next + NSEC_PER_MSEC))) {
94
95
96
97
98
99
100
101
102
103
104 dc->writeback_rate_integral += error *
105 dc->writeback_rate_update_seconds;
106 }
107
108 integral_scaled = div_s64(dc->writeback_rate_integral,
109 dc->writeback_rate_i_term_inverse);
110
111 new_rate = clamp_t(int32_t, (proportional_scaled + integral_scaled),
112 dc->writeback_rate_minimum, NSEC_PER_SEC);
113
114 dc->writeback_rate_proportional = proportional_scaled;
115 dc->writeback_rate_integral_scaled = integral_scaled;
116 dc->writeback_rate_change = new_rate -
117 atomic_long_read(&dc->writeback_rate.rate);
118 atomic_long_set(&dc->writeback_rate.rate, new_rate);
119 dc->writeback_rate_target = target;
120}
121
122static bool set_at_max_writeback_rate(struct cache_set *c,
123 struct cached_dev *dc)
124{
125
126 if (!c->idle_max_writeback_rate_enabled)
127 return false;
128
129
130 if (!c->gc_mark_valid)
131 return false;
132
133
134
135
136
137
138
139
140
141
142
143
144
145 if (atomic_inc_return(&c->idle_counter) <
146 atomic_read(&c->attached_dev_nr) * 6)
147 return false;
148
149 if (atomic_read(&c->at_max_writeback_rate) != 1)
150 atomic_set(&c->at_max_writeback_rate, 1);
151
152 atomic_long_set(&dc->writeback_rate.rate, INT_MAX);
153
154
155 dc->writeback_rate_proportional = 0;
156 dc->writeback_rate_integral_scaled = 0;
157 dc->writeback_rate_change = 0;
158
159
160
161
162
163
164
165 if ((atomic_read(&c->idle_counter) <
166 atomic_read(&c->attached_dev_nr) * 6) ||
167 !atomic_read(&c->at_max_writeback_rate))
168 return false;
169
170 return true;
171}
172
173static void update_writeback_rate(struct work_struct *work)
174{
175 struct cached_dev *dc = container_of(to_delayed_work(work),
176 struct cached_dev,
177 writeback_rate_update);
178 struct cache_set *c = dc->disk.c;
179
180
181
182
183
184 set_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags);
185
186 smp_mb();
187
188
189
190
191
192 if (!test_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags) ||
193 test_bit(CACHE_SET_IO_DISABLE, &c->flags)) {
194 clear_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags);
195
196 smp_mb();
197 return;
198 }
199
200 if (atomic_read(&dc->has_dirty) && dc->writeback_percent) {
201
202
203
204
205
206
207 if (!set_at_max_writeback_rate(c, dc)) {
208 down_read(&dc->writeback_lock);
209 __update_writeback_rate(dc);
210 update_gc_after_writeback(c);
211 up_read(&dc->writeback_lock);
212 }
213 }
214
215
216
217
218
219
220 if (test_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags) &&
221 !test_bit(CACHE_SET_IO_DISABLE, &c->flags)) {
222 schedule_delayed_work(&dc->writeback_rate_update,
223 dc->writeback_rate_update_seconds * HZ);
224 }
225
226
227
228
229
230 clear_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags);
231
232 smp_mb();
233}
234
235static unsigned int writeback_delay(struct cached_dev *dc,
236 unsigned int sectors)
237{
238 if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
239 !dc->writeback_percent)
240 return 0;
241
242 return bch_next_delay(&dc->writeback_rate, sectors);
243}
244
245struct dirty_io {
246 struct closure cl;
247 struct cached_dev *dc;
248 uint16_t sequence;
249 struct bio bio;
250};
251
252static void dirty_init(struct keybuf_key *w)
253{
254 struct dirty_io *io = w->private;
255 struct bio *bio = &io->bio;
256
257 bio_init(bio, bio->bi_inline_vecs,
258 DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS));
259 if (!io->dc->writeback_percent)
260 bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
261
262 bio->bi_iter.bi_size = KEY_SIZE(&w->key) << 9;
263 bio->bi_private = w;
264 bch_bio_map(bio, NULL);
265}
266
267static void dirty_io_destructor(struct closure *cl)
268{
269 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
270
271 kfree(io);
272}
273
274static void write_dirty_finish(struct closure *cl)
275{
276 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
277 struct keybuf_key *w = io->bio.bi_private;
278 struct cached_dev *dc = io->dc;
279
280 bio_free_pages(&io->bio);
281
282
283 if (KEY_DIRTY(&w->key)) {
284 int ret;
285 unsigned int i;
286 struct keylist keys;
287
288 bch_keylist_init(&keys);
289
290 bkey_copy(keys.top, &w->key);
291 SET_KEY_DIRTY(keys.top, false);
292 bch_keylist_push(&keys);
293
294 for (i = 0; i < KEY_PTRS(&w->key); i++)
295 atomic_inc(&PTR_BUCKET(dc->disk.c, &w->key, i)->pin);
296
297 ret = bch_btree_insert(dc->disk.c, &keys, NULL, &w->key);
298
299 if (ret)
300 trace_bcache_writeback_collision(&w->key);
301
302 atomic_long_inc(ret
303 ? &dc->disk.c->writeback_keys_failed
304 : &dc->disk.c->writeback_keys_done);
305 }
306
307 bch_keybuf_del(&dc->writeback_keys, w);
308 up(&dc->in_flight);
309
310 closure_return_with_destructor(cl, dirty_io_destructor);
311}
312
313static void dirty_endio(struct bio *bio)
314{
315 struct keybuf_key *w = bio->bi_private;
316 struct dirty_io *io = w->private;
317
318 if (bio->bi_status) {
319 SET_KEY_DIRTY(&w->key, false);
320 bch_count_backing_io_errors(io->dc, bio);
321 }
322
323 closure_put(&io->cl);
324}
325
326static void write_dirty(struct closure *cl)
327{
328 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
329 struct keybuf_key *w = io->bio.bi_private;
330 struct cached_dev *dc = io->dc;
331
332 uint16_t next_sequence;
333
334 if (atomic_read(&dc->writeback_sequence_next) != io->sequence) {
335
336 closure_wait(&dc->writeback_ordering_wait, cl);
337
338 if (atomic_read(&dc->writeback_sequence_next) == io->sequence) {
339
340
341
342
343 closure_wake_up(&dc->writeback_ordering_wait);
344 }
345
346 continue_at(cl, write_dirty, io->dc->writeback_write_wq);
347 return;
348 }
349
350 next_sequence = io->sequence + 1;
351
352
353
354
355
356
357
358 if (KEY_DIRTY(&w->key)) {
359 dirty_init(w);
360 bio_set_op_attrs(&io->bio, REQ_OP_WRITE, 0);
361 io->bio.bi_iter.bi_sector = KEY_START(&w->key);
362 bio_set_dev(&io->bio, io->dc->bdev);
363 io->bio.bi_end_io = dirty_endio;
364
365
366 closure_bio_submit(io->dc->disk.c, &io->bio, cl);
367 }
368
369 atomic_set(&dc->writeback_sequence_next, next_sequence);
370 closure_wake_up(&dc->writeback_ordering_wait);
371
372 continue_at(cl, write_dirty_finish, io->dc->writeback_write_wq);
373}
374
375static void read_dirty_endio(struct bio *bio)
376{
377 struct keybuf_key *w = bio->bi_private;
378 struct dirty_io *io = w->private;
379
380
381 bch_count_io_errors(PTR_CACHE(io->dc->disk.c, &w->key, 0),
382 bio->bi_status, 1,
383 "reading dirty data from cache");
384
385 dirty_endio(bio);
386}
387
388static void read_dirty_submit(struct closure *cl)
389{
390 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
391
392 closure_bio_submit(io->dc->disk.c, &io->bio, cl);
393
394 continue_at(cl, write_dirty, io->dc->writeback_write_wq);
395}
396
397static void read_dirty(struct cached_dev *dc)
398{
399 unsigned int delay = 0;
400 struct keybuf_key *next, *keys[MAX_WRITEBACKS_IN_PASS], *w;
401 size_t size;
402 int nk, i;
403 struct dirty_io *io;
404 struct closure cl;
405 uint16_t sequence = 0;
406
407 BUG_ON(!llist_empty(&dc->writeback_ordering_wait.list));
408 atomic_set(&dc->writeback_sequence_next, sequence);
409 closure_init_stack(&cl);
410
411
412
413
414
415
416 next = bch_keybuf_next(&dc->writeback_keys);
417
418 while (!kthread_should_stop() &&
419 !test_bit(CACHE_SET_IO_DISABLE, &dc->disk.c->flags) &&
420 next) {
421 size = 0;
422 nk = 0;
423
424 do {
425 BUG_ON(ptr_stale(dc->disk.c, &next->key, 0));
426
427
428
429
430
431 if (nk >= MAX_WRITEBACKS_IN_PASS)
432 break;
433
434
435
436
437
438 if (size >= MAX_WRITESIZE_IN_PASS)
439 break;
440
441
442
443
444
445
446
447
448
449
450 if ((nk != 0) && bkey_cmp(&keys[nk-1]->key,
451 &START_KEY(&next->key)))
452 break;
453
454 size += KEY_SIZE(&next->key);
455 keys[nk++] = next;
456 } while ((next = bch_keybuf_next(&dc->writeback_keys)));
457
458
459 for (i = 0; i < nk; i++) {
460 w = keys[i];
461
462 io = kzalloc(sizeof(struct dirty_io) +
463 sizeof(struct bio_vec) *
464 DIV_ROUND_UP(KEY_SIZE(&w->key),
465 PAGE_SECTORS),
466 GFP_KERNEL);
467 if (!io)
468 goto err;
469
470 w->private = io;
471 io->dc = dc;
472 io->sequence = sequence++;
473
474 dirty_init(w);
475 bio_set_op_attrs(&io->bio, REQ_OP_READ, 0);
476 io->bio.bi_iter.bi_sector = PTR_OFFSET(&w->key, 0);
477 bio_set_dev(&io->bio,
478 PTR_CACHE(dc->disk.c, &w->key, 0)->bdev);
479 io->bio.bi_end_io = read_dirty_endio;
480
481 if (bch_bio_alloc_pages(&io->bio, GFP_KERNEL))
482 goto err_free;
483
484 trace_bcache_writeback(&w->key);
485
486 down(&dc->in_flight);
487
488
489
490
491
492
493 closure_call(&io->cl, read_dirty_submit, NULL, &cl);
494 }
495
496 delay = writeback_delay(dc, size);
497
498 while (!kthread_should_stop() &&
499 !test_bit(CACHE_SET_IO_DISABLE, &dc->disk.c->flags) &&
500 delay) {
501 schedule_timeout_interruptible(delay);
502 delay = writeback_delay(dc, 0);
503 }
504 }
505
506 if (0) {
507err_free:
508 kfree(w->private);
509err:
510 bch_keybuf_del(&dc->writeback_keys, w);
511 }
512
513
514
515
516
517 closure_sync(&cl);
518}
519
520
521
522void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned int inode,
523 uint64_t offset, int nr_sectors)
524{
525 struct bcache_device *d = c->devices[inode];
526 unsigned int stripe_offset, stripe, sectors_dirty;
527
528 if (!d)
529 return;
530
531 if (UUID_FLASH_ONLY(&c->uuids[inode]))
532 atomic_long_add(nr_sectors, &c->flash_dev_dirty_sectors);
533
534 stripe = offset_to_stripe(d, offset);
535 stripe_offset = offset & (d->stripe_size - 1);
536
537 while (nr_sectors) {
538 int s = min_t(unsigned int, abs(nr_sectors),
539 d->stripe_size - stripe_offset);
540
541 if (nr_sectors < 0)
542 s = -s;
543
544 if (stripe >= d->nr_stripes)
545 return;
546
547 sectors_dirty = atomic_add_return(s,
548 d->stripe_sectors_dirty + stripe);
549 if (sectors_dirty == d->stripe_size)
550 set_bit(stripe, d->full_dirty_stripes);
551 else
552 clear_bit(stripe, d->full_dirty_stripes);
553
554 nr_sectors -= s;
555 stripe_offset = 0;
556 stripe++;
557 }
558}
559
560static bool dirty_pred(struct keybuf *buf, struct bkey *k)
561{
562 struct cached_dev *dc = container_of(buf,
563 struct cached_dev,
564 writeback_keys);
565
566 BUG_ON(KEY_INODE(k) != dc->disk.id);
567
568 return KEY_DIRTY(k);
569}
570
571static void refill_full_stripes(struct cached_dev *dc)
572{
573 struct keybuf *buf = &dc->writeback_keys;
574 unsigned int start_stripe, stripe, next_stripe;
575 bool wrapped = false;
576
577 stripe = offset_to_stripe(&dc->disk, KEY_OFFSET(&buf->last_scanned));
578
579 if (stripe >= dc->disk.nr_stripes)
580 stripe = 0;
581
582 start_stripe = stripe;
583
584 while (1) {
585 stripe = find_next_bit(dc->disk.full_dirty_stripes,
586 dc->disk.nr_stripes, stripe);
587
588 if (stripe == dc->disk.nr_stripes)
589 goto next;
590
591 next_stripe = find_next_zero_bit(dc->disk.full_dirty_stripes,
592 dc->disk.nr_stripes, stripe);
593
594 buf->last_scanned = KEY(dc->disk.id,
595 stripe * dc->disk.stripe_size, 0);
596
597 bch_refill_keybuf(dc->disk.c, buf,
598 &KEY(dc->disk.id,
599 next_stripe * dc->disk.stripe_size, 0),
600 dirty_pred);
601
602 if (array_freelist_empty(&buf->freelist))
603 return;
604
605 stripe = next_stripe;
606next:
607 if (wrapped && stripe > start_stripe)
608 return;
609
610 if (stripe == dc->disk.nr_stripes) {
611 stripe = 0;
612 wrapped = true;
613 }
614 }
615}
616
617
618
619
620static bool refill_dirty(struct cached_dev *dc)
621{
622 struct keybuf *buf = &dc->writeback_keys;
623 struct bkey start = KEY(dc->disk.id, 0, 0);
624 struct bkey end = KEY(dc->disk.id, MAX_KEY_OFFSET, 0);
625 struct bkey start_pos;
626
627
628
629
630
631
632 if (bkey_cmp(&buf->last_scanned, &start) < 0 ||
633 bkey_cmp(&buf->last_scanned, &end) > 0)
634 buf->last_scanned = start;
635
636 if (dc->partial_stripes_expensive) {
637 refill_full_stripes(dc);
638 if (array_freelist_empty(&buf->freelist))
639 return false;
640 }
641
642 start_pos = buf->last_scanned;
643 bch_refill_keybuf(dc->disk.c, buf, &end, dirty_pred);
644
645 if (bkey_cmp(&buf->last_scanned, &end) < 0)
646 return false;
647
648
649
650
651
652 buf->last_scanned = start;
653 bch_refill_keybuf(dc->disk.c, buf, &start_pos, dirty_pred);
654
655 return bkey_cmp(&buf->last_scanned, &start_pos) >= 0;
656}
657
658static int bch_writeback_thread(void *arg)
659{
660 struct cached_dev *dc = arg;
661 struct cache_set *c = dc->disk.c;
662 bool searched_full_index;
663
664 bch_ratelimit_reset(&dc->writeback_rate);
665
666 while (!kthread_should_stop() &&
667 !test_bit(CACHE_SET_IO_DISABLE, &c->flags)) {
668 down_write(&dc->writeback_lock);
669 set_current_state(TASK_INTERRUPTIBLE);
670
671
672
673
674
675
676
677 if (!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) &&
678 (!atomic_read(&dc->has_dirty) || !dc->writeback_running)) {
679 up_write(&dc->writeback_lock);
680
681 if (kthread_should_stop() ||
682 test_bit(CACHE_SET_IO_DISABLE, &c->flags)) {
683 set_current_state(TASK_RUNNING);
684 break;
685 }
686
687 schedule();
688 continue;
689 }
690 set_current_state(TASK_RUNNING);
691
692 searched_full_index = refill_dirty(dc);
693
694 if (searched_full_index &&
695 RB_EMPTY_ROOT(&dc->writeback_keys.keys)) {
696 atomic_set(&dc->has_dirty, 0);
697 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
698 bch_write_bdev_super(dc, NULL);
699
700
701
702
703
704
705 if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) {
706 up_write(&dc->writeback_lock);
707 break;
708 }
709
710
711
712
713
714
715
716
717
718
719
720
721 if (c->gc_after_writeback ==
722 (BCH_ENABLE_AUTO_GC|BCH_DO_AUTO_GC)) {
723 c->gc_after_writeback &= ~BCH_DO_AUTO_GC;
724 force_wake_up_gc(c);
725 }
726 }
727
728 up_write(&dc->writeback_lock);
729
730 read_dirty(dc);
731
732 if (searched_full_index) {
733 unsigned int delay = dc->writeback_delay * HZ;
734
735 while (delay &&
736 !kthread_should_stop() &&
737 !test_bit(CACHE_SET_IO_DISABLE, &c->flags) &&
738 !test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
739 delay = schedule_timeout_interruptible(delay);
740
741 bch_ratelimit_reset(&dc->writeback_rate);
742 }
743 }
744
745 if (dc->writeback_write_wq) {
746 flush_workqueue(dc->writeback_write_wq);
747 destroy_workqueue(dc->writeback_write_wq);
748 }
749 cached_dev_put(dc);
750 wait_for_kthread_stop();
751
752 return 0;
753}
754
755
756#define INIT_KEYS_EACH_TIME 500000
757#define INIT_KEYS_SLEEP_MS 100
758
759struct sectors_dirty_init {
760 struct btree_op op;
761 unsigned int inode;
762 size_t count;
763 struct bkey start;
764};
765
766static int sectors_dirty_init_fn(struct btree_op *_op, struct btree *b,
767 struct bkey *k)
768{
769 struct sectors_dirty_init *op = container_of(_op,
770 struct sectors_dirty_init, op);
771 if (KEY_INODE(k) > op->inode)
772 return MAP_DONE;
773
774 if (KEY_DIRTY(k))
775 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
776 KEY_START(k), KEY_SIZE(k));
777
778 op->count++;
779 if (atomic_read(&b->c->search_inflight) &&
780 !(op->count % INIT_KEYS_EACH_TIME)) {
781 bkey_copy_key(&op->start, k);
782 return -EAGAIN;
783 }
784
785 return MAP_CONTINUE;
786}
787
788void bch_sectors_dirty_init(struct bcache_device *d)
789{
790 struct sectors_dirty_init op;
791 int ret;
792
793 bch_btree_op_init(&op.op, -1);
794 op.inode = d->id;
795 op.count = 0;
796 op.start = KEY(op.inode, 0, 0);
797
798 do {
799 ret = bch_btree_map_keys(&op.op, d->c, &op.start,
800 sectors_dirty_init_fn, 0);
801 if (ret == -EAGAIN)
802 schedule_timeout_interruptible(
803 msecs_to_jiffies(INIT_KEYS_SLEEP_MS));
804 else if (ret < 0) {
805 pr_warn("sectors dirty init failed, ret=%d!", ret);
806 break;
807 }
808 } while (ret == -EAGAIN);
809}
810
811void bch_cached_dev_writeback_init(struct cached_dev *dc)
812{
813 sema_init(&dc->in_flight, 64);
814 init_rwsem(&dc->writeback_lock);
815 bch_keybuf_init(&dc->writeback_keys);
816
817 dc->writeback_metadata = true;
818 dc->writeback_running = false;
819 dc->writeback_percent = 10;
820 dc->writeback_delay = 30;
821 atomic_long_set(&dc->writeback_rate.rate, 1024);
822 dc->writeback_rate_minimum = 8;
823
824 dc->writeback_rate_update_seconds = WRITEBACK_RATE_UPDATE_SECS_DEFAULT;
825 dc->writeback_rate_p_term_inverse = 40;
826 dc->writeback_rate_i_term_inverse = 10000;
827
828 WARN_ON(test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags));
829 INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate);
830}
831
832int bch_cached_dev_writeback_start(struct cached_dev *dc)
833{
834 dc->writeback_write_wq = alloc_workqueue("bcache_writeback_wq",
835 WQ_MEM_RECLAIM, 0);
836 if (!dc->writeback_write_wq)
837 return -ENOMEM;
838
839 cached_dev_get(dc);
840 dc->writeback_thread = kthread_create(bch_writeback_thread, dc,
841 "bcache_writeback");
842 if (IS_ERR(dc->writeback_thread)) {
843 cached_dev_put(dc);
844 destroy_workqueue(dc->writeback_write_wq);
845 return PTR_ERR(dc->writeback_thread);
846 }
847 dc->writeback_running = true;
848
849 WARN_ON(test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags));
850 schedule_delayed_work(&dc->writeback_rate_update,
851 dc->writeback_rate_update_seconds * HZ);
852
853 bch_writeback_queue(dc);
854
855 return 0;
856}
857