Skip to contentSkip to conversation

crypto: safexcel: fix AEAD DMA mapping and cleanup

PATCH In review Ralf Lici 2 participants 5 messages Dec 3

Subsystems (2)
lore
Share & tools
lore
author
#1 lore raw

PATCH v2 1/2 crypto: safexcel - Avoid unmapping failed DMA mappings

In reply to Ralf Lici

dma_map_sg undoes any partial mapping before returning zero. If mapping the destination of an out-of-place request fails, safexcel nevertheless reaches its common cleanup path with a positive destination SG count and calls dma_unmap_sg on the already-unmapped list.

Track which mappings completed successfully and restrict error cleanup to those lists.

Fixes 87e02063d077 ("crypto: safexcel - Add error handling for dma_map_sg() calls")

---
No changes since v1 https://lore.kernel.org/linux-crypto/88b0d054b0518f324f3d62ee7ffab589a61fcda6.1789469436.git.ralf@mandelbit.com/

 .../crypto/inside-secure/safexcel_cipher.c    | 36 ++++++++++++-------
 1 file changed, 23 insertions(+), 13 deletions(-)
drivers/crypto/inside-secure/safexcel_cipher.c +23 -13
index b031cb9652ec..b9d8f5179de6 100644
@@ -693,6 +693,7 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 	struct safexcel_token *atoken;
 	int n_cdesc = 0, n_rdesc = 0;
 	int queued, i, ret = 0;
+	bool src_mapped = false, dst_mapped = false;
 	bool first = true;
 
 	sreq->nr_src = sg_nents_for_len(src, totlen_src);
@@ -744,9 +745,12 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 				max(totlen_src, totlen_dst));
 			return -EINVAL;
 		}
