Skip to contentSkip to conversation

xfs: fix incorrect context handling in xfs_trans_roll

PATCH Applied Wenwu Hou 3 participants 3 messages Jan 17 – Jan 21 XFS filesystem

lore
Share & tools
lore

Wenwu Hou

author
#0 lore raw

PATCH v2 1/1 xfs: fix incorrect context handling in xfs_trans_roll

The memalloc_nofs_save() and memalloc_nofs_restore() calls are incorrectly paired in xfs_trans_roll.

Call path:
xfs_trans_alloc()

    __xfs_trans_alloc()
	// tp->t_pflags = memalloc_nofs_save();
	xfs_trans_set_context()
...
xfs_defer_trans_roll()
    xfs_trans_roll()
        xfs_trans_dup()
            // old_tp->t_pflags = 0;
            xfs_trans_switch_context()
        __xfs_trans_commit()
            xfs_trans_free()
                // memalloc_nofs_restore(tp->t_pflags);
                xfs_trans_clear_context()

The code passes 0 to memalloc_nofs_restore() when committing the original transaction, but memalloc_nofs_restore() should always receive the flags returned from the paired memalloc_nofs_save() call.

Before commit 3f6d5e6a468d ("mm: introduce memalloc_flags_{save,restore}"), calling memalloc_nofs_restore(0) would unset the PF_MEMALLOC_NOFS flag, which could cause memory allocation deadlocks[1].
Fortunately, after that commit, memalloc_nofs_restore(0) does nothing, so this issue is currently harmless.

Fixes 756b1c343333 ("xfs: use current->journal_info for detecting transaction recursion")

---
v2: add comment from Christoph Hellwig

 fs/xfs/xfs_trans.c | 8 ++++++--
 fs/xfs/xfs_trans.h | 9 ---------
 2 files changed, 6 insertions(+), 11 deletions(-)
fs/xfs/xfs_trans.c +6 -2
index 474f5a04ec63..dbe92bf9f031 100644
@@ -124,8 +124,6 @@ xfs_trans_dup(
 	ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
 	tp->t_rtx_res = tp->t_rtx_res_used;
 
-	xfs_trans_switch_context(tp, ntp);
-
 	/* move deferred ops over to the new tp */
 	xfs_defer_move(ntp, tp);
 
@@ -1043,6 +1041,12 @@ 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 +0 -9
index 7fb860f645a3..f7fcb95afe41 100644
@@ -278,13 +278,4 @@ xfs_trans_clear_context(
 	memalloc_nofs_restore(tp->t_pflags);
 }
 
-static inline void
-xfs_trans_switch_context(
-	struct xfs_trans	*old_tp,
-	struct xfs_trans	*new_tp)
-{
-	new_tp->t_pflags = old_tp->t_pflags;
-	old_tp->t_pflags = 0;
-}
-
 #endif	/* __XFS_TRANS_H__ */
Signature · 1 line
2.39.5
maintainer Applied
#2 lore raw
In reply to Wenwu Hou
Quoted 19 lines · Wenwu Hou

The memalloc_nofs_save() and memalloc_nofs_restore() calls are incorrectly paired in xfs_trans_roll.

Call path:
xfs_trans_alloc()

    __xfs_trans_alloc()
	// tp->t_pflags = memalloc_nofs_save();
	xfs_trans_set_context()
...
xfs_defer_trans_roll()
    xfs_trans_roll()
        xfs_trans_dup()
            // old_tp->t_pflags = 0;
            xfs_trans_switch_context()
        __xfs_trans_commit()
            xfs_trans_free()
                // memalloc_nofs_restore(tp->t_pflags);
                xfs_trans_clear_context()
Quoted 1 line

[...]

Applied to for-next, thanks!

[1/1] xfs: fix incorrect context handling in xfs_trans_roll

      commit: a1ca658d649a4d8972e2e21ac2625b633217e327

Best regards,

Signature · 1 line
Carlos Maiolino <[email protected]>