1 From dfd65c34f343bfc44a0a219b9160c66d7d5046b8 Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Tue, 1 Sep 2020 08:09:01 -0400
4 Subject: btrfs: sysfs: init devices outside of the chunk_mutex
6 From: Josef Bacik <josef@toxicpanda.com>
8 [ Upstream commit ca10845a56856fff4de3804c85e6424d0f6d0cde ]
10 While running btrfs/061, btrfs/073, btrfs/078, or btrfs/178 we hit the
11 following lockdep splat:
13 ======================================================
14 WARNING: possible circular locking dependency detected
15 5.9.0-rc3+ #4 Not tainted
16 ------------------------------------------------------
17 kswapd0/100 is trying to acquire lock:
18 ffff96ecc22ef4a0 (&delayed_node->mutex){+.+.}-{3:3}, at: __btrfs_release_delayed_node.part.0+0x3f/0x330
20 but task is already holding lock:
21 ffffffff8dd74700 (fs_reclaim){+.+.}-{0:0}, at: __fs_reclaim_acquire+0x5/0x30
23 which lock already depends on the new lock.
25 the existing dependency chain (in reverse order) is:
27 -> #3 (fs_reclaim){+.+.}-{0:0}:
28 fs_reclaim_acquire+0x65/0x80
29 slab_pre_alloc_hook.constprop.0+0x20/0x200
30 kmem_cache_alloc+0x37/0x270
32 iget_locked+0x10d/0x2c0
33 kernfs_get_inode+0x1b/0x130
34 kernfs_get_tree+0x136/0x240
35 sysfs_get_tree+0x16/0x40
36 vfs_get_tree+0x28/0xc0
37 path_mount+0x434/0xc00
38 __x64_sys_mount+0xe3/0x120
39 do_syscall_64+0x33/0x40
40 entry_SYSCALL_64_after_hwframe+0x44/0xa9
42 -> #2 (kernfs_mutex){+.+.}-{3:3}:
43 __mutex_lock+0x7e/0x7e0
44 kernfs_add_one+0x23/0x150
45 kernfs_create_link+0x63/0xa0
46 sysfs_do_create_link_sd+0x5e/0xd0
47 btrfs_sysfs_add_devices_dir+0x81/0x130
48 btrfs_init_new_device+0x67f/0x1250
49 btrfs_ioctl+0x1ef/0x2e20
50 __x64_sys_ioctl+0x83/0xb0
51 do_syscall_64+0x33/0x40
52 entry_SYSCALL_64_after_hwframe+0x44/0xa9
54 -> #1 (&fs_info->chunk_mutex){+.+.}-{3:3}:
55 __mutex_lock+0x7e/0x7e0
56 btrfs_chunk_alloc+0x125/0x3a0
57 find_free_extent+0xdf6/0x1210
58 btrfs_reserve_extent+0xb3/0x1b0
59 btrfs_alloc_tree_block+0xb0/0x310
60 alloc_tree_block_no_bg_flush+0x4a/0x60
61 __btrfs_cow_block+0x11a/0x530
62 btrfs_cow_block+0x104/0x220
63 btrfs_search_slot+0x52e/0x9d0
64 btrfs_insert_empty_items+0x64/0xb0
65 btrfs_insert_delayed_items+0x90/0x4f0
66 btrfs_commit_inode_delayed_items+0x93/0x140
67 btrfs_log_inode+0x5de/0x2020
68 btrfs_log_inode_parent+0x429/0xc90
69 btrfs_log_new_name+0x95/0x9b
70 btrfs_rename2+0xbb9/0x1800
71 vfs_rename+0x64f/0x9f0
72 do_renameat2+0x320/0x4e0
73 __x64_sys_rename+0x1f/0x30
74 do_syscall_64+0x33/0x40
75 entry_SYSCALL_64_after_hwframe+0x44/0xa9
77 -> #0 (&delayed_node->mutex){+.+.}-{3:3}:
78 __lock_acquire+0x119c/0x1fc0
79 lock_acquire+0xa7/0x3d0
80 __mutex_lock+0x7e/0x7e0
81 __btrfs_release_delayed_node.part.0+0x3f/0x330
82 btrfs_evict_inode+0x24c/0x500
84 dispose_list+0x48/0x70
85 prune_icache_sb+0x44/0x50
86 super_cache_scan+0x161/0x1e0
87 do_shrink_slab+0x178/0x3c0
88 shrink_slab+0x17c/0x290
89 shrink_node+0x2b2/0x6d0
90 balance_pgdat+0x30a/0x670
93 ret_from_fork+0x1f/0x30
95 other info that might help us debug this:
98 &delayed_node->mutex --> kernfs_mutex --> fs_reclaim
100 Possible unsafe locking scenario:
107 lock(&delayed_node->mutex);
111 3 locks held by kswapd0/100:
112 #0: ffffffff8dd74700 (fs_reclaim){+.+.}-{0:0}, at: __fs_reclaim_acquire+0x5/0x30
113 #1: ffffffff8dd65c50 (shrinker_rwsem){++++}-{3:3}, at: shrink_slab+0x115/0x290
114 #2: ffff96ed2ade30e0 (&type->s_umount_key#36){++++}-{3:3}, at: super_cache_scan+0x38/0x1e0
117 CPU: 0 PID: 100 Comm: kswapd0 Not tainted 5.9.0-rc3+ #4
118 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-2.fc32 04/01/2014
121 check_noncircular+0x12d/0x150
122 __lock_acquire+0x119c/0x1fc0
123 lock_acquire+0xa7/0x3d0
124 ? __btrfs_release_delayed_node.part.0+0x3f/0x330
125 __mutex_lock+0x7e/0x7e0
126 ? __btrfs_release_delayed_node.part.0+0x3f/0x330
127 ? __btrfs_release_delayed_node.part.0+0x3f/0x330
128 ? lock_acquire+0xa7/0x3d0
129 ? find_held_lock+0x2b/0x80
130 __btrfs_release_delayed_node.part.0+0x3f/0x330
131 btrfs_evict_inode+0x24c/0x500
133 dispose_list+0x48/0x70
134 prune_icache_sb+0x44/0x50
135 super_cache_scan+0x161/0x1e0
136 do_shrink_slab+0x178/0x3c0
137 shrink_slab+0x17c/0x290
138 shrink_node+0x2b2/0x6d0
139 balance_pgdat+0x30a/0x670
141 ? _raw_spin_unlock_irqrestore+0x41/0x50
142 ? add_wait_queue_exclusive+0x70/0x70
143 ? balance_pgdat+0x670/0x670
145 ? kthread_create_worker_on_cpu+0x40/0x40
146 ret_from_fork+0x1f/0x30
148 This happens because we are holding the chunk_mutex at the time of
149 adding in a new device. However we only need to hold the
150 device_list_mutex, as we're going to iterate over the fs_devices
151 devices. Move the sysfs init stuff outside of the chunk_mutex to get
152 rid of this lockdep splat.
154 CC: stable@vger.kernel.org # 4.4.x: f3cd2c58110dad14e: btrfs: sysfs, rename device_link add/remove functions
155 CC: stable@vger.kernel.org # 4.4.x
156 Reported-by: David Sterba <dsterba@suse.com>
157 Signed-off-by: Josef Bacik <josef@toxicpanda.com>
158 Reviewed-by: David Sterba <dsterba@suse.com>
159 Signed-off-by: David Sterba <dsterba@suse.com>
160 Signed-off-by: Sasha Levin <sashal@kernel.org>
162 fs/btrfs/volumes.c | 7 ++++---
163 1 file changed, 4 insertions(+), 3 deletions(-)
165 diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
166 index f3cb042a28d5c..3d92feff249a0 100644
167 --- a/fs/btrfs/volumes.c
168 +++ b/fs/btrfs/volumes.c
169 @@ -2432,9 +2432,6 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
170 tmp = btrfs_super_num_devices(fs_info->super_copy);
171 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
173 - /* add sysfs device entry */
174 - btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
177 * we've got more storage, clear any full flags on the space
179 @@ -2442,6 +2439,10 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
180 btrfs_clear_space_info_full(fs_info);
182 mutex_unlock(&fs_info->chunk_mutex);
184 + /* Add sysfs device entry */
185 + btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
187 mutex_unlock(&fs_info->fs_devices->device_list_mutex);