xfs: restore nofs context unconditionally in xfs_trans_roll
Subsystems (2)
v2 cover xfs: restore nofs context unconditionally in xfs_trans_roll
xfs_trans_roll() loses the NOFS allocation context on the error path, causing a circular lock dependency between xfs_nondir_ilock_class and fs_reclaim reported by syzbot. Fix this by transferring the nofs context from the old transaction to the new one in xfs_trans_dup(), so it remains active regardless of commit success or failure.
v2:
- Instead of moving xfs_trans_set_context() before the error check, transfer the nofs context in xfs_trans_dup() via a new memalloc_flags_move() helper, as suggested by Darrick.
- Change t_pflags from unsigned long to unsigned int to match mm API.
v1: https://lore.kernel.org/all/[email protected]/T/
Yun Zhou (2):
mm: introduce memalloc_flags_move() for transferring allocation scopes
xfs: restore nofs context unconditionally in xfs_trans_roll
fs/xfs/xfs_trans.c | 9 +++------ fs/xfs/xfs_trans.h | 2 +- include/linux/sched/mm.h | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 mm: introduce memalloc_flags_move() for transferring allocation scopes
Add memalloc_flags_move() to transfer a saved memalloc scope from one tracking variable to another. The source is zeroed so that a subsequent memalloc_flags_restore() on it becomes a no-op, effectively transferring ownership of the scope to the destination.
This is needed when a subsystem hands off an allocation context from one structure to another (e.g. during transaction rolling in filesystems), and needs to ensure the scope remains active without an extra save/restore cycle.
--- include/linux/sched/mm.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
include/linux/sched/mm.h +17 -0
@@ -342,6 +342,23 @@ static inline void memalloc_flags_restore(unsigned flags) current->flags &= ~flags; } +/** +* memalloc_flags_move - transfer a memalloc scope from one tracking +* variable to another. +* @old_flags: pointer to the source flags (will be zeroed) +* +* Returns the flags value to store in the destination. The source is +* set to zero so that a subsequent memalloc_flags_restore() on it is +* a no-op. +*/ +static inline unsigned int memalloc_flags_move (unsigned int *old_flags) +{ +unsigned int ret = *old_flags; + + *old_flags =0 ; +return ret; +} +/** * memalloc_noio_save - Marks implicit GFP_NOIO allocation scope. *
Signature · 1 line
2.43.0
Andrew Morton
maintainerRe: [PATCH v2 1/2] mm: introduce memalloc_flags_move() for transferring allocation scopes
Quoted 5 lines · On Sun, 19 Jul 2026 17:57:31 +0800 Yun Zhou <[email protected]> wrote:
Add
memalloc_flags_move()to transfer a saved memalloc scope from one tracking variable to another. The source is zeroed so that a subsequentmemalloc_flags_restore()on it becomes a no-op, effectively transferring ownership of the scope to the destination.
Quoted 7 lines
This is needed when a subsystem hands off an allocation context from one structure to another (e.g. during transaction rolling in filesystems), and needs to ensure the scope remains active without an extra save/restore cycle.
...
Thanks. fyi, Sashiko might have found a pre-existing xfs issue:
https://sashiko.dev/#/patchset/[email protected]
Quoted 2 lines
include/linux/sched/mm.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
This file isn't mentioned in MAINTAINERS. Could someone please propose a patch?
From the MM side, probably just place it in every record which mentions include/linux/mm.h - close enough.
Quoted 13 lines
include/linux/sched/mm.h +7
-0@@ -342,6 +342,23 @@ static inline void memalloc_flags_restore(unsigned flags) current->flags &= ~flags; } +/** +* memalloc_flags_move - transfer a memalloc scope from one tracking +* variable to another. +* @old_flags: pointer to the source flags (will be zeroed) +* +* Returns the flags value to store in the destination. The source is +* set to zero so that a subsequent memalloc_flags_restore() on it is
include/linux/sched/mm.h static inline void memalloc_flags_restore(unsigned flags) · line 361
Quoted 10 lines
include/linux/sched/mm.h +10
-0+* a no-op. +*/ +static inline unsigned int memalloc_flags_move (unsigned int *old_flags) +{ +unsigned int ret = *old_flags; + + *old_flags =0 ; +return ret; +} +
I've added linux-mm to cc here. Please include it in any future versions of this patchset.
Re: [PATCH v2 1/2] mm: introduce memalloc_flags_move() for transferring allocation scopes
Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Sun, 19 Jul 2026 17:57:31 +0800 Yun Zhou <[email protected]> wrote:
Add
memalloc_flags_move()to transfer a saved memalloc scope from one tracking variable to another. The source is zeroed so that a subsequentmemalloc_flags_restore()on it becomes a no-op, effectively transferring ownership of the scope to the destination.This is needed when a subsystem hands off an allocation context from one structure to another (e.g. during transaction rolling in filesystems), and needs to ensure the scope remains active without an extra save/restore cycle.
Quoted 6 lines
Quoted 2 lines
...
Thanks. fyi, Sashiko might have found a pre-existing xfs issue:
https://sashiko.dev/#/patchset/[email protected]
Good catch. This is indeed a pre-existing issue, and needs a separate fix.
Quoted 27 lines
Quoted 2 lines
include/linux/sched/mm.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)This file isn't mentioned in
MAINTAINERS. Could someone please propose a patch?From the MM side, probably just place it in every record which mentions
include/linux/mm.h- close enough.Quoted 18 lines
include/linux/sched/mm.h +12
-0@@ -342,6 +342,23 @@ static inline void memalloc_flags_restore(unsigned flags) current->flags &= ~flags; } +/** +* memalloc_flags_move - transfer a memalloc scope from one tracking +* variable to another. +* @old_flags: pointer to the source flags (will be zeroed) +* +* Returns the flags value to store in the destination. The source is +* set to zero so that a subsequent memalloc_flags_restore() on it is +* a no-op. +*/ +static inline unsigned int memalloc_flags_move (unsigned int *old_flags) +{ +unsigned int ret = *old_flags;
Quoted 9 lines
include/linux/sched/mm.h static inline void memalloc_flags_restore(unsigned flags) · line 361
Quoted 5 lines
include/linux/sched/mm.h +5
-0+ + *old_flags =0 ; +return ret; +} +I've added linux-mm to cc here. Please include it in any future versions of this patchset.
Well, got it.
Thanks,
Yun
PATCH v2 2/2 xfs: restore nofs context unconditionally in xfs_trans_roll
In reply to Yun ZhouWhen __xfs_trans_commit() fails in xfs_trans_roll(), the NOFS context is cleared but only restored in the success path. This leaves the error path without nofs protection, causing a circular lock dependency between xfs_nondir_ilock_class and fs_reclaim:
CPU0 CPU1
---- ----
lock(&xfs_nondir_ilock_class);
lock(fs_reclaim);
lock(&xfs_nondir_ilock_class); lock(fs_reclaim);
Fix this by transferring the nofs context from the old transaction to the new one in xfs_trans_dup() via memalloc_flags_move(), so it remains active throughout the entire roll sequence regardless of commit success or failure.
--- fs/xfs/xfs_trans.c | 9 +++------ fs/xfs/xfs_trans.h | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-)
fs/xfs/xfs_trans.c +3 -6
@@ -127,6 +127,9 @@ xfs_trans_dup(/* move deferred ops over to the new tp */ xfs_defer_move (ntp, tp); +/* move the nofs context to the new transaction */ + ntp->t_pflags =memalloc_flags_move (&tp->t_pflags); +xfs_trans_dup_dqinfo (tp, ntp);return ntp; }
@@ -1041,12 +1044,6 @@ xfs_trans_roll(* locked be logged in the prior and the next transactions. */ tp = *tpp; -/* -* __xfs_trans_commit cleared the NOFS flag by calling into -* xfs_trans_free. Set it again here before doing memory -* allocations. -*/ -xfs_trans_set_context (tp); error =xfs_log_regrant (tp->t_mountp, tp->t_ticket);if (error)return error;
fs/xfs/xfs_trans.h +1 -1
@@ -152,7 +152,7 @@ typedef struct xfs_trans {struct list_head t_items;/* log item descriptors */ struct list_head t_busy;/* list of busy extents */ struct list_head t_dfops;/* deferred operations */ -unsigned long t_pflags;/* saved process flags state */ +unsigned int t_pflags;/* saved process flags state */ } xfs_trans_t;/*
Signature · 1 line
2.43.0
Quoted 10 lines · Yun Zhou
When
__xfs_trans_commit()fails inxfs_trans_roll(), the NOFS context is cleared but only restored in the success path. This leaves the error path without nofs protection, causing a circular lock dependency betweenxfs_nondir_ilock_classandfs_reclaim:CPU0 CPU1 ---- ---- lock(&xfs_nondir_ilock_class); lock(fs_reclaim); lock(&xfs_nondir_ilock_class);
Quoted 6 lines
lock(fs_reclaim);Fix this by transferring the nofs context from the old transaction to the new one in
xfs_trans_dup()viamemalloc_flags_move(), so it remains active throughout the entire roll sequence regardless of commit success or failure.
Good find, but we keep adding more magic here that will just lead to further bugs. Your previous version was much better, and could be improved by also removing the hiding under xfs_trans_set_context as a follow on.
Having a maze of single or two caller magic macros that modify global (or rather global-ish as it's per thread) state makes hairy bits like this really error prone as seen by the bugs now found.
Hi all,
Quoted 18 lines · Christoph Hellwig
Quoted 10 lines · Yun Zhou
When
__xfs_trans_commit()fails inxfs_trans_roll(), the NOFS context is cleared but only restored in the success path. This leaves the error path without nofs protection, causing a circular lock dependency betweenxfs_nondir_ilock_classandfs_reclaim:CPU0 CPU1 ---- ---- lock(&xfs_nondir_ilock_class); lock(fs_reclaim); lock(&xfs_nondir_ilock_class);Quoted 6 lines
lock(fs_reclaim);Fix this by transferring the nofs context from the old transaction to the new one in
xfs_trans_dup()viamemalloc_flags_move(), so it remains active throughout the entire roll sequence regardless of commit success or failure.
Quoted 8 lines
Good find, but we keep adding more magic here that will just lead to further bugs. Your previous version was much better, and could be improved by also removing the hiding under
xfs_trans_set_contextas a follow on.Having a maze of single or two caller magic macros that modify global (or rather global-ish as it's per thread) state makes hairy bits like this really error prone as seen by the bugs now found.
It's been a few days since the last discussion. I'm planning to respin as v3 following Christoph's suggestion (drop the helper, re-save directly in xfs_trans_roll). Any other suggestions?
Thanks,
Yun
Quoted 7 lines · Yun
Quoted 3 lines
Having a maze of single or two caller magic macros that modify global (or rather global-ish as it's per thread) state makes hairy bits like this really error prone as seen by the bugs now found.
It's been a few days since the last discussion. I'm planning to respin as v3 following Christoph's suggestion (drop the helper, re-save directly in
xfs_trans_roll). Any other suggestions?
Sounds good to me.