btree_key_cache.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "bcachefs.h"
  3. #include "btree_cache.h"
  4. #include "btree_iter.h"
  5. #include "btree_key_cache.h"
  6. #include "btree_locking.h"
  7. #include "btree_update.h"
  8. #include "errcode.h"
  9. #include "error.h"
  10. #include "journal.h"
  11. #include "journal_reclaim.h"
  12. #include "trace.h"
  13. #include <linux/sched/mm.h>
  14. static inline bool btree_uses_pcpu_readers(enum btree_id id)
  15. {
  16. return id == BTREE_ID_subvolumes;
  17. }
  18. static struct kmem_cache *bch2_key_cache;
  19. static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
  20. const void *obj)
  21. {
  22. const struct bkey_cached *ck = obj;
  23. const struct bkey_cached_key *key = arg->key;
  24. return ck->key.btree_id != key->btree_id ||
  25. !bpos_eq(ck->key.pos, key->pos);
  26. }
  27. static const struct rhashtable_params bch2_btree_key_cache_params = {
  28. .head_offset = offsetof(struct bkey_cached, hash),
  29. .key_offset = offsetof(struct bkey_cached, key),
  30. .key_len = sizeof(struct bkey_cached_key),
  31. .obj_cmpfn = bch2_btree_key_cache_cmp_fn,
  32. .automatic_shrinking = true,
  33. };
  34. static inline void btree_path_cached_set(struct btree_trans *trans, struct btree_path *path,
  35. struct bkey_cached *ck,
  36. enum btree_node_locked_type lock_held)
  37. {
  38. path->l[0].lock_seq = six_lock_seq(&ck->c.lock);
  39. path->l[0].b = (void *) ck;
  40. mark_btree_node_locked(trans, path, 0, lock_held);
  41. }
  42. __flatten
  43. inline struct bkey_cached *
  44. bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
  45. {
  46. struct bkey_cached_key key = {
  47. .btree_id = btree_id,
  48. .pos = pos,
  49. };
  50. return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
  51. bch2_btree_key_cache_params);
  52. }
  53. static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
  54. {
  55. if (!six_trylock_intent(&ck->c.lock))
  56. return false;
  57. if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
  58. six_unlock_intent(&ck->c.lock);
  59. return false;
  60. }
  61. if (!six_trylock_write(&ck->c.lock)) {
  62. six_unlock_intent(&ck->c.lock);
  63. return false;
  64. }
  65. return true;
  66. }
  67. static bool bkey_cached_evict(struct btree_key_cache *c,
  68. struct bkey_cached *ck)
  69. {
  70. bool ret = !rhashtable_remove_fast(&c->table, &ck->hash,
  71. bch2_btree_key_cache_params);
  72. if (ret) {
  73. memset(&ck->key, ~0, sizeof(ck->key));
  74. atomic_long_dec(&c->nr_keys);
  75. }
  76. return ret;
  77. }
  78. static void __bkey_cached_free(struct rcu_pending *pending, struct rcu_head *rcu)
  79. {
  80. struct bch_fs *c = container_of(pending->srcu, struct bch_fs, btree_trans_barrier);
  81. struct bkey_cached *ck = container_of(rcu, struct bkey_cached, rcu);
  82. this_cpu_dec(*c->btree_key_cache.nr_pending);
  83. kmem_cache_free(bch2_key_cache, ck);
  84. }
  85. static void bkey_cached_free(struct btree_key_cache *bc,
  86. struct bkey_cached *ck)
  87. {
  88. kfree(ck->k);
  89. ck->k = NULL;
  90. ck->u64s = 0;
  91. six_unlock_write(&ck->c.lock);
  92. six_unlock_intent(&ck->c.lock);
  93. bool pcpu_readers = ck->c.lock.readers != NULL;
  94. rcu_pending_enqueue(&bc->pending[pcpu_readers], &ck->rcu);
  95. this_cpu_inc(*bc->nr_pending);
  96. }
  97. static struct bkey_cached *__bkey_cached_alloc(unsigned key_u64s, gfp_t gfp)
  98. {
  99. gfp |= __GFP_ACCOUNT|__GFP_RECLAIMABLE;
  100. struct bkey_cached *ck = kmem_cache_zalloc(bch2_key_cache, gfp);
  101. if (unlikely(!ck))
  102. return NULL;
  103. ck->k = kmalloc(key_u64s * sizeof(u64), gfp);
  104. if (unlikely(!ck->k)) {
  105. kmem_cache_free(bch2_key_cache, ck);
  106. return NULL;
  107. }
  108. ck->u64s = key_u64s;
  109. return ck;
  110. }
  111. static struct bkey_cached *
  112. bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path, unsigned key_u64s)
  113. {
  114. struct bch_fs *c = trans->c;
  115. struct btree_key_cache *bc = &c->btree_key_cache;
  116. bool pcpu_readers = btree_uses_pcpu_readers(path->btree_id);
  117. int ret;
  118. struct bkey_cached *ck = container_of_or_null(
  119. rcu_pending_dequeue(&bc->pending[pcpu_readers]),
  120. struct bkey_cached, rcu);
  121. if (ck)
  122. goto lock;
  123. ck = allocate_dropping_locks(trans, ret,
  124. __bkey_cached_alloc(key_u64s, _gfp));
  125. if (ret) {
  126. if (ck)
  127. kfree(ck->k);
  128. kmem_cache_free(bch2_key_cache, ck);
  129. return ERR_PTR(ret);
  130. }
  131. if (ck) {
  132. bch2_btree_lock_init(&ck->c, pcpu_readers ? SIX_LOCK_INIT_PCPU : 0);
  133. ck->c.cached = true;
  134. goto lock;
  135. }
  136. ck = container_of_or_null(rcu_pending_dequeue_from_all(&bc->pending[pcpu_readers]),
  137. struct bkey_cached, rcu);
  138. if (ck)
  139. goto lock;
  140. lock:
  141. six_lock_intent(&ck->c.lock, NULL, NULL);
  142. six_lock_write(&ck->c.lock, NULL, NULL);
  143. return ck;
  144. }
  145. static struct bkey_cached *
  146. bkey_cached_reuse(struct btree_key_cache *c)
  147. {
  148. struct bucket_table *tbl;
  149. struct rhash_head *pos;
  150. struct bkey_cached *ck;
  151. unsigned i;
  152. rcu_read_lock();
  153. tbl = rht_dereference_rcu(c->table.tbl, &c->table);
  154. for (i = 0; i < tbl->size; i++)
  155. rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
  156. if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
  157. bkey_cached_lock_for_evict(ck)) {
  158. if (bkey_cached_evict(c, ck))
  159. goto out;
  160. six_unlock_write(&ck->c.lock);
  161. six_unlock_intent(&ck->c.lock);
  162. }
  163. }
  164. ck = NULL;
  165. out:
  166. rcu_read_unlock();
  167. return ck;
  168. }
  169. static int btree_key_cache_create(struct btree_trans *trans, struct btree_path *path,
  170. struct bkey_s_c k)
  171. {
  172. struct bch_fs *c = trans->c;
  173. struct btree_key_cache *bc = &c->btree_key_cache;
  174. /*
  175. * bch2_varint_decode can read past the end of the buffer by at
  176. * most 7 bytes (it won't be used):
  177. */
  178. unsigned key_u64s = k.k->u64s + 1;
  179. /*
  180. * Allocate some extra space so that the transaction commit path is less
  181. * likely to have to reallocate, since that requires a transaction
  182. * restart:
  183. */
  184. key_u64s = min(256U, (key_u64s * 3) / 2);
  185. key_u64s = roundup_pow_of_two(key_u64s);
  186. struct bkey_cached *ck = bkey_cached_alloc(trans, path, key_u64s);
  187. int ret = PTR_ERR_OR_ZERO(ck);
  188. if (ret)
  189. return ret;
  190. if (unlikely(!ck)) {
  191. ck = bkey_cached_reuse(bc);
  192. if (unlikely(!ck)) {
  193. bch_err(c, "error allocating memory for key cache item, btree %s",
  194. bch2_btree_id_str(path->btree_id));
  195. return -BCH_ERR_ENOMEM_btree_key_cache_create;
  196. }
  197. }
  198. ck->c.level = 0;
  199. ck->c.btree_id = path->btree_id;
  200. ck->key.btree_id = path->btree_id;
  201. ck->key.pos = path->pos;
  202. ck->flags = 1U << BKEY_CACHED_ACCESSED;
  203. if (unlikely(key_u64s > ck->u64s)) {
  204. mark_btree_node_locked_noreset(path, 0, BTREE_NODE_UNLOCKED);
  205. struct bkey_i *new_k = allocate_dropping_locks(trans, ret,
  206. kmalloc(key_u64s * sizeof(u64), _gfp));
  207. if (unlikely(!new_k)) {
  208. bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
  209. bch2_btree_id_str(ck->key.btree_id), key_u64s);
  210. ret = -BCH_ERR_ENOMEM_btree_key_cache_fill;
  211. } else if (ret) {
  212. kfree(new_k);
  213. goto err;
  214. }
  215. kfree(ck->k);
  216. ck->k = new_k;
  217. ck->u64s = key_u64s;
  218. }
  219. bkey_reassemble(ck->k, k);
  220. ret = rhashtable_lookup_insert_fast(&bc->table, &ck->hash, bch2_btree_key_cache_params);
  221. if (unlikely(ret)) /* raced with another fill? */
  222. goto err;
  223. atomic_long_inc(&bc->nr_keys);
  224. six_unlock_write(&ck->c.lock);
  225. enum six_lock_type lock_want = __btree_lock_want(path, 0);
  226. if (lock_want == SIX_LOCK_read)
  227. six_lock_downgrade(&ck->c.lock);
  228. btree_path_cached_set(trans, path, ck, (enum btree_node_locked_type) lock_want);
  229. path->uptodate = BTREE_ITER_UPTODATE;
  230. return 0;
  231. err:
  232. bkey_cached_free(bc, ck);
  233. mark_btree_node_locked_noreset(path, 0, BTREE_NODE_UNLOCKED);
  234. return ret;
  235. }
  236. static noinline int btree_key_cache_fill(struct btree_trans *trans,
  237. struct btree_path *ck_path,
  238. unsigned flags)
  239. {
  240. if (flags & BTREE_ITER_cached_nofill) {
  241. ck_path->uptodate = BTREE_ITER_UPTODATE;
  242. return 0;
  243. }
  244. struct bch_fs *c = trans->c;
  245. struct btree_iter iter;
  246. struct bkey_s_c k;
  247. int ret;
  248. bch2_trans_iter_init(trans, &iter, ck_path->btree_id, ck_path->pos,
  249. BTREE_ITER_key_cache_fill|
  250. BTREE_ITER_cached_nofill);
  251. iter.flags &= ~BTREE_ITER_with_journal;
  252. k = bch2_btree_iter_peek_slot(&iter);
  253. ret = bkey_err(k);
  254. if (ret)
  255. goto err;
  256. /* Recheck after btree lookup, before allocating: */
  257. ret = bch2_btree_key_cache_find(c, ck_path->btree_id, ck_path->pos) ? -EEXIST : 0;
  258. if (unlikely(ret))
  259. goto out;
  260. ret = btree_key_cache_create(trans, ck_path, k);
  261. if (ret)
  262. goto err;
  263. out:
  264. /* We're not likely to need this iterator again: */
  265. bch2_set_btree_iter_dontneed(&iter);
  266. err:
  267. bch2_trans_iter_exit(trans, &iter);
  268. return ret;
  269. }
  270. static inline int btree_path_traverse_cached_fast(struct btree_trans *trans,
  271. struct btree_path *path)
  272. {
  273. struct bch_fs *c = trans->c;
  274. struct bkey_cached *ck;
  275. retry:
  276. ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
  277. if (!ck)
  278. return -ENOENT;
  279. enum six_lock_type lock_want = __btree_lock_want(path, 0);
  280. int ret = btree_node_lock(trans, path, (void *) ck, 0, lock_want, _THIS_IP_);
  281. if (ret)
  282. return ret;
  283. if (ck->key.btree_id != path->btree_id ||
  284. !bpos_eq(ck->key.pos, path->pos)) {
  285. six_unlock_type(&ck->c.lock, lock_want);
  286. goto retry;
  287. }
  288. if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
  289. set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
  290. btree_path_cached_set(trans, path, ck, (enum btree_node_locked_type) lock_want);
  291. path->uptodate = BTREE_ITER_UPTODATE;
  292. return 0;
  293. }
  294. int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
  295. unsigned flags)
  296. {
  297. EBUG_ON(path->level);
  298. path->l[1].b = NULL;
  299. int ret;
  300. do {
  301. ret = btree_path_traverse_cached_fast(trans, path);
  302. if (unlikely(ret == -ENOENT))
  303. ret = btree_key_cache_fill(trans, path, flags);
  304. } while (ret == -EEXIST);
  305. if (unlikely(ret)) {
  306. path->uptodate = BTREE_ITER_NEED_TRAVERSE;
  307. if (!bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
  308. btree_node_unlock(trans, path, 0);
  309. path->l[0].b = ERR_PTR(ret);
  310. }
  311. }
  312. return ret;
  313. }
  314. static int btree_key_cache_flush_pos(struct btree_trans *trans,
  315. struct bkey_cached_key key,
  316. u64 journal_seq,
  317. unsigned commit_flags,
  318. bool evict)
  319. {
  320. struct bch_fs *c = trans->c;
  321. struct journal *j = &c->journal;
  322. struct btree_iter c_iter, b_iter;
  323. struct bkey_cached *ck = NULL;
  324. int ret;
  325. bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
  326. BTREE_ITER_slots|
  327. BTREE_ITER_intent|
  328. BTREE_ITER_all_snapshots);
  329. bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
  330. BTREE_ITER_cached|
  331. BTREE_ITER_intent);
  332. b_iter.flags &= ~BTREE_ITER_with_key_cache;
  333. ret = bch2_btree_iter_traverse(&c_iter);
  334. if (ret)
  335. goto out;
  336. ck = (void *) btree_iter_path(trans, &c_iter)->l[0].b;
  337. if (!ck)
  338. goto out;
  339. if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
  340. if (evict)
  341. goto evict;
  342. goto out;
  343. }
  344. if (journal_seq && ck->journal.seq != journal_seq)
  345. goto out;
  346. trans->journal_res.seq = ck->journal.seq;
  347. /*
  348. * If we're at the end of the journal, we really want to free up space
  349. * in the journal right away - we don't want to pin that old journal
  350. * sequence number with a new btree node write, we want to re-journal
  351. * the update
  352. */
  353. if (ck->journal.seq == journal_last_seq(j))
  354. commit_flags |= BCH_WATERMARK_reclaim;
  355. if (ck->journal.seq != journal_last_seq(j) ||
  356. !test_bit(JOURNAL_space_low, &c->journal.flags))
  357. commit_flags |= BCH_TRANS_COMMIT_no_journal_res;
  358. ret = bch2_btree_iter_traverse(&b_iter) ?:
  359. bch2_trans_update(trans, &b_iter, ck->k,
  360. BTREE_UPDATE_key_cache_reclaim|
  361. BTREE_UPDATE_internal_snapshot_node|
  362. BTREE_TRIGGER_norun) ?:
  363. bch2_trans_commit(trans, NULL, NULL,
  364. BCH_TRANS_COMMIT_no_check_rw|
  365. BCH_TRANS_COMMIT_no_enospc|
  366. commit_flags);
  367. bch2_fs_fatal_err_on(ret &&
  368. !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
  369. !bch2_err_matches(ret, BCH_ERR_journal_reclaim_would_deadlock) &&
  370. !bch2_journal_error(j), c,
  371. "flushing key cache: %s", bch2_err_str(ret));
  372. if (ret)
  373. goto out;
  374. bch2_journal_pin_drop(j, &ck->journal);
  375. struct btree_path *path = btree_iter_path(trans, &c_iter);
  376. BUG_ON(!btree_node_locked(path, 0));
  377. if (!evict) {
  378. if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
  379. clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
  380. atomic_long_dec(&c->btree_key_cache.nr_dirty);
  381. }
  382. } else {
  383. struct btree_path *path2;
  384. unsigned i;
  385. evict:
  386. trans_for_each_path(trans, path2, i)
  387. if (path2 != path)
  388. __bch2_btree_path_unlock(trans, path2);
  389. bch2_btree_node_lock_write_nofail(trans, path, &ck->c);
  390. if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
  391. clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
  392. atomic_long_dec(&c->btree_key_cache.nr_dirty);
  393. }
  394. mark_btree_node_locked_noreset(path, 0, BTREE_NODE_UNLOCKED);
  395. if (bkey_cached_evict(&c->btree_key_cache, ck)) {
  396. bkey_cached_free(&c->btree_key_cache, ck);
  397. } else {
  398. six_unlock_write(&ck->c.lock);
  399. six_unlock_intent(&ck->c.lock);
  400. }
  401. }
  402. out:
  403. bch2_trans_iter_exit(trans, &b_iter);
  404. bch2_trans_iter_exit(trans, &c_iter);
  405. return ret;
  406. }
  407. int bch2_btree_key_cache_journal_flush(struct journal *j,
  408. struct journal_entry_pin *pin, u64 seq)
  409. {
  410. struct bch_fs *c = container_of(j, struct bch_fs, journal);
  411. struct bkey_cached *ck =
  412. container_of(pin, struct bkey_cached, journal);
  413. struct bkey_cached_key key;
  414. struct btree_trans *trans = bch2_trans_get(c);
  415. int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
  416. int ret = 0;
  417. btree_node_lock_nopath_nofail(trans, &ck->c, SIX_LOCK_read);
  418. key = ck->key;
  419. if (ck->journal.seq != seq ||
  420. !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
  421. six_unlock_read(&ck->c.lock);
  422. goto unlock;
  423. }
  424. if (ck->seq != seq) {
  425. bch2_journal_pin_update(&c->journal, ck->seq, &ck->journal,
  426. bch2_btree_key_cache_journal_flush);
  427. six_unlock_read(&ck->c.lock);
  428. goto unlock;
  429. }
  430. six_unlock_read(&ck->c.lock);
  431. ret = lockrestart_do(trans,
  432. btree_key_cache_flush_pos(trans, key, seq,
  433. BCH_TRANS_COMMIT_journal_reclaim, false));
  434. unlock:
  435. srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
  436. bch2_trans_put(trans);
  437. return ret;
  438. }
  439. bool bch2_btree_insert_key_cached(struct btree_trans *trans,
  440. unsigned flags,
  441. struct btree_insert_entry *insert_entry)
  442. {
  443. struct bch_fs *c = trans->c;
  444. struct bkey_cached *ck = (void *) (trans->paths + insert_entry->path)->l[0].b;
  445. struct bkey_i *insert = insert_entry->k;
  446. bool kick_reclaim = false;
  447. BUG_ON(insert->k.u64s > ck->u64s);
  448. bkey_copy(ck->k, insert);
  449. if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
  450. EBUG_ON(test_bit(BCH_FS_clean_shutdown, &c->flags));
  451. set_bit(BKEY_CACHED_DIRTY, &ck->flags);
  452. atomic_long_inc(&c->btree_key_cache.nr_dirty);
  453. if (bch2_nr_btree_keys_need_flush(c))
  454. kick_reclaim = true;
  455. }
  456. /*
  457. * To minimize lock contention, we only add the journal pin here and
  458. * defer pin updates to the flush callback via ->seq. Be careful not to
  459. * update ->seq on nojournal commits because we don't want to update the
  460. * pin to a seq that doesn't include journal updates on disk. Otherwise
  461. * we risk losing the update after a crash.
  462. *
  463. * The only exception is if the pin is not active in the first place. We
  464. * have to add the pin because journal reclaim drives key cache
  465. * flushing. The flush callback will not proceed unless ->seq matches
  466. * the latest pin, so make sure it starts with a consistent value.
  467. */
  468. if (!(insert_entry->flags & BTREE_UPDATE_nojournal) ||
  469. !journal_pin_active(&ck->journal)) {
  470. ck->seq = trans->journal_res.seq;
  471. }
  472. bch2_journal_pin_add(&c->journal, trans->journal_res.seq,
  473. &ck->journal, bch2_btree_key_cache_journal_flush);
  474. if (kick_reclaim)
  475. journal_reclaim_kick(&c->journal);
  476. return true;
  477. }
  478. void bch2_btree_key_cache_drop(struct btree_trans *trans,
  479. struct btree_path *path)
  480. {
  481. struct bch_fs *c = trans->c;
  482. struct btree_key_cache *bc = &c->btree_key_cache;
  483. struct bkey_cached *ck = (void *) path->l[0].b;
  484. /*
  485. * We just did an update to the btree, bypassing the key cache: the key
  486. * cache key is now stale and must be dropped, even if dirty:
  487. */
  488. if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
  489. clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
  490. atomic_long_dec(&c->btree_key_cache.nr_dirty);
  491. bch2_journal_pin_drop(&c->journal, &ck->journal);
  492. }
  493. bkey_cached_evict(bc, ck);
  494. bkey_cached_free(bc, ck);
  495. mark_btree_node_locked(trans, path, 0, BTREE_NODE_UNLOCKED);
  496. btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
  497. path->should_be_locked = false;
  498. }
  499. static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
  500. struct shrink_control *sc)
  501. {
  502. struct bch_fs *c = shrink->private_data;
  503. struct btree_key_cache *bc = &c->btree_key_cache;
  504. struct bucket_table *tbl;
  505. struct bkey_cached *ck;
  506. size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
  507. unsigned iter, start;
  508. int srcu_idx;
  509. srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
  510. rcu_read_lock();
  511. tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
  512. /*
  513. * Scanning is expensive while a rehash is in progress - most elements
  514. * will be on the new hashtable, if it's in progress
  515. *
  516. * A rehash could still start while we're scanning - that's ok, we'll
  517. * still see most elements.
  518. */
  519. if (unlikely(tbl->nest)) {
  520. rcu_read_unlock();
  521. srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
  522. return SHRINK_STOP;
  523. }
  524. iter = bc->shrink_iter;
  525. if (iter >= tbl->size)
  526. iter = 0;
  527. start = iter;
  528. do {
  529. struct rhash_head *pos, *next;
  530. pos = rht_ptr_rcu(&tbl->buckets[iter]);
  531. while (!rht_is_a_nulls(pos)) {
  532. next = rht_dereference_bucket_rcu(pos->next, tbl, iter);
  533. ck = container_of(pos, struct bkey_cached, hash);
  534. if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
  535. bc->skipped_dirty++;
  536. } else if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags)) {
  537. clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
  538. bc->skipped_accessed++;
  539. } else if (!bkey_cached_lock_for_evict(ck)) {
  540. bc->skipped_lock_fail++;
  541. } else if (bkey_cached_evict(bc, ck)) {
  542. bkey_cached_free(bc, ck);
  543. bc->freed++;
  544. freed++;
  545. } else {
  546. six_unlock_write(&ck->c.lock);
  547. six_unlock_intent(&ck->c.lock);
  548. }
  549. scanned++;
  550. if (scanned >= nr)
  551. goto out;
  552. pos = next;
  553. }
  554. iter++;
  555. if (iter >= tbl->size)
  556. iter = 0;
  557. } while (scanned < nr && iter != start);
  558. out:
  559. bc->shrink_iter = iter;
  560. rcu_read_unlock();
  561. srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
  562. return freed;
  563. }
  564. static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
  565. struct shrink_control *sc)
  566. {
  567. struct bch_fs *c = shrink->private_data;
  568. struct btree_key_cache *bc = &c->btree_key_cache;
  569. long nr = atomic_long_read(&bc->nr_keys) -
  570. atomic_long_read(&bc->nr_dirty);
  571. /*
  572. * Avoid hammering our shrinker too much if it's nearly empty - the
  573. * shrinker code doesn't take into account how big our cache is, if it's
  574. * mostly empty but the system is under memory pressure it causes nasty
  575. * lock contention:
  576. */
  577. nr -= 128;
  578. return max(0L, nr);
  579. }
  580. void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
  581. {
  582. struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
  583. struct bucket_table *tbl;
  584. struct bkey_cached *ck;
  585. struct rhash_head *pos;
  586. LIST_HEAD(items);
  587. unsigned i;
  588. shrinker_free(bc->shrink);
  589. /*
  590. * The loop is needed to guard against racing with rehash:
  591. */
  592. while (atomic_long_read(&bc->nr_keys)) {
  593. rcu_read_lock();
  594. tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
  595. if (tbl) {
  596. if (tbl->nest) {
  597. /* wait for in progress rehash */
  598. rcu_read_unlock();
  599. mutex_lock(&bc->table.mutex);
  600. mutex_unlock(&bc->table.mutex);
  601. rcu_read_lock();
  602. continue;
  603. }
  604. for (i = 0; i < tbl->size; i++)
  605. while (pos = rht_ptr_rcu(&tbl->buckets[i]), !rht_is_a_nulls(pos)) {
  606. ck = container_of(pos, struct bkey_cached, hash);
  607. BUG_ON(!bkey_cached_evict(bc, ck));
  608. kfree(ck->k);
  609. kmem_cache_free(bch2_key_cache, ck);
  610. }
  611. }
  612. rcu_read_unlock();
  613. }
  614. if (atomic_long_read(&bc->nr_dirty) &&
  615. !bch2_journal_error(&c->journal) &&
  616. test_bit(BCH_FS_was_rw, &c->flags))
  617. panic("btree key cache shutdown error: nr_dirty nonzero (%li)\n",
  618. atomic_long_read(&bc->nr_dirty));
  619. if (atomic_long_read(&bc->nr_keys))
  620. panic("btree key cache shutdown error: nr_keys nonzero (%li)\n",
  621. atomic_long_read(&bc->nr_keys));
  622. if (bc->table_init_done)
  623. rhashtable_destroy(&bc->table);
  624. rcu_pending_exit(&bc->pending[0]);
  625. rcu_pending_exit(&bc->pending[1]);
  626. free_percpu(bc->nr_pending);
  627. }
  628. void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
  629. {
  630. }
  631. int bch2_fs_btree_key_cache_init(struct btree_key_cache *bc)
  632. {
  633. struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
  634. struct shrinker *shrink;
  635. bc->nr_pending = alloc_percpu(size_t);
  636. if (!bc->nr_pending)
  637. return -BCH_ERR_ENOMEM_fs_btree_cache_init;
  638. if (rcu_pending_init(&bc->pending[0], &c->btree_trans_barrier, __bkey_cached_free) ||
  639. rcu_pending_init(&bc->pending[1], &c->btree_trans_barrier, __bkey_cached_free))
  640. return -BCH_ERR_ENOMEM_fs_btree_cache_init;
  641. if (rhashtable_init(&bc->table, &bch2_btree_key_cache_params))
  642. return -BCH_ERR_ENOMEM_fs_btree_cache_init;
  643. bc->table_init_done = true;
  644. shrink = shrinker_alloc(0, "%s-btree_key_cache", c->name);
  645. if (!shrink)
  646. return -BCH_ERR_ENOMEM_fs_btree_cache_init;
  647. bc->shrink = shrink;
  648. shrink->count_objects = bch2_btree_key_cache_count;
  649. shrink->scan_objects = bch2_btree_key_cache_scan;
  650. shrink->batch = 1 << 14;
  651. shrink->seeks = 0;
  652. shrink->private_data = c;
  653. shrinker_register(shrink);
  654. return 0;
  655. }
  656. void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *bc)
  657. {
  658. printbuf_tabstop_push(out, 24);
  659. printbuf_tabstop_push(out, 12);
  660. prt_printf(out, "keys:\t%lu\r\n", atomic_long_read(&bc->nr_keys));
  661. prt_printf(out, "dirty:\t%lu\r\n", atomic_long_read(&bc->nr_dirty));
  662. prt_printf(out, "table size:\t%u\r\n", bc->table.tbl->size);
  663. prt_newline(out);
  664. prt_printf(out, "shrinker:\n");
  665. prt_printf(out, "requested_to_free:\t%lu\r\n", bc->requested_to_free);
  666. prt_printf(out, "freed:\t%lu\r\n", bc->freed);
  667. prt_printf(out, "skipped_dirty:\t%lu\r\n", bc->skipped_dirty);
  668. prt_printf(out, "skipped_accessed:\t%lu\r\n", bc->skipped_accessed);
  669. prt_printf(out, "skipped_lock_fail:\t%lu\r\n", bc->skipped_lock_fail);
  670. prt_newline(out);
  671. prt_printf(out, "pending:\t%zu\r\n", per_cpu_sum(bc->nr_pending));
  672. }
  673. void bch2_btree_key_cache_exit(void)
  674. {
  675. kmem_cache_destroy(bch2_key_cache);
  676. }
  677. int __init bch2_btree_key_cache_init(void)
  678. {
  679. bch2_key_cache = KMEM_CACHE(bkey_cached, SLAB_RECLAIM_ACCOUNT);
  680. if (!bch2_key_cache)
  681. return -ENOMEM;
  682. return 0;
  683. }