mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH 0/1] crypto: caam - fix in-place RSA payload DMA mapping
@ 2026-10-20 19:25 Jeff Barnes
  2026-10-20 19:25 ` [RFC PATCH 1/1] " Jeff Barnes
  2026-11-10 23:19 ` [RFC PATCH 0/1] " Jeff Barnes
  0 siblings, 2 replies; 8+ messages in thread
From: Jeff Barnes @ 2026-10-20 19:25 UTC (permalink / raw)
  To: horia.geanta, pankaj.gupta, gaurav.jain, herbert, davem
  Cc: linux-crypto, linux-kernel

Hi,

This RFC fixes a DMA ownership problem in the CAAM RSA driver when the
effective source and destination are the same scatterlist.

rsa_edesc_alloc() currently maps the effective source with DMA_TO_DEVICE
and the destination with DMA_FROM_DEVICE. For an in-place request, this
maps the same scatterlist twice concurrently with different DMA directions.

dma_map_sg() is allowed to modify the scatterlist DMA metadata. On the
arm64/IOMMU system where I reproduced this, the two mappings returned
different DMA addresses. The second mapping replaced the metadata needed
to tear down the first one.

Completion subsequently unmapped the destination and then attempted to
unmap the source using the overwritten scatterlist metadata. This resulted
in ARM-LPAE/IOMMU unmap warnings from the CAAM RSA completion paths.

The user-visible crypto self-test failure included:

  alg: sig: sign test failed: invalid output
  alg: sig: test 1 failed for pkcs1(rsa-caam,sha256): err -22

The warnings were observed through the CAAM RSA public and private
completion paths, rsa_pub_done() and rsa_priv_f_done().

The patch gives an exact effective-head alias a single DMA owner.
CAAM reads and writes the shared payload, so the scatterlist is mapped
once with DMA_BIDIRECTIONAL and unmapped once with the original SG count.
Requests with distinct effective source and destination heads retain the
existing DMA_TO_DEVICE / DMA_FROM_DEVICE mappings.

There is one intentional limitation in this RFC.

For the existing direct hardware representation, a shared mapping
returning one DMA segment is straightforward. A shared mapping returning
multiple DMA segments is not. The logical source and destination may
cover different extents of the shared scatterlist, while the existing
SEC4/PDB bookkeeping uses the mapped source/destination counts to select
and locate those hardware views.

Source analysis found request shapes for which assigning the shared
returned count to both views causes the destination SEC4 table position
to differ from the offset reconstructed by PDB setup. Short-source
public RSA also raises a separate source-view selection issue.

Rather than send a representation known to be incorrect to CAAM, this
RFC unmaps a shared mapping that returns more than one DMA segment and
returns -EOPNOTSUPP before SEC4/PDB construction or job submission.

I would particularly appreciate feedback on two questions:

1. Is a single DMA_BIDIRECTIONAL mapping the preferred ownership model
   for an exact effective source/destination alias?

2. For a shared mapping returning multiple DMA segments, should CAAM
   grow separately bounded source and destination SEC4 views, or should
   such requests instead use a distinct destination, for example through
   a bounce-buffer path?

I tested the patch on an LX2160A arm64 system with CAAM Era 10 behind an
ARM SMMU.

The rsa-caam and pkcs1(rsa-caam,sha256) self-tests passed, and the
certificate-verification RSA selftest completed successfully, with no
recurrence of the original ARM-LPAE/IOMMU unmap warnings.

I also exercised the RFC containment path with a targeted full-width
2048-bit RSA request. The same two-entry scatterlist was supplied as
source and destination, using two separately allocated 128-byte entries.
This reached the multi-segment shared-mapping containment path.

The operation returned -EOPNOTSUPP synchronously, no completion callback
ran, and no CAAM descriptor/job-ring, DMA/IOMMU, WARN, Oops, panic, or
invalid-output error occurred during the test interval. The test module
unloaded cleanly.

