1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d6d48196SJens Axboe /* 3d6d48196SJens Axboe * Functions related to segment and merge handling 4d6d48196SJens Axboe */ 5d6d48196SJens Axboe #include <linux/kernel.h> 6d6d48196SJens Axboe #include <linux/module.h> 7d6d48196SJens Axboe #include <linux/bio.h> 8d6d48196SJens Axboe #include <linux/blkdev.h> 9d6d48196SJens Axboe #include <linux/scatterlist.h> 10d6d48196SJens Axboe 11cda22646SMike Krinkin #include <trace/events/block.h> 12cda22646SMike Krinkin 13d6d48196SJens Axboe #include "blk.h" 148e756373SBaolin Wang #include "blk-rq-qos.h" 15d6d48196SJens Axboe 16e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q, 17e9907009SChristoph Hellwig struct request *prev_rq, struct bio *prev, struct bio *next) 18e9907009SChristoph Hellwig { 19e9907009SChristoph Hellwig struct bio_vec pb, nb; 20e9907009SChristoph Hellwig 21e9907009SChristoph Hellwig if (!bio_has_data(prev) || !queue_virt_boundary(q)) 22e9907009SChristoph Hellwig return false; 23e9907009SChristoph Hellwig 24e9907009SChristoph Hellwig /* 25e9907009SChristoph Hellwig * Don't merge if the 1st bio starts with non-zero offset, otherwise it 26e9907009SChristoph Hellwig * is quite difficult to respect the sg gap limit. We work hard to 27e9907009SChristoph Hellwig * merge a huge number of small single bios in case of mkfs. 28e9907009SChristoph Hellwig */ 29e9907009SChristoph Hellwig if (prev_rq) 30e9907009SChristoph Hellwig bio_get_first_bvec(prev_rq->bio, &pb); 31e9907009SChristoph Hellwig else 32e9907009SChristoph Hellwig bio_get_first_bvec(prev, &pb); 33df376b2eSJohannes Thumshirn if (pb.bv_offset & queue_virt_boundary(q)) 34e9907009SChristoph Hellwig return true; 35e9907009SChristoph Hellwig 36e9907009SChristoph Hellwig /* 37e9907009SChristoph Hellwig * We don't need to worry about the situation that the merged segment 38e9907009SChristoph Hellwig * ends in unaligned virt boundary: 39e9907009SChristoph Hellwig * 40e9907009SChristoph Hellwig * - if 'pb' ends aligned, the merged segment ends aligned 41e9907009SChristoph Hellwig * - if 'pb' ends unaligned, the next bio must include 42e9907009SChristoph Hellwig * one single bvec of 'nb', otherwise the 'nb' can't 43e9907009SChristoph Hellwig * merge with 'pb' 44e9907009SChristoph Hellwig */ 45e9907009SChristoph Hellwig bio_get_last_bvec(prev, &pb); 46e9907009SChristoph Hellwig bio_get_first_bvec(next, &nb); 47200a9affSChristoph Hellwig if (biovec_phys_mergeable(q, &pb, &nb)) 48e9907009SChristoph Hellwig return false; 49e9907009SChristoph Hellwig return __bvec_gap_to_prev(q, &pb, nb.bv_offset); 50e9907009SChristoph Hellwig } 51e9907009SChristoph Hellwig 52e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio) 53e9907009SChristoph Hellwig { 54e9907009SChristoph Hellwig return bio_will_gap(req->q, req, req->biotail, bio); 55e9907009SChristoph Hellwig } 56e9907009SChristoph Hellwig 57e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio) 58e9907009SChristoph Hellwig { 59e9907009SChristoph Hellwig return bio_will_gap(req->q, NULL, bio, req->bio); 60e9907009SChristoph Hellwig } 61e9907009SChristoph Hellwig 6254efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 6354efd50bSKent Overstreet struct bio *bio, 64bdced438SMing Lei struct bio_set *bs, 65bdced438SMing Lei unsigned *nsegs) 6654efd50bSKent Overstreet { 6754efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 6854efd50bSKent Overstreet int alignment; 6954efd50bSKent Overstreet sector_t tmp; 7054efd50bSKent Overstreet unsigned split_sectors; 7154efd50bSKent Overstreet 72bdced438SMing Lei *nsegs = 1; 73bdced438SMing Lei 7454efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 7554efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 7654efd50bSKent Overstreet 771adfc5e4SMing Lei max_discard_sectors = min(q->limits.max_discard_sectors, 781adfc5e4SMing Lei bio_allowed_max_sectors(q)); 7954efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 8054efd50bSKent Overstreet 8154efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 8254efd50bSKent Overstreet /* XXX: warn */ 8354efd50bSKent Overstreet return NULL; 8454efd50bSKent Overstreet } 8554efd50bSKent Overstreet 8654efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 8754efd50bSKent Overstreet return NULL; 8854efd50bSKent Overstreet 8954efd50bSKent Overstreet split_sectors = max_discard_sectors; 9054efd50bSKent Overstreet 9154efd50bSKent Overstreet /* 9254efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 9354efd50bSKent Overstreet * the previous aligned sector. 9454efd50bSKent Overstreet */ 9554efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 9654efd50bSKent Overstreet 9754efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 9854efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 9954efd50bSKent Overstreet 10054efd50bSKent Overstreet if (split_sectors > tmp) 10154efd50bSKent Overstreet split_sectors -= tmp; 10254efd50bSKent Overstreet 10354efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 10454efd50bSKent Overstreet } 10554efd50bSKent Overstreet 106885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, 107885fa13fSChristoph Hellwig struct bio *bio, struct bio_set *bs, unsigned *nsegs) 108885fa13fSChristoph Hellwig { 109d665e12aSChristoph Hellwig *nsegs = 0; 110885fa13fSChristoph Hellwig 111885fa13fSChristoph Hellwig if (!q->limits.max_write_zeroes_sectors) 112885fa13fSChristoph Hellwig return NULL; 113885fa13fSChristoph Hellwig 114885fa13fSChristoph Hellwig if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) 115885fa13fSChristoph Hellwig return NULL; 116885fa13fSChristoph Hellwig 117885fa13fSChristoph Hellwig return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); 118885fa13fSChristoph Hellwig } 119885fa13fSChristoph Hellwig 12054efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 12154efd50bSKent Overstreet struct bio *bio, 122bdced438SMing Lei struct bio_set *bs, 123bdced438SMing Lei unsigned *nsegs) 12454efd50bSKent Overstreet { 125bdced438SMing Lei *nsegs = 1; 126bdced438SMing Lei 12754efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 12854efd50bSKent Overstreet return NULL; 12954efd50bSKent Overstreet 13054efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 13154efd50bSKent Overstreet return NULL; 13254efd50bSKent Overstreet 13354efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 13454efd50bSKent Overstreet } 13554efd50bSKent Overstreet 1369cc5169cSBart Van Assche /* 1379cc5169cSBart Van Assche * Return the maximum number of sectors from the start of a bio that may be 1389cc5169cSBart Van Assche * submitted as a single request to a block device. If enough sectors remain, 1399cc5169cSBart Van Assche * align the end to the physical block size. Otherwise align the end to the 1409cc5169cSBart Van Assche * logical block size. This approach minimizes the number of non-aligned 1419cc5169cSBart Van Assche * requests that are submitted to a block device if the start of a bio is not 1429cc5169cSBart Van Assche * aligned to a physical block boundary. 1439cc5169cSBart Van Assche */ 144d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 145d0e5fbb0SMing Lei struct bio *bio) 146d0e5fbb0SMing Lei { 1473ee16db3SMike Snitzer unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector, 0); 1489cc5169cSBart Van Assche unsigned max_sectors = sectors; 1499cc5169cSBart Van Assche unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT; 1509cc5169cSBart Van Assche unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT; 1519cc5169cSBart Van Assche unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1); 152d0e5fbb0SMing Lei 1539cc5169cSBart Van Assche max_sectors += start_offset; 1549cc5169cSBart Van Assche max_sectors &= ~(pbs - 1); 1559cc5169cSBart Van Assche if (max_sectors > start_offset) 1569cc5169cSBart Van Assche return max_sectors - start_offset; 157d0e5fbb0SMing Lei 158e4b469c6SKeith Busch return sectors & ~(lbs - 1); 159d0e5fbb0SMing Lei } 160d0e5fbb0SMing Lei 161429120f3SMing Lei static inline unsigned get_max_segment_size(const struct request_queue *q, 162429120f3SMing Lei struct page *start_page, 163429120f3SMing Lei unsigned long offset) 164dcebd755SMing Lei { 165dcebd755SMing Lei unsigned long mask = queue_segment_boundary(q); 166dcebd755SMing Lei 167429120f3SMing Lei offset = mask & (page_to_phys(start_page) + offset); 1684a2f704eSMing Lei 1694a2f704eSMing Lei /* 1704a2f704eSMing Lei * overflow may be triggered in case of zero page physical address 1714a2f704eSMing Lei * on 32bit arch, use queue's max segment size when that happens. 1724a2f704eSMing Lei */ 1734a2f704eSMing Lei return min_not_zero(mask - offset + 1, 1744a2f704eSMing Lei (unsigned long)queue_max_segment_size(q)); 175dcebd755SMing Lei } 176dcebd755SMing Lei 177708b25b3SBart Van Assche /** 178708b25b3SBart Van Assche * bvec_split_segs - verify whether or not a bvec should be split in the middle 179708b25b3SBart Van Assche * @q: [in] request queue associated with the bio associated with @bv 180708b25b3SBart Van Assche * @bv: [in] bvec to examine 181708b25b3SBart Van Assche * @nsegs: [in,out] Number of segments in the bio being built. Incremented 182708b25b3SBart Van Assche * by the number of segments from @bv that may be appended to that 183708b25b3SBart Van Assche * bio without exceeding @max_segs 184708b25b3SBart Van Assche * @sectors: [in,out] Number of sectors in the bio being built. Incremented 185708b25b3SBart Van Assche * by the number of sectors from @bv that may be appended to that 186708b25b3SBart Van Assche * bio without exceeding @max_sectors 187708b25b3SBart Van Assche * @max_segs: [in] upper bound for *@nsegs 188708b25b3SBart Van Assche * @max_sectors: [in] upper bound for *@sectors 189708b25b3SBart Van Assche * 190708b25b3SBart Van Assche * When splitting a bio, it can happen that a bvec is encountered that is too 191708b25b3SBart Van Assche * big to fit in a single segment and hence that it has to be split in the 192708b25b3SBart Van Assche * middle. This function verifies whether or not that should happen. The value 193708b25b3SBart Van Assche * %true is returned if and only if appending the entire @bv to a bio with 194708b25b3SBart Van Assche * *@nsegs segments and *@sectors sectors would make that bio unacceptable for 195708b25b3SBart Van Assche * the block driver. 196dcebd755SMing Lei */ 197af2c68feSBart Van Assche static bool bvec_split_segs(const struct request_queue *q, 198af2c68feSBart Van Assche const struct bio_vec *bv, unsigned *nsegs, 199708b25b3SBart Van Assche unsigned *sectors, unsigned max_segs, 200708b25b3SBart Van Assche unsigned max_sectors) 201dcebd755SMing Lei { 202708b25b3SBart Van Assche unsigned max_len = (min(max_sectors, UINT_MAX >> 9) - *sectors) << 9; 203708b25b3SBart Van Assche unsigned len = min(bv->bv_len, max_len); 204dcebd755SMing Lei unsigned total_len = 0; 205ff9811b3SBart Van Assche unsigned seg_size = 0; 206dcebd755SMing Lei 207ff9811b3SBart Van Assche while (len && *nsegs < max_segs) { 208429120f3SMing Lei seg_size = get_max_segment_size(q, bv->bv_page, 209429120f3SMing Lei bv->bv_offset + total_len); 210dcebd755SMing Lei seg_size = min(seg_size, len); 211dcebd755SMing Lei 212ff9811b3SBart Van Assche (*nsegs)++; 213dcebd755SMing Lei total_len += seg_size; 214dcebd755SMing Lei len -= seg_size; 215dcebd755SMing Lei 216dcebd755SMing Lei if ((bv->bv_offset + total_len) & queue_virt_boundary(q)) 217dcebd755SMing Lei break; 218dcebd755SMing Lei } 219dcebd755SMing Lei 220dcebd755SMing Lei *sectors += total_len >> 9; 221dcebd755SMing Lei 222708b25b3SBart Van Assche /* tell the caller to split the bvec if it is too big to fit */ 223708b25b3SBart Van Assche return len > 0 || bv->bv_len > max_len; 224dcebd755SMing Lei } 225dcebd755SMing Lei 226dad77584SBart Van Assche /** 227dad77584SBart Van Assche * blk_bio_segment_split - split a bio in two bios 228dad77584SBart Van Assche * @q: [in] request queue pointer 229dad77584SBart Van Assche * @bio: [in] bio to be split 230dad77584SBart Van Assche * @bs: [in] bio set to allocate the clone from 231dad77584SBart Van Assche * @segs: [out] number of segments in the bio with the first half of the sectors 232dad77584SBart Van Assche * 233dad77584SBart Van Assche * Clone @bio, update the bi_iter of the clone to represent the first sectors 234dad77584SBart Van Assche * of @bio and update @bio->bi_iter to represent the remaining sectors. The 235dad77584SBart Van Assche * following is guaranteed for the cloned bio: 236dad77584SBart Van Assche * - That it has at most get_max_io_size(@q, @bio) sectors. 237dad77584SBart Van Assche * - That it has at most queue_max_segments(@q) segments. 238dad77584SBart Van Assche * 239dad77584SBart Van Assche * Except for discard requests the cloned bio will point at the bi_io_vec of 240dad77584SBart Van Assche * the original bio. It is the responsibility of the caller to ensure that the 241dad77584SBart Van Assche * original bio is not freed before the cloned bio. The caller is also 242dad77584SBart Van Assche * responsible for ensuring that @bs is only destroyed after processing of the 243dad77584SBart Van Assche * split bio has finished. 244dad77584SBart Van Assche */ 24554efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 24654efd50bSKent Overstreet struct bio *bio, 247bdced438SMing Lei struct bio_set *bs, 248bdced438SMing Lei unsigned *segs) 24954efd50bSKent Overstreet { 2505014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 25154efd50bSKent Overstreet struct bvec_iter iter; 2526869875fSChristoph Hellwig unsigned nsegs = 0, sectors = 0; 253d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 25405b700baSMing Lei const unsigned max_segs = queue_max_segments(q); 25554efd50bSKent Overstreet 256dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 25754efd50bSKent Overstreet /* 25854efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 25954efd50bSKent Overstreet * offset would create a gap, disallow it. 26054efd50bSKent Overstreet */ 2615014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 26254efd50bSKent Overstreet goto split; 26354efd50bSKent Overstreet 26405b700baSMing Lei if (nsegs < max_segs && 265708b25b3SBart Van Assche sectors + (bv.bv_len >> 9) <= max_sectors && 266708b25b3SBart Van Assche bv.bv_offset + bv.bv_len <= PAGE_SIZE) { 267708b25b3SBart Van Assche nsegs++; 268708b25b3SBart Van Assche sectors += bv.bv_len >> 9; 269708b25b3SBart Van Assche } else if (bvec_split_segs(q, &bv, &nsegs, §ors, max_segs, 270708b25b3SBart Van Assche max_sectors)) { 271e36f6204SKeith Busch goto split; 272e36f6204SKeith Busch } 273e36f6204SKeith Busch 27454efd50bSKent Overstreet bvprv = bv; 275578270bfSMing Lei bvprvp = &bvprv; 27654efd50bSKent Overstreet } 27754efd50bSKent Overstreet 278d627065dSChristoph Hellwig *segs = nsegs; 279d627065dSChristoph Hellwig return NULL; 28054efd50bSKent Overstreet split: 281bdced438SMing Lei *segs = nsegs; 282cc29e1bfSJeffle Xu 283cc29e1bfSJeffle Xu /* 284cc29e1bfSJeffle Xu * Bio splitting may cause subtle trouble such as hang when doing sync 285cc29e1bfSJeffle Xu * iopoll in direct IO routine. Given performance gain of iopoll for 286cc29e1bfSJeffle Xu * big IO can be trival, disable iopoll when split needed. 287cc29e1bfSJeffle Xu */ 288cc29e1bfSJeffle Xu bio->bi_opf &= ~REQ_HIPRI; 289cc29e1bfSJeffle Xu 290d627065dSChristoph Hellwig return bio_split(bio, sectors, GFP_NOIO, bs); 29154efd50bSKent Overstreet } 29254efd50bSKent Overstreet 293dad77584SBart Van Assche /** 294dad77584SBart Van Assche * __blk_queue_split - split a bio and submit the second half 295dad77584SBart Van Assche * @bio: [in, out] bio to be split 296dad77584SBart Van Assche * @nr_segs: [out] number of segments in the first bio 297dad77584SBart Van Assche * 298dad77584SBart Van Assche * Split a bio into two bios, chain the two bios, submit the second half and 299dad77584SBart Van Assche * store a pointer to the first half in *@bio. If the second bio is still too 300dad77584SBart Van Assche * big it will be split by a recursive call to this function. Since this 301309dca30SChristoph Hellwig * function may allocate a new bio from q->bio_split, it is the responsibility 302309dca30SChristoph Hellwig * of the caller to ensure that q->bio_split is only released after processing 303309dca30SChristoph Hellwig * of the split bio has finished. 304dad77584SBart Van Assche */ 305f695ca38SChristoph Hellwig void __blk_queue_split(struct bio **bio, unsigned int *nr_segs) 30654efd50bSKent Overstreet { 307309dca30SChristoph Hellwig struct request_queue *q = (*bio)->bi_bdev->bd_disk->queue; 308fa532287SChristoph Hellwig struct bio *split = NULL; 30954efd50bSKent Overstreet 3107afafc8aSAdrian Hunter switch (bio_op(*bio)) { 3117afafc8aSAdrian Hunter case REQ_OP_DISCARD: 3127afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 31314ccb66bSChristoph Hellwig split = blk_bio_discard_split(q, *bio, &q->bio_split, nr_segs); 3147afafc8aSAdrian Hunter break; 315a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 31614ccb66bSChristoph Hellwig split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, 31714ccb66bSChristoph Hellwig nr_segs); 318a6f0788eSChaitanya Kulkarni break; 3197afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 32014ccb66bSChristoph Hellwig split = blk_bio_write_same_split(q, *bio, &q->bio_split, 32114ccb66bSChristoph Hellwig nr_segs); 3227afafc8aSAdrian Hunter break; 3237afafc8aSAdrian Hunter default: 324fa532287SChristoph Hellwig /* 325fa532287SChristoph Hellwig * All drivers must accept single-segments bios that are <= 326fa532287SChristoph Hellwig * PAGE_SIZE. This is a quick and dirty check that relies on 327fa532287SChristoph Hellwig * the fact that bi_io_vec[0] is always valid if a bio has data. 328fa532287SChristoph Hellwig * The check might lead to occasional false negatives when bios 329fa532287SChristoph Hellwig * are cloned, but compared to the performance impact of cloned 330fa532287SChristoph Hellwig * bios themselves the loop below doesn't matter anyway. 331fa532287SChristoph Hellwig */ 332fa532287SChristoph Hellwig if (!q->limits.chunk_sectors && 333fa532287SChristoph Hellwig (*bio)->bi_vcnt == 1 && 33459db8ba2SMing Lei ((*bio)->bi_io_vec[0].bv_len + 3351e279153SJens Axboe (*bio)->bi_io_vec[0].bv_offset) <= PAGE_SIZE) { 336fa532287SChristoph Hellwig *nr_segs = 1; 337fa532287SChristoph Hellwig break; 338fa532287SChristoph Hellwig } 33914ccb66bSChristoph Hellwig split = blk_bio_segment_split(q, *bio, &q->bio_split, nr_segs); 3407afafc8aSAdrian Hunter break; 3417afafc8aSAdrian Hunter } 342bdced438SMing Lei 34354efd50bSKent Overstreet if (split) { 3446ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 3451eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 3466ac45aebSMing Lei 34754efd50bSKent Overstreet bio_chain(split, *bio); 348eb6f7f7cSChristoph Hellwig trace_block_split(split, (*bio)->bi_iter.bi_sector); 349ed00aabdSChristoph Hellwig submit_bio_noacct(*bio); 35054efd50bSKent Overstreet *bio = split; 351*4f1e9630SChunguang Xu 352*4f1e9630SChunguang Xu blk_throtl_charge_bio_split(*bio); 35354efd50bSKent Overstreet } 35454efd50bSKent Overstreet } 35514ccb66bSChristoph Hellwig 356dad77584SBart Van Assche /** 357dad77584SBart Van Assche * blk_queue_split - split a bio and submit the second half 358dad77584SBart Van Assche * @bio: [in, out] bio to be split 359dad77584SBart Van Assche * 360dad77584SBart Van Assche * Split a bio into two bios, chains the two bios, submit the second half and 361dad77584SBart Van Assche * store a pointer to the first half in *@bio. Since this function may allocate 362309dca30SChristoph Hellwig * a new bio from q->bio_split, it is the responsibility of the caller to ensure 363309dca30SChristoph Hellwig * that q->bio_split is only released after processing of the split bio has 364309dca30SChristoph Hellwig * finished. 365dad77584SBart Van Assche */ 366f695ca38SChristoph Hellwig void blk_queue_split(struct bio **bio) 36714ccb66bSChristoph Hellwig { 36814ccb66bSChristoph Hellwig unsigned int nr_segs; 36914ccb66bSChristoph Hellwig 370f695ca38SChristoph Hellwig __blk_queue_split(bio, &nr_segs); 37114ccb66bSChristoph Hellwig } 37254efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 37354efd50bSKent Overstreet 374e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq) 375d6d48196SJens Axboe { 3766869875fSChristoph Hellwig unsigned int nr_phys_segs = 0; 377ff9811b3SBart Van Assche unsigned int nr_sectors = 0; 378e9cd19c0SChristoph Hellwig struct req_iterator iter; 3796869875fSChristoph Hellwig struct bio_vec bv; 380d6d48196SJens Axboe 381e9cd19c0SChristoph Hellwig if (!rq->bio) 3821e428079SJens Axboe return 0; 383d6d48196SJens Axboe 384e9cd19c0SChristoph Hellwig switch (bio_op(rq->bio)) { 385a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 386a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 387a958937fSDavid Jeffery if (queue_max_discard_segments(rq->q) > 1) { 388a958937fSDavid Jeffery struct bio *bio = rq->bio; 389a958937fSDavid Jeffery 390a958937fSDavid Jeffery for_each_bio(bio) 391a958937fSDavid Jeffery nr_phys_segs++; 392a958937fSDavid Jeffery return nr_phys_segs; 393a958937fSDavid Jeffery } 394a958937fSDavid Jeffery return 1; 395a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 396f9d03f96SChristoph Hellwig return 0; 397f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 3985cb8850cSKent Overstreet return 1; 399a6f0788eSChaitanya Kulkarni } 4005cb8850cSKent Overstreet 401e9cd19c0SChristoph Hellwig rq_for_each_bvec(bv, rq, iter) 402ff9811b3SBart Van Assche bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors, 403708b25b3SBart Van Assche UINT_MAX, UINT_MAX); 4041e428079SJens Axboe return nr_phys_segs; 4051e428079SJens Axboe } 4061e428079SJens Axboe 40748d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, 408862e5a5eSMing Lei struct scatterlist *sglist) 409862e5a5eSMing Lei { 410862e5a5eSMing Lei if (!*sg) 411862e5a5eSMing Lei return sglist; 412862e5a5eSMing Lei 413862e5a5eSMing Lei /* 414862e5a5eSMing Lei * If the driver previously mapped a shorter list, we could see a 415862e5a5eSMing Lei * termination bit prematurely unless it fully inits the sg table 416862e5a5eSMing Lei * on each mapping. We KNOW that there must be more entries here 417862e5a5eSMing Lei * or the driver would be buggy, so force clear the termination bit 418862e5a5eSMing Lei * to avoid doing a full sg_init_table() in drivers for each command. 419862e5a5eSMing Lei */ 420862e5a5eSMing Lei sg_unmark_end(*sg); 421862e5a5eSMing Lei return sg_next(*sg); 422862e5a5eSMing Lei } 423862e5a5eSMing Lei 424862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q, 425862e5a5eSMing Lei struct bio_vec *bvec, struct scatterlist *sglist, 426862e5a5eSMing Lei struct scatterlist **sg) 427862e5a5eSMing Lei { 428862e5a5eSMing Lei unsigned nbytes = bvec->bv_len; 4298a96a0e4SChristoph Hellwig unsigned nsegs = 0, total = 0; 430862e5a5eSMing Lei 431862e5a5eSMing Lei while (nbytes > 0) { 4328a96a0e4SChristoph Hellwig unsigned offset = bvec->bv_offset + total; 433429120f3SMing Lei unsigned len = min(get_max_segment_size(q, bvec->bv_page, 434429120f3SMing Lei offset), nbytes); 435f9f76879SChristoph Hellwig struct page *page = bvec->bv_page; 436f9f76879SChristoph Hellwig 437f9f76879SChristoph Hellwig /* 438f9f76879SChristoph Hellwig * Unfortunately a fair number of drivers barf on scatterlists 439f9f76879SChristoph Hellwig * that have an offset larger than PAGE_SIZE, despite other 440f9f76879SChristoph Hellwig * subsystems dealing with that invariant just fine. For now 441f9f76879SChristoph Hellwig * stick to the legacy format where we never present those from 442f9f76879SChristoph Hellwig * the block layer, but the code below should be removed once 443f9f76879SChristoph Hellwig * these offenders (mostly MMC/SD drivers) are fixed. 444f9f76879SChristoph Hellwig */ 445f9f76879SChristoph Hellwig page += (offset >> PAGE_SHIFT); 446f9f76879SChristoph Hellwig offset &= ~PAGE_MASK; 447862e5a5eSMing Lei 448862e5a5eSMing Lei *sg = blk_next_sg(sg, sglist); 449f9f76879SChristoph Hellwig sg_set_page(*sg, page, len, offset); 450862e5a5eSMing Lei 4518a96a0e4SChristoph Hellwig total += len; 4528a96a0e4SChristoph Hellwig nbytes -= len; 453862e5a5eSMing Lei nsegs++; 454862e5a5eSMing Lei } 455862e5a5eSMing Lei 456862e5a5eSMing Lei return nsegs; 457862e5a5eSMing Lei } 458862e5a5eSMing Lei 45916e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv, 46016e3e418SMing Lei struct scatterlist *sglist, struct scatterlist **sg) 46116e3e418SMing Lei { 46216e3e418SMing Lei *sg = blk_next_sg(sg, sglist); 46316e3e418SMing Lei sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 46416e3e418SMing Lei return 1; 46516e3e418SMing Lei } 46616e3e418SMing Lei 467f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */ 468f6970f83SMing Lei static inline bool 469f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec, 470f6970f83SMing Lei struct bio_vec *bvprv, struct scatterlist **sg) 471963ab9e5SAsias He { 472963ab9e5SAsias He 473963ab9e5SAsias He int nbytes = bvec->bv_len; 474963ab9e5SAsias He 475f6970f83SMing Lei if (!*sg) 476f6970f83SMing Lei return false; 477f6970f83SMing Lei 478b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 479f6970f83SMing Lei return false; 480f6970f83SMing Lei 4813dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec)) 482f6970f83SMing Lei return false; 483963ab9e5SAsias He 484963ab9e5SAsias He (*sg)->length += nbytes; 485f6970f83SMing Lei 486f6970f83SMing Lei return true; 487963ab9e5SAsias He } 488963ab9e5SAsias He 4895cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 4905cb8850cSKent Overstreet struct scatterlist *sglist, 4915cb8850cSKent Overstreet struct scatterlist **sg) 4925cb8850cSKent Overstreet { 4933f649ab7SKees Cook struct bio_vec bvec, bvprv = { NULL }; 4945cb8850cSKent Overstreet struct bvec_iter iter; 49538417468SChristoph Hellwig int nsegs = 0; 496f6970f83SMing Lei bool new_bio = false; 4975cb8850cSKent Overstreet 498f6970f83SMing Lei for_each_bio(bio) { 499f6970f83SMing Lei bio_for_each_bvec(bvec, bio, iter) { 500f6970f83SMing Lei /* 501f6970f83SMing Lei * Only try to merge bvecs from two bios given we 502f6970f83SMing Lei * have done bio internal merge when adding pages 503f6970f83SMing Lei * to bio 504f6970f83SMing Lei */ 505f6970f83SMing Lei if (new_bio && 506f6970f83SMing Lei __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg)) 507f6970f83SMing Lei goto next_bvec; 508f6970f83SMing Lei 509f6970f83SMing Lei if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE) 510f6970f83SMing Lei nsegs += __blk_bvec_map_sg(bvec, sglist, sg); 511f6970f83SMing Lei else 512f6970f83SMing Lei nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg); 513f6970f83SMing Lei next_bvec: 514f6970f83SMing Lei new_bio = false; 515f6970f83SMing Lei } 516b21e11c5SMing Lei if (likely(bio->bi_iter.bi_size)) { 517f6970f83SMing Lei bvprv = bvec; 518f6970f83SMing Lei new_bio = true; 519f6970f83SMing Lei } 520b21e11c5SMing Lei } 5215cb8850cSKent Overstreet 5225cb8850cSKent Overstreet return nsegs; 5235cb8850cSKent Overstreet } 5245cb8850cSKent Overstreet 525d6d48196SJens Axboe /* 526d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 527d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 528d6d48196SJens Axboe */ 52989de1504SChristoph Hellwig int __blk_rq_map_sg(struct request_queue *q, struct request *rq, 53089de1504SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **last_sg) 531d6d48196SJens Axboe { 5325cb8850cSKent Overstreet int nsegs = 0; 533d6d48196SJens Axboe 534f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 53589de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg); 536f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 53789de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, last_sg); 538f9d03f96SChristoph Hellwig else if (rq->bio) 53989de1504SChristoph Hellwig nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg); 540f18573abSFUJITA Tomonori 54189de1504SChristoph Hellwig if (*last_sg) 54289de1504SChristoph Hellwig sg_mark_end(*last_sg); 543d6d48196SJens Axboe 54412e57f59SMing Lei /* 54512e57f59SMing Lei * Something must have been wrong if the figured number of 54612e57f59SMing Lei * segment is bigger than number of req's physical segments 54712e57f59SMing Lei */ 548f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 54912e57f59SMing Lei 550d6d48196SJens Axboe return nsegs; 551d6d48196SJens Axboe } 55289de1504SChristoph Hellwig EXPORT_SYMBOL(__blk_rq_map_sg); 553d6d48196SJens Axboe 554943b40c8SMing Lei static inline unsigned int blk_rq_get_max_segments(struct request *rq) 555943b40c8SMing Lei { 556943b40c8SMing Lei if (req_op(rq) == REQ_OP_DISCARD) 557943b40c8SMing Lei return queue_max_discard_segments(rq->q); 558943b40c8SMing Lei return queue_max_segments(rq->q); 559943b40c8SMing Lei } 560943b40c8SMing Lei 56114ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio, 56214ccb66bSChristoph Hellwig unsigned int nr_phys_segs) 563d6d48196SJens Axboe { 5642705dfb2SMing Lei if (blk_integrity_merge_bio(req->q, req, bio) == false) 56513f05c8dSMartin K. Petersen goto no_merge; 56613f05c8dSMartin K. Petersen 5672705dfb2SMing Lei /* discard request merge won't add new segment */ 5682705dfb2SMing Lei if (req_op(req) == REQ_OP_DISCARD) 5692705dfb2SMing Lei return 1; 5702705dfb2SMing Lei 5712705dfb2SMing Lei if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req)) 57213f05c8dSMartin K. Petersen goto no_merge; 573d6d48196SJens Axboe 574d6d48196SJens Axboe /* 575d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 576d6d48196SJens Axboe * counters. 577d6d48196SJens Axboe */ 578d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 579d6d48196SJens Axboe return 1; 58013f05c8dSMartin K. Petersen 58113f05c8dSMartin K. Petersen no_merge: 58214ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 58313f05c8dSMartin K. Petersen return 0; 584d6d48196SJens Axboe } 585d6d48196SJens Axboe 58614ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs) 587d6d48196SJens Axboe { 5885e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 5895e7c4274SJens Axboe return 0; 5907f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5917f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 5927f39add3SSagi Grimberg return 0; 593a892c8d5SSatya Tangirala if (!bio_crypt_ctx_back_mergeable(req, bio)) 594a892c8d5SSatya Tangirala return 0; 595f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 59617007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 59714ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 598d6d48196SJens Axboe return 0; 599d6d48196SJens Axboe } 600d6d48196SJens Axboe 60114ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 602d6d48196SJens Axboe } 603d6d48196SJens Axboe 604eda5cc99SChristoph Hellwig static int ll_front_merge_fn(struct request *req, struct bio *bio, 605eda5cc99SChristoph Hellwig unsigned int nr_segs) 606d6d48196SJens Axboe { 6075e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 6085e7c4274SJens Axboe return 0; 6097f39add3SSagi Grimberg if (blk_integrity_rq(req) && 6107f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 6117f39add3SSagi Grimberg return 0; 612a892c8d5SSatya Tangirala if (!bio_crypt_ctx_front_mergeable(req, bio)) 613a892c8d5SSatya Tangirala return 0; 614f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 61517007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 61614ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 617d6d48196SJens Axboe return 0; 618d6d48196SJens Axboe } 619d6d48196SJens Axboe 62014ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 621d6d48196SJens Axboe } 622d6d48196SJens Axboe 623445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 624445251d0SJens Axboe struct request *next) 625445251d0SJens Axboe { 626445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 627445251d0SJens Axboe 628445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 629445251d0SJens Axboe goto no_merge; 630445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 631445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 632445251d0SJens Axboe goto no_merge; 633445251d0SJens Axboe 634445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 635445251d0SJens Axboe return true; 636445251d0SJens Axboe no_merge: 637445251d0SJens Axboe req_set_nomerge(q, req); 638445251d0SJens Axboe return false; 639445251d0SJens Axboe } 640445251d0SJens Axboe 641d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 642d6d48196SJens Axboe struct request *next) 643d6d48196SJens Axboe { 644d6d48196SJens Axboe int total_phys_segments; 645d6d48196SJens Axboe 6465e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 647854fbb9cSKeith Busch return 0; 648854fbb9cSKeith Busch 649d6d48196SJens Axboe /* 650d6d48196SJens Axboe * Will it become too large? 651d6d48196SJens Axboe */ 652f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 65317007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 654d6d48196SJens Axboe return 0; 655d6d48196SJens Axboe 656d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 657943b40c8SMing Lei if (total_phys_segments > blk_rq_get_max_segments(req)) 658d6d48196SJens Axboe return 0; 659d6d48196SJens Axboe 6604eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 66113f05c8dSMartin K. Petersen return 0; 66213f05c8dSMartin K. Petersen 663a892c8d5SSatya Tangirala if (!bio_crypt_ctx_merge_rq(req, next)) 664a892c8d5SSatya Tangirala return 0; 665a892c8d5SSatya Tangirala 666d6d48196SJens Axboe /* Merge is OK... */ 667d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 668d6d48196SJens Axboe return 1; 669d6d48196SJens Axboe } 670d6d48196SJens Axboe 67180a761fdSTejun Heo /** 67280a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 67380a761fdSTejun Heo * @rq: request to mark as mixed merge 67480a761fdSTejun Heo * 67580a761fdSTejun Heo * Description: 67680a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 67780a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 67880a761fdSTejun Heo * merged. 67980a761fdSTejun Heo */ 68080a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 68180a761fdSTejun Heo { 68280a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 68380a761fdSTejun Heo struct bio *bio; 68480a761fdSTejun Heo 685e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 68680a761fdSTejun Heo return; 68780a761fdSTejun Heo 68880a761fdSTejun Heo /* 68980a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 69080a761fdSTejun Heo * contained bios. It will just track those of the first one. 69180a761fdSTejun Heo * Distributes the attributs to each bio. 69280a761fdSTejun Heo */ 69380a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 6941eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 6951eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 6961eff9d32SJens Axboe bio->bi_opf |= ff; 69780a761fdSTejun Heo } 698e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 69980a761fdSTejun Heo } 70080a761fdSTejun Heo 701b9c54f56SKonstantin Khlebnikov static void blk_account_io_merge_request(struct request *req) 70226308eabSJerome Marchand { 70326308eabSJerome Marchand if (blk_do_io_stat(req)) { 704112f158fSMike Snitzer part_stat_lock(); 705b9c54f56SKonstantin Khlebnikov part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); 70626308eabSJerome Marchand part_stat_unlock(); 70726308eabSJerome Marchand } 70826308eabSJerome Marchand } 709b9c54f56SKonstantin Khlebnikov 710e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req, 711e96c0d83SEric Biggers struct request *next) 71269840466SJianchao Wang { 71369840466SJianchao Wang if (blk_discard_mergable(req)) 71469840466SJianchao Wang return ELEVATOR_DISCARD_MERGE; 71569840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) 71669840466SJianchao Wang return ELEVATOR_BACK_MERGE; 71769840466SJianchao Wang 71869840466SJianchao Wang return ELEVATOR_NO_MERGE; 71969840466SJianchao Wang } 72026308eabSJerome Marchand 721d6d48196SJens Axboe /* 722b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 723b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 724d6d48196SJens Axboe */ 725b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 726b973cb7eSJens Axboe struct request *req, struct request *next) 727d6d48196SJens Axboe { 728d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 729b973cb7eSJens Axboe return NULL; 730d6d48196SJens Axboe 731288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 732b973cb7eSJens Axboe return NULL; 733f31dc1cdSMartin K. Petersen 734d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 7352081a56bSJens Axboe || req->rq_disk != next->rq_disk) 736b973cb7eSJens Axboe return NULL; 737d6d48196SJens Axboe 7388fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 7394363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 740b973cb7eSJens Axboe return NULL; 7414363ac7cSMartin K. Petersen 742d6d48196SJens Axboe /* 743cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 744cb6934f8SJens Axboe * non-hint IO. 745cb6934f8SJens Axboe */ 746cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 747cb6934f8SJens Axboe return NULL; 748cb6934f8SJens Axboe 749668ffc03SDamien Le Moal if (req->ioprio != next->ioprio) 750668ffc03SDamien Le Moal return NULL; 751668ffc03SDamien Le Moal 752cb6934f8SJens Axboe /* 753d6d48196SJens Axboe * If we are allowed to merge, then append bio list 754d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 755d6d48196SJens Axboe * will have updated segment counts, update sector 756445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 757445251d0SJens Axboe * have separate settings. 758d6d48196SJens Axboe */ 75969840466SJianchao Wang 76069840466SJianchao Wang switch (blk_try_req_merge(req, next)) { 76169840466SJianchao Wang case ELEVATOR_DISCARD_MERGE: 762445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 763445251d0SJens Axboe return NULL; 76469840466SJianchao Wang break; 76569840466SJianchao Wang case ELEVATOR_BACK_MERGE: 76669840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next)) 767b973cb7eSJens Axboe return NULL; 76869840466SJianchao Wang break; 76969840466SJianchao Wang default: 77069840466SJianchao Wang return NULL; 77169840466SJianchao Wang } 772d6d48196SJens Axboe 773d6d48196SJens Axboe /* 77480a761fdSTejun Heo * If failfast settings disagree or any of the two is already 77580a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 77680a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 77780a761fdSTejun Heo * set properly. 77880a761fdSTejun Heo */ 779e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 78080a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 78180a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 78280a761fdSTejun Heo blk_rq_set_mixed_merge(req); 78380a761fdSTejun Heo blk_rq_set_mixed_merge(next); 78480a761fdSTejun Heo } 78580a761fdSTejun Heo 78680a761fdSTejun Heo /* 787522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 788522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 789522a7775SOmar Sandoval * current request for accounting purposes. 790d6d48196SJens Axboe */ 791522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 792522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 793d6d48196SJens Axboe 794d6d48196SJens Axboe req->biotail->bi_next = next->bio; 795d6d48196SJens Axboe req->biotail = next->biotail; 796d6d48196SJens Axboe 797a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 798d6d48196SJens Axboe 7992a5cf35cSMing Lei if (!blk_discard_mergable(req)) 800d6d48196SJens Axboe elv_merge_requests(q, req, next); 801d6d48196SJens Axboe 80242dad764SJerome Marchand /* 80342dad764SJerome Marchand * 'next' is going away, so update stats accordingly 80442dad764SJerome Marchand */ 805b9c54f56SKonstantin Khlebnikov blk_account_io_merge_request(next); 806d6d48196SJens Axboe 807a54895faSChristoph Hellwig trace_block_rq_merge(next); 808f3bdc62fSJan Kara 809e4d750c9SJens Axboe /* 810e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 811e4d750c9SJens Axboe * the caller to free 812e4d750c9SJens Axboe */ 8131cd96c24SBoaz Harrosh next->bio = NULL; 814b973cb7eSJens Axboe return next; 815d6d48196SJens Axboe } 816d6d48196SJens Axboe 817eda5cc99SChristoph Hellwig static struct request *attempt_back_merge(struct request_queue *q, 818eda5cc99SChristoph Hellwig struct request *rq) 819d6d48196SJens Axboe { 820d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 821d6d48196SJens Axboe 822d6d48196SJens Axboe if (next) 823d6d48196SJens Axboe return attempt_merge(q, rq, next); 824d6d48196SJens Axboe 825b973cb7eSJens Axboe return NULL; 826d6d48196SJens Axboe } 827d6d48196SJens Axboe 828eda5cc99SChristoph Hellwig static struct request *attempt_front_merge(struct request_queue *q, 829eda5cc99SChristoph Hellwig struct request *rq) 830d6d48196SJens Axboe { 831d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 832d6d48196SJens Axboe 833d6d48196SJens Axboe if (prev) 834d6d48196SJens Axboe return attempt_merge(q, prev, rq); 835d6d48196SJens Axboe 836b973cb7eSJens Axboe return NULL; 837d6d48196SJens Axboe } 8385e84ea3aSJens Axboe 839fd2ef39cSJan Kara /* 840fd2ef39cSJan Kara * Try to merge 'next' into 'rq'. Return true if the merge happened, false 841fd2ef39cSJan Kara * otherwise. The caller is responsible for freeing 'next' if the merge 842fd2ef39cSJan Kara * happened. 843fd2ef39cSJan Kara */ 844fd2ef39cSJan Kara bool blk_attempt_req_merge(struct request_queue *q, struct request *rq, 8455e84ea3aSJens Axboe struct request *next) 8465e84ea3aSJens Axboe { 847fd2ef39cSJan Kara return attempt_merge(q, rq, next); 8485e84ea3aSJens Axboe } 849050c8ea8STejun Heo 850050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 851050c8ea8STejun Heo { 852e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 853050c8ea8STejun Heo return false; 854050c8ea8STejun Heo 855288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 856f31dc1cdSMartin K. Petersen return false; 857f31dc1cdSMartin K. Petersen 858050c8ea8STejun Heo /* different data direction or already started, don't merge */ 859050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 860050c8ea8STejun Heo return false; 861050c8ea8STejun Heo 8622081a56bSJens Axboe /* must be same device */ 863309dca30SChristoph Hellwig if (rq->rq_disk != bio->bi_bdev->bd_disk) 864050c8ea8STejun Heo return false; 865050c8ea8STejun Heo 866050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 8674eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 868050c8ea8STejun Heo return false; 869050c8ea8STejun Heo 870a892c8d5SSatya Tangirala /* Only merge if the crypt contexts are compatible */ 871a892c8d5SSatya Tangirala if (!bio_crypt_rq_ctx_compatible(rq, bio)) 872a892c8d5SSatya Tangirala return false; 873a892c8d5SSatya Tangirala 8744363ac7cSMartin K. Petersen /* must be using the same buffer */ 8758fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 8764363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 8774363ac7cSMartin K. Petersen return false; 8784363ac7cSMartin K. Petersen 879cb6934f8SJens Axboe /* 880cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 881cb6934f8SJens Axboe * non-hint IO. 882cb6934f8SJens Axboe */ 883cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 884cb6934f8SJens Axboe return false; 885cb6934f8SJens Axboe 886668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio)) 887668ffc03SDamien Le Moal return false; 888668ffc03SDamien Le Moal 889050c8ea8STejun Heo return true; 890050c8ea8STejun Heo } 891050c8ea8STejun Heo 89234fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 893050c8ea8STejun Heo { 89469840466SJianchao Wang if (blk_discard_mergable(rq)) 8951e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 8961e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 897050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 8984f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 899050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 900050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 901050c8ea8STejun Heo } 9028e756373SBaolin Wang 9038e756373SBaolin Wang static void blk_account_io_merge_bio(struct request *req) 9048e756373SBaolin Wang { 9058e756373SBaolin Wang if (!blk_do_io_stat(req)) 9068e756373SBaolin Wang return; 9078e756373SBaolin Wang 9088e756373SBaolin Wang part_stat_lock(); 9098e756373SBaolin Wang part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); 9108e756373SBaolin Wang part_stat_unlock(); 9118e756373SBaolin Wang } 9128e756373SBaolin Wang 913eda5cc99SChristoph Hellwig enum bio_merge_status { 914eda5cc99SChristoph Hellwig BIO_MERGE_OK, 915eda5cc99SChristoph Hellwig BIO_MERGE_NONE, 916eda5cc99SChristoph Hellwig BIO_MERGE_FAILED, 917eda5cc99SChristoph Hellwig }; 918eda5cc99SChristoph Hellwig 919eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_back_merge(struct request *req, 920eda5cc99SChristoph Hellwig struct bio *bio, unsigned int nr_segs) 9218e756373SBaolin Wang { 9228e756373SBaolin Wang const int ff = bio->bi_opf & REQ_FAILFAST_MASK; 9238e756373SBaolin Wang 9248e756373SBaolin Wang if (!ll_back_merge_fn(req, bio, nr_segs)) 9257d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 9268e756373SBaolin Wang 927e8a676d6SChristoph Hellwig trace_block_bio_backmerge(bio); 9288e756373SBaolin Wang rq_qos_merge(req->q, req, bio); 9298e756373SBaolin Wang 9308e756373SBaolin Wang if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) 9318e756373SBaolin Wang blk_rq_set_mixed_merge(req); 9328e756373SBaolin Wang 9338e756373SBaolin Wang req->biotail->bi_next = bio; 9348e756373SBaolin Wang req->biotail = bio; 9358e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 9368e756373SBaolin Wang 9378e756373SBaolin Wang bio_crypt_free_ctx(bio); 9388e756373SBaolin Wang 9398e756373SBaolin Wang blk_account_io_merge_bio(req); 9407d7ca7c5SBaolin Wang return BIO_MERGE_OK; 9418e756373SBaolin Wang } 9428e756373SBaolin Wang 943eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_front_merge(struct request *req, 944eda5cc99SChristoph Hellwig struct bio *bio, unsigned int nr_segs) 9458e756373SBaolin Wang { 9468e756373SBaolin Wang const int ff = bio->bi_opf & REQ_FAILFAST_MASK; 9478e756373SBaolin Wang 9488e756373SBaolin Wang if (!ll_front_merge_fn(req, bio, nr_segs)) 9497d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 9508e756373SBaolin Wang 951e8a676d6SChristoph Hellwig trace_block_bio_frontmerge(bio); 9528e756373SBaolin Wang rq_qos_merge(req->q, req, bio); 9538e756373SBaolin Wang 9548e756373SBaolin Wang if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) 9558e756373SBaolin Wang blk_rq_set_mixed_merge(req); 9568e756373SBaolin Wang 9578e756373SBaolin Wang bio->bi_next = req->bio; 9588e756373SBaolin Wang req->bio = bio; 9598e756373SBaolin Wang 9608e756373SBaolin Wang req->__sector = bio->bi_iter.bi_sector; 9618e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 9628e756373SBaolin Wang 9638e756373SBaolin Wang bio_crypt_do_front_merge(req, bio); 9648e756373SBaolin Wang 9658e756373SBaolin Wang blk_account_io_merge_bio(req); 9667d7ca7c5SBaolin Wang return BIO_MERGE_OK; 9678e756373SBaolin Wang } 9688e756373SBaolin Wang 969eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_discard_merge(struct request_queue *q, 970eda5cc99SChristoph Hellwig struct request *req, struct bio *bio) 9718e756373SBaolin Wang { 9728e756373SBaolin Wang unsigned short segments = blk_rq_nr_discard_segments(req); 9738e756373SBaolin Wang 9748e756373SBaolin Wang if (segments >= queue_max_discard_segments(q)) 9758e756373SBaolin Wang goto no_merge; 9768e756373SBaolin Wang if (blk_rq_sectors(req) + bio_sectors(bio) > 9778e756373SBaolin Wang blk_rq_get_max_sectors(req, blk_rq_pos(req))) 9788e756373SBaolin Wang goto no_merge; 9798e756373SBaolin Wang 9808e756373SBaolin Wang rq_qos_merge(q, req, bio); 9818e756373SBaolin Wang 9828e756373SBaolin Wang req->biotail->bi_next = bio; 9838e756373SBaolin Wang req->biotail = bio; 9848e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size; 9858e756373SBaolin Wang req->nr_phys_segments = segments + 1; 9868e756373SBaolin Wang 9878e756373SBaolin Wang blk_account_io_merge_bio(req); 9887d7ca7c5SBaolin Wang return BIO_MERGE_OK; 9898e756373SBaolin Wang no_merge: 9908e756373SBaolin Wang req_set_nomerge(q, req); 9917d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 9927d7ca7c5SBaolin Wang } 9937d7ca7c5SBaolin Wang 9947d7ca7c5SBaolin Wang static enum bio_merge_status blk_attempt_bio_merge(struct request_queue *q, 9957d7ca7c5SBaolin Wang struct request *rq, 9967d7ca7c5SBaolin Wang struct bio *bio, 9977d7ca7c5SBaolin Wang unsigned int nr_segs, 9987d7ca7c5SBaolin Wang bool sched_allow_merge) 9997d7ca7c5SBaolin Wang { 10007d7ca7c5SBaolin Wang if (!blk_rq_merge_ok(rq, bio)) 10017d7ca7c5SBaolin Wang return BIO_MERGE_NONE; 10027d7ca7c5SBaolin Wang 10037d7ca7c5SBaolin Wang switch (blk_try_merge(rq, bio)) { 10047d7ca7c5SBaolin Wang case ELEVATOR_BACK_MERGE: 1005265600b7SBaolin Wang if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio)) 10067d7ca7c5SBaolin Wang return bio_attempt_back_merge(rq, bio, nr_segs); 10077d7ca7c5SBaolin Wang break; 10087d7ca7c5SBaolin Wang case ELEVATOR_FRONT_MERGE: 1009265600b7SBaolin Wang if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio)) 10107d7ca7c5SBaolin Wang return bio_attempt_front_merge(rq, bio, nr_segs); 10117d7ca7c5SBaolin Wang break; 10127d7ca7c5SBaolin Wang case ELEVATOR_DISCARD_MERGE: 10137d7ca7c5SBaolin Wang return bio_attempt_discard_merge(q, rq, bio); 10147d7ca7c5SBaolin Wang default: 10157d7ca7c5SBaolin Wang return BIO_MERGE_NONE; 10167d7ca7c5SBaolin Wang } 10177d7ca7c5SBaolin Wang 10187d7ca7c5SBaolin Wang return BIO_MERGE_FAILED; 10198e756373SBaolin Wang } 10208e756373SBaolin Wang 10218e756373SBaolin Wang /** 10228e756373SBaolin Wang * blk_attempt_plug_merge - try to merge with %current's plugged list 10238e756373SBaolin Wang * @q: request_queue new bio is being queued at 10248e756373SBaolin Wang * @bio: new bio being queued 10258e756373SBaolin Wang * @nr_segs: number of segments in @bio 10268e756373SBaolin Wang * @same_queue_rq: pointer to &struct request that gets filled in when 10278e756373SBaolin Wang * another request associated with @q is found on the plug list 10288e756373SBaolin Wang * (optional, may be %NULL) 10298e756373SBaolin Wang * 10308e756373SBaolin Wang * Determine whether @bio being queued on @q can be merged with a request 10318e756373SBaolin Wang * on %current's plugged list. Returns %true if merge was successful, 10328e756373SBaolin Wang * otherwise %false. 10338e756373SBaolin Wang * 10348e756373SBaolin Wang * Plugging coalesces IOs from the same issuer for the same purpose without 10358e756373SBaolin Wang * going through @q->queue_lock. As such it's more of an issuing mechanism 10368e756373SBaolin Wang * than scheduling, and the request, while may have elvpriv data, is not 10378e756373SBaolin Wang * added on the elevator at this point. In addition, we don't have 10388e756373SBaolin Wang * reliable access to the elevator outside queue lock. Only check basic 10398e756373SBaolin Wang * merging parameters without querying the elevator. 10408e756373SBaolin Wang * 10418e756373SBaolin Wang * Caller must ensure !blk_queue_nomerges(q) beforehand. 10428e756373SBaolin Wang */ 10438e756373SBaolin Wang bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio, 10448e756373SBaolin Wang unsigned int nr_segs, struct request **same_queue_rq) 10458e756373SBaolin Wang { 10468e756373SBaolin Wang struct blk_plug *plug; 10478e756373SBaolin Wang struct request *rq; 10488e756373SBaolin Wang struct list_head *plug_list; 10498e756373SBaolin Wang 10508e756373SBaolin Wang plug = blk_mq_plug(q, bio); 10518e756373SBaolin Wang if (!plug) 10528e756373SBaolin Wang return false; 10538e756373SBaolin Wang 10548e756373SBaolin Wang plug_list = &plug->mq_list; 10558e756373SBaolin Wang 10568e756373SBaolin Wang list_for_each_entry_reverse(rq, plug_list, queuelist) { 10578e756373SBaolin Wang if (rq->q == q && same_queue_rq) { 10588e756373SBaolin Wang /* 10598e756373SBaolin Wang * Only blk-mq multiple hardware queues case checks the 10608e756373SBaolin Wang * rq in the same queue, there should be only one such 10618e756373SBaolin Wang * rq in a queue 10628e756373SBaolin Wang **/ 10638e756373SBaolin Wang *same_queue_rq = rq; 10648e756373SBaolin Wang } 10658e756373SBaolin Wang 10667d7ca7c5SBaolin Wang if (rq->q != q) 10678e756373SBaolin Wang continue; 10688e756373SBaolin Wang 10697d7ca7c5SBaolin Wang if (blk_attempt_bio_merge(q, rq, bio, nr_segs, false) == 10707d7ca7c5SBaolin Wang BIO_MERGE_OK) 10718e756373SBaolin Wang return true; 10728e756373SBaolin Wang } 10738e756373SBaolin Wang 10748e756373SBaolin Wang return false; 10758e756373SBaolin Wang } 1076bdc6a287SBaolin Wang 1077bdc6a287SBaolin Wang /* 1078bdc6a287SBaolin Wang * Iterate list of requests and see if we can merge this bio with any 1079bdc6a287SBaolin Wang * of them. 1080bdc6a287SBaolin Wang */ 1081bdc6a287SBaolin Wang bool blk_bio_list_merge(struct request_queue *q, struct list_head *list, 1082bdc6a287SBaolin Wang struct bio *bio, unsigned int nr_segs) 1083bdc6a287SBaolin Wang { 1084bdc6a287SBaolin Wang struct request *rq; 1085bdc6a287SBaolin Wang int checked = 8; 1086bdc6a287SBaolin Wang 1087bdc6a287SBaolin Wang list_for_each_entry_reverse(rq, list, queuelist) { 1088bdc6a287SBaolin Wang if (!checked--) 1089bdc6a287SBaolin Wang break; 1090bdc6a287SBaolin Wang 10917d7ca7c5SBaolin Wang switch (blk_attempt_bio_merge(q, rq, bio, nr_segs, true)) { 10927d7ca7c5SBaolin Wang case BIO_MERGE_NONE: 1093bdc6a287SBaolin Wang continue; 10947d7ca7c5SBaolin Wang case BIO_MERGE_OK: 10957d7ca7c5SBaolin Wang return true; 10967d7ca7c5SBaolin Wang case BIO_MERGE_FAILED: 10977d7ca7c5SBaolin Wang return false; 1098bdc6a287SBaolin Wang } 1099bdc6a287SBaolin Wang 1100bdc6a287SBaolin Wang } 1101bdc6a287SBaolin Wang 1102bdc6a287SBaolin Wang return false; 1103bdc6a287SBaolin Wang } 1104bdc6a287SBaolin Wang EXPORT_SYMBOL_GPL(blk_bio_list_merge); 1105eda5cc99SChristoph Hellwig 1106eda5cc99SChristoph Hellwig bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio, 1107eda5cc99SChristoph Hellwig unsigned int nr_segs, struct request **merged_request) 1108eda5cc99SChristoph Hellwig { 1109eda5cc99SChristoph Hellwig struct request *rq; 1110eda5cc99SChristoph Hellwig 1111eda5cc99SChristoph Hellwig switch (elv_merge(q, &rq, bio)) { 1112eda5cc99SChristoph Hellwig case ELEVATOR_BACK_MERGE: 1113eda5cc99SChristoph Hellwig if (!blk_mq_sched_allow_merge(q, rq, bio)) 1114eda5cc99SChristoph Hellwig return false; 1115eda5cc99SChristoph Hellwig if (bio_attempt_back_merge(rq, bio, nr_segs) != BIO_MERGE_OK) 1116eda5cc99SChristoph Hellwig return false; 1117eda5cc99SChristoph Hellwig *merged_request = attempt_back_merge(q, rq); 1118eda5cc99SChristoph Hellwig if (!*merged_request) 1119eda5cc99SChristoph Hellwig elv_merged_request(q, rq, ELEVATOR_BACK_MERGE); 1120eda5cc99SChristoph Hellwig return true; 1121eda5cc99SChristoph Hellwig case ELEVATOR_FRONT_MERGE: 1122eda5cc99SChristoph Hellwig if (!blk_mq_sched_allow_merge(q, rq, bio)) 1123eda5cc99SChristoph Hellwig return false; 1124eda5cc99SChristoph Hellwig if (bio_attempt_front_merge(rq, bio, nr_segs) != BIO_MERGE_OK) 1125eda5cc99SChristoph Hellwig return false; 1126eda5cc99SChristoph Hellwig *merged_request = attempt_front_merge(q, rq); 1127eda5cc99SChristoph Hellwig if (!*merged_request) 1128eda5cc99SChristoph Hellwig elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE); 1129eda5cc99SChristoph Hellwig return true; 1130eda5cc99SChristoph Hellwig case ELEVATOR_DISCARD_MERGE: 1131eda5cc99SChristoph Hellwig return bio_attempt_discard_merge(q, rq, bio) == BIO_MERGE_OK; 1132eda5cc99SChristoph Hellwig default: 1133eda5cc99SChristoph Hellwig return false; 1134eda5cc99SChristoph Hellwig } 1135eda5cc99SChristoph Hellwig } 1136eda5cc99SChristoph Hellwig EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge); 1137