bounce.c 7.4 KB

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