* [PATCH 1/8] crypto: amcc - fix scatter ring full check
2026-11-27 4:04 [PATCH 0/8] crypto: amcc - fixes for the crypto4xx driver Rosen Penev
@ 2026-11-27 4:04 ` Rosen Penev
2026-11-27 4:04 ` [PATCH 2/8] crypto: amcc - fix missing DMA memory barriers in descriptor handling Rosen Penev
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Rosen Penev @ 2026-11-27 4:04 UTC (permalink / raw)
To: linux-crypto
Cc: Herbert Xu, David S. Miller, James Hsiao, Rosen Penev,
Christian Lamparter, stable
crypto4xx_get_n_sd() compares the scatter ring head with the gather
ring tail (gdr_tail) instead of the scatter ring tail. When
sdr_head > sdr_tail but sdr_head <= gdr_tail, neither branch of the
full check runs, and the allocation is granted even if it wraps over
scatter descriptors that are still in use.
On an APM82181 (Meraki MX60), a stress test that keeps many
out-of-place requests in flight stalls the engine with this: the
engine stops at a packet descriptor that has HOST_READY set, no further
interrupts arrive, and every later request hangs. With the check fixed,
the same test runs to completion.
Compare with sdr_tail.
Fixes: 049359d65527 ("crypto: amcc - Add crypt4xx driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/amcc/crypto4xx_core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index acf18b71e54d..7a1ababd9198 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -402,7 +402,7 @@ static u32 crypto4xx_get_n_sd(struct crypto4xx_device *dev, int n)
retval = dev->sdr_head;
tmp = (dev->sdr_head + n) % PPC4XX_NUM_SD;
- if (dev->sdr_head > dev->gdr_tail) {
+ if (dev->sdr_head > dev->sdr_tail) {
if (tmp < dev->sdr_head && tmp >= dev->sdr_tail)
return ERING_WAS_FULL;
} else if (dev->sdr_head < dev->sdr_tail) {
--
2.56.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 2/8] crypto: amcc - fix missing DMA memory barriers in descriptor handling
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 ` Rosen Penev
2026-11-27 4:04 ` [PATCH 3/8] crypto: amcc - allocate all algorithm wrappers before registering Rosen Penev
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Rosen Penev @ 2026-11-27 4:04 UTC (permalink / raw)
To: linux-crypto
Cc: Herbert Xu, David S. Miller, James Hsiao, Rosen Penev,
Christian Lamparter, stable
The descriptor ring lives in coherent memory that the engine scans on
its own, and PD_CTL_HOST_READY in pd_ctl is the ownership handover; the
INT_DESCR_RD writes are only a doorbell. crypto4xx_build_pd() sets
HOST_READY before writing pd_ctl_len, and nothing orders the descriptor
and SA writes before it, so the engine can fetch a descriptor with a
stale length. The wmb() before the doorbell does not help, since
HOST_READY is already visible by then.
Write pd_ctl_len first, then dma_wmb(), then HOST_READY.
The completion handler takes an entry as finished when it is marked
PD_ENTRY_INUSE and pd_ctl has PE_DONE set and HOST_READY clear. Because
the entry state is written after HOST_READY, an engine that finishes
quickly can complete a request whose entry the handler then skips as
not in use, and nothing completes it until the next interrupt. Marking
the entry in use earlier is not enough on its own: pd_ctl is never
cleared when an entry is recycled, so it still holds PE_DONE from its
previous use, and the handler would complete the half-built request.
Clear pd_ctl when the entry is taken, mark it in use before HOST_READY,
and pair the two with smp_wmb()/smp_rmb().
On the completion side, crypto4xx_pd_done() reads the descriptor and
result data right after the PE_DONE check with no barrier in between.
Add a dma_rmb() after the check.
Fixes: 049359d65527 ("crypto: amcc - Add crypt4xx driver")
Cc: stable@vger.kernel.org
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/amcc/crypto4xx_core.c | 42 ++++++++++++++++++++--------
1 file changed, 31 insertions(+), 11 deletions(-)
diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index 7a1ababd9198..e28e836c4f35 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -767,6 +767,12 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
spin_unlock_irqrestore(&dev->core_dev->lock, flags);
pd = &dev->pdr[pd_entry];
+ /*
+ * pd_ctl still holds PE_DONE from this entry's previous use. Clear
+ * it before the entry is marked in use below, or the completion
+ * handler would take the half-built descriptor as finished.
+ */
+ WRITE_ONCE(pd->pd_ctl.w, 0);
pd->sa_len = sa_len;
pd_uinfo = &dev->pdr_uinfo[pd_entry];
@@ -876,11 +882,21 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
}
}
+ pd->pd_ctl_len.w = 0x00400000 | (assoclen + datalen);
+ /* Pairs with smp_rmb() in crypto4xx_bh_tasklet_cb(). */
+ smp_wmb();
+ WRITE_ONCE(pd_uinfo->state,
+ PD_ENTRY_INUSE | (is_busy ? PD_ENTRY_BUSY : 0));
+
+ /*
+ * The engine may fetch the descriptor as soon as it sees
+ * PD_CTL_HOST_READY, without waiting for the doorbell below, so
+ * the descriptor and SA must be visible to it first.
+ */
+ dma_wmb();
pd->pd_ctl.w = PD_CTL_HOST_READY |
((crypto_tfm_alg_type(req->tfm) == CRYPTO_ALG_TYPE_AEAD) ?
PD_CTL_HASH_FINAL : 0);
- pd->pd_ctl_len.w = 0x00400000 | (assoclen + datalen);
- pd_uinfo->state = PD_ENTRY_INUSE | (is_busy ? PD_ENTRY_BUSY : 0);
wmb();
/* write any value to push engine to read a pd */
@@ -1027,16 +1043,20 @@ static void crypto4xx_bh_tasklet_cb(unsigned long data)
do {
pd_uinfo = &core_dev->dev->pdr_uinfo[tail];
pd = &core_dev->dev->pdr[tail];
- if ((pd_uinfo->state & PD_ENTRY_INUSE) &&
- ((READ_ONCE(pd->pd_ctl.w) &
- (PD_CTL_PE_DONE | PD_CTL_HOST_READY)) ==
- PD_CTL_PE_DONE)) {
- crypto4xx_pd_done(core_dev->dev, tail);
- tail = crypto4xx_put_pd_to_pdr(core_dev->dev, tail);
- } else {
- /* if tail not done, break */
+ if (!(READ_ONCE(pd_uinfo->state) & PD_ENTRY_INUSE))
break;
- }
+ /*
+ * Pairs with smp_wmb() in crypto4xx_build_pd(): an in-use
+ * entry has its stale PE_DONE cleared.
+ */
+ smp_rmb();
+ if ((READ_ONCE(pd->pd_ctl.w) &
+ (PD_CTL_PE_DONE | PD_CTL_HOST_READY)) != PD_CTL_PE_DONE)
+ break;
+ /* Read the results only after seeing PE_DONE. */
+ dma_rmb();
+ crypto4xx_pd_done(core_dev->dev, tail);
+ tail = crypto4xx_put_pd_to_pdr(core_dev->dev, tail);
} while (head != tail);
}
--
2.56.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 3/8] crypto: amcc - allocate all algorithm wrappers before registering
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 ` Rosen Penev
2026-11-27 4:04 ` [PATCH 4/8] crypto: amcc - set up the TRNG before registering the algorithms Rosen Penev
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Rosen Penev @ 2026-11-27 4:04 UTC (permalink / raw)
To: linux-crypto
Cc: Herbert Xu, David S. Miller, James Hsiao, Rosen Penev,
Christian Lamparter
crypto4xx_register_alg() allocates a wrapper and registers it, one
algorithm at a time. If an allocation fails partway, it returns
-ENOMEM with the earlier algorithms still registered. The probe error
path does not unregister them, but devres frees the device context and
descriptor rings they point to. The next request on one of them,
typically its own self-test, is a use-after-free. With an allocation
failure injected at the third algorithm on an APM82181:
crypto4xx 400180000.crypto: probe with driver crypto4xx failed with error -12
BUG: Unable to handle kernel data access on write at 0xe4fbd028
NIP [e4f9bb4c] crypto4xx_build_pd+0x174/0x83c [crypto4xx]
[c188b7b0] [e4f9c888] crypto4xx_ctr_crypt+0xc8/0x188 [crypto4xx]
[c188b9e0] [c035f098] test_skcipher_vec_cfg+0x3bc/0x890
Unregistering the algorithms on that path is not safe either: with
self-tests enabled, a test that started on registration may still hold
the algorithm and have requests on the engine.
Allocate every wrapper first and only then register, so an allocation
failure leaves nothing registered.
Fixes: 049359d65527 ("crypto: amcc - Add crypt4xx driver")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/amcc/crypto4xx_core.c | 24 +++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)
diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index e28e836c4f35..6131fd6c0607 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -982,15 +982,32 @@ static int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
struct crypto4xx_alg_common *crypto_alg,
int array_size)
{
- struct crypto4xx_alg *alg;
+ struct crypto4xx_alg **algs, *alg;
int i;
int rc = 0;
+ /*
+ * Allocate every wrapper before registering anything: a registered
+ * algorithm may already have its self-test running on the engine,
+ * so it cannot simply be unregistered again if a later allocation
+ * fails.
+ */
+ algs = kzalloc_objs(*algs, array_size);
+ if (!algs)
+ return -ENOMEM;
+
for (i = 0; i < array_size; i++) {
- alg = kzalloc_obj(struct crypto4xx_alg);
- if (!alg)
+ algs[i] = kzalloc_obj(*algs[i]);
+ if (!algs[i]) {
+ while (i--)
+ kfree(algs[i]);
+ kfree(algs);
return -ENOMEM;
+ }
+ }
+ for (i = 0; i < array_size; i++) {
+ alg = algs[i];
alg->alg = crypto_alg[i];
alg->dev = sec_dev;
@@ -1010,6 +1027,7 @@ static int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
list_add_tail(&alg->entry, &sec_dev->alg_list);
}
+ kfree(algs);
return 0;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 4/8] crypto: amcc - set up the TRNG before registering the algorithms
2026-11-27 4:04 [PATCH 0/8] crypto: amcc - fixes for the crypto4xx driver Rosen Penev
` (2 preceding siblings ...)
2026-11-27 4:04 ` [PATCH 3/8] crypto: amcc - allocate all algorithm wrappers before registering Rosen Penev
@ 2026-11-27 4:04 ` 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
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Rosen Penev @ 2026-11-27 4:04 UTC (permalink / raw)
To: linux-crypto
Cc: Herbert Xu, David S. Miller, James Hsiao, Rosen Penev,
Christian Lamparter
crypto4xx_probe() registers the algorithms and then probes the TRNG. If
ppc4xx_trng_probe() fails, the error path unregisters the algorithms
again. With self-tests enabled, a test that started on registration may
still hold the algorithm and have requests on the engine at that
point. crypto_unregister_alg() then warns that the algorithm is busy,
the driver frees it under the running test, and the IRQ and descriptor
rings are torn down with test requests still in flight. With a TRNG
failure injected on an APM82181, each algorithm hits:
WARNING: CPU: 0 PID: 2547 at crypto_unregister_alg+0xfc/0x158
and the module is left with references it never drops.
Probe the TRNG first and register the algorithms as the last step, so
nothing can fail after registration. crypto4xx_register_alg() only
fails before registering anything, so its error path just tears down
the TRNG again.
Fixes: f033e862a32c ("crypto: crypto4xx - use int for ppc4xx_trng_probe()")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/amcc/crypto4xx_core.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index 6131fd6c0607..0dc23d29845e 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -1347,19 +1347,23 @@ static int crypto4xx_probe(struct platform_device *ofdev)
/* need to setup pdr, rdr, gdr and sdr before this */
crypto4xx_hw_init(core_dev->dev);
- /* Register security algorithms with Linux CryptoAPI */
- rc = crypto4xx_register_alg(core_dev->dev, crypto4xx_alg,
- ARRAY_SIZE(crypto4xx_alg));
+ rc = ppc4xx_trng_probe(core_dev);
if (rc)
goto err_irq;
- rc = ppc4xx_trng_probe(core_dev);
+ /*
+ * Register security algorithms with Linux CryptoAPI. This must be
+ * the last step: their self-tests may start running on the engine
+ * right away, so nothing after this point may fail and unwind.
+ */
+ rc = crypto4xx_register_alg(core_dev->dev, crypto4xx_alg,
+ ARRAY_SIZE(crypto4xx_alg));
if (rc)
- goto err_crypto;
+ goto err_trng;
return 0;
-err_crypto:
- crypto4xx_unregister_alg(core_dev->dev);
+err_trng:
+ ppc4xx_trng_remove(core_dev);
err_irq:
free_irq(core_dev->irq, core_dev);
err_tasklet:
--
2.56.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 5/8] crypto: amcc - don't use memcpy() on the coherent SA and state record
2026-11-27 4:04 [PATCH 0/8] crypto: amcc - fixes for the crypto4xx driver Rosen Penev
` (3 preceding siblings ...)
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 ` Rosen Penev
2026-11-27 4:04 ` [PATCH 6/8] crypto: amcc - fix the DMA mappings of the source and destination Rosen Penev
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Rosen Penev @ 2026-11-27 4:04 UTC (permalink / raw)
To: linux-crypto
Cc: Herbert Xu, David S. Miller, James Hsiao, Rosen Penev,
Christian Lamparter
crypto4xx_build_pd() copies the SA and the IV into the shadow SA and
state record pools with memcpy(). Those pools come from
dma_alloc_coherent(), which is cache-inhibited on the non-coherent 4xx
cores. The 32-bit powerpc memcpy() zeroes each whole destination cache
line with dcbz before filling it, and dcbz on cache-inhibited memory
raises an alignment exception. The kernel emulates it, so the copy
still works, but each such copy takes a trap, and the first one
warns:
WARNING: CPU: 0 PID: 2434 at fix_alignment+0x184/0x1ac
...
---- interrupt: 600 at memcpy+0x88/0x10c
LR [e4f9b88c] crypto4xx_build_pd+0x1c8/0x854 [crypto4xx]
Copy both with word stores instead. Every caller passes word-aligned
buffers whose lengths are multiples of 4.
Fixes: 049359d65527 ("crypto: amcc - Add crypt4xx driver")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/amcc/crypto4xx_core.c | 5 +++--
drivers/crypto/amcc/crypto4xx_core.h | 18 ++++++++++++++++++
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index 0dc23d29845e..cb9b36d6f4fc 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -782,10 +782,11 @@ int crypto4xx_build_pd(struct crypto_async_request *req,
pd_uinfo->async_req = req;
if (iv_len)
- memcpy(pd_uinfo->sr_va->save_iv, iv, iv_len);
+ crypto4xx_memcpy_to_coherent(pd_uinfo->sr_va->save_iv, iv,
+ iv_len);
sa = pd_uinfo->sa_va;
- memcpy(sa, req_sa, sa_len * 4);
+ crypto4xx_memcpy_to_coherent(sa, req_sa, sa_len * 4);
sa->sa_command_1.bf.hash_crypto_offset = (assoclen >> 2);
offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(sa);
diff --git a/drivers/crypto/amcc/crypto4xx_core.h b/drivers/crypto/amcc/crypto4xx_core.h
index 66a95733c86d..294a57b77733 100644
--- a/drivers/crypto/amcc/crypto4xx_core.h
+++ b/drivers/crypto/amcc/crypto4xx_core.h
@@ -209,6 +209,24 @@ static inline void crypto4xx_memcpy_swab32(u32 *dst, const void *buf,
}
}
+/*
+ * The shadow SA and state record pools come from dma_alloc_coherent(),
+ * which is cache-inhibited on the non-coherent 4xx cores. memcpy() zeroes
+ * whole destination cache lines with dcbz, and dcbz on cache-inhibited
+ * memory takes an alignment exception, so copy word by word instead.
+ * WRITE_ONCE() keeps the compiler from turning the loop back into memcpy().
+ * Both pointers must be word aligned and len a multiple of 4.
+ */
+static inline void crypto4xx_memcpy_to_coherent(void *dst, const void *src,
+ size_t len)
+{
+ u32 *d = dst;
+ const u32 *s = src;
+
+ for (; len >= sizeof(u32); len -= sizeof(u32))
+ WRITE_ONCE(*d++, *s++);
+}
+
static inline void crypto4xx_memcpy_from_le32(u32 *dst, const void *buf,
size_t len)
{
--
2.56.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 6/8] crypto: amcc - fix the DMA mappings of the source and destination
2026-11-27 4:04 [PATCH 0/8] crypto: amcc - fixes for the crypto4xx driver Rosen Penev
` (4 preceding siblings ...)
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
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
7 siblings, 0 replies; 9+ messages in thread
From: Rosen Penev @ 2026-11-27 4:04 UTC (permalink / raw)
To: linux-crypto
Cc: Herbert Xu, David S. Miller, James Hsiao, Rosen Penev,
Christian Lamparter
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
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 7/8] crypto: amcc - fix the inverted AEAD error ratelimit
2026-11-27 4:04 [PATCH 0/8] crypto: amcc - fixes for the crypto4xx driver Rosen Penev
` (5 preceding siblings ...)
2026-11-27 4:04 ` [PATCH 6/8] crypto: amcc - fix the DMA mappings of the source and destination Rosen Penev
@ 2026-11-27 4:04 ` Rosen Penev
2026-11-27 4:04 ` [PATCH 8/8] crypto: amcc - wipe the SA key material when freeing it Rosen Penev
7 siblings, 0 replies; 9+ messages in thread
From: Rosen Penev @ 2026-11-27 4:04 UTC (permalink / raw)
To: linux-crypto
Cc: Herbert Xu, David S. Miller, James Hsiao, Rosen Penev,
Christian Lamparter
crypto4xx_aead_done() prints the engine's error status only when
!__ratelimit() is true, but __ratelimit() returns non-zero when a
message may be printed. The errors are therefore dropped while the
rate is low and printed only once messages are being suppressed.
Report the status with one dev_err_ratelimited() call instead, which
also names the device, and drop the driver's own ratelimit state.
Fixes: a0aae821ba3d ("crypto: crypto4xx - prepare for AEAD support")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/amcc/crypto4xx_core.c | 20 +++++++-------------
drivers/crypto/amcc/crypto4xx_core.h | 2 --
2 files changed, 7 insertions(+), 15 deletions(-)
diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index 4c1b76f28de0..8b070b7edcb6 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -588,18 +588,13 @@ static void crypto4xx_aead_done(struct crypto4xx_device *dev,
crypto4xx_ret_sg_desc(dev, pd_uinfo);
if (pd->pd_ctl.bf.status & 0xff) {
- if (!__ratelimit(&dev->aead_ratelimit)) {
- if (pd->pd_ctl.bf.status & 2)
- pr_err("pad fail error\n");
- if (pd->pd_ctl.bf.status & 4)
- pr_err("seqnum fail\n");
- if (pd->pd_ctl.bf.status & 8)
- pr_err("error _notify\n");
- pr_err("aead return err status = 0x%02x\n",
- pd->pd_ctl.bf.status & 0xff);
- pr_err("pd pad_ctl = 0x%08x\n",
- pd->pd_ctl.bf.pd_pad_ctl);
- }
+ dev_err_ratelimited(dev->core_dev->device,
+ "aead error status 0x%02x%s%s%s, pad_ctl 0x%08x\n",
+ pd->pd_ctl.bf.status & 0xff,
+ pd->pd_ctl.bf.status & 2 ? " (pad fail)" : "",
+ pd->pd_ctl.bf.status & 4 ? " (seqnum fail)" : "",
+ pd->pd_ctl.bf.status & 8 ? " (error notify)" : "",
+ pd->pd_ctl.bf.pd_pad_ctl);
err = -EINVAL;
}
@@ -1352,7 +1347,6 @@ static int crypto4xx_probe(struct platform_device *ofdev)
core_dev->device = dev;
spin_lock_init(&core_dev->lock);
INIT_LIST_HEAD(&core_dev->dev->alg_list);
- ratelimit_default_init(&core_dev->dev->aead_ratelimit);
rc = crypto4xx_build_sdr(core_dev->dev);
if (rc)
goto err_build_sdr;
diff --git a/drivers/crypto/amcc/crypto4xx_core.h b/drivers/crypto/amcc/crypto4xx_core.h
index cdd40bb7d254..a8da46f5a44f 100644
--- a/drivers/crypto/amcc/crypto4xx_core.h
+++ b/drivers/crypto/amcc/crypto4xx_core.h
@@ -13,7 +13,6 @@
#ifndef __CRYPTO4XX_CORE_H__
#define __CRYPTO4XX_CORE_H__
-#include <linux/ratelimit.h>
#include <linux/scatterlist.h>
#include <crypto/internal/aead.h>
#include <crypto/internal/skcipher.h>
@@ -100,7 +99,6 @@ struct crypto4xx_device {
struct pd_uinfo *pdr_uinfo;
struct list_head alg_list; /* List of algorithm supported
by this device */
- struct ratelimit_state aead_ratelimit;
bool is_revb;
};
--
2.56.1
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 8/8] crypto: amcc - wipe the SA key material when freeing it
2026-11-27 4:04 [PATCH 0/8] crypto: amcc - fixes for the crypto4xx driver Rosen Penev
` (6 preceding siblings ...)
2026-11-27 4:04 ` [PATCH 7/8] crypto: amcc - fix the inverted AEAD error ratelimit Rosen Penev
@ 2026-11-27 4:04 ` Rosen Penev
7 siblings, 0 replies; 9+ messages in thread
From: Rosen Penev @ 2026-11-27 4:04 UTC (permalink / raw)
To: linux-crypto
Cc: Herbert Xu, David S. Miller, James Hsiao, Rosen Penev,
Christian Lamparter
The inbound and outbound SAs of a transform hold the AES key, and for
GCM the hash key derived from it, but crypto4xx_free_sa() releases
them with plain kfree(). The key stays in freed memory both on rekey
and when the transform is destroyed.
Free them with kfree_sensitive() instead.
Fixes: 049359d65527 ("crypto: amcc - Add crypt4xx driver")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/amcc/crypto4xx_core.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index 8b070b7edcb6..bb078b524994 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -150,9 +150,9 @@ int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size)
void crypto4xx_free_sa(struct crypto4xx_ctx *ctx)
{
- kfree(ctx->sa_in);
+ kfree_sensitive(ctx->sa_in);
ctx->sa_in = NULL;
- kfree(ctx->sa_out);
+ kfree_sensitive(ctx->sa_out);
ctx->sa_out = NULL;
ctx->sa_len = 0;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 9+ messages in thread