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);
93 six_unlock_write(&ck->c.lock);
94 six_unlock_intent(&ck->c.lock);
97 static struct bkey_cached *
98 bkey_cached_alloc(struct btree_key_cache *c)
100 struct bkey_cached *ck;
102 ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
104 INIT_LIST_HEAD(&ck->list);
105 six_lock_init(&ck->c.lock);
106 BUG_ON(!six_trylock_intent(&ck->c.lock));
107 BUG_ON(!six_trylock_write(&ck->c.lock));
114 static struct bkey_cached *
115 bkey_cached_reuse(struct btree_key_cache *c)
117 struct bucket_table *tbl;
118 struct rhash_head *pos;
119 struct bkey_cached *ck;
122 mutex_lock(&c->lock);
123 list_for_each_entry_reverse(ck, &c->freed, list)
124 if (bkey_cached_lock_for_evict(ck)) {
127 mutex_unlock(&c->lock);
130 mutex_unlock(&c->lock);
133 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
134 for (i = 0; i < tbl->size; i++)
135 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
136 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
137 bkey_cached_lock_for_evict(ck)) {
138 bkey_cached_evict(c, ck);
148 static struct bkey_cached *
149 btree_key_cache_create(struct btree_key_cache *c,
150 enum btree_id btree_id,
153 struct bkey_cached *ck;
156 ck = bkey_cached_alloc(c);
159 ck = bkey_cached_reuse(c);
161 return ERR_PTR(-ENOMEM);
167 ck->c.btree_id = btree_id;
168 ck->key.btree_id = btree_id;
171 ck->flags = 1U << BKEY_CACHED_ACCESSED;
173 if (unlikely(rhashtable_lookup_insert_fast(&c->table,
175 bch2_btree_key_cache_params))) {
176 /* We raced with another fill: */
178 if (likely(was_new)) {
179 six_unlock_write(&ck->c.lock);
180 six_unlock_intent(&ck->c.lock);
183 mutex_lock(&c->lock);
184 bkey_cached_free(c, ck);
185 mutex_unlock(&c->lock);
191 atomic_long_inc(&c->nr_keys);
193 six_unlock_write(&ck->c.lock);
198 static int btree_key_cache_fill(struct btree_trans *trans,
199 struct btree_iter *ck_iter,
200 struct bkey_cached *ck)
202 struct btree_iter *iter;
204 unsigned new_u64s = 0;
205 struct bkey_i *new_k = NULL;
208 iter = bch2_trans_get_iter(trans, ck->key.btree_id,
209 ck->key.pos, BTREE_ITER_SLOTS);
210 k = bch2_btree_iter_peek_slot(iter);
215 if (!bch2_btree_node_relock(ck_iter, 0)) {
216 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
221 if (k.k->u64s > ck->u64s) {
222 new_u64s = roundup_pow_of_two(k.k->u64s);
223 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
230 bch2_btree_node_lock_write(ck_iter->l[0].b, ck_iter);
237 bkey_reassemble(ck->k, k);
239 bch2_btree_node_unlock_write(ck_iter->l[0].b, ck_iter);
241 /* We're not likely to need this iterator again: */
242 set_btree_iter_dontneed(trans, iter);
244 bch2_trans_iter_put(trans, iter);
248 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
250 struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
251 const struct btree_iter *iter = p;
253 return ck->key.btree_id == iter->btree_id &&
254 !bpos_cmp(ck->key.pos, iter->pos) ? 0 : -1;
258 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
260 struct btree_trans *trans = iter->trans;
261 struct bch_fs *c = trans->c;
262 struct bkey_cached *ck;
267 if (btree_node_locked(iter, 0)) {
268 ck = (void *) iter->l[0].b;
272 ck = bch2_btree_key_cache_find(c, iter->btree_id, iter->pos);
274 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
279 ck = btree_key_cache_create(&c->btree_key_cache,
280 iter->btree_id, iter->pos);
281 ret = PTR_ERR_OR_ZERO(ck);
287 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
288 iter->locks_want = 1;
290 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
292 if (!btree_node_lock((void *) ck, iter->pos, 0, iter, lock_want,
293 bkey_cached_check_fn, iter, _THIS_IP_)) {
294 if (ck->key.btree_id != iter->btree_id ||
295 bpos_cmp(ck->key.pos, iter->pos)) {
299 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
304 if (ck->key.btree_id != iter->btree_id ||
305 bpos_cmp(ck->key.pos, iter->pos)) {
306 six_unlock_type(&ck->c.lock, lock_want);
310 mark_btree_node_locked(iter, 0, lock_want);
313 iter->l[0].lock_seq = ck->c.lock.state.seq;
314 iter->l[0].b = (void *) ck;
316 if (!ck->valid && !(iter->flags & BTREE_ITER_CACHED_NOFILL)) {
317 if (!btree_node_intent_locked(iter, 0))
318 bch2_btree_iter_upgrade(iter, 1);
319 if (!btree_node_intent_locked(iter, 0)) {
320 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
325 ret = btree_key_cache_fill(trans, iter, ck);
330 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
331 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
333 iter->uptodate = BTREE_ITER_NEED_PEEK;
335 if (!(iter->flags & BTREE_ITER_INTENT))
336 bch2_btree_iter_downgrade(iter);
337 else if (!iter->locks_want) {
338 if (!__bch2_btree_iter_upgrade(iter, 1))
345 btree_node_unlock(iter, 0);
346 iter->flags |= BTREE_ITER_ERROR;
347 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
352 static int btree_key_cache_flush_pos(struct btree_trans *trans,
353 struct bkey_cached_key key,
357 struct bch_fs *c = trans->c;
358 struct journal *j = &c->journal;
359 struct btree_iter *c_iter = NULL, *b_iter = NULL;
360 struct bkey_cached *ck = NULL;
363 b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
366 c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
368 BTREE_ITER_CACHED_NOFILL|
369 BTREE_ITER_CACHED_NOCREATE|
372 ret = bch2_btree_iter_traverse(c_iter);
376 ck = (void *) c_iter->l[0].b;
378 (journal_seq && ck->journal.seq != journal_seq))
381 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
387 ret = bch2_btree_iter_traverse(b_iter) ?:
388 bch2_trans_update(trans, b_iter, ck->k, BTREE_TRIGGER_NORUN) ?:
389 bch2_trans_commit(trans, NULL, NULL,
390 BTREE_INSERT_NOUNLOCK|
391 BTREE_INSERT_NOCHECK_RW|
393 BTREE_INSERT_JOURNAL_RESERVED|
394 BTREE_INSERT_JOURNAL_RECLAIM);
400 bch2_fs_fatal_err_on(!bch2_journal_error(j), c,
401 "error flushing key cache: %i", ret);
405 bch2_journal_pin_drop(j, &ck->journal);
406 bch2_journal_preres_put(j, &ck->res);
408 BUG_ON(!btree_node_locked(c_iter, 0));
411 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
412 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
413 atomic_long_dec(&c->btree_key_cache.nr_dirty);
417 BUG_ON(!btree_node_intent_locked(c_iter, 0));
419 mark_btree_node_unlocked(c_iter, 0);
420 c_iter->l[0].b = NULL;
422 six_lock_write(&ck->c.lock, NULL, NULL);
424 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
425 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
426 atomic_long_dec(&c->btree_key_cache.nr_dirty);
429 bkey_cached_evict(&c->btree_key_cache, ck);
431 mutex_lock(&c->btree_key_cache.lock);
432 bkey_cached_free(&c->btree_key_cache, ck);
433 mutex_unlock(&c->btree_key_cache.lock);
436 bch2_trans_iter_put(trans, b_iter);
437 bch2_trans_iter_put(trans, c_iter);
441 static void btree_key_cache_journal_flush(struct journal *j,
442 struct journal_entry_pin *pin,
445 struct bch_fs *c = container_of(j, struct bch_fs, journal);
446 struct bkey_cached *ck =
447 container_of(pin, struct bkey_cached, journal);
448 struct bkey_cached_key key;
449 struct btree_trans trans;
451 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
453 six_lock_read(&ck->c.lock, NULL, NULL);
456 if (ck->journal.seq != seq ||
457 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
458 six_unlock_read(&ck->c.lock);
461 six_unlock_read(&ck->c.lock);
463 bch2_trans_init(&trans, c, 0, 0);
464 btree_key_cache_flush_pos(&trans, key, seq, false);
465 bch2_trans_exit(&trans);
467 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
471 * Flush and evict a key from the key cache:
473 int bch2_btree_key_cache_flush(struct btree_trans *trans,
474 enum btree_id id, struct bpos pos)
476 struct bch_fs *c = trans->c;
477 struct bkey_cached_key key = { id, pos };
479 /* Fastpath - assume it won't be found: */
480 if (!bch2_btree_key_cache_find(c, id, pos))
483 return btree_key_cache_flush_pos(trans, key, 0, true);
486 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
487 struct btree_iter *iter,
488 struct bkey_i *insert)
490 struct bch_fs *c = trans->c;
491 struct bkey_cached *ck = (void *) iter->l[0].b;
492 bool kick_reclaim = false;
494 BUG_ON(insert->u64s > ck->u64s);
496 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
499 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
501 difference = jset_u64s(insert->u64s) - ck->res.u64s;
502 if (difference > 0) {
503 trans->journal_preres.u64s -= difference;
504 ck->res.u64s += difference;
508 bkey_copy(ck->k, insert);
511 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
512 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
513 atomic_long_inc(&c->btree_key_cache.nr_dirty);
515 if (bch2_nr_btree_keys_need_flush(c))
519 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
520 &ck->journal, btree_key_cache_journal_flush);
523 journal_reclaim_kick(&c->journal);
527 #ifdef CONFIG_BCACHEFS_DEBUG
528 void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
529 enum btree_id id, struct bpos pos)
531 BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
535 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
536 struct shrink_control *sc)
538 struct bch_fs *c = container_of(shrink, struct bch_fs,
539 btree_key_cache.shrink);
540 struct btree_key_cache *bc = &c->btree_key_cache;
541 struct bucket_table *tbl;
542 struct bkey_cached *ck, *t;
543 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
544 unsigned start, flags;
547 /* Return -1 if we can't do anything right now */
548 if (sc->gfp_mask & __GFP_FS)
549 mutex_lock(&bc->lock);
550 else if (!mutex_trylock(&bc->lock))
553 srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
554 flags = memalloc_nofs_save();
557 * Newest freed entries are at the end of the list - once we hit one
558 * that's too new to be freed, we can bail out:
560 list_for_each_entry_safe(ck, t, &bc->freed, list) {
561 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
562 ck->btree_trans_barrier_seq))
566 kmem_cache_free(bch2_key_cache, ck);
576 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
577 if (bc->shrink_iter >= tbl->size)
579 start = bc->shrink_iter;
582 struct rhash_head *pos, *next;
584 rht_for_each_entry_safe(ck, pos, next, tbl, bc->shrink_iter, hash) {
585 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
588 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
589 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
590 else if (bkey_cached_lock_for_evict(ck)) {
591 bkey_cached_evict(bc, ck);
592 bkey_cached_free(bc, ck);
601 if (bc->shrink_iter >= tbl->size)
603 } while (scanned < nr && bc->shrink_iter != start);
607 memalloc_nofs_restore(flags);
608 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
609 mutex_unlock(&bc->lock);
614 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
615 struct shrink_control *sc)
617 struct bch_fs *c = container_of(shrink, struct bch_fs,
618 btree_key_cache.shrink);
619 struct btree_key_cache *bc = &c->btree_key_cache;
621 return atomic_long_read(&bc->nr_keys);
624 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
626 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
627 struct bucket_table *tbl;
628 struct bkey_cached *ck, *n;
629 struct rhash_head *pos;
632 if (bc->shrink.list.next)
633 unregister_shrinker(&bc->shrink);
635 mutex_lock(&bc->lock);
638 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
639 for (i = 0; i < tbl->size; i++)
640 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
641 bkey_cached_evict(bc, ck);
642 list_add(&ck->list, &bc->freed);
646 list_for_each_entry_safe(ck, n, &bc->freed, list) {
649 bch2_journal_pin_drop(&c->journal, &ck->journal);
650 bch2_journal_preres_put(&c->journal, &ck->res);
654 kmem_cache_free(bch2_key_cache, ck);
657 BUG_ON(atomic_long_read(&bc->nr_dirty) && !bch2_journal_error(&c->journal));
658 BUG_ON(atomic_long_read(&bc->nr_keys));
660 mutex_unlock(&bc->lock);
662 if (bc->table_init_done)
663 rhashtable_destroy(&bc->table);
666 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
668 mutex_init(&c->lock);
669 INIT_LIST_HEAD(&c->freed);
672 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
677 c->shrink.count_objects = bch2_btree_key_cache_count;
678 c->shrink.scan_objects = bch2_btree_key_cache_scan;
680 ret = register_shrinker(&c->shrink);
684 ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
688 c->table_init_done = true;
692 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
694 pr_buf(out, "nr_freed:\t%zu\n", c->nr_freed);
695 pr_buf(out, "nr_keys:\t%zu\n", atomic_long_read(&c->nr_keys));
696 pr_buf(out, "nr_dirty:\t%zu\n", atomic_long_read(&c->nr_dirty));
699 void bch2_btree_key_cache_exit(void)
702 kmem_cache_destroy(bch2_key_cache);
705 int __init bch2_btree_key_cache_init(void)
707 bch2_key_cache = KMEM_CACHE(bkey_cached, 0);