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 (prev
&& bvec_gap_to_prev(q
, &bvprv
, bv
.bv_offset
))
88 if (prev
&& blk_queue_cluster(q
)) {
89 if (seg_size
+ bv
.bv_len
> queue_max_segment_size(q
))
91 if (!BIOVEC_PHYS_MERGEABLE(&bvprv
, &bv
))
93 if (!BIOVEC_SEG_BOUNDARY(q
, &bvprv
, &bv
))
96 seg_size
+= bv
.bv_len
;
102 if (nsegs
== queue_max_segments(q
))
108 seg_size
= bv
.bv_len
;
113 split
= bio_clone_bioset(bio
, GFP_NOIO
, bs
);
115 split
->bi_iter
.bi_size
-= iter
.bi_size
;
118 if (bio_integrity(bio
)) {
119 bio_integrity_advance(bio
, split
->bi_iter
.bi_size
);
120 bio_integrity_trim(split
, 0, bio_sectors(split
));
126 void blk_queue_split(struct request_queue
*q
, struct bio
**bio
,
131 if ((*bio
)->bi_rw
& REQ_DISCARD
)
132 split
= blk_bio_discard_split(q
, *bio
, bs
);
133 else if ((*bio
)->bi_rw
& REQ_WRITE_SAME
)
134 split
= blk_bio_write_same_split(q
, *bio
, bs
);
136 split
= blk_bio_segment_split(q
, *bio
, q
->bio_split
);
139 bio_chain(split
, *bio
);
140 generic_make_request(*bio
);
144 EXPORT_SYMBOL(blk_queue_split
);
146 static unsigned int __blk_recalc_rq_segments(struct request_queue
*q
,
150 struct bio_vec bv
, bvprv
= { NULL
};
151 int cluster
, prev
= 0;
152 unsigned int seg_size
, nr_phys_segs
;
153 struct bio
*fbio
, *bbio
;
154 struct bvec_iter iter
;
160 * This should probably be returning 0, but blk_add_request_payload()
163 if (bio
->bi_rw
& REQ_DISCARD
)
166 if (bio
->bi_rw
& REQ_WRITE_SAME
)
170 cluster
= blk_queue_cluster(q
);
174 bio_for_each_segment(bv
, bio
, iter
) {
176 * If SG merging is disabled, each bio vector is
182 if (prev
&& cluster
) {
183 if (seg_size
+ bv
.bv_len
184 > queue_max_segment_size(q
))
186 if (!BIOVEC_PHYS_MERGEABLE(&bvprv
, &bv
))
188 if (!BIOVEC_SEG_BOUNDARY(q
, &bvprv
, &bv
))
191 seg_size
+= bv
.bv_len
;
196 if (nr_phys_segs
== 1 && seg_size
>
197 fbio
->bi_seg_front_size
)
198 fbio
->bi_seg_front_size
= seg_size
;
203 seg_size
= bv
.bv_len
;
208 if (nr_phys_segs
== 1 && seg_size
> fbio
->bi_seg_front_size
)
209 fbio
->bi_seg_front_size
= seg_size
;
210 if (seg_size
> bbio
->bi_seg_back_size
)
211 bbio
->bi_seg_back_size
= seg_size
;
216 void blk_recalc_rq_segments(struct request
*rq
)
218 bool no_sg_merge
= !!test_bit(QUEUE_FLAG_NO_SG_MERGE
,
219 &rq
->q
->queue_flags
);
221 rq
->nr_phys_segments
= __blk_recalc_rq_segments(rq
->q
, rq
->bio
,
225 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
227 unsigned short seg_cnt
;
229 /* estimate segment number by bi_vcnt for non-cloned bio */
230 if (bio_flagged(bio
, BIO_CLONED
))
231 seg_cnt
= bio_segments(bio
);
233 seg_cnt
= bio
->bi_vcnt
;
235 if (test_bit(QUEUE_FLAG_NO_SG_MERGE
, &q
->queue_flags
) &&
236 (seg_cnt
< queue_max_segments(q
)))
237 bio
->bi_phys_segments
= seg_cnt
;
239 struct bio
*nxt
= bio
->bi_next
;
242 bio
->bi_phys_segments
= __blk_recalc_rq_segments(q
, bio
, false);
246 bio_set_flag(bio
, BIO_SEG_VALID
);
248 EXPORT_SYMBOL(blk_recount_segments
);
250 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
253 struct bio_vec end_bv
= { NULL
}, nxt_bv
;
254 struct bvec_iter iter
;
256 if (!blk_queue_cluster(q
))
259 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
260 queue_max_segment_size(q
))
263 if (!bio_has_data(bio
))
266 bio_for_each_segment(end_bv
, bio
, iter
)
267 if (end_bv
.bv_len
== iter
.bi_size
)
270 nxt_bv
= bio_iovec(nxt
);
272 if (!BIOVEC_PHYS_MERGEABLE(&end_bv
, &nxt_bv
))
276 * bio and nxt are contiguous in memory; check if the queue allows
277 * these two to be merged into one
279 if (BIOVEC_SEG_BOUNDARY(q
, &end_bv
, &nxt_bv
))
286 __blk_segment_map_sg(struct request_queue
*q
, struct bio_vec
*bvec
,
287 struct scatterlist
*sglist
, struct bio_vec
*bvprv
,
288 struct scatterlist
**sg
, int *nsegs
, int *cluster
)
291 int nbytes
= bvec
->bv_len
;
293 if (*sg
&& *cluster
) {
294 if ((*sg
)->length
+ nbytes
> queue_max_segment_size(q
))
297 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
299 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
302 (*sg
)->length
+= nbytes
;
309 * If the driver previously mapped a shorter
310 * list, we could see a termination bit
311 * prematurely unless it fully inits the sg
312 * table on each mapping. We KNOW that there
313 * must be more entries here or the driver
314 * would be buggy, so force clear the
315 * termination bit to avoid doing a full
316 * sg_init_table() in drivers for each command.
322 sg_set_page(*sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
328 static int __blk_bios_map_sg(struct request_queue
*q
, struct bio
*bio
,
329 struct scatterlist
*sglist
,
330 struct scatterlist
**sg
)
332 struct bio_vec bvec
, bvprv
= { NULL
};
333 struct bvec_iter iter
;
337 cluster
= blk_queue_cluster(q
);
339 if (bio
->bi_rw
& REQ_DISCARD
) {
341 * This is a hack - drivers should be neither modifying the
342 * biovec, nor relying on bi_vcnt - but because of
343 * blk_add_request_payload(), a discard bio may or may not have
344 * a payload we need to set up here (thank you Christoph) and
345 * bi_vcnt is really the only way of telling if we need to.
354 if (bio
->bi_rw
& REQ_WRITE_SAME
) {
357 bvec
= bio_iovec(bio
);
358 sg_set_page(*sg
, bvec
.bv_page
, bvec
.bv_len
, bvec
.bv_offset
);
363 bio_for_each_segment(bvec
, bio
, iter
)
364 __blk_segment_map_sg(q
, &bvec
, sglist
, &bvprv
, sg
,
371 * map a request to scatterlist, return number of sg entries setup. Caller
372 * must make sure sg can hold rq->nr_phys_segments entries
374 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
375 struct scatterlist
*sglist
)
377 struct scatterlist
*sg
= NULL
;
381 nsegs
= __blk_bios_map_sg(q
, rq
->bio
, sglist
, &sg
);
383 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
384 (blk_rq_bytes(rq
) & q
->dma_pad_mask
)) {
385 unsigned int pad_len
=
386 (q
->dma_pad_mask
& ~blk_rq_bytes(rq
)) + 1;
388 sg
->length
+= pad_len
;
389 rq
->extra_len
+= pad_len
;
392 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
393 if (rq
->cmd_flags
& REQ_WRITE
)
394 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
398 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
400 ((unsigned long)q
->dma_drain_buffer
) &
403 rq
->extra_len
+= q
->dma_drain_size
;
411 EXPORT_SYMBOL(blk_rq_map_sg
);
413 static inline int ll_new_hw_segment(struct request_queue
*q
,
417 int nr_phys_segs
= bio_phys_segments(q
, bio
);
419 if (req
->nr_phys_segments
+ nr_phys_segs
> queue_max_segments(q
))
422 if (blk_integrity_merge_bio(q
, req
, bio
) == false)
426 * This will form the start of a new hw segment. Bump both
429 req
->nr_phys_segments
+= nr_phys_segs
;
433 req
->cmd_flags
|= REQ_NOMERGE
;
434 if (req
== q
->last_merge
)
435 q
->last_merge
= NULL
;
439 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
442 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
443 blk_rq_get_max_sectors(req
)) {
444 req
->cmd_flags
|= REQ_NOMERGE
;
445 if (req
== q
->last_merge
)
446 q
->last_merge
= NULL
;
449 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
450 blk_recount_segments(q
, req
->biotail
);
451 if (!bio_flagged(bio
, BIO_SEG_VALID
))
452 blk_recount_segments(q
, bio
);
454 return ll_new_hw_segment(q
, req
, bio
);
457 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
460 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
461 blk_rq_get_max_sectors(req
)) {
462 req
->cmd_flags
|= REQ_NOMERGE
;
463 if (req
== q
->last_merge
)
464 q
->last_merge
= NULL
;
467 if (!bio_flagged(bio
, BIO_SEG_VALID
))
468 blk_recount_segments(q
, bio
);
469 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
470 blk_recount_segments(q
, req
->bio
);
472 return ll_new_hw_segment(q
, req
, bio
);
476 * blk-mq uses req->special to carry normal driver per-request payload, it
477 * does not indicate a prepared command that we cannot merge with.
479 static bool req_no_special_merge(struct request
*req
)
481 struct request_queue
*q
= req
->q
;
483 return !q
->mq_ops
&& req
->special
;
486 static int req_gap_to_prev(struct request
*req
, struct bio
*next
)
488 struct bio
*prev
= req
->biotail
;
490 return bvec_gap_to_prev(req
->q
, &prev
->bi_io_vec
[prev
->bi_vcnt
- 1],
491 next
->bi_io_vec
[0].bv_offset
);
494 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
495 struct request
*next
)
497 int total_phys_segments
;
498 unsigned int seg_size
=
499 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
502 * First check if the either of the requests are re-queued
503 * requests. Can't merge them if they are.
505 if (req_no_special_merge(req
) || req_no_special_merge(next
))
508 if (req_gap_to_prev(req
, next
->bio
))
512 * Will it become too large?
514 if ((blk_rq_sectors(req
) + blk_rq_sectors(next
)) >
515 blk_rq_get_max_sectors(req
))
518 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
519 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
520 if (req
->nr_phys_segments
== 1)
521 req
->bio
->bi_seg_front_size
= seg_size
;
522 if (next
->nr_phys_segments
== 1)
523 next
->biotail
->bi_seg_back_size
= seg_size
;
524 total_phys_segments
--;
527 if (total_phys_segments
> queue_max_segments(q
))
530 if (blk_integrity_merge_rq(q
, req
, next
) == false)
534 req
->nr_phys_segments
= total_phys_segments
;
539 * blk_rq_set_mixed_merge - mark a request as mixed merge
540 * @rq: request to mark as mixed merge
543 * @rq is about to be mixed merged. Make sure the attributes
544 * which can be mixed are set in each bio and mark @rq as mixed
547 void blk_rq_set_mixed_merge(struct request
*rq
)
549 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
552 if (rq
->cmd_flags
& REQ_MIXED_MERGE
)
556 * @rq will no longer represent mixable attributes for all the
557 * contained bios. It will just track those of the first one.
558 * Distributes the attributs to each bio.
560 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
561 WARN_ON_ONCE((bio
->bi_rw
& REQ_FAILFAST_MASK
) &&
562 (bio
->bi_rw
& REQ_FAILFAST_MASK
) != ff
);
565 rq
->cmd_flags
|= REQ_MIXED_MERGE
;
568 static void blk_account_io_merge(struct request
*req
)
570 if (blk_do_io_stat(req
)) {
571 struct hd_struct
*part
;
574 cpu
= part_stat_lock();
577 part_round_stats(cpu
, part
);
578 part_dec_in_flight(part
, rq_data_dir(req
));
586 * Has to be called with the request spinlock acquired
588 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
589 struct request
*next
)
591 if (!rq_mergeable(req
) || !rq_mergeable(next
))
594 if (!blk_check_merge_flags(req
->cmd_flags
, next
->cmd_flags
))
600 if (blk_rq_pos(req
) + blk_rq_sectors(req
) != blk_rq_pos(next
))
603 if (rq_data_dir(req
) != rq_data_dir(next
)
604 || req
->rq_disk
!= next
->rq_disk
605 || req_no_special_merge(next
))
608 if (req
->cmd_flags
& REQ_WRITE_SAME
&&
609 !blk_write_same_mergeable(req
->bio
, next
->bio
))
613 * If we are allowed to merge, then append bio list
614 * from next to rq and release next. merge_requests_fn
615 * will have updated segment counts, update sector
618 if (!ll_merge_requests_fn(q
, req
, next
))
622 * If failfast settings disagree or any of the two is already
623 * a mixed merge, mark both as mixed before proceeding. This
624 * makes sure that all involved bios have mixable attributes
627 if ((req
->cmd_flags
| next
->cmd_flags
) & REQ_MIXED_MERGE
||
628 (req
->cmd_flags
& REQ_FAILFAST_MASK
) !=
629 (next
->cmd_flags
& REQ_FAILFAST_MASK
)) {
630 blk_rq_set_mixed_merge(req
);
631 blk_rq_set_mixed_merge(next
);
635 * At this point we have either done a back merge
636 * or front merge. We need the smaller start_time of
637 * the merged requests to be the current request
638 * for accounting purposes.
640 if (time_after(req
->start_time
, next
->start_time
))
641 req
->start_time
= next
->start_time
;
643 req
->biotail
->bi_next
= next
->bio
;
644 req
->biotail
= next
->biotail
;
646 req
->__data_len
+= blk_rq_bytes(next
);
648 elv_merge_requests(q
, req
, next
);
651 * 'next' is going away, so update stats accordingly
653 blk_account_io_merge(next
);
655 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
656 if (blk_rq_cpu_valid(next
))
657 req
->cpu
= next
->cpu
;
659 /* owner-ship of bio passed from next to req */
661 __blk_put_request(q
, next
);
665 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
667 struct request
*next
= elv_latter_request(q
, rq
);
670 return attempt_merge(q
, rq
, next
);
675 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
677 struct request
*prev
= elv_former_request(q
, rq
);
680 return attempt_merge(q
, prev
, rq
);
685 int blk_attempt_req_merge(struct request_queue
*q
, struct request
*rq
,
686 struct request
*next
)
688 return attempt_merge(q
, rq
, next
);
691 bool blk_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
693 if (!rq_mergeable(rq
) || !bio_mergeable(bio
))
696 if (!blk_check_merge_flags(rq
->cmd_flags
, bio
->bi_rw
))
699 /* different data direction or already started, don't merge */
700 if (bio_data_dir(bio
) != rq_data_dir(rq
))
703 /* must be same device and not a special request */
704 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| req_no_special_merge(rq
))
707 /* only merge integrity protected bio into ditto rq */
708 if (blk_integrity_merge_bio(rq
->q
, rq
, bio
) == false)
711 /* must be using the same buffer */
712 if (rq
->cmd_flags
& REQ_WRITE_SAME
&&
713 !blk_write_same_mergeable(rq
->bio
, bio
))
716 /* Only check gaps if the bio carries data */
717 if (bio_has_data(bio
) && req_gap_to_prev(rq
, bio
))
723 int blk_try_merge(struct request
*rq
, struct bio
*bio
)
725 if (blk_rq_pos(rq
) + blk_rq_sectors(rq
) == bio
->bi_iter
.bi_sector
)
726 return ELEVATOR_BACK_MERGE
;
727 else if (blk_rq_pos(rq
) - bio_sectors(bio
) == bio
->bi_iter
.bi_sector
)
728 return ELEVATOR_FRONT_MERGE
;
729 return ELEVATOR_NO_MERGE
;