alloc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. if (!can_invalidate_bucket(ca, b))
  200. continue;
  201. if (!GC_SECTORS_USED(b)) {
  202. if (!bch_bucket_add_unused(ca, b))
  203. return;
  204. } else {
  205. if (!heap_full(&ca->heap))
  206. heap_add(&ca->heap, b, bucket_max_cmp);
  207. else if (bucket_max_cmp(b, heap_peek(&ca->heap))) {
  208. ca->heap.data[0] = b;
  209. heap_sift(&ca->heap, 0, bucket_max_cmp);
  210. }
  211. }
  212. }
  213. if (ca->heap.used * 2 < ca->heap.size)
  214. bch_queue_gc(ca->set);
  215. for (i = ca->heap.used / 2 - 1; i >= 0; --i)
  216. heap_sift(&ca->heap, i, bucket_min_cmp);
  217. while (!fifo_full(&ca->free_inc)) {
  218. if (!heap_pop(&ca->heap, b, bucket_min_cmp)) {
  219. /* We don't want to be calling invalidate_buckets()
  220. * multiple times when it can't do anything
  221. */
  222. ca->invalidate_needs_gc = 1;
  223. bch_queue_gc(ca->set);
  224. return;
  225. }
  226. invalidate_one_bucket(ca, b);
  227. }
  228. }
  229. static void invalidate_buckets_fifo(struct cache *ca)
  230. {
  231. struct bucket *b;
  232. size_t checked = 0;
  233. while (!fifo_full(&ca->free_inc)) {
  234. if (ca->fifo_last_bucket < ca->sb.first_bucket ||
  235. ca->fifo_last_bucket >= ca->sb.nbuckets)
  236. ca->fifo_last_bucket = ca->sb.first_bucket;
  237. b = ca->buckets + ca->fifo_last_bucket++;
  238. if (can_invalidate_bucket(ca, b))
  239. invalidate_one_bucket(ca, b);
  240. if (++checked >= ca->sb.nbuckets) {
  241. ca->invalidate_needs_gc = 1;
  242. bch_queue_gc(ca->set);
  243. return;
  244. }
  245. }
  246. }
  247. static void invalidate_buckets_random(struct cache *ca)
  248. {
  249. struct bucket *b;
  250. size_t checked = 0;
  251. while (!fifo_full(&ca->free_inc)) {
  252. size_t n;
  253. get_random_bytes(&n, sizeof(n));
  254. n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
  255. n += ca->sb.first_bucket;
  256. b = ca->buckets + n;
  257. if (can_invalidate_bucket(ca, b))
  258. invalidate_one_bucket(ca, b);
  259. if (++checked >= ca->sb.nbuckets / 2) {
  260. ca->invalidate_needs_gc = 1;
  261. bch_queue_gc(ca->set);
  262. return;
  263. }
  264. }
  265. }
  266. static void invalidate_buckets(struct cache *ca)
  267. {
  268. if (ca->invalidate_needs_gc)
  269. return;
  270. switch (CACHE_REPLACEMENT(&ca->sb)) {
  271. case CACHE_REPLACEMENT_LRU:
  272. invalidate_buckets_lru(ca);
  273. break;
  274. case CACHE_REPLACEMENT_FIFO:
  275. invalidate_buckets_fifo(ca);
  276. break;
  277. case CACHE_REPLACEMENT_RANDOM:
  278. invalidate_buckets_random(ca);
  279. break;
  280. }
  281. }
  282. #define allocator_wait(ca, cond) \
  283. do { \
  284. DEFINE_WAIT(__wait); \
  285. \
  286. while (!(cond)) { \
  287. prepare_to_wait(&ca->set->alloc_wait, \
  288. &__wait, TASK_INTERRUPTIBLE); \
  289. \
  290. mutex_unlock(&(ca)->set->bucket_lock); \
  291. if (test_bit(CACHE_SET_STOPPING_2, &ca->set->flags)) { \
  292. finish_wait(&ca->set->alloc_wait, &__wait); \
  293. closure_return(cl); \
  294. } \
  295. \
  296. schedule(); \
  297. __set_current_state(TASK_RUNNING); \
  298. mutex_lock(&(ca)->set->bucket_lock); \
  299. } \
  300. \
  301. finish_wait(&ca->set->alloc_wait, &__wait); \
  302. } while (0)
  303. void bch_allocator_thread(struct closure *cl)
  304. {
  305. struct cache *ca = container_of(cl, struct cache, alloc);
  306. mutex_lock(&ca->set->bucket_lock);
  307. while (1) {
  308. while (1) {
  309. long bucket;
  310. if ((!atomic_read(&ca->set->prio_blocked) ||
  311. !CACHE_SYNC(&ca->set->sb)) &&
  312. !fifo_empty(&ca->unused))
  313. fifo_pop(&ca->unused, bucket);
  314. else if (!fifo_empty(&ca->free_inc))
  315. fifo_pop(&ca->free_inc, bucket);
  316. else
  317. break;
  318. allocator_wait(ca, (int) fifo_free(&ca->free) >
  319. atomic_read(&ca->discards_in_flight));
  320. if (ca->discard) {
  321. allocator_wait(ca, !list_empty(&ca->discards));
  322. do_discard(ca, bucket);
  323. } else {
  324. fifo_push(&ca->free, bucket);
  325. closure_wake_up(&ca->set->bucket_wait);
  326. }
  327. }
  328. allocator_wait(ca, ca->set->gc_mark_valid);
  329. invalidate_buckets(ca);
  330. allocator_wait(ca, !atomic_read(&ca->set->prio_blocked) ||
  331. !CACHE_SYNC(&ca->set->sb));
  332. if (CACHE_SYNC(&ca->set->sb) &&
  333. (!fifo_empty(&ca->free_inc) ||
  334. ca->need_save_prio > 64)) {
  335. bch_prio_write(ca);
  336. }
  337. }
  338. }
  339. long bch_bucket_alloc(struct cache *ca, unsigned watermark, struct closure *cl)
  340. {
  341. long r = -1;
  342. again:
  343. wake_up(&ca->set->alloc_wait);
  344. if (fifo_used(&ca->free) > ca->watermark[watermark] &&
  345. fifo_pop(&ca->free, r)) {
  346. struct bucket *b = ca->buckets + r;
  347. #ifdef CONFIG_BCACHE_EDEBUG
  348. size_t iter;
  349. long i;
  350. for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
  351. BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
  352. fifo_for_each(i, &ca->free, iter)
  353. BUG_ON(i == r);
  354. fifo_for_each(i, &ca->free_inc, iter)
  355. BUG_ON(i == r);
  356. fifo_for_each(i, &ca->unused, iter)
  357. BUG_ON(i == r);
  358. #endif
  359. BUG_ON(atomic_read(&b->pin) != 1);
  360. SET_GC_SECTORS_USED(b, ca->sb.bucket_size);
  361. if (watermark <= WATERMARK_METADATA) {
  362. SET_GC_MARK(b, GC_MARK_METADATA);
  363. b->prio = BTREE_PRIO;
  364. } else {
  365. SET_GC_MARK(b, GC_MARK_RECLAIMABLE);
  366. b->prio = INITIAL_PRIO;
  367. }
  368. return r;
  369. }
  370. pr_debug("alloc failure: blocked %i free %zu free_inc %zu unused %zu",
  371. atomic_read(&ca->set->prio_blocked), fifo_used(&ca->free),
  372. fifo_used(&ca->free_inc), fifo_used(&ca->unused));
  373. if (cl) {
  374. closure_wait(&ca->set->bucket_wait, cl);
  375. if (closure_blocking(cl)) {
  376. mutex_unlock(&ca->set->bucket_lock);
  377. closure_sync(cl);
  378. mutex_lock(&ca->set->bucket_lock);
  379. goto again;
  380. }
  381. }
  382. return -1;
  383. }
  384. void bch_bucket_free(struct cache_set *c, struct bkey *k)
  385. {
  386. unsigned i;
  387. for (i = 0; i < KEY_PTRS(k); i++) {
  388. struct bucket *b = PTR_BUCKET(c, k, i);
  389. SET_GC_MARK(b, 0);
  390. SET_GC_SECTORS_USED(b, 0);
  391. bch_bucket_add_unused(PTR_CACHE(c, k, i), b);
  392. }
  393. }
  394. int __bch_bucket_alloc_set(struct cache_set *c, unsigned watermark,
  395. struct bkey *k, int n, struct closure *cl)
  396. {
  397. int i;
  398. lockdep_assert_held(&c->bucket_lock);
  399. BUG_ON(!n || n > c->caches_loaded || n > 8);
  400. bkey_init(k);
  401. /* sort by free space/prio of oldest data in caches */
  402. for (i = 0; i < n; i++) {
  403. struct cache *ca = c->cache_by_alloc[i];
  404. long b = bch_bucket_alloc(ca, watermark, cl);
  405. if (b == -1)
  406. goto err;
  407. k->ptr[i] = PTR(ca->buckets[b].gen,
  408. bucket_to_sector(c, b),
  409. ca->sb.nr_this_dev);
  410. SET_KEY_PTRS(k, i + 1);
  411. }
  412. return 0;
  413. err:
  414. bch_bucket_free(c, k);
  415. __bkey_put(c, k);
  416. return -1;
  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 ret;
  422. mutex_lock(&c->bucket_lock);
  423. ret = __bch_bucket_alloc_set(c, watermark, k, n, cl);
  424. mutex_unlock(&c->bucket_lock);
  425. return ret;
  426. }
  427. /* Init */
  428. void bch_cache_allocator_exit(struct cache *ca)
  429. {
  430. struct discard *d;
  431. while (!list_empty(&ca->discards)) {
  432. d = list_first_entry(&ca->discards, struct discard, list);
  433. cancel_work_sync(&d->work);
  434. list_del(&d->list);
  435. kfree(d);
  436. }
  437. }
  438. int bch_cache_allocator_init(struct cache *ca)
  439. {
  440. unsigned i;
  441. /*
  442. * Reserve:
  443. * Prio/gen writes first
  444. * Then 8 for btree allocations
  445. * Then half for the moving garbage collector
  446. */
  447. ca->watermark[WATERMARK_PRIO] = 0;
  448. ca->watermark[WATERMARK_METADATA] = prio_buckets(ca);
  449. ca->watermark[WATERMARK_MOVINGGC] = 8 +
  450. ca->watermark[WATERMARK_METADATA];
  451. ca->watermark[WATERMARK_NONE] = ca->free.size / 2 +
  452. ca->watermark[WATERMARK_MOVINGGC];
  453. for (i = 0; i < MAX_IN_FLIGHT_DISCARDS; i++) {
  454. struct discard *d = kzalloc(sizeof(*d), GFP_KERNEL);
  455. if (!d)
  456. return -ENOMEM;
  457. d->ca = ca;
  458. INIT_WORK(&d->work, discard_finish);
  459. list_add(&d->list, &ca->discards);
  460. }
  461. return 0;
  462. }