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"
10 #include "journal_reclaim.h"
12 #include <linux/sched/mm.h>
13 #include <trace/events/bcachefs.h>
15 static struct kmem_cache *bch2_key_cache;
17 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
20 const struct bkey_cached *ck = obj;
21 const struct bkey_cached_key *key = arg->key;
23 return cmp_int(ck->key.btree_id, key->btree_id) ?:
24 bpos_cmp(ck->key.pos, key->pos);
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,
35 inline struct bkey_cached *
36 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
38 struct bkey_cached_key key = {
43 return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
44 bch2_btree_key_cache_params);
47 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
49 if (!six_trylock_intent(&ck->c.lock))
52 if (!six_trylock_write(&ck->c.lock)) {
53 six_unlock_intent(&ck->c.lock);
57 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
58 six_unlock_write(&ck->c.lock);
59 six_unlock_intent(&ck->c.lock);
66 static void bkey_cached_evict(struct btree_key_cache *c,
67 struct bkey_cached *ck)
69 BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
70 bch2_btree_key_cache_params));
71 memset(&ck->key, ~0, sizeof(ck->key));
73 atomic_long_dec(&c->nr_keys);
76 static void bkey_cached_free(struct btree_key_cache *bc,
77 struct bkey_cached *ck)
79 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
81 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
83 ck->btree_trans_barrier_seq =
84 start_poll_synchronize_srcu(&c->btree_trans_barrier);
86 list_move_tail(&ck->list, &bc->freed);
87 atomic_long_inc(&bc->nr_freed);
93 six_unlock_write(&ck->c.lock);
94 six_unlock_intent(&ck->c.lock);
97 static void bkey_cached_free_fast(struct btree_key_cache *bc,
98 struct bkey_cached *ck)
100 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
101 struct btree_key_cache_freelist *f;
104 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
106 ck->btree_trans_barrier_seq =
107 start_poll_synchronize_srcu(&c->btree_trans_barrier);
109 list_del_init(&ck->list);
110 atomic_long_inc(&bc->nr_freed);
117 f = this_cpu_ptr(bc->pcpu_freed);
119 if (f->nr < ARRAY_SIZE(f->objs)) {
120 f->objs[f->nr++] = ck;
126 mutex_lock(&bc->lock);
128 f = this_cpu_ptr(bc->pcpu_freed);
130 while (f->nr > ARRAY_SIZE(f->objs) / 2) {
131 struct bkey_cached *ck2 = f->objs[--f->nr];
133 list_move_tail(&ck2->list, &bc->freed);
137 list_move_tail(&ck->list, &bc->freed);
138 mutex_unlock(&bc->lock);
141 six_unlock_write(&ck->c.lock);
142 six_unlock_intent(&ck->c.lock);
145 static struct bkey_cached *
146 bkey_cached_alloc(struct btree_key_cache *c)
148 struct bkey_cached *ck = NULL;
149 struct btree_key_cache_freelist *f;
152 f = this_cpu_ptr(c->pcpu_freed);
154 ck = f->objs[--f->nr];
158 mutex_lock(&c->lock);
160 f = this_cpu_ptr(c->pcpu_freed);
162 while (!list_empty(&c->freed) &&
163 f->nr < ARRAY_SIZE(f->objs) / 2) {
164 ck = list_last_entry(&c->freed, struct bkey_cached, list);
165 list_del_init(&ck->list);
166 f->objs[f->nr++] = ck;
169 ck = f->nr ? f->objs[--f->nr] : NULL;
171 mutex_unlock(&c->lock);
175 six_lock_intent(&ck->c.lock, NULL, NULL);
176 six_lock_write(&ck->c.lock, NULL, NULL);
180 ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
182 INIT_LIST_HEAD(&ck->list);
183 six_lock_init(&ck->c.lock);
184 BUG_ON(!six_trylock_intent(&ck->c.lock));
185 BUG_ON(!six_trylock_write(&ck->c.lock));
192 static struct bkey_cached *
193 bkey_cached_reuse(struct btree_key_cache *c)
195 struct bucket_table *tbl;
196 struct rhash_head *pos;
197 struct bkey_cached *ck;
201 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
202 for (i = 0; i < tbl->size; i++)
203 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
204 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
205 bkey_cached_lock_for_evict(ck)) {
206 bkey_cached_evict(c, ck);
216 static struct bkey_cached *
217 btree_key_cache_create(struct bch_fs *c,
218 enum btree_id btree_id,
221 struct btree_key_cache *bc = &c->btree_key_cache;
222 struct bkey_cached *ck;
225 ck = bkey_cached_alloc(bc);
228 ck = bkey_cached_reuse(bc);
230 bch_err(c, "error allocating memory for key cache item, btree %s",
231 bch2_btree_ids[btree_id]);
232 return ERR_PTR(-ENOMEM);
237 if (btree_id == BTREE_ID_subvolumes)
238 six_lock_pcpu_alloc(&ck->c.lock);
240 six_lock_pcpu_free(&ck->c.lock);
244 ck->c.btree_id = btree_id;
245 ck->key.btree_id = btree_id;
248 ck->flags = 1U << BKEY_CACHED_ACCESSED;
250 if (unlikely(rhashtable_lookup_insert_fast(&bc->table,
252 bch2_btree_key_cache_params))) {
253 /* We raced with another fill: */
255 if (likely(was_new)) {
256 six_unlock_write(&ck->c.lock);
257 six_unlock_intent(&ck->c.lock);
260 bkey_cached_free_fast(bc, ck);
266 atomic_long_inc(&bc->nr_keys);
268 six_unlock_write(&ck->c.lock);
273 static int btree_key_cache_fill(struct btree_trans *trans,
274 struct btree_path *ck_path,
275 struct bkey_cached *ck)
277 struct btree_path *path;
279 unsigned new_u64s = 0;
280 struct bkey_i *new_k = NULL;
284 path = bch2_path_get(trans, ck->key.btree_id,
285 ck->key.pos, 0, 0, 0, _THIS_IP_);
286 ret = bch2_btree_path_traverse(trans, path, 0);
290 k = bch2_btree_path_peek_slot(path, &u);
292 if (!bch2_btree_node_relock(trans, ck_path, 0)) {
293 trace_trans_restart_relock_key_cache_fill(trans->fn,
294 _THIS_IP_, ck_path->btree_id, &ck_path->pos);
295 ret = btree_trans_restart(trans);
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 return ck->key.btree_id == path->btree_id &&
351 !bpos_cmp(ck->key.pos, path->pos) ? 0 : -1;
355 int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
358 struct bch_fs *c = trans->c;
359 struct bkey_cached *ck;
366 if (bch2_btree_node_relock(trans, path, 0)) {
367 ck = (void *) path->l[0].b;
371 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
373 if (flags & BTREE_ITER_CACHED_NOCREATE) {
378 ck = btree_key_cache_create(c, path->btree_id, path->pos);
379 ret = PTR_ERR_OR_ZERO(ck);
385 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
386 path->locks_want = 1;
388 enum six_lock_type lock_want = __btree_lock_want(path, 0);
390 if (!btree_node_lock(trans, path, (void *) ck, path->pos, 0,
392 bkey_cached_check_fn, path, _THIS_IP_)) {
393 if (!trans->restarted)
400 if (ck->key.btree_id != path->btree_id ||
401 bpos_cmp(ck->key.pos, path->pos)) {
402 six_unlock_type(&ck->c.lock, lock_want);
406 mark_btree_node_locked(trans, path, 0, lock_want);
409 path->l[0].lock_seq = ck->c.lock.state.seq;
410 path->l[0].b = (void *) ck;
412 if (!ck->valid && !(flags & BTREE_ITER_CACHED_NOFILL)) {
413 if (!path->locks_want &&
414 !__bch2_btree_path_upgrade(trans, path, 1)) {
415 trace_transaction_restart_ip(trans->fn, _THIS_IP_);
416 ret = btree_trans_restart(trans);
420 ret = btree_key_cache_fill(trans, path, ck);
425 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
426 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
428 path->uptodate = BTREE_ITER_UPTODATE;
429 BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
434 btree_node_unlock(path, 0);
435 path->l[0].b = BTREE_ITER_NO_NODE_ERROR;
440 static int btree_key_cache_flush_pos(struct btree_trans *trans,
441 struct bkey_cached_key key,
443 unsigned commit_flags,
446 struct bch_fs *c = trans->c;
447 struct journal *j = &c->journal;
448 struct btree_iter c_iter, b_iter;
449 struct bkey_cached *ck = NULL;
452 bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
455 BTREE_ITER_ALL_SNAPSHOTS);
456 bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
458 BTREE_ITER_CACHED_NOFILL|
459 BTREE_ITER_CACHED_NOCREATE|
461 b_iter.flags &= ~BTREE_ITER_WITH_KEY_CACHE;
463 ret = bch2_btree_iter_traverse(&c_iter);
467 ck = (void *) c_iter.path->l[0].b;
471 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
479 if (journal_seq && ck->journal.seq != journal_seq)
483 * Since journal reclaim depends on us making progress here, and the
484 * allocator/copygc depend on journal reclaim making progress, we need
485 * to be using alloc reserves:
487 ret = bch2_btree_iter_traverse(&b_iter) ?:
488 bch2_trans_update(trans, &b_iter, ck->k,
489 BTREE_UPDATE_KEY_CACHE_RECLAIM|
490 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
491 BTREE_TRIGGER_NORUN) ?:
492 bch2_trans_commit(trans, NULL, NULL,
493 BTREE_INSERT_NOCHECK_RW|
495 BTREE_INSERT_USE_RESERVE|
496 (ck->journal.seq == journal_last_seq(j)
497 ? JOURNAL_WATERMARK_reserved
501 bch2_fs_fatal_err_on(ret != -EINTR &&
503 !bch2_journal_error(j), c,
504 "error flushing key cache: %i", ret);
508 bch2_journal_pin_drop(j, &ck->journal);
509 bch2_journal_preres_put(j, &ck->res);
511 BUG_ON(!btree_node_locked(c_iter.path, 0));
514 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
515 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
516 atomic_long_dec(&c->btree_key_cache.nr_dirty);
520 BUG_ON(!btree_node_intent_locked(c_iter.path, 0));
522 mark_btree_node_unlocked(c_iter.path, 0);
523 c_iter.path->l[0].b = NULL;
525 six_lock_write(&ck->c.lock, NULL, NULL);
527 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
528 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
529 atomic_long_dec(&c->btree_key_cache.nr_dirty);
532 bkey_cached_evict(&c->btree_key_cache, ck);
534 bkey_cached_free_fast(&c->btree_key_cache, ck);
537 bch2_trans_iter_exit(trans, &b_iter);
538 bch2_trans_iter_exit(trans, &c_iter);
542 int bch2_btree_key_cache_journal_flush(struct journal *j,
543 struct journal_entry_pin *pin, u64 seq)
545 struct bch_fs *c = container_of(j, struct bch_fs, journal);
546 struct bkey_cached *ck =
547 container_of(pin, struct bkey_cached, journal);
548 struct bkey_cached_key key;
551 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
553 six_lock_read(&ck->c.lock, NULL, NULL);
556 if (ck->journal.seq != seq ||
557 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
558 six_unlock_read(&ck->c.lock);
561 six_unlock_read(&ck->c.lock);
563 ret = bch2_trans_do(c, NULL, NULL, 0,
564 btree_key_cache_flush_pos(&trans, key, seq,
565 BTREE_INSERT_JOURNAL_RECLAIM, false));
567 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
573 * Flush and evict a key from the key cache:
575 int bch2_btree_key_cache_flush(struct btree_trans *trans,
576 enum btree_id id, struct bpos pos)
578 struct bch_fs *c = trans->c;
579 struct bkey_cached_key key = { id, pos };
581 /* Fastpath - assume it won't be found: */
582 if (!bch2_btree_key_cache_find(c, id, pos))
585 return btree_key_cache_flush_pos(trans, key, 0, 0, true);
588 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
589 struct btree_path *path,
590 struct bkey_i *insert)
592 struct bch_fs *c = trans->c;
593 struct bkey_cached *ck = (void *) path->l[0].b;
594 bool kick_reclaim = false;
596 BUG_ON(insert->u64s > ck->u64s);
598 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
601 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
603 difference = jset_u64s(insert->u64s) - ck->res.u64s;
604 if (difference > 0) {
605 trans->journal_preres.u64s -= difference;
606 ck->res.u64s += difference;
610 bkey_copy(ck->k, insert);
613 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
614 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
615 atomic_long_inc(&c->btree_key_cache.nr_dirty);
617 if (bch2_nr_btree_keys_need_flush(c))
621 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
622 &ck->journal, bch2_btree_key_cache_journal_flush);
625 journal_reclaim_kick(&c->journal);
629 void bch2_btree_key_cache_drop(struct btree_trans *trans,
630 struct btree_path *path)
632 struct bkey_cached *ck = (void *) path->l[0].b;
636 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
639 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
640 struct shrink_control *sc)
642 struct bch_fs *c = container_of(shrink, struct bch_fs,
643 btree_key_cache.shrink);
644 struct btree_key_cache *bc = &c->btree_key_cache;
645 struct bucket_table *tbl;
646 struct bkey_cached *ck, *t;
647 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
648 unsigned start, flags;
651 /* Return -1 if we can't do anything right now */
652 if (sc->gfp_mask & __GFP_FS)
653 mutex_lock(&bc->lock);
654 else if (!mutex_trylock(&bc->lock))
657 srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
658 flags = memalloc_nofs_save();
661 * Newest freed entries are at the end of the list - once we hit one
662 * that's too new to be freed, we can bail out:
664 list_for_each_entry_safe(ck, t, &bc->freed, list) {
665 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
666 ck->btree_trans_barrier_seq))
670 kmem_cache_free(bch2_key_cache, ck);
671 atomic_long_dec(&bc->nr_freed);
680 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
681 if (bc->shrink_iter >= tbl->size)
683 start = bc->shrink_iter;
686 struct rhash_head *pos, *next;
688 pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
690 while (!rht_is_a_nulls(pos)) {
691 next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
692 ck = container_of(pos, struct bkey_cached, hash);
694 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
697 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
698 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
699 else if (bkey_cached_lock_for_evict(ck)) {
700 bkey_cached_evict(bc, ck);
701 bkey_cached_free(bc, ck);
712 if (bc->shrink_iter >= tbl->size)
714 } while (scanned < nr && bc->shrink_iter != start);
718 memalloc_nofs_restore(flags);
719 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
720 mutex_unlock(&bc->lock);
725 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
726 struct shrink_control *sc)
728 struct bch_fs *c = container_of(shrink, struct bch_fs,
729 btree_key_cache.shrink);
730 struct btree_key_cache *bc = &c->btree_key_cache;
731 long nr = atomic_long_read(&bc->nr_keys) -
732 atomic_long_read(&bc->nr_dirty);
737 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
739 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
740 struct bucket_table *tbl;
741 struct bkey_cached *ck, *n;
742 struct rhash_head *pos;
746 if (bc->shrink.list.next)
747 unregister_shrinker(&bc->shrink);
749 mutex_lock(&bc->lock);
752 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
754 for (i = 0; i < tbl->size; i++)
755 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
756 bkey_cached_evict(bc, ck);
757 list_add(&ck->list, &bc->freed);
761 for_each_possible_cpu(cpu) {
762 struct btree_key_cache_freelist *f =
763 per_cpu_ptr(bc->pcpu_freed, cpu);
765 for (i = 0; i < f->nr; i++) {
767 list_add(&ck->list, &bc->freed);
771 list_for_each_entry_safe(ck, n, &bc->freed, list) {
774 bch2_journal_pin_drop(&c->journal, &ck->journal);
775 bch2_journal_preres_put(&c->journal, &ck->res);
779 kmem_cache_free(bch2_key_cache, ck);
782 BUG_ON(atomic_long_read(&bc->nr_dirty) &&
783 !bch2_journal_error(&c->journal) &&
784 test_bit(BCH_FS_WAS_RW, &c->flags));
785 BUG_ON(atomic_long_read(&bc->nr_keys));
787 mutex_unlock(&bc->lock);
789 if (bc->table_init_done)
790 rhashtable_destroy(&bc->table);
792 free_percpu(bc->pcpu_freed);
795 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
797 mutex_init(&c->lock);
798 INIT_LIST_HEAD(&c->freed);
801 static void bch2_btree_key_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
803 struct btree_key_cache *bc =
804 container_of(shrink, struct btree_key_cache, shrink);
806 bch2_btree_key_cache_to_text(out, bc);
809 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
813 c->pcpu_freed = alloc_percpu(struct btree_key_cache_freelist);
817 ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
821 c->table_init_done = true;
824 c->shrink.count_objects = bch2_btree_key_cache_count;
825 c->shrink.scan_objects = bch2_btree_key_cache_scan;
826 c->shrink.to_text = bch2_btree_key_cache_shrinker_to_text;
827 return register_shrinker(&c->shrink);
830 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
832 prt_printf(out, "nr_freed:\t%zu\n", atomic_long_read(&c->nr_freed));
833 prt_printf(out, "nr_keys:\t%lu\n", atomic_long_read(&c->nr_keys));
834 prt_printf(out, "nr_dirty:\t%lu\n", atomic_long_read(&c->nr_dirty));
837 void bch2_btree_key_cache_exit(void)
840 kmem_cache_destroy(bch2_key_cache);
843 int __init bch2_btree_key_cache_init(void)
845 bch2_key_cache = KMEM_CACHE(bkey_cached, 0);