From nobody Mon Dec 15 22:04:27 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 464B5226549 for ; Thu, 6 Feb 2025 06:46:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738824381; cv=none; b=cSkq+IsqRmjTMlOgK9DWGWJgykEr6Yw1gkyI3mNJCy/qktB1rjbg03WQd4gr4o0zaQ0Lt67fxoVwo3F/cXGCi67Vu3t96Mxxsd2IwuRz9IA9kiZlanZFMpGf/lHGsC8U8qOa+wIh5cavFYD25TA7ihHVzcxSdI8mWfX6YMQpLVs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738824381; c=relaxed/simple; bh=Pfg/38nxS+DyvkOOtEkHQrsOJ2Ljhr4G+q80x5NJrSg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Cg5dTKf/w27w7Ezazi1WCBw2OVWeGqhomKCU1cAv0IazzU9e7Qls8XMIxVsSAG9nnveH6qjVUwy0WWyB5NDrbifw3SdnJg8W+ONf8hwdH2dCyZxn2lP3yVCMuvvnoYiBF/iZQm5SmmvBMK6+6Mb4PJAk9JpJPuRM7pGv/Zmpjog= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hOn34gxd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hOn34gxd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1316C4CEDD; Thu, 6 Feb 2025 06:46:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738824379; bh=Pfg/38nxS+DyvkOOtEkHQrsOJ2Ljhr4G+q80x5NJrSg=; h=From:To:Cc:Subject:Date:From; b=hOn34gxdatWeUyt+/K8Kl/kw7P54DCETWOyTLK3XFo3naZ67BaFM8WMNL/cY7pF/W walI+uN1fjaYWd1Y1KzsVsmz6BIy2TfTBk0jPCDlVFjNwG5bTbCh2XOOhdUACz11TL mbP6ZOipz7zHQ3a7ORHDWgDWJoUxhG9y55wNz3/Fah1BuJixyykXAvjRGK5BxNNGDX SCFlzOHbHP4U2whniYEACxIl6N9zQBH33FS4XlNADcUk5lZ7CejadwHDp+/jrjgBLj lm3bScFKueFUSCU6v+RaSrse5OY9FCZXde3pDmkofJLfJ5MlnacG48KlIdB4ZG/WjS kMEde0YTmtNew== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu Subject: [PATCH] f2fs: quota: fix to avoid warning in dquot_writeback_dquots() Date: Thu, 6 Feb 2025 14:44:51 +0800 Message-ID: <20250206064451.3088028-1-chao@kernel.org> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" F2FS-fs (dm-59): checkpoint=3Denable has some unwritten data. ------------[ cut here ]------------ WARNING: CPU: 6 PID: 8013 at fs/quota/dquot.c:691 dquot_writeback_dquots+0x= 2fc/0x308 pc : dquot_writeback_dquots+0x2fc/0x308 lr : f2fs_quota_sync+0xcc/0x1c4 Call trace: dquot_writeback_dquots+0x2fc/0x308 f2fs_quota_sync+0xcc/0x1c4 f2fs_write_checkpoint+0x3d4/0x9b0 f2fs_issue_checkpoint+0x1bc/0x2c0 f2fs_sync_fs+0x54/0x150 f2fs_do_sync_file+0x2f8/0x814 __f2fs_ioctl+0x1960/0x3244 f2fs_ioctl+0x54/0xe0 __arm64_sys_ioctl+0xa8/0xe4 invoke_syscall+0x58/0x114 checkpoint and f2fs_remount may race as below, resulting triggering warning in dquot_writeback_dquots(). atomic write remount - do_remount - down_write(&sb->s_umount= ); - f2fs_remount - ioctl - f2fs_do_sync_file - f2fs_sync_fs - f2fs_write_checkpoint - block_operations - locked =3D down_read_trylock(&sbi->sb->s_umount) : fail to lock due to the write lock was held by remount - up_write(&sb->s_umount); - f2fs_quota_sync - dquot_writeback_dquots - WARN_ON_ONCE(!rwsem_is_locked(&sb->s_umount)) : trigger warning because s_umount lock was unlocked by remount If checkpoint comes from mount/umount/remount/freeze/quotactl, caller of checkpoint has already held s_umount lock, calling dquot_writeback_dquots() in the context should be safe. So let's record task to sbi->umount_lock_holder, so that checkpoint can know whether the lock has held in the context or not by checking current w/ it. In addition, in order to misrepresent caller of checkpoint, we should not allow to trigger async checkpoint for those callers: mount/umount/remount/ freeze/quotactl. Fixes: af033b2aa8a8 ("f2fs: guarantee journalled quota data by checkpoint") Signed-off-by: Chao Yu --- fs/f2fs/checkpoint.c | 17 ++++++++----- fs/f2fs/f2fs.h | 3 ++- fs/f2fs/super.c | 59 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c index efda9a022981..baff639ac0c4 100644 --- a/fs/f2fs/checkpoint.c +++ b/fs/f2fs/checkpoint.c @@ -1237,7 +1237,8 @@ static int block_operations(struct f2fs_sb_info *sbi) retry_flush_quotas: f2fs_lock_all(sbi); if (__need_flush_quota(sbi)) { - int locked; + bool need_lock =3D sbi->umount_lock_holder !=3D current; + bool locked =3D false; =20 if (++cnt > DEFAULT_RETRY_QUOTA_FLUSH_COUNT) { set_sbi_flag(sbi, SBI_QUOTA_SKIP_FLUSH); @@ -1246,10 +1247,13 @@ static int block_operations(struct f2fs_sb_info *sb= i) } f2fs_unlock_all(sbi); =20 - /* only failed during mount/umount/freeze/quotactl */ - locked =3D down_read_trylock(&sbi->sb->s_umount); - f2fs_quota_sync(sbi->sb, -1); - if (locked) + /* don't grab s_umount lock during mount/umount/remount/freeze/quotactl = */ + if (need_lock && !down_read_trylock(&sbi->sb->s_umount)) { + cond_resched(); + goto retry_flush_quotas; + } + f2fs_do_quota_sync(sbi->sb, -1); + if (need_lock) up_read(&sbi->sb->s_umount); cond_resched(); goto retry_flush_quotas; @@ -1867,7 +1871,8 @@ int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi) struct cp_control cpc; =20 cpc.reason =3D __get_cp_reason(sbi); - if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason !=3D CP_SYNC) { + if (!test_opt(sbi, MERGE_CHECKPOINT) || cpc.reason !=3D CP_SYNC || + sbi->umount_lock_holder =3D=3D current) { int ret; =20 f2fs_down_write(&sbi->gc_lock); diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 62b7fed1514a..7174dea641e9 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -1670,6 +1670,7 @@ struct f2fs_sb_info { =20 unsigned int nquota_files; /* # of quota sysfile */ struct f2fs_rwsem quota_sem; /* blocking cp for flags */ + struct task_struct *umount_lock_holder; /* s_umount lock holder */ =20 /* # of pages, see count_type */ atomic_t nr_pages[NR_COUNT_TYPE]; @@ -3652,7 +3653,7 @@ int f2fs_inode_dirtied(struct inode *inode, bool sync= ); void f2fs_inode_synced(struct inode *inode); int f2fs_dquot_initialize(struct inode *inode); int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly); -int f2fs_quota_sync(struct super_block *sb, int type); +int f2fs_do_quota_sync(struct super_block *sb, int type); loff_t max_file_blocks(struct inode *inode); void f2fs_quota_off_umount(struct super_block *sb); void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag); diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index ef639a6d82e5..cb9d7b6fa3ad 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -1740,22 +1740,28 @@ int f2fs_sync_fs(struct super_block *sb, int sync) =20 static int f2fs_freeze(struct super_block *sb) { + struct f2fs_sb_info *sbi =3D F2FS_SB(sb); + if (f2fs_readonly(sb)) return 0; =20 /* IO error happened before */ - if (unlikely(f2fs_cp_error(F2FS_SB(sb)))) + if (unlikely(f2fs_cp_error(sbi))) return -EIO; =20 /* must be clean, since sync_filesystem() was already called */ - if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY)) + if (is_sbi_flag_set(sbi, SBI_IS_DIRTY)) return -EINVAL; =20 + sbi->umount_lock_holder =3D current; + /* Let's flush checkpoints and stop the thread. */ - f2fs_flush_ckpt_thread(F2FS_SB(sb)); + f2fs_flush_ckpt_thread(sbi); + + sbi->umount_lock_holder =3D NULL; =20 /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */ - set_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING); + set_sbi_flag(sbi, SBI_IS_FREEZING); return 0; } =20 @@ -2332,6 +2338,8 @@ static int f2fs_remount(struct super_block *sb, int *= flags, char *data) org_mount_opt =3D sbi->mount_opt; old_sb_flags =3D sb->s_flags; =20 + sbi->umount_lock_holder =3D current; + #ifdef CONFIG_QUOTA org_mount_opt.s_jquota_fmt =3D F2FS_OPTION(sbi).s_jquota_fmt; for (i =3D 0; i < MAXQUOTAS; i++) { @@ -2555,6 +2563,8 @@ static int f2fs_remount(struct super_block *sb, int *= flags, char *data) =20 limit_reserve_root(sbi); *flags =3D (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME); + + sbi->umount_lock_holder =3D NULL; return 0; restore_checkpoint: if (need_enable_checkpoint) { @@ -2595,6 +2605,8 @@ static int f2fs_remount(struct super_block *sb, int *= flags, char *data) #endif sbi->mount_opt =3D org_mount_opt; sb->s_flags =3D old_sb_flags; + + sbi->umount_lock_holder =3D NULL; return err; } =20 @@ -2911,7 +2923,7 @@ static int f2fs_quota_sync_file(struct f2fs_sb_info *= sbi, int type) return ret; } =20 -int f2fs_quota_sync(struct super_block *sb, int type) +int f2fs_do_quota_sync(struct super_block *sb, int type) { struct f2fs_sb_info *sbi =3D F2FS_SB(sb); struct quota_info *dqopt =3D sb_dqopt(sb); @@ -2959,6 +2971,16 @@ int f2fs_quota_sync(struct super_block *sb, int type) return ret; } =20 +static int f2fs_quota_sync(struct super_block *sb, int type) +{ + int ret; + + F2FS_SB(sb)->umount_lock_holder =3D current; + ret =3D f2fs_do_quota_sync(sb, type); + F2FS_SB(sb)->umount_lock_holder =3D NULL; + return ret; +} + static int f2fs_quota_on(struct super_block *sb, int type, int format_id, const struct path *path) { @@ -2974,30 +2996,33 @@ static int f2fs_quota_on(struct super_block *sb, in= t type, int format_id, if (path->dentry->d_sb !=3D sb) return -EXDEV; =20 - err =3D f2fs_quota_sync(sb, type); + F2FS_SB(sb)->umount_lock_holder =3D current; + + err =3D f2fs_do_quota_sync(sb, type); if (err) - return err; + goto out; =20 inode =3D d_inode(path->dentry); =20 err =3D filemap_fdatawrite(inode->i_mapping); if (err) - return err; + goto out; =20 err =3D filemap_fdatawait(inode->i_mapping); if (err) - return err; + goto out; =20 err =3D dquot_quota_on(sb, type, format_id, path); if (err) - return err; + goto out; =20 inode_lock(inode); F2FS_I(inode)->i_flags |=3D F2FS_QUOTA_DEFAULT_FL; f2fs_set_inode_flags(inode); inode_unlock(inode); f2fs_mark_inode_dirty_sync(inode, false); - +out: + F2FS_SB(sb)->umount_lock_holder =3D NULL; return 0; } =20 @@ -3009,7 +3034,7 @@ static int __f2fs_quota_off(struct super_block *sb, i= nt type) if (!inode || !igrab(inode)) return dquot_quota_off(sb, type); =20 - err =3D f2fs_quota_sync(sb, type); + err =3D f2fs_do_quota_sync(sb, type); if (err) goto out_put; =20 @@ -3032,6 +3057,8 @@ static int f2fs_quota_off(struct super_block *sb, int= type) struct f2fs_sb_info *sbi =3D F2FS_SB(sb); int err; =20 + F2FS_SB(sb)->umount_lock_holder =3D current; + err =3D __f2fs_quota_off(sb, type); =20 /* @@ -3041,6 +3068,9 @@ static int f2fs_quota_off(struct super_block *sb, int= type) */ if (is_journalled_quota(sbi)) set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR); + + F2FS_SB(sb)->umount_lock_holder =3D NULL; + return err; } =20 @@ -4715,6 +4745,7 @@ static int f2fs_fill_super(struct super_block *sb, vo= id *data, int silent) if (err) goto free_compress_inode; =20 + sbi->umount_lock_holder =3D current; #ifdef CONFIG_QUOTA /* Enable quota usage during mount */ if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) { @@ -4841,6 +4872,8 @@ static int f2fs_fill_super(struct super_block *sb, vo= id *data, int silent) f2fs_update_time(sbi, CP_TIME); f2fs_update_time(sbi, REQ_TIME); clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK); + + sbi->umount_lock_holder =3D NULL; return 0; =20 sync_free_meta: @@ -4945,6 +4978,8 @@ static void kill_f2fs_super(struct super_block *sb) struct f2fs_sb_info *sbi =3D F2FS_SB(sb); =20 if (sb->s_root) { + sbi->umount_lock_holder =3D current; + set_sbi_flag(sbi, SBI_IS_CLOSE); f2fs_stop_gc_thread(sbi); f2fs_stop_discard_thread(sbi); --=20 2.48.1.502.g6dc24dfdaf-goog