blk-merge.c 14 KB

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