diff mbox series

[v11,3/4] xfs: refactor the usage around xfs_trans_context_{set,clear}

Message ID 20201208122824.16118-4-laoar.shao@gmail.com (mailing list archive)
State New, archived
Headers show
Series xfs: avoid transaction reservation recursion | expand

Commit Message

Yafang Shao Dec. 8, 2020, 12:28 p.m. UTC
The xfs_trans context should be active after it is allocated, and
deactive when it is freed.

So these two helpers are refactored as,
- xfs_trans_context_set()
  Used in xfs_trans_alloc()
- xfs_trans_context_clear()
  Used in xfs_trans_free()

This patch is based on Darrick's work to fix the issue in xfs/141 in the
earlier version. [1]

1. https://lore.kernel.org/linux-xfs/20201104001649.GN7123@magnolia

Cc: Darrick J. Wong <darrick.wong@oracle.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Dave Chinner <david@fromorbit.com>
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 fs/xfs/xfs_trans.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

Comments

Darrick J. Wong Dec. 8, 2020, 6:59 p.m. UTC | #1
On Tue, Dec 08, 2020 at 08:28:23PM +0800, Yafang Shao wrote:
> The xfs_trans context should be active after it is allocated, and
> deactive when it is freed.
> 
> So these two helpers are refactored as,
> - xfs_trans_context_set()
>   Used in xfs_trans_alloc()
> - xfs_trans_context_clear()
>   Used in xfs_trans_free()
> 
> This patch is based on Darrick's work to fix the issue in xfs/141 in the
> earlier version. [1]
> 
> 1. https://lore.kernel.org/linux-xfs/20201104001649.GN7123@magnolia
> 
> Cc: Darrick J. Wong <darrick.wong@oracle.com>
> Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Dave Chinner <david@fromorbit.com>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
>  fs/xfs/xfs_trans.c | 20 +++++++-------------
>  1 file changed, 7 insertions(+), 13 deletions(-)
> 
> diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> index 11d390f0d3f2..fe20398a214e 100644
> --- a/fs/xfs/xfs_trans.c
> +++ b/fs/xfs/xfs_trans.c
> @@ -67,6 +67,9 @@ xfs_trans_free(
>  	xfs_extent_busy_sort(&tp->t_busy);
>  	xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
>  
> +	/* Detach the transaction from this thread. */
> +	xfs_trans_context_clear(tp);

Don't you need to check if tp is still the current transaction before
you clear PF_MEMALLOC_NOFS, now that the NOFS is bound to the lifespan
of the transaction itself instead of the reservation?

--D

> +
>  	trace_xfs_trans_free(tp, _RET_IP_);
>  	if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
>  		sb_end_intwrite(tp->t_mountp->m_super);
> @@ -153,9 +156,6 @@ xfs_trans_reserve(
>  	int			error = 0;
>  	bool			rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
>  
> -	/* Mark this thread as being in a transaction */
> -	xfs_trans_context_set(tp);
> -
>  	/*
>  	 * Attempt to reserve the needed disk blocks by decrementing
>  	 * the number needed from the number available.  This will
> @@ -163,10 +163,9 @@ xfs_trans_reserve(
>  	 */
>  	if (blocks > 0) {
>  		error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
> -		if (error != 0) {
> -			xfs_trans_context_clear(tp);
> +		if (error != 0)
>  			return -ENOSPC;
> -		}
> +
>  		tp->t_blk_res += blocks;
>  	}
>  
> @@ -241,8 +240,6 @@ xfs_trans_reserve(
>  		tp->t_blk_res = 0;
>  	}
>  
> -	xfs_trans_context_clear(tp);
> -
>  	return error;
>  }
>  
> @@ -284,6 +281,8 @@ xfs_trans_alloc(
>  	INIT_LIST_HEAD(&tp->t_dfops);
>  	tp->t_firstblock = NULLFSBLOCK;
>  
> +	/* Mark this thread as being in a transaction */
> +	xfs_trans_context_set(tp);
>  	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
>  	if (error) {
>  		xfs_trans_cancel(tp);
> @@ -878,7 +877,6 @@ __xfs_trans_commit(
>  
>  	xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
>  
> -	xfs_trans_context_clear(tp);
>  	xfs_trans_free(tp);
>  
>  	/*
> @@ -911,7 +909,6 @@ __xfs_trans_commit(
>  		tp->t_ticket = NULL;
>  	}
>  
> -	xfs_trans_context_clear(tp);
>  	xfs_trans_free_items(tp, !!error);
>  	xfs_trans_free(tp);
>  
> @@ -971,9 +968,6 @@ xfs_trans_cancel(
>  		tp->t_ticket = NULL;
>  	}
>  
> -	/* mark this thread as no longer being in a transaction */
> -	xfs_trans_context_clear(tp);
> -
>  	xfs_trans_free_items(tp, dirty);
>  	xfs_trans_free(tp);
>  }
> -- 
> 2.18.4
>
Yafang Shao Dec. 9, 2020, 1:47 a.m. UTC | #2
On Wed, Dec 9, 2020 at 2:59 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
>
> On Tue, Dec 08, 2020 at 08:28:23PM +0800, Yafang Shao wrote:
> > The xfs_trans context should be active after it is allocated, and
> > deactive when it is freed.
> >
> > So these two helpers are refactored as,
> > - xfs_trans_context_set()
> >   Used in xfs_trans_alloc()
> > - xfs_trans_context_clear()
> >   Used in xfs_trans_free()
> >
> > This patch is based on Darrick's work to fix the issue in xfs/141 in the
> > earlier version. [1]
> >
> > 1. https://lore.kernel.org/linux-xfs/20201104001649.GN7123@magnolia
> >
> > Cc: Darrick J. Wong <darrick.wong@oracle.com>
> > Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
> > Cc: Christoph Hellwig <hch@lst.de>
> > Cc: Dave Chinner <david@fromorbit.com>
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > ---
> >  fs/xfs/xfs_trans.c | 20 +++++++-------------
> >  1 file changed, 7 insertions(+), 13 deletions(-)
> >
> > diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> > index 11d390f0d3f2..fe20398a214e 100644
> > --- a/fs/xfs/xfs_trans.c
> > +++ b/fs/xfs/xfs_trans.c
> > @@ -67,6 +67,9 @@ xfs_trans_free(
> >       xfs_extent_busy_sort(&tp->t_busy);
> >       xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
> >
> > +     /* Detach the transaction from this thread. */
> > +     xfs_trans_context_clear(tp);
>
> Don't you need to check if tp is still the current transaction before
> you clear PF_MEMALLOC_NOFS, now that the NOFS is bound to the lifespan
> of the transaction itself instead of the reservation?
>

The current->journal_info is always the same with tp here in my verification.
I don't know in which case they are different. It would be better if
you could explain in detail.
Anyway I can add the check with your comment in the next version.


>
> > +
> >       trace_xfs_trans_free(tp, _RET_IP_);
> >       if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
> >               sb_end_intwrite(tp->t_mountp->m_super);
> > @@ -153,9 +156,6 @@ xfs_trans_reserve(
> >       int                     error = 0;
> >       bool                    rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
> >
> > -     /* Mark this thread as being in a transaction */
> > -     xfs_trans_context_set(tp);
> > -
> >       /*
> >        * Attempt to reserve the needed disk blocks by decrementing
> >        * the number needed from the number available.  This will
> > @@ -163,10 +163,9 @@ xfs_trans_reserve(
> >        */
> >       if (blocks > 0) {
> >               error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
> > -             if (error != 0) {
> > -                     xfs_trans_context_clear(tp);
> > +             if (error != 0)
> >                       return -ENOSPC;
> > -             }
> > +
> >               tp->t_blk_res += blocks;
> >       }
> >
> > @@ -241,8 +240,6 @@ xfs_trans_reserve(
> >               tp->t_blk_res = 0;
> >       }
> >
> > -     xfs_trans_context_clear(tp);
> > -
> >       return error;
> >  }
> >
> > @@ -284,6 +281,8 @@ xfs_trans_alloc(
> >       INIT_LIST_HEAD(&tp->t_dfops);
> >       tp->t_firstblock = NULLFSBLOCK;
> >
> > +     /* Mark this thread as being in a transaction */
> > +     xfs_trans_context_set(tp);
> >       error = xfs_trans_reserve(tp, resp, blocks, rtextents);
> >       if (error) {
> >               xfs_trans_cancel(tp);
> > @@ -878,7 +877,6 @@ __xfs_trans_commit(
> >
> >       xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
> >
> > -     xfs_trans_context_clear(tp);
> >       xfs_trans_free(tp);
> >
> >       /*
> > @@ -911,7 +909,6 @@ __xfs_trans_commit(
> >               tp->t_ticket = NULL;
> >       }
> >
> > -     xfs_trans_context_clear(tp);
> >       xfs_trans_free_items(tp, !!error);
> >       xfs_trans_free(tp);
> >
> > @@ -971,9 +968,6 @@ xfs_trans_cancel(
> >               tp->t_ticket = NULL;
> >       }
> >
> > -     /* mark this thread as no longer being in a transaction */
> > -     xfs_trans_context_clear(tp);
> > -
> >       xfs_trans_free_items(tp, dirty);
> >       xfs_trans_free(tp);
> >  }
> > --
> > 2.18.4
> >
Darrick J. Wong Dec. 9, 2020, 3:53 a.m. UTC | #3
On Wed, Dec 09, 2020 at 09:47:38AM +0800, Yafang Shao wrote:
> On Wed, Dec 9, 2020 at 2:59 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
> >
> > On Tue, Dec 08, 2020 at 08:28:23PM +0800, Yafang Shao wrote:
> > > The xfs_trans context should be active after it is allocated, and
> > > deactive when it is freed.
> > >
> > > So these two helpers are refactored as,
> > > - xfs_trans_context_set()
> > >   Used in xfs_trans_alloc()
> > > - xfs_trans_context_clear()
> > >   Used in xfs_trans_free()
> > >
> > > This patch is based on Darrick's work to fix the issue in xfs/141 in the
> > > earlier version. [1]
> > >
> > > 1. https://lore.kernel.org/linux-xfs/20201104001649.GN7123@magnolia
> > >
> > > Cc: Darrick J. Wong <darrick.wong@oracle.com>
> > > Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
> > > Cc: Christoph Hellwig <hch@lst.de>
> > > Cc: Dave Chinner <david@fromorbit.com>
> > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > > ---
> > >  fs/xfs/xfs_trans.c | 20 +++++++-------------
> > >  1 file changed, 7 insertions(+), 13 deletions(-)
> > >
> > > diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> > > index 11d390f0d3f2..fe20398a214e 100644
> > > --- a/fs/xfs/xfs_trans.c
> > > +++ b/fs/xfs/xfs_trans.c
> > > @@ -67,6 +67,9 @@ xfs_trans_free(
> > >       xfs_extent_busy_sort(&tp->t_busy);
> > >       xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
> > >
> > > +     /* Detach the transaction from this thread. */
> > > +     xfs_trans_context_clear(tp);
> >
> > Don't you need to check if tp is still the current transaction before
> > you clear PF_MEMALLOC_NOFS, now that the NOFS is bound to the lifespan
> > of the transaction itself instead of the reservation?
> >
> 
> The current->journal_info is always the same with tp here in my verification.
> I don't know in which case they are different.

I don't know why you changed it from the previous version.

> It would be better if you could explain in detail.  Anyway I can add
> the check with your comment in the next version.

xfs_trans_alloc is called to allocate a transaction.  We set _NOFS and
save the old flags (which don't contain _NOFS) to this transaction.

thread logs some changes and calls xfs_trans_roll.

xfs_trans_roll calls xfs_trans_dup to duplicate the old transaction.

xfs_trans_dup allocates a new transaction, which sets PF_MEMALLOC_NOFS
and saves the current context flags (in which _NOFS is set) in the new
transaction.

xfs_trans_roll then commits the old transaction

xfs_trans_commit frees the old transaction

xfs_trans_free restores the old context (which didn't have _NOFS) and
now we've dropped NOFS incorrectly

now we move on with the new transaction, but in the wrong NOFS mode.

note that this becomes a lot more obvious once you start fiddling with
current->journal_info in the last patch.

--D

> 
> >
> > > +
> > >       trace_xfs_trans_free(tp, _RET_IP_);
> > >       if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
> > >               sb_end_intwrite(tp->t_mountp->m_super);
> > > @@ -153,9 +156,6 @@ xfs_trans_reserve(
> > >       int                     error = 0;
> > >       bool                    rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
> > >
> > > -     /* Mark this thread as being in a transaction */
> > > -     xfs_trans_context_set(tp);
> > > -
> > >       /*
> > >        * Attempt to reserve the needed disk blocks by decrementing
> > >        * the number needed from the number available.  This will
> > > @@ -163,10 +163,9 @@ xfs_trans_reserve(
> > >        */
> > >       if (blocks > 0) {
> > >               error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
> > > -             if (error != 0) {
> > > -                     xfs_trans_context_clear(tp);
> > > +             if (error != 0)
> > >                       return -ENOSPC;
> > > -             }
> > > +
> > >               tp->t_blk_res += blocks;
> > >       }
> > >
> > > @@ -241,8 +240,6 @@ xfs_trans_reserve(
> > >               tp->t_blk_res = 0;
> > >       }
> > >
> > > -     xfs_trans_context_clear(tp);
> > > -
> > >       return error;
> > >  }
> > >
> > > @@ -284,6 +281,8 @@ xfs_trans_alloc(
> > >       INIT_LIST_HEAD(&tp->t_dfops);
> > >       tp->t_firstblock = NULLFSBLOCK;
> > >
> > > +     /* Mark this thread as being in a transaction */
> > > +     xfs_trans_context_set(tp);
> > >       error = xfs_trans_reserve(tp, resp, blocks, rtextents);
> > >       if (error) {
> > >               xfs_trans_cancel(tp);
> > > @@ -878,7 +877,6 @@ __xfs_trans_commit(
> > >
> > >       xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
> > >
> > > -     xfs_trans_context_clear(tp);
> > >       xfs_trans_free(tp);
> > >
> > >       /*
> > > @@ -911,7 +909,6 @@ __xfs_trans_commit(
> > >               tp->t_ticket = NULL;
> > >       }
> > >
> > > -     xfs_trans_context_clear(tp);
> > >       xfs_trans_free_items(tp, !!error);
> > >       xfs_trans_free(tp);
> > >
> > > @@ -971,9 +968,6 @@ xfs_trans_cancel(
> > >               tp->t_ticket = NULL;
> > >       }
> > >
> > > -     /* mark this thread as no longer being in a transaction */
> > > -     xfs_trans_context_clear(tp);
> > > -
> > >       xfs_trans_free_items(tp, dirty);
> > >       xfs_trans_free(tp);
> > >  }
> > > --
> > > 2.18.4
> > >
> 
> 
> 
> -- 
> Thanks
> Yafang
Yafang Shao Dec. 9, 2020, 10:43 a.m. UTC | #4
On Wed, Dec 9, 2020 at 11:53 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
>
> On Wed, Dec 09, 2020 at 09:47:38AM +0800, Yafang Shao wrote:
> > On Wed, Dec 9, 2020 at 2:59 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
> > >
> > > On Tue, Dec 08, 2020 at 08:28:23PM +0800, Yafang Shao wrote:
> > > > The xfs_trans context should be active after it is allocated, and
> > > > deactive when it is freed.
> > > >
> > > > So these two helpers are refactored as,
> > > > - xfs_trans_context_set()
> > > >   Used in xfs_trans_alloc()
> > > > - xfs_trans_context_clear()
> > > >   Used in xfs_trans_free()
> > > >
> > > > This patch is based on Darrick's work to fix the issue in xfs/141 in the
> > > > earlier version. [1]
> > > >
> > > > 1. https://lore.kernel.org/linux-xfs/20201104001649.GN7123@magnolia
> > > >
> > > > Cc: Darrick J. Wong <darrick.wong@oracle.com>
> > > > Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
> > > > Cc: Christoph Hellwig <hch@lst.de>
> > > > Cc: Dave Chinner <david@fromorbit.com>
> > > > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > > > ---
> > > >  fs/xfs/xfs_trans.c | 20 +++++++-------------
> > > >  1 file changed, 7 insertions(+), 13 deletions(-)
> > > >
> > > > diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> > > > index 11d390f0d3f2..fe20398a214e 100644
> > > > --- a/fs/xfs/xfs_trans.c
> > > > +++ b/fs/xfs/xfs_trans.c
> > > > @@ -67,6 +67,9 @@ xfs_trans_free(
> > > >       xfs_extent_busy_sort(&tp->t_busy);
> > > >       xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
> > > >
> > > > +     /* Detach the transaction from this thread. */
> > > > +     xfs_trans_context_clear(tp);
> > >
> > > Don't you need to check if tp is still the current transaction before
> > > you clear PF_MEMALLOC_NOFS, now that the NOFS is bound to the lifespan
> > > of the transaction itself instead of the reservation?
> > >
> >
> > The current->journal_info is always the same with tp here in my verification.
> > I don't know in which case they are different.
>
> I don't know why you changed it from the previous version.
>

I should explain it in the change log. Sorry about that.

> > It would be better if you could explain in detail.  Anyway I can add
> > the check with your comment in the next version.
>
> xfs_trans_alloc is called to allocate a transaction.  We set _NOFS and
> save the old flags (which don't contain _NOFS) to this transaction.
>
> thread logs some changes and calls xfs_trans_roll.
>
> xfs_trans_roll calls xfs_trans_dup to duplicate the old transaction.
>
> xfs_trans_dup allocates a new transaction, which sets PF_MEMALLOC_NOFS
> and saves the current context flags (in which _NOFS is set) in the new
> transaction.
>
> xfs_trans_roll then commits the old transaction
>
> xfs_trans_commit frees the old transaction
>
> xfs_trans_free restores the old context (which didn't have _NOFS) and
> now we've dropped NOFS incorrectly
>
> now we move on with the new transaction, but in the wrong NOFS mode.
>
> note that this becomes a lot more obvious once you start fiddling with
> current->journal_info in the last patch.
>

Many thanks for the detailed explanation. I missed the rolling transaction.
I will add this check in the next version.

> --D
>
> >
> > >
> > > > +
> > > >       trace_xfs_trans_free(tp, _RET_IP_);
> > > >       if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
> > > >               sb_end_intwrite(tp->t_mountp->m_super);
> > > > @@ -153,9 +156,6 @@ xfs_trans_reserve(
> > > >       int                     error = 0;
> > > >       bool                    rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
> > > >
> > > > -     /* Mark this thread as being in a transaction */
> > > > -     xfs_trans_context_set(tp);
> > > > -
> > > >       /*
> > > >        * Attempt to reserve the needed disk blocks by decrementing
> > > >        * the number needed from the number available.  This will
> > > > @@ -163,10 +163,9 @@ xfs_trans_reserve(
> > > >        */
> > > >       if (blocks > 0) {
> > > >               error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
> > > > -             if (error != 0) {
> > > > -                     xfs_trans_context_clear(tp);
> > > > +             if (error != 0)
> > > >                       return -ENOSPC;
> > > > -             }
> > > > +
> > > >               tp->t_blk_res += blocks;
> > > >       }
> > > >
> > > > @@ -241,8 +240,6 @@ xfs_trans_reserve(
> > > >               tp->t_blk_res = 0;
> > > >       }
> > > >
> > > > -     xfs_trans_context_clear(tp);
> > > > -
> > > >       return error;
> > > >  }
> > > >
> > > > @@ -284,6 +281,8 @@ xfs_trans_alloc(
> > > >       INIT_LIST_HEAD(&tp->t_dfops);
> > > >       tp->t_firstblock = NULLFSBLOCK;
> > > >
> > > > +     /* Mark this thread as being in a transaction */
> > > > +     xfs_trans_context_set(tp);
> > > >       error = xfs_trans_reserve(tp, resp, blocks, rtextents);
> > > >       if (error) {
> > > >               xfs_trans_cancel(tp);
> > > > @@ -878,7 +877,6 @@ __xfs_trans_commit(
> > > >
> > > >       xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
> > > >
> > > > -     xfs_trans_context_clear(tp);
> > > >       xfs_trans_free(tp);
> > > >
> > > >       /*
> > > > @@ -911,7 +909,6 @@ __xfs_trans_commit(
> > > >               tp->t_ticket = NULL;
> > > >       }
> > > >
> > > > -     xfs_trans_context_clear(tp);
> > > >       xfs_trans_free_items(tp, !!error);
> > > >       xfs_trans_free(tp);
> > > >
> > > > @@ -971,9 +968,6 @@ xfs_trans_cancel(
> > > >               tp->t_ticket = NULL;
> > > >       }
> > > >
> > > > -     /* mark this thread as no longer being in a transaction */
> > > > -     xfs_trans_context_clear(tp);
> > > > -
> > > >       xfs_trans_free_items(tp, dirty);
> > > >       xfs_trans_free(tp);
> > > >  }
> > > > --
> > > > 2.18.4
> > > >
> >
> >
> >
> > --
> > Thanks
> > Yafang
diff mbox series

Patch

diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
index 11d390f0d3f2..fe20398a214e 100644
--- a/fs/xfs/xfs_trans.c
+++ b/fs/xfs/xfs_trans.c
@@ -67,6 +67,9 @@  xfs_trans_free(
 	xfs_extent_busy_sort(&tp->t_busy);
 	xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
 
+	/* Detach the transaction from this thread. */
+	xfs_trans_context_clear(tp);
+
 	trace_xfs_trans_free(tp, _RET_IP_);
 	if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
 		sb_end_intwrite(tp->t_mountp->m_super);
@@ -153,9 +156,6 @@  xfs_trans_reserve(
 	int			error = 0;
 	bool			rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
 
-	/* Mark this thread as being in a transaction */
-	xfs_trans_context_set(tp);
-
 	/*
 	 * Attempt to reserve the needed disk blocks by decrementing
 	 * the number needed from the number available.  This will
@@ -163,10 +163,9 @@  xfs_trans_reserve(
 	 */
 	if (blocks > 0) {
 		error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
-		if (error != 0) {
-			xfs_trans_context_clear(tp);
+		if (error != 0)
 			return -ENOSPC;
-		}
+
 		tp->t_blk_res += blocks;
 	}
 
@@ -241,8 +240,6 @@  xfs_trans_reserve(
 		tp->t_blk_res = 0;
 	}
 
-	xfs_trans_context_clear(tp);
-
 	return error;
 }
 
@@ -284,6 +281,8 @@  xfs_trans_alloc(
 	INIT_LIST_HEAD(&tp->t_dfops);
 	tp->t_firstblock = NULLFSBLOCK;
 
+	/* Mark this thread as being in a transaction */
+	xfs_trans_context_set(tp);
 	error = xfs_trans_reserve(tp, resp, blocks, rtextents);
 	if (error) {
 		xfs_trans_cancel(tp);
@@ -878,7 +877,6 @@  __xfs_trans_commit(
 
 	xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
 
-	xfs_trans_context_clear(tp);
 	xfs_trans_free(tp);
 
 	/*
@@ -911,7 +909,6 @@  __xfs_trans_commit(
 		tp->t_ticket = NULL;
 	}
 
-	xfs_trans_context_clear(tp);
 	xfs_trans_free_items(tp, !!error);
 	xfs_trans_free(tp);
 
@@ -971,9 +968,6 @@  xfs_trans_cancel(
 		tp->t_ticket = NULL;
 	}
 
-	/* mark this thread as no longer being in a transaction */
-	xfs_trans_context_clear(tp);
-
 	xfs_trans_free_items(tp, dirty);
 	xfs_trans_free(tp);
 }