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 15e9907009SChristoph Hellwig /* 16e9907009SChristoph Hellwig * Check if the two bvecs from two bios can be merged to one segment. If yes, 17e9907009SChristoph Hellwig * no need to check gap between the two bios since the 1st bio and the 1st bvec 18e9907009SChristoph Hellwig * in the 2nd bio can be handled in one segment. 19e9907009SChristoph Hellwig */ 20e9907009SChristoph Hellwig static inline bool bios_segs_mergeable(struct request_queue *q, 21e9907009SChristoph Hellwig struct bio *prev, struct bio_vec *prev_last_bv, 22e9907009SChristoph Hellwig struct bio_vec *next_first_bv) 23e9907009SChristoph Hellwig { 243dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, prev_last_bv, next_first_bv)) 25e9907009SChristoph Hellwig return false; 26e9907009SChristoph Hellwig if (prev->bi_seg_back_size + next_first_bv->bv_len > 27e9907009SChristoph Hellwig queue_max_segment_size(q)) 28e9907009SChristoph Hellwig return false; 29e9907009SChristoph Hellwig return true; 30e9907009SChristoph Hellwig } 31e9907009SChristoph Hellwig 32e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q, 33e9907009SChristoph Hellwig struct request *prev_rq, struct bio *prev, struct bio *next) 34e9907009SChristoph Hellwig { 35e9907009SChristoph Hellwig struct bio_vec pb, nb; 36e9907009SChristoph Hellwig 37e9907009SChristoph Hellwig if (!bio_has_data(prev) || !queue_virt_boundary(q)) 38e9907009SChristoph Hellwig return false; 39e9907009SChristoph Hellwig 40e9907009SChristoph Hellwig /* 41e9907009SChristoph Hellwig * Don't merge if the 1st bio starts with non-zero offset, otherwise it 42e9907009SChristoph Hellwig * is quite difficult to respect the sg gap limit. We work hard to 43e9907009SChristoph Hellwig * merge a huge number of small single bios in case of mkfs. 44e9907009SChristoph Hellwig */ 45e9907009SChristoph Hellwig if (prev_rq) 46e9907009SChristoph Hellwig bio_get_first_bvec(prev_rq->bio, &pb); 47e9907009SChristoph Hellwig else 48e9907009SChristoph Hellwig bio_get_first_bvec(prev, &pb); 49df376b2eSJohannes Thumshirn if (pb.bv_offset & queue_virt_boundary(q)) 50e9907009SChristoph Hellwig return true; 51e9907009SChristoph Hellwig 52e9907009SChristoph Hellwig /* 53e9907009SChristoph Hellwig * We don't need to worry about the situation that the merged segment 54e9907009SChristoph Hellwig * ends in unaligned virt boundary: 55e9907009SChristoph Hellwig * 56e9907009SChristoph Hellwig * - if 'pb' ends aligned, the merged segment ends aligned 57e9907009SChristoph Hellwig * - if 'pb' ends unaligned, the next bio must include 58e9907009SChristoph Hellwig * one single bvec of 'nb', otherwise the 'nb' can't 59e9907009SChristoph Hellwig * merge with 'pb' 60e9907009SChristoph Hellwig */ 61e9907009SChristoph Hellwig bio_get_last_bvec(prev, &pb); 62e9907009SChristoph Hellwig bio_get_first_bvec(next, &nb); 63e9907009SChristoph Hellwig if (bios_segs_mergeable(q, prev, &pb, &nb)) 64e9907009SChristoph Hellwig return false; 65e9907009SChristoph Hellwig return __bvec_gap_to_prev(q, &pb, nb.bv_offset); 66e9907009SChristoph Hellwig } 67e9907009SChristoph Hellwig 68e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio) 69e9907009SChristoph Hellwig { 70e9907009SChristoph Hellwig return bio_will_gap(req->q, req, req->biotail, bio); 71e9907009SChristoph Hellwig } 72e9907009SChristoph Hellwig 73e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio) 74e9907009SChristoph Hellwig { 75e9907009SChristoph Hellwig return bio_will_gap(req->q, NULL, bio, req->bio); 76e9907009SChristoph Hellwig } 77e9907009SChristoph Hellwig 7854efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 7954efd50bSKent Overstreet struct bio *bio, 80bdced438SMing Lei struct bio_set *bs, 81bdced438SMing Lei unsigned *nsegs) 8254efd50bSKent Overstreet { 8354efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 8454efd50bSKent Overstreet int alignment; 8554efd50bSKent Overstreet sector_t tmp; 8654efd50bSKent Overstreet unsigned split_sectors; 8754efd50bSKent Overstreet 88bdced438SMing Lei *nsegs = 1; 89bdced438SMing Lei 9054efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 9154efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 9254efd50bSKent Overstreet 931adfc5e4SMing Lei max_discard_sectors = min(q->limits.max_discard_sectors, 941adfc5e4SMing Lei bio_allowed_max_sectors(q)); 9554efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 9654efd50bSKent Overstreet 9754efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 9854efd50bSKent Overstreet /* XXX: warn */ 9954efd50bSKent Overstreet return NULL; 10054efd50bSKent Overstreet } 10154efd50bSKent Overstreet 10254efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 10354efd50bSKent Overstreet return NULL; 10454efd50bSKent Overstreet 10554efd50bSKent Overstreet split_sectors = max_discard_sectors; 10654efd50bSKent Overstreet 10754efd50bSKent Overstreet /* 10854efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 10954efd50bSKent Overstreet * the previous aligned sector. 11054efd50bSKent Overstreet */ 11154efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 11254efd50bSKent Overstreet 11354efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 11454efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 11554efd50bSKent Overstreet 11654efd50bSKent Overstreet if (split_sectors > tmp) 11754efd50bSKent Overstreet split_sectors -= tmp; 11854efd50bSKent Overstreet 11954efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 12054efd50bSKent Overstreet } 12154efd50bSKent Overstreet 122885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, 123885fa13fSChristoph Hellwig struct bio *bio, struct bio_set *bs, unsigned *nsegs) 124885fa13fSChristoph Hellwig { 125885fa13fSChristoph Hellwig *nsegs = 1; 126885fa13fSChristoph Hellwig 127885fa13fSChristoph Hellwig if (!q->limits.max_write_zeroes_sectors) 128885fa13fSChristoph Hellwig return NULL; 129885fa13fSChristoph Hellwig 130885fa13fSChristoph Hellwig if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) 131885fa13fSChristoph Hellwig return NULL; 132885fa13fSChristoph Hellwig 133885fa13fSChristoph Hellwig return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); 134885fa13fSChristoph Hellwig } 135885fa13fSChristoph Hellwig 13654efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 13754efd50bSKent Overstreet struct bio *bio, 138bdced438SMing Lei struct bio_set *bs, 139bdced438SMing Lei unsigned *nsegs) 14054efd50bSKent Overstreet { 141bdced438SMing Lei *nsegs = 1; 142bdced438SMing Lei 14354efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 14454efd50bSKent Overstreet return NULL; 14554efd50bSKent Overstreet 14654efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 14754efd50bSKent Overstreet return NULL; 14854efd50bSKent Overstreet 14954efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 15054efd50bSKent Overstreet } 15154efd50bSKent Overstreet 152d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 153d0e5fbb0SMing Lei struct bio *bio) 154d0e5fbb0SMing Lei { 155d0e5fbb0SMing Lei unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector); 156d0e5fbb0SMing Lei unsigned mask = queue_logical_block_size(q) - 1; 157d0e5fbb0SMing Lei 158d0e5fbb0SMing Lei /* aligned to logical block size */ 159d0e5fbb0SMing Lei sectors &= ~(mask >> 9); 160d0e5fbb0SMing Lei 161d0e5fbb0SMing Lei return sectors; 162d0e5fbb0SMing Lei } 163d0e5fbb0SMing Lei 16454efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 16554efd50bSKent Overstreet struct bio *bio, 166bdced438SMing Lei struct bio_set *bs, 167bdced438SMing Lei unsigned *segs) 16854efd50bSKent Overstreet { 1695014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 17054efd50bSKent Overstreet struct bvec_iter iter; 1718ae12666SKent Overstreet unsigned seg_size = 0, nsegs = 0, sectors = 0; 17202e70742SMing Lei unsigned front_seg_size = bio->bi_seg_front_size; 17302e70742SMing Lei bool do_split = true; 17402e70742SMing Lei struct bio *new = NULL; 175d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 17654efd50bSKent Overstreet 17754efd50bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 17854efd50bSKent Overstreet /* 17954efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 18054efd50bSKent Overstreet * offset would create a gap, disallow it. 18154efd50bSKent Overstreet */ 1825014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 18354efd50bSKent Overstreet goto split; 18454efd50bSKent Overstreet 185d0e5fbb0SMing Lei if (sectors + (bv.bv_len >> 9) > max_sectors) { 186e36f6204SKeith Busch /* 187e36f6204SKeith Busch * Consider this a new segment if we're splitting in 188e36f6204SKeith Busch * the middle of this vector. 189e36f6204SKeith Busch */ 190e36f6204SKeith Busch if (nsegs < queue_max_segments(q) && 191d0e5fbb0SMing Lei sectors < max_sectors) { 192e36f6204SKeith Busch nsegs++; 193d0e5fbb0SMing Lei sectors = max_sectors; 194e36f6204SKeith Busch } 195e36f6204SKeith Busch goto split; 196e36f6204SKeith Busch } 197e36f6204SKeith Busch 1985014c311SJens Axboe if (bvprvp && blk_queue_cluster(q)) { 199b4b6cb61SMing Lei if (seg_size + bv.bv_len > queue_max_segment_size(q)) 200b4b6cb61SMing Lei goto new_segment; 2013dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprvp, &bv)) 20254efd50bSKent Overstreet goto new_segment; 20354efd50bSKent Overstreet 20454efd50bSKent Overstreet seg_size += bv.bv_len; 20554efd50bSKent Overstreet bvprv = bv; 206578270bfSMing Lei bvprvp = &bvprv; 20752cc6eeaSMing Lei sectors += bv.bv_len >> 9; 208a88d32afSMing Lei 20954efd50bSKent Overstreet continue; 21054efd50bSKent Overstreet } 21154efd50bSKent Overstreet new_segment: 21254efd50bSKent Overstreet if (nsegs == queue_max_segments(q)) 21354efd50bSKent Overstreet goto split; 21454efd50bSKent Overstreet 2156a501bf0SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 2166a501bf0SMing Lei front_seg_size = seg_size; 2176a501bf0SMing Lei 21854efd50bSKent Overstreet nsegs++; 21954efd50bSKent Overstreet bvprv = bv; 220578270bfSMing Lei bvprvp = &bvprv; 22154efd50bSKent Overstreet seg_size = bv.bv_len; 22252cc6eeaSMing Lei sectors += bv.bv_len >> 9; 22302e70742SMing Lei 22454efd50bSKent Overstreet } 22554efd50bSKent Overstreet 22602e70742SMing Lei do_split = false; 22754efd50bSKent Overstreet split: 228bdced438SMing Lei *segs = nsegs; 22902e70742SMing Lei 23002e70742SMing Lei if (do_split) { 23102e70742SMing Lei new = bio_split(bio, sectors, GFP_NOIO, bs); 23202e70742SMing Lei if (new) 23302e70742SMing Lei bio = new; 23402e70742SMing Lei } 23502e70742SMing Lei 2366a501bf0SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 2376a501bf0SMing Lei front_seg_size = seg_size; 23802e70742SMing Lei bio->bi_seg_front_size = front_seg_size; 23902e70742SMing Lei if (seg_size > bio->bi_seg_back_size) 24002e70742SMing Lei bio->bi_seg_back_size = seg_size; 24102e70742SMing Lei 24202e70742SMing Lei return do_split ? new : NULL; 24354efd50bSKent Overstreet } 24454efd50bSKent Overstreet 245af67c31fSNeilBrown void blk_queue_split(struct request_queue *q, struct bio **bio) 24654efd50bSKent Overstreet { 247bdced438SMing Lei struct bio *split, *res; 248bdced438SMing Lei unsigned nsegs; 24954efd50bSKent Overstreet 2507afafc8aSAdrian Hunter switch (bio_op(*bio)) { 2517afafc8aSAdrian Hunter case REQ_OP_DISCARD: 2527afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 253338aa96dSKent Overstreet split = blk_bio_discard_split(q, *bio, &q->bio_split, &nsegs); 2547afafc8aSAdrian Hunter break; 255a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 256338aa96dSKent Overstreet split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, &nsegs); 257a6f0788eSChaitanya Kulkarni break; 2587afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 259338aa96dSKent Overstreet split = blk_bio_write_same_split(q, *bio, &q->bio_split, &nsegs); 2607afafc8aSAdrian Hunter break; 2617afafc8aSAdrian Hunter default: 262338aa96dSKent Overstreet split = blk_bio_segment_split(q, *bio, &q->bio_split, &nsegs); 2637afafc8aSAdrian Hunter break; 2647afafc8aSAdrian Hunter } 265bdced438SMing Lei 266bdced438SMing Lei /* physical segments can be figured out during splitting */ 267bdced438SMing Lei res = split ? split : *bio; 268bdced438SMing Lei res->bi_phys_segments = nsegs; 269bdced438SMing Lei bio_set_flag(res, BIO_SEG_VALID); 27054efd50bSKent Overstreet 27154efd50bSKent Overstreet if (split) { 2726ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 2731eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 2746ac45aebSMing Lei 275cd4a4ae4SJens Axboe /* 276cd4a4ae4SJens Axboe * Since we're recursing into make_request here, ensure 277cd4a4ae4SJens Axboe * that we mark this bio as already having entered the queue. 278cd4a4ae4SJens Axboe * If not, and the queue is going away, we can get stuck 279cd4a4ae4SJens Axboe * forever on waiting for the queue reference to drop. But 280cd4a4ae4SJens Axboe * that will never happen, as we're already holding a 281cd4a4ae4SJens Axboe * reference to it. 282cd4a4ae4SJens Axboe */ 283cd4a4ae4SJens Axboe bio_set_flag(*bio, BIO_QUEUE_ENTERED); 284cd4a4ae4SJens Axboe 28554efd50bSKent Overstreet bio_chain(split, *bio); 286cda22646SMike Krinkin trace_block_split(q, split, (*bio)->bi_iter.bi_sector); 28754efd50bSKent Overstreet generic_make_request(*bio); 28854efd50bSKent Overstreet *bio = split; 28954efd50bSKent Overstreet } 29054efd50bSKent Overstreet } 29154efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 29254efd50bSKent Overstreet 2931e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 29407388549SMing Lei struct bio *bio, 29507388549SMing Lei bool no_sg_merge) 296d6d48196SJens Axboe { 2977988613bSKent Overstreet struct bio_vec bv, bvprv = { NULL }; 29854efd50bSKent Overstreet int cluster, prev = 0; 2991e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 30059247eaeSJens Axboe struct bio *fbio, *bbio; 3017988613bSKent Overstreet struct bvec_iter iter; 302d6d48196SJens Axboe 3031e428079SJens Axboe if (!bio) 3041e428079SJens Axboe return 0; 305d6d48196SJens Axboe 306a6f0788eSChaitanya Kulkarni switch (bio_op(bio)) { 307a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 308a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 309a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 310f9d03f96SChristoph Hellwig return 0; 311f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 3125cb8850cSKent Overstreet return 1; 313a6f0788eSChaitanya Kulkarni } 3145cb8850cSKent Overstreet 3151e428079SJens Axboe fbio = bio; 316e692cb66SMartin K. Petersen cluster = blk_queue_cluster(q); 3175df97b91SMikulas Patocka seg_size = 0; 3182c8919deSAndi Kleen nr_phys_segs = 0; 3191e428079SJens Axboe for_each_bio(bio) { 3207988613bSKent Overstreet bio_for_each_segment(bv, bio, iter) { 321d6d48196SJens Axboe /* 32205f1dd53SJens Axboe * If SG merging is disabled, each bio vector is 32305f1dd53SJens Axboe * a segment 32405f1dd53SJens Axboe */ 32505f1dd53SJens Axboe if (no_sg_merge) 32605f1dd53SJens Axboe goto new_segment; 32705f1dd53SJens Axboe 32854efd50bSKent Overstreet if (prev && cluster) { 3297988613bSKent Overstreet if (seg_size + bv.bv_len 330ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 331d6d48196SJens Axboe goto new_segment; 3323dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, &bvprv, &bv)) 333d6d48196SJens Axboe goto new_segment; 334d6d48196SJens Axboe 3357988613bSKent Overstreet seg_size += bv.bv_len; 336d6d48196SJens Axboe bvprv = bv; 337d6d48196SJens Axboe continue; 338d6d48196SJens Axboe } 339d6d48196SJens Axboe new_segment: 3401e428079SJens Axboe if (nr_phys_segs == 1 && seg_size > 3411e428079SJens Axboe fbio->bi_seg_front_size) 3421e428079SJens Axboe fbio->bi_seg_front_size = seg_size; 34386771427SFUJITA Tomonori 344d6d48196SJens Axboe nr_phys_segs++; 345d6d48196SJens Axboe bvprv = bv; 34654efd50bSKent Overstreet prev = 1; 3477988613bSKent Overstreet seg_size = bv.bv_len; 348d6d48196SJens Axboe } 34959247eaeSJens Axboe bbio = bio; 3501e428079SJens Axboe } 351d6d48196SJens Axboe 35259247eaeSJens Axboe if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size) 35359247eaeSJens Axboe fbio->bi_seg_front_size = seg_size; 35459247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 35559247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 3561e428079SJens Axboe 3571e428079SJens Axboe return nr_phys_segs; 3581e428079SJens Axboe } 3591e428079SJens Axboe 3601e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 3611e428079SJens Axboe { 36207388549SMing Lei bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE, 36307388549SMing Lei &rq->q->queue_flags); 36407388549SMing Lei 36507388549SMing Lei rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio, 36607388549SMing Lei no_sg_merge); 367d6d48196SJens Axboe } 368d6d48196SJens Axboe 369d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 370d6d48196SJens Axboe { 3717f60dcaaSMing Lei unsigned short seg_cnt; 372764f612cSMing Lei 3737f60dcaaSMing Lei /* estimate segment number by bi_vcnt for non-cloned bio */ 3747f60dcaaSMing Lei if (bio_flagged(bio, BIO_CLONED)) 3757f60dcaaSMing Lei seg_cnt = bio_segments(bio); 3767f60dcaaSMing Lei else 3777f60dcaaSMing Lei seg_cnt = bio->bi_vcnt; 3787f60dcaaSMing Lei 3797f60dcaaSMing Lei if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) && 3807f60dcaaSMing Lei (seg_cnt < queue_max_segments(q))) 3817f60dcaaSMing Lei bio->bi_phys_segments = seg_cnt; 38205f1dd53SJens Axboe else { 383d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 3841e428079SJens Axboe 385d6d48196SJens Axboe bio->bi_next = NULL; 3867f60dcaaSMing Lei bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false); 387d6d48196SJens Axboe bio->bi_next = nxt; 38805f1dd53SJens Axboe } 38905f1dd53SJens Axboe 390b7c44ed9SJens Axboe bio_set_flag(bio, BIO_SEG_VALID); 391d6d48196SJens Axboe } 392d6d48196SJens Axboe EXPORT_SYMBOL(blk_recount_segments); 393d6d48196SJens Axboe 394d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 395d6d48196SJens Axboe struct bio *nxt) 396d6d48196SJens Axboe { 3972b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 398f619d254SKent Overstreet 399e692cb66SMartin K. Petersen if (!blk_queue_cluster(q)) 400d6d48196SJens Axboe return 0; 401d6d48196SJens Axboe 40286771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 403ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 404d6d48196SJens Axboe return 0; 405d6d48196SJens Axboe 406e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 407e17fc0a1SDavid Woodhouse return 1; 408e17fc0a1SDavid Woodhouse 409e827091cSMing Lei bio_get_last_bvec(bio, &end_bv); 410e827091cSMing Lei bio_get_first_bvec(nxt, &nxt_bv); 411f619d254SKent Overstreet 4123dccdae5SChristoph Hellwig return biovec_phys_mergeable(q, &end_bv, &nxt_bv); 413d6d48196SJens Axboe } 414d6d48196SJens Axboe 4157988613bSKent Overstreet static inline void 416963ab9e5SAsias He __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, 4177988613bSKent Overstreet struct scatterlist *sglist, struct bio_vec *bvprv, 418963ab9e5SAsias He struct scatterlist **sg, int *nsegs, int *cluster) 419963ab9e5SAsias He { 420963ab9e5SAsias He 421963ab9e5SAsias He int nbytes = bvec->bv_len; 422963ab9e5SAsias He 4237988613bSKent Overstreet if (*sg && *cluster) { 424b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 425b4b6cb61SMing Lei goto new_segment; 4263dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec)) 427963ab9e5SAsias He goto new_segment; 428963ab9e5SAsias He 429963ab9e5SAsias He (*sg)->length += nbytes; 430963ab9e5SAsias He } else { 431963ab9e5SAsias He new_segment: 432963ab9e5SAsias He if (!*sg) 433963ab9e5SAsias He *sg = sglist; 434963ab9e5SAsias He else { 435963ab9e5SAsias He /* 436963ab9e5SAsias He * If the driver previously mapped a shorter 437963ab9e5SAsias He * list, we could see a termination bit 438963ab9e5SAsias He * prematurely unless it fully inits the sg 439963ab9e5SAsias He * table on each mapping. We KNOW that there 440963ab9e5SAsias He * must be more entries here or the driver 441963ab9e5SAsias He * would be buggy, so force clear the 442963ab9e5SAsias He * termination bit to avoid doing a full 443963ab9e5SAsias He * sg_init_table() in drivers for each command. 444963ab9e5SAsias He */ 445c8164d89SPaolo Bonzini sg_unmark_end(*sg); 446963ab9e5SAsias He *sg = sg_next(*sg); 447963ab9e5SAsias He } 448963ab9e5SAsias He 449963ab9e5SAsias He sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); 450963ab9e5SAsias He (*nsegs)++; 451963ab9e5SAsias He } 4527988613bSKent Overstreet *bvprv = *bvec; 453963ab9e5SAsias He } 454963ab9e5SAsias He 455f9d03f96SChristoph Hellwig static inline int __blk_bvec_map_sg(struct request_queue *q, struct bio_vec bv, 456f9d03f96SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **sg) 457f9d03f96SChristoph Hellwig { 458f9d03f96SChristoph Hellwig *sg = sglist; 459f9d03f96SChristoph Hellwig sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 460f9d03f96SChristoph Hellwig return 1; 461f9d03f96SChristoph Hellwig } 462f9d03f96SChristoph Hellwig 4635cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 4645cb8850cSKent Overstreet struct scatterlist *sglist, 4655cb8850cSKent Overstreet struct scatterlist **sg) 4665cb8850cSKent Overstreet { 4675cb8850cSKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 4685cb8850cSKent Overstreet struct bvec_iter iter; 469f9d03f96SChristoph Hellwig int cluster = blk_queue_cluster(q), nsegs = 0; 4705cb8850cSKent Overstreet 4715cb8850cSKent Overstreet for_each_bio(bio) 4725cb8850cSKent Overstreet bio_for_each_segment(bvec, bio, iter) 4735cb8850cSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, 4745cb8850cSKent Overstreet &nsegs, &cluster); 4755cb8850cSKent Overstreet 4765cb8850cSKent Overstreet return nsegs; 4775cb8850cSKent Overstreet } 4785cb8850cSKent Overstreet 479d6d48196SJens Axboe /* 480d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 481d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 482d6d48196SJens Axboe */ 483d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 484d6d48196SJens Axboe struct scatterlist *sglist) 485d6d48196SJens Axboe { 4865cb8850cSKent Overstreet struct scatterlist *sg = NULL; 4875cb8850cSKent Overstreet int nsegs = 0; 488d6d48196SJens Axboe 489f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 490f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, rq->special_vec, sglist, &sg); 491f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 492f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, bio_iovec(rq->bio), sglist, &sg); 493f9d03f96SChristoph Hellwig else if (rq->bio) 4945cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 495f18573abSFUJITA Tomonori 496e8064021SChristoph Hellwig if (unlikely(rq->rq_flags & RQF_COPY_USER) && 4972e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 4982e46e8b2STejun Heo unsigned int pad_len = 4992e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 500f18573abSFUJITA Tomonori 501f18573abSFUJITA Tomonori sg->length += pad_len; 502f18573abSFUJITA Tomonori rq->extra_len += pad_len; 503f18573abSFUJITA Tomonori } 504f18573abSFUJITA Tomonori 5052fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 506a8ebb056SMike Christie if (op_is_write(req_op(rq))) 507db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 508db0a2e00STejun Heo 509da81ed16SDan Williams sg_unmark_end(sg); 510d6d48196SJens Axboe sg = sg_next(sg); 511d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 512d6d48196SJens Axboe q->dma_drain_size, 513d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 514d6d48196SJens Axboe (PAGE_SIZE - 1)); 515d6d48196SJens Axboe nsegs++; 5167a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 517d6d48196SJens Axboe } 518d6d48196SJens Axboe 519d6d48196SJens Axboe if (sg) 520d6d48196SJens Axboe sg_mark_end(sg); 521d6d48196SJens Axboe 52212e57f59SMing Lei /* 52312e57f59SMing Lei * Something must have been wrong if the figured number of 52412e57f59SMing Lei * segment is bigger than number of req's physical segments 52512e57f59SMing Lei */ 526f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 52712e57f59SMing Lei 528d6d48196SJens Axboe return nsegs; 529d6d48196SJens Axboe } 530d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 531d6d48196SJens Axboe 532d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 533d6d48196SJens Axboe struct request *req, 534d6d48196SJens Axboe struct bio *bio) 535d6d48196SJens Axboe { 536d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 537d6d48196SJens Axboe 53813f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 53913f05c8dSMartin K. Petersen goto no_merge; 54013f05c8dSMartin K. Petersen 5414eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(q, req, bio) == false) 54213f05c8dSMartin K. Petersen goto no_merge; 543d6d48196SJens Axboe 544d6d48196SJens Axboe /* 545d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 546d6d48196SJens Axboe * counters. 547d6d48196SJens Axboe */ 548d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 549d6d48196SJens Axboe return 1; 55013f05c8dSMartin K. Petersen 55113f05c8dSMartin K. Petersen no_merge: 552e0c72300SRitesh Harjani req_set_nomerge(q, req); 55313f05c8dSMartin K. Petersen return 0; 554d6d48196SJens Axboe } 555d6d48196SJens Axboe 556d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 557d6d48196SJens Axboe struct bio *bio) 558d6d48196SJens Axboe { 5595e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 5605e7c4274SJens Axboe return 0; 5617f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5627f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 5637f39add3SSagi Grimberg return 0; 564f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 56517007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 566e0c72300SRitesh Harjani req_set_nomerge(q, req); 567d6d48196SJens Axboe return 0; 568d6d48196SJens Axboe } 5692cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 570d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 5712cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 572d6d48196SJens Axboe blk_recount_segments(q, bio); 573d6d48196SJens Axboe 574d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 575d6d48196SJens Axboe } 576d6d48196SJens Axboe 577d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 578d6d48196SJens Axboe struct bio *bio) 579d6d48196SJens Axboe { 5805e7c4274SJens Axboe 5815e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 5825e7c4274SJens Axboe return 0; 5837f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5847f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 5857f39add3SSagi Grimberg return 0; 586f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 58717007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 588e0c72300SRitesh Harjani req_set_nomerge(q, req); 589d6d48196SJens Axboe return 0; 590d6d48196SJens Axboe } 5912cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 592d6d48196SJens Axboe blk_recount_segments(q, bio); 5932cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 594d6d48196SJens Axboe blk_recount_segments(q, req->bio); 595d6d48196SJens Axboe 596d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 597d6d48196SJens Axboe } 598d6d48196SJens Axboe 599445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 600445251d0SJens Axboe struct request *next) 601445251d0SJens Axboe { 602445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 603445251d0SJens Axboe 604445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 605445251d0SJens Axboe goto no_merge; 606445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 607445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 608445251d0SJens Axboe goto no_merge; 609445251d0SJens Axboe 610445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 611445251d0SJens Axboe return true; 612445251d0SJens Axboe no_merge: 613445251d0SJens Axboe req_set_nomerge(q, req); 614445251d0SJens Axboe return false; 615445251d0SJens Axboe } 616445251d0SJens Axboe 617d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 618d6d48196SJens Axboe struct request *next) 619d6d48196SJens Axboe { 620d6d48196SJens Axboe int total_phys_segments; 62186771427SFUJITA Tomonori unsigned int seg_size = 62286771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 623d6d48196SJens Axboe 6245e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 625854fbb9cSKeith Busch return 0; 626854fbb9cSKeith Busch 627d6d48196SJens Axboe /* 628d6d48196SJens Axboe * Will it become too large? 629d6d48196SJens Axboe */ 630f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 63117007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 632d6d48196SJens Axboe return 0; 633d6d48196SJens Axboe 634d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 63586771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 63686771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 63786771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 63886771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 63986771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 640d6d48196SJens Axboe total_phys_segments--; 64186771427SFUJITA Tomonori } 642d6d48196SJens Axboe 6438a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 644d6d48196SJens Axboe return 0; 645d6d48196SJens Axboe 6464eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 64713f05c8dSMartin K. Petersen return 0; 64813f05c8dSMartin K. Petersen 649d6d48196SJens Axboe /* Merge is OK... */ 650d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 651d6d48196SJens Axboe return 1; 652d6d48196SJens Axboe } 653d6d48196SJens Axboe 65480a761fdSTejun Heo /** 65580a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 65680a761fdSTejun Heo * @rq: request to mark as mixed merge 65780a761fdSTejun Heo * 65880a761fdSTejun Heo * Description: 65980a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 66080a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 66180a761fdSTejun Heo * merged. 66280a761fdSTejun Heo */ 66380a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 66480a761fdSTejun Heo { 66580a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 66680a761fdSTejun Heo struct bio *bio; 66780a761fdSTejun Heo 668e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 66980a761fdSTejun Heo return; 67080a761fdSTejun Heo 67180a761fdSTejun Heo /* 67280a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 67380a761fdSTejun Heo * contained bios. It will just track those of the first one. 67480a761fdSTejun Heo * Distributes the attributs to each bio. 67580a761fdSTejun Heo */ 67680a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 6771eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 6781eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 6791eff9d32SJens Axboe bio->bi_opf |= ff; 68080a761fdSTejun Heo } 681e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 68280a761fdSTejun Heo } 68380a761fdSTejun Heo 68426308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 68526308eabSJerome Marchand { 68626308eabSJerome Marchand if (blk_do_io_stat(req)) { 68726308eabSJerome Marchand struct hd_struct *part; 68826308eabSJerome Marchand 689*112f158fSMike Snitzer part_stat_lock(); 69009e099d4SJerome Marchand part = req->part; 69126308eabSJerome Marchand 692*112f158fSMike Snitzer part_round_stats(req->q, part); 693d62e26b3SJens Axboe part_dec_in_flight(req->q, part, rq_data_dir(req)); 69426308eabSJerome Marchand 6956c23a968SJens Axboe hd_struct_put(part); 69626308eabSJerome Marchand part_stat_unlock(); 69726308eabSJerome Marchand } 69826308eabSJerome Marchand } 69969840466SJianchao Wang /* 70069840466SJianchao Wang * Two cases of handling DISCARD merge: 70169840466SJianchao Wang * If max_discard_segments > 1, the driver takes every bio 70269840466SJianchao Wang * as a range and send them to controller together. The ranges 70369840466SJianchao Wang * needn't to be contiguous. 70469840466SJianchao Wang * Otherwise, the bios/requests will be handled as same as 70569840466SJianchao Wang * others which should be contiguous. 70669840466SJianchao Wang */ 70769840466SJianchao Wang static inline bool blk_discard_mergable(struct request *req) 70869840466SJianchao Wang { 70969840466SJianchao Wang if (req_op(req) == REQ_OP_DISCARD && 71069840466SJianchao Wang queue_max_discard_segments(req->q) > 1) 71169840466SJianchao Wang return true; 71269840466SJianchao Wang return false; 71369840466SJianchao Wang } 71469840466SJianchao Wang 715e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req, 716e96c0d83SEric Biggers struct request *next) 71769840466SJianchao Wang { 71869840466SJianchao Wang if (blk_discard_mergable(req)) 71969840466SJianchao Wang return ELEVATOR_DISCARD_MERGE; 72069840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) 72169840466SJianchao Wang return ELEVATOR_BACK_MERGE; 72269840466SJianchao Wang 72369840466SJianchao Wang return ELEVATOR_NO_MERGE; 72469840466SJianchao Wang } 72526308eabSJerome Marchand 726d6d48196SJens Axboe /* 727b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 728b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 729d6d48196SJens Axboe */ 730b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 731b973cb7eSJens Axboe struct request *req, struct request *next) 732d6d48196SJens Axboe { 733d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 734b973cb7eSJens Axboe return NULL; 735d6d48196SJens Axboe 736288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 737b973cb7eSJens Axboe return NULL; 738f31dc1cdSMartin K. Petersen 739d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 7402081a56bSJens Axboe || req->rq_disk != next->rq_disk) 741b973cb7eSJens Axboe return NULL; 742d6d48196SJens Axboe 7438fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 7444363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 745b973cb7eSJens Axboe return NULL; 7464363ac7cSMartin K. Petersen 747d6d48196SJens Axboe /* 748cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 749cb6934f8SJens Axboe * non-hint IO. 750cb6934f8SJens Axboe */ 751cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 752cb6934f8SJens Axboe return NULL; 753cb6934f8SJens Axboe 754668ffc03SDamien Le Moal if (req->ioprio != next->ioprio) 755668ffc03SDamien Le Moal return NULL; 756668ffc03SDamien Le Moal 757cb6934f8SJens Axboe /* 758d6d48196SJens Axboe * If we are allowed to merge, then append bio list 759d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 760d6d48196SJens Axboe * will have updated segment counts, update sector 761445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 762445251d0SJens Axboe * have separate settings. 763d6d48196SJens Axboe */ 76469840466SJianchao Wang 76569840466SJianchao Wang switch (blk_try_req_merge(req, next)) { 76669840466SJianchao Wang case ELEVATOR_DISCARD_MERGE: 767445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 768445251d0SJens Axboe return NULL; 76969840466SJianchao Wang break; 77069840466SJianchao Wang case ELEVATOR_BACK_MERGE: 77169840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next)) 772b973cb7eSJens Axboe return NULL; 77369840466SJianchao Wang break; 77469840466SJianchao Wang default: 77569840466SJianchao Wang return NULL; 77669840466SJianchao Wang } 777d6d48196SJens Axboe 778d6d48196SJens Axboe /* 77980a761fdSTejun Heo * If failfast settings disagree or any of the two is already 78080a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 78180a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 78280a761fdSTejun Heo * set properly. 78380a761fdSTejun Heo */ 784e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 78580a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 78680a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 78780a761fdSTejun Heo blk_rq_set_mixed_merge(req); 78880a761fdSTejun Heo blk_rq_set_mixed_merge(next); 78980a761fdSTejun Heo } 79080a761fdSTejun Heo 79180a761fdSTejun Heo /* 792522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 793522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 794522a7775SOmar Sandoval * current request for accounting purposes. 795d6d48196SJens Axboe */ 796522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 797522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 798d6d48196SJens Axboe 799d6d48196SJens Axboe req->biotail->bi_next = next->bio; 800d6d48196SJens Axboe req->biotail = next->biotail; 801d6d48196SJens Axboe 802a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 803d6d48196SJens Axboe 8042a5cf35cSMing Lei if (!blk_discard_mergable(req)) 805d6d48196SJens Axboe elv_merge_requests(q, req, next); 806d6d48196SJens Axboe 80742dad764SJerome Marchand /* 80842dad764SJerome Marchand * 'next' is going away, so update stats accordingly 80942dad764SJerome Marchand */ 81042dad764SJerome Marchand blk_account_io_merge(next); 811d6d48196SJens Axboe 812e4d750c9SJens Axboe /* 813e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 814e4d750c9SJens Axboe * the caller to free 815e4d750c9SJens Axboe */ 8161cd96c24SBoaz Harrosh next->bio = NULL; 817b973cb7eSJens Axboe return next; 818d6d48196SJens Axboe } 819d6d48196SJens Axboe 820b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq) 821d6d48196SJens Axboe { 822d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 823d6d48196SJens Axboe 824d6d48196SJens Axboe if (next) 825d6d48196SJens Axboe return attempt_merge(q, rq, next); 826d6d48196SJens Axboe 827b973cb7eSJens Axboe return NULL; 828d6d48196SJens Axboe } 829d6d48196SJens Axboe 830b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq) 831d6d48196SJens Axboe { 832d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 833d6d48196SJens Axboe 834d6d48196SJens Axboe if (prev) 835d6d48196SJens Axboe return attempt_merge(q, prev, rq); 836d6d48196SJens Axboe 837b973cb7eSJens Axboe return NULL; 838d6d48196SJens Axboe } 8395e84ea3aSJens Axboe 8405e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 8415e84ea3aSJens Axboe struct request *next) 8425e84ea3aSJens Axboe { 843e4d750c9SJens Axboe struct request *free; 84472ef799bSTahsin Erdogan 845e4d750c9SJens Axboe free = attempt_merge(q, rq, next); 846e4d750c9SJens Axboe if (free) { 84792bc5a24SJens Axboe blk_put_request(free); 848e4d750c9SJens Axboe return 1; 849e4d750c9SJens Axboe } 850e4d750c9SJens Axboe 851e4d750c9SJens Axboe return 0; 8525e84ea3aSJens Axboe } 853050c8ea8STejun Heo 854050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 855050c8ea8STejun Heo { 856e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 857050c8ea8STejun Heo return false; 858050c8ea8STejun Heo 859288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 860f31dc1cdSMartin K. Petersen return false; 861f31dc1cdSMartin K. Petersen 862050c8ea8STejun Heo /* different data direction or already started, don't merge */ 863050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 864050c8ea8STejun Heo return false; 865050c8ea8STejun Heo 8662081a56bSJens Axboe /* must be same device */ 8672081a56bSJens Axboe if (rq->rq_disk != bio->bi_disk) 868050c8ea8STejun Heo return false; 869050c8ea8STejun Heo 870050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 8714eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 872050c8ea8STejun Heo return false; 873050c8ea8STejun Heo 8744363ac7cSMartin K. Petersen /* must be using the same buffer */ 8758fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 8764363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 8774363ac7cSMartin K. Petersen return false; 8784363ac7cSMartin K. Petersen 879cb6934f8SJens Axboe /* 880cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 881cb6934f8SJens Axboe * non-hint IO. 882cb6934f8SJens Axboe */ 883cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 884cb6934f8SJens Axboe return false; 885cb6934f8SJens Axboe 886668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio)) 887668ffc03SDamien Le Moal return false; 888668ffc03SDamien Le Moal 889050c8ea8STejun Heo return true; 890050c8ea8STejun Heo } 891050c8ea8STejun Heo 89234fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 893050c8ea8STejun Heo { 89469840466SJianchao Wang if (blk_discard_mergable(rq)) 8951e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 8961e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 897050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 8984f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 899050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 900050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 901050c8ea8STejun Heo } 902