blob: aea1a8e5d1954190c58b7dadb3a33347b450ac5a [file] [log] [blame]
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +01001/*
2 * Synchronous Compression operations
3 *
4 * Copyright 2015 LG Electronics Inc.
5 * Copyright (c) 2016, Intel Corporation
6 * Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 */
14#include <linux/errno.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/seq_file.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/crypto.h>
Gideon Israel Dsouzad8c34b92016-12-31 21:26:23 +053021#include <linux/compiler.h>
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010022#include <linux/vmalloc.h>
23#include <crypto/algapi.h>
24#include <linux/cryptouser.h>
25#include <net/netlink.h>
26#include <linux/scatterlist.h>
27#include <crypto/scatterwalk.h>
28#include <crypto/internal/acompress.h>
29#include <crypto/internal/scompress.h>
30#include "internal.h"
31
32static const struct crypto_type crypto_scomp_type;
33static void * __percpu *scomp_src_scratches;
34static void * __percpu *scomp_dst_scratches;
35static int scomp_scratch_users;
36static DEFINE_MUTEX(scomp_lock);
37
38#ifdef CONFIG_NET
39static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
40{
41 struct crypto_report_comp rscomp;
42
Eric Biggers37db69e2018-11-03 14:56:03 -070043 memset(&rscomp, 0, sizeof(rscomp));
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010044
Eric Biggers37db69e2018-11-03 14:56:03 -070045 strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010046
Eric Biggers37db69e2018-11-03 14:56:03 -070047 return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
48 sizeof(rscomp), &rscomp);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010049}
50#else
51static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
52{
53 return -ENOSYS;
54}
55#endif
56
57static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
Gideon Israel Dsouzad8c34b92016-12-31 21:26:23 +053058 __maybe_unused;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010059
60static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
61{
62 seq_puts(m, "type : scomp\n");
63}
64
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010065static void crypto_scomp_free_scratches(void * __percpu *scratches)
66{
67 int i;
68
69 if (!scratches)
70 return;
71
72 for_each_possible_cpu(i)
73 vfree(*per_cpu_ptr(scratches, i));
74
75 free_percpu(scratches);
76}
77
78static void * __percpu *crypto_scomp_alloc_scratches(void)
79{
80 void * __percpu *scratches;
81 int i;
82
83 scratches = alloc_percpu(void *);
84 if (!scratches)
85 return NULL;
86
87 for_each_possible_cpu(i) {
88 void *scratch;
89
90 scratch = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
91 if (!scratch)
92 goto error;
93 *per_cpu_ptr(scratches, i) = scratch;
94 }
95
96 return scratches;
97
98error:
99 crypto_scomp_free_scratches(scratches);
100 return NULL;
101}
102
103static void crypto_scomp_free_all_scratches(void)
104{
105 if (!--scomp_scratch_users) {
106 crypto_scomp_free_scratches(scomp_src_scratches);
107 crypto_scomp_free_scratches(scomp_dst_scratches);
108 scomp_src_scratches = NULL;
109 scomp_dst_scratches = NULL;
110 }
111}
112
113static int crypto_scomp_alloc_all_scratches(void)
114{
115 if (!scomp_scratch_users++) {
116 scomp_src_scratches = crypto_scomp_alloc_scratches();
117 if (!scomp_src_scratches)
118 return -ENOMEM;
119 scomp_dst_scratches = crypto_scomp_alloc_scratches();
Ard Biesheuvelcc4d1102017-07-21 16:42:37 +0100120 if (!scomp_dst_scratches) {
121 crypto_scomp_free_scratches(scomp_src_scratches);
122 scomp_src_scratches = NULL;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100123 return -ENOMEM;
Ard Biesheuvelcc4d1102017-07-21 16:42:37 +0100124 }
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100125 }
126 return 0;
127}
128
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100129static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
130{
131 int ret;
132
133 mutex_lock(&scomp_lock);
134 ret = crypto_scomp_alloc_all_scratches();
135 mutex_unlock(&scomp_lock);
136
137 return ret;
138}
139
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100140static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
141{
142 struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
143 void **tfm_ctx = acomp_tfm_ctx(tfm);
144 struct crypto_scomp *scomp = *tfm_ctx;
145 void **ctx = acomp_request_ctx(req);
146 const int cpu = get_cpu();
147 u8 *scratch_src = *per_cpu_ptr(scomp_src_scratches, cpu);
148 u8 *scratch_dst = *per_cpu_ptr(scomp_dst_scratches, cpu);
149 int ret;
150
151 if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE) {
152 ret = -EINVAL;
153 goto out;
154 }
155
156 if (req->dst && !req->dlen) {
157 ret = -EINVAL;
158 goto out;
159 }
160
161 if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
162 req->dlen = SCOMP_SCRATCH_SIZE;
163
164 scatterwalk_map_and_copy(scratch_src, req->src, 0, req->slen, 0);
165 if (dir)
166 ret = crypto_scomp_compress(scomp, scratch_src, req->slen,
167 scratch_dst, &req->dlen, *ctx);
168 else
169 ret = crypto_scomp_decompress(scomp, scratch_src, req->slen,
170 scratch_dst, &req->dlen, *ctx);
171 if (!ret) {
172 if (!req->dst) {
Bart Van Assche8cd579d2018-01-05 08:26:47 -0800173 req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
Sebastian Andrzej Siewior6a4d1b12019-03-29 14:09:55 +0100174 if (!req->dst) {
175 ret = -ENOMEM;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100176 goto out;
Sebastian Andrzej Siewior6a4d1b12019-03-29 14:09:55 +0100177 }
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100178 }
179 scatterwalk_map_and_copy(scratch_dst, req->dst, 0, req->dlen,
180 1);
181 }
182out:
183 put_cpu();
184 return ret;
185}
186
187static int scomp_acomp_compress(struct acomp_req *req)
188{
189 return scomp_acomp_comp_decomp(req, 1);
190}
191
192static int scomp_acomp_decompress(struct acomp_req *req)
193{
194 return scomp_acomp_comp_decomp(req, 0);
195}
196
197static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
198{
199 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
200
201 crypto_free_scomp(*ctx);
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100202
203 mutex_lock(&scomp_lock);
204 crypto_scomp_free_all_scratches();
205 mutex_unlock(&scomp_lock);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100206}
207
208int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
209{
210 struct crypto_alg *calg = tfm->__crt_alg;
211 struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
212 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
213 struct crypto_scomp *scomp;
214
215 if (!crypto_mod_get(calg))
216 return -EAGAIN;
217
218 scomp = crypto_create_tfm(calg, &crypto_scomp_type);
219 if (IS_ERR(scomp)) {
220 crypto_mod_put(calg);
221 return PTR_ERR(scomp);
222 }
223
224 *ctx = scomp;
225 tfm->exit = crypto_exit_scomp_ops_async;
226
227 crt->compress = scomp_acomp_compress;
228 crt->decompress = scomp_acomp_decompress;
Bart Van Assche8cd579d2018-01-05 08:26:47 -0800229 crt->dst_free = sgl_free;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100230 crt->reqsize = sizeof(void *);
231
232 return 0;
233}
234
235struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
236{
237 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
238 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
239 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
240 struct crypto_scomp *scomp = *tfm_ctx;
241 void *ctx;
242
243 ctx = crypto_scomp_alloc_ctx(scomp);
244 if (IS_ERR(ctx)) {
245 kfree(req);
246 return NULL;
247 }
248
249 *req->__ctx = ctx;
250
251 return req;
252}
253
254void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
255{
256 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
257 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
258 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
259 struct crypto_scomp *scomp = *tfm_ctx;
260 void *ctx = *req->__ctx;
261
262 if (ctx)
263 crypto_scomp_free_ctx(scomp, ctx);
264}
265
266static const struct crypto_type crypto_scomp_type = {
267 .extsize = crypto_alg_extsize,
268 .init_tfm = crypto_scomp_init_tfm,
269#ifdef CONFIG_PROC_FS
270 .show = crypto_scomp_show,
271#endif
272 .report = crypto_scomp_report,
273 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
274 .maskset = CRYPTO_ALG_TYPE_MASK,
275 .type = CRYPTO_ALG_TYPE_SCOMPRESS,
276 .tfmsize = offsetof(struct crypto_scomp, base),
277};
278
279int crypto_register_scomp(struct scomp_alg *alg)
280{
281 struct crypto_alg *base = &alg->base;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100282
283 base->cra_type = &crypto_scomp_type;
284 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
285 base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
286
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100287 return crypto_register_alg(base);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100288}
289EXPORT_SYMBOL_GPL(crypto_register_scomp);
290
291int crypto_unregister_scomp(struct scomp_alg *alg)
292{
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100293 return crypto_unregister_alg(&alg->base);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100294}
295EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
296
Giovanni Cabiddu3de4f5e2017-04-21 21:54:29 +0100297int crypto_register_scomps(struct scomp_alg *algs, int count)
298{
299 int i, ret;
300
301 for (i = 0; i < count; i++) {
302 ret = crypto_register_scomp(&algs[i]);
303 if (ret)
304 goto err;
305 }
306
307 return 0;
308
309err:
310 for (--i; i >= 0; --i)
311 crypto_unregister_scomp(&algs[i]);
312
313 return ret;
314}
315EXPORT_SYMBOL_GPL(crypto_register_scomps);
316
317void crypto_unregister_scomps(struct scomp_alg *algs, int count)
318{
319 int i;
320
321 for (i = count - 1; i >= 0; --i)
322 crypto_unregister_scomp(&algs[i]);
323}
324EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
325
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100326MODULE_LICENSE("GPL");
327MODULE_DESCRIPTION("Synchronous compression type");