aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/api.c
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2006-08-06 06:28:44 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2006-09-20 21:16:29 -0400
commit6521f30273fbec65146a0f16de74b7b402b0f7b0 (patch)
tree1e664f6c1a7c960c60c4cae01585933029f81a5f /crypto/api.c
parent72fa491912689ca69dd15f4266945d2c2f2819f8 (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>
Diffstat (limited to 'crypto/api.c')
-rw-r--r--crypto/api.c32
1 files changed, 26 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 @@
29LIST_HEAD(crypto_alg_list); 29LIST_HEAD(crypto_alg_list);
30DECLARE_RWSEM(crypto_alg_sem); 30DECLARE_RWSEM(crypto_alg_sem);
31 31
32static inline int crypto_mod_get(struct crypto_alg *alg) 32static 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
38static 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
44static 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
37static inline void crypto_mod_put(struct crypto_alg *alg) 49static 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);
277out: 291out:
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 }
297out: 309out:
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
302int crypto_alg_available(const char *name, u32 flags) 322int crypto_alg_available(const char *name, u32 flags)