Replace a pointer to a small completion structure with the structure
itself to avoid unnecessary dynamic allocations on hotpath. The embedding
structure is itself dynamically allocated and we get a marginal increase
in performance from elimination of unnecessary code.

Signed-off-by: Cristian Stoica <cristian.sto...@nxp.com>
---
 cryptlib.c | 47 +++++++++++++----------------------------------
 cryptlib.h |  9 +++++++--
 2 files changed, 20 insertions(+), 36 deletions(-)

diff --git a/cryptlib.c b/cryptlib.c
index e5c4530..aa03610 100644
--- a/cryptlib.c
+++ b/cryptlib.c
@@ -39,11 +39,6 @@
 #include "cryptodev_int.h"
 
 
-struct cryptodev_result {
-       struct completion completion;
-       int err;
-};
-
 static void cryptodev_complete(struct crypto_async_request *req, int err)
 {
        struct cryptodev_result *res = req->data;
@@ -182,13 +177,7 @@ int cryptodev_cipher_init(struct cipher_data *out, const 
char *alg_name,
        out->stream = stream;
        out->aead = aead;
 
-       out->async.result = kzalloc(sizeof(*out->async.result), GFP_KERNEL);
-       if (unlikely(!out->async.result)) {
-               ret = -ENOMEM;
-               goto error;
-       }
-
-       init_completion(&out->async.result->completion);
+       init_completion(&out->async.result.completion);
 
        if (aead == 0) {
                out->async.request = ablkcipher_request_alloc(out->async.s, 
GFP_KERNEL);
@@ -200,7 +189,7 @@ int cryptodev_cipher_init(struct cipher_data *out, const 
char *alg_name,
 
                ablkcipher_request_set_callback(out->async.request,
                                        CRYPTO_TFM_REQ_MAY_BACKLOG,
-                                       cryptodev_complete, out->async.result);
+                                       cryptodev_complete, &out->async.result);
        } else {
                out->async.arequest = aead_request_alloc(out->async.as, 
GFP_KERNEL);
                if (unlikely(!out->async.arequest)) {
@@ -211,7 +200,7 @@ int cryptodev_cipher_init(struct cipher_data *out, const 
char *alg_name,
 
                aead_request_set_callback(out->async.arequest,
                                        CRYPTO_TFM_REQ_MAY_BACKLOG,
-                                       cryptodev_complete, out->async.result);
+                                       cryptodev_complete, &out->async.result);
        }
 
        out->init = 1;
@@ -228,7 +217,6 @@ error:
                if (out->async.as)
                        crypto_free_aead(out->async.as);
        }
-       kfree(out->async.result);
 
        return ret;
 }
@@ -248,7 +236,6 @@ void cryptodev_cipher_deinit(struct cipher_data *cdata)
                                crypto_free_aead(cdata->async.as);
                }
 
-               kfree(cdata->async.result);
                cdata->init = 0;
        }
 }
