bounce.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* bounce buffer handling for block devices
  3. *
  4. * - Split from highmem.c
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/mm.h>
  8. #include <linux/export.h>
  9. #include <linux/swap.h>
  10. #include <linux/gfp.h>
  11. #include <linux/bio-integrity.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/mempool.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/init.h>
  17. #include <linux/hash.h>
  18. #include <linux/highmem.h>
  19. #include <linux/printk.h>
  20. #include <asm/tlbflush.h>
  21. #include <trace/events/block.h>
  22. #include "blk.h"
  23. #include "blk-cgroup.h"
  24. #define POOL_SIZE 64
  25. #define ISA_POOL_SIZE 16
  26. static struct bio_set bounce_bio_set, bounce_bio_split;
  27. static mempool_t page_pool;
  28. static void init_bounce_bioset(void)
  29. {
  30. static bool bounce_bs_setup;
  31. int ret;
  32. if (bounce_bs_setup)
  33. return;
  34. ret = bioset_init(&bounce_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
  35. BUG_ON(ret);
  36. ret = bioset_init(&bounce_bio_split, BIO_POOL_SIZE, 0, 0);
  37. BUG_ON(ret);
  38. bounce_bs_setup = true;
  39. }
  40. static __init int init_emergency_pool(void)
  41. {
  42. int ret;
  43. #ifndef CONFIG_MEMORY_HOTPLUG
  44. if (max_pfn <= max_low_pfn)
  45. return 0;
  46. #endif
  47. ret = mempool_init_page_pool(&page_pool, POOL_SIZE, 0);
  48. BUG_ON(ret);
  49. pr_info("pool size: %d pages\n", POOL_SIZE);
  50. init_bounce_bioset();
  51. return 0;
  52. }
  53. __initcall(init_emergency_pool);
  54. /*
  55. * Simple bounce buffer support for highmem pages. Depending on the
  56. * queue gfp mask set, *to may or may not be a highmem page. kmap it
  57. * always, it will do the Right Thing
  58. */
  59. static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
  60. {
  61. struct bio_vec tovec, fromvec;
  62. struct bvec_iter iter;
  63. /*
  64. * The bio of @from is created by bounce, so we can iterate
  65. * its bvec from start to end, but the @from->bi_iter can't be
  66. * trusted because it might be changed by splitting.
  67. */
  68. struct bvec_iter from_iter = BVEC_ITER_ALL_INIT;
  69. bio_for_each_segment(tovec, to, iter) {
  70. fromvec = bio_iter_iovec(from, from_iter);
  71. if (tovec.bv_page != fromvec.bv_page) {
  72. /*
  73. * fromvec->bv_offset and fromvec->bv_len might have
  74. * been modified by the block layer, so use the original
  75. * copy, bounce_copy_vec already uses tovec->bv_len
  76. */
  77. memcpy_to_bvec(&tovec, page_address(fromvec.bv_page) +
  78. tovec.bv_offset);
  79. }
  80. bio_advance_iter(from, &from_iter, tovec.bv_len);
  81. }
  82. }
  83. static void bounce_end_io(struct bio *bio)
  84. {
  85. struct bio *bio_orig = bio->bi_private;
  86. struct bio_vec *bvec, orig_vec;
  87. struct bvec_iter orig_iter = bio_orig->bi_iter;
  88. struct bvec_iter_all iter_all;
  89. /*
  90. * free up bounce indirect pages used
  91. */
  92. bio_for_each_segment_all(bvec, bio, iter_all) {
  93. orig_vec = bio_iter_iovec(bio_orig, orig_iter);
  94. if (bvec->bv_page != orig_vec.bv_page) {
  95. dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
  96. mempool_free(bvec->bv_page, &page_pool);
  97. }
  98. bio_advance_iter(bio_orig, &orig_iter, orig_vec.bv_len);
  99. }
  100. bio_orig->bi_status = bio->bi_status;
  101. bio_endio(bio_orig);
  102. bio_put(bio);
  103. }
  104. static void bounce_end_io_write(struct bio *bio)
  105. {
  106. bounce_end_io(bio);
  107. }
  108. static void bounce_end_io_read(struct bio *bio)
  109. {
  110. struct bio *bio_orig = bio->bi_private;
  111. if (!bio->bi_status)
  112. copy_to_high_bio_irq(bio_orig, bio);
  113. bounce_end_io(bio);
  114. }
  115. static struct bio *bounce_clone_bio(struct bio *bio_src)
  116. {
  117. struct bvec_iter iter;
  118. struct bio_vec bv;
  119. struct bio *bio;
  120. /*
  121. * Pre immutable biovecs, __bio_clone() used to just do a memcpy from
  122. * bio_src->bi_io_vec to bio->bi_io_vec.
  123. *
  124. * We can't do that anymore, because:
  125. *
  126. * - The point of cloning the biovec is to produce a bio with a biovec
  127. * the caller can modify: bi_idx and bi_bvec_done should be 0.
  128. *
  129. * - The original bio could've had more than BIO_MAX_VECS biovecs; if
  130. * we tried to clone the whole thing bio_alloc_bioset() would fail.
  131. * But the clone should succeed as long as the number of biovecs we
  132. * actually need to allocate is fewer than BIO_MAX_VECS.
  133. *
  134. * - Lastly, bi_vcnt should not be looked at or relied upon by code
  135. * that does not own the bio - reason being drivers don't use it for
  136. * iterating over the biovec anymore, so expecting it to be kept up
  137. * to date (i.e. for clones that share the parent biovec) is just
  138. * asking for trouble and would force extra work.
  139. */
  140. bio = bio_alloc_bioset(bio_src->bi_bdev, bio_segments(bio_src),
  141. bio_src->bi_opf, GFP_NOIO, &bounce_bio_set);
  142. if (bio_flagged(bio_src, BIO_REMAPPED))
  143. bio_set_flag(bio, BIO_REMAPPED);
  144. bio->bi_ioprio = bio_src->bi_ioprio;
  145. bio->bi_write_hint = bio_src->bi_write_hint;
  146. bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
  147. bio->bi_iter.bi_size = bio_src->bi_iter.bi_size;
  148. switch (bio_op(bio)) {
  149. case REQ_OP_DISCARD:
  150. case REQ_OP_SECURE_ERASE:
  151. case REQ_OP_WRITE_ZEROES:
  152. break;
  153. default:
  154. bio_for_each_segment(bv, bio_src, iter)
  155. bio->bi_io_vec[bio->bi_vcnt++] = bv;
  156. break;
  157. }
  158. if (bio_crypt_clone(bio, bio_src, GFP_NOIO) < 0)
  159. goto err_put;
  160. if (bio_integrity(bio_src) &&
  161. bio_integrity_clone(bio, bio_src, GFP_NOIO) < 0)
  162. goto err_put;
  163. bio_clone_blkg_association(bio, bio_src);
  164. return bio;
  165. err_put:
  166. bio_put(bio);
  167. return NULL;
  168. }
  169. struct bio *__blk_queue_bounce(struct bio *bio_orig, struct request_queue *q)
  170. {
  171. struct bio *bio;
  172. int rw = bio_data_dir(bio_orig);
  173. struct bio_vec *to, from;
  174. struct bvec_iter iter;
  175. unsigned i = 0, bytes = 0;
  176. bool bounce = false;
  177. int sectors;
  178. bio_for_each_segment(from, bio_orig, iter) {
  179. if (i++ < BIO_MAX_VECS)
  180. bytes += from.bv_len;
  181. if (PageHighMem(from.bv_page))
  182. bounce = true;
  183. }
  184. if (!bounce)
  185. return bio_orig;
  186. /*
  187. * Individual bvecs might not be logical block aligned. Round down
  188. * the split size so that each bio is properly block size aligned,
  189. * even if we do not use the full hardware limits.
  190. */
  191. sectors = ALIGN_DOWN(bytes, queue_logical_block_size(q)) >>
  192. SECTOR_SHIFT;
  193. if (sectors < bio_sectors(bio_orig)) {
  194. bio = bio_split(bio_orig, sectors, GFP_NOIO, &bounce_bio_split);
  195. bio_chain(bio, bio_orig);
  196. submit_bio_noacct(bio_orig);
  197. bio_orig = bio;
  198. }
  199. bio = bounce_clone_bio(bio_orig);
  200. /*
  201. * Bvec table can't be updated by bio_for_each_segment_all(),
  202. * so retrieve bvec from the table directly. This way is safe
  203. * because the 'bio' is single-page bvec.
  204. */
  205. for (i = 0, to = bio->bi_io_vec; i < bio->bi_vcnt; to++, i++) {
  206. struct page *bounce_page;
  207. if (!PageHighMem(to->bv_page))
  208. continue;
  209. bounce_page = mempool_alloc(&page_pool, GFP_NOIO);
  210. inc_zone_page_state(bounce_page, NR_BOUNCE);
  211. if (rw == WRITE) {
  212. flush_dcache_page(to->bv_page);
  213. memcpy_from_bvec(page_address(bounce_page), to);
  214. }
  215. to->bv_page = bounce_page;
  216. }
  217. trace_block_bio_bounce(bio_orig);
  218. bio->bi_flags |= (1 << BIO_BOUNCED);
  219. if (rw == READ)
  220. bio->bi_end_io = bounce_end_io_read;
  221. else
  222. bio->bi_end_io = bounce_end_io_write;
  223. bio->bi_private = bio_orig;
  224. return bio;
  225. }