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, no_sg_merge;
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);
38 no_sg_merge = test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
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 rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
94 void blk_recount_segments(struct request_queue *q, struct bio *bio)
96 if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags))
97 bio->bi_phys_segments = bio->bi_vcnt;
99 struct bio *nxt = bio->bi_next;
102 bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
106 bio->bi_flags |= (1 << BIO_SEG_VALID);
108 EXPORT_SYMBOL(blk_recount_segments);
110 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
113 struct bio_vec end_bv = { NULL }, nxt_bv;
114 struct bvec_iter iter;
116 if (!blk_queue_cluster(q))
119 if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
120 queue_max_segment_size(q))
123 if (!bio_has_data(bio))
126 bio_for_each_segment(end_bv, bio, iter)
127 if (end_bv.bv_len == iter.bi_size)
130 nxt_bv = bio_iovec(nxt);
132 if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
136 * bio and nxt are contiguous in memory; check if the queue allows
137 * these two to be merged into one
139 if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
146 __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
147 struct scatterlist *sglist, struct bio_vec *bvprv,
148 struct scatterlist **sg, int *nsegs, int *cluster)
151 int nbytes = bvec->bv_len;
153 if (*sg && *cluster) {
154 if ((*sg)->length + nbytes > queue_max_segment_size(q))
157 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
159 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
162 (*sg)->length += nbytes;
169 * If the driver previously mapped a shorter
170 * list, we could see a termination bit
171 * prematurely unless it fully inits the sg
172 * table on each mapping. We KNOW that there
173 * must be more entries here or the driver
174 * would be buggy, so force clear the
175 * termination bit to avoid doing a full
176 * sg_init_table() in drivers for each command.
182 sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
188 static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
189 struct scatterlist *sglist,
190 struct scatterlist **sg)
192 struct bio_vec bvec, bvprv = { NULL };
193 struct bvec_iter iter;
197 cluster = blk_queue_cluster(q);
199 if (bio->bi_rw & REQ_DISCARD) {
201 * This is a hack - drivers should be neither modifying the
202 * biovec, nor relying on bi_vcnt - but because of
203 * blk_add_request_payload(), a discard bio may or may not have
204 * a payload we need to set up here (thank you Christoph) and
205 * bi_vcnt is really the only way of telling if we need to.
214 if (bio->bi_rw & REQ_WRITE_SAME) {
217 bvec = bio_iovec(bio);
218 sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
223 bio_for_each_segment(bvec, bio, iter)
224 __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
231 * map a request to scatterlist, return number of sg entries setup. Caller
232 * must make sure sg can hold rq->nr_phys_segments entries
234 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
235 struct scatterlist *sglist)
237 struct scatterlist *sg = NULL;
241 nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
243 if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
244 (blk_rq_bytes(rq) & q->dma_pad_mask)) {
245 unsigned int pad_len =
246 (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
248 sg->length += pad_len;
249 rq->extra_len += pad_len;
252 if (q->dma_drain_size && q->dma_drain_needed(rq)) {
253 if (rq->cmd_flags & REQ_WRITE)
254 memset(q->dma_drain_buffer, 0, q->dma_drain_size);
256 sg->page_link &= ~0x02;
258 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
260 ((unsigned long)q->dma_drain_buffer) &
263 rq->extra_len += q->dma_drain_size;
271 EXPORT_SYMBOL(blk_rq_map_sg);
274 * blk_bio_map_sg - map a bio to a scatterlist
275 * @q: request_queue in question
276 * @bio: bio being mapped
277 * @sglist: scatterlist being mapped
280 * Caller must make sure sg can hold bio->bi_phys_segments entries
282 * Will return the number of sg entries setup
284 int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
285 struct scatterlist *sglist)
287 struct scatterlist *sg = NULL;
289 struct bio *next = bio->bi_next;
292 nsegs = __blk_bios_map_sg(q, bio, sglist, &sg);
297 BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments);
300 EXPORT_SYMBOL(blk_bio_map_sg);
302 static inline int ll_new_hw_segment(struct request_queue *q,
306 int nr_phys_segs = bio_phys_segments(q, bio);
308 if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
311 if (bio_integrity(bio) && blk_integrity_merge_bio(q, req, bio))
315 * This will form the start of a new hw segment. Bump both
318 req->nr_phys_segments += nr_phys_segs;
322 req->cmd_flags |= REQ_NOMERGE;
323 if (req == q->last_merge)
324 q->last_merge = NULL;
328 int ll_back_merge_fn(struct request_queue *q, struct request *req,
331 if (blk_rq_sectors(req) + bio_sectors(bio) >
332 blk_rq_get_max_sectors(req)) {
333 req->cmd_flags |= REQ_NOMERGE;
334 if (req == q->last_merge)
335 q->last_merge = NULL;
338 if (!bio_flagged(req->biotail, BIO_SEG_VALID))
339 blk_recount_segments(q, req->biotail);
340 if (!bio_flagged(bio, BIO_SEG_VALID))
341 blk_recount_segments(q, bio);
343 return ll_new_hw_segment(q, req, bio);
346 int ll_front_merge_fn(struct request_queue *q, struct request *req,
349 if (blk_rq_sectors(req) + bio_sectors(bio) >
350 blk_rq_get_max_sectors(req)) {
351 req->cmd_flags |= REQ_NOMERGE;
352 if (req == q->last_merge)
353 q->last_merge = NULL;
356 if (!bio_flagged(bio, BIO_SEG_VALID))
357 blk_recount_segments(q, bio);
358 if (!bio_flagged(req->bio, BIO_SEG_VALID))
359 blk_recount_segments(q, req->bio);
361 return ll_new_hw_segment(q, req, bio);
365 * blk-mq uses req->special to carry normal driver per-request payload, it
366 * does not indicate a prepared command that we cannot merge with.
368 static bool req_no_special_merge(struct request *req)
370 struct request_queue *q = req->q;
372 return !q->mq_ops && req->special;
375 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
376 struct request *next)
378 int total_phys_segments;
379 unsigned int seg_size =
380 req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
383 * First check if the either of the requests are re-queued
384 * requests. Can't merge them if they are.
386 if (req_no_special_merge(req) || req_no_special_merge(next))
390 * Will it become too large?
392 if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
393 blk_rq_get_max_sectors(req))
396 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
397 if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
398 if (req->nr_phys_segments == 1)
399 req->bio->bi_seg_front_size = seg_size;
400 if (next->nr_phys_segments == 1)
401 next->biotail->bi_seg_back_size = seg_size;
402 total_phys_segments--;
405 if (total_phys_segments > queue_max_segments(q))
408 if (blk_integrity_rq(req) && blk_integrity_merge_rq(q, req, next))
412 req->nr_phys_segments = total_phys_segments;
417 * blk_rq_set_mixed_merge - mark a request as mixed merge
418 * @rq: request to mark as mixed merge
421 * @rq is about to be mixed merged. Make sure the attributes
422 * which can be mixed are set in each bio and mark @rq as mixed
425 void blk_rq_set_mixed_merge(struct request *rq)
427 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
430 if (rq->cmd_flags & REQ_MIXED_MERGE)
434 * @rq will no longer represent mixable attributes for all the
435 * contained bios. It will just track those of the first one.
436 * Distributes the attributs to each bio.
438 for (bio = rq->bio; bio; bio = bio->bi_next) {
439 WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
440 (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
443 rq->cmd_flags |= REQ_MIXED_MERGE;
446 static void blk_account_io_merge(struct request *req)
448 if (blk_do_io_stat(req)) {
449 struct hd_struct *part;
452 cpu = part_stat_lock();
455 part_round_stats(cpu, part);
456 part_dec_in_flight(part, rq_data_dir(req));
464 * Has to be called with the request spinlock acquired
466 static int attempt_merge(struct request_queue *q, struct request *req,
467 struct request *next)
469 if (!rq_mergeable(req) || !rq_mergeable(next))
472 if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
478 if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
481 if (rq_data_dir(req) != rq_data_dir(next)
482 || req->rq_disk != next->rq_disk
483 || req_no_special_merge(next))
486 if (req->cmd_flags & REQ_WRITE_SAME &&
487 !blk_write_same_mergeable(req->bio, next->bio))
491 * If we are allowed to merge, then append bio list
492 * from next to rq and release next. merge_requests_fn
493 * will have updated segment counts, update sector
496 if (!ll_merge_requests_fn(q, req, next))
500 * If failfast settings disagree or any of the two is already
501 * a mixed merge, mark both as mixed before proceeding. This
502 * makes sure that all involved bios have mixable attributes
505 if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
506 (req->cmd_flags & REQ_FAILFAST_MASK) !=
507 (next->cmd_flags & REQ_FAILFAST_MASK)) {
508 blk_rq_set_mixed_merge(req);
509 blk_rq_set_mixed_merge(next);
513 * At this point we have either done a back merge
514 * or front merge. We need the smaller start_time of
515 * the merged requests to be the current request
516 * for accounting purposes.
518 if (time_after(req->start_time, next->start_time))
519 req->start_time = next->start_time;
521 req->biotail->bi_next = next->bio;
522 req->biotail = next->biotail;
524 req->__data_len += blk_rq_bytes(next);
526 elv_merge_requests(q, req, next);
529 * 'next' is going away, so update stats accordingly
531 blk_account_io_merge(next);
533 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
534 if (blk_rq_cpu_valid(next))
535 req->cpu = next->cpu;
537 /* owner-ship of bio passed from next to req */
539 __blk_put_request(q, next);
543 int attempt_back_merge(struct request_queue *q, struct request *rq)
545 struct request *next = elv_latter_request(q, rq);
548 return attempt_merge(q, rq, next);
553 int attempt_front_merge(struct request_queue *q, struct request *rq)
555 struct request *prev = elv_former_request(q, rq);
558 return attempt_merge(q, prev, rq);
563 int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
564 struct request *next)
566 return attempt_merge(q, rq, next);
569 bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
571 if (!rq_mergeable(rq) || !bio_mergeable(bio))
574 if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
577 /* different data direction or already started, don't merge */
578 if (bio_data_dir(bio) != rq_data_dir(rq))
581 /* must be same device and not a special request */
582 if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
585 /* only merge integrity protected bio into ditto rq */
586 if (bio_integrity(bio) != blk_integrity_rq(rq))
589 /* must be using the same buffer */
590 if (rq->cmd_flags & REQ_WRITE_SAME &&
591 !blk_write_same_mergeable(rq->bio, bio))
597 int blk_try_merge(struct request *rq, struct bio *bio)
599 if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
600 return ELEVATOR_BACK_MERGE;
601 else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
602 return ELEVATOR_FRONT_MERGE;
603 return ELEVATOR_NO_MERGE;