linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 3/5] mm: Add kmalloc NULL tests
@ 2009-07-30 14:10 Julia Lawall
  2009-07-30 15:36 ` Johannes Weiner
  0 siblings, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2009-07-30 14:10 UTC (permalink / raw)
  To: linux-mm, linux-kernel, kernel-janitors

From: Julia Lawall <julia@diku.dk>

Check that the result of kmalloc is not NULL before passing it to other
functions.

The semantic match that finds this problem is as follows:
(http://www.emn.fr/x-info/coccinelle/)

// <smpl>
@@
expression *x;
identifier f;
constant char *C;
@@

x = \(kmalloc\|kcalloc\|kzalloc\)(...);
... when != x == NULL
    when != x != NULL
    when != (x || ...)
(
kfree(x)
|
f(...,C,...,x,...)
|
*f(...,x,...)
|
*x->f
)
// </smpl>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
 mm/slab.c       |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/mm/slab.c b/mm/slab.c
index 7b5d4de..972e427 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -1502,6 +1502,7 @@ void __init kmem_cache_init(void)
 
 		ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
 
+		BUG_ON(!ptr);
 		BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
 		memcpy(ptr, cpu_cache_get(&cache_cache),
 		       sizeof(struct arraycache_init));
@@ -1514,6 +1515,7 @@ void __init kmem_cache_init(void)
 
 		ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
 
+		BUG_ON(!ptr);
 		BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
 		       != &initarray_generic.cache);
 		memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH 3/5] mm: Add kmalloc NULL tests
  2009-07-30 14:10 [PATCH 3/5] mm: Add kmalloc NULL tests Julia Lawall
@ 2009-07-30 15:36 ` Johannes Weiner
  2009-07-30 18:35   ` Jörn Engel
  0 siblings, 1 reply; 5+ messages in thread
From: Johannes Weiner @ 2009-07-30 15:36 UTC (permalink / raw)
  To: Julia Lawall; +Cc: linux-mm, linux-kernel, kernel-janitors

Hello Julia,

On Thu, Jul 30, 2009 at 04:10:22PM +0200, Julia Lawall wrote:

> diff --git a/mm/slab.c b/mm/slab.c
> index 7b5d4de..972e427 100644
> --- a/mm/slab.c
> +++ b/mm/slab.c
> @@ -1502,6 +1502,7 @@ void __init kmem_cache_init(void)
>  
>  		ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
>  
> +		BUG_ON(!ptr);
>  		BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
>  		memcpy(ptr, cpu_cache_get(&cache_cache),
>  		       sizeof(struct arraycache_init));

This does not change the end result when the allocation fails: you get
a stacktrace and a kernel panic.  Leaving it as is saves a line of
code.

> @@ -1514,6 +1515,7 @@ void __init kmem_cache_init(void)
>  
>  		ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
>  
> +		BUG_ON(!ptr);
>  		BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
>  		       != &initarray_generic.cache);
>  		memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),

	Hannes

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH 3/5] mm: Add kmalloc NULL tests
  2009-07-30 15:36 ` Johannes Weiner
@ 2009-07-30 18:35   ` Jörn Engel
  2009-07-30 19:12     ` Johannes Weiner
  0 siblings, 1 reply; 5+ messages in thread
From: Jörn Engel @ 2009-07-30 18:35 UTC (permalink / raw)
  To: Johannes Weiner; +Cc: Julia Lawall, linux-mm, linux-kernel, kernel-janitors

On Thu, 30 July 2009 17:36:58 +0200, Johannes Weiner wrote:
> On Thu, Jul 30, 2009 at 04:10:22PM +0200, Julia Lawall wrote:
> 
> > diff --git a/mm/slab.c b/mm/slab.c
> > index 7b5d4de..972e427 100644
> > --- a/mm/slab.c
> > +++ b/mm/slab.c
> > @@ -1502,6 +1502,7 @@ void __init kmem_cache_init(void)
> >  
> >  		ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
> >  
> > +		BUG_ON(!ptr);
> >  		BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
> >  		memcpy(ptr, cpu_cache_get(&cache_cache),
> >  		       sizeof(struct arraycache_init));
> 
> This does not change the end result when the allocation fails: you get
> a stacktrace and a kernel panic.  Leaving it as is saves a line of
> code.

According to http://lwn.net/Articles/342420/, there may be a subtle
difference.

JA?rn

-- 
"Error protection by error detection and correction."
-- from a university class

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH 3/5] mm: Add kmalloc NULL tests
  2009-07-30 18:35   ` Jörn Engel
@ 2009-07-30 19:12     ` Johannes Weiner
  2009-07-30 20:19       ` Jörn Engel
  0 siblings, 1 reply; 5+ messages in thread
From: Johannes Weiner @ 2009-07-30 19:12 UTC (permalink / raw)
  To: Jörn Engel; +Cc: Julia Lawall, linux-mm, linux-kernel, kernel-janitors

On Thu, Jul 30, 2009 at 08:35:59PM +0200, JA?rn Engel wrote:
> On Thu, 30 July 2009 17:36:58 +0200, Johannes Weiner wrote:
> > On Thu, Jul 30, 2009 at 04:10:22PM +0200, Julia Lawall wrote:
> > 
> > > diff --git a/mm/slab.c b/mm/slab.c
> > > index 7b5d4de..972e427 100644
> > > --- a/mm/slab.c
> > > +++ b/mm/slab.c
> > > @@ -1502,6 +1502,7 @@ void __init kmem_cache_init(void)
> > >  
> > >  		ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
> > >  
> > > +		BUG_ON(!ptr);
> > >  		BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
> > >  		memcpy(ptr, cpu_cache_get(&cache_cache),
> > >  		       sizeof(struct arraycache_init));
> > 
> > This does not change the end result when the allocation fails: you get
> > a stacktrace and a kernel panic.  Leaving it as is saves a line of
> > code.
> 
> According to http://lwn.net/Articles/342420/, there may be a subtle
> difference.

You will probably have a hard time establishing a userspace mapping
before slab is initializied :)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH 3/5] mm: Add kmalloc NULL tests
  2009-07-30 19:12     ` Johannes Weiner
@ 2009-07-30 20:19       ` Jörn Engel
  0 siblings, 0 replies; 5+ messages in thread
From: Jörn Engel @ 2009-07-30 20:19 UTC (permalink / raw)
  To: Johannes Weiner; +Cc: Julia Lawall, linux-mm, linux-kernel, kernel-janitors

On Thu, 30 July 2009 21:12:14 +0200, Johannes Weiner wrote:
> 
> You will probably have a hard time establishing a userspace mapping
> before slab is initializied :)

Agreed.

JA?rn

-- 
The story so far:
In the beginning the Universe was created.  This has made a lot
of people very angry and been widely regarded as a bad move.
-- Douglas Adams

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

end of thread, other threads:[~2009-07-30 20:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-30 14:10 [PATCH 3/5] mm: Add kmalloc NULL tests Julia Lawall
2009-07-30 15:36 ` Johannes Weiner
2009-07-30 18:35   ` Jörn Engel
2009-07-30 19:12     ` Johannes Weiner
2009-07-30 20:19       ` Jörn Engel

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).