blob: 2fb86524d70bcc95e8ced2aae7a767355e4edb1c [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002/*
3 * mm/kmemleak.c
4 *
5 * Copyright (C) 2008 ARM Limited
6 * Written by Catalin Marinas <catalin.marinas@arm.com>
7 *
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01008 * For more information on the algorithm and kmemleak usage, please see
Andreas Platschek22901c62016-12-12 16:42:01 -08009 * Documentation/dev-tools/kmemleak.rst.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010010 *
11 * Notes on locking
12 * ----------------
13 *
14 * The following locks and mutexes are used by kmemleak:
15 *
16 * - kmemleak_lock (rwlock): protects the object_list modifications and
17 * accesses to the object_tree_root. The object_list is the main list
18 * holding the metadata (struct kmemleak_object) for the allocated memory
Michel Lespinasse85d3a312012-10-08 16:31:27 -070019 * blocks. The object_tree_root is a red black tree used to look-up
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010020 * metadata based on a pointer to the corresponding memory block. The
21 * kmemleak_object structures are added to the object_list and
22 * object_tree_root in the create_object() function called from the
23 * kmemleak_alloc() callback and removed in delete_object() called from the
24 * kmemleak_free() callback
25 * - kmemleak_object.lock (spinlock): protects a kmemleak_object. Accesses to
26 * the metadata (e.g. count) are protected by this lock. Note that some
27 * members of this structure may be protected by other means (atomic or
28 * kmemleak_lock). This lock is also held when scanning the corresponding
29 * memory block to avoid the kernel freeing it via the kmemleak_free()
30 * callback. This is less heavyweight than holding a global lock like
31 * kmemleak_lock during scanning
32 * - scan_mutex (mutex): ensures that only one thread may scan the memory for
33 * unreferenced objects at a time. The gray_list contains the objects which
34 * are already referenced or marked as false positives and need to be
35 * scanned. This list is only modified during a scanning episode when the
36 * scan_mutex is held. At the end of a scan, the gray_list is always empty.
37 * Note that the kmemleak_object.use_count is incremented when an object is
Catalin Marinas4698c1f2009-06-26 17:38:27 +010038 * added to the gray_list and therefore cannot be freed. This mutex also
39 * prevents multiple users of the "kmemleak" debugfs file together with
40 * modifications to the memory scanning parameters including the scan_thread
41 * pointer
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010042 *
Catalin Marinas93ada572015-06-24 16:58:37 -070043 * Locks and mutexes are acquired/nested in the following order:
Catalin Marinas9d5a4c72015-06-24 16:58:34 -070044 *
Catalin Marinas93ada572015-06-24 16:58:37 -070045 * scan_mutex [-> object->lock] -> kmemleak_lock -> other_object->lock (SINGLE_DEPTH_NESTING)
46 *
47 * No kmemleak_lock and object->lock nesting is allowed outside scan_mutex
48 * regions.
Catalin Marinas9d5a4c72015-06-24 16:58:34 -070049 *
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010050 * The kmemleak_object structures have a use_count incremented or decremented
51 * using the get_object()/put_object() functions. When the use_count becomes
52 * 0, this count can no longer be incremented and put_object() schedules the
53 * kmemleak_object freeing via an RCU callback. All calls to the get_object()
54 * function must be protected by rcu_read_lock() to avoid accessing a freed
55 * structure.
56 */
57
Joe Perchesae281062009-06-23 14:40:26 +010058#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
59
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010060#include <linux/init.h>
61#include <linux/kernel.h>
62#include <linux/list.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010063#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010064#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010065#include <linux/sched/task_stack.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010066#include <linux/jiffies.h>
67#include <linux/delay.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040068#include <linux/export.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010069#include <linux/kthread.h>
Michel Lespinasse85d3a312012-10-08 16:31:27 -070070#include <linux/rbtree.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010071#include <linux/fs.h>
72#include <linux/debugfs.h>
73#include <linux/seq_file.h>
74#include <linux/cpumask.h>
75#include <linux/spinlock.h>
Vincent Whitchurch154221c2018-10-26 15:03:42 -070076#include <linux/module.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010077#include <linux/mutex.h>
78#include <linux/rcupdate.h>
79#include <linux/stacktrace.h>
80#include <linux/cache.h>
81#include <linux/percpu.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070082#include <linux/memblock.h>
Catalin Marinas9099dae2016-10-11 13:55:11 -070083#include <linux/pfn.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010084#include <linux/mmzone.h>
85#include <linux/slab.h>
86#include <linux/thread_info.h>
87#include <linux/err.h>
88#include <linux/uaccess.h>
89#include <linux/string.h>
90#include <linux/nodemask.h>
91#include <linux/mm.h>
Catalin Marinas179a8102009-09-07 10:14:42 +010092#include <linux/workqueue.h>
Catalin Marinas04609ccc2009-10-28 13:33:12 +000093#include <linux/crc32.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010094
95#include <asm/sections.h>
96#include <asm/processor.h>
Arun Sharma600634972011-07-26 16:09:06 -070097#include <linux/atomic.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +010098
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -080099#include <linux/kasan.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100100#include <linux/kmemleak.h>
Laura Abbott029aeff2011-11-15 23:49:09 +0000101#include <linux/memory_hotplug.h>
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100102
103/*
104 * Kmemleak configuration and common defines.
105 */
106#define MAX_TRACE 16 /* stack trace length */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100107#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100108#define SECS_FIRST_SCAN 60 /* delay before the first scan */
109#define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */
Catalin Marinasaf986032009-08-27 14:29:12 +0100110#define MAX_SCAN_SIZE 4096 /* maximum size of a scanned block */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100111
112#define BYTES_PER_POINTER sizeof(void *)
113
Catalin Marinas216c04b2009-06-17 18:29:02 +0100114/* GFP bitmask for kmemleak internal allocations */
Vladimir Davydov20b5c302016-01-14 15:18:08 -0800115#define gfp_kmemleak_mask(gfp) (((gfp) & (GFP_KERNEL | GFP_ATOMIC)) | \
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000116 __GFP_NORETRY | __GFP_NOMEMALLOC | \
Yang Shidf9576d2019-08-02 21:48:37 -0700117 __GFP_NOWARN)
Catalin Marinas216c04b2009-06-17 18:29:02 +0100118
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100119/* scanning area inside a memory block */
120struct kmemleak_scan_area {
121 struct hlist_node node;
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000122 unsigned long start;
123 size_t size;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100124};
125
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700126#define KMEMLEAK_GREY 0
127#define KMEMLEAK_BLACK -1
128
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100129/*
130 * Structure holding the metadata for each allocated memory block.
131 * Modifications to such objects should be made while holding the
132 * object->lock. Insertions or deletions from object_list, gray_list or
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700133 * rb_node are already protected by the corresponding locks or mutex (see
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100134 * the notes on locking above). These objects are reference-counted
135 * (use_count) and freed using the RCU mechanism.
136 */
137struct kmemleak_object {
138 spinlock_t lock;
Catalin Marinasf66abf02017-07-06 15:40:16 -0700139 unsigned int flags; /* object status flags */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100140 struct list_head object_list;
141 struct list_head gray_list;
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700142 struct rb_node rb_node;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100143 struct rcu_head rcu; /* object_list lockless traversal */
144 /* object usage count; object freed when use_count == 0 */
145 atomic_t use_count;
146 unsigned long pointer;
147 size_t size;
Catalin Marinas94f4a162017-07-06 15:40:22 -0700148 /* pass surplus references to this pointer */
149 unsigned long excess_ref;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100150 /* minimum number of a pointers found before it is considered leak */
151 int min_count;
152 /* the total number of pointers found pointing to this object */
153 int count;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000154 /* checksum for detecting modified objects */
155 u32 checksum;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100156 /* memory ranges to be scanned inside an object (empty for all) */
157 struct hlist_head area_list;
158 unsigned long trace[MAX_TRACE];
159 unsigned int trace_len;
160 unsigned long jiffies; /* creation timestamp */
161 pid_t pid; /* pid of the current task */
162 char comm[TASK_COMM_LEN]; /* executable name */
163};
164
165/* flag representing the memory block allocation status */
166#define OBJECT_ALLOCATED (1 << 0)
167/* flag set after the first reporting of an unreference object */
168#define OBJECT_REPORTED (1 << 1)
169/* flag set to not scan the object */
170#define OBJECT_NO_SCAN (1 << 2)
Catalin Marinasdba82d92019-09-23 15:33:59 -0700171/* flag set to fully scan the object when scan_area allocation failed */
172#define OBJECT_FULL_SCAN (1 << 3)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100173
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700174#define HEX_PREFIX " "
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100175/* number of bytes to print per line; must be 16 or 32 */
176#define HEX_ROW_SIZE 16
177/* number of bytes to print at a time (1, 2, 4, 8) */
178#define HEX_GROUP_SIZE 1
179/* include ASCII after the hex output */
180#define HEX_ASCII 1
181/* max number of lines to be printed */
182#define HEX_MAX_LINES 2
Catalin Marinas06473982019-09-23 15:34:02 -0700183/* memory pool size */
184#define MEM_POOL_SIZE 16000
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100185
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100186/* the list of all allocated objects */
187static LIST_HEAD(object_list);
188/* the list of gray-colored objects (see color_gray comment below) */
189static LIST_HEAD(gray_list);
Catalin Marinas06473982019-09-23 15:34:02 -0700190/* memory pool allocation */
191static struct kmemleak_object mem_pool[MEM_POOL_SIZE];
192static int mem_pool_free_count = ARRAY_SIZE(mem_pool);
193static LIST_HEAD(mem_pool_free_list);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700194/* search tree for object boundaries */
195static struct rb_root object_tree_root = RB_ROOT;
196/* rw_lock protecting the access to object_list and object_tree_root */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100197static DEFINE_RWLOCK(kmemleak_lock);
198
199/* allocation caches for kmemleak internal data */
200static struct kmem_cache *object_cache;
201static struct kmem_cache *scan_area_cache;
202
203/* set if tracing memory operations is enabled */
Li Zefan8910ae892014-04-03 14:46:29 -0700204static int kmemleak_enabled;
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -0700205/* same as above but only for the kmemleak_free() callback */
206static int kmemleak_free_enabled;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100207/* set in the late_initcall if there were no errors */
Li Zefan8910ae892014-04-03 14:46:29 -0700208static int kmemleak_initialized;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100209/* enables or disables early logging of the memory operations */
Li Zefan8910ae892014-04-03 14:46:29 -0700210static int kmemleak_early_log = 1;
Catalin Marinas5f790202011-09-28 12:17:03 +0100211/* set if a kmemleak warning was issued */
Li Zefan8910ae892014-04-03 14:46:29 -0700212static int kmemleak_warning;
Catalin Marinas5f790202011-09-28 12:17:03 +0100213/* set if a fatal kmemleak error has occurred */
Li Zefan8910ae892014-04-03 14:46:29 -0700214static int kmemleak_error;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100215
216/* minimum and maximum address that may be valid pointers */
217static unsigned long min_addr = ULONG_MAX;
218static unsigned long max_addr;
219
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100220static struct task_struct *scan_thread;
Catalin Marinasacf49682009-06-26 17:38:29 +0100221/* used to avoid reporting of recently allocated objects */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100222static unsigned long jiffies_min_age;
Catalin Marinasacf49682009-06-26 17:38:29 +0100223static unsigned long jiffies_last_scan;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100224/* delay between automatic memory scannings */
225static signed long jiffies_scan_wait;
226/* enables or disables the task stacks scanning */
Catalin Marinase0a2a162009-06-26 17:38:25 +0100227static int kmemleak_stack_scan = 1;
Catalin Marinas4698c1f2009-06-26 17:38:27 +0100228/* protects the memory scanning, parameters and debug/kmemleak file access */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100229static DEFINE_MUTEX(scan_mutex);
Jason Baronab0155a2010-07-19 11:54:17 +0100230/* setting kmemleak=on, will set this var, skipping the disable */
231static int kmemleak_skip_disable;
Li Zefandc9b3f42014-04-03 14:46:26 -0700232/* If there are leaks that can be reported */
233static bool kmemleak_found_leaks;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100234
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700235static bool kmemleak_verbose;
236module_param_named(verbose, kmemleak_verbose, bool, 0600);
237
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100238/*
Catalin Marinas20301172009-06-17 18:29:04 +0100239 * Early object allocation/freeing logging. Kmemleak is initialized after the
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100240 * kernel allocator. However, both the kernel allocator and kmemleak may
Catalin Marinas20301172009-06-17 18:29:04 +0100241 * allocate memory blocks which need to be tracked. Kmemleak defines an
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100242 * arbitrary buffer to hold the allocation/freeing information before it is
243 * fully initialized.
244 */
245
246/* kmemleak operation type for early logging */
247enum {
248 KMEMLEAK_ALLOC,
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100249 KMEMLEAK_ALLOC_PERCPU,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100250 KMEMLEAK_FREE,
Catalin Marinas53238a62009-07-07 10:33:00 +0100251 KMEMLEAK_FREE_PART,
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100252 KMEMLEAK_FREE_PERCPU,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100253 KMEMLEAK_NOT_LEAK,
254 KMEMLEAK_IGNORE,
255 KMEMLEAK_SCAN_AREA,
Catalin Marinas94f4a162017-07-06 15:40:22 -0700256 KMEMLEAK_NO_SCAN,
257 KMEMLEAK_SET_EXCESS_REF
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100258};
259
260/*
261 * Structure holding the information passed to kmemleak callbacks during the
262 * early logging.
263 */
264struct early_log {
265 int op_type; /* kmemleak operation type */
Catalin Marinasf66abf02017-07-06 15:40:16 -0700266 int min_count; /* minimum reference count */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100267 const void *ptr; /* allocated/freed memory block */
Catalin Marinas94f4a162017-07-06 15:40:22 -0700268 union {
269 size_t size; /* memory block size */
270 unsigned long excess_ref; /* surplus reference passing */
271 };
Catalin Marinasfd678962009-08-27 14:29:17 +0100272 unsigned long trace[MAX_TRACE]; /* stack trace */
273 unsigned int trace_len; /* stack trace length */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100274};
275
276/* early logging buffer and current position */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100277static struct early_log
278 early_log[CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE] __initdata;
279static int crt_early_log __initdata;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100280
281static void kmemleak_disable(void);
282
283/*
284 * Print a warning and dump the stack trace.
285 */
Catalin Marinas5f790202011-09-28 12:17:03 +0100286#define kmemleak_warn(x...) do { \
Joe Perches598d8092016-03-17 14:19:44 -0700287 pr_warn(x); \
Catalin Marinas5f790202011-09-28 12:17:03 +0100288 dump_stack(); \
Li Zefan8910ae892014-04-03 14:46:29 -0700289 kmemleak_warning = 1; \
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100290} while (0)
291
292/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300293 * Macro invoked when a serious kmemleak condition occurred and cannot be
Catalin Marinas20301172009-06-17 18:29:04 +0100294 * recovered from. Kmemleak will be disabled and further allocation/freeing
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100295 * tracing no longer available.
296 */
Catalin Marinas000814f2009-06-17 18:29:03 +0100297#define kmemleak_stop(x...) do { \
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100298 kmemleak_warn(x); \
299 kmemleak_disable(); \
300} while (0)
301
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700302#define warn_or_seq_printf(seq, fmt, ...) do { \
303 if (seq) \
304 seq_printf(seq, fmt, ##__VA_ARGS__); \
305 else \
306 pr_warn(fmt, ##__VA_ARGS__); \
307} while (0)
308
309static void warn_or_seq_hex_dump(struct seq_file *seq, int prefix_type,
310 int rowsize, int groupsize, const void *buf,
311 size_t len, bool ascii)
312{
313 if (seq)
314 seq_hex_dump(seq, HEX_PREFIX, prefix_type, rowsize, groupsize,
315 buf, len, ascii);
316 else
317 print_hex_dump(KERN_WARNING, pr_fmt(HEX_PREFIX), prefix_type,
318 rowsize, groupsize, buf, len, ascii);
319}
320
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100321/*
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100322 * Printing of the objects hex dump to the seq file. The number of lines to be
323 * printed is limited to HEX_MAX_LINES to prevent seq file spamming. The
324 * actual number of printed bytes depends on HEX_ROW_SIZE. It must be called
325 * with the object->lock held.
326 */
327static void hex_dump_object(struct seq_file *seq,
328 struct kmemleak_object *object)
329{
330 const u8 *ptr = (const u8 *)object->pointer;
Andy Shevchenko6fc37c42015-09-09 15:38:45 -0700331 size_t len;
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100332
333 /* limit the number of lines to HEX_MAX_LINES */
Andy Shevchenko6fc37c42015-09-09 15:38:45 -0700334 len = min_t(size_t, object->size, HEX_MAX_LINES * HEX_ROW_SIZE);
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100335
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700336 warn_or_seq_printf(seq, " hex dump (first %zu bytes):\n", len);
Dmitry Vyukov5c335fe2016-06-24 14:50:07 -0700337 kasan_disable_current();
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700338 warn_or_seq_hex_dump(seq, DUMP_PREFIX_NONE, HEX_ROW_SIZE,
339 HEX_GROUP_SIZE, ptr, len, HEX_ASCII);
Dmitry Vyukov5c335fe2016-06-24 14:50:07 -0700340 kasan_enable_current();
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100341}
342
343/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100344 * Object colors, encoded with count and min_count:
345 * - white - orphan object, not enough references to it (count < min_count)
346 * - gray - not orphan, not marked as false positive (min_count == 0) or
347 * sufficient references to it (count >= min_count)
348 * - black - ignore, it doesn't contain references (e.g. text section)
349 * (min_count == -1). No function defined for this color.
350 * Newly created objects don't have any color assigned (object->count == -1)
351 * before the next memory scan when they become white.
352 */
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100353static bool color_white(const struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100354{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700355 return object->count != KMEMLEAK_BLACK &&
356 object->count < object->min_count;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100357}
358
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100359static bool color_gray(const struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100360{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700361 return object->min_count != KMEMLEAK_BLACK &&
362 object->count >= object->min_count;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100363}
364
365/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100366 * Objects are considered unreferenced only if their color is white, they have
367 * not be deleted and have a minimum age to avoid false positives caused by
368 * pointers temporarily stored in CPU registers.
369 */
Luis R. Rodriguez4a558dd2009-09-08 16:34:50 +0100370static bool unreferenced_object(struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100371{
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000372 return (color_white(object) && object->flags & OBJECT_ALLOCATED) &&
Catalin Marinasacf49682009-06-26 17:38:29 +0100373 time_before_eq(object->jiffies + jiffies_min_age,
374 jiffies_last_scan);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100375}
376
377/*
Catalin Marinasbab4a342009-06-26 17:38:26 +0100378 * Printing of the unreferenced objects information to the seq file. The
379 * print_unreferenced function must be called with the object->lock held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100380 */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100381static void print_unreferenced(struct seq_file *seq,
382 struct kmemleak_object *object)
383{
384 int i;
Catalin Marinasfefdd332009-10-28 13:33:12 +0000385 unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100386
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700387 warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
Catalin Marinasbab4a342009-06-26 17:38:26 +0100388 object->pointer, object->size);
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700389 warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n",
Catalin Marinasfefdd332009-10-28 13:33:12 +0000390 object->comm, object->pid, object->jiffies,
391 msecs_age / 1000, msecs_age % 1000);
Sergey Senozhatsky0494e082009-08-27 14:29:18 +0100392 hex_dump_object(seq, object);
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700393 warn_or_seq_printf(seq, " backtrace:\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100394
395 for (i = 0; i < object->trace_len; i++) {
396 void *ptr = (void *)object->trace[i];
Vincent Whitchurch154221c2018-10-26 15:03:42 -0700397 warn_or_seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100398 }
399}
400
401/*
402 * Print the kmemleak_object information. This function is used mainly for
403 * debugging special cases when kmemleak operations. It must be called with
404 * the object->lock held.
405 */
406static void dump_object_info(struct kmemleak_object *object)
407{
Joe Perchesae281062009-06-23 14:40:26 +0100408 pr_notice("Object 0x%08lx (size %zu):\n",
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700409 object->pointer, object->size);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100410 pr_notice(" comm \"%s\", pid %d, jiffies %lu\n",
411 object->comm, object->pid, object->jiffies);
412 pr_notice(" min_count = %d\n", object->min_count);
413 pr_notice(" count = %d\n", object->count);
Catalin Marinasf66abf02017-07-06 15:40:16 -0700414 pr_notice(" flags = 0x%x\n", object->flags);
Jianpeng Maaae0ad72014-06-06 14:38:16 -0700415 pr_notice(" checksum = %u\n", object->checksum);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100416 pr_notice(" backtrace:\n");
Thomas Gleixner07984aa2019-04-25 11:45:01 +0200417 stack_trace_print(object->trace, object->trace_len, 4);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100418}
419
420/*
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700421 * Look-up a memory block metadata (kmemleak_object) in the object search
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100422 * tree based on a pointer value. If alias is 0, only values pointing to the
423 * beginning of the memory block are allowed. The kmemleak_lock must be held
424 * when calling this function.
425 */
426static struct kmemleak_object *lookup_object(unsigned long ptr, int alias)
427{
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700428 struct rb_node *rb = object_tree_root.rb_node;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100429
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700430 while (rb) {
431 struct kmemleak_object *object =
432 rb_entry(rb, struct kmemleak_object, rb_node);
433 if (ptr < object->pointer)
434 rb = object->rb_node.rb_left;
435 else if (object->pointer + object->size <= ptr)
436 rb = object->rb_node.rb_right;
437 else if (object->pointer == ptr || alias)
438 return object;
439 else {
Catalin Marinas5f790202011-09-28 12:17:03 +0100440 kmemleak_warn("Found object by alias at 0x%08lx\n",
441 ptr);
Catalin Marinasa7686a42010-07-19 11:54:16 +0100442 dump_object_info(object);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700443 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100444 }
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700445 }
446 return NULL;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100447}
448
449/*
450 * Increment the object use_count. Return 1 if successful or 0 otherwise. Note
451 * that once an object's use_count reached 0, the RCU freeing was already
452 * registered and the object should no longer be used. This function must be
453 * called under the protection of rcu_read_lock().
454 */
455static int get_object(struct kmemleak_object *object)
456{
457 return atomic_inc_not_zero(&object->use_count);
458}
459
460/*
Catalin Marinas06473982019-09-23 15:34:02 -0700461 * Memory pool allocation and freeing. kmemleak_lock must not be held.
462 */
463static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
464{
465 unsigned long flags;
466 struct kmemleak_object *object;
467
468 /* try the slab allocator first */
469 object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
470 if (object)
471 return object;
472
473 /* slab allocation failed, try the memory pool */
474 write_lock_irqsave(&kmemleak_lock, flags);
475 object = list_first_entry_or_null(&mem_pool_free_list,
476 typeof(*object), object_list);
477 if (object)
478 list_del(&object->object_list);
479 else if (mem_pool_free_count)
480 object = &mem_pool[--mem_pool_free_count];
481 write_unlock_irqrestore(&kmemleak_lock, flags);
482
483 return object;
484}
485
486/*
487 * Return the object to either the slab allocator or the memory pool.
488 */
489static void mem_pool_free(struct kmemleak_object *object)
490{
491 unsigned long flags;
492
493 if (object < mem_pool || object >= mem_pool + ARRAY_SIZE(mem_pool)) {
494 kmem_cache_free(object_cache, object);
495 return;
496 }
497
498 /* add the object to the memory pool free list */
499 write_lock_irqsave(&kmemleak_lock, flags);
500 list_add(&object->object_list, &mem_pool_free_list);
501 write_unlock_irqrestore(&kmemleak_lock, flags);
502}
503
504/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100505 * RCU callback to free a kmemleak_object.
506 */
507static void free_object_rcu(struct rcu_head *rcu)
508{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800509 struct hlist_node *tmp;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100510 struct kmemleak_scan_area *area;
511 struct kmemleak_object *object =
512 container_of(rcu, struct kmemleak_object, rcu);
513
514 /*
515 * Once use_count is 0 (guaranteed by put_object), there is no other
516 * code accessing this object, hence no need for locking.
517 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800518 hlist_for_each_entry_safe(area, tmp, &object->area_list, node) {
519 hlist_del(&area->node);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100520 kmem_cache_free(scan_area_cache, area);
521 }
Catalin Marinas06473982019-09-23 15:34:02 -0700522 mem_pool_free(object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100523}
524
525/*
526 * Decrement the object use_count. Once the count is 0, free the object using
527 * an RCU callback. Since put_object() may be called via the kmemleak_free() ->
528 * delete_object() path, the delayed RCU freeing ensures that there is no
529 * recursive call to the kernel allocator. Lock-less RCU object_list traversal
530 * is also possible.
531 */
532static void put_object(struct kmemleak_object *object)
533{
534 if (!atomic_dec_and_test(&object->use_count))
535 return;
536
537 /* should only get here after delete_object was called */
538 WARN_ON(object->flags & OBJECT_ALLOCATED);
539
540 call_rcu(&object->rcu, free_object_rcu);
541}
542
543/*
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700544 * Look up an object in the object search tree and increase its use_count.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100545 */
546static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias)
547{
548 unsigned long flags;
Alexey Klimov9fbed252015-11-05 18:45:57 -0800549 struct kmemleak_object *object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100550
551 rcu_read_lock();
552 read_lock_irqsave(&kmemleak_lock, flags);
Catalin Marinas93ada572015-06-24 16:58:37 -0700553 object = lookup_object(ptr, alias);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100554 read_unlock_irqrestore(&kmemleak_lock, flags);
555
556 /* check whether the object is still available */
557 if (object && !get_object(object))
558 object = NULL;
559 rcu_read_unlock();
560
561 return object;
562}
563
564/*
Catalin Marinase781a9a2015-06-24 16:58:29 -0700565 * Look up an object in the object search tree and remove it from both
566 * object_tree_root and object_list. The returned object's use_count should be
567 * at least 1, as initially set by create_object().
568 */
569static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int alias)
570{
571 unsigned long flags;
572 struct kmemleak_object *object;
573
574 write_lock_irqsave(&kmemleak_lock, flags);
575 object = lookup_object(ptr, alias);
576 if (object) {
577 rb_erase(&object->rb_node, &object_tree_root);
578 list_del_rcu(&object->object_list);
579 }
580 write_unlock_irqrestore(&kmemleak_lock, flags);
581
582 return object;
583}
584
585/*
Catalin Marinasfd678962009-08-27 14:29:17 +0100586 * Save stack trace to the given array of MAX_TRACE size.
587 */
588static int __save_stack_trace(unsigned long *trace)
589{
Thomas Gleixner07984aa2019-04-25 11:45:01 +0200590 return stack_trace_save(trace, MAX_TRACE, 2);
Catalin Marinasfd678962009-08-27 14:29:17 +0100591}
592
593/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100594 * Create the metadata (struct kmemleak_object) corresponding to an allocated
595 * memory block and add it to the object_list and object_tree_root.
596 */
Catalin Marinasfd678962009-08-27 14:29:17 +0100597static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
598 int min_count, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100599{
600 unsigned long flags;
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700601 struct kmemleak_object *object, *parent;
602 struct rb_node **link, *rb_parent;
Andrey Konovalova2f77572019-02-20 22:19:16 -0800603 unsigned long untagged_ptr;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100604
Catalin Marinas06473982019-09-23 15:34:02 -0700605 object = mem_pool_alloc(gfp);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100606 if (!object) {
Joe Perches598d8092016-03-17 14:19:44 -0700607 pr_warn("Cannot allocate a kmemleak_object structure\n");
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000608 kmemleak_disable();
Catalin Marinasfd678962009-08-27 14:29:17 +0100609 return NULL;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100610 }
611
612 INIT_LIST_HEAD(&object->object_list);
613 INIT_LIST_HEAD(&object->gray_list);
614 INIT_HLIST_HEAD(&object->area_list);
615 spin_lock_init(&object->lock);
616 atomic_set(&object->use_count, 1);
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000617 object->flags = OBJECT_ALLOCATED;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100618 object->pointer = ptr;
619 object->size = size;
Catalin Marinas94f4a162017-07-06 15:40:22 -0700620 object->excess_ref = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100621 object->min_count = min_count;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000622 object->count = 0; /* white color initially */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100623 object->jiffies = jiffies;
Catalin Marinas04609ccc2009-10-28 13:33:12 +0000624 object->checksum = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100625
626 /* task information */
627 if (in_irq()) {
628 object->pid = 0;
629 strncpy(object->comm, "hardirq", sizeof(object->comm));
Dmitry Vyukov6ef90562019-07-11 20:53:39 -0700630 } else if (in_serving_softirq()) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100631 object->pid = 0;
632 strncpy(object->comm, "softirq", sizeof(object->comm));
633 } else {
634 object->pid = current->pid;
635 /*
636 * There is a small chance of a race with set_task_comm(),
637 * however using get_task_comm() here may cause locking
638 * dependency issues with current->alloc_lock. In the worst
639 * case, the command line is not correct.
640 */
641 strncpy(object->comm, current->comm, sizeof(object->comm));
642 }
643
644 /* kernel backtrace */
Catalin Marinasfd678962009-08-27 14:29:17 +0100645 object->trace_len = __save_stack_trace(object->trace);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100646
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100647 write_lock_irqsave(&kmemleak_lock, flags);
Luis R. Rodriguez0580a182009-09-08 17:32:34 +0100648
Andrey Konovalova2f77572019-02-20 22:19:16 -0800649 untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr);
650 min_addr = min(min_addr, untagged_ptr);
651 max_addr = max(max_addr, untagged_ptr + size);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700652 link = &object_tree_root.rb_node;
653 rb_parent = NULL;
654 while (*link) {
655 rb_parent = *link;
656 parent = rb_entry(rb_parent, struct kmemleak_object, rb_node);
657 if (ptr + size <= parent->pointer)
658 link = &parent->rb_node.rb_left;
659 else if (parent->pointer + parent->size <= ptr)
660 link = &parent->rb_node.rb_right;
661 else {
Joe Perches756a0252016-03-17 14:19:47 -0700662 kmemleak_stop("Cannot insert 0x%lx into the object search tree (overlaps existing)\n",
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700663 ptr);
Catalin Marinas9d5a4c72015-06-24 16:58:34 -0700664 /*
665 * No need for parent->lock here since "parent" cannot
666 * be freed while the kmemleak_lock is held.
667 */
668 dump_object_info(parent);
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700669 kmem_cache_free(object_cache, object);
Catalin Marinas9d5a4c72015-06-24 16:58:34 -0700670 object = NULL;
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700671 goto out;
672 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100673 }
Michel Lespinasse85d3a312012-10-08 16:31:27 -0700674 rb_link_node(&object->rb_node, rb_parent, link);
675 rb_insert_color(&object->rb_node, &object_tree_root);
676
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100677 list_add_tail_rcu(&object->object_list, &object_list);
678out:
679 write_unlock_irqrestore(&kmemleak_lock, flags);
Catalin Marinasfd678962009-08-27 14:29:17 +0100680 return object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100681}
682
683/*
Catalin Marinase781a9a2015-06-24 16:58:29 -0700684 * Mark the object as not allocated and schedule RCU freeing via put_object().
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100685 */
Catalin Marinas53238a62009-07-07 10:33:00 +0100686static void __delete_object(struct kmemleak_object *object)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100687{
688 unsigned long flags;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100689
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100690 WARN_ON(!(object->flags & OBJECT_ALLOCATED));
Catalin Marinase781a9a2015-06-24 16:58:29 -0700691 WARN_ON(atomic_read(&object->use_count) < 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100692
693 /*
694 * Locking here also ensures that the corresponding memory block
695 * cannot be freed when it is being scanned.
696 */
697 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100698 object->flags &= ~OBJECT_ALLOCATED;
699 spin_unlock_irqrestore(&object->lock, flags);
700 put_object(object);
701}
702
703/*
Catalin Marinas53238a62009-07-07 10:33:00 +0100704 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
705 * delete it.
706 */
707static void delete_object_full(unsigned long ptr)
708{
709 struct kmemleak_object *object;
710
Catalin Marinase781a9a2015-06-24 16:58:29 -0700711 object = find_and_remove_object(ptr, 0);
Catalin Marinas53238a62009-07-07 10:33:00 +0100712 if (!object) {
713#ifdef DEBUG
714 kmemleak_warn("Freeing unknown object at 0x%08lx\n",
715 ptr);
716#endif
717 return;
718 }
719 __delete_object(object);
Catalin Marinas53238a62009-07-07 10:33:00 +0100720}
721
722/*
723 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
724 * delete it. If the memory block is partially freed, the function may create
725 * additional metadata for the remaining parts of the block.
726 */
727static void delete_object_part(unsigned long ptr, size_t size)
728{
729 struct kmemleak_object *object;
730 unsigned long start, end;
731
Catalin Marinase781a9a2015-06-24 16:58:29 -0700732 object = find_and_remove_object(ptr, 1);
Catalin Marinas53238a62009-07-07 10:33:00 +0100733 if (!object) {
734#ifdef DEBUG
Joe Perches756a0252016-03-17 14:19:47 -0700735 kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
736 ptr, size);
Catalin Marinas53238a62009-07-07 10:33:00 +0100737#endif
738 return;
739 }
Catalin Marinas53238a62009-07-07 10:33:00 +0100740
741 /*
742 * Create one or two objects that may result from the memory block
743 * split. Note that partial freeing is only done by free_bootmem() and
744 * this happens before kmemleak_init() is called. The path below is
745 * only executed during early log recording in kmemleak_init(), so
746 * GFP_KERNEL is enough.
747 */
748 start = object->pointer;
749 end = object->pointer + object->size;
750 if (ptr > start)
751 create_object(start, ptr - start, object->min_count,
752 GFP_KERNEL);
753 if (ptr + size < end)
754 create_object(ptr + size, end - ptr - size, object->min_count,
755 GFP_KERNEL);
756
Catalin Marinase781a9a2015-06-24 16:58:29 -0700757 __delete_object(object);
Catalin Marinas53238a62009-07-07 10:33:00 +0100758}
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700759
760static void __paint_it(struct kmemleak_object *object, int color)
761{
762 object->min_count = color;
763 if (color == KMEMLEAK_BLACK)
764 object->flags |= OBJECT_NO_SCAN;
765}
766
767static void paint_it(struct kmemleak_object *object, int color)
768{
769 unsigned long flags;
770
771 spin_lock_irqsave(&object->lock, flags);
772 __paint_it(object, color);
773 spin_unlock_irqrestore(&object->lock, flags);
774}
775
776static void paint_ptr(unsigned long ptr, int color)
777{
778 struct kmemleak_object *object;
779
780 object = find_and_get_object(ptr, 0);
781 if (!object) {
Joe Perches756a0252016-03-17 14:19:47 -0700782 kmemleak_warn("Trying to color unknown object at 0x%08lx as %s\n",
783 ptr,
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700784 (color == KMEMLEAK_GREY) ? "Grey" :
785 (color == KMEMLEAK_BLACK) ? "Black" : "Unknown");
786 return;
787 }
788 paint_it(object, color);
789 put_object(object);
790}
791
Catalin Marinas53238a62009-07-07 10:33:00 +0100792/*
Holger Hans Peter Freyther145b64b2010-07-22 19:54:13 +0800793 * Mark an object permanently as gray-colored so that it can no longer be
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100794 * reported as a leak. This is used in general to mark a false positive.
795 */
796static void make_gray_object(unsigned long ptr)
797{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700798 paint_ptr(ptr, KMEMLEAK_GREY);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100799}
800
801/*
802 * Mark the object as black-colored so that it is ignored from scans and
803 * reporting.
804 */
805static void make_black_object(unsigned long ptr)
806{
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -0700807 paint_ptr(ptr, KMEMLEAK_BLACK);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100808}
809
810/*
811 * Add a scanning area to the object. If at least one such area is added,
812 * kmemleak will only scan these ranges rather than the whole memory block.
813 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000814static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100815{
816 unsigned long flags;
817 struct kmemleak_object *object;
818 struct kmemleak_scan_area *area;
819
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000820 object = find_and_get_object(ptr, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100821 if (!object) {
Joe Perchesae281062009-06-23 14:40:26 +0100822 kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n",
823 ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100824 return;
825 }
826
Catalin Marinas6ae4bd12011-01-27 10:30:26 +0000827 area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100828
829 spin_lock_irqsave(&object->lock, flags);
Catalin Marinasdba82d92019-09-23 15:33:59 -0700830 if (!area) {
831 pr_warn_once("Cannot allocate a scan area, scanning the full object\n");
832 /* mark the object for full scan to avoid false positives */
833 object->flags |= OBJECT_FULL_SCAN;
834 goto out_unlock;
835 }
Catalin Marinas7f88f882013-11-12 15:07:45 -0800836 if (size == SIZE_MAX) {
837 size = object->pointer + object->size - ptr;
838 } else if (ptr + size > object->pointer + object->size) {
Joe Perchesae281062009-06-23 14:40:26 +0100839 kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100840 dump_object_info(object);
841 kmem_cache_free(scan_area_cache, area);
842 goto out_unlock;
843 }
844
845 INIT_HLIST_NODE(&area->node);
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000846 area->start = ptr;
847 area->size = size;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100848
849 hlist_add_head(&area->node, &object->area_list);
850out_unlock:
851 spin_unlock_irqrestore(&object->lock, flags);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100852 put_object(object);
853}
854
855/*
Catalin Marinas94f4a162017-07-06 15:40:22 -0700856 * Any surplus references (object already gray) to 'ptr' are passed to
857 * 'excess_ref'. This is used in the vmalloc() case where a pointer to
858 * vm_struct may be used as an alternative reference to the vmalloc'ed object
859 * (see free_thread_stack()).
860 */
861static void object_set_excess_ref(unsigned long ptr, unsigned long excess_ref)
862{
863 unsigned long flags;
864 struct kmemleak_object *object;
865
866 object = find_and_get_object(ptr, 0);
867 if (!object) {
868 kmemleak_warn("Setting excess_ref on unknown object at 0x%08lx\n",
869 ptr);
870 return;
871 }
872
873 spin_lock_irqsave(&object->lock, flags);
874 object->excess_ref = excess_ref;
875 spin_unlock_irqrestore(&object->lock, flags);
876 put_object(object);
877}
878
879/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100880 * Set the OBJECT_NO_SCAN flag for the object corresponding to the give
881 * pointer. Such object will not be scanned by kmemleak but references to it
882 * are searched.
883 */
884static void object_no_scan(unsigned long ptr)
885{
886 unsigned long flags;
887 struct kmemleak_object *object;
888
889 object = find_and_get_object(ptr, 0);
890 if (!object) {
Joe Perchesae281062009-06-23 14:40:26 +0100891 kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100892 return;
893 }
894
895 spin_lock_irqsave(&object->lock, flags);
896 object->flags |= OBJECT_NO_SCAN;
897 spin_unlock_irqrestore(&object->lock, flags);
898 put_object(object);
899}
900
901/*
902 * Log an early kmemleak_* call to the early_log buffer. These calls will be
903 * processed later once kmemleak is fully initialized.
904 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100905static void __init log_early(int op_type, const void *ptr, size_t size,
Catalin Marinasc017b4b2009-10-28 13:33:09 +0000906 int min_count)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100907{
908 unsigned long flags;
909 struct early_log *log;
910
Li Zefan8910ae892014-04-03 14:46:29 -0700911 if (kmemleak_error) {
Catalin Marinasb6693002011-09-28 17:22:56 +0100912 /* kmemleak stopped recording, just count the requests */
913 crt_early_log++;
914 return;
915 }
916
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100917 if (crt_early_log >= ARRAY_SIZE(early_log)) {
Wang Kai21cd3a62015-09-08 15:03:41 -0700918 crt_early_log++;
Catalin Marinasa9d90582009-06-25 10:16:11 +0100919 kmemleak_disable();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100920 return;
921 }
922
923 /*
924 * There is no need for locking since the kernel is still in UP mode
925 * at this stage. Disabling the IRQs is enough.
926 */
927 local_irq_save(flags);
928 log = &early_log[crt_early_log];
929 log->op_type = op_type;
930 log->ptr = ptr;
931 log->size = size;
932 log->min_count = min_count;
Catalin Marinas5f790202011-09-28 12:17:03 +0100933 log->trace_len = __save_stack_trace(log->trace);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100934 crt_early_log++;
935 local_irq_restore(flags);
936}
937
938/*
Catalin Marinasfd678962009-08-27 14:29:17 +0100939 * Log an early allocated block and populate the stack trace.
940 */
941static void early_alloc(struct early_log *log)
942{
943 struct kmemleak_object *object;
944 unsigned long flags;
945 int i;
946
Li Zefan8910ae892014-04-03 14:46:29 -0700947 if (!kmemleak_enabled || !log->ptr || IS_ERR(log->ptr))
Catalin Marinasfd678962009-08-27 14:29:17 +0100948 return;
949
950 /*
951 * RCU locking needed to ensure object is not freed via put_object().
952 */
953 rcu_read_lock();
954 object = create_object((unsigned long)log->ptr, log->size,
Tetsuo Handac1bcd6b2009-10-09 10:39:24 +0100955 log->min_count, GFP_ATOMIC);
Catalin Marinas0d5d1aa2009-10-09 10:30:34 +0100956 if (!object)
957 goto out;
Catalin Marinasfd678962009-08-27 14:29:17 +0100958 spin_lock_irqsave(&object->lock, flags);
959 for (i = 0; i < log->trace_len; i++)
960 object->trace[i] = log->trace[i];
961 object->trace_len = log->trace_len;
962 spin_unlock_irqrestore(&object->lock, flags);
Catalin Marinas0d5d1aa2009-10-09 10:30:34 +0100963out:
Catalin Marinasfd678962009-08-27 14:29:17 +0100964 rcu_read_unlock();
965}
966
Catalin Marinasf528f0b2011-09-26 17:12:53 +0100967/*
968 * Log an early allocated block and populate the stack trace.
969 */
970static void early_alloc_percpu(struct early_log *log)
971{
972 unsigned int cpu;
973 const void __percpu *ptr = log->ptr;
974
975 for_each_possible_cpu(cpu) {
976 log->ptr = per_cpu_ptr(ptr, cpu);
977 early_alloc(log);
978 }
979}
980
Catalin Marinasa2b6bf62010-07-19 11:54:17 +0100981/**
982 * kmemleak_alloc - register a newly allocated object
983 * @ptr: pointer to beginning of the object
984 * @size: size of the object
985 * @min_count: minimum number of references to this object. If during memory
986 * scanning a number of references less than @min_count is found,
987 * the object is reported as a memory leak. If @min_count is 0,
988 * the object is never reported as a leak. If @min_count is -1,
989 * the object is ignored (not scanned and not reported as a leak)
990 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
991 *
992 * This function is called from the kernel allocators when a new object
Catalin Marinas94f4a162017-07-06 15:40:22 -0700993 * (memory block) is allocated (kmem_cache_alloc, kmalloc etc.).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100994 */
Catalin Marinasa6186d82009-08-27 14:29:16 +0100995void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count,
996 gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +0100997{
998 pr_debug("%s(0x%p, %zu, %d)\n", __func__, ptr, size, min_count);
999
Li Zefan8910ae892014-04-03 14:46:29 -07001000 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001001 create_object((unsigned long)ptr, size, min_count, gfp);
Li Zefan8910ae892014-04-03 14:46:29 -07001002 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001003 log_early(KMEMLEAK_ALLOC, ptr, size, min_count);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001004}
1005EXPORT_SYMBOL_GPL(kmemleak_alloc);
1006
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001007/**
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001008 * kmemleak_alloc_percpu - register a newly allocated __percpu object
1009 * @ptr: __percpu pointer to beginning of the object
1010 * @size: size of the object
Larry Finger8a8c35f2015-06-24 16:58:51 -07001011 * @gfp: flags used for kmemleak internal memory allocations
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001012 *
1013 * This function is called from the kernel percpu allocator when a new object
Larry Finger8a8c35f2015-06-24 16:58:51 -07001014 * (memory block) is allocated (alloc_percpu).
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001015 */
Larry Finger8a8c35f2015-06-24 16:58:51 -07001016void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size,
1017 gfp_t gfp)
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001018{
1019 unsigned int cpu;
1020
1021 pr_debug("%s(0x%p, %zu)\n", __func__, ptr, size);
1022
1023 /*
1024 * Percpu allocations are only scanned and not reported as leaks
1025 * (min_count is set to 0).
1026 */
Li Zefan8910ae892014-04-03 14:46:29 -07001027 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001028 for_each_possible_cpu(cpu)
1029 create_object((unsigned long)per_cpu_ptr(ptr, cpu),
Larry Finger8a8c35f2015-06-24 16:58:51 -07001030 size, 0, gfp);
Li Zefan8910ae892014-04-03 14:46:29 -07001031 else if (kmemleak_early_log)
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001032 log_early(KMEMLEAK_ALLOC_PERCPU, ptr, size, 0);
1033}
1034EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
1035
1036/**
Catalin Marinas94f4a162017-07-06 15:40:22 -07001037 * kmemleak_vmalloc - register a newly vmalloc'ed object
1038 * @area: pointer to vm_struct
1039 * @size: size of the object
1040 * @gfp: __vmalloc() flags used for kmemleak internal memory allocations
1041 *
1042 * This function is called from the vmalloc() kernel allocator when a new
1043 * object (memory block) is allocated.
1044 */
1045void __ref kmemleak_vmalloc(const struct vm_struct *area, size_t size, gfp_t gfp)
1046{
1047 pr_debug("%s(0x%p, %zu)\n", __func__, area, size);
1048
1049 /*
1050 * A min_count = 2 is needed because vm_struct contains a reference to
1051 * the virtual address of the vmalloc'ed block.
1052 */
1053 if (kmemleak_enabled) {
1054 create_object((unsigned long)area->addr, size, 2, gfp);
1055 object_set_excess_ref((unsigned long)area,
1056 (unsigned long)area->addr);
1057 } else if (kmemleak_early_log) {
1058 log_early(KMEMLEAK_ALLOC, area->addr, size, 2);
1059 /* reusing early_log.size for storing area->addr */
1060 log_early(KMEMLEAK_SET_EXCESS_REF,
1061 area, (unsigned long)area->addr, 0);
1062 }
1063}
1064EXPORT_SYMBOL_GPL(kmemleak_vmalloc);
1065
1066/**
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001067 * kmemleak_free - unregister a previously registered object
1068 * @ptr: pointer to beginning of the object
1069 *
1070 * This function is called from the kernel allocators when an object (memory
1071 * block) is freed (kmem_cache_free, kfree, vfree etc.).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001072 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001073void __ref kmemleak_free(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001074{
1075 pr_debug("%s(0x%p)\n", __func__, ptr);
1076
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07001077 if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas53238a62009-07-07 10:33:00 +01001078 delete_object_full((unsigned long)ptr);
Li Zefan8910ae892014-04-03 14:46:29 -07001079 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001080 log_early(KMEMLEAK_FREE, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001081}
1082EXPORT_SYMBOL_GPL(kmemleak_free);
1083
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001084/**
1085 * kmemleak_free_part - partially unregister a previously registered object
1086 * @ptr: pointer to the beginning or inside the object. This also
1087 * represents the start of the range to be freed
1088 * @size: size to be unregistered
1089 *
1090 * This function is called when only a part of a memory block is freed
1091 * (usually from the bootmem allocator).
Catalin Marinas53238a62009-07-07 10:33:00 +01001092 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001093void __ref kmemleak_free_part(const void *ptr, size_t size)
Catalin Marinas53238a62009-07-07 10:33:00 +01001094{
1095 pr_debug("%s(0x%p)\n", __func__, ptr);
1096
Li Zefan8910ae892014-04-03 14:46:29 -07001097 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas53238a62009-07-07 10:33:00 +01001098 delete_object_part((unsigned long)ptr, size);
Li Zefan8910ae892014-04-03 14:46:29 -07001099 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001100 log_early(KMEMLEAK_FREE_PART, ptr, size, 0);
Catalin Marinas53238a62009-07-07 10:33:00 +01001101}
1102EXPORT_SYMBOL_GPL(kmemleak_free_part);
1103
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001104/**
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001105 * kmemleak_free_percpu - unregister a previously registered __percpu object
1106 * @ptr: __percpu pointer to beginning of the object
1107 *
1108 * This function is called from the kernel percpu allocator when an object
1109 * (memory block) is freed (free_percpu).
1110 */
1111void __ref kmemleak_free_percpu(const void __percpu *ptr)
1112{
1113 unsigned int cpu;
1114
1115 pr_debug("%s(0x%p)\n", __func__, ptr);
1116
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07001117 if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001118 for_each_possible_cpu(cpu)
1119 delete_object_full((unsigned long)per_cpu_ptr(ptr,
1120 cpu));
Li Zefan8910ae892014-04-03 14:46:29 -07001121 else if (kmemleak_early_log)
Catalin Marinasf528f0b2011-09-26 17:12:53 +01001122 log_early(KMEMLEAK_FREE_PERCPU, ptr, 0, 0);
1123}
1124EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
1125
1126/**
Catalin Marinasffe2c742014-06-06 14:38:17 -07001127 * kmemleak_update_trace - update object allocation stack trace
1128 * @ptr: pointer to beginning of the object
1129 *
1130 * Override the object allocation stack trace for cases where the actual
1131 * allocation place is not always useful.
1132 */
1133void __ref kmemleak_update_trace(const void *ptr)
1134{
1135 struct kmemleak_object *object;
1136 unsigned long flags;
1137
1138 pr_debug("%s(0x%p)\n", __func__, ptr);
1139
1140 if (!kmemleak_enabled || IS_ERR_OR_NULL(ptr))
1141 return;
1142
1143 object = find_and_get_object((unsigned long)ptr, 1);
1144 if (!object) {
1145#ifdef DEBUG
1146 kmemleak_warn("Updating stack trace for unknown object at %p\n",
1147 ptr);
1148#endif
1149 return;
1150 }
1151
1152 spin_lock_irqsave(&object->lock, flags);
1153 object->trace_len = __save_stack_trace(object->trace);
1154 spin_unlock_irqrestore(&object->lock, flags);
1155
1156 put_object(object);
1157}
1158EXPORT_SYMBOL(kmemleak_update_trace);
1159
1160/**
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001161 * kmemleak_not_leak - mark an allocated object as false positive
1162 * @ptr: pointer to beginning of the object
1163 *
1164 * Calling this function on an object will cause the memory block to no longer
1165 * be reported as leak and always be scanned.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001166 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001167void __ref kmemleak_not_leak(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001168{
1169 pr_debug("%s(0x%p)\n", __func__, ptr);
1170
Li Zefan8910ae892014-04-03 14:46:29 -07001171 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001172 make_gray_object((unsigned long)ptr);
Li Zefan8910ae892014-04-03 14:46:29 -07001173 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001174 log_early(KMEMLEAK_NOT_LEAK, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001175}
1176EXPORT_SYMBOL(kmemleak_not_leak);
1177
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001178/**
1179 * kmemleak_ignore - ignore an allocated object
1180 * @ptr: pointer to beginning of the object
1181 *
1182 * Calling this function on an object will cause the memory block to be
1183 * ignored (not scanned and not reported as a leak). This is usually done when
1184 * it is known that the corresponding block is not a leak and does not contain
1185 * any references to other allocated memory blocks.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001186 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001187void __ref kmemleak_ignore(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001188{
1189 pr_debug("%s(0x%p)\n", __func__, ptr);
1190
Li Zefan8910ae892014-04-03 14:46:29 -07001191 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001192 make_black_object((unsigned long)ptr);
Li Zefan8910ae892014-04-03 14:46:29 -07001193 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001194 log_early(KMEMLEAK_IGNORE, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001195}
1196EXPORT_SYMBOL(kmemleak_ignore);
1197
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001198/**
1199 * kmemleak_scan_area - limit the range to be scanned in an allocated object
1200 * @ptr: pointer to beginning or inside the object. This also
1201 * represents the start of the scan area
1202 * @size: size of the scan area
1203 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
1204 *
1205 * This function is used when it is known that only certain parts of an object
1206 * contain references to other objects. Kmemleak will only scan these areas
1207 * reducing the number false negatives.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001208 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001209void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001210{
1211 pr_debug("%s(0x%p)\n", __func__, ptr);
1212
Li Zefan8910ae892014-04-03 14:46:29 -07001213 if (kmemleak_enabled && ptr && size && !IS_ERR(ptr))
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001214 add_scan_area((unsigned long)ptr, size, gfp);
Li Zefan8910ae892014-04-03 14:46:29 -07001215 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001216 log_early(KMEMLEAK_SCAN_AREA, ptr, size, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001217}
1218EXPORT_SYMBOL(kmemleak_scan_area);
1219
Catalin Marinasa2b6bf62010-07-19 11:54:17 +01001220/**
1221 * kmemleak_no_scan - do not scan an allocated object
1222 * @ptr: pointer to beginning of the object
1223 *
1224 * This function notifies kmemleak not to scan the given memory block. Useful
1225 * in situations where it is known that the given object does not contain any
1226 * references to other objects. Kmemleak will not scan such objects reducing
1227 * the number of false negatives.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001228 */
Catalin Marinasa6186d82009-08-27 14:29:16 +01001229void __ref kmemleak_no_scan(const void *ptr)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001230{
1231 pr_debug("%s(0x%p)\n", __func__, ptr);
1232
Li Zefan8910ae892014-04-03 14:46:29 -07001233 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001234 object_no_scan((unsigned long)ptr);
Li Zefan8910ae892014-04-03 14:46:29 -07001235 else if (kmemleak_early_log)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001236 log_early(KMEMLEAK_NO_SCAN, ptr, 0, 0);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001237}
1238EXPORT_SYMBOL(kmemleak_no_scan);
1239
Catalin Marinas9099dae2016-10-11 13:55:11 -07001240/**
1241 * kmemleak_alloc_phys - similar to kmemleak_alloc but taking a physical
1242 * address argument
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001243 * @phys: physical address of the object
1244 * @size: size of the object
1245 * @min_count: minimum number of references to this object.
1246 * See kmemleak_alloc()
1247 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
Catalin Marinas9099dae2016-10-11 13:55:11 -07001248 */
1249void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, int min_count,
1250 gfp_t gfp)
1251{
1252 if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn)
1253 kmemleak_alloc(__va(phys), size, min_count, gfp);
1254}
1255EXPORT_SYMBOL(kmemleak_alloc_phys);
1256
1257/**
1258 * kmemleak_free_part_phys - similar to kmemleak_free_part but taking a
1259 * physical address argument
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001260 * @phys: physical address if the beginning or inside an object. This
1261 * also represents the start of the range to be freed
1262 * @size: size to be unregistered
Catalin Marinas9099dae2016-10-11 13:55:11 -07001263 */
1264void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size)
1265{
1266 if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn)
1267 kmemleak_free_part(__va(phys), size);
1268}
1269EXPORT_SYMBOL(kmemleak_free_part_phys);
1270
1271/**
1272 * kmemleak_not_leak_phys - similar to kmemleak_not_leak but taking a physical
1273 * address argument
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001274 * @phys: physical address of the object
Catalin Marinas9099dae2016-10-11 13:55:11 -07001275 */
1276void __ref kmemleak_not_leak_phys(phys_addr_t phys)
1277{
1278 if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn)
1279 kmemleak_not_leak(__va(phys));
1280}
1281EXPORT_SYMBOL(kmemleak_not_leak_phys);
1282
1283/**
1284 * kmemleak_ignore_phys - similar to kmemleak_ignore but taking a physical
1285 * address argument
Mike Rapoporte8b098f2018-04-05 16:24:57 -07001286 * @phys: physical address of the object
Catalin Marinas9099dae2016-10-11 13:55:11 -07001287 */
1288void __ref kmemleak_ignore_phys(phys_addr_t phys)
1289{
1290 if (!IS_ENABLED(CONFIG_HIGHMEM) || PHYS_PFN(phys) < max_low_pfn)
1291 kmemleak_ignore(__va(phys));
1292}
1293EXPORT_SYMBOL(kmemleak_ignore_phys);
1294
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001295/*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001296 * Update an object's checksum and return true if it was modified.
1297 */
1298static bool update_checksum(struct kmemleak_object *object)
1299{
1300 u32 old_csum = object->checksum;
1301
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001302 kasan_disable_current();
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001303 object->checksum = crc32(0, (void *)object->pointer, object->size);
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001304 kasan_enable_current();
1305
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001306 return object->checksum != old_csum;
1307}
1308
1309/*
Catalin Marinas04f70d12017-07-06 15:40:19 -07001310 * Update an object's references. object->lock must be held by the caller.
1311 */
1312static void update_refs(struct kmemleak_object *object)
1313{
1314 if (!color_white(object)) {
1315 /* non-orphan, ignored or new */
1316 return;
1317 }
1318
1319 /*
1320 * Increase the object's reference count (number of pointers to the
1321 * memory block). If this count reaches the required minimum, the
1322 * object's color will become gray and it will be added to the
1323 * gray_list.
1324 */
1325 object->count++;
1326 if (color_gray(object)) {
1327 /* put_object() called when removing from gray_list */
1328 WARN_ON(!get_object(object));
1329 list_add_tail(&object->gray_list, &gray_list);
1330 }
1331}
1332
1333/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001334 * Memory scanning is a long process and it needs to be interruptable. This
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001335 * function checks whether such interrupt condition occurred.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001336 */
1337static int scan_should_stop(void)
1338{
Li Zefan8910ae892014-04-03 14:46:29 -07001339 if (!kmemleak_enabled)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001340 return 1;
1341
1342 /*
1343 * This function may be called from either process or kthread context,
1344 * hence the need to check for both stop conditions.
1345 */
1346 if (current->mm)
1347 return signal_pending(current);
1348 else
1349 return kthread_should_stop();
1350
1351 return 0;
1352}
1353
1354/*
1355 * Scan a memory block (exclusive range) for valid pointers and add those
1356 * found to the gray list.
1357 */
1358static void scan_block(void *_start, void *_end,
Catalin Marinas93ada572015-06-24 16:58:37 -07001359 struct kmemleak_object *scanned)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001360{
1361 unsigned long *ptr;
1362 unsigned long *start = PTR_ALIGN(_start, BYTES_PER_POINTER);
1363 unsigned long *end = _end - (BYTES_PER_POINTER - 1);
Catalin Marinas93ada572015-06-24 16:58:37 -07001364 unsigned long flags;
Andrey Konovalova2f77572019-02-20 22:19:16 -08001365 unsigned long untagged_ptr;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001366
Catalin Marinas93ada572015-06-24 16:58:37 -07001367 read_lock_irqsave(&kmemleak_lock, flags);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001368 for (ptr = start; ptr < end; ptr++) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001369 struct kmemleak_object *object;
Pekka Enberg8e019362009-08-27 14:50:00 +01001370 unsigned long pointer;
Catalin Marinas94f4a162017-07-06 15:40:22 -07001371 unsigned long excess_ref;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001372
1373 if (scan_should_stop())
1374 break;
1375
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001376 kasan_disable_current();
Pekka Enberg8e019362009-08-27 14:50:00 +01001377 pointer = *ptr;
Andrey Ryabinine79ed2f2015-02-13 14:39:49 -08001378 kasan_enable_current();
Pekka Enberg8e019362009-08-27 14:50:00 +01001379
Andrey Konovalova2f77572019-02-20 22:19:16 -08001380 untagged_ptr = (unsigned long)kasan_reset_tag((void *)pointer);
1381 if (untagged_ptr < min_addr || untagged_ptr >= max_addr)
Catalin Marinas93ada572015-06-24 16:58:37 -07001382 continue;
1383
1384 /*
1385 * No need for get_object() here since we hold kmemleak_lock.
1386 * object->use_count cannot be dropped to 0 while the object
1387 * is still present in object_tree_root and object_list
1388 * (with updates protected by kmemleak_lock).
1389 */
1390 object = lookup_object(pointer, 1);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001391 if (!object)
1392 continue;
Catalin Marinas93ada572015-06-24 16:58:37 -07001393 if (object == scanned)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001394 /* self referenced, ignore */
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001395 continue;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001396
1397 /*
1398 * Avoid the lockdep recursive warning on object->lock being
1399 * previously acquired in scan_object(). These locks are
1400 * enclosed by scan_mutex.
1401 */
Catalin Marinas93ada572015-06-24 16:58:37 -07001402 spin_lock_nested(&object->lock, SINGLE_DEPTH_NESTING);
Catalin Marinas94f4a162017-07-06 15:40:22 -07001403 /* only pass surplus references (object already gray) */
1404 if (color_gray(object)) {
1405 excess_ref = object->excess_ref;
1406 /* no need for update_refs() if object already gray */
1407 } else {
1408 excess_ref = 0;
1409 update_refs(object);
1410 }
Catalin Marinas93ada572015-06-24 16:58:37 -07001411 spin_unlock(&object->lock);
Catalin Marinas94f4a162017-07-06 15:40:22 -07001412
1413 if (excess_ref) {
1414 object = lookup_object(excess_ref, 0);
1415 if (!object)
1416 continue;
1417 if (object == scanned)
1418 /* circular reference, ignore */
1419 continue;
1420 spin_lock_nested(&object->lock, SINGLE_DEPTH_NESTING);
1421 update_refs(object);
1422 spin_unlock(&object->lock);
1423 }
Catalin Marinas93ada572015-06-24 16:58:37 -07001424 }
1425 read_unlock_irqrestore(&kmemleak_lock, flags);
1426}
Catalin Marinas0587da42009-10-28 13:33:11 +00001427
Catalin Marinas93ada572015-06-24 16:58:37 -07001428/*
1429 * Scan a large memory block in MAX_SCAN_SIZE chunks to reduce the latency.
1430 */
Arnd Bergmanndce5b0b2019-04-18 17:50:48 -07001431#ifdef CONFIG_SMP
Catalin Marinas93ada572015-06-24 16:58:37 -07001432static void scan_large_block(void *start, void *end)
1433{
1434 void *next;
1435
1436 while (start < end) {
1437 next = min(start + MAX_SCAN_SIZE, end);
1438 scan_block(start, next, NULL);
1439 start = next;
1440 cond_resched();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001441 }
1442}
Arnd Bergmanndce5b0b2019-04-18 17:50:48 -07001443#endif
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001444
1445/*
1446 * Scan a memory block corresponding to a kmemleak_object. A condition is
1447 * that object->use_count >= 1.
1448 */
1449static void scan_object(struct kmemleak_object *object)
1450{
1451 struct kmemleak_scan_area *area;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001452 unsigned long flags;
1453
1454 /*
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001455 * Once the object->lock is acquired, the corresponding memory block
1456 * cannot be freed (the same lock is acquired in delete_object).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001457 */
1458 spin_lock_irqsave(&object->lock, flags);
1459 if (object->flags & OBJECT_NO_SCAN)
1460 goto out;
1461 if (!(object->flags & OBJECT_ALLOCATED))
1462 /* already freed object */
1463 goto out;
Catalin Marinasdba82d92019-09-23 15:33:59 -07001464 if (hlist_empty(&object->area_list) ||
1465 object->flags & OBJECT_FULL_SCAN) {
Catalin Marinasaf986032009-08-27 14:29:12 +01001466 void *start = (void *)object->pointer;
1467 void *end = (void *)(object->pointer + object->size);
Catalin Marinas93ada572015-06-24 16:58:37 -07001468 void *next;
Catalin Marinasaf986032009-08-27 14:29:12 +01001469
Catalin Marinas93ada572015-06-24 16:58:37 -07001470 do {
1471 next = min(start + MAX_SCAN_SIZE, end);
1472 scan_block(start, next, object);
1473
1474 start = next;
1475 if (start >= end)
1476 break;
Catalin Marinasaf986032009-08-27 14:29:12 +01001477
1478 spin_unlock_irqrestore(&object->lock, flags);
1479 cond_resched();
1480 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas93ada572015-06-24 16:58:37 -07001481 } while (object->flags & OBJECT_ALLOCATED);
Catalin Marinasaf986032009-08-27 14:29:12 +01001482 } else
Sasha Levinb67bfe02013-02-27 17:06:00 -08001483 hlist_for_each_entry(area, &object->area_list, node)
Catalin Marinasc017b4b2009-10-28 13:33:09 +00001484 scan_block((void *)area->start,
1485 (void *)(area->start + area->size),
Catalin Marinas93ada572015-06-24 16:58:37 -07001486 object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001487out:
1488 spin_unlock_irqrestore(&object->lock, flags);
1489}
1490
1491/*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001492 * Scan the objects already referenced (gray objects). More objects will be
1493 * referenced and, if there are no memory leaks, all the objects are scanned.
1494 */
1495static void scan_gray_list(void)
1496{
1497 struct kmemleak_object *object, *tmp;
1498
1499 /*
1500 * The list traversal is safe for both tail additions and removals
1501 * from inside the loop. The kmemleak objects cannot be freed from
1502 * outside the loop because their use_count was incremented.
1503 */
1504 object = list_entry(gray_list.next, typeof(*object), gray_list);
1505 while (&object->gray_list != &gray_list) {
1506 cond_resched();
1507
1508 /* may add new objects to the list */
1509 if (!scan_should_stop())
1510 scan_object(object);
1511
1512 tmp = list_entry(object->gray_list.next, typeof(*object),
1513 gray_list);
1514
1515 /* remove the object from the list and release it */
1516 list_del(&object->gray_list);
1517 put_object(object);
1518
1519 object = tmp;
1520 }
1521 WARN_ON(!list_empty(&gray_list));
1522}
1523
1524/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001525 * Scan data sections and all the referenced memory blocks allocated via the
1526 * kernel's standard allocators. This function must be called with the
1527 * scan_mutex held.
1528 */
1529static void kmemleak_scan(void)
1530{
1531 unsigned long flags;
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001532 struct kmemleak_object *object;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001533 int i;
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001534 int new_leaks = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001535
Catalin Marinasacf49682009-06-26 17:38:29 +01001536 jiffies_last_scan = jiffies;
1537
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001538 /* prepare the kmemleak_object's */
1539 rcu_read_lock();
1540 list_for_each_entry_rcu(object, &object_list, object_list) {
1541 spin_lock_irqsave(&object->lock, flags);
1542#ifdef DEBUG
1543 /*
1544 * With a few exceptions there should be a maximum of
1545 * 1 reference to any object at this point.
1546 */
1547 if (atomic_read(&object->use_count) > 1) {
Joe Perchesae281062009-06-23 14:40:26 +01001548 pr_debug("object->use_count = %d\n",
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001549 atomic_read(&object->use_count));
1550 dump_object_info(object);
1551 }
1552#endif
1553 /* reset the reference count (whiten the object) */
1554 object->count = 0;
1555 if (color_gray(object) && get_object(object))
1556 list_add_tail(&object->gray_list, &gray_list);
1557
1558 spin_unlock_irqrestore(&object->lock, flags);
1559 }
1560 rcu_read_unlock();
1561
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001562#ifdef CONFIG_SMP
1563 /* per-cpu sections scanning */
1564 for_each_possible_cpu(i)
Catalin Marinas93ada572015-06-24 16:58:37 -07001565 scan_large_block(__per_cpu_start + per_cpu_offset(i),
1566 __per_cpu_end + per_cpu_offset(i));
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001567#endif
1568
1569 /*
Laura Abbott029aeff2011-11-15 23:49:09 +00001570 * Struct page scanning for each node.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001571 */
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001572 get_online_mems();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001573 for_each_online_node(i) {
Cody P Schafer108bcc92013-02-22 16:35:23 -08001574 unsigned long start_pfn = node_start_pfn(i);
1575 unsigned long end_pfn = node_end_pfn(i);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001576 unsigned long pfn;
1577
1578 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
Oscar Salvador9f1eb382018-12-28 00:37:57 -08001579 struct page *page = pfn_to_online_page(pfn);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001580
Oscar Salvador9f1eb382018-12-28 00:37:57 -08001581 if (!page)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001582 continue;
Oscar Salvador9f1eb382018-12-28 00:37:57 -08001583
1584 /* only scan pages belonging to this node */
1585 if (page_to_nid(page) != i)
1586 continue;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001587 /* only scan if page is in use */
1588 if (page_count(page) == 0)
1589 continue;
Catalin Marinas93ada572015-06-24 16:58:37 -07001590 scan_block(page, page + 1, NULL);
Andrew Morton13ab183d2017-12-14 15:32:31 -08001591 if (!(pfn & 63))
Yisheng Xiebde5f6b2017-11-29 16:11:08 -08001592 cond_resched();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001593 }
1594 }
Vladimir Davydovbfc8c902014-06-04 16:07:18 -07001595 put_online_mems();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001596
1597 /*
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001598 * Scanning the task stacks (may introduce false negatives).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001599 */
1600 if (kmemleak_stack_scan) {
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001601 struct task_struct *p, *g;
1602
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001603 read_lock(&tasklist_lock);
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001604 do_each_thread(g, p) {
Catalin Marinas37df49f2016-10-27 17:46:47 -07001605 void *stack = try_get_task_stack(p);
1606 if (stack) {
1607 scan_block(stack, stack + THREAD_SIZE, NULL);
1608 put_task_stack(p);
1609 }
Catalin Marinas43ed5d62009-09-01 11:12:44 +01001610 } while_each_thread(g, p);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001611 read_unlock(&tasklist_lock);
1612 }
1613
1614 /*
1615 * Scan the objects already referenced from the sections scanned
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001616 * above.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001617 */
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001618 scan_gray_list();
Catalin Marinas25873622009-07-07 10:32:58 +01001619
1620 /*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001621 * Check for new or unreferenced objects modified since the previous
1622 * scan and color them gray until the next scan.
Catalin Marinas25873622009-07-07 10:32:58 +01001623 */
1624 rcu_read_lock();
1625 list_for_each_entry_rcu(object, &object_list, object_list) {
1626 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001627 if (color_white(object) && (object->flags & OBJECT_ALLOCATED)
1628 && update_checksum(object) && get_object(object)) {
1629 /* color it gray temporarily */
1630 object->count = object->min_count;
Catalin Marinas25873622009-07-07 10:32:58 +01001631 list_add_tail(&object->gray_list, &gray_list);
1632 }
1633 spin_unlock_irqrestore(&object->lock, flags);
1634 }
1635 rcu_read_unlock();
1636
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001637 /*
1638 * Re-scan the gray list for modified unreferenced objects.
1639 */
1640 scan_gray_list();
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001641
1642 /*
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001643 * If scanning was stopped do not report any new unreferenced objects.
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001644 */
Catalin Marinas04609ccc2009-10-28 13:33:12 +00001645 if (scan_should_stop())
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001646 return;
1647
1648 /*
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001649 * Scanning result reporting.
1650 */
1651 rcu_read_lock();
1652 list_for_each_entry_rcu(object, &object_list, object_list) {
1653 spin_lock_irqsave(&object->lock, flags);
1654 if (unreferenced_object(object) &&
1655 !(object->flags & OBJECT_REPORTED)) {
1656 object->flags |= OBJECT_REPORTED;
Vincent Whitchurch154221c2018-10-26 15:03:42 -07001657
1658 if (kmemleak_verbose)
1659 print_unreferenced(NULL, object);
1660
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001661 new_leaks++;
1662 }
1663 spin_unlock_irqrestore(&object->lock, flags);
1664 }
1665 rcu_read_unlock();
1666
Li Zefandc9b3f42014-04-03 14:46:26 -07001667 if (new_leaks) {
1668 kmemleak_found_leaks = true;
1669
Joe Perches756a0252016-03-17 14:19:47 -07001670 pr_info("%d new suspected memory leaks (see /sys/kernel/debug/kmemleak)\n",
1671 new_leaks);
Li Zefandc9b3f42014-04-03 14:46:26 -07001672 }
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001673
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001674}
1675
1676/*
1677 * Thread function performing automatic memory scanning. Unreferenced objects
1678 * at the end of a memory scan are reported but only the first time.
1679 */
1680static int kmemleak_scan_thread(void *arg)
1681{
Sri Krishna chowdaryd53ce042018-12-28 00:38:54 -08001682 static int first_run = IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001683
Joe Perchesae281062009-06-23 14:40:26 +01001684 pr_info("Automatic memory scanning thread started\n");
Catalin Marinasbf2a76b2009-07-07 10:32:55 +01001685 set_user_nice(current, 10);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001686
1687 /*
1688 * Wait before the first scan to allow the system to fully initialize.
1689 */
1690 if (first_run) {
Vegard Nossum98c42d92016-07-28 15:48:32 -07001691 signed long timeout = msecs_to_jiffies(SECS_FIRST_SCAN * 1000);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001692 first_run = 0;
Vegard Nossum98c42d92016-07-28 15:48:32 -07001693 while (timeout && !kthread_should_stop())
1694 timeout = schedule_timeout_interruptible(timeout);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001695 }
1696
1697 while (!kthread_should_stop()) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001698 signed long timeout = jiffies_scan_wait;
1699
1700 mutex_lock(&scan_mutex);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001701 kmemleak_scan();
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001702 mutex_unlock(&scan_mutex);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001703
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001704 /* wait before the next scan */
1705 while (timeout && !kthread_should_stop())
1706 timeout = schedule_timeout_interruptible(timeout);
1707 }
1708
Joe Perchesae281062009-06-23 14:40:26 +01001709 pr_info("Automatic memory scanning thread ended\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001710
1711 return 0;
1712}
1713
1714/*
1715 * Start the automatic memory scanning thread. This function must be called
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001716 * with the scan_mutex held.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001717 */
Luis R. Rodriguez7eb0d5e2009-09-08 17:31:45 +01001718static void start_scan_thread(void)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001719{
1720 if (scan_thread)
1721 return;
1722 scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak");
1723 if (IS_ERR(scan_thread)) {
Joe Perches598d8092016-03-17 14:19:44 -07001724 pr_warn("Failed to create the scan thread\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001725 scan_thread = NULL;
1726 }
1727}
1728
1729/*
Vinayak Menon914b6df2018-03-28 16:01:16 -07001730 * Stop the automatic memory scanning thread.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001731 */
Luis R. Rodriguez7eb0d5e2009-09-08 17:31:45 +01001732static void stop_scan_thread(void)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001733{
1734 if (scan_thread) {
1735 kthread_stop(scan_thread);
1736 scan_thread = NULL;
1737 }
1738}
1739
1740/*
1741 * Iterate over the object_list and return the first valid object at or after
1742 * the required position with its use_count incremented. The function triggers
1743 * a memory scanning when the pos argument points to the first position.
1744 */
1745static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
1746{
1747 struct kmemleak_object *object;
1748 loff_t n = *pos;
Catalin Marinasb87324d2009-07-07 10:32:58 +01001749 int err;
1750
1751 err = mutex_lock_interruptible(&scan_mutex);
1752 if (err < 0)
1753 return ERR_PTR(err);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001754
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001755 rcu_read_lock();
1756 list_for_each_entry_rcu(object, &object_list, object_list) {
1757 if (n-- > 0)
1758 continue;
1759 if (get_object(object))
1760 goto out;
1761 }
1762 object = NULL;
1763out:
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001764 return object;
1765}
1766
1767/*
1768 * Return the next object in the object_list. The function decrements the
1769 * use_count of the previous object and increases that of the next one.
1770 */
1771static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1772{
1773 struct kmemleak_object *prev_obj = v;
1774 struct kmemleak_object *next_obj = NULL;
Michael Wang58fac092012-08-17 12:33:34 +08001775 struct kmemleak_object *obj = prev_obj;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001776
1777 ++(*pos);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001778
Michael Wang58fac092012-08-17 12:33:34 +08001779 list_for_each_entry_continue_rcu(obj, &object_list, object_list) {
Catalin Marinas52c3ce42011-04-27 16:44:26 +01001780 if (get_object(obj)) {
1781 next_obj = obj;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001782 break;
Catalin Marinas52c3ce42011-04-27 16:44:26 +01001783 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001784 }
Catalin Marinas288c8572009-07-07 10:32:57 +01001785
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001786 put_object(prev_obj);
1787 return next_obj;
1788}
1789
1790/*
1791 * Decrement the use_count of the last object required, if any.
1792 */
1793static void kmemleak_seq_stop(struct seq_file *seq, void *v)
1794{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001795 if (!IS_ERR(v)) {
1796 /*
1797 * kmemleak_seq_start may return ERR_PTR if the scan_mutex
1798 * waiting was interrupted, so only release it if !IS_ERR.
1799 */
Catalin Marinasf5886c72009-07-29 16:26:57 +01001800 rcu_read_unlock();
Catalin Marinasb87324d2009-07-07 10:32:58 +01001801 mutex_unlock(&scan_mutex);
1802 if (v)
1803 put_object(v);
1804 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001805}
1806
1807/*
1808 * Print the information for an unreferenced object to the seq file.
1809 */
1810static int kmemleak_seq_show(struct seq_file *seq, void *v)
1811{
1812 struct kmemleak_object *object = v;
1813 unsigned long flags;
1814
1815 spin_lock_irqsave(&object->lock, flags);
Catalin Marinas288c8572009-07-07 10:32:57 +01001816 if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object))
Catalin Marinas17bb9e02009-06-29 17:13:56 +01001817 print_unreferenced(seq, object);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001818 spin_unlock_irqrestore(&object->lock, flags);
1819 return 0;
1820}
1821
1822static const struct seq_operations kmemleak_seq_ops = {
1823 .start = kmemleak_seq_start,
1824 .next = kmemleak_seq_next,
1825 .stop = kmemleak_seq_stop,
1826 .show = kmemleak_seq_show,
1827};
1828
1829static int kmemleak_open(struct inode *inode, struct file *file)
1830{
Catalin Marinasb87324d2009-07-07 10:32:58 +01001831 return seq_open(file, &kmemleak_seq_ops);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001832}
1833
Catalin Marinas189d84e2009-08-27 14:29:15 +01001834static int dump_str_object_info(const char *str)
1835{
1836 unsigned long flags;
1837 struct kmemleak_object *object;
1838 unsigned long addr;
1839
Abhijit Pawardc053732012-12-18 14:23:27 -08001840 if (kstrtoul(str, 0, &addr))
1841 return -EINVAL;
Catalin Marinas189d84e2009-08-27 14:29:15 +01001842 object = find_and_get_object(addr, 0);
1843 if (!object) {
1844 pr_info("Unknown object at 0x%08lx\n", addr);
1845 return -EINVAL;
1846 }
1847
1848 spin_lock_irqsave(&object->lock, flags);
1849 dump_object_info(object);
1850 spin_unlock_irqrestore(&object->lock, flags);
1851
1852 put_object(object);
1853 return 0;
1854}
1855
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001856/*
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001857 * We use grey instead of black to ensure we can do future scans on the same
1858 * objects. If we did not do future scans these black objects could
1859 * potentially contain references to newly allocated objects in the future and
1860 * we'd end up with false positives.
1861 */
1862static void kmemleak_clear(void)
1863{
1864 struct kmemleak_object *object;
1865 unsigned long flags;
1866
1867 rcu_read_lock();
1868 list_for_each_entry_rcu(object, &object_list, object_list) {
1869 spin_lock_irqsave(&object->lock, flags);
1870 if ((object->flags & OBJECT_REPORTED) &&
1871 unreferenced_object(object))
Luis R. Rodrigueza1084c82009-09-04 17:44:52 -07001872 __paint_it(object, KMEMLEAK_GREY);
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001873 spin_unlock_irqrestore(&object->lock, flags);
1874 }
1875 rcu_read_unlock();
Li Zefandc9b3f42014-04-03 14:46:26 -07001876
1877 kmemleak_found_leaks = false;
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001878}
1879
Li Zefanc89da702014-04-03 14:46:27 -07001880static void __kmemleak_do_cleanup(void);
1881
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001882/*
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001883 * File write operation to configure kmemleak at run-time. The following
1884 * commands can be written to the /sys/kernel/debug/kmemleak file:
1885 * off - disable kmemleak (irreversible)
1886 * stack=on - enable the task stacks scanning
1887 * stack=off - disable the tasks stacks scanning
1888 * scan=on - start the automatic memory scanning thread
1889 * scan=off - stop the automatic memory scanning thread
1890 * scan=... - set the automatic memory scanning period in seconds (0 to
1891 * disable it)
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001892 * scan - trigger a memory scan
Luis R. Rodriguez30b37102009-09-04 17:44:51 -07001893 * clear - mark all current reported unreferenced kmemleak objects as
Li Zefanc89da702014-04-03 14:46:27 -07001894 * grey to ignore printing them, or free all kmemleak objects
1895 * if kmemleak has been disabled.
Catalin Marinas189d84e2009-08-27 14:29:15 +01001896 * dump=... - dump information about the object found at the given address
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001897 */
1898static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
1899 size_t size, loff_t *ppos)
1900{
1901 char buf[64];
1902 int buf_size;
Catalin Marinasb87324d2009-07-07 10:32:58 +01001903 int ret;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001904
1905 buf_size = min(size, (sizeof(buf) - 1));
1906 if (strncpy_from_user(buf, user_buf, buf_size) < 0)
1907 return -EFAULT;
1908 buf[buf_size] = 0;
1909
Catalin Marinasb87324d2009-07-07 10:32:58 +01001910 ret = mutex_lock_interruptible(&scan_mutex);
1911 if (ret < 0)
1912 return ret;
1913
Li Zefanc89da702014-04-03 14:46:27 -07001914 if (strncmp(buf, "clear", 5) == 0) {
Li Zefan8910ae892014-04-03 14:46:29 -07001915 if (kmemleak_enabled)
Li Zefanc89da702014-04-03 14:46:27 -07001916 kmemleak_clear();
1917 else
1918 __kmemleak_do_cleanup();
1919 goto out;
1920 }
1921
Li Zefan8910ae892014-04-03 14:46:29 -07001922 if (!kmemleak_enabled) {
André Almeida4e4dfce2019-07-11 20:53:43 -07001923 ret = -EPERM;
Li Zefanc89da702014-04-03 14:46:27 -07001924 goto out;
1925 }
1926
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001927 if (strncmp(buf, "off", 3) == 0)
1928 kmemleak_disable();
1929 else if (strncmp(buf, "stack=on", 8) == 0)
1930 kmemleak_stack_scan = 1;
1931 else if (strncmp(buf, "stack=off", 9) == 0)
1932 kmemleak_stack_scan = 0;
1933 else if (strncmp(buf, "scan=on", 7) == 0)
1934 start_scan_thread();
1935 else if (strncmp(buf, "scan=off", 8) == 0)
1936 stop_scan_thread();
1937 else if (strncmp(buf, "scan=", 5) == 0) {
1938 unsigned long secs;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001939
Jingoo Han3dbb95f2013-09-11 14:20:25 -07001940 ret = kstrtoul(buf + 5, 0, &secs);
Catalin Marinasb87324d2009-07-07 10:32:58 +01001941 if (ret < 0)
1942 goto out;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001943 stop_scan_thread();
1944 if (secs) {
1945 jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
1946 start_scan_thread();
1947 }
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001948 } else if (strncmp(buf, "scan", 4) == 0)
1949 kmemleak_scan();
Catalin Marinas189d84e2009-08-27 14:29:15 +01001950 else if (strncmp(buf, "dump=", 5) == 0)
1951 ret = dump_str_object_info(buf + 5);
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001952 else
Catalin Marinasb87324d2009-07-07 10:32:58 +01001953 ret = -EINVAL;
1954
1955out:
1956 mutex_unlock(&scan_mutex);
1957 if (ret < 0)
1958 return ret;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001959
1960 /* ignore the rest of the buffer, only one command at a time */
1961 *ppos += size;
1962 return size;
1963}
1964
1965static const struct file_operations kmemleak_fops = {
1966 .owner = THIS_MODULE,
1967 .open = kmemleak_open,
1968 .read = seq_read,
1969 .write = kmemleak_write,
1970 .llseek = seq_lseek,
Li Zefan5f3bf192014-04-03 14:46:28 -07001971 .release = seq_release,
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001972};
1973
Li Zefanc89da702014-04-03 14:46:27 -07001974static void __kmemleak_do_cleanup(void)
1975{
1976 struct kmemleak_object *object;
1977
1978 rcu_read_lock();
1979 list_for_each_entry_rcu(object, &object_list, object_list)
1980 delete_object_full(object->pointer);
1981 rcu_read_unlock();
1982}
1983
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001984/*
Catalin Marinas74341702011-09-29 11:50:07 +01001985 * Stop the memory scanning thread and free the kmemleak internal objects if
1986 * no previous scan thread (otherwise, kmemleak may still have some useful
1987 * information on memory leaks).
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001988 */
Catalin Marinas179a8102009-09-07 10:14:42 +01001989static void kmemleak_do_cleanup(struct work_struct *work)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01001990{
Catalin Marinas4698c1f2009-06-26 17:38:27 +01001991 stop_scan_thread();
1992
Vinayak Menon914b6df2018-03-28 16:01:16 -07001993 mutex_lock(&scan_mutex);
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07001994 /*
Vinayak Menon914b6df2018-03-28 16:01:16 -07001995 * Once it is made sure that kmemleak_scan has stopped, it is safe to no
1996 * longer track object freeing. Ordering of the scan thread stopping and
1997 * the memory accesses below is guaranteed by the kthread_stop()
1998 * function.
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07001999 */
2000 kmemleak_free_enabled = 0;
Vinayak Menon914b6df2018-03-28 16:01:16 -07002001 mutex_unlock(&scan_mutex);
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07002002
Li Zefanc89da702014-04-03 14:46:27 -07002003 if (!kmemleak_found_leaks)
2004 __kmemleak_do_cleanup();
2005 else
Joe Perches756a0252016-03-17 14:19:47 -07002006 pr_info("Kmemleak disabled without freeing internal data. Reclaim the memory with \"echo clear > /sys/kernel/debug/kmemleak\".\n");
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002007}
2008
Catalin Marinas179a8102009-09-07 10:14:42 +01002009static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002010
2011/*
2012 * Disable kmemleak. No memory allocation/freeing will be traced once this
2013 * function is called. Disabling kmemleak is an irreversible operation.
2014 */
2015static void kmemleak_disable(void)
2016{
2017 /* atomically check whether it was already invoked */
Li Zefan8910ae892014-04-03 14:46:29 -07002018 if (cmpxchg(&kmemleak_error, 0, 1))
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002019 return;
2020
2021 /* stop any memory operation tracing */
Li Zefan8910ae892014-04-03 14:46:29 -07002022 kmemleak_enabled = 0;
Catalin Marinasfcf3a5b2019-08-13 15:37:34 -07002023 kmemleak_early_log = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002024
2025 /* check whether it is too early for a kernel thread */
Li Zefan8910ae892014-04-03 14:46:29 -07002026 if (kmemleak_initialized)
Catalin Marinas179a8102009-09-07 10:14:42 +01002027 schedule_work(&cleanup_work);
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07002028 else
2029 kmemleak_free_enabled = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002030
2031 pr_info("Kernel memory leak detector disabled\n");
2032}
2033
2034/*
2035 * Allow boot-time kmemleak disabling (enabled by default).
2036 */
Dou Liyang8bd30c12018-04-05 16:23:46 -07002037static int __init kmemleak_boot_config(char *str)
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002038{
2039 if (!str)
2040 return -EINVAL;
2041 if (strcmp(str, "off") == 0)
2042 kmemleak_disable();
Jason Baronab0155a2010-07-19 11:54:17 +01002043 else if (strcmp(str, "on") == 0)
2044 kmemleak_skip_disable = 1;
2045 else
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002046 return -EINVAL;
2047 return 0;
2048}
2049early_param("kmemleak", kmemleak_boot_config);
2050
Catalin Marinas5f790202011-09-28 12:17:03 +01002051static void __init print_log_trace(struct early_log *log)
2052{
Catalin Marinas5f790202011-09-28 12:17:03 +01002053 pr_notice("Early log backtrace:\n");
Thomas Gleixner07984aa2019-04-25 11:45:01 +02002054 stack_trace_print(log->trace, log->trace_len, 2);
Catalin Marinas5f790202011-09-28 12:17:03 +01002055}
2056
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002057/*
Catalin Marinas20301172009-06-17 18:29:04 +01002058 * Kmemleak initialization.
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002059 */
2060void __init kmemleak_init(void)
2061{
2062 int i;
2063 unsigned long flags;
2064
Jason Baronab0155a2010-07-19 11:54:17 +01002065#ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF
2066 if (!kmemleak_skip_disable) {
2067 kmemleak_disable();
2068 return;
2069 }
2070#endif
2071
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002072 jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE);
2073 jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000);
2074
2075 object_cache = KMEM_CACHE(kmemleak_object, SLAB_NOLEAKTRACE);
2076 scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002077
Wang Kai21cd3a62015-09-08 15:03:41 -07002078 if (crt_early_log > ARRAY_SIZE(early_log))
Joe Perches598d8092016-03-17 14:19:44 -07002079 pr_warn("Early log buffer exceeded (%d), please increase DEBUG_KMEMLEAK_EARLY_LOG_SIZE\n",
2080 crt_early_log);
Catalin Marinasb6693002011-09-28 17:22:56 +01002081
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002082 /* the kernel is still in UP mode, so disabling the IRQs is enough */
2083 local_irq_save(flags);
Catalin Marinas3551a922014-05-09 15:36:59 -07002084 kmemleak_early_log = 0;
Li Zefan8910ae892014-04-03 14:46:29 -07002085 if (kmemleak_error) {
Catalin Marinasb6693002011-09-28 17:22:56 +01002086 local_irq_restore(flags);
2087 return;
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07002088 } else {
Li Zefan8910ae892014-04-03 14:46:29 -07002089 kmemleak_enabled = 1;
Catalin Marinasc5f3b1a2015-06-24 16:58:26 -07002090 kmemleak_free_enabled = 1;
2091 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002092 local_irq_restore(flags);
2093
Catalin Marinas298a32b2019-04-05 18:38:49 -07002094 /* register the data/bss sections */
2095 create_object((unsigned long)_sdata, _edata - _sdata,
2096 KMEMLEAK_GREY, GFP_ATOMIC);
2097 create_object((unsigned long)__bss_start, __bss_stop - __bss_start,
2098 KMEMLEAK_GREY, GFP_ATOMIC);
2099 /* only register .data..ro_after_init if not within .data */
2100 if (__start_ro_after_init < _sdata || __end_ro_after_init > _edata)
2101 create_object((unsigned long)__start_ro_after_init,
2102 __end_ro_after_init - __start_ro_after_init,
2103 KMEMLEAK_GREY, GFP_ATOMIC);
2104
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002105 /*
2106 * This is the point where tracking allocations is safe. Automatic
2107 * scanning is started during the late initcall. Add the early logged
2108 * callbacks to the kmemleak infrastructure.
2109 */
2110 for (i = 0; i < crt_early_log; i++) {
2111 struct early_log *log = &early_log[i];
2112
2113 switch (log->op_type) {
2114 case KMEMLEAK_ALLOC:
Catalin Marinasfd678962009-08-27 14:29:17 +01002115 early_alloc(log);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002116 break;
Catalin Marinasf528f0b2011-09-26 17:12:53 +01002117 case KMEMLEAK_ALLOC_PERCPU:
2118 early_alloc_percpu(log);
2119 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002120 case KMEMLEAK_FREE:
2121 kmemleak_free(log->ptr);
2122 break;
Catalin Marinas53238a62009-07-07 10:33:00 +01002123 case KMEMLEAK_FREE_PART:
2124 kmemleak_free_part(log->ptr, log->size);
2125 break;
Catalin Marinasf528f0b2011-09-26 17:12:53 +01002126 case KMEMLEAK_FREE_PERCPU:
2127 kmemleak_free_percpu(log->ptr);
2128 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002129 case KMEMLEAK_NOT_LEAK:
2130 kmemleak_not_leak(log->ptr);
2131 break;
2132 case KMEMLEAK_IGNORE:
2133 kmemleak_ignore(log->ptr);
2134 break;
2135 case KMEMLEAK_SCAN_AREA:
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002136 kmemleak_scan_area(log->ptr, log->size, GFP_KERNEL);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002137 break;
2138 case KMEMLEAK_NO_SCAN:
2139 kmemleak_no_scan(log->ptr);
2140 break;
Catalin Marinas94f4a162017-07-06 15:40:22 -07002141 case KMEMLEAK_SET_EXCESS_REF:
2142 object_set_excess_ref((unsigned long)log->ptr,
2143 log->excess_ref);
2144 break;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002145 default:
Catalin Marinas5f790202011-09-28 12:17:03 +01002146 kmemleak_warn("Unknown early log operation: %d\n",
2147 log->op_type);
2148 }
2149
Li Zefan8910ae892014-04-03 14:46:29 -07002150 if (kmemleak_warning) {
Catalin Marinas5f790202011-09-28 12:17:03 +01002151 print_log_trace(log);
Li Zefan8910ae892014-04-03 14:46:29 -07002152 kmemleak_warning = 0;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002153 }
2154 }
2155}
2156
2157/*
2158 * Late initialization function.
2159 */
2160static int __init kmemleak_late_init(void)
2161{
Li Zefan8910ae892014-04-03 14:46:29 -07002162 kmemleak_initialized = 1;
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002163
Greg Kroah-Hartman282401d2019-01-22 16:21:12 +01002164 debugfs_create_file("kmemleak", 0644, NULL, NULL, &kmemleak_fops);
Vincent Whitchurchb3537562018-09-04 15:45:44 -07002165
Li Zefan8910ae892014-04-03 14:46:29 -07002166 if (kmemleak_error) {
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002167 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002168 * Some error occurred and kmemleak was disabled. There is a
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002169 * small chance that kmemleak_disable() was called immediately
2170 * after setting kmemleak_initialized and we may end up with
2171 * two clean-up threads but serialized by scan_mutex.
2172 */
Catalin Marinas179a8102009-09-07 10:14:42 +01002173 schedule_work(&cleanup_work);
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002174 return -ENOMEM;
2175 }
2176
Sri Krishna chowdaryd53ce042018-12-28 00:38:54 -08002177 if (IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN)) {
2178 mutex_lock(&scan_mutex);
2179 start_scan_thread();
2180 mutex_unlock(&scan_mutex);
2181 }
Catalin Marinas3c7b4e62009-06-11 13:22:39 +01002182
2183 pr_info("Kernel memory leak detector initialized\n");
2184
2185 return 0;
2186}
2187late_initcall(kmemleak_late_init);