bounce.c 5.8 KB

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