linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/vmalloc.c: check the addr first
@ 2020-09-27 16:33 Hui Su
  2020-09-28 18:04 ` Ira Weiny
  0 siblings, 1 reply; 3+ messages in thread
From: Hui Su @ 2020-09-27 16:33 UTC (permalink / raw)
  To: akpm, linux-mm, linux-kernel

As the comments said, if @addr is NULL, no operation
is performed, check the addr first in vfree() and
vfree_atomic() maybe a better choice.

Signed-off-by: Hui Su <sh_def@163.com>
---
 mm/vmalloc.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index be4724b916b3..1cf50749a209 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -2305,10 +2305,11 @@ void vfree_atomic(const void *addr)
 {
 	BUG_ON(in_nmi());
 
-	kmemleak_free(addr);
-
 	if (!addr)
 		return;
+
+	kmemleak_free(addr);
+
 	__vfree_deferred(addr);
 }
 
@@ -2340,13 +2341,13 @@ void vfree(const void *addr)
 {
 	BUG_ON(in_nmi());
 
+	if (!addr)
+		return;
+
 	kmemleak_free(addr);
 
 	might_sleep_if(!in_interrupt());
 
-	if (!addr)
-		return;
-
 	__vfree(addr);
 }
 EXPORT_SYMBOL(vfree);
-- 
2.25.1




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

* Re: [PATCH] mm/vmalloc.c: check the addr first
  2020-09-27 16:33 [PATCH] mm/vmalloc.c: check the addr first Hui Su
@ 2020-09-28 18:04 ` Ira Weiny
  2020-10-08 12:11   ` Hui Su
  0 siblings, 1 reply; 3+ messages in thread
From: Ira Weiny @ 2020-09-28 18:04 UTC (permalink / raw)
  To: Hui Su; +Cc: akpm, linux-mm, linux-kernel

On Mon, Sep 28, 2020 at 12:33:37AM +0800, Hui Su wrote:
> As the comments said, if @addr is NULL, no operation
> is performed, check the addr first in vfree() and
> vfree_atomic() maybe a better choice.

I don't see how this change helps anything.  kmemleak_free() checks addr so no
danger there.  Also kmemleak_free() contains a pr_debug() which some might find
useful.

Ira

> 
> Signed-off-by: Hui Su <sh_def@163.com>
> ---
>  mm/vmalloc.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index be4724b916b3..1cf50749a209 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -2305,10 +2305,11 @@ void vfree_atomic(const void *addr)
>  {
>  	BUG_ON(in_nmi());
>  
> -	kmemleak_free(addr);
> -
>  	if (!addr)
>  		return;
> +
> +	kmemleak_free(addr);
> +
>  	__vfree_deferred(addr);
>  }
>  
> @@ -2340,13 +2341,13 @@ void vfree(const void *addr)
>  {
>  	BUG_ON(in_nmi());
>  
> +	if (!addr)
> +		return;
> +
>  	kmemleak_free(addr);
>  
>  	might_sleep_if(!in_interrupt());
>  
> -	if (!addr)
> -		return;
> -
>  	__vfree(addr);
>  }
>  EXPORT_SYMBOL(vfree);
> -- 
> 2.25.1
> 
> 
> 


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

* Re: [PATCH] mm/vmalloc.c: check the addr first
  2020-09-28 18:04 ` Ira Weiny
@ 2020-10-08 12:11   ` Hui Su
  0 siblings, 0 replies; 3+ messages in thread
From: Hui Su @ 2020-10-08 12:11 UTC (permalink / raw)
  To: Ira Weiny; +Cc: akpm, linux-mm, linux-kernel

On Mon, Sep 28, 2020 at 11:04:34AM -0700, Ira Weiny wrote:
> On Mon, Sep 28, 2020 at 12:33:37AM +0800, Hui Su wrote:
> > As the comments said, if @addr is NULL, no operation
> > is performed, check the addr first in vfree() and
> > vfree_atomic() maybe a better choice.
> 
> I don't see how this change helps anything.  kmemleak_free() checks addr so no
> danger there.  Also kmemleak_free() contains a pr_debug() which some might find
> useful.
> 
> Ira
> 
Thanks for your explanations, Ira.

Firstly, as you have said, the kmemleak_free() only unregister a previously
registered object(which shouldn't be NULL), and kmemleak_free() alse check the
ptr whether is NULL, it does no danger here.

Secondly, as you said kmemleak_free() contains a pr_debug() which will be useful,
and i don't think so because this patch only filter out the NULL pointer case, which
may not be useful?(maybe not right?)

Thirdly, let's see kimage_file_prepare_segments() and kimage_file_post_load_cleanup()
in kernel/kexec_file.c.

kimage_file_prepare_segments() uses 'out' label in several places, when reading
files from kernel_fd or initrd_fd or memdup_user cmdline_buf.
The out label will run kimage_file_post_load_cleanup(), which will do vfree()
for ->kernel_buf and ->initrd_buf and ->cmdline_buf and so on...

Supposing the arch_kexec_kernel_image_probe() failed, and it goto out, then it
still vfree(->initrd_buf). In this case we can return faster if we check param
addr first in vfree().

In this case maybe we should use more Precise labels?

All above is my opinion, thanks.

> > 
> > Signed-off-by: Hui Su <sh_def@163.com>
> > ---
> >  mm/vmalloc.c | 11 ++++++-----
> >  1 file changed, 6 insertions(+), 5 deletions(-)
> > 
> > diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> > index be4724b916b3..1cf50749a209 100644
> > --- a/mm/vmalloc.c
> > +++ b/mm/vmalloc.c
> > @@ -2305,10 +2305,11 @@ void vfree_atomic(const void *addr)
> >  {
> >  	BUG_ON(in_nmi());
> >  
> > -	kmemleak_free(addr);
> > -
> >  	if (!addr)
> >  		return;
> > +
> > +	kmemleak_free(addr);
> > +
> >  	__vfree_deferred(addr);
> >  }
> >  
> > @@ -2340,13 +2341,13 @@ void vfree(const void *addr)
> >  {
> >  	BUG_ON(in_nmi());
> >  
> > +	if (!addr)
> > +		return;
> > +
> >  	kmemleak_free(addr);
> >  
> >  	might_sleep_if(!in_interrupt());
> >  
> > -	if (!addr)
> > -		return;
> > -
> >  	__vfree(addr);
> >  }
> >  EXPORT_SYMBOL(vfree);
> > -- 
> > 2.25.1
> > 
> > 
> > 



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

end of thread, other threads:[~2020-10-08 12:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-27 16:33 [PATCH] mm/vmalloc.c: check the addr first Hui Su
2020-09-28 18:04 ` Ira Weiny
2020-10-08 12:11   ` Hui Su

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).