Skip to content

Commit 0676c43

Browse files
Ming Leiaxboe
authored andcommitted
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. Cc: Yi Zhang <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Ye Bin <[email protected]> Cc: [email protected] Signed-off-by: Ming Lei <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent aa6ff4e commit 0676c43

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

block/blk-mq.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,6 +2909,17 @@ static void blk_mq_use_cached_rq(struct request *rq, struct blk_plug *plug,
29092909
INIT_LIST_HEAD(&rq->queuelist);
29102910
}
29112911

2912+
static bool bio_unaligned(const struct bio *bio, struct request_queue *q)
2913+
{
2914+
unsigned int bs_mask = queue_logical_block_size(q) - 1;
2915+
2916+
/* .bi_sector of any zero sized bio need to be initialized */
2917+
if ((bio->bi_iter.bi_size & bs_mask) ||
2918+
((bio->bi_iter.bi_sector << SECTOR_SHIFT) & bs_mask))
2919+
return true;
2920+
return false;
2921+
}
2922+
29122923
/**
29132924
* blk_mq_submit_bio - Create and send a request to block device.
29142925
* @bio: Bio pointer.
@@ -2961,6 +2972,15 @@ void blk_mq_submit_bio(struct bio *bio)
29612972
return;
29622973
}
29632974

2975+
/*
2976+
* Device reconfiguration may change logical block size, so alignment
2977+
* check has to be done with queue usage counter held
2978+
*/
2979+
if (unlikely(bio_unaligned(bio, q))) {
2980+
bio_io_error(bio);
2981+
goto queue_exit;
2982+
}
2983+
29642984
if (unlikely(bio_may_exceed_limits(bio, &q->limits))) {
29652985
bio = __bio_split_to_limits(bio, &q->limits, &nr_segs);
29662986
if (!bio)

0 commit comments

Comments
 (0)