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 bool no_sg_merge
= !!test_bit(QUEUE_FLAG_NO_SG_MERGE
,
102 bool merge_not_need
= bio
->bi_vcnt
< queue_max_segments(q
);
104 if (no_sg_merge
&& !bio_flagged(bio
, BIO_CLONED
) &&
106 bio
->bi_phys_segments
= bio
->bi_vcnt
;
108 struct bio
*nxt
= bio
->bi_next
;
111 bio
->bi_phys_segments
= __blk_recalc_rq_segments(q
, bio
,
112 no_sg_merge
&& merge_not_need
);
116 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
118 EXPORT_SYMBOL(blk_recount_segments
);
120 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
123 struct bio_vec end_bv
= { NULL
}, nxt_bv
;
124 struct bvec_iter iter
;
126 if (!blk_queue_cluster(q
))
129 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
130 queue_max_segment_size(q
))
133 if (!bio_has_data(bio
))
136 bio_for_each_segment(end_bv
, bio
, iter
)
137 if (end_bv
.bv_len
== iter
.bi_size
)
140 nxt_bv
= bio_iovec(nxt
);
142 if (!BIOVEC_PHYS_MERGEABLE(&end_bv
, &nxt_bv
))
146 * bio and nxt are contiguous in memory; check if the queue allows
147 * these two to be merged into one
149 if (BIOVEC_SEG_BOUNDARY(q
, &end_bv
, &nxt_bv
))
156 __blk_segment_map_sg(struct request_queue
*q
, struct bio_vec
*bvec
,
157 struct scatterlist
*sglist
, struct bio_vec
*bvprv
,
158 struct scatterlist
**sg
, int *nsegs
, int *cluster
)
161 int nbytes
= bvec
->bv_len
;
163 if (*sg
&& *cluster
) {
164 if ((*sg
)->length
+ nbytes
> queue_max_segment_size(q
))
167 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
169 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
172 (*sg
)->length
+= nbytes
;
179 * If the driver previously mapped a shorter
180 * list, we could see a termination bit
181 * prematurely unless it fully inits the sg
182 * table on each mapping. We KNOW that there
183 * must be more entries here or the driver
184 * would be buggy, so force clear the
185 * termination bit to avoid doing a full
186 * sg_init_table() in drivers for each command.
192 sg_set_page(*sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
198 static int __blk_bios_map_sg(struct request_queue
*q
, struct bio
*bio
,
199 struct scatterlist
*sglist
,
200 struct scatterlist
**sg
)
202 struct bio_vec bvec
, bvprv
= { NULL
};
203 struct bvec_iter iter
;
207 cluster
= blk_queue_cluster(q
);
209 if (bio
->bi_rw
& REQ_DISCARD
) {
211 * This is a hack - drivers should be neither modifying the
212 * biovec, nor relying on bi_vcnt - but because of
213 * blk_add_request_payload(), a discard bio may or may not have
214 * a payload we need to set up here (thank you Christoph) and
215 * bi_vcnt is really the only way of telling if we need to.
224 if (bio
->bi_rw
& REQ_WRITE_SAME
) {
227 bvec
= bio_iovec(bio
);
228 sg_set_page(*sg
, bvec
.bv_page
, bvec
.bv_len
, bvec
.bv_offset
);
233 bio_for_each_segment(bvec
, bio
, iter
)
234 __blk_segment_map_sg(q
, &bvec
, sglist
, &bvprv
, sg
,
241 * map a request to scatterlist, return number of sg entries setup. Caller
242 * must make sure sg can hold rq->nr_phys_segments entries
244 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
245 struct scatterlist
*sglist
)
247 struct scatterlist
*sg
= NULL
;
251 nsegs
= __blk_bios_map_sg(q
, rq
->bio
, sglist
, &sg
);
253 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
254 (blk_rq_bytes(rq
) & q
->dma_pad_mask
)) {
255 unsigned int pad_len
=
256 (q
->dma_pad_mask
& ~blk_rq_bytes(rq
)) + 1;
258 sg
->length
+= pad_len
;
259 rq
->extra_len
+= pad_len
;
262 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
263 if (rq
->cmd_flags
& REQ_WRITE
)
264 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
266 sg
->page_link
&= ~0x02;
268 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
270 ((unsigned long)q
->dma_drain_buffer
) &
273 rq
->extra_len
+= q
->dma_drain_size
;
281 EXPORT_SYMBOL(blk_rq_map_sg
);
284 * blk_bio_map_sg - map a bio to a scatterlist
285 * @q: request_queue in question
286 * @bio: bio being mapped
287 * @sglist: scatterlist being mapped
290 * Caller must make sure sg can hold bio->bi_phys_segments entries
292 * Will return the number of sg entries setup
294 int blk_bio_map_sg(struct request_queue
*q
, struct bio
*bio
,
295 struct scatterlist
*sglist
)
297 struct scatterlist
*sg
= NULL
;
299 struct bio
*next
= bio
->bi_next
;
302 nsegs
= __blk_bios_map_sg(q
, bio
, sglist
, &sg
);
307 BUG_ON(bio
->bi_phys_segments
&& nsegs
> bio
->bi_phys_segments
);
310 EXPORT_SYMBOL(blk_bio_map_sg
);
312 static inline int ll_new_hw_segment(struct request_queue
*q
,
316 int nr_phys_segs
= bio_phys_segments(q
, bio
);
318 if (req
->nr_phys_segments
+ nr_phys_segs
> queue_max_segments(q
))
321 if (blk_integrity_merge_bio(q
, req
, bio
) == false)
325 * This will form the start of a new hw segment. Bump both
328 req
->nr_phys_segments
+= nr_phys_segs
;
332 req
->cmd_flags
|= REQ_NOMERGE
;
333 if (req
== q
->last_merge
)
334 q
->last_merge
= NULL
;
338 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
341 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
342 blk_rq_get_max_sectors(req
)) {
343 req
->cmd_flags
|= REQ_NOMERGE
;
344 if (req
== q
->last_merge
)
345 q
->last_merge
= NULL
;
348 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
349 blk_recount_segments(q
, req
->biotail
);
350 if (!bio_flagged(bio
, BIO_SEG_VALID
))
351 blk_recount_segments(q
, bio
);
353 return ll_new_hw_segment(q
, req
, bio
);
356 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
359 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
360 blk_rq_get_max_sectors(req
)) {
361 req
->cmd_flags
|= REQ_NOMERGE
;
362 if (req
== q
->last_merge
)
363 q
->last_merge
= NULL
;
366 if (!bio_flagged(bio
, BIO_SEG_VALID
))
367 blk_recount_segments(q
, bio
);
368 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
369 blk_recount_segments(q
, req
->bio
);
371 return ll_new_hw_segment(q
, req
, bio
);
375 * blk-mq uses req->special to carry normal driver per-request payload, it
376 * does not indicate a prepared command that we cannot merge with.
378 static bool req_no_special_merge(struct request
*req
)
380 struct request_queue
*q
= req
->q
;
382 return !q
->mq_ops
&& req
->special
;
385 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
386 struct request
*next
)
388 int total_phys_segments
;
389 unsigned int seg_size
=
390 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
393 * First check if the either of the requests are re-queued
394 * requests. Can't merge them if they are.
396 if (req_no_special_merge(req
) || req_no_special_merge(next
))
400 * Will it become too large?
402 if ((blk_rq_sectors(req
) + blk_rq_sectors(next
)) >
403 blk_rq_get_max_sectors(req
))
406 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
407 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
408 if (req
->nr_phys_segments
== 1)
409 req
->bio
->bi_seg_front_size
= seg_size
;
410 if (next
->nr_phys_segments
== 1)
411 next
->biotail
->bi_seg_back_size
= seg_size
;
412 total_phys_segments
--;
415 if (total_phys_segments
> queue_max_segments(q
))
418 if (blk_integrity_merge_rq(q
, req
, next
) == false)
422 req
->nr_phys_segments
= total_phys_segments
;
427 * blk_rq_set_mixed_merge - mark a request as mixed merge
428 * @rq: request to mark as mixed merge
431 * @rq is about to be mixed merged. Make sure the attributes
432 * which can be mixed are set in each bio and mark @rq as mixed
435 void blk_rq_set_mixed_merge(struct request
*rq
)
437 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
440 if (rq
->cmd_flags
& REQ_MIXED_MERGE
)
444 * @rq will no longer represent mixable attributes for all the
445 * contained bios. It will just track those of the first one.
446 * Distributes the attributs to each bio.
448 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
449 WARN_ON_ONCE((bio
->bi_rw
& REQ_FAILFAST_MASK
) &&
450 (bio
->bi_rw
& REQ_FAILFAST_MASK
) != ff
);
453 rq
->cmd_flags
|= REQ_MIXED_MERGE
;
456 static void blk_account_io_merge(struct request
*req
)
458 if (blk_do_io_stat(req
)) {
459 struct hd_struct
*part
;
462 cpu
= part_stat_lock();
465 part_round_stats(cpu
, part
);
466 part_dec_in_flight(part
, rq_data_dir(req
));
474 * Has to be called with the request spinlock acquired
476 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
477 struct request
*next
)
479 if (!rq_mergeable(req
) || !rq_mergeable(next
))
482 if (!blk_check_merge_flags(req
->cmd_flags
, next
->cmd_flags
))
488 if (blk_rq_pos(req
) + blk_rq_sectors(req
) != blk_rq_pos(next
))
491 if (rq_data_dir(req
) != rq_data_dir(next
)
492 || req
->rq_disk
!= next
->rq_disk
493 || req_no_special_merge(next
))
496 if (req
->cmd_flags
& REQ_WRITE_SAME
&&
497 !blk_write_same_mergeable(req
->bio
, next
->bio
))
501 * If we are allowed to merge, then append bio list
502 * from next to rq and release next. merge_requests_fn
503 * will have updated segment counts, update sector
506 if (!ll_merge_requests_fn(q
, req
, next
))
510 * If failfast settings disagree or any of the two is already
511 * a mixed merge, mark both as mixed before proceeding. This
512 * makes sure that all involved bios have mixable attributes
515 if ((req
->cmd_flags
| next
->cmd_flags
) & REQ_MIXED_MERGE
||
516 (req
->cmd_flags
& REQ_FAILFAST_MASK
) !=
517 (next
->cmd_flags
& REQ_FAILFAST_MASK
)) {
518 blk_rq_set_mixed_merge(req
);
519 blk_rq_set_mixed_merge(next
);
523 * At this point we have either done a back merge
524 * or front merge. We need the smaller start_time of
525 * the merged requests to be the current request
526 * for accounting purposes.
528 if (time_after(req
->start_time
, next
->start_time
))
529 req
->start_time
= next
->start_time
;
531 req
->biotail
->bi_next
= next
->bio
;
532 req
->biotail
= next
->biotail
;
534 req
->__data_len
+= blk_rq_bytes(next
);
536 elv_merge_requests(q
, req
, next
);
539 * 'next' is going away, so update stats accordingly
541 blk_account_io_merge(next
);
543 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
544 if (blk_rq_cpu_valid(next
))
545 req
->cpu
= next
->cpu
;
547 /* owner-ship of bio passed from next to req */
549 __blk_put_request(q
, next
);
553 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
555 struct request
*next
= elv_latter_request(q
, rq
);
558 return attempt_merge(q
, rq
, next
);
563 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
565 struct request
*prev
= elv_former_request(q
, rq
);
568 return attempt_merge(q
, prev
, rq
);
573 int blk_attempt_req_merge(struct request_queue
*q
, struct request
*rq
,
574 struct request
*next
)
576 return attempt_merge(q
, rq
, next
);
579 bool blk_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
581 struct request_queue
*q
= rq
->q
;
583 if (!rq_mergeable(rq
) || !bio_mergeable(bio
))
586 if (!blk_check_merge_flags(rq
->cmd_flags
, bio
->bi_rw
))
589 /* different data direction or already started, don't merge */
590 if (bio_data_dir(bio
) != rq_data_dir(rq
))
593 /* must be same device and not a special request */
594 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| req_no_special_merge(rq
))
597 /* only merge integrity protected bio into ditto rq */
598 if (blk_integrity_merge_bio(rq
->q
, rq
, bio
) == false)
601 /* must be using the same buffer */
602 if (rq
->cmd_flags
& REQ_WRITE_SAME
&&
603 !blk_write_same_mergeable(rq
->bio
, bio
))
606 if (q
->queue_flags
& (1 << QUEUE_FLAG_SG_GAPS
)) {
607 struct bio_vec
*bprev
;
609 bprev
= &rq
->biotail
->bi_io_vec
[bio
->bi_vcnt
- 1];
610 if (bvec_gap_to_prev(bprev
, bio
->bi_io_vec
[0].bv_offset
))
617 int blk_try_merge(struct request
*rq
, struct bio
*bio
)
619 if (blk_rq_pos(rq
) + blk_rq_sectors(rq
) == bio
->bi_iter
.bi_sector
)
620 return ELEVATOR_BACK_MERGE
;
621 else if (blk_rq_pos(rq
) - bio_sectors(bio
) == bio
->bi_iter
.bi_sector
)
622 return ELEVATOR_FRONT_MERGE
;
623 return ELEVATOR_NO_MERGE
;
This page took 0.089774 seconds and 6 git commands to generate.