1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d6d48196SJens Axboe /* 3d6d48196SJens Axboe * Functions related to segment and merge handling 4d6d48196SJens Axboe */ 5d6d48196SJens Axboe #include <linux/kernel.h> 6d6d48196SJens Axboe #include <linux/module.h> 7d6d48196SJens Axboe #include <linux/bio.h> 8d6d48196SJens Axboe #include <linux/blkdev.h> 9d6d48196SJens Axboe #include <linux/scatterlist.h> 10d6d48196SJens Axboe 11cda22646SMike Krinkin #include <trace/events/block.h> 12cda22646SMike Krinkin 13d6d48196SJens Axboe #include "blk.h" 14d6d48196SJens Axboe 15e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q, 16e9907009SChristoph Hellwig struct request *prev_rq, struct bio *prev, struct bio *next) 17e9907009SChristoph Hellwig { 18e9907009SChristoph Hellwig struct bio_vec pb, nb; 19e9907009SChristoph Hellwig 20e9907009SChristoph Hellwig if (!bio_has_data(prev) || !queue_virt_boundary(q)) 21e9907009SChristoph Hellwig return false; 22e9907009SChristoph Hellwig 23e9907009SChristoph Hellwig /* 24e9907009SChristoph Hellwig * Don't merge if the 1st bio starts with non-zero offset, otherwise it 25e9907009SChristoph Hellwig * is quite difficult to respect the sg gap limit. We work hard to 26e9907009SChristoph Hellwig * merge a huge number of small single bios in case of mkfs. 27e9907009SChristoph Hellwig */ 28e9907009SChristoph Hellwig if (prev_rq) 29e9907009SChristoph Hellwig bio_get_first_bvec(prev_rq->bio, &pb); 30e9907009SChristoph Hellwig else 31e9907009SChristoph Hellwig bio_get_first_bvec(prev, &pb); 32df376b2eSJohannes Thumshirn if (pb.bv_offset & queue_virt_boundary(q)) 33e9907009SChristoph Hellwig return true; 34e9907009SChristoph Hellwig 35e9907009SChristoph Hellwig /* 36e9907009SChristoph Hellwig * We don't need to worry about the situation that the merged segment 37e9907009SChristoph Hellwig * ends in unaligned virt boundary: 38e9907009SChristoph Hellwig * 39e9907009SChristoph Hellwig * - if 'pb' ends aligned, the merged segment ends aligned 40e9907009SChristoph Hellwig * - if 'pb' ends unaligned, the next bio must include 41e9907009SChristoph Hellwig * one single bvec of 'nb', otherwise the 'nb' can't 42e9907009SChristoph Hellwig * merge with 'pb' 43e9907009SChristoph Hellwig */ 44e9907009SChristoph Hellwig bio_get_last_bvec(prev, &pb); 45e9907009SChristoph Hellwig bio_get_first_bvec(next, &nb); 46200a9affSChristoph Hellwig if (biovec_phys_mergeable(q, &pb, &nb)) 47e9907009SChristoph Hellwig return false; 48e9907009SChristoph Hellwig return __bvec_gap_to_prev(q, &pb, nb.bv_offset); 49e9907009SChristoph Hellwig } 50e9907009SChristoph Hellwig 51e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio) 52e9907009SChristoph Hellwig { 53e9907009SChristoph Hellwig return bio_will_gap(req->q, req, req->biotail, bio); 54e9907009SChristoph Hellwig } 55e9907009SChristoph Hellwig 56e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio) 57e9907009SChristoph Hellwig { 58e9907009SChristoph Hellwig return bio_will_gap(req->q, NULL, bio, req->bio); 59e9907009SChristoph Hellwig } 60e9907009SChristoph Hellwig 6154efd50bSKent Overstreet static struct bio *blk_bio_discard_split(struct request_queue *q, 6254efd50bSKent Overstreet struct bio *bio, 63bdced438SMing Lei struct bio_set *bs, 64bdced438SMing Lei unsigned *nsegs) 6554efd50bSKent Overstreet { 6654efd50bSKent Overstreet unsigned int max_discard_sectors, granularity; 6754efd50bSKent Overstreet int alignment; 6854efd50bSKent Overstreet sector_t tmp; 6954efd50bSKent Overstreet unsigned split_sectors; 7054efd50bSKent Overstreet 71bdced438SMing Lei *nsegs = 1; 72bdced438SMing Lei 7354efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */ 7454efd50bSKent Overstreet granularity = max(q->limits.discard_granularity >> 9, 1U); 7554efd50bSKent Overstreet 761adfc5e4SMing Lei max_discard_sectors = min(q->limits.max_discard_sectors, 771adfc5e4SMing Lei bio_allowed_max_sectors(q)); 7854efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity; 7954efd50bSKent Overstreet 8054efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) { 8154efd50bSKent Overstreet /* XXX: warn */ 8254efd50bSKent Overstreet return NULL; 8354efd50bSKent Overstreet } 8454efd50bSKent Overstreet 8554efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors) 8654efd50bSKent Overstreet return NULL; 8754efd50bSKent Overstreet 8854efd50bSKent Overstreet split_sectors = max_discard_sectors; 8954efd50bSKent Overstreet 9054efd50bSKent Overstreet /* 9154efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at 9254efd50bSKent Overstreet * the previous aligned sector. 9354efd50bSKent Overstreet */ 9454efd50bSKent Overstreet alignment = (q->limits.discard_alignment >> 9) % granularity; 9554efd50bSKent Overstreet 9654efd50bSKent Overstreet tmp = bio->bi_iter.bi_sector + split_sectors - alignment; 9754efd50bSKent Overstreet tmp = sector_div(tmp, granularity); 9854efd50bSKent Overstreet 9954efd50bSKent Overstreet if (split_sectors > tmp) 10054efd50bSKent Overstreet split_sectors -= tmp; 10154efd50bSKent Overstreet 10254efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs); 10354efd50bSKent Overstreet } 10454efd50bSKent Overstreet 105885fa13fSChristoph Hellwig static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, 106885fa13fSChristoph Hellwig struct bio *bio, struct bio_set *bs, unsigned *nsegs) 107885fa13fSChristoph Hellwig { 108d665e12aSChristoph Hellwig *nsegs = 0; 109885fa13fSChristoph Hellwig 110885fa13fSChristoph Hellwig if (!q->limits.max_write_zeroes_sectors) 111885fa13fSChristoph Hellwig return NULL; 112885fa13fSChristoph Hellwig 113885fa13fSChristoph Hellwig if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) 114885fa13fSChristoph Hellwig return NULL; 115885fa13fSChristoph Hellwig 116885fa13fSChristoph Hellwig return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); 117885fa13fSChristoph Hellwig } 118885fa13fSChristoph Hellwig 11954efd50bSKent Overstreet static struct bio *blk_bio_write_same_split(struct request_queue *q, 12054efd50bSKent Overstreet struct bio *bio, 121bdced438SMing Lei struct bio_set *bs, 122bdced438SMing Lei unsigned *nsegs) 12354efd50bSKent Overstreet { 124bdced438SMing Lei *nsegs = 1; 125bdced438SMing Lei 12654efd50bSKent Overstreet if (!q->limits.max_write_same_sectors) 12754efd50bSKent Overstreet return NULL; 12854efd50bSKent Overstreet 12954efd50bSKent Overstreet if (bio_sectors(bio) <= q->limits.max_write_same_sectors) 13054efd50bSKent Overstreet return NULL; 13154efd50bSKent Overstreet 13254efd50bSKent Overstreet return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); 13354efd50bSKent Overstreet } 13454efd50bSKent Overstreet 1359cc5169cSBart Van Assche /* 1369cc5169cSBart Van Assche * Return the maximum number of sectors from the start of a bio that may be 1379cc5169cSBart Van Assche * submitted as a single request to a block device. If enough sectors remain, 1389cc5169cSBart Van Assche * align the end to the physical block size. Otherwise align the end to the 1399cc5169cSBart Van Assche * logical block size. This approach minimizes the number of non-aligned 1409cc5169cSBart Van Assche * requests that are submitted to a block device if the start of a bio is not 1419cc5169cSBart Van Assche * aligned to a physical block boundary. 1429cc5169cSBart Van Assche */ 143d0e5fbb0SMing Lei static inline unsigned get_max_io_size(struct request_queue *q, 144d0e5fbb0SMing Lei struct bio *bio) 145d0e5fbb0SMing Lei { 146d0e5fbb0SMing Lei unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector); 1479cc5169cSBart Van Assche unsigned max_sectors = sectors; 1489cc5169cSBart Van Assche unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT; 1499cc5169cSBart Van Assche unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT; 1509cc5169cSBart Van Assche unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1); 151d0e5fbb0SMing Lei 1529cc5169cSBart Van Assche max_sectors += start_offset; 1539cc5169cSBart Van Assche max_sectors &= ~(pbs - 1); 1549cc5169cSBart Van Assche if (max_sectors > start_offset) 1559cc5169cSBart Van Assche return max_sectors - start_offset; 156d0e5fbb0SMing Lei 1579cc5169cSBart Van Assche return sectors & (lbs - 1); 158d0e5fbb0SMing Lei } 159d0e5fbb0SMing Lei 160429120f3SMing Lei static inline unsigned get_max_segment_size(const struct request_queue *q, 161429120f3SMing Lei struct page *start_page, 162429120f3SMing Lei unsigned long offset) 163dcebd755SMing Lei { 164dcebd755SMing Lei unsigned long mask = queue_segment_boundary(q); 165dcebd755SMing Lei 166429120f3SMing Lei offset = mask & (page_to_phys(start_page) + offset); 1674a2f704eSMing Lei 1684a2f704eSMing Lei /* 1694a2f704eSMing Lei * overflow may be triggered in case of zero page physical address 1704a2f704eSMing Lei * on 32bit arch, use queue's max segment size when that happens. 1714a2f704eSMing Lei */ 1724a2f704eSMing Lei return min_not_zero(mask - offset + 1, 1734a2f704eSMing Lei (unsigned long)queue_max_segment_size(q)); 174dcebd755SMing Lei } 175dcebd755SMing Lei 176708b25b3SBart Van Assche /** 177708b25b3SBart Van Assche * bvec_split_segs - verify whether or not a bvec should be split in the middle 178708b25b3SBart Van Assche * @q: [in] request queue associated with the bio associated with @bv 179708b25b3SBart Van Assche * @bv: [in] bvec to examine 180708b25b3SBart Van Assche * @nsegs: [in,out] Number of segments in the bio being built. Incremented 181708b25b3SBart Van Assche * by the number of segments from @bv that may be appended to that 182708b25b3SBart Van Assche * bio without exceeding @max_segs 183708b25b3SBart Van Assche * @sectors: [in,out] Number of sectors in the bio being built. Incremented 184708b25b3SBart Van Assche * by the number of sectors from @bv that may be appended to that 185708b25b3SBart Van Assche * bio without exceeding @max_sectors 186708b25b3SBart Van Assche * @max_segs: [in] upper bound for *@nsegs 187708b25b3SBart Van Assche * @max_sectors: [in] upper bound for *@sectors 188708b25b3SBart Van Assche * 189708b25b3SBart Van Assche * When splitting a bio, it can happen that a bvec is encountered that is too 190708b25b3SBart Van Assche * big to fit in a single segment and hence that it has to be split in the 191708b25b3SBart Van Assche * middle. This function verifies whether or not that should happen. The value 192708b25b3SBart Van Assche * %true is returned if and only if appending the entire @bv to a bio with 193708b25b3SBart Van Assche * *@nsegs segments and *@sectors sectors would make that bio unacceptable for 194708b25b3SBart Van Assche * the block driver. 195dcebd755SMing Lei */ 196af2c68feSBart Van Assche static bool bvec_split_segs(const struct request_queue *q, 197af2c68feSBart Van Assche const struct bio_vec *bv, unsigned *nsegs, 198708b25b3SBart Van Assche unsigned *sectors, unsigned max_segs, 199708b25b3SBart Van Assche unsigned max_sectors) 200dcebd755SMing Lei { 201708b25b3SBart Van Assche unsigned max_len = (min(max_sectors, UINT_MAX >> 9) - *sectors) << 9; 202708b25b3SBart Van Assche unsigned len = min(bv->bv_len, max_len); 203dcebd755SMing Lei unsigned total_len = 0; 204ff9811b3SBart Van Assche unsigned seg_size = 0; 205dcebd755SMing Lei 206ff9811b3SBart Van Assche while (len && *nsegs < max_segs) { 207429120f3SMing Lei seg_size = get_max_segment_size(q, bv->bv_page, 208429120f3SMing Lei bv->bv_offset + total_len); 209dcebd755SMing Lei seg_size = min(seg_size, len); 210dcebd755SMing Lei 211ff9811b3SBart Van Assche (*nsegs)++; 212dcebd755SMing Lei total_len += seg_size; 213dcebd755SMing Lei len -= seg_size; 214dcebd755SMing Lei 215dcebd755SMing Lei if ((bv->bv_offset + total_len) & queue_virt_boundary(q)) 216dcebd755SMing Lei break; 217dcebd755SMing Lei } 218dcebd755SMing Lei 219dcebd755SMing Lei *sectors += total_len >> 9; 220dcebd755SMing Lei 221708b25b3SBart Van Assche /* tell the caller to split the bvec if it is too big to fit */ 222708b25b3SBart Van Assche return len > 0 || bv->bv_len > max_len; 223dcebd755SMing Lei } 224dcebd755SMing Lei 225dad77584SBart Van Assche /** 226dad77584SBart Van Assche * blk_bio_segment_split - split a bio in two bios 227dad77584SBart Van Assche * @q: [in] request queue pointer 228dad77584SBart Van Assche * @bio: [in] bio to be split 229dad77584SBart Van Assche * @bs: [in] bio set to allocate the clone from 230dad77584SBart Van Assche * @segs: [out] number of segments in the bio with the first half of the sectors 231dad77584SBart Van Assche * 232dad77584SBart Van Assche * Clone @bio, update the bi_iter of the clone to represent the first sectors 233dad77584SBart Van Assche * of @bio and update @bio->bi_iter to represent the remaining sectors. The 234dad77584SBart Van Assche * following is guaranteed for the cloned bio: 235dad77584SBart Van Assche * - That it has at most get_max_io_size(@q, @bio) sectors. 236dad77584SBart Van Assche * - That it has at most queue_max_segments(@q) segments. 237dad77584SBart Van Assche * 238dad77584SBart Van Assche * Except for discard requests the cloned bio will point at the bi_io_vec of 239dad77584SBart Van Assche * the original bio. It is the responsibility of the caller to ensure that the 240dad77584SBart Van Assche * original bio is not freed before the cloned bio. The caller is also 241dad77584SBart Van Assche * responsible for ensuring that @bs is only destroyed after processing of the 242dad77584SBart Van Assche * split bio has finished. 243dad77584SBart Van Assche */ 24454efd50bSKent Overstreet static struct bio *blk_bio_segment_split(struct request_queue *q, 24554efd50bSKent Overstreet struct bio *bio, 246bdced438SMing Lei struct bio_set *bs, 247bdced438SMing Lei unsigned *segs) 24854efd50bSKent Overstreet { 2495014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL; 25054efd50bSKent Overstreet struct bvec_iter iter; 2516869875fSChristoph Hellwig unsigned nsegs = 0, sectors = 0; 252d0e5fbb0SMing Lei const unsigned max_sectors = get_max_io_size(q, bio); 25305b700baSMing Lei const unsigned max_segs = queue_max_segments(q); 25454efd50bSKent Overstreet 255dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) { 25654efd50bSKent Overstreet /* 25754efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this 25854efd50bSKent Overstreet * offset would create a gap, disallow it. 25954efd50bSKent Overstreet */ 2605014c311SJens Axboe if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) 26154efd50bSKent Overstreet goto split; 26254efd50bSKent Overstreet 26305b700baSMing Lei if (nsegs < max_segs && 264708b25b3SBart Van Assche sectors + (bv.bv_len >> 9) <= max_sectors && 265708b25b3SBart Van Assche bv.bv_offset + bv.bv_len <= PAGE_SIZE) { 266708b25b3SBart Van Assche nsegs++; 267708b25b3SBart Van Assche sectors += bv.bv_len >> 9; 268708b25b3SBart Van Assche } else if (bvec_split_segs(q, &bv, &nsegs, §ors, max_segs, 269708b25b3SBart Van Assche max_sectors)) { 270e36f6204SKeith Busch goto split; 271e36f6204SKeith Busch } 272e36f6204SKeith Busch 27354efd50bSKent Overstreet bvprv = bv; 274578270bfSMing Lei bvprvp = &bvprv; 27554efd50bSKent Overstreet } 27654efd50bSKent Overstreet 277d627065dSChristoph Hellwig *segs = nsegs; 278d627065dSChristoph Hellwig return NULL; 27954efd50bSKent Overstreet split: 280bdced438SMing Lei *segs = nsegs; 281d627065dSChristoph Hellwig return bio_split(bio, sectors, GFP_NOIO, bs); 28254efd50bSKent Overstreet } 28354efd50bSKent Overstreet 284dad77584SBart Van Assche /** 285dad77584SBart Van Assche * __blk_queue_split - split a bio and submit the second half 286dad77584SBart Van Assche * @bio: [in, out] bio to be split 287dad77584SBart Van Assche * @nr_segs: [out] number of segments in the first bio 288dad77584SBart Van Assche * 289dad77584SBart Van Assche * Split a bio into two bios, chain the two bios, submit the second half and 290dad77584SBart Van Assche * store a pointer to the first half in *@bio. If the second bio is still too 291dad77584SBart Van Assche * big it will be split by a recursive call to this function. Since this 292f695ca38SChristoph Hellwig * function may allocate a new bio from @bio->bi_disk->queue->bio_split, it is 293f695ca38SChristoph Hellwig * the responsibility of the caller to ensure that 294f695ca38SChristoph Hellwig * @bio->bi_disk->queue->bio_split is only released after processing of the 295dad77584SBart Van Assche * split bio has finished. 296dad77584SBart Van Assche */ 297f695ca38SChristoph Hellwig void __blk_queue_split(struct bio **bio, unsigned int *nr_segs) 29854efd50bSKent Overstreet { 299f695ca38SChristoph Hellwig struct request_queue *q = (*bio)->bi_disk->queue; 300fa532287SChristoph Hellwig struct bio *split = NULL; 30154efd50bSKent Overstreet 3027afafc8aSAdrian Hunter switch (bio_op(*bio)) { 3037afafc8aSAdrian Hunter case REQ_OP_DISCARD: 3047afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 30514ccb66bSChristoph Hellwig split = blk_bio_discard_split(q, *bio, &q->bio_split, nr_segs); 3067afafc8aSAdrian Hunter break; 307a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 30814ccb66bSChristoph Hellwig split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, 30914ccb66bSChristoph Hellwig nr_segs); 310a6f0788eSChaitanya Kulkarni break; 3117afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 31214ccb66bSChristoph Hellwig split = blk_bio_write_same_split(q, *bio, &q->bio_split, 31314ccb66bSChristoph Hellwig nr_segs); 3147afafc8aSAdrian Hunter break; 3157afafc8aSAdrian Hunter default: 316fa532287SChristoph Hellwig /* 317fa532287SChristoph Hellwig * All drivers must accept single-segments bios that are <= 318fa532287SChristoph Hellwig * PAGE_SIZE. This is a quick and dirty check that relies on 319fa532287SChristoph Hellwig * the fact that bi_io_vec[0] is always valid if a bio has data. 320fa532287SChristoph Hellwig * The check might lead to occasional false negatives when bios 321fa532287SChristoph Hellwig * are cloned, but compared to the performance impact of cloned 322fa532287SChristoph Hellwig * bios themselves the loop below doesn't matter anyway. 323fa532287SChristoph Hellwig */ 324fa532287SChristoph Hellwig if (!q->limits.chunk_sectors && 325fa532287SChristoph Hellwig (*bio)->bi_vcnt == 1 && 32659db8ba2SMing Lei ((*bio)->bi_io_vec[0].bv_len + 3271e279153SJens Axboe (*bio)->bi_io_vec[0].bv_offset) <= PAGE_SIZE) { 328fa532287SChristoph Hellwig *nr_segs = 1; 329fa532287SChristoph Hellwig break; 330fa532287SChristoph Hellwig } 33114ccb66bSChristoph Hellwig split = blk_bio_segment_split(q, *bio, &q->bio_split, nr_segs); 3327afafc8aSAdrian Hunter break; 3337afafc8aSAdrian Hunter } 334bdced438SMing Lei 33554efd50bSKent Overstreet if (split) { 3366ac45aebSMing Lei /* there isn't chance to merge the splitted bio */ 3371eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE; 3386ac45aebSMing Lei 33954efd50bSKent Overstreet bio_chain(split, *bio); 340cda22646SMike Krinkin trace_block_split(q, split, (*bio)->bi_iter.bi_sector); 341*ed00aabdSChristoph Hellwig submit_bio_noacct(*bio); 34254efd50bSKent Overstreet *bio = split; 34354efd50bSKent Overstreet } 34454efd50bSKent Overstreet } 34514ccb66bSChristoph Hellwig 346dad77584SBart Van Assche /** 347dad77584SBart Van Assche * blk_queue_split - split a bio and submit the second half 348dad77584SBart Van Assche * @bio: [in, out] bio to be split 349dad77584SBart Van Assche * 350dad77584SBart Van Assche * Split a bio into two bios, chains the two bios, submit the second half and 351dad77584SBart Van Assche * store a pointer to the first half in *@bio. Since this function may allocate 352f695ca38SChristoph Hellwig * a new bio from @bio->bi_disk->queue->bio_split, it is the responsibility of 353f695ca38SChristoph Hellwig * the caller to ensure that @bio->bi_disk->queue->bio_split is only released 354f695ca38SChristoph Hellwig * after processing of the split bio has finished. 355dad77584SBart Van Assche */ 356f695ca38SChristoph Hellwig void blk_queue_split(struct bio **bio) 35714ccb66bSChristoph Hellwig { 35814ccb66bSChristoph Hellwig unsigned int nr_segs; 35914ccb66bSChristoph Hellwig 360f695ca38SChristoph Hellwig __blk_queue_split(bio, &nr_segs); 36114ccb66bSChristoph Hellwig } 36254efd50bSKent Overstreet EXPORT_SYMBOL(blk_queue_split); 36354efd50bSKent Overstreet 364e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq) 365d6d48196SJens Axboe { 3666869875fSChristoph Hellwig unsigned int nr_phys_segs = 0; 367ff9811b3SBart Van Assche unsigned int nr_sectors = 0; 368e9cd19c0SChristoph Hellwig struct req_iterator iter; 3696869875fSChristoph Hellwig struct bio_vec bv; 370d6d48196SJens Axboe 371e9cd19c0SChristoph Hellwig if (!rq->bio) 3721e428079SJens Axboe return 0; 373d6d48196SJens Axboe 374e9cd19c0SChristoph Hellwig switch (bio_op(rq->bio)) { 375a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD: 376a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE: 377a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 378f9d03f96SChristoph Hellwig return 0; 379f9d03f96SChristoph Hellwig case REQ_OP_WRITE_SAME: 3805cb8850cSKent Overstreet return 1; 381a6f0788eSChaitanya Kulkarni } 3825cb8850cSKent Overstreet 383e9cd19c0SChristoph Hellwig rq_for_each_bvec(bv, rq, iter) 384ff9811b3SBart Van Assche bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors, 385708b25b3SBart Van Assche UINT_MAX, UINT_MAX); 3861e428079SJens Axboe return nr_phys_segs; 3871e428079SJens Axboe } 3881e428079SJens Axboe 38948d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, 390862e5a5eSMing Lei struct scatterlist *sglist) 391862e5a5eSMing Lei { 392862e5a5eSMing Lei if (!*sg) 393862e5a5eSMing Lei return sglist; 394862e5a5eSMing Lei 395862e5a5eSMing Lei /* 396862e5a5eSMing Lei * If the driver previously mapped a shorter list, we could see a 397862e5a5eSMing Lei * termination bit prematurely unless it fully inits the sg table 398862e5a5eSMing Lei * on each mapping. We KNOW that there must be more entries here 399862e5a5eSMing Lei * or the driver would be buggy, so force clear the termination bit 400862e5a5eSMing Lei * to avoid doing a full sg_init_table() in drivers for each command. 401862e5a5eSMing Lei */ 402862e5a5eSMing Lei sg_unmark_end(*sg); 403862e5a5eSMing Lei return sg_next(*sg); 404862e5a5eSMing Lei } 405862e5a5eSMing Lei 406862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q, 407862e5a5eSMing Lei struct bio_vec *bvec, struct scatterlist *sglist, 408862e5a5eSMing Lei struct scatterlist **sg) 409862e5a5eSMing Lei { 410862e5a5eSMing Lei unsigned nbytes = bvec->bv_len; 4118a96a0e4SChristoph Hellwig unsigned nsegs = 0, total = 0; 412862e5a5eSMing Lei 413862e5a5eSMing Lei while (nbytes > 0) { 4148a96a0e4SChristoph Hellwig unsigned offset = bvec->bv_offset + total; 415429120f3SMing Lei unsigned len = min(get_max_segment_size(q, bvec->bv_page, 416429120f3SMing Lei offset), nbytes); 417f9f76879SChristoph Hellwig struct page *page = bvec->bv_page; 418f9f76879SChristoph Hellwig 419f9f76879SChristoph Hellwig /* 420f9f76879SChristoph Hellwig * Unfortunately a fair number of drivers barf on scatterlists 421f9f76879SChristoph Hellwig * that have an offset larger than PAGE_SIZE, despite other 422f9f76879SChristoph Hellwig * subsystems dealing with that invariant just fine. For now 423f9f76879SChristoph Hellwig * stick to the legacy format where we never present those from 424f9f76879SChristoph Hellwig * the block layer, but the code below should be removed once 425f9f76879SChristoph Hellwig * these offenders (mostly MMC/SD drivers) are fixed. 426f9f76879SChristoph Hellwig */ 427f9f76879SChristoph Hellwig page += (offset >> PAGE_SHIFT); 428f9f76879SChristoph Hellwig offset &= ~PAGE_MASK; 429862e5a5eSMing Lei 430862e5a5eSMing Lei *sg = blk_next_sg(sg, sglist); 431f9f76879SChristoph Hellwig sg_set_page(*sg, page, len, offset); 432862e5a5eSMing Lei 4338a96a0e4SChristoph Hellwig total += len; 4348a96a0e4SChristoph Hellwig nbytes -= len; 435862e5a5eSMing Lei nsegs++; 436862e5a5eSMing Lei } 437862e5a5eSMing Lei 438862e5a5eSMing Lei return nsegs; 439862e5a5eSMing Lei } 440862e5a5eSMing Lei 44116e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv, 44216e3e418SMing Lei struct scatterlist *sglist, struct scatterlist **sg) 44316e3e418SMing Lei { 44416e3e418SMing Lei *sg = blk_next_sg(sg, sglist); 44516e3e418SMing Lei sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); 44616e3e418SMing Lei return 1; 44716e3e418SMing Lei } 44816e3e418SMing Lei 449f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */ 450f6970f83SMing Lei static inline bool 451f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec, 452f6970f83SMing Lei struct bio_vec *bvprv, struct scatterlist **sg) 453963ab9e5SAsias He { 454963ab9e5SAsias He 455963ab9e5SAsias He int nbytes = bvec->bv_len; 456963ab9e5SAsias He 457f6970f83SMing Lei if (!*sg) 458f6970f83SMing Lei return false; 459f6970f83SMing Lei 460b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q)) 461f6970f83SMing Lei return false; 462f6970f83SMing Lei 4633dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec)) 464f6970f83SMing Lei return false; 465963ab9e5SAsias He 466963ab9e5SAsias He (*sg)->length += nbytes; 467f6970f83SMing Lei 468f6970f83SMing Lei return true; 469963ab9e5SAsias He } 470963ab9e5SAsias He 4715cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, 4725cb8850cSKent Overstreet struct scatterlist *sglist, 4735cb8850cSKent Overstreet struct scatterlist **sg) 4745cb8850cSKent Overstreet { 475b21e11c5SMing Lei struct bio_vec uninitialized_var(bvec), bvprv = { NULL }; 4765cb8850cSKent Overstreet struct bvec_iter iter; 47738417468SChristoph Hellwig int nsegs = 0; 478f6970f83SMing Lei bool new_bio = false; 4795cb8850cSKent Overstreet 480f6970f83SMing Lei for_each_bio(bio) { 481f6970f83SMing Lei bio_for_each_bvec(bvec, bio, iter) { 482f6970f83SMing Lei /* 483f6970f83SMing Lei * Only try to merge bvecs from two bios given we 484f6970f83SMing Lei * have done bio internal merge when adding pages 485f6970f83SMing Lei * to bio 486f6970f83SMing Lei */ 487f6970f83SMing Lei if (new_bio && 488f6970f83SMing Lei __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg)) 489f6970f83SMing Lei goto next_bvec; 490f6970f83SMing Lei 491f6970f83SMing Lei if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE) 492f6970f83SMing Lei nsegs += __blk_bvec_map_sg(bvec, sglist, sg); 493f6970f83SMing Lei else 494f6970f83SMing Lei nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg); 495f6970f83SMing Lei next_bvec: 496f6970f83SMing Lei new_bio = false; 497f6970f83SMing Lei } 498b21e11c5SMing Lei if (likely(bio->bi_iter.bi_size)) { 499f6970f83SMing Lei bvprv = bvec; 500f6970f83SMing Lei new_bio = true; 501f6970f83SMing Lei } 502b21e11c5SMing Lei } 5035cb8850cSKent Overstreet 5045cb8850cSKent Overstreet return nsegs; 5055cb8850cSKent Overstreet } 5065cb8850cSKent Overstreet 507d6d48196SJens Axboe /* 508d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller 509d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries 510d6d48196SJens Axboe */ 51189de1504SChristoph Hellwig int __blk_rq_map_sg(struct request_queue *q, struct request *rq, 51289de1504SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **last_sg) 513d6d48196SJens Axboe { 5145cb8850cSKent Overstreet int nsegs = 0; 515d6d48196SJens Axboe 516f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) 51789de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg); 518f9d03f96SChristoph Hellwig else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) 51989de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, last_sg); 520f9d03f96SChristoph Hellwig else if (rq->bio) 52189de1504SChristoph Hellwig nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg); 522f18573abSFUJITA Tomonori 52389de1504SChristoph Hellwig if (*last_sg) 52489de1504SChristoph Hellwig sg_mark_end(*last_sg); 525d6d48196SJens Axboe 52612e57f59SMing Lei /* 52712e57f59SMing Lei * Something must have been wrong if the figured number of 52812e57f59SMing Lei * segment is bigger than number of req's physical segments 52912e57f59SMing Lei */ 530f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); 53112e57f59SMing Lei 532d6d48196SJens Axboe return nsegs; 533d6d48196SJens Axboe } 53489de1504SChristoph Hellwig EXPORT_SYMBOL(__blk_rq_map_sg); 535d6d48196SJens Axboe 53614ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio, 53714ccb66bSChristoph Hellwig unsigned int nr_phys_segs) 538d6d48196SJens Axboe { 53914ccb66bSChristoph Hellwig if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(req->q)) 54013f05c8dSMartin K. Petersen goto no_merge; 54113f05c8dSMartin K. Petersen 54214ccb66bSChristoph Hellwig if (blk_integrity_merge_bio(req->q, req, bio) == false) 54313f05c8dSMartin K. Petersen goto no_merge; 544d6d48196SJens Axboe 545d6d48196SJens Axboe /* 546d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both 547d6d48196SJens Axboe * counters. 548d6d48196SJens Axboe */ 549d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs; 550d6d48196SJens Axboe return 1; 55113f05c8dSMartin K. Petersen 55213f05c8dSMartin K. Petersen no_merge: 55314ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 55413f05c8dSMartin K. Petersen return 0; 555d6d48196SJens Axboe } 556d6d48196SJens Axboe 55714ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs) 558d6d48196SJens Axboe { 5595e7c4274SJens Axboe if (req_gap_back_merge(req, bio)) 5605e7c4274SJens Axboe return 0; 5617f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5627f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio)) 5637f39add3SSagi Grimberg return 0; 564a892c8d5SSatya Tangirala if (!bio_crypt_ctx_back_mergeable(req, bio)) 565a892c8d5SSatya Tangirala return 0; 566f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 56717007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) { 56814ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 569d6d48196SJens Axboe return 0; 570d6d48196SJens Axboe } 571d6d48196SJens Axboe 57214ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 573d6d48196SJens Axboe } 574d6d48196SJens Axboe 57514ccb66bSChristoph Hellwig int ll_front_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs) 576d6d48196SJens Axboe { 5775e7c4274SJens Axboe if (req_gap_front_merge(req, bio)) 5785e7c4274SJens Axboe return 0; 5797f39add3SSagi Grimberg if (blk_integrity_rq(req) && 5807f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio)) 5817f39add3SSagi Grimberg return 0; 582a892c8d5SSatya Tangirala if (!bio_crypt_ctx_front_mergeable(req, bio)) 583a892c8d5SSatya Tangirala return 0; 584f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) > 58517007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { 58614ccb66bSChristoph Hellwig req_set_nomerge(req->q, req); 587d6d48196SJens Axboe return 0; 588d6d48196SJens Axboe } 589d6d48196SJens Axboe 59014ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs); 591d6d48196SJens Axboe } 592d6d48196SJens Axboe 593445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, 594445251d0SJens Axboe struct request *next) 595445251d0SJens Axboe { 596445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req); 597445251d0SJens Axboe 598445251d0SJens Axboe if (segments >= queue_max_discard_segments(q)) 599445251d0SJens Axboe goto no_merge; 600445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) > 601445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req))) 602445251d0SJens Axboe goto no_merge; 603445251d0SJens Axboe 604445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); 605445251d0SJens Axboe return true; 606445251d0SJens Axboe no_merge: 607445251d0SJens Axboe req_set_nomerge(q, req); 608445251d0SJens Axboe return false; 609445251d0SJens Axboe } 610445251d0SJens Axboe 611d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req, 612d6d48196SJens Axboe struct request *next) 613d6d48196SJens Axboe { 614d6d48196SJens Axboe int total_phys_segments; 615d6d48196SJens Axboe 6165e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio)) 617854fbb9cSKeith Busch return 0; 618854fbb9cSKeith Busch 619d6d48196SJens Axboe /* 620d6d48196SJens Axboe * Will it become too large? 621d6d48196SJens Axboe */ 622f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > 62317007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) 624d6d48196SJens Axboe return 0; 625d6d48196SJens Axboe 626d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; 6278a78362cSMartin K. Petersen if (total_phys_segments > queue_max_segments(q)) 628d6d48196SJens Axboe return 0; 629d6d48196SJens Axboe 6304eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false) 63113f05c8dSMartin K. Petersen return 0; 63213f05c8dSMartin K. Petersen 633a892c8d5SSatya Tangirala if (!bio_crypt_ctx_merge_rq(req, next)) 634a892c8d5SSatya Tangirala return 0; 635a892c8d5SSatya Tangirala 636d6d48196SJens Axboe /* Merge is OK... */ 637d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments; 638d6d48196SJens Axboe return 1; 639d6d48196SJens Axboe } 640d6d48196SJens Axboe 64180a761fdSTejun Heo /** 64280a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge 64380a761fdSTejun Heo * @rq: request to mark as mixed merge 64480a761fdSTejun Heo * 64580a761fdSTejun Heo * Description: 64680a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes 64780a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed 64880a761fdSTejun Heo * merged. 64980a761fdSTejun Heo */ 65080a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq) 65180a761fdSTejun Heo { 65280a761fdSTejun Heo unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; 65380a761fdSTejun Heo struct bio *bio; 65480a761fdSTejun Heo 655e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE) 65680a761fdSTejun Heo return; 65780a761fdSTejun Heo 65880a761fdSTejun Heo /* 65980a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the 66080a761fdSTejun Heo * contained bios. It will just track those of the first one. 66180a761fdSTejun Heo * Distributes the attributs to each bio. 66280a761fdSTejun Heo */ 66380a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) { 6641eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && 6651eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff); 6661eff9d32SJens Axboe bio->bi_opf |= ff; 66780a761fdSTejun Heo } 668e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE; 66980a761fdSTejun Heo } 67080a761fdSTejun Heo 671b9c54f56SKonstantin Khlebnikov static void blk_account_io_merge_request(struct request *req) 67226308eabSJerome Marchand { 67326308eabSJerome Marchand if (blk_do_io_stat(req)) { 674112f158fSMike Snitzer part_stat_lock(); 675b9c54f56SKonstantin Khlebnikov part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); 67626308eabSJerome Marchand part_stat_unlock(); 677524f9ffdSChristoph Hellwig 678524f9ffdSChristoph Hellwig hd_struct_put(req->part); 67926308eabSJerome Marchand } 68026308eabSJerome Marchand } 681b9c54f56SKonstantin Khlebnikov 68269840466SJianchao Wang /* 68369840466SJianchao Wang * Two cases of handling DISCARD merge: 68469840466SJianchao Wang * If max_discard_segments > 1, the driver takes every bio 68569840466SJianchao Wang * as a range and send them to controller together. The ranges 68669840466SJianchao Wang * needn't to be contiguous. 68769840466SJianchao Wang * Otherwise, the bios/requests will be handled as same as 68869840466SJianchao Wang * others which should be contiguous. 68969840466SJianchao Wang */ 69069840466SJianchao Wang static inline bool blk_discard_mergable(struct request *req) 69169840466SJianchao Wang { 69269840466SJianchao Wang if (req_op(req) == REQ_OP_DISCARD && 69369840466SJianchao Wang queue_max_discard_segments(req->q) > 1) 69469840466SJianchao Wang return true; 69569840466SJianchao Wang return false; 69669840466SJianchao Wang } 69769840466SJianchao Wang 698e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req, 699e96c0d83SEric Biggers struct request *next) 70069840466SJianchao Wang { 70169840466SJianchao Wang if (blk_discard_mergable(req)) 70269840466SJianchao Wang return ELEVATOR_DISCARD_MERGE; 70369840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) 70469840466SJianchao Wang return ELEVATOR_BACK_MERGE; 70569840466SJianchao Wang 70669840466SJianchao Wang return ELEVATOR_NO_MERGE; 70769840466SJianchao Wang } 70826308eabSJerome Marchand 709d6d48196SJens Axboe /* 710b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired. 711b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held. 712d6d48196SJens Axboe */ 713b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q, 714b973cb7eSJens Axboe struct request *req, struct request *next) 715d6d48196SJens Axboe { 716d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next)) 717b973cb7eSJens Axboe return NULL; 718d6d48196SJens Axboe 719288dab8aSChristoph Hellwig if (req_op(req) != req_op(next)) 720b973cb7eSJens Axboe return NULL; 721f31dc1cdSMartin K. Petersen 722d6d48196SJens Axboe if (rq_data_dir(req) != rq_data_dir(next) 7232081a56bSJens Axboe || req->rq_disk != next->rq_disk) 724b973cb7eSJens Axboe return NULL; 725d6d48196SJens Axboe 7268fe0d473SMike Christie if (req_op(req) == REQ_OP_WRITE_SAME && 7274363ac7cSMartin K. Petersen !blk_write_same_mergeable(req->bio, next->bio)) 728b973cb7eSJens Axboe return NULL; 7294363ac7cSMartin K. Petersen 730d6d48196SJens Axboe /* 731cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 732cb6934f8SJens Axboe * non-hint IO. 733cb6934f8SJens Axboe */ 734cb6934f8SJens Axboe if (req->write_hint != next->write_hint) 735cb6934f8SJens Axboe return NULL; 736cb6934f8SJens Axboe 737668ffc03SDamien Le Moal if (req->ioprio != next->ioprio) 738668ffc03SDamien Le Moal return NULL; 739668ffc03SDamien Le Moal 740cb6934f8SJens Axboe /* 741d6d48196SJens Axboe * If we are allowed to merge, then append bio list 742d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn 743d6d48196SJens Axboe * will have updated segment counts, update sector 744445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they 745445251d0SJens Axboe * have separate settings. 746d6d48196SJens Axboe */ 74769840466SJianchao Wang 74869840466SJianchao Wang switch (blk_try_req_merge(req, next)) { 74969840466SJianchao Wang case ELEVATOR_DISCARD_MERGE: 750445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next)) 751445251d0SJens Axboe return NULL; 75269840466SJianchao Wang break; 75369840466SJianchao Wang case ELEVATOR_BACK_MERGE: 75469840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next)) 755b973cb7eSJens Axboe return NULL; 75669840466SJianchao Wang break; 75769840466SJianchao Wang default: 75869840466SJianchao Wang return NULL; 75969840466SJianchao Wang } 760d6d48196SJens Axboe 761d6d48196SJens Axboe /* 76280a761fdSTejun Heo * If failfast settings disagree or any of the two is already 76380a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This 76480a761fdSTejun Heo * makes sure that all involved bios have mixable attributes 76580a761fdSTejun Heo * set properly. 76680a761fdSTejun Heo */ 767e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || 76880a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) != 76980a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) { 77080a761fdSTejun Heo blk_rq_set_mixed_merge(req); 77180a761fdSTejun Heo blk_rq_set_mixed_merge(next); 77280a761fdSTejun Heo } 77380a761fdSTejun Heo 77480a761fdSTejun Heo /* 775522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We 776522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the 777522a7775SOmar Sandoval * current request for accounting purposes. 778d6d48196SJens Axboe */ 779522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns) 780522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns; 781d6d48196SJens Axboe 782d6d48196SJens Axboe req->biotail->bi_next = next->bio; 783d6d48196SJens Axboe req->biotail = next->biotail; 784d6d48196SJens Axboe 785a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next); 786d6d48196SJens Axboe 7872a5cf35cSMing Lei if (!blk_discard_mergable(req)) 788d6d48196SJens Axboe elv_merge_requests(q, req, next); 789d6d48196SJens Axboe 79042dad764SJerome Marchand /* 79142dad764SJerome Marchand * 'next' is going away, so update stats accordingly 79242dad764SJerome Marchand */ 793b9c54f56SKonstantin Khlebnikov blk_account_io_merge_request(next); 794d6d48196SJens Axboe 795f3bdc62fSJan Kara trace_block_rq_merge(q, next); 796f3bdc62fSJan Kara 797e4d750c9SJens Axboe /* 798e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for 799e4d750c9SJens Axboe * the caller to free 800e4d750c9SJens Axboe */ 8011cd96c24SBoaz Harrosh next->bio = NULL; 802b973cb7eSJens Axboe return next; 803d6d48196SJens Axboe } 804d6d48196SJens Axboe 805b973cb7eSJens Axboe struct request *attempt_back_merge(struct request_queue *q, struct request *rq) 806d6d48196SJens Axboe { 807d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq); 808d6d48196SJens Axboe 809d6d48196SJens Axboe if (next) 810d6d48196SJens Axboe return attempt_merge(q, rq, next); 811d6d48196SJens Axboe 812b973cb7eSJens Axboe return NULL; 813d6d48196SJens Axboe } 814d6d48196SJens Axboe 815b973cb7eSJens Axboe struct request *attempt_front_merge(struct request_queue *q, struct request *rq) 816d6d48196SJens Axboe { 817d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq); 818d6d48196SJens Axboe 819d6d48196SJens Axboe if (prev) 820d6d48196SJens Axboe return attempt_merge(q, prev, rq); 821d6d48196SJens Axboe 822b973cb7eSJens Axboe return NULL; 823d6d48196SJens Axboe } 8245e84ea3aSJens Axboe 8255e84ea3aSJens Axboe int blk_attempt_req_merge(struct request_queue *q, struct request *rq, 8265e84ea3aSJens Axboe struct request *next) 8275e84ea3aSJens Axboe { 828e4d750c9SJens Axboe struct request *free; 82972ef799bSTahsin Erdogan 830e4d750c9SJens Axboe free = attempt_merge(q, rq, next); 831e4d750c9SJens Axboe if (free) { 83292bc5a24SJens Axboe blk_put_request(free); 833e4d750c9SJens Axboe return 1; 834e4d750c9SJens Axboe } 835e4d750c9SJens Axboe 836e4d750c9SJens Axboe return 0; 8375e84ea3aSJens Axboe } 838050c8ea8STejun Heo 839050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio) 840050c8ea8STejun Heo { 841e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio)) 842050c8ea8STejun Heo return false; 843050c8ea8STejun Heo 844288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio)) 845f31dc1cdSMartin K. Petersen return false; 846f31dc1cdSMartin K. Petersen 847050c8ea8STejun Heo /* different data direction or already started, don't merge */ 848050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq)) 849050c8ea8STejun Heo return false; 850050c8ea8STejun Heo 8512081a56bSJens Axboe /* must be same device */ 8522081a56bSJens Axboe if (rq->rq_disk != bio->bi_disk) 853050c8ea8STejun Heo return false; 854050c8ea8STejun Heo 855050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */ 8564eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false) 857050c8ea8STejun Heo return false; 858050c8ea8STejun Heo 859a892c8d5SSatya Tangirala /* Only merge if the crypt contexts are compatible */ 860a892c8d5SSatya Tangirala if (!bio_crypt_rq_ctx_compatible(rq, bio)) 861a892c8d5SSatya Tangirala return false; 862a892c8d5SSatya Tangirala 8634363ac7cSMartin K. Petersen /* must be using the same buffer */ 8648fe0d473SMike Christie if (req_op(rq) == REQ_OP_WRITE_SAME && 8654363ac7cSMartin K. Petersen !blk_write_same_mergeable(rq->bio, bio)) 8664363ac7cSMartin K. Petersen return false; 8674363ac7cSMartin K. Petersen 868cb6934f8SJens Axboe /* 869cb6934f8SJens Axboe * Don't allow merge of different write hints, or for a hint with 870cb6934f8SJens Axboe * non-hint IO. 871cb6934f8SJens Axboe */ 872cb6934f8SJens Axboe if (rq->write_hint != bio->bi_write_hint) 873cb6934f8SJens Axboe return false; 874cb6934f8SJens Axboe 875668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio)) 876668ffc03SDamien Le Moal return false; 877668ffc03SDamien Le Moal 878050c8ea8STejun Heo return true; 879050c8ea8STejun Heo } 880050c8ea8STejun Heo 88134fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) 882050c8ea8STejun Heo { 88369840466SJianchao Wang if (blk_discard_mergable(rq)) 8841e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE; 8851e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) 886050c8ea8STejun Heo return ELEVATOR_BACK_MERGE; 8874f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) 888050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE; 889050c8ea8STejun Heo return ELEVATOR_NO_MERGE; 890050c8ea8STejun Heo } 891