crypto: cryptd - simplify error handling in cryptd_create_*()
authorEric Biggers <ebiggers@google.com>
Wed, 26 Feb 2020 04:59:15 +0000 (20:59 -0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 6 Mar 2020 01:28:22 +0000 (12:28 +1100)
Simplify the error handling in the various cryptd_create_*() functions
by taking advantage of crypto_grab_*() now handling an ERR_PTR() name
and by taking advantage of crypto_drop_*() now accepting (as a no-op) a
spawn that hasn't been grabbed yet.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/cryptd.c

index d94c75c..2832122 100644 (file)
@@ -369,7 +369,6 @@ static int cryptd_create_skcipher(struct crypto_template *tmpl,
        struct skcipherd_instance_ctx *ctx;
        struct skcipher_instance *inst;
        struct skcipher_alg *alg;
-       const char *name;
        u32 type;
        u32 mask;
        int err;
@@ -379,10 +378,6 @@ static int cryptd_create_skcipher(struct crypto_template *tmpl,
 
        cryptd_check_internal(tb, &type, &mask);
 
-       name = crypto_attr_alg_name(tb[1]);
-       if (IS_ERR(name))
-               return PTR_ERR(name);
-
        inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
        if (!inst)
                return -ENOMEM;
@@ -391,14 +386,14 @@ static int cryptd_create_skcipher(struct crypto_template *tmpl,
        ctx->queue = queue;
 
        err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
-                                  name, type, mask);
+                                  crypto_attr_alg_name(tb[1]), type, mask);
        if (err)
-               goto out_free_inst;
+               goto err_free_inst;
 
        alg = crypto_spawn_skcipher_alg(&ctx->spawn);
        err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
        if (err)
-               goto out_drop_skcipher;
+               goto err_free_inst;
 
        inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
                                   (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
@@ -421,10 +416,8 @@ static int cryptd_create_skcipher(struct crypto_template *tmpl,
 
        err = skcipher_register_instance(tmpl, inst);
        if (err) {
-out_drop_skcipher:
-               crypto_drop_skcipher(&ctx->spawn);
-out_free_inst:
-               kfree(inst);
+err_free_inst:
+               cryptd_skcipher_free(inst);
        }
        return err;
 }
@@ -694,8 +687,7 @@ static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
        err = ahash_register_instance(tmpl, inst);
        if (err) {
 err_free_inst:
-               crypto_drop_shash(&ctx->spawn);
-               kfree(inst);
+               cryptd_hash_free(inst);
        }
        return err;
 }
@@ -833,17 +825,12 @@ static int cryptd_create_aead(struct crypto_template *tmpl,
        struct aead_instance_ctx *ctx;
        struct aead_instance *inst;
        struct aead_alg *alg;
-       const char *name;
        u32 type = 0;
        u32 mask = CRYPTO_ALG_ASYNC;
        int err;
 
        cryptd_check_internal(tb, &type, &mask);
 
-       name = crypto_attr_alg_name(tb[1]);
-       if (IS_ERR(name))
-               return PTR_ERR(name);
-
        inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
        if (!inst)
                return -ENOMEM;
@@ -852,14 +839,14 @@ static int cryptd_create_aead(struct crypto_template *tmpl,
        ctx->queue = queue;
 
        err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst),
-                              name, type, mask);
+                              crypto_attr_alg_name(tb[1]), type, mask);
        if (err)
-               goto out_free_inst;
+               goto err_free_inst;
 
        alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
        err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
        if (err)
-               goto out_drop_aead;
+               goto err_free_inst;
 
        inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
                                   (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
@@ -879,10 +866,8 @@ static int cryptd_create_aead(struct crypto_template *tmpl,
 
        err = aead_register_instance(tmpl, inst);
        if (err) {
-out_drop_aead:
-               crypto_drop_aead(&ctx->aead_spawn);
-out_free_inst:
-               kfree(inst);
+err_free_inst:
+               cryptd_aead_free(inst);
        }
        return err;
 }