Re: [PATCH] dma-buf: Remove unnecessary kmalloc() cast
From: Andrew Morton
Date: Tue Jul 02 2024 - 02:26:40 EST
On Sun, 30 Jun 2024 03:12:16 +0200 Thorsten Blum <thorsten.blum@xxxxxxxxxx> wrote:
> Casting the return value of kmalloc() is unnecessary and can be
> removed. Remove it and fix the following Coccinelle/coccicheck warning
> reported by alloc_cast.cocci:
>
> WARNING: casting value returned by memory allocation function to (struct dma_fence_chain *) is useless.
>
> Compile-tested only.
>
> ...
>
> --- a/include/linux/dma-fence-chain.h
> +++ b/include/linux/dma-fence-chain.h
> @@ -87,7 +87,7 @@ dma_fence_chain_contained(struct dma_fence *fence)
> * Returns a new struct dma_fence_chain object or NULL on failure.
> */
> #define dma_fence_chain_alloc() \
> - ((struct dma_fence_chain *)kmalloc(sizeof(struct dma_fence_chain), GFP_KERNEL))
> + kmalloc(sizeof(struct dma_fence_chain), GFP_KERNEL)
>
> /**
> * dma_fence_chain_free
No, I do think the cast is useful:
struct page *page = dma_fence_chain_alloc();
will presently generate a warning. We want this. Your change will
remove that useful warning.
Unrelatedly: there is no earthly reason why this is implemented as a
macro. A static inline function would be so much better. Why do we
keep doing this.