bounce.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* bounce buffer handling for block devices
  3. *
  4. * - Split from highmem.c
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/mm.h>
  8. #include <linux/export.h>
  9. #include <linux/swap.h>
  10. #include <linux/gfp.h>
  11. #include <linux/bio.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/mempool.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/init.h>
  17. #include <linux/hash.h>
  18. #include <linux/highmem.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/printk.h>
  21. #include <asm/tlbflush.h>
  22. #include <trace/events/block.h>
  23. #include "blk.h"
  24. #define POOL_SIZE 64
  25. #define ISA_POOL_SIZE 16
  26. static struct bio_set bounce_bio_set, bounce_bio_split;
  27. static mempool_t page_pool, isa_page_pool;
  28. #if defined(CONFIG_HIGHMEM)
  29. static __init int init_emergency_pool(void)
  30. {
  31. int ret;
  32. #if defined(CONFIG_HIGHMEM) && !defined(CONFIG_MEMORY_HOTPLUG)
  33. if (max_pfn <= max_low_pfn)
  34. return 0;
  35. #endif
  36. ret = mempool_init_page_pool(&page_pool, POOL_SIZE, 0);
  37. BUG_ON(ret);
  38. pr_info("pool size: %d pages\n", POOL_SIZE);
  39. ret = bioset_init(&bounce_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
  40. BUG_ON(ret);
  41. if (bioset_integrity_create(&bounce_bio_set, BIO_POOL_SIZE))
  42. BUG_ON(1);
  43. ret = bioset_init(&bounce_bio_split, BIO_POOL_SIZE, 0, 0);
  44. BUG_ON(ret);
  45. return 0;
  46. }
  47. __initcall(init_emergency_pool);
  48. #endif
  49. #ifdef CONFIG_HIGHMEM
  50. /*
  51. * highmem version, map in to vec
  52. */
  53. static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
  54. {
  55. unsigned char *vto;
  56. vto = kmap_atomic(to->bv_page);
  57. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  58. kunmap_atomic(vto);
  59. }
  60. #else /* CONFIG_HIGHMEM */
  61. #define bounce_copy_vec(to, vfrom) \
  62. memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
  63. #endif /* CONFIG_HIGHMEM */
  64. /*
  65. * allocate pages in the DMA region for the ISA pool
  66. */
  67. static void *mempool_alloc_pages_isa(gfp_t gfp_mask, void *data)
  68. {
  69. return mempool_alloc_pages(gfp_mask | GFP_DMA, data);
  70. }
  71. /*
  72. * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
  73. * as the max address, so check if the pool has already been created.
  74. */
  75. int init_emergency_isa_pool(void)
  76. {
  77. int ret;
  78. if (mempool_initialized(&isa_page_pool))
  79. return 0;
  80. ret = mempool_init(&isa_page_pool, ISA_POOL_SIZE, mempool_alloc_pages_isa,
  81. mempool_free_pages, (void *) 0);
  82. BUG_ON(ret);
  83. pr_info("isa pool size: %d pages\n", ISA_POOL_SIZE);
  84. return 0;
  85. }
  86. /*
  87. * Simple bounce buffer support for highmem pages. Depending on the
  88. * queue gfp mask set, *to may or may not be a highmem page. kmap it
  89. * always, it will do the Right Thing
  90. */
  91. static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
  92. {
  93. unsigned char *vfrom;
  94. struct bio_vec tovec, fromvec;
  95. struct bvec_iter iter;
  96. /*
  97. * The bio of @from is created by bounce, so we can iterate
  98. * its bvec from start to end, but the @from->bi_iter can't be
  99. * trusted because it might be changed by splitting.
  100. */
  101. struct bvec_iter from_iter = BVEC_ITER_ALL_INIT;
  102. bio_for_each_segment(tovec, to, iter) {
  103. fromvec = bio_iter_iovec(from, from_iter);
  104. if (tovec.bv_page != fromvec.bv_page) {
  105. /*
  106. * fromvec->bv_offset and fromvec->bv_len might have
  107. * been modified by the block layer, so use the original
  108. * copy, bounce_copy_vec already uses tovec->bv_len
  109. */
  110. vfrom = page_address(fromvec.bv_page) +
  111. tovec.bv_offset;
  112. bounce_copy_vec(&tovec, vfrom);
  113. flush_dcache_page(tovec.bv_page);
  114. }
  115. bio_advance_iter(from, &from_iter, tovec.bv_len);
  116. }
  117. }
  118. static void bounce_end_io(struct bio *bio, mempool_t *pool)
  119. {
  120. struct bio *bio_orig = bio->bi_private;
  121. struct bio_vec *bvec, orig_vec;
  122. int i;
  123. struct bvec_iter orig_iter = bio_orig->bi_iter;
  124. /*
  125. * free up bounce indirect pages used
  126. */
  127. bio_for_each_segment_all(bvec, bio, i) {
  128. orig_vec = bio_iter_iovec(bio_orig, orig_iter);
  129. if (bvec->bv_page != orig_vec.bv_page) {
  130. dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
  131. mempool_free(bvec->bv_page, pool);
  132. }
  133. bio_advance_iter(bio_orig, &orig_iter, orig_vec.bv_len);
  134. }
  135. bio_orig->bi_status = bio->bi_status;
  136. bio_endio(bio_orig);
  137. bio_put(bio);
  138. }
  139. static void bounce_end_io_write(struct bio *bio)
  140. {
  141. bounce_end_io(bio, &page_pool);
  142. }
  143. static void bounce_end_io_write_isa(struct bio *bio)
  144. {
  145. bounce_end_io(bio, &isa_page_pool);
  146. }
  147. static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
  148. {
  149. struct bio *bio_orig = bio->bi_private;
  150. if (!bio->bi_status)
  151. copy_to_high_bio_irq(bio_orig, bio);
  152. bounce_end_io(bio, pool);
  153. }
  154. static void bounce_end_io_read(struct bio *bio)
  155. {
  156. __bounce_end_io_read(bio, &page_pool);
  157. }
  158. static void bounce_end_io_read_isa(struct bio *bio)
  159. {
  160. __bounce_end_io_read(bio, &isa_page_pool);
  161. }
  162. static struct bio *bounce_clone_bio(struct bio *bio_src, gfp_t gfp_mask,
  163. struct bio_set *bs)
  164. {
  165. struct bvec_iter iter;
  166. struct bio_vec bv;
  167. struct bio *bio;
  168. /*
  169. * Pre immutable biovecs, __bio_clone() used to just do a memcpy from
  170. * bio_src->bi_io_vec to bio->bi_io_vec.
  171. *
  172. * We can't do that anymore, because:
  173. *
  174. * - The point of cloning the biovec is to produce a bio with a biovec
  175. * the caller can modify: bi_idx and bi_bvec_done should be 0.
  176. *
  177. * - The original bio could've had more than BIO_MAX_PAGES biovecs; if
  178. * we tried to clone the whole thing bio_alloc_bioset() would fail.
  179. * But the clone should succeed as long as the number of biovecs we
  180. * actually need to allocate is fewer than BIO_MAX_PAGES.
  181. *
  182. * - Lastly, bi_vcnt should not be looked at or relied upon by code
  183. * that does not own the bio - reason being drivers don't use it for
  184. * iterating over the biovec anymore, so expecting it to be kept up
  185. * to date (i.e. for clones that share the parent biovec) is just
  186. * asking for trouble and would force extra work on
  187. * __bio_clone_fast() anyways.
  188. */
  189. bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs);
  190. if (!bio)
  191. return NULL;
  192. bio->bi_disk = bio_src->bi_disk;
  193. bio->bi_opf = bio_src->bi_opf;
  194. bio->bi_write_hint = bio_src->bi_write_hint;
  195. bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
  196. bio->bi_iter.bi_size = bio_src->bi_iter.bi_size;
  197. switch (bio_op(bio)) {
  198. case REQ_OP_DISCARD:
  199. case REQ_OP_SECURE_ERASE:
  200. case REQ_OP_WRITE_ZEROES:
  201. break;
  202. case REQ_OP_WRITE_SAME:
  203. bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0];
  204. break;
  205. default:
  206. bio_for_each_segment(bv, bio_src, iter)
  207. bio->bi_io_vec[bio->bi_vcnt++] = bv;
  208. break;
  209. }
  210. if (bio_integrity(bio_src)) {
  211. int ret;
  212. ret = bio_integrity_clone(bio, bio_src, gfp_mask);
  213. if (ret < 0) {
  214. bio_put(bio);
  215. return NULL;
  216. }
  217. }
  218. bio_clone_blkcg_association(bio, bio_src);
  219. return bio;
  220. }
  221. static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
  222. mempool_t *pool)
  223. {
  224. struct bio *bio;
  225. int rw = bio_data_dir(*bio_orig);
  226. struct bio_vec *to, from;
  227. struct bvec_iter iter;
  228. unsigned i = 0;
  229. bool bounce = false;
  230. int sectors = 0;
  231. bool passthrough = bio_is_passthrough(*bio_orig);
  232. bio_for_each_segment(from, *bio_orig, iter) {
  233. if (i++ < BIO_MAX_PAGES)
  234. sectors += from.bv_len >> 9;
  235. if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn)
  236. bounce = true;
  237. }
  238. if (!bounce)
  239. return;
  240. if (!passthrough && sectors < bio_sectors(*bio_orig)) {
  241. bio = bio_split(*bio_orig, sectors, GFP_NOIO, &bounce_bio_split);
  242. bio_chain(bio, *bio_orig);
  243. generic_make_request(*bio_orig);
  244. *bio_orig = bio;
  245. }
  246. bio = bounce_clone_bio(*bio_orig, GFP_NOIO, passthrough ? NULL :
  247. &bounce_bio_set);
  248. bio_for_each_segment_all(to, bio, i) {
  249. struct page *page = to->bv_page;
  250. if (page_to_pfn(page) <= q->limits.bounce_pfn)
  251. continue;
  252. to->bv_page = mempool_alloc(pool, q->bounce_gfp);
  253. inc_zone_page_state(to->bv_page, NR_BOUNCE);
  254. if (rw == WRITE) {
  255. char *vto, *vfrom;
  256. flush_dcache_page(page);
  257. vto = page_address(to->bv_page) + to->bv_offset;
  258. vfrom = kmap_atomic(page) + to->bv_offset;
  259. memcpy(vto, vfrom, to->bv_len);
  260. kunmap_atomic(vfrom);
  261. }
  262. }
  263. trace_block_bio_bounce(q, *bio_orig);
  264. bio->bi_flags |= (1 << BIO_BOUNCED);
  265. if (pool == &page_pool) {
  266. bio->bi_end_io = bounce_end_io_write;
  267. if (rw == READ)
  268. bio->bi_end_io = bounce_end_io_read;
  269. } else {
  270. bio->bi_end_io = bounce_end_io_write_isa;
  271. if (rw == READ)
  272. bio->bi_end_io = bounce_end_io_read_isa;
  273. }
  274. bio->bi_private = *bio_orig;
  275. *bio_orig = bio;
  276. }
  277. void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
  278. {
  279. mempool_t *pool;
  280. /*
  281. * Data-less bio, nothing to bounce
  282. */
  283. if (!bio_has_data(*bio_orig))
  284. return;
  285. /*
  286. * for non-isa bounce case, just check if the bounce pfn is equal
  287. * to or bigger than the highest pfn in the system -- in that case,
  288. * don't waste time iterating over bio segments
  289. */
  290. if (!(q->bounce_gfp & GFP_DMA)) {
  291. if (q->limits.bounce_pfn >= blk_max_pfn)
  292. return;
  293. pool = &page_pool;
  294. } else {
  295. BUG_ON(!mempool_initialized(&isa_page_pool));
  296. pool = &isa_page_pool;
  297. }
  298. /*
  299. * slow path
  300. */
  301. __blk_queue_bounce(q, bio_orig, pool);
  302. }