mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH crypto v2 0/2] crypto: safexcel: fix AEAD DMA mapping and cleanup
@ 2026-12-03  8:40 Ralf Lici
  2026-12-03  8:40 ` [PATCH crypto v2 1/2] crypto: safexcel - Avoid unmapping failed DMA mappings Ralf Lici
  2026-12-03  8:40 ` [PATCH crypto v2 2/2] crypto: safexcel - Map AEAD buffers with accurate DMA directions Ralf Lici
  0 siblings, 2 replies; 3+ messages in thread
From: Ralf Lici @ 2026-12-03  8:40 UTC (permalink / raw)
  To: linux-crypto
  Cc: Antoine Tenart, Herbert Xu, David S. Miller, Nikita Zhandarovich,
	Antonio Quartulli, linux-kernel

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/cover.1789469436.git.ralf@mandelbit.com/
- 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
-- 
2.56.1


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

* [PATCH crypto v2 1/2] crypto: safexcel - Avoid unmapping failed DMA mappings
  2026-12-03  8:40 [PATCH crypto v2 0/2] crypto: safexcel: fix AEAD DMA mapping and cleanup Ralf Lici
@ 2026-12-03  8:40 ` Ralf Lici
  2026-12-03  8:40 ` [PATCH crypto v2 2/2] crypto: safexcel - Map AEAD buffers with accurate DMA directions Ralf Lici
  1 sibling, 0 replies; 3+ messages in thread
From: Ralf Lici @ 2026-12-03  8:40 UTC (permalink / raw)
  To: linux-crypto
  Cc: Antoine Tenart, Herbert Xu, David S. Miller, Nikita Zhandarovich,
	Antonio Quartulli, linux-kernel

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")
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
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(-)

diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c b/drivers/crypto/inside-secure/safexcel_cipher.c
index b031cb9652ec..b9d8f5179de6 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -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);
 	}
-- 
2.56.1


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

* [PATCH crypto v2 2/2] crypto: safexcel - Map AEAD buffers with accurate DMA directions
  2026-12-03  8:40 [PATCH crypto v2 0/2] crypto: safexcel: fix AEAD DMA mapping and cleanup Ralf Lici
  2026-12-03  8:40 ` [PATCH crypto v2 1/2] crypto: safexcel - Avoid unmapping failed DMA mappings Ralf Lici
@ 2026-12-03  8:40 ` Ralf Lici
  1 sibling, 0 replies; 3+ messages in thread
From: Ralf Lici @ 2026-12-03  8:40 UTC (permalink / raw)
  To: linux-crypto
  Cc: Antoine Tenart, Herbert Xu, David S. Miller, Nikita Zhandarovich,
	Antonio Quartulli, linux-kernel

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")
Signed-off-by: Ralf Lici <ralf@mandelbit.com>
---
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(-)

diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c b/drivers/crypto/inside-secure/safexcel_cipher.c
index b9d8f5179de6..78f90ba6c136 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -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;
-- 
2.56.1


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

end of thread, other threads:[~2026-12-03  8:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  8:40 [PATCH crypto v2 0/2] crypto: safexcel: fix AEAD DMA mapping and cleanup Ralf Lici
2026-12-03  8:40 ` [PATCH crypto v2 1/2] crypto: safexcel - Avoid unmapping failed DMA mappings Ralf Lici
2026-12-03  8:40 ` [PATCH crypto v2 2/2] crypto: safexcel - Map AEAD buffers with accurate DMA directions Ralf Lici

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®