writeback.c 14 KB

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