]> bbs.cooldavid.org Git - net-next-2.6.git/blame - Documentation/kmemleak.txt
cfq: fix the log message after dispatched a request
[net-next-2.6.git] / Documentation / kmemleak.txt
CommitLineData
04f70336
CM
1Kernel Memory Leak Detector
2===========================
3
4Introduction
5------------
6
7Kmemleak provides a way of detecting possible kernel memory leaks in a
8way similar to a tracing garbage collector
9(http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Tracing_garbage_collectors),
10with the difference that the orphan objects are not freed but only
11reported via /sys/kernel/debug/kmemleak. A similar method is used by the
12Valgrind tool (memcheck --leak-check) to detect the memory leaks in
13user-space applications.
14
15Usage
16-----
17
18CONFIG_DEBUG_KMEMLEAK in "Kernel hacking" has to be enabled. A kernel
bab4a34a 19thread scans the memory every 10 minutes (by default) and prints the
4698c1f2
CM
20number of new unreferenced objects found. To display the details of all
21the possible memory leaks:
04f70336
CM
22
23 # mount -t debugfs nodev /sys/kernel/debug/
24 # cat /sys/kernel/debug/kmemleak
25
4698c1f2
CM
26To trigger an intermediate memory scan:
27
28 # echo scan > /sys/kernel/debug/kmemleak
29
04f70336
CM
30Note that the orphan objects are listed in the order they were allocated
31and one object at the beginning of the list may cause other subsequent
32objects to be reported as orphan.
33
34Memory scanning parameters can be modified at run-time by writing to the
35/sys/kernel/debug/kmemleak file. The following parameters are supported:
36
37 off - disable kmemleak (irreversible)
e0a2a160 38 stack=on - enable the task stacks scanning (default)
04f70336 39 stack=off - disable the tasks stacks scanning
e0a2a160 40 scan=on - start the automatic memory scanning thread (default)
04f70336 41 scan=off - stop the automatic memory scanning thread
e0a2a160
CM
42 scan=<secs> - set the automatic memory scanning period in seconds
43 (default 600, 0 to stop the automatic scanning)
4698c1f2 44 scan - trigger a memory scan
04f70336
CM
45
46Kmemleak can also be disabled at boot-time by passing "kmemleak=off" on
47the kernel command line.
48
a9d9058a
CM
49Memory may be allocated or freed before kmemleak is initialised and
50these actions are stored in an early log buffer. The size of this buffer
51is configured via the CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE option.
52
04f70336
CM
53Basic Algorithm
54---------------
55
56The memory allocations via kmalloc, vmalloc, kmem_cache_alloc and
57friends are traced and the pointers, together with additional
58information like size and stack trace, are stored in a prio search tree.
59The corresponding freeing function calls are tracked and the pointers
60removed from the kmemleak data structures.
61
62An allocated block of memory is considered orphan if no pointer to its
63start address or to any location inside the block can be found by
64scanning the memory (including saved registers). This means that there
65might be no way for the kernel to pass the address of the allocated
66block to a freeing function and therefore the block is considered a
67memory leak.
68
69The scanning algorithm steps:
70
71 1. mark all objects as white (remaining white objects will later be
72 considered orphan)
73 2. scan the memory starting with the data section and stacks, checking
74 the values against the addresses stored in the prio search tree. If
75 a pointer to a white object is found, the object is added to the
76 gray list
77 3. scan the gray objects for matching addresses (some white objects
78 can become gray and added at the end of the gray list) until the
79 gray set is finished
80 4. the remaining white objects are considered orphan and reported via
81 /sys/kernel/debug/kmemleak
82
83Some allocated memory blocks have pointers stored in the kernel's
84internal data structures and they cannot be detected as orphans. To
85avoid this, kmemleak can also store the number of values pointing to an
86address inside the block address range that need to be found so that the
87block is not considered a leak. One example is __vmalloc().
88
89Kmemleak API
90------------
91
92See the include/linux/kmemleak.h header for the functions prototype.
93
94kmemleak_init - initialize kmemleak
95kmemleak_alloc - notify of a memory block allocation
96kmemleak_free - notify of a memory block freeing
97kmemleak_not_leak - mark an object as not a leak
98kmemleak_ignore - do not scan or report an object as leak
99kmemleak_scan_area - add scan areas inside a memory block
100kmemleak_no_scan - do not scan a memory block
101kmemleak_erase - erase an old value in a pointer variable
102kmemleak_alloc_recursive - as kmemleak_alloc but checks the recursiveness
103kmemleak_free_recursive - as kmemleak_free but checks the recursiveness
104
105Dealing with false positives/negatives
106--------------------------------------
107
108The false negatives are real memory leaks (orphan objects) but not
109reported by kmemleak because values found during the memory scanning
110point to such objects. To reduce the number of false negatives, kmemleak
111provides the kmemleak_ignore, kmemleak_scan_area, kmemleak_no_scan and
112kmemleak_erase functions (see above). The task stacks also increase the
113amount of false negatives and their scanning is not enabled by default.
114
115The false positives are objects wrongly reported as being memory leaks
116(orphan). For objects known not to be leaks, kmemleak provides the
117kmemleak_not_leak function. The kmemleak_ignore could also be used if
118the memory block is known not to contain other pointers and it will no
119longer be scanned.
120
121Some of the reported leaks are only transient, especially on SMP
122systems, because of pointers temporarily stored in CPU registers or
123stacks. Kmemleak defines MSECS_MIN_AGE (defaulting to 1000) representing
124the minimum age of an object to be reported as a memory leak.
125
126Limitations and Drawbacks
127-------------------------
128
129The main drawback is the reduced performance of memory allocation and
130freeing. To avoid other penalties, the memory scanning is only performed
131when the /sys/kernel/debug/kmemleak file is read. Anyway, this tool is
132intended for debugging purposes where the performance might not be the
133most important requirement.
134
135To keep the algorithm simple, kmemleak scans for values pointing to any
136address inside a block's address range. This may lead to an increased
137number of false negatives. However, it is likely that a real memory leak
138will eventually become visible.
139
140Another source of false negatives is the data stored in non-pointer
141values. In a future version, kmemleak could only scan the pointer
142members in the allocated structures. This feature would solve many of
143the false negative cases described above.
144
145The tool can report false positives. These are cases where an allocated
146block doesn't need to be freed (some cases in the init_call functions),
147the pointer is calculated by other methods than the usual container_of
148macro or the pointer is stored in a location not scanned by kmemleak.
149
150Page allocations and ioremap are not tracked. Only the ARM and x86
151architectures are currently supported.