crypto: scompress - free partially allocated scratch buffers on failure
[linux-block.git] / crypto / scompress.c
CommitLineData
1ab53a77
GC
1/*
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>
d8c34b94 21#include <linux/compiler.h>
1ab53a77
GC
22#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
43 strncpy(rscomp.type, "scomp", sizeof(rscomp.type));
44
45 if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
46 sizeof(struct crypto_report_comp), &rscomp))
47 goto nla_put_failure;
48 return 0;
49
50nla_put_failure:
51 return -EMSGSIZE;
52}
53#else
54static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
55{
56 return -ENOSYS;
57}
58#endif
59
60static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
d8c34b94 61 __maybe_unused;
1ab53a77
GC
62
63static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
64{
65 seq_puts(m, "type : scomp\n");
66}
67
68static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
69{
70 return 0;
71}
72
73static void crypto_scomp_free_scratches(void * __percpu *scratches)
74{
75 int i;
76
77 if (!scratches)
78 return;
79
80 for_each_possible_cpu(i)
81 vfree(*per_cpu_ptr(scratches, i));
82
83 free_percpu(scratches);
84}
85
86static void * __percpu *crypto_scomp_alloc_scratches(void)
87{
88 void * __percpu *scratches;
89 int i;
90
91 scratches = alloc_percpu(void *);
92 if (!scratches)
93 return NULL;
94
95 for_each_possible_cpu(i) {
96 void *scratch;
97
98 scratch = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
99 if (!scratch)
100 goto error;
101 *per_cpu_ptr(scratches, i) = scratch;
102 }
103
104 return scratches;
105
106error:
107 crypto_scomp_free_scratches(scratches);
108 return NULL;
109}
110
111static void crypto_scomp_free_all_scratches(void)
112{
113 if (!--scomp_scratch_users) {
114 crypto_scomp_free_scratches(scomp_src_scratches);
115 crypto_scomp_free_scratches(scomp_dst_scratches);
116 scomp_src_scratches = NULL;
117 scomp_dst_scratches = NULL;
118 }
119}
120
121static int crypto_scomp_alloc_all_scratches(void)
122{
123 if (!scomp_scratch_users++) {
124 scomp_src_scratches = crypto_scomp_alloc_scratches();
125 if (!scomp_src_scratches)
126 return -ENOMEM;
127 scomp_dst_scratches = crypto_scomp_alloc_scratches();
cc4d110e
AB
128 if (!scomp_dst_scratches) {
129 crypto_scomp_free_scratches(scomp_src_scratches);
130 scomp_src_scratches = NULL;
1ab53a77 131 return -ENOMEM;
cc4d110e 132 }
1ab53a77
GC
133 }
134 return 0;
135}
136
137static void crypto_scomp_sg_free(struct scatterlist *sgl)
138{
139 int i, n;
140 struct page *page;
141
142 if (!sgl)
143 return;
144
145 n = sg_nents(sgl);
146 for_each_sg(sgl, sgl, n, i) {
147 page = sg_page(sgl);
148 if (page)
149 __free_page(page);
150 }
151
152 kfree(sgl);
153}
154
155static struct scatterlist *crypto_scomp_sg_alloc(size_t size, gfp_t gfp)
156{
157 struct scatterlist *sgl;
158 struct page *page;
159 int i, n;
160
161 n = ((size - 1) >> PAGE_SHIFT) + 1;
162
163 sgl = kmalloc_array(n, sizeof(struct scatterlist), gfp);
164 if (!sgl)
165 return NULL;
166
167 sg_init_table(sgl, n);
168
169 for (i = 0; i < n; i++) {
170 page = alloc_page(gfp);
171 if (!page)
172 goto err;
173 sg_set_page(sgl + i, page, PAGE_SIZE, 0);
174 }
175
176 return sgl;
177
178err:
179 sg_mark_end(sgl + i);
180 crypto_scomp_sg_free(sgl);
181 return NULL;
182}
183
184static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
185{
186 struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
187 void **tfm_ctx = acomp_tfm_ctx(tfm);
188 struct crypto_scomp *scomp = *tfm_ctx;
189 void **ctx = acomp_request_ctx(req);
190 const int cpu = get_cpu();
191 u8 *scratch_src = *per_cpu_ptr(scomp_src_scratches, cpu);
192 u8 *scratch_dst = *per_cpu_ptr(scomp_dst_scratches, cpu);
193 int ret;
194
195 if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE) {
196 ret = -EINVAL;
197 goto out;
198 }
199
200 if (req->dst && !req->dlen) {
201 ret = -EINVAL;
202 goto out;
203 }
204
205 if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
206 req->dlen = SCOMP_SCRATCH_SIZE;
207
208 scatterwalk_map_and_copy(scratch_src, req->src, 0, req->slen, 0);
209 if (dir)
210 ret = crypto_scomp_compress(scomp, scratch_src, req->slen,
211 scratch_dst, &req->dlen, *ctx);
212 else
213 ret = crypto_scomp_decompress(scomp, scratch_src, req->slen,
214 scratch_dst, &req->dlen, *ctx);
215 if (!ret) {
216 if (!req->dst) {
3c083772 217 req->dst = crypto_scomp_sg_alloc(req->dlen, GFP_ATOMIC);
1ab53a77
GC
218 if (!req->dst)
219 goto out;
220 }
221 scatterwalk_map_and_copy(scratch_dst, req->dst, 0, req->dlen,
222 1);
223 }
224out:
225 put_cpu();
226 return ret;
227}
228
229static int scomp_acomp_compress(struct acomp_req *req)
230{
231 return scomp_acomp_comp_decomp(req, 1);
232}
233
234static int scomp_acomp_decompress(struct acomp_req *req)
235{
236 return scomp_acomp_comp_decomp(req, 0);
237}
238
239static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
240{
241 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
242
243 crypto_free_scomp(*ctx);
244}
245
246int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
247{
248 struct crypto_alg *calg = tfm->__crt_alg;
249 struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
250 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
251 struct crypto_scomp *scomp;
252
253 if (!crypto_mod_get(calg))
254 return -EAGAIN;
255
256 scomp = crypto_create_tfm(calg, &crypto_scomp_type);
257 if (IS_ERR(scomp)) {
258 crypto_mod_put(calg);
259 return PTR_ERR(scomp);
260 }
261
262 *ctx = scomp;
263 tfm->exit = crypto_exit_scomp_ops_async;
264
265 crt->compress = scomp_acomp_compress;
266 crt->decompress = scomp_acomp_decompress;
267 crt->dst_free = crypto_scomp_sg_free;
268 crt->reqsize = sizeof(void *);
269
270 return 0;
271}
272
273struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
274{
275 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
276 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
277 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
278 struct crypto_scomp *scomp = *tfm_ctx;
279 void *ctx;
280
281 ctx = crypto_scomp_alloc_ctx(scomp);
282 if (IS_ERR(ctx)) {
283 kfree(req);
284 return NULL;
285 }
286
287 *req->__ctx = ctx;
288
289 return req;
290}
291
292void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
293{
294 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
295 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
296 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
297 struct crypto_scomp *scomp = *tfm_ctx;
298 void *ctx = *req->__ctx;
299
300 if (ctx)
301 crypto_scomp_free_ctx(scomp, ctx);
302}
303
304static const struct crypto_type crypto_scomp_type = {
305 .extsize = crypto_alg_extsize,
306 .init_tfm = crypto_scomp_init_tfm,
307#ifdef CONFIG_PROC_FS
308 .show = crypto_scomp_show,
309#endif
310 .report = crypto_scomp_report,
311 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
312 .maskset = CRYPTO_ALG_TYPE_MASK,
313 .type = CRYPTO_ALG_TYPE_SCOMPRESS,
314 .tfmsize = offsetof(struct crypto_scomp, base),
315};
316
317int crypto_register_scomp(struct scomp_alg *alg)
318{
319 struct crypto_alg *base = &alg->base;
320 int ret = -ENOMEM;
321
322 mutex_lock(&scomp_lock);
323 if (crypto_scomp_alloc_all_scratches())
324 goto error;
325
326 base->cra_type = &crypto_scomp_type;
327 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
328 base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
329
330 ret = crypto_register_alg(base);
331 if (ret)
332 goto error;
333
334 mutex_unlock(&scomp_lock);
335 return ret;
336
337error:
338 crypto_scomp_free_all_scratches();
339 mutex_unlock(&scomp_lock);
340 return ret;
341}
342EXPORT_SYMBOL_GPL(crypto_register_scomp);
343
344int crypto_unregister_scomp(struct scomp_alg *alg)
345{
346 int ret;
347
348 mutex_lock(&scomp_lock);
349 ret = crypto_unregister_alg(&alg->base);
350 crypto_scomp_free_all_scratches();
351 mutex_unlock(&scomp_lock);
352
353 return ret;
354}
355EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
356
3de4f5e1
GC
357int crypto_register_scomps(struct scomp_alg *algs, int count)
358{
359 int i, ret;
360
361 for (i = 0; i < count; i++) {
362 ret = crypto_register_scomp(&algs[i]);
363 if (ret)
364 goto err;
365 }
366
367 return 0;
368
369err:
370 for (--i; i >= 0; --i)
371 crypto_unregister_scomp(&algs[i]);
372
373 return ret;
374}
375EXPORT_SYMBOL_GPL(crypto_register_scomps);
376
377void crypto_unregister_scomps(struct scomp_alg *algs, int count)
378{
379 int i;
380
381 for (i = count - 1; i >= 0; --i)
382 crypto_unregister_scomp(&algs[i]);
383}
384EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
385
1ab53a77
GC
386MODULE_LICENSE("GPL");
387MODULE_DESCRIPTION("Synchronous compression type");