1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d6d48196SJens Axboe /* 3d6d48196SJens Axboe * Functions related to segment and merge handling 4d6d48196SJens Axboe */ 5d6d48196SJens Axboe #include <linux/kernel.h> 6d6d48196SJens Axboe #include <linux/module.h> 7d6d48196SJens Axboe #include <linux/bio.h> 8d6d48196SJens Axboe #include <linux/blkdev.h> 9d6d48196SJens Axboe #include <linux/scatterlist.h> 10d6d48196SJens Axboe 11cda22646SMike Krinkin #include <trace/events/block.h> 12cda22646SMike Krinkin 13d6d48196SJens Axboe #include "blk.h" 14d6d48196SJens Axboe 15*e9907009SChristoph Hellwig /* 16*e9907009SChristoph Hellwig * Check if the two bvecs from two bios can be merged to one segment. If yes, 17*e9907009SChristoph Hellwig * no need to check gap between the two bios since the 1st bio and the 1st bvec 18*e9907009SChristoph Hellwig * in the 2nd bio can be handled in one segment. 19*e9907009SChristoph Hellwig */ 20*e9907009SChristoph Hellwig static inline bool bios_segs_mergeable(struct request_queue *q, 21*e9907009SChristoph Hellwig struct bio *prev, struct bio_vec *prev_last_bv, 22*e9907009SChristoph Hellwig struct bio_vec *next_first_bv) 23*e9907009SChristoph Hellwig { 24*e9907009SChristoph Hellwig if (!BIOVEC_PHYS_MERGEABLE(prev_last_bv, next_first_bv)) 25*e9907009SChristoph Hellwig return false; 26*e9907009SChristoph Hellwig if (!BIOVEC_SEG_BOUNDARY(q, prev_last_bv, next_first_bv)) 27*e9907009SChristoph Hellwig return false; 28*e9907009SChristoph Hellwig if (prev->bi_seg_back_size + next_first_bv->bv_len > 29*e9907009SChristoph Hellwig queue_max_segment_size(q)) 30*e9907009SChristoph Hellwig return false; 31*e9907009SChristoph Hellwig return true; 32*e9907009SChristoph Hellwig } 33*e9907009SChristoph Hellwig 34*e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q, 35*e9907009SChristoph Hellwig struct request *prev_rq, struct bio *prev, struct bio *next) 36*e9907009SChristoph Hellwig { 37*e9907009SChristoph Hellwig struct bio_vec pb, nb; 38*e9907009SChristoph Hellwig 39*e9907009SChristoph Hellwig if (!bio_has_data(prev) || !queue_virt_boundary(q)) 40*e9907009SChristoph Hellwig return false; 41*e9907009SChristoph Hellwig 42*e9907009SChristoph Hellwig /* 43*e9907009SChristoph Hellwig * Don't merge if the 1st bio starts with non-zero offset, otherwise it 44*e9907009SChristoph Hellwig * is quite difficult to respect the sg gap limit. We work hard to 45*e9907009SChristoph Hellwig * merge a huge number of small single bios in case of mkfs. 46*e9907009SChristoph Hellwig */ 47*e9907009SChristoph Hellwig if (prev_rq) 48*e9907009SChristoph Hellwig bio_get_first_bvec(prev_rq->bio, &pb); 49*e9907009SChristoph Hellwig else 50*e9907009SChristoph Hellwig bio_get_first_bvec(prev, &pb); 51*e9907009SChristoph Hellwig if (pb.bv_offset) 52*e9907009SChristoph Hellwig return true; 53*e9907009SChristoph Hellwig 54*e9907009SChristoph Hellwig /* 55*e9907009SChristoph Hellwig * We don't need to worry about the situation that the merged segment 56*e9907009SChristoph Hellwig * ends in unaligned virt boundary: 57*e9907009SChristoph Hellwig * 58*e9907009SChristoph Hellwig * - if 'pb' ends aligned, the merged segment ends aligned 59*e9907009SChristoph Hellwig * - if 'pb' ends unaligned, the next bio must include 60*e9907009SChristoph Hellwig * one single bvec of 'nb', otherwise the 'nb' can't 61*e9907009SChristoph Hellwig * merge with 'pb' 62*e9907009SChristoph Hellwig */ 63*e9907009SChristoph Hellwig bio_get_last_bvec(prev, &pb); 64*e9907009SChristoph Hellwig bio_get_first_bvec(next, &nb); 65*e9907009SChristoph Hellwig if (bios_segs_mergeable(q, prev, &pb, &nb)) 66*e9907009SChristoph Hellwig return false; 67*e9907009SChristoph Hellwig return __bvec_gap_to_prev(q, &pb, nb.bv_offset); 68*e9907009SChristoph Hellwig } 69*e9907009SChristoph Hellwig 70*e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio) 71*e9907009SChristoph Hellwig { 72*e9907009SChristoph Hellwig return bio_will_gap(req->q, req, req->biotail, bio); 73*e9907009SChristoph Hellwig } 74*e9907009SChristoph Hellwig 75*e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio) 76*e9907009SChristoph Hellwig { 77*e9907009SChristoph Hellwig return bio_will_gap(req->q, NULL, bio, req->bio); 78*e9907009SChristoph Hellwig } 79*e9907009SChristoph Hellwig 8054efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 8154efd50bSKent Overstreet struct bio *bio, 82bdced438SMing Lei struct bio_set *bs, 83bdced438SMing Lei unsigned *nsegs) 8454efd50bSKent Overstreet { 8554efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 8654efd50bSKent Overstreet int alignment; 8754efd50bSKent Overstreet sector_t tmp; 8854efd50bSKent Overstreet unsigned split_sectors; 8954efd50bSKent Overstreet 90bdced438SMing Lei *nsegs = 1; 91bdced438SMing Lei 9254efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 9354efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 9454efd50bSKent Overstreet 9554efd50bSKent Overstreet max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); 9654efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 9754efd50bSKent Overstreet 9854efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 9954efd50bSKent Overstreet /* XXX: warn */ 10054efd50bSKent Overstreet return NULL; 10154efd50bSKent Overstreet } 10254efd50bSKent Overstreet 10354efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 10454efd50bSKent Overstreet return NULL; 10554efd50bSKent Overstreet 10654efd50bSKent Overstreet split_sectors = max_discard_sectors; 10754efd50bSKent Overstreet 10854efd50bSKent Overstreet /* 10954efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 11054efd50bSKent Overstreet * the previous aligned sector. 11154efd50bSKent Overstreet */ 11254efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 11354efd50bSKent Overstreet 11454efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 11554efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 11654efd50bSKent Overstreet 11754efd50bSKent Overstreet if (split_sectors > tmp) 11854efd50bSKent Overstreet split_sectors -= tmp; 11954efd50bSKent Overstreet 12054efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 12154efd50bSKent Overstreet } 12254efd50bSKent Overstreet 123885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, 124885fa13fSChristoph Hellwig struct bio *bio, struct bio_set *bs, unsigned *nsegs) 125885fa13fSChristoph Hellwig { 126885fa13fSChristoph Hellwig *nsegs = 1; 127885fa13fSChristoph Hellwig 128885fa13fSChristoph Hellwig if (!q->limits.max_write_zeroes_sectors) 129885fa13fSChristoph Hellwig return NULL; 130885fa13fSChristoph Hellwig 131885fa13fSChristoph Hellwig if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) 132885fa13fSChristoph Hellwig return NULL; 133885fa13fSChristoph Hellwig 134885fa13fSChristoph Hellwig return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); 135885fa13fSChristoph Hellwig } 136885fa13fSChristoph Hellwig 13754efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 13854efd50bSKent Overstreet struct bio *bio, 139bdced438SMing Lei struct bio_set *bs, 140bdced438SMing Lei unsigned *nsegs) 14154efd50bSKent Overstreet { 142bdced438SMing Lei *nsegs = 1; 143bdced438SMing Lei 14454efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 14554efd50bSKent Overstreet return NULL; 14654efd50bSKent Overstreet 14754efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 14854efd50bSKent Overstreet return NULL; 14954efd50bSKent Overstreet 15054efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 15154efd50bSKent Overstreet } 15254efd50bSKent Overstreet 153d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 154d0e5fbb0SMing Lei struct bio *bio) 155d0e5fbb0SMing Lei { 156d0e5fbb0SMing Lei unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector); 157d0e5fbb0SMing Lei unsigned mask = queue_logical_block_size(q) - 1; 158d0e5fbb0SMing Lei 159d0e5fbb0SMing Lei /* aligned to logical block size */ 160d0e5fbb0SMing Lei sectors &= ~(mask >> 9); 161d0e5fbb0SMing Lei 162d0e5fbb0SMing Lei return sectors; 163d0e5fbb0SMing Lei } 164d0e5fbb0SMing Lei 16554efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 16654efd50bSKent Overstreet struct bio *bio, 167bdced438SMing Lei struct bio_set *bs, 168bdced438SMing Lei unsigned *segs) 16954efd50bSKent Overstreet { 1705014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 17154efd50bSKent Overstreet struct bvec_iter iter; 1728ae12666SKent Overstreet unsigned seg_size = 0, nsegs = 0, sectors = 0; 17302e70742SMing Lei unsigned front_seg_size = bio->bi_seg_front_size; 17402e70742SMing Lei bool do_split = true; 17502e70742SMing Lei struct bio *new = NULL; 176d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 17754efd50bSKent Overstreet 17854efd50bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 17954efd50bSKent Overstreet /* 18054efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 18154efd50bSKent Overstreet * offset would create a gap, disallow it. 18254efd50bSKent Overstreet */ 1835014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 18454efd50bSKent Overstreet goto split; 18554efd50bSKent Overstreet 186d0e5fbb0SMing Lei if (sectors + (bv.bv_len >> 9) > max_sectors) { 187e36f6204SKeith Busch /* 188e36f6204SKeith Busch * Consider this a new segment if we're splitting in 189e36f6204SKeith Busch * the middle of this vector. 190e36f6204SKeith Busch */ 191e36f6204SKeith Busch if (nsegs < queue_max_segments(q) && 192d0e5fbb0SMing Lei sectors < max_sectors) { 193e36f6204SKeith Busch nsegs++; 194d0e5fbb0SMing Lei sectors = max_sectors; 195e36f6204SKeith Busch } 196e36f6204SKeith Busch goto split; 197e36f6204SKeith Busch } 198e36f6204SKeith Busch 1995014c311SJens Axboe if (bvprvp && blk_queue_cluster(q)) { 200b4b6cb61SMing Lei if (seg_size + bv.bv_len > queue_max_segment_size(q)) 201b4b6cb61SMing Lei goto new_segment; 2025014c311SJens Axboe if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv)) 20354efd50bSKent Overstreet goto new_segment; 2045014c311SJens Axboe if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv)) 20554efd50bSKent Overstreet goto new_segment; 20654efd50bSKent Overstreet 20754efd50bSKent Overstreet seg_size += bv.bv_len; 20854efd50bSKent Overstreet bvprv = bv; 209578270bfSMing Lei bvprvp = &bvprv; 21052cc6eeaSMing Lei sectors += bv.bv_len >> 9; 211a88d32afSMing Lei 21254efd50bSKent Overstreet continue; 21354efd50bSKent Overstreet } 21454efd50bSKent Overstreet new_segment: 21554efd50bSKent Overstreet if (nsegs == queue_max_segments(q)) 21654efd50bSKent Overstreet goto split; 21754efd50bSKent Overstreet 2186a501bf0SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 2196a501bf0SMing Lei front_seg_size = seg_size; 2206a501bf0SMing Lei 22154efd50bSKent Overstreet nsegs++; 22254efd50bSKent Overstreet bvprv = bv; 223578270bfSMing Lei bvprvp = &bvprv; 22454efd50bSKent Overstreet seg_size = bv.bv_len; 22552cc6eeaSMing Lei sectors += bv.bv_len >> 9; 22602e70742SMing Lei 22754efd50bSKent Overstreet } 22854efd50bSKent Overstreet 22902e70742SMing Lei do_split = false; 23054efd50bSKent Overstreet split: 231bdced438SMing Lei *segs = nsegs; 23202e70742SMing Lei 23302e70742SMing Lei if (do_split) { 23402e70742SMing Lei new = bio_split(bio, sectors, GFP_NOIO, bs); 23502e70742SMing Lei if (new) 23602e70742SMing Lei bio = new; 23702e70742SMing Lei } 23802e70742SMing Lei 2396a501bf0SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 2406a501bf0SMing Lei front_seg_size = seg_size; 24102e70742SMing Lei bio->bi_seg_front_size = front_seg_size; 24202e70742SMing Lei if (seg_size > bio->bi_seg_back_size) 24302e70742SMing Lei bio->bi_seg_back_size = seg_size; 24402e70742SMing Lei 24502e70742SMing Lei return do_split ? new : NULL; 24654efd50bSKent Overstreet } 24754efd50bSKent Overstreet 248af67c31fSNeilBrown void blk_queue_split(struct request_queue *q, struct bio **bio) 24954efd50bSKent Overstreet { 250bdced438SMing Lei struct bio *split, *res; 251bdced438SMing Lei unsigned nsegs; 25254efd50bSKent Overstreet 2537afafc8aSAdrian Hunter switch (bio_op(*bio)) { 2547afafc8aSAdrian Hunter case REQ_OP_DISCARD: 2557afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 256338aa96dSKent Overstreet split = blk_bio_discard_split(q, *bio, &q->bio_split, &nsegs); 2577afafc8aSAdrian Hunter break; 258a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 259338aa96dSKent Overstreet split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, &nsegs); 260a6f0788eSChaitanya Kulkarni break; 2617afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 262338aa96dSKent Overstreet split = blk_bio_write_same_split(q, *bio, &q->bio_split, &nsegs); 2637afafc8aSAdrian Hunter break; 2647afafc8aSAdrian Hunter default: 265338aa96dSKent Overstreet split = blk_bio_segment_split(q, *bio, &q->bio_split, &nsegs); 2667afafc8aSAdrian Hunter break; 2677afafc8aSAdrian Hunter } 268bdced438SMing Lei 269bdced438SMing Lei /* physical segments can be figured out during splitting */ 270bdced438SMing Lei res = split ? split : *bio; 271bdced438SMing Lei res->bi_phys_segments = nsegs; 272bdced438SMing Lei bio_set_flag(res, BIO_SEG_VALID); 27354efd50bSKent Overstreet 27454efd50bSKent Overstreet if (split) { 2756ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 2761eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 2776ac45aebSMing Lei 278cd4a4ae4SJens Axboe /* 279cd4a4ae4SJens Axboe * Since we're recursing into make_request here, ensure 280cd4a4ae4SJens Axboe * that we mark this bio as already having entered the queue. 281cd4a4ae4SJens Axboe * If not, and the queue is going away, we can get stuck 282cd4a4ae4SJens Axboe * forever on waiting for the queue reference to drop. But 283cd4a4ae4SJens Axboe * that will never happen, as we're already holding a 284cd4a4ae4SJens Axboe * reference to it. 285cd4a4ae4SJens Axboe */ 286cd4a4ae4SJens Axboe bio_set_flag(*bio, BIO_QUEUE_ENTERED); 287cd4a4ae4SJens Axboe 28854efd50bSKent Overstreet bio_chain(split, *bio); 289cda22646SMike Krinkin trace_block_split(q, split, (*bio)->bi_iter.bi_sector); 29054efd50bSKent Overstreet generic_make_request(*bio); 29154efd50bSKent Overstreet *bio = split; 29254efd50bSKent Overstreet } 29354efd50bSKent Overstreet } 29454efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 29554efd50bSKent Overstreet 2961e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 29707388549SMing Lei struct bio *bio, 29807388549SMing Lei bool no_sg_merge) 299d6d48196SJens Axboe { 3007988613bSKent Overstreet struct bio_vec bv, bvprv = { NULL }; 30154efd50bSKent Overstreet int cluster, prev = 0; 3021e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 30359247eaeSJens Axboe struct bio *fbio, *bbio; 3047988613bSKent Overstreet struct bvec_iter iter; 305d6d48196SJens Axboe 3061e428079SJens Axboe if (!bio) 3071e428079SJens Axboe return 0; 308d6d48196SJens Axboe 309a6f0788eSChaitanya Kulkarni switch (bio_op(bio)) { 310a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 311a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 312a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 313f9d03f96SChristoph Hellwig return 0; 314f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 3155cb8850cSKent Overstreet return 1; 316a6f0788eSChaitanya Kulkarni } 3175cb8850cSKent Overstreet 3181e428079SJens Axboe fbio = bio; 319e692cb66SMartin K. Petersen cluster = blk_queue_cluster(q); 3205df97b91SMikulas Patocka seg_size = 0; 3212c8919deSAndi Kleen nr_phys_segs = 0; 3221e428079SJens Axboe for_each_bio(bio) { 3237988613bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 324d6d48196SJens Axboe /* 32505f1dd53SJens Axboe * If SG merging is disabled, each bio vector is 32605f1dd53SJens Axboe * a segment 32705f1dd53SJens Axboe */ 32805f1dd53SJens Axboe if (no_sg_merge) 32905f1dd53SJens Axboe goto new_segment; 33005f1dd53SJens Axboe 33154efd50bSKent Overstreet if (prev && cluster) { 3327988613bSKent Overstreet if (seg_size + bv.bv_len 333ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 334d6d48196SJens Axboe goto new_segment; 3357988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) 336d6d48196SJens Axboe goto new_segment; 3377988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) 338d6d48196SJens Axboe goto new_segment; 339d6d48196SJens Axboe 3407988613bSKent Overstreet seg_size += bv.bv_len; 341d6d48196SJens Axboe bvprv = bv; 342d6d48196SJens Axboe continue; 343d6d48196SJens Axboe } 344d6d48196SJens Axboe new_segment: 3451e428079SJens Axboe if (nr_phys_segs == 1 && seg_size > 3461e428079SJens Axboe fbio->bi_seg_front_size) 3471e428079SJens Axboe fbio->bi_seg_front_size = seg_size; 34886771427SFUJITA Tomonori 349d6d48196SJens Axboe nr_phys_segs++; 350d6d48196SJens Axboe bvprv = bv; 35154efd50bSKent Overstreet prev = 1; 3527988613bSKent Overstreet seg_size = bv.bv_len; 353d6d48196SJens Axboe } 35459247eaeSJens Axboe bbio = bio; 3551e428079SJens Axboe } 356d6d48196SJens Axboe 35759247eaeSJens Axboe if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) 35859247eaeSJens Axboe fbio->bi_seg_front_size = seg_size; 35959247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 36059247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 3611e428079SJens Axboe 3621e428079SJens Axboe return nr_phys_segs; 3631e428079SJens Axboe } 3641e428079SJens Axboe 3651e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 3661e428079SJens Axboe { 36707388549SMing Lei bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE, 36807388549SMing Lei &rq->q->queue_flags); 36907388549SMing Lei 37007388549SMing Lei rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio, 37107388549SMing Lei no_sg_merge); 372d6d48196SJens Axboe } 373d6d48196SJens Axboe 374d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 375d6d48196SJens Axboe { 3767f60dcaaSMing Lei unsigned short seg_cnt; 377764f612cSMing Lei 3787f60dcaaSMing Lei /* estimate segment number by bi_vcnt for non-cloned bio */ 3797f60dcaaSMing Lei if (bio_flagged(bio, BIO_CLONED)) 3807f60dcaaSMing Lei seg_cnt = bio_segments(bio); 3817f60dcaaSMing Lei else 3827f60dcaaSMing Lei seg_cnt = bio->bi_vcnt; 3837f60dcaaSMing Lei 3847f60dcaaSMing Lei if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) && 3857f60dcaaSMing Lei (seg_cnt < queue_max_segments(q))) 3867f60dcaaSMing Lei bio->bi_phys_segments = seg_cnt; 38705f1dd53SJens Axboe else { 388d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 3891e428079SJens Axboe 390d6d48196SJens Axboe bio->bi_next = NULL; 3917f60dcaaSMing Lei bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false); 392d6d48196SJens Axboe bio->bi_next = nxt; 39305f1dd53SJens Axboe } 39405f1dd53SJens Axboe 395b7c44ed9SJens Axboe bio_set_flag(bio, BIO_SEG_VALID); 396d6d48196SJens Axboe } 397d6d48196SJens Axboe EXPORT_SYMBOL(blk_recount_segments); 398d6d48196SJens Axboe 399d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 400d6d48196SJens Axboe struct bio *nxt) 401d6d48196SJens Axboe { 4022b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 403f619d254SKent Overstreet 404e692cb66SMartin K. Petersen if (!blk_queue_cluster(q)) 405d6d48196SJens Axboe return 0; 406d6d48196SJens Axboe 40786771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 408ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 409d6d48196SJens Axboe return 0; 410d6d48196SJens Axboe 411e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 412e17fc0a1SDavid Woodhouse return 1; 413e17fc0a1SDavid Woodhouse 414e827091cSMing Lei bio_get_last_bvec(bio, &end_bv); 415e827091cSMing Lei bio_get_first_bvec(nxt, &nxt_bv); 416f619d254SKent Overstreet 417f619d254SKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) 418e17fc0a1SDavid Woodhouse return 0; 419e17fc0a1SDavid Woodhouse 420d6d48196SJens Axboe /* 421e17fc0a1SDavid Woodhouse * bio and nxt are contiguous in memory; check if the queue allows 422d6d48196SJens Axboe * these two to be merged into one 423d6d48196SJens Axboe */ 424f619d254SKent Overstreet if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) 425d6d48196SJens Axboe return 1; 426d6d48196SJens Axboe 427d6d48196SJens Axboe return 0; 428d6d48196SJens Axboe } 429d6d48196SJens Axboe 4307988613bSKent Overstreet static inline void 431963ab9e5SAsias He __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, 4327988613bSKent Overstreet struct scatterlist *sglist, struct bio_vec *bvprv, 433963ab9e5SAsias He struct scatterlist **sg, int *nsegs, int *cluster) 434963ab9e5SAsias He { 435963ab9e5SAsias He 436963ab9e5SAsias He int nbytes = bvec->bv_len; 437963ab9e5SAsias He 4387988613bSKent Overstreet if (*sg && *cluster) { 439b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 440b4b6cb61SMing Lei goto new_segment; 441b4b6cb61SMing Lei 4427988613bSKent Overstreet if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 443963ab9e5SAsias He goto new_segment; 4447988613bSKent Overstreet if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 445963ab9e5SAsias He goto new_segment; 446963ab9e5SAsias He 447963ab9e5SAsias He (*sg)->length += nbytes; 448963ab9e5SAsias He } else { 449963ab9e5SAsias He new_segment: 450963ab9e5SAsias He if (!*sg) 451963ab9e5SAsias He *sg = sglist; 452963ab9e5SAsias He else { 453963ab9e5SAsias He /* 454963ab9e5SAsias He * If the driver previously mapped a shorter 455963ab9e5SAsias He * list, we could see a termination bit 456963ab9e5SAsias He * prematurely unless it fully inits the sg 457963ab9e5SAsias He * table on each mapping. We KNOW that there 458963ab9e5SAsias He * must be more entries here or the driver 459963ab9e5SAsias He * would be buggy, so force clear the 460963ab9e5SAsias He * termination bit to avoid doing a full 461963ab9e5SAsias He * sg_init_table() in drivers for each command. 462963ab9e5SAsias He */ 463c8164d89SPaolo Bonzini sg_unmark_end(*sg); 464963ab9e5SAsias He *sg = sg_next(*sg); 465963ab9e5SAsias He } 466963ab9e5SAsias He 467963ab9e5SAsias He sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); 468963ab9e5SAsias He (*nsegs)++; 469963ab9e5SAsias He } 4707988613bSKent Overstreet *bvprv = *bvec; 471963ab9e5SAsias He } 472963ab9e5SAsias He 473f9d03f96SChristoph Hellwig static inline int __blk_bvec_map_sg(struct request_queue *q, struct bio_vec bv, 474f9d03f96SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **sg) 475f9d03f96SChristoph Hellwig { 476f9d03f96SChristoph Hellwig *sg = sglist; 477f9d03f96SChristoph Hellwig sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 478f9d03f96SChristoph Hellwig return 1; 479f9d03f96SChristoph Hellwig } 480f9d03f96SChristoph Hellwig 4815cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 4825cb8850cSKent Overstreet struct scatterlist *sglist, 4835cb8850cSKent Overstreet struct scatterlist **sg) 4845cb8850cSKent Overstreet { 4855cb8850cSKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 4865cb8850cSKent Overstreet struct bvec_iter iter; 487f9d03f96SChristoph Hellwig int cluster = blk_queue_cluster(q), nsegs = 0; 4885cb8850cSKent Overstreet 4895cb8850cSKent Overstreet for_each_bio(bio) 4905cb8850cSKent Overstreet bio_for_each_segment(bvec, bio, iter) 4915cb8850cSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, 4925cb8850cSKent Overstreet &nsegs, &cluster); 4935cb8850cSKent Overstreet 4945cb8850cSKent Overstreet return nsegs; 4955cb8850cSKent Overstreet } 4965cb8850cSKent Overstreet 497d6d48196SJens Axboe /* 498d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 499d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 500d6d48196SJens Axboe */ 501d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 502d6d48196SJens Axboe struct scatterlist *sglist) 503d6d48196SJens Axboe { 5045cb8850cSKent Overstreet struct scatterlist *sg = NULL; 5055cb8850cSKent Overstreet int nsegs = 0; 506d6d48196SJens Axboe 507f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 508f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, rq->special_vec, sglist, &sg); 509f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 510f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, bio_iovec(rq->bio), sglist, &sg); 511f9d03f96SChristoph Hellwig else if (rq->bio) 5125cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 513f18573abSFUJITA Tomonori 514e8064021SChristoph Hellwig if (unlikely(rq->rq_flags & RQF_COPY_USER) && 5152e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 5162e46e8b2STejun Heo unsigned int pad_len = 5172e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 518f18573abSFUJITA Tomonori 519f18573abSFUJITA Tomonori sg->length += pad_len; 520f18573abSFUJITA Tomonori rq->extra_len += pad_len; 521f18573abSFUJITA Tomonori } 522f18573abSFUJITA Tomonori 5232fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 524a8ebb056SMike Christie if (op_is_write(req_op(rq))) 525db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 526db0a2e00STejun Heo 527da81ed16SDan Williams sg_unmark_end(sg); 528d6d48196SJens Axboe sg = sg_next(sg); 529d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 530d6d48196SJens Axboe q->dma_drain_size, 531d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 532d6d48196SJens Axboe (PAGE_SIZE - 1)); 533d6d48196SJens Axboe nsegs++; 5347a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 535d6d48196SJens Axboe } 536d6d48196SJens Axboe 537d6d48196SJens Axboe if (sg) 538d6d48196SJens Axboe sg_mark_end(sg); 539d6d48196SJens Axboe 54012e57f59SMing Lei /* 54112e57f59SMing Lei * Something must have been wrong if the figured number of 54212e57f59SMing Lei * segment is bigger than number of req's physical segments 54312e57f59SMing Lei */ 544f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 54512e57f59SMing Lei 546d6d48196SJens Axboe return nsegs; 547d6d48196SJens Axboe } 548d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 549d6d48196SJens Axboe 550d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 551d6d48196SJens Axboe struct request *req, 552d6d48196SJens Axboe struct bio *bio) 553d6d48196SJens Axboe { 554d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 555d6d48196SJens Axboe 55613f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 55713f05c8dSMartin K. Petersen goto no_merge; 55813f05c8dSMartin K. Petersen 5594eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(q, req, bio) == false) 56013f05c8dSMartin K. Petersen goto no_merge; 561d6d48196SJens Axboe 562d6d48196SJens Axboe /* 563d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 564d6d48196SJens Axboe * counters. 565d6d48196SJens Axboe */ 566d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 567d6d48196SJens Axboe return 1; 56813f05c8dSMartin K. Petersen 56913f05c8dSMartin K. Petersen no_merge: 570e0c72300SRitesh Harjani req_set_nomerge(q, req); 57113f05c8dSMartin K. Petersen return 0; 572d6d48196SJens Axboe } 573d6d48196SJens Axboe 574d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 575d6d48196SJens Axboe struct bio *bio) 576d6d48196SJens Axboe { 5775e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 5785e7c4274SJens Axboe return 0; 5797f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5807f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 5817f39add3SSagi Grimberg return 0; 582f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 58317007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 584e0c72300SRitesh Harjani req_set_nomerge(q, req); 585d6d48196SJens Axboe return 0; 586d6d48196SJens Axboe } 5872cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 588d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 5892cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 590d6d48196SJens Axboe blk_recount_segments(q, bio); 591d6d48196SJens Axboe 592d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 593d6d48196SJens Axboe } 594d6d48196SJens Axboe 595d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 596d6d48196SJens Axboe struct bio *bio) 597d6d48196SJens Axboe { 5985e7c4274SJens Axboe 5995e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 6005e7c4274SJens Axboe return 0; 6017f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6027f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 6037f39add3SSagi Grimberg return 0; 604f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 60517007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 606e0c72300SRitesh Harjani req_set_nomerge(q, req); 607d6d48196SJens Axboe return 0; 608d6d48196SJens Axboe } 6092cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 610d6d48196SJens Axboe blk_recount_segments(q, bio); 6112cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 612d6d48196SJens Axboe blk_recount_segments(q, req->bio); 613d6d48196SJens Axboe 614d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 615d6d48196SJens Axboe } 616d6d48196SJens Axboe 617e7e24500SJens Axboe /* 618e7e24500SJens Axboe * blk-mq uses req->special to carry normal driver per-request payload, it 619e7e24500SJens Axboe * does not indicate a prepared command that we cannot merge with. 620e7e24500SJens Axboe */ 621e7e24500SJens Axboe static bool req_no_special_merge(struct request *req) 622e7e24500SJens Axboe { 623e7e24500SJens Axboe struct request_queue *q = req->q; 624e7e24500SJens Axboe 625e7e24500SJens Axboe return !q->mq_ops && req->special; 626e7e24500SJens Axboe } 627e7e24500SJens Axboe 628445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 629445251d0SJens Axboe struct request *next) 630445251d0SJens Axboe { 631445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 632445251d0SJens Axboe 633445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 634445251d0SJens Axboe goto no_merge; 635445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 636445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 637445251d0SJens Axboe goto no_merge; 638445251d0SJens Axboe 639445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 640445251d0SJens Axboe return true; 641445251d0SJens Axboe no_merge: 642445251d0SJens Axboe req_set_nomerge(q, req); 643445251d0SJens Axboe return false; 644445251d0SJens Axboe } 645445251d0SJens Axboe 646d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 647d6d48196SJens Axboe struct request *next) 648d6d48196SJens Axboe { 649d6d48196SJens Axboe int total_phys_segments; 65086771427SFUJITA Tomonori unsigned int seg_size = 65186771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 652d6d48196SJens Axboe 653d6d48196SJens Axboe /* 654d6d48196SJens Axboe * First check if the either of the requests are re-queued 655d6d48196SJens Axboe * requests. Can't merge them if they are. 656d6d48196SJens Axboe */ 657e7e24500SJens Axboe if (req_no_special_merge(req) || req_no_special_merge(next)) 658d6d48196SJens Axboe return 0; 659d6d48196SJens Axboe 6605e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 661854fbb9cSKeith Busch return 0; 662854fbb9cSKeith Busch 663d6d48196SJens Axboe /* 664d6d48196SJens Axboe * Will it become too large? 665d6d48196SJens Axboe */ 666f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 66717007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 668d6d48196SJens Axboe return 0; 669d6d48196SJens Axboe 670d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 67186771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 67286771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 67386771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 67486771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 67586771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 676d6d48196SJens Axboe total_phys_segments--; 67786771427SFUJITA Tomonori } 678d6d48196SJens Axboe 6798a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 680d6d48196SJens Axboe return 0; 681d6d48196SJens Axboe 6824eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 68313f05c8dSMartin K. Petersen return 0; 68413f05c8dSMartin K. Petersen 685d6d48196SJens Axboe /* Merge is OK... */ 686d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 687d6d48196SJens Axboe return 1; 688d6d48196SJens Axboe } 689d6d48196SJens Axboe 69080a761fdSTejun Heo /** 69180a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 69280a761fdSTejun Heo * @rq: request to mark as mixed merge 69380a761fdSTejun Heo * 69480a761fdSTejun Heo * Description: 69580a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 69680a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 69780a761fdSTejun Heo * merged. 69880a761fdSTejun Heo */ 69980a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 70080a761fdSTejun Heo { 70180a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 70280a761fdSTejun Heo struct bio *bio; 70380a761fdSTejun Heo 704e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 70580a761fdSTejun Heo return; 70680a761fdSTejun Heo 70780a761fdSTejun Heo /* 70880a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 70980a761fdSTejun Heo * contained bios. It will just track those of the first one. 71080a761fdSTejun Heo * Distributes the attributs to each bio. 71180a761fdSTejun Heo */ 71280a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 7131eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 7141eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 7151eff9d32SJens Axboe bio->bi_opf |= ff; 71680a761fdSTejun Heo } 717e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 71880a761fdSTejun Heo } 71980a761fdSTejun Heo 72026308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 72126308eabSJerome Marchand { 72226308eabSJerome Marchand if (blk_do_io_stat(req)) { 72326308eabSJerome Marchand struct hd_struct *part; 72426308eabSJerome Marchand int cpu; 72526308eabSJerome Marchand 72626308eabSJerome Marchand cpu = part_stat_lock(); 72709e099d4SJerome Marchand part = req->part; 72826308eabSJerome Marchand 729d62e26b3SJens Axboe part_round_stats(req->q, cpu, part); 730d62e26b3SJens Axboe part_dec_in_flight(req->q, part, rq_data_dir(req)); 73126308eabSJerome Marchand 7326c23a968SJens Axboe hd_struct_put(part); 73326308eabSJerome Marchand part_stat_unlock(); 73426308eabSJerome Marchand } 73526308eabSJerome Marchand } 73626308eabSJerome Marchand 737d6d48196SJens Axboe /* 738b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 739b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 740d6d48196SJens Axboe */ 741b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 742b973cb7eSJens Axboe struct request *req, struct request *next) 743d6d48196SJens Axboe { 7442fff8a92SBart Van Assche if (!q->mq_ops) 7452fff8a92SBart Van Assche lockdep_assert_held(q->queue_lock); 7462fff8a92SBart Van Assche 747d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 748b973cb7eSJens Axboe return NULL; 749d6d48196SJens Axboe 750288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 751b973cb7eSJens Axboe return NULL; 752f31dc1cdSMartin K. Petersen 753d6d48196SJens Axboe /* 754d6d48196SJens Axboe * not contiguous 755d6d48196SJens Axboe */ 75683096ebfSTejun Heo if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) 757b973cb7eSJens Axboe return NULL; 758d6d48196SJens Axboe 759d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 760d6d48196SJens Axboe || req->rq_disk != next->rq_disk 761e7e24500SJens Axboe || req_no_special_merge(next)) 762b973cb7eSJens Axboe return NULL; 763d6d48196SJens Axboe 7648fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 7654363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 766b973cb7eSJens Axboe return NULL; 7674363ac7cSMartin K. Petersen 768d6d48196SJens Axboe /* 769cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 770cb6934f8SJens Axboe * non-hint IO. 771cb6934f8SJens Axboe */ 772cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 773cb6934f8SJens Axboe return NULL; 774cb6934f8SJens Axboe 775cb6934f8SJens Axboe /* 776d6d48196SJens Axboe * If we are allowed to merge, then append bio list 777d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 778d6d48196SJens Axboe * will have updated segment counts, update sector 779445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 780445251d0SJens Axboe * have separate settings. 781d6d48196SJens Axboe */ 782445251d0SJens Axboe if (req_op(req) == REQ_OP_DISCARD) { 783445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 784445251d0SJens Axboe return NULL; 785445251d0SJens Axboe } else if (!ll_merge_requests_fn(q, req, next)) 786b973cb7eSJens Axboe return NULL; 787d6d48196SJens Axboe 788d6d48196SJens Axboe /* 78980a761fdSTejun Heo * If failfast settings disagree or any of the two is already 79080a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 79180a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 79280a761fdSTejun Heo * set properly. 79380a761fdSTejun Heo */ 794e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 79580a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 79680a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 79780a761fdSTejun Heo blk_rq_set_mixed_merge(req); 79880a761fdSTejun Heo blk_rq_set_mixed_merge(next); 79980a761fdSTejun Heo } 80080a761fdSTejun Heo 80180a761fdSTejun Heo /* 802522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 803522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 804522a7775SOmar Sandoval * current request for accounting purposes. 805d6d48196SJens Axboe */ 806522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 807522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 808d6d48196SJens Axboe 809d6d48196SJens Axboe req->biotail->bi_next = next->bio; 810d6d48196SJens Axboe req->biotail = next->biotail; 811d6d48196SJens Axboe 812a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 813d6d48196SJens Axboe 814445251d0SJens Axboe if (req_op(req) != REQ_OP_DISCARD) 815d6d48196SJens Axboe elv_merge_requests(q, req, next); 816d6d48196SJens Axboe 81742dad764SJerome Marchand /* 81842dad764SJerome Marchand * 'next' is going away, so update stats accordingly 81942dad764SJerome Marchand */ 82042dad764SJerome Marchand blk_account_io_merge(next); 821d6d48196SJens Axboe 822d6d48196SJens Axboe req->ioprio = ioprio_best(req->ioprio, next->ioprio); 823ab780f1eSJens Axboe if (blk_rq_cpu_valid(next)) 824ab780f1eSJens Axboe req->cpu = next->cpu; 825d6d48196SJens Axboe 826e4d750c9SJens Axboe /* 827e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 828e4d750c9SJens Axboe * the caller to free 829e4d750c9SJens Axboe */ 8301cd96c24SBoaz Harrosh next->bio = NULL; 831b973cb7eSJens Axboe return next; 832d6d48196SJens Axboe } 833d6d48196SJens Axboe 834b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq) 835d6d48196SJens Axboe { 836d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 837d6d48196SJens Axboe 838d6d48196SJens Axboe if (next) 839d6d48196SJens Axboe return attempt_merge(q, rq, next); 840d6d48196SJens Axboe 841b973cb7eSJens Axboe return NULL; 842d6d48196SJens Axboe } 843d6d48196SJens Axboe 844b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq) 845d6d48196SJens Axboe { 846d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 847d6d48196SJens Axboe 848d6d48196SJens Axboe if (prev) 849d6d48196SJens Axboe return attempt_merge(q, prev, rq); 850d6d48196SJens Axboe 851b973cb7eSJens Axboe return NULL; 852d6d48196SJens Axboe } 8535e84ea3aSJens Axboe 8545e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 8555e84ea3aSJens Axboe struct request *next) 8565e84ea3aSJens Axboe { 85772ef799bSTahsin Erdogan struct elevator_queue *e = q->elevator; 858e4d750c9SJens Axboe struct request *free; 85972ef799bSTahsin Erdogan 860bd166ef1SJens Axboe if (!e->uses_mq && e->type->ops.sq.elevator_allow_rq_merge_fn) 861c51ca6cfSJens Axboe if (!e->type->ops.sq.elevator_allow_rq_merge_fn(q, rq, next)) 86272ef799bSTahsin Erdogan return 0; 86372ef799bSTahsin Erdogan 864e4d750c9SJens Axboe free = attempt_merge(q, rq, next); 865e4d750c9SJens Axboe if (free) { 866e4d750c9SJens Axboe __blk_put_request(q, free); 867e4d750c9SJens Axboe return 1; 868e4d750c9SJens Axboe } 869e4d750c9SJens Axboe 870e4d750c9SJens Axboe return 0; 8715e84ea3aSJens Axboe } 872050c8ea8STejun Heo 873050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 874050c8ea8STejun Heo { 875e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 876050c8ea8STejun Heo return false; 877050c8ea8STejun Heo 878288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 879f31dc1cdSMartin K. Petersen return false; 880f31dc1cdSMartin K. Petersen 881050c8ea8STejun Heo /* different data direction or already started, don't merge */ 882050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 883050c8ea8STejun Heo return false; 884050c8ea8STejun Heo 885050c8ea8STejun Heo /* must be same device and not a special request */ 88674d46992SChristoph Hellwig if (rq->rq_disk != bio->bi_disk || req_no_special_merge(rq)) 887050c8ea8STejun Heo return false; 888050c8ea8STejun Heo 889050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 8904eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 891050c8ea8STejun Heo return false; 892050c8ea8STejun Heo 8934363ac7cSMartin K. Petersen /* must be using the same buffer */ 8948fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 8954363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 8964363ac7cSMartin K. Petersen return false; 8974363ac7cSMartin K. Petersen 898cb6934f8SJens Axboe /* 899cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 900cb6934f8SJens Axboe * non-hint IO. 901cb6934f8SJens Axboe */ 902cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 903cb6934f8SJens Axboe return false; 904cb6934f8SJens Axboe 905050c8ea8STejun Heo return true; 906050c8ea8STejun Heo } 907050c8ea8STejun Heo 90834fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 909050c8ea8STejun Heo { 9101e739730SChristoph Hellwig if (req_op(rq) == REQ_OP_DISCARD && 9111e739730SChristoph Hellwig queue_max_discard_segments(rq->q) > 1) 9121e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 9131e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 914050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 9154f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 916050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 917050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 918050c8ea8STejun Heo } 919