blob: 15c50302ff930f0ee0c57f5eadbbed212f8fcd98 [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
32 * blocks. The object_tree_root is a priority search tree used to look-up
33 * metadata based on a pointer to the corresponding memory block. The
34 * kmemleak_object structures are added to the object_list and
35 * object_tree_root in the create_object() function called from the
36 * kmemleak_alloc() callback and removed in delete_object() called from the
37 * kmemleak_free() callback
38 * - kmemleak_object.lock (spinlock): protects a kmemleak_object. Accesses to
39 * the metadata (e.g. count) are protected by this lock. Note that some
40 * members of this structure may be protected by other means (atomic or
41 * kmemleak_lock). This lock is also held when scanning the corresponding
42 * memory block to avoid the kernel freeing it via the kmemleak_free()
43 * callback. This is less heavyweight than holding a global lock like
44 * kmemleak_lock during scanning
45 * - scan_mutex (mutex): ensures that only one thread may scan the memory for
46 * unreferenced objects at a time. The gray_list contains the objects which
47 * are already referenced or marked as false positives and need to be
48 * scanned. This list is only modified during a scanning episode when the
49 * scan_mutex is held. At the end of a scan, the gray_list is always empty.
50 * Note that the kmemleak_object.use_count is incremented when an object is
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>
74#include <linux/prio_tree.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
Pekka Enberg8e019362009-08-27 14:50:00 +0100101#include <linux/kmemcheck.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100102#include <linux/kmemleak.h>
103
104/*
105 * Kmemleak configuration and common defines.
106 */
107#define MAX_TRACE 16 /* stack trace length */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100108#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100109#define SECS_FIRST_SCAN 60 /* delay before the first scan */
110#define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */
Catalin Marinasaf986032009-08-27 14:29:12 +0100111#define MAX_SCAN_SIZE 4096 /* maximum size of a scanned block */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100112
113#define BYTES_PER_POINTER sizeof(void *)
114
Catalin Marinas216c04b2009-06-17 18:29:02 +0100115/* GFP bitmask for kmemleak internal allocations */
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000116#define gfp_kmemleak_mask(gfp) (((gfp) & (GFP_KERNEL | GFP_ATOMIC)) | \
117 __GFP_NORETRY | __GFP_NOMEMALLOC | \
118 __GFP_NOWARN)
Catalin Marinas216c04b2009-06-17 18:29:02 +0100119
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100120/* scanning area inside a memory block */
121struct kmemleak_scan_area {
122 struct hlist_node node;
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000123 unsigned long start;
124 size_t size;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100125};
126
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700127#define KMEMLEAK_GREY 0
128#define KMEMLEAK_BLACK -1
129
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100130/*
131 * Structure holding the metadata for each allocated memory block.
132 * Modifications to such objects should be made while holding the
133 * object->lock. Insertions or deletions from object_list, gray_list or
134 * tree_node are already protected by the corresponding locks or mutex (see
135 * the notes on locking above). These objects are reference-counted
136 * (use_count) and freed using the RCU mechanism.
137 */
138struct kmemleak_object {
139 spinlock_t lock;
140 unsigned long flags; /* object status flags */
141 struct list_head object_list;
142 struct list_head gray_list;
143 struct prio_tree_node tree_node;
144 struct rcu_head rcu; /* object_list lockless traversal */
145 /* object usage count; object freed when use_count == 0 */
146 atomic_t use_count;
147 unsigned long pointer;
148 size_t size;
149 /* minimum number of a pointers found before it is considered leak */
150 int min_count;
151 /* the total number of pointers found pointing to this object */
152 int count;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000153 /* checksum for detecting modified objects */
154 u32 checksum;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100155 /* memory ranges to be scanned inside an object (empty for all) */
156 struct hlist_head area_list;
157 unsigned long trace[MAX_TRACE];
158 unsigned int trace_len;
159 unsigned long jiffies; /* creation timestamp */
160 pid_t pid; /* pid of the current task */
161 char comm[TASK_COMM_LEN]; /* executable name */
162};
163
164/* flag representing the memory block allocation status */
165#define OBJECT_ALLOCATED (1 << 0)
166/* flag set after the first reporting of an unreference object */
167#define OBJECT_REPORTED (1 << 1)
168/* flag set to not scan the object */
169#define OBJECT_NO_SCAN (1 << 2)
170
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100171/* number of bytes to print per line; must be 16 or 32 */
172#define HEX_ROW_SIZE 16
173/* number of bytes to print at a time (1, 2, 4, 8) */
174#define HEX_GROUP_SIZE 1
175/* include ASCII after the hex output */
176#define HEX_ASCII 1
177/* max number of lines to be printed */
178#define HEX_MAX_LINES 2
179
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100180/* the list of all allocated objects */
181static LIST_HEAD(object_list);
182/* the list of gray-colored objects (see color_gray comment below) */
183static LIST_HEAD(gray_list);
184/* prio search tree for object boundaries */
185static struct prio_tree_root object_tree_root;
186/* rw_lock protecting the access to object_list and prio_tree_root */
187static DEFINE_RWLOCK(kmemleak_lock);
188
189/* allocation caches for kmemleak internal data */
190static struct kmem_cache *object_cache;
191static struct kmem_cache *scan_area_cache;
192
193/* set if tracing memory operations is enabled */
194static atomic_t kmemleak_enabled = ATOMIC_INIT(0);
195/* set in the late_initcall if there were no errors */
196static atomic_t kmemleak_initialized = ATOMIC_INIT(0);
197/* enables or disables early logging of the memory operations */
198static atomic_t kmemleak_early_log = ATOMIC_INIT(1);
Catalin Marinas5f790202011-09-28 12:17:03 +0100199/* set if a kmemleak warning was issued */
200static atomic_t kmemleak_warning = ATOMIC_INIT(0);
201/* set if a fatal kmemleak error has occurred */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100202static atomic_t kmemleak_error = ATOMIC_INIT(0);
203
204/* minimum and maximum address that may be valid pointers */
205static unsigned long min_addr = ULONG_MAX;
206static unsigned long max_addr;
207
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100208static struct task_struct *scan_thread;
Catalin Marinasacf49682009-06-26 17:38:29 +0100209/* used to avoid reporting of recently allocated objects */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100210static unsigned long jiffies_min_age;
Catalin Marinasacf49682009-06-26 17:38:29 +0100211static unsigned long jiffies_last_scan;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100212/* delay between automatic memory scannings */
213static signed long jiffies_scan_wait;
214/* enables or disables the task stacks scanning */
Catalin Marinase0a2a162009-06-26 17:38:25 +0100215static int kmemleak_stack_scan = 1;
Catalin Marinas4698c1f2009-06-26 17:38:27 +0100216/* protects the memory scanning, parameters and debug/kmemleak file access */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100217static DEFINE_MUTEX(scan_mutex);
Jason Baronab0155a2010-07-19 11:54:17 +0100218/* setting kmemleak=on, will set this var, skipping the disable */
219static int kmemleak_skip_disable;
220
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100221
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100222/*
Catalin Marinas20301172009-06-17 18:29:04 +0100223 * Early object allocation/freeing logging. Kmemleak is initialized after the
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100224 * kernel allocator. However, both the kernel allocator and kmemleak may
Catalin Marinas20301172009-06-17 18:29:04 +0100225 * allocate memory blocks which need to be tracked. Kmemleak defines an
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100226 * arbitrary buffer to hold the allocation/freeing information before it is
227 * fully initialized.
228 */
229
230/* kmemleak operation type for early logging */
231enum {
232 KMEMLEAK_ALLOC,
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100233 KMEMLEAK_ALLOC_PERCPU,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100234 KMEMLEAK_FREE,
Catalin Marinas53238a62009-07-07 10:33:00 +0100235 KMEMLEAK_FREE_PART,
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100236 KMEMLEAK_FREE_PERCPU,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100237 KMEMLEAK_NOT_LEAK,
238 KMEMLEAK_IGNORE,
239 KMEMLEAK_SCAN_AREA,
240 KMEMLEAK_NO_SCAN
241};
242
243/*
244 * Structure holding the information passed to kmemleak callbacks during the
245 * early logging.
246 */
247struct early_log {
248 int op_type; /* kmemleak operation type */
249 const void *ptr; /* allocated/freed memory block */
250 size_t size; /* memory block size */
251 int min_count; /* minimum reference count */
Catalin Marinasfd678962009-08-27 14:29:17 +0100252 unsigned long trace[MAX_TRACE]; /* stack trace */
253 unsigned int trace_len; /* stack trace length */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100254};
255
256/* early logging buffer and current position */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100257static struct early_log
258 early_log[CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE] __initdata;
259static int crt_early_log __initdata;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100260
261static void kmemleak_disable(void);
262
263/*
264 * Print a warning and dump the stack trace.
265 */
Catalin Marinas5f790202011-09-28 12:17:03 +0100266#define kmemleak_warn(x...) do { \
267 pr_warning(x); \
268 dump_stack(); \
269 atomic_set(&kmemleak_warning, 1); \
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100270} while (0)
271
272/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300273 * Macro invoked when a serious kmemleak condition occurred and cannot be
Catalin Marinas20301172009-06-17 18:29:04 +0100274 * recovered from. Kmemleak will be disabled and further allocation/freeing
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100275 * tracing no longer available.
276 */
Catalin Marinas000814f2009-06-17 18:29:03 +0100277#define kmemleak_stop(x...) do { \
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100278 kmemleak_warn(x); \
279 kmemleak_disable(); \
280} while (0)
281
282/*
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100283 * Printing of the objects hex dump to the seq file. The number of lines to be
284 * printed is limited to HEX_MAX_LINES to prevent seq file spamming. The
285 * actual number of printed bytes depends on HEX_ROW_SIZE. It must be called
286 * with the object->lock held.
287 */
288static void hex_dump_object(struct seq_file *seq,
289 struct kmemleak_object *object)
290{
291 const u8 *ptr = (const u8 *)object->pointer;
292 int i, len, remaining;
293 unsigned char linebuf[HEX_ROW_SIZE * 5];
294
295 /* limit the number of lines to HEX_MAX_LINES */
296 remaining = len =
297 min(object->size, (size_t)(HEX_MAX_LINES * HEX_ROW_SIZE));
298
299 seq_printf(seq, " hex dump (first %d bytes):\n", len);
300 for (i = 0; i < len; i += HEX_ROW_SIZE) {
301 int linelen = min(remaining, HEX_ROW_SIZE);
302
303 remaining -= HEX_ROW_SIZE;
304 hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE,
305 HEX_GROUP_SIZE, linebuf, sizeof(linebuf),
306 HEX_ASCII);
307 seq_printf(seq, " %s\n", linebuf);
308 }
309}
310
311/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100312 * Object colors, encoded with count and min_count:
313 * - white - orphan object, not enough references to it (count < min_count)
314 * - gray - not orphan, not marked as false positive (min_count == 0) or
315 * sufficient references to it (count >= min_count)
316 * - black - ignore, it doesn't contain references (e.g. text section)
317 * (min_count == -1). No function defined for this color.
318 * Newly created objects don't have any color assigned (object->count == -1)
319 * before the next memory scan when they become white.
320 */
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100321static bool color_white(const struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100322{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700323 return object->count != KMEMLEAK_BLACK &&
324 object->count < object->min_count;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100325}
326
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100327static bool color_gray(const struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100328{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700329 return object->min_count != KMEMLEAK_BLACK &&
330 object->count >= object->min_count;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100331}
332
333/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100334 * Objects are considered unreferenced only if their color is white, they have
335 * not be deleted and have a minimum age to avoid false positives caused by
336 * pointers temporarily stored in CPU registers.
337 */
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100338static bool unreferenced_object(struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100339{
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000340 return (color_white(object) && object->flags & OBJECT_ALLOCATED) &&
Catalin Marinasacf49682009-06-26 17:38:29 +0100341 time_before_eq(object->jiffies + jiffies_min_age,
342 jiffies_last_scan);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100343}
344
345/*
Catalin Marinasbab4a342009-06-26 17:38:26 +0100346 * Printing of the unreferenced objects information to the seq file. The
347 * print_unreferenced function must be called with the object->lock held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100348 */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100349static void print_unreferenced(struct seq_file *seq,
350 struct kmemleak_object *object)
351{
352 int i;
Catalin Marinasfefdd332009-10-28 13:33:12 +0000353 unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100354
Catalin Marinasbab4a342009-06-26 17:38:26 +0100355 seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
356 object->pointer, object->size);
Catalin Marinasfefdd332009-10-28 13:33:12 +0000357 seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n",
358 object->comm, object->pid, object->jiffies,
359 msecs_age / 1000, msecs_age % 1000);
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100360 hex_dump_object(seq, object);
Catalin Marinasbab4a342009-06-26 17:38:26 +0100361 seq_printf(seq, " backtrace:\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100362
363 for (i = 0; i < object->trace_len; i++) {
364 void *ptr = (void *)object->trace[i];
Catalin Marinasbab4a342009-06-26 17:38:26 +0100365 seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100366 }
367}
368
369/*
370 * Print the kmemleak_object information. This function is used mainly for
371 * debugging special cases when kmemleak operations. It must be called with
372 * the object->lock held.
373 */
374static void dump_object_info(struct kmemleak_object *object)
375{
376 struct stack_trace trace;
377
378 trace.nr_entries = object->trace_len;
379 trace.entries = object->trace;
380
Joe Perchesae281062009-06-23 14:40:26 +0100381 pr_notice("Object 0x%08lx (size %zu):\n",
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100382 object->tree_node.start, object->size);
383 pr_notice(" comm \"%s\", pid %d, jiffies %lu\n",
384 object->comm, object->pid, object->jiffies);
385 pr_notice(" min_count = %d\n", object->min_count);
386 pr_notice(" count = %d\n", object->count);
Catalin Marinas189d84e2009-08-27 14:29:15 +0100387 pr_notice(" flags = 0x%lx\n", object->flags);
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000388 pr_notice(" checksum = %d\n", object->checksum);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100389 pr_notice(" backtrace:\n");
390 print_stack_trace(&trace, 4);
391}
392
393/*
394 * Look-up a memory block metadata (kmemleak_object) in the priority search
395 * tree based on a pointer value. If alias is 0, only values pointing to the
396 * beginning of the memory block are allowed. The kmemleak_lock must be held
397 * when calling this function.
398 */
399static struct kmemleak_object *lookup_object(unsigned long ptr, int alias)
400{
401 struct prio_tree_node *node;
402 struct prio_tree_iter iter;
403 struct kmemleak_object *object;
404
405 prio_tree_iter_init(&iter, &object_tree_root, ptr, ptr);
406 node = prio_tree_next(&iter);
407 if (node) {
408 object = prio_tree_entry(node, struct kmemleak_object,
409 tree_node);
410 if (!alias && object->pointer != ptr) {
Catalin Marinas5f790202011-09-28 12:17:03 +0100411 kmemleak_warn("Found object by alias at 0x%08lx\n",
412 ptr);
Catalin Marinasa7686a42010-07-19 11:54:16 +0100413 dump_object_info(object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100414 object = NULL;
415 }
416 } else
417 object = NULL;
418
419 return object;
420}
421
422/*
423 * Increment the object use_count. Return 1 if successful or 0 otherwise. Note
424 * that once an object's use_count reached 0, the RCU freeing was already
425 * registered and the object should no longer be used. This function must be
426 * called under the protection of rcu_read_lock().
427 */
428static int get_object(struct kmemleak_object *object)
429{
430 return atomic_inc_not_zero(&object->use_count);
431}
432
433/*
434 * RCU callback to free a kmemleak_object.
435 */
436static void free_object_rcu(struct rcu_head *rcu)
437{
438 struct hlist_node *elem, *tmp;
439 struct kmemleak_scan_area *area;
440 struct kmemleak_object *object =
441 container_of(rcu, struct kmemleak_object, rcu);
442
443 /*
444 * Once use_count is 0 (guaranteed by put_object), there is no other
445 * code accessing this object, hence no need for locking.
446 */
447 hlist_for_each_entry_safe(area, elem, tmp, &object->area_list, node) {
448 hlist_del(elem);
449 kmem_cache_free(scan_area_cache, area);
450 }
451 kmem_cache_free(object_cache, object);
452}
453
454/*
455 * Decrement the object use_count. Once the count is 0, free the object using
456 * an RCU callback. Since put_object() may be called via the kmemleak_free() ->
457 * delete_object() path, the delayed RCU freeing ensures that there is no
458 * recursive call to the kernel allocator. Lock-less RCU object_list traversal
459 * is also possible.
460 */
461static void put_object(struct kmemleak_object *object)
462{
463 if (!atomic_dec_and_test(&object->use_count))
464 return;
465
466 /* should only get here after delete_object was called */
467 WARN_ON(object->flags & OBJECT_ALLOCATED);
468
469 call_rcu(&object->rcu, free_object_rcu);
470}
471
472/*
473 * Look up an object in the prio search tree and increase its use_count.
474 */
475static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias)
476{
477 unsigned long flags;
478 struct kmemleak_object *object = NULL;
479
480 rcu_read_lock();
481 read_lock_irqsave(&kmemleak_lock, flags);
482 if (ptr >= min_addr && ptr < max_addr)
483 object = lookup_object(ptr, alias);
484 read_unlock_irqrestore(&kmemleak_lock, flags);
485
486 /* check whether the object is still available */
487 if (object && !get_object(object))
488 object = NULL;
489 rcu_read_unlock();
490
491 return object;
492}
493
494/*
Catalin Marinasfd678962009-08-27 14:29:17 +0100495 * Save stack trace to the given array of MAX_TRACE size.
496 */
497static int __save_stack_trace(unsigned long *trace)
498{
499 struct stack_trace stack_trace;
500
501 stack_trace.max_entries = MAX_TRACE;
502 stack_trace.nr_entries = 0;
503 stack_trace.entries = trace;
504 stack_trace.skip = 2;
505 save_stack_trace(&stack_trace);
506
507 return stack_trace.nr_entries;
508}
509
510/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100511 * Create the metadata (struct kmemleak_object) corresponding to an allocated
512 * memory block and add it to the object_list and object_tree_root.
513 */
Catalin Marinasfd678962009-08-27 14:29:17 +0100514static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
515 int min_count, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100516{
517 unsigned long flags;
518 struct kmemleak_object *object;
519 struct prio_tree_node *node;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100520
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000521 object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100522 if (!object) {
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000523 pr_warning("Cannot allocate a kmemleak_object structure\n");
524 kmemleak_disable();
Catalin Marinasfd678962009-08-27 14:29:17 +0100525 return NULL;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100526 }
527
528 INIT_LIST_HEAD(&object->object_list);
529 INIT_LIST_HEAD(&object->gray_list);
530 INIT_HLIST_HEAD(&object->area_list);
531 spin_lock_init(&object->lock);
532 atomic_set(&object->use_count, 1);
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000533 object->flags = OBJECT_ALLOCATED;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100534 object->pointer = ptr;
535 object->size = size;
536 object->min_count = min_count;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000537 object->count = 0; /* white color initially */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100538 object->jiffies = jiffies;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000539 object->checksum = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100540
541 /* task information */
542 if (in_irq()) {
543 object->pid = 0;
544 strncpy(object->comm, "hardirq", sizeof(object->comm));
545 } else if (in_softirq()) {
546 object->pid = 0;
547 strncpy(object->comm, "softirq", sizeof(object->comm));
548 } else {
549 object->pid = current->pid;
550 /*
551 * There is a small chance of a race with set_task_comm(),
552 * however using get_task_comm() here may cause locking
553 * dependency issues with current->alloc_lock. In the worst
554 * case, the command line is not correct.
555 */
556 strncpy(object->comm, current->comm, sizeof(object->comm));
557 }
558
559 /* kernel backtrace */
Catalin Marinasfd678962009-08-27 14:29:17 +0100560 object->trace_len = __save_stack_trace(object->trace);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100561
562 INIT_PRIO_TREE_NODE(&object->tree_node);
563 object->tree_node.start = ptr;
564 object->tree_node.last = ptr + size - 1;
565
566 write_lock_irqsave(&kmemleak_lock, flags);
Luis R. Rodriguez0580a182009-09-08 17:32:34 +0100567
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100568 min_addr = min(min_addr, ptr);
569 max_addr = max(max_addr, ptr + size);
570 node = prio_tree_insert(&object_tree_root, &object->tree_node);
571 /*
572 * The code calling the kernel does not yet have the pointer to the
573 * memory block to be able to free it. However, we still hold the
574 * kmemleak_lock here in case parts of the kernel started freeing
575 * random memory blocks.
576 */
577 if (node != &object->tree_node) {
Joe Perchesae281062009-06-23 14:40:26 +0100578 kmemleak_stop("Cannot insert 0x%lx into the object search tree "
579 "(already existing)\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100580 object = lookup_object(ptr, 1);
Luis R. Rodriguez0580a182009-09-08 17:32:34 +0100581 spin_lock(&object->lock);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100582 dump_object_info(object);
Luis R. Rodriguez0580a182009-09-08 17:32:34 +0100583 spin_unlock(&object->lock);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100584
585 goto out;
586 }
587 list_add_tail_rcu(&object->object_list, &object_list);
588out:
589 write_unlock_irqrestore(&kmemleak_lock, flags);
Catalin Marinasfd678962009-08-27 14:29:17 +0100590 return object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100591}
592
593/*
594 * Remove the metadata (struct kmemleak_object) for a memory block from the
595 * object_list and object_tree_root and decrement its use_count.
596 */
Catalin Marinas53238a62009-07-07 10:33:00 +0100597static void __delete_object(struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100598{
599 unsigned long flags;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100600
601 write_lock_irqsave(&kmemleak_lock, flags);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100602 prio_tree_remove(&object_tree_root, &object->tree_node);
603 list_del_rcu(&object->object_list);
604 write_unlock_irqrestore(&kmemleak_lock, flags);
605
606 WARN_ON(!(object->flags & OBJECT_ALLOCATED));
Catalin Marinas53238a62009-07-07 10:33:00 +0100607 WARN_ON(atomic_read(&object->use_count) < 2);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100608
609 /*
610 * Locking here also ensures that the corresponding memory block
611 * cannot be freed when it is being scanned.
612 */
613 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100614 object->flags &= ~OBJECT_ALLOCATED;
615 spin_unlock_irqrestore(&object->lock, flags);
616 put_object(object);
617}
618
619/*
Catalin Marinas53238a62009-07-07 10:33:00 +0100620 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
621 * delete it.
622 */
623static void delete_object_full(unsigned long ptr)
624{
625 struct kmemleak_object *object;
626
627 object = find_and_get_object(ptr, 0);
628 if (!object) {
629#ifdef DEBUG
630 kmemleak_warn("Freeing unknown object at 0x%08lx\n",
631 ptr);
632#endif
633 return;
634 }
635 __delete_object(object);
636 put_object(object);
637}
638
639/*
640 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
641 * delete it. If the memory block is partially freed, the function may create
642 * additional metadata for the remaining parts of the block.
643 */
644static void delete_object_part(unsigned long ptr, size_t size)
645{
646 struct kmemleak_object *object;
647 unsigned long start, end;
648
649 object = find_and_get_object(ptr, 1);
650 if (!object) {
651#ifdef DEBUG
652 kmemleak_warn("Partially freeing unknown object at 0x%08lx "
653 "(size %zu)\n", ptr, size);
654#endif
655 return;
656 }
657 __delete_object(object);
658
659 /*
660 * Create one or two objects that may result from the memory block
661 * split. Note that partial freeing is only done by free_bootmem() and
662 * this happens before kmemleak_init() is called. The path below is
663 * only executed during early log recording in kmemleak_init(), so
664 * GFP_KERNEL is enough.
665 */
666 start = object->pointer;
667 end = object->pointer + object->size;
668 if (ptr > start)
669 create_object(start, ptr - start, object->min_count,
670 GFP_KERNEL);
671 if (ptr + size < end)
672 create_object(ptr + size, end - ptr - size, object->min_count,
673 GFP_KERNEL);
674
675 put_object(object);
676}
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700677
678static void __paint_it(struct kmemleak_object *object, int color)
679{
680 object->min_count = color;
681 if (color == KMEMLEAK_BLACK)
682 object->flags |= OBJECT_NO_SCAN;
683}
684
685static void paint_it(struct kmemleak_object *object, int color)
686{
687 unsigned long flags;
688
689 spin_lock_irqsave(&object->lock, flags);
690 __paint_it(object, color);
691 spin_unlock_irqrestore(&object->lock, flags);
692}
693
694static void paint_ptr(unsigned long ptr, int color)
695{
696 struct kmemleak_object *object;
697
698 object = find_and_get_object(ptr, 0);
699 if (!object) {
700 kmemleak_warn("Trying to color unknown object "
701 "at 0x%08lx as %s\n", ptr,
702 (color == KMEMLEAK_GREY) ? "Grey" :
703 (color == KMEMLEAK_BLACK) ? "Black" : "Unknown");
704 return;
705 }
706 paint_it(object, color);
707 put_object(object);
708}
709
Catalin Marinas53238a62009-07-07 10:33:00 +0100710/*
Holger Hans Peter Freyther145b64b2010-07-22 19:54:13 +0800711 * Mark an object permanently as gray-colored so that it can no longer be
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100712 * reported as a leak. This is used in general to mark a false positive.
713 */
714static void make_gray_object(unsigned long ptr)
715{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700716 paint_ptr(ptr, KMEMLEAK_GREY);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100717}
718
719/*
720 * Mark the object as black-colored so that it is ignored from scans and
721 * reporting.
722 */
723static void make_black_object(unsigned long ptr)
724{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700725 paint_ptr(ptr, KMEMLEAK_BLACK);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100726}
727
728/*
729 * Add a scanning area to the object. If at least one such area is added,
730 * kmemleak will only scan these ranges rather than the whole memory block.
731 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000732static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100733{
734 unsigned long flags;
735 struct kmemleak_object *object;
736 struct kmemleak_scan_area *area;
737
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000738 object = find_and_get_object(ptr, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100739 if (!object) {
Joe Perchesae281062009-06-23 14:40:26 +0100740 kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n",
741 ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100742 return;
743 }
744
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000745 area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100746 if (!area) {
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000747 pr_warning("Cannot allocate a scan area\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100748 goto out;
749 }
750
751 spin_lock_irqsave(&object->lock, flags);
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000752 if (ptr + size > object->pointer + object->size) {
Joe Perchesae281062009-06-23 14:40:26 +0100753 kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100754 dump_object_info(object);
755 kmem_cache_free(scan_area_cache, area);
756 goto out_unlock;
757 }
758
759 INIT_HLIST_NODE(&area->node);
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000760 area->start = ptr;
761 area->size = size;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100762
763 hlist_add_head(&area->node, &object->area_list);
764out_unlock:
765 spin_unlock_irqrestore(&object->lock, flags);
766out:
767 put_object(object);
768}
769
770/*
771 * Set the OBJECT_NO_SCAN flag for the object corresponding to the give
772 * pointer. Such object will not be scanned by kmemleak but references to it
773 * are searched.
774 */
775static void object_no_scan(unsigned long ptr)
776{
777 unsigned long flags;
778 struct kmemleak_object *object;
779
780 object = find_and_get_object(ptr, 0);
781 if (!object) {
Joe Perchesae281062009-06-23 14:40:26 +0100782 kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100783 return;
784 }
785
786 spin_lock_irqsave(&object->lock, flags);
787 object->flags |= OBJECT_NO_SCAN;
788 spin_unlock_irqrestore(&object->lock, flags);
789 put_object(object);
790}
791
792/*
793 * Log an early kmemleak_* call to the early_log buffer. These calls will be
794 * processed later once kmemleak is fully initialized.
795 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100796static void __init log_early(int op_type, const void *ptr, size_t size,
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000797 int min_count)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100798{
799 unsigned long flags;
800 struct early_log *log;
801
Catalin Marinasb6693002011-09-28 17:22:56 +0100802 if (atomic_read(&kmemleak_error)) {
803 /* kmemleak stopped recording, just count the requests */
804 crt_early_log++;
805 return;
806 }
807
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100808 if (crt_early_log >= ARRAY_SIZE(early_log)) {
Catalin Marinasa9d90582009-06-25 10:16:11 +0100809 kmemleak_disable();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100810 return;
811 }
812
813 /*
814 * There is no need for locking since the kernel is still in UP mode
815 * at this stage. Disabling the IRQs is enough.
816 */
817 local_irq_save(flags);
818 log = &early_log[crt_early_log];
819 log->op_type = op_type;
820 log->ptr = ptr;
821 log->size = size;
822 log->min_count = min_count;
Catalin Marinas5f790202011-09-28 12:17:03 +0100823 log->trace_len = __save_stack_trace(log->trace);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100824 crt_early_log++;
825 local_irq_restore(flags);
826}
827
828/*
Catalin Marinasfd678962009-08-27 14:29:17 +0100829 * Log an early allocated block and populate the stack trace.
830 */
831static void early_alloc(struct early_log *log)
832{
833 struct kmemleak_object *object;
834 unsigned long flags;
835 int i;
836
837 if (!atomic_read(&kmemleak_enabled) || !log->ptr || IS_ERR(log->ptr))
838 return;
839
840 /*
841 * RCU locking needed to ensure object is not freed via put_object().
842 */
843 rcu_read_lock();
844 object = create_object((unsigned long)log->ptr, log->size,
Tetsuo Handac1bcd6b2009-10-09 10:39:24 +0100845 log->min_count, GFP_ATOMIC);
Catalin Marinas0d5d1aa2009-10-09 10:30:34 +0100846 if (!object)
847 goto out;
Catalin Marinasfd678962009-08-27 14:29:17 +0100848 spin_lock_irqsave(&object->lock, flags);
849 for (i = 0; i < log->trace_len; i++)
850 object->trace[i] = log->trace[i];
851 object->trace_len = log->trace_len;
852 spin_unlock_irqrestore(&object->lock, flags);
Catalin Marinas0d5d1aa2009-10-09 10:30:34 +0100853out:
Catalin Marinasfd678962009-08-27 14:29:17 +0100854 rcu_read_unlock();
855}
856
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100857/*
858 * Log an early allocated block and populate the stack trace.
859 */
860static void early_alloc_percpu(struct early_log *log)
861{
862 unsigned int cpu;
863 const void __percpu *ptr = log->ptr;
864
865 for_each_possible_cpu(cpu) {
866 log->ptr = per_cpu_ptr(ptr, cpu);
867 early_alloc(log);
868 }
869}
870
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100871/**
872 * kmemleak_alloc - register a newly allocated object
873 * @ptr: pointer to beginning of the object
874 * @size: size of the object
875 * @min_count: minimum number of references to this object. If during memory
876 * scanning a number of references less than @min_count is found,
877 * the object is reported as a memory leak. If @min_count is 0,
878 * the object is never reported as a leak. If @min_count is -1,
879 * the object is ignored (not scanned and not reported as a leak)
880 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
881 *
882 * This function is called from the kernel allocators when a new object
883 * (memory block) is allocated (kmem_cache_alloc, kmalloc, vmalloc etc.).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100884 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100885void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count,
886 gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100887{
888 pr_debug("%s(0x%p, %zu, %d)\n", __func__, ptr, size, min_count);
889
890 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
891 create_object((unsigned long)ptr, size, min_count, gfp);
892 else if (atomic_read(&kmemleak_early_log))
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000893 log_early(KMEMLEAK_ALLOC, ptr, size, min_count);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100894}
895EXPORT_SYMBOL_GPL(kmemleak_alloc);
896
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100897/**
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100898 * kmemleak_alloc_percpu - register a newly allocated __percpu object
899 * @ptr: __percpu pointer to beginning of the object
900 * @size: size of the object
901 *
902 * This function is called from the kernel percpu allocator when a new object
903 * (memory block) is allocated (alloc_percpu). It assumes GFP_KERNEL
904 * allocation.
905 */
906void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size)
907{
908 unsigned int cpu;
909
910 pr_debug("%s(0x%p, %zu)\n", __func__, ptr, size);
911
912 /*
913 * Percpu allocations are only scanned and not reported as leaks
914 * (min_count is set to 0).
915 */
916 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
917 for_each_possible_cpu(cpu)
918 create_object((unsigned long)per_cpu_ptr(ptr, cpu),
919 size, 0, GFP_KERNEL);
920 else if (atomic_read(&kmemleak_early_log))
921 log_early(KMEMLEAK_ALLOC_PERCPU, ptr, size, 0);
922}
923EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
924
925/**
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100926 * kmemleak_free - unregister a previously registered object
927 * @ptr: pointer to beginning of the object
928 *
929 * This function is called from the kernel allocators when an object (memory
930 * block) is freed (kmem_cache_free, kfree, vfree etc.).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100931 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100932void __ref kmemleak_free(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100933{
934 pr_debug("%s(0x%p)\n", __func__, ptr);
935
936 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
Catalin Marinas53238a62009-07-07 10:33:00 +0100937 delete_object_full((unsigned long)ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100938 else if (atomic_read(&kmemleak_early_log))
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000939 log_early(KMEMLEAK_FREE, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100940}
941EXPORT_SYMBOL_GPL(kmemleak_free);
942
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100943/**
944 * kmemleak_free_part - partially unregister a previously registered object
945 * @ptr: pointer to the beginning or inside the object. This also
946 * represents the start of the range to be freed
947 * @size: size to be unregistered
948 *
949 * This function is called when only a part of a memory block is freed
950 * (usually from the bootmem allocator).
Catalin Marinas53238a62009-07-07 10:33:00 +0100951 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100952void __ref kmemleak_free_part(const void *ptr, size_t size)
Catalin Marinas53238a62009-07-07 10:33:00 +0100953{
954 pr_debug("%s(0x%p)\n", __func__, ptr);
955
956 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
957 delete_object_part((unsigned long)ptr, size);
958 else if (atomic_read(&kmemleak_early_log))
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000959 log_early(KMEMLEAK_FREE_PART, ptr, size, 0);
Catalin Marinas53238a62009-07-07 10:33:00 +0100960}
961EXPORT_SYMBOL_GPL(kmemleak_free_part);
962
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100963/**
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100964 * kmemleak_free_percpu - unregister a previously registered __percpu object
965 * @ptr: __percpu pointer to beginning of the object
966 *
967 * This function is called from the kernel percpu allocator when an object
968 * (memory block) is freed (free_percpu).
969 */
970void __ref kmemleak_free_percpu(const void __percpu *ptr)
971{
972 unsigned int cpu;
973
974 pr_debug("%s(0x%p)\n", __func__, ptr);
975
976 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
977 for_each_possible_cpu(cpu)
978 delete_object_full((unsigned long)per_cpu_ptr(ptr,
979 cpu));
980 else if (atomic_read(&kmemleak_early_log))
981 log_early(KMEMLEAK_FREE_PERCPU, ptr, 0, 0);
982}
983EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
984
985/**
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100986 * kmemleak_not_leak - mark an allocated object as false positive
987 * @ptr: pointer to beginning of the object
988 *
989 * Calling this function on an object will cause the memory block to no longer
990 * be reported as leak and always be scanned.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100991 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100992void __ref kmemleak_not_leak(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100993{
994 pr_debug("%s(0x%p)\n", __func__, ptr);
995
996 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
997 make_gray_object((unsigned long)ptr);
998 else if (atomic_read(&kmemleak_early_log))
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000999 log_early(KMEMLEAK_NOT_LEAK, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001000}
1001EXPORT_SYMBOL(kmemleak_not_leak);
1002
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001003/**
1004 * kmemleak_ignore - ignore an allocated object
1005 * @ptr: pointer to beginning of the object
1006 *
1007 * Calling this function on an object will cause the memory block to be
1008 * ignored (not scanned and not reported as a leak). This is usually done when
1009 * it is known that the corresponding block is not a leak and does not contain
1010 * any references to other allocated memory blocks.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001011 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001012void __ref kmemleak_ignore(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001013{
1014 pr_debug("%s(0x%p)\n", __func__, ptr);
1015
1016 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
1017 make_black_object((unsigned long)ptr);
1018 else if (atomic_read(&kmemleak_early_log))
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001019 log_early(KMEMLEAK_IGNORE, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001020}
1021EXPORT_SYMBOL(kmemleak_ignore);
1022
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001023/**
1024 * kmemleak_scan_area - limit the range to be scanned in an allocated object
1025 * @ptr: pointer to beginning or inside the object. This also
1026 * represents the start of the scan area
1027 * @size: size of the scan area
1028 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
1029 *
1030 * This function is used when it is known that only certain parts of an object
1031 * contain references to other objects. Kmemleak will only scan these areas
1032 * reducing the number false negatives.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001033 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001034void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001035{
1036 pr_debug("%s(0x%p)\n", __func__, ptr);
1037
1038 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001039 add_scan_area((unsigned long)ptr, size, gfp);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001040 else if (atomic_read(&kmemleak_early_log))
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001041 log_early(KMEMLEAK_SCAN_AREA, ptr, size, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001042}
1043EXPORT_SYMBOL(kmemleak_scan_area);
1044
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001045/**
1046 * kmemleak_no_scan - do not scan an allocated object
1047 * @ptr: pointer to beginning of the object
1048 *
1049 * This function notifies kmemleak not to scan the given memory block. Useful
1050 * in situations where it is known that the given object does not contain any
1051 * references to other objects. Kmemleak will not scan such objects reducing
1052 * the number of false negatives.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001053 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001054void __ref kmemleak_no_scan(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001055{
1056 pr_debug("%s(0x%p)\n", __func__, ptr);
1057
1058 if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
1059 object_no_scan((unsigned long)ptr);
1060 else if (atomic_read(&kmemleak_early_log))
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001061 log_early(KMEMLEAK_NO_SCAN, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001062}
1063EXPORT_SYMBOL(kmemleak_no_scan);
1064
1065/*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001066 * Update an object's checksum and return true if it was modified.
1067 */
1068static bool update_checksum(struct kmemleak_object *object)
1069{
1070 u32 old_csum = object->checksum;
1071
1072 if (!kmemcheck_is_obj_initialized(object->pointer, object->size))
1073 return false;
1074
1075 object->checksum = crc32(0, (void *)object->pointer, object->size);
1076 return object->checksum != old_csum;
1077}
1078
1079/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001080 * Memory scanning is a long process and it needs to be interruptable. This
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001081 * function checks whether such interrupt condition occurred.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001082 */
1083static int scan_should_stop(void)
1084{
1085 if (!atomic_read(&kmemleak_enabled))
1086 return 1;
1087
1088 /*
1089 * This function may be called from either process or kthread context,
1090 * hence the need to check for both stop conditions.
1091 */
1092 if (current->mm)
1093 return signal_pending(current);
1094 else
1095 return kthread_should_stop();
1096
1097 return 0;
1098}
1099
1100/*
1101 * Scan a memory block (exclusive range) for valid pointers and add those
1102 * found to the gray list.
1103 */
1104static void scan_block(void *_start, void *_end,
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001105 struct kmemleak_object *scanned, int allow_resched)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001106{
1107 unsigned long *ptr;
1108 unsigned long *start = PTR_ALIGN(_start, BYTES_PER_POINTER);
1109 unsigned long *end = _end - (BYTES_PER_POINTER - 1);
1110
1111 for (ptr = start; ptr < end; ptr++) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001112 struct kmemleak_object *object;
Pekka Enberg8e019362009-08-27 14:50:00 +01001113 unsigned long flags;
1114 unsigned long pointer;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001115
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001116 if (allow_resched)
1117 cond_resched();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001118 if (scan_should_stop())
1119 break;
1120
Pekka Enberg8e019362009-08-27 14:50:00 +01001121 /* don't scan uninitialized memory */
1122 if (!kmemcheck_is_obj_initialized((unsigned long)ptr,
1123 BYTES_PER_POINTER))
1124 continue;
1125
1126 pointer = *ptr;
1127
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001128 object = find_and_get_object(pointer, 1);
1129 if (!object)
1130 continue;
1131 if (object == scanned) {
1132 /* self referenced, ignore */
1133 put_object(object);
1134 continue;
1135 }
1136
1137 /*
1138 * Avoid the lockdep recursive warning on object->lock being
1139 * previously acquired in scan_object(). These locks are
1140 * enclosed by scan_mutex.
1141 */
1142 spin_lock_irqsave_nested(&object->lock, flags,
1143 SINGLE_DEPTH_NESTING);
1144 if (!color_white(object)) {
1145 /* non-orphan, ignored or new */
1146 spin_unlock_irqrestore(&object->lock, flags);
1147 put_object(object);
1148 continue;
1149 }
1150
1151 /*
1152 * Increase the object's reference count (number of pointers
1153 * to the memory block). If this count reaches the required
1154 * minimum, the object's color will become gray and it will be
1155 * added to the gray_list.
1156 */
1157 object->count++;
Catalin Marinas0587da42009-10-28 13:33:11 +00001158 if (color_gray(object)) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001159 list_add_tail(&object->gray_list, &gray_list);
Catalin Marinas0587da42009-10-28 13:33:11 +00001160 spin_unlock_irqrestore(&object->lock, flags);
1161 continue;
1162 }
1163
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001164 spin_unlock_irqrestore(&object->lock, flags);
Catalin Marinas0587da42009-10-28 13:33:11 +00001165 put_object(object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001166 }
1167}
1168
1169/*
1170 * Scan a memory block corresponding to a kmemleak_object. A condition is
1171 * that object->use_count >= 1.
1172 */
1173static void scan_object(struct kmemleak_object *object)
1174{
1175 struct kmemleak_scan_area *area;
1176 struct hlist_node *elem;
1177 unsigned long flags;
1178
1179 /*
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001180 * Once the object->lock is acquired, the corresponding memory block
1181 * cannot be freed (the same lock is acquired in delete_object).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001182 */
1183 spin_lock_irqsave(&object->lock, flags);
1184 if (object->flags & OBJECT_NO_SCAN)
1185 goto out;
1186 if (!(object->flags & OBJECT_ALLOCATED))
1187 /* already freed object */
1188 goto out;
Catalin Marinasaf986032009-08-27 14:29:12 +01001189 if (hlist_empty(&object->area_list)) {
1190 void *start = (void *)object->pointer;
1191 void *end = (void *)(object->pointer + object->size);
1192
1193 while (start < end && (object->flags & OBJECT_ALLOCATED) &&
1194 !(object->flags & OBJECT_NO_SCAN)) {
1195 scan_block(start, min(start + MAX_SCAN_SIZE, end),
1196 object, 0);
1197 start += MAX_SCAN_SIZE;
1198
1199 spin_unlock_irqrestore(&object->lock, flags);
1200 cond_resched();
1201 spin_lock_irqsave(&object->lock, flags);
1202 }
1203 } else
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001204 hlist_for_each_entry(area, elem, &object->area_list, node)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001205 scan_block((void *)area->start,
1206 (void *)(area->start + area->size),
1207 object, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001208out:
1209 spin_unlock_irqrestore(&object->lock, flags);
1210}
1211
1212/*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001213 * Scan the objects already referenced (gray objects). More objects will be
1214 * referenced and, if there are no memory leaks, all the objects are scanned.
1215 */
1216static void scan_gray_list(void)
1217{
1218 struct kmemleak_object *object, *tmp;
1219
1220 /*
1221 * The list traversal is safe for both tail additions and removals
1222 * from inside the loop. The kmemleak objects cannot be freed from
1223 * outside the loop because their use_count was incremented.
1224 */
1225 object = list_entry(gray_list.next, typeof(*object), gray_list);
1226 while (&object->gray_list != &gray_list) {
1227 cond_resched();
1228
1229 /* may add new objects to the list */
1230 if (!scan_should_stop())
1231 scan_object(object);
1232
1233 tmp = list_entry(object->gray_list.next, typeof(*object),
1234 gray_list);
1235
1236 /* remove the object from the list and release it */
1237 list_del(&object->gray_list);
1238 put_object(object);
1239
1240 object = tmp;
1241 }
1242 WARN_ON(!list_empty(&gray_list));
1243}
1244
1245/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001246 * Scan data sections and all the referenced memory blocks allocated via the
1247 * kernel's standard allocators. This function must be called with the
1248 * scan_mutex held.
1249 */
1250static void kmemleak_scan(void)
1251{
1252 unsigned long flags;
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001253 struct kmemleak_object *object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001254 int i;
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001255 int new_leaks = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001256
Catalin Marinasacf49682009-06-26 17:38:29 +01001257 jiffies_last_scan = jiffies;
1258
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001259 /* prepare the kmemleak_object's */
1260 rcu_read_lock();
1261 list_for_each_entry_rcu(object, &object_list, object_list) {
1262 spin_lock_irqsave(&object->lock, flags);
1263#ifdef DEBUG
1264 /*
1265 * With a few exceptions there should be a maximum of
1266 * 1 reference to any object at this point.
1267 */
1268 if (atomic_read(&object->use_count) > 1) {
Joe Perchesae281062009-06-23 14:40:26 +01001269 pr_debug("object->use_count = %d\n",
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001270 atomic_read(&object->use_count));
1271 dump_object_info(object);
1272 }
1273#endif
1274 /* reset the reference count (whiten the object) */
1275 object->count = 0;
1276 if (color_gray(object) && get_object(object))
1277 list_add_tail(&object->gray_list, &gray_list);
1278
1279 spin_unlock_irqrestore(&object->lock, flags);
1280 }
1281 rcu_read_unlock();
1282
1283 /* data/bss scanning */
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001284 scan_block(_sdata, _edata, NULL, 1);
1285 scan_block(__bss_start, __bss_stop, NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001286
1287#ifdef CONFIG_SMP
1288 /* per-cpu sections scanning */
1289 for_each_possible_cpu(i)
1290 scan_block(__per_cpu_start + per_cpu_offset(i),
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001291 __per_cpu_end + per_cpu_offset(i), NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001292#endif
1293
1294 /*
1295 * Struct page scanning for each node. The code below is not yet safe
1296 * with MEMORY_HOTPLUG.
1297 */
1298 for_each_online_node(i) {
1299 pg_data_t *pgdat = NODE_DATA(i);
1300 unsigned long start_pfn = pgdat->node_start_pfn;
1301 unsigned long end_pfn = start_pfn + pgdat->node_spanned_pages;
1302 unsigned long pfn;
1303
1304 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1305 struct page *page;
1306
1307 if (!pfn_valid(pfn))
1308 continue;
1309 page = pfn_to_page(pfn);
1310 /* only scan if page is in use */
1311 if (page_count(page) == 0)
1312 continue;
Catalin Marinas4b8a9672009-07-07 10:32:56 +01001313 scan_block(page, page + 1, NULL, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001314 }
1315 }
1316
1317 /*
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001318 * Scanning the task stacks (may introduce false negatives).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001319 */
1320 if (kmemleak_stack_scan) {
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001321 struct task_struct *p, *g;
1322
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001323 read_lock(&tasklist_lock);
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001324 do_each_thread(g, p) {
1325 scan_block(task_stack_page(p), task_stack_page(p) +
1326 THREAD_SIZE, NULL, 0);
1327 } while_each_thread(g, p);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001328 read_unlock(&tasklist_lock);
1329 }
1330
1331 /*
1332 * Scan the objects already referenced from the sections scanned
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001333 * above.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001334 */
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001335 scan_gray_list();
Catalin Marinas25873622009-07-07 10:32:58 +01001336
1337 /*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001338 * Check for new or unreferenced objects modified since the previous
1339 * scan and color them gray until the next scan.
Catalin Marinas25873622009-07-07 10:32:58 +01001340 */
1341 rcu_read_lock();
1342 list_for_each_entry_rcu(object, &object_list, object_list) {
1343 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001344 if (color_white(object) && (object->flags & OBJECT_ALLOCATED)
1345 && update_checksum(object) && get_object(object)) {
1346 /* color it gray temporarily */
1347 object->count = object->min_count;
Catalin Marinas25873622009-07-07 10:32:58 +01001348 list_add_tail(&object->gray_list, &gray_list);
1349 }
1350 spin_unlock_irqrestore(&object->lock, flags);
1351 }
1352 rcu_read_unlock();
1353
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001354 /*
1355 * Re-scan the gray list for modified unreferenced objects.
1356 */
1357 scan_gray_list();
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001358
1359 /*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001360 * If scanning was stopped do not report any new unreferenced objects.
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001361 */
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001362 if (scan_should_stop())
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001363 return;
1364
1365 /*
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001366 * Scanning result reporting.
1367 */
1368 rcu_read_lock();
1369 list_for_each_entry_rcu(object, &object_list, object_list) {
1370 spin_lock_irqsave(&object->lock, flags);
1371 if (unreferenced_object(object) &&
1372 !(object->flags & OBJECT_REPORTED)) {
1373 object->flags |= OBJECT_REPORTED;
1374 new_leaks++;
1375 }
1376 spin_unlock_irqrestore(&object->lock, flags);
1377 }
1378 rcu_read_unlock();
1379
1380 if (new_leaks)
1381 pr_info("%d new suspected memory leaks (see "
1382 "/sys/kernel/debug/kmemleak)\n", new_leaks);
1383
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001384}
1385
1386/*
1387 * Thread function performing automatic memory scanning. Unreferenced objects
1388 * at the end of a memory scan are reported but only the first time.
1389 */
1390static int kmemleak_scan_thread(void *arg)
1391{
1392 static int first_run = 1;
1393
Joe Perchesae281062009-06-23 14:40:26 +01001394 pr_info("Automatic memory scanning thread started\n");
Catalin Marinasbf2a76b2009-07-07 10:32:55 +01001395 set_user_nice(current, 10);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001396
1397 /*
1398 * Wait before the first scan to allow the system to fully initialize.
1399 */
1400 if (first_run) {
1401 first_run = 0;
1402 ssleep(SECS_FIRST_SCAN);
1403 }
1404
1405 while (!kthread_should_stop()) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001406 signed long timeout = jiffies_scan_wait;
1407
1408 mutex_lock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001409 kmemleak_scan();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001410 mutex_unlock(&scan_mutex);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001411
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001412 /* wait before the next scan */
1413 while (timeout && !kthread_should_stop())
1414 timeout = schedule_timeout_interruptible(timeout);
1415 }
1416
Joe Perchesae281062009-06-23 14:40:26 +01001417 pr_info("Automatic memory scanning thread ended\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001418
1419 return 0;
1420}
1421
1422/*
1423 * Start the automatic memory scanning thread. This function must be called
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001424 * with the scan_mutex held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001425 */
Luis R. Rodriguez7eb0d5e2009-09-08 17:31:45 +01001426static void start_scan_thread(void)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001427{
1428 if (scan_thread)
1429 return;
1430 scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak");
1431 if (IS_ERR(scan_thread)) {
Joe Perchesae281062009-06-23 14:40:26 +01001432 pr_warning("Failed to create the scan thread\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001433 scan_thread = NULL;
1434 }
1435}
1436
1437/*
1438 * Stop the automatic memory scanning thread. This function must be called
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001439 * with the scan_mutex held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001440 */
Luis R. Rodriguez7eb0d5e2009-09-08 17:31:45 +01001441static void stop_scan_thread(void)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001442{
1443 if (scan_thread) {
1444 kthread_stop(scan_thread);
1445 scan_thread = NULL;
1446 }
1447}
1448
1449/*
1450 * Iterate over the object_list and return the first valid object at or after
1451 * the required position with its use_count incremented. The function triggers
1452 * a memory scanning when the pos argument points to the first position.
1453 */
1454static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
1455{
1456 struct kmemleak_object *object;
1457 loff_t n = *pos;
Catalin Marinasb87324d2009-07-07 10:32:58 +01001458 int err;
1459
1460 err = mutex_lock_interruptible(&scan_mutex);
1461 if (err < 0)
1462 return ERR_PTR(err);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001463
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001464 rcu_read_lock();
1465 list_for_each_entry_rcu(object, &object_list, object_list) {
1466 if (n-- > 0)
1467 continue;
1468 if (get_object(object))
1469 goto out;
1470 }
1471 object = NULL;
1472out:
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001473 return object;
1474}
1475
1476/*
1477 * Return the next object in the object_list. The function decrements the
1478 * use_count of the previous object and increases that of the next one.
1479 */
1480static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1481{
1482 struct kmemleak_object *prev_obj = v;
1483 struct kmemleak_object *next_obj = NULL;
1484 struct list_head *n = &prev_obj->object_list;
1485
1486 ++(*pos);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001487
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001488 list_for_each_continue_rcu(n, &object_list) {
Catalin Marinas52c3ce42011-04-27 16:44:26 +01001489 struct kmemleak_object *obj =
1490 list_entry(n, struct kmemleak_object, object_list);
1491 if (get_object(obj)) {
1492 next_obj = obj;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001493 break;
Catalin Marinas52c3ce42011-04-27 16:44:26 +01001494 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001495 }
Catalin Marinas288c8572009-07-07 10:32:57 +01001496
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001497 put_object(prev_obj);
1498 return next_obj;
1499}
1500
1501/*
1502 * Decrement the use_count of the last object required, if any.
1503 */
1504static void kmemleak_seq_stop(struct seq_file *seq, void *v)
1505{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001506 if (!IS_ERR(v)) {
1507 /*
1508 * kmemleak_seq_start may return ERR_PTR if the scan_mutex
1509 * waiting was interrupted, so only release it if !IS_ERR.
1510 */
Catalin Marinasf5886c72009-07-29 16:26:57 +01001511 rcu_read_unlock();
Catalin Marinasb87324d2009-07-07 10:32:58 +01001512 mutex_unlock(&scan_mutex);
1513 if (v)
1514 put_object(v);
1515 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001516}
1517
1518/*
1519 * Print the information for an unreferenced object to the seq file.
1520 */
1521static int kmemleak_seq_show(struct seq_file *seq, void *v)
1522{
1523 struct kmemleak_object *object = v;
1524 unsigned long flags;
1525
1526 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas288c8572009-07-07 10:32:57 +01001527 if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object))
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001528 print_unreferenced(seq, object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001529 spin_unlock_irqrestore(&object->lock, flags);
1530 return 0;
1531}
1532
1533static const struct seq_operations kmemleak_seq_ops = {
1534 .start = kmemleak_seq_start,
1535 .next = kmemleak_seq_next,
1536 .stop = kmemleak_seq_stop,
1537 .show = kmemleak_seq_show,
1538};
1539
1540static int kmemleak_open(struct inode *inode, struct file *file)
1541{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001542 return seq_open(file, &kmemleak_seq_ops);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001543}
1544
1545static int kmemleak_release(struct inode *inode, struct file *file)
1546{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001547 return seq_release(inode, file);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001548}
1549
Catalin Marinas189d84e2009-08-27 14:29:15 +01001550static int dump_str_object_info(const char *str)
1551{
1552 unsigned long flags;
1553 struct kmemleak_object *object;
1554 unsigned long addr;
1555
1556 addr= simple_strtoul(str, NULL, 0);
1557 object = find_and_get_object(addr, 0);
1558 if (!object) {
1559 pr_info("Unknown object at 0x%08lx\n", addr);
1560 return -EINVAL;
1561 }
1562
1563 spin_lock_irqsave(&object->lock, flags);
1564 dump_object_info(object);
1565 spin_unlock_irqrestore(&object->lock, flags);
1566
1567 put_object(object);
1568 return 0;
1569}
1570
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001571/*
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001572 * We use grey instead of black to ensure we can do future scans on the same
1573 * objects. If we did not do future scans these black objects could
1574 * potentially contain references to newly allocated objects in the future and
1575 * we'd end up with false positives.
1576 */
1577static void kmemleak_clear(void)
1578{
1579 struct kmemleak_object *object;
1580 unsigned long flags;
1581
1582 rcu_read_lock();
1583 list_for_each_entry_rcu(object, &object_list, object_list) {
1584 spin_lock_irqsave(&object->lock, flags);
1585 if ((object->flags & OBJECT_REPORTED) &&
1586 unreferenced_object(object))
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -07001587 __paint_it(object, KMEMLEAK_GREY);
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001588 spin_unlock_irqrestore(&object->lock, flags);
1589 }
1590 rcu_read_unlock();
1591}
1592
1593/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001594 * File write operation to configure kmemleak at run-time. The following
1595 * commands can be written to the /sys/kernel/debug/kmemleak file:
1596 * off - disable kmemleak (irreversible)
1597 * stack=on - enable the task stacks scanning
1598 * stack=off - disable the tasks stacks scanning
1599 * scan=on - start the automatic memory scanning thread
1600 * scan=off - stop the automatic memory scanning thread
1601 * scan=... - set the automatic memory scanning period in seconds (0 to
1602 * disable it)
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001603 * scan - trigger a memory scan
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001604 * clear - mark all current reported unreferenced kmemleak objects as
1605 * grey to ignore printing them
Catalin Marinas189d84e2009-08-27 14:29:15 +01001606 * dump=... - dump information about the object found at the given address
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001607 */
1608static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
1609 size_t size, loff_t *ppos)
1610{
1611 char buf[64];
1612 int buf_size;
Catalin Marinasb87324d2009-07-07 10:32:58 +01001613 int ret;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001614
Catalin Marinas74341702011-09-29 11:50:07 +01001615 if (!atomic_read(&kmemleak_enabled))
1616 return -EBUSY;
1617
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001618 buf_size = min(size, (sizeof(buf) - 1));
1619 if (strncpy_from_user(buf, user_buf, buf_size) < 0)
1620 return -EFAULT;
1621 buf[buf_size] = 0;
1622
Catalin Marinasb87324d2009-07-07 10:32:58 +01001623 ret = mutex_lock_interruptible(&scan_mutex);
1624 if (ret < 0)
1625 return ret;
1626
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001627 if (strncmp(buf, "off", 3) == 0)
1628 kmemleak_disable();
1629 else if (strncmp(buf, "stack=on", 8) == 0)
1630 kmemleak_stack_scan = 1;
1631 else if (strncmp(buf, "stack=off", 9) == 0)
1632 kmemleak_stack_scan = 0;
1633 else if (strncmp(buf, "scan=on", 7) == 0)
1634 start_scan_thread();
1635 else if (strncmp(buf, "scan=off", 8) == 0)
1636 stop_scan_thread();
1637 else if (strncmp(buf, "scan=", 5) == 0) {
1638 unsigned long secs;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001639
Catalin Marinasb87324d2009-07-07 10:32:58 +01001640 ret = strict_strtoul(buf + 5, 0, &secs);
1641 if (ret < 0)
1642 goto out;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001643 stop_scan_thread();
1644 if (secs) {
1645 jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
1646 start_scan_thread();
1647 }
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001648 } else if (strncmp(buf, "scan", 4) == 0)
1649 kmemleak_scan();
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001650 else if (strncmp(buf, "clear", 5) == 0)
1651 kmemleak_clear();
Catalin Marinas189d84e2009-08-27 14:29:15 +01001652 else if (strncmp(buf, "dump=", 5) == 0)
1653 ret = dump_str_object_info(buf + 5);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001654 else
Catalin Marinasb87324d2009-07-07 10:32:58 +01001655 ret = -EINVAL;
1656
1657out:
1658 mutex_unlock(&scan_mutex);
1659 if (ret < 0)
1660 return ret;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001661
1662 /* ignore the rest of the buffer, only one command at a time */
1663 *ppos += size;
1664 return size;
1665}
1666
1667static const struct file_operations kmemleak_fops = {
1668 .owner = THIS_MODULE,
1669 .open = kmemleak_open,
1670 .read = seq_read,
1671 .write = kmemleak_write,
1672 .llseek = seq_lseek,
1673 .release = kmemleak_release,
1674};
1675
1676/*
Catalin Marinas74341702011-09-29 11:50:07 +01001677 * Stop the memory scanning thread and free the kmemleak internal objects if
1678 * no previous scan thread (otherwise, kmemleak may still have some useful
1679 * information on memory leaks).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001680 */
Catalin Marinas179a8102009-09-07 10:14:42 +01001681static void kmemleak_do_cleanup(struct work_struct *work)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001682{
1683 struct kmemleak_object *object;
Catalin Marinas74341702011-09-29 11:50:07 +01001684 bool cleanup = scan_thread == NULL;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001685
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001686 mutex_lock(&scan_mutex);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001687 stop_scan_thread();
1688
Catalin Marinas74341702011-09-29 11:50:07 +01001689 if (cleanup) {
1690 rcu_read_lock();
1691 list_for_each_entry_rcu(object, &object_list, object_list)
1692 delete_object_full(object->pointer);
1693 rcu_read_unlock();
1694 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001695 mutex_unlock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001696}
1697
Catalin Marinas179a8102009-09-07 10:14:42 +01001698static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001699
1700/*
1701 * Disable kmemleak. No memory allocation/freeing will be traced once this
1702 * function is called. Disabling kmemleak is an irreversible operation.
1703 */
1704static void kmemleak_disable(void)
1705{
1706 /* atomically check whether it was already invoked */
1707 if (atomic_cmpxchg(&kmemleak_error, 0, 1))
1708 return;
1709
1710 /* stop any memory operation tracing */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001711 atomic_set(&kmemleak_enabled, 0);
1712
1713 /* check whether it is too early for a kernel thread */
1714 if (atomic_read(&kmemleak_initialized))
Catalin Marinas179a8102009-09-07 10:14:42 +01001715 schedule_work(&cleanup_work);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001716
1717 pr_info("Kernel memory leak detector disabled\n");
1718}
1719
1720/*
1721 * Allow boot-time kmemleak disabling (enabled by default).
1722 */
1723static int kmemleak_boot_config(char *str)
1724{
1725 if (!str)
1726 return -EINVAL;
1727 if (strcmp(str, "off") == 0)
1728 kmemleak_disable();
Jason Baronab0155a2010-07-19 11:54:17 +01001729 else if (strcmp(str, "on") == 0)
1730 kmemleak_skip_disable = 1;
1731 else
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001732 return -EINVAL;
1733 return 0;
1734}
1735early_param("kmemleak", kmemleak_boot_config);
1736
Catalin Marinas5f790202011-09-28 12:17:03 +01001737static void __init print_log_trace(struct early_log *log)
1738{
1739 struct stack_trace trace;
1740
1741 trace.nr_entries = log->trace_len;
1742 trace.entries = log->trace;
1743
1744 pr_notice("Early log backtrace:\n");
1745 print_stack_trace(&trace, 2);
1746}
1747
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001748/*
Catalin Marinas20301172009-06-17 18:29:04 +01001749 * Kmemleak initialization.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001750 */
1751void __init kmemleak_init(void)
1752{
1753 int i;
1754 unsigned long flags;
1755
Jason Baronab0155a2010-07-19 11:54:17 +01001756#ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF
1757 if (!kmemleak_skip_disable) {
1758 kmemleak_disable();
1759 return;
1760 }
1761#endif
1762
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001763 jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE);
1764 jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000);
1765
1766 object_cache = KMEM_CACHE(kmemleak_object, SLAB_NOLEAKTRACE);
1767 scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE);
1768 INIT_PRIO_TREE_ROOT(&object_tree_root);
1769
Catalin Marinasb6693002011-09-28 17:22:56 +01001770 if (crt_early_log >= ARRAY_SIZE(early_log))
1771 pr_warning("Early log buffer exceeded (%d), please increase "
1772 "DEBUG_KMEMLEAK_EARLY_LOG_SIZE\n", crt_early_log);
1773
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001774 /* the kernel is still in UP mode, so disabling the IRQs is enough */
1775 local_irq_save(flags);
Catalin Marinasb6693002011-09-28 17:22:56 +01001776 atomic_set(&kmemleak_early_log, 0);
1777 if (atomic_read(&kmemleak_error)) {
1778 local_irq_restore(flags);
1779 return;
1780 } else
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001781 atomic_set(&kmemleak_enabled, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001782 local_irq_restore(flags);
1783
1784 /*
1785 * This is the point where tracking allocations is safe. Automatic
1786 * scanning is started during the late initcall. Add the early logged
1787 * callbacks to the kmemleak infrastructure.
1788 */
1789 for (i = 0; i < crt_early_log; i++) {
1790 struct early_log *log = &early_log[i];
1791
1792 switch (log->op_type) {
1793 case KMEMLEAK_ALLOC:
Catalin Marinasfd678962009-08-27 14:29:17 +01001794 early_alloc(log);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001795 break;
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001796 case KMEMLEAK_ALLOC_PERCPU:
1797 early_alloc_percpu(log);
1798 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001799 case KMEMLEAK_FREE:
1800 kmemleak_free(log->ptr);
1801 break;
Catalin Marinas53238a62009-07-07 10:33:00 +01001802 case KMEMLEAK_FREE_PART:
1803 kmemleak_free_part(log->ptr, log->size);
1804 break;
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001805 case KMEMLEAK_FREE_PERCPU:
1806 kmemleak_free_percpu(log->ptr);
1807 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001808 case KMEMLEAK_NOT_LEAK:
1809 kmemleak_not_leak(log->ptr);
1810 break;
1811 case KMEMLEAK_IGNORE:
1812 kmemleak_ignore(log->ptr);
1813 break;
1814 case KMEMLEAK_SCAN_AREA:
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001815 kmemleak_scan_area(log->ptr, log->size, GFP_KERNEL);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001816 break;
1817 case KMEMLEAK_NO_SCAN:
1818 kmemleak_no_scan(log->ptr);
1819 break;
1820 default:
Catalin Marinas5f790202011-09-28 12:17:03 +01001821 kmemleak_warn("Unknown early log operation: %d\n",
1822 log->op_type);
1823 }
1824
1825 if (atomic_read(&kmemleak_warning)) {
1826 print_log_trace(log);
1827 atomic_set(&kmemleak_warning, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001828 }
1829 }
1830}
1831
1832/*
1833 * Late initialization function.
1834 */
1835static int __init kmemleak_late_init(void)
1836{
1837 struct dentry *dentry;
1838
1839 atomic_set(&kmemleak_initialized, 1);
1840
1841 if (atomic_read(&kmemleak_error)) {
1842 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001843 * Some error occurred and kmemleak was disabled. There is a
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001844 * small chance that kmemleak_disable() was called immediately
1845 * after setting kmemleak_initialized and we may end up with
1846 * two clean-up threads but serialized by scan_mutex.
1847 */
Catalin Marinas179a8102009-09-07 10:14:42 +01001848 schedule_work(&cleanup_work);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001849 return -ENOMEM;
1850 }
1851
1852 dentry = debugfs_create_file("kmemleak", S_IRUGO, NULL, NULL,
1853 &kmemleak_fops);
1854 if (!dentry)
Joe Perchesae281062009-06-23 14:40:26 +01001855 pr_warning("Failed to create the debugfs kmemleak file\n");
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001856 mutex_lock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001857 start_scan_thread();
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001858 mutex_unlock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001859
1860 pr_info("Kernel memory leak detector initialized\n");
1861
1862 return 0;
1863}
1864late_initcall(kmemleak_late_init);