* [PATCH 1/8] crypto: amcc - fix scatter ring full check
[not found] <20261127040423.1023233-1-rosenp@gmail.com>
@ 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
1 sibling, 0 replies; 2+ 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] 2+ messages in thread* [PATCH 2/8] crypto: amcc - fix missing DMA memory barriers in descriptor handling
[not found] <20261127040423.1023233-1-rosenp@gmail.com>
2026-11-27 4:04 ` [PATCH 1/8] crypto: amcc - fix scatter ring full check Rosen Penev
@ 2026-11-27 4:04 ` Rosen Penev
1 sibling, 0 replies; 2+ 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] 2+ messages in thread