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