1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d6d48196SJens Axboe /*
3d6d48196SJens Axboe * Functions related to segment and merge handling
4d6d48196SJens Axboe */
5d6d48196SJens Axboe #include <linux/kernel.h>
6d6d48196SJens Axboe #include <linux/module.h>
7d6d48196SJens Axboe #include <linux/bio.h>
8d6d48196SJens Axboe #include <linux/blkdev.h>
9fe45e630SChristoph Hellwig #include <linux/blk-integrity.h>
10d6d48196SJens Axboe #include <linux/scatterlist.h>
1182d981d4SChristoph Hellwig #include <linux/part_stat.h>
126b2b0459STejun Heo #include <linux/blk-cgroup.h>
13d6d48196SJens Axboe
14cda22646SMike Krinkin #include <trace/events/block.h>
15cda22646SMike Krinkin
16d6d48196SJens Axboe #include "blk.h"
172aa7745bSChristoph Hellwig #include "blk-mq-sched.h"
188e756373SBaolin Wang #include "blk-rq-qos.h"
19a7b36ee6SJens Axboe #include "blk-throttle.h"
20d6d48196SJens Axboe
bio_get_first_bvec(struct bio * bio,struct bio_vec * bv)21ff18d77bSChristoph Hellwig static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
22ff18d77bSChristoph Hellwig {
23ff18d77bSChristoph Hellwig *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
24ff18d77bSChristoph Hellwig }
25ff18d77bSChristoph Hellwig
bio_get_last_bvec(struct bio * bio,struct bio_vec * bv)26ff18d77bSChristoph Hellwig static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
27ff18d77bSChristoph Hellwig {
28ff18d77bSChristoph Hellwig struct bvec_iter iter = bio->bi_iter;
29ff18d77bSChristoph Hellwig int idx;
30ff18d77bSChristoph Hellwig
31ff18d77bSChristoph Hellwig bio_get_first_bvec(bio, bv);
32ff18d77bSChristoph Hellwig if (bv->bv_len == bio->bi_iter.bi_size)
33ff18d77bSChristoph Hellwig return; /* this bio only has a single bvec */
34ff18d77bSChristoph Hellwig
35ff18d77bSChristoph Hellwig bio_advance_iter(bio, &iter, iter.bi_size);
36ff18d77bSChristoph Hellwig
37ff18d77bSChristoph Hellwig if (!iter.bi_bvec_done)
38ff18d77bSChristoph Hellwig idx = iter.bi_idx - 1;
39ff18d77bSChristoph Hellwig else /* in the middle of bvec */
40ff18d77bSChristoph Hellwig idx = iter.bi_idx;
41ff18d77bSChristoph Hellwig
42ff18d77bSChristoph Hellwig *bv = bio->bi_io_vec[idx];
43ff18d77bSChristoph Hellwig
44ff18d77bSChristoph Hellwig /*
45ff18d77bSChristoph Hellwig * iter.bi_bvec_done records actual length of the last bvec
46ff18d77bSChristoph Hellwig * if this bio ends in the middle of one io vector
47ff18d77bSChristoph Hellwig */
48ff18d77bSChristoph Hellwig if (iter.bi_bvec_done)
49ff18d77bSChristoph Hellwig bv->bv_len = iter.bi_bvec_done;
50ff18d77bSChristoph Hellwig }
51ff18d77bSChristoph Hellwig
bio_will_gap(struct request_queue * q,struct request * prev_rq,struct bio * prev,struct bio * next)52e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q,
53e9907009SChristoph Hellwig struct request *prev_rq, struct bio *prev, struct bio *next)
54e9907009SChristoph Hellwig {
55e9907009SChristoph Hellwig struct bio_vec pb, nb;
56e9907009SChristoph Hellwig
57e9907009SChristoph Hellwig if (!bio_has_data(prev) || !queue_virt_boundary(q))
58e9907009SChristoph Hellwig return false;
59e9907009SChristoph Hellwig
60e9907009SChristoph Hellwig /*
61e9907009SChristoph Hellwig * Don't merge if the 1st bio starts with non-zero offset, otherwise it
62e9907009SChristoph Hellwig * is quite difficult to respect the sg gap limit. We work hard to
63e9907009SChristoph Hellwig * merge a huge number of small single bios in case of mkfs.
64e9907009SChristoph Hellwig */
65e9907009SChristoph Hellwig if (prev_rq)
66e9907009SChristoph Hellwig bio_get_first_bvec(prev_rq->bio, &pb);
67e9907009SChristoph Hellwig else
68e9907009SChristoph Hellwig bio_get_first_bvec(prev, &pb);
69df376b2eSJohannes Thumshirn if (pb.bv_offset & queue_virt_boundary(q))
70e9907009SChristoph Hellwig return true;
71e9907009SChristoph Hellwig
72e9907009SChristoph Hellwig /*
73e9907009SChristoph Hellwig * We don't need to worry about the situation that the merged segment
74e9907009SChristoph Hellwig * ends in unaligned virt boundary:
75e9907009SChristoph Hellwig *
76e9907009SChristoph Hellwig * - if 'pb' ends aligned, the merged segment ends aligned
77e9907009SChristoph Hellwig * - if 'pb' ends unaligned, the next bio must include
78e9907009SChristoph Hellwig * one single bvec of 'nb', otherwise the 'nb' can't
79e9907009SChristoph Hellwig * merge with 'pb'
80e9907009SChristoph Hellwig */
81e9907009SChristoph Hellwig bio_get_last_bvec(prev, &pb);
82e9907009SChristoph Hellwig bio_get_first_bvec(next, &nb);
83200a9affSChristoph Hellwig if (biovec_phys_mergeable(q, &pb, &nb))
84e9907009SChristoph Hellwig return false;
85c55ddd90SChristoph Hellwig return __bvec_gap_to_prev(&q->limits, &pb, nb.bv_offset);
86e9907009SChristoph Hellwig }
87e9907009SChristoph Hellwig
req_gap_back_merge(struct request * req,struct bio * bio)88e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio)
89e9907009SChristoph Hellwig {
90e9907009SChristoph Hellwig return bio_will_gap(req->q, req, req->biotail, bio);
91e9907009SChristoph Hellwig }
92e9907009SChristoph Hellwig
req_gap_front_merge(struct request * req,struct bio * bio)93e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio)
94e9907009SChristoph Hellwig {
95e9907009SChristoph Hellwig return bio_will_gap(req->q, NULL, bio, req->bio);
96e9907009SChristoph Hellwig }
97e9907009SChristoph Hellwig
98b6dc6198SChristoph Hellwig /*
99b6dc6198SChristoph Hellwig * The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
100b6dc6198SChristoph Hellwig * is defined as 'unsigned int', meantime it has to be aligned to with the
101b6dc6198SChristoph Hellwig * logical block size, which is the minimum accepted unit by hardware.
102b6dc6198SChristoph Hellwig */
bio_allowed_max_sectors(const struct queue_limits * lim)103aa261f20SBart Van Assche static unsigned int bio_allowed_max_sectors(const struct queue_limits *lim)
104b6dc6198SChristoph Hellwig {
105c55ddd90SChristoph Hellwig return round_down(UINT_MAX, lim->logical_block_size) >> SECTOR_SHIFT;
106b6dc6198SChristoph Hellwig }
107b6dc6198SChristoph Hellwig
bio_split_discard(struct bio * bio,const struct queue_limits * lim,unsigned * nsegs,struct bio_set * bs)108aa261f20SBart Van Assche static struct bio *bio_split_discard(struct bio *bio,
109aa261f20SBart Van Assche const struct queue_limits *lim,
1105a97806fSChristoph Hellwig unsigned *nsegs, struct bio_set *bs)
11154efd50bSKent Overstreet {
11254efd50bSKent Overstreet unsigned int max_discard_sectors, granularity;
11354efd50bSKent Overstreet sector_t tmp;
11454efd50bSKent Overstreet unsigned split_sectors;
11554efd50bSKent Overstreet
116bdced438SMing Lei *nsegs = 1;
117bdced438SMing Lei
11854efd50bSKent Overstreet /* Zero-sector (unknown) and one-sector granularities are the same. */
119c55ddd90SChristoph Hellwig granularity = max(lim->discard_granularity >> 9, 1U);
12054efd50bSKent Overstreet
121c55ddd90SChristoph Hellwig max_discard_sectors =
122c55ddd90SChristoph Hellwig min(lim->max_discard_sectors, bio_allowed_max_sectors(lim));
12354efd50bSKent Overstreet max_discard_sectors -= max_discard_sectors % granularity;
12454efd50bSKent Overstreet
12554efd50bSKent Overstreet if (unlikely(!max_discard_sectors)) {
12654efd50bSKent Overstreet /* XXX: warn */
12754efd50bSKent Overstreet return NULL;
12854efd50bSKent Overstreet }
12954efd50bSKent Overstreet
13054efd50bSKent Overstreet if (bio_sectors(bio) <= max_discard_sectors)
13154efd50bSKent Overstreet return NULL;
13254efd50bSKent Overstreet
13354efd50bSKent Overstreet split_sectors = max_discard_sectors;
13454efd50bSKent Overstreet
13554efd50bSKent Overstreet /*
13654efd50bSKent Overstreet * If the next starting sector would be misaligned, stop the discard at
13754efd50bSKent Overstreet * the previous aligned sector.
13854efd50bSKent Overstreet */
139c55ddd90SChristoph Hellwig tmp = bio->bi_iter.bi_sector + split_sectors -
140c55ddd90SChristoph Hellwig ((lim->discard_alignment >> 9) % granularity);
14154efd50bSKent Overstreet tmp = sector_div(tmp, granularity);
14254efd50bSKent Overstreet
14354efd50bSKent Overstreet if (split_sectors > tmp)
14454efd50bSKent Overstreet split_sectors -= tmp;
14554efd50bSKent Overstreet
14654efd50bSKent Overstreet return bio_split(bio, split_sectors, GFP_NOIO, bs);
14754efd50bSKent Overstreet }
14854efd50bSKent Overstreet
bio_split_write_zeroes(struct bio * bio,const struct queue_limits * lim,unsigned * nsegs,struct bio_set * bs)1495a97806fSChristoph Hellwig static struct bio *bio_split_write_zeroes(struct bio *bio,
150aa261f20SBart Van Assche const struct queue_limits *lim,
151aa261f20SBart Van Assche unsigned *nsegs, struct bio_set *bs)
152885fa13fSChristoph Hellwig {
153d665e12aSChristoph Hellwig *nsegs = 0;
154c55ddd90SChristoph Hellwig if (!lim->max_write_zeroes_sectors)
155885fa13fSChristoph Hellwig return NULL;
156c55ddd90SChristoph Hellwig if (bio_sectors(bio) <= lim->max_write_zeroes_sectors)
157885fa13fSChristoph Hellwig return NULL;
158c55ddd90SChristoph Hellwig return bio_split(bio, lim->max_write_zeroes_sectors, GFP_NOIO, bs);
159885fa13fSChristoph Hellwig }
160885fa13fSChristoph Hellwig
1619cc5169cSBart Van Assche /*
1629cc5169cSBart Van Assche * Return the maximum number of sectors from the start of a bio that may be
1639cc5169cSBart Van Assche * submitted as a single request to a block device. If enough sectors remain,
1649cc5169cSBart Van Assche * align the end to the physical block size. Otherwise align the end to the
1659cc5169cSBart Van Assche * logical block size. This approach minimizes the number of non-aligned
1669cc5169cSBart Van Assche * requests that are submitted to a block device if the start of a bio is not
1679cc5169cSBart Van Assche * aligned to a physical block boundary.
1689cc5169cSBart Van Assche */
get_max_io_size(struct bio * bio,const struct queue_limits * lim)1695a97806fSChristoph Hellwig static inline unsigned get_max_io_size(struct bio *bio,
170aa261f20SBart Van Assche const struct queue_limits *lim)
171d0e5fbb0SMing Lei {
172c55ddd90SChristoph Hellwig unsigned pbs = lim->physical_block_size >> SECTOR_SHIFT;
173c55ddd90SChristoph Hellwig unsigned lbs = lim->logical_block_size >> SECTOR_SHIFT;
174c55ddd90SChristoph Hellwig unsigned max_sectors = lim->max_sectors, start, end;
175d0e5fbb0SMing Lei
176c55ddd90SChristoph Hellwig if (lim->chunk_sectors) {
177efef739dSChristoph Hellwig max_sectors = min(max_sectors,
178efef739dSChristoph Hellwig blk_chunk_sectors_left(bio->bi_iter.bi_sector,
179c55ddd90SChristoph Hellwig lim->chunk_sectors));
180efef739dSChristoph Hellwig }
181d0e5fbb0SMing Lei
18284613bedSChristoph Hellwig start = bio->bi_iter.bi_sector & (pbs - 1);
18384613bedSChristoph Hellwig end = (start + max_sectors) & ~(pbs - 1);
18484613bedSChristoph Hellwig if (end > start)
18584613bedSChristoph Hellwig return end - start;
18684613bedSChristoph Hellwig return max_sectors & ~(lbs - 1);
187d0e5fbb0SMing Lei }
188d0e5fbb0SMing Lei
18995465318SBart Van Assche /**
19095465318SBart Van Assche * get_max_segment_size() - maximum number of bytes to add as a single segment
19195465318SBart Van Assche * @lim: Request queue limits.
19295465318SBart Van Assche * @start_page: See below.
19395465318SBart Van Assche * @offset: Offset from @start_page where to add a segment.
19495465318SBart Van Assche *
19595465318SBart Van Assche * Returns the maximum number of bytes that can be added as a single segment.
19695465318SBart Van Assche */
get_max_segment_size(const struct queue_limits * lim,struct page * start_page,unsigned long offset)197aa261f20SBart Van Assche static inline unsigned get_max_segment_size(const struct queue_limits *lim,
198c55ddd90SChristoph Hellwig struct page *start_page, unsigned long offset)
199dcebd755SMing Lei {
200c55ddd90SChristoph Hellwig unsigned long mask = lim->seg_boundary_mask;
201dcebd755SMing Lei
202429120f3SMing Lei offset = mask & (page_to_phys(start_page) + offset);
2034a2f704eSMing Lei
2044a2f704eSMing Lei /*
20595465318SBart Van Assche * Prevent an overflow if mask = ULONG_MAX and offset = 0 by adding 1
20695465318SBart Van Assche * after having calculated the minimum.
2074a2f704eSMing Lei */
20895465318SBart Van Assche return min(mask - offset, (unsigned long)lim->max_segment_size - 1) + 1;
209dcebd755SMing Lei }
210dcebd755SMing Lei
211708b25b3SBart Van Assche /**
212708b25b3SBart Van Assche * bvec_split_segs - verify whether or not a bvec should be split in the middle
213c55ddd90SChristoph Hellwig * @lim: [in] queue limits to split based on
214708b25b3SBart Van Assche * @bv: [in] bvec to examine
215708b25b3SBart Van Assche * @nsegs: [in,out] Number of segments in the bio being built. Incremented
216708b25b3SBart Van Assche * by the number of segments from @bv that may be appended to that
217708b25b3SBart Van Assche * bio without exceeding @max_segs
21867927d22SKeith Busch * @bytes: [in,out] Number of bytes in the bio being built. Incremented
21967927d22SKeith Busch * by the number of bytes from @bv that may be appended to that
22067927d22SKeith Busch * bio without exceeding @max_bytes
221708b25b3SBart Van Assche * @max_segs: [in] upper bound for *@nsegs
22267927d22SKeith Busch * @max_bytes: [in] upper bound for *@bytes
223708b25b3SBart Van Assche *
224708b25b3SBart Van Assche * When splitting a bio, it can happen that a bvec is encountered that is too
225708b25b3SBart Van Assche * big to fit in a single segment and hence that it has to be split in the
226708b25b3SBart Van Assche * middle. This function verifies whether or not that should happen. The value
227708b25b3SBart Van Assche * %true is returned if and only if appending the entire @bv to a bio with
228708b25b3SBart Van Assche * *@nsegs segments and *@sectors sectors would make that bio unacceptable for
229708b25b3SBart Van Assche * the block driver.
230dcebd755SMing Lei */
bvec_split_segs(const struct queue_limits * lim,const struct bio_vec * bv,unsigned * nsegs,unsigned * bytes,unsigned max_segs,unsigned max_bytes)231aa261f20SBart Van Assche static bool bvec_split_segs(const struct queue_limits *lim,
232aa261f20SBart Van Assche const struct bio_vec *bv, unsigned *nsegs, unsigned *bytes,
233aa261f20SBart Van Assche unsigned max_segs, unsigned max_bytes)
234dcebd755SMing Lei {
23567927d22SKeith Busch unsigned max_len = min(max_bytes, UINT_MAX) - *bytes;
236708b25b3SBart Van Assche unsigned len = min(bv->bv_len, max_len);
237dcebd755SMing Lei unsigned total_len = 0;
238ff9811b3SBart Van Assche unsigned seg_size = 0;
239dcebd755SMing Lei
240ff9811b3SBart Van Assche while (len && *nsegs < max_segs) {
241c55ddd90SChristoph Hellwig seg_size = get_max_segment_size(lim, bv->bv_page,
242429120f3SMing Lei bv->bv_offset + total_len);
243dcebd755SMing Lei seg_size = min(seg_size, len);
244dcebd755SMing Lei
245ff9811b3SBart Van Assche (*nsegs)++;
246dcebd755SMing Lei total_len += seg_size;
247dcebd755SMing Lei len -= seg_size;
248dcebd755SMing Lei
249c55ddd90SChristoph Hellwig if ((bv->bv_offset + total_len) & lim->virt_boundary_mask)
250dcebd755SMing Lei break;
251dcebd755SMing Lei }
252dcebd755SMing Lei
25367927d22SKeith Busch *bytes += total_len;
254dcebd755SMing Lei
255708b25b3SBart Van Assche /* tell the caller to split the bvec if it is too big to fit */
256708b25b3SBart Van Assche return len > 0 || bv->bv_len > max_len;
257dcebd755SMing Lei }
258dcebd755SMing Lei
bio_split_alignment(struct bio * bio,const struct queue_limits * lim)259*fad4262bSChristoph Hellwig static unsigned int bio_split_alignment(struct bio *bio,
260*fad4262bSChristoph Hellwig const struct queue_limits *lim)
261*fad4262bSChristoph Hellwig {
262*fad4262bSChristoph Hellwig if (op_is_write(bio_op(bio)) && lim->zone_write_granularity)
263*fad4262bSChristoph Hellwig return lim->zone_write_granularity;
264*fad4262bSChristoph Hellwig return lim->logical_block_size;
265*fad4262bSChristoph Hellwig }
266*fad4262bSChristoph Hellwig
267dad77584SBart Van Assche /**
2685a97806fSChristoph Hellwig * bio_split_rw - split a bio in two bios
269dad77584SBart Van Assche * @bio: [in] bio to be split
270c55ddd90SChristoph Hellwig * @lim: [in] queue limits to split based on
271dad77584SBart Van Assche * @segs: [out] number of segments in the bio with the first half of the sectors
2725a97806fSChristoph Hellwig * @bs: [in] bio set to allocate the clone from
273a85b3637SChristoph Hellwig * @max_bytes: [in] maximum number of bytes per bio
274dad77584SBart Van Assche *
275dad77584SBart Van Assche * Clone @bio, update the bi_iter of the clone to represent the first sectors
276dad77584SBart Van Assche * of @bio and update @bio->bi_iter to represent the remaining sectors. The
277dad77584SBart Van Assche * following is guaranteed for the cloned bio:
278a85b3637SChristoph Hellwig * - That it has at most @max_bytes worth of data
279dad77584SBart Van Assche * - That it has at most queue_max_segments(@q) segments.
280dad77584SBart Van Assche *
281dad77584SBart Van Assche * Except for discard requests the cloned bio will point at the bi_io_vec of
282dad77584SBart Van Assche * the original bio. It is the responsibility of the caller to ensure that the
283dad77584SBart Van Assche * original bio is not freed before the cloned bio. The caller is also
284dad77584SBart Van Assche * responsible for ensuring that @bs is only destroyed after processing of the
285dad77584SBart Van Assche * split bio has finished.
286dad77584SBart Van Assche */
bio_split_rw(struct bio * bio,const struct queue_limits * lim,unsigned * segs,struct bio_set * bs,unsigned max_bytes)287fd8f8edeSChristoph Hellwig struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim,
288a85b3637SChristoph Hellwig unsigned *segs, struct bio_set *bs, unsigned max_bytes)
28954efd50bSKent Overstreet {
2905014c311SJens Axboe struct bio_vec bv, bvprv, *bvprvp = NULL;
29154efd50bSKent Overstreet struct bvec_iter iter;
29267927d22SKeith Busch unsigned nsegs = 0, bytes = 0;
29354efd50bSKent Overstreet
294dcebd755SMing Lei bio_for_each_bvec(bv, bio, iter) {
29554efd50bSKent Overstreet /*
29654efd50bSKent Overstreet * If the queue doesn't support SG gaps and adding this
29754efd50bSKent Overstreet * offset would create a gap, disallow it.
29854efd50bSKent Overstreet */
299c55ddd90SChristoph Hellwig if (bvprvp && bvec_gap_to_prev(lim, bvprvp, bv.bv_offset))
30054efd50bSKent Overstreet goto split;
30154efd50bSKent Overstreet
302c55ddd90SChristoph Hellwig if (nsegs < lim->max_segments &&
30367927d22SKeith Busch bytes + bv.bv_len <= max_bytes &&
304708b25b3SBart Van Assche bv.bv_offset + bv.bv_len <= PAGE_SIZE) {
305708b25b3SBart Van Assche nsegs++;
30667927d22SKeith Busch bytes += bv.bv_len;
307c55ddd90SChristoph Hellwig } else {
308c55ddd90SChristoph Hellwig if (bvec_split_segs(lim, &bv, &nsegs, &bytes,
309c55ddd90SChristoph Hellwig lim->max_segments, max_bytes))
310e36f6204SKeith Busch goto split;
311e36f6204SKeith Busch }
312e36f6204SKeith Busch
31354efd50bSKent Overstreet bvprv = bv;
314578270bfSMing Lei bvprvp = &bvprv;
31554efd50bSKent Overstreet }
31654efd50bSKent Overstreet
317d627065dSChristoph Hellwig *segs = nsegs;
318d627065dSChristoph Hellwig return NULL;
31954efd50bSKent Overstreet split:
3209cea62b2SJens Axboe /*
3219cea62b2SJens Axboe * We can't sanely support splitting for a REQ_NOWAIT bio. End it
3229cea62b2SJens Axboe * with EAGAIN if splitting is required and return an error pointer.
3239cea62b2SJens Axboe */
3249cea62b2SJens Axboe if (bio->bi_opf & REQ_NOWAIT) {
3259cea62b2SJens Axboe bio->bi_status = BLK_STS_AGAIN;
3269cea62b2SJens Axboe bio_endio(bio);
3279cea62b2SJens Axboe return ERR_PTR(-EAGAIN);
3289cea62b2SJens Axboe }
3299cea62b2SJens Axboe
330bdced438SMing Lei *segs = nsegs;
331cc29e1bfSJeffle Xu
332cc29e1bfSJeffle Xu /*
33367927d22SKeith Busch * Individual bvecs might not be logical block aligned. Round down the
33467927d22SKeith Busch * split size so that each bio is properly block size aligned, even if
33567927d22SKeith Busch * we do not use the full hardware limits.
33667927d22SKeith Busch */
337*fad4262bSChristoph Hellwig bytes = ALIGN_DOWN(bytes, bio_split_alignment(bio, lim));
33867927d22SKeith Busch
33967927d22SKeith Busch /*
340cc29e1bfSJeffle Xu * Bio splitting may cause subtle trouble such as hang when doing sync
341cc29e1bfSJeffle Xu * iopoll in direct IO routine. Given performance gain of iopoll for
342cc29e1bfSJeffle Xu * big IO can be trival, disable iopoll when split needed.
343cc29e1bfSJeffle Xu */
3446ce913feSChristoph Hellwig bio_clear_polled(bio);
34567927d22SKeith Busch return bio_split(bio, bytes >> SECTOR_SHIFT, GFP_NOIO, bs);
34654efd50bSKent Overstreet }
347fd8f8edeSChristoph Hellwig EXPORT_SYMBOL_GPL(bio_split_rw);
34854efd50bSKent Overstreet
349dad77584SBart Van Assche /**
3505a97806fSChristoph Hellwig * __bio_split_to_limits - split a bio to fit the queue limits
3515a97806fSChristoph Hellwig * @bio: bio to be split
352c55ddd90SChristoph Hellwig * @lim: queue limits to split based on
3535a97806fSChristoph Hellwig * @nr_segs: returns the number of segments in the returned bio
354dad77584SBart Van Assche *
3555a97806fSChristoph Hellwig * Check if @bio needs splitting based on the queue limits, and if so split off
3565a97806fSChristoph Hellwig * a bio fitting the limits from the beginning of @bio and return it. @bio is
3575a97806fSChristoph Hellwig * shortened to the remainder and re-submitted.
3585a97806fSChristoph Hellwig *
3595a97806fSChristoph Hellwig * The split bio is allocated from @q->bio_split, which is provided by the
3605a97806fSChristoph Hellwig * block layer.
361dad77584SBart Van Assche */
__bio_split_to_limits(struct bio * bio,const struct queue_limits * lim,unsigned int * nr_segs)362aa261f20SBart Van Assche struct bio *__bio_split_to_limits(struct bio *bio,
363aa261f20SBart Van Assche const struct queue_limits *lim,
364abd45c15SJens Axboe unsigned int *nr_segs)
36554efd50bSKent Overstreet {
36646754bd0SChristoph Hellwig struct bio_set *bs = &bio->bi_bdev->bd_disk->bio_split;
3675a97806fSChristoph Hellwig struct bio *split;
36854efd50bSKent Overstreet
3695a97806fSChristoph Hellwig switch (bio_op(bio)) {
3707afafc8aSAdrian Hunter case REQ_OP_DISCARD:
3717afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE:
372c55ddd90SChristoph Hellwig split = bio_split_discard(bio, lim, nr_segs, bs);
3737afafc8aSAdrian Hunter break;
374a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES:
375c55ddd90SChristoph Hellwig split = bio_split_write_zeroes(bio, lim, nr_segs, bs);
376a6f0788eSChaitanya Kulkarni break;
3777afafc8aSAdrian Hunter default:
378c55ddd90SChristoph Hellwig split = bio_split_rw(bio, lim, nr_segs, bs,
379c55ddd90SChristoph Hellwig get_max_io_size(bio, lim) << SECTOR_SHIFT);
380613b1488SJens Axboe if (IS_ERR(split))
381613b1488SJens Axboe return NULL;
3827afafc8aSAdrian Hunter break;
3837afafc8aSAdrian Hunter }
384bdced438SMing Lei
38554efd50bSKent Overstreet if (split) {
386613b1488SJens Axboe /* there isn't chance to merge the split bio */
3871eff9d32SJens Axboe split->bi_opf |= REQ_NOMERGE;
3886ac45aebSMing Lei
389957a2b34SMuchun Song blkcg_bio_issue_init(split);
3905a97806fSChristoph Hellwig bio_chain(split, bio);
3915a97806fSChristoph Hellwig trace_block_split(split, bio->bi_iter.bi_sector);
3925a97806fSChristoph Hellwig submit_bio_noacct(bio);
3935a97806fSChristoph Hellwig return split;
39454efd50bSKent Overstreet }
3955a97806fSChristoph Hellwig return bio;
39654efd50bSKent Overstreet }
39714ccb66bSChristoph Hellwig
398dad77584SBart Van Assche /**
3995a97806fSChristoph Hellwig * bio_split_to_limits - split a bio to fit the queue limits
4005a97806fSChristoph Hellwig * @bio: bio to be split
401dad77584SBart Van Assche *
4025a97806fSChristoph Hellwig * Check if @bio needs splitting based on the queue limits of @bio->bi_bdev, and
4035a97806fSChristoph Hellwig * if so split off a bio fitting the limits from the beginning of @bio and
4045a97806fSChristoph Hellwig * return it. @bio is shortened to the remainder and re-submitted.
4055a97806fSChristoph Hellwig *
4065a97806fSChristoph Hellwig * The split bio is allocated from @q->bio_split, which is provided by the
4075a97806fSChristoph Hellwig * block layer.
408dad77584SBart Van Assche */
bio_split_to_limits(struct bio * bio)4095a97806fSChristoph Hellwig struct bio *bio_split_to_limits(struct bio *bio)
41014ccb66bSChristoph Hellwig {
411aa261f20SBart Van Assche const struct queue_limits *lim = &bdev_get_queue(bio->bi_bdev)->limits;
41214ccb66bSChristoph Hellwig unsigned int nr_segs;
41314ccb66bSChristoph Hellwig
414c55ddd90SChristoph Hellwig if (bio_may_exceed_limits(bio, lim))
415c55ddd90SChristoph Hellwig return __bio_split_to_limits(bio, lim, &nr_segs);
4165a97806fSChristoph Hellwig return bio;
41714ccb66bSChristoph Hellwig }
4185a97806fSChristoph Hellwig EXPORT_SYMBOL(bio_split_to_limits);
41954efd50bSKent Overstreet
blk_recalc_rq_segments(struct request * rq)420e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq)
421d6d48196SJens Axboe {
4226869875fSChristoph Hellwig unsigned int nr_phys_segs = 0;
42367927d22SKeith Busch unsigned int bytes = 0;
424e9cd19c0SChristoph Hellwig struct req_iterator iter;
4256869875fSChristoph Hellwig struct bio_vec bv;
426d6d48196SJens Axboe
427e9cd19c0SChristoph Hellwig if (!rq->bio)
4281e428079SJens Axboe return 0;
429d6d48196SJens Axboe
430e9cd19c0SChristoph Hellwig switch (bio_op(rq->bio)) {
431a6f0788eSChaitanya Kulkarni case REQ_OP_DISCARD:
432a6f0788eSChaitanya Kulkarni case REQ_OP_SECURE_ERASE:
433a958937fSDavid Jeffery if (queue_max_discard_segments(rq->q) > 1) {
434a958937fSDavid Jeffery struct bio *bio = rq->bio;
435a958937fSDavid Jeffery
436a958937fSDavid Jeffery for_each_bio(bio)
437a958937fSDavid Jeffery nr_phys_segs++;
438a958937fSDavid Jeffery return nr_phys_segs;
439a958937fSDavid Jeffery }
440a958937fSDavid Jeffery return 1;
441a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES:
442f9d03f96SChristoph Hellwig return 0;
4432d9b02beSBart Van Assche default:
4442d9b02beSBart Van Assche break;
445a6f0788eSChaitanya Kulkarni }
4465cb8850cSKent Overstreet
447e9cd19c0SChristoph Hellwig rq_for_each_bvec(bv, rq, iter)
448c55ddd90SChristoph Hellwig bvec_split_segs(&rq->q->limits, &bv, &nr_phys_segs, &bytes,
449708b25b3SBart Van Assche UINT_MAX, UINT_MAX);
4501e428079SJens Axboe return nr_phys_segs;
4511e428079SJens Axboe }
4521e428079SJens Axboe
blk_next_sg(struct scatterlist ** sg,struct scatterlist * sglist)45348d7727cSMing Lei static inline struct scatterlist *blk_next_sg(struct scatterlist **sg,
454862e5a5eSMing Lei struct scatterlist *sglist)
455862e5a5eSMing Lei {
456862e5a5eSMing Lei if (!*sg)
457862e5a5eSMing Lei return sglist;
458862e5a5eSMing Lei
459862e5a5eSMing Lei /*
460862e5a5eSMing Lei * If the driver previously mapped a shorter list, we could see a
461862e5a5eSMing Lei * termination bit prematurely unless it fully inits the sg table
462862e5a5eSMing Lei * on each mapping. We KNOW that there must be more entries here
463862e5a5eSMing Lei * or the driver would be buggy, so force clear the termination bit
464862e5a5eSMing Lei * to avoid doing a full sg_init_table() in drivers for each command.
465862e5a5eSMing Lei */
466862e5a5eSMing Lei sg_unmark_end(*sg);
467862e5a5eSMing Lei return sg_next(*sg);
468862e5a5eSMing Lei }
469862e5a5eSMing Lei
blk_bvec_map_sg(struct request_queue * q,struct bio_vec * bvec,struct scatterlist * sglist,struct scatterlist ** sg)470862e5a5eSMing Lei static unsigned blk_bvec_map_sg(struct request_queue *q,
471862e5a5eSMing Lei struct bio_vec *bvec, struct scatterlist *sglist,
472862e5a5eSMing Lei struct scatterlist **sg)
473862e5a5eSMing Lei {
474862e5a5eSMing Lei unsigned nbytes = bvec->bv_len;
4758a96a0e4SChristoph Hellwig unsigned nsegs = 0, total = 0;
476862e5a5eSMing Lei
477862e5a5eSMing Lei while (nbytes > 0) {
4788a96a0e4SChristoph Hellwig unsigned offset = bvec->bv_offset + total;
479c55ddd90SChristoph Hellwig unsigned len = min(get_max_segment_size(&q->limits,
480c55ddd90SChristoph Hellwig bvec->bv_page, offset), nbytes);
481f9f76879SChristoph Hellwig struct page *page = bvec->bv_page;
482f9f76879SChristoph Hellwig
483f9f76879SChristoph Hellwig /*
484f9f76879SChristoph Hellwig * Unfortunately a fair number of drivers barf on scatterlists
485f9f76879SChristoph Hellwig * that have an offset larger than PAGE_SIZE, despite other
486f9f76879SChristoph Hellwig * subsystems dealing with that invariant just fine. For now
487f9f76879SChristoph Hellwig * stick to the legacy format where we never present those from
488f9f76879SChristoph Hellwig * the block layer, but the code below should be removed once
489f9f76879SChristoph Hellwig * these offenders (mostly MMC/SD drivers) are fixed.
490f9f76879SChristoph Hellwig */
491f9f76879SChristoph Hellwig page += (offset >> PAGE_SHIFT);
492f9f76879SChristoph Hellwig offset &= ~PAGE_MASK;
493862e5a5eSMing Lei
494862e5a5eSMing Lei *sg = blk_next_sg(sg, sglist);
495f9f76879SChristoph Hellwig sg_set_page(*sg, page, len, offset);
496862e5a5eSMing Lei
4978a96a0e4SChristoph Hellwig total += len;
4988a96a0e4SChristoph Hellwig nbytes -= len;
499862e5a5eSMing Lei nsegs++;
500862e5a5eSMing Lei }
501862e5a5eSMing Lei
502862e5a5eSMing Lei return nsegs;
503862e5a5eSMing Lei }
504862e5a5eSMing Lei
__blk_bvec_map_sg(struct bio_vec bv,struct scatterlist * sglist,struct scatterlist ** sg)50516e3e418SMing Lei static inline int __blk_bvec_map_sg(struct bio_vec bv,
50616e3e418SMing Lei struct scatterlist *sglist, struct scatterlist **sg)
50716e3e418SMing Lei {
50816e3e418SMing Lei *sg = blk_next_sg(sg, sglist);
50916e3e418SMing Lei sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
51016e3e418SMing Lei return 1;
51116e3e418SMing Lei }
51216e3e418SMing Lei
513f6970f83SMing Lei /* only try to merge bvecs into one sg if they are from two bios */
514f6970f83SMing Lei static inline bool
__blk_segment_map_sg_merge(struct request_queue * q,struct bio_vec * bvec,struct bio_vec * bvprv,struct scatterlist ** sg)515f6970f83SMing Lei __blk_segment_map_sg_merge(struct request_queue *q, struct bio_vec *bvec,
516f6970f83SMing Lei struct bio_vec *bvprv, struct scatterlist **sg)
517963ab9e5SAsias He {
518963ab9e5SAsias He
519963ab9e5SAsias He int nbytes = bvec->bv_len;
520963ab9e5SAsias He
521f6970f83SMing Lei if (!*sg)
522f6970f83SMing Lei return false;
523f6970f83SMing Lei
524b4b6cb61SMing Lei if ((*sg)->length + nbytes > queue_max_segment_size(q))
525f6970f83SMing Lei return false;
526f6970f83SMing Lei
5273dccdae5SChristoph Hellwig if (!biovec_phys_mergeable(q, bvprv, bvec))
528f6970f83SMing Lei return false;
529963ab9e5SAsias He
530963ab9e5SAsias He (*sg)->length += nbytes;
531f6970f83SMing Lei
532f6970f83SMing Lei return true;
533963ab9e5SAsias He }
534963ab9e5SAsias He
__blk_bios_map_sg(struct request_queue * q,struct bio * bio,struct scatterlist * sglist,struct scatterlist ** sg)5355cb8850cSKent Overstreet static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
5365cb8850cSKent Overstreet struct scatterlist *sglist,
5375cb8850cSKent Overstreet struct scatterlist **sg)
5385cb8850cSKent Overstreet {
5393f649ab7SKees Cook struct bio_vec bvec, bvprv = { NULL };
5405cb8850cSKent Overstreet struct bvec_iter iter;
54138417468SChristoph Hellwig int nsegs = 0;
542f6970f83SMing Lei bool new_bio = false;
5435cb8850cSKent Overstreet
544f6970f83SMing Lei for_each_bio(bio) {
545f6970f83SMing Lei bio_for_each_bvec(bvec, bio, iter) {
546f6970f83SMing Lei /*
547f6970f83SMing Lei * Only try to merge bvecs from two bios given we
548f6970f83SMing Lei * have done bio internal merge when adding pages
549f6970f83SMing Lei * to bio
550f6970f83SMing Lei */
551f6970f83SMing Lei if (new_bio &&
552f6970f83SMing Lei __blk_segment_map_sg_merge(q, &bvec, &bvprv, sg))
553f6970f83SMing Lei goto next_bvec;
554f6970f83SMing Lei
555f6970f83SMing Lei if (bvec.bv_offset + bvec.bv_len <= PAGE_SIZE)
556f6970f83SMing Lei nsegs += __blk_bvec_map_sg(bvec, sglist, sg);
557f6970f83SMing Lei else
558f6970f83SMing Lei nsegs += blk_bvec_map_sg(q, &bvec, sglist, sg);
559f6970f83SMing Lei next_bvec:
560f6970f83SMing Lei new_bio = false;
561f6970f83SMing Lei }
562b21e11c5SMing Lei if (likely(bio->bi_iter.bi_size)) {
563f6970f83SMing Lei bvprv = bvec;
564f6970f83SMing Lei new_bio = true;
565f6970f83SMing Lei }
566b21e11c5SMing Lei }
5675cb8850cSKent Overstreet
5685cb8850cSKent Overstreet return nsegs;
5695cb8850cSKent Overstreet }
5705cb8850cSKent Overstreet
571d6d48196SJens Axboe /*
572d6d48196SJens Axboe * map a request to scatterlist, return number of sg entries setup. Caller
573d6d48196SJens Axboe * must make sure sg can hold rq->nr_phys_segments entries
574d6d48196SJens Axboe */
__blk_rq_map_sg(struct request_queue * q,struct request * rq,struct scatterlist * sglist,struct scatterlist ** last_sg)57589de1504SChristoph Hellwig int __blk_rq_map_sg(struct request_queue *q, struct request *rq,
57689de1504SChristoph Hellwig struct scatterlist *sglist, struct scatterlist **last_sg)
577d6d48196SJens Axboe {
5785cb8850cSKent Overstreet int nsegs = 0;
579d6d48196SJens Axboe
580f9d03f96SChristoph Hellwig if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
58189de1504SChristoph Hellwig nsegs = __blk_bvec_map_sg(rq->special_vec, sglist, last_sg);
582f9d03f96SChristoph Hellwig else if (rq->bio)
58389de1504SChristoph Hellwig nsegs = __blk_bios_map_sg(q, rq->bio, sglist, last_sg);
584f18573abSFUJITA Tomonori
58589de1504SChristoph Hellwig if (*last_sg)
58689de1504SChristoph Hellwig sg_mark_end(*last_sg);
587d6d48196SJens Axboe
58812e57f59SMing Lei /*
58912e57f59SMing Lei * Something must have been wrong if the figured number of
59012e57f59SMing Lei * segment is bigger than number of req's physical segments
59112e57f59SMing Lei */
592f9d03f96SChristoph Hellwig WARN_ON(nsegs > blk_rq_nr_phys_segments(rq));
59312e57f59SMing Lei
594d6d48196SJens Axboe return nsegs;
595d6d48196SJens Axboe }
59689de1504SChristoph Hellwig EXPORT_SYMBOL(__blk_rq_map_sg);
597d6d48196SJens Axboe
blk_rq_get_max_sectors(struct request * rq,sector_t offset)598badf7f64SChristoph Hellwig static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
599badf7f64SChristoph Hellwig sector_t offset)
600badf7f64SChristoph Hellwig {
601badf7f64SChristoph Hellwig struct request_queue *q = rq->q;
602c8875190SChristoph Hellwig unsigned int max_sectors;
603badf7f64SChristoph Hellwig
604badf7f64SChristoph Hellwig if (blk_rq_is_passthrough(rq))
605badf7f64SChristoph Hellwig return q->limits.max_hw_sectors;
606badf7f64SChristoph Hellwig
607c8875190SChristoph Hellwig max_sectors = blk_queue_get_max_sectors(q, req_op(rq));
608badf7f64SChristoph Hellwig if (!q->limits.chunk_sectors ||
609badf7f64SChristoph Hellwig req_op(rq) == REQ_OP_DISCARD ||
610badf7f64SChristoph Hellwig req_op(rq) == REQ_OP_SECURE_ERASE)
611c8875190SChristoph Hellwig return max_sectors;
612c8875190SChristoph Hellwig return min(max_sectors,
613c8875190SChristoph Hellwig blk_chunk_sectors_left(offset, q->limits.chunk_sectors));
614badf7f64SChristoph Hellwig }
615badf7f64SChristoph Hellwig
ll_new_hw_segment(struct request * req,struct bio * bio,unsigned int nr_phys_segs)61614ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
61714ccb66bSChristoph Hellwig unsigned int nr_phys_segs)
618d6d48196SJens Axboe {
6196b2b0459STejun Heo if (!blk_cgroup_mergeable(req, bio))
6206b2b0459STejun Heo goto no_merge;
6216b2b0459STejun Heo
6222705dfb2SMing Lei if (blk_integrity_merge_bio(req->q, req, bio) == false)
62313f05c8dSMartin K. Petersen goto no_merge;
62413f05c8dSMartin K. Petersen
6252705dfb2SMing Lei /* discard request merge won't add new segment */
6262705dfb2SMing Lei if (req_op(req) == REQ_OP_DISCARD)
6272705dfb2SMing Lei return 1;
6282705dfb2SMing Lei
6292705dfb2SMing Lei if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req))
63013f05c8dSMartin K. Petersen goto no_merge;
631d6d48196SJens Axboe
632d6d48196SJens Axboe /*
633d6d48196SJens Axboe * This will form the start of a new hw segment. Bump both
634d6d48196SJens Axboe * counters.
635d6d48196SJens Axboe */
636d6d48196SJens Axboe req->nr_phys_segments += nr_phys_segs;
637d6d48196SJens Axboe return 1;
63813f05c8dSMartin K. Petersen
63913f05c8dSMartin K. Petersen no_merge:
64014ccb66bSChristoph Hellwig req_set_nomerge(req->q, req);
64113f05c8dSMartin K. Petersen return 0;
642d6d48196SJens Axboe }
643d6d48196SJens Axboe
ll_back_merge_fn(struct request * req,struct bio * bio,unsigned int nr_segs)64414ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs)
645d6d48196SJens Axboe {
6465e7c4274SJens Axboe if (req_gap_back_merge(req, bio))
6475e7c4274SJens Axboe return 0;
6487f39add3SSagi Grimberg if (blk_integrity_rq(req) &&
6497f39add3SSagi Grimberg integrity_req_gap_back_merge(req, bio))
6507f39add3SSagi Grimberg return 0;
651a892c8d5SSatya Tangirala if (!bio_crypt_ctx_back_mergeable(req, bio))
652a892c8d5SSatya Tangirala return 0;
653f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) >
65417007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req))) {
65514ccb66bSChristoph Hellwig req_set_nomerge(req->q, req);
656d6d48196SJens Axboe return 0;
657d6d48196SJens Axboe }
658d6d48196SJens Axboe
65914ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs);
660d6d48196SJens Axboe }
661d6d48196SJens Axboe
ll_front_merge_fn(struct request * req,struct bio * bio,unsigned int nr_segs)662eda5cc99SChristoph Hellwig static int ll_front_merge_fn(struct request *req, struct bio *bio,
663eda5cc99SChristoph Hellwig unsigned int nr_segs)
664d6d48196SJens Axboe {
6655e7c4274SJens Axboe if (req_gap_front_merge(req, bio))
6665e7c4274SJens Axboe return 0;
6677f39add3SSagi Grimberg if (blk_integrity_rq(req) &&
6687f39add3SSagi Grimberg integrity_req_gap_front_merge(req, bio))
6697f39add3SSagi Grimberg return 0;
670a892c8d5SSatya Tangirala if (!bio_crypt_ctx_front_mergeable(req, bio))
671a892c8d5SSatya Tangirala return 0;
672f31dc1cdSMartin K. Petersen if (blk_rq_sectors(req) + bio_sectors(bio) >
67317007f39SDamien Le Moal blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) {
67414ccb66bSChristoph Hellwig req_set_nomerge(req->q, req);
675d6d48196SJens Axboe return 0;
676d6d48196SJens Axboe }
677d6d48196SJens Axboe
67814ccb66bSChristoph Hellwig return ll_new_hw_segment(req, bio, nr_segs);
679d6d48196SJens Axboe }
680d6d48196SJens Axboe
req_attempt_discard_merge(struct request_queue * q,struct request * req,struct request * next)681445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req,
682445251d0SJens Axboe struct request *next)
683445251d0SJens Axboe {
684445251d0SJens Axboe unsigned short segments = blk_rq_nr_discard_segments(req);
685445251d0SJens Axboe
686445251d0SJens Axboe if (segments >= queue_max_discard_segments(q))
687445251d0SJens Axboe goto no_merge;
688445251d0SJens Axboe if (blk_rq_sectors(req) + bio_sectors(next->bio) >
689445251d0SJens Axboe blk_rq_get_max_sectors(req, blk_rq_pos(req)))
690445251d0SJens Axboe goto no_merge;
691445251d0SJens Axboe
692445251d0SJens Axboe req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next);
693445251d0SJens Axboe return true;
694445251d0SJens Axboe no_merge:
695445251d0SJens Axboe req_set_nomerge(q, req);
696445251d0SJens Axboe return false;
697445251d0SJens Axboe }
698445251d0SJens Axboe
ll_merge_requests_fn(struct request_queue * q,struct request * req,struct request * next)699d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
700d6d48196SJens Axboe struct request *next)
701d6d48196SJens Axboe {
702d6d48196SJens Axboe int total_phys_segments;
703d6d48196SJens Axboe
7045e7c4274SJens Axboe if (req_gap_back_merge(req, next->bio))
705854fbb9cSKeith Busch return 0;
706854fbb9cSKeith Busch
707d6d48196SJens Axboe /*
708d6d48196SJens Axboe * Will it become too large?
709d6d48196SJens Axboe */
710f31dc1cdSMartin K. Petersen if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
71117007f39SDamien Le Moal blk_rq_get_max_sectors(req, blk_rq_pos(req)))
712d6d48196SJens Axboe return 0;
713d6d48196SJens Axboe
714d6d48196SJens Axboe total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
715943b40c8SMing Lei if (total_phys_segments > blk_rq_get_max_segments(req))
716d6d48196SJens Axboe return 0;
717d6d48196SJens Axboe
7186b2b0459STejun Heo if (!blk_cgroup_mergeable(req, next->bio))
7196b2b0459STejun Heo return 0;
7206b2b0459STejun Heo
7214eaf99beSMartin K. Petersen if (blk_integrity_merge_rq(q, req, next) == false)
72213f05c8dSMartin K. Petersen return 0;
72313f05c8dSMartin K. Petersen
724a892c8d5SSatya Tangirala if (!bio_crypt_ctx_merge_rq(req, next))
725a892c8d5SSatya Tangirala return 0;
726a892c8d5SSatya Tangirala
727d6d48196SJens Axboe /* Merge is OK... */
728d6d48196SJens Axboe req->nr_phys_segments = total_phys_segments;
729d6d48196SJens Axboe return 1;
730d6d48196SJens Axboe }
731d6d48196SJens Axboe
73280a761fdSTejun Heo /**
73380a761fdSTejun Heo * blk_rq_set_mixed_merge - mark a request as mixed merge
73480a761fdSTejun Heo * @rq: request to mark as mixed merge
73580a761fdSTejun Heo *
73680a761fdSTejun Heo * Description:
73780a761fdSTejun Heo * @rq is about to be mixed merged. Make sure the attributes
73880a761fdSTejun Heo * which can be mixed are set in each bio and mark @rq as mixed
73980a761fdSTejun Heo * merged.
74080a761fdSTejun Heo */
blk_rq_set_mixed_merge(struct request * rq)74180a761fdSTejun Heo void blk_rq_set_mixed_merge(struct request *rq)
74280a761fdSTejun Heo {
74316458cf3SBart Van Assche blk_opf_t ff = rq->cmd_flags & REQ_FAILFAST_MASK;
74480a761fdSTejun Heo struct bio *bio;
74580a761fdSTejun Heo
746e8064021SChristoph Hellwig if (rq->rq_flags & RQF_MIXED_MERGE)
74780a761fdSTejun Heo return;
74880a761fdSTejun Heo
74980a761fdSTejun Heo /*
75080a761fdSTejun Heo * @rq will no longer represent mixable attributes for all the
75180a761fdSTejun Heo * contained bios. It will just track those of the first one.
75280a761fdSTejun Heo * Distributes the attributs to each bio.
75380a761fdSTejun Heo */
75480a761fdSTejun Heo for (bio = rq->bio; bio; bio = bio->bi_next) {
7551eff9d32SJens Axboe WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) &&
7561eff9d32SJens Axboe (bio->bi_opf & REQ_FAILFAST_MASK) != ff);
7571eff9d32SJens Axboe bio->bi_opf |= ff;
75880a761fdSTejun Heo }
759e8064021SChristoph Hellwig rq->rq_flags |= RQF_MIXED_MERGE;
76080a761fdSTejun Heo }
76180a761fdSTejun Heo
bio_failfast(const struct bio * bio)762f3ca7386SJens Axboe static inline blk_opf_t bio_failfast(const struct bio *bio)
7633ce6a115SMing Lei {
7643ce6a115SMing Lei if (bio->bi_opf & REQ_RAHEAD)
7653ce6a115SMing Lei return REQ_FAILFAST_MASK;
7663ce6a115SMing Lei
7673ce6a115SMing Lei return bio->bi_opf & REQ_FAILFAST_MASK;
7683ce6a115SMing Lei }
7693ce6a115SMing Lei
7703ce6a115SMing Lei /*
7713ce6a115SMing Lei * After we are marked as MIXED_MERGE, any new RA bio has to be updated
7723ce6a115SMing Lei * as failfast, and request's failfast has to be updated in case of
7733ce6a115SMing Lei * front merge.
7743ce6a115SMing Lei */
blk_update_mixed_merge(struct request * req,struct bio * bio,bool front_merge)7753ce6a115SMing Lei static inline void blk_update_mixed_merge(struct request *req,
7763ce6a115SMing Lei struct bio *bio, bool front_merge)
7773ce6a115SMing Lei {
7783ce6a115SMing Lei if (req->rq_flags & RQF_MIXED_MERGE) {
7793ce6a115SMing Lei if (bio->bi_opf & REQ_RAHEAD)
7803ce6a115SMing Lei bio->bi_opf |= REQ_FAILFAST_MASK;
7813ce6a115SMing Lei
7823ce6a115SMing Lei if (front_merge) {
7833ce6a115SMing Lei req->cmd_flags &= ~REQ_FAILFAST_MASK;
7843ce6a115SMing Lei req->cmd_flags |= bio->bi_opf & REQ_FAILFAST_MASK;
7853ce6a115SMing Lei }
7863ce6a115SMing Lei }
7873ce6a115SMing Lei }
7883ce6a115SMing Lei
blk_account_io_merge_request(struct request * req)789b9c54f56SKonstantin Khlebnikov static void blk_account_io_merge_request(struct request *req)
79026308eabSJerome Marchand {
79126308eabSJerome Marchand if (blk_do_io_stat(req)) {
792112f158fSMike Snitzer part_stat_lock();
793b9c54f56SKonstantin Khlebnikov part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
794e5d98cc3SYu Kuai part_stat_local_dec(req->part,
795e5d98cc3SYu Kuai in_flight[op_is_write(req_op(req))]);
79626308eabSJerome Marchand part_stat_unlock();
79726308eabSJerome Marchand }
79826308eabSJerome Marchand }
799b9c54f56SKonstantin Khlebnikov
blk_try_req_merge(struct request * req,struct request * next)800e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req,
801e96c0d83SEric Biggers struct request *next)
80269840466SJianchao Wang {
80369840466SJianchao Wang if (blk_discard_mergable(req))
80469840466SJianchao Wang return ELEVATOR_DISCARD_MERGE;
80569840466SJianchao Wang else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
80669840466SJianchao Wang return ELEVATOR_BACK_MERGE;
80769840466SJianchao Wang
80869840466SJianchao Wang return ELEVATOR_NO_MERGE;
80969840466SJianchao Wang }
81026308eabSJerome Marchand
811d6d48196SJens Axboe /*
812b973cb7eSJens Axboe * For non-mq, this has to be called with the request spinlock acquired.
813b973cb7eSJens Axboe * For mq with scheduling, the appropriate queue wide lock should be held.
814d6d48196SJens Axboe */
attempt_merge(struct request_queue * q,struct request * req,struct request * next)815b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q,
816b973cb7eSJens Axboe struct request *req, struct request *next)
817d6d48196SJens Axboe {
818d6d48196SJens Axboe if (!rq_mergeable(req) || !rq_mergeable(next))
819b973cb7eSJens Axboe return NULL;
820d6d48196SJens Axboe
821288dab8aSChristoph Hellwig if (req_op(req) != req_op(next))
822b973cb7eSJens Axboe return NULL;
823f31dc1cdSMartin K. Petersen
82479bb1dbdSChristoph Hellwig if (rq_data_dir(req) != rq_data_dir(next))
825b973cb7eSJens Axboe return NULL;
826d6d48196SJens Axboe
827668ffc03SDamien Le Moal if (req->ioprio != next->ioprio)
828668ffc03SDamien Le Moal return NULL;
829668ffc03SDamien Le Moal
830cb6934f8SJens Axboe /*
831d6d48196SJens Axboe * If we are allowed to merge, then append bio list
832d6d48196SJens Axboe * from next to rq and release next. merge_requests_fn
833d6d48196SJens Axboe * will have updated segment counts, update sector
834445251d0SJens Axboe * counts here. Handle DISCARDs separately, as they
835445251d0SJens Axboe * have separate settings.
836d6d48196SJens Axboe */
83769840466SJianchao Wang
83869840466SJianchao Wang switch (blk_try_req_merge(req, next)) {
83969840466SJianchao Wang case ELEVATOR_DISCARD_MERGE:
840445251d0SJens Axboe if (!req_attempt_discard_merge(q, req, next))
841445251d0SJens Axboe return NULL;
84269840466SJianchao Wang break;
84369840466SJianchao Wang case ELEVATOR_BACK_MERGE:
84469840466SJianchao Wang if (!ll_merge_requests_fn(q, req, next))
845b973cb7eSJens Axboe return NULL;
84669840466SJianchao Wang break;
84769840466SJianchao Wang default:
84869840466SJianchao Wang return NULL;
84969840466SJianchao Wang }
850d6d48196SJens Axboe
851d6d48196SJens Axboe /*
85280a761fdSTejun Heo * If failfast settings disagree or any of the two is already
85380a761fdSTejun Heo * a mixed merge, mark both as mixed before proceeding. This
85480a761fdSTejun Heo * makes sure that all involved bios have mixable attributes
85580a761fdSTejun Heo * set properly.
85680a761fdSTejun Heo */
857e8064021SChristoph Hellwig if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) ||
85880a761fdSTejun Heo (req->cmd_flags & REQ_FAILFAST_MASK) !=
85980a761fdSTejun Heo (next->cmd_flags & REQ_FAILFAST_MASK)) {
86080a761fdSTejun Heo blk_rq_set_mixed_merge(req);
86180a761fdSTejun Heo blk_rq_set_mixed_merge(next);
86280a761fdSTejun Heo }
86380a761fdSTejun Heo
86480a761fdSTejun Heo /*
865522a7775SOmar Sandoval * At this point we have either done a back merge or front merge. We
866522a7775SOmar Sandoval * need the smaller start_time_ns of the merged requests to be the
867522a7775SOmar Sandoval * current request for accounting purposes.
868d6d48196SJens Axboe */
869522a7775SOmar Sandoval if (next->start_time_ns < req->start_time_ns)
870522a7775SOmar Sandoval req->start_time_ns = next->start_time_ns;
871d6d48196SJens Axboe
872d6d48196SJens Axboe req->biotail->bi_next = next->bio;
873d6d48196SJens Axboe req->biotail = next->biotail;
874d6d48196SJens Axboe
875a2dec7b3STejun Heo req->__data_len += blk_rq_bytes(next);
876d6d48196SJens Axboe
8772a5cf35cSMing Lei if (!blk_discard_mergable(req))
878d6d48196SJens Axboe elv_merge_requests(q, req, next);
879d6d48196SJens Axboe
8809cd1e566SEric Biggers blk_crypto_rq_put_keyslot(next);
8819cd1e566SEric Biggers
88242dad764SJerome Marchand /*
88342dad764SJerome Marchand * 'next' is going away, so update stats accordingly
88442dad764SJerome Marchand */
885b9c54f56SKonstantin Khlebnikov blk_account_io_merge_request(next);
886d6d48196SJens Axboe
887a54895faSChristoph Hellwig trace_block_rq_merge(next);
888f3bdc62fSJan Kara
889e4d750c9SJens Axboe /*
890e4d750c9SJens Axboe * ownership of bio passed from next to req, return 'next' for
891e4d750c9SJens Axboe * the caller to free
892e4d750c9SJens Axboe */
8931cd96c24SBoaz Harrosh next->bio = NULL;
894b973cb7eSJens Axboe return next;
895d6d48196SJens Axboe }
896d6d48196SJens Axboe
attempt_back_merge(struct request_queue * q,struct request * rq)897eda5cc99SChristoph Hellwig static struct request *attempt_back_merge(struct request_queue *q,
898eda5cc99SChristoph Hellwig struct request *rq)
899d6d48196SJens Axboe {
900d6d48196SJens Axboe struct request *next = elv_latter_request(q, rq);
901d6d48196SJens Axboe
902d6d48196SJens Axboe if (next)
903d6d48196SJens Axboe return attempt_merge(q, rq, next);
904d6d48196SJens Axboe
905b973cb7eSJens Axboe return NULL;
906d6d48196SJens Axboe }
907d6d48196SJens Axboe
attempt_front_merge(struct request_queue * q,struct request * rq)908eda5cc99SChristoph Hellwig static struct request *attempt_front_merge(struct request_queue *q,
909eda5cc99SChristoph Hellwig struct request *rq)
910d6d48196SJens Axboe {
911d6d48196SJens Axboe struct request *prev = elv_former_request(q, rq);
912d6d48196SJens Axboe
913d6d48196SJens Axboe if (prev)
914d6d48196SJens Axboe return attempt_merge(q, prev, rq);
915d6d48196SJens Axboe
916b973cb7eSJens Axboe return NULL;
917d6d48196SJens Axboe }
9185e84ea3aSJens Axboe
919fd2ef39cSJan Kara /*
920fd2ef39cSJan Kara * Try to merge 'next' into 'rq'. Return true if the merge happened, false
921fd2ef39cSJan Kara * otherwise. The caller is responsible for freeing 'next' if the merge
922fd2ef39cSJan Kara * happened.
923fd2ef39cSJan Kara */
blk_attempt_req_merge(struct request_queue * q,struct request * rq,struct request * next)924fd2ef39cSJan Kara bool blk_attempt_req_merge(struct request_queue *q, struct request *rq,
9255e84ea3aSJens Axboe struct request *next)
9265e84ea3aSJens Axboe {
927fd2ef39cSJan Kara return attempt_merge(q, rq, next);
9285e84ea3aSJens Axboe }
929050c8ea8STejun Heo
blk_rq_merge_ok(struct request * rq,struct bio * bio)930050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
931050c8ea8STejun Heo {
932e2a60da7SMartin K. Petersen if (!rq_mergeable(rq) || !bio_mergeable(bio))
933050c8ea8STejun Heo return false;
934050c8ea8STejun Heo
935288dab8aSChristoph Hellwig if (req_op(rq) != bio_op(bio))
936f31dc1cdSMartin K. Petersen return false;
937f31dc1cdSMartin K. Petersen
938050c8ea8STejun Heo /* different data direction or already started, don't merge */
939050c8ea8STejun Heo if (bio_data_dir(bio) != rq_data_dir(rq))
940050c8ea8STejun Heo return false;
941050c8ea8STejun Heo
9426b2b0459STejun Heo /* don't merge across cgroup boundaries */
9436b2b0459STejun Heo if (!blk_cgroup_mergeable(rq, bio))
9446b2b0459STejun Heo return false;
9456b2b0459STejun Heo
946050c8ea8STejun Heo /* only merge integrity protected bio into ditto rq */
9474eaf99beSMartin K. Petersen if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
948050c8ea8STejun Heo return false;
949050c8ea8STejun Heo
950a892c8d5SSatya Tangirala /* Only merge if the crypt contexts are compatible */
951a892c8d5SSatya Tangirala if (!bio_crypt_rq_ctx_compatible(rq, bio))
952a892c8d5SSatya Tangirala return false;
953a892c8d5SSatya Tangirala
954668ffc03SDamien Le Moal if (rq->ioprio != bio_prio(bio))
955668ffc03SDamien Le Moal return false;
956668ffc03SDamien Le Moal
957050c8ea8STejun Heo return true;
958050c8ea8STejun Heo }
959050c8ea8STejun Heo
blk_try_merge(struct request * rq,struct bio * bio)96034fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio)
961050c8ea8STejun Heo {
96269840466SJianchao Wang if (blk_discard_mergable(rq))
9631e739730SChristoph Hellwig return ELEVATOR_DISCARD_MERGE;
9641e739730SChristoph Hellwig else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
965050c8ea8STejun Heo return ELEVATOR_BACK_MERGE;
9664f024f37SKent Overstreet else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
967050c8ea8STejun Heo return ELEVATOR_FRONT_MERGE;
968050c8ea8STejun Heo return ELEVATOR_NO_MERGE;
969050c8ea8STejun Heo }
9708e756373SBaolin Wang
blk_account_io_merge_bio(struct request * req)9718e756373SBaolin Wang static void blk_account_io_merge_bio(struct request *req)
9728e756373SBaolin Wang {
9738e756373SBaolin Wang if (!blk_do_io_stat(req))
9748e756373SBaolin Wang return;
9758e756373SBaolin Wang
9768e756373SBaolin Wang part_stat_lock();
9778e756373SBaolin Wang part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
9788e756373SBaolin Wang part_stat_unlock();
9798e756373SBaolin Wang }
9808e756373SBaolin Wang
981eda5cc99SChristoph Hellwig enum bio_merge_status {
982eda5cc99SChristoph Hellwig BIO_MERGE_OK,
983eda5cc99SChristoph Hellwig BIO_MERGE_NONE,
984eda5cc99SChristoph Hellwig BIO_MERGE_FAILED,
985eda5cc99SChristoph Hellwig };
986eda5cc99SChristoph Hellwig
bio_attempt_back_merge(struct request * req,struct bio * bio,unsigned int nr_segs)987eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_back_merge(struct request *req,
988eda5cc99SChristoph Hellwig struct bio *bio, unsigned int nr_segs)
9898e756373SBaolin Wang {
9903ce6a115SMing Lei const blk_opf_t ff = bio_failfast(bio);
9918e756373SBaolin Wang
9928e756373SBaolin Wang if (!ll_back_merge_fn(req, bio, nr_segs))
9937d7ca7c5SBaolin Wang return BIO_MERGE_FAILED;
9948e756373SBaolin Wang
995e8a676d6SChristoph Hellwig trace_block_bio_backmerge(bio);
9968e756373SBaolin Wang rq_qos_merge(req->q, req, bio);
9978e756373SBaolin Wang
9988e756373SBaolin Wang if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
9998e756373SBaolin Wang blk_rq_set_mixed_merge(req);
10008e756373SBaolin Wang
10013ce6a115SMing Lei blk_update_mixed_merge(req, bio, false);
10023ce6a115SMing Lei
10038e756373SBaolin Wang req->biotail->bi_next = bio;
10048e756373SBaolin Wang req->biotail = bio;
10058e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size;
10068e756373SBaolin Wang
10078e756373SBaolin Wang bio_crypt_free_ctx(bio);
10088e756373SBaolin Wang
10098e756373SBaolin Wang blk_account_io_merge_bio(req);
10107d7ca7c5SBaolin Wang return BIO_MERGE_OK;
10118e756373SBaolin Wang }
10128e756373SBaolin Wang
bio_attempt_front_merge(struct request * req,struct bio * bio,unsigned int nr_segs)1013eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_front_merge(struct request *req,
1014eda5cc99SChristoph Hellwig struct bio *bio, unsigned int nr_segs)
10158e756373SBaolin Wang {
10163ce6a115SMing Lei const blk_opf_t ff = bio_failfast(bio);
10178e756373SBaolin Wang
10188e756373SBaolin Wang if (!ll_front_merge_fn(req, bio, nr_segs))
10197d7ca7c5SBaolin Wang return BIO_MERGE_FAILED;
10208e756373SBaolin Wang
1021e8a676d6SChristoph Hellwig trace_block_bio_frontmerge(bio);
10228e756373SBaolin Wang rq_qos_merge(req->q, req, bio);
10238e756373SBaolin Wang
10248e756373SBaolin Wang if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
10258e756373SBaolin Wang blk_rq_set_mixed_merge(req);
10268e756373SBaolin Wang
10273ce6a115SMing Lei blk_update_mixed_merge(req, bio, true);
10283ce6a115SMing Lei
10298e756373SBaolin Wang bio->bi_next = req->bio;
10308e756373SBaolin Wang req->bio = bio;
10318e756373SBaolin Wang
10328e756373SBaolin Wang req->__sector = bio->bi_iter.bi_sector;
10338e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size;
10348e756373SBaolin Wang
10358e756373SBaolin Wang bio_crypt_do_front_merge(req, bio);
10368e756373SBaolin Wang
10378e756373SBaolin Wang blk_account_io_merge_bio(req);
10387d7ca7c5SBaolin Wang return BIO_MERGE_OK;
10398e756373SBaolin Wang }
10408e756373SBaolin Wang
bio_attempt_discard_merge(struct request_queue * q,struct request * req,struct bio * bio)1041eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_discard_merge(struct request_queue *q,
1042eda5cc99SChristoph Hellwig struct request *req, struct bio *bio)
10438e756373SBaolin Wang {
10448e756373SBaolin Wang unsigned short segments = blk_rq_nr_discard_segments(req);
10458e756373SBaolin Wang
10468e756373SBaolin Wang if (segments >= queue_max_discard_segments(q))
10478e756373SBaolin Wang goto no_merge;
10488e756373SBaolin Wang if (blk_rq_sectors(req) + bio_sectors(bio) >
10498e756373SBaolin Wang blk_rq_get_max_sectors(req, blk_rq_pos(req)))
10508e756373SBaolin Wang goto no_merge;
10518e756373SBaolin Wang
10528e756373SBaolin Wang rq_qos_merge(q, req, bio);
10538e756373SBaolin Wang
10548e756373SBaolin Wang req->biotail->bi_next = bio;
10558e756373SBaolin Wang req->biotail = bio;
10568e756373SBaolin Wang req->__data_len += bio->bi_iter.bi_size;
10578e756373SBaolin Wang req->nr_phys_segments = segments + 1;
10588e756373SBaolin Wang
10598e756373SBaolin Wang blk_account_io_merge_bio(req);
10607d7ca7c5SBaolin Wang return BIO_MERGE_OK;
10618e756373SBaolin Wang no_merge:
10628e756373SBaolin Wang req_set_nomerge(q, req);
10637d7ca7c5SBaolin Wang return BIO_MERGE_FAILED;
10647d7ca7c5SBaolin Wang }
10657d7ca7c5SBaolin Wang
blk_attempt_bio_merge(struct request_queue * q,struct request * rq,struct bio * bio,unsigned int nr_segs,bool sched_allow_merge)10667d7ca7c5SBaolin Wang static enum bio_merge_status blk_attempt_bio_merge(struct request_queue *q,
10677d7ca7c5SBaolin Wang struct request *rq,
10687d7ca7c5SBaolin Wang struct bio *bio,
10697d7ca7c5SBaolin Wang unsigned int nr_segs,
10707d7ca7c5SBaolin Wang bool sched_allow_merge)
10717d7ca7c5SBaolin Wang {
10727d7ca7c5SBaolin Wang if (!blk_rq_merge_ok(rq, bio))
10737d7ca7c5SBaolin Wang return BIO_MERGE_NONE;
10747d7ca7c5SBaolin Wang
10757d7ca7c5SBaolin Wang switch (blk_try_merge(rq, bio)) {
10767d7ca7c5SBaolin Wang case ELEVATOR_BACK_MERGE:
1077265600b7SBaolin Wang if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio))
10787d7ca7c5SBaolin Wang return bio_attempt_back_merge(rq, bio, nr_segs);
10797d7ca7c5SBaolin Wang break;
10807d7ca7c5SBaolin Wang case ELEVATOR_FRONT_MERGE:
1081265600b7SBaolin Wang if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio))
10827d7ca7c5SBaolin Wang return bio_attempt_front_merge(rq, bio, nr_segs);
10837d7ca7c5SBaolin Wang break;
10847d7ca7c5SBaolin Wang case ELEVATOR_DISCARD_MERGE:
10857d7ca7c5SBaolin Wang return bio_attempt_discard_merge(q, rq, bio);
10867d7ca7c5SBaolin Wang default:
10877d7ca7c5SBaolin Wang return BIO_MERGE_NONE;
10887d7ca7c5SBaolin Wang }
10897d7ca7c5SBaolin Wang
10907d7ca7c5SBaolin Wang return BIO_MERGE_FAILED;
10918e756373SBaolin Wang }
10928e756373SBaolin Wang
10938e756373SBaolin Wang /**
10948e756373SBaolin Wang * blk_attempt_plug_merge - try to merge with %current's plugged list
10958e756373SBaolin Wang * @q: request_queue new bio is being queued at
10968e756373SBaolin Wang * @bio: new bio being queued
10978e756373SBaolin Wang * @nr_segs: number of segments in @bio
109887c037d1SJens Axboe * from the passed in @q already in the plug list
10998e756373SBaolin Wang *
1100d38a9c04SJens Axboe * Determine whether @bio being queued on @q can be merged with the previous
1101d38a9c04SJens Axboe * request on %current's plugged list. Returns %true if merge was successful,
11028e756373SBaolin Wang * otherwise %false.
11038e756373SBaolin Wang *
11048e756373SBaolin Wang * Plugging coalesces IOs from the same issuer for the same purpose without
11058e756373SBaolin Wang * going through @q->queue_lock. As such it's more of an issuing mechanism
11068e756373SBaolin Wang * than scheduling, and the request, while may have elvpriv data, is not
11078e756373SBaolin Wang * added on the elevator at this point. In addition, we don't have
11088e756373SBaolin Wang * reliable access to the elevator outside queue lock. Only check basic
11098e756373SBaolin Wang * merging parameters without querying the elevator.
11108e756373SBaolin Wang *
11118e756373SBaolin Wang * Caller must ensure !blk_queue_nomerges(q) beforehand.
11128e756373SBaolin Wang */
blk_attempt_plug_merge(struct request_queue * q,struct bio * bio,unsigned int nr_segs)11138e756373SBaolin Wang bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
11140c5bcc92SChristoph Hellwig unsigned int nr_segs)
11158e756373SBaolin Wang {
11168e756373SBaolin Wang struct blk_plug *plug;
11178e756373SBaolin Wang struct request *rq;
11188e756373SBaolin Wang
11196deacb3bSChristoph Hellwig plug = blk_mq_plug(bio);
1120bc490f81SJens Axboe if (!plug || rq_list_empty(plug->mq_list))
11218e756373SBaolin Wang return false;
11228e756373SBaolin Wang
11235b205071SJens Axboe rq_list_for_each(&plug->mq_list, rq) {
112487c037d1SJens Axboe if (rq->q == q) {
1125a1cb6537SMing Lei if (blk_attempt_bio_merge(q, rq, bio, nr_segs, false) ==
1126a1cb6537SMing Lei BIO_MERGE_OK)
11278e756373SBaolin Wang return true;
11285b205071SJens Axboe break;
11295b205071SJens Axboe }
11305b205071SJens Axboe
11315b205071SJens Axboe /*
11325b205071SJens Axboe * Only keep iterating plug list for merges if we have multiple
11335b205071SJens Axboe * queues
11345b205071SJens Axboe */
11355b205071SJens Axboe if (!plug->multiple_queues)
11365b205071SJens Axboe break;
1137a1cb6537SMing Lei }
11388e756373SBaolin Wang return false;
11398e756373SBaolin Wang }
1140bdc6a287SBaolin Wang
1141bdc6a287SBaolin Wang /*
1142bdc6a287SBaolin Wang * Iterate list of requests and see if we can merge this bio with any
1143bdc6a287SBaolin Wang * of them.
1144bdc6a287SBaolin Wang */
blk_bio_list_merge(struct request_queue * q,struct list_head * list,struct bio * bio,unsigned int nr_segs)1145bdc6a287SBaolin Wang bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
1146bdc6a287SBaolin Wang struct bio *bio, unsigned int nr_segs)
1147bdc6a287SBaolin Wang {
1148bdc6a287SBaolin Wang struct request *rq;
1149bdc6a287SBaolin Wang int checked = 8;
1150bdc6a287SBaolin Wang
1151bdc6a287SBaolin Wang list_for_each_entry_reverse(rq, list, queuelist) {
1152bdc6a287SBaolin Wang if (!checked--)
1153bdc6a287SBaolin Wang break;
1154bdc6a287SBaolin Wang
11557d7ca7c5SBaolin Wang switch (blk_attempt_bio_merge(q, rq, bio, nr_segs, true)) {
11567d7ca7c5SBaolin Wang case BIO_MERGE_NONE:
1157bdc6a287SBaolin Wang continue;
11587d7ca7c5SBaolin Wang case BIO_MERGE_OK:
11597d7ca7c5SBaolin Wang return true;
11607d7ca7c5SBaolin Wang case BIO_MERGE_FAILED:
11617d7ca7c5SBaolin Wang return false;
1162bdc6a287SBaolin Wang }
1163bdc6a287SBaolin Wang
1164bdc6a287SBaolin Wang }
1165bdc6a287SBaolin Wang
1166bdc6a287SBaolin Wang return false;
1167bdc6a287SBaolin Wang }
1168bdc6a287SBaolin Wang EXPORT_SYMBOL_GPL(blk_bio_list_merge);
1169eda5cc99SChristoph Hellwig
blk_mq_sched_try_merge(struct request_queue * q,struct bio * bio,unsigned int nr_segs,struct request ** merged_request)1170eda5cc99SChristoph Hellwig bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
1171eda5cc99SChristoph Hellwig unsigned int nr_segs, struct request **merged_request)
1172eda5cc99SChristoph Hellwig {
1173eda5cc99SChristoph Hellwig struct request *rq;
1174eda5cc99SChristoph Hellwig
1175eda5cc99SChristoph Hellwig switch (elv_merge(q, &rq, bio)) {
1176eda5cc99SChristoph Hellwig case ELEVATOR_BACK_MERGE:
1177eda5cc99SChristoph Hellwig if (!blk_mq_sched_allow_merge(q, rq, bio))
1178eda5cc99SChristoph Hellwig return false;
1179eda5cc99SChristoph Hellwig if (bio_attempt_back_merge(rq, bio, nr_segs) != BIO_MERGE_OK)
1180eda5cc99SChristoph Hellwig return false;
1181eda5cc99SChristoph Hellwig *merged_request = attempt_back_merge(q, rq);
1182eda5cc99SChristoph Hellwig if (!*merged_request)
1183eda5cc99SChristoph Hellwig elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
1184eda5cc99SChristoph Hellwig return true;
1185eda5cc99SChristoph Hellwig case ELEVATOR_FRONT_MERGE:
1186eda5cc99SChristoph Hellwig if (!blk_mq_sched_allow_merge(q, rq, bio))
1187eda5cc99SChristoph Hellwig return false;
1188eda5cc99SChristoph Hellwig if (bio_attempt_front_merge(rq, bio, nr_segs) != BIO_MERGE_OK)
1189eda5cc99SChristoph Hellwig return false;
1190eda5cc99SChristoph Hellwig *merged_request = attempt_front_merge(q, rq);
1191eda5cc99SChristoph Hellwig if (!*merged_request)
1192eda5cc99SChristoph Hellwig elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
1193eda5cc99SChristoph Hellwig return true;
1194eda5cc99SChristoph Hellwig case ELEVATOR_DISCARD_MERGE:
1195eda5cc99SChristoph Hellwig return bio_attempt_discard_merge(q, rq, bio) == BIO_MERGE_OK;
1196eda5cc99SChristoph Hellwig default:
1197eda5cc99SChristoph Hellwig return false;
1198eda5cc99SChristoph Hellwig }
1199eda5cc99SChristoph Hellwig }
1200eda5cc99SChristoph Hellwig EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
1201