Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Synchronous Compression operations |
| 3 | * |
| 4 | * Copyright 2015 LG Electronics Inc. |
| 5 | * Copyright (c) 2016, Intel Corporation |
| 6 | * Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com> |
| 7 | * |
| 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) |
| 11 | * any later version. |
| 12 | * |
| 13 | */ |
| 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> |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 21 | #include <linux/compiler.h> |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 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> |
| 30 | #include "internal.h" |
| 31 | |
| 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); |
| 37 | |
| 38 | #ifdef CONFIG_NET |
| 39 | static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 40 | { |
| 41 | struct crypto_report_comp rscomp; |
| 42 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 43 | memset(&rscomp, 0, sizeof(rscomp)); |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 44 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 45 | strscpy(rscomp.type, "scomp", sizeof(rscomp.type)); |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 46 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 47 | return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS, |
| 48 | sizeof(rscomp), &rscomp); |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 49 | } |
| 50 | #else |
| 51 | static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg) |
| 52 | { |
| 53 | return -ENOSYS; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg) |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 58 | __maybe_unused; |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 59 | |
| 60 | static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg) |
| 61 | { |
| 62 | seq_puts(m, "type : scomp\n"); |
| 63 | } |
| 64 | |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 65 | static void crypto_scomp_free_scratches(void * __percpu *scratches) |
| 66 | { |
| 67 | int i; |
| 68 | |
| 69 | if (!scratches) |
| 70 | return; |
| 71 | |
| 72 | for_each_possible_cpu(i) |
| 73 | vfree(*per_cpu_ptr(scratches, i)); |
| 74 | |
| 75 | free_percpu(scratches); |
| 76 | } |
| 77 | |
| 78 | static void * __percpu *crypto_scomp_alloc_scratches(void) |
| 79 | { |
| 80 | void * __percpu *scratches; |
| 81 | int i; |
| 82 | |
| 83 | scratches = alloc_percpu(void *); |
| 84 | if (!scratches) |
| 85 | return NULL; |
| 86 | |
| 87 | for_each_possible_cpu(i) { |
| 88 | void *scratch; |
| 89 | |
| 90 | scratch = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i)); |
| 91 | if (!scratch) |
| 92 | goto error; |
| 93 | *per_cpu_ptr(scratches, i) = scratch; |
| 94 | } |
| 95 | |
| 96 | return scratches; |
| 97 | |
| 98 | error: |
| 99 | crypto_scomp_free_scratches(scratches); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | static void crypto_scomp_free_all_scratches(void) |
| 104 | { |
| 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; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static int crypto_scomp_alloc_all_scratches(void) |
| 114 | { |
| 115 | if (!scomp_scratch_users++) { |
| 116 | scomp_src_scratches = crypto_scomp_alloc_scratches(); |
| 117 | if (!scomp_src_scratches) |
| 118 | return -ENOMEM; |
| 119 | scomp_dst_scratches = crypto_scomp_alloc_scratches(); |
Ard Biesheuvel | cc4d110 | 2017-07-21 16:42:37 +0100 | [diff] [blame] | 120 | if (!scomp_dst_scratches) { |
| 121 | crypto_scomp_free_scratches(scomp_src_scratches); |
| 122 | scomp_src_scratches = NULL; |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 123 | return -ENOMEM; |
Ard Biesheuvel | cc4d110 | 2017-07-21 16:42:37 +0100 | [diff] [blame] | 124 | } |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
Ard Biesheuvel | 6a8487a | 2017-07-21 16:42:38 +0100 | [diff] [blame] | 129 | static int crypto_scomp_init_tfm(struct crypto_tfm *tfm) |
| 130 | { |
| 131 | int ret; |
| 132 | |
| 133 | mutex_lock(&scomp_lock); |
| 134 | ret = crypto_scomp_alloc_all_scratches(); |
| 135 | mutex_unlock(&scomp_lock); |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 140 | static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir) |
| 141 | { |
| 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); |
| 149 | int ret; |
| 150 | |
| 151 | if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE) { |
| 152 | ret = -EINVAL; |
| 153 | goto out; |
| 154 | } |
| 155 | |
| 156 | if (req->dst && !req->dlen) { |
| 157 | ret = -EINVAL; |
| 158 | goto out; |
| 159 | } |
| 160 | |
| 161 | if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE) |
| 162 | req->dlen = SCOMP_SCRATCH_SIZE; |
| 163 | |
| 164 | scatterwalk_map_and_copy(scratch_src, req->src, 0, req->slen, 0); |
| 165 | if (dir) |
| 166 | ret = crypto_scomp_compress(scomp, scratch_src, req->slen, |
| 167 | scratch_dst, &req->dlen, *ctx); |
| 168 | else |
| 169 | ret = crypto_scomp_decompress(scomp, scratch_src, req->slen, |
| 170 | scratch_dst, &req->dlen, *ctx); |
| 171 | if (!ret) { |
| 172 | if (!req->dst) { |
Bart Van Assche | 8cd579d | 2018-01-05 08:26:47 -0800 | [diff] [blame] | 173 | req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL); |
Sebastian Andrzej Siewior | 6a4d1b1 | 2019-03-29 14:09:55 +0100 | [diff] [blame^] | 174 | if (!req->dst) { |
| 175 | ret = -ENOMEM; |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 176 | goto out; |
Sebastian Andrzej Siewior | 6a4d1b1 | 2019-03-29 14:09:55 +0100 | [diff] [blame^] | 177 | } |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 178 | } |
| 179 | scatterwalk_map_and_copy(scratch_dst, req->dst, 0, req->dlen, |
| 180 | 1); |
| 181 | } |
| 182 | out: |
| 183 | put_cpu(); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | static int scomp_acomp_compress(struct acomp_req *req) |
| 188 | { |
| 189 | return scomp_acomp_comp_decomp(req, 1); |
| 190 | } |
| 191 | |
| 192 | static int scomp_acomp_decompress(struct acomp_req *req) |
| 193 | { |
| 194 | return scomp_acomp_comp_decomp(req, 0); |
| 195 | } |
| 196 | |
| 197 | static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm) |
| 198 | { |
| 199 | struct crypto_scomp **ctx = crypto_tfm_ctx(tfm); |
| 200 | |
| 201 | crypto_free_scomp(*ctx); |
Ard Biesheuvel | 6a8487a | 2017-07-21 16:42:38 +0100 | [diff] [blame] | 202 | |
| 203 | mutex_lock(&scomp_lock); |
| 204 | crypto_scomp_free_all_scratches(); |
| 205 | mutex_unlock(&scomp_lock); |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | int crypto_init_scomp_ops_async(struct crypto_tfm *tfm) |
| 209 | { |
| 210 | struct crypto_alg *calg = tfm->__crt_alg; |
| 211 | struct crypto_acomp *crt = __crypto_acomp_tfm(tfm); |
| 212 | struct crypto_scomp **ctx = crypto_tfm_ctx(tfm); |
| 213 | struct crypto_scomp *scomp; |
| 214 | |
| 215 | if (!crypto_mod_get(calg)) |
| 216 | return -EAGAIN; |
| 217 | |
| 218 | scomp = crypto_create_tfm(calg, &crypto_scomp_type); |
| 219 | if (IS_ERR(scomp)) { |
| 220 | crypto_mod_put(calg); |
| 221 | return PTR_ERR(scomp); |
| 222 | } |
| 223 | |
| 224 | *ctx = scomp; |
| 225 | tfm->exit = crypto_exit_scomp_ops_async; |
| 226 | |
| 227 | crt->compress = scomp_acomp_compress; |
| 228 | crt->decompress = scomp_acomp_decompress; |
Bart Van Assche | 8cd579d | 2018-01-05 08:26:47 -0800 | [diff] [blame] | 229 | crt->dst_free = sgl_free; |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 230 | crt->reqsize = sizeof(void *); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req) |
| 236 | { |
| 237 | struct crypto_acomp *acomp = crypto_acomp_reqtfm(req); |
| 238 | struct crypto_tfm *tfm = crypto_acomp_tfm(acomp); |
| 239 | struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm); |
| 240 | struct crypto_scomp *scomp = *tfm_ctx; |
| 241 | void *ctx; |
| 242 | |
| 243 | ctx = crypto_scomp_alloc_ctx(scomp); |
| 244 | if (IS_ERR(ctx)) { |
| 245 | kfree(req); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | *req->__ctx = ctx; |
| 250 | |
| 251 | return req; |
| 252 | } |
| 253 | |
| 254 | void crypto_acomp_scomp_free_ctx(struct acomp_req *req) |
| 255 | { |
| 256 | struct crypto_acomp *acomp = crypto_acomp_reqtfm(req); |
| 257 | struct crypto_tfm *tfm = crypto_acomp_tfm(acomp); |
| 258 | struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm); |
| 259 | struct crypto_scomp *scomp = *tfm_ctx; |
| 260 | void *ctx = *req->__ctx; |
| 261 | |
| 262 | if (ctx) |
| 263 | crypto_scomp_free_ctx(scomp, ctx); |
| 264 | } |
| 265 | |
| 266 | static const struct crypto_type crypto_scomp_type = { |
| 267 | .extsize = crypto_alg_extsize, |
| 268 | .init_tfm = crypto_scomp_init_tfm, |
| 269 | #ifdef CONFIG_PROC_FS |
| 270 | .show = crypto_scomp_show, |
| 271 | #endif |
| 272 | .report = crypto_scomp_report, |
| 273 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
| 274 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 275 | .type = CRYPTO_ALG_TYPE_SCOMPRESS, |
| 276 | .tfmsize = offsetof(struct crypto_scomp, base), |
| 277 | }; |
| 278 | |
| 279 | int crypto_register_scomp(struct scomp_alg *alg) |
| 280 | { |
| 281 | struct crypto_alg *base = &alg->base; |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 282 | |
| 283 | base->cra_type = &crypto_scomp_type; |
| 284 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 285 | base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS; |
| 286 | |
Ard Biesheuvel | 6a8487a | 2017-07-21 16:42:38 +0100 | [diff] [blame] | 287 | return crypto_register_alg(base); |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 288 | } |
| 289 | EXPORT_SYMBOL_GPL(crypto_register_scomp); |
| 290 | |
| 291 | int crypto_unregister_scomp(struct scomp_alg *alg) |
| 292 | { |
Ard Biesheuvel | 6a8487a | 2017-07-21 16:42:38 +0100 | [diff] [blame] | 293 | return crypto_unregister_alg(&alg->base); |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 294 | } |
| 295 | EXPORT_SYMBOL_GPL(crypto_unregister_scomp); |
| 296 | |
Giovanni Cabiddu | 3de4f5e | 2017-04-21 21:54:29 +0100 | [diff] [blame] | 297 | int crypto_register_scomps(struct scomp_alg *algs, int count) |
| 298 | { |
| 299 | int i, ret; |
| 300 | |
| 301 | for (i = 0; i < count; i++) { |
| 302 | ret = crypto_register_scomp(&algs[i]); |
| 303 | if (ret) |
| 304 | goto err; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | |
| 309 | err: |
| 310 | for (--i; i >= 0; --i) |
| 311 | crypto_unregister_scomp(&algs[i]); |
| 312 | |
| 313 | return ret; |
| 314 | } |
| 315 | EXPORT_SYMBOL_GPL(crypto_register_scomps); |
| 316 | |
| 317 | void crypto_unregister_scomps(struct scomp_alg *algs, int count) |
| 318 | { |
| 319 | int i; |
| 320 | |
| 321 | for (i = count - 1; i >= 0; --i) |
| 322 | crypto_unregister_scomp(&algs[i]); |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(crypto_unregister_scomps); |
| 325 | |
Giovanni Cabiddu | 1ab53a7 | 2016-10-21 13:19:48 +0100 | [diff] [blame] | 326 | MODULE_LICENSE("GPL"); |
| 327 | MODULE_DESCRIPTION("Synchronous compression type"); |