bounce.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_NEED_BOUNCE_POOL)
  23. static __init int init_emergency_pool(void)
  24. {
  25. #if defined(CONFIG_HIGHMEM) && !defined(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("bounce pool size: %d pages\n", POOL_SIZE);
  32. return 0;
  33. }
  34. __initcall(init_emergency_pool);
  35. #endif
  36. #ifdef CONFIG_HIGHMEM
  37. /*
  38. * highmem version, map in to vec
  39. */
  40. static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
  41. {
  42. unsigned long flags;
  43. unsigned char *vto;
  44. local_irq_save(flags);
  45. vto = kmap_atomic(to->bv_page);
  46. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  47. kunmap_atomic(vto);
  48. local_irq_restore(flags);
  49. }
  50. #else /* CONFIG_HIGHMEM */
  51. #define bounce_copy_vec(to, vfrom) \
  52. memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
  53. #endif /* CONFIG_HIGHMEM */
  54. /*
  55. * allocate pages in the DMA region for the ISA pool
  56. */
  57. static void *mempool_alloc_pages_isa(gfp_t gfp_mask, void *data)
  58. {
  59. return mempool_alloc_pages(gfp_mask | GFP_DMA, data);
  60. }
  61. /*
  62. * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
  63. * as the max address, so check if the pool has already been created.
  64. */
  65. int init_emergency_isa_pool(void)
  66. {
  67. if (isa_page_pool)
  68. return 0;
  69. isa_page_pool = mempool_create(ISA_POOL_SIZE, mempool_alloc_pages_isa,
  70. mempool_free_pages, (void *) 0);
  71. BUG_ON(!isa_page_pool);
  72. printk("isa bounce pool size: %d pages\n", ISA_POOL_SIZE);
  73. return 0;
  74. }
  75. /*
  76. * Simple bounce buffer support for highmem pages. Depending on the
  77. * queue gfp mask set, *to may or may not be a highmem page. kmap it
  78. * always, it will do the Right Thing
  79. */
  80. static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
  81. {
  82. unsigned char *vfrom;
  83. struct bio_vec *tovec, *fromvec;
  84. int i;
  85. __bio_for_each_segment(tovec, to, i, 0) {
  86. fromvec = from->bi_io_vec + i;
  87. /*
  88. * not bounced
  89. */
  90. if (tovec->bv_page == fromvec->bv_page)
  91. continue;
  92. /*
  93. * fromvec->bv_offset and fromvec->bv_len might have been
  94. * modified by the block layer, so use the original copy,
  95. * bounce_copy_vec already uses tovec->bv_len
  96. */
  97. vfrom = page_address(fromvec->bv_page) + tovec->bv_offset;
  98. bounce_copy_vec(tovec, vfrom);
  99. flush_dcache_page(tovec->bv_page);
  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(bvec, bio, i, 0) {
  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. struct page *page;
  149. struct backing_dev_info *bdi;
  150. struct address_space *mapping;
  151. struct bio_vec *from;
  152. int i;
  153. if (bio_data_dir(bio) != WRITE)
  154. return 0;
  155. if (!bdi_cap_stable_pages_required(&q->backing_dev_info))
  156. return 0;
  157. /*
  158. * Based on the first page that has a valid mapping, decide whether or
  159. * not we have to employ bounce buffering to guarantee stable pages.
  160. */
  161. bio_for_each_segment(from, bio, i) {
  162. page = from->bv_page;
  163. mapping = page_mapping(page);
  164. if (!mapping)
  165. continue;
  166. bdi = mapping->backing_dev_info;
  167. return mapping->host->i_sb->s_flags & MS_SNAP_STABLE;
  168. }
  169. return 0;
  170. }
  171. #else
  172. static int must_snapshot_stable_pages(struct request_queue *q, struct bio *bio)
  173. {
  174. return 0;
  175. }
  176. #endif /* CONFIG_NEED_BOUNCE_POOL */
  177. static void __blk_queue_bounce(struct request_queue *q, struct bio **bio_orig,
  178. mempool_t *pool, int force)
  179. {
  180. struct page *page;
  181. struct bio *bio = NULL;
  182. int i, rw = bio_data_dir(*bio_orig);
  183. struct bio_vec *to, *from;
  184. bio_for_each_segment(from, *bio_orig, i) {
  185. page = from->bv_page;
  186. /*
  187. * is destination page below bounce pfn?
  188. */
  189. if (page_to_pfn(page) <= queue_bounce_pfn(q) && !force)
  190. continue;
  191. /*
  192. * irk, bounce it
  193. */
  194. if (!bio) {
  195. unsigned int cnt = (*bio_orig)->bi_vcnt;
  196. bio = bio_alloc(GFP_NOIO, cnt);
  197. memset(bio->bi_io_vec, 0, cnt * sizeof(struct bio_vec));
  198. }
  199. to = bio->bi_io_vec + i;
  200. to->bv_page = mempool_alloc(pool, q->bounce_gfp);
  201. to->bv_len = from->bv_len;
  202. to->bv_offset = from->bv_offset;
  203. inc_zone_page_state(to->bv_page, NR_BOUNCE);
  204. if (rw == WRITE) {
  205. char *vto, *vfrom;
  206. flush_dcache_page(from->bv_page);
  207. vto = page_address(to->bv_page) + to->bv_offset;
  208. vfrom = kmap(from->bv_page) + from->bv_offset;
  209. memcpy(vto, vfrom, to->bv_len);
  210. kunmap(from->bv_page);
  211. }
  212. }
  213. /*
  214. * no pages bounced
  215. */
  216. if (!bio)
  217. return;
  218. trace_block_bio_bounce(q, *bio_orig);
  219. /*
  220. * at least one page was bounced, fill in possible non-highmem
  221. * pages
  222. */
  223. __bio_for_each_segment(from, *bio_orig, i, 0) {
  224. to = bio_iovec_idx(bio, i);
  225. if (!to->bv_page) {
  226. to->bv_page = from->bv_page;
  227. to->bv_len = from->bv_len;
  228. to->bv_offset = from->bv_offset;
  229. }
  230. }
  231. bio->bi_bdev = (*bio_orig)->bi_bdev;
  232. bio->bi_flags |= (1 << BIO_BOUNCED);
  233. bio->bi_sector = (*bio_orig)->bi_sector;
  234. bio->bi_rw = (*bio_orig)->bi_rw;
  235. bio->bi_vcnt = (*bio_orig)->bi_vcnt;
  236. bio->bi_idx = (*bio_orig)->bi_idx;
  237. bio->bi_size = (*bio_orig)->bi_size;
  238. if (pool == page_pool) {
  239. bio->bi_end_io = bounce_end_io_write;
  240. if (rw == READ)
  241. bio->bi_end_io = bounce_end_io_read;
  242. } else {
  243. bio->bi_end_io = bounce_end_io_write_isa;
  244. if (rw == READ)
  245. bio->bi_end_io = bounce_end_io_read_isa;
  246. }
  247. bio->bi_private = *bio_orig;
  248. *bio_orig = bio;
  249. }
  250. void blk_queue_bounce(struct request_queue *q, struct bio **bio_orig)
  251. {
  252. int must_bounce;
  253. mempool_t *pool;
  254. /*
  255. * Data-less bio, nothing to bounce
  256. */
  257. if (!bio_has_data(*bio_orig))
  258. return;
  259. must_bounce = must_snapshot_stable_pages(q, *bio_orig);
  260. /*
  261. * for non-isa bounce case, just check if the bounce pfn is equal
  262. * to or bigger than the highest pfn in the system -- in that case,
  263. * don't waste time iterating over bio segments
  264. */
  265. if (!(q->bounce_gfp & GFP_DMA)) {
  266. if (queue_bounce_pfn(q) >= blk_max_pfn && !must_bounce)
  267. return;
  268. pool = page_pool;
  269. } else {
  270. BUG_ON(!isa_page_pool);
  271. pool = isa_page_pool;
  272. }
  273. /*
  274. * slow path
  275. */
  276. __blk_queue_bounce(q, bio_orig, pool, must_bounce);
  277. }
  278. EXPORT_SYMBOL(blk_queue_bounce);