Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Brian Foster <bfoster@redhat.com>
To: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: brauner@kernel.org, djwong@kernel.org, cem@kernel.org,
	akpm@linux-foundation.org, vbabka@kernel.org, surenb@google.com,
	mhocko@suse.com, brendan.jackman@linux.dev, hannes@cmpxchg.org,
	ziy@nvidia.com, david@kernel.org, qi.zheng@linux.dev,
	shakeel.butt@linux.dev, ljs@kernel.org,
	linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-mm@kvack.org
Subject: Re: [PATCH] xfs: fix NOFS state corruption in btree split worker
Date: Fri, 4 Sep 2026 07:57:15 -0400	[thread overview]
Message-ID: <apqyG_d6jEUqcMN-@bfoster> (raw)
In-Reply-To: <62729d59-198d-4ed2-b1e1-b18384e73b98@huawei.com>

On Fri, Sep 04, 2026 at 08:41:26AM +0800, Kefeng Wang wrote:
> 
> 
> On 9/3/2026 9:52 PM, Brian Foster wrote:
> > On Thu, Sep 03, 2026 at 09:37:56PM +0800, Kefeng Wang wrote:
> > > xfs_btree_split_worker() calls xfs_trans_set_context() and
> > > xfs_trans_clear_context() on the caller's transaction, overwriting
> > > tp->t_pflags with the worker's NOFS state. When the caller already has
> > > PF_MEMALLOC_NOFS set (e.g. xfs_end_ioend_write, xfs_dio_write_end_io),
> > > the corrupted tp->t_pflags causes xfs_trans_free() to erroneously clear
> > > the caller's NOFS protection.
> > > 
> > > Use memalloc_nofs_save/restore with a local variable instead so
> > > tp->t_pflags is never touched.
> > > 
> > > Closes: https://sashiko.dev/#/patchset/20260902131653.1338227-1-wangkefeng.wang@huawei.com
> > > Fixes: 756b1c343333 ("xfs: use current->journal_info for detecting transaction recursion")
> > > Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
> > > ---
> > 
> > I agree that the Sashiko analysis looks correct. The only thing I wonder
> > is whether it might be a bit cleaner to have the set_context() helper
> > return the context instead of hardcode the assignment to ->t_pflags so
> > it can be used in both places. The reasoning is just that the current
> > arrangement kind of makes it easy to repeat this mistake in the future.
> > 
> > Then again, it's a single line helper so maybe another option could be
> > to just remove and open code it. I suppose the pro of keeping the helper
> > is that it's a decent spot to document the concern and why it returns a
> > value, etc. *shrug* Thoughts?
> 
> 
> I personally tend to remove the helper functions, but let's see what others
> think.
> 

I noticed after writing this that there were many more open coded nofs
calls than I originally thought, so this is a fair point. After some
thought I think this is a reasonable approach regardless:

Reviewed-by: Brian Foster <bfoster@redhat.com>

You might want to send as a standalone patch though..

Brian

> > 
> > (Please don't change this patch just on my comments alone. Let's see if
> > others have input first..).
> > 
> > Brian
> > 
> > >   fs/xfs/libxfs/xfs_btree.c | 10 ++++++++--
> > >   1 file changed, 8 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/fs/xfs/libxfs/xfs_btree.c b/fs/xfs/libxfs/xfs_btree.c
> > > index 6738d9d1511b..8ae4b94e6995 100644
> > > --- a/fs/xfs/libxfs/xfs_btree.c
> > > +++ b/fs/xfs/libxfs/xfs_btree.c
> > > @@ -3007,12 +3007,18 @@ xfs_btree_split_worker(
> > >   {
> > >   	struct xfs_btree_split_args	*args = container_of(work,
> > >   						struct xfs_btree_split_args, work);
> > > -	xfs_trans_set_context(args->cur->bc_tp);
> > > +	unsigned int			nofs_flags;
> > > +
> > > +	/*
> > > +	 * Don't use xfs_trans_set_context() here: it would overwrite the
> > > +	 * caller's saved NOFS state in tp->t_pflags.  Use a local scope.
> > > +	 */
> > > +	nofs_flags = memalloc_nofs_save();
> > >   	args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
> > >   					 args->key, args->curp, args->stat);
> > > -	xfs_trans_clear_context(args->cur->bc_tp);
> > > +	memalloc_nofs_restore(nofs_flags);
> > >   	/*
> > >   	 * Do not access args after complete() has run here. We don't own args
> > > -- 
> > > 2.55.0
> > > 
> > > 
> > 
> > 
> 



  reply	other threads:[~2026-09-04 11:57 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-02 13:16 [PATCH 0/4] mm: replace PF_KCOMPACTD/PF_KSWAPD with kthread_func() Kefeng Wang
2026-09-02 13:16 ` [PATCH 1/4] xfs: remove dead kswapd flag inheritance from btree split worker Kefeng Wang
2026-09-02 14:32   ` Christoph Hellwig
2026-09-02 15:33   ` Shakeel Butt
2026-09-02 13:16 ` [PATCH 2/4] iomap: simplify writepages reclaim guard Kefeng Wang
2026-09-02 14:32   ` Christoph Hellwig
2026-09-03 13:46     ` Kefeng Wang
2026-09-02 15:34   ` Shakeel Butt
2026-09-02 13:16 ` [PATCH 3/4] mm: replace PF_KSWAPD flag with kthread_func() check Kefeng Wang
2026-09-02 15:36   ` Shakeel Butt
2026-09-02 16:35   ` Vlastimil Babka (SUSE)
2026-09-02 16:39   ` Zi Yan
2026-09-02 13:16 ` [PATCH 4/4] mm: replace PF_KCOMPACTD " Kefeng Wang
2026-09-02 15:37   ` Shakeel Butt
2026-09-02 16:36   ` Vlastimil Babka (SUSE)
2026-09-02 16:40   ` Zi Yan
2026-09-02 15:31 ` [PATCH 0/4] mm: replace PF_KCOMPACTD/PF_KSWAPD with kthread_func() Shakeel Butt
2026-09-02 20:44 ` Andrew Morton
2026-09-03 13:37   ` [PATCH] xfs: fix NOFS state corruption in btree split worker Kefeng Wang
2026-09-03 13:52     ` Brian Foster
2026-09-04  0:41       ` Kefeng Wang
2026-09-04 11:57         ` Brian Foster [this message]
2026-09-03 13:40   ` [PATCH 0/4] mm: replace PF_KCOMPACTD/PF_KSWAPD with kthread_func() Kefeng Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=apqyG_d6jEUqcMN-@bfoster \
    --to=bfoster@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=brendan.jackman@linux.dev \
    --cc=cem@kernel.org \
    --cc=david@kernel.org \
    --cc=djwong@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=qi.zheng@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=wangkefeng.wang@huawei.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox