void xfs transaction reservation recursion
Subsystems (4)
v4 cover void xfs transaction reservation recursion
From: Yafang Shao <[email protected]>
This patchset avoids transaction reservation recursion by reintroducing the discarded PF_FSTRANS in a new way, suggested by Dave. In this new implementation, two new helpers are introduced, which are xfs_trans_context_{begin, end}, suggested by Christoph. And re-using the task->journal_info to indicates whehter the task is in fstrans or not, suggested by Willy.
v4:
- retitle from "xfs: introduce task->in_fstrans for transaction reservation recursion protection"
- reuse current->journal_info, per Willy
Yafang Shao (2):
xfs: avoid double restore PF_MEMALLOC_NOFS if transaction reservation
fails
xfs: avoid transaction reservation recursion
fs/iomap/buffered-io.c | 4 ++-- fs/xfs/libxfs/xfs_btree.c | 2 ++ fs/xfs/xfs_aops.c | 3 +++ fs/xfs/xfs_linux.h | 19 +++++++++++++++++++ fs/xfs/xfs_trans.c | 21 +++++++++++++++------ 5 files changed, 41 insertions(+), 8 deletions(-)
Signature · 1 line
2.18.1
PATCH v4 1/2 xfs: avoid double restore PF_MEMALLOC_NOFS if transaction reservation fails
From: Yafang Shao <[email protected]>
In xfs_trans_alloc(), if xfs_trans_reserve() fails, it will call xfs_trans_cancel(), in which it will restore the flag PF_MEMALLOC_NOFS. However this flags has been restored in xfs_trans_reserve(). Although this behavior doesn't introduce any obvious issue, we'd better improve it.
--- fs/xfs/xfs_trans.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
fs/xfs/xfs_trans.c +9 -6
@@ -162,10 +162,9 @@ xfs_trans_reserve(*/ if (blocks >0 ) { error =xfs_mod_fdblocks (mp, -((int64_t )blocks), rsvd); -if (error !=0 ) { -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +if (error !=0 )return -ENOSPC ; - } + tp->t_blk_res += blocks; }
@@ -240,8 +239,6 @@ xfs_trans_reserve( tp->t_blk_res =0 ; } -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); -return error; }
@@ -972,6 +969,7 @@ xfs_trans_roll( struct xfs_trans **tpp) {struct xfs_trans *trans = *tpp; +struct xfs_trans *tp;struct xfs_trans_res tres;int error;
@@ -1005,5 +1003,10 @@ xfs_trans_roll(* the prior and the next transactions. */ tres.tr_logflags =XFS_TRANS_PERM_LOG_RES ; -return xfs_trans_reserve (*tpp, &tres,0 ,0 ); + tp = *tpp; + error =xfs_trans_reserve (tp, &tres,0 ,0 ); +if (error) +current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); + +return error; }
Signature · 1 line
2.18.1
Re: [PATCH v4 1/2] xfs: avoid double restore PF_MEMALLOC_NOFS if transaction reservation fails
Quoted 6 lines · Yafang Shao
From: Yafang Shao <[email protected]>
In
xfs_trans_alloc(), ifxfs_trans_reserve()fails, it will callxfs_trans_cancel(), in which it will restore the flagPF_MEMALLOC_NOFS. However this flags has been restored inxfs_trans_reserve(). Although this behavior doesn't introduce any obvious issue, we'd better improve it.
.....
Quoted 29 lines
Signed-off-by Yafang Shao <[email protected]>Cc Dave Chinner <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Michal Hocko <[email protected]>Cc Darrick J. Wong <[email protected]>Cc Matthew Wilcox <[email protected]>--- fs/xfs/xfs_trans.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)fs/xfs/xfs_trans.c +2
-3@@ -162,10 +162,9 @@ xfs_trans_reserve(*/ if (blocks >0 ) { error =xfs_mod_fdblocks (mp, -((int64_t )blocks), rsvd); -if (error !=0 ) { -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +if (error !=0 )return -ENOSPC ; - } + tp->t_blk_res += blocks; }
fs/xfs/xfs_trans.c xfs_trans_reserve( · line 243
Quoted 7 lines
fs/xfs/xfs_trans.c +0
-2@@ -240,8 +239,6 @@ xfs_trans_reserve( tp->t_blk_res =0 ; } -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); -return error; }
So you fix it here....
Quoted 11 lines
fs/xfs/xfs_trans.c +1
-0@@ -972,6 +969,7 @@ xfs_trans_roll( struct xfs_trans **tpp) {struct xfs_trans *trans = *tpp; +struct xfs_trans *tp;struct xfs_trans_res tres;int error;@@ -1005,5 +1003,10 @@ xfs_trans_roll(* the prior and the next transactions.
fs/xfs/xfs_trans.c xfs_trans_roll( · line 1012
Quoted 10 lines
fs/xfs/xfs_trans.c +6
-1*/ tres.tr_logflags =XFS_TRANS_PERM_LOG_RES ; -return xfs_trans_reserve (*tpp, &tres,0 ,0 ); + tp = *tpp; + error =xfs_trans_reserve (tp, &tres,0 ,0 ); +if (error) +current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); + +return error; }
.... but then introduce the exact same issue here. i.e. when xfs_trans_roll() fails, the caller will call xfs_trans_cancel() on the returned transaction....
Realistically, I think this is kinda missing the overall intent of rolling transactions. The issue here is that when we commit a transaction, we unconditionally clear the PF_MEMALLOC_NOFS flag via __xfs_trans_commit() just before freeing the transaction despite the fact the long running rolling transaction has not yet completed.
This means that when we roll a transactions, we are bouncing the NOFS state unnecessarily like so:
t0 t1 t2
alloc
reserve
NOFS
roll
alloc
commit
clear NOFS
reserve NOFS roll alloc commit clear NOFS reserve NOFS .....
right until the last commit of the sequence. IOWs, we are repeatedly setting and clearing NOFS even though we actually only need to set it at the t0 and clear it on the final commit or cancel.
Hence I think that __xfs_trans_commit() should probably only clear NOFS on successful commit if @regrant is false (i.e. not called from xfs_trans_roll()) and the setting of NOFS should be removed from xfs_trans_reserve() and moved up into the initial xfs_trans_alloc() before xfs_trans_reserve() is called.
This way the calls to either xfs_trans_commit() or xfs_trans_cancel() will clear the NOFS state as they indicate that we are exiting transaction context completely....
Also, please convert these to memalloc_nofs_save()/restore() calls as that is the way we are supposed to mark these regions now.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH v4 1/2] xfs: avoid double restore PF_MEMALLOC_NOFS if transaction reservation fails
Quoted 2 lines · Dave Chinner
Also, please convert these to memalloc_nofs_save()/restore() calls as that is the way we are supposed to mark these regions now.
I have a patch for that!
From 7830a7dc66f349e16ef34bd408b9962f4c4bcdba Mon Sep 17 00:00:00 2001
From: "Matthew Wilcox (Oracle)" <[email protected]>
Date: Fri, 27 Mar 2020 13:01:57 -0400
Subject: [PATCH 3/6] xfs: Convert to memalloc_nofs_save
To: [email protected],
[email protected]
Cc: [email protected],
[email protected],
Mikulas Patocka <[email protected]>,
Jens Axboe <[email protected]>,
NeilBrown <[email protected]>
Instead of using custom macros to set/restore PF_MEMALLOC_NOFS, use memalloc_nofs_save() like the rest of the kernel.
--- fs/xfs/kmem.c | 2 +- fs/xfs/xfs_aops.c | 4 ++-- fs/xfs/xfs_buf.c | 2 +- fs/xfs/xfs_linux.h | 6 ------ fs/xfs/xfs_trans.c | 14 +++++++------- fs/xfs/xfs_trans.h | 2 +- 6 files changed, 12 insertions(+), 18 deletions(-)
fs/xfs/kmem.c +1 -1
@@ -35,7 +35,7 @@ kmem_alloc(size_t size, xfs_km_flags_t flags)* __vmalloc() will allocate data pages and auxiliary structures (e.g. * pagetables) with GFP_KERNEL, yet we may be under GFP_NOFS context here. Hence * we need to tell memory reclaim that we are in such a context via -* PF_MEMALLOC_NOFS to prevent memory reclaim re-entering the filesystem here +* memalloc_nofs to prevent memory reclaim re-entering the filesystem here * and potentially deadlocking. */ static void *
fs/xfs/xfs_aops.c +2 -2
@@ -62,7 +62,7 @@ xfs_setfilesize_trans_alloc(* We hand off the transaction to the completion thread now, so * clear the flag here. */ -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +memalloc_nofs_restore (tp->t_memalloc);return 0 ; }
@@ -125,7 +125,7 @@ xfs_setfilesize_ioend(* thus we need to mark ourselves as being in a transaction manually. * Similarly for freeze protection. */ -current_set_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); + tp->t_memalloc =memalloc_nofs_save ();__sb_writers_acquired (VFS_I (ip)->i_sb,SB_FREEZE_FS );/* we abort the update if there was an IO error */
fs/xfs/xfs_buf.c +1 -1
@@ -470,7 +470,7 @@ _xfs_buf_map_pages(* vm_map_ram() will allocate auxiliary structures (e.g. * pagetables) with GFP_KERNEL, yet we are likely to be under * GFP_NOFS context here. Hence we need to tell memory reclaim -* that we are in such a context via PF_MEMALLOC_NOFS to prevent +* that we are in such a context via memalloc_nofs to prevent * memory reclaim re-entering the filesystem here and * potentially deadlocking. */
fs/xfs/xfs_linux.h +0 -6
@@ -104,12 +104,6 @@ typedef __u32 xfs_nlink_t;#define current_cpu () (raw_smp_processor_id())#define current_pid () (current->pid)#define current_test_flags (f) (current->flags & (f)) -#define current_set_flags_nested (sp, f) \ - (*(sp) = current->flags, current->flags |= (f)) -#define current_clear_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 */
fs/xfs/xfs_trans.c +7 -7
@@ -118,7 +118,7 @@ xfs_trans_dup( ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; tp->t_rtx_res = tp->t_rtx_res_used; - ntp->t_pflags = tp->t_pflags; + ntp->t_memalloc = tp->t_memalloc;/* move deferred ops over to the new tp */ xfs_defer_move (ntp, tp);
@@ -153,7 +153,7 @@ xfs_trans_reserve(bool rsvd = (tp->t_flags &XFS_TRANS_RESERVE ) !=0 ;/* Mark this thread as being in a transaction */ -current_set_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); + tp->t_memalloc =memalloc_nofs_save ();/* * Attempt to reserve the needed disk blocks by decrementing
@@ -163,7 +163,7 @@ xfs_trans_reserve(if (blocks >0 ) { error =xfs_mod_fdblocks (mp, -((int64_t )blocks), rsvd);if (error !=0 ) { -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +memalloc_nofs_restore (tp->t_memalloc);return -ENOSPC ; } tp->t_blk_res += blocks;
@@ -240,7 +240,7 @@ xfs_trans_reserve( tp->t_blk_res =0 ; } -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +memalloc_nofs_restore (tp->t_memalloc);return error; }
@@ -861,7 +861,7 @@ __xfs_trans_commit(xfs_log_commit_cil (mp, tp, &commit_lsn, regrant); -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +memalloc_nofs_restore (tp->t_memalloc);xfs_trans_free (tp);/*
@@ -893,7 +893,7 @@ __xfs_trans_commit(xfs_log_ticket_ungrant (mp->m_log, tp->t_ticket); tp->t_ticket =NULL ; } -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +memalloc_nofs_restore (tp->t_memalloc);xfs_trans_free_items (tp, !!error);xfs_trans_free (tp);
@@ -954,7 +954,7 @@ xfs_trans_cancel( }/* mark this thread as no longer being in a transaction */ -current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +memalloc_nofs_restore (tp->t_memalloc);xfs_trans_free_items (tp, dirty);xfs_trans_free (tp);
fs/xfs/xfs_trans.h +1 -1
@@ -118,6 +118,7 @@ typedef struct xfs_trans {unsigned int t_rtx_res;/* # of rt extents resvd */ unsigned int t_rtx_res_used;/* # of resvd rt extents used */ unsigned int t_flags;/* misc flags */ +unsigned int t_memalloc;/* saved memalloc state */ xfs_fsblock_t t_firstblock;/* first block allocated */ struct xlog_ticket *t_ticket;/* log mgr ticket */ struct xfs_mount *t_mountp;/* ptr to fs mount struct */
@@ -144,7 +145,6 @@ typedef struct xfs_trans {struct list_head t_items;/* log item descriptors */ struct list_head t_busy;/* list of busy extents */ struct list_head t_dfops;/* deferred operations */ -unsigned long t_pflags;/* saved process flags state */ } xfs_trans_t;/*
Signature · 1 line
2.27.0
Re: [PATCH v4 1/2] xfs: avoid double restore PF_MEMALLOC_NOFS if transaction reservation fails
Quoted 5 lines · Matthew Wilcox
Quoted 2 lines · Dave Chinner
Also, please convert these to memalloc_nofs_save()/restore() calls as that is the way we are supposed to mark these regions now.
I have a patch for that!
Did you compile test it? :)
Quoted 8 lines
--- fs/xfs/kmem.c | 2 +- fs/xfs/xfs_aops.c | 4 ++-- fs/xfs/xfs_buf.c | 2 +- fs/xfs/xfs_linux.h | 6 ------ fs/xfs/xfs_trans.c | 14 +++++++------- fs/xfs/xfs_trans.h | 2 +- 6 files changed, 12 insertions(+), 18 deletions(-)
.....
Quoted 5 lines
fs/xfs/xfs_linux.h +0
-0
fs/xfs/xfs_linux.h typedef __u32 xfs_nlink_t; · line 112
Quoted 9 lines
fs/xfs/xfs_linux.h +0
-6@@ -104,12 +104,6 @@ typedef __u32 xfs_nlink_t;#define current_cpu () (raw_smp_processor_id())#define current_pid () (current->pid)#define current_test_flags (f) (current->flags & (f)) -#define current_set_flags_nested (sp, f) \ - (*(sp) = current->flags, current->flags |= (f)) -#define current_clear_flags_nested (sp, f) \ - (*(sp) = current->flags, current->flags &= ~(f)) -#define current_restore_flags_nested (sp, f) \ - (current->flags = ((current->flags & ~(f)) | (*(sp) & (f))))
current_set_flags_nested() and current_restore_flags_nested() are used in xfs_btree_split_worker() in fs/xfs/libxfs/xfs_btree.c and that's not a file you modified in this patch...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH v4 1/2] xfs: avoid double restore PF_MEMALLOC_NOFS if transaction reservation fails
Quoted 31 lines · Dave Chinner
Quoted 5 lines · Matthew Wilcox
Quoted 2 lines · Dave Chinner
Also, please convert these to memalloc_nofs_save()/restore() calls as that is the way we are supposed to mark these regions now.
I have a patch for that!
Did you compile test it? :)
Quoted 8 lines
--- fs/xfs/kmem.c | 2 +- fs/xfs/xfs_aops.c | 4 ++-- fs/xfs/xfs_buf.c | 2 +- fs/xfs/xfs_linux.h | 6 ------ fs/xfs/xfs_trans.c | 14 +++++++------- fs/xfs/xfs_trans.h | 2 +- 6 files changed, 12 insertions(+), 18 deletions(-).....
Quoted 10 lines
fs/xfs/xfs_linux.h +0
-2@@ -104,12 +104,6 @@ typedef __u32 xfs_nlink_t;#define current_cpu () (raw_smp_processor_id())#define current_pid () (current->pid)#define current_test_flags (f) (current->flags & (f)) -#define current_set_flags_nested (sp, f) \ - (*(sp) = current->flags, current->flags |= (f))
Quoted 9 lines
fs/xfs/xfs_linux.h typedef __u32 xfs_nlink_t; · line 112
Quoted 4 lines
fs/xfs/xfs_linux.h +0
-4-#define current_clear_flags_nested (sp, f) \ - (*(sp) = current->flags, current->flags &= ~(f)) -#define current_restore_flags_nested (sp, f) \ - (current->flags = ((current->flags & ~(f)) | (*(sp) & (f))))
current_set_flags_nested()andcurrent_restore_flags_nested()are used inxfs_btree_split_worker()infs/xfs/libxfs/xfs_btree.cand that's not a file you modified in this patch...
It is in Willy's another patch "mm: Add become_kswapd and restore_kswapd"[1], which can be committed independently from "Overhaul memalloc_no*" series. I will carry it in the next version.
[1] https://lore.kernel.org/linux-mm/[email protected]/#t
[2] https://lore.kernel.org/linux-mm/[email protected]/
Signature · 2 lines
Thanks Yafang
PATCH v4 2/2 xfs: avoid transaction reservation recursion
In reply to Yafang ShaoFrom: Yafang Shao <[email protected]>
PF_FSTRANS which is used to avoid transaction reservation recursion, is dropped since commit 9070733b4efa ("xfs: abstract PF_FSTRANS to PF_MEMALLOC_NOFS") and commit 7dea19f9ee63 ("mm: introduce memalloc_nofs_{save,restore} API") and replaced by PF_MEMALLOC_NOFS which means to avoid filesystem reclaim recursion. That change is subtle. Let's take the exmple of the check of WARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS)) to explain why this abstraction from PF_FSTRANS to PF_MEMALLOC_NOFS is not proper.
Below comment is quoted from Dave,
Quoted 5 lines
It wasn't for memory allocation recursion protection in XFS - it was for transaction reservation recursion protection by something trying to flush data pages while holding a transaction reservation. Doing this could deadlock the journal because the existing reservation could prevent the nested reservation for being able to reserve space
Quoted 8 lines
in the journal and that is a self-deadlock vector.
IOWs, this check is not protecting against memory reclaim recursion bugs at all (that's the previous check [1]). This check is protecting against the filesystem calling writepages directly from a context where it can self-deadlock.
So what we are seeing here is that thePF_FSTRANS->PF_MEMALLOC_NOFSabstraction lost all the actual useful information about what type of error this check was protecting against.
As a result, we should reintroduce PF_FSTRANS. As current->journal_info isn't used in XFS, we can reuse it to indicate whehter the task is in fstrans or not.
[1]. Below check is to avoid memory reclaim recursion. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
PF_MEMALLOC))
goto redirty;--- fs/iomap/buffered-io.c | 4 ++-- fs/xfs/libxfs/xfs_btree.c | 2 ++ fs/xfs/xfs_aops.c | 3 +++ fs/xfs/xfs_linux.h | 19 +++++++++++++++++++ fs/xfs/xfs_trans.c | 8 +++++++- 5 files changed, 33 insertions(+), 3 deletions(-)
fs/iomap/buffered-io.c +2 -2
@@ -1500,9 +1500,9 @@ iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data)/* * Given that we do not allow direct reclaim to call us, we should -* never be called in a recursive filesystem reclaim context. +* never be called while in a filesystem transaction. */ -if (WARN_ON_ONCE (current->flags &PF_MEMALLOC_NOFS )) +if (WARN_ON_ONCE (current->journal_info)) goto redirty;/*
fs/xfs/libxfs/xfs_btree.c +2 -0
@@ -2825,6 +2825,7 @@ xfs_btree_split_worker(if (args->kswapd) new_pflags |=PF_MEMALLOC |PF_SWAPWRITE |PF_KSWAPD ; +xfs_trans_context_start ();current_set_flags_nested (&pflags, new_pflags); args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
@@ -2832,6 +2833,7 @@ xfs_btree_split_worker(complete (args->done);current_restore_flags_nested (&pflags, new_pflags); +xfs_trans_context_end (); }/*
fs/xfs/xfs_aops.c +3 -0
@@ -63,6 +63,8 @@ xfs_setfilesize_trans_alloc(* clear the flag here. */ current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +xfs_trans_context_end (); +return 0 ; }
@@ -125,6 +127,7 @@ xfs_setfilesize_ioend(* thus we need to mark ourselves as being in a transaction manually. * Similarly for freeze protection. */ +xfs_trans_context_start ();current_set_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS );__sb_writers_acquired (VFS_I (ip)->i_sb,SB_FREEZE_FS );
fs/xfs/xfs_linux.h +19 -0
@@ -111,6 +111,25 @@ typedef __u32 xfs_nlink_t;#define current_restore_flags_nested (sp, f) \ (current->flags = ((current->flags & ~(f)) | (*(sp) & (f)))) +static inline void xfs_trans_context_start (void ) +{ +long flags = (long )current->journal_info; + +/* +* Reuse journal_info to indicate whehter the current is in fstrans +* or not. +*/ + current->journal_info = (void *)(flags +1 ); +} + +static inline void xfs_trans_context_end (void ) +{ +long flags = (long )current->journal_info; + +WARN_ON_ONCE (flags <=0 ); + current->journal_info = ((void *)(flags -1 )); +} +#define NBBY 8/* number of bits per byte */ /*
fs/xfs/xfs_trans.c +7 -1
@@ -153,6 +153,7 @@ xfs_trans_reserve(bool rsvd = (tp->t_flags &XFS_TRANS_RESERVE ) !=0 ;/* Mark this thread as being in a transaction */ +xfs_trans_context_start ();current_set_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS );/*
@@ -859,6 +860,7 @@ __xfs_trans_commit(xfs_log_commit_cil (mp, tp, &commit_lsn, regrant);current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +xfs_trans_context_end ();xfs_trans_free (tp);/*
@@ -891,6 +893,7 @@ __xfs_trans_commit( tp->t_ticket =NULL ; }current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +xfs_trans_context_end ();xfs_trans_free_items (tp, !!error);xfs_trans_free (tp);
@@ -952,6 +955,7 @@ xfs_trans_cancel(/* mark this thread as no longer being in a transaction */ current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +xfs_trans_context_end ();xfs_trans_free_items (tp, dirty);xfs_trans_free (tp);
@@ -1005,8 +1009,10 @@ xfs_trans_roll( tres.tr_logflags =XFS_TRANS_PERM_LOG_RES ; tp = *tpp; error =xfs_trans_reserve (tp, &tres,0 ,0 ); -if (error) +if (error) {current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +xfs_trans_context_end (); + }return error; }
Signature · 1 line
2.18.1
Re: [PATCH v4 2/2] xfs: avoid transaction reservation recursion
Quoted 25 lines · Yafang Shao
From: Yafang Shao <[email protected]>
PF_FSTRANSwhich is used to avoid transaction reservation recursion, is dropped since commit9070733b4efa("xfs: abstractPF_FSTRANStoPF_MEMALLOC_NOFS") and commit7dea19f9ee63("mm: introduce memalloc_nofs_{save,restore} API") and replaced byPF_MEMALLOC_NOFSwhich means to avoid filesystem reclaim recursion. That change is subtle. Let's take the exmple of the check ofWARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS)) to explain why this abstraction fromPF_FSTRANStoPF_MEMALLOC_NOFSis not proper.
Below comment is quoted from Dave,Quoted 5 lines
It wasn't for memory allocation recursion protection in XFS - it was for transaction reservation recursion protection by something trying to flush data pages while holding a transaction reservation. Doing this could deadlock the journal because the existing reservation could prevent the nested reservation for being able to reserve space
Quoted 8 lines
in the journal and that is a self-deadlock vector.
IOWs, this check is not protecting against memory reclaim recursion bugs at all (that's the previous check [1]). This check is protecting against the filesystem calling writepages directly from a context where it can self-deadlock.
So what we are seeing here is that thePF_FSTRANS->PF_MEMALLOC_NOFSabstraction lost all the actual useful information about what type of error this check was protecting against.
Quoted 3 lines
As a result, we should reintroduce
PF_FSTRANS. Ascurrent->journal_infoisn't used in XFS, we can reuse it to indicate whehter the task is in fstrans or not.
IF we are just going to use ->journal_info, do it the simple way.
fs/xfs/libxfs/xfs_btree.c xfs_btree_split_worker( · line 2828
Quoted 8 lines
index
2d25bab68764..0795511f9e6a100644fs/xfs/libxfs/xfs_btree.c +1
-0@@ -2825,6 +2825,7 @@ xfs_btree_split_worker(if (args->kswapd) new_pflags |=PF_MEMALLOC |PF_SWAPWRITE |PF_KSWAPD ; +xfs_trans_context_start ();
Not really. We are transferring a transaction context here, not starting one. Hence this reads somewhat strangely.
This implies that the helper function should be something like:
xfs_trans_context_set(tp);
fs/xfs/libxfs/xfs_btree.c xfs_btree_split_worker( · line 2836
Quoted 8 lines
fs/xfs/libxfs/xfs_btree.c +1
-0current_set_flags_nested (&pflags, new_pflags); args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,@@ -2832,6 +2833,7 @@ xfs_btree_split_worker(complete (args->done);current_restore_flags_nested (&pflags, new_pflags); +xfs_trans_context_end ();
And this is more likely xfs_trans_context_clear(tp)
Reasons for this will become clear soon...
fs/xfs/xfs_aops.c xfs_setfilesize_trans_alloc( · line 66
Quoted 12 lines
fs/xfs/libxfs/xfs_btree.c +0
-0}/* fs/xfs/xfs_aops.c +1
-0@@ -63,6 +63,8 @@ xfs_setfilesize_trans_alloc(* clear the flag here. */ current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +xfs_trans_context_end ();
Note the repeated pairing of functions in this patch?
Quoted 28 lines
fs/xfs/xfs_aops.c +2
-0+return 0 ; }@@ -125,6 +127,7 @@ xfs_setfilesize_ioend(* thus we need to mark ourselves as being in a transaction manually. * Similarly for freeze protection. */ +xfs_trans_context_start ();current_set_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS );__sb_writers_acquired (VFS_I (ip)->i_sb,SB_FREEZE_FS );fs/xfs/xfs_linux.h +8
-0@@ -111,6 +111,25 @@ typedef __u32 xfs_nlink_t;#define current_restore_flags_nested (sp, f) \ (current->flags = ((current->flags & ~(f)) | (*(sp) & (f)))) +static inline void xfs_trans_context_start (void ) +{ +long flags = (long )current->journal_info; + +/* +* Reuse journal_info to indicate whehter the current is in fstrans +* or not. +*/
fs/xfs/xfs_linux.h typedef __u32 xfs_nlink_t; · line 131
Quoted 10 lines
fs/xfs/xfs_linux.h +10
-0+ current->journal_info = (void *)(flags +1 ); +} + +static inline void xfs_trans_context_end (void ) +{ +long flags = (long )current->journal_info; + +WARN_ON_ONCE (flags <=0 ); + current->journal_info = ((void *)(flags -1 )); +}
This is overly complex, and cannot be used for validation that we are clearing the transaction context we expect to be clearing. These are really "set" and "clear" operations, and for rolling transactions we are going to need an "update" operation, too.
As per my comments about the previous patch, _set() would be done in xfs_trans_alloc(), _clear() would be done on the final xfs_trans_commit() or _cancel() and _update() would be done when the transaction rolls.
Then we can roll in the NOFS updates, and we get these three helper functions that keep all the per-transaction thread state coherent:
static inline voidxfs_trans_context_set(struct xfs_trans *tp)
{
ASSERT(!current->journal_info);
current->journal_info = tp;
tp->t_flags = memalloc_nofs_save();
}static inline voidxfs_trans_context_update(struct xfs_trans *old, struct xfs_trans *new)
{
ASSERT(current->journal_info == old);
current->journal_info = new;
new->t_flags = old->t_flags;
}static inline voidxfs_trans_context_clear(struct xfs_trans *tp)
{
ASSERT(current->journal_info == tp);
current->journal_info = NULL;
memalloc_nofs_restore(tp->t_flags);
}static boolxfs_trans_context_active(void)
{
return current->journal_info != NULL;
}Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH v4 2/2] xfs: avoid transaction reservation recursion
Quoted 146 lines · Dave Chinner
Quoted 25 lines · Yafang Shao
From: Yafang Shao <[email protected]>
PF_FSTRANSwhich is used to avoid transaction reservation recursion, is dropped since commit9070733b4efa("xfs: abstractPF_FSTRANStoPF_MEMALLOC_NOFS") and commit7dea19f9ee63("mm: introduce memalloc_nofs_{save,restore} API") and replaced byPF_MEMALLOC_NOFSwhich means to avoid filesystem reclaim recursion. That change is subtle. Let's take the exmple of the check ofWARN_ON_ONCE(current->flags & PF_MEMALLOC_NOFS)) to explain why this abstraction fromPF_FSTRANStoPF_MEMALLOC_NOFSis not proper.
Below comment is quoted from Dave,Quoted 5 lines
It wasn't for memory allocation recursion protection in XFS - it was for transaction reservation recursion protection by something trying to flush data pages while holding a transaction reservation. Doing this could deadlock the journal because the existing reservation could prevent the nested reservation for being able to reserve space
Quoted 8 lines
in the journal and that is a self-deadlock vector.
IOWs, this check is not protecting against memory reclaim recursion bugs at all (that's the previous check [1]). This check is protecting against the filesystem calling writepages directly from a context where it can self-deadlock.
So what we are seeing here is that thePF_FSTRANS->PF_MEMALLOC_NOFSabstraction lost all the actual useful information about what type of error this check was protecting against.Quoted 3 lines
As a result, we should reintroduce
PF_FSTRANS. Ascurrent->journal_infoisn't used in XFS, we can reuse it to indicate whehter the task is in fstrans or not.IF we are just going to use ->journal_info, do it the simple way.
fs/xfs/libxfs/xfs_btree.c xfs_btree_split_worker( · line 2828
Quoted 8 lines
index
2d25bab68764..0795511f9e6a100644fs/xfs/libxfs/xfs_btree.c +1
-0@@ -2825,6 +2825,7 @@ xfs_btree_split_worker(if (args->kswapd) new_pflags |=PF_MEMALLOC |PF_SWAPWRITE |PF_KSWAPD ; +xfs_trans_context_start ();Not really. We are transferring a transaction context here, not starting one. Hence this reads somewhat strangely.
This implies that the helper function should be something like:
xfs_trans_context_set(tp);fs/xfs/libxfs/xfs_btree.c xfs_btree_split_worker( · line 2836
Quoted 8 lines
fs/xfs/libxfs/xfs_btree.c +1
-0current_set_flags_nested (&pflags, new_pflags); args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,@@ -2832,6 +2833,7 @@ xfs_btree_split_worker(complete (args->done);current_restore_flags_nested (&pflags, new_pflags); +xfs_trans_context_end ();And this is more likely
xfs_trans_context_clear(tp)Reasons for this will become clear soon...
fs/xfs/xfs_aops.c xfs_setfilesize_trans_alloc( · line 66
Quoted 12 lines
fs/xfs/libxfs/xfs_btree.c +0
-0}/* fs/xfs/xfs_aops.c +1
-0@@ -63,6 +63,8 @@ xfs_setfilesize_trans_alloc(* clear the flag here. */ current_restore_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS ); +xfs_trans_context_end ();Note the repeated pairing of functions in this patch?
Quoted 28 lines
fs/xfs/xfs_aops.c +2
-0+return 0 ; }@@ -125,6 +127,7 @@ xfs_setfilesize_ioend(* thus we need to mark ourselves as being in a transaction manually. * Similarly for freeze protection. */ +xfs_trans_context_start ();current_set_flags_nested (&tp->t_pflags,PF_MEMALLOC_NOFS );__sb_writers_acquired (VFS_I (ip)->i_sb,SB_FREEZE_FS );fs/xfs/xfs_linux.h +8
-0@@ -111,6 +111,25 @@ typedef __u32 xfs_nlink_t;#define current_restore_flags_nested (sp, f) \ (current->flags = ((current->flags & ~(f)) | (*(sp) & (f)))) +static inline void xfs_trans_context_start (void ) +{ +long flags = (long )current->journal_info; + +/* +* Reuse journal_info to indicate whehter the current is in fstrans +* or not. +*/ fs/xfs/xfs_linux.h typedef __u32 xfs_nlink_t; · line 131
Quoted 10 lines
fs/xfs/xfs_linux.h +10
-0+ current->journal_info = (void *)(flags +1 ); +} + +static inline void xfs_trans_context_end (void ) +{ +long flags = (long )current->journal_info; + +WARN_ON_ONCE (flags <=0 ); + current->journal_info = ((void *)(flags -1 )); +}This is overly complex, and cannot be used for validation that we are clearing the transaction context we expect to be clearing. These are really "set" and "clear" operations, and for rolling transactions we are going to need an "update" operation, too.
As per my comments about the previous patch,
_set()would be done inxfs_trans_alloc(),_clear()would be done on the finalxfs_trans_commit()or_cancel()and_update()would be done when the transaction rolls.Then we can roll in the NOFS updates, and we get these three helper functions that keep all the per-transaction thread state coherent:
static inline void
xfs_trans_context_set(struct xfs_trans *tp){ ASSERT(!current->journal_info); current->journal_info = tp; tp->t_flags = memalloc_nofs_save(); }static inline void
xfs_trans_context_update(struct xfs_trans *old, struct xfs_trans *new){ ASSERT(current->journal_info == old); current->journal_info = new; new->t_flags = old->t_flags; }
Quoted 8 lines
static inline void
xfs_trans_context_clear(struct xfs_trans *tp){ ASSERT(current->journal_info == tp); current->journal_info = NULL; memalloc_nofs_restore(tp->t_flags); }
Below helper will be used in fs/iomap/buffered-io.c, so I think we'd better name it with fstrans_context_active() and put it in include/linux/iomap.h, while regarding the other three helpers I think we'd better put them in fs/xfs/xfs_trans.h.
Quoted 6 lines
static bool
xfs_trans_context_active(void){ return current->journal_info != NULL; }
Many thanks for the detailed explanation, I will update with your suggestion in the next version.
Signature · 2 lines
Thanks Yafang