bounce.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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) || defined(CONFIG_NEED_BOUNCE_POOL)
  29. static __init int init_emergency_pool(void)
  30. {
  31. #if defined(CONFIG_HIGHMEM) && !defined(CONFIG_MEMORY_HOTPLUG)
  32. if (max_pfn <= max_low_pfn)
  33. return 0;
  34. #endif
  35. page_pool = mempool_create_page_pool(POOL_SIZE, 0);
  36. BUG_ON(!page_pool);
  37. pr_info("pool size: %d pages\n", POOL_SIZE);
  38. bounce_bio_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
  39. BUG_ON(!bounce_bio_set);
  40. if (bioset_integrity_create(bounce_bio_set, BIO_POOL_SIZE))
  41. BUG_ON(1);
  42. bounce_bio_split = bioset_create(BIO_POOL_SIZE, 0, 0);
  43. BUG_ON(!bounce_bio_split);
  44. return 0;
  45. }
  46. __initcall(init_emergency_pool);
  47. #endif
  48. #ifdef CONFIG_HIGHMEM
  49. /*
  50. * highmem version, map in to vec
  51. */
  52. static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
  53. {
  54. unsigned long flags;
  55. unsigned char *vto;
  56. local_irq_save(flags);
  57. vto = kmap_atomic(to->bv_page);
  58. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  59. kunmap_atomic(vto);
  60. local_irq_restore(flags);
  61. }
  62. #else /* CONFIG_HIGHMEM */
  63. #define bounce_copy_vec(to, vfrom) \
  64. memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
  65. #endif /* CONFIG_HIGHMEM */
  66. /*
  67. * allocate pages in the DMA region for the ISA pool
  68. */
  69. static void *mempool_alloc_pages_isa(gfp_t gfp_mask, void *data)
  70. {
  71. return mempool_alloc_pages(gfp_mask | GFP_DMA, data);
  72. }
  73. /*
  74. * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
  75. * as the max address, so check if the pool has already been created.
  76. */
  77. int init_emergency_isa_pool(void)
  78. {
  79. if (isa_page_pool)
  80. return 0;
  81. isa_page_pool = mempool_create(ISA_POOL_SIZE, mempool_alloc_pages_isa,
  82. mempool_free_pages, (void *) 0);
  83. BUG_ON(!isa_page_pool);
  84. pr_info("isa pool size: %d pages\n", ISA_POOL_SIZE);
  85. return 0;
  86. }
  87. /*
  88. * Simple bounce buffer support for highmem pages. Depending on the
  89. * queue gfp mask set, *to may or may not be a highmem page. kmap it
  90. * always, it will do the Right Thing
  91. */
  92. static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
  93. {
  94. unsigned char *vfrom;
  95. struct bio_vec tovec, *fromvec = from->bi_io_vec;
  96. struct bvec_iter iter;
  97. bio_for_each_segment(tovec, to, iter) {
  98. if (tovec.bv_page != fromvec->bv_page) {
  99. /*
  100. * fromvec->bv_offset and fromvec->bv_len might have
  101. * been modified by the block layer, so use the original
  102. * copy, bounce_copy_vec already uses tovec->bv_len
  103. */
  104. vfrom = page_address(fromvec->bv_page) +
  105. tovec.bv_offset;
  106. bounce_copy_vec(&tovec, vfrom);
  107. flush_dcache_page(tovec.bv_page);
  108. }
  109. fromvec++;
  110. }
  111. }
  112. static void bounce_end_io(struct bio *bio, mempool_t *pool)
  113. {
  114. struct bio *bio_orig = bio->bi_private;
  115. struct bio_vec *bvec, *org_vec;
  116. int i;
  117. int start = bio_orig->bi_iter.bi_idx;
  118. /*
  119. * free up bounce indirect pages used
  120. */
  121. bio_for_each_segment_all(bvec, bio, i) {
  122. org_vec = bio_orig->bi_io_vec + i + start;
  123. if (bvec->bv_page == org_vec->bv_page)
  124. continue;
  125. dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
  126. mempool_free(bvec->bv_page, pool);
  127. }
  128. bio_orig->bi_status = bio->bi_status;
  129. bio_endio(bio_orig);
  130. bio_put(bio);
  131. }
  132. static void bounce_end_io_write(struct bio *bio)
  133. {
  134. bounce_end_io(bio, page_pool);
  135. }
  136. static void bounce_end_io_write_isa(struct bio *bio)
  137. {
  138. bounce_end_io(bio, isa_page_pool);
  139. }
  140. static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
  141. {
  142. struct bio *bio_orig = bio->bi_private;
  143. if (!bio->bi_status)
  144. copy_to_high_bio_irq(bio_orig, bio);
  145. bounce_end_io(bio, pool);
  146. }
  147. static void bounce_end_io_read(struct bio *bio)
  148. {
  149. __bounce_end_io_read(bio, page_pool);
  150. }
  151. static void bounce_end_io_read_isa(struct bio *bio)
  152. {
  153. __bounce_end_io_read(bio, isa_page_pool);
  154. }
  155. static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
  156. mempool_t *pool)
  157. {
  158. struct bio *bio;
  159. int rw = bio_data_dir(*bio_orig);
  160. struct bio_vec *to, from;
  161. struct bvec_iter iter;
  162. unsigned i = 0;
  163. bool bounce = false;
  164. int sectors = 0;
  165. bool passthrough = bio_is_passthrough(*bio_orig);
  166. bio_for_each_segment(from, *bio_orig, iter) {
  167. if (i++ < BIO_MAX_PAGES)
  168. sectors += from.bv_len >> 9;
  169. if (page_to_pfn(from.bv_page) > q->limits.bounce_pfn)
  170. bounce = true;
  171. }
  172. if (!bounce)
  173. return;
  174. if (!passthrough && sectors < bio_sectors(*bio_orig)) {
  175. bio = bio_split(*bio_orig, sectors, GFP_NOIO, bounce_bio_split);
  176. bio_chain(bio, *bio_orig);
  177. generic_make_request(*bio_orig);
  178. *bio_orig = bio;
  179. }
  180. bio = bio_clone_bioset(*bio_orig, GFP_NOIO, passthrough ? NULL :
  181. bounce_bio_set);
  182. bio_for_each_segment_all(to, bio, i) {
  183. struct page *page = to->bv_page;
  184. if (page_to_pfn(page) <= q->limits.bounce_pfn)
  185. continue;
  186. to->bv_page = mempool_alloc(pool, q->bounce_gfp);
  187. inc_zone_page_state(to->bv_page, NR_BOUNCE);
  188. if (rw == WRITE) {
  189. char *vto, *vfrom;
  190. flush_dcache_page(page);
  191. vto = page_address(to->bv_page) + to->bv_offset;
  192. vfrom = kmap_atomic(page) + to->bv_offset;
  193. memcpy(vto, vfrom, to->bv_len);
  194. kunmap_atomic(vfrom);
  195. }
  196. }
  197. trace_block_bio_bounce(q, *bio_orig);
  198. bio->bi_flags |= (1 << BIO_BOUNCED);
  199. if (pool == page_pool) {
  200. bio->bi_end_io = bounce_end_io_write;
  201. if (rw == READ)
  202. bio->bi_end_io = bounce_end_io_read;
  203. } else {
  204. bio->bi_end_io = bounce_end_io_write_isa;
  205. if (rw == READ)
  206. bio->bi_end_io = bounce_end_io_read_isa;
  207. }
  208. bio->bi_private = *bio_orig;
  209. *bio_orig = bio;
  210. }
  211. void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
  212. {
  213. mempool_t *pool;
  214. /*
  215. * Data-less bio, nothing to bounce
  216. */
  217. if (!bio_has_data(*bio_orig))
  218. return;
  219. /*
  220. * for non-isa bounce case, just check if the bounce pfn is equal
  221. * to or bigger than the highest pfn in the system -- in that case,
  222. * don't waste time iterating over bio segments
  223. */
  224. if (!(q->bounce_gfp & GFP_DMA)) {
  225. if (q->limits.bounce_pfn >= blk_max_pfn)
  226. return;
  227. pool = page_pool;
  228. } else {
  229. BUG_ON(!isa_page_pool);
  230. pool = isa_page_pool;
  231. }
  232. /*
  233. * slow path
  234. */
  235. __blk_queue_bounce(q, bio_orig, pool);
  236. }