blob: 1cef6bb06a811e0ca1294ee47fe6a0d82f2f9e0e [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +01002/*
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>
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +01008 */
Herbert Xu0a742382023-02-16 18:35:17 +08009
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>
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010015#include <linux/kernel.h>
16#include <linux/module.h>
Herbert Xu0a742382023-02-16 18:35:17 +080017#include <linux/scatterlist.h>
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010018#include <linux/seq_file.h>
19#include <linux/slab.h>
20#include <linux/string.h>
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010021#include <linux/vmalloc.h>
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010022#include <net/netlink.h>
Herbert Xu0a742382023-02-16 18:35:17 +080023
24#include "compress.h"
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010025
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +010026struct 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
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010036static const struct crypto_type crypto_scomp_type;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010037static int scomp_scratch_users;
38static DEFINE_MUTEX(scomp_lock);
39
Herbert Xuc0f9e012023-02-16 18:35:28 +080040static int __maybe_unused crypto_scomp_report(
41 struct sk_buff *skb, struct crypto_alg *alg)
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010042{
43 struct crypto_report_comp rscomp;
44
Eric Biggers37db69e2018-11-03 14:56:03 -070045 memset(&rscomp, 0, sizeof(rscomp));
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010046
Eric Biggers37db69e2018-11-03 14:56:03 -070047 strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010048
Eric Biggers37db69e2018-11-03 14:56:03 -070049 return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
50 sizeof(rscomp), &rscomp);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010051}
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010052
53static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
Gideon Israel Dsouzad8c34b92016-12-31 21:26:23 +053054 __maybe_unused;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010055
56static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
57{
58 seq_puts(m, "type : scomp\n");
59}
60
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +010061static void crypto_scomp_free_scratches(void)
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010062{
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +010063 struct scomp_scratch *scratch;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010064 int i;
65
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010066 for_each_possible_cpu(i) {
Sebastian Andrzej Siewior8c3fffe2019-04-12 17:14:15 +020067 scratch = per_cpu_ptr(&scomp_scratch, i);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010068
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +010069 vfree(scratch->src);
70 vfree(scratch->dst);
71 scratch->src = NULL;
72 scratch->dst = NULL;
73 }
74}
75
76static int crypto_scomp_alloc_scratches(void)
77{
78 struct scomp_scratch *scratch;
79 int i;
80
81 for_each_possible_cpu(i) {
82 void *mem;
83
Sebastian Andrzej Siewior8c3fffe2019-04-12 17:14:15 +020084 scratch = per_cpu_ptr(&scomp_scratch, i);
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +010085
86 mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
87 if (!mem)
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010088 goto error;
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +010089 scratch->src = mem;
90 mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
91 if (!mem)
92 goto error;
93 scratch->dst = mem;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010094 }
95 return 0;
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +010096error:
97 crypto_scomp_free_scratches();
98 return -ENOMEM;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010099}
100
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100101static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
102{
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +0100103 int ret = 0;
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100104
105 mutex_lock(&scomp_lock);
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +0100106 if (!scomp_scratch_users++)
107 ret = crypto_scomp_alloc_scratches();
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100108 mutex_unlock(&scomp_lock);
109
110 return ret;
111}
112
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100113static 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);
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +0100119 struct scomp_scratch *scratch;
Barry Song77292bb2024-03-02 08:27:45 +1300120 void *src, *dst;
Chengming Zhou744e18852023-12-27 09:35:23 +0000121 unsigned int dlen;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100122 int ret;
123
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +0100124 if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
125 return -EINVAL;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100126
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +0100127 if (req->dst && !req->dlen)
128 return -EINVAL;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100129
130 if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
131 req->dlen = SCOMP_SCRATCH_SIZE;
132
Chengming Zhou744e18852023-12-27 09:35:23 +0000133 dlen = req->dlen;
134
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +0100135 scratch = raw_cpu_ptr(&scomp_scratch);
136 spin_lock(&scratch->lock);
137
Barry Song77292bb2024-03-02 08:27:45 +1300138 if (sg_nents(req->src) == 1 && !PageHighMem(sg_page(req->src))) {
139 src = page_to_virt(sg_page(req->src)) + req->src->offset;
140 } else {
141 scatterwalk_map_and_copy(scratch->src, req->src, 0,
142 req->slen, 0);
143 src = scratch->src;
144 }
145
146 if (req->dst && sg_nents(req->dst) == 1 && !PageHighMem(sg_page(req->dst)))
147 dst = page_to_virt(sg_page(req->dst)) + req->dst->offset;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100148 else
Barry Song77292bb2024-03-02 08:27:45 +1300149 dst = scratch->dst;
150
151 if (dir)
152 ret = crypto_scomp_compress(scomp, src, req->slen,
153 dst, &req->dlen, *ctx);
154 else
155 ret = crypto_scomp_decompress(scomp, src, req->slen,
156 dst, &req->dlen, *ctx);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100157 if (!ret) {
158 if (!req->dst) {
Bart Van Assche8cd579d2018-01-05 08:26:47 -0800159 req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
Sebastian Andrzej Siewior6a4d1b12019-03-29 14:09:55 +0100160 if (!req->dst) {
161 ret = -ENOMEM;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100162 goto out;
Sebastian Andrzej Siewior6a4d1b12019-03-29 14:09:55 +0100163 }
Chengming Zhou744e18852023-12-27 09:35:23 +0000164 } else if (req->dlen > dlen) {
165 ret = -ENOSPC;
166 goto out;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100167 }
Barry Song77292bb2024-03-02 08:27:45 +1300168 if (dst == scratch->dst) {
169 scatterwalk_map_and_copy(scratch->dst, req->dst, 0,
170 req->dlen, 1);
171 } else {
172 int nr_pages = DIV_ROUND_UP(req->dst->offset + req->dlen, PAGE_SIZE);
173 int i;
174 struct page *dst_page = sg_page(req->dst);
175
176 for (i = 0; i < nr_pages; i++)
177 flush_dcache_page(dst_page + i);
178 }
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100179 }
180out:
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +0100181 spin_unlock(&scratch->lock);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100182 return ret;
183}
184
185static int scomp_acomp_compress(struct acomp_req *req)
186{
187 return scomp_acomp_comp_decomp(req, 1);
188}
189
190static int scomp_acomp_decompress(struct acomp_req *req)
191{
192 return scomp_acomp_comp_decomp(req, 0);
193}
194
195static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
196{
197 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
198
199 crypto_free_scomp(*ctx);
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100200
201 mutex_lock(&scomp_lock);
Sebastian Andrzej Siewior71052dc2019-03-29 14:09:56 +0100202 if (!--scomp_scratch_users)
203 crypto_scomp_free_scratches();
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100204 mutex_unlock(&scomp_lock);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100205}
206
207int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
208{
209 struct crypto_alg *calg = tfm->__crt_alg;
210 struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
211 struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
212 struct crypto_scomp *scomp;
213
214 if (!crypto_mod_get(calg))
215 return -EAGAIN;
216
217 scomp = crypto_create_tfm(calg, &crypto_scomp_type);
218 if (IS_ERR(scomp)) {
219 crypto_mod_put(calg);
220 return PTR_ERR(scomp);
221 }
222
223 *ctx = scomp;
224 tfm->exit = crypto_exit_scomp_ops_async;
225
226 crt->compress = scomp_acomp_compress;
227 crt->decompress = scomp_acomp_decompress;
Bart Van Assche8cd579d2018-01-05 08:26:47 -0800228 crt->dst_free = sgl_free;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100229 crt->reqsize = sizeof(void *);
230
231 return 0;
232}
233
234struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
235{
236 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
237 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
238 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
239 struct crypto_scomp *scomp = *tfm_ctx;
240 void *ctx;
241
242 ctx = crypto_scomp_alloc_ctx(scomp);
243 if (IS_ERR(ctx)) {
244 kfree(req);
245 return NULL;
246 }
247
248 *req->__ctx = ctx;
249
250 return req;
251}
252
253void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
254{
255 struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
256 struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
257 struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
258 struct crypto_scomp *scomp = *tfm_ctx;
259 void *ctx = *req->__ctx;
260
261 if (ctx)
262 crypto_scomp_free_ctx(scomp, ctx);
263}
264
265static const struct crypto_type crypto_scomp_type = {
266 .extsize = crypto_alg_extsize,
267 .init_tfm = crypto_scomp_init_tfm,
268#ifdef CONFIG_PROC_FS
269 .show = crypto_scomp_show,
270#endif
Ondrej Mosnacekb8969a12023-05-02 10:02:33 +0200271#if IS_ENABLED(CONFIG_CRYPTO_USER)
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100272 .report = crypto_scomp_report,
Herbert Xuc0f9e012023-02-16 18:35:28 +0800273#endif
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100274 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
275 .maskset = CRYPTO_ALG_TYPE_MASK,
276 .type = CRYPTO_ALG_TYPE_SCOMPRESS,
277 .tfmsize = offsetof(struct crypto_scomp, base),
278};
279
280int crypto_register_scomp(struct scomp_alg *alg)
281{
Herbert Xu6a8dbd72024-03-13 09:49:37 +0800282 struct crypto_alg *base = &alg->calg.base;
283
284 comp_prepare_alg(&alg->calg);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100285
286 base->cra_type = &crypto_scomp_type;
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100287 base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
288
Ard Biesheuvel6a8487a2017-07-21 16:42:38 +0100289 return crypto_register_alg(base);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100290}
291EXPORT_SYMBOL_GPL(crypto_register_scomp);
292
Eric Biggersc6d633a2019-12-15 15:51:19 -0800293void crypto_unregister_scomp(struct scomp_alg *alg)
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100294{
Eric Biggersc6d633a2019-12-15 15:51:19 -0800295 crypto_unregister_alg(&alg->base);
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100296}
297EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
298
Giovanni Cabiddu3de4f5e2017-04-21 21:54:29 +0100299int crypto_register_scomps(struct scomp_alg *algs, int count)
300{
301 int i, ret;
302
303 for (i = 0; i < count; i++) {
304 ret = crypto_register_scomp(&algs[i]);
305 if (ret)
306 goto err;
307 }
308
309 return 0;
310
311err:
312 for (--i; i >= 0; --i)
313 crypto_unregister_scomp(&algs[i]);
314
315 return ret;
316}
317EXPORT_SYMBOL_GPL(crypto_register_scomps);
318
319void crypto_unregister_scomps(struct scomp_alg *algs, int count)
320{
321 int i;
322
323 for (i = count - 1; i >= 0; --i)
324 crypto_unregister_scomp(&algs[i]);
325}
326EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
327
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +0100328MODULE_LICENSE("GPL");
329MODULE_DESCRIPTION("Synchronous compression type");