diff mbox series

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

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

Commit Message

Yafang Shao Dec. 9, 2020, 1:11 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 | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

Comments

Darrick J. Wong Dec. 9, 2020, 7:52 p.m. UTC | #1
On Wed, Dec 09, 2020 at 09:11:45PM +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 | 28 +++++++++++++++-------------
>  1 file changed, 15 insertions(+), 13 deletions(-)
> 
> diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> index 11d390f0d3f2..4f4645329bb2 100644
> --- a/fs/xfs/xfs_trans.c
> +++ b/fs/xfs/xfs_trans.c
> @@ -67,6 +67,17 @@ 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. */
> +	ASSERT(current->journal_info != NULL);
> +	/*
> +	 * The PF_MEMALLOC_NOFS is bound to the transaction itself instead
> +	 * of the reservation, so we need to check if tp is still the
> +	 * current transaction before clearing the flag.
> +	 */
> +	if (current->journal_info == tp)

Um, you don't start setting journal_info until the next patch, so this
means that someone who lands on this commit with git bisect will have a
xfs with broken logic.

Because this is the patch that changes where we set and restore NOFS
context, I think you have to introduce xfs_trans_context_swap here,
and not in the next patch.

I also think the _swap routine has to move the old NOFS state to the
new transaction's t_pflags, and then set NOFS in the old transaction's
t_pflags so that when we clear the context on the old transaction we
don't actually change the thread's NOFS state.

--D

