2 * Synchronous Compression operations
4 * Copyright 2015 LG Electronics Inc.
5 * Copyright (c) 2016, Intel Corporation
6 * Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
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)
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>
21 #include <linux/compiler.h>
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>
32 static const struct crypto_type crypto_scomp_type;
33 static void * __percpu *scomp_src_scratches;
34 static void * __percpu *scomp_dst_scratches;
35 static int scomp_scratch_users;
36 static DEFINE_MUTEX(scomp_lock);
39 static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
41 struct crypto_report_comp rscomp;
43 strncpy(rscomp.type, "scomp", sizeof(rscomp.type));
45 if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
46 sizeof(struct crypto_report_comp), &rscomp))
54 static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
60 static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
63 static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
65 seq_puts(m, "type : scomp\n");
68 static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
73 static void crypto_scomp_free_scratches(void * __percpu *scratches)
80 for_each_possible_cpu(i)
81 vfree(*per_cpu_ptr(scratches, i));
83 free_percpu(scratches);
86 static void * __percpu *crypto_scomp_alloc_scratches(void)
88 void * __percpu *scratches;
91 scratches = alloc_percpu(void *);
95 for_each_possible_cpu(i) {
98 scratch = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
101 *per_cpu_ptr(scratches, i) = scratch;
107 crypto_scomp_free_scratches(scratches);
111 static void crypto_scomp_free_all_scratches(void)
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;
121 static int crypto_scomp_alloc_all_scratches(void)
123 if (!scomp_scratch_users++) {
124 scomp_src_scratches = crypto_scomp_alloc_scratches();
125 if (!scomp_src_scratches)
127 scomp_dst_scratches = crypto_scomp_alloc_scratches();
128 if (!scomp_dst_scratches) {
129 crypto_scomp_free_scratches(scomp_src_scratches);
130 scomp_src_scratches = NULL;
137 static void crypto_scomp_sg_free(struct scatterlist *sgl)
146 for_each_sg(sgl, sgl, n, i) {
155 static struct scatterlist *crypto_scomp_sg_alloc(size_t size, gfp_t gfp)
157 struct scatterlist *sgl;
161 n = ((size - 1) >> PAGE_SHIFT) + 1;
163 sgl = kmalloc_array(n, sizeof(struct scatterlist), gfp);
167 sg_init_table(sgl, n);
169 for (i = 0; i < n; i++) {
170 page = alloc_page(gfp);
173 sg_set_page(sgl + i, page, PAGE_SIZE, 0);
179 sg_mark_end(sgl + i);
180 crypto_scomp_sg_free(sgl);
184 static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
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);
195 if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE) {
200 if (req->dst && !req->dlen) {
205 if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
206 req->dlen = SCOMP_SCRATCH_SIZE;
208 scatterwalk_map_and_copy(scratch_src, req->src, 0, req->slen, 0);
210 ret = crypto_scomp_compress(scomp, scratch_src, req->slen,
211 scratch_dst, &req->dlen, *ctx);
213 ret = crypto_scomp_decompress(scomp, scratch_src, req->slen,
214 scratch_dst, &req->dlen, *ctx);
217 req->dst = crypto_scomp_sg_alloc(req->dlen, GFP_ATOMIC);
221 scatterwalk_map_and_copy(scratch_dst, req->dst, 0, req->dlen,
229 static int scomp_acomp_compress(struct acomp_req *req)
231 return scomp_acomp_comp_decomp(req, 1);
234 static int scomp_acomp_decompress(struct acomp_req *req)
236 return scomp_acomp_comp_decomp(req, 0);
239 static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
241 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
243 crypto_free_scomp(*ctx);
246 int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
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;
253 if (!crypto_mod_get(calg))
256 scomp = crypto_create_tfm(calg, &crypto_scomp_type);
258 crypto_mod_put(calg);
259 return PTR_ERR(scomp);
263 tfm->exit = crypto_exit_scomp_ops_async;
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 *);
273 struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
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;
281 ctx = crypto_scomp_alloc_ctx(scomp);
292 void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
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;
301 crypto_scomp_free_ctx(scomp, ctx);
304 static 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,
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),
317 int crypto_register_scomp(struct scomp_alg *alg)
319 struct crypto_alg *base = &alg->base;
322 mutex_lock(&scomp_lock);
323 if (crypto_scomp_alloc_all_scratches())
326 base->cra_type = &crypto_scomp_type;
327 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
328 base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
330 ret = crypto_register_alg(base);
334 mutex_unlock(&scomp_lock);
338 crypto_scomp_free_all_scratches();
339 mutex_unlock(&scomp_lock);
342 EXPORT_SYMBOL_GPL(crypto_register_scomp);
344 int crypto_unregister_scomp(struct scomp_alg *alg)
348 mutex_lock(&scomp_lock);
349 ret = crypto_unregister_alg(&alg->base);
350 crypto_scomp_free_all_scratches();
351 mutex_unlock(&scomp_lock);
355 EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
357 int crypto_register_scomps(struct scomp_alg *algs, int count)
361 for (i = 0; i < count; i++) {
362 ret = crypto_register_scomp(&algs[i]);
370 for (--i; i >= 0; --i)
371 crypto_unregister_scomp(&algs[i]);
375 EXPORT_SYMBOL_GPL(crypto_register_scomps);
377 void crypto_unregister_scomps(struct scomp_alg *algs, int count)
381 for (i = count - 1; i >= 0; --i)
382 crypto_unregister_scomp(&algs[i]);
384 EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
386 MODULE_LICENSE("GPL");
387 MODULE_DESCRIPTION("Synchronous compression type");