blk-merge.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 "blk.h"
  10. static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
  11. struct bio *bio,
  12. bool no_sg_merge)
  13. {
  14. struct bio_vec bv, bvprv = { NULL };
  15. int cluster, high, highprv = 1;
  16. unsigned int seg_size, nr_phys_segs;
  17. struct bio *fbio, *bbio;
  18. struct bvec_iter iter;
  19. if (!bio)
  20. return 0;
  21. /*
  22. * This should probably be returning 0, but blk_add_request_payload()
  23. * (Christoph!!!!)
  24. */
  25. if (bio->bi_rw & REQ_DISCARD)
  26. return 1;
  27. if (bio->bi_rw & REQ_WRITE_SAME)
  28. return 1;
  29. fbio = bio;
  30. cluster = blk_queue_cluster(q);
  31. seg_size = 0;
  32. nr_phys_segs = 0;
  33. high = 0;
  34. for_each_bio(bio) {
  35. bio_for_each_segment(bv, bio, iter) {
  36. /*
  37. * If SG merging is disabled, each bio vector is
  38. * a segment
  39. */
  40. if (no_sg_merge)
  41. goto new_segment;
  42. /*
  43. * the trick here is making sure that a high page is
  44. * never considered part of another segment, since
  45. * that might change with the bounce page.
  46. */
  47. high = page_to_pfn(bv.bv_page) > queue_bounce_pfn(q);
  48. if (!high && !highprv && cluster) {
  49. if (seg_size + bv.bv_len
  50. > queue_max_segment_size(q))
  51. goto new_segment;
  52. if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
  53. goto new_segment;
  54. if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
  55. goto new_segment;
  56. seg_size += bv.bv_len;
  57. bvprv = bv;
  58. continue;
  59. }
  60. new_segment:
  61. if (nr_phys_segs == 1 && seg_size >
  62. fbio->bi_seg_front_size)
  63. fbio->bi_seg_front_size = seg_size;
  64. nr_phys_segs++;
  65. bvprv = bv;
  66. seg_size = bv.bv_len;
  67. highprv = high;
  68. }
  69. bbio = bio;
  70. }
  71. if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
  72. fbio->bi_seg_front_size = seg_size;
  73. if (seg_size > bbio->bi_seg_back_size)
  74. bbio->bi_seg_back_size = seg_size;
  75. return nr_phys_segs;
  76. }
  77. void blk_recalc_rq_segments(struct request *rq)
  78. {
  79. bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
  80. &rq->q->queue_flags);
  81. rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
  82. no_sg_merge);
  83. }
  84. void blk_recount_segments(struct request_queue *q, struct bio *bio)
  85. {
  86. bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
  87. &q->queue_flags);
  88. if (no_sg_merge && !bio_flagged(bio, BIO_CLONED) &&
  89. bio->bi_vcnt < queue_max_segments(q))
  90. bio->bi_phys_segments = bio->bi_vcnt;
  91. else {
  92. struct bio *nxt = bio->bi_next;
  93. bio->bi_next = NULL;
  94. bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio,
  95. no_sg_merge);
  96. bio->bi_next = nxt;
  97. }
  98. bio->bi_flags |= (1 << BIO_SEG_VALID);
  99. }
  100. EXPORT_SYMBOL(blk_recount_segments);
  101. static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
  102. struct bio *nxt)
  103. {
  104. struct bio_vec end_bv = { NULL }, nxt_bv;
  105. struct bvec_iter iter;
  106. if (!blk_queue_cluster(q))
  107. return 0;
  108. if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
  109. queue_max_segment_size(q))
  110. return 0;
  111. if (!bio_has_data(bio))
  112. return 1;
  113. bio_for_each_segment(end_bv, bio, iter)
  114. if (end_bv.bv_len == iter.bi_size)
  115. break;
  116. nxt_bv = bio_iovec(nxt);
  117. if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
  118. return 0;
  119. /*
  120. * bio and nxt are contiguous in memory; check if the queue allows
  121. * these two to be merged into one
  122. */
  123. if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
  124. return 1;
  125. return 0;
  126. }
  127. static inline void
  128. __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
  129. struct scatterlist *sglist, struct bio_vec *bvprv,
  130. struct scatterlist **sg, int *nsegs, int *cluster)
  131. {
  132. int nbytes = bvec->bv_len;
  133. if (*sg && *cluster) {
  134. if ((*sg)->length + nbytes > queue_max_segment_size(q))
  135. goto new_segment;
  136. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
  137. goto new_segment;
  138. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
  139. goto new_segment;
  140. (*sg)->length += nbytes;
  141. } else {
  142. new_segment:
  143. if (!*sg)
  144. *sg = sglist;
  145. else {
  146. /*
  147. * If the driver previously mapped a shorter
  148. * list, we could see a termination bit
  149. * prematurely unless it fully inits the sg
  150. * table on each mapping. We KNOW that there
  151. * must be more entries here or the driver
  152. * would be buggy, so force clear the
  153. * termination bit to avoid doing a full
  154. * sg_init_table() in drivers for each command.
  155. */
  156. sg_unmark_end(*sg);
  157. *sg = sg_next(*sg);
  158. }
  159. sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
  160. (*nsegs)++;
  161. }
  162. *bvprv = *bvec;
  163. }
  164. static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
  165. struct scatterlist *sglist,
  166. struct scatterlist **sg)
  167. {
  168. struct bio_vec bvec, bvprv = { NULL };
  169. struct bvec_iter iter;
  170. int nsegs, cluster;
  171. nsegs = 0;
  172. cluster = blk_queue_cluster(q);
  173. if (bio->bi_rw & REQ_DISCARD) {
  174. /*
  175. * This is a hack - drivers should be neither modifying the
  176. * biovec, nor relying on bi_vcnt - but because of
  177. * blk_add_request_payload(), a discard bio may or may not have
  178. * a payload we need to set up here (thank you Christoph) and
  179. * bi_vcnt is really the only way of telling if we need to.
  180. */
  181. if (bio->bi_vcnt)
  182. goto single_segment;
  183. return 0;
  184. }
  185. if (bio->bi_rw & REQ_WRITE_SAME) {
  186. single_segment:
  187. *sg = sglist;
  188. bvec = bio_iovec(bio);
  189. sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
  190. return 1;
  191. }
  192. for_each_bio(bio)
  193. bio_for_each_segment(bvec, bio, iter)
  194. __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
  195. &nsegs, &cluster);
  196. return nsegs;
  197. }
  198. /*
  199. * map a request to scatterlist, return number of sg entries setup. Caller
  200. * must make sure sg can hold rq->nr_phys_segments entries
  201. */
  202. int blk_rq_map_sg(struct request_queue *q, struct request *rq,
  203. struct scatterlist *sglist)
  204. {
  205. struct scatterlist *sg = NULL;
  206. int nsegs = 0;
  207. if (rq->bio)
  208. nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
  209. if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
  210. (blk_rq_bytes(rq) & q->dma_pad_mask)) {
  211. unsigned int pad_len =
  212. (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
  213. sg->length += pad_len;
  214. rq->extra_len += pad_len;
  215. }
  216. if (q->dma_drain_size && q->dma_drain_needed(rq)) {
  217. if (rq->cmd_flags & REQ_WRITE)
  218. memset(q->dma_drain_buffer, 0, q->dma_drain_size);
  219. sg->page_link &= ~0x02;
  220. sg = sg_next(sg);
  221. sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
  222. q->dma_drain_size,
  223. ((unsigned long)q->dma_drain_buffer) &
  224. (PAGE_SIZE - 1));
  225. nsegs++;
  226. rq->extra_len += q->dma_drain_size;
  227. }
  228. if (sg)
  229. sg_mark_end(sg);
  230. return nsegs;
  231. }
  232. EXPORT_SYMBOL(blk_rq_map_sg);
  233. /**
  234. * blk_bio_map_sg - map a bio to a scatterlist
  235. * @q: request_queue in question
  236. * @bio: bio being mapped
  237. * @sglist: scatterlist being mapped
  238. *
  239. * Note:
  240. * Caller must make sure sg can hold bio->bi_phys_segments entries
  241. *
  242. * Will return the number of sg entries setup
  243. */
  244. int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
  245. struct scatterlist *sglist)
  246. {
  247. struct scatterlist *sg = NULL;
  248. int nsegs;
  249. struct bio *next = bio->bi_next;
  250. bio->bi_next = NULL;
  251. nsegs = __blk_bios_map_sg(q, bio, sglist, &sg);
  252. bio->bi_next = next;
  253. if (sg)
  254. sg_mark_end(sg);
  255. BUG_ON(bio->bi_phys_segments && nsegs > bio->bi_phys_segments);
  256. return nsegs;
  257. }
  258. EXPORT_SYMBOL(blk_bio_map_sg);
  259. static inline int ll_new_hw_segment(struct request_queue *q,
  260. struct request *req,
  261. struct bio *bio)
  262. {
  263. int nr_phys_segs = bio_phys_segments(q, bio);
  264. if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
  265. goto no_merge;
  266. if (blk_integrity_merge_bio(q, req, bio) == false)
  267. goto no_merge;
  268. /*
  269. * This will form the start of a new hw segment. Bump both
  270. * counters.
  271. */
  272. req->nr_phys_segments += nr_phys_segs;
  273. return 1;
  274. no_merge:
  275. req->cmd_flags |= REQ_NOMERGE;
  276. if (req == q->last_merge)
  277. q->last_merge = NULL;
  278. return 0;
  279. }
  280. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  281. struct bio *bio)
  282. {
  283. if (blk_rq_sectors(req) + bio_sectors(bio) >
  284. blk_rq_get_max_sectors(req)) {
  285. req->cmd_flags |= REQ_NOMERGE;
  286. if (req == q->last_merge)
  287. q->last_merge = NULL;
  288. return 0;
  289. }
  290. if (!bio_flagged(req->biotail, BIO_SEG_VALID))
  291. blk_recount_segments(q, req->biotail);
  292. if (!bio_flagged(bio, BIO_SEG_VALID))
  293. blk_recount_segments(q, bio);
  294. return ll_new_hw_segment(q, req, bio);
  295. }
  296. int ll_front_merge_fn(struct request_queue *q, struct request *req,
  297. struct bio *bio)
  298. {
  299. if (blk_rq_sectors(req) + bio_sectors(bio) >
  300. blk_rq_get_max_sectors(req)) {
  301. req->cmd_flags |= REQ_NOMERGE;
  302. if (req == q->last_merge)
  303. q->last_merge = NULL;
  304. return 0;
  305. }
  306. if (!bio_flagged(bio, BIO_SEG_VALID))
  307. blk_recount_segments(q, bio);
  308. if (!bio_flagged(req->bio, BIO_SEG_VALID))
  309. blk_recount_segments(q, req->bio);
  310. return ll_new_hw_segment(q, req, bio);
  311. }
  312. /*
  313. * blk-mq uses req->special to carry normal driver per-request payload, it
  314. * does not indicate a prepared command that we cannot merge with.
  315. */
  316. static bool req_no_special_merge(struct request *req)
  317. {
  318. struct request_queue *q = req->q;
  319. return !q->mq_ops && req->special;
  320. }
  321. static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
  322. struct request *next)
  323. {
  324. int total_phys_segments;
  325. unsigned int seg_size =
  326. req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
  327. /*
  328. * First check if the either of the requests are re-queued
  329. * requests. Can't merge them if they are.
  330. */
  331. if (req_no_special_merge(req) || req_no_special_merge(next))
  332. return 0;
  333. /*
  334. * Will it become too large?
  335. */
  336. if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
  337. blk_rq_get_max_sectors(req))
  338. return 0;
  339. total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
  340. if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
  341. if (req->nr_phys_segments == 1)
  342. req->bio->bi_seg_front_size = seg_size;
  343. if (next->nr_phys_segments == 1)
  344. next->biotail->bi_seg_back_size = seg_size;
  345. total_phys_segments--;
  346. }
  347. if (total_phys_segments > queue_max_segments(q))
  348. return 0;
  349. if (blk_integrity_merge_rq(q, req, next) == false)
  350. return 0;
  351. /* Merge is OK... */
  352. req->nr_phys_segments = total_phys_segments;
  353. return 1;
  354. }
  355. /**
  356. * blk_rq_set_mixed_merge - mark a request as mixed merge
  357. * @rq: request to mark as mixed merge
  358. *
  359. * Description:
  360. * @rq is about to be mixed merged. Make sure the attributes
  361. * which can be mixed are set in each bio and mark @rq as mixed
  362. * merged.
  363. */
  364. void blk_rq_set_mixed_merge(struct request *rq)
  365. {
  366. unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
  367. struct bio *bio;
  368. if (rq->cmd_flags & REQ_MIXED_MERGE)
  369. return;
  370. /*
  371. * @rq will no longer represent mixable attributes for all the
  372. * contained bios. It will just track those of the first one.
  373. * Distributes the attributs to each bio.
  374. */
  375. for (bio = rq->bio; bio; bio = bio->bi_next) {
  376. WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
  377. (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
  378. bio->bi_rw |= ff;
  379. }
  380. rq->cmd_flags |= REQ_MIXED_MERGE;
  381. }
  382. static void blk_account_io_merge(struct request *req)
  383. {
  384. if (blk_do_io_stat(req)) {
  385. struct hd_struct *part;
  386. int cpu;
  387. cpu = part_stat_lock();
  388. part = req->part;
  389. part_round_stats(cpu, part);
  390. part_dec_in_flight(part, rq_data_dir(req));
  391. hd_struct_put(part);
  392. part_stat_unlock();
  393. }
  394. }
  395. /*
  396. * Has to be called with the request spinlock acquired
  397. */
  398. static int attempt_merge(struct request_queue *q, struct request *req,
  399. struct request *next)
  400. {
  401. if (!rq_mergeable(req) || !rq_mergeable(next))
  402. return 0;
  403. if (!blk_check_merge_flags(req->cmd_flags, next->cmd_flags))
  404. return 0;
  405. /*
  406. * not contiguous
  407. */
  408. if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
  409. return 0;
  410. if (rq_data_dir(req) != rq_data_dir(next)
  411. || req->rq_disk != next->rq_disk
  412. || req_no_special_merge(next))
  413. return 0;
  414. if (req->cmd_flags & REQ_WRITE_SAME &&
  415. !blk_write_same_mergeable(req->bio, next->bio))
  416. return 0;
  417. /*
  418. * If we are allowed to merge, then append bio list
  419. * from next to rq and release next. merge_requests_fn
  420. * will have updated segment counts, update sector
  421. * counts here.
  422. */
  423. if (!ll_merge_requests_fn(q, req, next))
  424. return 0;
  425. /*
  426. * If failfast settings disagree or any of the two is already
  427. * a mixed merge, mark both as mixed before proceeding. This
  428. * makes sure that all involved bios have mixable attributes
  429. * set properly.
  430. */
  431. if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
  432. (req->cmd_flags & REQ_FAILFAST_MASK) !=
  433. (next->cmd_flags & REQ_FAILFAST_MASK)) {
  434. blk_rq_set_mixed_merge(req);
  435. blk_rq_set_mixed_merge(next);
  436. }
  437. /*
  438. * At this point we have either done a back merge
  439. * or front merge. We need the smaller start_time of
  440. * the merged requests to be the current request
  441. * for accounting purposes.
  442. */
  443. if (time_after(req->start_time, next->start_time))
  444. req->start_time = next->start_time;
  445. req->biotail->bi_next = next->bio;
  446. req->biotail = next->biotail;
  447. req->__data_len += blk_rq_bytes(next);
  448. elv_merge_requests(q, req, next);
  449. /*
  450. * 'next' is going away, so update stats accordingly
  451. */
  452. blk_account_io_merge(next);
  453. req->ioprio = ioprio_best(req->ioprio, next->ioprio);
  454. if (blk_rq_cpu_valid(next))
  455. req->cpu = next->cpu;
  456. /* owner-ship of bio passed from next to req */
  457. next->bio = NULL;
  458. __blk_put_request(q, next);
  459. return 1;
  460. }
  461. int attempt_back_merge(struct request_queue *q, struct request *rq)
  462. {
  463. struct request *next = elv_latter_request(q, rq);
  464. if (next)
  465. return attempt_merge(q, rq, next);
  466. return 0;
  467. }
  468. int attempt_front_merge(struct request_queue *q, struct request *rq)
  469. {
  470. struct request *prev = elv_former_request(q, rq);
  471. if (prev)
  472. return attempt_merge(q, prev, rq);
  473. return 0;
  474. }
  475. int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
  476. struct request *next)
  477. {
  478. return attempt_merge(q, rq, next);
  479. }
  480. bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
  481. {
  482. struct request_queue *q = rq->q;
  483. if (!rq_mergeable(rq) || !bio_mergeable(bio))
  484. return false;
  485. if (!blk_check_merge_flags(rq->cmd_flags, bio->bi_rw))
  486. return false;
  487. /* different data direction or already started, don't merge */
  488. if (bio_data_dir(bio) != rq_data_dir(rq))
  489. return false;
  490. /* must be same device and not a special request */
  491. if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
  492. return false;
  493. /* only merge integrity protected bio into ditto rq */
  494. if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
  495. return false;
  496. /* must be using the same buffer */
  497. if (rq->cmd_flags & REQ_WRITE_SAME &&
  498. !blk_write_same_mergeable(rq->bio, bio))
  499. return false;
  500. if (q->queue_flags & (1 << QUEUE_FLAG_SG_GAPS)) {
  501. struct bio_vec *bprev;
  502. bprev = &rq->biotail->bi_io_vec[bio->bi_vcnt - 1];
  503. if (bvec_gap_to_prev(bprev, bio->bi_io_vec[0].bv_offset))
  504. return false;
  505. }
  506. return true;
  507. }
  508. int blk_try_merge(struct request *rq, struct bio *bio)
  509. {
  510. if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
  511. return ELEVATOR_BACK_MERGE;
  512. else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
  513. return ELEVATOR_FRONT_MERGE;
  514. return ELEVATOR_NO_MERGE;
  515. }