hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/Documentation/dev-tools/kmemleak.rst
....@@ -2,23 +2,25 @@
22 ===========================
33
44 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>`_,
77 with the difference that the orphan objects are not freed but only
88 reported via /sys/kernel/debug/kmemleak. A similar method is used by the
99 Valgrind tool (``memcheck --leak-check``) to detect the memory leaks in
1010 user-space applications.
11
-Kmemleak is supported on x86, arm, powerpc, sparc, sh, microblaze, ppc, mips, s390 and tile.
1211
1312 Usage
1413 -----
1514
1615 CONFIG_DEBUG_KMEMLEAK in "Kernel hacking" has to be enabled. A kernel
1716 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::
2019
2120 # mount -t debugfs nodev /sys/kernel/debug/
21
+
22
+To display the details of all the possible scanned memory leaks::
23
+
2224 # cat /sys/kernel/debug/kmemleak
2325
2426 To trigger an intermediate memory scan::
....@@ -66,11 +68,14 @@
6668
6769 Memory may be allocated or freed before kmemleak is initialised and
6870 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.
7072
7173 If CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF are enabled, the kmemleak is
7274 disabled by default. Passing ``kmemleak=on`` on the kernel command
7375 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.
7479
7580 Basic Algorithm
7681 ---------------
....@@ -218,3 +223,37 @@
218223 macro or the pointer is stored in a location not scanned by kmemleak.
219224
220225 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.