All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] block: fix BLKSECDISCARD zero-length range causing page cache invalidation
@ 2026-07-04  7:39 Zizhi Wo
  2026-07-05 15:56 ` yu kuai
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Zizhi Wo @ 2026-07-04  7:39 UTC (permalink / raw)
  To: axboe, linux-block; +Cc: linux-kernel, yangerkun, chengzhihao1, wozizhi

From: Zizhi Wo <wozizhi@huawei.com>

Commit 697ba0b6ec4a ("block: fix integer overflow in BLKSECDISCARD") fixed
the start+len overflow via check_add_overflow() but did not handle the
start=0, len=0 case. There, start + len = 0, so end = 0 passes all checks,
and truncate_bdev_range()->truncate_inode_pages_range() is then called with
lend=UINT64_MAX, whitch is the "truncate to the end of file" sentinel, so
the entire page cache is invalidated.

Fix this by replacing the validation with blk_validate_byte_range(), which
already rejects a zero-length range and is what BLKDISCARD uses. This also
switches the alignment check from a hardcoded 512 to
bdev_logical_block_size().

Signed-off-by: Zizhi Wo <wozizhi@huawei.com>
---
 block/ioctl.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/block/ioctl.c b/block/ioctl.c
index 3d4ea1537457..3b7d33a737e8 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -176,8 +176,7 @@ static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode,
 static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
 		void __user *argp)
 {
-	uint64_t start, len, end;
-	uint64_t range[2];
+	uint64_t range[2], start, len;
 	int err;
 
 	if (!(mode & BLK_OPEN_WRITE))
@@ -189,15 +188,13 @@ static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
 
 	start = range[0];
 	len = range[1];
-	if ((start & 511) || (len & 511))
-		return -EINVAL;
-	if (check_add_overflow(start, len, &end) ||
-	    end > bdev_nr_bytes(bdev))
-		return -EINVAL;
+	err = blk_validate_byte_range(bdev, start, len);
+	if (err)
+		return err;
 
 	inode_lock(bdev->bd_mapping->host);
 	filemap_invalidate_lock(bdev->bd_mapping);
-	err = truncate_bdev_range(bdev, mode, start, end - 1);
+	err = truncate_bdev_range(bdev, mode, start, start + len - 1);
 	if (!err)
 		err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9,
 						GFP_KERNEL);
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] block: fix BLKSECDISCARD zero-length range causing page cache invalidation
  2026-07-04  7:39 [PATCH] block: fix BLKSECDISCARD zero-length range causing page cache invalidation Zizhi Wo
@ 2026-07-05 15:56 ` yu kuai
  2026-07-24  1:17 ` Zizhi Wo
  2026-07-24  4:52 ` Christoph Hellwig
  2 siblings, 0 replies; 4+ messages in thread
From: yu kuai @ 2026-07-05 15:56 UTC (permalink / raw)
  To: Zizhi Wo, axboe, linux-block
  Cc: linux-kernel, yangerkun, chengzhihao1, wozizhi, yukuai

在 2026/7/4 15:39, Zizhi Wo 写道:

> From: Zizhi Wo<wozizhi@huawei.com>
>
> Commit 697ba0b6ec4a ("block: fix integer overflow in BLKSECDISCARD") fixed
> the start+len overflow via check_add_overflow() but did not handle the
> start=0, len=0 case. There, start + len = 0, so end = 0 passes all checks,
> and truncate_bdev_range()->truncate_inode_pages_range() is then called with
> lend=UINT64_MAX, whitch is the "truncate to the end of file" sentinel, so
> the entire page cache is invalidated.
>
> Fix this by replacing the validation with blk_validate_byte_range(), which
> already rejects a zero-length range and is what BLKDISCARD uses. This also
> switches the alignment check from a hardcoded 512 to
> bdev_logical_block_size().
>
> Signed-off-by: Zizhi Wo<wozizhi@huawei.com>
> ---
>   block/ioctl.c | 13 +++++--------
>   1 file changed, 5 insertions(+), 8 deletions(-)

Reviewed-by: Yu Kuai <yukuai@fygo.io>