There are related RSA DMA fixes under discussion. CAAM work for unaligned
RSA destinations uses a bounce-buffer approach, which may provide one
possible direction for the general multi-segment alias case. Separately,
work on RSA buffers on non-coherent DMA systems identified a cacheline
sharing problem and also demonstrates legitimate in-place RSA requests.

Neither is required by this RFC. This patch is based on current Linus
master and addresses the independent problem of acquiring two streaming
DMA mappings over the same live scatterlist.

Thanks,
Jeff

Jeff Barnes (1):
  crypto: caam - fix in-place RSA payload DMA mapping

 drivers/crypto/caam/caampkc.c | 79 ++++++++++++++++++++++++++++++------
 drivers/crypto/caam/caampkc.h |  3 ++
 2 files changed, 69 insertions(+), 13 deletions(-)

-- 
2.43.0

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

* [RFC PATCH 1/1] crypto: caam - fix in-place RSA payload DMA mapping
  2026-10-20 19:25 [RFC PATCH 0/1] crypto: caam - fix in-place RSA payload DMA mapping Jeff Barnes
@ 2026-10-20 19:25 ` Jeff Barnes
  2026-11-11 10:54   ` Herbert Xu
  2026-11-12  8:15   ` Sahil Malhotra
  2026-11-10 23:19 ` [RFC PATCH 0/1] " Jeff Barnes
  1 sibling, 2 replies; 8+ messages in thread
From: Jeff Barnes @ 2026-10-20 19:25 UTC (permalink / raw)
  To: horia.geanta, pankaj.gupta, gaurav.jain, herbert, davem
  Cc: linux-crypto, linux-kernel

CAAM can receive RSA requests whose effective source and destination
scatterlists are identical. The driver currently maps the source with
DMA_TO_DEVICE and then maps the destination with DMA_FROM_DEVICE.

When both refer to the same scatterlist, the second dma_map_sg() can
overwrite the DMA metadata stored in the scatterlist by the first mapping.
On an IOMMU-backed non-coherent arm64 system, the two mappings can return
different DMA addresses. Completion then unmaps the destination mapping and
subsequently uses the overwritten scatterlist metadata while attempting to
unmap the source.

As a result, the destination mapping can be unmapped a second time while
the original source mapping is no longer represented by the scatterlist DMA
metadata.

This was observed during CAAM RSA crypto self-tests. One visible failure
was:

  alg: sig: sign test failed: invalid output
  alg: sig: test 1 failed for pkcs1(rsa-caam,sha256): err -22

The failures were accompanied by ARM-LPAE/IOMMU unmap warnings through
dma_unmap_sg_attrs(), originating from the CAAM RSA completion paths. The
warnings were observed through both rsa_pub_done() and rsa_priv_f_done().

Fix the DMA ownership problem by mapping an exact effective-head alias only
once using DMA_BIDIRECTIONAL, since CAAM both reads from and writes to the
shared payload. Retain the original scatterlist entry count passed to
dma_map_sg() and use that same count for the matching dma_unmap_sg().

Keep the existing DMA_TO_DEVICE and DMA_FROM_DEVICE ownership for requests
whose effective source and destination heads differ.

The source and destination can cover different logical byte extents of the
same scatterlist. For a shared mapping, therefore map the number of
original SG entries needed to cover the larger of the effective source
length and destination length.

A shared mapping for which dma_map_sg() returns one DMA segment can
continue using the existing direct hardware views. If the shared mapping
returns more than one DMA segment, however, the source and destination
require separately bounded SEC4 views.

The current SEC4/PDB bookkeeping cannot correctly represent every such
multi-segment request. In particular, for a short source the reserved
source view and the actual destination-table position can differ from the
offset reconstructed later by the PDB setup. Using the shared returned
segment count for source-table selection can also select a padding-
prefixed source view whose representation does not match the original
logical source length.

For this RFC, release a shared mapping that returns multiple DMA segments
and return -EOPNOTSUPP before allocating the extended descriptor,
constructing the SEC4/PDB representation, or submitting a job to CAAM. This
prevents a known-invalid representation from reaching hardware while
leaving the general multi-segment representation for further discussion.

