mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Maxim Levitsky <mlevitsk@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: "Darrick J . Wong" <darrick.wong@oracle.com>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	linux-fsdevel@vger.kernel.org (open list:FILESYSTEMS (VFS and
	infrastructure)), Jan Kara <jack@suse.cz>,
	Maxim Levitsky <mlevitsk@redhat.com>
Subject: [PATCH] block: fallocate: avoid false positive on collision detection
Date: Thu,  7 Jan 2021 14:40:22 +0200	[thread overview]
Message-ID: <20210107124022.900172-1-mlevitsk@redhat.com> (raw)
In-Reply-To: <45420b24124b5b91bc0a80a4abad2e06acb8c2b3.camel@redhat.com>

Align start and end on page boundaries before calling
invalidate_inode_pages2_range.

This might allow us to miss a collision if the write and the discard were done
to the same page and do overlap but it is still better than returning -EBUSY
if those writes didn't overlap.

Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
 fs/block_dev.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/fs/block_dev.c b/fs/block_dev.c
index 9e84b1928b94..97f0d16661b5 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -1970,6 +1970,7 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
 	loff_t end = start + len - 1;
 	loff_t isize;
 	int error;
+	pgoff_t invalidate_first_page, invalidate_last_page;
 
 	/* Fail if we don't recognize the flags. */
 	if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
@@ -2020,12 +2021,23 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
 
 	/*
 	 * Invalidate again; if someone wandered in and dirtied a page,
-	 * the caller will be given -EBUSY.  The third argument is
-	 * inclusive, so the rounding here is safe.
+	 * the caller will be given -EBUSY.
+	 *
+	 * If the start/end of the range is not page aligned, exclude the
+	 * non aligned regions to avoid false positives.
 	 */
+	invalidate_first_page = DIV_ROUND_UP(start, PAGE_SIZE);
+	invalidate_last_page = end >> PAGE_SHIFT;
+
+	if ((end + 1) & PAGE_MASK)
+		invalidate_last_page--;
+
+	if (invalidate_last_page < invalidate_first_page)
+		return 0;
+
 	return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
-					     start >> PAGE_SHIFT,
-					     end >> PAGE_SHIFT);
+					     invalidate_first_page,
+					     invalidate_last_page);
 }
 
 const struct file_operations def_blk_fops = {
-- 
2.26.2


       reply	other threads:[~2021-01-07 12:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <45420b24124b5b91bc0a80a4abad2e06acb8c2b3.camel@redhat.com>
2021-01-07 12:40 ` Maxim Levitsky [this message]
2021-01-07 12:42   ` Maxim Levitsky
2021-01-07 15:37   ` 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=20210107124022.900172-1-mlevitsk@redhat.com \
    --to=mlevitsk@redhat.com \
    --cc=darrick.wong@oracle.com \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®