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