Skip to content

Commit 9e7cc91

Browse files
wangxiaoguangmasoncl
authored andcommitted
btrfs: fix fsfreeze hang caused by delayed iputs deal
When running fstests generic/068, sometimes we got below deadlock: xfs_io D ffff8800331dbb20 0 6697 6693 0x00000080 ffff8800331dbb20 ffff88007acfc140 ffff880034d895c0 ffff8800331dc000 ffff880032d243e8 fffffffeffffffff ffff880032d24400 0000000000000001 ffff8800331dbb38 ffffffff816a9045 ffff880034d895c0 ffff8800331dbba8 Call Trace: [<ffffffff816a9045>] schedule+0x35/0x80 [<ffffffff816abab2>] rwsem_down_read_failed+0xf2/0x140 [<ffffffff8118f5e1>] ? __filemap_fdatawrite_range+0xd1/0x100 [<ffffffff8134f978>] call_rwsem_down_read_failed+0x18/0x30 [<ffffffffa06631fc>] ? btrfs_alloc_block_rsv+0x2c/0xb0 [btrfs] [<ffffffff810d32b5>] percpu_down_read+0x35/0x50 [<ffffffff81217dfc>] __sb_start_write+0x2c/0x40 [<ffffffffa067f5d5>] start_transaction+0x2a5/0x4d0 [btrfs] [<ffffffffa067f857>] btrfs_join_transaction+0x17/0x20 [btrfs] [<ffffffffa068ba34>] btrfs_evict_inode+0x3c4/0x5d0 [btrfs] [<ffffffff81230a1a>] evict+0xba/0x1a0 [<ffffffff812316b6>] iput+0x196/0x200 [<ffffffffa06851d0>] btrfs_run_delayed_iputs+0x70/0xc0 [btrfs] [<ffffffffa067f1d8>] btrfs_commit_transaction+0x928/0xa80 [btrfs] [<ffffffffa0646df0>] btrfs_freeze+0x30/0x40 [btrfs] [<ffffffff81218040>] freeze_super+0xf0/0x190 [<ffffffff81229275>] do_vfs_ioctl+0x4a5/0x5c0 [<ffffffff81003176>] ? do_audit_syscall_entry+0x66/0x70 [<ffffffff810038cf>] ? syscall_trace_enter_phase1+0x11f/0x140 [<ffffffff81229409>] SyS_ioctl+0x79/0x90 [<ffffffff81003c12>] do_syscall_64+0x62/0x110 [<ffffffff816acbe1>] entry_SYSCALL64_slow_path+0x25/0x25 >From this warning, freeze_super() already holds SB_FREEZE_FS, but btrfs_freeze() will call btrfs_commit_transaction() again, if btrfs_commit_transaction() finds that it has delayed iputs to handle, it'll start_transaction(), which will try to get SB_FREEZE_FS lock again, then deadlock occurs. The root cause is that in btrfs, sync_filesystem(sb) does not make sure all metadata is updated. There still maybe some codes adding delayed iputs, see below sample race window: CPU1 | CPU2 |-> freeze_super() | |-> sync_filesystem(sb); | | |-> cleaner_kthread() | | |-> btrfs_delete_unused_bgs() | | |-> btrfs_remove_chunk() | | |-> btrfs_remove_block_group() | | |-> btrfs_add_delayed_iput() | | |-> sb->s_writers.frozen = SB_FREEZE_FS; | |-> sb_wait_write(sb, SB_FREEZE_FS); | | acquire SB_FREEZE_FS lock. | | | |-> btrfs_freeze() | |-> btrfs_commit_transaction() | |-> btrfs_run_delayed_iputs() | | will handle delayed iputs, | | that means start_transaction() | | will be called, which will try | | to get SB_FREEZE_FS lock. | To fix this issue, introduce a "int fs_frozen" to record internally whether fs has been frozen. If fs has been frozen, we can not handle delayed iputs. Signed-off-by: Wang Xiaoguang <[email protected]> Reviewed-by: David Sterba <[email protected]> [ add comment to btrfs_freeze ] Signed-off-by: David Sterba <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent 1851309 commit 9e7cc91

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

fs/btrfs/ctree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,8 @@ struct btrfs_fs_info {
10801080
struct list_head pinned_chunks;
10811081

10821082
int creating_free_space_tree;
1083+
/* Used to record internally whether fs has been frozen */
1084+
int fs_frozen;
10831085
};
10841086

10851087
struct btrfs_subvolume_writers {

fs/btrfs/disk-io.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,6 +2631,7 @@ int open_ctree(struct super_block *sb,
26312631
atomic_set(&fs_info->qgroup_op_seq, 0);
26322632
atomic_set(&fs_info->reada_works_cnt, 0);
26332633
atomic64_set(&fs_info->tree_mod_seq, 0);
2634+
fs_info->fs_frozen = 0;
26342635
fs_info->sb = sb;
26352636
fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
26362637
fs_info->metadata_ratio = 0;

fs/btrfs/super.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,13 @@ static int btrfs_freeze(struct super_block *sb)
22412241
struct btrfs_trans_handle *trans;
22422242
struct btrfs_root *root = btrfs_sb(sb)->tree_root;
22432243

2244+
root->fs_info->fs_frozen = 1;
2245+
/*
2246+
* We don't need a barrier here, we'll wait for any transaction that
2247+
* could be in progress on other threads (and do delayed iputs that
2248+
* we want to avoid on a frozen filesystem), or do the commit
2249+
* ourselves.
2250+
*/
22442251
trans = btrfs_attach_transaction_barrier(root);
22452252
if (IS_ERR(trans)) {
22462253
/* no transaction, don't bother */
@@ -2251,6 +2258,14 @@ static int btrfs_freeze(struct super_block *sb)
22512258
return btrfs_commit_transaction(trans, root);
22522259
}
22532260

2261+
static int btrfs_unfreeze(struct super_block *sb)
2262+
{
2263+
struct btrfs_root *root = btrfs_sb(sb)->tree_root;
2264+
2265+
root->fs_info->fs_frozen = 0;
2266+
return 0;
2267+
}
2268+
22542269
static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
22552270
{
22562271
struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
@@ -2299,6 +2314,7 @@ static const struct super_operations btrfs_super_ops = {
22992314
.statfs = btrfs_statfs,
23002315
.remount_fs = btrfs_remount,
23012316
.freeze_fs = btrfs_freeze,
2317+
.unfreeze_fs = btrfs_unfreeze,
23022318
};
23032319

23042320
static const struct file_operations btrfs_ctl_fops = {

fs/btrfs/transaction.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2278,8 +2278,13 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
22782278

22792279
kmem_cache_free(btrfs_trans_handle_cachep, trans);
22802280

2281+
/*
2282+
* If fs has been frozen, we can not handle delayed iputs, otherwise
2283+
* it'll result in deadlock about SB_FREEZE_FS.
2284+
*/
22812285
if (current != root->fs_info->transaction_kthread &&
2282-
current != root->fs_info->cleaner_kthread)
2286+
current != root->fs_info->cleaner_kthread &&
2287+
!root->fs_info->fs_frozen)
22832288
btrfs_run_delayed_iputs(root);
22842289

22852290
return ret;

0 commit comments

Comments
 (0)