blk-merge.c 20 KB

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