xfs: Fix races between readahead and hole punching
Subsystems (6)
Jan Kara
reviewerv2 cover xfs: Fix races between readahead and hole punching
Hello,
this is a patch series that addresses a possible race between readahead and hole punching Amir has discovered [1]. The first patch makes madvise(2) to handle readahead requests through fadvise infrastructure, the third patch then adds necessary locking to XFS to protect against the race. Note that other filesystems need similar protections but e.g. in case of ext4 it isn't so simple without seriously regressing mixed rw workload performance so I'm pushing just xfs fix at this moment which is simple.
Changes since v1 (posted at [2]):
* Added reviewed-by tags
* Fixed indentation in xfs_file_fadvise()
* Improved comment and readibility of xfs_file_fadvise()
Honza
[1] https://lore.kernel.org/linux-fsdevel/CAOQ4uxjQNmxqmtA_VbYW0Su9rKRk2zobJmahcyeaEVOFKVQ5dw@mail.gmail.com/
[2] https://lore.kernel.org/linux-fsdevel/[email protected]/
Jan Kara
reviewerPATCH v2 1/3 mm: Handle MADV_WILLNEED through vfs_fadvise()
Currently handling of MADV_WILLNEED hint calls directly into readahead code. Handle it by calling vfs_fadvise() instead so that filesystem can use its ->fadvise() callback to acquire necessary locks or otherwise prepare for the request.
--- mm/madvise.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
mm/madvise.c +16 -6
@@ -14,6 +14,7 @@#include <linux/userfaultfd_k.h> #include <linux/hugetlb.h> #include <linux/falloc.h> +#include <linux/fadvise.h> #include <linux/sched.h> #include <linux/ksm.h> #include <linux/fs.h>
@@ -275,6 +276,7 @@ static long madvise_willneed(struct vm_area_struct *vma, unsigned long start, unsigned long end) {struct file *file = vma->vm_file; +loff_t offset; *prev = vma;#ifdef CONFIG_SWAP
@@ -298,12 +300,20 @@ static long madvise_willneed(struct vm_area_struct *vma,return 0 ; } - start = ((start - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; -if (end > vma->vm_end) - end = vma->vm_end; - end = ((end - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; - -force_page_cache_readahead (file->f_mapping, file, start, end - start); +/* +* Filesystem's fadvise may need to take various locks. We need to +* explicitly grab a reference because the vma (and hence the +* vma's reference to the file) can go away as soon as we drop +* mmap_sem. +*/ + *prev =NULL ;/* tell sys_madvise we drop mmap_sem */ +get_file (file); +up_read (¤t->mm->mmap_sem); + offset = (loff_t)(start - vma->vm_start) + + ((loff_t )vma->vm_pgoff <<PAGE_SHIFT ); +vfs_fadvise (file, offset, end - start,POSIX_FADV_WILLNEED ); +fput (file); +down_read (¤t->mm->mmap_sem);return 0 ; }
Signature · 1 line
2.16.4
Re: [PATCH 1/3] mm: Handle MADV_WILLNEED through vfs_fadvise()
Quoted 9 lines · Jan Kara
Currently handling of
MADV_WILLNEEDhint calls directly into readahead code. Handle it by callingvfs_fadvise()instead so that filesystem can use its ->fadvise() callback to acquire necessary locks or otherwise prepare for the request.Suggested-by Amir Goldstein <[email protected]>Reviewed-by Boaz Harrosh <[email protected]>Signed-off-by Jan Kara <[email protected]>
Looks ok,
--D
Quoted 54 lines
--- mm/madvise.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)mm/madvise.c +16
-6@@ -14,6 +14,7 @@#include <linux/userfaultfd_k.h> #include <linux/hugetlb.h> #include <linux/falloc.h> +#include <linux/fadvise.h> #include <linux/sched.h> #include <linux/ksm.h> #include <linux/fs.h> @@ -275,6 +276,7 @@ static long madvise_willneed(struct vm_area_struct *vma, unsigned long start, unsigned long end) {struct file *file = vma->vm_file; +loff_t offset; *prev = vma;#ifdef CONFIG_SWAP @@ -298,12 +300,20 @@ static long madvise_willneed(struct vm_area_struct *vma,return 0 ; } - start = ((start - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; -if (end > vma->vm_end) - end = vma->vm_end; - end = ((end - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; - -force_page_cache_readahead (file->f_mapping, file, start, end - start); +/* +* Filesystem's fadvise may need to take various locks. We need to +* explicitly grab a reference because the vma (and hence the +* vma's reference to the file) can go away as soon as we drop +* mmap_sem. +*/ + *prev =NULL ;/* tell sys_madvise we drop mmap_sem */ +get_file (file); +up_read (¤t->mm->mmap_sem); + offset = (loff_t)(start - vma->vm_start) + + ((loff_t )vma->vm_pgoff <<PAGE_SHIFT ); +vfs_fadvise (file, offset, end - start,POSIX_FADV_WILLNEED ); +fput (file); +down_read (¤t->mm->mmap_sem);return 0 ; }Signature · 1 line
2.16.4
Jan Kara
reviewerPATCH v2 2/3 fs: Export generic_fadvise()
In reply to Jan KaraFilesystems will need to call this function from their fadvise handlers.
--- include/linux/fs.h | 2 ++ mm/fadvise.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-)
include/linux/fs.h +2 -0
@@ -3531,6 +3531,8 @@ extern void inode_nohighmem(struct inode *inode);/* mm/fadvise.c */ extern int vfs_fadvise (struct file *file,loff_t offset,loff_t len,int advice); +extern int generic_fadvise (struct file *file,loff_t offset,loff_t len, +int advice);#if defined(CONFIG_IO_URING )extern struct sock *io_uring_get_socket (struct file *file);
mm/fadvise.c +2 -2
@@ -27,8 +27,7 @@* deactivate the pages and clear PG_Referenced. */ -static int generic_fadvise (struct file *file,loff_t offset,loff_t len, -int advice) +int generic_fadvise (struct file *file,loff_t offset,loff_t len,int advice) {struct inode *inode;struct address_space *mapping;
@@ -178,6 +177,7 @@ static int generic_fadvise(struct file *file, loff_t offset, loff_t len, }return 0 ; } +EXPORT_SYMBOL (generic_fadvise);int vfs_fadvise (struct file *file,loff_t offset,loff_t len,int advice) {
Signature · 1 line
2.16.4
Jan Kara
reviewerPATCH v2 3/3 xfs: Fix stale data exposure when readahead races with hole punch
In reply to Jan KaraHole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both XFS_IOLOCK_EXCL and XFS_MMAPLOCK_EXCL which provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered by fadvise() or madvise() from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache in xfs_flush_unmap_range() but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.
Fix the problem by protecting handling of readahead requests by XFS_IOLOCK_SHARED similarly as we protect reads.
--- fs/xfs/xfs_file.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
fs/xfs/xfs_file.c +26 -0
@@ -28,6 +28,7 @@#include <linux/falloc.h> #include <linux/backing-dev.h> #include <linux/mman.h> +#include <linux/fadvise.h> static const struct vm_operations_struct xfs_file_vm_ops;
@@ -933,6 +934,30 @@ xfs_file_fallocate(return error; } +STATIC int +xfs_file_fadvise ( +struct file *file, +loff_t start, +loff_t end, +int advice) +{ +struct xfs_inode *ip =XFS_I (file_inode (file)); +int ret; +int lockflags =0 ; + +/* +* Operations creating pages in page cache need protection from hole +* punching and similar ops +*/ +if (advice ==POSIX_FADV_WILLNEED ) { + lockflags =XFS_IOLOCK_SHARED ; +xfs_ilock (ip, lockflags); + } + ret =generic_fadvise (file, start, end, advice); +if (lockflags) +xfs_iunlock (ip, lockflags); +return ret; +} STATIC loff_t xfs_file_remap_range(
@@ -1232,6 +1257,7 @@ const struct file_operations xfs_file_operations = { .fsync = xfs_file_fsync, .get_unmapped_area = thp_get_unmapped_area, .fallocate = xfs_file_fallocate, + .fadvise = xfs_file_fadvise, .remap_file_range = xfs_file_remap_range, };
Signature · 1 line
2.16.4
Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch
Quoted 11 lines · Jan Kara
Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both
XFS_IOLOCK_EXCLandXFS_MMAPLOCK_EXCLwhich provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered byfadvise()ormadvise()from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache inxfs_flush_unmap_range()but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.
Quoted 7 lines
Fix the problem by protecting handling of readahead requests by
XFS_IOLOCK_SHAREDsimilarly as we protect reads.
Is there a test on xfstests to demonstrate this race?
Will test it out though...
--D
Quoted 58 lines
--- fs/xfs/xfs_file.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)fs/xfs/xfs_file.c +26
-0@@ -28,6 +28,7 @@#include <linux/falloc.h> #include <linux/backing-dev.h> #include <linux/mman.h> +#include <linux/fadvise.h> static const struct vm_operations_struct xfs_file_vm_ops;@@ -933,6 +934,30 @@ xfs_file_fallocate(return error; } +STATIC int +xfs_file_fadvise ( +struct file *file, +loff_t start, +loff_t end, +int advice) +{ +struct xfs_inode *ip =XFS_I (file_inode (file)); +int ret; +int lockflags =0 ; + +/* +* Operations creating pages in page cache need protection from hole +* punching and similar ops +*/ +if (advice ==POSIX_FADV_WILLNEED ) { + lockflags =XFS_IOLOCK_SHARED ; +xfs_ilock (ip, lockflags); + } + ret =generic_fadvise (file, start, end, advice); +if (lockflags) +xfs_iunlock (ip, lockflags); +return ret; +} STATIC loff_t xfs_file_remap_range(@@ -1232,6 +1257,7 @@ const struct file_operations xfs_file_operations = { .fsync = xfs_file_fsync, .get_unmapped_area = thp_get_unmapped_area, .fallocate = xfs_file_fallocate, + .fadvise = xfs_file_fadvise, .remap_file_range = xfs_file_remap_range, };Signature · 1 line
2.16.4
Jan Kara
reviewerRe: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch
Quoted 15 lines · Darrick J. Wong
Quoted 14 lines · Jan Kara
Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both
XFS_IOLOCK_EXCLandXFS_MMAPLOCK_EXCLwhich provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered byfadvise()ormadvise()from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache inxfs_flush_unmap_range()but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.Fix the problem by protecting handling of readahead requests by
XFS_IOLOCK_SHAREDsimilarly as we protect reads.
Quoted 6 lines
Quoted 4 lines
Is there a test on xfstests to demonstrate this race?
No, but I can try to create one.
Quoted 3 lines
Will test it out though...
Reviewed-by Darrick J. Wong <[email protected]>
Thanks. BTW, will you pick up these patches please?
Honza
Quoted 61 lines
--D
Quoted 58 lines
--- fs/xfs/xfs_file.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)fs/xfs/xfs_file.c +26
-0@@ -28,6 +28,7 @@#include <linux/falloc.h> #include <linux/backing-dev.h> #include <linux/mman.h> +#include <linux/fadvise.h> static const struct vm_operations_struct xfs_file_vm_ops;@@ -933,6 +934,30 @@ xfs_file_fallocate(return error; } +STATIC int +xfs_file_fadvise ( +struct file *file, +loff_t start, +loff_t end, +int advice) +{ +struct xfs_inode *ip =XFS_I (file_inode (file)); +int ret; +int lockflags =0 ; + +/* +* Operations creating pages in page cache need protection from hole +* punching and similar ops +*/ +if (advice ==POSIX_FADV_WILLNEED ) { + lockflags =XFS_IOLOCK_SHARED ; +xfs_ilock (ip, lockflags); + } + ret =generic_fadvise (file, start, end, advice); +if (lockflags) +xfs_iunlock (ip, lockflags); +return ret; +} STATIC loff_t xfs_file_remap_range(@@ -1232,6 +1257,7 @@ const struct file_operations xfs_file_operations = { .fsync = xfs_file_fsync, .get_unmapped_area = thp_get_unmapped_area, .fallocate = xfs_file_fallocate, + .fadvise = xfs_file_fadvise, .remap_file_range = xfs_file_remap_range, };Signature · 1 line
2.16.4
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch
Quoted 16 lines · Jan Kara
Quoted 15 lines · Darrick J. Wong
Quoted 14 lines · Jan Kara
Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both
XFS_IOLOCK_EXCLandXFS_MMAPLOCK_EXCLwhich provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered byfadvise()ormadvise()from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache inxfs_flush_unmap_range()but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.Fix the problem by protecting handling of readahead requests by
XFS_IOLOCK_SHAREDsimilarly as we protect reads.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Is there a test on xfstests to demonstrate this race?
No, but I can try to create one.
<nod> I imgaine this race was hard to spot in the first place...
Quoted 5 lines
Quoted 3 lines
Will test it out though...
Reviewed-by Darrick J. Wong <[email protected]>Thanks. BTW, will you pick up these patches please?
Yeah, they looked fine.
--D
Quoted 67 lines
Honza
Quoted 62 lines
--D
Quoted 59 lines
--- fs/xfs/xfs_file.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)fs/xfs/xfs_file.c +25
-0@@ -28,6 +28,7 @@#include <linux/falloc.h> #include <linux/backing-dev.h> #include <linux/mman.h> +#include <linux/fadvise.h> static const struct vm_operations_struct xfs_file_vm_ops;@@ -933,6 +934,30 @@ xfs_file_fallocate(return error; } +STATIC int +xfs_file_fadvise ( +struct file *file, +loff_t start, +loff_t end, +int advice) +{ +struct xfs_inode *ip =XFS_I (file_inode (file)); +int ret; +int lockflags =0 ; + +/* +* Operations creating pages in page cache need protection from hole +* punching and similar ops +*/ +if (advice ==POSIX_FADV_WILLNEED ) { + lockflags =XFS_IOLOCK_SHARED ; +xfs_ilock (ip, lockflags); + } + ret =generic_fadvise (file, start, end, advice); +if (lockflags) +xfs_iunlock (ip, lockflags); +return ret; +}STATIC loff_txfs_file_remap_range(
fs/xfs/xfs_file.c +1
-0@@ -1232,6 +1257,7 @@ const struct file_operations xfs_file_operations = { .fsync = xfs_file_fsync, .get_unmapped_area = thp_get_unmapped_area, .fallocate = xfs_file_fallocate, + .fadvise = xfs_file_fadvise, .remap_file_range = xfs_file_remap_range, };Signature · 1 line
2.16.4Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerRe: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch
In reply to Jan KaraQuoted 16 lines · Jan Kara
Quoted 15 lines · Darrick J. Wong
Quoted 14 lines · Jan Kara
Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both
XFS_IOLOCK_EXCLandXFS_MMAPLOCK_EXCLwhich provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered byfadvise()ormadvise()from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache inxfs_flush_unmap_range()but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.Fix the problem by protecting handling of readahead requests by
XFS_IOLOCK_SHAREDsimilarly as we protect reads.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Is there a test on xfstests to demonstrate this race?
No, but I can try to create one.
I was experimenting with this but I could not reproduce the issue in my test VM without inserting artificial delay at appropriate place... So I don't think there's much point in the fstest for this.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch
Quoted 22 lines · Jan Kara
Quoted 21 lines · Jan Kara
Quoted 20 lines · Darrick J. Wong
Quoted 18 lines · Jan Kara
Hole puching currently evicts pages from page cache and then goes on to remove blocks from the inode. This happens under both
XFS_IOLOCK_EXCLandXFS_MMAPLOCK_EXCLwhich provides appropriate serialization with racing reads or page faults. However there is currently nothing that prevents readahead triggered byfadvise()ormadvise()from racing with the hole punch and instantiating page cache page after hole punching has evicted page cache inxfs_flush_unmap_range()but before it has removed blocks from the inode. This page cache page will be mapping soon to be freed block and that can lead to returning stale data to userspace or even filesystem corruption.Fix the problem by protecting handling of readahead requests by
XFS_IOLOCK_SHAREDsimilarly as we protect reads.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Is there a test on xfstests to demonstrate this race?
No, but I can try to create one.
I was experimenting with this but I could not reproduce the issue in my test VM without inserting artificial delay at appropriate place... So I don't think there's much point in the fstest for this.
<shrug> We've added debugging knobs to XFS that inject delays to demonstrate race conditions that are hard to reproduce, but OTOH it's more fun to have a generic/ test that you can use to convince the other fs maintainers to take your patches. :)
--D
Quoted 5 lines
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch
In reply to Jan KaraOn 18/09/2019 15:31, Jan Kara wrote:
<>
Quoted 10 lines
Quoted 3 lines
Quoted 1 line
Is there a test on xfstests to demonstrate this race?
No, but I can try to create one.
I was experimenting with this but I could not reproduce the issue in my test VM without inserting artificial delay at appropriate place... So I don't think there's much point in the fstest for this.
Honza
If I understand correctly you will need threads that direct-write files, then fadvise(WILL_NEED) - in parallel to truncate (punch_hole) these files - In parallel to trash caches.
(Direct-write is so data is not present in cache when you come to WILL_NEED
it into the cache, otherwise the xfs b-trees are not exercised. Or are you more worried about the page_cache races?
)
Also the d-writes might want to exercise multiple size extents + holes as well.
I have a very different system but its kind of the test we did for this problem.
The reason it is never hit is because fadvise(WILL_NEED) is never really used that much, and there are no applications that actually blindly truncate during IO, this is only us in testing that do this meaningless thing.
Thanks Jan again for working on this
Boaz
Jan Kara
reviewerRe: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch
Quoted 13 lines · Boaz Harrosh
On 18/09/2019 15:31, Jan Kara wrote:
<>Quoted 10 lines
Quoted 3 lines
Quoted 1 line
Is there a test on xfstests to demonstrate this race?
No, but I can try to create one.
I was experimenting with this but I could not reproduce the issue in my test VM without inserting artificial delay at appropriate place... So I don't think there's much point in the fstest for this.
Honza
Quoted 7 lines
If I understand correctly you will need threads that direct-write files, then
fadvise(WILL_NEED)- in parallel to truncate (punch_hole) these files - In parallel to trash caches.
(Direct-write is so data is not present in cache when you come toWILL_NEED
it into the cache, otherwise the xfs b-trees are not exercised. Or are you more worried about thepage_cacheraces?
)
What I was testing was:
Fill file with data.
One process does fadvise(WILLNEED) block by block from end of the file. Another process punches hole into the file.
If they race is the right way, following read will show old data instead of zeros. And as I said I'm able to hit this but only if I add artificial delay between truncating page cache and actually removing blocks.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 3/3] xfs: Fix stale data exposure when readahead races with hole punch
Quoted 22 lines · Jan Kara
Quoted 13 lines · Boaz Harrosh
On 18/09/2019 15:31, Jan Kara wrote:
<>Quoted 10 lines
Quoted 3 lines
Quoted 1 line
Is there a test on xfstests to demonstrate this race?
No, but I can try to create one.
I was experimenting with this but I could not reproduce the issue in my test VM without inserting artificial delay at appropriate place... So I don't think there's much point in the fstest for this.
Honza
Quoted 7 lines
If I understand correctly you will need threads that direct-write files, then
fadvise(WILL_NEED)- in parallel to truncate (punch_hole) these files - In parallel to trash caches.
(Direct-write is so data is not present in cache when you come toWILL_NEED
it into the cache, otherwise the xfs b-trees are not exercised. Or are you more worried about thepage_cacheraces?
)
Quoted 2 lines
What I was testing was:
Fill file with data.
But are you sure data is not in page cache after this stage?
Also this stage sould create multiple extents perhaps with gaps in between
Quoted 3 lines
One process does fadvise(WILLNEED) block by block from end of the file. Another process punches hole into the file.
(Perhaps randome placement that spans multiple extents in one go)
Quoted 4 lines
If they race is the right way, following read will show old data instead of zeros. And as I said I'm able to hit this but only if I add artificial delay between truncating page cache and actually removing blocks.
I was more afraid of iterating on a btree or xarray in parallel of it being destroyed / punched.
I think if you iterate backwards in the WILLNEED case the tree/xarry corruption is less likely
But now that I think about it. Maybe your case is very different to mine, because read_pages() in xfs does take the ilock. I'm not familiar with this code.
Quoted 2 lines
Honza
But I guess the window is very small then
Thanks
Boaz
Quoted 11 lines · Jan Kara
Hello,
this is a patch series that addresses a possible race between readahead and hole punching Amir has discovered [1]. The first patch makes madvise(2) to handle readahead requests through fadvise infrastructure, the third patch then adds necessary locking to XFS to protect against the race. Note that other filesystems need similar protections but e.g. in case of ext4 it isn't so simple without seriously regressing mixed rw workload performance so I'm pushing just xfs fix at this moment which is simple.
Jan,
Could you give a quick status update about the state of this issue for ext4 and other fs. I remember some solutions were discussed. Perhaps this could be a good topic for a cross track session in LSF/MM? Aren't the challenges posed by this race also relevant for RWF_UNCACHED?
Thanks,
Amir.
Quoted 14 lines · Amir Goldstein
Quoted 11 lines · Jan Kara
Hello,
this is a patch series that addresses a possible race between readahead and hole punching Amir has discovered [1]. The first patch makes madvise(2) to handle readahead requests through fadvise infrastructure, the third patch then adds necessary locking to XFS to protect against the race. Note that other filesystems need similar protections but e.g. in case of ext4 it isn't so simple without seriously regressing mixed rw workload performance so I'm pushing just xfs fix at this moment which is simple.
Quoted 7 lines
Jan,
Could you give a quick status update about the state of this issue for ext4 and other fs. I remember some solutions were discussed. Perhaps this could be a good topic for a cross track session in LSF/MM? Aren't the challenges posed by this race also relevant for
RWF_UNCACHED?
Maybe a silly question:
Can someone please explain to me why we even bother truncating pages on punch hole?
Wouldn't it solve the race if instead we zeroed those pages and marked them readonly?
The comment above trunacte_pagecache_range() says:
* This function should typically be called before the filesystem
* releases resources associated with the freed range (eg. deallocates
* blocks). This way, pagecache will always stay logically coherent
* with on-disk format, and the filesystem would not have to deal with
* situations such as writepage being called for a page that has already
* had its underlying blocks deallocated.
So in order to prevent writepage from being called on a punched hole, we need to make sure that page write fault will be called, which is the same state as if an exiting hole has been read into page cache but not written yet. Right? Wrong?
What am I missing here?
Thanks,
Amir.
Jan Kara
reviewerQuoted 23 lines · Amir Goldstein
Quoted 14 lines · Amir Goldstein
Quoted 11 lines · Jan Kara
Hello,
this is a patch series that addresses a possible race between readahead and hole punching Amir has discovered [1]. The first patch makes madvise(2) to handle readahead requests through fadvise infrastructure, the third patch then adds necessary locking to XFS to protect against the race. Note that other filesystems need similar protections but e.g. in case of ext4 it isn't so simple without seriously regressing mixed rw workload performance so I'm pushing just xfs fix at this moment which is simple.
Quoted 7 lines
Jan,
Could you give a quick status update about the state of this issue for ext4 and other fs. I remember some solutions were discussed. Perhaps this could be a good topic for a cross track session in LSF/MM? Aren't the challenges posed by this race also relevant for
RWF_UNCACHED?
Quoted 6 lines
Maybe a silly question:
Can someone please explain to me why we even bother truncating pages on punch hole?
Wouldn't it solve the race if instead we zeroed those pages and marked them readonly?
Not if we also didn't keep them locked. Page reclaim can reclaim clean unlocked pages any time it wants... Plus the CPU overhead of zeroing potentially large ranges of pages would be significant.
Quoted 8 lines
The comment above
trunacte_pagecache_range()says:
* This function should typically be called before the filesystem
* releases resources associated with the freed range (eg. deallocates
* blocks). This way, pagecache will always stay logically coherent
* with on-disk format, and the filesystem would not have to deal with
* situations such as writepage being called for a page that has already
* had its underlying blocks deallocated.
Quoted 4 lines
So in order to prevent writepage from being called on a punched hole, we need to make sure that page write fault will be called, which is the same state as if an exiting hole has been read into page cache but not written yet. Right? Wrong?
Also the writeback in the comment you mention above is just an example. As the race you've found shows, there is a problem with reading as well.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerHi Amir!
Quoted 13 lines · Amir Goldstein
Quoted 11 lines · Jan Kara
Hello,
this is a patch series that addresses a possible race between readahead and hole punching Amir has discovered [1]. The first patch makes madvise(2) to handle readahead requests through fadvise infrastructure, the third patch then adds necessary locking to XFS to protect against the race. Note that other filesystems need similar protections but e.g. in case of ext4 it isn't so simple without seriously regressing mixed rw workload performance so I'm pushing just xfs fix at this moment which is simple.
Quoted 2 lines
Could you give a quick status update about the state of this issue for ext4 and other fs. I remember some solutions were discussed.
Shortly: I didn't get to this. I'm sorry :-|. I'll bump up a priority but I can't promise anything at the moment.
Quoted 1 line
Perhaps this could be a good topic for a cross track session in LSF/MM?
Maybe although this is one of the cases where it's easy to chat about possible solutions but somewhat tedious to write one so I'm not sure how productive that would be. BTW my discussion with Kent [1] is in fact very related to this problem (the interval lock he has is to stop exactly races like this).
Quoted 1 line
Aren't the challenges posed by this race also relevant for
RWF_UNCACHED?
Do you have anything particular in mind? I don't see how RWF_UNCACHED would make this any better or worse than DIO / readahead...
Honza
[1] https://lore.kernel.org/linux-fsdevel/20191216193852.GA8664@kmo-pixel/
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 23 lines · Jan Kara
Hi Amir!
Quoted 13 lines · Amir Goldstein
Quoted 11 lines · Jan Kara
Hello,
this is a patch series that addresses a possible race between readahead and hole punching Amir has discovered [1]. The first patch makes madvise(2) to handle readahead requests through fadvise infrastructure, the third patch then adds necessary locking to XFS to protect against the race. Note that other filesystems need similar protections but e.g. in case of ext4 it isn't so simple without seriously regressing mixed rw workload performance so I'm pushing just xfs fix at this moment which is simple.
Quoted 2 lines
Could you give a quick status update about the state of this issue for ext4 and other fs. I remember some solutions were discussed.
Shortly: I didn't get to this. I'm sorry :-|. I'll bump up a priority but I can't promise anything at the moment.
Quoted 8 lines
Quoted 1 line
Perhaps this could be a good topic for a cross track session in LSF/MM?
Maybe although this is one of the cases where it's easy to chat about possible solutions but somewhat tedious to write one so I'm not sure how productive that would be. BTW my discussion with Kent [1] is in fact very related to this problem (the interval lock he has is to stop exactly races like this).
Well, I was mostly interested to know if there is an agreement on the way to solve the problem. If we need to discuss it to reach consensus than it might be a good topic for LSF/MM. If you already know what needs to be done, there is no need for a discussion.
Quoted 5 lines
Quoted 1 line
Aren't the challenges posed by this race also relevant for
RWF_UNCACHED?Do you have anything particular in mind? I don't see how
RWF_UNCACHEDwould make this any better or worse than DIO / readahead...
Not better nor worse. I meant that RFW_UNCACHED is another case that would suffer the same races.
Thanks,
Amir.
Jan Kara
reviewerQuoted 30 lines · Amir Goldstein
Quoted 20 lines · Jan Kara
Quoted 13 lines · Amir Goldstein
Quoted 11 lines · Jan Kara
Hello,
this is a patch series that addresses a possible race between readahead and hole punching Amir has discovered [1]. The first patch makes madvise(2) to handle readahead requests through fadvise infrastructure, the third patch then adds necessary locking to XFS to protect against the race. Note that other filesystems need similar protections but e.g. in case of ext4 it isn't so simple without seriously regressing mixed rw workload performance so I'm pushing just xfs fix at this moment which is simple.
Quoted 2 lines
Could you give a quick status update about the state of this issue for ext4 and other fs. I remember some solutions were discussed.
Shortly: I didn't get to this. I'm sorry :-|. I'll bump up a priority but I can't promise anything at the moment.
Quoted 8 lines
Quoted 1 line
Perhaps this could be a good topic for a cross track session in LSF/MM?
Maybe although this is one of the cases where it's easy to chat about possible solutions but somewhat tedious to write one so I'm not sure how productive that would be. BTW my discussion with Kent [1] is in fact very related to this problem (the interval lock he has is to stop exactly races like this).
Quoted 4 lines
Well, I was mostly interested to know if there is an agreement on the way to solve the problem. If we need to discuss it to reach consensus than it might be a good topic for LSF/MM. If you already know what needs to be done, there is no need for a discussion.
So I have an idea how it could be solved: Change calling convention for ->readpage() so that it gets called without page locked and take i_mmap_sem there (and in ->readpages()) to protect from the race. But I wanted to present it in the form of patches as the devil here is in the details and it may prove to be too ugly to be bearable.
If I won't get to writing the patches, you're right it may be sensible to present the idea to people at LSF/MM what they think about it.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Aren't the challenges posed by this race also relevant for
RWF_UNCACHED?Do you have anything particular in mind? I don't see how
RWF_UNCACHEDwould make this any better or worse than DIO / readahead...Not better nor worse. I meant that
RFW_UNCACHEDis another case that would suffer the same races.
Yes, that's right.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR