Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  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>
  8 */
  9#include <linux/errno.h>
 
 
 
 
 
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 
 12#include <linux/seq_file.h>
 13#include <linux/slab.h>
 14#include <linux/string.h>
 15#include <linux/crypto.h>
 16#include <linux/compiler.h>
 17#include <linux/vmalloc.h>
 18#include <crypto/algapi.h>
 19#include <linux/cryptouser.h>
 20#include <net/netlink.h>
 21#include <linux/scatterlist.h>
 22#include <crypto/scatterwalk.h>
 23#include <crypto/internal/acompress.h>
 24#include <crypto/internal/scompress.h>
 25#include "internal.h"
 26
 27struct scomp_scratch {
 28	spinlock_t	lock;
 29	void		*src;
 30	void		*dst;
 31};
 32
 33static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = {
 34	.lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock),
 35};
 36
 37static const struct crypto_type crypto_scomp_type;
 38static int scomp_scratch_users;
 39static DEFINE_MUTEX(scomp_lock);
 40
 41#ifdef CONFIG_NET
 42static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
 43{
 44	struct crypto_report_comp rscomp;
 45
 46	memset(&rscomp, 0, sizeof(rscomp));
 47
 48	strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
 49
 50	return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
 51		       sizeof(rscomp), &rscomp);
 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)
 61	__maybe_unused;
 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 void crypto_scomp_free_scratches(void)
 69{
 70	struct scomp_scratch *scratch;
 71	int i;
 72
 73	for_each_possible_cpu(i) {
 74		scratch = per_cpu_ptr(&scomp_scratch, i);
 75
 76		vfree(scratch->src);
 77		vfree(scratch->dst);
 78		scratch->src = NULL;
 79		scratch->dst = NULL;
 80	}
 81}
 82
 83static int crypto_scomp_alloc_scratches(void)
 84{
 85	struct scomp_scratch *scratch;
 86	int i;
 87
 88	for_each_possible_cpu(i) {
 89		void *mem;
 90
 91		scratch = per_cpu_ptr(&scomp_scratch, i);
 92
 93		mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
 94		if (!mem)
 95			goto error;
 96		scratch->src = mem;
 97		mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
 98		if (!mem)
 99			goto error;
100		scratch->dst = mem;
101	}
102	return 0;
103error:
104	crypto_scomp_free_scratches();
105	return -ENOMEM;
106}
107
108static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
109{
110	int ret = 0;
111
112	mutex_lock(&scomp_lock);
113	if (!scomp_scratch_users++)
114		ret = crypto_scomp_alloc_scratches();
115	mutex_unlock(&scomp_lock);
116
117	return ret;
118}
119
120static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
121{
122	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
123	void **tfm_ctx = acomp_tfm_ctx(tfm);
124	struct crypto_scomp *scomp = *tfm_ctx;
125	void **ctx = acomp_request_ctx(req);
126	struct scomp_scratch *scratch;
 
 
127	int ret;
128
129	if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
130		return -EINVAL;
131
132	if (req->dst && !req->dlen)
133		return -EINVAL;
134
135	if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
136		req->dlen = SCOMP_SCRATCH_SIZE;
137
 
 
138	scratch = raw_cpu_ptr(&scomp_scratch);
139	spin_lock(&scratch->lock);
140
141	scatterwalk_map_and_copy(scratch->src, req->src, 0, req->slen, 0);
 
 
 
 
 
 
 
 
 
 
 
 
142	if (dir)
143		ret = crypto_scomp_compress(scomp, scratch->src, req->slen,
144					    scratch->dst, &req->dlen, *ctx);
145	else
146		ret = crypto_scomp_decompress(scomp, scratch->src, req->slen,
147					      scratch->dst, &req->dlen, *ctx);
148	if (!ret) {
149		if (!req->dst) {
150			req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
151			if (!req->dst) {
152				ret = -ENOMEM;
153				goto out;
154			}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155		}
156		scatterwalk_map_and_copy(scratch->dst, req->dst, 0, req->dlen,
157					 1);
158	}
159out:
160	spin_unlock(&scratch->lock);
161	return ret;
162}
163
164static int scomp_acomp_compress(struct acomp_req *req)
165{
166	return scomp_acomp_comp_decomp(req, 1);
167}
168
169static int scomp_acomp_decompress(struct acomp_req *req)
170{
171	return scomp_acomp_comp_decomp(req, 0);
172}
173
174static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
175{
176	struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
177
178	crypto_free_scomp(*ctx);
179
180	mutex_lock(&scomp_lock);
181	if (!--scomp_scratch_users)
182		crypto_scomp_free_scratches();
183	mutex_unlock(&scomp_lock);
184}
185
186int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
187{
188	struct crypto_alg *calg = tfm->__crt_alg;
189	struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
190	struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
191	struct crypto_scomp *scomp;
192
193	if (!crypto_mod_get(calg))
194		return -EAGAIN;
195
196	scomp = crypto_create_tfm(calg, &crypto_scomp_type);
197	if (IS_ERR(scomp)) {
198		crypto_mod_put(calg);
199		return PTR_ERR(scomp);
200	}
201
202	*ctx = scomp;
203	tfm->exit = crypto_exit_scomp_ops_async;
204
205	crt->compress = scomp_acomp_compress;
206	crt->decompress = scomp_acomp_decompress;
207	crt->dst_free = sgl_free;
208	crt->reqsize = sizeof(void *);
209
210	return 0;
211}
212
213struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
214{
215	struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
216	struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
217	struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
218	struct crypto_scomp *scomp = *tfm_ctx;
219	void *ctx;
220
221	ctx = crypto_scomp_alloc_ctx(scomp);
222	if (IS_ERR(ctx)) {
223		kfree(req);
224		return NULL;
225	}
226
227	*req->__ctx = ctx;
228
229	return req;
230}
231
232void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
233{
234	struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
235	struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
236	struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
237	struct crypto_scomp *scomp = *tfm_ctx;
238	void *ctx = *req->__ctx;
239
240	if (ctx)
241		crypto_scomp_free_ctx(scomp, ctx);
242}
243
244static const struct crypto_type crypto_scomp_type = {
245	.extsize = crypto_alg_extsize,
246	.init_tfm = crypto_scomp_init_tfm,
247#ifdef CONFIG_PROC_FS
248	.show = crypto_scomp_show,
249#endif
 
250	.report = crypto_scomp_report,
 
251	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
252	.maskset = CRYPTO_ALG_TYPE_MASK,
253	.type = CRYPTO_ALG_TYPE_SCOMPRESS,
254	.tfmsize = offsetof(struct crypto_scomp, base),
255};
256
257int crypto_register_scomp(struct scomp_alg *alg)
258{
259	struct crypto_alg *base = &alg->base;
 
 
260
261	base->cra_type = &crypto_scomp_type;
262	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
263	base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
264
265	return crypto_register_alg(base);
266}
267EXPORT_SYMBOL_GPL(crypto_register_scomp);
268
269void crypto_unregister_scomp(struct scomp_alg *alg)
270{
271	crypto_unregister_alg(&alg->base);
272}
273EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
274
275int crypto_register_scomps(struct scomp_alg *algs, int count)
276{
277	int i, ret;
278
279	for (i = 0; i < count; i++) {
280		ret = crypto_register_scomp(&algs[i]);
281		if (ret)
282			goto err;
283	}
284
285	return 0;
286
287err:
288	for (--i; i >= 0; --i)
289		crypto_unregister_scomp(&algs[i]);
290
291	return ret;
292}
293EXPORT_SYMBOL_GPL(crypto_register_scomps);
294
295void crypto_unregister_scomps(struct scomp_alg *algs, int count)
296{
297	int i;
298
299	for (i = count - 1; i >= 0; --i)
300		crypto_unregister_scomp(&algs[i]);
301}
302EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
303
304MODULE_LICENSE("GPL");
305MODULE_DESCRIPTION("Synchronous compression type");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  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>
  8 */
  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>
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/scatterlist.h>
 18#include <linux/seq_file.h>
 19#include <linux/slab.h>
 20#include <linux/string.h>
 
 
 21#include <linux/vmalloc.h>
 
 
 22#include <net/netlink.h>
 23
 24#include "compress.h"
 
 
 
 25
 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
 36static const struct crypto_type crypto_scomp_type;
 37static int scomp_scratch_users;
 38static DEFINE_MUTEX(scomp_lock);
 39
 40static int __maybe_unused crypto_scomp_report(
 41	struct sk_buff *skb, struct crypto_alg *alg)
 42{
 43	struct crypto_report_comp rscomp;
 44
 45	memset(&rscomp, 0, sizeof(rscomp));
 46
 47	strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
 48
 49	return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
 50		       sizeof(rscomp), &rscomp);
 51}
 
 
 
 
 
 
 52
 53static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
 54	__maybe_unused;
 55
 56static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
 57{
 58	seq_puts(m, "type         : scomp\n");
 59}
 60
 61static void crypto_scomp_free_scratches(void)
 62{
 63	struct scomp_scratch *scratch;
 64	int i;
 65
 66	for_each_possible_cpu(i) {
 67		scratch = per_cpu_ptr(&scomp_scratch, i);
 68
 69		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
 84		scratch = per_cpu_ptr(&scomp_scratch, i);
 85
 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;
 94	}
 95	return 0;
 96error:
 97	crypto_scomp_free_scratches();
 98	return -ENOMEM;
 99}