-		if (sreq->nr_src > 0 &&
-		    !dma_map_sg(priv->dev, src, sreq->nr_src, DMA_BIDIRECTIONAL))
-			return -EIO;
+		if (sreq->nr_src > 0) {
+			src_mapped = dma_map_sg(priv->dev, src, sreq->nr_src,
+						DMA_BIDIRECTIONAL);
+			if (!src_mapped)
+				return -EIO;
+		}
 	} else {
 		if (unlikely(totlen_src && (sreq->nr_src <= 0))) {
 			dev_err(priv->dev, "Source buffer not large enough (need %d bytes)!",
@@ -754,9 +758,12 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 			return -EINVAL;
 		}
 
-		if (sreq->nr_src > 0 &&
-		    !dma_map_sg(priv->dev, src, sreq->nr_src, DMA_TO_DEVICE))
-			return -EIO;
+		if (sreq->nr_src > 0) {
+			src_mapped = dma_map_sg(priv->dev, src, sreq->nr_src,
+						DMA_TO_DEVICE);
+			if (!src_mapped)
+				return -EIO;
+		}
 
 		if (unlikely(totlen_dst && (sreq->nr_dst <= 0))) {
 			dev_err(priv->dev, "Dest buffer not large enough (need %d bytes)!",
@@ -765,10 +772,13 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 			goto unmap;
 		}
 
-		if (sreq->nr_dst > 0 &&
-		    !dma_map_sg(priv->dev, dst, sreq->nr_dst, DMA_FROM_DEVICE)) {
-			ret = -EIO;
-			goto unmap;
+		if (sreq->nr_dst > 0) {
+			dst_mapped = dma_map_sg(priv->dev, dst, sreq->nr_dst,
+						DMA_FROM_DEVICE);
+			if (!dst_mapped) {
+				ret = -EIO;
+				goto unmap;
+			}
 		}
 	}
 
@@ -901,14 +911,14 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 		safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);
 unmap:
 	if (src == dst) {
-		if (sreq->nr_src > 0)
+		if (src_mapped)
 			dma_unmap_sg(priv->dev, src, sreq->nr_src,
 				     DMA_BIDIRECTIONAL);
 	} else {
-		if (sreq->nr_src > 0)
+		if (src_mapped)
 			dma_unmap_sg(priv->dev, src, sreq->nr_src,
 				     DMA_TO_DEVICE);
-		if (sreq->nr_dst > 0)
+		if (dst_mapped)
 			dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
 				     DMA_FROM_DEVICE);
 	}
Signature · 1 line
2.56.1

Ralf Lici

author
#0 lore raw

v2 cover crypto: safexcel: fix AEAD DMA mapping and cleanup

Hi,

This series fixes two DMA mapping bugs in the safexcel AEAD path. The first avoids unmapping a destination list whose mapping failed. The second maps each AEAD scatterlist entry with the direction its contents need, which fixes silent output corruption on non-coherent systems.

In the v1 discussion, Herbert asked which caller is affected and whether mapping the destination DMA_BIDIRECTIONAL would be enough. The caller is in-tree ovpn: its in-place requests start with an 8-byte AAD entry that shares a cache line with the payload. Mapping that entry DMA_BIDIRECTIONAL, as the in-place path already does, is what corrupts the output. The entry is bounced through SWIOTLB, and copying it back at unmap dirties a stale cache line that the next unmap then writes over the accelerator output. Patch 2 has the details. This also corrects my earlier explanation: swiotlb fills the bounce buffer in every direction, so the problem is the copy-back through a stale cache line, not uninitialized bounce data.

I tested this on a GL.iNet GL-MT6000 (MT7986 EIP97, non-coherent Cortex-A53) with a matrix of AES-GCM requests in the ovpn in-place and fragmented out-of-place layouts plus linear ones, for encryption and decryption, at three data offsets and queue depths up to 400. The stock driver failed 27 of the 72 cases, all ovpn layouts, and this series fixes all of them.

Unfortunately no choice of direction can fix the case where two adjacent device-written entries share a cache line and the first is small enough to be bounced. While I have not seen this with ovpn or ESP, it might explain the kTLS failure noted in v1 (still unfixed by this series).

Changes since v1 https://lore.kernel.org/linux-crypto/[email protected]/
- Rebase onto crypto-2.6.
- Rewrite the commit message of patch 2 to explain why
DMA_BIDIRECTIONAL is not safe for small entries and to name the
in-tree caller. (Herbert)

Cheers!

Ralf Lici
Mandelbit Srl
---

Ralf Lici (2):
crypto: safexcel - Avoid unmapping failed DMA mappings
crypto: safexcel - Map AEAD buffers with accurate DMA directions

 .../crypto/inside-secure/safexcel_cipher.c    | 173 +++++++++++++++---
 1 file changed, 150 insertions(+), 23 deletions(-)

base-commit: 26ca5f34841f0a86d0883664a1c13be89a01d351

Signature · 1 line
2.56.1
author
#2 lore raw

PATCH v2 2/2 crypto: safexcel - Map AEAD buffers with accurate DMA directions

Safexcel maps every entry of an in-place AEAD request with DMA_BIDIRECTIONAL and every entry of an out-of-place destination with DMA_FROM_DEVICE, even though the associated data is never written and a decryption tag is input-only.

This is problematic on non-coherent systems because a small entry that is not mapped DMA_TO_DEVICE is bounced through SWIOTLB (see dma_kmalloc_needs_bounce), and unmapping it copies the bounce buffer back with the CPU. If the entry shares a cache line with output in a later entry, that copy dirties the stale cached line, and the cache maintenance when unmapping the later entry writes it back over the accelerator output. The request completes successfully, but its output is corrupted.

ovpn hits this in place. It stores a packet as [AAD][tag][payload] and describes it in logical order as [AAD][payload][tag], so the 8-byte AAD entry shares a cache line with the start of the payload. On an MT7986 EIP97 (non-coherent Cortex-A53), the first 40, 8 or 38 payload bytes were corrupted at data offsets 0, 32 and 66 modulo 128, even with a single request in flight. Fragmented out-of-place encryption failed the same way, also when the destination was mapped DMA_BIDIRECTIONAL.

Map AEAD entries separately according to their overlap with the logical output range. Use DMA_TO_DEVICE for input-only entries, DMA_FROM_DEVICE for pure out-of-place output entries, and DMA_BIDIRECTIONAL where an entry contains both kinds of data or is updated in place.

Fixes f6beaea30487 ("crypto: inside-secure - authenc(hmac(sha256), cbc(aes)) support")

---
Changes since v1 https://lore.kernel.org/linux-crypto/7bd0c89dd32e9712d7d08393842498698148064b.1789469436.git.ralf@mandelbit.com/
- Explain why DMA_BIDIRECTIONAL is not safe for small entries and name
the affected in-tree caller. (Herbert)
- Move the test results to the cover letter.

 .../crypto/inside-secure/safexcel_cipher.c    | 149 ++++++++++++++++--
 1 file changed, 133 insertions(+), 16 deletions(-)
drivers/crypto/inside-secure/safexcel_cipher.c +133 -16
index b9d8f5179de6..78f90ba6c136 100644
@@ -73,6 +73,8 @@ struct safexcel_cipher_req {
 	unsigned int rdescs;
 	bool needs_inv;
 	int  nr_src, nr_dst;
+	unsigned int output_offset;
+	unsigned int output_len;
 };
 
 static int safexcel_skcipher_iv(struct safexcel_cipher_ctx *ctx, u8 *iv,
@@ -606,6 +608,71 @@ static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
 	return 0;
 }
 
+/* classify each scatterlist entry by its overlap with the logical output */
+static enum dma_data_direction safexcel_aead_dma_dir(unsigned int offset,
+						     unsigned int len,
+						     unsigned int output_offset,
+						     unsigned int output_len,
+						     bool inplace)
+{
+	unsigned int end = offset + len;
+	unsigned int output_end = output_offset + output_len;
+
+	if (!output_len || offset >= output_end || output_offset >= end)
+		return DMA_TO_DEVICE;
+
+	if (inplace || offset < output_offset || end > output_end)
+		return DMA_BIDIRECTIONAL;
+
+	return DMA_FROM_DEVICE;
+}
+
+static void safexcel_unmap_aead(struct device *dev, struct scatterlist *sgl,
+				int nents, unsigned int output_offset,
+				unsigned int output_len, bool inplace)
+{
+	struct scatterlist *sg;
+	unsigned int offset = 0;
+	int i;
+
+	for_each_sg(sgl, sg, nents, i) {
+		enum dma_data_direction dir;
+
+		dir = safexcel_aead_dma_dir(offset, sg->length, output_offset,
+					    output_len, inplace);
+		dma_unmap_page(dev, sg_dma_address(sg), sg_dma_len(sg), dir);
+		offset += sg->length;
+	}
+}
+
+static int safexcel_map_aead(struct device *dev, struct scatterlist *sgl,
+			     int nents, unsigned int output_offset,
+			     unsigned int output_len, bool inplace)
+{
+	struct scatterlist *sg;
+	unsigned int offset = 0;
+	int i;
+
+	for_each_sg(sgl, sg, nents, i) {
+		enum dma_data_direction dir;
+
+		dir = safexcel_aead_dma_dir(offset, sg->length, output_offset,
+					    output_len, inplace);
+		sg_dma_address(sg) = dma_map_page(dev, sg_page(sg), sg->offset,
+						  sg->length, dir);
+		if (dma_mapping_error(dev, sg_dma_address(sg)))
+			goto err_unmap;
+		sg_dma_len(sg) = sg->length;
+		offset += sg->length;
+	}
+
+	return nents;
+
+err_unmap:
+	safexcel_unmap_aead(dev, sgl, i, output_offset, output_len, inplace);
+	return 0;
+}
+
 static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int ring,
 				      struct crypto_async_request *async,
 				      struct scatterlist *src,
@@ -643,16 +710,30 @@ static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int rin
 	safexcel_complete(priv, ring);
 
 	if (src == dst) {
-		if (sreq->nr_src > 0)
-			dma_unmap_sg(priv->dev, src, sreq->nr_src,
-				     DMA_BIDIRECTIONAL);
+		if (sreq->nr_src > 0) {
+			if (ctx->aead)
+				safexcel_unmap_aead(priv->dev, src,
+						    sreq->nr_src,
+						    sreq->output_offset,
+						    sreq->output_len, true);
+			else
+				dma_unmap_sg(priv->dev, src, sreq->nr_src,
+					     DMA_BIDIRECTIONAL);
+		}
 	} else {
 		if (sreq->nr_src > 0)
 			dma_unmap_sg(priv->dev, src, sreq->nr_src,
 				     DMA_TO_DEVICE);
-		if (sreq->nr_dst > 0)
-			dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
-				     DMA_FROM_DEVICE);
+		if (sreq->nr_dst > 0) {
+			if (ctx->aead)
+				safexcel_unmap_aead(priv->dev, dst,
+						    sreq->nr_dst,
+						    sreq->output_offset,
+						    sreq->output_len, false);
+			else
+				dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
+					     DMA_FROM_DEVICE);
+		}
 	}
 
 	/*
@@ -735,6 +816,10 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 	 */
 	totlen = totlen_src;
 	queued = totlen_src;
