diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2006-08-06 06:28:44 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2006-09-20 21:16:29 -0400 |
commit | 6521f30273fbec65146a0f16de74b7b402b0f7b0 (patch) | |
tree | 1e664f6c1a7c960c60c4cae01585933029f81a5f | |
parent | 72fa491912689ca69dd15f4266945d2c2f2819f8 (diff) |
[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>
-rw-r--r-- | crypto/api.c | 32 | ||||
-rw-r--r-- | crypto/proc.c | 3 | ||||
-rw-r--r-- | include/linux/crypto.h | 3 |
3 files changed, 32 insertions, 6 deletions
diff --git a/crypto/api.c b/crypto/api.c index 8c2743a05f90..5994a58ef954 100644 --- a/crypto/api.c +++ b/crypto/api.c | |||
@@ -29,13 +29,26 @@ | |||
29 | LIST_HEAD(crypto_alg_list); | 29 | LIST_HEAD(crypto_alg_list); |
30 | DECLARE_RWSEM(crypto_alg_sem); | 30 | DECLARE_RWSEM(crypto_alg_sem); |
31 | 31 | ||
32 | static inline int crypto_mod_get(struct crypto_alg *alg) | 32 | static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) |
33 | { | 33 | { |
34 | return try_module_get(alg->cra_module); | 34 | atomic_inc(&alg->cra_refcnt); |
35 | return alg; | ||
36 | } | ||
37 | |||
38 | static inline void crypto_alg_put(struct crypto_alg *alg) | ||
39 | { | ||
40 | if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy) | ||
41 | alg->cra_destroy(alg); | ||
42 | } | ||
43 | |||
44 | static struct crypto_alg *crypto_mod_get(struct crypto_alg *alg) | ||
45 | { | ||
46 | return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL; | ||
35 | } | 47 | } |
36 | 48 | ||
37 | static inline void crypto_mod_put(struct crypto_alg *alg) | 49 | static void crypto_mod_put(struct crypto_alg *alg) |
38 | { | 50 | { |
51 | crypto_alg_put(alg); | ||
39 | module_put(alg->cra_module); | 52 | module_put(alg->cra_module); |
40 | } | 53 | } |
41 | 54 | ||
@@ -274,6 +287,7 @@ int crypto_register_alg(struct crypto_alg *alg) | |||
274 | } | 287 | } |
275 | 288 | ||
276 | list_add(&alg->cra_list, &crypto_alg_list); | 289 | list_add(&alg->cra_list, &crypto_alg_list); |
290 | atomic_set(&alg->cra_refcnt, 1); | ||
277 | out: | 291 | out: |
278 | up_write(&crypto_alg_sem); | 292 | up_write(&crypto_alg_sem); |
279 | return ret; | 293 | return ret; |
@@ -284,8 +298,6 @@ int crypto_unregister_alg(struct crypto_alg *alg) | |||
284 | int ret = -ENOENT; | 298 | int ret = -ENOENT; |
285 | struct crypto_alg *q; | 299 | struct crypto_alg *q; |
286 | 300 | ||
287 | BUG_ON(!alg->cra_module); | ||
288 | |||
289 | down_write(&crypto_alg_sem); | 301 | down_write(&crypto_alg_sem); |
290 | list_for_each_entry(q, &crypto_alg_list, cra_list) { | 302 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
291 | if (alg == q) { | 303 | if (alg == q) { |
@@ -296,7 +308,15 @@ int crypto_unregister_alg(struct crypto_alg *alg) | |||
296 | } | 308 | } |
297 | out: | 309 | out: |
298 | up_write(&crypto_alg_sem); | 310 | up_write(&crypto_alg_sem); |
299 | return ret; | 311 | |
312 | if (ret) | ||
313 | return ret; | ||
314 | |||
315 | BUG_ON(atomic_read(&alg->cra_refcnt) != 1); | ||
316 | if (alg->cra_destroy) | ||
317 | alg->cra_destroy(alg); | ||
318 | |||
319 | return 0; | ||
300 | } | 320 | } |
301 | 321 | ||
302 | int crypto_alg_available(const char *name, u32 flags) | 322 | int crypto_alg_available(const char *name, u32 flags) |
diff --git a/crypto/proc.c b/crypto/proc.c index c0a5dd7ce2cc..8543b7a157d6 100644 --- a/crypto/proc.c +++ b/crypto/proc.c | |||
@@ -12,6 +12,8 @@ | |||
12 | * any later version. | 12 | * any later version. |
13 | * | 13 | * |
14 | */ | 14 | */ |
15 | |||
16 | #include <asm/atomic.h> | ||
15 | #include <linux/init.h> | 17 | #include <linux/init.h> |
16 | #include <linux/crypto.h> | 18 | #include <linux/crypto.h> |
17 | #include <linux/rwsem.h> | 19 | #include <linux/rwsem.h> |
@@ -54,6 +56,7 @@ static int c_show(struct seq_file *m, void *p) | |||
54 | seq_printf(m, "driver : %s\n", alg->cra_driver_name); | 56 | seq_printf(m, "driver : %s\n", alg->cra_driver_name); |
55 | seq_printf(m, "module : %s\n", module_name(alg->cra_module)); | 57 | seq_printf(m, "module : %s\n", module_name(alg->cra_module)); |
56 | seq_printf(m, "priority : %d\n", alg->cra_priority); | 58 | seq_printf(m, "priority : %d\n", alg->cra_priority); |
59 | seq_printf(m, "refcnt : %d\n", atomic_read(&alg->cra_refcnt)); | ||
57 | 60 | ||
58 | switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { | 61 | switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { |
59 | case CRYPTO_ALG_TYPE_CIPHER: | 62 | case CRYPTO_ALG_TYPE_CIPHER: |
diff --git a/include/linux/crypto.h b/include/linux/crypto.h index cb1e6631b132..7f57ff8ec975 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h | |||
@@ -17,6 +17,7 @@ | |||
17 | #ifndef _LINUX_CRYPTO_H | 17 | #ifndef _LINUX_CRYPTO_H |
18 | #define _LINUX_CRYPTO_H | 18 | #define _LINUX_CRYPTO_H |
19 | 19 | ||
20 | #include <asm/atomic.h> | ||
20 | #include <linux/module.h> | 21 | #include <linux/module.h> |
21 | #include <linux/kernel.h> | 22 | #include <linux/kernel.h> |
22 | #include <linux/types.h> | 23 | #include <linux/types.h> |
@@ -148,6 +149,7 @@ struct crypto_alg { | |||
148 | unsigned int cra_alignmask; | 149 | unsigned int cra_alignmask; |
149 | 150 | ||
150 | int cra_priority; | 151 | int cra_priority; |
152 | atomic_t cra_refcnt; | ||
151 | 153 | ||
152 | char cra_name[CRYPTO_MAX_ALG_NAME]; | 154 | char cra_name[CRYPTO_MAX_ALG_NAME]; |
153 | char cra_driver_name[CRYPTO_MAX_ALG_NAME]; | 155 | char cra_driver_name[CRYPTO_MAX_ALG_NAME]; |
@@ -160,6 +162,7 @@ struct crypto_alg { | |||
160 | 162 | ||
161 | int (*cra_init)(struct crypto_tfm *tfm); | 163 | int (*cra_init)(struct crypto_tfm *tfm); |
162 | void (*cra_exit)(struct crypto_tfm *tfm); | 164 | void (*cra_exit)(struct crypto_tfm *tfm); |
165 | void (*cra_destroy)(struct crypto_alg *alg); | ||
163 | 166 | ||
164 | struct module *cra_module; | 167 | struct module *cra_module; |
165 | }; | 168 | }; |