Skip to content

Commit 3fa421d

Browse files
josefbacikkdave
authored andcommitted
btrfs: delay blkdev_put until after the device remove
When removing the device we call blkdev_put() on the device once we've removed it, and because we have an EXCL open we need to take the ->open_mutex on the block device to clean it up. Unfortunately during device remove we are holding the sb writers lock, which results in the following lockdep splat: ====================================================== WARNING: possible circular locking dependency detected 5.14.0-rc2+ #407 Not tainted ------------------------------------------------------ losetup/11595 is trying to acquire lock: ffff973ac35dd138 ((wq_completion)loop0){+.+.}-{0:0}, at: flush_workqueue+0x67/0x5e0 but task is already holding lock: ffff973ac9812c68 (&lo->lo_mutex){+.+.}-{3:3}, at: __loop_clr_fd+0x41/0x660 [loop] which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #4 (&lo->lo_mutex){+.+.}-{3:3}: __mutex_lock+0x7d/0x750 lo_open+0x28/0x60 [loop] blkdev_get_whole+0x25/0xf0 blkdev_get_by_dev.part.0+0x168/0x3c0 blkdev_open+0xd2/0xe0 do_dentry_open+0x161/0x390 path_openat+0x3cc/0xa20 do_filp_open+0x96/0x120 do_sys_openat2+0x7b/0x130 __x64_sys_openat+0x46/0x70 do_syscall_64+0x38/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae -> #3 (&disk->open_mutex){+.+.}-{3:3}: __mutex_lock+0x7d/0x750 blkdev_put+0x3a/0x220 btrfs_rm_device.cold+0x62/0xe5 btrfs_ioctl+0x2a31/0x2e70 __x64_sys_ioctl+0x80/0xb0 do_syscall_64+0x38/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae -> #2 (sb_writers#12){.+.+}-{0:0}: lo_write_bvec+0xc2/0x240 [loop] loop_process_work+0x238/0xd00 [loop] process_one_work+0x26b/0x560 worker_thread+0x55/0x3c0 kthread+0x140/0x160 ret_from_fork+0x1f/0x30 -> #1 ((work_completion)(&lo->rootcg_work)){+.+.}-{0:0}: process_one_work+0x245/0x560 worker_thread+0x55/0x3c0 kthread+0x140/0x160 ret_from_fork+0x1f/0x30 -> #0 ((wq_completion)loop0){+.+.}-{0:0}: __lock_acquire+0x10ea/0x1d90 lock_acquire+0xb5/0x2b0 flush_workqueue+0x91/0x5e0 drain_workqueue+0xa0/0x110 destroy_workqueue+0x36/0x250 __loop_clr_fd+0x9a/0x660 [loop] block_ioctl+0x3f/0x50 __x64_sys_ioctl+0x80/0xb0 do_syscall_64+0x38/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae other info that might help us debug this: Chain exists of: (wq_completion)loop0 --> &disk->open_mutex --> &lo->lo_mutex Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&lo->lo_mutex); lock(&disk->open_mutex); lock(&lo->lo_mutex); lock((wq_completion)loop0); *** DEADLOCK *** 1 lock held by losetup/11595: #0: ffff973ac9812c68 (&lo->lo_mutex){+.+.}-{3:3}, at: __loop_clr_fd+0x41/0x660 [loop] stack backtrace: CPU: 0 PID: 11595 Comm: losetup Not tainted 5.14.0-rc2+ #407 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-2.fc32 04/01/2014 Call Trace: dump_stack_lvl+0x57/0x72 check_noncircular+0xcf/0xf0 ? stack_trace_save+0x3b/0x50 __lock_acquire+0x10ea/0x1d90 lock_acquire+0xb5/0x2b0 ? flush_workqueue+0x67/0x5e0 ? lockdep_init_map_type+0x47/0x220 flush_workqueue+0x91/0x5e0 ? flush_workqueue+0x67/0x5e0 ? verify_cpu+0xf0/0x100 drain_workqueue+0xa0/0x110 destroy_workqueue+0x36/0x250 __loop_clr_fd+0x9a/0x660 [loop] ? blkdev_ioctl+0x8d/0x2a0 block_ioctl+0x3f/0x50 __x64_sys_ioctl+0x80/0xb0 do_syscall_64+0x38/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x7fc21255d4cb So instead save the bdev and do the put once we've dropped the sb writers lock in order to avoid the lockdep recursion. Reviewed-by: Anand Jain <[email protected]> Signed-off-by: Josef Bacik <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 8f96a5b commit 3fa421d

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