The ownership fix was validated on an LX2160A arm64 system with CAAM Era 10
and an ARM SMMU. The existing single-segment exact-alias paths passed the
rsa-caam and pkcs1(rsa-caam,sha256) self-tests, including certificate
verification, with no recurrence of the ARM-LPAE/IOMMU unmap warnings.

The RFC containment path was also exercised directly on the same hardware.
A full-width 2048-bit RSA request used the same two-entry scatterlist as
both source and destination, with two separately allocated 128-byte
entries. The shared DMA mapping returned multiple DMA segments and reached
the containment path.

The RSA operation returned -EOPNOTSUPP synchronously before CAAM
submission, with no completion callback. The test unloaded cleanly, and no
ARM-LPAE, IOMMU/DMA API, CAAM descriptor, invalid-output, WARN, Oops, or
panic message was observed during the test interval.

The runtime test establishes that the multi-segment containment path was
reached and returned synchronously without submitting the RSA operation.
The single matching dma_unmap_sg() of that mapping follows from the
reviewed control flow; the test did not independently instrument or count
DMA unmap calls.

Signed-off-by: Jeff Barnes <jeffbarnes@linux.microsoft.com>
---
 drivers/crypto/caam/caampkc.c | 79 +++++++++++++++++++++++++++++------
 drivers/crypto/caam/caampkc.h |  3 ++
 2 files changed, 69 insertions(+), 13 deletions(-)

diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
index cb001aa1de66..fed20a401712 100644
--- a/drivers/crypto/caam/caampkc.c
+++ b/drivers/crypto/caam/caampkc.c
@@ -51,8 +51,14 @@ 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->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->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,
@@ -260,6 +266,7 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 	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;
@@ -289,19 +296,59 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req,
 
 	src_nents = sg_nents_for_len(req_ctx->fixup_src,
 				     req_ctx->fixup_src_len);
-	dst_nents = sg_nents_for_len(req->dst, req->dst_len);
+	if (unlikely(src_nents < 0)) {
+		dev_err(dev, "Insufficient bytes (%u) in src S/G\n",
+			req_ctx->fixup_src_len);
+		return ERR_PTR(src_nents);
+	}
 
-	mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src, src_nents,
-				      DMA_TO_DEVICE);
-	if (unlikely(!mapped_src_nents)) {
-		dev_err(dev, "unable to map source\n");
-		return ERR_PTR(-ENOMEM);
+	dst_nents = sg_nents_for_len(req->dst, req->dst_len);
+	if (unlikely(dst_nents < 0)) {
+		dev_err(dev, "Insufficient bytes (%u) in dst S/G\n",
+			req->dst_len);
+		return ERR_PTR(dst_nents);
 	}
-	mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
-				      DMA_FROM_DEVICE);
-	if (unlikely(!mapped_dst_nents)) {
-		dev_err(dev, "unable to map destination\n");
-		goto src_fail;
+
+	if (req_ctx->fixup_src == req->dst) {
+		shared_nents = sg_nents_for_len(req_ctx->fixup_src,
+						max(req_ctx->fixup_src_len,
+						    req->dst_len));
+		if (unlikely(shared_nents < 0)) {
+			dev_err(dev, "Invalid shared S/G extent\n");
+			return ERR_PTR(shared_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");
+			return ERR_PTR(-ENOMEM);
+		}
+		if (mapped_src_nents > 1) {
+			/*
+			 * Multi-segment aliases need separately bounded source
+			 * and destination hardware views. The current SEC4/PDB
+			 * layout cannot represent all such requests; defer support.
+			 */
+			dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
+				     DMA_BIDIRECTIONAL);
+			dev_err(dev, "Multi-segment shared S/G is not supported\n");
+			return ERR_PTR(-EOPNOTSUPP);
+		}
+		mapped_dst_nents = mapped_src_nents;
+	} else {
+		mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src, src_nents,
+					      DMA_TO_DEVICE);
+		if (unlikely(!mapped_src_nents)) {
+			dev_err(dev, "unable to map source\n");
+			return ERR_PTR(-ENOMEM);
+		}
+		mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
+					      DMA_FROM_DEVICE);
+		if (unlikely(!mapped_dst_nents)) {
+			dev_err(dev, "unable to map destination\n");
+			goto src_fail;
+		}
 	}
 
 	if (!diff_size && mapped_src_nents == 1)
