]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
crypto: rng - Do not free default RNG when it becomes unused
authorHerbert Xu <herbert@gondor.apana.org.au>
Sun, 21 Jun 2015 11:11:43 +0000 (19:11 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Mon, 22 Jun 2015 07:49:18 +0000 (15:49 +0800)
Currently we free the default RNG when its use count hits zero.
This was OK when the IV generators would latch onto the RNG at
instance creation time and keep it until the instance is torn
down.

Now that IV generators only keep the RNG reference during init
time this scheme causes the default RNG to come and go at a high
frequencey.  This is highly undesirable as we want to keep a single
RNG in use unless the admin wants it to be removed.

This patch changes the scheme so that the system RNG once allocated
is never removed unless a specifically requested.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/rng.c
include/crypto/internal/rng.h

index 13155058b19317282c36b2a56261d74d28f8ab6a..b81cffb13babc35cc2203ce5355ac6a04b18c5c4 100644 (file)
@@ -155,14 +155,33 @@ EXPORT_SYMBOL_GPL(crypto_get_default_rng);
 void crypto_put_default_rng(void)
 {
        mutex_lock(&crypto_default_rng_lock);
-       if (!--crypto_default_rng_refcnt) {
-               crypto_free_rng(crypto_default_rng);
-               crypto_default_rng = NULL;
-       }
+       crypto_default_rng_refcnt--;
        mutex_unlock(&crypto_default_rng_lock);
 }
 EXPORT_SYMBOL_GPL(crypto_put_default_rng);
 
+#if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
+int crypto_del_default_rng(void)
+{
+       int err = -EBUSY;
+
+       mutex_lock(&crypto_default_rng_lock);
+       if (crypto_default_rng_refcnt)
+               goto out;
+
+       crypto_free_rng(crypto_default_rng);
+       crypto_default_rng = NULL;
+
+       err = 0;
+
+out:
+       mutex_unlock(&crypto_default_rng_lock);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(crypto_del_default_rng);
+#endif
+
 int crypto_register_rng(struct rng_alg *alg)
 {
        struct crypto_alg *base = &alg->base;
index 263f1a5eebc7070122ab7bae111c8a0d4bee8265..a52ef3483dd75032134c2d2b0785f76520e2cceb 100644 (file)
@@ -22,6 +22,15 @@ void crypto_unregister_rng(struct rng_alg *alg);
 int crypto_register_rngs(struct rng_alg *algs, int count);
 void crypto_unregister_rngs(struct rng_alg *algs, int count);
 
+#if defined(CONFIG_CRYPTO_RNG) || defined(CONFIG_CRYPTO_RNG_MODULE)
+int crypto_del_default_rng(void);
+#else
+static inline int crypto_del_default_rng(void)
+{
+       return 0;
+}
+#endif
+
 static inline void *crypto_rng_ctx(struct crypto_rng *tfm)
 {
        return crypto_tfm_ctx(&tfm->base);