From nobody Sat Feb 7 23:12:10 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75D76C77B7A for ; Tue, 23 May 2023 03:58:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234595AbjEWD6n (ORCPT ); Mon, 22 May 2023 23:58:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230345AbjEWD6b (ORCPT ); Mon, 22 May 2023 23:58:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59780E9; Mon, 22 May 2023 20:58:30 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BAC14624BC; Tue, 23 May 2023 03:58:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7CA9FC433D2; Tue, 23 May 2023 03:58:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684814309; bh=FniiWmm9rXXh4clLmDfg99004+nvesmQImAp7CGl4fs=; h=From:To:Cc:Subject:Date:From; b=mS8d4fDz0lmWfjIN8n86vCloGS4zt1Z7Wi+oX4BuPwhUxkw4bckWr8fieU7fPHnDL YheukgIxVrsA02N7vw4DScURHobA/cGDMMEQ9sTIdCAmyvT52kE6zxmnujG6vFMLVI II2qfFR8mU2yYSOzCEY4vaik90jjmpYmv8b+Lt1IHzNqVlLJCKUKjNrtnOIqSZEq1G ZT7J8RK9KIjPE+RpJ72RotUjTJppcBOFuyMbgYGLTagXr+tZeL7ICrCok0VOHiDWp3 wJqTX+BsX8oTATY1/fCCcB3E0Mjln4o2LXh9EVTfVQN9rshbfa0mDfW138w0yYrKGx H6wUMZ00IaDWg== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , stable@vger.kernel.org, syzbot+d015b6c2fbb5c383bf08@syzkaller.appspotmail.com Subject: [PATCH] f2fs: don't reset unchangable mount option in f2fs_remount() Date: Tue, 23 May 2023 11:58:22 +0800 Message-Id: <20230523035822.578123-1-chao@kernel.org> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" syzbot reports a bug as below: general protection fault, probably for non-canonical address 0xdffffc000000= 0009: 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: Reported-by: syzbot+d015b6c2fbb5c383bf08@syzkaller.appspotmail.com Closes: https://lore.kernel.org/linux-f2fs-devel/20230522124203.3838360-1-c= hao@kernel.org Signed-off-by: Chao Yu --- fs/f2fs/super.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index aeed413cf5cd..96bf7e727175 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -2097,9 +2097,22 @@ static int f2fs_show_options(struct seq_file *seq, s= truct dentry *root) return 0; } =20 -static void default_options(struct f2fs_sb_info *sbi) +static void default_options(struct f2fs_sb_info *sbi, bool remount) { /* init some FS parameters */ + if (!remount) { + set_opt(sbi, READ_EXTENT_CACHE); + clear_opt(sbi, DISABLE_CHECKPOINT); + + if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) + set_opt(sbi, DISCARD); + + if (f2fs_sb_has_blkzoned(sbi)) + F2FS_OPTION(sbi).discard_unit =3D DISCARD_UNIT_SECTION; + else + F2FS_OPTION(sbi).discard_unit =3D DISCARD_UNIT_BLOCK; + } + if (f2fs_sb_has_readonly(sbi)) F2FS_OPTION(sbi).active_logs =3D NR_CURSEG_RO_TYPE; else @@ -2129,23 +2142,16 @@ static void default_options(struct f2fs_sb_info *sb= i) set_opt(sbi, INLINE_XATTR); set_opt(sbi, INLINE_DATA); set_opt(sbi, INLINE_DENTRY); - set_opt(sbi, READ_EXTENT_CACHE); set_opt(sbi, NOHEAP); - clear_opt(sbi, DISABLE_CHECKPOINT); set_opt(sbi, MERGE_CHECKPOINT); F2FS_OPTION(sbi).unusable_cap =3D 0; sbi->sb->s_flags |=3D SB_LAZYTIME; if (!f2fs_is_readonly(sbi)) set_opt(sbi, FLUSH_MERGE); - if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi)) - set_opt(sbi, DISCARD); - if (f2fs_sb_has_blkzoned(sbi)) { + if (f2fs_sb_has_blkzoned(sbi)) F2FS_OPTION(sbi).fs_mode =3D FS_MODE_LFS; - F2FS_OPTION(sbi).discard_unit =3D DISCARD_UNIT_SECTION; - } else { + else F2FS_OPTION(sbi).fs_mode =3D FS_MODE_ADAPTIVE; - F2FS_OPTION(sbi).discard_unit =3D DISCARD_UNIT_BLOCK; - } =20 #ifdef CONFIG_F2FS_FS_XATTR set_opt(sbi, XATTR_USER); @@ -2317,7 +2323,7 @@ static int f2fs_remount(struct super_block *sb, int *= flags, char *data) clear_sbi_flag(sbi, SBI_NEED_SB_WRITE); } =20 - default_options(sbi); + default_options(sbi, true); =20 /* parse mount options */ err =3D parse_options(sb, data, true); @@ -4377,7 +4383,7 @@ static int f2fs_fill_super(struct super_block *sb, vo= id *data, int silent) sbi->s_chksum_seed =3D f2fs_chksum(sbi, ~0, raw_super->uuid, sizeof(raw_super->uuid)); =20 - default_options(sbi); + default_options(sbi, false); /* parse mount options */ options =3D kstrdup((const char *)data, GFP_KERNEL); if (data && !options) { --=20 2.40.1