blk-merge.c 13 KB

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