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> 11*82d981d4SChristoph Hellwig #include <linux/part_stat.h> 12d6d48196SJens Axboe 13cda22646SMike Krinkin #include <trace/events/block.h> 14cda22646SMike Krinkin 15d6d48196SJens Axboe #include "blk.h" 162aa7745bSChristoph Hellwig #include "blk-mq-sched.h" 178e756373SBaolin Wang #include "blk-rq-qos.h" 18a7b36ee6SJens Axboe #include "blk-throttle.h" 19d6d48196SJens Axboe 20ff18d77bSChristoph Hellwig static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv) 21ff18d77bSChristoph Hellwig { 22ff18d77bSChristoph Hellwig *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 23ff18d77bSChristoph Hellwig } 24ff18d77bSChristoph Hellwig 25ff18d77bSChristoph Hellwig static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv) 26ff18d77bSChristoph Hellwig { 27ff18d77bSChristoph Hellwig struct bvec_iter iter = bio->bi_iter; 28ff18d77bSChristoph Hellwig int idx; 29ff18d77bSChristoph Hellwig 30ff18d77bSChristoph Hellwig bio_get_first_bvec(bio, bv); 31ff18d77bSChristoph Hellwig if (bv->bv_len == bio->bi_iter.bi_size) 32ff18d77bSChristoph Hellwig return; /* this bio only has a single bvec */ 33ff18d77bSChristoph Hellwig 34ff18d77bSChristoph Hellwig bio_advance_iter(bio, &iter, iter.bi_size); 35ff18d77bSChristoph Hellwig 36ff18d77bSChristoph Hellwig if (!iter.bi_bvec_done) 37ff18d77bSChristoph Hellwig idx = iter.bi_idx - 1; 38ff18d77bSChristoph Hellwig else /* in the middle of bvec */ 39ff18d77bSChristoph Hellwig idx = iter.bi_idx; 40ff18d77bSChristoph Hellwig 41ff18d77bSChristoph Hellwig *bv = bio->bi_io_vec[idx]; 42ff18d77bSChristoph Hellwig 43ff18d77bSChristoph Hellwig /* 44ff18d77bSChristoph Hellwig * iter.bi_bvec_done records actual length of the last bvec 45ff18d77bSChristoph Hellwig * if this bio ends in the middle of one io vector 46ff18d77bSChristoph Hellwig */ 47ff18d77bSChristoph Hellwig if (iter.bi_bvec_done) 48ff18d77bSChristoph Hellwig bv->bv_len = iter.bi_bvec_done; 49ff18d77bSChristoph Hellwig } 50ff18d77bSChristoph Hellwig 51e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q, 52e9907009SChristoph Hellwig struct request *prev_rq, struct bio *prev, struct bio *next) 53e9907009SChristoph Hellwig { 54e9907009SChristoph Hellwig struct bio_vec pb, nb; 55e9907009SChristoph Hellwig 56e9907009SChristoph Hellwig if (!bio_has_data(prev) || !queue_virt_boundary(q)) 57e9907009SChristoph Hellwig return false; 58e9907009SChristoph Hellwig 59e9907009SChristoph Hellwig /* 60e9907009SChristoph Hellwig * Don't merge if the 1st bio starts with non-zero offset, otherwise it 61e9907009SChristoph Hellwig * is quite difficult to respect the sg gap limit. We work hard to 62e9907009SChristoph Hellwig * merge a huge number of small single bios in case of mkfs. 63e9907009SChristoph Hellwig */ 64e9907009SChristoph Hellwig if (prev_rq) 65e9907009SChristoph Hellwig bio_get_first_bvec(prev_rq->bio, &pb); 66e9907009SChristoph Hellwig else 67e9907009SChristoph Hellwig bio_get_first_bvec(prev, &pb); 68df376b2eSJohannes Thumshirn if (pb.bv_offset & queue_virt_boundary(q)) 69e9907009SChristoph Hellwig return true; 70e9907009SChristoph Hellwig 71e9907009SChristoph Hellwig /* 72e9907009SChristoph Hellwig * We don't need to worry about the situation that the merged segment 73e9907009SChristoph Hellwig * ends in unaligned virt boundary: 74e9907009SChristoph Hellwig * 75e9907009SChristoph Hellwig * - if 'pb' ends aligned, the merged segment ends aligned 76e9907009SChristoph Hellwig * - if 'pb' ends unaligned, the next bio must include 77e9907009SChristoph Hellwig * one single bvec of 'nb', otherwise the 'nb' can't 78e9907009SChristoph Hellwig * merge with 'pb' 79e9907009SChristoph Hellwig */ 80e9907009SChristoph Hellwig bio_get_last_bvec(prev, &pb); 81e9907009SChristoph Hellwig bio_get_first_bvec(next, &nb); 82200a9affSChristoph Hellwig if (biovec_phys_mergeable(q, &pb, &nb)) 83e9907009SChristoph Hellwig return false; 84e9907009SChristoph Hellwig return __bvec_gap_to_prev(q, &pb, nb.bv_offset); 85e9907009SChristoph Hellwig } 86e9907009SChristoph Hellwig 87e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio) 88e9907009SChristoph Hellwig { 89e9907009SChristoph Hellwig return bio_will_gap(req->q, req, req->biotail, bio); 90e9907009SChristoph Hellwig } 91e9907009SChristoph Hellwig 92e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio) 93e9907009SChristoph Hellwig { 94e9907009SChristoph Hellwig return bio_will_gap(req->q, NULL, bio, req->bio); 95e9907009SChristoph Hellwig } 96e9907009SChristoph Hellwig 9754efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 9854efd50bSKent Overstreet struct bio *bio, 99bdced438SMing Lei struct bio_set *bs, 100bdced438SMing Lei unsigned *nsegs) 10154efd50bSKent Overstreet { 10254efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 10354efd50bSKent Overstreet int alignment; 10454efd50bSKent Overstreet sector_t tmp; 10554efd50bSKent Overstreet unsigned split_sectors; 10654efd50bSKent Overstreet 107bdced438SMing Lei *nsegs = 1; 108bdced438SMing Lei 10954efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 11054efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 11154efd50bSKent Overstreet 1121adfc5e4SMing Lei max_discard_sectors = min(q->limits.max_discard_sectors, 1131adfc5e4SMing Lei bio_allowed_max_sectors(q)); 11454efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 11554efd50bSKent Overstreet 11654efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 11754efd50bSKent Overstreet /* XXX: warn */ 11854efd50bSKent Overstreet return NULL; 11954efd50bSKent Overstreet } 12054efd50bSKent Overstreet 12154efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 12254efd50bSKent Overstreet return NULL; 12354efd50bSKent Overstreet 12454efd50bSKent Overstreet split_sectors = max_discard_sectors; 12554efd50bSKent Overstreet 12654efd50bSKent Overstreet /* 12754efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 12854efd50bSKent Overstreet * the previous aligned sector. 12954efd50bSKent Overstreet */ 13054efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 13154efd50bSKent Overstreet 13254efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 13354efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 13454efd50bSKent Overstreet 13554efd50bSKent Overstreet if (split_sectors > tmp) 13654efd50bSKent Overstreet split_sectors -= tmp; 13754efd50bSKent Overstreet 13854efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 13954efd50bSKent Overstreet } 14054efd50bSKent Overstreet 141885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, 142885fa13fSChristoph Hellwig struct bio *bio, struct bio_set *bs, unsigned *nsegs) 143885fa13fSChristoph Hellwig { 144d665e12aSChristoph Hellwig *nsegs = 0; 145885fa13fSChristoph Hellwig 146885fa13fSChristoph Hellwig if (!q->limits.max_write_zeroes_sectors) 147885fa13fSChristoph Hellwig return NULL; 148885fa13fSChristoph Hellwig 149885fa13fSChristoph Hellwig if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) 150885fa13fSChristoph Hellwig return NULL; 151885fa13fSChristoph Hellwig 152885fa13fSChristoph Hellwig return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); 153885fa13fSChristoph Hellwig } 154885fa13fSChristoph Hellwig 15554efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 15654efd50bSKent Overstreet struct bio *bio, 157bdced438SMing Lei struct bio_set *bs, 158bdced438SMing Lei unsigned *nsegs) 15954efd50bSKent Overstreet { 160bdced438SMing Lei *nsegs = 1; 161bdced438SMing Lei 16254efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 16354efd50bSKent Overstreet return NULL; 16454efd50bSKent Overstreet 16554efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 16654efd50bSKent Overstreet return NULL; 16754efd50bSKent Overstreet 16854efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 16954efd50bSKent Overstreet } 17054efd50bSKent Overstreet 1719cc5169cSBart Van Assche /* 1729cc5169cSBart Van Assche * Return the maximum number of sectors from the start of a bio that may be 1739cc5169cSBart Van Assche * submitted as a single request to a block device. If enough sectors remain, 1749cc5169cSBart Van Assche * align the end to the physical block size. Otherwise align the end to the 1759cc5169cSBart Van Assche * logical block size. This approach minimizes the number of non-aligned 1769cc5169cSBart Van Assche * requests that are submitted to a block device if the start of a bio is not 1779cc5169cSBart Van Assche * aligned to a physical block boundary. 1789cc5169cSBart Van Assche */ 179d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 180d0e5fbb0SMing Lei struct bio *bio) 181d0e5fbb0SMing Lei { 1823ee16db3SMike Snitzer unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector, 0); 1839cc5169cSBart Van Assche unsigned max_sectors = sectors; 1849cc5169cSBart Van Assche unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT; 1859cc5169cSBart Van Assche unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT; 1869cc5169cSBart Van Assche unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1); 187d0e5fbb0SMing Lei 1889cc5169cSBart Van Assche max_sectors += start_offset; 1899cc5169cSBart Van Assche max_sectors &= ~(pbs - 1); 1909cc5169cSBart Van Assche if (max_sectors > start_offset) 1919cc5169cSBart Van Assche return max_sectors - start_offset; 192d0e5fbb0SMing Lei 193e4b469c6SKeith Busch return sectors & ~(lbs - 1); 194d0e5fbb0SMing Lei } 195d0e5fbb0SMing Lei 196429120f3SMing Lei static inline unsigned get_max_segment_size(const struct request_queue *q, 197429120f3SMing Lei struct page *start_page, 198429120f3SMing Lei unsigned long offset) 199dcebd755SMing Lei { 200dcebd755SMing Lei unsigned long mask = queue_segment_boundary(q); 201dcebd755SMing Lei 202429120f3SMing Lei offset = mask & (page_to_phys(start_page) + offset); 2034a2f704eSMing Lei 2044a2f704eSMing Lei /* 2054a2f704eSMing Lei * overflow may be triggered in case of zero page physical address 2064a2f704eSMing Lei * on 32bit arch, use queue's max segment size when that happens. 2074a2f704eSMing Lei */ 2084a2f704eSMing Lei return min_not_zero(mask - offset + 1, 2094a2f704eSMing Lei (unsigned long)queue_max_segment_size(q)); 210dcebd755SMing Lei } 211dcebd755SMing Lei 212708b25b3SBart Van Assche /** 213708b25b3SBart Van Assche * bvec_split_segs - verify whether or not a bvec should be split in the middle 214708b25b3SBart Van Assche * @q: [in] request queue associated with the bio associated with @bv 215708b25b3SBart Van Assche * @bv: [in] bvec to examine 216708b25b3SBart Van Assche * @nsegs: [in,out] Number of segments in the bio being built. Incremented 217708b25b3SBart Van Assche * by the number of segments from @bv that may be appended to that 218708b25b3SBart Van Assche * bio without exceeding @max_segs 219708b25b3SBart Van Assche * @sectors: [in,out] Number of sectors in the bio being built. Incremented 220708b25b3SBart Van Assche * by the number of sectors from @bv that may be appended to that 221708b25b3SBart Van Assche * bio without exceeding @max_sectors 222708b25b3SBart Van Assche * @max_segs: [in] upper bound for *@nsegs 223708b25b3SBart Van Assche * @max_sectors: [in] upper bound for *@sectors 224708b25b3SBart Van Assche * 225708b25b3SBart Van Assche * When splitting a bio, it can happen that a bvec is encountered that is too 226708b25b3SBart Van Assche * big to fit in a single segment and hence that it has to be split in the 227708b25b3SBart Van Assche * middle. This function verifies whether or not that should happen. The value 228708b25b3SBart Van Assche * %true is returned if and only if appending the entire @bv to a bio with 229708b25b3SBart Van Assche * *@nsegs segments and *@sectors sectors would make that bio unacceptable for 230708b25b3SBart Van Assche * the block driver. 231dcebd755SMing Lei */ 232af2c68feSBart Van Assche static bool bvec_split_segs(const struct request_queue *q, 233af2c68feSBart Van Assche const struct bio_vec *bv, unsigned *nsegs, 234708b25b3SBart Van Assche unsigned *sectors, unsigned max_segs, 235708b25b3SBart Van Assche unsigned max_sectors) 236dcebd755SMing Lei { 237708b25b3SBart Van Assche unsigned max_len = (min(max_sectors, UINT_MAX >> 9) - *sectors) << 9; 238708b25b3SBart Van Assche unsigned len = min(bv->bv_len, max_len); 239dcebd755SMing Lei unsigned total_len = 0; 240ff9811b3SBart Van Assche unsigned seg_size = 0; 241dcebd755SMing Lei 242ff9811b3SBart Van Assche while (len && *nsegs < max_segs) { 243429120f3SMing Lei seg_size = get_max_segment_size(q, bv->bv_page, 244429120f3SMing Lei bv->bv_offset + total_len); 245dcebd755SMing Lei seg_size = min(seg_size, len); 246dcebd755SMing Lei 247ff9811b3SBart Van Assche (*nsegs)++; 248dcebd755SMing Lei total_len += seg_size; 249dcebd755SMing Lei len -= seg_size; 250dcebd755SMing Lei 251dcebd755SMing Lei if ((bv->bv_offset + total_len) & queue_virt_boundary(q)) 252dcebd755SMing Lei break; 253dcebd755SMing Lei } 254dcebd755SMing Lei 255dcebd755SMing Lei *sectors += total_len >> 9; 256dcebd755SMing Lei 257708b25b3SBart Van Assche /* tell the caller to split the bvec if it is too big to fit */ 258708b25b3SBart Van Assche return len > 0 || bv->bv_len > max_len; 259dcebd755SMing Lei } 260dcebd755SMing Lei 261dad77584SBart Van Assche /** 262dad77584SBart Van Assche * blk_bio_segment_split - split a bio in two bios 263dad77584SBart Van Assche * @q: [in] request queue pointer 264dad77584SBart Van Assche * @bio: [in] bio to be split 265dad77584SBart Van Assche * @bs: [in] bio set to allocate the clone from 266dad77584SBart Van Assche * @segs: [out] number of segments in the bio with the first half of the sectors 267dad77584SBart Van Assche * 268dad77584SBart Van Assche * Clone @bio, update the bi_iter of the clone to represent the first sectors 269dad77584SBart Van Assche * of @bio and update @bio->bi_iter to represent the remaining sectors. The 270dad77584SBart Van Assche * following is guaranteed for the cloned bio: 271dad77584SBart Van Assche * - That it has at most get_max_io_size(@q, @bio) sectors. 272dad77584SBart Van Assche * - That it has at most queue_max_segments(@q) segments. 273dad77584SBart Van Assche * 274dad77584SBart Van Assche * Except for discard requests the cloned bio will point at the bi_io_vec of 275dad77584SBart Van Assche * the original bio. It is the responsibility of the caller to ensure that the 276dad77584SBart Van Assche * original bio is not freed before the cloned bio. The caller is also 277dad77584SBart Van Assche * responsible for ensuring that @bs is only destroyed after processing of the 278dad77584SBart Van Assche * split bio has finished. 279dad77584SBart Van Assche */ 28054efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 28154efd50bSKent Overstreet struct bio *bio, 282bdced438SMing Lei struct bio_set *bs, 283bdced438SMing Lei unsigned *segs) 28454efd50bSKent Overstreet { 2855014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 28654efd50bSKent Overstreet struct bvec_iter iter; 2876869875fSChristoph Hellwig unsigned nsegs = 0, sectors = 0; 288d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 28905b700baSMing Lei const unsigned max_segs = queue_max_segments(q); 29054efd50bSKent Overstreet 291dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 29254efd50bSKent Overstreet /* 29354efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 29454efd50bSKent Overstreet * offset would create a gap, disallow it. 29554efd50bSKent Overstreet */ 2965014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 29754efd50bSKent Overstreet goto split; 29854efd50bSKent Overstreet 29905b700baSMing Lei if (nsegs < max_segs && 300708b25b3SBart Van Assche sectors + (bv.bv_len >> 9) <= max_sectors && 301708b25b3SBart Van Assche bv.bv_offset + bv.bv_len <= PAGE_SIZE) { 302708b25b3SBart Van Assche nsegs++; 303708b25b3SBart Van Assche sectors += bv.bv_len >> 9; 304708b25b3SBart Van Assche } else if (bvec_split_segs(q, &bv, &nsegs, §ors, max_segs, 305708b25b3SBart Van Assche max_sectors)) { 306e36f6204SKeith Busch goto split; 307e36f6204SKeith Busch } 308e36f6204SKeith Busch 30954efd50bSKent Overstreet bvprv = bv; 310578270bfSMing Lei bvprvp = &bvprv; 31154efd50bSKent Overstreet } 31254efd50bSKent Overstreet 313d627065dSChristoph Hellwig *segs = nsegs; 314d627065dSChristoph Hellwig return NULL; 31554efd50bSKent Overstreet split: 316bdced438SMing Lei *segs = nsegs; 317cc29e1bfSJeffle Xu 318cc29e1bfSJeffle Xu /* 319cc29e1bfSJeffle Xu * Bio splitting may cause subtle trouble such as hang when doing sync 320cc29e1bfSJeffle Xu * iopoll in direct IO routine. Given performance gain of iopoll for 321cc29e1bfSJeffle Xu * big IO can be trival, disable iopoll when split needed. 322cc29e1bfSJeffle Xu */ 3236ce913feSChristoph Hellwig bio_clear_polled(bio); 324d627065dSChristoph Hellwig return bio_split(bio, sectors, GFP_NOIO, bs); 32554efd50bSKent Overstreet } 32654efd50bSKent Overstreet 327dad77584SBart Van Assche /** 328dad77584SBart Van Assche * __blk_queue_split - split a bio and submit the second half 329abd45c15SJens Axboe * @q: [in] request_queue new bio is being queued at 330dad77584SBart Van Assche * @bio: [in, out] bio to be split 331dad77584SBart Van Assche * @nr_segs: [out] number of segments in the first bio 332dad77584SBart Van Assche * 333dad77584SBart Van Assche * Split a bio into two bios, chain the two bios, submit the second half and 334dad77584SBart Van Assche * store a pointer to the first half in *@bio. If the second bio is still too 335dad77584SBart Van Assche * big it will be split by a recursive call to this function. Since this 336309dca30SChristoph Hellwig * function may allocate a new bio from q->bio_split, it is the responsibility 337309dca30SChristoph Hellwig * of the caller to ensure that q->bio_split is only released after processing 338309dca30SChristoph Hellwig * of the split bio has finished. 339dad77584SBart Van Assche */ 340abd45c15SJens Axboe void __blk_queue_split(struct request_queue *q, struct bio **bio, 341abd45c15SJens Axboe unsigned int *nr_segs) 34254efd50bSKent Overstreet { 343fa532287SChristoph Hellwig struct bio *split = NULL; 34454efd50bSKent Overstreet 3457afafc8aSAdrian Hunter switch (bio_op(*bio)) { 3467afafc8aSAdrian Hunter case REQ_OP_DISCARD: 3477afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 34814ccb66bSChristoph Hellwig split = blk_bio_discard_split(q, *bio, &q->bio_split, nr_segs); 3497afafc8aSAdrian Hunter break; 350a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 35114ccb66bSChristoph Hellwig split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, 35214ccb66bSChristoph Hellwig nr_segs); 353a6f0788eSChaitanya Kulkarni break; 3547afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 35514ccb66bSChristoph Hellwig split = blk_bio_write_same_split(q, *bio, &q->bio_split, 35614ccb66bSChristoph Hellwig nr_segs); 3577afafc8aSAdrian Hunter break; 3587afafc8aSAdrian Hunter default: 35914ccb66bSChristoph Hellwig split = blk_bio_segment_split(q, *bio, &q->bio_split, nr_segs); 3607afafc8aSAdrian Hunter break; 3617afafc8aSAdrian Hunter } 362bdced438SMing Lei 36354efd50bSKent Overstreet if (split) { 3646ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 3651eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 3666ac45aebSMing Lei 36754efd50bSKent Overstreet bio_chain(split, *bio); 368eb6f7f7cSChristoph Hellwig trace_block_split(split, (*bio)->bi_iter.bi_sector); 369ed00aabdSChristoph Hellwig submit_bio_noacct(*bio); 37054efd50bSKent Overstreet *bio = split; 3714f1e9630SChunguang Xu 3724f1e9630SChunguang Xu blk_throtl_charge_bio_split(*bio); 37354efd50bSKent Overstreet } 37454efd50bSKent Overstreet } 37514ccb66bSChristoph Hellwig 376dad77584SBart Van Assche /** 377dad77584SBart Van Assche * blk_queue_split - split a bio and submit the second half 378dad77584SBart Van Assche * @bio: [in, out] bio to be split 379dad77584SBart Van Assche * 380dad77584SBart Van Assche * Split a bio into two bios, chains the two bios, submit the second half and 381dad77584SBart Van Assche * store a pointer to the first half in *@bio. Since this function may allocate 382309dca30SChristoph Hellwig * a new bio from q->bio_split, it is the responsibility of the caller to ensure 383309dca30SChristoph Hellwig * that q->bio_split is only released after processing of the split bio has 384309dca30SChristoph Hellwig * finished. 385dad77584SBart Van Assche */ 386f695ca38SChristoph Hellwig void blk_queue_split(struct bio **bio) 38714ccb66bSChristoph Hellwig { 388859897c3SPavel Begunkov struct request_queue *q = bdev_get_queue((*bio)->bi_bdev); 38914ccb66bSChristoph Hellwig unsigned int nr_segs; 39014ccb66bSChristoph Hellwig 391abd45c15SJens Axboe if (blk_may_split(q, *bio)) 392abd45c15SJens Axboe __blk_queue_split(q, bio, &nr_segs); 39314ccb66bSChristoph Hellwig } 39454efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 39554efd50bSKent Overstreet 396e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq) 397d6d48196SJens Axboe { 3986869875fSChristoph Hellwig unsigned int nr_phys_segs = 0; 399ff9811b3SBart Van Assche unsigned int nr_sectors = 0; 400e9cd19c0SChristoph Hellwig struct req_iterator iter; 4016869875fSChristoph Hellwig struct bio_vec bv; 402d6d48196SJens Axboe 403e9cd19c0SChristoph Hellwig if (!rq->bio) 4041e428079SJens Axboe return 0; 405d6d48196SJens Axboe 406e9cd19c0SChristoph Hellwig switch (bio_op(rq->bio)) { 407a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 408a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 409a958937fSDavid Jeffery if (queue_max_discard_segments(rq->q) > 1) { 410a958937fSDavid Jeffery struct bio *bio = rq->bio; 411a958937fSDavid Jeffery 412a958937fSDavid Jeffery for_each_bio(bio) 413a958937fSDavid Jeffery nr_phys_segs++; 414a958937fSDavid Jeffery return nr_phys_segs; 415a958937fSDavid Jeffery } 416a958937fSDavid Jeffery return 1; 417a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 418f9d03f96SChristoph Hellwig return 0; 419f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 4205cb8850cSKent Overstreet return 1; 421a6f0788eSChaitanya Kulkarni } 4225cb8850cSKent Overstreet 423e9cd19c0SChristoph Hellwig rq_for_each_bvec(bv, rq, iter) 424ff9811b3SBart Van Assche bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors, 425708b25b3SBart Van Assche UINT_MAX, UINT_MAX); 4261e428079SJens Axboe return nr_phys_segs; 4271e428079SJens Axboe } 4281e428079SJens Axboe 42948d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, 430862e5a5eSMing Lei struct scatterlist *sglist) 431862e5a5eSMing Lei { 432862e5a5eSMing Lei if (!*sg) 433862e5a5eSMing Lei return sglist; 434862e5a5eSMing Lei 435862e5a5eSMing Lei /* 436862e5a5eSMing Lei * If the driver previously mapped a shorter list, we could see a 437862e5a5eSMing Lei * termination bit prematurely unless it fully inits the sg table 438862e5a5eSMing Lei * on each mapping. We KNOW that there must be more entries here 439862e5a5eSMing Lei * or the driver would be buggy, so force clear the termination bit 440862e5a5eSMing Lei * to avoid doing a full sg_init_table() in drivers for each command. 441862e5a5eSMing Lei */ 442862e5a5eSMing Lei sg_unmark_end(*sg); 443862e5a5eSMing Lei return sg_next(*sg); 444862e5a5eSMing Lei } 445862e5a5eSMing Lei 446862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q, 447862e5a5eSMing Lei struct bio_vec *bvec, struct scatterlist *sglist, 448862e5a5eSMing Lei struct scatterlist **sg) 449862e5a5eSMing Lei { 450862e5a5eSMing Lei unsigned nbytes = bvec->bv_len; 4518a96a0e4SChristoph Hellwig unsigned nsegs = 0, total = 0; 452862e5a5eSMing Lei 453862e5a5eSMing Lei while (nbytes > 0) { 4548a96a0e4SChristoph Hellwig unsigned offset = bvec->bv_offset + total; 455429120f3SMing Lei unsigned len = min(get_max_segment_size(q, bvec->bv_page, 456429120f3SMing Lei offset), nbytes); 457f9f76879SChristoph Hellwig struct page *page = bvec->bv_page; 458f9f76879SChristoph Hellwig 459f9f76879SChristoph Hellwig /* 460f9f76879SChristoph Hellwig * Unfortunately a fair number of drivers barf on scatterlists 461f9f76879SChristoph Hellwig * that have an offset larger than PAGE_SIZE, despite other 462f9f76879SChristoph Hellwig * subsystems dealing with that invariant just fine. For now 463f9f76879SChristoph Hellwig * stick to the legacy format where we never present those from 464f9f76879SChristoph Hellwig * the block layer, but the code below should be removed once 465f9f76879SChristoph Hellwig * these offenders (mostly MMC/SD drivers) are fixed. 466f9f76879SChristoph Hellwig */ 467f9f76879SChristoph Hellwig page += (offset >> PAGE_SHIFT); 468f9f76879SChristoph Hellwig offset &= ~PAGE_MASK; 469862e5a5eSMing Lei 470862e5a5eSMing Lei *sg = blk_next_sg(sg, sglist); 471f9f76879SChristoph Hellwig sg_set_page(*sg, page, len, offset); 472862e5a5eSMing Lei 4738a96a0e4SChristoph Hellwig total += len; 4748a96a0e4SChristoph Hellwig nbytes -= len; 475862e5a5eSMing Lei nsegs++; 476862e5a5eSMing Lei } 477862e5a5eSMing Lei 478862e5a5eSMing Lei return nsegs; 479862e5a5eSMing Lei } 480862e5a5eSMing Lei 48116e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv, 48216e3e418SMing Lei struct scatterlist *sglist, struct scatterlist **sg) 48316e3e418SMing Lei { 48416e3e418SMing Lei *sg = blk_next_sg(sg, sglist); 48516e3e418SMing Lei sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 48616e3e418SMing Lei return 1; 48716e3e418SMing Lei } 48816e3e418SMing Lei 489f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */ 490f6970f83SMing Lei static inline bool 491f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec, 492f6970f83SMing Lei struct bio_vec *bvprv, struct scatterlist **sg) 493963ab9e5SAsias He { 494963ab9e5SAsias He 495963ab9e5SAsias He int nbytes = bvec->bv_len; 496963ab9e5SAsias He 497f6970f83SMing Lei if (!*sg) 498f6970f83SMing Lei return false; 499f6970f83SMing Lei 500b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 501f6970f83SMing Lei return false; 502f6970f83SMing Lei 5033dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec)) 504f6970f83SMing Lei return false; 505963ab9e5SAsias He 506963ab9e5SAsias He (*sg)->length += nbytes; 507f6970f83SMing Lei 508f6970f83SMing Lei return true; 509963ab9e5SAsias He } 510963ab9e5SAsias He 5115cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 5125cb8850cSKent Overstreet struct scatterlist *sglist, 5135cb8850cSKent Overstreet struct scatterlist **sg) 5145cb8850cSKent Overstreet { 5153f649ab7SKees Cook struct bio_vec bvec, bvprv = { NULL }; 5165cb8850cSKent Overstreet struct bvec_iter iter; 51738417468SChristoph Hellwig int nsegs = 0; 518f6970f83SMing Lei bool new_bio = false; 5195cb8850cSKent Overstreet 520f6970f83SMing Lei for_each_bio(bio) { 521f6970f83SMing Lei bio_for_each_bvec(bvec, bio, iter) { 522f6970f83SMing Lei /* 523f6970f83SMing Lei * Only try to merge bvecs from two bios given we 524f6970f83SMing Lei * have done bio internal merge when adding pages 525f6970f83SMing Lei * to bio 526f6970f83SMing Lei */ 527f6970f83SMing Lei if (new_bio && 528f6970f83SMing Lei __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg)) 529f6970f83SMing Lei goto next_bvec; 530f6970f83SMing Lei 531f6970f83SMing Lei if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE) 532f6970f83SMing Lei nsegs += __blk_bvec_map_sg(bvec, sglist, sg); 533f6970f83SMing Lei else 534f6970f83SMing Lei nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg); 535f6970f83SMing Lei next_bvec: 536f6970f83SMing Lei new_bio = false; 537f6970f83SMing Lei } 538b21e11c5SMing Lei if (likely(bio->bi_iter.bi_size)) { 539f6970f83SMing Lei bvprv = bvec; 540f6970f83SMing Lei new_bio = true; 541f6970f83SMing Lei } 542b21e11c5SMing Lei } 5435cb8850cSKent Overstreet 5445cb8850cSKent Overstreet return nsegs; 5455cb8850cSKent Overstreet } 5465cb8850cSKent Overstreet 547d6d48196SJens Axboe /* 548d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 549d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 550d6d48196SJens Axboe */ 55189de1504SChristoph Hellwig int __blk_rq_map_sg(struct request_queue *q, struct request *rq, 55289de1504SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **last_sg) 553d6d48196SJens Axboe { 5545cb8850cSKent Overstreet int nsegs = 0; 555d6d48196SJens Axboe 556f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 55789de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg); 558f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 55989de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, last_sg); 560f9d03f96SChristoph Hellwig else if (rq->bio) 56189de1504SChristoph Hellwig nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg); 562f18573abSFUJITA Tomonori 56389de1504SChristoph Hellwig if (*last_sg) 56489de1504SChristoph Hellwig sg_mark_end(*last_sg); 565d6d48196SJens Axboe 56612e57f59SMing Lei /* 56712e57f59SMing Lei * Something must have been wrong if the figured number of 56812e57f59SMing Lei * segment is bigger than number of req's physical segments 56912e57f59SMing Lei */ 570f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 57112e57f59SMing Lei 572d6d48196SJens Axboe return nsegs; 573d6d48196SJens Axboe } 57489de1504SChristoph Hellwig EXPORT_SYMBOL(__blk_rq_map_sg); 575d6d48196SJens Axboe 576943b40c8SMing Lei static inline unsigned int blk_rq_get_max_segments(struct request *rq) 577943b40c8SMing Lei { 578943b40c8SMing Lei if (req_op(rq) == REQ_OP_DISCARD) 579943b40c8SMing Lei return queue_max_discard_segments(rq->q); 580943b40c8SMing Lei return queue_max_segments(rq->q); 581943b40c8SMing Lei } 582943b40c8SMing Lei 583badf7f64SChristoph Hellwig static inline unsigned int blk_rq_get_max_sectors(struct request *rq, 584badf7f64SChristoph Hellwig sector_t offset) 585badf7f64SChristoph Hellwig { 586badf7f64SChristoph Hellwig struct request_queue *q = rq->q; 587badf7f64SChristoph Hellwig 588badf7f64SChristoph Hellwig if (blk_rq_is_passthrough(rq)) 589badf7f64SChristoph Hellwig return q->limits.max_hw_sectors; 590badf7f64SChristoph Hellwig 591badf7f64SChristoph Hellwig if (!q->limits.chunk_sectors || 592badf7f64SChristoph Hellwig req_op(rq) == REQ_OP_DISCARD || 593badf7f64SChristoph Hellwig req_op(rq) == REQ_OP_SECURE_ERASE) 594badf7f64SChristoph Hellwig return blk_queue_get_max_sectors(q, req_op(rq)); 595badf7f64SChristoph Hellwig 596badf7f64SChristoph Hellwig return min(blk_max_size_offset(q, offset, 0), 597badf7f64SChristoph Hellwig blk_queue_get_max_sectors(q, req_op(rq))); 598badf7f64SChristoph Hellwig } 599badf7f64SChristoph Hellwig 60014ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio, 60114ccb66bSChristoph Hellwig unsigned int nr_phys_segs) 602d6d48196SJens Axboe { 6032705dfb2SMing Lei if (blk_integrity_merge_bio(req->q, req, bio) == false) 60413f05c8dSMartin K. Petersen goto no_merge; 60513f05c8dSMartin K. Petersen 6062705dfb2SMing Lei /* discard request merge won't add new segment */ 6072705dfb2SMing Lei if (req_op(req) == REQ_OP_DISCARD) 6082705dfb2SMing Lei return 1; 6092705dfb2SMing Lei 6102705dfb2SMing Lei if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req)) 61113f05c8dSMartin K. Petersen goto no_merge; 612d6d48196SJens Axboe 613d6d48196SJens Axboe /* 614d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 615d6d48196SJens Axboe * counters. 616d6d48196SJens Axboe */ 617d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 618d6d48196SJens Axboe return 1; 61913f05c8dSMartin K. Petersen 62013f05c8dSMartin K. Petersen no_merge: 62114ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 62213f05c8dSMartin K. Petersen return 0; 623d6d48196SJens Axboe } 624d6d48196SJens Axboe 62514ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs) 626d6d48196SJens Axboe { 6275e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 6285e7c4274SJens Axboe return 0; 6297f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6307f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 6317f39add3SSagi Grimberg return 0; 632a892c8d5SSatya Tangirala if (!bio_crypt_ctx_back_mergeable(req, bio)) 633a892c8d5SSatya Tangirala return 0; 634f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 63517007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 63614ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 637d6d48196SJens Axboe return 0; 638d6d48196SJens Axboe } 639d6d48196SJens Axboe 64014ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 641d6d48196SJens Axboe } 642d6d48196SJens Axboe 643eda5cc99SChristoph Hellwig static int ll_front_merge_fn(struct request *req, struct bio *bio, 644eda5cc99SChristoph Hellwig unsigned int nr_segs) 645d6d48196SJens Axboe { 6465e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 6475e7c4274SJens Axboe return 0; 6487f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6497f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 6507f39add3SSagi Grimberg return 0; 651a892c8d5SSatya Tangirala if (!bio_crypt_ctx_front_mergeable(req, bio)) 652a892c8d5SSatya Tangirala return 0; 653f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 65417007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 65514ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 656d6d48196SJens Axboe return 0; 657d6d48196SJens Axboe } 658d6d48196SJens Axboe 65914ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 660d6d48196SJens Axboe } 661d6d48196SJens Axboe 662445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 663445251d0SJens Axboe struct request *next) 664445251d0SJens Axboe { 665445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 666445251d0SJens Axboe 667445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 668445251d0SJens Axboe goto no_merge; 669445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 670445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 671445251d0SJens Axboe goto no_merge; 672445251d0SJens Axboe 673445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 674445251d0SJens Axboe return true; 675445251d0SJens Axboe no_merge: 676445251d0SJens Axboe req_set_nomerge(q, req); 677445251d0SJens Axboe return false; 678445251d0SJens Axboe } 679445251d0SJens Axboe 680d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 681d6d48196SJens Axboe struct request *next) 682d6d48196SJens Axboe { 683d6d48196SJens Axboe int total_phys_segments; 684d6d48196SJens Axboe 6855e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 686854fbb9cSKeith Busch return 0; 687854fbb9cSKeith Busch 688d6d48196SJens Axboe /* 689d6d48196SJens Axboe * Will it become too large? 690d6d48196SJens Axboe */ 691f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 69217007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 693d6d48196SJens Axboe return 0; 694d6d48196SJens Axboe 695d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 696943b40c8SMing Lei if (total_phys_segments > blk_rq_get_max_segments(req)) 697d6d48196SJens Axboe return 0; 698d6d48196SJens Axboe 6994eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 70013f05c8dSMartin K. Petersen return 0; 70113f05c8dSMartin K. Petersen 702a892c8d5SSatya Tangirala if (!bio_crypt_ctx_merge_rq(req, next)) 703a892c8d5SSatya Tangirala return 0; 704a892c8d5SSatya Tangirala 705d6d48196SJens Axboe /* Merge is OK... */ 706d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 707d6d48196SJens Axboe return 1; 708d6d48196SJens Axboe } 709d6d48196SJens Axboe 71080a761fdSTejun Heo /** 71180a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 71280a761fdSTejun Heo * @rq: request to mark as mixed merge 71380a761fdSTejun Heo * 71480a761fdSTejun Heo * Description: 71580a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 71680a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 71780a761fdSTejun Heo * merged. 71880a761fdSTejun Heo */ 71980a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 72080a761fdSTejun Heo { 72180a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 72280a761fdSTejun Heo struct bio *bio; 72380a761fdSTejun Heo 724e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 72580a761fdSTejun Heo return; 72680a761fdSTejun Heo 72780a761fdSTejun Heo /* 72880a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 72980a761fdSTejun Heo * contained bios. It will just track those of the first one. 73080a761fdSTejun Heo * Distributes the attributs to each bio. 73180a761fdSTejun Heo */ 73280a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 7331eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 7341eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 7351eff9d32SJens Axboe bio->bi_opf |= ff; 73680a761fdSTejun Heo } 737e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 73880a761fdSTejun Heo } 73980a761fdSTejun Heo 740b9c54f56SKonstantin Khlebnikov static void blk_account_io_merge_request(struct request *req) 74126308eabSJerome Marchand { 74226308eabSJerome Marchand if (blk_do_io_stat(req)) { 743112f158fSMike Snitzer part_stat_lock(); 744b9c54f56SKonstantin Khlebnikov part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); 74526308eabSJerome Marchand part_stat_unlock(); 74626308eabSJerome Marchand } 74726308eabSJerome Marchand } 748b9c54f56SKonstantin Khlebnikov 749e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req, 750e96c0d83SEric Biggers struct request *next) 75169840466SJianchao Wang { 75269840466SJianchao Wang if (blk_discard_mergable(req)) 75369840466SJianchao Wang return ELEVATOR_DISCARD_MERGE; 75469840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) 75569840466SJianchao Wang return ELEVATOR_BACK_MERGE; 75669840466SJianchao Wang 75769840466SJianchao Wang return ELEVATOR_NO_MERGE; 75869840466SJianchao Wang } 75926308eabSJerome Marchand 760badf7f64SChristoph Hellwig static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b) 761badf7f64SChristoph Hellwig { 762badf7f64SChristoph Hellwig if (bio_page(a) == bio_page(b) && bio_offset(a) == bio_offset(b)) 763badf7f64SChristoph Hellwig return true; 764badf7f64SChristoph Hellwig return false; 765badf7f64SChristoph Hellwig } 766badf7f64SChristoph Hellwig 767d6d48196SJens Axboe /* 768b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 769b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 770d6d48196SJens Axboe */ 771b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 772b973cb7eSJens Axboe struct request *req, struct request *next) 773d6d48196SJens Axboe { 774d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 775b973cb7eSJens Axboe return NULL; 776d6d48196SJens Axboe 777288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 778b973cb7eSJens Axboe return NULL; 779f31dc1cdSMartin K. Petersen 780d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 7812081a56bSJens Axboe || req->rq_disk != next->rq_disk) 782b973cb7eSJens Axboe return NULL; 783d6d48196SJens Axboe 7848fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 7854363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 786b973cb7eSJens Axboe return NULL; 7874363ac7cSMartin K. Petersen 788d6d48196SJens Axboe /* 789cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 790cb6934f8SJens Axboe * non-hint IO. 791cb6934f8SJens Axboe */ 792cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 793cb6934f8SJens Axboe return NULL; 794cb6934f8SJens Axboe 795668ffc03SDamien Le Moal if (req->ioprio != next->ioprio) 796668ffc03SDamien Le Moal return NULL; 797668ffc03SDamien Le Moal 798cb6934f8SJens Axboe /* 799d6d48196SJens Axboe * If we are allowed to merge, then append bio list 800d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 801d6d48196SJens Axboe * will have updated segment counts, update sector 802445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 803445251d0SJens Axboe * have separate settings. 804d6d48196SJens Axboe */ 80569840466SJianchao Wang 80669840466SJianchao Wang switch (blk_try_req_merge(req, next)) { 80769840466SJianchao Wang case ELEVATOR_DISCARD_MERGE: 808445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 809445251d0SJens Axboe return NULL; 81069840466SJianchao Wang break; 81169840466SJianchao Wang case ELEVATOR_BACK_MERGE: 81269840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next)) 813b973cb7eSJens Axboe return NULL; 81469840466SJianchao Wang break; 81569840466SJianchao Wang default: 81669840466SJianchao Wang return NULL; 81769840466SJianchao Wang } 818d6d48196SJens Axboe 819d6d48196SJens Axboe /* 82080a761fdSTejun Heo * If failfast settings disagree or any of the two is already 82180a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 82280a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 82380a761fdSTejun Heo * set properly. 82480a761fdSTejun Heo */ 825e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 82680a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 82780a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 82880a761fdSTejun Heo blk_rq_set_mixed_merge(req); 82980a761fdSTejun Heo blk_rq_set_mixed_merge(next); 83080a761fdSTejun Heo } 83180a761fdSTejun Heo 83280a761fdSTejun Heo /* 833522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 834522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 835522a7775SOmar Sandoval * current request for accounting purposes. 836d6d48196SJens Axboe */ 837522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 838522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 839d6d48196SJens Axboe 840d6d48196SJens Axboe req->biotail->bi_next = next->bio; 841d6d48196SJens Axboe req->biotail = next->biotail; 842d6d48196SJens Axboe 843a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 844d6d48196SJens Axboe 8452a5cf35cSMing Lei if (!blk_discard_mergable(req)) 846d6d48196SJens Axboe elv_merge_requests(q, req, next); 847d6d48196SJens Axboe 84842dad764SJerome Marchand /* 84942dad764SJerome Marchand * 'next' is going away, so update stats accordingly 85042dad764SJerome Marchand */ 851b9c54f56SKonstantin Khlebnikov blk_account_io_merge_request(next); 852d6d48196SJens Axboe 853a54895faSChristoph Hellwig trace_block_rq_merge(next); 854f3bdc62fSJan Kara 855e4d750c9SJens Axboe /* 856e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 857e4d750c9SJens Axboe * the caller to free 858e4d750c9SJens Axboe */ 8591cd96c24SBoaz Harrosh next->bio = NULL; 860b973cb7eSJens Axboe return next; 861d6d48196SJens Axboe } 862d6d48196SJens Axboe 863eda5cc99SChristoph Hellwig static struct request *attempt_back_merge(struct request_queue *q, 864eda5cc99SChristoph Hellwig struct request *rq) 865d6d48196SJens Axboe { 866d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 867d6d48196SJens Axboe 868d6d48196SJens Axboe if (next) 869d6d48196SJens Axboe return attempt_merge(q, rq, next); 870d6d48196SJens Axboe 871b973cb7eSJens Axboe return NULL; 872d6d48196SJens Axboe } 873d6d48196SJens Axboe 874eda5cc99SChristoph Hellwig static struct request *attempt_front_merge(struct request_queue *q, 875eda5cc99SChristoph Hellwig struct request *rq) 876d6d48196SJens Axboe { 877d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 878d6d48196SJens Axboe 879d6d48196SJens Axboe if (prev) 880d6d48196SJens Axboe return attempt_merge(q, prev, rq); 881d6d48196SJens Axboe 882b973cb7eSJens Axboe return NULL; 883d6d48196SJens Axboe } 8845e84ea3aSJens Axboe 885fd2ef39cSJan Kara /* 886fd2ef39cSJan Kara * Try to merge 'next' into 'rq'. Return true if the merge happened, false 887fd2ef39cSJan Kara * otherwise. The caller is responsible for freeing 'next' if the merge 888fd2ef39cSJan Kara * happened. 889fd2ef39cSJan Kara */ 890fd2ef39cSJan Kara bool blk_attempt_req_merge(struct request_queue *q, struct request *rq, 8915e84ea3aSJens Axboe struct request *next) 8925e84ea3aSJens Axboe { 893fd2ef39cSJan Kara return attempt_merge(q, rq, next); 8945e84ea3aSJens Axboe } 895050c8ea8STejun Heo 896050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 897050c8ea8STejun Heo { 898e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 899050c8ea8STejun Heo return false; 900050c8ea8STejun Heo 901288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 902f31dc1cdSMartin K. Petersen return false; 903f31dc1cdSMartin K. Petersen 904050c8ea8STejun Heo /* different data direction or already started, don't merge */ 905050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 906050c8ea8STejun Heo return false; 907050c8ea8STejun Heo 9082081a56bSJens Axboe /* must be same device */ 909309dca30SChristoph Hellwig if (rq->rq_disk != bio->bi_bdev->bd_disk) 910050c8ea8STejun Heo return false; 911050c8ea8STejun Heo 912050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 9134eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 914050c8ea8STejun Heo return false; 915050c8ea8STejun Heo 916a892c8d5SSatya Tangirala /* Only merge if the crypt contexts are compatible */ 917a892c8d5SSatya Tangirala if (!bio_crypt_rq_ctx_compatible(rq, bio)) 918a892c8d5SSatya Tangirala return false; 919a892c8d5SSatya Tangirala 9204363ac7cSMartin K. Petersen /* must be using the same buffer */ 9218fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 9224363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 9234363ac7cSMartin K. Petersen return false; 9244363ac7cSMartin K. Petersen 925cb6934f8SJens Axboe /* 926cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 927cb6934f8SJens Axboe * non-hint IO. 928cb6934f8SJens Axboe */ 929cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 930cb6934f8SJens Axboe return false; 931cb6934f8SJens Axboe 932668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio)) 933668ffc03SDamien Le Moal return false; 934668ffc03SDamien Le Moal 935050c8ea8STejun Heo return true; 936050c8ea8STejun Heo } 937050c8ea8STejun Heo 93834fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 939050c8ea8STejun Heo { 94069840466SJianchao Wang if (blk_discard_mergable(rq)) 9411e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 9421e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 943050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 9444f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 945050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 946050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 947050c8ea8STejun Heo } 9488e756373SBaolin Wang 9498e756373SBaolin Wang static void blk_account_io_merge_bio(struct request *req) 9508e756373SBaolin Wang { 9518e756373SBaolin Wang if (!blk_do_io_stat(req)) 9528e756373SBaolin Wang return; 9538e756373SBaolin Wang 9548e756373SBaolin Wang part_stat_lock(); 9558e756373SBaolin Wang part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); 9568e756373SBaolin Wang part_stat_unlock(); 9578e756373SBaolin Wang } 9588e756373SBaolin Wang 959eda5cc99SChristoph Hellwig enum bio_merge_status { 960eda5cc99SChristoph Hellwig BIO_MERGE_OK, 961eda5cc99SChristoph Hellwig BIO_MERGE_NONE, 962eda5cc99SChristoph Hellwig BIO_MERGE_FAILED, 963eda5cc99SChristoph Hellwig }; 964eda5cc99SChristoph Hellwig 965eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_back_merge(struct request *req, 966eda5cc99SChristoph Hellwig struct bio *bio, unsigned int nr_segs) 9678e756373SBaolin Wang { 9688e756373SBaolin Wang const int ff = bio->bi_opf & REQ_FAILFAST_MASK; 9698e756373SBaolin Wang 9708e756373SBaolin Wang if (!ll_back_merge_fn(req, bio, nr_segs)) 9717d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 9728e756373SBaolin Wang 973e8a676d6SChristoph Hellwig trace_block_bio_backmerge(bio); 9748e756373SBaolin Wang rq_qos_merge(req->q, req, bio); 9758e756373SBaolin Wang 9768e756373SBaolin Wang if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) 9778e756373SBaolin Wang blk_rq_set_mixed_merge(req); 9788e756373SBaolin Wang 9798e756373SBaolin Wang req->biotail->bi_next = bio; 9808e756373SBaolin Wang req->biotail = bio; 9818e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 9828e756373SBaolin Wang 9838e756373SBaolin Wang bio_crypt_free_ctx(bio); 9848e756373SBaolin Wang 9858e756373SBaolin Wang blk_account_io_merge_bio(req); 9867d7ca7c5SBaolin Wang return BIO_MERGE_OK; 9878e756373SBaolin Wang } 9888e756373SBaolin Wang 989eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_front_merge(struct request *req, 990eda5cc99SChristoph Hellwig struct bio *bio, unsigned int nr_segs) 9918e756373SBaolin Wang { 9928e756373SBaolin Wang const int ff = bio->bi_opf & REQ_FAILFAST_MASK; 9938e756373SBaolin Wang 9948e756373SBaolin Wang if (!ll_front_merge_fn(req, bio, nr_segs)) 9957d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 9968e756373SBaolin Wang 997e8a676d6SChristoph Hellwig trace_block_bio_frontmerge(bio); 9988e756373SBaolin Wang rq_qos_merge(req->q, req, bio); 9998e756373SBaolin Wang 10008e756373SBaolin Wang if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) 10018e756373SBaolin Wang blk_rq_set_mixed_merge(req); 10028e756373SBaolin Wang 10038e756373SBaolin Wang bio->bi_next = req->bio; 10048e756373SBaolin Wang req->bio = bio; 10058e756373SBaolin Wang 10068e756373SBaolin Wang req->__sector = bio->bi_iter.bi_sector; 10078e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 10088e756373SBaolin Wang 10098e756373SBaolin Wang bio_crypt_do_front_merge(req, bio); 10108e756373SBaolin Wang 10118e756373SBaolin Wang blk_account_io_merge_bio(req); 10127d7ca7c5SBaolin Wang return BIO_MERGE_OK; 10138e756373SBaolin Wang } 10148e756373SBaolin Wang 1015eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_discard_merge(struct request_queue *q, 1016eda5cc99SChristoph Hellwig struct request *req, struct bio *bio) 10178e756373SBaolin Wang { 10188e756373SBaolin Wang unsigned short segments = blk_rq_nr_discard_segments(req); 10198e756373SBaolin Wang 10208e756373SBaolin Wang if (segments >= queue_max_discard_segments(q)) 10218e756373SBaolin Wang goto no_merge; 10228e756373SBaolin Wang if (blk_rq_sectors(req) + bio_sectors(bio) > 10238e756373SBaolin Wang blk_rq_get_max_sectors(req, blk_rq_pos(req))) 10248e756373SBaolin Wang goto no_merge; 10258e756373SBaolin Wang 10268e756373SBaolin Wang rq_qos_merge(q, req, bio); 10278e756373SBaolin Wang 10288e756373SBaolin Wang req->biotail->bi_next = bio; 10298e756373SBaolin Wang req->biotail = bio; 10308e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 10318e756373SBaolin Wang req->nr_phys_segments = segments + 1; 10328e756373SBaolin Wang 10338e756373SBaolin Wang blk_account_io_merge_bio(req); 10347d7ca7c5SBaolin Wang return BIO_MERGE_OK; 10358e756373SBaolin Wang no_merge: 10368e756373SBaolin Wang req_set_nomerge(q, req); 10377d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 10387d7ca7c5SBaolin Wang } 10397d7ca7c5SBaolin Wang 10407d7ca7c5SBaolin Wang static enum bio_merge_status blk_attempt_bio_merge(struct request_queue *q, 10417d7ca7c5SBaolin Wang struct request *rq, 10427d7ca7c5SBaolin Wang struct bio *bio, 10437d7ca7c5SBaolin Wang unsigned int nr_segs, 10447d7ca7c5SBaolin Wang bool sched_allow_merge) 10457d7ca7c5SBaolin Wang { 10467d7ca7c5SBaolin Wang if (!blk_rq_merge_ok(rq, bio)) 10477d7ca7c5SBaolin Wang return BIO_MERGE_NONE; 10487d7ca7c5SBaolin Wang 10497d7ca7c5SBaolin Wang switch (blk_try_merge(rq, bio)) { 10507d7ca7c5SBaolin Wang case ELEVATOR_BACK_MERGE: 1051265600b7SBaolin Wang if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio)) 10527d7ca7c5SBaolin Wang return bio_attempt_back_merge(rq, bio, nr_segs); 10537d7ca7c5SBaolin Wang break; 10547d7ca7c5SBaolin Wang case ELEVATOR_FRONT_MERGE: 1055265600b7SBaolin Wang if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio)) 10567d7ca7c5SBaolin Wang return bio_attempt_front_merge(rq, bio, nr_segs); 10577d7ca7c5SBaolin Wang break; 10587d7ca7c5SBaolin Wang case ELEVATOR_DISCARD_MERGE: 10597d7ca7c5SBaolin Wang return bio_attempt_discard_merge(q, rq, bio); 10607d7ca7c5SBaolin Wang default: 10617d7ca7c5SBaolin Wang return BIO_MERGE_NONE; 10627d7ca7c5SBaolin Wang } 10637d7ca7c5SBaolin Wang 10647d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 10658e756373SBaolin Wang } 10668e756373SBaolin Wang 10678e756373SBaolin Wang /** 10688e756373SBaolin Wang * blk_attempt_plug_merge - try to merge with %current's plugged list 10698e756373SBaolin Wang * @q: request_queue new bio is being queued at 10708e756373SBaolin Wang * @bio: new bio being queued 10718e756373SBaolin Wang * @nr_segs: number of segments in @bio 107287c037d1SJens Axboe * from the passed in @q already in the plug list 10738e756373SBaolin Wang * 1074d38a9c04SJens Axboe * Determine whether @bio being queued on @q can be merged with the previous 1075d38a9c04SJens Axboe * request on %current's plugged list. Returns %true if merge was successful, 10768e756373SBaolin Wang * otherwise %false. 10778e756373SBaolin Wang * 10788e756373SBaolin Wang * Plugging coalesces IOs from the same issuer for the same purpose without 10798e756373SBaolin Wang * going through @q->queue_lock. As such it's more of an issuing mechanism 10808e756373SBaolin Wang * than scheduling, and the request, while may have elvpriv data, is not 10818e756373SBaolin Wang * added on the elevator at this point. In addition, we don't have 10828e756373SBaolin Wang * reliable access to the elevator outside queue lock. Only check basic 10838e756373SBaolin Wang * merging parameters without querying the elevator. 10848e756373SBaolin Wang * 10858e756373SBaolin Wang * Caller must ensure !blk_queue_nomerges(q) beforehand. 10868e756373SBaolin Wang */ 10878e756373SBaolin Wang bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio, 10880c5bcc92SChristoph Hellwig unsigned int nr_segs) 10898e756373SBaolin Wang { 10908e756373SBaolin Wang struct blk_plug *plug; 10918e756373SBaolin Wang struct request *rq; 10928e756373SBaolin Wang 10938e756373SBaolin Wang plug = blk_mq_plug(q, bio); 1094bc490f81SJens Axboe if (!plug || rq_list_empty(plug->mq_list)) 10958e756373SBaolin Wang return false; 10968e756373SBaolin Wang 1097d38a9c04SJens Axboe /* check the previously added entry for a quick merge attempt */ 1098bc490f81SJens Axboe rq = rq_list_peek(&plug->mq_list); 109987c037d1SJens Axboe if (rq->q == q) { 1100a1cb6537SMing Lei if (blk_attempt_bio_merge(q, rq, bio, nr_segs, false) == 1101a1cb6537SMing Lei BIO_MERGE_OK) 11028e756373SBaolin Wang return true; 1103a1cb6537SMing Lei } 11048e756373SBaolin Wang return false; 11058e756373SBaolin Wang } 1106bdc6a287SBaolin Wang 1107bdc6a287SBaolin Wang /* 1108bdc6a287SBaolin Wang * Iterate list of requests and see if we can merge this bio with any 1109bdc6a287SBaolin Wang * of them. 1110bdc6a287SBaolin Wang */ 1111bdc6a287SBaolin Wang bool blk_bio_list_merge(struct request_queue *q, struct list_head *list, 1112bdc6a287SBaolin Wang struct bio *bio, unsigned int nr_segs) 1113bdc6a287SBaolin Wang { 1114bdc6a287SBaolin Wang struct request *rq; 1115bdc6a287SBaolin Wang int checked = 8; 1116bdc6a287SBaolin Wang 1117bdc6a287SBaolin Wang list_for_each_entry_reverse(rq, list, queuelist) { 1118bdc6a287SBaolin Wang if (!checked--) 1119bdc6a287SBaolin Wang break; 1120bdc6a287SBaolin Wang 11217d7ca7c5SBaolin Wang switch (blk_attempt_bio_merge(q, rq, bio, nr_segs, true)) { 11227d7ca7c5SBaolin Wang case BIO_MERGE_NONE: 1123bdc6a287SBaolin Wang continue; 11247d7ca7c5SBaolin Wang case BIO_MERGE_OK: 11257d7ca7c5SBaolin Wang return true; 11267d7ca7c5SBaolin Wang case BIO_MERGE_FAILED: 11277d7ca7c5SBaolin Wang return false; 1128bdc6a287SBaolin Wang } 1129bdc6a287SBaolin Wang 1130bdc6a287SBaolin Wang } 1131bdc6a287SBaolin Wang 1132bdc6a287SBaolin Wang return false; 1133bdc6a287SBaolin Wang } 1134bdc6a287SBaolin Wang EXPORT_SYMBOL_GPL(blk_bio_list_merge); 1135eda5cc99SChristoph Hellwig 1136eda5cc99SChristoph Hellwig bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio, 1137eda5cc99SChristoph Hellwig unsigned int nr_segs, struct request **merged_request) 1138eda5cc99SChristoph Hellwig { 1139eda5cc99SChristoph Hellwig struct request *rq; 1140eda5cc99SChristoph Hellwig 1141eda5cc99SChristoph Hellwig switch (elv_merge(q, &rq, bio)) { 1142eda5cc99SChristoph Hellwig case ELEVATOR_BACK_MERGE: 1143eda5cc99SChristoph Hellwig if (!blk_mq_sched_allow_merge(q, rq, bio)) 1144eda5cc99SChristoph Hellwig return false; 1145eda5cc99SChristoph Hellwig if (bio_attempt_back_merge(rq, bio, nr_segs) != BIO_MERGE_OK) 1146eda5cc99SChristoph Hellwig return false; 1147eda5cc99SChristoph Hellwig *merged_request = attempt_back_merge(q, rq); 1148eda5cc99SChristoph Hellwig if (!*merged_request) 1149eda5cc99SChristoph Hellwig elv_merged_request(q, rq, ELEVATOR_BACK_MERGE); 1150eda5cc99SChristoph Hellwig return true; 1151eda5cc99SChristoph Hellwig case ELEVATOR_FRONT_MERGE: 1152eda5cc99SChristoph Hellwig if (!blk_mq_sched_allow_merge(q, rq, bio)) 1153eda5cc99SChristoph Hellwig return false; 1154eda5cc99SChristoph Hellwig if (bio_attempt_front_merge(rq, bio, nr_segs) != BIO_MERGE_OK) 1155eda5cc99SChristoph Hellwig return false; 1156eda5cc99SChristoph Hellwig *merged_request = attempt_front_merge(q, rq); 1157eda5cc99SChristoph Hellwig if (!*merged_request) 1158eda5cc99SChristoph Hellwig elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE); 1159eda5cc99SChristoph Hellwig return true; 1160eda5cc99SChristoph Hellwig case ELEVATOR_DISCARD_MERGE: 1161eda5cc99SChristoph Hellwig return bio_attempt_discard_merge(q, rq, bio) == BIO_MERGE_OK; 1162eda5cc99SChristoph Hellwig default: 1163eda5cc99SChristoph Hellwig return false; 1164eda5cc99SChristoph Hellwig } 1165eda5cc99SChristoph Hellwig } 1166eda5cc99SChristoph Hellwig EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge); 1167