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/sched/stat.h>
28 #include <linux/slab.h>
29 #include <linux/hardirq.h>
31 #define MCRYPTD_MAX_CPU_QLEN 100
32 #define MCRYPTD_BATCH 9
34 static void *mcryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
37 struct mcryptd_flush_list {
38 struct list_head list;
42 static struct mcryptd_flush_list __percpu *mcryptd_flist;
44 struct hashd_instance_ctx {
45 struct crypto_ahash_spawn spawn;
46 struct mcryptd_queue *queue;
49 static void mcryptd_queue_worker(struct work_struct *work);
51 void mcryptd_arm_flusher(struct mcryptd_alg_cstate *cstate, unsigned long delay)
53 struct mcryptd_flush_list *flist;
55 if (!cstate->flusher_engaged) {
56 /* put the flusher on the flush list */
57 flist = per_cpu_ptr(mcryptd_flist, smp_processor_id());
58 mutex_lock(&flist->lock);
59 list_add_tail(&cstate->flush_list, &flist->list);
60 cstate->flusher_engaged = true;
61 cstate->next_flush = jiffies + delay;
62 queue_delayed_work_on(smp_processor_id(), kcrypto_wq,
63 &cstate->flush, delay);
64 mutex_unlock(&flist->lock);
67 EXPORT_SYMBOL(mcryptd_arm_flusher);
69 static int mcryptd_init_queue(struct mcryptd_queue *queue,
70 unsigned int max_cpu_qlen)
73 struct mcryptd_cpu_queue *cpu_queue;
75 queue->cpu_queue = alloc_percpu(struct mcryptd_cpu_queue);
76 pr_debug("mqueue:%p mcryptd_cpu_queue %p\n", queue, queue->cpu_queue);
77 if (!queue->cpu_queue)
79 for_each_possible_cpu(cpu) {
80 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
81 pr_debug("cpu_queue #%d %p\n", cpu, queue->cpu_queue);
82 crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
83 INIT_WORK(&cpu_queue->work, mcryptd_queue_worker);
84 spin_lock_init(&cpu_queue->q_lock);
89 static void mcryptd_fini_queue(struct mcryptd_queue *queue)
92 struct mcryptd_cpu_queue *cpu_queue;
94 for_each_possible_cpu(cpu) {
95 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
96 BUG_ON(cpu_queue->queue.qlen);
98 free_percpu(queue->cpu_queue);
101 static int mcryptd_enqueue_request(struct mcryptd_queue *queue,
102 struct crypto_async_request *request,
103 struct mcryptd_hash_request_ctx *rctx)
106 struct mcryptd_cpu_queue *cpu_queue;
108 cpu_queue = raw_cpu_ptr(queue->cpu_queue);
109 spin_lock(&cpu_queue->q_lock);
110 cpu = smp_processor_id();
111 rctx->tag.cpu = smp_processor_id();
113 err = crypto_enqueue_request(&cpu_queue->queue, request);
114 pr_debug("enqueue request: cpu %d cpu_queue %p request %p\n",
115 cpu, cpu_queue, request);
116 spin_unlock(&cpu_queue->q_lock);
117 queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
123 * Try to opportunisticlly flush the partially completed jobs if
124 * crypto daemon is the only task running.
126 static void mcryptd_opportunistic_flush(void)
128 struct mcryptd_flush_list *flist;
129 struct mcryptd_alg_cstate *cstate;
131 flist = per_cpu_ptr(mcryptd_flist, smp_processor_id());
132 while (single_task_running()) {
133 mutex_lock(&flist->lock);
134 cstate = list_first_entry_or_null(&flist->list,
135 struct mcryptd_alg_cstate, flush_list);
136 if (!cstate || !cstate->flusher_engaged) {
137 mutex_unlock(&flist->lock);
140 list_del(&cstate->flush_list);
141 cstate->flusher_engaged = false;
142 mutex_unlock(&flist->lock);
143 cstate->alg_state->flusher(cstate);
148 * Called in workqueue context, do one real cryption work (via
149 * req->complete) and reschedule itself if there are more work to
152 static void mcryptd_queue_worker(struct work_struct *work)
154 struct mcryptd_cpu_queue *cpu_queue;
155 struct crypto_async_request *req, *backlog;
159 * Need to loop through more than once for multi-buffer to
163 cpu_queue = container_of(work, struct mcryptd_cpu_queue, work);
165 while (i < MCRYPTD_BATCH || single_task_running()) {
167 spin_lock_bh(&cpu_queue->q_lock);
168 backlog = crypto_get_backlog(&cpu_queue->queue);
169 req = crypto_dequeue_request(&cpu_queue->queue);
170 spin_unlock_bh(&cpu_queue->q_lock);
173 mcryptd_opportunistic_flush();
178 backlog->complete(backlog, -EINPROGRESS);
179 req->complete(req, 0);
180 if (!cpu_queue->queue.qlen)
184 if (cpu_queue->queue.qlen)
185 queue_work_on(smp_processor_id(), kcrypto_wq, &cpu_queue->work);
188 void mcryptd_flusher(struct work_struct *__work)
190 struct mcryptd_alg_cstate *alg_cpu_state;
191 struct mcryptd_alg_state *alg_state;
192 struct mcryptd_flush_list *flist;
195 cpu = smp_processor_id();
196 alg_cpu_state = container_of(to_delayed_work(__work),
197 struct mcryptd_alg_cstate, flush);
198 alg_state = alg_cpu_state->alg_state;
199 if (alg_cpu_state->cpu != cpu)
200 pr_debug("mcryptd error: work on cpu %d, should be cpu %d\n",
201 cpu, alg_cpu_state->cpu);
203 if (alg_cpu_state->flusher_engaged) {
204 flist = per_cpu_ptr(mcryptd_flist, cpu);
205 mutex_lock(&flist->lock);
206 list_del(&alg_cpu_state->flush_list);
207 alg_cpu_state->flusher_engaged = false;
208 mutex_unlock(&flist->lock);
209 alg_state->flusher(alg_cpu_state);
212 EXPORT_SYMBOL_GPL(mcryptd_flusher);
214 static inline struct mcryptd_queue *mcryptd_get_queue(struct crypto_tfm *tfm)
216 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
217 struct mcryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
222 static void *mcryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
226 struct crypto_instance *inst;
229 p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
231 return ERR_PTR(-ENOMEM);
233 inst = (void *)(p + head);
236 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
237 "mcryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
240 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
242 inst->alg.cra_priority = alg->cra_priority + 50;
243 inst->alg.cra_blocksize = alg->cra_blocksize;
244 inst->alg.cra_alignmask = alg->cra_alignmask;
255 static inline bool mcryptd_check_internal(struct rtattr **tb, u32 *type,
258 struct crypto_attr_type *algt;
260 algt = crypto_get_attr_type(tb);
264 *type |= algt->type & CRYPTO_ALG_INTERNAL;
265 *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
267 if (*type & *mask & CRYPTO_ALG_INTERNAL)
273 static int mcryptd_hash_init_tfm(struct crypto_tfm *tfm)
275 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
276 struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
277 struct crypto_ahash_spawn *spawn = &ictx->spawn;
278 struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
279 struct crypto_ahash *hash;
281 hash = crypto_spawn_ahash(spawn);
283 return PTR_ERR(hash);
286 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
287 sizeof(struct mcryptd_hash_request_ctx) +
288 crypto_ahash_reqsize(hash));
292 static void mcryptd_hash_exit_tfm(struct crypto_tfm *tfm)
294 struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
296 crypto_free_ahash(ctx->child);
299 static int mcryptd_hash_setkey(struct crypto_ahash *parent,
300 const u8 *key, unsigned int keylen)
302 struct mcryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
303 struct crypto_ahash *child = ctx->child;
306 crypto_ahash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
307 crypto_ahash_set_flags(child, crypto_ahash_get_flags(parent) &
308 CRYPTO_TFM_REQ_MASK);
309 err = crypto_ahash_setkey(child, key, keylen);
310 crypto_ahash_set_flags(parent, crypto_ahash_get_flags(child) &
311 CRYPTO_TFM_RES_MASK);
315 static int mcryptd_hash_enqueue(struct ahash_request *req,
316 crypto_completion_t complete)
320 struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
321 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
322 struct mcryptd_queue *queue =
323 mcryptd_get_queue(crypto_ahash_tfm(tfm));
325 rctx->complete = req->base.complete;
326 req->base.complete = complete;
328 ret = mcryptd_enqueue_request(queue, &req->base, rctx);
333 static void mcryptd_hash_init(struct crypto_async_request *req_async, int err)
335 struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
336 struct crypto_ahash *child = ctx->child;
337 struct ahash_request *req = ahash_request_cast(req_async);
338 struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
339 struct ahash_request *desc = &rctx->areq;
341 if (unlikely(err == -EINPROGRESS))
344 ahash_request_set_tfm(desc, child);
345 ahash_request_set_callback(desc, CRYPTO_TFM_REQ_MAY_SLEEP,
346 rctx->complete, req_async);
348 rctx->out = req->result;
349 err = crypto_ahash_init(desc);
353 rctx->complete(&req->base, err);
357 static int mcryptd_hash_init_enqueue(struct ahash_request *req)
359 return mcryptd_hash_enqueue(req, mcryptd_hash_init);
362 static void mcryptd_hash_update(struct crypto_async_request *req_async, int err)
364 struct ahash_request *req = ahash_request_cast(req_async);
365 struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
367 if (unlikely(err == -EINPROGRESS))
370 rctx->out = req->result;
371 err = ahash_mcryptd_update(&rctx->areq);
373 req->base.complete = rctx->complete;
380 rctx->complete(&req->base, err);
384 static int mcryptd_hash_update_enqueue(struct ahash_request *req)
386 return mcryptd_hash_enqueue(req, mcryptd_hash_update);
389 static void mcryptd_hash_final(struct crypto_async_request *req_async, int err)
391 struct ahash_request *req = ahash_request_cast(req_async);
392 struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
394 if (unlikely(err == -EINPROGRESS))
397 rctx->out = req->result;
398 err = ahash_mcryptd_final(&rctx->areq);
400 req->base.complete = rctx->complete;
407 rctx->complete(&req->base, err);
411 static int mcryptd_hash_final_enqueue(struct ahash_request *req)
413 return mcryptd_hash_enqueue(req, mcryptd_hash_final);
416 static void mcryptd_hash_finup(struct crypto_async_request *req_async, int err)
418 struct ahash_request *req = ahash_request_cast(req_async);
419 struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
421 if (unlikely(err == -EINPROGRESS))
423 rctx->out = req->result;
424 err = ahash_mcryptd_finup(&rctx->areq);
427 req->base.complete = rctx->complete;
434 rctx->complete(&req->base, err);
438 static int mcryptd_hash_finup_enqueue(struct ahash_request *req)
440 return mcryptd_hash_enqueue(req, mcryptd_hash_finup);
443 static void mcryptd_hash_digest(struct crypto_async_request *req_async, int err)
445 struct mcryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
446 struct crypto_ahash *child = ctx->child;
447 struct ahash_request *req = ahash_request_cast(req_async);
448 struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
449 struct ahash_request *desc = &rctx->areq;
451 if (unlikely(err == -EINPROGRESS))
454 ahash_request_set_tfm(desc, child);
455 ahash_request_set_callback(desc, CRYPTO_TFM_REQ_MAY_SLEEP,
456 rctx->complete, req_async);
458 rctx->out = req->result;
459 err = ahash_mcryptd_digest(desc);
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_ahash_export(&rctx->areq, 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_ahash_import(&rctx->areq, 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 hash_alg_common *halg;
492 struct crypto_alg *alg;
497 if (!mcryptd_check_internal(tb, &type, &mask))
500 halg = ahash_attr_alg(tb[1], type, mask);
502 return PTR_ERR(halg);
505 pr_debug("crypto: mcryptd hash alg: %s\n", alg->cra_name);
506 inst = mcryptd_alloc_instance(alg, ahash_instance_headroom(),
512 ctx = ahash_instance_ctx(inst);
515 err = crypto_init_ahash_spawn(&ctx->spawn, halg,
516 ahash_crypto_instance(inst));
520 inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
521 (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
522 CRYPTO_ALG_OPTIONAL_KEY));
524 inst->alg.halg.digestsize = halg->digestsize;
525 inst->alg.halg.statesize = halg->statesize;
526 inst->alg.halg.base.cra_ctxsize = sizeof(struct mcryptd_hash_ctx);
528 inst->alg.halg.base.cra_init = mcryptd_hash_init_tfm;
529 inst->alg.halg.base.cra_exit = mcryptd_hash_exit_tfm;
531 inst->alg.init = mcryptd_hash_init_enqueue;
532 inst->alg.update = mcryptd_hash_update_enqueue;
533 inst->alg.final = mcryptd_hash_final_enqueue;
534 inst->alg.finup = mcryptd_hash_finup_enqueue;
535 inst->alg.export = mcryptd_hash_export;
536 inst->alg.import = mcryptd_hash_import;
537 if (crypto_hash_alg_has_setkey(halg))
538 inst->alg.setkey = mcryptd_hash_setkey;
539 inst->alg.digest = mcryptd_hash_digest_enqueue;
541 err = ahash_register_instance(tmpl, inst);
543 crypto_drop_ahash(&ctx->spawn);
553 static struct mcryptd_queue mqueue;
555 static int mcryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
557 struct crypto_attr_type *algt;
559 algt = crypto_get_attr_type(tb);
561 return PTR_ERR(algt);
563 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
564 case CRYPTO_ALG_TYPE_DIGEST:
565 return mcryptd_create_hash(tmpl, tb, &mqueue);
572 static void mcryptd_free(struct crypto_instance *inst)
574 struct mcryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
575 struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
577 switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
578 case CRYPTO_ALG_TYPE_AHASH:
579 crypto_drop_ahash(&hctx->spawn);
580 kfree(ahash_instance(inst));
583 crypto_drop_spawn(&ctx->spawn);
588 static struct crypto_template mcryptd_tmpl = {
590 .create = mcryptd_create,
591 .free = mcryptd_free,
592 .module = THIS_MODULE,
595 struct mcryptd_ahash *mcryptd_alloc_ahash(const char *alg_name,
598 char mcryptd_alg_name[CRYPTO_MAX_ALG_NAME];
599 struct crypto_ahash *tfm;
601 if (snprintf(mcryptd_alg_name, CRYPTO_MAX_ALG_NAME,
602 "mcryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
603 return ERR_PTR(-EINVAL);
604 tfm = crypto_alloc_ahash(mcryptd_alg_name, type, mask);
606 return ERR_CAST(tfm);
607 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
608 crypto_free_ahash(tfm);
609 return ERR_PTR(-EINVAL);
612 return __mcryptd_ahash_cast(tfm);
614 EXPORT_SYMBOL_GPL(mcryptd_alloc_ahash);
616 int ahash_mcryptd_digest(struct ahash_request *desc)
618 return crypto_ahash_init(desc) ?: ahash_mcryptd_finup(desc);
621 int ahash_mcryptd_update(struct ahash_request *desc)
623 /* alignment is to be done by multi-buffer crypto algorithm if needed */
625 return crypto_ahash_update(desc);
628 int ahash_mcryptd_finup(struct ahash_request *desc)
630 /* alignment is to be done by multi-buffer crypto algorithm if needed */
632 return crypto_ahash_finup(desc);
635 int ahash_mcryptd_final(struct ahash_request *desc)
637 /* alignment is to be done by multi-buffer crypto algorithm if needed */
639 return crypto_ahash_final(desc);
642 struct crypto_ahash *mcryptd_ahash_child(struct mcryptd_ahash *tfm)
644 struct mcryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
648 EXPORT_SYMBOL_GPL(mcryptd_ahash_child);
650 struct ahash_request *mcryptd_ahash_desc(struct ahash_request *req)
652 struct mcryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
655 EXPORT_SYMBOL_GPL(mcryptd_ahash_desc);
657 void mcryptd_free_ahash(struct mcryptd_ahash *tfm)
659 crypto_free_ahash(&tfm->base);
661 EXPORT_SYMBOL_GPL(mcryptd_free_ahash);
663 static int __init mcryptd_init(void)
666 struct mcryptd_flush_list *flist;
668 mcryptd_flist = alloc_percpu(struct mcryptd_flush_list);
669 for_each_possible_cpu(cpu) {
670 flist = per_cpu_ptr(mcryptd_flist, cpu);
671 INIT_LIST_HEAD(&flist->list);
672 mutex_init(&flist->lock);
675 err = mcryptd_init_queue(&mqueue, MCRYPTD_MAX_CPU_QLEN);
677 free_percpu(mcryptd_flist);
681 err = crypto_register_template(&mcryptd_tmpl);
683 mcryptd_fini_queue(&mqueue);
684 free_percpu(mcryptd_flist);
690 static void __exit mcryptd_exit(void)
692 mcryptd_fini_queue(&mqueue);
693 crypto_unregister_template(&mcryptd_tmpl);
694 free_percpu(mcryptd_flist);
697 subsys_initcall(mcryptd_init);
698 module_exit(mcryptd_exit);
700 MODULE_LICENSE("GPL");
701 MODULE_DESCRIPTION("Software async multibuffer crypto daemon");
702 MODULE_ALIAS_CRYPTO("mcryptd");