Since multi-page bvecs are supported there can be multiple segments per bvec (see also bvec_split_segs()). Hence this patch that calculates the number of segments per bvec instead of assuming that there is only one segment per bvec. Cc: Christoph Hellwig <hch@xxxxxx> Cc: Ming Lei <ming.lei@xxxxxxxxxx> Cc: Keith Busch <kbusch@xxxxxxxxxx> Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx> --- block/blk-map.c | 14 +++++++++++++- block/blk-mq.c | 2 ++ block/blk.h | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/block/blk-map.c b/block/blk-map.c index 4505307d758e..2976a8d68992 100644 --- a/block/blk-map.c +++ b/block/blk-map.c @@ -529,6 +529,18 @@ static struct bio *bio_copy_kern(struct request_queue *q, void *data, return ERR_PTR(-ENOMEM); } +/* Number of DMA segments required to transfer @bytes data. */ +unsigned int blk_segments(const struct queue_limits *limits, unsigned int bytes) +{ + const unsigned int mss = limits->max_segment_size; + + if (bytes <= mss) + return 1; + if (is_power_of_2(mss)) + return round_up(bytes, mss) >> ilog2(mss); + return (bytes + mss - 1) / mss; +} + /* * Append a bio to a passthrough request. Only works if the bio can be merged * into the request based on the driver constraints. @@ -540,7 +552,7 @@ int blk_rq_append_bio(struct request *rq, struct bio *bio) unsigned int nr_segs = 0; bio_for_each_bvec(bv, bio, iter) - nr_segs++; + nr_segs += blk_segments(&rq->q->limits, bv.bv_len); if (!rq->bio) { blk_rq_bio_prep(rq, bio, nr_segs); diff --git a/block/blk-mq.c b/block/blk-mq.c index 8070b6c10e8d..82ae8099afdd 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2883,6 +2883,8 @@ void blk_mq_submit_bio(struct bio *bio) bio = blk_queue_bounce(bio, q); if (bio_may_exceed_limits(bio, &q->limits)) bio = __bio_split_to_limits(bio, &q->limits, &nr_segs); + else if (bio->bi_vcnt == 1) + nr_segs = blk_segments(&q->limits, bio->bi_io_vec[0].bv_len); if (!bio_integrity_prep(bio)) return; diff --git a/block/blk.h b/block/blk.h index 7f9e089ab1f7..342ba7d86e87 100644 --- a/block/blk.h +++ b/block/blk.h @@ -81,6 +81,9 @@ struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs, gfp_t gfp_mask); void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs); +unsigned int blk_segments(const struct queue_limits *limits, + unsigned int bytes); + static inline bool biovec_phys_mergeable(struct request_queue *q, struct bio_vec *vec1, struct bio_vec *vec2) {