bcachefs: Centralize btree node lock initialization
authorKent Overstreet <kent.overstreet@linux.dev>
Fri, 3 Mar 2023 05:03:01 +0000 (00:03 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:55 +0000 (17:09 -0400)
This fixes some confusion in the lockdep code due to initializing btree
node/key cache locks with the same lockdep key, but different names.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_cache.c
fs/bcachefs/btree_io.c
fs/bcachefs/btree_key_cache.c
fs/bcachefs/btree_locking.c
fs/bcachefs/btree_locking.h

index 76cad6109297284aee4ebde977a330e6dd087015..9b331c319accf174ccd97cbc66806321d937f2a9 100644 (file)
@@ -119,8 +119,7 @@ static struct btree *__btree_node_mem_alloc(struct bch_fs *c, gfp_t gfp)
                return NULL;
 
        bkey_btree_ptr_init(&b->key);
-       six_lock_init(&b->c.lock);
-       lockdep_set_novalidate_class(&b->c.lock);
+       bch2_btree_lock_init(&b->c);
        INIT_LIST_HEAD(&b->list);
        INIT_LIST_HEAD(&b->write_blocked);
        b->byte_order = ilog2(btree_bytes(c));
index 7ffdce97214eedf51793b5e62f9f5859c980bc56..358a285c3bcfed8546b4ea7e20789584cb668170 100644 (file)
@@ -33,6 +33,8 @@ void bch2_btree_node_io_unlock(struct btree *b)
 
 void bch2_btree_node_io_lock(struct btree *b)
 {
+       bch2_assert_btree_nodes_not_locked();
+
        wait_on_bit_lock_io(&b->flags, BTREE_NODE_write_in_flight,
                            TASK_UNINTERRUPTIBLE);
 }
@@ -51,12 +53,16 @@ void __bch2_btree_node_wait_on_write(struct btree *b)
 
 void bch2_btree_node_wait_on_read(struct btree *b)
 {
+       bch2_assert_btree_nodes_not_locked();
+
        wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
                       TASK_UNINTERRUPTIBLE);
 }
 
 void bch2_btree_node_wait_on_write(struct btree *b)
 {
+       bch2_assert_btree_nodes_not_locked();
+
        wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight,
                       TASK_UNINTERRUPTIBLE);
 }
index 67db6b9d8e10801f4d2e0eba4753b6ecba193335..1e692c0a2f3ade5058fcf46df9db4f862b0bb849 100644 (file)
@@ -282,8 +282,7 @@ bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path,
                return NULL;
 init:
        INIT_LIST_HEAD(&ck->list);
-       __six_lock_init(&ck->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
-       lockdep_set_novalidate_class(&ck->c.lock);
+       bch2_btree_lock_init(&ck->c);
        if (pcpu_readers)
                six_lock_pcpu_alloc(&ck->c.lock);
 
index 14a0614af436480cc3bcc37a32f7e09e4324d564..9e097ab668a6dccfed831a6c6b9d1d4767999033 100644 (file)
@@ -4,7 +4,23 @@
 #include "btree_locking.h"
 #include "btree_types.h"
 
-struct lock_class_key bch2_btree_node_lock_key;
+static struct lock_class_key bch2_btree_node_lock_key;
+
+void bch2_btree_lock_init(struct btree_bkey_cached_common *b)
+{
+       __six_lock_init(&b->lock, "b->c.lock", &bch2_btree_node_lock_key);
+       lockdep_set_novalidate_class(&b->lock);
+}
+
+#ifdef CONFIG_LOCKDEP
+void bch2_assert_btree_nodes_not_locked(void)
+{
+#if 0
+       //Re-enable when lock_class_is_held() is merged:
+       BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
+#endif
+}
+#endif
 
 /* Btree node locking: */
 
index 30c89daa50099636f5c903ec8f46c22528b3c823..76aac49966fe80dff3f7728934fa58f124d6b937 100644 (file)
 #include "btree_iter.h"
 #include "six.h"
 
-extern struct lock_class_key bch2_btree_node_lock_key;
+void bch2_btree_lock_init(struct btree_bkey_cached_common *);
+
+#ifdef CONFIG_LOCKDEP
+void bch2_assert_btree_nodes_not_locked(void);
+#else
+static inline void bch2_assert_btree_nodes_not_locked(void) {}
+#endif
 
 static inline bool is_btree_node(struct btree_path *path, unsigned l)
 {