Skip to content

Commit ebed7d8

Browse files
josefbacikgregkh
authored andcommitted
btrfs: delay blkdev_put until after the device remove
[ Upstream commit 3fa421d ] 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 13d4c38 commit ebed7d8

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
@@ -3205,6 +3205,8 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
32053205
struct inode *inode = file_inode(file);
32063206
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
32073207
struct btrfs_ioctl_vol_args_v2 *vol_args;
3208+
struct block_device *bdev = NULL;
3209+
fmode_t mode;
32083210
int ret;
32093211
bool cancel = false;
32103212

@@ -3237,9 +3239,9 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
32373239
/* Exclusive operation is now claimed */
32383240

32393241
if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3240-
ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3242+
ret = btrfs_rm_device(fs_info, NULL, vol_args->devid, &bdev, &mode);
32413243
else
3242-
ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3244+
ret = btrfs_rm_device(fs_info, vol_args->name, 0, &bdev, &mode);
32433245

32443246
btrfs_exclop_finish(fs_info);
32453247

@@ -3255,6 +3257,8 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
32553257
kfree(vol_args);
32563258
err_drop:
32573259
mnt_drop_write_file(file);
3260+
if (bdev)
3261+
blkdev_put(bdev, mode);
32583262
return ret;
32593263
}
32603264

@@ -3263,6 +3267,8 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
32633267
struct inode *inode = file_inode(file);
32643268
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
32653269
struct btrfs_ioctl_vol_args *vol_args;
3270+
struct block_device *bdev = NULL;
3271+
fmode_t mode;
32663272
int ret;
32673273
bool cancel;
32683274

@@ -3284,7 +3290,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
32843290
ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
32853291
cancel);
32863292
if (ret == 0) {
3287-
ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3293+
ret = btrfs_rm_device(fs_info, vol_args->name, 0, &bdev, &mode);
32883294
if (!ret)
32893295
btrfs_info(fs_info, "disk deleted %s", vol_args->name);
32903296
btrfs_exclop_finish(fs_info);
@@ -3293,7 +3299,8 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
32933299
kfree(vol_args);
32943300
out_drop_write:
32953301
mnt_drop_write_file(file);
3296-
3302+
if (bdev)
3303+
blkdev_put(bdev, mode);
32973304
return ret;
32983305
}
32993306

fs/btrfs/volumes.c

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

21242124
int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2125-
u64 devid)
2125+
u64 devid, struct block_device **bdev, fmode_t *mode)
21262126
{
21272127
struct btrfs_device *device;
21282128
struct btrfs_fs_devices *cur_devices;
@@ -2236,15 +2236,26 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
22362236
mutex_unlock(&fs_devices->device_list_mutex);
22372237

22382238
/*
2239-
* at this point, the device is zero sized and detached from
2240-
* the devices list. All that's left is to zero out the old
2241-
* supers and free the device.
2239+
* At this point, the device is zero sized and detached from the
2240+
* devices list. All that's left is to zero out the old supers and
2241+
* free the device.
2242+
*
2243+
* We cannot call btrfs_close_bdev() here because we're holding the sb
2244+
* write lock, and blkdev_put() will pull in the ->open_mutex on the
2245+
* block device and it's dependencies. Instead just flush the device
2246+
* and let the caller do the final blkdev_put.
22422247
*/
2243-
if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2248+
if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
22442249
btrfs_scratch_superblocks(fs_info, device->bdev,
22452250
device->name->str);
2251+
if (device->bdev) {
2252+
sync_blockdev(device->bdev);
2253+
invalidate_bdev(device->bdev);
2254+
}
2255+
}
22462256

2247-
btrfs_close_bdev(device);
2257+
*bdev = device->bdev;
2258+
*mode = device->mode;
22482259
synchronize_rcu();
22492260
btrfs_free_device(device);
22502261

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)