2 * background writeback - scan btree for dirty data and write it to the backing
6 * Copyright 2012 Google, Inc.
12 #include "writeback.h"
14 #include <linux/delay.h>
15 #include <linux/kthread.h>
16 #include <trace/events/bcache.h>
20 static void __update_writeback_rate(struct cached_dev *dc)
22 struct cache_set *c = dc->disk.c;
23 uint64_t cache_sectors = c->nbuckets * c->sb.bucket_size;
24 uint64_t cache_dirty_target =
25 div_u64(cache_sectors * dc->writeback_percent, 100);
27 int64_t target = div64_u64(cache_dirty_target * bdev_sectors(dc->bdev),
28 c->cached_dev_sectors);
32 int64_t dirty = bcache_dev_sectors_dirty(&dc->disk);
33 int64_t derivative = dirty - dc->disk.sectors_dirty_last;
34 int64_t proportional = dirty - target;
37 dc->disk.sectors_dirty_last = dirty;
39 /* Scale to sectors per second */
41 proportional *= dc->writeback_rate_update_seconds;
42 proportional = div_s64(proportional, dc->writeback_rate_p_term_inverse);
44 derivative = div_s64(derivative, dc->writeback_rate_update_seconds);
46 derivative = ewma_add(dc->disk.sectors_dirty_derivative, derivative,
47 (dc->writeback_rate_d_term /
48 dc->writeback_rate_update_seconds) ?: 1, 0);
50 derivative *= dc->writeback_rate_d_term;
51 derivative = div_s64(derivative, dc->writeback_rate_p_term_inverse);
53 change = proportional + derivative;
55 /* Don't increase writeback rate if the device isn't keeping up */
57 time_after64(local_clock(),
58 dc->writeback_rate.next + NSEC_PER_MSEC))
61 dc->writeback_rate.rate =
62 clamp_t(int64_t, (int64_t) dc->writeback_rate.rate + change,
65 dc->writeback_rate_proportional = proportional;
66 dc->writeback_rate_derivative = derivative;
67 dc->writeback_rate_change = change;
68 dc->writeback_rate_target = target;
71 static void update_writeback_rate(struct work_struct *work)
73 struct cached_dev *dc = container_of(to_delayed_work(work),
75 writeback_rate_update);
77 down_read(&dc->writeback_lock);
79 if (atomic_read(&dc->has_dirty) &&
80 dc->writeback_percent)
81 __update_writeback_rate(dc);
83 up_read(&dc->writeback_lock);
85 schedule_delayed_work(&dc->writeback_rate_update,
86 dc->writeback_rate_update_seconds * HZ);
89 static unsigned writeback_delay(struct cached_dev *dc, unsigned sectors)
91 if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
92 !dc->writeback_percent)
95 return bch_next_delay(&dc->writeback_rate, sectors);
100 struct cached_dev *dc;
104 static void dirty_init(struct keybuf_key *w)
106 struct dirty_io *io = w->private;
107 struct bio *bio = &io->bio;
110 if (!io->dc->writeback_percent)
111 bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
113 bio->bi_iter.bi_size = KEY_SIZE(&w->key) << 9;
114 bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS);
116 bio->bi_io_vec = bio->bi_inline_vecs;
117 bch_bio_map(bio, NULL);
120 static void dirty_io_destructor(struct closure *cl)
122 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
126 static void write_dirty_finish(struct closure *cl)
128 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
129 struct keybuf_key *w = io->bio.bi_private;
130 struct cached_dev *dc = io->dc;
132 bio_free_pages(&io->bio);
134 /* This is kind of a dumb way of signalling errors. */
135 if (KEY_DIRTY(&w->key)) {
140 bch_keylist_init(&keys);
142 bkey_copy(keys.top, &w->key);
143 SET_KEY_DIRTY(keys.top, false);
144 bch_keylist_push(&keys);
146 for (i = 0; i < KEY_PTRS(&w->key); i++)
147 atomic_inc(&PTR_BUCKET(dc->disk.c, &w->key, i)->pin);
149 ret = bch_btree_insert(dc->disk.c, &keys, NULL, &w->key);
152 trace_bcache_writeback_collision(&w->key);
155 ? &dc->disk.c->writeback_keys_failed
156 : &dc->disk.c->writeback_keys_done);
159 bch_keybuf_del(&dc->writeback_keys, w);
162 closure_return_with_destructor(cl, dirty_io_destructor);
165 static void dirty_endio(struct bio *bio)
167 struct keybuf_key *w = bio->bi_private;
168 struct dirty_io *io = w->private;
171 SET_KEY_DIRTY(&w->key, false);
173 closure_put(&io->cl);
176 static void write_dirty(struct closure *cl)
178 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
179 struct keybuf_key *w = io->bio.bi_private;
182 bio_set_op_attrs(&io->bio, REQ_OP_WRITE, 0);
183 io->bio.bi_iter.bi_sector = KEY_START(&w->key);
184 io->bio.bi_bdev = io->dc->bdev;
185 io->bio.bi_end_io = dirty_endio;
187 closure_bio_submit(&io->bio, cl);
189 continue_at(cl, write_dirty_finish, system_wq);
192 static void read_dirty_endio(struct bio *bio)
194 struct keybuf_key *w = bio->bi_private;
195 struct dirty_io *io = w->private;
197 bch_count_io_errors(PTR_CACHE(io->dc->disk.c, &w->key, 0),
198 bio->bi_error, "reading dirty data from cache");
203 static void read_dirty_submit(struct closure *cl)
205 struct dirty_io *io = container_of(cl, struct dirty_io, cl);
207 closure_bio_submit(&io->bio, cl);
209 continue_at(cl, write_dirty, system_wq);
212 static void read_dirty(struct cached_dev *dc)
215 struct keybuf_key *w;
219 closure_init_stack(&cl);
222 * XXX: if we error, background writeback just spins. Should use some
226 while (!kthread_should_stop()) {
228 w = bch_keybuf_next(&dc->writeback_keys);
232 BUG_ON(ptr_stale(dc->disk.c, &w->key, 0));
234 if (KEY_START(&w->key) != dc->last_read ||
235 jiffies_to_msecs(delay) > 50)
236 while (!kthread_should_stop() && delay)
237 delay = schedule_timeout_interruptible(delay);
239 dc->last_read = KEY_OFFSET(&w->key);
241 io = kzalloc(sizeof(struct dirty_io) + sizeof(struct bio_vec)
242 * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
251 bio_set_op_attrs(&io->bio, REQ_OP_READ, 0);
252 io->bio.bi_iter.bi_sector = PTR_OFFSET(&w->key, 0);
253 io->bio.bi_bdev = PTR_CACHE(dc->disk.c,
255 io->bio.bi_end_io = read_dirty_endio;
257 if (bio_alloc_pages(&io->bio, GFP_KERNEL))
260 trace_bcache_writeback(&w->key);
262 down(&dc->in_flight);
263 closure_call(&io->cl, read_dirty_submit, NULL, &cl);
265 delay = writeback_delay(dc, KEY_SIZE(&w->key));
272 bch_keybuf_del(&dc->writeback_keys, w);
276 * Wait for outstanding writeback IOs to finish (and keybuf slots to be
277 * freed) before refilling again
282 /* Scan for dirty data */
284 void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned inode,
285 uint64_t offset, int nr_sectors)
287 struct bcache_device *d = c->devices[inode];
288 unsigned stripe_offset, stripe, sectors_dirty;
293 stripe = offset_to_stripe(d, offset);
294 stripe_offset = offset & (d->stripe_size - 1);
297 int s = min_t(unsigned, abs(nr_sectors),
298 d->stripe_size - stripe_offset);
303 if (stripe >= d->nr_stripes)
306 sectors_dirty = atomic_add_return(s,
307 d->stripe_sectors_dirty + stripe);
308 if (sectors_dirty == d->stripe_size)
309 set_bit(stripe, d->full_dirty_stripes);
311 clear_bit(stripe, d->full_dirty_stripes);
319 static bool dirty_pred(struct keybuf *buf, struct bkey *k)
321 struct cached_dev *dc = container_of(buf, struct cached_dev, writeback_keys);
323 BUG_ON(KEY_INODE(k) != dc->disk.id);
328 static void refill_full_stripes(struct cached_dev *dc)
330 struct keybuf *buf = &dc->writeback_keys;
331 unsigned start_stripe, stripe, next_stripe;
332 bool wrapped = false;
334 stripe = offset_to_stripe(&dc->disk, KEY_OFFSET(&buf->last_scanned));
336 if (stripe >= dc->disk.nr_stripes)
339 start_stripe = stripe;
342 stripe = find_next_bit(dc->disk.full_dirty_stripes,
343 dc->disk.nr_stripes, stripe);
345 if (stripe == dc->disk.nr_stripes)
348 next_stripe = find_next_zero_bit(dc->disk.full_dirty_stripes,
349 dc->disk.nr_stripes, stripe);
351 buf->last_scanned = KEY(dc->disk.id,
352 stripe * dc->disk.stripe_size, 0);
354 bch_refill_keybuf(dc->disk.c, buf,
356 next_stripe * dc->disk.stripe_size, 0),
359 if (array_freelist_empty(&buf->freelist))
362 stripe = next_stripe;
364 if (wrapped && stripe > start_stripe)
367 if (stripe == dc->disk.nr_stripes) {
375 * Returns true if we scanned the entire disk
377 static bool refill_dirty(struct cached_dev *dc)
379 struct keybuf *buf = &dc->writeback_keys;
380 struct bkey start = KEY(dc->disk.id, 0, 0);
381 struct bkey end = KEY(dc->disk.id, MAX_KEY_OFFSET, 0);
382 struct bkey start_pos;
385 * make sure keybuf pos is inside the range for this disk - at bringup
386 * we might not be attached yet so this disk's inode nr isn't
389 if (bkey_cmp(&buf->last_scanned, &start) < 0 ||
390 bkey_cmp(&buf->last_scanned, &end) > 0)
391 buf->last_scanned = start;
393 if (dc->partial_stripes_expensive) {
394 refill_full_stripes(dc);
395 if (array_freelist_empty(&buf->freelist))
399 start_pos = buf->last_scanned;
400 bch_refill_keybuf(dc->disk.c, buf, &end, dirty_pred);
402 if (bkey_cmp(&buf->last_scanned, &end) < 0)
406 * If we get to the end start scanning again from the beginning, and
407 * only scan up to where we initially started scanning from:
409 buf->last_scanned = start;
410 bch_refill_keybuf(dc->disk.c, buf, &start_pos, dirty_pred);
412 return bkey_cmp(&buf->last_scanned, &start_pos) >= 0;
415 static int bch_writeback_thread(void *arg)
417 struct cached_dev *dc = arg;
418 bool searched_full_index;
420 while (!kthread_should_stop()) {
421 down_write(&dc->writeback_lock);
422 if (!atomic_read(&dc->has_dirty) ||
423 (!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) &&
424 !dc->writeback_running)) {
425 up_write(&dc->writeback_lock);
426 set_current_state(TASK_INTERRUPTIBLE);
428 if (kthread_should_stop())
435 searched_full_index = refill_dirty(dc);
437 if (searched_full_index &&
438 RB_EMPTY_ROOT(&dc->writeback_keys.keys)) {
439 atomic_set(&dc->has_dirty, 0);
441 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
442 bch_write_bdev_super(dc, NULL);
445 up_write(&dc->writeback_lock);
447 bch_ratelimit_reset(&dc->writeback_rate);
450 if (searched_full_index) {
451 unsigned delay = dc->writeback_delay * HZ;
454 !kthread_should_stop() &&
455 !test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
456 delay = schedule_timeout_interruptible(delay);
465 struct sectors_dirty_init {
470 static int sectors_dirty_init_fn(struct btree_op *_op, struct btree *b,
473 struct sectors_dirty_init *op = container_of(_op,
474 struct sectors_dirty_init, op);
475 if (KEY_INODE(k) > op->inode)
479 bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
480 KEY_START(k), KEY_SIZE(k));
485 void bch_sectors_dirty_init(struct cached_dev *dc)
487 struct sectors_dirty_init op;
489 bch_btree_op_init(&op.op, -1);
490 op.inode = dc->disk.id;
492 bch_btree_map_keys(&op.op, dc->disk.c, &KEY(op.inode, 0, 0),
493 sectors_dirty_init_fn, 0);
495 dc->disk.sectors_dirty_last = bcache_dev_sectors_dirty(&dc->disk);
498 void bch_cached_dev_writeback_init(struct cached_dev *dc)
500 sema_init(&dc->in_flight, 64);
501 init_rwsem(&dc->writeback_lock);
502 bch_keybuf_init(&dc->writeback_keys);
504 dc->writeback_metadata = true;
505 dc->writeback_running = true;
506 dc->writeback_percent = 10;
507 dc->writeback_delay = 30;
508 dc->writeback_rate.rate = 1024;
510 dc->writeback_rate_update_seconds = 5;
511 dc->writeback_rate_d_term = 30;
512 dc->writeback_rate_p_term_inverse = 6000;
514 INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate);
517 int bch_cached_dev_writeback_start(struct cached_dev *dc)
519 dc->writeback_thread = kthread_create(bch_writeback_thread, dc,
521 if (IS_ERR(dc->writeback_thread))
522 return PTR_ERR(dc->writeback_thread);
524 schedule_delayed_work(&dc->writeback_rate_update,
525 dc->writeback_rate_update_seconds * HZ);
527 bch_writeback_queue(dc);