diff options
author | André Almeida <andrealmeid@collabora.com> | 2019-07-11 23:53:46 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2019-07-12 14:05:42 -0400 |
commit | b7c3613e685076bad6e5540501d84cc31bb30377 (patch) | |
tree | ae15f6423bbe16939446ad7a36912973024e89bd /Documentation/dev-tools | |
parent | 4e4dfce2278929de4379cdcfa2335dad7a6c4aa0 (diff) |
docs: kmemleak: add more documentation details
Wikipedia now has a main article to "tracing garbage collector" topic.
Change the URL and use the reStructuredText syntax for hyperlinks and add
more details about the use of the tool. Add a section about how to use
the kmemleak-test module to test the memory leak scanning.
Link: http://lkml.kernel.org/r/20190612155231.19448-2-andrealmeid@collabora.com
Signed-off-by: André Almeida <andrealmeid@collabora.com>
Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'Documentation/dev-tools')
-rw-r--r-- | Documentation/dev-tools/kmemleak.rst | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/Documentation/dev-tools/kmemleak.rst b/Documentation/dev-tools/kmemleak.rst index e6f51260ff32..3621cd5e1eef 100644 --- a/Documentation/dev-tools/kmemleak.rst +++ b/Documentation/dev-tools/kmemleak.rst | |||
@@ -2,8 +2,8 @@ Kernel Memory Leak Detector | |||
2 | =========================== | 2 | =========================== |
3 | 3 | ||
4 | Kmemleak provides a way of detecting possible kernel memory leaks in a | 4 | Kmemleak provides a way of detecting possible kernel memory leaks in a |
5 | way similar to a tracing garbage collector | 5 | way similar to a `tracing garbage collector |
6 | (https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Tracing_garbage_collectors), | 6 | <https://en.wikipedia.org/wiki/Tracing_garbage_collection>`_, |
7 | with the difference that the orphan objects are not freed but only | 7 | with the difference that the orphan objects are not freed but only |
8 | reported via /sys/kernel/debug/kmemleak. A similar method is used by the | 8 | reported via /sys/kernel/debug/kmemleak. A similar method is used by the |
9 | Valgrind tool (``memcheck --leak-check``) to detect the memory leaks in | 9 | Valgrind tool (``memcheck --leak-check``) to detect the memory leaks in |
@@ -15,10 +15,13 @@ Usage | |||
15 | 15 | ||
16 | CONFIG_DEBUG_KMEMLEAK in "Kernel hacking" has to be enabled. A kernel | 16 | CONFIG_DEBUG_KMEMLEAK in "Kernel hacking" has to be enabled. A kernel |
17 | thread scans the memory every 10 minutes (by default) and prints the | 17 | 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 | 18 | number of new unreferenced objects found. If the ``debugfs`` isn't already |
19 | the possible memory leaks:: | 19 | mounted, mount with:: |
20 | 20 | ||
21 | # mount -t debugfs nodev /sys/kernel/debug/ | 21 | # mount -t debugfs nodev /sys/kernel/debug/ |
22 | |||
23 | To display the details of all the possible scanned memory leaks:: | ||
24 | |||
22 | # cat /sys/kernel/debug/kmemleak | 25 | # cat /sys/kernel/debug/kmemleak |
23 | 26 | ||
24 | To trigger an intermediate memory scan:: | 27 | To trigger an intermediate memory scan:: |
@@ -72,6 +75,9 @@ If CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF are enabled, the kmemleak is | |||
72 | disabled by default. Passing ``kmemleak=on`` on the kernel command | 75 | disabled by default. Passing ``kmemleak=on`` on the kernel command |
73 | line enables the function. | 76 | line enables the function. |
74 | 77 | ||
78 | If you are getting errors like "Error while writing to stdout" or "write_loop: | ||
79 | Invalid argument", make sure kmemleak is properly enabled. | ||
80 | |||
75 | Basic Algorithm | 81 | Basic Algorithm |
76 | --------------- | 82 | --------------- |
77 | 83 | ||
@@ -218,3 +224,37 @@ the pointer is calculated by other methods than the usual container_of | |||
218 | macro or the pointer is stored in a location not scanned by kmemleak. | 224 | macro or the pointer is stored in a location not scanned by kmemleak. |
219 | 225 | ||
220 | Page allocations and ioremap are not tracked. | 226 | Page allocations and ioremap are not tracked. |
227 | |||
228 | Testing with kmemleak-test | ||
229 | -------------------------- | ||
230 | |||
231 | To check if you have all set up to use kmemleak, you can use the kmemleak-test | ||
232 | module, a module that deliberately leaks memory. Set CONFIG_DEBUG_KMEMLEAK_TEST | ||
233 | as module (it can't be used as bult-in) and boot the kernel with kmemleak | ||
234 | enabled. Load the module and perform a scan with:: | ||
235 | |||
236 | # modprobe kmemleak-test | ||
237 | # echo scan > /sys/kernel/debug/kmemleak | ||
238 | |||
239 | Note that the you may not get results instantly or on the first scanning. When | ||
240 | kmemleak gets results, it'll log ``kmemleak: <count of leaks> new suspected | ||
241 | memory leaks``. Then read the file to see then:: | ||
242 | |||
243 | # cat /sys/kernel/debug/kmemleak | ||
244 | unreferenced object 0xffff89862ca702e8 (size 32): | ||
245 | comm "modprobe", pid 2088, jiffies 4294680594 (age 375.486s) | ||
246 | hex dump (first 32 bytes): | ||
247 | 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk | ||
248 | 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b a5 kkkkkkkkkkkkkkk. | ||
249 | backtrace: | ||
250 | [<00000000e0a73ec7>] 0xffffffffc01d2036 | ||
251 | [<000000000c5d2a46>] do_one_initcall+0x41/0x1df | ||
252 | [<0000000046db7e0a>] do_init_module+0x55/0x200 | ||
253 | [<00000000542b9814>] load_module+0x203c/0x2480 | ||
254 | [<00000000c2850256>] __do_sys_finit_module+0xba/0xe0 | ||
255 | [<000000006564e7ef>] do_syscall_64+0x43/0x110 | ||
256 | [<000000007c873fa6>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 | ||
257 | ... | ||
258 | |||
259 | Removing the module with ``rmmod kmemleak_test`` should also trigger some | ||
260 | kmemleak results. | ||