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> 9fe45e630SChristoph Hellwig #include <linux/blk-integrity.h> 10d6d48196SJens Axboe #include <linux/scatterlist.h> 11d6d48196SJens Axboe 12cda22646SMike Krinkin #include <trace/events/block.h> 13cda22646SMike Krinkin 14d6d48196SJens Axboe #include "blk.h" 158e756373SBaolin Wang #include "blk-rq-qos.h" 16a7b36ee6SJens Axboe #include "blk-throttle.h" 17d6d48196SJens Axboe 18ff18d77bSChristoph Hellwig static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv) 19ff18d77bSChristoph Hellwig { 20ff18d77bSChristoph Hellwig *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 21ff18d77bSChristoph Hellwig } 22ff18d77bSChristoph Hellwig 23ff18d77bSChristoph Hellwig static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv) 24ff18d77bSChristoph Hellwig { 25ff18d77bSChristoph Hellwig struct bvec_iter iter = bio->bi_iter; 26ff18d77bSChristoph Hellwig int idx; 27ff18d77bSChristoph Hellwig 28ff18d77bSChristoph Hellwig bio_get_first_bvec(bio, bv); 29ff18d77bSChristoph Hellwig if (bv->bv_len == bio->bi_iter.bi_size) 30ff18d77bSChristoph Hellwig return; /* this bio only has a single bvec */ 31ff18d77bSChristoph Hellwig 32ff18d77bSChristoph Hellwig bio_advance_iter(bio, &iter, iter.bi_size); 33ff18d77bSChristoph Hellwig 34ff18d77bSChristoph Hellwig if (!iter.bi_bvec_done) 35ff18d77bSChristoph Hellwig idx = iter.bi_idx - 1; 36ff18d77bSChristoph Hellwig else /* in the middle of bvec */ 37ff18d77bSChristoph Hellwig idx = iter.bi_idx; 38ff18d77bSChristoph Hellwig 39ff18d77bSChristoph Hellwig *bv = bio->bi_io_vec[idx]; 40ff18d77bSChristoph Hellwig 41ff18d77bSChristoph Hellwig /* 42ff18d77bSChristoph Hellwig * iter.bi_bvec_done records actual length of the last bvec 43ff18d77bSChristoph Hellwig * if this bio ends in the middle of one io vector 44ff18d77bSChristoph Hellwig */ 45ff18d77bSChristoph Hellwig if (iter.bi_bvec_done) 46ff18d77bSChristoph Hellwig bv->bv_len = iter.bi_bvec_done; 47ff18d77bSChristoph Hellwig } 48ff18d77bSChristoph Hellwig 49e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q, 50e9907009SChristoph Hellwig struct request *prev_rq, struct bio *prev, struct bio *next) 51e9907009SChristoph Hellwig { 52e9907009SChristoph Hellwig struct bio_vec pb, nb; 53e9907009SChristoph Hellwig 54e9907009SChristoph Hellwig if (!bio_has_data(prev) || !queue_virt_boundary(q)) 55e9907009SChristoph Hellwig return false; 56e9907009SChristoph Hellwig 57e9907009SChristoph Hellwig /* 58e9907009SChristoph Hellwig * Don't merge if the 1st bio starts with non-zero offset, otherwise it 59e9907009SChristoph Hellwig * is quite difficult to respect the sg gap limit. We work hard to 60e9907009SChristoph Hellwig * merge a huge number of small single bios in case of mkfs. 61e9907009SChristoph Hellwig */ 62e9907009SChristoph Hellwig if (prev_rq) 63e9907009SChristoph Hellwig bio_get_first_bvec(prev_rq->bio, &pb); 64e9907009SChristoph Hellwig else 65e9907009SChristoph Hellwig bio_get_first_bvec(prev, &pb); 66df376b2eSJohannes Thumshirn if (pb.bv_offset & queue_virt_boundary(q)) 67e9907009SChristoph Hellwig return true; 68e9907009SChristoph Hellwig 69e9907009SChristoph Hellwig /* 70e9907009SChristoph Hellwig * We don't need to worry about the situation that the merged segment 71e9907009SChristoph Hellwig * ends in unaligned virt boundary: 72e9907009SChristoph Hellwig * 73e9907009SChristoph Hellwig * - if 'pb' ends aligned, the merged segment ends aligned 74e9907009SChristoph Hellwig * - if 'pb' ends unaligned, the next bio must include 75e9907009SChristoph Hellwig * one single bvec of 'nb', otherwise the 'nb' can't 76e9907009SChristoph Hellwig * merge with 'pb' 77e9907009SChristoph Hellwig */ 78e9907009SChristoph Hellwig bio_get_last_bvec(prev, &pb); 79e9907009SChristoph Hellwig bio_get_first_bvec(next, &nb); 80200a9affSChristoph Hellwig if (biovec_phys_mergeable(q, &pb, &nb)) 81e9907009SChristoph Hellwig return false; 82e9907009SChristoph Hellwig return __bvec_gap_to_prev(q, &pb, nb.bv_offset); 83e9907009SChristoph Hellwig } 84e9907009SChristoph Hellwig 85e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio) 86e9907009SChristoph Hellwig { 87e9907009SChristoph Hellwig return bio_will_gap(req->q, req, req->biotail, bio); 88e9907009SChristoph Hellwig } 89e9907009SChristoph Hellwig 90e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio) 91e9907009SChristoph Hellwig { 92e9907009SChristoph Hellwig return bio_will_gap(req->q, NULL, bio, req->bio); 93e9907009SChristoph Hellwig } 94e9907009SChristoph Hellwig 9554efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 9654efd50bSKent Overstreet struct bio *bio, 97bdced438SMing Lei struct bio_set *bs, 98bdced438SMing Lei unsigned *nsegs) 9954efd50bSKent Overstreet { 10054efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 10154efd50bSKent Overstreet int alignment; 10254efd50bSKent Overstreet sector_t tmp; 10354efd50bSKent Overstreet unsigned split_sectors; 10454efd50bSKent Overstreet 105bdced438SMing Lei *nsegs = 1; 106bdced438SMing Lei 10754efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 10854efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 10954efd50bSKent Overstreet 1101adfc5e4SMing Lei max_discard_sectors = min(q->limits.max_discard_sectors, 1111adfc5e4SMing Lei bio_allowed_max_sectors(q)); 11254efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 11354efd50bSKent Overstreet 11454efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 11554efd50bSKent Overstreet /* XXX: warn */ 11654efd50bSKent Overstreet return NULL; 11754efd50bSKent Overstreet } 11854efd50bSKent Overstreet 11954efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 12054efd50bSKent Overstreet return NULL; 12154efd50bSKent Overstreet 12254efd50bSKent Overstreet split_sectors = max_discard_sectors; 12354efd50bSKent Overstreet 12454efd50bSKent Overstreet /* 12554efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 12654efd50bSKent Overstreet * the previous aligned sector. 12754efd50bSKent Overstreet */ 12854efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 12954efd50bSKent Overstreet 13054efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 13154efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 13254efd50bSKent Overstreet 13354efd50bSKent Overstreet if (split_sectors > tmp) 13454efd50bSKent Overstreet split_sectors -= tmp; 13554efd50bSKent Overstreet 13654efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 13754efd50bSKent Overstreet } 13854efd50bSKent Overstreet 139885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, 140885fa13fSChristoph Hellwig struct bio *bio, struct bio_set *bs, unsigned *nsegs) 141885fa13fSChristoph Hellwig { 142d665e12aSChristoph Hellwig *nsegs = 0; 143885fa13fSChristoph Hellwig 144885fa13fSChristoph Hellwig if (!q->limits.max_write_zeroes_sectors) 145885fa13fSChristoph Hellwig return NULL; 146885fa13fSChristoph Hellwig 147885fa13fSChristoph Hellwig if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) 148885fa13fSChristoph Hellwig return NULL; 149885fa13fSChristoph Hellwig 150885fa13fSChristoph Hellwig return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); 151885fa13fSChristoph Hellwig } 152885fa13fSChristoph Hellwig 15354efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 15454efd50bSKent Overstreet struct bio *bio, 155bdced438SMing Lei struct bio_set *bs, 156bdced438SMing Lei unsigned *nsegs) 15754efd50bSKent Overstreet { 158bdced438SMing Lei *nsegs = 1; 159bdced438SMing Lei 16054efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 16154efd50bSKent Overstreet return NULL; 16254efd50bSKent Overstreet 16354efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 16454efd50bSKent Overstreet return NULL; 16554efd50bSKent Overstreet 16654efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 16754efd50bSKent Overstreet } 16854efd50bSKent Overstreet 1699cc5169cSBart Van Assche /* 1709cc5169cSBart Van Assche * Return the maximum number of sectors from the start of a bio that may be 1719cc5169cSBart Van Assche * submitted as a single request to a block device. If enough sectors remain, 1729cc5169cSBart Van Assche * align the end to the physical block size. Otherwise align the end to the 1739cc5169cSBart Van Assche * logical block size. This approach minimizes the number of non-aligned 1749cc5169cSBart Van Assche * requests that are submitted to a block device if the start of a bio is not 1759cc5169cSBart Van Assche * aligned to a physical block boundary. 1769cc5169cSBart Van Assche */ 177d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 178d0e5fbb0SMing Lei struct bio *bio) 179d0e5fbb0SMing Lei { 1803ee16db3SMike Snitzer unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector, 0); 1819cc5169cSBart Van Assche unsigned max_sectors = sectors; 1829cc5169cSBart Van Assche unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT; 1839cc5169cSBart Van Assche unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT; 1849cc5169cSBart Van Assche unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1); 185d0e5fbb0SMing Lei 1869cc5169cSBart Van Assche max_sectors += start_offset; 1879cc5169cSBart Van Assche max_sectors &= ~(pbs - 1); 1889cc5169cSBart Van Assche if (max_sectors > start_offset) 1899cc5169cSBart Van Assche return max_sectors - start_offset; 190d0e5fbb0SMing Lei 191e4b469c6SKeith Busch return sectors & ~(lbs - 1); 192d0e5fbb0SMing Lei } 193d0e5fbb0SMing Lei 194429120f3SMing Lei static inline unsigned get_max_segment_size(const struct request_queue *q, 195429120f3SMing Lei struct page *start_page, 196429120f3SMing Lei unsigned long offset) 197dcebd755SMing Lei { 198dcebd755SMing Lei unsigned long mask = queue_segment_boundary(q); 199dcebd755SMing Lei 200429120f3SMing Lei offset = mask & (page_to_phys(start_page) + offset); 2014a2f704eSMing Lei 2024a2f704eSMing Lei /* 2034a2f704eSMing Lei * overflow may be triggered in case of zero page physical address 2044a2f704eSMing Lei * on 32bit arch, use queue's max segment size when that happens. 2054a2f704eSMing Lei */ 2064a2f704eSMing Lei return min_not_zero(mask - offset + 1, 2074a2f704eSMing Lei (unsigned long)queue_max_segment_size(q)); 208dcebd755SMing Lei } 209dcebd755SMing Lei 210708b25b3SBart Van Assche /** 211708b25b3SBart Van Assche * bvec_split_segs - verify whether or not a bvec should be split in the middle 212708b25b3SBart Van Assche * @q: [in] request queue associated with the bio associated with @bv 213708b25b3SBart Van Assche * @bv: [in] bvec to examine 214708b25b3SBart Van Assche * @nsegs: [in,out] Number of segments in the bio being built. Incremented 215708b25b3SBart Van Assche * by the number of segments from @bv that may be appended to that 216708b25b3SBart Van Assche * bio without exceeding @max_segs 217708b25b3SBart Van Assche * @sectors: [in,out] Number of sectors in the bio being built. Incremented 218708b25b3SBart Van Assche * by the number of sectors from @bv that may be appended to that 219708b25b3SBart Van Assche * bio without exceeding @max_sectors 220708b25b3SBart Van Assche * @max_segs: [in] upper bound for *@nsegs 221708b25b3SBart Van Assche * @max_sectors: [in] upper bound for *@sectors 222708b25b3SBart Van Assche * 223708b25b3SBart Van Assche * When splitting a bio, it can happen that a bvec is encountered that is too 224708b25b3SBart Van Assche * big to fit in a single segment and hence that it has to be split in the 225708b25b3SBart Van Assche * middle. This function verifies whether or not that should happen. The value 226708b25b3SBart Van Assche * %true is returned if and only if appending the entire @bv to a bio with 227708b25b3SBart Van Assche * *@nsegs segments and *@sectors sectors would make that bio unacceptable for 228708b25b3SBart Van Assche * the block driver. 229dcebd755SMing Lei */ 230af2c68feSBart Van Assche static bool bvec_split_segs(const struct request_queue *q, 231af2c68feSBart Van Assche const struct bio_vec *bv, unsigned *nsegs, 232708b25b3SBart Van Assche unsigned *sectors, unsigned max_segs, 233708b25b3SBart Van Assche unsigned max_sectors) 234dcebd755SMing Lei { 235708b25b3SBart Van Assche unsigned max_len = (min(max_sectors, UINT_MAX >> 9) - *sectors) << 9; 236708b25b3SBart Van Assche unsigned len = min(bv->bv_len, max_len); 237dcebd755SMing Lei unsigned total_len = 0; 238ff9811b3SBart Van Assche unsigned seg_size = 0; 239dcebd755SMing Lei 240ff9811b3SBart Van Assche while (len && *nsegs < max_segs) { 241429120f3SMing Lei seg_size = get_max_segment_size(q, bv->bv_page, 242429120f3SMing Lei bv->bv_offset + total_len); 243dcebd755SMing Lei seg_size = min(seg_size, len); 244dcebd755SMing Lei 245ff9811b3SBart Van Assche (*nsegs)++; 246dcebd755SMing Lei total_len += seg_size; 247dcebd755SMing Lei len -= seg_size; 248dcebd755SMing Lei 249dcebd755SMing Lei if ((bv->bv_offset + total_len) & queue_virt_boundary(q)) 250dcebd755SMing Lei break; 251dcebd755SMing Lei } 252dcebd755SMing Lei 253dcebd755SMing Lei *sectors += total_len >> 9; 254dcebd755SMing Lei 255708b25b3SBart Van Assche /* tell the caller to split the bvec if it is too big to fit */ 256708b25b3SBart Van Assche return len > 0 || bv->bv_len > max_len; 257dcebd755SMing Lei } 258dcebd755SMing Lei 259dad77584SBart Van Assche /** 260dad77584SBart Van Assche * blk_bio_segment_split - split a bio in two bios 261dad77584SBart Van Assche * @q: [in] request queue pointer 262dad77584SBart Van Assche * @bio: [in] bio to be split 263dad77584SBart Van Assche * @bs: [in] bio set to allocate the clone from 264dad77584SBart Van Assche * @segs: [out] number of segments in the bio with the first half of the sectors 265dad77584SBart Van Assche * 266dad77584SBart Van Assche * Clone @bio, update the bi_iter of the clone to represent the first sectors 267dad77584SBart Van Assche * of @bio and update @bio->bi_iter to represent the remaining sectors. The 268dad77584SBart Van Assche * following is guaranteed for the cloned bio: 269dad77584SBart Van Assche * - That it has at most get_max_io_size(@q, @bio) sectors. 270dad77584SBart Van Assche * - That it has at most queue_max_segments(@q) segments. 271dad77584SBart Van Assche * 272dad77584SBart Van Assche * Except for discard requests the cloned bio will point at the bi_io_vec of 273dad77584SBart Van Assche * the original bio. It is the responsibility of the caller to ensure that the 274dad77584SBart Van Assche * original bio is not freed before the cloned bio. The caller is also 275dad77584SBart Van Assche * responsible for ensuring that @bs is only destroyed after processing of the 276dad77584SBart Van Assche * split bio has finished. 277dad77584SBart Van Assche */ 27854efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 27954efd50bSKent Overstreet struct bio *bio, 280bdced438SMing Lei struct bio_set *bs, 281bdced438SMing Lei unsigned *segs) 28254efd50bSKent Overstreet { 2835014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 28454efd50bSKent Overstreet struct bvec_iter iter; 2856869875fSChristoph Hellwig unsigned nsegs = 0, sectors = 0; 286d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 28705b700baSMing Lei const unsigned max_segs = queue_max_segments(q); 28854efd50bSKent Overstreet 289dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 29054efd50bSKent Overstreet /* 29154efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 29254efd50bSKent Overstreet * offset would create a gap, disallow it. 29354efd50bSKent Overstreet */ 2945014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 29554efd50bSKent Overstreet goto split; 29654efd50bSKent Overstreet 29705b700baSMing Lei if (nsegs < max_segs && 298708b25b3SBart Van Assche sectors + (bv.bv_len >> 9) <= max_sectors && 299708b25b3SBart Van Assche bv.bv_offset + bv.bv_len <= PAGE_SIZE) { 300708b25b3SBart Van Assche nsegs++; 301708b25b3SBart Van Assche sectors += bv.bv_len >> 9; 302708b25b3SBart Van Assche } else if (bvec_split_segs(q, &bv, &nsegs, §ors, max_segs, 303708b25b3SBart Van Assche max_sectors)) { 304e36f6204SKeith Busch goto split; 305e36f6204SKeith Busch } 306e36f6204SKeith Busch 30754efd50bSKent Overstreet bvprv = bv; 308578270bfSMing Lei bvprvp = &bvprv; 30954efd50bSKent Overstreet } 31054efd50bSKent Overstreet 311d627065dSChristoph Hellwig *segs = nsegs; 312d627065dSChristoph Hellwig return NULL; 31354efd50bSKent Overstreet split: 314bdced438SMing Lei *segs = nsegs; 315cc29e1bfSJeffle Xu 316cc29e1bfSJeffle Xu /* 317cc29e1bfSJeffle Xu * Bio splitting may cause subtle trouble such as hang when doing sync 318cc29e1bfSJeffle Xu * iopoll in direct IO routine. Given performance gain of iopoll for 319cc29e1bfSJeffle Xu * big IO can be trival, disable iopoll when split needed. 320cc29e1bfSJeffle Xu */ 3216ce913feSChristoph Hellwig bio_clear_polled(bio); 322d627065dSChristoph Hellwig return bio_split(bio, sectors, GFP_NOIO, bs); 32354efd50bSKent Overstreet } 32454efd50bSKent Overstreet 325dad77584SBart Van Assche /** 326dad77584SBart Van Assche * __blk_queue_split - split a bio and submit the second half 327abd45c15SJens Axboe * @q: [in] request_queue new bio is being queued at 328dad77584SBart Van Assche * @bio: [in, out] bio to be split 329dad77584SBart Van Assche * @nr_segs: [out] number of segments in the first bio 330dad77584SBart Van Assche * 331dad77584SBart Van Assche * Split a bio into two bios, chain the two bios, submit the second half and 332dad77584SBart Van Assche * store a pointer to the first half in *@bio. If the second bio is still too 333dad77584SBart Van Assche * big it will be split by a recursive call to this function. Since this 334309dca30SChristoph Hellwig * function may allocate a new bio from q->bio_split, it is the responsibility 335309dca30SChristoph Hellwig * of the caller to ensure that q->bio_split is only released after processing 336309dca30SChristoph Hellwig * of the split bio has finished. 337dad77584SBart Van Assche */ 338abd45c15SJens Axboe void __blk_queue_split(struct request_queue *q, struct bio **bio, 339abd45c15SJens Axboe unsigned int *nr_segs) 34054efd50bSKent Overstreet { 341fa532287SChristoph Hellwig struct bio *split = NULL; 34254efd50bSKent Overstreet 3437afafc8aSAdrian Hunter switch (bio_op(*bio)) { 3447afafc8aSAdrian Hunter case REQ_OP_DISCARD: 3457afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 34614ccb66bSChristoph Hellwig split = blk_bio_discard_split(q, *bio, &q->bio_split, nr_segs); 3477afafc8aSAdrian Hunter break; 348a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 34914ccb66bSChristoph Hellwig split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, 35014ccb66bSChristoph Hellwig nr_segs); 351a6f0788eSChaitanya Kulkarni break; 3527afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 35314ccb66bSChristoph Hellwig split = blk_bio_write_same_split(q, *bio, &q->bio_split, 35414ccb66bSChristoph Hellwig nr_segs); 3557afafc8aSAdrian Hunter break; 3567afafc8aSAdrian Hunter default: 35714ccb66bSChristoph Hellwig split = blk_bio_segment_split(q, *bio, &q->bio_split, nr_segs); 3587afafc8aSAdrian Hunter break; 3597afafc8aSAdrian Hunter } 360bdced438SMing Lei 36154efd50bSKent Overstreet if (split) { 3626ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 3631eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 3646ac45aebSMing Lei 36554efd50bSKent Overstreet bio_chain(split, *bio); 366eb6f7f7cSChristoph Hellwig trace_block_split(split, (*bio)->bi_iter.bi_sector); 367ed00aabdSChristoph Hellwig submit_bio_noacct(*bio); 36854efd50bSKent Overstreet *bio = split; 3694f1e9630SChunguang Xu 3704f1e9630SChunguang Xu blk_throtl_charge_bio_split(*bio); 37154efd50bSKent Overstreet } 37254efd50bSKent Overstreet } 37314ccb66bSChristoph Hellwig 374dad77584SBart Van Assche /** 375dad77584SBart Van Assche * blk_queue_split - split a bio and submit the second half 376dad77584SBart Van Assche * @bio: [in, out] bio to be split 377dad77584SBart Van Assche * 378dad77584SBart Van Assche * Split a bio into two bios, chains the two bios, submit the second half and 379dad77584SBart Van Assche * store a pointer to the first half in *@bio. Since this function may allocate 380309dca30SChristoph Hellwig * a new bio from q->bio_split, it is the responsibility of the caller to ensure 381309dca30SChristoph Hellwig * that q->bio_split is only released after processing of the split bio has 382309dca30SChristoph Hellwig * finished. 383dad77584SBart Van Assche */ 384f695ca38SChristoph Hellwig void blk_queue_split(struct bio **bio) 38514ccb66bSChristoph Hellwig { 386abd45c15SJens Axboe struct request_queue *q = (*bio)->bi_bdev->bd_disk->queue; 38714ccb66bSChristoph Hellwig unsigned int nr_segs; 38814ccb66bSChristoph Hellwig 389abd45c15SJens Axboe if (blk_may_split(q, *bio)) 390abd45c15SJens Axboe __blk_queue_split(q, bio, &nr_segs); 39114ccb66bSChristoph Hellwig } 39254efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 39354efd50bSKent Overstreet 394e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq) 395d6d48196SJens Axboe { 3966869875fSChristoph Hellwig unsigned int nr_phys_segs = 0; 397ff9811b3SBart Van Assche unsigned int nr_sectors = 0; 398e9cd19c0SChristoph Hellwig struct req_iterator iter; 3996869875fSChristoph Hellwig struct bio_vec bv; 400d6d48196SJens Axboe 401e9cd19c0SChristoph Hellwig if (!rq->bio) 4021e428079SJens Axboe return 0; 403d6d48196SJens Axboe 404e9cd19c0SChristoph Hellwig switch (bio_op(rq->bio)) { 405a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 406a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 407a958937fSDavid Jeffery if (queue_max_discard_segments(rq->q) > 1) { 408a958937fSDavid Jeffery struct bio *bio = rq->bio; 409a958937fSDavid Jeffery 410a958937fSDavid Jeffery for_each_bio(bio) 411a958937fSDavid Jeffery nr_phys_segs++; 412a958937fSDavid Jeffery return nr_phys_segs; 413a958937fSDavid Jeffery } 414a958937fSDavid Jeffery return 1; 415a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 416f9d03f96SChristoph Hellwig return 0; 417f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 4185cb8850cSKent Overstreet return 1; 419a6f0788eSChaitanya Kulkarni } 4205cb8850cSKent Overstreet 421e9cd19c0SChristoph Hellwig rq_for_each_bvec(bv, rq, iter) 422ff9811b3SBart Van Assche bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors, 423708b25b3SBart Van Assche UINT_MAX, UINT_MAX); 4241e428079SJens Axboe return nr_phys_segs; 4251e428079SJens Axboe } 4261e428079SJens Axboe 42748d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, 428862e5a5eSMing Lei struct scatterlist *sglist) 429862e5a5eSMing Lei { 430862e5a5eSMing Lei if (!*sg) 431862e5a5eSMing Lei return sglist; 432862e5a5eSMing Lei 433862e5a5eSMing Lei /* 434862e5a5eSMing Lei * If the driver previously mapped a shorter list, we could see a 435862e5a5eSMing Lei * termination bit prematurely unless it fully inits the sg table 436862e5a5eSMing Lei * on each mapping. We KNOW that there must be more entries here 437862e5a5eSMing Lei * or the driver would be buggy, so force clear the termination bit 438862e5a5eSMing Lei * to avoid doing a full sg_init_table() in drivers for each command. 439862e5a5eSMing Lei */ 440862e5a5eSMing Lei sg_unmark_end(*sg); 441862e5a5eSMing Lei return sg_next(*sg); 442862e5a5eSMing Lei } 443862e5a5eSMing Lei 444862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q, 445862e5a5eSMing Lei struct bio_vec *bvec, struct scatterlist *sglist, 446862e5a5eSMing Lei struct scatterlist **sg) 447862e5a5eSMing Lei { 448862e5a5eSMing Lei unsigned nbytes = bvec->bv_len; 4498a96a0e4SChristoph Hellwig unsigned nsegs = 0, total = 0; 450862e5a5eSMing Lei 451862e5a5eSMing Lei while (nbytes > 0) { 4528a96a0e4SChristoph Hellwig unsigned offset = bvec->bv_offset + total; 453429120f3SMing Lei unsigned len = min(get_max_segment_size(q, bvec->bv_page, 454429120f3SMing Lei offset), nbytes); 455f9f76879SChristoph Hellwig struct page *page = bvec->bv_page; 456f9f76879SChristoph Hellwig 457f9f76879SChristoph Hellwig /* 458f9f76879SChristoph Hellwig * Unfortunately a fair number of drivers barf on scatterlists 459f9f76879SChristoph Hellwig * that have an offset larger than PAGE_SIZE, despite other 460f9f76879SChristoph Hellwig * subsystems dealing with that invariant just fine. For now 461f9f76879SChristoph Hellwig * stick to the legacy format where we never present those from 462f9f76879SChristoph Hellwig * the block layer, but the code below should be removed once 463f9f76879SChristoph Hellwig * these offenders (mostly MMC/SD drivers) are fixed. 464f9f76879SChristoph Hellwig */ 465f9f76879SChristoph Hellwig page += (offset >> PAGE_SHIFT); 466f9f76879SChristoph Hellwig offset &= ~PAGE_MASK; 467862e5a5eSMing Lei 468862e5a5eSMing Lei *sg = blk_next_sg(sg, sglist); 469f9f76879SChristoph Hellwig sg_set_page(*sg, page, len, offset); 470862e5a5eSMing Lei 4718a96a0e4SChristoph Hellwig total += len; 4728a96a0e4SChristoph Hellwig nbytes -= len; 473862e5a5eSMing Lei nsegs++; 474862e5a5eSMing Lei } 475862e5a5eSMing Lei 476862e5a5eSMing Lei return nsegs; 477862e5a5eSMing Lei } 478862e5a5eSMing Lei 47916e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv, 48016e3e418SMing Lei struct scatterlist *sglist, struct scatterlist **sg) 48116e3e418SMing Lei { 48216e3e418SMing Lei *sg = blk_next_sg(sg, sglist); 48316e3e418SMing Lei sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 48416e3e418SMing Lei return 1; 48516e3e418SMing Lei } 48616e3e418SMing Lei 487f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */ 488f6970f83SMing Lei static inline bool 489f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec, 490f6970f83SMing Lei struct bio_vec *bvprv, struct scatterlist **sg) 491963ab9e5SAsias He { 492963ab9e5SAsias He 493963ab9e5SAsias He int nbytes = bvec->bv_len; 494963ab9e5SAsias He 495f6970f83SMing Lei if (!*sg) 496f6970f83SMing Lei return false; 497f6970f83SMing Lei 498b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 499f6970f83SMing Lei return false; 500f6970f83SMing Lei 5013dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec)) 502f6970f83SMing Lei return false; 503963ab9e5SAsias He 504963ab9e5SAsias He (*sg)->length += nbytes; 505f6970f83SMing Lei 506f6970f83SMing Lei return true; 507963ab9e5SAsias He } 508963ab9e5SAsias He 5095cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 5105cb8850cSKent Overstreet struct scatterlist *sglist, 5115cb8850cSKent Overstreet struct scatterlist **sg) 5125cb8850cSKent Overstreet { 5133f649ab7SKees Cook struct bio_vec bvec, bvprv = { NULL }; 5145cb8850cSKent Overstreet struct bvec_iter iter; 51538417468SChristoph Hellwig int nsegs = 0; 516f6970f83SMing Lei bool new_bio = false; 5175cb8850cSKent Overstreet 518f6970f83SMing Lei for_each_bio(bio) { 519f6970f83SMing Lei bio_for_each_bvec(bvec, bio, iter) { 520f6970f83SMing Lei /* 521f6970f83SMing Lei * Only try to merge bvecs from two bios given we 522f6970f83SMing Lei * have done bio internal merge when adding pages 523f6970f83SMing Lei * to bio 524f6970f83SMing Lei */ 525f6970f83SMing Lei if (new_bio && 526f6970f83SMing Lei __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg)) 527f6970f83SMing Lei goto next_bvec; 528f6970f83SMing Lei 529f6970f83SMing Lei if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE) 530f6970f83SMing Lei nsegs += __blk_bvec_map_sg(bvec, sglist, sg); 531f6970f83SMing Lei else 532f6970f83SMing Lei nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg); 533f6970f83SMing Lei next_bvec: 534f6970f83SMing Lei new_bio = false; 535f6970f83SMing Lei } 536b21e11c5SMing Lei if (likely(bio->bi_iter.bi_size)) { 537f6970f83SMing Lei bvprv = bvec; 538f6970f83SMing Lei new_bio = true; 539f6970f83SMing Lei } 540b21e11c5SMing Lei } 5415cb8850cSKent Overstreet 5425cb8850cSKent Overstreet return nsegs; 5435cb8850cSKent Overstreet } 5445cb8850cSKent Overstreet 545d6d48196SJens Axboe /* 546d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 547d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 548d6d48196SJens Axboe */ 54989de1504SChristoph Hellwig int __blk_rq_map_sg(struct request_queue *q, struct request *rq, 55089de1504SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **last_sg) 551d6d48196SJens Axboe { 5525cb8850cSKent Overstreet int nsegs = 0; 553d6d48196SJens Axboe 554f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 55589de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg); 556f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 55789de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, last_sg); 558f9d03f96SChristoph Hellwig else if (rq->bio) 55989de1504SChristoph Hellwig nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg); 560f18573abSFUJITA Tomonori 56189de1504SChristoph Hellwig if (*last_sg) 56289de1504SChristoph Hellwig sg_mark_end(*last_sg); 563d6d48196SJens Axboe 56412e57f59SMing Lei /* 56512e57f59SMing Lei * Something must have been wrong if the figured number of 56612e57f59SMing Lei * segment is bigger than number of req's physical segments 56712e57f59SMing Lei */ 568f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 56912e57f59SMing Lei 570d6d48196SJens Axboe return nsegs; 571d6d48196SJens Axboe } 57289de1504SChristoph Hellwig EXPORT_SYMBOL(__blk_rq_map_sg); 573d6d48196SJens Axboe 574943b40c8SMing Lei static inline unsigned int blk_rq_get_max_segments(struct request *rq) 575943b40c8SMing Lei { 576943b40c8SMing Lei if (req_op(rq) == REQ_OP_DISCARD) 577943b40c8SMing Lei return queue_max_discard_segments(rq->q); 578943b40c8SMing Lei return queue_max_segments(rq->q); 579943b40c8SMing Lei } 580943b40c8SMing Lei 581badf7f64SChristoph Hellwig static inline unsigned int blk_rq_get_max_sectors(struct request *rq, 582badf7f64SChristoph Hellwig sector_t offset) 583badf7f64SChristoph Hellwig { 584badf7f64SChristoph Hellwig struct request_queue *q = rq->q; 585badf7f64SChristoph Hellwig 586badf7f64SChristoph Hellwig if (blk_rq_is_passthrough(rq)) 587badf7f64SChristoph Hellwig return q->limits.max_hw_sectors; 588badf7f64SChristoph Hellwig 589badf7f64SChristoph Hellwig if (!q->limits.chunk_sectors || 590badf7f64SChristoph Hellwig req_op(rq) == REQ_OP_DISCARD || 591badf7f64SChristoph Hellwig req_op(rq) == REQ_OP_SECURE_ERASE) 592badf7f64SChristoph Hellwig return blk_queue_get_max_sectors(q, req_op(rq)); 593badf7f64SChristoph Hellwig 594badf7f64SChristoph Hellwig return min(blk_max_size_offset(q, offset, 0), 595badf7f64SChristoph Hellwig blk_queue_get_max_sectors(q, req_op(rq))); 596badf7f64SChristoph Hellwig } 597badf7f64SChristoph Hellwig 59814ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio, 59914ccb66bSChristoph Hellwig unsigned int nr_phys_segs) 600d6d48196SJens Axboe { 6012705dfb2SMing Lei if (blk_integrity_merge_bio(req->q, req, bio) == false) 60213f05c8dSMartin K. Petersen goto no_merge; 60313f05c8dSMartin K. Petersen 6042705dfb2SMing Lei /* discard request merge won't add new segment */ 6052705dfb2SMing Lei if (req_op(req) == REQ_OP_DISCARD) 6062705dfb2SMing Lei return 1; 6072705dfb2SMing Lei 6082705dfb2SMing Lei if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req)) 60913f05c8dSMartin K. Petersen goto no_merge; 610d6d48196SJens Axboe 611d6d48196SJens Axboe /* 612d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 613d6d48196SJens Axboe * counters. 614d6d48196SJens Axboe */ 615d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 616d6d48196SJens Axboe return 1; 61713f05c8dSMartin K. Petersen 61813f05c8dSMartin K. Petersen no_merge: 61914ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 62013f05c8dSMartin K. Petersen return 0; 621d6d48196SJens Axboe } 622d6d48196SJens Axboe 62314ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs) 624d6d48196SJens Axboe { 6255e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 6265e7c4274SJens Axboe return 0; 6277f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6287f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 6297f39add3SSagi Grimberg return 0; 630a892c8d5SSatya Tangirala if (!bio_crypt_ctx_back_mergeable(req, bio)) 631a892c8d5SSatya Tangirala return 0; 632f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 63317007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 63414ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 635d6d48196SJens Axboe return 0; 636d6d48196SJens Axboe } 637d6d48196SJens Axboe 63814ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 639d6d48196SJens Axboe } 640d6d48196SJens Axboe 641eda5cc99SChristoph Hellwig static int ll_front_merge_fn(struct request *req, struct bio *bio, 642eda5cc99SChristoph Hellwig unsigned int nr_segs) 643d6d48196SJens 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; 649a892c8d5SSatya Tangirala if (!bio_crypt_ctx_front_mergeable(req, bio)) 650a892c8d5SSatya Tangirala return 0; 651f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 65217007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 65314ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 654d6d48196SJens Axboe return 0; 655d6d48196SJens Axboe } 656d6d48196SJens Axboe 65714ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 658d6d48196SJens Axboe } 659d6d48196SJens Axboe 660445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 661445251d0SJens Axboe struct request *next) 662445251d0SJens Axboe { 663445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 664445251d0SJens Axboe 665445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 666445251d0SJens Axboe goto no_merge; 667445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 668445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 669445251d0SJens Axboe goto no_merge; 670445251d0SJens Axboe 671445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 672445251d0SJens Axboe return true; 673445251d0SJens Axboe no_merge: 674445251d0SJens Axboe req_set_nomerge(q, req); 675445251d0SJens Axboe return false; 676445251d0SJens Axboe } 677445251d0SJens Axboe 678d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 679d6d48196SJens Axboe struct request *next) 680d6d48196SJens Axboe { 681d6d48196SJens Axboe int total_phys_segments; 682d6d48196SJens Axboe 6835e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 684854fbb9cSKeith Busch return 0; 685854fbb9cSKeith Busch 686d6d48196SJens Axboe /* 687d6d48196SJens Axboe * Will it become too large? 688d6d48196SJens Axboe */ 689f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 69017007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 691d6d48196SJens Axboe return 0; 692d6d48196SJens Axboe 693d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 694943b40c8SMing Lei if (total_phys_segments > blk_rq_get_max_segments(req)) 695d6d48196SJens Axboe return 0; 696d6d48196SJens Axboe 6974eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 69813f05c8dSMartin K. Petersen return 0; 69913f05c8dSMartin K. Petersen 700a892c8d5SSatya Tangirala if (!bio_crypt_ctx_merge_rq(req, next)) 701a892c8d5SSatya Tangirala return 0; 702a892c8d5SSatya Tangirala 703d6d48196SJens Axboe /* Merge is OK... */ 704d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 705d6d48196SJens Axboe return 1; 706d6d48196SJens Axboe } 707d6d48196SJens Axboe 70880a761fdSTejun Heo /** 70980a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 71080a761fdSTejun Heo * @rq: request to mark as mixed merge 71180a761fdSTejun Heo * 71280a761fdSTejun Heo * Description: 71380a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 71480a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 71580a761fdSTejun Heo * merged. 71680a761fdSTejun Heo */ 71780a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 71880a761fdSTejun Heo { 71980a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 72080a761fdSTejun Heo struct bio *bio; 72180a761fdSTejun Heo 722e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 72380a761fdSTejun Heo return; 72480a761fdSTejun Heo 72580a761fdSTejun Heo /* 72680a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 72780a761fdSTejun Heo * contained bios. It will just track those of the first one. 72880a761fdSTejun Heo * Distributes the attributs to each bio. 72980a761fdSTejun Heo */ 73080a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 7311eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 7321eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 7331eff9d32SJens Axboe bio->bi_opf |= ff; 73480a761fdSTejun Heo } 735e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 73680a761fdSTejun Heo } 73780a761fdSTejun Heo 738b9c54f56SKonstantin Khlebnikov static void blk_account_io_merge_request(struct request *req) 73926308eabSJerome Marchand { 74026308eabSJerome Marchand if (blk_do_io_stat(req)) { 741112f158fSMike Snitzer part_stat_lock(); 742b9c54f56SKonstantin Khlebnikov part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); 74326308eabSJerome Marchand part_stat_unlock(); 74426308eabSJerome Marchand } 74526308eabSJerome Marchand } 746b9c54f56SKonstantin Khlebnikov 747e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req, 748e96c0d83SEric Biggers struct request *next) 74969840466SJianchao Wang { 75069840466SJianchao Wang if (blk_discard_mergable(req)) 75169840466SJianchao Wang return ELEVATOR_DISCARD_MERGE; 75269840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) 75369840466SJianchao Wang return ELEVATOR_BACK_MERGE; 75469840466SJianchao Wang 75569840466SJianchao Wang return ELEVATOR_NO_MERGE; 75669840466SJianchao Wang } 75726308eabSJerome Marchand 758badf7f64SChristoph Hellwig static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b) 759badf7f64SChristoph Hellwig { 760badf7f64SChristoph Hellwig if (bio_page(a) == bio_page(b) && bio_offset(a) == bio_offset(b)) 761badf7f64SChristoph Hellwig return true; 762badf7f64SChristoph Hellwig return false; 763badf7f64SChristoph Hellwig } 764badf7f64SChristoph Hellwig 765d6d48196SJens Axboe /* 766b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 767b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 768d6d48196SJens Axboe */ 769b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 770b973cb7eSJens Axboe struct request *req, struct request *next) 771d6d48196SJens Axboe { 772d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 773b973cb7eSJens Axboe return NULL; 774d6d48196SJens Axboe 775288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 776b973cb7eSJens Axboe return NULL; 777f31dc1cdSMartin K. Petersen 778d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 7792081a56bSJens Axboe || req->rq_disk != next->rq_disk) 780b973cb7eSJens Axboe return NULL; 781d6d48196SJens Axboe 7828fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 7834363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 784b973cb7eSJens Axboe return NULL; 7854363ac7cSMartin K. Petersen 786d6d48196SJens Axboe /* 787cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 788cb6934f8SJens Axboe * non-hint IO. 789cb6934f8SJens Axboe */ 790cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 791cb6934f8SJens Axboe return NULL; 792cb6934f8SJens Axboe 793668ffc03SDamien Le Moal if (req->ioprio != next->ioprio) 794668ffc03SDamien Le Moal return NULL; 795668ffc03SDamien Le Moal 796cb6934f8SJens Axboe /* 797d6d48196SJens Axboe * If we are allowed to merge, then append bio list 798d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 799d6d48196SJens Axboe * will have updated segment counts, update sector 800445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 801445251d0SJens Axboe * have separate settings. 802d6d48196SJens Axboe */ 80369840466SJianchao Wang 80469840466SJianchao Wang switch (blk_try_req_merge(req, next)) { 80569840466SJianchao Wang case ELEVATOR_DISCARD_MERGE: 806445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 807445251d0SJens Axboe return NULL; 80869840466SJianchao Wang break; 80969840466SJianchao Wang case ELEVATOR_BACK_MERGE: 81069840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next)) 811b973cb7eSJens Axboe return NULL; 81269840466SJianchao Wang break; 81369840466SJianchao Wang default: 81469840466SJianchao Wang return NULL; 81569840466SJianchao Wang } 816d6d48196SJens Axboe 817d6d48196SJens Axboe /* 81880a761fdSTejun Heo * If failfast settings disagree or any of the two is already 81980a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 82080a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 82180a761fdSTejun Heo * set properly. 82280a761fdSTejun Heo */ 823e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 82480a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 82580a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 82680a761fdSTejun Heo blk_rq_set_mixed_merge(req); 82780a761fdSTejun Heo blk_rq_set_mixed_merge(next); 82880a761fdSTejun Heo } 82980a761fdSTejun Heo 83080a761fdSTejun Heo /* 831522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 832522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 833522a7775SOmar Sandoval * current request for accounting purposes. 834d6d48196SJens Axboe */ 835522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 836522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 837d6d48196SJens Axboe 838d6d48196SJens Axboe req->biotail->bi_next = next->bio; 839d6d48196SJens Axboe req->biotail = next->biotail; 840d6d48196SJens Axboe 841a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 842d6d48196SJens Axboe 8432a5cf35cSMing Lei if (!blk_discard_mergable(req)) 844d6d48196SJens Axboe elv_merge_requests(q, req, next); 845d6d48196SJens Axboe 84642dad764SJerome Marchand /* 84742dad764SJerome Marchand * 'next' is going away, so update stats accordingly 84842dad764SJerome Marchand */ 849b9c54f56SKonstantin Khlebnikov blk_account_io_merge_request(next); 850d6d48196SJens Axboe 851a54895faSChristoph Hellwig trace_block_rq_merge(next); 852f3bdc62fSJan Kara 853e4d750c9SJens Axboe /* 854e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 855e4d750c9SJens Axboe * the caller to free 856e4d750c9SJens Axboe */ 8571cd96c24SBoaz Harrosh next->bio = NULL; 858b973cb7eSJens Axboe return next; 859d6d48196SJens Axboe } 860d6d48196SJens Axboe 861eda5cc99SChristoph Hellwig static struct request *attempt_back_merge(struct request_queue *q, 862eda5cc99SChristoph Hellwig struct request *rq) 863d6d48196SJens Axboe { 864d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 865d6d48196SJens Axboe 866d6d48196SJens Axboe if (next) 867d6d48196SJens Axboe return attempt_merge(q, rq, next); 868d6d48196SJens Axboe 869b973cb7eSJens Axboe return NULL; 870d6d48196SJens Axboe } 871d6d48196SJens Axboe 872eda5cc99SChristoph Hellwig static struct request *attempt_front_merge(struct request_queue *q, 873eda5cc99SChristoph Hellwig struct request *rq) 874d6d48196SJens Axboe { 875d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 876d6d48196SJens Axboe 877d6d48196SJens Axboe if (prev) 878d6d48196SJens Axboe return attempt_merge(q, prev, rq); 879d6d48196SJens Axboe 880b973cb7eSJens Axboe return NULL; 881d6d48196SJens Axboe } 8825e84ea3aSJens Axboe 883fd2ef39cSJan Kara /* 884fd2ef39cSJan Kara * Try to merge 'next' into 'rq'. Return true if the merge happened, false 885fd2ef39cSJan Kara * otherwise. The caller is responsible for freeing 'next' if the merge 886fd2ef39cSJan Kara * happened. 887fd2ef39cSJan Kara */ 888fd2ef39cSJan Kara bool blk_attempt_req_merge(struct request_queue *q, struct request *rq, 8895e84ea3aSJens Axboe struct request *next) 8905e84ea3aSJens Axboe { 891fd2ef39cSJan Kara return attempt_merge(q, rq, next); 8925e84ea3aSJens Axboe } 893050c8ea8STejun Heo 894050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 895050c8ea8STejun Heo { 896e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 897050c8ea8STejun Heo return false; 898050c8ea8STejun Heo 899288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 900f31dc1cdSMartin K. Petersen return false; 901f31dc1cdSMartin K. Petersen 902050c8ea8STejun Heo /* different data direction or already started, don't merge */ 903050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 904050c8ea8STejun Heo return false; 905050c8ea8STejun Heo 9062081a56bSJens Axboe /* must be same device */ 907309dca30SChristoph Hellwig if (rq->rq_disk != bio->bi_bdev->bd_disk) 908050c8ea8STejun Heo return false; 909050c8ea8STejun Heo 910050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 9114eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 912050c8ea8STejun Heo return false; 913050c8ea8STejun Heo 914a892c8d5SSatya Tangirala /* Only merge if the crypt contexts are compatible */ 915a892c8d5SSatya Tangirala if (!bio_crypt_rq_ctx_compatible(rq, bio)) 916a892c8d5SSatya Tangirala return false; 917a892c8d5SSatya Tangirala 9184363ac7cSMartin K. Petersen /* must be using the same buffer */ 9198fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 9204363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 9214363ac7cSMartin K. Petersen return false; 9224363ac7cSMartin K. Petersen 923cb6934f8SJens Axboe /* 924cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 925cb6934f8SJens Axboe * non-hint IO. 926cb6934f8SJens Axboe */ 927cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 928cb6934f8SJens Axboe return false; 929cb6934f8SJens Axboe 930668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio)) 931668ffc03SDamien Le Moal return false; 932668ffc03SDamien Le Moal 933050c8ea8STejun Heo return true; 934050c8ea8STejun Heo } 935050c8ea8STejun Heo 93634fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 937050c8ea8STejun Heo { 93869840466SJianchao Wang if (blk_discard_mergable(rq)) 9391e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 9401e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 941050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 9424f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 943050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 944050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 945050c8ea8STejun Heo } 9468e756373SBaolin Wang 9478e756373SBaolin Wang static void blk_account_io_merge_bio(struct request *req) 9488e756373SBaolin Wang { 9498e756373SBaolin Wang if (!blk_do_io_stat(req)) 9508e756373SBaolin Wang return; 9518e756373SBaolin Wang 9528e756373SBaolin Wang part_stat_lock(); 9538e756373SBaolin Wang part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); 9548e756373SBaolin Wang part_stat_unlock(); 9558e756373SBaolin Wang } 9568e756373SBaolin Wang 957eda5cc99SChristoph Hellwig enum bio_merge_status { 958eda5cc99SChristoph Hellwig BIO_MERGE_OK, 959eda5cc99SChristoph Hellwig BIO_MERGE_NONE, 960eda5cc99SChristoph Hellwig BIO_MERGE_FAILED, 961eda5cc99SChristoph Hellwig }; 962eda5cc99SChristoph Hellwig 963eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_back_merge(struct request *req, 964eda5cc99SChristoph Hellwig struct bio *bio, unsigned int nr_segs) 9658e756373SBaolin Wang { 9668e756373SBaolin Wang const int ff = bio->bi_opf & REQ_FAILFAST_MASK; 9678e756373SBaolin Wang 9688e756373SBaolin Wang if (!ll_back_merge_fn(req, bio, nr_segs)) 9697d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 9708e756373SBaolin Wang 971e8a676d6SChristoph Hellwig trace_block_bio_backmerge(bio); 9728e756373SBaolin Wang rq_qos_merge(req->q, req, bio); 9738e756373SBaolin Wang 9748e756373SBaolin Wang if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) 9758e756373SBaolin Wang blk_rq_set_mixed_merge(req); 9768e756373SBaolin Wang 9778e756373SBaolin Wang req->biotail->bi_next = bio; 9788e756373SBaolin Wang req->biotail = bio; 9798e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 9808e756373SBaolin Wang 9818e756373SBaolin Wang bio_crypt_free_ctx(bio); 9828e756373SBaolin Wang 9838e756373SBaolin Wang blk_account_io_merge_bio(req); 9847d7ca7c5SBaolin Wang return BIO_MERGE_OK; 9858e756373SBaolin Wang } 9868e756373SBaolin Wang 987eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_front_merge(struct request *req, 988eda5cc99SChristoph Hellwig struct bio *bio, unsigned int nr_segs) 9898e756373SBaolin Wang { 9908e756373SBaolin Wang const int ff = bio->bi_opf & REQ_FAILFAST_MASK; 9918e756373SBaolin Wang 9928e756373SBaolin Wang if (!ll_front_merge_fn(req, bio, nr_segs)) 9937d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 9948e756373SBaolin Wang 995e8a676d6SChristoph Hellwig trace_block_bio_frontmerge(bio); 9968e756373SBaolin Wang rq_qos_merge(req->q, req, bio); 9978e756373SBaolin Wang 9988e756373SBaolin Wang if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) 9998e756373SBaolin Wang blk_rq_set_mixed_merge(req); 10008e756373SBaolin Wang 10018e756373SBaolin Wang bio->bi_next = req->bio; 10028e756373SBaolin Wang req->bio = bio; 10038e756373SBaolin Wang 10048e756373SBaolin Wang req->__sector = bio->bi_iter.bi_sector; 10058e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 10068e756373SBaolin Wang 10078e756373SBaolin Wang bio_crypt_do_front_merge(req, bio); 10088e756373SBaolin Wang 10098e756373SBaolin Wang blk_account_io_merge_bio(req); 10107d7ca7c5SBaolin Wang return BIO_MERGE_OK; 10118e756373SBaolin Wang } 10128e756373SBaolin Wang 1013eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_discard_merge(struct request_queue *q, 1014eda5cc99SChristoph Hellwig struct request *req, struct bio *bio) 10158e756373SBaolin Wang { 10168e756373SBaolin Wang unsigned short segments = blk_rq_nr_discard_segments(req); 10178e756373SBaolin Wang 10188e756373SBaolin Wang if (segments >= queue_max_discard_segments(q)) 10198e756373SBaolin Wang goto no_merge; 10208e756373SBaolin Wang if (blk_rq_sectors(req) + bio_sectors(bio) > 10218e756373SBaolin Wang blk_rq_get_max_sectors(req, blk_rq_pos(req))) 10228e756373SBaolin Wang goto no_merge; 10238e756373SBaolin Wang 10248e756373SBaolin Wang rq_qos_merge(q, req, bio); 10258e756373SBaolin Wang 10268e756373SBaolin Wang req->biotail->bi_next = bio; 10278e756373SBaolin Wang req->biotail = bio; 10288e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 10298e756373SBaolin Wang req->nr_phys_segments = segments + 1; 10308e756373SBaolin Wang 10318e756373SBaolin Wang blk_account_io_merge_bio(req); 10327d7ca7c5SBaolin Wang return BIO_MERGE_OK; 10338e756373SBaolin Wang no_merge: 10348e756373SBaolin Wang req_set_nomerge(q, req); 10357d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 10367d7ca7c5SBaolin Wang } 10377d7ca7c5SBaolin Wang 10387d7ca7c5SBaolin Wang static enum bio_merge_status blk_attempt_bio_merge(struct request_queue *q, 10397d7ca7c5SBaolin Wang struct request *rq, 10407d7ca7c5SBaolin Wang struct bio *bio, 10417d7ca7c5SBaolin Wang unsigned int nr_segs, 10427d7ca7c5SBaolin Wang bool sched_allow_merge) 10437d7ca7c5SBaolin Wang { 10447d7ca7c5SBaolin Wang if (!blk_rq_merge_ok(rq, bio)) 10457d7ca7c5SBaolin Wang return BIO_MERGE_NONE; 10467d7ca7c5SBaolin Wang 10477d7ca7c5SBaolin Wang switch (blk_try_merge(rq, bio)) { 10487d7ca7c5SBaolin Wang case ELEVATOR_BACK_MERGE: 1049265600b7SBaolin Wang if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio)) 10507d7ca7c5SBaolin Wang return bio_attempt_back_merge(rq, bio, nr_segs); 10517d7ca7c5SBaolin Wang break; 10527d7ca7c5SBaolin Wang case ELEVATOR_FRONT_MERGE: 1053265600b7SBaolin Wang if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio)) 10547d7ca7c5SBaolin Wang return bio_attempt_front_merge(rq, bio, nr_segs); 10557d7ca7c5SBaolin Wang break; 10567d7ca7c5SBaolin Wang case ELEVATOR_DISCARD_MERGE: 10577d7ca7c5SBaolin Wang return bio_attempt_discard_merge(q, rq, bio); 10587d7ca7c5SBaolin Wang default: 10597d7ca7c5SBaolin Wang return BIO_MERGE_NONE; 10607d7ca7c5SBaolin Wang } 10617d7ca7c5SBaolin Wang 10627d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 10638e756373SBaolin Wang } 10648e756373SBaolin Wang 10658e756373SBaolin Wang /** 10668e756373SBaolin Wang * blk_attempt_plug_merge - try to merge with %current's plugged list 10678e756373SBaolin Wang * @q: request_queue new bio is being queued at 10688e756373SBaolin Wang * @bio: new bio being queued 10698e756373SBaolin Wang * @nr_segs: number of segments in @bio 1070*87c037d1SJens Axboe * @same_queue_rq: output value, will be true if there's an existing request 1071*87c037d1SJens Axboe * from the passed in @q already in the plug list 10728e756373SBaolin Wang * 1073d38a9c04SJens Axboe * Determine whether @bio being queued on @q can be merged with the previous 1074d38a9c04SJens Axboe * request on %current's plugged list. Returns %true if merge was successful, 10758e756373SBaolin Wang * otherwise %false. 10768e756373SBaolin Wang * 10778e756373SBaolin Wang * Plugging coalesces IOs from the same issuer for the same purpose without 10788e756373SBaolin Wang * going through @q->queue_lock. As such it's more of an issuing mechanism 10798e756373SBaolin Wang * than scheduling, and the request, while may have elvpriv data, is not 10808e756373SBaolin Wang * added on the elevator at this point. In addition, we don't have 10818e756373SBaolin Wang * reliable access to the elevator outside queue lock. Only check basic 10828e756373SBaolin Wang * merging parameters without querying the elevator. 10838e756373SBaolin Wang * 10848e756373SBaolin Wang * Caller must ensure !blk_queue_nomerges(q) beforehand. 10858e756373SBaolin Wang */ 10868e756373SBaolin Wang bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio, 1087*87c037d1SJens Axboe unsigned int nr_segs, bool *same_queue_rq) 10888e756373SBaolin Wang { 10898e756373SBaolin Wang struct blk_plug *plug; 10908e756373SBaolin Wang struct request *rq; 10918e756373SBaolin Wang 10928e756373SBaolin Wang plug = blk_mq_plug(q, bio); 1093d38a9c04SJens Axboe if (!plug || list_empty(&plug->mq_list)) 10948e756373SBaolin Wang return false; 10958e756373SBaolin Wang 1096d38a9c04SJens Axboe /* check the previously added entry for a quick merge attempt */ 1097d38a9c04SJens Axboe rq = list_last_entry(&plug->mq_list, struct request, queuelist); 1098*87c037d1SJens Axboe if (rq->q == q) { 10998e756373SBaolin Wang /* 1100d38a9c04SJens Axboe * Only blk-mq multiple hardware queues case checks the rq in 1101d38a9c04SJens Axboe * the same queue, there should be only one such rq in a queue 1102d38a9c04SJens Axboe */ 1103*87c037d1SJens Axboe *same_queue_rq = true; 11048e756373SBaolin Wang } 1105d38a9c04SJens Axboe if (blk_attempt_bio_merge(q, rq, bio, nr_segs, false) == BIO_MERGE_OK) 11068e756373SBaolin Wang return true; 11078e756373SBaolin Wang return false; 11088e756373SBaolin Wang } 1109bdc6a287SBaolin Wang 1110bdc6a287SBaolin Wang /* 1111bdc6a287SBaolin Wang * Iterate list of requests and see if we can merge this bio with any 1112bdc6a287SBaolin Wang * of them. 1113bdc6a287SBaolin Wang */ 1114bdc6a287SBaolin Wang bool blk_bio_list_merge(struct request_queue *q, struct list_head *list, 1115bdc6a287SBaolin Wang struct bio *bio, unsigned int nr_segs) 1116bdc6a287SBaolin Wang { 1117bdc6a287SBaolin Wang struct request *rq; 1118bdc6a287SBaolin Wang int checked = 8; 1119bdc6a287SBaolin Wang 1120bdc6a287SBaolin Wang list_for_each_entry_reverse(rq, list, queuelist) { 1121bdc6a287SBaolin Wang if (!checked--) 1122bdc6a287SBaolin Wang break; 1123bdc6a287SBaolin Wang 11247d7ca7c5SBaolin Wang switch (blk_attempt_bio_merge(q, rq, bio, nr_segs, true)) { 11257d7ca7c5SBaolin Wang case BIO_MERGE_NONE: 1126bdc6a287SBaolin Wang continue; 11277d7ca7c5SBaolin Wang case BIO_MERGE_OK: 11287d7ca7c5SBaolin Wang return true; 11297d7ca7c5SBaolin Wang case BIO_MERGE_FAILED: 11307d7ca7c5SBaolin Wang return false; 1131bdc6a287SBaolin Wang } 1132bdc6a287SBaolin Wang 1133bdc6a287SBaolin Wang } 1134bdc6a287SBaolin Wang 1135bdc6a287SBaolin Wang return false; 1136bdc6a287SBaolin Wang } 1137bdc6a287SBaolin Wang EXPORT_SYMBOL_GPL(blk_bio_list_merge); 1138eda5cc99SChristoph Hellwig 1139eda5cc99SChristoph Hellwig bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio, 1140eda5cc99SChristoph Hellwig unsigned int nr_segs, struct request **merged_request) 1141eda5cc99SChristoph Hellwig { 1142eda5cc99SChristoph Hellwig struct request *rq; 1143eda5cc99SChristoph Hellwig 1144eda5cc99SChristoph Hellwig switch (elv_merge(q, &rq, bio)) { 1145eda5cc99SChristoph Hellwig case ELEVATOR_BACK_MERGE: 1146eda5cc99SChristoph Hellwig if (!blk_mq_sched_allow_merge(q, rq, bio)) 1147eda5cc99SChristoph Hellwig return false; 1148eda5cc99SChristoph Hellwig if (bio_attempt_back_merge(rq, bio, nr_segs) != BIO_MERGE_OK) 1149eda5cc99SChristoph Hellwig return false; 1150eda5cc99SChristoph Hellwig *merged_request = attempt_back_merge(q, rq); 1151eda5cc99SChristoph Hellwig if (!*merged_request) 1152eda5cc99SChristoph Hellwig elv_merged_request(q, rq, ELEVATOR_BACK_MERGE); 1153eda5cc99SChristoph Hellwig return true; 1154eda5cc99SChristoph Hellwig case ELEVATOR_FRONT_MERGE: 1155eda5cc99SChristoph Hellwig if (!blk_mq_sched_allow_merge(q, rq, bio)) 1156eda5cc99SChristoph Hellwig return false; 1157eda5cc99SChristoph Hellwig if (bio_attempt_front_merge(rq, bio, nr_segs) != BIO_MERGE_OK) 1158eda5cc99SChristoph Hellwig return false; 1159eda5cc99SChristoph Hellwig *merged_request = attempt_front_merge(q, rq); 1160eda5cc99SChristoph Hellwig if (!*merged_request) 1161eda5cc99SChristoph Hellwig elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE); 1162eda5cc99SChristoph Hellwig return true; 1163eda5cc99SChristoph Hellwig case ELEVATOR_DISCARD_MERGE: 1164eda5cc99SChristoph Hellwig return bio_attempt_discard_merge(q, rq, bio) == BIO_MERGE_OK; 1165eda5cc99SChristoph Hellwig default: 1166eda5cc99SChristoph Hellwig return false; 1167eda5cc99SChristoph Hellwig } 1168eda5cc99SChristoph Hellwig } 1169eda5cc99SChristoph Hellwig EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge); 1170