-- 
Thanks,
Kuai

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] block: fix BLKSECDISCARD zero-length range causing page cache invalidation
  2026-07-04  7:39 [PATCH] block: fix BLKSECDISCARD zero-length range causing page cache invalidation Zizhi Wo
  2026-07-05 15:56 ` yu kuai
@ 2026-07-24  1:17 ` Zizhi Wo
  2026-07-24  4:52 ` Christoph Hellwig
  2 siblings, 0 replies; 4+ messages in thread
From: Zizhi Wo @ 2026-07-24  1:17 UTC (permalink / raw)
  To: Zizhi Wo, axboe, linux-block
  Cc: linux-kernel, yangerkun, chengzhihao1, yukuai

friendly ping...

在 2026/7/4 15:39, Zizhi Wo 写道:
> From: Zizhi Wo <wozizhi@huawei.com>
> 
> Commit 697ba0b6ec4a ("block: fix integer overflow in BLKSECDISCARD") fixed
> the start+len overflow via check_add_overflow() but did not handle the
> start=0, len=0 case. There, start + len = 0, so end = 0 passes all checks,
> and truncate_bdev_range()->truncate_inode_pages_range() is then called with
> lend=UINT64_MAX, whitch is the "truncate to the end of file" sentinel, so
> the entire page cache is invalidated.
> 
> Fix this by replacing the validation with blk_validate_byte_range(), which
> already rejects a zero-length range and is what BLKDISCARD uses. This also
> switches the alignment check from a hardcoded 512 to
> bdev_logical_block_size().
> 
> Signed-off-by: Zizhi Wo <wozizhi@huawei.com>
> ---
>   block/ioctl.c | 13 +++++--------
>   1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/block/ioctl.c b/block/ioctl.c
> index 3d4ea1537457..3b7d33a737e8 100644
> --- a/block/ioctl.c
> +++ b/block/ioctl.c
> @@ -176,8 +176,7 @@ static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode,
>   static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
>   		void __user *argp)
>   {
> -	uint64_t start, len, end;
> -	uint64_t range[2];
> +	uint64_t range[2], start, len;
>   	int err;
>   
>   	if (!(mode & BLK_OPEN_WRITE))
> @@ -189,15 +188,13 @@ static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
>   
>   	start = range[0];
>   	len = range[1];
> -	if ((start & 511) || (len & 511))
> -		return -EINVAL;
> -	if (check_add_overflow(start, len, &end) ||
> -	    end > bdev_nr_bytes(bdev))
> -		return -EINVAL;
> +	err = blk_validate_byte_range(bdev, start, len);
> +	if (err)
> +		return err;
>   
>   	inode_lock(bdev->bd_mapping->host);
>   	filemap_invalidate_lock(bdev->bd_mapping);
> -	err = truncate_bdev_range(bdev, mode, start, end - 1);
> +	err = truncate_bdev_range(bdev, mode, start, start + len - 1);
>   	if (!err)
>   		err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9,
>   						GFP_KERNEL);


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] block: fix BLKSECDISCARD zero-length range causing page cache invalidation
  2026-07-04  7:39 [PATCH] block: fix BLKSECDISCARD zero-length range causing page cache invalidation Zizhi Wo
  2026-07-05 15:56 ` yu kuai
  2026-07-24  1:17 ` Zizhi Wo
@ 2026-07-24  4:52 ` Christoph Hellwig
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2026-07-24  4:52 UTC (permalink / raw)
  To: Zizhi Wo; +Cc: axboe, linux-block, linux-kernel, yangerkun, chengzhihao1,
	wozizhi

On Sat, Jul 04, 2026 at 03:39:42PM +0800, Zizhi Wo wrote:
> From: Zizhi Wo <wozizhi@huawei.com>
> 
> Commit 697ba0b6ec4a ("block: fix integer overflow in BLKSECDISCARD") fixed
> the start+len overflow via check_add_overflow() but did not handle the
> start=0, len=0 case. There, start + len = 0, so end = 0 passes all checks,
> and truncate_bdev_range()->truncate_inode_pages_range() is then called with
> lend=UINT64_MAX, whitch is the "truncate to the end of file" sentinel, so
> the entire page cache is invalidated.
> 
> Fix this by replacing the validation with blk_validate_byte_range(), which
> already rejects a zero-length range and is what BLKDISCARD uses. This also
> switches the alignment check from a hardcoded 512 to
> bdev_logical_block_size().

I though I had reviewed this before?  But either way it looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-24  4:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-04  7:39 [PATCH] block: fix BLKSECDISCARD zero-length range causing page cache invalidation Zizhi Wo
2026-07-05 15:56 ` yu kuai
2026-07-24  1:17 ` Zizhi Wo
2026-07-24  4:52 ` Christoph Hellwig

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.