[PATCH v15 34/35] crypto: amlogic: keep the cipher key/IV buffer per request

Jose Nieto jnieto.alaber at gmail.com
Wed Dec 2 02:10:35 PST 2026


struct meson_cipher_tfm_ctx is shared by all requests of a transform, but
requests are dispatched round-robin across the independent flow engines,
so two requests on the same transform can run concurrently on different
crypto_engine threads and race on op->keyiv (the CBC IV is rewritten for
every request).

Move the DMA-mapped key/IV buffer into the per-request context and use the
DMA-aligned request context accessors so concurrent requests no longer
share it.  The per-tfm key is still kept in meson_cipher_tfm_ctx and
copied into the request buffer.

Signed-off-by: Jose Nieto <jnieto.alaber at gmail.com>
---
 drivers/crypto/amlogic/amlogic-gxl-cipher.c | 20 +++++++++++---------
 drivers/crypto/amlogic/amlogic-gxl.h        |  2 ++
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
index e3a0b75c7daf..3264264514f5 100644
--- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
+++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
@@ -64,7 +64,7 @@ static int meson_cipher_do_fallback(struct skcipher_request *areq)
 {
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
 	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx_dma(tfm);
-	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
+	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx_dma(areq);
 	int err;
 #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
@@ -145,7 +145,7 @@ static void meson_unmap_scatterlist(struct skcipher_request *areq, struct meson_
 
 static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
 {
-	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(ctx->areq);
+	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx_dma(ctx->areq);
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(ctx->areq);
 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
 	struct meson_alg_template *algt = container_of(alg,
@@ -161,7 +161,7 @@ static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
 		return;
 
 	if (blockmode == DESC_OPMODE_CBC) {
-		memcpy(op->keyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize);
+		memcpy(rctx->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);
 	}
@@ -185,7 +185,7 @@ static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
 
 static bool meson_setup_data_descs(struct cipher_ctx *ctx)
 {
-	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(ctx->areq);
+	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx_dma(ctx->areq);
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(ctx->areq);
 	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx_dma(tfm);
 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
@@ -228,7 +228,7 @@ static bool meson_setup_data_descs(struct cipher_ctx *ctx)
 
 static int meson_kick_hardware(struct cipher_ctx *ctx)
 {
-	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(ctx->areq);
+	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx_dma(ctx->areq);
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(ctx->areq);
 	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx_dma(tfm);
 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
@@ -301,7 +301,7 @@ static int meson_cipher(struct skcipher_request *areq)
 {
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
 	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx_dma(tfm);
-	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
+	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx_dma(areq);
 	struct meson_dev *mc = op->mc;
 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
 	struct meson_alg_template *algt;
@@ -336,7 +336,9 @@ static int meson_cipher(struct skcipher_request *areq)
 	if (algt->blockmode == DESC_OPMODE_CBC)
 		ctx.keyiv.len = AES_MAX_KEY_SIZE + ivsize;
 
-	ctx.keyiv.addr = dma_map_single(mc->dev, op->keyiv, ctx.keyiv.len,
+	memcpy(rctx->keyiv, op->keyiv, AES_MAX_KEY_SIZE);
+
+	ctx.keyiv.addr = dma_map_single(mc->dev, rctx->keyiv, ctx.keyiv.len,
 					DMA_TO_DEVICE);
 	err = dma_mapping_error(mc->dev, ctx.keyiv.addr);
 	if (err) {
@@ -395,7 +397,7 @@ static int meson_skdecrypt(struct skcipher_request *areq)
 {
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
 	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx_dma(tfm);
-	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
+	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx_dma(areq);
 	struct crypto_engine *engine;
 	int e;
 
@@ -413,7 +415,7 @@ static int meson_skencrypt(struct skcipher_request *areq)
 {
 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
 	struct meson_cipher_tfm_ctx *op = crypto_skcipher_ctx_dma(tfm);
-	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
+	struct meson_cipher_req_ctx *rctx = skcipher_request_ctx_dma(areq);
 	struct crypto_engine *engine;
 	int e;
 
diff --git a/drivers/crypto/amlogic/amlogic-gxl.h b/drivers/crypto/amlogic/amlogic-gxl.h
index e76576f05271..2a298763b99b 100644
--- a/drivers/crypto/amlogic/amlogic-gxl.h
+++ b/drivers/crypto/amlogic/amlogic-gxl.h
@@ -125,10 +125,12 @@ struct meson_dev {
 
 /*
  * struct meson_cipher_req_ctx - context for a skcipher request
+ * @keyiv:	per-request key/IV buffer (DMA-mapped)
  * @op_dir:	direction (encrypt vs decrypt) for this request
  * @flow:	the flow to use for this request
  */
 struct meson_cipher_req_ctx {
+	u8 keyiv[AES_MAX_KEY_SIZE + AES_BLOCK_SIZE];
 	u32 op_dir;
 	int flow;
 	struct skcipher_request fallback_req;	// keep at the end
-- 
2.47.3




More information about the linux-arm-kernel mailing list