[PATCH crypto 1/2] crypto: safexcel - Avoid unmapping failed DMA mappings
From: Ralf Lici
Date: Tue Sep 15 2026 - 08:15:01 EST
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@xxxxxxxxxxxxx>
---
.../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 a8349b684693..4982c64bf236 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -695,6 +695,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);
@@ -746,9 +747,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)!",
@@ -756,9 +760,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)!",
@@ -767,10 +774,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;
+ }
}
}
@@ -903,14 +913,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.55.0