1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
/*
* background writeback - scan btree for dirty data and write it to the backing
* device
*
* Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
* Copyright 2012 Google, Inc.
*/
#include "bcache.h"
#include "btree_update.h"
#include "clock.h"
#include "debug.h"
#include "error.h"
#include "extents.h"
#include "io.h"
#include "keybuf.h"
#include "keylist.h"
#include "writeback.h"
#include <linux/delay.h>
#include <linux/freezer.h>
#include <linux/kthread.h>
#include <trace/events/bcache.h>
/* Rate limiting */
static void __update_writeback_rate(struct cached_dev *dc)
{
struct bch_fs *c = dc->disk.c;
u64 cache_dirty_target =
div_u64(c->capacity * dc->writeback_percent, 100);
s64 target = div64_u64(cache_dirty_target *
bdev_sectors(dc->disk_sb.bdev),
c->cached_dev_sectors);
s64 dirty = bcache_dev_sectors_dirty(&dc->disk);
bch_pd_controller_update(&dc->writeback_pd, target << 9,
dirty << 9, -1);
}
static void update_writeback_rate(struct work_struct *work)
{
struct cached_dev *dc = container_of(to_delayed_work(work),
struct cached_dev,
writeback_pd_update);
down_read(&dc->writeback_lock);
if (atomic_read(&dc->has_dirty) &&
dc->writeback_percent &&
!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
__update_writeback_rate(dc);
else
dc->writeback_pd.rate.rate = UINT_MAX;
up_read(&dc->writeback_lock);
schedule_delayed_work(&dc->writeback_pd_update,
dc->writeback_pd_update_seconds * HZ);
}
struct dirty_io {
struct closure cl;
struct bch_replace_info replace;
struct cached_dev *dc;
struct bch_dev *ca;
struct keybuf_key *w;
struct bch_extent_ptr ptr;
int error;
bool from_mempool;
/* Must be last */
struct bio bio;
};
#define DIRTY_IO_MEMPOOL_BVECS 64
#define DIRTY_IO_MEMPOOL_SECTORS (DIRTY_IO_MEMPOOL_BVECS * PAGE_SECTORS)
static void dirty_init(struct dirty_io *io)
{
struct bio *bio = &io->bio;
bio_init(bio);
if (!io->dc->writeback_percent)
bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
bio->bi_iter.bi_size = io->replace.key.k.size << 9;
bio->bi_max_vecs =
DIV_ROUND_UP(io->replace.key.k.size, PAGE_SECTORS);
bio->bi_io_vec = bio->bi_inline_vecs;
bch_bio_map(bio, NULL);
}
static void dirty_io_destructor(struct closure *cl)
{
struct dirty_io *io = container_of(cl, struct dirty_io, cl);
if (io->from_mempool)
mempool_free(io, &io->dc->writeback_io_pool);
else
kfree(io);
}
static void write_dirty_finish(struct closure *cl)
{
struct dirty_io *io = container_of(cl, struct dirty_io, cl);
struct cached_dev *dc = io->dc;
struct bio_vec *bv;
int i;
bio_for_each_segment_all(bv, &io->bio, i)
mempool_free(bv->bv_page, &dc->writeback_page_pool);
if (!io->error) {
BKEY_PADDED(k) tmp;
int ret;
bkey_copy(&tmp.k, &io->replace.key);
io->replace.hook.fn = bch_extent_cmpxchg;
bkey_extent_set_cached(&tmp.k.k, true);
ret = bch_btree_insert(dc->disk.c, BTREE_ID_EXTENTS, &tmp.k,
NULL, &io->replace.hook, NULL, 0);
if (io->replace.successes == 0)
trace_bcache_writeback_collision(&io->replace.key.k);
atomic_long_inc(ret
? &dc->disk.c->writeback_keys_failed
: &dc->disk.c->writeback_keys_done);
}
bch_keybuf_put(&dc->writeback_keys, io->w);
closure_return_with_destructor(cl, dirty_io_destructor);
}
static void dirty_endio(struct bio *bio)
{
struct dirty_io *io = container_of(bio, struct dirty_io, bio);
if (bio->bi_error) {
trace_bcache_writeback_error(&io->replace.key.k,
op_is_write(bio_op(&io->bio)),
bio->bi_error);
io->error = bio->bi_error;
}
closure_put(&io->cl);
}
static void write_dirty(struct closure *cl)
{
struct dirty_io *io = container_of(cl, struct dirty_io, cl);
if (!io->error) {
dirty_init(io);
bio_set_op_attrs(&io->bio, REQ_OP_WRITE, 0);
io->bio.bi_iter.bi_sector =
bkey_start_offset(&io->replace.key.k);
io->bio.bi_bdev = io->dc->disk_sb.bdev;
io->bio.bi_end_io = dirty_endio;
closure_bio_submit(&io->bio, cl);
}
continue_at(cl, write_dirty_finish, io->dc->disk.c->wq);
}
static void read_dirty_endio(struct bio *bio)
{
struct dirty_io *io = container_of(bio, struct dirty_io, bio);
bch_dev_nonfatal_io_err_on(bio->bi_error, io->ca, "writeback read");
bch_account_io_completion(io->ca);
if (ptr_stale(io->ca, &io->ptr))
bio->bi_error = -EINTR;
dirty_endio(bio);
}
static void read_dirty_submit(struct closure *cl)
{
struct dirty_io *io = container_of(cl, struct dirty_io, cl);
closure_bio_submit(&io->bio, cl);
continue_at(cl, write_dirty, system_freezable_wq);
}
static u64 read_dirty(struct cached_dev *dc)
{
struct keybuf_key *w;
struct dirty_io *io;
struct closure cl;
unsigned i;
struct bio_vec *bv;
u64 sectors_written = 0;
BKEY_PADDED(k) tmp;
closure_init_stack(&cl);
while (!bch_ratelimit_wait_freezable_stoppable(&dc->writeback_pd.rate)) {
w = bch_keybuf_next(&dc->writeback_keys);
if (!w)
break;
sectors_written += w->key.k.size;
bkey_copy(&tmp.k, &w->key);
while (tmp.k.k.size) {
struct extent_pick_ptr pick;
bch_extent_pick_ptr(dc->disk.c,
bkey_i_to_s_c(&tmp.k),
&pick);
if (IS_ERR_OR_NULL(pick.ca))
break;
io = kzalloc(sizeof(*io) + sizeof(struct bio_vec) *
DIV_ROUND_UP(tmp.k.k.size,
PAGE_SECTORS),
GFP_KERNEL);
if (!io) {
trace_bcache_writeback_alloc_fail(pick.ca->fs,
tmp.k.k.size);
io = mempool_alloc(&dc->writeback_io_pool,
GFP_KERNEL);
memset(io, 0, sizeof(*io) +
sizeof(struct bio_vec) *
DIRTY_IO_MEMPOOL_BVECS);
io->from_mempool = true;
bkey_copy(&io->replace.key, &tmp.k);
if (DIRTY_IO_MEMPOOL_SECTORS <
io->replace.key.k.size)
bch_key_resize(&io->replace.key.k,
DIRTY_IO_MEMPOOL_SECTORS);
} else {
bkey_copy(&io->replace.key, &tmp.k);
}
io->dc = dc;
io->ca = pick.ca;
io->w = w;
io->ptr = pick.ptr;
atomic_inc(&w->ref);
dirty_init(io);
bio_set_op_attrs(&io->bio, REQ_OP_READ, 0);
io->bio.bi_iter.bi_sector = pick.ptr.offset;
io->bio.bi_bdev = pick.ca->disk_sb.bdev;
io->bio.bi_end_io = read_dirty_endio;
bio_for_each_segment_all(bv, &io->bio, i) {
bv->bv_page =
mempool_alloc(&dc->writeback_page_pool,
i ? GFP_NOWAIT
: GFP_KERNEL);
if (!bv->bv_page) {
BUG_ON(!i);
io->bio.bi_vcnt = i;
io->bio.bi_iter.bi_size =
io->bio.bi_vcnt * PAGE_SIZE;
bch_key_resize(&io->replace.key.k,
bio_sectors(&io->bio));
break;
}
}
bch_cut_front(io->replace.key.k.p, &tmp.k);
trace_bcache_writeback(&io->replace.key.k);
bch_ratelimit_increment(&dc->writeback_pd.rate,
io->replace.key.k.size << 9);
closure_call(&io->cl, read_dirty_submit, NULL, &cl);
}
bch_keybuf_put(&dc->writeback_keys, w);
}
/*
* Wait for outstanding writeback IOs to finish (and keybuf slots to be
* freed) before refilling again
*/
closure_sync(&cl);
return sectors_written;
}
/* Scan for dirty data */
static void __bcache_dev_sectors_dirty_add(struct bcache_device *d,
u64 offset, int nr_sectors)
{
unsigned stripe_offset, stripe, sectors_dirty;
if (!d)
return;
if (!d->stripe_sectors_dirty)
return;
stripe = offset_to_stripe(d, offset);
stripe_offset = offset & (d->stripe_size - 1);
while (nr_sectors) {
int s = min_t(unsigned, abs(nr_sectors),
d->stripe_size - stripe_offset);
if (nr_sectors < 0)
s = -s;
if (stripe >= d->nr_stripes)
return;
sectors_dirty = atomic_add_return(s,
d->stripe_sectors_dirty + stripe);
if (sectors_dirty == d->stripe_size)
set_bit(stripe, d->full_dirty_stripes);
else
clear_bit(stripe, d->full_dirty_stripes);
nr_sectors -= s;
stripe_offset = 0;
stripe++;
}
}
void bcache_dev_sectors_dirty_add(struct bch_fs *c, unsigned inode,
u64 offset, int nr_sectors)
{
struct bcache_device *d;
rcu_read_lock();
d = bch_dev_find(c, inode);
if (d)
__bcache_dev_sectors_dirty_add(d, offset, nr_sectors);
rcu_read_unlock();
}
static bool dirty_pred(struct keybuf *buf, struct bkey_s_c k)
{
struct cached_dev *dc = container_of(buf, struct cached_dev, writeback_keys);
BUG_ON(k.k->p.inode != bcache_dev_inum(&dc->disk));
return bkey_extent_is_data(k.k) &&
!bkey_extent_is_cached(k.k);
}
static void refill_full_stripes(struct cached_dev *dc)
{
struct keybuf *buf = &dc->writeback_keys;
unsigned inode = bcache_dev_inum(&dc->disk);
unsigned start_stripe, stripe, next_stripe;
bool wrapped = false;
stripe = offset_to_stripe(&dc->disk, buf->last_scanned.offset);
if (stripe >= dc->disk.nr_stripes)
stripe = 0;
start_stripe = stripe;
while (1) {
stripe = find_next_bit(dc->disk.full_dirty_stripes,
dc->disk.nr_stripes, stripe);
if (stripe == dc->disk.nr_stripes)
goto next;
next_stripe = find_next_zero_bit(dc->disk.full_dirty_stripes,
dc->disk.nr_stripes, stripe);
buf->last_scanned = POS(inode,
stripe * dc->disk.stripe_size);
bch_refill_keybuf(dc->disk.c, buf,
POS(inode,
next_stripe * dc->disk.stripe_size),
dirty_pred);
if (array_freelist_empty(&buf->freelist))
return;
stripe = next_stripe;
next:
if (wrapped && stripe > start_stripe)
return;
if (stripe == dc->disk.nr_stripes) {
stripe = 0;
wrapped = true;
}
}
}
static u64 bch_writeback(struct cached_dev *dc)
{
struct keybuf *buf = &dc->writeback_keys;
unsigned inode = bcache_dev_inum(&dc->disk);
struct bpos start = POS(inode, 0);
struct bpos end = POS(inode, KEY_OFFSET_MAX);
struct bpos start_pos;
u64 sectors_written = 0;
buf->last_scanned = POS(inode, 0);
while (bkey_cmp(buf->last_scanned, end) < 0 &&
!kthread_should_stop()) {
down_write(&dc->writeback_lock);
if (!atomic_read(&dc->has_dirty)) {
up_write(&dc->writeback_lock);
set_current_state(TASK_INTERRUPTIBLE);
if (kthread_should_stop())
return sectors_written;
schedule();
try_to_freeze();
return sectors_written;
}
if (bkey_cmp(buf->last_scanned, end) >= 0)
buf->last_scanned = POS(inode, 0);
if (dc->partial_stripes_expensive) {
refill_full_stripes(dc);
if (array_freelist_empty(&buf->freelist))
goto refill_done;
}
start_pos = buf->last_scanned;
bch_refill_keybuf(dc->disk.c, buf, end, dirty_pred);
if (bkey_cmp(buf->last_scanned, end) >= 0) {
/*
* If we get to the end start scanning again from the
* beginning, and only scan up to where we initially
* started scanning from:
*/
buf->last_scanned = start;
bch_refill_keybuf(dc->disk.c, buf, start_pos,
dirty_pred);
}
if (RB_EMPTY_ROOT(&dc->writeback_keys.keys)) {
atomic_set(&dc->has_dirty, 0);
cached_dev_put(dc);
SET_BDEV_STATE(dc->disk_sb.sb, BDEV_STATE_CLEAN);
bch_write_bdev_super(dc, NULL);
}
refill_done:
up_write(&dc->writeback_lock);
bch_ratelimit_reset(&dc->writeback_pd.rate);
sectors_written += read_dirty(dc);
}
return sectors_written;
}
static int bch_writeback_thread(void *arg)
{
struct cached_dev *dc = arg;
struct bch_fs *c = dc->disk.c;
struct io_clock *clock = &c->io_clock[WRITE];
unsigned long last;
u64 sectors_written;
set_freezable();
while (!kthread_should_stop()) {
if (kthread_wait_freezable(dc->writeback_running ||
test_bit(BCACHE_DEV_DETACHING,
&dc->disk.flags)))
break;
last = atomic_long_read(&clock->now);
sectors_written = bch_writeback(dc);
if (sectors_written < c->capacity >> 4)
bch_kthread_io_clock_wait(clock,
last + (c->capacity >> 5));
}
return 0;
}
/**
* bch_keylist_recalc_oldest_gens - update oldest_gen pointers from writeback keys
*
* This prevents us from wrapping around gens for a bucket only referenced from
* writeback keybufs. We don't actually care that the data in those buckets is
* marked live, only that we don't wrap the gens.
*/
void bch_writeback_recalc_oldest_gens(struct bch_fs *c)
{
struct radix_tree_iter iter;
void **slot;
rcu_read_lock();
radix_tree_for_each_slot(slot, &c->devices, &iter, 0) {
struct bcache_device *d;
struct cached_dev *dc;
d = radix_tree_deref_slot(slot);
if (!CACHED_DEV(&d->inode.v))
continue;
dc = container_of(d, struct cached_dev, disk);
bch_keybuf_recalc_oldest_gens(c, &dc->writeback_keys);
}
rcu_read_unlock();
}
/* Init */
void bch_sectors_dirty_init(struct cached_dev *dc, struct bch_fs *c)
{
struct bcache_device *d = &dc->disk;
struct btree_iter iter;
struct bkey_s_c k;
/*
* We have to do this before the disk is added to the radix tree or we
* race with moving GC
*/
for_each_btree_key(&iter, c, BTREE_ID_EXTENTS,
POS(bcache_dev_inum(d), 0), k) {
if (k.k->p.inode > bcache_dev_inum(d))
break;
if (bkey_extent_is_data(k.k) &&
!bkey_extent_is_cached(k.k))
__bcache_dev_sectors_dirty_add(d,
bkey_start_offset(k.k),
k.k->size);
bch_btree_iter_cond_resched(&iter);
}
bch_btree_iter_unlock(&iter);
dc->writeback_pd.last_actual = bcache_dev_sectors_dirty(d);
}
void bch_cached_dev_writeback_stop(struct cached_dev *dc)
{
cancel_delayed_work_sync(&dc->writeback_pd_update);
if (!IS_ERR_OR_NULL(dc->writeback_thread)) {
kthread_stop(dc->writeback_thread);
dc->writeback_thread = NULL;
}
}
void bch_cached_dev_writeback_free(struct cached_dev *dc)
{
struct bcache_device *d = &dc->disk;
mempool_exit(&dc->writeback_page_pool);
mempool_exit(&dc->writeback_io_pool);
kvfree(d->full_dirty_stripes);
kvfree(d->stripe_sectors_dirty);
}
int bch_cached_dev_writeback_init(struct cached_dev *dc)
{
struct bcache_device *d = &dc->disk;
sector_t sectors;
size_t n;
sectors = get_capacity(dc->disk.disk);
if (!d->stripe_size) {
#ifdef CONFIG_BCACHE_DEBUG
d->stripe_size = 1 << 0;
#else
d->stripe_size = 1 << 31;
#endif
}
pr_debug("stripe size: %d sectors", d->stripe_size);
d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
if (!d->nr_stripes ||
d->nr_stripes > INT_MAX ||
d->nr_stripes > SIZE_MAX / sizeof(atomic_t)) {
pr_err("nr_stripes too large or invalid: %u (start sector beyond end of disk?)",
(unsigned)d->nr_stripes);
return -ENOMEM;
}
n = d->nr_stripes * sizeof(atomic_t);
d->stripe_sectors_dirty = n < PAGE_SIZE << 6
? kzalloc(n, GFP_KERNEL)
: vzalloc(n);
if (!d->stripe_sectors_dirty) {
pr_err("cannot allocate stripe_sectors_dirty");
return -ENOMEM;
}
n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
d->full_dirty_stripes = n < PAGE_SIZE << 6
? kzalloc(n, GFP_KERNEL)
: vzalloc(n);
if (!d->full_dirty_stripes) {
pr_err("cannot allocate full_dirty_stripes");
return -ENOMEM;
}
if (mempool_init_kmalloc_pool(&dc->writeback_io_pool, 4,
sizeof(struct dirty_io) +
sizeof(struct bio_vec) *
DIRTY_IO_MEMPOOL_BVECS) ||
mempool_init_page_pool(&dc->writeback_page_pool,
(64 << 10) / PAGE_SIZE, 0))
return -ENOMEM;
init_rwsem(&dc->writeback_lock);
bch_keybuf_init(&dc->writeback_keys);
dc->writeback_metadata = true;
dc->writeback_running = true;
dc->writeback_percent = 10;
dc->writeback_pd_update_seconds = 5;
bch_pd_controller_init(&dc->writeback_pd);
INIT_DELAYED_WORK(&dc->writeback_pd_update, update_writeback_rate);
return 0;
}
int bch_cached_dev_writeback_start(struct cached_dev *dc)
{
dc->writeback_thread = kthread_create(bch_writeback_thread, dc,
"bcache_writeback");
if (IS_ERR(dc->writeback_thread))
return PTR_ERR(dc->writeback_thread);
schedule_delayed_work(&dc->writeback_pd_update,
dc->writeback_pd_update_seconds * HZ);
bch_writeback_queue(dc);
return 0;
}
|