For testing the behavior of the kernel when it leaks memory, I am writing a kernel module that continuously allocates memory e.g. the code looks like
int bytesLeaked = 128000;
char *var = kmalloc(bytesLeaked, GFP_KERNEL);
if (var != NULL)
printk("leaked %d bytes at address %x\n", bytesLeaked, (unsigned int)var);
This code is in the init_module. I have the following questions
MLEE successfully reports 120 new memory leak bugs in the Linux kernel. It is the first time these memory leaks are uncovered by a leak detector for OS kernels. Memory leaks are a common class of memory management bugs and wide spread in many critical software systems.
One way to check for memory leak is to press and hold down your Windows key and tap the Pause/Break key to bring up System Properties. Click on the Performance tab and check System Resources for the percentage of free or available RAM.
If you need to check if a kernel module has leaked memory and your machine has x86 architecture, you can use KEDR system, it includes a memory leak detector.
KEDR does not require you to rebuild the kernel. The online docs (see "Getting Started", for example) describe how to install and use KEDR. In short, the procedure is as follows.
Installation (from source): untar source archive - cmake <...> - make - make install
Start KEDR before you load your module:
$ kedr start <name_of_the_module_to_analyze> -f leak_check.conf
Then you can load your module and work with it as usual. After you unload it, KEDR will give you a report in debugfs (usually debugfs is mounted to /sys/kernel/debug
), for example:
$ cat /sys/kernel/debug/kedr_leak_check/info
Target module: "...",
Memory allocations: 3
Possible leaks: 2
Unallocated frees: 0
The file possible_leaks
from /sys/kernel/debug/kedr_leak_check/
provides information (address, size, call stack) about each leaked memory block.
Finally, you can stop KEDR (note that /sys/kernel/debug/kedr_leak_check/
will disappear):
kedr stop
If you are using a system with architecture other than x86, Kmemleak may also be helpful although it is a bit more difficult to use. You will probably need to rebuild the kernel with CONFIG_DEBUG_KMEMLEAK parameter set to 'y'. Still, Kmemleak is a very useful tool too. See Documentation/kmemleak.txt in the kernel sources for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With