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 struct kmem_cache *bch2_key_cache;
18 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
21 const struct bkey_cached *ck = obj;
22 const struct bkey_cached_key *key = arg->key;
24 return cmp_int(ck->key.btree_id, key->btree_id) ?:
25 bpos_cmp(ck->key.pos, key->pos);
28 static const struct rhashtable_params bch2_btree_key_cache_params = {
29 .head_offset = offsetof(struct bkey_cached, hash),
30 .key_offset = offsetof(struct bkey_cached, key),
31 .key_len = sizeof(struct bkey_cached_key),
32 .obj_cmpfn = bch2_btree_key_cache_cmp_fn,
36 inline struct bkey_cached *
37 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
39 struct bkey_cached_key key = {
44 return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
45 bch2_btree_key_cache_params);
48 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
50 if (!six_trylock_intent(&ck->c.lock))
53 if (!six_trylock_write(&ck->c.lock)) {
54 six_unlock_intent(&ck->c.lock);
58 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
59 six_unlock_write(&ck->c.lock);
60 six_unlock_intent(&ck->c.lock);
67 static void bkey_cached_evict(struct btree_key_cache *c,
68 struct bkey_cached *ck)
70 BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
71 bch2_btree_key_cache_params));
72 memset(&ck->key, ~0, sizeof(ck->key));
74 atomic_long_dec(&c->nr_keys);
77 static void bkey_cached_free(struct btree_key_cache *bc,
78 struct bkey_cached *ck)
80 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
82 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
84 ck->btree_trans_barrier_seq =
85 start_poll_synchronize_srcu(&c->btree_trans_barrier);
87 list_move_tail(&ck->list, &bc->freed);
88 atomic_long_inc(&bc->nr_freed);
94 six_unlock_write(&ck->c.lock);
95 six_unlock_intent(&ck->c.lock);
98 static void bkey_cached_free_fast(struct btree_key_cache *bc,
99 struct bkey_cached *ck)
101 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
102 struct btree_key_cache_freelist *f;
105 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
107 ck->btree_trans_barrier_seq =
108 start_poll_synchronize_srcu(&c->btree_trans_barrier);
110 list_del_init(&ck->list);
111 atomic_long_inc(&bc->nr_freed);
118 f = this_cpu_ptr(bc->pcpu_freed);
120 if (f->nr < ARRAY_SIZE(f->objs)) {
121 f->objs[f->nr++] = ck;
127 mutex_lock(&bc->lock);
129 f = this_cpu_ptr(bc->pcpu_freed);
131 while (f->nr > ARRAY_SIZE(f->objs) / 2) {
132 struct bkey_cached *ck2 = f->objs[--f->nr];
134 list_move_tail(&ck2->list, &bc->freed);
138 list_move_tail(&ck->list, &bc->freed);
139 mutex_unlock(&bc->lock);
142 six_unlock_write(&ck->c.lock);
143 six_unlock_intent(&ck->c.lock);
146 static struct bkey_cached *
147 bkey_cached_alloc(struct btree_key_cache *c)
149 struct bkey_cached *ck = NULL;
150 struct btree_key_cache_freelist *f;
153 f = this_cpu_ptr(c->pcpu_freed);
155 ck = f->objs[--f->nr];
159 mutex_lock(&c->lock);
161 f = this_cpu_ptr(c->pcpu_freed);
163 while (!list_empty(&c->freed) &&
164 f->nr < ARRAY_SIZE(f->objs) / 2) {
165 ck = list_last_entry(&c->freed, struct bkey_cached, list);
166 list_del_init(&ck->list);
167 f->objs[f->nr++] = ck;
170 ck = f->nr ? f->objs[--f->nr] : NULL;
172 mutex_unlock(&c->lock);
176 six_lock_intent(&ck->c.lock, NULL, NULL);
177 six_lock_write(&ck->c.lock, NULL, NULL);
181 ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
183 INIT_LIST_HEAD(&ck->list);
184 six_lock_init(&ck->c.lock);
185 BUG_ON(!six_trylock_intent(&ck->c.lock));
186 BUG_ON(!six_trylock_write(&ck->c.lock));
193 static struct bkey_cached *
194 bkey_cached_reuse(struct btree_key_cache *c)
196 struct bucket_table *tbl;
197 struct rhash_head *pos;
198 struct bkey_cached *ck;
202 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
203 for (i = 0; i < tbl->size; i++)
204 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
205 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
206 bkey_cached_lock_for_evict(ck)) {
207 bkey_cached_evict(c, ck);
217 static struct bkey_cached *
218 btree_key_cache_create(struct bch_fs *c,
219 enum btree_id btree_id,
222 struct btree_key_cache *bc = &c->btree_key_cache;
223 struct bkey_cached *ck;
226 ck = bkey_cached_alloc(bc);
229 ck = bkey_cached_reuse(bc);
231 bch_err(c, "error allocating memory for key cache item, btree %s",
232 bch2_btree_ids[btree_id]);
233 return ERR_PTR(-ENOMEM);
238 if (btree_id == BTREE_ID_subvolumes)
239 six_lock_pcpu_alloc(&ck->c.lock);
241 six_lock_pcpu_free(&ck->c.lock);
245 ck->c.btree_id = btree_id;
246 ck->key.btree_id = btree_id;
249 ck->flags = 1U << BKEY_CACHED_ACCESSED;
251 if (unlikely(rhashtable_lookup_insert_fast(&bc->table,
253 bch2_btree_key_cache_params))) {
254 /* We raced with another fill: */
256 if (likely(was_new)) {
257 six_unlock_write(&ck->c.lock);
258 six_unlock_intent(&ck->c.lock);
261 bkey_cached_free_fast(bc, ck);
267 atomic_long_inc(&bc->nr_keys);
269 six_unlock_write(&ck->c.lock);
274 static int btree_key_cache_fill(struct btree_trans *trans,
275 struct btree_path *ck_path,
276 struct bkey_cached *ck)
278 struct btree_path *path;
280 unsigned new_u64s = 0;
281 struct bkey_i *new_k = NULL;
285 path = bch2_path_get(trans, ck->key.btree_id,
286 ck->key.pos, 0, 0, 0, _THIS_IP_);
287 ret = bch2_btree_path_traverse(trans, path, 0);
291 k = bch2_btree_path_peek_slot(path, &u);
293 if (!bch2_btree_node_relock(trans, ck_path, 0)) {
294 trace_trans_restart_relock_key_cache_fill(trans, _THIS_IP_, ck_path);
295 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
300 * bch2_varint_decode can read past the end of the buffer by at
301 * most 7 bytes (it won't be used):
303 new_u64s = k.k->u64s + 1;
306 * Allocate some extra space so that the transaction commit path is less
307 * likely to have to reallocate, since that requires a transaction
310 new_u64s = min(256U, (new_u64s * 3) / 2);
312 if (new_u64s > ck->u64s) {
313 new_u64s = roundup_pow_of_two(new_u64s);
314 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
316 bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
317 bch2_btree_ids[ck->key.btree_id], new_u64s);
324 * XXX: not allowed to be holding read locks when we take a write lock,
327 bch2_btree_node_lock_write(trans, ck_path, ck_path->l[0].b);
334 bkey_reassemble(ck->k, k);
336 bch2_btree_node_unlock_write(trans, ck_path, ck_path->l[0].b);
338 /* We're not likely to need this iterator again: */
339 path->preserve = false;
341 bch2_path_put(trans, path, 0);
345 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
347 struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
348 const struct btree_path *path = p;
350 if (ck->key.btree_id != path->btree_id &&
351 bpos_cmp(ck->key.pos, path->pos))
352 return BCH_ERR_lock_fail_node_reused;
357 int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
360 struct bch_fs *c = trans->c;
361 struct bkey_cached *ck;
368 if (bch2_btree_node_relock(trans, path, 0)) {
369 ck = (void *) path->l[0].b;
373 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
375 ck = btree_key_cache_create(c, path->btree_id, path->pos);
376 ret = PTR_ERR_OR_ZERO(ck);
382 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
383 path->locks_want = 1;
385 enum six_lock_type lock_want = __btree_lock_want(path, 0);
387 ret = btree_node_lock(trans, path, (void *) ck, path->pos, 0,
389 bkey_cached_check_fn, path, _THIS_IP_);
391 if (bch2_err_matches(ret, BCH_ERR_lock_fail_node_reused))
393 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
398 if (ck->key.btree_id != path->btree_id ||
399 bpos_cmp(ck->key.pos, path->pos)) {
400 six_unlock_type(&ck->c.lock, lock_want);
404 mark_btree_node_locked(trans, path, 0, lock_want);
407 path->l[0].lock_seq = ck->c.lock.state.seq;
408 path->l[0].b = (void *) ck;
412 * Using the underscore version because we haven't set
413 * path->uptodate yet:
415 if (!path->locks_want &&
416 !__bch2_btree_path_upgrade(trans, path, 1)) {
417 trace_transaction_restart_key_cache_upgrade(trans, _THIS_IP_);
418 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_upgrade);
422 ret = btree_key_cache_fill(trans, path, ck);
427 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
428 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
430 path->uptodate = BTREE_ITER_UPTODATE;
432 BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
436 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
437 btree_node_unlock(trans, path, 0);
438 path->l[0].b = ERR_PTR(ret);
443 static int btree_key_cache_flush_pos(struct btree_trans *trans,
444 struct bkey_cached_key key,
446 unsigned commit_flags,
449 struct bch_fs *c = trans->c;
450 struct journal *j = &c->journal;
451 struct btree_iter c_iter, b_iter;
452 struct bkey_cached *ck = NULL;
455 bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
458 BTREE_ITER_ALL_SNAPSHOTS);
459 bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
462 b_iter.flags &= ~BTREE_ITER_WITH_KEY_CACHE;
464 ret = bch2_btree_iter_traverse(&c_iter);
468 ck = (void *) c_iter.path->l[0].b;
472 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
480 if (journal_seq && ck->journal.seq != journal_seq)
484 * Since journal reclaim depends on us making progress here, and the
485 * allocator/copygc depend on journal reclaim making progress, we need
486 * to be using alloc reserves:
488 ret = bch2_btree_iter_traverse(&b_iter) ?:
489 bch2_trans_update(trans, &b_iter, ck->k,
490 BTREE_UPDATE_KEY_CACHE_RECLAIM|
491 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
492 BTREE_TRIGGER_NORUN) ?:
493 bch2_trans_commit(trans, NULL, NULL,
494 BTREE_INSERT_NOCHECK_RW|
496 BTREE_INSERT_USE_RESERVE|
497 (ck->journal.seq == journal_last_seq(j)
498 ? JOURNAL_WATERMARK_reserved
502 bch2_fs_fatal_err_on(ret &&
503 !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
504 !bch2_err_matches(ret, BCH_ERR_journal_reclaim_would_deadlock) &&
505 !bch2_journal_error(j), c,
506 "error flushing key cache: %s", bch2_err_str(ret));
510 bch2_journal_pin_drop(j, &ck->journal);
511 bch2_journal_preres_put(j, &ck->res);
513 BUG_ON(!btree_node_locked(c_iter.path, 0));
516 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
517 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
518 atomic_long_dec(&c->btree_key_cache.nr_dirty);
522 BUG_ON(!btree_node_intent_locked(c_iter.path, 0));
524 mark_btree_node_unlocked(c_iter.path, 0);
525 c_iter.path->l[0].b = NULL;
527 six_lock_write(&ck->c.lock, NULL, NULL);
529 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
530 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
531 atomic_long_dec(&c->btree_key_cache.nr_dirty);
534 bkey_cached_evict(&c->btree_key_cache, ck);
536 bkey_cached_free_fast(&c->btree_key_cache, ck);
539 bch2_trans_iter_exit(trans, &b_iter);
540 bch2_trans_iter_exit(trans, &c_iter);
544 int bch2_btree_key_cache_journal_flush(struct journal *j,
545 struct journal_entry_pin *pin, u64 seq)
547 struct bch_fs *c = container_of(j, struct bch_fs, journal);
548 struct bkey_cached *ck =
549 container_of(pin, struct bkey_cached, journal);
550 struct bkey_cached_key key;
553 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
555 six_lock_read(&ck->c.lock, NULL, NULL);
558 if (ck->journal.seq != seq ||
559 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
560 six_unlock_read(&ck->c.lock);
563 six_unlock_read(&ck->c.lock);
565 ret = bch2_trans_do(c, NULL, NULL, 0,
566 btree_key_cache_flush_pos(&trans, key, seq,
567 BTREE_INSERT_JOURNAL_RECLAIM, false));
569 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
575 * Flush and evict a key from the key cache:
577 int bch2_btree_key_cache_flush(struct btree_trans *trans,
578 enum btree_id id, struct bpos pos)
580 struct bch_fs *c = trans->c;
581 struct bkey_cached_key key = { id, pos };
583 /* Fastpath - assume it won't be found: */
584 if (!bch2_btree_key_cache_find(c, id, pos))
587 return btree_key_cache_flush_pos(trans, key, 0, 0, true);
590 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
591 struct btree_path *path,
592 struct bkey_i *insert)
594 struct bch_fs *c = trans->c;
595 struct bkey_cached *ck = (void *) path->l[0].b;
596 bool kick_reclaim = false;
598 BUG_ON(insert->u64s > ck->u64s);
600 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
603 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
605 difference = jset_u64s(insert->u64s) - ck->res.u64s;
606 if (difference > 0) {
607 trans->journal_preres.u64s -= difference;
608 ck->res.u64s += difference;
612 bkey_copy(ck->k, insert);
615 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
616 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
617 atomic_long_inc(&c->btree_key_cache.nr_dirty);
619 if (bch2_nr_btree_keys_need_flush(c))
623 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
624 &ck->journal, bch2_btree_key_cache_journal_flush);
627 journal_reclaim_kick(&c->journal);
631 void bch2_btree_key_cache_drop(struct btree_trans *trans,
632 struct btree_path *path)
634 struct bkey_cached *ck = (void *) path->l[0].b;
638 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
641 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
642 struct shrink_control *sc)
644 struct bch_fs *c = container_of(shrink, struct bch_fs,
645 btree_key_cache.shrink);
646 struct btree_key_cache *bc = &c->btree_key_cache;
647 struct bucket_table *tbl;
648 struct bkey_cached *ck, *t;
649 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
650 unsigned start, flags;
653 /* Return -1 if we can't do anything right now */
654 if (sc->gfp_mask & __GFP_FS)
655 mutex_lock(&bc->lock);
656 else if (!mutex_trylock(&bc->lock))
659 srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
660 flags = memalloc_nofs_save();
663 * Newest freed entries are at the end of the list - once we hit one
664 * that's too new to be freed, we can bail out:
666 list_for_each_entry_safe(ck, t, &bc->freed, list) {
667 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
668 ck->btree_trans_barrier_seq))
672 kmem_cache_free(bch2_key_cache, ck);
673 atomic_long_dec(&bc->nr_freed);
682 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
683 if (bc->shrink_iter >= tbl->size)
685 start = bc->shrink_iter;
688 struct rhash_head *pos, *next;
690 pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
692 while (!rht_is_a_nulls(pos)) {
693 next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
694 ck = container_of(pos, struct bkey_cached, hash);
696 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
699 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
700 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
701 else if (bkey_cached_lock_for_evict(ck)) {
702 bkey_cached_evict(bc, ck);
703 bkey_cached_free(bc, ck);
714 if (bc->shrink_iter >= tbl->size)
716 } while (scanned < nr && bc->shrink_iter != start);
720 memalloc_nofs_restore(flags);
721 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
722 mutex_unlock(&bc->lock);
727 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
728 struct shrink_control *sc)
730 struct bch_fs *c = container_of(shrink, struct bch_fs,
731 btree_key_cache.shrink);
732 struct btree_key_cache *bc = &c->btree_key_cache;
733 long nr = atomic_long_read(&bc->nr_keys) -
734 atomic_long_read(&bc->nr_dirty);
739 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
741 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
742 struct bucket_table *tbl;
743 struct bkey_cached *ck, *n;
744 struct rhash_head *pos;
748 if (bc->shrink.list.next)
749 unregister_shrinker(&bc->shrink);
751 mutex_lock(&bc->lock);
754 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
756 for (i = 0; i < tbl->size; i++)
757 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
758 bkey_cached_evict(bc, ck);
759 list_add(&ck->list, &bc->freed);
763 for_each_possible_cpu(cpu) {
764 struct btree_key_cache_freelist *f =
765 per_cpu_ptr(bc->pcpu_freed, cpu);
767 for (i = 0; i < f->nr; i++) {
769 list_add(&ck->list, &bc->freed);
773 list_for_each_entry_safe(ck, n, &bc->freed, list) {
776 bch2_journal_pin_drop(&c->journal, &ck->journal);
777 bch2_journal_preres_put(&c->journal, &ck->res);
781 kmem_cache_free(bch2_key_cache, ck);
784 BUG_ON(atomic_long_read(&bc->nr_dirty) &&
785 !bch2_journal_error(&c->journal) &&
786 test_bit(BCH_FS_WAS_RW, &c->flags));
787 BUG_ON(atomic_long_read(&bc->nr_keys));
789 mutex_unlock(&bc->lock);
791 if (bc->table_init_done)
792 rhashtable_destroy(&bc->table);
794 free_percpu(bc->pcpu_freed);
797 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
799 mutex_init(&c->lock);
800 INIT_LIST_HEAD(&c->freed);
803 static void bch2_btree_key_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
805 struct btree_key_cache *bc =
806 container_of(shrink, struct btree_key_cache, shrink);
808 bch2_btree_key_cache_to_text(out, bc);
811 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
815 c->pcpu_freed = alloc_percpu(struct btree_key_cache_freelist);
819 ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
823 c->table_init_done = true;
826 c->shrink.count_objects = bch2_btree_key_cache_count;
827 c->shrink.scan_objects = bch2_btree_key_cache_scan;
828 c->shrink.to_text = bch2_btree_key_cache_shrinker_to_text;
829 return register_shrinker(&c->shrink);
832 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
834 prt_printf(out, "nr_freed:\t%zu\n", atomic_long_read(&c->nr_freed));
835 prt_printf(out, "nr_keys:\t%lu\n", atomic_long_read(&c->nr_keys));
836 prt_printf(out, "nr_dirty:\t%lu\n", atomic_long_read(&c->nr_dirty));
839 void bch2_btree_key_cache_exit(void)
842 kmem_cache_destroy(bch2_key_cache);
845 int __init bch2_btree_key_cache_init(void)
847 bch2_key_cache = KMEM_CACHE(bkey_cached, 0);