]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - mm/kmemleak.c
Linux 6.10-rc1
[thirdparty/kernel/stable.git] / mm / kmemleak.c
CommitLineData
45051539 1// SPDX-License-Identifier: GPL-2.0-only
3c7b4e6b
CM
2/*
3 * mm/kmemleak.c
4 *
5 * Copyright (C) 2008 ARM Limited
6 * Written by Catalin Marinas <catalin.marinas@arm.com>
7 *
3c7b4e6b 8 * For more information on the algorithm and kmemleak usage, please see
22901c6c 9 * Documentation/dev-tools/kmemleak.rst.
3c7b4e6b
CM
10 *
11 * Notes on locking
12 * ----------------
13 *
14 * The following locks and mutexes are used by kmemleak:
15 *
782e4179 16 * - kmemleak_lock (raw_spinlock_t): protects the object_list as well as
39042079
CM
17 * del_state modifications and accesses to the object trees
18 * (object_tree_root, object_phys_tree_root, object_percpu_tree_root). The
19 * object_list is the main list holding the metadata (struct
20 * kmemleak_object) for the allocated memory blocks. The object trees are
21 * red black trees used to look-up metadata based on a pointer to the
22 * corresponding memory block. The kmemleak_object structures are added to
23 * the object_list and the object tree root in the create_object() function
24 * called from the kmemleak_alloc{,_phys,_percpu}() callback and removed in
25 * delete_object() called from the kmemleak_free{,_phys,_percpu}() callback
8c96f1bc
HZ
26 * - kmemleak_object.lock (raw_spinlock_t): protects a kmemleak_object.
27 * Accesses to the metadata (e.g. count) are protected by this lock. Note
28 * that some members of this structure may be protected by other means
29 * (atomic or kmemleak_lock). This lock is also held when scanning the
30 * corresponding memory block to avoid the kernel freeing it via the
31 * kmemleak_free() callback. This is less heavyweight than holding a global
32 * lock like kmemleak_lock during scanning.
3c7b4e6b
CM
33 * - scan_mutex (mutex): ensures that only one thread may scan the memory for
34 * unreferenced objects at a time. The gray_list contains the objects which
35 * are already referenced or marked as false positives and need to be
36 * scanned. This list is only modified during a scanning episode when the
37 * scan_mutex is held. At the end of a scan, the gray_list is always empty.
38 * Note that the kmemleak_object.use_count is incremented when an object is
4698c1f2
CM
39 * added to the gray_list and therefore cannot be freed. This mutex also
40 * prevents multiple users of the "kmemleak" debugfs file together with
41 * modifications to the memory scanning parameters including the scan_thread
42 * pointer
3c7b4e6b 43 *
93ada579 44 * Locks and mutexes are acquired/nested in the following order:
9d5a4c73 45 *
93ada579
CM
46 * scan_mutex [-> object->lock] -> kmemleak_lock -> other_object->lock (SINGLE_DEPTH_NESTING)
47 *
48 * No kmemleak_lock and object->lock nesting is allowed outside scan_mutex
49 * regions.
9d5a4c73 50 *
3c7b4e6b
CM
51 * The kmemleak_object structures have a use_count incremented or decremented
52 * using the get_object()/put_object() functions. When the use_count becomes
53 * 0, this count can no longer be incremented and put_object() schedules the
54 * kmemleak_object freeing via an RCU callback. All calls to the get_object()
55 * function must be protected by rcu_read_lock() to avoid accessing a freed
56 * structure.
57 */
58
ae281064
JP
59#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
60
3c7b4e6b
CM
61#include <linux/init.h>
62#include <linux/kernel.h>
63#include <linux/list.h>
3f07c014 64#include <linux/sched/signal.h>
29930025 65#include <linux/sched/task.h>
68db0cf1 66#include <linux/sched/task_stack.h>
3c7b4e6b
CM
67#include <linux/jiffies.h>
68#include <linux/delay.h>
b95f1b31 69#include <linux/export.h>
3c7b4e6b 70#include <linux/kthread.h>
85d3a316 71#include <linux/rbtree.h>
3c7b4e6b
CM
72#include <linux/fs.h>
73#include <linux/debugfs.h>
74#include <linux/seq_file.h>
75#include <linux/cpumask.h>
76#include <linux/spinlock.h>
154221c3 77#include <linux/module.h>
3c7b4e6b
CM
78#include <linux/mutex.h>
79#include <linux/rcupdate.h>
80#include <linux/stacktrace.h>
56a61617 81#include <linux/stackdepot.h>
3c7b4e6b
CM
82#include <linux/cache.h>
83#include <linux/percpu.h>
57c8a661 84#include <linux/memblock.h>
9099daed 85#include <linux/pfn.h>
3c7b4e6b
CM
86#include <linux/mmzone.h>
87#include <linux/slab.h>
88#include <linux/thread_info.h>
89#include <linux/err.h>
90#include <linux/uaccess.h>
91#include <linux/string.h>
92#include <linux/nodemask.h>
93#include <linux/mm.h>
179a8100 94#include <linux/workqueue.h>
04609ccc 95#include <linux/crc32.h>
3c7b4e6b
CM
96
97#include <asm/sections.h>
98#include <asm/processor.h>
60063497 99#include <linux/atomic.h>
3c7b4e6b 100
e79ed2f1 101#include <linux/kasan.h>
95511580 102#include <linux/kfence.h>
3c7b4e6b 103#include <linux/kmemleak.h>
029aeff5 104#include <linux/memory_hotplug.h>
3c7b4e6b
CM
105
106/*
107 * Kmemleak configuration and common defines.
108 */
109#define MAX_TRACE 16 /* stack trace length */
3c7b4e6b 110#define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
3c7b4e6b
CM
111#define SECS_FIRST_SCAN 60 /* delay before the first scan */
112#define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */
af98603d 113#define MAX_SCAN_SIZE 4096 /* maximum size of a scanned block */
3c7b4e6b
CM
114
115#define BYTES_PER_POINTER sizeof(void *)
116
117/* scanning area inside a memory block */
118struct kmemleak_scan_area {
119 struct hlist_node node;
c017b4be
CM
120 unsigned long start;
121 size_t size;
3c7b4e6b
CM
122};
123
a1084c87
LR
124#define KMEMLEAK_GREY 0
125#define KMEMLEAK_BLACK -1
126
3c7b4e6b
CM
127/*
128 * Structure holding the metadata for each allocated memory block.
129 * Modifications to such objects should be made while holding the
130 * object->lock. Insertions or deletions from object_list, gray_list or
85d3a316 131 * rb_node are already protected by the corresponding locks or mutex (see
3c7b4e6b
CM
132 * the notes on locking above). These objects are reference-counted
133 * (use_count) and freed using the RCU mechanism.
134 */
135struct kmemleak_object {
8c96f1bc 136 raw_spinlock_t lock;
f66abf09 137 unsigned int flags; /* object status flags */
3c7b4e6b
CM
138 struct list_head object_list;
139 struct list_head gray_list;
85d3a316 140 struct rb_node rb_node;
3c7b4e6b
CM
141 struct rcu_head rcu; /* object_list lockless traversal */
142 /* object usage count; object freed when use_count == 0 */
143 atomic_t use_count;
782e4179 144 unsigned int del_state; /* deletion state */
3c7b4e6b
CM
145 unsigned long pointer;
146 size_t size;
94f4a161
CM
147 /* pass surplus references to this pointer */
148 unsigned long excess_ref;
3c7b4e6b
CM
149 /* minimum number of a pointers found before it is considered leak */
150 int min_count;
151 /* the total number of pointers found pointing to this object */
152 int count;
04609ccc
CM
153 /* checksum for detecting modified objects */
154 u32 checksum;
b04da042 155 depot_stack_handle_t trace_handle;
3c7b4e6b
CM
156 /* memory ranges to be scanned inside an object (empty for all) */
157 struct hlist_head area_list;
3c7b4e6b
CM
158 unsigned long jiffies; /* creation timestamp */
159 pid_t pid; /* pid of the current task */
160 char comm[TASK_COMM_LEN]; /* executable name */
161};
162
163/* flag representing the memory block allocation status */
164#define OBJECT_ALLOCATED (1 << 0)
165/* flag set after the first reporting of an unreference object */
166#define OBJECT_REPORTED (1 << 1)
167/* flag set to not scan the object */
168#define OBJECT_NO_SCAN (1 << 2)
dba82d94
CM
169/* flag set to fully scan the object when scan_area allocation failed */
170#define OBJECT_FULL_SCAN (1 << 3)
8e0c4ab3
PW
171/* flag set for object allocated with physical address */
172#define OBJECT_PHYS (1 << 4)
39042079
CM
173/* flag set for per-CPU pointers */
174#define OBJECT_PERCPU (1 << 5)
3c7b4e6b 175
782e4179
WL
176/* set when __remove_object() called */
177#define DELSTATE_REMOVED (1 << 0)
178/* set to temporarily prevent deletion from object_list */
179#define DELSTATE_NO_DELETE (1 << 1)
180
154221c3 181#define HEX_PREFIX " "
0494e082
SS
182/* number of bytes to print per line; must be 16 or 32 */
183#define HEX_ROW_SIZE 16
184/* number of bytes to print at a time (1, 2, 4, 8) */
185#define HEX_GROUP_SIZE 1
186/* include ASCII after the hex output */
187#define HEX_ASCII 1
188/* max number of lines to be printed */
189#define HEX_MAX_LINES 2
190
3c7b4e6b
CM
191/* the list of all allocated objects */
192static LIST_HEAD(object_list);
193/* the list of gray-colored objects (see color_gray comment below) */
194static LIST_HEAD(gray_list);
0647398a 195/* memory pool allocation */
c5665868 196static struct kmemleak_object mem_pool[CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE];
0647398a
CM
197static int mem_pool_free_count = ARRAY_SIZE(mem_pool);
198static LIST_HEAD(mem_pool_free_list);
85d3a316
ML
199/* search tree for object boundaries */
200static struct rb_root object_tree_root = RB_ROOT;
0c24e061
PW
201/* search tree for object (with OBJECT_PHYS flag) boundaries */
202static struct rb_root object_phys_tree_root = RB_ROOT;
39042079
CM
203/* search tree for object (with OBJECT_PERCPU flag) boundaries */
204static struct rb_root object_percpu_tree_root = RB_ROOT;
0c24e061 205/* protecting the access to object_list, object_tree_root (or object_phys_tree_root) */
8c96f1bc 206static DEFINE_RAW_SPINLOCK(kmemleak_lock);
3c7b4e6b
CM
207
208/* allocation caches for kmemleak internal data */
209static struct kmem_cache *object_cache;
210static struct kmem_cache *scan_area_cache;
211
212/* set if tracing memory operations is enabled */
c5665868 213static int kmemleak_enabled = 1;
c5f3b1a5 214/* same as above but only for the kmemleak_free() callback */
c5665868 215static int kmemleak_free_enabled = 1;
3c7b4e6b 216/* set in the late_initcall if there were no errors */
d160ef71 217static int kmemleak_late_initialized;
5f79020c 218/* set if a kmemleak warning was issued */
8910ae89 219static int kmemleak_warning;
5f79020c 220/* set if a fatal kmemleak error has occurred */
8910ae89 221static int kmemleak_error;
3c7b4e6b
CM
222
223/* minimum and maximum address that may be valid pointers */
224static unsigned long min_addr = ULONG_MAX;
225static unsigned long max_addr;
226
3c7b4e6b 227static struct task_struct *scan_thread;
acf4968e 228/* used to avoid reporting of recently allocated objects */
3c7b4e6b 229static unsigned long jiffies_min_age;
acf4968e 230static unsigned long jiffies_last_scan;
3c7b4e6b 231/* delay between automatic memory scannings */
54dd200c 232static unsigned long jiffies_scan_wait;
3c7b4e6b 233/* enables or disables the task stacks scanning */
e0a2a160 234static int kmemleak_stack_scan = 1;
4698c1f2 235/* protects the memory scanning, parameters and debug/kmemleak file access */
3c7b4e6b 236static DEFINE_MUTEX(scan_mutex);
ab0155a2
JB
237/* setting kmemleak=on, will set this var, skipping the disable */
238static int kmemleak_skip_disable;
dc9b3f42
LZ
239/* If there are leaks that can be reported */
240static bool kmemleak_found_leaks;
3c7b4e6b 241
154221c3
VW
242static bool kmemleak_verbose;
243module_param_named(verbose, kmemleak_verbose, bool, 0600);
244
3c7b4e6b
CM
245static void kmemleak_disable(void);
246
247/*
248 * Print a warning and dump the stack trace.
249 */
5f79020c 250#define kmemleak_warn(x...) do { \
598d8091 251 pr_warn(x); \
5f79020c 252 dump_stack(); \
8910ae89 253 kmemleak_warning = 1; \
3c7b4e6b
CM
254} while (0)
255
256/*
25985edc 257 * Macro invoked when a serious kmemleak condition occurred and cannot be
2030117d 258 * recovered from. Kmemleak will be disabled and further allocation/freeing
3c7b4e6b
CM
259 * tracing no longer available.
260 */
000814f4 261#define kmemleak_stop(x...) do { \
3c7b4e6b
CM
262 kmemleak_warn(x); \
263 kmemleak_disable(); \
264} while (0)
265
154221c3
VW
266#define warn_or_seq_printf(seq, fmt, ...) do { \
267 if (seq) \
268 seq_printf(seq, fmt, ##__VA_ARGS__); \
269 else \
270 pr_warn(fmt, ##__VA_ARGS__); \
271} while (0)
272
273static void warn_or_seq_hex_dump(struct seq_file *seq, int prefix_type,
274 int rowsize, int groupsize, const void *buf,
275 size_t len, bool ascii)
276{
277 if (seq)
278 seq_hex_dump(seq, HEX_PREFIX, prefix_type, rowsize, groupsize,
279 buf, len, ascii);
280 else
281 print_hex_dump(KERN_WARNING, pr_fmt(HEX_PREFIX), prefix_type,
282 rowsize, groupsize, buf, len, ascii);
283}
284
0494e082
SS
285/*
286 * Printing of the objects hex dump to the seq file. The number of lines to be
287 * printed is limited to HEX_MAX_LINES to prevent seq file spamming. The
288 * actual number of printed bytes depends on HEX_ROW_SIZE. It must be called
289 * with the object->lock held.
290 */
291static void hex_dump_object(struct seq_file *seq,
292 struct kmemleak_object *object)
293{
294 const u8 *ptr = (const u8 *)object->pointer;
6fc37c49 295 size_t len;
0494e082 296
39042079 297 if (WARN_ON_ONCE(object->flags & (OBJECT_PHYS | OBJECT_PERCPU)))
0c24e061
PW
298 return;
299
0494e082 300 /* limit the number of lines to HEX_MAX_LINES */
6fc37c49 301 len = min_t(size_t, object->size, HEX_MAX_LINES * HEX_ROW_SIZE);
0494e082 302
154221c3 303 warn_or_seq_printf(seq, " hex dump (first %zu bytes):\n", len);
5c335fe0 304 kasan_disable_current();
154221c3 305 warn_or_seq_hex_dump(seq, DUMP_PREFIX_NONE, HEX_ROW_SIZE,
6c7a00b8 306 HEX_GROUP_SIZE, kasan_reset_tag((void *)ptr), len, HEX_ASCII);
5c335fe0 307 kasan_enable_current();
0494e082
SS
308}
309
3c7b4e6b
CM
310/*
311 * Object colors, encoded with count and min_count:
312 * - white - orphan object, not enough references to it (count < min_count)
313 * - gray - not orphan, not marked as false positive (min_count == 0) or
314 * sufficient references to it (count >= min_count)
315 * - black - ignore, it doesn't contain references (e.g. text section)
316 * (min_count == -1). No function defined for this color.
317 * Newly created objects don't have any color assigned (object->count == -1)
318 * before the next memory scan when they become white.
319 */
4a558dd6 320static bool color_white(const struct kmemleak_object *object)
3c7b4e6b 321{
a1084c87
LR
322 return object->count != KMEMLEAK_BLACK &&
323 object->count < object->min_count;
3c7b4e6b
CM
324}
325
4a558dd6 326static bool color_gray(const struct kmemleak_object *object)
3c7b4e6b 327{
a1084c87
LR
328 return object->min_count != KMEMLEAK_BLACK &&
329 object->count >= object->min_count;
3c7b4e6b
CM
330}
331
3c7b4e6b
CM
332/*
333 * Objects are considered unreferenced only if their color is white, they have
334 * not be deleted and have a minimum age to avoid false positives caused by
335 * pointers temporarily stored in CPU registers.
336 */
4a558dd6 337static bool unreferenced_object(struct kmemleak_object *object)
3c7b4e6b 338{
04609ccc 339 return (color_white(object) && object->flags & OBJECT_ALLOCATED) &&
acf4968e
CM
340 time_before_eq(object->jiffies + jiffies_min_age,
341 jiffies_last_scan);
3c7b4e6b
CM
342}
343
344/*
bab4a34a
CM
345 * Printing of the unreferenced objects information to the seq file. The
346 * print_unreferenced function must be called with the object->lock held.
3c7b4e6b 347 */
3c7b4e6b
CM
348static void print_unreferenced(struct seq_file *seq,
349 struct kmemleak_object *object)
350{
351 int i;
56a61617
ZH
352 unsigned long *entries;
353 unsigned int nr_entries;
3c7b4e6b 354
56a61617 355 nr_entries = stack_depot_fetch(object->trace_handle, &entries);
154221c3 356 warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
56a61617 357 object->pointer, object->size);
88f9ee2b
JC
358 warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu\n",
359 object->comm, object->pid, object->jiffies);
0494e082 360 hex_dump_object(seq, object);
52c5d2bc 361 warn_or_seq_printf(seq, " backtrace (crc %x):\n", object->checksum);
3c7b4e6b 362
56a61617
ZH
363 for (i = 0; i < nr_entries; i++) {
364 void *ptr = (void *)entries[i];
3a6f33d8 365 warn_or_seq_printf(seq, " [<%pK>] %pS\n", ptr, ptr);
3c7b4e6b
CM
366 }
367}
368
369/*
370 * Print the kmemleak_object information. This function is used mainly for
371 * debugging special cases when kmemleak operations. It must be called with
372 * the object->lock held.
373 */
374static void dump_object_info(struct kmemleak_object *object)
375{
ae281064 376 pr_notice("Object 0x%08lx (size %zu):\n",
56a61617 377 object->pointer, object->size);
3c7b4e6b 378 pr_notice(" comm \"%s\", pid %d, jiffies %lu\n",
56a61617 379 object->comm, object->pid, object->jiffies);
3c7b4e6b
CM
380 pr_notice(" min_count = %d\n", object->min_count);
381 pr_notice(" count = %d\n", object->count);
f66abf09 382 pr_notice(" flags = 0x%x\n", object->flags);
aae0ad7a 383 pr_notice(" checksum = %u\n", object->checksum);
3c7b4e6b 384 pr_notice(" backtrace:\n");
56a61617
ZH
385 if (object->trace_handle)
386 stack_depot_print(object->trace_handle);
3c7b4e6b
CM
387}
388
39042079
CM
389static struct rb_root *object_tree(unsigned long objflags)
390{
391 if (objflags & OBJECT_PHYS)
392 return &object_phys_tree_root;
393 if (objflags & OBJECT_PERCPU)
394 return &object_percpu_tree_root;
395 return &object_tree_root;
396}
397
3c7b4e6b 398/*
85d3a316 399 * Look-up a memory block metadata (kmemleak_object) in the object search
3c7b4e6b
CM
400 * tree based on a pointer value. If alias is 0, only values pointing to the
401 * beginning of the memory block are allowed. The kmemleak_lock must be held
402 * when calling this function.
403 */
0c24e061 404static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias,
39042079 405 unsigned int objflags)
3c7b4e6b 406{
39042079 407 struct rb_node *rb = object_tree(objflags)->rb_node;
ad1a3e15 408 unsigned long untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr);
85d3a316
ML
409
410 while (rb) {
ad1a3e15
KYL
411 struct kmemleak_object *object;
412 unsigned long untagged_objp;
413
414 object = rb_entry(rb, struct kmemleak_object, rb_node);
415 untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
416
417 if (untagged_ptr < untagged_objp)
85d3a316 418 rb = object->rb_node.rb_left;
ad1a3e15 419 else if (untagged_objp + object->size <= untagged_ptr)
85d3a316 420 rb = object->rb_node.rb_right;
ad1a3e15 421 else if (untagged_objp == untagged_ptr || alias)
85d3a316
ML
422 return object;
423 else {
5f79020c
CM
424 kmemleak_warn("Found object by alias at 0x%08lx\n",
425 ptr);
a7686a45 426 dump_object_info(object);
85d3a316 427 break;
3c7b4e6b 428 }
85d3a316
ML
429 }
430 return NULL;
3c7b4e6b
CM
431}
432
0c24e061
PW
433/* Look-up a kmemleak object which allocated with virtual address. */
434static struct kmemleak_object *lookup_object(unsigned long ptr, int alias)
435{
39042079 436 return __lookup_object(ptr, alias, 0);
0c24e061
PW
437}
438
3c7b4e6b
CM
439/*
440 * Increment the object use_count. Return 1 if successful or 0 otherwise. Note
441 * that once an object's use_count reached 0, the RCU freeing was already
442 * registered and the object should no longer be used. This function must be
443 * called under the protection of rcu_read_lock().
444 */
445static int get_object(struct kmemleak_object *object)
446{
447 return atomic_inc_not_zero(&object->use_count);
448}
449
0647398a
CM
450/*
451 * Memory pool allocation and freeing. kmemleak_lock must not be held.
452 */
453static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
454{
455 unsigned long flags;
456 struct kmemleak_object *object;
457
458 /* try the slab allocator first */
c5665868 459 if (object_cache) {
1c00f936
DC
460 object = kmem_cache_alloc_noprof(object_cache,
461 gfp_nested_mask(gfp));
c5665868
CM
462 if (object)
463 return object;
464 }
0647398a
CM
465
466 /* slab allocation failed, try the memory pool */
8c96f1bc 467 raw_spin_lock_irqsave(&kmemleak_lock, flags);
0647398a
CM
468 object = list_first_entry_or_null(&mem_pool_free_list,
469 typeof(*object), object_list);
470 if (object)
471 list_del(&object->object_list);
472 else if (mem_pool_free_count)
473 object = &mem_pool[--mem_pool_free_count];
c5665868
CM
474 else
475 pr_warn_once("Memory pool empty, consider increasing CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE\n");
8c96f1bc 476 raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
0647398a
CM
477
478 return object;
479}
480
481/*
482 * Return the object to either the slab allocator or the memory pool.
483 */
484static void mem_pool_free(struct kmemleak_object *object)
485{
486 unsigned long flags;
487
488 if (object < mem_pool || object >= mem_pool + ARRAY_SIZE(mem_pool)) {
489 kmem_cache_free(object_cache, object);
490 return;
491 }
492
493 /* add the object to the memory pool free list */
8c96f1bc 494 raw_spin_lock_irqsave(&kmemleak_lock, flags);
0647398a 495 list_add(&object->object_list, &mem_pool_free_list);
8c96f1bc 496 raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
0647398a
CM
497}
498
3c7b4e6b
CM
499/*
500 * RCU callback to free a kmemleak_object.
501 */
502static void free_object_rcu(struct rcu_head *rcu)
503{
b67bfe0d 504 struct hlist_node *tmp;
3c7b4e6b
CM
505 struct kmemleak_scan_area *area;
506 struct kmemleak_object *object =
507 container_of(rcu, struct kmemleak_object, rcu);
508
509 /*
510 * Once use_count is 0 (guaranteed by put_object), there is no other
511 * code accessing this object, hence no need for locking.
512 */
b67bfe0d
SL
513 hlist_for_each_entry_safe(area, tmp, &object->area_list, node) {
514 hlist_del(&area->node);
3c7b4e6b
CM
515 kmem_cache_free(scan_area_cache, area);
516 }
0647398a 517 mem_pool_free(object);
3c7b4e6b
CM
518}
519
520/*
521 * Decrement the object use_count. Once the count is 0, free the object using
522 * an RCU callback. Since put_object() may be called via the kmemleak_free() ->
523 * delete_object() path, the delayed RCU freeing ensures that there is no
524 * recursive call to the kernel allocator. Lock-less RCU object_list traversal
525 * is also possible.
526 */
527static void put_object(struct kmemleak_object *object)
528{
529 if (!atomic_dec_and_test(&object->use_count))
530 return;
531
532 /* should only get here after delete_object was called */
533 WARN_ON(object->flags & OBJECT_ALLOCATED);
534
c5665868
CM
535 /*
536 * It may be too early for the RCU callbacks, however, there is no
537 * concurrent object_list traversal when !object_cache and all objects
538 * came from the memory pool. Free the object directly.
539 */
540 if (object_cache)
541 call_rcu(&object->rcu, free_object_rcu);
542 else
543 free_object_rcu(&object->rcu);
3c7b4e6b
CM
544}
545
546/*
85d3a316 547 * Look up an object in the object search tree and increase its use_count.
3c7b4e6b 548 */
0c24e061 549static struct kmemleak_object *__find_and_get_object(unsigned long ptr, int alias,
39042079 550 unsigned int objflags)
3c7b4e6b
CM
551{
552 unsigned long flags;
9fbed254 553 struct kmemleak_object *object;
3c7b4e6b
CM
554
555 rcu_read_lock();
8c96f1bc 556 raw_spin_lock_irqsave(&kmemleak_lock, flags);
39042079 557 object = __lookup_object(ptr, alias, objflags);
8c96f1bc 558 raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
3c7b4e6b
CM
559
560 /* check whether the object is still available */
561 if (object && !get_object(object))
562 object = NULL;
563 rcu_read_unlock();
564
565 return object;
566}
567
0c24e061
PW
568/* Look up and get an object which allocated with virtual address. */
569static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias)
570{
39042079 571 return __find_and_get_object(ptr, alias, 0);
0c24e061
PW
572}
573
2abd839a 574/*
39042079
CM
575 * Remove an object from its object tree and object_list. Must be called with
576 * the kmemleak_lock held _if_ kmemleak is still enabled.
2abd839a
CM
577 */
578static void __remove_object(struct kmemleak_object *object)
579{
39042079 580 rb_erase(&object->rb_node, object_tree(object->flags));
782e4179
WL
581 if (!(object->del_state & DELSTATE_NO_DELETE))
582 list_del_rcu(&object->object_list);
583 object->del_state |= DELSTATE_REMOVED;
2abd839a
CM
584}
585
858a195b
LS
586static struct kmemleak_object *__find_and_remove_object(unsigned long ptr,
587 int alias,
39042079 588 unsigned int objflags)
858a195b
LS
589{
590 struct kmemleak_object *object;
591
39042079 592 object = __lookup_object(ptr, alias, objflags);
858a195b
LS
593 if (object)
594 __remove_object(object);
595
596 return object;
597}
598
e781a9ab 599/*
39042079
CM
600 * Look up an object in the object search tree and remove it from both object
601 * tree root and object_list. The returned object's use_count should be at
602 * least 1, as initially set by create_object().
e781a9ab 603 */
0c24e061 604static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int alias,
39042079 605 unsigned int objflags)
e781a9ab
CM
606{
607 unsigned long flags;
608 struct kmemleak_object *object;
609
8c96f1bc 610 raw_spin_lock_irqsave(&kmemleak_lock, flags);
39042079 611 object = __find_and_remove_object(ptr, alias, objflags);
8c96f1bc 612 raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
e781a9ab
CM
613
614 return object;
615}
616
56a61617 617static noinline depot_stack_handle_t set_track_prepare(void)
fd678967 618{
56a61617
ZH
619 depot_stack_handle_t trace_handle;
620 unsigned long entries[MAX_TRACE];
621 unsigned int nr_entries;
622
835bc157
XW
623 /*
624 * Use object_cache to determine whether kmemleak_init() has
625 * been invoked. stack_depot_early_init() is called before
626 * kmemleak_init() in mm_core_init().
627 */
628 if (!object_cache)
56a61617
ZH
629 return 0;
630 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3);
631 trace_handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT);
632
633 return trace_handle;
fd678967
CM
634}
635
0edd7b58 636static struct kmemleak_object *__alloc_object(gfp_t gfp)
3c7b4e6b 637{
0edd7b58 638 struct kmemleak_object *object;
3c7b4e6b 639
0647398a 640 object = mem_pool_alloc(gfp);
3c7b4e6b 641 if (!object) {
598d8091 642 pr_warn("Cannot allocate a kmemleak_object structure\n");
6ae4bd1f 643 kmemleak_disable();
4eff7d62 644 return NULL;
3c7b4e6b
CM
645 }
646
647 INIT_LIST_HEAD(&object->object_list);
648 INIT_LIST_HEAD(&object->gray_list);
649 INIT_HLIST_HEAD(&object->area_list);
8c96f1bc 650 raw_spin_lock_init(&object->lock);
3c7b4e6b 651 atomic_set(&object->use_count, 1);
94f4a161 652 object->excess_ref = 0;
04609ccc 653 object->count = 0; /* white color initially */
04609ccc 654 object->checksum = 0;
782e4179 655 object->del_state = 0;
3c7b4e6b
CM
656
657 /* task information */
ea0eafea 658 if (in_hardirq()) {
3c7b4e6b
CM
659 object->pid = 0;
660 strncpy(object->comm, "hardirq", sizeof(object->comm));
6ef90569 661 } else if (in_serving_softirq()) {
3c7b4e6b
CM
662 object->pid = 0;
663 strncpy(object->comm, "softirq", sizeof(object->comm));
664 } else {
665 object->pid = current->pid;
666 /*
667 * There is a small chance of a race with set_task_comm(),
668 * however using get_task_comm() here may cause locking
669 * dependency issues with current->alloc_lock. In the worst
670 * case, the command line is not correct.
671 */
672 strncpy(object->comm, current->comm, sizeof(object->comm));
673 }
674
675 /* kernel backtrace */
56a61617 676 object->trace_handle = set_track_prepare();
3c7b4e6b 677
4eff7d62
LS
678 return object;
679}
680
681static int __link_object(struct kmemleak_object *object, unsigned long ptr,
39042079 682 size_t size, int min_count, unsigned int objflags)
4eff7d62
LS
683{
684
685 struct kmemleak_object *parent;
686 struct rb_node **link, *rb_parent;
687 unsigned long untagged_ptr;
688 unsigned long untagged_objp;
689
39042079 690 object->flags = OBJECT_ALLOCATED | objflags;
4eff7d62
LS
691 object->pointer = ptr;
692 object->size = kfence_ksize((void *)ptr) ?: size;
693 object->min_count = min_count;
694 object->jiffies = jiffies;
695
a2f77575 696 untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr);
0c24e061
PW
697 /*
698 * Only update min_addr and max_addr with object
699 * storing virtual address.
700 */
39042079 701 if (!(objflags & (OBJECT_PHYS | OBJECT_PERCPU))) {
0c24e061
PW
702 min_addr = min(min_addr, untagged_ptr);
703 max_addr = max(max_addr, untagged_ptr + size);
704 }
39042079 705 link = &object_tree(objflags)->rb_node;
85d3a316
ML
706 rb_parent = NULL;
707 while (*link) {
708 rb_parent = *link;
709 parent = rb_entry(rb_parent, struct kmemleak_object, rb_node);
ad1a3e15
KYL
710 untagged_objp = (unsigned long)kasan_reset_tag((void *)parent->pointer);
711 if (untagged_ptr + size <= untagged_objp)
85d3a316 712 link = &parent->rb_node.rb_left;
ad1a3e15 713 else if (untagged_objp + parent->size <= untagged_ptr)
85d3a316
ML
714 link = &parent->rb_node.rb_right;
715 else {
756a025f 716 kmemleak_stop("Cannot insert 0x%lx into the object search tree (overlaps existing)\n",
85d3a316 717 ptr);
9d5a4c73
CM
718 /*
719 * No need for parent->lock here since "parent" cannot
720 * be freed while the kmemleak_lock is held.
721 */
722 dump_object_info(parent);
2e1d4738 723 return -EEXIST;
85d3a316 724 }
3c7b4e6b 725 }
85d3a316 726 rb_link_node(&object->rb_node, rb_parent, link);
39042079 727 rb_insert_color(&object->rb_node, object_tree(objflags));
3c7b4e6b 728 list_add_tail_rcu(&object->object_list, &object_list);
2e1d4738
LS
729
730 return 0;
0edd7b58
LS
731}
732
733/*
734 * Create the metadata (struct kmemleak_object) corresponding to an allocated
39042079 735 * memory block and add it to the object_list and object tree.
0edd7b58
LS
736 */
737static void __create_object(unsigned long ptr, size_t size,
39042079 738 int min_count, gfp_t gfp, unsigned int objflags)
0edd7b58
LS
739{
740 struct kmemleak_object *object;
741 unsigned long flags;
2e1d4738 742 int ret;
0edd7b58
LS
743
744 object = __alloc_object(gfp);
745 if (!object)
746 return;
747
748 raw_spin_lock_irqsave(&kmemleak_lock, flags);
39042079 749 ret = __link_object(object, ptr, size, min_count, objflags);
8c96f1bc 750 raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
2e1d4738
LS
751 if (ret)
752 mem_pool_free(object);
3c7b4e6b
CM
753}
754
8e0c4ab3 755/* Create kmemleak object which allocated with virtual address. */
b955aa70
LS
756static void create_object(unsigned long ptr, size_t size,
757 int min_count, gfp_t gfp)
8e0c4ab3 758{
39042079 759 __create_object(ptr, size, min_count, gfp, 0);
8e0c4ab3
PW
760}
761
762/* Create kmemleak object which allocated with physical address. */
b955aa70
LS
763static void create_object_phys(unsigned long ptr, size_t size,
764 int min_count, gfp_t gfp)
8e0c4ab3 765{
39042079
CM
766 __create_object(ptr, size, min_count, gfp, OBJECT_PHYS);
767}
768
769/* Create kmemleak object corresponding to a per-CPU allocation. */
770static void create_object_percpu(unsigned long ptr, size_t size,
771 int min_count, gfp_t gfp)
772{
773 __create_object(ptr, size, min_count, gfp, OBJECT_PERCPU);
8e0c4ab3
PW
774}
775
3c7b4e6b 776/*
e781a9ab 777 * Mark the object as not allocated and schedule RCU freeing via put_object().
3c7b4e6b 778 */
53238a60 779static void __delete_object(struct kmemleak_object *object)
3c7b4e6b
CM
780{
781 unsigned long flags;
3c7b4e6b 782
3c7b4e6b 783 WARN_ON(!(object->flags & OBJECT_ALLOCATED));
e781a9ab 784 WARN_ON(atomic_read(&object->use_count) < 1);
3c7b4e6b
CM
785
786 /*
787 * Locking here also ensures that the corresponding memory block
788 * cannot be freed when it is being scanned.
789 */
8c96f1bc 790 raw_spin_lock_irqsave(&object->lock, flags);
3c7b4e6b 791 object->flags &= ~OBJECT_ALLOCATED;
8c96f1bc 792 raw_spin_unlock_irqrestore(&object->lock, flags);
3c7b4e6b
CM
793 put_object(object);
794}
795
53238a60
CM
796/*
797 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
798 * delete it.
799 */
39042079 800static void delete_object_full(unsigned long ptr, unsigned int objflags)
53238a60
CM
801{
802 struct kmemleak_object *object;
803
39042079 804 object = find_and_remove_object(ptr, 0, objflags);
53238a60
CM
805 if (!object) {
806#ifdef DEBUG
807 kmemleak_warn("Freeing unknown object at 0x%08lx\n",
808 ptr);
809#endif
810 return;
811 }
812 __delete_object(object);
53238a60
CM
813}
814
815/*
816 * Look up the metadata (struct kmemleak_object) corresponding to ptr and
817 * delete it. If the memory block is partially freed, the function may create
818 * additional metadata for the remaining parts of the block.
819 */
39042079
CM
820static void delete_object_part(unsigned long ptr, size_t size,
821 unsigned int objflags)
53238a60 822{
5e4fc577
LS
823 struct kmemleak_object *object, *object_l, *object_r;
824 unsigned long start, end, flags;
825
826 object_l = __alloc_object(GFP_KERNEL);
827 if (!object_l)
828 return;
53238a60 829
5e4fc577
LS
830 object_r = __alloc_object(GFP_KERNEL);
831 if (!object_r)
832 goto out;
833
834 raw_spin_lock_irqsave(&kmemleak_lock, flags);
39042079 835 object = __find_and_remove_object(ptr, 1, objflags);
53238a60
CM
836 if (!object) {
837#ifdef DEBUG
756a025f
JP
838 kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
839 ptr, size);
53238a60 840#endif
5e4fc577 841 goto unlock;
53238a60 842 }
53238a60
CM
843
844 /*
845 * Create one or two objects that may result from the memory block
846 * split. Note that partial freeing is only done by free_bootmem() and
c5665868 847 * this happens before kmemleak_init() is called.
53238a60
CM
848 */
849 start = object->pointer;
850 end = object->pointer + object->size;
5e4fc577
LS
851 if ((ptr > start) &&
852 !__link_object(object_l, start, ptr - start,
39042079 853 object->min_count, objflags))
5e4fc577
LS
854 object_l = NULL;
855 if ((ptr + size < end) &&
856 !__link_object(object_r, ptr + size, end - ptr - size,
39042079 857 object->min_count, objflags))
5e4fc577
LS
858 object_r = NULL;
859
860unlock:
861 raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
862 if (object)
863 __delete_object(object);
53238a60 864
5e4fc577
LS
865out:
866 if (object_l)
867 mem_pool_free(object_l);
868 if (object_r)
869 mem_pool_free(object_r);
53238a60 870}
a1084c87
LR
871
872static void __paint_it(struct kmemleak_object *object, int color)
873{
874 object->min_count = color;
875 if (color == KMEMLEAK_BLACK)
876 object->flags |= OBJECT_NO_SCAN;
877}
878
879static void paint_it(struct kmemleak_object *object, int color)
3c7b4e6b
CM
880{
881 unsigned long flags;
a1084c87 882
8c96f1bc 883 raw_spin_lock_irqsave(&object->lock, flags);
a1084c87 884 __paint_it(object, color);
8c96f1bc 885 raw_spin_unlock_irqrestore(&object->lock, flags);
a1084c87
LR
886}
887
39042079 888static void paint_ptr(unsigned long ptr, int color, unsigned int objflags)
a1084c87 889{
3c7b4e6b
CM
890 struct kmemleak_object *object;
891
39042079 892 object = __find_and_get_object(ptr, 0, objflags);
3c7b4e6b 893 if (!object) {
756a025f
JP
894 kmemleak_warn("Trying to color unknown object at 0x%08lx as %s\n",
895 ptr,
a1084c87
LR
896 (color == KMEMLEAK_GREY) ? "Grey" :
897 (color == KMEMLEAK_BLACK) ? "Black" : "Unknown");
3c7b4e6b
CM
898 return;
899 }
a1084c87 900 paint_it(object, color);
3c7b4e6b
CM
901 put_object(object);
902}
903
a1084c87 904/*
145b64b9 905 * Mark an object permanently as gray-colored so that it can no longer be
a1084c87
LR
906 * reported as a leak. This is used in general to mark a false positive.
907 */
908static void make_gray_object(unsigned long ptr)
909{
39042079 910 paint_ptr(ptr, KMEMLEAK_GREY, 0);
a1084c87
LR
911}
912
3c7b4e6b
CM
913/*
914 * Mark the object as black-colored so that it is ignored from scans and
915 * reporting.
916 */
39042079 917static void make_black_object(unsigned long ptr, unsigned int objflags)
3c7b4e6b 918{
39042079 919 paint_ptr(ptr, KMEMLEAK_BLACK, objflags);
3c7b4e6b
CM
920}
921
922/*
923 * Add a scanning area to the object. If at least one such area is added,
924 * kmemleak will only scan these ranges rather than the whole memory block.
925 */
c017b4be 926static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
3c7b4e6b
CM
927{
928 unsigned long flags;
929 struct kmemleak_object *object;
c5665868 930 struct kmemleak_scan_area *area = NULL;
bfc8089f
KYL
931 unsigned long untagged_ptr;
932 unsigned long untagged_objp;
3c7b4e6b 933
c017b4be 934 object = find_and_get_object(ptr, 1);
3c7b4e6b 935 if (!object) {
ae281064
JP
936 kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n",
937 ptr);
3c7b4e6b
CM
938 return;
939 }
940
bfc8089f
KYL
941 untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr);
942 untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
943
c5665868 944 if (scan_area_cache)
1c00f936
DC
945 area = kmem_cache_alloc_noprof(scan_area_cache,
946 gfp_nested_mask(gfp));
3c7b4e6b 947
8c96f1bc 948 raw_spin_lock_irqsave(&object->lock, flags);
dba82d94
CM
949 if (!area) {
950 pr_warn_once("Cannot allocate a scan area, scanning the full object\n");
951 /* mark the object for full scan to avoid false positives */
952 object->flags |= OBJECT_FULL_SCAN;
953 goto out_unlock;
954 }
7f88f88f 955 if (size == SIZE_MAX) {
bfc8089f
KYL
956 size = untagged_objp + object->size - untagged_ptr;
957 } else if (untagged_ptr + size > untagged_objp + object->size) {
ae281064 958 kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr);
3c7b4e6b
CM
959 dump_object_info(object);
960 kmem_cache_free(scan_area_cache, area);
961 goto out_unlock;
962 }
963
964 INIT_HLIST_NODE(&area->node);
c017b4be
CM
965 area->start = ptr;
966 area->size = size;
3c7b4e6b
CM
967
968 hlist_add_head(&area->node, &object->area_list);
969out_unlock:
8c96f1bc 970 raw_spin_unlock_irqrestore(&object->lock, flags);
3c7b4e6b
CM
971 put_object(object);
972}
973
94f4a161
CM
974/*
975 * Any surplus references (object already gray) to 'ptr' are passed to
976 * 'excess_ref'. This is used in the vmalloc() case where a pointer to
977 * vm_struct may be used as an alternative reference to the vmalloc'ed object
978 * (see free_thread_stack()).
979 */
980static void object_set_excess_ref(unsigned long ptr, unsigned long excess_ref)
981{
982 unsigned long flags;
983 struct kmemleak_object *object;
984
985 object = find_and_get_object(ptr, 0);
986 if (!object) {
987 kmemleak_warn("Setting excess_ref on unknown object at 0x%08lx\n",
988 ptr);
989 return;
990 }
991
8c96f1bc 992 raw_spin_lock_irqsave(&object->lock, flags);
94f4a161 993 object->excess_ref = excess_ref;
8c96f1bc 994 raw_spin_unlock_irqrestore(&object->lock, flags);
94f4a161
CM
995 put_object(object);
996}
997
3c7b4e6b
CM
998/*
999 * Set the OBJECT_NO_SCAN flag for the object corresponding to the give
1000 * pointer. Such object will not be scanned by kmemleak but references to it
1001 * are searched.
1002 */
1003static void object_no_scan(unsigned long ptr)
1004{
1005 unsigned long flags;
1006 struct kmemleak_object *object;
1007
1008 object = find_and_get_object(ptr, 0);
1009 if (!object) {
ae281064 1010 kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr);
3c7b4e6b
CM
1011 return;
1012 }
1013
8c96f1bc 1014 raw_spin_lock_irqsave(&object->lock, flags);
3c7b4e6b 1015 object->flags |= OBJECT_NO_SCAN;
8c96f1bc 1016 raw_spin_unlock_irqrestore(&object->lock, flags);
3c7b4e6b
CM
1017 put_object(object);
1018}
1019
a2b6bf63
CM
1020/**
1021 * kmemleak_alloc - register a newly allocated object
1022 * @ptr: pointer to beginning of the object
1023 * @size: size of the object
1024 * @min_count: minimum number of references to this object. If during memory
1025 * scanning a number of references less than @min_count is found,
1026 * the object is reported as a memory leak. If @min_count is 0,
1027 * the object is never reported as a leak. If @min_count is -1,
1028 * the object is ignored (not scanned and not reported as a leak)
1029 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
1030 *
1031 * This function is called from the kernel allocators when a new object
94f4a161 1032 * (memory block) is allocated (kmem_cache_alloc, kmalloc etc.).
3c7b4e6b 1033 */
a6186d89
CM
1034void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count,
1035 gfp_t gfp)
3c7b4e6b 1036{
62047e0f 1037 pr_debug("%s(0x%px, %zu, %d)\n", __func__, ptr, size, min_count);
3c7b4e6b 1038
8910ae89 1039 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
3c7b4e6b 1040 create_object((unsigned long)ptr, size, min_count, gfp);
3c7b4e6b
CM
1041}
1042EXPORT_SYMBOL_GPL(kmemleak_alloc);
1043
f528f0b8
CM
1044/**
1045 * kmemleak_alloc_percpu - register a newly allocated __percpu object
1046 * @ptr: __percpu pointer to beginning of the object
1047 * @size: size of the object
8a8c35fa 1048 * @gfp: flags used for kmemleak internal memory allocations
f528f0b8
CM
1049 *
1050 * This function is called from the kernel percpu allocator when a new object
8a8c35fa 1051 * (memory block) is allocated (alloc_percpu).
f528f0b8 1052 */
8a8c35fa
LF
1053void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size,
1054 gfp_t gfp)
f528f0b8 1055{
62047e0f 1056 pr_debug("%s(0x%px, %zu)\n", __func__, ptr, size);
f528f0b8
CM
1057
1058 /*
1059 * Percpu allocations are only scanned and not reported as leaks
1060 * (min_count is set to 0).
1061 */
8910ae89 1062 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
39042079 1063 create_object_percpu((unsigned long)ptr, size, 0, gfp);
f528f0b8
CM
1064}
1065EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
1066
94f4a161
CM
1067/**
1068 * kmemleak_vmalloc - register a newly vmalloc'ed object
1069 * @area: pointer to vm_struct
1070 * @size: size of the object
1071 * @gfp: __vmalloc() flags used for kmemleak internal memory allocations
1072 *
1073 * This function is called from the vmalloc() kernel allocator when a new
1074 * object (memory block) is allocated.
1075 */
1076void __ref kmemleak_vmalloc(const struct vm_struct *area, size_t size, gfp_t gfp)
1077{
62047e0f 1078 pr_debug("%s(0x%px, %zu)\n", __func__, area, size);
94f4a161
CM
1079
1080 /*
1081 * A min_count = 2 is needed because vm_struct contains a reference to
1082 * the virtual address of the vmalloc'ed block.
1083 */
1084 if (kmemleak_enabled) {
1085 create_object((unsigned long)area->addr, size, 2, gfp);
1086 object_set_excess_ref((unsigned long)area,
1087 (unsigned long)area->addr);
94f4a161
CM
1088 }
1089}
1090EXPORT_SYMBOL_GPL(kmemleak_vmalloc);
1091
a2b6bf63
CM
1092/**
1093 * kmemleak_free - unregister a previously registered object
1094 * @ptr: pointer to beginning of the object
1095 *
1096 * This function is called from the kernel allocators when an object (memory
1097 * block) is freed (kmem_cache_free, kfree, vfree etc.).
3c7b4e6b 1098 */
a6186d89 1099void __ref kmemleak_free(const void *ptr)
3c7b4e6b 1100{
62047e0f 1101 pr_debug("%s(0x%px)\n", __func__, ptr);
3c7b4e6b 1102
c5f3b1a5 1103 if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
39042079 1104 delete_object_full((unsigned long)ptr, 0);
3c7b4e6b
CM
1105}
1106EXPORT_SYMBOL_GPL(kmemleak_free);
1107
a2b6bf63
CM
1108/**
1109 * kmemleak_free_part - partially unregister a previously registered object
1110 * @ptr: pointer to the beginning or inside the object. This also
1111 * represents the start of the range to be freed
1112 * @size: size to be unregistered
1113 *
1114 * This function is called when only a part of a memory block is freed
1115 * (usually from the bootmem allocator).
53238a60 1116 */
a6186d89 1117void __ref kmemleak_free_part(const void *ptr, size_t size)
53238a60 1118{
62047e0f 1119 pr_debug("%s(0x%px)\n", __func__, ptr);
53238a60 1120
8910ae89 1121 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
39042079 1122 delete_object_part((unsigned long)ptr, size, 0);
53238a60
CM
1123}
1124EXPORT_SYMBOL_GPL(kmemleak_free_part);
1125
f528f0b8
CM
1126/**
1127 * kmemleak_free_percpu - unregister a previously registered __percpu object
1128 * @ptr: __percpu pointer to beginning of the object
1129 *
1130 * This function is called from the kernel percpu allocator when an object
1131 * (memory block) is freed (free_percpu).
1132 */
1133void __ref kmemleak_free_percpu(const void __percpu *ptr)
1134{
62047e0f 1135 pr_debug("%s(0x%px)\n", __func__, ptr);
f528f0b8 1136
c5f3b1a5 1137 if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
39042079 1138 delete_object_full((unsigned long)ptr, OBJECT_PERCPU);
f528f0b8
CM
1139}
1140EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
1141
ffe2c748
CM
1142/**
1143 * kmemleak_update_trace - update object allocation stack trace
1144 * @ptr: pointer to beginning of the object
1145 *
1146 * Override the object allocation stack trace for cases where the actual
1147 * allocation place is not always useful.
1148 */
1149void __ref kmemleak_update_trace(const void *ptr)
1150{
1151 struct kmemleak_object *object;
d63385a7 1152 depot_stack_handle_t trace_handle;
ffe2c748
CM
1153 unsigned long flags;
1154
62047e0f 1155 pr_debug("%s(0x%px)\n", __func__, ptr);
ffe2c748
CM
1156
1157 if (!kmemleak_enabled || IS_ERR_OR_NULL(ptr))
1158 return;
1159
1160 object = find_and_get_object((unsigned long)ptr, 1);
1161 if (!object) {
1162#ifdef DEBUG
1163 kmemleak_warn("Updating stack trace for unknown object at %p\n",
1164 ptr);
1165#endif
1166 return;
1167 }
1168
d63385a7 1169 trace_handle = set_track_prepare();
8c96f1bc 1170 raw_spin_lock_irqsave(&object->lock, flags);
d63385a7 1171 object->trace_handle = trace_handle;
8c96f1bc 1172 raw_spin_unlock_irqrestore(&object->lock, flags);
ffe2c748
CM
1173
1174 put_object(object);
1175}
1176EXPORT_SYMBOL(kmemleak_update_trace);
1177
a2b6bf63
CM
1178/**
1179 * kmemleak_not_leak - mark an allocated object as false positive
1180 * @ptr: pointer to beginning of the object
1181 *
1182 * Calling this function on an object will cause the memory block to no longer
1183 * be reported as leak and always be scanned.
3c7b4e6b 1184 */
a6186d89 1185void __ref kmemleak_not_leak(const void *ptr)
3c7b4e6b 1186{
62047e0f 1187 pr_debug("%s(0x%px)\n", __func__, ptr);
3c7b4e6b 1188
8910ae89 1189 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
3c7b4e6b 1190 make_gray_object((unsigned long)ptr);
3c7b4e6b
CM
1191}
1192EXPORT_SYMBOL(kmemleak_not_leak);
1193
a2b6bf63
CM
1194/**
1195 * kmemleak_ignore - ignore an allocated object
1196 * @ptr: pointer to beginning of the object
1197 *
1198 * Calling this function on an object will cause the memory block to be
1199 * ignored (not scanned and not reported as a leak). This is usually done when
1200 * it is known that the corresponding block is not a leak and does not contain
1201 * any references to other allocated memory blocks.
3c7b4e6b 1202 */
a6186d89 1203void __ref kmemleak_ignore(const void *ptr)
3c7b4e6b 1204{
62047e0f 1205 pr_debug("%s(0x%px)\n", __func__, ptr);
3c7b4e6b 1206
8910ae89 1207 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
39042079 1208 make_black_object((unsigned long)ptr, 0);
3c7b4e6b
CM
1209}
1210EXPORT_SYMBOL(kmemleak_ignore);
1211
a2b6bf63
CM
1212/**
1213 * kmemleak_scan_area - limit the range to be scanned in an allocated object
1214 * @ptr: pointer to beginning or inside the object. This also
1215 * represents the start of the scan area
1216 * @size: size of the scan area
1217 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
1218 *
1219 * This function is used when it is known that only certain parts of an object
1220 * contain references to other objects. Kmemleak will only scan these areas
1221 * reducing the number false negatives.
3c7b4e6b 1222 */
c017b4be 1223void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp)
3c7b4e6b 1224{
62047e0f 1225 pr_debug("%s(0x%px)\n", __func__, ptr);
3c7b4e6b 1226
8910ae89 1227 if (kmemleak_enabled && ptr && size && !IS_ERR(ptr))
c017b4be 1228 add_scan_area((unsigned long)ptr, size, gfp);
3c7b4e6b
CM
1229}
1230EXPORT_SYMBOL(kmemleak_scan_area);
1231
a2b6bf63
CM
1232/**
1233 * kmemleak_no_scan - do not scan an allocated object
1234 * @ptr: pointer to beginning of the object
1235 *
1236 * This function notifies kmemleak not to scan the given memory block. Useful
1237 * in situations where it is known that the given object does not contain any
1238 * references to other objects. Kmemleak will not scan such objects reducing
1239 * the number of false negatives.
3c7b4e6b 1240 */
a6186d89 1241void __ref kmemleak_no_scan(const void *ptr)
3c7b4e6b 1242{
62047e0f 1243 pr_debug("%s(0x%px)\n", __func__, ptr);
3c7b4e6b 1244
8910ae89 1245 if (kmemleak_enabled && ptr && !IS_ERR(ptr))
3c7b4e6b 1246 object_no_scan((unsigned long)ptr);
3c7b4e6b
CM
1247}
1248EXPORT_SYMBOL(kmemleak_no_scan);
1249
9099daed
CM
1250/**
1251 * kmemleak_alloc_phys - similar to kmemleak_alloc but taking a physical
1252 * address argument
e8b098fc
MR
1253 * @phys: physical address of the object
1254 * @size: size of the object
e8b098fc 1255 * @gfp: kmalloc() flags used for kmemleak internal memory allocations
9099daed 1256 */
c200d900 1257void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp)
9099daed 1258{
62047e0f 1259 pr_debug("%s(0x%px, %zu)\n", __func__, &phys, size);
8e0c4ab3 1260
84c32629 1261 if (kmemleak_enabled)
8e0c4ab3
PW
1262 /*
1263 * Create object with OBJECT_PHYS flag and
1264 * assume min_count 0.
1265 */
0c24e061 1266 create_object_phys((unsigned long)phys, size, 0, gfp);
9099daed
CM
1267}
1268EXPORT_SYMBOL(kmemleak_alloc_phys);
1269
1270/**
1271 * kmemleak_free_part_phys - similar to kmemleak_free_part but taking a
1272 * physical address argument
e8b098fc
MR
1273 * @phys: physical address if the beginning or inside an object. This
1274 * also represents the start of the range to be freed
1275 * @size: size to be unregistered
9099daed
CM
1276 */
1277void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size)
1278{
62047e0f 1279 pr_debug("%s(0x%px)\n", __func__, &phys);
0c24e061 1280
84c32629 1281 if (kmemleak_enabled)
39042079 1282 delete_object_part((unsigned long)phys, size, OBJECT_PHYS);
9099daed
CM
1283}
1284EXPORT_SYMBOL(kmemleak_free_part_phys);
1285
9099daed
CM
1286/**
1287 * kmemleak_ignore_phys - similar to kmemleak_ignore but taking a physical
1288 * address argument
e8b098fc 1289 * @phys: physical address of the object
9099daed
CM
1290 */
1291void __ref kmemleak_ignore_phys(phys_addr_t phys)
1292{
62047e0f 1293 pr_debug("%s(0x%px)\n", __func__, &phys);
0c24e061 1294
84c32629 1295 if (kmemleak_enabled)
39042079 1296 make_black_object((unsigned long)phys, OBJECT_PHYS);
9099daed
CM
1297}
1298EXPORT_SYMBOL(kmemleak_ignore_phys);
1299
04609ccc
CM
1300/*
1301 * Update an object's checksum and return true if it was modified.
1302 */
1303static bool update_checksum(struct kmemleak_object *object)
1304{
1305 u32 old_csum = object->checksum;
1306
39042079 1307 if (WARN_ON_ONCE(object->flags & (OBJECT_PHYS | OBJECT_PERCPU)))
0c24e061
PW
1308 return false;
1309
e79ed2f1 1310 kasan_disable_current();
69d0b54d 1311 kcsan_disable_current();
6c7a00b8 1312 object->checksum = crc32(0, kasan_reset_tag((void *)object->pointer), object->size);
e79ed2f1 1313 kasan_enable_current();
69d0b54d 1314 kcsan_enable_current();
e79ed2f1 1315
04609ccc
CM
1316 return object->checksum != old_csum;
1317}
1318
04f70d13
CM
1319/*
1320 * Update an object's references. object->lock must be held by the caller.
1321 */
1322static void update_refs(struct kmemleak_object *object)
1323{
1324 if (!color_white(object)) {
1325 /* non-orphan, ignored or new */
1326 return;
1327 }
1328
1329 /*
1330 * Increase the object's reference count (number of pointers to the
1331 * memory block). If this count reaches the required minimum, the
1332 * object's color will become gray and it will be added to the
1333 * gray_list.
1334 */
1335 object->count++;
1336 if (color_gray(object)) {
1337 /* put_object() called when removing from gray_list */
1338 WARN_ON(!get_object(object));
1339 list_add_tail(&object->gray_list, &gray_list);
1340 }
1341}
1342
3c7b4e6b 1343/*
0b5121ef 1344 * Memory scanning is a long process and it needs to be interruptible. This
25985edc 1345 * function checks whether such interrupt condition occurred.
3c7b4e6b
CM
1346 */
1347static int scan_should_stop(void)
1348{
8910ae89 1349 if (!kmemleak_enabled)
3c7b4e6b
CM
1350 return 1;
1351
1352 /*
1353 * This function may be called from either process or kthread context,
1354 * hence the need to check for both stop conditions.
1355 */
1356 if (current->mm)
1357 return signal_pending(current);
1358 else
1359 return kthread_should_stop();
1360
1361 return 0;
1362}
1363
1364/*
1365 * Scan a memory block (exclusive range) for valid pointers and add those
1366 * found to the gray list.
1367 */
1368static void scan_block(void *_start, void *_end,
93ada579 1369 struct kmemleak_object *scanned)
3c7b4e6b
CM
1370{
1371 unsigned long *ptr;
1372 unsigned long *start = PTR_ALIGN(_start, BYTES_PER_POINTER);
1373 unsigned long *end = _end - (BYTES_PER_POINTER - 1);
93ada579 1374 unsigned long flags;
a2f77575 1375 unsigned long untagged_ptr;
3c7b4e6b 1376
8c96f1bc 1377 raw_spin_lock_irqsave(&kmemleak_lock, flags);
3c7b4e6b 1378 for (ptr = start; ptr < end; ptr++) {
3c7b4e6b 1379 struct kmemleak_object *object;
8e019366 1380 unsigned long pointer;
94f4a161 1381 unsigned long excess_ref;
3c7b4e6b
CM
1382
1383 if (scan_should_stop())
1384 break;
1385
e79ed2f1 1386 kasan_disable_current();
6c7a00b8 1387 pointer = *(unsigned long *)kasan_reset_tag((void *)ptr);
e79ed2f1 1388 kasan_enable_current();
8e019366 1389
a2f77575
AK
1390 untagged_ptr = (unsigned long)kasan_reset_tag((void *)pointer);
1391 if (untagged_ptr < min_addr || untagged_ptr >= max_addr)
93ada579
CM
1392 continue;
1393
1394 /*
1395 * No need for get_object() here since we hold kmemleak_lock.
1396 * object->use_count cannot be dropped to 0 while the object
1397 * is still present in object_tree_root and object_list
1398 * (with updates protected by kmemleak_lock).
1399 */
1400 object = lookup_object(pointer, 1);
3c7b4e6b
CM
1401 if (!object)
1402 continue;
93ada579 1403 if (object == scanned)
3c7b4e6b 1404 /* self referenced, ignore */
3c7b4e6b 1405 continue;
3c7b4e6b
CM
1406
1407 /*
1408 * Avoid the lockdep recursive warning on object->lock being
1409 * previously acquired in scan_object(). These locks are
1410 * enclosed by scan_mutex.
1411 */
8c96f1bc 1412 raw_spin_lock_nested(&object->lock, SINGLE_DEPTH_NESTING);
94f4a161
CM
1413 /* only pass surplus references (object already gray) */
1414 if (color_gray(object)) {
1415 excess_ref = object->excess_ref;
1416 /* no need for update_refs() if object already gray */
1417 } else {
1418 excess_ref = 0;
1419 update_refs(object);
1420 }
8c96f1bc 1421 raw_spin_unlock(&object->lock);
94f4a161
CM
1422
1423 if (excess_ref) {
1424 object = lookup_object(excess_ref, 0);
1425 if (!object)
1426 continue;
1427 if (object == scanned)
1428 /* circular reference, ignore */
1429 continue;
8c96f1bc 1430 raw_spin_lock_nested(&object->lock, SINGLE_DEPTH_NESTING);
94f4a161 1431 update_refs(object);
8c96f1bc 1432 raw_spin_unlock(&object->lock);
94f4a161 1433 }
93ada579 1434 }
8c96f1bc 1435 raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
93ada579 1436}
0587da40 1437
93ada579
CM
1438/*
1439 * Scan a large memory block in MAX_SCAN_SIZE chunks to reduce the latency.
1440 */
dce5b0bd 1441#ifdef CONFIG_SMP
93ada579
CM
1442static void scan_large_block(void *start, void *end)
1443{
1444 void *next;
1445
1446 while (start < end) {
1447 next = min(start + MAX_SCAN_SIZE, end);
1448 scan_block(start, next, NULL);
1449 start = next;
1450 cond_resched();
3c7b4e6b
CM
1451 }
1452}
dce5b0bd 1453#endif
3c7b4e6b
CM
1454
1455/*
1456 * Scan a memory block corresponding to a kmemleak_object. A condition is
1457 * that object->use_count >= 1.
1458 */
1459static void scan_object(struct kmemleak_object *object)
1460{
1461 struct kmemleak_scan_area *area;
3c7b4e6b
CM
1462 unsigned long flags;
1463
1464 /*
21ae2956
UKK
1465 * Once the object->lock is acquired, the corresponding memory block
1466 * cannot be freed (the same lock is acquired in delete_object).
3c7b4e6b 1467 */
8c96f1bc 1468 raw_spin_lock_irqsave(&object->lock, flags);
3c7b4e6b
CM
1469 if (object->flags & OBJECT_NO_SCAN)
1470 goto out;
1471 if (!(object->flags & OBJECT_ALLOCATED))
1472 /* already freed object */
1473 goto out;
0c24e061 1474
39042079
CM
1475 if (object->flags & OBJECT_PERCPU) {
1476 unsigned int cpu;
1477
1478 for_each_possible_cpu(cpu) {
1479 void *start = per_cpu_ptr((void __percpu *)object->pointer, cpu);
1480 void *end = start + object->size;
0c24e061 1481
39042079
CM
1482 scan_block(start, end, object);
1483
1484 raw_spin_unlock_irqrestore(&object->lock, flags);
1485 cond_resched();
1486 raw_spin_lock_irqsave(&object->lock, flags);
1487 if (!(object->flags & OBJECT_ALLOCATED))
1488 break;
1489 }
1490 } else if (hlist_empty(&object->area_list) ||
dba82d94 1491 object->flags & OBJECT_FULL_SCAN) {
39042079
CM
1492 void *start = object->flags & OBJECT_PHYS ?
1493 __va((phys_addr_t)object->pointer) :
1494 (void *)object->pointer;
1495 void *end = start + object->size;
93ada579
CM
1496 void *next;
1497
1498 do {
1499 next = min(start + MAX_SCAN_SIZE, end);
1500 scan_block(start, next, object);
af98603d 1501
93ada579
CM
1502 start = next;
1503 if (start >= end)
1504 break;
af98603d 1505
8c96f1bc 1506 raw_spin_unlock_irqrestore(&object->lock, flags);
af98603d 1507 cond_resched();
8c96f1bc 1508 raw_spin_lock_irqsave(&object->lock, flags);
93ada579 1509 } while (object->flags & OBJECT_ALLOCATED);
39042079 1510 } else {
b67bfe0d 1511 hlist_for_each_entry(area, &object->area_list, node)
c017b4be
CM
1512 scan_block((void *)area->start,
1513 (void *)(area->start + area->size),
93ada579 1514 object);
39042079 1515 }
3c7b4e6b 1516out:
8c96f1bc 1517 raw_spin_unlock_irqrestore(&object->lock, flags);
3c7b4e6b
CM
1518}
1519
04609ccc
CM
1520/*
1521 * Scan the objects already referenced (gray objects). More objects will be
1522 * referenced and, if there are no memory leaks, all the objects are scanned.
1523 */
1524static void scan_gray_list(void)
1525{
1526 struct kmemleak_object *object, *tmp;
1527
1528 /*
1529 * The list traversal is safe for both tail additions and removals
1530 * from inside the loop. The kmemleak objects cannot be freed from
1531 * outside the loop because their use_count was incremented.
1532 */
1533 object = list_entry(gray_list.next, typeof(*object), gray_list);
1534 while (&object->gray_list != &gray_list) {
1535 cond_resched();
1536
1537 /* may add new objects to the list */
1538 if (!scan_should_stop())
1539 scan_object(object);
1540
1541 tmp = list_entry(object->gray_list.next, typeof(*object),
1542 gray_list);
1543
1544 /* remove the object from the list and release it */
1545 list_del(&object->gray_list);
1546 put_object(object);
1547
1548 object = tmp;
1549 }
1550 WARN_ON(!list_empty(&gray_list));
1551}
1552
984a6083 1553/*
25e9fa22 1554 * Conditionally call resched() in an object iteration loop while making sure
984a6083 1555 * that the given object won't go away without RCU read lock by performing a
6061e740 1556 * get_object() if necessaary.
984a6083 1557 */
6061e740 1558static void kmemleak_cond_resched(struct kmemleak_object *object)
984a6083 1559{
6061e740
WL
1560 if (!get_object(object))
1561 return; /* Try next object */
984a6083 1562
782e4179
WL
1563 raw_spin_lock_irq(&kmemleak_lock);
1564 if (object->del_state & DELSTATE_REMOVED)
1565 goto unlock_put; /* Object removed */
1566 object->del_state |= DELSTATE_NO_DELETE;
1567 raw_spin_unlock_irq(&kmemleak_lock);
1568
984a6083
WL
1569 rcu_read_unlock();
1570 cond_resched();
1571 rcu_read_lock();
782e4179
WL
1572
1573 raw_spin_lock_irq(&kmemleak_lock);
1574 if (object->del_state & DELSTATE_REMOVED)
1575 list_del_rcu(&object->object_list);
1576 object->del_state &= ~DELSTATE_NO_DELETE;
1577unlock_put:
1578 raw_spin_unlock_irq(&kmemleak_lock);
6061e740 1579 put_object(object);
984a6083
WL
1580}
1581
3c7b4e6b
CM
1582/*
1583 * Scan data sections and all the referenced memory blocks allocated via the
1584 * kernel's standard allocators. This function must be called with the
1585 * scan_mutex held.
1586 */
1587static void kmemleak_scan(void)
1588{
04609ccc 1589 struct kmemleak_object *object;
c10a0f87
LY
1590 struct zone *zone;
1591 int __maybe_unused i;
4698c1f2 1592 int new_leaks = 0;
3c7b4e6b 1593
acf4968e
CM
1594 jiffies_last_scan = jiffies;
1595
3c7b4e6b
CM
1596 /* prepare the kmemleak_object's */
1597 rcu_read_lock();
1598 list_for_each_entry_rcu(object, &object_list, object_list) {
00c15506 1599 raw_spin_lock_irq(&object->lock);
3c7b4e6b
CM
1600#ifdef DEBUG
1601 /*
1602 * With a few exceptions there should be a maximum of
1603 * 1 reference to any object at this point.
1604 */
1605 if (atomic_read(&object->use_count) > 1) {
ae281064 1606 pr_debug("object->use_count = %d\n",
3c7b4e6b
CM
1607 atomic_read(&object->use_count));
1608 dump_object_info(object);
1609 }
1610#endif
84c32629
PW
1611
1612 /* ignore objects outside lowmem (paint them black) */
1613 if ((object->flags & OBJECT_PHYS) &&
1614 !(object->flags & OBJECT_NO_SCAN)) {
1615 unsigned long phys = object->pointer;
1616
1617 if (PHYS_PFN(phys) < min_low_pfn ||
1618 PHYS_PFN(phys + object->size) >= max_low_pfn)
1619 __paint_it(object, KMEMLEAK_BLACK);
1620 }
1621
3c7b4e6b
CM
1622 /* reset the reference count (whiten the object) */
1623 object->count = 0;
6061e740 1624 if (color_gray(object) && get_object(object))
3c7b4e6b
CM
1625 list_add_tail(&object->gray_list, &gray_list);
1626
00c15506 1627 raw_spin_unlock_irq(&object->lock);
6edda04c 1628
6061e740
WL
1629 if (need_resched())
1630 kmemleak_cond_resched(object);
3c7b4e6b
CM
1631 }
1632 rcu_read_unlock();
1633
3c7b4e6b
CM
1634#ifdef CONFIG_SMP
1635 /* per-cpu sections scanning */
1636 for_each_possible_cpu(i)
93ada579
CM
1637 scan_large_block(__per_cpu_start + per_cpu_offset(i),
1638 __per_cpu_end + per_cpu_offset(i));
3c7b4e6b
CM
1639#endif
1640
1641 /*
029aeff5 1642 * Struct page scanning for each node.
3c7b4e6b 1643 */
bfc8c901 1644 get_online_mems();
c10a0f87
LY
1645 for_each_populated_zone(zone) {
1646 unsigned long start_pfn = zone->zone_start_pfn;
1647 unsigned long end_pfn = zone_end_pfn(zone);
3c7b4e6b
CM
1648 unsigned long pfn;
1649
1650 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
9f1eb38e 1651 struct page *page = pfn_to_online_page(pfn);
3c7b4e6b 1652
e68d343d
WL
1653 if (!(pfn & 63))
1654 cond_resched();
1655
9f1eb38e
OS
1656 if (!page)
1657 continue;
1658
c10a0f87
LY
1659 /* only scan pages belonging to this zone */
1660 if (page_zone(page) != zone)
3c7b4e6b 1661 continue;
3c7b4e6b
CM
1662 /* only scan if page is in use */
1663 if (page_count(page) == 0)
1664 continue;
93ada579 1665 scan_block(page, page + 1, NULL);
3c7b4e6b
CM
1666 }
1667 }
bfc8c901 1668 put_online_mems();
3c7b4e6b
CM
1669
1670 /*
43ed5d6e 1671 * Scanning the task stacks (may introduce false negatives).
3c7b4e6b
CM
1672 */
1673 if (kmemleak_stack_scan) {
43ed5d6e
CM
1674 struct task_struct *p, *g;
1675
c4b28963
DB
1676 rcu_read_lock();
1677 for_each_process_thread(g, p) {
37df49f4
CM
1678 void *stack = try_get_task_stack(p);
1679 if (stack) {
1680 scan_block(stack, stack + THREAD_SIZE, NULL);
1681 put_task_stack(p);
1682 }
c4b28963
DB
1683 }
1684 rcu_read_unlock();
3c7b4e6b
CM
1685 }
1686
1687 /*
1688 * Scan the objects already referenced from the sections scanned
04609ccc 1689 * above.
3c7b4e6b 1690 */
04609ccc 1691 scan_gray_list();
2587362e
CM
1692
1693 /*
04609ccc
CM
1694 * Check for new or unreferenced objects modified since the previous
1695 * scan and color them gray until the next scan.
2587362e
CM
1696 */
1697 rcu_read_lock();
1698 list_for_each_entry_rcu(object, &object_list, object_list) {
6061e740
WL
1699 if (need_resched())
1700 kmemleak_cond_resched(object);
984a6083 1701
64977918
WL
1702 /*
1703 * This is racy but we can save the overhead of lock/unlock
1704 * calls. The missed objects, if any, should be caught in
1705 * the next scan.
1706 */
1707 if (!color_white(object))
1708 continue;
00c15506 1709 raw_spin_lock_irq(&object->lock);
04609ccc
CM
1710 if (color_white(object) && (object->flags & OBJECT_ALLOCATED)
1711 && update_checksum(object) && get_object(object)) {
1712 /* color it gray temporarily */
1713 object->count = object->min_count;
2587362e
CM
1714 list_add_tail(&object->gray_list, &gray_list);
1715 }
00c15506 1716 raw_spin_unlock_irq(&object->lock);
2587362e
CM
1717 }
1718 rcu_read_unlock();
1719
04609ccc
CM
1720 /*
1721 * Re-scan the gray list for modified unreferenced objects.
1722 */
1723 scan_gray_list();
4698c1f2 1724
17bb9e0d 1725 /*
04609ccc 1726 * If scanning was stopped do not report any new unreferenced objects.
17bb9e0d 1727 */
04609ccc 1728 if (scan_should_stop())
17bb9e0d
CM
1729 return;
1730
4698c1f2
CM
1731 /*
1732 * Scanning result reporting.
1733 */
1734 rcu_read_lock();
1735 list_for_each_entry_rcu(object, &object_list, object_list) {
6061e740
WL
1736 if (need_resched())
1737 kmemleak_cond_resched(object);
984a6083 1738
64977918
WL
1739 /*
1740 * This is racy but we can save the overhead of lock/unlock
1741 * calls. The missed objects, if any, should be caught in
1742 * the next scan.
1743 */
1744 if (!color_white(object))
1745 continue;
00c15506 1746 raw_spin_lock_irq(&object->lock);
4698c1f2
CM
1747 if (unreferenced_object(object) &&
1748 !(object->flags & OBJECT_REPORTED)) {
1749 object->flags |= OBJECT_REPORTED;
154221c3
VW
1750
1751 if (kmemleak_verbose)
1752 print_unreferenced(NULL, object);
1753
4698c1f2
CM
1754 new_leaks++;
1755 }
00c15506 1756 raw_spin_unlock_irq(&object->lock);
4698c1f2
CM
1757 }
1758 rcu_read_unlock();
1759
dc9b3f42
LZ
1760 if (new_leaks) {
1761 kmemleak_found_leaks = true;
1762
756a025f
JP
1763 pr_info("%d new suspected memory leaks (see /sys/kernel/debug/kmemleak)\n",
1764 new_leaks);
dc9b3f42 1765 }
4698c1f2 1766
3c7b4e6b
CM
1767}
1768
1769/*
1770 * Thread function performing automatic memory scanning. Unreferenced objects
1771 * at the end of a memory scan are reported but only the first time.
1772 */
1773static int kmemleak_scan_thread(void *arg)
1774{
d53ce042 1775 static int first_run = IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN);
3c7b4e6b 1776
ae281064 1777 pr_info("Automatic memory scanning thread started\n");
bf2a76b3 1778 set_user_nice(current, 10);
3c7b4e6b
CM
1779
1780 /*
1781 * Wait before the first scan to allow the system to fully initialize.
1782 */
1783 if (first_run) {
98c42d94 1784 signed long timeout = msecs_to_jiffies(SECS_FIRST_SCAN * 1000);
3c7b4e6b 1785 first_run = 0;
98c42d94
VN
1786 while (timeout && !kthread_should_stop())
1787 timeout = schedule_timeout_interruptible(timeout);
3c7b4e6b
CM
1788 }
1789
1790 while (!kthread_should_stop()) {
54dd200c 1791 signed long timeout = READ_ONCE(jiffies_scan_wait);
3c7b4e6b
CM
1792
1793 mutex_lock(&scan_mutex);
3c7b4e6b 1794 kmemleak_scan();
3c7b4e6b 1795 mutex_unlock(&scan_mutex);
4698c1f2 1796
3c7b4e6b
CM
1797 /* wait before the next scan */
1798 while (timeout && !kthread_should_stop())
1799 timeout = schedule_timeout_interruptible(timeout);
1800 }
1801
ae281064 1802 pr_info("Automatic memory scanning thread ended\n");
3c7b4e6b
CM
1803
1804 return 0;
1805}
1806
1807/*
1808 * Start the automatic memory scanning thread. This function must be called
4698c1f2 1809 * with the scan_mutex held.
3c7b4e6b 1810 */
7eb0d5e5 1811static void start_scan_thread(void)
3c7b4e6b
CM
1812{
1813 if (scan_thread)
1814 return;
1815 scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak");
1816 if (IS_ERR(scan_thread)) {
598d8091 1817 pr_warn("Failed to create the scan thread\n");
3c7b4e6b
CM
1818 scan_thread = NULL;
1819 }
1820}
1821
1822/*
914b6dff 1823 * Stop the automatic memory scanning thread.
3c7b4e6b 1824 */
7eb0d5e5 1825static void stop_scan_thread(void)
3c7b4e6b
CM
1826{
1827 if (scan_thread) {
1828 kthread_stop(scan_thread);
1829 scan_thread = NULL;
1830 }
1831}
1832
1833/*
1834 * Iterate over the object_list and return the first valid object at or after
1835 * the required position with its use_count incremented. The function triggers
1836 * a memory scanning when the pos argument points to the first position.
1837 */
1838static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos)
1839{
1840 struct kmemleak_object *object;
1841 loff_t n = *pos;
b87324d0
CM
1842 int err;
1843
1844 err = mutex_lock_interruptible(&scan_mutex);
1845 if (err < 0)
1846 return ERR_PTR(err);
3c7b4e6b 1847
3c7b4e6b
CM
1848 rcu_read_lock();
1849 list_for_each_entry_rcu(object, &object_list, object_list) {
1850 if (n-- > 0)
1851 continue;
1852 if (get_object(object))
1853 goto out;
1854 }
1855 object = NULL;
1856out:
3c7b4e6b
CM
1857 return object;
1858}
1859
1860/*
1861 * Return the next object in the object_list. The function decrements the
1862 * use_count of the previous object and increases that of the next one.
1863 */
1864static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1865{
1866 struct kmemleak_object *prev_obj = v;
1867 struct kmemleak_object *next_obj = NULL;
58fac095 1868 struct kmemleak_object *obj = prev_obj;
3c7b4e6b
CM
1869
1870 ++(*pos);
3c7b4e6b 1871
58fac095 1872 list_for_each_entry_continue_rcu(obj, &object_list, object_list) {
52c3ce4e
CM
1873 if (get_object(obj)) {
1874 next_obj = obj;
3c7b4e6b 1875 break;
52c3ce4e 1876 }
3c7b4e6b 1877 }
288c857d 1878
3c7b4e6b
CM
1879 put_object(prev_obj);
1880 return next_obj;
1881}
1882
1883/*
1884 * Decrement the use_count of the last object required, if any.
1885 */
1886static void kmemleak_seq_stop(struct seq_file *seq, void *v)
1887{
b87324d0
CM
1888 if (!IS_ERR(v)) {
1889 /*
1890 * kmemleak_seq_start may return ERR_PTR if the scan_mutex
1891 * waiting was interrupted, so only release it if !IS_ERR.
1892 */
f5886c7f 1893 rcu_read_unlock();
b87324d0
CM
1894 mutex_unlock(&scan_mutex);
1895 if (v)
1896 put_object(v);
1897 }
3c7b4e6b
CM
1898}
1899
1900/*
1901 * Print the information for an unreferenced object to the seq file.
1902 */
1903static int kmemleak_seq_show(struct seq_file *seq, void *v)
1904{
1905 struct kmemleak_object *object = v;
1906 unsigned long flags;
1907
8c96f1bc 1908 raw_spin_lock_irqsave(&object->lock, flags);
288c857d 1909 if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object))
17bb9e0d 1910 print_unreferenced(seq, object);
8c96f1bc 1911 raw_spin_unlock_irqrestore(&object->lock, flags);
3c7b4e6b
CM
1912 return 0;
1913}
1914
1915static const struct seq_operations kmemleak_seq_ops = {
1916 .start = kmemleak_seq_start,
1917 .next = kmemleak_seq_next,
1918 .stop = kmemleak_seq_stop,
1919 .show = kmemleak_seq_show,
1920};
1921
1922static int kmemleak_open(struct inode *inode, struct file *file)
1923{
b87324d0 1924 return seq_open(file, &kmemleak_seq_ops);
3c7b4e6b
CM
1925}
1926
189d84ed
CM
1927static int dump_str_object_info(const char *str)
1928{
1929 unsigned long flags;
1930 struct kmemleak_object *object;
1931 unsigned long addr;
1932
dc053733
AP
1933 if (kstrtoul(str, 0, &addr))
1934 return -EINVAL;
189d84ed
CM
1935 object = find_and_get_object(addr, 0);
1936 if (!object) {
1937 pr_info("Unknown object at 0x%08lx\n", addr);
1938 return -EINVAL;
1939 }
1940
8c96f1bc 1941 raw_spin_lock_irqsave(&object->lock, flags);
189d84ed 1942 dump_object_info(object);
8c96f1bc 1943 raw_spin_unlock_irqrestore(&object->lock, flags);
189d84ed
CM
1944
1945 put_object(object);
1946 return 0;
1947}
1948
30b37101
LR
1949/*
1950 * We use grey instead of black to ensure we can do future scans on the same
1951 * objects. If we did not do future scans these black objects could
1952 * potentially contain references to newly allocated objects in the future and
1953 * we'd end up with false positives.
1954 */
1955static void kmemleak_clear(void)
1956{
1957 struct kmemleak_object *object;
30b37101
LR
1958
1959 rcu_read_lock();
1960 list_for_each_entry_rcu(object, &object_list, object_list) {
00c15506 1961 raw_spin_lock_irq(&object->lock);
30b37101
LR
1962 if ((object->flags & OBJECT_REPORTED) &&
1963 unreferenced_object(object))
a1084c87 1964 __paint_it(object, KMEMLEAK_GREY);
00c15506 1965 raw_spin_unlock_irq(&object->lock);
30b37101
LR
1966 }
1967 rcu_read_unlock();
dc9b3f42
LZ
1968
1969 kmemleak_found_leaks = false;
30b37101
LR
1970}
1971
c89da70c
LZ
1972static void __kmemleak_do_cleanup(void);
1973
3c7b4e6b
CM
1974/*
1975 * File write operation to configure kmemleak at run-time. The following
1976 * commands can be written to the /sys/kernel/debug/kmemleak file:
1977 * off - disable kmemleak (irreversible)
1978 * stack=on - enable the task stacks scanning
1979 * stack=off - disable the tasks stacks scanning
1980 * scan=on - start the automatic memory scanning thread
1981 * scan=off - stop the automatic memory scanning thread
1982 * scan=... - set the automatic memory scanning period in seconds (0 to
1983 * disable it)
4698c1f2 1984 * scan - trigger a memory scan
30b37101 1985 * clear - mark all current reported unreferenced kmemleak objects as
c89da70c
LZ
1986 * grey to ignore printing them, or free all kmemleak objects
1987 * if kmemleak has been disabled.
189d84ed 1988 * dump=... - dump information about the object found at the given address
3c7b4e6b
CM
1989 */
1990static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
1991 size_t size, loff_t *ppos)
1992{
1993 char buf[64];
1994 int buf_size;
b87324d0 1995 int ret;
3c7b4e6b
CM
1996
1997 buf_size = min(size, (sizeof(buf) - 1));
1998 if (strncpy_from_user(buf, user_buf, buf_size) < 0)
1999 return -EFAULT;
2000 buf[buf_size] = 0;
2001
b87324d0
CM
2002 ret = mutex_lock_interruptible(&scan_mutex);
2003 if (ret < 0)
2004 return ret;
2005
c89da70c 2006 if (strncmp(buf, "clear", 5) == 0) {
8910ae89 2007 if (kmemleak_enabled)
c89da70c
LZ
2008 kmemleak_clear();
2009 else
2010 __kmemleak_do_cleanup();
2011 goto out;
2012 }
2013
8910ae89 2014 if (!kmemleak_enabled) {
4e4dfce2 2015 ret = -EPERM;
c89da70c
LZ
2016 goto out;
2017 }
2018
3c7b4e6b
CM
2019 if (strncmp(buf, "off", 3) == 0)
2020 kmemleak_disable();
2021 else if (strncmp(buf, "stack=on", 8) == 0)
2022 kmemleak_stack_scan = 1;
2023 else if (strncmp(buf, "stack=off", 9) == 0)
2024 kmemleak_stack_scan = 0;
2025 else if (strncmp(buf, "scan=on", 7) == 0)
2026 start_scan_thread();
2027 else if (strncmp(buf, "scan=off", 8) == 0)
2028 stop_scan_thread();
2029 else if (strncmp(buf, "scan=", 5) == 0) {
54dd200c
YX
2030 unsigned secs;
2031 unsigned long msecs;
3c7b4e6b 2032
54dd200c 2033 ret = kstrtouint(buf + 5, 0, &secs);
b87324d0
CM
2034 if (ret < 0)
2035 goto out;
54dd200c
YX
2036
2037 msecs = secs * MSEC_PER_SEC;
2038 if (msecs > UINT_MAX)
2039 msecs = UINT_MAX;
2040
3c7b4e6b 2041 stop_scan_thread();
54dd200c
YX
2042 if (msecs) {
2043 WRITE_ONCE(jiffies_scan_wait, msecs_to_jiffies(msecs));
3c7b4e6b
CM
2044 start_scan_thread();
2045 }
4698c1f2
CM
2046 } else if (strncmp(buf, "scan", 4) == 0)
2047 kmemleak_scan();
189d84ed
CM
2048 else if (strncmp(buf, "dump=", 5) == 0)
2049 ret = dump_str_object_info(buf + 5);
4698c1f2 2050 else
b87324d0
CM
2051 ret = -EINVAL;
2052
2053out:
2054 mutex_unlock(&scan_mutex);
2055 if (ret < 0)
2056 return ret;
3c7b4e6b
CM
2057
2058 /* ignore the rest of the buffer, only one command at a time */
2059 *ppos += size;
2060 return size;
2061}
2062
2063static const struct file_operations kmemleak_fops = {
2064 .owner = THIS_MODULE,
2065 .open = kmemleak_open,
2066 .read = seq_read,
2067 .write = kmemleak_write,
2068 .llseek = seq_lseek,
5f3bf19a 2069 .release = seq_release,
3c7b4e6b
CM
2070};
2071
c89da70c
LZ
2072static void __kmemleak_do_cleanup(void)
2073{
2abd839a 2074 struct kmemleak_object *object, *tmp;
c89da70c 2075
2abd839a
CM
2076 /*
2077 * Kmemleak has already been disabled, no need for RCU list traversal
2078 * or kmemleak_lock held.
2079 */
2080 list_for_each_entry_safe(object, tmp, &object_list, object_list) {
2081 __remove_object(object);
2082 __delete_object(object);
2083 }
c89da70c
LZ
2084}
2085
3c7b4e6b 2086/*
74341703
CM
2087 * Stop the memory scanning thread and free the kmemleak internal objects if
2088 * no previous scan thread (otherwise, kmemleak may still have some useful
2089 * information on memory leaks).
3c7b4e6b 2090 */
179a8100 2091static void kmemleak_do_cleanup(struct work_struct *work)
3c7b4e6b 2092{
3c7b4e6b 2093 stop_scan_thread();
3c7b4e6b 2094
914b6dff 2095 mutex_lock(&scan_mutex);
c5f3b1a5 2096 /*
914b6dff
VM
2097 * Once it is made sure that kmemleak_scan has stopped, it is safe to no
2098 * longer track object freeing. Ordering of the scan thread stopping and
2099 * the memory accesses below is guaranteed by the kthread_stop()
2100 * function.
c5f3b1a5
CM
2101 */
2102 kmemleak_free_enabled = 0;
914b6dff 2103 mutex_unlock(&scan_mutex);
c5f3b1a5 2104
c89da70c
LZ
2105 if (!kmemleak_found_leaks)
2106 __kmemleak_do_cleanup();
2107 else
756a025f 2108 pr_info("Kmemleak disabled without freeing internal data. Reclaim the memory with \"echo clear > /sys/kernel/debug/kmemleak\".\n");
3c7b4e6b
CM
2109}
2110
179a8100 2111static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup);
3c7b4e6b
CM
2112
2113/*
2114 * Disable kmemleak. No memory allocation/freeing will be traced once this
2115 * function is called. Disabling kmemleak is an irreversible operation.
2116 */
2117static void kmemleak_disable(void)
2118{
2119 /* atomically check whether it was already invoked */
8910ae89 2120 if (cmpxchg(&kmemleak_error, 0, 1))
3c7b4e6b
CM
2121 return;
2122
2123 /* stop any memory operation tracing */
8910ae89 2124 kmemleak_enabled = 0;
3c7b4e6b
CM
2125
2126 /* check whether it is too early for a kernel thread */
d160ef71 2127 if (kmemleak_late_initialized)
179a8100 2128 schedule_work(&cleanup_work);
c5f3b1a5
CM
2129 else
2130 kmemleak_free_enabled = 0;
3c7b4e6b
CM
2131
2132 pr_info("Kernel memory leak detector disabled\n");
2133}
2134
2135/*
2136 * Allow boot-time kmemleak disabling (enabled by default).
2137 */
8bd30c10 2138static int __init kmemleak_boot_config(char *str)
3c7b4e6b
CM
2139{
2140 if (!str)
2141 return -EINVAL;
2142 if (strcmp(str, "off") == 0)
2143 kmemleak_disable();
993f57e0 2144 else if (strcmp(str, "on") == 0) {
ab0155a2 2145 kmemleak_skip_disable = 1;
1c0310ad 2146 stack_depot_request_early_init();
993f57e0 2147 }
ab0155a2 2148 else
3c7b4e6b
CM
2149 return -EINVAL;
2150 return 0;
2151}
2152early_param("kmemleak", kmemleak_boot_config);
2153
2154/*
2030117d 2155 * Kmemleak initialization.
3c7b4e6b
CM
2156 */
2157void __init kmemleak_init(void)
2158{
ab0155a2
JB
2159#ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF
2160 if (!kmemleak_skip_disable) {
2161 kmemleak_disable();
2162 return;
2163 }
2164#endif
2165
c5665868
CM
2166 if (kmemleak_error)
2167 return;
2168
3c7b4e6b
CM
2169 jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE);
2170 jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000);
2171
2172 object_cache = KMEM_CACHE(kmemleak_object, SLAB_NOLEAKTRACE);
2173 scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE);
3c7b4e6b 2174
298a32b1
CM
2175 /* register the data/bss sections */
2176 create_object((unsigned long)_sdata, _edata - _sdata,
2177 KMEMLEAK_GREY, GFP_ATOMIC);
2178 create_object((unsigned long)__bss_start, __bss_stop - __bss_start,
2179 KMEMLEAK_GREY, GFP_ATOMIC);
2180 /* only register .data..ro_after_init if not within .data */
b0d14fc4 2181 if (&__start_ro_after_init < &_sdata || &__end_ro_after_init > &_edata)
298a32b1
CM
2182 create_object((unsigned long)__start_ro_after_init,
2183 __end_ro_after_init - __start_ro_after_init,
2184 KMEMLEAK_GREY, GFP_ATOMIC);
3c7b4e6b
CM
2185}
2186
2187/*
2188 * Late initialization function.
2189 */
2190static int __init kmemleak_late_init(void)
2191{
d160ef71 2192 kmemleak_late_initialized = 1;
3c7b4e6b 2193
282401df 2194 debugfs_create_file("kmemleak", 0644, NULL, NULL, &kmemleak_fops);
b353756b 2195
8910ae89 2196 if (kmemleak_error) {
3c7b4e6b 2197 /*
25985edc 2198 * Some error occurred and kmemleak was disabled. There is a
3c7b4e6b 2199 * small chance that kmemleak_disable() was called immediately
d160ef71 2200 * after setting kmemleak_late_initialized and we may end up with
3c7b4e6b
CM
2201 * two clean-up threads but serialized by scan_mutex.
2202 */
179a8100 2203 schedule_work(&cleanup_work);
3c7b4e6b
CM
2204 return -ENOMEM;
2205 }
2206
d53ce042
SK
2207 if (IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN)) {
2208 mutex_lock(&scan_mutex);
2209 start_scan_thread();
2210 mutex_unlock(&scan_mutex);
2211 }
3c7b4e6b 2212
0e965a6b
QC
2213 pr_info("Kernel memory leak detector initialized (mem pool available: %d)\n",
2214 mem_pool_free_count);
3c7b4e6b
CM
2215
2216 return 0;
2217}
2218late_initcall(kmemleak_late_init);