2024-02-12 14:00:39

by Alexey Romanov

[permalink] [raw]
Subject: [PATCH v4 10/20] drivers: crypto: meson: avoid kzalloc in engine thread

It makes no sense to allocate memory via kzalloc, we
can use static buffer, speedup data processing and
don't think about kfree() calls.

Signed-off-by: Alexey Romanov <[email protected]>
---
drivers/crypto/amlogic/amlogic-gxl-cipher.c | 26 ++++++++-------------
drivers/crypto/amlogic/amlogic-gxl.h | 6 ++---
2 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
index c1b3569a614a..3f42b2cc568d 100644
--- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
+++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
@@ -91,7 +91,6 @@ struct cipher_ctx {
struct skcipher_request *areq;
struct scatterlist *src_sg;
struct scatterlist *dst_sg;
- void *bkeyiv;

unsigned int src_offset;
unsigned int dst_offset;
@@ -156,8 +155,7 @@ static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
return;

if (blockmode == DESC_OPMODE_CBC) {
- memcpy(ctx->bkeyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize);
- ctx->keyiv.len = AES_MAX_KEY_SIZE + ivsize;
+ memcpy(op->keyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize);
dma_sync_single_for_device(mc->dev, ctx->keyiv.addr,
ctx->keyiv.len, DMA_TO_DEVICE);
}
@@ -304,6 +302,7 @@ static int meson_cipher(struct skcipher_request *areq)
.dst_sg = areq->dst,
.cryptlen = areq->cryptlen,
};
+ unsigned int ivsize = crypto_skcipher_ivsize(tfm);
int err;

dev_dbg(mc->dev, "%s %s %u %x IV(%u) key=%u ctx.flow=%d\n", __func__,
@@ -319,16 +318,16 @@ static int meson_cipher(struct skcipher_request *areq)
mc->chanlist[rctx->flow].stat_req++;
#endif

- ctx.bkeyiv = kzalloc(48, GFP_KERNEL | GFP_DMA);
- if (!ctx.bkeyiv)
- return -ENOMEM;
-
- memcpy(ctx.bkeyiv, op->key, op->keylen);
ctx.keyiv.len = op->keylen;
if (ctx.keyiv.len == AES_KEYSIZE_192)
ctx.keyiv.len = AES_MAX_KEY_SIZE;

- ctx.keyiv.addr = dma_map_single(mc->dev, ctx.bkeyiv, ctx.keyiv.len,
+ if (algt->blockmode == DESC_OPMODE_CBC) {
+ memcpy(op->keyiv + AES_MAX_KEY_SIZE, areq->iv, ivsize);
+ ctx.keyiv.len = AES_MAX_KEY_SIZE + ivsize;
+ }
+
+ ctx.keyiv.addr = dma_map_single(mc->dev, op->keyiv, ctx.keyiv.len,
DMA_TO_DEVICE);
err = dma_mapping_error(mc->dev, ctx.keyiv.addr);
if (err) {
@@ -366,8 +365,6 @@ static int meson_cipher(struct skcipher_request *areq)
meson_unmap_scatterlist(areq, mc);

theend:
- kfree_sensitive(ctx.bkeyiv);
-
return err;
}

@@ -450,7 +447,6 @@ static void meson_cipher_exit(struct crypto_tfm *tfm)
{
struct meson_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);

- kfree_sensitive(op->key);
crypto_free_skcipher(op->fallback_tfm);
}

@@ -474,11 +470,9 @@ static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
dev_dbg(mc->dev, "ERROR: Invalid keylen %u\n", keylen);
return -EINVAL;
}
- kfree_sensitive(op->key);
+
+ memcpy(op->keyiv, key, keylen);
op->keylen = keylen;
- op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
- if (!op->key)
- return -ENOMEM;

