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