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