dm vdo io-submitter: rename to vdo_submit_data_vio
[linux-2.6-block.git] / crypto / scompress.c
CommitLineData
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
26struct scomp_scratch {
27 spinlock_t lock;
28 void *src;
29 void *dst;
30};
31
32static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = {
33 .lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock),
34};
35
1ab53a77 36static const struct crypto_type crypto_scomp_type;
1ab53a77
GC
37static int scomp_scratch_users;
38static DEFINE_MUTEX(scomp_lock);
39
c0f9e01d
HX
40static 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
53static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
d8c34b94 54 __maybe_unused;
1ab53a77
GC
55
56static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
57{
58 seq_puts(m, "type : scomp\n");
59}
60
71052dcf 61static 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 76static 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
96error:
97 crypto_scomp_free_scratches();
98 return -ENOMEM;
1ab53a77
GC
99}
100
6a8487a1
AB
101static 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
113static 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;
744e1885 120 unsigned int dlen;
1ab53a77
GC
121 int ret;
122
71052dcf
SAS
123 if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
124 return -EINVAL;
1ab53a77 125
71052dcf
SAS
126 if (req->dst && !req->dlen)
127 return -EINVAL;
1ab53a77
GC
128
129 if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
130 req->dlen = SCOMP_SCRATCH_SIZE;
131
744e1885
CZ
132 dlen = req->dlen;
133
71052dcf
SAS
134 scratch = raw_cpu_ptr(&scomp_scratch);
135 spin_lock(&scratch->lock);
136
137 scatterwalk_map_and_copy(scratch->src, req->src, 0, req->slen, 0);
1ab53a77 138 if (dir)
71052dcf
SAS
139 ret = crypto_scomp_compress(scomp, scratch->src, req->slen,
140 scratch->dst, &req->dlen, *ctx);
1ab53a77 141 else
71052dcf
SAS
142 ret = crypto_scomp_decompress(scomp, scratch->src, req->slen,
143 scratch->dst, &req->dlen, *ctx);
1ab53a77
GC
144 if (!ret) {
145 if (!req->dst) {
8cd579d2 146 req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
6a4d1b18
SAS
147 if (!req->dst) {
148 ret = -ENOMEM;
1ab53a77 149 goto out;
6a4d1b18 150 }
744e1885
CZ
151 } else if (req->dlen > dlen) {
152 ret = -ENOSPC;
153 goto out;
1ab53a77 154 }
71052dcf 155 scatterwalk_map_and_copy(scratch->dst, req->dst, 0, req->dlen,
1ab53a77
GC
156 1);
157 }
158out:
71052dcf 159 spin_unlock(&scratch->lock);
1ab53a77
GC
160 return ret;
161}
162
163static int scomp_acomp_compress(struct acomp_req *req)
164{
165 return scomp_acomp_comp_decomp(req, 1);
166}
167
168static int scomp_acomp_decompress(struct acomp_req *req)
169{
170 return scomp_acomp_comp_decomp(req, 0);
171}
172
173static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
174{
175 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
176
177 crypto_free_scomp(*ctx);
6a8487a1
AB
178
179 mutex_lock(&scomp_lock);
71052dcf
SAS
180 if (!--scomp_scratch_users)
181 crypto_scomp_free_scratches();
6a8487a1 182 mutex_unlock(&scomp_lock);
1ab53a77
GC
183}
184
185int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
186{
187 struct crypto_alg *calg = tfm->__crt_alg;
188 struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
189 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
190 struct crypto_scomp *scomp;
191
192 if (!crypto_mod_get(calg))
193 return -EAGAIN;
194
195 scomp = crypto_create_tfm(calg, &crypto_scomp_type);
196 if (IS_ERR(scomp)) {
197 crypto_mod_put(calg);
198 return PTR_ERR(scomp);
199 }
200
201 *ctx = scomp;
202 tfm->exit = crypto_exit_scomp_ops_async;
203
204 crt->compress = scomp_acomp_compress;
205 crt->decompress = scomp_acomp_decompress;
8cd579d2 206 crt->dst_free = sgl_free;
1ab53a77
GC
207 crt->reqsize = sizeof(void *);
208
209 return 0;
210}
211
212struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
213{
214 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
215 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
216 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
217 struct crypto_scomp *scomp = *tfm_ctx;
218 void *ctx;
219
220 ctx = crypto_scomp_alloc_ctx(scomp);
221 if (IS_ERR(ctx)) {
222 kfree(req);
223 return NULL;
224 }
225
226 *req->__ctx = ctx;
227
228 return req;
229}
230
231void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
232{
233 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
234 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
235 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
236 struct crypto_scomp *scomp = *tfm_ctx;
237 void *ctx = *req->__ctx;
238
239 if (ctx)
240 crypto_scomp_free_ctx(scomp, ctx);
241}
242
243static const struct crypto_type crypto_scomp_type = {
244 .extsize = crypto_alg_extsize,
245 .init_tfm = crypto_scomp_init_tfm,
246#ifdef CONFIG_PROC_FS
247 .show = crypto_scomp_show,
248#endif
b8969a1b 249#if IS_ENABLED(CONFIG_CRYPTO_USER)
1ab53a77 250 .report = crypto_scomp_report,
c0f9e01d 251#endif
0a742389
HX
252#ifdef CONFIG_CRYPTO_STATS
253 .report_stat = crypto_acomp_report_stat,
254#endif
1ab53a77
GC
255 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
256 .maskset = CRYPTO_ALG_TYPE_MASK,
257 .type = CRYPTO_ALG_TYPE_SCOMPRESS,
258 .tfmsize = offsetof(struct crypto_scomp, base),
259};
260
261int crypto_register_scomp(struct scomp_alg *alg)
262{
0a742389
HX
263 struct crypto_alg *base = &alg->calg.base;
264
265 comp_prepare_alg(&alg->calg);
1ab53a77
GC
266
267 base->cra_type = &crypto_scomp_type;
1ab53a77
GC
268 base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
269
6a8487a1 270 return crypto_register_alg(base);
1ab53a77
GC
271}
272EXPORT_SYMBOL_GPL(crypto_register_scomp);
273
c6d633a9 274void crypto_unregister_scomp(struct scomp_alg *alg)
1ab53a77 275{
c6d633a9 276 crypto_unregister_alg(&alg->base);
1ab53a77
GC
277}
278EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
279
3de4f5e1
GC
280int crypto_register_scomps(struct scomp_alg *algs, int count)
281{
282 int i, ret;
283
284 for (i = 0; i < count; i++) {
285 ret = crypto_register_scomp(&algs[i]);
286 if (ret)
287 goto err;
288 }
289
290 return 0;
291
292err:
293 for (--i; i >= 0; --i)
294 crypto_unregister_scomp(&algs[i]);
295
296 return ret;
297}
298EXPORT_SYMBOL_GPL(crypto_register_scomps);
299
300void crypto_unregister_scomps(struct scomp_alg *algs, int count)
301{
302 int i;
303
304 for (i = count - 1; i >= 0; --i)
305 crypto_unregister_scomp(&algs[i]);
306}
307EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
308
1ab53a77
GC
309MODULE_LICENSE("GPL");
310MODULE_DESCRIPTION("Synchronous compression type");