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 static inline bool bio_will_gap(struct request_queue *q, 16e9907009SChristoph Hellwig struct request *prev_rq, struct bio *prev, struct bio *next) 17e9907009SChristoph Hellwig { 18e9907009SChristoph Hellwig struct bio_vec pb, nb; 19e9907009SChristoph Hellwig 20e9907009SChristoph Hellwig if (!bio_has_data(prev) || !queue_virt_boundary(q)) 21e9907009SChristoph Hellwig return false; 22e9907009SChristoph Hellwig 23e9907009SChristoph Hellwig /* 24e9907009SChristoph Hellwig * Don't merge if the 1st bio starts with non-zero offset, otherwise it 25e9907009SChristoph Hellwig * is quite difficult to respect the sg gap limit. We work hard to 26e9907009SChristoph Hellwig * merge a huge number of small single bios in case of mkfs. 27e9907009SChristoph Hellwig */ 28e9907009SChristoph Hellwig if (prev_rq) 29e9907009SChristoph Hellwig bio_get_first_bvec(prev_rq->bio, &pb); 30e9907009SChristoph Hellwig else 31e9907009SChristoph Hellwig bio_get_first_bvec(prev, &pb); 32df376b2eSJohannes Thumshirn if (pb.bv_offset & queue_virt_boundary(q)) 33e9907009SChristoph Hellwig return true; 34e9907009SChristoph Hellwig 35e9907009SChristoph Hellwig /* 36e9907009SChristoph Hellwig * We don't need to worry about the situation that the merged segment 37e9907009SChristoph Hellwig * ends in unaligned virt boundary: 38e9907009SChristoph Hellwig * 39e9907009SChristoph Hellwig * - if 'pb' ends aligned, the merged segment ends aligned 40e9907009SChristoph Hellwig * - if 'pb' ends unaligned, the next bio must include 41e9907009SChristoph Hellwig * one single bvec of 'nb', otherwise the 'nb' can't 42e9907009SChristoph Hellwig * merge with 'pb' 43e9907009SChristoph Hellwig */ 44e9907009SChristoph Hellwig bio_get_last_bvec(prev, &pb); 45e9907009SChristoph Hellwig bio_get_first_bvec(next, &nb); 46200a9affSChristoph Hellwig if (biovec_phys_mergeable(q, &pb, &nb)) 47e9907009SChristoph Hellwig return false; 48e9907009SChristoph Hellwig return __bvec_gap_to_prev(q, &pb, nb.bv_offset); 49e9907009SChristoph Hellwig } 50e9907009SChristoph Hellwig 51e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio) 52e9907009SChristoph Hellwig { 53e9907009SChristoph Hellwig return bio_will_gap(req->q, req, req->biotail, bio); 54e9907009SChristoph Hellwig } 55e9907009SChristoph Hellwig 56e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio) 57e9907009SChristoph Hellwig { 58e9907009SChristoph Hellwig return bio_will_gap(req->q, NULL, bio, req->bio); 59e9907009SChristoph Hellwig } 60e9907009SChristoph Hellwig 6154efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 6254efd50bSKent Overstreet struct bio *bio, 63bdced438SMing Lei struct bio_set *bs, 64bdced438SMing Lei unsigned *nsegs) 6554efd50bSKent Overstreet { 6654efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 6754efd50bSKent Overstreet int alignment; 6854efd50bSKent Overstreet sector_t tmp; 6954efd50bSKent Overstreet unsigned split_sectors; 7054efd50bSKent Overstreet 71bdced438SMing Lei *nsegs = 1; 72bdced438SMing Lei 7354efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 7454efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 7554efd50bSKent Overstreet 761adfc5e4SMing Lei max_discard_sectors = min(q->limits.max_discard_sectors, 771adfc5e4SMing Lei bio_allowed_max_sectors(q)); 7854efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 7954efd50bSKent Overstreet 8054efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 8154efd50bSKent Overstreet /* XXX: warn */ 8254efd50bSKent Overstreet return NULL; 8354efd50bSKent Overstreet } 8454efd50bSKent Overstreet 8554efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 8654efd50bSKent Overstreet return NULL; 8754efd50bSKent Overstreet 8854efd50bSKent Overstreet split_sectors = max_discard_sectors; 8954efd50bSKent Overstreet 9054efd50bSKent Overstreet /* 9154efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 9254efd50bSKent Overstreet * the previous aligned sector. 9354efd50bSKent Overstreet */ 9454efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 9554efd50bSKent Overstreet 9654efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 9754efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 9854efd50bSKent Overstreet 9954efd50bSKent Overstreet if (split_sectors > tmp) 10054efd50bSKent Overstreet split_sectors -= tmp; 10154efd50bSKent Overstreet 10254efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 10354efd50bSKent Overstreet } 10454efd50bSKent Overstreet 105885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, 106885fa13fSChristoph Hellwig struct bio *bio, struct bio_set *bs, unsigned *nsegs) 107885fa13fSChristoph Hellwig { 108d665e12aSChristoph Hellwig *nsegs = 0; 109885fa13fSChristoph Hellwig 110885fa13fSChristoph Hellwig if (!q->limits.max_write_zeroes_sectors) 111885fa13fSChristoph Hellwig return NULL; 112885fa13fSChristoph Hellwig 113885fa13fSChristoph Hellwig if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) 114885fa13fSChristoph Hellwig return NULL; 115885fa13fSChristoph Hellwig 116885fa13fSChristoph Hellwig return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); 117885fa13fSChristoph Hellwig } 118885fa13fSChristoph Hellwig 11954efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 12054efd50bSKent Overstreet struct bio *bio, 121bdced438SMing Lei struct bio_set *bs, 122bdced438SMing Lei unsigned *nsegs) 12354efd50bSKent Overstreet { 124bdced438SMing Lei *nsegs = 1; 125bdced438SMing Lei 12654efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 12754efd50bSKent Overstreet return NULL; 12854efd50bSKent Overstreet 12954efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 13054efd50bSKent Overstreet return NULL; 13154efd50bSKent Overstreet 13254efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 13354efd50bSKent Overstreet } 13454efd50bSKent Overstreet 1359cc5169cSBart Van Assche /* 1369cc5169cSBart Van Assche * Return the maximum number of sectors from the start of a bio that may be 1379cc5169cSBart Van Assche * submitted as a single request to a block device. If enough sectors remain, 1389cc5169cSBart Van Assche * align the end to the physical block size. Otherwise align the end to the 1399cc5169cSBart Van Assche * logical block size. This approach minimizes the number of non-aligned 1409cc5169cSBart Van Assche * requests that are submitted to a block device if the start of a bio is not 1419cc5169cSBart Van Assche * aligned to a physical block boundary. 1429cc5169cSBart Van Assche */ 143d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 144d0e5fbb0SMing Lei struct bio *bio) 145d0e5fbb0SMing Lei { 146d0e5fbb0SMing Lei unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector); 1479cc5169cSBart Van Assche unsigned max_sectors = sectors; 1489cc5169cSBart Van Assche unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT; 1499cc5169cSBart Van Assche unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT; 1509cc5169cSBart Van Assche unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1); 151d0e5fbb0SMing Lei 1529cc5169cSBart Van Assche max_sectors += start_offset; 1539cc5169cSBart Van Assche max_sectors &= ~(pbs - 1); 1549cc5169cSBart Van Assche if (max_sectors > start_offset) 1559cc5169cSBart Van Assche return max_sectors - start_offset; 156d0e5fbb0SMing Lei 1579cc5169cSBart Van Assche return sectors & (lbs - 1); 158d0e5fbb0SMing Lei } 159d0e5fbb0SMing Lei 160*429120f3SMing Lei static inline unsigned get_max_segment_size(const struct request_queue *q, 161*429120f3SMing Lei struct page *start_page, 162*429120f3SMing Lei unsigned long offset) 163dcebd755SMing Lei { 164dcebd755SMing Lei unsigned long mask = queue_segment_boundary(q); 165dcebd755SMing Lei 166*429120f3SMing Lei offset = mask & (page_to_phys(start_page) + offset); 167*429120f3SMing Lei return min_t(unsigned long, mask - offset + 1, 168dcebd755SMing Lei queue_max_segment_size(q)); 169dcebd755SMing Lei } 170dcebd755SMing Lei 171708b25b3SBart Van Assche /** 172708b25b3SBart Van Assche * bvec_split_segs - verify whether or not a bvec should be split in the middle 173708b25b3SBart Van Assche * @q: [in] request queue associated with the bio associated with @bv 174708b25b3SBart Van Assche * @bv: [in] bvec to examine 175708b25b3SBart Van Assche * @nsegs: [in,out] Number of segments in the bio being built. Incremented 176708b25b3SBart Van Assche * by the number of segments from @bv that may be appended to that 177708b25b3SBart Van Assche * bio without exceeding @max_segs 178708b25b3SBart Van Assche * @sectors: [in,out] Number of sectors in the bio being built. Incremented 179708b25b3SBart Van Assche * by the number of sectors from @bv that may be appended to that 180708b25b3SBart Van Assche * bio without exceeding @max_sectors 181708b25b3SBart Van Assche * @max_segs: [in] upper bound for *@nsegs 182708b25b3SBart Van Assche * @max_sectors: [in] upper bound for *@sectors 183708b25b3SBart Van Assche * 184708b25b3SBart Van Assche * When splitting a bio, it can happen that a bvec is encountered that is too 185708b25b3SBart Van Assche * big to fit in a single segment and hence that it has to be split in the 186708b25b3SBart Van Assche * middle. This function verifies whether or not that should happen. The value 187708b25b3SBart Van Assche * %true is returned if and only if appending the entire @bv to a bio with 188708b25b3SBart Van Assche * *@nsegs segments and *@sectors sectors would make that bio unacceptable for 189708b25b3SBart Van Assche * the block driver. 190dcebd755SMing Lei */ 191af2c68feSBart Van Assche static bool bvec_split_segs(const struct request_queue *q, 192af2c68feSBart Van Assche const struct bio_vec *bv, unsigned *nsegs, 193708b25b3SBart Van Assche unsigned *sectors, unsigned max_segs, 194708b25b3SBart Van Assche unsigned max_sectors) 195dcebd755SMing Lei { 196708b25b3SBart Van Assche unsigned max_len = (min(max_sectors, UINT_MAX >> 9) - *sectors) << 9; 197708b25b3SBart Van Assche unsigned len = min(bv->bv_len, max_len); 198dcebd755SMing Lei unsigned total_len = 0; 199ff9811b3SBart Van Assche unsigned seg_size = 0; 200dcebd755SMing Lei 201ff9811b3SBart Van Assche while (len && *nsegs < max_segs) { 202*429120f3SMing Lei seg_size = get_max_segment_size(q, bv->bv_page, 203*429120f3SMing Lei bv->bv_offset + total_len); 204dcebd755SMing Lei seg_size = min(seg_size, len); 205dcebd755SMing Lei 206ff9811b3SBart Van Assche (*nsegs)++; 207dcebd755SMing Lei total_len += seg_size; 208dcebd755SMing Lei len -= seg_size; 209dcebd755SMing Lei 210dcebd755SMing Lei if ((bv->bv_offset + total_len) & queue_virt_boundary(q)) 211dcebd755SMing Lei break; 212dcebd755SMing Lei } 213dcebd755SMing Lei 214dcebd755SMing Lei *sectors += total_len >> 9; 215dcebd755SMing Lei 216708b25b3SBart Van Assche /* tell the caller to split the bvec if it is too big to fit */ 217708b25b3SBart Van Assche return len > 0 || bv->bv_len > max_len; 218dcebd755SMing Lei } 219dcebd755SMing Lei 220dad77584SBart Van Assche /** 221dad77584SBart Van Assche * blk_bio_segment_split - split a bio in two bios 222dad77584SBart Van Assche * @q: [in] request queue pointer 223dad77584SBart Van Assche * @bio: [in] bio to be split 224dad77584SBart Van Assche * @bs: [in] bio set to allocate the clone from 225dad77584SBart Van Assche * @segs: [out] number of segments in the bio with the first half of the sectors 226dad77584SBart Van Assche * 227dad77584SBart Van Assche * Clone @bio, update the bi_iter of the clone to represent the first sectors 228dad77584SBart Van Assche * of @bio and update @bio->bi_iter to represent the remaining sectors. The 229dad77584SBart Van Assche * following is guaranteed for the cloned bio: 230dad77584SBart Van Assche * - That it has at most get_max_io_size(@q, @bio) sectors. 231dad77584SBart Van Assche * - That it has at most queue_max_segments(@q) segments. 232dad77584SBart Van Assche * 233dad77584SBart Van Assche * Except for discard requests the cloned bio will point at the bi_io_vec of 234dad77584SBart Van Assche * the original bio. It is the responsibility of the caller to ensure that the 235dad77584SBart Van Assche * original bio is not freed before the cloned bio. The caller is also 236dad77584SBart Van Assche * responsible for ensuring that @bs is only destroyed after processing of the 237dad77584SBart Van Assche * split bio has finished. 238dad77584SBart Van Assche */ 23954efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 24054efd50bSKent Overstreet struct bio *bio, 241bdced438SMing Lei struct bio_set *bs, 242bdced438SMing Lei unsigned *segs) 24354efd50bSKent Overstreet { 2445014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 24554efd50bSKent Overstreet struct bvec_iter iter; 2466869875fSChristoph Hellwig unsigned nsegs = 0, sectors = 0; 247d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 24805b700baSMing Lei const unsigned max_segs = queue_max_segments(q); 24954efd50bSKent Overstreet 250dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 25154efd50bSKent Overstreet /* 25254efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 25354efd50bSKent Overstreet * offset would create a gap, disallow it. 25454efd50bSKent Overstreet */ 2555014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 25654efd50bSKent Overstreet goto split; 25754efd50bSKent Overstreet 25805b700baSMing Lei if (nsegs < max_segs && 259708b25b3SBart Van Assche sectors + (bv.bv_len >> 9) <= max_sectors && 260708b25b3SBart Van Assche bv.bv_offset + bv.bv_len <= PAGE_SIZE) { 261708b25b3SBart Van Assche nsegs++; 262708b25b3SBart Van Assche sectors += bv.bv_len >> 9; 263708b25b3SBart Van Assche } else if (bvec_split_segs(q, &bv, &nsegs, §ors, max_segs, 264708b25b3SBart Van Assche max_sectors)) { 265e36f6204SKeith Busch goto split; 266e36f6204SKeith Busch } 267e36f6204SKeith Busch 26854efd50bSKent Overstreet bvprv = bv; 269578270bfSMing Lei bvprvp = &bvprv; 27054efd50bSKent Overstreet } 27154efd50bSKent Overstreet 272d627065dSChristoph Hellwig *segs = nsegs; 273d627065dSChristoph Hellwig return NULL; 27454efd50bSKent Overstreet split: 275bdced438SMing Lei *segs = nsegs; 276d627065dSChristoph Hellwig return bio_split(bio, sectors, GFP_NOIO, bs); 27754efd50bSKent Overstreet } 27854efd50bSKent Overstreet 279dad77584SBart Van Assche /** 280dad77584SBart Van Assche * __blk_queue_split - split a bio and submit the second half 281dad77584SBart Van Assche * @q: [in] request queue pointer 282dad77584SBart Van Assche * @bio: [in, out] bio to be split 283dad77584SBart Van Assche * @nr_segs: [out] number of segments in the first bio 284dad77584SBart Van Assche * 285dad77584SBart Van Assche * Split a bio into two bios, chain the two bios, submit the second half and 286dad77584SBart Van Assche * store a pointer to the first half in *@bio. If the second bio is still too 287dad77584SBart Van Assche * big it will be split by a recursive call to this function. Since this 288dad77584SBart Van Assche * function may allocate a new bio from @q->bio_split, it is the responsibility 289dad77584SBart Van Assche * of the caller to ensure that @q is only released after processing of the 290dad77584SBart Van Assche * split bio has finished. 291dad77584SBart Van Assche */ 29214ccb66bSChristoph Hellwig void __blk_queue_split(struct request_queue *q, struct bio **bio, 29314ccb66bSChristoph Hellwig unsigned int *nr_segs) 29454efd50bSKent Overstreet { 295fa532287SChristoph Hellwig struct bio *split = NULL; 29654efd50bSKent Overstreet 2977afafc8aSAdrian Hunter switch (bio_op(*bio)) { 2987afafc8aSAdrian Hunter case REQ_OP_DISCARD: 2997afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 30014ccb66bSChristoph Hellwig split = blk_bio_discard_split(q, *bio, &q->bio_split, nr_segs); 3017afafc8aSAdrian Hunter break; 302a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 30314ccb66bSChristoph Hellwig split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, 30414ccb66bSChristoph Hellwig nr_segs); 305a6f0788eSChaitanya Kulkarni break; 3067afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 30714ccb66bSChristoph Hellwig split = blk_bio_write_same_split(q, *bio, &q->bio_split, 30814ccb66bSChristoph Hellwig nr_segs); 3097afafc8aSAdrian Hunter break; 3107afafc8aSAdrian Hunter default: 311fa532287SChristoph Hellwig /* 312fa532287SChristoph Hellwig * All drivers must accept single-segments bios that are <= 313fa532287SChristoph Hellwig * PAGE_SIZE. This is a quick and dirty check that relies on 314fa532287SChristoph Hellwig * the fact that bi_io_vec[0] is always valid if a bio has data. 315fa532287SChristoph Hellwig * The check might lead to occasional false negatives when bios 316fa532287SChristoph Hellwig * are cloned, but compared to the performance impact of cloned 317fa532287SChristoph Hellwig * bios themselves the loop below doesn't matter anyway. 318fa532287SChristoph Hellwig */ 319fa532287SChristoph Hellwig if (!q->limits.chunk_sectors && 320fa532287SChristoph Hellwig (*bio)->bi_vcnt == 1 && 32159db8ba2SMing Lei ((*bio)->bi_io_vec[0].bv_len + 3221e279153SJens Axboe (*bio)->bi_io_vec[0].bv_offset) <= PAGE_SIZE) { 323fa532287SChristoph Hellwig *nr_segs = 1; 324fa532287SChristoph Hellwig break; 325fa532287SChristoph Hellwig } 32614ccb66bSChristoph Hellwig split = blk_bio_segment_split(q, *bio, &q->bio_split, nr_segs); 3277afafc8aSAdrian Hunter break; 3287afafc8aSAdrian Hunter } 329bdced438SMing Lei 33054efd50bSKent Overstreet if (split) { 3316ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 3321eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 3336ac45aebSMing Lei 334947b7ac1SJens Axboe /* 335947b7ac1SJens Axboe * Since we're recursing into make_request here, ensure 336947b7ac1SJens Axboe * that we mark this bio as already having entered the queue. 337947b7ac1SJens Axboe * If not, and the queue is going away, we can get stuck 338947b7ac1SJens Axboe * forever on waiting for the queue reference to drop. But 339947b7ac1SJens Axboe * that will never happen, as we're already holding a 340947b7ac1SJens Axboe * reference to it. 341947b7ac1SJens Axboe */ 342947b7ac1SJens Axboe bio_set_flag(*bio, BIO_QUEUE_ENTERED); 343947b7ac1SJens Axboe 34454efd50bSKent Overstreet bio_chain(split, *bio); 345cda22646SMike Krinkin trace_block_split(q, split, (*bio)->bi_iter.bi_sector); 34654efd50bSKent Overstreet generic_make_request(*bio); 34754efd50bSKent Overstreet *bio = split; 34854efd50bSKent Overstreet } 34954efd50bSKent Overstreet } 35014ccb66bSChristoph Hellwig 351dad77584SBart Van Assche /** 352dad77584SBart Van Assche * blk_queue_split - split a bio and submit the second half 353dad77584SBart Van Assche * @q: [in] request queue pointer 354dad77584SBart Van Assche * @bio: [in, out] bio to be split 355dad77584SBart Van Assche * 356dad77584SBart Van Assche * Split a bio into two bios, chains the two bios, submit the second half and 357dad77584SBart Van Assche * store a pointer to the first half in *@bio. Since this function may allocate 358dad77584SBart Van Assche * a new bio from @q->bio_split, it is the responsibility of the caller to 359dad77584SBart Van Assche * ensure that @q is only released after processing of the split bio has 360dad77584SBart Van Assche * finished. 361dad77584SBart Van Assche */ 36214ccb66bSChristoph Hellwig void blk_queue_split(struct request_queue *q, struct bio **bio) 36314ccb66bSChristoph Hellwig { 36414ccb66bSChristoph Hellwig unsigned int nr_segs; 36514ccb66bSChristoph Hellwig 36614ccb66bSChristoph Hellwig __blk_queue_split(q, bio, &nr_segs); 36714ccb66bSChristoph Hellwig } 36854efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 36954efd50bSKent Overstreet 370e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq) 371d6d48196SJens Axboe { 3726869875fSChristoph Hellwig unsigned int nr_phys_segs = 0; 373ff9811b3SBart Van Assche unsigned int nr_sectors = 0; 374e9cd19c0SChristoph Hellwig struct req_iterator iter; 3756869875fSChristoph Hellwig struct bio_vec bv; 376d6d48196SJens Axboe 377e9cd19c0SChristoph Hellwig if (!rq->bio) 3781e428079SJens Axboe return 0; 379d6d48196SJens Axboe 380e9cd19c0SChristoph Hellwig switch (bio_op(rq->bio)) { 381a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 382a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 383a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 384f9d03f96SChristoph Hellwig return 0; 385f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 3865cb8850cSKent Overstreet return 1; 387a6f0788eSChaitanya Kulkarni } 3885cb8850cSKent Overstreet 389e9cd19c0SChristoph Hellwig rq_for_each_bvec(bv, rq, iter) 390ff9811b3SBart Van Assche bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors, 391708b25b3SBart Van Assche UINT_MAX, UINT_MAX); 3921e428079SJens Axboe return nr_phys_segs; 3931e428079SJens Axboe } 3941e428079SJens Axboe 39548d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, 396862e5a5eSMing Lei struct scatterlist *sglist) 397862e5a5eSMing Lei { 398862e5a5eSMing Lei if (!*sg) 399862e5a5eSMing Lei return sglist; 400862e5a5eSMing Lei 401862e5a5eSMing Lei /* 402862e5a5eSMing Lei * If the driver previously mapped a shorter list, we could see a 403862e5a5eSMing Lei * termination bit prematurely unless it fully inits the sg table 404862e5a5eSMing Lei * on each mapping. We KNOW that there must be more entries here 405862e5a5eSMing Lei * or the driver would be buggy, so force clear the termination bit 406862e5a5eSMing Lei * to avoid doing a full sg_init_table() in drivers for each command. 407862e5a5eSMing Lei */ 408862e5a5eSMing Lei sg_unmark_end(*sg); 409862e5a5eSMing Lei return sg_next(*sg); 410862e5a5eSMing Lei } 411862e5a5eSMing Lei 412862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q, 413862e5a5eSMing Lei struct bio_vec *bvec, struct scatterlist *sglist, 414862e5a5eSMing Lei struct scatterlist **sg) 415862e5a5eSMing Lei { 416862e5a5eSMing Lei unsigned nbytes = bvec->bv_len; 4178a96a0e4SChristoph Hellwig unsigned nsegs = 0, total = 0; 418862e5a5eSMing Lei 419862e5a5eSMing Lei while (nbytes > 0) { 4208a96a0e4SChristoph Hellwig unsigned offset = bvec->bv_offset + total; 421*429120f3SMing Lei unsigned len = min(get_max_segment_size(q, bvec->bv_page, 422*429120f3SMing Lei offset), nbytes); 423f9f76879SChristoph Hellwig struct page *page = bvec->bv_page; 424f9f76879SChristoph Hellwig 425f9f76879SChristoph Hellwig /* 426f9f76879SChristoph Hellwig * Unfortunately a fair number of drivers barf on scatterlists 427f9f76879SChristoph Hellwig * that have an offset larger than PAGE_SIZE, despite other 428f9f76879SChristoph Hellwig * subsystems dealing with that invariant just fine. For now 429f9f76879SChristoph Hellwig * stick to the legacy format where we never present those from 430f9f76879SChristoph Hellwig * the block layer, but the code below should be removed once 431f9f76879SChristoph Hellwig * these offenders (mostly MMC/SD drivers) are fixed. 432f9f76879SChristoph Hellwig */ 433f9f76879SChristoph Hellwig page += (offset >> PAGE_SHIFT); 434f9f76879SChristoph Hellwig offset &= ~PAGE_MASK; 435862e5a5eSMing Lei 436862e5a5eSMing Lei *sg = blk_next_sg(sg, sglist); 437f9f76879SChristoph Hellwig sg_set_page(*sg, page, len, offset); 438862e5a5eSMing Lei 4398a96a0e4SChristoph Hellwig total += len; 4408a96a0e4SChristoph Hellwig nbytes -= len; 441862e5a5eSMing Lei nsegs++; 442862e5a5eSMing Lei } 443862e5a5eSMing Lei 444862e5a5eSMing Lei return nsegs; 445862e5a5eSMing Lei } 446862e5a5eSMing Lei 44716e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv, 44816e3e418SMing Lei struct scatterlist *sglist, struct scatterlist **sg) 44916e3e418SMing Lei { 45016e3e418SMing Lei *sg = blk_next_sg(sg, sglist); 45116e3e418SMing Lei sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 45216e3e418SMing Lei return 1; 45316e3e418SMing Lei } 45416e3e418SMing Lei 455f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */ 456f6970f83SMing Lei static inline bool 457f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec, 458f6970f83SMing Lei struct bio_vec *bvprv, struct scatterlist **sg) 459963ab9e5SAsias He { 460963ab9e5SAsias He 461963ab9e5SAsias He int nbytes = bvec->bv_len; 462963ab9e5SAsias He 463f6970f83SMing Lei if (!*sg) 464f6970f83SMing Lei return false; 465f6970f83SMing Lei 466b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 467f6970f83SMing Lei return false; 468f6970f83SMing Lei 4693dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec)) 470f6970f83SMing Lei return false; 471963ab9e5SAsias He 472963ab9e5SAsias He (*sg)->length += nbytes; 473f6970f83SMing Lei 474f6970f83SMing Lei return true; 475963ab9e5SAsias He } 476963ab9e5SAsias He 4775cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 4785cb8850cSKent Overstreet struct scatterlist *sglist, 4795cb8850cSKent Overstreet struct scatterlist **sg) 4805cb8850cSKent Overstreet { 481b21e11c5SMing Lei struct bio_vec uninitialized_var(bvec), bvprv = { NULL }; 4825cb8850cSKent Overstreet struct bvec_iter iter; 48338417468SChristoph Hellwig int nsegs = 0; 484f6970f83SMing Lei bool new_bio = false; 4855cb8850cSKent Overstreet 486f6970f83SMing Lei for_each_bio(bio) { 487f6970f83SMing Lei bio_for_each_bvec(bvec, bio, iter) { 488f6970f83SMing Lei /* 489f6970f83SMing Lei * Only try to merge bvecs from two bios given we 490f6970f83SMing Lei * have done bio internal merge when adding pages 491f6970f83SMing Lei * to bio 492f6970f83SMing Lei */ 493f6970f83SMing Lei if (new_bio && 494f6970f83SMing Lei __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg)) 495f6970f83SMing Lei goto next_bvec; 496f6970f83SMing Lei 497f6970f83SMing Lei if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE) 498f6970f83SMing Lei nsegs += __blk_bvec_map_sg(bvec, sglist, sg); 499f6970f83SMing Lei else 500f6970f83SMing Lei nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg); 501f6970f83SMing Lei next_bvec: 502f6970f83SMing Lei new_bio = false; 503f6970f83SMing Lei } 504b21e11c5SMing Lei if (likely(bio->bi_iter.bi_size)) { 505f6970f83SMing Lei bvprv = bvec; 506f6970f83SMing Lei new_bio = true; 507f6970f83SMing Lei } 508b21e11c5SMing Lei } 5095cb8850cSKent Overstreet 5105cb8850cSKent Overstreet return nsegs; 5115cb8850cSKent Overstreet } 5125cb8850cSKent Overstreet 513d6d48196SJens Axboe /* 514d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 515d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 516d6d48196SJens Axboe */ 517d6d48196SJens Axboe int blk_rq_map_sg(struct request_queue *q, struct request *rq, 518d6d48196SJens Axboe struct scatterlist *sglist) 519d6d48196SJens Axboe { 5205cb8850cSKent Overstreet struct scatterlist *sg = NULL; 5215cb8850cSKent Overstreet int nsegs = 0; 522d6d48196SJens Axboe 523f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 524cae6c2e5SMing Lei nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, &sg); 525f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 526cae6c2e5SMing Lei nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, &sg); 527f9d03f96SChristoph Hellwig else if (rq->bio) 5285cb8850cSKent Overstreet nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); 529f18573abSFUJITA Tomonori 530e8064021SChristoph Hellwig if (unlikely(rq->rq_flags & RQF_COPY_USER) && 5312e46e8b2STejun Heo (blk_rq_bytes(rq) & q->dma_pad_mask)) { 5322e46e8b2STejun Heo unsigned int pad_len = 5332e46e8b2STejun Heo (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; 534f18573abSFUJITA Tomonori 535f18573abSFUJITA Tomonori sg->length += pad_len; 536f18573abSFUJITA Tomonori rq->extra_len += pad_len; 537f18573abSFUJITA Tomonori } 538f18573abSFUJITA Tomonori 5392fb98e84STejun Heo if (q->dma_drain_size && q->dma_drain_needed(rq)) { 540a8ebb056SMike Christie if (op_is_write(req_op(rq))) 541db0a2e00STejun Heo memset(q->dma_drain_buffer, 0, q->dma_drain_size); 542db0a2e00STejun Heo 543da81ed16SDan Williams sg_unmark_end(sg); 544d6d48196SJens Axboe sg = sg_next(sg); 545d6d48196SJens Axboe sg_set_page(sg, virt_to_page(q->dma_drain_buffer), 546d6d48196SJens Axboe q->dma_drain_size, 547d6d48196SJens Axboe ((unsigned long)q->dma_drain_buffer) & 548d6d48196SJens Axboe (PAGE_SIZE - 1)); 549d6d48196SJens Axboe nsegs++; 5507a85f889SFUJITA Tomonori rq->extra_len += q->dma_drain_size; 551d6d48196SJens Axboe } 552d6d48196SJens Axboe 553d6d48196SJens Axboe if (sg) 554d6d48196SJens Axboe sg_mark_end(sg); 555d6d48196SJens Axboe 55612e57f59SMing Lei /* 55712e57f59SMing Lei * Something must have been wrong if the figured number of 55812e57f59SMing Lei * segment is bigger than number of req's physical segments 55912e57f59SMing Lei */ 560f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 56112e57f59SMing Lei 562d6d48196SJens Axboe return nsegs; 563d6d48196SJens Axboe } 564d6d48196SJens Axboe EXPORT_SYMBOL(blk_rq_map_sg); 565d6d48196SJens Axboe 56614ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio, 56714ccb66bSChristoph Hellwig unsigned int nr_phys_segs) 568d6d48196SJens Axboe { 56914ccb66bSChristoph Hellwig if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(req->q)) 57013f05c8dSMartin K. Petersen goto no_merge; 57113f05c8dSMartin K. Petersen 57214ccb66bSChristoph Hellwig if (blk_integrity_merge_bio(req->q, req, bio) == false) 57313f05c8dSMartin K. Petersen goto no_merge; 574d6d48196SJens Axboe 575d6d48196SJens Axboe /* 576d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 577d6d48196SJens Axboe * counters. 578d6d48196SJens Axboe */ 579d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 580d6d48196SJens Axboe return 1; 58113f05c8dSMartin K. Petersen 58213f05c8dSMartin K. Petersen no_merge: 58314ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 58413f05c8dSMartin K. Petersen return 0; 585d6d48196SJens Axboe } 586d6d48196SJens Axboe 58714ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs) 588d6d48196SJens Axboe { 5895e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 5905e7c4274SJens Axboe return 0; 5917f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5927f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 5937f39add3SSagi Grimberg return 0; 594f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 59517007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 59614ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 597d6d48196SJens Axboe return 0; 598d6d48196SJens Axboe } 599d6d48196SJens Axboe 60014ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 601d6d48196SJens Axboe } 602d6d48196SJens Axboe 60314ccb66bSChristoph Hellwig int ll_front_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs) 604d6d48196SJens Axboe { 6055e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 6065e7c4274SJens Axboe return 0; 6077f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6087f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 6097f39add3SSagi Grimberg return 0; 610f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 61117007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 61214ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 613d6d48196SJens Axboe return 0; 614d6d48196SJens Axboe } 615d6d48196SJens Axboe 61614ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 617d6d48196SJens Axboe } 618d6d48196SJens Axboe 619445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 620445251d0SJens Axboe struct request *next) 621445251d0SJens Axboe { 622445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 623445251d0SJens Axboe 624445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 625445251d0SJens Axboe goto no_merge; 626445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 627445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 628445251d0SJens Axboe goto no_merge; 629445251d0SJens Axboe 630445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 631445251d0SJens Axboe return true; 632445251d0SJens Axboe no_merge: 633445251d0SJens Axboe req_set_nomerge(q, req); 634445251d0SJens Axboe return false; 635445251d0SJens Axboe } 636445251d0SJens Axboe 637d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 638d6d48196SJens Axboe struct request *next) 639d6d48196SJens Axboe { 640d6d48196SJens Axboe int total_phys_segments; 641d6d48196SJens Axboe 6425e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 643854fbb9cSKeith Busch return 0; 644854fbb9cSKeith Busch 645d6d48196SJens Axboe /* 646d6d48196SJens Axboe * Will it become too large? 647d6d48196SJens Axboe */ 648f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 64917007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 650d6d48196SJens Axboe return 0; 651d6d48196SJens Axboe 652d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 6538a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 654d6d48196SJens Axboe return 0; 655d6d48196SJens Axboe 6564eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 65713f05c8dSMartin K. Petersen return 0; 65813f05c8dSMartin K. Petersen 659d6d48196SJens Axboe /* Merge is OK... */ 660d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 661d6d48196SJens Axboe return 1; 662d6d48196SJens Axboe } 663d6d48196SJens Axboe 66480a761fdSTejun Heo /** 66580a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 66680a761fdSTejun Heo * @rq: request to mark as mixed merge 66780a761fdSTejun Heo * 66880a761fdSTejun Heo * Description: 66980a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 67080a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 67180a761fdSTejun Heo * merged. 67280a761fdSTejun Heo */ 67380a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 67480a761fdSTejun Heo { 67580a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 67680a761fdSTejun Heo struct bio *bio; 67780a761fdSTejun Heo 678e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 67980a761fdSTejun Heo return; 68080a761fdSTejun Heo 68180a761fdSTejun Heo /* 68280a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 68380a761fdSTejun Heo * contained bios. It will just track those of the first one. 68480a761fdSTejun Heo * Distributes the attributs to each bio. 68580a761fdSTejun Heo */ 68680a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 6871eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 6881eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 6891eff9d32SJens Axboe bio->bi_opf |= ff; 69080a761fdSTejun Heo } 691e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 69280a761fdSTejun Heo } 69380a761fdSTejun Heo 69426308eabSJerome Marchand static void blk_account_io_merge(struct request *req) 69526308eabSJerome Marchand { 69626308eabSJerome Marchand if (blk_do_io_stat(req)) { 69726308eabSJerome Marchand struct hd_struct *part; 69826308eabSJerome Marchand 699112f158fSMike Snitzer part_stat_lock(); 70009e099d4SJerome Marchand part = req->part; 70126308eabSJerome Marchand 702d62e26b3SJens Axboe part_dec_in_flight(req->q, part, rq_data_dir(req)); 70326308eabSJerome Marchand 7046c23a968SJens Axboe hd_struct_put(part); 70526308eabSJerome Marchand part_stat_unlock(); 70626308eabSJerome Marchand } 70726308eabSJerome Marchand } 70869840466SJianchao Wang /* 70969840466SJianchao Wang * Two cases of handling DISCARD merge: 71069840466SJianchao Wang * If max_discard_segments > 1, the driver takes every bio 71169840466SJianchao Wang * as a range and send them to controller together. The ranges 71269840466SJianchao Wang * needn't to be contiguous. 71369840466SJianchao Wang * Otherwise, the bios/requests will be handled as same as 71469840466SJianchao Wang * others which should be contiguous. 71569840466SJianchao Wang */ 71669840466SJianchao Wang static inline bool blk_discard_mergable(struct request *req) 71769840466SJianchao Wang { 71869840466SJianchao Wang if (req_op(req) == REQ_OP_DISCARD && 71969840466SJianchao Wang queue_max_discard_segments(req->q) > 1) 72069840466SJianchao Wang return true; 72169840466SJianchao Wang return false; 72269840466SJianchao Wang } 72369840466SJianchao Wang 724e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req, 725e96c0d83SEric Biggers struct request *next) 72669840466SJianchao Wang { 72769840466SJianchao Wang if (blk_discard_mergable(req)) 72869840466SJianchao Wang return ELEVATOR_DISCARD_MERGE; 72969840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) 73069840466SJianchao Wang return ELEVATOR_BACK_MERGE; 73169840466SJianchao Wang 73269840466SJianchao Wang return ELEVATOR_NO_MERGE; 73369840466SJianchao Wang } 73426308eabSJerome Marchand 735d6d48196SJens Axboe /* 736b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 737b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 738d6d48196SJens Axboe */ 739b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 740b973cb7eSJens Axboe struct request *req, struct request *next) 741d6d48196SJens Axboe { 742d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 743b973cb7eSJens Axboe return NULL; 744d6d48196SJens Axboe 745288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 746b973cb7eSJens Axboe return NULL; 747f31dc1cdSMartin K. Petersen 748d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 7492081a56bSJens Axboe || req->rq_disk != next->rq_disk) 750b973cb7eSJens Axboe return NULL; 751d6d48196SJens Axboe 7528fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 7534363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 754b973cb7eSJens Axboe return NULL; 7554363ac7cSMartin K. Petersen 756d6d48196SJens Axboe /* 757cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 758cb6934f8SJens Axboe * non-hint IO. 759cb6934f8SJens Axboe */ 760cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 761cb6934f8SJens Axboe return NULL; 762cb6934f8SJens Axboe 763668ffc03SDamien Le Moal if (req->ioprio != next->ioprio) 764668ffc03SDamien Le Moal return NULL; 765668ffc03SDamien Le Moal 766cb6934f8SJens Axboe /* 767d6d48196SJens Axboe * If we are allowed to merge, then append bio list 768d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 769d6d48196SJens Axboe * will have updated segment counts, update sector 770445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 771445251d0SJens Axboe * have separate settings. 772d6d48196SJens Axboe */ 77369840466SJianchao Wang 77469840466SJianchao Wang switch (blk_try_req_merge(req, next)) { 77569840466SJianchao Wang case ELEVATOR_DISCARD_MERGE: 776445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 777445251d0SJens Axboe return NULL; 77869840466SJianchao Wang break; 77969840466SJianchao Wang case ELEVATOR_BACK_MERGE: 78069840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next)) 781b973cb7eSJens Axboe return NULL; 78269840466SJianchao Wang break; 78369840466SJianchao Wang default: 78469840466SJianchao Wang return NULL; 78569840466SJianchao Wang } 786d6d48196SJens Axboe 787d6d48196SJens Axboe /* 78880a761fdSTejun Heo * If failfast settings disagree or any of the two is already 78980a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 79080a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 79180a761fdSTejun Heo * set properly. 79280a761fdSTejun Heo */ 793e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 79480a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 79580a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 79680a761fdSTejun Heo blk_rq_set_mixed_merge(req); 79780a761fdSTejun Heo blk_rq_set_mixed_merge(next); 79880a761fdSTejun Heo } 79980a761fdSTejun Heo 80080a761fdSTejun Heo /* 801522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 802522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 803522a7775SOmar Sandoval * current request for accounting purposes. 804d6d48196SJens Axboe */ 805522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 806522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 807d6d48196SJens Axboe 808d6d48196SJens Axboe req->biotail->bi_next = next->bio; 809d6d48196SJens Axboe req->biotail = next->biotail; 810d6d48196SJens Axboe 811a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 812d6d48196SJens Axboe 8132a5cf35cSMing Lei if (!blk_discard_mergable(req)) 814d6d48196SJens Axboe elv_merge_requests(q, req, next); 815d6d48196SJens Axboe 81642dad764SJerome Marchand /* 81742dad764SJerome Marchand * 'next' is going away, so update stats accordingly 81842dad764SJerome Marchand */ 81942dad764SJerome Marchand blk_account_io_merge(next); 820d6d48196SJens Axboe 821e4d750c9SJens Axboe /* 822e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 823e4d750c9SJens Axboe * the caller to free 824e4d750c9SJens Axboe */ 8251cd96c24SBoaz Harrosh next->bio = NULL; 826b973cb7eSJens Axboe return next; 827d6d48196SJens Axboe } 828d6d48196SJens Axboe 829b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq) 830d6d48196SJens Axboe { 831d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 832d6d48196SJens Axboe 833d6d48196SJens Axboe if (next) 834d6d48196SJens Axboe return attempt_merge(q, rq, next); 835d6d48196SJens Axboe 836b973cb7eSJens Axboe return NULL; 837d6d48196SJens Axboe } 838d6d48196SJens Axboe 839b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq) 840d6d48196SJens Axboe { 841d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 842d6d48196SJens Axboe 843d6d48196SJens Axboe if (prev) 844d6d48196SJens Axboe return attempt_merge(q, prev, rq); 845d6d48196SJens Axboe 846b973cb7eSJens Axboe return NULL; 847d6d48196SJens Axboe } 8485e84ea3aSJens Axboe 8495e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 8505e84ea3aSJens Axboe struct request *next) 8515e84ea3aSJens Axboe { 852e4d750c9SJens Axboe struct request *free; 85372ef799bSTahsin Erdogan 854e4d750c9SJens Axboe free = attempt_merge(q, rq, next); 855e4d750c9SJens Axboe if (free) { 85692bc5a24SJens Axboe blk_put_request(free); 857e4d750c9SJens Axboe return 1; 858e4d750c9SJens Axboe } 859e4d750c9SJens Axboe 860e4d750c9SJens Axboe return 0; 8615e84ea3aSJens Axboe } 862050c8ea8STejun Heo 863050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 864050c8ea8STejun Heo { 865e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 866050c8ea8STejun Heo return false; 867050c8ea8STejun Heo 868288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 869f31dc1cdSMartin K. Petersen return false; 870f31dc1cdSMartin K. Petersen 871050c8ea8STejun Heo /* different data direction or already started, don't merge */ 872050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 873050c8ea8STejun Heo return false; 874050c8ea8STejun Heo 8752081a56bSJens Axboe /* must be same device */ 8762081a56bSJens Axboe if (rq->rq_disk != bio->bi_disk) 877050c8ea8STejun Heo return false; 878050c8ea8STejun Heo 879050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 8804eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 881050c8ea8STejun Heo return false; 882050c8ea8STejun Heo 8834363ac7cSMartin K. Petersen /* must be using the same buffer */ 8848fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 8854363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 8864363ac7cSMartin K. Petersen return false; 8874363ac7cSMartin K. Petersen 888cb6934f8SJens Axboe /* 889cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 890cb6934f8SJens Axboe * non-hint IO. 891cb6934f8SJens Axboe */ 892cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 893cb6934f8SJens Axboe return false; 894cb6934f8SJens Axboe 895668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio)) 896668ffc03SDamien Le Moal return false; 897668ffc03SDamien Le Moal 898050c8ea8STejun Heo return true; 899050c8ea8STejun Heo } 900050c8ea8STejun Heo 90134fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 902050c8ea8STejun Heo { 90369840466SJianchao Wang if (blk_discard_mergable(rq)) 9041e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 9051e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 906050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 9074f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 908050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 909050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 910050c8ea8STejun Heo } 911