2018-02-14 18:32:40

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 0/2] crypto/caam: Adjustments for eight function implementations

From: Markus Elfring <[email protected]>
Date: Wed, 14 Feb 2018 19:23:45 +0100

Two update suggestions were taken into account
from static source code analysis.

Markus Elfring (2):
Delete an error message for a failed memory allocation in seven functions
Use common error handling code in four functions

drivers/crypto/caam/caamalg.c | 38 +++++++++++++++++---------------------
drivers/crypto/caam/caamhash.c | 35 +++++++++++++----------------------
drivers/crypto/caam/key_gen.c | 4 +---
3 files changed, 31 insertions(+), 46 deletions(-)

--
2.16.1



2018-02-14 18:33:02

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 1/2] crypto: caam: Delete an error message for a failed memory allocation in seven functions

From: Markus Elfring <[email protected]>
Date: Wed, 14 Feb 2018 18:22:38 +0100

Omit an extra message for a memory allocation failure in these functions.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <[email protected]>
---
drivers/crypto/caam/caamalg.c | 6 +-----
drivers/crypto/caam/caamhash.c | 12 +++---------
drivers/crypto/caam/key_gen.c | 4 +---
3 files changed, 5 insertions(+), 17 deletions(-)

diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c
index 2188235be02d..d1f25a90552a 100644
--- a/drivers/crypto/caam/caamalg.c
+++ b/drivers/crypto/caam/caamalg.c
@@ -1561,7 +1561,6 @@ static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request
edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
GFP_DMA | flags);
if (!edesc) {
- dev_err(jrdev, "could not allocate extended descriptor\n");
caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
iv_dma, ivsize, 0, 0);
return ERR_PTR(-ENOMEM);
@@ -1770,7 +1769,6 @@ static struct ablkcipher_edesc *ablkcipher_giv_edesc_alloc(
edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
GFP_DMA | flags);
if (!edesc) {
- dev_err(jrdev, "could not allocate extended descriptor\n");
caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
iv_dma, ivsize, 0, 0);
return ERR_PTR(-ENOMEM);
@@ -3372,10 +3370,8 @@ static struct caam_crypto_alg *caam_alg_alloc(struct caam_alg_template
struct crypto_alg *alg;

t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
- if (!t_alg) {
- pr_err("failed to allocate t_alg\n");
+ if (!t_alg)
return ERR_PTR(-ENOMEM);
- }

alg = &t_alg->crypto_alg;

diff --git a/drivers/crypto/caam/caamhash.c b/drivers/crypto/caam/caamhash.c
index 0beb28196e20..dc269eba08ad 100644
--- a/drivers/crypto/caam/caamhash.c
+++ b/drivers/crypto/caam/caamhash.c
@@ -362,10 +362,8 @@ static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
int ret;

desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
- if (!desc) {
- dev_err(jrdev, "unable to allocate key input memory\n");
+ if (!desc)
return -ENOMEM;
- }

init_job_desc(desc, 0);

@@ -689,10 +687,8 @@ static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);

edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
- if (!edesc) {
- dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
+ if (!edesc)
return NULL;
- }

init_job_desc_shared(edesc->hw_desc, sh_desc_dma, desc_len(sh_desc),
HDR_SHARE_DEFER | HDR_REVERSE);
@@ -1818,10 +1814,8 @@ caam_hash_alloc(struct caam_hash_template *template,
struct crypto_alg *alg;

t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
- if (!t_alg) {
- pr_err("failed to allocate t_alg\n");
+ if (!t_alg)
return ERR_PTR(-ENOMEM);
- }

t_alg->ahash_alg = template->template_ahash;
halg = &t_alg->ahash_alg;
diff --git a/drivers/crypto/caam/key_gen.c b/drivers/crypto/caam/key_gen.c
index 312b5f042f31..dd077ac8c41e 100644
--- a/drivers/crypto/caam/key_gen.c
+++ b/drivers/crypto/caam/key_gen.c
@@ -66,10 +66,8 @@ int gen_split_key(struct device *jrdev, u8 *key_out,
return -EINVAL;

desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
- if (!desc) {
- dev_err(jrdev, "unable to allocate key input memory\n");
+ if (!desc)
return ret;
- }

dma_addr_in = dma_map_single(jrdev, (void *)key_in, keylen,
DMA_TO_DEVICE);
--
2.16.1


2018-02-14 20:12:13

by SF Markus Elfring

[permalink] [raw]
Subject: [PATCH 2/2] crypto: caam: Use common error handling code in four functions

From: Markus Elfring <[email protected]>
Date: Wed, 14 Feb 2018 19:14:49 +0100

Add jump targets so that a bit of exception handling can be better reused
at the end of these functions.

Signed-off-by: Markus Elfring <[email protected]>
---
drivers/crypto/caam/caamalg.c | 32 ++++++++++++++++----------------
drivers/crypto/caam/caamhash.c | 23 ++++++++++-------------
2 files changed, 26 insertions(+), 29 deletions(-)

diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c
index d1f25a90552a..3d26c44040c7 100644
--- a/drivers/crypto/caam/caamalg.c
+++ b/drivers/crypto/caam/caamalg.c
@@ -1560,11 +1560,8 @@ static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request
/* allocate space for base edesc and hw desc commands, link tables */
edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
GFP_DMA | flags);
- if (!edesc) {
- caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
- iv_dma, ivsize, 0, 0);
- return ERR_PTR(-ENOMEM);
- }
+ if (!edesc)
+ goto unmap_caam;

edesc->src_nents = src_nents;
edesc->dst_nents = dst_nents;
@@ -1587,10 +1584,8 @@ static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request
sec4_sg_bytes, DMA_TO_DEVICE);
if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
dev_err(jrdev, "unable to map S/G table\n");
- caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
- iv_dma, ivsize, 0, 0);
kfree(edesc);
- return ERR_PTR(-ENOMEM);
+ goto unmap_caam;
}

edesc->iv_dma = iv_dma;
@@ -1603,6 +1598,11 @@ static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request

*iv_contig_out = in_contig;
return edesc;
+
+unmap_caam:
+ caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
+ iv_dma, ivsize, 0, 0);
+ return ERR_PTR(-ENOMEM);
}

static int ablkcipher_encrypt(struct ablkcipher_request *req)
@@ -1768,11 +1768,8 @@ static struct ablkcipher_edesc *ablkcipher_giv_edesc_alloc(
sec4_sg_bytes = sec4_sg_ents * sizeof(struct sec4_sg_entry);
edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
GFP_DMA | flags);
- if (!edesc) {
- caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
- iv_dma, ivsize, 0, 0);
- return ERR_PTR(-ENOMEM);
- }
+ if (!edesc)
+ goto unmap_caam;

edesc->src_nents = src_nents;
edesc->dst_nents = dst_nents;
@@ -1795,10 +1792,8 @@ static struct ablkcipher_edesc *ablkcipher_giv_edesc_alloc(
sec4_sg_bytes, DMA_TO_DEVICE);
if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
dev_err(jrdev, "unable to map S/G table\n");
- caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
- iv_dma, ivsize, 0, 0);
kfree(edesc);
- return ERR_PTR(-ENOMEM);
+ goto unmap_caam;
}
edesc->iv_dma = iv_dma;

@@ -1811,6 +1806,11 @@ static struct ablkcipher_edesc *ablkcipher_giv_edesc_alloc(

*iv_contig_out = out_contig;
return edesc;
+
+unmap_caam:
+ caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents,
+ iv_dma, ivsize, 0, 0);
+ return ERR_PTR(-ENOMEM);
}

static int ablkcipher_givencrypt(struct skcipher_givcrypt_request *creq)
diff --git a/drivers/crypto/caam/caamhash.c b/drivers/crypto/caam/caamhash.c
index dc269eba08ad..b5e43a1f38f0 100644
--- a/drivers/crypto/caam/caamhash.c
+++ b/drivers/crypto/caam/caamhash.c
@@ -371,16 +371,16 @@ static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
DMA_TO_DEVICE);
if (dma_mapping_error(jrdev, src_dma)) {
dev_err(jrdev, "unable to map key input memory\n");
- kfree(desc);
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto free_desc;
}
dst_dma = dma_map_single(jrdev, (void *)key_out, digestsize,
DMA_FROM_DEVICE);
if (dma_mapping_error(jrdev, dst_dma)) {
dev_err(jrdev, "unable to map key output memory\n");
dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
- kfree(desc);
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto free_desc;
}

/* Job descriptor to perform unkeyed hash on key_in */
@@ -419,7 +419,7 @@ static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
dma_unmap_single(jrdev, dst_dma, digestsize, DMA_FROM_DEVICE);

*keylen = digestsize;
-
+free_desc:
kfree(desc);

return ret;
@@ -1070,11 +1070,8 @@ static int ahash_digest(struct ahash_request *req)

ret = ahash_edesc_add_src(ctx, edesc, req, mapped_nents, 0, 0,
req->nbytes);
- if (ret) {
- ahash_unmap(jrdev, edesc, req, digestsize);
- kfree(edesc);
- return ret;
- }
+ if (ret)
+ goto unmap_hash;

desc = edesc->hw_desc;

@@ -1082,9 +1079,8 @@ static int ahash_digest(struct ahash_request *req)
digestsize);
if (dma_mapping_error(jrdev, edesc->dst_dma)) {
dev_err(jrdev, "unable to map dst\n");
- ahash_unmap(jrdev, edesc, req, digestsize);
- kfree(edesc);
- return -ENOMEM;
+ ret = -ENOMEM;
+ goto unmap_hash;
}

#ifdef DEBUG
@@ -1096,6 +1092,7 @@ static int ahash_digest(struct ahash_request *req)
if (!ret) {
ret = -EINPROGRESS;
} else {
+unmap_hash:
ahash_unmap(jrdev, edesc, req, digestsize);
kfree(edesc);
}
--
2.16.1


2018-02-15 07:46:15

by Horia Geanta

[permalink] [raw]
Subject: Re: [PATCH 1/2] crypto: caam: Delete an error message for a failed memory allocation in seven functions

On 2/14/2018 8:31 PM, SF Markus Elfring wrote:
> From: Markus Elfring <[email protected]>
> Date: Wed, 14 Feb 2018 18:22:38 +0100
>
> Omit an extra message for a memory allocation failure in these functions.
>
> This issue was detected by using the Coccinelle software.
>
> Signed-off-by: Markus Elfring <[email protected]>
Reviewed-by: Horia Geant? <[email protected]>

though please consider the following

> @@ -689,10 +687,8 @@ static struct ahash_edesc *ahash_edesc_alloc(struct caam_hash_ctx *ctx,
> unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry);
>
> edesc = kzalloc(sizeof(*edesc) + sg_size, GFP_DMA | flags);
> - if (!edesc) {
> - dev_err(ctx->jrdev, "could not allocate extended descriptor\n");
> + if (!edesc)
> return NULL;
> - }
>
With this change, ctx parameter is no longer used in ahash_edesc_alloc().
Either here on in a different patch the function should be updated.

Thanks,
Horia

2018-02-15 08:04:51

by Horia Geanta

[permalink] [raw]
Subject: Re: [PATCH 2/2] crypto: caam: Use common error handling code in four functions

On 2/14/2018 8:32 PM, SF Markus Elfring wrote:
> From: Markus Elfring <[email protected]>
> Date: Wed, 14 Feb 2018 19:14:49 +0100
>
> Add jump targets so that a bit of exception handling can be better reused
> at the end of these functions.
>
> Signed-off-by: Markus Elfring <[email protected]>
[snip]
> @@ -1096,6 +1092,7 @@ static int ahash_digest(struct ahash_request *req)
> if (!ret) {
> ret = -EINPROGRESS;
> } else {
> +unmap_hash:
> ahash_unmap(jrdev, edesc, req, digestsize);
> kfree(edesc);
> }
>
I understand jumps are a necessary evil for dealing with shortcomings of C,
however please avoid jumping in an if/else branch.

Code could be rewritten as:

if (!ret)
return -EINPROGRESS;

unmap_hash:
ahash_unmap(jrdev, edesc, req, digestsize);
kfree(edesc);

Thanks,
Horia