+	if (ctx->aead) {
+		sreq->output_offset = assoclen;
+		sreq->output_len = totlen_dst - assoclen;
+	}
 
 	if (src == dst) {
 		sreq->nr_src = max(sreq->nr_src, sreq->nr_dst);
@@ -746,8 +831,17 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 			return -EINVAL;
 		}
 		if (sreq->nr_src > 0) {
-			src_mapped = dma_map_sg(priv->dev, src, sreq->nr_src,
-						DMA_BIDIRECTIONAL);
+			if (ctx->aead)
+				src_mapped =
+					safexcel_map_aead(priv->dev, src,
+							  sreq->nr_src,
+							  sreq->output_offset,
+							  sreq->output_len,
+							  true);
+			else
+				src_mapped = dma_map_sg(priv->dev, src,
+							sreq->nr_src,
+							DMA_BIDIRECTIONAL);
 			if (!src_mapped)
 				return -EIO;
 		}
@@ -773,8 +867,17 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 		}
 
 		if (sreq->nr_dst > 0) {
-			dst_mapped = dma_map_sg(priv->dev, dst, sreq->nr_dst,
-						DMA_FROM_DEVICE);
+			if (ctx->aead)
+				dst_mapped =
+					safexcel_map_aead(priv->dev, dst,
+							  sreq->nr_dst,
+							  sreq->output_offset,
+							  sreq->output_len,
+							  false);
+			else
+				dst_mapped = dma_map_sg(priv->dev, dst,
+							sreq->nr_dst,
+							DMA_FROM_DEVICE);
 			if (!dst_mapped) {
 				ret = -EIO;
 				goto unmap;
@@ -911,16 +1014,30 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 		safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);
 unmap:
 	if (src == dst) {
-		if (src_mapped)
-			dma_unmap_sg(priv->dev, src, sreq->nr_src,
-				     DMA_BIDIRECTIONAL);
+		if (src_mapped) {
+			if (ctx->aead)
+				safexcel_unmap_aead(priv->dev, src,
+						    sreq->nr_src,
+						    sreq->output_offset,
+						    sreq->output_len, true);
+			else
+				dma_unmap_sg(priv->dev, src, sreq->nr_src,
+					     DMA_BIDIRECTIONAL);
+		}
 	} else {
 		if (src_mapped)
 			dma_unmap_sg(priv->dev, src, sreq->nr_src,
 				     DMA_TO_DEVICE);
-		if (dst_mapped)
-			dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
-				     DMA_FROM_DEVICE);
+		if (dst_mapped) {
+			if (ctx->aead)
+				safexcel_unmap_aead(priv->dev, dst,
+						    sreq->nr_dst,
+						    sreq->output_offset,
+						    sreq->output_len, false);
+			else
+				dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
+					     DMA_FROM_DEVICE);
+		}
 	}
 
 	return ret;
Signature · 1 line
2.56.1
#3 lore raw

Re: [PATCH crypto v2 2/2] crypto: safexcel - Map AEAD buffers with accurate DMA directions

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] crypto: safexcel: per-entry AEAD DMA mapping regresses coherent/IOMMU paths - [Medium] crypto…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] crypto: safexcel: per-entry AEAD DMA mapping regresses coherent/IOMMU paths
- [Medium] crypto: safexcel: safexcel_aead_dma_dir() over-maps out-of-place dst buffers
- [Low] crypto: safexcel: duplicated DMA unmap dispatch in cipher req paths
--

commit 0fc5903031a933ef50a45baa1e600103db377d61
Author: Ralf Lici <[email protected]>

crypto: safexcel - Map AEAD buffers with accurate DMA directions

This commit replaces batched dma_map_sg() and dma_unmap_sg() calls on AEAD scatterlists with per-entry dma_map_page() and dma_unmap_page() helpers that select the DMA direction from each entry's overlap with the logical output range.

Quoted 4 lines
drivers/crypto/inside-secure/safexcel_cipher.c +0 -0
index b9d8f5179de6c..78f90ba6c1365 100644

[ ... ]

Quoted 12 lines
drivers/crypto/inside-secure/safexcel_cipher.c +8 -0
@@ -606,6 +608,71 @@ static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
 	return 0;
 }
 
