mm,dax: Fix data corruption due to mmap inconsistency
Subsystems (7)
Jan Kara
maintainerv3 cover mm,dax: Fix data corruption due to mmap inconsistency
Hello,
this series fixes data corruption that can happen for DAX mounts when page faults race with write(2) and as a result page tables get out of sync with block mappings in the filesystem and thus data seen through mmap is different from data seen through read(2).
The series passes testing with t_mmap_stale test program from Ross and also other mmap related tests on DAX filesystem.
Andrew, can you please merge these patches? Thanks!
Changes since v2:
* Added reviewed-by tag from Ross
Changes since v1:
* Improved performance of unmapping pages
* Changed fault locking to fix another write vs fault race
Honza
Jan Kara
maintainerPATCH v3 1/4 dax: prevent invalidation of mapped DAX entries
From: Ross Zwisler <ross.zwisler-VuQAYsv1563Yd54FQh9/[email protected]>
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().
--- 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.12.0
Jan Kara
maintainerPATCH v3 2/4 mm: Fix data corruption due to stale mmap reads
In reply to Jan KaraCurrently, we didn't invalidate page tables during invalidate_inode_pages2() for DAX. That could result in e.g. 2MiB zero page being mapped into page tables while there were already underlying blocks allocated and thus data seen through mmap were different from data seen by read(2). The following sequence reproduces the problem:
- 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.
Fix the problem by unconditionally calling invalidate_inode_pages2_range() in dax_iomap_actor() for new block allocations and by properly invalidating page tables in invalidate_inode_pages2_range() for DAX mappings.
--- fs/dax.c | 2 +- mm/truncate.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-)
fs/dax.c +1 -1
@@ -1003,7 +1003,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 );
mm/truncate.c +11 -1
@@ -686,7 +686,17 @@ int invalidate_inode_pages2_range(struct address_space *mapping,cond_resched (); index++; } - +/* +* For DAX we invalidate page tables after invalidating radix tree. We +* could invalidate page tables while invalidating each entry however +* that would be expensive. And doing range unmapping before doesn't +* work as we have no cheap way to find whether radix tree entry didn't +* get remapped later. +*/ +if (dax_mapping (mapping)) { +unmap_mapping_range (mapping, (loff_t )start <<PAGE_SHIFT , + (loff_t)(end - start +1 ) <<PAGE_SHIFT ,0 ); + } out:cleancache_invalidate_inode (mapping);return ret;
Signature · 1 line
2.12.0
Jan Kara
maintainerPATCH v3 3/4 ext4: Return back to starting transaction in ext4_dax_huge_fault()
In reply to Jan KaraDAX will return to locking exceptional entry before mapping blocks for a page fault to fix possible races with concurrent writes. To avoid lock inversion between exceptional entry lock and transaction start, start the transaction already in ext4_dax_huge_fault().
--- fs/ext4/file.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
fs/ext4/file.c +17 -4
@@ -257,6 +257,7 @@ static int ext4_dax_huge_fault(struct vm_fault *vmf, enum page_entry_size pe_size) {int result; +handle_t *handle =NULL ;struct inode *inode =file_inode (vmf->vma->vm_file);struct super_block *sb = inode->i_sb;bool write = vmf->flags &FAULT_FLAG_WRITE ;
@@ -264,12 +265,24 @@ static int ext4_dax_huge_fault(struct vm_fault *vmf,if (write) {sb_start_pagefault (sb);file_update_time (vmf->vma->vm_file); +down_read (&EXT4_I (inode)->i_mmap_sem); + handle =ext4_journal_start_sb (sb,EXT4_HT_WRITE_PAGE , +EXT4_DATA_TRANS_BLOCKS (sb)); + }else { +down_read (&EXT4_I (inode)->i_mmap_sem); } -down_read (&EXT4_I (inode)->i_mmap_sem); - result =dax_iomap_fault (vmf, pe_size, &ext4_iomap_ops); -up_read (&EXT4_I (inode)->i_mmap_sem); -if (write) +if (!IS_ERR (handle)) + result =dax_iomap_fault (vmf, pe_size, &ext4_iomap_ops); +else + result =VM_FAULT_SIGBUS ; +if (write) { +if (!IS_ERR (handle)) +ext4_journal_stop (handle); +up_read (&EXT4_I (inode)->i_mmap_sem);sb_end_pagefault (sb); + }else { +up_read (&EXT4_I (inode)->i_mmap_sem); + }return result; }
Signature · 1 line
2.12.0
Jan Kara
maintainerPATCH v3 4/4 dax: Fix data corruption when fault races with write
In reply to Jan KaraCurrently DAX read fault can race with write(2) in the following way:
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 and map it to page tables
The result is that hole page is mapped into page tables (and thus zeros are seen in mmap) while file has data written in that place.
Fix the problem by locking exception entry before mapping blocks for the fault. That way we are sure invalidate_inode_pages2_range() call for racing write will either block on entry lock waiting for the fault to finish (and unmap stale page tables after that) or read fault will see already allocated blocks by write(2).
--- fs/dax.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
fs/dax.c +16 -16
@@ -1124,23 +1124,23 @@ static int dax_iomap_pte_fault(struct vm_fault *vmf,if ((vmf->flags &FAULT_FLAG_WRITE ) && !vmf->cow_page) flags |=IOMAP_WRITE ; + entry =grab_mapping_entry (mapping, vmf->pgoff,0 ); +if (IS_ERR (entry)) +return dax_fault_return (PTR_ERR (entry)); +/* * Note that we don't bother to use iomap_apply here: DAX required * the file system block size to be equal the page size, which means * that we never have to deal with more than a single extent here. */ error = ops->iomap_begin (inode, pos,PAGE_SIZE , flags, &iomap); -if (error) -return dax_fault_return (error); -if (WARN_ON_ONCE (iomap.offset + iomap.length < pos +PAGE_SIZE )) { - vmf_ret =dax_fault_return (-EIO );/* fs corruption? */ - goto finish_iomap; +if (error) { + vmf_ret =dax_fault_return (error); + goto unlock_entry; } - - entry =grab_mapping_entry (mapping, vmf->pgoff,0 ); -if (IS_ERR (entry)) { - vmf_ret =dax_fault_return (PTR_ERR (entry)); - goto finish_iomap; +if (WARN_ON_ONCE (iomap.offset + iomap.length < pos +PAGE_SIZE )) { + error = -EIO ;/* fs corruption? */ + goto error_finish_iomap; } sector =dax_iomap_sector (&iomap, pos);
@@ -1162,13 +1162,13 @@ static int dax_iomap_pte_fault(struct vm_fault *vmf, }if (error) - goto error_unlock_entry; + goto error_finish_iomap;__SetPageUptodate (vmf->cow_page); vmf_ret =finish_fault (vmf);if (!vmf_ret) vmf_ret =VM_FAULT_DONE_COW ; - goto unlock_entry; + goto finish_iomap; }switch (iomap.type) {
@@ -1188,7 +1188,7 @@ static int dax_iomap_pte_fault(struct vm_fault *vmf,case IOMAP_HOLE :if (!(vmf->flags &FAULT_FLAG_WRITE )) { vmf_ret =dax_load_hole (mapping, &entry, vmf); - goto unlock_entry; + goto finish_iomap; }/*FALLTHRU*/ default :
@@ -1197,10 +1197,8 @@ static int dax_iomap_pte_fault(struct vm_fault *vmf,break ; } - error_unlock_entry: + error_finish_iomap: vmf_ret =dax_fault_return (error) | major; - unlock_entry: -put_locked_mapping_entry (mapping, vmf->pgoff, entry); finish_iomap:if (ops->iomap_end) {int copied =PAGE_SIZE ;
@@ -1215,6 +1213,8 @@ static int dax_iomap_pte_fault(struct vm_fault *vmf,*/ ops->iomap_end (inode, pos,PAGE_SIZE , copied, flags, &iomap); } + unlock_entry: +put_locked_mapping_entry (mapping, vmf->pgoff, entry);return vmf_ret; }
Signature · 1 line
2.12.0