diff mbox series

[07/12] f2fs: Convert to using invalidate_lock

Message ID 20210423173018.23133-7-jack@suse.cz (mailing list archive)
State New
Headers show
Series fs: Hole punch vs page cache filling races | expand

Commit Message

Jan Kara April 23, 2021, 5:29 p.m. UTC
Use invalidate_lock instead of f2fs' private i_mmap_sem. The intended
purpose is exactly the same. By this conversion we fix a long standing
race between hole punching and read(2) / readahead(2) paths that can
lead to stale page cache contents.

CC: Jaegeuk Kim <jaegeuk@kernel.org>
CC: Chao Yu <yuchao0@huawei.com>
CC: linux-f2fs-devel@lists.sourceforge.net
Signed-off-by: Jan Kara <jack@suse.cz>
---
 fs/f2fs/data.c  |  4 ++--
 fs/f2fs/f2fs.h  |  1 -
 fs/f2fs/file.c  | 58 ++++++++++++++++++++++++-------------------------
 fs/f2fs/super.c |  1 -
 4 files changed, 30 insertions(+), 34 deletions(-)

Comments

kernel test robot April 23, 2021, 7:15 p.m. UTC | #1
Hi Jan,

I love your patch! Yet something to improve:

[auto build test ERROR on ext4/dev]
[also build test ERROR on fuse/for-next linus/master v5.12-rc8]
[cannot apply to hnaz-linux-mm/master next-20210423]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jan-Kara/fs-Hole-punch-vs-page-cache-filling-races/20210424-013114
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4.git dev
config: m68k-randconfig-m031-20210423 (attached as .config)
compiler: m68k-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/7a9e8e67e7f7d0070294e9f0a3567a3f28985383
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jan-Kara/fs-Hole-punch-vs-page-cache-filling-races/20210424-013114
        git checkout 7a9e8e67e7f7d0070294e9f0a3567a3f28985383
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross W=1 ARCH=m68k 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   fs/f2fs/file.c: In function 'f2fs_release_compress_blocks':
>> fs/f2fs/file.c:3567:14: error: 'mapping' undeclared (first use in this function)
    3567 |  down_write(&mapping->invalidate_lock);
         |              ^~~~~~~
   fs/f2fs/file.c:3567:14: note: each undeclared identifier is reported only once for each function it appears in


vim +/mapping +3567 fs/f2fs/file.c

  3515	
  3516	static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
  3517	{
  3518		struct inode *inode = file_inode(filp);
  3519		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3520		pgoff_t page_idx = 0, last_idx;
  3521		unsigned int released_blocks = 0;
  3522		int ret;
  3523		int writecount;
  3524	
  3525		if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
  3526			return -EOPNOTSUPP;
  3527	
  3528		if (!f2fs_compressed_file(inode))
  3529			return -EINVAL;
  3530	
  3531		if (f2fs_readonly(sbi->sb))
  3532			return -EROFS;
  3533	
  3534		ret = mnt_want_write_file(filp);
  3535		if (ret)
  3536			return ret;
  3537	
  3538		f2fs_balance_fs(F2FS_I_SB(inode), true);
  3539	
  3540		inode_lock(inode);
  3541	
  3542		writecount = atomic_read(&inode->i_writecount);
  3543		if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
  3544				(!(filp->f_mode & FMODE_WRITE) && writecount)) {
  3545			ret = -EBUSY;
  3546			goto out;
  3547		}
  3548	
  3549		if (IS_IMMUTABLE(inode)) {
  3550			ret = -EINVAL;
  3551			goto out;
  3552		}
  3553	
  3554		ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
  3555		if (ret)
  3556			goto out;
  3557	
  3558		F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
  3559		f2fs_set_inode_flags(inode);
  3560		inode->i_ctime = current_time(inode);
  3561		f2fs_mark_inode_dirty_sync(inode, true);
  3562	
  3563		if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
  3564			goto out;
  3565	
  3566		down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
> 3567		down_write(&mapping->invalidate_lock);
  3568	
  3569		last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  3570	
  3571		while (page_idx < last_idx) {
  3572			struct dnode_of_data dn;
  3573			pgoff_t end_offset, count;
  3574	
  3575			set_new_dnode(&dn, inode, NULL, NULL, 0);
  3576			ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
  3577			if (ret) {
  3578				if (ret == -ENOENT) {
  3579					page_idx = f2fs_get_next_page_offset(&dn,
  3580									page_idx);
  3581					ret = 0;
  3582					continue;
  3583				}
  3584				break;
  3585			}
  3586	
  3587			end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  3588			count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
  3589			count = round_up(count, F2FS_I(inode)->i_cluster_size);
  3590	
  3591			ret = release_compress_blocks(&dn, count);
  3592	
  3593			f2fs_put_dnode(&dn);
  3594	
  3595			if (ret < 0)
  3596				break;
  3597	
  3598			page_idx += count;
  3599			released_blocks += ret;
  3600		}
  3601	
  3602		up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  3603		up_write(&mapping->invalidate_lock);
  3604	out:
  3605		inode_unlock(inode);
  3606	
  3607		mnt_drop_write_file(filp);
  3608	
  3609		if (ret >= 0) {
  3610			ret = put_user(released_blocks, (u64 __user *)arg);
  3611		} else if (released_blocks &&
  3612				atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
  3613			set_sbi_flag(sbi, SBI_NEED_FSCK);
  3614			f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
  3615				"iblocks=%llu, released=%u, compr_blocks=%u, "
  3616				"run fsck to fix.",
  3617				__func__, inode->i_ino, inode->i_blocks,
  3618				released_blocks,
  3619				atomic_read(&F2FS_I(inode)->i_compr_blocks));
  3620		}
  3621	
  3622		return ret;
  3623	}
  3624	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot April 23, 2021, 8:05 p.m. UTC | #2
Hi Jan,

I love your patch! Yet something to improve:

[auto build test ERROR on ext4/dev]
[also build test ERROR on fuse/for-next linus/master v5.12-rc8]
[cannot apply to hnaz-linux-mm/master next-20210423]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jan-Kara/fs-Hole-punch-vs-page-cache-filling-races/20210424-013114
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4.git dev
config: arm-randconfig-r031-20210423 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 06234f758e1945084582cf80450b396f75a9c06e)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm cross compiling tool for clang build
        # apt-get install binutils-arm-linux-gnueabi
        # https://github.com/0day-ci/linux/commit/7a9e8e67e7f7d0070294e9f0a3567a3f28985383
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jan-Kara/fs-Hole-punch-vs-page-cache-filling-races/20210424-013114
        git checkout 7a9e8e67e7f7d0070294e9f0a3567a3f28985383
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=arm 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> fs/f2fs/file.c:3567:14: error: use of undeclared identifier 'mapping'
           down_write(&mapping->invalidate_lock);
                       ^
   fs/f2fs/file.c:3603:12: error: use of undeclared identifier 'mapping'
           up_write(&mapping->invalidate_lock);
                     ^
   2 errors generated.


vim +/mapping +3567 fs/f2fs/file.c

  3515	
  3516	static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
  3517	{
  3518		struct inode *inode = file_inode(filp);
  3519		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3520		pgoff_t page_idx = 0, last_idx;
  3521		unsigned int released_blocks = 0;
  3522		int ret;
  3523		int writecount;
  3524	
  3525		if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
  3526			return -EOPNOTSUPP;
  3527	
  3528		if (!f2fs_compressed_file(inode))
  3529			return -EINVAL;
  3530	
  3531		if (f2fs_readonly(sbi->sb))
  3532			return -EROFS;
  3533	
  3534		ret = mnt_want_write_file(filp);
  3535		if (ret)
  3536			return ret;
  3537	
  3538		f2fs_balance_fs(F2FS_I_SB(inode), true);
  3539	
  3540		inode_lock(inode);
  3541	
  3542		writecount = atomic_read(&inode->i_writecount);
  3543		if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
  3544				(!(filp->f_mode & FMODE_WRITE) && writecount)) {
  3545			ret = -EBUSY;
  3546			goto out;
  3547		}
  3548	
  3549		if (IS_IMMUTABLE(inode)) {
  3550			ret = -EINVAL;
  3551			goto out;
  3552		}
  3553	
  3554		ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
  3555		if (ret)
  3556			goto out;
  3557	
  3558		F2FS_I(inode)->i_flags |= F2FS_IMMUTABLE_FL;
  3559		f2fs_set_inode_flags(inode);
  3560		inode->i_ctime = current_time(inode);
  3561		f2fs_mark_inode_dirty_sync(inode, true);
  3562	
  3563		if (!atomic_read(&F2FS_I(inode)->i_compr_blocks))
  3564			goto out;
  3565	
  3566		down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
