[PATCH] f2fs: fix dirty quota page accounting during quota transitions

From: m9 . akhlaghpoor

Date: Wed Dec 02 2026 - 09:16:06 EST


F2FS_DIRTY_QDATA relies on S_NOQUOTA to decide whether a dirty page
should be counted. The problem is that this flag can change while a page
is still dirty.

During quota activation, a page can become dirty before S_NOQUOTA is
set and then be cleaned after it changes. We end up decrementing
F2FS_DIRTY_QDATA without a matching increment, which can leave the
counter negative and trigger a BUG in f2fs_put_super().

Fix this by tracking how many dirty quota pages each inode has
contributed to the counter. Updates are serialized with a per-inode
lock, and the count is reconciled when the quota state changes.
The exceptional quota-off paths are handled as well.

I tested this with the syzbot reproducer on a kernel built with LOCKDEP.
The run completed 206 F2FS mounts, exercised the quota-on path 205
times, and reached f2fs_put_super() 206 times without any observed
kernel warnings or BUGs.

Fixes: 2c8a4a282334 ("f2fs: expose quota information in debugfs")
Reported-by: syzbot+467d56faa7809a907f05@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=467d56faa7809a907f05
Signed-off-by: Amirhossein Akhlaghpour <m9.akhlaghpoor@xxxxxxxxx>
---

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 6941891..3cb995e 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -959,6 +959,9 @@ struct f2fs_inode_info {
unsigned int ioprio_hint; /* hint for IO priority */
struct f2fs_rwsem i_sem; /* protect fi info */
atomic_t dirty_pages; /* # of dirty pages */
+ spinlock_t qdata_lock; /* protects quota data accounting */
+ int qdata_pages; /* pages reflected in DIRTY_QDATA */
+ bool qdata_transition; /* quota state is changing */
f2fs_hash_t chash; /* hash value of given file name */
unsigned int clevel; /* maximum level of given file name */
struct task_struct *task; /* lookup and create consistency */
@@ -2866,13 +2869,48 @@ static inline void inc_cache_count(struct
f2fs_sb_info *sbi, int count_type)
set_sbi_flag(sbi, SBI_IS_DIRTY);
}

+
+static inline void f2fs_reconcile_qdata_pages(struct inode *inode)
+{
+ struct f2fs_inode_info *fi = F2FS_I(inode);
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+ int desired = (fi->qdata_transition || IS_NOQUOTA(inode)) ?
+ atomic_read(&fi->dirty_pages) : 0;
+ int delta = desired - fi->qdata_pages;
+
+ /* Caller holds fi->qdata_lock. */
+ if (!delta)
+ return;
+
+ atomic_add(delta, &sbi->nr_caches[F2FS_DIRTY_QDATA]);
+ fi->qdata_pages = desired;
+
+ if (delta > 0)
+ set_sbi_flag(sbi, SBI_IS_DIRTY);
+}
+
+static inline void f2fs_set_qdata_transition(struct inode *inode,
+ bool enabled)
+{
+ struct f2fs_inode_info *fi = F2FS_I(inode);
+
+ spin_lock(&fi->qdata_lock);
+ fi->qdata_transition = enabled;
+ f2fs_reconcile_qdata_pages(inode);
+ spin_unlock(&fi->qdata_lock);
+}
+
static inline void inode_inc_dirty_pages(struct inode *inode)
{
- atomic_inc(&F2FS_I(inode)->dirty_pages);
- inc_cache_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
- F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
- if (IS_NOQUOTA(inode))
- inc_cache_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
+ struct f2fs_inode_info *fi = F2FS_I(inode);
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+
+ spin_lock(&fi->qdata_lock);
+ atomic_inc(&fi->dirty_pages);
+ inc_cache_count(sbi, S_ISDIR(inode->i_mode) ?
+ F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
+ f2fs_reconcile_qdata_pages(inode);
+ spin_unlock(&fi->qdata_lock);
}

static inline void dec_cache_count(struct f2fs_sb_info *sbi, int count_type)
@@ -2882,15 +2920,19 @@ static inline void dec_cache_count(struct
f2fs_sb_info *sbi, int count_type)

static inline void inode_dec_dirty_pages(struct inode *inode)
{
+ struct f2fs_inode_info *fi = F2FS_I(inode);
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+
if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
- !S_ISLNK(inode->i_mode))
+ !S_ISLNK(inode->i_mode))
return;

