block: fail unaligned bio from submit_bio_noacct()
PATCH v2 1/1 block: fail unaligned bio from submit_bio_noacct()
For any FS bio, its start sector and size have to be aligned with the queue's logical block size from beginning, because bio split code can't make one aligned bio.
This rule is obvious, but there is still user which may send unaligned bio to block layer, and it is observed that dm-integrity can do that, and cause double free of driver's dma meta buffer.
So failfast unaligned bio from submit_bio_noacct() for avoiding more troubles.
Meantime remove this kind of check in dio and discard code path.
---
V2:
- remove the check in dio and discard code path
- check .bi_sector with (logical_block_size >> 9) - 1
block/blk-core.c | 16 ++++++++++++++++ block/blk-lib.c | 17 ----------------- block/fops.c | 3 +-- 3 files changed, 17 insertions(+), 19 deletions(-)
block/blk-core.c +16 -0
@@ -729,6 +729,19 @@ void submit_bio_noacct_nocheck(struct bio *bio)__submit_bio_noacct (bio); } +static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size; + +if (bio->bi_iter.bi_size & (bs -1 )) +return false; + +if (bio->bi_iter.bi_sector & ((bs >>SECTOR_SHIFT ) -1 )) +return false; + +return true; +} +/** * submit_bio_noacct - re-submit a bio to the block device layer for I/O * @bio: The bio describing the location in memory and on the device.
@@ -780,6 +793,9 @@ void submit_bio_noacct(struct bio *bio) } } +if (WARN_ON_ONCE (!bio_check_alignment (bio, q))) + goto end_io; +if (!test_bit (QUEUE_FLAG_POLL , &q->queue_flags))bio_clear_polled (bio);
block/blk-lib.c +0 -17
@@ -39,7 +39,6 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop) { struct bio *bio = *biop; -sector_t bs_mask;if (bdev_read_only (bdev))return -EPERM ;
@@ -53,10 +52,6 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,return -EOPNOTSUPP ; } - bs_mask = (bdev_logical_block_size (bdev) >>9 ) -1 ; -if ((sector | nr_sects) & bs_mask) -return -EINVAL ; -if (!nr_sects)return -EINVAL ;
@@ -217,11 +212,6 @@ int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, unsigned flags) {int ret; -sector_t bs_mask; - - bs_mask = (bdev_logical_block_size (bdev) >>9 ) -1 ; -if ((sector | nr_sects) & bs_mask) -return -EINVAL ; ret =__blkdev_issue_write_zeroes (bdev, sector, nr_sects, gfp_mask, biop, flags);
@@ -250,15 +240,10 @@ int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, unsigned flags) { int ret = 0; -sector_t bs_mask;struct bio *bio;struct blk_plug plug;bool try_write_zeroes = !!bdev_write_zeroes_sectors (bdev); - bs_mask = (bdev_logical_block_size (bdev) >>9 ) -1 ; -if ((sector | nr_sects) & bs_mask) -return -EINVAL ; - retry: bio =NULL ;blk_start_plug (&plug);
@@ -313,8 +298,6 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,if (max_sectors ==0 )return -EOPNOTSUPP ; -if ((sector | nr_sects) & bs_mask) -return -EINVAL ;if (bdev_read_only (bdev))return -EPERM ;
block/fops.c +1 -2
@@ -37,8 +37,7 @@ static blk_opf_t dio_bio_write_op(struct kiocb *iocb)static bool blkdev_dio_unaligned (struct block_device *bdev,loff_t pos,struct iov_iter *iter) { -return pos & (bdev_logical_block_size (bdev) -1 ) || - !bdev_iter_is_aligned (bdev, iter); +return !bdev_iter_is_aligned (bdev, iter); }#define DIO_INLINE_BIO_VECS 4
Signature · 1 line
2.41.0
Quoted 40 lines · Ming Lei
For any FS bio, its start sector and size have to be aligned with the queue's logical block size from beginning, because bio split code can't make one aligned bio.
This rule is obvious, but there is still user which may send unaligned bio to block layer, and it is observed that dm-integrity can do that, and cause double free of driver's dma meta buffer.
So failfast unaligned bio from
submit_bio_noacct()for avoiding more troubles.Meantime remove this kind of check in dio and discard code path.
Cc Keith Busch <[email protected]>Cc Bart Van Assche <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Mikulas Patocka <[email protected]>Cc Mike Snitzer <[email protected]>Signed-off-by Ming Lei <[email protected]>---
V2:
- remove the check in dio and discard code path
- check .bi_sector with (logical_block_size>> 9) - 1block/blk-core.c | 16 ++++++++++++++++ block/blk-lib.c | 17 ----------------- block/fops.c | 3 +-- 3 files changed, 17 insertions(+), 19 deletions(-)block/blk-core.c +3
-0@@ -729,6 +729,19 @@ void submit_bio_noacct_nocheck(struct bio *bio)__submit_bio_noacct (bio); } +static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size;
block/blk-core.c void submit_bio_noacct_nocheck(struct bio *bio) · line 744
Quoted 10 lines
block/blk-core.c +10
-0+ +if (bio->bi_iter.bi_size & (bs -1 )) +return false; + +if (bio->bi_iter.bi_sector & ((bs >>SECTOR_SHIFT ) -1 )) +return false; + +return true; +} +
You missed Christoph's reply to v1 where he offered:
"This should just use bdev_logical_block_size() on bio->bi_bdev."
Otherwise, looks good.
Mike
block/blk-core.c void submit_bio_noacct_nocheck(struct bio *bio) · line 743
Quoted 12 lines · Ming Lei
block/blk-core.c +12
-0+static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size; + +if (bio->bi_iter.bi_size & (bs -1 )) +return false; + +if (bio->bi_iter.bi_sector & ((bs >>SECTOR_SHIFT ) -1 )) +return false; + +return true; +}
This should still use bdev_logic_block_size. And maybe it's just me, but I think dropping thelines after the false returns would actually make it more readle.
block/fops.c static blk_opf_t dio_bio_write_op(struct kiocb *iocb) · line 40
Quoted 11 lines
block/fops.c +1
-2@@ -37,8 +37,7 @@ static blk_opf_t dio_bio_write_op(struct kiocb *iocb)static bool blkdev_dio_unaligned (struct block_device *bdev,loff_t pos,struct iov_iter *iter) { -return pos & (bdev_logical_block_size (bdev) -1 ) || - !bdev_iter_is_aligned (bdev, iter); +return !bdev_iter_is_aligned (bdev, iter);
If you drop this:
- we now actually go all the way down to building and submiting a bio for a trivial bounds check.
- your get a trivial to trigger WARN_ON.
I'd strongly advise against dropping this check.
Quoted 10 lines · Christoph Hellwig
Quoted 9 lines · Ming Lei
block/blk-core.c +9
-0+static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size; + +if (bio->bi_iter.bi_size & (bs -1 )) +return false; + +if (bio->bi_iter.bi_sector & ((bs >>SECTOR_SHIFT ) -1 )) +return false;
Quoted 8 lines
block/blk-core.c void submit_bio_noacct_nocheck(struct bio *bio) · line 743
Quoted 3 lines
block/blk-core.c +3
-0+ +return true; +}This should still use
bdev_logic_block_size. And maybe it's just me, but I think dropping thelines after the false returns would actually make it more readle.
OK, will remove the blank line.
Quoted 15 lines
block/fops.c static blk_opf_t dio_bio_write_op(struct kiocb *iocb) · line 40
Quoted 12 lines
block/fops.c +1
-2@@ -37,8 +37,7 @@ static blk_opf_t dio_bio_write_op(struct kiocb *iocb)static bool blkdev_dio_unaligned (struct block_device *bdev,loff_t pos,struct iov_iter *iter) { -return pos & (bdev_logical_block_size (bdev) -1 ) || - !bdev_iter_is_aligned (bdev, iter); +return !bdev_iter_is_aligned (bdev, iter);If you drop this:
Quoted 5 lines
- we now actually go all the way down to building and submiting a bio for a trivial bounds check.
- your get a trivial to triggerWARN_ON.I'd strongly advise against dropping this check.
OK.
Also only q->limits.logical_block_size is fetched for small BS IO fast path, I think log(lbs) can be cached in request_queue for avoiding the extra fetch of q.limits. Especially, it could be easier to do so with your recent queue limit atomic update changes.
Thanks,
Ming
Quoted 4 lines · Ming Lei
Also only
q->limits.logical_block_sizeis fetched for small BS IO fast path, I think log(lbs) can be cached inrequest_queuefor avoiding the extra fetch of q.limits. Especially, it could be easier to do so with your recent queue limit atomic update changes.
So. One thing I've been thinking of for a while (and which Bart also mentioned) is tht queue_limits currently is a bit of a mess between
the actual queue limits, and the gneidks configuration. The logical
block size is firmly in the latter, and we should probably move it to the gendisk eventually. Depending on how converting the SCSI ULDs to the atomic queue limits API goes that imght happen rather sooner than later.
Quoted 10 lines · Christoph Hellwig
Quoted 4 lines · Ming Lei
Also only
q->limits.logical_block_sizeis fetched for small BS IO fast path, I think log(lbs) can be cached inrequest_queuefor avoiding the extra fetch of q.limits. Especially, it could be easier to do so with your recent queue limit atomic update changes.So. One thing I've been thinking of for a while (and which Bart also mentioned) is tht
queue_limitscurrently is a bit of a mess between
the actual queue limits, and the gneidks configuration. The logical
block size is firmly in the latter, and we should probably move it
lbs and pbs belong to disk, but some others may not be very obvious.
Strictly speaking elevator/blkcg belong to disk too, but still stay in request_queue, :-)
Thanks,
Ming
block/blk-core.c void submit_bio_noacct(struct bio *bio) · line 798
Quoted 7 lines · Ming Lei
block/blk-core.c +3
-0@@ -780,6 +793,9 @@ void submit_bio_noacct(struct bio *bio) } } +if (WARN_ON_ONCE (!bio_check_alignment (bio, q))) + goto end_io; +
The "status" at this point is "BLK_STS_IOERR", so user space would see EIO, but the existing checks return EINVAL. I'm not sure if that's "ok", but assuming it is, I think the user visible different behavior should be mentioned in the changelog.
Alternatively, maybe we want an asynchronous way to return EINVAL for these conditions. It's more informative to a user where the problem is than a generic EIO. There is no BLK_STS_ value that translates to EINVAL, though, so maybe we need a new block status code like BLK_STS_INVALID_REQUEST.
Quoted 10 lines
block/blk-lib.c +0
-4@@ -53,10 +52,6 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,return -EOPNOTSUPP ; } - bs_mask = (bdev_logical_block_size (bdev) >>9 ) -1 ; -if ((sector | nr_sects) & bs_mask) -return -EINVAL ; -if (!nr_sects)return -EINVAL ;
Quoted 14 lines · Keith Busch
block/blk-core.c void submit_bio_noacct(struct bio *bio) · line 798
Quoted 7 lines · Ming Lei
block/blk-core.c +3
-0@@ -780,6 +793,9 @@ void submit_bio_noacct(struct bio *bio) } } +if (WARN_ON_ONCE (!bio_check_alignment (bio, q))) + goto end_io; +The "status" at this point is "
BLK_STS_IOERR", so user space would see EIO, but the existing checks return EINVAL. I'm not sure if that's "ok", but assuming it is, I think the user visible different behavior should be mentioned in the changelog.
Quoted 1 line
Alternatively, maybe we want an asynchronous way to return EINVAL for
It has to be async way to return it because submit_bio*() returns void.
Quoted 4 lines
these conditions. It's more informative to a user where the problem is than a generic EIO. There is no
BLK_STS_value that translates to EINVAL, though, so maybe we need a new block status code likeBLK_STS_INVALID_REQUEST.
Yeah, I agree, but that is one existed issue. The 'status' should have been initialized as 'BLK_STS_INVALID_REQUEST' or 'BLK_STS_INVALID' in submit_bio_noacct(), and all check failure can be thought as -EINVAL.
Thanks,
Ming