* [PATCH] block: use blk_validate_byte_range() for BLKZEROOUT and BLKSECDISCARD
@ 2026-05-29 6:56 dayou5941
2026-06-01 7:42 ` Christoph Hellwig
0 siblings, 1 reply; 2+ messages in thread
From: dayou5941 @ 2026-05-29 6:56 UTC (permalink / raw)
To: axboe; +Cc: linux-block, liyouhong
From: liyouhong <liyouhong@kylinos.cn>
blk_validate_byte_range() was extracted from BLKDISCARD in 2024 but
BLKZEROOUT and BLKSECDISCARD still used legacy 512-byte alignment
checks. On 4K logical block devices this allows misaligned requests
to pass ioctl validation, invalidate the page cache, and then fail in
blkdev_issue_zeroout() or blkdev_issue_secure_erase().
Use blk_validate_byte_range() for both ioctls so range validation
matches BLKDISCARD, fallocate, and the blk-lib helpers.
Signed-off-by: liyouhong <liyouhong@kylinos.cn>
---
block/ioctl.c | 29 ++++++++++-------------------
1 file changed, 10 insertions(+), 19 deletions(-)
diff --git a/block/ioctl.c b/block/ioctl.c
index ab2c9ed79946..e2bec9acc3b7 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -176,7 +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 start, len;
uint64_t range[2];
int err;
@@ -189,15 +189,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);
@@ -211,7 +209,7 @@ static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode,
unsigned long arg)
{
uint64_t range[2];
- uint64_t start, end, len;
+ uint64_t start, len;
int err;
if (!(mode & BLK_OPEN_WRITE))
@@ -222,21 +220,14 @@ static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode,
start = range[0];
len = range[1];
- end = start + len - 1;
-
- if (start & 511)
- return -EINVAL;
- if (len & 511)
- return -EINVAL;
- if (end >= (uint64_t)bdev_nr_bytes(bdev))
- return -EINVAL;
- if (end < start)
- return -EINVAL;
+ err = blk_validate_byte_range(bdev, start, len);
+ if (err)
+ return err;
/* Invalidate the page cache, including dirty pages */
inode_lock(bdev->bd_mapping->host);
filemap_invalidate_lock(bdev->bd_mapping);
- err = truncate_bdev_range(bdev, mode, start, end);
+ err = truncate_bdev_range(bdev, mode, start, start + len - 1);
if (err)
goto fail;
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] block: use blk_validate_byte_range() for BLKZEROOUT and BLKSECDISCARD
2026-05-29 6:56 [PATCH] block: use blk_validate_byte_range() for BLKZEROOUT and BLKSECDISCARD dayou5941
@ 2026-06-01 7:42 ` Christoph Hellwig
0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2026-06-01 7:42 UTC (permalink / raw)
To: dayou5941; +Cc: axboe, linux-block, liyouhong
Looks good:
Reviewed-by: Christoph Hellwig <hch@lst.de>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-06-01 7:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-29 6:56 [PATCH] block: use blk_validate_byte_range() for BLKZEROOUT and BLKSECDISCARD dayou5941
2026-06-01 7:42 ` Christoph Hellwig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox