Re: [PATCH] block: fallocate: avoid false positive on collision detection

2021-01-07 Thread Jan Kara
On Thu 07-01-21 14:40:22, Maxim Levitsky wrote:
> 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 

Thanks for getting back to this and I'm sorry I didn't get to this earlier
myself! I actually think the fix should be different as we discussed with
Darrick. Attached patch should fix the issue for you (I'll also post it
formally for inclusion).

Honza

> ---
>  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
> 
-- 
Jan Kara 
SUSE Labs, CR
>From 36f751ac88420a6bda8a3c161986455629dc80d4 Mon Sep 17 00:00:00 2001
From: Jan Kara 
Date: Thu, 7 Jan 2021 16:26:52 +0100
Subject: [PATCH] bdev: Do not return EBUSY if bdev discard races with write

blkdev_fallocate() tries to detect whether a discard raced with an
overlapping write by calling invalidate_inode_pages2_range(). However
this check can give both false negatives (when writing using direct IO
or when writeback already writes out the written pagecache range) and
false positives (when write is not actually overlapping but ends in the
same page when blocksize < pagesize). This actually causes issues for
qemu which is getting confused by EBUSY errors.

Fix the problem by removing this conflicting write detection since it is
inherently racy and thus of little use anyway.

Reported-by: Maxim Levitsky 
CC: "Darrick J. Wong" 
Signed-off-by: Jan Kara 
---
 fs/block_dev.c | 10 --
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/fs/block_dev.c b/fs/block_dev.c
index 3e5b02f6606c..a97f43b49839 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -1797,13 +1797,11 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
 		return error;
 
 	/*
-	 * 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.
+	 * Invalidate the page cache again; if someone wandered in and dirtied
+	 * a page, we just discard it - userspace has no way of knowing whether
+	 * the write happened before or after discard completing...
 	 */
-	return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
-	 start >> PAGE_SHIFT,
-	 end >> PAGE_SHIFT);
+	return truncate_bdev_range(bdev, file->f_mode, start, end);
 }
 
 const struct file_operations def_blk_fops = {
-- 
2.26.2



Re: [PATCH] block: fallocate: avoid false positive on collision detection

2021-01-07 Thread Maxim Levitsky
On Thu, 2021-01-07 at 14:40 +0200, Maxim Levitsky wrote:
> 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 

This is my attempt to fix this issue. I am not 100% sure
that this is the right solution though.

Any feedback is welcome!

Best regards,
Maxim Levitsky



[PATCH] block: fallocate: avoid false positive on collision detection

2021-01-07 Thread Maxim Levitsky
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 
---
 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