Skip to contentSkip to conversation

bdev: Do not return EBUSY if bdev discard races with write

PATCH In review Jan Kara 5 participants 8 messages Jan 7, 2021 – Jan 27, 2021 VFS

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

PATCH 1/1 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.

---
 fs/block_dev.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/block_dev.c +4 -6
index 3e5b02f6606c..a97f43b49839 100644
@@ -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 = {
Signature · 1 line
2.26.2
#1 lore raw
Quoted 29 lines · Jan Kara

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.

---
 fs/block_dev.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/block_dev.c +0 -2
index 3e5b02f6606c..a97f43b49839 100644
@@ -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

fs/block_dev.c static long blkdev_fallocate(struct file *file, int mode, loff_t start, · line 1805

Quoted 10 lines
fs/block_dev.c +4 -4
-	 * 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);
 }

But what happens if write and discard don't overlap? Won't we discard the written data in this case?

Best regards,
Maxim Levitsky

Quoted 2 lines
fs/block_dev.c +0 -0
 const struct file_operations def_blk_fops = {
Reviewed-by
#2 lore raw
Quoted 27 lines · Maxim Levitsky
Quoted 26 lines · Jan Kara

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.

---
 fs/block_dev.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/block_dev.c +0 -0
index 3e5b02f6606c..a97f43b49839 100644
@@ -1797,13 +1797,11 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
 		return error;
 
Quoted 16 lines

fs/block_dev.c static long blkdev_fallocate(struct file *file, int mode, loff_t start, · line 1805

Quoted 13 lines
fs/block_dev.c +4 -6
 	/*
-	 * 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);
 }

But what happens if write and discard don't overlap? Won't we discard the written data in this case?

Ah, I see, the truncate_bdev_range preserves the partial areas that are not included in the range.

In this case this indeed looks right.

Best regards,
Maxim Levitsky

Quoted 8 lines

Best regards,
Maxim Levitsky

Quoted 2 lines
fs/block_dev.c +0 -0
 const struct file_operations def_blk_fops = {
Reviewed-by on v1 1/1
#3 lore raw
In reply to Jan Kara
Quoted 8 lines · Jan Kara

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.

Quoted 7 lines

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

Looks good to me,

--D

Quoted 29 lines
---
 fs/block_dev.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/block_dev.c +4 -6
index 3e5b02f6606c..a97f43b49839 100644
@@ -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 = {
Signature · 1 line
2.26.2
author reviewer
#5 lore raw
In reply to Jan Kara
Quoted 8 lines · Jan Kara

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.

Quoted 7 lines

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

Jens, can you please pick up this patch? Thanks!

Honza

Quoted 29 lines
---
 fs/block_dev.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/block_dev.c +4 -6
index 3e5b02f6606c..a97f43b49839 100644
@@ -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 = {
Signature · 1 line
2.26.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw
Quoted 12 lines · Jan Kara
Quoted 11 lines · Jan Kara

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.

Quoted 6 lines
Quoted 4 lines

Jens, can you please pick up this patch? Thanks!

Picked it up for 5.12, hope that works. It looks simple enough but not really meeting criteria for 5.11 at this point.

Signature · 1 line
Jens Axboe
author reviewer
#7 lore raw
Quoted 18 lines · Jens Axboe
Quoted 17 lines · Jan Kara
Quoted 15 lines · Jan Kara

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.

Quoted 4 lines
Quoted 1 line

Jens, can you please pick up this patch? Thanks!

Picked it up for 5.12, hope that works. It looks simple enough but not really meeting criteria for 5.11 at this point.

Sure, 5.12 is fine. We've been living with the current behavior for quite some time and not many people complained...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR