All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.com>
To: linux-ext4@vger.kernel.org
Cc: Ted Tso <tytso@mit.edu>, Dan Williams <dan.j.williams@intel.com>,
	ross.zwisler@linux.intel.com, willy@linux.intel.com,
	Jan Kara <jack@suse.com>
Subject: [PATCH 3/9] ext4: Fix races between buffered IO and collapse / insert range
Date: Thu, 22 Oct 2015 10:15:55 +0200	[thread overview]
Message-ID: <1445501761-14528-4-git-send-email-jack@suse.com> (raw)
In-Reply-To: <1445501761-14528-1-git-send-email-jack@suse.com>

Current code implementing FALLOC_FL_COLLAPSE_RANGE and
FALLOC_FL_INSERT_RANGE is prove to races with buffered writes and page
faults. If buffered write or write via mmap manages to squeeze between
filemap_write_and_wait_range() and truncate_pagecache() in the fallocate
implementations, the written data is simply discarded by
truncate_pagecache() although it should have been shifted.

Fix the problem by moving filemap_write_and_wait_range() call inside
i_mutex and i_mmap_sem. That way we are protected against races with
both buffered writes and page faults.

Signed-off-by: Jan Kara <jack@suse.com>
---
 fs/ext4/extents.c | 62 +++++++++++++++++++++++++++++--------------------------
 1 file changed, 33 insertions(+), 29 deletions(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 66ab89b58c1f..892245a55c53 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -5483,21 +5483,7 @@ int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
 			return ret;
 	}
 
-	/*
-	 * Need to round down offset to be aligned with page size boundary
-	 * for page size > block size.
-	 */
-	ioffset = round_down(offset, PAGE_SIZE);
-
-	/* Write out all dirty pages */
-	ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
-					   LLONG_MAX);
-	if (ret)
-		return ret;
-
-	/* Take mutex lock */
 	mutex_lock(&inode->i_mutex);
-
 	/*
 	 * There is no need to overlap collapse range with EOF, in which case
 	 * it is effectively a truncate operation
@@ -5518,10 +5504,32 @@ int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
 	inode_dio_wait(inode);
 
 	/*
-	 * Prevent page faults from reinstantiating pages we have released from
+	 * Prevent page faults from reinstantiating we have released from
 	 * page cache.
 	 */
 	down_write(&EXT4_I(inode)->i_mmap_sem);
+	/*
+	 * Need to round down offset to be aligned with page size boundary
+	 * for page size > block size.
+	 */
+	ioffset = round_down(offset, PAGE_SIZE);
+	/*
+	 * Write tail of last page before removed range since it will get
+	 * removed from page cache below.
+	 */
+	ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
+					   offset - ioffset);
+	if (ret)
+		goto out_mmap;
+	/*
+	 * Write data that will be shifted to preserve them when discarding
+	 * page cache below. We are also protected from pages becoming dirty
+	 * by i_mmap_sem.
+	 */
+	ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
+					   LLONG_MAX);
+	if (ret)
+		goto out_mmap;
 	truncate_pagecache(inode, ioffset);
 
 	credits = ext4_writepage_trans_blocks(inode);
@@ -5622,21 +5630,7 @@ int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
 			return ret;
 	}
 
-	/*
-	 * Need to round down to align start offset to page size boundary
-	 * for page size > block size.
-	 */
-	ioffset = round_down(offset, PAGE_SIZE);
-
-	/* Write out all dirty pages */
-	ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
-			LLONG_MAX);
-	if (ret)
-		return ret;
-
-	/* Take mutex lock */
 	mutex_lock(&inode->i_mutex);

  parent reply	other threads:[~2015-10-22  8:16 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-22  8:15 [PATCH 0/9 v2] ext4: Punch hole and DAX fixes Jan Kara
2015-10-22  8:15 ` [PATCH 1/9] ext4: Fix races between page faults and hole punching Jan Kara
2015-10-24  1:21   ` Theodore Ts'o
2015-10-25  4:58     ` Jan Kara
2015-10-22  8:15 ` [PATCH 2/9] ext4: Move unlocked dio protection from ext4_alloc_file_blocks() Jan Kara
2015-10-22  8:15 ` Jan Kara [this message]
2015-10-24  1:22   ` [PATCH 3/9] ext4: Fix races between buffered IO and collapse / insert range Theodore Ts'o
2015-10-24  4:59   ` Theodore Ts'o
2015-10-22  8:15 ` [PATCH 4/9] ext4: Fix races of writeback with punch hole and zero range Jan Kara
2015-10-22  8:15 ` [PATCH 5/9] ext4: Document lock ordering Jan Kara
2015-10-22  8:15 ` [PATCH 6/9] ext4: Get rid of EXT4_GET_BLOCKS_NO_LOCK flag Jan Kara
2015-10-22  8:15 ` [PATCH 7/9] ext4: Provide ext4_issue_zeroout() Jan Kara
2015-10-22  8:16 ` [PATCH 8/9] ext4: Implement allocation of pre-zeroed blocks Jan Kara
2015-10-22  8:16 ` [PATCH 9/9] ext4: Use pre-zeroed blocks for DAX page faults Jan Kara
2015-10-25  9:23   ` Theodore Ts'o
2015-10-22 18:09 ` [PATCH 0/9 v2] ext4: Punch hole and DAX fixes Ross Zwisler
2015-10-22 21:14   ` Jan Kara
2015-10-23  3:35   ` Eryu Guan
  -- strict thread matches above, loose matches on Subject: below --
2015-11-04 16:18 [PATCH 0/9 v3] " Jan Kara
2015-11-04 16:18 ` [PATCH 3/9] ext4: Fix races between buffered IO and collapse / insert range Jan Kara
2015-11-10 19:50 [PATCH 0/9 v4] ext4: Punch hole and DAX fixes Jan Kara
2015-11-10 19:50 ` [PATCH 3/9] ext4: Fix races between buffered IO and collapse / insert range Jan Kara
2015-11-18  1:39   ` Elliott, Robert (Persistent Memory)
2015-11-18 15:16     ` Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1445501761-14528-4-git-send-email-jack@suse.com \
    --to=jack@suse.com \
    --cc=dan.j.williams@intel.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=ross.zwisler@linux.intel.com \
    --cc=tytso@mit.edu \
    --cc=willy@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.