Skip to content

Commit ae131f4

Browse files
committed
crypto: api - Add crypto_tfm_get
Add a crypto_tfm_get interface to allow tfm objects to be shared. They can still be freed in the usual way. This should only be done with tfm objects with no keys. You must also not modify the tfm flags in any way once it becomes shared. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Reviewed-by: Simon Horman <simon.horman@corigine.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 94330fb commit ae131f4

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

crypto/api.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
408408
goto out_err;
409409

410410
tfm->__crt_alg = alg;
411+
refcount_set(&tfm->refcnt, 1);
411412

412413
err = crypto_init_ops(tfm, type, mask);
413414
if (err)
@@ -507,6 +508,7 @@ void *crypto_create_tfm_node(struct crypto_alg *alg,
507508
tfm = (struct crypto_tfm *)(mem + tfmsize);
508509
tfm->__crt_alg = alg;
509510
tfm->node = node;
511+
refcount_set(&tfm->refcnt, 1);
510512

511513
err = frontend->init_tfm(tfm);
512514
if (err)
@@ -619,6 +621,8 @@ void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
619621
if (IS_ERR_OR_NULL(mem))
620622
return;
621623

624+
if (!refcount_dec_and_test(&tfm->refcnt))
625+
return;
622626
alg = tfm->__crt_alg;
623627

624628
if (!tfm->exit && alg->cra_exit)

crypto/internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <crypto/algapi.h>
1212
#include <linux/completion.h>
13+
#include <linux/err.h>
1314
#include <linux/jump_label.h>
1415
#include <linux/list.h>
1516
#include <linux/module.h>
@@ -186,5 +187,10 @@ static inline int crypto_is_test_larval(struct crypto_larval *larval)
186187
return larval->alg.cra_driver_name[0];
187188
}
188189

190+
static inline struct crypto_tfm *crypto_tfm_get(struct crypto_tfm *tfm)
191+
{
192+
return refcount_inc_not_zero(&tfm->refcnt) ? tfm : ERR_PTR(-EOVERFLOW);
193+
}
194+
189195
#endif /* _CRYPTO_INTERNAL_H */
190196

include/linux/crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ int crypto_has_alg(const char *name, u32 type, u32 mask);
419419
*/
420420

421421
struct crypto_tfm {
422+
refcount_t refcnt;
422423

423424
u32 crt_flags;
424425

0 commit comments

Comments
 (0)