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