100
101static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
102{
103	int ret = 0;
104
105	mutex_lock(&scomp_lock);
106	if (!scomp_scratch_users++)
107		ret = crypto_scomp_alloc_scratches();
108	mutex_unlock(&scomp_lock);
109
110	return ret;
111}
112
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);
119	struct scomp_scratch *scratch;
120	void *src, *dst;
121	unsigned int dlen;
122	int ret;
123
124	if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
125		return -EINVAL;
126
127	if (req->dst && !req->dlen)
128		return -EINVAL;
129
130	if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
131		req->dlen = SCOMP_SCRATCH_SIZE;
132
133	dlen = req->dlen;
134
135	scratch = raw_cpu_ptr(&scomp_scratch);
136	spin_lock(&scratch->lock);
137
138	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;
148	else
149		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);
157	if (!ret) {
158		if (!req->dst) {
159			req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
160			if (!req->dst) {
161				ret = -ENOMEM;
162				goto out;
163			}
164		} else if (req->dlen > dlen) {
165			ret = -ENOSPC;
166			goto out;
167		}
168		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		}
 
 
179	}
180out:
181	spin_unlock(&scratch->lock);
182	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);
200
201	mutex_lock(&scomp_lock);
202	if (!--scomp_scratch_users)
203		crypto_scomp_free_scratches();
204	mutex_unlock(&scomp_lock);
205}
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;
228	crt->dst_free = sgl_free;
229	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
271#if IS_ENABLED(CONFIG_CRYPTO_USER)
272	.report = crypto_scomp_report,
273#endif
274	.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{
282	struct crypto_alg *base = &alg->calg.base;
283
284	comp_prepare_alg(&alg->calg);
285
286	base->cra_type = &crypto_scomp_type;
 
287	base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
288
289	return crypto_register_alg(base);
290}
291EXPORT_SYMBOL_GPL(crypto_register_scomp);
292
293void crypto_unregister_scomp(struct scomp_alg *alg)
294{
295	crypto_unregister_alg(&alg->base);
296}
297EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
298
299int 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
328MODULE_LICENSE("GPL");
329MODULE_DESCRIPTION("Synchronous compression type");