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"
11 #include "journal_reclaim.h"
13 #include <linux/sched/mm.h>
14 #include <trace/events/bcachefs.h>
16 static inline bool btree_uses_pcpu_readers(enum btree_id id)
18 return id == BTREE_ID_subvolumes;
21 static struct kmem_cache *bch2_key_cache;
23 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
26 const struct bkey_cached *ck = obj;
27 const struct bkey_cached_key *key = arg->key;
29 return cmp_int(ck->key.btree_id, key->btree_id) ?:
30 bpos_cmp(ck->key.pos, key->pos);
33 static const struct rhashtable_params bch2_btree_key_cache_params = {
34 .head_offset = offsetof(struct bkey_cached, hash),
35 .key_offset = offsetof(struct bkey_cached, key),
36 .key_len = sizeof(struct bkey_cached_key),
37 .obj_cmpfn = bch2_btree_key_cache_cmp_fn,
41 inline struct bkey_cached *
42 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
44 struct bkey_cached_key key = {
49 return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
50 bch2_btree_key_cache_params);
53 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
55 if (!six_trylock_intent(&ck->c.lock))
58 if (!six_trylock_write(&ck->c.lock)) {
59 six_unlock_intent(&ck->c.lock);
63 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
64 six_unlock_write(&ck->c.lock);
65 six_unlock_intent(&ck->c.lock);
72 static void bkey_cached_evict(struct btree_key_cache *c,
73 struct bkey_cached *ck)
75 BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
76 bch2_btree_key_cache_params));
77 memset(&ck->key, ~0, sizeof(ck->key));
79 atomic_long_dec(&c->nr_keys);
82 static void bkey_cached_free(struct btree_key_cache *bc,
83 struct bkey_cached *ck)
85 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
87 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
89 ck->btree_trans_barrier_seq =
90 start_poll_synchronize_srcu(&c->btree_trans_barrier);
92 if (ck->c.lock.readers)
93 list_move_tail(&ck->list, &bc->freed_pcpu);
95 list_move_tail(&ck->list, &bc->freed_nonpcpu);
96 atomic_long_inc(&bc->nr_freed);
102 six_unlock_write(&ck->c.lock);
103 six_unlock_intent(&ck->c.lock);
106 static void bkey_cached_move_to_freelist(struct btree_key_cache *bc,
107 struct bkey_cached *ck)
109 struct btree_key_cache_freelist *f;
112 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
114 if (!ck->c.lock.readers) {
116 f = this_cpu_ptr(bc->pcpu_freed);
118 if (f->nr < ARRAY_SIZE(f->objs)) {
119 f->objs[f->nr++] = ck;
125 mutex_lock(&bc->lock);
127 f = this_cpu_ptr(bc->pcpu_freed);
129 while (f->nr > ARRAY_SIZE(f->objs) / 2) {
130 struct bkey_cached *ck2 = f->objs[--f->nr];
132 list_move_tail(&ck2->list, &bc->freed_nonpcpu);
136 list_move_tail(&ck->list, &bc->freed_nonpcpu);
137 mutex_unlock(&bc->lock);
140 mutex_lock(&bc->lock);
141 list_move_tail(&ck->list, &bc->freed_pcpu);
142 mutex_unlock(&bc->lock);
146 static void bkey_cached_free_fast(struct btree_key_cache *bc,
147 struct bkey_cached *ck)
149 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
151 ck->btree_trans_barrier_seq =
152 start_poll_synchronize_srcu(&c->btree_trans_barrier);
154 list_del_init(&ck->list);
155 atomic_long_inc(&bc->nr_freed);
161 bkey_cached_move_to_freelist(bc, ck);
163 six_unlock_write(&ck->c.lock);
164 six_unlock_intent(&ck->c.lock);
167 static struct bkey_cached *
168 bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path)
170 struct bch_fs *c = trans->c;
171 struct btree_key_cache *bc = &c->btree_key_cache;
172 struct bkey_cached *ck = NULL;
173 struct btree_key_cache_freelist *f;
174 bool pcpu_readers = btree_uses_pcpu_readers(path->btree_id);
178 f = this_cpu_ptr(bc->pcpu_freed);
180 ck = f->objs[--f->nr];
184 mutex_lock(&bc->lock);
186 f = this_cpu_ptr(bc->pcpu_freed);
188 while (!list_empty(&bc->freed_nonpcpu) &&
189 f->nr < ARRAY_SIZE(f->objs) / 2) {
190 ck = list_last_entry(&bc->freed_nonpcpu, struct bkey_cached, list);
191 list_del_init(&ck->list);
192 f->objs[f->nr++] = ck;
195 ck = f->nr ? f->objs[--f->nr] : NULL;
197 mutex_unlock(&bc->lock);
200 mutex_lock(&bc->lock);
201 if (!list_empty(&bc->freed_pcpu)) {
202 ck = list_last_entry(&bc->freed_pcpu, struct bkey_cached, list);
203 list_del_init(&ck->list);
205 mutex_unlock(&bc->lock);
211 ret = btree_node_lock_nopath(trans, &ck->c, SIX_LOCK_intent);
213 bkey_cached_move_to_freelist(bc, ck);
217 path->l[0].b = (void *) ck;
218 path->l[0].lock_seq = ck->c.lock.state.seq;
219 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
221 ret = bch2_btree_node_lock_write(trans, path, &ck->c);
223 btree_node_unlock(trans, path, 0);
224 bkey_cached_move_to_freelist(bc, ck);
231 ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
233 INIT_LIST_HEAD(&ck->list);
234 __six_lock_init(&ck->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
236 six_lock_pcpu_alloc(&ck->c.lock);
239 BUG_ON(!six_trylock_intent(&ck->c.lock));
240 BUG_ON(!six_trylock_write(&ck->c.lock));
247 static struct bkey_cached *
248 bkey_cached_reuse(struct btree_key_cache *c)
250 struct bucket_table *tbl;
251 struct rhash_head *pos;
252 struct bkey_cached *ck;
256 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
257 for (i = 0; i < tbl->size; i++)
258 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
259 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
260 bkey_cached_lock_for_evict(ck)) {
261 bkey_cached_evict(c, ck);
271 static struct bkey_cached *
272 btree_key_cache_create(struct btree_trans *trans, struct btree_path *path)
274 struct bch_fs *c = trans->c;
275 struct btree_key_cache *bc = &c->btree_key_cache;
276 struct bkey_cached *ck;
279 ck = bkey_cached_alloc(trans, path);
280 if (unlikely(IS_ERR(ck)))
284 ck = bkey_cached_reuse(bc);
286 bch_err(c, "error allocating memory for key cache item, btree %s",
287 bch2_btree_ids[path->btree_id]);
288 return ERR_PTR(-ENOMEM);
291 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
294 if (path->btree_id == BTREE_ID_subvolumes)
295 six_lock_pcpu_alloc(&ck->c.lock);
299 ck->c.btree_id = path->btree_id;
300 ck->key.btree_id = path->btree_id;
301 ck->key.pos = path->pos;
303 ck->flags = 1U << BKEY_CACHED_ACCESSED;
305 if (unlikely(rhashtable_lookup_insert_fast(&bc->table,
307 bch2_btree_key_cache_params))) {
308 /* We raced with another fill: */
310 if (likely(was_new)) {
311 six_unlock_write(&ck->c.lock);
312 six_unlock_intent(&ck->c.lock);
313 mark_btree_node_locked(trans, path, 0, BTREE_NODE_UNLOCKED);
316 bkey_cached_free_fast(bc, ck);
322 atomic_long_inc(&bc->nr_keys);
324 six_unlock_write(&ck->c.lock);
329 static int btree_key_cache_fill(struct btree_trans *trans,
330 struct btree_path *ck_path,
331 struct bkey_cached *ck)
333 struct btree_path *path;
335 unsigned new_u64s = 0;
336 struct bkey_i *new_k = NULL;
340 path = bch2_path_get(trans, ck->key.btree_id,
341 ck->key.pos, 0, 0, 0, _THIS_IP_);
342 ret = bch2_btree_path_traverse(trans, path, 0);
346 k = bch2_btree_path_peek_slot(path, &u);
348 if (!bch2_btree_node_relock(trans, ck_path, 0)) {
349 trace_and_count(trans->c, trans_restart_relock_key_cache_fill, trans, _THIS_IP_, ck_path);
350 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
355 * bch2_varint_decode can read past the end of the buffer by at
356 * most 7 bytes (it won't be used):
358 new_u64s = k.k->u64s + 1;
361 * Allocate some extra space so that the transaction commit path is less
362 * likely to have to reallocate, since that requires a transaction
365 new_u64s = min(256U, (new_u64s * 3) / 2);
367 if (new_u64s > ck->u64s) {
368 new_u64s = roundup_pow_of_two(new_u64s);
369 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
371 bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
372 bch2_btree_ids[ck->key.btree_id], new_u64s);
378 ret = bch2_btree_node_lock_write(trans, ck_path, &ck_path->l[0].b->c);
390 bkey_reassemble(ck->k, k);
392 bch2_btree_node_unlock_write(trans, ck_path, ck_path->l[0].b);
394 /* We're not likely to need this iterator again: */
395 path->preserve = false;
397 bch2_path_put(trans, path, 0);
402 bch2_btree_path_traverse_cached_slowpath(struct btree_trans *trans, struct btree_path *path,
405 struct bch_fs *c = trans->c;
406 struct bkey_cached *ck;
413 if (bch2_btree_node_relock(trans, path, 0)) {
414 ck = (void *) path->l[0].b;
418 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
420 ck = btree_key_cache_create(trans, path);
421 ret = PTR_ERR_OR_ZERO(ck);
427 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
428 path->locks_want = 1;
430 enum six_lock_type lock_want = __btree_lock_want(path, 0);
432 ret = btree_node_lock(trans, path, (void *) ck, 0,
433 lock_want, _THIS_IP_);
434 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
439 if (ck->key.btree_id != path->btree_id ||
440 bpos_cmp(ck->key.pos, path->pos)) {
441 six_unlock_type(&ck->c.lock, lock_want);
445 mark_btree_node_locked(trans, path, 0, lock_want);
448 path->l[0].lock_seq = ck->c.lock.state.seq;
449 path->l[0].b = (void *) ck;
453 * Using the underscore version because we haven't set
454 * path->uptodate yet:
456 if (!path->locks_want &&
457 !__bch2_btree_path_upgrade(trans, path, 1)) {
458 trace_and_count(trans->c, trans_restart_key_cache_upgrade, trans, _THIS_IP_);
459 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_upgrade);
463 ret = btree_key_cache_fill(trans, path, ck);
468 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
469 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
471 path->uptodate = BTREE_ITER_UPTODATE;
473 BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
477 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
478 btree_node_unlock(trans, path, 0);
479 path->l[0].b = ERR_PTR(ret);
484 int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
487 struct bch_fs *c = trans->c;
488 struct bkey_cached *ck;
491 EBUG_ON(path->level);
495 if (bch2_btree_node_relock(trans, path, 0)) {
496 ck = (void *) path->l[0].b;
500 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
502 return bch2_btree_path_traverse_cached_slowpath(trans, path, flags);
504 enum six_lock_type lock_want = __btree_lock_want(path, 0);
506 ret = btree_node_lock(trans, path, (void *) ck, 0,
507 lock_want, _THIS_IP_);
508 EBUG_ON(ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart));
513 if (ck->key.btree_id != path->btree_id ||
514 bpos_cmp(ck->key.pos, path->pos)) {
515 six_unlock_type(&ck->c.lock, lock_want);
519 mark_btree_node_locked(trans, path, 0, lock_want);
522 path->l[0].lock_seq = ck->c.lock.state.seq;
523 path->l[0].b = (void *) ck;
526 return bch2_btree_path_traverse_cached_slowpath(trans, path, flags);
528 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
529 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
531 path->uptodate = BTREE_ITER_UPTODATE;
533 EBUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
538 static int btree_key_cache_flush_pos(struct btree_trans *trans,
539 struct bkey_cached_key key,
541 unsigned commit_flags,
544 struct bch_fs *c = trans->c;
545 struct journal *j = &c->journal;
546 struct btree_iter c_iter, b_iter;
547 struct bkey_cached *ck = NULL;
550 bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
553 BTREE_ITER_ALL_SNAPSHOTS);
554 bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
557 b_iter.flags &= ~BTREE_ITER_WITH_KEY_CACHE;
559 ret = bch2_btree_iter_traverse(&c_iter);
563 ck = (void *) c_iter.path->l[0].b;
567 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
575 if (journal_seq && ck->journal.seq != journal_seq)
579 * Since journal reclaim depends on us making progress here, and the
580 * allocator/copygc depend on journal reclaim making progress, we need
581 * to be using alloc reserves:
583 ret = bch2_btree_iter_traverse(&b_iter) ?:
584 bch2_trans_update(trans, &b_iter, ck->k,
585 BTREE_UPDATE_KEY_CACHE_RECLAIM|
586 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
587 BTREE_TRIGGER_NORUN) ?:
588 bch2_trans_commit(trans, NULL, NULL,
589 BTREE_INSERT_NOCHECK_RW|
591 BTREE_INSERT_USE_RESERVE|
592 (ck->journal.seq == journal_last_seq(j)
593 ? JOURNAL_WATERMARK_reserved
597 bch2_fs_fatal_err_on(ret &&
598 !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
599 !bch2_err_matches(ret, BCH_ERR_journal_reclaim_would_deadlock) &&
600 !bch2_journal_error(j), c,
601 "error flushing key cache: %s", bch2_err_str(ret));
605 bch2_journal_pin_drop(j, &ck->journal);
606 bch2_journal_preres_put(j, &ck->res);
608 BUG_ON(!btree_node_locked(c_iter.path, 0));
611 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
612 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
613 atomic_long_dec(&c->btree_key_cache.nr_dirty);
616 struct btree_path *path2;
618 trans_for_each_path(trans, path2)
619 if (path2 != c_iter.path)
620 __bch2_btree_path_unlock(trans, path2);
622 bch2_btree_node_lock_write_nofail(trans, c_iter.path, &ck->c);
624 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
625 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
626 atomic_long_dec(&c->btree_key_cache.nr_dirty);
629 mark_btree_node_locked_noreset(c_iter.path, 0, BTREE_NODE_UNLOCKED);
630 bkey_cached_evict(&c->btree_key_cache, ck);
631 bkey_cached_free_fast(&c->btree_key_cache, ck);
634 bch2_trans_iter_exit(trans, &b_iter);
635 bch2_trans_iter_exit(trans, &c_iter);
639 int bch2_btree_key_cache_journal_flush(struct journal *j,
640 struct journal_entry_pin *pin, u64 seq)
642 struct bch_fs *c = container_of(j, struct bch_fs, journal);
643 struct bkey_cached *ck =
644 container_of(pin, struct bkey_cached, journal);
645 struct bkey_cached_key key;
646 struct btree_trans trans;
647 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
650 bch2_trans_init(&trans, c, 0, 0);
652 btree_node_lock_nopath_nofail(&trans, &ck->c, SIX_LOCK_read);
655 if (ck->journal.seq != seq ||
656 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
657 six_unlock_read(&ck->c.lock);
660 six_unlock_read(&ck->c.lock);
662 ret = commit_do(&trans, NULL, NULL, 0,
663 btree_key_cache_flush_pos(&trans, key, seq,
664 BTREE_INSERT_JOURNAL_RECLAIM, false));
666 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
668 bch2_trans_exit(&trans);
673 * Flush and evict a key from the key cache:
675 int bch2_btree_key_cache_flush(struct btree_trans *trans,
676 enum btree_id id, struct bpos pos)
678 struct bch_fs *c = trans->c;
679 struct bkey_cached_key key = { id, pos };
681 /* Fastpath - assume it won't be found: */
682 if (!bch2_btree_key_cache_find(c, id, pos))
685 return btree_key_cache_flush_pos(trans, key, 0, 0, true);
688 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
689 struct btree_path *path,
690 struct bkey_i *insert)
692 struct bch_fs *c = trans->c;
693 struct bkey_cached *ck = (void *) path->l[0].b;
694 bool kick_reclaim = false;
696 BUG_ON(insert->u64s > ck->u64s);
698 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
701 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
703 difference = jset_u64s(insert->u64s) - ck->res.u64s;
704 if (difference > 0) {
705 trans->journal_preres.u64s -= difference;
706 ck->res.u64s += difference;
710 bkey_copy(ck->k, insert);
713 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
714 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
715 atomic_long_inc(&c->btree_key_cache.nr_dirty);
717 if (bch2_nr_btree_keys_need_flush(c))
721 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
722 &ck->journal, bch2_btree_key_cache_journal_flush);
725 journal_reclaim_kick(&c->journal);
729 void bch2_btree_key_cache_drop(struct btree_trans *trans,
730 struct btree_path *path)
732 struct bch_fs *c = trans->c;
733 struct bkey_cached *ck = (void *) path->l[0].b;
738 * We just did an update to the btree, bypassing the key cache: the key
739 * cache key is now stale and must be dropped, even if dirty:
741 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
742 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
743 atomic_long_dec(&c->btree_key_cache.nr_dirty);
744 bch2_journal_pin_drop(&c->journal, &ck->journal);
750 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
751 struct shrink_control *sc)
753 struct bch_fs *c = container_of(shrink, struct bch_fs,
754 btree_key_cache.shrink);
755 struct btree_key_cache *bc = &c->btree_key_cache;
756 struct bucket_table *tbl;
757 struct bkey_cached *ck, *t;
758 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
759 unsigned start, flags;
762 /* Return -1 if we can't do anything right now */
763 if (sc->gfp_mask & __GFP_FS)
764 mutex_lock(&bc->lock);
765 else if (!mutex_trylock(&bc->lock))
768 srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
769 flags = memalloc_nofs_save();
772 * Newest freed entries are at the end of the list - once we hit one
773 * that's too new to be freed, we can bail out:
775 list_for_each_entry_safe(ck, t, &bc->freed_nonpcpu, list) {
776 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
777 ck->btree_trans_barrier_seq))
781 six_lock_pcpu_free(&ck->c.lock);
782 kmem_cache_free(bch2_key_cache, ck);
783 atomic_long_dec(&bc->nr_freed);
791 list_for_each_entry_safe(ck, t, &bc->freed_pcpu, list) {
792 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
793 ck->btree_trans_barrier_seq))
797 six_lock_pcpu_free(&ck->c.lock);
798 kmem_cache_free(bch2_key_cache, ck);
799 atomic_long_dec(&bc->nr_freed);
808 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
809 if (bc->shrink_iter >= tbl->size)
811 start = bc->shrink_iter;
814 struct rhash_head *pos, *next;
816 pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
818 while (!rht_is_a_nulls(pos)) {
819 next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
820 ck = container_of(pos, struct bkey_cached, hash);
822 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
825 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
826 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
827 else if (bkey_cached_lock_for_evict(ck)) {
828 bkey_cached_evict(bc, ck);
829 bkey_cached_free(bc, ck);
840 if (bc->shrink_iter >= tbl->size)
842 } while (scanned < nr && bc->shrink_iter != start);
846 memalloc_nofs_restore(flags);
847 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
848 mutex_unlock(&bc->lock);
853 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
854 struct shrink_control *sc)
856 struct bch_fs *c = container_of(shrink, struct bch_fs,
857 btree_key_cache.shrink);
858 struct btree_key_cache *bc = &c->btree_key_cache;
859 long nr = atomic_long_read(&bc->nr_keys) -
860 atomic_long_read(&bc->nr_dirty);
865 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
867 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
868 struct bucket_table *tbl;
869 struct bkey_cached *ck, *n;
870 struct rhash_head *pos;
874 if (bc->shrink.list.next)
875 unregister_shrinker(&bc->shrink);
877 mutex_lock(&bc->lock);
880 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
882 for (i = 0; i < tbl->size; i++)
883 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
884 bkey_cached_evict(bc, ck);
885 list_add(&ck->list, &bc->freed_nonpcpu);
889 for_each_possible_cpu(cpu) {
890 struct btree_key_cache_freelist *f =
891 per_cpu_ptr(bc->pcpu_freed, cpu);
893 for (i = 0; i < f->nr; i++) {
895 list_add(&ck->list, &bc->freed_nonpcpu);
899 list_splice(&bc->freed_pcpu, &bc->freed_nonpcpu);
901 list_for_each_entry_safe(ck, n, &bc->freed_nonpcpu, list) {
904 bch2_journal_pin_drop(&c->journal, &ck->journal);
905 bch2_journal_preres_put(&c->journal, &ck->res);
909 six_lock_pcpu_free(&ck->c.lock);
910 kmem_cache_free(bch2_key_cache, ck);
913 BUG_ON(atomic_long_read(&bc->nr_dirty) &&
914 !bch2_journal_error(&c->journal) &&
915 test_bit(BCH_FS_WAS_RW, &c->flags));
916 BUG_ON(atomic_long_read(&bc->nr_keys));
918 mutex_unlock(&bc->lock);
920 if (bc->table_init_done)
921 rhashtable_destroy(&bc->table);
923 free_percpu(bc->pcpu_freed);
926 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
928 mutex_init(&c->lock);
929 INIT_LIST_HEAD(&c->freed_pcpu);
930 INIT_LIST_HEAD(&c->freed_nonpcpu);
933 static void bch2_btree_key_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
935 struct btree_key_cache *bc =
936 container_of(shrink, struct btree_key_cache, shrink);
938 bch2_btree_key_cache_to_text(out, bc);
941 int bch2_fs_btree_key_cache_init(struct btree_key_cache *bc)
943 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
946 bc->pcpu_freed = alloc_percpu(struct btree_key_cache_freelist);
950 ret = rhashtable_init(&bc->table, &bch2_btree_key_cache_params);
954 bc->table_init_done = true;
956 bc->shrink.seeks = 1;
957 bc->shrink.count_objects = bch2_btree_key_cache_count;
958 bc->shrink.scan_objects = bch2_btree_key_cache_scan;
959 bc->shrink.to_text = bch2_btree_key_cache_shrinker_to_text;
960 return register_shrinker(&bc->shrink, "%s/btree_key_cache", c->name);
963 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
965 prt_printf(out, "nr_freed:\t%zu\n", atomic_long_read(&c->nr_freed));
966 prt_printf(out, "nr_keys:\t%lu\n", atomic_long_read(&c->nr_keys));
967 prt_printf(out, "nr_dirty:\t%lu\n", atomic_long_read(&c->nr_dirty));
970 void bch2_btree_key_cache_exit(void)
973 kmem_cache_destroy(bch2_key_cache);
976 int __init bch2_btree_key_cache_init(void)
978 bch2_key_cache = KMEM_CACHE(bkey_cached, 0);