writeback.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 uint64_t __calc_target_rate(struct cached_dev *dc)
  19. {
  20. struct cache_set *c = dc->disk.c;
  21. /*
  22. * This is the size of the cache, minus the amount used for
  23. * flash-only devices
  24. */
  25. uint64_t cache_sectors = c->nbuckets * c->sb.bucket_size -
  26. atomic_long_read(&c->flash_dev_dirty_sectors);
  27. /*
  28. * Unfortunately there is no control of global dirty data. If the
  29. * user states that they want 10% dirty data in the cache, and has,
  30. * e.g., 5 backing volumes of equal size, we try and ensure each
  31. * backing volume uses about 2% of the cache for dirty data.
  32. */
  33. uint32_t bdev_share =
  34. div64_u64(bdev_sectors(dc->bdev) << WRITEBACK_SHARE_SHIFT,
  35. c->cached_dev_sectors);
  36. uint64_t cache_dirty_target =
  37. div_u64(cache_sectors * dc->writeback_percent, 100);
  38. /* Ensure each backing dev gets at least one dirty share */
  39. if (bdev_share < 1)
  40. bdev_share = 1;
  41. return (cache_dirty_target * bdev_share) >> WRITEBACK_SHARE_SHIFT;
  42. }
  43. static void __update_writeback_rate(struct cached_dev *dc)
  44. {
  45. /*
  46. * PI controller:
  47. * Figures out the amount that should be written per second.
  48. *
  49. * First, the error (number of sectors that are dirty beyond our
  50. * target) is calculated. The error is accumulated (numerically
  51. * integrated).
  52. *
  53. * Then, the proportional value and integral value are scaled
  54. * based on configured values. These are stored as inverses to
  55. * avoid fixed point math and to make configuration easy-- e.g.
  56. * the default value of 40 for writeback_rate_p_term_inverse
  57. * attempts to write at a rate that would retire all the dirty
  58. * blocks in 40 seconds.
  59. *
  60. * The writeback_rate_i_inverse value of 10000 means that 1/10000th
  61. * of the error is accumulated in the integral term per second.
  62. * This acts as a slow, long-term average that is not subject to
  63. * variations in usage like the p term.
  64. */
  65. int64_t target = __calc_target_rate(dc);
  66. int64_t dirty = bcache_dev_sectors_dirty(&dc->disk);
  67. int64_t error = dirty - target;
  68. int64_t proportional_scaled =
  69. div_s64(error, dc->writeback_rate_p_term_inverse);
  70. int64_t integral_scaled;
  71. uint32_t new_rate;
  72. if ((error < 0 && dc->writeback_rate_integral > 0) ||
  73. (error > 0 && time_before64(local_clock(),
  74. dc->writeback_rate.next + NSEC_PER_MSEC))) {
  75. /*
  76. * Only decrease the integral term if it's more than
  77. * zero. Only increase the integral term if the device
  78. * is keeping up. (Don't wind up the integral
  79. * ineffectively in either case).
  80. *
  81. * It's necessary to scale this by
  82. * writeback_rate_update_seconds to keep the integral
  83. * term dimensioned properly.
  84. */
  85. dc->writeback_rate_integral += error *
  86. dc->writeback_rate_update_seconds;
  87. }
  88. integral_scaled = div_s64(dc->writeback_rate_integral,
  89. dc->writeback_rate_i_term_inverse);
  90. new_rate = clamp_t(int32_t, (proportional_scaled + integral_scaled),
  91. dc->writeback_rate_minimum, NSEC_PER_SEC);
  92. dc->writeback_rate_proportional = proportional_scaled;
  93. dc->writeback_rate_integral_scaled = integral_scaled;
  94. dc->writeback_rate_change = new_rate -
  95. atomic_long_read(&dc->writeback_rate.rate);
  96. atomic_long_set(&dc->writeback_rate.rate, new_rate);
  97. dc->writeback_rate_target = target;
  98. }
  99. static bool set_at_max_writeback_rate(struct cache_set *c,
  100. struct cached_dev *dc)
  101. {
  102. /*
  103. * Idle_counter is increased everytime when update_writeback_rate() is
  104. * called. If all backing devices attached to the same cache set have
  105. * identical dc->writeback_rate_update_seconds values, it is about 6
  106. * rounds of update_writeback_rate() on each backing device before
  107. * c->at_max_writeback_rate is set to 1, and then max wrteback rate set
  108. * to each dc->writeback_rate.rate.
  109. * In order to avoid extra locking cost for counting exact dirty cached
  110. * devices number, c->attached_dev_nr is used to calculate the idle
  111. * throushold. It might be bigger if not all cached device are in write-
  112. * back mode, but it still works well with limited extra rounds of
  113. * update_writeback_rate().
  114. */
  115. if (atomic_inc_return(&c->idle_counter) <
  116. atomic_read(&c->attached_dev_nr) * 6)
  117. return false;
  118. if (atomic_read(&c->at_max_writeback_rate) != 1)
  119. atomic_set(&c->at_max_writeback_rate, 1);
  120. atomic_long_set(&dc->writeback_rate.rate, INT_MAX);
  121. /* keep writeback_rate_target as existing value */
  122. dc->writeback_rate_proportional = 0;
  123. dc->writeback_rate_integral_scaled = 0;
  124. dc->writeback_rate_change = 0;
  125. /*
  126. * Check c->idle_counter and c->at_max_writeback_rate agagain in case
  127. * new I/O arrives during before set_at_max_writeback_rate() returns.
  128. * Then the writeback rate is set to 1, and its new value should be
  129. * decided via __update_writeback_rate().
  130. */
  131. if ((atomic_read(&c->idle_counter) <
  132. atomic_read(&c->attached_dev_nr) * 6) ||
  133. !atomic_read(&c->at_max_writeback_rate))
  134. return false;
  135. return true;
  136. }
  137. static void update_writeback_rate(struct work_struct *work)
  138. {
  139. struct cached_dev *dc = container_of(to_delayed_work(work),
  140. struct cached_dev,
  141. writeback_rate_update);
  142. struct cache_set *c = dc->disk.c;
  143. /*
  144. * should check BCACHE_DEV_RATE_DW_RUNNING before calling
  145. * cancel_delayed_work_sync().
  146. */
  147. set_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags);
  148. /* paired with where BCACHE_DEV_RATE_DW_RUNNING is tested */
  149. smp_mb();
  150. /*
  151. * CACHE_SET_IO_DISABLE might be set via sysfs interface,
  152. * check it here too.
  153. */
  154. if (!test_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags) ||
  155. test_bit(CACHE_SET_IO_DISABLE, &c->flags)) {
  156. clear_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags);
  157. /* paired with where BCACHE_DEV_RATE_DW_RUNNING is tested */
  158. smp_mb();
  159. return;
  160. }
  161. if (atomic_read(&dc->has_dirty) && dc->writeback_percent) {
  162. /*
  163. * If the whole cache set is idle, set_at_max_writeback_rate()
  164. * will set writeback rate to a max number. Then it is
  165. * unncessary to update writeback rate for an idle cache set
  166. * in maximum writeback rate number(s).
  167. */
  168. if (!set_at_max_writeback_rate(c, dc)) {
  169. down_read(&dc->writeback_lock);
  170. __update_writeback_rate(dc);
  171. up_read(&dc->writeback_lock);
  172. }
  173. }
  174. /*
  175. * CACHE_SET_IO_DISABLE might be set via sysfs interface,
  176. * check it here too.
  177. */
  178. if (test_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags) &&
  179. !test_bit(CACHE_SET_IO_DISABLE, &c->flags)) {
  180. schedule_delayed_work(&dc->writeback_rate_update,
  181. dc->writeback_rate_update_seconds * HZ);
  182. }
  183. /*
  184. * should check BCACHE_DEV_RATE_DW_RUNNING before calling
  185. * cancel_delayed_work_sync().
  186. */
  187. clear_bit(BCACHE_DEV_RATE_DW_RUNNING, &dc->disk.flags);
  188. /* paired with where BCACHE_DEV_RATE_DW_RUNNING is tested */
  189. smp_mb();
  190. }
  191. static unsigned int writeback_delay(struct cached_dev *dc,
  192. unsigned int sectors)
  193. {
  194. if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
  195. !dc->writeback_percent)
  196. return 0;
  197. return bch_next_delay(&dc->writeback_rate, sectors);
  198. }
  199. struct dirty_io {
  200. struct closure cl;
  201. struct cached_dev *dc;
  202. uint16_t sequence;
  203. struct bio bio;
  204. };
  205. static void dirty_init(struct keybuf_key *w)
  206. {
  207. struct dirty_io *io = w->private;
  208. struct bio *bio = &io->bio;
  209. bio_init(bio, bio->bi_inline_vecs,
  210. DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS));
  211. if (!io->dc->writeback_percent)
  212. bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
  213. bio->bi_iter.bi_size = KEY_SIZE(&w->key) << 9;
  214. bio->bi_private = w;
  215. bch_bio_map(bio, NULL);
  216. }
  217. static void dirty_io_destructor(struct closure *cl)
  218. {
  219. struct dirty_io *io = container_of(cl, struct dirty_io, cl);
  220. kfree(io);
  221. }
  222. static void write_dirty_finish(struct closure *cl)
  223. {
  224. struct dirty_io *io = container_of(cl, struct dirty_io, cl);
  225. struct keybuf_key *w = io->bio.bi_private;
  226. struct cached_dev *dc = io->dc;
  227. bio_free_pages(&io->bio);
  228. /* This is kind of a dumb way of signalling errors. */
  229. if (KEY_DIRTY(&w->key)) {
  230. int ret;
  231. unsigned int i;
  232. struct keylist keys;
  233. bch_keylist_init(&keys);
  234. bkey_copy(keys.top, &w->key);
  235. SET_KEY_DIRTY(keys.top, false);
  236. bch_keylist_push(&keys);
  237. for (i = 0; i < KEY_PTRS(&w->key); i++)
  238. atomic_inc(&PTR_BUCKET(dc->disk.c, &w->key, i)->pin);
  239. ret = bch_btree_insert(dc->disk.c, &keys, NULL, &w->key);
  240. if (ret)
  241. trace_bcache_writeback_collision(&w->key);
  242. atomic_long_inc(ret
  243. ? &dc->disk.c->writeback_keys_failed
  244. : &dc->disk.c->writeback_keys_done);
  245. }
  246. bch_keybuf_del(&dc->writeback_keys, w);
  247. up(&dc->in_flight);
  248. closure_return_with_destructor(cl, dirty_io_destructor);
  249. }
  250. static void dirty_endio(struct bio *bio)
  251. {
  252. struct keybuf_key *w = bio->bi_private;
  253. struct dirty_io *io = w->private;
  254. if (bio->bi_status) {
  255. SET_KEY_DIRTY(&w->key, false);
  256. bch_count_backing_io_errors(io->dc, bio);
  257. }
  258. closure_put(&io->cl);
  259. }
  260. static void write_dirty(struct closure *cl)
  261. {
  262. struct dirty_io *io = container_of(cl, struct dirty_io, cl);
  263. struct keybuf_key *w = io->bio.bi_private;
  264. struct cached_dev *dc = io->dc;
  265. uint16_t next_sequence;
  266. if (atomic_read(&dc->writeback_sequence_next) != io->sequence) {
  267. /* Not our turn to write; wait for a write to complete */
  268. closure_wait(&dc->writeback_ordering_wait, cl);
  269. if (atomic_read(&dc->writeback_sequence_next) == io->sequence) {
  270. /*
  271. * Edge case-- it happened in indeterminate order
  272. * relative to when we were added to wait list..
  273. */
  274. closure_wake_up(&dc->writeback_ordering_wait);
  275. }
  276. continue_at(cl, write_dirty, io->dc->writeback_write_wq);
  277. return;
  278. }
  279. next_sequence = io->sequence + 1;
  280. /*
  281. * IO errors are signalled using the dirty bit on the key.
  282. * If we failed to read, we should not attempt to write to the
  283. * backing device. Instead, immediately go to write_dirty_finish
  284. * to clean up.
  285. */
  286. if (KEY_DIRTY(&w->key)) {
  287. dirty_init(w);
  288. bio_set_op_attrs(&io->bio, REQ_OP_WRITE, 0);
  289. io->bio.bi_iter.bi_sector = KEY_START(&w->key);
  290. bio_set_dev(&io->bio, io->dc->bdev);
  291. io->bio.bi_end_io = dirty_endio;
  292. /* I/O request sent to backing device */
  293. closure_bio_submit(io->dc->disk.c, &io->bio, cl);
  294. }
  295. atomic_set(&dc->writeback_sequence_next, next_sequence);
  296. closure_wake_up(&dc->writeback_ordering_wait);
  297. continue_at(cl, write_dirty_finish, io->dc->writeback_write_wq);
  298. }
  299. static void read_dirty_endio(struct bio *bio)
  300. {
  301. struct keybuf_key *w = bio->bi_private;
  302. struct dirty_io *io = w->private;
  303. /* is_read = 1 */
  304. bch_count_io_errors(PTR_CACHE(io->dc->disk.c, &w->key, 0),
  305. bio->bi_status, 1,
  306. "reading dirty data from cache");
  307. dirty_endio(bio);
  308. }
  309. static void read_dirty_submit(struct closure *cl)
  310. {
  311. struct dirty_io *io = container_of(cl, struct dirty_io, cl);
  312. closure_bio_submit(io->dc->disk.c, &io->bio, cl);
  313. continue_at(cl, write_dirty, io->dc->writeback_write_wq);
  314. }
  315. static void read_dirty(struct cached_dev *dc)
  316. {
  317. unsigned int delay = 0;
  318. struct keybuf_key *next, *keys[MAX_WRITEBACKS_IN_PASS], *w;
  319. size_t size;
  320. int nk, i;
  321. struct dirty_io *io;
  322. struct closure cl;
  323. uint16_t sequence = 0;
  324. BUG_ON(!llist_empty(&dc->writeback_ordering_wait.list));
  325. atomic_set(&dc->writeback_sequence_next, sequence);
  326. closure_init_stack(&cl);
  327. /*
  328. * XXX: if we error, background writeback just spins. Should use some
  329. * mempools.
  330. */
  331. next = bch_keybuf_next(&dc->writeback_keys);
  332. while (!kthread_should_stop() &&
  333. !test_bit(CACHE_SET_IO_DISABLE, &dc->disk.c->flags) &&
  334. next) {
  335. size = 0;
  336. nk = 0;
  337. do {
  338. BUG_ON(ptr_stale(dc->disk.c, &next->key, 0));
  339. /*
  340. * Don't combine too many operations, even if they
  341. * are all small.
  342. */
  343. if (nk >= MAX_WRITEBACKS_IN_PASS)
  344. break;
  345. /*
  346. * If the current operation is very large, don't
  347. * further combine operations.
  348. */
  349. if (size >= MAX_WRITESIZE_IN_PASS)
  350. break;
  351. /*
  352. * Operations are only eligible to be combined
  353. * if they are contiguous.
  354. *
  355. * TODO: add a heuristic willing to fire a
  356. * certain amount of non-contiguous IO per pass,
  357. * so that we can benefit from backing device
  358. * command queueing.
  359. */
  360. if ((nk != 0) && bkey_cmp(&keys[nk-1]->key,
  361. &START_KEY(&next->key)))
  362. break;
  363. size += KEY_SIZE(&next->key);
  364. keys[nk++] = next;
  365. } while ((next = bch_keybuf_next(&dc->writeback_keys)));
  366. /* Now we have gathered a set of 1..5 keys to write back. */
  367. for (i = 0; i < nk; i++) {
  368. w = keys[i];
  369. io = kzalloc(sizeof(struct dirty_io) +
  370. sizeof(struct bio_vec) *
  371. DIV_ROUND_UP(KEY_SIZE(&w->key),
  372. PAGE_SECTORS),
  373. GFP_KERNEL);
  374. if (!io)
  375. goto err;
  376. w->private = io;
  377. io->dc = dc;
  378. io->sequence = sequence++;
  379. dirty_init(w);
  380. bio_set_op_attrs(&io->bio, REQ_OP_READ, 0);
  381. io->bio.bi_iter.bi_sector = PTR_OFFSET(&w->key, 0);
  382. bio_set_dev(&io->bio,
  383. PTR_CACHE(dc->disk.c, &w->key, 0)->bdev);
  384. io->bio.bi_end_io = read_dirty_endio;
  385. if (bch_bio_alloc_pages(&io->bio, GFP_KERNEL))
  386. goto err_free;
  387. trace_bcache_writeback(&w->key);
  388. down(&dc->in_flight);
  389. /*
  390. * We've acquired a semaphore for the maximum
  391. * simultaneous number of writebacks; from here
  392. * everything happens asynchronously.
  393. */
  394. closure_call(&io->cl, read_dirty_submit, NULL, &cl);
  395. }
  396. delay = writeback_delay(dc, size);
  397. while (!kthread_should_stop() &&
  398. !test_bit(CACHE_SET_IO_DISABLE, &dc->disk.c->flags) &&
  399. delay) {
  400. schedule_timeout_interruptible(delay);
  401. delay = writeback_delay(dc, 0);
  402. }
  403. }
  404. if (0) {
  405. err_free:
  406. kfree(w->private);
  407. err:
  408. bch_keybuf_del(&dc->writeback_keys, w);
  409. }
  410. /*
  411. * Wait for outstanding writeback IOs to finish (and keybuf slots to be
  412. * freed) before refilling again
  413. */
  414. closure_sync(&cl);
  415. }
  416. /* Scan for dirty data */
  417. void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned int inode,
  418. uint64_t offset, int nr_sectors)
  419. {
  420. struct bcache_device *d = c->devices[inode];
  421. unsigned int stripe_offset, stripe, sectors_dirty;
  422. if (!d)
  423. return;
  424. if (UUID_FLASH_ONLY(&c->uuids[inode]))
  425. atomic_long_add(nr_sectors, &c->flash_dev_dirty_sectors);
  426. stripe = offset_to_stripe(d, offset);
  427. stripe_offset = offset & (d->stripe_size - 1);
  428. while (nr_sectors) {
  429. int s = min_t(unsigned int, abs(nr_sectors),
  430. d->stripe_size - stripe_offset);
  431. if (nr_sectors < 0)
  432. s = -s;
  433. if (stripe >= d->nr_stripes)
  434. return;
  435. sectors_dirty = atomic_add_return(s,
  436. d->stripe_sectors_dirty + stripe);
  437. if (sectors_dirty == d->stripe_size)
  438. set_bit(stripe, d->full_dirty_stripes);
  439. else
  440. clear_bit(stripe, d->full_dirty_stripes);
  441. nr_sectors -= s;
  442. stripe_offset = 0;
  443. stripe++;
  444. }
  445. }
  446. static bool dirty_pred(struct keybuf *buf, struct bkey *k)
  447. {
  448. struct cached_dev *dc = container_of(buf,
  449. struct cached_dev,
  450. writeback_keys);
  451. BUG_ON(KEY_INODE(k) != dc->disk.id);
  452. return KEY_DIRTY(k);
  453. }
  454. static void refill_full_stripes(struct cached_dev *dc)
  455. {
  456. struct keybuf *buf = &dc->writeback_keys;
  457. unsigned int start_stripe, stripe, next_stripe;
  458. bool wrapped = false;
  459. stripe = offset_to_stripe(&dc->disk, KEY_OFFSET(&buf->last_scanned));
  460. if (stripe >= dc->disk.nr_stripes)
  461. stripe = 0;
  462. start_stripe = stripe;
  463. while (1) {
  464. stripe = find_next_bit(dc->disk.full_dirty_stripes,
  465. dc->disk.nr_stripes, stripe);
  466. if (stripe == dc->disk.nr_stripes)
  467. goto next;
  468. next_stripe = find_next_zero_bit(dc->disk.full_dirty_stripes,
  469. dc->disk.nr_stripes, stripe);
  470. buf->last_scanned = KEY(dc->disk.id,
  471. stripe * dc->disk.stripe_size, 0);
  472. bch_refill_keybuf(dc->disk.c, buf,
  473. &KEY(dc->disk.id,
  474. next_stripe * dc->disk.stripe_size, 0),
  475. dirty_pred);
  476. if (array_freelist_empty(&buf->freelist))
  477. return;
  478. stripe = next_stripe;
  479. next:
  480. if (wrapped && stripe > start_stripe)
  481. return;
  482. if (stripe == dc->disk.nr_stripes) {
  483. stripe = 0;
  484. wrapped = true;
  485. }
  486. }
  487. }
  488. /*
  489. * Returns true if we scanned the entire disk
  490. */
  491. static bool refill_dirty(struct cached_dev *dc)
  492. {
  493. struct keybuf *buf = &dc->writeback_keys;
  494. struct bkey start = KEY(dc->disk.id, 0, 0);
  495. struct bkey end = KEY(dc->disk.id, MAX_KEY_OFFSET, 0);
  496. struct bkey start_pos;
  497. /*
  498. * make sure keybuf pos is inside the range for this disk - at bringup
  499. * we might not be attached yet so this disk's inode nr isn't
  500. * initialized then
  501. */
  502. if (bkey_cmp(&buf->last_scanned, &start) < 0 ||
  503. bkey_cmp(&buf->last_scanned, &end) > 0)
  504. buf->last_scanned = start;
  505. if (dc->partial_stripes_expensive) {
  506. refill_full_stripes(dc);
  507. if (array_freelist_empty(&buf->freelist))
  508. return false;
  509. }
  510. start_pos = buf->last_scanned;
  511. bch_refill_keybuf(dc->disk.c, buf, &end, dirty_pred);
  512. if (bkey_cmp(&buf->last_scanned, &end) < 0)
  513. return false;
  514. /*
  515. * If we get to the end start scanning again from the beginning, and
  516. * only scan up to where we initially started scanning from:
  517. */
  518. buf->last_scanned = start;
  519. bch_refill_keybuf(dc->disk.c, buf, &start_pos, dirty_pred);
  520. return bkey_cmp(&buf->last_scanned, &start_pos) >= 0;
  521. }
  522. static int bch_writeback_thread(void *arg)
  523. {
  524. struct cached_dev *dc = arg;
  525. struct cache_set *c = dc->disk.c;
  526. bool searched_full_index;
  527. bch_ratelimit_reset(&dc->writeback_rate);
  528. while (!kthread_should_stop() &&
  529. !test_bit(CACHE_SET_IO_DISABLE, &c->flags)) {
  530. down_write(&dc->writeback_lock);
  531. set_current_state(TASK_INTERRUPTIBLE);
  532. /*
  533. * If the bache device is detaching, skip here and continue
  534. * to perform writeback. Otherwise, if no dirty data on cache,
  535. * or there is dirty data on cache but writeback is disabled,
  536. * the writeback thread should sleep here and wait for others
  537. * to wake up it.
  538. */
  539. if (!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) &&
  540. (!atomic_read(&dc->has_dirty) || !dc->writeback_running)) {
  541. up_write(&dc->writeback_lock);
  542. if (kthread_should_stop() ||
  543. test_bit(CACHE_SET_IO_DISABLE, &c->flags)) {
  544. set_current_state(TASK_RUNNING);
  545. break;
  546. }
  547. schedule();
  548. continue;
  549. }
  550. set_current_state(TASK_RUNNING);
  551. searched_full_index = refill_dirty(dc);
  552. if (searched_full_index &&
  553. RB_EMPTY_ROOT(&dc->writeback_keys.keys)) {
  554. atomic_set(&dc->has_dirty, 0);
  555. SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
  556. bch_write_bdev_super(dc, NULL);
  557. /*
  558. * If bcache device is detaching via sysfs interface,
  559. * writeback thread should stop after there is no dirty
  560. * data on cache. BCACHE_DEV_DETACHING flag is set in
  561. * bch_cached_dev_detach().
  562. */
  563. if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) {
  564. up_write(&dc->writeback_lock);
  565. break;
  566. }
  567. }
  568. up_write(&dc->writeback_lock);
  569. read_dirty(dc);
  570. if (searched_full_index) {
  571. unsigned int delay = dc->writeback_delay * HZ;
  572. while (delay &&
  573. !kthread_should_stop() &&
  574. !test_bit(CACHE_SET_IO_DISABLE, &c->flags) &&
  575. !test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
  576. delay = schedule_timeout_interruptible(delay);
  577. bch_ratelimit_reset(&dc->writeback_rate);
  578. }
  579. }
  580. if (dc->writeback_write_wq) {
  581. flush_workqueue(dc->writeback_write_wq);
  582. destroy_workqueue(dc->writeback_write_wq);
  583. }
  584. cached_dev_put(dc);
  585. wait_for_kthread_stop();
  586. return 0;
  587. }
  588. /* Init */
  589. #define INIT_KEYS_EACH_TIME 500000
  590. #define INIT_KEYS_SLEEP_MS 100
  591. struct sectors_dirty_init {
  592. struct btree_op op;
  593. unsigned int inode;
  594. size_t count;
  595. struct bkey start;
  596. };
  597. static int sectors_dirty_init_fn(struct btree_op *_op, struct btree *b,
  598. struct bkey *k)
  599. {
  600. struct sectors_dirty_init *op = container_of(_op,
  601. struct sectors_dirty_init, op);
  602. if (KEY_INODE(k) > op->inode)
  603. return MAP_DONE;
  604. if (KEY_DIRTY(k))
  605. bcache_dev_sectors_dirty_add(b->c, KEY_INODE(k),
  606. KEY_START(k), KEY_SIZE(k));
  607. op->count++;
  608. if (atomic_read(&b->c->search_inflight) &&
  609. !(op->count % INIT_KEYS_EACH_TIME)) {
  610. bkey_copy_key(&op->start, k);
  611. return -EAGAIN;
  612. }
  613. return MAP_CONTINUE;
  614. }
  615. void bch_sectors_dirty_init(struct bcache_device *d)
  616. {
  617. struct sectors_dirty_init op;
  618. int ret;
  619. bch_btree_op_init(&op.op, -1);
  620. op.inode = d->id;
  621. op.count = 0;
  622. op.start = KEY(op.inode, 0, 0);
  623. do {
  624. ret = bch_btree_map_keys(&op.op, d->c, &op.start,
  625. sectors_dirty_init_fn, 0);
  626. if (ret == -EAGAIN)
  627. schedule_timeout_interruptible(
  628. msecs_to_jiffies(INIT_KEYS_SLEEP_MS));
  629. else if (ret < 0) {
  630. pr_warn("sectors dirty init failed, ret=%d!", ret);
  631. break;
  632. }
  633. } while (ret == -EAGAIN);
  634. }
  635. void bch_cached_dev_writeback_init(struct cached_dev *dc)
  636. {
  637. sema_init(&dc->in_flight, 64);
  638. init_rwsem(&dc->writeback_lock);
  639. bch_keybuf_init(&dc->writeback_keys);
  640. dc->writeback_metadata = true;
  641. dc->writeback_running = false;
  642. dc->writeback_percent = 10;
  643. dc->writeback_delay = 30;
  644. atomic_long_set(&dc->writeback_rate.rate, 1024);
  645. dc->writeback_rate_minimum = 8;
  646. dc->writeback_rate_update_seconds = WRITEBACK_RATE_UPDATE_SECS_DEFAULT;
  647. dc->writeback_rate_p_term_inverse = 40;
  648. dc->writeback_rate_i_term_inverse = 10000;
  649. WARN_ON(test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags));
  650. INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate);
  651. }
  652. int bch_cached_dev_writeback_start(struct cached_dev *dc)
  653. {
  654. dc->writeback_write_wq = alloc_workqueue("bcache_writeback_wq",
  655. WQ_MEM_RECLAIM, 0);
  656. if (!dc->writeback_write_wq)
  657. return -ENOMEM;
  658. cached_dev_get(dc);
  659. dc->writeback_thread = kthread_create(bch_writeback_thread, dc,
  660. "bcache_writeback");
  661. if (IS_ERR(dc->writeback_thread)) {
  662. cached_dev_put(dc);
  663. destroy_workqueue(dc->writeback_write_wq);
  664. return PTR_ERR(dc->writeback_thread);
  665. }
  666. dc->writeback_running = true;
  667. WARN_ON(test_and_set_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags));
  668. schedule_delayed_work(&dc->writeback_rate_update,
  669. dc->writeback_rate_update_seconds * HZ);
  670. bch_writeback_queue(dc);
  671. return 0;
  672. }