public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] dma-buf: Remove unnecessary kmalloc() cast
@ 2024-06-30  1:12 Thorsten Blum
  2024-07-02  6:26 ` Andrew Morton
  0 siblings, 1 reply; 8+ messages in thread
From: Thorsten Blum @ 2024-06-30  1:12 UTC (permalink / raw)
  To: akpm, jack, surenb; +Cc: linux-kernel, Thorsten Blum

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.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 include/linux/dma-fence-chain.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/dma-fence-chain.h b/include/linux/dma-fence-chain.h
index ad9e2506c2f4..a9dc82e1b7f3 100644
--- 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
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] dma-buf: Remove unnecessary kmalloc() cast
  2024-06-30  1:12 [PATCH] dma-buf: Remove unnecessary kmalloc() cast Thorsten Blum
@ 2024-07-02  6:26 ` Andrew Morton
  2024-07-02  6:40   ` Christoph Hellwig
  0 siblings, 1 reply; 8+ messages in thread
From: Andrew Morton @ 2024-07-02  6:26 UTC (permalink / raw)
  To: Thorsten Blum; +Cc: jack, surenb, linux-kernel, Christoph Hellwig

On Sun, 30 Jun 2024 03:12:16 +0200 Thorsten Blum <thorsten.blum@toblux.com> 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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] dma-buf: Remove unnecessary kmalloc() cast
  2024-07-02  6:26 ` Andrew Morton
@ 2024-07-02  6:40   ` Christoph Hellwig
  2024-07-02  7:13     ` Christian König
  0 siblings, 1 reply; 8+ messages in thread
From: Christoph Hellwig @ 2024-07-02  6:40 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Thorsten Blum, jack, surenb, linux-kernel, Sumit Semwal,
	Christian König, linux-media, dri-devel, linaro-mm-sig

On Mon, Jul 01, 2024 at 11:26:34PM -0700, Andrew Morton wrote:
> 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.

Agreed with all of the above.  Adding the dmabuf maintainers.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] dma-buf: Remove unnecessary kmalloc() cast
  2024-07-02  6:40   ` Christoph Hellwig
@ 2024-07-02  7:13     ` Christian König
  2024-07-02  7:33       ` Andrew Morton
  0 siblings, 1 reply; 8+ messages in thread
From: Christian König @ 2024-07-02  7:13 UTC (permalink / raw)
  To: Christoph Hellwig, Andrew Morton
  Cc: Thorsten Blum, jack, surenb, linux-kernel, Sumit Semwal,
	linux-media, dri-devel, linaro-mm-sig

Am 02.07.24 um 08:40 schrieb Christoph Hellwig:
> On Mon, Jul 01, 2024 at 11:26:34PM -0700, Andrew Morton wrote:
>> 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.
> Agreed with all of the above.  Adding the dmabuf maintainers.

Thanks for adding me and I have to ask to be added on DMA-buf patches 
when initially sending them out.

First of all: Yes that cast is intentionally there and yes that is 
intentionally a define and not an inline function.

See this patch here which changed that:

commit 2c321f3f70bc284510598f712b702ce8d60c4d14
Author: Suren Baghdasaryan <surenb@google.com>
Date:   Sun Apr 14 19:07:31 2024 -0700

     mm: change inlined allocation helpers to account at the call site

     Main goal of memory allocation profiling patchset is to provide 
accounting
     that is cheap enough to run in production.  To achieve that we inject
     counters using codetags at the allocation call sites to account 
every time
     allocation is made.  This injection allows us to perform accounting
     efficiently because injected counters are immediately available as 
opposed
     to the alternative methods, such as using _RET_IP_, which would require
     counter lookup and appropriate locking that makes accounting much more
     expensive.  This method requires all allocation functions to inject
     separate counters at their call sites so that their callers can be
     individually accounted.  Counter injection is implemented by allocation
     hooks which should wrap all allocation functions.

     Inlined functions which perform allocations but do not use allocation
     hooks are directly charged for the allocations they perform.  In most
     cases these functions are just specialized allocation wrappers used 
from
     multiple places to allocate objects of a specific type.  It would 
be more
     useful to do the accounting at their call sites instead. Instrument 
these
     helpers to do accounting at the call site.  Simple inlined allocation
     wrappers are converted directly into macros.  More complex 
allocators or
     allocators with documentation are converted into _noprof versions and
     allocation hooks are added.  This allows memory allocation profiling
     mechanism to charge allocations to the callers of these functions.

Regards,
Christian.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] dma-buf: Remove unnecessary kmalloc() cast
  2024-07-02  7:13     ` Christian König
