Skip to content

Commit 3fd6372

Browse files
josefbacikkdave
authored andcommitted
btrfs: make the extent buffer leak check per fs info
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 <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 7b7b743 commit 3fd6372

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

fs/btrfs/ctree.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,9 @@ struct btrfs_fs_info {
949949
struct kobject *debug_kobj;
950950
struct kobject *discard_debug_kobj;
951951
struct list_head allocated_roots;
952+
953+
spinlock_t eb_leak_lock;
954+
struct list_head allocated_ebs;
952955
#endif
953956
};
954957

fs/btrfs/disk-io.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
15301530
btrfs_put_root(fs_info->free_space_root);
15311531
btrfs_put_root(fs_info->fs_root);
15321532
btrfs_check_leaked_roots(fs_info);
1533+
btrfs_extent_buffer_leak_debug_check(fs_info);
15331534
kfree(fs_info->super_copy);
15341535
kfree(fs_info->super_for_commit);
15351536
kvfree(fs_info);
@@ -2656,6 +2657,8 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
26562657
INIT_LIST_HEAD(&fs_info->unused_bgs);
26572658
#ifdef CONFIG_BTRFS_DEBUG
26582659
INIT_LIST_HEAD(&fs_info->allocated_roots);
2660+
INIT_LIST_HEAD(&fs_info->allocated_ebs);
2661+
spin_lock_init(&fs_info->eb_leak_lock);
26592662
#endif
26602663
extent_map_tree_init(&fs_info->mapping_tree);
26612664
btrfs_init_block_rsv(&fs_info->global_block_rsv,

fs/btrfs/extent_io.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,45 @@ static inline bool extent_state_in_tree(const struct extent_state *state)
3535
}
3636

3737
#ifdef CONFIG_BTRFS_DEBUG
38-
static LIST_HEAD(buffers);
3938
static LIST_HEAD(states);
40-
4139
static DEFINE_SPINLOCK(leak_lock);
4240

43-
static inline
44-
void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
41+
static inline void btrfs_leak_debug_add(spinlock_t *lock,
42+
struct list_head *new,
43+
struct list_head *head)
4544
{
4645
unsigned long flags;
4746

48-
spin_lock_irqsave(&leak_lock, flags);
47+
spin_lock_irqsave(lock, flags);
4948
list_add(new, head);
50-
spin_unlock_irqrestore(&leak_lock, flags);
49+
spin_unlock_irqrestore(lock, flags);
5150
}
5251

53-
static inline
54-
void btrfs_leak_debug_del(struct list_head *entry)
52+
static inline void btrfs_leak_debug_del(spinlock_t *lock,
53+
struct list_head *entry)
5554
{
5655
unsigned long flags;
5756

58-
spin_lock_irqsave(&leak_lock, flags);
57+
spin_lock_irqsave(lock, flags);
5958
list_del(entry);
60-
spin_unlock_irqrestore(&leak_lock, flags);
59+
spin_unlock_irqrestore(lock, flags);
6160
}
6261

63-
static inline void btrfs_extent_buffer_leak_debug_check(void)
62+
void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
6463
{
6564
struct extent_buffer *eb;
65+
unsigned long flags;
6666

67-
while (!list_empty(&buffers)) {
68-
eb = list_entry(buffers.next, struct extent_buffer, leak_list);
67+
spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
68+
while (!list_empty(&fs_info->allocated_ebs)) {
69+
eb = list_first_entry(&fs_info->allocated_ebs,
70+
struct extent_buffer, leak_list);
6971
pr_err("BTRFS: buffer leak start %llu len %lu refs %d bflags %lu\n",
7072
eb->start, eb->len, atomic_read(&eb->refs), eb->bflags);
7173
list_del(&eb->leak_list);
7274
kmem_cache_free(extent_buffer_cache, eb);
7375
}
76+
spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
7477
}
7578

7679
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,
107110
}
108111
}
109112
#else
110-
#define btrfs_leak_debug_add(new, head) do {} while (0)
111-
#define btrfs_leak_debug_del(entry) do {} while (0)
112-
#define btrfs_extent_buffer_leak_debug_check() do {} while (0)
113+
#define btrfs_leak_debug_add(lock, new, head) do {} while (0)
114+
#define btrfs_leak_debug_del(lock, entry) do {} while (0)
113115
#define btrfs_extent_state_leak_debug_check() do {} while (0)
114116
#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
115117
#endif
@@ -245,8 +247,6 @@ void __cold extent_state_cache_exit(void)
245247

246248
void __cold extent_io_exit(void)
247249
{
248-
btrfs_extent_buffer_leak_debug_check();
249-
250250
/*
251251
* Make sure all delayed rcu free are flushed before we
252252
* destroy caches.
@@ -324,7 +324,7 @@ static struct extent_state *alloc_extent_state(gfp_t mask)
324324
state->state = 0;
325325
state->failrec = NULL;
326326
RB_CLEAR_NODE(&state->rb_node);
327-
btrfs_leak_debug_add(&state->leak_list, &states);
327+
btrfs_leak_debug_add(&leak_lock, &state->leak_list, &states);
328328
refcount_set(&state->refs, 1);
329329
init_waitqueue_head(&state->wq);
330330
trace_alloc_extent_state(state, mask, _RET_IP_);
@@ -337,7 +337,7 @@ void free_extent_state(struct extent_state *state)
337337
return;
338338
if (refcount_dec_and_test(&state->refs)) {
339339
WARN_ON(extent_state_in_tree(state));
340-
btrfs_leak_debug_del(&state->leak_list);
340+
btrfs_leak_debug_del(&leak_lock, &state->leak_list);
341341
trace_free_extent_state(state, _RET_IP_);
342342
kmem_cache_free(extent_state_cache, state);
343343
}
@@ -4875,7 +4875,7 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
48754875

48764876
static void __free_extent_buffer(struct extent_buffer *eb)
48774877
{
4878-
btrfs_leak_debug_del(&eb->leak_list);
4878+
btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list);
48794879
kmem_cache_free(extent_buffer_cache, eb);
48804880
}
48814881

@@ -4962,7 +4962,8 @@ __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
49624962
init_waitqueue_head(&eb->write_lock_wq);
49634963
init_waitqueue_head(&eb->read_lock_wq);
49644964

4965-
btrfs_leak_debug_add(&eb->leak_list, &buffers);
4965+
btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list,
4966+
&fs_info->allocated_ebs);
49664967

49674968
spin_lock_init(&eb->refs_lock);
49684969
atomic_set(&eb->refs, 1);

fs/btrfs/extent_io.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,11 @@ bool find_lock_delalloc_range(struct inode *inode,
325325
#endif
326326
struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
327327
u64 start);
328+
329+
#ifdef CONFIG_BTRFS_DEBUG
330+
void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info);
331+
#else
332+
#define btrfs_extent_buffer_leak_debug_check(fs_info) do {} while (0)
333+
#endif
334+
328335
#endif

0 commit comments

Comments
 (0)