scatterwalk.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Cryptographic scatter and gather helpers.
  4. *
  5. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  6. * Copyright (c) 2002 Adam J. Richter <adam@yggdrasil.com>
  7. * Copyright (c) 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
  8. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  9. */
  10. #ifndef _CRYPTO_SCATTERWALK_H
  11. #define _CRYPTO_SCATTERWALK_H
  12. #include <crypto/algapi.h>
  13. #include <linux/highmem.h>
  14. #include <linux/mm.h>
  15. #include <linux/scatterlist.h>
  16. static inline void scatterwalk_crypto_chain(struct scatterlist *head,
  17. struct scatterlist *sg, int num)
  18. {
  19. if (sg)
  20. sg_chain(head, num, sg);
  21. else
  22. sg_mark_end(head);
  23. }
  24. static inline void scatterwalk_start(struct scatter_walk *walk,
  25. struct scatterlist *sg)
  26. {
  27. walk->sg = sg;
  28. walk->offset = sg->offset;
  29. }
  30. /*
  31. * This is equivalent to scatterwalk_start(walk, sg) followed by
  32. * scatterwalk_skip(walk, pos).
  33. */
  34. static inline void scatterwalk_start_at_pos(struct scatter_walk *walk,
  35. struct scatterlist *sg,
  36. unsigned int pos)
  37. {
  38. while (pos > sg->length) {
  39. pos -= sg->length;
  40. sg = sg_next(sg);
  41. }
  42. walk->sg = sg;
  43. walk->offset = sg->offset + pos;
  44. }
  45. static inline unsigned int scatterwalk_clamp(struct scatter_walk *walk,
  46. unsigned int nbytes)
  47. {
  48. unsigned int len_this_sg;
  49. unsigned int limit;
  50. if (walk->offset >= walk->sg->offset + walk->sg->length)
  51. scatterwalk_start(walk, sg_next(walk->sg));
  52. len_this_sg = walk->sg->offset + walk->sg->length - walk->offset;
  53. /*
  54. * HIGHMEM case: the page may have to be mapped into memory. To avoid
  55. * the complexity of having to map multiple pages at once per sg entry,
  56. * clamp the returned length to not cross a page boundary.
  57. *
  58. * !HIGHMEM case: no mapping is needed; all pages of the sg entry are
  59. * already mapped contiguously in the kernel's direct map. For improved
  60. * performance, allow the walker to return data segments that cross a
  61. * page boundary. Do still cap the length to PAGE_SIZE, since some
  62. * users rely on that to avoid disabling preemption for too long when
  63. * using SIMD. It's also needed for when skcipher_walk uses a bounce
  64. * page due to the data not being aligned to the algorithm's alignmask.
  65. */
  66. if (IS_ENABLED(CONFIG_HIGHMEM))
  67. limit = PAGE_SIZE - offset_in_page(walk->offset);
  68. else
  69. limit = PAGE_SIZE;
  70. return min3(nbytes, len_this_sg, limit);
  71. }
  72. /*
  73. * Create a scatterlist that represents the remaining data in a walk. Uses
  74. * chaining to reference the original scatterlist, so this uses at most two
  75. * entries in @sg_out regardless of the number of entries in the original list.
  76. * Assumes that sg_init_table() was already done.
  77. */
  78. static inline void scatterwalk_get_sglist(struct scatter_walk *walk,
  79. struct scatterlist sg_out[2])
  80. {
  81. if (walk->offset >= walk->sg->offset + walk->sg->length)
  82. scatterwalk_start(walk, sg_next(walk->sg));
  83. sg_set_page(sg_out, sg_page(walk->sg),
  84. walk->sg->offset + walk->sg->length - walk->offset,
  85. walk->offset);
  86. scatterwalk_crypto_chain(sg_out, sg_next(walk->sg), 2);
  87. }
  88. static inline void scatterwalk_map(struct scatter_walk *walk)
  89. {
  90. struct page *base_page = sg_page(walk->sg);
  91. unsigned int offset = walk->offset;
  92. void *addr;
  93. if (IS_ENABLED(CONFIG_HIGHMEM)) {
  94. struct page *page;
  95. page = base_page + (offset >> PAGE_SHIFT);
  96. offset = offset_in_page(offset);
  97. addr = kmap_local_page(page) + offset;
  98. } else {
  99. /*
  100. * When !HIGHMEM we allow the walker to return segments that
  101. * span a page boundary; see scatterwalk_clamp(). To make it
  102. * clear that in this case we're working in the linear buffer of
  103. * the whole sg entry in the kernel's direct map rather than
  104. * within the mapped buffer of a single page, compute the
  105. * address as an offset from the page_address() of the first
  106. * page of the sg entry. Either way the result is the address
  107. * in the direct map, but this makes it clearer what is really
  108. * going on.
  109. */
  110. addr = page_address(base_page) + offset;
  111. }
  112. walk->__addr = addr;
  113. }
  114. /**
  115. * scatterwalk_next() - Get the next data buffer in a scatterlist walk
  116. * @walk: the scatter_walk
  117. * @total: the total number of bytes remaining, > 0
  118. *
  119. * A virtual address for the next segment of data from the scatterlist will
  120. * be placed into @walk->addr. The caller must call scatterwalk_done_src()
  121. * or scatterwalk_done_dst() when it is done using this virtual address.
  122. *
  123. * Returns: the next number of bytes available, <= @total
  124. */
  125. static inline unsigned int scatterwalk_next(struct scatter_walk *walk,
  126. unsigned int total)
  127. {
  128. unsigned int nbytes = scatterwalk_clamp(walk, total);
  129. scatterwalk_map(walk);
  130. return nbytes;
  131. }
  132. static inline void scatterwalk_unmap(struct scatter_walk *walk)
  133. {
  134. if (IS_ENABLED(CONFIG_HIGHMEM))
  135. kunmap_local(walk->__addr);
  136. }
  137. static inline void scatterwalk_advance(struct scatter_walk *walk,
  138. unsigned int nbytes)
  139. {
  140. walk->offset += nbytes;
  141. }
  142. /**
  143. * scatterwalk_done_src() - Finish one step of a walk of source scatterlist
  144. * @walk: the scatter_walk
  145. * @nbytes: the number of bytes processed this step, less than or equal to the
  146. * number of bytes that scatterwalk_next() returned.
  147. *
  148. * Use this if the mapped address was not written to, i.e. it is source data.
  149. */
  150. static inline void scatterwalk_done_src(struct scatter_walk *walk,
  151. unsigned int nbytes)
  152. {
  153. scatterwalk_unmap(walk);
  154. scatterwalk_advance(walk, nbytes);
  155. }
  156. /*
  157. * Flush the dcache of any pages that overlap the region
  158. * [offset, offset + nbytes) relative to base_page.
  159. *
  160. * This should be called only when ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE, to ensure
  161. * that all relevant code (including the call to sg_page() in the caller, if
  162. * applicable) gets fully optimized out when !ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE.
  163. */
  164. static inline void __scatterwalk_flush_dcache_pages(struct page *base_page,
  165. unsigned int offset,
  166. unsigned int nbytes)
  167. {
  168. unsigned int num_pages;
  169. base_page += offset / PAGE_SIZE;
  170. offset %= PAGE_SIZE;
  171. /*
  172. * This is an overflow-safe version of
  173. * num_pages = DIV_ROUND_UP(offset + nbytes, PAGE_SIZE).
  174. */
  175. num_pages = nbytes / PAGE_SIZE;
  176. num_pages += DIV_ROUND_UP(offset + (nbytes % PAGE_SIZE), PAGE_SIZE);
  177. for (unsigned int i = 0; i < num_pages; i++)
  178. flush_dcache_page(base_page + i);
  179. }
  180. /**
  181. * scatterwalk_done_dst() - Finish one step of a walk of destination scatterlist
  182. * @walk: the scatter_walk
  183. * @nbytes: the number of bytes processed this step, less than or equal to the
  184. * number of bytes that scatterwalk_next() returned.
  185. *
  186. * Use this if the mapped address may have been written to, i.e. it is
  187. * destination data.
  188. */
  189. static inline void scatterwalk_done_dst(struct scatter_walk *walk,
  190. unsigned int nbytes)
  191. {
  192. scatterwalk_unmap(walk);
  193. if (ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE)
  194. __scatterwalk_flush_dcache_pages(sg_page(walk->sg),
  195. walk->offset, nbytes);
  196. scatterwalk_advance(walk, nbytes);
  197. }
  198. void scatterwalk_skip(struct scatter_walk *walk, unsigned int nbytes);
  199. void memcpy_from_scatterwalk(void *buf, struct scatter_walk *walk,
  200. unsigned int nbytes);
  201. void memcpy_to_scatterwalk(struct scatter_walk *walk, const void *buf,
  202. unsigned int nbytes);
  203. void memcpy_from_sglist(void *buf, struct scatterlist *sg,
  204. unsigned int start, unsigned int nbytes);
  205. void memcpy_to_sglist(struct scatterlist *sg, unsigned int start,
  206. const void *buf, unsigned int nbytes);
  207. void memcpy_sglist(struct scatterlist *dst, struct scatterlist *src,
  208. unsigned int nbytes);
  209. /* In new code, please use memcpy_{from,to}_sglist() directly instead. */
  210. static inline void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
  211. unsigned int start,
  212. unsigned int nbytes, int out)
  213. {
  214. if (out)
  215. memcpy_to_sglist(sg, start, buf, nbytes);
  216. else
  217. memcpy_from_sglist(buf, sg, start, nbytes);
  218. }
  219. struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
  220. struct scatterlist *src,
  221. unsigned int len);
  222. #endif /* _CRYPTO_SCATTERWALK_H */