Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: drivers - Use kmemdup rather than duplicating its implementation

kmemdup is introduced to duplicate a region of memory in a neat way.
Rather than kmalloc/kzalloc + memcpy, which the programmer needs to
write the size twice (sometimes lead to mistakes), kmemdup improves
readability, leads to smaller code and also reduce the chances of mistakes.
Suggestion to use kmemdup rather than using kmalloc/kzalloc + memcpy.

Signed-off-by: Fuqian Huang <huangfq.daxian@gmail.com>
Reviewed-by: Horia Geantă <horia.geanta@nxp.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Fuqian Huang and committed by
Herbert Xu
cc2a58f1 97bcb161

+4 -11
+3 -8
drivers/crypto/caam/caampkc.c
··· 867 867 return ret; 868 868 869 869 /* Copy key in DMA zone */ 870 - rsa_key->e = kzalloc(raw_key.e_sz, GFP_DMA | GFP_KERNEL); 870 + rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL); 871 871 if (!rsa_key->e) 872 872 goto err; 873 873 ··· 888 888 889 889 rsa_key->e_sz = raw_key.e_sz; 890 890 rsa_key->n_sz = raw_key.n_sz; 891 - 892 - memcpy(rsa_key->e, raw_key.e, raw_key.e_sz); 893 891 894 892 return 0; 895 893 err: ··· 969 971 return ret; 970 972 971 973 /* Copy key in DMA zone */ 972 - rsa_key->d = kzalloc(raw_key.d_sz, GFP_DMA | GFP_KERNEL); 974 + rsa_key->d = kmemdup(raw_key.d, raw_key.d_sz, GFP_DMA | GFP_KERNEL); 973 975 if (!rsa_key->d) 974 976 goto err; 975 977 976 - rsa_key->e = kzalloc(raw_key.e_sz, GFP_DMA | GFP_KERNEL); 978 + rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL); 977 979 if (!rsa_key->e) 978 980 goto err; 979 981 ··· 995 997 rsa_key->d_sz = raw_key.d_sz; 996 998 rsa_key->e_sz = raw_key.e_sz; 997 999 rsa_key->n_sz = raw_key.n_sz; 998 - 999 - memcpy(rsa_key->d, raw_key.d, raw_key.d_sz); 1000 - memcpy(rsa_key->e, raw_key.e, raw_key.e_sz); 1001 1000 1002 1001 caam_rsa_set_priv_key_form(ctx, &raw_key); 1003 1002
+1 -3
drivers/crypto/virtio/virtio_crypto_algs.c
··· 129 129 * Avoid to do DMA from the stack, switch to using 130 130 * dynamically-allocated for the key 131 131 */ 132 - uint8_t *cipher_key = kmalloc(keylen, GFP_ATOMIC); 132 + uint8_t *cipher_key = kmemdup(key, keylen, GFP_ATOMIC); 133 133 134 134 if (!cipher_key) 135 135 return -ENOMEM; 136 - 137 - memcpy(cipher_key, key, keylen); 138 136 139 137 spin_lock(&vcrypto->ctrl_lock); 140 138 /* Pad ctrl header */