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 struct scomp_scratch {
38 static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = {
39 .lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock),
42 static const struct crypto_type crypto_scomp_type;
43 static int scomp_scratch_users;
44 static DEFINE_MUTEX(scomp_lock);
47 static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
49 struct crypto_report_comp rscomp;
51 memset(&rscomp, 0, sizeof(rscomp));
53 strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
55 return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
56 sizeof(rscomp), &rscomp);
59 static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
65 static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
68 static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
70 seq_puts(m, "type : scomp\n");
73 static void crypto_scomp_free_scratches(void)
75 struct scomp_scratch *scratch;
78 for_each_possible_cpu(i) {
79 scratch = per_cpu_ptr(&scomp_scratch, i);
88 static int crypto_scomp_alloc_scratches(void)
90 struct scomp_scratch *scratch;
93 for_each_possible_cpu(i) {
96 scratch = per_cpu_ptr(&scomp_scratch, i);
98 mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
102 mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
109 crypto_scomp_free_scratches();
113 static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
117 mutex_lock(&scomp_lock);
118 if (!scomp_scratch_users++)
119 ret = crypto_scomp_alloc_scratches();
120 mutex_unlock(&scomp_lock);
125 static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
127 struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
128 void **tfm_ctx = acomp_tfm_ctx(tfm);
129 struct crypto_scomp *scomp = *tfm_ctx;
130 void **ctx = acomp_request_ctx(req);
131 struct scomp_scratch *scratch;
134 if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
137 if (req->dst && !req->dlen)
140 if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
141 req->dlen = SCOMP_SCRATCH_SIZE;
143 scratch = raw_cpu_ptr(&scomp_scratch);
144 spin_lock(&scratch->lock);
146 scatterwalk_map_and_copy(scratch->src, req->src, 0, req->slen, 0);
148 ret = crypto_scomp_compress(scomp, scratch->src, req->slen,
149 scratch->dst, &req->dlen, *ctx);
151 ret = crypto_scomp_decompress(scomp, scratch->src, req->slen,
152 scratch->dst, &req->dlen, *ctx);
155 req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
161 scatterwalk_map_and_copy(scratch->dst, req->dst, 0, req->dlen,
165 spin_unlock(&scratch->lock);
169 static int scomp_acomp_compress(struct acomp_req *req)
171 return scomp_acomp_comp_decomp(req, 1);
174 static int scomp_acomp_decompress(struct acomp_req *req)
176 return scomp_acomp_comp_decomp(req, 0);
179 static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
181 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
183 crypto_free_scomp(*ctx);
185 mutex_lock(&scomp_lock);
186 if (!--scomp_scratch_users)
187 crypto_scomp_free_scratches();
188 mutex_unlock(&scomp_lock);
191 int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
193 struct crypto_alg *calg = tfm->__crt_alg;
194 struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
195 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
196 struct crypto_scomp *scomp;
198 if (!crypto_mod_get(calg))
201 scomp = crypto_create_tfm(calg, &crypto_scomp_type);
203 crypto_mod_put(calg);
204 return PTR_ERR(scomp);
208 tfm->exit = crypto_exit_scomp_ops_async;
210 crt->compress = scomp_acomp_compress;
211 crt->decompress = scomp_acomp_decompress;
212 crt->dst_free = sgl_free;
213 crt->reqsize = sizeof(void *);
218 struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
220 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
221 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
222 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
223 struct crypto_scomp *scomp = *tfm_ctx;
226 ctx = crypto_scomp_alloc_ctx(scomp);
237 void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
239 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
240 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
241 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
242 struct crypto_scomp *scomp = *tfm_ctx;
243 void *ctx = *req->__ctx;
246 crypto_scomp_free_ctx(scomp, ctx);
249 static const struct crypto_type crypto_scomp_type = {
250 .extsize = crypto_alg_extsize,
251 .init_tfm = crypto_scomp_init_tfm,
252 #ifdef CONFIG_PROC_FS
253 .show = crypto_scomp_show,
255 .report = crypto_scomp_report,
256 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
257 .maskset = CRYPTO_ALG_TYPE_MASK,
258 .type = CRYPTO_ALG_TYPE_SCOMPRESS,
259 .tfmsize = offsetof(struct crypto_scomp, base),
262 int crypto_register_scomp(struct scomp_alg *alg)
264 struct crypto_alg *base = &alg->base;
266 base->cra_type = &crypto_scomp_type;
267 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
268 base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
270 return crypto_register_alg(base);
272 EXPORT_SYMBOL_GPL(crypto_register_scomp);
274 int crypto_unregister_scomp(struct scomp_alg *alg)
276 return crypto_unregister_alg(&alg->base);
278 EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
280 int crypto_register_scomps(struct scomp_alg *algs, int count)
284 for (i = 0; i < count; i++) {
285 ret = crypto_register_scomp(&algs[i]);
293 for (--i; i >= 0; --i)
294 crypto_unregister_scomp(&algs[i]);
298 EXPORT_SYMBOL_GPL(crypto_register_scomps);
300 void crypto_unregister_scomps(struct scomp_alg *algs, int count)
304 for (i = count - 1; i >= 0; --i)
305 crypto_unregister_scomp(&algs[i]);
307 EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
309 MODULE_LICENSE("GPL");
310 MODULE_DESCRIPTION("Synchronous compression type");