Commit 
1b46c90c8e00 ("crypto: caam - convert top level drivers to libraries")
changed entry and exit points behavior for caamalg,
caamalg_qi, caamalg_qi2, caamhash, caampkc, caamrng.
For example, previously caam_pkc_init() and caam_pkc_exit() were
module entry/exit points. This means that if an error would happen
in caam_pkc_init(), then caam_pkc_exit() wouldn't have been called.
After the mentioned commit, caam_pkc_init() and caam_pkc_exit()
are manually called - from jr.c. caam_pkc_exit() is called
unconditionally, even if caam_pkc_init() failed.
Added a global variable to keep the status of the algorithm
registration and free of resources.
The exit point of caampkc/caamrng module is executed only if the
registration was successful. Therefore we avoid double free of
resources in case the algorithm registration failed.
Fixes: 1b46c90c8e00 ("crypto: caam - convert top level drivers to libraries")
Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com>
Reviewed-by: Horia Geanta <horia.geanta@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
 /* buffer filled with zeros, used for padding */
 static u8 *zero_buffer;
 
+/*
+ * variable used to avoid double free of resources in case
+ * algorithm registration was unsuccessful
+ */
+static bool init_done;
+
 static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc,
                         struct akcipher_request *req)
 {
        struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
        u32 pk_inst;
        int err;
+       init_done = false;
 
        /* Determine public key hardware accelerator presence. */
        if (priv->era < 10)
                dev_warn(ctrldev, "%s alg registration failed\n",
                         caam_rsa.base.cra_driver_name);
        } else {
+               init_done = true;
                dev_info(ctrldev, "caam pkc algorithms registered in /proc/crypto\n");
        }
 
 
 void caam_pkc_exit(void)
 {
+       if (!init_done)
+               return;
+
        kfree(zero_buffer);
        crypto_unregister_akcipher(&caam_rsa);
 }
 
 
 static struct caam_rng_ctx *rng_ctx;
 
+/*
+ * Variable used to avoid double free of resources in case
+ * algorithm registration was unsuccessful
+ */
+static bool init_done;
+
 static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd)
 {
        if (bd->addr)
 
 void caam_rng_exit(void)
 {
+       if (!init_done)
+               return;
+
        caam_jr_free(rng_ctx->jrdev);
        hwrng_unregister(&caam_rng);
        kfree(rng_ctx);
        u32 rng_inst;
        struct caam_drv_private *priv = dev_get_drvdata(ctrldev);
        int err;
+       init_done = false;
 
        /* Check for an instantiated RNG before registration */
        if (priv->era < 10)
        dev_info(dev, "registering rng-caam\n");
 
        err = hwrng_register(&caam_rng);
-       if (!err)
+       if (!err) {
+               init_done = true;
                return err;
+       }
 
 free_rng_ctx:
        kfree(rng_ctx);