@@ -338,6 +385,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;
 
@@ -365,6 +413,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);
+		return ERR_PTR(-ENOMEM);
+	}
 	dma_unmap_sg(dev, req->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 96d03704c9be..09b6aa5cee98 100644
--- a/drivers/crypto/caam/caampkc.h
+++ b/drivers/crypto/caam/caampkc.h
@@ -119,6 +119,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 count of a successful shared BIDIRECTIONAL mapping;
+ *                 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
@@ -131,6 +133,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


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

* Re: [RFC PATCH 0/1] crypto: caam - fix in-place RSA payload DMA mapping
  2026-10-20 19:25 [RFC PATCH 0/1] crypto: caam - fix in-place RSA payload DMA mapping Jeff Barnes
  2026-10-20 19:25 ` [RFC PATCH 1/1] " Jeff Barnes
@ 2026-11-10 23:19 ` Jeff Barnes
  1 sibling, 0 replies; 8+ messages in thread
From: Jeff Barnes @ 2026-11-10 23:19 UTC (permalink / raw)
  To: horia.geanta, pankaj.gupta, gaurav.jain, herbert, davem
  Cc: linux-crypto, linux-kernel

Gentle ping on this RFC.

I would appreciate any feedback on the DMA mapping approach, particularly whether a single DMA_BIDIRECTIONAL mapping is the right model for the exact source/destination alias case.

Thanks,
Jeff

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

* Re: [RFC PATCH 1/1] crypto: caam - fix in-place RSA payload DMA mapping
  2026-10-20 19:25 ` [RFC PATCH 1/1] " Jeff Barnes
@ 2026-11-11 10:54   ` Herbert Xu
  2026-11-19 19:53     ` Jeff Barnes
  2026-11-12  8:15   ` Sahil Malhotra
  1 sibling, 1 reply; 8+ messages in thread
From: Herbert Xu @ 2026-11-11 10:54 UTC (permalink / raw)
  To: Jeff Barnes
  Cc: horia.geanta, pankaj.gupta, gaurav.jain, davem, linux-crypto,
	linux-kernel, Sahil Malhotra

On Tue, Oct 20, 2026 at 03:25:11PM -0400, Jeff Barnes wrote:
> CAAM can receive RSA requests whose effective source and destination
> scatterlists are identical. The driver currently maps the source with
> DMA_TO_DEVICE and then maps the destination with DMA_FROM_DEVICE.

Sashiko has some comments on this patch:

https://sashiko.dev/#/patchset/20261020192511.3389133-1-jeffbarnes%40linux.microsoft.com

Please also explain how your patch interacts with the other
patches on caam:

https://patchwork.kernel.org/project/linux-crypto/list/?series=1189382

In any case, as I said in reply to the other patches, unless
the caam maintainers step up I think this should just be removed.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

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

* Re: [RFC PATCH 1/1] crypto: caam - fix in-place RSA payload DMA mapping
  2026-10-20 19:25 ` [RFC PATCH 1/1] " Jeff Barnes
  2026-11-11 10:54   ` Herbert Xu
@ 2026-11-12  8:15   ` Sahil Malhotra
  2026-11-13 20:46     ` Jeff Barnes
  1 sibling, 1 reply; 8+ messages in thread
From: Sahil Malhotra @ 2026-11-12  8:15 UTC (permalink / raw)
  To: Jeff Barnes
  Cc: herbert, davem, horia.geanta, pankaj.gupta, gaurav.jain,
	changwei.zou, linux-crypto, linux-kernel

On Tue, Oct 20, 2026 at 03:25:11PM -0400, Jeff Barnes wrote:
> CAAM can receive RSA requests whose effective source and destination
> scatterlists are identical. The driver currently maps the source with
> DMA_TO_DEVICE and then maps the destination with DMA_FROM_DEVICE.

Hi Jeff,

Thanks for the analysis, and sorry for the slow response.

To answer your first question: yes. When the effective source and
destination are the same scatterlist, I think a single DMA_BIDIRECTIONAL
mapping is the right model. The in-tree user that hits this is
rsassa-pkcs1 verify (crypto/rsassa-pkcs1.c), which calls
akcipher_request_set_crypt(child_req, &sg, &sg, slen, slen) with
slen == key size. That gives an exact, single-entry alias, which matches
the pkcs1(rsa-caam,sha256) failure you saw.

I have a few comments below.

[...]

> +	if (req_ctx->fixup_src == req->dst) {

This only detects the case where the two head pointers are equal. When
req->src_len > n_sz and leading zeros are stripped, fixup_src is the
result of scatterwalk_ffwd(). That is either req_ctx->src[] or a later
entry of req->src, so the comparison fails even when req->src == req->dst.
The same memory then still gets one TO_DEVICE and one FROM_DEVICE
mapping, through different struct scatterlist. The sg metadata is no
longer overwritten, but we still have two overlapping streaming mappings
with opposite directions, and CONFIG_DMA_API_DEBUG should complain about
that.

Could you either check req->src == req->dst (before the leading zeros are
stripped), or at least state in the commit message that only an exact
head alias is handled?

> +		shared_nents = sg_nents_for_len(req_ctx->fixup_src,
> +						max(req_ctx->fixup_src_len,
> +						    req->dst_len));

[...]

> +		if (mapped_src_nents > 1) {
> +			/*
> +			 * Multi-segment aliases need separately bounded source
> +			 * and destination hardware views. The current SEC4/PDB
> +			 * layout cannot represent all such requests; defer support.
> +			 */
> +			dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
> +				     DMA_BIDIRECTIONAL);
> +			dev_err(dev, "Multi-segment shared S/G is not supported\n");
> +			return ERR_PTR(-EOPNOTSUPP);
> +		}

I'm concerned this is a regression on systems where CAAM is not behind an
IOMMU, e.g. most i.MX8M parts, which use plain dma-direct. There both
dma_map_sg() calls return the same addresses, so as far as I can tell
in-place multi-entry requests work today. With this patch they would fail
with -EOPNOTSUPP, and the driver has no software fallback. The dev_err()
would also fire on every such request, and since a user can trigger
these requests, it should at least be rate-limited.

On your second question, I would prefer giving the request a separate
destination over growing separate source/destination SEC4 views. For the
multi-segment alias case: map the source DMA_TO_DEVICE, map a
driver-owned buffer DMA_FROM_DEVICE, and copy the result back on
completion. That keeps the SEC4/PDB layout unchanged.

Changwei's v4 2/2 ("Use bounce buffer for unaligned RSA destination
buffers") already adds most of this infrastructure:

  https://lore.kernel.org/linux-crypto/20261111230221.40297-3-changwei.zou@canonical.com/

so the alias case could simply be one more trigger for the bounce path.

On that point, the two patches rewrite the same block in
rsa_edesc_alloc() and will conflict. On top of Changwei's patch, the new
early "return ERR_PTR(...)" exits here (the sg_nents_for_len() < 0
checks, the shared map failure, and -EOPNOTSUPP) would leak
req_ctx->bounce_buf. They would need to go through his bounce_fail label.
Also, the sg_nents_for_len() checks have to run before his
for_each_sg(req->dst, sg, dst_nents, i) loop. Could the two of you agree
on an order, so that whichever lands second is rebased with this in mind?

> +	if (unlikely(src_nents < 0)) {
> +		dev_err(dev, "Insufficient bytes (%u) in src S/G\n",
> +			req_ctx->fixup_src_len);
> +		return ERR_PTR(src_nents);
> +	}

These checks are a good fix on their own: today a negative count goes
straight into dma_map_sg(). Could you split them into a separate patch?
That makes them easier to backport.

[...]

>  dst_fail:
> +	if (shared_nents) {
> +		dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
> +			     DMA_BIDIRECTIONAL);
> +		return ERR_PTR(-ENOMEM);
> +	}

