.. | .. |
---|
2 | 2 | =========================== |
---|
3 | 3 | |
---|
4 | 4 | Kmemleak provides a way of detecting possible kernel memory leaks in a |
---|
5 | | -way similar to a tracing garbage collector |
---|
6 | | -(https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Tracing_garbage_collectors), |
---|
| 5 | +way similar to a `tracing garbage collector |
---|
| 6 | +<https://en.wikipedia.org/wiki/Tracing_garbage_collection>`_, |
---|
7 | 7 | with the difference that the orphan objects are not freed but only |
---|
8 | 8 | reported via /sys/kernel/debug/kmemleak. A similar method is used by the |
---|
9 | 9 | Valgrind tool (``memcheck --leak-check``) to detect the memory leaks in |
---|
10 | 10 | user-space applications. |
---|
11 | | -Kmemleak is supported on x86, arm, powerpc, sparc, sh, microblaze, ppc, mips, s390 and tile. |
---|
12 | 11 | |
---|
13 | 12 | Usage |
---|
14 | 13 | ----- |
---|
15 | 14 | |
---|
16 | 15 | CONFIG_DEBUG_KMEMLEAK in "Kernel hacking" has to be enabled. A kernel |
---|
17 | 16 | thread scans the memory every 10 minutes (by default) and prints the |
---|
18 | | -number of new unreferenced objects found. To display the details of all |
---|
19 | | -the possible memory leaks:: |
---|
| 17 | +number of new unreferenced objects found. If the ``debugfs`` isn't already |
---|
| 18 | +mounted, mount with:: |
---|
20 | 19 | |
---|
21 | 20 | # mount -t debugfs nodev /sys/kernel/debug/ |
---|
| 21 | + |
---|
| 22 | +To display the details of all the possible scanned memory leaks:: |
---|
| 23 | + |
---|
22 | 24 | # cat /sys/kernel/debug/kmemleak |
---|
23 | 25 | |
---|
24 | 26 | To trigger an intermediate memory scan:: |
---|
.. | .. |
---|
66 | 68 | |
---|
67 | 69 | Memory may be allocated or freed before kmemleak is initialised and |
---|
68 | 70 | these actions are stored in an early log buffer. The size of this buffer |
---|
69 | | -is configured via the CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE option. |
---|
| 71 | +is configured via the CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE option. |
---|
70 | 72 | |
---|
71 | 73 | If CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF are enabled, the kmemleak is |
---|
72 | 74 | disabled by default. Passing ``kmemleak=on`` on the kernel command |
---|
73 | 75 | line enables the function. |
---|
| 76 | + |
---|
| 77 | +If you are getting errors like "Error while writing to stdout" or "write_loop: |
---|
| 78 | +Invalid argument", make sure kmemleak is properly enabled. |
---|
74 | 79 | |
---|
75 | 80 | Basic Algorithm |
---|
76 | 81 | --------------- |
---|
.. | .. |
---|
218 | 223 | macro or the pointer is stored in a location not scanned by kmemleak. |
---|
219 | 224 | |
---|
220 | 225 | Page allocations and ioremap are not tracked. |
---|
| 226 | + |
---|
| 227 | +Testing with kmemleak-test |
---|
| 228 | +-------------------------- |
---|
| 229 | + |
---|
| 230 | +To check if you have all set up to use kmemleak, you can use the kmemleak-test |
---|
| 231 | +module, a module that deliberately leaks memory. Set CONFIG_DEBUG_KMEMLEAK_TEST |
---|
| 232 | +as module (it can't be used as built-in) and boot the kernel with kmemleak |
---|
| 233 | +enabled. Load the module and perform a scan with:: |
---|
| 234 | + |
---|
| 235 | + # modprobe kmemleak-test |
---|
| 236 | + # echo scan > /sys/kernel/debug/kmemleak |
---|
| 237 | + |
---|
| 238 | +Note that the you may not get results instantly or on the first scanning. When |
---|
| 239 | +kmemleak gets results, it'll log ``kmemleak: <count of leaks> new suspected |
---|
| 240 | +memory leaks``. Then read the file to see then:: |
---|
| 241 | + |
---|
| 242 | + # cat /sys/kernel/debug/kmemleak |
---|
| 243 | + unreferenced object 0xffff89862ca702e8 (size 32): |
---|
| 244 | + comm "modprobe", pid 2088, jiffies 4294680594 (age 375.486s) |
---|
| 245 | + hex dump (first 32 bytes): |
---|
| 246 | + 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk |
---|
| 247 | + 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b a5 kkkkkkkkkkkkkkk. |
---|
| 248 | + backtrace: |
---|
| 249 | + [<00000000e0a73ec7>] 0xffffffffc01d2036 |
---|
| 250 | + [<000000000c5d2a46>] do_one_initcall+0x41/0x1df |
---|
| 251 | + [<0000000046db7e0a>] do_init_module+0x55/0x200 |
---|
| 252 | + [<00000000542b9814>] load_module+0x203c/0x2480 |
---|
| 253 | + [<00000000c2850256>] __do_sys_finit_module+0xba/0xe0 |
---|
| 254 | + [<000000006564e7ef>] do_syscall_64+0x43/0x110 |
---|
| 255 | + [<000000007c873fa6>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 |
---|
| 256 | + ... |
---|
| 257 | + |
---|
| 258 | +Removing the module with ``rmmod kmemleak_test`` should also trigger some |
---|
| 259 | +kmemleak results. |
---|