blob: 5405aff5a590c370d3c8ca37c0f9a0c19cb46775 [file] [log] [blame]
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001/*
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
Michel Lespinasse85d3a312012-10-08 16:31:27 -070032 * blocks. The object_tree_root is a red black tree used to look-up
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010033 * 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
Catalin Marinas4698c1f2009-06-26 17:38:27 +010051 * 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
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010055 *
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
Joe Perchesae281062009-06-23 14:40:26 +010064#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010066#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>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040072#include <linux/export.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010073#include <linux/kthread.h>
Michel Lespinasse85d3a312012-10-08 16:31:27 -070074#include <linux/rbtree.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010075#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>
Catalin Marinas179a8102009-09-07 10:14:42 +010094#include <linux/workqueue.h>
Catalin Marinas04609ccc2009-10-28 13:33:12 +000095#include <linux/crc32.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010096
97#include <asm/sections.h>
98#include <asm/processor.h>
Arun Sharma600634972011-07-26 16:09:06 -070099#include <linux/atomic.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100100
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -0800101#include <linux/kasan.h>
Pekka Enberg8e019362009-08-27 14:50:00 +0100102#include <linux/kmemcheck.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100103#include <linux/kmemleak.h>
Laura Abbott029aeff2011-11-15 23:49:09 +0000104#include <linux/memory_hotplug.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100105
106/*
107 * Kmemleak configuration and common defines.
108 */
109#define MAX_TRACE 16 /* stack trace length */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100110#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100111#define SECS_FIRST_SCAN 60 /* delay before the first scan */
112#define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */
Catalin Marinasaf986032009-08-27 14:29:12 +0100113#define MAX_SCAN_SIZE 4096 /* maximum size of a scanned block */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100114
115#define BYTES_PER_POINTER sizeof(void *)
116
Catalin Marinas216c04b2009-06-17 18:29:02 +0100117/* GFP bitmask for kmemleak internal allocations */
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000118#define gfp_kmemleak_mask(gfp) (((gfp) & (GFP_KERNEL | GFP_ATOMIC)) | \
119 __GFP_NORETRY | __GFP_NOMEMALLOC | \
120 __GFP_NOWARN)
Catalin Marinas216c04b2009-06-17 18:29:02 +0100121
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100122/* scanning area inside a memory block */
123struct kmemleak_scan_area {
124 struct hlist_node node;
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000125 unsigned long start;
126 size_t size;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100127};
128
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700129#define KMEMLEAK_GREY 0
130#define KMEMLEAK_BLACK -1
131
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100132/*
133 * Structure holding the metadata for each allocated memory block.
134 * Modifications to such objects should be made while holding the
135 * object->lock. Insertions or deletions from object_list, gray_list or
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700136 * rb_node are already protected by the corresponding locks or mutex (see
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100137 * the notes on locking above). These objects are reference-counted
138 * (use_count) and freed using the RCU mechanism.
139 */
140struct kmemleak_object {
141 spinlock_t lock;
142 unsigned long flags; /* object status flags */
143 struct list_head object_list;
144 struct list_head gray_list;
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700145 struct rb_node rb_node;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100146 struct rcu_head rcu; /* object_list lockless traversal */
147 /* object usage count; object freed when use_count == 0 */
148 atomic_t use_count;
149 unsigned long pointer;
150 size_t size;
151 /* minimum number of a pointers found before it is considered leak */
152 int min_count;
153 /* the total number of pointers found pointing to this object */
154 int count;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000155 /* checksum for detecting modified objects */
156 u32 checksum;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100157 /* memory ranges to be scanned inside an object (empty for all) */
158 struct hlist_head area_list;
159 unsigned long trace[MAX_TRACE];
160 unsigned int trace_len;
161 unsigned long jiffies; /* creation timestamp */
162 pid_t pid; /* pid of the current task */
163 char comm[TASK_COMM_LEN]; /* executable name */
164};
165
166/* flag representing the memory block allocation status */
167#define OBJECT_ALLOCATED (1 << 0)
168/* flag set after the first reporting of an unreference object */
169#define OBJECT_REPORTED (1 << 1)
170/* flag set to not scan the object */
171#define OBJECT_NO_SCAN (1 << 2)
172
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100173/* number of bytes to print per line; must be 16 or 32 */
174#define HEX_ROW_SIZE 16
175/* number of bytes to print at a time (1, 2, 4, 8) */
176#define HEX_GROUP_SIZE 1
177/* include ASCII after the hex output */
178#define HEX_ASCII 1
179/* max number of lines to be printed */
180#define HEX_MAX_LINES 2
181
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100182/* the list of all allocated objects */
183static LIST_HEAD(object_list);
184/* the list of gray-colored objects (see color_gray comment below) */
185static LIST_HEAD(gray_list);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700186/* search tree for object boundaries */
187static struct rb_root object_tree_root = RB_ROOT;
188/* rw_lock protecting the access to object_list and object_tree_root */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100189static DEFINE_RWLOCK(kmemleak_lock);
190
191/* allocation caches for kmemleak internal data */
192static struct kmem_cache *object_cache;
193static struct kmem_cache *scan_area_cache;
194
195/* set if tracing memory operations is enabled */
Li Zefan8910ae82014-04-03 14:46:29 -0700196static int kmemleak_enabled;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100197/* set in the late_initcall if there were no errors */
Li Zefan8910ae82014-04-03 14:46:29 -0700198static int kmemleak_initialized;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100199/* enables or disables early logging of the memory operations */
Li Zefan8910ae82014-04-03 14:46:29 -0700200static int kmemleak_early_log = 1;
Catalin Marinas5f790202011-09-28 12:17:03 +0100201/* set if a kmemleak warning was issued */
Li Zefan8910ae82014-04-03 14:46:29 -0700202static int kmemleak_warning;
Catalin Marinas5f790202011-09-28 12:17:03 +0100203/* set if a fatal kmemleak error has occurred */
Li Zefan8910ae82014-04-03 14:46:29 -0700204static int kmemleak_error;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100205
206/* minimum and maximum address that may be valid pointers */
207static unsigned long min_addr = ULONG_MAX;
208static unsigned long max_addr;
209
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100210static struct task_struct *scan_thread;
Catalin Marinasacf49682009-06-26 17:38:29 +0100211/* used to avoid reporting of recently allocated objects */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100212static unsigned long jiffies_min_age;
Catalin Marinasacf49682009-06-26 17:38:29 +0100213static unsigned long jiffies_last_scan;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100214/* delay between automatic memory scannings */
215static signed long jiffies_scan_wait;
216/* enables or disables the task stacks scanning */
Catalin Marinase0a2a162009-06-26 17:38:25 +0100217static int kmemleak_stack_scan = 1;
Catalin Marinas4698c1f2009-06-26 17:38:27 +0100218/* protects the memory scanning, parameters and debug/kmemleak file access */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100219static DEFINE_MUTEX(scan_mutex);
Jason Baronab0155a2010-07-19 11:54:17 +0100220/* setting kmemleak=on, will set this var, skipping the disable */
221static int kmemleak_skip_disable;
Li Zefandc9b3f42014-04-03 14:46:26 -0700222/* If there are leaks that can be reported */
223static bool kmemleak_found_leaks;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100224
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100225/*
Catalin Marinas20301172009-06-17 18:29:04 +0100226 * Early object allocation/freeing logging. Kmemleak is initialized after the
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100227 * kernel allocator. However, both the kernel allocator and kmemleak may
Catalin Marinas20301172009-06-17 18:29:04 +0100228 * allocate memory blocks which need to be tracked. Kmemleak defines an
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100229 * arbitrary buffer to hold the allocation/freeing information before it is
230 * fully initialized.
231 */
232
233/* kmemleak operation type for early logging */
234enum {
235 KMEMLEAK_ALLOC,
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100236 KMEMLEAK_ALLOC_PERCPU,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100237 KMEMLEAK_FREE,
Catalin Marinas53238a62009-07-07 10:33:00 +0100238 KMEMLEAK_FREE_PART,
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100239 KMEMLEAK_FREE_PERCPU,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100240 KMEMLEAK_NOT_LEAK,
241 KMEMLEAK_IGNORE,
242 KMEMLEAK_SCAN_AREA,
243 KMEMLEAK_NO_SCAN
244};
245
246/*
247 * Structure holding the information passed to kmemleak callbacks during the
248 * early logging.
249 */
250struct early_log {
251 int op_type; /* kmemleak operation type */
252 const void *ptr; /* allocated/freed memory block */
253 size_t size; /* memory block size */
254 int min_count; /* minimum reference count */
Catalin Marinasfd678962009-08-27 14:29:17 +0100255 unsigned long trace[MAX_TRACE]; /* stack trace */
256 unsigned int trace_len; /* stack trace length */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100257};
258
259/* early logging buffer and current position */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100260static struct early_log
261 early_log[CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE] __initdata;
262static int crt_early_log __initdata;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100263
264static void kmemleak_disable(void);
265
266/*
267 * Print a warning and dump the stack trace.
268 */
Catalin Marinas5f790202011-09-28 12:17:03 +0100269#define kmemleak_warn(x...) do { \
270 pr_warning(x); \
271 dump_stack(); \
Li Zefan8910ae82014-04-03 14:46:29 -0700272 kmemleak_warning = 1; \
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100273} while (0)
274
275/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300276 * Macro invoked when a serious kmemleak condition occurred and cannot be
Catalin Marinas20301172009-06-17 18:29:04 +0100277 * recovered from. Kmemleak will be disabled and further allocation/freeing
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100278 * tracing no longer available.
279 */
Catalin Marinas000814f2009-06-17 18:29:03 +0100280#define kmemleak_stop(x...) do { \
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100281 kmemleak_warn(x); \
282 kmemleak_disable(); \
283} while (0)
284
285/*
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100286 * Printing of the objects hex dump to the seq file. The number of lines to be
287 * printed is limited to HEX_MAX_LINES to prevent seq file spamming. The
288 * actual number of printed bytes depends on HEX_ROW_SIZE. It must be called
289 * with the object->lock held.
290 */
291static void hex_dump_object(struct seq_file *seq,
292 struct kmemleak_object *object)
293{
294 const u8 *ptr = (const u8 *)object->pointer;
295 int i, len, remaining;
296 unsigned char linebuf[HEX_ROW_SIZE * 5];
297
298 /* limit the number of lines to HEX_MAX_LINES */
299 remaining = len =
300 min(object->size, (size_t)(HEX_MAX_LINES * HEX_ROW_SIZE));
301
302 seq_printf(seq, " hex dump (first %d bytes):\n", len);
303 for (i = 0; i < len; i += HEX_ROW_SIZE) {
304 int linelen = min(remaining, HEX_ROW_SIZE);
305
306 remaining -= HEX_ROW_SIZE;
307 hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE,
308 HEX_GROUP_SIZE, linebuf, sizeof(linebuf),
309 HEX_ASCII);
310 seq_printf(seq, " %s\n", linebuf);
311 }
312}
313
314/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100315 * Object colors, encoded with count and min_count:
316 * - white - orphan object, not enough references to it (count < min_count)
317 * - gray - not orphan, not marked as false positive (min_count == 0) or
318 * sufficient references to it (count >= min_count)
319 * - black - ignore, it doesn't contain references (e.g. text section)
320 * (min_count == -1). No function defined for this color.
321 * Newly created objects don't have any color assigned (object->count == -1)
322 * before the next memory scan when they become white.
323 */
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100324static bool color_white(const struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100325{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700326 return object->count != KMEMLEAK_BLACK &&
327 object->count < object->min_count;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100328}
329
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100330static bool color_gray(const struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100331{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700332 return object->min_count != KMEMLEAK_BLACK &&
333 object->count >= object->min_count;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100334}
335
336/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100337 * Objects are considered unreferenced only if their color is white, they have
338 * not be deleted and have a minimum age to avoid false positives caused by
339 * pointers temporarily stored in CPU registers.
340 */
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100341static bool unreferenced_object(struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100342{
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000343 return (color_white(object) && object->flags & OBJECT_ALLOCATED) &&
Catalin Marinasacf49682009-06-26 17:38:29 +0100344 time_before_eq(object->jiffies + jiffies_min_age,
345 jiffies_last_scan);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100346}
347
348/*
Catalin Marinasbab4a342009-06-26 17:38:26 +0100349 * Printing of the unreferenced objects information to the seq file. The
350 * print_unreferenced function must be called with the object->lock held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100351 */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100352static void print_unreferenced(struct seq_file *seq,
353 struct kmemleak_object *object)
354{
355 int i;
Catalin Marinasfefdd332009-10-28 13:33:12 +0000356 unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100357
Catalin Marinasbab4a342009-06-26 17:38:26 +0100358 seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
359 object->pointer, object->size);
Catalin Marinasfefdd332009-10-28 13:33:12 +0000360 seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n",
361 object->comm, object->pid, object->jiffies,
362 msecs_age / 1000, msecs_age % 1000);
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100363 hex_dump_object(seq, object);
Catalin Marinasbab4a342009-06-26 17:38:26 +0100364 seq_printf(seq, " backtrace:\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100365
366 for (i = 0; i < object->trace_len; i++) {
367 void *ptr = (void *)object->trace[i];
Catalin Marinasbab4a342009-06-26 17:38:26 +0100368 seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100369 }
370}
371
372/*
373 * Print the kmemleak_object information. This function is used mainly for
374 * debugging special cases when kmemleak operations. It must be called with
375 * the object->lock held.
376 */
377static void dump_object_info(struct kmemleak_object *object)
378{
379 struct stack_trace trace;
380
381 trace.nr_entries = object->trace_len;
382 trace.entries = object->trace;
383
Joe Perchesae281062009-06-23 14:40:26 +0100384 pr_notice("Object 0x%08lx (size %zu):\n",
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700385 object->pointer, object->size);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100386 pr_notice(" comm \"%s\", pid %d, jiffies %lu\n",
387 object->comm, object->pid, object->jiffies);
388 pr_notice(" min_count = %d\n", object->min_count);
389 pr_notice(" count = %d\n", object->count);
Catalin Marinas189d84e2009-08-27 14:29:15 +0100390 pr_notice(" flags = 0x%lx\n", object->flags);
Jianpeng Maaae0ad72014-06-06 14:38:16 -0700391 pr_notice(" checksum = %u\n", object->checksum);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100392 pr_notice(" backtrace:\n");
393 print_stack_trace(&trace, 4);
394}
395
396/*
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700397 * Look-up a memory block metadata (kmemleak_object) in the object search
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100398 * tree based on a pointer value. If alias is 0, only values pointing to the
399 * beginning of the memory block are allowed. The kmemleak_lock must be held
400 * when calling this function.
401 */
402static struct kmemleak_object *lookup_object(unsigned long ptr, int alias)
403{
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700404 struct rb_node *rb = object_tree_root.rb_node;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100405
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700406 while (rb) {
407 struct kmemleak_object *object =
408 rb_entry(rb, struct kmemleak_object, rb_node);
409 if (ptr < object->pointer)
410 rb = object->rb_node.rb_left;
411 else if (object->pointer + object->size <= ptr)
412 rb = object->rb_node.rb_right;
413 else if (object->pointer == ptr || alias)
414 return object;
415 else {
Catalin Marinas5f790202011-09-28 12:17:03 +0100416 kmemleak_warn("Found object by alias at 0x%08lx\n",
417 ptr);
Catalin Marinasa7686a42010-07-19 11:54:16 +0100418 dump_object_info(object);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700419 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100420 }
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700421 }
422 return NULL;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100423}
424
425/*
426 * Increment the object use_count. Return 1 if successful or 0 otherwise. Note
427 * that once an object's use_count reached 0, the RCU freeing was already
428 * registered and the object should no longer be used. This function must be
429 * called under the protection of rcu_read_lock().
430 */
431static int get_object(struct kmemleak_object *object)
432{
433 return atomic_inc_not_zero(&object->use_count);
434}
435
436/*
437 * RCU callback to free a kmemleak_object.
438 */
439static void free_object_rcu(struct rcu_head *rcu)
440{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800441 struct hlist_node *tmp;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100442 struct kmemleak_scan_area *area;
443 struct kmemleak_object *object =
444 container_of(rcu, struct kmemleak_object, rcu);
445
446 /*
447 * Once use_count is 0 (guaranteed by put_object), there is no other
448 * code accessing this object, hence no need for locking.
449 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800450 hlist_for_each_entry_safe(area, tmp, &object->area_list, node) {
451 hlist_del(&area->node);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100452 kmem_cache_free(scan_area_cache, area);
453 }
454 kmem_cache_free(object_cache, object);
455}
456
457/*
458 * Decrement the object use_count. Once the count is 0, free the object using
459 * an RCU callback. Since put_object() may be called via the kmemleak_free() ->
460 * delete_object() path, the delayed RCU freeing ensures that there is no
461 * recursive call to the kernel allocator. Lock-less RCU object_list traversal
462 * is also possible.
463 */
464static void put_object(struct kmemleak_object *object)
465{
466 if (!atomic_dec_and_test(&object->use_count))
467 return;
468
469 /* should only get here after delete_object was called */
470 WARN_ON(object->flags & OBJECT_ALLOCATED);
471
472 call_rcu(&object->rcu, free_object_rcu);
473}
474
475/*
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700476 * Look up an object in the object search tree and increase its use_count.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100477 */
478static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias)
479{
480 unsigned long flags;
481 struct kmemleak_object *object = NULL;
482
483 rcu_read_lock();
484 read_lock_irqsave(&kmemleak_lock, flags);
485 if (ptr >= min_addr && ptr < max_addr)
486 object = lookup_object(ptr, alias);
487 read_unlock_irqrestore(&kmemleak_lock, flags);
488
489 /* check whether the object is still available */
490 if (object && !get_object(object))
491 object = NULL;
492 rcu_read_unlock();
493
494 return object;
495}
496
497/*
Catalin Marinasfd678962009-08-27 14:29:17 +0100498 * Save stack trace to the given array of MAX_TRACE size.
499 */
500static int __save_stack_trace(unsigned long *trace)
501{
502 struct stack_trace stack_trace;
503
504 stack_trace.max_entries = MAX_TRACE;
505 stack_trace.nr_entries = 0;
506 stack_trace.entries = trace;
507 stack_trace.skip = 2;
508 save_stack_trace(&stack_trace);
509
510 return stack_trace.nr_entries;
511}
512
513/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100514 * Create the metadata (struct kmemleak_object) corresponding to an allocated
515 * memory block and add it to the object_list and object_tree_root.
516 */
Catalin Marinasfd678962009-08-27 14:29:17 +0100517static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
518 int min_count, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100519{
520 unsigned long flags;
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700521 struct kmemleak_object *object, *parent;
522 struct rb_node **link, *rb_parent;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100523
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000524 object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100525 if (!object) {
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000526 pr_warning("Cannot allocate a kmemleak_object structure\n");
527 kmemleak_disable();
Catalin Marinasfd678962009-08-27 14:29:17 +0100528 return NULL;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100529 }
530
531 INIT_LIST_HEAD(&object->object_list);
532 INIT_LIST_HEAD(&object->gray_list);
533 INIT_HLIST_HEAD(&object->area_list);
534 spin_lock_init(&object->lock);
535 atomic_set(&object->use_count, 1);
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000536 object->flags = OBJECT_ALLOCATED;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100537 object->pointer = ptr;
538 object->size = size;
539 object->min_count = min_count;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000540 object->count = 0; /* white color initially */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100541 object->jiffies = jiffies;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000542 object->checksum = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100543
544 /* task information */
545 if (in_irq()) {
546 object->pid = 0;
547 strncpy(object->comm, "hardirq", sizeof(object->comm));
548 } else if (in_softirq()) {
549 object->pid = 0;
550 strncpy(object->comm, "softirq", sizeof(object->comm));
551 } else {
552 object->pid = current->pid;
553 /*
554 * There is a small chance of a race with set_task_comm(),
555 * however using get_task_comm() here may cause locking
556 * dependency issues with current->alloc_lock. In the worst
557 * case, the command line is not correct.
558 */
559 strncpy(object->comm, current->comm, sizeof(object->comm));
560 }
561
562 /* kernel backtrace */
Catalin Marinasfd678962009-08-27 14:29:17 +0100563 object->trace_len = __save_stack_trace(object->trace);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100564
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100565 write_lock_irqsave(&kmemleak_lock, flags);
Luis R. Rodriguez0580a182009-09-08 17:32:34 +0100566
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100567 min_addr = min(min_addr, ptr);
568 max_addr = max(max_addr, ptr + size);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700569 link = &object_tree_root.rb_node;
570 rb_parent = NULL;
571 while (*link) {
572 rb_parent = *link;
573 parent = rb_entry(rb_parent, struct kmemleak_object, rb_node);
574 if (ptr + size <= parent->pointer)
575 link = &parent->rb_node.rb_left;
576 else if (parent->pointer + parent->size <= ptr)
577 link = &parent->rb_node.rb_right;
578 else {
579 kmemleak_stop("Cannot insert 0x%lx into the object "
580 "search tree (overlaps existing)\n",
581 ptr);
582 kmem_cache_free(object_cache, object);
583 object = parent;
584 spin_lock(&object->lock);
585 dump_object_info(object);
586 spin_unlock(&object->lock);
587 goto out;
588 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100589 }
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700590 rb_link_node(&object->rb_node, rb_parent, link);
591 rb_insert_color(&object->rb_node, &object_tree_root);
592
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100593 list_add_tail_rcu(&object->object_list, &object_list);
594out:
595 write_unlock_irqrestore(&kmemleak_lock, flags);
Catalin Marinasfd678962009-08-27 14:29:17 +0100596 return object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100597}
598
599/*
600 * Remove the metadata (struct kmemleak_object) for a memory block from the
601 * object_list and object_tree_root and decrement its use_count.
602 */
Catalin Marinas53238a62009-07-07 10:33:00 +0100603static void __delete_object(struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100604{
605 unsigned long flags;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100606
607 write_lock_irqsave(&kmemleak_lock, flags);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700608 rb_erase(&object->rb_node, &object_tree_root);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100609 list_del_rcu(&object->object_list);
610 write_unlock_irqrestore(&kmemleak_lock, flags);
611
612 WARN_ON(!(object->flags & OBJECT_ALLOCATED));
Catalin Marinas53238a62009-07-07 10:33:00 +0100613 WARN_ON(atomic_read(&object->use_count) < 2);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100614
615 /*
616 * Locking here also ensures that the corresponding memory block
617 * cannot be freed when it is being scanned.
618 */
619 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100620 object->flags &= ~OBJECT_ALLOCATED;
621 spin_unlock_irqrestore(&object->lock, flags);
622 put_object(object);
623}
624
625/*
Catalin Marinas53238a62009-07-07 10:33:00 +0100626 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
627 * delete it.
628 */
629static void delete_object_full(unsigned long ptr)
630{
631 struct kmemleak_object *object;
632
633 object = find_and_get_object(ptr, 0);
634 if (!object) {
635#ifdef DEBUG
636 kmemleak_warn("Freeing unknown object at 0x%08lx\n",
637 ptr);
638#endif
639 return;
640 }
641 __delete_object(object);
642 put_object(object);
643}
644
645/*
646 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
647 * delete it. If the memory block is partially freed, the function may create
648 * additional metadata for the remaining parts of the block.
649 */
650static void delete_object_part(unsigned long ptr, size_t size)
651{
652 struct kmemleak_object *object;
653 unsigned long start, end;
654
655 object = find_and_get_object(ptr, 1);
656 if (!object) {
657#ifdef DEBUG
658 kmemleak_warn("Partially freeing unknown object at 0x%08lx "
659 "(size %zu)\n", ptr, size);
660#endif
661 return;
662 }
663 __delete_object(object);
664
665 /*
666 * Create one or two objects that may result from the memory block
667 * split. Note that partial freeing is only done by free_bootmem() and
668 * this happens before kmemleak_init() is called. The path below is
669 * only executed during early log recording in kmemleak_init(), so
670 * GFP_KERNEL is enough.
671 */
672 start = object->pointer;
673 end = object->pointer + object->size;
674 if (ptr > start)
675 create_object(start, ptr - start, object->min_count,
676 GFP_KERNEL);
677 if (ptr + size < end)
678 create_object(ptr + size, end - ptr - size, object->min_count,
679 GFP_KERNEL);
680
681 put_object(object);
682}
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700683
684static void __paint_it(struct kmemleak_object *object, int color)
685{
686 object->min_count = color;
687 if (color == KMEMLEAK_BLACK)
688 object->flags |= OBJECT_NO_SCAN;
689}
690
691static void paint_it(struct kmemleak_object *object, int color)
692{
693 unsigned long flags;
694
695 spin_lock_irqsave(&object->lock, flags);
696 __paint_it(object, color);
697 spin_unlock_irqrestore(&object->lock, flags);
698}
699
700static void paint_ptr(unsigned long ptr, int color)
701{
702 struct kmemleak_object *object;
703
704 object = find_and_get_object(ptr, 0);
705 if (!object) {
706 kmemleak_warn("Trying to color unknown object "
707 "at 0x%08lx as %s\n", ptr,
708 (color == KMEMLEAK_GREY) ? "Grey" :
709 (color == KMEMLEAK_BLACK) ? "Black" : "Unknown");
710 return;
711 }
712 paint_it(object, color);
713 put_object(object);
714}
715
Catalin Marinas53238a62009-07-07 10:33:00 +0100716/*
Holger Hans Peter Freyther145b64b2010-07-22 19:54:13 +0800717 * Mark an object permanently as gray-colored so that it can no longer be
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100718 * reported as a leak. This is used in general to mark a false positive.
719 */
720static void make_gray_object(unsigned long ptr)
721{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700722 paint_ptr(ptr, KMEMLEAK_GREY);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100723}
724
725/*
726 * Mark the object as black-colored so that it is ignored from scans and
727 * reporting.
728 */
729static void make_black_object(unsigned long ptr)
730{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700731 paint_ptr(ptr, KMEMLEAK_BLACK);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100732}
733
734/*
735 * Add a scanning area to the object. If at least one such area is added,
736 * kmemleak will only scan these ranges rather than the whole memory block.
737 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000738static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100739{
740 unsigned long flags;
741 struct kmemleak_object *object;
742 struct kmemleak_scan_area *area;
743
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000744 object = find_and_get_object(ptr, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100745 if (!object) {
Joe Perchesae281062009-06-23 14:40:26 +0100746 kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n",
747 ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100748 return;
749 }
750
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000751 area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100752 if (!area) {
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000753 pr_warning("Cannot allocate a scan area\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100754 goto out;
755 }
756
757 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas7f88f882013-11-12 15:07:45 -0800758 if (size == SIZE_MAX) {
759 size = object->pointer + object->size - ptr;
760 } else if (ptr + size > object->pointer + object->size) {
Joe Perchesae281062009-06-23 14:40:26 +0100761 kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100762 dump_object_info(object);
763 kmem_cache_free(scan_area_cache, area);
764 goto out_unlock;
765 }
766
767 INIT_HLIST_NODE(&area->node);
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000768 area->start = ptr;
769 area->size = size;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100770
771 hlist_add_head(&area->node, &object->area_list);
772out_unlock:
773 spin_unlock_irqrestore(&object->lock, flags);
774out:
775 put_object(object);
776}
777
778/*
779 * Set the OBJECT_NO_SCAN flag for the object corresponding to the give
780 * pointer. Such object will not be scanned by kmemleak but references to it
781 * are searched.
782 */
783static void object_no_scan(unsigned long ptr)
784{
785 unsigned long flags;
786 struct kmemleak_object *object;
787
788 object = find_and_get_object(ptr, 0);
789 if (!object) {
Joe Perchesae281062009-06-23 14:40:26 +0100790 kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100791 return;
792 }
793
794 spin_lock_irqsave(&object->lock, flags);
795 object->flags |= OBJECT_NO_SCAN;
796 spin_unlock_irqrestore(&object->lock, flags);
797 put_object(object);
798}
799
800/*
801 * Log an early kmemleak_* call to the early_log buffer. These calls will be
802 * processed later once kmemleak is fully initialized.
803 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100804static void __init log_early(int op_type, const void *ptr, size_t size,
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000805 int min_count)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100806{
807 unsigned long flags;
808 struct early_log *log;
809
Li Zefan8910ae82014-04-03 14:46:29 -0700810 if (kmemleak_error) {
Catalin Marinasb6693002011-09-28 17:22:56 +0100811 /* kmemleak stopped recording, just count the requests */
812 crt_early_log++;
813 return;
814 }
815
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100816 if (crt_early_log >= ARRAY_SIZE(early_log)) {
Catalin Marinasa9d90582009-06-25 10:16:11 +0100817 kmemleak_disable();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100818 return;
819 }
820
821 /*
822 * There is no need for locking since the kernel is still in UP mode
823 * at this stage. Disabling the IRQs is enough.
824 */
825 local_irq_save(flags);
826 log = &early_log[crt_early_log];
827 log->op_type = op_type;
828 log->ptr = ptr;
829 log->size = size;
830 log->min_count = min_count;
Catalin Marinas5f790202011-09-28 12:17:03 +0100831 log->trace_len = __save_stack_trace(log->trace);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100832 crt_early_log++;
833 local_irq_restore(flags);
834}
835
836/*
Catalin Marinasfd678962009-08-27 14:29:17 +0100837 * Log an early allocated block and populate the stack trace.
838 */
839static void early_alloc(struct early_log *log)
840{
841 struct kmemleak_object *object;
842 unsigned long flags;
843 int i;
844
Li Zefan8910ae82014-04-03 14:46:29 -0700845 if (!kmemleak_enabled || !log->ptr || IS_ERR(log->ptr))
Catalin Marinasfd678962009-08-27 14:29:17 +0100846 return;
847
848 /*
849 * RCU locking needed to ensure object is not freed via put_object().
850 */
851 rcu_read_lock();
852 object = create_object((unsigned long)log->ptr, log->size,
Tetsuo Handac1bcd6b2009-10-09 10:39:24 +0100853 log->min_count, GFP_ATOMIC);
Catalin Marinas0d5d1aa2009-10-09 10:30:34 +0100854 if (!object)
855 goto out;
Catalin Marinasfd678962009-08-27 14:29:17 +0100856 spin_lock_irqsave(&object->lock, flags);
857 for (i = 0; i < log->trace_len; i++)
858 object->trace[i] = log->trace[i];
859 object->trace_len = log->trace_len;
860 spin_unlock_irqrestore(&object->lock, flags);
Catalin Marinas0d5d1aa2009-10-09 10:30:34 +0100861out:
Catalin Marinasfd678962009-08-27 14:29:17 +0100862 rcu_read_unlock();
863}
864
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100865/*
866 * Log an early allocated block and populate the stack trace.
867 */
868static void early_alloc_percpu(struct early_log *log)
869{
870 unsigned int cpu;
871 const void __percpu *ptr = log->ptr;
872
873 for_each_possible_cpu(cpu) {
874 log->ptr = per_cpu_ptr(ptr, cpu);
875 early_alloc(log);
876 }
877}
878
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100879/**
880 * kmemleak_alloc - register a newly allocated object
881 * @ptr: pointer to beginning of the object
882 * @size: size of the object
883 * @min_count: minimum number of references to this object. If during memory
884 * scanning a number of references less than @min_count is found,
885 * the object is reported as a memory leak. If @min_count is 0,
886 * the object is never reported as a leak. If @min_count is -1,
887 * the object is ignored (not scanned and not reported as a leak)
888 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
889 *
890 * This function is called from the kernel allocators when a new object
891 * (memory block) is allocated (kmem_cache_alloc, kmalloc, vmalloc etc.).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100892 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100893void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count,
894 gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100895{
896 pr_debug("%s(0x%p, %zu, %d)\n", __func__, ptr, size, min_count);
897
Li Zefan8910ae82014-04-03 14:46:29 -0700898 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100899 create_object((unsigned long)ptr, size, min_count, gfp);
Li Zefan8910ae82014-04-03 14:46:29 -0700900 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000901 log_early(KMEMLEAK_ALLOC, ptr, size, min_count);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100902}
903EXPORT_SYMBOL_GPL(kmemleak_alloc);
904
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100905/**
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100906 * kmemleak_alloc_percpu - register a newly allocated __percpu object
907 * @ptr: __percpu pointer to beginning of the object
908 * @size: size of the object
909 *
910 * This function is called from the kernel percpu allocator when a new object
911 * (memory block) is allocated (alloc_percpu). It assumes GFP_KERNEL
912 * allocation.
913 */
914void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size)
915{
916 unsigned int cpu;
917
918 pr_debug("%s(0x%p, %zu)\n", __func__, ptr, size);
919
920 /*
921 * Percpu allocations are only scanned and not reported as leaks
922 * (min_count is set to 0).
923 */
Li Zefan8910ae82014-04-03 14:46:29 -0700924 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100925 for_each_possible_cpu(cpu)
926 create_object((unsigned long)per_cpu_ptr(ptr, cpu),
927 size, 0, GFP_KERNEL);
Li Zefan8910ae82014-04-03 14:46:29 -0700928 else if (kmemleak_early_log)
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100929 log_early(KMEMLEAK_ALLOC_PERCPU, ptr, size, 0);
930}
931EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
932
933/**
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100934 * kmemleak_free - unregister a previously registered object
935 * @ptr: pointer to beginning of the object
936 *
937 * This function is called from the kernel allocators when an object (memory
938 * block) is freed (kmem_cache_free, kfree, vfree etc.).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100939 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100940void __ref kmemleak_free(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100941{
942 pr_debug("%s(0x%p)\n", __func__, ptr);
943
Li Zefan8910ae82014-04-03 14:46:29 -0700944 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas53238a62009-07-07 10:33:00 +0100945 delete_object_full((unsigned long)ptr);
Li Zefan8910ae82014-04-03 14:46:29 -0700946 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000947 log_early(KMEMLEAK_FREE, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100948}
949EXPORT_SYMBOL_GPL(kmemleak_free);
950
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100951/**
952 * kmemleak_free_part - partially unregister a previously registered object
953 * @ptr: pointer to the beginning or inside the object. This also
954 * represents the start of the range to be freed
955 * @size: size to be unregistered
956 *
957 * This function is called when only a part of a memory block is freed
958 * (usually from the bootmem allocator).
Catalin Marinas53238a62009-07-07 10:33:00 +0100959 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100960void __ref kmemleak_free_part(const void *ptr, size_t size)
Catalin Marinas53238a62009-07-07 10:33:00 +0100961{
962 pr_debug("%s(0x%p)\n", __func__, ptr);
963
Li Zefan8910ae82014-04-03 14:46:29 -0700964 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas53238a62009-07-07 10:33:00 +0100965 delete_object_part((unsigned long)ptr, size);
Li Zefan8910ae82014-04-03 14:46:29 -0700966 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000967 log_early(KMEMLEAK_FREE_PART, ptr, size, 0);
Catalin Marinas53238a62009-07-07 10:33:00 +0100968}
969EXPORT_SYMBOL_GPL(kmemleak_free_part);
970
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100971/**
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100972 * kmemleak_free_percpu - unregister a previously registered __percpu object
973 * @ptr: __percpu pointer to beginning of the object
974 *
975 * This function is called from the kernel percpu allocator when an object
976 * (memory block) is freed (free_percpu).
977 */
978void __ref kmemleak_free_percpu(const void __percpu *ptr)
979{
980 unsigned int cpu;
981
982 pr_debug("%s(0x%p)\n", __func__, ptr);
983
Li Zefan8910ae82014-04-03 14:46:29 -0700984 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100985 for_each_possible_cpu(cpu)
986 delete_object_full((unsigned long)per_cpu_ptr(ptr,
987 cpu));
Li Zefan8910ae82014-04-03 14:46:29 -0700988 else if (kmemleak_early_log)
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100989 log_early(KMEMLEAK_FREE_PERCPU, ptr, 0, 0);
990}
991EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
992
993/**
Catalin Marinasffe2c742014-06-06 14:38:17 -0700994 * kmemleak_update_trace - update object allocation stack trace
995 * @ptr: pointer to beginning of the object
996 *
997 * Override the object allocation stack trace for cases where the actual
998 * allocation place is not always useful.
999 */
1000void __ref kmemleak_update_trace(const void *ptr)
1001{
1002 struct kmemleak_object *object;
1003 unsigned long flags;
1004
1005 pr_debug("%s(0x%p)\n", __func__, ptr);
1006
1007 if (!kmemleak_enabled || IS_ERR_OR_NULL(ptr))
1008 return;
1009
1010 object = find_and_get_object((unsigned long)ptr, 1);
1011 if (!object) {
1012#ifdef DEBUG
1013 kmemleak_warn("Updating stack trace for unknown object at %p\n",
1014 ptr);
1015#endif
1016 return;
1017 }
1018
1019 spin_lock_irqsave(&object->lock, flags);
1020 object->trace_len = __save_stack_trace(object->trace);
1021 spin_unlock_irqrestore(&object->lock, flags);
1022
1023 put_object(object);
1024}
1025EXPORT_SYMBOL(kmemleak_update_trace);
1026
1027/**
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001028 * kmemleak_not_leak - mark an allocated object as false positive
1029 * @ptr: pointer to beginning of the object
1030 *
1031 * Calling this function on an object will cause the memory block to no longer
1032 * be reported as leak and always be scanned.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001033 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001034void __ref kmemleak_not_leak(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001035{
1036 pr_debug("%s(0x%p)\n", __func__, ptr);
1037
Li Zefan8910ae82014-04-03 14:46:29 -07001038 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001039 make_gray_object((unsigned long)ptr);
Li Zefan8910ae82014-04-03 14:46:29 -07001040 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001041 log_early(KMEMLEAK_NOT_LEAK, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001042}
1043EXPORT_SYMBOL(kmemleak_not_leak);
1044
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001045/**
1046 * kmemleak_ignore - ignore an allocated object
1047 * @ptr: pointer to beginning of the object
1048 *
1049 * Calling this function on an object will cause the memory block to be
1050 * ignored (not scanned and not reported as a leak). This is usually done when
1051 * it is known that the corresponding block is not a leak and does not contain
1052 * any references to other allocated memory blocks.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001053 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001054void __ref kmemleak_ignore(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001055{
1056 pr_debug("%s(0x%p)\n", __func__, ptr);
1057
Li Zefan8910ae82014-04-03 14:46:29 -07001058 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001059 make_black_object((unsigned long)ptr);
Li Zefan8910ae82014-04-03 14:46:29 -07001060 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001061 log_early(KMEMLEAK_IGNORE, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001062}
1063EXPORT_SYMBOL(kmemleak_ignore);
1064
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001065/**
1066 * kmemleak_scan_area - limit the range to be scanned in an allocated object
1067 * @ptr: pointer to beginning or inside the object. This also
1068 * represents the start of the scan area
1069 * @size: size of the scan area
1070 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
1071 *
1072 * This function is used when it is known that only certain parts of an object
1073 * contain references to other objects. Kmemleak will only scan these areas
1074 * reducing the number false negatives.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001075 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001076void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001077{
1078 pr_debug("%s(0x%p)\n", __func__, ptr);
1079
Li Zefan8910ae82014-04-03 14:46:29 -07001080 if (kmemleak_enabled && ptr && size && !IS_ERR(ptr))
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001081 add_scan_area((unsigned long)ptr, size, gfp);
Li Zefan8910ae82014-04-03 14:46:29 -07001082 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001083 log_early(KMEMLEAK_SCAN_AREA, ptr, size, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001084}
1085EXPORT_SYMBOL(kmemleak_scan_area);
1086
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001087/**
1088 * kmemleak_no_scan - do not scan an allocated object
1089 * @ptr: pointer to beginning of the object
1090 *
1091 * This function notifies kmemleak not to scan the given memory block. Useful
1092 * in situations where it is known that the given object does not contain any
1093 * references to other objects. Kmemleak will not scan such objects reducing
1094 * the number of false negatives.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001095 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001096void __ref kmemleak_no_scan(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001097{
1098 pr_debug("%s(0x%p)\n", __func__, ptr);
1099
Li Zefan8910ae82014-04-03 14:46:29 -07001100 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001101 object_no_scan((unsigned long)ptr);
Li Zefan8910ae82014-04-03 14:46:29 -07001102 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001103 log_early(KMEMLEAK_NO_SCAN, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001104}
1105EXPORT_SYMBOL(kmemleak_no_scan);
1106
1107/*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001108 * Update an object's checksum and return true if it was modified.
1109 */
1110static bool update_checksum(struct kmemleak_object *object)
1111{
1112 u32 old_csum = object->checksum;
1113
1114 if (!kmemcheck_is_obj_initialized(object->pointer, object->size))
1115 return false;
1116
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001117 kasan_disable_current();
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001118 object->checksum = crc32(0, (void *)object->pointer, object->size);
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001119 kasan_enable_current();
1120
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001121 return object->checksum != old_csum;
1122}
1123
1124/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001125 * Memory scanning is a long process and it needs to be interruptable. This
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001126 * function checks whether such interrupt condition occurred.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001127 */
1128static int scan_should_stop(void)
1129{
Li Zefan8910ae82014-04-03 14:46:29 -07001130 if (!kmemleak_enabled)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001131 return 1;
1132
1133 /*
1134 * This function may be called from either process or kthread context,
1135 * hence the need to check for both stop conditions.
1136 */
1137 if (current->mm)
1138 return signal_pending(current);
1139 else
1140 return kthread_should_stop();
1141
1142 return 0;
1143}
1144
1145/*
1146 * Scan a memory block (exclusive range) for valid pointers and add those
1147 * found to the gray list.
1148 */
1149static void scan_block(void *_start, void *_end,
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001150 struct kmemleak_object *scanned, int allow_resched)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001151{
1152 unsigned long *ptr;
1153 unsigned long *start = PTR_ALIGN(_start, BYTES_PER_POINTER);
1154 unsigned long *end = _end - (BYTES_PER_POINTER - 1);
1155
1156 for (ptr = start; ptr < end; ptr++) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001157 struct kmemleak_object *object;
Pekka Enberg8e019362009-08-27 14:50:00 +01001158 unsigned long flags;
1159 unsigned long pointer;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001160
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001161 if (allow_resched)
1162 cond_resched();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001163 if (scan_should_stop())
1164 break;
1165
Pekka Enberg8e019362009-08-27 14:50:00 +01001166 /* don't scan uninitialized memory */
1167 if (!kmemcheck_is_obj_initialized((unsigned long)ptr,
1168 BYTES_PER_POINTER))
1169 continue;
1170
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001171 kasan_disable_current();
Pekka Enberg8e019362009-08-27 14:50:00 +01001172 pointer = *ptr;
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001173 kasan_enable_current();
Pekka Enberg8e019362009-08-27 14:50:00 +01001174
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001175 object = find_and_get_object(pointer, 1);
1176 if (!object)
1177 continue;
1178 if (object == scanned) {
1179 /* self referenced, ignore */
1180 put_object(object);
1181 continue;
1182 }
1183
1184 /*
1185 * Avoid the lockdep recursive warning on object->lock being
1186 * previously acquired in scan_object(). These locks are
1187 * enclosed by scan_mutex.
1188 */
1189 spin_lock_irqsave_nested(&object->lock, flags,
1190 SINGLE_DEPTH_NESTING);
1191 if (!color_white(object)) {
1192 /* non-orphan, ignored or new */
1193 spin_unlock_irqrestore(&object->lock, flags);
1194 put_object(object);
1195 continue;
1196 }
1197
1198 /*
1199 * Increase the object's reference count (number of pointers
1200 * to the memory block). If this count reaches the required
1201 * minimum, the object's color will become gray and it will be
1202 * added to the gray_list.
1203 */
1204 object->count++;
Catalin Marinas0587da42009-10-28 13:33:11 +00001205 if (color_gray(object)) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001206 list_add_tail(&object->gray_list, &gray_list);
Catalin Marinas0587da42009-10-28 13:33:11 +00001207 spin_unlock_irqrestore(&object->lock, flags);
1208 continue;
1209 }
1210
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001211 spin_unlock_irqrestore(&object->lock, flags);
Catalin Marinas0587da42009-10-28 13:33:11 +00001212 put_object(object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001213 }
1214}
1215
1216/*
1217 * Scan a memory block corresponding to a kmemleak_object. A condition is
1218 * that object->use_count >= 1.
1219 */
1220static void scan_object(struct kmemleak_object *object)
1221{
1222 struct kmemleak_scan_area *area;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001223 unsigned long flags;
1224
1225 /*
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001226 * Once the object->lock is acquired, the corresponding memory block
1227 * cannot be freed (the same lock is acquired in delete_object).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001228 */
1229 spin_lock_irqsave(&object->lock, flags);
1230 if (object->flags & OBJECT_NO_SCAN)
1231 goto out;
1232 if (!(object->flags & OBJECT_ALLOCATED))
1233 /* already freed object */
1234 goto out;
Catalin Marinasaf986032009-08-27 14:29:12 +01001235 if (hlist_empty(&object->area_list)) {
1236 void *start = (void *)object->pointer;
1237 void *end = (void *)(object->pointer + object->size);
1238
1239 while (start < end && (object->flags & OBJECT_ALLOCATED) &&
1240 !(object->flags & OBJECT_NO_SCAN)) {
1241 scan_block(start, min(start + MAX_SCAN_SIZE, end),
1242 object, 0);
1243 start += MAX_SCAN_SIZE;
1244
1245 spin_unlock_irqrestore(&object->lock, flags);
1246 cond_resched();
1247 spin_lock_irqsave(&object->lock, flags);
1248 }
1249 } else
Sasha Levinb67bfe02013-02-27 17:06:00 -08001250 hlist_for_each_entry(area, &object->area_list, node)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001251 scan_block((void *)area->start,
1252 (void *)(area->start + area->size),
1253 object, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001254out:
1255 spin_unlock_irqrestore(&object->lock, flags);
1256}
1257
1258/*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001259 * Scan the objects already referenced (gray objects). More objects will be
1260 * referenced and, if there are no memory leaks, all the objects are scanned.
1261 */
1262static void scan_gray_list(void)
1263{
1264 struct kmemleak_object *object, *tmp;
1265
1266 /*
1267 * The list traversal is safe for both tail additions and removals
1268 * from inside the loop. The kmemleak objects cannot be freed from
1269 * outside the loop because their use_count was incremented.
1270 */
1271 object = list_entry(gray_list.next, typeof(*object), gray_list);
1272 while (&object->gray_list != &gray_list) {
1273 cond_resched();
1274
1275 /* may add new objects to the list */
1276 if (!scan_should_stop())
1277 scan_object(object);
1278
1279 tmp = list_entry(object->gray_list.next, typeof(*object),
1280 gray_list);
1281
1282 /* remove the object from the list and release it */
1283 list_del(&object->gray_list);
1284 put_object(object);
1285
1286 object = tmp;
1287 }
1288 WARN_ON(!list_empty(&gray_list));
1289}
1290
1291/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001292 * Scan data sections and all the referenced memory blocks allocated via the
1293 * kernel's standard allocators. This function must be called with the
1294 * scan_mutex held.
1295 */
1296static void kmemleak_scan(void)
1297{
1298 unsigned long flags;
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001299 struct kmemleak_object *object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001300 int i;
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001301 int new_leaks = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001302
Catalin Marinasacf49682009-06-26 17:38:29 +01001303 jiffies_last_scan = jiffies;
1304
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001305 /* prepare the kmemleak_object's */
1306 rcu_read_lock();
1307 list_for_each_entry_rcu(object, &object_list, object_list) {
1308 spin_lock_irqsave(&object->lock, flags);
1309#ifdef DEBUG
1310 /*
1311 * With a few exceptions there should be a maximum of
1312 * 1 reference to any object at this point.
1313 */
1314 if (atomic_read(&object->use_count) > 1) {
Joe Perchesae281062009-06-23 14:40:26 +01001315 pr_debug("object->use_count = %d\n",
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001316 atomic_read(&object->use_count));
1317 dump_object_info(object);
1318 }
1319#endif
1320 /* reset the reference count (whiten the object) */
1321 object->count = 0;
1322 if (color_gray(object) && get_object(object))
1323 list_add_tail(&object->gray_list, &gray_list);
1324
1325 spin_unlock_irqrestore(&object->lock, flags);
1326 }
1327 rcu_read_unlock();
1328
1329 /* data/bss scanning */
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001330 scan_block(_sdata, _edata, NULL, 1);
1331 scan_block(__bss_start, __bss_stop, NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001332
1333#ifdef CONFIG_SMP
1334 /* per-cpu sections scanning */
1335 for_each_possible_cpu(i)
1336 scan_block(__per_cpu_start + per_cpu_offset(i),
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001337 __per_cpu_end + per_cpu_offset(i), NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001338#endif
1339
1340 /*
Laura Abbott029aeff2011-11-15 23:49:09 +00001341 * Struct page scanning for each node.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001342 */
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001343 get_online_mems();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001344 for_each_online_node(i) {
Cody P Schafer108bcc92013-02-22 16:35:23 -08001345 unsigned long start_pfn = node_start_pfn(i);
1346 unsigned long end_pfn = node_end_pfn(i);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001347 unsigned long pfn;
1348
1349 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1350 struct page *page;
1351
1352 if (!pfn_valid(pfn))
1353 continue;
1354 page = pfn_to_page(pfn);
1355 /* only scan if page is in use */
1356 if (page_count(page) == 0)
1357 continue;
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001358 scan_block(page, page + 1, NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001359 }
1360 }
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001361 put_online_mems();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001362
1363 /*
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001364 * Scanning the task stacks (may introduce false negatives).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001365 */
1366 if (kmemleak_stack_scan) {
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001367 struct task_struct *p, *g;
1368
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001369 read_lock(&tasklist_lock);
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001370 do_each_thread(g, p) {
1371 scan_block(task_stack_page(p), task_stack_page(p) +
1372 THREAD_SIZE, NULL, 0);
1373 } while_each_thread(g, p);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001374 read_unlock(&tasklist_lock);
1375 }
1376
1377 /*
1378 * Scan the objects already referenced from the sections scanned
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001379 * above.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001380 */
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001381 scan_gray_list();
Catalin Marinas25873622009-07-07 10:32:58 +01001382
1383 /*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001384 * Check for new or unreferenced objects modified since the previous
1385 * scan and color them gray until the next scan.
Catalin Marinas25873622009-07-07 10:32:58 +01001386 */
1387 rcu_read_lock();
1388 list_for_each_entry_rcu(object, &object_list, object_list) {
1389 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001390 if (color_white(object) && (object->flags & OBJECT_ALLOCATED)
1391 && update_checksum(object) && get_object(object)) {
1392 /* color it gray temporarily */
1393 object->count = object->min_count;
Catalin Marinas25873622009-07-07 10:32:58 +01001394 list_add_tail(&object->gray_list, &gray_list);
1395 }
1396 spin_unlock_irqrestore(&object->lock, flags);
1397 }
1398 rcu_read_unlock();
1399
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001400 /*
1401 * Re-scan the gray list for modified unreferenced objects.
1402 */
1403 scan_gray_list();
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001404
1405 /*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001406 * If scanning was stopped do not report any new unreferenced objects.
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001407 */
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001408 if (scan_should_stop())
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001409 return;
1410
1411 /*
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001412 * Scanning result reporting.
1413 */
1414 rcu_read_lock();
1415 list_for_each_entry_rcu(object, &object_list, object_list) {
1416 spin_lock_irqsave(&object->lock, flags);
1417 if (unreferenced_object(object) &&
1418 !(object->flags & OBJECT_REPORTED)) {
1419 object->flags |= OBJECT_REPORTED;
1420 new_leaks++;
1421 }
1422 spin_unlock_irqrestore(&object->lock, flags);
1423 }
1424 rcu_read_unlock();
1425
Li Zefandc9b3f42014-04-03 14:46:26 -07001426 if (new_leaks) {
1427 kmemleak_found_leaks = true;
1428
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001429 pr_info("%d new suspected memory leaks (see "
1430 "/sys/kernel/debug/kmemleak)\n", new_leaks);
Li Zefandc9b3f42014-04-03 14:46:26 -07001431 }
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001432
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001433}
1434
1435/*
1436 * Thread function performing automatic memory scanning. Unreferenced objects
1437 * at the end of a memory scan are reported but only the first time.
1438 */
1439static int kmemleak_scan_thread(void *arg)
1440{
1441 static int first_run = 1;
1442
Joe Perchesae281062009-06-23 14:40:26 +01001443 pr_info("Automatic memory scanning thread started\n");
Catalin Marinasbf2a76b2009-07-07 10:32:55 +01001444 set_user_nice(current, 10);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001445
1446 /*
1447 * Wait before the first scan to allow the system to fully initialize.
1448 */
1449 if (first_run) {
1450 first_run = 0;
1451 ssleep(SECS_FIRST_SCAN);
1452 }
1453
1454 while (!kthread_should_stop()) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001455 signed long timeout = jiffies_scan_wait;
1456
1457 mutex_lock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001458 kmemleak_scan();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001459 mutex_unlock(&scan_mutex);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001460
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001461 /* wait before the next scan */
1462 while (timeout && !kthread_should_stop())
1463 timeout = schedule_timeout_interruptible(timeout);
1464 }
1465
Joe Perchesae281062009-06-23 14:40:26 +01001466 pr_info("Automatic memory scanning thread ended\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001467
1468 return 0;
1469}
1470
1471/*
1472 * Start the automatic memory scanning thread. This function must be called
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001473 * with the scan_mutex held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001474 */
Luis R. Rodriguez7eb0d5e2009-09-08 17:31:45 +01001475static void start_scan_thread(void)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001476{
1477 if (scan_thread)
1478 return;
1479 scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak");
1480 if (IS_ERR(scan_thread)) {
Joe Perchesae281062009-06-23 14:40:26 +01001481 pr_warning("Failed to create the scan thread\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001482 scan_thread = NULL;
1483 }
1484}
1485
1486/*
1487 * Stop the automatic memory scanning thread. This function must be called
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001488 * with the scan_mutex held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001489 */
Luis R. Rodriguez7eb0d5e2009-09-08 17:31:45 +01001490static void stop_scan_thread(void)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001491{
1492 if (scan_thread) {
1493 kthread_stop(scan_thread);
1494 scan_thread = NULL;
1495 }
1496}
1497
1498/*
1499 * Iterate over the object_list and return the first valid object at or after
1500 * the required position with its use_count incremented. The function triggers
1501 * a memory scanning when the pos argument points to the first position.
1502 */
1503static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
1504{
1505 struct kmemleak_object *object;
1506 loff_t n = *pos;
Catalin Marinasb87324d2009-07-07 10:32:58 +01001507 int err;
1508
1509 err = mutex_lock_interruptible(&scan_mutex);
1510 if (err < 0)
1511 return ERR_PTR(err);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001512
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001513 rcu_read_lock();
1514 list_for_each_entry_rcu(object, &object_list, object_list) {
1515 if (n-- > 0)
1516 continue;
1517 if (get_object(object))
1518 goto out;
1519 }
1520 object = NULL;
1521out:
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001522 return object;
1523}
1524
1525/*
1526 * Return the next object in the object_list. The function decrements the
1527 * use_count of the previous object and increases that of the next one.
1528 */
1529static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1530{
1531 struct kmemleak_object *prev_obj = v;
1532 struct kmemleak_object *next_obj = NULL;
Michael Wang58fac092012-08-17 12:33:34 +08001533 struct kmemleak_object *obj = prev_obj;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001534
1535 ++(*pos);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001536
Michael Wang58fac092012-08-17 12:33:34 +08001537 list_for_each_entry_continue_rcu(obj, &object_list, object_list) {
Catalin Marinas52c3ce42011-04-27 16:44:26 +01001538 if (get_object(obj)) {
1539 next_obj = obj;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001540 break;
Catalin Marinas52c3ce42011-04-27 16:44:26 +01001541 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001542 }
Catalin Marinas288c8572009-07-07 10:32:57 +01001543
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001544 put_object(prev_obj);
1545 return next_obj;
1546}
1547
1548/*
1549 * Decrement the use_count of the last object required, if any.
1550 */
1551static void kmemleak_seq_stop(struct seq_file *seq, void *v)
1552{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001553 if (!IS_ERR(v)) {
1554 /*
1555 * kmemleak_seq_start may return ERR_PTR if the scan_mutex
1556 * waiting was interrupted, so only release it if !IS_ERR.
1557 */
Catalin Marinasf5886c72009-07-29 16:26:57 +01001558 rcu_read_unlock();
Catalin Marinasb87324d2009-07-07 10:32:58 +01001559 mutex_unlock(&scan_mutex);
1560 if (v)
1561 put_object(v);
1562 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001563}
1564
1565/*
1566 * Print the information for an unreferenced object to the seq file.
1567 */
1568static int kmemleak_seq_show(struct seq_file *seq, void *v)
1569{
1570 struct kmemleak_object *object = v;
1571 unsigned long flags;
1572
1573 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas288c8572009-07-07 10:32:57 +01001574 if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object))
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001575 print_unreferenced(seq, object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001576 spin_unlock_irqrestore(&object->lock, flags);
1577 return 0;
1578}
1579
1580static const struct seq_operations kmemleak_seq_ops = {
1581 .start = kmemleak_seq_start,
1582 .next = kmemleak_seq_next,
1583 .stop = kmemleak_seq_stop,
1584 .show = kmemleak_seq_show,
1585};
1586
1587static int kmemleak_open(struct inode *inode, struct file *file)
1588{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001589 return seq_open(file, &kmemleak_seq_ops);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001590}
1591
Catalin Marinas189d84e2009-08-27 14:29:15 +01001592static int dump_str_object_info(const char *str)
1593{
1594 unsigned long flags;
1595 struct kmemleak_object *object;
1596 unsigned long addr;
1597
Abhijit Pawardc053732012-12-18 14:23:27 -08001598 if (kstrtoul(str, 0, &addr))
1599 return -EINVAL;
Catalin Marinas189d84e2009-08-27 14:29:15 +01001600 object = find_and_get_object(addr, 0);
1601 if (!object) {
1602 pr_info("Unknown object at 0x%08lx\n", addr);
1603 return -EINVAL;
1604 }
1605
1606 spin_lock_irqsave(&object->lock, flags);
1607 dump_object_info(object);
1608 spin_unlock_irqrestore(&object->lock, flags);
1609
1610 put_object(object);
1611 return 0;
1612}
1613
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001614/*
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001615 * We use grey instead of black to ensure we can do future scans on the same
1616 * objects. If we did not do future scans these black objects could
1617 * potentially contain references to newly allocated objects in the future and
1618 * we'd end up with false positives.
1619 */
1620static void kmemleak_clear(void)
1621{
1622 struct kmemleak_object *object;
1623 unsigned long flags;
1624
1625 rcu_read_lock();
1626 list_for_each_entry_rcu(object, &object_list, object_list) {
1627 spin_lock_irqsave(&object->lock, flags);
1628 if ((object->flags & OBJECT_REPORTED) &&
1629 unreferenced_object(object))
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -07001630 __paint_it(object, KMEMLEAK_GREY);
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001631 spin_unlock_irqrestore(&object->lock, flags);
1632 }
1633 rcu_read_unlock();
Li Zefandc9b3f42014-04-03 14:46:26 -07001634
1635 kmemleak_found_leaks = false;
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001636}
1637
Li Zefanc89da702014-04-03 14:46:27 -07001638static void __kmemleak_do_cleanup(void);
1639
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001640/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001641 * File write operation to configure kmemleak at run-time. The following
1642 * commands can be written to the /sys/kernel/debug/kmemleak file:
1643 * off - disable kmemleak (irreversible)
1644 * stack=on - enable the task stacks scanning
1645 * stack=off - disable the tasks stacks scanning
1646 * scan=on - start the automatic memory scanning thread
1647 * scan=off - stop the automatic memory scanning thread
1648 * scan=... - set the automatic memory scanning period in seconds (0 to
1649 * disable it)
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001650 * scan - trigger a memory scan
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001651 * clear - mark all current reported unreferenced kmemleak objects as
Li Zefanc89da702014-04-03 14:46:27 -07001652 * grey to ignore printing them, or free all kmemleak objects
1653 * if kmemleak has been disabled.
Catalin Marinas189d84e2009-08-27 14:29:15 +01001654 * dump=... - dump information about the object found at the given address
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001655 */
1656static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
1657 size_t size, loff_t *ppos)
1658{
1659 char buf[64];
1660 int buf_size;
Catalin Marinasb87324d2009-07-07 10:32:58 +01001661 int ret;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001662
1663 buf_size = min(size, (sizeof(buf) - 1));
1664 if (strncpy_from_user(buf, user_buf, buf_size) < 0)
1665 return -EFAULT;
1666 buf[buf_size] = 0;
1667
Catalin Marinasb87324d2009-07-07 10:32:58 +01001668 ret = mutex_lock_interruptible(&scan_mutex);
1669 if (ret < 0)
1670 return ret;
1671
Li Zefanc89da702014-04-03 14:46:27 -07001672 if (strncmp(buf, "clear", 5) == 0) {
Li Zefan8910ae82014-04-03 14:46:29 -07001673 if (kmemleak_enabled)
Li Zefanc89da702014-04-03 14:46:27 -07001674 kmemleak_clear();
1675 else
1676 __kmemleak_do_cleanup();
1677 goto out;
1678 }
1679
Li Zefan8910ae82014-04-03 14:46:29 -07001680 if (!kmemleak_enabled) {
Li Zefanc89da702014-04-03 14:46:27 -07001681 ret = -EBUSY;
1682 goto out;
1683 }
1684
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001685 if (strncmp(buf, "off", 3) == 0)
1686 kmemleak_disable();
1687 else if (strncmp(buf, "stack=on", 8) == 0)
1688 kmemleak_stack_scan = 1;
1689 else if (strncmp(buf, "stack=off", 9) == 0)
1690 kmemleak_stack_scan = 0;
1691 else if (strncmp(buf, "scan=on", 7) == 0)
1692 start_scan_thread();
1693 else if (strncmp(buf, "scan=off", 8) == 0)
1694 stop_scan_thread();
1695 else if (strncmp(buf, "scan=", 5) == 0) {
1696 unsigned long secs;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001697
Jingoo Han3dbb95f2013-09-11 14:20:25 -07001698 ret = kstrtoul(buf + 5, 0, &secs);
Catalin Marinasb87324d2009-07-07 10:32:58 +01001699 if (ret < 0)
1700 goto out;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001701 stop_scan_thread();
1702 if (secs) {
1703 jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
1704 start_scan_thread();
1705 }
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001706 } else if (strncmp(buf, "scan", 4) == 0)
1707 kmemleak_scan();
Catalin Marinas189d84e2009-08-27 14:29:15 +01001708 else if (strncmp(buf, "dump=", 5) == 0)
1709 ret = dump_str_object_info(buf + 5);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001710 else
Catalin Marinasb87324d2009-07-07 10:32:58 +01001711 ret = -EINVAL;
1712
1713out:
1714 mutex_unlock(&scan_mutex);
1715 if (ret < 0)
1716 return ret;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001717
1718 /* ignore the rest of the buffer, only one command at a time */
1719 *ppos += size;
1720 return size;
1721}
1722
1723static const struct file_operations kmemleak_fops = {
1724 .owner = THIS_MODULE,
1725 .open = kmemleak_open,
1726 .read = seq_read,
1727 .write = kmemleak_write,
1728 .llseek = seq_lseek,
Li Zefan5f3bf192014-04-03 14:46:28 -07001729 .release = seq_release,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001730};
1731
Li Zefanc89da702014-04-03 14:46:27 -07001732static void __kmemleak_do_cleanup(void)
1733{
1734 struct kmemleak_object *object;
1735
1736 rcu_read_lock();
1737 list_for_each_entry_rcu(object, &object_list, object_list)
1738 delete_object_full(object->pointer);
1739 rcu_read_unlock();
1740}
1741
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001742/*
Catalin Marinas74341702011-09-29 11:50:07 +01001743 * Stop the memory scanning thread and free the kmemleak internal objects if
1744 * no previous scan thread (otherwise, kmemleak may still have some useful
1745 * information on memory leaks).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001746 */
Catalin Marinas179a8102009-09-07 10:14:42 +01001747static void kmemleak_do_cleanup(struct work_struct *work)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001748{
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001749 mutex_lock(&scan_mutex);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001750 stop_scan_thread();
1751
Li Zefanc89da702014-04-03 14:46:27 -07001752 if (!kmemleak_found_leaks)
1753 __kmemleak_do_cleanup();
1754 else
1755 pr_info("Kmemleak disabled without freeing internal data. "
1756 "Reclaim the memory with \"echo clear > /sys/kernel/debug/kmemleak\"\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001757 mutex_unlock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001758}
1759
Catalin Marinas179a8102009-09-07 10:14:42 +01001760static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001761
1762/*
1763 * Disable kmemleak. No memory allocation/freeing will be traced once this
1764 * function is called. Disabling kmemleak is an irreversible operation.
1765 */
1766static void kmemleak_disable(void)
1767{
1768 /* atomically check whether it was already invoked */
Li Zefan8910ae82014-04-03 14:46:29 -07001769 if (cmpxchg(&kmemleak_error, 0, 1))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001770 return;
1771
1772 /* stop any memory operation tracing */
Li Zefan8910ae82014-04-03 14:46:29 -07001773 kmemleak_enabled = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001774
1775 /* check whether it is too early for a kernel thread */
Li Zefan8910ae82014-04-03 14:46:29 -07001776 if (kmemleak_initialized)
Catalin Marinas179a8102009-09-07 10:14:42 +01001777 schedule_work(&cleanup_work);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001778
1779 pr_info("Kernel memory leak detector disabled\n");
1780}
1781
1782/*
1783 * Allow boot-time kmemleak disabling (enabled by default).
1784 */
1785static int kmemleak_boot_config(char *str)
1786{
1787 if (!str)
1788 return -EINVAL;
1789 if (strcmp(str, "off") == 0)
1790 kmemleak_disable();
Jason Baronab0155a2010-07-19 11:54:17 +01001791 else if (strcmp(str, "on") == 0)
1792 kmemleak_skip_disable = 1;
1793 else
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001794 return -EINVAL;
1795 return 0;
1796}
1797early_param("kmemleak", kmemleak_boot_config);
1798
Catalin Marinas5f790202011-09-28 12:17:03 +01001799static void __init print_log_trace(struct early_log *log)
1800{
1801 struct stack_trace trace;
1802
1803 trace.nr_entries = log->trace_len;
1804 trace.entries = log->trace;
1805
1806 pr_notice("Early log backtrace:\n");
1807 print_stack_trace(&trace, 2);
1808}
1809
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001810/*
Catalin Marinas20301172009-06-17 18:29:04 +01001811 * Kmemleak initialization.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001812 */
1813void __init kmemleak_init(void)
1814{
1815 int i;
1816 unsigned long flags;
1817
Jason Baronab0155a2010-07-19 11:54:17 +01001818#ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF
1819 if (!kmemleak_skip_disable) {
Catalin Marinas3551a922014-05-09 15:36:59 -07001820 kmemleak_early_log = 0;
Jason Baronab0155a2010-07-19 11:54:17 +01001821 kmemleak_disable();
1822 return;
1823 }
1824#endif
1825
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001826 jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE);
1827 jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000);
1828
1829 object_cache = KMEM_CACHE(kmemleak_object, SLAB_NOLEAKTRACE);
1830 scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001831
Catalin Marinasb6693002011-09-28 17:22:56 +01001832 if (crt_early_log >= ARRAY_SIZE(early_log))
1833 pr_warning("Early log buffer exceeded (%d), please increase "
1834 "DEBUG_KMEMLEAK_EARLY_LOG_SIZE\n", crt_early_log);
1835
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001836 /* the kernel is still in UP mode, so disabling the IRQs is enough */
1837 local_irq_save(flags);
Catalin Marinas3551a922014-05-09 15:36:59 -07001838 kmemleak_early_log = 0;
Li Zefan8910ae82014-04-03 14:46:29 -07001839 if (kmemleak_error) {
Catalin Marinasb6693002011-09-28 17:22:56 +01001840 local_irq_restore(flags);
1841 return;
1842 } else
Li Zefan8910ae82014-04-03 14:46:29 -07001843 kmemleak_enabled = 1;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001844 local_irq_restore(flags);
1845
1846 /*
1847 * This is the point where tracking allocations is safe. Automatic
1848 * scanning is started during the late initcall. Add the early logged
1849 * callbacks to the kmemleak infrastructure.
1850 */
1851 for (i = 0; i < crt_early_log; i++) {
1852 struct early_log *log = &early_log[i];
1853
1854 switch (log->op_type) {
1855 case KMEMLEAK_ALLOC:
Catalin Marinasfd678962009-08-27 14:29:17 +01001856 early_alloc(log);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001857 break;
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001858 case KMEMLEAK_ALLOC_PERCPU:
1859 early_alloc_percpu(log);
1860 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001861 case KMEMLEAK_FREE:
1862 kmemleak_free(log->ptr);
1863 break;
Catalin Marinas53238a62009-07-07 10:33:00 +01001864 case KMEMLEAK_FREE_PART:
1865 kmemleak_free_part(log->ptr, log->size);
1866 break;
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001867 case KMEMLEAK_FREE_PERCPU:
1868 kmemleak_free_percpu(log->ptr);
1869 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001870 case KMEMLEAK_NOT_LEAK:
1871 kmemleak_not_leak(log->ptr);
1872 break;
1873 case KMEMLEAK_IGNORE:
1874 kmemleak_ignore(log->ptr);
1875 break;
1876 case KMEMLEAK_SCAN_AREA:
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001877 kmemleak_scan_area(log->ptr, log->size, GFP_KERNEL);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001878 break;
1879 case KMEMLEAK_NO_SCAN:
1880 kmemleak_no_scan(log->ptr);
1881 break;
1882 default:
Catalin Marinas5f790202011-09-28 12:17:03 +01001883 kmemleak_warn("Unknown early log operation: %d\n",
1884 log->op_type);
1885 }
1886
Li Zefan8910ae82014-04-03 14:46:29 -07001887 if (kmemleak_warning) {
Catalin Marinas5f790202011-09-28 12:17:03 +01001888 print_log_trace(log);
Li Zefan8910ae82014-04-03 14:46:29 -07001889 kmemleak_warning = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001890 }
1891 }
1892}
1893
1894/*
1895 * Late initialization function.
1896 */
1897static int __init kmemleak_late_init(void)
1898{
1899 struct dentry *dentry;
1900
Li Zefan8910ae82014-04-03 14:46:29 -07001901 kmemleak_initialized = 1;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001902
Li Zefan8910ae82014-04-03 14:46:29 -07001903 if (kmemleak_error) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001904 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001905 * Some error occurred and kmemleak was disabled. There is a
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001906 * small chance that kmemleak_disable() was called immediately
1907 * after setting kmemleak_initialized and we may end up with
1908 * two clean-up threads but serialized by scan_mutex.
1909 */
Catalin Marinas179a8102009-09-07 10:14:42 +01001910 schedule_work(&cleanup_work);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001911 return -ENOMEM;
1912 }
1913
1914 dentry = debugfs_create_file("kmemleak", S_IRUGO, NULL, NULL,
1915 &kmemleak_fops);
1916 if (!dentry)
Joe Perchesae281062009-06-23 14:40:26 +01001917 pr_warning("Failed to create the debugfs kmemleak file\n");
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001918 mutex_lock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001919 start_scan_thread();
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001920 mutex_unlock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001921
1922 pr_info("Kernel memory leak detector initialized\n");
1923
1924 return 0;
1925}
1926late_initcall(kmemleak_late_init);