alloc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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/random.h>
  65. #define MAX_IN_FLIGHT_DISCARDS 8U
  66. /* Bucket heap / gen */
  67. uint8_t bch_inc_gen(struct cache *ca, struct bucket *b)
  68. {
  69. uint8_t ret = ++b->gen;
  70. ca->set->need_gc = max(ca->set->need_gc, bucket_gc_gen(b));
  71. WARN_ON_ONCE(ca->set->need_gc > BUCKET_GC_GEN_MAX);
  72. if (CACHE_SYNC(&ca->set->sb)) {
  73. ca->need_save_prio = max(ca->need_save_prio,
  74. bucket_disk_gen(b));
  75. WARN_ON_ONCE(ca->need_save_prio > BUCKET_DISK_GEN_MAX);
  76. }
  77. return ret;
  78. }
  79. void bch_rescale_priorities(struct cache_set *c, int sectors)
  80. {
  81. struct cache *ca;
  82. struct bucket *b;
  83. unsigned next = c->nbuckets * c->sb.bucket_size / 1024;
  84. unsigned i;
  85. int r;
  86. atomic_sub(sectors, &c->rescale);
  87. do {
  88. r = atomic_read(&c->rescale);
  89. if (r >= 0)
  90. return;
  91. } while (atomic_cmpxchg(&c->rescale, r, r + next) != r);
  92. mutex_lock(&c->bucket_lock);
  93. c->min_prio = USHRT_MAX;
  94. for_each_cache(ca, c, i)
  95. for_each_bucket(b, ca)
  96. if (b->prio &&
  97. b->prio != BTREE_PRIO &&
  98. !atomic_read(&b->pin)) {
  99. b->prio--;
  100. c->min_prio = min(c->min_prio, b->prio);
  101. }
  102. mutex_unlock(&c->bucket_lock);
  103. }
  104. /* Discard/TRIM */
  105. struct discard {
  106. struct list_head list;
  107. struct work_struct work;
  108. struct cache *ca;
  109. long bucket;
  110. struct bio bio;
  111. struct bio_vec bv;
  112. };
  113. static void discard_finish(struct work_struct *w)
  114. {
  115. struct discard *d = container_of(w, struct discard, work);
  116. struct cache *ca = d->ca;
  117. char buf[BDEVNAME_SIZE];
  118. if (!test_bit(BIO_UPTODATE, &d->bio.bi_flags)) {
  119. pr_notice("discard error on %s, disabling",
  120. bdevname(ca->bdev, buf));
  121. d->ca->discard = 0;
  122. }
  123. mutex_lock(&ca->set->bucket_lock);
  124. fifo_push(&ca->free, d->bucket);
  125. list_add(&d->list, &ca->discards);
  126. atomic_dec(&ca->discards_in_flight);
  127. mutex_unlock(&ca->set->bucket_lock);
  128. closure_wake_up(&ca->set->bucket_wait);
  129. wake_up(&ca->set->alloc_wait);
  130. closure_put(&ca->set->cl);
  131. }
  132. static void discard_endio(struct bio *bio, int error)
  133. {
  134. struct discard *d = container_of(bio, struct discard, bio);
  135. schedule_work(&d->work);
  136. }
  137. static void do_discard(struct cache *ca, long bucket)
  138. {
  139. struct discard *d = list_first_entry(&ca->discards,
  140. struct discard, list);
  141. list_del(&d->list);
  142. d->bucket = bucket;
  143. atomic_inc(&ca->discards_in_flight);
  144. closure_get(&ca->set->cl);
  145. bio_init(&d->bio);
  146. d->bio.bi_sector = bucket_to_sector(ca->set, d->bucket);
  147. d->bio.bi_bdev = ca->bdev;
  148. d->bio.bi_rw = REQ_WRITE|REQ_DISCARD;
  149. d->bio.bi_max_vecs = 1;
  150. d->bio.bi_io_vec = d->bio.bi_inline_vecs;
  151. d->bio.bi_size = bucket_bytes(ca);
  152. d->bio.bi_end_io = discard_endio;
  153. bio_set_prio(&d->bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
  154. submit_bio(0, &d->bio);
  155. }
  156. /* Allocation */
  157. static inline bool can_inc_bucket_gen(struct bucket *b)
  158. {
  159. return bucket_gc_gen(b) < BUCKET_GC_GEN_MAX &&
  160. bucket_disk_gen(b) < BUCKET_DISK_GEN_MAX;
  161. }
  162. bool bch_bucket_add_unused(struct cache *ca, struct bucket *b)
  163. {
  164. BUG_ON(GC_MARK(b) || GC_SECTORS_USED(b));
  165. if (fifo_used(&ca->free) > ca->watermark[WATERMARK_MOVINGGC] &&
  166. CACHE_REPLACEMENT(&ca->sb) == CACHE_REPLACEMENT_FIFO)
  167. return false;
  168. b->prio = 0;
  169. if (can_inc_bucket_gen(b) &&
  170. fifo_push(&ca->unused, b - ca->buckets)) {
  171. atomic_inc(&b->pin);
  172. return true;
  173. }
  174. return false;
  175. }
  176. static bool can_invalidate_bucket(struct cache *ca, struct bucket *b)
  177. {
  178. return GC_MARK(b) == GC_MARK_RECLAIMABLE &&
  179. !atomic_read(&b->pin) &&
  180. can_inc_bucket_gen(b);
  181. }
  182. static void invalidate_one_bucket(struct cache *ca, struct bucket *b)
  183. {
  184. bch_inc_gen(ca, b);
  185. b->prio = INITIAL_PRIO;
  186. atomic_inc(&b->pin);
  187. fifo_push(&ca->free_inc, b - ca->buckets);
  188. }
  189. #define bucket_prio(b) \
  190. (((unsigned) (b->prio - ca->set->min_prio)) * GC_SECTORS_USED(b))
  191. #define bucket_max_cmp(l, r) (bucket_prio(l) < bucket_prio(r))
  192. #define bucket_min_cmp(l, r) (bucket_prio(l) > bucket_prio(r))
  193. static void invalidate_buckets_lru(struct cache *ca)
  194. {
  195. struct bucket *b;
  196. ssize_t i;
  197. ca->heap.used = 0;
  198. for_each_bucket(b, ca) {
  199. /*
  200. * If we fill up the unused list, if we then return before
  201. * adding anything to the free_inc list we'll skip writing
  202. * prios/gens and just go back to allocating from the unused
  203. * list:
  204. */
  205. if (fifo_full(&ca->unused))
  206. return;
  207. if (!can_invalidate_bucket(ca, b))
  208. continue;
  209. if (!GC_SECTORS_USED(b) &&
  210. bch_bucket_add_unused(ca, b))
  211. continue;
  212. if (!heap_full(&ca->heap))
  213. heap_add(&ca->heap, b, bucket_max_cmp);
  214. else if (bucket_max_cmp(b, heap_peek(&ca->heap))) {
  215. ca->heap.data[0] = b;
  216. heap_sift(&ca->heap, 0, bucket_max_cmp);
  217. }
  218. }
  219. for (i = ca->heap.used / 2 - 1; i >= 0; --i)
  220. heap_sift(&ca->heap, i, bucket_min_cmp);
  221. while (!fifo_full(&ca->free_inc)) {
  222. if (!heap_pop(&ca->heap, b, bucket_min_cmp)) {
  223. /*
  224. * We don't want to be calling invalidate_buckets()
  225. * multiple times when it can't do anything
  226. */
  227. ca->invalidate_needs_gc = 1;
  228. bch_queue_gc(ca->set);
  229. return;
  230. }
  231. invalidate_one_bucket(ca, b);
  232. }
  233. }
  234. static void invalidate_buckets_fifo(struct cache *ca)
  235. {
  236. struct bucket *b;
  237. size_t checked = 0;
  238. while (!fifo_full(&ca->free_inc)) {
  239. if (ca->fifo_last_bucket < ca->sb.first_bucket ||
  240. ca->fifo_last_bucket >= ca->sb.nbuckets)
  241. ca->fifo_last_bucket = ca->sb.first_bucket;
  242. b = ca->buckets + ca->fifo_last_bucket++;
  243. if (can_invalidate_bucket(ca, b))
  244. invalidate_one_bucket(ca, b);
  245. if (++checked >= ca->sb.nbuckets) {
  246. ca->invalidate_needs_gc = 1;
  247. bch_queue_gc(ca->set);
  248. return;
  249. }
  250. }
  251. }
  252. static void invalidate_buckets_random(struct cache *ca)
  253. {
  254. struct bucket *b;
  255. size_t checked = 0;
  256. while (!fifo_full(&ca->free_inc)) {
  257. size_t n;
  258. get_random_bytes(&n, sizeof(n));
  259. n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
  260. n += ca->sb.first_bucket;
  261. b = ca->buckets + n;
  262. if (can_invalidate_bucket(ca, b))
  263. invalidate_one_bucket(ca, b);
  264. if (++checked >= ca->sb.nbuckets / 2) {
  265. ca->invalidate_needs_gc = 1;
  266. bch_queue_gc(ca->set);
  267. return;
  268. }
  269. }
  270. }
  271. static void invalidate_buckets(struct cache *ca)
  272. {
  273. if (ca->invalidate_needs_gc)
  274. return;
  275. switch (CACHE_REPLACEMENT(&ca->sb)) {
  276. case CACHE_REPLACEMENT_LRU:
  277. invalidate_buckets_lru(ca);
  278. break;
  279. case CACHE_REPLACEMENT_FIFO:
  280. invalidate_buckets_fifo(ca);
  281. break;
  282. case CACHE_REPLACEMENT_RANDOM:
  283. invalidate_buckets_random(ca);
  284. break;
  285. }
  286. pr_debug("free %zu/%zu free_inc %zu/%zu unused %zu/%zu",
  287. fifo_used(&ca->free), ca->free.size,
  288. fifo_used(&ca->free_inc), ca->free_inc.size,
  289. fifo_used(&ca->unused), ca->unused.size);
  290. }
  291. #define allocator_wait(ca, cond) \
  292. do { \
  293. DEFINE_WAIT(__wait); \
  294. \
  295. while (1) { \
  296. prepare_to_wait(&ca->set->alloc_wait, \
  297. &__wait, TASK_INTERRUPTIBLE); \
  298. if (cond) \
  299. break; \
  300. \
  301. mutex_unlock(&(ca)->set->bucket_lock); \
  302. if (test_bit(CACHE_SET_STOPPING_2, &ca->set->flags)) { \
  303. finish_wait(&ca->set->alloc_wait, &__wait); \
  304. closure_return(cl); \
  305. } \
  306. \
  307. schedule(); \
  308. mutex_lock(&(ca)->set->bucket_lock); \
  309. } \
  310. \
  311. finish_wait(&ca->set->alloc_wait, &__wait); \
  312. } while (0)
  313. void bch_allocator_thread(struct closure *cl)
  314. {
  315. struct cache *ca = container_of(cl, struct cache, alloc);
  316. mutex_lock(&ca->set->bucket_lock);
  317. while (1) {
  318. /*
  319. * First, we pull buckets off of the unused and free_inc lists,
  320. * possibly issue discards to them, then we add the bucket to
  321. * the free list:
  322. */
  323. while (1) {
  324. long bucket;
  325. if ((!atomic_read(&ca->set->prio_blocked) ||
  326. !CACHE_SYNC(&ca->set->sb)) &&
  327. !fifo_empty(&ca->unused))
  328. fifo_pop(&ca->unused, bucket);
  329. else if (!fifo_empty(&ca->free_inc))
  330. fifo_pop(&ca->free_inc, bucket);
  331. else
  332. break;
  333. allocator_wait(ca, (int) fifo_free(&ca->free) >
  334. atomic_read(&ca->discards_in_flight));
  335. if (ca->discard) {
  336. allocator_wait(ca, !list_empty(&ca->discards));
  337. do_discard(ca, bucket);
  338. } else {
  339. fifo_push(&ca->free, bucket);
  340. closure_wake_up(&ca->set->bucket_wait);
  341. }
  342. }
  343. /*
  344. * We've run out of free buckets, we need to find some buckets
  345. * we can invalidate. First, invalidate them in memory and add
  346. * them to the free_inc list:
  347. */
  348. allocator_wait(ca, ca->set->gc_mark_valid &&
  349. (ca->need_save_prio > 64 ||
  350. !ca->invalidate_needs_gc));
  351. invalidate_buckets(ca);
  352. /*
  353. * Now, we write their new gens to disk so we can start writing
  354. * new stuff to them:
  355. */
  356. allocator_wait(ca, !atomic_read(&ca->set->prio_blocked));
  357. if (CACHE_SYNC(&ca->set->sb) &&
  358. (!fifo_empty(&ca->free_inc) ||
  359. ca->need_save_prio > 64))
  360. bch_prio_write(ca);
  361. }
  362. }
  363. long bch_bucket_alloc(struct cache *ca, unsigned watermark, struct closure *cl)
  364. {
  365. long r = -1;
  366. again:
  367. wake_up(&ca->set->alloc_wait);
  368. if (fifo_used(&ca->free) > ca->watermark[watermark] &&
  369. fifo_pop(&ca->free, r)) {
  370. struct bucket *b = ca->buckets + r;
  371. #ifdef CONFIG_BCACHE_EDEBUG
  372. size_t iter;
  373. long i;
  374. for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
  375. BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
  376. fifo_for_each(i, &ca->free, iter)
  377. BUG_ON(i == r);
  378. fifo_for_each(i, &ca->free_inc, iter)
  379. BUG_ON(i == r);
  380. fifo_for_each(i, &ca->unused, iter)
  381. BUG_ON(i == r);
  382. #endif
  383. BUG_ON(atomic_read(&b->pin) != 1);
  384. SET_GC_SECTORS_USED(b, ca->sb.bucket_size);
  385. if (watermark <= WATERMARK_METADATA) {
  386. SET_GC_MARK(b, GC_MARK_METADATA);
  387. b->prio = BTREE_PRIO;
  388. } else {
  389. SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
  390. b->prio = INITIAL_PRIO;
  391. }
  392. return r;
  393. }
  394. pr_debug("alloc failure: blocked %i free %zu free_inc %zu unused %zu",
  395. atomic_read(&ca->set->prio_blocked), fifo_used(&ca->free),
  396. fifo_used(&ca->free_inc), fifo_used(&ca->unused));
  397. if (cl) {
  398. closure_wait(&ca->set->bucket_wait, cl);
  399. if (closure_blocking(cl)) {
  400. mutex_unlock(&ca->set->bucket_lock);
  401. closure_sync(cl);
  402. mutex_lock(&ca->set->bucket_lock);
  403. goto again;
  404. }
  405. }
  406. return -1;
  407. }
  408. void bch_bucket_free(struct cache_set *c, struct bkey *k)
  409. {
  410. unsigned i;
  411. for (i = 0; i < KEY_PTRS(k); i++) {
  412. struct bucket *b = PTR_BUCKET(c, k, i);
  413. SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
  414. SET_GC_SECTORS_USED(b, 0);
  415. bch_bucket_add_unused(PTR_CACHE(c, k, i), b);
  416. }
  417. }
  418. int __bch_bucket_alloc_set(struct cache_set *c, unsigned watermark,
  419. struct bkey *k, int n, struct closure *cl)
  420. {
  421. int i;
  422. lockdep_assert_held(&c->bucket_lock);
  423. BUG_ON(!n || n > c->caches_loaded || n > 8);
  424. bkey_init(k);
  425. /* sort by free space/prio of oldest data in caches */
  426. for (i = 0; i < n; i++) {
  427. struct cache *ca = c->cache_by_alloc[i];
  428. long b = bch_bucket_alloc(ca, watermark, cl);
  429. if (b == -1)
  430. goto err;
  431. k->ptr[i] = PTR(ca->buckets[b].gen,
  432. bucket_to_sector(c, b),
  433. ca->sb.nr_this_dev);
  434. SET_KEY_PTRS(k, i + 1);
  435. }
  436. return 0;
  437. err:
  438. bch_bucket_free(c, k);
  439. __bkey_put(c, k);
  440. return -1;
  441. }
  442. int bch_bucket_alloc_set(struct cache_set *c, unsigned watermark,
  443. struct bkey *k, int n, struct closure *cl)
  444. {
  445. int ret;
  446. mutex_lock(&c->bucket_lock);
  447. ret = __bch_bucket_alloc_set(c, watermark, k, n, cl);
  448. mutex_unlock(&c->bucket_lock);
  449. return ret;
  450. }
  451. /* Init */
  452. void bch_cache_allocator_exit(struct cache *ca)
  453. {
  454. struct discard *d;
  455. while (!list_empty(&ca->discards)) {
  456. d = list_first_entry(&ca->discards, struct discard, list);
  457. cancel_work_sync(&d->work);
  458. list_del(&d->list);
  459. kfree(d);
  460. }
  461. }
  462. int bch_cache_allocator_init(struct cache *ca)
  463. {
  464. unsigned i;
  465. /*
  466. * Reserve:
  467. * Prio/gen writes first
  468. * Then 8 for btree allocations
  469. * Then half for the moving garbage collector
  470. */
  471. ca->watermark[WATERMARK_PRIO] = 0;
  472. ca->watermark[WATERMARK_METADATA] = prio_buckets(ca);
  473. ca->watermark[WATERMARK_MOVINGGC] = 8 +
  474. ca->watermark[WATERMARK_METADATA];
  475. ca->watermark[WATERMARK_NONE] = ca->free.size / 2 +
  476. ca->watermark[WATERMARK_MOVINGGC];
  477. for (i = 0; i < MAX_IN_FLIGHT_DISCARDS; i++) {
  478. struct discard *d = kzalloc(sizeof(*d), GFP_KERNEL);
  479. if (!d)
  480. return -ENOMEM;
  481. d->ca = ca;
  482. INIT_WORK(&d->work, discard_finish);
  483. list_add(&d->list, &ca->discards);
  484. }
  485. return 0;
  486. }