2006-10-16 22:52:03

by Amit Choudhary

[permalink] [raw]
Subject: potential mem leak when system is low on memory

Hi All,

I was just scrubbing the kernel for checking the return ststus of
kmalloc(), and I saw at many places the following things. I just
wanted to report this.

1. If there are more than one kmalloc() calls in the same function,
and if kmalloc() returns NULL for one of them, then the memory
obtained from previous kmalloc() calls is not released.

Something like:

func()
{
var1 = kmalloc(size);
if (!var1)
return -ENOMEM;

var2 = kmalloc(size);
if (!var2)
return -ENOMEM;

/* mem leak as var1 is not freed */

}

2. Sometimes, memory is allocated in a loop. So, if kmalloc() fails at
some point, memory allocated previously is not released.

func()
{
for (i = 0; i < LENGTH; i++) {
var1[i] = kmalloc(size);
if (!var1[i])
return -ENOMEM;

/* mem leak as var1[0] to var1[i - 1] is not freed */

}
}

So, already the system is running low on memory and on top of it there
are leaks.

-Amit


2006-10-17 08:05:21

by Jarek Poplawski

[permalink] [raw]
Subject: Re: potential mem leak when system is low on memory

On 17-10-2006 00:52, Amit Choudhary wrote:
> Hi All,
>
> I was just scrubbing the kernel for checking the return ststus of
> kmalloc(), and I saw at many places the following things. I just
> wanted to report this.
>
> 1. If there are more than one kmalloc() calls in the same function,
> and if kmalloc() returns NULL for one of them, then the memory
> obtained from previous kmalloc() calls is not released.
>
> Something like:
>
> func()
> {
> var1 = kmalloc(size);
> if (!var1)
> return -ENOMEM;
>
> var2 = kmalloc(size);
> if (!var2)
> return -ENOMEM;
>
> /* mem leak as var1 is not freed */
>
> }
>
> 2. Sometimes, memory is allocated in a loop. So, if kmalloc() fails at
> some point, memory allocated previously is not released.
>
> func()
> {
> for (i = 0; i < LENGTH; i++) {
> var1[i] = kmalloc(size);
> if (!var1[i])
> return -ENOMEM;
>
> /* mem leak as var1[0] to var1[i - 1] is not freed */
>
> }
> }
>
> So, already the system is running low on memory and on top of it there
> are leaks.

So you've found elementary programming bugs and it would
be nice to point this places.

Jarek P.

2006-10-17 18:58:30

by Amit Choudhary

[permalink] [raw]
Subject: Re: potential mem leak when system is low on memory


> > func()
> > {
> > for (i = 0; i < LENGTH; i++) {
> > var1[i] = kmalloc(size);
> > if (!var1[i])
> > return -ENOMEM;
> >
> > /* mem leak as var1[0] to var1[i - 1] is not freed */
> >
> > }
> > }
> >
> > So, already the system is running low on memory and on top of it there
> > are leaks.
>
> So you've found elementary programming bugs and it would
> be nice to point this places.
>

As of now, when I come across such a leak, I fix it. But if I am unable to do so, I will
definitely send out a mail.

Regards,
Amit Choudhary



__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com