blob: 41df5b8efd25ff150766dbf9b62c6262d54d6ed8 [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 */
Vladimir Davydov8f4fc072015-05-14 15:16:55 -0700118#define gfp_kmemleak_mask(gfp) (((gfp) & (GFP_KERNEL | GFP_ATOMIC | \
119 __GFP_NOACCOUNT)) | \
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000120 __GFP_NORETRY | __GFP_NOMEMALLOC | \
121 __GFP_NOWARN)
Catalin Marinas216c04b2009-06-17 18:29:02 +0100122
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100123/* scanning area inside a memory block */
124struct kmemleak_scan_area {
125 struct hlist_node node;
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000126 unsigned long start;
127 size_t size;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100128};
129
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700130#define KMEMLEAK_GREY 0
131#define KMEMLEAK_BLACK -1
132
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100133/*
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
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700137 * rb_node are already protected by the corresponding locks or mutex (see
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100138 * the notes on locking above). These objects are reference-counted
139 * (use_count) and freed using the RCU mechanism.
140 */
141struct 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;
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700146 struct rb_node rb_node;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100147 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;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000156 /* checksum for detecting modified objects */
157 u32 checksum;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100158 /* 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
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100174/* 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
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100183/* the list of all allocated objects */
184static LIST_HEAD(object_list);
185/* the list of gray-colored objects (see color_gray comment below) */
186static LIST_HEAD(gray_list);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700187/* search tree for object boundaries */
188static struct rb_root object_tree_root = RB_ROOT;
189/* rw_lock protecting the access to object_list and object_tree_root */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100190static DEFINE_RWLOCK(kmemleak_lock);
191
192/* allocation caches for kmemleak internal data */
193static struct kmem_cache *object_cache;
194static struct kmem_cache *scan_area_cache;
195
196/* set if tracing memory operations is enabled */
Li Zefan8910ae82014-04-03 14:46:29 -0700197static int kmemleak_enabled;
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -0700198/* same as above but only for the kmemleak_free() callback */
199static int kmemleak_free_enabled;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100200/* set in the late_initcall if there were no errors */
Li Zefan8910ae82014-04-03 14:46:29 -0700201static int kmemleak_initialized;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100202/* enables or disables early logging of the memory operations */
Li Zefan8910ae82014-04-03 14:46:29 -0700203static int kmemleak_early_log = 1;
Catalin Marinas5f790202011-09-28 12:17:03 +0100204/* set if a kmemleak warning was issued */
Li Zefan8910ae82014-04-03 14:46:29 -0700205static int kmemleak_warning;
Catalin Marinas5f790202011-09-28 12:17:03 +0100206/* set if a fatal kmemleak error has occurred */
Li Zefan8910ae82014-04-03 14:46:29 -0700207static int kmemleak_error;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100208
209/* minimum and maximum address that may be valid pointers */
210static unsigned long min_addr = ULONG_MAX;
211static unsigned long max_addr;
212
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100213static struct task_struct *scan_thread;
Catalin Marinasacf49682009-06-26 17:38:29 +0100214/* used to avoid reporting of recently allocated objects */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100215static unsigned long jiffies_min_age;
Catalin Marinasacf49682009-06-26 17:38:29 +0100216static unsigned long jiffies_last_scan;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100217/* delay between automatic memory scannings */
218static signed long jiffies_scan_wait;
219/* enables or disables the task stacks scanning */
Catalin Marinase0a2a162009-06-26 17:38:25 +0100220static int kmemleak_stack_scan = 1;
Catalin Marinas4698c1f2009-06-26 17:38:27 +0100221/* protects the memory scanning, parameters and debug/kmemleak file access */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100222static DEFINE_MUTEX(scan_mutex);
Jason Baronab0155a2010-07-19 11:54:17 +0100223/* setting kmemleak=on, will set this var, skipping the disable */
224static int kmemleak_skip_disable;
Li Zefandc9b3f42014-04-03 14:46:26 -0700225/* If there are leaks that can be reported */
226static bool kmemleak_found_leaks;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100227
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100228/*
Catalin Marinas20301172009-06-17 18:29:04 +0100229 * Early object allocation/freeing logging. Kmemleak is initialized after the
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100230 * kernel allocator. However, both the kernel allocator and kmemleak may
Catalin Marinas20301172009-06-17 18:29:04 +0100231 * allocate memory blocks which need to be tracked. Kmemleak defines an
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100232 * arbitrary buffer to hold the allocation/freeing information before it is
233 * fully initialized.
234 */
235
236/* kmemleak operation type for early logging */
237enum {
238 KMEMLEAK_ALLOC,
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100239 KMEMLEAK_ALLOC_PERCPU,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100240 KMEMLEAK_FREE,
Catalin Marinas53238a62009-07-07 10:33:00 +0100241 KMEMLEAK_FREE_PART,
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100242 KMEMLEAK_FREE_PERCPU,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100243 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 */
253struct 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 */
Catalin Marinasfd678962009-08-27 14:29:17 +0100258 unsigned long trace[MAX_TRACE]; /* stack trace */
259 unsigned int trace_len; /* stack trace length */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100260};
261
262/* early logging buffer and current position */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100263static struct early_log
264 early_log[CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE] __initdata;
265static int crt_early_log __initdata;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100266
267static void kmemleak_disable(void);
268
269/*
270 * Print a warning and dump the stack trace.
271 */
Catalin Marinas5f790202011-09-28 12:17:03 +0100272#define kmemleak_warn(x...) do { \
273 pr_warning(x); \
274 dump_stack(); \
Li Zefan8910ae82014-04-03 14:46:29 -0700275 kmemleak_warning = 1; \
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100276} while (0)
277
278/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300279 * Macro invoked when a serious kmemleak condition occurred and cannot be
Catalin Marinas20301172009-06-17 18:29:04 +0100280 * recovered from. Kmemleak will be disabled and further allocation/freeing
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100281 * tracing no longer available.
282 */
Catalin Marinas000814f2009-06-17 18:29:03 +0100283#define kmemleak_stop(x...) do { \
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100284 kmemleak_warn(x); \
285 kmemleak_disable(); \
286} while (0)
287
288/*
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100289 * 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 */
294static 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/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100318 * 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 */
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100327static bool color_white(const struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100328{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700329 return object->count != KMEMLEAK_BLACK &&
330 object->count < object->min_count;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100331}
332
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100333static bool color_gray(const struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100334{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700335 return object->min_count != KMEMLEAK_BLACK &&
336 object->count >= object->min_count;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100337}
338
339/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100340 * 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 */
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100344static bool unreferenced_object(struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100345{
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000346 return (color_white(object) && object->flags & OBJECT_ALLOCATED) &&
Catalin Marinasacf49682009-06-26 17:38:29 +0100347 time_before_eq(object->jiffies + jiffies_min_age,
348 jiffies_last_scan);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100349}
350
351/*
Catalin Marinasbab4a342009-06-26 17:38:26 +0100352 * Printing of the unreferenced objects information to the seq file. The
353 * print_unreferenced function must be called with the object->lock held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100354 */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100355static void print_unreferenced(struct seq_file *seq,
356 struct kmemleak_object *object)
357{
358 int i;
Catalin Marinasfefdd332009-10-28 13:33:12 +0000359 unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100360
Catalin Marinasbab4a342009-06-26 17:38:26 +0100361 seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
362 object->pointer, object->size);
Catalin Marinasfefdd332009-10-28 13:33:12 +0000363 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);
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100366 hex_dump_object(seq, object);
Catalin Marinasbab4a342009-06-26 17:38:26 +0100367 seq_printf(seq, " backtrace:\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100368
369 for (i = 0; i < object->trace_len; i++) {
370 void *ptr = (void *)object->trace[i];
Catalin Marinasbab4a342009-06-26 17:38:26 +0100371 seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100372 }
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 */
380static 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
Joe Perchesae281062009-06-23 14:40:26 +0100387 pr_notice("Object 0x%08lx (size %zu):\n",
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700388 object->pointer, object->size);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100389 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);
Catalin Marinas189d84e2009-08-27 14:29:15 +0100393 pr_notice(" flags = 0x%lx\n", object->flags);
Jianpeng Maaae0ad72014-06-06 14:38:16 -0700394 pr_notice(" checksum = %u\n", object->checksum);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100395 pr_notice(" backtrace:\n");
396 print_stack_trace(&trace, 4);
397}
398
399/*
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700400 * Look-up a memory block metadata (kmemleak_object) in the object search
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100401 * 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 */
405static struct kmemleak_object *lookup_object(unsigned long ptr, int alias)
406{
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700407 struct rb_node *rb = object_tree_root.rb_node;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100408
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700409 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 {
Catalin Marinas5f790202011-09-28 12:17:03 +0100419 kmemleak_warn("Found object by alias at 0x%08lx\n",
420 ptr);
Catalin Marinasa7686a42010-07-19 11:54:16 +0100421 dump_object_info(object);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700422 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100423 }
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700424 }
425 return NULL;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100426}
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 */
434static 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 */
442static void free_object_rcu(struct rcu_head *rcu)
443{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800444 struct hlist_node *tmp;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100445 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 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800453 hlist_for_each_entry_safe(area, tmp, &object->area_list, node) {
454 hlist_del(&area->node);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100455 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 */
467static 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/*
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700479 * Look up an object in the object search tree and increase its use_count.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100480 */
481static 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/*
Catalin Marinasfd678962009-08-27 14:29:17 +0100501 * Save stack trace to the given array of MAX_TRACE size.
502 */
503static 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/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100517 * 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 */
Catalin Marinasfd678962009-08-27 14:29:17 +0100520static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
521 int min_count, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100522{
523 unsigned long flags;
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700524 struct kmemleak_object *object, *parent;
525 struct rb_node **link, *rb_parent;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100526
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000527 object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100528 if (!object) {
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000529 pr_warning("Cannot allocate a kmemleak_object structure\n");
530 kmemleak_disable();
Catalin Marinasfd678962009-08-27 14:29:17 +0100531 return NULL;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100532 }
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);
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000539 object->flags = OBJECT_ALLOCATED;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100540 object->pointer = ptr;
541 object->size = size;
542 object->min_count = min_count;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000543 object->count = 0; /* white color initially */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100544 object->jiffies = jiffies;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000545 object->checksum = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100546
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 */
Catalin Marinasfd678962009-08-27 14:29:17 +0100566 object->trace_len = __save_stack_trace(object->trace);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100567
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100568 write_lock_irqsave(&kmemleak_lock, flags);
Luis R. Rodriguez0580a182009-09-08 17:32:34 +0100569
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100570 min_addr = min(min_addr, ptr);
571 max_addr = max(max_addr, ptr + size);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700572 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 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100592 }
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700593 rb_link_node(&object->rb_node, rb_parent, link);
594 rb_insert_color(&object->rb_node, &object_tree_root);
595
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100596 list_add_tail_rcu(&object->object_list, &object_list);
597out:
598 write_unlock_irqrestore(&kmemleak_lock, flags);
Catalin Marinasfd678962009-08-27 14:29:17 +0100599 return object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100600}
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 */
Catalin Marinas53238a62009-07-07 10:33:00 +0100606static void __delete_object(struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100607{
608 unsigned long flags;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100609
610 write_lock_irqsave(&kmemleak_lock, flags);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700611 rb_erase(&object->rb_node, &object_tree_root);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100612 list_del_rcu(&object->object_list);
613 write_unlock_irqrestore(&kmemleak_lock, flags);
614
615 WARN_ON(!(object->flags & OBJECT_ALLOCATED));
Catalin Marinas53238a62009-07-07 10:33:00 +0100616 WARN_ON(atomic_read(&object->use_count) < 2);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100617
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);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100623 object->flags &= ~OBJECT_ALLOCATED;
624 spin_unlock_irqrestore(&object->lock, flags);
625 put_object(object);
626}
627
628/*
Catalin Marinas53238a62009-07-07 10:33:00 +0100629 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
630 * delete it.
631 */
632static 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 */
653static 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}
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700686
687static 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
694static 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
703static 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
Catalin Marinas53238a62009-07-07 10:33:00 +0100719/*
Holger Hans Peter Freyther145b64b2010-07-22 19:54:13 +0800720 * Mark an object permanently as gray-colored so that it can no longer be
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100721 * reported as a leak. This is used in general to mark a false positive.
722 */
723static void make_gray_object(unsigned long ptr)
724{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700725 paint_ptr(ptr, KMEMLEAK_GREY);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100726}
727
728/*
729 * Mark the object as black-colored so that it is ignored from scans and
730 * reporting.
731 */
732static void make_black_object(unsigned long ptr)
733{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700734 paint_ptr(ptr, KMEMLEAK_BLACK);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100735}
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 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000741static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100742{
743 unsigned long flags;
744 struct kmemleak_object *object;
745 struct kmemleak_scan_area *area;
746
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000747 object = find_and_get_object(ptr, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100748 if (!object) {
Joe Perchesae281062009-06-23 14:40:26 +0100749 kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n",
750 ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100751 return;
752 }
753
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000754 area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100755 if (!area) {
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000756 pr_warning("Cannot allocate a scan area\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100757 goto out;
758 }
759
760 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas7f88f882013-11-12 15:07:45 -0800761 if (size == SIZE_MAX) {
762 size = object->pointer + object->size - ptr;
763 } else if (ptr + size > object->pointer + object->size) {
Joe Perchesae281062009-06-23 14:40:26 +0100764 kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100765 dump_object_info(object);
766 kmem_cache_free(scan_area_cache, area);
767 goto out_unlock;
768 }
769
770 INIT_HLIST_NODE(&area->node);
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000771 area->start = ptr;
772 area->size = size;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100773
774 hlist_add_head(&area->node, &object->area_list);
775out_unlock:
776 spin_unlock_irqrestore(&object->lock, flags);
777out:
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 */
786static 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) {
Joe Perchesae281062009-06-23 14:40:26 +0100793 kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100794 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 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100807static void __init log_early(int op_type, const void *ptr, size_t size,
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000808 int min_count)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100809{
810 unsigned long flags;
811 struct early_log *log;
812
Li Zefan8910ae82014-04-03 14:46:29 -0700813 if (kmemleak_error) {
Catalin Marinasb6693002011-09-28 17:22:56 +0100814 /* kmemleak stopped recording, just count the requests */
815 crt_early_log++;
816 return;
817 }
818
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100819 if (crt_early_log >= ARRAY_SIZE(early_log)) {
Catalin Marinasa9d90582009-06-25 10:16:11 +0100820 kmemleak_disable();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100821 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;
Catalin Marinas5f790202011-09-28 12:17:03 +0100834 log->trace_len = __save_stack_trace(log->trace);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100835 crt_early_log++;
836 local_irq_restore(flags);
837}
838
839/*
Catalin Marinasfd678962009-08-27 14:29:17 +0100840 * Log an early allocated block and populate the stack trace.
841 */
842static void early_alloc(struct early_log *log)
843{
844 struct kmemleak_object *object;
845 unsigned long flags;
846 int i;
847
Li Zefan8910ae82014-04-03 14:46:29 -0700848 if (!kmemleak_enabled || !log->ptr || IS_ERR(log->ptr))
Catalin Marinasfd678962009-08-27 14:29:17 +0100849 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,
Tetsuo Handac1bcd6b2009-10-09 10:39:24 +0100856 log->min_count, GFP_ATOMIC);
Catalin Marinas0d5d1aa2009-10-09 10:30:34 +0100857 if (!object)
858 goto out;
Catalin Marinasfd678962009-08-27 14:29:17 +0100859 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);
Catalin Marinas0d5d1aa2009-10-09 10:30:34 +0100864out:
Catalin Marinasfd678962009-08-27 14:29:17 +0100865 rcu_read_unlock();
866}
867
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100868/*
869 * Log an early allocated block and populate the stack trace.
870 */
871static 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
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100882/**
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.).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100895 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100896void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count,
897 gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100898{
899 pr_debug("%s(0x%p, %zu, %d)\n", __func__, ptr, size, min_count);
900
Li Zefan8910ae82014-04-03 14:46:29 -0700901 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100902 create_object((unsigned long)ptr, size, min_count, gfp);
Li Zefan8910ae82014-04-03 14:46:29 -0700903 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000904 log_early(KMEMLEAK_ALLOC, ptr, size, min_count);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100905}
906EXPORT_SYMBOL_GPL(kmemleak_alloc);
907
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100908/**
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100909 * 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 */
917void __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 */
Li Zefan8910ae82014-04-03 14:46:29 -0700927 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100928 for_each_possible_cpu(cpu)
929 create_object((unsigned long)per_cpu_ptr(ptr, cpu),
930 size, 0, GFP_KERNEL);
Li Zefan8910ae82014-04-03 14:46:29 -0700931 else if (kmemleak_early_log)
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100932 log_early(KMEMLEAK_ALLOC_PERCPU, ptr, size, 0);
933}
934EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
935
936/**
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100937 * 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.).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100942 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100943void __ref kmemleak_free(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100944{
945 pr_debug("%s(0x%p)\n", __func__, ptr);
946
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -0700947 if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas53238a62009-07-07 10:33:00 +0100948 delete_object_full((unsigned long)ptr);
Li Zefan8910ae82014-04-03 14:46:29 -0700949 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000950 log_early(KMEMLEAK_FREE, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100951}
952EXPORT_SYMBOL_GPL(kmemleak_free);
953
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100954/**
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).
Catalin Marinas53238a62009-07-07 10:33:00 +0100962 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100963void __ref kmemleak_free_part(const void *ptr, size_t size)
Catalin Marinas53238a62009-07-07 10:33:00 +0100964{
965 pr_debug("%s(0x%p)\n", __func__, ptr);
966
Li Zefan8910ae82014-04-03 14:46:29 -0700967 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas53238a62009-07-07 10:33:00 +0100968 delete_object_part((unsigned long)ptr, size);
Li Zefan8910ae82014-04-03 14:46:29 -0700969 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000970 log_early(KMEMLEAK_FREE_PART, ptr, size, 0);
Catalin Marinas53238a62009-07-07 10:33:00 +0100971}
972EXPORT_SYMBOL_GPL(kmemleak_free_part);
973
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100974/**
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100975 * 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 */
981void __ref kmemleak_free_percpu(const void __percpu *ptr)
982{
983 unsigned int cpu;
984
985 pr_debug("%s(0x%p)\n", __func__, ptr);
986
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -0700987 if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100988 for_each_possible_cpu(cpu)
989 delete_object_full((unsigned long)per_cpu_ptr(ptr,
990 cpu));
Li Zefan8910ae82014-04-03 14:46:29 -0700991 else if (kmemleak_early_log)
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100992 log_early(KMEMLEAK_FREE_PERCPU, ptr, 0, 0);
993}
994EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
995
996/**
Catalin Marinasffe2c742014-06-06 14:38:17 -0700997 * 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 */
1003void __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}
1028EXPORT_SYMBOL(kmemleak_update_trace);
1029
1030/**
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001031 * 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.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001036 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001037void __ref kmemleak_not_leak(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001038{
1039 pr_debug("%s(0x%p)\n", __func__, ptr);
1040
Li Zefan8910ae82014-04-03 14:46:29 -07001041 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001042 make_gray_object((unsigned long)ptr);
Li Zefan8910ae82014-04-03 14:46:29 -07001043 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001044 log_early(KMEMLEAK_NOT_LEAK, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001045}
1046EXPORT_SYMBOL(kmemleak_not_leak);
1047
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001048/**
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.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001056 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001057void __ref kmemleak_ignore(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001058{
1059 pr_debug("%s(0x%p)\n", __func__, ptr);
1060
Li Zefan8910ae82014-04-03 14:46:29 -07001061 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001062 make_black_object((unsigned long)ptr);
Li Zefan8910ae82014-04-03 14:46:29 -07001063 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001064 log_early(KMEMLEAK_IGNORE, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001065}
1066EXPORT_SYMBOL(kmemleak_ignore);
1067
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001068/**
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.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001078 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001079void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001080{
1081 pr_debug("%s(0x%p)\n", __func__, ptr);
1082
Li Zefan8910ae82014-04-03 14:46:29 -07001083 if (kmemleak_enabled && ptr && size && !IS_ERR(ptr))
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001084 add_scan_area((unsigned long)ptr, size, gfp);
Li Zefan8910ae82014-04-03 14:46:29 -07001085 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001086 log_early(KMEMLEAK_SCAN_AREA, ptr, size, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001087}
1088EXPORT_SYMBOL(kmemleak_scan_area);
1089
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001090/**
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.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001098 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001099void __ref kmemleak_no_scan(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001100{
1101 pr_debug("%s(0x%p)\n", __func__, ptr);
1102
Li Zefan8910ae82014-04-03 14:46:29 -07001103 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001104 object_no_scan((unsigned long)ptr);
Li Zefan8910ae82014-04-03 14:46:29 -07001105 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001106 log_early(KMEMLEAK_NO_SCAN, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001107}
1108EXPORT_SYMBOL(kmemleak_no_scan);
1109
1110/*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001111 * Update an object's checksum and return true if it was modified.
1112 */
1113static 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
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001120 kasan_disable_current();
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001121 object->checksum = crc32(0, (void *)object->pointer, object->size);
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001122 kasan_enable_current();
1123
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001124 return object->checksum != old_csum;
1125}
1126
1127/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001128 * Memory scanning is a long process and it needs to be interruptable. This
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001129 * function checks whether such interrupt condition occurred.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001130 */
1131static int scan_should_stop(void)
1132{
Li Zefan8910ae82014-04-03 14:46:29 -07001133 if (!kmemleak_enabled)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001134 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 */
1152static void scan_block(void *_start, void *_end,
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001153 struct kmemleak_object *scanned, int allow_resched)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001154{
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++) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001160 struct kmemleak_object *object;
Pekka Enberg8e019362009-08-27 14:50:00 +01001161 unsigned long flags;
1162 unsigned long pointer;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001163
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001164 if (allow_resched)
1165 cond_resched();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001166 if (scan_should_stop())
1167 break;
1168
Pekka Enberg8e019362009-08-27 14:50:00 +01001169 /* don't scan uninitialized memory */
1170 if (!kmemcheck_is_obj_initialized((unsigned long)ptr,
1171 BYTES_PER_POINTER))
1172 continue;
1173
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001174 kasan_disable_current();
Pekka Enberg8e019362009-08-27 14:50:00 +01001175 pointer = *ptr;
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001176 kasan_enable_current();
Pekka Enberg8e019362009-08-27 14:50:00 +01001177
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001178 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++;
Catalin Marinas0587da42009-10-28 13:33:11 +00001208 if (color_gray(object)) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001209 list_add_tail(&object->gray_list, &gray_list);
Catalin Marinas0587da42009-10-28 13:33:11 +00001210 spin_unlock_irqrestore(&object->lock, flags);
1211 continue;
1212 }
1213
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001214 spin_unlock_irqrestore(&object->lock, flags);
Catalin Marinas0587da42009-10-28 13:33:11 +00001215 put_object(object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001216 }
1217}
1218
1219/*
1220 * Scan a memory block corresponding to a kmemleak_object. A condition is
1221 * that object->use_count >= 1.
1222 */
1223static void scan_object(struct kmemleak_object *object)
1224{
1225 struct kmemleak_scan_area *area;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001226 unsigned long flags;
1227
1228 /*
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001229 * Once the object->lock is acquired, the corresponding memory block
1230 * cannot be freed (the same lock is acquired in delete_object).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001231 */
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;
Catalin Marinasaf986032009-08-27 14:29:12 +01001238 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
Sasha Levinb67bfe02013-02-27 17:06:00 -08001253 hlist_for_each_entry(area, &object->area_list, node)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001254 scan_block((void *)area->start,
1255 (void *)(area->start + area->size),
1256 object, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001257out:
1258 spin_unlock_irqrestore(&object->lock, flags);
1259}
1260
1261/*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001262 * 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 */
1265static 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/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001295 * 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 */
1299static void kmemleak_scan(void)
1300{
1301 unsigned long flags;
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001302 struct kmemleak_object *object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001303 int i;
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001304 int new_leaks = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001305
Catalin Marinasacf49682009-06-26 17:38:29 +01001306 jiffies_last_scan = jiffies;
1307
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001308 /* 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) {
Joe Perchesae281062009-06-23 14:40:26 +01001318 pr_debug("object->use_count = %d\n",
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001319 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 */
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001333 scan_block(_sdata, _edata, NULL, 1);
1334 scan_block(__bss_start, __bss_stop, NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001335
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),
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001340 __per_cpu_end + per_cpu_offset(i), NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001341#endif
1342
1343 /*
Laura Abbott029aeff2011-11-15 23:49:09 +00001344 * Struct page scanning for each node.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001345 */
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001346 get_online_mems();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001347 for_each_online_node(i) {
Cody P Schafer108bcc92013-02-22 16:35:23 -08001348 unsigned long start_pfn = node_start_pfn(i);
1349 unsigned long end_pfn = node_end_pfn(i);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001350 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;
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001361 scan_block(page, page + 1, NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001362 }
1363 }
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001364 put_online_mems();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001365
1366 /*
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001367 * Scanning the task stacks (may introduce false negatives).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001368 */
1369 if (kmemleak_stack_scan) {
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001370 struct task_struct *p, *g;
1371
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001372 read_lock(&tasklist_lock);
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001373 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);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001377 read_unlock(&tasklist_lock);
1378 }
1379
1380 /*
1381 * Scan the objects already referenced from the sections scanned
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001382 * above.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001383 */
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001384 scan_gray_list();
Catalin Marinas25873622009-07-07 10:32:58 +01001385
1386 /*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001387 * Check for new or unreferenced objects modified since the previous
1388 * scan and color them gray until the next scan.
Catalin Marinas25873622009-07-07 10:32:58 +01001389 */
1390 rcu_read_lock();
1391 list_for_each_entry_rcu(object, &object_list, object_list) {
1392 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001393 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;
Catalin Marinas25873622009-07-07 10:32:58 +01001397 list_add_tail(&object->gray_list, &gray_list);
1398 }
1399 spin_unlock_irqrestore(&object->lock, flags);
1400 }
1401 rcu_read_unlock();
1402
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001403 /*
1404 * Re-scan the gray list for modified unreferenced objects.
1405 */
1406 scan_gray_list();
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001407
1408 /*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001409 * If scanning was stopped do not report any new unreferenced objects.
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001410 */
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001411 if (scan_should_stop())
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001412 return;
1413
1414 /*
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001415 * 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
Li Zefandc9b3f42014-04-03 14:46:26 -07001429 if (new_leaks) {
1430 kmemleak_found_leaks = true;
1431
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001432 pr_info("%d new suspected memory leaks (see "
1433 "/sys/kernel/debug/kmemleak)\n", new_leaks);
Li Zefandc9b3f42014-04-03 14:46:26 -07001434 }
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001435
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001436}
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 */
1442static int kmemleak_scan_thread(void *arg)
1443{
1444 static int first_run = 1;
1445
Joe Perchesae281062009-06-23 14:40:26 +01001446 pr_info("Automatic memory scanning thread started\n");
Catalin Marinasbf2a76b2009-07-07 10:32:55 +01001447 set_user_nice(current, 10);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001448
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()) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001458 signed long timeout = jiffies_scan_wait;
1459
1460 mutex_lock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001461 kmemleak_scan();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001462 mutex_unlock(&scan_mutex);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001463
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001464 /* wait before the next scan */
1465 while (timeout && !kthread_should_stop())
1466 timeout = schedule_timeout_interruptible(timeout);
1467 }
1468
Joe Perchesae281062009-06-23 14:40:26 +01001469 pr_info("Automatic memory scanning thread ended\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001470
1471 return 0;
1472}
1473
1474/*
1475 * Start the automatic memory scanning thread. This function must be called
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001476 * with the scan_mutex held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001477 */
Luis R. Rodriguez7eb0d5e2009-09-08 17:31:45 +01001478static void start_scan_thread(void)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001479{
1480 if (scan_thread)
1481 return;
1482 scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak");
1483 if (IS_ERR(scan_thread)) {
Joe Perchesae281062009-06-23 14:40:26 +01001484 pr_warning("Failed to create the scan thread\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001485 scan_thread = NULL;
1486 }
1487}
1488
1489/*
1490 * Stop the automatic memory scanning thread. This function must be called
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001491 * with the scan_mutex held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001492 */
Luis R. Rodriguez7eb0d5e2009-09-08 17:31:45 +01001493static void stop_scan_thread(void)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001494{
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 */
1506static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
1507{
1508 struct kmemleak_object *object;
1509 loff_t n = *pos;
Catalin Marinasb87324d2009-07-07 10:32:58 +01001510 int err;
1511
1512 err = mutex_lock_interruptible(&scan_mutex);
1513 if (err < 0)
1514 return ERR_PTR(err);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001515
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001516 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;
1524out:
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001525 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 */
1532static 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;
Michael Wang58fac092012-08-17 12:33:34 +08001536 struct kmemleak_object *obj = prev_obj;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001537
1538 ++(*pos);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001539
Michael Wang58fac092012-08-17 12:33:34 +08001540 list_for_each_entry_continue_rcu(obj, &object_list, object_list) {
Catalin Marinas52c3ce42011-04-27 16:44:26 +01001541 if (get_object(obj)) {
1542 next_obj = obj;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001543 break;
Catalin Marinas52c3ce42011-04-27 16:44:26 +01001544 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001545 }
Catalin Marinas288c8572009-07-07 10:32:57 +01001546
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001547 put_object(prev_obj);
1548 return next_obj;
1549}
1550
1551/*
1552 * Decrement the use_count of the last object required, if any.
1553 */
1554static void kmemleak_seq_stop(struct seq_file *seq, void *v)
1555{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001556 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 */
Catalin Marinasf5886c72009-07-29 16:26:57 +01001561 rcu_read_unlock();
Catalin Marinasb87324d2009-07-07 10:32:58 +01001562 mutex_unlock(&scan_mutex);
1563 if (v)
1564 put_object(v);
1565 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001566}
1567
1568/*
1569 * Print the information for an unreferenced object to the seq file.
1570 */
1571static 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);
Catalin Marinas288c8572009-07-07 10:32:57 +01001577 if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object))
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001578 print_unreferenced(seq, object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001579 spin_unlock_irqrestore(&object->lock, flags);
1580 return 0;
1581}
1582
1583static 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
1590static int kmemleak_open(struct inode *inode, struct file *file)
1591{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001592 return seq_open(file, &kmemleak_seq_ops);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001593}
1594
Catalin Marinas189d84e2009-08-27 14:29:15 +01001595static int dump_str_object_info(const char *str)
1596{
1597 unsigned long flags;
1598 struct kmemleak_object *object;
1599 unsigned long addr;
1600
Abhijit Pawardc053732012-12-18 14:23:27 -08001601 if (kstrtoul(str, 0, &addr))
1602 return -EINVAL;
Catalin Marinas189d84e2009-08-27 14:29:15 +01001603 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
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001617/*
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001618 * 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 */
1623static 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))
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -07001633 __paint_it(object, KMEMLEAK_GREY);
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001634 spin_unlock_irqrestore(&object->lock, flags);
1635 }
1636 rcu_read_unlock();
Li Zefandc9b3f42014-04-03 14:46:26 -07001637
1638 kmemleak_found_leaks = false;
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001639}
1640
Li Zefanc89da702014-04-03 14:46:27 -07001641static void __kmemleak_do_cleanup(void);
1642
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001643/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001644 * 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)
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001653 * scan - trigger a memory scan
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001654 * clear - mark all current reported unreferenced kmemleak objects as
Li Zefanc89da702014-04-03 14:46:27 -07001655 * grey to ignore printing them, or free all kmemleak objects
1656 * if kmemleak has been disabled.
Catalin Marinas189d84e2009-08-27 14:29:15 +01001657 * dump=... - dump information about the object found at the given address
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001658 */
1659static 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;
Catalin Marinasb87324d2009-07-07 10:32:58 +01001664 int ret;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001665
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
Catalin Marinasb87324d2009-07-07 10:32:58 +01001671 ret = mutex_lock_interruptible(&scan_mutex);
1672 if (ret < 0)
1673 return ret;
1674
Li Zefanc89da702014-04-03 14:46:27 -07001675 if (strncmp(buf, "clear", 5) == 0) {
Li Zefan8910ae82014-04-03 14:46:29 -07001676 if (kmemleak_enabled)
Li Zefanc89da702014-04-03 14:46:27 -07001677 kmemleak_clear();
1678 else
1679 __kmemleak_do_cleanup();
1680 goto out;
1681 }
1682
Li Zefan8910ae82014-04-03 14:46:29 -07001683 if (!kmemleak_enabled) {
Li Zefanc89da702014-04-03 14:46:27 -07001684 ret = -EBUSY;
1685 goto out;
1686 }
1687
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001688 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;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001700
Jingoo Han3dbb95f2013-09-11 14:20:25 -07001701 ret = kstrtoul(buf + 5, 0, &secs);
Catalin Marinasb87324d2009-07-07 10:32:58 +01001702 if (ret < 0)
1703 goto out;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001704 stop_scan_thread();
1705 if (secs) {
1706 jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
1707 start_scan_thread();
1708 }
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001709 } else if (strncmp(buf, "scan", 4) == 0)
1710 kmemleak_scan();
Catalin Marinas189d84e2009-08-27 14:29:15 +01001711 else if (strncmp(buf, "dump=", 5) == 0)
1712 ret = dump_str_object_info(buf + 5);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001713 else
Catalin Marinasb87324d2009-07-07 10:32:58 +01001714 ret = -EINVAL;
1715
1716out:
1717 mutex_unlock(&scan_mutex);
1718 if (ret < 0)
1719 return ret;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001720
1721 /* ignore the rest of the buffer, only one command at a time */
1722 *ppos += size;
1723 return size;
1724}
1725
1726static 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,
Li Zefan5f3bf192014-04-03 14:46:28 -07001732 .release = seq_release,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001733};
1734
Li Zefanc89da702014-04-03 14:46:27 -07001735static 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
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001745/*
Catalin Marinas74341702011-09-29 11:50:07 +01001746 * 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).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001749 */
Catalin Marinas179a8102009-09-07 10:14:42 +01001750static void kmemleak_do_cleanup(struct work_struct *work)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001751{
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001752 mutex_lock(&scan_mutex);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001753 stop_scan_thread();
1754
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07001755 /*
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
Li Zefanc89da702014-04-03 14:46:27 -07001762 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");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001767 mutex_unlock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001768}
1769
Catalin Marinas179a8102009-09-07 10:14:42 +01001770static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001771
1772/*
1773 * Disable kmemleak. No memory allocation/freeing will be traced once this
1774 * function is called. Disabling kmemleak is an irreversible operation.
1775 */
1776static void kmemleak_disable(void)
1777{
1778 /* atomically check whether it was already invoked */
Li Zefan8910ae82014-04-03 14:46:29 -07001779 if (cmpxchg(&kmemleak_error, 0, 1))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001780 return;
1781
1782 /* stop any memory operation tracing */
Li Zefan8910ae82014-04-03 14:46:29 -07001783 kmemleak_enabled = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001784
1785 /* check whether it is too early for a kernel thread */
Li Zefan8910ae82014-04-03 14:46:29 -07001786 if (kmemleak_initialized)
Catalin Marinas179a8102009-09-07 10:14:42 +01001787 schedule_work(&cleanup_work);
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07001788 else
1789 kmemleak_free_enabled = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001790
1791 pr_info("Kernel memory leak detector disabled\n");
1792}
1793
1794/*
1795 * Allow boot-time kmemleak disabling (enabled by default).
1796 */
1797static int kmemleak_boot_config(char *str)
1798{
1799 if (!str)
1800 return -EINVAL;
1801 if (strcmp(str, "off") == 0)
1802 kmemleak_disable();
Jason Baronab0155a2010-07-19 11:54:17 +01001803 else if (strcmp(str, "on") == 0)
1804 kmemleak_skip_disable = 1;
1805 else
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001806 return -EINVAL;
1807 return 0;
1808}
1809early_param("kmemleak", kmemleak_boot_config);
1810
Catalin Marinas5f790202011-09-28 12:17:03 +01001811static 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
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001822/*
Catalin Marinas20301172009-06-17 18:29:04 +01001823 * Kmemleak initialization.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001824 */
1825void __init kmemleak_init(void)
1826{
1827 int i;
1828 unsigned long flags;
1829
Jason Baronab0155a2010-07-19 11:54:17 +01001830#ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF
1831 if (!kmemleak_skip_disable) {
Catalin Marinas3551a922014-05-09 15:36:59 -07001832 kmemleak_early_log = 0;
Jason Baronab0155a2010-07-19 11:54:17 +01001833 kmemleak_disable();
1834 return;
1835 }
1836#endif
1837
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001838 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);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001843
Catalin Marinasb6693002011-09-28 17:22:56 +01001844 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
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001848 /* the kernel is still in UP mode, so disabling the IRQs is enough */
1849 local_irq_save(flags);
Catalin Marinas3551a922014-05-09 15:36:59 -07001850 kmemleak_early_log = 0;
Li Zefan8910ae82014-04-03 14:46:29 -07001851 if (kmemleak_error) {
Catalin Marinasb6693002011-09-28 17:22:56 +01001852 local_irq_restore(flags);
1853 return;
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07001854 } else {
Li Zefan8910ae82014-04-03 14:46:29 -07001855 kmemleak_enabled = 1;
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07001856 kmemleak_free_enabled = 1;
1857 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001858 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:
Catalin Marinasfd678962009-08-27 14:29:17 +01001870 early_alloc(log);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001871 break;
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001872 case KMEMLEAK_ALLOC_PERCPU:
1873 early_alloc_percpu(log);
1874 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001875 case KMEMLEAK_FREE:
1876 kmemleak_free(log->ptr);
1877 break;
Catalin Marinas53238a62009-07-07 10:33:00 +01001878 case KMEMLEAK_FREE_PART:
1879 kmemleak_free_part(log->ptr, log->size);
1880 break;
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001881 case KMEMLEAK_FREE_PERCPU:
1882 kmemleak_free_percpu(log->ptr);
1883 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001884 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:
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001891 kmemleak_scan_area(log->ptr, log->size, GFP_KERNEL);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001892 break;
1893 case KMEMLEAK_NO_SCAN:
1894 kmemleak_no_scan(log->ptr);
1895 break;
1896 default:
Catalin Marinas5f790202011-09-28 12:17:03 +01001897 kmemleak_warn("Unknown early log operation: %d\n",
1898 log->op_type);
1899 }
1900
Li Zefan8910ae82014-04-03 14:46:29 -07001901 if (kmemleak_warning) {
Catalin Marinas5f790202011-09-28 12:17:03 +01001902 print_log_trace(log);
Li Zefan8910ae82014-04-03 14:46:29 -07001903 kmemleak_warning = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001904 }
1905 }
1906}
1907
1908/*
1909 * Late initialization function.
1910 */
1911static int __init kmemleak_late_init(void)
1912{
1913 struct dentry *dentry;
1914
Li Zefan8910ae82014-04-03 14:46:29 -07001915 kmemleak_initialized = 1;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001916
Li Zefan8910ae82014-04-03 14:46:29 -07001917 if (kmemleak_error) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001918 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001919 * Some error occurred and kmemleak was disabled. There is a
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001920 * 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 */
Catalin Marinas179a8102009-09-07 10:14:42 +01001924 schedule_work(&cleanup_work);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001925 return -ENOMEM;
1926 }
1927
1928 dentry = debugfs_create_file("kmemleak", S_IRUGO, NULL, NULL,
1929 &kmemleak_fops);
1930 if (!dentry)
Joe Perchesae281062009-06-23 14:40:26 +01001931 pr_warning("Failed to create the debugfs kmemleak file\n");
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001932 mutex_lock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001933 start_scan_thread();
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001934 mutex_unlock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001935
1936 pr_info("Kernel memory leak detector initialized\n");
1937
1938 return 0;
1939}
1940late_initcall(kmemleak_late_init);