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
,
16 unsigned int max_discard_sectors
, granularity
;
19 unsigned split_sectors
;
21 /* Zero-sector (unknown) and one-sector granularities are the same. */
22 granularity
= max(q
->limits
.discard_granularity
>> 9, 1U);
24 max_discard_sectors
= min(q
->limits
.max_discard_sectors
, UINT_MAX
>> 9);
25 max_discard_sectors
-= max_discard_sectors
% granularity
;
27 if (unlikely(!max_discard_sectors
)) {
32 if (bio_sectors(bio
) <= max_discard_sectors
)
35 split_sectors
= max_discard_sectors
;
38 * If the next starting sector would be misaligned, stop the discard at
39 * the previous aligned sector.
41 alignment
= (q
->limits
.discard_alignment
>> 9) % granularity
;
43 tmp
= bio
->bi_iter
.bi_sector
+ split_sectors
- alignment
;
44 tmp
= sector_div(tmp
, granularity
);
46 if (split_sectors
> tmp
)
49 return bio_split(bio
, split_sectors
, GFP_NOIO
, bs
);
52 static struct bio
*blk_bio_write_same_split(struct request_queue
*q
,
56 if (!q
->limits
.max_write_same_sectors
)
59 if (bio_sectors(bio
) <= q
->limits
.max_write_same_sectors
)
62 return bio_split(bio
, q
->limits
.max_write_same_sectors
, GFP_NOIO
, bs
);
65 static struct bio
*blk_bio_segment_split(struct request_queue
*q
,
70 struct bio_vec bv
, bvprv
;
71 struct bvec_iter iter
;
72 unsigned seg_size
= 0, nsegs
= 0, sectors
= 0;
75 bio_for_each_segment(bv
, bio
, iter
) {
76 sectors
+= bv
.bv_len
>> 9;
78 if (sectors
> queue_max_sectors(q
))
82 * If the queue doesn't support SG gaps and adding this
83 * offset would create a gap, disallow it.
85 if (q
->queue_flags
& (1 << QUEUE_FLAG_SG_GAPS
) &&
86 prev
&& bvec_gap_to_prev(&bvprv
, bv
.bv_offset
))
89 if (prev
&& blk_queue_cluster(q
)) {
90 if (seg_size
+ bv
.bv_len
> queue_max_segment_size(q
))
92 if (!BIOVEC_PHYS_MERGEABLE(&bvprv
, &bv
))
94 if (!BIOVEC_SEG_BOUNDARY(q
, &bvprv
, &bv
))
97 seg_size
+= bv
.bv_len
;
103 if (nsegs
== queue_max_segments(q
))
109 seg_size
= bv
.bv_len
;
114 split
= bio_clone_bioset(bio
, GFP_NOIO
, bs
);
116 split
->bi_iter
.bi_size
-= iter
.bi_size
;
119 if (bio_integrity(bio
)) {
120 bio_integrity_advance(bio
, split
->bi_iter
.bi_size
);
121 bio_integrity_trim(split
, 0, bio_sectors(split
));
127 void blk_queue_split(struct request_queue
*q
, struct bio
**bio
,
132 if ((*bio
)->bi_rw
& REQ_DISCARD
)
133 split
= blk_bio_discard_split(q
, *bio
, bs
);
134 else if ((*bio
)->bi_rw
& REQ_WRITE_SAME
)
135 split
= blk_bio_write_same_split(q
, *bio
, bs
);
137 split
= blk_bio_segment_split(q
, *bio
, q
->bio_split
);
140 bio_chain(split
, *bio
);
141 generic_make_request(*bio
);
145 EXPORT_SYMBOL(blk_queue_split
);
147 static unsigned int __blk_recalc_rq_segments(struct request_queue
*q
,
151 struct bio_vec bv
, bvprv
= { NULL
};
152 int cluster
, prev
= 0;
153 unsigned int seg_size
, nr_phys_segs
;
154 struct bio
*fbio
, *bbio
;
155 struct bvec_iter iter
;
161 * This should probably be returning 0, but blk_add_request_payload()
164 if (bio
->bi_rw
& REQ_DISCARD
)
167 if (bio
->bi_rw
& REQ_WRITE_SAME
)
171 cluster
= blk_queue_cluster(q
);
175 bio_for_each_segment(bv
, bio
, iter
) {
177 * If SG merging is disabled, each bio vector is
183 if (prev
&& cluster
) {
184 if (seg_size
+ bv
.bv_len
185 > queue_max_segment_size(q
))
187 if (!BIOVEC_PHYS_MERGEABLE(&bvprv
, &bv
))
189 if (!BIOVEC_SEG_BOUNDARY(q
, &bvprv
, &bv
))
192 seg_size
+= bv
.bv_len
;
197 if (nr_phys_segs
== 1 && seg_size
>
198 fbio
->bi_seg_front_size
)
199 fbio
->bi_seg_front_size
= seg_size
;
204 seg_size
= bv
.bv_len
;
209 if (nr_phys_segs
== 1 && seg_size
> fbio
->bi_seg_front_size
)
210 fbio
->bi_seg_front_size
= seg_size
;
211 if (seg_size
> bbio
->bi_seg_back_size
)
212 bbio
->bi_seg_back_size
= seg_size
;
217 void blk_recalc_rq_segments(struct request
*rq
)
219 bool no_sg_merge
= !!test_bit(QUEUE_FLAG_NO_SG_MERGE
,
220 &rq
->q
->queue_flags
);
222 rq
->nr_phys_segments
= __blk_recalc_rq_segments(rq
->q
, rq
->bio
,
226 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
228 unsigned short seg_cnt
;
230 /* estimate segment number by bi_vcnt for non-cloned bio */
231 if (bio_flagged(bio
, BIO_CLONED
))
232 seg_cnt
= bio_segments(bio
);
234 seg_cnt
= bio
->bi_vcnt
;
236 if (test_bit(QUEUE_FLAG_NO_SG_MERGE
, &q
->queue_flags
) &&
237 (seg_cnt
< queue_max_segments(q
)))
238 bio
->bi_phys_segments
= seg_cnt
;
240 struct bio
*nxt
= bio
->bi_next
;
243 bio
->bi_phys_segments
= __blk_recalc_rq_segments(q
, bio
, false);
247 bio_set_flag(bio
, BIO_SEG_VALID
);
249 EXPORT_SYMBOL(blk_recount_segments
);
251 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
254 struct bio_vec end_bv
= { NULL
}, nxt_bv
;
255 struct bvec_iter iter
;
257 if (!blk_queue_cluster(q
))
260 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
261 queue_max_segment_size(q
))
264 if (!bio_has_data(bio
))
267 bio_for_each_segment(end_bv
, bio
, iter
)
268 if (end_bv
.bv_len
== iter
.bi_size
)
271 nxt_bv
= bio_iovec(nxt
);
273 if (!BIOVEC_PHYS_MERGEABLE(&end_bv
, &nxt_bv
))
277 * bio and nxt are contiguous in memory; check if the queue allows
278 * these two to be merged into one
280 if (BIOVEC_SEG_BOUNDARY(q
, &end_bv
, &nxt_bv
))
287 __blk_segment_map_sg(struct request_queue
*q
, struct bio_vec
*bvec
,
288 struct scatterlist
*sglist
, struct bio_vec
*bvprv
,
289 struct scatterlist
**sg
, int *nsegs
, int *cluster
)
292 int nbytes
= bvec
->bv_len
;
294 if (*sg
&& *cluster
) {
295 if ((*sg
)->length
+ nbytes
> queue_max_segment_size(q
))
298 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
300 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
303 (*sg
)->length
+= nbytes
;
310 * If the driver previously mapped a shorter
311 * list, we could see a termination bit
312 * prematurely unless it fully inits the sg
313 * table on each mapping. We KNOW that there
314 * must be more entries here or the driver
315 * would be buggy, so force clear the
316 * termination bit to avoid doing a full
317 * sg_init_table() in drivers for each command.
323 sg_set_page(*sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
329 static int __blk_bios_map_sg(struct request_queue
*q
, struct bio
*bio
,
330 struct scatterlist
*sglist
,
331 struct scatterlist
**sg
)
333 struct bio_vec bvec
, bvprv
= { NULL
};
334 struct bvec_iter iter
;
338 cluster
= blk_queue_cluster(q
);
340 if (bio
->bi_rw
& REQ_DISCARD
) {
342 * This is a hack - drivers should be neither modifying the
343 * biovec, nor relying on bi_vcnt - but because of
344 * blk_add_request_payload(), a discard bio may or may not have
345 * a payload we need to set up here (thank you Christoph) and
346 * bi_vcnt is really the only way of telling if we need to.
355 if (bio
->bi_rw
& REQ_WRITE_SAME
) {
358 bvec
= bio_iovec(bio
);
359 sg_set_page(*sg
, bvec
.bv_page
, bvec
.bv_len
, bvec
.bv_offset
);
364 bio_for_each_segment(bvec
, bio
, iter
)
365 __blk_segment_map_sg(q
, &bvec
, sglist
, &bvprv
, sg
,
372 * map a request to scatterlist, return number of sg entries setup. Caller
373 * must make sure sg can hold rq->nr_phys_segments entries
375 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
376 struct scatterlist
*sglist
)
378 struct scatterlist
*sg
= NULL
;
382 nsegs
= __blk_bios_map_sg(q
, rq
->bio
, sglist
, &sg
);
384 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
385 (blk_rq_bytes(rq
) & q
->dma_pad_mask
)) {
386 unsigned int pad_len
=
387 (q
->dma_pad_mask
& ~blk_rq_bytes(rq
)) + 1;
389 sg
->length
+= pad_len
;
390 rq
->extra_len
+= pad_len
;
393 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
394 if (rq
->cmd_flags
& REQ_WRITE
)
395 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
399 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
401 ((unsigned long)q
->dma_drain_buffer
) &
404 rq
->extra_len
+= q
->dma_drain_size
;
412 EXPORT_SYMBOL(blk_rq_map_sg
);
414 static inline int ll_new_hw_segment(struct request_queue
*q
,
418 int nr_phys_segs
= bio_phys_segments(q
, bio
);
420 if (req
->nr_phys_segments
+ nr_phys_segs
> queue_max_segments(q
))
423 if (blk_integrity_merge_bio(q
, req
, bio
) == false)
427 * This will form the start of a new hw segment. Bump both
430 req
->nr_phys_segments
+= nr_phys_segs
;
434 req
->cmd_flags
|= REQ_NOMERGE
;
435 if (req
== q
->last_merge
)
436 q
->last_merge
= NULL
;
440 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
443 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
444 blk_rq_get_max_sectors(req
)) {
445 req
->cmd_flags
|= REQ_NOMERGE
;
446 if (req
== q
->last_merge
)
447 q
->last_merge
= NULL
;
450 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
451 blk_recount_segments(q
, req
->biotail
);
452 if (!bio_flagged(bio
, BIO_SEG_VALID
))
453 blk_recount_segments(q
, bio
);
455 return ll_new_hw_segment(q
, req
, bio
);
458 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
461 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
462 blk_rq_get_max_sectors(req
)) {
463 req
->cmd_flags
|= REQ_NOMERGE
;
464 if (req
== q
->last_merge
)
465 q
->last_merge
= NULL
;
468 if (!bio_flagged(bio
, BIO_SEG_VALID
))
469 blk_recount_segments(q
, bio
);
470 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
471 blk_recount_segments(q
, req
->bio
);
473 return ll_new_hw_segment(q
, req
, bio
);
477 * blk-mq uses req->special to carry normal driver per-request payload, it
478 * does not indicate a prepared command that we cannot merge with.
480 static bool req_no_special_merge(struct request
*req
)
482 struct request_queue
*q
= req
->q
;
484 return !q
->mq_ops
&& req
->special
;
487 static int req_gap_to_prev(struct request
*req
, struct request
*next
)
489 struct bio
*prev
= req
->biotail
;
491 return bvec_gap_to_prev(&prev
->bi_io_vec
[prev
->bi_vcnt
- 1],
492 next
->bio
->bi_io_vec
[0].bv_offset
);
495 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
496 struct request
*next
)
498 int total_phys_segments
;
499 unsigned int seg_size
=
500 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
503 * First check if the either of the requests are re-queued
504 * requests. Can't merge them if they are.
506 if (req_no_special_merge(req
) || req_no_special_merge(next
))
509 if (test_bit(QUEUE_FLAG_SG_GAPS
, &q
->queue_flags
) &&
510 req_gap_to_prev(req
, next
))
514 * Will it become too large?
516 if ((blk_rq_sectors(req
) + blk_rq_sectors(next
)) >
517 blk_rq_get_max_sectors(req
))
520 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
521 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
522 if (req
->nr_phys_segments
== 1)
523 req
->bio
->bi_seg_front_size
= seg_size
;
524 if (next
->nr_phys_segments
== 1)
525 next
->biotail
->bi_seg_back_size
= seg_size
;
526 total_phys_segments
--;
529 if (total_phys_segments
> queue_max_segments(q
))
532 if (blk_integrity_merge_rq(q
, req
, next
) == false)
536 req
->nr_phys_segments
= total_phys_segments
;
541 * blk_rq_set_mixed_merge - mark a request as mixed merge
542 * @rq: request to mark as mixed merge
545 * @rq is about to be mixed merged. Make sure the attributes
546 * which can be mixed are set in each bio and mark @rq as mixed
549 void blk_rq_set_mixed_merge(struct request
*rq
)
551 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
554 if (rq
->cmd_flags
& REQ_MIXED_MERGE
)
558 * @rq will no longer represent mixable attributes for all the
559 * contained bios. It will just track those of the first one.
560 * Distributes the attributs to each bio.
562 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
563 WARN_ON_ONCE((bio
->bi_rw
& REQ_FAILFAST_MASK
) &&
564 (bio
->bi_rw
& REQ_FAILFAST_MASK
) != ff
);
567 rq
->cmd_flags
|= REQ_MIXED_MERGE
;
570 static void blk_account_io_merge(struct request
*req
)
572 if (blk_do_io_stat(req
)) {
573 struct hd_struct
*part
;
576 cpu
= part_stat_lock();
579 part_round_stats(cpu
, part
);
580 part_dec_in_flight(part
, rq_data_dir(req
));
588 * Has to be called with the request spinlock acquired
590 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
591 struct request
*next
)
593 if (!rq_mergeable(req
) || !rq_mergeable(next
))
596 if (!blk_check_merge_flags(req
->cmd_flags
, next
->cmd_flags
))
602 if (blk_rq_pos(req
) + blk_rq_sectors(req
) != blk_rq_pos(next
))
605 if (rq_data_dir(req
) != rq_data_dir(next
)
606 || req
->rq_disk
!= next
->rq_disk
607 || req_no_special_merge(next
))
610 if (req
->cmd_flags
& REQ_WRITE_SAME
&&
611 !blk_write_same_mergeable(req
->bio
, next
->bio
))
615 * If we are allowed to merge, then append bio list
616 * from next to rq and release next. merge_requests_fn
617 * will have updated segment counts, update sector
620 if (!ll_merge_requests_fn(q
, req
, next
))
624 * If failfast settings disagree or any of the two is already
625 * a mixed merge, mark both as mixed before proceeding. This
626 * makes sure that all involved bios have mixable attributes
629 if ((req
->cmd_flags
| next
->cmd_flags
) & REQ_MIXED_MERGE
||
630 (req
->cmd_flags
& REQ_FAILFAST_MASK
) !=
631 (next
->cmd_flags
& REQ_FAILFAST_MASK
)) {
632 blk_rq_set_mixed_merge(req
);
633 blk_rq_set_mixed_merge(next
);
637 * At this point we have either done a back merge
638 * or front merge. We need the smaller start_time of
639 * the merged requests to be the current request
640 * for accounting purposes.
642 if (time_after(req
->start_time
, next
->start_time
))
643 req
->start_time
= next
->start_time
;
645 req
->biotail
->bi_next
= next
->bio
;
646 req
->biotail
= next
->biotail
;
648 req
->__data_len
+= blk_rq_bytes(next
);
650 elv_merge_requests(q
, req
, next
);
653 * 'next' is going away, so update stats accordingly
655 blk_account_io_merge(next
);
657 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
658 if (blk_rq_cpu_valid(next
))
659 req
->cpu
= next
->cpu
;
661 /* owner-ship of bio passed from next to req */
663 __blk_put_request(q
, next
);
667 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
669 struct request
*next
= elv_latter_request(q
, rq
);
672 return attempt_merge(q
, rq
, next
);
677 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
679 struct request
*prev
= elv_former_request(q
, rq
);
682 return attempt_merge(q
, prev
, rq
);
687 int blk_attempt_req_merge(struct request_queue
*q
, struct request
*rq
,
688 struct request
*next
)
690 return attempt_merge(q
, rq
, next
);
693 bool blk_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
695 struct request_queue
*q
= rq
->q
;
697 if (!rq_mergeable(rq
) || !bio_mergeable(bio
))
700 if (!blk_check_merge_flags(rq
->cmd_flags
, bio
->bi_rw
))
703 /* different data direction or already started, don't merge */
704 if (bio_data_dir(bio
) != rq_data_dir(rq
))
707 /* must be same device and not a special request */
708 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| req_no_special_merge(rq
))
711 /* only merge integrity protected bio into ditto rq */
712 if (blk_integrity_merge_bio(rq
->q
, rq
, bio
) == false)
715 /* must be using the same buffer */
716 if (rq
->cmd_flags
& REQ_WRITE_SAME
&&
717 !blk_write_same_mergeable(rq
->bio
, bio
))
720 /* Only check gaps if the bio carries data */
721 if (q
->queue_flags
& (1 << QUEUE_FLAG_SG_GAPS
) && bio_has_data(bio
)) {
722 struct bio_vec
*bprev
;
724 bprev
= &rq
->biotail
->bi_io_vec
[rq
->biotail
->bi_vcnt
- 1];
725 if (bvec_gap_to_prev(bprev
, bio
->bi_io_vec
[0].bv_offset
))
732 int blk_try_merge(struct request
*rq
, struct bio
*bio
)
734 if (blk_rq_pos(rq
) + blk_rq_sectors(rq
) == bio
->bi_iter
.bi_sector
)
735 return ELEVATOR_BACK_MERGE
;
736 else if (blk_rq_pos(rq
) - bio_sectors(bio
) == bio
->bi_iter
.bi_sector
)
737 return ELEVATOR_FRONT_MERGE
;
738 return ELEVATOR_NO_MERGE
;