blk-map.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions related to mapping data to requests
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/sched/task_stack.h>
  7. #include <linux/module.h>
  8. #include <linux/bio.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/uio.h>
  11. #include "blk.h"
  12. /*
  13. * Append a bio to a passthrough request. Only works if the bio can be merged
  14. * into the request based on the driver constraints.
  15. */
  16. int blk_rq_append_bio(struct request *rq, struct bio **bio)
  17. {
  18. struct bio *orig_bio = *bio;
  19. blk_queue_bounce(rq->q, bio);
  20. if (!rq->bio) {
  21. blk_rq_bio_prep(rq->q, rq, *bio);
  22. } else {
  23. if (!ll_back_merge_fn(rq->q, rq, *bio)) {
  24. if (orig_bio != *bio) {
  25. bio_put(*bio);
  26. *bio = orig_bio;
  27. }
  28. return -EINVAL;
  29. }
  30. rq->biotail->bi_next = *bio;
  31. rq->biotail = *bio;
  32. rq->__data_len += (*bio)->bi_iter.bi_size;
  33. }
  34. return 0;
  35. }
  36. EXPORT_SYMBOL(blk_rq_append_bio);
  37. static int __blk_rq_unmap_user(struct bio *bio)
  38. {
  39. int ret = 0;
  40. if (bio) {
  41. if (bio_flagged(bio, BIO_USER_MAPPED))
  42. bio_unmap_user(bio);
  43. else
  44. ret = bio_uncopy_user(bio);
  45. }
  46. return ret;
  47. }
  48. static int __blk_rq_map_user_iov(struct request *rq,
  49. struct rq_map_data *map_data, struct iov_iter *iter,
  50. gfp_t gfp_mask, bool copy)
  51. {
  52. struct request_queue *q = rq->q;
  53. struct bio *bio, *orig_bio;
  54. int ret;
  55. if (copy)
  56. bio = bio_copy_user_iov(q, map_data, iter, gfp_mask);
  57. else
  58. bio = bio_map_user_iov(q, iter, gfp_mask);
  59. if (IS_ERR(bio))
  60. return PTR_ERR(bio);
  61. bio->bi_opf &= ~REQ_OP_MASK;
  62. bio->bi_opf |= req_op(rq);
  63. orig_bio = bio;
  64. /*
  65. * We link the bounce buffer in and could have to traverse it
  66. * later so we have to get a ref to prevent it from being freed
  67. */
  68. ret = blk_rq_append_bio(rq, &bio);
  69. if (ret) {
  70. __blk_rq_unmap_user(orig_bio);
  71. return ret;
  72. }
  73. bio_get(bio);
  74. return 0;
  75. }
  76. /**
  77. * blk_rq_map_user_iov - map user data to a request, for passthrough requests
  78. * @q: request queue where request should be inserted
  79. * @rq: request to map data to
  80. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  81. * @iter: iovec iterator
  82. * @gfp_mask: memory allocation flags
  83. *
  84. * Description:
  85. * Data will be mapped directly for zero copy I/O, if possible. Otherwise
  86. * a kernel bounce buffer is used.
  87. *
  88. * A matching blk_rq_unmap_user() must be issued at the end of I/O, while
  89. * still in process context.
  90. *
  91. * Note: The mapped bio may need to be bounced through blk_queue_bounce()
  92. * before being submitted to the device, as pages mapped may be out of
  93. * reach. It's the callers responsibility to make sure this happens. The
  94. * original bio must be passed back in to blk_rq_unmap_user() for proper
  95. * unmapping.
  96. */
  97. int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
  98. struct rq_map_data *map_data,
  99. const struct iov_iter *iter, gfp_t gfp_mask)
  100. {
  101. bool copy = false;
  102. unsigned long align = q->dma_pad_mask | queue_dma_alignment(q);
  103. struct bio *bio = NULL;
  104. struct iov_iter i;
  105. int ret = -EINVAL;
  106. if (!iter_is_iovec(iter))
  107. goto fail;
  108. if (map_data)
  109. copy = true;
  110. else if (iov_iter_alignment(iter) & align)
  111. copy = true;
  112. else if (queue_virt_boundary(q))
  113. copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter);
  114. i = *iter;
  115. do {
  116. ret =__blk_rq_map_user_iov(rq, map_data, &i, gfp_mask, copy);
  117. if (ret)
  118. goto unmap_rq;
  119. if (!bio)
  120. bio = rq->bio;
  121. } while (iov_iter_count(&i));
  122. if (!bio_flagged(bio, BIO_USER_MAPPED))
  123. rq->rq_flags |= RQF_COPY_USER;
  124. return 0;
  125. unmap_rq:
  126. blk_rq_unmap_user(bio);
  127. fail:
  128. rq->bio = NULL;
  129. return ret;
  130. }
  131. EXPORT_SYMBOL(blk_rq_map_user_iov);
  132. int blk_rq_map_user(struct request_queue *q, struct request *rq,
  133. struct rq_map_data *map_data, void __user *ubuf,
  134. unsigned long len, gfp_t gfp_mask)
  135. {
  136. struct iovec iov;
  137. struct iov_iter i;
  138. int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
  139. if (unlikely(ret < 0))
  140. return ret;
  141. return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
  142. }
  143. EXPORT_SYMBOL(blk_rq_map_user);
  144. /**
  145. * blk_rq_unmap_user - unmap a request with user data
  146. * @bio: start of bio list
  147. *
  148. * Description:
  149. * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
  150. * supply the original rq->bio from the blk_rq_map_user() return, since
  151. * the I/O completion may have changed rq->bio.
  152. */
  153. int blk_rq_unmap_user(struct bio *bio)
  154. {
  155. struct bio *mapped_bio;
  156. int ret = 0, ret2;
  157. while (bio) {
  158. mapped_bio = bio;
  159. if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
  160. mapped_bio = bio->bi_private;
  161. ret2 = __blk_rq_unmap_user(mapped_bio);
  162. if (ret2 && !ret)
  163. ret = ret2;
  164. mapped_bio = bio;
  165. bio = bio->bi_next;
  166. bio_put(mapped_bio);
  167. }
  168. return ret;
  169. }
  170. EXPORT_SYMBOL(blk_rq_unmap_user);
  171. /**
  172. * blk_rq_map_kern - map kernel data to a request, for passthrough requests
  173. * @q: request queue where request should be inserted
  174. * @rq: request to fill
  175. * @kbuf: the kernel buffer
  176. * @len: length of user data
  177. * @gfp_mask: memory allocation flags
  178. *
  179. * Description:
  180. * Data will be mapped directly if possible. Otherwise a bounce
  181. * buffer is used. Can be called multiple times to append multiple
  182. * buffers.
  183. */
  184. int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
  185. unsigned int len, gfp_t gfp_mask)
  186. {
  187. int reading = rq_data_dir(rq) == READ;
  188. unsigned long addr = (unsigned long) kbuf;
  189. int do_copy = 0;
  190. struct bio *bio, *orig_bio;
  191. int ret;
  192. if (len > (queue_max_hw_sectors(q) << 9))
  193. return -EINVAL;
  194. if (!len || !kbuf)
  195. return -EINVAL;
  196. do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
  197. if (do_copy)
  198. bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
  199. else
  200. bio = bio_map_kern(q, kbuf, len, gfp_mask);
  201. if (IS_ERR(bio))
  202. return PTR_ERR(bio);
  203. bio->bi_opf &= ~REQ_OP_MASK;
  204. bio->bi_opf |= req_op(rq);
  205. if (do_copy)
  206. rq->rq_flags |= RQF_COPY_USER;
  207. orig_bio = bio;
  208. ret = blk_rq_append_bio(rq, &bio);
  209. if (unlikely(ret)) {
  210. /* request is too big */
  211. bio_put(orig_bio);
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. EXPORT_SYMBOL(blk_rq_map_kern);