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 cluster
= blk_queue_cluster(q
);
29 bio_for_each_segment(bv
, bio
, iter
) {
31 * the trick here is making sure that a high page is
32 * never considered part of another segment, since that
33 * might change with the bounce page.
35 high
= page_to_pfn(bv
.bv_page
) > queue_bounce_pfn(q
);
36 if (!high
&& !highprv
&& cluster
) {
37 if (seg_size
+ bv
.bv_len
38 > queue_max_segment_size(q
))
40 if (!BIOVEC_PHYS_MERGEABLE(&bvprv
, &bv
))
42 if (!BIOVEC_SEG_BOUNDARY(q
, &bvprv
, &bv
))
45 seg_size
+= bv
.bv_len
;
50 if (nr_phys_segs
== 1 && seg_size
>
51 fbio
->bi_seg_front_size
)
52 fbio
->bi_seg_front_size
= seg_size
;
62 if (nr_phys_segs
== 1 && seg_size
> fbio
->bi_seg_front_size
)
63 fbio
->bi_seg_front_size
= seg_size
;
64 if (seg_size
> bbio
->bi_seg_back_size
)
65 bbio
->bi_seg_back_size
= seg_size
;
70 void blk_recalc_rq_segments(struct request
*rq
)
72 rq
->nr_phys_segments
= __blk_recalc_rq_segments(rq
->q
, rq
->bio
);
75 void blk_recount_segments(struct request_queue
*q
, struct bio
*bio
)
77 struct bio
*nxt
= bio
->bi_next
;
80 bio
->bi_phys_segments
= __blk_recalc_rq_segments(q
, bio
);
82 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
84 EXPORT_SYMBOL(blk_recount_segments
);
86 static int blk_phys_contig_segment(struct request_queue
*q
, struct bio
*bio
,
89 struct bio_vec end_bv
= { NULL
}, nxt_bv
;
90 struct bvec_iter iter
;
92 if (!blk_queue_cluster(q
))
95 if (bio
->bi_seg_back_size
+ nxt
->bi_seg_front_size
>
96 queue_max_segment_size(q
))
99 if (!bio_has_data(bio
))
102 bio_for_each_segment(end_bv
, bio
, iter
)
103 if (end_bv
.bv_len
== iter
.bi_size
)
106 nxt_bv
= bio_iovec(nxt
);
108 if (!BIOVEC_PHYS_MERGEABLE(&end_bv
, &nxt_bv
))
112 * bio and nxt are contiguous in memory; check if the queue allows
113 * these two to be merged into one
115 if (BIOVEC_SEG_BOUNDARY(q
, &end_bv
, &nxt_bv
))
122 __blk_segment_map_sg(struct request_queue
*q
, struct bio_vec
*bvec
,
123 struct scatterlist
*sglist
, struct bio_vec
*bvprv
,
124 struct scatterlist
**sg
, int *nsegs
, int *cluster
)
127 int nbytes
= bvec
->bv_len
;
129 if (*sg
&& *cluster
) {
130 if ((*sg
)->length
+ nbytes
> queue_max_segment_size(q
))
133 if (!BIOVEC_PHYS_MERGEABLE(bvprv
, bvec
))
135 if (!BIOVEC_SEG_BOUNDARY(q
, bvprv
, bvec
))
138 (*sg
)->length
+= nbytes
;
145 * If the driver previously mapped a shorter
146 * list, we could see a termination bit
147 * prematurely unless it fully inits the sg
148 * table on each mapping. We KNOW that there
149 * must be more entries here or the driver
150 * would be buggy, so force clear the
151 * termination bit to avoid doing a full
152 * sg_init_table() in drivers for each command.
158 sg_set_page(*sg
, bvec
->bv_page
, nbytes
, bvec
->bv_offset
);
165 * map a request to scatterlist, return number of sg entries setup. Caller
166 * must make sure sg can hold rq->nr_phys_segments entries
168 int blk_rq_map_sg(struct request_queue
*q
, struct request
*rq
,
169 struct scatterlist
*sglist
)
171 struct bio_vec bvec
, bvprv
= { NULL
};
172 struct req_iterator iter
;
173 struct scatterlist
*sg
;
177 cluster
= blk_queue_cluster(q
);
183 rq_for_each_segment(bvec
, rq
, iter
) {
184 __blk_segment_map_sg(q
, &bvec
, sglist
, &bvprv
, &sg
,
186 } /* segments in rq */
189 if (unlikely(rq
->cmd_flags
& REQ_COPY_USER
) &&
190 (blk_rq_bytes(rq
) & q
->dma_pad_mask
)) {
191 unsigned int pad_len
=
192 (q
->dma_pad_mask
& ~blk_rq_bytes(rq
)) + 1;
194 sg
->length
+= pad_len
;
195 rq
->extra_len
+= pad_len
;
198 if (q
->dma_drain_size
&& q
->dma_drain_needed(rq
)) {
199 if (rq
->cmd_flags
& REQ_WRITE
)
200 memset(q
->dma_drain_buffer
, 0, q
->dma_drain_size
);
202 sg
->page_link
&= ~0x02;
204 sg_set_page(sg
, virt_to_page(q
->dma_drain_buffer
),
206 ((unsigned long)q
->dma_drain_buffer
) &
209 rq
->extra_len
+= q
->dma_drain_size
;
217 EXPORT_SYMBOL(blk_rq_map_sg
);
220 * blk_bio_map_sg - map a bio to a scatterlist
221 * @q: request_queue in question
222 * @bio: bio being mapped
223 * @sglist: scatterlist being mapped
226 * Caller must make sure sg can hold bio->bi_phys_segments entries
228 * Will return the number of sg entries setup
230 int blk_bio_map_sg(struct request_queue
*q
, struct bio
*bio
,
231 struct scatterlist
*sglist
)
233 struct bio_vec bvec
, bvprv
= { NULL
};
234 struct scatterlist
*sg
;
236 struct bvec_iter iter
;
239 cluster
= blk_queue_cluster(q
);
242 bio_for_each_segment(bvec
, bio
, iter
) {
243 __blk_segment_map_sg(q
, &bvec
, sglist
, &bvprv
, &sg
,
245 } /* segments in bio */
250 BUG_ON(bio
->bi_phys_segments
&& nsegs
> bio
->bi_phys_segments
);
253 EXPORT_SYMBOL(blk_bio_map_sg
);
255 static inline int ll_new_hw_segment(struct request_queue
*q
,
259 int nr_phys_segs
= bio_phys_segments(q
, bio
);
261 if (req
->nr_phys_segments
+ nr_phys_segs
> queue_max_segments(q
))
264 if (bio_integrity(bio
) && blk_integrity_merge_bio(q
, req
, bio
))
268 * This will form the start of a new hw segment. Bump both
271 req
->nr_phys_segments
+= nr_phys_segs
;
275 req
->cmd_flags
|= REQ_NOMERGE
;
276 if (req
== q
->last_merge
)
277 q
->last_merge
= NULL
;
281 int ll_back_merge_fn(struct request_queue
*q
, struct request
*req
,
284 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
285 blk_rq_get_max_sectors(req
)) {
286 req
->cmd_flags
|= REQ_NOMERGE
;
287 if (req
== q
->last_merge
)
288 q
->last_merge
= NULL
;
291 if (!bio_flagged(req
->biotail
, BIO_SEG_VALID
))
292 blk_recount_segments(q
, req
->biotail
);
293 if (!bio_flagged(bio
, BIO_SEG_VALID
))
294 blk_recount_segments(q
, bio
);
296 return ll_new_hw_segment(q
, req
, bio
);
299 int ll_front_merge_fn(struct request_queue
*q
, struct request
*req
,
302 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
303 blk_rq_get_max_sectors(req
)) {
304 req
->cmd_flags
|= REQ_NOMERGE
;
305 if (req
== q
->last_merge
)
306 q
->last_merge
= NULL
;
309 if (!bio_flagged(bio
, BIO_SEG_VALID
))
310 blk_recount_segments(q
, bio
);
311 if (!bio_flagged(req
->bio
, BIO_SEG_VALID
))
312 blk_recount_segments(q
, req
->bio
);
314 return ll_new_hw_segment(q
, req
, bio
);
318 * blk-mq uses req->special to carry normal driver per-request payload, it
319 * does not indicate a prepared command that we cannot merge with.
321 static bool req_no_special_merge(struct request
*req
)
323 struct request_queue
*q
= req
->q
;
325 return !q
->mq_ops
&& req
->special
;
328 static int ll_merge_requests_fn(struct request_queue
*q
, struct request
*req
,
329 struct request
*next
)
331 int total_phys_segments
;
332 unsigned int seg_size
=
333 req
->biotail
->bi_seg_back_size
+ next
->bio
->bi_seg_front_size
;
336 * First check if the either of the requests are re-queued
337 * requests. Can't merge them if they are.
339 if (req_no_special_merge(req
) || req_no_special_merge(next
))
343 * Will it become too large?
345 if ((blk_rq_sectors(req
) + blk_rq_sectors(next
)) >
346 blk_rq_get_max_sectors(req
))
349 total_phys_segments
= req
->nr_phys_segments
+ next
->nr_phys_segments
;
350 if (blk_phys_contig_segment(q
, req
->biotail
, next
->bio
)) {
351 if (req
->nr_phys_segments
== 1)
352 req
->bio
->bi_seg_front_size
= seg_size
;
353 if (next
->nr_phys_segments
== 1)
354 next
->biotail
->bi_seg_back_size
= seg_size
;
355 total_phys_segments
--;
358 if (total_phys_segments
> queue_max_segments(q
))
361 if (blk_integrity_rq(req
) && blk_integrity_merge_rq(q
, req
, next
))
365 req
->nr_phys_segments
= total_phys_segments
;
370 * blk_rq_set_mixed_merge - mark a request as mixed merge
371 * @rq: request to mark as mixed merge
374 * @rq is about to be mixed merged. Make sure the attributes
375 * which can be mixed are set in each bio and mark @rq as mixed
378 void blk_rq_set_mixed_merge(struct request
*rq
)
380 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
383 if (rq
->cmd_flags
& REQ_MIXED_MERGE
)
387 * @rq will no longer represent mixable attributes for all the
388 * contained bios. It will just track those of the first one.
389 * Distributes the attributs to each bio.
391 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
392 WARN_ON_ONCE((bio
->bi_rw
& REQ_FAILFAST_MASK
) &&
393 (bio
->bi_rw
& REQ_FAILFAST_MASK
) != ff
);
396 rq
->cmd_flags
|= REQ_MIXED_MERGE
;
399 static void blk_account_io_merge(struct request
*req
)
401 if (blk_do_io_stat(req
)) {
402 struct hd_struct
*part
;
405 cpu
= part_stat_lock();
408 part_round_stats(cpu
, part
);
409 part_dec_in_flight(part
, rq_data_dir(req
));
417 * Has to be called with the request spinlock acquired
419 static int attempt_merge(struct request_queue
*q
, struct request
*req
,
420 struct request
*next
)
422 if (!rq_mergeable(req
) || !rq_mergeable(next
))
425 if (!blk_check_merge_flags(req
->cmd_flags
, next
->cmd_flags
))
431 if (blk_rq_pos(req
) + blk_rq_sectors(req
) != blk_rq_pos(next
))
434 if (rq_data_dir(req
) != rq_data_dir(next
)
435 || req
->rq_disk
!= next
->rq_disk
436 || req_no_special_merge(next
))
439 if (req
->cmd_flags
& REQ_WRITE_SAME
&&
440 !blk_write_same_mergeable(req
->bio
, next
->bio
))
444 * If we are allowed to merge, then append bio list
445 * from next to rq and release next. merge_requests_fn
446 * will have updated segment counts, update sector
449 if (!ll_merge_requests_fn(q
, req
, next
))
453 * If failfast settings disagree or any of the two is already
454 * a mixed merge, mark both as mixed before proceeding. This
455 * makes sure that all involved bios have mixable attributes
458 if ((req
->cmd_flags
| next
->cmd_flags
) & REQ_MIXED_MERGE
||
459 (req
->cmd_flags
& REQ_FAILFAST_MASK
) !=
460 (next
->cmd_flags
& REQ_FAILFAST_MASK
)) {
461 blk_rq_set_mixed_merge(req
);
462 blk_rq_set_mixed_merge(next
);
466 * At this point we have either done a back merge
467 * or front merge. We need the smaller start_time of
468 * the merged requests to be the current request
469 * for accounting purposes.
471 if (time_after(req
->start_time
, next
->start_time
))
472 req
->start_time
= next
->start_time
;
474 req
->biotail
->bi_next
= next
->bio
;
475 req
->biotail
= next
->biotail
;
477 req
->__data_len
+= blk_rq_bytes(next
);
479 elv_merge_requests(q
, req
, next
);
482 * 'next' is going away, so update stats accordingly
484 blk_account_io_merge(next
);
486 req
->ioprio
= ioprio_best(req
->ioprio
, next
->ioprio
);
487 if (blk_rq_cpu_valid(next
))
488 req
->cpu
= next
->cpu
;
490 /* owner-ship of bio passed from next to req */
492 __blk_put_request(q
, next
);
496 int attempt_back_merge(struct request_queue
*q
, struct request
*rq
)
498 struct request
*next
= elv_latter_request(q
, rq
);
501 return attempt_merge(q
, rq
, next
);
506 int attempt_front_merge(struct request_queue
*q
, struct request
*rq
)
508 struct request
*prev
= elv_former_request(q
, rq
);
511 return attempt_merge(q
, prev
, rq
);
516 int blk_attempt_req_merge(struct request_queue
*q
, struct request
*rq
,
517 struct request
*next
)
519 return attempt_merge(q
, rq
, next
);
522 bool blk_rq_merge_ok(struct request
*rq
, struct bio
*bio
)
524 if (!rq_mergeable(rq
) || !bio_mergeable(bio
))
527 if (!blk_check_merge_flags(rq
->cmd_flags
, bio
->bi_rw
))
530 /* different data direction or already started, don't merge */
531 if (bio_data_dir(bio
) != rq_data_dir(rq
))
534 /* must be same device and not a special request */
535 if (rq
->rq_disk
!= bio
->bi_bdev
->bd_disk
|| req_no_special_merge(rq
))
538 /* only merge integrity protected bio into ditto rq */
539 if (bio_integrity(bio
) != blk_integrity_rq(rq
))
542 /* must be using the same buffer */
543 if (rq
->cmd_flags
& REQ_WRITE_SAME
&&
544 !blk_write_same_mergeable(rq
->bio
, bio
))
550 int blk_try_merge(struct request
*rq
, struct bio
*bio
)
552 if (blk_rq_pos(rq
) + blk_rq_sectors(rq
) == bio
->bi_iter
.bi_sector
)
553 return ELEVATOR_BACK_MERGE
;
554 else if (blk_rq_pos(rq
) - bio_sectors(bio
) == bio
->bi_iter
.bi_sector
)
555 return ELEVATOR_FRONT_MERGE
;
556 return ELEVATOR_NO_MERGE
;