forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
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,18 +66,9 @@
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;
79
- struct crypto_skcipher *child;
70
+ refcount_t refcnt;
71
+ struct crypto_sync_skcipher *child;
8072 };
8173
8274 struct cryptd_skcipher_request_ctx {
....@@ -84,7 +76,7 @@
8476 };
8577
8678 struct cryptd_hash_ctx {
87
- atomic_t refcnt;
79
+ refcount_t refcnt;
8880 struct crypto_shash *child;
8981 };
9082
....@@ -94,7 +86,7 @@
9486 };
9587
9688 struct cryptd_aead_ctx {
97
- atomic_t refcnt;
89
+ refcount_t refcnt;
9890 struct crypto_aead *child;
9991 };
10092
....@@ -137,28 +129,28 @@
137129 static int cryptd_enqueue_request(struct cryptd_queue *queue,
138130 struct crypto_async_request *request)
139131 {
140
- int cpu, err;
132
+ int err;
141133 struct cryptd_cpu_queue *cpu_queue;
142
- atomic_t *refcnt;
134
+ refcount_t *refcnt;
143135
144
- cpu = get_cpu();
136
+ local_bh_disable();
145137 cpu_queue = this_cpu_ptr(queue->cpu_queue);
146138 err = crypto_enqueue_request(&cpu_queue->queue, request);
147139
148140 refcnt = crypto_tfm_ctx(request->tfm);
149141
150142 if (err == -ENOSPC)
151
- goto out_put_cpu;
143
+ goto out;
152144
153
- queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
145
+ queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work);
154146
155
- if (!atomic_read(refcnt))
156
- goto out_put_cpu;
147
+ if (!refcount_read(refcnt))
148
+ goto out;
157149
158
- atomic_inc(refcnt);
150
+ refcount_inc(refcnt);
159151
160
-out_put_cpu:
161
- put_cpu();
152
+out:
153
+ local_bh_enable();
162154
163155 return err;
164156 }
....@@ -174,15 +166,10 @@
174166 cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
175167 /*
176168 * 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.
180169 */
181170 local_bh_disable();
182
- preempt_disable();
183171 backlog = crypto_get_backlog(&cpu_queue->queue);
184172 req = crypto_dequeue_request(&cpu_queue->queue);
185
- preempt_enable();
186173 local_bh_enable();
187174
188175 if (!req)
....@@ -193,7 +180,7 @@
193180 req->complete(req, 0);
194181
195182 if (cpu_queue->queue.qlen)
196
- queue_work(kcrypto_wq, &cpu_queue->work);
183
+ queue_work(cryptd_wq, &cpu_queue->work);
197184 }
198185
199186 static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
....@@ -203,140 +190,20 @@
203190 return ictx->queue;
204191 }
205192
206
-static inline void cryptd_check_internal(struct rtattr **tb, u32 *type,
207
- u32 *mask)
193
+static void cryptd_type_and_mask(struct crypto_attr_type *algt,
194
+ u32 *type, u32 *mask)
208195 {
209
- struct crypto_attr_type *algt;
196
+ /*
197
+ * cryptd is allowed to wrap internal algorithms, but in that case the
198
+ * resulting cryptd instance will be marked as internal as well.
199
+ */
200
+ *type = algt->type & CRYPTO_ALG_INTERNAL;
201
+ *mask = algt->mask & CRYPTO_ALG_INTERNAL;
210202
211
- algt = crypto_get_attr_type(tb);
212
- if (IS_ERR(algt))
213
- return;
203
+ /* No point in cryptd wrapping an algorithm that's already async. */
204
+ *mask |= CRYPTO_ALG_ASYNC;
214205
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);
206
+ *mask |= crypto_algt_inherited_mask(algt);
340207 }
341208
342209 static int cryptd_init_instance(struct crypto_instance *inst,
....@@ -356,109 +223,17 @@
356223 return 0;
357224 }
358225
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
-
448226 static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
449227 const u8 *key, unsigned int keylen)
450228 {
451229 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
452
- struct crypto_skcipher *child = ctx->child;
453
- int err;
230
+ struct crypto_sync_skcipher *child = ctx->child;
454231
455
- crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
456
- crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
232
+ crypto_sync_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
233
+ crypto_sync_skcipher_set_flags(child,
234
+ crypto_skcipher_get_flags(parent) &
457235 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;
236
+ return crypto_sync_skcipher_setkey(child, key, keylen);
462237 }
463238
464239 static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
....@@ -466,13 +241,13 @@
466241 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
467242 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
468243 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
469
- int refcnt = atomic_read(&ctx->refcnt);
244
+ int refcnt = refcount_read(&ctx->refcnt);
470245
471246 local_bh_disable();
472247 rctx->complete(&req->base, err);
473248 local_bh_enable();
474249
475
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
250
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
476251 crypto_free_skcipher(tfm);
477252 }
478253
....@@ -483,13 +258,13 @@
483258 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
484259 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
485260 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
486
- struct crypto_skcipher *child = ctx->child;
487
- SKCIPHER_REQUEST_ON_STACK(subreq, child);
261
+ struct crypto_sync_skcipher *child = ctx->child;
262
+ SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
488263
489264 if (unlikely(err == -EINPROGRESS))
490265 goto out;
491266
492
- skcipher_request_set_tfm(subreq, child);
267
+ skcipher_request_set_sync_tfm(subreq, child);
493268 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
494269 NULL, NULL);
495270 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
....@@ -511,13 +286,13 @@
511286 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
512287 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
513288 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
514
- struct crypto_skcipher *child = ctx->child;
515
- SKCIPHER_REQUEST_ON_STACK(subreq, child);
289
+ struct crypto_sync_skcipher *child = ctx->child;
290
+ SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
516291
517292 if (unlikely(err == -EINPROGRESS))
518293 goto out;
519294
520
- skcipher_request_set_tfm(subreq, child);
295
+ skcipher_request_set_sync_tfm(subreq, child);
521296 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
522297 NULL, NULL);
523298 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
....@@ -568,7 +343,7 @@
568343 if (IS_ERR(cipher))
569344 return PTR_ERR(cipher);
570345
571
- ctx->child = cipher;
346
+ ctx->child = (struct crypto_sync_skcipher *)cipher;
572347 crypto_skcipher_set_reqsize(
573348 tfm, sizeof(struct cryptd_skcipher_request_ctx));
574349 return 0;
....@@ -578,7 +353,7 @@
578353 {
579354 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
580355
581
- crypto_free_skcipher(ctx->child);
356
+ crypto_free_sync_skcipher(ctx->child);
582357 }
583358
584359 static void cryptd_skcipher_free(struct skcipher_instance *inst)
....@@ -591,24 +366,17 @@
591366
592367 static int cryptd_create_skcipher(struct crypto_template *tmpl,
593368 struct rtattr **tb,
369
+ struct crypto_attr_type *algt,
594370 struct cryptd_queue *queue)
595371 {
596372 struct skcipherd_instance_ctx *ctx;
597373 struct skcipher_instance *inst;
598374 struct skcipher_alg *alg;
599
- const char *name;
600375 u32 type;
601376 u32 mask;
602377 int err;
603378
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);
379
+ cryptd_type_and_mask(algt, &type, &mask);
612380
613381 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
614382 if (!inst)
....@@ -617,19 +385,18 @@
617385 ctx = skcipher_instance_ctx(inst);
618386 ctx->queue = queue;
619387
620
- crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
621
- err = crypto_grab_skcipher(&ctx->spawn, name, type, mask);
388
+ err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
389
+ crypto_attr_alg_name(tb[1]), type, mask);
622390 if (err)
623
- goto out_free_inst;
391
+ goto err_free_inst;
624392
625393 alg = crypto_spawn_skcipher_alg(&ctx->spawn);
626394 err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
627395 if (err)
628
- goto out_drop_skcipher;
396
+ goto err_free_inst;
629397
630
- inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
631
- (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
632
-
398
+ inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
399
+ (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
633400 inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
634401 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
635402 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
....@@ -648,10 +415,8 @@
648415
649416 err = skcipher_register_instance(tmpl, inst);
650417 if (err) {
651
-out_drop_skcipher:
652
- crypto_drop_skcipher(&ctx->spawn);
653
-out_free_inst:
654
- kfree(inst);
418
+err_free_inst:
419
+ cryptd_skcipher_free(inst);
655420 }
656421 return err;
657422 }
....@@ -687,15 +452,11 @@
687452 {
688453 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
689454 struct crypto_shash *child = ctx->child;
690
- int err;
691455
692456 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
693457 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
694458 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;
459
+ return crypto_shash_setkey(child, key, keylen);
699460 }
700461
701462 static int cryptd_hash_enqueue(struct ahash_request *req,
....@@ -717,13 +478,13 @@
717478 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
718479 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
719480 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
720
- int refcnt = atomic_read(&ctx->refcnt);
481
+ int refcnt = refcount_read(&ctx->refcnt);
721482
722483 local_bh_disable();
723484 rctx->complete(&req->base, err);
724485 local_bh_enable();
725486
726
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
487
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
727488 crypto_free_ahash(tfm);
728489 }
729490
....@@ -739,7 +500,6 @@
739500 goto out;
740501
741502 desc->tfm = child;
742
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
743503
744504 err = crypto_shash_init(desc);
745505
....@@ -831,7 +591,6 @@
831591 goto out;
832592
833593 desc->tfm = child;
834
- desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
835594
836595 err = shash_ahash_digest(req, desc);
837596
....@@ -860,49 +619,53 @@
860619 struct shash_desc *desc = cryptd_shash_desc(req);
861620
862621 desc->tfm = ctx->child;
863
- desc->flags = req->base.flags;
864622
865623 return crypto_shash_import(desc, in);
866624 }
867625
626
+static void cryptd_hash_free(struct ahash_instance *inst)
627
+{
628
+ struct hashd_instance_ctx *ctx = ahash_instance_ctx(inst);
629
+
630
+ crypto_drop_shash(&ctx->spawn);
631
+ kfree(inst);
632
+}
633
+
868634 static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
635
+ struct crypto_attr_type *algt,
869636 struct cryptd_queue *queue)
870637 {
871638 struct hashd_instance_ctx *ctx;
872639 struct ahash_instance *inst;
873
- struct shash_alg *salg;
874
- struct crypto_alg *alg;
875
- u32 type = 0;
876
- u32 mask = 0;
640
+ struct shash_alg *alg;
641
+ u32 type;
642
+ u32 mask;
877643 int err;
878644
879
- cryptd_check_internal(tb, &type, &mask);
645
+ cryptd_type_and_mask(algt, &type, &mask);
880646
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;
647
+ inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
648
+ if (!inst)
649
+ return -ENOMEM;
891650
892651 ctx = ahash_instance_ctx(inst);
893652 ctx->queue = queue;
894653
895
- err = crypto_init_shash_spawn(&ctx->spawn, salg,
896
- ahash_crypto_instance(inst));
654
+ err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst),
655
+ crypto_attr_alg_name(tb[1]), type, mask);
897656 if (err)
898
- goto out_free_inst;
657
+ goto err_free_inst;
658
+ alg = crypto_spawn_shash_alg(&ctx->spawn);
899659
900
- inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
901
- (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
902
- CRYPTO_ALG_OPTIONAL_KEY));
660
+ err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base);
661
+ if (err)
662
+ goto err_free_inst;
903663
904
- inst->alg.halg.digestsize = salg->digestsize;
905
- inst->alg.halg.statesize = salg->statesize;
664
+ inst->alg.halg.base.cra_flags |= CRYPTO_ALG_ASYNC |
665
+ (alg->base.cra_flags & (CRYPTO_ALG_INTERNAL|
666
+ CRYPTO_ALG_OPTIONAL_KEY));
667
+ inst->alg.halg.digestsize = alg->digestsize;
668
+ inst->alg.halg.statesize = alg->statesize;
906669 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
907670
908671 inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
....@@ -914,19 +677,17 @@
914677 inst->alg.finup = cryptd_hash_finup_enqueue;
915678 inst->alg.export = cryptd_hash_export;
916679 inst->alg.import = cryptd_hash_import;
917
- if (crypto_shash_alg_has_setkey(salg))
680
+ if (crypto_shash_alg_has_setkey(alg))
918681 inst->alg.setkey = cryptd_hash_setkey;
919682 inst->alg.digest = cryptd_hash_digest_enqueue;
920683
684
+ inst->free = cryptd_hash_free;
685
+
921686 err = ahash_register_instance(tmpl, inst);
922687 if (err) {
923
- crypto_drop_shash(&ctx->spawn);
924
-out_free_inst:
925
- kfree(inst);
688
+err_free_inst:
689
+ cryptd_hash_free(inst);
926690 }
927
-
928
-out_put_alg:
929
- crypto_mod_put(alg);
930691 return err;
931692 }
932693
....@@ -971,13 +732,13 @@
971732
972733 out:
973734 ctx = crypto_aead_ctx(tfm);
974
- refcnt = atomic_read(&ctx->refcnt);
735
+ refcnt = refcount_read(&ctx->refcnt);
975736
976737 local_bh_disable();
977738 compl(&req->base, err);
978739 local_bh_enable();
979740
980
- if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
741
+ if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
981742 crypto_free_aead(tfm);
982743 }
983744
....@@ -1048,23 +809,27 @@
1048809 crypto_free_aead(ctx->child);
1049810 }
1050811
812
+static void cryptd_aead_free(struct aead_instance *inst)
813
+{
814
+ struct aead_instance_ctx *ctx = aead_instance_ctx(inst);
815
+
816
+ crypto_drop_aead(&ctx->aead_spawn);
817
+ kfree(inst);
818
+}
819
+
1051820 static int cryptd_create_aead(struct crypto_template *tmpl,
1052821 struct rtattr **tb,
822
+ struct crypto_attr_type *algt,
1053823 struct cryptd_queue *queue)
1054824 {
1055825 struct aead_instance_ctx *ctx;
1056826 struct aead_instance *inst;
1057827 struct aead_alg *alg;
1058
- const char *name;
1059
- u32 type = 0;
1060
- u32 mask = CRYPTO_ALG_ASYNC;
828
+ u32 type;
829
+ u32 mask;
1061830 int err;
1062831
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);
832
+ cryptd_type_and_mask(algt, &type, &mask);
1068833
1069834 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
1070835 if (!inst)
....@@ -1073,18 +838,18 @@
1073838 ctx = aead_instance_ctx(inst);
1074839 ctx->queue = queue;
1075840
1076
- crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
1077
- err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask);
841
+ err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst),
842
+ crypto_attr_alg_name(tb[1]), type, mask);
1078843 if (err)
1079
- goto out_free_inst;
844
+ goto err_free_inst;
1080845
1081846 alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
1082847 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
1083848 if (err)
1084
- goto out_drop_aead;
849
+ goto err_free_inst;
1085850
1086
- inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
1087
- (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
851
+ inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
852
+ (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
1088853 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
1089854
1090855 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
....@@ -1097,12 +862,12 @@
1097862 inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
1098863 inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
1099864
865
+ inst->free = cryptd_aead_free;
866
+
1100867 err = aead_register_instance(tmpl, inst);
1101868 if (err) {
1102
-out_drop_aead:
1103
- crypto_drop_aead(&ctx->aead_spawn);
1104
-out_free_inst:
1105
- kfree(inst);
869
+err_free_inst:
870
+ cryptd_aead_free(inst);
1106871 }
1107872 return err;
1108873 }
....@@ -1118,100 +883,22 @@
1118883 return PTR_ERR(algt);
1119884
1120885 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);
886
+ case CRYPTO_ALG_TYPE_SKCIPHER:
887
+ return cryptd_create_skcipher(tmpl, tb, algt, &queue);
888
+ case CRYPTO_ALG_TYPE_HASH:
889
+ return cryptd_create_hash(tmpl, tb, algt, &queue);
1129890 case CRYPTO_ALG_TYPE_AEAD:
1130
- return cryptd_create_aead(tmpl, tb, &queue);
891
+ return cryptd_create_aead(tmpl, tb, algt, &queue);
1131892 }
1132893
1133894 return -EINVAL;
1134895 }
1135896
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
-
1157897 static struct crypto_template cryptd_tmpl = {
1158898 .name = "cryptd",
1159899 .create = cryptd_create,
1160
- .free = cryptd_free,
1161900 .module = THIS_MODULE,
1162901 };
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);
1215902
1216903 struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
1217904 u32 type, u32 mask)
....@@ -1234,7 +921,7 @@
1234921 }
1235922
1236923 ctx = crypto_skcipher_ctx(tfm);
1237
- atomic_set(&ctx->refcnt, 1);
924
+ refcount_set(&ctx->refcnt, 1);
1238925
1239926 return container_of(tfm, struct cryptd_skcipher, base);
1240927 }
....@@ -1244,7 +931,7 @@
1244931 {
1245932 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1246933
1247
- return ctx->child;
934
+ return &ctx->child->base;
1248935 }
1249936 EXPORT_SYMBOL_GPL(cryptd_skcipher_child);
1250937
....@@ -1252,7 +939,7 @@
1252939 {
1253940 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1254941
1255
- return atomic_read(&ctx->refcnt) - 1;
942
+ return refcount_read(&ctx->refcnt) - 1;
1256943 }
1257944 EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
1258945
....@@ -1260,7 +947,7 @@
1260947 {
1261948 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1262949
1263
- if (atomic_dec_and_test(&ctx->refcnt))
950
+ if (refcount_dec_and_test(&ctx->refcnt))
1264951 crypto_free_skcipher(&tfm->base);
1265952 }
1266953 EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
....@@ -1284,7 +971,7 @@
1284971 }
1285972
1286973 ctx = crypto_ahash_ctx(tfm);
1287
- atomic_set(&ctx->refcnt, 1);
974
+ refcount_set(&ctx->refcnt, 1);
1288975
1289976 return __cryptd_ahash_cast(tfm);
1290977 }
....@@ -1309,7 +996,7 @@
1309996 {
1310997 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1311998
1312
- return atomic_read(&ctx->refcnt) - 1;
999
+ return refcount_read(&ctx->refcnt) - 1;
13131000 }
13141001 EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
13151002
....@@ -1317,7 +1004,7 @@
13171004 {
13181005 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
13191006
1320
- if (atomic_dec_and_test(&ctx->refcnt))
1007
+ if (refcount_dec_and_test(&ctx->refcnt))
13211008 crypto_free_ahash(&tfm->base);
13221009 }
13231010 EXPORT_SYMBOL_GPL(cryptd_free_ahash);
....@@ -1341,7 +1028,7 @@
13411028 }
13421029
13431030 ctx = crypto_aead_ctx(tfm);
1344
- atomic_set(&ctx->refcnt, 1);
1031
+ refcount_set(&ctx->refcnt, 1);
13451032
13461033 return __cryptd_aead_cast(tfm);
13471034 }
....@@ -1359,7 +1046,7 @@
13591046 {
13601047 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
13611048
1362
- return atomic_read(&ctx->refcnt) - 1;
1049
+ return refcount_read(&ctx->refcnt) - 1;
13631050 }
13641051 EXPORT_SYMBOL_GPL(cryptd_aead_queued);
13651052
....@@ -1367,7 +1054,7 @@
13671054 {
13681055 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
13691056
1370
- if (atomic_dec_and_test(&ctx->refcnt))
1057
+ if (refcount_dec_and_test(&ctx->refcnt))
13711058 crypto_free_aead(&tfm->base);
13721059 }
13731060 EXPORT_SYMBOL_GPL(cryptd_free_aead);
....@@ -1376,19 +1063,31 @@
13761063 {
13771064 int err;
13781065
1066
+ cryptd_wq = alloc_workqueue("cryptd", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
1067
+ 1);
1068
+ if (!cryptd_wq)
1069
+ return -ENOMEM;
1070
+
13791071 err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
13801072 if (err)
1381
- return err;
1073
+ goto err_destroy_wq;
13821074
13831075 err = crypto_register_template(&cryptd_tmpl);
13841076 if (err)
1385
- cryptd_fini_queue(&queue);
1077
+ goto err_fini_queue;
13861078
1079
+ return 0;
1080
+
1081
+err_fini_queue:
1082
+ cryptd_fini_queue(&queue);
1083
+err_destroy_wq:
1084
+ destroy_workqueue(cryptd_wq);
13871085 return err;
13881086 }
13891087
13901088 static void __exit cryptd_exit(void)
13911089 {
1090
+ destroy_workqueue(cryptd_wq);
13921091 cryptd_fini_queue(&queue);
13931092 crypto_unregister_template(&cryptd_tmpl);
13941093 }