blob: 0edf5b54fc778d6f2f159821496f8bac267bea50 [file] [log] [blame]
Thomas Gleixnera61127c2019-05-29 16:57:49 -07001// SPDX-License-Identifier: GPL-2.0-only
Steffen Klassert5068c7a2010-01-07 15:57:19 +11002/*
3 * pcrypt - Parallel crypto wrapper.
4 *
5 * Copyright (C) 2009 secunet Security Networks AG
6 * Copyright (C) 2009 Steffen Klassert <steffen.klassert@secunet.com>
Steffen Klassert5068c7a2010-01-07 15:57:19 +11007 */
8
9#include <crypto/algapi.h>
10#include <crypto/internal/aead.h>
Herbert Xua5a22e52015-05-22 20:34:22 +080011#include <linux/atomic.h>
Steffen Klassert5068c7a2010-01-07 15:57:19 +110012#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/slab.h>
Dan Kruchinine15bacb2010-07-14 14:31:57 +040016#include <linux/notifier.h>
Dan Kruchinina3fb1e32010-07-14 14:34:15 +040017#include <linux/kobject.h>
Steffen Klassertd3f64e42010-07-27 07:18:46 +020018#include <linux/cpu.h>
Steffen Klassert5068c7a2010-01-07 15:57:19 +110019#include <crypto/pcrypt.h>
20
Steffen Klassertc57e8422010-07-27 07:16:33 +020021struct padata_pcrypt {
Dan Kruchinine15bacb2010-07-14 14:31:57 +040022 struct padata_instance *pinst;
23 struct workqueue_struct *wq;
24
25 /*
26 * Cpumask for callback CPUs. It should be
27 * equal to serial cpumask of corresponding padata instance,
28 * so it is updated when padata notifies us about serial
29 * cpumask change.
30 *
31 * cb_cpumask is protected by RCU. This fact prevents us from
32 * using cpumask_var_t directly because the actual type of
33 * cpumsak_var_t depends on kernel configuration(particularly on
34 * CONFIG_CPUMASK_OFFSTACK macro). Depending on the configuration
35 * cpumask_var_t may be either a pointer to the struct cpumask
36 * or a variable allocated on the stack. Thus we can not safely use
37 * cpumask_var_t with RCU operations such as rcu_assign_pointer or
38 * rcu_dereference. So cpumask_var_t is wrapped with struct
39 * pcrypt_cpumask which makes possible to use it with RCU.
40 */
41 struct pcrypt_cpumask {
42 cpumask_var_t mask;
43 } *cb_cpumask;
44 struct notifier_block nblock;
45};
46
Steffen Klassertc57e8422010-07-27 07:16:33 +020047static struct padata_pcrypt pencrypt;
48static struct padata_pcrypt pdecrypt;
Dan Kruchinina3fb1e32010-07-14 14:34:15 +040049static struct kset *pcrypt_kset;
Steffen Klassert5068c7a2010-01-07 15:57:19 +110050
51struct pcrypt_instance_ctx {
Herbert Xu66d948e2015-05-21 15:10:58 +080052 struct crypto_aead_spawn spawn;
Herbert Xua5a22e52015-05-22 20:34:22 +080053 atomic_t tfm_count;
Steffen Klassert5068c7a2010-01-07 15:57:19 +110054};
55
56struct pcrypt_aead_ctx {
57 struct crypto_aead *child;
58 unsigned int cb_cpu;
59};
60
61static int pcrypt_do_parallel(struct padata_priv *padata, unsigned int *cb_cpu,
Steffen Klassertc57e8422010-07-27 07:16:33 +020062 struct padata_pcrypt *pcrypt)
Steffen Klassert5068c7a2010-01-07 15:57:19 +110063{
64 unsigned int cpu_index, cpu, i;
Dan Kruchinine15bacb2010-07-14 14:31:57 +040065 struct pcrypt_cpumask *cpumask;
Steffen Klassert5068c7a2010-01-07 15:57:19 +110066
67 cpu = *cb_cpu;
68
Dan Kruchinine15bacb2010-07-14 14:31:57 +040069 rcu_read_lock_bh();
Mathias Krause3110e402013-11-28 19:20:04 +010070 cpumask = rcu_dereference_bh(pcrypt->cb_cpumask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +040071 if (cpumask_test_cpu(cpu, cpumask->mask))
Steffen Klassert5068c7a2010-01-07 15:57:19 +110072 goto out;
73
Steffen Klassertcc74f4b2010-07-20 08:52:20 +020074 if (!cpumask_weight(cpumask->mask))
75 goto out;
76
Dan Kruchinine15bacb2010-07-14 14:31:57 +040077 cpu_index = cpu % cpumask_weight(cpumask->mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +110078
Dan Kruchinine15bacb2010-07-14 14:31:57 +040079 cpu = cpumask_first(cpumask->mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +110080 for (i = 0; i < cpu_index; i++)
Dan Kruchinine15bacb2010-07-14 14:31:57 +040081 cpu = cpumask_next(cpu, cpumask->mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +110082
83 *cb_cpu = cpu;
84
85out:
Dan Kruchinine15bacb2010-07-14 14:31:57 +040086 rcu_read_unlock_bh();
87 return padata_do_parallel(pcrypt->pinst, padata, cpu);
Steffen Klassert5068c7a2010-01-07 15:57:19 +110088}
89
90static int pcrypt_aead_setkey(struct crypto_aead *parent,
91 const u8 *key, unsigned int keylen)
92{
93 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
94
95 return crypto_aead_setkey(ctx->child, key, keylen);
96}
97
98static int pcrypt_aead_setauthsize(struct crypto_aead *parent,
99 unsigned int authsize)
100{
101 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(parent);
102
103 return crypto_aead_setauthsize(ctx->child, authsize);
104}
105
106static void pcrypt_aead_serial(struct padata_priv *padata)
107{
108 struct pcrypt_request *preq = pcrypt_padata_request(padata);
109 struct aead_request *req = pcrypt_request_ctx(preq);
110
111 aead_request_complete(req->base.data, padata->info);
112}
113
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100114static void pcrypt_aead_done(struct crypto_async_request *areq, int err)
115{
116 struct aead_request *req = areq->data;
117 struct pcrypt_request *preq = aead_request_ctx(req);
118 struct padata_priv *padata = pcrypt_request_padata(preq);
119
120 padata->info = err;
121 req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
122
123 padata_do_serial(padata);
124}
125
126static void pcrypt_aead_enc(struct padata_priv *padata)
127{
128 struct pcrypt_request *preq = pcrypt_padata_request(padata);
129 struct aead_request *req = pcrypt_request_ctx(preq);
130
131 padata->info = crypto_aead_encrypt(req);
132
Steffen Klassert5a1436b2010-02-04 11:40:17 +1100133 if (padata->info == -EINPROGRESS)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100134 return;
135
136 padata_do_serial(padata);
137}
138
139static int pcrypt_aead_encrypt(struct aead_request *req)
140{
141 int err;
142 struct pcrypt_request *preq = aead_request_ctx(req);
143 struct aead_request *creq = pcrypt_request_ctx(preq);
144 struct padata_priv *padata = pcrypt_request_padata(preq);
145 struct crypto_aead *aead = crypto_aead_reqtfm(req);
146 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
147 u32 flags = aead_request_flags(req);
148
149 memset(padata, 0, sizeof(struct padata_priv));
150
151 padata->parallel = pcrypt_aead_enc;
152 padata->serial = pcrypt_aead_serial;
153
154 aead_request_set_tfm(creq, ctx->child);
155 aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
156 pcrypt_aead_done, req);
157 aead_request_set_crypt(creq, req->src, req->dst,
158 req->cryptlen, req->iv);
Herbert Xu0496f562015-05-28 22:08:00 +0800159 aead_request_set_ad(creq, req->assoclen);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100160
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400161 err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pencrypt);
Steffen Klassert83f619f2010-07-07 15:32:02 +0200162 if (!err)
163 return -EINPROGRESS;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100164
165 return err;
166}
167
168static void pcrypt_aead_dec(struct padata_priv *padata)
169{
170 struct pcrypt_request *preq = pcrypt_padata_request(padata);
171 struct aead_request *req = pcrypt_request_ctx(preq);
172
173 padata->info = crypto_aead_decrypt(req);
174
Steffen Klassert5a1436b2010-02-04 11:40:17 +1100175 if (padata->info == -EINPROGRESS)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100176 return;
177
178 padata_do_serial(padata);
179}
180
181static int pcrypt_aead_decrypt(struct aead_request *req)
182{
183 int err;
184 struct pcrypt_request *preq = aead_request_ctx(req);
185 struct aead_request *creq = pcrypt_request_ctx(preq);
186 struct padata_priv *padata = pcrypt_request_padata(preq);
187 struct crypto_aead *aead = crypto_aead_reqtfm(req);
188 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(aead);
189 u32 flags = aead_request_flags(req);
190
191 memset(padata, 0, sizeof(struct padata_priv));
192
193 padata->parallel = pcrypt_aead_dec;
194 padata->serial = pcrypt_aead_serial;
195
196 aead_request_set_tfm(creq, ctx->child);
197 aead_request_set_callback(creq, flags & ~CRYPTO_TFM_REQ_MAY_SLEEP,
198 pcrypt_aead_done, req);
199 aead_request_set_crypt(creq, req->src, req->dst,
200 req->cryptlen, req->iv);
Herbert Xu0496f562015-05-28 22:08:00 +0800201 aead_request_set_ad(creq, req->assoclen);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100202
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400203 err = pcrypt_do_parallel(padata, &ctx->cb_cpu, &pdecrypt);
Steffen Klassert83f619f2010-07-07 15:32:02 +0200204 if (!err)
205 return -EINPROGRESS;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100206
207 return err;
208}
209
Herbert Xu0496f562015-05-28 22:08:00 +0800210static int pcrypt_aead_init_tfm(struct crypto_aead *tfm)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100211{
212 int cpu, cpu_index;
Herbert Xu0496f562015-05-28 22:08:00 +0800213 struct aead_instance *inst = aead_alg_instance(tfm);
214 struct pcrypt_instance_ctx *ictx = aead_instance_ctx(inst);
215 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100216 struct crypto_aead *cipher;
217
Herbert Xua5a22e52015-05-22 20:34:22 +0800218 cpu_index = (unsigned int)atomic_inc_return(&ictx->tfm_count) %
219 cpumask_weight(cpu_online_mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100220
Steffen Klassertfbf0ca12012-03-28 08:51:03 +0200221 ctx->cb_cpu = cpumask_first(cpu_online_mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100222 for (cpu = 0; cpu < cpu_index; cpu++)
Steffen Klassertfbf0ca12012-03-28 08:51:03 +0200223 ctx->cb_cpu = cpumask_next(ctx->cb_cpu, cpu_online_mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100224
Herbert Xu0496f562015-05-28 22:08:00 +0800225 cipher = crypto_spawn_aead(&ictx->spawn);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100226
227 if (IS_ERR(cipher))
228 return PTR_ERR(cipher);
229
230 ctx->child = cipher;
Herbert Xu0496f562015-05-28 22:08:00 +0800231 crypto_aead_set_reqsize(tfm, sizeof(struct pcrypt_request) +
232 sizeof(struct aead_request) +
233 crypto_aead_reqsize(cipher));
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100234
235 return 0;
236}
237
Herbert Xu0496f562015-05-28 22:08:00 +0800238static void pcrypt_aead_exit_tfm(struct crypto_aead *tfm)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100239{
Herbert Xu0496f562015-05-28 22:08:00 +0800240 struct pcrypt_aead_ctx *ctx = crypto_aead_ctx(tfm);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100241
242 crypto_free_aead(ctx->child);
243}
244
Eric Biggersd76c6812017-12-20 14:28:25 -0800245static void pcrypt_free(struct aead_instance *inst)
246{
247 struct pcrypt_instance_ctx *ctx = aead_instance_ctx(inst);
248
249 crypto_drop_aead(&ctx->spawn);
250 kfree(inst);
251}
252
Herbert Xu66d948e2015-05-21 15:10:58 +0800253static int pcrypt_init_instance(struct crypto_instance *inst,
254 struct crypto_alg *alg)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100255{
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100256 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
257 "pcrypt(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
Herbert Xu66d948e2015-05-21 15:10:58 +0800258 return -ENAMETOOLONG;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100259
260 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
261
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100262 inst->alg.cra_priority = alg->cra_priority + 100;
263 inst->alg.cra_blocksize = alg->cra_blocksize;
264 inst->alg.cra_alignmask = alg->cra_alignmask;
265
Herbert Xu66d948e2015-05-21 15:10:58 +0800266 return 0;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100267}
268
Herbert Xu0496f562015-05-28 22:08:00 +0800269static int pcrypt_create_aead(struct crypto_template *tmpl, struct rtattr **tb,
270 u32 type, u32 mask)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100271{
Herbert Xu66d948e2015-05-21 15:10:58 +0800272 struct pcrypt_instance_ctx *ctx;
Herbert Xu846f97d2015-07-09 07:17:18 +0800273 struct crypto_attr_type *algt;
Herbert Xu0496f562015-05-28 22:08:00 +0800274 struct aead_instance *inst;
275 struct aead_alg *alg;
Herbert Xu66d948e2015-05-21 15:10:58 +0800276 const char *name;
277 int err;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100278
Herbert Xu846f97d2015-07-09 07:17:18 +0800279 algt = crypto_get_attr_type(tb);
280 if (IS_ERR(algt))
281 return PTR_ERR(algt);
282
Herbert Xu66d948e2015-05-21 15:10:58 +0800283 name = crypto_attr_alg_name(tb[1]);
284 if (IS_ERR(name))
Herbert Xu0496f562015-05-28 22:08:00 +0800285 return PTR_ERR(name);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100286
Herbert Xu66d948e2015-05-21 15:10:58 +0800287 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
288 if (!inst)
Herbert Xu0496f562015-05-28 22:08:00 +0800289 return -ENOMEM;
Herbert Xu66d948e2015-05-21 15:10:58 +0800290
Herbert Xu0496f562015-05-28 22:08:00 +0800291 ctx = aead_instance_ctx(inst);
292 crypto_set_aead_spawn(&ctx->spawn, aead_crypto_instance(inst));
Herbert Xu66d948e2015-05-21 15:10:58 +0800293
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800294 err = crypto_grab_aead(&ctx->spawn, name, 0, 0);
Herbert Xu66d948e2015-05-21 15:10:58 +0800295 if (err)
296 goto out_free_inst;
297
Herbert Xu0496f562015-05-28 22:08:00 +0800298 alg = crypto_spawn_aead_alg(&ctx->spawn);
299 err = pcrypt_init_instance(aead_crypto_instance(inst), &alg->base);
Herbert Xu66d948e2015-05-21 15:10:58 +0800300 if (err)
301 goto out_drop_aead;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100302
Herbert Xu846f97d2015-07-09 07:17:18 +0800303 inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC;
Herbert Xu846f97d2015-07-09 07:17:18 +0800304
Herbert Xu0496f562015-05-28 22:08:00 +0800305 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
306 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100307
Herbert Xu0496f562015-05-28 22:08:00 +0800308 inst->alg.base.cra_ctxsize = sizeof(struct pcrypt_aead_ctx);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100309
Herbert Xu0496f562015-05-28 22:08:00 +0800310 inst->alg.init = pcrypt_aead_init_tfm;
311 inst->alg.exit = pcrypt_aead_exit_tfm;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100312
Herbert Xu0496f562015-05-28 22:08:00 +0800313 inst->alg.setkey = pcrypt_aead_setkey;
314 inst->alg.setauthsize = pcrypt_aead_setauthsize;
315 inst->alg.encrypt = pcrypt_aead_encrypt;
316 inst->alg.decrypt = pcrypt_aead_decrypt;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100317
Eric Biggersd76c6812017-12-20 14:28:25 -0800318 inst->free = pcrypt_free;
319
Herbert Xu0496f562015-05-28 22:08:00 +0800320 err = aead_register_instance(tmpl, inst);
321 if (err)
322 goto out_drop_aead;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100323
Herbert Xu66d948e2015-05-21 15:10:58 +0800324out:
Herbert Xu0496f562015-05-28 22:08:00 +0800325 return err;
Herbert Xu66d948e2015-05-21 15:10:58 +0800326
327out_drop_aead:
328 crypto_drop_aead(&ctx->spawn);
329out_free_inst:
330 kfree(inst);
Herbert Xu66d948e2015-05-21 15:10:58 +0800331 goto out;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100332}
333
Herbert Xu0496f562015-05-28 22:08:00 +0800334static int pcrypt_create(struct crypto_template *tmpl, struct rtattr **tb)
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100335{
336 struct crypto_attr_type *algt;
337
338 algt = crypto_get_attr_type(tb);
339 if (IS_ERR(algt))
Herbert Xu0496f562015-05-28 22:08:00 +0800340 return PTR_ERR(algt);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100341
342 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
343 case CRYPTO_ALG_TYPE_AEAD:
Herbert Xu0496f562015-05-28 22:08:00 +0800344 return pcrypt_create_aead(tmpl, tb, algt->type, algt->mask);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100345 }
346
Herbert Xu0496f562015-05-28 22:08:00 +0800347 return -EINVAL;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100348}
349
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400350static int pcrypt_cpumask_change_notify(struct notifier_block *self,
351 unsigned long val, void *data)
352{
Steffen Klassertc57e8422010-07-27 07:16:33 +0200353 struct padata_pcrypt *pcrypt;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400354 struct pcrypt_cpumask *new_mask, *old_mask;
Steffen Klassertd3f64e42010-07-27 07:18:46 +0200355 struct padata_cpumask *cpumask = (struct padata_cpumask *)data;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400356
357 if (!(val & PADATA_CPU_SERIAL))
358 return 0;
359
Steffen Klassertc57e8422010-07-27 07:16:33 +0200360 pcrypt = container_of(self, struct padata_pcrypt, nblock);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400361 new_mask = kmalloc(sizeof(*new_mask), GFP_KERNEL);
362 if (!new_mask)
363 return -ENOMEM;
364 if (!alloc_cpumask_var(&new_mask->mask, GFP_KERNEL)) {
365 kfree(new_mask);
366 return -ENOMEM;
367 }
368
369 old_mask = pcrypt->cb_cpumask;
370
Steffen Klassertd3f64e42010-07-27 07:18:46 +0200371 cpumask_copy(new_mask->mask, cpumask->cbcpu);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400372 rcu_assign_pointer(pcrypt->cb_cpumask, new_mask);
Paul E. McKenneya0076e12018-11-05 16:57:40 -0800373 synchronize_rcu();
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400374
375 free_cpumask_var(old_mask->mask);
376 kfree(old_mask);
377 return 0;
378}
379
Dan Kruchinina3fb1e32010-07-14 14:34:15 +0400380static int pcrypt_sysfs_add(struct padata_instance *pinst, const char *name)
381{
382 int ret;
383
384 pinst->kobj.kset = pcrypt_kset;
Colin Ian Kingb1e38742018-10-27 15:49:26 +0100385 ret = kobject_add(&pinst->kobj, NULL, "%s", name);
Dan Kruchinina3fb1e32010-07-14 14:34:15 +0400386 if (!ret)
387 kobject_uevent(&pinst->kobj, KOBJ_ADD);
388
389 return ret;
390}
391
Steffen Klassertc57e8422010-07-27 07:16:33 +0200392static int pcrypt_init_padata(struct padata_pcrypt *pcrypt,
393 const char *name)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400394{
395 int ret = -ENOMEM;
396 struct pcrypt_cpumask *mask;
397
Steffen Klassertd3f64e42010-07-27 07:18:46 +0200398 get_online_cpus();
399
Kees Cookd8537542013-07-03 15:04:57 -0700400 pcrypt->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
401 1, name);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400402 if (!pcrypt->wq)
403 goto err;
404
Steffen Klasserte6cc1172010-07-27 07:14:28 +0200405 pcrypt->pinst = padata_alloc_possible(pcrypt->wq);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400406 if (!pcrypt->pinst)
407 goto err_destroy_workqueue;
408
409 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
410 if (!mask)
411 goto err_free_padata;
412 if (!alloc_cpumask_var(&mask->mask, GFP_KERNEL)) {
413 kfree(mask);
414 goto err_free_padata;
415 }
416
Steffen Klassertfbf0ca12012-03-28 08:51:03 +0200417 cpumask_and(mask->mask, cpu_possible_mask, cpu_online_mask);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400418 rcu_assign_pointer(pcrypt->cb_cpumask, mask);
419
420 pcrypt->nblock.notifier_call = pcrypt_cpumask_change_notify;
421 ret = padata_register_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
422 if (ret)
423 goto err_free_cpumask;
424
Dan Kruchinina3fb1e32010-07-14 14:34:15 +0400425 ret = pcrypt_sysfs_add(pcrypt->pinst, name);
426 if (ret)
427 goto err_unregister_notifier;
428
Steffen Klassertd3f64e42010-07-27 07:18:46 +0200429 put_online_cpus();
430
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400431 return ret;
Steffen Klassertd3f64e42010-07-27 07:18:46 +0200432
Dan Kruchinina3fb1e32010-07-14 14:34:15 +0400433err_unregister_notifier:
434 padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400435err_free_cpumask:
436 free_cpumask_var(mask->mask);
437 kfree(mask);
438err_free_padata:
439 padata_free(pcrypt->pinst);
440err_destroy_workqueue:
441 destroy_workqueue(pcrypt->wq);
442err:
Steffen Klassertd3f64e42010-07-27 07:18:46 +0200443 put_online_cpus();
444
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400445 return ret;
446}
447
Steffen Klassertc57e8422010-07-27 07:16:33 +0200448static void pcrypt_fini_padata(struct padata_pcrypt *pcrypt)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400449{
450 free_cpumask_var(pcrypt->cb_cpumask->mask);
451 kfree(pcrypt->cb_cpumask);
452
453 padata_stop(pcrypt->pinst);
454 padata_unregister_cpumask_notifier(pcrypt->pinst, &pcrypt->nblock);
455 destroy_workqueue(pcrypt->wq);
456 padata_free(pcrypt->pinst);
457}
458
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100459static struct crypto_template pcrypt_tmpl = {
460 .name = "pcrypt",
Herbert Xu0496f562015-05-28 22:08:00 +0800461 .create = pcrypt_create,
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100462 .module = THIS_MODULE,
463};
464
465static int __init pcrypt_init(void)
466{
Dan Kruchinina3fb1e32010-07-14 14:34:15 +0400467 int err = -ENOMEM;
468
469 pcrypt_kset = kset_create_and_add("pcrypt", NULL, kernel_kobj);
470 if (!pcrypt_kset)
471 goto err;
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400472
Steffen Klassertc57e8422010-07-27 07:16:33 +0200473 err = pcrypt_init_padata(&pencrypt, "pencrypt");
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400474 if (err)
Dan Kruchinina3fb1e32010-07-14 14:34:15 +0400475 goto err_unreg_kset;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100476
Steffen Klassertc57e8422010-07-27 07:16:33 +0200477 err = pcrypt_init_padata(&pdecrypt, "pdecrypt");
Steffen Klassert4c879172010-07-07 15:30:10 +0200478 if (err)
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400479 goto err_deinit_pencrypt;
Steffen Klassert4c879172010-07-07 15:30:10 +0200480
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400481 padata_start(pencrypt.pinst);
482 padata_start(pdecrypt.pinst);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100483
484 return crypto_register_template(&pcrypt_tmpl);
485
Dan Kruchinine15bacb2010-07-14 14:31:57 +0400486err_deinit_pencrypt:
Steffen Klassertc57e8422010-07-27 07:16:33 +0200487 pcrypt_fini_padata(&pencrypt);
Dan Kruchinina3fb1e32010-07-14 14:34:15 +0400488err_unreg_kset:
489 kset_unregister(pcrypt_kset);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100490err:
Steffen Klassert4c879172010-07-07 15:30:10 +0200491 return err;
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100492}
493
494static void __exit pcrypt_exit(void)
495{
Steffen Klassertc57e8422010-07-27 07:16:33 +0200496 pcrypt_fini_padata(&pencrypt);
497 pcrypt_fini_padata(&pdecrypt);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100498
Dan Kruchinina3fb1e32010-07-14 14:34:15 +0400499 kset_unregister(pcrypt_kset);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100500 crypto_unregister_template(&pcrypt_tmpl);
501}
502
Eric Biggersc4741b22019-04-11 21:57:42 -0700503subsys_initcall(pcrypt_init);
Steffen Klassert5068c7a2010-01-07 15:57:19 +1100504module_exit(pcrypt_exit);
505
506MODULE_LICENSE("GPL");
507MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
508MODULE_DESCRIPTION("Parallel crypto wrapper");
Kees Cook4943ba12014-11-24 16:32:38 -0800509MODULE_ALIAS_CRYPTO("pcrypt");