Looks fine. The map/unmap counts and directions match on every path.

A couple of process points for the non-RFC version:

- The double mapping goes back to when RSA support was added, so please
  add:

    Fixes: 8c419778ab57 ("crypto: caam - add support for RSA algorithm")
    Cc: stable@vger.kernel.org

I can test the respin on i.MX8M (dma-direct, non-coherent) with
CONFIG_DMA_API_DEBUG=y.

Thanks,
Sahil

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

* Re: [RFC PATCH 1/1] crypto: caam - fix in-place RSA payload DMA mapping
  2026-11-12  8:15   ` Sahil Malhotra
@ 2026-11-13 20:46     ` Jeff Barnes
  2026-11-13 22:58       ` Changwei Zou
  0 siblings, 1 reply; 8+ messages in thread
From: Jeff Barnes @ 2026-11-13 20:46 UTC (permalink / raw)
  To: Sahil Malhotra
  Cc: herbert, davem, horia.geanta, pankaj.gupta, gaurav.jain,
	changwei.zou, linux-crypto, linux-kernel

Hi Sahil, Changwei,

Thanks for the review. 
                         
> Could you either check req->src == req->dst (before the leading zeros are
> stripped), or at least state in the commit message that only an exact
> head alias is handled?

Agreed. I'll detect the original req->src == req->dst alias before
leading-zero stripping and handle the advanced-source case through the
destination bounce path, rather than limit the fix to effective-head
equality.

> so the alias case could simply be one more trigger for the bounce path.

That makes sense. I'll use the bounce path for multi-segment aliases
instead of rejecting them with -EOPNOTSUPP or adding separate SEC4 views.

> Could the two of you agree on an order, so that whichever lands second
> is rebased with this in mind?

Changwei, would putting your series first work for you? I see v5 now
uses fixup_dst; I propose basing my respin on that infrastructure.
I'll ensure all failure paths release any allocated bounce buffer.

> Could you split them into a separate patch?
> That makes them easier to backport.

Yes, I'll split out the sg_nents_for_len() validation and place the
checks before destination traversal and bounce allocation.

I'll also add the requested Fixes and stable tags to the DMA alias fix.

Thanks for offering to test on i.MX8M with CONFIG_DMA_API_DEBUG.
I'll copy you on the respin and identify its prerequisite series.

Thanks,
Jeff


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

* Re: [RFC PATCH 1/1] crypto: caam - fix in-place RSA payload DMA mapping
  2026-11-13 20:46     ` Jeff Barnes
