blk-merge.c 31 KB

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