post-EOF block handling revamp v2
Hi all,
this series reworks handling of post-EOF blocks, primarily in ->release. This takes over the work originally started by Dave in:
https://marc.info/?l=linux-xfs&m=154951612101291&w=2
and lingering in Darricks's tree:
for years to ensure ->release doesn't too eagerly kill post-EOF block speculative preallocation and then goes on to not let preallocation on inodes with the append only flag set linger forever.
I'll also post a rebased version of Dave's patches from back then.
The first patch has already been sent standalone and as part of Darrick's fixes series, but as the rest of the series depends on it I'm sending it here again - third time's a charm.
Changes since v1:
- fix a few commit log and comment typos
Diffstat:
xfs_bmap_util.c | 34 ++++++++--------------- xfs_file.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++-- xfs_icache.c | 2 - xfs_inode.c | 82 -------------------------------------------------------- xfs_inode.h | 5 +-- 5 files changed, 85 insertions(+), 111 deletions(-)
PATCH 1/9 xfs: remove the i_mode check in xfs_release
xfs_release is only called from xfs_file_release, which is wired up as the f_op->release handler for regular files only.
--- fs/xfs/xfs_inode.c | 3 --- 1 file changed, 3 deletions(-)
fs/xfs/xfs_inode.c +0 -3
@@ -1086,9 +1086,6 @@ xfs_release(xfs_mount_t *mp = ip->i_mount;int error =0 ; -if (!S_ISREG (VFS_I (ip)->i_mode) || (VFS_I (ip)->i_mode ==0 )) -return 0 ; -/* If this is a read-only mount, don't do this (would generate I/O) */ if (xfs_is_readonly (mp))return 0 ;
Signature · 1 line
2.43.0
PATCH 2/9 xfs: refactor f_op->release handling
In reply to Christoph HellwigCurrently f_op->release is split in not very obvious ways. Fix that by folding xfs_release into xfs_file_release.
--- fs/xfs/xfs_file.c | 71 +++++++++++++++++++++++++++++++++++++++-- fs/xfs/xfs_inode.c | 79 ---------------------------------------------- fs/xfs/xfs_inode.h | 1 - 3 files changed, 68 insertions(+), 83 deletions(-)
fs/xfs/xfs_file.c +68 -3
@@ -1177,10 +1177,75 @@ xfs_dir_open( STATIC intxfs_file_release ( -struct inode *inode, -struct file *filp) +struct inode *inode, +struct file *file) { -return xfs_release (XFS_I (inode)); +struct xfs_inode *ip =XFS_I (inode); +struct xfs_mount *mp = ip->i_mount; +int error; + +/* If this is a read-only mount, don't generate I/O */ +if (xfs_is_readonly (mp)) +return 0 ; + +/* +* If we previously truncated this file and removed old data in the +* process, we want to initiate "early" writeout on the last close. +* This is an attempt to combat the notorious NULL files problem which +* is particularly noticeable from a truncate down, buffered (re-)write +* (delalloc), followed by a crash. What we are effectively doing here +* is significantly reducing the time window where we'd otherwise be +* exposed to that problem. +*/ +if (!xfs_is_shutdown (mp) && +xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) { +xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE ); +if (ip->i_delayed_blks >0 ) { + error =filemap_flush (inode->i_mapping); +if (error) +return error; + } + } + +/* +* XFS aggressively preallocates post-EOF space to generate contiguous +* allocations for writers that append to the end of the file and we +* try to free these when an open file context is released. +* +* There is no point in freeing blocks here for open but unlinked files +* as they will be taken care of by the inactivation path soon. +* +* If we can't get the iolock just skip truncating the blocks past EOF +* because we could deadlock with the mmap_lock otherwise. We'll get +* another chance to drop them once the last reference to the inode is +* dropped, so we'll never leak blocks permanently. +*/ +if (inode->i_nlink &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) { +if (xfs_can_free_eofblocks (ip) && + !xfs_iflags_test (ip,XFS_IDIRTY_RELEASE )) { +/* +* Check if the inode is being opened, written and +* closed frequently and we have delayed allocation +* blocks outstanding (e.g. streaming writes from the +* NFS server), truncating the blocks past EOF will +* cause fragmentation to occur. +* +* In this case don't do the truncation, but we have to +* be careful how we detect this case. Blocks beyond EOF +* show up as i_delayed_blks even when the inode is +* clean, so we need to truncate them away first before +* checking for a dirty release. Hence on the first +* dirty close we will still remove the speculative +* allocation, but after that we will leave it in place. +*/ + error =xfs_free_eofblocks (ip); +if (!error && ip->i_delayed_blks) +xfs_iflags_set (ip,XFS_IDIRTY_RELEASE ); + } +xfs_iunlock (ip,XFS_IOLOCK_EXCL ); + } + +return error; }STATIC int
fs/xfs/xfs_inode.c +0 -79
85 lines not on this page · raw message
fs/xfs/xfs_inode.h +0 -1
@@ -512,7 +512,6 @@ enum layout_break_reason {#define XFS_INHERIT_GID (pip) \ (xfs_has_grpid((pip)->i_mount) || (VFS_I(pip)->i_mode & S_ISGID)) -int xfs_release (struct xfs_inode *ip);int xfs_inactive (struct xfs_inode *ip);int xfs_lookup (struct xfs_inode *dp,const struct xfs_name *name,struct xfs_inode **ipp,struct xfs_name *ci_name);
Signature · 1 line
2.43.0
PATCH 3/9 xfs: don't bother returning errors from xfs_file_release
In reply to Christoph HellwigWhile ->release returns int, the only caller ignores the return value. As we're only doing cleanup work there isn't much of a point in return a value to start with, so just document the situation instead.
--- fs/xfs/xfs_file.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
fs/xfs/xfs_file.c +9 -9
@@ -1175,6 +1175,10 @@ xfs_dir_open(return error; } +/* +* Don't bother propagating errors. We're just doing cleanup, and the caller +* ignores the return value anyway. +*/ STATIC int xfs_file_release( struct inode *inode,
@@ -1182,7 +1186,6 @@ xfs_file_release( {struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount; -int error;/* If this is a read-only mount, don't generate I/O */ if (xfs_is_readonly (mp))
@@ -1200,11 +1203,8 @@ xfs_file_release(if (!xfs_is_shutdown (mp) &&xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) {xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE ); -if (ip->i_delayed_blks >0 ) { - error =filemap_flush (inode->i_mapping); -if (error) -return error; - } +if (ip->i_delayed_blks >0 ) +filemap_flush (inode->i_mapping); }/*
@@ -1238,14 +1238,14 @@ xfs_file_release(* dirty close we will still remove the speculative * allocation, but after that we will leave it in place. */ - error =xfs_free_eofblocks (ip); -if (!error && ip->i_delayed_blks) +xfs_free_eofblocks (ip); +if (ip->i_delayed_blks)xfs_iflags_set (ip,XFS_IDIRTY_RELEASE ); }xfs_iunlock (ip,XFS_IOLOCK_EXCL ); } -return error; +return 0 ; }STATIC int
Signature · 1 line
2.43.0
PATCH 4/9 xfs: skip all of xfs_file_release when shut down
In reply to Christoph HellwigThere is no point in trying to free post-EOF blocks when the file system is shutdown, as it will just error out ASAP. Instead return instantly when xfs_file_shutdown is called on a shut down file system.
--- fs/xfs/xfs_file.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
fs/xfs/xfs_file.c +6 -4
@@ -1187,8 +1187,11 @@ xfs_file_release(struct xfs_inode *ip =XFS_I (inode);struct xfs_mount *mp = ip->i_mount; -/* If this is a read-only mount, don't generate I/O */ -if (xfs_is_readonly (mp)) +/* +* If this is a read-only mount or the file system has been shut down, +* don't generate I/O. +*/ +if (xfs_is_readonly (mp) ||xfs_is_shutdown (mp))return 0 ;/*
@@ -1200,8 +1203,7 @@ xfs_file_release(* is significantly reducing the time window where we'd otherwise be * exposed to that problem. */ -if (!xfs_is_shutdown (mp) && -xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) { +if (xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) {xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE );if (ip->i_delayed_blks >0 )filemap_flush (inode->i_mapping);
Signature · 1 line
2.43.0
Re: [PATCH 4/9] xfs: skip all of xfs_file_release when shut down
Quoted 3 lines · Christoph Hellwig
There is no point in trying to free post-EOF blocks when the file system is shutdown, as it will just error out ASAP. Instead return instantly when
xfs_file_shutdownis called on a shut down file system.
^^^^^^^^
xfs_file_release()-Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH 5/9 xfs: don't free post-EOF blocks on read close
In reply to Christoph HellwigFrom: Dave Chinner <[email protected]>
When we have a workload that does open/read/close in parallel with other allocation, the file becomes rapidly fragmented. This is due to close() calling xfs_file_release() and removing the speculative preallocation beyond EOF.
Add a check for a writable context to xfs_file_release to skip the post-EOF block freeing (an the similarly pointless flushing on truncate down).
Before:
Test 1: sync write fragmentation counts
/mnt/scratch/file.0: 919/mnt/scratch/file.1: 916/mnt/scratch/file.2: 919/mnt/scratch/file.3: 920/mnt/scratch/file.4: 920/mnt/scratch/file.5: 921/mnt/scratch/file.6: 916/mnt/scratch/file.7: 918
After:
Test 1: sync write fragmentation counts
/mnt/scratch/file.0: 24/mnt/scratch/file.1: 24/mnt/scratch/file.2: 11/mnt/scratch/file.3: 24/mnt/scratch/file.4: 3/mnt/scratch/file.5: 24/mnt/scratch/file.6: 24/mnt/scratch/file.7: 23
--- fs/xfs/xfs_file.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
fs/xfs/xfs_file.c +7 -1
@@ -1217,12 +1217,18 @@ xfs_file_release(* There is no point in freeing blocks here for open but unlinked files * as they will be taken care of by the inactivation path soon. * +* When releasing a read-only context, don't flush data or trim post-EOF +* blocks. This avoids open/read/close workloads from removing EOF +* blocks that other writers depend upon to reduce fragmentation. +* * If we can't get the iolock just skip truncating the blocks past EOF * because we could deadlock with the mmap_lock otherwise. We'll get * another chance to drop them once the last reference to the inode is * dropped, so we'll never leak blocks permanently. */ -if (inode->i_nlink &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) { +if (inode->i_nlink && + (file->f_mode &FMODE_WRITE ) && +xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) {if (xfs_can_free_eofblocks (ip) && !xfs_iflags_test (ip,XFS_IDIRTY_RELEASE )) {/*
Signature · 1 line
2.43.0
PATCH 6/9 xfs: only free posteof blocks on first close
In reply to Christoph HellwigFrom: "Darrick J. Wong" <[email protected]>
Certain workloads fragment files on XFS very badly, such as a software package that creates a number of threads, each of which repeatedly run the sequence: open a file, perform a synchronous write, and close the file, which defeats the speculative preallocation mechanism. We work around this problem by only deleting posteof blocks the /first/ time a file is closed to preserve the behavior that unpacking a tarball lays out files one after the other with no gaps.
--- fs/xfs/xfs_file.c | 32 +++++++++++--------------------- fs/xfs/xfs_inode.h | 4 ++-- 2 files changed, 13 insertions(+), 23 deletions(-)
fs/xfs/xfs_file.c +11 -21
@@ -1204,15 +1204,21 @@ xfs_file_release(* exposed to that problem. */ if (xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) { -xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE ); +xfs_iflags_clear (ip,XFS_EOFBLOCKS_RELEASED );if (ip->i_delayed_blks >0 )filemap_flush (inode->i_mapping); }/* * XFS aggressively preallocates post-EOF space to generate contiguous -* allocations for writers that append to the end of the file and we -* try to free these when an open file context is released. +* allocations for writers that append to the end of the file. +* +* To support workloads that close and reopen the file frequently, these +* preallocations usually persist after a close unless it is the first +* close for the inode. This is a tradeoff to generate tightly packed +* data layouts for unpacking tarballs or similar archives that write +* one file after another without going back to it while keeping the +* preallocation for files that have recurring open/write/close cycles. * * There is no point in freeing blocks here for open but unlinked files * as they will be taken care of by the inactivation path soon.
@@ -1230,25 +1236,9 @@ xfs_file_release( (file->f_mode & FMODE_WRITE) && xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {if (xfs_can_free_eofblocks (ip) && - !xfs_iflags_test (ip,XFS_IDIRTY_RELEASE )) { -/* -* Check if the inode is being opened, written and -* closed frequently and we have delayed allocation -* blocks outstanding (e.g. streaming writes from the -* NFS server), truncating the blocks past EOF will -* cause fragmentation to occur. -* -* In this case don't do the truncation, but we have to -* be careful how we detect this case. Blocks beyond EOF -* show up as i_delayed_blks even when the inode is -* clean, so we need to truncate them away first before -* checking for a dirty release. Hence on the first -* dirty close we will still remove the speculative -* allocation, but after that we will leave it in place. -*/ + !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED )) {xfs_free_eofblocks (ip); -if (ip->i_delayed_blks) -xfs_iflags_set (ip,XFS_IDIRTY_RELEASE ); +xfs_iflags_set (ip,XFS_EOFBLOCKS_RELEASED ); }xfs_iunlock (ip,XFS_IOLOCK_EXCL ); }
fs/xfs/xfs_inode.h +2 -2
@@ -335,7 +335,7 @@ static inline bool xfs_inode_has_bigrtalloc(struct xfs_inode *ip)#define XFS_INEW (1 << 3)/* inode has just been allocated */ #define XFS_IPRESERVE_DM_FIELDS (1 << 4)/* has legacy DMAPI fields set */ #define XFS_ITRUNCATED (1 << 5)/* truncated down so flush-on-close */ -#define XFS_IDIRTY_RELEASE (1 << 6)/* dirty release already seen */ +#define XFS_EOFBLOCKS_RELEASED (1 << 6)/* eofblocks were freed in ->release */ #define XFS_IFLUSHING (1 << 7)/* inode is being flushed */ #define __XFS_IPINNED_BIT 8/* wakeup key for zero pin count */ #define XFS_IPINNED (1 << __XFS_IPINNED_BIT)
@@ -382,7 +382,7 @@ static inline bool xfs_inode_has_bigrtalloc(struct xfs_inode *ip)*/ #define XFS_IRECLAIM_RESET_FLAGS \ (XFS_IRECLAIMABLE | XFS_IRECLAIM | \ - XFS_IDIRTY_RELEASE | XFS_ITRUNCATED | XFS_NEED_INACTIVE | \ + XFS_EOFBLOCKS_RELEASED | XFS_ITRUNCATED | XFS_NEED_INACTIVE | \ XFS_INACTIVATING | XFS_IQUOTAUNCHECKED)/*
Signature · 1 line
2.43.0
Re: [PATCH 6/9] xfs: only free posteof blocks on first close
Quoted 24 lines · Christoph Hellwig
From: "Darrick J. Wong" <[email protected]>
Certain workloads fragment files on XFS very badly, such as a software package that creates a number of threads, each of which repeatedly run the sequence: open a file, perform a synchronous write, and close the file, which defeats the speculative preallocation mechanism. We work around this problem by only deleting posteof blocks the /first/ time a file is closed to preserve the behavior that unpacking a tarball lays out files one after the other with no gaps.
Signed-off-by Darrick J. Wong <[email protected]>[hch: rebased, updated comment, renamed the flag]Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_file.c | 32 +++++++++++--------------------- fs/xfs/xfs_inode.h | 4 ++-- 2 files changed, 13 insertions(+), 23 deletions(-)fs/xfs/xfs_file.c +0
-0
fs/xfs/xfs_file.c xfs_file_release( · line 1210
Quoted 8 lines
fs/xfs/xfs_file.c +1
-1@@ -1204,15 +1204,21 @@ xfs_file_release(* exposed to that problem. */ if (xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) { -xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE ); +xfs_iflags_clear (ip,XFS_EOFBLOCKS_RELEASED );if (ip->i_delayed_blks >0 )filemap_flush (inode->i_mapping); }
This should probably be open coded to minimise lock cycles and lock contention on the flags lock when concurrent open/sync write/close cycles are run on the file (as recently reported by Mateusz). i.e:
if (ip->i_flags & XFS_ITRUNCATED) {
spin_lock(&ip->i_flags_lock);
if (ip->i_flags & XFS_ITRUNCATED)
ip->i_flags &= ~(XFS_ITRUNCATED | XFS_EOFBLOCKS_RELEASED);
spin_unlock(&ip->i_flags_lock);
if (ip->i_delayed_blks > 0)
filemap_flush(inode->i_mapping);
}....
Quoted 15 lines
fs/xfs/xfs_file.c +0
-11@@ -1230,25 +1236,9 @@ xfs_file_release( (file->f_mode & FMODE_WRITE) && xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { if (xfs_can_free_eofblocks(ip) && - !xfs_iflags_test (ip,XFS_IDIRTY_RELEASE )) { -/* -* Check if the inode is being opened, written and -* closed frequently and we have delayed allocation -* blocks outstanding (e.g. streaming writes from the -* NFS server), truncating the blocks past EOF will -* cause fragmentation to occur. -* -* In this case don't do the truncation, but we have to -* be careful how we detect this case. Blocks beyond EOF -* show up as i_delayed_blks even when the inode is
fs/xfs/xfs_file.c xfs_file_release( · line 1241
Quoted 10 lines
fs/xfs/xfs_file.c +2
-7-* clean, so we need to truncate them away first before -* checking for a dirty release. Hence on the first -* dirty close we will still remove the speculative -* allocation, but after that we will leave it in place. -*/ + !xfs_iflags_test(ip, XFS_EOFBLOCKS_RELEASED)) {xfs_free_eofblocks (ip); -if (ip->i_delayed_blks) -xfs_iflags_set (ip,XFS_IDIRTY_RELEASE ); +xfs_iflags_set (ip,XFS_EOFBLOCKS_RELEASED );
!xfs_iflags_test_and_set(ip, XFS_EOFBLOCKS_RELEASED) xfs_free_eofblocks(ip);
This also avoids an extra lock cycle to set the flag....
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 6/9] xfs: only free posteof blocks on first close
Quoted 9 lines · Dave Chinner
Quoted 9 lines
fs/xfs/xfs_file.c +0
-1@@ -1204,15 +1204,21 @@ xfs_file_release(* exposed to that problem. */ if (xfs_iflags_test_and_clear (ip,XFS_ITRUNCATED )) { -xfs_iflags_clear (ip,XFS_IDIRTY_RELEASE );
Quoted 8 lines
fs/xfs/xfs_file.c xfs_file_release( · line 1210
Quoted 4 lines
fs/xfs/xfs_file.c +1
-0+xfs_iflags_clear (ip,XFS_EOFBLOCKS_RELEASED );if (ip->i_delayed_blks >0 )filemap_flush (inode->i_mapping); }This should probably be open coded to minimise lock cycles and lock contention on the flags lock when concurrent open/sync write/close cycles are run on the file (as recently reported by Mateusz). i.e:
Let's do that as a separate patch on top of the series, as that is unrelated.
PATCH 7/9 xfs: check XFS_EOFBLOCKS_RELEASED earlier in xfs_release_eofblocks
In reply to Christoph HellwigIf the XFS_EOFBLOCKS_RELEASED flag is set, we are not going to free the eofblocks, so don't bother locking the inode or performing the checks in xfs_can_free_eofblocks.
--- fs/xfs/xfs_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/xfs/xfs_file.c +2 -2
@@ -1234,9 +1234,9 @@ xfs_file_release(*/ if (inode->i_nlink && (file->f_mode &FMODE_WRITE ) && + !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED ) &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) { -if (xfs_can_free_eofblocks (ip) && - !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED )) { +if (xfs_can_free_eofblocks (ip)) {xfs_free_eofblocks (ip);xfs_iflags_set (ip,XFS_EOFBLOCKS_RELEASED ); }
Signature · 1 line
2.43.0
Re: [PATCH 7/9] xfs: check XFS_EOFBLOCKS_RELEASED earlier in xfs_release_eofblocks
Quoted 16 lines · Christoph Hellwig
If the
XFS_EOFBLOCKS_RELEASEDflag is set, we are not going to free the eofblocks, so don't bother locking the inode or performing the checks inxfs_can_free_eofblocks.Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)fs/xfs/xfs_file.c +0
-0@@ -1234,9 +1234,9 @@ xfs_file_release(*/
fs/xfs/xfs_file.c xfs_file_release( · line 1242
Quoted 10 lines
fs/xfs/xfs_file.c +2
-2if (inode->i_nlink && (file->f_mode &FMODE_WRITE ) && + !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED ) &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) { -if (xfs_can_free_eofblocks (ip) && - !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED )) { +if (xfs_can_free_eofblocks (ip)) {xfs_free_eofblocks (ip);xfs_iflags_set (ip,XFS_EOFBLOCKS_RELEASED ); }
The test and set here is racy. A long time can pass between the test and the setting of the flag, so maybe this should be optimised to something like:
if (inode->i_nlink &&
(file->f_mode & FMODE_WRITE) &&
(!(ip->i_flags & XFS_EOFBLOCKS_RELEASED)) &&
xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
if (xfs_can_free_eofblocks(ip) &&
!xfs_iflags_test_and_set(ip, XFS_EOFBLOCKS_RELEASED))
xfs_free_eofblocks(ip);
xfs_iunlock(ip, XFS_IOLOCK_EXCL);
}I do wonder, though - why do we need to hold the IOLOCK to call xfs_can_free_eofblocks()? The only thing that really needs serialisation is the xfS_bmapi_read() call, and that's done under the ILOCK not the IOLOCK. Sure, xfs_free_eofblocks() needs the IOLOCK because it's effectively a truncate w.r.t. extending writes, but races with extending writes while checking if we need to do that operation aren't really a big deal. Worst case is we take the lock and free the EOF blocks beyond the writes we raced with.
What am I missing here?
i.e. it seems to me that the logic here could be:
if (inode->i_nlink &&
(file->f_mode & FMODE_WRITE) &&
(!(ip->i_flags & XFS_EOFBLOCKS_RELEASED)) &&
xfs_can_free_eofblocks(ip) &&
xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL) &&
!xfs_iflags_test_and_set(ip, XFS_EOFBLOCKS_RELEASED)) {
xfs_free_eofblocks(ip);
xfs_iunlock(ip, XFS_IOLOCK_EXCL);
}And so avoids attempting to take or taking locks in all the cases where locks can be avoided.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 7/9] xfs: check XFS_EOFBLOCKS_RELEASED earlier in xfs_release_eofblocks
Quoted 2 lines · Dave Chinner
The test and set here is racy. A long time can pass between the test and the setting of the flag,
The race window is much tighter due to the iolock, but if we really care about the race here, the right fix for that is to keep a second check for the XFS_EOFBLOCKS_RELEASED flag inside the iolock.
Quoted 12 lines
so maybe this should be optimised to
something like:if (inode->i_nlink && (file->f_mode & FMODE_WRITE) && (!(ip->i_flags & XFS_EOFBLOCKS_RELEASED)) && xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { if (xfs_can_free_eofblocks(ip) && !xfs_iflags_test_and_set(ip, XFS_EOFBLOCKS_RELEASED)) xfs_free_eofblocks(ip); xfs_iunlock(ip, XFS_IOLOCK_EXCL); }
All these direct i_flags access actually are racy too (at least in theory). We'd probably be better off moving those over to the atomic bitops and only using i_lock for any coordination beyond the actual flags. I'd rather not get into that here for now, even if it is a worthwhile project for later.
Quoted 10 lines
I do wonder, though - why do we need to hold the IOLOCK to call
xfs_can_free_eofblocks()? The only thing that really needs serialisation is thexfS_bmapi_read()call, and that's done under the ILOCK not the IOLOCK. Sure,xfs_free_eofblocks()needs the IOLOCK because it's effectively a truncate w.r.t. extending writes, but races with extending writes while checking if we need to do that operation aren't really a big deal. Worst case is we take the lock and free the EOF blocks beyond the writes we raced with.What am I missing here?
I think the prime part of the story is that xfs_can_free_eofblocks was split out of xfs_free_eofblocks, which requires the iolock. But I'm not sure if some of the checks are a little racy without the iolock, although I doubt it matter in practice as they are all optimizations. I'd need to take a deeper look at this, so maybe it's worth a follow on together with the changes in i_flags handling.
Re: [PATCH 7/9] xfs: check XFS_EOFBLOCKS_RELEASED earlier in xfs_release_eofblocks
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Dave Chinner
The test and set here is racy. A long time can pass between the test and the setting of the flag,
The race window is much tighter due to the iolock, but if we really care about the race here, the right fix for that is to keep a second check for the
XFS_EOFBLOCKS_RELEASEDflag inside the iolock.
Right, that's exactly what the code I proposed below does.
Quoted 13 lines
Quoted 12 lines
so maybe this should be optimised to
something like:if (inode->i_nlink && (file->f_mode & FMODE_WRITE) && (!(ip->i_flags & XFS_EOFBLOCKS_RELEASED)) && xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { if (xfs_can_free_eofblocks(ip) && !xfs_iflags_test_and_set(ip, XFS_EOFBLOCKS_RELEASED)) xfs_free_eofblocks(ip); xfs_iunlock(ip, XFS_IOLOCK_EXCL); }
Quoted 2 lines
All these direct
i_flagsaccess actually are racy too (at least in theory).
Yes, but we really don't care about racing against the bit being set. The flag never gets cleared unless a truncate down occurs, so we don't really have to care about racing with that case - there will be no eofblocks to free.
If the test races with another release call setting the flag (i.e. we see it clear) then we are going to go the slow way and then do exactly the right thing according to the current bit state once we hold the IO lock and the i_flags_lock.
Quoted 4 lines
We'd probably be better off moving those over to the atomic bitops and only using
i_lockfor any coordination beyond the actual flags. I'd rather not get into that here for now, even if it is a worthwhile project for later.
That doesn't solve the exclusive cacheline access problem Mateusz reported. It allows us to isolate the bitop updates, but in this case here the atomic test-and-set op still requires exclusive cacheline access.
Hence we'd still need test-test-and-set optimisations here to avoid the exclusive cacheline contention when the bit is already set...
Quoted 9 lines
Quoted 9 lines
I do wonder, though - why do we need to hold the IOLOCK to call
xfs_can_free_eofblocks()? The only thing that really needs serialisation is thexfS_bmapi_read()call, and that's done under the ILOCK not the IOLOCK. Sure,xfs_free_eofblocks()needs the IOLOCK because it's effectively a truncate w.r.t. extending writes, but races with extending writes while checking if we need to do that operation aren't really a big deal. Worst case is we take the lock and free the EOF blocks beyond the writes we raced with.
Quoted 5 lines
Quoted 1 line
What am I missing here?
I think the prime part of the story is that
xfs_can_free_eofblockswas split out ofxfs_free_eofblocks, which requires the iolock. But I'm not sure if some of the checks are a little racy without the iolock,
Ok. I think the checks are racy even with the iolock - most of the checks are for inode metadata that is modified under the ilock (e.g. i_diflags, i_delayed_blks) or the ip->i_flags_lock (e.g. VFS_I(ip)->i_size for serialisation with updates via xfs_dio_write_end_io()). Hence I don't think that holding the IO lock here makes any difference here at all...
Quoted 3 lines
although I doubt it matter in practice as they are all optimizations. I'd need to take a deeper look at this, so maybe it's worth a follow on together with the changes in
i_flagshandling.
*nod*
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH 8/9 xfs: simplify extent lookup in xfs_can_free_eofblocks
In reply to Christoph Hellwigxfs_can_free_eofblocks just cares if there is an extent beyond EOF. Replace the call to xfs_bmapi_read with a xfs_iext_lookup_extent as we've already checked that extents are read in earlier.
--- fs/xfs/xfs_bmap_util.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-)
fs/xfs/xfs_bmap_util.c +7 -15
@@ -492,12 +492,12 @@ boolxfs_can_free_eofblocks ( struct xfs_inode *ip) { -struct xfs_bmbt_irec imap;struct xfs_mount *mp = ip->i_mount; +bool found_blocks = false;xfs_fileoff_t end_fsb;xfs_fileoff_t last_fsb; -int nimaps =1 ; -int error; +struct xfs_bmbt_irec imap; +struct xfs_iext_cursor icur;/* * Caller must either hold the exclusive io lock; or be inactivating
@@ -544,21 +544,13 @@ xfs_can_free_eofblocks(return false;/* -* Look up the mapping for the first block past EOF. If we can't find -* it, there's nothing to free. +* Check if there is an post-EOF extent to free. */ xfs_ilock (ip,XFS_ILOCK_SHARED ); - error =xfs_bmapi_read (ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps, -0 ); +if (xfs_iext_lookup_extent (ip, &ip->i_df, end_fsb, &icur, &imap)) + found_blocks = true;xfs_iunlock (ip,XFS_ILOCK_SHARED ); -if (error || nimaps ==0 ) -return false; - -/* -* If there's a real mapping there or there are delayed allocation -* reservations, then we have post-EOF blocks to try to free. -*/ -return imap.br_startblock !=HOLESTARTBLOCK || ip->i_delayed_blks; +return found_blocks; }/*
Signature · 1 line
2.43.0
PATCH 9/9 xfs: reclaim speculative preallocations for append only files
In reply to Christoph HellwigThe XFS XFS_DIFLAG_APPEND maps to the VFS S_APPEND flag, which forbids writes that don't append at the current EOF.
But the commit originally adding XFS_DIFLAG_APPEND support (commit a23321e766d in xfs xfs-import repository) also checked it to skip releasing speculative preallocations, which doesn't make any sense.
Another commit (dd9f438e3290 in the xfs-import repository) later extended that flag to also report these speculation preallocations which should not exist in getbmap.
Remove these checks as nothing XFS_DIFLAG_APPEND implies that preallocations beyond EOF should exist, but explicitly check for XFS_DIFLAG_APPEND in xfs_file_release to bypass the algorithm that discard preallocations on the first close as append only files aren't expected to be written to only once.
--- fs/xfs/xfs_bmap_util.c | 12 +++++------- fs/xfs/xfs_file.c | 4 ++++ fs/xfs/xfs_icache.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)
fs/xfs/xfs_bmap_util.c +5 -7
@@ -331,8 +331,7 @@ xfs_getbmap( }if (xfs_get_extsz_hint (ip) || - (ip->i_diflags & - (XFS_DIFLAG_PREALLOC |XFS_DIFLAG_APPEND ))) + (ip->i_diflags &XFS_DIFLAG_PREALLOC )) max_len = mp->m_super->s_maxbytes;else max_len =XFS_ISIZE (ip);
@@ -524,12 +523,11 @@ xfs_can_free_eofblocks(return false;/* -* Only free real extents for inodes with persistent preallocations or -* the append-only flag. +* Do not free real extents in preallocated files unless the file has +* delalloc blocks and we are forced to remove them. */ -if (ip->i_diflags & (XFS_DIFLAG_PREALLOC |XFS_DIFLAG_APPEND )) -if (ip->i_delayed_blks ==0 ) -return false; +if ((ip->i_diflags &XFS_DIFLAG_PREALLOC ) && !ip->i_delayed_blks) +return false;/* * Do not try to free post-EOF blocks if EOF is beyond the end of the
fs/xfs/xfs_file.c +4 -0
@@ -1220,6 +1220,9 @@ xfs_file_release(* one file after another without going back to it while keeping the * preallocation for files that have recurring open/write/close cycles. * +* This heuristic is skipped for inodes with the append-only flag as +* that flag is rather pointless for inodes written only once. +* * There is no point in freeing blocks here for open but unlinked files * as they will be taken care of by the inactivation path soon. *
@@ -1234,6 +1237,7 @@ xfs_file_release(*/ if (inode->i_nlink && (file->f_mode &FMODE_WRITE ) && + !(ip->i_diflags &XFS_DIFLAG_APPEND ) && !xfs_iflags_test (ip,XFS_EOFBLOCKS_RELEASED ) &&xfs_ilock_nowait (ip,XFS_IOLOCK_EXCL )) {if (xfs_can_free_eofblocks (ip)) {
fs/xfs/xfs_icache.c +1 -1
@@ -1159,7 +1159,7 @@ xfs_inode_free_eofblocks(if (xfs_can_free_eofblocks (ip))return xfs_free_eofblocks (ip); -/* inode could be preallocated or append-only */ +/* inode could be preallocated */ trace_xfs_inode_free_eofblocks_invalid (ip);xfs_inode_clear_eofblocks_tag (ip);return 0 ;
Signature · 1 line
2.43.0