xfs: fix NOFS state corruption in btree split worker
PATCH v2 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.
---
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 long 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 30 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.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]>---
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 +0
-0
fs/xfs/libxfs/xfs_btree.c xfs_btree_split_worker( · line 3013
Quoted 4 lines
fs/xfs/libxfs/xfs_btree.c +1
-0@@ -3010,6 +3010,7 @@ xfs_btree_split_worker( struct xfs_btree_split_args, work);unsigned long pflags;unsigned long new_pflags =0 ; +unsigned long nofs_flags;
Might want to call this old_pflags? We're adding NOFS, which means that this variable is just the old value.
Also, doesn't memalloc_nofs_save return an unsigned int?
--D
Quoted 27 lines
fs/xfs/libxfs/xfs_btree.c +7
-2/* * 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 26 lines · Darrick J. Wong
Quoted 25 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.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]>---
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(-)
Quoted 12 lines
fs/xfs/libxfs/xfs_btree.c xfs_btree_split_worker( · line 3013
Quoted 9 lines
fs/xfs/libxfs/xfs_btree.c +1
-0@@ -3010,6 +3010,7 @@ xfs_btree_split_worker( struct xfs_btree_split_args, work);unsigned long pflags;unsigned long new_pflags =0 ; +unsigned long nofs_flags;Might want to call this
old_pflags? We're adding NOFS, which means that this variable is just the old value.
$ git grep -w "= memalloc_nofs_save" fs/xfs/ fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/xfs_aops.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_dquot.c: nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_file.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_file.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_icache.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_ioend.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_log_cil.c: unsigned int nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_log_recover.c: unsigned int nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_super.c: flags = memalloc_nofs_save(); fs/xfs/xfs_trans.h: tp->t_pflags = memalloc_nofs_save(); fs/xfs/xfs_zone_gc.c: nofs_flag = memalloc_nofs_save();
The most places are using the nofs flag, I could change it to old_pflags if needed.>
Quoted 2 lines
Also, doesn't
memalloc_nofs_savereturn an unsigned int?
Sorry, I fixed the conflict but missed the type change. Should I resend a new version with the renaming(nofs_flag -> old_pflags)?
Quoted 60 lines · Kefeng Wang
Quoted 26 lines · Darrick J. Wong
Quoted 25 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.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]>---
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(-)Quoted 12 lines
fs/xfs/libxfs/xfs_btree.c xfs_btree_split_worker( · line 3013
Quoted 9 lines
fs/xfs/libxfs/xfs_btree.c +1
-0@@ -3010,6 +3010,7 @@ xfs_btree_split_worker( struct xfs_btree_split_args, work);unsigned long pflags;unsigned long new_pflags =0 ; +unsigned long nofs_flags;Might want to call this
old_pflags? We're adding NOFS, which means that this variable is just the old value.$ git grep -w "= memalloc_nofs_save" fs/xfs/ fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/xfs_aops.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_dquot.c: nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_file.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_file.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_icache.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_ioend.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_log_cil.c: unsigned int nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_log_recover.c: unsigned int nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_super.c: flags = memalloc_nofs_save(); fs/xfs/xfs_trans.h: tp->t_pflags = memalloc_nofs_save(); fs/xfs/xfs_zone_gc.c: nofs_flag = memalloc_nofs_save();
Quoted 7 lines
The most places are using the nofs flag, I could change it to
old_pflagsif needed.>Quoted 2 lines
Also, doesn't
memalloc_nofs_savereturn an unsigned int?Sorry, I fixed the conflict but missed the type change. Should I resend a new version with the
renaming(nofs_flag -> old_pflags)?
Eh, I suppose we're not at all consistent, so pflags/nofs_flag is fine. But we should still use the correct type to avoid wasting stack space.
--D
Quoted 63 lines · Darrick J. Wong
Quoted 62 lines · Kefeng Wang
Quoted 26 lines · Darrick J. Wong
Quoted 25 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.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]>---
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(-)Quoted 12 lines
fs/xfs/libxfs/xfs_btree.c xfs_btree_split_worker( · line 3013
Quoted 9 lines
fs/xfs/libxfs/xfs_btree.c +1
-0@@ -3010,6 +3010,7 @@ xfs_btree_split_worker( struct xfs_btree_split_args, work);unsigned long pflags;unsigned long new_pflags =0 ; +unsigned long nofs_flags;Might want to call this
old_pflags? We're adding NOFS, which means that this variable is just the old value.$ git grep -w "= memalloc_nofs_save" fs/xfs/ fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/scrub/xfile.c: pflags = memalloc_nofs_save(); fs/xfs/xfs_aops.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_dquot.c: nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_file.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_file.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_icache.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_ioend.c: nofs_flag = memalloc_nofs_save(); fs/xfs/xfs_log_cil.c: unsigned int nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_log_recover.c: unsigned int nofs_flags = memalloc_nofs_save(); fs/xfs/xfs_super.c: flags = memalloc_nofs_save(); fs/xfs/xfs_trans.h: tp->t_pflags = memalloc_nofs_save(); fs/xfs/xfs_zone_gc.c: nofs_flag = memalloc_nofs_save();The most places are using the nofs flag, I could change it to
old_pflagsif needed.>
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Also, doesn't
memalloc_nofs_savereturn an unsigned int?Sorry, I fixed the conflict but missed the type change. Should I resend a new version with the
renaming(nofs_flag -> old_pflags)?Eh, I suppose we're not at all consistent, so pflags/nofs_flag is fine.
I will retain the naming of nofs_flag,
Quoted 1 line
But we should still use the correct type to avoid wasting stack space.
and fix the type in v3. Thank you for your careful review.
Quoted 2 lines
--D