bdev: Do not return EBUSY if bdev discard races with write
Jan Kara
reviewerPATCH 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
@@ -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
Quoted 29 lines · Jan Kara
blkdev_fallocate()tries to detect whether a discard raced with an overlapping write by callinginvalidate_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 <[email protected]>CC "Darrick J. Wong" <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/block_dev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)fs/block_dev.c +0
-2@@ -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
-0const struct file_operations def_blk_fops = {
Maxim Levitsky
Reviewed-byQuoted 27 lines · Maxim Levitsky
Quoted 26 lines · Jan Kara
blkdev_fallocate()tries to detect whether a discard raced with an overlapping write by callinginvalidate_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 <[email protected]>CC "Darrick J. Wong" <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/block_dev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)fs/block_dev.c +0
-0@@ -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 LevitskyQuoted 2 lines
fs/block_dev.c +0
-0const struct file_operations def_blk_fops = {
Quoted 8 lines · Jan Kara
blkdev_fallocate()tries to detect whether a discard raced with an overlapping write by callinginvalidate_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.
Reported-by Maxim Levitsky <[email protected]>CC "Darrick J. Wong" <[email protected]>Signed-off-by Jan Kara <[email protected]>
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@@ -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
Looks good,
Jan Kara
reviewerQuoted 8 lines · Jan Kara
blkdev_fallocate()tries to detect whether a discard raced with an overlapping write by callinginvalidate_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.
Reported-by Maxim Levitsky <[email protected]>CC "Darrick J. Wong" <[email protected]>Signed-off-by Jan Kara <[email protected]>
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@@ -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
Quoted 12 lines · Jan Kara
Quoted 11 lines · Jan Kara
blkdev_fallocate()tries to detect whether a discard raced with an overlapping write by callinginvalidate_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
Reported-by Maxim Levitsky <[email protected]>CC "Darrick J. Wong" <[email protected]>Signed-off-by Jan Kara <[email protected]>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
Jan Kara
reviewerQuoted 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 callinginvalidate_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 <[email protected]>CC "Darrick J. Wong" <[email protected]>Signed-off-by Jan Kara <[email protected]>
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