> 3567		down_write(&mapping->invalidate_lock);
  3568	
  3569		last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
  3570	
  3571		while (page_idx < last_idx) {
  3572			struct dnode_of_data dn;
  3573			pgoff_t end_offset, count;
  3574	
  3575			set_new_dnode(&dn, inode, NULL, NULL, 0);
  3576			ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
  3577			if (ret) {
  3578				if (ret == -ENOENT) {
  3579					page_idx = f2fs_get_next_page_offset(&dn,
  3580									page_idx);
  3581					ret = 0;
  3582					continue;
  3583				}
  3584				break;
  3585			}
  3586	
  3587			end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  3588			count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
  3589			count = round_up(count, F2FS_I(inode)->i_cluster_size);
  3590	
  3591			ret = release_compress_blocks(&dn, count);
  3592	
  3593			f2fs_put_dnode(&dn);
  3594	
  3595			if (ret < 0)
  3596				break;
  3597	
  3598			page_idx += count;
  3599			released_blocks += ret;
  3600		}
  3601	
  3602		up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  3603		up_write(&mapping->invalidate_lock);
  3604	out:
  3605		inode_unlock(inode);
  3606	
  3607		mnt_drop_write_file(filp);
  3608	
  3609		if (ret >= 0) {
  3610			ret = put_user(released_blocks, (u64 __user *)arg);
  3611		} else if (released_blocks &&
  3612				atomic_read(&F2FS_I(inode)->i_compr_blocks)) {
  3613			set_sbi_flag(sbi, SBI_NEED_FSCK);
  3614			f2fs_warn(sbi, "%s: partial blocks were released i_ino=%lx "
  3615				"iblocks=%llu, released=%u, compr_blocks=%u, "
  3616				"run fsck to fix.",
  3617				__func__, inode->i_ino, inode->i_blocks,
  3618				released_blocks,
  3619				atomic_read(&F2FS_I(inode)->i_compr_blocks));
  3620		}
  3621	
  3622		return ret;
  3623	}
  3624	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 4e5257c763d0..932c773b7b97 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -3154,12 +3154,12 @@  static void f2fs_write_failed(struct address_space *mapping, loff_t to)
 	/* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
 	if (to > i_size && !f2fs_verity_in_progress(inode)) {
 		down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-		down_write(&F2FS_I(inode)->i_mmap_sem);
+		down_write(&mapping->invalidate_lock);
 
 		truncate_pagecache(inode, i_size);
 		f2fs_truncate_blocks(inode, i_size, true);
 
-		up_write(&F2FS_I(inode)->i_mmap_sem);
+		up_write(&mapping->invalidate_lock);
 		up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 	}
 }
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index e2d302ae3a46..f9010c69a57e 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -742,7 +742,6 @@  struct f2fs_inode_info {
 
 	/* avoid racing between foreground op and gc */
 	struct rw_semaphore i_gc_rwsem[2];
-	struct rw_semaphore i_mmap_sem;
 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
 
 	int i_extra_isize;		/* size of extra space located in i_addr */
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index d26ff2ae3f5e..e58d67a264ac 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -37,10 +37,7 @@  static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
 	struct inode *inode = file_inode(vmf->vma->vm_file);
 	vm_fault_t ret;
 
-	down_read(&F2FS_I(inode)->i_mmap_sem);
 	ret = filemap_fault(vmf);
-	up_read(&F2FS_I(inode)->i_mmap_sem);
-
 	if (!ret)
 		f2fs_update_iostat(F2FS_I_SB(inode), APP_MAPPED_READ_IO,
 							F2FS_BLKSIZE);
