diff mbox series

crypto: talitos - Remove GFP_DMA and add DMA alignment padding

Message ID Y66T1UCBTqBE6GQ4@gondor.apana.org.au
State Accepted
Commit 8e613cec25196b51601dfac50c5bf229acd72bc6
Headers show
Series crypto: talitos - Remove GFP_DMA and add DMA alignment padding | expand

Commit Message

Herbert Xu Dec. 30, 2022, 7:31 a.m. UTC
GFP_DMA does not guarantee that the returned memory is aligned
for DMA.  It should be removed where it is superfluous.

However, kmalloc may start returning DMA-unaligned memory in future
so fix this by adding the alignment by hand.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Comments

Christoph Hellwig Jan. 8, 2023, 5:37 p.m. UTC | #1
Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>
Christophe Leroy Jan. 9, 2023, 7:18 a.m. UTC | #2
Le 30/12/2022 à 08:31, Herbert Xu a écrit :
> GFP_DMA does not guarantee that the returned memory is aligned
> for DMA.  It should be removed where it is superfluous.

Doesn't GFP_DMA guarantees that the provided memory is addressable for 
DMA ? Or do we assume that all memory returned by kmalloc can be used 
for DMA ?

> 
> However, kmalloc may start returning DMA-unaligned memory in future
> so fix this by adding the alignment by hand.

kmalloc() already returns not DMA aligned memory, why does it becomes a 
problem now ?
Ok, that may be suboptimal, but is that a problem at all ?

By the way, I'm not sure I understand the solution, what's the added 
value of aligning allocation length to the cache alignment ?

> 
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
> 
> diff --git a/drivers/crypto/talitos.c b/drivers/crypto/talitos.c
> index 71db6450b6aa..d62ec68e3183 100644
> --- a/drivers/crypto/talitos.c
> +++ b/drivers/crypto/talitos.c
> @@ -1393,7 +1393,7 @@ static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
>   		alloc_len += sizeof(struct talitos_desc);
>   	alloc_len += ivsize;
>   
> -	edesc = kmalloc(alloc_len, GFP_DMA | flags);
> +	edesc = kmalloc(ALIGN(alloc_len, dma_get_cache_alignment()), flags);
>   	if (!edesc)
>   		return ERR_PTR(-ENOMEM);
>   	if (ivsize) {
Herbert Xu Jan. 9, 2023, 7:37 a.m. UTC | #3
On Mon, Jan 09, 2023 at 07:18:27AM +0000, Christophe Leroy wrote:
>
> Doesn't GFP_DMA guarantees that the provided memory is addressable for 
> DMA ? Or do we assume that all memory returned by kmalloc can be used 
> for DMA ?

No it does not.  Please refer to the DMI API documentation.

> kmalloc() already returns not DMA aligned memory, why does it becomes a 
> problem now ?

As it stands kmalloc on arm64 returns DMA-aligned memory.  This
will soon no longer be the case.

Cheers,
diff mbox series

Patch

diff --git a/drivers/crypto/talitos.c b/drivers/crypto/talitos.c
index 71db6450b6aa..d62ec68e3183 100644
--- a/drivers/crypto/talitos.c
+++ b/drivers/crypto/talitos.c
@@ -1393,7 +1393,7 @@  static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
 		alloc_len += sizeof(struct talitos_desc);
 	alloc_len += ivsize;
 
-	edesc = kmalloc(alloc_len, GFP_DMA | flags);
+	edesc = kmalloc(ALIGN(alloc_len, dma_get_cache_alignment()), flags);
 	if (!edesc)
 		return ERR_PTR(-ENOMEM);
 	if (ivsize) {