]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_key_cache.c
Update bcachefs sources to a1b6677dca57 bcachefs: Fix looping around bch2_propagate_k...
[bcachefs-tools-debian] / libbcachefs / btree_key_cache.c
index 298a674dbfd6e9963a624a410fe7d5a814954a80..1407f69140c2060c4f85ccda0f4f59d18f5e2304 100644 (file)
 #include "error.h"
 #include "journal.h"
 #include "journal_reclaim.h"
+#include "trace.h"
 
 #include <linux/sched/mm.h>
 #include <linux/seq_buf.h>
-#include <trace/events/bcachefs.h>
 
 static inline bool btree_uses_pcpu_readers(enum btree_id id)
 {
@@ -243,8 +243,6 @@ 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, _THIS_IP_);
                if (unlikely(ret)) {
                        bkey_cached_move_to_freelist(bc, ck);
@@ -252,8 +250,8 @@ bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path,
                }
 
                path->l[0].b = (void *) ck;
-               path->l[0].lock_seq = ck->c.lock.state.seq;
-               mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
+               path->l[0].lock_seq = six_lock_seq(&ck->c.lock);
+               mark_btree_node_locked(trans, path, 0, BTREE_NODE_INTENT_LOCKED);
 
                ret = bch2_btree_node_lock_write(trans, path, &ck->c);
                if (unlikely(ret)) {
@@ -265,15 +263,8 @@ bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path,
                return ck;
        }
 
-       ck = kmem_cache_zalloc(bch2_key_cache, GFP_NOWAIT|__GFP_NOWARN);
-       if (likely(ck))
-               goto init;
-
-       bch2_trans_unlock(trans);
-
-       ck = kmem_cache_zalloc(bch2_key_cache, GFP_KERNEL);
-
-       ret = bch2_trans_relock(trans);
+       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);
@@ -281,11 +272,9 @@ bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path,
 
        if (!ck)
                return NULL;
-init:
+
        INIT_LIST_HEAD(&ck->list);
-       bch2_btree_lock_init(&ck->c);
-       if (pcpu_readers)
-               six_lock_pcpu_alloc(&ck->c.lock);
+       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));
@@ -337,13 +326,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);
+               mark_btree_node_locked(trans, path, 0, BTREE_NODE_INTENT_LOCKED);
        }
 
        ck->c.level             = 0;
@@ -387,10 +373,9 @@ static int btree_key_cache_fill(struct btree_trans *trans,
        struct bkey_i *new_k = NULL;
        int ret;
 
-       bch2_trans_iter_init(trans, &iter, ck->key.btree_id, ck->key.pos,
-                            BTREE_ITER_KEY_CACHE_FILL|
-                            BTREE_ITER_CACHED_NOFILL);
-       k = bch2_btree_iter_peek_slot(&iter);
+       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;
@@ -424,7 +409,7 @@ static int btree_key_cache_fill(struct btree_trans *trans,
                        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;
+                               ret = -BCH_ERR_ENOMEM_btree_key_cache_fill;
                                goto err;
                        }
 
@@ -492,7 +477,7 @@ retry:
                if (!ck)
                        goto retry;
 
-               mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
+               mark_btree_node_locked(trans, path, 0, BTREE_NODE_INTENT_LOCKED);
                path->locks_want = 1;
        } else {
                enum six_lock_type lock_want = __btree_lock_want(path, 0);
@@ -510,10 +495,11 @@ retry:
                        goto retry;
                }
 
-               mark_btree_node_locked(trans, path, 0, lock_want);
+               mark_btree_node_locked(trans, path, 0,
+                                      (enum btree_node_locked_type) 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:
        path->uptodate = BTREE_ITER_UPTODATE;
@@ -592,10 +578,11 @@ retry:
                        goto retry;
                }
 
-               mark_btree_node_locked(trans, path, 0, lock_want);
+               mark_btree_node_locked(trans, path, 0,
+                                      (enum btree_node_locked_type) 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)
@@ -664,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);
 
@@ -719,13 +705,11 @@ int bch2_btree_key_cache_journal_flush(struct journal *j,
        struct bkey_cached *ck =
                container_of(pin, struct bkey_cached, journal);
        struct bkey_cached_key key;
-       struct btree_trans trans;
+       struct btree_trans *trans = bch2_trans_get(c);
        int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
        int ret = 0;
 
-       bch2_trans_init(&trans, c, 0, 0);
-
-       btree_node_lock_nopath_nofail(&trans, &ck->c, SIX_LOCK_read);
+       btree_node_lock_nopath_nofail(trans, &ck->c, SIX_LOCK_read);
        key = ck->key;
 
        if (ck->journal.seq != seq ||
@@ -742,13 +726,13 @@ int bch2_btree_key_cache_journal_flush(struct journal *j,
        }
        six_unlock_read(&ck->c.lock);
 
-       ret = commit_do(&trans, NULL, NULL, 0,
-               btree_key_cache_flush_pos(&trans, key, seq,
+       ret = commit_do(trans, NULL, NULL, 0,
+               btree_key_cache_flush_pos(trans, key, seq,
                                BTREE_INSERT_JOURNAL_RECLAIM, false));
 unlock:
        srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
 
-       bch2_trans_exit(&trans);
+       bch2_trans_put(trans);
        return ret;
 }
 
@@ -770,11 +754,11 @@ int bch2_btree_key_cache_flush(struct btree_trans *trans,
 
 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
                                  unsigned flags,
-                                 struct btree_path *path,
-                                 struct bkey_i *insert)
+                                 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->k.u64s > ck->u64s);
@@ -795,6 +779,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);
 
@@ -802,9 +787,24 @@ bool bch2_btree_insert_key_cached(struct btree_trans *trans,
                        kick_reclaim = true;
        }
 
+       /*
+        * 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);
-       ck->seq = trans->journal_res.seq;
 
        if (kick_reclaim)
                journal_reclaim_kick(&c->journal);
@@ -858,7 +858,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++;
@@ -874,7 +874,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++;
@@ -954,8 +954,7 @@ void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
        int cpu;
 #endif
 
-       if (bc->shrink.list.next)
-               unregister_shrinker(&bc->shrink);
+       unregister_shrinker(&bc->shrink);
 
        mutex_lock(&bc->lock);
 
@@ -999,7 +998,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);
        }
 
@@ -1041,17 +1040,15 @@ static void bch2_btree_key_cache_shrinker_to_text(struct seq_buf *s, struct shri
 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;
 
@@ -1059,12 +1056,14 @@ 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)
 {
-       prt_printf(out, "nr_freed:\t%zu",       atomic_long_read(&c->nr_freed));
+       prt_printf(out, "nr_freed:\t%lu",       atomic_long_read(&c->nr_freed));
        prt_newline(out);
        prt_printf(out, "nr_keys:\t%lu",        atomic_long_read(&c->nr_keys));
        prt_newline(out);
@@ -1079,7 +1078,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;