xref: /linux/block/blk-merge.c (revision f4b369c6fe0ceaba2da2daff8c9eb415f85926dd)
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>
1082d981d4SChristoph Hellwig #include <linux/part_stat.h>
116b2b0459STejun Heo #include <linux/blk-cgroup.h>
12d6d48196SJens Axboe 
13cda22646SMike Krinkin #include <trace/events/block.h>
14cda22646SMike Krinkin 
15d6d48196SJens Axboe #include "blk.h"
162aa7745bSChristoph Hellwig #include "blk-mq-sched.h"
178e756373SBaolin Wang #include "blk-rq-qos.h"
18a7b36ee6SJens Axboe #include "blk-throttle.h"
19d6d48196SJens Axboe 
bio_get_first_bvec(struct bio * bio,struct bio_vec * bv)20ff18d77bSChristoph Hellwig static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
21ff18d77bSChristoph Hellwig {
22ff18d77bSChristoph Hellwig 	*bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
23ff18d77bSChristoph Hellwig }
24ff18d77bSChristoph Hellwig 
bio_get_last_bvec(struct bio * bio,struct bio_vec * bv)25ff18d77bSChristoph Hellwig static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
26ff18d77bSChristoph Hellwig {
27ff18d77bSChristoph Hellwig 	struct bvec_iter iter = bio->bi_iter;
28ff18d77bSChristoph Hellwig 	int idx;
29ff18d77bSChristoph Hellwig 
30ff18d77bSChristoph Hellwig 	bio_get_first_bvec(bio, bv);
31ff18d77bSChristoph Hellwig 	if (bv->bv_len == bio->bi_iter.bi_size)
32ff18d77bSChristoph Hellwig 		return;		/* this bio only has a single bvec */
33ff18d77bSChristoph Hellwig 
34ff18d77bSChristoph Hellwig 	bio_advance_iter(bio, &iter, iter.bi_size);
35ff18d77bSChristoph Hellwig 
36ff18d77bSChristoph Hellwig 	if (!iter.bi_bvec_done)
37ff18d77bSChristoph Hellwig 		idx = iter.bi_idx - 1;
38ff18d77bSChristoph Hellwig 	else	/* in the middle of bvec */
39ff18d77bSChristoph Hellwig 		idx = iter.bi_idx;
40ff18d77bSChristoph Hellwig 
41ff18d77bSChristoph Hellwig 	*bv = bio->bi_io_vec[idx];
42ff18d77bSChristoph Hellwig 
43ff18d77bSChristoph Hellwig 	/*
44ff18d77bSChristoph Hellwig 	 * iter.bi_bvec_done records actual length of the last bvec
45ff18d77bSChristoph Hellwig 	 * if this bio ends in the middle of one io vector
46ff18d77bSChristoph Hellwig 	 */
47ff18d77bSChristoph Hellwig 	if (iter.bi_bvec_done)
48ff18d77bSChristoph Hellwig 		bv->bv_len = iter.bi_bvec_done;
49ff18d77bSChristoph Hellwig }
50ff18d77bSChristoph Hellwig 
bio_will_gap(struct request_queue * q,struct request * prev_rq,struct bio * prev,struct bio * next)51e9907009SChristoph Hellwig static inline bool bio_will_gap(struct request_queue *q,
52e9907009SChristoph Hellwig 		struct request *prev_rq, struct bio *prev, struct bio *next)
53e9907009SChristoph Hellwig {
54e9907009SChristoph Hellwig 	struct bio_vec pb, nb;
55e9907009SChristoph Hellwig 
56e9907009SChristoph Hellwig 	if (!bio_has_data(prev) || !queue_virt_boundary(q))
57e9907009SChristoph Hellwig 		return false;
58e9907009SChristoph Hellwig 
59e9907009SChristoph Hellwig 	/*
60e9907009SChristoph Hellwig 	 * Don't merge if the 1st bio starts with non-zero offset, otherwise it
61e9907009SChristoph Hellwig 	 * is quite difficult to respect the sg gap limit.  We work hard to
62e9907009SChristoph Hellwig 	 * merge a huge number of small single bios in case of mkfs.
63e9907009SChristoph Hellwig 	 */
64e9907009SChristoph Hellwig 	if (prev_rq)
65e9907009SChristoph Hellwig 		bio_get_first_bvec(prev_rq->bio, &pb);
66e9907009SChristoph Hellwig 	else
67e9907009SChristoph Hellwig 		bio_get_first_bvec(prev, &pb);
68df376b2eSJohannes Thumshirn 	if (pb.bv_offset & queue_virt_boundary(q))
69e9907009SChristoph Hellwig 		return true;
70e9907009SChristoph Hellwig 
71e9907009SChristoph Hellwig 	/*
72e9907009SChristoph Hellwig 	 * We don't need to worry about the situation that the merged segment
73e9907009SChristoph Hellwig 	 * ends in unaligned virt boundary:
74e9907009SChristoph Hellwig 	 *
75e9907009SChristoph Hellwig 	 * - if 'pb' ends aligned, the merged segment ends aligned
76e9907009SChristoph Hellwig 	 * - if 'pb' ends unaligned, the next bio must include
77e9907009SChristoph Hellwig 	 *   one single bvec of 'nb', otherwise the 'nb' can't
78e9907009SChristoph Hellwig 	 *   merge with 'pb'
79e9907009SChristoph Hellwig 	 */
80e9907009SChristoph Hellwig 	bio_get_last_bvec(prev, &pb);
81e9907009SChristoph Hellwig 	bio_get_first_bvec(next, &nb);
82200a9affSChristoph Hellwig 	if (biovec_phys_mergeable(q, &pb, &nb))
83e9907009SChristoph Hellwig 		return false;
84c55ddd90SChristoph Hellwig 	return __bvec_gap_to_prev(&q->limits, &pb, nb.bv_offset);
85e9907009SChristoph Hellwig }
86e9907009SChristoph Hellwig 
req_gap_back_merge(struct request * req,struct bio * bio)87e9907009SChristoph Hellwig static inline bool req_gap_back_merge(struct request *req, struct bio *bio)
88e9907009SChristoph Hellwig {
89e9907009SChristoph Hellwig 	return bio_will_gap(req->q, req, req->biotail, bio);
90e9907009SChristoph Hellwig }
91e9907009SChristoph Hellwig 
req_gap_front_merge(struct request * req,struct bio * bio)92e9907009SChristoph Hellwig static inline bool req_gap_front_merge(struct request *req, struct bio *bio)
93e9907009SChristoph Hellwig {
94e9907009SChristoph Hellwig 	return bio_will_gap(req->q, NULL, bio, req->bio);
95e9907009SChristoph Hellwig }
96e9907009SChristoph Hellwig 
97b6dc6198SChristoph Hellwig /*
98fa0bdd45SChristoph Hellwig  * The maximum size that a bio can fit has to be aligned down to the
99b6dc6198SChristoph Hellwig  * logical block size, which is the minimum accepted unit by hardware.
100b6dc6198SChristoph Hellwig  */
bio_allowed_max_sectors(const struct queue_limits * lim)101aa261f20SBart Van Assche static unsigned int bio_allowed_max_sectors(const struct queue_limits *lim)
102b6dc6198SChristoph Hellwig {
103fa0bdd45SChristoph Hellwig 	return round_down(BIO_MAX_SIZE, lim->logical_block_size) >>
104fa0bdd45SChristoph Hellwig 			SECTOR_SHIFT;
105b6dc6198SChristoph Hellwig }
106b6dc6198SChristoph Hellwig 
107e37b5596SYu Kuai /*
108e37b5596SYu Kuai  * bio_submit_split_bioset - Submit a bio, splitting it at a designated sector
109e37b5596SYu Kuai  * @bio:		the original bio to be submitted and split
110e37b5596SYu Kuai  * @split_sectors:	the sector count at which to split
111e37b5596SYu Kuai  * @bs:			the bio set used for allocating the new split bio
112e37b5596SYu Kuai  *
113e37b5596SYu Kuai  * The original bio is modified to contain the remaining sectors and submitted.
114e37b5596SYu Kuai  * The caller is responsible for submitting the returned bio.
115e37b5596SYu Kuai  *
116e37b5596SYu Kuai  * If succeed, the newly allocated bio representing the initial part will be
117e37b5596SYu Kuai  * returned, on failure NULL will be returned and original bio will fail.
118e37b5596SYu Kuai  */
bio_submit_split_bioset(struct bio * bio,unsigned int split_sectors,struct bio_set * bs)119e37b5596SYu Kuai struct bio *bio_submit_split_bioset(struct bio *bio, unsigned int split_sectors,
120e37b5596SYu Kuai 				    struct bio_set *bs)
121b35243a4SChristoph Hellwig {
122e37b5596SYu Kuai 	struct bio *split = bio_split(bio, split_sectors, GFP_NOIO, bs);
123b35243a4SChristoph Hellwig 
1246eb09685SJohn Garry 	if (IS_ERR(split)) {
125e37b5596SYu Kuai 		bio->bi_status = errno_to_blk_status(PTR_ERR(split));
126e37b5596SYu Kuai 		bio_endio(bio);
127e37b5596SYu Kuai 		return NULL;
1286eb09685SJohn Garry 	}
129e37b5596SYu Kuai 
130b35243a4SChristoph Hellwig 	bio_chain(split, bio);
131b35243a4SChristoph Hellwig 	trace_block_split(split, bio->bi_iter.bi_sector);
132b35243a4SChristoph Hellwig 	WARN_ON_ONCE(bio_zone_write_plugging(bio));
1330b64682eSYu Kuai 
1340b64682eSYu Kuai 	if (should_fail_bio(bio))
1350b64682eSYu Kuai 		bio_io_error(bio);
1360b64682eSYu Kuai 	else if (!blk_throtl_bio(bio))
137b2f59740SYu Kuai 		submit_bio_noacct_nocheck(bio, true);
138e37b5596SYu Kuai 
139b35243a4SChristoph Hellwig 	return split;
140b35243a4SChristoph Hellwig }
141e37b5596SYu Kuai EXPORT_SYMBOL_GPL(bio_submit_split_bioset);
142b35243a4SChristoph Hellwig 
bio_submit_split(struct bio * bio,int split_sectors)143e37b5596SYu Kuai static struct bio *bio_submit_split(struct bio *bio, int split_sectors)
144e37b5596SYu Kuai {
145e37b5596SYu Kuai 	if (unlikely(split_sectors < 0)) {
1466eb09685SJohn Garry 		bio->bi_status = errno_to_blk_status(split_sectors);
1476eb09685SJohn Garry 		bio_endio(bio);
1486eb09685SJohn Garry 		return NULL;
149b35243a4SChristoph Hellwig 	}
150b35243a4SChristoph Hellwig 
151e37b5596SYu Kuai 	if (split_sectors) {
152e37b5596SYu Kuai 		bio = bio_submit_split_bioset(bio, split_sectors,
153e37b5596SYu Kuai 				&bio->bi_bdev->bd_disk->bio_split);
154e37b5596SYu Kuai 		if (bio)
155e37b5596SYu Kuai 			bio->bi_opf |= REQ_NOMERGE;
156e37b5596SYu Kuai 	}
157e37b5596SYu Kuai 
158e37b5596SYu Kuai 	return bio;
159e37b5596SYu Kuai }
160e37b5596SYu Kuai 
__bio_split_discard(struct bio * bio,const struct queue_limits * lim,unsigned * nsegs,unsigned int max_sectors)161*ee81212fSLuke Wang static struct bio *__bio_split_discard(struct bio *bio,
162*ee81212fSLuke Wang 		const struct queue_limits *lim, unsigned *nsegs,
163*ee81212fSLuke Wang 		unsigned int max_sectors)
16454efd50bSKent Overstreet {
16554efd50bSKent Overstreet 	unsigned int max_discard_sectors, granularity;
16654efd50bSKent Overstreet 	sector_t tmp;
16754efd50bSKent Overstreet 	unsigned split_sectors;
16854efd50bSKent Overstreet 
169bdced438SMing Lei 	*nsegs = 1;
170bdced438SMing Lei 
171c55ddd90SChristoph Hellwig 	granularity = max(lim->discard_granularity >> 9, 1U);
17254efd50bSKent Overstreet 
173*ee81212fSLuke Wang 	max_discard_sectors = min(max_sectors, bio_allowed_max_sectors(lim));
17454efd50bSKent Overstreet 	max_discard_sectors -= max_discard_sectors % granularity;
175928a5dd3SChristoph Hellwig 	if (unlikely(!max_discard_sectors))
176b35243a4SChristoph Hellwig 		return bio;
17754efd50bSKent Overstreet 
17854efd50bSKent Overstreet 	if (bio_sectors(bio) <= max_discard_sectors)
179b35243a4SChristoph Hellwig 		return bio;
18054efd50bSKent Overstreet 
18154efd50bSKent Overstreet 	split_sectors = max_discard_sectors;
18254efd50bSKent Overstreet 
18354efd50bSKent Overstreet 	/*
18454efd50bSKent Overstreet 	 * If the next starting sector would be misaligned, stop the discard at
18554efd50bSKent Overstreet 	 * the previous aligned sector.
18654efd50bSKent Overstreet 	 */
187c55ddd90SChristoph Hellwig 	tmp = bio->bi_iter.bi_sector + split_sectors -
188c55ddd90SChristoph Hellwig 		((lim->discard_alignment >> 9) % granularity);
18954efd50bSKent Overstreet 	tmp = sector_div(tmp, granularity);
19054efd50bSKent Overstreet 
19154efd50bSKent Overstreet 	if (split_sectors > tmp)
19254efd50bSKent Overstreet 		split_sectors -= tmp;
19354efd50bSKent Overstreet 
194b35243a4SChristoph Hellwig 	return bio_submit_split(bio, split_sectors);
19554efd50bSKent Overstreet }
19654efd50bSKent Overstreet 
bio_split_discard(struct bio * bio,const struct queue_limits * lim,unsigned * nsegs)197*ee81212fSLuke Wang struct bio *bio_split_discard(struct bio *bio, const struct queue_limits *lim,
198*ee81212fSLuke Wang 		unsigned *nsegs)
199*ee81212fSLuke Wang {
200*ee81212fSLuke Wang 	unsigned int max_sectors;
201*ee81212fSLuke Wang 
202*ee81212fSLuke Wang 	if (bio_op(bio) == REQ_OP_SECURE_ERASE)
203*ee81212fSLuke Wang 		max_sectors = lim->max_secure_erase_sectors;
204*ee81212fSLuke Wang 	else
205*ee81212fSLuke Wang 		max_sectors = lim->max_discard_sectors;
206*ee81212fSLuke Wang 
207*ee81212fSLuke Wang 	return __bio_split_discard(bio, lim, nsegs, max_sectors);
208*ee81212fSLuke Wang }
209*ee81212fSLuke Wang 
blk_boundary_sectors(const struct queue_limits * lim,bool is_atomic)2109da3d1e9SJohn Garry static inline unsigned int blk_boundary_sectors(const struct queue_limits *lim,
2119da3d1e9SJohn Garry 						bool is_atomic)
212f70167a7SJohn Garry {
2139da3d1e9SJohn Garry 	/*
2149da3d1e9SJohn Garry 	 * chunk_sectors must be a multiple of atomic_write_boundary_sectors if
2159da3d1e9SJohn Garry 	 * both non-zero.
2169da3d1e9SJohn Garry 	 */
2179da3d1e9SJohn Garry 	if (is_atomic && lim->atomic_write_boundary_sectors)
2189da3d1e9SJohn Garry 		return lim->atomic_write_boundary_sectors;
2199da3d1e9SJohn Garry 
220f70167a7SJohn Garry 	return lim->chunk_sectors;
221f70167a7SJohn Garry }
222f70167a7SJohn Garry 
2239cc5169cSBart Van Assche /*
2249cc5169cSBart Van Assche  * Return the maximum number of sectors from the start of a bio that may be
2259cc5169cSBart Van Assche  * submitted as a single request to a block device. If enough sectors remain,
2269cc5169cSBart Van Assche  * align the end to the physical block size. Otherwise align the end to the
2279cc5169cSBart Van Assche  * logical block size. This approach minimizes the number of non-aligned
2289cc5169cSBart Van Assche  * requests that are submitted to a block device if the start of a bio is not
2299cc5169cSBart Van Assche  * aligned to a physical block boundary.
2309cc5169cSBart Van Assche  */
get_max_io_size(struct bio * bio,const struct queue_limits * lim)2315a97806fSChristoph Hellwig static inline unsigned get_max_io_size(struct bio *bio,
232aa261f20SBart Van Assche 				       const struct queue_limits *lim)
233d0e5fbb0SMing Lei {
234c55ddd90SChristoph Hellwig 	unsigned pbs = lim->physical_block_size >> SECTOR_SHIFT;
235c55ddd90SChristoph Hellwig 	unsigned lbs = lim->logical_block_size >> SECTOR_SHIFT;
2369da3d1e9SJohn Garry 	bool is_atomic = bio->bi_opf & REQ_ATOMIC;
2379da3d1e9SJohn Garry 	unsigned boundary_sectors = blk_boundary_sectors(lim, is_atomic);
2389da3d1e9SJohn Garry 	unsigned max_sectors, start, end;
2399da3d1e9SJohn Garry 
2409da3d1e9SJohn Garry 	/*
2419da3d1e9SJohn Garry 	 * We ignore lim->max_sectors for atomic writes because it may less
2429da3d1e9SJohn Garry 	 * than the actual bio size, which we cannot tolerate.
2439da3d1e9SJohn Garry 	 */
24460dc5ea6SChristoph Hellwig 	if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
24560dc5ea6SChristoph Hellwig 		max_sectors = lim->max_write_zeroes_sectors;
24660dc5ea6SChristoph Hellwig 	else if (is_atomic)
2479da3d1e9SJohn Garry 		max_sectors = lim->atomic_write_max_sectors;
2489da3d1e9SJohn Garry 	else
2499da3d1e9SJohn Garry 		max_sectors = lim->max_sectors;
250d0e5fbb0SMing Lei 
251f70167a7SJohn Garry 	if (boundary_sectors) {
252efef739dSChristoph Hellwig 		max_sectors = min(max_sectors,
253f70167a7SJohn Garry 			blk_boundary_sectors_left(bio->bi_iter.bi_sector,
254f70167a7SJohn Garry 					      boundary_sectors));
255efef739dSChristoph Hellwig 	}
256d0e5fbb0SMing Lei 
25784613bedSChristoph Hellwig 	start = bio->bi_iter.bi_sector & (pbs - 1);
25884613bedSChristoph Hellwig 	end = (start + max_sectors) & ~(pbs - 1);
25984613bedSChristoph Hellwig 	if (end > start)
26084613bedSChristoph Hellwig 		return end - start;
26184613bedSChristoph Hellwig 	return max_sectors & ~(lbs - 1);
262d0e5fbb0SMing Lei }
263d0e5fbb0SMing Lei 
26495465318SBart Van Assche /**
265708b25b3SBart Van Assche  * bvec_split_segs - verify whether or not a bvec should be split in the middle
266c55ddd90SChristoph Hellwig  * @lim:      [in] queue limits to split based on
267708b25b3SBart Van Assche  * @bv:       [in] bvec to examine
268708b25b3SBart Van Assche  * @nsegs:    [in,out] Number of segments in the bio being built. Incremented
269708b25b3SBart Van Assche  *            by the number of segments from @bv that may be appended to that
270708b25b3SBart Van Assche  *            bio without exceeding @max_segs
27167927d22SKeith Busch  * @bytes:    [in,out] Number of bytes in the bio being built. Incremented
27267927d22SKeith Busch  *            by the number of bytes from @bv that may be appended to that
27367927d22SKeith Busch  *            bio without exceeding @max_bytes
274708b25b3SBart Van Assche  * @max_segs: [in] upper bound for *@nsegs
27567927d22SKeith Busch  * @max_bytes: [in] upper bound for *@bytes
276708b25b3SBart Van Assche  *
277708b25b3SBart Van Assche  * When splitting a bio, it can happen that a bvec is encountered that is too
278708b25b3SBart Van Assche  * big to fit in a single segment and hence that it has to be split in the
279708b25b3SBart Van Assche  * middle. This function verifies whether or not that should happen. The value
280708b25b3SBart Van Assche  * %true is returned if and only if appending the entire @bv to a bio with
281708b25b3SBart Van Assche  * *@nsegs segments and *@sectors sectors would make that bio unacceptable for
282708b25b3SBart Van Assche  * the block driver.
283dcebd755SMing Lei  */
bvec_split_segs(const struct queue_limits * lim,const struct bio_vec * bv,unsigned * nsegs,unsigned * bytes,unsigned max_segs,unsigned max_bytes)284aa261f20SBart Van Assche static bool bvec_split_segs(const struct queue_limits *lim,
285aa261f20SBart Van Assche 		const struct bio_vec *bv, unsigned *nsegs, unsigned *bytes,
286aa261f20SBart Van Assche 		unsigned max_segs, unsigned max_bytes)
287dcebd755SMing Lei {
28843c70b10SCaleb Sander Mateos 	unsigned max_len = max_bytes - *bytes;
289708b25b3SBart Van Assche 	unsigned len = min(bv->bv_len, max_len);
290dcebd755SMing Lei 	unsigned total_len = 0;
291ff9811b3SBart Van Assche 	unsigned seg_size = 0;
292dcebd755SMing Lei 
293ff9811b3SBart Van Assche 	while (len && *nsegs < max_segs) {
29409595e0cSChristoph Hellwig 		seg_size = get_max_segment_size(lim, bvec_phys(bv) + total_len, len);
295dcebd755SMing Lei 
296ff9811b3SBart Van Assche 		(*nsegs)++;
297dcebd755SMing Lei 		total_len += seg_size;
298dcebd755SMing Lei 		len -= seg_size;
299dcebd755SMing Lei 
300c55ddd90SChristoph Hellwig 		if ((bv->bv_offset + total_len) & lim->virt_boundary_mask)
301dcebd755SMing Lei 			break;
302dcebd755SMing Lei 	}
303dcebd755SMing Lei 
30467927d22SKeith Busch 	*bytes += total_len;
305dcebd755SMing Lei 
306708b25b3SBart Van Assche 	/* tell the caller to split the bvec if it is too big to fit */
307708b25b3SBart Van Assche 	return len > 0 || bv->bv_len > max_len;
308dcebd755SMing Lei }
309dcebd755SMing Lei 
bio_split_alignment(struct bio * bio,const struct queue_limits * lim)3107ecd2cd4SChristoph Hellwig static unsigned int bio_split_alignment(struct bio *bio,
3117ecd2cd4SChristoph Hellwig 		const struct queue_limits *lim)
3127ecd2cd4SChristoph Hellwig {
3137ecd2cd4SChristoph Hellwig 	if (op_is_write(bio_op(bio)) && lim->zone_write_granularity)
3147ecd2cd4SChristoph Hellwig 		return lim->zone_write_granularity;
3157ecd2cd4SChristoph Hellwig 	return lim->logical_block_size;
3167ecd2cd4SChristoph Hellwig }
3177ecd2cd4SChristoph Hellwig 
bvec_seg_gap(struct bio_vec * bvprv,struct bio_vec * bv)3182f6b2565SKeith Busch static inline unsigned int bvec_seg_gap(struct bio_vec *bvprv,
3192f6b2565SKeith Busch 					struct bio_vec *bv)
3202f6b2565SKeith Busch {
3212f6b2565SKeith Busch 	return bv->bv_offset | (bvprv->bv_offset + bvprv->bv_len);
3222f6b2565SKeith Busch }
3232f6b2565SKeith Busch 
324dad77584SBart Van Assche /**
325fec2e705SKeith Busch  * bio_split_io_at - check if and where to split a bio
326dad77584SBart Van Assche  * @bio:  [in] bio to be split
327c55ddd90SChristoph Hellwig  * @lim:  [in] queue limits to split based on
328dad77584SBart Van Assche  * @segs: [out] number of segments in the bio with the first half of the sectors
329a85b3637SChristoph Hellwig  * @max_bytes: [in] maximum number of bytes per bio
330fec2e705SKeith Busch  * @len_align_mask: [in] length alignment mask for each vector
331dad77584SBart Van Assche  *
332b35243a4SChristoph Hellwig  * Find out if @bio needs to be split to fit the queue limits in @lim and a
333b35243a4SChristoph Hellwig  * maximum size of @max_bytes.  Returns a negative error number if @bio can't be
334b35243a4SChristoph Hellwig  * split, 0 if the bio doesn't have to be split, or a positive sector offset if
335b35243a4SChristoph Hellwig  * @bio needs to be split.
336dad77584SBart Van Assche  */
bio_split_io_at(struct bio * bio,const struct queue_limits * lim,unsigned * segs,unsigned max_bytes,unsigned len_align_mask)337fec2e705SKeith Busch int bio_split_io_at(struct bio *bio, const struct queue_limits *lim,
338fec2e705SKeith Busch 		unsigned *segs, unsigned max_bytes, unsigned len_align_mask)
33954efd50bSKent Overstreet {
34066e5a11dSChristoph Hellwig 	struct bio_crypt_ctx *bc = bio_crypt_ctx(bio);
3415014c311SJens Axboe 	struct bio_vec bv, bvprv, *bvprvp = NULL;
3422f6b2565SKeith Busch 	unsigned nsegs = 0, bytes = 0, gaps = 0;
34354efd50bSKent Overstreet 	struct bvec_iter iter;
34466e5a11dSChristoph Hellwig 	unsigned start_align_mask = lim->dma_alignment;
34566e5a11dSChristoph Hellwig 
34666e5a11dSChristoph Hellwig 	if (bc) {
34766e5a11dSChristoph Hellwig 		start_align_mask |= (bc->bc_key->crypto_cfg.data_unit_size - 1);
34866e5a11dSChristoph Hellwig 		len_align_mask |= (bc->bc_key->crypto_cfg.data_unit_size - 1);
34966e5a11dSChristoph Hellwig 	}
35054efd50bSKent Overstreet 
351dcebd755SMing Lei 	bio_for_each_bvec(bv, bio, iter) {
35266e5a11dSChristoph Hellwig 		if (bv.bv_offset & start_align_mask ||
353fec2e705SKeith Busch 		    bv.bv_len & len_align_mask)
354fec2e705SKeith Busch 			return -EINVAL;
355fec2e705SKeith Busch 
35654efd50bSKent Overstreet 		/*
35754efd50bSKent Overstreet 		 * If the queue doesn't support SG gaps and adding this
35854efd50bSKent Overstreet 		 * offset would create a gap, disallow it.
35954efd50bSKent Overstreet 		 */
3602f6b2565SKeith Busch 		if (bvprvp) {
3612f6b2565SKeith Busch 			if (bvec_gap_to_prev(lim, bvprvp, bv.bv_offset))
36254efd50bSKent Overstreet 				goto split;
3632f6b2565SKeith Busch 			gaps |= bvec_seg_gap(bvprvp, &bv);
3642f6b2565SKeith Busch 		}
36554efd50bSKent Overstreet 
366c55ddd90SChristoph Hellwig 		if (nsegs < lim->max_segments &&
36767927d22SKeith Busch 		    bytes + bv.bv_len <= max_bytes &&
3685c5028eeSKeith Busch 		    bv.bv_offset + bv.bv_len <= lim->max_fast_segment_size) {
369708b25b3SBart Van Assche 			nsegs++;
37067927d22SKeith Busch 			bytes += bv.bv_len;
371c55ddd90SChristoph Hellwig 		} else {
372c55ddd90SChristoph Hellwig 			if (bvec_split_segs(lim, &bv, &nsegs, &bytes,
373c55ddd90SChristoph Hellwig 					lim->max_segments, max_bytes))
374e36f6204SKeith Busch 				goto split;
375e36f6204SKeith Busch 		}
376e36f6204SKeith Busch 
37754efd50bSKent Overstreet 		bvprv = bv;
378578270bfSMing Lei 		bvprvp = &bvprv;
37954efd50bSKent Overstreet 	}
38054efd50bSKent Overstreet 
381d627065dSChristoph Hellwig 	*segs = nsegs;
3822f6b2565SKeith Busch 	bio->bi_bvec_gap_bit = ffs(gaps);
383b35243a4SChristoph Hellwig 	return 0;
38454efd50bSKent Overstreet split:
385b35243a4SChristoph Hellwig 	if (bio->bi_opf & REQ_ATOMIC)
386b35243a4SChristoph Hellwig 		return -EINVAL;
387b35243a4SChristoph Hellwig 
3889cea62b2SJens Axboe 	/*
3899cea62b2SJens Axboe 	 * We can't sanely support splitting for a REQ_NOWAIT bio. End it
3909cea62b2SJens Axboe 	 * with EAGAIN if splitting is required and return an error pointer.
3919cea62b2SJens Axboe 	 */
392b35243a4SChristoph Hellwig 	if (bio->bi_opf & REQ_NOWAIT)
393b35243a4SChristoph Hellwig 		return -EAGAIN;
3949cea62b2SJens Axboe 
395bdced438SMing Lei 	*segs = nsegs;
396cc29e1bfSJeffle Xu 
397cc29e1bfSJeffle Xu 	/*
39867927d22SKeith Busch 	 * Individual bvecs might not be logical block aligned. Round down the
39967927d22SKeith Busch 	 * split size so that each bio is properly block size aligned, even if
40067927d22SKeith Busch 	 * we do not use the full hardware limits.
401fec2e705SKeith Busch 	 *
402fec2e705SKeith Busch 	 * It is possible to submit a bio that can't be split into a valid io:
403fec2e705SKeith Busch 	 * there may either be too many discontiguous vectors for the max
404fec2e705SKeith Busch 	 * segments limit, or contain virtual boundary gaps without having a
405fec2e705SKeith Busch 	 * valid block sized split. A zero byte result means one of those
406fec2e705SKeith Busch 	 * conditions occured.
40767927d22SKeith Busch 	 */
4087ecd2cd4SChristoph Hellwig 	bytes = ALIGN_DOWN(bytes, bio_split_alignment(bio, lim));
409fec2e705SKeith Busch 	if (!bytes)
410fec2e705SKeith Busch 		return -EINVAL;
41167927d22SKeith Busch 
41267927d22SKeith Busch 	/*
413cc29e1bfSJeffle Xu 	 * Bio splitting may cause subtle trouble such as hang when doing sync
414cc29e1bfSJeffle Xu 	 * iopoll in direct IO routine. Given performance gain of iopoll for
415cc29e1bfSJeffle Xu 	 * big IO can be trival, disable iopoll when split needed.
416cc29e1bfSJeffle Xu 	 */
4176ce913feSChristoph Hellwig 	bio_clear_polled(bio);
4182f6b2565SKeith Busch 	bio->bi_bvec_gap_bit = ffs(gaps);
419b35243a4SChristoph Hellwig 	return bytes >> SECTOR_SHIFT;
42054efd50bSKent Overstreet }
421fec2e705SKeith Busch EXPORT_SYMBOL_GPL(bio_split_io_at);
42254efd50bSKent Overstreet 
bio_split_rw(struct bio * bio,const struct queue_limits * lim,unsigned * nr_segs)423b35243a4SChristoph Hellwig struct bio *bio_split_rw(struct bio *bio, const struct queue_limits *lim,
424b35243a4SChristoph Hellwig 		unsigned *nr_segs)
42554efd50bSKent Overstreet {
426b35243a4SChristoph Hellwig 	return bio_submit_split(bio,
427b35243a4SChristoph Hellwig 		bio_split_rw_at(bio, lim, nr_segs,
428b35243a4SChristoph Hellwig 			get_max_io_size(bio, lim) << SECTOR_SHIFT));
42954efd50bSKent Overstreet }
43014ccb66bSChristoph Hellwig 
4311e8a7f6aSChristoph Hellwig /*
4321e8a7f6aSChristoph Hellwig  * REQ_OP_ZONE_APPEND bios must never be split by the block layer.
4331e8a7f6aSChristoph Hellwig  *
4341e8a7f6aSChristoph Hellwig  * But we want the nr_segs calculation provided by bio_split_rw_at, and having
4351e8a7f6aSChristoph Hellwig  * a good sanity check that the submitter built the bio correctly is nice to
4361e8a7f6aSChristoph Hellwig  * have as well.
4371e8a7f6aSChristoph Hellwig  */
bio_split_zone_append(struct bio * bio,const struct queue_limits * lim,unsigned * nr_segs)4381e8a7f6aSChristoph Hellwig struct bio *bio_split_zone_append(struct bio *bio,
4391e8a7f6aSChristoph Hellwig 		const struct queue_limits *lim, unsigned *nr_segs)
4401e8a7f6aSChristoph Hellwig {
4411e8a7f6aSChristoph Hellwig 	int split_sectors;
4421e8a7f6aSChristoph Hellwig 
4431e8a7f6aSChristoph Hellwig 	split_sectors = bio_split_rw_at(bio, lim, nr_segs,
444559218d4SChristoph Hellwig 			lim->max_zone_append_sectors << SECTOR_SHIFT);
4451e8a7f6aSChristoph Hellwig 	if (WARN_ON_ONCE(split_sectors > 0))
4461e8a7f6aSChristoph Hellwig 		split_sectors = -EINVAL;
4471e8a7f6aSChristoph Hellwig 	return bio_submit_split(bio, split_sectors);
4481e8a7f6aSChristoph Hellwig }
4491e8a7f6aSChristoph Hellwig 
bio_split_write_zeroes(struct bio * bio,const struct queue_limits * lim,unsigned * nsegs)45060dc5ea6SChristoph Hellwig struct bio *bio_split_write_zeroes(struct bio *bio,
45160dc5ea6SChristoph Hellwig 		const struct queue_limits *lim, unsigned *nsegs)
45260dc5ea6SChristoph Hellwig {
45360dc5ea6SChristoph Hellwig 	unsigned int max_sectors = get_max_io_size(bio, lim);
45460dc5ea6SChristoph Hellwig 
45560dc5ea6SChristoph Hellwig 	*nsegs = 0;
45660dc5ea6SChristoph Hellwig 
45760dc5ea6SChristoph Hellwig 	/*
45860dc5ea6SChristoph Hellwig 	 * An unset limit should normally not happen, as bio submission is keyed
45960dc5ea6SChristoph Hellwig 	 * off having a non-zero limit.  But SCSI can clear the limit in the
46060dc5ea6SChristoph Hellwig 	 * I/O completion handler, and we can race and see this.  Splitting to a
46160dc5ea6SChristoph Hellwig 	 * zero limit obviously doesn't make sense, so band-aid it here.
46260dc5ea6SChristoph Hellwig 	 */
46360dc5ea6SChristoph Hellwig 	if (!max_sectors)
46460dc5ea6SChristoph Hellwig 		return bio;
46560dc5ea6SChristoph Hellwig 	if (bio_sectors(bio) <= max_sectors)
46660dc5ea6SChristoph Hellwig 		return bio;
46760dc5ea6SChristoph Hellwig 	return bio_submit_split(bio, max_sectors);
46860dc5ea6SChristoph Hellwig }
46960dc5ea6SChristoph Hellwig 
470dad77584SBart Van Assche /**
4715a97806fSChristoph Hellwig  * bio_split_to_limits - split a bio to fit the queue limits
4725a97806fSChristoph Hellwig  * @bio:     bio to be split
473dad77584SBart Van Assche  *
4745a97806fSChristoph Hellwig  * Check if @bio needs splitting based on the queue limits of @bio->bi_bdev, and
4755a97806fSChristoph Hellwig  * if so split off a bio fitting the limits from the beginning of @bio and
4765a97806fSChristoph Hellwig  * return it.  @bio is shortened to the remainder and re-submitted.
4775a97806fSChristoph Hellwig  *
4785a97806fSChristoph Hellwig  * The split bio is allocated from @q->bio_split, which is provided by the
4795a97806fSChristoph Hellwig  * block layer.
480dad77584SBart Van Assche  */
bio_split_to_limits(struct bio * bio)4815a97806fSChristoph Hellwig struct bio *bio_split_to_limits(struct bio *bio)
48214ccb66bSChristoph Hellwig {
48314ccb66bSChristoph Hellwig 	unsigned int nr_segs;
48414ccb66bSChristoph Hellwig 
4852f5a65efSChristoph Hellwig 	return __bio_split_to_limits(bio, bdev_limits(bio->bi_bdev), &nr_segs);
48614ccb66bSChristoph Hellwig }
4875a97806fSChristoph Hellwig EXPORT_SYMBOL(bio_split_to_limits);
48854efd50bSKent Overstreet 
blk_recalc_rq_segments(struct request * rq)489e9cd19c0SChristoph Hellwig unsigned int blk_recalc_rq_segments(struct request *rq)
490d6d48196SJens Axboe {
4916869875fSChristoph Hellwig 	unsigned int nr_phys_segs = 0;
49267927d22SKeith Busch 	unsigned int bytes = 0;
493e9cd19c0SChristoph Hellwig 	struct req_iterator iter;
4946869875fSChristoph Hellwig 	struct bio_vec bv;
495d6d48196SJens Axboe 
496e9cd19c0SChristoph Hellwig 	if (!rq->bio)
4971e428079SJens Axboe 		return 0;
498d6d48196SJens Axboe 
499e9cd19c0SChristoph Hellwig 	switch (bio_op(rq->bio)) {
500a6f0788eSChaitanya Kulkarni 	case REQ_OP_DISCARD:
501a6f0788eSChaitanya Kulkarni 	case REQ_OP_SECURE_ERASE:
502a958937fSDavid Jeffery 		if (queue_max_discard_segments(rq->q) > 1) {
503a958937fSDavid Jeffery 			struct bio *bio = rq->bio;
504a958937fSDavid Jeffery 
505a958937fSDavid Jeffery 			for_each_bio(bio)
506a958937fSDavid Jeffery 				nr_phys_segs++;
507a958937fSDavid Jeffery 			return nr_phys_segs;
508a958937fSDavid Jeffery 		}
509a958937fSDavid Jeffery 		return 1;
510a6f0788eSChaitanya Kulkarni 	case REQ_OP_WRITE_ZEROES:
511f9d03f96SChristoph Hellwig 		return 0;
5122d9b02beSBart Van Assche 	default:
5132d9b02beSBart Van Assche 		break;
514a6f0788eSChaitanya Kulkarni 	}
5155cb8850cSKent Overstreet 
516e9cd19c0SChristoph Hellwig 	rq_for_each_bvec(bv, rq, iter)
517c55ddd90SChristoph Hellwig 		bvec_split_segs(&rq->q->limits, &bv, &nr_phys_segs, &bytes,
518fa0bdd45SChristoph Hellwig 				UINT_MAX, BIO_MAX_SIZE);
5191e428079SJens Axboe 	return nr_phys_segs;
5201e428079SJens Axboe }
5211e428079SJens Axboe 
blk_rq_get_max_sectors(struct request * rq,sector_t offset)522badf7f64SChristoph Hellwig static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
523badf7f64SChristoph Hellwig 						  sector_t offset)
524badf7f64SChristoph Hellwig {
525badf7f64SChristoph Hellwig 	struct request_queue *q = rq->q;
526f70167a7SJohn Garry 	struct queue_limits *lim = &q->limits;
527f70167a7SJohn Garry 	unsigned int max_sectors, boundary_sectors;
5289da3d1e9SJohn Garry 	bool is_atomic = rq->cmd_flags & REQ_ATOMIC;
529badf7f64SChristoph Hellwig 
530badf7f64SChristoph Hellwig 	if (blk_rq_is_passthrough(rq))
531badf7f64SChristoph Hellwig 		return q->limits.max_hw_sectors;
532badf7f64SChristoph Hellwig 
5339da3d1e9SJohn Garry 	boundary_sectors = blk_boundary_sectors(lim, is_atomic);
5348d1dfd51SJohn Garry 	max_sectors = blk_queue_get_max_sectors(rq);
5358d1dfd51SJohn Garry 
536f70167a7SJohn Garry 	if (!boundary_sectors ||
537badf7f64SChristoph Hellwig 	    req_op(rq) == REQ_OP_DISCARD ||
538badf7f64SChristoph Hellwig 	    req_op(rq) == REQ_OP_SECURE_ERASE)
539c8875190SChristoph Hellwig 		return max_sectors;
540c8875190SChristoph Hellwig 	return min(max_sectors,
541f70167a7SJohn Garry 		   blk_boundary_sectors_left(offset, boundary_sectors));
542badf7f64SChristoph Hellwig }
543badf7f64SChristoph Hellwig 
ll_new_hw_segment(struct request * req,struct bio * bio,unsigned int nr_phys_segs)54414ccb66bSChristoph Hellwig static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
54514ccb66bSChristoph Hellwig 		unsigned int nr_phys_segs)
546d6d48196SJens Axboe {
5476b2b0459STejun Heo 	if (!blk_cgroup_mergeable(req, bio))
5486b2b0459STejun Heo 		goto no_merge;
5496b2b0459STejun Heo 
5502705dfb2SMing Lei 	if (blk_integrity_merge_bio(req->q, req, bio) == false)
55113f05c8dSMartin K. Petersen 		goto no_merge;
55213f05c8dSMartin K. Petersen 
5532705dfb2SMing Lei 	/* discard request merge won't add new segment */
5542705dfb2SMing Lei 	if (req_op(req) == REQ_OP_DISCARD)
5552705dfb2SMing Lei 		return 1;
5562705dfb2SMing Lei 
5572705dfb2SMing Lei 	if (req->nr_phys_segments + nr_phys_segs > blk_rq_get_max_segments(req))
55813f05c8dSMartin K. Petersen 		goto no_merge;
559d6d48196SJens Axboe 
560d6d48196SJens Axboe 	/*
561d6d48196SJens Axboe 	 * This will form the start of a new hw segment.  Bump both
562d6d48196SJens Axboe 	 * counters.
563d6d48196SJens Axboe 	 */
564d6d48196SJens Axboe 	req->nr_phys_segments += nr_phys_segs;
565d148d750SKeith Busch 	if (bio_integrity(bio))
566d148d750SKeith Busch 		req->nr_integrity_segments += blk_rq_count_integrity_sg(req->q,
567d148d750SKeith Busch 									bio);
568d6d48196SJens Axboe 	return 1;
56913f05c8dSMartin K. Petersen 
57013f05c8dSMartin K. Petersen no_merge:
57114ccb66bSChristoph Hellwig 	req_set_nomerge(req->q, req);
57213f05c8dSMartin K. Petersen 	return 0;
573d6d48196SJens Axboe }
574d6d48196SJens Axboe 
ll_back_merge_fn(struct request * req,struct bio * bio,unsigned int nr_segs)57514ccb66bSChristoph Hellwig int ll_back_merge_fn(struct request *req, struct bio *bio, unsigned int nr_segs)
576d6d48196SJens Axboe {
5775e7c4274SJens Axboe 	if (req_gap_back_merge(req, bio))
5785e7c4274SJens Axboe 		return 0;
5797f39add3SSagi Grimberg 	if (blk_integrity_rq(req) &&
5807f39add3SSagi Grimberg 	    integrity_req_gap_back_merge(req, bio))
5817f39add3SSagi Grimberg 		return 0;
582a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_back_mergeable(req, bio))
583a892c8d5SSatya Tangirala 		return 0;
584f31dc1cdSMartin K. Petersen 	if (blk_rq_sectors(req) + bio_sectors(bio) >
58517007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, blk_rq_pos(req))) {
58614ccb66bSChristoph Hellwig 		req_set_nomerge(req->q, req);
587d6d48196SJens Axboe 		return 0;
588d6d48196SJens Axboe 	}
589d6d48196SJens Axboe 
59014ccb66bSChristoph Hellwig 	return ll_new_hw_segment(req, bio, nr_segs);
591d6d48196SJens Axboe }
592d6d48196SJens Axboe 
ll_front_merge_fn(struct request * req,struct bio * bio,unsigned int nr_segs)593eda5cc99SChristoph Hellwig static int ll_front_merge_fn(struct request *req, struct bio *bio,
594eda5cc99SChristoph Hellwig 		unsigned int nr_segs)
595d6d48196SJens Axboe {
5965e7c4274SJens Axboe 	if (req_gap_front_merge(req, bio))
5975e7c4274SJens Axboe 		return 0;
5987f39add3SSagi Grimberg 	if (blk_integrity_rq(req) &&
5997f39add3SSagi Grimberg 	    integrity_req_gap_front_merge(req, bio))
6007f39add3SSagi Grimberg 		return 0;
601a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_front_mergeable(req, bio))
602a892c8d5SSatya Tangirala 		return 0;
603f31dc1cdSMartin K. Petersen 	if (blk_rq_sectors(req) + bio_sectors(bio) >
60417007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) {
60514ccb66bSChristoph Hellwig 		req_set_nomerge(req->q, req);
606d6d48196SJens Axboe 		return 0;
607d6d48196SJens Axboe 	}
608d6d48196SJens Axboe 
60914ccb66bSChristoph Hellwig 	return ll_new_hw_segment(req, bio, nr_segs);
610d6d48196SJens Axboe }
611d6d48196SJens Axboe 
req_attempt_discard_merge(struct request_queue * q,struct request * req,struct request * next)612445251d0SJens Axboe static bool req_attempt_discard_merge(struct request_queue *q, struct request *req,
613445251d0SJens Axboe 		struct request *next)
614445251d0SJens Axboe {
615445251d0SJens Axboe 	unsigned short segments = blk_rq_nr_discard_segments(req);
616445251d0SJens Axboe 
617445251d0SJens Axboe 	if (segments >= queue_max_discard_segments(q))
618445251d0SJens Axboe 		goto no_merge;
619445251d0SJens Axboe 	if (blk_rq_sectors(req) + bio_sectors(next->bio) >
620445251d0SJens Axboe 	    blk_rq_get_max_sectors(req, blk_rq_pos(req)))
621445251d0SJens Axboe 		goto no_merge;
622445251d0SJens Axboe 
623445251d0SJens Axboe 	req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next);
624445251d0SJens Axboe 	return true;
625445251d0SJens Axboe no_merge:
626445251d0SJens Axboe 	req_set_nomerge(q, req);
627445251d0SJens Axboe 	return false;
628445251d0SJens Axboe }
629445251d0SJens Axboe 
ll_merge_requests_fn(struct request_queue * q,struct request * req,struct request * next)630d6d48196SJens Axboe static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
631d6d48196SJens Axboe 				struct request *next)
632d6d48196SJens Axboe {
633d6d48196SJens Axboe 	int total_phys_segments;
634d6d48196SJens Axboe 
6355e7c4274SJens Axboe 	if (req_gap_back_merge(req, next->bio))
636854fbb9cSKeith Busch 		return 0;
637854fbb9cSKeith Busch 
638d6d48196SJens Axboe 	/*
639d6d48196SJens Axboe 	 * Will it become too large?
640d6d48196SJens Axboe 	 */
641f31dc1cdSMartin K. Petersen 	if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
64217007f39SDamien Le Moal 	    blk_rq_get_max_sectors(req, blk_rq_pos(req)))
643d6d48196SJens Axboe 		return 0;
644d6d48196SJens Axboe 
645d6d48196SJens Axboe 	total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
646943b40c8SMing Lei 	if (total_phys_segments > blk_rq_get_max_segments(req))
647d6d48196SJens Axboe 		return 0;
648d6d48196SJens Axboe 
6496b2b0459STejun Heo 	if (!blk_cgroup_mergeable(req, next->bio))
6506b2b0459STejun Heo 		return 0;
6516b2b0459STejun Heo 
6524eaf99beSMartin K. Petersen 	if (blk_integrity_merge_rq(q, req, next) == false)
65313f05c8dSMartin K. Petersen 		return 0;
65413f05c8dSMartin K. Petersen 
655a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_merge_rq(req, next))
656a892c8d5SSatya Tangirala 		return 0;
657a892c8d5SSatya Tangirala 
658d6d48196SJens Axboe 	/* Merge is OK... */
659d6d48196SJens Axboe 	req->nr_phys_segments = total_phys_segments;
660d148d750SKeith Busch 	req->nr_integrity_segments += next->nr_integrity_segments;
661d6d48196SJens Axboe 	return 1;
662d6d48196SJens Axboe }
663d6d48196SJens Axboe 
66480a761fdSTejun Heo /**
66580a761fdSTejun Heo  * blk_rq_set_mixed_merge - mark a request as mixed merge
66680a761fdSTejun Heo  * @rq: request to mark as mixed merge
66780a761fdSTejun Heo  *
66880a761fdSTejun Heo  * Description:
66980a761fdSTejun Heo  *     @rq is about to be mixed merged.  Make sure the attributes
67080a761fdSTejun Heo  *     which can be mixed are set in each bio and mark @rq as mixed
67180a761fdSTejun Heo  *     merged.
67280a761fdSTejun Heo  */
blk_rq_set_mixed_merge(struct request * rq)673dc53d9eaSJohn Garry static void blk_rq_set_mixed_merge(struct request *rq)
67480a761fdSTejun Heo {
67516458cf3SBart Van Assche 	blk_opf_t ff = rq->cmd_flags & REQ_FAILFAST_MASK;
67680a761fdSTejun Heo 	struct bio *bio;
67780a761fdSTejun Heo 
678e8064021SChristoph Hellwig 	if (rq->rq_flags & RQF_MIXED_MERGE)
67980a761fdSTejun Heo 		return;
68080a761fdSTejun Heo 
68180a761fdSTejun Heo 	/*
68280a761fdSTejun Heo 	 * @rq will no longer represent mixable attributes for all the
68380a761fdSTejun Heo 	 * contained bios.  It will just track those of the first one.
68480a761fdSTejun Heo 	 * Distributes the attributs to each bio.
68580a761fdSTejun Heo 	 */
68680a761fdSTejun Heo 	for (bio = rq->bio; bio; bio = bio->bi_next) {
6871eff9d32SJens Axboe 		WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) &&
6881eff9d32SJens Axboe 			     (bio->bi_opf & REQ_FAILFAST_MASK) != ff);
6891eff9d32SJens Axboe 		bio->bi_opf |= ff;
69080a761fdSTejun Heo 	}
691e8064021SChristoph Hellwig 	rq->rq_flags |= RQF_MIXED_MERGE;
69280a761fdSTejun Heo }
69380a761fdSTejun Heo 
bio_failfast(const struct bio * bio)694f3ca7386SJens Axboe static inline blk_opf_t bio_failfast(const struct bio *bio)
6953ce6a115SMing Lei {
6963ce6a115SMing Lei 	if (bio->bi_opf & REQ_RAHEAD)
6973ce6a115SMing Lei 		return REQ_FAILFAST_MASK;
6983ce6a115SMing Lei 
6993ce6a115SMing Lei 	return bio->bi_opf & REQ_FAILFAST_MASK;
7003ce6a115SMing Lei }
7013ce6a115SMing Lei 
7023ce6a115SMing Lei /*
7033ce6a115SMing Lei  * After we are marked as MIXED_MERGE, any new RA bio has to be updated
7043ce6a115SMing Lei  * as failfast, and request's failfast has to be updated in case of
7053ce6a115SMing Lei  * front merge.
7063ce6a115SMing Lei  */
blk_update_mixed_merge(struct request * req,struct bio * bio,bool front_merge)7073ce6a115SMing Lei static inline void blk_update_mixed_merge(struct request *req,
7083ce6a115SMing Lei 		struct bio *bio, bool front_merge)
7093ce6a115SMing Lei {
7103ce6a115SMing Lei 	if (req->rq_flags & RQF_MIXED_MERGE) {
7113ce6a115SMing Lei 		if (bio->bi_opf & REQ_RAHEAD)
7123ce6a115SMing Lei 			bio->bi_opf |= REQ_FAILFAST_MASK;
7133ce6a115SMing Lei 
7143ce6a115SMing Lei 		if (front_merge) {
7153ce6a115SMing Lei 			req->cmd_flags &= ~REQ_FAILFAST_MASK;
7163ce6a115SMing Lei 			req->cmd_flags |= bio->bi_opf & REQ_FAILFAST_MASK;
7173ce6a115SMing Lei 		}
7183ce6a115SMing Lei 	}
7193ce6a115SMing Lei }
7203ce6a115SMing Lei 
blk_account_io_merge_request(struct request * req)721b9c54f56SKonstantin Khlebnikov static void blk_account_io_merge_request(struct request *req)
72226308eabSJerome Marchand {
723e3569ecaSJens Axboe 	if (req->rq_flags & RQF_IO_STAT) {
724112f158fSMike Snitzer 		part_stat_lock();
725b9c54f56SKonstantin Khlebnikov 		part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
72699dc4223SYu Kuai 		part_stat_local_dec(req->part,
72799dc4223SYu Kuai 				    in_flight[op_is_write(req_op(req))]);
72826308eabSJerome Marchand 		part_stat_unlock();
72926308eabSJerome Marchand 	}
73026308eabSJerome Marchand }
731b9c54f56SKonstantin Khlebnikov 
blk_try_req_merge(struct request * req,struct request * next)732e96c0d83SEric Biggers static enum elv_merge blk_try_req_merge(struct request *req,
733e96c0d83SEric Biggers 					struct request *next)
73469840466SJianchao Wang {
735caebce24SJens Axboe 	if (blk_discard_mergable(req))
7362516c246SKeith Busch 		return ELEVATOR_DISCARD_MERGE;
737caebce24SJens Axboe 	else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
738caebce24SJens Axboe 		return ELEVATOR_BACK_MERGE;
73969840466SJianchao Wang 
74069840466SJianchao Wang 	return ELEVATOR_NO_MERGE;
74169840466SJianchao Wang }
74226308eabSJerome Marchand 
blk_atomic_write_mergeable_rq_bio(struct request * rq,struct bio * bio)7439da3d1e9SJohn Garry static bool blk_atomic_write_mergeable_rq_bio(struct request *rq,
7449da3d1e9SJohn Garry 					      struct bio *bio)
7459da3d1e9SJohn Garry {
7469da3d1e9SJohn Garry 	return (rq->cmd_flags & REQ_ATOMIC) == (bio->bi_opf & REQ_ATOMIC);
7479da3d1e9SJohn Garry }
7489da3d1e9SJohn Garry 
blk_atomic_write_mergeable_rqs(struct request * rq,struct request * next)7499da3d1e9SJohn Garry static bool blk_atomic_write_mergeable_rqs(struct request *rq,
7509da3d1e9SJohn Garry 					   struct request *next)
7519da3d1e9SJohn Garry {
7529da3d1e9SJohn Garry 	return (rq->cmd_flags & REQ_ATOMIC) == (next->cmd_flags & REQ_ATOMIC);
7539da3d1e9SJohn Garry }
7549da3d1e9SJohn Garry 
bio_seg_gap(struct request_queue * q,struct bio * prev,struct bio * next,u8 gaps_bit)7552f6b2565SKeith Busch u8 bio_seg_gap(struct request_queue *q, struct bio *prev, struct bio *next,
7562f6b2565SKeith Busch 	       u8 gaps_bit)
7572f6b2565SKeith Busch {
7582f6b2565SKeith Busch 	struct bio_vec pb, nb;
7592f6b2565SKeith Busch 
760fd9ecd00SKeith Busch 	if (!bio_has_data(prev))
761fd9ecd00SKeith Busch 		return 0;
762fd9ecd00SKeith Busch 
7632f6b2565SKeith Busch 	gaps_bit = min_not_zero(gaps_bit, prev->bi_bvec_gap_bit);
7642f6b2565SKeith Busch 	gaps_bit = min_not_zero(gaps_bit, next->bi_bvec_gap_bit);
7652f6b2565SKeith Busch 
7662f6b2565SKeith Busch 	bio_get_last_bvec(prev, &pb);
7672f6b2565SKeith Busch 	bio_get_first_bvec(next, &nb);
7682f6b2565SKeith Busch 	if (!biovec_phys_mergeable(q, &pb, &nb))
7692f6b2565SKeith Busch 		gaps_bit = min_not_zero(gaps_bit, ffs(bvec_seg_gap(&pb, &nb)));
7702f6b2565SKeith Busch 	return gaps_bit;
7712f6b2565SKeith Busch }
7722f6b2565SKeith Busch 
773d6d48196SJens Axboe /*
774b973cb7eSJens Axboe  * For non-mq, this has to be called with the request spinlock acquired.
775b973cb7eSJens Axboe  * For mq with scheduling, the appropriate queue wide lock should be held.
776d6d48196SJens Axboe  */
attempt_merge(struct request_queue * q,struct request * req,struct request * next)777b973cb7eSJens Axboe static struct request *attempt_merge(struct request_queue *q,
778b973cb7eSJens Axboe 				     struct request *req, struct request *next)
779d6d48196SJens Axboe {
780d6d48196SJens Axboe 	if (!rq_mergeable(req) || !rq_mergeable(next))
781b973cb7eSJens Axboe 		return NULL;
782d6d48196SJens Axboe 
783288dab8aSChristoph Hellwig 	if (req_op(req) != req_op(next))
784b973cb7eSJens Axboe 		return NULL;
785f31dc1cdSMartin K. Petersen 
78661952bb7SChristoph Hellwig 	if (req->bio->bi_write_hint != next->bio->bi_write_hint)
78744981351SBart Van Assche 		return NULL;
7885006f85eSChristoph Hellwig 	if (req->bio->bi_write_stream != next->bio->bi_write_stream)
7895006f85eSChristoph Hellwig 		return NULL;
7906975c1a4SChristoph Hellwig 	if (req->bio->bi_ioprio != next->bio->bi_ioprio)
791668ffc03SDamien Le Moal 		return NULL;
7929da3d1e9SJohn Garry 	if (!blk_atomic_write_mergeable_rqs(req, next))
7939da3d1e9SJohn Garry 		return NULL;
7949da3d1e9SJohn Garry 
795cb6934f8SJens Axboe 	/*
796d6d48196SJens Axboe 	 * If we are allowed to merge, then append bio list
797d6d48196SJens Axboe 	 * from next to rq and release next. merge_requests_fn
798d6d48196SJens Axboe 	 * will have updated segment counts, update sector
799445251d0SJens Axboe 	 * counts here. Handle DISCARDs separately, as they
800445251d0SJens Axboe 	 * have separate settings.
801d6d48196SJens Axboe 	 */
80269840466SJianchao Wang 
80369840466SJianchao Wang 	switch (blk_try_req_merge(req, next)) {
80469840466SJianchao Wang 	case ELEVATOR_DISCARD_MERGE:
805445251d0SJens Axboe 		if (!req_attempt_discard_merge(q, req, next))
806445251d0SJens Axboe 			return NULL;
80769840466SJianchao Wang 		break;
80869840466SJianchao Wang 	case ELEVATOR_BACK_MERGE:
80969840466SJianchao Wang 		if (!ll_merge_requests_fn(q, req, next))
810b973cb7eSJens Axboe 			return NULL;
81169840466SJianchao Wang 		break;
81269840466SJianchao Wang 	default:
81369840466SJianchao Wang 		return NULL;
81469840466SJianchao Wang 	}
815d6d48196SJens Axboe 
816d6d48196SJens Axboe 	/*
81780a761fdSTejun Heo 	 * If failfast settings disagree or any of the two is already
81880a761fdSTejun Heo 	 * a mixed merge, mark both as mixed before proceeding.  This
81980a761fdSTejun Heo 	 * makes sure that all involved bios have mixable attributes
82080a761fdSTejun Heo 	 * set properly.
82180a761fdSTejun Heo 	 */
822e8064021SChristoph Hellwig 	if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) ||
82380a761fdSTejun Heo 	    (req->cmd_flags & REQ_FAILFAST_MASK) !=
82480a761fdSTejun Heo 	    (next->cmd_flags & REQ_FAILFAST_MASK)) {
82580a761fdSTejun Heo 		blk_rq_set_mixed_merge(req);
82680a761fdSTejun Heo 		blk_rq_set_mixed_merge(next);
82780a761fdSTejun Heo 	}
82880a761fdSTejun Heo 
82980a761fdSTejun Heo 	/*
830522a7775SOmar Sandoval 	 * At this point we have either done a back merge or front merge. We
831522a7775SOmar Sandoval 	 * need the smaller start_time_ns of the merged requests to be the
832522a7775SOmar Sandoval 	 * current request for accounting purposes.
833d6d48196SJens Axboe 	 */
834522a7775SOmar Sandoval 	if (next->start_time_ns < req->start_time_ns)
835522a7775SOmar Sandoval 		req->start_time_ns = next->start_time_ns;
836d6d48196SJens Axboe 
8372f6b2565SKeith Busch 	req->phys_gap_bit = bio_seg_gap(req->q, req->biotail, next->bio,
8382f6b2565SKeith Busch 					min_not_zero(next->phys_gap_bit,
8392f6b2565SKeith Busch 						     req->phys_gap_bit));
840d6d48196SJens Axboe 	req->biotail->bi_next = next->bio;
841d6d48196SJens Axboe 	req->biotail = next->biotail;
842d6d48196SJens Axboe 
843a2dec7b3STejun Heo 	req->__data_len += blk_rq_bytes(next);
844d6d48196SJens Axboe 
8452a5cf35cSMing Lei 	if (!blk_discard_mergable(req))
846d6d48196SJens Axboe 		elv_merge_requests(q, req, next);
847d6d48196SJens Axboe 
8489cd1e566SEric Biggers 	blk_crypto_rq_put_keyslot(next);
8499cd1e566SEric Biggers 
85042dad764SJerome Marchand 	/*
85142dad764SJerome Marchand 	 * 'next' is going away, so update stats accordingly
85242dad764SJerome Marchand 	 */
853b9c54f56SKonstantin Khlebnikov 	blk_account_io_merge_request(next);
854d6d48196SJens Axboe 
855a54895faSChristoph Hellwig 	trace_block_rq_merge(next);
856f3bdc62fSJan Kara 
857e4d750c9SJens Axboe 	/*
858e4d750c9SJens Axboe 	 * ownership of bio passed from next to req, return 'next' for
859e4d750c9SJens Axboe 	 * the caller to free
860e4d750c9SJens Axboe 	 */
8611cd96c24SBoaz Harrosh 	next->bio = NULL;
862b973cb7eSJens Axboe 	return next;
863d6d48196SJens Axboe }
864d6d48196SJens Axboe 
attempt_back_merge(struct request_queue * q,struct request * rq)865eda5cc99SChristoph Hellwig static struct request *attempt_back_merge(struct request_queue *q,
866eda5cc99SChristoph Hellwig 		struct request *rq)
867d6d48196SJens Axboe {
868d6d48196SJens Axboe 	struct request *next = elv_latter_request(q, rq);
869d6d48196SJens Axboe 
870d6d48196SJens Axboe 	if (next)
871d6d48196SJens Axboe 		return attempt_merge(q, rq, next);
872d6d48196SJens Axboe 
873b973cb7eSJens Axboe 	return NULL;
874d6d48196SJens Axboe }
875d6d48196SJens Axboe 
attempt_front_merge(struct request_queue * q,struct request * rq)876eda5cc99SChristoph Hellwig static struct request *attempt_front_merge(struct request_queue *q,
877eda5cc99SChristoph Hellwig 		struct request *rq)
878d6d48196SJens Axboe {
879d6d48196SJens Axboe 	struct request *prev = elv_former_request(q, rq);
880d6d48196SJens Axboe 
881d6d48196SJens Axboe 	if (prev)
882d6d48196SJens Axboe 		return attempt_merge(q, prev, rq);
883d6d48196SJens Axboe 
884b973cb7eSJens Axboe 	return NULL;
885d6d48196SJens Axboe }
8865e84ea3aSJens Axboe 
887fd2ef39cSJan Kara /*
888fd2ef39cSJan Kara  * Try to merge 'next' into 'rq'. Return true if the merge happened, false
889fd2ef39cSJan Kara  * otherwise. The caller is responsible for freeing 'next' if the merge
890fd2ef39cSJan Kara  * happened.
891fd2ef39cSJan Kara  */
blk_attempt_req_merge(struct request_queue * q,struct request * rq,struct request * next)892fd2ef39cSJan Kara bool blk_attempt_req_merge(struct request_queue *q, struct request *rq,
8935e84ea3aSJens Axboe 			   struct request *next)
8945e84ea3aSJens Axboe {
895fd2ef39cSJan Kara 	return attempt_merge(q, rq, next);
8965e84ea3aSJens Axboe }
897050c8ea8STejun Heo 
blk_rq_merge_ok(struct request * rq,struct bio * bio)898050c8ea8STejun Heo bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
899050c8ea8STejun Heo {
900e2a60da7SMartin K. Petersen 	if (!rq_mergeable(rq) || !bio_mergeable(bio))
901050c8ea8STejun Heo 		return false;
902050c8ea8STejun Heo 
903288dab8aSChristoph Hellwig 	if (req_op(rq) != bio_op(bio))
904f31dc1cdSMartin K. Petersen 		return false;
905f31dc1cdSMartin K. Petersen 
9066b2b0459STejun Heo 	if (!blk_cgroup_mergeable(rq, bio))
9076b2b0459STejun Heo 		return false;
9084eaf99beSMartin K. Petersen 	if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
909050c8ea8STejun Heo 		return false;
910a892c8d5SSatya Tangirala 	if (!bio_crypt_rq_ctx_compatible(rq, bio))
911a892c8d5SSatya Tangirala 		return false;
91261952bb7SChristoph Hellwig 	if (rq->bio->bi_write_hint != bio->bi_write_hint)
91344981351SBart Van Assche 		return false;
9145006f85eSChristoph Hellwig 	if (rq->bio->bi_write_stream != bio->bi_write_stream)
9155006f85eSChristoph Hellwig 		return false;
9166975c1a4SChristoph Hellwig 	if (rq->bio->bi_ioprio != bio->bi_ioprio)
917668ffc03SDamien Le Moal 		return false;
9189da3d1e9SJohn Garry 	if (blk_atomic_write_mergeable_rq_bio(rq, bio) == false)
9199da3d1e9SJohn Garry 		return false;
9209da3d1e9SJohn Garry 
921050c8ea8STejun Heo 	return true;
922050c8ea8STejun Heo }
923050c8ea8STejun Heo 
blk_try_merge(struct request * rq,struct bio * bio)92434fe7c05SChristoph Hellwig enum elv_merge blk_try_merge(struct request *rq, struct bio *bio)
925050c8ea8STejun Heo {
926caebce24SJens Axboe 	if (blk_discard_mergable(rq))
927caebce24SJens Axboe 		return ELEVATOR_DISCARD_MERGE;
928caebce24SJens Axboe 	else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
929050c8ea8STejun Heo 		return ELEVATOR_BACK_MERGE;
9304f024f37SKent Overstreet 	else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
931050c8ea8STejun Heo 		return ELEVATOR_FRONT_MERGE;
932050c8ea8STejun Heo 	return ELEVATOR_NO_MERGE;
933050c8ea8STejun Heo }
9348e756373SBaolin Wang 
blk_account_io_merge_bio(struct request * req)9358e756373SBaolin Wang static void blk_account_io_merge_bio(struct request *req)
9368e756373SBaolin Wang {
937e3569ecaSJens Axboe 	if (req->rq_flags & RQF_IO_STAT) {
9388e756373SBaolin Wang 		part_stat_lock();
9398e756373SBaolin Wang 		part_stat_inc(req->part, merges[op_stat_group(req_op(req))]);
9408e756373SBaolin Wang 		part_stat_unlock();
9418e756373SBaolin Wang 	}
942e3569ecaSJens Axboe }
9438e756373SBaolin Wang 
bio_attempt_back_merge(struct request * req,struct bio * bio,unsigned int nr_segs)944dd850ff3SDamien Le Moal enum bio_merge_status bio_attempt_back_merge(struct request *req,
945eda5cc99SChristoph Hellwig 		struct bio *bio, unsigned int nr_segs)
9468e756373SBaolin Wang {
9473ce6a115SMing Lei 	const blk_opf_t ff = bio_failfast(bio);
9488e756373SBaolin Wang 
9498e756373SBaolin Wang 	if (!ll_back_merge_fn(req, bio, nr_segs))
9507d7ca7c5SBaolin Wang 		return BIO_MERGE_FAILED;
9518e756373SBaolin Wang 
952e8a676d6SChristoph Hellwig 	trace_block_bio_backmerge(bio);
9538e756373SBaolin Wang 	rq_qos_merge(req->q, req, bio);
9548e756373SBaolin Wang 
9558e756373SBaolin Wang 	if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
9568e756373SBaolin Wang 		blk_rq_set_mixed_merge(req);
9578e756373SBaolin Wang 
9583ce6a115SMing Lei 	blk_update_mixed_merge(req, bio, false);
9593ce6a115SMing Lei 
960dd291d77SDamien Le Moal 	if (req->rq_flags & RQF_ZONE_WRITE_PLUGGING)
961dd291d77SDamien Le Moal 		blk_zone_write_plug_bio_merged(bio);
962dd291d77SDamien Le Moal 
9632f6b2565SKeith Busch 	req->phys_gap_bit = bio_seg_gap(req->q, req->biotail, bio,
9642f6b2565SKeith Busch 					req->phys_gap_bit);
9658e756373SBaolin Wang 	req->biotail->bi_next = bio;
9668e756373SBaolin Wang 	req->biotail = bio;
9678e756373SBaolin Wang 	req->__data_len += bio->bi_iter.bi_size;
9688e756373SBaolin Wang 
9698e756373SBaolin Wang 	bio_crypt_free_ctx(bio);
9708e756373SBaolin Wang 
9718e756373SBaolin Wang 	blk_account_io_merge_bio(req);
9727d7ca7c5SBaolin Wang 	return BIO_MERGE_OK;
9738e756373SBaolin Wang }
9748e756373SBaolin Wang 
bio_attempt_front_merge(struct request * req,struct bio * bio,unsigned int nr_segs)975eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_front_merge(struct request *req,
976eda5cc99SChristoph Hellwig 		struct bio *bio, unsigned int nr_segs)
9778e756373SBaolin Wang {
9783ce6a115SMing Lei 	const blk_opf_t ff = bio_failfast(bio);
9798e756373SBaolin Wang 
980dd291d77SDamien Le Moal 	/*
981dd291d77SDamien Le Moal 	 * A front merge for writes to sequential zones of a zoned block device
982dd291d77SDamien Le Moal 	 * can happen only if the user submitted writes out of order. Do not
983dd291d77SDamien Le Moal 	 * merge such write to let it fail.
984dd291d77SDamien Le Moal 	 */
985dd291d77SDamien Le Moal 	if (req->rq_flags & RQF_ZONE_WRITE_PLUGGING)
986dd291d77SDamien Le Moal 		return BIO_MERGE_FAILED;
987dd291d77SDamien Le Moal 
9888e756373SBaolin Wang 	if (!ll_front_merge_fn(req, bio, nr_segs))
9897d7ca7c5SBaolin Wang 		return BIO_MERGE_FAILED;
9908e756373SBaolin Wang 
991e8a676d6SChristoph Hellwig 	trace_block_bio_frontmerge(bio);
9928e756373SBaolin Wang 	rq_qos_merge(req->q, req, bio);
9938e756373SBaolin Wang 
9948e756373SBaolin Wang 	if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
9958e756373SBaolin Wang 		blk_rq_set_mixed_merge(req);
9968e756373SBaolin Wang 
9973ce6a115SMing Lei 	blk_update_mixed_merge(req, bio, true);
9983ce6a115SMing Lei 
9992f6b2565SKeith Busch 	req->phys_gap_bit = bio_seg_gap(req->q, bio, req->bio,
10002f6b2565SKeith Busch 					req->phys_gap_bit);
10018e756373SBaolin Wang 	bio->bi_next = req->bio;
10028e756373SBaolin Wang 	req->bio = bio;
10038e756373SBaolin Wang 
10048e756373SBaolin Wang 	req->__sector = bio->bi_iter.bi_sector;
10058e756373SBaolin Wang 	req->__data_len += bio->bi_iter.bi_size;
10068e756373SBaolin Wang 
10078e756373SBaolin Wang 	bio_crypt_do_front_merge(req, bio);
10088e756373SBaolin Wang 
10098e756373SBaolin Wang 	blk_account_io_merge_bio(req);
10107d7ca7c5SBaolin Wang 	return BIO_MERGE_OK;
10118e756373SBaolin Wang }
10128e756373SBaolin Wang 
bio_attempt_discard_merge(struct request_queue * q,struct request * req,struct bio * bio)1013eda5cc99SChristoph Hellwig static enum bio_merge_status bio_attempt_discard_merge(struct request_queue *q,
1014eda5cc99SChristoph Hellwig 		struct request *req, struct bio *bio)
10158e756373SBaolin Wang {
10168e756373SBaolin Wang 	unsigned short segments = blk_rq_nr_discard_segments(req);
10178e756373SBaolin Wang 
10188e756373SBaolin Wang 	if (segments >= queue_max_discard_segments(q))
10198e756373SBaolin Wang 		goto no_merge;
10208e756373SBaolin Wang 	if (blk_rq_sectors(req) + bio_sectors(bio) >
10218e756373SBaolin Wang 	    blk_rq_get_max_sectors(req, blk_rq_pos(req)))
10228e756373SBaolin Wang 		goto no_merge;
10238e756373SBaolin Wang 
10248e756373SBaolin Wang 	rq_qos_merge(q, req, bio);
10258e756373SBaolin Wang 
10268e756373SBaolin Wang 	req->biotail->bi_next = bio;
10278e756373SBaolin Wang 	req->biotail = bio;
10288e756373SBaolin Wang 	req->__data_len += bio->bi_iter.bi_size;
10298e756373SBaolin Wang 	req->nr_phys_segments = segments + 1;
10308e756373SBaolin Wang 
10318e756373SBaolin Wang 	blk_account_io_merge_bio(req);
10327d7ca7c5SBaolin Wang 	return BIO_MERGE_OK;
10338e756373SBaolin Wang no_merge:
10348e756373SBaolin Wang 	req_set_nomerge(q, req);
10357d7ca7c5SBaolin Wang 	return BIO_MERGE_FAILED;
10367d7ca7c5SBaolin Wang }
10377d7ca7c5SBaolin Wang 
blk_attempt_bio_merge(struct request_queue * q,struct request * rq,struct bio * bio,unsigned int nr_segs,bool sched_allow_merge)10387d7ca7c5SBaolin Wang static enum bio_merge_status blk_attempt_bio_merge(struct request_queue *q,
10397d7ca7c5SBaolin Wang 						   struct request *rq,
10407d7ca7c5SBaolin Wang 						   struct bio *bio,
10417d7ca7c5SBaolin Wang 						   unsigned int nr_segs,
10427d7ca7c5SBaolin Wang 						   bool sched_allow_merge)
10437d7ca7c5SBaolin Wang {
10447d7ca7c5SBaolin Wang 	if (!blk_rq_merge_ok(rq, bio))
10457d7ca7c5SBaolin Wang 		return BIO_MERGE_NONE;
10467d7ca7c5SBaolin Wang 
10477d7ca7c5SBaolin Wang 	switch (blk_try_merge(rq, bio)) {
10487d7ca7c5SBaolin Wang 	case ELEVATOR_BACK_MERGE:
1049265600b7SBaolin Wang 		if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio))
10507d7ca7c5SBaolin Wang 			return bio_attempt_back_merge(rq, bio, nr_segs);
10517d7ca7c5SBaolin Wang 		break;
10527d7ca7c5SBaolin Wang 	case ELEVATOR_FRONT_MERGE:
1053265600b7SBaolin Wang 		if (!sched_allow_merge || blk_mq_sched_allow_merge(q, rq, bio))
10547d7ca7c5SBaolin Wang 			return bio_attempt_front_merge(rq, bio, nr_segs);
10557d7ca7c5SBaolin Wang 		break;
10567d7ca7c5SBaolin Wang 	case ELEVATOR_DISCARD_MERGE:
10577d7ca7c5SBaolin Wang 		return bio_attempt_discard_merge(q, rq, bio);
10587d7ca7c5SBaolin Wang 	default:
10597d7ca7c5SBaolin Wang 		return BIO_MERGE_NONE;
10607d7ca7c5SBaolin Wang 	}
10617d7ca7c5SBaolin Wang 
10627d7ca7c5SBaolin Wang 	return BIO_MERGE_FAILED;
10638e756373SBaolin Wang }
10648e756373SBaolin Wang 
10658e756373SBaolin Wang /**
10668e756373SBaolin Wang  * blk_attempt_plug_merge - try to merge with %current's plugged list
10678e756373SBaolin Wang  * @q: request_queue new bio is being queued at
10688e756373SBaolin Wang  * @bio: new bio being queued
10698e756373SBaolin Wang  * @nr_segs: number of segments in @bio
107087c037d1SJens Axboe  * from the passed in @q already in the plug list
10718e756373SBaolin Wang  *
1072d38a9c04SJens Axboe  * Determine whether @bio being queued on @q can be merged with the previous
1073d38a9c04SJens Axboe  * request on %current's plugged list.  Returns %true if merge was successful,
10748e756373SBaolin Wang  * otherwise %false.
10758e756373SBaolin Wang  *
10768e756373SBaolin Wang  * Plugging coalesces IOs from the same issuer for the same purpose without
10778e756373SBaolin Wang  * going through @q->queue_lock.  As such it's more of an issuing mechanism
10788e756373SBaolin Wang  * than scheduling, and the request, while may have elvpriv data, is not
10798e756373SBaolin Wang  * added on the elevator at this point.  In addition, we don't have
10808e756373SBaolin Wang  * reliable access to the elevator outside queue lock.  Only check basic
10818e756373SBaolin Wang  * merging parameters without querying the elevator.
10828e756373SBaolin Wang  *
10838e756373SBaolin Wang  * Caller must ensure !blk_queue_nomerges(q) beforehand.
10848e756373SBaolin Wang  */
blk_attempt_plug_merge(struct request_queue * q,struct bio * bio,unsigned int nr_segs)10858e756373SBaolin Wang bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
10860c5bcc92SChristoph Hellwig 		unsigned int nr_segs)
10878e756373SBaolin Wang {
108899a9476bSDamien Le Moal 	struct blk_plug *plug = current->plug;
10898e756373SBaolin Wang 	struct request *rq;
10908e756373SBaolin Wang 
1091a3396b99SChristoph Hellwig 	if (!plug || rq_list_empty(&plug->mq_list))
10928e756373SBaolin Wang 		return false;
10938e756373SBaolin Wang 
1094961296e8SJens Axboe 	rq = plug->mq_list.tail;
1095961296e8SJens Axboe 	if (rq->q == q)
1096961296e8SJens Axboe 		return blk_attempt_bio_merge(q, rq, bio, nr_segs, false) ==
1097961296e8SJens Axboe 			BIO_MERGE_OK;
1098961296e8SJens Axboe 	else if (!plug->multiple_queues)
1099961296e8SJens Axboe 		return false;
1100961296e8SJens Axboe 
11015b205071SJens Axboe 	rq_list_for_each(&plug->mq_list, rq) {
1102961296e8SJens Axboe 		if (rq->q != q)
1103961296e8SJens Axboe 			continue;
1104a1cb6537SMing Lei 		if (blk_attempt_bio_merge(q, rq, bio, nr_segs, false) ==
1105a1cb6537SMing Lei 		    BIO_MERGE_OK)
11068e756373SBaolin Wang 			return true;
11075b205071SJens Axboe 		break;
11085b205071SJens Axboe 	}
11098e756373SBaolin Wang 	return false;
11108e756373SBaolin Wang }
1111bdc6a287SBaolin Wang 
1112bdc6a287SBaolin Wang /*
1113bdc6a287SBaolin Wang  * Iterate list of requests and see if we can merge this bio with any
1114bdc6a287SBaolin Wang  * of them.
1115bdc6a287SBaolin Wang  */
blk_bio_list_merge(struct request_queue * q,struct list_head * list,struct bio * bio,unsigned int nr_segs)1116bdc6a287SBaolin Wang bool blk_bio_list_merge(struct request_queue *q, struct list_head *list,
1117bdc6a287SBaolin Wang 			struct bio *bio, unsigned int nr_segs)
1118bdc6a287SBaolin Wang {
1119bdc6a287SBaolin Wang 	struct request *rq;
1120bdc6a287SBaolin Wang 	int checked = 8;
1121bdc6a287SBaolin Wang 
1122bdc6a287SBaolin Wang 	list_for_each_entry_reverse(rq, list, queuelist) {
1123bdc6a287SBaolin Wang 		if (!checked--)
1124bdc6a287SBaolin Wang 			break;
1125bdc6a287SBaolin Wang 
11267d7ca7c5SBaolin Wang 		switch (blk_attempt_bio_merge(q, rq, bio, nr_segs, true)) {
11277d7ca7c5SBaolin Wang 		case BIO_MERGE_NONE:
1128bdc6a287SBaolin Wang 			continue;
11297d7ca7c5SBaolin Wang 		case BIO_MERGE_OK:
11307d7ca7c5SBaolin Wang 			return true;
11317d7ca7c5SBaolin Wang 		case BIO_MERGE_FAILED:
11327d7ca7c5SBaolin Wang 			return false;
1133bdc6a287SBaolin Wang 		}
1134bdc6a287SBaolin Wang 
1135bdc6a287SBaolin Wang 	}
1136bdc6a287SBaolin Wang 
1137bdc6a287SBaolin Wang 	return false;
1138bdc6a287SBaolin Wang }
1139bdc6a287SBaolin Wang EXPORT_SYMBOL_GPL(blk_bio_list_merge);
1140eda5cc99SChristoph Hellwig 
blk_mq_sched_try_merge(struct request_queue * q,struct bio * bio,unsigned int nr_segs,struct request ** merged_request)1141eda5cc99SChristoph Hellwig bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
1142eda5cc99SChristoph Hellwig 		unsigned int nr_segs, struct request **merged_request)
1143eda5cc99SChristoph Hellwig {
1144eda5cc99SChristoph Hellwig 	struct request *rq;
1145eda5cc99SChristoph Hellwig 
1146eda5cc99SChristoph Hellwig 	switch (elv_merge(q, &rq, bio)) {
1147eda5cc99SChristoph Hellwig 	case ELEVATOR_BACK_MERGE:
1148eda5cc99SChristoph Hellwig 		if (!blk_mq_sched_allow_merge(q, rq, bio))
1149eda5cc99SChristoph Hellwig 			return false;
1150eda5cc99SChristoph Hellwig 		if (bio_attempt_back_merge(rq, bio, nr_segs) != BIO_MERGE_OK)
1151eda5cc99SChristoph Hellwig 			return false;
1152eda5cc99SChristoph Hellwig 		*merged_request = attempt_back_merge(q, rq);
1153eda5cc99SChristoph Hellwig 		if (!*merged_request)
1154eda5cc99SChristoph Hellwig 			elv_merged_request(q, rq, ELEVATOR_BACK_MERGE);
1155eda5cc99SChristoph Hellwig 		return true;
1156eda5cc99SChristoph Hellwig 	case ELEVATOR_FRONT_MERGE:
1157eda5cc99SChristoph Hellwig 		if (!blk_mq_sched_allow_merge(q, rq, bio))
1158eda5cc99SChristoph Hellwig 			return false;
1159eda5cc99SChristoph Hellwig 		if (bio_attempt_front_merge(rq, bio, nr_segs) != BIO_MERGE_OK)
1160eda5cc99SChristoph Hellwig 			return false;
1161eda5cc99SChristoph Hellwig 		*merged_request = attempt_front_merge(q, rq);
1162eda5cc99SChristoph Hellwig 		if (!*merged_request)
1163eda5cc99SChristoph Hellwig 			elv_merged_request(q, rq, ELEVATOR_FRONT_MERGE);
1164eda5cc99SChristoph Hellwig 		return true;
1165eda5cc99SChristoph Hellwig 	case ELEVATOR_DISCARD_MERGE:
1166eda5cc99SChristoph Hellwig 		return bio_attempt_discard_merge(q, rq, bio) == BIO_MERGE_OK;
1167eda5cc99SChristoph Hellwig 	default:
1168eda5cc99SChristoph Hellwig 		return false;
1169eda5cc99SChristoph Hellwig 	}
1170eda5cc99SChristoph Hellwig }
1171eda5cc99SChristoph Hellwig EXPORT_SYMBOL_GPL(blk_mq_sched_try_merge);
1172