@ 2026-11-13 22:58       ` Changwei Zou
  0 siblings, 0 replies; 8+ messages in thread
From: Changwei Zou @ 2026-11-13 22:58 UTC (permalink / raw)
  To: jeffbarnes
  Cc: changwei.zou, davem, gaurav.jain, herbert, horia.geanta,
	linux-crypto, linux-kernel, pankaj.gupta, sahil.malhotra

Hi Jeff,

Yes. I agree with that.
Thank you very much.

Kind regards,
Changwei

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

* Re: [RFC PATCH 1/1] crypto: caam - fix in-place RSA payload DMA mapping
  2026-11-11 10:54   ` Herbert Xu
@ 2026-11-19 19:53     ` Jeff Barnes
  0 siblings, 0 replies; 8+ messages in thread
From: Jeff Barnes @ 2026-11-19 19:53 UTC (permalink / raw)
  To: herbert
  Cc: horia.geanta, pankaj.gupta, sahil.malhotra, changwei.zou,
	gaurav.jain, davem, linux-crypto, linux-kernel

Hi Herbert,

> Sashiko has some comments on this patch:

Thanks for pointing me to the review, and sorry for the delayed response.
The prepared v2 addresses two concerns, but the short-input SEC4/PDB
accounting concern remains. Here is the disposition of each finding:

1. PVF1: rejection of multi-segment in-place requests

   Agreed. The RFC's -EOPNOTSUPP containment is not suitable as the final
   fix. V2 removes it. An aligned exact alias retains one bidirectional
   mapping only when dma_map_sg() returns a single DMA segment.

   If it returns multiple segments, v2 releases that mapping using the
   original entry count, then maps the source DMA_TO_DEVICE and a separate
   destination bounce buffer DMA_FROM_DEVICE. This follows Sahil's
   recommendation and reuses Changwei's destination-bounce infrastructure.

   The padding/layout observations also included in PVF1 overlap PVF2
   below; I am not claiming those are fixed by destination bouncing.

2. PVF2: short-input padding and PDB source/table selection

   The source shows the accounting mismatch described in the review.
   The allocator reserves an extra SEC4 entry when diff_size is nonzero,
   but edesc->mapped_src_nents does not include that entry. The private
   PDB setters select a source S/G table only when that stored count is
   greater than one. Their destination-table offset also uses that count.

   I checked the unpatched baseline used by Sashiko,
   178aff4cf425b3fc51ff09c571fa1bfbc1eb8ea4. It already contains these
   allocation/count/selection rules, before applying this RFC. The issue
   is therefore not introduced by the shared-mapping change, but it
   remains present in the prepared v2 and needs separate treatment.

   In particular, fixing the private padded-input view and auditing the
   public input-length/table handling must not be conflated with simply
   changing payload DMA ownership. I have not hardware-reproduced or
   fixed this finding, and do not claim the public-operation tests below
   validate private signing or decryption.

3. PVF3: zero-length source passing the RFC's negative-only check

   Agreed. V2 patch 1 rejects src_nents <= 0 before payload mapping or
   SEC4 construction, returning -EINVAL for zero and propagating negative
   extent errors. It applies the same checks to the destination extent.
   A zero-length source therefore cannot reach the shared-mapping path
   merely because the destination extent is nonempty.

> Please also explain how your patch interacts with the other
> patches on caam:

The prepared v2 is based on both patches of Changwei's v6:

https://lore.kernel.org/linux-crypto/20261114060032.872141-1-changwei.zou@canonical.com/

His error-path fix provides the operation-appropriate unmap dispatch.
His alignment fix provides fixup_dst, destination allocation, and
copy-back/free handling. V2 reuses that infrastructure rather than
adding another destination-buffer implementation or modifying req->dst.
The payload is unmapped before destination copy-back.

The ordering is Changwei's two patches, then the separate SG-extent
validation patch, then the in-place DMA ownership patch. The latter also
records req->src == req->dst before leading-zero stripping and forces a
destination bounce if the effective source advances.

A backport of that combination was tested on an arm64 system with CAAM
behind an SMMUv2, a translated IOMMU domain, and DMA-API debugging
enabled, running kernel 6.6.160.4. All 12 RSA-2048 public-operation alias
tests passed, with no DMA-API, ARM-LPAE/IOMMU, or scatterlist-iterator
warnings observed.

Those tests used direct and backlog-enabled submission; they did not
establish that requests entered the backlog, test private operations, or
cover noncoherent/dma-direct hardware.

The test run also included the independent iterator-boundary correction
now submitted separately:

https://lore.kernel.org/linux-crypto/20261119192007.786827-1-jeffbarnes@linux.microsoft.com/

On the maintenance concern, Sahil has provided substantive review and
offered noncoherent/dma-direct testing. I have copied the CAAM maintainers,
but cannot speak for them regarding ongoing maintenance.

GitHub Copilot assisted with the implementation, regression tests,
source comparison, and drafting this response.

Thanks,
Jeff

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

end of thread, other threads:[~2026-11-19 19:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20 19:25 [RFC PATCH 0/1] crypto: caam - fix in-place RSA payload DMA mapping Jeff Barnes
2026-10-20 19:25 ` [RFC PATCH 1/1] " Jeff Barnes
2026-11-11 10:54   ` Herbert Xu
2026-11-19 19:53     ` Jeff Barnes
2026-11-12  8:15   ` Sahil Malhotra
2026-11-13 20:46     ` Jeff Barnes
2026-11-13 22:58       ` Changwei Zou
2026-11-10 23:19 ` [RFC PATCH 0/1] " Jeff Barnes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®