writeback.c 12 KB

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