blk-merge.c 22 KB

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