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 unsigned int __blk_recalc_rq_segments(struct request_queue
*q
,
16 struct bio_vec bv
, bvprv
= { NULL
};
17 int cluster
, high
, highprv
= 1;
18 unsigned int seg_size
, nr_phys_segs
;
19 struct bio
*fbio
, *bbio
;
20 struct bvec_iter iter
;
26 * This should probably be returning 0, but blk_add_request_payload()
29 if (bio
->bi_rw
& REQ_DISCARD
)
32 if (bio
->bi_rw
& REQ_WRITE_SAME
)
36 cluster
= blk_queue_cluster(q
);
41 bio_for_each_segment(bv
, bio
, iter
) {
43 * If SG merging is disabled, each bio vector is
50 * the trick here is making sure that a high page is
51 * never considered part of another segment, since
52 * that might change with the bounce page.
54 high
= page_to_pfn(bv
.bv_page
) > queue_bounce_pfn(q
);
55 if (!high
&& !highprv
&& cluster
) {
56 if (seg_size
+ bv
.bv_len
57 > queue_max_segment_size(q
))
59 if (!BIOVEC_PHYS_MERGEABLE(&bvprv
, &bv
))
61 if (!BIOVEC_SEG_BOUNDARY(q
, &bvprv
, &bv
))
64 seg_size
+= bv
.bv_len
;
69 if (nr_phys_segs
== 1 && seg_size
>
70 fbio
->bi_seg_front_size
)
71 fbio
->bi_seg_front_size
= seg_size
;
81 if (nr_phys_segs
== 1 && seg_size
> fbio
->bi_seg_front_size
)
82 fbio
->bi_seg_front_size
= seg_size
;
83 if (seg_size
> bbio
->bi_seg_back_size
)
84 bbio
->bi_seg_back_size
= seg_size
;
89 void blk_recalc_rq_segments(struct request
*rq
)
91 bool no_sg_merge
= !!test_bit(QUEUE_FLAG_NO_SG_MERGE
,
94 rq
->nr_phys_segments
= __blk_recalc_rq_segments(rq
->q
, rq
->bio
,
98 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
100 unsigned short seg_cnt
;
102 /* estimate segment number by bi_vcnt for non-cloned bio */
103 if (bio_flagged(bio
, BIO_CLONED
))
104 seg_cnt
= bio_segments(bio
);
106 seg_cnt
= bio
->bi_vcnt
;
108 if (test_bit(QUEUE_FLAG_NO_SG_MERGE
, &q
->queue_flags
) &&
109 (seg_cnt
< queue_max_segments(q
)))
110 bio
->bi_phys_segments
= seg_cnt
;
112 struct bio
*nxt
= bio
->bi_next
;
115 bio
->bi_phys_segments
= __blk_recalc_rq_segments(q
, bio
, false);
119 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
121 EXPORT_SYMBOL(blk_recount_segments
);
123 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
126 struct bio_vec end_bv
= { NULL
}, nxt_bv
;
127 struct bvec_iter iter
;
129 if (!blk_queue_cluster(q
))
132 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
133 queue_max_segment_size(q
))
136 if (!bio_has_data(bio
))
139 bio_for_each_segment(end_bv
, bio
, iter
)
140 if (end_bv
.bv_len
== iter
.bi_size
)
143 nxt_bv
= bio_iovec(nxt
);
145 if (!BIOVEC_PHYS_MERGEABLE(&end_bv
, &nxt_bv
))
149 * bio and nxt are contiguous in memory; check if the queue allows
150 * these two to be merged into one
152 if (BIOVEC_SEG_BOUNDARY(q
, &end_bv
, &nxt_bv
))
159 __blk_segment_map_sg(struct request_queue
*q
, struct bio_vec
*bvec
,
160 struct scatterlist
*sglist
, struct bio_vec
*bvprv
,
161 struct scatterlist
**sg
, int *nsegs
, int *cluster
)
164 int nbytes
= bvec
->bv_len
;
166 if (*sg
&& *cluster
) {
167 if ((*sg
)->length
+ nbytes
> queue_max_segment_size(q
))
170 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
172 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
175 (*sg
)->length
+= nbytes
;
182 * If the driver previously mapped a shorter
183 * list, we could see a termination bit
184 * prematurely unless it fully inits the sg
185 * table on each mapping. We KNOW that there
186 * must be more entries here or the driver
187 * would be buggy, so force clear the
188 * termination bit to avoid doing a full
189 * sg_init_table() in drivers for each command.
195 sg_set_page(*sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
201 static int __blk_bios_map_sg(struct request_queue
*q
, struct bio
*bio
,
202 struct scatterlist
*sglist
,
203 struct scatterlist
**sg
)
205 struct bio_vec bvec
, bvprv
= { NULL
};
206 struct bvec_iter iter
;
210 cluster
= blk_queue_cluster(q
);
212 if (bio
->bi_rw
& REQ_DISCARD
) {
214 * This is a hack - drivers should be neither modifying the
215 * biovec, nor relying on bi_vcnt - but because of
216 * blk_add_request_payload(), a discard bio may or may not have
217 * a payload we need to set up here (thank you Christoph) and
218 * bi_vcnt is really the only way of telling if we need to.
227 if (bio
->bi_rw
& REQ_WRITE_SAME
) {
230 bvec
= bio_iovec(bio
);
231 sg_set_page(*sg
, bvec
.bv_page
, bvec
.bv_len
, bvec
.bv_offset
);
236 bio_for_each_segment(bvec
, bio
, iter
)
237 __blk_segment_map_sg(q
, &bvec
, sglist
, &bvprv
, sg
,
244 * map a request to scatterlist, return number of sg entries setup. Caller
245 * must make sure sg can hold rq->nr_phys_segments entries
247 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
248 struct scatterlist
*sglist
)
250 struct scatterlist
*sg
= NULL
;
254 nsegs
= __blk_bios_map_sg(q
, rq
->bio
, sglist
, &sg
);
256 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
257 (blk_rq_bytes(rq
) & q
->dma_pad_mask
)) {
258 unsigned int pad_len
=
259 (q
->dma_pad_mask
& ~blk_rq_bytes(rq
)) + 1;
261 sg
->length
+= pad_len
;
262 rq
->extra_len
+= pad_len
;
265 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
266 if (rq
->cmd_flags
& REQ_WRITE
)
267 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
269 sg
->page_link
&= ~0x02;
271 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
273 ((unsigned long)q
->dma_drain_buffer
) &
276 rq
->extra_len
+= q
->dma_drain_size
;
284 EXPORT_SYMBOL(blk_rq_map_sg
);
286 static inline int ll_new_hw_segment(struct request_queue
*q
,
290 int nr_phys_segs
= bio_phys_segments(q
, bio
);
292 if (req
->nr_phys_segments
+ nr_phys_segs
> queue_max_segments(q
))
295 if (blk_integrity_merge_bio(q
, req
, bio
) == false)
299 * This will form the start of a new hw segment. Bump both
302 req
->nr_phys_segments
+= nr_phys_segs
;
306 req
->cmd_flags
|= REQ_NOMERGE
;
307 if (req
== q
->last_merge
)
308 q
->last_merge
= NULL
;
312 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
315 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
316 blk_rq_get_max_sectors(req
)) {
317 req
->cmd_flags
|= REQ_NOMERGE
;
318 if (req
== q
->last_merge
)
319 q
->last_merge
= NULL
;
322 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
323 blk_recount_segments(q
, req
->biotail
);
324 if (!bio_flagged(bio
, BIO_SEG_VALID
))
325 blk_recount_segments(q
, bio
);
327 return ll_new_hw_segment(q
, req
, bio
);
330 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
333 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
334 blk_rq_get_max_sectors(req
)) {
335 req
->cmd_flags
|= REQ_NOMERGE
;
336 if (req
== q
->last_merge
)
337 q
->last_merge
= NULL
;
340 if (!bio_flagged(bio
, BIO_SEG_VALID
))
341 blk_recount_segments(q
, bio
);
342 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
343 blk_recount_segments(q
, req
->bio
);
345 return ll_new_hw_segment(q
, req
, bio
);
349 * blk-mq uses req->special to carry normal driver per-request payload, it
350 * does not indicate a prepared command that we cannot merge with.
352 static bool req_no_special_merge(struct request
*req
)
354 struct request_queue
*q
= req
->q
;
356 return !q
->mq_ops
&& req
->special
;
359 static int req_gap_to_prev(struct request
*req
, struct request
*next
)
361 struct bio
*prev
= req
->biotail
;
363 return bvec_gap_to_prev(&prev
->bi_io_vec
[prev
->bi_vcnt
- 1],
364 next
->bio
->bi_io_vec
[0].bv_offset
);
367 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
368 struct request
*next
)
370 int total_phys_segments
;
371 unsigned int seg_size
=
372 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
375 * First check if the either of the requests are re-queued
376 * requests. Can't merge them if they are.
378 if (req_no_special_merge(req
) || req_no_special_merge(next
))
381 if (test_bit(QUEUE_FLAG_SG_GAPS
, &q
->queue_flags
) &&
382 req_gap_to_prev(req
, next
))
386 * Will it become too large?
388 if ((blk_rq_sectors(req
) + blk_rq_sectors(next
)) >
389 blk_rq_get_max_sectors(req
))
392 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
393 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
394 if (req
->nr_phys_segments
== 1)
395 req
->bio
->bi_seg_front_size
= seg_size
;
396 if (next
->nr_phys_segments
== 1)
397 next
->biotail
->bi_seg_back_size
= seg_size
;
398 total_phys_segments
--;
401 if (total_phys_segments
> queue_max_segments(q
))
404 if (blk_integrity_merge_rq(q
, req
, next
) == false)
408 req
->nr_phys_segments
= total_phys_segments
;
413 * blk_rq_set_mixed_merge - mark a request as mixed merge
414 * @rq: request to mark as mixed merge
417 * @rq is about to be mixed merged. Make sure the attributes
418 * which can be mixed are set in each bio and mark @rq as mixed
421 void blk_rq_set_mixed_merge(struct request
*rq
)
423 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
426 if (rq
->cmd_flags
& REQ_MIXED_MERGE
)
430 * @rq will no longer represent mixable attributes for all the
431 * contained bios. It will just track those of the first one.
432 * Distributes the attributs to each bio.
434 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
435 WARN_ON_ONCE((bio
->bi_rw
& REQ_FAILFAST_MASK
) &&
436 (bio
->bi_rw
& REQ_FAILFAST_MASK
) != ff
);
439 rq
->cmd_flags
|= REQ_MIXED_MERGE
;
442 static void blk_account_io_merge(struct request
*req
)
444 if (blk_do_io_stat(req
)) {
445 struct hd_struct
*part
;
448 cpu
= part_stat_lock();
451 part_round_stats(cpu
, part
);
452 part_dec_in_flight(part
, rq_data_dir(req
));
460 * Has to be called with the request spinlock acquired
462 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
463 struct request
*next
)
465 if (!rq_mergeable(req
) || !rq_mergeable(next
))
468 if (!blk_check_merge_flags(req
->cmd_flags
, next
->cmd_flags
))
474 if (blk_rq_pos(req
) + blk_rq_sectors(req
) != blk_rq_pos(next
))
477 if (rq_data_dir(req
) != rq_data_dir(next
)
478 || req
->rq_disk
!= next
->rq_disk
479 || req_no_special_merge(next
))
482 if (req
->cmd_flags
& REQ_WRITE_SAME
&&
483 !blk_write_same_mergeable(req
->bio
, next
->bio
))
487 * If we are allowed to merge, then append bio list
488 * from next to rq and release next. merge_requests_fn
489 * will have updated segment counts, update sector
492 if (!ll_merge_requests_fn(q
, req
, next
))
496 * If failfast settings disagree or any of the two is already
497 * a mixed merge, mark both as mixed before proceeding. This
498 * makes sure that all involved bios have mixable attributes
501 if ((req
->cmd_flags
| next
->cmd_flags
) & REQ_MIXED_MERGE
||
502 (req
->cmd_flags
& REQ_FAILFAST_MASK
) !=
503 (next
->cmd_flags
& REQ_FAILFAST_MASK
)) {
504 blk_rq_set_mixed_merge(req
);
505 blk_rq_set_mixed_merge(next
);
509 * At this point we have either done a back merge
510 * or front merge. We need the smaller start_time of
511 * the merged requests to be the current request
512 * for accounting purposes.
514 if (time_after(req
->start_time
, next
->start_time
))
515 req
->start_time
= next
->start_time
;
517 req
->biotail
->bi_next
= next
->bio
;
518 req
->biotail
= next
->biotail
;
520 req
->__data_len
+= blk_rq_bytes(next
);
522 elv_merge_requests(q
, req
, next
);
525 * 'next' is going away, so update stats accordingly
527 blk_account_io_merge(next
);
529 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
530 if (blk_rq_cpu_valid(next
))
531 req
->cpu
= next
->cpu
;
533 /* owner-ship of bio passed from next to req */
535 __blk_put_request(q
, next
);
539 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
541 struct request
*next
= elv_latter_request(q
, rq
);
544 return attempt_merge(q
, rq
, next
);
549 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
551 struct request
*prev
= elv_former_request(q
, rq
);
554 return attempt_merge(q
, prev
, rq
);
559 int blk_attempt_req_merge(struct request_queue
*q
, struct request
*rq
,
560 struct request
*next
)
562 return attempt_merge(q
, rq
, next
);
565 bool blk_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
567 struct request_queue
*q
= rq
->q
;
569 if (!rq_mergeable(rq
) || !bio_mergeable(bio
))
572 if (!blk_check_merge_flags(rq
->cmd_flags
, bio
->bi_rw
))
575 /* different data direction or already started, don't merge */
576 if (bio_data_dir(bio
) != rq_data_dir(rq
))
579 /* must be same device and not a special request */
580 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| req_no_special_merge(rq
))
583 /* only merge integrity protected bio into ditto rq */
584 if (blk_integrity_merge_bio(rq
->q
, rq
, bio
) == false)
587 /* must be using the same buffer */
588 if (rq
->cmd_flags
& REQ_WRITE_SAME
&&
589 !blk_write_same_mergeable(rq
->bio
, bio
))
592 if (q
->queue_flags
& (1 << QUEUE_FLAG_SG_GAPS
)) {
593 struct bio_vec
*bprev
;
595 bprev
= &rq
->biotail
->bi_io_vec
[bio
->bi_vcnt
- 1];
596 if (bvec_gap_to_prev(bprev
, bio
->bi_io_vec
[0].bv_offset
))
603 int blk_try_merge(struct request
*rq
, struct bio
*bio
)
605 if (blk_rq_pos(rq
) + blk_rq_sectors(rq
) == bio
->bi_iter
.bi_sector
)
606 return ELEVATOR_BACK_MERGE
;
607 else if (blk_rq_pos(rq
) - bio_sectors(bio
) == bio
->bi_iter
.bi_sector
)
608 return ELEVATOR_FRONT_MERGE
;
609 return ELEVATOR_NO_MERGE
;