1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_cache.h"
5 #include "btree_iter.h"
6 #include "btree_key_cache.h"
7 #include "btree_locking.h"
8 #include "btree_update.h"
12 #include "journal_reclaim.h"
14 #include <linux/sched/mm.h>
15 #include <trace/events/bcachefs.h>
17 static inline bool btree_uses_pcpu_readers(enum btree_id id)
19 return id == BTREE_ID_subvolumes;
22 static struct kmem_cache *bch2_key_cache;
24 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
27 const struct bkey_cached *ck = obj;
28 const struct bkey_cached_key *key = arg->key;
30 return ck->key.btree_id != key->btree_id ||
31 !bpos_eq(ck->key.pos, key->pos);
34 static const struct rhashtable_params bch2_btree_key_cache_params = {
35 .head_offset = offsetof(struct bkey_cached, hash),
36 .key_offset = offsetof(struct bkey_cached, key),
37 .key_len = sizeof(struct bkey_cached_key),
38 .obj_cmpfn = bch2_btree_key_cache_cmp_fn,
42 inline struct bkey_cached *
43 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
45 struct bkey_cached_key key = {
50 return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
51 bch2_btree_key_cache_params);
54 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
56 if (!six_trylock_intent(&ck->c.lock))
59 if (!six_trylock_write(&ck->c.lock)) {
60 six_unlock_intent(&ck->c.lock);
64 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
65 six_unlock_write(&ck->c.lock);
66 six_unlock_intent(&ck->c.lock);
73 static void bkey_cached_evict(struct btree_key_cache *c,
74 struct bkey_cached *ck)
76 BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
77 bch2_btree_key_cache_params));
78 memset(&ck->key, ~0, sizeof(ck->key));
80 atomic_long_dec(&c->nr_keys);
83 static void bkey_cached_free(struct btree_key_cache *bc,
84 struct bkey_cached *ck)
86 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
88 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
90 ck->btree_trans_barrier_seq =
91 start_poll_synchronize_srcu(&c->btree_trans_barrier);
93 if (ck->c.lock.readers)
94 list_move_tail(&ck->list, &bc->freed_pcpu);
96 list_move_tail(&ck->list, &bc->freed_nonpcpu);
97 atomic_long_inc(&bc->nr_freed);
103 six_unlock_write(&ck->c.lock);
104 six_unlock_intent(&ck->c.lock);
108 static void __bkey_cached_move_to_freelist_ordered(struct btree_key_cache *bc,
109 struct bkey_cached *ck)
111 struct bkey_cached *pos;
113 list_for_each_entry_reverse(pos, &bc->freed_nonpcpu, list) {
114 if (ULONG_CMP_GE(ck->btree_trans_barrier_seq,
115 pos->btree_trans_barrier_seq)) {
116 list_move(&ck->list, &pos->list);
121 list_move(&ck->list, &bc->freed_nonpcpu);
125 static void bkey_cached_move_to_freelist(struct btree_key_cache *bc,
126 struct bkey_cached *ck)
128 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
130 if (!ck->c.lock.readers) {
132 struct btree_key_cache_freelist *f;
136 f = this_cpu_ptr(bc->pcpu_freed);
138 if (f->nr < ARRAY_SIZE(f->objs)) {
139 f->objs[f->nr++] = ck;
145 mutex_lock(&bc->lock);
147 f = this_cpu_ptr(bc->pcpu_freed);
149 while (f->nr > ARRAY_SIZE(f->objs) / 2) {
150 struct bkey_cached *ck2 = f->objs[--f->nr];
152 __bkey_cached_move_to_freelist_ordered(bc, ck2);
156 __bkey_cached_move_to_freelist_ordered(bc, ck);
157 mutex_unlock(&bc->lock);
160 mutex_lock(&bc->lock);
161 list_move_tail(&ck->list, &bc->freed_nonpcpu);
162 mutex_unlock(&bc->lock);
165 mutex_lock(&bc->lock);
166 list_move_tail(&ck->list, &bc->freed_pcpu);
167 mutex_unlock(&bc->lock);
171 static void bkey_cached_free_fast(struct btree_key_cache *bc,
172 struct bkey_cached *ck)
174 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
176 ck->btree_trans_barrier_seq =
177 start_poll_synchronize_srcu(&c->btree_trans_barrier);
179 list_del_init(&ck->list);
180 atomic_long_inc(&bc->nr_freed);
186 bkey_cached_move_to_freelist(bc, ck);
188 six_unlock_write(&ck->c.lock);
189 six_unlock_intent(&ck->c.lock);
192 static struct bkey_cached *
193 bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path,
196 struct bch_fs *c = trans->c;
197 struct btree_key_cache *bc = &c->btree_key_cache;
198 struct bkey_cached *ck = NULL;
199 bool pcpu_readers = btree_uses_pcpu_readers(path->btree_id);
203 struct btree_key_cache_freelist *f;
206 f = this_cpu_ptr(bc->pcpu_freed);
208 ck = f->objs[--f->nr];
212 mutex_lock(&bc->lock);
214 f = this_cpu_ptr(bc->pcpu_freed);
216 while (!list_empty(&bc->freed_nonpcpu) &&
217 f->nr < ARRAY_SIZE(f->objs) / 2) {
218 ck = list_last_entry(&bc->freed_nonpcpu, struct bkey_cached, list);
219 list_del_init(&ck->list);
220 f->objs[f->nr++] = ck;
223 ck = f->nr ? f->objs[--f->nr] : NULL;
225 mutex_unlock(&bc->lock);
228 mutex_lock(&bc->lock);
229 if (!list_empty(&bc->freed_nonpcpu)) {
230 ck = list_last_entry(&bc->freed_nonpcpu, struct bkey_cached, list);
231 list_del_init(&ck->list);
233 mutex_unlock(&bc->lock);
236 mutex_lock(&bc->lock);
237 if (!list_empty(&bc->freed_pcpu)) {
238 ck = list_last_entry(&bc->freed_pcpu, struct bkey_cached, list);
239 list_del_init(&ck->list);
241 mutex_unlock(&bc->lock);
247 ret = btree_node_lock_nopath(trans, &ck->c, SIX_LOCK_intent);
249 bkey_cached_move_to_freelist(bc, ck);
253 path->l[0].b = (void *) ck;
254 path->l[0].lock_seq = ck->c.lock.state.seq;
255 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
257 ret = bch2_btree_node_lock_write(trans, path, &ck->c);
259 btree_node_unlock(trans, path, 0);
260 bkey_cached_move_to_freelist(bc, ck);
267 /* GFP_NOFS because we're holding btree locks: */
268 ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
270 INIT_LIST_HEAD(&ck->list);
271 __six_lock_init(&ck->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
273 six_lock_pcpu_alloc(&ck->c.lock);
276 BUG_ON(!six_trylock_intent(&ck->c.lock));
277 BUG_ON(!six_trylock_write(&ck->c.lock));
285 static struct bkey_cached *
286 bkey_cached_reuse(struct btree_key_cache *c)
288 struct bucket_table *tbl;
289 struct rhash_head *pos;
290 struct bkey_cached *ck;
293 mutex_lock(&c->lock);
295 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
296 for (i = 0; i < tbl->size; i++)
297 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
298 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
299 bkey_cached_lock_for_evict(ck)) {
300 bkey_cached_evict(c, ck);
307 mutex_unlock(&c->lock);
311 static struct bkey_cached *
312 btree_key_cache_create(struct btree_trans *trans, struct btree_path *path)
314 struct bch_fs *c = trans->c;
315 struct btree_key_cache *bc = &c->btree_key_cache;
316 struct bkey_cached *ck;
317 bool was_new = false;
319 ck = bkey_cached_alloc(trans, path, &was_new);
324 ck = bkey_cached_reuse(bc);
326 bch_err(c, "error allocating memory for key cache item, btree %s",
327 bch2_btree_ids[path->btree_id]);
328 return ERR_PTR(-ENOMEM);
331 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
333 if (path->btree_id == BTREE_ID_subvolumes)
334 six_lock_pcpu_alloc(&ck->c.lock);
338 ck->c.btree_id = path->btree_id;
339 ck->key.btree_id = path->btree_id;
340 ck->key.pos = path->pos;
342 ck->flags = 1U << BKEY_CACHED_ACCESSED;
344 if (unlikely(rhashtable_lookup_insert_fast(&bc->table,
346 bch2_btree_key_cache_params))) {
347 /* We raced with another fill: */
349 if (likely(was_new)) {
350 six_unlock_write(&ck->c.lock);
351 six_unlock_intent(&ck->c.lock);
354 bkey_cached_free_fast(bc, ck);
357 mark_btree_node_locked(trans, path, 0, BTREE_NODE_UNLOCKED);
361 atomic_long_inc(&bc->nr_keys);
363 six_unlock_write(&ck->c.lock);
368 static int btree_key_cache_fill(struct btree_trans *trans,
369 struct btree_path *ck_path,
370 struct bkey_cached *ck)
372 struct btree_path *path;
374 unsigned new_u64s = 0;
375 struct bkey_i *new_k = NULL;
379 path = bch2_path_get(trans, ck->key.btree_id,
380 ck->key.pos, 0, 0, 0, _THIS_IP_);
381 ret = bch2_btree_path_traverse(trans, path, 0);
385 k = bch2_btree_path_peek_slot(path, &u);
387 if (!bch2_btree_node_relock(trans, ck_path, 0)) {
388 trace_and_count(trans->c, trans_restart_relock_key_cache_fill, trans, _THIS_IP_, ck_path);
389 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
394 * bch2_varint_decode can read past the end of the buffer by at
395 * most 7 bytes (it won't be used):
397 new_u64s = k.k->u64s + 1;
400 * Allocate some extra space so that the transaction commit path is less
401 * likely to have to reallocate, since that requires a transaction
404 new_u64s = min(256U, (new_u64s * 3) / 2);
406 if (new_u64s > ck->u64s) {
407 new_u64s = roundup_pow_of_two(new_u64s);
408 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
410 bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
411 bch2_btree_ids[ck->key.btree_id], new_u64s);
417 ret = bch2_btree_node_lock_write(trans, ck_path, &ck_path->l[0].b->c);
429 bkey_reassemble(ck->k, k);
431 bch2_btree_node_unlock_write(trans, ck_path, ck_path->l[0].b);
433 /* We're not likely to need this iterator again: */
434 path->preserve = false;
436 bch2_path_put(trans, path, 0);
441 bch2_btree_path_traverse_cached_slowpath(struct btree_trans *trans, struct btree_path *path,
444 struct bch_fs *c = trans->c;
445 struct bkey_cached *ck;
452 if (bch2_btree_node_relock(trans, path, 0)) {
453 ck = (void *) path->l[0].b;
457 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
459 ck = btree_key_cache_create(trans, path);
460 ret = PTR_ERR_OR_ZERO(ck);
466 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
467 path->locks_want = 1;
469 enum six_lock_type lock_want = __btree_lock_want(path, 0);
471 ret = btree_node_lock(trans, path, (void *) ck, 0,
472 lock_want, _THIS_IP_);
473 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
478 if (ck->key.btree_id != path->btree_id ||
479 !bpos_eq(ck->key.pos, path->pos)) {
480 six_unlock_type(&ck->c.lock, lock_want);
484 mark_btree_node_locked(trans, path, 0, lock_want);
487 path->l[0].lock_seq = ck->c.lock.state.seq;
488 path->l[0].b = (void *) ck;
492 * Using the underscore version because we haven't set
493 * path->uptodate yet:
495 if (!path->locks_want &&
496 !__bch2_btree_path_upgrade(trans, path, 1)) {
497 trace_and_count(trans->c, trans_restart_key_cache_upgrade, trans, _THIS_IP_);
498 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_upgrade);
502 ret = btree_key_cache_fill(trans, path, ck);
507 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
508 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
510 path->uptodate = BTREE_ITER_UPTODATE;
512 BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
516 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
517 btree_node_unlock(trans, path, 0);
518 path->l[0].b = ERR_PTR(ret);
523 int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
526 struct bch_fs *c = trans->c;
527 struct bkey_cached *ck;
530 EBUG_ON(path->level);
534 if (bch2_btree_node_relock(trans, path, 0)) {
535 ck = (void *) path->l[0].b;
539 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
541 return bch2_btree_path_traverse_cached_slowpath(trans, path, flags);
543 enum six_lock_type lock_want = __btree_lock_want(path, 0);
545 ret = btree_node_lock(trans, path, (void *) ck, 0,
546 lock_want, _THIS_IP_);
547 EBUG_ON(ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart));
552 if (ck->key.btree_id != path->btree_id ||
553 !bpos_eq(ck->key.pos, path->pos)) {
554 six_unlock_type(&ck->c.lock, lock_want);
558 mark_btree_node_locked(trans, path, 0, lock_want);
561 path->l[0].lock_seq = ck->c.lock.state.seq;
562 path->l[0].b = (void *) ck;
565 return bch2_btree_path_traverse_cached_slowpath(trans, path, flags);
567 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
568 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
570 path->uptodate = BTREE_ITER_UPTODATE;
572 EBUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
577 static int btree_key_cache_flush_pos(struct btree_trans *trans,
578 struct bkey_cached_key key,
580 unsigned commit_flags,
583 struct bch_fs *c = trans->c;
584 struct journal *j = &c->journal;
585 struct btree_iter c_iter, b_iter;
586 struct bkey_cached *ck = NULL;
589 bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
592 BTREE_ITER_ALL_SNAPSHOTS);
593 bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
596 b_iter.flags &= ~BTREE_ITER_WITH_KEY_CACHE;
598 ret = bch2_btree_iter_traverse(&c_iter);
602 ck = (void *) c_iter.path->l[0].b;
606 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
614 if (journal_seq && ck->journal.seq != journal_seq)
618 * Since journal reclaim depends on us making progress here, and the
619 * allocator/copygc depend on journal reclaim making progress, we need
620 * to be using alloc reserves:
622 ret = bch2_btree_iter_traverse(&b_iter) ?:
623 bch2_trans_update(trans, &b_iter, ck->k,
624 BTREE_UPDATE_KEY_CACHE_RECLAIM|
625 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
626 BTREE_TRIGGER_NORUN) ?:
627 bch2_trans_commit(trans, NULL, NULL,
628 BTREE_INSERT_NOCHECK_RW|
630 BTREE_INSERT_USE_RESERVE|
631 (ck->journal.seq == journal_last_seq(j)
632 ? JOURNAL_WATERMARK_reserved
636 bch2_fs_fatal_err_on(ret &&
637 !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
638 !bch2_err_matches(ret, BCH_ERR_journal_reclaim_would_deadlock) &&
639 !bch2_journal_error(j), c,
640 "error flushing key cache: %s", bch2_err_str(ret));
644 bch2_journal_pin_drop(j, &ck->journal);
645 bch2_journal_preres_put(j, &ck->res);
647 BUG_ON(!btree_node_locked(c_iter.path, 0));
650 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
651 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
652 atomic_long_dec(&c->btree_key_cache.nr_dirty);
655 struct btree_path *path2;
657 trans_for_each_path(trans, path2)
658 if (path2 != c_iter.path)
659 __bch2_btree_path_unlock(trans, path2);
661 bch2_btree_node_lock_write_nofail(trans, c_iter.path, &ck->c);
663 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
664 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
665 atomic_long_dec(&c->btree_key_cache.nr_dirty);
668 mark_btree_node_locked_noreset(c_iter.path, 0, BTREE_NODE_UNLOCKED);
669 bkey_cached_evict(&c->btree_key_cache, ck);
670 bkey_cached_free_fast(&c->btree_key_cache, ck);
673 bch2_trans_iter_exit(trans, &b_iter);
674 bch2_trans_iter_exit(trans, &c_iter);
678 int bch2_btree_key_cache_journal_flush(struct journal *j,
679 struct journal_entry_pin *pin, u64 seq)
681 struct bch_fs *c = container_of(j, struct bch_fs, journal);
682 struct bkey_cached *ck =
683 container_of(pin, struct bkey_cached, journal);
684 struct bkey_cached_key key;
685 struct btree_trans trans;
686 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
689 bch2_trans_init(&trans, c, 0, 0);
691 btree_node_lock_nopath_nofail(&trans, &ck->c, SIX_LOCK_read);
694 if (ck->journal.seq != seq ||
695 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
696 six_unlock_read(&ck->c.lock);
699 six_unlock_read(&ck->c.lock);
701 ret = commit_do(&trans, NULL, NULL, 0,
702 btree_key_cache_flush_pos(&trans, key, seq,
703 BTREE_INSERT_JOURNAL_RECLAIM, false));
705 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
707 bch2_trans_exit(&trans);
712 * Flush and evict a key from the key cache:
714 int bch2_btree_key_cache_flush(struct btree_trans *trans,
715 enum btree_id id, struct bpos pos)
717 struct bch_fs *c = trans->c;
718 struct bkey_cached_key key = { id, pos };
720 /* Fastpath - assume it won't be found: */
721 if (!bch2_btree_key_cache_find(c, id, pos))
724 return btree_key_cache_flush_pos(trans, key, 0, 0, true);
727 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
728 struct btree_path *path,
729 struct bkey_i *insert)
731 struct bch_fs *c = trans->c;
732 struct bkey_cached *ck = (void *) path->l[0].b;
733 bool kick_reclaim = false;
735 BUG_ON(insert->u64s > ck->u64s);
737 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
740 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
742 difference = jset_u64s(insert->u64s) - ck->res.u64s;
743 if (difference > 0) {
744 trans->journal_preres.u64s -= difference;
745 ck->res.u64s += difference;
749 bkey_copy(ck->k, insert);
752 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
753 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
754 atomic_long_inc(&c->btree_key_cache.nr_dirty);
756 if (bch2_nr_btree_keys_need_flush(c))
760 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
761 &ck->journal, bch2_btree_key_cache_journal_flush);
764 journal_reclaim_kick(&c->journal);
768 void bch2_btree_key_cache_drop(struct btree_trans *trans,
769 struct btree_path *path)
771 struct bch_fs *c = trans->c;
772 struct bkey_cached *ck = (void *) path->l[0].b;
777 * We just did an update to the btree, bypassing the key cache: the key
778 * cache key is now stale and must be dropped, even if dirty:
780 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
781 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
782 atomic_long_dec(&c->btree_key_cache.nr_dirty);
783 bch2_journal_pin_drop(&c->journal, &ck->journal);
789 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
790 struct shrink_control *sc)
792 struct bch_fs *c = container_of(shrink, struct bch_fs,
793 btree_key_cache.shrink);
794 struct btree_key_cache *bc = &c->btree_key_cache;
795 struct bucket_table *tbl;
796 struct bkey_cached *ck, *t;
797 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
798 unsigned start, flags;
801 mutex_lock(&bc->lock);
802 srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
803 flags = memalloc_nofs_save();
806 * Newest freed entries are at the end of the list - once we hit one
807 * that's too new to be freed, we can bail out:
809 list_for_each_entry_safe(ck, t, &bc->freed_nonpcpu, list) {
810 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
811 ck->btree_trans_barrier_seq))
815 six_lock_pcpu_free(&ck->c.lock);
816 kmem_cache_free(bch2_key_cache, ck);
817 atomic_long_dec(&bc->nr_freed);
825 list_for_each_entry_safe(ck, t, &bc->freed_pcpu, list) {
826 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
827 ck->btree_trans_barrier_seq))
831 six_lock_pcpu_free(&ck->c.lock);
832 kmem_cache_free(bch2_key_cache, ck);
833 atomic_long_dec(&bc->nr_freed);
842 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
843 if (bc->shrink_iter >= tbl->size)
845 start = bc->shrink_iter;
848 struct rhash_head *pos, *next;
850 pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
852 while (!rht_is_a_nulls(pos)) {
853 next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
854 ck = container_of(pos, struct bkey_cached, hash);
856 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
859 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
860 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
861 else if (bkey_cached_lock_for_evict(ck)) {
862 bkey_cached_evict(bc, ck);
863 bkey_cached_free(bc, ck);
874 if (bc->shrink_iter >= tbl->size)
876 } while (scanned < nr && bc->shrink_iter != start);
880 memalloc_nofs_restore(flags);
881 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
882 mutex_unlock(&bc->lock);
887 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
888 struct shrink_control *sc)
890 struct bch_fs *c = container_of(shrink, struct bch_fs,
891 btree_key_cache.shrink);
892 struct btree_key_cache *bc = &c->btree_key_cache;
893 long nr = atomic_long_read(&bc->nr_keys) -
894 atomic_long_read(&bc->nr_dirty);
899 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
901 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
902 struct bucket_table *tbl;
903 struct bkey_cached *ck, *n;
904 struct rhash_head *pos;
910 if (bc->shrink.list.next)
911 unregister_shrinker(&bc->shrink);
913 mutex_lock(&bc->lock);
916 * The loop is needed to guard against racing with rehash:
918 while (atomic_long_read(&bc->nr_keys)) {
920 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
922 for (i = 0; i < tbl->size; i++)
923 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
924 bkey_cached_evict(bc, ck);
925 list_add(&ck->list, &bc->freed_nonpcpu);
931 for_each_possible_cpu(cpu) {
932 struct btree_key_cache_freelist *f =
933 per_cpu_ptr(bc->pcpu_freed, cpu);
935 for (i = 0; i < f->nr; i++) {
937 list_add(&ck->list, &bc->freed_nonpcpu);
942 list_splice(&bc->freed_pcpu, &bc->freed_nonpcpu);
944 list_for_each_entry_safe(ck, n, &bc->freed_nonpcpu, list) {
947 bch2_journal_pin_drop(&c->journal, &ck->journal);
948 bch2_journal_preres_put(&c->journal, &ck->res);
952 six_lock_pcpu_free(&ck->c.lock);
953 kmem_cache_free(bch2_key_cache, ck);
956 if (atomic_long_read(&bc->nr_dirty) &&
957 !bch2_journal_error(&c->journal) &&
958 test_bit(BCH_FS_WAS_RW, &c->flags))
959 panic("btree key cache shutdown error: nr_dirty nonzero (%li)\n",
960 atomic_long_read(&bc->nr_dirty));
962 if (atomic_long_read(&bc->nr_keys))
963 panic("btree key cache shutdown error: nr_keys nonzero (%li)\n",
964 atomic_long_read(&bc->nr_keys));
966 mutex_unlock(&bc->lock);
968 if (bc->table_init_done)
969 rhashtable_destroy(&bc->table);
971 free_percpu(bc->pcpu_freed);
974 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
976 mutex_init(&c->lock);
977 INIT_LIST_HEAD(&c->freed_pcpu);
978 INIT_LIST_HEAD(&c->freed_nonpcpu);
981 static void bch2_btree_key_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
983 struct btree_key_cache *bc =
984 container_of(shrink, struct btree_key_cache, shrink);
986 bch2_btree_key_cache_to_text(out, bc);
989 int bch2_fs_btree_key_cache_init(struct btree_key_cache *bc)
991 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
995 bc->pcpu_freed = alloc_percpu(struct btree_key_cache_freelist);
1000 ret = rhashtable_init(&bc->table, &bch2_btree_key_cache_params);
1004 bc->table_init_done = true;
1006 bc->shrink.seeks = 0;
1007 bc->shrink.count_objects = bch2_btree_key_cache_count;
1008 bc->shrink.scan_objects = bch2_btree_key_cache_scan;
1009 bc->shrink.to_text = bch2_btree_key_cache_shrinker_to_text;
1010 return register_shrinker(&bc->shrink, "%s/btree_key_cache", c->name);
1013 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
1015 prt_printf(out, "nr_freed:\t%zu", atomic_long_read(&c->nr_freed));
1017 prt_printf(out, "nr_keys:\t%lu", atomic_long_read(&c->nr_keys));
1019 prt_printf(out, "nr_dirty:\t%lu", atomic_long_read(&c->nr_dirty));
1023 void bch2_btree_key_cache_exit(void)
1025 kmem_cache_destroy(bch2_key_cache);
1028 int __init bch2_btree_key_cache_init(void)
1030 bch2_key_cache = KMEM_CACHE(bkey_cached, 0);
1031 if (!bch2_key_cache)