linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/8] crypto: allwinner: various improvments
@ 2022-01-26 21:04 Corentin Labbe
  2022-01-26 21:04 ` [PATCH 1/8] crypto: sun8i-ce: do not allocate memory when handling requests Corentin Labbe
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

Hello

The main topic was to remove memory allocation from requests function.
Doing this on sun8i-ss, lead to some extra fixes to be found.

Regards

Corentin Labbe (8):
  crypto: sun8i-ce: do not allocate memory when handling requests
  crypto: sun4i-ss: do not allocate backup IV on requests
  crypto: sun8i-ss: handle zero sized sg
  crypto: sun8i-ss: do not allocate memory when handling hash requests
  crypto: sun8i-ss: do not zeroize all pad
  crypto: sun8i-ss: remove redundant test
  crypto: sun8i-ss: test error before assigning
  crypto: sun8i-ss: handle requests if last block is not modulo 64

 .../allwinner/sun4i-ss/sun4i-ss-cipher.c      | 22 ++---
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h  |  1 +
 .../allwinner/sun8i-ce/sun8i-ce-cipher.c      | 28 ++-----
 .../crypto/allwinner/sun8i-ce/sun8i-ce-core.c | 20 ++++-
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  |  8 +-
 .../crypto/allwinner/sun8i-ss/sun8i-ss-core.c | 10 +++
 .../crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 84 ++++++++++---------
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h  |  6 ++
 8 files changed, 100 insertions(+), 79 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/8] crypto: sun8i-ce: do not allocate memory when handling requests
  2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
@ 2022-01-26 21:04 ` Corentin Labbe
  2022-01-27  4:17   ` kernel test robot
  2022-01-26 21:04 ` [PATCH 2/8] crypto: sun4i-ss: do not allocate backup IV on requests Corentin Labbe
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

Instead of allocate memory on each requests, it is easier to
pre-allocate buffer for IV.
This made error path easier.

Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 .../allwinner/sun8i-ce/sun8i-ce-cipher.c      | 28 ++++++-------------
 .../crypto/allwinner/sun8i-ce/sun8i-ce-core.c | 20 ++++++++++---
 drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h  |  8 +++---
 3 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c
