]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_key_cache.c
New upstream release
[bcachefs-tools-debian] / libbcachefs / btree_key_cache.c
index 634c67318a8da84cdaca37f6060dcbf42732df8e..f7c001d42391faa5151f1c807514db35392f3abe 100644 (file)
 #include "error.h"
 #include "journal.h"
 #include "journal_reclaim.h"
+#include "trace.h"
 
 #include <linux/sched/mm.h>
-#include <trace/events/bcachefs.h>
+#include <linux/seq_buf.h>
 
 static inline bool btree_uses_pcpu_readers(enum btree_id id)
 {
@@ -27,8 +28,8 @@ static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
        const struct bkey_cached *ck = obj;
        const struct bkey_cached_key *key = arg->key;
 
-       return cmp_int(ck->key.btree_id, key->btree_id) ?:
-               bpos_cmp(ck->key.pos, key->pos);
+       return ck->key.btree_id != key->btree_id ||
+               !bpos_eq(ck->key.pos, key->pos);
 }
 
 static const struct rhashtable_params bch2_btree_key_cache_params = {
@@ -56,13 +57,12 @@ static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
        if (!six_trylock_intent(&ck->c.lock))
                return false;
 
-       if (!six_trylock_write(&ck->c.lock)) {
+       if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
                six_unlock_intent(&ck->c.lock);
                return false;
        }
 
-       if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
-               six_unlock_write(&ck->c.lock);
+       if (!six_trylock_write(&ck->c.lock)) {
                six_unlock_intent(&ck->c.lock);
                return false;
        }
@@ -197,6 +197,7 @@ bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path,
        struct btree_key_cache *bc = &c->btree_key_cache;
        struct bkey_cached *ck = NULL;
        bool pcpu_readers = btree_uses_pcpu_readers(path->btree_id);
+       int ret;
 
        if (!pcpu_readers) {
 #ifdef __KERNEL__
@@ -244,14 +245,14 @@ bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path,
        if (ck) {
                int ret;
 
-               ret = btree_node_lock_nopath(trans, &ck->c, SIX_LOCK_intent);
+               ret = btree_node_lock_nopath(trans, &ck->c, SIX_LOCK_intent, _THIS_IP_);
                if (unlikely(ret)) {
                        bkey_cached_move_to_freelist(bc, ck);
                        return ERR_PTR(ret);
                }
 
                path->l[0].b = (void *) ck;
-               path->l[0].lock_seq = ck->c.lock.state.seq;
+               path->l[0].lock_seq = six_lock_seq(&ck->c.lock);
                mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
 
                ret = bch2_btree_node_lock_write(trans, path, &ck->c);
@@ -264,22 +265,24 @@ bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path,
                return ck;
        }
 
-       /* GFP_NOFS because we're holding btree locks: */
-       ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
-       if (likely(ck)) {
-               INIT_LIST_HEAD(&ck->list);
-               __six_lock_init(&ck->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
-               if (pcpu_readers)
-                       six_lock_pcpu_alloc(&ck->c.lock);
-
-               ck->c.cached = true;
-               BUG_ON(!six_trylock_intent(&ck->c.lock));
-               BUG_ON(!six_trylock_write(&ck->c.lock));
-               *was_new = true;
-               return ck;
+       ck = allocate_dropping_locks(trans, ret,
+                       kmem_cache_zalloc(bch2_key_cache, _gfp));
+       if (ret) {
+               kmem_cache_free(bch2_key_cache, ck);
+               return ERR_PTR(ret);
        }
 
-       return NULL;
+       if (!ck)
+               return NULL;
+
+       INIT_LIST_HEAD(&ck->list);
+       bch2_btree_lock_init(&ck->c, pcpu_readers ? SIX_LOCK_INIT_PCPU : 0);
+
+       ck->c.cached = true;
+       BUG_ON(!six_trylock_intent(&ck->c.lock));
+       BUG_ON(!six_trylock_write(&ck->c.lock));
+       *was_new = true;
+       return ck;
 }
 
 static struct bkey_cached *
@@ -325,13 +328,10 @@ btree_key_cache_create(struct btree_trans *trans, struct btree_path *path)
                if (unlikely(!ck)) {
                        bch_err(c, "error allocating memory for key cache item, btree %s",
                                bch2_btree_ids[path->btree_id]);
-                       return ERR_PTR(-ENOMEM);
+                       return ERR_PTR(-BCH_ERR_ENOMEM_btree_key_cache_create);
                }
 
                mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
-       } else {
-               if (path->btree_id == BTREE_ID_subvolumes)
-                       six_lock_pcpu_alloc(&ck->c.lock);
        }
 
        ck->c.level             = 0;
@@ -369,24 +369,22 @@ static int btree_key_cache_fill(struct btree_trans *trans,
                                struct btree_path *ck_path,
                                struct bkey_cached *ck)
 {
-       struct btree_path *path;
+       struct btree_iter iter;
        struct bkey_s_c k;
        unsigned new_u64s = 0;
        struct bkey_i *new_k = NULL;
-       struct bkey u;
        int ret;
 
-       path = bch2_path_get(trans, ck->key.btree_id,
-                            ck->key.pos, 0, 0, 0, _THIS_IP_);
-       ret = bch2_btree_path_traverse(trans, path, 0);
+       k = bch2_bkey_get_iter(trans, &iter, ck->key.btree_id, ck->key.pos,
+                              BTREE_ITER_KEY_CACHE_FILL|
+                              BTREE_ITER_CACHED_NOFILL);
+       ret = bkey_err(k);
        if (ret)
                goto err;
 
-       k = bch2_btree_path_peek_slot(path, &u);
-
        if (!bch2_btree_node_relock(trans, ck_path, 0)) {
                trace_and_count(trans->c, trans_restart_relock_key_cache_fill, trans, _THIS_IP_, ck_path);
-               ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
+               ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_fill);
                goto err;
        }
 
@@ -405,12 +403,30 @@ static int btree_key_cache_fill(struct btree_trans *trans,
 
        if (new_u64s > ck->u64s) {
                new_u64s = roundup_pow_of_two(new_u64s);
-               new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
+               new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOWAIT|__GFP_NOWARN);
                if (!new_k) {
-                       bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
-                               bch2_btree_ids[ck->key.btree_id], new_u64s);
-                       ret = -ENOMEM;
-                       goto err;
+                       bch2_trans_unlock(trans);
+
+                       new_k = kmalloc(new_u64s * sizeof(u64), GFP_KERNEL);
+                       if (!new_k) {
+                               bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
+                                       bch2_btree_ids[ck->key.btree_id], new_u64s);
+                               ret = -BCH_ERR_ENOMEM_btree_key_cache_fill;
+                               goto err;
+                       }
+
+                       if (!bch2_btree_node_relock(trans, ck_path, 0)) {
+                               kfree(new_k);
+                               trace_and_count(trans->c, trans_restart_relock_key_cache_fill, trans, _THIS_IP_, ck_path);
+                               ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_fill);
+                               goto err;
+                       }
+
+                       ret = bch2_trans_relock(trans);
+                       if (ret) {
+                               kfree(new_k);
+                               goto err;
+                       }
                }
        }
 
