Skip to content

Commit e2fb24c

Browse files
chaseyugregkh
authored andcommitted
f2fs: don't reset unchangable mount option in f2fs_remount()
[ Upstream commit 458c15d ] syzbot reports a bug as below: general protection fault, probably for non-canonical address 0xdffffc0000000009: 0000 [#1] PREEMPT SMP KASAN RIP: 0010:__lock_acquire+0x69/0x2000 kernel/locking/lockdep.c:4942 Call Trace: lock_acquire+0x1e3/0x520 kernel/locking/lockdep.c:5691 __raw_write_lock include/linux/rwlock_api_smp.h:209 [inline] _raw_write_lock+0x2e/0x40 kernel/locking/spinlock.c:300 __drop_extent_tree+0x3ac/0x660 fs/f2fs/extent_cache.c:1100 f2fs_drop_extent_tree+0x17/0x30 fs/f2fs/extent_cache.c:1116 f2fs_insert_range+0x2d5/0x3c0 fs/f2fs/file.c:1664 f2fs_fallocate+0x4e4/0x6d0 fs/f2fs/file.c:1838 vfs_fallocate+0x54b/0x6b0 fs/open.c:324 ksys_fallocate fs/open.c:347 [inline] __do_sys_fallocate fs/open.c:355 [inline] __se_sys_fallocate fs/open.c:353 [inline] __x64_sys_fallocate+0xbd/0x100 fs/open.c:353 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd The root cause is race condition as below: - since it tries to remount rw filesystem, so that do_remount won't call sb_prepare_remount_readonly to block fallocate, there may be race condition in between remount and fallocate. - in f2fs_remount(), default_options() will reset mount option to default one, and then update it based on result of parse_options(), so there is a hole which race condition can happen. Thread A Thread B - f2fs_fill_super - parse_options - clear_opt(READ_EXTENT_CACHE) - f2fs_remount - default_options - set_opt(READ_EXTENT_CACHE) - f2fs_fallocate - f2fs_insert_range - f2fs_drop_extent_tree - __drop_extent_tree - __may_extent_tree - test_opt(READ_EXTENT_CACHE) return true - write_lock(&et->lock) access NULL pointer - parse_options - clear_opt(READ_EXTENT_CACHE) Cc: <stable@vger.kernel.org> Reported-by: syzbot+d015b6c2fbb5c383bf08@syzkaller.appspotmail.com Closes: https://lore.kernel.org/linux-f2fs-devel/20230522124203.3838360-1-chao@kernel.org Signed-off-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 6ba0594 commit e2fb24c

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

fs/f2fs/super.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,9 +2040,22 @@ static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
20402040
return 0;
20412041
}
20422042

2043-
static void default_options(struct f2fs_sb_info *sbi)
2043+
static void default_options(struct f2fs_sb_info *sbi, bool remount)
20442044
{
20452045
/* init some FS parameters */
2046+
if (!remount) {
2047+
set_opt(sbi, READ_EXTENT_CACHE);
2048+
clear_opt(sbi, DISABLE_CHECKPOINT);
2049+
2050+
if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2051+
set_opt(sbi, DISCARD);
2052+
2053+
if (f2fs_sb_has_blkzoned(sbi))
2054+
F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2055+
else
2056+
F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2057+
}
2058+
20462059
if (f2fs_sb_has_readonly(sbi))
20472060
F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
20482061
else
@@ -2065,23 +2078,16 @@ static void default_options(struct f2fs_sb_info *sbi)
20652078
set_opt(sbi, INLINE_XATTR);
20662079
set_opt(sbi, INLINE_DATA);
20672080
set_opt(sbi, INLINE_DENTRY);
2068-
set_opt(sbi, READ_EXTENT_CACHE);
20692081
set_opt(sbi, NOHEAP);
2070-
clear_opt(sbi, DISABLE_CHECKPOINT);
20712082
set_opt(sbi, MERGE_CHECKPOINT);
20722083
F2FS_OPTION(sbi).unusable_cap = 0;
20732084
sbi->sb->s_flags |= SB_LAZYTIME;
20742085
if (!f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb))
20752086
set_opt(sbi, FLUSH_MERGE);
2076-
if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
2077-
set_opt(sbi, DISCARD);
2078-
if (f2fs_sb_has_blkzoned(sbi)) {
2087+
if (f2fs_sb_has_blkzoned(sbi))
20792088
F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
2080-
F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
2081-
} else {
2089+
else
20822090
F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
2083-
F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
2084-
}
20852091

20862092
#ifdef CONFIG_F2FS_FS_XATTR
20872093
set_opt(sbi, XATTR_USER);
@@ -2253,7 +2259,7 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
22532259
clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
22542260
}
22552261

2256-
default_options(sbi);
2262+
default_options(sbi, true);
22572263

22582264
/* parse mount options */
22592265
err = parse_options(sb, data, true);
@@ -4150,7 +4156,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
41504156
sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
41514157
sizeof(raw_super->uuid));
41524158

4153-
default_options(sbi);
4159+
default_options(sbi, false);
41544160
/* parse mount options */
41554161
options = kstrdup((const char *)data, GFP_KERNEL);
41564162
if (data && !options) {

0 commit comments

Comments
 (0)