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