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 bkey_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));
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 list_for_each_entry_reverse(ck, &c->freed, list)
103 if (bkey_cached_lock_for_evict(ck)) {
108 ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
110 INIT_LIST_HEAD(&ck->list);
111 six_lock_init(&ck->c.lock);
112 BUG_ON(!six_trylock_intent(&ck->c.lock));
113 BUG_ON(!six_trylock_write(&ck->c.lock));
117 list_for_each_entry(ck, &c->clean, list)
118 if (bkey_cached_lock_for_evict(ck)) {
119 bkey_cached_evict(c, ck);
126 static struct bkey_cached *
127 btree_key_cache_create(struct btree_key_cache *c,
128 enum btree_id btree_id,
131 struct bkey_cached *ck;
133 ck = bkey_cached_alloc(c);
135 return ERR_PTR(-ENOMEM);
138 ck->c.btree_id = btree_id;
139 ck->key.btree_id = btree_id;
142 ck->flags = 1U << BKEY_CACHED_ACCESSED;
144 if (rhashtable_lookup_insert_fast(&c->table,
146 bch2_btree_key_cache_params)) {
147 /* We raced with another fill: */
148 bkey_cached_free(c, ck);
154 list_move(&ck->list, &c->clean);
155 six_unlock_write(&ck->c.lock);
160 static int btree_key_cache_fill(struct btree_trans *trans,
161 struct btree_iter *ck_iter,
162 struct bkey_cached *ck)
164 struct btree_iter *iter;
166 unsigned new_u64s = 0;
167 struct bkey_i *new_k = NULL;
170 iter = bch2_trans_get_iter(trans, ck->key.btree_id,
171 ck->key.pos, BTREE_ITER_SLOTS);
172 k = bch2_btree_iter_peek_slot(iter);
175 bch2_trans_iter_put(trans, iter);
179 if (!bch2_btree_node_relock(ck_iter, 0)) {
180 bch2_trans_iter_put(trans, iter);
181 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
185 if (k.k->u64s > ck->u64s) {
186 new_u64s = roundup_pow_of_two(k.k->u64s);
187 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
189 bch2_trans_iter_put(trans, iter);
194 bch2_btree_node_lock_write(ck_iter->l[0].b, ck_iter);
201 bkey_reassemble(ck->k, k);
203 bch2_btree_node_unlock_write(ck_iter->l[0].b, ck_iter);
205 /* We're not likely to need this iterator again: */
206 bch2_trans_iter_free(trans, iter);
211 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
213 struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
214 const struct btree_iter *iter = p;
216 return ck->key.btree_id == iter->btree_id &&
217 !bkey_cmp(ck->key.pos, iter->pos) ? 0 : -1;
221 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
223 struct btree_trans *trans = iter->trans;
224 struct bch_fs *c = trans->c;
225 struct bkey_cached *ck;
230 if (btree_node_locked(iter, 0)) {
231 ck = (void *) iter->l[0].b;
235 ck = bch2_btree_key_cache_find(c, iter->btree_id, iter->pos);
237 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
242 mutex_lock(&c->btree_key_cache.lock);
243 ck = btree_key_cache_create(&c->btree_key_cache,
244 iter->btree_id, iter->pos);
245 mutex_unlock(&c->btree_key_cache.lock);
247 ret = PTR_ERR_OR_ZERO(ck);
253 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
254 iter->locks_want = 1;
256 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
258 if (!btree_node_lock((void *) ck, iter->pos, 0, iter, lock_want,
259 bkey_cached_check_fn, iter, _THIS_IP_)) {
260 if (ck->key.btree_id != iter->btree_id ||
261 bkey_cmp(ck->key.pos, iter->pos)) {
265 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
270 if (ck->key.btree_id != iter->btree_id ||
271 bkey_cmp(ck->key.pos, iter->pos)) {
272 six_unlock_type(&ck->c.lock, lock_want);
276 mark_btree_node_locked(iter, 0, lock_want);
279 iter->l[0].lock_seq = ck->c.lock.state.seq;
280 iter->l[0].b = (void *) ck;
282 if (!ck->valid && !(iter->flags & BTREE_ITER_CACHED_NOFILL)) {
283 if (!btree_node_intent_locked(iter, 0))
284 bch2_btree_iter_upgrade(iter, 1);
285 if (!btree_node_intent_locked(iter, 0)) {
286 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
291 ret = btree_key_cache_fill(trans, iter, ck);
296 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
297 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
299 iter->uptodate = BTREE_ITER_NEED_PEEK;
300 bch2_btree_iter_downgrade(iter);
304 btree_node_unlock(iter, 0);
305 iter->flags |= BTREE_ITER_ERROR;
306 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
311 static int btree_key_cache_flush_pos(struct btree_trans *trans,
312 struct bkey_cached_key key,
316 struct bch_fs *c = trans->c;
317 struct journal *j = &c->journal;
318 struct btree_iter *c_iter = NULL, *b_iter = NULL;
319 struct bkey_cached *ck = NULL;
322 b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
325 c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
327 BTREE_ITER_CACHED_NOFILL|
328 BTREE_ITER_CACHED_NOCREATE|
331 ret = bch2_btree_iter_traverse(c_iter);
335 ck = (void *) c_iter->l[0].b;
337 (journal_seq && ck->journal.seq != journal_seq))
340 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
346 ret = bch2_btree_iter_traverse(b_iter) ?:
347 bch2_trans_update(trans, b_iter, ck->k, BTREE_TRIGGER_NORUN) ?:
348 bch2_trans_commit(trans, NULL, NULL,
349 BTREE_INSERT_NOUNLOCK|
350 BTREE_INSERT_NOCHECK_RW|
352 BTREE_INSERT_USE_RESERVE|
353 BTREE_INSERT_USE_ALLOC_RESERVE|
354 BTREE_INSERT_JOURNAL_RESERVED|
355 BTREE_INSERT_JOURNAL_RECLAIM);
361 bch2_fs_fatal_err_on(!bch2_journal_error(j), c,
362 "error flushing key cache: %i", ret);
366 bch2_journal_pin_drop(j, &ck->journal);
367 bch2_journal_preres_put(j, &ck->res);
370 mutex_lock(&c->btree_key_cache.lock);
371 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
372 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
373 c->btree_key_cache.nr_dirty--;
376 list_move_tail(&ck->list, &c->btree_key_cache.clean);
377 mutex_unlock(&c->btree_key_cache.lock);
380 BUG_ON(!btree_node_intent_locked(c_iter, 0));
382 mark_btree_node_unlocked(c_iter, 0);
383 c_iter->l[0].b = NULL;
385 six_lock_write(&ck->c.lock, NULL, NULL);
387 mutex_lock(&c->btree_key_cache.lock);
388 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
389 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
390 c->btree_key_cache.nr_dirty--;
393 bkey_cached_evict(&c->btree_key_cache, ck);
394 bkey_cached_free(&c->btree_key_cache, ck);
395 mutex_unlock(&c->btree_key_cache.lock);
398 bch2_trans_iter_put(trans, b_iter);
399 bch2_trans_iter_put(trans, c_iter);
403 static void btree_key_cache_journal_flush(struct journal *j,
404 struct journal_entry_pin *pin,
407 struct bch_fs *c = container_of(j, struct bch_fs, journal);
408 struct bkey_cached *ck =
409 container_of(pin, struct bkey_cached, journal);
410 struct bkey_cached_key key;
411 struct btree_trans trans;
413 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
415 six_lock_read(&ck->c.lock, NULL, NULL);
418 if (ck->journal.seq != seq ||
419 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
420 six_unlock_read(&ck->c.lock);
423 six_unlock_read(&ck->c.lock);
425 bch2_trans_init(&trans, c, 0, 0);
426 btree_key_cache_flush_pos(&trans, key, seq, false);
427 bch2_trans_exit(&trans);
429 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
433 * Flush and evict a key from the key cache:
435 int bch2_btree_key_cache_flush(struct btree_trans *trans,
436 enum btree_id id, struct bpos pos)
438 struct bch_fs *c = trans->c;
439 struct bkey_cached_key key = { id, pos };
441 /* Fastpath - assume it won't be found: */
442 if (!bch2_btree_key_cache_find(c, id, pos))
445 return btree_key_cache_flush_pos(trans, key, 0, true);
448 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
449 struct btree_iter *iter,
450 struct bkey_i *insert)
452 struct bch_fs *c = trans->c;
453 struct bkey_cached *ck = (void *) iter->l[0].b;
454 bool kick_reclaim = false;
456 BUG_ON(insert->u64s > ck->u64s);
458 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
461 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
463 difference = jset_u64s(insert->u64s) - ck->res.u64s;
464 if (difference > 0) {
465 trans->journal_preres.u64s -= difference;
466 ck->res.u64s += difference;
470 bkey_copy(ck->k, insert);
473 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
474 mutex_lock(&c->btree_key_cache.lock);
475 list_move(&ck->list, &c->btree_key_cache.dirty);
477 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
478 c->btree_key_cache.nr_dirty++;
480 if (bch2_nr_btree_keys_need_flush(c))
483 mutex_unlock(&c->btree_key_cache.lock);
486 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
487 &ck->journal, btree_key_cache_journal_flush);
490 journal_reclaim_kick(&c->journal);
494 #ifdef CONFIG_BCACHEFS_DEBUG
495 void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
496 enum btree_id id, struct bpos pos)
498 BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
502 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
503 struct shrink_control *sc)
505 struct bch_fs *c = container_of(shrink, struct bch_fs,
506 btree_key_cache.shrink);
507 struct btree_key_cache *bc = &c->btree_key_cache;
508 struct bkey_cached *ck, *t;
509 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
512 /* Return -1 if we can't do anything right now */
513 if (sc->gfp_mask & __GFP_FS)
514 mutex_lock(&bc->lock);
515 else if (!mutex_trylock(&bc->lock))
518 flags = memalloc_nofs_save();
521 * Newest freed entries are at the end of the list - once we hit one
522 * that's too new to be freed, we can bail out:
524 list_for_each_entry_safe(ck, t, &bc->freed, list) {
525 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
526 ck->btree_trans_barrier_seq))
530 kmem_cache_free(bch2_key_cache, ck);
539 list_for_each_entry_safe(ck, t, &bc->clean, list) {
540 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
541 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
542 else if (bkey_cached_lock_for_evict(ck)) {
543 bkey_cached_evict(bc, ck);
544 bkey_cached_free(bc, ck);
549 if (&t->list != &bc->clean)
550 list_move_tail(&bc->clean, &t->list);
555 memalloc_nofs_restore(flags);
556 mutex_unlock(&bc->lock);
561 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
562 struct shrink_control *sc)
564 struct bch_fs *c = container_of(shrink, struct bch_fs,
565 btree_key_cache.shrink);
566 struct btree_key_cache *bc = &c->btree_key_cache;
571 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
573 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
574 struct bkey_cached *ck, *n;
576 if (bc->shrink.list.next)
577 unregister_shrinker(&bc->shrink);
579 mutex_lock(&bc->lock);
580 list_splice(&bc->dirty, &bc->clean);
582 list_for_each_entry_safe(ck, n, &bc->clean, list) {
583 bch2_journal_pin_drop(&c->journal, &ck->journal);
584 bch2_journal_preres_put(&c->journal, &ck->res);
588 kmem_cache_free(bch2_key_cache, ck);
592 BUG_ON(bc->nr_dirty && !bch2_journal_error(&c->journal));
595 list_for_each_entry_safe(ck, n, &bc->freed, list) {
597 kmem_cache_free(bch2_key_cache, ck);
599 mutex_unlock(&bc->lock);
601 if (bc->table_init_done)
602 rhashtable_destroy(&bc->table);
605 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
607 mutex_init(&c->lock);
608 INIT_LIST_HEAD(&c->freed);
609 INIT_LIST_HEAD(&c->clean);
610 INIT_LIST_HEAD(&c->dirty);
613 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
618 c->shrink.count_objects = bch2_btree_key_cache_count;
619 c->shrink.scan_objects = bch2_btree_key_cache_scan;
621 ret = register_shrinker(&c->shrink);
625 ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
629 c->table_init_done = true;
633 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
635 pr_buf(out, "nr_freed:\t%zu\n", c->nr_freed);
636 pr_buf(out, "nr_keys:\t%zu\n", c->nr_keys);
637 pr_buf(out, "nr_dirty:\t%zu\n", c->nr_dirty);
640 void bch2_btree_key_cache_exit(void)
643 kmem_cache_destroy(bch2_key_cache);
646 int __init bch2_btree_key_cache_init(void)
648 bch2_key_cache = KMEM_CACHE(bkey_cached, 0);