f2fs: dax: fix races between page faults and truncating pages
Subsystems (2)
PATCH v2 1/2 f2fs: dax: fix races between page faults and truncating pages
From: Qiuyang Sun <[email protected]>
Currently in F2FS, page faults and operations that truncate the pagecahe or data blocks, are completely unsynchronized. This can result in page fault faulting in a page into a range that we are changing after truncating, and thus we can end up with a page mapped to disk blocks that will be shortly freed. Filesystem corruption will shortly follow.
This patch fixes the problem by creating new rw semaphore i_mmap_sem in f2fs_inode_info and grab it for functions removing blocks from extent tree and for read over page faults. The mechanism is similar to that in ext4.
---
Changelog v1 -> v2:
- Apply the new rw semaphore in some other necessary scenarios:
f2fs_write_failed
f2fs_filemap_fault (new function) f2fs_vm_page_mkwrite
f2fs_setattr
(f2fs_add_inline_entries() does not need this rw semaphore, as dir is a directory file and its pages would not be mmap'ed.)
- Lock coverage in the scenarios below are reconsidered:
punch_hole
f2fs_collapse_range
f2fs_zero_range
f2fs_insert_range
The v2 patches are at 4.12-rc1.
--- fs/f2fs/data.c | 2 ++ fs/f2fs/f2fs.h | 1 + fs/f2fs/file.c | 48 +++++++++++++++++++++++++++++++++++++++--------- fs/f2fs/super.c | 1 + 4 files changed, 43 insertions(+), 9 deletions(-)
fs/f2fs/data.c +2 -0
@@ -1753,8 +1753,10 @@ static void f2fs_write_failed(struct address_space *mapping, loff_t to)loff_t i_size =i_size_read (inode);if (to > i_size) { +down_write (&F2FS_I (inode)->i_mmap_sem);truncate_pagecache (inode, i_size);truncate_blocks (inode, i_size, true); +up_write (&F2FS_I (inode)->i_mmap_sem); } }
fs/f2fs/f2fs.h +1 -0
@@ -519,6 +519,7 @@ struct f2fs_inode_info {struct mutex inmem_lock;/* lock for inmemory pages */ struct extent_tree *extent_tree;/* cached extent_tree entry */ struct rw_semaphore dio_rwsem[2 ];/* avoid racing between dio and gc */ +struct rw_semaphore i_mmap_sem; }; static inline void get_extent_info(struct extent_info *ext,
fs/f2fs/file.c +39 -9
169 lines not on this page · raw message
fs/f2fs/super.c +1 -0
@@ -624,6 +624,7 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb)mutex_init (&fi->inmem_lock);init_rwsem (&fi->dio_rwsem[READ ]);init_rwsem (&fi->dio_rwsem[WRITE ]); +init_rwsem (&fi->i_mmap_sem);/* Will be used by directory only */ fi->i_dir_level =F2FS_SB (sb)->dir_level;
Signature · 6 lines
1.8.3.1 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Quoted 8 lines · sunqiuyang
From: Qiuyang Sun <[email protected]>
Currently in F2FS, page faults and operations that truncate the pagecahe or data blocks, are completely unsynchronized. This can result in page fault faulting in a page into a range that we are changing after truncating, and thus we can end up with a page mapped to disk blocks that will be shortly freed. Filesystem corruption will shortly follow.
Quoted 5 lines
This patch fixes the problem by creating new rw semaphore
i_mmap_seminf2fs_inode_infoand grab it for functions removing blocks from extent tree and for read over page faults. The mechanism is similar to that in ext4.Signed-off-by Qiuyang Sun <[email protected]>