bounce.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/memblock.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. struct bvec_iter orig_iter = bio_orig->bi_iter;
  138. struct bvec_iter_all iter_all;
  139. /*
  140. * free up bounce indirect pages used
  141. */
  142. bio_for_each_segment_all(bvec, bio, iter_all) {
  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_crypt_clone(bio, bio_src, gfp_mask) < 0)
  227. goto err_put;
  228. if (bio_integrity(bio_src) &&
  229. bio_integrity_clone(bio, bio_src, gfp_mask) < 0)
  230. goto err_put;
  231. bio_clone_blkg_association(bio, bio_src);
  232. blkcg_bio_issue_init(bio);
  233. return bio;
  234. err_put:
  235. bio_put(bio);
  236. return NULL;
  237. }
  238. static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
  239. mempool_t *pool)
  240. {
  241. struct bio *bio;
  242. int rw = bio_data_dir(*bio_orig);
  243. struct bio_vec *to, from;
  244. struct bvec_iter iter;
  245. unsigned i = 0;
  246. bool bounce = false;
  247. int sectors = 0;
  248. bool passthrough = bio_is_passthrough(*bio_orig);
  249. bio_for_each_segment(from, *bio_orig, iter) {
  250. if (i++ < BIO_MAX_PAGES)
  251. sectors += from.bv_len >> 9;
  252. if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn)
  253. bounce = true;
  254. }
  255. if (!bounce)
  256. return;
  257. if (!passthrough && sectors < bio_sectors(*bio_orig)) {
  258. bio = bio_split(*bio_orig, sectors, GFP_NOIO, &bounce_bio_split);
  259. bio_chain(bio, *bio_orig);
  260. submit_bio_noacct(*bio_orig);
  261. *bio_orig = bio;
  262. }
  263. bio = bounce_clone_bio(*bio_orig, GFP_NOIO, passthrough ? NULL :
  264. &bounce_bio_set);
  265. /*
  266. * Bvec table can't be updated by bio_for_each_segment_all(),
  267. * so retrieve bvec from the table directly. This way is safe
  268. * because the 'bio' is single-page bvec.
  269. */
  270. for (i = 0, to = bio->bi_io_vec; i < bio->bi_vcnt; to++, i++) {
  271. struct page *page = to->bv_page;
  272. if (page_to_pfn(page) <= q->limits.bounce_pfn)
  273. continue;
  274. to->bv_page = mempool_alloc(pool, q->bounce_gfp);
  275. inc_zone_page_state(to->bv_page, NR_BOUNCE);
  276. if (rw == WRITE) {
  277. char *vto, *vfrom;
  278. flush_dcache_page(page);
  279. vto = page_address(to->bv_page) + to->bv_offset;
  280. vfrom = kmap_atomic(page) + to->bv_offset;
  281. memcpy(vto, vfrom, to->bv_len);
  282. kunmap_atomic(vfrom);
  283. }
  284. }
  285. trace_block_bio_bounce(q, *bio_orig);
  286. bio->bi_flags |= (1 << BIO_BOUNCED);
  287. if (pool == &page_pool) {
  288. bio->bi_end_io = bounce_end_io_write;
  289. if (rw == READ)
  290. bio->bi_end_io = bounce_end_io_read;
  291. } else {
  292. bio->bi_end_io = bounce_end_io_write_isa;
  293. if (rw == READ)
  294. bio->bi_end_io = bounce_end_io_read_isa;
  295. }
  296. bio->bi_private = *bio_orig;
  297. *bio_orig = bio;
  298. }
  299. void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
  300. {
  301. mempool_t *pool;
  302. /*
  303. * Data-less bio, nothing to bounce
  304. */
  305. if (!bio_has_data(*bio_orig))
  306. return;
  307. /*
  308. * for non-isa bounce case, just check if the bounce pfn is equal
  309. * to or bigger than the highest pfn in the system -- in that case,
  310. * don't waste time iterating over bio segments
  311. */
  312. if (!(q->bounce_gfp & GFP_DMA)) {
  313. if (q->limits.bounce_pfn >= blk_max_pfn)
  314. return;
  315. pool = &page_pool;
  316. } else {
  317. BUG_ON(!mempool_initialized(&isa_page_pool));
  318. pool = &isa_page_pool;
  319. }
  320. /*
  321. * slow path
  322. */
  323. __blk_queue_bounce(q, bio_orig, pool);
  324. }