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