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 164dcebd755SMing Lei static unsigned get_max_segment_size(struct request_queue *q, 165dcebd755SMing Lei unsigned offset) 166dcebd755SMing Lei { 167dcebd755SMing Lei unsigned long mask = queue_segment_boundary(q); 168dcebd755SMing Lei 169dcebd755SMing Lei /* default segment boundary mask means no boundary limit */ 170dcebd755SMing Lei if (mask == BLK_SEG_BOUNDARY_MASK) 171dcebd755SMing Lei return queue_max_segment_size(q); 172dcebd755SMing Lei 173dcebd755SMing Lei return min_t(unsigned long, mask - (mask & offset) + 1, 174dcebd755SMing Lei queue_max_segment_size(q)); 175dcebd755SMing Lei } 176dcebd755SMing Lei 177dcebd755SMing Lei /* 178dcebd755SMing Lei * Split the bvec @bv into segments, and update all kinds of 179dcebd755SMing Lei * variables. 180dcebd755SMing Lei */ 181dcebd755SMing Lei static bool bvec_split_segs(struct request_queue *q, struct bio_vec *bv, 182dcebd755SMing Lei unsigned *nsegs, unsigned *last_seg_size, 18305b700baSMing Lei unsigned *front_seg_size, unsigned *sectors, unsigned max_segs) 184dcebd755SMing Lei { 185dcebd755SMing Lei unsigned len = bv->bv_len; 186dcebd755SMing Lei unsigned total_len = 0; 187dcebd755SMing Lei unsigned new_nsegs = 0, seg_size = 0; 188dcebd755SMing Lei 189dcebd755SMing Lei /* 190dcebd755SMing Lei * Multi-page bvec may be too big to hold in one segment, so the 191dcebd755SMing Lei * current bvec has to be splitted as multiple segments. 192dcebd755SMing Lei */ 19305b700baSMing Lei while (len && new_nsegs + *nsegs < max_segs) { 194dcebd755SMing Lei seg_size = get_max_segment_size(q, bv->bv_offset + total_len); 195dcebd755SMing Lei seg_size = min(seg_size, len); 196dcebd755SMing Lei 197dcebd755SMing Lei new_nsegs++; 198dcebd755SMing Lei total_len += seg_size; 199dcebd755SMing Lei len -= seg_size; 200dcebd755SMing Lei 201dcebd755SMing Lei if ((bv->bv_offset + total_len) & queue_virt_boundary(q)) 202dcebd755SMing Lei break; 203dcebd755SMing Lei } 204dcebd755SMing Lei 205dcebd755SMing Lei if (!new_nsegs) 206dcebd755SMing Lei return !!len; 207dcebd755SMing Lei 208dcebd755SMing Lei /* update front segment size */ 209dcebd755SMing Lei if (!*nsegs) { 210dcebd755SMing Lei unsigned first_seg_size; 211dcebd755SMing Lei 212dcebd755SMing Lei if (new_nsegs == 1) 213dcebd755SMing Lei first_seg_size = get_max_segment_size(q, bv->bv_offset); 214dcebd755SMing Lei else 215dcebd755SMing Lei first_seg_size = queue_max_segment_size(q); 216dcebd755SMing Lei 217dcebd755SMing Lei if (*front_seg_size < first_seg_size) 218dcebd755SMing Lei *front_seg_size = first_seg_size; 219dcebd755SMing Lei } 220dcebd755SMing Lei 221dcebd755SMing Lei /* update other varibles */ 222dcebd755SMing Lei *last_seg_size = seg_size; 223dcebd755SMing Lei *nsegs += new_nsegs; 224dcebd755SMing Lei if (sectors) 225dcebd755SMing Lei *sectors += total_len >> 9; 226dcebd755SMing Lei 227dcebd755SMing Lei /* split in the middle of the bvec if len != 0 */ 228dcebd755SMing Lei return !!len; 229dcebd755SMing Lei } 230dcebd755SMing Lei 23154efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 23254efd50bSKent Overstreet struct bio *bio, 233bdced438SMing Lei struct bio_set *bs, 234bdced438SMing Lei unsigned *segs) 23554efd50bSKent Overstreet { 2365014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 23754efd50bSKent Overstreet struct bvec_iter iter; 2388ae12666SKent Overstreet unsigned seg_size = 0, nsegs = 0, sectors = 0; 23902e70742SMing Lei unsigned front_seg_size = bio->bi_seg_front_size; 24002e70742SMing Lei bool do_split = true; 24102e70742SMing Lei struct bio *new = NULL; 242d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 24305b700baSMing Lei const unsigned max_segs = queue_max_segments(q); 24454efd50bSKent Overstreet 245dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 24654efd50bSKent Overstreet /* 24754efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 24854efd50bSKent Overstreet * offset would create a gap, disallow it. 24954efd50bSKent Overstreet */ 2505014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 25154efd50bSKent Overstreet goto split; 25254efd50bSKent Overstreet 253d0e5fbb0SMing Lei if (sectors + (bv.bv_len >> 9) > max_sectors) { 254e36f6204SKeith Busch /* 255e36f6204SKeith Busch * Consider this a new segment if we're splitting in 256e36f6204SKeith Busch * the middle of this vector. 257e36f6204SKeith Busch */ 25805b700baSMing Lei if (nsegs < max_segs && 259d0e5fbb0SMing Lei sectors < max_sectors) { 260dcebd755SMing Lei /* split in the middle of bvec */ 261dcebd755SMing Lei bv.bv_len = (max_sectors - sectors) << 9; 262dcebd755SMing Lei bvec_split_segs(q, &bv, &nsegs, 263dcebd755SMing Lei &seg_size, 264dcebd755SMing Lei &front_seg_size, 26505b700baSMing Lei §ors, max_segs); 266e36f6204SKeith Busch } 267e36f6204SKeith Busch goto split; 268e36f6204SKeith Busch } 269e36f6204SKeith Busch 27005b700baSMing Lei if (nsegs == max_segs) 27154efd50bSKent Overstreet goto split; 27254efd50bSKent Overstreet 27354efd50bSKent Overstreet bvprv = bv; 274578270bfSMing Lei bvprvp = &bvprv; 275dcebd755SMing Lei 276bbcbbd56SMing Lei if (bv.bv_offset + bv.bv_len <= PAGE_SIZE) { 277bbcbbd56SMing Lei nsegs++; 278bbcbbd56SMing Lei seg_size = bv.bv_len; 279bbcbbd56SMing Lei sectors += bv.bv_len >> 9; 280bbcbbd56SMing Lei if (nsegs == 1 && seg_size > front_seg_size) 281bbcbbd56SMing Lei front_seg_size = seg_size; 282bbcbbd56SMing Lei } else if (bvec_split_segs(q, &bv, &nsegs, &seg_size, 28305b700baSMing Lei &front_seg_size, §ors, max_segs)) { 284dcebd755SMing Lei goto split; 285bbcbbd56SMing Lei } 28654efd50bSKent Overstreet } 28754efd50bSKent Overstreet 28802e70742SMing Lei do_split = false; 28954efd50bSKent Overstreet split: 290bdced438SMing Lei *segs = nsegs; 29102e70742SMing Lei 29202e70742SMing Lei if (do_split) { 29302e70742SMing Lei new = bio_split(bio, sectors, GFP_NOIO, bs); 29402e70742SMing Lei if (new) 29502e70742SMing Lei bio = new; 29602e70742SMing Lei } 29702e70742SMing Lei 29802e70742SMing Lei bio->bi_seg_front_size = front_seg_size; 29902e70742SMing Lei if (seg_size > bio->bi_seg_back_size) 30002e70742SMing Lei bio->bi_seg_back_size = seg_size; 30102e70742SMing Lei 30202e70742SMing Lei return do_split ? new : NULL; 30354efd50bSKent Overstreet } 30454efd50bSKent Overstreet 305af67c31fSNeilBrown void blk_queue_split(struct request_queue *q, struct bio **bio) 30654efd50bSKent Overstreet { 307bdced438SMing Lei struct bio *split, *res; 308bdced438SMing Lei unsigned nsegs; 30954efd50bSKent Overstreet 3107afafc8aSAdrian Hunter switch (bio_op(*bio)) { 3117afafc8aSAdrian Hunter case REQ_OP_DISCARD: 3127afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 313338aa96dSKent Overstreet split = blk_bio_discard_split(q, *bio, &q->bio_split, &nsegs); 3147afafc8aSAdrian Hunter break; 315a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 316338aa96dSKent Overstreet split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, &nsegs); 317a6f0788eSChaitanya Kulkarni break; 3187afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 319338aa96dSKent Overstreet split = blk_bio_write_same_split(q, *bio, &q->bio_split, &nsegs); 3207afafc8aSAdrian Hunter break; 3217afafc8aSAdrian Hunter default: 322338aa96dSKent Overstreet split = blk_bio_segment_split(q, *bio, &q->bio_split, &nsegs); 3237afafc8aSAdrian Hunter break; 3247afafc8aSAdrian Hunter } 325bdced438SMing Lei 326bdced438SMing Lei /* physical segments can be figured out during splitting */ 327bdced438SMing Lei res = split ? split : *bio; 328bdced438SMing Lei res->bi_phys_segments = nsegs; 329bdced438SMing Lei bio_set_flag(res, BIO_SEG_VALID); 33054efd50bSKent Overstreet 33154efd50bSKent Overstreet if (split) { 3326ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 3331eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 3346ac45aebSMing Lei 335947b7ac1SJens Axboe /* 336947b7ac1SJens Axboe * Since we're recursing into make_request here, ensure 337947b7ac1SJens Axboe * that we mark this bio as already having entered the queue. 338947b7ac1SJens Axboe * If not, and the queue is going away, we can get stuck 339947b7ac1SJens Axboe * forever on waiting for the queue reference to drop. But 340947b7ac1SJens Axboe * that will never happen, as we're already holding a 341947b7ac1SJens Axboe * reference to it. 342947b7ac1SJens Axboe */ 343947b7ac1SJens Axboe bio_set_flag(*bio, BIO_QUEUE_ENTERED); 344947b7ac1SJens Axboe 34554efd50bSKent Overstreet bio_chain(split, *bio); 346cda22646SMike Krinkin trace_block_split(q, split, (*bio)->bi_iter.bi_sector); 34754efd50bSKent Overstreet generic_make_request(*bio); 34854efd50bSKent Overstreet *bio = split; 34954efd50bSKent Overstreet } 35054efd50bSKent Overstreet } 35154efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 35254efd50bSKent Overstreet 3531e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 3542705c937SMing Lei struct bio *bio) 355d6d48196SJens Axboe { 356*b21e11c5SMing Lei struct bio_vec uninitialized_var(bv), bvprv = { NULL }; 3571e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 35849b1f22bSMing Lei unsigned front_seg_size; 35959247eaeSJens Axboe struct bio *fbio, *bbio; 3607988613bSKent Overstreet struct bvec_iter iter; 361f6970f83SMing Lei bool new_bio = false; 362d6d48196SJens Axboe 3631e428079SJens Axboe if (!bio) 3641e428079SJens Axboe return 0; 365d6d48196SJens Axboe 36649b1f22bSMing Lei front_seg_size = bio->bi_seg_front_size; 36749b1f22bSMing Lei 368a6f0788eSChaitanya Kulkarni switch (bio_op(bio)) { 369a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 370a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 371a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 372f9d03f96SChristoph Hellwig return 0; 373f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 3745cb8850cSKent Overstreet return 1; 375a6f0788eSChaitanya Kulkarni } 3765cb8850cSKent Overstreet 3771e428079SJens Axboe fbio = bio; 3785df97b91SMikulas Patocka seg_size = 0; 3792c8919deSAndi Kleen nr_phys_segs = 0; 3801e428079SJens Axboe for_each_bio(bio) { 381dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 382f6970f83SMing Lei if (new_bio) { 3837988613bSKent Overstreet if (seg_size + bv.bv_len 384ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 385d6d48196SJens Axboe goto new_segment; 3863dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, &bvprv, &bv)) 387d6d48196SJens Axboe goto new_segment; 388d6d48196SJens Axboe 3897988613bSKent Overstreet seg_size += bv.bv_len; 390aaeee62cSMing Lei 391aaeee62cSMing Lei if (nr_phys_segs == 1 && seg_size > 392aaeee62cSMing Lei front_seg_size) 393aaeee62cSMing Lei front_seg_size = seg_size; 394aaeee62cSMing Lei 395d6d48196SJens Axboe continue; 396d6d48196SJens Axboe } 397d6d48196SJens Axboe new_segment: 398dcebd755SMing Lei bvec_split_segs(q, &bv, &nr_phys_segs, &seg_size, 39905b700baSMing Lei &front_seg_size, NULL, UINT_MAX); 400f6970f83SMing Lei new_bio = false; 401d6d48196SJens Axboe } 40259247eaeSJens Axboe bbio = bio; 403*b21e11c5SMing Lei if (likely(bio->bi_iter.bi_size)) { 404f6970f83SMing Lei bvprv = bv; 405f6970f83SMing Lei new_bio = true; 4061e428079SJens Axboe } 407*b21e11c5SMing Lei } 408d6d48196SJens Axboe 409dcebd755SMing Lei fbio->bi_seg_front_size = front_seg_size; 41059247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 41159247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 4121e428079SJens Axboe 4131e428079SJens Axboe return nr_phys_segs; 4141e428079SJens Axboe } 4151e428079SJens Axboe 4161e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 4171e428079SJens Axboe { 4182705c937SMing Lei rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio); 419d6d48196SJens Axboe } 420d6d48196SJens Axboe 421d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 422d6d48196SJens Axboe { 423d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 4241e428079SJens Axboe 425d6d48196SJens Axboe bio->bi_next = NULL; 4262705c937SMing Lei bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio); 427d6d48196SJens Axboe bio->bi_next = nxt; 42805f1dd53SJens Axboe 429b7c44ed9SJens Axboe bio_set_flag(bio, BIO_SEG_VALID); 430d6d48196SJens Axboe } 431d6d48196SJens Axboe 432d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 433d6d48196SJens Axboe struct bio *nxt) 434d6d48196SJens Axboe { 4352b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 436f619d254SKent Overstreet 43786771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 438ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 439d6d48196SJens Axboe return 0; 440d6d48196SJens Axboe 441e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 442e17fc0a1SDavid Woodhouse return 1; 443e17fc0a1SDavid Woodhouse 444e827091cSMing Lei bio_get_last_bvec(bio, &end_bv); 445e827091cSMing Lei bio_get_first_bvec(nxt, &nxt_bv); 446f619d254SKent Overstreet 4473dccdae5SChristoph Hellwig return biovec_phys_mergeable(q, &end_bv, &nxt_bv); 448d6d48196SJens Axboe } 449d6d48196SJens Axboe 45048d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, 451862e5a5eSMing Lei struct scatterlist *sglist) 452862e5a5eSMing Lei { 453862e5a5eSMing Lei if (!*sg) 454862e5a5eSMing Lei return sglist; 455862e5a5eSMing Lei 456862e5a5eSMing Lei /* 457862e5a5eSMing Lei * If the driver previously mapped a shorter list, we could see a 458862e5a5eSMing Lei * termination bit prematurely unless it fully inits the sg table 459862e5a5eSMing Lei * on each mapping. We KNOW that there must be more entries here 460862e5a5eSMing Lei * or the driver would be buggy, so force clear the termination bit 461862e5a5eSMing Lei * to avoid doing a full sg_init_table() in drivers for each command. 462862e5a5eSMing Lei */ 463862e5a5eSMing Lei sg_unmark_end(*sg); 464862e5a5eSMing Lei return sg_next(*sg); 465862e5a5eSMing Lei } 466862e5a5eSMing Lei 467862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q, 468862e5a5eSMing Lei struct bio_vec *bvec, struct scatterlist *sglist, 469862e5a5eSMing Lei struct scatterlist **sg) 470862e5a5eSMing Lei { 471862e5a5eSMing Lei unsigned nbytes = bvec->bv_len; 472862e5a5eSMing Lei unsigned nsegs = 0, total = 0, offset = 0; 473862e5a5eSMing Lei 474862e5a5eSMing Lei while (nbytes > 0) { 475862e5a5eSMing Lei unsigned seg_size; 476862e5a5eSMing Lei struct page *pg; 477862e5a5eSMing Lei unsigned idx; 478862e5a5eSMing Lei 479862e5a5eSMing Lei *sg = blk_next_sg(sg, sglist); 480862e5a5eSMing Lei 481862e5a5eSMing Lei seg_size = get_max_segment_size(q, bvec->bv_offset + total); 482862e5a5eSMing Lei seg_size = min(nbytes, seg_size); 483862e5a5eSMing Lei 484862e5a5eSMing Lei offset = (total + bvec->bv_offset) % PAGE_SIZE; 485862e5a5eSMing Lei idx = (total + bvec->bv_offset) / PAGE_SIZE; 4864d633062SMing Lei pg = bvec_nth_page(bvec->bv_page, idx); 487862e5a5eSMing Lei 488862e5a5eSMing Lei sg_set_page(*sg, pg, seg_size, offset); 489862e5a5eSMing Lei 490862e5a5eSMing Lei total += seg_size; 491862e5a5eSMing Lei nbytes -= seg_size; 492862e5a5eSMing Lei nsegs++; 493862e5a5eSMing Lei } 494862e5a5eSMing Lei 495862e5a5eSMing Lei return nsegs; 496862e5a5eSMing Lei } 497862e5a5eSMing Lei 49816e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv, 49916e3e418SMing Lei struct scatterlist *sglist, struct scatterlist **sg) 50016e3e418SMing Lei { 50116e3e418SMing Lei *sg = blk_next_sg(sg, sglist); 50216e3e418SMing Lei sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 50316e3e418SMing Lei return 1; 50416e3e418SMing Lei } 50516e3e418SMing Lei 506f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */ 507f6970f83SMing Lei static inline bool 508f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec, 509f6970f83SMing Lei struct bio_vec *bvprv, struct scatterlist **sg) 510963ab9e5SAsias He { 511963ab9e5SAsias He 512963ab9e5SAsias He int nbytes = bvec->bv_len; 513963ab9e5SAsias He 514f6970f83SMing Lei if (!*sg) 515f6970f83SMing Lei return false; 516f6970f83SMing Lei 517b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 518f6970f83SMing Lei return false; 519f6970f83SMing Lei 5203dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec)) 521f6970f83SMing Lei return false; 522963ab9e5SAsias He 523963ab9e5SAsias He (*sg)->length += nbytes; 524f6970f83SMing Lei 525f6970f83SMing Lei return true; 526963ab9e5SAsias He } 527963ab9e5SAsias He 5285cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 5295cb8850cSKent Overstreet struct scatterlist *sglist, 5305cb8850cSKent Overstreet struct scatterlist **sg) 5315cb8850cSKent Overstreet { 532*b21e11c5SMing Lei struct bio_vec uninitialized_var(bvec), bvprv = { NULL }; 5335cb8850cSKent Overstreet struct bvec_iter iter; 53438417468SChristoph Hellwig int nsegs = 0; 535f6970f83SMing Lei bool new_bio = false; 5365cb8850cSKent Overstreet 537f6970f83SMing Lei for_each_bio(bio) { 538f6970f83SMing Lei bio_for_each_bvec(bvec, bio, iter) { 539f6970f83SMing Lei /* 540f6970f83SMing Lei * Only try to merge bvecs from two bios given we 541f6970f83SMing Lei * have done bio internal merge when adding pages 542f6970f83SMing Lei * to bio 543f6970f83SMing Lei */ 544f6970f83SMing Lei if (new_bio && 545f6970f83SMing Lei __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg)) 546f6970f83SMing Lei goto next_bvec; 547f6970f83SMing Lei 548f6970f83SMing Lei if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE) 549f6970f83SMing Lei nsegs += __blk_bvec_map_sg(bvec, sglist, sg); 550f6970f83SMing Lei else 551f6970f83SMing Lei nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg); 552f6970f83SMing Lei next_bvec: 553f6970f83SMing Lei new_bio = false; 554f6970f83SMing Lei } 555*b21e11c5SMing Lei if (likely(bio->bi_iter.bi_size)) { 556f6970f83SMing Lei bvprv = bvec; 557f6970f83SMing Lei new_bio = true; 558f6970f83SMing Lei } 559*b21e11c5SMing Lei } 5605cb8850cSKent Overstreet 5615cb8850cSKent Overstreet return nsegs; 5625cb8850cSKent Overstreet } 5635cb8850cSKent Overstreet 564d6d48196SJens Axboe /* 565d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 566d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 567d6d48196SJens Axboe */ 568d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 569d6d48196SJens Axboe struct scatterlist *sglist) 570d6d48196SJens Axboe { 5715cb8850cSKent Overstreet struct scatterlist *sg = NULL; 5725cb8850cSKent Overstreet int nsegs = 0; 573d6d48196SJens Axboe 574f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 575cae6c2e5SMing Lei nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, &sg); 576f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 577cae6c2e5SMing Lei nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, &sg); 578f9d03f96SChristoph Hellwig else if (rq->bio) 5795cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 580f18573abSFUJITA Tomonori 581e8064021SChristoph Hellwig if (unlikely(rq->rq_flags & RQF_COPY_USER) && 5822e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 5832e46e8b2STejun Heo unsigned int pad_len = 5842e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 585f18573abSFUJITA Tomonori 586f18573abSFUJITA Tomonori sg->length += pad_len; 587f18573abSFUJITA Tomonori rq->extra_len += pad_len; 588f18573abSFUJITA Tomonori } 589f18573abSFUJITA Tomonori 5902fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 591a8ebb056SMike Christie if (op_is_write(req_op(rq))) 592db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 593db0a2e00STejun Heo 594da81ed16SDan Williams sg_unmark_end(sg); 595d6d48196SJens Axboe sg = sg_next(sg); 596d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 597d6d48196SJens Axboe q->dma_drain_size, 598d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 599d6d48196SJens Axboe (PAGE_SIZE - 1)); 600d6d48196SJens Axboe nsegs++; 6017a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 602d6d48196SJens Axboe } 603d6d48196SJens Axboe 604d6d48196SJens Axboe if (sg) 605d6d48196SJens Axboe sg_mark_end(sg); 606d6d48196SJens Axboe 60712e57f59SMing Lei /* 60812e57f59SMing Lei * Something must have been wrong if the figured number of 60912e57f59SMing Lei * segment is bigger than number of req's physical segments 61012e57f59SMing Lei */ 611f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 61212e57f59SMing Lei 613d6d48196SJens Axboe return nsegs; 614d6d48196SJens Axboe } 615d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 616d6d48196SJens Axboe 617d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 618d6d48196SJens Axboe struct request *req, 619d6d48196SJens Axboe struct bio *bio) 620d6d48196SJens Axboe { 621d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 622d6d48196SJens Axboe 62313f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 62413f05c8dSMartin K. Petersen goto no_merge; 62513f05c8dSMartin K. Petersen 6264eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(q, req, bio) == false) 62713f05c8dSMartin K. Petersen goto no_merge; 628d6d48196SJens Axboe 629d6d48196SJens Axboe /* 630d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 631d6d48196SJens Axboe * counters. 632d6d48196SJens Axboe */ 633d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 634d6d48196SJens Axboe return 1; 63513f05c8dSMartin K. Petersen 63613f05c8dSMartin K. Petersen no_merge: 637e0c72300SRitesh Harjani req_set_nomerge(q, req); 63813f05c8dSMartin K. Petersen return 0; 639d6d48196SJens Axboe } 640d6d48196SJens Axboe 641d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 642d6d48196SJens Axboe struct bio *bio) 643d6d48196SJens Axboe { 6445e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 6455e7c4274SJens Axboe return 0; 6467f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6477f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 6487f39add3SSagi Grimberg return 0; 649f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 65017007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 651e0c72300SRitesh Harjani req_set_nomerge(q, req); 652d6d48196SJens Axboe return 0; 653d6d48196SJens Axboe } 6542cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 655d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 6562cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 657d6d48196SJens Axboe blk_recount_segments(q, bio); 658d6d48196SJens Axboe 659d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 660d6d48196SJens Axboe } 661d6d48196SJens Axboe 662d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 663d6d48196SJens Axboe struct bio *bio) 664d6d48196SJens Axboe { 6655e7c4274SJens Axboe 6665e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 6675e7c4274SJens Axboe return 0; 6687f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6697f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 6707f39add3SSagi Grimberg return 0; 671f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 67217007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 673e0c72300SRitesh Harjani req_set_nomerge(q, req); 674d6d48196SJens Axboe return 0; 675d6d48196SJens Axboe } 6762cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 677d6d48196SJens Axboe blk_recount_segments(q, bio); 6782cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 679d6d48196SJens Axboe blk_recount_segments(q, req->bio); 680d6d48196SJens Axboe 681d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 682d6d48196SJens Axboe } 683d6d48196SJens Axboe 684445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 685445251d0SJens Axboe struct request *next) 686445251d0SJens Axboe { 687445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 688445251d0SJens Axboe 689445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 690445251d0SJens Axboe goto no_merge; 691445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 692445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 693445251d0SJens Axboe goto no_merge; 694445251d0SJens Axboe 695445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 696445251d0SJens Axboe return true; 697445251d0SJens Axboe no_merge: 698445251d0SJens Axboe req_set_nomerge(q, req); 699445251d0SJens Axboe return false; 700445251d0SJens Axboe } 701445251d0SJens Axboe 702d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 703d6d48196SJens Axboe struct request *next) 704d6d48196SJens Axboe { 705d6d48196SJens Axboe int total_phys_segments; 70686771427SFUJITA Tomonori unsigned int seg_size = 70786771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 708d6d48196SJens Axboe 7095e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 710854fbb9cSKeith Busch return 0; 711854fbb9cSKeith Busch 712d6d48196SJens Axboe /* 713d6d48196SJens Axboe * Will it become too large? 714d6d48196SJens Axboe */ 715f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 71617007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 717d6d48196SJens Axboe return 0; 718d6d48196SJens Axboe 719d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 72086771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 72186771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 72286771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 72386771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 72486771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 725d6d48196SJens Axboe total_phys_segments--; 72686771427SFUJITA Tomonori } 727d6d48196SJens Axboe 7288a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 729d6d48196SJens Axboe return 0; 730d6d48196SJens Axboe 7314eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 73213f05c8dSMartin K. Petersen return 0; 73313f05c8dSMartin K. Petersen 734d6d48196SJens Axboe /* Merge is OK... */ 735d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 736d6d48196SJens Axboe return 1; 737d6d48196SJens Axboe } 738d6d48196SJens Axboe 73980a761fdSTejun Heo /** 74080a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 74180a761fdSTejun Heo * @rq: request to mark as mixed merge 74280a761fdSTejun Heo * 74380a761fdSTejun Heo * Description: 74480a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 74580a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 74680a761fdSTejun Heo * merged. 74780a761fdSTejun Heo */ 74880a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 74980a761fdSTejun Heo { 75080a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 75180a761fdSTejun Heo struct bio *bio; 75280a761fdSTejun Heo 753e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 75480a761fdSTejun Heo return; 75580a761fdSTejun Heo 75680a761fdSTejun Heo /* 75780a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 75880a761fdSTejun Heo * contained bios. It will just track those of the first one. 75980a761fdSTejun Heo * Distributes the attributs to each bio. 76080a761fdSTejun Heo */ 76180a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 7621eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 7631eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 7641eff9d32SJens Axboe bio->bi_opf |= ff; 76580a761fdSTejun Heo } 766e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 76780a761fdSTejun Heo } 76880a761fdSTejun Heo 76926308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 77026308eabSJerome Marchand { 77126308eabSJerome Marchand if (blk_do_io_stat(req)) { 77226308eabSJerome Marchand struct hd_struct *part; 77326308eabSJerome Marchand 774112f158fSMike Snitzer part_stat_lock(); 77509e099d4SJerome Marchand part = req->part; 77626308eabSJerome Marchand 777d62e26b3SJens Axboe part_dec_in_flight(req->q, part, rq_data_dir(req)); 77826308eabSJerome Marchand 7796c23a968SJens Axboe hd_struct_put(part); 78026308eabSJerome Marchand part_stat_unlock(); 78126308eabSJerome Marchand } 78226308eabSJerome Marchand } 78369840466SJianchao Wang /* 78469840466SJianchao Wang * Two cases of handling DISCARD merge: 78569840466SJianchao Wang * If max_discard_segments > 1, the driver takes every bio 78669840466SJianchao Wang * as a range and send them to controller together. The ranges 78769840466SJianchao Wang * needn't to be contiguous. 78869840466SJianchao Wang * Otherwise, the bios/requests will be handled as same as 78969840466SJianchao Wang * others which should be contiguous. 79069840466SJianchao Wang */ 79169840466SJianchao Wang static inline bool blk_discard_mergable(struct request *req) 79269840466SJianchao Wang { 79369840466SJianchao Wang if (req_op(req) == REQ_OP_DISCARD && 79469840466SJianchao Wang queue_max_discard_segments(req->q) > 1) 79569840466SJianchao Wang return true; 79669840466SJianchao Wang return false; 79769840466SJianchao Wang } 79869840466SJianchao Wang 799e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req, 800e96c0d83SEric Biggers struct request *next) 80169840466SJianchao Wang { 80269840466SJianchao Wang if (blk_discard_mergable(req)) 80369840466SJianchao Wang return ELEVATOR_DISCARD_MERGE; 80469840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) 80569840466SJianchao Wang return ELEVATOR_BACK_MERGE; 80669840466SJianchao Wang 80769840466SJianchao Wang return ELEVATOR_NO_MERGE; 80869840466SJianchao Wang } 80926308eabSJerome Marchand 810d6d48196SJens Axboe /* 811b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 812b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 813d6d48196SJens Axboe */ 814b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 815b973cb7eSJens Axboe struct request *req, struct request *next) 816d6d48196SJens Axboe { 817d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 818b973cb7eSJens Axboe return NULL; 819d6d48196SJens Axboe 820288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 821b973cb7eSJens Axboe return NULL; 822f31dc1cdSMartin K. Petersen 823d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 8242081a56bSJens Axboe || req->rq_disk != next->rq_disk) 825b973cb7eSJens Axboe return NULL; 826d6d48196SJens Axboe 8278fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 8284363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 829b973cb7eSJens Axboe return NULL; 8304363ac7cSMartin K. Petersen 831d6d48196SJens Axboe /* 832cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 833cb6934f8SJens Axboe * non-hint IO. 834cb6934f8SJens Axboe */ 835cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 836cb6934f8SJens Axboe return NULL; 837cb6934f8SJens Axboe 838668ffc03SDamien Le Moal if (req->ioprio != next->ioprio) 839668ffc03SDamien Le Moal return NULL; 840668ffc03SDamien Le Moal 841cb6934f8SJens Axboe /* 842d6d48196SJens Axboe * If we are allowed to merge, then append bio list 843d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 844d6d48196SJens Axboe * will have updated segment counts, update sector 845445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 846445251d0SJens Axboe * have separate settings. 847d6d48196SJens Axboe */ 84869840466SJianchao Wang 84969840466SJianchao Wang switch (blk_try_req_merge(req, next)) { 85069840466SJianchao Wang case ELEVATOR_DISCARD_MERGE: 851445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 852445251d0SJens Axboe return NULL; 85369840466SJianchao Wang break; 85469840466SJianchao Wang case ELEVATOR_BACK_MERGE: 85569840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next)) 856b973cb7eSJens Axboe return NULL; 85769840466SJianchao Wang break; 85869840466SJianchao Wang default: 85969840466SJianchao Wang return NULL; 86069840466SJianchao Wang } 861d6d48196SJens Axboe 862d6d48196SJens Axboe /* 86380a761fdSTejun Heo * If failfast settings disagree or any of the two is already 86480a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 86580a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 86680a761fdSTejun Heo * set properly. 86780a761fdSTejun Heo */ 868e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 86980a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 87080a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 87180a761fdSTejun Heo blk_rq_set_mixed_merge(req); 87280a761fdSTejun Heo blk_rq_set_mixed_merge(next); 87380a761fdSTejun Heo } 87480a761fdSTejun Heo 87580a761fdSTejun Heo /* 876522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 877522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 878522a7775SOmar Sandoval * current request for accounting purposes. 879d6d48196SJens Axboe */ 880522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 881522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 882d6d48196SJens Axboe 883d6d48196SJens Axboe req->biotail->bi_next = next->bio; 884d6d48196SJens Axboe req->biotail = next->biotail; 885d6d48196SJens Axboe 886a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 887d6d48196SJens Axboe 8882a5cf35cSMing Lei if (!blk_discard_mergable(req)) 889d6d48196SJens Axboe elv_merge_requests(q, req, next); 890d6d48196SJens Axboe 89142dad764SJerome Marchand /* 89242dad764SJerome Marchand * 'next' is going away, so update stats accordingly 89342dad764SJerome Marchand */ 89442dad764SJerome Marchand blk_account_io_merge(next); 895d6d48196SJens Axboe 896e4d750c9SJens Axboe /* 897e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 898e4d750c9SJens Axboe * the caller to free 899e4d750c9SJens Axboe */ 9001cd96c24SBoaz Harrosh next->bio = NULL; 901b973cb7eSJens Axboe return next; 902d6d48196SJens Axboe } 903d6d48196SJens Axboe 904b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq) 905d6d48196SJens Axboe { 906d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 907d6d48196SJens Axboe 908d6d48196SJens Axboe if (next) 909d6d48196SJens Axboe return attempt_merge(q, rq, next); 910d6d48196SJens Axboe 911b973cb7eSJens Axboe return NULL; 912d6d48196SJens Axboe } 913d6d48196SJens Axboe 914b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq) 915d6d48196SJens Axboe { 916d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 917d6d48196SJens Axboe 918d6d48196SJens Axboe if (prev) 919d6d48196SJens Axboe return attempt_merge(q, prev, rq); 920d6d48196SJens Axboe 921b973cb7eSJens Axboe return NULL; 922d6d48196SJens Axboe } 9235e84ea3aSJens Axboe 9245e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 9255e84ea3aSJens Axboe struct request *next) 9265e84ea3aSJens Axboe { 927e4d750c9SJens Axboe struct request *free; 92872ef799bSTahsin Erdogan 929e4d750c9SJens Axboe free = attempt_merge(q, rq, next); 930e4d750c9SJens Axboe if (free) { 93192bc5a24SJens Axboe blk_put_request(free); 932e4d750c9SJens Axboe return 1; 933e4d750c9SJens Axboe } 934e4d750c9SJens Axboe 935e4d750c9SJens Axboe return 0; 9365e84ea3aSJens Axboe } 937050c8ea8STejun Heo 938050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 939050c8ea8STejun Heo { 940e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 941050c8ea8STejun Heo return false; 942050c8ea8STejun Heo 943288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 944f31dc1cdSMartin K. Petersen return false; 945f31dc1cdSMartin K. Petersen 946050c8ea8STejun Heo /* different data direction or already started, don't merge */ 947050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 948050c8ea8STejun Heo return false; 949050c8ea8STejun Heo 9502081a56bSJens Axboe /* must be same device */ 9512081a56bSJens Axboe if (rq->rq_disk != bio->bi_disk) 952050c8ea8STejun Heo return false; 953050c8ea8STejun Heo 954050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 9554eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 956050c8ea8STejun Heo return false; 957050c8ea8STejun Heo 9584363ac7cSMartin K. Petersen /* must be using the same buffer */ 9598fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 9604363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 9614363ac7cSMartin K. Petersen return false; 9624363ac7cSMartin K. Petersen 963cb6934f8SJens Axboe /* 964cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 965cb6934f8SJens Axboe * non-hint IO. 966cb6934f8SJens Axboe */ 967cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 968cb6934f8SJens Axboe return false; 969cb6934f8SJens Axboe 970668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio)) 971668ffc03SDamien Le Moal return false; 972668ffc03SDamien Le Moal 973050c8ea8STejun Heo return true; 974050c8ea8STejun Heo } 975050c8ea8STejun Heo 97634fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 977050c8ea8STejun Heo { 97869840466SJianchao Wang if (blk_discard_mergable(rq)) 9791e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 9801e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 981050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 9824f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 983050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 984050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 985050c8ea8STejun Heo } 986