Commit 2f1f34c1 authored by Eric Biggers's avatar Eric Biggers Committed by Herbert Xu

crypto: ahash - optimize performance when wrapping shash

The "ahash" API provides access to both CPU-based and hardware offload-
based implementations of hash algorithms.  Typically the former are
implemented as "shash" algorithms under the hood, while the latter are
implemented as "ahash" algorithms.  The "ahash" API provides access to
both.  Various kernel subsystems use the ahash API because they want to
support hashing hardware offload without using a separate API for it.

Yet, the common case is that a crypto accelerator is not actually being
used, and ahash is just wrapping a CPU-based shash algorithm.

This patch optimizes the ahash API for that common case by eliminating
the extra indirect call for each ahash operation on top of shash.

It also fixes the double-counting of crypto stats in this scenario
(though CONFIG_CRYPTO_STATS should *not* be enabled by anyone interested
in performance anyway...), and it eliminates redundant checking of
CRYPTO_TFM_NEED_KEY.  As a bonus, it also shrinks struct crypto_ahash.
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 85b84327
This diff is collapsed.
...@@ -12,6 +12,16 @@ ...@@ -12,6 +12,16 @@
#include "internal.h" #include "internal.h"
static inline struct crypto_istat_hash *hash_get_stat(
struct hash_alg_common *alg)
{
#ifdef CONFIG_CRYPTO_STATS
return &alg->stat;
#else
return NULL;
#endif
}
static inline int crypto_hash_report_stat(struct sk_buff *skb, static inline int crypto_hash_report_stat(struct sk_buff *skb,
struct crypto_alg *alg, struct crypto_alg *alg,
const char *type) const char *type)
......
...@@ -23,7 +23,13 @@ static inline struct crypto_istat_hash *shash_get_stat(struct shash_alg *alg) ...@@ -23,7 +23,13 @@ static inline struct crypto_istat_hash *shash_get_stat(struct shash_alg *alg)
static inline int crypto_shash_errstat(struct shash_alg *alg, int err) static inline int crypto_shash_errstat(struct shash_alg *alg, int err)
{ {
return crypto_hash_errstat(&alg->halg, err); if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
return err;
if (err && err != -EINPROGRESS && err != -EBUSY)
atomic64_inc(&shash_get_stat(alg)->err_cnt);
return err;
} }
int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
......
...@@ -250,16 +250,7 @@ struct shash_alg { ...@@ -250,16 +250,7 @@ struct shash_alg {
#undef HASH_ALG_COMMON_STAT #undef HASH_ALG_COMMON_STAT
struct crypto_ahash { struct crypto_ahash {
int (*init)(struct ahash_request *req); bool using_shash; /* Underlying algorithm is shash, not ahash */
int (*update)(struct ahash_request *req);
int (*final)(struct ahash_request *req);
int (*finup)(struct ahash_request *req);
int (*digest)(struct ahash_request *req);
int (*export)(struct ahash_request *req, void *out);
int (*import)(struct ahash_request *req, const void *in);
int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
unsigned int keylen);
unsigned int statesize; unsigned int statesize;
unsigned int reqsize; unsigned int reqsize;
struct crypto_tfm base; struct crypto_tfm base;
...@@ -513,10 +504,7 @@ int crypto_ahash_digest(struct ahash_request *req); ...@@ -513,10 +504,7 @@ int crypto_ahash_digest(struct ahash_request *req);
* *
* Return: 0 if the export was successful; < 0 if an error occurred * Return: 0 if the export was successful; < 0 if an error occurred
*/ */
static inline int crypto_ahash_export(struct ahash_request *req, void *out) int crypto_ahash_export(struct ahash_request *req, void *out);
{
return crypto_ahash_reqtfm(req)->export(req, out);
}
/** /**
* crypto_ahash_import() - import message digest state * crypto_ahash_import() - import message digest state
...@@ -529,15 +517,7 @@ static inline int crypto_ahash_export(struct ahash_request *req, void *out) ...@@ -529,15 +517,7 @@ static inline int crypto_ahash_export(struct ahash_request *req, void *out)
* *
* Return: 0 if the import was successful; < 0 if an error occurred * Return: 0 if the import was successful; < 0 if an error occurred
*/ */
static inline int crypto_ahash_import(struct ahash_request *req, const void *in) int crypto_ahash_import(struct ahash_request *req, const void *in);
{
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
return -ENOKEY;
return tfm->import(req, in);
}
/** /**
* crypto_ahash_init() - (re)initialize message digest handle * crypto_ahash_init() - (re)initialize message digest handle
...@@ -550,36 +530,7 @@ static inline int crypto_ahash_import(struct ahash_request *req, const void *in) ...@@ -550,36 +530,7 @@ static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
* *
* Return: see crypto_ahash_final() * Return: see crypto_ahash_final()
*/ */
static inline int crypto_ahash_init(struct ahash_request *req) int crypto_ahash_init(struct ahash_request *req);
{
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
return -ENOKEY;
return tfm->init(req);
}
static inline struct crypto_istat_hash *hash_get_stat(
struct hash_alg_common *alg)
{
#ifdef CONFIG_CRYPTO_STATS
return &alg->stat;
#else
return NULL;
#endif
}
static inline int crypto_hash_errstat(struct hash_alg_common *alg, int err)
{
if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
return err;
if (err && err != -EINPROGRESS && err != -EBUSY)
atomic64_inc(&hash_get_stat(alg)->err_cnt);
return err;
}
/** /**
* crypto_ahash_update() - add data to message digest for processing * crypto_ahash_update() - add data to message digest for processing
...@@ -592,16 +543,7 @@ static inline int crypto_hash_errstat(struct hash_alg_common *alg, int err) ...@@ -592,16 +543,7 @@ static inline int crypto_hash_errstat(struct hash_alg_common *alg, int err)
* *
* Return: see crypto_ahash_final() * Return: see crypto_ahash_final()
*/ */
static inline int crypto_ahash_update(struct ahash_request *req) int crypto_ahash_update(struct ahash_request *req);
{
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
if (IS_ENABLED(CONFIG_CRYPTO_STATS))
atomic64_add(req->nbytes, &hash_get_stat(alg)->hash_tlen);
return crypto_hash_errstat(alg, tfm->update(req));
}
/** /**
* DOC: Asynchronous Hash Request Handle * DOC: Asynchronous Hash Request Handle
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment