]>
Commit | Line | Data |
---|---|---|
3c7b4e6b CM |
1 | /* |
2 | * mm/kmemleak.c | |
3 | * | |
4 | * Copyright (C) 2008 ARM Limited | |
5 | * Written by Catalin Marinas <catalin.marinas@arm.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | * | |
21 | * For more information on the algorithm and kmemleak usage, please see | |
22 | * Documentation/kmemleak.txt. | |
23 | * | |
24 | * Notes on locking | |
25 | * ---------------- | |
26 | * | |
27 | * The following locks and mutexes are used by kmemleak: | |
28 | * | |
29 | * - kmemleak_lock (rwlock): protects the object_list modifications and | |
30 | * accesses to the object_tree_root. The object_list is the main list | |
31 | * holding the metadata (struct kmemleak_object) for the allocated memory | |
32 | * blocks. The object_tree_root is a priority search tree used to look-up | |
33 | * metadata based on a pointer to the corresponding memory block. The | |
34 | * kmemleak_object structures are added to the object_list and | |
35 | * object_tree_root in the create_object() function called from the | |
36 | * kmemleak_alloc() callback and removed in delete_object() called from the | |
37 | * kmemleak_free() callback | |
38 | * - kmemleak_object.lock (spinlock): protects a kmemleak_object. Accesses to | |
39 | * the metadata (e.g. count) are protected by this lock. Note that some | |
40 | * members of this structure may be protected by other means (atomic or | |
41 | * kmemleak_lock). This lock is also held when scanning the corresponding | |
42 | * memory block to avoid the kernel freeing it via the kmemleak_free() | |
43 | * callback. This is less heavyweight than holding a global lock like | |
44 | * kmemleak_lock during scanning | |
45 | * - scan_mutex (mutex): ensures that only one thread may scan the memory for | |
46 | * unreferenced objects at a time. The gray_list contains the objects which | |
47 | * are already referenced or marked as false positives and need to be | |
48 | * scanned. This list is only modified during a scanning episode when the | |
49 | * scan_mutex is held. At the end of a scan, the gray_list is always empty. | |
50 | * Note that the kmemleak_object.use_count is incremented when an object is | |
4698c1f2 CM |
51 | * added to the gray_list and therefore cannot be freed. This mutex also |
52 | * prevents multiple users of the "kmemleak" debugfs file together with | |
53 | * modifications to the memory scanning parameters including the scan_thread | |
54 | * pointer | |
3c7b4e6b CM |
55 | * |
56 | * The kmemleak_object structures have a use_count incremented or decremented | |
57 | * using the get_object()/put_object() functions. When the use_count becomes | |
58 | * 0, this count can no longer be incremented and put_object() schedules the | |
59 | * kmemleak_object freeing via an RCU callback. All calls to the get_object() | |
60 | * function must be protected by rcu_read_lock() to avoid accessing a freed | |
61 | * structure. | |
62 | */ | |
63 | ||
ae281064 JP |
64 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
65 | ||
3c7b4e6b CM |
66 | #include <linux/init.h> |
67 | #include <linux/kernel.h> | |
68 | #include <linux/list.h> | |
69 | #include <linux/sched.h> | |
70 | #include <linux/jiffies.h> | |
71 | #include <linux/delay.h> | |
b95f1b31 | 72 | #include <linux/export.h> |
3c7b4e6b CM |
73 | #include <linux/kthread.h> |
74 | #include <linux/prio_tree.h> | |
3c7b4e6b CM |
75 | #include <linux/fs.h> |
76 | #include <linux/debugfs.h> | |
77 | #include <linux/seq_file.h> | |
78 | #include <linux/cpumask.h> | |
79 | #include <linux/spinlock.h> | |
80 | #include <linux/mutex.h> | |
81 | #include <linux/rcupdate.h> | |
82 | #include <linux/stacktrace.h> | |
83 | #include <linux/cache.h> | |
84 | #include <linux/percpu.h> | |
85 | #include <linux/hardirq.h> | |
86 | #include <linux/mmzone.h> | |
87 | #include <linux/slab.h> | |
88 | #include <linux/thread_info.h> | |
89 | #include <linux/err.h> | |
90 | #include <linux/uaccess.h> | |
91 | #include <linux/string.h> | |
92 | #include <linux/nodemask.h> | |
93 | #include <linux/mm.h> | |
179a8100 | 94 | #include <linux/workqueue.h> |
04609ccc | 95 | #include <linux/crc32.h> |
3c7b4e6b CM |
96 | |
97 | #include <asm/sections.h> | |
98 | #include <asm/processor.h> | |
60063497 | 99 | #include <linux/atomic.h> |
3c7b4e6b | 100 | |
8e019366 | 101 | #include <linux/kmemcheck.h> |
3c7b4e6b CM |
102 | #include <linux/kmemleak.h> |
103 | ||
104 | /* | |
105 | * Kmemleak configuration and common defines. | |
106 | */ | |
107 | #define MAX_TRACE 16 /* stack trace length */ | |
3c7b4e6b | 108 | #define MSECS_MIN_AGE 5000 /* minimum object age for reporting */ |
3c7b4e6b CM |
109 | #define SECS_FIRST_SCAN 60 /* delay before the first scan */ |
110 | #define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */ | |
af98603d | 111 | #define MAX_SCAN_SIZE 4096 /* maximum size of a scanned block */ |
3c7b4e6b CM |
112 | |
113 | #define BYTES_PER_POINTER sizeof(void *) | |
114 | ||
216c04b0 | 115 | /* GFP bitmask for kmemleak internal allocations */ |
6ae4bd1f CM |
116 | #define gfp_kmemleak_mask(gfp) (((gfp) & (GFP_KERNEL | GFP_ATOMIC)) | \ |
117 | __GFP_NORETRY | __GFP_NOMEMALLOC | \ | |
118 | __GFP_NOWARN) | |
216c04b0 | 119 | |
3c7b4e6b CM |
120 | /* scanning area inside a memory block */ |
121 | struct kmemleak_scan_area { | |
122 | struct hlist_node node; | |
c017b4be CM |
123 | unsigned long start; |
124 | size_t size; | |
3c7b4e6b CM |
125 | }; |
126 | ||
a1084c87 LR |
127 | #define KMEMLEAK_GREY 0 |
128 | #define KMEMLEAK_BLACK -1 | |
129 | ||
3c7b4e6b CM |
130 | /* |
131 | * Structure holding the metadata for each allocated memory block. | |
132 | * Modifications to such objects should be made while holding the | |
133 | * object->lock. Insertions or deletions from object_list, gray_list or | |
134 | * tree_node are already protected by the corresponding locks or mutex (see | |
135 | * the notes on locking above). These objects are reference-counted | |
136 | * (use_count) and freed using the RCU mechanism. | |
137 | */ | |
138 | struct kmemleak_object { | |
139 | spinlock_t lock; | |
140 | unsigned long flags; /* object status flags */ | |
141 | struct list_head object_list; | |
142 | struct list_head gray_list; | |
143 | struct prio_tree_node tree_node; | |
144 | struct rcu_head rcu; /* object_list lockless traversal */ | |
145 | /* object usage count; object freed when use_count == 0 */ | |
146 | atomic_t use_count; | |
147 | unsigned long pointer; | |
148 | size_t size; | |
149 | /* minimum number of a pointers found before it is considered leak */ | |
150 | int min_count; | |
151 | /* the total number of pointers found pointing to this object */ | |
152 | int count; | |
04609ccc CM |
153 | /* checksum for detecting modified objects */ |
154 | u32 checksum; | |
3c7b4e6b CM |
155 | /* memory ranges to be scanned inside an object (empty for all) */ |
156 | struct hlist_head area_list; | |
157 | unsigned long trace[MAX_TRACE]; | |
158 | unsigned int trace_len; | |
159 | unsigned long jiffies; /* creation timestamp */ | |
160 | pid_t pid; /* pid of the current task */ | |
161 | char comm[TASK_COMM_LEN]; /* executable name */ | |
162 | }; | |
163 | ||
164 | /* flag representing the memory block allocation status */ | |
165 | #define OBJECT_ALLOCATED (1 << 0) | |
166 | /* flag set after the first reporting of an unreference object */ | |
167 | #define OBJECT_REPORTED (1 << 1) | |
168 | /* flag set to not scan the object */ | |
169 | #define OBJECT_NO_SCAN (1 << 2) | |
170 | ||
0494e082 SS |
171 | /* number of bytes to print per line; must be 16 or 32 */ |
172 | #define HEX_ROW_SIZE 16 | |
173 | /* number of bytes to print at a time (1, 2, 4, 8) */ | |
174 | #define HEX_GROUP_SIZE 1 | |
175 | /* include ASCII after the hex output */ | |
176 | #define HEX_ASCII 1 | |
177 | /* max number of lines to be printed */ | |
178 | #define HEX_MAX_LINES 2 | |
179 | ||
3c7b4e6b CM |
180 | /* the list of all allocated objects */ |
181 | static LIST_HEAD(object_list); | |
182 | /* the list of gray-colored objects (see color_gray comment below) */ | |
183 | static LIST_HEAD(gray_list); | |
184 | /* prio search tree for object boundaries */ | |
185 | static struct prio_tree_root object_tree_root; | |
186 | /* rw_lock protecting the access to object_list and prio_tree_root */ | |
187 | static DEFINE_RWLOCK(kmemleak_lock); | |
188 | ||
189 | /* allocation caches for kmemleak internal data */ | |
190 | static struct kmem_cache *object_cache; | |
191 | static struct kmem_cache *scan_area_cache; | |
192 | ||
193 | /* set if tracing memory operations is enabled */ | |
194 | static atomic_t kmemleak_enabled = ATOMIC_INIT(0); | |
195 | /* set in the late_initcall if there were no errors */ | |
196 | static atomic_t kmemleak_initialized = ATOMIC_INIT(0); | |
197 | /* enables or disables early logging of the memory operations */ | |
198 | static atomic_t kmemleak_early_log = ATOMIC_INIT(1); | |
5f79020c CM |
199 | /* set if a kmemleak warning was issued */ |
200 | static atomic_t kmemleak_warning = ATOMIC_INIT(0); | |
201 | /* set if a fatal kmemleak error has occurred */ | |
3c7b4e6b CM |
202 | static atomic_t kmemleak_error = ATOMIC_INIT(0); |
203 | ||
204 | /* minimum and maximum address that may be valid pointers */ | |
205 | static unsigned long min_addr = ULONG_MAX; | |
206 | static unsigned long max_addr; | |
207 | ||
3c7b4e6b | 208 | static struct task_struct *scan_thread; |
acf4968e | 209 | /* used to avoid reporting of recently allocated objects */ |
3c7b4e6b | 210 | static unsigned long jiffies_min_age; |
acf4968e | 211 | static unsigned long jiffies_last_scan; |
3c7b4e6b CM |
212 | /* delay between automatic memory scannings */ |
213 | static signed long jiffies_scan_wait; | |
214 | /* enables or disables the task stacks scanning */ | |
e0a2a160 | 215 | static int kmemleak_stack_scan = 1; |
4698c1f2 | 216 | /* protects the memory scanning, parameters and debug/kmemleak file access */ |
3c7b4e6b | 217 | static DEFINE_MUTEX(scan_mutex); |
ab0155a2 JB |
218 | /* setting kmemleak=on, will set this var, skipping the disable */ |
219 | static int kmemleak_skip_disable; | |
220 | ||
3c7b4e6b | 221 | |
3c7b4e6b | 222 | /* |
2030117d | 223 | * Early object allocation/freeing logging. Kmemleak is initialized after the |
3c7b4e6b | 224 | * kernel allocator. However, both the kernel allocator and kmemleak may |
2030117d | 225 | * allocate memory blocks which need to be tracked. Kmemleak defines an |
3c7b4e6b CM |
226 | * arbitrary buffer to hold the allocation/freeing information before it is |
227 | * fully initialized. | |
228 | */ | |
229 | ||
230 | /* kmemleak operation type for early logging */ | |
231 | enum { | |
232 | KMEMLEAK_ALLOC, | |
f528f0b8 | 233 | KMEMLEAK_ALLOC_PERCPU, |
3c7b4e6b | 234 | KMEMLEAK_FREE, |
53238a60 | 235 | KMEMLEAK_FREE_PART, |
f528f0b8 | 236 | KMEMLEAK_FREE_PERCPU, |
3c7b4e6b CM |
237 | KMEMLEAK_NOT_LEAK, |
238 | KMEMLEAK_IGNORE, | |
239 | KMEMLEAK_SCAN_AREA, | |
240 | KMEMLEAK_NO_SCAN | |
241 | }; | |
242 | ||
243 | /* | |
244 | * Structure holding the information passed to kmemleak callbacks during the | |
245 | * early logging. | |
246 | */ | |
247 | struct early_log { | |
248 | int op_type; /* kmemleak operation type */ | |
249 | const void *ptr; /* allocated/freed memory block */ | |
250 | size_t size; /* memory block size */ | |
251 | int min_count; /* minimum reference count */ | |
fd678967 CM |
252 | unsigned long trace[MAX_TRACE]; /* stack trace */ |
253 | unsigned int trace_len; /* stack trace length */ | |
3c7b4e6b CM |
254 | }; |
255 | ||
256 | /* early logging buffer and current position */ | |
a6186d89 CM |
257 | static struct early_log |
258 | early_log[CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE] __initdata; | |
259 | static int crt_early_log __initdata; | |
3c7b4e6b CM |
260 | |
261 | static void kmemleak_disable(void); | |
262 | ||
263 | /* | |
264 | * Print a warning and dump the stack trace. | |
265 | */ | |
5f79020c CM |
266 | #define kmemleak_warn(x...) do { \ |
267 | pr_warning(x); \ | |
268 | dump_stack(); \ | |
269 | atomic_set(&kmemleak_warning, 1); \ | |
3c7b4e6b CM |
270 | } while (0) |
271 | ||
272 | /* | |
25985edc | 273 | * Macro invoked when a serious kmemleak condition occurred and cannot be |
2030117d | 274 | * recovered from. Kmemleak will be disabled and further allocation/freeing |
3c7b4e6b CM |
275 | * tracing no longer available. |
276 | */ | |
000814f4 | 277 | #define kmemleak_stop(x...) do { \ |
3c7b4e6b CM |
278 | kmemleak_warn(x); \ |
279 | kmemleak_disable(); \ | |
280 | } while (0) | |
281 | ||
0494e082 SS |
282 | /* |
283 | * Printing of the objects hex dump to the seq file. The number of lines to be | |
284 | * printed is limited to HEX_MAX_LINES to prevent seq file spamming. The | |
285 | * actual number of printed bytes depends on HEX_ROW_SIZE. It must be called | |
286 | * with the object->lock held. | |
287 | */ | |
288 | static void hex_dump_object(struct seq_file *seq, | |
289 | struct kmemleak_object *object) | |
290 | { | |
291 | const u8 *ptr = (const u8 *)object->pointer; | |
292 | int i, len, remaining; | |
293 | unsigned char linebuf[HEX_ROW_SIZE * 5]; | |
294 | ||
295 | /* limit the number of lines to HEX_MAX_LINES */ | |
296 | remaining = len = | |
297 | min(object->size, (size_t)(HEX_MAX_LINES * HEX_ROW_SIZE)); | |
298 | ||
299 | seq_printf(seq, " hex dump (first %d bytes):\n", len); | |
300 | for (i = 0; i < len; i += HEX_ROW_SIZE) { | |
301 | int linelen = min(remaining, HEX_ROW_SIZE); | |
302 | ||
303 | remaining -= HEX_ROW_SIZE; | |
304 | hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE, | |
305 | HEX_GROUP_SIZE, linebuf, sizeof(linebuf), | |
306 | HEX_ASCII); | |
307 | seq_printf(seq, " %s\n", linebuf); | |
308 | } | |
309 | } | |
310 | ||
3c7b4e6b CM |
311 | /* |
312 | * Object colors, encoded with count and min_count: | |
313 | * - white - orphan object, not enough references to it (count < min_count) | |
314 | * - gray - not orphan, not marked as false positive (min_count == 0) or | |
315 | * sufficient references to it (count >= min_count) | |
316 | * - black - ignore, it doesn't contain references (e.g. text section) | |
317 | * (min_count == -1). No function defined for this color. | |
318 | * Newly created objects don't have any color assigned (object->count == -1) | |
319 | * before the next memory scan when they become white. | |
320 | */ | |
4a558dd6 | 321 | static bool color_white(const struct kmemleak_object *object) |
3c7b4e6b | 322 | { |
a1084c87 LR |
323 | return object->count != KMEMLEAK_BLACK && |
324 | object->count < object->min_count; | |
3c7b4e6b CM |
325 | } |
326 | ||
4a558dd6 | 327 | static bool color_gray(const struct kmemleak_object *object) |
3c7b4e6b | 328 | { |
a1084c87 LR |
329 | return object->min_count != KMEMLEAK_BLACK && |
330 | object->count >= object->min_count; | |
3c7b4e6b CM |
331 | } |
332 | ||
3c7b4e6b CM |
333 | /* |
334 | * Objects are considered unreferenced only if their color is white, they have | |
335 | * not be deleted and have a minimum age to avoid false positives caused by | |
336 | * pointers temporarily stored in CPU registers. | |
337 | */ | |
4a558dd6 | 338 | static bool unreferenced_object(struct kmemleak_object *object) |
3c7b4e6b | 339 | { |
04609ccc | 340 | return (color_white(object) && object->flags & OBJECT_ALLOCATED) && |
acf4968e CM |
341 | time_before_eq(object->jiffies + jiffies_min_age, |
342 | jiffies_last_scan); | |
3c7b4e6b CM |
343 | } |
344 | ||
345 | /* | |
bab4a34a CM |
346 | * Printing of the unreferenced objects information to the seq file. The |
347 | * print_unreferenced function must be called with the object->lock held. | |
3c7b4e6b | 348 | */ |
3c7b4e6b CM |
349 | static void print_unreferenced(struct seq_file *seq, |
350 | struct kmemleak_object *object) | |
351 | { | |
352 | int i; | |
fefdd336 | 353 | unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies); |
3c7b4e6b | 354 | |
bab4a34a CM |
355 | seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n", |
356 | object->pointer, object->size); | |
fefdd336 CM |
357 | seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n", |
358 | object->comm, object->pid, object->jiffies, | |
359 | msecs_age / 1000, msecs_age % 1000); | |
0494e082 | 360 | hex_dump_object(seq, object); |
bab4a34a | 361 | seq_printf(seq, " backtrace:\n"); |
3c7b4e6b CM |
362 | |
363 | for (i = 0; i < object->trace_len; i++) { | |
364 | void *ptr = (void *)object->trace[i]; | |
bab4a34a | 365 | seq_printf(seq, " [<%p>] %pS\n", ptr, ptr); |
3c7b4e6b CM |
366 | } |
367 | } | |
368 | ||
369 | /* | |
370 | * Print the kmemleak_object information. This function is used mainly for | |
371 | * debugging special cases when kmemleak operations. It must be called with | |
372 | * the object->lock held. | |
373 | */ | |
374 | static void dump_object_info(struct kmemleak_object *object) | |
375 | { | |
376 | struct stack_trace trace; | |
377 | ||
378 | trace.nr_entries = object->trace_len; | |
379 | trace.entries = object->trace; | |
380 | ||
ae281064 | 381 | pr_notice("Object 0x%08lx (size %zu):\n", |
3c7b4e6b CM |
382 | object->tree_node.start, object->size); |
383 | pr_notice(" comm \"%s\", pid %d, jiffies %lu\n", | |
384 | object->comm, object->pid, object->jiffies); | |
385 | pr_notice(" min_count = %d\n", object->min_count); | |
386 | pr_notice(" count = %d\n", object->count); | |
189d84ed | 387 | pr_notice(" flags = 0x%lx\n", object->flags); |
04609ccc | 388 | pr_notice(" checksum = %d\n", object->checksum); |
3c7b4e6b CM |
389 | pr_notice(" backtrace:\n"); |
390 | print_stack_trace(&trace, 4); | |
391 | } | |
392 | ||
393 | /* | |
394 | * Look-up a memory block metadata (kmemleak_object) in the priority search | |
395 | * tree based on a pointer value. If alias is 0, only values pointing to the | |
396 | * beginning of the memory block are allowed. The kmemleak_lock must be held | |
397 | * when calling this function. | |
398 | */ | |
399 | static struct kmemleak_object *lookup_object(unsigned long ptr, int alias) | |
400 | { | |
401 | struct prio_tree_node *node; | |
402 | struct prio_tree_iter iter; | |
403 | struct kmemleak_object *object; | |
404 | ||
405 | prio_tree_iter_init(&iter, &object_tree_root, ptr, ptr); | |
406 | node = prio_tree_next(&iter); | |
407 | if (node) { | |
408 | object = prio_tree_entry(node, struct kmemleak_object, | |
409 | tree_node); | |
410 | if (!alias && object->pointer != ptr) { | |
5f79020c CM |
411 | kmemleak_warn("Found object by alias at 0x%08lx\n", |
412 | ptr); | |
a7686a45 | 413 | dump_object_info(object); |
3c7b4e6b CM |
414 | object = NULL; |
415 | } | |
416 | } else | |
417 | object = NULL; | |
418 | ||
419 | return object; | |
420 | } | |
421 | ||
422 | /* | |
423 | * Increment the object use_count. Return 1 if successful or 0 otherwise. Note | |
424 | * that once an object's use_count reached 0, the RCU freeing was already | |
425 | * registered and the object should no longer be used. This function must be | |
426 | * called under the protection of rcu_read_lock(). | |
427 | */ | |
428 | static int get_object(struct kmemleak_object *object) | |
429 | { | |
430 | return atomic_inc_not_zero(&object->use_count); | |
431 | } | |
432 | ||
433 | /* | |
434 | * RCU callback to free a kmemleak_object. | |
435 | */ | |
436 | static void free_object_rcu(struct rcu_head *rcu) | |
437 | { | |
438 | struct hlist_node *elem, *tmp; | |
439 | struct kmemleak_scan_area *area; | |
440 | struct kmemleak_object *object = | |
441 | container_of(rcu, struct kmemleak_object, rcu); | |
442 | ||
443 | /* | |
444 | * Once use_count is 0 (guaranteed by put_object), there is no other | |
445 | * code accessing this object, hence no need for locking. | |
446 | */ | |
447 | hlist_for_each_entry_safe(area, elem, tmp, &object->area_list, node) { | |
448 | hlist_del(elem); | |
449 | kmem_cache_free(scan_area_cache, area); | |
450 | } | |
451 | kmem_cache_free(object_cache, object); | |
452 | } | |
453 | ||
454 | /* | |
455 | * Decrement the object use_count. Once the count is 0, free the object using | |
456 | * an RCU callback. Since put_object() may be called via the kmemleak_free() -> | |
457 | * delete_object() path, the delayed RCU freeing ensures that there is no | |
458 | * recursive call to the kernel allocator. Lock-less RCU object_list traversal | |
459 | * is also possible. | |
460 | */ | |
461 | static void put_object(struct kmemleak_object *object) | |
462 | { | |
463 | if (!atomic_dec_and_test(&object->use_count)) | |
464 | return; | |
465 | ||
466 | /* should only get here after delete_object was called */ | |
467 | WARN_ON(object->flags & OBJECT_ALLOCATED); | |
468 | ||
469 | call_rcu(&object->rcu, free_object_rcu); | |
470 | } | |
471 | ||
472 | /* | |
473 | * Look up an object in the prio search tree and increase its use_count. | |
474 | */ | |
475 | static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias) | |
476 | { | |
477 | unsigned long flags; | |
478 | struct kmemleak_object *object = NULL; | |
479 | ||
480 | rcu_read_lock(); | |
481 | read_lock_irqsave(&kmemleak_lock, flags); | |
482 | if (ptr >= min_addr && ptr < max_addr) | |
483 | object = lookup_object(ptr, alias); | |
484 | read_unlock_irqrestore(&kmemleak_lock, flags); | |
485 | ||
486 | /* check whether the object is still available */ | |
487 | if (object && !get_object(object)) | |
488 | object = NULL; | |
489 | rcu_read_unlock(); | |
490 | ||
491 | return object; | |
492 | } | |
493 | ||
fd678967 CM |
494 | /* |
495 | * Save stack trace to the given array of MAX_TRACE size. | |
496 | */ | |
497 | static int __save_stack_trace(unsigned long *trace) | |
498 | { | |
499 | struct stack_trace stack_trace; | |
500 | ||
501 | stack_trace.max_entries = MAX_TRACE; | |
502 | stack_trace.nr_entries = 0; | |
503 | stack_trace.entries = trace; | |
504 | stack_trace.skip = 2; | |
505 | save_stack_trace(&stack_trace); | |
506 | ||
507 | return stack_trace.nr_entries; | |
508 | } | |
509 | ||
3c7b4e6b CM |
510 | /* |
511 | * Create the metadata (struct kmemleak_object) corresponding to an allocated | |
512 | * memory block and add it to the object_list and object_tree_root. | |
513 | */ | |
fd678967 CM |
514 | static struct kmemleak_object *create_object(unsigned long ptr, size_t size, |
515 | int min_count, gfp_t gfp) | |
3c7b4e6b CM |
516 | { |
517 | unsigned long flags; | |
518 | struct kmemleak_object *object; | |
519 | struct prio_tree_node *node; | |
3c7b4e6b | 520 | |
6ae4bd1f | 521 | object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp)); |
3c7b4e6b | 522 | if (!object) { |
6ae4bd1f CM |
523 | pr_warning("Cannot allocate a kmemleak_object structure\n"); |
524 | kmemleak_disable(); | |
fd678967 | 525 | return NULL; |
3c7b4e6b CM |
526 | } |
527 | ||
528 | INIT_LIST_HEAD(&object->object_list); | |
529 | INIT_LIST_HEAD(&object->gray_list); | |
530 | INIT_HLIST_HEAD(&object->area_list); | |
531 | spin_lock_init(&object->lock); | |
532 | atomic_set(&object->use_count, 1); | |
04609ccc | 533 | object->flags = OBJECT_ALLOCATED; |
3c7b4e6b CM |
534 | object->pointer = ptr; |
535 | object->size = size; | |
536 | object->min_count = min_count; | |
04609ccc | 537 | object->count = 0; /* white color initially */ |
3c7b4e6b | 538 | object->jiffies = jiffies; |
04609ccc | 539 | object->checksum = 0; |
3c7b4e6b CM |
540 | |
541 | /* task information */ | |
542 | if (in_irq()) { | |
543 | object->pid = 0; | |
544 | strncpy(object->comm, "hardirq", sizeof(object->comm)); | |
545 | } else if (in_softirq()) { | |
546 | object->pid = 0; | |
547 | strncpy(object->comm, "softirq", sizeof(object->comm)); | |
548 | } else { | |
549 | object->pid = current->pid; | |
550 | /* | |
551 | * There is a small chance of a race with set_task_comm(), | |
552 | * however using get_task_comm() here may cause locking | |
553 | * dependency issues with current->alloc_lock. In the worst | |
554 | * case, the command line is not correct. | |
555 | */ | |
556 | strncpy(object->comm, current->comm, sizeof(object->comm)); | |
557 | } | |
558 | ||
559 | /* kernel backtrace */ | |
fd678967 | 560 | object->trace_len = __save_stack_trace(object->trace); |
3c7b4e6b CM |
561 | |
562 | INIT_PRIO_TREE_NODE(&object->tree_node); | |
563 | object->tree_node.start = ptr; | |
564 | object->tree_node.last = ptr + size - 1; | |
565 | ||
566 | write_lock_irqsave(&kmemleak_lock, flags); | |
0580a181 | 567 | |
3c7b4e6b CM |
568 | min_addr = min(min_addr, ptr); |
569 | max_addr = max(max_addr, ptr + size); | |
570 | node = prio_tree_insert(&object_tree_root, &object->tree_node); | |
571 | /* | |
572 | * The code calling the kernel does not yet have the pointer to the | |
573 | * memory block to be able to free it. However, we still hold the | |
574 | * kmemleak_lock here in case parts of the kernel started freeing | |
575 | * random memory blocks. | |
576 | */ | |
577 | if (node != &object->tree_node) { | |
ae281064 JP |
578 | kmemleak_stop("Cannot insert 0x%lx into the object search tree " |
579 | "(already existing)\n", ptr); | |
3c7b4e6b | 580 | object = lookup_object(ptr, 1); |
0580a181 | 581 | spin_lock(&object->lock); |
3c7b4e6b | 582 | dump_object_info(object); |
0580a181 | 583 | spin_unlock(&object->lock); |
3c7b4e6b CM |
584 | |
585 | goto out; | |
586 | } | |
587 | list_add_tail_rcu(&object->object_list, &object_list); | |
588 | out: | |
589 | write_unlock_irqrestore(&kmemleak_lock, flags); | |
fd678967 | 590 | return object; |
3c7b4e6b CM |
591 | } |
592 | ||
593 | /* | |
594 | * Remove the metadata (struct kmemleak_object) for a memory block from the | |
595 | * object_list and object_tree_root and decrement its use_count. | |
596 | */ | |
53238a60 | 597 | static void __delete_object(struct kmemleak_object *object) |
3c7b4e6b CM |
598 | { |
599 | unsigned long flags; | |
3c7b4e6b CM |
600 | |
601 | write_lock_irqsave(&kmemleak_lock, flags); | |
3c7b4e6b CM |
602 | prio_tree_remove(&object_tree_root, &object->tree_node); |
603 | list_del_rcu(&object->object_list); | |
604 | write_unlock_irqrestore(&kmemleak_lock, flags); | |
605 | ||
606 | WARN_ON(!(object->flags & OBJECT_ALLOCATED)); | |
53238a60 | 607 | WARN_ON(atomic_read(&object->use_count) < 2); |
3c7b4e6b CM |
608 | |
609 | /* | |
610 | * Locking here also ensures that the corresponding memory block | |
611 | * cannot be freed when it is being scanned. | |
612 | */ | |
613 | spin_lock_irqsave(&object->lock, flags); | |
3c7b4e6b CM |
614 | object->flags &= ~OBJECT_ALLOCATED; |
615 | spin_unlock_irqrestore(&object->lock, flags); | |
616 | put_object(object); | |
617 | } | |
618 | ||
53238a60 CM |
619 | /* |
620 | * Look up the metadata (struct kmemleak_object) corresponding to ptr and | |
621 | * delete it. | |
622 | */ | |
623 | static void delete_object_full(unsigned long ptr) | |
624 | { | |
625 | struct kmemleak_object *object; | |
626 | ||
627 | object = find_and_get_object(ptr, 0); | |
628 | if (!object) { | |
629 | #ifdef DEBUG | |
630 | kmemleak_warn("Freeing unknown object at 0x%08lx\n", | |
631 | ptr); | |
632 | #endif | |
633 | return; | |
634 | } | |
635 | __delete_object(object); | |
636 | put_object(object); | |
637 | } | |
638 | ||
639 | /* | |
640 | * Look up the metadata (struct kmemleak_object) corresponding to ptr and | |
641 | * delete it. If the memory block is partially freed, the function may create | |
642 | * additional metadata for the remaining parts of the block. | |
643 | */ | |
644 | static void delete_object_part(unsigned long ptr, size_t size) | |
645 | { | |
646 | struct kmemleak_object *object; | |
647 | unsigned long start, end; | |
648 | ||
649 | object = find_and_get_object(ptr, 1); | |
650 | if (!object) { | |
651 | #ifdef DEBUG | |
652 | kmemleak_warn("Partially freeing unknown object at 0x%08lx " | |
653 | "(size %zu)\n", ptr, size); | |
654 | #endif | |
655 | return; | |
656 | } | |
657 | __delete_object(object); | |
658 | ||
659 | /* | |
660 | * Create one or two objects that may result from the memory block | |
661 | * split. Note that partial freeing is only done by free_bootmem() and | |
662 | * this happens before kmemleak_init() is called. The path below is | |
663 | * only executed during early log recording in kmemleak_init(), so | |
664 | * GFP_KERNEL is enough. | |
665 | */ | |
666 | start = object->pointer; | |
667 | end = object->pointer + object->size; | |
668 | if (ptr > start) | |
669 | create_object(start, ptr - start, object->min_count, | |
670 | GFP_KERNEL); | |
671 | if (ptr + size < end) | |
672 | create_object(ptr + size, end - ptr - size, object->min_count, | |
673 | GFP_KERNEL); | |
674 | ||
675 | put_object(object); | |
676 | } | |
a1084c87 LR |
677 | |
678 | static void __paint_it(struct kmemleak_object *object, int color) | |
679 | { | |
680 | object->min_count = color; | |
681 | if (color == KMEMLEAK_BLACK) | |
682 | object->flags |= OBJECT_NO_SCAN; | |
683 | } | |
684 | ||
685 | static void paint_it(struct kmemleak_object *object, int color) | |
3c7b4e6b CM |
686 | { |
687 | unsigned long flags; | |
a1084c87 LR |
688 | |
689 | spin_lock_irqsave(&object->lock, flags); | |
690 | __paint_it(object, color); | |
691 | spin_unlock_irqrestore(&object->lock, flags); | |
692 | } | |
693 | ||
694 | static void paint_ptr(unsigned long ptr, int color) | |
695 | { | |
3c7b4e6b CM |
696 | struct kmemleak_object *object; |
697 | ||
698 | object = find_and_get_object(ptr, 0); | |
699 | if (!object) { | |
a1084c87 LR |
700 | kmemleak_warn("Trying to color unknown object " |
701 | "at 0x%08lx as %s\n", ptr, | |
702 | (color == KMEMLEAK_GREY) ? "Grey" : | |
703 | (color == KMEMLEAK_BLACK) ? "Black" : "Unknown"); | |
3c7b4e6b CM |
704 | return; |
705 | } | |
a1084c87 | 706 | paint_it(object, color); |
3c7b4e6b CM |
707 | put_object(object); |
708 | } | |
709 | ||
a1084c87 | 710 | /* |
145b64b9 | 711 | * Mark an object permanently as gray-colored so that it can no longer be |
a1084c87 LR |
712 | * reported as a leak. This is used in general to mark a false positive. |
713 | */ | |
714 | static void make_gray_object(unsigned long ptr) | |
715 | { | |
716 | paint_ptr(ptr, KMEMLEAK_GREY); | |
717 | } | |
718 | ||
3c7b4e6b CM |
719 | /* |
720 | * Mark the object as black-colored so that it is ignored from scans and | |
721 | * reporting. | |
722 | */ | |
723 | static void make_black_object(unsigned long ptr) | |
724 | { | |
a1084c87 | 725 | paint_ptr(ptr, KMEMLEAK_BLACK); |
3c7b4e6b CM |
726 | } |
727 | ||
728 | /* | |
729 | * Add a scanning area to the object. If at least one such area is added, | |
730 | * kmemleak will only scan these ranges rather than the whole memory block. | |
731 | */ | |
c017b4be | 732 | static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp) |
3c7b4e6b CM |
733 | { |
734 | unsigned long flags; | |
735 | struct kmemleak_object *object; | |
736 | struct kmemleak_scan_area *area; | |
737 | ||
c017b4be | 738 | object = find_and_get_object(ptr, 1); |
3c7b4e6b | 739 | if (!object) { |
ae281064 JP |
740 | kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n", |
741 | ptr); | |
3c7b4e6b CM |
742 | return; |
743 | } | |
744 | ||
6ae4bd1f | 745 | area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp)); |
3c7b4e6b | 746 | if (!area) { |
6ae4bd1f | 747 | pr_warning("Cannot allocate a scan area\n"); |
3c7b4e6b CM |
748 | goto out; |
749 | } | |
750 | ||
751 | spin_lock_irqsave(&object->lock, flags); | |
c017b4be | 752 | if (ptr + size > object->pointer + object->size) { |
ae281064 | 753 | kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr); |
3c7b4e6b CM |
754 | dump_object_info(object); |
755 | kmem_cache_free(scan_area_cache, area); | |
756 | goto out_unlock; | |
757 | } | |
758 | ||
759 | INIT_HLIST_NODE(&area->node); | |
c017b4be CM |
760 | area->start = ptr; |
761 | area->size = size; | |
3c7b4e6b CM |
762 | |
763 | hlist_add_head(&area->node, &object->area_list); | |
764 | out_unlock: | |
765 | spin_unlock_irqrestore(&object->lock, flags); | |
766 | out: | |
767 | put_object(object); | |
768 | } | |
769 | ||
770 | /* | |
771 | * Set the OBJECT_NO_SCAN flag for the object corresponding to the give | |
772 | * pointer. Such object will not be scanned by kmemleak but references to it | |
773 | * are searched. | |
774 | */ | |
775 | static void object_no_scan(unsigned long ptr) | |
776 | { | |
777 | unsigned long flags; | |
778 | struct kmemleak_object *object; | |
779 | ||
780 | object = find_and_get_object(ptr, 0); | |
781 | if (!object) { | |
ae281064 | 782 | kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr); |
3c7b4e6b CM |
783 | return; |
784 | } | |
785 | ||
786 | spin_lock_irqsave(&object->lock, flags); | |
787 | object->flags |= OBJECT_NO_SCAN; | |
788 | spin_unlock_irqrestore(&object->lock, flags); | |
789 | put_object(object); | |
790 | } | |
791 | ||
792 | /* | |
793 | * Log an early kmemleak_* call to the early_log buffer. These calls will be | |
794 | * processed later once kmemleak is fully initialized. | |
795 | */ | |
a6186d89 | 796 | static void __init log_early(int op_type, const void *ptr, size_t size, |
c017b4be | 797 | int min_count) |
3c7b4e6b CM |
798 | { |
799 | unsigned long flags; | |
800 | struct early_log *log; | |
801 | ||
b6693005 CM |
802 | if (atomic_read(&kmemleak_error)) { |
803 | /* kmemleak stopped recording, just count the requests */ | |
804 | crt_early_log++; | |
805 | return; | |
806 | } | |
807 | ||
3c7b4e6b | 808 | if (crt_early_log >= ARRAY_SIZE(early_log)) { |
a9d9058a | 809 | kmemleak_disable(); |
3c7b4e6b CM |
810 | return; |
811 | } | |
812 | ||
813 | /* | |
814 | * There is no need for locking since the kernel is still in UP mode | |
815 | * at this stage. Disabling the IRQs is enough. | |
816 | */ | |
817 | local_irq_save(flags); | |
818 | log = &early_log[crt_early_log]; | |
819 | log->op_type = op_type; | |
820 | log->ptr = ptr; | |
821 | log->size = size; | |
822 | log->min_count = min_count; | |
5f79020c | 823 | log->trace_len = __save_stack_trace(log->trace); |
3c7b4e6b CM |
824 | crt_early_log++; |
825 | local_irq_restore(flags); | |
826 | } | |
827 | ||
fd678967 CM |
828 | /* |
829 | * Log an early allocated block and populate the stack trace. | |
830 | */ | |
831 | static void early_alloc(struct early_log *log) | |
832 | { | |
833 | struct kmemleak_object *object; | |
834 | unsigned long flags; | |
835 | int i; | |
836 | ||
837 | if (!atomic_read(&kmemleak_enabled) || !log->ptr || IS_ERR(log->ptr)) | |
838 | return; | |
839 | ||
840 | /* | |
841 | * RCU locking needed to ensure object is not freed via put_object(). | |
842 | */ | |
843 | rcu_read_lock(); | |
844 | object = create_object((unsigned long)log->ptr, log->size, | |
c1bcd6b3 | 845 | log->min_count, GFP_ATOMIC); |
0d5d1aad CM |
846 | if (!object) |
847 | goto out; | |
fd678967 CM |
848 | spin_lock_irqsave(&object->lock, flags); |
849 | for (i = 0; i < log->trace_len; i++) | |
850 | object->trace[i] = log->trace[i]; | |
851 | object->trace_len = log->trace_len; | |
852 | spin_unlock_irqrestore(&object->lock, flags); | |
0d5d1aad | 853 | out: |
fd678967 CM |
854 | rcu_read_unlock(); |
855 | } | |
856 | ||
f528f0b8 CM |
857 | /* |
858 | * Log an early allocated block and populate the stack trace. | |
859 | */ | |
860 | static void early_alloc_percpu(struct early_log *log) | |
861 | { | |
862 | unsigned int cpu; | |
863 | const void __percpu *ptr = log->ptr; | |
864 | ||
865 | for_each_possible_cpu(cpu) { | |
866 | log->ptr = per_cpu_ptr(ptr, cpu); | |
867 | early_alloc(log); | |
868 | } | |
869 | } | |
870 | ||
a2b6bf63 CM |
871 | /** |
872 | * kmemleak_alloc - register a newly allocated object | |
873 | * @ptr: pointer to beginning of the object | |
874 | * @size: size of the object | |
875 | * @min_count: minimum number of references to this object. If during memory | |
876 | * scanning a number of references less than @min_count is found, | |
877 | * the object is reported as a memory leak. If @min_count is 0, | |
878 | * the object is never reported as a leak. If @min_count is -1, | |
879 | * the object is ignored (not scanned and not reported as a leak) | |
880 | * @gfp: kmalloc() flags used for kmemleak internal memory allocations | |
881 | * | |
882 | * This function is called from the kernel allocators when a new object | |
883 | * (memory block) is allocated (kmem_cache_alloc, kmalloc, vmalloc etc.). | |
3c7b4e6b | 884 | */ |
a6186d89 CM |
885 | void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count, |
886 | gfp_t gfp) | |
3c7b4e6b CM |
887 | { |
888 | pr_debug("%s(0x%p, %zu, %d)\n", __func__, ptr, size, min_count); | |
889 | ||
890 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
891 | create_object((unsigned long)ptr, size, min_count, gfp); | |
892 | else if (atomic_read(&kmemleak_early_log)) | |
c017b4be | 893 | log_early(KMEMLEAK_ALLOC, ptr, size, min_count); |
3c7b4e6b CM |
894 | } |
895 | EXPORT_SYMBOL_GPL(kmemleak_alloc); | |
896 | ||
f528f0b8 CM |
897 | /** |
898 | * kmemleak_alloc_percpu - register a newly allocated __percpu object | |
899 | * @ptr: __percpu pointer to beginning of the object | |
900 | * @size: size of the object | |
901 | * | |
902 | * This function is called from the kernel percpu allocator when a new object | |
903 | * (memory block) is allocated (alloc_percpu). It assumes GFP_KERNEL | |
904 | * allocation. | |
905 | */ | |
906 | void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size) | |
907 | { | |
908 | unsigned int cpu; | |
909 | ||
910 | pr_debug("%s(0x%p, %zu)\n", __func__, ptr, size); | |
911 | ||
912 | /* | |
913 | * Percpu allocations are only scanned and not reported as leaks | |
914 | * (min_count is set to 0). | |
915 | */ | |
916 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
917 | for_each_possible_cpu(cpu) | |
918 | create_object((unsigned long)per_cpu_ptr(ptr, cpu), | |
919 | size, 0, GFP_KERNEL); | |
920 | else if (atomic_read(&kmemleak_early_log)) | |
921 | log_early(KMEMLEAK_ALLOC_PERCPU, ptr, size, 0); | |
922 | } | |
923 | EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu); | |
924 | ||
a2b6bf63 CM |
925 | /** |
926 | * kmemleak_free - unregister a previously registered object | |
927 | * @ptr: pointer to beginning of the object | |
928 | * | |
929 | * This function is called from the kernel allocators when an object (memory | |
930 | * block) is freed (kmem_cache_free, kfree, vfree etc.). | |
3c7b4e6b | 931 | */ |
a6186d89 | 932 | void __ref kmemleak_free(const void *ptr) |
3c7b4e6b CM |
933 | { |
934 | pr_debug("%s(0x%p)\n", __func__, ptr); | |
935 | ||
936 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
53238a60 | 937 | delete_object_full((unsigned long)ptr); |
3c7b4e6b | 938 | else if (atomic_read(&kmemleak_early_log)) |
c017b4be | 939 | log_early(KMEMLEAK_FREE, ptr, 0, 0); |
3c7b4e6b CM |
940 | } |
941 | EXPORT_SYMBOL_GPL(kmemleak_free); | |
942 | ||
a2b6bf63 CM |
943 | /** |
944 | * kmemleak_free_part - partially unregister a previously registered object | |
945 | * @ptr: pointer to the beginning or inside the object. This also | |
946 | * represents the start of the range to be freed | |
947 | * @size: size to be unregistered | |
948 | * | |
949 | * This function is called when only a part of a memory block is freed | |
950 | * (usually from the bootmem allocator). | |
53238a60 | 951 | */ |
a6186d89 | 952 | void __ref kmemleak_free_part(const void *ptr, size_t size) |
53238a60 CM |
953 | { |
954 | pr_debug("%s(0x%p)\n", __func__, ptr); | |
955 | ||
956 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
957 | delete_object_part((unsigned long)ptr, size); | |
958 | else if (atomic_read(&kmemleak_early_log)) | |
c017b4be | 959 | log_early(KMEMLEAK_FREE_PART, ptr, size, 0); |
53238a60 CM |
960 | } |
961 | EXPORT_SYMBOL_GPL(kmemleak_free_part); | |
962 | ||
f528f0b8 CM |
963 | /** |
964 | * kmemleak_free_percpu - unregister a previously registered __percpu object | |
965 | * @ptr: __percpu pointer to beginning of the object | |
966 | * | |
967 | * This function is called from the kernel percpu allocator when an object | |
968 | * (memory block) is freed (free_percpu). | |
969 | */ | |
970 | void __ref kmemleak_free_percpu(const void __percpu *ptr) | |
971 | { | |
972 | unsigned int cpu; | |
973 | ||
974 | pr_debug("%s(0x%p)\n", __func__, ptr); | |
975 | ||
976 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
977 | for_each_possible_cpu(cpu) | |
978 | delete_object_full((unsigned long)per_cpu_ptr(ptr, | |
979 | cpu)); | |
980 | else if (atomic_read(&kmemleak_early_log)) | |
981 | log_early(KMEMLEAK_FREE_PERCPU, ptr, 0, 0); | |
982 | } | |
983 | EXPORT_SYMBOL_GPL(kmemleak_free_percpu); | |
984 | ||
a2b6bf63 CM |
985 | /** |
986 | * kmemleak_not_leak - mark an allocated object as false positive | |
987 | * @ptr: pointer to beginning of the object | |
988 | * | |
989 | * Calling this function on an object will cause the memory block to no longer | |
990 | * be reported as leak and always be scanned. | |
3c7b4e6b | 991 | */ |
a6186d89 | 992 | void __ref kmemleak_not_leak(const void *ptr) |
3c7b4e6b CM |
993 | { |
994 | pr_debug("%s(0x%p)\n", __func__, ptr); | |
995 | ||
996 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
997 | make_gray_object((unsigned long)ptr); | |
998 | else if (atomic_read(&kmemleak_early_log)) | |
c017b4be | 999 | log_early(KMEMLEAK_NOT_LEAK, ptr, 0, 0); |
3c7b4e6b CM |
1000 | } |
1001 | EXPORT_SYMBOL(kmemleak_not_leak); | |
1002 | ||
a2b6bf63 CM |
1003 | /** |
1004 | * kmemleak_ignore - ignore an allocated object | |
1005 | * @ptr: pointer to beginning of the object | |
1006 | * | |
1007 | * Calling this function on an object will cause the memory block to be | |
1008 | * ignored (not scanned and not reported as a leak). This is usually done when | |
1009 | * it is known that the corresponding block is not a leak and does not contain | |
1010 | * any references to other allocated memory blocks. | |
3c7b4e6b | 1011 | */ |
a6186d89 | 1012 | void __ref kmemleak_ignore(const void *ptr) |
3c7b4e6b CM |
1013 | { |
1014 | pr_debug("%s(0x%p)\n", __func__, ptr); | |
1015 | ||
1016 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
1017 | make_black_object((unsigned long)ptr); | |
1018 | else if (atomic_read(&kmemleak_early_log)) | |
c017b4be | 1019 | log_early(KMEMLEAK_IGNORE, ptr, 0, 0); |
3c7b4e6b CM |
1020 | } |
1021 | EXPORT_SYMBOL(kmemleak_ignore); | |
1022 | ||
a2b6bf63 CM |
1023 | /** |
1024 | * kmemleak_scan_area - limit the range to be scanned in an allocated object | |
1025 | * @ptr: pointer to beginning or inside the object. This also | |
1026 | * represents the start of the scan area | |
1027 | * @size: size of the scan area | |
1028 | * @gfp: kmalloc() flags used for kmemleak internal memory allocations | |
1029 | * | |
1030 | * This function is used when it is known that only certain parts of an object | |
1031 | * contain references to other objects. Kmemleak will only scan these areas | |
1032 | * reducing the number false negatives. | |
3c7b4e6b | 1033 | */ |
c017b4be | 1034 | void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp) |
3c7b4e6b CM |
1035 | { |
1036 | pr_debug("%s(0x%p)\n", __func__, ptr); | |
1037 | ||
1038 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
c017b4be | 1039 | add_scan_area((unsigned long)ptr, size, gfp); |
3c7b4e6b | 1040 | else if (atomic_read(&kmemleak_early_log)) |
c017b4be | 1041 | log_early(KMEMLEAK_SCAN_AREA, ptr, size, 0); |
3c7b4e6b CM |
1042 | } |
1043 | EXPORT_SYMBOL(kmemleak_scan_area); | |
1044 | ||
a2b6bf63 CM |
1045 | /** |
1046 | * kmemleak_no_scan - do not scan an allocated object | |
1047 | * @ptr: pointer to beginning of the object | |
1048 | * | |
1049 | * This function notifies kmemleak not to scan the given memory block. Useful | |
1050 | * in situations where it is known that the given object does not contain any | |
1051 | * references to other objects. Kmemleak will not scan such objects reducing | |
1052 | * the number of false negatives. | |
3c7b4e6b | 1053 | */ |
a6186d89 | 1054 | void __ref kmemleak_no_scan(const void *ptr) |
3c7b4e6b CM |
1055 | { |
1056 | pr_debug("%s(0x%p)\n", __func__, ptr); | |
1057 | ||
1058 | if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) | |
1059 | object_no_scan((unsigned long)ptr); | |
1060 | else if (atomic_read(&kmemleak_early_log)) | |
c017b4be | 1061 | log_early(KMEMLEAK_NO_SCAN, ptr, 0, 0); |
3c7b4e6b CM |
1062 | } |
1063 | EXPORT_SYMBOL(kmemleak_no_scan); | |
1064 | ||
04609ccc CM |
1065 | /* |
1066 | * Update an object's checksum and return true if it was modified. | |
1067 | */ | |
1068 | static bool update_checksum(struct kmemleak_object *object) | |
1069 | { | |
1070 | u32 old_csum = object->checksum; | |
1071 | ||
1072 | if (!kmemcheck_is_obj_initialized(object->pointer, object->size)) | |
1073 | return false; | |
1074 | ||
1075 | object->checksum = crc32(0, (void *)object->pointer, object->size); | |
1076 | return object->checksum != old_csum; | |
1077 | } | |
1078 | ||
3c7b4e6b CM |
1079 | /* |
1080 | * Memory scanning is a long process and it needs to be interruptable. This | |
25985edc | 1081 | * function checks whether such interrupt condition occurred. |
3c7b4e6b CM |
1082 | */ |
1083 | static int scan_should_stop(void) | |
1084 | { | |
1085 | if (!atomic_read(&kmemleak_enabled)) | |
1086 | return 1; | |
1087 | ||
1088 | /* | |
1089 | * This function may be called from either process or kthread context, | |
1090 | * hence the need to check for both stop conditions. | |
1091 | */ | |
1092 | if (current->mm) | |
1093 | return signal_pending(current); | |
1094 | else | |
1095 | return kthread_should_stop(); | |
1096 | ||
1097 | return 0; | |
1098 | } | |
1099 | ||
1100 | /* | |
1101 | * Scan a memory block (exclusive range) for valid pointers and add those | |
1102 | * found to the gray list. | |
1103 | */ | |
1104 | static void scan_block(void *_start, void *_end, | |
4b8a9674 | 1105 | struct kmemleak_object *scanned, int allow_resched) |
3c7b4e6b CM |
1106 | { |
1107 | unsigned long *ptr; | |
1108 | unsigned long *start = PTR_ALIGN(_start, BYTES_PER_POINTER); | |
1109 | unsigned long *end = _end - (BYTES_PER_POINTER - 1); | |
1110 | ||
1111 | for (ptr = start; ptr < end; ptr++) { | |
3c7b4e6b | 1112 | struct kmemleak_object *object; |
8e019366 PE |
1113 | unsigned long flags; |
1114 | unsigned long pointer; | |
3c7b4e6b | 1115 | |
4b8a9674 CM |
1116 | if (allow_resched) |
1117 | cond_resched(); | |
3c7b4e6b CM |
1118 | if (scan_should_stop()) |
1119 | break; | |
1120 | ||
8e019366 PE |
1121 | /* don't scan uninitialized memory */ |
1122 | if (!kmemcheck_is_obj_initialized((unsigned long)ptr, | |
1123 | BYTES_PER_POINTER)) | |
1124 | continue; | |
1125 | ||
1126 | pointer = *ptr; | |
1127 | ||
3c7b4e6b CM |
1128 | object = find_and_get_object(pointer, 1); |
1129 | if (!object) | |
1130 | continue; | |
1131 | if (object == scanned) { | |
1132 | /* self referenced, ignore */ | |
1133 | put_object(object); | |
1134 | continue; | |
1135 | } | |
1136 | ||
1137 | /* | |
1138 | * Avoid the lockdep recursive warning on object->lock being | |
1139 | * previously acquired in scan_object(). These locks are | |
1140 | * enclosed by scan_mutex. | |
1141 | */ | |
1142 | spin_lock_irqsave_nested(&object->lock, flags, | |
1143 | SINGLE_DEPTH_NESTING); | |
1144 | if (!color_white(object)) { | |
1145 | /* non-orphan, ignored or new */ | |
1146 | spin_unlock_irqrestore(&object->lock, flags); | |
1147 | put_object(object); | |
1148 | continue; | |
1149 | } | |
1150 | ||
1151 | /* | |
1152 | * Increase the object's reference count (number of pointers | |
1153 | * to the memory block). If this count reaches the required | |
1154 | * minimum, the object's color will become gray and it will be | |
1155 | * added to the gray_list. | |
1156 | */ | |
1157 | object->count++; | |
0587da40 | 1158 | if (color_gray(object)) { |
3c7b4e6b | 1159 | list_add_tail(&object->gray_list, &gray_list); |
0587da40 CM |
1160 | spin_unlock_irqrestore(&object->lock, flags); |
1161 | continue; | |
1162 | } | |
1163 | ||
3c7b4e6b | 1164 | spin_unlock_irqrestore(&object->lock, flags); |
0587da40 | 1165 | put_object(object); |
3c7b4e6b CM |
1166 | } |
1167 | } | |
1168 | ||
1169 | /* | |
1170 | * Scan a memory block corresponding to a kmemleak_object. A condition is | |
1171 | * that object->use_count >= 1. | |
1172 | */ | |
1173 | static void scan_object(struct kmemleak_object *object) | |
1174 | { | |
1175 | struct kmemleak_scan_area *area; | |
1176 | struct hlist_node *elem; | |
1177 | unsigned long flags; | |
1178 | ||
1179 | /* | |
21ae2956 UKK |
1180 | * Once the object->lock is acquired, the corresponding memory block |
1181 | * cannot be freed (the same lock is acquired in delete_object). | |
3c7b4e6b CM |
1182 | */ |
1183 | spin_lock_irqsave(&object->lock, flags); | |
1184 | if (object->flags & OBJECT_NO_SCAN) | |
1185 | goto out; | |
1186 | if (!(object->flags & OBJECT_ALLOCATED)) | |
1187 | /* already freed object */ | |
1188 | goto out; | |
af98603d CM |
1189 | if (hlist_empty(&object->area_list)) { |
1190 | void *start = (void *)object->pointer; | |
1191 | void *end = (void *)(object->pointer + object->size); | |
1192 | ||
1193 | while (start < end && (object->flags & OBJECT_ALLOCATED) && | |
1194 | !(object->flags & OBJECT_NO_SCAN)) { | |
1195 | scan_block(start, min(start + MAX_SCAN_SIZE, end), | |
1196 | object, 0); | |
1197 | start += MAX_SCAN_SIZE; | |
1198 | ||
1199 | spin_unlock_irqrestore(&object->lock, flags); | |
1200 | cond_resched(); | |
1201 | spin_lock_irqsave(&object->lock, flags); | |
1202 | } | |
1203 | } else | |
3c7b4e6b | 1204 | hlist_for_each_entry(area, elem, &object->area_list, node) |
c017b4be CM |
1205 | scan_block((void *)area->start, |
1206 | (void *)(area->start + area->size), | |
1207 | object, 0); | |
3c7b4e6b CM |
1208 | out: |
1209 | spin_unlock_irqrestore(&object->lock, flags); | |
1210 | } | |
1211 | ||
04609ccc CM |
1212 | /* |
1213 | * Scan the objects already referenced (gray objects). More objects will be | |
1214 | * referenced and, if there are no memory leaks, all the objects are scanned. | |
1215 | */ | |
1216 | static void scan_gray_list(void) | |
1217 | { | |
1218 | struct kmemleak_object *object, *tmp; | |
1219 | ||
1220 | /* | |
1221 | * The list traversal is safe for both tail additions and removals | |
1222 | * from inside the loop. The kmemleak objects cannot be freed from | |
1223 | * outside the loop because their use_count was incremented. | |
1224 | */ | |
1225 | object = list_entry(gray_list.next, typeof(*object), gray_list); | |
1226 | while (&object->gray_list != &gray_list) { | |
1227 | cond_resched(); | |
1228 | ||
1229 | /* may add new objects to the list */ | |
1230 | if (!scan_should_stop()) | |
1231 | scan_object(object); | |
1232 | ||
1233 | tmp = list_entry(object->gray_list.next, typeof(*object), | |
1234 | gray_list); | |
1235 | ||
1236 | /* remove the object from the list and release it */ | |
1237 | list_del(&object->gray_list); | |
1238 | put_object(object); | |
1239 | ||
1240 | object = tmp; | |
1241 | } | |
1242 | WARN_ON(!list_empty(&gray_list)); | |
1243 | } | |
1244 | ||
3c7b4e6b CM |
1245 | /* |
1246 | * Scan data sections and all the referenced memory blocks allocated via the | |
1247 | * kernel's standard allocators. This function must be called with the | |
1248 | * scan_mutex held. | |
1249 | */ | |
1250 | static void kmemleak_scan(void) | |
1251 | { | |
1252 | unsigned long flags; | |
04609ccc | 1253 | struct kmemleak_object *object; |
3c7b4e6b | 1254 | int i; |
4698c1f2 | 1255 | int new_leaks = 0; |
3c7b4e6b | 1256 | |
acf4968e CM |
1257 | jiffies_last_scan = jiffies; |
1258 | ||
3c7b4e6b CM |
1259 | /* prepare the kmemleak_object's */ |
1260 | rcu_read_lock(); | |
1261 | list_for_each_entry_rcu(object, &object_list, object_list) { | |
1262 | spin_lock_irqsave(&object->lock, flags); | |
1263 | #ifdef DEBUG | |
1264 | /* | |
1265 | * With a few exceptions there should be a maximum of | |
1266 | * 1 reference to any object at this point. | |
1267 | */ | |
1268 | if (atomic_read(&object->use_count) > 1) { | |
ae281064 | 1269 | pr_debug("object->use_count = %d\n", |
3c7b4e6b CM |
1270 | atomic_read(&object->use_count)); |
1271 | dump_object_info(object); | |
1272 | } | |
1273 | #endif | |
1274 | /* reset the reference count (whiten the object) */ | |
1275 | object->count = 0; | |
1276 | if (color_gray(object) && get_object(object)) | |
1277 | list_add_tail(&object->gray_list, &gray_list); | |
1278 | ||
1279 | spin_unlock_irqrestore(&object->lock, flags); | |
1280 | } | |
1281 | rcu_read_unlock(); | |
1282 | ||
1283 | /* data/bss scanning */ | |
4b8a9674 CM |
1284 | scan_block(_sdata, _edata, NULL, 1); |
1285 | scan_block(__bss_start, __bss_stop, NULL, 1); | |
3c7b4e6b CM |
1286 | |
1287 | #ifdef CONFIG_SMP | |
1288 | /* per-cpu sections scanning */ | |
1289 | for_each_possible_cpu(i) | |
1290 | scan_block(__per_cpu_start + per_cpu_offset(i), | |
4b8a9674 | 1291 | __per_cpu_end + per_cpu_offset(i), NULL, 1); |
3c7b4e6b CM |
1292 | #endif |
1293 | ||
1294 | /* | |
1295 | * Struct page scanning for each node. The code below is not yet safe | |
1296 | * with MEMORY_HOTPLUG. | |
1297 | */ | |
1298 | for_each_online_node(i) { | |
1299 | pg_data_t *pgdat = NODE_DATA(i); | |
1300 | unsigned long start_pfn = pgdat->node_start_pfn; | |
1301 | unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages; | |
1302 | unsigned long pfn; | |
1303 | ||
1304 | for (pfn = start_pfn; pfn < end_pfn; pfn++) { | |
1305 | struct page *page; | |
1306 | ||
1307 | if (!pfn_valid(pfn)) | |
1308 | continue; | |
1309 | page = pfn_to_page(pfn); | |
1310 | /* only scan if page is in use */ | |
1311 | if (page_count(page) == 0) | |
1312 | continue; | |
4b8a9674 | 1313 | scan_block(page, page + 1, NULL, 1); |
3c7b4e6b CM |
1314 | } |
1315 | } | |
1316 | ||
1317 | /* | |
43ed5d6e | 1318 | * Scanning the task stacks (may introduce false negatives). |
3c7b4e6b CM |
1319 | */ |
1320 | if (kmemleak_stack_scan) { | |
43ed5d6e CM |
1321 | struct task_struct *p, *g; |
1322 | ||
3c7b4e6b | 1323 | read_lock(&tasklist_lock); |
43ed5d6e CM |
1324 | do_each_thread(g, p) { |
1325 | scan_block(task_stack_page(p), task_stack_page(p) + | |
1326 | THREAD_SIZE, NULL, 0); | |
1327 | } while_each_thread(g, p); | |
3c7b4e6b CM |
1328 | read_unlock(&tasklist_lock); |
1329 | } | |
1330 | ||
1331 | /* | |
1332 | * Scan the objects already referenced from the sections scanned | |
04609ccc | 1333 | * above. |
3c7b4e6b | 1334 | */ |
04609ccc | 1335 | scan_gray_list(); |
2587362e CM |
1336 | |
1337 | /* | |
04609ccc CM |
1338 | * Check for new or unreferenced objects modified since the previous |
1339 | * scan and color them gray until the next scan. | |
2587362e CM |
1340 | */ |
1341 | rcu_read_lock(); | |
1342 | list_for_each_entry_rcu(object, &object_list, object_list) { | |
1343 | spin_lock_irqsave(&object->lock, flags); | |
04609ccc CM |
1344 | if (color_white(object) && (object->flags & OBJECT_ALLOCATED) |
1345 | && update_checksum(object) && get_object(object)) { | |
1346 | /* color it gray temporarily */ | |
1347 | object->count = object->min_count; | |
2587362e CM |
1348 | list_add_tail(&object->gray_list, &gray_list); |
1349 | } | |
1350 | spin_unlock_irqrestore(&object->lock, flags); | |
1351 | } | |
1352 | rcu_read_unlock(); | |
1353 | ||
04609ccc CM |
1354 | /* |
1355 | * Re-scan the gray list for modified unreferenced objects. | |
1356 | */ | |
1357 | scan_gray_list(); | |
4698c1f2 | 1358 | |
17bb9e0d | 1359 | /* |
04609ccc | 1360 | * If scanning was stopped do not report any new unreferenced objects. |
17bb9e0d | 1361 | */ |
04609ccc | 1362 | if (scan_should_stop()) |
17bb9e0d CM |
1363 | return; |
1364 | ||
4698c1f2 CM |
1365 | /* |
1366 | * Scanning result reporting. | |
1367 | */ | |
1368 | rcu_read_lock(); | |
1369 | list_for_each_entry_rcu(object, &object_list, object_list) { | |
1370 | spin_lock_irqsave(&object->lock, flags); | |
1371 | if (unreferenced_object(object) && | |
1372 | !(object->flags & OBJECT_REPORTED)) { | |
1373 | object->flags |= OBJECT_REPORTED; | |
1374 | new_leaks++; | |
1375 | } | |
1376 | spin_unlock_irqrestore(&object->lock, flags); | |
1377 | } | |
1378 | rcu_read_unlock(); | |
1379 | ||
1380 | if (new_leaks) | |
1381 | pr_info("%d new suspected memory leaks (see " | |
1382 | "/sys/kernel/debug/kmemleak)\n", new_leaks); | |
1383 | ||
3c7b4e6b CM |
1384 | } |
1385 | ||
1386 | /* | |
1387 | * Thread function performing automatic memory scanning. Unreferenced objects | |
1388 | * at the end of a memory scan are reported but only the first time. | |
1389 | */ | |
1390 | static int kmemleak_scan_thread(void *arg) | |
1391 | { | |
1392 | static int first_run = 1; | |
1393 | ||
ae281064 | 1394 | pr_info("Automatic memory scanning thread started\n"); |
bf2a76b3 | 1395 | set_user_nice(current, 10); |
3c7b4e6b CM |
1396 | |
1397 | /* | |
1398 | * Wait before the first scan to allow the system to fully initialize. | |
1399 | */ | |
1400 | if (first_run) { | |
1401 | first_run = 0; | |
1402 | ssleep(SECS_FIRST_SCAN); | |
1403 | } | |
1404 | ||
1405 | while (!kthread_should_stop()) { | |
3c7b4e6b CM |
1406 | signed long timeout = jiffies_scan_wait; |
1407 | ||
1408 | mutex_lock(&scan_mutex); | |
3c7b4e6b | 1409 | kmemleak_scan(); |
3c7b4e6b | 1410 | mutex_unlock(&scan_mutex); |
4698c1f2 | 1411 | |
3c7b4e6b CM |
1412 | /* wait before the next scan */ |
1413 | while (timeout && !kthread_should_stop()) | |
1414 | timeout = schedule_timeout_interruptible(timeout); | |
1415 | } | |
1416 | ||
ae281064 | 1417 | pr_info("Automatic memory scanning thread ended\n"); |
3c7b4e6b CM |
1418 | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | /* | |
1423 | * Start the automatic memory scanning thread. This function must be called | |
4698c1f2 | 1424 | * with the scan_mutex held. |
3c7b4e6b | 1425 | */ |
7eb0d5e5 | 1426 | static void start_scan_thread(void) |
3c7b4e6b CM |
1427 | { |
1428 | if (scan_thread) | |
1429 | return; | |
1430 | scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak"); | |
1431 | if (IS_ERR(scan_thread)) { | |
ae281064 | 1432 | pr_warning("Failed to create the scan thread\n"); |
3c7b4e6b CM |
1433 | scan_thread = NULL; |
1434 | } | |
1435 | } | |
1436 | ||
1437 | /* | |
1438 | * Stop the automatic memory scanning thread. This function must be called | |
4698c1f2 | 1439 | * with the scan_mutex held. |
3c7b4e6b | 1440 | */ |
7eb0d5e5 | 1441 | static void stop_scan_thread(void) |
3c7b4e6b CM |
1442 | { |
1443 | if (scan_thread) { | |
1444 | kthread_stop(scan_thread); | |
1445 | scan_thread = NULL; | |
1446 | } | |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Iterate over the object_list and return the first valid object at or after | |
1451 | * the required position with its use_count incremented. The function triggers | |
1452 | * a memory scanning when the pos argument points to the first position. | |
1453 | */ | |
1454 | static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos) | |
1455 | { | |
1456 | struct kmemleak_object *object; | |
1457 | loff_t n = *pos; | |
b87324d0 CM |
1458 | int err; |
1459 | ||
1460 | err = mutex_lock_interruptible(&scan_mutex); | |
1461 | if (err < 0) | |
1462 | return ERR_PTR(err); | |
3c7b4e6b | 1463 | |
3c7b4e6b CM |
1464 | rcu_read_lock(); |
1465 | list_for_each_entry_rcu(object, &object_list, object_list) { | |
1466 | if (n-- > 0) | |
1467 | continue; | |
1468 | if (get_object(object)) | |
1469 | goto out; | |
1470 | } | |
1471 | object = NULL; | |
1472 | out: | |
3c7b4e6b CM |
1473 | return object; |
1474 | } | |
1475 | ||
1476 | /* | |
1477 | * Return the next object in the object_list. The function decrements the | |
1478 | * use_count of the previous object and increases that of the next one. | |
1479 | */ | |
1480 | static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
1481 | { | |
1482 | struct kmemleak_object *prev_obj = v; | |
1483 | struct kmemleak_object *next_obj = NULL; | |
1484 | struct list_head *n = &prev_obj->object_list; | |
1485 | ||
1486 | ++(*pos); | |
3c7b4e6b | 1487 | |
3c7b4e6b | 1488 | list_for_each_continue_rcu(n, &object_list) { |
52c3ce4e CM |
1489 | struct kmemleak_object *obj = |
1490 | list_entry(n, struct kmemleak_object, object_list); | |
1491 | if (get_object(obj)) { | |
1492 | next_obj = obj; | |
3c7b4e6b | 1493 | break; |
52c3ce4e | 1494 | } |
3c7b4e6b | 1495 | } |
288c857d | 1496 | |
3c7b4e6b CM |
1497 | put_object(prev_obj); |
1498 | return next_obj; | |
1499 | } | |
1500 | ||
1501 | /* | |
1502 | * Decrement the use_count of the last object required, if any. | |
1503 | */ | |
1504 | static void kmemleak_seq_stop(struct seq_file *seq, void *v) | |
1505 | { | |
b87324d0 CM |
1506 | if (!IS_ERR(v)) { |
1507 | /* | |
1508 | * kmemleak_seq_start may return ERR_PTR if the scan_mutex | |
1509 | * waiting was interrupted, so only release it if !IS_ERR. | |
1510 | */ | |
f5886c7f | 1511 | rcu_read_unlock(); |
b87324d0 CM |
1512 | mutex_unlock(&scan_mutex); |
1513 | if (v) | |
1514 | put_object(v); | |
1515 | } | |
3c7b4e6b CM |
1516 | } |
1517 | ||
1518 | /* | |
1519 | * Print the information for an unreferenced object to the seq file. | |
1520 | */ | |
1521 | static int kmemleak_seq_show(struct seq_file *seq, void *v) | |
1522 | { | |
1523 | struct kmemleak_object *object = v; | |
1524 | unsigned long flags; | |
1525 | ||
1526 | spin_lock_irqsave(&object->lock, flags); | |
288c857d | 1527 | if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object)) |
17bb9e0d | 1528 | print_unreferenced(seq, object); |
3c7b4e6b CM |
1529 | spin_unlock_irqrestore(&object->lock, flags); |
1530 | return 0; | |
1531 | } | |
1532 | ||
1533 | static const struct seq_operations kmemleak_seq_ops = { | |
1534 | .start = kmemleak_seq_start, | |
1535 | .next = kmemleak_seq_next, | |
1536 | .stop = kmemleak_seq_stop, | |
1537 | .show = kmemleak_seq_show, | |
1538 | }; | |
1539 | ||
1540 | static int kmemleak_open(struct inode *inode, struct file *file) | |
1541 | { | |
b87324d0 | 1542 | return seq_open(file, &kmemleak_seq_ops); |
3c7b4e6b CM |
1543 | } |
1544 | ||
1545 | static int kmemleak_release(struct inode *inode, struct file *file) | |
1546 | { | |
b87324d0 | 1547 | return seq_release(inode, file); |
3c7b4e6b CM |
1548 | } |
1549 | ||
189d84ed CM |
1550 | static int dump_str_object_info(const char *str) |
1551 | { | |
1552 | unsigned long flags; | |
1553 | struct kmemleak_object *object; | |
1554 | unsigned long addr; | |
1555 | ||
1556 | addr= simple_strtoul(str, NULL, 0); | |
1557 | object = find_and_get_object(addr, 0); | |
1558 | if (!object) { | |
1559 | pr_info("Unknown object at 0x%08lx\n", addr); | |
1560 | return -EINVAL; | |
1561 | } | |
1562 | ||
1563 | spin_lock_irqsave(&object->lock, flags); | |
1564 | dump_object_info(object); | |
1565 | spin_unlock_irqrestore(&object->lock, flags); | |
1566 | ||
1567 | put_object(object); | |
1568 | return 0; | |
1569 | } | |
1570 | ||
30b37101 LR |
1571 | /* |
1572 | * We use grey instead of black to ensure we can do future scans on the same | |
1573 | * objects. If we did not do future scans these black objects could | |
1574 | * potentially contain references to newly allocated objects in the future and | |
1575 | * we'd end up with false positives. | |
1576 | */ | |
1577 | static void kmemleak_clear(void) | |
1578 | { | |
1579 | struct kmemleak_object *object; | |
1580 | unsigned long flags; | |
1581 | ||
1582 | rcu_read_lock(); | |
1583 | list_for_each_entry_rcu(object, &object_list, object_list) { | |
1584 | spin_lock_irqsave(&object->lock, flags); | |
1585 | if ((object->flags & OBJECT_REPORTED) && | |
1586 | unreferenced_object(object)) | |
a1084c87 | 1587 | __paint_it(object, KMEMLEAK_GREY); |
30b37101 LR |
1588 | spin_unlock_irqrestore(&object->lock, flags); |
1589 | } | |
1590 | rcu_read_unlock(); | |
1591 | } | |
1592 | ||
3c7b4e6b CM |
1593 | /* |
1594 | * File write operation to configure kmemleak at run-time. The following | |
1595 | * commands can be written to the /sys/kernel/debug/kmemleak file: | |
1596 | * off - disable kmemleak (irreversible) | |
1597 | * stack=on - enable the task stacks scanning | |
1598 | * stack=off - disable the tasks stacks scanning | |
1599 | * scan=on - start the automatic memory scanning thread | |
1600 | * scan=off - stop the automatic memory scanning thread | |
1601 | * scan=... - set the automatic memory scanning period in seconds (0 to | |
1602 | * disable it) | |
4698c1f2 | 1603 | * scan - trigger a memory scan |
30b37101 LR |
1604 | * clear - mark all current reported unreferenced kmemleak objects as |
1605 | * grey to ignore printing them | |
189d84ed | 1606 | * dump=... - dump information about the object found at the given address |
3c7b4e6b CM |
1607 | */ |
1608 | static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, | |
1609 | size_t size, loff_t *ppos) | |
1610 | { | |
1611 | char buf[64]; | |
1612 | int buf_size; | |
b87324d0 | 1613 | int ret; |
3c7b4e6b | 1614 | |
74341703 CM |
1615 | if (!atomic_read(&kmemleak_enabled)) |
1616 | return -EBUSY; | |
1617 | ||
3c7b4e6b CM |
1618 | buf_size = min(size, (sizeof(buf) - 1)); |
1619 | if (strncpy_from_user(buf, user_buf, buf_size) < 0) | |
1620 | return -EFAULT; | |
1621 | buf[buf_size] = 0; | |
1622 | ||
b87324d0 CM |
1623 | ret = mutex_lock_interruptible(&scan_mutex); |
1624 | if (ret < 0) | |
1625 | return ret; | |
1626 | ||
3c7b4e6b CM |
1627 | if (strncmp(buf, "off", 3) == 0) |
1628 | kmemleak_disable(); | |
1629 | else if (strncmp(buf, "stack=on", 8) == 0) | |
1630 | kmemleak_stack_scan = 1; | |
1631 | else if (strncmp(buf, "stack=off", 9) == 0) | |
1632 | kmemleak_stack_scan = 0; | |
1633 | else if (strncmp(buf, "scan=on", 7) == 0) | |
1634 | start_scan_thread(); | |
1635 | else if (strncmp(buf, "scan=off", 8) == 0) | |
1636 | stop_scan_thread(); | |
1637 | else if (strncmp(buf, "scan=", 5) == 0) { | |
1638 | unsigned long secs; | |
3c7b4e6b | 1639 | |
b87324d0 CM |
1640 | ret = strict_strtoul(buf + 5, 0, &secs); |
1641 | if (ret < 0) | |
1642 | goto out; | |
3c7b4e6b CM |
1643 | stop_scan_thread(); |
1644 | if (secs) { | |
1645 | jiffies_scan_wait = msecs_to_jiffies(secs * 1000); | |
1646 | start_scan_thread(); | |
1647 | } | |
4698c1f2 CM |
1648 | } else if (strncmp(buf, "scan", 4) == 0) |
1649 | kmemleak_scan(); | |
30b37101 LR |
1650 | else if (strncmp(buf, "clear", 5) == 0) |
1651 | kmemleak_clear(); | |
189d84ed CM |
1652 | else if (strncmp(buf, "dump=", 5) == 0) |
1653 | ret = dump_str_object_info(buf + 5); | |
4698c1f2 | 1654 | else |
b87324d0 CM |
1655 | ret = -EINVAL; |
1656 | ||
1657 | out: | |
1658 | mutex_unlock(&scan_mutex); | |
1659 | if (ret < 0) | |
1660 | return ret; | |
3c7b4e6b CM |
1661 | |
1662 | /* ignore the rest of the buffer, only one command at a time */ | |
1663 | *ppos += size; | |
1664 | return size; | |
1665 | } | |
1666 | ||
1667 | static const struct file_operations kmemleak_fops = { | |
1668 | .owner = THIS_MODULE, | |
1669 | .open = kmemleak_open, | |
1670 | .read = seq_read, | |
1671 | .write = kmemleak_write, | |
1672 | .llseek = seq_lseek, | |
1673 | .release = kmemleak_release, | |
1674 | }; | |
1675 | ||
1676 | /* | |
74341703 CM |
1677 | * Stop the memory scanning thread and free the kmemleak internal objects if |
1678 | * no previous scan thread (otherwise, kmemleak may still have some useful | |
1679 | * information on memory leaks). | |
3c7b4e6b | 1680 | */ |
179a8100 | 1681 | static void kmemleak_do_cleanup(struct work_struct *work) |
3c7b4e6b CM |
1682 | { |
1683 | struct kmemleak_object *object; | |
74341703 | 1684 | bool cleanup = scan_thread == NULL; |
3c7b4e6b | 1685 | |
4698c1f2 | 1686 | mutex_lock(&scan_mutex); |
3c7b4e6b | 1687 | stop_scan_thread(); |
3c7b4e6b | 1688 | |
74341703 CM |
1689 | if (cleanup) { |
1690 | rcu_read_lock(); | |
1691 | list_for_each_entry_rcu(object, &object_list, object_list) | |
1692 | delete_object_full(object->pointer); | |
1693 | rcu_read_unlock(); | |
1694 | } | |
3c7b4e6b | 1695 | mutex_unlock(&scan_mutex); |
3c7b4e6b CM |
1696 | } |
1697 | ||
179a8100 | 1698 | static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup); |
3c7b4e6b CM |
1699 | |
1700 | /* | |
1701 | * Disable kmemleak. No memory allocation/freeing will be traced once this | |
1702 | * function is called. Disabling kmemleak is an irreversible operation. | |
1703 | */ | |
1704 | static void kmemleak_disable(void) | |
1705 | { | |
1706 | /* atomically check whether it was already invoked */ | |
1707 | if (atomic_cmpxchg(&kmemleak_error, 0, 1)) | |
1708 | return; | |
1709 | ||
1710 | /* stop any memory operation tracing */ | |
3c7b4e6b CM |
1711 | atomic_set(&kmemleak_enabled, 0); |
1712 | ||
1713 | /* check whether it is too early for a kernel thread */ | |
1714 | if (atomic_read(&kmemleak_initialized)) | |
179a8100 | 1715 | schedule_work(&cleanup_work); |
3c7b4e6b CM |
1716 | |
1717 | pr_info("Kernel memory leak detector disabled\n"); | |
1718 | } | |
1719 | ||
1720 | /* | |
1721 | * Allow boot-time kmemleak disabling (enabled by default). | |
1722 | */ | |
1723 | static int kmemleak_boot_config(char *str) | |
1724 | { | |
1725 | if (!str) | |
1726 | return -EINVAL; | |
1727 | if (strcmp(str, "off") == 0) | |
1728 | kmemleak_disable(); | |
ab0155a2 JB |
1729 | else if (strcmp(str, "on") == 0) |
1730 | kmemleak_skip_disable = 1; | |
1731 | else | |
3c7b4e6b CM |
1732 | return -EINVAL; |
1733 | return 0; | |
1734 | } | |
1735 | early_param("kmemleak", kmemleak_boot_config); | |
1736 | ||
5f79020c CM |
1737 | static void __init print_log_trace(struct early_log *log) |
1738 | { | |
1739 | struct stack_trace trace; | |
1740 | ||
1741 | trace.nr_entries = log->trace_len; | |
1742 | trace.entries = log->trace; | |
1743 | ||
1744 | pr_notice("Early log backtrace:\n"); | |
1745 | print_stack_trace(&trace, 2); | |
1746 | } | |
1747 | ||
3c7b4e6b | 1748 | /* |
2030117d | 1749 | * Kmemleak initialization. |
3c7b4e6b CM |
1750 | */ |
1751 | void __init kmemleak_init(void) | |
1752 | { | |
1753 | int i; | |
1754 | unsigned long flags; | |
1755 | ||
ab0155a2 JB |
1756 | #ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF |
1757 | if (!kmemleak_skip_disable) { | |
1758 | kmemleak_disable(); | |
1759 | return; | |
1760 | } | |
1761 | #endif | |
1762 | ||
3c7b4e6b CM |
1763 | jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE); |
1764 | jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000); | |
1765 | ||
1766 | object_cache = KMEM_CACHE(kmemleak_object, SLAB_NOLEAKTRACE); | |
1767 | scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE); | |
1768 | INIT_PRIO_TREE_ROOT(&object_tree_root); | |
1769 | ||
b6693005 CM |
1770 | if (crt_early_log >= ARRAY_SIZE(early_log)) |
1771 | pr_warning("Early log buffer exceeded (%d), please increase " | |
1772 | "DEBUG_KMEMLEAK_EARLY_LOG_SIZE\n", crt_early_log); | |
1773 | ||
3c7b4e6b CM |
1774 | /* the kernel is still in UP mode, so disabling the IRQs is enough */ |
1775 | local_irq_save(flags); | |
b6693005 CM |
1776 | atomic_set(&kmemleak_early_log, 0); |
1777 | if (atomic_read(&kmemleak_error)) { | |
1778 | local_irq_restore(flags); | |
1779 | return; | |
1780 | } else | |
3c7b4e6b | 1781 | atomic_set(&kmemleak_enabled, 1); |
3c7b4e6b CM |
1782 | local_irq_restore(flags); |
1783 | ||
1784 | /* | |
1785 | * This is the point where tracking allocations is safe. Automatic | |
1786 | * scanning is started during the late initcall. Add the early logged | |
1787 | * callbacks to the kmemleak infrastructure. | |
1788 | */ | |
1789 | for (i = 0; i < crt_early_log; i++) { | |
1790 | struct early_log *log = &early_log[i]; | |
1791 | ||
1792 | switch (log->op_type) { | |
1793 | case KMEMLEAK_ALLOC: | |
fd678967 | 1794 | early_alloc(log); |
3c7b4e6b | 1795 | break; |
f528f0b8 CM |
1796 | case KMEMLEAK_ALLOC_PERCPU: |
1797 | early_alloc_percpu(log); | |
1798 | break; | |
3c7b4e6b CM |
1799 | case KMEMLEAK_FREE: |
1800 | kmemleak_free(log->ptr); | |
1801 | break; | |
53238a60 CM |
1802 | case KMEMLEAK_FREE_PART: |
1803 | kmemleak_free_part(log->ptr, log->size); | |
1804 | break; | |
f528f0b8 CM |
1805 | case KMEMLEAK_FREE_PERCPU: |
1806 | kmemleak_free_percpu(log->ptr); | |
1807 | break; | |
3c7b4e6b CM |
1808 | case KMEMLEAK_NOT_LEAK: |
1809 | kmemleak_not_leak(log->ptr); | |
1810 | break; | |
1811 | case KMEMLEAK_IGNORE: | |
1812 | kmemleak_ignore(log->ptr); | |
1813 | break; | |
1814 | case KMEMLEAK_SCAN_AREA: | |
c017b4be | 1815 | kmemleak_scan_area(log->ptr, log->size, GFP_KERNEL); |
3c7b4e6b CM |
1816 | break; |
1817 | case KMEMLEAK_NO_SCAN: | |
1818 | kmemleak_no_scan(log->ptr); | |
1819 | break; | |
1820 | default: | |
5f79020c CM |
1821 | kmemleak_warn("Unknown early log operation: %d\n", |
1822 | log->op_type); | |
1823 | } | |
1824 | ||
1825 | if (atomic_read(&kmemleak_warning)) { | |
1826 | print_log_trace(log); | |
1827 | atomic_set(&kmemleak_warning, 0); | |
3c7b4e6b CM |
1828 | } |
1829 | } | |
1830 | } | |
1831 | ||
1832 | /* | |
1833 | * Late initialization function. | |
1834 | */ | |
1835 | static int __init kmemleak_late_init(void) | |
1836 | { | |
1837 | struct dentry *dentry; | |
1838 | ||
1839 | atomic_set(&kmemleak_initialized, 1); | |
1840 | ||
1841 | if (atomic_read(&kmemleak_error)) { | |
1842 | /* | |
25985edc | 1843 | * Some error occurred and kmemleak was disabled. There is a |
3c7b4e6b CM |
1844 | * small chance that kmemleak_disable() was called immediately |
1845 | * after setting kmemleak_initialized and we may end up with | |
1846 | * two clean-up threads but serialized by scan_mutex. | |
1847 | */ | |
179a8100 | 1848 | schedule_work(&cleanup_work); |
3c7b4e6b CM |
1849 | return -ENOMEM; |
1850 | } | |
1851 | ||
1852 | dentry = debugfs_create_file("kmemleak", S_IRUGO, NULL, NULL, | |
1853 | &kmemleak_fops); | |
1854 | if (!dentry) | |
ae281064 | 1855 | pr_warning("Failed to create the debugfs kmemleak file\n"); |
4698c1f2 | 1856 | mutex_lock(&scan_mutex); |
3c7b4e6b | 1857 | start_scan_thread(); |
4698c1f2 | 1858 | mutex_unlock(&scan_mutex); |
3c7b4e6b CM |
1859 | |
1860 | pr_info("Kernel memory leak detector initialized\n"); | |
1861 | ||
1862 | return 0; | |
1863 | } | |
1864 | late_initcall(kmemleak_late_init); |