Skip to content

Commit ada69a1

Browse files
Gilad Ben-Yossefherbertx
authored andcommitted
crypto: introduce crypto wait for async op
Invoking a possibly async. crypto op and waiting for completion while correctly handling backlog processing is a common task in the crypto API implementation and outside users of it. This patch adds a generic implementation for doing so in preparation for using it across the board instead of hand rolled versions. Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com> CC: Eric Biggers <ebiggers3@gmail.com> CC: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3d549e3 commit ada69a1

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎crypto/api.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/sched/signal.h>
2525
#include <linux/slab.h>
2626
#include <linux/string.h>
27+
#include <linux/completion.h>
2728
#include "internal.h"
2829

2930
LIST_HEAD(crypto_alg_list);
@@ -595,5 +596,17 @@ int crypto_has_alg(const char *name, u32 type, u32 mask)
595596
}
596597
EXPORT_SYMBOL_GPL(crypto_has_alg);
597598

599+
void crypto_req_done(struct crypto_async_request *req, int err)
600+
{
601+
struct crypto_wait *wait = req->data;
602+
603+
if (err == -EINPROGRESS)
604+
return;
605+
606+
wait->err = err;
607+
complete(&wait->completion);
608+
}
609+
EXPORT_SYMBOL_GPL(crypto_req_done);
610+
598611
MODULE_DESCRIPTION("Cryptographic core API");
599612
MODULE_LICENSE("GPL");

‎include/linux/crypto.h‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/slab.h>
2525
#include <linux/string.h>
2626
#include <linux/uaccess.h>
27+
#include <linux/completion.h>
2728

2829
/*
2930
* Autoloaded crypto modules should only use a prefixed name to avoid allowing
@@ -467,6 +468,45 @@ struct crypto_alg {
467468
struct module *cra_module;
468469
} CRYPTO_MINALIGN_ATTR;
469470

471+
/*
472+
* A helper struct for waiting for completion of async crypto ops
473+
*/
474+
struct crypto_wait {
475+
struct completion completion;
476+
int err;
477+
};
478+
479+
/*
480+
* Macro for declaring a crypto op async wait object on stack
481+
*/
482+
#define DECLARE_CRYPTO_WAIT(_wait) \
483+
struct crypto_wait _wait = { \
484+
COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
485+
486+
/*
487+
* Async ops completion helper functioons
488+
*/
489+
void crypto_req_done(struct crypto_async_request *req, int err);
490+
491+
static inline int crypto_wait_req(int err, struct crypto_wait *wait)
492+
{
493+
switch (err) {
494+
case -EINPROGRESS:
495+
case -EBUSY:
496+
wait_for_completion(&wait->completion);
497+
reinit_completion(&wait->completion);
498+
err = wait->err;
499+
break;
500+
};
501+
502+
return err;
503+
}
504+
505+
static inline void crypto_init_wait(struct crypto_wait *wait)
506+
{
507+
init_completion(&wait->completion);
508+
}
509+
470510
/*
471511
* Algorithm registration interface.
472512
*/

0 commit comments

Comments
 (0)