[PATCH v5 1/2] crypto: caam - Fix wrong unmap operations

From: Changwei Zou

Date: Fri Nov 13 2026 - 07:34:14 EST


Since priv_form only takes values FORM1/FORM2/FORM3 with no distinct
public key enumerator, rsa_pub_unmap() was not called correctly in
akcipher_do_one_req() and akcipher_enqueue_req().

Use the completion callback (rsa_pub_done vs rsa_priv_f_done) to
distinguish public from private key operations, then dispatch to the
correct unmap function based on key->priv_form. Factor this dispatch into
a shared rsa_pdb_unmap() helper so both error paths stay in sync.

caam_rsa_set_priv_key_form() implicitly relies on zero-initialization
for priv_form. Set priv_form = FORM1 explicitly at the head of the function
for clarity and robustness.

Fixes: bf53795025a2 ("crypto: caam - add crypto_engine support for RSA algorithms")
Signed-off-by: Changwei Zou <changwei.zou@xxxxxxxxxxxxx>
Assisted-by: OpenCode:claude-sonnet-4.6
Reviewed-by: Sahil Malhotra <sahil.malhotra@xxxxxxx>
---
drivers/crypto/caam/caampkc.c | 48 ++++++++++++++++++++++-------------
1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
index cb001aa1de66..24c740cc4a8c 100644
--- a/drivers/crypto/caam/caampkc.c
+++ b/drivers/crypto/caam/caampkc.c
@@ -192,6 +192,32 @@ static void rsa_priv_f_done(struct device *dev, u32 *desc, u32 err,
crypto_finalize_akcipher_request(jrp->engine, req, ecode);
}

+static void rsa_pdb_unmap(struct device *jrdev, struct rsa_edesc *edesc,
+ struct akcipher_request *req,
+ void (*cbk)(struct device *jrdev, u32 *desc,
+ u32 err, void *context))
+{
+ if (cbk == rsa_pub_done) {
+ rsa_pub_unmap(jrdev, edesc, req);
+ } else {
+ struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
+ struct caam_rsa_ctx *ctx = akcipher_tfm_ctx_dma(tfm);
+ struct caam_rsa_key *key = &ctx->key;
+
+ switch (key->priv_form) {
+ case FORM1:
+ rsa_priv_f1_unmap(jrdev, edesc, req);
+ break;
+ case FORM2:
+ rsa_priv_f2_unmap(jrdev, edesc, req);
+ break;
+ case FORM3:
+ rsa_priv_f3_unmap(jrdev, edesc, req);
+ break;
+ }
+ }
+}
+
/**
* caam_rsa_count_leading_zeros - Count leading zeros, need it to strip,
* from a given scatterlist
@@ -391,7 +417,8 @@ static int akcipher_do_one_req(struct crypto_engine *engine, void *areq)
return ret;

if (ret != -EINPROGRESS) {
- rsa_pub_unmap(jrdev, req_ctx->edesc, req);
+ rsa_pdb_unmap(jrdev, req_ctx->edesc, req,
+ req_ctx->akcipher_op_done);
rsa_io_unmap(jrdev, req_ctx->edesc, req);
kfree(req_ctx->edesc);
} else {
@@ -670,9 +697,6 @@ static int akcipher_enqueue_req(struct device *jrdev,
struct akcipher_request *req)
{
struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev);
- struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
- struct caam_rsa_ctx *ctx = akcipher_tfm_ctx_dma(tfm);
- struct caam_rsa_key *key = &ctx->key;
struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req);
struct rsa_edesc *edesc = req_ctx->edesc;
u32 *desc = edesc->hw_desc;
@@ -691,19 +715,7 @@ static int akcipher_enqueue_req(struct device *jrdev,
ret = caam_jr_enqueue(jrdev, desc, cbk, req);

if ((ret != -EINPROGRESS) && (ret != -EBUSY)) {
- switch (key->priv_form) {
- case FORM1:
- rsa_priv_f1_unmap(jrdev, edesc, req);
- break;
- case FORM2:
- rsa_priv_f2_unmap(jrdev, edesc, req);
- break;
- case FORM3:
- rsa_priv_f3_unmap(jrdev, edesc, req);
- break;
- default:
- rsa_pub_unmap(jrdev, edesc, req);
- }
+ rsa_pdb_unmap(jrdev, edesc, req, cbk);
rsa_io_unmap(jrdev, edesc, req);
kfree(edesc);
}
@@ -992,6 +1004,8 @@ static int caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx,
size_t q_sz = raw_key->q_sz;
unsigned aligned_size;

+ rsa_key->priv_form = FORM1;
+
rsa_key->p = caam_read_raw_data(raw_key->p, &p_sz);
if (!rsa_key->p)
return -ENOMEM;
--
2.43.0