bounce.c 6.5 KB

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