public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Allow NULL pointers in percpu_free
@ 2006-11-17 17:36 Alan Stern
  2006-11-17 17:47 ` Daniel Walker
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Stern @ 2006-11-17 17:36 UTC (permalink / raw)
  To: Andrew Morton, Venkatesh Pallipadi, Jens Axboe, Christoph Lameter,
	Pedro Roque, David S. Miller
  Cc: Paul E. McKenney, Kernel development list

The patch (as824) makes percpu_free() ignore NULL arguments, as one
would expect for a deallocation routine.  (Note that free_percpu is
#defined as percpu_free in include/linux/percpu.h.)  A few callers are
updated to remove now-unneeded tests for NULL.  A few other callers
already seem to assume that passing a NULL pointer to percpu_free() is
okay!

The patch also removes an unnecessary NULL check in percpu_depopulate().

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>

---

Index: usb-2.6/arch/i386/kernel/acpi/cstate.c
===================================================================
--- usb-2.6.orig/arch/i386/kernel/acpi/cstate.c
+++ usb-2.6/arch/i386/kernel/acpi/cstate.c
@@ -155,10 +155,8 @@ static int __init ffh_cstate_init(void)
 
 static void __exit ffh_cstate_exit(void)
 {
-	if (cpu_cstate_entry) {
-		free_percpu(cpu_cstate_entry);
-		cpu_cstate_entry = NULL;
-	}
+	free_percpu(cpu_cstate_entry);
+	cpu_cstate_entry = NULL;
 }
 
 arch_initcall(ffh_cstate_init);
Index: usb-2.6/block/blktrace.c
===================================================================
--- usb-2.6.orig/block/blktrace.c
+++ usb-2.6/block/blktrace.c
@@ -366,8 +366,7 @@ err:
 	if (bt) {
 		if (bt->dropped_file)
 			debugfs_remove(bt->dropped_file);
-		if (bt->sequence)
-			free_percpu(bt->sequence);
+		free_percpu(bt->sequence);
 		if (bt->rchan)
 			relay_close(bt->rchan);
 		kfree(bt);
Index: usb-2.6/mm/allocpercpu.c
===================================================================
--- usb-2.6.orig/mm/allocpercpu.c
+++ usb-2.6/mm/allocpercpu.c
@@ -17,10 +17,9 @@
 void percpu_depopulate(void *__pdata, int cpu)
 {
 	struct percpu_data *pdata = __percpu_disguise(__pdata);
-	if (pdata->ptrs[cpu]) {
-		kfree(pdata->ptrs[cpu]);
-		pdata->ptrs[cpu] = NULL;
-	}
+
+	kfree(pdata->ptrs[cpu]);
+	pdata->ptrs[cpu] = NULL;
 }
 EXPORT_SYMBOL_GPL(percpu_depopulate);
 
@@ -123,6 +122,8 @@ EXPORT_SYMBOL_GPL(__percpu_alloc_mask);
  */
 void percpu_free(void *__pdata)
 {
+	if (!__pdata)
+		return;
 	__percpu_depopulate_mask(__pdata, &cpu_possible_map);
 	kfree(__percpu_disguise(__pdata));
 }
Index: usb-2.6/net/ipv6/af_inet6.c
===================================================================
--- usb-2.6.orig/net/ipv6/af_inet6.c
+++ usb-2.6/net/ipv6/af_inet6.c
@@ -719,10 +719,8 @@ snmp6_mib_free(void *ptr[2])
 {
 	if (ptr == NULL)
 		return;
-	if (ptr[0])
-		free_percpu(ptr[0]);
-	if (ptr[1])
-		free_percpu(ptr[1]);
+	free_percpu(ptr[0]);
+	free_percpu(ptr[1]);
 	ptr[0] = ptr[1] = NULL;
 }
 


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

* Re: [PATCH] Allow NULL pointers in percpu_free
  2006-11-17 17:36 [PATCH] Allow NULL pointers in percpu_free Alan Stern
@ 2006-11-17 17:47 ` Daniel Walker
  2006-11-17 18:07   ` Alan Stern
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Walker @ 2006-11-17 17:47 UTC (permalink / raw)
  To: Alan Stern
  Cc: Andrew Morton, Venkatesh Pallipadi, Jens Axboe, Christoph Lameter,
	Pedro Roque, David S. Miller, Paul E. McKenney,
	Kernel development list

On Fri, 2006-11-17 at 12:36 -0500, Alan Stern wrote:

>  void percpu_free(void *__pdata)
>  {
> +	if (!__pdata)
> +		return;

Should be unlikely() right?

Daniel


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

* Re: [PATCH] Allow NULL pointers in percpu_free
  2006-11-17 17:47 ` Daniel Walker
