Linux-3.14.12 Memory Management Note "Memory Leak detection Kmemleak example" "Go"

Source: Internet
Author: User
Tags dmesg

This article was reproduced from: http://blog.chinaunix.net/uid-26859697-id-5758037.html

After analyzing the Kmemleak implementation, test it as usual to determine the function is normal.

As with Kmemcheck, this feature needs to be enabled in case the kernel is turned on. The main configuration items are: Config_debug_kernel, Config_have_debug_kmemleak, Config_debug_kmemleak, and the config_debug_kmemleak_ of the number of configuration information record bars Early_log_size, usually the number of configurations can not be modified, but for self-initiated initialization of the module, it is necessary to adjust, in order to record more information, to avoid omission.

When you compile the configuration kernel for make menuconfig, the configuration path is as follows:

Kernel hacking

+-->kernel debugging (the item needs to be turned on)

+-->memory debugging

+-->kernel Memory Leak Detector (turn on the item)

Of course, these configuration items are dependent on other configuration items and can be found in the make Menuconfig backslash search for the corresponding configuration item.

There are a few things to be aware of in the setup process: The Kernel memory leak detector After opening a few of the children are careful not to set up, which is "simple test for the Kernel memory leak Detector "and" Default Kmemleak to Off ".

For example, the configuration options in the experiment are set as follows:

After compiling the kernel, install the debug version of the kernel, reboot the Linux system, and enter the new kernel. You will be able to see the existence of the path file to/sys/kernel/debug/kmemleak. If present, it indicates a successful start.

The experiment still uses 64-bit system to do the demonstration, modifies the experiment code in front Kmemcheck, constructs the memory leak. The specific code is as follows:

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/mm.h>
  4. #include <linux/slab.h>
  5. #include <asm/page.h>
  6. void Kmemleak_memalloc (void)
  7. {
  8. Char *pmem;
  9. Pmem = Kmalloc (Gfp_kernel);
  10. if (!PMEM)
  11. {
  12. PRINTK ("[Kmemleak]: Kmalloc fail!\n");
  13. }
  14. Else
  15. {
  16. PRINTK ("[Kmemleak]: kmalloc return%p \ n", Pmem);
  17. }
  18. }
  19. int __init kmemleak_test_init (void)
  20. {
  21. PRINTK ("[Kmemleak]: kmemleak_test_init! \ n ");
  22. Kmemleak_memalloc ();
  23. return 0;
  24. }
  25. void __exit kmemleak_test_exit (void)
  26. {
  27. PRINTK ("[Kmemleak]: Kmemleak_test_exit now \ n");
  28. }
  29. Module_init (Kmemleak_test_init)
  30. Module_exit (Kmemleak_test_exit)
  31. Module_license ("GPL");

Makefile script after a minor modification:

    1. Obj-m = KMEMLEAK_TEST.O
    2. All
    3. Make-c/lib/modules/' uname-r '/build m= ' pwd '
    4. Clean
    5. Rm-f *.o *.ko *.mod.c modules.order module.symvers

After compiling the kernel Ko, through the ismod command installation module, according to the code to write the printed log information, through the DMESG to confirm the module initialization is complete and memory application is appropriate.

The relevant information is as follows:

In order to ensure the success of the experiment, KO installed after the wait a few minutes later.

Memory scan detection via command: Echo Scan >/sys/kernel/debug/kmemleak. The test results will be recorded in the/sys/kernel/debug/kmemleak file. By DMESG information, you can see that the requested memory space is located at the 0xffff95c9bf81b600 address. By searching, you can see the information record in Kmemleak:

Kmemleak the information of the test results, the information of the call stack requesting the memory, the memory header 32byte and the memory address and size of the leak are clearly recorded. But the size here is not a very precise size, here is the size of a slab memory piece, the size of the application in the experiment is 300 bytes, corresponding to 512 bytes of slab slices. The specific combination of code analysis part is omitted, that is the basic ability of the developer must, I do not swim.

Next, the control parameters of Kmemleak are described, and the control kmemleak mainly by writing parameters to the/sys/kernel/debug/kmemleak file. For example, the command that enables Kmemleak to scan for memory leaks is written to scan, with the specific command: Echo Scan >/sys/kernel/debug/kmemleak. The parameters used to clear the Kmemleak scan result are clear, and the corresponding commands are: Echo clear >/sys/kernel/debug/kmemleak.

More Kmemleak parameters are:

  1. Off-disable Kmemleak (Irreversible)
  2. Stack=on-enable the task stacks scanning (default)
  3. Stack=off-disable The Tasks Stacks scanning
  4. Scan=on-start the automatic memory scanning thread (default)
  5. Scan=off-stop the automatic memory scanning thread
  6. Scan=<secs>-Set the automatic memory scanning period in seconds
  7. (default 0 to stop the automatic scanning)
  8. Scan-trigger A memory scan
  9. Clear-clear List of current memory leak suspects
  10. Marking all reported unreferenced objects grey
  11. dump=<addr>-Dump information about the object found at <addr>

If you are testing a kernel module, the correct procedure should be:

1. Clear Kmemleak historical information to record new data:

#echo Clear >/sys/kernel/debug/kmemleak

2. Load the kernel modules that need to be tested and execute relevant test cases;

#ismod ***.ko

3. Enable Kmemleak to scan for memory leaks:

#echo Scan >/sys/kernel/debug/kmemleak

4. View the results of the analysis scan:

#cat/sys/kernel/debug/kmemleak

At this point, the experiment is complete, kmemleak is very useful, suitable for kernel module development to detect the location of memory leaks.

Linux-3.14.12 Memory Management Note "Memory Leak detection Kmemleak example" "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.