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 memset(&rscomp
, 0, sizeof(rscomp
));
45 strscpy(rscomp
.type
, "scomp", sizeof(rscomp
.type
));
47 return nla_put(skb
, CRYPTOCFGA_REPORT_COMPRESS
,
48 sizeof(rscomp
), &rscomp
);
51 static int crypto_scomp_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
57 static void crypto_scomp_show(struct seq_file
*m
, struct crypto_alg
*alg
)
60 static void crypto_scomp_show(struct seq_file
*m
, struct crypto_alg
*alg
)
62 seq_puts(m
, "type : scomp\n");
65 static void crypto_scomp_free_scratches(void * __percpu
*scratches
)
72 for_each_possible_cpu(i
)
73 vfree(*per_cpu_ptr(scratches
, i
));
75 free_percpu(scratches
);
78 static void * __percpu
*crypto_scomp_alloc_scratches(void)
80 void * __percpu
*scratches
;
83 scratches
= alloc_percpu(void *);
87 for_each_possible_cpu(i
) {
90 scratch
= vmalloc_node(SCOMP_SCRATCH_SIZE
, cpu_to_node(i
));
93 *per_cpu_ptr(scratches
, i
) = scratch
;
99 crypto_scomp_free_scratches(scratches
);
103 static void crypto_scomp_free_all_scratches(void)
105 if (!--scomp_scratch_users
) {
106 crypto_scomp_free_scratches(scomp_src_scratches
);
107 crypto_scomp_free_scratches(scomp_dst_scratches
);
108 scomp_src_scratches
= NULL
;
109 scomp_dst_scratches
= NULL
;
113 static int crypto_scomp_alloc_all_scratches(void)
115 if (!scomp_scratch_users
++) {
116 scomp_src_scratches
= crypto_scomp_alloc_scratches();
117 if (!scomp_src_scratches
)
119 scomp_dst_scratches
= crypto_scomp_alloc_scratches();
120 if (!scomp_dst_scratches
) {
121 crypto_scomp_free_scratches(scomp_src_scratches
);
122 scomp_src_scratches
= NULL
;
129 static int crypto_scomp_init_tfm(struct crypto_tfm
*tfm
)
133 mutex_lock(&scomp_lock
);
134 ret
= crypto_scomp_alloc_all_scratches();
135 mutex_unlock(&scomp_lock
);
140 static int scomp_acomp_comp_decomp(struct acomp_req
*req
, int dir
)
142 struct crypto_acomp
*tfm
= crypto_acomp_reqtfm(req
);
143 void **tfm_ctx
= acomp_tfm_ctx(tfm
);
144 struct crypto_scomp
*scomp
= *tfm_ctx
;
145 void **ctx
= acomp_request_ctx(req
);
146 const int cpu
= get_cpu();
147 u8
*scratch_src
= *per_cpu_ptr(scomp_src_scratches
, cpu
);
148 u8
*scratch_dst
= *per_cpu_ptr(scomp_dst_scratches
, cpu
);
151 if (!req
->src
|| !req
->slen
|| req
->slen
> SCOMP_SCRATCH_SIZE
) {
156 if (req
->dst
&& !req
->dlen
) {
161 if (!req
->dlen
|| req
->dlen
> SCOMP_SCRATCH_SIZE
)
162 req
->dlen
= SCOMP_SCRATCH_SIZE
;
164 scatterwalk_map_and_copy(scratch_src
, req
->src
, 0, req
->slen
, 0);
166 ret
= crypto_scomp_compress(scomp
, scratch_src
, req
->slen
,
167 scratch_dst
, &req
->dlen
, *ctx
);
169 ret
= crypto_scomp_decompress(scomp
, scratch_src
, req
->slen
,
170 scratch_dst
, &req
->dlen
, *ctx
);
173 req
->dst
= sgl_alloc(req
->dlen
, GFP_ATOMIC
, NULL
);
177 scatterwalk_map_and_copy(scratch_dst
, req
->dst
, 0, req
->dlen
,
185 static int scomp_acomp_compress(struct acomp_req
*req
)
187 return scomp_acomp_comp_decomp(req
, 1);
190 static int scomp_acomp_decompress(struct acomp_req
*req
)
192 return scomp_acomp_comp_decomp(req
, 0);
195 static void crypto_exit_scomp_ops_async(struct crypto_tfm
*tfm
)
197 struct crypto_scomp
**ctx
= crypto_tfm_ctx(tfm
);
199 crypto_free_scomp(*ctx
);
201 mutex_lock(&scomp_lock
);
202 crypto_scomp_free_all_scratches();
203 mutex_unlock(&scomp_lock
);
206 int crypto_init_scomp_ops_async(struct crypto_tfm
*tfm
)
208 struct crypto_alg
*calg
= tfm
->__crt_alg
;
209 struct crypto_acomp
*crt
= __crypto_acomp_tfm(tfm
);
210 struct crypto_scomp
**ctx
= crypto_tfm_ctx(tfm
);
211 struct crypto_scomp
*scomp
;
213 if (!crypto_mod_get(calg
))
216 scomp
= crypto_create_tfm(calg
, &crypto_scomp_type
);
218 crypto_mod_put(calg
);
219 return PTR_ERR(scomp
);
223 tfm
->exit
= crypto_exit_scomp_ops_async
;
225 crt
->compress
= scomp_acomp_compress
;
226 crt
->decompress
= scomp_acomp_decompress
;
227 crt
->dst_free
= sgl_free
;
228 crt
->reqsize
= sizeof(void *);
233 struct acomp_req
*crypto_acomp_scomp_alloc_ctx(struct acomp_req
*req
)
235 struct crypto_acomp
*acomp
= crypto_acomp_reqtfm(req
);
236 struct crypto_tfm
*tfm
= crypto_acomp_tfm(acomp
);
237 struct crypto_scomp
**tfm_ctx
= crypto_tfm_ctx(tfm
);
238 struct crypto_scomp
*scomp
= *tfm_ctx
;
241 ctx
= crypto_scomp_alloc_ctx(scomp
);
252 void crypto_acomp_scomp_free_ctx(struct acomp_req
*req
)
254 struct crypto_acomp
*acomp
= crypto_acomp_reqtfm(req
);
255 struct crypto_tfm
*tfm
= crypto_acomp_tfm(acomp
);
256 struct crypto_scomp
**tfm_ctx
= crypto_tfm_ctx(tfm
);
257 struct crypto_scomp
*scomp
= *tfm_ctx
;
258 void *ctx
= *req
->__ctx
;
261 crypto_scomp_free_ctx(scomp
, ctx
);
264 static const struct crypto_type crypto_scomp_type
= {
265 .extsize
= crypto_alg_extsize
,
266 .init_tfm
= crypto_scomp_init_tfm
,
267 #ifdef CONFIG_PROC_FS
268 .show
= crypto_scomp_show
,
270 .report
= crypto_scomp_report
,
271 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
272 .maskset
= CRYPTO_ALG_TYPE_MASK
,
273 .type
= CRYPTO_ALG_TYPE_SCOMPRESS
,
274 .tfmsize
= offsetof(struct crypto_scomp
, base
),
277 int crypto_register_scomp(struct scomp_alg
*alg
)
279 struct crypto_alg
*base
= &alg
->base
;
281 base
->cra_type
= &crypto_scomp_type
;
282 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
283 base
->cra_flags
|= CRYPTO_ALG_TYPE_SCOMPRESS
;
285 return crypto_register_alg(base
);
287 EXPORT_SYMBOL_GPL(crypto_register_scomp
);
289 int crypto_unregister_scomp(struct scomp_alg
*alg
)
291 return crypto_unregister_alg(&alg
->base
);
293 EXPORT_SYMBOL_GPL(crypto_unregister_scomp
);
295 int crypto_register_scomps(struct scomp_alg
*algs
, int count
)
299 for (i
= 0; i
< count
; i
++) {
300 ret
= crypto_register_scomp(&algs
[i
]);
308 for (--i
; i
>= 0; --i
)
309 crypto_unregister_scomp(&algs
[i
]);
313 EXPORT_SYMBOL_GPL(crypto_register_scomps
);
315 void crypto_unregister_scomps(struct scomp_alg
*algs
, int count
)
319 for (i
= count
- 1; i
>= 0; --i
)
320 crypto_unregister_scomp(&algs
[i
]);
322 EXPORT_SYMBOL_GPL(crypto_unregister_scomps
);
324 MODULE_LICENSE("GPL");
325 MODULE_DESCRIPTION("Synchronous compression type");