+/* classify each scatterlist entry by its overlap with the logical output */
+static enum dma_data_direction safexcel_aead_dma_dir(unsigned int offset,
+						     unsigned int len,
+						     unsigned int output_offset,
+						     unsigned int output_len,
+						     bool inplace)
+{
+	unsigned int end = offset + len;

drivers/crypto/inside-secure/safexcel_cipher.c static int safexcel_context_control(struct safexcel_cipher_ctx *ctx, · line 628

Quoted 10 lines
drivers/crypto/inside-secure/safexcel_cipher.c +10 -0
+	unsigned int output_end = output_offset + output_len;
+
+	if (!output_len || offset >= output_end || output_offset >= end)
+		return DMA_TO_DEVICE;
+
+	if (inplace || offset < output_offset || end > output_end)
+		return DMA_BIDIRECTIONAL;
+
+	return DMA_FROM_DEVICE;
+}

[Severity: Medium]
Should safexcel_aead_dma_dir() map out-of-place destination entries with DMA_TO_DEVICE or DMA_BIDIRECTIONAL when the hardware never reads dst?

When inplace is false, safexcel_send_req() skips pure-AAD dst entries in the result descriptor loop without using their DMA addresses. Mapping and unmapping those entries with DMA_TO_DEVICE adds unnecessary cache maintenance and barriers on non-coherent ARM64 and redundant IOMMU mappings on hot out-of-place paths such as kTLS.

Mapping uninitialized out-of-place dst buffers with DMA_TO_DEVICE or DMA_BIDIRECTIONAL also triggers kmsan_internal_check_memory() under KMSAN, regressing callers like generate_aead_message() in crypto/testmgr.c that pass uninitialized kmalloc() buffers. Skipping non-overlapping out-of-place entries (while still setting sg_dma_len()) and mapping only the written sub-range of partial-overlap entries with DMA_FROM_DEVICE would avoid both issues.

Quoted 35 lines
drivers/crypto/inside-secure/safexcel_cipher.c +35 -0
+static void safexcel_unmap_aead(struct device *dev, struct scatterlist *sgl,
+				int nents, unsigned int output_offset,
+				unsigned int output_len, bool inplace)
+{
+	struct scatterlist *sg;
+	unsigned int offset = 0;
+	int i;
+
+	for_each_sg(sgl, sg, nents, i) {
+		enum dma_data_direction dir;
+
+		dir = safexcel_aead_dma_dir(offset, sg->length, output_offset,
+					    output_len, inplace);
+		dma_unmap_page(dev, sg_dma_address(sg), sg_dma_len(sg), dir);
+		offset += sg->length;
+	}
+}
+
+static int safexcel_map_aead(struct device *dev, struct scatterlist *sgl,
+			     int nents, unsigned int output_offset,
+			     unsigned int output_len, bool inplace)
+{
+	struct scatterlist *sg;
+	unsigned int offset = 0;
+	int i;
+
+	for_each_sg(sgl, sg, nents, i) {
+		enum dma_data_direction dir;
+
+		dir = safexcel_aead_dma_dir(offset, sg->length, output_offset,
+					    output_len, inplace);
+		sg_dma_address(sg) = dma_map_page(dev, sg_page(sg), sg->offset,
+						  sg->length, dir);
+		if (dma_mapping_error(dev, sg_dma_address(sg)))
+			goto err_unmap;

drivers/crypto/inside-secure/safexcel_cipher.c static int safexcel_context_control(struct safexcel_cipher_ctx *ctx, · line 674

Quoted 10 lines
drivers/crypto/inside-secure/safexcel_cipher.c +10 -0
+		sg_dma_len(sg) = sg->length;
+		offset += sg->length;
+	}
+
+	return nents;
+
+err_unmap:
+	safexcel_unmap_aead(dev, sgl, i, output_offset, output_len, inplace);
+	return 0;
+}

[Severity: Medium]
Does unconditionally replacing dma_map_sg() and dma_unmap_sg() with per-entry dma_map_page() and dma_unmap_page() regress AEAD throughput on DMA-coherent and IOMMU-backed devices?

dma_kmalloc_safe() returns true when dev_is_dma_coherent(dev) is set, so coherent platforms do not bounce through SWIOTLB or hit the non-coherent cacheline corruption. On IOMMU-backed safexcel devices (such as PCIe EIP197 boards or coherent Armada CP11x), per-entry calls lose batched IOVA allocation and segment coalescing and perform an iommu_iova_to_phys() walk plus a separate __iommu_dma_unmap() per entry.

On non-coherent ARM64 dma-direct platforms, per-entry dma_map_page() and dma_unmap_page() also execute arch_sync_dma_flush() (dsb(sy)) on every entry, including DMA_TO_DEVICE unmaps, instead of once per scatterlist. Gating the per-entry path on !dev_is_dma_coherent(priv->dev) would preserve batched dma_map_sg() performance on coherent and IOMMU-backed devices.

[ ... ]

Quoted 25 lines
drivers/crypto/inside-secure/safexcel_cipher.c +11 -6
@@ -911,16 +1014,30 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 		safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);
 unmap:
 	if (src == dst) {
-		if (src_mapped)
-			dma_unmap_sg(priv->dev, src, sreq->nr_src,
-				     DMA_BIDIRECTIONAL);
+		if (src_mapped) {
+			if (ctx->aead)
+				safexcel_unmap_aead(priv->dev, src,
+						    sreq->nr_src,
+						    sreq->output_offset,
+						    sreq->output_len, true);
+			else
+				dma_unmap_sg(priv->dev, src, sreq->nr_src,
+					     DMA_BIDIRECTIONAL);
+		}
 	} else {
 		if (src_mapped)
 			dma_unmap_sg(priv->dev, src, sreq->nr_src,
 				     DMA_TO_DEVICE);
-		if (dst_mapped)
-			dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
-				     DMA_FROM_DEVICE);
+		if (dst_mapped) {

drivers/crypto/inside-secure/safexcel_cipher.c static int safexcel_send_req(struct crypto_async_request *base, int ring, · line 1041

Quoted 10 lines
drivers/crypto/inside-secure/safexcel_cipher.c +9 -0
+			if (ctx->aead)
+				safexcel_unmap_aead(priv->dev, dst,
+						    sreq->nr_dst,
+						    sreq->output_offset,
+						    sreq->output_len, false);
+			else
+				dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
+					     DMA_FROM_DEVICE);
+		}
 	}

