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