block: fail unaligned bio from submit_bio_noacct()
Subsystems (2)
PATCH 1/1 block: fail unaligned bio from submit_bio_noacct()
For any bio with data, its start sector and size have to be aligned with the queue's logical block size.
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.
--- block/blk-core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
block/blk-core.c +17 -0
@@ -729,6 +729,20 @@ 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; +unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -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 +794,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);
Signature · 1 line
2.41.0
block/blk-core.c void submit_bio_noacct_nocheck(struct bio *bio) · line 741
Quoted 10 lines · Ming Lei
block/blk-core.c +10
-0+static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size; +unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 ))) +return false;
Why "size &&"? It doesn't harm to reject unaligned bios if size == 0 and it will reduce the number of if-tests in the hot path.
Why to shift bio->bi_iter.bi_sector left instead of shifting (bs - 1) right?
Thanks,
Bart.
Quoted 13 lines · Bart Van Assche
block/blk-core.c void submit_bio_noacct_nocheck(struct bio *bio) · line 741
Quoted 10 lines · Ming Lei
block/blk-core.c +10
-0+static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size; +unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 ))) +return false;Why "size &&"? It doesn't harm to reject unaligned bios if size == 0 and it will reduce the number of if-tests in the hot path.
It doesn't make sense to check the alignment for bio without data.
Quoted 3 lines
Why to shift
bio->bi_iter.bi_sectorleft instead of shifting (bs - 1) right?
unit of bs is bytes, so .bi_sector needs to be converted to byte first.
Thanks,
Ming
Mike Snitzer
maintainerQuoted 56 lines · Ming Lei
For any bio with data, its start sector and size have to be aligned with the queue's logical block size.
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.Cc Mikulas Patocka <[email protected]>Cc Mike Snitzer <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)block/blk-core.c +17
-0@@ -729,6 +729,20 @@ 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; +unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -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 +794,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);Signature · 1 line
2.41.0
This check would really help more quickly find buggy code, but it would be unfortunate for these extra checks to be required in production. It feels like this is the type of check that should be wrapped by a debug CONFIG option (so only debug kernels have it).
Do we already have an appropriate CONFIG option to use?
Mike
Mikulas Patocka
maintainerQuoted 28 lines · Mike Snitzer
Quoted 25 lines · Ming Lei
For any bio with data, its start sector and size have to be aligned with the queue's logical block size.
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.Cc Mikulas Patocka <[email protected]>Cc Mike Snitzer <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)block/blk-core.c +0
-0@@ -729,6 +729,20 @@ void submit_bio_noacct_nocheck(struct bio *bio)__submit_bio_noacct (bio); }
Quoted 13 lines
block/blk-core.c void submit_bio_noacct_nocheck(struct bio *bio) · line 744
Quoted 13 lines
block/blk-core.c +13
-0+static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size; +unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 ))) +return false; + +return true; +}
I would change it to
if (unlikely(((bi_iter.bi_sector | bio_sectors(bio)) & ((queue_logical_block_size(q) >> 9) - 1)) != 0))
return false;
Quoted 23 lines
Quoted 17 lines
block/blk-core.c +3
-0/** * 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 +794,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);Signature · 1 line
2.41.0This check would really help more quickly find buggy code, but it would be unfortunate for these extra checks to be required in production. It feels like this is the type of check that should be wrapped by a debug CONFIG option (so only debug kernels have it).
Quoted 3 lines
Do we already have an appropriate CONFIG option to use?
Mike
But then, the system would crash with the config option being 'n' and return an error with the config option being 'y' - which would be unfortunate.
We could remove the check from the drivers and add it to the generic I/O path - this wouldn't add extra overhead.
Mikulas
Jens Axboe
maintainerQuoted 43 lines · Ming Lei
For any bio with data, its start sector and size have to be aligned with the queue's logical block size.
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.Cc Mikulas Patocka <[email protected]>Cc Mike Snitzer <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)block/blk-core.c +14
-0@@ -729,6 +729,20 @@ 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; +unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -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.
block/blk-core.c void submit_bio_noacct(struct bio *bio) · line 801
Quoted 8 lines
block/blk-core.c +3
-0@@ -780,6 +794,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);
Where is this IO coming from? The normal block level dio has checks. And in fact they are expensive... If we add this one, then we should be able to kill the block/fops.c checks, no?
Signature · 1 line
Jens Axboe
Quoted 9 lines · Ming Lei
For any bio with data, its start sector and size have to be aligned with the queue's logical block size.
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.
I've been wanting to do that for the next merge window, as the lack of this check is kinda stunning. Note that we have open coded versions of it in __blkdev_issue_dicard and blkdev_issue_zeroout that can go away now.
block/blk-core.c void submit_bio_noacct_nocheck(struct bio *bio) · line 735
Quoted 4 lines
block/blk-core.c +4
-0+static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size; +unsigned int size = bio->bi_iter.bi_size;
This should just use bdev_logical_block_size() on bio->bi_bdev.
Quoted 6 lines · Mikulas Patocka
But then, the system would crash with the config option being 'n' and return an error with the config option being 'y' - which would be unfortunate.
We could remove the check from the drivers and add it to the generic I/O path - this wouldn't add extra overhead.
Agreed. This is a pretty cheap check, and I'd rather have it all the time instead of being duplicated in various drivers, just like we do a lot of other sanity checking in submit_bio.
Quoted 2 lines · Jens Axboe
Where is this IO coming from? The normal block level dio has checks. And in fact they are expensive...
How is this expensive when we need the bio cache lines all the time during I/O submission which follows instantly?
Quoted 2 lines
If we add this one, then we should be able
to kill theblock/fops.cchecks, no?
That would mean we'd only get an async error back, and it would be past say page cache invalidation. Not sure that's a good way to handle failed alignment from userspace.
Jens Axboe
maintainerQuoted 6 lines · Christoph Hellwig
Quoted 2 lines · Jens Axboe
Where is this IO coming from? The normal block level dio has checks. And in fact they are expensive...
How is this expensive when we need the bio cache lines all the time during I/O submission which follows instantly?
This part isn't expensive, it's the general dio checks that are which check memory alignment as well.
Quoted 6 lines
Quoted 2 lines
If we add this one, then we should be able
to kill theblock/fops.cchecks, no?That would mean we'd only get an async error back, and it would be past say page cache invalidation. Not sure that's a good way to handle failed alignment from userspace.
It would fail during submission, at least for sane cases, so it would be readily apparent after submit. Unfortunately we still don't have sane passback of errors there, this is one example and EWOULDBLOCK as well.
Signature · 1 line
Jens Axboe
Quoted 10 lines · Jens Axboe
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Jens Axboe
Where is this IO coming from? The normal block level dio has checks. And in fact they are expensive...
How is this expensive when we need the bio cache lines all the time during I/O submission which follows instantly?
This part isn't expensive, it's the general dio checks that are which check memory alignment as well.
Well, the memory alignment has to walk over the whole iterator, so it definitively is way more expensive.
Quoted 47 lines · Jens Axboe
Quoted 46 lines · Ming Lei
For any bio with data, its start sector and size have to be aligned with the queue's logical block size.
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.Cc Mikulas Patocka <[email protected]>Cc Mike Snitzer <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)block/blk-core.c +14
-0@@ -729,6 +729,20 @@ 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; +unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -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 +794,9 @@ void submit_bio_noacct(struct bio *bio) } }
Quoted 9 lines
block/blk-core.c void submit_bio_noacct(struct bio *bio) · line 801
Quoted 5 lines
block/blk-core.c +3
-0+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);Where is this IO coming from? The normal block level dio has checks. And in fact they are expensive... If we add this one, then we should be able to kill the
block/fops.cchecks, no?
I think Most of fs code should send good bio since I didn't trigger it in xfstests.
But we still have md, dm, bcache and target code which build bio in their way. The reported issue is from device mapper integrity code.
Yes, all (offset & size) alignment in fops.c shouldn't be needed any more.
Thanks,
Ming
Quoted 48 lines · Mikulas Patocka
Quoted 28 lines · Mike Snitzer
Quoted 25 lines · Ming Lei
For any bio with data, its start sector and size have to be aligned with the queue's logical block size.
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.Cc Mikulas Patocka <[email protected]>Cc Mike Snitzer <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)block/blk-core.c +0
-0@@ -729,6 +729,20 @@ void submit_bio_noacct_nocheck(struct bio *bio)__submit_bio_noacct (bio); }Quoted 13 lines
block/blk-core.c void submit_bio_noacct_nocheck(struct bio *bio) · line 744
Quoted 13 lines
block/blk-core.c +13
-0+static bool bio_check_alignment (struct bio *bio,struct request_queue *q) +{ +unsigned int bs = q->limits.logical_block_size; +unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 ))) +return false; + +return true; +}I would change it to
Quoted 2 lines
if (
unlikely(((bi_iter.bi_sector | bio_sectors(bio)) & ((queue_logical_block_size(q) >> 9) - 1)) != 0))return false;
What if bio->bi_iter.bi_size isn't aligned with 512? The above check can't find that at all.
Quoted 24 lines
Quoted 24 lines
Quoted 18 lines
block/blk-core.c +3
-0/** * 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 +794,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);Signature · 1 line
2.41.0This check would really help more quickly find buggy code, but it would be unfortunate for these extra checks to be required in production. It feels like this is the type of check that should be wrapped by a debug CONFIG option (so only debug kernels have it).
Quoted 7 lines
Quoted 3 lines
Do we already have an appropriate CONFIG option to use?
Mike
But then, the system would crash with the config option being 'n' and return an error with the config option being 'y' - which would be unfortunate.
Yes, the check is basically zero-cost, not necessary to add config to make things more complicated.
Thanks,
Ming
Quoted 8 lines · Ming Lei
Quoted 4 lines · Mikulas Patocka
I would change it to
if (
unlikely(((bi_iter.bi_sector | bio_sectors(bio)) & ((queue_logical_block_size(q) >> 9) - 1)) != 0))return false;What if
bio->bi_iter.bi_sizeisn't aligned with 512? The above check can't find that at all.
Shouldn't that mean this check doesn't apply to REQ_OP_DRV_IN/OUT? Those ops don't necessarily imply any alignment requirements. It may not matter here since it looks like all existing users go through blk_execute_rq() instead of submit_bio(), but there are other checks for DRV_IN/OUT in this path, so I guess it is supposed to be supported?
Mikulas Patocka
maintainerQuoted 19 lines · Ming Lei
Quoted 18 lines · Mikulas Patocka
Quoted 15 lines
Quoted 5 lines
block/blk-core.c +3
-0+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+unsigned int size = bio->bi_iter.bi_size; + +if (size & (bs -1 )) +return false; + +if (size && ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 ))) +return false; + +return true; +}I would change it to
Quoted 5 lines
Quoted 2 lines
if (
unlikely(((bi_iter.bi_sector | bio_sectors(bio)) & ((queue_logical_block_size(q) >> 9) - 1)) != 0))return false;What if
bio->bi_iter.bi_sizeisn't aligned with 512? The above check can't find that at all.
Could it happen that bi_size is not aligned to 512? I haven't seen such a bio yet. If you have seen it, say where was it created.
Mikulas
Quoted 11 lines · Keith Busch
Quoted 8 lines · Ming Lei
Quoted 4 lines · Mikulas Patocka
I would change it to
if (
unlikely(((bi_iter.bi_sector | bio_sectors(bio)) & ((queue_logical_block_size(q) >> 9) - 1)) != 0))return false;What if
bio->bi_iter.bi_sizeisn't aligned with 512? The above check can't find that at all.Shouldn't that mean this check doesn't apply to REQ_OP_DRV_IN/OUT?
For REQ_OP_DRV_IN/OUT, only the real user IO command may need the check, and others needn't this check, maybe they don't use .bi_sector or .bi_size at all.
Quoted 4 lines
Those ops don't necessarily imply any alignment requirements. It may not matter here since it looks like all existing users go through
blk_execute_rq()instead ofsubmit_bio(), but there are other checks for DRV_IN/OUT in this path, so I guess it is supposed to be supported?
This patch focuses on FS bio, and passthough request case is more complicated, and we even don't have central entry for real user pt IO request only.
Thanks,
Ming