[Severity: Low]
This isn't a bug, but this unmap block duplicates the same nested if (src == dst) / if (ctx->aead) dispatch in safexcel_handle_req_result(), differing only in checking src_mapped/dst_mapped versus sreq->nr_src > 0 / sreq->nr_dst > 0. Extracting a shared helper would avoid keeping both copies in sync.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#4 lore raw

Re: [PATCH crypto v2 2/2] crypto: safexcel - Map AEAD buffers with accurate DMA directions

Quoted 33 lines · On Thu, 03 Dec 2026 09:09:12 +0000, [email protected] wrote:
Quoted 12 lines
drivers/crypto/inside-secure/safexcel_cipher.c +8 -0
@@ -606,6 +608,71 @@ static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
 	return 0;
 }
 
+/* classify each scatterlist entry by its overlap with the logical output */
+static enum dma_data_direction safexcel_aead_dma_dir(unsigned int offset,
+						     unsigned int len,
+						     unsigned int output_offset,
+						     unsigned int output_len,
+						     bool inplace)
+{
+	unsigned int end = offset + len;

drivers/crypto/inside-secure/safexcel_cipher.c static int safexcel_context_control(struct safexcel_cipher_ctx *ctx, · line 628

Quoted 10 lines
drivers/crypto/inside-secure/safexcel_cipher.c +10 -0
+	unsigned int output_end = output_offset + output_len;
+
+	if (!output_len || offset >= output_end || output_offset >= end)
+		return DMA_TO_DEVICE;
+
+	if (inplace || offset < output_offset || end > output_end)
+		return DMA_BIDIRECTIONAL;
+
+	return DMA_FROM_DEVICE;
+}

[Severity: Medium]
Should safexcel_aead_dma_dir() map out-of-place destination entries with DMA_TO_DEVICE or DMA_BIDIRECTIONAL when the hardware never reads dst?

When inplace is false, safexcel_send_req() skips pure-AAD dst entries in the result descriptor loop without using their DMA addresses. Mapping and unmapping those entries with DMA_TO_DEVICE adds unnecessary cache maintenance and barriers on non-coherent ARM64 and redundant IOMMU mappings on hot out-of-place paths such as kTLS.

Quoted 8 lines

Mapping uninitialized out-of-place dst buffers with DMA_TO_DEVICE or DMA_BIDIRECTIONAL also triggers kmsan_internal_check_memory() under KMSAN, regressing callers like generate_aead_message() in crypto/testmgr.c that pass uninitialized kmalloc() buffers. Skipping non-overlapping out-of-place entries (while still setting sg_dma_len()) and mapping only the written sub-range of partial-overlap entries with DMA_FROM_DEVICE would avoid both issues.

The extra cache maintenance is not new, since the stock driver already maps the reserved dst AAD (with DMA_FROM_DEVICE). The testmgr example does not apply either, as generate_aead_message runs on the generic implementation and testmgr poisons its destination buffers.

The KMSAN concern OTOH does hold for mixed out-of-place dst entries, which are now mapped DMA_BIDIRECTIONAL while their output part is usually uninitialized. Since the engine skips the dst AAD anyway, the cleaner option would be to fast-forward dst past assoclen and map only the output, DMA_FROM_DEVICE.

Quoted 58 lines
Quoted 35 lines
drivers/crypto/inside-secure/safexcel_cipher.c +35 -0
+static void safexcel_unmap_aead(struct device *dev, struct scatterlist *sgl,
+				int nents, unsigned int output_offset,
+				unsigned int output_len, bool inplace)
+{
+	struct scatterlist *sg;
+	unsigned int offset = 0;
+	int i;
+
+	for_each_sg(sgl, sg, nents, i) {
+		enum dma_data_direction dir;
+
+		dir = safexcel_aead_dma_dir(offset, sg->length, output_offset,
+					    output_len, inplace);
+		dma_unmap_page(dev, sg_dma_address(sg), sg_dma_len(sg), dir);
+		offset += sg->length;
+	}
+}
+
+static int safexcel_map_aead(struct device *dev, struct scatterlist *sgl,
+			     int nents, unsigned int output_offset,
+			     unsigned int output_len, bool inplace)
+{
+	struct scatterlist *sg;
+	unsigned int offset = 0;
+	int i;
+
+	for_each_sg(sgl, sg, nents, i) {
+		enum dma_data_direction dir;
+
+		dir = safexcel_aead_dma_dir(offset, sg->length, output_offset,
+					    output_len, inplace);
+		sg_dma_address(sg) = dma_map_page(dev, sg_page(sg), sg->offset,
+						  sg->length, dir);
+		if (dma_mapping_error(dev, sg_dma_address(sg)))
+			goto err_unmap;

drivers/crypto/inside-secure/safexcel_cipher.c static int safexcel_context_control(struct safexcel_cipher_ctx *ctx, · line 674

Quoted 10 lines
drivers/crypto/inside-secure/safexcel_cipher.c +10 -0
+		sg_dma_len(sg) = sg->length;
+		offset += sg->length;
+	}
+
+	return nents;
+
+err_unmap:
+	safexcel_unmap_aead(dev, sgl, i, output_offset, output_len, inplace);
+	return 0;
+}

[Severity: Medium]
Does unconditionally replacing dma_map_sg() and dma_unmap_sg() with per-entry dma_map_page() and dma_unmap_page() regress AEAD throughput on DMA-coherent and IOMMU-backed devices?

dma_kmalloc_safe() returns true when dev_is_dma_coherent(dev) is set, so coherent platforms do not bounce through SWIOTLB or hit the non-coherent cacheline corruption. On IOMMU-backed safexcel devices (such as PCIe EIP197 boards or coherent Armada CP11x), per-entry calls lose batched IOVA allocation and segment coalescing and perform an iommu_iova_to_phys() walk plus a separate __iommu_dma_unmap() per entry.

Quoted 7 lines

On non-coherent ARM64 dma-direct platforms, per-entry dma_map_page() and dma_unmap_page() also execute arch_sync_dma_flush() (dsb(sy)) on every entry, including DMA_TO_DEVICE unmaps, instead of once per scatterlist. Gating the per-entry path on !dev_is_dma_coherent(priv->dev) would preserve batched dma_map_sg() performance on coherent and IOMMU-backed devices.

Coherent devices never hit the bug, so the per-entry path could be limited to non-coherent devices. The extra barrier per entry is real since v7.1, but small next to the per-request cost of the EIP97.

Quoted 34 lines
Quoted 34 lines
drivers/crypto/inside-secure/safexcel_cipher.c +20 -6
@@ -911,16 +1014,30 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
 		safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);
 unmap:
 	if (src == dst) {
-		if (src_mapped)
-			dma_unmap_sg(priv->dev, src, sreq->nr_src,
-				     DMA_BIDIRECTIONAL);
+		if (src_mapped) {
+			if (ctx->aead)
+				safexcel_unmap_aead(priv->dev, src,
+						    sreq->nr_src,
+						    sreq->output_offset,
+						    sreq->output_len, true);
+			else
+				dma_unmap_sg(priv->dev, src, sreq->nr_src,
+					     DMA_BIDIRECTIONAL);
+		}
 	} else {
 		if (src_mapped)
 			dma_unmap_sg(priv->dev, src, sreq->nr_src,
 				     DMA_TO_DEVICE);
-		if (dst_mapped)
-			dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
-				     DMA_FROM_DEVICE);
+		if (dst_mapped) {
+			if (ctx->aead)
+				safexcel_unmap_aead(priv->dev, dst,
+						    sreq->nr_dst,
+						    sreq->output_offset,
+						    sreq->output_len, false);
+			else
+				dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
+					     DMA_FROM_DEVICE);
+		}
Quoted 9 lines

Review static int safexcel_send_req(struct crypto_async_request *base, int ring, · line 1041

Quoted 1 line
(quoted diff) +0 -0
 	}

[Severity: Low]
This isn't a bug, but this unmap block duplicates the same nested if (src == dst) / if (ctx->aead) dispatch in safexcel_handle_req_result(), differing only in checking src_mapped/dst_mapped versus sreq->nr_src > 0 / sreq->nr_dst > 0. Extracting a shared helper would avoid keeping both copies in sync.

The two unmap paths were already duplicated before this patch, as are other hunks in the driver, so I kept the fix consistent with the existing style rather than refactoring in a patch meant for stable.

Addressing the first two findings would make the patch larger rather than smaller, which goes the opposite way of the simpler approach Herbert suggested in v1. So I'd rather wait for his opinion on the overall approach before reworking it.

Herbert, would you like these changes in a v3, or do you prefer to keep the patch as it is?

Signature · 2 lines
Ralf Lici
Mandelbit Srl