Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Jeff Barnes <jeffbarnes@linux.microsoft.com>
To: linux-crypto@vger.kernel.org
Cc: herbert@gondor.apana.org.au, davem@davemloft.net,
	horia.geanta@nxp.com, pankaj.gupta@nxp.com,
	sahil.malhotra@oss.nxp.com, changwei.zou@canonical.com,
	meenakshi.aggarwal@nxp.com, tudor-dan.ambarus@nxp.com,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 2/2] crypto: caam - fix in-place RSA payload DMA mapping
Date: Thu, 19 Nov 2026 14:59:48 -0500	[thread overview]
Message-ID: <20261119195948.816661-3-jeffbarnes@linux.microsoft.com> (raw)
In-Reply-To: <20261119195948.816661-1-jeffbarnes@linux.microsoft.com>

CAAM maps the effective RSA source DMA_TO_DEVICE and the destination
DMA_FROM_DEVICE independently. When both use the same scatterlist,
the second mapping can overwrite the DMA metadata needed to release
the first. On an arm64 system with CAAM behind an SMMU this caused
ARM-LPAE/IOMMU unmap warnings and RSA signature self-test failures.

For an aligned exact effective-head alias, map the entries covering the
larger source/destination extent once with DMA_BIDIRECTIONAL. If this
returns a single DMA segment, retain that mapping and release it once
with the original entry count on completion or allocation failure.

If the shared mapping returns multiple DMA segments, release it before
mapping either hardware view independently. Use the existing destination
bounce buffer, mapping the source DMA_TO_DEVICE and the distinct bounce
buffer DMA_FROM_DEVICE. This avoids separate bounded SEC4 views without
rejecting requests that previously worked on dma-direct systems.

Detect the original req->src == req->dst relationship before stripping
leading zeros. If stripping advances the effective source, force the
destination bounce path even though the effective heads differ. This
avoids overlapping streaming mappings through different SG metadata.
Arbitrary overlapping buffers described by otherwise distinct original
and effective SG heads are outside the scope of this fix.

Reuse the fixup_dst and bounce-buffer lifetime handling from Changwei's
unaligned-destination fix. In particular, unmap the payload before
copy-back, copy only the RSA output on successful completion, and retain
the bounce buffer across job-ring retries and accepted backlog requests.
Non-aliased requests retain their existing mapping directions.

Unlike the RFC, multi-segment aliases no longer return -EOPNOTSUPP.

Fixes: 8c419778ab57 ("crypto: caam - add support for RSA algorithm")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/linux-crypto/20261114060032.872141-1-changwei.zou@canonical.com/
Link: https://lore.kernel.org/linux-crypto/20261020192511.3389133-1-jeffbarnes@linux.microsoft.com/
Signed-off-by: Jeff Barnes <jeffbarnes@linux.microsoft.com>
---
 drivers/crypto/caam/caampkc.c | 84 ++++++++++++++++++++++++++---------
 drivers/crypto/caam/caampkc.h |  3 ++
 2 files changed, 65 insertions(+), 22 deletions(-)

diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
index d81ef97861c0c..bffd499164436 100644
--- a/drivers/crypto/caam/caampkc.c
+++ b/drivers/crypto/caam/caampkc.c
@@ -51,8 +51,15 @@ static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc,
 {
 	struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
 
-	dma_unmap_sg(dev, req_ctx->fixup_dst, edesc->dst_nents, DMA_FROM_DEVICE);
-	dma_unmap_sg(dev, req_ctx->fixup_src, edesc->src_nents, DMA_TO_DEVICE);
+	if (edesc->shared_nents) {
+		dma_unmap_sg(dev, req_ctx->fixup_src, edesc->shared_nents,
+			     DMA_BIDIRECTIONAL);
+	} else {
+		dma_unmap_sg(dev, req_ctx->fixup_dst, edesc->dst_nents,
+			     DMA_FROM_DEVICE);
+		dma_unmap_sg(dev, req_ctx->fixup_src, edesc->src_nents,
+			     DMA_TO_DEVICE);
+	}
 
 	if (edesc->sec4_sg_bytes)
 		dma_unmap_single(dev, edesc->sec4_sg_dma, edesc->sec4_sg_bytes,
@@ -330,14 +337,18 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 	struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
 	struct caam_rsa_key *key = &ctx->key;
 	struct rsa_edesc *edesc;
+	struct scatterlist *sg;
 	gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
 		       GFP_KERNEL : GFP_ATOMIC;
 	int sg_flags = (flags == GFP_ATOMIC) ? SG_MITER_ATOMIC : 0;
 	int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
 	int src_nents, dst_nents;
+	int shared_nents = 0;
 	int mapped_src_nents, mapped_dst_nents;
 	unsigned int diff_size = 0;
-	int lzeros;
+	bool in_place = req->src == req->dst;
+	bool bounce;
+	int lzeros, i;
 
 	if (req->src_len > key->n_sz) {
 		/*
@@ -373,26 +384,48 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 
 	req_ctx->bounce_buf = NULL;
 	req_ctx->fixup_dst = req->dst;
-	if (req->dst_len > 0) {
-		struct scatterlist *sg;
-		int i;
-
-		for_each_sg(req->dst, sg, dst_nents, i) {
-			if (!sg_is_dma_aligned(sg)) {
-				unsigned int bounce_len =
-					ALIGN(req->dst_len,
-					      dma_get_cache_alignment());
-
-				req_ctx->bounce_buf = kzalloc(bounce_len, flags);
-				if (!req_ctx->bounce_buf)
-					return ERR_PTR(-ENOMEM);
-				sg_init_one(&req_ctx->dst, req_ctx->bounce_buf,
-					    bounce_len);
-				req_ctx->fixup_dst = &req_ctx->dst;
-				dst_nents = 1;
-				break;
-			}
+	bounce = in_place && req_ctx->fixup_src != req->src;
+	for_each_sg(req->dst, sg, dst_nents, i) {
+		if (!sg_is_dma_aligned(sg)) {
+			bounce = true;
+			break;
+		}
+	}
+
+	if (!bounce && req_ctx->fixup_src == req_ctx->fixup_dst) {
+		shared_nents = max(src_nents, dst_nents);
+		mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src,
+					      shared_nents, DMA_BIDIRECTIONAL);
+		if (unlikely(!mapped_src_nents)) {
+			dev_err(dev, "unable to map shared S/G\n");
+			goto bounce_fail;
+		}
+
+		if (mapped_src_nents == 1) {
+			mapped_dst_nents = mapped_src_nents;
+			goto alloc_edesc;
 		}
+
+		/*
+		 * Distinct DMA segments need independently bounded hardware
+		 * views. Bounce the destination instead of sharing SEC4 tables.
+		 */
+		dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
+			     DMA_BIDIRECTIONAL);
+		shared_nents = 0;
+		bounce = true;
+	}
+
+	if (bounce) {
+		unsigned int bounce_len = ALIGN(req->dst_len,
+						dma_get_cache_alignment());
+
+		req_ctx->bounce_buf = kzalloc(bounce_len, flags);
+		if (!req_ctx->bounce_buf)
+			goto bounce_fail;
+		sg_init_one(&req_ctx->dst, req_ctx->bounce_buf, bounce_len);
+		req_ctx->fixup_dst = &req_ctx->dst;
+		dst_nents = 1;
 	}
 
 	mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src, src_nents,
@@ -408,6 +441,7 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 		goto src_fail;
 	}
 
+alloc_edesc:
 	if (!diff_size && mapped_src_nents == 1)
 		sec4_sg_len = 0; /* no need for an input hw s/g table */
 	else
@@ -442,6 +476,7 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 	/* Save nents for later use in Job Descriptor */
 	edesc->src_nents = src_nents;
 	edesc->dst_nents = dst_nents;
+	edesc->shared_nents = shared_nents;
 
 	req_ctx->edesc = edesc;
 
@@ -469,6 +504,11 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 sec4_sg_fail:
 	kfree(edesc);
 dst_fail:
+	if (shared_nents) {
+		dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
+			     DMA_BIDIRECTIONAL);
+		goto bounce_fail;
+	}
 	dma_unmap_sg(dev, req_ctx->fixup_dst, dst_nents, DMA_FROM_DEVICE);
 src_fail:
 	dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE);
diff --git a/drivers/crypto/caam/caampkc.h b/drivers/crypto/caam/caampkc.h
index 8810bcc435ccd..5ec93d8973347 100644
--- a/drivers/crypto/caam/caampkc.h
+++ b/drivers/crypto/caam/caampkc.h
@@ -126,6 +126,8 @@ struct caam_rsa_req_ctx {
  * rsa_edesc - s/w-extended rsa descriptor
  * @src_nents     : number of segments in input s/w scatterlist
  * @dst_nents     : number of segments in output s/w scatterlist
+ * @shared_nents  : original shared DMA_BIDIRECTIONAL map/unmap count,
+ *                 or zero for independent source and destination mappings
  * @mapped_src_nents: number of segments in input h/w link table
  * @mapped_dst_nents: number of segments in output h/w link table
  * @sec4_sg_bytes : length of h/w link table
@@ -138,6 +140,7 @@ struct caam_rsa_req_ctx {
 struct rsa_edesc {
 	int src_nents;
 	int dst_nents;
+	int shared_nents;
 	int mapped_src_nents;
 	int mapped_dst_nents;
 	int sec4_sg_bytes;
-- 
2.43.0


      parent reply	other threads:[~2026-11-19 20:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:59 [PATCH v2 0/2] crypto: caam - fix in-place RSA DMA mapping Jeff Barnes
2026-11-19 19:59 ` [PATCH v2 1/2] crypto: caam - validate RSA scatterlist extents before mapping Jeff Barnes
2026-11-19 19:59 ` Jeff Barnes [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119195948.816661-3-jeffbarnes@linux.microsoft.com \
    --to=jeffbarnes@linux.microsoft.com \
    --cc=changwei.zou@canonical.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=horia.geanta@nxp.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=meenakshi.aggarwal@nxp.com \
    --cc=pankaj.gupta@nxp.com \
    --cc=sahil.malhotra@oss.nxp.com \
    --cc=tudor-dan.ambarus@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox