Skip to content

Commit

Permalink
crypto: sun8i-ss - do not allocate memory when handling hash requests
Browse files Browse the repository at this point in the history
Instead of allocate memory on each requests, it is easier to
pre-allocate buffers.
This made error path easier.

Signed-off-by: Corentin Labbe <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
montjoie authored and herbertx committed May 13, 2022
1 parent 4d867be commit 8eec456
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
10 changes: 10 additions & 0 deletions drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,16 @@ static int allocate_flows(struct sun8i_ss_dev *ss)
goto error_engine;
}

/* the padding could be up to two block. */
ss->flows[i].pad = devm_kmalloc(ss->dev, SHA256_BLOCK_SIZE * 2,
GFP_KERNEL | GFP_DMA);
if (!ss->flows[i].pad)
goto error_engine;
ss->flows[i].result = devm_kmalloc(ss->dev, SHA256_DIGEST_SIZE,
GFP_KERNEL | GFP_DMA);
if (!ss->flows[i].result)
goto error_engine;

ss->flows[i].engine = crypto_engine_alloc_init(ss->dev, true);
if (!ss->flows[i].engine) {
dev_err(ss->dev, "Cannot allocate engine\n");
Expand Down
15 changes: 3 additions & 12 deletions drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,18 +332,11 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
if (digestsize == SHA224_DIGEST_SIZE)
digestsize = SHA256_DIGEST_SIZE;

/* the padding could be up to two block. */
pad = kzalloc(algt->alg.hash.halg.base.cra_blocksize * 2, GFP_KERNEL | GFP_DMA);
if (!pad)
return -ENOMEM;
result = ss->flows[rctx->flow].result;
pad = ss->flows[rctx->flow].pad;
memset(pad, 0, algt->alg.hash.halg.base.cra_blocksize * 2);
bf = (__le32 *)pad;

result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA);
if (!result) {
kfree(pad);
return -ENOMEM;
}

for (i = 0; i < MAX_SG; i++) {
rctx->t_dst[i].addr = 0;
rctx->t_dst[i].len = 0;
Expand Down Expand Up @@ -439,8 +432,6 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)

memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
theend:
kfree(pad);
kfree(result);
local_bh_disable();
crypto_finalize_hash_request(engine, breq, err);
local_bh_enable();
Expand Down
4 changes: 4 additions & 0 deletions drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ struct sginfo {
* @stat_req: number of request done by this flow
* @iv: list of IV to use for each step
* @biv: buffer which contain the backuped IV
* @pad: padding buffer for hash operations
* @result: buffer for storing the result of hash operations
*/
struct sun8i_ss_flow {
struct crypto_engine *engine;
struct completion complete;
int status;
u8 *iv[MAX_SG];
u8 *biv;
void *pad;
void *result;
#ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
unsigned long stat_req;
#endif
Expand Down

0 comments on commit 8eec456

Please sign in to comment.