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,10 +25,13 @@
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");
33
+
34
+static struct workqueue_struct *cryptd_wq;
3835
3936 struct cryptd_cpu_queue {
4037 struct crypto_queue queue;
....@@ -42,6 +39,10 @@
4239 };
4340
4441 struct cryptd_queue {
42
+ /*
43
+ * Protected by disabling BH to allow enqueueing from softinterrupt and
44
+ * dequeuing from kworker (cryptd_queue_worker()).
45
+ */
4546 struct cryptd_cpu_queue __percpu *cpu_queue;
4647 };
4748
....@@ -65,26 +66,18 @@
6566 struct cryptd_queue *queue;
6667 };
6768
68
-struct cryptd_blkcipher_ctx {
69
- atomic_t refcnt;
70
- struct crypto_blkcipher *child;
71
-};
72
-
73
-struct cryptd_blkcipher_request_ctx {
74
- crypto_completion_t complete;
75
-};
76
-
7769 struct cryptd_skcipher_ctx {
78
- atomic_t refcnt;
70
+ refcount_t refcnt;
7971 struct crypto_skcipher *child;
8072 };
8173
8274 struct cryptd_skcipher_request_ctx {
8375 crypto_completion_t complete;
76
+ struct skcipher_request req;
8477 };
8578
8679 struct cryptd_hash_ctx {
87
- atomic_t refcnt;
80
+ refcount_t refcnt;
8881 struct crypto_shash *child;
8982 };
9083
....@@ -94,7 +87,7 @@
9487 };
9588
9689 struct cryptd_aead_ctx {
97
- atomic_t refcnt;
90
+ refcount_t refcnt;
9891 struct crypto_aead *child;
9992 };
10093
....@@ -137,28 +130,28 @@
137130 static int cryptd_enqueue_request(struct cryptd_queue *queue,
138131 struct crypto_async_request *request)
139132 {
140
- int cpu, err;
133
+ int err;
141134 struct cryptd_cpu_queue *cpu_queue;
142
- atomic_t *refcnt;
135
+ refcount_t *refcnt;
143136
144
- cpu = get_cpu();
137
+ local_bh_disable();
145138 cpu_queue = this_cpu_ptr(queue->cpu_queue);
146139 err = crypto_enqueue_request(&cpu_queue->queue, request);
147140
148141 refcnt = crypto_tfm_ctx(request->tfm);
149142
150143 if (err == -ENOSPC)
151
- goto out_put_cpu;
144
+ goto out;
152145
153
- queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
146
+ queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work);
154147
155
- if (!atomic_read(refcnt))
156
- goto out_put_cpu;
148
+ if (!refcount_read(refcnt))
149
+ goto out;
157150
158
- atomic_inc(refcnt);
151
+ refcount_inc(refcnt);
159152
160
-out_put_cpu:
161
- put_cpu();
153
+out:
154
+ local_bh_enable();
162155
163156 return err;
164157 }
....@@ -174,15 +167,10 @@
174167 cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
175168 /*
176169 * Only handle one request at a time to avoid hogging crypto workqueue.
177
- * preempt_disable/enable is used to prevent being preempted by
178
- * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent
179
- * cryptd_enqueue_request() being accessed from software interrupts.
180170 */
181171 local_bh_disable();
182
- preempt_disable();
183172 backlog = crypto_get_backlog(&cpu_queue->queue);
184173 req = crypto_dequeue_request(&cpu_queue->queue);
185
- preempt_enable();
186174 local_bh_enable();
187175
188176 if (!req)
....@@ -193,7 +181,7 @@
193181 req->complete(req, 0);
194182
195183 if (cpu_queue->queue.qlen)
196
- queue_work(kcrypto_wq, &cpu_queue->work);
184
+ queue_work(cryptd_wq, &cpu_queue->work);
197185 }
198186
199187 static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
....@@ -203,140 +191,20 @@
203191 return ictx->queue;
204192 }
205193
206
-static inline void cryptd_check_internal(struct rtattr **tb, u32 *type,
207
- u32 *mask)
194
+static void cryptd_type_and_mask(struct crypto_attr_type *algt,
195
+ u32 *type, u32 *mask)
208196 {
209
- 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;
210203
211
- algt = crypto_get_attr_type(tb);
212
- if (IS_ERR(algt))
213
- return;
204
+ /* No point in cryptd wrapping an algorithm that's already async. */
205
+ *mask |= CRYPTO_ALG_ASYNC;
214206
215
- *type |= algt->type & CRYPTO_ALG_INTERNAL;
216
- *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
217
-}
218
-
219
-static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
220
- const u8 *key, unsigned int keylen)
221
-{
222
- struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
223
- struct crypto_blkcipher *child = ctx->child;
224
- int err;
225
-
226
- crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
227
- crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
228
- CRYPTO_TFM_REQ_MASK);
229
- err = crypto_blkcipher_setkey(child, key, keylen);
230
- crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
231
- CRYPTO_TFM_RES_MASK);
232
- return err;
233
-}
234
-
235
-static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
236
- struct crypto_blkcipher *child,
237
- int err,
238
- int (*crypt)(struct blkcipher_desc *desc,
239
- struct scatterlist *dst,
240
- struct scatterlist *src,
241
- unsigned int len))
242
-{
243
- struct cryptd_blkcipher_request_ctx *rctx;
244
- struct cryptd_blkcipher_ctx *ctx;
245
- struct crypto_ablkcipher *tfm;
246
- struct blkcipher_desc desc;
247
- int refcnt;
248
-
249
- rctx = ablkcipher_request_ctx(req);
250
-
251
- if (unlikely(err == -EINPROGRESS))
252
- goto out;
253
-
254
- desc.tfm = child;
255
- desc.info = req->info;
256
- desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
257
-
258
- err = crypt(&desc, req->dst, req->src, req->nbytes);
259
-
260
- req->base.complete = rctx->complete;
261
-
262
-out:
263
- tfm = crypto_ablkcipher_reqtfm(req);
264
- ctx = crypto_ablkcipher_ctx(tfm);
265
- refcnt = atomic_read(&ctx->refcnt);
266
-
267
- local_bh_disable();
268
- rctx->complete(&req->base, err);
269
- local_bh_enable();
270
-
271
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
272
- crypto_free_ablkcipher(tfm);
273
-}
274
-
275
-static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
276
-{
277
- struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
278
- struct crypto_blkcipher *child = ctx->child;
279
-
280
- cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
281
- crypto_blkcipher_crt(child)->encrypt);
282
-}
283
-
284
-static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
285
-{
286
- struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
287
- struct crypto_blkcipher *child = ctx->child;
288
-
289
- cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
290
- crypto_blkcipher_crt(child)->decrypt);
291
-}
292
-
293
-static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
294
- crypto_completion_t compl)
295
-{
296
- struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
297
- struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
298
- struct cryptd_queue *queue;
299
-
300
- queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
301
- rctx->complete = req->base.complete;
302
- req->base.complete = compl;
303
-
304
- return cryptd_enqueue_request(queue, &req->base);
305
-}
306
-
307
-static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
308
-{
309
- return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
310
-}
311
-
312
-static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
313
-{
314
- return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
315
-}
316
-
317
-static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
318
-{
319
- struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
320
- struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
321
- struct crypto_spawn *spawn = &ictx->spawn;
322
- struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
323
- struct crypto_blkcipher *cipher;
324
-
325
- cipher = crypto_spawn_blkcipher(spawn);
326
- if (IS_ERR(cipher))
327
- return PTR_ERR(cipher);
328
-
329
- ctx->child = cipher;
330
- tfm->crt_ablkcipher.reqsize =
331
- sizeof(struct cryptd_blkcipher_request_ctx);
332
- return 0;
333
-}
334
-
335
-static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
336
-{
337
- struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
338
-
339
- crypto_free_blkcipher(ctx->child);
207
+ *mask |= crypto_algt_inherited_mask(algt);
340208 }
341209
342210 static int cryptd_init_instance(struct crypto_instance *inst,
....@@ -356,109 +224,17 @@
356224 return 0;
357225 }
358226
359
-static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
360
- unsigned int tail)
361
-{
362
- char *p;
363
- struct crypto_instance *inst;
364
- int err;
365
-
366
- p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
367
- if (!p)
368
- return ERR_PTR(-ENOMEM);
369
-
370
- inst = (void *)(p + head);
371
-
372
- err = cryptd_init_instance(inst, alg);
373
- if (err)
374
- goto out_free_inst;
375
-
376
-out:
377
- return p;
378
-
379
-out_free_inst:
380
- kfree(p);
381
- p = ERR_PTR(err);
382
- goto out;
383
-}
384
-
385
-static int cryptd_create_blkcipher(struct crypto_template *tmpl,
386
- struct rtattr **tb,
387
- struct cryptd_queue *queue)
388
-{
389
- struct cryptd_instance_ctx *ctx;
390
- struct crypto_instance *inst;
391
- struct crypto_alg *alg;
392
- u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
393
- u32 mask = CRYPTO_ALG_TYPE_MASK;
394
- int err;
395
-
396
- cryptd_check_internal(tb, &type, &mask);
397
-
398
- alg = crypto_get_attr_alg(tb, type, mask);
399
- if (IS_ERR(alg))
400
- return PTR_ERR(alg);
401
-
402
- inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx));
403
- err = PTR_ERR(inst);
404
- if (IS_ERR(inst))
405
- goto out_put_alg;
406
-
407
- ctx = crypto_instance_ctx(inst);
408
- ctx->queue = queue;
409
-
410
- err = crypto_init_spawn(&ctx->spawn, alg, inst,
411
- CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
412
- if (err)
413
- goto out_free_inst;
414
-
415
- type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
416
- if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
417
- type |= CRYPTO_ALG_INTERNAL;
418
- inst->alg.cra_flags = type;
419
- inst->alg.cra_type = &crypto_ablkcipher_type;
420
-
421
- inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
422
- inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
423
- inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
424
-
425
- inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
426
-
427
- inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
428
-
429
- inst->alg.cra_init = cryptd_blkcipher_init_tfm;
430
- inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
431
-
432
- inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
433
- inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
434
- inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
435
-
436
- err = crypto_register_instance(tmpl, inst);
437
- if (err) {
438
- crypto_drop_spawn(&ctx->spawn);
439
-out_free_inst:
440
- kfree(inst);
441
- }
442
-
443
-out_put_alg:
444
- crypto_mod_put(alg);
445
- return err;
446
-}
447
-
448227 static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
449228 const u8 *key, unsigned int keylen)
450229 {
451230 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
452231 struct crypto_skcipher *child = ctx->child;
453
- int err;
454232
455233 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
456
- crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
457
- CRYPTO_TFM_REQ_MASK);
458
- err = crypto_skcipher_setkey(child, key, keylen);
459
- crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
460
- CRYPTO_TFM_RES_MASK);
461
- 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);
462238 }
463239
464240 static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
....@@ -466,13 +242,13 @@
466242 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
467243 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
468244 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
469
- int refcnt = atomic_read(&ctx->refcnt);
245
+ int refcnt = refcount_read(&ctx->refcnt);
470246
471247 local_bh_disable();
472248 rctx->complete(&req->base, err);
473249 local_bh_enable();
474250
475
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
251
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
476252 crypto_free_skcipher(tfm);
477253 }
478254
....@@ -483,8 +259,8 @@
483259 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
484260 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
485261 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
262
+ struct skcipher_request *subreq = &rctx->req;
486263 struct crypto_skcipher *child = ctx->child;
487
- SKCIPHER_REQUEST_ON_STACK(subreq, child);
488264
489265 if (unlikely(err == -EINPROGRESS))
490266 goto out;
....@@ -511,8 +287,8 @@
511287 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
512288 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
513289 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
290
+ struct skcipher_request *subreq = &rctx->req;
514291 struct crypto_skcipher *child = ctx->child;
515
- SKCIPHER_REQUEST_ON_STACK(subreq, child);
516292
517293 if (unlikely(err == -EINPROGRESS))
518294 goto out;
....@@ -570,7 +346,8 @@
570346
571347 ctx->child = cipher;
572348 crypto_skcipher_set_reqsize(
573
- tfm, sizeof(struct cryptd_skcipher_request_ctx));
349
+ tfm, sizeof(struct cryptd_skcipher_request_ctx) +
350
+ crypto_skcipher_reqsize(cipher));
574351 return 0;
575352 }
576353
....@@ -591,24 +368,17 @@
591368
592369 static int cryptd_create_skcipher(struct crypto_template *tmpl,
593370 struct rtattr **tb,
371
+ struct crypto_attr_type *algt,
594372 struct cryptd_queue *queue)
595373 {
596374 struct skcipherd_instance_ctx *ctx;
597375 struct skcipher_instance *inst;
598376 struct skcipher_alg *alg;
599
- const char *name;
600377 u32 type;
601378 u32 mask;
602379 int err;
603380
604
- type = 0;
605
- mask = CRYPTO_ALG_ASYNC;
606
-
607
- cryptd_check_internal(tb, &type, &mask);
608
-
609
- name = crypto_attr_alg_name(tb[1]);
610
- if (IS_ERR(name))
611
- return PTR_ERR(name);
381
+ cryptd_type_and_mask(algt, &type, &mask);
612382
613383 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
614384 if (!inst)
....@@ -617,19 +387,18 @@
617387 ctx = skcipher_instance_ctx(inst);
618388 ctx->queue = queue;
619389
620
- crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
621
- 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);
622392 if (err)
623
- goto out_free_inst;
393
+ goto err_free_inst;
624394
625395 alg = crypto_spawn_skcipher_alg(&ctx->spawn);
626396 err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
627397 if (err)
628
- goto out_drop_skcipher;
398
+ goto err_free_inst;
629399
630
- inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
631
- (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
632
-
400
+ inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
401
+ (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
633402 inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
634403 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
635404 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
....@@ -648,10 +417,8 @@
648417
649418 err = skcipher_register_instance(tmpl, inst);
650419 if (err) {
651
-out_drop_skcipher:
652
- crypto_drop_skcipher(&ctx->spawn);
653
-out_free_inst:
654
- kfree(inst);
420
+err_free_inst:
421
+ cryptd_skcipher_free(inst);
655422 }
656423 return err;
657424 }
....@@ -687,15 +454,11 @@
687454 {
688455 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
689456 struct crypto_shash *child = ctx->child;
690
- int err;
691457
692458 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
693459 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
694460 CRYPTO_TFM_REQ_MASK);
695
- err = crypto_shash_setkey(child, key, keylen);
696
- crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
697
- CRYPTO_TFM_RES_MASK);
698
- return err;
461
+ return crypto_shash_setkey(child, key, keylen);
699462 }
700463
701464 static int cryptd_hash_enqueue(struct ahash_request *req,
....@@ -717,13 +480,13 @@
717480 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
718481 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
719482 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
720
- int refcnt = atomic_read(&ctx->refcnt);
483
+ int refcnt = refcount_read(&ctx->refcnt);
721484
722485 local_bh_disable();
723486 rctx->complete(&req->base, err);
724487 local_bh_enable();
725488
726
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
489
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
727490 crypto_free_ahash(tfm);
728491 }
729492
....@@ -739,7 +502,6 @@
739502 goto out;
740503
741504 desc->tfm = child;
742
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
743505
744506 err = crypto_shash_init(desc);
745507
....@@ -831,7 +593,6 @@
831593 goto out;
832594
833595 desc->tfm = child;
834
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
835596
836597 err = shash_ahash_digest(req, desc);
837598
....@@ -860,49 +621,53 @@
860621 struct shash_desc *desc = cryptd_shash_desc(req);
861622
862623 desc->tfm = ctx->child;
863
- desc->flags = req->base.flags;
864624
865625 return crypto_shash_import(desc, in);
866626 }
867627
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
+
868636 static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
637
+ struct crypto_attr_type *algt,
869638 struct cryptd_queue *queue)
870639 {
871640 struct hashd_instance_ctx *ctx;
872641 struct ahash_instance *inst;
873
- struct shash_alg *salg;
874
- struct crypto_alg *alg;
875
- u32 type = 0;
876
- u32 mask = 0;
642
+ struct shash_alg *alg;
643
+ u32 type;
644
+ u32 mask;
877645 int err;
878646
879
- cryptd_check_internal(tb, &type, &mask);
647
+ cryptd_type_and_mask(algt, &type, &mask);
880648
881
- salg = shash_attr_alg(tb[1], type, mask);
882
- if (IS_ERR(salg))
883
- return PTR_ERR(salg);
884
-
885
- alg = &salg->base;
886
- inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
887
- sizeof(*ctx));
888
- err = PTR_ERR(inst);
889
- if (IS_ERR(inst))
890
- goto out_put_alg;
649
+ inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
650
+ if (!inst)
651
+ return -ENOMEM;
891652
892653 ctx = ahash_instance_ctx(inst);
893654 ctx->queue = queue;
894655
895
- err = crypto_init_shash_spawn(&ctx->spawn, salg,
896
- ahash_crypto_instance(inst));
656
+ err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst),
657
+ crypto_attr_alg_name(tb[1]), type, mask);
897658 if (err)
898
- goto out_free_inst;
659
+ goto err_free_inst;
660
+ alg = crypto_spawn_shash_alg(&ctx->spawn);
899661
900
- inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
901
- (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
902
- CRYPTO_ALG_OPTIONAL_KEY));
662
+ err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base);
663
+ if (err)
664
+ goto err_free_inst;
903665
904
- inst->alg.halg.digestsize = salg->digestsize;
905
- 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;
906671 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
907672
908673 inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
....@@ -914,19 +679,17 @@
914679 inst->alg.finup = cryptd_hash_finup_enqueue;
915680 inst->alg.export = cryptd_hash_export;
916681 inst->alg.import = cryptd_hash_import;
917
- if (crypto_shash_alg_has_setkey(salg))
682
+ if (crypto_shash_alg_has_setkey(alg))
918683 inst->alg.setkey = cryptd_hash_setkey;
919684 inst->alg.digest = cryptd_hash_digest_enqueue;
920685
686
+ inst->free = cryptd_hash_free;
687
+
921688 err = ahash_register_instance(tmpl, inst);
922689 if (err) {
923
- crypto_drop_shash(&ctx->spawn);
924
-out_free_inst:
925
- kfree(inst);
690
+err_free_inst:
691
+ cryptd_hash_free(inst);
926692 }
927
-
928
-out_put_alg:
929
- crypto_mod_put(alg);
930693 return err;
931694 }
932695
....@@ -971,13 +734,13 @@
971734
972735 out:
973736 ctx = crypto_aead_ctx(tfm);
974
- refcnt = atomic_read(&ctx->refcnt);
737
+ refcnt = refcount_read(&ctx->refcnt);
975738
976739 local_bh_disable();
977740 compl(&req->base, err);
978741 local_bh_enable();
979742
980
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
743
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
981744 crypto_free_aead(tfm);
982745 }
983746
....@@ -1048,23 +811,27 @@
1048811 crypto_free_aead(ctx->child);
1049812 }
1050813
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
+
1051822 static int cryptd_create_aead(struct crypto_template *tmpl,
1052823 struct rtattr **tb,
824
+ struct crypto_attr_type *algt,
1053825 struct cryptd_queue *queue)
1054826 {
1055827 struct aead_instance_ctx *ctx;
1056828 struct aead_instance *inst;
1057829 struct aead_alg *alg;
1058
- const char *name;
1059
- u32 type = 0;
1060
- u32 mask = CRYPTO_ALG_ASYNC;
830
+ u32 type;
831
+ u32 mask;
1061832 int err;
1062833
1063
- cryptd_check_internal(tb, &type, &mask);
1064
-
1065
- name = crypto_attr_alg_name(tb[1]);
1066
- if (IS_ERR(name))
1067
- return PTR_ERR(name);
834
+ cryptd_type_and_mask(algt, &type, &mask);
1068835
1069836 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
1070837 if (!inst)
....@@ -1073,18 +840,18 @@
1073840 ctx = aead_instance_ctx(inst);
1074841 ctx->queue = queue;
1075842
1076
- crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
1077
- 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);
1078845 if (err)
1079
- goto out_free_inst;
846
+ goto err_free_inst;
1080847
1081848 alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
1082849 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
1083850 if (err)
1084
- goto out_drop_aead;
851
+ goto err_free_inst;
1085852
1086
- inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
1087
- (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
853
+ inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
854
+ (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
1088855 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
1089856
1090857 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
....@@ -1097,12 +864,12 @@
1097864 inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
1098865 inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
1099866
867
+ inst->free = cryptd_aead_free;
868
+
1100869 err = aead_register_instance(tmpl, inst);
1101870 if (err) {
1102
-out_drop_aead:
1103
- crypto_drop_aead(&ctx->aead_spawn);
1104
-out_free_inst:
1105
- kfree(inst);
871
+err_free_inst:
872
+ cryptd_aead_free(inst);
1106873 }
1107874 return err;
1108875 }
....@@ -1118,100 +885,22 @@
1118885 return PTR_ERR(algt);
1119886
1120887 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
1121
- case CRYPTO_ALG_TYPE_BLKCIPHER:
1122
- if ((algt->type & CRYPTO_ALG_TYPE_MASK) ==
1123
- CRYPTO_ALG_TYPE_BLKCIPHER)
1124
- return cryptd_create_blkcipher(tmpl, tb, &queue);
1125
-
1126
- return cryptd_create_skcipher(tmpl, tb, &queue);
1127
- case CRYPTO_ALG_TYPE_DIGEST:
1128
- 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);
1129892 case CRYPTO_ALG_TYPE_AEAD:
1130
- return cryptd_create_aead(tmpl, tb, &queue);
893
+ return cryptd_create_aead(tmpl, tb, algt, &queue);
1131894 }
1132895
1133896 return -EINVAL;
1134897 }
1135898
1136
-static void cryptd_free(struct crypto_instance *inst)
1137
-{
1138
- struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
1139
- struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
1140
- struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
1141
-
1142
- switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
1143
- case CRYPTO_ALG_TYPE_AHASH:
1144
- crypto_drop_shash(&hctx->spawn);
1145
- kfree(ahash_instance(inst));
1146
- return;
1147
- case CRYPTO_ALG_TYPE_AEAD:
1148
- crypto_drop_aead(&aead_ctx->aead_spawn);
1149
- kfree(aead_instance(inst));
1150
- return;
1151
- default:
1152
- crypto_drop_spawn(&ctx->spawn);
1153
- kfree(inst);
1154
- }
1155
-}
1156
-
1157899 static struct crypto_template cryptd_tmpl = {
1158900 .name = "cryptd",
1159901 .create = cryptd_create,
1160
- .free = cryptd_free,
1161902 .module = THIS_MODULE,
1162903 };
1163
-
1164
-struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
1165
- u32 type, u32 mask)
1166
-{
1167
- char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
1168
- struct cryptd_blkcipher_ctx *ctx;
1169
- struct crypto_tfm *tfm;
1170
-
1171
- if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1172
- "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1173
- return ERR_PTR(-EINVAL);
1174
- type = crypto_skcipher_type(type);
1175
- mask &= ~CRYPTO_ALG_TYPE_MASK;
1176
- mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
1177
- tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
1178
- if (IS_ERR(tfm))
1179
- return ERR_CAST(tfm);
1180
- if (tfm->__crt_alg->cra_module != THIS_MODULE) {
1181
- crypto_free_tfm(tfm);
1182
- return ERR_PTR(-EINVAL);
1183
- }
1184
-
1185
- ctx = crypto_tfm_ctx(tfm);
1186
- atomic_set(&ctx->refcnt, 1);
1187
-
1188
- return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
1189
-}
1190
-EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
1191
-
1192
-struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
1193
-{
1194
- struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
1195
- return ctx->child;
1196
-}
1197
-EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
1198
-
1199
-bool cryptd_ablkcipher_queued(struct cryptd_ablkcipher *tfm)
1200
-{
1201
- struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
1202
-
1203
- return atomic_read(&ctx->refcnt) - 1;
1204
-}
1205
-EXPORT_SYMBOL_GPL(cryptd_ablkcipher_queued);
1206
-
1207
-void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
1208
-{
1209
- struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
1210
-
1211
- if (atomic_dec_and_test(&ctx->refcnt))
1212
- crypto_free_ablkcipher(&tfm->base);
1213
-}
1214
-EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
1215904
1216905 struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
1217906 u32 type, u32 mask)
....@@ -1234,7 +923,7 @@
1234923 }
1235924
1236925 ctx = crypto_skcipher_ctx(tfm);
1237
- atomic_set(&ctx->refcnt, 1);
926
+ refcount_set(&ctx->refcnt, 1);
1238927
1239928 return container_of(tfm, struct cryptd_skcipher, base);
1240929 }
....@@ -1252,7 +941,7 @@
1252941 {
1253942 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1254943
1255
- return atomic_read(&ctx->refcnt) - 1;
944
+ return refcount_read(&ctx->refcnt) - 1;
1256945 }
1257946 EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
1258947
....@@ -1260,7 +949,7 @@
1260949 {
1261950 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1262951
1263
- if (atomic_dec_and_test(&ctx->refcnt))
952
+ if (refcount_dec_and_test(&ctx->refcnt))
1264953 crypto_free_skcipher(&tfm->base);
1265954 }
1266955 EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
....@@ -1284,7 +973,7 @@
1284973 }
1285974
1286975 ctx = crypto_ahash_ctx(tfm);
1287
- atomic_set(&ctx->refcnt, 1);
976
+ refcount_set(&ctx->refcnt, 1);
1288977
1289978 return __cryptd_ahash_cast(tfm);
1290979 }
....@@ -1309,7 +998,7 @@
1309998 {
1310999 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
13111000
1312
- return atomic_read(&ctx->refcnt) - 1;
1001
+ return refcount_read(&ctx->refcnt) - 1;
13131002 }
13141003 EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
13151004
....@@ -1317,7 +1006,7 @@
13171006 {
13181007 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
13191008
1320
- if (atomic_dec_and_test(&ctx->refcnt))
1009
+ if (refcount_dec_and_test(&ctx->refcnt))
13211010 crypto_free_ahash(&tfm->base);
13221011 }
13231012 EXPORT_SYMBOL_GPL(cryptd_free_ahash);
....@@ -1341,7 +1030,7 @@
13411030 }
13421031
13431032 ctx = crypto_aead_ctx(tfm);
1344
- atomic_set(&ctx->refcnt, 1);
1033
+ refcount_set(&ctx->refcnt, 1);
13451034
13461035 return __cryptd_aead_cast(tfm);
13471036 }
....@@ -1359,7 +1048,7 @@
13591048 {
13601049 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
13611050
1362
- return atomic_read(&ctx->refcnt) - 1;
1051
+ return refcount_read(&ctx->refcnt) - 1;
13631052 }
13641053 EXPORT_SYMBOL_GPL(cryptd_aead_queued);
13651054
....@@ -1367,7 +1056,7 @@
13671056 {
13681057 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
13691058
1370
- if (atomic_dec_and_test(&ctx->refcnt))
1059
+ if (refcount_dec_and_test(&ctx->refcnt))
13711060 crypto_free_aead(&tfm->base);
13721061 }
13731062 EXPORT_SYMBOL_GPL(cryptd_free_aead);
....@@ -1376,19 +1065,31 @@
13761065 {
13771066 int err;
13781067
1068
+ cryptd_wq = alloc_workqueue("cryptd", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
1069
+ 1);
1070
+ if (!cryptd_wq)
1071
+ return -ENOMEM;
1072
+
13791073 err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
13801074 if (err)
1381
- return err;
1075
+ goto err_destroy_wq;
13821076
13831077 err = crypto_register_template(&cryptd_tmpl);
13841078 if (err)
1385
- cryptd_fini_queue(&queue);
1079
+ goto err_fini_queue;
13861080
1081
+ return 0;
1082
+
1083
+err_fini_queue:
1084
+ cryptd_fini_queue(&queue);
1085
+err_destroy_wq:
1086
+ destroy_workqueue(cryptd_wq);
13871087 return err;
13881088 }
13891089
13901090 static void __exit cryptd_exit(void)
13911091 {
1092
+ destroy_workqueue(cryptd_wq);
13921093 cryptd_fini_queue(&queue);
13931094 crypto_unregister_template(&cryptd_tmpl);
13941095 }