@ 2006-11-17 18:07   ` Alan Stern
  2006-11-17 18:14     ` Daniel Walker
  0 siblings, 1 reply; 5+ messages in thread
From: Alan Stern @ 2006-11-17 18:07 UTC (permalink / raw)
  To: Daniel Walker
  Cc: Andrew Morton, Venkatesh Pallipadi, Jens Axboe, Christoph Lameter,
	Pedro Roque, David S. Miller, Paul E. McKenney,
	Kernel development list

On Fri, 17 Nov 2006, Daniel Walker wrote:

> On Fri, 2006-11-17 at 12:36 -0500, Alan Stern wrote:
> 
> >  void percpu_free(void *__pdata)
> >  {
> > +	if (!__pdata)
> > +		return;
> 
> Should be unlikely() right?

It certainly could be.  I tend not to put such annotations in my code, but 
it wouldn't hurt.

Alan Stern


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

* Re: [PATCH] Allow NULL pointers in percpu_free
  2006-11-17 18:07   ` Alan Stern
@ 2006-11-17 18:14     ` Daniel Walker
  2006-11-17 19:35       ` Alan Stern
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Walker @ 2006-11-17 18:14 UTC (permalink / raw)
  To: Alan Stern
  Cc: Andrew Morton, Venkatesh Pallipadi, Jens Axboe, Christoph Lameter,
	Pedro Roque, David S. Miller, Paul E. McKenney,
	Kernel development list

On Fri, 2006-11-17 at 13:07 -0500, Alan Stern wrote:
> On Fri, 17 Nov 2006, Daniel Walker wrote:
> 
> > On Fri, 2006-11-17 at 12:36 -0500, Alan Stern wrote:
> > 
> > >  void percpu_free(void *__pdata)
> > >  {
> > > +	if (!__pdata)
> > > +		return;
> > 
> > Should be unlikely() right?
> 
> It certainly could be.  I tend not to put such annotations in my code, but 
> it wouldn't hurt.

It's actually a really good idea to add them .. I've noticed they tend
to make my kernels smaller, although I wouldn't expect that to always be
the case.. Another reason is that in -mm we can track how often this
condition is triggered with likely profiling. With kfree, for instance,
there were a number of callers that frequently called kfree(NULL), which
IMO isn't good.

Daniel


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

* Re: [PATCH] Allow NULL pointers in percpu_free
  2006-11-17 18:14     ` Daniel Walker
@ 2006-11-17 19:35       ` Alan Stern
  0 siblings, 0 replies; 5+ messages in thread
From: Alan Stern @ 2006-11-17 19:35 UTC (permalink / raw)
  To: Daniel Walker, Andrew Morton
  Cc: Venkatesh Pallipadi, Jens Axboe, Christoph Lameter,
	David S. Miller, Paul E. McKenney, Kernel development list

The patch (as824b) makes percpu_free() ignore NULL arguments, as one
would expect for a deallocation routine.  (Note that free_percpu is
#defined as percpu_free in include/linux/percpu.h.)  A few callers are
updated to remove now-unneeded tests for NULL.  A few other callers
already seem to assume that passing a NULL pointer to percpu_free() is
okay!

The patch also removes an unnecessary NULL check in percpu_depopulate().

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>

---

On Fri, 17 Nov 2006, Daniel Walker wrote:

> > > Should be unlikely() right?
> > 
> > It certainly could be.  I tend not to put such annotations in my code, but
> > it wouldn't hurt.
> 
> It's actually a really good idea to add them .. I've noticed they tend
> to make my kernels smaller, although I wouldn't expect that to always be
> the case..

Smaller???!!!  How on earth do you think that could happen?  When you
don't use unlikely() there are fewer restrictions on the compiler; hence
it should be free to generate smaller code without the annotation than
with it.  Unless maybe you don't set CONFIG_CC_OPTIMIZE_FOR_SIZE, which 
ought to be a much better way of getting small kernels...

> Another reason is that in -mm we can track how often this
> condition is triggered with likely profiling. With kfree, for instance,
> there were a number of callers that frequently called kfree(NULL), which
> IMO isn't good.

Okay, here's the revised patch.

Alan Stern



Index: usb-2.6/arch/i386/kernel/acpi/cstate.c
===================================================================
--- usb-2.6.orig/arch/i386/kernel/acpi/cstate.c
+++ usb-2.6/arch/i386/kernel/acpi/cstate.c
@@ -155,10 +155,8 @@ static int __init ffh_cstate_init(void)
 
 static void __exit ffh_cstate_exit(void)
 {
-	if (cpu_cstate_entry) {
-		free_percpu(cpu_cstate_entry);
-		cpu_cstate_entry = NULL;
-	}
+	free_percpu(cpu_cstate_entry);
+	cpu_cstate_entry = NULL;
 }
 
 arch_initcall(ffh_cstate_init);
Index: usb-2.6/block/blktrace.c
===================================================================
--- usb-2.6.orig/block/blktrace.c
+++ usb-2.6/block/blktrace.c
@@ -366,8 +366,7 @@ err:
 	if (bt) {
 		if (bt->dropped_file)
 			debugfs_remove(bt->dropped_file);
-		if (bt->sequence)
-			free_percpu(bt->sequence);
+		free_percpu(bt->sequence);
 		if (bt->rchan)
 			relay_close(bt->rchan);
 		kfree(bt);
Index: usb-2.6/mm/allocpercpu.c
===================================================================
--- usb-2.6.orig/mm/allocpercpu.c
+++ usb-2.6/mm/allocpercpu.c
@@ -17,10 +17,9 @@
 void percpu_depopulate(void *__pdata, int cpu)
 {
 	struct percpu_data *pdata = __percpu_disguise(__pdata);
-	if (pdata->ptrs[cpu]) {
-		kfree(pdata->ptrs[cpu]);
-		pdata->ptrs[cpu] = NULL;
-	}
+
+	kfree(pdata->ptrs[cpu]);
+	pdata->ptrs[cpu] = NULL;
 }
 EXPORT_SYMBOL_GPL(percpu_depopulate);
 
@@ -123,6 +122,8 @@ EXPORT_SYMBOL_GPL(__percpu_alloc_mask);
  */
 void percpu_free(void *__pdata)
 {
+	if (unlikely(!__pdata))
+		return;
 	__percpu_depopulate_mask(__pdata, &cpu_possible_map);
 	kfree(__percpu_disguise(__pdata));
 }
Index: usb-2.6/net/ipv6/af_inet6.c
===================================================================
--- usb-2.6.orig/net/ipv6/af_inet6.c
+++ usb-2.6/net/ipv6/af_inet6.c
@@ -719,10 +719,8 @@ snmp6_mib_free(void *ptr[2])
 {
 	if (ptr == NULL)
 		return;
-	if (ptr[0])
-		free_percpu(ptr[0]);
-	if (ptr[1])
-		free_percpu(ptr[1]);
+	free_percpu(ptr[0]);
+	free_percpu(ptr[1]);
 	ptr[0] = ptr[1] = NULL;
 }
 


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

end of thread, other threads:[~2006-11-17 19:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-11-17 17:36 [PATCH] Allow NULL pointers in percpu_free Alan Stern
2006-11-17 17:47 ` Daniel Walker
2006-11-17 18:07   ` Alan Stern
2006-11-17 18:14     ` Daniel Walker
2006-11-17 19:35       ` Alan Stern

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