ext4: protect ext4_acquire_dquot against freezing
PATCH 1/1 ext4: protect ext4_acquire_dquot against freezing
From: Ye Bin <[email protected]>
A writable open of a regular file reaches the filesystem's ->open() callback without holding any superblock write (freeze) protection, but ext4_file_open() -> dquot_file_open() -> dquot_initialize() may need to acquire a dquot and write the quota file. If the filesystem reaches SB_FREEZE_COMPLETE in that window, ext4_journal_check_start() hits
WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
and the open then sleeps in jbd2 until the filesystem is thawed.
The background and the earlier discussion of this problem are described in:
https://patchwork.kernel.org/project/linux-fsdevel/patch/[email protected]/
The initial attempt added mnt_want_write() for every writable open of a regular file in do_open(). Christian Brauner pointed out that this would make a plain open(O_WRONLY|O_RDWR) on a frozen filesystem block even when it never writes -- log files, editors and backup tools would hang rather than fail on a later write, which is a drastic and unacceptable uapi change. As Jan Kara noted, the write comes from the quota acquisition path, so it should be fixed there instead of penalizing the whole VFS layer. Jan also suggested using sb_start_intwrite() in the filesystem's .acquire_dquot method, which neither deadlocks nor changes VFS semantics. ocfs2 is already fine because ocfs2_start_trans() takes sb_start_intwrite() itself, so only ext4 needs the fix.
Protect the quota acquisition path itself: take sb_start_intwrite() around ext4_acquire_dquot() when no transaction is already running. Running transactions are already protected against freezing and taking sb_start_intwrite() inside one can deadlock.
--- fs/ext4/super.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
fs/ext4/super.c +19 -1
@@ -7110,11 +7110,25 @@ static int ext4_acquire_dquot(struct dquot *dquot) {int ret, err;handle_t *handle; +bool freeze_protected = false; + +/* +* Trying to sb_start_intwrite() in a running transaction +* can result in a deadlock. Further, running transactions +* are already protected from freezing. +*/ +if (!ext4_journal_current_handle ()) { +sb_start_intwrite (dquot->dq_sb); + freeze_protected = true; + } handle =ext4_journal_start (dquot_to_inode (dquot),EXT4_HT_QUOTA ,EXT4_QUOTA_INIT_BLOCKS (dquot->dq_sb)); -if (IS_ERR (handle)) +if (IS_ERR (handle)) { +if (freeze_protected) +sb_end_intwrite (dquot->dq_sb);return PTR_ERR (handle); + } ret =dquot_acquire (dquot);if (ret <0 ) ext4_error_err(dquot->dq_sb, -ret,
@@ -7123,6 +7137,10 @@ static int ext4_acquire_dquot(struct dquot *dquot) err =ext4_journal_stop (handle);if (!ret) ret = err; + +if (freeze_protected) +sb_end_intwrite (dquot->dq_sb); +return ret; }
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] ext4: deadlock with thaw_super() in ext4_acquire_dquot() via Q_XGETQUOTA --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] ext4: deadlock with thaw_super() in ext4_acquire_dquot() via Q_XGETQUOTA
--
commit f23e3eb68e6a88d3c8dec26c7efee607385b6388
Author: Ye Bin <[email protected]>
ext4: protect ext4_acquire_dquot against freezing
This commit wraps ext4_acquire_dquot() with sb_start_intwrite() and sb_end_intwrite() when no journal transaction is currently active to protect quota acquisition against filesystem freezing.
Quoted 9 lines
fs/ext4/super.c +1
-0@@ -7118,11 +7118,25 @@ static int ext4_acquire_dquot(struct dquot *dquot) {int ret, err;handle_t *handle; +bool freeze_protected = false;
fs/ext4/super.c static int ext4_acquire_dquot(struct dquot *dquot) · line 7131
Quoted 10 lines
fs/ext4/super.c +10
-0+ +/* +* Trying to sb_start_intwrite() in a running transaction +* can result in a deadlock. Further, running transactions +* are already protected from freezing. +*/ +if (!ext4_journal_current_handle ()) { +sb_start_intwrite (dquot->dq_sb); + freeze_protected = true; + }
[Severity: High]
Can calling sb_start_intwrite() here cause an ABBA deadlock between sb->s_umount and SB_FREEZE_FS when Q_XGETQUOTA or Q_XGETNEXTQUOTA runs on a frozen filesystem?
quotactl_cmd_write() returns 0 for Q_XGETQUOTA and Q_XGETNEXTQUOTA, so quotactl_block() and quotactl_fd() acquire down_read(&sb->s_umount) without waiting for the filesystem to thaw. If the queried dquot is not yet active in memory, dquot_get_dqblk() calls dqget() -> ext4_acquire_dquot() with no running transaction and blocks in sb_start_intwrite() while holding s_umount:
CPU0 CPU1
---- ----down_read(&sb->s_umount)
sb_freeze_unlock(sb, SB_FREEZE_FS)sb_start_intwrite(sb)
down_write(&sb->s_umount)
*** DEADLOCK ***
CPU0: do_quotactl() -> quota_getxquota() -> dquot_get_dqblk() ->
dqget() -> ext4_acquire_dquot()
CPU1: thaw_super() -> super_lock_excl() -> thaw_super_locked()
Once this happens, thaw_super() cannot acquire s_umount to unfreeze the filesystem, leaving the filesystem permanently wedged.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 56 lines · On 2026/11/23 20:02, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] ext4: deadlock withthaw_super()inext4_acquire_dquot()viaQ_XGETQUOTA
--commit
f23e3eb68e6a88d3c8dec26c7efee607385b6388
Author: Ye Bin <[email protected]>ext4: protect
ext4_acquire_dquotagainst freezingThis commit wraps
ext4_acquire_dquot()withsb_start_intwrite()andsb_end_intwrite()when no journal transaction is currently active to protect quota acquisition against filesystem freezing.Quoted 9 lines
fs/ext4/super.c +1
-0@@ -7118,11 +7118,25 @@ static int ext4_acquire_dquot(struct dquot *dquot) {int ret, err;handle_t *handle; +bool freeze_protected = false;fs/ext4/super.c static int ext4_acquire_dquot(struct dquot *dquot) · line 7131
Quoted 10 lines
fs/ext4/super.c +10
-0+ +/* +* Trying to sb_start_intwrite() in a running transaction +* can result in a deadlock. Further, running transactions +* are already protected from freezing. +*/ +if (!ext4_journal_current_handle ()) { +sb_start_intwrite (dquot->dq_sb); + freeze_protected = true; + }[Severity: High]
Can callingsb_start_intwrite()here cause an ABBA deadlock betweensb->s_umountandSB_FREEZE_FSwhenQ_XGETQUOTAorQ_XGETNEXTQUOTAruns on a frozen filesystem?
quotactl_cmd_write()returns 0 forQ_XGETQUOTAandQ_XGETNEXTQUOTA, soquotactl_block()andquotactl_fd()acquiredown_read(&sb->s_umount)without waiting for the filesystem to thaw. If the queried dquot is not yet active in memory,dquot_get_dqblk()callsdqget()->ext4_acquire_dquot()with no running transaction and blocks insb_start_intwrite()while holdings_umount:CPU0 CPU1
---- ----down_read(&sb->s_umount)
sb_freeze_unlock(sb, SB_FREEZE_FS)sb_start_intwrite(sb)down_write(&sb->s_umount)
Quoted 9 lines
*** DEADLOCK ***
CPU0:
do_quotactl()->quota_getxquota()->dquot_get_dqblk()->dqget() -> ext4_acquire_dquot()CPU1:
thaw_super()->super_lock_excl()->thaw_super_locked()Once this happens,
thaw_super()cannot acquires_umountto unfreeze the filesystem, leaving the filesystem permanently wedged.
Hi Jan,
Sashiko reported a deadlock with thaw_super() when Q_XGETQUOTA or Q_XGETNEXTQUOTA runs on a frozen filesystem.
Looking at this, I think the same reasoning as commit ccf370e43e08 ("quota: Forbid Q_GETQUOTA and Q_GETNEXTQUOTA for frozen filesystem") also applies to Q_XGETQUOTA/Q_XGETNEXTQUOTA, since these can likewise end up in ->acquire_dquot().
So my question is: would it be acceptable to also forbid these two commands on a frozen filesystem, i.e. add them to quotactl_cmd_write()?
My concern is the resulting behavior change for read-only mounts. quotactl_fd() would then call mnt_want_write() for these commands, which returns -EROFS on a read-only mount. So Q_XGETQUOTA on a read-only mounted filesystem (e.g. via xfs_quota) would change from working to failing with -EROFS.
Alternatively, I can keep them as read-only commands and only make both quotactl() and quotactl_fd() wait for the filesystem to thaw without taking write access, which keeps read-only mount queries working and only waits while the filesystem is frozen.
Which approach would you prefer?
Thanks,
Ye Bin