Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Functions related to segment and merge handling |
| 4 | */ |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/bio.h> |
| 8 | #include <linux/blkdev.h> |
Christoph Hellwig | fe45e63 | 2021-09-20 14:33:27 +0200 | [diff] [blame] | 9 | #include <linux/blk-integrity.h> |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 10 | #include <linux/scatterlist.h> |
Christoph Hellwig | 82d981d | 2021-11-23 19:53:12 +0100 | [diff] [blame] | 11 | #include <linux/part_stat.h> |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 12 | |
Mike Krinkin | cda2264 | 2015-12-03 17:32:30 +0300 | [diff] [blame] | 13 | #include <trace/events/block.h> |
| 14 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 15 | #include "blk.h" |
Christoph Hellwig | 2aa7745 | 2021-11-23 19:53:08 +0100 | [diff] [blame] | 16 | #include "blk-mq-sched.h" |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 17 | #include "blk-rq-qos.h" |
Jens Axboe | a7b36ee | 2021-10-05 09:11:56 -0600 | [diff] [blame] | 18 | #include "blk-throttle.h" |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 19 | |
Christoph Hellwig | ff18d77 | 2021-10-12 18:18:03 +0200 | [diff] [blame] | 20 | static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv) |
| 21 | { |
| 22 | *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); |
| 23 | } |
| 24 | |
| 25 | static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv) |
| 26 | { |
| 27 | struct bvec_iter iter = bio->bi_iter; |
| 28 | int idx; |
| 29 | |
| 30 | bio_get_first_bvec(bio, bv); |
| 31 | if (bv->bv_len == bio->bi_iter.bi_size) |
| 32 | return; /* this bio only has a single bvec */ |
| 33 | |
| 34 | bio_advance_iter(bio, &iter, iter.bi_size); |
| 35 | |
| 36 | if (!iter.bi_bvec_done) |
| 37 | idx = iter.bi_idx - 1; |
| 38 | else /* in the middle of bvec */ |
| 39 | idx = iter.bi_idx; |
| 40 | |
| 41 | *bv = bio->bi_io_vec[idx]; |
| 42 | |
| 43 | /* |
| 44 | * iter.bi_bvec_done records actual length of the last bvec |
| 45 | * if this bio ends in the middle of one io vector |
| 46 | */ |
| 47 | if (iter.bi_bvec_done) |
| 48 | bv->bv_len = iter.bi_bvec_done; |
| 49 | } |
| 50 | |
Christoph Hellwig | e990700 | 2018-09-24 09:43:48 +0200 | [diff] [blame] | 51 | static inline bool bio_will_gap(struct request_queue *q, |
| 52 | struct request *prev_rq, struct bio *prev, struct bio *next) |
| 53 | { |
| 54 | struct bio_vec pb, nb; |
| 55 | |
| 56 | if (!bio_has_data(prev) || !queue_virt_boundary(q)) |
| 57 | return false; |
| 58 | |
| 59 | /* |
| 60 | * Don't merge if the 1st bio starts with non-zero offset, otherwise it |
| 61 | * is quite difficult to respect the sg gap limit. We work hard to |
| 62 | * merge a huge number of small single bios in case of mkfs. |
| 63 | */ |
| 64 | if (prev_rq) |
| 65 | bio_get_first_bvec(prev_rq->bio, &pb); |
| 66 | else |
| 67 | bio_get_first_bvec(prev, &pb); |
Johannes Thumshirn | df376b2 | 2018-11-07 14:58:14 +0100 | [diff] [blame] | 68 | if (pb.bv_offset & queue_virt_boundary(q)) |
Christoph Hellwig | e990700 | 2018-09-24 09:43:48 +0200 | [diff] [blame] | 69 | return true; |
| 70 | |
| 71 | /* |
| 72 | * We don't need to worry about the situation that the merged segment |
| 73 | * ends in unaligned virt boundary: |
| 74 | * |
| 75 | * - if 'pb' ends aligned, the merged segment ends aligned |
| 76 | * - if 'pb' ends unaligned, the next bio must include |
| 77 | * one single bvec of 'nb', otherwise the 'nb' can't |
| 78 | * merge with 'pb' |
| 79 | */ |
| 80 | bio_get_last_bvec(prev, &pb); |
| 81 | bio_get_first_bvec(next, &nb); |
Christoph Hellwig | 200a9af | 2019-05-21 09:01:42 +0200 | [diff] [blame] | 82 | if (biovec_phys_mergeable(q, &pb, &nb)) |
Christoph Hellwig | e990700 | 2018-09-24 09:43:48 +0200 | [diff] [blame] | 83 | return false; |
| 84 | return __bvec_gap_to_prev(q, &pb, nb.bv_offset); |
| 85 | } |
| 86 | |
| 87 | static inline bool req_gap_back_merge(struct request *req, struct bio *bio) |
| 88 | { |
| 89 | return bio_will_gap(req->q, req, req->biotail, bio); |
| 90 | } |
| 91 | |
| 92 | static inline bool req_gap_front_merge(struct request *req, struct bio *bio) |
| 93 | { |
| 94 | return bio_will_gap(req->q, NULL, bio, req->bio); |
| 95 | } |
| 96 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 97 | static struct bio *blk_bio_discard_split(struct request_queue *q, |
| 98 | struct bio *bio, |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 99 | struct bio_set *bs, |
| 100 | unsigned *nsegs) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 101 | { |
| 102 | unsigned int max_discard_sectors, granularity; |
| 103 | int alignment; |
| 104 | sector_t tmp; |
| 105 | unsigned split_sectors; |
| 106 | |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 107 | *nsegs = 1; |
| 108 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 109 | /* Zero-sector (unknown) and one-sector granularities are the same. */ |
| 110 | granularity = max(q->limits.discard_granularity >> 9, 1U); |
| 111 | |
Ming Lei | 1adfc5e | 2018-10-29 20:57:17 +0800 | [diff] [blame] | 112 | max_discard_sectors = min(q->limits.max_discard_sectors, |
| 113 | bio_allowed_max_sectors(q)); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 114 | max_discard_sectors -= max_discard_sectors % granularity; |
| 115 | |
| 116 | if (unlikely(!max_discard_sectors)) { |
| 117 | /* XXX: warn */ |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | if (bio_sectors(bio) <= max_discard_sectors) |
| 122 | return NULL; |
| 123 | |
| 124 | split_sectors = max_discard_sectors; |
| 125 | |
| 126 | /* |
| 127 | * If the next starting sector would be misaligned, stop the discard at |
| 128 | * the previous aligned sector. |
| 129 | */ |
| 130 | alignment = (q->limits.discard_alignment >> 9) % granularity; |
| 131 | |
| 132 | tmp = bio->bi_iter.bi_sector + split_sectors - alignment; |
| 133 | tmp = sector_div(tmp, granularity); |
| 134 | |
| 135 | if (split_sectors > tmp) |
| 136 | split_sectors -= tmp; |
| 137 | |
| 138 | return bio_split(bio, split_sectors, GFP_NOIO, bs); |
| 139 | } |
| 140 | |
Christoph Hellwig | 885fa13 | 2017-04-05 19:21:01 +0200 | [diff] [blame] | 141 | static struct bio *blk_bio_write_zeroes_split(struct request_queue *q, |
| 142 | struct bio *bio, struct bio_set *bs, unsigned *nsegs) |
| 143 | { |
Christoph Hellwig | d665e12 | 2019-07-03 05:24:35 -0700 | [diff] [blame] | 144 | *nsegs = 0; |
Christoph Hellwig | 885fa13 | 2017-04-05 19:21:01 +0200 | [diff] [blame] | 145 | |
| 146 | if (!q->limits.max_write_zeroes_sectors) |
| 147 | return NULL; |
| 148 | |
| 149 | if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors) |
| 150 | return NULL; |
| 151 | |
| 152 | return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs); |
| 153 | } |
| 154 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 155 | static struct bio *blk_bio_write_same_split(struct request_queue *q, |
| 156 | struct bio *bio, |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 157 | struct bio_set *bs, |
| 158 | unsigned *nsegs) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 159 | { |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 160 | *nsegs = 1; |
| 161 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 162 | if (!q->limits.max_write_same_sectors) |
| 163 | return NULL; |
| 164 | |
| 165 | if (bio_sectors(bio) <= q->limits.max_write_same_sectors) |
| 166 | return NULL; |
| 167 | |
| 168 | return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs); |
| 169 | } |
| 170 | |
Bart Van Assche | 9cc5169 | 2019-08-01 15:50:44 -0700 | [diff] [blame] | 171 | /* |
| 172 | * Return the maximum number of sectors from the start of a bio that may be |
| 173 | * submitted as a single request to a block device. If enough sectors remain, |
| 174 | * align the end to the physical block size. Otherwise align the end to the |
| 175 | * logical block size. This approach minimizes the number of non-aligned |
| 176 | * requests that are submitted to a block device if the start of a bio is not |
| 177 | * aligned to a physical block boundary. |
| 178 | */ |
Ming Lei | d0e5fbb | 2016-01-23 08:05:33 +0800 | [diff] [blame] | 179 | static inline unsigned get_max_io_size(struct request_queue *q, |
| 180 | struct bio *bio) |
| 181 | { |
Mike Snitzer | 3ee16db | 2020-11-30 10:57:43 -0500 | [diff] [blame] | 182 | unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector, 0); |
Bart Van Assche | 9cc5169 | 2019-08-01 15:50:44 -0700 | [diff] [blame] | 183 | unsigned max_sectors = sectors; |
| 184 | unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT; |
| 185 | unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT; |
| 186 | unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1); |
Ming Lei | d0e5fbb | 2016-01-23 08:05:33 +0800 | [diff] [blame] | 187 | |
Bart Van Assche | 9cc5169 | 2019-08-01 15:50:44 -0700 | [diff] [blame] | 188 | max_sectors += start_offset; |
| 189 | max_sectors &= ~(pbs - 1); |
| 190 | if (max_sectors > start_offset) |
| 191 | return max_sectors - start_offset; |
Ming Lei | d0e5fbb | 2016-01-23 08:05:33 +0800 | [diff] [blame] | 192 | |
Keith Busch | e4b469c | 2020-08-06 14:58:37 -0700 | [diff] [blame] | 193 | return sectors & ~(lbs - 1); |
Ming Lei | d0e5fbb | 2016-01-23 08:05:33 +0800 | [diff] [blame] | 194 | } |
| 195 | |
Ming Lei | 429120f | 2019-12-29 10:32:30 +0800 | [diff] [blame] | 196 | static inline unsigned get_max_segment_size(const struct request_queue *q, |
| 197 | struct page *start_page, |
| 198 | unsigned long offset) |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 199 | { |
| 200 | unsigned long mask = queue_segment_boundary(q); |
| 201 | |
Ming Lei | 429120f | 2019-12-29 10:32:30 +0800 | [diff] [blame] | 202 | offset = mask & (page_to_phys(start_page) + offset); |
Ming Lei | 4a2f704 | 2020-01-11 20:57:43 +0800 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * overflow may be triggered in case of zero page physical address |
| 206 | * on 32bit arch, use queue's max segment size when that happens. |
| 207 | */ |
| 208 | return min_not_zero(mask - offset + 1, |
| 209 | (unsigned long)queue_max_segment_size(q)); |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 210 | } |
| 211 | |
Bart Van Assche | 708b25b | 2019-08-01 15:50:43 -0700 | [diff] [blame] | 212 | /** |
| 213 | * bvec_split_segs - verify whether or not a bvec should be split in the middle |
| 214 | * @q: [in] request queue associated with the bio associated with @bv |
| 215 | * @bv: [in] bvec to examine |
| 216 | * @nsegs: [in,out] Number of segments in the bio being built. Incremented |
| 217 | * by the number of segments from @bv that may be appended to that |
| 218 | * bio without exceeding @max_segs |
| 219 | * @sectors: [in,out] Number of sectors in the bio being built. Incremented |
| 220 | * by the number of sectors from @bv that may be appended to that |
| 221 | * bio without exceeding @max_sectors |
| 222 | * @max_segs: [in] upper bound for *@nsegs |
| 223 | * @max_sectors: [in] upper bound for *@sectors |
| 224 | * |
| 225 | * When splitting a bio, it can happen that a bvec is encountered that is too |
| 226 | * big to fit in a single segment and hence that it has to be split in the |
| 227 | * middle. This function verifies whether or not that should happen. The value |
| 228 | * %true is returned if and only if appending the entire @bv to a bio with |
| 229 | * *@nsegs segments and *@sectors sectors would make that bio unacceptable for |
| 230 | * the block driver. |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 231 | */ |
Bart Van Assche | af2c68f | 2019-08-01 15:50:40 -0700 | [diff] [blame] | 232 | static bool bvec_split_segs(const struct request_queue *q, |
| 233 | const struct bio_vec *bv, unsigned *nsegs, |
Bart Van Assche | 708b25b | 2019-08-01 15:50:43 -0700 | [diff] [blame] | 234 | unsigned *sectors, unsigned max_segs, |
| 235 | unsigned max_sectors) |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 236 | { |
Bart Van Assche | 708b25b | 2019-08-01 15:50:43 -0700 | [diff] [blame] | 237 | unsigned max_len = (min(max_sectors, UINT_MAX >> 9) - *sectors) << 9; |
| 238 | unsigned len = min(bv->bv_len, max_len); |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 239 | unsigned total_len = 0; |
Bart Van Assche | ff9811b | 2019-08-01 15:50:42 -0700 | [diff] [blame] | 240 | unsigned seg_size = 0; |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 241 | |
Bart Van Assche | ff9811b | 2019-08-01 15:50:42 -0700 | [diff] [blame] | 242 | while (len && *nsegs < max_segs) { |
Ming Lei | 429120f | 2019-12-29 10:32:30 +0800 | [diff] [blame] | 243 | seg_size = get_max_segment_size(q, bv->bv_page, |
| 244 | bv->bv_offset + total_len); |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 245 | seg_size = min(seg_size, len); |
| 246 | |
Bart Van Assche | ff9811b | 2019-08-01 15:50:42 -0700 | [diff] [blame] | 247 | (*nsegs)++; |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 248 | total_len += seg_size; |
| 249 | len -= seg_size; |
| 250 | |
| 251 | if ((bv->bv_offset + total_len) & queue_virt_boundary(q)) |
| 252 | break; |
| 253 | } |
| 254 | |
Bart Van Assche | ff9811b | 2019-08-01 15:50:42 -0700 | [diff] [blame] | 255 | *sectors += total_len >> 9; |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 256 | |
Bart Van Assche | 708b25b | 2019-08-01 15:50:43 -0700 | [diff] [blame] | 257 | /* tell the caller to split the bvec if it is too big to fit */ |
| 258 | return len > 0 || bv->bv_len > max_len; |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Bart Van Assche | dad7758 | 2019-08-01 15:50:41 -0700 | [diff] [blame] | 261 | /** |
| 262 | * blk_bio_segment_split - split a bio in two bios |
| 263 | * @q: [in] request queue pointer |
| 264 | * @bio: [in] bio to be split |
| 265 | * @bs: [in] bio set to allocate the clone from |
| 266 | * @segs: [out] number of segments in the bio with the first half of the sectors |
| 267 | * |
| 268 | * Clone @bio, update the bi_iter of the clone to represent the first sectors |
| 269 | * of @bio and update @bio->bi_iter to represent the remaining sectors. The |
| 270 | * following is guaranteed for the cloned bio: |
| 271 | * - That it has at most get_max_io_size(@q, @bio) sectors. |
| 272 | * - That it has at most queue_max_segments(@q) segments. |
| 273 | * |
| 274 | * Except for discard requests the cloned bio will point at the bi_io_vec of |
| 275 | * the original bio. It is the responsibility of the caller to ensure that the |
| 276 | * original bio is not freed before the cloned bio. The caller is also |
| 277 | * responsible for ensuring that @bs is only destroyed after processing of the |
| 278 | * split bio has finished. |
| 279 | */ |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 280 | static struct bio *blk_bio_segment_split(struct request_queue *q, |
| 281 | struct bio *bio, |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 282 | struct bio_set *bs, |
| 283 | unsigned *segs) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 284 | { |
Jens Axboe | 5014c31 | 2015-09-02 16:46:02 -0600 | [diff] [blame] | 285 | struct bio_vec bv, bvprv, *bvprvp = NULL; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 286 | struct bvec_iter iter; |
Christoph Hellwig | 6869875 | 2019-05-21 09:01:43 +0200 | [diff] [blame] | 287 | unsigned nsegs = 0, sectors = 0; |
Ming Lei | d0e5fbb | 2016-01-23 08:05:33 +0800 | [diff] [blame] | 288 | const unsigned max_sectors = get_max_io_size(q, bio); |
Ming Lei | 05b700b | 2019-03-03 21:17:48 +0800 | [diff] [blame] | 289 | const unsigned max_segs = queue_max_segments(q); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 290 | |
Ming Lei | dcebd75 | 2019-02-15 19:13:12 +0800 | [diff] [blame] | 291 | bio_for_each_bvec(bv, bio, iter) { |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 292 | /* |
| 293 | * If the queue doesn't support SG gaps and adding this |
| 294 | * offset would create a gap, disallow it. |
| 295 | */ |
Jens Axboe | 5014c31 | 2015-09-02 16:46:02 -0600 | [diff] [blame] | 296 | if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset)) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 297 | goto split; |
| 298 | |
Bart Van Assche | 708b25b | 2019-08-01 15:50:43 -0700 | [diff] [blame] | 299 | if (nsegs < max_segs && |
| 300 | sectors + (bv.bv_len >> 9) <= max_sectors && |
| 301 | bv.bv_offset + bv.bv_len <= PAGE_SIZE) { |
| 302 | nsegs++; |
| 303 | sectors += bv.bv_len >> 9; |
| 304 | } else if (bvec_split_segs(q, &bv, &nsegs, §ors, max_segs, |
| 305 | max_sectors)) { |
Ming Lei | cf8c0c6 | 2017-12-18 20:22:16 +0800 | [diff] [blame] | 306 | goto split; |
Keith Busch | e36f620 | 2016-01-12 15:08:39 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 309 | bvprv = bv; |
Ming Lei | 578270b | 2015-11-24 10:35:29 +0800 | [diff] [blame] | 310 | bvprvp = &bvprv; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Christoph Hellwig | d627065 | 2019-06-06 12:29:03 +0200 | [diff] [blame] | 313 | *segs = nsegs; |
| 314 | return NULL; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 315 | split: |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 316 | *segs = nsegs; |
Jeffle Xu | cc29e1b | 2020-11-26 17:18:52 +0800 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * Bio splitting may cause subtle trouble such as hang when doing sync |
| 320 | * iopoll in direct IO routine. Given performance gain of iopoll for |
| 321 | * big IO can be trival, disable iopoll when split needed. |
| 322 | */ |
Christoph Hellwig | 6ce913f | 2021-10-12 13:12:21 +0200 | [diff] [blame] | 323 | bio_clear_polled(bio); |
Christoph Hellwig | d627065 | 2019-06-06 12:29:03 +0200 | [diff] [blame] | 324 | return bio_split(bio, sectors, GFP_NOIO, bs); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Bart Van Assche | dad7758 | 2019-08-01 15:50:41 -0700 | [diff] [blame] | 327 | /** |
| 328 | * __blk_queue_split - split a bio and submit the second half |
Jens Axboe | abd45c1 | 2021-10-13 12:43:41 -0600 | [diff] [blame] | 329 | * @q: [in] request_queue new bio is being queued at |
Bart Van Assche | dad7758 | 2019-08-01 15:50:41 -0700 | [diff] [blame] | 330 | * @bio: [in, out] bio to be split |
| 331 | * @nr_segs: [out] number of segments in the first bio |
| 332 | * |
| 333 | * Split a bio into two bios, chain the two bios, submit the second half and |
| 334 | * store a pointer to the first half in *@bio. If the second bio is still too |
| 335 | * big it will be split by a recursive call to this function. Since this |
Christoph Hellwig | 309dca30 | 2021-01-24 11:02:34 +0100 | [diff] [blame] | 336 | * function may allocate a new bio from q->bio_split, it is the responsibility |
| 337 | * of the caller to ensure that q->bio_split is only released after processing |
| 338 | * of the split bio has finished. |
Bart Van Assche | dad7758 | 2019-08-01 15:50:41 -0700 | [diff] [blame] | 339 | */ |
Jens Axboe | abd45c1 | 2021-10-13 12:43:41 -0600 | [diff] [blame] | 340 | void __blk_queue_split(struct request_queue *q, struct bio **bio, |
| 341 | unsigned int *nr_segs) |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 342 | { |
Christoph Hellwig | fa53228 | 2019-11-04 10:05:43 -0800 | [diff] [blame] | 343 | struct bio *split = NULL; |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 344 | |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 345 | switch (bio_op(*bio)) { |
| 346 | case REQ_OP_DISCARD: |
| 347 | case REQ_OP_SECURE_ERASE: |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 348 | split = blk_bio_discard_split(q, *bio, &q->bio_split, nr_segs); |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 349 | break; |
Chaitanya Kulkarni | a6f0788 | 2016-11-30 12:28:59 -0800 | [diff] [blame] | 350 | case REQ_OP_WRITE_ZEROES: |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 351 | split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, |
| 352 | nr_segs); |
Chaitanya Kulkarni | a6f0788 | 2016-11-30 12:28:59 -0800 | [diff] [blame] | 353 | break; |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 354 | case REQ_OP_WRITE_SAME: |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 355 | split = blk_bio_write_same_split(q, *bio, &q->bio_split, |
| 356 | nr_segs); |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 357 | break; |
| 358 | default: |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 359 | split = blk_bio_segment_split(q, *bio, &q->bio_split, nr_segs); |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 360 | break; |
| 361 | } |
Ming Lei | bdced43 | 2015-10-20 23:13:52 +0800 | [diff] [blame] | 362 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 363 | if (split) { |
Ming Lei | 6ac45ae | 2015-10-20 23:13:53 +0800 | [diff] [blame] | 364 | /* there isn't chance to merge the splitted bio */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 365 | split->bi_opf |= REQ_NOMERGE; |
Ming Lei | 6ac45ae | 2015-10-20 23:13:53 +0800 | [diff] [blame] | 366 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 367 | bio_chain(split, *bio); |
Christoph Hellwig | eb6f7f7 | 2020-12-03 17:21:37 +0100 | [diff] [blame] | 368 | trace_block_split(split, (*bio)->bi_iter.bi_sector); |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 369 | submit_bio_noacct(*bio); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 370 | *bio = split; |
Chunguang Xu | 4f1e963 | 2021-08-02 11:51:56 +0800 | [diff] [blame] | 371 | |
| 372 | blk_throtl_charge_bio_split(*bio); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 375 | |
Bart Van Assche | dad7758 | 2019-08-01 15:50:41 -0700 | [diff] [blame] | 376 | /** |
| 377 | * blk_queue_split - split a bio and submit the second half |
Bart Van Assche | dad7758 | 2019-08-01 15:50:41 -0700 | [diff] [blame] | 378 | * @bio: [in, out] bio to be split |
| 379 | * |
| 380 | * Split a bio into two bios, chains the two bios, submit the second half and |
| 381 | * store a pointer to the first half in *@bio. Since this function may allocate |
Christoph Hellwig | 309dca30 | 2021-01-24 11:02:34 +0100 | [diff] [blame] | 382 | * a new bio from q->bio_split, it is the responsibility of the caller to ensure |
| 383 | * that q->bio_split is only released after processing of the split bio has |
| 384 | * finished. |
Bart Van Assche | dad7758 | 2019-08-01 15:50:41 -0700 | [diff] [blame] | 385 | */ |
Christoph Hellwig | f695ca3 | 2020-07-01 10:59:39 +0200 | [diff] [blame] | 386 | void blk_queue_split(struct bio **bio) |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 387 | { |
Pavel Begunkov | 859897c | 2021-10-19 22:24:11 +0100 | [diff] [blame] | 388 | struct request_queue *q = bdev_get_queue((*bio)->bi_bdev); |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 389 | unsigned int nr_segs; |
| 390 | |
Jens Axboe | abd45c1 | 2021-10-13 12:43:41 -0600 | [diff] [blame] | 391 | if (blk_may_split(q, *bio)) |
| 392 | __blk_queue_split(q, bio, &nr_segs); |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 393 | } |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 394 | EXPORT_SYMBOL(blk_queue_split); |
| 395 | |
Christoph Hellwig | e9cd19c | 2019-06-06 12:29:02 +0200 | [diff] [blame] | 396 | unsigned int blk_recalc_rq_segments(struct request *rq) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 397 | { |
Christoph Hellwig | 6869875 | 2019-05-21 09:01:43 +0200 | [diff] [blame] | 398 | unsigned int nr_phys_segs = 0; |
Bart Van Assche | ff9811b | 2019-08-01 15:50:42 -0700 | [diff] [blame] | 399 | unsigned int nr_sectors = 0; |
Christoph Hellwig | e9cd19c | 2019-06-06 12:29:02 +0200 | [diff] [blame] | 400 | struct req_iterator iter; |
Christoph Hellwig | 6869875 | 2019-05-21 09:01:43 +0200 | [diff] [blame] | 401 | struct bio_vec bv; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 402 | |
Christoph Hellwig | e9cd19c | 2019-06-06 12:29:02 +0200 | [diff] [blame] | 403 | if (!rq->bio) |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 404 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 405 | |
Christoph Hellwig | e9cd19c | 2019-06-06 12:29:02 +0200 | [diff] [blame] | 406 | switch (bio_op(rq->bio)) { |
Chaitanya Kulkarni | a6f0788 | 2016-11-30 12:28:59 -0800 | [diff] [blame] | 407 | case REQ_OP_DISCARD: |
| 408 | case REQ_OP_SECURE_ERASE: |
David Jeffery | a958937f | 2021-02-11 09:38:07 -0500 | [diff] [blame] | 409 | if (queue_max_discard_segments(rq->q) > 1) { |
| 410 | struct bio *bio = rq->bio; |
| 411 | |
| 412 | for_each_bio(bio) |
| 413 | nr_phys_segs++; |
| 414 | return nr_phys_segs; |
| 415 | } |
| 416 | return 1; |
Chaitanya Kulkarni | a6f0788 | 2016-11-30 12:28:59 -0800 | [diff] [blame] | 417 | case REQ_OP_WRITE_ZEROES: |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | case REQ_OP_WRITE_SAME: |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 420 | return 1; |
Chaitanya Kulkarni | a6f0788 | 2016-11-30 12:28:59 -0800 | [diff] [blame] | 421 | } |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 422 | |
Christoph Hellwig | e9cd19c | 2019-06-06 12:29:02 +0200 | [diff] [blame] | 423 | rq_for_each_bvec(bv, rq, iter) |
Bart Van Assche | ff9811b | 2019-08-01 15:50:42 -0700 | [diff] [blame] | 424 | bvec_split_segs(rq->q, &bv, &nr_phys_segs, &nr_sectors, |
Bart Van Assche | 708b25b | 2019-08-01 15:50:43 -0700 | [diff] [blame] | 425 | UINT_MAX, UINT_MAX); |
Jens Axboe | 1e42807 | 2009-02-23 09:03:10 +0100 | [diff] [blame] | 426 | return nr_phys_segs; |
| 427 | } |
| 428 | |
Ming Lei | 48d7727 | 2019-02-27 20:40:11 +0800 | [diff] [blame] | 429 | static inline struct scatterlist *blk_next_sg(struct scatterlist **sg, |
Ming Lei | 862e5a5 | 2019-02-15 19:13:13 +0800 | [diff] [blame] | 430 | struct scatterlist *sglist) |
| 431 | { |
| 432 | if (!*sg) |
| 433 | return sglist; |
| 434 | |
| 435 | /* |
| 436 | * If the driver previously mapped a shorter list, we could see a |
| 437 | * termination bit prematurely unless it fully inits the sg table |
| 438 | * on each mapping. We KNOW that there must be more entries here |
| 439 | * or the driver would be buggy, so force clear the termination bit |
| 440 | * to avoid doing a full sg_init_table() in drivers for each command. |
| 441 | */ |
| 442 | sg_unmark_end(*sg); |
| 443 | return sg_next(*sg); |
| 444 | } |
| 445 | |
| 446 | static unsigned blk_bvec_map_sg(struct request_queue *q, |
| 447 | struct bio_vec *bvec, struct scatterlist *sglist, |
| 448 | struct scatterlist **sg) |
| 449 | { |
| 450 | unsigned nbytes = bvec->bv_len; |
Christoph Hellwig | 8a96a0e | 2019-04-11 08:23:27 +0200 | [diff] [blame] | 451 | unsigned nsegs = 0, total = 0; |
Ming Lei | 862e5a5 | 2019-02-15 19:13:13 +0800 | [diff] [blame] | 452 | |
| 453 | while (nbytes > 0) { |
Christoph Hellwig | 8a96a0e | 2019-04-11 08:23:27 +0200 | [diff] [blame] | 454 | unsigned offset = bvec->bv_offset + total; |
Ming Lei | 429120f | 2019-12-29 10:32:30 +0800 | [diff] [blame] | 455 | unsigned len = min(get_max_segment_size(q, bvec->bv_page, |
| 456 | offset), nbytes); |
Christoph Hellwig | f9f7687 | 2019-04-19 08:56:24 +0200 | [diff] [blame] | 457 | struct page *page = bvec->bv_page; |
| 458 | |
| 459 | /* |
| 460 | * Unfortunately a fair number of drivers barf on scatterlists |
| 461 | * that have an offset larger than PAGE_SIZE, despite other |
| 462 | * subsystems dealing with that invariant just fine. For now |
| 463 | * stick to the legacy format where we never present those from |
| 464 | * the block layer, but the code below should be removed once |
| 465 | * these offenders (mostly MMC/SD drivers) are fixed. |
| 466 | */ |
| 467 | page += (offset >> PAGE_SHIFT); |
| 468 | offset &= ~PAGE_MASK; |
Ming Lei | 862e5a5 | 2019-02-15 19:13:13 +0800 | [diff] [blame] | 469 | |
| 470 | *sg = blk_next_sg(sg, sglist); |
Christoph Hellwig | f9f7687 | 2019-04-19 08:56:24 +0200 | [diff] [blame] | 471 | sg_set_page(*sg, page, len, offset); |
Ming Lei | 862e5a5 | 2019-02-15 19:13:13 +0800 | [diff] [blame] | 472 | |
Christoph Hellwig | 8a96a0e | 2019-04-11 08:23:27 +0200 | [diff] [blame] | 473 | total += len; |
| 474 | nbytes -= len; |
Ming Lei | 862e5a5 | 2019-02-15 19:13:13 +0800 | [diff] [blame] | 475 | nsegs++; |
| 476 | } |
| 477 | |
| 478 | return nsegs; |
| 479 | } |
| 480 | |
Ming Lei | 16e3e41 | 2019-03-17 18:01:11 +0800 | [diff] [blame] | 481 | static inline int __blk_bvec_map_sg(struct bio_vec bv, |
| 482 | struct scatterlist *sglist, struct scatterlist **sg) |
| 483 | { |
| 484 | *sg = blk_next_sg(sg, sglist); |
| 485 | sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset); |
| 486 | return 1; |
| 487 | } |
| 488 | |
Ming Lei | f6970f8 | 2019-03-17 18:01:12 +0800 | [diff] [blame] | 489 | /* only try to merge bvecs into one sg if they are from two bios */ |
| 490 | static inline bool |
| 491 | __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec, |
| 492 | struct bio_vec *bvprv, struct scatterlist **sg) |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 493 | { |
| 494 | |
| 495 | int nbytes = bvec->bv_len; |
| 496 | |
Ming Lei | f6970f8 | 2019-03-17 18:01:12 +0800 | [diff] [blame] | 497 | if (!*sg) |
| 498 | return false; |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 499 | |
Ming Lei | f6970f8 | 2019-03-17 18:01:12 +0800 | [diff] [blame] | 500 | if ((*sg)->length + nbytes > queue_max_segment_size(q)) |
| 501 | return false; |
| 502 | |
| 503 | if (!biovec_phys_mergeable(q, bvprv, bvec)) |
| 504 | return false; |
| 505 | |
| 506 | (*sg)->length += nbytes; |
| 507 | |
| 508 | return true; |
Asias He | 963ab9e | 2012-08-02 23:42:03 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 511 | static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio, |
| 512 | struct scatterlist *sglist, |
| 513 | struct scatterlist **sg) |
| 514 | { |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 515 | struct bio_vec bvec, bvprv = { NULL }; |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 516 | struct bvec_iter iter; |
Christoph Hellwig | 3841746 | 2018-12-13 16:17:10 +0100 | [diff] [blame] | 517 | int nsegs = 0; |
Ming Lei | f6970f8 | 2019-03-17 18:01:12 +0800 | [diff] [blame] | 518 | bool new_bio = false; |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 519 | |
Ming Lei | f6970f8 | 2019-03-17 18:01:12 +0800 | [diff] [blame] | 520 | for_each_bio(bio) { |
| 521 | bio_for_each_bvec(bvec, bio, iter) { |
| 522 | /* |
| 523 | * Only try to merge bvecs from two bios given we |
| 524 | * have done bio internal merge when adding pages |
| 525 | * to bio |
| 526 | */ |
| 527 | if (new_bio && |
| 528 | __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg)) |
| 529 | goto next_bvec; |
| 530 | |
| 531 | if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE) |
| 532 | nsegs += __blk_bvec_map_sg(bvec, sglist, sg); |
| 533 | else |
| 534 | nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg); |
| 535 | next_bvec: |
| 536 | new_bio = false; |
| 537 | } |
Ming Lei | b21e11c | 2019-04-02 10:26:44 +0800 | [diff] [blame] | 538 | if (likely(bio->bi_iter.bi_size)) { |
| 539 | bvprv = bvec; |
| 540 | new_bio = true; |
| 541 | } |
Ming Lei | f6970f8 | 2019-03-17 18:01:12 +0800 | [diff] [blame] | 542 | } |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 543 | |
| 544 | return nsegs; |
| 545 | } |
| 546 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 547 | /* |
| 548 | * map a request to scatterlist, return number of sg entries setup. Caller |
| 549 | * must make sure sg can hold rq->nr_phys_segments entries |
| 550 | */ |
Christoph Hellwig | 89de150 | 2020-04-14 09:42:22 +0200 | [diff] [blame] | 551 | int __blk_rq_map_sg(struct request_queue *q, struct request *rq, |
| 552 | struct scatterlist *sglist, struct scatterlist **last_sg) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 553 | { |
Kent Overstreet | 5cb8850 | 2014-02-07 13:53:46 -0700 | [diff] [blame] | 554 | int nsegs = 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 555 | |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 556 | if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) |
Christoph Hellwig | 89de150 | 2020-04-14 09:42:22 +0200 | [diff] [blame] | 557 | nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg); |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 558 | else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME) |
Christoph Hellwig | 89de150 | 2020-04-14 09:42:22 +0200 | [diff] [blame] | 559 | nsegs = __blk_bvec_map_sg(bio_iovec(rq->bio), sglist, last_sg); |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 560 | else if (rq->bio) |
Christoph Hellwig | 89de150 | 2020-04-14 09:42:22 +0200 | [diff] [blame] | 561 | nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg); |
FUJITA Tomonori | f18573a | 2008-04-11 12:56:52 +0200 | [diff] [blame] | 562 | |
Christoph Hellwig | 89de150 | 2020-04-14 09:42:22 +0200 | [diff] [blame] | 563 | if (*last_sg) |
| 564 | sg_mark_end(*last_sg); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 565 | |
Ming Lei | 12e57f5 | 2015-11-24 10:35:31 +0800 | [diff] [blame] | 566 | /* |
| 567 | * Something must have been wrong if the figured number of |
| 568 | * segment is bigger than number of req's physical segments |
| 569 | */ |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 570 | WARN_ON(nsegs > blk_rq_nr_phys_segments(rq)); |
Ming Lei | 12e57f5 | 2015-11-24 10:35:31 +0800 | [diff] [blame] | 571 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 572 | return nsegs; |
| 573 | } |
Christoph Hellwig | 89de150 | 2020-04-14 09:42:22 +0200 | [diff] [blame] | 574 | EXPORT_SYMBOL(__blk_rq_map_sg); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 575 | |
Ming Lei | 943b40c | 2020-08-17 17:52:39 +0800 | [diff] [blame] | 576 | static inline unsigned int blk_rq_get_max_segments(struct request *rq) |
| 577 | { |
| 578 | if (req_op(rq) == REQ_OP_DISCARD) |
| 579 | return queue_max_discard_segments(rq->q); |
| 580 | return queue_max_segments(rq->q); |
| 581 | } |
| 582 | |
Christoph Hellwig | badf7f6 | 2021-09-20 14:33:26 +0200 | [diff] [blame] | 583 | static inline unsigned int blk_rq_get_max_sectors(struct request *rq, |
| 584 | sector_t offset) |
| 585 | { |
| 586 | struct request_queue *q = rq->q; |
| 587 | |
| 588 | if (blk_rq_is_passthrough(rq)) |
| 589 | return q->limits.max_hw_sectors; |
| 590 | |
| 591 | if (!q->limits.chunk_sectors || |
| 592 | req_op(rq) == REQ_OP_DISCARD || |
| 593 | req_op(rq) == REQ_OP_SECURE_ERASE) |
| 594 | return blk_queue_get_max_sectors(q, req_op(rq)); |
| 595 | |
| 596 | return min(blk_max_size_offset(q, offset, 0), |
| 597 | blk_queue_get_max_sectors(q, req_op(rq))); |
| 598 | } |
| 599 | |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 600 | static inline int ll_new_hw_segment(struct request *req, struct bio *bio, |
| 601 | unsigned int nr_phys_segs) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 602 | { |
Ming Lei | 2705dfb | 2021-06-28 10:33:12 +0800 | [diff] [blame] | 603 | if (blk_integrity_merge_bio(req->q, req, bio) == false) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 604 | goto no_merge; |
| 605 | |
Ming Lei | 2705dfb | 2021-06-28 10:33:12 +0800 | [diff] [blame] | 606 | /* discard request merge won't add new segment */ |
| 607 | if (req_op(req) == REQ_OP_DISCARD) |
| 608 | return 1; |
| 609 | |
| 610 | if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 611 | goto no_merge; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * This will form the start of a new hw segment. Bump both |
| 615 | * counters. |
| 616 | */ |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 617 | req->nr_phys_segments += nr_phys_segs; |
| 618 | return 1; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 619 | |
| 620 | no_merge: |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 621 | req_set_nomerge(req->q, req); |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 622 | return 0; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 625 | int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 626 | { |
Jens Axboe | 5e7c427 | 2015-09-03 19:28:20 +0300 | [diff] [blame] | 627 | if (req_gap_back_merge(req, bio)) |
| 628 | return 0; |
Sagi Grimberg | 7f39add | 2015-09-11 09:03:04 -0600 | [diff] [blame] | 629 | if (blk_integrity_rq(req) && |
| 630 | integrity_req_gap_back_merge(req, bio)) |
| 631 | return 0; |
Satya Tangirala | a892c8d | 2020-05-14 00:37:18 +0000 | [diff] [blame] | 632 | if (!bio_crypt_ctx_back_mergeable(req, bio)) |
| 633 | return 0; |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 634 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
Damien Le Moal | 17007f3 | 2016-07-20 21:40:47 -0600 | [diff] [blame] | 635 | blk_rq_get_max_sectors(req, blk_rq_pos(req))) { |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 636 | req_set_nomerge(req->q, req); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 637 | return 0; |
| 638 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 639 | |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 640 | return ll_new_hw_segment(req, bio, nr_segs); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Christoph Hellwig | eda5cc9 | 2020-10-06 09:07:19 +0200 | [diff] [blame] | 643 | static int ll_front_merge_fn(struct request *req, struct bio *bio, |
| 644 | unsigned int nr_segs) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 645 | { |
Jens Axboe | 5e7c427 | 2015-09-03 19:28:20 +0300 | [diff] [blame] | 646 | if (req_gap_front_merge(req, bio)) |
| 647 | return 0; |
Sagi Grimberg | 7f39add | 2015-09-11 09:03:04 -0600 | [diff] [blame] | 648 | if (blk_integrity_rq(req) && |
| 649 | integrity_req_gap_front_merge(req, bio)) |
| 650 | return 0; |
Satya Tangirala | a892c8d | 2020-05-14 00:37:18 +0000 | [diff] [blame] | 651 | if (!bio_crypt_ctx_front_mergeable(req, bio)) |
| 652 | return 0; |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 653 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
Damien Le Moal | 17007f3 | 2016-07-20 21:40:47 -0600 | [diff] [blame] | 654 | blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) { |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 655 | req_set_nomerge(req->q, req); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 656 | return 0; |
| 657 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 658 | |
Christoph Hellwig | 14ccb66 | 2019-06-06 12:29:01 +0200 | [diff] [blame] | 659 | return ll_new_hw_segment(req, bio, nr_segs); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 660 | } |
| 661 | |
Jens Axboe | 445251d | 2018-02-01 14:01:02 -0700 | [diff] [blame] | 662 | static bool req_attempt_discard_merge(struct request_queue *q, struct request *req, |
| 663 | struct request *next) |
| 664 | { |
| 665 | unsigned short segments = blk_rq_nr_discard_segments(req); |
| 666 | |
| 667 | if (segments >= queue_max_discard_segments(q)) |
| 668 | goto no_merge; |
| 669 | if (blk_rq_sectors(req) + bio_sectors(next->bio) > |
| 670 | blk_rq_get_max_sectors(req, blk_rq_pos(req))) |
| 671 | goto no_merge; |
| 672 | |
| 673 | req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next); |
| 674 | return true; |
| 675 | no_merge: |
| 676 | req_set_nomerge(q, req); |
| 677 | return false; |
| 678 | } |
| 679 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 680 | static int ll_merge_requests_fn(struct request_queue *q, struct request *req, |
| 681 | struct request *next) |
| 682 | { |
| 683 | int total_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 684 | |
Jens Axboe | 5e7c427 | 2015-09-03 19:28:20 +0300 | [diff] [blame] | 685 | if (req_gap_back_merge(req, next->bio)) |
Keith Busch | 854fbb9 | 2015-02-11 08:20:13 -0700 | [diff] [blame] | 686 | return 0; |
| 687 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 688 | /* |
| 689 | * Will it become too large? |
| 690 | */ |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 691 | if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > |
Damien Le Moal | 17007f3 | 2016-07-20 21:40:47 -0600 | [diff] [blame] | 692 | blk_rq_get_max_sectors(req, blk_rq_pos(req))) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 693 | return 0; |
| 694 | |
| 695 | total_phys_segments = req->nr_phys_segments + next->nr_phys_segments; |
Ming Lei | 943b40c | 2020-08-17 17:52:39 +0800 | [diff] [blame] | 696 | if (total_phys_segments > blk_rq_get_max_segments(req)) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 697 | return 0; |
| 698 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 699 | if (blk_integrity_merge_rq(q, req, next) == false) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 700 | return 0; |
| 701 | |
Satya Tangirala | a892c8d | 2020-05-14 00:37:18 +0000 | [diff] [blame] | 702 | if (!bio_crypt_ctx_merge_rq(req, next)) |
| 703 | return 0; |
| 704 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 705 | /* Merge is OK... */ |
| 706 | req->nr_phys_segments = total_phys_segments; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 707 | return 1; |
| 708 | } |
| 709 | |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 710 | /** |
| 711 | * blk_rq_set_mixed_merge - mark a request as mixed merge |
| 712 | * @rq: request to mark as mixed merge |
| 713 | * |
| 714 | * Description: |
| 715 | * @rq is about to be mixed merged. Make sure the attributes |
| 716 | * which can be mixed are set in each bio and mark @rq as mixed |
| 717 | * merged. |
| 718 | */ |
| 719 | void blk_rq_set_mixed_merge(struct request *rq) |
| 720 | { |
| 721 | unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; |
| 722 | struct bio *bio; |
| 723 | |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 724 | if (rq->rq_flags & RQF_MIXED_MERGE) |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 725 | return; |
| 726 | |
| 727 | /* |
| 728 | * @rq will no longer represent mixable attributes for all the |
| 729 | * contained bios. It will just track those of the first one. |
| 730 | * Distributes the attributs to each bio. |
| 731 | */ |
| 732 | for (bio = rq->bio; bio; bio = bio->bi_next) { |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 733 | WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) && |
| 734 | (bio->bi_opf & REQ_FAILFAST_MASK) != ff); |
| 735 | bio->bi_opf |= ff; |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 736 | } |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 737 | rq->rq_flags |= RQF_MIXED_MERGE; |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 738 | } |
| 739 | |
Konstantin Khlebnikov | b9c54f5 | 2020-05-27 07:24:15 +0200 | [diff] [blame] | 740 | static void blk_account_io_merge_request(struct request *req) |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 741 | { |
| 742 | if (blk_do_io_stat(req)) { |
Mike Snitzer | 112f158 | 2018-12-06 11:41:18 -0500 | [diff] [blame] | 743 | part_stat_lock(); |
Konstantin Khlebnikov | b9c54f5 | 2020-05-27 07:24:15 +0200 | [diff] [blame] | 744 | part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 745 | part_stat_unlock(); |
| 746 | } |
| 747 | } |
Konstantin Khlebnikov | b9c54f5 | 2020-05-27 07:24:15 +0200 | [diff] [blame] | 748 | |
Eric Biggers | e96c0d8 | 2018-11-14 17:19:46 -0800 | [diff] [blame] | 749 | static enum elv_merge blk_try_req_merge(struct request *req, |
| 750 | struct request *next) |
Jianchao Wang | 69840466 | 2018-10-27 19:52:14 +0800 | [diff] [blame] | 751 | { |
| 752 | if (blk_discard_mergable(req)) |
| 753 | return ELEVATOR_DISCARD_MERGE; |
| 754 | else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next)) |
| 755 | return ELEVATOR_BACK_MERGE; |
| 756 | |
| 757 | return ELEVATOR_NO_MERGE; |
| 758 | } |
Jerome Marchand | 26308ea | 2009-03-27 10:31:51 +0100 | [diff] [blame] | 759 | |
Christoph Hellwig | badf7f6 | 2021-09-20 14:33:26 +0200 | [diff] [blame] | 760 | static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b) |
| 761 | { |
| 762 | if (bio_page(a) == bio_page(b) && bio_offset(a) == bio_offset(b)) |
| 763 | return true; |
| 764 | return false; |
| 765 | } |
| 766 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 767 | /* |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 768 | * For non-mq, this has to be called with the request spinlock acquired. |
| 769 | * For mq with scheduling, the appropriate queue wide lock should be held. |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 770 | */ |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 771 | static struct request *attempt_merge(struct request_queue *q, |
| 772 | struct request *req, struct request *next) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 773 | { |
| 774 | if (!rq_mergeable(req) || !rq_mergeable(next)) |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 775 | return NULL; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 776 | |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 777 | if (req_op(req) != req_op(next)) |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 778 | return NULL; |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 779 | |
Christoph Hellwig | 79bb1db | 2021-11-26 13:17:59 +0100 | [diff] [blame] | 780 | if (rq_data_dir(req) != rq_data_dir(next)) |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 781 | return NULL; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 782 | |
Mike Christie | 8fe0d47 | 2016-06-05 14:32:15 -0500 | [diff] [blame] | 783 | if (req_op(req) == REQ_OP_WRITE_SAME && |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 784 | !blk_write_same_mergeable(req->bio, next->bio)) |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 785 | return NULL; |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 786 | |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 787 | /* |
Jens Axboe | cb6934f | 2017-06-27 09:22:02 -0600 | [diff] [blame] | 788 | * Don't allow merge of different write hints, or for a hint with |
| 789 | * non-hint IO. |
| 790 | */ |
| 791 | if (req->write_hint != next->write_hint) |
| 792 | return NULL; |
| 793 | |
Damien Le Moal | 668ffc0 | 2018-11-20 10:52:37 +0900 | [diff] [blame] | 794 | if (req->ioprio != next->ioprio) |
| 795 | return NULL; |
| 796 | |
Jens Axboe | cb6934f | 2017-06-27 09:22:02 -0600 | [diff] [blame] | 797 | /* |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 798 | * If we are allowed to merge, then append bio list |
| 799 | * from next to rq and release next. merge_requests_fn |
| 800 | * will have updated segment counts, update sector |
Jens Axboe | 445251d | 2018-02-01 14:01:02 -0700 | [diff] [blame] | 801 | * counts here. Handle DISCARDs separately, as they |
| 802 | * have separate settings. |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 803 | */ |
Jianchao Wang | 69840466 | 2018-10-27 19:52:14 +0800 | [diff] [blame] | 804 | |
| 805 | switch (blk_try_req_merge(req, next)) { |
| 806 | case ELEVATOR_DISCARD_MERGE: |
Jens Axboe | 445251d | 2018-02-01 14:01:02 -0700 | [diff] [blame] | 807 | if (!req_attempt_discard_merge(q, req, next)) |
| 808 | return NULL; |
Jianchao Wang | 69840466 | 2018-10-27 19:52:14 +0800 | [diff] [blame] | 809 | break; |
| 810 | case ELEVATOR_BACK_MERGE: |
| 811 | if (!ll_merge_requests_fn(q, req, next)) |
| 812 | return NULL; |
| 813 | break; |
| 814 | default: |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 815 | return NULL; |
Jianchao Wang | 69840466 | 2018-10-27 19:52:14 +0800 | [diff] [blame] | 816 | } |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 817 | |
| 818 | /* |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 819 | * If failfast settings disagree or any of the two is already |
| 820 | * a mixed merge, mark both as mixed before proceeding. This |
| 821 | * makes sure that all involved bios have mixable attributes |
| 822 | * set properly. |
| 823 | */ |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 824 | if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) || |
Tejun Heo | 80a761f | 2009-07-03 17:48:17 +0900 | [diff] [blame] | 825 | (req->cmd_flags & REQ_FAILFAST_MASK) != |
| 826 | (next->cmd_flags & REQ_FAILFAST_MASK)) { |
| 827 | blk_rq_set_mixed_merge(req); |
| 828 | blk_rq_set_mixed_merge(next); |
| 829 | } |
| 830 | |
| 831 | /* |
Omar Sandoval | 522a777 | 2018-05-09 02:08:53 -0700 | [diff] [blame] | 832 | * At this point we have either done a back merge or front merge. We |
| 833 | * need the smaller start_time_ns of the merged requests to be the |
| 834 | * current request for accounting purposes. |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 835 | */ |
Omar Sandoval | 522a777 | 2018-05-09 02:08:53 -0700 | [diff] [blame] | 836 | if (next->start_time_ns < req->start_time_ns) |
| 837 | req->start_time_ns = next->start_time_ns; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 838 | |
| 839 | req->biotail->bi_next = next->bio; |
| 840 | req->biotail = next->biotail; |
| 841 | |
Tejun Heo | a2dec7b | 2009-05-07 22:24:44 +0900 | [diff] [blame] | 842 | req->__data_len += blk_rq_bytes(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 843 | |
Ming Lei | 2a5cf35 | 2018-12-01 00:38:18 +0800 | [diff] [blame] | 844 | if (!blk_discard_mergable(req)) |
Jens Axboe | 445251d | 2018-02-01 14:01:02 -0700 | [diff] [blame] | 845 | elv_merge_requests(q, req, next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 846 | |
Jerome Marchand | 42dad76 | 2009-04-22 14:01:49 +0200 | [diff] [blame] | 847 | /* |
| 848 | * 'next' is going away, so update stats accordingly |
| 849 | */ |
Konstantin Khlebnikov | b9c54f5 | 2020-05-27 07:24:15 +0200 | [diff] [blame] | 850 | blk_account_io_merge_request(next); |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 851 | |
Christoph Hellwig | a54895f | 2020-12-03 17:21:39 +0100 | [diff] [blame] | 852 | trace_block_rq_merge(next); |
Jan Kara | f3bdc62 | 2020-06-17 15:58:23 +0200 | [diff] [blame] | 853 | |
Jens Axboe | e4d750c | 2017-02-03 09:48:28 -0700 | [diff] [blame] | 854 | /* |
| 855 | * ownership of bio passed from next to req, return 'next' for |
| 856 | * the caller to free |
| 857 | */ |
Boaz Harrosh | 1cd96c2 | 2009-03-24 12:35:07 +0100 | [diff] [blame] | 858 | next->bio = NULL; |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 859 | return next; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Christoph Hellwig | eda5cc9 | 2020-10-06 09:07:19 +0200 | [diff] [blame] | 862 | static struct request *attempt_back_merge(struct request_queue *q, |
| 863 | struct request *rq) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 864 | { |
| 865 | struct request *next = elv_latter_request(q, rq); |
| 866 | |
| 867 | if (next) |
| 868 | return attempt_merge(q, rq, next); |
| 869 | |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 870 | return NULL; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 871 | } |
| 872 | |
Christoph Hellwig | eda5cc9 | 2020-10-06 09:07:19 +0200 | [diff] [blame] | 873 | static struct request *attempt_front_merge(struct request_queue *q, |
| 874 | struct request *rq) |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 875 | { |
| 876 | struct request *prev = elv_former_request(q, rq); |
| 877 | |
| 878 | if (prev) |
| 879 | return attempt_merge(q, prev, rq); |
| 880 | |
Jens Axboe | b973cb7 | 2017-02-02 08:54:40 -0700 | [diff] [blame] | 881 | return NULL; |
Jens Axboe | d6d4819 | 2008-01-29 14:04:06 +0100 | [diff] [blame] | 882 | } |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 883 | |
Jan Kara | fd2ef39 | 2021-06-23 11:36:34 +0200 | [diff] [blame] | 884 | /* |
| 885 | * Try to merge 'next' into 'rq'. Return true if the merge happened, false |
| 886 | * otherwise. The caller is responsible for freeing 'next' if the merge |
| 887 | * happened. |
| 888 | */ |
| 889 | bool blk_attempt_req_merge(struct request_queue *q, struct request *rq, |
| 890 | struct request *next) |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 891 | { |
Jan Kara | fd2ef39 | 2021-06-23 11:36:34 +0200 | [diff] [blame] | 892 | return attempt_merge(q, rq, next); |
Jens Axboe | 5e84ea3 | 2011-03-21 10:14:27 +0100 | [diff] [blame] | 893 | } |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 894 | |
| 895 | bool blk_rq_merge_ok(struct request *rq, struct bio *bio) |
| 896 | { |
Martin K. Petersen | e2a60da | 2012-09-18 12:19:25 -0400 | [diff] [blame] | 897 | if (!rq_mergeable(rq) || !bio_mergeable(bio)) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 898 | return false; |
| 899 | |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 900 | if (req_op(rq) != bio_op(bio)) |
Martin K. Petersen | f31dc1c | 2012-09-18 12:19:26 -0400 | [diff] [blame] | 901 | return false; |
| 902 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 903 | /* different data direction or already started, don't merge */ |
| 904 | if (bio_data_dir(bio) != rq_data_dir(rq)) |
| 905 | return false; |
| 906 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 907 | /* only merge integrity protected bio into ditto rq */ |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 908 | if (blk_integrity_merge_bio(rq->q, rq, bio) == false) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 909 | return false; |
| 910 | |
Satya Tangirala | a892c8d | 2020-05-14 00:37:18 +0000 | [diff] [blame] | 911 | /* Only merge if the crypt contexts are compatible */ |
| 912 | if (!bio_crypt_rq_ctx_compatible(rq, bio)) |
| 913 | return false; |
| 914 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 915 | /* must be using the same buffer */ |
Mike Christie | 8fe0d47 | 2016-06-05 14:32:15 -0500 | [diff] [blame] | 916 | if (req_op(rq) == REQ_OP_WRITE_SAME && |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 917 | !blk_write_same_mergeable(rq->bio, bio)) |
| 918 | return false; |
| 919 | |
Jens Axboe | cb6934f | 2017-06-27 09:22:02 -0600 | [diff] [blame] | 920 | /* |
| 921 | * Don't allow merge of different write hints, or for a hint with |
| 922 | * non-hint IO. |
| 923 | */ |
| 924 | if (rq->write_hint != bio->bi_write_hint) |
| 925 | return false; |
| 926 | |
Damien Le Moal | 668ffc0 | 2018-11-20 10:52:37 +0900 | [diff] [blame] | 927 | if (rq->ioprio != bio_prio(bio)) |
| 928 | return false; |
| 929 | |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 930 | return true; |
| 931 | } |
| 932 | |
Christoph Hellwig | 34fe7c0 | 2017-02-08 14:46:48 +0100 | [diff] [blame] | 933 | enum elv_merge blk_try_merge(struct request *rq, struct bio *bio) |
Tejun Heo | 050c8ea | 2012-02-08 09:19:38 +0100 | [diff] [blame] | 934 | { |
Jianchao Wang | 69840466 | 2018-10-27 19:52:14 +0800 | [diff] [blame] | 935 | if (blk_discard_mergable(rq)) |
Christoph Hellwig | 1e73973 | 2017-02-08 14:46:49 +0100 | [diff] [blame] | 936 | return ELEVATOR_DISCARD_MERGE; |
| 937 | else 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] | 938 | return ELEVATOR_BACK_MERGE; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 939 | 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] | 940 | return ELEVATOR_FRONT_MERGE; |
| 941 | return ELEVATOR_NO_MERGE; |
| 942 | } |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 943 | |
| 944 | static void blk_account_io_merge_bio(struct request *req) |
| 945 | { |
| 946 | if (!blk_do_io_stat(req)) |
| 947 | return; |
| 948 | |
| 949 | part_stat_lock(); |
| 950 | part_stat_inc(req->part, merges[op_stat_group(req_op(req))]); |
| 951 | part_stat_unlock(); |
| 952 | } |
| 953 | |
Christoph Hellwig | eda5cc9 | 2020-10-06 09:07:19 +0200 | [diff] [blame] | 954 | enum bio_merge_status { |
| 955 | BIO_MERGE_OK, |
| 956 | BIO_MERGE_NONE, |
| 957 | BIO_MERGE_FAILED, |
| 958 | }; |
| 959 | |
| 960 | static enum bio_merge_status bio_attempt_back_merge(struct request *req, |
| 961 | struct bio *bio, unsigned int nr_segs) |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 962 | { |
| 963 | const int ff = bio->bi_opf & REQ_FAILFAST_MASK; |
| 964 | |
| 965 | if (!ll_back_merge_fn(req, bio, nr_segs)) |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 966 | return BIO_MERGE_FAILED; |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 967 | |
Christoph Hellwig | e8a676d | 2020-12-03 17:21:36 +0100 | [diff] [blame] | 968 | trace_block_bio_backmerge(bio); |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 969 | rq_qos_merge(req->q, req, bio); |
| 970 | |
| 971 | if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) |
| 972 | blk_rq_set_mixed_merge(req); |
| 973 | |
| 974 | req->biotail->bi_next = bio; |
| 975 | req->biotail = bio; |
| 976 | req->__data_len += bio->bi_iter.bi_size; |
| 977 | |
| 978 | bio_crypt_free_ctx(bio); |
| 979 | |
| 980 | blk_account_io_merge_bio(req); |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 981 | return BIO_MERGE_OK; |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 982 | } |
| 983 | |
Christoph Hellwig | eda5cc9 | 2020-10-06 09:07:19 +0200 | [diff] [blame] | 984 | static enum bio_merge_status bio_attempt_front_merge(struct request *req, |
| 985 | struct bio *bio, unsigned int nr_segs) |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 986 | { |
| 987 | const int ff = bio->bi_opf & REQ_FAILFAST_MASK; |
| 988 | |
| 989 | if (!ll_front_merge_fn(req, bio, nr_segs)) |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 990 | return BIO_MERGE_FAILED; |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 991 | |
Christoph Hellwig | e8a676d | 2020-12-03 17:21:36 +0100 | [diff] [blame] | 992 | trace_block_bio_frontmerge(bio); |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 993 | rq_qos_merge(req->q, req, bio); |
| 994 | |
| 995 | if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) |
| 996 | blk_rq_set_mixed_merge(req); |
| 997 | |
| 998 | bio->bi_next = req->bio; |
| 999 | req->bio = bio; |
| 1000 | |
| 1001 | req->__sector = bio->bi_iter.bi_sector; |
| 1002 | req->__data_len += bio->bi_iter.bi_size; |
| 1003 | |
| 1004 | bio_crypt_do_front_merge(req, bio); |
| 1005 | |
| 1006 | blk_account_io_merge_bio(req); |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 1007 | return BIO_MERGE_OK; |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1008 | } |
| 1009 | |
Christoph Hellwig | eda5cc9 | 2020-10-06 09:07:19 +0200 | [diff] [blame] | 1010 | static enum bio_merge_status bio_attempt_discard_merge(struct request_queue *q, |
| 1011 | struct request *req, struct bio *bio) |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1012 | { |
| 1013 | unsigned short segments = blk_rq_nr_discard_segments(req); |
| 1014 | |
| 1015 | if (segments >= queue_max_discard_segments(q)) |
| 1016 | goto no_merge; |
| 1017 | if (blk_rq_sectors(req) + bio_sectors(bio) > |
| 1018 | blk_rq_get_max_sectors(req, blk_rq_pos(req))) |
| 1019 | goto no_merge; |
| 1020 | |
| 1021 | rq_qos_merge(q, req, bio); |
| 1022 | |
| 1023 | req->biotail->bi_next = bio; |
| 1024 | req->biotail = bio; |
| 1025 | req->__data_len += bio->bi_iter.bi_size; |
| 1026 | req->nr_phys_segments = segments + 1; |
| 1027 | |
| 1028 | blk_account_io_merge_bio(req); |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 1029 | return BIO_MERGE_OK; |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1030 | no_merge: |
| 1031 | req_set_nomerge(q, req); |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 1032 | return BIO_MERGE_FAILED; |
| 1033 | } |
| 1034 | |
| 1035 | static enum bio_merge_status blk_attempt_bio_merge(struct request_queue *q, |
| 1036 | struct request *rq, |
| 1037 | struct bio *bio, |
| 1038 | unsigned int nr_segs, |
| 1039 | bool sched_allow_merge) |
| 1040 | { |
| 1041 | if (!blk_rq_merge_ok(rq, bio)) |
| 1042 | return BIO_MERGE_NONE; |
| 1043 | |
| 1044 | switch (blk_try_merge(rq, bio)) { |
| 1045 | case ELEVATOR_BACK_MERGE: |
Baolin Wang | 265600b | 2020-09-02 09:45:25 +0800 | [diff] [blame] | 1046 | if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio)) |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 1047 | return bio_attempt_back_merge(rq, bio, nr_segs); |
| 1048 | break; |
| 1049 | case ELEVATOR_FRONT_MERGE: |
Baolin Wang | 265600b | 2020-09-02 09:45:25 +0800 | [diff] [blame] | 1050 | if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio)) |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 1051 | return bio_attempt_front_merge(rq, bio, nr_segs); |
| 1052 | break; |
| 1053 | case ELEVATOR_DISCARD_MERGE: |
| 1054 | return bio_attempt_discard_merge(q, rq, bio); |
| 1055 | default: |
| 1056 | return BIO_MERGE_NONE; |
| 1057 | } |
| 1058 | |
| 1059 | return BIO_MERGE_FAILED; |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * blk_attempt_plug_merge - try to merge with %current's plugged list |
| 1064 | * @q: request_queue new bio is being queued at |
| 1065 | * @bio: new bio being queued |
| 1066 | * @nr_segs: number of segments in @bio |
Jens Axboe | 87c037d | 2021-10-18 10:07:09 -0600 | [diff] [blame] | 1067 | * from the passed in @q already in the plug list |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1068 | * |
Jens Axboe | d38a9c0 | 2021-10-14 07:24:07 -0600 | [diff] [blame] | 1069 | * Determine whether @bio being queued on @q can be merged with the previous |
| 1070 | * request on %current's plugged list. Returns %true if merge was successful, |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1071 | * otherwise %false. |
| 1072 | * |
| 1073 | * Plugging coalesces IOs from the same issuer for the same purpose without |
| 1074 | * going through @q->queue_lock. As such it's more of an issuing mechanism |
| 1075 | * than scheduling, and the request, while may have elvpriv data, is not |
| 1076 | * added on the elevator at this point. In addition, we don't have |
| 1077 | * reliable access to the elevator outside queue lock. Only check basic |
| 1078 | * merging parameters without querying the elevator. |
| 1079 | * |
| 1080 | * Caller must ensure !blk_queue_nomerges(q) beforehand. |
| 1081 | */ |
| 1082 | bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio, |
Christoph Hellwig | 0c5bcc9 | 2021-11-23 17:04:41 +0100 | [diff] [blame] | 1083 | unsigned int nr_segs) |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1084 | { |
| 1085 | struct blk_plug *plug; |
| 1086 | struct request *rq; |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1087 | |
| 1088 | plug = blk_mq_plug(q, bio); |
Jens Axboe | bc490f8 | 2021-10-18 10:12:12 -0600 | [diff] [blame] | 1089 | if (!plug || rq_list_empty(plug->mq_list)) |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1090 | return false; |
| 1091 | |
Jens Axboe | d38a9c0 | 2021-10-14 07:24:07 -0600 | [diff] [blame] | 1092 | /* check the previously added entry for a quick merge attempt */ |
Jens Axboe | bc490f8 | 2021-10-18 10:12:12 -0600 | [diff] [blame] | 1093 | rq = rq_list_peek(&plug->mq_list); |
Jens Axboe | 87c037d | 2021-10-18 10:07:09 -0600 | [diff] [blame] | 1094 | if (rq->q == q) { |
Ming Lei | a1cb653 | 2021-11-02 21:35:00 +0800 | [diff] [blame] | 1095 | if (blk_attempt_bio_merge(q, rq, bio, nr_segs, false) == |
| 1096 | BIO_MERGE_OK) |
| 1097 | return true; |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1098 | } |
Baolin Wang | 8e75637 | 2020-08-28 10:52:54 +0800 | [diff] [blame] | 1099 | return false; |
| 1100 | } |
Baolin Wang | bdc6a287 | 2020-08-28 10:52:55 +0800 | [diff] [blame] | 1101 | |
| 1102 | /* |
| 1103 | * Iterate list of requests and see if we can merge this bio with any |
| 1104 | * of them. |
| 1105 | */ |
| 1106 | bool blk_bio_list_merge(struct request_queue *q, struct list_head *list, |
| 1107 | struct bio *bio, unsigned int nr_segs) |
| 1108 | { |
| 1109 | struct request *rq; |
| 1110 | int checked = 8; |
| 1111 | |
| 1112 | list_for_each_entry_reverse(rq, list, queuelist) { |
Baolin Wang | bdc6a287 | 2020-08-28 10:52:55 +0800 | [diff] [blame] | 1113 | if (!checked--) |
| 1114 | break; |
| 1115 | |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 1116 | switch (blk_attempt_bio_merge(q, rq, bio, nr_segs, true)) { |
| 1117 | case BIO_MERGE_NONE: |
Baolin Wang | bdc6a287 | 2020-08-28 10:52:55 +0800 | [diff] [blame] | 1118 | continue; |
Baolin Wang | 7d7ca7c | 2020-08-28 10:52:56 +0800 | [diff] [blame] | 1119 | case BIO_MERGE_OK: |
| 1120 | return true; |
| 1121 | case BIO_MERGE_FAILED: |
| 1122 | return false; |
Baolin Wang | bdc6a287 | 2020-08-28 10:52:55 +0800 | [diff] [blame] | 1123 | } |
| 1124 | |
Baolin Wang | bdc6a287 | 2020-08-28 10:52:55 +0800 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | return false; |
| 1128 | } |
| 1129 | EXPORT_SYMBOL_GPL(blk_bio_list_merge); |
Christoph Hellwig | eda5cc9 | 2020-10-06 09:07:19 +0200 | [diff] [blame] | 1130 | |
| 1131 | bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio, |
| 1132 | unsigned int nr_segs, struct request **merged_request) |
| 1133 | { |
| 1134 | struct request *rq; |
| 1135 | |
| 1136 | switch (elv_merge(q, &rq, bio)) { |
| 1137 | case ELEVATOR_BACK_MERGE: |
| 1138 | if (!blk_mq_sched_allow_merge(q, rq, bio)) |
| 1139 | return false; |
| 1140 | if (bio_attempt_back_merge(rq, bio, nr_segs) != BIO_MERGE_OK) |
| 1141 | return false; |
| 1142 | *merged_request = attempt_back_merge(q, rq); |
| 1143 | if (!*merged_request) |
| 1144 | elv_merged_request(q, rq, ELEVATOR_BACK_MERGE); |
| 1145 | return true; |
| 1146 | case ELEVATOR_FRONT_MERGE: |
| 1147 | if (!blk_mq_sched_allow_merge(q, rq, bio)) |
| 1148 | return false; |
| 1149 | if (bio_attempt_front_merge(rq, bio, nr_segs) != BIO_MERGE_OK) |
| 1150 | return false; |
| 1151 | *merged_request = attempt_front_merge(q, rq); |
| 1152 | if (!*merged_request) |
| 1153 | elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE); |
| 1154 | return true; |
| 1155 | case ELEVATOR_DISCARD_MERGE: |
| 1156 | return bio_attempt_discard_merge(q, rq, bio) == BIO_MERGE_OK; |
| 1157 | default: |
| 1158 | return false; |
| 1159 | } |
| 1160 | } |
| 1161 | EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge); |