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, 183dcebd755SMing Lei unsigned *front_seg_size, unsigned *sectors) 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 */ 193dcebd755SMing Lei while (len && new_nsegs + *nsegs < queue_max_segments(q)) { 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); 24354efd50bSKent Overstreet 244dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 24554efd50bSKent Overstreet /* 24654efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 24754efd50bSKent Overstreet * offset would create a gap, disallow it. 24854efd50bSKent Overstreet */ 2495014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 25054efd50bSKent Overstreet goto split; 25154efd50bSKent Overstreet 252d0e5fbb0SMing Lei if (sectors + (bv.bv_len >> 9) > max_sectors) { 253e36f6204SKeith Busch /* 254e36f6204SKeith Busch * Consider this a new segment if we're splitting in 255e36f6204SKeith Busch * the middle of this vector. 256e36f6204SKeith Busch */ 257e36f6204SKeith Busch if (nsegs < queue_max_segments(q) && 258d0e5fbb0SMing Lei sectors < max_sectors) { 259dcebd755SMing Lei /* split in the middle of bvec */ 260dcebd755SMing Lei bv.bv_len = (max_sectors - sectors) << 9; 261dcebd755SMing Lei bvec_split_segs(q, &bv, &nsegs, 262dcebd755SMing Lei &seg_size, 263dcebd755SMing Lei &front_seg_size, 264dcebd755SMing Lei §ors); 265e36f6204SKeith Busch } 266e36f6204SKeith Busch goto split; 267e36f6204SKeith Busch } 268e36f6204SKeith Busch 26938417468SChristoph Hellwig if (bvprvp) { 270b4b6cb61SMing Lei if (seg_size + bv.bv_len > queue_max_segment_size(q)) 271b4b6cb61SMing Lei goto new_segment; 2723dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprvp, &bv)) 27354efd50bSKent Overstreet goto new_segment; 27454efd50bSKent Overstreet 27554efd50bSKent Overstreet seg_size += bv.bv_len; 27654efd50bSKent Overstreet bvprv = bv; 277578270bfSMing Lei bvprvp = &bvprv; 27852cc6eeaSMing Lei sectors += bv.bv_len >> 9; 279a88d32afSMing Lei 28054efd50bSKent Overstreet continue; 28154efd50bSKent Overstreet } 28254efd50bSKent Overstreet new_segment: 28354efd50bSKent Overstreet if (nsegs == queue_max_segments(q)) 28454efd50bSKent Overstreet goto split; 28554efd50bSKent Overstreet 28654efd50bSKent Overstreet bvprv = bv; 287578270bfSMing Lei bvprvp = &bvprv; 288dcebd755SMing Lei 289dcebd755SMing Lei if (bvec_split_segs(q, &bv, &nsegs, &seg_size, 290dcebd755SMing Lei &front_seg_size, §ors)) 291dcebd755SMing Lei goto split; 29202e70742SMing Lei 29354efd50bSKent Overstreet } 29454efd50bSKent Overstreet 29502e70742SMing Lei do_split = false; 29654efd50bSKent Overstreet split: 297bdced438SMing Lei *segs = nsegs; 29802e70742SMing Lei 29902e70742SMing Lei if (do_split) { 30002e70742SMing Lei new = bio_split(bio, sectors, GFP_NOIO, bs); 30102e70742SMing Lei if (new) 30202e70742SMing Lei bio = new; 30302e70742SMing Lei } 30402e70742SMing Lei 30502e70742SMing Lei bio->bi_seg_front_size = front_seg_size; 30602e70742SMing Lei if (seg_size > bio->bi_seg_back_size) 30702e70742SMing Lei bio->bi_seg_back_size = seg_size; 30802e70742SMing Lei 30902e70742SMing Lei return do_split ? new : NULL; 31054efd50bSKent Overstreet } 31154efd50bSKent Overstreet 312af67c31fSNeilBrown void blk_queue_split(struct request_queue *q, struct bio **bio) 31354efd50bSKent Overstreet { 314bdced438SMing Lei struct bio *split, *res; 315bdced438SMing Lei unsigned nsegs; 31654efd50bSKent Overstreet 3177afafc8aSAdrian Hunter switch (bio_op(*bio)) { 3187afafc8aSAdrian Hunter case REQ_OP_DISCARD: 3197afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 320338aa96dSKent Overstreet split = blk_bio_discard_split(q, *bio, &q->bio_split, &nsegs); 3217afafc8aSAdrian Hunter break; 322a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 323338aa96dSKent Overstreet split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, &nsegs); 324a6f0788eSChaitanya Kulkarni break; 3257afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 326338aa96dSKent Overstreet split = blk_bio_write_same_split(q, *bio, &q->bio_split, &nsegs); 3277afafc8aSAdrian Hunter break; 3287afafc8aSAdrian Hunter default: 329338aa96dSKent Overstreet split = blk_bio_segment_split(q, *bio, &q->bio_split, &nsegs); 3307afafc8aSAdrian Hunter break; 3317afafc8aSAdrian Hunter } 332bdced438SMing Lei 333bdced438SMing Lei /* physical segments can be figured out during splitting */ 334bdced438SMing Lei res = split ? split : *bio; 335bdced438SMing Lei res->bi_phys_segments = nsegs; 336bdced438SMing Lei bio_set_flag(res, BIO_SEG_VALID); 33754efd50bSKent Overstreet 33854efd50bSKent Overstreet if (split) { 3396ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 3401eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 3416ac45aebSMing Lei 342947b7ac1SJens Axboe /* 343947b7ac1SJens Axboe * Since we're recursing into make_request here, ensure 344947b7ac1SJens Axboe * that we mark this bio as already having entered the queue. 345947b7ac1SJens Axboe * If not, and the queue is going away, we can get stuck 346947b7ac1SJens Axboe * forever on waiting for the queue reference to drop. But 347947b7ac1SJens Axboe * that will never happen, as we're already holding a 348947b7ac1SJens Axboe * reference to it. 349947b7ac1SJens Axboe */ 350947b7ac1SJens Axboe bio_set_flag(*bio, BIO_QUEUE_ENTERED); 351947b7ac1SJens Axboe 35254efd50bSKent Overstreet bio_chain(split, *bio); 353cda22646SMike Krinkin trace_block_split(q, split, (*bio)->bi_iter.bi_sector); 35454efd50bSKent Overstreet generic_make_request(*bio); 35554efd50bSKent Overstreet *bio = split; 35654efd50bSKent Overstreet } 35754efd50bSKent Overstreet } 35854efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 35954efd50bSKent Overstreet 3601e428079SJens Axboe static unsigned int __blk_recalc_rq_segments(struct request_queue *q, 361*2705c937SMing Lei struct bio *bio) 362d6d48196SJens Axboe { 3637988613bSKent Overstreet struct bio_vec bv, bvprv = { NULL }; 36438417468SChristoph Hellwig int prev = 0; 3651e428079SJens Axboe unsigned int seg_size, nr_phys_segs; 366dcebd755SMing Lei unsigned front_seg_size = bio->bi_seg_front_size; 36759247eaeSJens Axboe struct bio *fbio, *bbio; 3687988613bSKent Overstreet struct bvec_iter iter; 369d6d48196SJens Axboe 3701e428079SJens Axboe if (!bio) 3711e428079SJens Axboe return 0; 372d6d48196SJens Axboe 373a6f0788eSChaitanya Kulkarni switch (bio_op(bio)) { 374a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 375a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 376a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 377f9d03f96SChristoph Hellwig return 0; 378f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 3795cb8850cSKent Overstreet return 1; 380a6f0788eSChaitanya Kulkarni } 3815cb8850cSKent Overstreet 3821e428079SJens Axboe fbio = bio; 3835df97b91SMikulas Patocka seg_size = 0; 3842c8919deSAndi Kleen nr_phys_segs = 0; 3851e428079SJens Axboe for_each_bio(bio) { 386dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 38738417468SChristoph Hellwig if (prev) { 3887988613bSKent Overstreet if (seg_size + bv.bv_len 389ae03bf63SMartin K. Petersen > queue_max_segment_size(q)) 390d6d48196SJens Axboe goto new_segment; 3913dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, &bvprv, &bv)) 392d6d48196SJens Axboe goto new_segment; 393d6d48196SJens Axboe 3947988613bSKent Overstreet seg_size += bv.bv_len; 395d6d48196SJens Axboe bvprv = bv; 396d6d48196SJens Axboe continue; 397d6d48196SJens Axboe } 398d6d48196SJens Axboe new_segment: 399d6d48196SJens Axboe bvprv = bv; 40054efd50bSKent Overstreet prev = 1; 401dcebd755SMing Lei bvec_split_segs(q, &bv, &nr_phys_segs, &seg_size, 402dcebd755SMing Lei &front_seg_size, NULL); 403d6d48196SJens Axboe } 40459247eaeSJens Axboe bbio = bio; 4051e428079SJens Axboe } 406d6d48196SJens Axboe 407dcebd755SMing Lei fbio->bi_seg_front_size = front_seg_size; 40859247eaeSJens Axboe if (seg_size > bbio->bi_seg_back_size) 40959247eaeSJens Axboe bbio->bi_seg_back_size = seg_size; 4101e428079SJens Axboe 4111e428079SJens Axboe return nr_phys_segs; 4121e428079SJens Axboe } 4131e428079SJens Axboe 4141e428079SJens Axboe void blk_recalc_rq_segments(struct request *rq) 4151e428079SJens Axboe { 416*2705c937SMing Lei rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio); 417d6d48196SJens Axboe } 418d6d48196SJens Axboe 419d6d48196SJens Axboe void blk_recount_segments(struct request_queue *q, struct bio *bio) 420d6d48196SJens Axboe { 421d6d48196SJens Axboe struct bio *nxt = bio->bi_next; 4221e428079SJens Axboe 423d6d48196SJens Axboe bio->bi_next = NULL; 424*2705c937SMing Lei bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio); 425d6d48196SJens Axboe bio->bi_next = nxt; 42605f1dd53SJens Axboe 427b7c44ed9SJens Axboe bio_set_flag(bio, BIO_SEG_VALID); 428d6d48196SJens Axboe } 429d6d48196SJens Axboe 430d6d48196SJens Axboe static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, 431d6d48196SJens Axboe struct bio *nxt) 432d6d48196SJens Axboe { 4332b8221e1SKent Overstreet struct bio_vec end_bv = { NULL }, nxt_bv; 434f619d254SKent Overstreet 43586771427SFUJITA Tomonori if (bio->bi_seg_back_size + nxt->bi_seg_front_size > 436ae03bf63SMartin K. Petersen queue_max_segment_size(q)) 437d6d48196SJens Axboe return 0; 438d6d48196SJens Axboe 439e17fc0a1SDavid Woodhouse if (!bio_has_data(bio)) 440e17fc0a1SDavid Woodhouse return 1; 441e17fc0a1SDavid Woodhouse 442e827091cSMing Lei bio_get_last_bvec(bio, &end_bv); 443e827091cSMing Lei bio_get_first_bvec(nxt, &nxt_bv); 444f619d254SKent Overstreet 4453dccdae5SChristoph Hellwig return biovec_phys_mergeable(q, &end_bv, &nxt_bv); 446d6d48196SJens Axboe } 447d6d48196SJens Axboe 448862e5a5eSMing Lei static struct scatterlist *blk_next_sg(struct scatterlist **sg, 449862e5a5eSMing Lei struct scatterlist *sglist) 450862e5a5eSMing Lei { 451862e5a5eSMing Lei if (!*sg) 452862e5a5eSMing Lei return sglist; 453862e5a5eSMing Lei 454862e5a5eSMing Lei /* 455862e5a5eSMing Lei * If the driver previously mapped a shorter list, we could see a 456862e5a5eSMing Lei * termination bit prematurely unless it fully inits the sg table 457862e5a5eSMing Lei * on each mapping. We KNOW that there must be more entries here 458862e5a5eSMing Lei * or the driver would be buggy, so force clear the termination bit 459862e5a5eSMing Lei * to avoid doing a full sg_init_table() in drivers for each command. 460862e5a5eSMing Lei */ 461862e5a5eSMing Lei sg_unmark_end(*sg); 462862e5a5eSMing Lei return sg_next(*sg); 463862e5a5eSMing Lei } 464862e5a5eSMing Lei 465862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q, 466862e5a5eSMing Lei struct bio_vec *bvec, struct scatterlist *sglist, 467862e5a5eSMing Lei struct scatterlist **sg) 468862e5a5eSMing Lei { 469862e5a5eSMing Lei unsigned nbytes = bvec->bv_len; 470862e5a5eSMing Lei unsigned nsegs = 0, total = 0, offset = 0; 471862e5a5eSMing Lei 472862e5a5eSMing Lei while (nbytes > 0) { 473862e5a5eSMing Lei unsigned seg_size; 474862e5a5eSMing Lei struct page *pg; 475862e5a5eSMing Lei unsigned idx; 476862e5a5eSMing Lei 477862e5a5eSMing Lei *sg = blk_next_sg(sg, sglist); 478862e5a5eSMing Lei 479862e5a5eSMing Lei seg_size = get_max_segment_size(q, bvec->bv_offset + total); 480862e5a5eSMing Lei seg_size = min(nbytes, seg_size); 481862e5a5eSMing Lei 482862e5a5eSMing Lei offset = (total + bvec->bv_offset) % PAGE_SIZE; 483862e5a5eSMing Lei idx = (total + bvec->bv_offset) / PAGE_SIZE; 484862e5a5eSMing Lei pg = nth_page(bvec->bv_page, idx); 485862e5a5eSMing Lei 486862e5a5eSMing Lei sg_set_page(*sg, pg, seg_size, offset); 487862e5a5eSMing Lei 488862e5a5eSMing Lei total += seg_size; 489862e5a5eSMing Lei nbytes -= seg_size; 490862e5a5eSMing Lei nsegs++; 491862e5a5eSMing Lei } 492862e5a5eSMing Lei 493862e5a5eSMing Lei return nsegs; 494862e5a5eSMing Lei } 495862e5a5eSMing Lei 4967988613bSKent Overstreet static inline void 497963ab9e5SAsias He __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, 4987988613bSKent Overstreet struct scatterlist *sglist, struct bio_vec *bvprv, 49938417468SChristoph Hellwig struct scatterlist **sg, int *nsegs) 500963ab9e5SAsias He { 501963ab9e5SAsias He 502963ab9e5SAsias He int nbytes = bvec->bv_len; 503963ab9e5SAsias He 50438417468SChristoph Hellwig if (*sg) { 505b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 506b4b6cb61SMing Lei goto new_segment; 5073dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec)) 508963ab9e5SAsias He goto new_segment; 509963ab9e5SAsias He 510963ab9e5SAsias He (*sg)->length += nbytes; 511963ab9e5SAsias He } else { 512963ab9e5SAsias He new_segment: 513862e5a5eSMing Lei (*nsegs) += blk_bvec_map_sg(q, bvec, sglist, sg); 514963ab9e5SAsias He } 5157988613bSKent Overstreet *bvprv = *bvec; 516963ab9e5SAsias He } 517963ab9e5SAsias He 518f9d03f96SChristoph Hellwig static inline int __blk_bvec_map_sg(struct request_queue *q, struct bio_vec bv, 519f9d03f96SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **sg) 520f9d03f96SChristoph Hellwig { 521f9d03f96SChristoph Hellwig *sg = sglist; 522f9d03f96SChristoph Hellwig sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 523f9d03f96SChristoph Hellwig return 1; 524f9d03f96SChristoph Hellwig } 525f9d03f96SChristoph Hellwig 5265cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 5275cb8850cSKent Overstreet struct scatterlist *sglist, 5285cb8850cSKent Overstreet struct scatterlist **sg) 5295cb8850cSKent Overstreet { 5305cb8850cSKent Overstreet struct bio_vec bvec, bvprv = { NULL }; 5315cb8850cSKent Overstreet struct bvec_iter iter; 53238417468SChristoph Hellwig int nsegs = 0; 5335cb8850cSKent Overstreet 5345cb8850cSKent Overstreet for_each_bio(bio) 535862e5a5eSMing Lei bio_for_each_bvec(bvec, bio, iter) 5365cb8850cSKent Overstreet __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, 53738417468SChristoph Hellwig &nsegs); 5385cb8850cSKent Overstreet 5395cb8850cSKent Overstreet return nsegs; 5405cb8850cSKent Overstreet } 5415cb8850cSKent Overstreet 542d6d48196SJens Axboe /* 543d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 544d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 545d6d48196SJens Axboe */ 546d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 547d6d48196SJens Axboe struct scatterlist *sglist) 548d6d48196SJens Axboe { 5495cb8850cSKent Overstreet struct scatterlist *sg = NULL; 5505cb8850cSKent Overstreet int nsegs = 0; 551d6d48196SJens Axboe 552f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 553f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, rq->special_vec, sglist, &sg); 554f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 555f9d03f96SChristoph Hellwig nsegs = __blk_bvec_map_sg(q, bio_iovec(rq->bio), sglist, &sg); 556f9d03f96SChristoph Hellwig else if (rq->bio) 5575cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 558f18573abSFUJITA Tomonori 559e8064021SChristoph Hellwig if (unlikely(rq->rq_flags & RQF_COPY_USER) && 5602e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 5612e46e8b2STejun Heo unsigned int pad_len = 5622e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 563f18573abSFUJITA Tomonori 564f18573abSFUJITA Tomonori sg->length += pad_len; 565f18573abSFUJITA Tomonori rq->extra_len += pad_len; 566f18573abSFUJITA Tomonori } 567f18573abSFUJITA Tomonori 5682fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 569a8ebb056SMike Christie if (op_is_write(req_op(rq))) 570db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 571db0a2e00STejun Heo 572da81ed16SDan Williams sg_unmark_end(sg); 573d6d48196SJens Axboe sg = sg_next(sg); 574d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 575d6d48196SJens Axboe q->dma_drain_size, 576d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 577d6d48196SJens Axboe (PAGE_SIZE - 1)); 578d6d48196SJens Axboe nsegs++; 5797a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 580d6d48196SJens Axboe } 581d6d48196SJens Axboe 582d6d48196SJens Axboe if (sg) 583d6d48196SJens Axboe sg_mark_end(sg); 584d6d48196SJens Axboe 58512e57f59SMing Lei /* 58612e57f59SMing Lei * Something must have been wrong if the figured number of 58712e57f59SMing Lei * segment is bigger than number of req's physical segments 58812e57f59SMing Lei */ 589f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 59012e57f59SMing Lei 591d6d48196SJens Axboe return nsegs; 592d6d48196SJens Axboe } 593d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 594d6d48196SJens Axboe 595d6d48196SJens Axboe static inline int ll_new_hw_segment(struct request_queue *q, 596d6d48196SJens Axboe struct request *req, 597d6d48196SJens Axboe struct bio *bio) 598d6d48196SJens Axboe { 599d6d48196SJens Axboe int nr_phys_segs = bio_phys_segments(q, bio); 600d6d48196SJens Axboe 60113f05c8dSMartin K. Petersen if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) 60213f05c8dSMartin K. Petersen goto no_merge; 60313f05c8dSMartin K. Petersen 6044eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(q, req, bio) == false) 60513f05c8dSMartin K. Petersen goto no_merge; 606d6d48196SJens Axboe 607d6d48196SJens Axboe /* 608d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 609d6d48196SJens Axboe * counters. 610d6d48196SJens Axboe */ 611d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 612d6d48196SJens Axboe return 1; 61313f05c8dSMartin K. Petersen 61413f05c8dSMartin K. Petersen no_merge: 615e0c72300SRitesh Harjani req_set_nomerge(q, req); 61613f05c8dSMartin K. Petersen return 0; 617d6d48196SJens Axboe } 618d6d48196SJens Axboe 619d6d48196SJens Axboe int ll_back_merge_fn(struct request_queue *q, struct request *req, 620d6d48196SJens Axboe struct bio *bio) 621d6d48196SJens Axboe { 6225e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 6235e7c4274SJens Axboe return 0; 6247f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6257f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 6267f39add3SSagi Grimberg return 0; 627f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 62817007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 629e0c72300SRitesh Harjani req_set_nomerge(q, req); 630d6d48196SJens Axboe return 0; 631d6d48196SJens Axboe } 6322cdf79caSJens Axboe if (!bio_flagged(req->biotail, BIO_SEG_VALID)) 633d6d48196SJens Axboe blk_recount_segments(q, req->biotail); 6342cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 635d6d48196SJens Axboe blk_recount_segments(q, bio); 636d6d48196SJens Axboe 637d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 638d6d48196SJens Axboe } 639d6d48196SJens Axboe 640d6d48196SJens Axboe int ll_front_merge_fn(struct request_queue *q, struct request *req, 641d6d48196SJens Axboe struct bio *bio) 642d6d48196SJens Axboe { 6435e7c4274SJens Axboe 6445e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 6455e7c4274SJens Axboe return 0; 6467f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6477f39add3SSagi Grimberg integrity_req_gap_front_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, bio->bi_iter.bi_sector)) { 651e0c72300SRitesh Harjani req_set_nomerge(q, req); 652d6d48196SJens Axboe return 0; 653d6d48196SJens Axboe } 6542cdf79caSJens Axboe if (!bio_flagged(bio, BIO_SEG_VALID)) 655d6d48196SJens Axboe blk_recount_segments(q, bio); 6562cdf79caSJens Axboe if (!bio_flagged(req->bio, BIO_SEG_VALID)) 657d6d48196SJens Axboe blk_recount_segments(q, req->bio); 658d6d48196SJens Axboe 659d6d48196SJens Axboe return ll_new_hw_segment(q, req, bio); 660d6d48196SJens Axboe } 661d6d48196SJens Axboe 662445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 663445251d0SJens Axboe struct request *next) 664445251d0SJens Axboe { 665445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 666445251d0SJens Axboe 667445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 668445251d0SJens Axboe goto no_merge; 669445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 670445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 671445251d0SJens Axboe goto no_merge; 672445251d0SJens Axboe 673445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 674445251d0SJens Axboe return true; 675445251d0SJens Axboe no_merge: 676445251d0SJens Axboe req_set_nomerge(q, req); 677445251d0SJens Axboe return false; 678445251d0SJens Axboe } 679445251d0SJens Axboe 680d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 681d6d48196SJens Axboe struct request *next) 682d6d48196SJens Axboe { 683d6d48196SJens Axboe int total_phys_segments; 68486771427SFUJITA Tomonori unsigned int seg_size = 68586771427SFUJITA Tomonori req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; 686d6d48196SJens Axboe 6875e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 688854fbb9cSKeith Busch return 0; 689854fbb9cSKeith Busch 690d6d48196SJens Axboe /* 691d6d48196SJens Axboe * Will it become too large? 692d6d48196SJens Axboe */ 693f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 69417007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 695d6d48196SJens Axboe return 0; 696d6d48196SJens Axboe 697d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 69886771427SFUJITA Tomonori if (blk_phys_contig_segment(q, req->biotail, next->bio)) { 69986771427SFUJITA Tomonori if (req->nr_phys_segments == 1) 70086771427SFUJITA Tomonori req->bio->bi_seg_front_size = seg_size; 70186771427SFUJITA Tomonori if (next->nr_phys_segments == 1) 70286771427SFUJITA Tomonori next->biotail->bi_seg_back_size = seg_size; 703d6d48196SJens Axboe total_phys_segments--; 70486771427SFUJITA Tomonori } 705d6d48196SJens Axboe 7068a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 707d6d48196SJens Axboe return 0; 708d6d48196SJens Axboe 7094eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 71013f05c8dSMartin K. Petersen return 0; 71113f05c8dSMartin K. Petersen 712d6d48196SJens Axboe /* Merge is OK... */ 713d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 714d6d48196SJens Axboe return 1; 715d6d48196SJens Axboe } 716d6d48196SJens Axboe 71780a761fdSTejun Heo /** 71880a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 71980a761fdSTejun Heo * @rq: request to mark as mixed merge 72080a761fdSTejun Heo * 72180a761fdSTejun Heo * Description: 72280a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 72380a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 72480a761fdSTejun Heo * merged. 72580a761fdSTejun Heo */ 72680a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 72780a761fdSTejun Heo { 72880a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 72980a761fdSTejun Heo struct bio *bio; 73080a761fdSTejun Heo 731e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 73280a761fdSTejun Heo return; 73380a761fdSTejun Heo 73480a761fdSTejun Heo /* 73580a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 73680a761fdSTejun Heo * contained bios. It will just track those of the first one. 73780a761fdSTejun Heo * Distributes the attributs to each bio. 73880a761fdSTejun Heo */ 73980a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 7401eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 7411eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 7421eff9d32SJens Axboe bio->bi_opf |= ff; 74380a761fdSTejun Heo } 744e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 74580a761fdSTejun Heo } 74680a761fdSTejun Heo 74726308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 74826308eabSJerome Marchand { 74926308eabSJerome Marchand if (blk_do_io_stat(req)) { 75026308eabSJerome Marchand struct hd_struct *part; 75126308eabSJerome Marchand 752112f158fSMike Snitzer part_stat_lock(); 75309e099d4SJerome Marchand part = req->part; 75426308eabSJerome Marchand 755d62e26b3SJens Axboe part_dec_in_flight(req->q, part, rq_data_dir(req)); 75626308eabSJerome Marchand 7576c23a968SJens Axboe hd_struct_put(part); 75826308eabSJerome Marchand part_stat_unlock(); 75926308eabSJerome Marchand } 76026308eabSJerome Marchand } 76169840466SJianchao Wang /* 76269840466SJianchao Wang * Two cases of handling DISCARD merge: 76369840466SJianchao Wang * If max_discard_segments > 1, the driver takes every bio 76469840466SJianchao Wang * as a range and send them to controller together. The ranges 76569840466SJianchao Wang * needn't to be contiguous. 76669840466SJianchao Wang * Otherwise, the bios/requests will be handled as same as 76769840466SJianchao Wang * others which should be contiguous. 76869840466SJianchao Wang */ 76969840466SJianchao Wang static inline bool blk_discard_mergable(struct request *req) 77069840466SJianchao Wang { 77169840466SJianchao Wang if (req_op(req) == REQ_OP_DISCARD && 77269840466SJianchao Wang queue_max_discard_segments(req->q) > 1) 77369840466SJianchao Wang return true; 77469840466SJianchao Wang return false; 77569840466SJianchao Wang } 77669840466SJianchao Wang 777e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req, 778e96c0d83SEric Biggers struct request *next) 77969840466SJianchao Wang { 78069840466SJianchao Wang if (blk_discard_mergable(req)) 78169840466SJianchao Wang return ELEVATOR_DISCARD_MERGE; 78269840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) 78369840466SJianchao Wang return ELEVATOR_BACK_MERGE; 78469840466SJianchao Wang 78569840466SJianchao Wang return ELEVATOR_NO_MERGE; 78669840466SJianchao Wang } 78726308eabSJerome Marchand 788d6d48196SJens Axboe /* 789b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 790b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 791d6d48196SJens Axboe */ 792b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 793b973cb7eSJens Axboe struct request *req, struct request *next) 794d6d48196SJens Axboe { 795d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 796b973cb7eSJens Axboe return NULL; 797d6d48196SJens Axboe 798288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 799b973cb7eSJens Axboe return NULL; 800f31dc1cdSMartin K. Petersen 801d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 8022081a56bSJens Axboe || req->rq_disk != next->rq_disk) 803b973cb7eSJens Axboe return NULL; 804d6d48196SJens Axboe 8058fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 8064363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 807b973cb7eSJens Axboe return NULL; 8084363ac7cSMartin K. Petersen 809d6d48196SJens Axboe /* 810cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 811cb6934f8SJens Axboe * non-hint IO. 812cb6934f8SJens Axboe */ 813cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 814cb6934f8SJens Axboe return NULL; 815cb6934f8SJens Axboe 816668ffc03SDamien Le Moal if (req->ioprio != next->ioprio) 817668ffc03SDamien Le Moal return NULL; 818668ffc03SDamien Le Moal 819cb6934f8SJens Axboe /* 820d6d48196SJens Axboe * If we are allowed to merge, then append bio list 821d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 822d6d48196SJens Axboe * will have updated segment counts, update sector 823445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 824445251d0SJens Axboe * have separate settings. 825d6d48196SJens Axboe */ 82669840466SJianchao Wang 82769840466SJianchao Wang switch (blk_try_req_merge(req, next)) { 82869840466SJianchao Wang case ELEVATOR_DISCARD_MERGE: 829445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 830445251d0SJens Axboe return NULL; 83169840466SJianchao Wang break; 83269840466SJianchao Wang case ELEVATOR_BACK_MERGE: 83369840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next)) 834b973cb7eSJens Axboe return NULL; 83569840466SJianchao Wang break; 83669840466SJianchao Wang default: 83769840466SJianchao Wang return NULL; 83869840466SJianchao Wang } 839d6d48196SJens Axboe 840d6d48196SJens Axboe /* 84180a761fdSTejun Heo * If failfast settings disagree or any of the two is already 84280a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 84380a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 84480a761fdSTejun Heo * set properly. 84580a761fdSTejun Heo */ 846e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 84780a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 84880a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 84980a761fdSTejun Heo blk_rq_set_mixed_merge(req); 85080a761fdSTejun Heo blk_rq_set_mixed_merge(next); 85180a761fdSTejun Heo } 85280a761fdSTejun Heo 85380a761fdSTejun Heo /* 854522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 855522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 856522a7775SOmar Sandoval * current request for accounting purposes. 857d6d48196SJens Axboe */ 858522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 859522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 860d6d48196SJens Axboe 861d6d48196SJens Axboe req->biotail->bi_next = next->bio; 862d6d48196SJens Axboe req->biotail = next->biotail; 863d6d48196SJens Axboe 864a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 865d6d48196SJens Axboe 8662a5cf35cSMing Lei if (!blk_discard_mergable(req)) 867d6d48196SJens Axboe elv_merge_requests(q, req, next); 868d6d48196SJens Axboe 86942dad764SJerome Marchand /* 87042dad764SJerome Marchand * 'next' is going away, so update stats accordingly 87142dad764SJerome Marchand */ 87242dad764SJerome Marchand blk_account_io_merge(next); 873d6d48196SJens Axboe 874e4d750c9SJens Axboe /* 875e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 876e4d750c9SJens Axboe * the caller to free 877e4d750c9SJens Axboe */ 8781cd96c24SBoaz Harrosh next->bio = NULL; 879b973cb7eSJens Axboe return next; 880d6d48196SJens Axboe } 881d6d48196SJens Axboe 882b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq) 883d6d48196SJens Axboe { 884d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 885d6d48196SJens Axboe 886d6d48196SJens Axboe if (next) 887d6d48196SJens Axboe return attempt_merge(q, rq, next); 888d6d48196SJens Axboe 889b973cb7eSJens Axboe return NULL; 890d6d48196SJens Axboe } 891d6d48196SJens Axboe 892b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq) 893d6d48196SJens Axboe { 894d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 895d6d48196SJens Axboe 896d6d48196SJens Axboe if (prev) 897d6d48196SJens Axboe return attempt_merge(q, prev, rq); 898d6d48196SJens Axboe 899b973cb7eSJens Axboe return NULL; 900d6d48196SJens Axboe } 9015e84ea3aSJens Axboe 9025e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 9035e84ea3aSJens Axboe struct request *next) 9045e84ea3aSJens Axboe { 905e4d750c9SJens Axboe struct request *free; 90672ef799bSTahsin Erdogan 907e4d750c9SJens Axboe free = attempt_merge(q, rq, next); 908e4d750c9SJens Axboe if (free) { 90992bc5a24SJens Axboe blk_put_request(free); 910e4d750c9SJens Axboe return 1; 911e4d750c9SJens Axboe } 912e4d750c9SJens Axboe 913e4d750c9SJens Axboe return 0; 9145e84ea3aSJens Axboe } 915050c8ea8STejun Heo 916050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 917050c8ea8STejun Heo { 918e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 919050c8ea8STejun Heo return false; 920050c8ea8STejun Heo 921288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 922f31dc1cdSMartin K. Petersen return false; 923f31dc1cdSMartin K. Petersen 924050c8ea8STejun Heo /* different data direction or already started, don't merge */ 925050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 926050c8ea8STejun Heo return false; 927050c8ea8STejun Heo 9282081a56bSJens Axboe /* must be same device */ 9292081a56bSJens Axboe if (rq->rq_disk != bio->bi_disk) 930050c8ea8STejun Heo return false; 931050c8ea8STejun Heo 932050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 9334eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 934050c8ea8STejun Heo return false; 935050c8ea8STejun Heo 9364363ac7cSMartin K. Petersen /* must be using the same buffer */ 9378fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 9384363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 9394363ac7cSMartin K. Petersen return false; 9404363ac7cSMartin K. Petersen 941cb6934f8SJens Axboe /* 942cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 943cb6934f8SJens Axboe * non-hint IO. 944cb6934f8SJens Axboe */ 945cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 946cb6934f8SJens Axboe return false; 947cb6934f8SJens Axboe 948668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio)) 949668ffc03SDamien Le Moal return false; 950668ffc03SDamien Le Moal 951050c8ea8STejun Heo return true; 952050c8ea8STejun Heo } 953050c8ea8STejun Heo 95434fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 955050c8ea8STejun Heo { 95669840466SJianchao Wang if (blk_discard_mergable(rq)) 9571e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 9581e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 959050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 9604f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 961050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 962050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 963050c8ea8STejun Heo } 964