Saturday, February 22, 2014

kmemleak testing & results


To test & explore kmemleak functionality, I used the following test kernel module (also at https://github.com/babuneelam/kmemleak_tests):
kmemleak_test.c
#include <stdio.h>
 
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
 
void alloc_mem(void)
{
        char *ptr;
        ptr = kmalloc(1024, GFP_KERNEL);
        ptr = vmalloc(512);
        ptr = kmalloc(2048, GFP_KERNEL);
 }
 
int kmemleak_test_init(void)
{
        printk("Kmemleak Test Init");
        alloc_mem();
        return 0;
}
 
static void kmemleak_test_exit(void)
{
        printk("Kmemleak test de-init");
}
 
module_init(kmemleak_test_init);
module_exit(kmemleak_test_exit);
 
MODULE_LICENSE("Proprietary");
MODULE_AUTHOR("Babu");
}

Makefile:
obj-m += kmemleak_test.o

all:
   make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
   make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Here are the next steps I used to test:
        root@babu-VirtualBox:~/km# ls
        kmemleak_test.c  Makefile
        root@babu-VirtualBox:~/km# 
        root@babu-VirtualBox:~/km# 
        root@babu-VirtualBox:~/km# 
        root@babu-VirtualBox:~/km# make
        make -C /lib/modules/3.11.0/build M=/home/babu/km modules
        make[1]: Entering directory `/usr/src/linux-3.11'
          CC [M]  /home/babu/km/kmemleak_test.o
          Building modules, stage 2.
          MODPOST 1 modules
          CC      /home/babu/km/kmemleak_test.mod.o
          LD [M]  /home/babu/km/kmemleak_test.ko
        make[1]: Leaving directory `/usr/src/linux-3.11'
        root@babu-VirtualBox:~/km# echo clear > /sys/kernel/debug/kmemleak
        root@babu-VirtualBox:~/km# insmod kmemleak_test.ko
        root@babu-VirtualBox:~/km# date
        Sat Feb 22 07:58:52 PST 2014 
        root@babu-VirtualBox:~/km# echo scan > /sys/kernel/debug/kmemleak 
        root@babu-VirtualBox:~/km# cat /sys/kernel/debug/kmemleak
        root@babu-VirtualBox:~/km# date
        Sat Feb 22 07:59:30 PST 2014
        root@babu-VirtualBox:~/km#
        root@babu-VirtualBox:~/km# echo scan > /sys/kernel/debug/kmemleak          
        root@babu-VirtualBox:~/km#
          unreferenced object 0xe3d60960 (size 1024):
              comm "insmod", pid 6806, jiffies 2498951 (age 37.084s) 
              hex dump (first 32 bytes): 
                  6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk 
                  6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk 
              backtrace: 
                  [<c161785c>] kmemleak_alloc+0x3c/0xa0 
                  [<c1156b75>] kmem_cache_alloc_trace+0xa5/0x170 
                  [<f864601c>] alloc_mem+0x1c/0x40 [kmemleak_test] 
                  [<f864605c>] init_module+0x1c/0x20 [kmemleak_test] 
                  [<c10020ca>] do_one_initcall+0xca/0x190 
                  [<c10b4524>] load_module+0x1ae4/0x2460 
                  [<c10b4f2f>] SyS_init_module+0x8f/0xf0 
                  [<c162c54d>] sysenter_do_call+0x12/0x28 
                  [<ffffffff>] 0xffffffff 
           unreferenced object 0xf3a22b70 (size 2048): 
               comm "insmod", pid 6806, jiffies 2498951 (age 37.084s) 
               hex dump (first 32 bytes): 
                   6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk 
                   6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk 
               backtrace: 
                   [<c161785c>] kmemleak_alloc+0x3c/0xa0 
                   [<c1156b75>] kmem_cache_alloc_trace+0xa5/0x170 
                   [<f864603a>] alloc_mem+0x3a/0x40 [kmemleak_test] 
                   [<f864605c>] init_module+0x1c/0x20 [kmemleak_test] 
                   [<c10020ca>] do_one_initcall+0xca/0x190 
                   [<c10b4524>] load_module+0x1ae4/0x2460 
                   [<c10b4f2f>] SyS_init_module+0x8f/0xf0 
                   [<c162c54d>] sysenter_do_call+0x12/0x28 
                   [<ffffffff>] 0xffffffff 
        root@babu-VirtualBox:~/km#

So, kmemleak reported 2 memory leaks in alloc_mem function. However, as we can see, my test program has 3 memory leaks. kmemleak failed to report the vmalloc memory leak. I waited for around 30 minutes, but still this leak didn't show up. This is a false negative. I'm yet to understand why kmemleak couldn't have detected this leak.
Overall, kmemleak does help in detecting some memory leaks. The basic advantage of kmemleak is that memory leaks need not go undetected until scalability/performance testing related OOMs show up. Instead, QA teams or developers can deploy kmemleak during functional/unit testing to find memory leaks proactively.
Hope this helps.


No comments:

UA-48797665-1