Skip to contentSkip to conversation

xfs: fix NOFS state corruption in btree split worker

PATCH RESEND Superseded Kefeng Wang 2 participants 7 messages Sep 11 – Sep 16 XFS filesystem

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover xfs: fix NOFS state corruption in btree split worker

xfs_trans_set_context() overwrites tp->t_pflags, dropping the caller's NOFS protection. Replace it with local memalloc_nofs_save()/restore() to fix it, also drop the obsolete kswapd flag inheritance, which simplifying the fix.

Note:
- rebased on v7.3-rc2
- Add ACK/RB
- split the patches, suggested by Christoph Hellwig[1]

[1] https://lore.kernel.org/linux-mm/[email protected]/

Kefeng Wang (2):
xfs: remove dead kswapd flag inheritance from btree split worker
xfs: fix NOFS state corruption in btree split worker

 fs/xfs/libxfs/xfs_btree.c | 22 ++++++----------------
 fs/xfs/xfs_platform.h     |  4 ----
 2 files changed, 6 insertions(+), 20 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH RESEND 1/2 xfs: remove dead kswapd flag inheritance from btree split worker

Commit 1f6d64829db7 ("xfs: block allocation work needs to be kswapd aware") added PF_MEMALLOC | PF_KSWAPD inheritance to xfs_btree_split_worker() so that block allocation offloaded from kswapd to a workqueue thread could access emergency memory reserves and avoid reclaim throttling.

pageout() no longer calls ->writepage() for filesystem folios -- it returns PAGE_ACTIVATE for non-shmem, non-anon pages. kswapd therefore never enters XFS writeback and cannot reach btree split. The only path that offloads btree splits is unwritten extent conversion at IO completion (xfs_end_io -> xfs_iomap_write_unwritten), which runs in a workqueue context where current_is_kswapd() is always false.

Let's remove the dead kswapd flag and related codes.

---
 fs/xfs/libxfs/xfs_btree.c | 18 +-----------------
 fs/xfs/xfs_platform.h     |  4 ----
 2 files changed, 1 insertion(+), 21 deletions(-)
fs/xfs/libxfs/xfs_btree.c +1 -17
index 60ef7f08b1d3..6738d9d1511b 100644
@@ -2994,7 +2994,6 @@ struct xfs_btree_split_args {
 	struct xfs_btree_cur	**curp;
 	int			*stat;		/* success/failure */
 	int			result;
-	bool			kswapd;	/* allocation in kswapd context */
 	struct completion	*done;
 	struct work_struct	work;
 };
@@ -3008,33 +3007,18 @@ xfs_btree_split_worker(
 {
 	struct xfs_btree_split_args	*args = container_of(work,
 						struct xfs_btree_split_args, work);
-	unsigned long		pflags;
-	unsigned long		new_pflags = 0;
-
-	/*
-	 * we are in a transaction context here, but may also be doing work
-	 * in kswapd context, and hence we may need to inherit that state
-	 * temporarily to ensure that we don't block waiting for memory reclaim
-	 * in any way.
-	 */
-	if (args->kswapd)
-		new_pflags |= PF_MEMALLOC | PF_KSWAPD;
-
-	current_set_flags_nested(&pflags, new_pflags);
 	xfs_trans_set_context(args->cur->bc_tp);
 
 	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);
-	current_restore_flags_nested(&pflags, new_pflags);
 
 	/*
 	 * Do not access args after complete() has run here. We don't own args
 	 * and the owner may run and free args before we return here.
 	 */
 	complete(args->done);
-
 }
 
 /*
@@ -3078,7 +3062,7 @@ xfs_btree_split(
 	args.curp = curp;
 	args.stat = stat;
 	args.done = &done;
-	args.kswapd = current_is_kswapd();
+
 	INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
 	queue_work(xfs_alloc_wq, &args.work);
 	wait_for_completion(&done);
fs/xfs/xfs_platform.h +0 -4
index 5d542e95fe44..a49fa159894d 100644
@@ -115,10 +115,6 @@ typedef __u32			xfs_nlink_t;
 #define xfs_blockgc_secs	xfs_params.blockgc_timer.val
 
 #define current_cpu()		(raw_smp_processor_id())
-#define current_set_flags_nested(sp, f)		\
-		(*(sp) = current->flags, current->flags |= (f))
-#define current_restore_flags_nested(sp, f)	\
-		(current->flags = ((current->flags & ~(f)) | (*(sp) & (f))))
 
 #define NBBY		8		/* number of bits per byte */
 
Signature · 1 line
2.55.0
#2 lore raw

PATCH RESEND 2/2 xfs: fix NOFS state corruption in btree split worker

In reply to Kefeng Wang

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.

Fixes 756b1c343333 ("xfs: use current->journal_info for detecting transaction recursion")
---
 fs/xfs/libxfs/xfs_btree.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_btree.c +8 -2
index 6738d9d1511b..a0133d662096 100644
@@ -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
Signature · 1 line
2.55.0
maintainer
#3 lore raw
In reply to Kefeng Wang
Quoted 11 lines · Kefeng Wang

xfs_trans_set_context() overwrites tp->t_pflags, dropping the caller's NOFS protection. Replace it with local memalloc_nofs_save()/restore() to fix it, also drop the obsolete kswapd flag inheritance, which simplifying the fix.

Note:
- rebased on v7.3-rc2
- Add ACK/RB
- split the patches, suggested by Christoph Hellwig[1]

[1] https://lore.kernel.org/linux-mm/[email protected]/

Can you please give me some more context on why are you resending it? I received an email from Andrew that he had pulled this patch already from the previous series.
You were Cc'ed on the email so I'm assuming you didn't see it?

Quoted 11 lines

Kefeng Wang (2):
xfs: remove dead kswapd flag inheritance from btree split worker
xfs: fix NOFS state corruption in btree split worker

 fs/xfs/libxfs/xfs_btree.c | 22 ++++++----------------
 fs/xfs/xfs_platform.h     |  4 ----
 2 files changed, 6 insertions(+), 20 deletions(-)
Signature · 1 line
2.55.0
#4 lore raw
Quoted 11 lines · Carlos Maiolino
Quoted 9 lines · Kefeng Wang

xfs_trans_set_context() overwrites tp->t_pflags, dropping the caller's NOFS protection. Replace it with local memalloc_nofs_save()/restore() to fix it, also drop the obsolete kswapd flag inheritance, which simplifying the fix.

Note:
- rebased on v7.3-rc2
- Add ACK/RB
- split the patches, suggested by Christoph Hellwig[1]

Quoted 7 lines
Quoted 1 line

[1] https://lore.kernel.org/linux-mm/[email protected]/

Can you please give me some more context on why are you resending it? I received an email from Andrew that he had pulled this patch already from the previous series.
You were Cc'ed on the email so I'm assuming you didn't see it?

Andrew only merged the dead kswapd flags part without the important bugfix. as Christoph said in above link[1], he suggest me send a standalone series that we can get the bugfix into 7.3 and -stable with as few dependencies as possible. So I rebase on v7.3-rc2 and resend them. If I misunderstood, please correct me.

Quoted 11 lines
Quoted 11 lines

Kefeng Wang (2):
xfs: remove dead kswapd flag inheritance from btree split worker
xfs: fix NOFS state corruption in btree split worker

  fs/xfs/libxfs/xfs_btree.c | 22 ++++++----------------
  fs/xfs/xfs_platform.h     |  4 ----
  2 files changed, 6 insertions(+), 20 deletions(-)
Signature · 1 line
2.55.0
maintainer
#5 lore raw
Quoted 22 lines · Kefeng Wang
Quoted 11 lines · Carlos Maiolino
Quoted 9 lines · Kefeng Wang

xfs_trans_set_context() overwrites tp->t_pflags, dropping the caller's NOFS protection. Replace it with local memalloc_nofs_save()/restore() to fix it, also drop the obsolete kswapd flag inheritance, which simplifying the fix.

Note:
- rebased on v7.3-rc2
- Add ACK/RB
- split the patches, suggested by Christoph Hellwig[1]

Quoted 7 lines
Quoted 1 line

[1] https://lore.kernel.org/linux-mm/[email protected]/

Can you please give me some more context on why are you resending it? I received an email from Andrew that he had pulled this patch already from the previous series.
You were Cc'ed on the email so I'm assuming you didn't see it?

Quoted 5 lines

Andrew only merged the dead kswapd flags part without the important bugfix. as Christoph said in above link[1], he suggest me send a standalone series that we can get the bugfix into 7.3 and -stable with as few dependencies as possible. So I rebase on v7.3-rc2 and resend them. If I misunderstood, please correct me.

Ok, so as we spoke, Andrew already merged the previous one. Please rebase the second one on top of xfs-linux/for-next and send it again by itself.

maintainer
#6 lore raw
In reply to Kefeng Wang

Now with some more context about this...

Quoted 10 lines · Kefeng Wang

xfs_trans_set_context() overwrites tp->t_pflags, dropping the caller's NOFS protection. Replace it with local memalloc_nofs_save()/restore() to fix it, also drop the obsolete kswapd flag inheritance, which simplifying the fix.

Note:
- rebased on v7.3-rc2
- Add ACK/RB
- split the patches, suggested by Christoph Hellwig[1]

Quoted 4 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/

Kefeng Wang (2):
xfs: remove dead kswapd flag inheritance from btree split worker

I don't think Christoph suggested you to send this patch again as this has been already picked up by Andrew,

Quoted 1 line

xfs: fix NOFS state corruption in btree split worker

but only this...

Let me confirm it then I'll pick just this one up if that's the case.

Quoted 7 lines
 fs/xfs/libxfs/xfs_btree.c | 22 ++++++----------------
 fs/xfs/xfs_platform.h     |  4 ----
 2 files changed, 6 insertions(+), 20 deletions(-)
Signature · 1 line
2.55.0