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

From: Changwei Zou

Date: Sat Nov 14 2026 - 01:02:03 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 the priv_form
switch into rsa_priv_unmap() and the public/private dispatch into
rsa_pdb_unmap(), so rsa_priv_f_done() and both error paths share one
implementation.

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 | 66 +++++++++++++++++++++--------------
1 file changed, 39 insertions(+), 27 deletions(-)

diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
index cb001aa1de66..789a8d571486 100644
--- a/drivers/crypto/caam/caampkc.c
+++ b/drivers/crypto/caam/caampkc.c
@@ -119,6 +119,23 @@ static void rsa_priv_f3_unmap(struct device *dev, struct rsa_edesc *edesc,
dma_unmap_single(dev, pdb->tmp2_dma, q_sz, DMA_BIDIRECTIONAL);
}

+static inline void rsa_priv_unmap(struct device *dev, struct rsa_edesc *edesc,
+ struct akcipher_request *req,
+ struct caam_rsa_key *key)
+{
+ switch (key->priv_form) {
+ case FORM1:
+ rsa_priv_f1_unmap(dev, edesc, req);
+ break;
+ case FORM2:
+ rsa_priv_f2_unmap(dev, edesc, req);
+ break;
+ case FORM3:
+ rsa_priv_f3_unmap(dev, edesc, req);
+ break;
+ }
+}
+
/* RSA Job Completion handler */
static void rsa_pub_done(struct device *dev, u32 *desc, u32 err, void *context)
{
@@ -168,16 +185,7 @@ static void rsa_priv_f_done(struct device *dev, u32 *desc, u32 err,
edesc = req_ctx->edesc;
has_bklog = edesc->bklog;

- switch (key->priv_form) {
- case FORM1:
- rsa_priv_f1_unmap(dev, edesc, req);
- break;
- case FORM2:
- rsa_priv_f2_unmap(dev, edesc, req);
- break;
- case FORM3:
- rsa_priv_f3_unmap(dev, edesc, req);
- }
+ rsa_priv_unmap(dev, edesc, req, key);

rsa_io_unmap(dev, edesc, req);
kfree(edesc);
@@ -192,6 +200,22 @@ 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;
+
+ rsa_priv_unmap(jrdev, edesc, req, key);
+ }
+}
+
/**
* caam_rsa_count_leading_zeros - Count leading zeros, need it to strip,
* from a given scatterlist
@@ -391,7 +415,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 +695,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 +713,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 +1002,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