1 // SPDX-License-Identifier: GPL-2.0
3 * Functions related to segment and merge handling
5 #include <linux/kernel.h>
6 #include <linux/module.h>
8 #include <linux/blkdev.h>
9 #include <linux/blk-integrity.h>
10 #include <linux/scatterlist.h>
11 #include <linux/part_stat.h>
13 #include <trace/events/block.h>
16 #include "blk-mq-sched.h"
17 #include "blk-rq-qos.h"
18 #include "blk-throttle.h"
20 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
22 *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
25 static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
27 struct bvec_iter iter = bio->bi_iter;
30 bio_get_first_bvec(bio, bv);
31 if (bv->bv_len == bio->bi_iter.bi_size)
32 return; /* this bio only has a single bvec */
34 bio_advance_iter(bio, &iter, iter.bi_size);
36 if (!iter.bi_bvec_done)
37 idx = iter.bi_idx - 1;
38 else /* in the middle of bvec */
41 *bv = bio->bi_io_vec[idx];
44 * iter.bi_bvec_done records actual length of the last bvec
45 * if this bio ends in the middle of one io vector
47 if (iter.bi_bvec_done)
48 bv->bv_len = iter.bi_bvec_done;
51 static inline bool bio_will_gap(struct request_queue *q,
52 struct request *prev_rq, struct bio *prev, struct bio *next)
54 struct bio_vec pb, nb;
56 if (!bio_has_data(prev) || !queue_virt_boundary(q))
60 * Don't merge if the 1st bio starts with non-zero offset, otherwise it
61 * is quite difficult to respect the sg gap limit. We work hard to
62 * merge a huge number of small single bios in case of mkfs.
65 bio_get_first_bvec(prev_rq->bio, &pb);
67 bio_get_first_bvec(prev, &pb);
68 if (pb.bv_offset & queue_virt_boundary(q))
72 * We don't need to worry about the situation that the merged segment
73 * ends in unaligned virt boundary:
75 * - if 'pb' ends aligned, the merged segment ends aligned
76 * - if 'pb' ends unaligned, the next bio must include
77 * one single bvec of 'nb', otherwise the 'nb' can't
80 bio_get_last_bvec(prev, &pb);
81 bio_get_first_bvec(next, &nb);
82 if (biovec_phys_mergeable(q, &pb, &nb))
84 return __bvec_gap_to_prev(q, &pb, nb.bv_offset);
87 static inline bool req_gap_back_merge(struct request *req, struct bio *bio)
89 return bio_will_gap(req->q, req, req->biotail, bio);
92 static inline bool req_gap_front_merge(struct request *req, struct bio *bio)
94 return bio_will_gap(req->q, NULL, bio, req->bio);
97 static struct bio *blk_bio_discard_split(struct request_queue *q,
102 unsigned int max_discard_sectors, granularity;
105 unsigned split_sectors;
109 /* Zero-sector (unknown) and one-sector granularities are the same. */
110 granularity = max(q->limits.discard_granularity >> 9, 1U);
112 max_discard_sectors = min(q->limits.max_discard_sectors,
113 bio_allowed_max_sectors(q));
114 max_discard_sectors -= max_discard_sectors % granularity;
116 if (unlikely(!max_discard_sectors)) {
121 if (bio_sectors(bio) <= max_discard_sectors)
124 split_sectors = max_discard_sectors;
127 * If the next starting sector would be misaligned, stop the discard at
128 * the previous aligned sector.
130 alignment = (q->limits.discard_alignment >> 9) % granularity;
132 tmp = bio->bi_iter.bi_sector + split_sectors - alignment;
133 tmp = sector_div(tmp, granularity);
135 if (split_sectors > tmp)
136 split_sectors -= tmp;
138 return bio_split(bio, split_sectors, GFP_NOIO, bs);
141 static struct bio *blk_bio_write_zeroes_split(struct request_queue *q,
142 struct bio *bio, struct bio_set *bs, unsigned *nsegs)
146 if (!q->limits.max_write_zeroes_sectors)
149 if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors)
152 return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs);
155 static struct bio *blk_bio_write_same_split(struct request_queue *q,
162 if (!q->limits.max_write_same_sectors)
165 if (bio_sectors(bio) <= q->limits.max_write_same_sectors)
168 return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs);
172 * Return the maximum number of sectors from the start of a bio that may be
173 * submitted as a single request to a block device. If enough sectors remain,
174 * align the end to the physical block size. Otherwise align the end to the
175 * logical block size. This approach minimizes the number of non-aligned
176 * requests that are submitted to a block device if the start of a bio is not
177 * aligned to a physical block boundary.
179 static inline unsigned get_max_io_size(struct request_queue *q,
182 unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector, 0);
183 unsigned max_sectors = sectors;
184 unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT;
185 unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT;
186 unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1);
188 max_sectors += start_offset;
189 max_sectors &= ~(pbs - 1);
190 if (max_sectors > start_offset)
191 return max_sectors - start_offset;
193 return sectors & ~(lbs - 1);
196 static inline unsigned get_max_segment_size(const struct request_queue *q,
197 struct page *start_page,
198 unsigned long offset)
200 unsigned long mask = queue_segment_boundary(q);
202 offset = mask & (page_to_phys(start_page) + offset);
205 * overflow may be triggered in case of zero page physical address
206 * on 32bit arch, use queue's max segment size when that happens.
208 return min_not_zero(mask - offset + 1,
209 (unsigned long)queue_max_segment_size(q));
213 * bvec_split_segs - verify whether or not a bvec should be split in the middle
214 * @q: [in] request queue associated with the bio associated with @bv
215 * @bv: [in] bvec to examine
216 * @nsegs: [in,out] Number of segments in the bio being built. Incremented
217 * by the number of segments from @bv that may be appended to that
218 * bio without exceeding @max_segs
219 * @sectors: [in,out] Number of sectors in the bio being built. Incremented
220 * by the number of sectors from @bv that may be appended to that
221 * bio without exceeding @max_sectors
222 * @max_segs: [in] upper bound for *@nsegs
223 * @max_sectors: [in] upper bound for *@sectors
225 * When splitting a bio, it can happen that a bvec is encountered that is too
226 * big to fit in a single segment and hence that it has to be split in the
227 * middle. This function verifies whether or not that should happen. The value
228 * %true is returned if and only if appending the entire @bv to a bio with
229 * *@nsegs segments and *@sectors sectors would make that bio unacceptable for
232 static bool bvec_split_segs(const struct request_queue *q,
233 const struct bio_vec *bv, unsigned *nsegs,
234 unsigned *sectors, unsigned max_segs,
235 unsigned max_sectors)
237 unsigned max_len = (min(max_sectors, UINT_MAX >> 9) - *sectors) << 9;
238 unsigned len = min(bv->bv_len, max_len);
239 unsigned total_len = 0;
240 unsigned seg_size = 0;
242 while (len && *nsegs < max_segs) {
243 seg_size = get_max_segment_size(q, bv->bv_page,
244 bv->bv_offset + total_len);
245 seg_size = min(seg_size, len);
248 total_len += seg_size;
251 if ((bv->bv_offset + total_len) & queue_virt_boundary(q))
255 *sectors += total_len >> 9;
257 /* tell the caller to split the bvec if it is too big to fit */
258 return len > 0 || bv->bv_len > max_len;
262 * blk_bio_segment_split - split a bio in two bios
263 * @q: [in] request queue pointer
264 * @bio: [in] bio to be split
265 * @bs: [in] bio set to allocate the clone from
266 * @segs: [out] number of segments in the bio with the first half of the sectors
268 * Clone @bio, update the bi_iter of the clone to represent the first sectors
269 * of @bio and update @bio->bi_iter to represent the remaining sectors. The
270 * following is guaranteed for the cloned bio:
271 * - That it has at most get_max_io_size(@q, @bio) sectors.
272 * - That it has at most queue_max_segments(@q) segments.
274 * Except for discard requests the cloned bio will point at the bi_io_vec of
275 * the original bio. It is the responsibility of the caller to ensure that the
276 * original bio is not freed before the cloned bio. The caller is also
277 * responsible for ensuring that @bs is only destroyed after processing of the
278 * split bio has finished.
280 static struct bio *blk_bio_segment_split(struct request_queue *q,
285 struct bio_vec bv, bvprv, *bvprvp = NULL;
286 struct bvec_iter iter;
287 unsigned nsegs = 0, sectors = 0;
288 const unsigned max_sectors = get_max_io_size(q, bio);
289 const unsigned max_segs = queue_max_segments(q);
291 bio_for_each_bvec(bv, bio, iter) {
293 * If the queue doesn't support SG gaps and adding this
294 * offset would create a gap, disallow it.
296 if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
299 if (nsegs < max_segs &&
300 sectors + (bv.bv_len >> 9) <= max_sectors &&
301 bv.bv_offset + bv.bv_len <= PAGE_SIZE) {
303 sectors += bv.bv_len >> 9;
304 } else if (bvec_split_segs(q, &bv, &nsegs, §ors, max_segs,
319 * Bio splitting may cause subtle trouble such as hang when doing sync
320 * iopoll in direct IO routine. Given performance gain of iopoll for
321 * big IO can be trival, disable iopoll when split needed.
323 bio_clear_polled(bio);
324 return bio_split(bio, sectors, GFP_NOIO, bs);
328 * __blk_queue_split - split a bio and submit the second half
329 * @q: [in] request_queue new bio is being queued at
330 * @bio: [in, out] bio to be split
331 * @nr_segs: [out] number of segments in the first bio
333 * Split a bio into two bios, chain the two bios, submit the second half and
334 * store a pointer to the first half in *@bio. If the second bio is still too
335 * big it will be split by a recursive call to this function. Since this
336 * function may allocate a new bio from q->bio_split, it is the responsibility
337 * of the caller to ensure that q->bio_split is only released after processing
338 * of the split bio has finished.
340 void __blk_queue_split(struct request_queue *q, struct bio **bio,
341 unsigned int *nr_segs)
343 struct bio *split = NULL;
345 switch (bio_op(*bio)) {
347 case REQ_OP_SECURE_ERASE:
348 split = blk_bio_discard_split(q, *bio, &q->bio_split, nr_segs);
350 case REQ_OP_WRITE_ZEROES:
351 split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split,
354 case REQ_OP_WRITE_SAME:
355 split = blk_bio_write_same_split(q, *bio, &q->bio_split,
359 split = blk_bio_segment_split(q, *bio, &q->bio_split, nr_segs);
364 /* there isn't chance to merge the splitted bio */
365 split->bi_opf |= REQ_NOMERGE;
367 bio_chain(split, *bio);
368 trace_block_split(split, (*bio)->bi_iter.bi_sector);
369 submit_bio_noacct(*bio);
375 * blk_queue_split - split a bio and submit the second half
376 * @bio: [in, out] bio to be split
378 * Split a bio into two bios, chains the two bios, submit the second half and
379 * store a pointer to the first half in *@bio. Since this function may allocate
380 * a new bio from q->bio_split, it is the responsibility of the caller to ensure
381 * that q->bio_split is only released after processing of the split bio has
384 void blk_queue_split(struct bio **bio)
386 struct request_queue *q = bdev_get_queue((*bio)->bi_bdev);
387 unsigned int nr_segs;
389 if (blk_may_split(q, *bio))
390 __blk_queue_split(q, bio, &nr_segs);
392 EXPORT_SYMBOL(blk_queue_split);
394 unsigned int blk_recalc_rq_segments(struct request *rq)
396 unsigned int nr_phys_segs = 0;
397 unsigned int nr_sectors = 0;
398 struct req_iterator iter;
404 switch (bio_op(rq->bio)) {
406 case REQ_OP_SECURE_ERASE:
407 if (queue_max_discard_segments(rq->q) > 1) {
408 struct bio *bio = rq->bio;
415 case REQ_OP_WRITE_ZEROES:
417 case REQ_OP_WRITE_SAME:
421 rq_for_each_bvec(bv, rq, iter)
422 bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors,
427 static inline struct scatterlist *blk_next_sg(struct scatterlist **sg,
428 struct scatterlist *sglist)
434 * If the driver previously mapped a shorter list, we could see a
435 * termination bit prematurely unless it fully inits the sg table
436 * on each mapping. We KNOW that there must be more entries here
437 * or the driver would be buggy, so force clear the termination bit
438 * to avoid doing a full sg_init_table() in drivers for each command.
444 static unsigned blk_bvec_map_sg(struct request_queue *q,
445 struct bio_vec *bvec, struct scatterlist *sglist,
446 struct scatterlist **sg)
448 unsigned nbytes = bvec->bv_len;
449 unsigned nsegs = 0, total = 0;
452 unsigned offset = bvec->bv_offset + total;
453 unsigned len = min(get_max_segment_size(q, bvec->bv_page,
455 struct page *page = bvec->bv_page;
458 * Unfortunately a fair number of drivers barf on scatterlists
459 * that have an offset larger than PAGE_SIZE, despite other
460 * subsystems dealing with that invariant just fine. For now
461 * stick to the legacy format where we never present those from
462 * the block layer, but the code below should be removed once
463 * these offenders (mostly MMC/SD drivers) are fixed.
465 page += (offset >> PAGE_SHIFT);
466 offset &= ~PAGE_MASK;
468 *sg = blk_next_sg(sg, sglist);
469 sg_set_page(*sg, page, len, offset);
479 static inline int __blk_bvec_map_sg(struct bio_vec bv,
480 struct scatterlist *sglist, struct scatterlist **sg)
482 *sg = blk_next_sg(sg, sglist);
483 sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
487 /* only try to merge bvecs into one sg if they are from two bios */
489 __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec,
490 struct bio_vec *bvprv, struct scatterlist **sg)
493 int nbytes = bvec->bv_len;
498 if ((*sg)->length + nbytes > queue_max_segment_size(q))
501 if (!biovec_phys_mergeable(q, bvprv, bvec))
504 (*sg)->length += nbytes;
509 static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
510 struct scatterlist *sglist,
511 struct scatterlist **sg)
513 struct bio_vec bvec, bvprv = { NULL };
514 struct bvec_iter iter;
516 bool new_bio = false;
519 bio_for_each_bvec(bvec, bio, iter) {
521 * Only try to merge bvecs from two bios given we
522 * have done bio internal merge when adding pages
526 __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg))
529 if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE)
530 nsegs += __blk_bvec_map_sg(bvec, sglist, sg);
532 nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg);
536 if (likely(bio->bi_iter.bi_size)) {
546 * map a request to scatterlist, return number of sg entries setup. Caller
547 * must make sure sg can hold rq->nr_phys_segments entries
549 int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
550 struct scatterlist *sglist, struct scatterlist **last_sg)
554 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
555 nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg);
556 else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME)
557 nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, last_sg);
559 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg);
562 sg_mark_end(*last_sg);
565 * Something must have been wrong if the figured number of
566 * segment is bigger than number of req's physical segments
568 WARN_ON(nsegs > blk_rq_nr_phys_segments(rq));
572 EXPORT_SYMBOL(__blk_rq_map_sg);
574 static inline unsigned int blk_rq_get_max_segments(struct request *rq)
576 if (req_op(rq) == REQ_OP_DISCARD)
577 return queue_max_discard_segments(rq->q);
578 return queue_max_segments(rq->q);
581 static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
584 struct request_queue *q = rq->q;
586 if (blk_rq_is_passthrough(rq))
587 return q->limits.max_hw_sectors;
589 if (!q->limits.chunk_sectors ||
590 req_op(rq) == REQ_OP_DISCARD ||
591 req_op(rq) == REQ_OP_SECURE_ERASE)
592 return blk_queue_get_max_sectors(q, req_op(rq));
594 return min(blk_max_size_offset(q, offset, 0),
595 blk_queue_get_max_sectors(q, req_op(rq)));
598 static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
599 unsigned int nr_phys_segs)
601 if (blk_integrity_merge_bio(req->q, req, bio) == false)
604 /* discard request merge won't add new segment */
605 if (req_op(req) == REQ_OP_DISCARD)
608 if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req))
612 * This will form the start of a new hw segment. Bump both
615 req->nr_phys_segments += nr_phys_segs;
619 req_set_nomerge(req->q, req);
623 int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs)
625 if (req_gap_back_merge(req, bio))
627 if (blk_integrity_rq(req) &&
628 integrity_req_gap_back_merge(req, bio))
630 if (!bio_crypt_ctx_back_mergeable(req, bio))
632 if (blk_rq_sectors(req) + bio_sectors(bio) >
633 blk_rq_get_max_sectors(req, blk_rq_pos(req))) {
634 req_set_nomerge(req->q, req);
638 return ll_new_hw_segment(req, bio, nr_segs);
641 static int ll_front_merge_fn(struct request *req, struct bio *bio,
642 unsigned int nr_segs)
644 if (req_gap_front_merge(req, bio))
646 if (blk_integrity_rq(req) &&
647 integrity_req_gap_front_merge(req, bio))
649 if (!bio_crypt_ctx_front_mergeable(req, bio))
651 if (blk_rq_sectors(req) + bio_sectors(bio) >
652 blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) {
653 req_set_nomerge(req->q, req);
657 return ll_new_hw_segment(req, bio, nr_segs);
660 static bool req_attempt_discard_merge(struct request_queue *q, struct request *req,
661 struct request *next)
663 unsigned short segments = blk_rq_nr_discard_segments(req);
665 if (segments >= queue_max_discard_segments(q))
667 if (blk_rq_sectors(req) + bio_sectors(next->bio) >
668 blk_rq_get_max_sectors(req, blk_rq_pos(req)))
671 req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next);
674 req_set_nomerge(q, req);
678 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
679 struct request *next)
681 int total_phys_segments;
683 if (req_gap_back_merge(req, next->bio))
687 * Will it become too large?
689 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
690 blk_rq_get_max_sectors(req, blk_rq_pos(req)))
693 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
694 if (total_phys_segments > blk_rq_get_max_segments(req))
697 if (blk_integrity_merge_rq(q, req, next) == false)
700 if (!bio_crypt_ctx_merge_rq(req, next))
704 req->nr_phys_segments = total_phys_segments;
709 * blk_rq_set_mixed_merge - mark a request as mixed merge
710 * @rq: request to mark as mixed merge
713 * @rq is about to be mixed merged. Make sure the attributes
714 * which can be mixed are set in each bio and mark @rq as mixed
717 void blk_rq_set_mixed_merge(struct request *rq)
719 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
722 if (rq->rq_flags & RQF_MIXED_MERGE)
726 * @rq will no longer represent mixable attributes for all the
727 * contained bios. It will just track those of the first one.
728 * Distributes the attributs to each bio.
730 for (bio = rq->bio; bio; bio = bio->bi_next) {
731 WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) &&
732 (bio->bi_opf & REQ_FAILFAST_MASK) != ff);
735 rq->rq_flags |= RQF_MIXED_MERGE;
738 static void blk_account_io_merge_request(struct request *req)
740 if (blk_do_io_stat(req)) {
742 part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
747 static enum elv_merge blk_try_req_merge(struct request *req,
748 struct request *next)
750 if (blk_discard_mergable(req))
751 return ELEVATOR_DISCARD_MERGE;
752 else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
753 return ELEVATOR_BACK_MERGE;
755 return ELEVATOR_NO_MERGE;
758 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
760 if (bio_page(a) == bio_page(b) && bio_offset(a) == bio_offset(b))
766 * For non-mq, this has to be called with the request spinlock acquired.
767 * For mq with scheduling, the appropriate queue wide lock should be held.
769 static struct request *attempt_merge(struct request_queue *q,
770 struct request *req, struct request *next)
772 if (!rq_mergeable(req) || !rq_mergeable(next))
775 if (req_op(req) != req_op(next))
778 if (rq_data_dir(req) != rq_data_dir(next))
781 if (req_op(req) == REQ_OP_WRITE_SAME &&
782 !blk_write_same_mergeable(req->bio, next->bio))
786 * Don't allow merge of different write hints, or for a hint with
789 if (req->write_hint != next->write_hint)
792 if (req->ioprio != next->ioprio)
796 * If we are allowed to merge, then append bio list
797 * from next to rq and release next. merge_requests_fn
798 * will have updated segment counts, update sector
799 * counts here. Handle DISCARDs separately, as they
800 * have separate settings.
803 switch (blk_try_req_merge(req, next)) {
804 case ELEVATOR_DISCARD_MERGE:
805 if (!req_attempt_discard_merge(q, req, next))
808 case ELEVATOR_BACK_MERGE:
809 if (!ll_merge_requests_fn(q, req, next))
817 * If failfast settings disagree or any of the two is already
818 * a mixed merge, mark both as mixed before proceeding. This
819 * makes sure that all involved bios have mixable attributes
822 if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) ||
823 (req->cmd_flags & REQ_FAILFAST_MASK) !=
824 (next->cmd_flags & REQ_FAILFAST_MASK)) {
825 blk_rq_set_mixed_merge(req);
826 blk_rq_set_mixed_merge(next);
830 * At this point we have either done a back merge or front merge. We
831 * need the smaller start_time_ns of the merged requests to be the
832 * current request for accounting purposes.
834 if (next->start_time_ns < req->start_time_ns)
835 req->start_time_ns = next->start_time_ns;
837 req->biotail->bi_next = next->bio;
838 req->biotail = next->biotail;
840 req->__data_len += blk_rq_bytes(next);
842 if (!blk_discard_mergable(req))
843 elv_merge_requests(q, req, next);
846 * 'next' is going away, so update stats accordingly
848 blk_account_io_merge_request(next);
850 trace_block_rq_merge(next);
853 * ownership of bio passed from next to req, return 'next' for
860 static struct request *attempt_back_merge(struct request_queue *q,
863 struct request *next = elv_latter_request(q, rq);
866 return attempt_merge(q, rq, next);
871 static struct request *attempt_front_merge(struct request_queue *q,
874 struct request *prev = elv_former_request(q, rq);
877 return attempt_merge(q, prev, rq);
883 * Try to merge 'next' into 'rq'. Return true if the merge happened, false
884 * otherwise. The caller is responsible for freeing 'next' if the merge
887 bool blk_attempt_req_merge(struct request_queue *q, struct request *rq,
888 struct request *next)
890 return attempt_merge(q, rq, next);
893 bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
895 if (!rq_mergeable(rq) || !bio_mergeable(bio))
898 if (req_op(rq) != bio_op(bio))
901 /* different data direction or already started, don't merge */
902 if (bio_data_dir(bio) != rq_data_dir(rq))
905 /* only merge integrity protected bio into ditto rq */
906 if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
909 /* Only merge if the crypt contexts are compatible */
910 if (!bio_crypt_rq_ctx_compatible(rq, bio))
913 /* must be using the same buffer */
914 if (req_op(rq) == REQ_OP_WRITE_SAME &&
915 !blk_write_same_mergeable(rq->bio, bio))
919 * Don't allow merge of different write hints, or for a hint with
922 if (rq->write_hint != bio->bi_write_hint)
925 if (rq->ioprio != bio_prio(bio))
931 enum elv_merge blk_try_merge(struct request *rq, struct bio *bio)
933 if (blk_discard_mergable(rq))
934 return ELEVATOR_DISCARD_MERGE;
935 else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
936 return ELEVATOR_BACK_MERGE;
937 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
938 return ELEVATOR_FRONT_MERGE;
939 return ELEVATOR_NO_MERGE;
942 static void blk_account_io_merge_bio(struct request *req)
944 if (!blk_do_io_stat(req))
948 part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
952 enum bio_merge_status {
958 static enum bio_merge_status bio_attempt_back_merge(struct request *req,
959 struct bio *bio, unsigned int nr_segs)
961 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
963 if (!ll_back_merge_fn(req, bio, nr_segs))
964 return BIO_MERGE_FAILED;
966 trace_block_bio_backmerge(bio);
967 rq_qos_merge(req->q, req, bio);
969 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
970 blk_rq_set_mixed_merge(req);
972 req->biotail->bi_next = bio;
974 req->__data_len += bio->bi_iter.bi_size;
976 bio_crypt_free_ctx(bio);
978 blk_account_io_merge_bio(req);
982 static enum bio_merge_status bio_attempt_front_merge(struct request *req,
983 struct bio *bio, unsigned int nr_segs)
985 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
987 if (!ll_front_merge_fn(req, bio, nr_segs))
988 return BIO_MERGE_FAILED;
990 trace_block_bio_frontmerge(bio);
991 rq_qos_merge(req->q, req, bio);
993 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
994 blk_rq_set_mixed_merge(req);
996 bio->bi_next = req->bio;
999 req->__sector = bio->bi_iter.bi_sector;
1000 req->__data_len += bio->bi_iter.bi_size;
1002 bio_crypt_do_front_merge(req, bio);
1004 blk_account_io_merge_bio(req);
1005 return BIO_MERGE_OK;
1008 static enum bio_merge_status bio_attempt_discard_merge(struct request_queue *q,
1009 struct request *req, struct bio *bio)
1011 unsigned short segments = blk_rq_nr_discard_segments(req);
1013 if (segments >= queue_max_discard_segments(q))
1015 if (blk_rq_sectors(req) + bio_sectors(bio) >
1016 blk_rq_get_max_sectors(req, blk_rq_pos(req)))
1019 rq_qos_merge(q, req, bio);
1021 req->biotail->bi_next = bio;
1023 req->__data_len += bio->bi_iter.bi_size;
1024 req->nr_phys_segments = segments + 1;
1026 blk_account_io_merge_bio(req);
1027 return BIO_MERGE_OK;
1029 req_set_nomerge(q, req);
1030 return BIO_MERGE_FAILED;
1033 static enum bio_merge_status blk_attempt_bio_merge(struct request_queue *q,
1036 unsigned int nr_segs,
1037 bool sched_allow_merge)
1039 if (!blk_rq_merge_ok(rq, bio))
1040 return BIO_MERGE_NONE;
1042 switch (blk_try_merge(rq, bio)) {
1043 case ELEVATOR_BACK_MERGE:
1044 if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio))
1045 return bio_attempt_back_merge(rq, bio, nr_segs);
1047 case ELEVATOR_FRONT_MERGE:
1048 if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio))
1049 return bio_attempt_front_merge(rq, bio, nr_segs);
1051 case ELEVATOR_DISCARD_MERGE:
1052 return bio_attempt_discard_merge(q, rq, bio);
1054 return BIO_MERGE_NONE;
1057 return BIO_MERGE_FAILED;
1061 * blk_attempt_plug_merge - try to merge with %current's plugged list
1062 * @q: request_queue new bio is being queued at
1063 * @bio: new bio being queued
1064 * @nr_segs: number of segments in @bio
1065 * from the passed in @q already in the plug list
1067 * Determine whether @bio being queued on @q can be merged with the previous
1068 * request on %current's plugged list. Returns %true if merge was successful,
1071 * Plugging coalesces IOs from the same issuer for the same purpose without
1072 * going through @q->queue_lock. As such it's more of an issuing mechanism
1073 * than scheduling, and the request, while may have elvpriv data, is not
1074 * added on the elevator at this point. In addition, we don't have
1075 * reliable access to the elevator outside queue lock. Only check basic
1076 * merging parameters without querying the elevator.
1078 * Caller must ensure !blk_queue_nomerges(q) beforehand.
1080 bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
1081 unsigned int nr_segs)
1083 struct blk_plug *plug;
1086 plug = blk_mq_plug(q, bio);
1087 if (!plug || rq_list_empty(plug->mq_list))
1090 /* check the previously added entry for a quick merge attempt */
1091 rq = rq_list_peek(&plug->mq_list);
1093 if (blk_attempt_bio_merge(q, rq, bio, nr_segs, false) ==
1101 * Iterate list of requests and see if we can merge this bio with any
1104 bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
1105 struct bio *bio, unsigned int nr_segs)
1110 list_for_each_entry_reverse(rq, list, queuelist) {
1114 switch (blk_attempt_bio_merge(q, rq, bio, nr_segs, true)) {
1115 case BIO_MERGE_NONE:
1119 case BIO_MERGE_FAILED:
1127 EXPORT_SYMBOL_GPL(blk_bio_list_merge);
1129 bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
1130 unsigned int nr_segs, struct request **merged_request)
1134 switch (elv_merge(q, &rq, bio)) {
1135 case ELEVATOR_BACK_MERGE:
1136 if (!blk_mq_sched_allow_merge(q, rq, bio))
1138 if (bio_attempt_back_merge(rq, bio, nr_segs) != BIO_MERGE_OK)
1140 *merged_request = attempt_back_merge(q, rq);
1141 if (!*merged_request)
1142 elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
1144 case ELEVATOR_FRONT_MERGE:
1145 if (!blk_mq_sched_allow_merge(q, rq, bio))
1147 if (bio_attempt_front_merge(rq, bio, nr_segs) != BIO_MERGE_OK)
1149 *merged_request = attempt_front_merge(q, rq);
1150 if (!*merged_request)
1151 elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
1153 case ELEVATOR_DISCARD_MERGE:
1154 return bio_attempt_discard_merge(q, rq, bio) == BIO_MERGE_OK;
1159 EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);