crypto: sun8i-ss - do not allocate memory when handling hash requests
authorCorentin Labbe <clabbe@baylibre.com>
Mon, 2 May 2022 20:19:19 +0000 (20:19 +0000)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 13 May 2022 09:24:18 +0000 (17:24 +0800)
Instead of allocate memory on each requests, it is easier to
pre-allocate buffers.
This made error path easier.

Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c
drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h

index 6575305..786b6f5 100644 (file)
@@ -486,6 +486,16 @@ static int allocate_flows(struct sun8i_ss_dev *ss)
                                goto error_engine;
                }
 
+               /* the padding could be up to two block. */
+               ss->flows[i].pad = devm_kmalloc(ss->dev, SHA256_BLOCK_SIZE * 2,
+                                               GFP_KERNEL | GFP_DMA);
+               if (!ss->flows[i].pad)
+                       goto error_engine;
+               ss->flows[i].result = devm_kmalloc(ss->dev, SHA256_DIGEST_SIZE,
+                                                  GFP_KERNEL | GFP_DMA);
+               if (!ss->flows[i].result)
+                       goto error_engine;
+
                ss->flows[i].engine = crypto_engine_alloc_init(ss->dev, true);
                if (!ss->flows[i].engine) {
                        dev_err(ss->dev, "Cannot allocate engine\n");
index 49e2e94..9582ac4 100644 (file)
@@ -332,18 +332,11 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
        if (digestsize == SHA224_DIGEST_SIZE)
                digestsize = SHA256_DIGEST_SIZE;
 
-       /* the padding could be up to two block. */
-       pad = kzalloc(algt->alg.hash.halg.base.cra_blocksize * 2, GFP_KERNEL | GFP_DMA);
-       if (!pad)
-               return -ENOMEM;
+       result = ss->flows[rctx->flow].result;
+       pad = ss->flows[rctx->flow].pad;
+       memset(pad, 0, algt->alg.hash.halg.base.cra_blocksize * 2);
        bf = (__le32 *)pad;
 
-       result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA);
-       if (!result) {
-               kfree(pad);
-               return -ENOMEM;
-       }
-
        for (i = 0; i < MAX_SG; i++) {
                rctx->t_dst[i].addr = 0;
                rctx->t_dst[i].len = 0;
@@ -439,8 +432,6 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 
        memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
 theend:
-       kfree(pad);
-       kfree(result);
        local_bh_disable();
        crypto_finalize_hash_request(engine, breq, err);
        local_bh_enable();
index 57ada86..eb82ee5 100644 (file)
@@ -123,6 +123,8 @@ struct sginfo {
  * @stat_req:  number of request done by this flow
  * @iv:                list of IV to use for each step
  * @biv:       buffer which contain the backuped IV
+ * @pad:       padding buffer for hash operations
+ * @result:    buffer for storing the result of hash operations
  */
 struct sun8i_ss_flow {
        struct crypto_engine *engine;
@@ -130,6 +132,8 @@ struct sun8i_ss_flow {
        int status;
        u8 *iv[MAX_SG];
        u8 *biv;
+       void *pad;
+       void *result;
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
        unsigned long stat_req;
 #endif