xfs: fix NOFS state corruption in btree split worker
PATCH v3 1/1 xfs: fix NOFS state corruption in btree split worker
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.
---
v3:
- Using correct unsigned int type for nofs_flags, per Darrick
v2:
- Send bugfix only based on xfs-linux/for-next, suggested by Carlos[1]
https://lore.kernel.org/linux-xfs/[email protected]/
v1:
- https://lore.kernel.org/linux-xfs/[email protected]/
fs/xfs/libxfs/xfs_btree.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_btree.c +8 -2
@@ -3010,6 +3010,7 @@ xfs_btree_split_worker( struct xfs_btree_split_args, work);unsigned long pflags;unsigned long new_pflags =0 ; +unsigned int nofs_flags;/* * we are in a transaction context here, but may also be doing work
@@ -3021,12 +3022,17 @@ xfs_btree_split_worker( new_pflags |=PF_MEMALLOC |PF_KSWAPD ;current_set_flags_nested (&pflags, new_pflags); -xfs_trans_set_context (args->cur->bc_tp); + +/* +* 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);current_restore_flags_nested (&pflags, new_pflags);/*
Signature · 1 line
2.55.0
Quoted 7 lines · Kefeng Wang
xfs_btree_split_worker()callsxfs_trans_set_context()andxfs_trans_clear_context()on the caller's transaction, overwritingtp->t_pflagswith the worker's NOFS state. When the caller already hasPF_MEMALLOC_NOFSset (e.g.xfs_end_ioend_write,xfs_dio_write_end_io), the corruptedtp->t_pflagscausesxfs_trans_free()to erroneously clear the caller's NOFS protection.
Quoted 8 lines
Use memalloc_nofs_save/restore with a local variable instead so
tp->t_pflagsis never touched.Fixes 756b1c343333 ("xfs: use current->journal_info for detecting transaction recursion")Reviewed-by Christoph Hellwig <[email protected]>Reviewed-by Brian Foster <[email protected]>Signed-off-by Kefeng Wang <[email protected]>
Looks good,
Quoted 49 lines
---
v3:
- Using correct unsigned int type fornofs_flags, per Darrickv2:
- Send bugfix only based on xfs-linux/for-next, suggested by Carlos[1]
https://lore.kernel.org/linux-xfs/[email protected]/
v1:
- https://lore.kernel.org/linux-xfs/[email protected]/fs/xfs/libxfs/xfs_btree.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)fs/xfs/libxfs/xfs_btree.c +8
-2@@ -3010,6 +3010,7 @@ xfs_btree_split_worker( struct xfs_btree_split_args, work);unsigned long pflags;unsigned long new_pflags =0 ; +unsigned int nofs_flags;/* * we are in a transaction context here, but may also be doing work @@ -3021,12 +3022,17 @@ xfs_btree_split_worker( new_pflags |=PF_MEMALLOC |PF_KSWAPD ;current_set_flags_nested (&pflags, new_pflags); -xfs_trans_set_context (args->cur->bc_tp); + +/* +* 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);current_restore_flags_nested (&pflags, new_pflags);/* Signature · 1 line
2.55.0
Quoted 7 lines · Kefeng Wang
xfs_btree_split_worker()callsxfs_trans_set_context()andxfs_trans_clear_context()on the caller's transaction, overwritingtp->t_pflagswith the worker's NOFS state. When the caller already hasPF_MEMALLOC_NOFSset (e.g.xfs_end_ioend_write,xfs_dio_write_end_io), the corruptedtp->t_pflagscausesxfs_trans_free()to erroneously clear the caller's NOFS protection.
Quoted 8 lines
Use memalloc_nofs_save/restore with a local variable instead so
tp->t_pflagsis never touched.Fixes 756b1c343333 ("xfs: use current->journal_info for detecting transaction recursion")Reviewed-by Christoph Hellwig <[email protected]>Reviewed-by Brian Foster <[email protected]>Signed-off-by Kefeng Wang <[email protected]>
Looks good to me now, thanks for cleaning this up
--D
Quoted 48 lines
---
v3:
- Using correct unsigned int type fornofs_flags, per Darrickv2:
- Send bugfix only based on xfs-linux/for-next, suggested by Carlos[1]
https://lore.kernel.org/linux-xfs/[email protected]/
v1:
- https://lore.kernel.org/linux-xfs/[email protected]/fs/xfs/libxfs/xfs_btree.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)fs/xfs/libxfs/xfs_btree.c +8
-2@@ -3010,6 +3010,7 @@ xfs_btree_split_worker( struct xfs_btree_split_args, work);unsigned long pflags;unsigned long new_pflags =0 ; +unsigned int nofs_flags;/* * we are in a transaction context here, but may also be doing work @@ -3021,12 +3022,17 @@ xfs_btree_split_worker( new_pflags |=PF_MEMALLOC |PF_KSWAPD ;current_set_flags_nested (&pflags, new_pflags); -xfs_trans_set_context (args->cur->bc_tp); + +/* +* 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);current_restore_flags_nested (&pflags, new_pflags);/* Signature · 1 line
2.55.0
Quoted 8 lines · Kefeng Wang
xfs_btree_split_worker()callsxfs_trans_set_context()andxfs_trans_clear_context()on the caller's transaction, overwritingtp->t_pflagswith the worker's NOFS state. When the caller already hasPF_MEMALLOC_NOFSset (e.g.xfs_end_ioend_write,xfs_dio_write_end_io), the corruptedtp->t_pflagscausesxfs_trans_free()to erroneously clear the caller's NOFS protection.[...]
Applied to for-next, thanks!
[1/1] xfs: fix NOFS state corruption in btree split worker
commit: 23f91a51425ed3d4a5c42978fc25f1087611e4ba
Best regards,
Signature · 1 line
Carlos Maiolino <[email protected]>
Quoted 11 lines · Carlos Maiolino
Quoted 8 lines · Kefeng Wang
xfs_btree_split_worker()callsxfs_trans_set_context()andxfs_trans_clear_context()on the caller's transaction, overwritingtp->t_pflagswith the worker's NOFS state. When the caller already hasPF_MEMALLOC_NOFSset (e.g.xfs_end_ioend_write,xfs_dio_write_end_io), the corruptedtp->t_pflagscausesxfs_trans_free()to erroneously clear the caller's NOFS protection.[...]
Applied to for-next, thanks!
Thank,I checked linux-next's next-20261009, and there was a commit 23f91a51425e, but the code was incorrect during the conflict resolution. I don't know if this is the correct state for linux-next, just a reminder.
Quoted 5 lines
[1/1] xfs: fix NOFS state corruption in btree split worker
commit: 23f91a51425ed3d4a5c42978fc25f1087611e4baBest regards,
Carlos Maiolino
maintainerQuoted 11 lines · Kefeng Wang
Quoted 8 lines · Carlos Maiolino
Quoted 7 lines · Kefeng Wang
xfs_btree_split_worker()callsxfs_trans_set_context()andxfs_trans_clear_context()on the caller's transaction, overwritingtp->t_pflagswith the worker's NOFS state. When the caller already hasPF_MEMALLOC_NOFSset (e.g.xfs_end_ioend_write,xfs_dio_write_end_io), the corruptedtp->t_pflagscausesxfs_trans_free()to erroneously clear the caller's NOFS protection.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
[...]
Applied to for-next, thanks!
Thank,I checked linux-next's next-20261009, and there was a commit
23f91a51425e, but the code was incorrect during the conflict resolution. I don't know if this is the correct state for linux-next, just a reminder.
Linux-next appears to have removed all changes from this patch. I don't know why it happened honestly, I'll need to dig into it later. IIRC there was a conflict with -mm patches.
Patches staged for xfs are in xfs-linux/xfs-7.4-merge. Code there is correct.
I always attempt a merge against Linus's tree before sending him the patches and at the time if I see any conflict, I'll give him a heads up.
Thanks for letting me know though. I don't keep comparing xfs-linux state against linux-next.
Quoted 9 lines
Quoted 7 lines
[1/1] xfs: fix NOFS state corruption in btree split worker
commit: 23f91a51425ed3d4a5c42978fc25f1087611e4baBest regards,