- atomic_dec(&F2FS_I(inode)->dirty_pages);
- dec_cache_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
- F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
- if (IS_NOQUOTA(inode))
- dec_cache_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
+ spin_lock(&fi->qdata_lock);
+ atomic_dec(&fi->dirty_pages);
+ dec_cache_count(sbi, S_ISDIR(inode->i_mode) ?
+ F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
+ f2fs_reconcile_qdata_pages(inode);
+ spin_unlock(&fi->qdata_lock);
}

static inline void inc_atomic_write_cnt(struct inode *inode)
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 1071059..8c4d17f 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -1853,6 +1853,9 @@ static struct inode *f2fs_alloc_inode(struct
super_block *sb)

/* Initialize f2fs-specific inode info */
atomic_set(&fi->dirty_pages, 0);
+ spin_lock_init(&fi->qdata_lock);
+ fi->qdata_pages = 0;
+ fi->qdata_transition = false;
atomic_set(&fi->i_compr_blocks, 0);
atomic_set(&fi->open_count, 0);
atomic_set(&fi->writeback, 0);
@@ -3359,6 +3362,21 @@ static int f2fs_quota_convert_inline(struct inode *inode)
return err;
}

+static int f2fs_dquot_quota_off(struct super_block *sb, int type)
+{
+ struct inode *inode = sb_dqopt(sb)->files[type];
+ int err;
+
+ if (!inode || !igrab(inode))
+ return dquot_quota_off(sb, type);
+
+ f2fs_set_qdata_transition(inode, true);
+ err = dquot_quota_off(sb, type);
+ f2fs_set_qdata_transition(inode, false);
+ iput(inode);
+ return err;
+}
+
static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
{
int err;
@@ -3375,7 +3393,7 @@ static int f2fs_quota_on_mount(struct
f2fs_sb_info *sbi, int type)

err = f2fs_quota_convert_inline(sb_dqopt(sbi->sb)->files[type]);
if (err)
- dquot_quota_off(sbi->sb, type);
+ f2fs_dquot_quota_off(sbi->sb, type);
return err;
}

@@ -3422,7 +3440,10 @@ static int f2fs_quota_enable(struct super_block
*sb, int type, int format_id,

/* Don't account quota for quota files to avoid recursion */
inode_lock(qf_inode);
+ f2fs_set_qdata_transition(qf_inode, true);
qf_inode->i_flags |= S_NOQUOTA;
+ f2fs_set_qdata_transition(qf_inode, false);
+

if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
F2FS_I(qf_inode)->i_flags |= qf_flag;
@@ -3463,7 +3484,7 @@ static int f2fs_enable_quotas(struct super_block *sb)
f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d).
Please run fsck to fix.",
type, err);
for (type--; type >= 0; type--)
- dquot_quota_off(sb, type);
+ f2fs_dquot_quota_off(sb, type);
set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
return err;
}
@@ -3593,7 +3614,9 @@ static int f2fs_quota_on(struct super_block *sb,
int type, int format_id,
if (err)
goto out;

+ f2fs_set_qdata_transition(inode, true);
err = dquot_quota_on(sb, type, format_id, path);
+ f2fs_set_qdata_transition(inode, false);
if (err)
goto out;

@@ -3613,13 +3636,15 @@ static int __f2fs_quota_off(struct super_block
*sb, int type)
int err;

if (!inode || !igrab(inode))
- return dquot_quota_off(sb, type);
+ return f2fs_dquot_quota_off(sb, type);

err = f2fs_do_quota_sync(sb, type);
if (err)
goto out_put;

+ f2fs_set_qdata_transition(inode, true);
err = dquot_quota_off(sb, type);
+ f2fs_set_qdata_transition(inode, false);
if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
goto out_put;

@@ -3663,7 +3688,7 @@ void f2fs_quota_off_umount(struct super_block *sb)
for (type = 0; type < MAXQUOTAS; type++) {
err = __f2fs_quota_off(sb, type);
if (err) {
- int ret = dquot_quota_off(sb, type);
+ int ret = f2fs_dquot_quota_off(sb, type);

f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err:
%d, ret:%d), Please run fsck to fix it.",
type, err, ret);