Skip to content

Commit ca8b201

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix to avoid potential deadlock
As Jiaming Zhang and syzbot reported, there is potential deadlock in f2fs as below: Chain exists of: &sbi->cp_rwsem --> fs_reclaim --> sb_internal#2 Possible unsafe locking scenario: CPU0 CPU1 ---- ---- rlock(sb_internal#2); lock(fs_reclaim); lock(sb_internal#2); rlock(&sbi->cp_rwsem); *** DEADLOCK *** 3 locks held by kswapd0/73: #0: ffffffff8e247a40 (fs_reclaim){+.+.}-{0:0}, at: balance_pgdat mm/vmscan.c:7015 [inline] #0: ffffffff8e247a40 (fs_reclaim){+.+.}-{0:0}, at: kswapd+0x951/0x2800 mm/vmscan.c:7389 #1: ffff8880118400e0 (&type->s_umount_key#50){.+.+}-{4:4}, at: super_trylock_shared fs/super.c:562 [inline] #1: ffff8880118400e0 (&type->s_umount_key#50){.+.+}-{4:4}, at: super_cache_scan+0x91/0x4b0 fs/super.c:197 #2: ffff888011840610 (sb_internal#2){.+.+}-{0:0}, at: f2fs_evict_inode+0x8d9/0x1b60 fs/f2fs/inode.c:890 stack backtrace: CPU: 0 UID: 0 PID: 73 Comm: kswapd0 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2~bpo12+1 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x189/0x250 lib/dump_stack.c:120 print_circular_bug+0x2ee/0x310 kernel/locking/lockdep.c:2043 check_noncircular+0x134/0x160 kernel/locking/lockdep.c:2175 check_prev_add kernel/locking/lockdep.c:3165 [inline] check_prevs_add kernel/locking/lockdep.c:3284 [inline] validate_chain+0xb9b/0x2140 kernel/locking/lockdep.c:3908 __lock_acquire+0xab9/0xd20 kernel/locking/lockdep.c:5237 lock_acquire+0x120/0x360 kernel/locking/lockdep.c:5868 down_read+0x46/0x2e0 kernel/locking/rwsem.c:1537 f2fs_down_read fs/f2fs/f2fs.h:2278 [inline] f2fs_lock_op fs/f2fs/f2fs.h:2357 [inline] f2fs_do_truncate_blocks+0x21c/0x10c0 fs/f2fs/file.c:791 f2fs_truncate_blocks+0x10a/0x300 fs/f2fs/file.c:867 f2fs_truncate+0x489/0x7c0 fs/f2fs/file.c:925 f2fs_evict_inode+0x9f2/0x1b60 fs/f2fs/inode.c:897 evict+0x504/0x9c0 fs/inode.c:810 f2fs_evict_inode+0x1dc/0x1b60 fs/f2fs/inode.c:853 evict+0x504/0x9c0 fs/inode.c:810 dispose_list fs/inode.c:852 [inline] prune_icache_sb+0x21b/0x2c0 fs/inode.c:1000 super_cache_scan+0x39b/0x4b0 fs/super.c:224 do_shrink_slab+0x6ef/0x1110 mm/shrinker.c:437 shrink_slab_memcg mm/shrinker.c:550 [inline] shrink_slab+0x7ef/0x10d0 mm/shrinker.c:628 shrink_one+0x28a/0x7c0 mm/vmscan.c:4955 shrink_many mm/vmscan.c:5016 [inline] lru_gen_shrink_node mm/vmscan.c:5094 [inline] shrink_node+0x315d/0x3780 mm/vmscan.c:6081 kswapd_shrink_node mm/vmscan.c:6941 [inline] balance_pgdat mm/vmscan.c:7124 [inline] kswapd+0x147c/0x2800 mm/vmscan.c:7389 kthread+0x70e/0x8a0 kernel/kthread.c:463 ret_from_fork+0x4bc/0x870 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245 </TASK> The root cause is deadlock among four locks as below: kswapd - fs_reclaim --- Lock A - shrink_one - evict - f2fs_evict_inode - sb_start_intwrite --- Lock B - iput - evict - f2fs_evict_inode - sb_start_intwrite --- Lock B - f2fs_truncate - f2fs_truncate_blocks - f2fs_do_truncate_blocks - f2fs_lock_op --- Lock C ioctl - f2fs_ioc_commit_atomic_write - f2fs_lock_op --- Lock C - __f2fs_commit_atomic_write - __replace_atomic_write_block - f2fs_get_dnode_of_data - __get_node_folio - f2fs_check_nid_range - f2fs_handle_error - f2fs_record_errors - f2fs_down_write --- Lock D open - do_open - do_truncate - security_inode_need_killpriv - f2fs_getxattr - lookup_all_xattrs - f2fs_handle_error - f2fs_record_errors - f2fs_down_write --- Lock D - f2fs_commit_super - read_mapping_folio - filemap_alloc_folio_noprof - prepare_alloc_pages - fs_reclaim_acquire --- Lock A In order to avoid such deadlock, we need to avoid grabbing sb_lock in f2fs_handle_error(), so, let's use asynchronous method instead: - remove f2fs_handle_error() implementation - rename f2fs_handle_error_async() to f2fs_handle_error() - spread f2fs_handle_error() Fixes: 95fa90c ("f2fs: support recording errors into superblock") Cc: stable@kernel.org Reported-by: syzbot+14b90e1156b9f6fc1266@syzkaller.appspotmail.com Closes: https://lore.kernel.org/linux-f2fs-devel/68eae49b.050a0220.ac43.0001.GAE@google.com Reported-by: Jiaming Zhang <r772577952@gmail.com> Closes: https://lore.kernel.org/lkml/CANypQFa-Gy9sD-N35o3PC+FystOWkNuN8pv6S75HLT0ga-Tzgw@mail.gmail.com Signed-off-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent c1cdb00 commit ca8b201

File tree

3 files changed

+1
-46
lines changed

3 files changed

+1
-46
lines changed

fs/f2fs/compress.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,7 @@ void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task)
759759
ret = -EFSCORRUPTED;
760760

761761
/* Avoid f2fs_commit_super in irq context */
762-
if (!in_task)
763-
f2fs_handle_error_async(sbi, ERROR_FAIL_DECOMPRESSION);
764-
else
765-
f2fs_handle_error(sbi, ERROR_FAIL_DECOMPRESSION);
762+
f2fs_handle_error(sbi, ERROR_FAIL_DECOMPRESSION);
766763
goto out_release;
767764
}
768765

fs/f2fs/f2fs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3800,7 +3800,6 @@ void f2fs_quota_off_umount(struct super_block *sb);
38003800
void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag);
38013801
void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason);
38023802
void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error);
3803-
void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error);
38043803
int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
38053804
int f2fs_sync_fs(struct super_block *sb, int sync);
38063805
int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);

fs/f2fs/super.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4544,48 +4544,7 @@ void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
45444544
spin_unlock_irqrestore(&sbi->error_lock, flags);
45454545
}
45464546

4547-
static bool f2fs_update_errors(struct f2fs_sb_info *sbi)
4548-
{
4549-
unsigned long flags;
4550-
bool need_update = false;
4551-
4552-
spin_lock_irqsave(&sbi->error_lock, flags);
4553-
if (sbi->error_dirty) {
4554-
memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
4555-
MAX_F2FS_ERRORS);
4556-
sbi->error_dirty = false;
4557-
need_update = true;
4558-
}
4559-
spin_unlock_irqrestore(&sbi->error_lock, flags);
4560-
4561-
return need_update;
4562-
}
4563-
4564-
static void f2fs_record_errors(struct f2fs_sb_info *sbi, unsigned char error)
4565-
{
4566-
int err;
4567-
4568-
f2fs_down_write(&sbi->sb_lock);
4569-
4570-
if (!f2fs_update_errors(sbi))
4571-
goto out_unlock;
4572-
4573-
err = f2fs_commit_super(sbi, false);
4574-
if (err)
4575-
f2fs_err_ratelimited(sbi,
4576-
"f2fs_commit_super fails to record errors:%u, err:%d",
4577-
error, err);
4578-
out_unlock:
4579-
f2fs_up_write(&sbi->sb_lock);
4580-
}
4581-
45824547
void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
4583-
{
4584-
f2fs_save_errors(sbi, error);
4585-
f2fs_record_errors(sbi, error);
4586-
}
4587-
4588-
void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error)
45894548
{
45904549
f2fs_save_errors(sbi, error);
45914550

0 commit comments

Comments
 (0)