bounce.c 6.5 KB

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