Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1ab53a77 GC |
2 | /* |
3 | * Synchronous Compression operations | |
4 | * | |
5 | * Copyright 2015 LG Electronics Inc. | |
6 | * Copyright (c) 2016, Intel Corporation | |
7 | * Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com> | |
1ab53a77 | 8 | */ |
0a742389 HX |
9 | |
10 | #include <crypto/internal/acompress.h> | |
11 | #include <crypto/internal/scompress.h> | |
12 | #include <crypto/scatterwalk.h> | |
13 | #include <linux/cryptouser.h> | |
14 | #include <linux/err.h> | |
1ab53a77 GC |
15 | #include <linux/kernel.h> |
16 | #include <linux/module.h> | |
0a742389 | 17 | #include <linux/scatterlist.h> |
1ab53a77 GC |
18 | #include <linux/seq_file.h> |
19 | #include <linux/slab.h> | |
20 | #include <linux/string.h> | |
1ab53a77 | 21 | #include <linux/vmalloc.h> |
1ab53a77 | 22 | #include <net/netlink.h> |
0a742389 HX |
23 | |
24 | #include "compress.h" | |
1ab53a77 | 25 | |
71052dcf SAS |
26 | struct scomp_scratch { |
27 | spinlock_t lock; | |
28 | void *src; | |
29 | void *dst; | |
30 | }; | |
31 | ||
32 | static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = { | |
33 | .lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock), | |
34 | }; | |
35 | ||
1ab53a77 | 36 | static const struct crypto_type crypto_scomp_type; |
1ab53a77 GC |
37 | static int scomp_scratch_users; |
38 | static DEFINE_MUTEX(scomp_lock); | |
39 | ||
c0f9e01d HX |
40 | static int __maybe_unused crypto_scomp_report( |
41 | struct sk_buff *skb, struct crypto_alg *alg) | |
1ab53a77 GC |
42 | { |
43 | struct crypto_report_comp rscomp; | |
44 | ||
37db69e0 | 45 | memset(&rscomp, 0, sizeof(rscomp)); |
1ab53a77 | 46 | |
37db69e0 | 47 | strscpy(rscomp.type, "scomp", sizeof(rscomp.type)); |
1ab53a77 | 48 | |
37db69e0 EB |
49 | return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS, |
50 | sizeof(rscomp), &rscomp); | |
1ab53a77 | 51 | } |
1ab53a77 GC |
52 | |
53 | static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg) | |
d8c34b94 | 54 | __maybe_unused; |
1ab53a77 GC |
55 | |
56 | static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg) | |
57 | { | |
58 | seq_puts(m, "type : scomp\n"); | |
59 | } | |
60 | ||
71052dcf | 61 | static void crypto_scomp_free_scratches(void) |
1ab53a77 | 62 | { |
71052dcf | 63 | struct scomp_scratch *scratch; |
1ab53a77 GC |
64 | int i; |
65 | ||
71052dcf | 66 | for_each_possible_cpu(i) { |
8c3fffe3 | 67 | scratch = per_cpu_ptr(&scomp_scratch, i); |
1ab53a77 | 68 | |
71052dcf SAS |
69 | vfree(scratch->src); |
70 | vfree(scratch->dst); | |
71 | scratch->src = NULL; | |
72 | scratch->dst = NULL; | |
73 | } | |
1ab53a77 GC |
74 | } |
75 | ||
71052dcf | 76 | static int crypto_scomp_alloc_scratches(void) |
1ab53a77 | 77 | { |
71052dcf | 78 | struct scomp_scratch *scratch; |
1ab53a77 GC |
79 | int i; |
80 | ||
1ab53a77 | 81 | for_each_possible_cpu(i) { |
71052dcf | 82 | void *mem; |
1ab53a77 | 83 | |
8c3fffe3 | 84 | scratch = per_cpu_ptr(&scomp_scratch, i); |
1ab53a77 | 85 | |
71052dcf SAS |
86 | mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i)); |
87 | if (!mem) | |
88 | goto error; | |
89 | scratch->src = mem; | |
90 | mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i)); | |
91 | if (!mem) | |
92 | goto error; | |
93 | scratch->dst = mem; | |
1ab53a77 GC |
94 | } |
95 | return 0; | |
71052dcf SAS |
96 | error: |
97 | crypto_scomp_free_scratches(); | |
98 | return -ENOMEM; | |
1ab53a77 GC |
99 | } |
100 | ||
6a8487a1 AB |
101 | static int crypto_scomp_init_tfm(struct crypto_tfm *tfm) |
102 | { | |
71052dcf | 103 | int ret = 0; |
6a8487a1 AB |
104 | |
105 | mutex_lock(&scomp_lock); | |
71052dcf SAS |
106 | if (!scomp_scratch_users++) |
107 | ret = crypto_scomp_alloc_scratches(); | |
6a8487a1 AB |
108 | mutex_unlock(&scomp_lock); |
109 | ||
110 | return ret; | |
111 | } | |
112 | ||
1ab53a77 GC |
113 | static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir) |
114 | { | |
115 | struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); | |
116 | void **tfm_ctx = acomp_tfm_ctx(tfm); | |
117 | struct crypto_scomp *scomp = *tfm_ctx; | |
118 | void **ctx = acomp_request_ctx(req); | |
71052dcf | 119 | struct scomp_scratch *scratch; |
77292bb8 | 120 | void *src, *dst; |
744e1885 | 121 | unsigned int dlen; |
1ab53a77 GC |
122 | int ret; |
123 | ||
71052dcf SAS |
124 | if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE) |
125 | return -EINVAL; | |
1ab53a77 | 126 | |
71052dcf SAS |
127 | if (req->dst && !req->dlen) |
128 | return -EINVAL; | |
1ab53a77 GC |
129 | |
130 | if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE) | |
131 | req->dlen = SCOMP_SCRATCH_SIZE; | |
132 | ||
744e1885 CZ |
133 | dlen = req->dlen; |
134 | ||
71052dcf SAS |
135 | scratch = raw_cpu_ptr(&scomp_scratch); |
136 | spin_lock(&scratch->lock); | |
137 | ||
77292bb8 BS |
138 | if (sg_nents(req->src) == 1 && !PageHighMem(sg_page(req->src))) { |
139 | src = page_to_virt(sg_page(req->src)) + req->src->offset; | |
140 | } else { | |
141 | scatterwalk_map_and_copy(scratch->src, req->src, 0, | |
142 | req->slen, 0); | |
143 | src = scratch->src; | |
144 | } | |
145 | ||
146 | if (req->dst && sg_nents(req->dst) == 1 && !PageHighMem(sg_page(req->dst))) | |
147 | dst = page_to_virt(sg_page(req->dst)) + req->dst->offset; | |
148 | else | |
149 | dst = scratch->dst; | |
150 | ||
1ab53a77 | 151 | if (dir) |
77292bb8 BS |
152 | ret = crypto_scomp_compress(scomp, src, req->slen, |
153 | dst, &req->dlen, *ctx); | |
1ab53a77 | 154 | else |
77292bb8 BS |
155 | ret = crypto_scomp_decompress(scomp, src, req->slen, |
156 | dst, &req->dlen, *ctx); | |
1ab53a77 GC |
157 | if (!ret) { |
158 | if (!req->dst) { | |
8cd579d2 | 159 | req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL); |
6a4d1b18 SAS |
160 | if (!req->dst) { |
161 | ret = -ENOMEM; | |
1ab53a77 | 162 | goto out; |
6a4d1b18 | 163 | } |
744e1885 CZ |
164 | } else if (req->dlen > dlen) { |
165 | ret = -ENOSPC; | |
166 | goto out; | |
1ab53a77 | 167 | } |
77292bb8 BS |
168 | if (dst == scratch->dst) { |
169 | scatterwalk_map_and_copy(scratch->dst, req->dst, 0, | |
170 | req->dlen, 1); | |
171 | } else { | |
172 | int nr_pages = DIV_ROUND_UP(req->dst->offset + req->dlen, PAGE_SIZE); | |
173 | int i; | |
174 | struct page *dst_page = sg_page(req->dst); | |
175 | ||
176 | for (i = 0; i < nr_pages; i++) | |
177 | flush_dcache_page(dst_page + i); | |
178 | } | |
1ab53a77 GC |
179 | } |
180 | out: | |
71052dcf | 181 | spin_unlock(&scratch->lock); |
1ab53a77 GC |
182 | return ret; |
183 | } | |
184 | ||
185 | static int scomp_acomp_compress(struct acomp_req *req) | |
186 | { | |
187 | return scomp_acomp_comp_decomp(req, 1); | |
188 | } | |
189 | ||
190 | static int scomp_acomp_decompress(struct acomp_req *req) | |
191 | { | |
192 | return scomp_acomp_comp_decomp(req, 0); | |
193 | } | |
194 | ||
195 | static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm) | |
196 | { | |
197 | struct crypto_scomp **ctx = crypto_tfm_ctx(tfm); | |
198 | ||
199 | crypto_free_scomp(*ctx); | |
6a8487a1 AB |
200 | |
201 | mutex_lock(&scomp_lock); | |
71052dcf SAS |
202 | if (!--scomp_scratch_users) |
203 | crypto_scomp_free_scratches(); | |
6a8487a1 | 204 | mutex_unlock(&scomp_lock); |
1ab53a77 GC |
205 | } |
206 | ||
207 | int crypto_init_scomp_ops_async(struct crypto_tfm *tfm) | |
208 | { | |
209 | struct crypto_alg *calg = tfm->__crt_alg; | |
210 | struct crypto_acomp *crt = __crypto_acomp_tfm(tfm); | |
211 | struct crypto_scomp **ctx = crypto_tfm_ctx(tfm); | |
212 | struct crypto_scomp *scomp; | |
213 | ||
214 | if (!crypto_mod_get(calg)) | |
215 | return -EAGAIN; | |
216 | ||
217 | scomp = crypto_create_tfm(calg, &crypto_scomp_type); | |
218 | if (IS_ERR(scomp)) { | |
219 | crypto_mod_put(calg); | |
220 | return PTR_ERR(scomp); | |
221 | } | |
222 | ||
223 | *ctx = scomp; | |
224 | tfm->exit = crypto_exit_scomp_ops_async; | |
225 | ||
226 | crt->compress = scomp_acomp_compress; | |
227 | crt->decompress = scomp_acomp_decompress; | |
8cd579d2 | 228 | crt->dst_free = sgl_free; |
1ab53a77 GC |
229 | crt->reqsize = sizeof(void *); |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req) | |
235 | { | |
236 | struct crypto_acomp *acomp = crypto_acomp_reqtfm(req); | |
237 | struct crypto_tfm *tfm = crypto_acomp_tfm(acomp); | |
238 | struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm); | |
239 | struct crypto_scomp *scomp = *tfm_ctx; | |
240 | void *ctx; | |
241 | ||
242 | ctx = crypto_scomp_alloc_ctx(scomp); | |
243 | if (IS_ERR(ctx)) { | |
244 | kfree(req); | |
245 | return NULL; | |
246 | } | |
247 | ||
248 | *req->__ctx = ctx; | |
249 | ||
250 | return req; | |
251 | } | |
252 | ||
253 | void crypto_acomp_scomp_free_ctx(struct acomp_req *req) | |
254 | { | |
255 | struct crypto_acomp *acomp = crypto_acomp_reqtfm(req); | |
256 | struct crypto_tfm *tfm = crypto_acomp_tfm(acomp); | |
257 | struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm); | |
258 | struct crypto_scomp *scomp = *tfm_ctx; | |
259 | void *ctx = *req->__ctx; | |
260 | ||
261 | if (ctx) | |
262 | crypto_scomp_free_ctx(scomp, ctx); | |
263 | } | |
264 | ||
265 | static const struct crypto_type crypto_scomp_type = { | |
266 | .extsize = crypto_alg_extsize, | |
267 | .init_tfm = crypto_scomp_init_tfm, | |
268 | #ifdef CONFIG_PROC_FS | |
269 | .show = crypto_scomp_show, | |
270 | #endif | |
b8969a1b | 271 | #if IS_ENABLED(CONFIG_CRYPTO_USER) |
1ab53a77 | 272 | .report = crypto_scomp_report, |
6a8dbd71 HX |
273 | #endif |
274 | #ifdef CONFIG_CRYPTO_STATS | |
275 | .report_stat = crypto_acomp_report_stat, | |
0a742389 | 276 | #endif |
1ab53a77 GC |
277 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
278 | .maskset = CRYPTO_ALG_TYPE_MASK, | |
279 | .type = CRYPTO_ALG_TYPE_SCOMPRESS, | |
280 | .tfmsize = offsetof(struct crypto_scomp, base), | |
281 | }; | |
282 | ||
283 | int crypto_register_scomp(struct scomp_alg *alg) | |
284 | { | |
6a8dbd71 HX |
285 | struct crypto_alg *base = &alg->calg.base; |
286 | ||
287 | comp_prepare_alg(&alg->calg); | |
1ab53a77 GC |
288 | |
289 | base->cra_type = &crypto_scomp_type; | |
1ab53a77 GC |
290 | base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS; |
291 | ||
6a8487a1 | 292 | return crypto_register_alg(base); |
1ab53a77 GC |
293 | } |
294 | EXPORT_SYMBOL_GPL(crypto_register_scomp); | |
295 | ||
c6d633a9 | 296 | void crypto_unregister_scomp(struct scomp_alg *alg) |
1ab53a77 | 297 | { |
c6d633a9 | 298 | crypto_unregister_alg(&alg->base); |
1ab53a77 GC |
299 | } |
300 | EXPORT_SYMBOL_GPL(crypto_unregister_scomp); | |
301 | ||
3de4f5e1 GC |
302 | int crypto_register_scomps(struct scomp_alg *algs, int count) |
303 | { | |
304 | int i, ret; | |
305 | ||
306 | for (i = 0; i < count; i++) { | |
307 | ret = crypto_register_scomp(&algs[i]); | |
308 | if (ret) | |
309 | goto err; | |
310 | } | |
311 | ||
312 | return 0; | |
313 | ||
314 | err: | |
315 | for (--i; i >= 0; --i) | |
316 | crypto_unregister_scomp(&algs[i]); | |
317 | ||
318 | return ret; | |
319 | } | |
320 | EXPORT_SYMBOL_GPL(crypto_register_scomps); | |
321 | ||
322 | void crypto_unregister_scomps(struct scomp_alg *algs, int count) | |
323 | { | |
324 | int i; | |
325 | ||
326 | for (i = count - 1; i >= 0; --i) | |
327 | crypto_unregister_scomp(&algs[i]); | |
328 | } | |
329 | EXPORT_SYMBOL_GPL(crypto_unregister_scomps); | |
330 | ||
1ab53a77 GC |
331 | MODULE_LICENSE("GPL"); |
332 | MODULE_DESCRIPTION("Synchronous compression type"); |