return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
}
diff --git a/drivers/crypto/amlogic/amlogic-gxl.h b/drivers/crypto/amlogic/amlogic-gxl.h
index eb2f8cd72b65..e1453dd2e9f4 100644
--- a/drivers/crypto/amlogic/amlogic-gxl.h
+++ b/drivers/crypto/amlogic/amlogic-gxl.h
@@ -129,15 +129,15 @@ struct meson_cipher_req_ctx {

/*
* struct meson_cipher_tfm_ctx - context for a skcipher TFM
- * @key: pointer to key data
+ * @keyiv: key data
* @keylen: len of the key
* @keymode: The keymode(type and size of key) associated with this TFM
* @mc: pointer to the private data of driver handling this TFM
* @fallback_tfm: pointer to the fallback TFM
*/
struct meson_cipher_tfm_ctx {
- u32 *key;
- u32 keylen;
+ u8 keyiv[AES_MAX_KEY_SIZE + AES_BLOCK_SIZE] ____cacheline_aligned;
+ u32 keylen ____cacheline_aligned;
u32 keymode;
struct meson_dev *mc;
struct crypto_skcipher *fallback_tfm;
--
2.34.1



2024-02-12 17:16:25

by Neil Armstrong

[permalink] [raw]
Subject: Re: [PATCH v4 10/20] drivers: crypto: meson: avoid kzalloc in engine thread

On 12/02/2024 14:50, Alexey Romanov wrote:
> It makes no sense to allocate memory via kzalloc, we
> can use static buffer, speedup data processing and
> don't think about kfree() calls.
>
> Signed-off-by: Alexey Romanov <[email protected]>
> ---
> drivers/crypto/amlogic/amlogic-gxl-cipher.c | 26 ++++++++-------------
> drivers/crypto/amlogic/amlogic-gxl.h | 6 ++---
> 2 files changed, 13 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> index c1b3569a614a..3f42b2cc568d 100644
> --- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> +++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
> @@ -91,7 +91,6 @@ struct cipher_ctx {
> struct skcipher_request *areq;
> struct scatterlist *src_sg;
> struct scatterlist *dst_sg;
> - void *bkeyiv;
>
> unsigned int src_offset;
> unsigned int dst_offset;
> @@ -156,8 +155,7 @@ static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
> return;
>
> if (blockmode == DESC_OPMODE_CBC) {
> - memcpy(ctx->bkeyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize);
> - ctx->keyiv.len = AES_MAX_KEY_SIZE + ivsize;
> + memcpy(op->keyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize);
> dma_sync_single_for_device(mc->dev, ctx->keyiv.addr,
> ctx->keyiv.len, DMA_TO_DEVICE);
> }
> @@ -304,6 +302,7 @@ static int meson_cipher(struct skcipher_request *areq)
> .dst_sg = areq->dst,
> .cryptlen = areq->cryptlen,
> };
> + unsigned int ivsize = crypto_skcipher_ivsize(tfm);
> int err;
>
> dev_dbg(mc->dev, "%s %s %u %x IV(%u) key=%u ctx.flow=%d\n", __func__,
> @@ -319,16 +318,16 @@ static int meson_cipher(struct skcipher_request *areq)
> mc->chanlist[rctx->flow].stat_req++;
> #endif
>
> - ctx.bkeyiv = kzalloc(48, GFP_KERNEL | GFP_DMA);
> - if (!ctx.bkeyiv)
> - return -ENOMEM;
> -
> - memcpy(ctx.bkeyiv, op->key, op->keylen);
> ctx.keyiv.len = op->keylen;
> if (ctx.keyiv.len == AES_KEYSIZE_192)
> ctx.keyiv.len = AES_MAX_KEY_SIZE;
>
> - ctx.keyiv.addr = dma_map_single(mc->dev, ctx.bkeyiv, ctx.keyiv.len,
> + if (algt->blockmode == DESC_OPMODE_CBC) {
> + memcpy(op->keyiv + AES_MAX_KEY_SIZE, areq->iv, ivsize);
> + ctx.keyiv.len = AES_MAX_KEY_SIZE + ivsize;
> + }
> +
> + ctx.keyiv.addr = dma_map_single(mc->dev, op->keyiv, ctx.keyiv.len,
> DMA_TO_DEVICE);
> err = dma_mapping_error(mc->dev, ctx.keyiv.addr);
> if (err) {
> @@ -366,8 +365,6 @@ static int meson_cipher(struct skcipher_request *areq)
> meson_unmap_scatterlist(areq, mc);
>
> theend:
> - kfree_sensitive(ctx.bkeyiv);
> -
> return err;
> }
>
> @@ -450,7 +447,6 @@ static void meson_cipher_exit(struct crypto_tfm *tfm)
> {
> struct meson_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm);
>
> - kfree_sensitive(op->key);
> crypto_free_skcipher(op->fallback_tfm);
> }
>
> @@ -474,11 +470,9 @@ static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
> dev_dbg(mc->dev, "ERROR: Invalid keylen %u\n", keylen);
> return -EINVAL;
> }
> - kfree_sensitive(op->key);
> +
> + memcpy(op->keyiv, key, keylen);
> op->keylen = keylen;
> - op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
> - if (!op->key)
> - return -ENOMEM;
>
> return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
> }
> diff --git a/drivers/crypto/amlogic/amlogic-gxl.h b/drivers/crypto/amlogic/amlogic-gxl.h
> index eb2f8cd72b65..e1453dd2e9f4 100644
> --- a/drivers/crypto/amlogic/amlogic-gxl.h
> +++ b/drivers/crypto/amlogic/amlogic-gxl.h
> @@ -129,15 +129,15 @@ struct meson_cipher_req_ctx {
>
> /*
> * struct meson_cipher_tfm_ctx - context for a skcipher TFM
> - * @key: pointer to key data
> + * @keyiv: key data
> * @keylen: len of the key
> * @keymode: The keymode(type and size of key) associated with this TFM
> * @mc: pointer to the private data of driver handling this TFM
> * @fallback_tfm: pointer to the fallback TFM
> */
> struct meson_cipher_tfm_ctx {
> - u32 *key;
> - u32 keylen;
> + u8 keyiv[AES_MAX_KEY_SIZE + AES_BLOCK_SIZE] ____cacheline_aligned;
> + u32 keylen ____cacheline_aligned;
> u32 keymode;
> struct meson_dev *mc;
> struct crypto_skcipher *fallback_tfm;

Reviewed-by: Neil Armstrong <[email protected]>