writeback.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * background writeback - scan btree for dirty data and write it to the backing
  3. * device
  4. *
  5. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  6. * Copyright 2012 Google, Inc.
  7. */
  8. #include "bcache.h"
  9. #include "btree.h"
  10. #include "debug.h"
  11. #include "writeback.h"
  12. #include <linux/delay.h>
  13. #include <linux/kthread.h>
  14. #include <trace/events/bcache.h>
  15. /* Rate limiting */
  16. static void __update_writeback_rate(struct cached_dev *dc)
  17. {
  18. struct cache_set *c = dc->disk.c;
  19. uint64_t cache_sectors = c->nbuckets * c->sb.bucket_size;
  20. uint64_t cache_dirty_target =
  21. div_u64(cache_sectors * dc->writeback_percent, 100);
  22. int64_t target = div64_u64(cache_dirty_target * bdev_sectors(dc->bdev),
  23. c->cached_dev_sectors);
  24. /* PD controller */
  25. int64_t dirty = bcache_dev_sectors_dirty(&dc->disk);
  26. int64_t derivative = dirty - dc->disk.sectors_dirty_last;
  27. int64_t proportional = dirty - target;
  28. int64_t change;
  29. dc->disk.sectors_dirty_last = dirty;
  30. /* Scale to sectors per second */
  31. proportional *= dc->writeback_rate_update_seconds;
  32. proportional = div_s64(proportional, dc->writeback_rate_p_term_inverse);
  33. derivative = div_s64(derivative, dc->writeback_rate_update_seconds);
  34. derivative = ewma_add(dc->disk.sectors_dirty_derivative, derivative,
  35. (dc->writeback_rate_d_term /
  36. dc->writeback_rate_update_seconds) ?: 1, 0);
  37. derivative *= dc->writeback_rate_d_term;
  38. derivative = div_s64(derivative, dc->writeback_rate_p_term_inverse);
  39. change = proportional + derivative;
  40. /* Don't increase writeback rate if the device isn't keeping up */
  41. if (change > 0 &&
  42. time_after64(local_clock(),
  43. dc->writeback_rate.next + NSEC_PER_MSEC))
  44. change = 0;
  45. dc->writeback_rate.rate =
  46. clamp_t(int64_t, (int64_t) dc->writeback_rate.rate + change,
  47. 1, NSEC_PER_MSEC);
  48. dc->writeback_rate_proportional = proportional;
  49. dc->writeback_rate_derivative = derivative;
  50. dc->writeback_rate_change = change;
  51. dc->writeback_rate_target = target;
  52. }
  53. static void update_writeback_rate(struct work_struct *work)
  54. {
  55. struct cached_dev *dc = container_of(to_delayed_work(work),
  56. struct cached_dev,
  57. writeback_rate_update);
  58. down_read(&dc->writeback_lock);
  59. if (atomic_read(&dc->has_dirty) &&
  60. dc->writeback_percent)
  61. __update_writeback_rate(dc);
  62. up_read(&dc->writeback_lock);
  63. schedule_delayed_work(&dc->writeback_rate_update,
  64. dc->writeback_rate_update_seconds * HZ);
  65. }
  66. static unsigned writeback_delay(struct cached_dev *dc, unsigned sectors)
  67. {
  68. if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  69. !dc->writeback_percent)
  70. return 0;
  71. return bch_next_delay(&dc->writeback_rate, sectors);
  72. }
  73. struct dirty_io {
  74. struct closure cl;
  75. struct cached_dev *dc;
  76. struct bio bio;
  77. };
  78. static void dirty_init(struct keybuf_key *w)
  79. {
  80. struct dirty_io *io = w->private;
  81. struct bio *bio = &io->bio;
  82. bio_init(bio);
  83. if (!io->dc->writeback_percent)
  84. bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
  85. bio->bi_iter.bi_size = KEY_SIZE(&w->key) << 9;
  86. bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS);
  87. bio->bi_private = w;
  88. bio->bi_io_vec = bio->bi_inline_vecs;
  89. bch_bio_map(bio, NULL);
  90. }
  91. static void dirty_io_destructor(struct closure *cl)
  92. {
  93. struct dirty_io *io = container_of(cl, struct dirty_io, cl);
  94. kfree(io);
  95. }
  96. static void write_dirty_finish(struct closure *cl)
  97. {
  98. struct dirty_io *io = container_of(cl, struct dirty_io, cl);
  99. struct keybuf_key *w = io->bio.bi_private;
  100. struct cached_dev *dc = io->dc;
  101. struct bio_vec *bv;
  102. int i;
  103. bio_for_each_segment_all(bv, &io->bio, i)
  104. __free_page(bv->bv_page);
  105. /* This is kind of a dumb way of signalling errors. */
  106. if (KEY_DIRTY(&w->key)) {
  107. int ret;
  108. unsigned i;
  109. struct keylist keys;
  110. bch_keylist_init(&keys);
  111. bkey_copy(keys.top, &w->key);
  112. SET_KEY_DIRTY(keys.top, false);
  113. bch_keylist_push(&keys);
  114. for (i = 0; i < KEY_PTRS(&w->key); i++)
  115. atomic_inc(&PTR_BUCKET(dc->disk.c, &w->key, i)->pin);
  116. ret = bch_btree_insert(dc->disk.c, &keys, NULL, &w->key);
  117. if (ret)
  118. trace_bcache_writeback_collision(&w->key);
  119. atomic_long_inc(ret
  120. ? &dc->disk.c->writeback_keys_failed
  121. : &dc->disk.c->writeback_keys_done);
  122. }
  123. bch_keybuf_del(&dc->writeback_keys, w);
  124. up(&dc->in_flight);
  125. closure_return_with_destructor(cl, dirty_io_destructor);
  126. }
  127. static void dirty_endio(struct bio *bio)
  128. {
  129. struct keybuf_key *w = bio->bi_private;
  130. struct dirty_io *io = w->private;
  131. if (bio->bi_error)
  132. SET_KEY_DIRTY(&w->key, false);
  133. closure_put(&io->cl);
  134. }
  135. static void write_dirty(struct closure *cl)
  136. {
  137. struct dirty_io *io = container_of(cl, struct dirty_io, cl);
  138. struct keybuf_key *w = io->bio.bi_private;
  139. dirty_init(w);
  140. bio_set_op_attrs(&io->bio, REQ_OP_WRITE, 0);
  141. io->bio.bi_iter.bi_sector = KEY_START(&w->key);
  142. io->bio.bi_bdev = io->dc->bdev;
  143. io->bio.bi_end_io = dirty_endio;
  144. closure_bio_submit(&io->bio, cl);
  145. continue_at(cl, write_dirty_finish, system_wq);
  146. }
  147. static void read_dirty_endio(struct bio *bio)
  148. {
  149. struct keybuf_key *w = bio->bi_private;
  150. struct dirty_io *io = w->private;
  151. bch_count_io_errors(PTR_CACHE(io->dc->disk.c, &w->key, 0),
  152. bio->bi_error, "reading dirty data from cache");
  153. dirty_endio(bio);
  154. }
  155. static void read_dirty_submit(struct closure *cl)
  156. {
  157. struct dirty_io *io = container_of(cl, struct dirty_io, cl);
  158. closure_bio_submit(&io->bio, cl);
  159. continue_at(cl, write_dirty, system_wq);
  160. }
  161. static void read_dirty(struct cached_dev *dc)
  162. {
  163. unsigned delay = 0;
  164. struct keybuf_key *w;
  165. struct dirty_io *io;
  166. struct closure cl;
  167. closure_init_stack(&cl);
  168. /*
  169. * XXX: if we error, background writeback just spins. Should use some
  170. * mempools.
  171. */
  172. while (!kthread_should_stop()) {
  173. w = bch_keybuf_next(&dc->writeback_keys);
  174. if (!w)
  175. break;
  176. BUG_ON(ptr_stale(dc->disk.c, &w->key, 0));
  177. if (KEY_START(&w->key) != dc->last_read ||
  178. jiffies_to_msecs(delay) > 50)
  179. while (!kthread_should_stop() && delay)
  180. delay = schedule_timeout_interruptible(delay);
  181. dc->last_read = KEY_OFFSET(&w->key);
  182. io = kzalloc(sizeof(struct dirty_io) + sizeof(struct bio_vec)
  183. * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
  184. GFP_KERNEL);
  185. if (!io)
  186. goto err;
  187. w->private = io;
  188. io->dc = dc;
  189. dirty_init(w);
  190. bio_set_op_attrs(&io->bio, REQ_OP_READ, 0);
  191. io->bio.bi_iter.bi_sector = PTR_OFFSET(&w->key, 0);
  192. io->bio.bi_bdev = PTR_CACHE(dc->disk.c,
  193. &w->key, 0)->bdev;
  194. io->bio.bi_end_io = read_dirty_endio;
  195. if (bio_alloc_pages(&io->bio, GFP_KERNEL))
  196. goto err_free;
  197. trace_bcache_writeback(&w->key);
  198. down(&dc->in_flight);
  199. closure_call(&io->cl, read_dirty_submit, NULL, &cl);
  200. delay = writeback_delay(dc, KEY_SIZE(&w->key));
  201. }
  202. if (0) {
  203. err_free:
  204. kfree(w->private);
  205. err:
  206. bch_keybuf_del(&dc->writeback_keys, w);
  207. }
  208. /*
  209. * Wait for outstanding writeback IOs to finish (and keybuf slots to be
  210. * freed) before refilling again
  211. */
  212. closure_sync(&cl);
  213. }
  214. /* Scan for dirty data */
  215. void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned inode,
  216. uint64_t offset, int nr_sectors)
  217. {
  218. struct bcache_device *d = c->devices[inode];
  219. unsigned stripe_offset, stripe, sectors_dirty;
  220. if (!d)
  221. return;
  222. stripe = offset_to_stripe(d, offset);
  223. stripe_offset = offset & (d->stripe_size - 1);
  224. while (nr_sectors) {
  225. int s = min_t(unsigned, abs(nr_sectors),
  226. d->stripe_size - stripe_offset);
  227. if (nr_sectors < 0)
  228. s = -s;
  229. if (stripe >= d->nr_stripes)
  230. return;
  231. sectors_dirty = atomic_add_return(s,
  232. d->stripe_sectors_dirty + stripe);
  233. if (sectors_dirty == d->stripe_size)
  234. set_bit(stripe, d->full_dirty_stripes);
  235. else
  236. clear_bit(stripe, d->full_dirty_stripes);
  237. nr_sectors -= s;
  238. stripe_offset = 0;
  239. stripe++;
  240. }
  241. }
  242. static bool dirty_pred(struct keybuf *buf, struct bkey *k)
  243. {
  244. struct cached_dev *dc = container_of(buf, struct cached_dev, writeback_keys);
  245. BUG_ON(KEY_INODE(k) != dc->disk.id);
  246. return KEY_DIRTY(k);
  247. }
  248. static void refill_full_stripes(struct cached_dev *dc)
  249. {
  250. struct keybuf *buf = &dc->writeback_keys;
  251. unsigned start_stripe, stripe, next_stripe;
  252. bool wrapped = false;
  253. stripe = offset_to_stripe(&dc->disk, KEY_OFFSET(&buf->last_scanned));
  254. if (stripe >= dc->disk.nr_stripes)
  255. stripe = 0;
  256. start_stripe = stripe;
  257. while (1) {
  258. stripe = find_next_bit(dc->disk.full_dirty_stripes,
  259. dc->disk.nr_stripes, stripe);
  260. if (stripe == dc->disk.nr_stripes)
  261. goto next;
  262. next_stripe = find_next_zero_bit(dc->disk.full_dirty_stripes,
  263. dc->disk.nr_stripes, stripe);
  264. buf->last_scanned = KEY(dc->disk.id,
  265. stripe * dc->disk.stripe_size, 0);
  266. bch_refill_keybuf(dc->disk.c, buf,
  267. &KEY(dc->disk.id,
  268. next_stripe * dc->disk.stripe_size, 0),
  269. dirty_pred);
  270. if (array_freelist_empty(&buf->freelist))
  271. return;
  272. stripe = next_stripe;
  273. next:
  274. if (wrapped && stripe > start_stripe)
  275. return;
  276. if (stripe == dc->disk.nr_stripes) {
  277. stripe = 0;
  278. wrapped = true;
  279. }
  280. }
  281. }
  282. /*
  283. * Returns true if we scanned the entire disk
  284. */
  285. static bool refill_dirty(struct cached_dev *dc)
  286. {
  287. struct keybuf *buf = &dc->writeback_keys;
  288. struct bkey start = KEY(dc->disk.id, 0, 0);
  289. struct bkey end = KEY(dc->disk.id, MAX_KEY_OFFSET, 0);
  290. struct bkey start_pos;
  291. /*
  292. * make sure keybuf pos is inside the range for this disk - at bringup
  293. * we might not be attached yet so this disk's inode nr isn't
  294. * initialized then
  295. */
  296. if (bkey_cmp(&buf->last_scanned, &start) < 0 ||
  297. bkey_cmp(&buf->last_scanned, &end) > 0)
  298. buf->last_scanned = start;
  299. if (dc->partial_stripes_expensive) {
  300. refill_full_stripes(dc);
  301. if (array_freelist_empty(&buf->freelist))
  302. return false;
  303. }
  304. start_pos = buf->last_scanned;
  305. bch_refill_keybuf(dc->disk.c, buf, &end, dirty_pred);
  306. if (bkey_cmp(&buf->last_scanned, &end) < 0)
  307. return false;
  308. /*
  309. * If we get to the end start scanning again from the beginning, and
  310. * only scan up to where we initially started scanning from:
  311. */
  312. buf->last_scanned = start;
  313. bch_refill_keybuf(dc->disk.c, buf, &start_pos, dirty_pred);
  314. return bkey_cmp(&buf->last_scanned, &start_pos) >= 0;
  315. }
  316. static int bch_writeback_thread(void *arg)
  317. {
  318. struct cached_dev *dc = arg;
  319. bool searched_full_index;
  320. while (!kthread_should_stop()) {
  321. down_write(&dc->writeback_lock);
  322. if (!atomic_read(&dc->has_dirty) ||
  323. (!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) &&
  324. !dc->writeback_running)) {
  325. up_write(&dc->writeback_lock);
  326. set_current_state(TASK_INTERRUPTIBLE);
  327. if (kthread_should_stop())
  328. return 0;
  329. schedule();
  330. continue;
  331. }
  332. searched_full_index = refill_dirty(dc);
  333. if (searched_full_index &&
  334. RB_EMPTY_ROOT(&dc->writeback_keys.keys)) {
  335. atomic_set(&dc->has_dirty, 0);
  336. cached_dev_put(dc);
  337. SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
  338. bch_write_bdev_super(dc, NULL);
  339. }
  340. up_write(&dc->writeback_lock);
  341. bch_ratelimit_reset(&dc->writeback_rate);
  342. read_dirty(dc);
  343. if (searched_full_index) {
  344. unsigned delay = dc->writeback_delay * HZ;
  345. while (delay &&
  346. !kthread_should_stop() &&
  347. !test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
  348. delay = schedule_timeout_interruptible(delay);
  349. }
  350. }
  351. return 0;
  352. }
  353. /* Init */
  354. struct sectors_dirty_init {
  355. struct btree_op op;
  356. unsigned inode;
  357. };
  358. static int sectors_dirty_init_fn(struct btree_op *_op, struct btree *b,
  359. struct bkey *k)
  360. {
  361. struct sectors_dirty_init *op = container_of(_op,
  362. struct sectors_dirty_init, op);
  363. if (KEY_INODE(k) > op->inode)
  364. return MAP_DONE;
  365. if (KEY_DIRTY(k))
  366. bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
  367. KEY_START(k), KEY_SIZE(k));
  368. return MAP_CONTINUE;
  369. }
  370. void bch_sectors_dirty_init(struct cached_dev *dc)
  371. {
  372. struct sectors_dirty_init op;
  373. bch_btree_op_init(&op.op, -1);
  374. op.inode = dc->disk.id;
  375. bch_btree_map_keys(&op.op, dc->disk.c, &KEY(op.inode, 0, 0),
  376. sectors_dirty_init_fn, 0);
  377. dc->disk.sectors_dirty_last = bcache_dev_sectors_dirty(&dc->disk);
  378. }
  379. void bch_cached_dev_writeback_init(struct cached_dev *dc)
  380. {
  381. sema_init(&dc->in_flight, 64);
  382. init_rwsem(&dc->writeback_lock);
  383. bch_keybuf_init(&dc->writeback_keys);
  384. dc->writeback_metadata = true;
  385. dc->writeback_running = true;
  386. dc->writeback_percent = 10;
  387. dc->writeback_delay = 30;
  388. dc->writeback_rate.rate = 1024;
  389. dc->writeback_rate_update_seconds = 5;
  390. dc->writeback_rate_d_term = 30;
  391. dc->writeback_rate_p_term_inverse = 6000;
  392. INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate);
  393. }
  394. int bch_cached_dev_writeback_start(struct cached_dev *dc)
  395. {
  396. dc->writeback_thread = kthread_create(bch_writeback_thread, dc,
  397. "bcache_writeback");
  398. if (IS_ERR(dc->writeback_thread))
  399. return PTR_ERR(dc->writeback_thread);
  400. schedule_delayed_work(&dc->writeback_rate_update,
  401. dc->writeback_rate_update_seconds * HZ);
  402. bch_writeback_queue(dc);
  403. return 0;
  404. }