fs/btrfs/ioctl.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3223,6 +3223,8 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
32233223
struct inode *inode = file_inode(file);
32243224
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
32253225
struct btrfs_ioctl_vol_args_v2 *vol_args;
3226+
struct block_device *bdev = NULL;
3227+
fmode_t mode;
32263228
int ret;
32273229
bool cancel = false;
32283230

@@ -3255,9 +3257,9 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
32553257
/* Exclusive operation is now claimed */
32563258

32573259
if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3258-
ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3260+
ret = btrfs_rm_device(fs_info, NULL, vol_args->devid, &bdev, &mode);
32593261
else
3260-
ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3262+
ret = btrfs_rm_device(fs_info, vol_args->name, 0, &bdev, &mode);
32613263

32623264
btrfs_exclop_finish(fs_info);
32633265

@@ -3273,6 +3275,8 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
32733275
kfree(vol_args);
32743276
err_drop:
32753277
mnt_drop_write_file(file);
3278+
if (bdev)
3279+
blkdev_put(bdev, mode);
32763280
return ret;
32773281
}
32783282

@@ -3281,6 +3285,8 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
32813285
struct inode *inode = file_inode(file);
32823286
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
32833287
struct btrfs_ioctl_vol_args *vol_args;
3288+
struct block_device *bdev = NULL;
3289+
fmode_t mode;
32843290
int ret;
32853291
bool cancel;
32863292

@@ -3302,7 +3308,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
33023308
ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
33033309
cancel);
33043310
if (ret == 0) {
3305-
ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3311+
ret = btrfs_rm_device(fs_info, vol_args->name, 0, &bdev, &mode);
33063312
if (!ret)
33073313
btrfs_info(fs_info, "disk deleted %s", vol_args->name);
33083314
btrfs_exclop_finish(fs_info);
@@ -3311,7 +3317,8 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
33113317
kfree(vol_args);
33123318
out_drop_write:
33133319
mnt_drop_write_file(file);
3314-
3320+
if (bdev)
3321+
blkdev_put(bdev, mode);
33153322
return ret;
33163323
}
33173324

fs/btrfs/volumes.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
20592059
}
20602060

20612061
int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2062-
u64 devid)
2062+
u64 devid, struct block_device **bdev, fmode_t *mode)
20632063
{
20642064
struct btrfs_device *device;
20652065
struct btrfs_fs_devices *cur_devices;
@@ -2173,15 +2173,26 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
21732173
mutex_unlock(&fs_devices->device_list_mutex);
21742174

21752175
/*
2176-
* at this point, the device is zero sized and detached from
2177-
* the devices list. All that's left is to zero out the old
2178-
* supers and free the device.
2176+
* At this point, the device is zero sized and detached from the
2177+
* devices list. All that's left is to zero out the old supers and
2178+
* free the device.
2179+
*
2180+
* We cannot call btrfs_close_bdev() here because we're holding the sb
2181+
* write lock, and blkdev_put() will pull in the ->open_mutex on the
2182+
* block device and it's dependencies. Instead just flush the device
2183+
* and let the caller do the final blkdev_put.
21792184
*/
2180-
if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2185+
if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
21812186
btrfs_scratch_superblocks(fs_info, device->bdev,
21822187
device->name->str);
2188+
if (device->bdev) {
2189+
sync_blockdev(device->bdev);
2190+
invalidate_bdev(device->bdev);
2191+
}
2192+
}
21832193

2184-
btrfs_close_bdev(device);
2194+
*bdev = device->bdev;
2195+
*mode = device->mode;
21852196
synchronize_rcu();
21862197
btrfs_free_device(device);
21872198

fs/btrfs/volumes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
472472
const u8 *uuid);
473473
void btrfs_free_device(struct btrfs_device *device);
474474
int btrfs_rm_device(struct btrfs_fs_info *fs_info,
475-
const char *device_path, u64 devid);
475+
const char *device_path, u64 devid,
476+
struct block_device **bdev, fmode_t *mode);
476477
void __exit btrfs_cleanup_fs_uuids(void);
477478
int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
478479
int btrfs_grow_device(struct btrfs_trans_handle *trans,

0 commit comments

Comments
 (0)