hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/crypto/cryptd.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Software async crypto daemon.
34 *
....@@ -9,20 +10,13 @@
910 * Gabriele Paoloni <gabriele.paoloni@intel.com>
1011 * Aidan O'Mahony (aidan.o.mahony@intel.com)
1112 * Copyright (c) 2010, Intel Corporation.
12
- *
13
- * This program is free software; you can redistribute it and/or modify it
14
- * under the terms of the GNU General Public License as published by the Free
15
- * Software Foundation; either version 2 of the License, or (at your option)
16
- * any later version.
17
- *
1813 */
1914
2015 #include <crypto/internal/hash.h>
2116 #include <crypto/internal/aead.h>
2217 #include <crypto/internal/skcipher.h>
2318 #include <crypto/cryptd.h>
24
-#include <crypto/crypto_wq.h>
25
-#include <linux/atomic.h>
19
+#include <linux/refcount.h>
2620 #include <linux/err.h>
2721 #include <linux/init.h>
2822 #include <linux/kernel.h>
....@@ -31,18 +25,24 @@
3125 #include <linux/scatterlist.h>
3226 #include <linux/sched.h>
3327 #include <linux/slab.h>
28
+#include <linux/workqueue.h>
3429
3530 static unsigned int cryptd_max_cpu_qlen = 1000;
3631 module_param(cryptd_max_cpu_qlen, uint, 0);
3732 MODULE_PARM_DESC(cryptd_max_cpu_qlen, "Set cryptd Max queue depth");
3833
34
+static struct workqueue_struct *cryptd_wq;
35
+
3936 struct cryptd_cpu_queue {
4037 struct crypto_queue queue;
4138 struct work_struct work;
42
- spinlock_t qlock;
4339 };
4440
4541 struct cryptd_queue {
42
+ /*
43
+ * Protected by disabling BH to allow enqueueing from softinterrupt and
44
+ * dequeuing from kworker (cryptd_queue_worker()).
45
+ */
4646 struct cryptd_cpu_queue __percpu *cpu_queue;
4747 };
4848
....@@ -66,26 +66,18 @@
6666 struct cryptd_queue *queue;
6767 };
6868
69
-struct cryptd_blkcipher_ctx {
70
- atomic_t refcnt;
71
- struct crypto_blkcipher *child;
72
-};
73
-
74
-struct cryptd_blkcipher_request_ctx {
75
- crypto_completion_t complete;
76
-};
77
-
7869 struct cryptd_skcipher_ctx {
79
- atomic_t refcnt;
70
+ refcount_t refcnt;
8071 struct crypto_skcipher *child;
8172 };
8273
8374 struct cryptd_skcipher_request_ctx {
8475 crypto_completion_t complete;
76
+ struct skcipher_request req;
8577 };
8678
8779 struct cryptd_hash_ctx {
88
- atomic_t refcnt;
80
+ refcount_t refcnt;
8981 struct crypto_shash *child;
9082 };
9183
....@@ -95,7 +87,7 @@
9587 };
9688
9789 struct cryptd_aead_ctx {
98
- atomic_t refcnt;
90
+ refcount_t refcnt;
9991 struct crypto_aead *child;
10092 };
10193
....@@ -118,7 +110,6 @@
118110 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
119111 crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
120112 INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
121
- spin_lock_init(&cpu_queue->qlock);
122113 }
123114 pr_info("cryptd: max_cpu_qlen set to %d\n", max_cpu_qlen);
124115 return 0;
....@@ -139,30 +130,28 @@
139130 static int cryptd_enqueue_request(struct cryptd_queue *queue,
140131 struct crypto_async_request *request)
141132 {
142
- int cpu, err;
133
+ int err;
143134 struct cryptd_cpu_queue *cpu_queue;
144
- atomic_t *refcnt;
135
+ refcount_t *refcnt;
145136
146
- cpu_queue = raw_cpu_ptr(queue->cpu_queue);
147
- spin_lock_bh(&cpu_queue->qlock);
148
- cpu = smp_processor_id();
149
-
137
+ local_bh_disable();
138
+ cpu_queue = this_cpu_ptr(queue->cpu_queue);
150139 err = crypto_enqueue_request(&cpu_queue->queue, request);
151140
152141 refcnt = crypto_tfm_ctx(request->tfm);
153142
154143 if (err == -ENOSPC)
155
- goto out_put_cpu;
144
+ goto out;
156145
157
- queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
146
+ queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work);
158147
159
- if (!atomic_read(refcnt))
160
- goto out_put_cpu;
148
+ if (!refcount_read(refcnt))
149
+ goto out;
161150
162
- atomic_inc(refcnt);
151
+ refcount_inc(refcnt);
163152
164
-out_put_cpu:
165
- spin_unlock_bh(&cpu_queue->qlock);
153
+out:
154
+ local_bh_enable();
166155
167156 return err;
168157 }
....@@ -179,10 +168,10 @@
179168 /*
180169 * Only handle one request at a time to avoid hogging crypto workqueue.
181170 */
182
- spin_lock_bh(&cpu_queue->qlock);
171
+ local_bh_disable();
183172 backlog = crypto_get_backlog(&cpu_queue->queue);
184173 req = crypto_dequeue_request(&cpu_queue->queue);
185
- spin_unlock_bh(&cpu_queue->qlock);
174
+ local_bh_enable();
186175
187176 if (!req)
188177 return;
....@@ -192,7 +181,7 @@
192181 req->complete(req, 0);
193182
194183 if (cpu_queue->queue.qlen)
195
- queue_work(kcrypto_wq, &cpu_queue->work);
184
+ queue_work(cryptd_wq, &cpu_queue->work);
196185 }
197186
198187 static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
....@@ -202,140 +191,20 @@
202191 return ictx->queue;
203192 }
204193
205
-static inline void cryptd_check_internal(struct rtattr **tb, u32 *type,
206
- u32 *mask)
194
+static void cryptd_type_and_mask(struct crypto_attr_type *algt,
195
+ u32 *type, u32 *mask)
207196 {
208
- struct crypto_attr_type *algt;
197
+ /*
198
+ * cryptd is allowed to wrap internal algorithms, but in that case the
199
+ * resulting cryptd instance will be marked as internal as well.
200
+ */
201
+ *type = algt->type & CRYPTO_ALG_INTERNAL;
202
+ *mask = algt->mask & CRYPTO_ALG_INTERNAL;
209203
210
- algt = crypto_get_attr_type(tb);
211
- if (IS_ERR(algt))
212
- return;
204
+ /* No point in cryptd wrapping an algorithm that's already async. */
205
+ *mask |= CRYPTO_ALG_ASYNC;
213206
214
- *type |= algt->type & CRYPTO_ALG_INTERNAL;
215
- *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
216
-}
217
-
218
-static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
219
- const u8 *key, unsigned int keylen)
220
-{
221
- struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
222
- struct crypto_blkcipher *child = ctx->child;
223
- int err;
224
-
225
- crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
226
- crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
227
- CRYPTO_TFM_REQ_MASK);
228
- err = crypto_blkcipher_setkey(child, key, keylen);
229
- crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
230
- CRYPTO_TFM_RES_MASK);
231
- return err;
232
-}
233
-
234
-static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
235
- struct crypto_blkcipher *child,
236
- int err,
237
- int (*crypt)(struct blkcipher_desc *desc,
238
- struct scatterlist *dst,
239
- struct scatterlist *src,
240
- unsigned int len))
241
-{
242
- struct cryptd_blkcipher_request_ctx *rctx;
243
- struct cryptd_blkcipher_ctx *ctx;
244
- struct crypto_ablkcipher *tfm;
245
- struct blkcipher_desc desc;
246
- int refcnt;
247
-
248
- rctx = ablkcipher_request_ctx(req);
249
-
250
- if (unlikely(err == -EINPROGRESS))
251
- goto out;
252
-
253
- desc.tfm = child;
254
- desc.info = req->info;
255
- desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
256
-
257
- err = crypt(&desc, req->dst, req->src, req->nbytes);
258
-
259
- req->base.complete = rctx->complete;
260
-
261
-out:
262
- tfm = crypto_ablkcipher_reqtfm(req);
263
- ctx = crypto_ablkcipher_ctx(tfm);
264
- refcnt = atomic_read(&ctx->refcnt);
265
-
266
- local_bh_disable();
267
- rctx->complete(&req->base, err);
268
- local_bh_enable();
269
-
270
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
271
- crypto_free_ablkcipher(tfm);
272
-}
273
-
274
-static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
275
-{
276
- struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
277
- struct crypto_blkcipher *child = ctx->child;
278
-
279
- cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
280
- crypto_blkcipher_crt(child)->encrypt);
281
-}
282
-
283
-static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
284
-{
285
- struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
286
- struct crypto_blkcipher *child = ctx->child;
287
-
288
- cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
289
- crypto_blkcipher_crt(child)->decrypt);
290
-}
291
-
292
-static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
293
- crypto_completion_t compl)
294
-{
295
- struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
296
- struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
297
- struct cryptd_queue *queue;
298
-
299
- queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
300
- rctx->complete = req->base.complete;
301
- req->base.complete = compl;
302
-
303
- return cryptd_enqueue_request(queue, &req->base);
304
-}
305
-
306
-static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
307
-{
308
- return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
309
-}
310
-
311
-static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
312
-{
313
- return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
314
-}
315
-
316
-static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
317
-{
318
- struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
319
- struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
320
- struct crypto_spawn *spawn = &ictx->spawn;
321
- struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
322
- struct crypto_blkcipher *cipher;
323
-
324
- cipher = crypto_spawn_blkcipher(spawn);
325
- if (IS_ERR(cipher))
326
- return PTR_ERR(cipher);
327
-
328
- ctx->child = cipher;
329
- tfm->crt_ablkcipher.reqsize =
330
- sizeof(struct cryptd_blkcipher_request_ctx);
331
- return 0;
332
-}
333
-
334
-static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
335
-{
336
- struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
337
-
338
- crypto_free_blkcipher(ctx->child);
207
+ *mask |= crypto_algt_inherited_mask(algt);
339208 }
340209
341210 static int cryptd_init_instance(struct crypto_instance *inst,
....@@ -355,109 +224,17 @@
355224 return 0;
356225 }
357226
358
-static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
359
- unsigned int tail)
360
-{
361
- char *p;
362
- struct crypto_instance *inst;
363
- int err;
364
-
365
- p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
366
- if (!p)
367
- return ERR_PTR(-ENOMEM);
368
-
369
- inst = (void *)(p + head);
370
-
371
- err = cryptd_init_instance(inst, alg);
372
- if (err)
373
- goto out_free_inst;
374
-
375
-out:
376
- return p;
377
-
378
-out_free_inst:
379
- kfree(p);
380
- p = ERR_PTR(err);
381
- goto out;
382
-}
383
-
384
-static int cryptd_create_blkcipher(struct crypto_template *tmpl,
385
- struct rtattr **tb,
386
- struct cryptd_queue *queue)
387
-{
388
- struct cryptd_instance_ctx *ctx;
389
- struct crypto_instance *inst;
390
- struct crypto_alg *alg;
391
- u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
392
- u32 mask = CRYPTO_ALG_TYPE_MASK;
393
- int err;
394
-
395
- cryptd_check_internal(tb, &type, &mask);
396
-
397
- alg = crypto_get_attr_alg(tb, type, mask);
398
- if (IS_ERR(alg))
399
- return PTR_ERR(alg);
400
-
401
- inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx));
402
- err = PTR_ERR(inst);
403
- if (IS_ERR(inst))
404
- goto out_put_alg;
405
-
406
- ctx = crypto_instance_ctx(inst);
407
- ctx->queue = queue;
408
-
409
- err = crypto_init_spawn(&ctx->spawn, alg, inst,
410
- CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
411
- if (err)
412
- goto out_free_inst;
413
-
414
- type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
415
- if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
416
- type |= CRYPTO_ALG_INTERNAL;
417
- inst->alg.cra_flags = type;
418
- inst->alg.cra_type = &crypto_ablkcipher_type;
419
-
420
- inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
421
- inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
422
- inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
423
-
424
- inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
425
-
426
- inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
427
-
428
- inst->alg.cra_init = cryptd_blkcipher_init_tfm;
429
- inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
430
-
431
- inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
432
- inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
433
- inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
434
-
435
- err = crypto_register_instance(tmpl, inst);
436
- if (err) {
437
- crypto_drop_spawn(&ctx->spawn);
438
-out_free_inst:
439
- kfree(inst);
440
- }
441
-
442
-out_put_alg:
443
- crypto_mod_put(alg);
444
- return err;
445
-}
446
-
447227 static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
448228 const u8 *key, unsigned int keylen)
449229 {
450230 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
451231 struct crypto_skcipher *child = ctx->child;
452
- int err;
453232
454233 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
455
- crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
456
- CRYPTO_TFM_REQ_MASK);
457
- err = crypto_skcipher_setkey(child, key, keylen);
458
- crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
459
- CRYPTO_TFM_RES_MASK);
460
- return err;
234
+ crypto_skcipher_set_flags(child,
235
+ crypto_skcipher_get_flags(parent) &
236
+ CRYPTO_TFM_REQ_MASK);
237
+ return crypto_skcipher_setkey(child, key, keylen);
461238 }
462239
463240 static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
....@@ -465,13 +242,13 @@
465242 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
466243 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
467244 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
468
- int refcnt = atomic_read(&ctx->refcnt);
245
+ int refcnt = refcount_read(&ctx->refcnt);
469246
470247 local_bh_disable();
471248 rctx->complete(&req->base, err);
472249 local_bh_enable();
473250
474
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
251
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
475252 crypto_free_skcipher(tfm);
476253 }
477254
....@@ -482,8 +259,8 @@
482259 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
483260 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
484261 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
262
+ struct skcipher_request *subreq = &rctx->req;
485263 struct crypto_skcipher *child = ctx->child;
486
- SKCIPHER_REQUEST_ON_STACK(subreq, child);
487264
488265 if (unlikely(err == -EINPROGRESS))
489266 goto out;
....@@ -510,8 +287,8 @@
510287 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
511288 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
512289 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
290
+ struct skcipher_request *subreq = &rctx->req;
513291 struct crypto_skcipher *child = ctx->child;
514
- SKCIPHER_REQUEST_ON_STACK(subreq, child);
515292
516293 if (unlikely(err == -EINPROGRESS))
517294 goto out;
....@@ -569,7 +346,8 @@
569346
570347 ctx->child = cipher;
571348 crypto_skcipher_set_reqsize(
572
- tfm, sizeof(struct cryptd_skcipher_request_ctx));
349
+ tfm, sizeof(struct cryptd_skcipher_request_ctx) +
350
+ crypto_skcipher_reqsize(cipher));
573351 return 0;
574352 }
575353
....@@ -590,24 +368,17 @@
590368
591369 static int cryptd_create_skcipher(struct crypto_template *tmpl,
592370 struct rtattr **tb,
371
+ struct crypto_attr_type *algt,
593372 struct cryptd_queue *queue)
594373 {
595374 struct skcipherd_instance_ctx *ctx;
596375 struct skcipher_instance *inst;
597376 struct skcipher_alg *alg;
598
- const char *name;
599377 u32 type;
600378 u32 mask;
601379 int err;
602380
603
- type = 0;
604
- mask = CRYPTO_ALG_ASYNC;
605
-
606
- cryptd_check_internal(tb, &type, &mask);
607
-
608
- name = crypto_attr_alg_name(tb[1]);
609
- if (IS_ERR(name))
610
- return PTR_ERR(name);
381
+ cryptd_type_and_mask(algt, &type, &mask);
611382
612383 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
613384 if (!inst)
....@@ -616,19 +387,18 @@
616387 ctx = skcipher_instance_ctx(inst);
617388 ctx->queue = queue;
618389
619
- crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
620
- err = crypto_grab_skcipher(&ctx->spawn, name, type, mask);
390
+ err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
391
+ crypto_attr_alg_name(tb[1]), type, mask);
621392 if (err)
622
- goto out_free_inst;
393
+ goto err_free_inst;
623394
624395 alg = crypto_spawn_skcipher_alg(&ctx->spawn);
625396 err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
626397 if (err)
627
- goto out_drop_skcipher;
398
+ goto err_free_inst;
628399
629
- inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
630
- (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
631
-
400
+ inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
401
+ (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
632402 inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
633403 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
634404 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
....@@ -647,10 +417,8 @@
647417
648418 err = skcipher_register_instance(tmpl, inst);
649419 if (err) {
650
-out_drop_skcipher:
651
- crypto_drop_skcipher(&ctx->spawn);
652
-out_free_inst:
653
- kfree(inst);
420
+err_free_inst:
421
+ cryptd_skcipher_free(inst);
654422 }
655423 return err;
656424 }
....@@ -686,15 +454,11 @@
686454 {
687455 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
688456 struct crypto_shash *child = ctx->child;
689
- int err;
690457
691458 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
692459 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
693460 CRYPTO_TFM_REQ_MASK);
694
- err = crypto_shash_setkey(child, key, keylen);
695
- crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
696
- CRYPTO_TFM_RES_MASK);
697
- return err;
461
+ return crypto_shash_setkey(child, key, keylen);
698462 }
699463
700464 static int cryptd_hash_enqueue(struct ahash_request *req,
....@@ -716,13 +480,13 @@
716480 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
717481 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
718482 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
719
- int refcnt = atomic_read(&ctx->refcnt);
483
+ int refcnt = refcount_read(&ctx->refcnt);
720484
721485 local_bh_disable();
722486 rctx->complete(&req->base, err);
723487 local_bh_enable();
724488
725
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
489
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
726490 crypto_free_ahash(tfm);
727491 }
728492
....@@ -738,7 +502,6 @@
738502 goto out;
739503
740504 desc->tfm = child;
741
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
742505
743506 err = crypto_shash_init(desc);
744507
....@@ -830,7 +593,6 @@
830593 goto out;
831594
832595 desc->tfm = child;
833
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
834596
835597 err = shash_ahash_digest(req, desc);
836598
....@@ -859,49 +621,53 @@
859621 struct shash_desc *desc = cryptd_shash_desc(req);
860622
861623 desc->tfm = ctx->child;
862
- desc->flags = req->base.flags;
863624
864625 return crypto_shash_import(desc, in);
865626 }
866627
628
+static void cryptd_hash_free(struct ahash_instance *inst)
629
+{
630
+ struct hashd_instance_ctx *ctx = ahash_instance_ctx(inst);
631
+
632
+ crypto_drop_shash(&ctx->spawn);
633
+ kfree(inst);
634
+}
635
+
867636 static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
637
+ struct crypto_attr_type *algt,
868638 struct cryptd_queue *queue)
869639 {
870640 struct hashd_instance_ctx *ctx;
871641 struct ahash_instance *inst;
872
- struct shash_alg *salg;
873
- struct crypto_alg *alg;
874
- u32 type = 0;
875
- u32 mask = 0;
642
+ struct shash_alg *alg;
643
+ u32 type;
644
+ u32 mask;
876645 int err;
877646
878
- cryptd_check_internal(tb, &type, &mask);
647
+ cryptd_type_and_mask(algt, &type, &mask);
879648
880
- salg = shash_attr_alg(tb[1], type, mask);
881
- if (IS_ERR(salg))
882
- return PTR_ERR(salg);
883
-
884
- alg = &salg->base;
885
- inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
886
- sizeof(*ctx));
887
- err = PTR_ERR(inst);
888
- if (IS_ERR(inst))
889
- goto out_put_alg;
649
+ inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
650
+ if (!inst)
651
+ return -ENOMEM;
890652
891653 ctx = ahash_instance_ctx(inst);
892654 ctx->queue = queue;
893655
894
- err = crypto_init_shash_spawn(&ctx->spawn, salg,
895
- ahash_crypto_instance(inst));
656
+ err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst),
657
+ crypto_attr_alg_name(tb[1]), type, mask);
896658 if (err)
897
- goto out_free_inst;
659
+ goto err_free_inst;
660
+ alg = crypto_spawn_shash_alg(&ctx->spawn);
898661
899
- inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
900
- (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
901
- CRYPTO_ALG_OPTIONAL_KEY));
662
+ err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base);
663
+ if (err)
664
+ goto err_free_inst;
902665
903
- inst->alg.halg.digestsize = salg->digestsize;
904
- inst->alg.halg.statesize = salg->statesize;
666
+ inst->alg.halg.base.cra_flags |= CRYPTO_ALG_ASYNC |
667
+ (alg->base.cra_flags & (CRYPTO_ALG_INTERNAL|
668
+ CRYPTO_ALG_OPTIONAL_KEY));
669
+ inst->alg.halg.digestsize = alg->digestsize;
670
+ inst->alg.halg.statesize = alg->statesize;
905671 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
906672
907673 inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
....@@ -913,19 +679,17 @@
913679 inst->alg.finup = cryptd_hash_finup_enqueue;
914680 inst->alg.export = cryptd_hash_export;
915681 inst->alg.import = cryptd_hash_import;
916
- if (crypto_shash_alg_has_setkey(salg))
682
+ if (crypto_shash_alg_has_setkey(alg))
917683 inst->alg.setkey = cryptd_hash_setkey;
918684 inst->alg.digest = cryptd_hash_digest_enqueue;
919685
686
+ inst->free = cryptd_hash_free;
687
+
920688 err = ahash_register_instance(tmpl, inst);
921689 if (err) {
922
- crypto_drop_shash(&ctx->spawn);
923
-out_free_inst:
924
- kfree(inst);
690
+err_free_inst:
691
+ cryptd_hash_free(inst);
925692 }
926
-
927
-out_put_alg:
928
- crypto_mod_put(alg);
929693 return err;
930694 }
931695
....@@ -970,13 +734,13 @@
970734
971735 out:
972736 ctx = crypto_aead_ctx(tfm);
973
- refcnt = atomic_read(&ctx->refcnt);
737
+ refcnt = refcount_read(&ctx->refcnt);
974738
975739 local_bh_disable();
976740 compl(&req->base, err);
977741 local_bh_enable();
978742
979
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
743
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
980744 crypto_free_aead(tfm);
981745 }
982746
....@@ -1047,23 +811,27 @@
1047811 crypto_free_aead(ctx->child);
1048812 }
1049813
814
+static void cryptd_aead_free(struct aead_instance *inst)
815
+{
816
+ struct aead_instance_ctx *ctx = aead_instance_ctx(inst);
817
+
818
+ crypto_drop_aead(&ctx->aead_spawn);
819
+ kfree(inst);
820
+}
821
+
1050822 static int cryptd_create_aead(struct crypto_template *tmpl,
1051823 struct rtattr **tb,
824
+ struct crypto_attr_type *algt,
1052825 struct cryptd_queue *queue)
1053826 {
1054827 struct aead_instance_ctx *ctx;
1055828 struct aead_instance *inst;
1056829 struct aead_alg *alg;
1057
- const char *name;
1058
- u32 type = 0;
1059
- u32 mask = CRYPTO_ALG_ASYNC;
830
+ u32 type;
831
+ u32 mask;
1060832 int err;
1061833
1062
- cryptd_check_internal(tb, &type, &mask);
1063
-
1064
- name = crypto_attr_alg_name(tb[1]);
1065
- if (IS_ERR(name))
1066
- return PTR_ERR(name);
834
+ cryptd_type_and_mask(algt, &type, &mask);
1067835
1068836 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
1069837 if (!inst)
....@@ -1072,18 +840,18 @@
1072840 ctx = aead_instance_ctx(inst);
1073841 ctx->queue = queue;
1074842
1075
- crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
1076
- err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask);
843
+ err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst),
844
+ crypto_attr_alg_name(tb[1]), type, mask);
1077845 if (err)
1078
- goto out_free_inst;
846
+ goto err_free_inst;
1079847
1080848 alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
1081849 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
1082850 if (err)
1083
- goto out_drop_aead;
851
+ goto err_free_inst;
1084852
1085
- inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
1086
- (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
853
+ inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
854
+ (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
1087855 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
1088856
1089857 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
....@@ -1096,12 +864,12 @@
1096864 inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
1097865 inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
1098866
867
+ inst->free = cryptd_aead_free;
868
+
1099869 err = aead_register_instance(tmpl, inst);
1100870 if (err) {
1101
-out_drop_aead:
1102
- crypto_drop_aead(&ctx->aead_spawn);
1103
-out_free_inst:
1104
- kfree(inst);
871
+err_free_inst:
872
+ cryptd_aead_free(inst);
1105873 }
1106874 return err;
1107875 }
....@@ -1117,100 +885,22 @@
1117885 return PTR_ERR(algt);
1118886
1119887 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
1120
- case CRYPTO_ALG_TYPE_BLKCIPHER:
1121
- if ((algt->type & CRYPTO_ALG_TYPE_MASK) ==
1122
- CRYPTO_ALG_TYPE_BLKCIPHER)
1123
- return cryptd_create_blkcipher(tmpl, tb, &queue);
1124
-
1125
- return cryptd_create_skcipher(tmpl, tb, &queue);
1126
- case CRYPTO_ALG_TYPE_DIGEST:
1127
- return cryptd_create_hash(tmpl, tb, &queue);
888
+ case CRYPTO_ALG_TYPE_SKCIPHER:
889
+ return cryptd_create_skcipher(tmpl, tb, algt, &queue);
890
+ case CRYPTO_ALG_TYPE_HASH:
891
+ return cryptd_create_hash(tmpl, tb, algt, &queue);
1128892 case CRYPTO_ALG_TYPE_AEAD:
1129
- return cryptd_create_aead(tmpl, tb, &queue);
893
+ return cryptd_create_aead(tmpl, tb, algt, &queue);
1130894 }
1131895
1132896 return -EINVAL;
1133897 }
1134898
1135
-static void cryptd_free(struct crypto_instance *inst)
1136
-{
1137
- struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
1138
- struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
1139
- struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
1140
-
1141
- switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
1142
- case CRYPTO_ALG_TYPE_AHASH:
1143
- crypto_drop_shash(&hctx->spawn);
1144
- kfree(ahash_instance(inst));
1145
- return;
1146
- case CRYPTO_ALG_TYPE_AEAD:
1147
- crypto_drop_aead(&aead_ctx->aead_spawn);
1148
- kfree(aead_instance(inst));
1149
- return;
1150
- default:
1151
- crypto_drop_spawn(&ctx->spawn);
1152
- kfree(inst);
1153
- }
1154
-}
1155
-
1156899 static struct crypto_template cryptd_tmpl = {
1157900 .name = "cryptd",
1158901 .create = cryptd_create,
1159
- .free = cryptd_free,
1160902 .module = THIS_MODULE,
1161903 };
1162
-
1163
-struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
1164
- u32 type, u32 mask)
1165
-{
1166
- char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
1167
- struct cryptd_blkcipher_ctx *ctx;
1168
- struct crypto_tfm *tfm;
1169
-
1170
- if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1171
- "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1172
- return ERR_PTR(-EINVAL);
1173
- type = crypto_skcipher_type(type);
1174
- mask &= ~CRYPTO_ALG_TYPE_MASK;
1175
- mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
1176
- tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
1177
- if (IS_ERR(tfm))
1178
- return ERR_CAST(tfm);
1179
- if (tfm->__crt_alg->cra_module != THIS_MODULE) {
1180
- crypto_free_tfm(tfm);
1181
- return ERR_PTR(-EINVAL);
1182
- }
1183
-
1184
- ctx = crypto_tfm_ctx(tfm);
1185
- atomic_set(&ctx->refcnt, 1);
1186
-
1187
- return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
1188
-}
1189
-EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
1190
-
1191
-struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
1192
-{
1193
- struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
1194
- return ctx->child;
1195
-}
1196
-EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
1197
-
1198
-bool cryptd_ablkcipher_queued(struct cryptd_ablkcipher *tfm)
1199
-{
1200
- struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
1201
-
1202
- return atomic_read(&ctx->refcnt) - 1;
1203
-}
1204
-EXPORT_SYMBOL_GPL(cryptd_ablkcipher_queued);
1205
-
1206
-void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
1207
-{
1208
- struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
1209
-
1210
- if (atomic_dec_and_test(&ctx->refcnt))
1211
- crypto_free_ablkcipher(&tfm->base);
1212
-}
1213
-EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
1214904
1215905 struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
1216906 u32 type, u32 mask)
....@@ -1233,7 +923,7 @@
1233923 }
1234924
1235925 ctx = crypto_skcipher_ctx(tfm);
1236
- atomic_set(&ctx->refcnt, 1);
926
+ refcount_set(&ctx->refcnt, 1);
1237927
1238928 return container_of(tfm, struct cryptd_skcipher, base);
1239929 }
....@@ -1251,7 +941,7 @@
1251941 {
1252942 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1253943
1254
- return atomic_read(&ctx->refcnt) - 1;
944
+ return refcount_read(&ctx->refcnt) - 1;
1255945 }
1256946 EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
1257947
....@@ -1259,7 +949,7 @@
1259949 {
1260950 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1261951
1262
- if (atomic_dec_and_test(&ctx->refcnt))
952
+ if (refcount_dec_and_test(&ctx->refcnt))
1263953 crypto_free_skcipher(&tfm->base);
1264954 }
1265955 EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
....@@ -1283,7 +973,7 @@
1283973 }
1284974
1285975 ctx = crypto_ahash_ctx(tfm);
1286
- atomic_set(&ctx->refcnt, 1);
976
+ refcount_set(&ctx->refcnt, 1);
1287977
1288978 return __cryptd_ahash_cast(tfm);
1289979 }
....@@ -1308,7 +998,7 @@
1308998 {
1309999 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
13101000
1311
- return atomic_read(&ctx->refcnt) - 1;
1001
+ return refcount_read(&ctx->refcnt) - 1;
13121002 }
13131003 EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
13141004
....@@ -1316,7 +1006,7 @@
13161006 {
13171007 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
13181008
1319
- if (atomic_dec_and_test(&ctx->refcnt))
1009
+ if (refcount_dec_and_test(&ctx->refcnt))
13201010 crypto_free_ahash(&tfm->base);
13211011 }
13221012 EXPORT_SYMBOL_GPL(cryptd_free_ahash);
....@@ -1340,7 +1030,7 @@
13401030 }
13411031
13421032 ctx = crypto_aead_ctx(tfm);
1343
- atomic_set(&ctx->refcnt, 1);
1033
+ refcount_set(&ctx->refcnt, 1);
13441034
13451035 return __cryptd_aead_cast(tfm);
13461036 }
....@@ -1358,7 +1048,7 @@
13581048 {
13591049 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
13601050
1361
- return atomic_read(&ctx->refcnt) - 1;
1051
+ return refcount_read(&ctx->refcnt) - 1;
13621052 }
13631053 EXPORT_SYMBOL_GPL(cryptd_aead_queued);
13641054
....@@ -1366,7 +1056,7 @@
13661056 {
13671057 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
13681058
1369
- if (atomic_dec_and_test(&ctx->refcnt))
1059
+ if (refcount_dec_and_test(&ctx->refcnt))
13701060 crypto_free_aead(&tfm->base);
13711061 }
13721062 EXPORT_SYMBOL_GPL(cryptd_free_aead);
....@@ -1375,19 +1065,31 @@
13751065 {
13761066 int err;
13771067
1068
+ cryptd_wq = alloc_workqueue("cryptd", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
1069
+ 1);
1070
+ if (!cryptd_wq)
1071
+ return -ENOMEM;
1072
+
13781073 err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
13791074 if (err)
1380
- return err;
1075
+ goto err_destroy_wq;
13811076
13821077 err = crypto_register_template(&cryptd_tmpl);
13831078 if (err)
1384
- cryptd_fini_queue(&queue);
1079
+ goto err_fini_queue;
13851080
1081
+ return 0;
1082
+
1083
+err_fini_queue:
1084
+ cryptd_fini_queue(&queue);
1085
+err_destroy_wq:
1086
+ destroy_workqueue(cryptd_wq);
13861087 return err;
13871088 }
13881089
13891090 static void __exit cryptd_exit(void)
13901091 {
1092
+ destroy_workqueue(cryptd_wq);
13911093 cryptd_fini_queue(&queue);
13921094 crypto_unregister_template(&cryptd_tmpl);
13931095 }