blk-merge.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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/scatterlist.h>
  10. #include <trace/events/block.h>
  11. #include "blk.h"
  12. static struct bio *blk_bio_discard_split(struct request_queue *q,
  13. struct bio *bio,
  14. struct bio_set *bs,
  15. unsigned *nsegs)
  16. {
  17. unsigned int max_discard_sectors, granularity;
  18. int alignment;
  19. sector_t tmp;
  20. unsigned split_sectors;
  21. *nsegs = 1;
  22. /* Zero-sector (unknown) and one-sector granularities are the same. */
  23. granularity = max(q->limits.discard_granularity >> 9, 1U);
  24. max_discard_sectors = min(q->limits.max_discard_sectors,
  25. bio_allowed_max_sectors(q));
  26. max_discard_sectors -= max_discard_sectors % granularity;
  27. if (unlikely(!max_discard_sectors)) {
  28. /* XXX: warn */
  29. return NULL;
  30. }
  31. if (bio_sectors(bio) <= max_discard_sectors)
  32. return NULL;
  33. split_sectors = max_discard_sectors;
  34. /*
  35. * If the next starting sector would be misaligned, stop the discard at
  36. * the previous aligned sector.
  37. */
  38. alignment = (q->limits.discard_alignment >> 9) % granularity;
  39. tmp = bio->bi_iter.bi_sector + split_sectors - alignment;
  40. tmp = sector_div(tmp, granularity);
  41. if (split_sectors > tmp)
  42. split_sectors -= tmp;
  43. return bio_split(bio, split_sectors, GFP_NOIO, bs);
  44. }
  45. static struct bio *blk_bio_write_zeroes_split(struct request_queue *q,
  46. struct bio *bio, struct bio_set *bs, unsigned *nsegs)
  47. {
  48. *nsegs = 1;
  49. if (!q->limits.max_write_zeroes_sectors)
  50. return NULL;
  51. if (bio_sectors(bio) <= q->limits.max_write_zeroes_sectors)
  52. return NULL;
  53. return bio_split(bio, q->limits.max_write_zeroes_sectors, GFP_NOIO, bs);
  54. }
  55. static struct bio *blk_bio_write_same_split(struct request_queue *q,
  56. struct bio *bio,
  57. struct bio_set *bs,
  58. unsigned *nsegs)
  59. {
  60. *nsegs = 1;
  61. if (!q->limits.max_write_same_sectors)
  62. return NULL;
  63. if (bio_sectors(bio) <= q->limits.max_write_same_sectors)
  64. return NULL;
  65. return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs);
  66. }
  67. static inline unsigned get_max_io_size(struct request_queue *q,
  68. struct bio *bio)
  69. {
  70. unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector);
  71. unsigned mask = queue_logical_block_size(q) - 1;
  72. /* aligned to logical block size */
  73. sectors &= ~(mask >> 9);
  74. return sectors;
  75. }
  76. static struct bio *blk_bio_segment_split(struct request_queue *q,
  77. struct bio *bio,
  78. struct bio_set *bs,
  79. unsigned *segs)
  80. {
  81. struct bio_vec bv, bvprv, *bvprvp = NULL;
  82. struct bvec_iter iter;
  83. unsigned seg_size = 0, nsegs = 0, sectors = 0;
  84. unsigned front_seg_size = bio->bi_seg_front_size;
  85. bool do_split = true;
  86. struct bio *new = NULL;
  87. const unsigned max_sectors = get_max_io_size(q, bio);
  88. bio_for_each_segment(bv, bio, iter) {
  89. /*
  90. * If the queue doesn't support SG gaps and adding this
  91. * offset would create a gap, disallow it.
  92. */
  93. if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
  94. goto split;
  95. if (sectors + (bv.bv_len >> 9) > max_sectors) {
  96. /*
  97. * Consider this a new segment if we're splitting in
  98. * the middle of this vector.
  99. */
  100. if (nsegs < queue_max_segments(q) &&
  101. sectors < max_sectors) {
  102. nsegs++;
  103. sectors = max_sectors;
  104. }
  105. goto split;
  106. }
  107. if (bvprvp && blk_queue_cluster(q)) {
  108. if (seg_size + bv.bv_len > queue_max_segment_size(q))
  109. goto new_segment;
  110. if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv))
  111. goto new_segment;
  112. if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv))
  113. goto new_segment;
  114. seg_size += bv.bv_len;
  115. bvprv = bv;
  116. bvprvp = &bvprv;
  117. sectors += bv.bv_len >> 9;
  118. continue;
  119. }
  120. new_segment:
  121. if (nsegs == queue_max_segments(q))
  122. goto split;
  123. if (nsegs == 1 && seg_size > front_seg_size)
  124. front_seg_size = seg_size;
  125. nsegs++;
  126. bvprv = bv;
  127. bvprvp = &bvprv;
  128. seg_size = bv.bv_len;
  129. sectors += bv.bv_len >> 9;
  130. }
  131. do_split = false;
  132. split:
  133. *segs = nsegs;
  134. if (do_split) {
  135. new = bio_split(bio, sectors, GFP_NOIO, bs);
  136. if (new)
  137. bio = new;
  138. }
  139. if (nsegs == 1 && seg_size > front_seg_size)
  140. front_seg_size = seg_size;
  141. bio->bi_seg_front_size = front_seg_size;
  142. if (seg_size > bio->bi_seg_back_size)
  143. bio->bi_seg_back_size = seg_size;
  144. return do_split ? new : NULL;
  145. }
  146. void blk_queue_split(struct request_queue *q, struct bio **bio)
  147. {
  148. struct bio *split, *res;
  149. unsigned nsegs;
  150. switch (bio_op(*bio)) {
  151. case REQ_OP_DISCARD:
  152. case REQ_OP_SECURE_ERASE:
  153. split = blk_bio_discard_split(q, *bio, &q->bio_split, &nsegs);
  154. break;
  155. case REQ_OP_WRITE_ZEROES:
  156. split = blk_bio_write_zeroes_split(q, *bio, &q->bio_split, &nsegs);
  157. break;
  158. case REQ_OP_WRITE_SAME:
  159. split = blk_bio_write_same_split(q, *bio, &q->bio_split, &nsegs);
  160. break;
  161. default:
  162. split = blk_bio_segment_split(q, *bio, &q->bio_split, &nsegs);
  163. break;
  164. }
  165. /* physical segments can be figured out during splitting */
  166. res = split ? split : *bio;
  167. res->bi_phys_segments = nsegs;
  168. bio_set_flag(res, BIO_SEG_VALID);
  169. if (split) {
  170. /* there isn't chance to merge the splitted bio */
  171. split->bi_opf |= REQ_NOMERGE;
  172. /*
  173. * Since we're recursing into make_request here, ensure
  174. * that we mark this bio as already having entered the queue.
  175. * If not, and the queue is going away, we can get stuck
  176. * forever on waiting for the queue reference to drop. But
  177. * that will never happen, as we're already holding a
  178. * reference to it.
  179. */
  180. bio_set_flag(*bio, BIO_QUEUE_ENTERED);
  181. bio_chain(split, *bio);
  182. trace_block_split(q, split, (*bio)->bi_iter.bi_sector);
  183. generic_make_request(*bio);
  184. *bio = split;
  185. }
  186. }
  187. EXPORT_SYMBOL(blk_queue_split);
  188. static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
  189. struct bio *bio,
  190. bool no_sg_merge)
  191. {
  192. struct bio_vec bv, bvprv = { NULL };
  193. int cluster, prev = 0;
  194. unsigned int seg_size, nr_phys_segs;
  195. struct bio *fbio, *bbio;
  196. struct bvec_iter iter;
  197. if (!bio)
  198. return 0;
  199. switch (bio_op(bio)) {
  200. case REQ_OP_DISCARD:
  201. case REQ_OP_SECURE_ERASE:
  202. case REQ_OP_WRITE_ZEROES:
  203. return 0;
  204. case REQ_OP_WRITE_SAME:
  205. return 1;
  206. }
  207. fbio = bio;
  208. cluster = blk_queue_cluster(q);
  209. seg_size = 0;
  210. nr_phys_segs = 0;
  211. for_each_bio(bio) {
  212. bio_for_each_segment(bv, bio, iter) {
  213. /*
  214. * If SG merging is disabled, each bio vector is
  215. * a segment
  216. */
  217. if (no_sg_merge)
  218. goto new_segment;
  219. if (prev && cluster) {
  220. if (seg_size + bv.bv_len
  221. > queue_max_segment_size(q))
  222. goto new_segment;
  223. if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
  224. goto new_segment;
  225. if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
  226. goto new_segment;
  227. seg_size += bv.bv_len;
  228. bvprv = bv;
  229. continue;
  230. }
  231. new_segment:
  232. if (nr_phys_segs == 1 && seg_size >
  233. fbio->bi_seg_front_size)
  234. fbio->bi_seg_front_size = seg_size;
  235. nr_phys_segs++;
  236. bvprv = bv;
  237. prev = 1;
  238. seg_size = bv.bv_len;
  239. }
  240. bbio = bio;
  241. }
  242. if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
  243. fbio->bi_seg_front_size = seg_size;
  244. if (seg_size > bbio->bi_seg_back_size)
  245. bbio->bi_seg_back_size = seg_size;
  246. return nr_phys_segs;
  247. }
  248. void blk_recalc_rq_segments(struct request *rq)
  249. {
  250. bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
  251. &rq->q->queue_flags);
  252. rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
  253. no_sg_merge);
  254. }
  255. void blk_recount_segments(struct request_queue *q, struct bio *bio)
  256. {
  257. unsigned short seg_cnt = bio_segments(bio);
  258. if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
  259. (seg_cnt < queue_max_segments(q)))
  260. bio->bi_phys_segments = seg_cnt;
  261. else {
  262. struct bio *nxt = bio->bi_next;
  263. bio->bi_next = NULL;
  264. bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
  265. bio->bi_next = nxt;
  266. }
  267. bio_set_flag(bio, BIO_SEG_VALID);
  268. }
  269. EXPORT_SYMBOL(blk_recount_segments);
  270. static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
  271. struct bio *nxt)
  272. {
  273. struct bio_vec end_bv = { NULL }, nxt_bv;
  274. if (!blk_queue_cluster(q))
  275. return 0;
  276. if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
  277. queue_max_segment_size(q))
  278. return 0;
  279. if (!bio_has_data(bio))
  280. return 1;
  281. bio_get_last_bvec(bio, &end_bv);
  282. bio_get_first_bvec(nxt, &nxt_bv);
  283. if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
  284. return 0;
  285. /*
  286. * bio and nxt are contiguous in memory; check if the queue allows
  287. * these two to be merged into one
  288. */
  289. if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
  290. return 1;
  291. return 0;
  292. }
  293. static inline void
  294. __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
  295. struct scatterlist *sglist, struct bio_vec *bvprv,
  296. struct scatterlist **sg, int *nsegs, int *cluster)
  297. {
  298. int nbytes = bvec->bv_len;
  299. if (*sg && *cluster) {
  300. if ((*sg)->length + nbytes > queue_max_segment_size(q))
  301. goto new_segment;
  302. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
  303. goto new_segment;
  304. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
  305. goto new_segment;
  306. (*sg)->length += nbytes;
  307. } else {
  308. new_segment:
  309. if (!*sg)
  310. *sg = sglist;
  311. else {
  312. /*
  313. * If the driver previously mapped a shorter
  314. * list, we could see a termination bit
  315. * prematurely unless it fully inits the sg
  316. * table on each mapping. We KNOW that there
  317. * must be more entries here or the driver
  318. * would be buggy, so force clear the
  319. * termination bit to avoid doing a full
  320. * sg_init_table() in drivers for each command.
  321. */
  322. sg_unmark_end(*sg);
  323. *sg = sg_next(*sg);
  324. }
  325. sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
  326. (*nsegs)++;
  327. }
  328. *bvprv = *bvec;
  329. }
  330. static inline int __blk_bvec_map_sg(struct request_queue *q, struct bio_vec bv,
  331. struct scatterlist *sglist, struct scatterlist **sg)
  332. {
  333. *sg = sglist;
  334. sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
  335. return 1;
  336. }
  337. static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
  338. struct scatterlist *sglist,
  339. struct scatterlist **sg)
  340. {
  341. struct bio_vec bvec, bvprv = { NULL };
  342. struct bvec_iter iter;
  343. int cluster = blk_queue_cluster(q), nsegs = 0;
  344. for_each_bio(bio)
  345. bio_for_each_segment(bvec, bio, iter)
  346. __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
  347. &nsegs, &cluster);
  348. return nsegs;
  349. }
  350. /*
  351. * map a request to scatterlist, return number of sg entries setup. Caller
  352. * must make sure sg can hold rq->nr_phys_segments entries
  353. */
  354. int blk_rq_map_sg(struct request_queue *q, struct request *rq,
  355. struct scatterlist *sglist)
  356. {
  357. struct scatterlist *sg = NULL;
  358. int nsegs = 0;
  359. if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
  360. nsegs = __blk_bvec_map_sg(q, rq->special_vec, sglist, &sg);
  361. else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME)
  362. nsegs = __blk_bvec_map_sg(q, bio_iovec(rq->bio), sglist, &sg);
  363. else if (rq->bio)
  364. nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
  365. if (unlikely(rq->rq_flags & RQF_COPY_USER) &&
  366. (blk_rq_bytes(rq) & q->dma_pad_mask)) {
  367. unsigned int pad_len =
  368. (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
  369. sg->length += pad_len;
  370. rq->extra_len += pad_len;
  371. }
  372. if (q->dma_drain_size && q->dma_drain_needed(rq)) {
  373. if (op_is_write(req_op(rq)))
  374. memset(q->dma_drain_buffer, 0, q->dma_drain_size);
  375. sg_unmark_end(sg);
  376. sg = sg_next(sg);
  377. sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
  378. q->dma_drain_size,
  379. ((unsigned long)q->dma_drain_buffer) &
  380. (PAGE_SIZE - 1));
  381. nsegs++;
  382. rq->extra_len += q->dma_drain_size;
  383. }
  384. if (sg)
  385. sg_mark_end(sg);
  386. /*
  387. * Something must have been wrong if the figured number of
  388. * segment is bigger than number of req's physical segments
  389. */
  390. WARN_ON(nsegs > blk_rq_nr_phys_segments(rq));
  391. return nsegs;
  392. }
  393. EXPORT_SYMBOL(blk_rq_map_sg);
  394. static inline int ll_new_hw_segment(struct request_queue *q,
  395. struct request *req,
  396. struct bio *bio)
  397. {
  398. int nr_phys_segs = bio_phys_segments(q, bio);
  399. if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
  400. goto no_merge;
  401. if (blk_integrity_merge_bio(q, req, bio) == false)
  402. goto no_merge;
  403. /*
  404. * This will form the start of a new hw segment. Bump both
  405. * counters.
  406. */
  407. req->nr_phys_segments += nr_phys_segs;
  408. return 1;
  409. no_merge:
  410. req_set_nomerge(q, req);
  411. return 0;
  412. }
  413. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  414. struct bio *bio)
  415. {
  416. if (req_gap_back_merge(req, bio))
  417. return 0;
  418. if (blk_integrity_rq(req) &&
  419. integrity_req_gap_back_merge(req, bio))
  420. return 0;
  421. if (blk_rq_sectors(req) + bio_sectors(bio) >
  422. blk_rq_get_max_sectors(req, blk_rq_pos(req))) {
  423. req_set_nomerge(q, req);
  424. return 0;
  425. }
  426. if (!bio_flagged(req->biotail, BIO_SEG_VALID))
  427. blk_recount_segments(q, req->biotail);
  428. if (!bio_flagged(bio, BIO_SEG_VALID))
  429. blk_recount_segments(q, bio);
  430. return ll_new_hw_segment(q, req, bio);
  431. }
  432. int ll_front_merge_fn(struct request_queue *q, struct request *req,
  433. struct bio *bio)
  434. {
  435. if (req_gap_front_merge(req, bio))
  436. return 0;
  437. if (blk_integrity_rq(req) &&
  438. integrity_req_gap_front_merge(req, bio))
  439. return 0;
  440. if (blk_rq_sectors(req) + bio_sectors(bio) >
  441. blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) {
  442. req_set_nomerge(q, req);
  443. return 0;
  444. }
  445. if (!bio_flagged(bio, BIO_SEG_VALID))
  446. blk_recount_segments(q, bio);
  447. if (!bio_flagged(req->bio, BIO_SEG_VALID))
  448. blk_recount_segments(q, req->bio);
  449. return ll_new_hw_segment(q, req, bio);
  450. }
  451. /*
  452. * blk-mq uses req->special to carry normal driver per-request payload, it
  453. * does not indicate a prepared command that we cannot merge with.
  454. */
  455. static bool req_no_special_merge(struct request *req)
  456. {
  457. struct request_queue *q = req->q;
  458. return !q->mq_ops && req->special;
  459. }
  460. static bool req_attempt_discard_merge(struct request_queue *q, struct request *req,
  461. struct request *next)
  462. {
  463. unsigned short segments = blk_rq_nr_discard_segments(req);
  464. if (segments >= queue_max_discard_segments(q))
  465. goto no_merge;
  466. if (blk_rq_sectors(req) + bio_sectors(next->bio) >
  467. blk_rq_get_max_sectors(req, blk_rq_pos(req)))
  468. goto no_merge;
  469. req->nr_phys_segments = segments + blk_rq_nr_discard_segments(next);
  470. return true;
  471. no_merge:
  472. req_set_nomerge(q, req);
  473. return false;
  474. }
  475. static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
  476. struct request *next)
  477. {
  478. int total_phys_segments;
  479. unsigned int seg_size =
  480. req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
  481. /*
  482. * First check if the either of the requests are re-queued
  483. * requests. Can't merge them if they are.
  484. */
  485. if (req_no_special_merge(req) || req_no_special_merge(next))
  486. return 0;
  487. if (req_gap_back_merge(req, next->bio))
  488. return 0;
  489. /*
  490. * Will it become too large?
  491. */
  492. if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
  493. blk_rq_get_max_sectors(req, blk_rq_pos(req)))
  494. return 0;
  495. total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
  496. if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
  497. if (req->nr_phys_segments == 1)
  498. req->bio->bi_seg_front_size = seg_size;
  499. if (next->nr_phys_segments == 1)
  500. next->biotail->bi_seg_back_size = seg_size;
  501. total_phys_segments--;
  502. }
  503. if (total_phys_segments > queue_max_segments(q))
  504. return 0;
  505. if (blk_integrity_merge_rq(q, req, next) == false)
  506. return 0;
  507. /* Merge is OK... */
  508. req->nr_phys_segments = total_phys_segments;
  509. return 1;
  510. }
  511. /**
  512. * blk_rq_set_mixed_merge - mark a request as mixed merge
  513. * @rq: request to mark as mixed merge
  514. *
  515. * Description:
  516. * @rq is about to be mixed merged. Make sure the attributes
  517. * which can be mixed are set in each bio and mark @rq as mixed
  518. * merged.
  519. */
  520. void blk_rq_set_mixed_merge(struct request *rq)
  521. {
  522. unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
  523. struct bio *bio;
  524. if (rq->rq_flags & RQF_MIXED_MERGE)
  525. return;
  526. /*
  527. * @rq will no longer represent mixable attributes for all the
  528. * contained bios. It will just track those of the first one.
  529. * Distributes the attributs to each bio.
  530. */
  531. for (bio = rq->bio; bio; bio = bio->bi_next) {
  532. WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) &&
  533. (bio->bi_opf & REQ_FAILFAST_MASK) != ff);
  534. bio->bi_opf |= ff;
  535. }
  536. rq->rq_flags |= RQF_MIXED_MERGE;
  537. }
  538. static void blk_account_io_merge(struct request *req)
  539. {
  540. if (blk_do_io_stat(req)) {
  541. struct hd_struct *part;
  542. int cpu;
  543. cpu = part_stat_lock();
  544. part = req->part;
  545. part_round_stats(req->q, cpu, part);
  546. part_dec_in_flight(req->q, part, rq_data_dir(req));
  547. hd_struct_put(part);
  548. part_stat_unlock();
  549. }
  550. }
  551. /*
  552. * Two cases of handling DISCARD merge:
  553. * If max_discard_segments > 1, the driver takes every bio
  554. * as a range and send them to controller together. The ranges
  555. * needn't to be contiguous.
  556. * Otherwise, the bios/requests will be handled as same as
  557. * others which should be contiguous.
  558. */
  559. static inline bool blk_discard_mergable(struct request *req)
  560. {
  561. if (req_op(req) == REQ_OP_DISCARD &&
  562. queue_max_discard_segments(req->q) > 1)
  563. return true;
  564. return false;
  565. }
  566. enum elv_merge blk_try_req_merge(struct request *req, struct request *next)
  567. {
  568. if (blk_discard_mergable(req))
  569. return ELEVATOR_DISCARD_MERGE;
  570. else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
  571. return ELEVATOR_BACK_MERGE;
  572. return ELEVATOR_NO_MERGE;
  573. }
  574. /*
  575. * For non-mq, this has to be called with the request spinlock acquired.
  576. * For mq with scheduling, the appropriate queue wide lock should be held.
  577. */
  578. static struct request *attempt_merge(struct request_queue *q,
  579. struct request *req, struct request *next)
  580. {
  581. if (!q->mq_ops)
  582. lockdep_assert_held(q->queue_lock);
  583. if (!rq_mergeable(req) || !rq_mergeable(next))
  584. return NULL;
  585. if (req_op(req) != req_op(next))
  586. return NULL;
  587. if (rq_data_dir(req) != rq_data_dir(next)
  588. || req->rq_disk != next->rq_disk
  589. || req_no_special_merge(next))
  590. return NULL;
  591. if (req_op(req) == REQ_OP_WRITE_SAME &&
  592. !blk_write_same_mergeable(req->bio, next->bio))
  593. return NULL;
  594. /*
  595. * Don't allow merge of different write hints, or for a hint with
  596. * non-hint IO.
  597. */
  598. if (req->write_hint != next->write_hint)
  599. return NULL;
  600. /*
  601. * If we are allowed to merge, then append bio list
  602. * from next to rq and release next. merge_requests_fn
  603. * will have updated segment counts, update sector
  604. * counts here. Handle DISCARDs separately, as they
  605. * have separate settings.
  606. */
  607. switch (blk_try_req_merge(req, next)) {
  608. case ELEVATOR_DISCARD_MERGE:
  609. if (!req_attempt_discard_merge(q, req, next))
  610. return NULL;
  611. break;
  612. case ELEVATOR_BACK_MERGE:
  613. if (!ll_merge_requests_fn(q, req, next))
  614. return NULL;
  615. break;
  616. default:
  617. return NULL;
  618. }
  619. /*
  620. * If failfast settings disagree or any of the two is already
  621. * a mixed merge, mark both as mixed before proceeding. This
  622. * makes sure that all involved bios have mixable attributes
  623. * set properly.
  624. */
  625. if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) ||
  626. (req->cmd_flags & REQ_FAILFAST_MASK) !=
  627. (next->cmd_flags & REQ_FAILFAST_MASK)) {
  628. blk_rq_set_mixed_merge(req);
  629. blk_rq_set_mixed_merge(next);
  630. }
  631. /*
  632. * At this point we have either done a back merge or front merge. We
  633. * need the smaller start_time_ns of the merged requests to be the
  634. * current request for accounting purposes.
  635. */
  636. if (next->start_time_ns < req->start_time_ns)
  637. req->start_time_ns = next->start_time_ns;
  638. req->biotail->bi_next = next->bio;
  639. req->biotail = next->biotail;
  640. req->__data_len += blk_rq_bytes(next);
  641. if (!blk_discard_mergable(req))
  642. elv_merge_requests(q, req, next);
  643. /*
  644. * 'next' is going away, so update stats accordingly
  645. */
  646. blk_account_io_merge(next);
  647. req->ioprio = ioprio_best(req->ioprio, next->ioprio);
  648. if (blk_rq_cpu_valid(next))
  649. req->cpu = next->cpu;
  650. /*
  651. * ownership of bio passed from next to req, return 'next' for
  652. * the caller to free
  653. */
  654. next->bio = NULL;
  655. return next;
  656. }
  657. struct request *attempt_back_merge(struct request_queue *q, struct request *rq)
  658. {
  659. struct request *next = elv_latter_request(q, rq);
  660. if (next)
  661. return attempt_merge(q, rq, next);
  662. return NULL;
  663. }
  664. struct request *attempt_front_merge(struct request_queue *q, struct request *rq)
  665. {
  666. struct request *prev = elv_former_request(q, rq);
  667. if (prev)
  668. return attempt_merge(q, prev, rq);
  669. return NULL;
  670. }
  671. int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
  672. struct request *next)
  673. {
  674. struct elevator_queue *e = q->elevator;
  675. struct request *free;
  676. if (!e->uses_mq && e->type->ops.sq.elevator_allow_rq_merge_fn)
  677. if (!e->type->ops.sq.elevator_allow_rq_merge_fn(q, rq, next))
  678. return 0;
  679. free = attempt_merge(q, rq, next);
  680. if (free) {
  681. __blk_put_request(q, free);
  682. return 1;
  683. }
  684. return 0;
  685. }
  686. bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
  687. {
  688. if (!rq_mergeable(rq) || !bio_mergeable(bio))
  689. return false;
  690. if (req_op(rq) != bio_op(bio))
  691. return false;
  692. /* different data direction or already started, don't merge */
  693. if (bio_data_dir(bio) != rq_data_dir(rq))
  694. return false;
  695. /* must be same device and not a special request */
  696. if (rq->rq_disk != bio->bi_disk || req_no_special_merge(rq))
  697. return false;
  698. /* only merge integrity protected bio into ditto rq */
  699. if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
  700. return false;
  701. /* must be using the same buffer */
  702. if (req_op(rq) == REQ_OP_WRITE_SAME &&
  703. !blk_write_same_mergeable(rq->bio, bio))
  704. return false;
  705. /*
  706. * Don't allow merge of different write hints, or for a hint with
  707. * non-hint IO.
  708. */
  709. if (rq->write_hint != bio->bi_write_hint)
  710. return false;
  711. return true;
  712. }
  713. enum elv_merge blk_try_merge(struct request *rq, struct bio *bio)
  714. {
  715. if (blk_discard_mergable(rq))
  716. return ELEVATOR_DISCARD_MERGE;
  717. else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
  718. return ELEVATOR_BACK_MERGE;
  719. else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
  720. return ELEVATOR_FRONT_MERGE;
  721. return ELEVATOR_NO_MERGE;
  722. }