Skip to content

Commit 39a76cf

Browse files
committed
crypto: sun8i-ss - Remove GFP_DMA and add DMA alignment padding
GFP_DMA does not guarantee that the returned memory is aligned for DMA. In fact for sun8i-ss it is superfluous and can be removed. However, kmalloc may start returning DMA-unaligned memory in future so fix this by adding the alignment by hand. Signed-off-by: Herbert Xu <[email protected]> Tested-by: Corentin Labbe <[email protected]> Acked-by: Corentin Labbe <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 4f28982 commit 39a76cf

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

drivers/crypto/allwinner/sun8i-ss/sun8i-ss-cipher.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
452452
}
453453
kfree_sensitive(op->key);
454454
op->keylen = keylen;
455-
op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
455+
op->key = kmemdup(key, keylen, GFP_KERNEL);
456456
if (!op->key)
457457
return -ENOMEM;
458458

@@ -475,7 +475,7 @@ int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
475475

476476
kfree_sensitive(op->key);
477477
op->keylen = keylen;
478-
op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
478+
op->key = kmemdup(key, keylen, GFP_KERNEL);
479479
if (!op->key)
480480
return -ENOMEM;
481481

drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/interrupt.h>
1717
#include <linux/io.h>
1818
#include <linux/irq.h>
19+
#include <linux/kernel.h>
1920
#include <linux/module.h>
2021
#include <linux/of.h>
2122
#include <linux/of_device.h>
@@ -527,15 +528,15 @@ static int allocate_flows(struct sun8i_ss_dev *ss)
527528
init_completion(&ss->flows[i].complete);
528529

529530
ss->flows[i].biv = devm_kmalloc(ss->dev, AES_BLOCK_SIZE,
530-
GFP_KERNEL | GFP_DMA);
531+
GFP_KERNEL);
531532
if (!ss->flows[i].biv) {
532533
err = -ENOMEM;
533534
goto error_engine;
534535
}
535536

536537
for (j = 0; j < MAX_SG; j++) {
537538
ss->flows[i].iv[j] = devm_kmalloc(ss->dev, AES_BLOCK_SIZE,
538-
GFP_KERNEL | GFP_DMA);
539+
GFP_KERNEL);
539540
if (!ss->flows[i].iv[j]) {
540541
err = -ENOMEM;
541542
goto error_engine;
@@ -544,13 +545,15 @@ static int allocate_flows(struct sun8i_ss_dev *ss)
544545

545546
/* the padding could be up to two block. */
546547
ss->flows[i].pad = devm_kmalloc(ss->dev, MAX_PAD_SIZE,
547-
GFP_KERNEL | GFP_DMA);
548+
GFP_KERNEL);
548549
if (!ss->flows[i].pad) {
549550
err = -ENOMEM;
550551
goto error_engine;
551552
}
552-
ss->flows[i].result = devm_kmalloc(ss->dev, SHA256_DIGEST_SIZE,
553-
GFP_KERNEL | GFP_DMA);
553+
ss->flows[i].result =
554+
devm_kmalloc(ss->dev, max(SHA256_DIGEST_SIZE,
555+
dma_get_cache_alignment()),
556+
GFP_KERNEL);
554557
if (!ss->flows[i].result) {
555558
err = -ENOMEM;
556559
goto error_engine;

drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ int sun8i_ss_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
7979
memcpy(tfmctx->key, key, keylen);
8080
}
8181

82-
tfmctx->ipad = kzalloc(bs, GFP_KERNEL | GFP_DMA);
82+
tfmctx->ipad = kzalloc(bs, GFP_KERNEL);
8383
if (!tfmctx->ipad)
8484
return -ENOMEM;
85-
tfmctx->opad = kzalloc(bs, GFP_KERNEL | GFP_DMA);
85+
tfmctx->opad = kzalloc(bs, GFP_KERNEL);
8686
if (!tfmctx->opad) {
8787
ret = -ENOMEM;
8888
goto err_opad;

drivers/crypto/allwinner/sun8i-ss/sun8i-ss-prng.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
#include "sun8i-ss.h"
1313
#include <linux/dma-mapping.h>
14+
#include <linux/kernel.h>
15+
#include <linux/mm.h>
1416
#include <linux/pm_runtime.h>
1517
#include <crypto/internal/rng.h>
1618

@@ -25,7 +27,7 @@ int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed,
2527
ctx->seed = NULL;
2628
}
2729
if (!ctx->seed)
28-
ctx->seed = kmalloc(slen, GFP_KERNEL | GFP_DMA);
30+
ctx->seed = kmalloc(slen, GFP_KERNEL);
2931
if (!ctx->seed)
3032
return -ENOMEM;
3133

@@ -58,6 +60,7 @@ int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
5860
struct sun8i_ss_rng_tfm_ctx *ctx = crypto_rng_ctx(tfm);
5961
struct rng_alg *alg = crypto_rng_alg(tfm);
6062
struct sun8i_ss_alg_template *algt;
63+
unsigned int todo_with_padding;
6164
struct sun8i_ss_dev *ss;
6265
dma_addr_t dma_iv, dma_dst;
6366
unsigned int todo;
@@ -81,7 +84,11 @@ int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
8184
todo = dlen + PRNG_SEED_SIZE + PRNG_DATA_SIZE;
8285
todo -= todo % PRNG_DATA_SIZE;
8386

84-
d = kzalloc(todo, GFP_KERNEL | GFP_DMA);
87+
todo_with_padding = ALIGN(todo, dma_get_cache_alignment());
88+
if (todo_with_padding < todo || todo < dlen)
89+
return -EOVERFLOW;
90+
91+
d = kzalloc(todo_with_padding, GFP_KERNEL);
8592
if (!d)
8693
return -ENOMEM;
8794

0 commit comments

Comments
 (0)