* [PATCH 0/4] QCE data DMA transfer length fixes
@ 2026-12-01 2:54 Wenjia Zhang
2026-12-01 2:54 ` [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment Wenjia Zhang
` (3 more replies)
0 siblings, 4 replies; 10+ messages in thread
From: Wenjia Zhang @ 2026-12-01 2:54 UTC (permalink / raw)
To: Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-arm-msm, linux-crypto, Wenjia Zhang
The QCE driver can map bytes beyond the crypto segment when the last
source scatterlist entry extends past the request length. This series
bounds the input mappings for AEAD, ahash and skcipher requests and
checks the mapped byte counts before submitting data DMA.
CCM decryption also requests a burst-sized output tail where the engine
produces only authentication-tag-sized zero padding. Use the tag length
for that tail and validate the expected output byte count.
The series is based on linux-next next-20261130, commit
253f86660c1f882fbf554db5eaa34bc5d66dc7e8.
Validation:
- Strict checkpatch: all four patches passed.
- ARM64 W=1: all QCE algorithms and the core driver built after each
patch as modules; the final series also built with them built-in.
- Public prerequisite lookup and direct patch replay passed.
Prerequisite: QCE core/auxiliary split v13
https://lore.kernel.org/all/20261127-qce-fix-self-tests-v13-1-80b601792901@oss.qualcomm.com/
Rebase and cover-letter preparation were assisted by Codex.
---
Wenjia Zhang (4):
crypto: qce - Bound AEAD input DMA to the programmed segment
crypto: qce - Size CCM decrypt output DMA for the tag length
crypto: qce - Bound ahash input DMA to the current segment
crypto: qce - Bound skcipher input DMA to cryptlen
drivers/crypto/qce/aead.c | 156 +++++++++++++++++++++++++++++++-----------
drivers/crypto/qce/cipher.h | 3 +-
drivers/crypto/qce/sha.c | 76 +++++++++++++++++---
drivers/crypto/qce/sha.h | 2 +
drivers/crypto/qce/skcipher.c | 59 ++++++++++++++--
5 files changed, 239 insertions(+), 57 deletions(-)
---
base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
change-id: 20261201-codex-qce-dma-bounds-upstream-20261201-b45695077c6c
prerequisite-patch-id: 1fbd4fe6425832e67d4511915ad631021264ce1b
Best regards,
--
Wenjia Zhang <wenjia.zhang@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment
2026-12-01 2:54 [PATCH 0/4] QCE data DMA transfer length fixes Wenjia Zhang
@ 2026-12-01 2:54 ` Wenjia Zhang
2026-12-01 3:06 ` sashiko-bot
2026-12-01 5:06 ` Kuldeep Singh
2026-12-01 2:54 ` [PATCH 2/4] crypto: qce - Size CCM decrypt output DMA for the tag length Wenjia Zhang
` (2 subsequent siblings)
3 siblings, 2 replies; 10+ messages in thread
From: Wenjia Zhang @ 2026-12-01 2:54 UTC (permalink / raw)
To: Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-arm-msm, linux-crypto, Wenjia Zhang
From: Wenjia Zhang <wenjz@qti.qualcomm.com>
An AEAD source scatterlist can contain bytes beyond the input segment,
such as space reserved for an output authentication tag.
sg_nents_for_len() counts the entries covering a length but does not
shorten the final entry. Mapping the original list can therefore pass
those trailing bytes to BAM, exceeding the programmed crypto segment and
causing transfer errors or timeouts.
Build a bounded source table for separately mapped input lists,
referencing the original pages and limiting the final entry to the
required length. Validate the total mapped input length against the
configured segment. Include the authentication tag for CCM decryption,
where hardware consumes it, but exclude it for authenc decryption, where
software verifies it.
Handle the zero return from dma_map_sg() as a mapping failure and
release the allocated scatterlist tables on completion and preparation
or submission errors.
Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
---
drivers/crypto/qce/aead.c | 117 +++++++++++++++++++++++++++++++---------------
1 file changed, 79 insertions(+), 38 deletions(-)
diff --git a/drivers/crypto/qce/aead.c b/drivers/crypto/qce/aead.c
index 2065c46162c217484016021bbe3ee2f2db082e65..793913b33da6fc01f96a1e7612807f5e5d413439 100644
--- a/drivers/crypto/qce/aead.c
+++ b/drivers/crypto/qce/aead.c
@@ -48,18 +48,8 @@ static void qce_aead_done(void *data)
dma_unmap_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
- if (IS_CCM(rctx->flags)) {
- if (req->assoclen) {
- sg_free_table(&rctx->src_tbl);
- if (diff_dst)
- sg_free_table(&rctx->dst_tbl);
- } else {
- if (!(IS_DECRYPT(rctx->flags) && !diff_dst))
- sg_free_table(&rctx->dst_tbl);
- }
- } else {
- sg_free_table(&rctx->dst_tbl);
- }
+ sg_free_table(&rctx->src_tbl);
+ sg_free_table(&rctx->dst_tbl);
error = qce_check_status(qce, &status);
if (error < 0 && (error != -EBADMSG))
@@ -291,11 +281,40 @@ qce_aead_ccm_prepare_buf_assoclen(struct aead_request *req)
return ret;
}
+static int qce_aead_prepare_src_buf(struct aead_request *req,
+ unsigned int len)
+{
+ struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
+ struct scatterlist *sg;
+ gfp_t gfp;
+ int ret;
+
+ rctx->src_nents = sg_nents_for_len(req->src, len);
+ if (rctx->src_nents < 0)
+ return rctx->src_nents;
+
+ gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
+ GFP_KERNEL : GFP_ATOMIC;
+ ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
+ if (ret)
+ return ret;
+
+ /* The last entry may include tag space or unrelated trailing bytes. */
+ sg = qce_sgtable_add(&rctx->src_tbl, req->src, len);
+ if (IS_ERR(sg)) {
+ ret = PTR_ERR(sg);
+ sg_free_table(&rctx->src_tbl);
+ return ret;
+ }
+ sg_mark_end(sg);
+ rctx->src_sg = rctx->src_tbl.sgl;
+
+ return 0;
+}
+
static int qce_aead_prepare_buf(struct aead_request *req)
{
struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
- struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
- struct qce_device *qce = tmpl->qce;
struct scatterlist *sg;
bool diff_dst = (req->src != req->dst) ? true : false;
unsigned int totallen;
@@ -305,17 +324,11 @@ static int qce_aead_prepare_buf(struct aead_request *req)
sg = qce_aead_prepare_dst_buf(req);
if (IS_ERR(sg))
return PTR_ERR(sg);
- if (diff_dst) {
- rctx->src_nents = sg_nents_for_len(req->src, totallen);
- if (rctx->src_nents < 0) {
- dev_err(qce->dev, "Invalid numbers of src SG.\n");
- return -EINVAL;
- }
- rctx->src_sg = req->src;
- } else {
- rctx->src_nents = rctx->dst_nents - 1;
- rctx->src_sg = rctx->dst_sg;
- }
+ if (diff_dst)
+ return qce_aead_prepare_src_buf(req, totallen);
+
+ rctx->src_nents = rctx->dst_nents - 1;
+ rctx->src_sg = rctx->dst_sg;
return 0;
}
@@ -327,6 +340,7 @@ static int qce_aead_ccm_prepare_buf(struct aead_request *req)
struct scatterlist *sg;
bool diff_dst = (req->src != req->dst) ? true : false;
unsigned int cryptlen;
+ int ret;
if (rctx->assoclen)
return qce_aead_ccm_prepare_buf_assoclen(req);
@@ -336,8 +350,9 @@ static int qce_aead_ccm_prepare_buf(struct aead_request *req)
cryptlen = rctx->cryptlen + ctx->authsize;
if (diff_dst) {
- rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
- rctx->src_sg = req->src;
+ ret = qce_aead_prepare_src_buf(req, cryptlen);
+ if (ret)
+ return ret;
sg = qce_aead_prepare_dst_buf(req);
if (IS_ERR(sg))
return PTR_ERR(sg);
@@ -397,6 +412,30 @@ static int qce_aead_create_ccm_nonce(struct qce_aead_reqctx *rctx, struct qce_ae
return 0;
}
+static int qce_aead_check_input(struct aead_request *req, int mapped_nents)
+{
+ struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
+ struct qce_aead_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
+ struct scatterlist *sg;
+ u64 dma_len = 0;
+ unsigned int expected = rctx->cryptlen + rctx->assoclen;
+ int nents, i, ret;
+
+ /* CCM decrypt consumes the tag; authenc verifies it in software. */
+ if (IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags))
+ expected += ctx->authsize;
+
+ nents = sg_nents_for_len(req->src, req->assoclen + req->cryptlen);
+ if (nents < 0)
+ return nents;
+ for_each_sg(rctx->src_sg, sg, mapped_nents, i)
+ dma_len += sg_dma_len(sg);
+
+ ret = dma_len == expected ? 0 : -EINVAL;
+
+ return ret;
+}
+
static int
qce_aead_async_req_handle(struct crypto_async_request *async_req)
{
@@ -410,6 +449,9 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
bool diff_dst;
int dst_nents, src_nents, ret;
+ rctx->src_tbl.sgl = NULL;
+ rctx->dst_tbl.sgl = NULL;
+
if (IS_CCM_RFC4309(rctx->flags)) {
memset(rctx->ccm_rfc4309_iv, 0, QCE_MAX_IV_SIZE);
rctx->ccm_rfc4309_iv[0] = 3;
@@ -441,7 +483,7 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
ret = qce_aead_prepare_buf(req);
if (ret)
- return ret;
+ goto error_free;
dst_nents = dma_map_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
if (!dst_nents) {
ret = -EIO;
@@ -450,8 +492,8 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
if (diff_dst) {
src_nents = dma_map_sg(qce->dma_dev, rctx->src_sg, rctx->src_nents, dir_src);
- if (src_nents < 0) {
- ret = src_nents;
+ if (!src_nents) {
+ ret = -EIO;
goto error_unmap_dst;
}
} else {
@@ -461,6 +503,10 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
src_nents = dst_nents - 1;
}
+ ret = qce_aead_check_input(req, src_nents);
+ if (ret)
+ goto error_unmap_src;
+
ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents, rctx->dst_sg, dst_nents,
qce_aead_done, async_req);
if (ret)
@@ -478,17 +524,12 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
qce_dma_terminate_all(&qce->dma);
error_unmap_src:
if (diff_dst)
- dma_unmap_sg(qce->dma_dev, req->src, rctx->src_nents, dir_src);
+ dma_unmap_sg(qce->dma_dev, rctx->src_sg, rctx->src_nents, dir_src);
error_unmap_dst:
dma_unmap_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
error_free:
- if (IS_CCM(rctx->flags) && rctx->assoclen) {
- sg_free_table(&rctx->src_tbl);
- if (diff_dst)
- sg_free_table(&rctx->dst_tbl);
- } else {
- sg_free_table(&rctx->dst_tbl);
- }
+ sg_free_table(&rctx->src_tbl);
+ sg_free_table(&rctx->dst_tbl);
return ret;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 2/4] crypto: qce - Size CCM decrypt output DMA for the tag length
2026-12-01 2:54 [PATCH 0/4] QCE data DMA transfer length fixes Wenjia Zhang
2026-12-01 2:54 ` [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment Wenjia Zhang
@ 2026-12-01 2:54 ` Wenjia Zhang
2026-12-01 3:13 ` sashiko-bot
2026-12-01 2:54 ` [PATCH 3/4] crypto: qce - Bound ahash input DMA to the current segment Wenjia Zhang
2026-12-01 2:54 ` [PATCH 4/4] crypto: qce - Bound skcipher input DMA to cryptlen Wenjia Zhang
3 siblings, 1 reply; 10+ messages in thread
From: Wenjia Zhang @ 2026-12-01 2:54 UTC (permalink / raw)
To: Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-arm-msm, linux-crypto, Wenjia Zhang
From: Wenjia Zhang <wenjz@qti.qualcomm.com>
In BAM mode, CCM decryption emits the plaintext followed by zero bytes
equal in length to the input authentication tag. The driver reserves a
full 64-byte burst for that tail. For a 32-byte payload and a 16-byte
tag, it requests 96 output bytes although the engine produces only 48.
Output DMA then waits for bytes that will never arrive.
Use authsize for the CCM decryption tail instead of QCE_BAM_BURST_SIZE.
Keep the burst-sized tail for encryption, where the generated tag is
padded to a burst boundary.
Validate the total mapped CCM output length before starting the request,
accounting for formatted associated data, the payload and the
direction-specific tag or padding length.
Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
---
drivers/crypto/qce/aead.c | 39 +++++++++++++++++++++++++++++++++++++--
1 file changed, 37 insertions(+), 2 deletions(-)
diff --git a/drivers/crypto/qce/aead.c b/drivers/crypto/qce/aead.c
index 793913b33da6fc01f96a1e7612807f5e5d413439..6d7844e82ab03022594cd3a95f447f69cdaeea39 100644
--- a/drivers/crypto/qce/aead.c
+++ b/drivers/crypto/qce/aead.c
@@ -91,9 +91,15 @@ static struct scatterlist *
qce_aead_prepare_ccm_result_buf(struct sg_table *tbl, struct aead_request *req)
{
struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
+ struct qce_aead_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
+ unsigned int len = QCE_BAM_BURST_SIZE;
+
+ /* HPG 3.10/3.12: BAM decrypt replaces the tag with equal-sized zeroes. */
+ if (IS_DECRYPT(rctx->flags))
+ len = ctx->authsize;
- sg_init_one(&rctx->result_sg, rctx->ccmresult_buf, QCE_BAM_BURST_SIZE);
- return qce_sgtable_add(tbl, &rctx->result_sg, QCE_BAM_BURST_SIZE);
+ sg_init_one(&rctx->result_sg, rctx->ccmresult_buf, len);
+ return qce_sgtable_add(tbl, &rctx->result_sg, len);
}
static struct scatterlist *
@@ -436,6 +442,29 @@ static int qce_aead_check_input(struct aead_request *req, int mapped_nents)
return ret;
}
+static int qce_aead_check_ccm_output(struct aead_request *req, int mapped_nents)
+{
+ struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
+ struct qce_aead_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
+ struct scatterlist *sg;
+ u64 expected = rctx->cryptlen, dma_len = 0;
+ bool decrypt = !!IS_DECRYPT(rctx->flags);
+ int i, ret;
+
+ expected += rctx->assoclen;
+ if (decrypt)
+ expected += ctx->authsize;
+ else
+ expected += ALIGN(ctx->authsize, QCE_BAM_BURST_SIZE);
+
+ for_each_sg(rctx->dst_sg, sg, mapped_nents, i)
+ dma_len += sg_dma_len(sg);
+
+ ret = dma_len == expected ? 0 : -EINVAL;
+
+ return ret;
+}
+
static int
qce_aead_async_req_handle(struct crypto_async_request *async_req)
{
@@ -507,6 +536,12 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
if (ret)
goto error_unmap_src;
+ if (IS_CCM(rctx->flags)) {
+ ret = qce_aead_check_ccm_output(req, dst_nents);
+ if (ret)
+ goto error_unmap_src;
+ }
+
ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents, rctx->dst_sg, dst_nents,
qce_aead_done, async_req);
if (ret)
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 3/4] crypto: qce - Bound ahash input DMA to the current segment
2026-12-01 2:54 [PATCH 0/4] QCE data DMA transfer length fixes Wenjia Zhang
2026-12-01 2:54 ` [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment Wenjia Zhang
2026-12-01 2:54 ` [PATCH 2/4] crypto: qce - Size CCM decrypt output DMA for the tag length Wenjia Zhang
@ 2026-12-01 2:54 ` Wenjia Zhang
2026-12-01 3:02 ` sashiko-bot
2026-12-01 2:54 ` [PATCH 4/4] crypto: qce - Bound skcipher input DMA to cryptlen Wenjia Zhang
3 siblings, 1 reply; 10+ messages in thread
From: Wenjia Zhang @ 2026-12-01 2:54 UTC (permalink / raw)
To: Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-arm-msm, linux-crypto, Wenjia Zhang
From: Wenjia Zhang <wenjz@qti.qualcomm.com>
Hash updates submit complete blocks and retain any remaining bytes for a
later submission. Reducing req->nbytes to the current segment does not
shorten the source scatterlist. sg_nents_for_len() only counts entries,
so mapping the last entry in full can send the retained bytes to BAM as
well. The DMA input then exceeds the programmed hash segment and the
request can fail.
Build a source table bounded to req->nbytes for each hardware submission
and validate its total mapped length. Pass the mapped entry count to DMA
descriptor preparation, while retaining the original entry count for
unmapping.
Free the table on completion and submission failure, and restore the
caller's original source list and byte count if submission fails.
Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
---
drivers/crypto/qce/sha.c | 76 +++++++++++++++++++++++++++++++++++++++++-------
drivers/crypto/qce/sha.h | 2 ++
2 files changed, 67 insertions(+), 11 deletions(-)
diff --git a/drivers/crypto/qce/sha.c b/drivers/crypto/qce/sha.c
index 3dca00e53e473d3d2f2cf02cf12bdd38e51dc539..1f7ed0e15afdf1f336a88ba5d03bd19c3a7fd778 100644
--- a/drivers/crypto/qce/sha.c
+++ b/drivers/crypto/qce/sha.c
@@ -31,6 +31,47 @@ static const u32 std_iv_sha256[SHA256_DIGEST_SIZE / sizeof(u32)] = {
SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7
};
+static int qce_ahash_prepare_src(struct ahash_request *req)
+{
+ struct qce_sha_reqctx *rctx = ahash_request_ctx_dma(req);
+ struct scatterlist *sg;
+ gfp_t gfp;
+ int ret;
+
+ rctx->src_nents = sg_nents_for_len(req->src, req->nbytes);
+ if (rctx->src_nents <= 0)
+ return rctx->src_nents ?: -EINVAL;
+
+ gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
+ GFP_KERNEL : GFP_ATOMIC;
+ ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
+ if (ret)
+ return ret;
+
+ /* HPG 3.1: input descriptors must match the segment size exactly. */
+ sg = qce_sgtable_add(&rctx->src_tbl, req->src, req->nbytes);
+ if (IS_ERR(sg)) {
+ ret = PTR_ERR(sg);
+ sg_free_table(&rctx->src_tbl);
+ return ret;
+ }
+ sg_mark_end(sg);
+ return 0;
+}
+
+static int qce_ahash_check_input(struct ahash_request *req, int mapped_nents)
+{
+ struct qce_sha_reqctx *rctx = ahash_request_ctx_dma(req);
+ struct scatterlist *sg;
+ u64 total = 0;
+ int i, ret;
+
+ for_each_sg(rctx->src_tbl.sgl, sg, mapped_nents, i)
+ total += sg_dma_len(sg);
+ ret = total == req->nbytes ? 0 : -EINVAL;
+ return ret;
+}
+
static void qce_ahash_done(void *data)
{
struct crypto_async_request *async_req = data;
@@ -48,8 +89,10 @@ static void qce_ahash_done(void *data)
if (error)
dev_dbg(qce->dev, "ahash dma termination error (%d)\n", error);
- dma_unmap_sg(qce->dma_dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
+ dma_unmap_sg(qce->dma_dev, rctx->src_tbl.sgl, rctx->src_nents,
+ DMA_TO_DEVICE);
dma_unmap_sg(qce->dma_dev, &rctx->result_sg, 1, DMA_FROM_DEVICE);
+ sg_free_table(&rctx->src_tbl);
memcpy(rctx->digest, result->auth_iv, digestsize);
if (req->result && rctx->last_blk)
@@ -78,7 +121,7 @@ static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
struct qce_device *qce = tmpl->qce;
unsigned long flags = rctx->flags;
- int ret;
+ int ret, mapped_nents;
if (IS_SHA_HMAC(flags)) {
rctx->authkey = ctx->authkey;
@@ -88,15 +131,20 @@ static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
rctx->authklen = AES_KEYSIZE_128;
}
- rctx->src_nents = sg_nents_for_len(req->src, req->nbytes);
- if (rctx->src_nents < 0) {
- dev_err(qce->dev, "Invalid numbers of src SG.\n");
- return rctx->src_nents;
+ ret = qce_ahash_prepare_src(req);
+ if (ret)
+ goto error_restore_req;
+
+ mapped_nents = dma_map_sg(qce->dma_dev, rctx->src_tbl.sgl,
+ rctx->src_nents, DMA_TO_DEVICE);
+ if (!mapped_nents) {
+ ret = -EIO;
+ goto error_free_src;
}
- ret = dma_map_sg(qce->dma_dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
- if (!ret)
- return -EIO;
+ ret = qce_ahash_check_input(req, mapped_nents);
+ if (ret)
+ goto error_unmap_src;
sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
@@ -106,7 +154,7 @@ static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
goto error_unmap_src;
}
- ret = qce_dma_prep_sgs(&qce->dma, req->src, rctx->src_nents,
+ ret = qce_dma_prep_sgs(&qce->dma, rctx->src_tbl.sgl, mapped_nents,
&rctx->result_sg, 1, qce_ahash_done, async_req);
if (ret)
goto error_unmap_dst;
@@ -124,7 +172,13 @@ static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
error_unmap_dst:
dma_unmap_sg(qce->dma_dev, &rctx->result_sg, 1, DMA_FROM_DEVICE);
error_unmap_src:
- dma_unmap_sg(qce->dma_dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
+ dma_unmap_sg(qce->dma_dev, rctx->src_tbl.sgl, rctx->src_nents,
+ DMA_TO_DEVICE);
+error_free_src:
+ sg_free_table(&rctx->src_tbl);
+error_restore_req:
+ req->src = rctx->src_orig;
+ req->nbytes = rctx->nbytes_orig;
return ret;
}
diff --git a/drivers/crypto/qce/sha.h b/drivers/crypto/qce/sha.h
index cb822fc334dc187cf1c66e2a332822a596ebcef3..e503dcb34f5fb4baae67d9b28f08be1292b8ce79 100644
--- a/drivers/crypto/qce/sha.h
+++ b/drivers/crypto/qce/sha.h
@@ -29,6 +29,7 @@ struct qce_sha_ctx {
* @src_orig: original request sg list
* @nbytes_orig: original request number of bytes
* @src_nents: source number of entries
+ * @src_tbl: owned source scatterlist bounded to the hardware input length
* @byte_count: byte count
* @count: save count in states during update, import and export
* @first_blk: is it the first block
@@ -47,6 +48,7 @@ struct qce_sha_reqctx {
struct scatterlist *src_orig;
unsigned int nbytes_orig;
int src_nents;
+ struct sg_table src_tbl;
__be32 byte_count[2];
u64 count;
bool first_blk;
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 4/4] crypto: qce - Bound skcipher input DMA to cryptlen
2026-12-01 2:54 [PATCH 0/4] QCE data DMA transfer length fixes Wenjia Zhang
` (2 preceding siblings ...)
2026-12-01 2:54 ` [PATCH 3/4] crypto: qce - Bound ahash input DMA to the current segment Wenjia Zhang
@ 2026-12-01 2:54 ` Wenjia Zhang
2026-12-01 3:01 ` sashiko-bot
3 siblings, 1 reply; 10+ messages in thread
From: Wenjia Zhang @ 2026-12-01 2:54 UTC (permalink / raw)
To: Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-arm-msm, linux-crypto, Wenjia Zhang
From: Wenjia Zhang <wenjz@qti.qualcomm.com>
When a skcipher request uses different source and destination lists, the
driver maps the source entries returned by sg_nents_for_len(). That
helper does not shorten the last entry to cryptlen. A request whose
final entry extends beyond the payload therefore sends excess data to
BAM and can fail because the input exceeds the programmed segment. This
also affects in-place requests using two lists for the same pages.
Build a separate source table bounded to cryptlen for this path,
referencing the existing pages without copying the payload. Validate the
total mapped input length before starting the engine, including the path
that shares the destination mapping.
Unmap the owned source table and free it on completion and every
submission error path.
Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
---
drivers/crypto/qce/cipher.h | 3 ++-
drivers/crypto/qce/skcipher.c | 59 +++++++++++++++++++++++++++++++++++++++----
2 files changed, 56 insertions(+), 6 deletions(-)
diff --git a/drivers/crypto/qce/cipher.h b/drivers/crypto/qce/cipher.h
index daea07551118d444d2f749588bdfe2ae2c6c553f..4dd927e9325a4c56937823e6bfe0c188a6599b51 100644
--- a/drivers/crypto/qce/cipher.h
+++ b/drivers/crypto/qce/cipher.h
@@ -28,7 +28,7 @@ struct qce_cipher_ctx {
* @result_sg: scatterlist used for result buffer
* @dst_tbl: destination sg table
* @dst_sg: destination sg pointer table beginning
- * @src_tbl: source sg table
+ * @src_tbl: owned source sg table bounded to the crypto length
* @src_sg: source sg pointer table beginning;
* @cryptlen: crypto length
*/
@@ -41,6 +41,7 @@ struct qce_cipher_reqctx {
struct scatterlist result_sg;
struct sg_table dst_tbl;
struct scatterlist *dst_sg;
+ struct sg_table src_tbl;
struct scatterlist *src_sg;
unsigned int cryptlen;
struct skcipher_request fallback_req; // keep at the end
diff --git a/drivers/crypto/qce/skcipher.c b/drivers/crypto/qce/skcipher.c
index 7067c4c20c1e7f8d55c2112dd91a9acdafcb9b60..150a140287df54a983a886d6a9424c68c5a9c0a9 100644
--- a/drivers/crypto/qce/skcipher.c
+++ b/drivers/crypto/qce/skcipher.c
@@ -25,6 +25,42 @@ MODULE_PARM_DESC(aes_sw_max_len,
static LIST_HEAD(skcipher_algs);
+static int qce_skcipher_prepare_src(struct skcipher_request *req, gfp_t gfp)
+{
+ struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
+ struct scatterlist *sg;
+ int ret;
+
+ ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
+ if (ret)
+ return ret;
+
+ /* HPG 3.1: tag space after the input must not reach the consumer pipe. */
+ sg = qce_sgtable_add(&rctx->src_tbl, req->src, req->cryptlen);
+ if (IS_ERR(sg)) {
+ ret = PTR_ERR(sg);
+ sg_free_table(&rctx->src_tbl);
+ return ret;
+ }
+ sg_mark_end(sg);
+ rctx->src_sg = rctx->src_tbl.sgl;
+ return 0;
+}
+
+static int qce_skcipher_check_input(struct skcipher_request *req,
+ int mapped_nents)
+{
+ struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
+ struct scatterlist *sg;
+ u64 total = 0;
+ int i, ret;
+
+ for_each_sg(rctx->src_sg, sg, mapped_nents, i)
+ total += sg_dma_len(sg);
+ ret = total == req->cryptlen ? 0 : -EINVAL;
+ return ret;
+}
+
static void qce_skcipher_done(void *data)
{
struct crypto_async_request *async_req = data;
@@ -47,8 +83,10 @@ static void qce_skcipher_done(void *data)
dev_dbg(qce->dev, "skcipher dma termination error (%d)\n",
error);
- if (diff_dst)
+ if (diff_dst) {
dma_unmap_sg(qce->dma_dev, rctx->src_sg, rctx->src_nents, dir_src);
+ sg_free_table(&rctx->src_tbl);
+ }
dma_unmap_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
sg_free_table(&rctx->dst_tbl);
@@ -131,17 +169,25 @@ qce_skcipher_async_req_handle(struct crypto_async_request *async_req)
}
if (diff_dst) {
- src_nents = dma_map_sg(qce->dma_dev, req->src, rctx->src_nents, dir_src);
+ ret = qce_skcipher_prepare_src(req, gfp);
+ if (ret)
+ goto error_unmap_dst;
+
+ src_nents = dma_map_sg(qce->dma_dev, rctx->src_sg,
+ rctx->src_nents, dir_src);
if (!src_nents) {
ret = -EIO;
- goto error_unmap_dst;
+ goto error_free_src;
}
- rctx->src_sg = req->src;
} else {
rctx->src_sg = rctx->dst_sg;
src_nents = dst_nents - 1;
}
+ ret = qce_skcipher_check_input(req, src_nents);
+ if (ret)
+ goto error_unmap_src;
+
ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents,
rctx->dst_sg, dst_nents,
qce_skcipher_done, async_req);
@@ -160,7 +206,10 @@ qce_skcipher_async_req_handle(struct crypto_async_request *async_req)
qce_dma_terminate_all(&qce->dma);
error_unmap_src:
if (diff_dst)
- dma_unmap_sg(qce->dma_dev, req->src, rctx->src_nents, dir_src);
+ dma_unmap_sg(qce->dma_dev, rctx->src_sg, rctx->src_nents, dir_src);
+error_free_src:
+ if (diff_dst)
+ sg_free_table(&rctx->src_tbl);
error_unmap_dst:
dma_unmap_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
error_free:
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 4/4] crypto: qce - Bound skcipher input DMA to cryptlen
2026-12-01 2:54 ` [PATCH 4/4] crypto: qce - Bound skcipher input DMA to cryptlen Wenjia Zhang
@ 2026-12-01 3:01 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-01 3:01 UTC (permalink / raw)
To: Wenjia Zhang, Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-crypto, Wenjia Zhang
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] crypto: qce: missing Fixes trailer for unbounded req->src DMA
mapping
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] crypto: qce: error code inversion in
qce_skcipher_async_req_handle() [1]
--
commit a975096f0c2cdacb9670c485b8f959458f058af4
Author: Wenjia Zhang <wenjz@qti.qualcomm.com>
> crypto: qce - Bound skcipher input DMA to cryptlen
>
> When a skcipher request uses different source and destination lists, the
> driver maps the source entries returned by sg_nents_for_len(). That
> helper does not shorten the last entry to cryptlen. A request whose
> final entry extends beyond the payload therefore sends excess data to
> BAM and can fail because the input exceeds the programmed segment. This
> also affects in-place requests using two lists for the same pages.
>
> Build a separate source table bounded to cryptlen for this path,
> referencing the existing pages without copying the payload. Validate the
> total mapped input length before starting the engine, including the path
> that shares the destination mapping.
>
> Unmap the owned source table and free it on completion and every
> submission error path.
>
> Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
[Severity: Low]
Since this fixes unbounded DMA mapping of req->src that has been present
since the driver was introduced, should this include a Fixes tag?
Fixes: ec8f5d8f6f76 ("crypto: qce - Qualcomm crypto engine driver")
> diff --git a/drivers/crypto/qce/skcipher.c b/drivers/crypto/qce/skcipher.c
> index 7067c4c20c1e7..150a140287df5 100644
> --- a/drivers/crypto/qce/skcipher.c
> +++ b/drivers/crypto/qce/skcipher.c
[ ... ]
> @@ -131,17 +169,25 @@ qce_skcipher_async_req_handle(struct crypto_async_request *async_req)
> }
>
> if (diff_dst) {
> - src_nents = dma_map_sg(qce->dma_dev, req->src, rctx->src_nents, dir_src);
> + ret = qce_skcipher_prepare_src(req, gfp);
> + if (ret)
> + goto error_unmap_dst;
[Severity: Critical]
This problem wasn't introduced by this patch, but earlier in
qce_skcipher_async_req_handle() [1], doesn't returning
-rctx->dst_nents when rctx->dst_nents < 0 invert the negative error code
from sg_nents_for_len() into a positive value?
drivers/crypto/qce/skcipher.c:qce_skcipher_async_req_handle() {
...
if (rctx->dst_nents < 0) {
dev_err(qce->dev, "Invalid numbers of dst SG.\n");
return -rctx->dst_nents;
}
...
}
When a request provides a destination scatterlist shorter than
req->cryptlen, sg_nents_for_len() returns -EINVAL, and negating it causes
callers such as AF_ALG to treat the failed request as a positive byte
count and read uninitialized destination memory.
[1] https://sashiko.dev/#/bug/linux-0ba52c88-bdb7-4c9e-8c47-2ec58cfe9437
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-codex-qce-dma-bounds-upstream-20261201-v1-0-c15d01b45bce@oss.qualcomm.com?part=4
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 3/4] crypto: qce - Bound ahash input DMA to the current segment
2026-12-01 2:54 ` [PATCH 3/4] crypto: qce - Bound ahash input DMA to the current segment Wenjia Zhang
@ 2026-12-01 3:02 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-01 3:02 UTC (permalink / raw)
To: Wenjia Zhang, Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-crypto, Wenjia Zhang
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] crypto: qce: missing CRYPTO_ALG_ALLOCATES_MEMORY in
qce_ahash_register_one()
- [Low] crypto: qce: missing Fixes trailer for ahash input DMA bounding
fix
--
commit 1b196f099887dc46df99e46259af8be85b93abbd
Author: Wenjia Zhang <wenjz@qti.qualcomm.com>
> crypto: qce - Bound ahash input DMA to the current segment
>
> Hash updates submit complete blocks and retain any remaining bytes for a
> later submission. Reducing req->nbytes to the current segment does not
> shorten the source scatterlist. sg_nents_for_len() only counts entries,
> so mapping the last entry in full can send the retained bytes to BAM as
> well. The DMA input then exceeds the programmed hash segment and the
> request can fail.
>
> Build a source table bounded to req->nbytes for each hardware submission
> and validate its total mapped length. Pass the mapped entry count to DMA
> descriptor preparation, while retaining the original entry count for
> unmapping.
>
> Free the table on completion and submission failure, and restore the
> caller's original source list and byte count if submission fails.
>
> Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
[Severity: Low]
Since this fixes a bug present since the driver was added, should it
include a Fixes tag?
Fixes: ec8f5d8f6f76 ("crypto: qce - Qualcomm crypto engine driver")
> diff --git a/drivers/crypto/qce/sha.c b/drivers/crypto/qce/sha.c
> index 3dca00e53e473..1f7ed0e15afdf 100644
> --- a/drivers/crypto/qce/sha.c
> +++ b/drivers/crypto/qce/sha.c
> @@ -31,6 +31,47 @@ static const u32 std_iv_sha256[SHA256_DIGEST_SIZE / sizeof(u32)] = {
> SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7
> };
>
> +static int qce_ahash_prepare_src(struct ahash_request *req)
> +{
> + struct qce_sha_reqctx *rctx = ahash_request_ctx_dma(req);
> + struct scatterlist *sg;
> + gfp_t gfp;
> + int ret;
> +
> + rctx->src_nents = sg_nents_for_len(req->src, req->nbytes);
> + if (rctx->src_nents <= 0)
> + return rctx->src_nents ?: -EINVAL;
> +
> + gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
> + GFP_KERNEL : GFP_ATOMIC;
> + ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
[Severity: Medium]
Should qce_ahash_register_one() now set CRYPTO_ALG_ALLOCATES_MEMORY in
base->cra_flags (like qce_skcipher_register_one() and
qce_aead_register_one() do)?
qce_ahash_prepare_src() now calls sg_alloc_table() during request
processing, while qce_ahash_register_one() still sets only
CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY. Block I/O callers such as
dm-integrity that mask out CRYPTO_ALG_ALLOCATES_MEMORY when calling
crypto_alloc_ahash() can bind to QCE ahash algorithms and trigger
GFP_KERNEL allocations from a GFP_NOIO block I/O context when
CRYPTO_TFM_REQ_MAY_SLEEP is set, risking memory reclaim recursion
deadlocks.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-codex-qce-dma-bounds-upstream-20261201-v1-0-c15d01b45bce@oss.qualcomm.com?part=3
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment
2026-12-01 2:54 ` [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment Wenjia Zhang
@ 2026-12-01 3:06 ` sashiko-bot
2026-12-01 5:06 ` Kuldeep Singh
1 sibling, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-01 3:06 UTC (permalink / raw)
To: Wenjia Zhang, Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-crypto, Wenjia Zhang
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] crypto: qce - unbounded src_sg on in-place CCM decrypt in
qce_aead_ccm_prepare_buf()
- [Low] crypto: qce - missing Fixes trailer in commit message
--
commit f3b55d49bd062185aa02d0a7a7eedf329e6ab5f7
Author: Wenjia Zhang <wenjz@qti.qualcomm.com>
> crypto: qce - Bound AEAD input DMA to the programmed segment
>
> An AEAD source scatterlist can contain bytes beyond the input segment,
> such as space reserved for an output authentication tag.
> sg_nents_for_len() counts the entries covering a length but does not
> shorten the final entry. Mapping the original list can therefore pass
> those trailing bytes to BAM, exceeding the programmed crypto segment and
> causing transfer errors or timeouts.
>
> Build a bounded source table for separately mapped input lists,
> referencing the original pages and limiting the final entry to the
> required length. Validate the total mapped input length against the
> configured segment. Include the authentication tag for CCM decryption,
> where hardware consumes it, but exclude it for authenc decryption, where
> software verifies it.
>
> Handle the zero return from dma_map_sg() as a mapping failure and
> release the allocated scatterlist tables on completion and preparation
> or submission errors.
>
> Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
[Severity: Low]
Since this fixes AEAD DMA mapping bugs, should it include a Fixes tag for
9363efb4181c ("crypto: qce - Add support for AEAD algorithms")?
> diff --git a/drivers/crypto/qce/aead.c b/drivers/crypto/qce/aead.c
> index 2065c46162c21..793913b33da6f 100644
> --- a/drivers/crypto/qce/aead.c
> +++ b/drivers/crypto/qce/aead.c
[ ... ]
> @@ -336,8 +350,9 @@ static int qce_aead_ccm_prepare_buf(struct aead_request *req)
>
> cryptlen = rctx->cryptlen + ctx->authsize;
> if (diff_dst) {
> - rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
> - rctx->src_sg = req->src;
> + ret = qce_aead_prepare_src_buf(req, cryptlen);
> + if (ret)
> + return ret;
> sg = qce_aead_prepare_dst_buf(req);
> if (IS_ERR(sg))
> return PTR_ERR(sg);
[Severity: Medium]
Does the !diff_dst branch just below this leave rctx->src_sg and
rctx->dst_sg unbounded for in-place CCM decryption with zero assoclen?
drivers/crypto/qce/aead.c:qce_aead_ccm_prepare_buf() {
...
} else {
rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
rctx->src_sg = req->src;
rctx->dst_nents = rctx->src_nents;
rctx->dst_sg = rctx->src_sg;
}
...
}
Because req->src is used directly without clamping the final entry to
cryptlen, any trailing bytes in the scatterlist remain mapped, causing the
new qce_aead_check_input() check to see dma_len != expected and reject the
request with -EINVAL. The !diff_dst branch also skips checking for a
negative return from sg_nents_for_len(), which would pass -EINVAL as nents
to dma_map_sg() if req->src is shorter than cryptlen.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-codex-qce-dma-bounds-upstream-20261201-v1-0-c15d01b45bce@oss.qualcomm.com?part=1
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/4] crypto: qce - Size CCM decrypt output DMA for the tag length
2026-12-01 2:54 ` [PATCH 2/4] crypto: qce - Size CCM decrypt output DMA for the tag length Wenjia Zhang
@ 2026-12-01 3:13 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-01 3:13 UTC (permalink / raw)
To: Wenjia Zhang, Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-crypto, Wenjia Zhang
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] crypto: qce: missing Fixes trailer for CCM decrypt output DMA
sizing fix
--
commit 5f10f5fde7f214005300d6bdb48de54c32de881c
Author: Wenjia Zhang <wenjz@qti.qualcomm.com>
> crypto: qce - Size CCM decrypt output DMA for the tag length
>
> In BAM mode, CCM decryption emits the plaintext followed by zero bytes
> equal in length to the input authentication tag. The driver reserves a
> full 64-byte burst for that tail. For a 32-byte payload and a 16-byte
> tag, it requests 96 output bytes although the engine produces only 48.
> Output DMA then waits for bytes that will never arrive.
>
> Use authsize for the CCM decryption tail instead of QCE_BAM_BURST_SIZE.
> Keep the burst-sized tail for encryption, where the generated tag is
> padded to a burst boundary.
>
> Validate the total mapped CCM output length before starting the request,
> accounting for formatted associated data, the payload and the
> direction-specific tag or padding length.
>
> Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
[Severity: Low]
This looks like a bug fix for CCM decryption output DMA sizing. Should
this include:
Fixes: 9363efb4181c ("crypto: qce - Add support for AEAD algorithms")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201-codex-qce-dma-bounds-upstream-20261201-v1-0-c15d01b45bce@oss.qualcomm.com?part=2
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment
2026-12-01 2:54 ` [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment Wenjia Zhang
2026-12-01 3:06 ` sashiko-bot
@ 2026-12-01 5:06 ` Kuldeep Singh
1 sibling, 0 replies; 10+ messages in thread
From: Kuldeep Singh @ 2026-12-01 5:06 UTC (permalink / raw)
To: Wenjia Zhang, Bartosz Golaszewski, Herbert Xu, David S. Miller
Cc: linux-arm-msm, linux-crypto, Wenjia Zhang
On 01-12-2026 08:24, Wenjia Zhang wrote:
> From: Wenjia Zhang <wenjz@qti.qualcomm.com>
Please check below.
>
> An AEAD source scatterlist can contain bytes beyond the input segment,
> such as space reserved for an output authentication tag.
> sg_nents_for_len() counts the entries covering a length but does not
> shorten the final entry. Mapping the original list can therefore pass
> those trailing bytes to BAM, exceeding the programmed crypto segment and
> causing transfer errors or timeouts.
>
> Build a bounded source table for separately mapped input lists,
> referencing the original pages and limiting the final entry to the
> required length. Validate the total mapped input length against the
> configured segment. Include the authentication tag for CCM decryption,
> where hardware consumes it, but exclude it for authenc decryption, where
> software verifies it.
>
> Handle the zero return from dma_map_sg() as a mapping failure and
> release the allocated scatterlist tables on completion and preparation
> or submission errors.
>
> Signed-off-by: Wenjia Zhang <wenjz@qti.qualcomm.com>
Please fix your S-o-b/From email address for all the submissions.
No submissions should be from qti email.
Did you run full selftests and userspace kcapi_convenience test both?
> ---
> drivers/crypto/qce/aead.c | 117 +++++++++++++++++++++++++++++++---------------
> 1 file changed, 79 insertions(+), 38 deletions(-)
>
> diff --git a/drivers/crypto/qce/aead.c b/drivers/crypto/qce/aead.c
> index 2065c46162c217484016021bbe3ee2f2db082e65..793913b33da6fc01f96a1e7612807f5e5d413439 100644
> --- a/drivers/crypto/qce/aead.c
> +++ b/drivers/crypto/qce/aead.c
> @@ -48,18 +48,8 @@ static void qce_aead_done(void *data)
>
> dma_unmap_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
>
> - if (IS_CCM(rctx->flags)) {
> - if (req->assoclen) {
> - sg_free_table(&rctx->src_tbl);
> - if (diff_dst)
> - sg_free_table(&rctx->dst_tbl);
> - } else {
> - if (!(IS_DECRYPT(rctx->flags) && !diff_dst))
> - sg_free_table(&rctx->dst_tbl);
> - }
> - } else {
> - sg_free_table(&rctx->dst_tbl);
> - }
> + sg_free_table(&rctx->src_tbl);
> + sg_free_table(&rctx->dst_tbl);
>
> error = qce_check_status(qce, &status);
> if (error < 0 && (error != -EBADMSG))
> @@ -291,11 +281,40 @@ qce_aead_ccm_prepare_buf_assoclen(struct aead_request *req)
> return ret;
> }
>
> +static int qce_aead_prepare_src_buf(struct aead_request *req,
> + unsigned int len)
> +{
> + struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
> + struct scatterlist *sg;
> + gfp_t gfp;
> + int ret;
> +
> + rctx->src_nents = sg_nents_for_len(req->src, len);
> + if (rctx->src_nents < 0)
> + return rctx->src_nents;
> +
> + gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
> + GFP_KERNEL : GFP_ATOMIC;
> + ret = sg_alloc_table(&rctx->src_tbl, rctx->src_nents, gfp);
> + if (ret)
> + return ret;
> +
> + /* The last entry may include tag space or unrelated trailing bytes. */
> + sg = qce_sgtable_add(&rctx->src_tbl, req->src, len);
> + if (IS_ERR(sg)) {
> + ret = PTR_ERR(sg);
> + sg_free_table(&rctx->src_tbl);
> + return ret;
> + }
> + sg_mark_end(sg);
> + rctx->src_sg = rctx->src_tbl.sgl;
> +
> + return 0;
> +}
> +
> static int qce_aead_prepare_buf(struct aead_request *req)
> {
> struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
> - struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
> - struct qce_device *qce = tmpl->qce;
> struct scatterlist *sg;
> bool diff_dst = (req->src != req->dst) ? true : false;
> unsigned int totallen;
> @@ -305,17 +324,11 @@ static int qce_aead_prepare_buf(struct aead_request *req)
> sg = qce_aead_prepare_dst_buf(req);
> if (IS_ERR(sg))
> return PTR_ERR(sg);
> - if (diff_dst) {
> - rctx->src_nents = sg_nents_for_len(req->src, totallen);
> - if (rctx->src_nents < 0) {
> - dev_err(qce->dev, "Invalid numbers of src SG.\n");
> - return -EINVAL;
> - }
> - rctx->src_sg = req->src;
> - } else {
> - rctx->src_nents = rctx->dst_nents - 1;
> - rctx->src_sg = rctx->dst_sg;
> - }
> + if (diff_dst)
> + return qce_aead_prepare_src_buf(req, totallen);
> +
> + rctx->src_nents = rctx->dst_nents - 1;
> + rctx->src_sg = rctx->dst_sg;
> return 0;
> }
>
> @@ -327,6 +340,7 @@ static int qce_aead_ccm_prepare_buf(struct aead_request *req)
> struct scatterlist *sg;
> bool diff_dst = (req->src != req->dst) ? true : false;
> unsigned int cryptlen;
> + int ret;
>
> if (rctx->assoclen)
> return qce_aead_ccm_prepare_buf_assoclen(req);
> @@ -336,8 +350,9 @@ static int qce_aead_ccm_prepare_buf(struct aead_request *req)
>
> cryptlen = rctx->cryptlen + ctx->authsize;
> if (diff_dst) {
> - rctx->src_nents = sg_nents_for_len(req->src, cryptlen);
> - rctx->src_sg = req->src;
> + ret = qce_aead_prepare_src_buf(req, cryptlen);
> + if (ret)
> + return ret;
> sg = qce_aead_prepare_dst_buf(req);
> if (IS_ERR(sg))
> return PTR_ERR(sg);
> @@ -397,6 +412,30 @@ static int qce_aead_create_ccm_nonce(struct qce_aead_reqctx *rctx, struct qce_ae
> return 0;
> }
>
> +static int qce_aead_check_input(struct aead_request *req, int mapped_nents)
> +{
> + struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
> + struct qce_aead_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
> + struct scatterlist *sg;
> + u64 dma_len = 0;
> + unsigned int expected = rctx->cryptlen + rctx->assoclen;
> + int nents, i, ret;
> +
> + /* CCM decrypt consumes the tag; authenc verifies it in software. */
> + if (IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags))
> + expected += ctx->authsize;
> +
> + nents = sg_nents_for_len(req->src, req->assoclen + req->cryptlen);
> + if (nents < 0)
> + return nents;
> + for_each_sg(rctx->src_sg, sg, mapped_nents, i)
> + dma_len += sg_dma_len(sg);
> +
> + ret = dma_len == expected ? 0 : -EINVAL;
> +
> + return ret;
> +}
> +
> static int
> qce_aead_async_req_handle(struct crypto_async_request *async_req)
> {
> @@ -410,6 +449,9 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
> bool diff_dst;
> int dst_nents, src_nents, ret;
>
> + rctx->src_tbl.sgl = NULL;
> + rctx->dst_tbl.sgl = NULL;
> +
> if (IS_CCM_RFC4309(rctx->flags)) {
> memset(rctx->ccm_rfc4309_iv, 0, QCE_MAX_IV_SIZE);
> rctx->ccm_rfc4309_iv[0] = 3;
> @@ -441,7 +483,7 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
> ret = qce_aead_prepare_buf(req);
>
> if (ret)
> - return ret;
> + goto error_free;
> dst_nents = dma_map_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
> if (!dst_nents) {
> ret = -EIO;
> @@ -450,8 +492,8 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
>
> if (diff_dst) {
> src_nents = dma_map_sg(qce->dma_dev, rctx->src_sg, rctx->src_nents, dir_src);
> - if (src_nents < 0) {
> - ret = src_nents;
> + if (!src_nents) {
> + ret = -EIO;
> goto error_unmap_dst;
> }
> } else {
> @@ -461,6 +503,10 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
> src_nents = dst_nents - 1;
> }
>
> + ret = qce_aead_check_input(req, src_nents);
> + if (ret)
> + goto error_unmap_src;
> +
> ret = qce_dma_prep_sgs(&qce->dma, rctx->src_sg, src_nents, rctx->dst_sg, dst_nents,
> qce_aead_done, async_req);
> if (ret)
> @@ -478,17 +524,12 @@ qce_aead_async_req_handle(struct crypto_async_request *async_req)
> qce_dma_terminate_all(&qce->dma);
> error_unmap_src:
> if (diff_dst)
> - dma_unmap_sg(qce->dma_dev, req->src, rctx->src_nents, dir_src);
> + dma_unmap_sg(qce->dma_dev, rctx->src_sg, rctx->src_nents, dir_src);
> error_unmap_dst:
> dma_unmap_sg(qce->dma_dev, rctx->dst_sg, rctx->dst_nents, dir_dst);
> error_free:
> - if (IS_CCM(rctx->flags) && rctx->assoclen) {
> - sg_free_table(&rctx->src_tbl);
> - if (diff_dst)
> - sg_free_table(&rctx->dst_tbl);
> - } else {
> - sg_free_table(&rctx->dst_tbl);
> - }
> + sg_free_table(&rctx->src_tbl);
> + sg_free_table(&rctx->dst_tbl);
> return ret;
> }
>
>
--
Regards
Kuldeep
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-12-01 5:06 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 2:54 [PATCH 0/4] QCE data DMA transfer length fixes Wenjia Zhang
2026-12-01 2:54 ` [PATCH 1/4] crypto: qce - Bound AEAD input DMA to the programmed segment Wenjia Zhang
2026-12-01 3:06 ` sashiko-bot
2026-12-01 5:06 ` Kuldeep Singh
2026-12-01 2:54 ` [PATCH 2/4] crypto: qce - Size CCM decrypt output DMA for the tag length Wenjia Zhang
2026-12-01 3:13 ` sashiko-bot
2026-12-01 2:54 ` [PATCH 3/4] crypto: qce - Bound ahash input DMA to the current segment Wenjia Zhang
2026-12-01 3:02 ` sashiko-bot
2026-12-01 2:54 ` [PATCH 4/4] crypto: qce - Bound skcipher input DMA to cryptlen Wenjia Zhang
2026-12-01 3:01 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox