blk-merge.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. {
  13. unsigned int phys_size;
  14. struct bio_vec *bv, *bvprv = NULL;
  15. int cluster, i, high, highprv = 1;
  16. unsigned int seg_size, nr_phys_segs;
  17. struct bio *fbio, *bbio;
  18. if (!bio)
  19. return 0;
  20. fbio = bio;
  21. cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  22. seg_size = 0;
  23. phys_size = nr_phys_segs = 0;
  24. for_each_bio(bio) {
  25. bio_for_each_segment(bv, bio, i) {
  26. /*
  27. * the trick here is making sure that a high page is
  28. * never considered part of another segment, since that
  29. * might change with the bounce page.
  30. */
  31. high = page_to_pfn(bv->bv_page) > queue_bounce_pfn(q);
  32. if (high || highprv)
  33. goto new_segment;
  34. if (cluster) {
  35. if (seg_size + bv->bv_len
  36. > queue_max_segment_size(q))
  37. goto new_segment;
  38. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
  39. goto new_segment;
  40. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
  41. goto new_segment;
  42. seg_size += bv->bv_len;
  43. bvprv = bv;
  44. continue;
  45. }
  46. new_segment:
  47. if (nr_phys_segs == 1 && seg_size >
  48. fbio->bi_seg_front_size)
  49. fbio->bi_seg_front_size = seg_size;
  50. nr_phys_segs++;
  51. bvprv = bv;
  52. seg_size = bv->bv_len;
  53. highprv = high;
  54. }
  55. bbio = bio;
  56. }
  57. if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
  58. fbio->bi_seg_front_size = seg_size;
  59. if (seg_size > bbio->bi_seg_back_size)
  60. bbio->bi_seg_back_size = seg_size;
  61. return nr_phys_segs;
  62. }
  63. void blk_recalc_rq_segments(struct request *rq)
  64. {
  65. rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
  66. }
  67. void blk_recount_segments(struct request_queue *q, struct bio *bio)
  68. {
  69. struct bio *nxt = bio->bi_next;
  70. bio->bi_next = NULL;
  71. bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
  72. bio->bi_next = nxt;
  73. bio->bi_flags |= (1 << BIO_SEG_VALID);
  74. }
  75. EXPORT_SYMBOL(blk_recount_segments);
  76. static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
  77. struct bio *nxt)
  78. {
  79. if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
  80. return 0;
  81. if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
  82. queue_max_segment_size(q))
  83. return 0;
  84. if (!bio_has_data(bio))
  85. return 1;
  86. if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
  87. return 0;
  88. /*
  89. * bio and nxt are contiguous in memory; check if the queue allows
  90. * these two to be merged into one
  91. */
  92. if (BIO_SEG_BOUNDARY(q, bio, nxt))
  93. return 1;
  94. return 0;
  95. }
  96. /*
  97. * map a request to scatterlist, return number of sg entries setup. Caller
  98. * must make sure sg can hold rq->nr_phys_segments entries
  99. */
  100. int blk_rq_map_sg(struct request_queue *q, struct request *rq,
  101. struct scatterlist *sglist)
  102. {
  103. struct bio_vec *bvec, *bvprv;
  104. struct req_iterator iter;
  105. struct scatterlist *sg;
  106. int nsegs, cluster;
  107. nsegs = 0;
  108. cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  109. /*
  110. * for each bio in rq
  111. */
  112. bvprv = NULL;
  113. sg = NULL;
  114. rq_for_each_segment(bvec, rq, iter) {
  115. int nbytes = bvec->bv_len;
  116. if (bvprv && cluster) {
  117. if (sg->length + nbytes > queue_max_segment_size(q))
  118. goto new_segment;
  119. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
  120. goto new_segment;
  121. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
  122. goto new_segment;
  123. sg->length += nbytes;
  124. } else {
  125. new_segment:
  126. if (!sg)
  127. sg = sglist;
  128. else {
  129. /*
  130. * If the driver previously mapped a shorter
  131. * list, we could see a termination bit
  132. * prematurely unless it fully inits the sg
  133. * table on each mapping. We KNOW that there
  134. * must be more entries here or the driver
  135. * would be buggy, so force clear the
  136. * termination bit to avoid doing a full
  137. * sg_init_table() in drivers for each command.
  138. */
  139. sg->page_link &= ~0x02;
  140. sg = sg_next(sg);
  141. }
  142. sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
  143. nsegs++;
  144. }
  145. bvprv = bvec;
  146. } /* segments in rq */
  147. if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
  148. (blk_rq_bytes(rq) & q->dma_pad_mask)) {
  149. unsigned int pad_len =
  150. (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
  151. sg->length += pad_len;
  152. rq->extra_len += pad_len;
  153. }
  154. if (q->dma_drain_size && q->dma_drain_needed(rq)) {
  155. if (rq->cmd_flags & REQ_RW)
  156. memset(q->dma_drain_buffer, 0, q->dma_drain_size);
  157. sg->page_link &= ~0x02;
  158. sg = sg_next(sg);
  159. sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
  160. q->dma_drain_size,
  161. ((unsigned long)q->dma_drain_buffer) &
  162. (PAGE_SIZE - 1));
  163. nsegs++;
  164. rq->extra_len += q->dma_drain_size;
  165. }
  166. if (sg)
  167. sg_mark_end(sg);
  168. return nsegs;
  169. }
  170. EXPORT_SYMBOL(blk_rq_map_sg);
  171. static inline int ll_new_hw_segment(struct request_queue *q,
  172. struct request *req,
  173. struct bio *bio)
  174. {
  175. int nr_phys_segs = bio_phys_segments(q, bio);
  176. if (req->nr_phys_segments + nr_phys_segs > queue_max_hw_segments(q) ||
  177. req->nr_phys_segments + nr_phys_segs > queue_max_phys_segments(q)) {
  178. req->cmd_flags |= REQ_NOMERGE;
  179. if (req == q->last_merge)
  180. q->last_merge = NULL;
  181. return 0;
  182. }
  183. /*
  184. * This will form the start of a new hw segment. Bump both
  185. * counters.
  186. */
  187. req->nr_phys_segments += nr_phys_segs;
  188. return 1;
  189. }
  190. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  191. struct bio *bio)
  192. {
  193. unsigned short max_sectors;
  194. if (unlikely(blk_pc_request(req)))
  195. max_sectors = queue_max_hw_sectors(q);
  196. else
  197. max_sectors = queue_max_sectors(q);
  198. if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
  199. req->cmd_flags |= REQ_NOMERGE;
  200. if (req == q->last_merge)
  201. q->last_merge = NULL;
  202. return 0;
  203. }
  204. if (!bio_flagged(req->biotail, BIO_SEG_VALID))
  205. blk_recount_segments(q, req->biotail);
  206. if (!bio_flagged(bio, BIO_SEG_VALID))
  207. blk_recount_segments(q, bio);
  208. return ll_new_hw_segment(q, req, bio);
  209. }
  210. int ll_front_merge_fn(struct request_queue *q, struct request *req,
  211. struct bio *bio)
  212. {
  213. unsigned short max_sectors;
  214. if (unlikely(blk_pc_request(req)))
  215. max_sectors = queue_max_hw_sectors(q);
  216. else
  217. max_sectors = queue_max_sectors(q);
  218. if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
  219. req->cmd_flags |= REQ_NOMERGE;
  220. if (req == q->last_merge)
  221. q->last_merge = NULL;
  222. return 0;
  223. }
  224. if (!bio_flagged(bio, BIO_SEG_VALID))
  225. blk_recount_segments(q, bio);
  226. if (!bio_flagged(req->bio, BIO_SEG_VALID))
  227. blk_recount_segments(q, req->bio);
  228. return ll_new_hw_segment(q, req, bio);
  229. }
  230. static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
  231. struct request *next)
  232. {
  233. int total_phys_segments;
  234. unsigned int seg_size =
  235. req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
  236. /*
  237. * First check if the either of the requests are re-queued
  238. * requests. Can't merge them if they are.
  239. */
  240. if (req->special || next->special)
  241. return 0;
  242. /*
  243. * Will it become too large?
  244. */
  245. if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > queue_max_sectors(q))
  246. return 0;
  247. total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
  248. if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
  249. if (req->nr_phys_segments == 1)
  250. req->bio->bi_seg_front_size = seg_size;
  251. if (next->nr_phys_segments == 1)
  252. next->biotail->bi_seg_back_size = seg_size;
  253. total_phys_segments--;
  254. }
  255. if (total_phys_segments > queue_max_phys_segments(q))
  256. return 0;
  257. if (total_phys_segments > queue_max_hw_segments(q))
  258. return 0;
  259. /* Merge is OK... */
  260. req->nr_phys_segments = total_phys_segments;
  261. return 1;
  262. }
  263. /**
  264. * blk_rq_set_mixed_merge - mark a request as mixed merge
  265. * @rq: request to mark as mixed merge
  266. *
  267. * Description:
  268. * @rq is about to be mixed merged. Make sure the attributes
  269. * which can be mixed are set in each bio and mark @rq as mixed
  270. * merged.
  271. */
  272. void blk_rq_set_mixed_merge(struct request *rq)
  273. {
  274. unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
  275. struct bio *bio;
  276. if (rq->cmd_flags & REQ_MIXED_MERGE)
  277. return;
  278. /*
  279. * @rq will no longer represent mixable attributes for all the
  280. * contained bios. It will just track those of the first one.
  281. * Distributes the attributs to each bio.
  282. */
  283. for (bio = rq->bio; bio; bio = bio->bi_next) {
  284. WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
  285. (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
  286. bio->bi_rw |= ff;
  287. }
  288. rq->cmd_flags |= REQ_MIXED_MERGE;
  289. }
  290. static void blk_account_io_merge(struct request *req)
  291. {
  292. if (blk_do_io_stat(req)) {
  293. struct hd_struct *part;
  294. int cpu;
  295. cpu = part_stat_lock();
  296. part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
  297. part_round_stats(cpu, part);
  298. part_dec_in_flight(part, rq_data_dir(req));
  299. part_stat_unlock();
  300. }
  301. }
  302. /*
  303. * Has to be called with the request spinlock acquired
  304. */
  305. static int attempt_merge(struct request_queue *q, struct request *req,
  306. struct request *next)
  307. {
  308. if (!rq_mergeable(req) || !rq_mergeable(next))
  309. return 0;
  310. /*
  311. * not contiguous
  312. */
  313. if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
  314. return 0;
  315. if (rq_data_dir(req) != rq_data_dir(next)
  316. || req->rq_disk != next->rq_disk
  317. || next->special)
  318. return 0;
  319. if (blk_integrity_rq(req) != blk_integrity_rq(next))
  320. return 0;
  321. /*
  322. * If we are allowed to merge, then append bio list
  323. * from next to rq and release next. merge_requests_fn
  324. * will have updated segment counts, update sector
  325. * counts here.
  326. */
  327. if (!ll_merge_requests_fn(q, req, next))
  328. return 0;
  329. /*
  330. * If failfast settings disagree or any of the two is already
  331. * a mixed merge, mark both as mixed before proceeding. This
  332. * makes sure that all involved bios have mixable attributes
  333. * set properly.
  334. */
  335. if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
  336. (req->cmd_flags & REQ_FAILFAST_MASK) !=
  337. (next->cmd_flags & REQ_FAILFAST_MASK)) {
  338. blk_rq_set_mixed_merge(req);
  339. blk_rq_set_mixed_merge(next);
  340. }
  341. /*
  342. * At this point we have either done a back merge
  343. * or front merge. We need the smaller start_time of
  344. * the merged requests to be the current request
  345. * for accounting purposes.
  346. */
  347. if (time_after(req->start_time, next->start_time))
  348. req->start_time = next->start_time;
  349. req->biotail->bi_next = next->bio;
  350. req->biotail = next->biotail;
  351. req->__data_len += blk_rq_bytes(next);
  352. elv_merge_requests(q, req, next);
  353. /*
  354. * 'next' is going away, so update stats accordingly
  355. */
  356. blk_account_io_merge(next);
  357. req->ioprio = ioprio_best(req->ioprio, next->ioprio);
  358. if (blk_rq_cpu_valid(next))
  359. req->cpu = next->cpu;
  360. /* owner-ship of bio passed from next to req */
  361. next->bio = NULL;
  362. __blk_put_request(q, next);
  363. return 1;
  364. }
  365. int attempt_back_merge(struct request_queue *q, struct request *rq)
  366. {
  367. struct request *next = elv_latter_request(q, rq);
  368. if (next)
  369. return attempt_merge(q, rq, next);
  370. return 0;
  371. }
  372. int attempt_front_merge(struct request_queue *q, struct request *rq)
  373. {
  374. struct request *prev = elv_former_request(q, rq);
  375. if (prev)
  376. return attempt_merge(q, prev, rq);
  377. return 0;
  378. }