@@ -286,7 +273,7 @@ ssize_t cryptodev_cipher_encrypt(struct cipher_data *cdata,
 {
        int ret;
 
-       reinit_completion(&cdata->async.result->completion);
+       reinit_completion(&cdata->async.result.completion);
 
        if (cdata->aead == 0) {
                ablkcipher_request_set_crypt(cdata->async.request,
@@ -300,7 +287,7 @@ ssize_t cryptodev_cipher_encrypt(struct cipher_data *cdata,
                ret = crypto_aead_encrypt(cdata->async.arequest);
        }
 
-       return waitfor(cdata->async.result, ret);
+       return waitfor(&cdata->async.result, ret);
 }
 
 ssize_t cryptodev_cipher_decrypt(struct cipher_data *cdata,
@@ -309,7 +296,7 @@ ssize_t cryptodev_cipher_decrypt(struct cipher_data *cdata,
 {
        int ret;
 
-       reinit_completion(&cdata->async.result->completion);
+       reinit_completion(&cdata->async.result.completion);
        if (cdata->aead == 0) {
                ablkcipher_request_set_crypt(cdata->async.request,
                        (struct scatterlist *)src, dst,
@@ -322,7 +309,7 @@ ssize_t cryptodev_cipher_decrypt(struct cipher_data *cdata,
                ret = crypto_aead_decrypt(cdata->async.arequest);
        }
 
-       return waitfor(cdata->async.result, ret);
+       return waitfor(&cdata->async.result, ret);
 }
 
 /* Hash functions */
@@ -352,13 +339,7 @@ int cryptodev_hash_init(struct hash_data *hdata, const 
char *alg_name,
        hdata->digestsize = crypto_ahash_digestsize(hdata->async.s);
        hdata->alignmask = crypto_ahash_alignmask(hdata->async.s);
 
-       hdata->async.result = kzalloc(sizeof(*hdata->async.result), GFP_KERNEL);
-       if (unlikely(!hdata->async.result)) {
-               ret = -ENOMEM;
-               goto error;
-       }
-
-       init_completion(&hdata->async.result->completion);
+       init_completion(&hdata->async.result.completion);
 
        hdata->async.request = ahash_request_alloc(hdata->async.s, GFP_KERNEL);
        if (unlikely(!hdata->async.request)) {
@@ -369,12 +350,11 @@ int cryptodev_hash_init(struct hash_data *hdata, const 
char *alg_name,
 
        ahash_request_set_callback(hdata->async.request,
                        CRYPTO_TFM_REQ_MAY_BACKLOG,
-                       cryptodev_complete, hdata->async.result);
+                       cryptodev_complete, &hdata->async.result);
        hdata->init = 1;
        return 0;
 
 error:
-       kfree(hdata->async.result);
        crypto_free_ahash(hdata->async.s);
        return ret;
 }
@@ -384,7 +364,6 @@ void cryptodev_hash_deinit(struct hash_data *hdata)
        if (hdata->init) {
                if (hdata->async.request)
                        ahash_request_free(hdata->async.request);
-               kfree(hdata->async.result);
                if (hdata->async.s)
                        crypto_free_ahash(hdata->async.s);
                hdata->init = 0;
@@ -410,23 +389,23 @@ ssize_t cryptodev_hash_update(struct hash_data *hdata,
 {
        int ret;
 
-       reinit_completion(&hdata->async.result->completion);
+       reinit_completion(&hdata->async.result.completion);
        ahash_request_set_crypt(hdata->async.request, sg, NULL, len);
 
        ret = crypto_ahash_update(hdata->async.request);
 
-       return waitfor(hdata->async.result, ret);
+       return waitfor(&hdata->async.result, ret);
 }
 
 int cryptodev_hash_final(struct hash_data *hdata, void *output)
 {
        int ret;
 
-       reinit_completion(&hdata->async.result->completion);
+       reinit_completion(&hdata->async.result.completion);
        ahash_request_set_crypt(hdata->async.request, NULL, output, 0);
 
        ret = crypto_ahash_final(hdata->async.request);
 
-       return waitfor(hdata->async.result, ret);
+       return waitfor(&hdata->async.result, ret);
 }
 
diff --git a/cryptlib.h b/cryptlib.h
index f0c2736..6a6e18f 100644
--- a/cryptlib.h
+++ b/cryptlib.h
@@ -3,6 +3,11 @@
 
 #include <linux/version.h>
 
+struct cryptodev_result {
+       struct completion completion;
+       int err;
+};
+
 struct cipher_data {
        int init; /* 0 uninitialized */
        int blocksize;
@@ -19,7 +24,7 @@ struct cipher_data {
                struct crypto_aead *as;
                struct aead_request *arequest;
 
-               struct cryptodev_result *result;
+               struct cryptodev_result result;
                uint8_t iv[EALG_MAX_BLOCK_LEN];
        } async;
 };
@@ -82,7 +87,7 @@ struct hash_data {
        int alignmask;
        struct {
                struct crypto_ahash *s;
-               struct cryptodev_result *result;
+               struct cryptodev_result result;
                struct ahash_request *request;
        } async;
 };
-- 
2.7.0


_______________________________________________
Cryptodev-linux-devel mailing list
Cryptodev-linux-devel@gna.org
https://mail.gna.org/listinfo/cryptodev-linux-devel

Reply via email to