@@ -431,9 +447,9 @@ static int btree_key_cache_fill(struct btree_trans *trans,
        bch2_btree_node_unlock_write(trans, ck_path, ck_path->l[0].b);
 
        /* We're not likely to need this iterator again: */
-       path->preserve = false;
+       set_btree_iter_dontneed(&iter);
 err:
-       bch2_path_put(trans, path, 0);
+       bch2_trans_iter_exit(trans, &iter);
        return ret;
 }
 
@@ -449,7 +465,7 @@ bch2_btree_path_traverse_cached_slowpath(struct btree_trans *trans, struct btree
 
        path->l[1].b = NULL;
 
-       if (bch2_btree_node_relock(trans, path, 0)) {
+       if (bch2_btree_node_relock_notrace(trans, path, 0)) {
                ck = (void *) path->l[0].b;
                goto fill;
        }
@@ -476,7 +492,7 @@ retry:
                BUG_ON(ret);
 
                if (ck->key.btree_id != path->btree_id ||
-                   bpos_cmp(ck->key.pos, path->pos)) {
+                   !bpos_eq(ck->key.pos, path->pos)) {
                        six_unlock_type(&ck->c.lock, lock_want);
                        goto retry;
                }
@@ -484,10 +500,12 @@ retry:
                mark_btree_node_locked(trans, path, 0, lock_want);
        }
 
-       path->l[0].lock_seq     = ck->c.lock.state.seq;
+       path->l[0].lock_seq     = six_lock_seq(&ck->c.lock);
        path->l[0].b            = (void *) ck;
 fill:
-       if (!ck->valid) {
+       path->uptodate = BTREE_ITER_UPTODATE;
+
+       if (!ck->valid && !(flags & BTREE_ITER_CACHED_NOFILL)) {
                /*
                 * Using the underscore version because we haven't set
                 * path->uptodate yet:
@@ -502,17 +520,23 @@ fill:
                ret = btree_key_cache_fill(trans, path, ck);
                if (ret)
                        goto err;
+
+               ret = bch2_btree_path_relock(trans, path, _THIS_IP_);
+               if (ret)
+                       goto err;
+
+               path->uptodate = BTREE_ITER_UPTODATE;
        }
 
        if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
                set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
 
-       path->uptodate = BTREE_ITER_UPTODATE;
-       BUG_ON(!ck->valid);
        BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
+       BUG_ON(path->uptodate);
 
        return ret;
 err:
+       path->uptodate = BTREE_ITER_NEED_TRAVERSE;
        if (!bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
                btree_node_unlock(trans, path, 0);
                path->l[0].b = ERR_PTR(ret);
@@ -531,7 +555,7 @@ int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path
 
        path->l[1].b = NULL;
 
-       if (bch2_btree_node_relock(trans, path, 0)) {
+       if (bch2_btree_node_relock_notrace(trans, path, 0)) {
                ck = (void *) path->l[0].b;
                goto fill;
        }
@@ -550,7 +574,7 @@ retry:
                        return ret;
 
                if (ck->key.btree_id != path->btree_id ||
-                   bpos_cmp(ck->key.pos, path->pos)) {
+                   !bpos_eq(ck->key.pos, path->pos)) {
                        six_unlock_type(&ck->c.lock, lock_want);
                        goto retry;
                }
@@ -558,7 +582,7 @@ retry:
                mark_btree_node_locked(trans, path, 0, lock_want);
        }
 
-       path->l[0].lock_seq     = ck->c.lock.state.seq;
+       path->l[0].lock_seq     = six_lock_seq(&ck->c.lock);
        path->l[0].b            = (void *) ck;
 fill:
        if (!ck->valid)
@@ -627,9 +651,8 @@ static int btree_key_cache_flush_pos(struct btree_trans *trans,
                bch2_trans_commit(trans, NULL, NULL,
                                  BTREE_INSERT_NOCHECK_RW|
                                  BTREE_INSERT_NOFAIL|
-                                 BTREE_INSERT_USE_RESERVE|
                                  (ck->journal.seq == journal_last_seq(j)
-                                  ? JOURNAL_WATERMARK_reserved
+                                  ? BCH_WATERMARK_reclaim
                                   : 0)|
                                  commit_flags);
 
@@ -696,6 +719,13 @@ int bch2_btree_key_cache_journal_flush(struct journal *j,
                six_unlock_read(&ck->c.lock);
                goto unlock;
        }
+
+       if (ck->seq != seq) {
+               bch2_journal_pin_update(&c->journal, ck->seq, &ck->journal,
+                                       bch2_btree_key_cache_journal_flush);
+               six_unlock_read(&ck->c.lock);
+               goto unlock;
+       }
        six_unlock_read(&ck->c.lock);
 
        ret = commit_do(&trans, NULL, NULL, 0,
@@ -725,21 +755,22 @@ int bch2_btree_key_cache_flush(struct btree_trans *trans,
 }
 
 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
-                                 struct btree_path *path,
-                                 struct bkey_i *insert)
+                                 unsigned flags,
+                                 struct btree_insert_entry *insert_entry)
 {
        struct bch_fs *c = trans->c;
-       struct bkey_cached *ck = (void *) path->l[0].b;
+       struct bkey_cached *ck = (void *) insert_entry->path->l[0].b;
+       struct bkey_i *insert = insert_entry->k;
        bool kick_reclaim = false;
 
-       BUG_ON(insert->u64s > ck->u64s);
+       BUG_ON(insert->k.u64s > ck->u64s);
 
-       if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
+       if (likely(!(flags & BTREE_INSERT_JOURNAL_REPLAY))) {
                int difference;
 
-               BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
+               BUG_ON(jset_u64s(insert->k.u64s) > trans->journal_preres.u64s);
 
-               difference = jset_u64s(insert->u64s) - ck->res.u64s;
+               difference = jset_u64s(insert->k.u64s) - ck->res.u64s;
                if (difference > 0) {
                        trans->journal_preres.u64s      -= difference;
                        ck->res.u64s                    += difference;
@@ -750,6 +781,7 @@ bool bch2_btree_insert_key_cached(struct btree_trans *trans,
        ck->valid = true;
 
        if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
+               EBUG_ON(test_bit(BCH_FS_CLEAN_SHUTDOWN, &c->flags));
                set_bit(BKEY_CACHED_DIRTY, &ck->flags);
                atomic_long_inc(&c->btree_key_cache.nr_dirty);
 
@@ -757,8 +789,24 @@ bool bch2_btree_insert_key_cached(struct btree_trans *trans,
                        kick_reclaim = true;
        }
 
-       bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
-                               &ck->journal, bch2_btree_key_cache_journal_flush);
+       /*
+        * To minimize lock contention, we only add the journal pin here and
+        * defer pin updates to the flush callback via ->seq. Be careful not to
+        * update ->seq on nojournal commits because we don't want to update the
+        * pin to a seq that doesn't include journal updates on disk. Otherwise
+        * we risk losing the update after a crash.
+        *
+        * The only exception is if the pin is not active in the first place. We
+        * have to add the pin because journal reclaim drives key cache
+        * flushing. The flush callback will not proceed unless ->seq matches
+        * the latest pin, so make sure it starts with a consistent value.
+        */
+       if (!(insert_entry->flags & BTREE_UPDATE_NOJOURNAL) ||
+           !journal_pin_active(&ck->journal)) {
+               ck->seq = trans->journal_res.seq;
+       }
+       bch2_journal_pin_add(&c->journal, trans->journal_res.seq,
+                            &ck->journal, bch2_btree_key_cache_journal_flush);
 
        if (kick_reclaim)
                journal_reclaim_kick(&c->journal);
@@ -812,7 +860,7 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
                        break;
 
                list_del(&ck->list);
-               six_lock_pcpu_free(&ck->c.lock);
+               six_lock_exit(&ck->c.lock);
                kmem_cache_free(bch2_key_cache, ck);
                atomic_long_dec(&bc->nr_freed);
                scanned++;
@@ -828,7 +876,7 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
                        break;
 
                list_del(&ck->list);
-               six_lock_pcpu_free(&ck->c.lock);
+               six_lock_exit(&ck->c.lock);
                kmem_cache_free(bch2_key_cache, ck);
                atomic_long_dec(&bc->nr_freed);
                scanned++;
@@ -902,13 +950,13 @@ void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
        struct bucket_table *tbl;
        struct bkey_cached *ck, *n;
        struct rhash_head *pos;
+       LIST_HEAD(items);
        unsigned i;
 #ifdef __KERNEL__
        int cpu;
 #endif
 
-       if (bc->shrink.list.next)
-               unregister_shrinker(&bc->shrink);
+       unregister_shrinker(&bc->shrink);
 
        mutex_lock(&bc->lock);
 
@@ -922,7 +970,7 @@ void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
                        for (i = 0; i < tbl->size; i++)
                                rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
                                        bkey_cached_evict(bc, ck);
-                                       list_add(&ck->list, &bc->freed_nonpcpu);
+                                       list_add(&ck->list, &items);
                                }
                rcu_read_unlock();
        }
@@ -934,14 +982,17 @@ void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
 
                for (i = 0; i < f->nr; i++) {
                        ck = f->objs[i];
-                       list_add(&ck->list, &bc->freed_nonpcpu);
+                       list_add(&ck->list, &items);
                }
        }
 #endif
 