@@ -101,7 +98,7 @@  static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
 	f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
 
 	file_update_time(vmf->vma->vm_file);
-	down_read(&F2FS_I(inode)->i_mmap_sem);
+	down_read(&inode->i_mapping->invalidate_lock);
 	lock_page(page);
 	if (unlikely(page->mapping != inode->i_mapping ||
 			page_offset(page) > i_size_read(inode) ||
@@ -160,7 +157,7 @@  static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
 
 	trace_f2fs_vm_page_mkwrite(page, DATA);
 out_sem:
-	up_read(&F2FS_I(inode)->i_mmap_sem);
+	up_read(&inode->i_mapping->invalidate_lock);
 
 	sb_end_pagefault(inode->i_sb);
 err:
@@ -941,7 +938,7 @@  int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
 		}
 
 		down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-		down_write(&F2FS_I(inode)->i_mmap_sem);
+		down_write(&inode->i_mapping->invalidate_lock);
 
 		truncate_setsize(inode, attr->ia_size);
 
@@ -951,7 +948,7 @@  int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
 		 * do not trim all blocks after i_size if target size is
 		 * larger than i_size.
 		 */
-		up_write(&F2FS_I(inode)->i_mmap_sem);
+		up_write(&inode->i_mapping->invalidate_lock);
 		up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 		if (err)
 			return err;
@@ -1094,7 +1091,7 @@  static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
 			blk_end = (loff_t)pg_end << PAGE_SHIFT;
 
 			down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-			down_write(&F2FS_I(inode)->i_mmap_sem);
+			down_write(&mapping->invalidate_lock);
 
 			truncate_inode_pages_range(mapping, blk_start,
 					blk_end - 1);
@@ -1103,7 +1100,7 @@  static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
 			ret = f2fs_truncate_hole(inode, pg_start, pg_end);
 			f2fs_unlock_op(sbi);
 
-			up_write(&F2FS_I(inode)->i_mmap_sem);
+			up_write(&mapping->invalidate_lock);
 			up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 		}
 	}
@@ -1338,7 +1335,7 @@  static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
 
 	/* avoid gc operation during block exchange */
 	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-	down_write(&F2FS_I(inode)->i_mmap_sem);
+	down_write(&inode->i_mapping->invalidate_lock);
 
 	f2fs_lock_op(sbi);
 	f2fs_drop_extent_tree(inode);
@@ -1346,7 +1343,7 @@  static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
 	ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
 	f2fs_unlock_op(sbi);
 
-	up_write(&F2FS_I(inode)->i_mmap_sem);
+	up_write(&inode->i_mapping->invalidate_lock);
 	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 	return ret;
 }
@@ -1377,13 +1374,13 @@  static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
 		return ret;
 
 	/* write out all moved pages, if possible */
-	down_write(&F2FS_I(inode)->i_mmap_sem);
+	down_write(&inode->i_mapping->invalidate_lock);
 	filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
 	truncate_pagecache(inode, offset);
 
 	new_size = i_size_read(inode) - len;
 	ret = f2fs_truncate_blocks(inode, new_size, true);
-	up_write(&F2FS_I(inode)->i_mmap_sem);
+	up_write(&inode->i_mapping->invalidate_lock);
 	if (!ret)
 		f2fs_i_size_write(inode, new_size);
 	return ret;
@@ -1483,7 +1480,7 @@  static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
 			pgoff_t end;
 
 			down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-			down_write(&F2FS_I(inode)->i_mmap_sem);