index 8f0031e45a03..f4221025d651 100644
--- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c
+++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c
@@ -151,23 +151,13 @@ static int sun8i_ce_cipher_prepare(struct crypto_engine *engine, void *async_req
 	ivsize = crypto_skcipher_ivsize(tfm);
 	if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
 		rctx->ivlen = ivsize;
-		rctx->bounce_iv = kzalloc(ivsize, GFP_KERNEL | GFP_DMA);
-		if (!rctx->bounce_iv) {
-			err = -ENOMEM;
-			goto theend_key;
-		}
 		if (rctx->op_dir & CE_DECRYPTION) {
-			rctx->backup_iv = kzalloc(ivsize, GFP_KERNEL);
-			if (!rctx->backup_iv) {
-				err = -ENOMEM;
-				goto theend_key;
-			}
 			offset = areq->cryptlen - ivsize;
-			scatterwalk_map_and_copy(rctx->backup_iv, areq->src,
+			scatterwalk_map_and_copy(chan->backup_iv, areq->src,
 						 offset, ivsize, 0);
 		}
-		memcpy(rctx->bounce_iv, areq->iv, ivsize);
-		rctx->addr_iv = dma_map_single(ce->dev, rctx->bounce_iv, rctx->ivlen,
+		memcpy(chan->bounce_iv, areq->iv, ivsize);
+		rctx->addr_iv = dma_map_single(ce->dev, chan->bounce_iv, rctx->ivlen,
 					       DMA_TO_DEVICE);
 		if (dma_mapping_error(ce->dev, rctx->addr_iv)) {
 			dev_err(ce->dev, "Cannot DMA MAP IV\n");
@@ -256,13 +246,13 @@ static int sun8i_ce_cipher_prepare(struct crypto_engine *engine, void *async_req
 			dma_unmap_single(ce->dev, rctx->addr_iv, rctx->ivlen, DMA_TO_DEVICE);
 		offset = areq->cryptlen - ivsize;
 		if (rctx->op_dir & CE_DECRYPTION) {
-			memcpy(areq->iv, rctx->backup_iv, ivsize);
-			kfree_sensitive(rctx->backup_iv);
+			memcpy(areq->iv, chan->backup_iv, ivsize);
+			memzero_explicit(chan->backup_iv, ivsize);
 		} else {
 			scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
 						 ivsize, 0);
 		}
-		kfree(rctx->bounce_iv);
+		memzero_explicit(chan->bounce_iv, ivsize);
 	}
 
 theend_key:
@@ -319,13 +309,13 @@ static int sun8i_ce_cipher_unprepare(struct crypto_engine *engine, void *async_r
 			dma_unmap_single(ce->dev, rctx->addr_iv, rctx->ivlen, DMA_TO_DEVICE);
 		offset = areq->cryptlen - ivsize;
 		if (rctx->op_dir & CE_DECRYPTION) {
-			memcpy(areq->iv, rctx->backup_iv, ivsize);
-			kfree_sensitive(rctx->backup_iv);
+			memcpy(areq->iv, chan->backup_iv, ivsize);
+			memzero_explicit(chan->backup_iv, ivsize);
 		} else {
 			scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
 						 ivsize, 0);
 		}
-		kfree(rctx->bounce_iv);
+		memzero_explicit(chan->bounce_iv, ivsize);
 	}
 
 	dma_unmap_single(ce->dev, rctx->addr_key, op->keylen, DMA_TO_DEVICE);
diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c
index ea2c655e4ff1..b57aed120d33 100644
--- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c
+++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-core.c
@@ -305,7 +305,7 @@ static struct sun8i_ce_alg_template ce_algs[] = {
 			.cra_priority = 400,
 			.cra_blocksize = AES_BLOCK_SIZE,
 			.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
-				CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY |
+				CRYPTO_ALG_ASYNC |
 				CRYPTO_ALG_NEED_FALLBACK,
 			.cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx),
 			.cra_module = THIS_MODULE,
@@ -332,7 +332,7 @@ static struct sun8i_ce_alg_template ce_algs[] = {
 			.cra_priority = 400,
 			.cra_blocksize = AES_BLOCK_SIZE,
 			.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
-				CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY |
+				CRYPTO_ALG_ASYNC |
 				CRYPTO_ALG_NEED_FALLBACK,
 			.cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx),
 			.cra_module = THIS_MODULE,
@@ -358,7 +358,7 @@ static struct sun8i_ce_alg_template ce_algs[] = {
 			.cra_priority = 400,
 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
 			.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
-				CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY |
+				CRYPTO_ALG_ASYNC |
 				CRYPTO_ALG_NEED_FALLBACK,
 			.cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx),
 			.cra_module = THIS_MODULE,
@@ -385,7 +385,7 @@ static struct sun8i_ce_alg_template ce_algs[] = {
 			.cra_priority = 400,
 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
 			.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
-				CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY |
+				CRYPTO_ALG_ASYNC |
 				CRYPTO_ALG_NEED_FALLBACK,
 			.cra_ctxsize = sizeof(struct sun8i_cipher_tfm_ctx),
 			.cra_module = THIS_MODULE,
@@ -728,6 +728,18 @@ static int sun8i_ce_allocate_chanlist(struct sun8i_ce_dev *ce)
 			err = -ENOMEM;
 			goto error_engine;
 		}
+		ce->chanlist[i].bounce_iv = devm_kmalloc(ce->dev, AES_BLOCK_SIZE,
+							 GFP_KERNEL | GFP_DMA);
+		if (!ce->chanlist[i].bounce_iv) {
+			err = -ENOMEM;
+			goto error_engine;
+		}
+		ce->chanlist[i].backup_iv = devm_kmalloc(ce->dev, AES_BLOCK_SIZE,
+							 GFP_KERNEL);
+		if (!ce->chanlist[i].backup_iv) {
+			err = -ENOMEM;
+			goto error_engine;
+		}
 	}
 	return 0;
 error_engine:
diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h
index 719f9a730857..229b696d5a2c 100644
--- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h
+++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h
@@ -213,6 +213,8 @@ struct ce_task {
  * @status:	set to 1 by interrupt if task is done
  * @t_phy:	Physical address of task
  * @tl:		pointer to the current ce_task for this flow
+ * @backup_iv:		buffer which contain the next IV to store
+ * @bounce_iv:		buffer which contain the IV
  * @stat_req:	number of request done by this flow
  */
 struct sun8i_ce_flow {
@@ -222,6 +224,8 @@ struct sun8i_ce_flow {
 	dma_addr_t t_phy;
 	int timeout;
 	struct ce_task *tl;
+	void *backup_iv;
+	void *bounce_iv;
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
 	unsigned long stat_req;
 #endif
@@ -268,8 +272,6 @@ struct sun8i_ce_dev {
  * struct sun8i_cipher_req_ctx - context for a skcipher request
  * @op_dir:		direction (encrypt vs decrypt) for this request
  * @flow:		the flow to use for this request
- * @backup_iv:		buffer which contain the next IV to store
- * @bounce_iv:		buffer which contain the IV
  * @ivlen:		size of bounce_iv
  * @nr_sgs:		The number of source SG (as given by dma_map_sg())
  * @nr_sgd:		The number of destination SG (as given by dma_map_sg())
@@ -280,8 +282,6 @@ struct sun8i_ce_dev {
 struct sun8i_cipher_req_ctx {
 	u32 op_dir;
 	int flow;
-	void *backup_iv;
-	void *bounce_iv;
 	unsigned int ivlen;
 	int nr_sgs;
 	int nr_sgd;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 2/8] crypto: sun4i-ss: do not allocate backup IV on requests
  2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
  2022-01-26 21:04 ` [PATCH 1/8] crypto: sun8i-ce: do not allocate memory when handling requests Corentin Labbe
@ 2022-01-26 21:04 ` Corentin Labbe
  2022-01-26 21:04 ` [PATCH 3/8] crypto: sun8i-ss: handle zero sized sg Corentin Labbe
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

Instead of allocate memory on each requests, it is easier to
pre-allocate buffer for backup IV.
This made error path easier.
Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 .../allwinner/sun4i-ss/sun4i-ss-cipher.c      | 22 +++++++------------
 drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h  |  1 +
 2 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
index 8dc2a475c601..a8c784acce13 100644
--- a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
+++ b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c
@@ -20,7 +20,6 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
 	struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
 	u32 mode = ctx->mode;
-	void *backup_iv = NULL;
 	/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
 	u32 rx_cnt = SS_RX_DEFAULT;
 	u32 tx_cnt = 0;
@@ -47,10 +46,8 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 	}
 
 	if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
-		backup_iv = kzalloc(ivsize, GFP_KERNEL);
-		if (!backup_iv)
-			return -ENOMEM;
-		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
+		scatterwalk_map_and_copy(ctx->backup_iv, areq->src,
+					 areq->cryptlen - ivsize, ivsize, 0);
 	}
 
 	if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
@@ -133,8 +130,8 @@ static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
 
 	if (areq->iv) {
 		if (mode & SS_DECRYPTION) {
-			memcpy(areq->iv, backup_iv, ivsize);
-			kfree_sensitive(backup_iv);
+			memcpy(areq->iv, ctx->backup_iv, ivsize);
+			memzero_explicit(ctx->backup_iv, ivsize);
 		} else {
 			scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
 						 ivsize, 0);
@@ -198,7 +195,6 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	unsigned int ileft = areq->cryptlen;
 	unsigned int oleft = areq->cryptlen;
 	unsigned int todo;
-	void *backup_iv = NULL;
 	struct sg_mapping_iter mi, mo;
 	unsigned long pi = 0, po = 0; /* progress for in and out */
 	bool miter_err;
@@ -242,10 +238,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 		return sun4i_ss_cipher_poll_fallback(areq);
 
 	if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
-		backup_iv = kzalloc(ivsize, GFP_KERNEL);
-		if (!backup_iv)
-			return -ENOMEM;
-		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
+		scatterwalk_map_and_copy(ctx->backup_iv, areq->src,
+					 areq->cryptlen - ivsize, ivsize, 0);
 	}
 
 	if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
@@ -382,8 +376,8 @@ static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
 	}
 	if (areq->iv) {
 		if (mode & SS_DECRYPTION) {
-			memcpy(areq->iv, backup_iv, ivsize);
-			kfree_sensitive(backup_iv);
+			memcpy(areq->iv, ctx->backup_iv, ivsize);
+			memzero_explicit(ctx->backup_iv, ivsize);
 		} else {
 			scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
 						 ivsize, 0);
diff --git a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h
index 0fee6f4e2d90..ba59c7a48825 100644
--- a/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h
+++ b/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h
@@ -183,6 +183,7 @@ struct sun4i_tfm_ctx {
 
 struct sun4i_cipher_req_ctx {
 	u32 mode;
+	u8 backup_iv[AES_BLOCK_SIZE];
 	struct skcipher_request fallback_req;   // keep at the end
 };
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 3/8] crypto: sun8i-ss: handle zero sized sg
  2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
  2022-01-26 21:04 ` [PATCH 1/8] crypto: sun8i-ce: do not allocate memory when handling requests Corentin Labbe
  2022-01-26 21:04 ` [PATCH 2/8] crypto: sun4i-ss: do not allocate backup IV on requests Corentin Labbe
@ 2022-01-26 21:04 ` Corentin Labbe
  2022-01-26 21:04 ` [PATCH 4/8] crypto: sun8i-ss: do not allocate memory when handling hash requests Corentin Labbe
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

sun8i-ss does not handle well the possible zero sized sg.

Fixes: d9b45418a917 ("crypto: sun8i-ss - support hash algorithms")
Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
index 3c073eb3db03..2557bb3fe7aa 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
@@ -379,13 +379,21 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 	}
 
 	len = areq->nbytes;
-	for_each_sg(areq->src, sg, nr_sgs, i) {
+	sg = areq->src;
+	i = 0;
+	while (len > 0 && sg) {
+		if (sg_dma_len(sg) == 0) {
+			sg = sg_next(sg);
+			continue;
+		}
 		rctx->t_src[i].addr = sg_dma_address(sg);
 		todo = min(len, sg_dma_len(sg));
 		rctx->t_src[i].len = todo / 4;
 		len -= todo;
 		rctx->t_dst[i].addr = addr_res;
 		rctx->t_dst[i].len = digestsize / 4;
+		sg = sg_next(sg);
+		i++;
 	}
 	if (len > 0) {
 		dev_err(ss->dev, "remaining len %d\n", len);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 4/8] crypto: sun8i-ss: do not allocate memory when handling hash requests
  2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
                   ` (2 preceding siblings ...)
  2022-01-26 21:04 ` [PATCH 3/8] crypto: sun8i-ss: handle zero sized sg Corentin Labbe
@ 2022-01-26 21:04 ` Corentin Labbe
  2022-01-26 21:04 ` [PATCH 5/8] crypto: sun8i-ss: do not zeroize all pad Corentin Labbe
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

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>
---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c | 10 ++++++++++
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 15 +++------------
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h      |  4 ++++
 3 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c
index 319fe3279a71..084261d7899c 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c
@@ -474,6 +474,16 @@ static int allocate_flows(struct sun8i_ss_dev *ss)
 	for (i = 0; i < MAXFLOW; i++) {
 		init_completion(&ss->flows[i].complete);
 
+		/* 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");
diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
index 2557bb3fe7aa..f7a9578e87f7 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
@@ -341,18 +341,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;
@@ -448,8 +441,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);
 	crypto_finalize_hash_request(engine, breq, err);
 	return 0;
 }
diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h
index 28188685b910..f9f089ede934 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h
@@ -121,11 +121,15 @@ struct sginfo {
  * @complete:	completion for the current task on this flow
  * @status:	set to 1 by interrupt if task is done
  * @stat_req:	number of request done by this flow
+ * @pad:	padding buffer for hash operations
+ * @result:	buffer for storing the result of hash operations
  */
 struct sun8i_ss_flow {
 	struct crypto_engine *engine;
 	struct completion complete;
 	int status;
+	void *pad;
+	void *result;
 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
 	unsigned long stat_req;
 #endif
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 5/8] crypto: sun8i-ss: do not zeroize all pad
  2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
                   ` (3 preceding siblings ...)
  2022-01-26 21:04 ` [PATCH 4/8] crypto: sun8i-ss: do not allocate memory when handling hash requests Corentin Labbe
@ 2022-01-26 21:04 ` Corentin Labbe
  2022-01-26 21:04 ` [PATCH 6/8] crypto: sun8i-ss: remove redundant test Corentin Labbe
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

Instead of memset all pad buffer, it is faster to only put 0 where
needed.

Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
index f7a9578e87f7..ef3020bc9547 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
@@ -328,7 +328,7 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 	unsigned int len;
 	u64 fill, min_fill, byte_count;
 	void *pad, *result;
-	int j, i, todo;
+	int j, i, k, todo;
 	__be64 *bebits;
 	__le64 *lebits;
 	dma_addr_t addr_res, addr_pad;
@@ -343,7 +343,6 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 
 	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;
 
 	for (i = 0; i < MAX_SG; i++) {
@@ -404,7 +403,10 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 	if (fill < min_fill)
 		fill += 64;
 
+	k = j;
 	j += (fill - min_fill) / sizeof(u32);
+	for (; k < j; k++)
+		bf[k] = 0;
 
 	switch (algt->ss_algo_id) {
 	case SS_ID_HASH_MD5:
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 6/8] crypto: sun8i-ss: remove redundant test
  2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
                   ` (4 preceding siblings ...)
  2022-01-26 21:04 ` [PATCH 5/8] crypto: sun8i-ss: do not zeroize all pad Corentin Labbe
@ 2022-01-26 21:04 ` Corentin Labbe
  2022-01-26 21:04 ` [PATCH 7/8] crypto: sun8i-ss: test error before assigning Corentin Labbe
  2022-01-26 21:04 ` [PATCH 8/8] crypto: sun8i-ss: handle requests if last block is not modulo 64 Corentin Labbe
  7 siblings, 0 replies; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

Some fallback tests were redundant with what sun8i_ss_hash_need_fallback() already do.

Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
index ef3020bc9547..7ebd11d3ff7d 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
@@ -287,21 +287,11 @@ int sun8i_ss_hash_digest(struct ahash_request *areq)
 	struct sun8i_ss_alg_template *algt;
 	struct sun8i_ss_dev *ss;
 	struct crypto_engine *engine;
-	struct scatterlist *sg;
-	int nr_sgs, e, i;
+	int e;
 
 	if (sun8i_ss_hash_need_fallback(areq))
 		return sun8i_ss_hash_digest_fb(areq);
 
-	nr_sgs = sg_nents(areq->src);
-	if (nr_sgs > MAX_SG - 1)
-		return sun8i_ss_hash_digest_fb(areq);
-
-	for_each_sg(areq->src, sg, nr_sgs, i) {
-		if (sg->length % 4 || !IS_ALIGNED(sg->offset, sizeof(u32)))
-			return sun8i_ss_hash_digest_fb(areq);
-	}
-
 	algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
 	ss = algt->ss;
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 7/8] crypto: sun8i-ss: test error before assigning
  2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
                   ` (5 preceding siblings ...)
  2022-01-26 21:04 ` [PATCH 6/8] crypto: sun8i-ss: remove redundant test Corentin Labbe
@ 2022-01-26 21:04 ` Corentin Labbe
  2022-01-26 21:04 ` [PATCH 8/8] crypto: sun8i-ss: handle requests if last block is not modulo 64 Corentin Labbe
  7 siblings, 0 replies; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

The first thing we should do after dma_map_single() is to test the
result.

Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
index 7ebd11d3ff7d..1aae36d541d8 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
@@ -414,15 +414,15 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 	}
 
 	addr_pad = dma_map_single(ss->dev, pad, j * 4, DMA_TO_DEVICE);
-	rctx->t_src[i].addr = addr_pad;
-	rctx->t_src[i].len = j;
-	rctx->t_dst[i].addr = addr_res;
-	rctx->t_dst[i].len = digestsize / 4;
 	if (dma_mapping_error(ss->dev, addr_pad)) {
 		dev_err(ss->dev, "DMA error on padding SG\n");
 		err = -EINVAL;
 		goto theend;
 	}
+	rctx->t_src[i].addr = addr_pad;
+	rctx->t_src[i].len = j;
+	rctx->t_dst[i].addr = addr_res;
+	rctx->t_dst[i].len = digestsize / 4;
 
 	err = sun8i_ss_run_hash_task(ss, rctx, crypto_tfm_alg_name(areq->base.tfm));
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 8/8] crypto: sun8i-ss: handle requests if last block is not modulo 64
  2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
                   ` (6 preceding siblings ...)
  2022-01-26 21:04 ` [PATCH 7/8] crypto: sun8i-ss: test error before assigning Corentin Labbe
@ 2022-01-26 21:04 ` Corentin Labbe
  7 siblings, 0 replies; 10+ messages in thread
From: Corentin Labbe @ 2022-01-26 21:04 UTC (permalink / raw)
  To: davem, herbert, jernej.skrabec, mripard, wens
  Cc: linux-arm-kernel, linux-crypto, linux-kernel, linux-sunxi,
	linux-sunxi, Corentin Labbe

The current sun8i-ss handle only requests with all SG length being
modulo 64.
But the last SG could be always handled by copying it on the pad buffer.

Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 .../crypto/allwinner/sun8i-ss/sun8i-ss-core.c |  2 +-
 .../crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 35 ++++++++++++++-----
 drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h  |  2 ++
 3 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c
index 084261d7899c..c8c079f3b466 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c
@@ -475,7 +475,7 @@ static int allocate_flows(struct sun8i_ss_dev *ss)
 		init_completion(&ss->flows[i].complete);
 
 		/* the padding could be up to two block. */
-		ss->flows[i].pad = devm_kmalloc(ss->dev, SHA256_BLOCK_SIZE * 2,
+		ss->flows[i].pad = devm_kmalloc(ss->dev, MAX_PAD_SIZE,
 						GFP_KERNEL | GFP_DMA);
 		if (!ss->flows[i].pad)
 			goto error_engine;
diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
index 1aae36d541d8..dd5e4f0fd5ab 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
@@ -13,6 +13,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/scatterlist.h>
 #include <crypto/internal/hash.h>
+#include <crypto/scatterwalk.h>
 #include <crypto/sha1.h>
 #include <crypto/sha2.h>
 #include <crypto/md5.h>
@@ -261,6 +262,9 @@ static bool sun8i_ss_hash_need_fallback(struct ahash_request *areq)
 
 	if (areq->nbytes == 0)
 		return true;
+	if (areq->nbytes >= MAX_PAD_SIZE - 64)
+		return true;
+
 	/* we need to reserve one SG for the padding one */
 	if (sg_nents(areq->src) > MAX_SG - 1)
 		return true;
@@ -269,10 +273,13 @@ static bool sun8i_ss_hash_need_fallback(struct ahash_request *areq)
 		/* SS can operate hash only on full block size
 		 * since SS support only MD5,sha1,sha224 and sha256, blocksize
 		 * is always 64
-		 * TODO: handle request if last SG is not len%64
-		 * but this will need to copy data on a new SG of size=64
 		 */
-		if (sg->length % 64 || !IS_ALIGNED(sg->offset, sizeof(u32)))
+		/* Only the last block could be bounced to the pad buffer */
+		if (sg->length % 64 && sg_next(sg))
+			return true;
+		if (!IS_ALIGNED(sg->offset, sizeof(u32)))
+			return true;
+		if (sg->length % 4)
 			return true;
 		sg = sg_next(sg);
 	}
@@ -360,6 +367,7 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 		goto theend;
 	}
 
+	j = 0;
 	len = areq->nbytes;
 	sg = areq->src;
 	i = 0;
@@ -368,12 +376,19 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 			sg = sg_next(sg);
 			continue;
 		}
-		rctx->t_src[i].addr = sg_dma_address(sg);
 		todo = min(len, sg_dma_len(sg));
-		rctx->t_src[i].len = todo / 4;
-		len -= todo;
-		rctx->t_dst[i].addr = addr_res;
-		rctx->t_dst[i].len = digestsize / 4;
+		/* only the last SG could be with a size not modulo64 */
+		if (todo % 64 == 0) {
+			rctx->t_src[i].addr = sg_dma_address(sg);
+			rctx->t_src[i].len = todo / 4;
+			rctx->t_dst[i].addr = addr_res;
+			rctx->t_dst[i].len = digestsize / 4;
+			len -= todo;
+		} else {
+			scatterwalk_map_and_copy(bf, sg, 0, todo, 0);
+			j += todo / 4;
+			len -= todo;
+		}
 		sg = sg_next(sg);
 		i++;
 	}
@@ -383,8 +398,10 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
 		goto theend;
 	}
 
+	if (j > 0)
+		i--;
+
 	byte_count = areq->nbytes;
-	j = 0;
 	bf[j++] = cpu_to_le32(0x80);
 
 	fill = 64 - (byte_count % 64);
diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h
index f9f089ede934..8c9649bab88c 100644
--- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h
+++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h
@@ -82,6 +82,8 @@
 #define PRNG_DATA_SIZE (160 / 8)
 #define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
 
+#define MAX_PAD_SIZE 4096
+
 /*
  * struct ss_clock - Describe clocks used by sun8i-ss
  * @name:       Name of clock needed by this variant
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/8] crypto: sun8i-ce: do not allocate memory when handling requests
  2022-01-26 21:04 ` [PATCH 1/8] crypto: sun8i-ce: do not allocate memory when handling requests Corentin Labbe
@ 2022-01-27  4:17   ` kernel test robot
  0 siblings, 0 replies; 10+ messages in thread
From: kernel test robot @ 2022-01-27  4:17 UTC (permalink / raw)
  To: Corentin Labbe, davem, herbert, jernej.skrabec, mripard, wens
  Cc: llvm, kbuild-all, linux-arm-kernel, linux-crypto, linux-kernel,
	linux-sunxi, linux-sunxi

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on sunxi/sunxi/for-next]
[also build test WARNING on herbert-cryptodev-2.6/master herbert-crypto-2.6/master v5.17-rc1 next-20220125]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Corentin-Labbe/crypto-allwinner-various-improvments/20220127-050556
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sunxi/linux.git sunxi/for-next
config: arm64-allmodconfig (https://download.01.org/0day-ci/archive/20220127/202201271243.hLjdr8IB-lkp@intel.com/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 2a1b7aa016c0f4b5598806205bdfbab1ea2d92c4)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm64 cross compiling tool for clang build
        # apt-get install binutils-aarch64-linux-gnu
        # https://github.com/0day-ci/linux/commit/d0b880af8c99abcd0f36463b82c92d71024408de
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Corentin-Labbe/crypto-allwinner-various-improvments/20220127-050556
        git checkout d0b880af8c99abcd0f36463b82c92d71024408de
        # save the config file to linux build tree
        mkdir build_dir
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash drivers/crypto/allwinner/sun8i-ce/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c:258:1: warning: unused label 'theend_key' [-Wunused-label]
   theend_key:
   ^~~~~~~~~~~
   1 warning generated.


vim +/theend_key +258 drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c

06f751b613296cc Corentin Labbe 2019-10-23  108  
06f751b613296cc Corentin Labbe 2019-10-23  109  	flow = rctx->flow;
06f751b613296cc Corentin Labbe 2019-10-23  110  
06f751b613296cc Corentin Labbe 2019-10-23  111  	chan = &ce->chanlist[flow];
06f751b613296cc Corentin Labbe 2019-10-23  112  
06f751b613296cc Corentin Labbe 2019-10-23  113  	cet = chan->tl;
06f751b613296cc Corentin Labbe 2019-10-23  114  	memset(cet, 0, sizeof(struct ce_task));
06f751b613296cc Corentin Labbe 2019-10-23  115  
93c7f4d357de68f Corentin Labbe 2019-11-17  116  	cet->t_id = cpu_to_le32(flow);
93c7f4d357de68f Corentin Labbe 2019-11-17  117  	common = ce->variant->alg_cipher[algt->ce_algo_id];
93c7f4d357de68f Corentin Labbe 2019-11-17  118  	common |= rctx->op_dir | CE_COMM_INT;
93c7f4d357de68f Corentin Labbe 2019-11-17  119  	cet->t_common_ctl = cpu_to_le32(common);
06f751b613296cc Corentin Labbe 2019-10-23  120  	/* CTS and recent CE (H6) need length in bytes, in word otherwise */
6b4f76c2cd9e6c3 Corentin Labbe 2020-09-18  121  	if (ce->variant->cipher_t_dlen_in_bytes)
93c7f4d357de68f Corentin Labbe 2019-11-17  122  		cet->t_dlen = cpu_to_le32(areq->cryptlen);
93c7f4d357de68f Corentin Labbe 2019-11-17  123  	else
93c7f4d357de68f Corentin Labbe 2019-11-17  124  		cet->t_dlen = cpu_to_le32(areq->cryptlen / 4);
06f751b613296cc Corentin Labbe 2019-10-23  125  
93c7f4d357de68f Corentin Labbe 2019-11-17  126  	sym = ce->variant->op_mode[algt->ce_blockmode];
06f751b613296cc Corentin Labbe 2019-10-23  127  	len = op->keylen;
06f751b613296cc Corentin Labbe 2019-10-23  128  	switch (len) {
06f751b613296cc Corentin Labbe 2019-10-23  129  	case 128 / 8:
93c7f4d357de68f Corentin Labbe 2019-11-17  130  		sym |= CE_AES_128BITS;
06f751b613296cc Corentin Labbe 2019-10-23  131  		break;
06f751b613296cc Corentin Labbe 2019-10-23  132  	case 192 / 8:
93c7f4d357de68f Corentin Labbe 2019-11-17  133  		sym |= CE_AES_192BITS;
06f751b613296cc Corentin Labbe 2019-10-23  134  		break;
06f751b613296cc Corentin Labbe 2019-10-23  135  	case 256 / 8:
93c7f4d357de68f Corentin Labbe 2019-11-17  136  		sym |= CE_AES_256BITS;
06f751b613296cc Corentin Labbe 2019-10-23  137  		break;
06f751b613296cc Corentin Labbe 2019-10-23  138  	}
06f751b613296cc Corentin Labbe 2019-10-23  139  
93c7f4d357de68f Corentin Labbe 2019-11-17  140  	cet->t_sym_ctl = cpu_to_le32(sym);
06f751b613296cc Corentin Labbe 2019-10-23  141  	cet->t_asym_ctl = 0;
06f751b613296cc Corentin Labbe 2019-10-23  142  
0605fa0f78266cc Corentin Labbe 2020-09-18  143  	rctx->addr_key = dma_map_single(ce->dev, op->key, op->keylen, DMA_TO_DEVICE);
0605fa0f78266cc Corentin Labbe 2020-09-18  144  	if (dma_mapping_error(ce->dev, rctx->addr_key)) {
06f751b613296cc Corentin Labbe 2019-10-23  145  		dev_err(ce->dev, "Cannot DMA MAP KEY\n");
06f751b613296cc Corentin Labbe 2019-10-23  146  		err = -EFAULT;
06f751b613296cc Corentin Labbe 2019-10-23  147  		goto theend;
06f751b613296cc Corentin Labbe 2019-10-23  148  	}
0605fa0f78266cc Corentin Labbe 2020-09-18  149  	cet->t_key = cpu_to_le32(rctx->addr_key);
06f751b613296cc Corentin Labbe 2019-10-23  150  
06f751b613296cc Corentin Labbe 2019-10-23  151  	ivsize = crypto_skcipher_ivsize(tfm);
06f751b613296cc Corentin Labbe 2019-10-23  152  	if (areq->iv && crypto_skcipher_ivsize(tfm) > 0) {
a216f8d540cf132 Corentin Labbe 2020-09-18  153  		rctx->ivlen = ivsize;
06f751b613296cc Corentin Labbe 2019-10-23  154  		if (rctx->op_dir & CE_DECRYPTION) {
06f751b613296cc Corentin Labbe 2019-10-23  155  			offset = areq->cryptlen - ivsize;
d0b880af8c99abc Corentin Labbe 2022-01-26  156  			scatterwalk_map_and_copy(chan->backup_iv, areq->src,
a216f8d540cf132 Corentin Labbe 2020-09-18  157  						 offset, ivsize, 0);
06f751b613296cc Corentin Labbe 2019-10-23  158  		}
d0b880af8c99abc Corentin Labbe 2022-01-26  159  		memcpy(chan->bounce_iv, areq->iv, ivsize);
d0b880af8c99abc Corentin Labbe 2022-01-26  160  		rctx->addr_iv = dma_map_single(ce->dev, chan->bounce_iv, rctx->ivlen,
93c7f4d357de68f Corentin Labbe 2019-11-17  161  					       DMA_TO_DEVICE);
0605fa0f78266cc Corentin Labbe 2020-09-18  162  		if (dma_mapping_error(ce->dev, rctx->addr_iv)) {
06f751b613296cc Corentin Labbe 2019-10-23  163  			dev_err(ce->dev, "Cannot DMA MAP IV\n");
06f751b613296cc Corentin Labbe 2019-10-23  164  			err = -ENOMEM;
06f751b613296cc Corentin Labbe 2019-10-23  165  			goto theend_iv;
06f751b613296cc Corentin Labbe 2019-10-23  166  		}
0605fa0f78266cc Corentin Labbe 2020-09-18  167  		cet->t_iv = cpu_to_le32(rctx->addr_iv);
06f751b613296cc Corentin Labbe 2019-10-23  168  	}
06f751b613296cc Corentin Labbe 2019-10-23  169  
06f751b613296cc Corentin Labbe 2019-10-23  170  	if (areq->src == areq->dst) {
06f751b613296cc Corentin Labbe 2019-10-23  171  		nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src),
06f751b613296cc Corentin Labbe 2019-10-23  172  				    DMA_BIDIRECTIONAL);
06f751b613296cc Corentin Labbe 2019-10-23  173  		if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
06f751b613296cc Corentin Labbe 2019-10-23  174  			dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
06f751b613296cc Corentin Labbe 2019-10-23  175  			err = -EINVAL;
06f751b613296cc Corentin Labbe 2019-10-23  176  			goto theend_iv;
06f751b613296cc Corentin Labbe 2019-10-23  177  		}
06f751b613296cc Corentin Labbe 2019-10-23  178  		nr_sgd = nr_sgs;
06f751b613296cc Corentin Labbe 2019-10-23  179  	} else {
06f751b613296cc Corentin Labbe 2019-10-23  180  		nr_sgs = dma_map_sg(ce->dev, areq->src, sg_nents(areq->src),
06f751b613296cc Corentin Labbe 2019-10-23  181  				    DMA_TO_DEVICE);
06f751b613296cc Corentin Labbe 2019-10-23  182  		if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
06f751b613296cc Corentin Labbe 2019-10-23  183  			dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs);
06f751b613296cc Corentin Labbe 2019-10-23  184  			err = -EINVAL;
06f751b613296cc Corentin Labbe 2019-10-23  185  			goto theend_iv;
06f751b613296cc Corentin Labbe 2019-10-23  186  		}
06f751b613296cc Corentin Labbe 2019-10-23  187  		nr_sgd = dma_map_sg(ce->dev, areq->dst, sg_nents(areq->dst),
06f751b613296cc Corentin Labbe 2019-10-23  188  				    DMA_FROM_DEVICE);
06f751b613296cc Corentin Labbe 2019-10-23  189  		if (nr_sgd <= 0 || nr_sgd > MAX_SG) {
06f751b613296cc Corentin Labbe 2019-10-23  190  			dev_err(ce->dev, "Invalid sg number %d\n", nr_sgd);
06f751b613296cc Corentin Labbe 2019-10-23  191  			err = -EINVAL;
06f751b613296cc Corentin Labbe 2019-10-23  192  			goto theend_sgs;
06f751b613296cc Corentin Labbe 2019-10-23  193  		}
06f751b613296cc Corentin Labbe 2019-10-23  194  	}
06f751b613296cc Corentin Labbe 2019-10-23  195  
06f751b613296cc Corentin Labbe 2019-10-23  196  	len = areq->cryptlen;
06f751b613296cc Corentin Labbe 2019-10-23  197  	for_each_sg(areq->src, sg, nr_sgs, i) {
93c7f4d357de68f Corentin Labbe 2019-11-17  198  		cet->t_src[i].addr = cpu_to_le32(sg_dma_address(sg));
06f751b613296cc Corentin Labbe 2019-10-23  199  		todo = min(len, sg_dma_len(sg));
93c7f4d357de68f Corentin Labbe 2019-11-17  200  		cet->t_src[i].len = cpu_to_le32(todo / 4);
06f751b613296cc Corentin Labbe 2019-10-23  201  		dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__,
06f751b613296cc Corentin Labbe 2019-10-23  202  			areq->cryptlen, i, cet->t_src[i].len, sg->offset, todo);
06f751b613296cc Corentin Labbe 2019-10-23  203  		len -= todo;
06f751b613296cc Corentin Labbe 2019-10-23  204  	}
06f751b613296cc Corentin Labbe 2019-10-23  205  	if (len > 0) {
06f751b613296cc Corentin Labbe 2019-10-23  206  		dev_err(ce->dev, "remaining len %d\n", len);
06f751b613296cc Corentin Labbe 2019-10-23  207  		err = -EINVAL;
06f751b613296cc Corentin Labbe 2019-10-23  208  		goto theend_sgs;
06f751b613296cc Corentin Labbe 2019-10-23  209  	}
06f751b613296cc Corentin Labbe 2019-10-23  210  
06f751b613296cc Corentin Labbe 2019-10-23  211  	len = areq->cryptlen;
06f751b613296cc Corentin Labbe 2019-10-23  212  	for_each_sg(areq->dst, sg, nr_sgd, i) {
93c7f4d357de68f Corentin Labbe 2019-11-17  213  		cet->t_dst[i].addr = cpu_to_le32(sg_dma_address(sg));
06f751b613296cc Corentin Labbe 2019-10-23  214  		todo = min(len, sg_dma_len(sg));
93c7f4d357de68f Corentin Labbe 2019-11-17  215  		cet->t_dst[i].len = cpu_to_le32(todo / 4);
06f751b613296cc Corentin Labbe 2019-10-23  216  		dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__,
06f751b613296cc Corentin Labbe 2019-10-23  217  			areq->cryptlen, i, cet->t_dst[i].len, sg->offset, todo);
06f751b613296cc Corentin Labbe 2019-10-23  218  		len -= todo;
06f751b613296cc Corentin Labbe 2019-10-23  219  	}
06f751b613296cc Corentin Labbe 2019-10-23  220  	if (len > 0) {
06f751b613296cc Corentin Labbe 2019-10-23  221  		dev_err(ce->dev, "remaining len %d\n", len);
06f751b613296cc Corentin Labbe 2019-10-23  222  		err = -EINVAL;
06f751b613296cc Corentin Labbe 2019-10-23  223  		goto theend_sgs;
06f751b613296cc Corentin Labbe 2019-10-23  224  	}
06f751b613296cc Corentin Labbe 2019-10-23  225  
06f751b613296cc Corentin Labbe 2019-10-23  226  	chan->timeout = areq->cryptlen;
0605fa0f78266cc Corentin Labbe 2020-09-18  227  	rctx->nr_sgs = nr_sgs;
0605fa0f78266cc Corentin Labbe 2020-09-18  228  	rctx->nr_sgd = nr_sgd;
0605fa0f78266cc Corentin Labbe 2020-09-18  229  	return 0;
06f751b613296cc Corentin Labbe 2019-10-23  230  
06f751b613296cc Corentin Labbe 2019-10-23  231  theend_sgs:
06f751b613296cc Corentin Labbe 2019-10-23  232  	if (areq->src == areq->dst) {
884b93c51025026 Xiang Chen     2021-03-16  233  		dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src),
884b93c51025026 Xiang Chen     2021-03-16  234  			     DMA_BIDIRECTIONAL);
06f751b613296cc Corentin Labbe 2019-10-23  235  	} else {
06f751b613296cc Corentin Labbe 2019-10-23  236  		if (nr_sgs > 0)
884b93c51025026 Xiang Chen     2021-03-16  237  			dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src),
884b93c51025026 Xiang Chen     2021-03-16  238  				     DMA_TO_DEVICE);
884b93c51025026 Xiang Chen     2021-03-16  239  		dma_unmap_sg(ce->dev, areq->dst, sg_nents(areq->dst),
884b93c51025026 Xiang Chen     2021-03-16  240  			     DMA_FROM_DEVICE);
06f751b613296cc Corentin Labbe 2019-10-23  241  	}
06f751b613296cc Corentin Labbe 2019-10-23  242  
06f751b613296cc Corentin Labbe 2019-10-23  243  theend_iv:
06f751b613296cc Corentin Labbe 2019-10-23  244  	if (areq->iv && ivsize > 0) {
0605fa0f78266cc Corentin Labbe 2020-09-18  245  		if (rctx->addr_iv)
0605fa0f78266cc Corentin Labbe 2020-09-18  246  			dma_unmap_single(ce->dev, rctx->addr_iv, rctx->ivlen, DMA_TO_DEVICE);
06f751b613296cc Corentin Labbe 2019-10-23  247  		offset = areq->cryptlen - ivsize;
06f751b613296cc Corentin Labbe 2019-10-23  248  		if (rctx->op_dir & CE_DECRYPTION) {
d0b880af8c99abc Corentin Labbe 2022-01-26  249  			memcpy(areq->iv, chan->backup_iv, ivsize);
d0b880af8c99abc Corentin Labbe 2022-01-26  250  			memzero_explicit(chan->backup_iv, ivsize);
06f751b613296cc Corentin Labbe 2019-10-23  251  		} else {
06f751b613296cc Corentin Labbe 2019-10-23  252  			scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
06f751b613296cc Corentin Labbe 2019-10-23  253  						 ivsize, 0);
06f751b613296cc Corentin Labbe 2019-10-23  254  		}
d0b880af8c99abc Corentin Labbe 2022-01-26  255  		memzero_explicit(chan->bounce_iv, ivsize);
06f751b613296cc Corentin Labbe 2019-10-23  256  	}
06f751b613296cc Corentin Labbe 2019-10-23  257  
06f751b613296cc Corentin Labbe 2019-10-23 @258  theend_key:
0605fa0f78266cc Corentin Labbe 2020-09-18  259  	dma_unmap_single(ce->dev, rctx->addr_key, op->keylen, DMA_TO_DEVICE);
06f751b613296cc Corentin Labbe 2019-10-23  260  
06f751b613296cc Corentin Labbe 2019-10-23  261  theend:
06f751b613296cc Corentin Labbe 2019-10-23  262  	return err;
06f751b613296cc Corentin Labbe 2019-10-23  263  }
06f751b613296cc Corentin Labbe 2019-10-23  264  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-01-27  4:17 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-26 21:04 [PATCH 0/8] crypto: allwinner: various improvments Corentin Labbe
2022-01-26 21:04 ` [PATCH 1/8] crypto: sun8i-ce: do not allocate memory when handling requests Corentin Labbe
2022-01-27  4:17   ` kernel test robot
2022-01-26 21:04 ` [PATCH 2/8] crypto: sun4i-ss: do not allocate backup IV on requests Corentin Labbe
2022-01-26 21:04 ` [PATCH 3/8] crypto: sun8i-ss: handle zero sized sg Corentin Labbe
2022-01-26 21:04 ` [PATCH 4/8] crypto: sun8i-ss: do not allocate memory when handling hash requests Corentin Labbe
2022-01-26 21:04 ` [PATCH 5/8] crypto: sun8i-ss: do not zeroize all pad Corentin Labbe
2022-01-26 21:04 ` [PATCH 6/8] crypto: sun8i-ss: remove redundant test Corentin Labbe
2022-01-26 21:04 ` [PATCH 7/8] crypto: sun8i-ss: test error before assigning Corentin Labbe
2022-01-26 21:04 ` [PATCH 8/8] crypto: sun8i-ss: handle requests if last block is not modulo 64 Corentin Labbe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).