bounce.c 9.1 KB

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