浏览代码

[CRYPTO] api: Add crypto_alg reference counting

Up until now we've relied on module reference counting to ensure that the
crypto_alg structures don't disappear from under us.  This was good enough
as long as each crypto_alg came from exactly one module.

However, with parameterised crypto algorithms a crypto_alg object may need
two or more modules to operate.  This means that we need to count the
references to the crypto_alg object directly.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
Herbert Xu 19 年之前
父节点
当前提交
6521f30273
共有 3 个文件被更改,包括 32 次插入6 次删除
  1. 26 6
      crypto/api.c
  2. 3 0
      crypto/proc.c
  3. 3 0
      include/linux/crypto.h

+ 26 - 6
crypto/api.c

@@ -29,13 +29,26 @@
 LIST_HEAD(crypto_alg_list);
 LIST_HEAD(crypto_alg_list);
 DECLARE_RWSEM(crypto_alg_sem);
 DECLARE_RWSEM(crypto_alg_sem);
 
 
-static inline int crypto_mod_get(struct crypto_alg *alg)
+static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
 {
 {
-	return try_module_get(alg->cra_module);
+	atomic_inc(&alg->cra_refcnt);
+	return alg;
+}
+
+static inline void crypto_alg_put(struct crypto_alg *alg)
+{
+	if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
+		alg->cra_destroy(alg);
+}
+
+static struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
+{
+	return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
 }
 }
 
 
-static inline void crypto_mod_put(struct crypto_alg *alg)
+static void crypto_mod_put(struct crypto_alg *alg)
 {
 {
+	crypto_alg_put(alg);
 	module_put(alg->cra_module);
 	module_put(alg->cra_module);
 }
 }
 
 
@@ -274,6 +287,7 @@ int crypto_register_alg(struct crypto_alg *alg)
 	}
 	}
 	
 	
 	list_add(&alg->cra_list, &crypto_alg_list);
 	list_add(&alg->cra_list, &crypto_alg_list);
+	atomic_set(&alg->cra_refcnt, 1);
 out:	
 out:	
 	up_write(&crypto_alg_sem);
 	up_write(&crypto_alg_sem);
 	return ret;
 	return ret;
@@ -284,8 +298,6 @@ int crypto_unregister_alg(struct crypto_alg *alg)
 	int ret = -ENOENT;
 	int ret = -ENOENT;
 	struct crypto_alg *q;
 	struct crypto_alg *q;
 	
 	
-	BUG_ON(!alg->cra_module);
-	
 	down_write(&crypto_alg_sem);
 	down_write(&crypto_alg_sem);
 	list_for_each_entry(q, &crypto_alg_list, cra_list) {
 	list_for_each_entry(q, &crypto_alg_list, cra_list) {
 		if (alg == q) {
 		if (alg == q) {
@@ -296,7 +308,15 @@ int crypto_unregister_alg(struct crypto_alg *alg)
 	}
 	}
 out:	
 out:	
 	up_write(&crypto_alg_sem);
 	up_write(&crypto_alg_sem);
-	return ret;
+
+	if (ret)
+		return ret;
+
+	BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
+	if (alg->cra_destroy)
+		alg->cra_destroy(alg);
+
+	return 0;
 }
 }
 
 
 int crypto_alg_available(const char *name, u32 flags)
 int crypto_alg_available(const char *name, u32 flags)

+ 3 - 0
crypto/proc.c

@@ -12,6 +12,8 @@
  * any later version.
  * any later version.
  *
  *
  */
  */
+
+#include <asm/atomic.h>
 #include <linux/init.h>
 #include <linux/init.h>
 #include <linux/crypto.h>
 #include <linux/crypto.h>
 #include <linux/rwsem.h>
 #include <linux/rwsem.h>
@@ -54,6 +56,7 @@ static int c_show(struct seq_file *m, void *p)
 	seq_printf(m, "driver       : %s\n", alg->cra_driver_name);
 	seq_printf(m, "driver       : %s\n", alg->cra_driver_name);
 	seq_printf(m, "module       : %s\n", module_name(alg->cra_module));
 	seq_printf(m, "module       : %s\n", module_name(alg->cra_module));
 	seq_printf(m, "priority     : %d\n", alg->cra_priority);
 	seq_printf(m, "priority     : %d\n", alg->cra_priority);
+	seq_printf(m, "refcnt       : %d\n", atomic_read(&alg->cra_refcnt));
 	
 	
 	switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
 	switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
 	case CRYPTO_ALG_TYPE_CIPHER:
 	case CRYPTO_ALG_TYPE_CIPHER:

+ 3 - 0
include/linux/crypto.h

@@ -17,6 +17,7 @@
 #ifndef _LINUX_CRYPTO_H
 #ifndef _LINUX_CRYPTO_H
 #define _LINUX_CRYPTO_H
 #define _LINUX_CRYPTO_H
 
 
+#include <asm/atomic.h>
 #include <linux/module.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/types.h>
@@ -148,6 +149,7 @@ struct crypto_alg {
 	unsigned int cra_alignmask;
 	unsigned int cra_alignmask;
 
 
 	int cra_priority;
 	int cra_priority;
+	atomic_t cra_refcnt;
 
 
 	char cra_name[CRYPTO_MAX_ALG_NAME];
 	char cra_name[CRYPTO_MAX_ALG_NAME];
 	char cra_driver_name[CRYPTO_MAX_ALG_NAME];
 	char cra_driver_name[CRYPTO_MAX_ALG_NAME];
@@ -160,6 +162,7 @@ struct crypto_alg {
 
 
 	int (*cra_init)(struct crypto_tfm *tfm);
 	int (*cra_init)(struct crypto_tfm *tfm);
 	void (*cra_exit)(struct crypto_tfm *tfm);
 	void (*cra_exit)(struct crypto_tfm *tfm);
+	void (*cra_destroy)(struct crypto_alg *alg);
 	
 	
 	struct module *cra_module;
 	struct module *cra_module;
 };
 };