Skip to content

Commit a029825

Browse files
chaseyugregkh
authored andcommitted
f2fs: quota: fix potential deadlock
[ Upstream commit 9de71ed ] xfstest generic/587 reports a deadlock issue as below: ====================================================== WARNING: possible circular locking dependency detected 5.14.0-rc1 #69 Not tainted ------------------------------------------------------ repquota/8606 is trying to acquire lock: ffff888022ac9320 (&sb->s_type->i_mutex_key#18){+.+.}-{3:3}, at: f2fs_quota_sync+0x207/0x300 [f2fs] but task is already holding lock: ffff8880084bcde8 (&sbi->quota_sem){.+.+}-{3:3}, at: f2fs_quota_sync+0x59/0x300 [f2fs] which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #2 (&sbi->quota_sem){.+.+}-{3:3}: __lock_acquire+0x648/0x10b0 lock_acquire+0x128/0x470 down_read+0x3b/0x2a0 f2fs_quota_sync+0x59/0x300 [f2fs] f2fs_quota_on+0x48/0x100 [f2fs] do_quotactl+0x5e3/0xb30 __x64_sys_quotactl+0x23a/0x4e0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae -> #1 (&sbi->cp_rwsem){++++}-{3:3}: __lock_acquire+0x648/0x10b0 lock_acquire+0x128/0x470 down_read+0x3b/0x2a0 f2fs_unlink+0x353/0x670 [f2fs] vfs_unlink+0x1c7/0x380 do_unlinkat+0x413/0x4b0 __x64_sys_unlinkat+0x50/0xb0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae -> #0 (&sb->s_type->i_mutex_key#18){+.+.}-{3:3}: check_prev_add+0xdc/0xb30 validate_chain+0xa67/0xb20 __lock_acquire+0x648/0x10b0 lock_acquire+0x128/0x470 down_write+0x39/0xc0 f2fs_quota_sync+0x207/0x300 [f2fs] do_quotactl+0xaff/0xb30 __x64_sys_quotactl+0x23a/0x4e0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae other info that might help us debug this: Chain exists of: &sb->s_type->i_mutex_key#18 --> &sbi->cp_rwsem --> &sbi->quota_sem Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&sbi->quota_sem); lock(&sbi->cp_rwsem); lock(&sbi->quota_sem); lock(&sb->s_type->i_mutex_key#18); *** DEADLOCK *** 3 locks held by repquota/8606: #0: ffff88801efac0e0 (&type->s_umount_key#53){++++}-{3:3}, at: user_get_super+0xd9/0x190 #1: ffff8880084bc380 (&sbi->cp_rwsem){++++}-{3:3}, at: f2fs_quota_sync+0x3e/0x300 [f2fs] #2: ffff8880084bcde8 (&sbi->quota_sem){.+.+}-{3:3}, at: f2fs_quota_sync+0x59/0x300 [f2fs] stack backtrace: CPU: 6 PID: 8606 Comm: repquota Not tainted 5.14.0-rc1 #69 Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006 Call Trace: dump_stack_lvl+0xce/0x134 dump_stack+0x17/0x20 print_circular_bug.isra.0.cold+0x239/0x253 check_noncircular+0x1be/0x1f0 check_prev_add+0xdc/0xb30 validate_chain+0xa67/0xb20 __lock_acquire+0x648/0x10b0 lock_acquire+0x128/0x470 down_write+0x39/0xc0 f2fs_quota_sync+0x207/0x300 [f2fs] do_quotactl+0xaff/0xb30 __x64_sys_quotactl+0x23a/0x4e0 do_syscall_64+0x3b/0x90 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x7f883b0b4efe The root cause is ABBA deadlock of inode lock and cp_rwsem, reorder locks in f2fs_quota_sync() as below to fix this issue: - lock inode - lock cp_rwsem - lock quota_sem Fixes: db6ec53 ("f2fs: add a rw_sem to cover quota flag changes") 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 bd74d6d commit a029825

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

fs/f2fs/super.c

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,64 +1994,76 @@ static int f2fs_enable_quotas(struct super_block *sb)
19941994
return 0;
19951995
}
19961996

1997+
static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
1998+
{
1999+
struct quota_info *dqopt = sb_dqopt(sbi->sb);
2000+
struct address_space *mapping = dqopt->files[type]->i_mapping;
2001+
int ret = 0;
2002+
2003+
ret = dquot_writeback_dquots(sbi->sb, type);
2004+
if (ret)
2005+
goto out;
2006+
2007+
ret = filemap_fdatawrite(mapping);
2008+
if (ret)
2009+
goto out;
2010+
2011+
/* if we are using journalled quota */
2012+
if (is_journalled_quota(sbi))
2013+
goto out;
2014+
2015+
ret = filemap_fdatawait(mapping);
2016+
2017+
truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2018+
out:
2019+
if (ret)
2020+
set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2021+
return ret;
2022+
}
2023+
19972024
int f2fs_quota_sync(struct super_block *sb, int type)
19982025
{
19992026
struct f2fs_sb_info *sbi = F2FS_SB(sb);
20002027
struct quota_info *dqopt = sb_dqopt(sb);
20012028
int cnt;
20022029
int ret;
20032030

2004-
/*
2005-
* do_quotactl
2006-
* f2fs_quota_sync
2007-
* down_read(quota_sem)
2008-
* dquot_writeback_dquots()
2009-
* f2fs_dquot_commit
2010-
* block_operation
2011-
* down_read(quota_sem)
2012-
*/
2013-
f2fs_lock_op(sbi);
2014-
2015-
down_read(&sbi->quota_sem);
2016-
ret = dquot_writeback_dquots(sb, type);
2017-
if (ret)
2018-
goto out;
2019-
20202031
/*
20212032
* Now when everything is written we can discard the pagecache so
20222033
* that userspace sees the changes.
20232034
*/
20242035
for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2025-
struct address_space *mapping;
20262036

20272037
if (type != -1 && cnt != type)
20282038
continue;
2029-
if (!sb_has_quota_active(sb, cnt))
2030-
continue;
20312039

2032-
mapping = dqopt->files[cnt]->i_mapping;
2040+
if (!sb_has_quota_active(sb, type))
2041+
return 0;
20332042

2034-
ret = filemap_fdatawrite(mapping);
2035-
if (ret)
2036-
goto out;
2043+
inode_lock(dqopt->files[cnt]);
20372044

2038-
/* if we are using journalled quota */
2039-
if (is_journalled_quota(sbi))
2040-
continue;
2045+
/*
2046+
* do_quotactl
2047+
* f2fs_quota_sync
2048+
* down_read(quota_sem)
2049+
* dquot_writeback_dquots()
2050+
* f2fs_dquot_commit
2051+
* block_operation
2052+
* down_read(quota_sem)
2053+
*/
2054+
f2fs_lock_op(sbi);
2055+
down_read(&sbi->quota_sem);
20412056

2042-
ret = filemap_fdatawait(mapping);
2043-
if (ret)
2044-
set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2057+
ret = f2fs_quota_sync_file(sbi, cnt);
2058+
2059+
up_read(&sbi->quota_sem);
2060+
f2fs_unlock_op(sbi);
20452061

2046-
inode_lock(dqopt->files[cnt]);
2047-
truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
20482062
inode_unlock(dqopt->files[cnt]);
2063+
2064+
if (ret)
2065+
break;
20492066
}
2050-
out:
2051-
if (ret)
2052-
set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2053-
up_read(&sbi->quota_sem);
2054-
f2fs_unlock_op(sbi);
20552067
return ret;
20562068
}
20572069

0 commit comments

Comments
 (0)