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>,
stable@vger.kernel.org
Subject: [PATCH 2/8] crypto: amcc - fix missing DMA memory barriers in descriptor handling
Date: Thu, 26 Nov 2026 20:04:17 -0800 [thread overview]
Message-ID: <20261127040423.1023233-3-rosenp@gmail.com> (raw)
In-Reply-To: <20261127040423.1023233-1-rosenp@gmail.com>
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
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 ` Rosen Penev [this message]
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 ` [PATCH 6/8] crypto: amcc - fix the DMA mappings of the source and destination 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
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-3-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 \
--cc=stable@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox