Skip to contentSkip to conversation

xfs: fix incorrect context handling in xfs_trans_roll

PATCH Superseded Wenwu Hou 3 participants 7 messages Jan 16 – Jan 20 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wenwu Hou

author
#0 lore raw

PATCH 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")
---
 fs/xfs/xfs_trans.c | 3 +--
 fs/xfs/xfs_trans.h | 9 ---------
 2 files changed, 1 insertion(+), 11 deletions(-)
fs/xfs/xfs_trans.c +1 -2
index 474f5a04ec63..d2ab296a52bc 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,7 @@ xfs_trans_roll(
 	 * locked be logged in the prior and the next transactions.
 	 */
 	tp = *tpp;
+	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.43.0
#1 lore raw
Quoted 51 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()

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")
---
 fs/xfs/xfs_trans.c | 3 +--
 fs/xfs/xfs_trans.h | 9 ---------
 2 files changed, 1 insertion(+), 11 deletions(-)
fs/xfs/xfs_trans.c +0 -2
index 474f5a04ec63..d2ab296a52bc 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);
 

fs/xfs/xfs_trans.c xfs_trans_roll( · line 1044

Quoted 4 lines
fs/xfs/xfs_trans.c +1 -0
@@ -1043,6 +1041,7 @@ xfs_trans_roll(
 	 * locked be logged in the prior and the next transactions.
 	 */
 	tp = *tpp;
+	xfs_trans_set_context(tp);

It took me a while to understand this, but it looks correct.

Can you add a comment here like:

/*
* __xfs_trans_commit cleared the NOFS flag by calling into
* xfs_trans_free. Set it again here before doing memory
* allocations.
*/

I also think we'd do better without the xfs_trans_*context helpers in their current form, but that's a separate issue I'll look into myself.

#2 lore raw
Quoted 67 lines · Christoph Hellwig
Quoted 51 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()

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")
---
 fs/xfs/xfs_trans.c | 3 +--
 fs/xfs/xfs_trans.h | 9 ---------
 2 files changed, 1 insertion(+), 11 deletions(-)
fs/xfs/xfs_trans.c +0 -2
index 474f5a04ec63..d2ab296a52bc 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);
 

fs/xfs/xfs_trans.c xfs_trans_roll( · line 1044

Quoted 4 lines
fs/xfs/xfs_trans.c +1 -0
@@ -1043,6 +1041,7 @@ xfs_trans_roll(
 	 * locked be logged in the prior and the next transactions.
 	 */
 	tp = *tpp;
+	xfs_trans_set_context(tp);

It took me a while to understand this, but it looks correct.

Can you add a comment here like:

/*
* __xfs_trans_commit cleared the NOFS flag by calling into
* xfs_trans_free. Set it again here before doing memory
* allocations.
*/

Quoted 3 lines

I also think we'd do better without the xfs_trans_*context helpers in their current form, but that's a separate issue I'll look into myself.

Those helpers were more useful to contain things back when they also set current->journal_info but now that's gone, I think it'd be clearer to opencode the memalloc_nofs transitions.

Also it might be a nice cleanup if we could avoid touching the PF_ flags at all, at least if the transaction can be rolled successfully.

--D

#3 lore raw
Quoted 2 lines · Darrick J. Wong

Also it might be a nice cleanup if we could avoid touching the PF_ flags at all, at least if the transaction can be rolled successfully.

That would mean passing a flag to not clear them into __xfs_trans_commit and xfs_trans_free. Which sounds pretty ugly to me, just to avoid to clear and reset a flag.

Quoted 1 line
#4 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Darrick J. Wong

Also it might be a nice cleanup if we could avoid touching the PF_ flags at all, at least if the transaction can be rolled successfully.

That would mean passing a flag to not clear them into __xfs_trans_commit and xfs_trans_free. Which sounds pretty ugly to me, just to avoid to clear and reset a flag.

I was more thinking that xfs_trans_alloc would set PF_MEMALLOC_NOFS, and that would be cleared at the end of xfs_trans_cancel or __xfs_trans_commit if !regrant.

--D

#5 lore raw
Quoted 10 lines · Darrick J. Wong
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Darrick J. Wong

Also it might be a nice cleanup if we could avoid touching the PF_ flags at all, at least if the transaction can be rolled successfully.

That would mean passing a flag to not clear them into __xfs_trans_commit and xfs_trans_free. Which sounds pretty ugly to me, just to avoid to clear and reset a flag.

I was more thinking that xfs_trans_alloc would set PF_MEMALLOC_NOFS, and

What about xfs_trans_alloc_empty?

Quoted 2 lines

that would be cleared at the end of xfs_trans_cancel or __xfs_trans_commit if !regrant.

I guess we could do that. Not sure it's any cleaner than the clear and reset approach, though.

author
#6 lore raw
In reply to Christoph Hellwig
Quoted 60 lines · Christoph Hellwig
Quoted 51 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()

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")
---
 fs/xfs/xfs_trans.c | 3 +--
 fs/xfs/xfs_trans.h | 9 ---------
 2 files changed, 1 insertion(+), 11 deletions(-)
fs/xfs/xfs_trans.c +0 -2
index 474f5a04ec63..d2ab296a52bc 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);
 

fs/xfs/xfs_trans.c xfs_trans_roll( · line 1044

Quoted 4 lines
fs/xfs/xfs_trans.c +1 -0
@@ -1043,6 +1041,7 @@ xfs_trans_roll(
       * locked be logged in the prior and the next transactions.
       */
      tp = *tpp;
+     xfs_trans_set_context(tp);

It took me a while to understand this, but it looks correct.

Quoted 7 lines

Can you add a comment here like:

/*
* __xfs_trans_commit cleared the NOFS flag by calling into
* xfs_trans_free. Set it again here before doing memory
* allocations.
*/

OK, thanks for your review. I will send v2 right now

Quoted 4 lines

I also think we'd do better without the xfs_trans_*context helpers in their current form, but that's a separate issue I'll look into myself.