blk-merge.c 21 KB

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