> +		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 +164,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 +171,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 +248,6 @@ xfs_trans_reserve(
>  		tp->t_blk_res = 0;
>  	}
>  
> -	xfs_trans_context_clear(tp);
> -
>  	return error;
>  }
>  
> @@ -284,6 +289,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 +885,6 @@ __xfs_trans_commit(
>  
>  	xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
>  
> -	xfs_trans_context_clear(tp);
>  	xfs_trans_free(tp);
>  
>  	/*
> @@ -911,7 +917,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 +976,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. 13, 2020, 9:09 a.m. UTC | #2
On Thu, Dec 10, 2020 at 3:52 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
>
> On Wed, Dec 09, 2020 at 09:11:45PM +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 | 28 +++++++++++++++-------------
> >  1 file changed, 15 insertions(+), 13 deletions(-)
> >
> > diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> > index 11d390f0d3f2..4f4645329bb2 100644
> > --- a/fs/xfs/xfs_trans.c
> > +++ b/fs/xfs/xfs_trans.c
> > @@ -67,6 +67,17 @@ 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. */
> > +     ASSERT(current->journal_info != NULL);
> > +     /*
> > +      * The PF_MEMALLOC_NOFS is bound to the transaction itself instead
> > +      * of the reservation, so we need to check if tp is still the
> > +      * current transaction before clearing the flag.
> > +      */
> > +     if (current->journal_info == tp)
>
> Um, you don't start setting journal_info until the next patch, so this
> means that someone who lands on this commit with git bisect will have a
> xfs with broken logic.
>
> Because this is the patch that changes where we set and restore NOFS
> context, I think you have to introduce xfs_trans_context_swap here,
> and not in the next patch.
>

Thanks for the review. I will change it in the next version.

> I also think the _swap routine has to move the old NOFS state to the
> new transaction's t_pflags,

Sure

> and then set NOFS in the old transaction's
> t_pflags so that when we clear the context on the old transaction we
> don't actually change the thread's NOFS state.
>

Both thread's NOFS state and thead's journal_info state can't be
changed in that case, right ?
So should it better be,

    __xfs_trans_commit(tp, regrant)
        xfs_trans_free(tp, regrant)
            if (!regrant). // don't clear the xfs_trans_context if
regrant is true.
                xfs_trans_context_clear()



> --D
>
> > +             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 +164,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 +171,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 +248,6 @@ xfs_trans_reserve(
> >               tp->t_blk_res = 0;
> >       }
> >
> > -     xfs_trans_context_clear(tp);
> > -
> >       return error;
> >  }
> >
> > @@ -284,6 +289,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 +885,6 @@ __xfs_trans_commit(
> >
> >       xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
> >
> > -     xfs_trans_context_clear(tp);
> >       xfs_trans_free(tp);
> >
> >       /*
> > @@ -911,7 +917,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 +976,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
> >
Dave Chinner Dec. 14, 2020, 9:08 p.m. UTC | #3
On Sun, Dec 13, 2020 at 05:09:02PM +0800, Yafang Shao wrote:
> On Thu, Dec 10, 2020 at 3:52 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
> >
> > On Wed, Dec 09, 2020 at 09:11:45PM +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 | 28 +++++++++++++++-------------
> > >  1 file changed, 15 insertions(+), 13 deletions(-)
> > >
> > > diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> > > index 11d390f0d3f2..4f4645329bb2 100644
> > > --- a/fs/xfs/xfs_trans.c
> > > +++ b/fs/xfs/xfs_trans.c
> > > @@ -67,6 +67,17 @@ 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. */
> > > +     ASSERT(current->journal_info != NULL);
> > > +     /*
> > > +      * The PF_MEMALLOC_NOFS is bound to the transaction itself instead
> > > +      * of the reservation, so we need to check if tp is still the
> > > +      * current transaction before clearing the flag.
> > > +      */
> > > +     if (current->journal_info == tp)
> >
> > Um, you don't start setting journal_info until the next patch, so this
> > means that someone who lands on this commit with git bisect will have a
> > xfs with broken logic.
> >
> > Because this is the patch that changes where we set and restore NOFS
> > context, I think you have to introduce xfs_trans_context_swap here,
> > and not in the next patch.
> >
> 
> Thanks for the review. I will change it in the next version.
> 
> > I also think the _swap routine has to move the old NOFS state to the
> > new transaction's t_pflags,
> 
> Sure
> 
> > and then set NOFS in the old transaction's
> > t_pflags so that when we clear the context on the old transaction we
> > don't actually change the thread's NOFS state.
> >
> 
> Both thread's NOFS state and thead's journal_info state can't be
> changed in that case, right ?
> So should it better be,
> 
>     __xfs_trans_commit(tp, regrant)
>         xfs_trans_free(tp, regrant)
>             if (!regrant). // don't clear the xfs_trans_context if
> regrant is true.
>                 xfs_trans_context_clear()

No. You are trying to make this way more complex than it needs to be.
The logic in the core XFS code is *already correct* and all we need
to do is move that logic to wrapper functions, then slightly modify
the implementation inside the wrapper functions.

That is, xfs_trans_context_clear() should end up like this:

static inline void
xfs_trans_context_clear(struct xfs_trans *tp)
{
	/*
	 * If xfs_trans_context_swap() handed the NOFS context to a
	 * new transaction we do not clear the context here.
	 */
	if (current->journal_info != tp)
		return;
	current->journal_info = NULL;
	memalloc_nofs_restore(tp->t_pflags);
}

-Dave.
Yafang Shao Dec. 15, 2020, 12:42 a.m. UTC | #4
On Tue, Dec 15, 2020 at 5:08 AM Dave Chinner <david@fromorbit.com> wrote:
>
> On Sun, Dec 13, 2020 at 05:09:02PM +0800, Yafang Shao wrote:
> > On Thu, Dec 10, 2020 at 3:52 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
> > >
> > > On Wed, Dec 09, 2020 at 09:11:45PM +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 | 28 +++++++++++++++-------------
> > > >  1 file changed, 15 insertions(+), 13 deletions(-)
> > > >
> > > > diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
> > > > index 11d390f0d3f2..4f4645329bb2 100644
> > > > --- a/fs/xfs/xfs_trans.c
> > > > +++ b/fs/xfs/xfs_trans.c
> > > > @@ -67,6 +67,17 @@ 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. */
> > > > +     ASSERT(current->journal_info != NULL);
> > > > +     /*
> > > > +      * The PF_MEMALLOC_NOFS is bound to the transaction itself instead
> > > > +      * of the reservation, so we need to check if tp is still the
> > > > +      * current transaction before clearing the flag.
> > > > +      */
> > > > +     if (current->journal_info == tp)
> > >
> > > Um, you don't start setting journal_info until the next patch, so this
> > > means that someone who lands on this commit with git bisect will have a
> > > xfs with broken logic.
> > >
> > > Because this is the patch that changes where we set and restore NOFS
> > > context, I think you have to introduce xfs_trans_context_swap here,
> > > and not in the next patch.
> > >
> >
> > Thanks for the review. I will change it in the next version.
> >
> > > I also think the _swap routine has to move the old NOFS state to the
> > > new transaction's t_pflags,
> >
> > Sure
> >
> > > and then set NOFS in the old transaction's
> > > t_pflags so that when we clear the context on the old transaction we
> > > don't actually change the thread's NOFS state.
> > >
> >
> > Both thread's NOFS state and thead's journal_info state can't be
> > changed in that case, right ?
> > So should it better be,
> >
> >     __xfs_trans_commit(tp, regrant)
> >         xfs_trans_free(tp, regrant)
> >             if (!regrant). // don't clear the xfs_trans_context if
> > regrant is true.
> >                 xfs_trans_context_clear()
>
> No. You are trying to make this way more complex than it needs to be.
> The logic in the core XFS code is *already correct* and all we need
> to do is move that logic to wrapper functions, then slightly modify
> the implementation inside the wrapper functions.
>

Thanks for the explanation.

>
> That is, xfs_trans_context_clear() should end up like this:
>

Agreed.

> static inline void
> xfs_trans_context_clear(struct xfs_trans *tp)
> {
>         /*
>          * If xfs_trans_context_swap() handed the NOFS context to a
>          * new transaction we do not clear the context here.
>          */
>         if (current->journal_info != tp)

current->journal_info hasn't been used in patch #3, that will make
patch #3 a little more complex.
We have to do some workaround in patch #3. I will think about it.

>                 return;
>         current->journal_info = NULL;
>         memalloc_nofs_restore(tp->t_pflags);
> }
>
> -Dave.
> --
> Dave Chinner
> david@fromorbit.com
Dave Chinner Dec. 15, 2020, 1:12 a.m. UTC | #5
On Tue, Dec 15, 2020 at 08:42:08AM +0800, Yafang Shao wrote:
> On Tue, Dec 15, 2020 at 5:08 AM Dave Chinner <david@fromorbit.com> wrote:
> > On Sun, Dec 13, 2020 at 05:09:02PM +0800, Yafang Shao wrote:
> > > On Thu, Dec 10, 2020 at 3:52 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
> > > > On Wed, Dec 09, 2020 at 09:11:45PM +0800, Yafang Shao wrote:
> > static inline void
> > xfs_trans_context_clear(struct xfs_trans *tp)
> > {
> >         /*
> >          * If xfs_trans_context_swap() handed the NOFS context to a
> >          * new transaction we do not clear the context here.
> >          */
> >         if (current->journal_info != tp)
> 
> current->journal_info hasn't been used in patch #3, that will make
> patch #3 a little more complex.
> We have to do some workaround in patch #3. I will think about it.

What I wrote is how the function should look at the end of the patch
series.  Do not add the current->journal_info parts of it until the
patch that introduces the current->journal_info tracking.

-Dave.
Yafang Shao Dec. 15, 2020, 4:39 a.m. UTC | #6
On Tue, Dec 15, 2020 at 9:12 AM Dave Chinner <david@fromorbit.com> wrote:
>
> On Tue, Dec 15, 2020 at 08:42:08AM +0800, Yafang Shao wrote:
> > On Tue, Dec 15, 2020 at 5:08 AM Dave Chinner <david@fromorbit.com> wrote:
> > > On Sun, Dec 13, 2020 at 05:09:02PM +0800, Yafang Shao wrote:
> > > > On Thu, Dec 10, 2020 at 3:52 AM Darrick J. Wong <darrick.wong@oracle.com> wrote:
> > > > > On Wed, Dec 09, 2020 at 09:11:45PM +0800, Yafang Shao wrote:
> > > static inline void
> > > xfs_trans_context_clear(struct xfs_trans *tp)
> > > {
> > >         /*
> > >          * If xfs_trans_context_swap() handed the NOFS context to a
> > >          * new transaction we do not clear the context here.
> > >          */
> > >         if (current->journal_info != tp)
> >
> > current->journal_info hasn't been used in patch #3, that will make
> > patch #3 a little more complex.
> > We have to do some workaround in patch #3. I will think about it.
>
> What I wrote is how the function should look at the end of the patch
> series.  Do not add the current->journal_info parts of it until the
> patch that introduces the current->journal_info tracking.
>

I know what you meant.
While I mean we have to do some hack, as suggested by Darrrick that
"set NOFS in the old transaction's
t_pflags so that when we clear the context on the old transaction we
don't actually change the thread's NOFS state." in patch #3 and then
remove it in patch #4.
diff mbox series

Patch

diff --git a/fs/xfs/xfs_trans.c b/fs/xfs/xfs_trans.c
index 11d390f0d3f2..4f4645329bb2 100644
--- a/fs/xfs/xfs_trans.c
+++ b/fs/xfs/xfs_trans.c
@@ -67,6 +67,17 @@  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. */
+	ASSERT(current->journal_info != NULL);
+	/*
+	 * The PF_MEMALLOC_NOFS is bound to the transaction itself instead
+	 * of the reservation, so we need to check if tp is still the
+	 * current transaction before clearing the flag.
+	 */
+	if (current->journal_info == tp)
+		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 +164,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 +171,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 +248,6 @@  xfs_trans_reserve(
 		tp->t_blk_res = 0;
 	}
 
-	xfs_trans_context_clear(tp);
-
 	return error;
 }
 
@@ -284,6 +289,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 +885,6 @@  __xfs_trans_commit(
 
 	xfs_log_commit_cil(mp, tp, &commit_lsn, regrant);
 
-	xfs_trans_context_clear(tp);
 	xfs_trans_free(tp);
 
 	/*
@@ -911,7 +917,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 +976,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);
 }