alloc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Primary bucket allocation code
  3. *
  4. * Copyright 2012 Google, Inc.
  5. *
  6. * Allocation in bcache is done in terms of buckets:
  7. *
  8. * Each bucket has associated an 8 bit gen; this gen corresponds to the gen in
  9. * btree pointers - they must match for the pointer to be considered valid.
  10. *
  11. * Thus (assuming a bucket has no dirty data or metadata in it) we can reuse a
  12. * bucket simply by incrementing its gen.
  13. *
  14. * The gens (along with the priorities; it's really the gens are important but
  15. * the code is named as if it's the priorities) are written in an arbitrary list
  16. * of buckets on disk, with a pointer to them in the journal header.
  17. *
  18. * When we invalidate a bucket, we have to write its new gen to disk and wait
  19. * for that write to complete before we use it - otherwise after a crash we
  20. * could have pointers that appeared to be good but pointed to data that had
  21. * been overwritten.
  22. *
  23. * Since the gens and priorities are all stored contiguously on disk, we can
  24. * batch this up: We fill up the free_inc list with freshly invalidated buckets,
  25. * call prio_write(), and when prio_write() finishes we pull buckets off the
  26. * free_inc list and optionally discard them.
  27. *
  28. * free_inc isn't the only freelist - if it was, we'd often to sleep while
  29. * priorities and gens were being written before we could allocate. c->free is a
  30. * smaller freelist, and buckets on that list are always ready to be used.
  31. *
  32. * If we've got discards enabled, that happens when a bucket moves from the
  33. * free_inc list to the free list.
  34. *
  35. * There is another freelist, because sometimes we have buckets that we know
  36. * have nothing pointing into them - these we can reuse without waiting for
  37. * priorities to be rewritten. These come from freed btree nodes and buckets
  38. * that garbage collection discovered no longer had valid keys pointing into
  39. * them (because they were overwritten). That's the unused list - buckets on the
  40. * unused list move to the free list, optionally being discarded in the process.
  41. *
  42. * It's also important to ensure that gens don't wrap around - with respect to
  43. * either the oldest gen in the btree or the gen on disk. This is quite
  44. * difficult to do in practice, but we explicitly guard against it anyways - if
  45. * a bucket is in danger of wrapping around we simply skip invalidating it that
  46. * time around, and we garbage collect or rewrite the priorities sooner than we
  47. * would have otherwise.
  48. *
  49. * bch_bucket_alloc() allocates a single bucket from a specific cache.
  50. *
  51. * bch_bucket_alloc_set() allocates one or more buckets from different caches
  52. * out of a cache set.
  53. *
  54. * free_some_buckets() drives all the processes described above. It's called
  55. * from bch_bucket_alloc() and a few other places that need to make sure free
  56. * buckets are ready.
  57. *
  58. * invalidate_buckets_(lru|fifo)() find buckets that are available to be
  59. * invalidated, and then invalidate them and stick them on the free_inc list -
  60. * in either lru or fifo order.
  61. */
  62. #include "bcache.h"
  63. #include "btree.h"
  64. #include <linux/freezer.h>
  65. #include <linux/kthread.h>
  66. #include <linux/random.h>
  67. #include <trace/events/bcache.h>
  68. #define MAX_IN_FLIGHT_DISCARDS 8U
  69. /* Bucket heap / gen */
  70. uint8_t bch_inc_gen(struct cache *ca, struct bucket *b)
  71. {
  72. uint8_t ret = ++b->gen;
  73. ca->set->need_gc = max(ca->set->need_gc, bucket_gc_gen(b));
  74. WARN_ON_ONCE(ca->set->need_gc > BUCKET_GC_GEN_MAX);
  75. if (CACHE_SYNC(&ca->set->sb)) {
  76. ca->need_save_prio = max(ca->need_save_prio,
  77. bucket_disk_gen(b));
  78. WARN_ON_ONCE(ca->need_save_prio > BUCKET_DISK_GEN_MAX);
  79. }
  80. return ret;
  81. }
  82. void bch_rescale_priorities(struct cache_set *c, int sectors)
  83. {
  84. struct cache *ca;
  85. struct bucket *b;
  86. unsigned next = c->nbuckets * c->sb.bucket_size / 1024;
  87. unsigned i;
  88. int r;
  89. atomic_sub(sectors, &c->rescale);
  90. do {
  91. r = atomic_read(&c->rescale);
  92. if (r >= 0)
  93. return;
  94. } while (atomic_cmpxchg(&c->rescale, r, r + next) != r);
  95. mutex_lock(&c->bucket_lock);
  96. c->min_prio = USHRT_MAX;
  97. for_each_cache(ca, c, i)
  98. for_each_bucket(b, ca)
  99. if (b->prio &&
  100. b->prio != BTREE_PRIO &&
  101. !atomic_read(&b->pin)) {
  102. b->prio--;
  103. c->min_prio = min(c->min_prio, b->prio);
  104. }
  105. mutex_unlock(&c->bucket_lock);
  106. }
  107. /* Discard/TRIM */
  108. struct discard {
  109. struct list_head list;
  110. struct work_struct work;
  111. struct cache *ca;
  112. long bucket;
  113. struct bio bio;
  114. struct bio_vec bv;
  115. };
  116. static void discard_finish(struct work_struct *w)
  117. {
  118. struct discard *d = container_of(w, struct discard, work);
  119. struct cache *ca = d->ca;
  120. char buf[BDEVNAME_SIZE];
  121. if (!test_bit(BIO_UPTODATE, &d->bio.bi_flags)) {
  122. pr_notice("discard error on %s, disabling",
  123. bdevname(ca->bdev, buf));
  124. d->ca->discard = 0;
  125. }
  126. mutex_lock(&ca->set->bucket_lock);
  127. fifo_push(&ca->free, d->bucket);
  128. list_add(&d->list, &ca->discards);
  129. atomic_dec(&ca->discards_in_flight);
  130. mutex_unlock(&ca->set->bucket_lock);
  131. closure_wake_up(&ca->set->bucket_wait);
  132. wake_up_process(ca->alloc_thread);
  133. closure_put(&ca->set->cl);
  134. }
  135. static void discard_endio(struct bio *bio, int error)
  136. {
  137. struct discard *d = container_of(bio, struct discard, bio);
  138. schedule_work(&d->work);
  139. }
  140. static void do_discard(struct cache *ca, long bucket)
  141. {
  142. struct discard *d = list_first_entry(&ca->discards,
  143. struct discard, list);
  144. list_del(&d->list);
  145. d->bucket = bucket;
  146. atomic_inc(&ca->discards_in_flight);
  147. closure_get(&ca->set->cl);
  148. bio_init(&d->bio);
  149. d->bio.bi_sector = bucket_to_sector(ca->set, d->bucket);
  150. d->bio.bi_bdev = ca->bdev;
  151. d->bio.bi_rw = REQ_WRITE|REQ_DISCARD;
  152. d->bio.bi_max_vecs = 1;
  153. d->bio.bi_io_vec = d->bio.bi_inline_vecs;
  154. d->bio.bi_size = bucket_bytes(ca);
  155. d->bio.bi_end_io = discard_endio;
  156. bio_set_prio(&d->bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
  157. submit_bio(0, &d->bio);
  158. }
  159. /* Allocation */
  160. static inline bool can_inc_bucket_gen(struct bucket *b)
  161. {
  162. return bucket_gc_gen(b) < BUCKET_GC_GEN_MAX &&
  163. bucket_disk_gen(b) < BUCKET_DISK_GEN_MAX;
  164. }
  165. bool bch_bucket_add_unused(struct cache *ca, struct bucket *b)
  166. {
  167. BUG_ON(GC_MARK(b) || GC_SECTORS_USED(b));
  168. if (fifo_used(&ca->free) > ca->watermark[WATERMARK_MOVINGGC] &&
  169. CACHE_REPLACEMENT(&ca->sb) == CACHE_REPLACEMENT_FIFO)
  170. return false;
  171. b->prio = 0;
  172. if (can_inc_bucket_gen(b) &&
  173. fifo_push(&ca->unused, b - ca->buckets)) {
  174. atomic_inc(&b->pin);
  175. return true;
  176. }
  177. return false;
  178. }
  179. static bool can_invalidate_bucket(struct cache *ca, struct bucket *b)
  180. {
  181. return GC_MARK(b) == GC_MARK_RECLAIMABLE &&
  182. !atomic_read(&b->pin) &&
  183. can_inc_bucket_gen(b);
  184. }
  185. static void invalidate_one_bucket(struct cache *ca, struct bucket *b)
  186. {
  187. bch_inc_gen(ca, b);
  188. b->prio = INITIAL_PRIO;
  189. atomic_inc(&b->pin);
  190. fifo_push(&ca->free_inc, b - ca->buckets);
  191. }
  192. #define bucket_prio(b) \
  193. (((unsigned) (b->prio - ca->set->min_prio)) * GC_SECTORS_USED(b))
  194. #define bucket_max_cmp(l, r) (bucket_prio(l) < bucket_prio(r))
  195. #define bucket_min_cmp(l, r) (bucket_prio(l) > bucket_prio(r))
  196. static void invalidate_buckets_lru(struct cache *ca)
  197. {
  198. struct bucket *b;
  199. ssize_t i;
  200. ca->heap.used = 0;
  201. for_each_bucket(b, ca) {
  202. /*
  203. * If we fill up the unused list, if we then return before
  204. * adding anything to the free_inc list we'll skip writing
  205. * prios/gens and just go back to allocating from the unused
  206. * list:
  207. */
  208. if (fifo_full(&ca->unused))
  209. return;
  210. if (!can_invalidate_bucket(ca, b))
  211. continue;
  212. if (!GC_SECTORS_USED(b) &&
  213. bch_bucket_add_unused(ca, b))
  214. continue;
  215. if (!heap_full(&ca->heap))
  216. heap_add(&ca->heap, b, bucket_max_cmp);
  217. else if (bucket_max_cmp(b, heap_peek(&ca->heap))) {
  218. ca->heap.data[0] = b;
  219. heap_sift(&ca->heap, 0, bucket_max_cmp);
  220. }
  221. }
  222. for (i = ca->heap.used / 2 - 1; i >= 0; --i)
  223. heap_sift(&ca->heap, i, bucket_min_cmp);
  224. while (!fifo_full(&ca->free_inc)) {
  225. if (!heap_pop(&ca->heap, b, bucket_min_cmp)) {
  226. /*
  227. * We don't want to be calling invalidate_buckets()
  228. * multiple times when it can't do anything
  229. */
  230. ca->invalidate_needs_gc = 1;
  231. bch_queue_gc(ca->set);
  232. return;
  233. }
  234. invalidate_one_bucket(ca, b);
  235. }
  236. }
  237. static void invalidate_buckets_fifo(struct cache *ca)
  238. {
  239. struct bucket *b;
  240. size_t checked = 0;
  241. while (!fifo_full(&ca->free_inc)) {
  242. if (ca->fifo_last_bucket < ca->sb.first_bucket ||
  243. ca->fifo_last_bucket >= ca->sb.nbuckets)
  244. ca->fifo_last_bucket = ca->sb.first_bucket;
  245. b = ca->buckets + ca->fifo_last_bucket++;
  246. if (can_invalidate_bucket(ca, b))
  247. invalidate_one_bucket(ca, b);
  248. if (++checked >= ca->sb.nbuckets) {
  249. ca->invalidate_needs_gc = 1;
  250. bch_queue_gc(ca->set);
  251. return;
  252. }
  253. }
  254. }
  255. static void invalidate_buckets_random(struct cache *ca)
  256. {
  257. struct bucket *b;
  258. size_t checked = 0;
  259. while (!fifo_full(&ca->free_inc)) {
  260. size_t n;
  261. get_random_bytes(&n, sizeof(n));
  262. n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
  263. n += ca->sb.first_bucket;
  264. b = ca->buckets + n;
  265. if (can_invalidate_bucket(ca, b))
  266. invalidate_one_bucket(ca, b);
  267. if (++checked >= ca->sb.nbuckets / 2) {
  268. ca->invalidate_needs_gc = 1;
  269. bch_queue_gc(ca->set);
  270. return;
  271. }
  272. }
  273. }
  274. static void invalidate_buckets(struct cache *ca)
  275. {
  276. if (ca->invalidate_needs_gc)
  277. return;
  278. switch (CACHE_REPLACEMENT(&ca->sb)) {
  279. case CACHE_REPLACEMENT_LRU:
  280. invalidate_buckets_lru(ca);
  281. break;
  282. case CACHE_REPLACEMENT_FIFO:
  283. invalidate_buckets_fifo(ca);
  284. break;
  285. case CACHE_REPLACEMENT_RANDOM:
  286. invalidate_buckets_random(ca);
  287. break;
  288. }
  289. trace_bcache_alloc_invalidate(ca);
  290. }
  291. #define allocator_wait(ca, cond) \
  292. do { \
  293. while (1) { \
  294. set_current_state(TASK_INTERRUPTIBLE); \
  295. if (cond) \
  296. break; \
  297. \
  298. mutex_unlock(&(ca)->set->bucket_lock); \
  299. if (kthread_should_stop()) \
  300. return 0; \
  301. \
  302. try_to_freeze(); \
  303. schedule(); \
  304. mutex_lock(&(ca)->set->bucket_lock); \
  305. } \
  306. __set_current_state(TASK_RUNNING); \
  307. } while (0)
  308. static int bch_allocator_thread(void *arg)
  309. {
  310. struct cache *ca = arg;
  311. mutex_lock(&ca->set->bucket_lock);
  312. while (1) {
  313. /*
  314. * First, we pull buckets off of the unused and free_inc lists,
  315. * possibly issue discards to them, then we add the bucket to
  316. * the free list:
  317. */
  318. while (1) {
  319. long bucket;
  320. if ((!atomic_read(&ca->set->prio_blocked) ||
  321. !CACHE_SYNC(&ca->set->sb)) &&
  322. !fifo_empty(&ca->unused))
  323. fifo_pop(&ca->unused, bucket);
  324. else if (!fifo_empty(&ca->free_inc))
  325. fifo_pop(&ca->free_inc, bucket);
  326. else
  327. break;
  328. allocator_wait(ca, (int) fifo_free(&ca->free) >
  329. atomic_read(&ca->discards_in_flight));
  330. if (ca->discard) {
  331. allocator_wait(ca, !list_empty(&ca->discards));
  332. do_discard(ca, bucket);
  333. } else {
  334. fifo_push(&ca->free, bucket);
  335. closure_wake_up(&ca->set->bucket_wait);
  336. }
  337. }
  338. /*
  339. * We've run out of free buckets, we need to find some buckets
  340. * we can invalidate. First, invalidate them in memory and add
  341. * them to the free_inc list:
  342. */
  343. allocator_wait(ca, ca->set->gc_mark_valid &&
  344. (ca->need_save_prio > 64 ||
  345. !ca->invalidate_needs_gc));
  346. invalidate_buckets(ca);
  347. /*
  348. * Now, we write their new gens to disk so we can start writing
  349. * new stuff to them:
  350. */
  351. allocator_wait(ca, !atomic_read(&ca->set->prio_blocked));
  352. if (CACHE_SYNC(&ca->set->sb) &&
  353. (!fifo_empty(&ca->free_inc) ||
  354. ca->need_save_prio > 64))
  355. bch_prio_write(ca);
  356. }
  357. }
  358. long bch_bucket_alloc(struct cache *ca, unsigned watermark, struct closure *cl)
  359. {
  360. long r = -1;
  361. again:
  362. wake_up_process(ca->alloc_thread);
  363. if (fifo_used(&ca->free) > ca->watermark[watermark] &&
  364. fifo_pop(&ca->free, r)) {
  365. struct bucket *b = ca->buckets + r;
  366. #ifdef CONFIG_BCACHE_EDEBUG
  367. size_t iter;
  368. long i;
  369. for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
  370. BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
  371. fifo_for_each(i, &ca->free, iter)
  372. BUG_ON(i == r);
  373. fifo_for_each(i, &ca->free_inc, iter)
  374. BUG_ON(i == r);
  375. fifo_for_each(i, &ca->unused, iter)
  376. BUG_ON(i == r);
  377. #endif
  378. BUG_ON(atomic_read(&b->pin) != 1);
  379. SET_GC_SECTORS_USED(b, ca->sb.bucket_size);
  380. if (watermark <= WATERMARK_METADATA) {
  381. SET_GC_MARK(b, GC_MARK_METADATA);
  382. b->prio = BTREE_PRIO;
  383. } else {
  384. SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
  385. b->prio = INITIAL_PRIO;
  386. }
  387. return r;
  388. }
  389. trace_bcache_alloc_fail(ca);
  390. if (cl) {
  391. closure_wait(&ca->set->bucket_wait, cl);
  392. if (closure_blocking(cl)) {
  393. mutex_unlock(&ca->set->bucket_lock);
  394. closure_sync(cl);
  395. mutex_lock(&ca->set->bucket_lock);
  396. goto again;
  397. }
  398. }
  399. return -1;
  400. }
  401. void bch_bucket_free(struct cache_set *c, struct bkey *k)
  402. {
  403. unsigned i;
  404. for (i = 0; i < KEY_PTRS(k); i++) {
  405. struct bucket *b = PTR_BUCKET(c, k, i);
  406. SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
  407. SET_GC_SECTORS_USED(b, 0);
  408. bch_bucket_add_unused(PTR_CACHE(c, k, i), b);
  409. }
  410. }
  411. int __bch_bucket_alloc_set(struct cache_set *c, unsigned watermark,
  412. struct bkey *k, int n, struct closure *cl)
  413. {
  414. int i;
  415. lockdep_assert_held(&c->bucket_lock);
  416. BUG_ON(!n || n > c->caches_loaded || n > 8);
  417. bkey_init(k);
  418. /* sort by free space/prio of oldest data in caches */
  419. for (i = 0; i < n; i++) {
  420. struct cache *ca = c->cache_by_alloc[i];
  421. long b = bch_bucket_alloc(ca, watermark, cl);
  422. if (b == -1)
  423. goto err;
  424. k->ptr[i] = PTR(ca->buckets[b].gen,
  425. bucket_to_sector(c, b),
  426. ca->sb.nr_this_dev);
  427. SET_KEY_PTRS(k, i + 1);
  428. }
  429. return 0;
  430. err:
  431. bch_bucket_free(c, k);
  432. __bkey_put(c, k);
  433. return -1;
  434. }
  435. int bch_bucket_alloc_set(struct cache_set *c, unsigned watermark,
  436. struct bkey *k, int n, struct closure *cl)
  437. {
  438. int ret;
  439. mutex_lock(&c->bucket_lock);
  440. ret = __bch_bucket_alloc_set(c, watermark, k, n, cl);
  441. mutex_unlock(&c->bucket_lock);
  442. return ret;
  443. }
  444. /* Init */
  445. int bch_cache_allocator_start(struct cache *ca)
  446. {
  447. struct task_struct *k = kthread_run(bch_allocator_thread,
  448. ca, "bcache_allocator");
  449. if (IS_ERR(k))
  450. return PTR_ERR(k);
  451. ca->alloc_thread = k;
  452. return 0;
  453. }
  454. void bch_cache_allocator_exit(struct cache *ca)
  455. {
  456. struct discard *d;
  457. while (!list_empty(&ca->discards)) {
  458. d = list_first_entry(&ca->discards, struct discard, list);
  459. cancel_work_sync(&d->work);
  460. list_del(&d->list);
  461. kfree(d);
  462. }
  463. }
  464. int bch_cache_allocator_init(struct cache *ca)
  465. {
  466. unsigned i;
  467. /*
  468. * Reserve:
  469. * Prio/gen writes first
  470. * Then 8 for btree allocations
  471. * Then half for the moving garbage collector
  472. */
  473. ca->watermark[WATERMARK_PRIO] = 0;
  474. ca->watermark[WATERMARK_METADATA] = prio_buckets(ca);
  475. ca->watermark[WATERMARK_MOVINGGC] = 8 +
  476. ca->watermark[WATERMARK_METADATA];
  477. ca->watermark[WATERMARK_NONE] = ca->free.size / 2 +
  478. ca->watermark[WATERMARK_MOVINGGC];
  479. for (i = 0; i < MAX_IN_FLIGHT_DISCARDS; i++) {
  480. struct discard *d = kzalloc(sizeof(*d), GFP_KERNEL);
  481. if (!d)
  482. return -ENOMEM;
  483. d->ca = ca;
  484. INIT_WORK(&d->work, discard_finish);
  485. list_add(&d->list, &ca->discards);
  486. }
  487. return 0;
  488. }