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