btrfs: make the extent buffer leak check per fs info
authorJosef Bacik <josef@toxicpanda.com>
Fri, 14 Feb 2020 21:11:40 +0000 (16:11 -0500)
committerDavid Sterba <dsterba@suse.com>
Mon, 23 Mar 2020 16:01:58 +0000 (17:01 +0100)
I'm going to make the entire destruction of btrfs_root's controlled by
their refcount, so it will be helpful to notice if we're leaking their
eb's on umount.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.h
fs/btrfs/disk-io.c
fs/btrfs/extent_io.c
fs/btrfs/extent_io.h

index ebca8e8..f1eef81 100644 (file)
@@ -949,6 +949,9 @@ struct btrfs_fs_info {
        struct kobject *debug_kobj;
        struct kobject *discard_debug_kobj;
        struct list_head allocated_roots;
+
+       spinlock_t eb_leak_lock;
+       struct list_head allocated_ebs;
 #endif
 };
 
index 6b00dde..56cad6a 100644 (file)
@@ -1530,6 +1530,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
        btrfs_put_root(fs_info->free_space_root);
        btrfs_put_root(fs_info->fs_root);
        btrfs_check_leaked_roots(fs_info);
+       btrfs_extent_buffer_leak_debug_check(fs_info);
        kfree(fs_info->super_copy);
        kfree(fs_info->super_for_commit);
        kvfree(fs_info);
@@ -2656,6 +2657,8 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
        INIT_LIST_HEAD(&fs_info->unused_bgs);
 #ifdef CONFIG_BTRFS_DEBUG
        INIT_LIST_HEAD(&fs_info->allocated_roots);
+       INIT_LIST_HEAD(&fs_info->allocated_ebs);
+       spin_lock_init(&fs_info->eb_leak_lock);
 #endif
        extent_map_tree_init(&fs_info->mapping_tree);
        btrfs_init_block_rsv(&fs_info->global_block_rsv,
index 837262d..36af710 100644 (file)
@@ -35,42 +35,45 @@ static inline bool extent_state_in_tree(const struct extent_state *state)
 }
 
 #ifdef CONFIG_BTRFS_DEBUG
-static LIST_HEAD(buffers);
 static LIST_HEAD(states);
-
 static DEFINE_SPINLOCK(leak_lock);
 
-static inline
-void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
+static inline void btrfs_leak_debug_add(spinlock_t *lock,
+                                       struct list_head *new,
+                                       struct list_head *head)
 {
        unsigned long flags;
 
-       spin_lock_irqsave(&leak_lock, flags);
+       spin_lock_irqsave(lock, flags);
        list_add(new, head);
-       spin_unlock_irqrestore(&leak_lock, flags);
+       spin_unlock_irqrestore(lock, flags);
 }
 
-static inline
-void btrfs_leak_debug_del(struct list_head *entry)
+static inline void btrfs_leak_debug_del(spinlock_t *lock,
+                                       struct list_head *entry)
 {
        unsigned long flags;
 
-       spin_lock_irqsave(&leak_lock, flags);
+       spin_lock_irqsave(lock, flags);
        list_del(entry);
-       spin_unlock_irqrestore(&leak_lock, flags);
+       spin_unlock_irqrestore(lock, flags);
 }
 
-static inline void btrfs_extent_buffer_leak_debug_check(void)
+void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
 {
        struct extent_buffer *eb;
+       unsigned long flags;
 
-       while (!list_empty(&buffers)) {
-               eb = list_entry(buffers.next, struct extent_buffer, leak_list);
+       spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
+       while (!list_empty(&fs_info->allocated_ebs)) {
+               eb = list_first_entry(&fs_info->allocated_ebs,
+                                     struct extent_buffer, leak_list);
                pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
                       eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
                list_del(&eb->leak_list);
                kmem_cache_free(extent_buffer_cache, eb);
        }
+       spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
 }
 
 static inline void btrfs_extent_state_leak_debug_check(void)
@@ -107,9 +110,8 @@ static inline void __btrfs_debug_check_extent_io_range(const char *caller,
        }
 }
 #else
-#define btrfs_leak_debug_add(new, head)        do {} while (0)
-#define btrfs_leak_debug_del(entry)    do {} while (0)
-#define btrfs_extent_buffer_leak_debug_check() do {} while (0)
+#define btrfs_leak_debug_add(lock, new, head)  do {} while (0)
+#define btrfs_leak_debug_del(lock, entry)      do {} while (0)
 #define btrfs_extent_state_leak_debug_check()  do {} while (0)
 #define btrfs_debug_check_extent_io_range(c, s, e)     do {} while (0)
 #endif
@@ -245,8 +247,6 @@ void __cold extent_state_cache_exit(void)
 
 void __cold extent_io_exit(void)
 {
-       btrfs_extent_buffer_leak_debug_check();
-
        /*
         * Make sure all delayed rcu free are flushed before we
         * destroy caches.
@@ -324,7 +324,7 @@ static struct extent_state *alloc_extent_state(gfp_t mask)
        state->state = 0;
        state->failrec = NULL;
        RB_CLEAR_NODE(&state->rb_node);
-       btrfs_leak_debug_add(&state->leak_list, &states);
+       btrfs_leak_debug_add(&leak_lock, &state->leak_list, &states);
        refcount_set(&state->refs, 1);
        init_waitqueue_head(&state->wq);
        trace_alloc_extent_state(state, mask, _RET_IP_);
@@ -337,7 +337,7 @@ void free_extent_state(struct extent_state *state)
                return;
        if (refcount_dec_and_test(&state->refs)) {
                WARN_ON(extent_state_in_tree(state));
-               btrfs_leak_debug_del(&state->leak_list);
+               btrfs_leak_debug_del(&leak_lock, &state->leak_list);
                trace_free_extent_state(state, _RET_IP_);
                kmem_cache_free(extent_state_cache, state);
        }
@@ -4875,7 +4875,7 @@ out_free_ulist:
 
 static void __free_extent_buffer(struct extent_buffer *eb)
 {
-       btrfs_leak_debug_del(&eb->leak_list);
+       btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
        kmem_cache_free(extent_buffer_cache, eb);
 }
 
@@ -4962,7 +4962,8 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
        init_waitqueue_head(&eb->write_lock_wq);
        init_waitqueue_head(&eb->read_lock_wq);
 
-       btrfs_leak_debug_add(&eb->leak_list, &buffers);
+       btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list,
+                            &fs_info->allocated_ebs);
 
        spin_lock_init(&eb->refs_lock);
        atomic_set(&eb->refs, 1);
index 2346221..2ed65bd 100644 (file)
@@ -325,4 +325,11 @@ bool find_lock_delalloc_range(struct inode *inode,
 #endif
 struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
                                               u64 start);
+
+#ifdef CONFIG_BTRFS_DEBUG
+void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info);
+#else
+#define btrfs_extent_buffer_leak_debug_check(fs_info)  do {} while (0)
+#endif
+
 #endif