Skip to content

Commit 084b6e7

Browse files
Qu Wenruomasoncl
authored andcommitted
btrfs: Fix a lockdep warning when running xfstest.
The following lockdep warning is triggered during xfstests: [ 1702.980872] ========================================================= [ 1702.981181] [ INFO: possible irq lock inversion dependency detected ] [ 1702.981482] 3.18.0-rc1 torvalds#27 Not tainted [ 1702.981781] --------------------------------------------------------- [ 1702.982095] kswapd0/77 just changed the state of lock: [ 1702.982415] (&delayed_node->mutex){+.+.-.}, at: [<ffffffffa03b0b51>] __btrfs_release_delayed_node+0x41/0x1f0 [btrfs] [ 1702.982794] but this lock took another, RECLAIM_FS-unsafe lock in the past: [ 1702.983160] (&fs_info->dev_replace.lock){+.+.+.} and interrupts could create inverse lock ordering between them. [ 1702.984675] other info that might help us debug this: [ 1702.985524] Chain exists of: &delayed_node->mutex --> &found->groups_sem --> &fs_info->dev_replace.lock [ 1702.986799] Possible interrupt unsafe locking scenario: [ 1702.987681] CPU0 CPU1 [ 1702.988137] ---- ---- [ 1702.988598] lock(&fs_info->dev_replace.lock); [ 1702.989069] local_irq_disable(); [ 1702.989534] lock(&delayed_node->mutex); [ 1702.990038] lock(&found->groups_sem); [ 1702.990494] <Interrupt> [ 1702.990938] lock(&delayed_node->mutex); [ 1702.991407] *** DEADLOCK *** It is because the btrfs_kobj_{add/rm}_device() will call memory allocation with GFP_KERNEL, which may flush fs page cache to free space, waiting for it self to do the commit, causing the deadlock. To solve the problem, move btrfs_kobj_{add/rm}_device() out of the dev_replace lock range, also involing split the btrfs_rm_dev_replace_srcdev() function into remove and free parts. Now only btrfs_rm_dev_replace_remove_srcdev() is called in dev_replace lock range, and kobj_{add/rm} and btrfs_rm_dev_replace_free_srcdev() are called out of the lock range. Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent ad27c0d commit 084b6e7

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

fs/btrfs/dev-replace.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,15 +577,11 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
577577
list_add(&tgt_device->dev_alloc_list, &fs_info->fs_devices->alloc_list);
578578
fs_info->fs_devices->rw_devices++;
579579

580-
/* replace the sysfs entry */
581-
btrfs_kobj_rm_device(fs_info, src_device);
582-
btrfs_kobj_add_device(fs_info, tgt_device);
583-
584580
btrfs_dev_replace_unlock(dev_replace);
585581

586582
btrfs_rm_dev_replace_blocked(fs_info);
587583

588-
btrfs_rm_dev_replace_srcdev(fs_info, src_device);
584+
btrfs_rm_dev_replace_remove_srcdev(fs_info, src_device);
589585

590586
btrfs_rm_dev_replace_unblocked(fs_info);
591587

@@ -600,6 +596,11 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
600596
mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
601597
mutex_unlock(&uuid_mutex);
602598

599+
/* replace the sysfs entry */
600+
btrfs_kobj_rm_device(fs_info, src_device);
601+
btrfs_kobj_add_device(fs_info, tgt_device);
602+
btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
603+
603604
/* write back the superblocks */
604605
trans = btrfs_start_transaction(root, 0);
605606
if (!IS_ERR(trans))

fs/btrfs/volumes.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,8 +1800,8 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
18001800
goto error_brelse;
18011801
}
18021802

1803-
void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1804-
struct btrfs_device *srcdev)
1803+
void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1804+
struct btrfs_device *srcdev)
18051805
{
18061806
struct btrfs_fs_devices *fs_devices;
18071807

@@ -1829,6 +1829,12 @@ void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
18291829

18301830
if (srcdev->bdev)
18311831
fs_devices->open_devices--;
1832+
}
1833+
1834+
void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1835+
struct btrfs_device *srcdev)
1836+
{
1837+
struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
18321838

18331839
call_rcu(&srcdev->rcu, free_device);
18341840

fs/btrfs/volumes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,10 @@ void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
448448
int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
449449
int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
450450
struct btrfs_fs_info *fs_info);
451-
void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
452-
struct btrfs_device *srcdev);
451+
void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
452+
struct btrfs_device *srcdev);
453+
void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
454+
struct btrfs_device *srcdev);
453455
void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
454456
struct btrfs_device *tgtdev);
455457
void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,

0 commit comments

Comments
 (0)