2 * Software multibuffer async crypto daemon.
4 * Copyright (c) 2014 Tim Chen <tim.c.chen@linux.intel.com>
6 * Adapted from crypto daemon.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <crypto/algapi.h>
16 #include <crypto/internal/hash.h>
17 #include <crypto/internal/aead.h>
18 #include <crypto/mcryptd.h>
19 #include <crypto/crypto_wq.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/scatterlist.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/hardirq.h>
30 #define MCRYPTD_MAX_CPU_QLEN 100
31 #define MCRYPTD_BATCH 9
33 static void *mcryptd_alloc_instance(struct crypto_alg
*alg
, unsigned int head
,
36 struct mcryptd_flush_list
{
37 struct list_head list
;
41 static struct mcryptd_flush_list __percpu
*mcryptd_flist
;
43 struct hashd_instance_ctx
{
44 struct crypto_shash_spawn spawn
;
45 struct mcryptd_queue
*queue
;
48 static void mcryptd_queue_worker(struct work_struct
*work
);
50 void mcryptd_arm_flusher(struct mcryptd_alg_cstate
*cstate
, unsigned long delay
)
52 struct mcryptd_flush_list
*flist
;
54 if (!cstate
->flusher_engaged
) {
55 /* put the flusher on the flush list */
56 flist
= per_cpu_ptr(mcryptd_flist
, smp_processor_id());
57 mutex_lock(&flist
->lock
);
58 list_add_tail(&cstate
->flush_list
, &flist
->list
);
59 cstate
->flusher_engaged
= true;
60 cstate
->next_flush
= jiffies
+ delay
;
61 queue_delayed_work_on(smp_processor_id(), kcrypto_wq
,
62 &cstate
->flush
, delay
);
63 mutex_unlock(&flist
->lock
);
66 EXPORT_SYMBOL(mcryptd_arm_flusher
);
68 static int mcryptd_init_queue(struct mcryptd_queue
*queue
,
69 unsigned int max_cpu_qlen
)
72 struct mcryptd_cpu_queue
*cpu_queue
;
74 queue
->cpu_queue
= alloc_percpu(struct mcryptd_cpu_queue
);
75 pr_debug("mqueue:%p mcryptd_cpu_queue %p\n", queue
, queue
->cpu_queue
);
76 if (!queue
->cpu_queue
)
78 for_each_possible_cpu(cpu
) {
79 cpu_queue
= per_cpu_ptr(queue
->cpu_queue
, cpu
);
80 pr_debug("cpu_queue #%d %p\n", cpu
, queue
->cpu_queue
);
81 crypto_init_queue(&cpu_queue
->queue
, max_cpu_qlen
);
82 INIT_WORK(&cpu_queue
->work
, mcryptd_queue_worker
);
87 static void mcryptd_fini_queue(struct mcryptd_queue
*queue
)
90 struct mcryptd_cpu_queue
*cpu_queue
;
92 for_each_possible_cpu(cpu
) {
93 cpu_queue
= per_cpu_ptr(queue
->cpu_queue
, cpu
);
94 BUG_ON(cpu_queue
->queue
.qlen
);
96 free_percpu(queue
->cpu_queue
);
99 static int mcryptd_enqueue_request(struct mcryptd_queue
*queue
,
100 struct crypto_async_request
*request
,
101 struct mcryptd_hash_request_ctx
*rctx
)
104 struct mcryptd_cpu_queue
*cpu_queue
;
107 cpu_queue
= this_cpu_ptr(queue
->cpu_queue
);
110 err
= crypto_enqueue_request(&cpu_queue
->queue
, request
);
111 pr_debug("enqueue request: cpu %d cpu_queue %p request %p\n",
112 cpu
, cpu_queue
, request
);
113 queue_work_on(cpu
, kcrypto_wq
, &cpu_queue
->work
);
120 * Try to opportunisticlly flush the partially completed jobs if
121 * crypto daemon is the only task running.
123 static void mcryptd_opportunistic_flush(void)
125 struct mcryptd_flush_list
*flist
;
126 struct mcryptd_alg_cstate
*cstate
;
128 flist
= per_cpu_ptr(mcryptd_flist
, smp_processor_id());
129 while (single_task_running()) {
130 mutex_lock(&flist
->lock
);
131 cstate
= list_first_entry_or_null(&flist
->list
,
132 struct mcryptd_alg_cstate
, flush_list
);
133 if (!cstate
|| !cstate
->flusher_engaged
) {
134 mutex_unlock(&flist
->lock
);
137 list_del(&cstate
->flush_list
);
138 cstate
->flusher_engaged
= false;
139 mutex_unlock(&flist
->lock
);
140 cstate
->alg_state
->flusher(cstate
);
145 * Called in workqueue context, do one real cryption work (via
146 * req->complete) and reschedule itself if there are more work to
149 static void mcryptd_queue_worker(struct work_struct
*work
)
151 struct mcryptd_cpu_queue
*cpu_queue
;
152 struct crypto_async_request
*req
, *backlog
;
156 * Need to loop through more than once for multi-buffer to
160 cpu_queue
= container_of(work
, struct mcryptd_cpu_queue
, work
);
162 while (i
< MCRYPTD_BATCH
|| single_task_running()) {
164 * preempt_disable/enable is used to prevent
165 * being preempted by mcryptd_enqueue_request()
169 backlog
= crypto_get_backlog(&cpu_queue
->queue
);
170 req
= crypto_dequeue_request(&cpu_queue
->queue
);
175 mcryptd_opportunistic_flush();
180 backlog
->complete(backlog
, -EINPROGRESS
);
181 req
->complete(req
, 0);
182 if (!cpu_queue
->queue
.qlen
)
186 if (cpu_queue
->queue
.qlen
)
187 queue_work(kcrypto_wq
, &cpu_queue
->work
);
190 void mcryptd_flusher(struct work_struct
*__work
)
192 struct mcryptd_alg_cstate
*alg_cpu_state
;
193 struct mcryptd_alg_state
*alg_state
;
194 struct mcryptd_flush_list
*flist
;
197 cpu
= smp_processor_id();
198 alg_cpu_state
= container_of(to_delayed_work(__work
),
199 struct mcryptd_alg_cstate
, flush
);
200 alg_state
= alg_cpu_state
->alg_state
;
201 if (alg_cpu_state
->cpu
!= cpu
)
202 pr_debug("mcryptd error: work on cpu %d, should be cpu %d\n",
203 cpu
, alg_cpu_state
->cpu
);
205 if (alg_cpu_state
->flusher_engaged
) {
206 flist
= per_cpu_ptr(mcryptd_flist
, cpu
);
207 mutex_lock(&flist
->lock
);
208 list_del(&alg_cpu_state
->flush_list
);
209 alg_cpu_state
->flusher_engaged
= false;
210 mutex_unlock(&flist
->lock
);
211 alg_state
->flusher(alg_cpu_state
);
214 EXPORT_SYMBOL_GPL(mcryptd_flusher
);
216 static inline struct mcryptd_queue
*mcryptd_get_queue(struct crypto_tfm
*tfm
)
218 struct crypto_instance
*inst
= crypto_tfm_alg_instance(tfm
);
219 struct mcryptd_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
224 static void *mcryptd_alloc_instance(struct crypto_alg
*alg
, unsigned int head
,
228 struct crypto_instance
*inst
;
231 p
= kzalloc(head
+ sizeof(*inst
) + tail
, GFP_KERNEL
);
233 return ERR_PTR(-ENOMEM
);
235 inst
= (void *)(p
+ head
);
238 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
239 "mcryptd(%s)", alg
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
242 memcpy(inst
->alg
.cra_name
, alg
->cra_name
, CRYPTO_MAX_ALG_NAME
);
244 inst
->alg
.cra_priority
= alg
->cra_priority
+ 50;
245 inst
->alg
.cra_blocksize
= alg
->cra_blocksize
;
246 inst
->alg
.cra_alignmask
= alg
->cra_alignmask
;
257 static inline void mcryptd_check_internal(struct rtattr
**tb
, u32
*type
,
260 struct crypto_attr_type
*algt
;
262 algt
= crypto_get_attr_type(tb
);
265 if ((algt
->type
& CRYPTO_ALG_INTERNAL
))
266 *type
|= CRYPTO_ALG_INTERNAL
;
267 if ((algt
->mask
& CRYPTO_ALG_INTERNAL
))
268 *mask
|= CRYPTO_ALG_INTERNAL
;
271 static int mcryptd_hash_init_tfm(struct crypto_tfm
*tfm
)
273 struct crypto_instance
*inst
= crypto_tfm_alg_instance(tfm
);
274 struct hashd_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
275 struct crypto_shash_spawn
*spawn
= &ictx
->spawn
;
276 struct mcryptd_hash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
277 struct crypto_shash
*hash
;
279 hash
= crypto_spawn_shash(spawn
);
281 return PTR_ERR(hash
);
284 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
285 sizeof(struct mcryptd_hash_request_ctx
) +
286 crypto_shash_descsize(hash
));
290 static void mcryptd_hash_exit_tfm(struct crypto_tfm
*tfm
)
292 struct mcryptd_hash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
294 crypto_free_shash(ctx
->child
);
297 static int mcryptd_hash_setkey(struct crypto_ahash
*parent
,
298 const u8
*key
, unsigned int keylen
)
300 struct mcryptd_hash_ctx
*ctx
= crypto_ahash_ctx(parent
);
301 struct crypto_shash
*child
= ctx
->child
;
304 crypto_shash_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
305 crypto_shash_set_flags(child
, crypto_ahash_get_flags(parent
) &
306 CRYPTO_TFM_REQ_MASK
);
307 err
= crypto_shash_setkey(child
, key
, keylen
);
308 crypto_ahash_set_flags(parent
, crypto_shash_get_flags(child
) &
309 CRYPTO_TFM_RES_MASK
);
313 static int mcryptd_hash_enqueue(struct ahash_request
*req
,
314 crypto_completion_t complete
)
318 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
319 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
320 struct mcryptd_queue
*queue
=
321 mcryptd_get_queue(crypto_ahash_tfm(tfm
));
323 rctx
->complete
= req
->base
.complete
;
324 req
->base
.complete
= complete
;
326 ret
= mcryptd_enqueue_request(queue
, &req
->base
, rctx
);
331 static void mcryptd_hash_init(struct crypto_async_request
*req_async
, int err
)
333 struct mcryptd_hash_ctx
*ctx
= crypto_tfm_ctx(req_async
->tfm
);
334 struct crypto_shash
*child
= ctx
->child
;
335 struct ahash_request
*req
= ahash_request_cast(req_async
);
336 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
337 struct shash_desc
*desc
= &rctx
->desc
;
339 if (unlikely(err
== -EINPROGRESS
))
343 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
345 err
= crypto_shash_init(desc
);
347 req
->base
.complete
= rctx
->complete
;
351 rctx
->complete(&req
->base
, err
);
355 static int mcryptd_hash_init_enqueue(struct ahash_request
*req
)
357 return mcryptd_hash_enqueue(req
, mcryptd_hash_init
);
360 static void mcryptd_hash_update(struct crypto_async_request
*req_async
, int err
)
362 struct ahash_request
*req
= ahash_request_cast(req_async
);
363 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
365 if (unlikely(err
== -EINPROGRESS
))
368 err
= shash_ahash_mcryptd_update(req
, &rctx
->desc
);
370 req
->base
.complete
= rctx
->complete
;
377 rctx
->complete(&req
->base
, err
);
381 static int mcryptd_hash_update_enqueue(struct ahash_request
*req
)
383 return mcryptd_hash_enqueue(req
, mcryptd_hash_update
);
386 static void mcryptd_hash_final(struct crypto_async_request
*req_async
, int err
)
388 struct ahash_request
*req
= ahash_request_cast(req_async
);
389 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
391 if (unlikely(err
== -EINPROGRESS
))
394 err
= shash_ahash_mcryptd_final(req
, &rctx
->desc
);
396 req
->base
.complete
= rctx
->complete
;
403 rctx
->complete(&req
->base
, err
);
407 static int mcryptd_hash_final_enqueue(struct ahash_request
*req
)
409 return mcryptd_hash_enqueue(req
, mcryptd_hash_final
);
412 static void mcryptd_hash_finup(struct crypto_async_request
*req_async
, int err
)
414 struct ahash_request
*req
= ahash_request_cast(req_async
);
415 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
417 if (unlikely(err
== -EINPROGRESS
))
420 err
= shash_ahash_mcryptd_finup(req
, &rctx
->desc
);
423 req
->base
.complete
= rctx
->complete
;
430 rctx
->complete(&req
->base
, err
);
434 static int mcryptd_hash_finup_enqueue(struct ahash_request
*req
)
436 return mcryptd_hash_enqueue(req
, mcryptd_hash_finup
);
439 static void mcryptd_hash_digest(struct crypto_async_request
*req_async
, int err
)
441 struct mcryptd_hash_ctx
*ctx
= crypto_tfm_ctx(req_async
->tfm
);
442 struct crypto_shash
*child
= ctx
->child
;
443 struct ahash_request
*req
= ahash_request_cast(req_async
);
444 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
445 struct shash_desc
*desc
= &rctx
->desc
;
447 if (unlikely(err
== -EINPROGRESS
))
451 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
; /* check this again */
453 err
= shash_ahash_mcryptd_digest(req
, desc
);
456 req
->base
.complete
= rctx
->complete
;
463 rctx
->complete(&req
->base
, err
);
467 static int mcryptd_hash_digest_enqueue(struct ahash_request
*req
)
469 return mcryptd_hash_enqueue(req
, mcryptd_hash_digest
);
472 static int mcryptd_hash_export(struct ahash_request
*req
, void *out
)
474 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
476 return crypto_shash_export(&rctx
->desc
, out
);
479 static int mcryptd_hash_import(struct ahash_request
*req
, const void *in
)
481 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
483 return crypto_shash_import(&rctx
->desc
, in
);
486 static int mcryptd_create_hash(struct crypto_template
*tmpl
, struct rtattr
**tb
,
487 struct mcryptd_queue
*queue
)
489 struct hashd_instance_ctx
*ctx
;
490 struct ahash_instance
*inst
;
491 struct shash_alg
*salg
;
492 struct crypto_alg
*alg
;
497 mcryptd_check_internal(tb
, &type
, &mask
);
499 salg
= shash_attr_alg(tb
[1], type
, mask
);
501 return PTR_ERR(salg
);
504 pr_debug("crypto: mcryptd hash alg: %s\n", alg
->cra_name
);
505 inst
= mcryptd_alloc_instance(alg
, ahash_instance_headroom(),
511 ctx
= ahash_instance_ctx(inst
);
514 err
= crypto_init_shash_spawn(&ctx
->spawn
, salg
,
515 ahash_crypto_instance(inst
));
519 type
= CRYPTO_ALG_ASYNC
;
520 if (alg
->cra_flags
& CRYPTO_ALG_INTERNAL
)
521 type
|= CRYPTO_ALG_INTERNAL
;
522 inst
->alg
.halg
.base
.cra_flags
= type
;
524 inst
->alg
.halg
.digestsize
= salg
->digestsize
;
525 inst
->alg
.halg
.base
.cra_ctxsize
= sizeof(struct mcryptd_hash_ctx
);
527 inst
->alg
.halg
.base
.cra_init
= mcryptd_hash_init_tfm
;
528 inst
->alg
.halg
.base
.cra_exit
= mcryptd_hash_exit_tfm
;
530 inst
->alg
.init
= mcryptd_hash_init_enqueue
;
531 inst
->alg
.update
= mcryptd_hash_update_enqueue
;
532 inst
->alg
.final
= mcryptd_hash_final_enqueue
;
533 inst
->alg
.finup
= mcryptd_hash_finup_enqueue
;
534 inst
->alg
.export
= mcryptd_hash_export
;
535 inst
->alg
.import
= mcryptd_hash_import
;
536 inst
->alg
.setkey
= mcryptd_hash_setkey
;
537 inst
->alg
.digest
= mcryptd_hash_digest_enqueue
;
539 err
= ahash_register_instance(tmpl
, inst
);
541 crypto_drop_shash(&ctx
->spawn
);
551 static struct mcryptd_queue mqueue
;
553 static int mcryptd_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
555 struct crypto_attr_type
*algt
;
557 algt
= crypto_get_attr_type(tb
);
559 return PTR_ERR(algt
);
561 switch (algt
->type
& algt
->mask
& CRYPTO_ALG_TYPE_MASK
) {
562 case CRYPTO_ALG_TYPE_DIGEST
:
563 return mcryptd_create_hash(tmpl
, tb
, &mqueue
);
570 static void mcryptd_free(struct crypto_instance
*inst
)
572 struct mcryptd_instance_ctx
*ctx
= crypto_instance_ctx(inst
);
573 struct hashd_instance_ctx
*hctx
= crypto_instance_ctx(inst
);
575 switch (inst
->alg
.cra_flags
& CRYPTO_ALG_TYPE_MASK
) {
576 case CRYPTO_ALG_TYPE_AHASH
:
577 crypto_drop_shash(&hctx
->spawn
);
578 kfree(ahash_instance(inst
));
581 crypto_drop_spawn(&ctx
->spawn
);
586 static struct crypto_template mcryptd_tmpl
= {
588 .create
= mcryptd_create
,
589 .free
= mcryptd_free
,
590 .module
= THIS_MODULE
,
593 struct mcryptd_ahash
*mcryptd_alloc_ahash(const char *alg_name
,
596 char mcryptd_alg_name
[CRYPTO_MAX_ALG_NAME
];
597 struct crypto_ahash
*tfm
;
599 if (snprintf(mcryptd_alg_name
, CRYPTO_MAX_ALG_NAME
,
600 "mcryptd(%s)", alg_name
) >= CRYPTO_MAX_ALG_NAME
)
601 return ERR_PTR(-EINVAL
);
602 tfm
= crypto_alloc_ahash(mcryptd_alg_name
, type
, mask
);
604 return ERR_CAST(tfm
);
605 if (tfm
->base
.__crt_alg
->cra_module
!= THIS_MODULE
) {
606 crypto_free_ahash(tfm
);
607 return ERR_PTR(-EINVAL
);
610 return __mcryptd_ahash_cast(tfm
);
612 EXPORT_SYMBOL_GPL(mcryptd_alloc_ahash
);
614 int shash_ahash_mcryptd_digest(struct ahash_request
*req
,
615 struct shash_desc
*desc
)
619 err
= crypto_shash_init(desc
) ?:
620 shash_ahash_mcryptd_finup(req
, desc
);
624 EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_digest
);
626 int shash_ahash_mcryptd_update(struct ahash_request
*req
,
627 struct shash_desc
*desc
)
629 struct crypto_shash
*tfm
= desc
->tfm
;
630 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
632 /* alignment is to be done by multi-buffer crypto algorithm if needed */
634 return shash
->update(desc
, NULL
, 0);
636 EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_update
);
638 int shash_ahash_mcryptd_finup(struct ahash_request
*req
,
639 struct shash_desc
*desc
)
641 struct crypto_shash
*tfm
= desc
->tfm
;
642 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
644 /* alignment is to be done by multi-buffer crypto algorithm if needed */
646 return shash
->finup(desc
, NULL
, 0, req
->result
);
648 EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_finup
);
650 int shash_ahash_mcryptd_final(struct ahash_request
*req
,
651 struct shash_desc
*desc
)
653 struct crypto_shash
*tfm
= desc
->tfm
;
654 struct shash_alg
*shash
= crypto_shash_alg(tfm
);
656 /* alignment is to be done by multi-buffer crypto algorithm if needed */
658 return shash
->final(desc
, req
->result
);
660 EXPORT_SYMBOL_GPL(shash_ahash_mcryptd_final
);
662 struct crypto_shash
*mcryptd_ahash_child(struct mcryptd_ahash
*tfm
)
664 struct mcryptd_hash_ctx
*ctx
= crypto_ahash_ctx(&tfm
->base
);
668 EXPORT_SYMBOL_GPL(mcryptd_ahash_child
);
670 struct shash_desc
*mcryptd_shash_desc(struct ahash_request
*req
)
672 struct mcryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
675 EXPORT_SYMBOL_GPL(mcryptd_shash_desc
);
677 void mcryptd_free_ahash(struct mcryptd_ahash
*tfm
)
679 crypto_free_ahash(&tfm
->base
);
681 EXPORT_SYMBOL_GPL(mcryptd_free_ahash
);
684 static int __init
mcryptd_init(void)
687 struct mcryptd_flush_list
*flist
;
689 mcryptd_flist
= alloc_percpu(struct mcryptd_flush_list
);
690 for_each_possible_cpu(cpu
) {
691 flist
= per_cpu_ptr(mcryptd_flist
, cpu
);
692 INIT_LIST_HEAD(&flist
->list
);
693 mutex_init(&flist
->lock
);
696 err
= mcryptd_init_queue(&mqueue
, MCRYPTD_MAX_CPU_QLEN
);
698 free_percpu(mcryptd_flist
);
702 err
= crypto_register_template(&mcryptd_tmpl
);
704 mcryptd_fini_queue(&mqueue
);
705 free_percpu(mcryptd_flist
);
711 static void __exit
mcryptd_exit(void)
713 mcryptd_fini_queue(&mqueue
);
714 crypto_unregister_template(&mcryptd_tmpl
);
715 free_percpu(mcryptd_flist
);
718 subsys_initcall(mcryptd_init
);
719 module_exit(mcryptd_exit
);
721 MODULE_LICENSE("GPL");
722 MODULE_DESCRIPTION("Software async multibuffer crypto daemon");
723 MODULE_ALIAS_CRYPTO("mcryptd");