From: Rosen Penev <rosenp@gmail.com>
To: linux-crypto@vger.kernel.org
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
"David S. Miller" <davem@davemloft.net>,
James Hsiao <jhsiao@amcc.com>, Rosen Penev <rosenp@gmail.com>,
Christian Lamparter <chunkeey@gmail.com>
Subject: [PATCH 6/8] crypto: amcc - fix the DMA mappings of the source and destination
Date: Thu, 26 Nov 2026 20:04:21 -0800 [thread overview]
Message-ID: <20261127040423.1023233-7-rosenp@gmail.com> (raw)
In-Reply-To: <20261127040423.1023233-1-rosenp@gmail.com>
crypto4xx_build_pd() maps every source segment with dma_map_page(),
but the mappings are never unmapped. The destination is mapped
DMA_TO_DEVICE and unmapped DMA_FROM_DEVICE with a different length,
and none of the mappings are checked for errors. The non-coherent
4xx DMA ops make this mostly harmless today, but it leaks mappings
on any setup that tracks them, and CONFIG_DMA_API_DEBUG reports all
of it.
Map the source with dma_map_sg() and the destination with
DMA_FROM_DEVICE before any ring descriptor is reserved, so a mapping
failure has nothing to give back, and record the mappings in the
pd_uinfo so the completion path unmaps exactly what was mapped. The
busy and ring-full error paths unmap them again.
A request with no data now completes right away, as there is nothing
to map.
Fixes: 049359d65527 ("crypto: amcc - Add crypt4xx driver")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/amcc/crypto4xx_core.c | 122 +++++++++++++++++----------
drivers/crypto/amcc/crypto4xx_core.h | 4 +
2 files changed, 81 insertions(+), 45 deletions(-)
diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index cb9b36d6f4fc..4c1b76f28de0 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -482,6 +482,23 @@ static void crypto4xx_copy_pkt_to_dst(struct crypto4xx_device *dev,
}
}
+static void crypto4xx_unmap(struct crypto4xx_device *dev,
+ struct scatterlist *src, int src_nents,
+ dma_addr_t dst_dma, u32 dst_len)
+{
+ if (dst_len)
+ dma_unmap_page(dev->core_dev->device, dst_dma, dst_len,
+ DMA_FROM_DEVICE);
+ dma_unmap_sg(dev->core_dev->device, src, src_nents, DMA_TO_DEVICE);
+}
+
+static void crypto4xx_unmap_pd(struct crypto4xx_device *dev,
+ struct pd_uinfo *pd_uinfo)
+{
+ crypto4xx_unmap(dev, pd_uinfo->src_va, pd_uinfo->src_nents,
+ pd_uinfo->dst_dma, pd_uinfo->dst_len);
+}
+
static void crypto4xx_ret_sg_desc(struct crypto4xx_device *dev,
struct pd_uinfo *pd_uinfo)
{
@@ -506,18 +523,14 @@ static void crypto4xx_cipher_done(struct crypto4xx_device *dev,
struct ce_pd *pd)
{
struct skcipher_request *req;
- struct scatterlist *dst;
req = skcipher_request_cast(pd_uinfo->async_req);
- if (pd_uinfo->sa_va->sa_command_0.bf.scatter) {
+ crypto4xx_unmap_pd(dev, pd_uinfo);
+
+ if (pd_uinfo->sa_va->sa_command_0.bf.scatter)
crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
req->cryptlen, req->dst);
- } else {
- dst = pd_uinfo->dest_va;
- dma_unmap_page(dev->core_dev->device, pd->dest, dst->length,
- DMA_FROM_DEVICE);
- }
if (pd_uinfo->sa_va->sa_command_0.bf.save_iv == SA_SAVE_IV) {
struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
@@ -546,14 +559,12 @@ static void crypto4xx_aead_done(struct crypto4xx_device *dev,
u32 icv[AES_BLOCK_SIZE];
int err = 0;
- if (pd_uinfo->sa_va->sa_command_0.bf.scatter) {
+ crypto4xx_unmap_pd(dev, pd_uinfo);
+
+ if (pd_uinfo->sa_va->sa_command_0.bf.scatter)
crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
pd->pd_ctl_len.bf.pkt_len,
dst);
- } else {
- dma_unmap_page(dev->core_dev->device, pd->dest, dst->length,
- DMA_FROM_DEVICE);
- }
if (pd_uinfo->sa_va->sa_command_0.bf.dir == DIR_OUTBOUND) {
/* append icv at the end */
@@ -660,7 +671,9 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
unsigned int nbytes = datalen;
size_t offset_to_sr_ptr;
u32 gd_idx = 0;
- int tmp;
+ dma_addr_t dst_dma = 0;
+ u32 dst_len = 0;
+ int src_nents, nmapped, i, rc;
bool is_busy, force_sd;
/*
@@ -679,15 +692,16 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
|| req_sa->sa_command_1.bf.crypto_mode9_8 == CRYPTO_MODE_OFB)
&& (datalen % AES_BLOCK_SIZE);
+ /* Nothing to process; there is nothing to map either. */
+ if (!(assoclen + datalen))
+ return 0;
+
/* figure how many gd are needed */
- tmp = sg_nents_for_len(src, assoclen + datalen);
- if (tmp < 0) {
+ src_nents = sg_nents_for_len(src, assoclen + datalen);
+ if (src_nents < 0) {
dev_err(dev->core_dev->device, "Invalid number of src SG.\n");
- return tmp;
+ return src_nents;
}
- if (tmp == 1)
- tmp = 0;
- num_gd = tmp;
if (assoclen) {
nbytes += assoclen;
@@ -707,6 +721,27 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
}
}
+ /*
+ * Map the buffers before reserving any descriptors, so a mapping
+ * failure has nothing to give back to the rings.
+ */
+ nmapped = dma_map_sg(dev->core_dev->device, src, src_nents,
+ DMA_TO_DEVICE);
+ if (!nmapped)
+ return -ENOMEM;
+ num_gd = nmapped == 1 ? 0 : nmapped;
+
+ if (!num_sd) {
+ dst_len = min(datalen, dst->length);
+ dst_dma = dma_map_page(dev->core_dev->device, sg_page(dst),
+ dst->offset, dst_len, DMA_FROM_DEVICE);
+ if (dma_mapping_error(dev->core_dev->device, dst_dma)) {
+ dma_unmap_sg(dev->core_dev->device, src, src_nents,
+ DMA_TO_DEVICE);
+ return -ENOMEM;
+ }
+ }
+
/*
* The follow section of code needs to be protected
* The gather ring and scatter ring needs to be consecutive
@@ -734,25 +769,23 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
((PPC4XX_NUM_PD * 15) / 16);
if (is_busy) {
- spin_unlock_irqrestore(&dev->core_dev->lock, flags);
- return -EBUSY;
+ rc = -EBUSY;
+ goto err_unlock;
}
}
+ rc = -EAGAIN;
if (num_gd) {
fst_gd = crypto4xx_get_n_gd(dev, num_gd);
- if (fst_gd == ERING_WAS_FULL) {
- spin_unlock_irqrestore(&dev->core_dev->lock, flags);
- return -EAGAIN;
- }
+ if (fst_gd == ERING_WAS_FULL)
+ goto err_unlock;
}
if (num_sd) {
fst_sd = crypto4xx_get_n_sd(dev, num_sd);
if (fst_sd == ERING_WAS_FULL) {
if (num_gd)
dev->gdr_head = fst_gd;
- spin_unlock_irqrestore(&dev->core_dev->lock, flags);
- return -EAGAIN;
+ goto err_unlock;
}
}
pd_entry = crypto4xx_get_pd_from_pdr_nolock(dev);
@@ -761,8 +794,7 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
dev->gdr_head = fst_gd;
if (num_sd)
dev->sdr_head = fst_sd;
- spin_unlock_irqrestore(&dev->core_dev->lock, flags);
- return -EAGAIN;
+ goto err_unlock;
}
spin_unlock_irqrestore(&dev->core_dev->lock, flags);
@@ -779,6 +811,10 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
pd_uinfo->num_gd = num_gd;
pd_uinfo->num_sd = num_sd;
pd_uinfo->dest_va = dst;
+ pd_uinfo->src_va = src;
+ pd_uinfo->src_nents = src_nents;
+ pd_uinfo->dst_dma = dst_dma;
+ pd_uinfo->dst_len = dst_len;
pd_uinfo->async_req = req;
if (iv_len)
@@ -805,28 +841,22 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
sa->sa_command_0.bf.gather = 1;
/* walk the sg, and setup gather array */
- sg = src;
- while (nbytes) {
- size_t len;
+ for_each_sg(src, sg, nmapped, i) {
+ u32 len = min(sg_dma_len(sg), nbytes);
- len = min(sg->length, nbytes);
- gd->ptr = dma_map_page(dev->core_dev->device,
- sg_page(sg), sg->offset, len, DMA_TO_DEVICE);
+ gd->ptr = sg_dma_address(sg);
gd->ctl_len.len = len;
gd->ctl_len.done = 0;
gd->ctl_len.ready = 1;
- if (len >= nbytes)
+ nbytes -= len;
+ if (!nbytes)
break;
- nbytes -= sg->length;
gd_idx = get_next_gd(gd_idx);
gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
- sg = sg_next(sg);
}
} else {
- pd->src = (u32)dma_map_page(dev->core_dev->device, sg_page(src),
- src->offset, min(nbytes, src->length),
- DMA_TO_DEVICE);
+ pd->src = sg_dma_address(src);
/*
* Disable gather in sa command
*/
@@ -843,10 +873,7 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
*/
pd_uinfo->first_sd = 0xffffffff;
sa->sa_command_0.bf.scatter = 0;
- pd->dest = (u32)dma_map_page(dev->core_dev->device,
- sg_page(dst), dst->offset,
- min(datalen, dst->length),
- DMA_TO_DEVICE);
+ pd->dest = dst_dma;
} else {
dma_addr_t sd_dma;
struct ce_sd *sd = NULL;
@@ -904,6 +931,11 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
writel(0, dev->ce_base + CRYPTO4XX_INT_DESCR_RD);
writel(1, dev->ce_base + CRYPTO4XX_INT_DESCR_RD);
return is_busy ? -EBUSY : -EINPROGRESS;
+
+err_unlock:
+ spin_unlock_irqrestore(&dev->core_dev->lock, flags);
+ crypto4xx_unmap(dev, src, src_nents, dst_dma, dst_len);
+ return rc;
}
/*
diff --git a/drivers/crypto/amcc/crypto4xx_core.h b/drivers/crypto/amcc/crypto4xx_core.h
index 294a57b77733..cdd40bb7d254 100644
--- a/drivers/crypto/amcc/crypto4xx_core.h
+++ b/drivers/crypto/amcc/crypto4xx_core.h
@@ -65,6 +65,10 @@ struct pd_uinfo {
struct sa_state_record *sr_va; /* state record for shadow sa */
u32 sr_pa;
struct scatterlist *dest_va;
+ struct scatterlist *src_va; /* DMA mapped source */
+ int src_nents;
+ dma_addr_t dst_dma; /* DMA mapped destination */
+ u32 dst_len; /* 0 if the scatter ring is used */
struct crypto_async_request *async_req; /* base crypto request
for this packet */
};
--
2.56.1
next prev parent reply other threads:[~2026-11-27 4:04 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 4:04 [PATCH 0/8] crypto: amcc - fixes for the crypto4xx driver Rosen Penev
2026-11-27 4:04 ` [PATCH 1/8] crypto: amcc - fix scatter ring full check Rosen Penev
2026-11-27 4:04 ` [PATCH 2/8] crypto: amcc - fix missing DMA memory barriers in descriptor handling Rosen Penev
2026-11-27 4:04 ` [PATCH 3/8] crypto: amcc - allocate all algorithm wrappers before registering Rosen Penev
2026-11-27 4:04 ` [PATCH 4/8] crypto: amcc - set up the TRNG before registering the algorithms Rosen Penev
2026-11-27 4:04 ` [PATCH 5/8] crypto: amcc - don't use memcpy() on the coherent SA and state record Rosen Penev
2026-11-27 4:04 ` Rosen Penev [this message]
2026-11-27 4:04 ` [PATCH 7/8] crypto: amcc - fix the inverted AEAD error ratelimit Rosen Penev
2026-11-27 4:04 ` [PATCH 8/8] crypto: amcc - wipe the SA key material when freeing it Rosen Penev
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=20261127040423.1023233-7-rosenp@gmail.com \
--to=rosenp@gmail.com \
--cc=chunkeey@gmail.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=jhsiao@amcc.com \
--cc=linux-crypto@vger.kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.