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
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • [PATCH] kmalloc, kzalloc comments cleanup and fix
    ... 1)Move comments for kmalloc to right place, ... * kmalloc - allocate memory ... how many bytes of memory are required. ... * The @flags argument may be one of: ...
    (Linux-Kernel)
  • PROBLEM: unchecked returns from kmalloc() in linux-2.6.10-rc2
    ... Checking whether memory allocation succeeded is important, ... Failure might cause a kernel crash. ... the return from kmalloc() is validated before ... Here, memory is allocated for pcpu_size on line 335, and then ...
    (Linux-Kernel)
  • Re: [Q] vmalloc and interrupt handler
    ... >> If you would use kmalloc, the returned memory would be really continuous, ... Of course you must free memory when you are done with it. ... You really should not allocate more than you need. ... > Max allocatable size of one kmallocis 128k, so I decided to use vmalloc(). ...
    (comp.os.linux.development.system)
  • Re: [PATCH 4/4] fdtable: Implement new pagesize-based fdtable allocation scheme.
    ... memory usage increases in easy page-sized chunks. ... Best would be to avoid vmalloc() completely because it can be quite ... This switch between kmalloc() and vmallocwas there in the ...
    (Linux-Kernel)
  • Re: [PATCH] cgroup(fix critical bug): new handling for tasks file
    ... _This memory is allocated by kmalloc_, So this memory is not statistical ... But kmalloc a big chunk of memory is not statistical. ... Using a seq_file iterator with your single pid array per cgroup might ...
    (Linux-Kernel)