+			down_write(&mapping->invalidate_lock);
 
 			truncate_pagecache_range(inode,
 				(loff_t)index << PAGE_SHIFT,
@@ -1495,7 +1492,7 @@  static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
 			ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
 			if (ret) {
 				f2fs_unlock_op(sbi);
-				up_write(&F2FS_I(inode)->i_mmap_sem);
+				up_write(&mapping->invalidate_lock);
 				up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 				goto out;
 			}
@@ -1507,7 +1504,7 @@  static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
 			f2fs_put_dnode(&dn);
 
 			f2fs_unlock_op(sbi);
-			up_write(&F2FS_I(inode)->i_mmap_sem);
+			up_write(&mapping->invalidate_lock);
 			up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 
 			f2fs_balance_fs(sbi, dn.node_changed);
@@ -1542,6 +1539,7 @@  static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+	struct address_space *mapping = inode->i_mapping;
 	pgoff_t nr, pg_start, pg_end, delta, idx;
 	loff_t new_size;
 	int ret = 0;
@@ -1564,14 +1562,14 @@  static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
 
 	f2fs_balance_fs(sbi, true);
 
-	down_write(&F2FS_I(inode)->i_mmap_sem);
+	down_write(&mapping->invalidate_lock);
 	ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
-	up_write(&F2FS_I(inode)->i_mmap_sem);
+	up_write(&mapping->invalidate_lock);
 	if (ret)
 		return ret;
 
 	/* write out all dirty pages from offset */
-	ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
+	ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
 	if (ret)
 		return ret;
 
@@ -1582,7 +1580,7 @@  static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
 
 	/* avoid gc operation during block exchange */
 	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-	down_write(&F2FS_I(inode)->i_mmap_sem);
+	down_write(&mapping->invalidate_lock);
 	truncate_pagecache(inode, offset);
 
 	while (!ret && idx > pg_start) {
@@ -1598,14 +1596,14 @@  static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
 					idx + delta, nr, false);
 		f2fs_unlock_op(sbi);
 	}
-	up_write(&F2FS_I(inode)->i_mmap_sem);
+	up_write(&mapping->invalidate_lock);
 	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 
 	/* write out all moved pages, if possible */
-	down_write(&F2FS_I(inode)->i_mmap_sem);
-	filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
+	down_write(&mapping->invalidate_lock);
+	filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
 	truncate_pagecache(inode, offset);
-	up_write(&F2FS_I(inode)->i_mmap_sem);
+	up_write(&mapping->invalidate_lock);
 
 	if (!ret)
 		f2fs_i_size_write(inode, new_size);
@@ -3566,7 +3564,7 @@  static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 		goto out;
 
 	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-	down_write(&F2FS_I(inode)->i_mmap_sem);
+	down_write(&mapping->invalidate_lock);
 
 	last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
 
@@ -3602,7 +3600,7 @@  static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
 	}
 
 	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-	up_write(&F2FS_I(inode)->i_mmap_sem);
+	up_write(&mapping->invalidate_lock);
 out:
 	inode_unlock(inode);
 
@@ -3719,7 +3717,7 @@  static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
 	}
 
 	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-	down_write(&F2FS_I(inode)->i_mmap_sem);
+	down_write(&inode->i_mapping->invalidate_lock);
 
 	last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
 
@@ -3755,7 +3753,7 @@  static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
 	}
 
 	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-	up_write(&F2FS_I(inode)->i_mmap_sem);
+	up_write(&inode->i_mapping->invalidate_lock);
 
 	if (ret >= 0) {
 		F2FS_I(inode)->i_flags &= ~F2FS_IMMUTABLE_FL;
@@ -3875,7 +3873,7 @@  static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
 		goto err;
 
 	down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
-	down_write(&F2FS_I(inode)->i_mmap_sem);
+	down_write(&mapping->invalidate_lock);
 
 	ret = filemap_write_and_wait_range(mapping, range.start,
 			to_end ? LLONG_MAX : end_addr - 1);
@@ -3962,7 +3960,7 @@  static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
 		ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
 				prev_block, len, range.flags);
 out:
-	up_write(&F2FS_I(inode)->i_mmap_sem);
+	up_write(&mapping->invalidate_lock);
 	up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
 err:
 	inode_unlock(inode);
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 82592b19b4e0..c6ade37338ed 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -1176,7 +1176,6 @@  static struct inode *f2fs_alloc_inode(struct super_block *sb)
 	mutex_init(&fi->inmem_lock);
 	init_rwsem(&fi->i_gc_rwsem[READ]);
 	init_rwsem(&fi->i_gc_rwsem[WRITE]);
-	init_rwsem(&fi->i_mmap_sem);
 	init_rwsem(&fi->i_xattr_sem);
 
 	/* Will be used by directory only */