-       list_splice(&bc->freed_pcpu, &bc->freed_nonpcpu);
+       list_splice(&bc->freed_pcpu,    &items);
+       list_splice(&bc->freed_nonpcpu, &items);
 
-       list_for_each_entry_safe(ck, n, &bc->freed_nonpcpu, list) {
+       mutex_unlock(&bc->lock);
+
+       list_for_each_entry_safe(ck, n, &items, list) {
                cond_resched();
 
                bch2_journal_pin_drop(&c->journal, &ck->journal);
@@ -949,7 +1000,7 @@ void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
 
                list_del(&ck->list);
                kfree(ck->k);
-               six_lock_pcpu_free(&ck->c.lock);
+               six_lock_exit(&ck->c.lock);
                kmem_cache_free(bch2_key_cache, ck);
        }
 
@@ -963,8 +1014,6 @@ void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
                panic("btree key cache shutdown error: nr_keys nonzero (%li)\n",
                      atomic_long_read(&bc->nr_keys));
 
-       mutex_unlock(&bc->lock);
-
        if (bc->table_init_done)
                rhashtable_destroy(&bc->table);
 
@@ -978,28 +1027,30 @@ void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
        INIT_LIST_HEAD(&c->freed_nonpcpu);
 }
 
-static void bch2_btree_key_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
+static void bch2_btree_key_cache_shrinker_to_text(struct seq_buf *s, struct shrinker *shrink)
 {
        struct btree_key_cache *bc =
                container_of(shrink, struct btree_key_cache, shrink);
+       char *cbuf;
+       size_t buflen = seq_buf_get_buf(s, &cbuf);
+       struct printbuf out = PRINTBUF_EXTERN(cbuf, buflen);
 
-       bch2_btree_key_cache_to_text(out, bc);
+       bch2_btree_key_cache_to_text(&out, bc);
+       seq_buf_commit(s, out.pos);
 }
 
 int bch2_fs_btree_key_cache_init(struct btree_key_cache *bc)
 {
        struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
-       int ret;
 
 #ifdef __KERNEL__
        bc->pcpu_freed = alloc_percpu(struct btree_key_cache_freelist);
        if (!bc->pcpu_freed)
-               return -ENOMEM;
+               return -BCH_ERR_ENOMEM_fs_btree_cache_init;
 #endif
 
-       ret = rhashtable_init(&bc->table, &bch2_btree_key_cache_params);
-       if (ret)
-               return ret;
+       if (rhashtable_init(&bc->table, &bch2_btree_key_cache_params))
+               return -BCH_ERR_ENOMEM_fs_btree_cache_init;
 
        bc->table_init_done = true;
 
@@ -1007,7 +1058,9 @@ int bch2_fs_btree_key_cache_init(struct btree_key_cache *bc)
        bc->shrink.count_objects        = bch2_btree_key_cache_count;
        bc->shrink.scan_objects         = bch2_btree_key_cache_scan;
        bc->shrink.to_text              = bch2_btree_key_cache_shrinker_to_text;
-       return register_shrinker(&bc->shrink, "%s/btree_key_cache", c->name);
+       if (register_shrinker(&bc->shrink, "%s/btree_key_cache", c->name))
+               return -BCH_ERR_ENOMEM_fs_btree_cache_init;
+       return 0;
 }
 
 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
@@ -1027,7 +1080,7 @@ void bch2_btree_key_cache_exit(void)
 
 int __init bch2_btree_key_cache_init(void)
 {
-       bch2_key_cache = KMEM_CACHE(bkey_cached, 0);
+       bch2_key_cache = KMEM_CACHE(bkey_cached, SLAB_RECLAIM_ACCOUNT);
        if (!bch2_key_cache)
                return -ENOMEM;