blk-merge.c 11 KB

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