block: check bio alignment in blk_mq_submit_bio
PATCH 1/1 block: check bio alignment in blk_mq_submit_bio
IO logical block size is one fundamental queue limit, and every IO has to be aligned with logical block size because our bio split can't deal with unaligned bio.
The check has to be done with queue usage counter grabbed because device reconfiguration may change logical block size, and we can prevent the reconfiguration from happening by holding queue usage counter.
logical_block_size stays in the 1st cache line of queue_limits, and this cache line is always fetched in fast path via bio_may_exceed_limits(), so IO perf won't be affected by this check.
--- block/blk-mq.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
block/blk-mq.c +24 -0
@@ -2914,6 +2914,21 @@ static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug,INIT_LIST_HEAD (&rq->queuelist); } +static bool bio_unaligned (const struct bio *bio, +const struct request_queue *q) +{ +unsigned int bs =queue_logical_block_size (q); + +if (bio->bi_iter.bi_size & (bs -1 )) +return true; + +if (bio->bi_iter.bi_size && + ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 ))) +return true; + +return false; +} +/** * blk_mq_submit_bio - Create and send a request to block device. * @bio: Bio pointer.
@@ -2966,6 +2981,15 @@ void blk_mq_submit_bio(struct bio *bio)return ; } +/* +* Device reconfiguration may change logical block size, so alignment +* check has to be done with queue usage counter held +*/ +if (unlikely (bio_unaligned (bio, q))) { +bio_io_error (bio); + goto queue_exit; + } +if (unlikely (bio_may_exceed_limits (bio, &q->limits))) { bio =__bio_split_to_limits (bio, &q->limits, &nr_segs);if (!bio)
Signature · 1 line
2.44.0
Quoted 28 lines · Ming Lei
IO logical block size is one fundamental queue limit, and every IO has to be aligned with logical block size because our bio split can't deal with unaligned bio.
The check has to be done with queue usage counter grabbed because device reconfiguration may change logical block size, and we can prevent the reconfiguration from happening by holding queue usage counter.
logical_block_sizestays in the 1st cache line ofqueue_limits, and this cache line is always fetched in fast path viabio_may_exceed_limits(), so IO perf won't be affected by this check.Cc Yi Zhang <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Ye Bin <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-mq.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)block/blk-mq.c +0
-0@@ -2914,6 +2914,21 @@ static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug,INIT_LIST_HEAD (&rq->queuelist); }
block/blk-mq.c static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug, · line 2926
Quoted 10 lines
block/blk-mq.c +10
-0+static bool bio_unaligned (const struct bio *bio, +const struct request_queue *q) +{ +unsigned int bs =queue_logical_block_size (q); + +if (bio->bi_iter.bi_size & (bs -1 )) +return true; + +if (bio->bi_iter.bi_size && + ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 )))
Hmmm... Some BIO operations have a 0 size but do specify a sector (e.g. zone management operations). So this seems incorrect to me...
Quoted 24 lines
block/blk-mq.c +14
-0+return true; + +return false; +} +/** * blk_mq_submit_bio - Create and send a request to block device. * @bio: Bio pointer. @@ -2966,6 +2981,15 @@ void blk_mq_submit_bio(struct bio *bio)return ; } +/* +* Device reconfiguration may change logical block size, so alignment +* check has to be done with queue usage counter held +*/ +if (unlikely (bio_unaligned (bio, q))) { +bio_io_error (bio); + goto queue_exit; + } +if (unlikely (bio_may_exceed_limits (bio, &q->limits))) { bio =__bio_split_to_limits (bio, &q->limits, &nr_segs);if (!bio)
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 29 lines · Damien Le Moal
Quoted 28 lines · Ming Lei
IO logical block size is one fundamental queue limit, and every IO has to be aligned with logical block size because our bio split can't deal with unaligned bio.
The check has to be done with queue usage counter grabbed because device reconfiguration may change logical block size, and we can prevent the reconfiguration from happening by holding queue usage counter.
logical_block_sizestays in the 1st cache line ofqueue_limits, and this cache line is always fetched in fast path viabio_may_exceed_limits(), so IO perf won't be affected by this check.Cc Yi Zhang <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Ye Bin <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-mq.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)block/blk-mq.c +0
-0@@ -2914,6 +2914,21 @@ static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug,INIT_LIST_HEAD (&rq->queuelist); }
Quoted 13 lines
block/blk-mq.c static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug, · line 2926
Quoted 10 lines
block/blk-mq.c +10
-0+static bool bio_unaligned (const struct bio *bio, +const struct request_queue *q) +{ +unsigned int bs =queue_logical_block_size (q); + +if (bio->bi_iter.bi_size & (bs -1 )) +return true; + +if (bio->bi_iter.bi_size && + ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 )))Hmmm... Some BIO operations have a 0 size but do specify a sector (e.g. zone management operations). So this seems incorrect to me...
I meant to say, why not checking the sector alignment for these BIOs as well ? Something like:
static bool bio_unaligned(const struct bio *bio,
const struct request_queue *q)
{
unsigned int bs_mask = queue_logical_block_size(q) - 1;
return (bio->bi_iter.bi_size & bs_mask) ||
((bio->bi_iter.bi_sector << SECTOR_SHIFT) & bs_mask);
}Quoted 26 lines
Quoted 25 lines
block/blk-mq.c +14
-0+return true; + +return false; +} +/** * blk_mq_submit_bio - Create and send a request to block device. * @bio: Bio pointer. @@ -2966,6 +2981,15 @@ void blk_mq_submit_bio(struct bio *bio)return ; } +/* +* Device reconfiguration may change logical block size, so alignment +* check has to be done with queue usage counter held +*/ +if (unlikely (bio_unaligned (bio, q))) { +bio_io_error (bio); + goto queue_exit; + } +if (unlikely (bio_may_exceed_limits (bio, &q->limits))) { bio =__bio_split_to_limits (bio, &q->limits, &nr_segs);if (!bio)
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 4 lines · Damien Le Moal
static bool bio_unaligned(const struct bio *bio,
const structrequest_queue*q){ unsigned int bs_mask = queue_logical_block_size(q) - 1;
Please avoid use of the queue helpers. This should be:
unsigned int bs_mask = bdev_logical_block_size(bio->bi_bdev);
Quoted 12 lines · Ming Lei
IO logical block size is one fundamental queue limit, and every IO has to be aligned with logical block size because our bio split can't deal with unaligned bio.
The check has to be done with queue usage counter grabbed because device reconfiguration may change logical block size, and we can prevent the reconfiguration from happening by holding queue usage counter.
logical_block_sizestays in the 1st cache line ofqueue_limits, and this cache line is always fetched in fast path viabio_may_exceed_limits(), so IO perf won't be affected by this check.
Quoted 8 lines
Cc Yi Zhang <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Ye Bin <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-mq.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
Is this still an issue after the atomic queue limits patchset from Christoph? One of the changes there is that we now always freeze the queue before changing any limits.
So really this check should never trigger.
Hmm?
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
Quoted 29 lines · Damien Le Moal
Quoted 28 lines · Ming Lei
IO logical block size is one fundamental queue limit, and every IO has to be aligned with logical block size because our bio split can't deal with unaligned bio.
The check has to be done with queue usage counter grabbed because device reconfiguration may change logical block size, and we can prevent the reconfiguration from happening by holding queue usage counter.
logical_block_sizestays in the 1st cache line ofqueue_limits, and this cache line is always fetched in fast path viabio_may_exceed_limits(), so IO perf won't be affected by this check.Cc Yi Zhang <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Ye Bin <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-mq.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)block/blk-mq.c +0
-0@@ -2914,6 +2914,21 @@ static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug,INIT_LIST_HEAD (&rq->queuelist); }
Quoted 13 lines
block/blk-mq.c static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug, · line 2926
Quoted 10 lines
block/blk-mq.c +10
-0+static bool bio_unaligned (const struct bio *bio, +const struct request_queue *q) +{ +unsigned int bs =queue_logical_block_size (q); + +if (bio->bi_iter.bi_size & (bs -1 )) +return true; + +if (bio->bi_iter.bi_size && + ((bio->bi_iter.bi_sector <<SECTOR_SHIFT ) & (bs -1 )))Hmmm... Some BIO operations have a 0 size but do specify a sector (e.g. zone management operations).
If we add the check for all type of IO, it requires ->bi_sector to be meaningful for zero size bio. I am not sure if it is always true, such as RESET_ALL.
Quoted 1 line
So this seems incorrect to me...
It is correct, but only cover bio with real ->bi_sector & ->bi_size.
Thanks,
Ming
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Damien Le Moal
static bool bio_unaligned(const struct bio *bio,
const structrequest_queue*q){ unsigned int bs_mask = queue_logical_block_size(q) - 1;Please avoid use of the queue helpers. This should be:
unsigned int bs_mask = bdev_logical_block_size(bio->bi_bdev);
It is one blk-mq internal helper, I think queue helper is more efficient since it is definitely in fast path.
Thanks,
Ming
Quoted 21 lines · Hannes Reinecke
Quoted 12 lines · Ming Lei
IO logical block size is one fundamental queue limit, and every IO has to be aligned with logical block size because our bio split can't deal with unaligned bio.
The check has to be done with queue usage counter grabbed because device reconfiguration may change logical block size, and we can prevent the reconfiguration from happening by holding queue usage counter.
logical_block_sizestays in the 1st cache line ofqueue_limits, and this cache line is always fetched in fast path viabio_may_exceed_limits(), so IO perf won't be affected by this check.Quoted 8 lines
Cc Yi Zhang <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Ye Bin <[email protected]>Signed-off-by Ming Lei <[email protected]>--- block/blk-mq.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
Quoted 5 lines
Is this still an issue after the atomic queue limits patchset from Christoph?
One of the changes there is that we now always freeze the queue before changing any limits.
So really this check should never trigger.
submit_bio() just blocks on queue freezing, and once queue is unfrozen, submit_bio() still moves on, then unaligned bio is issued to driver/hardware, please see:
Thanks,
Ming
Quoted 3 lines · Ming Lei
If we add the check for all type of IO, it requires ->bi_sector to be meaningful for zero size bio. I am not sure if it is always true, such as
RESET_ALL.
meaningful or initialized to zero. Given that bio_init initializes it to zero we should generally be fine (and are for BIO_OP_ZONE_RESET_ALL for all callers in tree).
Quoted 8 lines · Ming Lei
Quoted 5 lines
Quoted 1 line
unsigned int bs_mask = queue_logical_block_size(q) - 1;Please avoid use of the queue helpers. This should be:
unsigned int bs_mask = bdev_logical_block_size(bio->bi_bdev);It is one blk-mq internal helper, I think queue helper is more efficient since it is definitely in fast path.
Does it actually generate different code for you with all the inlining modern compilers do?
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Ming Lei
Quoted 4 lines
Quoted 1 line
unsigned int bs_mask = queue_logical_block_size(q) - 1;Please avoid use of the queue helpers. This should be:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
unsigned int bs_mask = bdev_logical_block_size(bio->bi_bdev);It is one blk-mq internal helper, I think queue helper is more efficient since it is definitely in fast path.
Does it actually generate different code for you with all the inlining modern compilers do?
It is hard to answer, cause there are so many compilers(versions).
I definitely agree bdev_logical_block_size() should be used in external users, but it is fine to use queue helper in block internal functions.
thanks,
Ming
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Ming Lei
If we add the check for all type of IO, it requires ->bi_sector to be meaningful for zero size bio. I am not sure if it is always true, such as
RESET_ALL.meaningful or initialized to zero. Given that
bio_initinitializes it to zero we should generally be fine (and are forBIO_OP_ZONE_RESET_ALLfor all callers in tree).
Fine, let's fail this kind potential not-well-initialized bio, which is brittle anyway.
Thanks,
Ming