bounce.c 6.4 KB

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