Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions related to segment and merge handling |
| 3 | */ |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/bio.h> |
| 7 | #include <linux/blkdev.h> |
| 8 | #include <linux/scatterlist.h> |
| 9 | |
| 10 | #include "blk.h" |
| 11 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 12 | static unsigned int __blk_recalc_rq_segments(struct request_queue *q, |
Ming Lei | 0738854 | 2014-09-02 23:02:59 +0800 | [diff] [blame] | 13 | struct bio *bio, |
| 14 | bool no_sg_merge) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 15 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 16 | struct bio_vec bv, bvprv = { NULL }; |
Ming Lei | 0738854 | 2014-09-02 23:02:59 +0800 | [diff] [blame] | 17 | int cluster, high, highprv = 1; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 18 | unsigned int seg_size, nr_phys_segs; |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 19 | struct bio *fbio, *bbio; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 20 | struct bvec_iter iter; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 21 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 22 | if (!bio) |
| 23 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 24 | |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 25 | /* |
| 26 | * This should probably be returning 0, but blk_add_request_payload() |
| 27 | * (Christoph!!!!) |
| 28 | */ |
| 29 | if (bio->bi_rw & REQ_DISCARD) |
| 30 | return 1; |
| 31 | |
| 32 | if (bio->bi_rw & REQ_WRITE_SAME) |
| 33 | return 1; |
| 34 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 35 | fbio = bio; |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 36 | cluster = blk_queue_cluster(q); |
Mikulas Patocka | 5df97b9 | 2008-08-15 10:20:02 +0200 | [diff] [blame] | 37 | seg_size = 0; |
Andi Kleen | 2c8919d | 2010-06-21 11:02:47 +0200 | [diff] [blame] | 38 | nr_phys_segs = 0; |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 39 | high = 0; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 40 | for_each_bio(bio) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 41 | bio_for_each_segment(bv, bio, iter) { |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 42 | /* |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 43 | * If SG merging is disabled, each bio vector is |
| 44 | * a segment |
| 45 | */ |
| 46 | if (no_sg_merge) |
| 47 | goto new_segment; |
| 48 | |
| 49 | /* |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 50 | * the trick here is making sure that a high page is |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 51 | * never considered part of another segment, since |
| 52 | * that might change with the bounce page. |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 53 | */ |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 54 | high = page_to_pfn(bv.bv_page) > queue_bounce_pfn(q); |
| 55 | if (!high && !highprv && cluster) { |
| 56 | if (seg_size + bv.bv_len |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 57 | > queue_max_segment_size(q)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 58 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 59 | if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 60 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 61 | if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv)) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 62 | goto new_segment; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 63 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 64 | seg_size += bv.bv_len; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 65 | bvprv = bv; |
| 66 | continue; |
| 67 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 68 | new_segment: |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 69 | if (nr_phys_segs == 1 && seg_size > |
| 70 | fbio->bi_seg_front_size) |
| 71 | fbio->bi_seg_front_size = seg_size; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 72 | |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 73 | nr_phys_segs++; |
| 74 | bvprv = bv; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 75 | seg_size = bv.bv_len; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 76 | highprv = high; |
| 77 | } |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 78 | bbio = bio; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Jens Axboe | 59247ea | 2009-03-06 08:55:24 +0100 | [diff] [blame] | 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; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 85 | |
| 86 | return nr_phys_segs; |
| 87 | } |
| 88 | |
| 89 | void blk_recalc_rq_segments(struct request *rq) |
| 90 | { |
Ming Lei | 0738854 | 2014-09-02 23:02:59 +0800 | [diff] [blame] | 91 | bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE, |
| 92 | &rq->q->queue_flags); |
| 93 | |
| 94 | rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio, |
| 95 | no_sg_merge); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void blk_recount_segments(struct request_queue *q, struct bio *bio) |
| 99 | { |
Ming Lei | 7f60dca | 2014-11-12 00:15:41 +0800 | [diff] [blame] | 100 | unsigned short seg_cnt; |
Ming Lei | 764f612 | 2014-10-09 23:17:35 +0800 | [diff] [blame] | 101 | |
Ming Lei | 7f60dca | 2014-11-12 00:15:41 +0800 | [diff] [blame] | 102 | /* estimate segment number by bi_vcnt for non-cloned bio */ |
| 103 | if (bio_flagged(bio, BIO_CLONED)) |
| 104 | seg_cnt = bio_segments(bio); |
| 105 | else |
| 106 | seg_cnt = bio->bi_vcnt; |
| 107 | |
| 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; |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 111 | else { |
| 112 | struct bio *nxt = bio->bi_next; |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 113 | |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 114 | bio->bi_next = NULL; |
Ming Lei | 7f60dca | 2014-11-12 00:15:41 +0800 | [diff] [blame] | 115 | bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false); |
Jens Axboe | 05f1dd5 | 2014-05-29 09:53:32 -0600 | [diff] [blame] | 116 | bio->bi_next = nxt; |
| 117 | } |
| 118 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 119 | bio->bi_flags |= (1 << BIO_SEG_VALID); |
| 120 | } |
| 121 | EXPORT_SYMBOL(blk_recount_segments); |
| 122 | |
| 123 | static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio, |
| 124 | struct bio *nxt) |
| 125 | { |
Kent Overstreet | 2b8221e | 2013-12-03 14:29:09 -0700 | [diff] [blame] | 126 | struct bio_vec end_bv = { NULL }, nxt_bv; |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 127 | struct bvec_iter iter; |
| 128 | |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 129 | if (!blk_queue_cluster(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 130 | return 0; |
| 131 | |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 132 | if (bio->bi_seg_back_size + nxt->bi_seg_front_size > |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 133 | queue_max_segment_size(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 134 | return 0; |
| 135 | |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 136 | if (!bio_has_data(bio)) |
| 137 | return 1; |
| 138 | |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 139 | bio_for_each_segment(end_bv, bio, iter) |
| 140 | if (end_bv.bv_len == iter.bi_size) |
| 141 | break; |
| 142 | |
| 143 | nxt_bv = bio_iovec(nxt); |
| 144 | |
| 145 | if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv)) |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 146 | return 0; |
| 147 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 148 | /* |
David Woodhouse | e17fc0a | 2008-08-09 16:42:20 +0100 | [diff] [blame] | 149 | * bio and nxt are contiguous in memory; check if the queue allows |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 150 | * these two to be merged into one |
| 151 | */ |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 152 | if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 153 | return 1; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 158 | static inline void |
Asias He | 963ab9e5 | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 159 | __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 160 | struct scatterlist *sglist, struct bio_vec *bvprv, |
Asias He | 963ab9e5 | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 161 | struct scatterlist **sg, int *nsegs, int *cluster) |
| 162 | { |
| 163 | |
| 164 | int nbytes = bvec->bv_len; |
| 165 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 166 | if (*sg && *cluster) { |
Asias He | 963ab9e5 | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 167 | if ((*sg)->length + nbytes > queue_max_segment_size(q)) |
| 168 | goto new_segment; |
| 169 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 170 | if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) |
Asias He | 963ab9e5 | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 171 | goto new_segment; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 172 | if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) |
Asias He | 963ab9e5 | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 173 | goto new_segment; |
| 174 | |
| 175 | (*sg)->length += nbytes; |
| 176 | } else { |
| 177 | new_segment: |
| 178 | if (!*sg) |
| 179 | *sg = sglist; |
| 180 | else { |
| 181 | /* |
| 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. |
| 190 | */ |
Paolo Bonzini | c8164d8 | 2013-03-20 15:37:08 +1030 | [diff] [blame] | 191 | sg_unmark_end(*sg); |
Asias He | 963ab9e5 | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 192 | *sg = sg_next(*sg); |
| 193 | } |
| 194 | |
| 195 | sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset); |
| 196 | (*nsegs)++; |
| 197 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 198 | *bvprv = *bvec; |
Asias He | 963ab9e5 | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 201 | static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, |
| 202 | struct scatterlist *sglist, |
| 203 | struct scatterlist **sg) |
| 204 | { |
| 205 | struct bio_vec bvec, bvprv = { NULL }; |
| 206 | struct bvec_iter iter; |
| 207 | int nsegs, cluster; |
| 208 | |
| 209 | nsegs = 0; |
| 210 | cluster = blk_queue_cluster(q); |
| 211 | |
| 212 | if (bio->bi_rw & REQ_DISCARD) { |
| 213 | /* |
| 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. |
| 219 | */ |
| 220 | |
| 221 | if (bio->bi_vcnt) |
| 222 | goto single_segment; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | if (bio->bi_rw & REQ_WRITE_SAME) { |
| 228 | single_segment: |
| 229 | *sg = sglist; |
| 230 | bvec = bio_iovec(bio); |
| 231 | sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset); |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | for_each_bio(bio) |
| 236 | bio_for_each_segment(bvec, bio, iter) |
| 237 | __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg, |
| 238 | &nsegs, &cluster); |
| 239 | |
| 240 | return nsegs; |
| 241 | } |
| 242 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 243 | /* |
| 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 |
| 246 | */ |
| 247 | int blk_rq_map_sg(struct request_queue *q, struct request *rq, |
| 248 | struct scatterlist *sglist) |
| 249 | { |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 250 | struct scatterlist *sg = NULL; |
| 251 | int nsegs = 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 252 | |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 253 | if (rq->bio) |
| 254 | nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg); |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 255 | |
| 256 | if (unlikely(rq->cmd_flags & REQ_COPY_USER) && |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 257 | (blk_rq_bytes(rq) & q->dma_pad_mask)) { |
| 258 | unsigned int pad_len = |
| 259 | (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1; |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 260 | |
| 261 | sg->length += pad_len; |
| 262 | rq->extra_len += pad_len; |
| 263 | } |
| 264 | |
Tejun Heo | 2fb98e8 | 2008-02-19 11:36:53 +0100 | [diff] [blame] | 265 | if (q->dma_drain_size && q->dma_drain_needed(rq)) { |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 266 | if (rq->cmd_flags & REQ_WRITE) |
Tejun Heo | db0a2e0 | 2008-02-19 11:36:55 +0100 | [diff] [blame] | 267 | memset(q->dma_drain_buffer, 0, q->dma_drain_size); |
| 268 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 269 | sg->page_link &= ~0x02; |
| 270 | sg = sg_next(sg); |
| 271 | sg_set_page(sg, virt_to_page(q->dma_drain_buffer), |
| 272 | q->dma_drain_size, |
| 273 | ((unsigned long)q->dma_drain_buffer) & |
| 274 | (PAGE_SIZE - 1)); |
| 275 | nsegs++; |
FUJITA Tomonori | 7a85f88 | 2008-03-04 11:17:11 +0100 | [diff] [blame] | 276 | rq->extra_len += q->dma_drain_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (sg) |
| 280 | sg_mark_end(sg); |
| 281 | |
| 282 | return nsegs; |
| 283 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 284 | EXPORT_SYMBOL(blk_rq_map_sg); |
| 285 | |
Asias He | 85b9f66 | 2012-08-02 23:42:04 +0200 | [diff] [blame] | 286 | /** |
| 287 | * blk_bio_map_sg - map a bio to a scatterlist |
| 288 | * @q: request_queue in question |
| 289 | * @bio: bio being mapped |
| 290 | * @sglist: scatterlist being mapped |
| 291 | * |
| 292 | * Note: |
| 293 | * Caller must make sure sg can hold bio->bi_phys_segments entries |
| 294 | * |
| 295 | * Will return the number of sg entries setup |
| 296 | */ |
| 297 | int blk_bio_map_sg(struct request_queue *q, struct bio *bio, |
| 298 | struct scatterlist *sglist) |
| 299 | { |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 300 | struct scatterlist *sg = NULL; |
| 301 | int nsegs; |
| 302 | struct bio *next = bio->bi_next; |
| 303 | bio->bi_next = NULL; |
Asias He | 85b9f66 | 2012-08-02 23:42:04 +0200 | [diff] [blame] | 304 | |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 305 | nsegs = __blk_bios_map_sg(q, bio, sglist, &sg); |
| 306 | bio->bi_next = next; |
Asias He | 85b9f66 | 2012-08-02 23:42:04 +0200 | [diff] [blame] | 307 | if (sg) |
| 308 | sg_mark_end(sg); |
| 309 | |
| 310 | BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments); |
| 311 | return nsegs; |
| 312 | } |
| 313 | EXPORT_SYMBOL(blk_bio_map_sg); |
| 314 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 315 | static inline int ll_new_hw_segment(struct request_queue *q, |
| 316 | struct request *req, |
| 317 | struct bio *bio) |
| 318 | { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 319 | int nr_phys_segs = bio_phys_segments(q, bio); |
| 320 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 321 | if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) |
| 322 | goto no_merge; |
| 323 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 324 | if (blk_integrity_merge_bio(q, req, bio) == false) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 325 | goto no_merge; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 326 | |
| 327 | /* |
| 328 | * This will form the start of a new hw segment. Bump both |
| 329 | * counters. |
| 330 | */ |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 331 | req->nr_phys_segments += nr_phys_segs; |
| 332 | return 1; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 333 | |
| 334 | no_merge: |
| 335 | req->cmd_flags |= REQ_NOMERGE; |
| 336 | if (req == q->last_merge) |
| 337 | q->last_merge = NULL; |
| 338 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | int ll_back_merge_fn(struct request_queue *q, struct request *req, |
| 342 | struct bio *bio) |
| 343 | { |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 344 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
| 345 | blk_rq_get_max_sectors(req)) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 346 | req->cmd_flags |= REQ_NOMERGE; |
| 347 | if (req == q->last_merge) |
| 348 | q->last_merge = NULL; |
| 349 | return 0; |
| 350 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 351 | if (!bio_flagged(req->biotail, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 352 | blk_recount_segments(q, req->biotail); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 353 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 354 | blk_recount_segments(q, bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 355 | |
| 356 | return ll_new_hw_segment(q, req, bio); |
| 357 | } |
| 358 | |
Jens Axboe | 6728cb0 | 2008-01-31 13:03:55 +0100 | [diff] [blame] | 359 | int ll_front_merge_fn(struct request_queue *q, struct request *req, |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 360 | struct bio *bio) |
| 361 | { |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 362 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
| 363 | blk_rq_get_max_sectors(req)) { |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 364 | req->cmd_flags |= REQ_NOMERGE; |
| 365 | if (req == q->last_merge) |
| 366 | q->last_merge = NULL; |
| 367 | return 0; |
| 368 | } |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 369 | if (!bio_flagged(bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 370 | blk_recount_segments(q, bio); |
Jens Axboe | 2cdf79c | 2008-05-07 09:33:55 +0200 | [diff] [blame] | 371 | if (!bio_flagged(req->bio, BIO_SEG_VALID)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 372 | blk_recount_segments(q, req->bio); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 373 | |
| 374 | return ll_new_hw_segment(q, req, bio); |
| 375 | } |
| 376 | |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 377 | /* |
| 378 | * blk-mq uses req->special to carry normal driver per-request payload, it |
| 379 | * does not indicate a prepared command that we cannot merge with. |
| 380 | */ |
| 381 | static bool req_no_special_merge(struct request *req) |
| 382 | { |
| 383 | struct request_queue *q = req->q; |
| 384 | |
| 385 | return !q->mq_ops && req->special; |
| 386 | } |
| 387 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 388 | static int ll_merge_requests_fn(struct request_queue *q, struct request *req, |
| 389 | struct request *next) |
| 390 | { |
| 391 | int total_phys_segments; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 392 | unsigned int seg_size = |
| 393 | req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 394 | |
| 395 | /* |
| 396 | * First check if the either of the requests are re-queued |
| 397 | * requests. Can't merge them if they are. |
| 398 | */ |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 399 | if (req_no_special_merge(req) || req_no_special_merge(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 400 | return 0; |
| 401 | |
| 402 | /* |
| 403 | * Will it become too large? |
| 404 | */ |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 405 | if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > |
| 406 | blk_rq_get_max_sectors(req)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 407 | return 0; |
| 408 | |
| 409 | total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 410 | if (blk_phys_contig_segment(q, req->biotail, next->bio)) { |
| 411 | if (req->nr_phys_segments == 1) |
| 412 | req->bio->bi_seg_front_size = seg_size; |
| 413 | if (next->nr_phys_segments == 1) |
| 414 | next->biotail->bi_seg_back_size = seg_size; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 415 | total_phys_segments--; |
FUJITA Tomonori | 8677142 | 2008-10-13 14:19:05 +0200 | [diff] [blame] | 416 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 417 | |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 418 | if (total_phys_segments > queue_max_segments(q)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 419 | return 0; |
| 420 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 421 | if (blk_integrity_merge_rq(q, req, next) == false) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 422 | return 0; |
| 423 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 424 | /* Merge is OK... */ |
| 425 | req->nr_phys_segments = total_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 426 | return 1; |
| 427 | } |
| 428 | |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 429 | /** |
| 430 | * blk_rq_set_mixed_merge - mark a request as mixed merge |
| 431 | * @rq: request to mark as mixed merge |
| 432 | * |
| 433 | * Description: |
| 434 | * @rq is about to be mixed merged. Make sure the attributes |
| 435 | * which can be mixed are set in each bio and mark @rq as mixed |
| 436 | * merged. |
| 437 | */ |
| 438 | void blk_rq_set_mixed_merge(struct request *rq) |
| 439 | { |
| 440 | unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; |
| 441 | struct bio *bio; |
| 442 | |
| 443 | if (rq->cmd_flags & REQ_MIXED_MERGE) |
| 444 | return; |
| 445 | |
| 446 | /* |
| 447 | * @rq will no longer represent mixable attributes for all the |
| 448 | * contained bios. It will just track those of the first one. |
| 449 | * Distributes the attributs to each bio. |
| 450 | */ |
| 451 | for (bio = rq->bio; bio; bio = bio->bi_next) { |
| 452 | WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) && |
| 453 | (bio->bi_rw & REQ_FAILFAST_MASK) != ff); |
| 454 | bio->bi_rw |= ff; |
| 455 | } |
| 456 | rq->cmd_flags |= REQ_MIXED_MERGE; |
| 457 | } |
| 458 | |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 459 | static void blk_account_io_merge(struct request *req) |
| 460 | { |
| 461 | if (blk_do_io_stat(req)) { |
| 462 | struct hd_struct *part; |
| 463 | int cpu; |
| 464 | |
| 465 | cpu = part_stat_lock(); |
Jerome Marchand | 09e099d | 2011-01-05 16:57:38 +0100 | [diff] [blame] | 466 | part = req->part; |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 467 | |
| 468 | part_round_stats(cpu, part); |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 469 | part_dec_in_flight(part, rq_data_dir(req)); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 470 | |
Jens Axboe | 6c23a96 | 2011-01-07 08:43:37 +0100 | [diff] [blame] | 471 | hd_struct_put(part); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 472 | part_stat_unlock(); |
| 473 | } |
| 474 | } |
| 475 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 476 | /* |
| 477 | * Has to be called with the request spinlock acquired |
| 478 | */ |
| 479 | static int attempt_merge(struct request_queue *q, struct request *req, |
| 480 | struct request *next) |
| 481 | { |
| 482 | if (!rq_mergeable(req) || !rq_mergeable(next)) |
| 483 | return 0; |
| 484 | |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 485 | if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags)) |
| 486 | return 0; |
| 487 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 488 | /* |
| 489 | * not contiguous |
| 490 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 491 | if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 492 | return 0; |
| 493 | |
| 494 | if (rq_data_dir(req) != rq_data_dir(next) |
| 495 | || req->rq_disk != next->rq_disk |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 496 | || req_no_special_merge(next)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 497 | return 0; |
| 498 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 499 | if (req->cmd_flags & REQ_WRITE_SAME && |
| 500 | !blk_write_same_mergeable(req->bio, next->bio)) |
| 501 | return 0; |
| 502 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 503 | /* |
| 504 | * If we are allowed to merge, then append bio list |
| 505 | * from next to rq and release next. merge_requests_fn |
| 506 | * will have updated segment counts, update sector |
| 507 | * counts here. |
| 508 | */ |
| 509 | if (!ll_merge_requests_fn(q, req, next)) |
| 510 | return 0; |
| 511 | |
| 512 | /* |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 513 | * If failfast settings disagree or any of the two is already |
| 514 | * a mixed merge, mark both as mixed before proceeding. This |
| 515 | * makes sure that all involved bios have mixable attributes |
| 516 | * set properly. |
| 517 | */ |
| 518 | if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE || |
| 519 | (req->cmd_flags & REQ_FAILFAST_MASK) != |
| 520 | (next->cmd_flags & REQ_FAILFAST_MASK)) { |
| 521 | blk_rq_set_mixed_merge(req); |
| 522 | blk_rq_set_mixed_merge(next); |
| 523 | } |
| 524 | |
| 525 | /* |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 526 | * At this point we have either done a back merge |
| 527 | * or front merge. We need the smaller start_time of |
| 528 | * the merged requests to be the current request |
| 529 | * for accounting purposes. |
| 530 | */ |
| 531 | if (time_after(req->start_time, next->start_time)) |
| 532 | req->start_time = next->start_time; |
| 533 | |
| 534 | req->biotail->bi_next = next->bio; |
| 535 | req->biotail = next->biotail; |
| 536 | |
Tejun Heo | a2dec7b | 2009-05-07 22:24:44 +0900 | [diff] [blame] | 537 | req->__data_len += blk_rq_bytes(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 538 | |
| 539 | elv_merge_requests(q, req, next); |
| 540 | |
Jerome Marchand | 42dad76 | 2009-04-22 14:01:49 +0200 | [diff] [blame] | 541 | /* |
| 542 | * 'next' is going away, so update stats accordingly |
| 543 | */ |
| 544 | blk_account_io_merge(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 545 | |
| 546 | req->ioprio = ioprio_best(req->ioprio, next->ioprio); |
Jens Axboe | ab780f1 | 2008-08-26 10:25:02 +0200 | [diff] [blame] | 547 | if (blk_rq_cpu_valid(next)) |
| 548 | req->cpu = next->cpu; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 549 | |
Boaz Harrosh | 1cd96c2 | 2009-03-24 12:35:07 +0100 | [diff] [blame] | 550 | /* owner-ship of bio passed from next to req */ |
| 551 | next->bio = NULL; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 552 | __blk_put_request(q, next); |
| 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | int attempt_back_merge(struct request_queue *q, struct request *rq) |
| 557 | { |
| 558 | struct request *next = elv_latter_request(q, rq); |
| 559 | |
| 560 | if (next) |
| 561 | return attempt_merge(q, rq, next); |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | int attempt_front_merge(struct request_queue *q, struct request *rq) |
| 567 | { |
| 568 | struct request *prev = elv_former_request(q, rq); |
| 569 | |
| 570 | if (prev) |
| 571 | return attempt_merge(q, prev, rq); |
| 572 | |
| 573 | return 0; |
| 574 | } |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 575 | |
| 576 | int blk_attempt_req_merge(struct request_queue *q, struct request *rq, |
| 577 | struct request *next) |
| 578 | { |
| 579 | return attempt_merge(q, rq, next); |
| 580 | } |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 581 | |
| 582 | bool blk_rq_merge_ok(struct request *rq, struct bio *bio) |
| 583 | { |
Jens Axboe | 66cb45a | 2014-06-24 16:22:24 -0600 | [diff] [blame] | 584 | struct request_queue *q = rq->q; |
| 585 | |
Martin K. Petersen | e2a60da | 2012-09-18 12:19:25 -0400 | [diff] [blame] | 586 | if (!rq_mergeable(rq) || !bio_mergeable(bio)) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 587 | return false; |
| 588 | |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 589 | if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw)) |
| 590 | return false; |
| 591 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 592 | /* different data direction or already started, don't merge */ |
| 593 | if (bio_data_dir(bio) != rq_data_dir(rq)) |
| 594 | return false; |
| 595 | |
| 596 | /* must be same device and not a special request */ |
Jens Axboe | e7e2450 | 2013-10-29 12:11:47 -0600 | [diff] [blame] | 597 | if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq)) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 598 | return false; |
| 599 | |
| 600 | /* only merge integrity protected bio into ditto rq */ |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 601 | if (blk_integrity_merge_bio(rq->q, rq, bio) == false) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 602 | return false; |
| 603 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 604 | /* must be using the same buffer */ |
| 605 | if (rq->cmd_flags & REQ_WRITE_SAME && |
| 606 | !blk_write_same_mergeable(rq->bio, bio)) |
| 607 | return false; |
| 608 | |
Jens Axboe | 66cb45a | 2014-06-24 16:22:24 -0600 | [diff] [blame] | 609 | if (q->queue_flags & (1 << QUEUE_FLAG_SG_GAPS)) { |
| 610 | struct bio_vec *bprev; |
| 611 | |
| 612 | bprev = &rq->biotail->bi_io_vec[bio->bi_vcnt - 1]; |
| 613 | if (bvec_gap_to_prev(bprev, bio->bi_io_vec[0].bv_offset)) |
| 614 | return false; |
| 615 | } |
| 616 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 617 | return true; |
| 618 | } |
| 619 | |
| 620 | int blk_try_merge(struct request *rq, struct bio *bio) |
| 621 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 622 | if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 623 | return ELEVATOR_BACK_MERGE; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 624 | else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 625 | return ELEVATOR_FRONT_MERGE; |
| 626 | return ELEVATOR_NO_MERGE; |
| 627 | } |