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
,
15 struct bio_vec bv
, bvprv
= { NULL
};
16 int cluster
, high
, highprv
= 1;
17 unsigned int seg_size
, nr_phys_segs
;
18 struct bio
*fbio
, *bbio
;
19 struct bvec_iter iter
;
25 * This should probably be returning 0, but blk_add_request_payload()
28 if (bio
->bi_rw
& REQ_DISCARD
)
31 if (bio
->bi_rw
& REQ_WRITE_SAME
)
35 cluster
= blk_queue_cluster(q
);
39 bio_for_each_segment(bv
, bio
, iter
) {
41 * the trick here is making sure that a high page is
42 * never considered part of another segment, since that
43 * might change with the bounce page.
45 high
= page_to_pfn(bv
.bv_page
) > queue_bounce_pfn(q
);
46 if (!high
&& !highprv
&& cluster
) {
47 if (seg_size
+ bv
.bv_len
48 > queue_max_segment_size(q
))
50 if (!BIOVEC_PHYS_MERGEABLE(&bvprv
, &bv
))
52 if (!BIOVEC_SEG_BOUNDARY(q
, &bvprv
, &bv
))
55 seg_size
+= bv
.bv_len
;
60 if (nr_phys_segs
== 1 && seg_size
>
61 fbio
->bi_seg_front_size
)
62 fbio
->bi_seg_front_size
= seg_size
;
72 if (nr_phys_segs
== 1 && seg_size
> fbio
->bi_seg_front_size
)
73 fbio
->bi_seg_front_size
= seg_size
;
74 if (seg_size
> bbio
->bi_seg_back_size
)
75 bbio
->bi_seg_back_size
= seg_size
;
80 void blk_recalc_rq_segments(struct request
*rq
)
82 rq
->nr_phys_segments
= __blk_recalc_rq_segments(rq
->q
, rq
->bio
);
85 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
87 struct bio
*nxt
= bio
->bi_next
;
90 bio
->bi_phys_segments
= __blk_recalc_rq_segments(q
, bio
);
92 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
94 EXPORT_SYMBOL(blk_recount_segments
);
96 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
99 struct bio_vec end_bv
= { NULL
}, nxt_bv
;
100 struct bvec_iter iter
;
102 if (!blk_queue_cluster(q
))
105 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
106 queue_max_segment_size(q
))
109 if (!bio_has_data(bio
))
112 bio_for_each_segment(end_bv
, bio
, iter
)
113 if (end_bv
.bv_len
== iter
.bi_size
)
116 nxt_bv
= bio_iovec(nxt
);
118 if (!BIOVEC_PHYS_MERGEABLE(&end_bv
, &nxt_bv
))
122 * bio and nxt are contiguous in memory; check if the queue allows
123 * these two to be merged into one
125 if (BIOVEC_SEG_BOUNDARY(q
, &end_bv
, &nxt_bv
))
132 __blk_segment_map_sg(struct request_queue
*q
, struct bio_vec
*bvec
,
133 struct scatterlist
*sglist
, struct bio_vec
*bvprv
,
134 struct scatterlist
**sg
, int *nsegs
, int *cluster
)
137 int nbytes
= bvec
->bv_len
;
139 if (*sg
&& *cluster
) {
140 if ((*sg
)->length
+ nbytes
> queue_max_segment_size(q
))
143 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
145 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
148 (*sg
)->length
+= nbytes
;
155 * If the driver previously mapped a shorter
156 * list, we could see a termination bit
157 * prematurely unless it fully inits the sg
158 * table on each mapping. We KNOW that there
159 * must be more entries here or the driver
160 * would be buggy, so force clear the
161 * termination bit to avoid doing a full
162 * sg_init_table() in drivers for each command.
168 sg_set_page(*sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
174 static int __blk_bios_map_sg(struct request_queue
*q
, struct bio
*bio
,
175 struct scatterlist
*sglist
,
176 struct scatterlist
**sg
)
178 struct bio_vec bvec
, bvprv
= { NULL
};
179 struct bvec_iter iter
;
183 cluster
= blk_queue_cluster(q
);
185 if (bio
->bi_rw
& REQ_DISCARD
) {
187 * This is a hack - drivers should be neither modifying the
188 * biovec, nor relying on bi_vcnt - but because of
189 * blk_add_request_payload(), a discard bio may or may not have
190 * a payload we need to set up here (thank you Christoph) and
191 * bi_vcnt is really the only way of telling if we need to.
200 if (bio
->bi_rw
& REQ_WRITE_SAME
) {
203 bvec
= bio_iovec(bio
);
204 sg_set_page(*sg
, bvec
.bv_page
, bvec
.bv_len
, bvec
.bv_offset
);
209 bio_for_each_segment(bvec
, bio
, iter
)
210 __blk_segment_map_sg(q
, &bvec
, sglist
, &bvprv
, sg
,
217 * map a request to scatterlist, return number of sg entries setup. Caller
218 * must make sure sg can hold rq->nr_phys_segments entries
220 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
221 struct scatterlist
*sglist
)
223 struct scatterlist
*sg
= NULL
;
227 nsegs
= __blk_bios_map_sg(q
, rq
->bio
, sglist
, &sg
);
229 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
230 (blk_rq_bytes(rq
) & q
->dma_pad_mask
)) {
231 unsigned int pad_len
=
232 (q
->dma_pad_mask
& ~blk_rq_bytes(rq
)) + 1;
234 sg
->length
+= pad_len
;
235 rq
->extra_len
+= pad_len
;
238 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
239 if (rq
->cmd_flags
& REQ_WRITE
)
240 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
242 sg
->page_link
&= ~0x02;
244 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
246 ((unsigned long)q
->dma_drain_buffer
) &
249 rq
->extra_len
+= q
->dma_drain_size
;
257 EXPORT_SYMBOL(blk_rq_map_sg
);
260 * blk_bio_map_sg - map a bio to a scatterlist
261 * @q: request_queue in question
262 * @bio: bio being mapped
263 * @sglist: scatterlist being mapped
266 * Caller must make sure sg can hold bio->bi_phys_segments entries
268 * Will return the number of sg entries setup
270 int blk_bio_map_sg(struct request_queue
*q
, struct bio
*bio
,
271 struct scatterlist
*sglist
)
273 struct scatterlist
*sg
= NULL
;
275 struct bio
*next
= bio
->bi_next
;
278 nsegs
= __blk_bios_map_sg(q
, bio
, sglist
, &sg
);
283 BUG_ON(bio
->bi_phys_segments
&& nsegs
> bio
->bi_phys_segments
);
286 EXPORT_SYMBOL(blk_bio_map_sg
);
288 static inline int ll_new_hw_segment(struct request_queue
*q
,
292 int nr_phys_segs
= bio_phys_segments(q
, bio
);
294 if (req
->nr_phys_segments
+ nr_phys_segs
> queue_max_segments(q
))
297 if (bio_integrity(bio
) && blk_integrity_merge_bio(q
, req
, bio
))
301 * This will form the start of a new hw segment. Bump both
304 req
->nr_phys_segments
+= nr_phys_segs
;
308 req
->cmd_flags
|= REQ_NOMERGE
;
309 if (req
== q
->last_merge
)
310 q
->last_merge
= NULL
;
314 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
317 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
318 blk_rq_get_max_sectors(req
)) {
319 req
->cmd_flags
|= REQ_NOMERGE
;
320 if (req
== q
->last_merge
)
321 q
->last_merge
= NULL
;
324 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
325 blk_recount_segments(q
, req
->biotail
);
326 if (!bio_flagged(bio
, BIO_SEG_VALID
))
327 blk_recount_segments(q
, bio
);
329 return ll_new_hw_segment(q
, req
, bio
);
332 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
335 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
336 blk_rq_get_max_sectors(req
)) {
337 req
->cmd_flags
|= REQ_NOMERGE
;
338 if (req
== q
->last_merge
)
339 q
->last_merge
= NULL
;
342 if (!bio_flagged(bio
, BIO_SEG_VALID
))
343 blk_recount_segments(q
, bio
);
344 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
345 blk_recount_segments(q
, req
->bio
);
347 return ll_new_hw_segment(q
, req
, bio
);
351 * blk-mq uses req->special to carry normal driver per-request payload, it
352 * does not indicate a prepared command that we cannot merge with.
354 static bool req_no_special_merge(struct request
*req
)
356 struct request_queue
*q
= req
->q
;
358 return !q
->mq_ops
&& req
->special
;
361 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
362 struct request
*next
)
364 int total_phys_segments
;
365 unsigned int seg_size
=
366 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
369 * First check if the either of the requests are re-queued
370 * requests. Can't merge them if they are.
372 if (req_no_special_merge(req
) || req_no_special_merge(next
))
376 * Will it become too large?
378 if ((blk_rq_sectors(req
) + blk_rq_sectors(next
)) >
379 blk_rq_get_max_sectors(req
))
382 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
383 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
384 if (req
->nr_phys_segments
== 1)
385 req
->bio
->bi_seg_front_size
= seg_size
;
386 if (next
->nr_phys_segments
== 1)
387 next
->biotail
->bi_seg_back_size
= seg_size
;
388 total_phys_segments
--;
391 if (total_phys_segments
> queue_max_segments(q
))
394 if (blk_integrity_rq(req
) && blk_integrity_merge_rq(q
, req
, next
))
398 req
->nr_phys_segments
= total_phys_segments
;
403 * blk_rq_set_mixed_merge - mark a request as mixed merge
404 * @rq: request to mark as mixed merge
407 * @rq is about to be mixed merged. Make sure the attributes
408 * which can be mixed are set in each bio and mark @rq as mixed
411 void blk_rq_set_mixed_merge(struct request
*rq
)
413 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
416 if (rq
->cmd_flags
& REQ_MIXED_MERGE
)
420 * @rq will no longer represent mixable attributes for all the
421 * contained bios. It will just track those of the first one.
422 * Distributes the attributs to each bio.
424 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
425 WARN_ON_ONCE((bio
->bi_rw
& REQ_FAILFAST_MASK
) &&
426 (bio
->bi_rw
& REQ_FAILFAST_MASK
) != ff
);
429 rq
->cmd_flags
|= REQ_MIXED_MERGE
;
432 static void blk_account_io_merge(struct request
*req
)
434 if (blk_do_io_stat(req
)) {
435 struct hd_struct
*part
;
438 cpu
= part_stat_lock();
441 part_round_stats(cpu
, part
);
442 part_dec_in_flight(part
, rq_data_dir(req
));
450 * Has to be called with the request spinlock acquired
452 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
453 struct request
*next
)
455 if (!rq_mergeable(req
) || !rq_mergeable(next
))
458 if (!blk_check_merge_flags(req
->cmd_flags
, next
->cmd_flags
))
464 if (blk_rq_pos(req
) + blk_rq_sectors(req
) != blk_rq_pos(next
))
467 if (rq_data_dir(req
) != rq_data_dir(next
)
468 || req
->rq_disk
!= next
->rq_disk
469 || req_no_special_merge(next
))
472 if (req
->cmd_flags
& REQ_WRITE_SAME
&&
473 !blk_write_same_mergeable(req
->bio
, next
->bio
))
477 * If we are allowed to merge, then append bio list
478 * from next to rq and release next. merge_requests_fn
479 * will have updated segment counts, update sector
482 if (!ll_merge_requests_fn(q
, req
, next
))
486 * If failfast settings disagree or any of the two is already
487 * a mixed merge, mark both as mixed before proceeding. This
488 * makes sure that all involved bios have mixable attributes
491 if ((req
->cmd_flags
| next
->cmd_flags
) & REQ_MIXED_MERGE
||
492 (req
->cmd_flags
& REQ_FAILFAST_MASK
) !=
493 (next
->cmd_flags
& REQ_FAILFAST_MASK
)) {
494 blk_rq_set_mixed_merge(req
);
495 blk_rq_set_mixed_merge(next
);
499 * At this point we have either done a back merge
500 * or front merge. We need the smaller start_time of
501 * the merged requests to be the current request
502 * for accounting purposes.
504 if (time_after(req
->start_time
, next
->start_time
))
505 req
->start_time
= next
->start_time
;
507 req
->biotail
->bi_next
= next
->bio
;
508 req
->biotail
= next
->biotail
;
510 req
->__data_len
+= blk_rq_bytes(next
);
512 elv_merge_requests(q
, req
, next
);
515 * 'next' is going away, so update stats accordingly
517 blk_account_io_merge(next
);
519 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
520 if (blk_rq_cpu_valid(next
))
521 req
->cpu
= next
->cpu
;
523 /* owner-ship of bio passed from next to req */
525 __blk_put_request(q
, next
);
529 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
531 struct request
*next
= elv_latter_request(q
, rq
);
534 return attempt_merge(q
, rq
, next
);
539 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
541 struct request
*prev
= elv_former_request(q
, rq
);
544 return attempt_merge(q
, prev
, rq
);
549 int blk_attempt_req_merge(struct request_queue
*q
, struct request
*rq
,
550 struct request
*next
)
552 return attempt_merge(q
, rq
, next
);
555 bool blk_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
557 if (!rq_mergeable(rq
) || !bio_mergeable(bio
))
560 if (!blk_check_merge_flags(rq
->cmd_flags
, bio
->bi_rw
))
563 /* different data direction or already started, don't merge */
564 if (bio_data_dir(bio
) != rq_data_dir(rq
))
567 /* must be same device and not a special request */
568 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| req_no_special_merge(rq
))
571 /* only merge integrity protected bio into ditto rq */
572 if (bio_integrity(bio
) != blk_integrity_rq(rq
))
575 /* must be using the same buffer */
576 if (rq
->cmd_flags
& REQ_WRITE_SAME
&&
577 !blk_write_same_mergeable(rq
->bio
, bio
))
583 int blk_try_merge(struct request
*rq
, struct bio
*bio
)
585 if (blk_rq_pos(rq
) + blk_rq_sectors(rq
) == bio
->bi_iter
.bi_sector
)
586 return ELEVATOR_BACK_MERGE
;
587 else if (blk_rq_pos(rq
) - bio_sectors(bio
) == bio
->bi_iter
.bi_sector
)
588 return ELEVATOR_FRONT_MERGE
;
589 return ELEVATOR_NO_MERGE
;