Properly invalidate data in the cleancache.
Subsystems (11)
cover Properly invalidate data in the cleancache.
We've noticed that after direct IO write, buffered read sometimes gets
stale data which is coming from the cleancache.
The reason for this is that some direct write hooks call call invalidate_inode_pages2[_range]() conditionally iff mapping->nrpages is not zero, so we may not invalidate
data in the cleancache.
Another odd thing is that we check only for ->nrpages and don't check for ->nrexceptional, but invalidate_inode_pages2[_range] also invalidates exceptional entries as well. So we invalidate exceptional entries only if ->nrpages != 0? This doesn't feel right.
- Patch 1 fixes direct IO writes by removing ->nrpages check.
- Patch 2 fixes similar case in invalidate_bdev().
Note: I only fixed conditional cleancache_invalidate_inode() here.
Do we also need to add ->nrexceptional check in into invalidate_bdev()?
- Patches 3-4: some optimizations.
Andrey Ryabinin (4):
fs: fix data invalidation in the cleancache during direct IO fs/block_dev: always invalidate cleancache in invalidate_bdev() mm/truncate: bail out early from invalidate_inode_pages2_range() if
mapping is empty
mm/truncate: avoid pointless cleancache_invalidate_inode() calls.
fs/9p/vfs_file.c | 2 +- fs/block_dev.c | 11 +++++------ fs/cifs/inode.c | 2 +- fs/dax.c | 2 +- fs/iomap.c | 16 +++++++--------- fs/nfs/direct.c | 6 ++---- fs/nfs/inode.c | 8 +++++--- mm/filemap.c | 26 +++++++++++--------------- mm/truncate.c | 13 +++++++++---- 9 files changed, 42 insertions(+), 44 deletions(-)
Signature · 1 line
2.10.2
PATCH 1/4 fs: fix data invalidation in the cleancache during direct IO
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff mapping->nrpages is not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.
Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
--- fs/9p/vfs_file.c | 2 +- fs/cifs/inode.c | 2 +- fs/dax.c | 2 +- fs/iomap.c | 16 +++++++--------- fs/nfs/direct.c | 6 ++---- fs/nfs/inode.c | 8 +++++--- mm/filemap.c | 26 +++++++++++--------------- 7 files changed, 28 insertions(+), 34 deletions(-)
fs/9p/vfs_file.c +1 -1
@@ -423,7 +423,7 @@ v9fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)unsigned long pg_start, pg_end; pg_start = origin >>PAGE_SHIFT ; pg_end = (origin + retval -1 ) >>PAGE_SHIFT ; -if (inode->i_mapping && inode->i_mapping->nrpages) +if (inode->i_mapping)invalidate_inode_pages2_range (inode->i_mapping, pg_start, pg_end); iocb->ki_pos += retval;
fs/cifs/inode.c +1 -1
@@ -1857,7 +1857,7 @@ cifs_invalidate_mapping(struct inode *inode) {int rc =0 ; -if (inode->i_mapping && inode->i_mapping->nrpages !=0 ) { +if (inode->i_mapping) { rc =invalidate_inode_pages2 (inode->i_mapping);if (rc) cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
fs/dax.c +1 -1
@@ -1047,7 +1047,7 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,* into page tables. We have to tear down these mappings so that data * written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if ((iomap->flags &IOMAP_F_NEW )) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );
fs/iomap.c +7 -9
@@ -880,16 +880,14 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, flags |=IOMAP_WRITE ; } -if (mapping->nrpages) { - ret =filemap_write_and_wait_range (mapping, start, end); -if (ret) - goto out_free_dio; + ret =filemap_write_and_wait_range (mapping, start, end); +if (ret) + goto out_free_dio; - ret =invalidate_inode_pages2_range (mapping, + ret =invalidate_inode_pages2_range (mapping, start >>PAGE_SHIFT , end >>PAGE_SHIFT ); -WARN_ON_ONCE (ret); - ret =0 ; - } +WARN_ON_ONCE (ret); + ret =0 ;inode_dio_begin (inode);
@@ -944,7 +942,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,* one is a pretty crazy thing to do, so we don't support it 100%. If * this invalidation fails, tough, the write still worked... */ -if (iov_iter_rw (iter) ==WRITE && mapping->nrpages) { +if (iov_iter_rw (iter) ==WRITE ) {int err =invalidate_inode_pages2_range (mapping, start >>PAGE_SHIFT , end >>PAGE_SHIFT );WARN_ON_ONCE (err);
fs/nfs/direct.c +2 -4
@@ -1024,10 +1024,8 @@ ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter) result =nfs_direct_write_schedule_iovec (dreq, iter, pos); -if (mapping->nrpages) { -invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end); - } +invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end);nfs_end_io_direct (inode);
fs/nfs/inode.c +5 -3
@@ -1118,10 +1118,12 @@ static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapif (ret <0 )return ret; } - ret =invalidate_inode_pages2 (mapping); -if (ret <0 ) -return ret; } + + ret =invalidate_inode_pages2 (mapping); +if (ret <0 ) +return ret; +if (S_ISDIR (inode->i_mode)) {spin_lock (&inode->i_lock);memset (nfsi->cookieverf,0 ,sizeof (nfsi->cookieverf));
mm/filemap.c +11 -15
@@ -2721,18 +2721,16 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)* about to write. We do this *before* the write so that we can return * without clobbering -EIOCBQUEUED from ->direct_IO(). */ -if (mapping->nrpages) { - written =invalidate_inode_pages2_range (mapping, + written =invalidate_inode_pages2_range (mapping, pos >>PAGE_SHIFT , end); -/* -* If a page can not be invalidated, return 0 to fall back -* to buffered write. -*/ -if (written) { -if (written == -EBUSY ) -return 0 ; - goto out; - } +/* +* If a page can not be invalidated, return 0 to fall back +* to buffered write. +*/ +if (written) { +if (written == -EBUSY ) +return 0 ; + goto out; } data = *from;
@@ -2746,10 +2744,8 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)* so we don't support it 100%. If this invalidation * fails, tough, the write still worked... */ -if (mapping->nrpages) { -invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end); - } +invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end);if (written >0 ) { pos += written;
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/4] fs: fix data invalidation in the cleancache during direct IO
Quoted 8 lines · Andrey Ryabinin
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Quoted 6 lines
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>---
<>
fs/dax.c dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, · line 1053
Quoted 13 lines
fs/dax.c +1
-1@@ -1047,7 +1047,7 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,* into page tables. We have to tear down these mappings so that data * written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if ((iomap->flags &IOMAP_F_NEW )) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );
tl;dr: I think the old code is correct, and that you don't need this change.
This should be harmless, but could slow us down a little if we keep calling invalidate_inode_pages2_range() without really needing to. Really for DAX I think we need to call invalidate_inode_page2_range() only if we have zero pages mapped over the place where we are doing I/O, which is why we check nrpages.
Is DAX even allowed to be used at the same time as cleancache? From a brief look at Documentation/vm/cleancache.txt, it seems like these two features are incompatible. With DAX we already are avoiding the page cache completely.
Anyway, I don't see how this change in DAX can save us from a data corruption (which is what you're seeing, right?), and I think it could slow us down, so I'd prefer to leave things as they are.
Re: [PATCH 1/4] fs: fix data invalidation in the cleancache during direct IO
Quoted 32 lines · Ross Zwisler
Quoted 8 lines · Andrey Ryabinin
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Quoted 6 lines
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>---
<>
fs/dax.c dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, · line 1053
Quoted 13 lines
fs/dax.c +1
-1@@ -1047,7 +1047,7 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,* into page tables. We have to tear down these mappings so that data * written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if ((iomap->flags &IOMAP_F_NEW )) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );tl;dr: I think the old code is correct, and that you don't need this change.
Quoted 6 lines
This should be harmless, but could slow us down a little if we keep calling
invalidate_inode_pages2_range()without really needing to. Really for DAX I think we need to callinvalidate_inode_page2_range()only if we have zero pages mapped over the place where we are doing I/O, which is why we check nrpages.
Check for ->nrpages only looks strange, because invalidate_inode_pages2_range() also invalidates exceptional radix tree entries. Is that correct that we invalidate exceptional entries only if ->nrpages > 0 and skip invalidation otherwise?
Quoted 3 lines
Is DAX even allowed to be used at the same time as cleancache? From a brief look at
Documentation/vm/cleancache.txt, it seems like these two features are incompatible. With DAX we already are avoiding the page cache completely.
tl;dr: I think you're right.
cleancache may store any PageUptodate && PageMappedToDisk page evicted from page cache (see __delete_from_page_cache) DAX deletes hole page via __delete_from_page_cache(), but I don't see we mark such page as Uptodate or MappedToDisk so it will never go into the cleancache.
Latter cleancache_get_page() (e.g. it's called from mpage_readpages() which is called from blkdev_read_pages()) I assume that DAX doesn't use a_ops->readpages() method so cleancache_get_page() is never called from DAX.
Quoted 4 lines
Anyway, I don't see how this change in DAX can save us from a data corruption (which is what you're seeing, right?), and I think it could slow us down, so I'd prefer to leave things as they are.
I'll remove this hunk from v2.
Thanks.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/4] fs: fix data invalidation in the cleancache during direct IO
Quoted 40 lines · Andrey Ryabinin
Quoted 32 lines · Ross Zwisler
Quoted 8 lines · Andrey Ryabinin
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Quoted 6 lines
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>---
<>
fs/dax.c dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, · line 1053
Quoted 13 lines
fs/dax.c +1
-1@@ -1047,7 +1047,7 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,* into page tables. We have to tear down these mappings so that data * written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if ((iomap->flags &IOMAP_F_NEW )) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );tl;dr: I think the old code is correct, and that you don't need this change.
Quoted 6 lines
This should be harmless, but could slow us down a little if we keep calling
invalidate_inode_pages2_range()without really needing to. Really for DAX I think we need to callinvalidate_inode_page2_range()only if we have zero pages mapped over the place where we are doing I/O, which is why we check nrpages.
Quoted 3 lines
Check for ->nrpages only looks strange, because
invalidate_inode_pages2_range()also invalidates exceptional radix tree entries. Is that correct that we invalidate exceptional entries only if ->nrpages > 0 and skip invalidation otherwise?
For DAX we only invalidate clean DAX exceptional entries so that we can keep dirty entries around for writeback, but yes you're correct that we only do the invalidation if nrpages > 0. And yes, it does seem a bit weird. :)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerRe: [PATCH 1/4] fs: fix data invalidation in the cleancache during direct IO
Quoted 41 lines · Ross Zwisler
Quoted 40 lines · Andrey Ryabinin
Quoted 38 lines · Ross Zwisler
Quoted 8 lines · Andrey Ryabinin
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Quoted 6 lines
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>---
<>
fs/dax.c dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, · line 1053
Quoted 13 lines
fs/dax.c +1
-1@@ -1047,7 +1047,7 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,* into page tables. We have to tear down these mappings so that data * written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if ((iomap->flags &IOMAP_F_NEW )) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );tl;dr: I think the old code is correct, and that you don't need this change.
This should be harmless, but could slow us down a little if we keep calling
invalidate_inode_pages2_range()without really needing to. Really for DAX I think we need to callinvalidate_inode_page2_range()only if we have zero pages mapped over the place where we are doing I/O, which is why we check nrpages.
Quoted 7 lines
Quoted 3 lines
Check for ->nrpages only looks strange, because
invalidate_inode_pages2_range()also invalidates exceptional radix tree entries. Is that correct that we invalidate exceptional entries only if ->nrpages > 0 and skip invalidation otherwise?For DAX we only invalidate clean DAX exceptional entries so that we can keep dirty entries around for writeback, but yes you're correct that we only do the invalidation if nrpages > 0. And yes, it does seem a bit weird. :)
Actually in this place the nrpages check is deliberate since there should only be hole pages or nothing in the invalidated range - see the comment before the if. But thinking more about it this assumption actually is not right in presence of zero PMD entries in the radix tree. So this change actually also fixes a possible bug for DAX but we should do it as a separate patch with a proper changelog.
Honza
Signature · 6 lines
Jan Kara <jack-IBi9RG/[email protected]> SUSE Labs, CR -- To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Jan Kara
maintainerRe: [PATCH 1/4] fs: fix data invalidation in the cleancache during direct IO
Quoted 50 lines · Jan Kara
Quoted 41 lines · Ross Zwisler
Quoted 40 lines · Andrey Ryabinin
Quoted 38 lines · Ross Zwisler
Quoted 8 lines · Andrey Ryabinin
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Quoted 6 lines
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>---
<>
fs/dax.c dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, · line 1053
Quoted 13 lines
fs/dax.c +1
-1@@ -1047,7 +1047,7 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,* into page tables. We have to tear down these mappings so that data * written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if ((iomap->flags &IOMAP_F_NEW )) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );tl;dr: I think the old code is correct, and that you don't need this change.
This should be harmless, but could slow us down a little if we keep calling
invalidate_inode_pages2_range()without really needing to. Really for DAX I think we need to callinvalidate_inode_page2_range()only if we have zero pages mapped over the place where we are doing I/O, which is why we check nrpages.Quoted 7 lines
Quoted 3 lines
Check for ->nrpages only looks strange, because
invalidate_inode_pages2_range()also invalidates exceptional radix tree entries. Is that correct that we invalidate exceptional entries only if ->nrpages > 0 and skip invalidation otherwise?For DAX we only invalidate clean DAX exceptional entries so that we can keep dirty entries around for writeback, but yes you're correct that we only do the invalidation if nrpages > 0. And yes, it does seem a bit weird. :)
Quoted 6 lines
Actually in this place the nrpages check is deliberate since there should only be hole pages or nothing in the invalidated range - see the comment before the if. But thinking more about it this assumption actually is not right in presence of zero PMD entries in the radix tree. So this change actually also fixes a possible bug for DAX but we should do it as a separate patch with a proper changelog.
Something like the attached patch. Ross?
Honza
Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]> SUSE Labs, CR
Quoted 1 line
From
da79b4b72a6fe5fcf1a554ca1ce77cb462e8a306Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Thu, 20 Apr 2017 16:38:20 +0200
Subject: [PATCH] dax: Fix inconsistency between mmap and write(2)
When a process has a PMD-sized hole mapped via mmap and later allocates part of the file underlying this area using write(2), memory mappings need not be appropriately invalidated if the file has no hole pages allocated and thus view via mmap will not show the data written by write(2). Fix the problem by always invalidating memory mappings covering part of the file for which blocks got allocated.
--- fs/dax.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/dax.c +4 -4
@@ -1028,11 +1028,11 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,return -EIO ;/* -* Write can allocate block for an area which has a hole page mapped -* into page tables. We have to tear down these mappings so that data -* written by write(2) is visible in mmap. +* Write can allocate block for an area which has a hole page or zero +* PMD entry in the radix tree. We have to tear down these mappings so +* that data written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if (iomap->flags &IOMAP_F_NEW ) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );
Signature · 1 line
2.12.0
Re: [PATCH 1/4] fs: fix data invalidation in the cleancache during direct IO
Quoted 51 lines · Jan Kara
Quoted 50 lines · Jan Kara
Quoted 48 lines · Ross Zwisler
Quoted 40 lines · Andrey Ryabinin
Quoted 32 lines · Ross Zwisler
Quoted 8 lines · Andrey Ryabinin
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Quoted 6 lines
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>---
<>
fs/dax.c dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, · line 1053
Quoted 13 lines
fs/dax.c +1
-1@@ -1047,7 +1047,7 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,* into page tables. We have to tear down these mappings so that data * written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if ((iomap->flags &IOMAP_F_NEW )) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );tl;dr: I think the old code is correct, and that you don't need this change.
Quoted 6 lines
This should be harmless, but could slow us down a little if we keep calling
invalidate_inode_pages2_range()without really needing to. Really for DAX I think we need to callinvalidate_inode_page2_range()only if we have zero pages mapped over the place where we are doing I/O, which is why we check nrpages.Quoted 3 lines
Check for ->nrpages only looks strange, because
invalidate_inode_pages2_range()also invalidates exceptional radix tree entries. Is that correct that we invalidate exceptional entries only if ->nrpages > 0 and skip invalidation otherwise?For DAX we only invalidate clean DAX exceptional entries so that we can keep dirty entries around for writeback, but yes you're correct that we only do the invalidation if nrpages > 0. And yes, it does seem a bit weird. :)
Quoted 8 lines
Quoted 6 lines
Actually in this place the nrpages check is deliberate since there should only be hole pages or nothing in the invalidated range - see the comment before the if. But thinking more about it this assumption actually is not right in presence of zero PMD entries in the radix tree. So this change actually also fixes a possible bug for DAX but we should do it as a separate patch with a proper changelog.
Something like the attached patch. Ross?
Yep, great catch, this is a real issue. The attached patch isn't sufficient, though, because invalidate_inode_pages2_range() for DAX exceptional entries only wipes out the radix tree entry, and doesn't call unmap_mapping_range() as it does in the case of real pages.
I'm working on a fix and an associated xfstest test.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/2 dax: prevent invalidation of mapped DAX entries
dax_invalidate_mapping_entry() currently removes DAX exceptional entries only if they are clean and unlocked. This is done via:
invalidate_mapping_pages()
invalidate_exceptional_entry()
dax_invalidate_mapping_entry()
However, for page cache pages removed in invalidate_mapping_pages() there is an additional criteria which is that the page must not be mapped. This is noted in the comments above invalidate_mapping_pages() and is checked in invalidate_inode_page().
For DAX entries this means that we can can end up in a situation where a DAX exceptional entry, either a huge zero page or a regular DAX entry, could end up mapped but without an associated radix tree entry. This is inconsistent with the rest of the DAX code and with what happens in the page cache case.
We aren't able to unmap the DAX exceptional entry because according to its comments invalidate_mapping_pages() isn't allowed to block, and unmap_mapping_range() takes a write lock on the mapping->i_mmap_rwsem.
Since we essentially never have unmapped DAX entries to evict from the radix tree, just remove dax_invalidate_mapping_entry().
---
This series applies cleanly to the current v4.11-rc7 based linux/master, and has passed an xfstests run with DAX on ext4 and XFS.
These patches also apply to v4.10.9 with a little work from the 3-way merge feature.
fs/dax.c | 29 ----------------------------- include/linux/dax.h | 1 - mm/truncate.c | 9 +++------ 3 files changed, 3 insertions(+), 36 deletions(-)
fs/dax.c +0 -29
@@ -507,35 +507,6 @@ int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) }/* -* Invalidate exceptional DAX entry if easily possible. This handles DAX -* entries for invalidate_inode_pages() so we evict the entry only if we can -* do so without blocking. -*/ -int dax_invalidate_mapping_entry (struct address_space *mapping,pgoff_t index) -{ -int ret =0 ; -void *entry, **slot; -struct radix_tree_root *page_tree = &mapping->page_tree; - -spin_lock_irq (&mapping->tree_lock); - entry =__radix_tree_lookup (page_tree, index,NULL , &slot); -if (!entry || !radix_tree_exceptional_entry (entry) || -slot_locked (mapping, slot)) - goto out; -if (radix_tree_tag_get (page_tree, index,PAGECACHE_TAG_DIRTY ) || -radix_tree_tag_get (page_tree, index,PAGECACHE_TAG_TOWRITE )) - goto out; -radix_tree_delete (page_tree, index); - mapping->nrexceptional--; - ret =1 ; -out: -spin_unlock_irq (&mapping->tree_lock); -if (ret) -dax_wake_mapping_entry_waiter (mapping, index, entry, true); -return ret; -} - -/* * Invalidate exceptional DAX entry if it is clean. */ int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
include/linux/dax.h +0 -1
@@ -41,7 +41,6 @@ ssize_t dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,int dax_iomap_fault (struct vm_fault *vmf,enum page_entry_size pe_size,const struct iomap_ops *ops);int dax_delete_mapping_entry (struct address_space *mapping,pgoff_t index); -int dax_invalidate_mapping_entry (struct address_space *mapping,pgoff_t index);int dax_invalidate_mapping_entry_sync (struct address_space *mapping,pgoff_t index); void dax_wake_mapping_entry_waiter(struct address_space *mapping,
mm/truncate.c +3 -6
@@ -67,17 +67,14 @@ static void truncate_exceptional_entry(struct address_space *mapping,/* * Invalidate exceptional entry if easily possible. This handles exceptional -* entries for invalidate_inode_pages() so for DAX it evicts only unlocked and -* clean entries. +* entries for invalidate_inode_pages(). */ static int invalidate_exceptional_entry (struct address_space *mapping,pgoff_t index,void *entry) { -/* Handled by shmem itself */ -if (shmem_mapping (mapping)) +/* Handled by shmem itself, or for DAX we do nothing. */ +if (shmem_mapping (mapping) ||dax_mapping (mapping))return 1 ; -if (dax_mapping (mapping)) -return dax_invalidate_mapping_entry (mapping, index);clear_shadow_entry (mapping, index, entry);return 1 ; }
Signature · 1 line
2.9.3
PATCH 1/2 xfs: fix incorrect argument count check
t_mmap_dio.c actually requires 4 arguments, not 3 as the current check enforces:
usage: t_mmap_dio <src file> <dest file> <size> <msg> open src(No such file or directory) len 0 (null)
--- src/t_mmap_dio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/t_mmap_dio.c +1 -1
@@ -39,7 +39,7 @@ int main(int argc, char **argv)char *dfile;unsigned long len, opt; -if (argc <4 ) +if (argc <5 )usage (basename (argv[0 ]));while ((opt =getopt (argc, argv,"b" )) !=-1 )
Signature · 7 lines
2.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/2 dax: add regression test for stale mmap reads
This adds a regression test for the following kernel patch:
dax: fix data corruption due to stale mmap reads
The above patch fixes an issue where users of DAX can suffer data corruption from stale mmap reads via the following sequence:
- open an mmap over a 2MiB hole
- read from a 2MiB hole, faulting in a 2MiB zero page
- write to the hole with write(3p). The write succeeds but we incorrectly leave the 2MiB zero page mapping intact.
- via the mmap, read the data that was just written. Since the zero page mapping is still intact we read back zeroes instead of the new data.
--- .gitignore | 1 + src/Makefile | 2 +- src/t_dax_stale_pmd.c | 56 ++++++++++++++++++++++++++++++++++++++++++ tests/generic/427 | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/427.out | 2 ++ tests/generic/group | 1 + 6 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/t_dax_stale_pmd.c create mode 100755 tests/generic/427 create mode 100644 tests/generic/427.out
.gitignore +1 -0
@@ -134,6 +134,7 @@ /src/renameat2 /src/t_rename_overwrite /src/t_mmap_dio +/src/t_dax_stale_pmd # dmapi/ binaries /dmapi/src/common/cmd/read_invis
src/Makefile +1 -1
@@ -12,7 +12,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ godown resvtest writemod makeextents itrash rename \ multi_open_unlink dmiperf unwritten_sync genhashnames t_holes \ t_mmap_writev t_truncate_cmtime dirhash_collide t_rename_overwrite \ - holetest t_truncate_self t_mmap_dio af_unix + holetest t_truncate_self t_mmap_dio af_unix t_dax_stale_pmd LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
src/t_dax_stale_pmd.c new file +56 -0
56 lines not on this page · raw message
tests/generic/427 new file +68 -0
68 lines not on this page · raw message
tests/generic/427.out new file +2 -0
@@ -0,0 +1,2 @@ +QA output created by 427 +Silence is golden
tests/generic/group +1 -0
@@ -429,3 +429,4 @@ 424 auto quick 425 auto quick attr 426 auto quick exportfs +427 auto quick
Signature · 7 lines
2.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/2] dax: add regression test for stale mmap reads
Quoted 4 lines · Ross Zwisler
This adds a regression test for the following kernel patch:
dax: fix data corruption due to stale mmap reads
Seems that this patch hasn't been merged into linus tree, thus 4.11-rc8 kernel should fail this test, but it passed for me, tested with 4.11-rc8 kernel on both ext4 and xfs, with both brd devices and pmem devices created from "memmap=10G!5G memmap=15G!15G" kernel boot command line. Did I miss anything?
# ./check -s ext4_pmem_4k generic/427 SECTION -- ext4_pmem_4k RECREATING -- ext4 on /dev/pmem0 FSTYP -- ext4 PLATFORM -- Linux/x86_64 hp-dl360g9-15 4.11.0-rc8.kasan MKFS_OPTIONS -- -b 4096 /dev/pmem1 MOUNT_OPTIONS -- -o acl,user_xattr -o context=system_u:object_r:root_t:s0 /dev/pmem1 /scratch
generic/427 1s ... 1s
Ran: generic/427
Passed all 1 tests
Some comments inline.
Quoted 81 lines
The above patch fixes an issue where users of DAX can suffer data corruption from stale mmap reads via the following sequence:
- open an mmap over a 2MiB hole
- read from a 2MiB hole, faulting in a 2MiB zero page
- write to the hole with write(3p). The write succeeds but we incorrectly leave the 2MiB zero page mapping intact.
- via the mmap, read the data that was just written. Since the zero page mapping is still intact we read back zeroes instead of the new data.
Signed-off-by Ross Zwisler <[email protected]>--- .gitignore | 1 + src/Makefile | 2 +- src/t_dax_stale_pmd.c | 56 ++++++++++++++++++++++++++++++++++++++++++ tests/generic/427 | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/427.out | 2 ++ tests/generic/group | 1 + 6 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/t_dax_stale_pmd.c create mode 100755 tests/generic/427 create mode 100644 tests/generic/427.out.gitignore +1
-0@@ -134,6 +134,7 @@ /src/renameat2 /src/t_rename_overwrite /src/t_mmap_dio +/src/t_dax_stale_pmd # dmapi/ binaries /dmapi/src/common/cmd/read_invissrc/Makefile +1
-1@@ -12,7 +12,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ godown resvtest writemod makeextents itrash rename \ multi_open_unlink dmiperf unwritten_sync genhashnames t_holes \ t_mmap_writev t_truncate_cmtime dirhash_collide t_rename_overwrite \ - holetest t_truncate_self t_mmap_dio af_unix + holetest t_truncate_self t_mmap_dio af_unix t_dax_stale_pmd LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \src/t_dax_stale_pmd.c new file +24
-0@@ -0,0 +1,56 @@ +#include <errno.h> +#include <fcntl.h> +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#define MiB (a) ((a)*1024*1024) + +void err_exit (char *op) +{ +fprintf (stderr,"%s: %s\n" , op,strerror (errno)); +exit (1 ); +} + +int main (int argc,char *argv[]) +{ + volatile int a __attribute__((__unused__)); +char *buffer ="HELLO WORLD!" ; +char *data;
src/t_dax_stale_pmd.c line 34
Quoted 10 lines
src/t_dax_stale_pmd.c new file +10
-0+int fd; + +if (argc <2 ) { +printf ("Usage: %s <pmem file>\n" ,basename (argv[0 ])); +exit (0 ); + } + + fd =open (argv[1 ],O_RDWR ); +if (fd <0 ) +err_exit ("fd" );
^^^^ Nitpick, the "op" should be "open"?
Quoted 8 lines
src/t_dax_stale_pmd.c new file +8
-0+ + data =mmap (NULL ,MiB (2 ),PROT_READ ,MAP_SHARED , fd,MiB (2 )); + +/* +* This faults in a 2MiB zero page to satisfy the read. +* 'a' is volatile so this read doesn't get optimized out. +*/ + a = data[0 ];
src/t_dax_stale_pmd.c line 52
Quoted 10 lines
src/t_dax_stale_pmd.c new file +10
-0+ +pwrite (fd, buffer,strlen (buffer),MiB (2 )); + +/* +* Try and use the mmap to read back the data we just wrote with +* pwrite(). If the kernel bug is present the mapping from the 2MiB +* zero page will still be intact, and we'll read back zeros instead. +*/ +if (strncmp (buffer, data,strlen (buffer))) +err_exit ("strncmp mismatch!" );
strncmp doesn't set errno, this err_exit message might be confusing:
"strncmp mismatch!: Success"
Quoted 51 lines
src/t_dax_stale_pmd.c new file +4
-0+ +close (fd); +return 0 ; +}tests/generic/427 new file +41
-0@@ -0,0 +1,68 @@ +#! /bin/bash +# FS QA Test 427 +# +# This is a regression test for kernel patch: +# dax: fix data corruption due to stale mmap reads +# created by Ross Zwisler <[email protected]> +# +#----------------------------------------------------------------------- +# Copyright (c) 2017 Intel Corporation. All Rights Reserved. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it would be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +#----------------------------------------------------------------------- +# + +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap "_cleanup; exit \$status" 0 1 2 3 15 + +_cleanup() +{ + cd / + rm -f $tmp.* +} + +# get standard environment, filters and checks
tests/generic/427 line 51
Quoted 10 lines
tests/generic/427 new file +10
-0+. ./common/rc +. ./common/filter + +# remove previous $seqres.full before test +rm -f $seqres.full + +# Modify as appropriate. +_supported_fs generic +_supported_os Linux +_require_scratch_dax
I don't think dax is a requirement here, this test could run on normal block device without "-o dax" option too. It won't hurt to run with more test configurations. And test on nvdimm device with dax mount option could be one of the test configs, e.g.
TEST_DEV=/dev/pmem0
SCRATCH_DEV=/dev/pmem1
MOUNT_OPTIONS="-o dax"
...
tests/generic/427 line 53
Quoted 2 lines
tests/generic/427 new file +2
-0+_require_test_program "t_dax_stale_pmd" +_require_user
_require_xfs_io_command "falloc"
So test _notrun on ext2/3.
tests/generic/427 line 57
Quoted 4 lines
tests/generic/427 new file +4
-0+ +# real QA test starts here +_scratch_mkfs >>$seqres.full 2>&1 +_scratch_mount "-o dax"
Same here, dax is not required.
tests/generic/427 line 61
Quoted 4 lines
tests/generic/427 new file +4
-0+ +$XFS_IO_PROG -f -c "falloc 0 4M" $SCRATCH_MNT/testfile >> $seqres.full 2>&1 +chmod 0644 $SCRATCH_MNT/testfile +chown $qa_user $SCRATCH_MNT/testfile
Any specific reason to use $qa_user to run this test? Comments would be great.
Thanks,
Eryu
Quoted 27 lines
tests/generic/427 new file +7
-0+ +_user_do "src/t_dax_stale_pmd $SCRATCH_MNT/testfile" + +# success, all done +echo "Silence is golden" +status=0 +exittests/generic/427.out new file +2
-0@@ -0,0 +1,2 @@ +QA output created by 427 +Silence is goldentests/generic/group +1
-0@@ -429,3 +429,4 @@ 424 auto quick 425 auto quick attr 426 auto quick exportfs +427 auto quickSignature · 1 line
2.9.3
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/2] dax: add regression test for stale mmap reads
Quoted 6 lines · Eryu Guan
Quoted 4 lines · Ross Zwisler
This adds a regression test for the following kernel patch:
dax: fix data corruption due to stale mmap reads
Quoted 8 lines
Seems that this patch hasn't been merged into linus tree, thus 4.11-rc8 kernel should fail this test, but it passed for me, tested with 4.11-rc8 kernel on both ext4 and xfs, with both brd devices and pmem devices created from "memmap=10G!5G memmap=15G!15G" kernel boot command line. Did I miss anything?
# ./check -s ext4_pmem_4k generic/427 SECTION -- ext4_pmem_4k
Ooh, I didn't add this 'ext4_pmem_4k' section goodness, and it's not present in the xfstests/master that I was using. Do you have patches to add that?
Quoted 9 lines
RECREATING -- ext4 on /dev/pmem0 FSTYP -- ext4 PLATFORM -- Linux/x86_64 hp-dl360g9-15 4.11.0-rc8.kasan MKFS_OPTIONS -- -b 4096 /dev/pmem1
MOUNT_OPTIONS-- -o acl,user_xattr -o context=system_u:object_r:root_t:s0/dev/pmem1/scratchgeneric/427 1s ... 1s
Ran: generic/427
Passed all 1 tests
Your memmap params look fine. I tested with BRD and PMEM, and with EXT4 and XFS, and all combinations failed for me as expected with v4.11-rc8.
One issue could have been that the test file already existed when the test was run. I wasn't removing it between runs earlier, but I've fixed that for v2.
Another issue I guess could have been that the hole that we got back from the filesystem was smaller than 2MiB? Can you try running v2 (which I'll post in a second) against a TEST_DEV made with one of the following:
ext4: mkfs.ext4 -b 4096 -E stride=512 -F $TEST_DEV xfs: mkfs.xfs -f -d su=2m,sw=1 $TEST_DEV
This helps us get 2MiB sized and aligned allocations so we can fault in PMDs, but I'm not sure whether or not it would matter for holes.
Quoted 107 lines
Some comments inline.
Quoted 81 lines
The above patch fixes an issue where users of DAX can suffer data corruption from stale mmap reads via the following sequence:
- open an mmap over a 2MiB hole
- read from a 2MiB hole, faulting in a 2MiB zero page
- write to the hole with write(3p). The write succeeds but we incorrectly leave the 2MiB zero page mapping intact.
- via the mmap, read the data that was just written. Since the zero page mapping is still intact we read back zeroes instead of the new data.
Signed-off-by Ross Zwisler <[email protected]>--- .gitignore | 1 + src/Makefile | 2 +- src/t_dax_stale_pmd.c | 56 ++++++++++++++++++++++++++++++++++++++++++ tests/generic/427 | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/427.out | 2 ++ tests/generic/group | 1 + 6 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/t_dax_stale_pmd.c create mode 100755 tests/generic/427 create mode 100644 tests/generic/427.out.gitignore +1
-0@@ -134,6 +134,7 @@ /src/renameat2 /src/t_rename_overwrite /src/t_mmap_dio +/src/t_dax_stale_pmd # dmapi/ binaries /dmapi/src/common/cmd/read_invissrc/Makefile +1
-1@@ -12,7 +12,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ godown resvtest writemod makeextents itrash rename \ multi_open_unlink dmiperf unwritten_sync genhashnames t_holes \ t_mmap_writev t_truncate_cmtime dirhash_collide t_rename_overwrite \ - holetest t_truncate_self t_mmap_dio af_unix + holetest t_truncate_self t_mmap_dio af_unix t_dax_stale_pmd LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \src/t_dax_stale_pmd.c new file +24
-0@@ -0,0 +1,56 @@ +#include <errno.h> +#include <fcntl.h> +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#define MiB (a) ((a)*1024*1024) + +void err_exit (char *op) +{ +fprintf (stderr,"%s: %s\n" , op,strerror (errno)); +exit (1 ); +} + +int main (int argc,char *argv[]) +{ + volatile int a __attribute__((__unused__)); +char *buffer ="HELLO WORLD!" ; +char *data;src/t_dax_stale_pmd.c line 34
Quoted 10 lines
src/t_dax_stale_pmd.c new file +10
-0+int fd; + +if (argc <2 ) { +printf ("Usage: %s <pmem file>\n" ,basename (argv[0 ])); +exit (0 ); + } + + fd =open (argv[1 ],O_RDWR ); +if (fd <0 ) +err_exit ("fd" );^^^^ Nitpick, the "op" should be "open"?
Quoted 13 lines
src/t_dax_stale_pmd.c new file +13
-0+ + data =mmap (NULL ,MiB (2 ),PROT_READ ,MAP_SHARED , fd,MiB (2 )); + +/* +* This faults in a 2MiB zero page to satisfy the read. +* 'a' is volatile so this read doesn't get optimized out. +*/ + a = data[0 ]; + +pwrite (fd, buffer,strlen (buffer),MiB (2 )); + +/* +* Try and use the mmap to read back the data we just wrote with
Quoted 8 lines
src/t_dax_stale_pmd.c line 52
Quoted 5 lines
src/t_dax_stale_pmd.c new file +5
-0+* pwrite(). If the kernel bug is present the mapping from the 2MiB +* zero page will still be intact, and we'll read back zeros instead. +*/ +if (strncmp (buffer, data,strlen (buffer))) +err_exit ("strncmp mismatch!" );strncmp doesn't set errno, this
err_exitmessage might be confusing:
"strncmp mismatch!: Success"
Ah, thanks, fixed in v2.
Quoted 67 lines
Quoted 51 lines
src/t_dax_stale_pmd.c new file +4
-0+ +close (fd); +return 0 ; +}tests/generic/427 new file +41
-0@@ -0,0 +1,68 @@ +#! /bin/bash +# FS QA Test 427 +# +# This is a regression test for kernel patch: +# dax: fix data corruption due to stale mmap reads +# created by Ross Zwisler <[email protected]> +# +#----------------------------------------------------------------------- +# Copyright (c) 2017 Intel Corporation. All Rights Reserved. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it would be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +#----------------------------------------------------------------------- +# + +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap "_cleanup; exit \$status" 0 1 2 3 15 + +_cleanup() +{ + cd / + rm -f $tmp.* +} + +# get standard environment, filters and checkstests/generic/427 line 51
Quoted 10 lines
tests/generic/427 new file +10
-0+. ./common/rc +. ./common/filter + +# remove previous $seqres.full before test +rm -f $seqres.full + +# Modify as appropriate. +_supported_fs generic +_supported_os Linux +_require_scratch_daxI don't think dax is a requirement here, this test could run on normal block device without "-o dax" option too. It won't hurt to run with more test configurations. And test on nvdimm device with dax mount option could be one of the test configs, e.g.
Quoted 4 lines
TEST_DEV=/dev/pmem0
SCRATCH_DEV=/dev/pmem1
MOUNT_OPTIONS="-o dax"
...
Yep, agreed, fixed in v2.
Quoted 6 lines
tests/generic/427 line 53
Quoted 2 lines
tests/generic/427 new file +2
-0+_require_test_program "t_dax_stale_pmd" +_require_user
_require_xfs_io_command"falloc"So test
_notrunon ext2/3.
Fixed in v2.
Quoted 6 lines
tests/generic/427 line 57
Quoted 4 lines
tests/generic/427 new file +4
-0+ +# real QA test starts here +_scratch_mkfs >>$seqres.full 2>&1 +_scratch_mount "-o dax"Same here, dax is not required.
Fixed in v2.
Quoted 8 lines
tests/generic/427 line 61
Quoted 5 lines
tests/generic/427 new file +4
-0+ +$XFS_IO_PROG -f -c "falloc 0 4M" $SCRATCH_MNT/testfile >> $seqres.full 2>&1 +chmod 0644 $SCRATCH_MNT/testfile +chown $qa_user $SCRATCH_MNT/testfileAny specific reason to use $qa_user to run this test? Comments would be great.
Nope, just cargo-culting my way through my first xfstest. :) I've removed this for v2.
Quoted 2 lines
Thanks,
Eryu
Thanks for the review!
Quoted 8 lines
tests/generic/427.out
Quoted 8 lines
tests/generic/427 new file +7
-0+ +_user_do "src/t_dax_stale_pmd $SCRATCH_MNT/testfile" + +# success, all done +echo "Silence is golden" +status=0 +exittests/generic/427.out +0
-0
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/2] dax: add regression test for stale mmap reads
Quoted 13 lines · Ross Zwisler
Quoted 12 lines · Eryu Guan
Quoted 4 lines · Ross Zwisler
This adds a regression test for the following kernel patch:
dax: fix data corruption due to stale mmap reads
Seems that this patch hasn't been merged into linus tree, thus 4.11-rc8 kernel should fail this test, but it passed for me, tested with 4.11-rc8 kernel on both ext4 and xfs, with both brd devices and pmem devices created from "memmap=10G!5G memmap=15G!15G" kernel boot command line. Did I miss anything?
Quoted 5 lines
Quoted 2 lines
# ./check -s ext4_pmem_4k generic/427 SECTION -- ext4_pmem_4kOoh, I didn't add this '
ext4_pmem_4k' section goodness, and it's not present in the xfstests/master that I was using. Do you have patches to add that?
That's one of my config sections, it's all user-defined, not committed to fstests repo :)
You can take a look at README.config-sections for more details. Here is my local.config file for your reference
[default]
TEST_DEV=/dev/pmem0
SCRATCH_DEV=/dev/pmem1
TEST_DIR=/mnt
SCRATCH_MNT=/scratch
RECREATE_TEST_DEV=true
[xfs_pmem_4k]
FSTYP=xfs
MKFS_OPTIONS="-f -m crc=1 -b size=4k"
[ext4_pmem_4k]
FSTYP=ext4
MKFS_OPTIONS="-b 4096"
Quoted 21 lines
Quoted 10 lines
RECREATING -- ext4 on /dev/pmem0 FSTYP -- ext4 PLATFORM -- Linux/x86_64 hp-dl360g9-15 4.11.0-rc8.kasan MKFS_OPTIONS -- -b 4096 /dev/pmem1
MOUNT_OPTIONS-- -o acl,user_xattr -o context=system_u:object_r:root_t:s0/dev/pmem1/scratchgeneric/427 1s ... 1s
Ran: generic/427
Passed all 1 testsYour memmap params look fine. I tested with BRD and PMEM, and with EXT4 and XFS, and all combinations failed for me as expected with v4.11-rc8.
One issue could have been that the test file already existed when the test was run. I wasn't removing it between runs earlier, but I've fixed that for v2.
Another issue I guess could have been that the hole that we got back from the filesystem was smaller than 2MiB? Can you try running v2 (which I'll post in a second) against a
TEST_DEVmade with one of the following:
Quoted 5 lines
ext4: mkfs.ext4 -b 4096 -E stride=512 -F $TEST_DEV xfs: mkfs.xfs -f -d su=2m,sw=1 $TEST_DEVThis helps us get 2MiB sized and aligned allocations so we can fault in PMDs, but I'm not sure whether or not it would matter for holes.
I guess that's the point to reproduce the failure, I'll confirm with v2 patches. If these non-default & not widely tested mkfs options are required to reproduce it, I think we can specify them in the test, as extra mkfs options to _scratch_mkfs, as what generic/413 does.
Quoted 186 lines
Quoted 108 lines
Some comments inline.
Quoted 81 lines
The above patch fixes an issue where users of DAX can suffer data corruption from stale mmap reads via the following sequence:
- open an mmap over a 2MiB hole
- read from a 2MiB hole, faulting in a 2MiB zero page
- write to the hole with write(3p). The write succeeds but we incorrectly leave the 2MiB zero page mapping intact.
- via the mmap, read the data that was just written. Since the zero page mapping is still intact we read back zeroes instead of the new data.
Signed-off-by Ross Zwisler <[email protected]>--- .gitignore | 1 + src/Makefile | 2 +- src/t_dax_stale_pmd.c | 56 ++++++++++++++++++++++++++++++++++++++++++ tests/generic/427 | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/427.out | 2 ++ tests/generic/group | 1 + 6 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/t_dax_stale_pmd.c create mode 100755 tests/generic/427 create mode 100644 tests/generic/427.out.gitignore +1
-0@@ -134,6 +134,7 @@ /src/renameat2 /src/t_rename_overwrite /src/t_mmap_dio +/src/t_dax_stale_pmd # dmapi/ binaries /dmapi/src/common/cmd/read_invissrc/Makefile +1
-1@@ -12,7 +12,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ godown resvtest writemod makeextents itrash rename \ multi_open_unlink dmiperf unwritten_sync genhashnames t_holes \ t_mmap_writev t_truncate_cmtime dirhash_collide t_rename_overwrite \ - holetest t_truncate_self t_mmap_dio af_unix + holetest t_truncate_self t_mmap_dio af_unix t_dax_stale_pmd LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \src/t_dax_stale_pmd.c new file +24
-0@@ -0,0 +1,56 @@ +#include <errno.h> +#include <fcntl.h> +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#define MiB (a) ((a)*1024*1024) + +void err_exit (char *op) +{ +fprintf (stderr,"%s: %s\n" , op,strerror (errno)); +exit (1 ); +} + +int main (int argc,char *argv[]) +{ + volatile int a __attribute__((__unused__)); +char *buffer ="HELLO WORLD!" ; +char *data;src/t_dax_stale_pmd.c line 34
Quoted 10 lines
src/t_dax_stale_pmd.c new file +10
-0+int fd; + +if (argc <2 ) { +printf ("Usage: %s <pmem file>\n" ,basename (argv[0 ])); +exit (0 ); + } + + fd =open (argv[1 ],O_RDWR ); +if (fd <0 ) +err_exit ("fd" );^^^^ Nitpick, the "op" should be "open"?
Quoted 13 lines
src/t_dax_stale_pmd.c new file +13
-0+ + data =mmap (NULL ,MiB (2 ),PROT_READ ,MAP_SHARED , fd,MiB (2 )); + +/* +* This faults in a 2MiB zero page to satisfy the read. +* 'a' is volatile so this read doesn't get optimized out. +*/ + a = data[0 ]; + +pwrite (fd, buffer,strlen (buffer),MiB (2 )); + +/* +* Try and use the mmap to read back the data we just wrote with Quoted 8 lines
src/t_dax_stale_pmd.c line 52
Quoted 5 lines
src/t_dax_stale_pmd.c new file +5
-0+* pwrite(). If the kernel bug is present the mapping from the 2MiB +* zero page will still be intact, and we'll read back zeros instead. +*/ +if (strncmp (buffer, data,strlen (buffer))) +err_exit ("strncmp mismatch!" );strncmp doesn't set errno, this
err_exitmessage might be confusing:
"strncmp mismatch!: Success"Ah, thanks, fixed in v2.
Quoted 67 lines
Quoted 51 lines
src/t_dax_stale_pmd.c new file +4
-0+ +close (fd); +return 0 ; +}tests/generic/427 new file +41
-0@@ -0,0 +1,68 @@ +#! /bin/bash +# FS QA Test 427 +# +# This is a regression test for kernel patch: +# dax: fix data corruption due to stale mmap reads +# created by Ross Zwisler <[email protected]> +# +#----------------------------------------------------------------------- +# Copyright (c) 2017 Intel Corporation. All Rights Reserved. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it would be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +#----------------------------------------------------------------------- +# + +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap "_cleanup; exit \$status" 0 1 2 3 15 + +_cleanup() +{ + cd / + rm -f $tmp.* +} + +# get standard environment, filters and checkstests/generic/427 line 51
Quoted 10 lines
tests/generic/427 new file +10
-0+. ./common/rc +. ./common/filter + +# remove previous $seqres.full before test +rm -f $seqres.full + +# Modify as appropriate. +_supported_fs generic +_supported_os Linux +_require_scratch_daxI don't think dax is a requirement here, this test could run on normal block device without "-o dax" option too. It won't hurt to run with more test configurations. And test on nvdimm device with dax mount option could be one of the test configs, e.g.
Quoted 6 lines
Quoted 4 lines
TEST_DEV=/dev/pmem0
SCRATCH_DEV=/dev/pmem1
MOUNT_OPTIONS="-o dax"
...Yep, agreed, fixed in v2.
Then perhaps the test program should be renamed? As no dax is required. How about t_mmap_stale_pmd?
Quoted 28 lines
Quoted 7 lines
tests/generic/427 line 53
Quoted 3 lines
tests/generic/427 new file +2
-0+_require_test_program "t_dax_stale_pmd" +_require_user
_require_xfs_io_command"falloc"So test
_notrunon ext2/3.Fixed in v2.
Quoted 6 lines
tests/generic/427 line 57
Quoted 4 lines
tests/generic/427 new file +4
-0+ +# real QA test starts here +_scratch_mkfs >>$seqres.full 2>&1 +_scratch_mount "-o dax"Same here, dax is not required.
Fixed in v2.
Quoted 8 lines
tests/generic/427 line 61
Quoted 5 lines
tests/generic/427 new file +4
-0+ +$XFS_IO_PROG -f -c "falloc 0 4M" $SCRATCH_MNT/testfile >> $seqres.full 2>&1 +chmod 0644 $SCRATCH_MNT/testfile +chown $qa_user $SCRATCH_MNT/testfileAny specific reason to use $qa_user to run this test? Comments would be great.
Quoted 2 lines
Nope, just cargo-culting my way through my first xfstest. :) I've removed this for v2.
I think it's in a pretty good shape for "first fstests test" :)
Quoted 5 lines
Quoted 3 lines
Thanks,
EryuThanks for the review!
Thanks for adding new test!
Eryu
Quoted 13 lines
Quoted 9 lines
tests/generic/427.out
Quoted 9 lines
tests/generic/427 new file +7
-0+ +_user_do "src/t_dax_stale_pmd $SCRATCH_MNT/testfile" + +# success, all done +echo "Silence is golden" +status=0 +exittests/generic/427.out +0
-0--
To unsubscribe from this list: send the line "unsubscribe fstests" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/2 dax: fix data corruption due to stale mmap reads
In reply to Ross ZwislerUsers of DAX can suffer data corruption from stale mmap reads via the following sequence:
- open an mmap over a 2MiB hole
- read from a 2MiB hole, faulting in a 2MiB zero page
- write to the hole with write(3p). The write succeeds but we incorrectly leave the 2MiB zero page mapping intact.
- via the mmap, read the data that was just written. Since the zero page mapping is still intact we read back zeroes instead of the new data.
We fix this by unconditionally calling invalidate_inode_pages2_range() in dax_iomap_actor() for new block allocations, and by enhancing __dax_invalidate_mapping_entry() so that it properly unmaps the DAX entry being removed from the radix tree.
This is based on an initial patch from Jan Kara.
--- fs/dax.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
fs/dax.c +19 -7
@@ -468,23 +468,35 @@ static int __dax_invalidate_mapping_entry(struct address_space *mapping, pgoff_t index, bool trunc) { int ret = 0; -void *entry; +void *entry, **slot;struct radix_tree_root *page_tree = &mapping->page_tree;spin_lock_irq (&mapping->tree_lock); - entry =get_unlocked_mapping_entry (mapping, index,NULL ); + entry =get_unlocked_mapping_entry (mapping, index, &slot);if (!entry || !radix_tree_exceptional_entry (entry)) goto out;if (!trunc && (radix_tree_tag_get (page_tree, index,PAGECACHE_TAG_DIRTY ) ||radix_tree_tag_get (page_tree, index,PAGECACHE_TAG_TOWRITE ))) goto out; + +/* +* Make sure 'entry' remains valid while we drop mapping->tree_lock to +* do the unmap_mapping_range() call. +*/ + entry =lock_slot (mapping, slot); +spin_unlock_irq (&mapping->tree_lock); + +unmap_mapping_range (mapping, (loff_t )index <<PAGE_SHIFT , + (loff_t )PAGE_SIZE <<dax_radix_order (entry),0 ); + +spin_lock_irq (&mapping->tree_lock);radix_tree_delete (page_tree, index); mapping->nrexceptional--; ret =1 ; out: -put_unlocked_mapping_entry (mapping, index, entry);spin_unlock_irq (&mapping->tree_lock); +dax_wake_mapping_entry_waiter (mapping, index, entry, true);return ret; }/*
@@ -999,11 +1011,11 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,return -EIO ;/* -* Write can allocate block for an area which has a hole page mapped -* into page tables. We have to tear down these mappings so that data -* written by write(2) is visible in mmap. +* Write can allocate block for an area which has a hole page or zero +* PMD entry in the radix tree. We have to tear down these mappings so +* that data written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if (iomap->flags &IOMAP_F_NEW ) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );
Signature · 1 line
2.9.3
Jan Kara
maintainerRe: [PATCH 2/2] dax: fix data corruption due to stale mmap reads
Quoted 45 lines · Ross Zwisler
Users of DAX can suffer data corruption from stale mmap reads via the following sequence:
- open an mmap over a 2MiB hole
- read from a 2MiB hole, faulting in a 2MiB zero page
- write to the hole with write(3p). The write succeeds but we incorrectly leave the 2MiB zero page mapping intact.
- via the mmap, read the data that was just written. Since the zero page mapping is still intact we read back zeroes instead of the new data.
We fix this by unconditionally calling
invalidate_inode_pages2_range()indax_iomap_actor()for new block allocations, and by enhancing__dax_invalidate_mapping_entry()so that it properly unmaps the DAX entry being removed from the radix tree.This is based on an initial patch from Jan Kara.
Fixes c6dcf52c23d2 ("mm: Invalidate DAX radix tree entries only if appropriate")Reported-by Jan Kara <[email protected]>Cc <[email protected]> [4.10+]--- fs/dax.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)fs/dax.c +2
-2@@ -468,23 +468,35 @@ static int __dax_invalidate_mapping_entry(struct address_space *mapping, pgoff_t index, bool trunc) { int ret = 0; -void *entry; +void *entry, **slot;struct radix_tree_root *page_tree = &mapping->page_tree;spin_lock_irq (&mapping->tree_lock); - entry =get_unlocked_mapping_entry (mapping, index,NULL ); + entry =get_unlocked_mapping_entry (mapping, index, &slot);if (!entry || !radix_tree_exceptional_entry (entry)) goto out;
fs/dax.c static int __dax_invalidate_mapping_entry(struct address_space *mapping, · line 487
Quoted 10 lines
fs/dax.c +6
-0if (!trunc && (radix_tree_tag_get (page_tree, index,PAGECACHE_TAG_DIRTY ) ||radix_tree_tag_get (page_tree, index,PAGECACHE_TAG_TOWRITE ))) goto out; + +/* +* Make sure 'entry' remains valid while we drop mapping->tree_lock to +* do the unmap_mapping_range() call. +*/ + entry =lock_slot (mapping, slot);
This also stops page faults from mapping the entry again. Maybe worth mentioning here as well.
fs/dax.c static int __dax_invalidate_mapping_entry(struct address_space *mapping, · line 491
Quoted 4 lines
fs/dax.c +4
-0+spin_unlock_irq (&mapping->tree_lock); + +unmap_mapping_range (mapping, (loff_t )index <<PAGE_SHIFT , + (loff_t )PAGE_SIZE <<dax_radix_order (entry),0 );
Ouch, unmapping entry-by-entry may get quite expensive if you are unmapping large ranges - each unmap means an rmap walk... Since this is a data corruption class of bug, let's fix it this way for now but I think we'll need to improve this later.
E.g. what if we called unmap_mapping_range() for the whole invalidated range after removing the radix tree entries?
Hum, but now thinking more about it I have hard time figuring out why write vs fault cannot actually still race:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
grab_mapping_entry()
- we add zero page in the radix tree & map it to page tables
Similarly read vs write fault may end up racing in a wrong way and try to replace already existing exceptional entry with a hole page?
Honza
Quoted 31 lines
fs/dax.c +7
-5+ +spin_lock_irq (&mapping->tree_lock);radix_tree_delete (page_tree, index); mapping->nrexceptional--; ret =1 ; out: -put_unlocked_mapping_entry (mapping, index, entry);spin_unlock_irq (&mapping->tree_lock); +dax_wake_mapping_entry_waiter (mapping, index, entry, true);return ret; }/* @@ -999,11 +1011,11 @@ dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,return -EIO ;/* -* Write can allocate block for an area which has a hole page mapped -* into page tables. We have to tear down these mappings so that data -* written by write(2) is visible in mmap. +* Write can allocate block for an area which has a hole page or zero +* PMD entry in the radix tree. We have to tear down these mappings so +* that data written by write(2) is visible in mmap. */ -if ((iomap->flags &IOMAP_F_NEW ) && inode->i_mapping->nrpages) { +if (iomap->flags &IOMAP_F_NEW ) {invalidate_inode_pages2_range (inode->i_mapping, pos >>PAGE_SHIFT , (end -1 ) >>PAGE_SHIFT );Signature · 1 line
2.9.3
Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]> SUSE Labs, CR
Re: [PATCH 2/2] dax: fix data corruption due to stale mmap reads
On Tue, Apr 25, 2017 at 01:10:43PM +0200, Jan Kara wrote:
<>
Quoted 17 lines
Hum, but now thinking more about it I have hard time figuring out why write vs fault cannot actually still race:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
grab_mapping_entry()
- we add zero page in the radix tree & map it to page tables
Quoted 2 lines
Similarly read vs write fault may end up racing in a wrong way and try to replace already existing exceptional entry with a hole page?
Yep, this race seems real to me, too. This seems very much like the issues that exist when a thread is doing direct I/O. One thread is doing I/O to an intermediate buffer (page cache for direct I/O case, zero page for us), and the other is going around it directly to media, and they can get out of sync.
IIRC the direct I/O code looked something like:
1/ invalidate existing mappings
2/ do direct I/O to media
3/ invalidate mappings again, just in case. Should be cheap if there weren't
any conflicting faults. This makes sure any new allocations we made are faulted in.
I guess one option would be to replicate that logic in the DAX I/O path, or we could try and enhance our locking so page faults can't race with I/O since both can allocate blocks.
I'm not sure, but will think on it.
Jan Kara
maintainerRe: [PATCH 2/2] dax: fix data corruption due to stale mmap reads
Quoted 27 lines · Ross Zwisler
On Tue, Apr 25, 2017 at 01:10:43PM +0200, Jan Kara wrote:
<>Quoted 17 lines
Hum, but now thinking more about it I have hard time figuring out why write vs fault cannot actually still race:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
grab_mapping_entry()
- we add zero page in the radix tree & map it to page tablesQuoted 2 lines
Similarly read vs write fault may end up racing in a wrong way and try to replace already existing exceptional entry with a hole page?
Yep, this race seems real to me, too. This seems very much like the issues that exist when a thread is doing direct I/O. One thread is doing I/O to an intermediate buffer (page cache for direct I/O case, zero page for us), and the other is going around it directly to media, and they can get out of sync.
Quoted 7 lines
IIRC the direct I/O code looked something like:
1/ invalidate existing mappings
2/ do direct I/O to media
3/ invalidate mappings again, just in case. Should be cheap if there weren't
any conflicting faults. This makes sure any new allocations we made are faulted in.
Yeah, the problem is people generally expect weird behavior when they mix direct and buffered IO (or let alone mmap) however everyone expects standard read(2) and write(2) to be completely coherent with mmap(2).
Quoted 3 lines
I guess one option would be to replicate that logic in the DAX I/O path, or we could try and enhance our locking so page faults can't race with I/O since both can allocate blocks.
In the abstract way, the problem is that we have radix tree (and page tables) cache block mapping information and the operation: "read block mapping information, store it in the radix tree" is not serialized in any way against other block allocations so the information we store can be out of date by the time we store it.
One way to solve this would be to move ->iomap_begin call in the fault paths under entry lock although that would mean I have to redo how ext4 handles DAX faults because with current code it would create lock inversion wrt transaction start.
Another solution would be to grab i_mmap_sem for write when doing write fault of a page and similarly have it grabbed for writing when doing write(2). This would scale rather poorly but if we later replaced it with a range lock (Davidlohr has already posted a nice implementation of it) it won't be as bad. But I guess option 1) is better...
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/2] dax: fix data corruption due to stale mmap reads
Quoted 36 lines · Jan Kara
Quoted 27 lines · Ross Zwisler
On Tue, Apr 25, 2017 at 01:10:43PM +0200, Jan Kara wrote:
<>Quoted 17 lines
Hum, but now thinking more about it I have hard time figuring out why write vs fault cannot actually still race:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
grab_mapping_entry()
- we add zero page in the radix tree & map it to page tablesQuoted 2 lines
Similarly read vs write fault may end up racing in a wrong way and try to replace already existing exceptional entry with a hole page?
Yep, this race seems real to me, too. This seems very much like the issues that exist when a thread is doing direct I/O. One thread is doing I/O to an intermediate buffer (page cache for direct I/O case, zero page for us), and the other is going around it directly to media, and they can get out of sync.
Quoted 7 lines
IIRC the direct I/O code looked something like:
1/ invalidate existing mappings
2/ do direct I/O to media
3/ invalidate mappings again, just in case. Should be cheap if there weren't
any conflicting faults. This makes sure any new allocations we made are faulted in.
Quoted 3 lines
Yeah, the problem is people generally expect weird behavior when they mix direct and buffered IO (or let alone mmap) however everyone expects standard read(2) and write(2) to be completely coherent with mmap(2).
Yep, fair enough.
Quoted 10 lines
Quoted 3 lines
I guess one option would be to replicate that logic in the DAX I/O path, or we could try and enhance our locking so page faults can't race with I/O since both can allocate blocks.
In the abstract way, the problem is that we have radix tree (and page tables) cache block mapping information and the operation: "read block mapping information, store it in the radix tree" is not serialized in any way against other block allocations so the information we store can be out of date by the time we store it.
Quoted 4 lines
One way to solve this would be to move ->iomap_begin call in the fault paths under entry lock although that would mean I have to redo how ext4 handles DAX faults because with current code it would create lock inversion wrt transaction start.
I don't think this alone is enough to save us. The I/O path doesn't currently take any DAX radix tree entry locks, so our race would just become:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() grab_mapping_entry() // newly moved ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
- we add zero page in the radix tree & map it to page tables
In their current form I don't think we want to take DAX radix tree entry locks in the I/O path because that would effectively serialize I/O over a given radix tree entry. For a 2MiB entry, for example, all I/O to that 2MiB range would be serialized.
Quoted 5 lines
Another solution would be to grab
i_mmap_semfor write when doing write fault of a page and similarly have it grabbed for writing when doing write(2). This would scale rather poorly but if we later replaced it with a range lock (Davidlohr has already posted a nice implementation of it) it won't be as bad. But I guess option 1) is better...
The best idea I had for handling this sounds similar, which would be to convert the radix tree locks to essentially be reader/writer locks. I/O and faults that don't modify the block mapping could just take read-level locks, and could all run concurrently. I/O or faults that modify a block mapping would take a write lock, and serialize with other writers and readers.
You could know if you needed a write lock without asking the filesystem - if you're a write and the radix tree entry is empty or is for a zero page, you grab the write lock.
This dovetails nicely with the idea of having the radix tree act as a cache for block mappings. You take the appropriate lock on the radix tree entry, and it has the block mapping info for your I/O or fault so you don't have to call into the FS. I/O would also participate so we would keep info about block mappings that we gather from I/O to help shortcut our page faults.
How does this sound vs the range lock idea? How hard do you think it would be to convert our current wait queue system to reader/writer style locking?
Also, how do you think we should deal with the current PMD corruption? Should we go with the current fix (I can augment the comments as you suggested), and then handle optimizations to that approach and the solution to this larger race as a follow-on?
Jan Kara
maintainerRe: [PATCH 2/2] dax: fix data corruption due to stale mmap reads
Quoted 75 lines · Ross Zwisler
Quoted 36 lines · Jan Kara
Quoted 27 lines · Ross Zwisler
On Tue, Apr 25, 2017 at 01:10:43PM +0200, Jan Kara wrote:
<>Quoted 17 lines
Hum, but now thinking more about it I have hard time figuring out why write vs fault cannot actually still race:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
grab_mapping_entry()
- we add zero page in the radix tree & map it to page tablesQuoted 2 lines
Similarly read vs write fault may end up racing in a wrong way and try to replace already existing exceptional entry with a hole page?
Yep, this race seems real to me, too. This seems very much like the issues that exist when a thread is doing direct I/O. One thread is doing I/O to an intermediate buffer (page cache for direct I/O case, zero page for us), and the other is going around it directly to media, and they can get out of sync.
Quoted 7 lines
IIRC the direct I/O code looked something like:
1/ invalidate existing mappings
2/ do direct I/O to media
3/ invalidate mappings again, just in case. Should be cheap if there weren't
any conflicting faults. This makes sure any new allocations we made are faulted in.Quoted 3 lines
Yeah, the problem is people generally expect weird behavior when they mix direct and buffered IO (or let alone mmap) however everyone expects standard read(2) and write(2) to be completely coherent with mmap(2).
Yep, fair enough.
Quoted 10 lines
Quoted 3 lines
I guess one option would be to replicate that logic in the DAX I/O path, or we could try and enhance our locking so page faults can't race with I/O since both can allocate blocks.
In the abstract way, the problem is that we have radix tree (and page tables) cache block mapping information and the operation: "read block mapping information, store it in the radix tree" is not serialized in any way against other block allocations so the information we store can be out of date by the time we store it.
Quoted 4 lines
One way to solve this would be to move ->iomap_begin call in the fault paths under entry lock although that would mean I have to redo how ext4 handles DAX faults because with current code it would create lock inversion wrt transaction start.
I don't think this alone is enough to save us. The I/O path doesn't currently take any DAX radix tree entry locks, so our race would just become:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() grab_mapping_entry() // newly moved ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
- we add zero page in the radix tree & map it to page tables
Quoted 4 lines
In their current form I don't think we want to take DAX radix tree entry locks in the I/O path because that would effectively serialize I/O over a given radix tree entry. For a 2MiB entry, for example, all I/O to that 2MiB range would be serialized.
Note that invalidate_inode_pages2_range() will see the entry created by grab_mapping_entry() on CPU2 and block waiting for its lock and this is exactly what stops the race. The invalidate_inode_pages2_range() effectively makes sure there isn't any page fault in progress for given range...
Also note that writes to a file are serialized by i_rwsem anyway (and at least serialization of writes to the overlapping range is required by POSIX) so this doesn't add any more serialization than we already have.
Quoted 11 lines
Quoted 5 lines
Another solution would be to grab
i_mmap_semfor write when doing write fault of a page and similarly have it grabbed for writing when doing write(2). This would scale rather poorly but if we later replaced it with a range lock (Davidlohr has already posted a nice implementation of it) it won't be as bad. But I guess option 1) is better...The best idea I had for handling this sounds similar, which would be to convert the radix tree locks to essentially be reader/writer locks. I/O and faults that don't modify the block mapping could just take read-level locks, and could all run concurrently. I/O or faults that modify a block mapping would take a write lock, and serialize with other writers and readers.
Well, this would be difficult to implement inside the radix tree (not enough bits in the entry) so you'd have to go for some external locking primitive anyway. And if you do that, read-write range lock Davidlohr has implemented is what you describe - well we could also have a radix tree with rwsems but I suspect the overhead of maintaining that would be too large. It would require larger rewrite than reusing entry locks as I suggest above though and it isn't an obvious performance win for realistic workloads either so I'd like to see some performance numbers before going that way. It likely improves a situation where processes race to fault the same page for which we already know the block mapping but I'm not sure if that translates to any measurable performance wins for workloads on DAX filesystem.
Quoted 10 lines
You could know if you needed a write lock without asking the filesystem - if you're a write and the radix tree entry is empty or is for a zero page, you grab the write lock.
This dovetails nicely with the idea of having the radix tree act as a cache for block mappings. You take the appropriate lock on the radix tree entry, and it has the block mapping info for your I/O or fault so you don't have to call into the FS. I/O would also participate so we would keep info about block mappings that we gather from I/O to help shortcut our page faults.
Quoted 7 lines
How does this sound vs the range lock idea? How hard do you think it would be to convert our current wait queue system to reader/writer style locking?
Also, how do you think we should deal with the current PMD corruption? Should we go with the current fix (I can augment the comments as you suggested), and then handle optimizations to that approach and the solution to this larger race as a follow-on?
So for now I'm still more inclined to just stay with the radix tree lock as is and just fix up the locking as I suggest and go for larger rewrite only if we can demonstrate further performance wins.
WRT your second patch, if we go with the locking as I suggest, it is enough to unmap the whole range after invalidate_inode_pages2() has cleared radix tree entries (*) which will be much cheaper (for large writes) than doing unmapping entry by entry. So I'd go for that. I'll prepare a patch for the locking change - it will require changes to ext4 transaction handling so it won't be completely trivial.
(*) The flow of information is: filesystem block mapping info -> radix tree -> page tables so if 'filesystem block mapping info' changes, we should go invalidate corresponding radix tree entries (new entries will already have uptodate info) and then invalidate corresponding page tables (again once radix tree has no stale entries, we are sure new page table entries will be uptodate).
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/2] dax: fix data corruption due to stale mmap reads
Quoted 1 line · Jan Kara
On Wed 26-04-17 16:52:36, Ross Zwisler wrote:
<>
Quoted 22 lines
Quoted 17 lines
I don't think this alone is enough to save us. The I/O path doesn't currently take any DAX radix tree entry locks, so our race would just become:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() grab_mapping_entry() // newly moved ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
- we add zero page in the radix tree & map it to page tablesQuoted 4 lines
In their current form I don't think we want to take DAX radix tree entry locks in the I/O path because that would effectively serialize I/O over a given radix tree entry. For a 2MiB entry, for example, all I/O to that 2MiB range would be serialized.
Quoted 5 lines
Note that
invalidate_inode_pages2_range()will see the entry created bygrab_mapping_entry()on CPU2 and block waiting for its lock and this is exactly what stops the race. Theinvalidate_inode_pages2_range()effectively makes sure there isn't any page fault in progress for given range...
Yep, this is the bit that I was missing. Thanks.
Quoted 42 lines
Also note that writes to a file are serialized by
i_rwsemanyway (and at least serialization of writes to the overlapping range is required by POSIX) so this doesn't add any more serialization than we already have.Quoted 11 lines
Quoted 5 lines
Another solution would be to grab
i_mmap_semfor write when doing write fault of a page and similarly have it grabbed for writing when doing write(2). This would scale rather poorly but if we later replaced it with a range lock (Davidlohr has already posted a nice implementation of it) it won't be as bad. But I guess option 1) is better...The best idea I had for handling this sounds similar, which would be to convert the radix tree locks to essentially be reader/writer locks. I/O and faults that don't modify the block mapping could just take read-level locks, and could all run concurrently. I/O or faults that modify a block mapping would take a write lock, and serialize with other writers and readers.
Well, this would be difficult to implement inside the radix tree (not enough bits in the entry) so you'd have to go for some external locking primitive anyway. And if you do that, read-write range lock Davidlohr has implemented is what you describe - well we could also have a radix tree with rwsems but I suspect the overhead of maintaining that would be too large. It would require larger rewrite than reusing entry locks as I suggest above though and it isn't an obvious performance win for realistic workloads either so I'd like to see some performance numbers before going that way. It likely improves a situation where processes race to fault the same page for which we already know the block mapping but I'm not sure if that translates to any measurable performance wins for workloads on DAX filesystem.
Quoted 13 lines
You could know if you needed a write lock without asking the filesystem - if you're a write and the radix tree entry is empty or is for a zero page, you grab the write lock.
This dovetails nicely with the idea of having the radix tree act as a cache for block mappings. You take the appropriate lock on the radix tree entry, and it has the block mapping info for your I/O or fault so you don't have to call into the FS. I/O would also participate so we would keep info about block mappings that we gather from I/O to help shortcut our page faults.
How does this sound vs the range lock idea? How hard do you think it would be to convert our current wait queue system to reader/writer style locking?
Quoted 8 lines
Quoted 4 lines
Also, how do you think we should deal with the current PMD corruption? Should we go with the current fix (I can augment the comments as you suggested), and then handle optimizations to that approach and the solution to this larger race as a follow-on?
So for now I'm still more inclined to just stay with the radix tree lock as is and just fix up the locking as I suggest and go for larger rewrite only if we can demonstrate further performance wins.
Sounds good.
Quoted 4 lines
WRT your second patch, if we go with the locking as I suggest, it is enough to unmap the whole range after
invalidate_inode_pages2()has cleared radix tree entries (*) which will be much cheaper (for large writes) than doing unmapping entry by entry.
I'm still not convinced that it is safe to do the unmap in a separate step. I see your point about it being expensive to do a rmap walk to unmap each entry in __dax_invalidate_mapping_entry(), but I think we might need to because the unmap is part of the contract imposed by invalidate_inode_pages2_range() and invalidate_inode_pages2(). This exists in the header comment above each:
* Any pages which are found to be mapped into pagetables are unmapped prior
* to invalidation.
If you look at the usage of invalidate_inode_pages2_range() in generic_file_direct_write() for example (which I realize we won't call for a DAX inode, but still), I think that it really does rely on the fact that invalidated pages are unmapped, right? If it didn't, and hole pages were mapped, the hole pages could remain mapped while a direct I/O write allocated blocks and then wrote real data.
If we really want to unmap the entire range at once, maybe it would have to be done in invalidate_inode_pages2_range(), after the loop? My hesitation about this is that we'd be leaking yet more DAX special casing up into the mm/truncate.c code.
Or am I missing something?
Quoted 15 lines
So I'd go for that. I'll prepare a patch for the
locking change - it will require changes to ext4 transaction handling so it won't be completely trivial.(*) The flow of information is: filesystem block mapping info -> radix tree -> page tables so if 'filesystem block mapping info' changes, we should go invalidate corresponding radix tree entries (new entries will already have uptodate info) and then invalidate corresponding page tables (again once radix tree has no stale entries, we are sure new page table entries will be uptodate).
Honza
Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]> SUSE Labs, CR
Jan Kara
maintainerRe: [PATCH 2/2] dax: fix data corruption due to stale mmap reads
Quoted 27 lines · Ross Zwisler
Quoted 3 lines
So for now I'm still more inclined to just stay with the radix tree lock as is and just fix up the locking as I suggest and go for larger rewrite only if we can demonstrate further performance wins.
Sounds good.
Quoted 4 lines
WRT your second patch, if we go with the locking as I suggest, it is enough to unmap the whole range after
invalidate_inode_pages2()has cleared radix tree entries (*) which will be much cheaper (for large writes) than doing unmapping entry by entry.I'm still not convinced that it is safe to do the unmap in a separate step. I see your point about it being expensive to do a rmap walk to unmap each entry in
__dax_invalidate_mapping_entry(), but I think we might need to because the unmap is part of the contract imposed byinvalidate_inode_pages2_range()andinvalidate_inode_pages2(). This exists in the header comment above each:* Any pages which are found to be mapped into pagetables are unmapped prior
* to invalidation.If you look at the usage of
invalidate_inode_pages2_range()ingeneric_file_direct_write()for example (which I realize we won't call for a DAX inode, but still), I think that it really does rely on the fact that invalidated pages are unmapped, right? If it didn't, and hole pages were mapped, the hole pages could remain mapped while a direct I/O write allocated blocks and then wrote real data.
Quoted 6 lines
If we really want to unmap the entire range at once, maybe it would have to be done in
invalidate_inode_pages2_range(), after the loop? My hesitation about this is that we'd be leaking yet more DAX special casing up into themm/truncate.ccode.Or am I missing something?
No, my thinking was to put the invalidation at the end of invalidate_inode_pages2_range(). I agree it means more special-casing for DAX in mm/truncate.c.
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/2] dax: fix data corruption due to stale mmap reads
In reply to Jan KaraQuoted 103 lines · Jan Kara
Quoted 75 lines · Ross Zwisler
Quoted 36 lines · Jan Kara
Quoted 27 lines · Ross Zwisler
On Tue, Apr 25, 2017 at 01:10:43PM +0200, Jan Kara wrote:
<>Quoted 17 lines
Hum, but now thinking more about it I have hard time figuring out why write vs fault cannot actually still race:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault() ->iomap_begin() - sees hole
dax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
grab_mapping_entry()
- we add zero page in the radix tree & map it to page tablesQuoted 2 lines
Similarly read vs write fault may end up racing in a wrong way and try to replace already existing exceptional entry with a hole page?
Yep, this race seems real to me, too. This seems very much like the issues that exist when a thread is doing direct I/O. One thread is doing I/O to an intermediate buffer (page cache for direct I/O case, zero page for us), and the other is going around it directly to media, and they can get out of sync.
Quoted 7 lines
IIRC the direct I/O code looked something like:
1/ invalidate existing mappings
2/ do direct I/O to media
3/ invalidate mappings again, just in case. Should be cheap if there weren't
any conflicting faults. This makes sure any new allocations we made are faulted in.Quoted 3 lines
Yeah, the problem is people generally expect weird behavior when they mix direct and buffered IO (or let alone mmap) however everyone expects standard read(2) and write(2) to be completely coherent with mmap(2).
Yep, fair enough.
Quoted 10 lines
Quoted 3 lines
I guess one option would be to replicate that logic in the DAX I/O path, or we could try and enhance our locking so page faults can't race with I/O since both can allocate blocks.
In the abstract way, the problem is that we have radix tree (and page tables) cache block mapping information and the operation: "read block mapping information, store it in the radix tree" is not serialized in any way against other block allocations so the information we store can be out of date by the time we store it.
Quoted 4 lines
One way to solve this would be to move ->iomap_begin call in the fault paths under entry lock although that would mean I have to redo how ext4 handles DAX faults because with current code it would create lock inversion wrt transaction start.
I don't think this alone is enough to save us. The I/O path doesn't currently take any DAX radix tree entry locks, so our race would just become:
CPU1 - write(2) CPU2 - read fault
dax_iomap_pte_fault()
grab_mapping_entry()// newly moved ->iomap_begin() - sees holedax_iomap_rw()
iomap_apply()
->iomap_begin - allocates blocks
dax_iomap_actor()
invalidate_inode_pages2_range()
- there's nothing to invalidate
- we add zero page in the radix tree & map it to page tablesQuoted 4 lines
In their current form I don't think we want to take DAX radix tree entry locks in the I/O path because that would effectively serialize I/O over a given radix tree entry. For a 2MiB entry, for example, all I/O to that 2MiB range would be serialized.
Note that
invalidate_inode_pages2_range()will see the entry created bygrab_mapping_entry()on CPU2 and block waiting for its lock and this is exactly what stops the race. Theinvalidate_inode_pages2_range()effectively makes sure there isn't any page fault in progress for given range...Also note that writes to a file are serialized by
i_rwsemanyway (and at least serialization of writes to the overlapping range is required by POSIX) so this doesn't add any more serialization than we already have.Quoted 11 lines
Quoted 5 lines
Another solution would be to grab
i_mmap_semfor write when doing write fault of a page and similarly have it grabbed for writing when doing write(2). This would scale rather poorly but if we later replaced it with a range lock (Davidlohr has already posted a nice implementation of it) it won't be as bad. But I guess option 1) is better...The best idea I had for handling this sounds similar, which would be to convert the radix tree locks to essentially be reader/writer locks. I/O and faults that don't modify the block mapping could just take read-level locks, and could all run concurrently. I/O or faults that modify a block mapping would take a write lock, and serialize with other writers and readers.
Quoted 12 lines
Well, this would be difficult to implement inside the radix tree (not enough bits in the entry) so you'd have to go for some external locking primitive anyway. And if you do that, read-write range lock Davidlohr has implemented is what you describe - well we could also have a radix tree with rwsems but I suspect the overhead of maintaining that would be too large. It would require larger rewrite than reusing entry locks as I suggest above though and it isn't an obvious performance win for realistic workloads either so I'd like to see some performance numbers before going that way. It likely improves a situation where processes race to fault the same page for which we already know the block mapping but I'm not sure if that translates to any measurable performance wins for workloads on DAX filesystem.
I'm also concerned about inventing new / fancy radix infrastructure when we're already in the space of needing struct page for any non-trivial usage of dax. As Kirill's transparent-huge-page page cache implementation matures I'd be interested in looking at a transition path away from radix locking towards something that it shared with the common case page cache locking.
Re: [PATCH 1/2] dax: prevent invalidation of mapped DAX entries
In reply to Ross ZwislerQuoted 22 lines · Ross Zwisler
dax_invalidate_mapping_entry()currently removes DAX exceptional entries only if they are clean and unlocked. This is done via:
invalidate_mapping_pages()
invalidate_exceptional_entry()
dax_invalidate_mapping_entry()However, for page cache pages removed in
invalidate_mapping_pages()there is an additional criteria which is that the page must not be mapped. This is noted in the comments aboveinvalidate_mapping_pages()and is checked ininvalidate_inode_page().For DAX entries this means that we can can end up in a situation where a DAX exceptional entry, either a huge zero page or a regular DAX entry, could end up mapped but without an associated radix tree entry. This is inconsistent with the rest of the DAX code and with what happens in the page cache case.
We aren't able to unmap the DAX exceptional entry because according to its comments
invalidate_mapping_pages()isn't allowed to block, andunmap_mapping_range()takes a write lock on themapping->i_mmap_rwsem.
Quoted 7 lines
Since we essentially never have unmapped DAX entries to evict from the radix tree, just remove
dax_invalidate_mapping_entry().Fixes c6dcf52c23d2 ("mm: Invalidate DAX radix tree entries only if appropriate")Reported-by Jan Kara <[email protected]>Cc <[email protected]> [4.10+]
Just as a side note - we wouldn't really have to unmap the mapping range covered by the DAX exceptional entry. It would be enough to find out whether such range is mapped and bail out in that case. But that would still be pretty expensive for DAX - we'd have to do rmap walk similar as in dax_mapping_entry_mkclean() and IMHO it is not worth it. So I agree with what you did. You can add:
Honza
Quoted 93 lines
---
This series applies cleanly to the current v4.11-rc7 based linux/master, and has passed an xfstests run with DAX on ext4 and XFS.
These patches also apply to v4.10.9 with a little work from the 3-way merge feature.
fs/dax.c | 29 ----------------------------- include/linux/dax.h | 1 - mm/truncate.c | 9 +++------ 3 files changed, 3 insertions(+), 36 deletions(-)fs/dax.c +0
-29@@ -507,35 +507,6 @@ int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) }/* -* Invalidate exceptional DAX entry if easily possible. This handles DAX -* entries for invalidate_inode_pages() so we evict the entry only if we can -* do so without blocking. -*/ -int dax_invalidate_mapping_entry (struct address_space *mapping,pgoff_t index) -{ -int ret =0 ; -void *entry, **slot; -struct radix_tree_root *page_tree = &mapping->page_tree; - -spin_lock_irq (&mapping->tree_lock); - entry =__radix_tree_lookup (page_tree, index,NULL , &slot); -if (!entry || !radix_tree_exceptional_entry (entry) || -slot_locked (mapping, slot)) - goto out; -if (radix_tree_tag_get (page_tree, index,PAGECACHE_TAG_DIRTY ) || -radix_tree_tag_get (page_tree, index,PAGECACHE_TAG_TOWRITE )) - goto out; -radix_tree_delete (page_tree, index); - mapping->nrexceptional--; - ret =1 ; -out: -spin_unlock_irq (&mapping->tree_lock); -if (ret) -dax_wake_mapping_entry_waiter (mapping, index, entry, true); -return ret; -} - -/* * Invalidate exceptional DAX entry if it is clean. */ int dax_invalidate_mapping_entry_sync(struct address_space *mapping,include/linux/dax.h +0
-1@@ -41,7 +41,6 @@ ssize_t dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,int dax_iomap_fault (struct vm_fault *vmf,enum page_entry_size pe_size,const struct iomap_ops *ops);int dax_delete_mapping_entry (struct address_space *mapping,pgoff_t index); -int dax_invalidate_mapping_entry (struct address_space *mapping,pgoff_t index);int dax_invalidate_mapping_entry_sync (struct address_space *mapping,pgoff_t index); void dax_wake_mapping_entry_waiter(struct address_space *mapping,mm/truncate.c +3
-6@@ -67,17 +67,14 @@ static void truncate_exceptional_entry(struct address_space *mapping,/* * Invalidate exceptional entry if easily possible. This handles exceptional -* entries for invalidate_inode_pages() so for DAX it evicts only unlocked and -* clean entries. +* entries for invalidate_inode_pages(). */ static int invalidate_exceptional_entry (struct address_space *mapping,pgoff_t index,void *entry) { -/* Handled by shmem itself */ -if (shmem_mapping (mapping)) +/* Handled by shmem itself, or for DAX we do nothing. */ +if (shmem_mapping (mapping) ||dax_mapping (mapping))return 1 ; -if (dax_mapping (mapping)) -return dax_invalidate_mapping_entry (mapping, index);clear_shadow_entry (mapping, index, entry);return 1 ; }Signature · 1 line
2.9.3
Signature · 2 lines
Jan Kara <jack-IBi9RG/[email protected]> SUSE Labs, CR
Re: [PATCH 1/2] dax: prevent invalidation of mapped DAX entries
Quoted 31 lines · Jan Kara
Quoted 22 lines · Ross Zwisler
dax_invalidate_mapping_entry()currently removes DAX exceptional entries only if they are clean and unlocked. This is done via:
invalidate_mapping_pages()
invalidate_exceptional_entry()
dax_invalidate_mapping_entry()However, for page cache pages removed in
invalidate_mapping_pages()there is an additional criteria which is that the page must not be mapped. This is noted in the comments aboveinvalidate_mapping_pages()and is checked ininvalidate_inode_page().For DAX entries this means that we can can end up in a situation where a DAX exceptional entry, either a huge zero page or a regular DAX entry, could end up mapped but without an associated radix tree entry. This is inconsistent with the rest of the DAX code and with what happens in the page cache case.
We aren't able to unmap the DAX exceptional entry because according to its comments
invalidate_mapping_pages()isn't allowed to block, andunmap_mapping_range()takes a write lock on themapping->i_mmap_rwsem.Quoted 7 lines
Since we essentially never have unmapped DAX entries to evict from the radix tree, just remove
dax_invalidate_mapping_entry().Fixes c6dcf52c23d2 ("mm: Invalidate DAX radix tree entries only if appropriate")Reported-by Jan Kara <[email protected]>Cc <[email protected]> [4.10+]
Quoted 8 lines
Just as a side note - we wouldn't really have to unmap the mapping range covered by the DAX exceptional entry. It would be enough to find out whether such range is mapped and bail out in that case. But that would still be pretty expensive for DAX - we'd have to do rmap walk similar as in
dax_mapping_entry_mkclean()and IMHO it is not worth it. So I agree with what you did. You can add:Reviewed-by Jan Kara <[email protected]>
Yep, that makes sense. Thanks for the review.
Andrew Morton
maintainerRe: [PATCH 1/4] fs: fix data invalidation in the cleancache during direct IO
In reply to Andrey RyabininQuoted 10 lines · On Fri, 14 Apr 2017 17:07:50 +0300 Andrey Ryabinin…
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
I'm not understanding this. I can buy the argument about nrexceptional, but why does cleancache require the invalidate_inode_pages2_range) call even when ->nrpages is zero?
I *assume* it's because invalidate_inode_pages2_range() calls cleancache_invalidate_inode(), yes? If so, can we please add this to the changelog? If not then please explain further.
Re: [PATCH 1/4] fs: fix data invalidation in the cleancache during direct IO
Quoted 13 lines · Andrew Morton
Quoted 10 lines · On Fri, 14 Apr 2017 17:07:50 +0300 Andrey Ryabinin <[email protected]>…
Some direct write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. If page cache is empty, buffered read following after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Quoted 8 lines
I'm not understanding this. I can buy the argument about nrexceptional, but why does cleancache require the
invalidate_inode_pages2_range) call even when ->nrpages is zero?I *assume* it's because
invalidate_inode_pages2_range()callscleancache_invalidate_inode(), yes? If so, can we please add this to the changelog? If not then please explain further.
Yes, your assumption is correct. I'll fix the changelog.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/4 fs/block_dev: always invalidate cleancache in invalidate_bdev()
In reply to Andrey Ryabinininvalidate_bdev() calls cleancache_invalidate_inode() iff ->nrpages != 0 which doen't make any sense.
Make invalidate_bdev() always invalidate cleancache data.
--- fs/block_dev.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
fs/block_dev.c +5 -6
@@ -103,12 +103,11 @@ void invalidate_bdev(struct block_device *bdev) {struct address_space *mapping = bdev->bd_inode->i_mapping; -if (mapping->nrpages ==0 ) -return ; - -invalidate_bh_lrus (); -lru_add_drain_all ();/* make sure all lru add caches are flushed */ -invalidate_mapping_pages (mapping,0 ,-1 ); +if (mapping->nrpages) { +invalidate_bh_lrus (); +lru_add_drain_all ();/* make sure all lru add caches are flushed */ +invalidate_mapping_pages (mapping,0 ,-1 ); + }/* 99% of the time, we don't need to flush the cleancache on the bdev. * But, for the strange corners, lets be cautious */
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/4] fs/block_dev: always invalidate cleancache in invalidate_bdev()
Quoted 19 lines · Andrey Ryabinin
invalidate_bdev()callscleancache_invalidate_inode()iff ->nrpages != 0 which doen't make any sense.
Makeinvalidate_bdev()always invalidate cleancache data.Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>--- fs/block_dev.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)fs/block_dev.c +0
-1@@ -103,12 +103,11 @@ void invalidate_bdev(struct block_device *bdev) {struct address_space *mapping = bdev->bd_inode->i_mapping; -if (mapping->nrpages ==0 )
fs/block_dev.c void invalidate_bdev(struct block_device *bdev) · line 110
Quoted 10 lines
fs/block_dev.c +5
-5-return ; - -invalidate_bh_lrus (); -lru_add_drain_all ();/* make sure all lru add caches are flushed */ -invalidate_mapping_pages (mapping,0 ,-1 ); +if (mapping->nrpages) { +invalidate_bh_lrus (); +lru_add_drain_all ();/* make sure all lru add caches are flushed */ +invalidate_mapping_pages (mapping,0 ,-1 ); + }
How is this different than the current code? You will only invalidate the mapping iff ->nrpages > 0 ( I assume it can't go down below 0) ? Perhaps just remove the if altogether?
fs/block_dev.c void invalidate_bdev(struct block_device *bdev) · line 113
Quoted 4 lines
fs/block_dev.c +0
-0/* 99% of the time, we don't need to flush the cleancache on the bdev. * But, for the strange corners, lets be cautious */
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/4] fs/block_dev: always invalidate cleancache in invalidate_bdev()
Quoted 21 lines · Nikolay Borisov
Quoted 18 lines · Andrey Ryabinin
invalidate_bdev()callscleancache_invalidate_inode()iff ->nrpages != 0 which doen't make any sense.
Makeinvalidate_bdev()always invalidate cleancache data.Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>--- fs/block_dev.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)fs/block_dev.c +0
-0@@ -103,12 +103,11 @@ void invalidate_bdev(struct block_device *bdev) {struct address_space *mapping = bdev->bd_inode->i_mapping;
Quoted 14 lines
fs/block_dev.c void invalidate_bdev(struct block_device *bdev) · line 110
Quoted 11 lines
fs/block_dev.c +5
-6-if (mapping->nrpages ==0 ) -return ; - -invalidate_bh_lrus (); -lru_add_drain_all ();/* make sure all lru add caches are flushed */ -invalidate_mapping_pages (mapping,0 ,-1 ); +if (mapping->nrpages) { +invalidate_bh_lrus (); +lru_add_drain_all ();/* make sure all lru add caches are flushed */ +invalidate_mapping_pages (mapping,0 ,-1 ); + }How is this different than the current code? You will only invalidate the mapping iff ->nrpages > 0 ( I assume it can't go down below 0) ?
The difference is that invalidate_bdev() now always calls cleancache_invalidate_inode() (you won't see it in this diff, it's placed after this if(mapping->nrpages){} block,)
Quoted 2 lines
Perhaps just remove the if altogether?
Given that invalidate_mapping_pages() invalidates exceptional entries as well, it certainly doesn't look right that we look only at mapping->nrpages and completely ignore ->nrexceptional.
So maybe removing if() would be a right thing to do. But I think that should be a separate patch as it would fix a another bug probably introduced by commit 91b0abe36a7b ("mm + fs: store shadow entries in page cache")
My intention here was to fix only cleancache case.
Quoted 4 lines
fs/block_dev.c void invalidate_bdev(struct block_device *bdev) · line 113
Quoted 4 lines
fs/block_dev.c +0
-0/* 99% of the time, we don't need to flush the cleancache on the bdev. * But, for the strange corners, lets be cautious */
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 3/4 mm/truncate: bail out early from invalidate_inode_pages2_range() if mapping is empty
In reply to Andrey RyabininIf mapping is empty (both ->nrpages and ->nrexceptional is zero) we can avoid pointless lookups in empty radix tree and bail out immediately after cleancache invalidation.
--- mm/truncate.c | 3 +++ 1 file changed, 3 insertions(+)
mm/truncate.c +3 -0
@@ -624,6 +624,9 @@ int invalidate_inode_pages2_range(struct address_space *mapping,int did_range_unmap =0 ;cleancache_invalidate_inode (mapping); +if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) +return 0 ; +pagevec_init (&pvec,0 ); index = start; while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 4/4 mm/truncate: avoid pointless cleancache_invalidate_inode() calls.
In reply to Andrey Ryabinincleancache_invalidate_inode() called truncate_inode_pages_range() and invalidate_inode_pages2_range() twice - on entry and on exit. It's stupid and waste of time. It's enough to call it once at exit.
--- mm/truncate.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
mm/truncate.c +7 -5
@@ -266,9 +266,8 @@ void truncate_inode_pages_range(struct address_space *mapping,pgoff_t index;int i; -cleancache_invalidate_inode (mapping);if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) -return ; + goto out;/* Offsets within partial pages */ partial_start = lstart & (PAGE_SIZE -1 );
@@ -363,7 +362,7 @@ void truncate_inode_pages_range(struct address_space *mapping,* will be released, just zeroed, so we can bail out now. */ if (start >= end) -return ; + goto out; index = start;for ( ; ; ) {
@@ -410,6 +409,8 @@ void truncate_inode_pages_range(struct address_space *mapping,pagevec_release (&pvec); index++; } + +out:cleancache_invalidate_inode (mapping); }EXPORT_SYMBOL (truncate_inode_pages_range);
@@ -623,9 +624,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping,int ret2 =0 ;int did_range_unmap =0 ; -cleancache_invalidate_inode (mapping);if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) -return 0 ; + goto out;pagevec_init (&pvec,0 ); index = start;
@@ -689,6 +689,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping,cond_resched (); index++; } + +out:cleancache_invalidate_inode (mapping);return ret; }
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 2 lines · Andrey Ryabinin
We've noticed that after direct IO write, buffered read sometimes gets stale data which is coming from the cleancache.
That is not good.
Quoted 5 lines
The reason for this is that some direct write hooks call call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero, so we may not invalidate
data in the cleancache.Another odd thing is that we check only for ->nrpages and don't check for ->nrexceptional,
Yikes.
Quoted 9 lines
but invalidate_inode_pages2[_range] also invalidates exceptional entries as well. So we invalidate exceptional entries only if ->nrpages != 0? This doesn't feel right.
- Patch 1 fixes direct IO writes by removing ->nrpages check.
- Patch 2 fixes similar case ininvalidate_bdev().
Note: I only fixed conditionalcleancache_invalidate_inode()here.
Do we also need to add ->nrexceptional check in intoinvalidate_bdev()?- Patches 3-4: some optimizations.
Thanks!
Quoted 22 lines
Andrey Ryabinin (4):
fs: fix data invalidation in the cleancache during direct IOfs/block_dev: always invalidate cleancache ininvalidate_bdev()mm/truncate: bail out early frominvalidate_inode_pages2_range()if
mapping is empty
mm/truncate: avoid pointlesscleancache_invalidate_inode()calls.fs/9p/vfs_file.c | 2 +- fs/block_dev.c | 11 +++++------ fs/cifs/inode.c | 2 +- fs/dax.c | 2 +- fs/iomap.c | 16 +++++++--------- fs/nfs/direct.c | 6 ++---- fs/nfs/inode.c | 8 +++++--- mm/filemap.c | 26 +++++++++++--------------- mm/truncate.c | 13 +++++++++---- 9 files changed, 42 insertions(+), 44 deletions(-)Signature · 1 line
2.10.2
v2 cover Properly invalidate data in the cleancache.
In reply to Andrey RyabininChanges since v1:
- Exclude DAX/nfs/cifs/9p hunks from the first patch. All these fs'es doesn't call cleancache_get_page() (nor directly, nor via mpage_readpage[s]()), so they are not affected by this bug.
- Updated changelog.
We've noticed that after direct IO write, buffered read sometimes gets
stale data which is coming from the cleancache.
The reason for this is that some direct write hooks call call invalidate_inode_pages2[_range]() conditionally iff mapping->nrpages is not zero, so we may not invalidate
data in the cleancache.
Another odd thing is that we check only for ->nrpages and don't check for ->nrexceptional, but invalidate_inode_pages2[_range] also invalidates exceptional entries as well. So we invalidate exceptional entries only if ->nrpages != 0? This doesn't feel right.
- Patch 1 fixes direct IO writes by removing ->nrpages check.
- Patch 2 fixes similar case in invalidate_bdev().
Note: I only fixed conditional cleancache_invalidate_inode() here.
Do we also need to add ->nrexceptional check in into invalidate_bdev()?
- Patches 3-4: some optimizations.
Andrey Ryabinin (4):
fs: fix data invalidation in the cleancache during direct IO fs/block_dev: always invalidate cleancache in invalidate_bdev() mm/truncate: bail out early from invalidate_inode_pages2_range() if
mapping is empty
mm/truncate: avoid pointless cleancache_invalidate_inode() calls.
fs/block_dev.c | 11 +++++------ fs/iomap.c | 18 ++++++++---------- mm/filemap.c | 26 +++++++++++--------------- mm/truncate.c | 13 +++++++++---- 4 files changed, 33 insertions(+), 35 deletions(-)
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 1/4 fs: fix data invalidation in the cleancache during direct IO
Some direct IO write fs hooks call invalidate_inode_pages2[_range]() conditionally iff mapping->nrpages is not zero. This can't be right, because invalidate_inode_pages2[_ragne]() also invalidate data in the cleancache via cleancache_invalidate_inode() call.
So if page cache is empty but there is some data in the cleancache, buffered read after direct IO write would get stale data from the cleancache.
Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Note: nfs,cifs,9p doesn't need similar fix because the never call cleancache_get_page() (nor directly, nor via mpage_readpage[s]()), so they are not affected by this bug.
--- fs/iomap.c | 18 ++++++++---------- mm/filemap.c | 26 +++++++++++--------------- 2 files changed, 19 insertions(+), 25 deletions(-)
fs/iomap.c +8 -10
@@ -881,16 +881,14 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, flags |=IOMAP_WRITE ; } -if (mapping->nrpages) { - ret =filemap_write_and_wait_range (mapping, start, end); -if (ret) - goto out_free_dio; + ret =filemap_write_and_wait_range (mapping, start, end); +if (ret) + goto out_free_dio; - ret =invalidate_inode_pages2_range (mapping, - start >>PAGE_SHIFT , end >>PAGE_SHIFT ); -WARN_ON_ONCE (ret); - ret =0 ; - } + ret =invalidate_inode_pages2_range (mapping, + start >>PAGE_SHIFT , end >>PAGE_SHIFT ); +WARN_ON_ONCE (ret); + ret =0 ;inode_dio_begin (inode);
@@ -945,7 +943,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,* one is a pretty crazy thing to do, so we don't support it 100%. If * this invalidation fails, tough, the write still worked... */ -if (iov_iter_rw (iter) ==WRITE && mapping->nrpages) { +if (iov_iter_rw (iter) ==WRITE ) {int err =invalidate_inode_pages2_range (mapping, start >>PAGE_SHIFT , end >>PAGE_SHIFT );WARN_ON_ONCE (err);
mm/filemap.c +11 -15
@@ -2720,18 +2720,16 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)* about to write. We do this *before* the write so that we can return * without clobbering -EIOCBQUEUED from ->direct_IO(). */ -if (mapping->nrpages) { - written =invalidate_inode_pages2_range (mapping, + written =invalidate_inode_pages2_range (mapping, pos >>PAGE_SHIFT , end); -/* -* If a page can not be invalidated, return 0 to fall back -* to buffered write. -*/ -if (written) { -if (written == -EBUSY ) -return 0 ; - goto out; - } +/* +* If a page can not be invalidated, return 0 to fall back +* to buffered write. +*/ +if (written) { +if (written == -EBUSY ) +return 0 ; + goto out; } written = mapping->a_ops->direct_IO (iocb, from);
@@ -2744,10 +2742,8 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)* so we don't support it 100%. If this invalidation * fails, tough, the write still worked... */ -if (mapping->nrpages) { -invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end); - } +invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end);if (written >0 ) { pos += written;
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2 1/4] fs: fix data invalidation in the cleancache during direct IO
Quoted 14 lines · Andrey Ryabinin
Some direct IO write fs hooks call invalidate_inode_pages2[_range]() conditionally iff
mapping->nrpagesis not zero. This can't be right, because invalidate_inode_pages2[_ragne]() also invalidate data in the cleancache viacleancache_invalidate_inode()call.
So if page cache is empty but there is some data in the cleancache, buffered read after direct IO write would get stale data from the cleancache.Also it doesn't feel right to check only for ->nrpages because invalidate_inode_pages2[_range] invalidates exceptional entries as well.
Fix this by calling invalidate_inode_pages2[_range]() regardless of nrpages state.
Quoted 8 lines
Note: nfs,cifs,9p doesn't need similar fix because the never call
cleancache_get_page()(nor directly, nor via mpage_readpage[s]()), so they are not affected by this bug.Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>Cc <[email protected]>
OK, looks good. You can add:
Honza
Quoted 90 lines
--- fs/iomap.c | 18 ++++++++---------- mm/filemap.c | 26 +++++++++++--------------- 2 files changed, 19 insertions(+), 25 deletions(-)fs/iomap.c +8
-10@@ -881,16 +881,14 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, flags |=IOMAP_WRITE ; } -if (mapping->nrpages) { - ret =filemap_write_and_wait_range (mapping, start, end); -if (ret) - goto out_free_dio; + ret =filemap_write_and_wait_range (mapping, start, end); +if (ret) + goto out_free_dio; - ret =invalidate_inode_pages2_range (mapping, - start >>PAGE_SHIFT , end >>PAGE_SHIFT ); -WARN_ON_ONCE (ret); - ret =0 ; - } + ret =invalidate_inode_pages2_range (mapping, + start >>PAGE_SHIFT , end >>PAGE_SHIFT ); +WARN_ON_ONCE (ret); + ret =0 ;inode_dio_begin (inode);@@ -945,7 +943,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,* one is a pretty crazy thing to do, so we don't support it 100%. If * this invalidation fails, tough, the write still worked... */ -if (iov_iter_rw (iter) ==WRITE && mapping->nrpages) { +if (iov_iter_rw (iter) ==WRITE ) {int err =invalidate_inode_pages2_range (mapping, start >>PAGE_SHIFT , end >>PAGE_SHIFT );WARN_ON_ONCE (err);mm/filemap.c +11
-15@@ -2720,18 +2720,16 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)* about to write. We do this *before* the write so that we can return * without clobbering -EIOCBQUEUED from ->direct_IO(). */ -if (mapping->nrpages) { - written =invalidate_inode_pages2_range (mapping, + written =invalidate_inode_pages2_range (mapping, pos >>PAGE_SHIFT , end); -/* -* If a page can not be invalidated, return 0 to fall back -* to buffered write. -*/ -if (written) { -if (written == -EBUSY ) -return 0 ; - goto out; - } +/* +* If a page can not be invalidated, return 0 to fall back +* to buffered write. +*/ +if (written) { +if (written == -EBUSY ) +return 0 ; + goto out; } written = mapping->a_ops->direct_IO (iocb, from);@@ -2744,10 +2742,8 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)* so we don't support it 100%. If this invalidation * fails, tough, the write still worked... */ -if (mapping->nrpages) { -invalidate_inode_pages2_range (mapping, - pos >>PAGE_SHIFT , end); - } +invalidate_inode_pages2_range (mapping, + pos >>PAGE_SHIFT , end);if (written >0 ) { pos += written;Signature · 1 line
2.10.2
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 2/4 fs/block_dev: always invalidate cleancache in invalidate_bdev()
In reply to Andrey Ryabinininvalidate_bdev() calls cleancache_invalidate_inode() iff ->nrpages != 0 which doen't make any sense.
Make sure that invalidate_bdev() always calls cleancache_invalidate_inode() regardless of mapping->nrpages value.
--- fs/block_dev.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
fs/block_dev.c +5 -6
@@ -104,12 +104,11 @@ void invalidate_bdev(struct block_device *bdev) {struct address_space *mapping = bdev->bd_inode->i_mapping; -if (mapping->nrpages ==0 ) -return ; - -invalidate_bh_lrus (); -lru_add_drain_all ();/* make sure all lru add caches are flushed */ -invalidate_mapping_pages (mapping,0 ,-1 ); +if (mapping->nrpages) { +invalidate_bh_lrus (); +lru_add_drain_all ();/* make sure all lru add caches are flushed */ +invalidate_mapping_pages (mapping,0 ,-1 ); + }/* 99% of the time, we don't need to flush the cleancache on the bdev. * But, for the strange corners, lets be cautious */
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2 2/4] fs/block_dev: always invalidate cleancache in invalidate_bdev()
Quoted 9 lines · Andrey Ryabinin
invalidate_bdev()callscleancache_invalidate_inode()iff ->nrpages != 0 which doen't make any sense.
Make sure thatinvalidate_bdev()always callscleancache_invalidate_inode()regardless ofmapping->nrpagesvalue.Fixes c515e1fd361c ("mm/fs: add hooks to support cleancache")Signed-off-by Andrey Ryabinin <[email protected]>Cc <[email protected]>
Looks good. You can add:
Honza
Quoted 29 lines
--- fs/block_dev.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)fs/block_dev.c +5
-6@@ -104,12 +104,11 @@ void invalidate_bdev(struct block_device *bdev) {struct address_space *mapping = bdev->bd_inode->i_mapping; -if (mapping->nrpages ==0 ) -return ; - -invalidate_bh_lrus (); -lru_add_drain_all ();/* make sure all lru add caches are flushed */ -invalidate_mapping_pages (mapping,0 ,-1 ); +if (mapping->nrpages) { +invalidate_bh_lrus (); +lru_add_drain_all ();/* make sure all lru add caches are flushed */ +invalidate_mapping_pages (mapping,0 ,-1 ); + }/* 99% of the time, we don't need to flush the cleancache on the bdev. * But, for the strange corners, lets be cautious */ Signature · 1 line
2.10.2
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 3/4 mm/truncate: bail out early from invalidate_inode_pages2_range() if mapping is empty
In reply to Andrey RyabininIf mapping is empty (both ->nrpages and ->nrexceptional is zero) we can avoid pointless lookups in empty radix tree and bail out immediately after cleancache invalidation.
--- mm/truncate.c | 3 +++ 1 file changed, 3 insertions(+)
mm/truncate.c +3 -0
@@ -624,6 +624,9 @@ int invalidate_inode_pages2_range(struct address_space *mapping,int did_range_unmap =0 ;cleancache_invalidate_inode (mapping); +if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) +return 0 ; +pagevec_init (&pvec,0 ); index = start; while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2 3/4] mm/truncate: bail out early from invalidate_inode_pages2_range() if mapping is empty
Quoted 6 lines · Andrey Ryabinin
If mapping is empty (both ->nrpages and ->nrexceptional is zero) we can avoid pointless lookups in empty radix tree and bail out immediately after cleancache invalidation.
Signed-off-by Andrey Ryabinin <[email protected]>
Looks good. You can add:
Honza
Quoted 21 lines
--- mm/truncate.c | 3 +++ 1 file changed, 3 insertions(+)mm/truncate.c +3
-0@@ -624,6 +624,9 @@ int invalidate_inode_pages2_range(struct address_space *mapping,int did_range_unmap =0 ;cleancache_invalidate_inode (mapping); +if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) +return 0 ; +pagevec_init (&pvec,0 ); index = start; while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,Signature · 1 line
2.10.2
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v2 4/4 mm/truncate: avoid pointless cleancache_invalidate_inode() calls.
In reply to Andrey Ryabinincleancache_invalidate_inode() called truncate_inode_pages_range() and invalidate_inode_pages2_range() twice - on entry and on exit. It's stupid and waste of time. It's enough to call it once at exit.
--- mm/truncate.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
mm/truncate.c +7 -5
@@ -266,9 +266,8 @@ void truncate_inode_pages_range(struct address_space *mapping,pgoff_t index;int i; -cleancache_invalidate_inode (mapping);if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) -return ; + goto out;/* Offsets within partial pages */ partial_start = lstart & (PAGE_SIZE -1 );
@@ -363,7 +362,7 @@ void truncate_inode_pages_range(struct address_space *mapping,* will be released, just zeroed, so we can bail out now. */ if (start >= end) -return ; + goto out; index = start;for ( ; ; ) {
@@ -410,6 +409,8 @@ void truncate_inode_pages_range(struct address_space *mapping,pagevec_release (&pvec); index++; } + +out:cleancache_invalidate_inode (mapping); }EXPORT_SYMBOL (truncate_inode_pages_range);
@@ -623,9 +624,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping,int ret2 =0 ;int did_range_unmap =0 ; -cleancache_invalidate_inode (mapping);if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) -return 0 ; + goto out;pagevec_init (&pvec,0 ); index = start;
@@ -689,6 +689,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping,cond_resched (); index++; } + +out:cleancache_invalidate_inode (mapping);return ret; }
Signature · 7 lines
2.10.2 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2 4/4] mm/truncate: avoid pointless cleancache_invalidate_inode() calls.
Quoted 7 lines · Andrey Ryabinin
cleancache_invalidate_inode()calledtruncate_inode_pages_range()andinvalidate_inode_pages2_range()twice - on entry and on exit. It's stupid and waste of time. It's enough to call it once at exit.Signed-off-by Andrey Ryabinin <[email protected]>
Looks sensible to me but I don't really know cleancache :). Anyway feel free to add:
Honza
Quoted 60 lines
--- mm/truncate.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)mm/truncate.c +7
-5@@ -266,9 +266,8 @@ void truncate_inode_pages_range(struct address_space *mapping,pgoff_t index;int i; -cleancache_invalidate_inode (mapping);if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) -return ; + goto out;/* Offsets within partial pages */ partial_start = lstart & (PAGE_SIZE -1 );@@ -363,7 +362,7 @@ void truncate_inode_pages_range(struct address_space *mapping,* will be released, just zeroed, so we can bail out now. */ if (start >= end) -return ; + goto out; index = start;for ( ; ; ) {@@ -410,6 +409,8 @@ void truncate_inode_pages_range(struct address_space *mapping,pagevec_release (&pvec); index++; } + +out:cleancache_invalidate_inode (mapping); }EXPORT_SYMBOL (truncate_inode_pages_range);@@ -623,9 +624,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping,int ret2 =0 ;int did_range_unmap =0 ; -cleancache_invalidate_inode (mapping);if (mapping->nrpages ==0 && mapping->nrexceptional ==0 ) -return 0 ; + goto out;pagevec_init (&pvec,0 ); index = start;@@ -689,6 +689,8 @@ int invalidate_inode_pages2_range(struct address_space *mapping,cond_resched (); index++; } + +out:cleancache_invalidate_inode (mapping);return ret; }Signature · 1 line
2.10.2
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>