2 * Functions related to segment and merge handling
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <linux/blkdev.h>
8 #include <linux/scatterlist.h>
12 static struct bio
*blk_bio_discard_split(struct request_queue
*q
,
17 unsigned int max_discard_sectors
, granularity
;
20 unsigned split_sectors
;
24 /* Zero-sector (unknown) and one-sector granularities are the same. */
25 granularity
= max(q
->limits
.discard_granularity
>> 9, 1U);
27 max_discard_sectors
= min(q
->limits
.max_discard_sectors
, UINT_MAX
>> 9);
28 max_discard_sectors
-= max_discard_sectors
% granularity
;
30 if (unlikely(!max_discard_sectors
)) {
35 if (bio_sectors(bio
) <= max_discard_sectors
)
38 split_sectors
= max_discard_sectors
;
41 * If the next starting sector would be misaligned, stop the discard at
42 * the previous aligned sector.
44 alignment
= (q
->limits
.discard_alignment
>> 9) % granularity
;
46 tmp
= bio
->bi_iter
.bi_sector
+ split_sectors
- alignment
;
47 tmp
= sector_div(tmp
, granularity
);
49 if (split_sectors
> tmp
)
52 return bio_split(bio
, split_sectors
, GFP_NOIO
, bs
);
55 static struct bio
*blk_bio_write_same_split(struct request_queue
*q
,
62 if (!q
->limits
.max_write_same_sectors
)
65 if (bio_sectors(bio
) <= q
->limits
.max_write_same_sectors
)
68 return bio_split(bio
, q
->limits
.max_write_same_sectors
, GFP_NOIO
, bs
);
71 static struct bio
*blk_bio_segment_split(struct request_queue
*q
,
76 struct bio_vec bv
, bvprv
, *bvprvp
= NULL
;
77 struct bvec_iter iter
;
78 unsigned seg_size
= 0, nsegs
= 0, sectors
= 0;
79 unsigned front_seg_size
= bio
->bi_seg_front_size
;
81 struct bio
*new = NULL
;
83 bio_for_each_segment(bv
, bio
, iter
) {
84 if (sectors
+ (bv
.bv_len
>> 9) > queue_max_sectors(q
))
88 * If the queue doesn't support SG gaps and adding this
89 * offset would create a gap, disallow it.
91 if (bvprvp
&& bvec_gap_to_prev(q
, bvprvp
, bv
.bv_offset
))
94 if (bvprvp
&& blk_queue_cluster(q
)) {
95 if (seg_size
+ bv
.bv_len
> queue_max_segment_size(q
))
97 if (!BIOVEC_PHYS_MERGEABLE(bvprvp
, &bv
))
99 if (!BIOVEC_SEG_BOUNDARY(q
, bvprvp
, &bv
))
102 seg_size
+= bv
.bv_len
;
105 sectors
+= bv
.bv_len
>> 9;
107 if (nsegs
== 1 && seg_size
> front_seg_size
)
108 front_seg_size
= seg_size
;
112 if (nsegs
== queue_max_segments(q
))
118 seg_size
= bv
.bv_len
;
119 sectors
+= bv
.bv_len
>> 9;
121 if (nsegs
== 1 && seg_size
> front_seg_size
)
122 front_seg_size
= seg_size
;
130 new = bio_split(bio
, sectors
, GFP_NOIO
, bs
);
135 bio
->bi_seg_front_size
= front_seg_size
;
136 if (seg_size
> bio
->bi_seg_back_size
)
137 bio
->bi_seg_back_size
= seg_size
;
139 return do_split
? new : NULL
;
142 void blk_queue_split(struct request_queue
*q
, struct bio
**bio
,
145 struct bio
*split
, *res
;
148 if ((*bio
)->bi_rw
& REQ_DISCARD
)
149 split
= blk_bio_discard_split(q
, *bio
, bs
, &nsegs
);
150 else if ((*bio
)->bi_rw
& REQ_WRITE_SAME
)
151 split
= blk_bio_write_same_split(q
, *bio
, bs
, &nsegs
);
153 split
= blk_bio_segment_split(q
, *bio
, q
->bio_split
, &nsegs
);
155 /* physical segments can be figured out during splitting */
156 res
= split
? split
: *bio
;
157 res
->bi_phys_segments
= nsegs
;
158 bio_set_flag(res
, BIO_SEG_VALID
);
161 /* there isn't chance to merge the splitted bio */
162 split
->bi_rw
|= REQ_NOMERGE
;
164 bio_chain(split
, *bio
);
165 generic_make_request(*bio
);
169 EXPORT_SYMBOL(blk_queue_split
);
171 static unsigned int __blk_recalc_rq_segments(struct request_queue
*q
,
175 struct bio_vec bv
, bvprv
= { NULL
};
176 int cluster
, prev
= 0;
177 unsigned int seg_size
, nr_phys_segs
;
178 struct bio
*fbio
, *bbio
;
179 struct bvec_iter iter
;
185 * This should probably be returning 0, but blk_add_request_payload()
188 if (bio
->bi_rw
& REQ_DISCARD
)
191 if (bio
->bi_rw
& REQ_WRITE_SAME
)
195 cluster
= blk_queue_cluster(q
);
199 bio_for_each_segment(bv
, bio
, iter
) {
201 * If SG merging is disabled, each bio vector is
207 if (prev
&& cluster
) {
208 if (seg_size
+ bv
.bv_len
209 > queue_max_segment_size(q
))
211 if (!BIOVEC_PHYS_MERGEABLE(&bvprv
, &bv
))
213 if (!BIOVEC_SEG_BOUNDARY(q
, &bvprv
, &bv
))
216 seg_size
+= bv
.bv_len
;
221 if (nr_phys_segs
== 1 && seg_size
>
222 fbio
->bi_seg_front_size
)
223 fbio
->bi_seg_front_size
= seg_size
;
228 seg_size
= bv
.bv_len
;
233 if (nr_phys_segs
== 1 && seg_size
> fbio
->bi_seg_front_size
)
234 fbio
->bi_seg_front_size
= seg_size
;
235 if (seg_size
> bbio
->bi_seg_back_size
)
236 bbio
->bi_seg_back_size
= seg_size
;
241 void blk_recalc_rq_segments(struct request
*rq
)
243 bool no_sg_merge
= !!test_bit(QUEUE_FLAG_NO_SG_MERGE
,
244 &rq
->q
->queue_flags
);
246 rq
->nr_phys_segments
= __blk_recalc_rq_segments(rq
->q
, rq
->bio
,
250 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
252 unsigned short seg_cnt
;
254 /* estimate segment number by bi_vcnt for non-cloned bio */
255 if (bio_flagged(bio
, BIO_CLONED
))
256 seg_cnt
= bio_segments(bio
);
258 seg_cnt
= bio
->bi_vcnt
;
260 if (test_bit(QUEUE_FLAG_NO_SG_MERGE
, &q
->queue_flags
) &&
261 (seg_cnt
< queue_max_segments(q
)))
262 bio
->bi_phys_segments
= seg_cnt
;
264 struct bio
*nxt
= bio
->bi_next
;
267 bio
->bi_phys_segments
= __blk_recalc_rq_segments(q
, bio
, false);
271 bio_set_flag(bio
, BIO_SEG_VALID
);
273 EXPORT_SYMBOL(blk_recount_segments
);
275 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
278 struct bio_vec end_bv
= { NULL
}, nxt_bv
;
279 struct bvec_iter iter
;
281 if (!blk_queue_cluster(q
))
284 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
285 queue_max_segment_size(q
))
288 if (!bio_has_data(bio
))
291 bio_for_each_segment(end_bv
, bio
, iter
)
292 if (end_bv
.bv_len
== iter
.bi_size
)
295 nxt_bv
= bio_iovec(nxt
);
297 if (!BIOVEC_PHYS_MERGEABLE(&end_bv
, &nxt_bv
))
301 * bio and nxt are contiguous in memory; check if the queue allows
302 * these two to be merged into one
304 if (BIOVEC_SEG_BOUNDARY(q
, &end_bv
, &nxt_bv
))
311 __blk_segment_map_sg(struct request_queue
*q
, struct bio_vec
*bvec
,
312 struct scatterlist
*sglist
, struct bio_vec
*bvprv
,
313 struct scatterlist
**sg
, int *nsegs
, int *cluster
)
316 int nbytes
= bvec
->bv_len
;
318 if (*sg
&& *cluster
) {
319 if ((*sg
)->length
+ nbytes
> queue_max_segment_size(q
))
322 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
324 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
327 (*sg
)->length
+= nbytes
;
334 * If the driver previously mapped a shorter
335 * list, we could see a termination bit
336 * prematurely unless it fully inits the sg
337 * table on each mapping. We KNOW that there
338 * must be more entries here or the driver
339 * would be buggy, so force clear the
340 * termination bit to avoid doing a full
341 * sg_init_table() in drivers for each command.
347 sg_set_page(*sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
353 static int __blk_bios_map_sg(struct request_queue
*q
, struct bio
*bio
,
354 struct scatterlist
*sglist
,
355 struct scatterlist
**sg
)
357 struct bio_vec bvec
, bvprv
= { NULL
};
358 struct bvec_iter iter
;
362 cluster
= blk_queue_cluster(q
);
364 if (bio
->bi_rw
& REQ_DISCARD
) {
366 * This is a hack - drivers should be neither modifying the
367 * biovec, nor relying on bi_vcnt - but because of
368 * blk_add_request_payload(), a discard bio may or may not have
369 * a payload we need to set up here (thank you Christoph) and
370 * bi_vcnt is really the only way of telling if we need to.
379 if (bio
->bi_rw
& REQ_WRITE_SAME
) {
382 bvec
= bio_iovec(bio
);
383 sg_set_page(*sg
, bvec
.bv_page
, bvec
.bv_len
, bvec
.bv_offset
);
388 bio_for_each_segment(bvec
, bio
, iter
)
389 __blk_segment_map_sg(q
, &bvec
, sglist
, &bvprv
, sg
,
396 * map a request to scatterlist, return number of sg entries setup. Caller
397 * must make sure sg can hold rq->nr_phys_segments entries
399 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
400 struct scatterlist
*sglist
)
402 struct scatterlist
*sg
= NULL
;
406 nsegs
= __blk_bios_map_sg(q
, rq
->bio
, sglist
, &sg
);
408 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
409 (blk_rq_bytes(rq
) & q
->dma_pad_mask
)) {
410 unsigned int pad_len
=
411 (q
->dma_pad_mask
& ~blk_rq_bytes(rq
)) + 1;
413 sg
->length
+= pad_len
;
414 rq
->extra_len
+= pad_len
;
417 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
418 if (rq
->cmd_flags
& REQ_WRITE
)
419 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
423 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
425 ((unsigned long)q
->dma_drain_buffer
) &
428 rq
->extra_len
+= q
->dma_drain_size
;
435 * Something must have been wrong if the figured number of
436 * segment is bigger than number of req's physical segments
438 WARN_ON(nsegs
> rq
->nr_phys_segments
);
442 EXPORT_SYMBOL(blk_rq_map_sg
);
444 static inline int ll_new_hw_segment(struct request_queue
*q
,
448 int nr_phys_segs
= bio_phys_segments(q
, bio
);
450 if (req
->nr_phys_segments
+ nr_phys_segs
> queue_max_segments(q
))
453 if (blk_integrity_merge_bio(q
, req
, bio
) == false)
457 * This will form the start of a new hw segment. Bump both
460 req
->nr_phys_segments
+= nr_phys_segs
;
464 req
->cmd_flags
|= REQ_NOMERGE
;
465 if (req
== q
->last_merge
)
466 q
->last_merge
= NULL
;
470 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
473 if (req_gap_back_merge(req
, bio
))
475 if (blk_integrity_rq(req
) &&
476 integrity_req_gap_back_merge(req
, bio
))
478 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
479 blk_rq_get_max_sectors(req
)) {
480 req
->cmd_flags
|= REQ_NOMERGE
;
481 if (req
== q
->last_merge
)
482 q
->last_merge
= NULL
;
485 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
486 blk_recount_segments(q
, req
->biotail
);
487 if (!bio_flagged(bio
, BIO_SEG_VALID
))
488 blk_recount_segments(q
, bio
);
490 return ll_new_hw_segment(q
, req
, bio
);
493 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
497 if (req_gap_front_merge(req
, bio
))
499 if (blk_integrity_rq(req
) &&
500 integrity_req_gap_front_merge(req
, bio
))
502 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
503 blk_rq_get_max_sectors(req
)) {
504 req
->cmd_flags
|= REQ_NOMERGE
;
505 if (req
== q
->last_merge
)
506 q
->last_merge
= NULL
;
509 if (!bio_flagged(bio
, BIO_SEG_VALID
))
510 blk_recount_segments(q
, bio
);
511 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
512 blk_recount_segments(q
, req
->bio
);
514 return ll_new_hw_segment(q
, req
, bio
);
518 * blk-mq uses req->special to carry normal driver per-request payload, it
519 * does not indicate a prepared command that we cannot merge with.
521 static bool req_no_special_merge(struct request
*req
)
523 struct request_queue
*q
= req
->q
;
525 return !q
->mq_ops
&& req
->special
;
528 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
529 struct request
*next
)
531 int total_phys_segments
;
532 unsigned int seg_size
=
533 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
536 * First check if the either of the requests are re-queued
537 * requests. Can't merge them if they are.
539 if (req_no_special_merge(req
) || req_no_special_merge(next
))
542 if (req_gap_back_merge(req
, next
->bio
))
546 * Will it become too large?
548 if ((blk_rq_sectors(req
) + blk_rq_sectors(next
)) >
549 blk_rq_get_max_sectors(req
))
552 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
553 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
554 if (req
->nr_phys_segments
== 1)
555 req
->bio
->bi_seg_front_size
= seg_size
;
556 if (next
->nr_phys_segments
== 1)
557 next
->biotail
->bi_seg_back_size
= seg_size
;
558 total_phys_segments
--;
561 if (total_phys_segments
> queue_max_segments(q
))
564 if (blk_integrity_merge_rq(q
, req
, next
) == false)
568 req
->nr_phys_segments
= total_phys_segments
;
573 * blk_rq_set_mixed_merge - mark a request as mixed merge
574 * @rq: request to mark as mixed merge
577 * @rq is about to be mixed merged. Make sure the attributes
578 * which can be mixed are set in each bio and mark @rq as mixed
581 void blk_rq_set_mixed_merge(struct request
*rq
)
583 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
586 if (rq
->cmd_flags
& REQ_MIXED_MERGE
)
590 * @rq will no longer represent mixable attributes for all the
591 * contained bios. It will just track those of the first one.
592 * Distributes the attributs to each bio.
594 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
595 WARN_ON_ONCE((bio
->bi_rw
& REQ_FAILFAST_MASK
) &&
596 (bio
->bi_rw
& REQ_FAILFAST_MASK
) != ff
);
599 rq
->cmd_flags
|= REQ_MIXED_MERGE
;
602 static void blk_account_io_merge(struct request
*req
)
604 if (blk_do_io_stat(req
)) {
605 struct hd_struct
*part
;
608 cpu
= part_stat_lock();
611 part_round_stats(cpu
, part
);
612 part_dec_in_flight(part
, rq_data_dir(req
));
620 * Has to be called with the request spinlock acquired
622 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
623 struct request
*next
)
625 if (!rq_mergeable(req
) || !rq_mergeable(next
))
628 if (!blk_check_merge_flags(req
->cmd_flags
, next
->cmd_flags
))
634 if (blk_rq_pos(req
) + blk_rq_sectors(req
) != blk_rq_pos(next
))
637 if (rq_data_dir(req
) != rq_data_dir(next
)
638 || req
->rq_disk
!= next
->rq_disk
639 || req_no_special_merge(next
))
642 if (req
->cmd_flags
& REQ_WRITE_SAME
&&
643 !blk_write_same_mergeable(req
->bio
, next
->bio
))
647 * If we are allowed to merge, then append bio list
648 * from next to rq and release next. merge_requests_fn
649 * will have updated segment counts, update sector
652 if (!ll_merge_requests_fn(q
, req
, next
))
656 * If failfast settings disagree or any of the two is already
657 * a mixed merge, mark both as mixed before proceeding. This
658 * makes sure that all involved bios have mixable attributes
661 if ((req
->cmd_flags
| next
->cmd_flags
) & REQ_MIXED_MERGE
||
662 (req
->cmd_flags
& REQ_FAILFAST_MASK
) !=
663 (next
->cmd_flags
& REQ_FAILFAST_MASK
)) {
664 blk_rq_set_mixed_merge(req
);
665 blk_rq_set_mixed_merge(next
);
669 * At this point we have either done a back merge
670 * or front merge. We need the smaller start_time of
671 * the merged requests to be the current request
672 * for accounting purposes.
674 if (time_after(req
->start_time
, next
->start_time
))
675 req
->start_time
= next
->start_time
;
677 req
->biotail
->bi_next
= next
->bio
;
678 req
->biotail
= next
->biotail
;
680 req
->__data_len
+= blk_rq_bytes(next
);
682 elv_merge_requests(q
, req
, next
);
685 * 'next' is going away, so update stats accordingly
687 blk_account_io_merge(next
);
689 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
690 if (blk_rq_cpu_valid(next
))
691 req
->cpu
= next
->cpu
;
693 /* owner-ship of bio passed from next to req */
695 __blk_put_request(q
, next
);
699 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
701 struct request
*next
= elv_latter_request(q
, rq
);
704 return attempt_merge(q
, rq
, next
);
709 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
711 struct request
*prev
= elv_former_request(q
, rq
);
714 return attempt_merge(q
, prev
, rq
);
719 int blk_attempt_req_merge(struct request_queue
*q
, struct request
*rq
,
720 struct request
*next
)
722 return attempt_merge(q
, rq
, next
);
725 bool blk_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
727 if (!rq_mergeable(rq
) || !bio_mergeable(bio
))
730 if (!blk_check_merge_flags(rq
->cmd_flags
, bio
->bi_rw
))
733 /* different data direction or already started, don't merge */
734 if (bio_data_dir(bio
) != rq_data_dir(rq
))
737 /* must be same device and not a special request */
738 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| req_no_special_merge(rq
))
741 /* only merge integrity protected bio into ditto rq */
742 if (blk_integrity_merge_bio(rq
->q
, rq
, bio
) == false)
745 /* must be using the same buffer */
746 if (rq
->cmd_flags
& REQ_WRITE_SAME
&&
747 !blk_write_same_mergeable(rq
->bio
, bio
))
753 int blk_try_merge(struct request
*rq
, struct bio
*bio
)
755 if (blk_rq_pos(rq
) + blk_rq_sectors(rq
) == bio
->bi_iter
.bi_sector
)
756 return ELEVATOR_BACK_MERGE
;
757 else if (blk_rq_pos(rq
) - bio_sectors(bio
) == bio
->bi_iter
.bi_sector
)
758 return ELEVATOR_FRONT_MERGE
;
759 return ELEVATOR_NO_MERGE
;