writeback.c 13 KB

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