Դsphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget&/translations/zh_CN/dev-tools/kmemleakmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget&/translations/zh_TW/dev-tools/kmemleakmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget&/translations/it_IT/dev-tools/kmemleakmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget&/translations/ja_JP/dev-tools/kmemleakmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget&/translations/ko_KR/dev-tools/kmemleakmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget&/translations/sp_SP/dev-tools/kmemleakmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(hKernel Memory Leak Detectorh]hKernel Memory Leak Detector}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhh@/var/lib/git/docbuild/linux/Documentation/dev-tools/kmemleak.rsthKubh paragraph)}(hXKmemleak provides a way of detecting possible kernel memory leaks in a way similar to a `tracing garbage collector `_, with the difference that the orphan objects are not freed but only reported via /sys/kernel/debug/kmemleak. A similar method is used by the Valgrind tool (``memcheck --leak-check``) to detect the memory leaks in user-space applications.h](hXKmemleak provides a way of detecting possible kernel memory leaks in a way similar to a }(hhhhhNhNubh reference)}(hW`tracing garbage collector `_h]htracing garbage collector}(hhhhhNhNubah}(h]h ]h"]h$]h&]nametracing garbage collectorrefuri8https://en.wikipedia.org/wiki/Tracing_garbage_collectionuh1hhhubhtarget)}(h; h]h}(h]tracing-garbage-collectorah ]h"]tracing garbage collectorah$]h&]refurihuh1hՌ referencedKhhubh, with the difference that the orphan objects are not freed but only reported via /sys/kernel/debug/kmemleak. A similar method is used by the Valgrind tool (}(hhhhhNhNubhliteral)}(h``memcheck --leak-check``h]hmemcheck --leak-check}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubh8) to detect the memory leaks in user-space applications.}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hhh](h)}(hUsageh]hUsage}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK ubh)}(hCONFIG_DEBUG_KMEMLEAK in "Kernel hacking" has to be enabled. A kernel thread scans the memory every 10 minutes (by default) and prints the number of new unreferenced objects found. If the ``debugfs`` isn't already mounted, mount with::h](hCONFIG_DEBUG_KMEMLEAK in “Kernel hacking” has to be enabled. A kernel thread scans the memory every 10 minutes (by default) and prints the number of new unreferenced objects found. If the }(hjhhhNhNubh)}(h ``debugfs``h]hdebugfs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh% isn’t already mounted, mount with:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh literal_block)}(h+# mount -t debugfs nodev /sys/kernel/debug/h]h+# mount -t debugfs nodev /sys/kernel/debug/}hj6sbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1j4hhhKhjhhubh)}(hATo display the details of all the possible scanned memory leaks::h]h@To display the details of all the possible scanned memory leaks:}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj5)}(h # cat /sys/kernel/debug/kmemleakh]h # cat /sys/kernel/debug/kmemleak}hjTsbah}(h]h ]h"]h$]h&]jDjEuh1j4hhhKhjhhubh)}(h(To trigger an intermediate memory scan::h]h'To trigger an intermediate memory scan:}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj5)}(h(# echo scan > /sys/kernel/debug/kmemleakh]h(# echo scan > /sys/kernel/debug/kmemleak}hjpsbah}(h]h ]h"]h$]h&]jDjEuh1j4hhhKhjhhubh)}(h8To clear the list of all current possible memory leaks::h]h7To clear the list of all current possible memory leaks:}(hj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj5)}(h)# echo clear > /sys/kernel/debug/kmemleakh]h)# echo clear > /sys/kernel/debug/kmemleak}hjsbah}(h]h ]h"]h$]h&]jDjEuh1j4hhhK hjhhubh)}(hNNew leaks will then come up upon reading ``/sys/kernel/debug/kmemleak`` again.h](h)New leaks will then come up upon reading }(hjhhhNhNubh)}(h``/sys/kernel/debug/kmemleak``h]h/sys/kernel/debug/kmemleak}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh again.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK"hjhhubh)}(hNote that the orphan objects are listed in the order they were allocated and one object at the beginning of the list may cause other subsequent objects to be reported as orphan.h]hNote that the orphan objects are listed in the order they were allocated and one object at the beginning of the list may cause other subsequent objects to be reported as orphan.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK%hjhhubh)}(hMemory scanning parameters can be modified at run-time by writing to the ``/sys/kernel/debug/kmemleak`` file. The following parameters are supported:h](hIMemory scanning parameters can be modified at run-time by writing to the }(hjhhhNhNubh)}(h``/sys/kernel/debug/kmemleak``h]h/sys/kernel/debug/kmemleak}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh. file. The following parameters are supported:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK)hjhhubh bullet_list)}(hhh](h list_item)}(h%off disable kmemleak (irreversible)h]hdefinition_list)}(hhh]hdefinition_list_item)}(h#off disable kmemleak (irreversible)h](hterm)}(hoffh]hoff}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK,hjubh definition)}(hhh]h)}(hdisable kmemleak (irreversible)h]hdisable kmemleak (irreversible)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK-hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK,hjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(h4stack=on enable the task stacks scanning (default)h]j)}(hhh]j)}(h2stack=on enable the task stacks scanning (default)h](j)}(hstack=onh]hstack=on}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK.hj@ubj)}(hhh]h)}(h)enable the task stacks scanning (default)h]h)enable the task stacks scanning (default)}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK/hjRubah}(h]h ]h"]h$]h&]uh1jhj@ubeh}(h]h ]h"]h$]h&]uh1jhhhK.hj=ubah}(h]h ]h"]h$]h&]uh1jhj9ubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(h-stack=off disable the tasks stacks scanningh]j)}(hhh]j)}(h+stack=off disable the tasks stacks scanningh](j)}(h stack=offh]h stack=off}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK0hjubj)}(hhh]h)}(h!disable the tasks stacks scanningh]h!disable the tasks stacks scanning}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK1hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK0hjubah}(h]h ]h"]h$]h&]uh1jhj{ubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(h>scan=on start the automatic memory scanning thread (default)h]j)}(hhh]j)}(h set the automatic memory scanning period in seconds (default 600, 0 to stop the automatic scanning)h]j)}(hhh]j)}(hoscan= set the automatic memory scanning period in seconds (default 600, 0 to stop the automatic scanning)h](j)}(h scan=h]h scan=}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK7hjHubj)}(hhh]h)}(hcset the automatic memory scanning period in seconds (default 600, 0 to stop the automatic scanning)h]hcset the automatic memory scanning period in seconds (default 600, 0 to stop the automatic scanning)}(hj]hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK7hjZubah}(h]h ]h"]h$]h&]uh1jhjHubeh}(h]h ]h"]h$]h&]uh1jhhhK7hjEubah}(h]h ]h"]h$]h&]uh1jhjAubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(hscan trigger a memory scanh]j)}(hhh]j)}(hscan trigger a memory scanh](j)}(hscanh]hscan}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK9hjubj)}(hhh]h)}(htrigger a memory scanh]htrigger a memory scan}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK:hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK9hjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(hclear clear list of current memory leak suspects, done by marking all current reported unreferenced objects grey, or free all kmemleak objects if kmemleak has been disabled.h]j)}(hhh]j)}(hclear clear list of current memory leak suspects, done by marking all current reported unreferenced objects grey, or free all kmemleak objects if kmemleak has been disabled.h](j)}(hclearh]hclear}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK=hjubj)}(hhh]h)}(hclear list of current memory leak suspects, done by marking all current reported unreferenced objects grey, or free all kmemleak objects if kmemleak has been disabled.h]hclear list of current memory leak suspects, done by marking all current reported unreferenced objects grey, or free all kmemleak objects if kmemleak has been disabled.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK dump information about the object found at h]j)}(hhh]j)}(h>dump= dump information about the object found at h](j)}(h dump=h]h dump=}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK@hjubj)}(hhh]h)}(h1dump information about the object found at h]h1dump information about the object found at }(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK@hj ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK@hj ubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubeh}(h]h ]h"]h$]h&]bullet-uh1jhhhK,hjhhubh)}(hbKmemleak can also be disabled at boot-time by passing ``kmemleak=off`` on the kernel command line.h](h6Kmemleak can also be disabled at boot-time by passing }(hjQhhhNhNubh)}(h``kmemleak=off``h]h kmemleak=off}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjQubh on the kernel command line.}(hjQhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKBhjhhubh)}(hMemory may be allocated or freed before kmemleak is initialised and these actions are stored in an early log buffer. The size of this buffer is configured via the CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE option.h]hMemory may be allocated or freed before kmemleak is initialised and these actions are stored in an early log buffer. The size of this buffer is configured via the CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE option.}(hjqhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKEhjhhubh)}(hIf CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF are enabled, the kmemleak is disabled by default. Passing ``kmemleak=on`` on the kernel command line enables the function.h](h_If CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF are enabled, the kmemleak is disabled by default. Passing }(hjhhhNhNubh)}(h``kmemleak=on``h]h kmemleak=on}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh1 on the kernel command line enables the function.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKIhjhhubh)}(hIf you are getting errors like "Error while writing to stdout" or "write_loop: Invalid argument", make sure kmemleak is properly enabled.h]hIf you are getting errors like “Error while writing to stdout” or “write_loop: Invalid argument”, make sure kmemleak is properly enabled.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKMhjhhubeh}(h]usageah ]h"]usageah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(hBasic Algorithmh]hBasic Algorithm}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKQubh)}(hXMThe memory allocations via :c:func:`kmalloc`, :c:func:`vmalloc`, :c:func:`kmem_cache_alloc` and friends are traced and the pointers, together with additional information like size and stack trace, are stored in a rbtree. The corresponding freeing function calls are tracked and the pointers removed from the kmemleak data structures.h](hThe memory allocations via }(hjhhhNhNubh)}(h:c:func:`kmalloc`h]h)}(hjh]h kmalloc()}(hjhhhNhNubah}(h]h ](xrefcc-funceh"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]refdocdev-tools/kmemleak refdomainjreftypefunc refexplicitrefwarn reftargetkmallocuh1hhhhKShjubh, }(hjhhhNhNubh)}(h:c:func:`vmalloc`h]h)}(hjh]h vmalloc()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjvmallocuh1hhhhKShjubh, }(hjhhhNhNubh)}(h:c:func:`kmem_cache_alloc`h]h)}(hjh]hkmem_cache_alloc()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjkmem_cache_allocuh1hhhhKShjubh and friends are traced and the pointers, together with additional information like size and stack trace, are stored in a rbtree. The corresponding freeing function calls are tracked and the pointers removed from the kmemleak data structures.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKShjhhubh)}(hXdAn allocated block of memory is considered orphan if no pointer to its start address or to any location inside the block can be found by scanning the memory (including saved registers). This means that there might be no way for the kernel to pass the address of the allocated block to a freeing function and therefore the block is considered a memory leak.h]hXdAn allocated block of memory is considered orphan if no pointer to its start address or to any location inside the block can be found by scanning the memory (including saved registers). This means that there might be no way for the kernel to pass the address of the allocated block to a freeing function and therefore the block is considered a memory leak.}(hjAhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKZhjhhubh)}(hThe scanning algorithm steps:h]hThe scanning algorithm steps:}(hjOhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKahjhhubh block_quote)}(hX31. mark all objects as white (remaining white objects will later be considered orphan) 2. scan the memory starting with the data section and stacks, checking the values against the addresses stored in the rbtree. If a pointer to a white object is found, the object is added to the gray list 3. scan the gray objects for matching addresses (some white objects can become gray and added at the end of the gray list) until the gray set is finished 4. the remaining white objects are considered orphan and reported via /sys/kernel/debug/kmemleak h]henumerated_list)}(hhh](j)}(hSmark all objects as white (remaining white objects will later be considered orphan)h]h)}(hSmark all objects as white (remaining white objects will later be considered orphan)h]hSmark all objects as white (remaining white objects will later be considered orphan)}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKchjhubah}(h]h ]h"]h$]h&]uh1jhjeubj)}(hscan the memory starting with the data section and stacks, checking the values against the addresses stored in the rbtree. If a pointer to a white object is found, the object is added to the gray listh]h)}(hscan the memory starting with the data section and stacks, checking the values against the addresses stored in the rbtree. If a pointer to a white object is found, the object is added to the gray listh]hscan the memory starting with the data section and stacks, checking the values against the addresses stored in the rbtree. If a pointer to a white object is found, the object is added to the gray list}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKehjubah}(h]h ]h"]h$]h&]uh1jhjeubj)}(hscan the gray objects for matching addresses (some white objects can become gray and added at the end of the gray list) until the gray set is finishedh]h)}(hscan the gray objects for matching addresses (some white objects can become gray and added at the end of the gray list) until the gray set is finishedh]hscan the gray objects for matching addresses (some white objects can become gray and added at the end of the gray list) until the gray set is finished}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKihjubah}(h]h ]h"]h$]h&]uh1jhjeubj)}(h^the remaining white objects are considered orphan and reported via /sys/kernel/debug/kmemleak h]h)}(h]the remaining white objects are considered orphan and reported via /sys/kernel/debug/kmemleakh]h]the remaining white objects are considered orphan and reported via /sys/kernel/debug/kmemleak}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKlhjubah}(h]h ]h"]h$]h&]uh1jhjeubeh}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix.uh1jchj_ubah}(h]h ]h"]h$]h&]uh1j]hhhKchjhhubh)}(hXRSome allocated memory blocks have pointers stored in the kernel's internal data structures and they cannot be detected as orphans. To avoid this, kmemleak can also store the number of values pointing to an address inside the block address range that need to be found so that the block is not considered a leak. One example is __vmalloc().h]hXTSome allocated memory blocks have pointers stored in the kernel’s internal data structures and they cannot be detected as orphans. To avoid this, kmemleak can also store the number of values pointing to an address inside the block address range that need to be found so that the block is not considered a leak. One example is __vmalloc().}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKohjhhubeh}(h]basic-algorithmah ]h"]basic algorithmah$]h&]uh1hhhhhhhhKQubh)}(hhh](h)}(h'Testing specific sections with kmemleakh]h'Testing specific sections with kmemleak}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKvubh)}(hXUpon initial bootup your /sys/kernel/debug/kmemleak output page may be quite extensive. This can also be the case if you have very buggy code when doing development. To work around these situations you can use the 'clear' command to clear all reported unreferenced objects from the /sys/kernel/debug/kmemleak output. By issuing a 'scan' after a 'clear' you can find new unreferenced objects; this should help with testing specific sections of code.h]hXUpon initial bootup your /sys/kernel/debug/kmemleak output page may be quite extensive. This can also be the case if you have very buggy code when doing development. To work around these situations you can use the ‘clear’ command to clear all reported unreferenced objects from the /sys/kernel/debug/kmemleak output. By issuing a ‘scan’ after a ‘clear’ you can find new unreferenced objects; this should help with testing specific sections of code.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKxhjhhubh)}(h?To test a critical section on demand with a clean kmemleak do::h]h>To test a critical section on demand with a clean kmemleak do:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj5)}(hv# echo clear > /sys/kernel/debug/kmemleak ... test your kernel or modules ... # echo scan > /sys/kernel/debug/kmemleakh]hv# echo clear > /sys/kernel/debug/kmemleak ... test your kernel or modules ... # echo scan > /sys/kernel/debug/kmemleak}hjsbah}(h]h ]h"]h$]h&]jDjEuh1j4hhhKhjhhubh)}(h'Then as usual to get your report with::h]h&Then as usual to get your report with:}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj5)}(h # cat /sys/kernel/debug/kmemleakh]h # cat /sys/kernel/debug/kmemleak}hj8sbah}(h]h ]h"]h$]h&]jDjEuh1j4hhhKhjhhubeh}(h]'testing-specific-sections-with-kmemleakah ]h"]'testing specific sections with kmemleakah$]h&]uh1hhhhhhhhKvubh)}(hhh](h)}(h!Freeing kmemleak internal objectsh]h!Freeing kmemleak internal objects}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjNhhhhhKubh)}(hTo allow access to previously found memory leaks after kmemleak has been disabled by the user or due to an fatal error, internal kmemleak objects won't be freed when kmemleak is disabled, and those objects may occupy a large part of physical memory.h]hTo allow access to previously found memory leaks after kmemleak has been disabled by the user or due to an fatal error, internal kmemleak objects won’t be freed when kmemleak is disabled, and those objects may occupy a large part of physical memory.}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjNhhubh)}(h0In this situation, you may reclaim memory with::h]h/In this situation, you may reclaim memory with:}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjNhhubj5)}(h)# echo clear > /sys/kernel/debug/kmemleakh]h)# echo clear > /sys/kernel/debug/kmemleak}hj{sbah}(h]h ]h"]h$]h&]jDjEuh1j4hhhKhjNhhubeh}(h]!freeing-kmemleak-internal-objectsah ]h"]!freeing kmemleak internal objectsah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h Kmemleak APIh]h Kmemleak API}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hDSee the include/linux/kmemleak.h header for the functions prototype.h]hDSee the include/linux/kmemleak.h header for the functions prototype.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hhh](j)}(h4``kmemleak_init`` - initialize kmemleakh]h)}(hjh](h)}(h``kmemleak_init``h]h kmemleak_init}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh# - initialize kmemleak}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hD``kmemleak_alloc`` - notify of a memory block allocationh]h)}(hjh](h)}(h``kmemleak_alloc``h]hkmemleak_alloc}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh2 - notify of a memory block allocation}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hK``kmemleak_alloc_percpu`` - notify of a percpu memory block allocationh]h)}(hjh](h)}(h``kmemleak_alloc_percpu``h]hkmemleak_alloc_percpu}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh2 - notify of a percpu memory block allocation}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hH``kmemleak_vmalloc`` - notify of a vmalloc() memory allocationh]h)}(hj$h](h)}(h``kmemleak_vmalloc``h]hkmemleak_vmalloc}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj&ubh4 - notify of a vmalloc() memory allocation}(hj&hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj"ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hA``kmemleak_free`` - notify of a memory block freeingh]h)}(hjIh](h)}(h``kmemleak_free``h]h kmemleak_free}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjKubh0 - notify of a memory block freeing}(hjKhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjGubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hI``kmemleak_free_part`` - notify of a partial memory block freeingh]h)}(hjnh](h)}(h``kmemleak_free_part``h]hkmemleak_free_part}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1hhjpubh3 - notify of a partial memory block freeing}(hjphhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjlubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hH``kmemleak_free_percpu`` - notify of a percpu memory block freeingh]h)}(hjh](h)}(h``kmemleak_free_percpu``h]hkmemleak_free_percpu}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh0 - notify of a percpu memory block freeing}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hE``kmemleak_update_trace`` - update object allocation stack traceh]h)}(hjh](h)}(h``kmemleak_update_trace``h]hkmemleak_update_trace}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh, - update object allocation stack trace}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h5``kmemleak_not_leak`` - mark an object as not a leakh]h)}(hjh](h)}(h``kmemleak_not_leak``h]hkmemleak_not_leak}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh - mark an object as not a leak}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hC``kmemleak_transient_leak`` - mark an object as a transient leakh]h)}(hjh](h)}(h``kmemleak_transient_leak``h]hkmemleak_transient_leak}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh( - mark an object as a transient leak}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hH``kmemleak_ignore`` - do not scan or report an object as leakh]h)}(hj'h](h)}(h``kmemleak_ignore``h]hkmemleak_ignore}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj)ubh5 - do not scan or report an object as leak}(hj)hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj%ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hE``kmemleak_scan_area`` - add scan areas inside a memory blockh]h)}(hjLh](h)}(h``kmemleak_scan_area``h]hkmemleak_scan_area}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjNubh/ - add scan areas inside a memory block}(hjNhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjJubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h3``kmemleak_no_scan`` - do not scan a memory blockh]h)}(hjqh](h)}(h``kmemleak_no_scan``h]hkmemleak_no_scan}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjsubh - do not scan a memory block}(hjshhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjoubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hI``kmemleak_erase`` - erase an old value in a pointer variableh]h)}(hjh](h)}(h``kmemleak_erase``h]hkmemleak_erase}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh7 - erase an old value in a pointer variable}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hM``kmemleak_alloc_recursive`` - as kmemleak_alloc but checks the recursivenessh]h)}(hjh](h)}(h``kmemleak_alloc_recursive``h]hkmemleak_alloc_recursive}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh1 - as kmemleak_alloc but checks the recursiveness}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hO``kmemleak_free_recursive`` - as kmemleak_free but checks the recursiveness h]h)}(hN``kmemleak_free_recursive`` - as kmemleak_free but checks the recursivenessh](h)}(h``kmemleak_free_recursive``h]hkmemleak_free_recursive}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh3 - as kmemleak_free but checks the recursiveness}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]jOjPuh1jhhhKhjhhubh)}(hThe following functions take a physical address as the object pointer and only perform the corresponding action if the address has a lowmem mapping:h]hThe following functions take a physical address as the object pointer and only perform the corresponding action if the address has a lowmem mapping:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hhh](j)}(h``kmemleak_alloc_phys``h]h)}(hj h]h)}(hj h]hkmemleak_alloc_phys}(hj" hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(h``kmemleak_free_part_phys``h]h)}(hj= h]h)}(hj= h]hkmemleak_free_part_phys}(hjB hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj? ubah}(h]h ]h"]h$]h&]uh1hhhhKhj; ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(h``kmemleak_ignore_phys`` h]h)}(h``kmemleak_ignore_phys``h]h)}(hja h]hkmemleak_ignore_phys}(hjc hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj_ ubah}(h]h ]h"]h$]h&]uh1hhhhKhj[ ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubeh}(h]h ]h"]h$]h&]jOjPuh1jhhhKhjhhubeh}(h] kmemleak-apiah ]h"] kmemleak apiah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h&Dealing with false positives/negativesh]h&Dealing with false positives/negatives}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKubh)}(hXThe false negatives are real memory leaks (orphan objects) but not reported by kmemleak because values found during the memory scanning point to such objects. To reduce the number of false negatives, kmemleak provides the kmemleak_ignore, kmemleak_scan_area, kmemleak_no_scan and kmemleak_erase functions (see above). The task stacks also increase the amount of false negatives and their scanning is not enabled by default.h]hXThe false negatives are real memory leaks (orphan objects) but not reported by kmemleak because values found during the memory scanning point to such objects. To reduce the number of false negatives, kmemleak provides the kmemleak_ignore, kmemleak_scan_area, kmemleak_no_scan and kmemleak_erase functions (see above). The task stacks also increase the amount of false negatives and their scanning is not enabled by default.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hX)The false positives are objects wrongly reported as being memory leaks (orphan). For objects known not to be leaks, kmemleak provides the kmemleak_not_leak function. The kmemleak_ignore could also be used if the memory block is known not to contain other pointers and it will no longer be scanned.h]hX)The false positives are objects wrongly reported as being memory leaks (orphan). For objects known not to be leaks, kmemleak provides the kmemleak_not_leak function. The kmemleak_ignore could also be used if the memory block is known not to contain other pointers and it will no longer be scanned.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hX Some of the reported leaks are only transient, especially on SMP systems, because of pointers temporarily stored in CPU registers or stacks. Kmemleak defines MSECS_MIN_AGE (defaulting to 1000) representing the minimum age of an object to be reported as a memory leak.h]hX Some of the reported leaks are only transient, especially on SMP systems, because of pointers temporarily stored in CPU registers or stacks. Kmemleak defines MSECS_MIN_AGE (defaulting to 1000) representing the minimum age of an object to be reported as a memory leak.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubeh}(h]&dealing-with-false-positives-negativesah ]h"]&dealing with false positives/negativesah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hLimitations and Drawbacksh]hLimitations and Drawbacks}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKubh)}(hX8The main drawback is the reduced performance of memory allocation and freeing. To avoid other penalties, the memory scanning is only performed when the /sys/kernel/debug/kmemleak file is read. Anyway, this tool is intended for debugging purposes where the performance might not be the most important requirement.h]hX8The main drawback is the reduced performance of memory allocation and freeing. To avoid other penalties, the memory scanning is only performed when the /sys/kernel/debug/kmemleak file is read. Anyway, this tool is intended for debugging purposes where the performance might not be the most important requirement.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hTo keep the algorithm simple, kmemleak scans for values pointing to any address inside a block's address range. This may lead to an increased number of false negatives. However, it is likely that a real memory leak will eventually become visible.h]hTo keep the algorithm simple, kmemleak scans for values pointing to any address inside a block’s address range. This may lead to an increased number of false negatives. However, it is likely that a real memory leak will eventually become visible.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hAnother source of false negatives is the data stored in non-pointer values. In a future version, kmemleak could only scan the pointer members in the allocated structures. This feature would solve many of the false negative cases described above.h]hAnother source of false negatives is the data stored in non-pointer values. In a future version, kmemleak could only scan the pointer members in the allocated structures. This feature would solve many of the false negative cases described above.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hXThe tool can report false positives. These are cases where an allocated block doesn't need to be freed (some cases in the init_call functions), the pointer is calculated by other methods than the usual container_of macro or the pointer is stored in a location not scanned by kmemleak.h]hXThe tool can report false positives. These are cases where an allocated block doesn’t need to be freed (some cases in the init_call functions), the pointer is calculated by other methods than the usual container_of macro or the pointer is stored in a location not scanned by kmemleak.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(h-Page allocations and ioremap are not tracked.h]h-Page allocations and ioremap are not tracked.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubeh}(h]limitations-and-drawbacksah ]h"]limitations and drawbacksah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hTesting with kmemleak-testh]hTesting with kmemleak-test}(hj/ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj, hhhhhKubh)}(hXTo check if you have all set up to use kmemleak, you can use the kmemleak-test module, a module that deliberately leaks memory. Set CONFIG_SAMPLE_KMEMLEAK as module (it can't be used as built-in) and boot the kernel with kmemleak enabled. Load the module and perform a scan with::h]hXTo check if you have all set up to use kmemleak, you can use the kmemleak-test module, a module that deliberately leaks memory. Set CONFIG_SAMPLE_KMEMLEAK as module (it can’t be used as built-in) and boot the kernel with kmemleak enabled. Load the module and perform a scan with:}(hj= hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj, hhubj5)}(hA# modprobe kmemleak-test # echo scan > /sys/kernel/debug/kmemleakh]hA# modprobe kmemleak-test # echo scan > /sys/kernel/debug/kmemleak}hjK sbah}(h]h ]h"]h$]h&]jDjEuh1j4hhhKhj, hhubh)}(hNote that the you may not get results instantly or on the first scanning. When kmemleak gets results, it'll log ``kmemleak: new suspected memory leaks``. Then read the file to see then::h](hrNote that the you may not get results instantly or on the first scanning. When kmemleak gets results, it’ll log }(hjY hhhNhNubh)}(h9``kmemleak: new suspected memory leaks``h]h5kmemleak: new suspected memory leaks}(hja hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjY ubh!. Then read the file to see then:}(hjY hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj, hhubj5)}(hX# cat /sys/kernel/debug/kmemleak unreferenced object 0xffff89862ca702e8 (size 32): comm "modprobe", pid 2088, jiffies 4294680594 (age 375.486s) 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 a5 kkkkkkkkkkkkkkk. backtrace: [<00000000e0a73ec7>] 0xffffffffc01d2036 [<000000000c5d2a46>] do_one_initcall+0x41/0x1df [<0000000046db7e0a>] do_init_module+0x55/0x200 [<00000000542b9814>] load_module+0x203c/0x2480 [<00000000c2850256>] __do_sys_finit_module+0xba/0xe0 [<000000006564e7ef>] do_syscall_64+0x43/0x110 [<000000007c873fa6>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 ...h]hX# cat /sys/kernel/debug/kmemleak unreferenced object 0xffff89862ca702e8 (size 32): comm "modprobe", pid 2088, jiffies 4294680594 (age 375.486s) 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 a5 kkkkkkkkkkkkkkk. backtrace: [<00000000e0a73ec7>] 0xffffffffc01d2036 [<000000000c5d2a46>] do_one_initcall+0x41/0x1df [<0000000046db7e0a>] do_init_module+0x55/0x200 [<00000000542b9814>] load_module+0x203c/0x2480 [<00000000c2850256>] __do_sys_finit_module+0xba/0xe0 [<000000006564e7ef>] do_syscall_64+0x43/0x110 [<000000007c873fa6>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 ...}hjy sbah}(h]h ]h"]h$]h&]jDjEuh1j4hhhKhj, hhubh)}(h[Removing the module with ``rmmod kmemleak_test`` should also trigger some kmemleak results.h](hRemoving the module with }(hj hhhNhNubh)}(h``rmmod kmemleak_test``h]hrmmod kmemleak_test}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh+ should also trigger some kmemleak results.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj, hhubeh}(h]testing-with-kmemleak-testah ]h"]testing with kmemleak-testah$]h&]uh1hhhhhhhhKubeh}(h]kernel-memory-leak-detectorah ]h"]kernel memory leak detectorah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerj error_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(j j hhjjjjjKjHjjj j j j j) j& j j u nametypes}(j hjjjKjj j j) j uh}(j hhhjjjjjHjjjNj jj j j& j j j, u footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages] transformerN include_log] decorationNhhub.