@ 2024-07-02  7:33       ` Andrew Morton
  2024-07-02  7:35         ` Christoph Hellwig
  2024-07-02 15:15         ` Suren Baghdasaryan
  0 siblings, 2 replies; 8+ messages in thread
From: Andrew Morton @ 2024-07-02  7:33 UTC (permalink / raw)
  To: Christian König
  Cc: Christoph Hellwig, Thorsten Blum, jack, surenb, linux-kernel,
	Sumit Semwal, linux-media, dri-devel, linaro-mm-sig

On Tue, 2 Jul 2024 09:13:35 +0200 Christian König <christian.koenig@amd.com> wrote:

> yes that is 
> intentionally a define and not an inline function.
> 
> See this patch here which changed that:
> 
> commit 2c321f3f70bc284510598f712b702ce8d60c4d14
> Author: Suren Baghdasaryan <surenb@google.com>
> Date:   Sun Apr 14 19:07:31 2024 -0700
> 
>      mm: change inlined allocation helpers to account at the call site

Dang, yes, that was a regrettable change.  But hardly the end of the
world.  I do think each such alteration should have included a comment
to prevent people from going and cleaning them up.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] dma-buf: Remove unnecessary kmalloc() cast
  2024-07-02  7:33       ` Andrew Morton
@ 2024-07-02  7:35         ` Christoph Hellwig
  2024-07-02 15:15         ` Suren Baghdasaryan
  1 sibling, 0 replies; 8+ messages in thread
From: Christoph Hellwig @ 2024-07-02  7:35 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Christian König, Christoph Hellwig, Thorsten Blum, jack,
	surenb, linux-kernel, Sumit Semwal, linux-media, dri-devel,
	linaro-mm-sig

On Tue, Jul 02, 2024 at 12:33:57AM -0700, Andrew Morton wrote:
> Dang, yes, that was a regrettable change.  But hardly the end of the
> world.  I do think each such alteration should have included a comment
> to prevent people from going and cleaning them up.

.. or we should have never merged that utter mess ..


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] dma-buf: Remove unnecessary kmalloc() cast
  2024-07-02  7:33       ` Andrew Morton
  2024-07-02  7:35         ` Christoph Hellwig
@ 2024-07-02 15:15         ` Suren Baghdasaryan
  2024-07-03 17:45           ` Suren Baghdasaryan
  1 sibling, 1 reply; 8+ messages in thread
From: Suren Baghdasaryan @ 2024-07-02 15:15 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Christian König, Christoph Hellwig, Thorsten Blum, jack,
	linux-kernel, Sumit Semwal, linux-media, dri-devel, linaro-mm-sig

On Tue, Jul 2, 2024 at 7:34 AM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Tue, 2 Jul 2024 09:13:35 +0200 Christian König <christian.koenig@amd.com> wrote:
>
> > yes that is
> > intentionally a define and not an inline function.
> >
> > See this patch here which changed that:
> >
> > commit 2c321f3f70bc284510598f712b702ce8d60c4d14
> > Author: Suren Baghdasaryan <surenb@google.com>
> > Date:   Sun Apr 14 19:07:31 2024 -0700
> >
> >      mm: change inlined allocation helpers to account at the call site
>
> Dang, yes, that was a regrettable change.  But hardly the end of the
> world.  I do think each such alteration should have included a comment
> to prevent people from going and cleaning them up.

Sorry I missed this discussion. Yes, the definition was intentional
and I will add comments for all the cases which were changed this way.
Thanks,
Suren.

>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] dma-buf: Remove unnecessary kmalloc() cast
  2024-07-02 15:15         ` Suren Baghdasaryan
@ 2024-07-03 17:45           ` Suren Baghdasaryan
  0 siblings, 0 replies; 8+ messages in thread
From: Suren Baghdasaryan @ 2024-07-03 17:45 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Christian König, Christoph Hellwig, Thorsten Blum, jack,
	linux-kernel, Sumit Semwal, linux-media, dri-devel, linaro-mm-sig

On Tue, Jul 2, 2024 at 8:15 AM Suren Baghdasaryan <surenb@google.com> wrote:
>
> On Tue, Jul 2, 2024 at 7:34 AM Andrew Morton <akpm@linux-foundation.org> wrote:
> >
> > On Tue, 2 Jul 2024 09:13:35 +0200 Christian König <christian.koenig@amd.com> wrote:
> >
> > > yes that is
> > > intentionally a define and not an inline function.
> > >
> > > See this patch here which changed that:
> > >
> > > commit 2c321f3f70bc284510598f712b702ce8d60c4d14
> > > Author: Suren Baghdasaryan <surenb@google.com>
> > > Date:   Sun Apr 14 19:07:31 2024 -0700
> > >
> > >      mm: change inlined allocation helpers to account at the call site
> >
> > Dang, yes, that was a regrettable change.  But hardly the end of the
> > world.  I do think each such alteration should have included a comment
> > to prevent people from going and cleaning them up.
>
> Sorry I missed this discussion. Yes, the definition was intentional
> and I will add comments for all the cases which were changed this way.

Posted https://lore.kernel.org/all/20240703174225.3891393-1-surenb@google.com/
adding clarifying comments.
Thanks,
Suren.

> Thanks,
> Suren.
>
> >
> >

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-07-03 17:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-30  1:12 [PATCH] dma-buf: Remove unnecessary kmalloc() cast Thorsten Blum
2024-07-02  6:26 ` Andrew Morton
2024-07-02  6:40   ` Christoph Hellwig
2024-07-02  7:13     ` Christian König
2024-07-02  7:33       ` Andrew Morton
2024-07-02  7:35         ` Christoph Hellwig
2024-07-02 15:15         ` Suren Baghdasaryan
2024-07-03 17:45           ` Suren Baghdasaryan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox