1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * mm/kmemleak.c 4 * 5 * Copyright (C) 2008 ARM Limited 6 * Written by Catalin Marinas <catalin.marinas@arm.com> 7 * 8 * For more information on the algorithm and kmemleak usage, please see 9 * Documentation/dev-tools/kmemleak.rst. 10 * 11 * Notes on locking 12 * ---------------- 13 * 14 * The following locks and mutexes are used by kmemleak: 15 * 16 * - kmemleak_lock (raw_spinlock_t): protects the object_list as well as 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 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. 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 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 43 * 44 * Locks and mutexes are acquired/nested in the following order: 45 * 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. 50 * 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 59 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 60 61 #include <linux/init.h> 62 #include <linux/kernel.h> 63 #include <linux/list.h> 64 #include <linux/sched/signal.h> 65 #include <linux/sched/task.h> 66 #include <linux/sched/task_stack.h> 67 #include <linux/jiffies.h> 68 #include <linux/delay.h> 69 #include <linux/export.h> 70 #include <linux/kthread.h> 71 #include <linux/rbtree.h> 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> 77 #include <linux/module.h> 78 #include <linux/mutex.h> 79 #include <linux/rcupdate.h> 80 #include <linux/stacktrace.h> 81 #include <linux/stackdepot.h> 82 #include <linux/cache.h> 83 #include <linux/percpu.h> 84 #include <linux/memblock.h> 85 #include <linux/pfn.h> 86 #include <linux/mmzone.h> 87 #include <linux/slab.h> 88 #include <linux/thread_info.h> 89 #include <linux/err.h> 90 #include <linux/uaccess.h> 91 #include <linux/string.h> 92 #include <linux/nodemask.h> 93 #include <linux/mm.h> 94 #include <linux/workqueue.h> 95 #include <linux/xarray.h> 96 #include <linux/crc32.h> 97 98 #include <asm/sections.h> 99 #include <asm/processor.h> 100 #include <linux/atomic.h> 101 102 #include <linux/kasan.h> 103 #include <linux/kfence.h> 104 #include <linux/kmemleak.h> 105 #include <linux/memory_hotplug.h> 106 107 /* 108 * Kmemleak configuration and common defines. 109 */ 110 #define MAX_TRACE 16 /* stack trace length */ 111 #define MSECS_MIN_AGE 5000 /* minimum object age for reporting */ 112 #define SECS_FIRST_SCAN 60 /* delay before the first scan */ 113 #define SECS_SCAN_WAIT 600 /* subsequent auto scanning delay */ 114 #define MAX_SCAN_SIZE 4096 /* maximum size of a scanned block */ 115 116 #define BYTES_PER_POINTER sizeof(void *) 117 118 /* scanning area inside a memory block */ 119 struct kmemleak_scan_area { 120 struct hlist_node node; 121 unsigned long start; 122 size_t size; 123 }; 124 125 #define KMEMLEAK_GREY 0 126 #define KMEMLEAK_BLACK -1 127 128 /* 129 * Structure holding the metadata for each allocated memory block. 130 * Modifications to such objects should be made while holding the 131 * object->lock. Insertions or deletions from object_list, gray_list or 132 * rb_node are already protected by the corresponding locks or mutex (see 133 * the notes on locking above). These objects are reference-counted 134 * (use_count) and freed using the RCU mechanism. 135 */ 136 struct kmemleak_object { 137 raw_spinlock_t lock; 138 unsigned int flags; /* object status flags */ 139 struct list_head object_list; 140 struct list_head gray_list; 141 struct rb_node rb_node; 142 struct rcu_head rcu; /* object_list lockless traversal */ 143 /* object usage count; object freed when use_count == 0 */ 144 atomic_t use_count; 145 unsigned int del_state; /* deletion state */ 146 unsigned long pointer; 147 size_t size; 148 /* pass surplus references to this pointer */ 149 unsigned long excess_ref; 150 /* 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; 154 /* consecutive scans the object has been seen unreferenced */ 155 unsigned int unref_scans; 156 /* checksum for detecting modified objects */ 157 u32 checksum; 158 depot_stack_handle_t trace_handle; 159 /* memory ranges to be scanned inside an object (empty for all) */ 160 struct hlist_head area_list; 161 unsigned long jiffies; /* creation timestamp */ 162 pid_t pid; /* pid of the current task */ 163 /* per-scan dedup count, valid only while in scan-local dedup xarray */ 164 unsigned int dup_count; 165 char comm[TASK_COMM_LEN]; /* executable name */ 166 }; 167 168 /* flag representing the memory block allocation status */ 169 #define OBJECT_ALLOCATED (1 << 0) 170 /* flag set after the first reporting of an unreference object */ 171 #define OBJECT_REPORTED (1 << 1) 172 /* flag set to not scan the object */ 173 #define OBJECT_NO_SCAN (1 << 2) 174 /* flag set to fully scan the object when scan_area allocation failed */ 175 #define OBJECT_FULL_SCAN (1 << 3) 176 /* flag set for object allocated with physical address */ 177 #define OBJECT_PHYS (1 << 4) 178 /* flag set for per-CPU pointers */ 179 #define OBJECT_PERCPU (1 << 5) 180 /* flag set on an object left unreferenced by the full scan, pending confirmation */ 181 #define OBJECT_SUSPECT (1 << 6) 182 183 /* set when __remove_object() called */ 184 #define DELSTATE_REMOVED (1 << 0) 185 /* set to temporarily prevent deletion from object_list */ 186 #define DELSTATE_NO_DELETE (1 << 1) 187 188 #define HEX_PREFIX " " 189 /* number of bytes to print per line; must be 16 or 32 */ 190 #define HEX_ROW_SIZE 16 191 /* number of bytes to print at a time (1, 2, 4, 8) */ 192 #define HEX_GROUP_SIZE 1 193 /* include ASCII after the hex output */ 194 #define HEX_ASCII 1 195 /* max number of lines to be printed */ 196 #define HEX_MAX_LINES 2 197 198 /* the list of all allocated objects */ 199 static LIST_HEAD(object_list); 200 /* the list of gray-colored objects (see color_gray comment below) */ 201 static LIST_HEAD(gray_list); 202 /* memory pool allocation */ 203 static struct kmemleak_object mem_pool[CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE]; 204 static int mem_pool_free_count = ARRAY_SIZE(mem_pool); 205 static LIST_HEAD(mem_pool_free_list); 206 /* search tree for object boundaries */ 207 static struct rb_root object_tree_root = RB_ROOT; 208 /* search tree for object (with OBJECT_PHYS flag) boundaries */ 209 static struct rb_root object_phys_tree_root = RB_ROOT; 210 /* search tree for object (with OBJECT_PERCPU flag) boundaries */ 211 static struct rb_root object_percpu_tree_root = RB_ROOT; 212 /* protecting the access to object_list, object_tree_root (or object_phys_tree_root) */ 213 static DEFINE_RAW_SPINLOCK(kmemleak_lock); 214 215 /* allocation caches for kmemleak internal data */ 216 static struct kmem_cache *object_cache; 217 static struct kmem_cache *scan_area_cache; 218 219 /* set if tracing memory operations is enabled */ 220 static int kmemleak_enabled __read_mostly = 1; 221 /* same as above but only for the kmemleak_free() callback */ 222 static int kmemleak_free_enabled __read_mostly = 1; 223 /* set in the late_initcall if there were no errors */ 224 static int kmemleak_late_initialized; 225 /* set if a fatal kmemleak error has occurred */ 226 static int kmemleak_error; 227 228 /* minimum and maximum address that may be valid pointers */ 229 static unsigned long min_addr = ULONG_MAX; 230 static unsigned long max_addr; 231 232 /* minimum and maximum address that may be valid per-CPU pointers */ 233 static unsigned long min_percpu_addr = ULONG_MAX; 234 static unsigned long max_percpu_addr; 235 236 static struct task_struct *scan_thread; 237 /* used to avoid reporting of recently allocated objects */ 238 static unsigned long jiffies_min_age; 239 /* consecutive scans an object must stay unreferenced before reporting */ 240 static unsigned int min_unref_scans = 241 IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_VERBOSE) ? 3 : 1; 242 module_param(min_unref_scans, uint, 0644); 243 static unsigned long jiffies_last_scan; 244 /* delay between automatic memory scannings */ 245 static unsigned long jiffies_scan_wait; 246 /* number of objects flagged OBJECT_SUSPECT during the current scan */ 247 static int nr_suspects; 248 /* enables or disables the task stacks scanning */ 249 static int kmemleak_stack_scan = 1; 250 /* protects the memory scanning, parameters and debug/kmemleak file access */ 251 static DEFINE_MUTEX(scan_mutex); 252 /* setting kmemleak=on, will set this var, skipping the disable */ 253 static int kmemleak_skip_disable; 254 /* If there are leaks that can be reported */ 255 static bool kmemleak_found_leaks; 256 257 static bool kmemleak_verbose = IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_VERBOSE); 258 module_param_named(verbose, kmemleak_verbose, bool, 0600); 259 260 static void kmemleak_disable(void); 261 262 /* 263 * Print a warning and dump the stack trace. 264 */ 265 #define kmemleak_warn(x...) do { \ 266 pr_warn(x); \ 267 dump_stack(); \ 268 } while (0) 269 270 /* 271 * Macro invoked when a serious kmemleak condition occurred and cannot be 272 * recovered from. Kmemleak will be disabled and further allocation/freeing 273 * tracing no longer available. 274 */ 275 #define kmemleak_stop(x...) do { \ 276 kmemleak_warn(x); \ 277 kmemleak_disable(); \ 278 } while (0) 279 280 #define warn_or_seq_printf(seq, fmt, ...) do { \ 281 if (seq) \ 282 seq_printf(seq, fmt, ##__VA_ARGS__); \ 283 else \ 284 pr_warn(fmt, ##__VA_ARGS__); \ 285 } while (0) 286 287 static void warn_or_seq_hex_dump(struct seq_file *seq, int prefix_type, 288 int rowsize, int groupsize, const void *buf, 289 size_t len, bool ascii) 290 { 291 if (seq) 292 seq_hex_dump(seq, HEX_PREFIX, prefix_type, rowsize, groupsize, 293 buf, len, ascii); 294 else 295 print_hex_dump(KERN_WARNING, pr_fmt(HEX_PREFIX), prefix_type, 296 rowsize, groupsize, buf, len, ascii); 297 } 298 299 /* 300 * Printing of the objects hex dump to the seq file. The number of lines to be 301 * printed is limited to HEX_MAX_LINES to prevent seq file spamming. The 302 * actual number of printed bytes depends on HEX_ROW_SIZE. It must be called 303 * with the object->lock held. 304 */ 305 static void hex_dump_object(struct seq_file *seq, 306 struct kmemleak_object *object) 307 { 308 const u8 *ptr = (const u8 *)object->pointer; 309 size_t len; 310 311 if (WARN_ON_ONCE(object->flags & OBJECT_PHYS)) 312 return; 313 314 if (object->flags & OBJECT_PERCPU) 315 ptr = (const u8 *)this_cpu_ptr((void __percpu *)object->pointer); 316 317 /* limit the number of lines to HEX_MAX_LINES */ 318 len = min_t(size_t, object->size, HEX_MAX_LINES * HEX_ROW_SIZE); 319 320 if (object->flags & OBJECT_PERCPU) 321 warn_or_seq_printf(seq, " hex dump (first %zu bytes on cpu %d):\n", 322 len, raw_smp_processor_id()); 323 else 324 warn_or_seq_printf(seq, " hex dump (first %zu bytes):\n", len); 325 kasan_disable_current(); 326 warn_or_seq_hex_dump(seq, DUMP_PREFIX_NONE, HEX_ROW_SIZE, 327 HEX_GROUP_SIZE, kasan_reset_tag((void *)ptr), len, HEX_ASCII); 328 kasan_enable_current(); 329 } 330 331 /* 332 * Object colors, encoded with count and min_count: 333 * - white - orphan object, not enough references to it (count < min_count) 334 * - gray - not orphan, not marked as false positive (min_count == 0) or 335 * sufficient references to it (count >= min_count) 336 * - black - ignore, it doesn't contain references (e.g. text section) 337 * (min_count == -1). No function defined for this color. 338 */ 339 static bool color_white(const struct kmemleak_object *object) 340 { 341 return object->count != KMEMLEAK_BLACK && 342 object->count < object->min_count; 343 } 344 345 static bool color_gray(const struct kmemleak_object *object) 346 { 347 return object->min_count != KMEMLEAK_BLACK && 348 object->count >= object->min_count; 349 } 350 351 /* 352 * Objects are considered unreferenced only if their color is white, they have 353 * not be deleted and have a minimum age to avoid false positives caused by 354 * pointers temporarily stored in CPU registers. 355 */ 356 static bool unreferenced_object(struct kmemleak_object *object) 357 { 358 return (color_white(object) && object->flags & OBJECT_ALLOCATED) && 359 time_before_eq(object->jiffies + jiffies_min_age, 360 jiffies_last_scan); 361 } 362 363 static const char *__object_type_str(struct kmemleak_object *object) 364 { 365 if (object->flags & OBJECT_PHYS) 366 return " (phys)"; 367 if (object->flags & OBJECT_PERCPU) 368 return " (percpu)"; 369 return ""; 370 } 371 372 /* 373 * Printing of the unreferenced objects information to the seq file. The 374 * print_unreferenced function must be called with the object->lock held. 375 */ 376 static void __print_unreferenced(struct seq_file *seq, 377 struct kmemleak_object *object, 378 bool hex_dump) 379 { 380 int i; 381 unsigned long *entries; 382 unsigned int nr_entries; 383 384 nr_entries = stack_depot_fetch(object->trace_handle, &entries); 385 warn_or_seq_printf(seq, "unreferenced object%s 0x%08lx (size %zu):\n", 386 __object_type_str(object), 387 object->pointer, object->size); 388 warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu\n", 389 object->comm, object->pid, object->jiffies); 390 if (hex_dump) 391 hex_dump_object(seq, object); 392 warn_or_seq_printf(seq, " backtrace (crc %x):\n", object->checksum); 393 394 for (i = 0; i < nr_entries; i++) { 395 void *ptr = (void *)entries[i]; 396 warn_or_seq_printf(seq, " %pS\n", ptr); 397 } 398 } 399 400 static void print_unreferenced(struct seq_file *seq, 401 struct kmemleak_object *object) 402 { 403 __print_unreferenced(seq, object, true); 404 } 405 406 /* 407 * Print the kmemleak_object information. This function is used mainly for 408 * debugging special cases when kmemleak operations. It must be called with 409 * the object->lock held. 410 */ 411 static void dump_object_info(struct kmemleak_object *object) 412 { 413 pr_notice("Object%s 0x%08lx (size %zu):\n", 414 __object_type_str(object), object->pointer, object->size); 415 pr_notice(" comm \"%s\", pid %d, jiffies %lu\n", 416 object->comm, object->pid, object->jiffies); 417 pr_notice(" min_count = %d\n", object->min_count); 418 pr_notice(" count = %d\n", object->count); 419 pr_notice(" flags = 0x%x\n", object->flags); 420 pr_notice(" checksum = %u\n", object->checksum); 421 pr_notice(" backtrace:\n"); 422 if (object->trace_handle) 423 stack_depot_print(object->trace_handle); 424 } 425 426 static struct rb_root *object_tree(unsigned long objflags) 427 { 428 if (objflags & OBJECT_PHYS) 429 return &object_phys_tree_root; 430 if (objflags & OBJECT_PERCPU) 431 return &object_percpu_tree_root; 432 return &object_tree_root; 433 } 434 435 /* 436 * Look-up a memory block metadata (kmemleak_object) in the object search 437 * tree based on a pointer value. If alias is 0, only values pointing to the 438 * beginning of the memory block are allowed. The kmemleak_lock must be held 439 * when calling this function. 440 */ 441 static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias, 442 unsigned int objflags) 443 { 444 struct rb_node *rb = object_tree(objflags)->rb_node; 445 unsigned long untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); 446 447 while (rb) { 448 struct kmemleak_object *object; 449 unsigned long untagged_objp; 450 451 object = rb_entry(rb, struct kmemleak_object, rb_node); 452 untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer); 453 454 if (untagged_ptr < untagged_objp) 455 rb = object->rb_node.rb_left; 456 else if (untagged_objp + object->size <= untagged_ptr) 457 rb = object->rb_node.rb_right; 458 else if (untagged_objp == untagged_ptr || alias) 459 return object; 460 else { 461 /* 462 * Printk deferring due to the kmemleak_lock held. 463 * This is done to avoid deadlock. 464 */ 465 printk_deferred_enter(); 466 kmemleak_warn("Found object by alias at 0x%08lx\n", 467 ptr); 468 dump_object_info(object); 469 printk_deferred_exit(); 470 break; 471 } 472 } 473 return NULL; 474 } 475 476 /* Look-up a kmemleak object which allocated with virtual address. */ 477 static struct kmemleak_object *lookup_object(unsigned long ptr, int alias) 478 { 479 return __lookup_object(ptr, alias, 0); 480 } 481 482 /* 483 * Increment the object use_count. Return 1 if successful or 0 otherwise. Note 484 * that once an object's use_count reached 0, the RCU freeing was already 485 * registered and the object should no longer be used. This function must be 486 * called under the protection of rcu_read_lock(). 487 */ 488 static int get_object(struct kmemleak_object *object) 489 { 490 return atomic_inc_not_zero(&object->use_count); 491 } 492 493 /* 494 * Memory pool allocation and freeing. kmemleak_lock must not be held. 495 */ 496 static struct kmemleak_object *mem_pool_alloc(gfp_t gfp) 497 { 498 unsigned long flags; 499 struct kmemleak_object *object; 500 bool warn = false; 501 502 /* try the slab allocator first */ 503 if (object_cache) { 504 object = kmem_cache_alloc_noprof(object_cache, 505 gfp_nested_mask(gfp)); 506 if (object) 507 return object; 508 } 509 510 /* slab allocation failed, try the memory pool */ 511 raw_spin_lock_irqsave(&kmemleak_lock, flags); 512 object = list_first_entry_or_null(&mem_pool_free_list, 513 typeof(*object), object_list); 514 if (object) 515 list_del(&object->object_list); 516 else if (mem_pool_free_count) 517 object = &mem_pool[--mem_pool_free_count]; 518 else 519 warn = true; 520 raw_spin_unlock_irqrestore(&kmemleak_lock, flags); 521 if (warn) 522 pr_warn_once("Memory pool empty, consider increasing CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE\n"); 523 524 return object; 525 } 526 527 /* 528 * Return the object to either the slab allocator or the memory pool. 529 */ 530 static void mem_pool_free(struct kmemleak_object *object) 531 { 532 unsigned long flags; 533 534 if (object < mem_pool || object >= ARRAY_END(mem_pool)) { 535 kmem_cache_free(object_cache, object); 536 return; 537 } 538 539 /* add the object to the memory pool free list */ 540 raw_spin_lock_irqsave(&kmemleak_lock, flags); 541 list_add(&object->object_list, &mem_pool_free_list); 542 raw_spin_unlock_irqrestore(&kmemleak_lock, flags); 543 } 544 545 /* 546 * RCU callback to free a kmemleak_object. 547 */ 548 static void free_object_rcu(struct rcu_head *rcu) 549 { 550 struct hlist_node *tmp; 551 struct kmemleak_scan_area *area; 552 struct kmemleak_object *object = 553 container_of(rcu, struct kmemleak_object, rcu); 554 555 /* 556 * Once use_count is 0 (guaranteed by put_object), there is no other 557 * code accessing this object, hence no need for locking. 558 */ 559 hlist_for_each_entry_safe(area, tmp, &object->area_list, node) { 560 hlist_del(&area->node); 561 kmem_cache_free(scan_area_cache, area); 562 } 563 mem_pool_free(object); 564 } 565 566 /* 567 * Decrement the object use_count. Once the count is 0, free the object using 568 * an RCU callback. Since put_object() may be called via the kmemleak_free() -> 569 * delete_object() path, the delayed RCU freeing ensures that there is no 570 * recursive call to the kernel allocator. Lock-less RCU object_list traversal 571 * is also possible. 572 */ 573 static void put_object(struct kmemleak_object *object) 574 { 575 if (!atomic_dec_and_test(&object->use_count)) 576 return; 577 578 /* should only get here after delete_object was called */ 579 WARN_ON(object->flags & OBJECT_ALLOCATED); 580 581 /* 582 * It may be too early for the RCU callbacks, however, there is no 583 * concurrent object_list traversal when !object_cache and all objects 584 * came from the memory pool. Free the object directly. 585 */ 586 if (object_cache) 587 call_rcu(&object->rcu, free_object_rcu); 588 else 589 free_object_rcu(&object->rcu); 590 } 591 592 /* 593 * Look up an object in the object search tree and increase its use_count. 594 */ 595 static struct kmemleak_object *__find_and_get_object(unsigned long ptr, int alias, 596 unsigned int objflags) 597 { 598 unsigned long flags; 599 struct kmemleak_object *object; 600 601 rcu_read_lock(); 602 raw_spin_lock_irqsave(&kmemleak_lock, flags); 603 object = __lookup_object(ptr, alias, objflags); 604 raw_spin_unlock_irqrestore(&kmemleak_lock, flags); 605 606 /* check whether the object is still available */ 607 if (object && !get_object(object)) 608 object = NULL; 609 rcu_read_unlock(); 610 611 return object; 612 } 613 614 /* Look up and get an object which allocated with virtual address. */ 615 static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias) 616 { 617 return __find_and_get_object(ptr, alias, 0); 618 } 619 620 /* 621 * Remove an object from its object tree and object_list. Must be called with 622 * the kmemleak_lock held _if_ kmemleak is still enabled. 623 */ 624 static void __remove_object(struct kmemleak_object *object) 625 { 626 rb_erase(&object->rb_node, object_tree(object->flags)); 627 if (!(object->del_state & DELSTATE_NO_DELETE)) 628 list_del_rcu(&object->object_list); 629 object->del_state |= DELSTATE_REMOVED; 630 } 631 632 static struct kmemleak_object *__find_and_remove_object(unsigned long ptr, 633 int alias, 634 unsigned int objflags) 635 { 636 struct kmemleak_object *object; 637 638 object = __lookup_object(ptr, alias, objflags); 639 if (object) 640 __remove_object(object); 641 642 return object; 643 } 644 645 /* 646 * Look up an object in the object search tree and remove it from both object 647 * tree root and object_list. The returned object's use_count should be at 648 * least 1, as initially set by create_object(). 649 */ 650 static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int alias, 651 unsigned int objflags) 652 { 653 unsigned long flags; 654 struct kmemleak_object *object; 655 656 raw_spin_lock_irqsave(&kmemleak_lock, flags); 657 object = __find_and_remove_object(ptr, alias, objflags); 658 raw_spin_unlock_irqrestore(&kmemleak_lock, flags); 659 660 return object; 661 } 662 663 static noinline depot_stack_handle_t set_track_prepare(void) 664 { 665 depot_stack_handle_t trace_handle; 666 unsigned long entries[MAX_TRACE]; 667 unsigned int nr_entries; 668 669 /* 670 * Use object_cache to determine whether kmemleak_init() has 671 * been invoked. stack_depot_early_init() is called before 672 * kmemleak_init() in mm_core_init(). 673 */ 674 if (!object_cache) 675 return 0; 676 nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3); 677 trace_handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT); 678 679 return trace_handle; 680 } 681 682 static struct kmemleak_object *__alloc_object(gfp_t gfp) 683 { 684 struct kmemleak_object *object; 685 686 object = mem_pool_alloc(gfp); 687 if (!object) { 688 pr_warn("Cannot allocate a kmemleak_object structure\n"); 689 kmemleak_disable(); 690 return NULL; 691 } 692 693 INIT_LIST_HEAD(&object->object_list); 694 INIT_LIST_HEAD(&object->gray_list); 695 INIT_HLIST_HEAD(&object->area_list); 696 raw_spin_lock_init(&object->lock); 697 atomic_set(&object->use_count, 1); 698 object->excess_ref = 0; 699 object->count = 0; /* white color initially */ 700 object->checksum = ~0; 701 object->unref_scans = 0; 702 object->del_state = 0; 703 704 /* task information */ 705 if (in_hardirq()) { 706 object->pid = 0; 707 strscpy(object->comm, "hardirq"); 708 } else if (in_serving_softirq()) { 709 object->pid = 0; 710 strscpy(object->comm, "softirq"); 711 } else { 712 object->pid = current->pid; 713 /* 714 * There is a small chance of a race with set_task_comm(), 715 * however using get_task_comm() here may cause locking 716 * dependency issues with current->alloc_lock. In the worst 717 * case, the command line is not correct. 718 */ 719 strscpy(object->comm, current->comm); 720 } 721 722 /* kernel backtrace */ 723 object->trace_handle = set_track_prepare(); 724 725 return object; 726 } 727 728 static int __link_object(struct kmemleak_object *object, unsigned long ptr, 729 size_t size, int min_count, unsigned int objflags) 730 { 731 732 struct kmemleak_object *parent; 733 struct rb_node **link, *rb_parent; 734 unsigned long untagged_ptr; 735 unsigned long untagged_objp; 736 737 object->flags = OBJECT_ALLOCATED | objflags; 738 object->pointer = ptr; 739 object->size = kfence_ksize((void *)ptr) ?: size; 740 object->min_count = min_count; 741 object->jiffies = jiffies; 742 743 untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); 744 /* 745 * Only update min_addr and max_addr with object storing virtual 746 * address. And update min_percpu_addr max_percpu_addr for per-CPU 747 * objects. 748 */ 749 if (objflags & OBJECT_PERCPU) { 750 min_percpu_addr = min(min_percpu_addr, untagged_ptr); 751 max_percpu_addr = max(max_percpu_addr, untagged_ptr + size); 752 } else if (!(objflags & OBJECT_PHYS)) { 753 min_addr = min(min_addr, untagged_ptr); 754 max_addr = max(max_addr, untagged_ptr + size); 755 } 756 link = &object_tree(objflags)->rb_node; 757 rb_parent = NULL; 758 while (*link) { 759 rb_parent = *link; 760 parent = rb_entry(rb_parent, struct kmemleak_object, rb_node); 761 untagged_objp = (unsigned long)kasan_reset_tag((void *)parent->pointer); 762 if (untagged_ptr + size <= untagged_objp) 763 link = &parent->rb_node.rb_left; 764 else if (untagged_objp + parent->size <= untagged_ptr) 765 link = &parent->rb_node.rb_right; 766 else { 767 /* 768 * Printk deferring due to the kmemleak_lock held. 769 * This is done to avoid deadlock. 770 */ 771 printk_deferred_enter(); 772 kmemleak_stop("Cannot insert 0x%lx into the object search tree (overlaps existing)\n", 773 ptr); 774 /* 775 * No need for parent->lock here since "parent" cannot 776 * be freed while the kmemleak_lock is held. 777 */ 778 dump_object_info(parent); 779 printk_deferred_exit(); 780 return -EEXIST; 781 } 782 } 783 rb_link_node(&object->rb_node, rb_parent, link); 784 rb_insert_color(&object->rb_node, object_tree(objflags)); 785 list_add_tail_rcu(&object->object_list, &object_list); 786 787 return 0; 788 } 789 790 /* 791 * Create the metadata (struct kmemleak_object) corresponding to an allocated 792 * memory block and add it to the object_list and object tree. 793 */ 794 static void __create_object(unsigned long ptr, size_t size, 795 int min_count, gfp_t gfp, unsigned int objflags) 796 { 797 struct kmemleak_object *object; 798 unsigned long flags; 799 int ret; 800 801 object = __alloc_object(gfp); 802 if (!object) 803 return; 804 805 raw_spin_lock_irqsave(&kmemleak_lock, flags); 806 ret = __link_object(object, ptr, size, min_count, objflags); 807 raw_spin_unlock_irqrestore(&kmemleak_lock, flags); 808 if (ret) 809 mem_pool_free(object); 810 } 811 812 /* Create kmemleak object which allocated with virtual address. */ 813 static void create_object(unsigned long ptr, size_t size, 814 int min_count, gfp_t gfp) 815 { 816 __create_object(ptr, size, min_count, gfp, 0); 817 } 818 819 /* Create kmemleak object which allocated with physical address. */ 820 static void create_object_phys(unsigned long ptr, size_t size, 821 int min_count, gfp_t gfp) 822 { 823 __create_object(ptr, size, min_count, gfp, OBJECT_PHYS); 824 } 825 826 /* Create kmemleak object corresponding to a per-CPU allocation. */ 827 static void create_object_percpu(unsigned long ptr, size_t size, 828 int min_count, gfp_t gfp) 829 { 830 __create_object(ptr, size, min_count, gfp, OBJECT_PERCPU); 831 } 832 833 /* 834 * Mark the object as not allocated and schedule RCU freeing via put_object(). 835 */ 836 static void __delete_object(struct kmemleak_object *object) 837 { 838 unsigned long flags; 839 840 WARN_ON(!(object->flags & OBJECT_ALLOCATED)); 841 WARN_ON(atomic_read(&object->use_count) < 1); 842 843 /* 844 * Locking here also ensures that the corresponding memory block 845 * cannot be freed when it is being scanned. 846 */ 847 raw_spin_lock_irqsave(&object->lock, flags); 848 object->flags &= ~OBJECT_ALLOCATED; 849 raw_spin_unlock_irqrestore(&object->lock, flags); 850 put_object(object); 851 } 852 853 /* 854 * Look up the metadata (struct kmemleak_object) corresponding to ptr and 855 * delete it. 856 */ 857 static void delete_object_full(unsigned long ptr, unsigned int objflags) 858 { 859 struct kmemleak_object *object; 860 861 object = find_and_remove_object(ptr, 0, objflags); 862 if (!object) 863 /* 864 * kmalloc_nolock() -> kfree() calls kmemleak_free() 865 * without kmemleak_alloc(). 866 */ 867 return; 868 __delete_object(object); 869 } 870 871 /* 872 * Look up the metadata (struct kmemleak_object) corresponding to ptr and 873 * delete it. If the memory block is partially freed, the function may create 874 * additional metadata for the remaining parts of the block. 875 */ 876 static void delete_object_part(unsigned long ptr, size_t size, 877 unsigned int objflags) 878 { 879 struct kmemleak_object *object, *object_l, *object_r; 880 unsigned long start, end, flags; 881 882 object_l = __alloc_object(GFP_KERNEL); 883 if (!object_l) 884 return; 885 886 object_r = __alloc_object(GFP_KERNEL); 887 if (!object_r) 888 goto out; 889 890 raw_spin_lock_irqsave(&kmemleak_lock, flags); 891 object = __find_and_remove_object(ptr, 1, objflags); 892 if (!object) 893 goto unlock; 894 895 /* 896 * Create one or two objects that may result from the memory block 897 * split. Note that partial freeing is only done by free_bootmem() and 898 * this happens before kmemleak_init() is called. 899 */ 900 start = object->pointer; 901 end = object->pointer + object->size; 902 if ((ptr > start) && 903 !__link_object(object_l, start, ptr - start, 904 object->min_count, objflags)) 905 object_l = NULL; 906 if ((ptr + size < end) && 907 !__link_object(object_r, ptr + size, end - ptr - size, 908 object->min_count, objflags)) 909 object_r = NULL; 910 911 unlock: 912 raw_spin_unlock_irqrestore(&kmemleak_lock, flags); 913 if (object) { 914 __delete_object(object); 915 } else { 916 #ifdef DEBUG 917 kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n", 918 ptr, size); 919 #endif 920 } 921 922 out: 923 if (object_l) 924 mem_pool_free(object_l); 925 if (object_r) 926 mem_pool_free(object_r); 927 } 928 929 static void __paint_it(struct kmemleak_object *object, int color) 930 { 931 object->min_count = color; 932 if (color == KMEMLEAK_BLACK) 933 object->flags |= OBJECT_NO_SCAN; 934 } 935 936 static void paint_it(struct kmemleak_object *object, int color) 937 { 938 unsigned long flags; 939 940 raw_spin_lock_irqsave(&object->lock, flags); 941 __paint_it(object, color); 942 raw_spin_unlock_irqrestore(&object->lock, flags); 943 } 944 945 static void paint_ptr(unsigned long ptr, int color, unsigned int objflags) 946 { 947 struct kmemleak_object *object; 948 949 object = __find_and_get_object(ptr, 0, objflags); 950 if (!object) 951 /* 952 * kmalloc_nolock() -> kfree_rcu() calls kmemleak_ignore() 953 * without kmemleak_alloc(). 954 */ 955 return; 956 paint_it(object, color); 957 put_object(object); 958 } 959 960 /* 961 * Mark an object permanently as gray-colored so that it can no longer be 962 * reported as a leak. This is used in general to mark a false positive. 963 */ 964 static void make_gray_object(unsigned long ptr) 965 { 966 paint_ptr(ptr, KMEMLEAK_GREY, 0); 967 } 968 969 /* 970 * Mark the object as black-colored so that it is ignored from scans and 971 * reporting. 972 */ 973 static void make_black_object(unsigned long ptr, unsigned int objflags) 974 { 975 paint_ptr(ptr, KMEMLEAK_BLACK, objflags); 976 } 977 978 /* 979 * Reset the checksum of an object. The immediate effect is that it will not 980 * be reported as a leak during the next scan until its checksum is updated. 981 */ 982 static void reset_checksum(unsigned long ptr) 983 { 984 unsigned long flags; 985 struct kmemleak_object *object; 986 987 object = find_and_get_object(ptr, 0); 988 if (!object) { 989 kmemleak_warn("Not resetting the checksum of an unknown object at 0x%08lx\n", 990 ptr); 991 return; 992 } 993 994 raw_spin_lock_irqsave(&object->lock, flags); 995 object->checksum = ~0; 996 raw_spin_unlock_irqrestore(&object->lock, flags); 997 put_object(object); 998 } 999 1000 /* 1001 * Add a scanning area to the object. If at least one such area is added, 1002 * kmemleak will only scan these ranges rather than the whole memory block. 1003 */ 1004 static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp) 1005 { 1006 unsigned long flags; 1007 struct kmemleak_object *object; 1008 struct kmemleak_scan_area *area = NULL; 1009 unsigned long untagged_ptr; 1010 unsigned long untagged_objp; 1011 1012 object = find_and_get_object(ptr, 1); 1013 if (!object) { 1014 kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n", 1015 ptr); 1016 return; 1017 } 1018 1019 untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); 1020 untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer); 1021 1022 if (scan_area_cache) 1023 area = kmem_cache_alloc_noprof(scan_area_cache, 1024 gfp_nested_mask(gfp)); 1025 1026 raw_spin_lock_irqsave(&object->lock, flags); 1027 if (!area) { 1028 pr_warn_once("Cannot allocate a scan area, scanning the full object\n"); 1029 /* mark the object for full scan to avoid false positives */ 1030 object->flags |= OBJECT_FULL_SCAN; 1031 goto out_unlock; 1032 } 1033 if (size == SIZE_MAX) { 1034 size = untagged_objp + object->size - untagged_ptr; 1035 } else if (untagged_ptr + size > untagged_objp + object->size) { 1036 kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr); 1037 dump_object_info(object); 1038 kmem_cache_free(scan_area_cache, area); 1039 goto out_unlock; 1040 } 1041 1042 INIT_HLIST_NODE(&area->node); 1043 area->start = ptr; 1044 area->size = size; 1045 1046 hlist_add_head(&area->node, &object->area_list); 1047 out_unlock: 1048 raw_spin_unlock_irqrestore(&object->lock, flags); 1049 put_object(object); 1050 } 1051 1052 /* 1053 * Any surplus references (object already gray) to 'ptr' are passed to 1054 * 'excess_ref'. This is used in the vmalloc() case where a pointer to 1055 * vm_struct may be used as an alternative reference to the vmalloc'ed object 1056 * (see free_thread_stack()). 1057 */ 1058 static void object_set_excess_ref(unsigned long ptr, unsigned long excess_ref) 1059 { 1060 unsigned long flags; 1061 struct kmemleak_object *object; 1062 1063 object = find_and_get_object(ptr, 0); 1064 if (!object) { 1065 kmemleak_warn("Setting excess_ref on unknown object at 0x%08lx\n", 1066 ptr); 1067 return; 1068 } 1069 1070 raw_spin_lock_irqsave(&object->lock, flags); 1071 object->excess_ref = excess_ref; 1072 raw_spin_unlock_irqrestore(&object->lock, flags); 1073 put_object(object); 1074 } 1075 1076 /* 1077 * Set the OBJECT_NO_SCAN flag for the object corresponding to the given 1078 * pointer. Such object will not be scanned by kmemleak but references to it 1079 * are searched. 1080 */ 1081 static void object_no_scan(unsigned long ptr) 1082 { 1083 unsigned long flags; 1084 struct kmemleak_object *object; 1085 1086 object = find_and_get_object(ptr, 0); 1087 if (!object) { 1088 kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr); 1089 return; 1090 } 1091 1092 raw_spin_lock_irqsave(&object->lock, flags); 1093 object->flags |= OBJECT_NO_SCAN; 1094 raw_spin_unlock_irqrestore(&object->lock, flags); 1095 put_object(object); 1096 } 1097 1098 /** 1099 * kmemleak_alloc - register a newly allocated object 1100 * @ptr: pointer to beginning of the object 1101 * @size: size of the object 1102 * @min_count: minimum number of references to this object. If during memory 1103 * scanning a number of references less than @min_count is found, 1104 * the object is reported as a memory leak. If @min_count is 0, 1105 * the object is never reported as a leak. If @min_count is -1, 1106 * the object is ignored (not scanned and not reported as a leak) 1107 * @gfp: kmalloc() flags used for kmemleak internal memory allocations 1108 * 1109 * This function is called from the kernel allocators when a new object 1110 * (memory block) is allocated (kmem_cache_alloc, kmalloc etc.). 1111 */ 1112 void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count, 1113 gfp_t gfp) 1114 { 1115 pr_debug("%s(0x%px, %zu, %d)\n", __func__, ptr, size, min_count); 1116 1117 if (kmemleak_enabled && ptr && !IS_ERR(ptr)) 1118 create_object((unsigned long)ptr, size, min_count, gfp); 1119 } 1120 EXPORT_SYMBOL_GPL(kmemleak_alloc); 1121 1122 /** 1123 * kmemleak_alloc_percpu - register a newly allocated __percpu object 1124 * @ptr: __percpu pointer to beginning of the object 1125 * @size: size of the object 1126 * @gfp: flags used for kmemleak internal memory allocations 1127 * 1128 * This function is called from the kernel percpu allocator when a new object 1129 * (memory block) is allocated (alloc_percpu). 1130 */ 1131 void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size, 1132 gfp_t gfp) 1133 { 1134 pr_debug("%s(0x%px, %zu)\n", __func__, ptr, size); 1135 1136 if (kmemleak_enabled && ptr && !IS_ERR_PCPU(ptr)) 1137 create_object_percpu((__force unsigned long)ptr, size, 1, gfp); 1138 } 1139 EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu); 1140 1141 /** 1142 * kmemleak_vmalloc - register a newly vmalloc'ed object 1143 * @area: pointer to vm_struct 1144 * @size: size of the object 1145 * @gfp: __vmalloc() flags used for kmemleak internal memory allocations 1146 * 1147 * This function is called from the vmalloc() kernel allocator when a new 1148 * object (memory block) is allocated. 1149 */ 1150 void __ref kmemleak_vmalloc(const struct vm_struct *area, size_t size, gfp_t gfp) 1151 { 1152 pr_debug("%s(0x%px, %zu)\n", __func__, area, size); 1153 1154 /* 1155 * A min_count = 2 is needed because vm_struct contains a reference to 1156 * the virtual address of the vmalloc'ed block. 1157 */ 1158 if (kmemleak_enabled) { 1159 create_object((unsigned long)area->addr, size, 2, gfp); 1160 object_set_excess_ref((unsigned long)area, 1161 (unsigned long)area->addr); 1162 } 1163 } 1164 EXPORT_SYMBOL_GPL(kmemleak_vmalloc); 1165 1166 /** 1167 * kmemleak_free - unregister a previously registered object 1168 * @ptr: pointer to beginning of the object 1169 * 1170 * This function is called from the kernel allocators when an object (memory 1171 * block) is freed (kmem_cache_free, kfree, vfree etc.). 1172 */ 1173 void __ref kmemleak_free(const void *ptr) 1174 { 1175 pr_debug("%s(0x%px)\n", __func__, ptr); 1176 1177 if (kmemleak_free_enabled && ptr && !IS_ERR(ptr)) 1178 delete_object_full((unsigned long)ptr, 0); 1179 } 1180 EXPORT_SYMBOL_GPL(kmemleak_free); 1181 1182 /** 1183 * kmemleak_free_part - partially unregister a previously registered object 1184 * @ptr: pointer to the beginning or inside the object. This also 1185 * represents the start of the range to be freed 1186 * @size: size to be unregistered 1187 * 1188 * This function is called when only a part of a memory block is freed 1189 * (usually from the bootmem allocator). 1190 */ 1191 void __ref kmemleak_free_part(const void *ptr, size_t size) 1192 { 1193 pr_debug("%s(0x%px)\n", __func__, ptr); 1194 1195 if (kmemleak_enabled && ptr && !IS_ERR(ptr)) 1196 delete_object_part((unsigned long)ptr, size, 0); 1197 } 1198 EXPORT_SYMBOL_GPL(kmemleak_free_part); 1199 1200 /** 1201 * kmemleak_free_percpu - unregister a previously registered __percpu object 1202 * @ptr: __percpu pointer to beginning of the object 1203 * 1204 * This function is called from the kernel percpu allocator when an object 1205 * (memory block) is freed (free_percpu). 1206 */ 1207 void __ref kmemleak_free_percpu(const void __percpu *ptr) 1208 { 1209 pr_debug("%s(0x%px)\n", __func__, ptr); 1210 1211 if (kmemleak_free_enabled && ptr && !IS_ERR_PCPU(ptr)) 1212 delete_object_full((__force unsigned long)ptr, OBJECT_PERCPU); 1213 } 1214 EXPORT_SYMBOL_GPL(kmemleak_free_percpu); 1215 1216 /** 1217 * kmemleak_update_trace - update object allocation stack trace 1218 * @ptr: pointer to beginning of the object 1219 * 1220 * Override the object allocation stack trace for cases where the actual 1221 * allocation place is not always useful. 1222 */ 1223 void __ref kmemleak_update_trace(const void *ptr) 1224 { 1225 struct kmemleak_object *object; 1226 depot_stack_handle_t trace_handle; 1227 unsigned long flags; 1228 1229 pr_debug("%s(0x%px)\n", __func__, ptr); 1230 1231 if (!kmemleak_enabled || IS_ERR_OR_NULL(ptr)) 1232 return; 1233 1234 object = find_and_get_object((unsigned long)ptr, 1); 1235 if (!object) { 1236 #ifdef DEBUG 1237 kmemleak_warn("Updating stack trace for unknown object at %p\n", 1238 ptr); 1239 #endif 1240 return; 1241 } 1242 1243 trace_handle = set_track_prepare(); 1244 raw_spin_lock_irqsave(&object->lock, flags); 1245 object->trace_handle = trace_handle; 1246 raw_spin_unlock_irqrestore(&object->lock, flags); 1247 1248 put_object(object); 1249 } 1250 EXPORT_SYMBOL(kmemleak_update_trace); 1251 1252 /** 1253 * kmemleak_not_leak - mark an allocated object as false positive 1254 * @ptr: pointer to beginning of the object 1255 * 1256 * Calling this function on an object will cause the memory block to no longer 1257 * be reported as leak and always be scanned. 1258 */ 1259 void __ref kmemleak_not_leak(const void *ptr) 1260 { 1261 pr_debug("%s(0x%px)\n", __func__, ptr); 1262 1263 if (kmemleak_enabled && ptr && !IS_ERR(ptr)) 1264 make_gray_object((unsigned long)ptr); 1265 } 1266 EXPORT_SYMBOL(kmemleak_not_leak); 1267 1268 /** 1269 * kmemleak_transient_leak - mark an allocated object as transient false positive 1270 * @ptr: pointer to beginning of the object 1271 * 1272 * Calling this function on an object will cause the memory block to not be 1273 * reported as a leak temporarily. This may happen, for example, if the object 1274 * is part of a singly linked list and the ->next reference to it is changed. 1275 */ 1276 void __ref kmemleak_transient_leak(const void *ptr) 1277 { 1278 pr_debug("%s(0x%px)\n", __func__, ptr); 1279 1280 if (kmemleak_enabled && ptr && !IS_ERR(ptr)) 1281 reset_checksum((unsigned long)ptr); 1282 } 1283 EXPORT_SYMBOL(kmemleak_transient_leak); 1284 1285 /** 1286 * kmemleak_ignore_percpu - similar to kmemleak_ignore but taking a percpu 1287 * address argument 1288 * @ptr: percpu address of the object 1289 */ 1290 void __ref kmemleak_ignore_percpu(const void __percpu *ptr) 1291 { 1292 pr_debug("%s(0x%px)\n", __func__, ptr); 1293 1294 if (kmemleak_enabled && ptr && !IS_ERR_PCPU(ptr)) 1295 make_black_object((unsigned long)ptr, OBJECT_PERCPU); 1296 } 1297 EXPORT_SYMBOL_GPL(kmemleak_ignore_percpu); 1298 1299 /** 1300 * kmemleak_ignore - ignore an allocated object 1301 * @ptr: pointer to beginning of the object 1302 * 1303 * Calling this function on an object will cause the memory block to be 1304 * ignored (not scanned and not reported as a leak). This is usually done when 1305 * it is known that the corresponding block is not a leak and does not contain 1306 * any references to other allocated memory blocks. 1307 */ 1308 void __ref kmemleak_ignore(const void *ptr) 1309 { 1310 pr_debug("%s(0x%px)\n", __func__, ptr); 1311 1312 if (kmemleak_enabled && ptr && !IS_ERR(ptr)) 1313 make_black_object((unsigned long)ptr, 0); 1314 } 1315 EXPORT_SYMBOL(kmemleak_ignore); 1316 1317 /** 1318 * kmemleak_scan_area - limit the range to be scanned in an allocated object 1319 * @ptr: pointer to beginning or inside the object. This also 1320 * represents the start of the scan area 1321 * @size: size of the scan area 1322 * @gfp: kmalloc() flags used for kmemleak internal memory allocations 1323 * 1324 * This function is used when it is known that only certain parts of an object 1325 * contain references to other objects. Kmemleak will only scan these areas 1326 * reducing the number false negatives. 1327 */ 1328 void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp) 1329 { 1330 pr_debug("%s(0x%px)\n", __func__, ptr); 1331 1332 if (kmemleak_enabled && ptr && size && !IS_ERR(ptr)) 1333 add_scan_area((unsigned long)ptr, size, gfp); 1334 } 1335 EXPORT_SYMBOL(kmemleak_scan_area); 1336 1337 /** 1338 * kmemleak_no_scan - do not scan an allocated object 1339 * @ptr: pointer to beginning of the object 1340 * 1341 * This function notifies kmemleak not to scan the given memory block. Useful 1342 * in situations where it is known that the given object does not contain any 1343 * references to other objects. Kmemleak will not scan such objects reducing 1344 * the number of false negatives. 1345 */ 1346 void __ref kmemleak_no_scan(const void *ptr) 1347 { 1348 pr_debug("%s(0x%px)\n", __func__, ptr); 1349 1350 if (kmemleak_enabled && ptr && !IS_ERR(ptr)) 1351 object_no_scan((unsigned long)ptr); 1352 } 1353 EXPORT_SYMBOL(kmemleak_no_scan); 1354 1355 /** 1356 * kmemleak_alloc_phys - similar to kmemleak_alloc but taking a physical 1357 * address argument 1358 * @phys: physical address of the object 1359 * @size: size of the object 1360 * @gfp: kmalloc() flags used for kmemleak internal memory allocations 1361 */ 1362 void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp) 1363 { 1364 pr_debug("%s(0x%px, %zu)\n", __func__, &phys, size); 1365 1366 if (kmemleak_enabled) 1367 /* 1368 * Create object with OBJECT_PHYS flag and 1369 * assume min_count 0. 1370 */ 1371 create_object_phys((unsigned long)phys, size, 0, gfp); 1372 } 1373 EXPORT_SYMBOL(kmemleak_alloc_phys); 1374 1375 /** 1376 * kmemleak_free_part_phys - similar to kmemleak_free_part but taking a 1377 * physical address argument 1378 * @phys: physical address if the beginning or inside an object. This 1379 * also represents the start of the range to be freed 1380 * @size: size to be unregistered 1381 */ 1382 void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size) 1383 { 1384 pr_debug("%s(0x%px)\n", __func__, &phys); 1385 1386 if (kmemleak_enabled) 1387 delete_object_part((unsigned long)phys, size, OBJECT_PHYS); 1388 } 1389 EXPORT_SYMBOL(kmemleak_free_part_phys); 1390 1391 /** 1392 * kmemleak_ignore_phys - similar to kmemleak_ignore but taking a physical 1393 * address argument 1394 * @phys: physical address of the object 1395 */ 1396 void __ref kmemleak_ignore_phys(phys_addr_t phys) 1397 { 1398 pr_debug("%s(0x%px)\n", __func__, &phys); 1399 1400 if (kmemleak_enabled) 1401 make_black_object((unsigned long)phys, OBJECT_PHYS); 1402 } 1403 EXPORT_SYMBOL(kmemleak_ignore_phys); 1404 1405 /* 1406 * Update an object's checksum and return true if it was modified. 1407 */ 1408 static bool update_checksum(struct kmemleak_object *object) 1409 { 1410 u32 old_csum = object->checksum; 1411 1412 if (WARN_ON_ONCE(object->flags & OBJECT_PHYS)) 1413 return false; 1414 1415 kasan_disable_current(); 1416 kcsan_disable_current(); 1417 if (object->flags & OBJECT_PERCPU) { 1418 unsigned int cpu; 1419 1420 object->checksum = 0; 1421 for_each_possible_cpu(cpu) { 1422 void *ptr = per_cpu_ptr((void __percpu *)object->pointer, cpu); 1423 1424 object->checksum = crc32(object->checksum, 1425 kasan_reset_tag((void *)ptr), object->size); 1426 } 1427 } else { 1428 object->checksum = crc32(0, kasan_reset_tag((void *)object->pointer), object->size); 1429 } 1430 kasan_enable_current(); 1431 kcsan_enable_current(); 1432 1433 return object->checksum != old_csum; 1434 } 1435 1436 /* 1437 * Update an object's references. object->lock must be held by the caller. 1438 */ 1439 static void update_refs(struct kmemleak_object *object) 1440 { 1441 if (!color_white(object)) { 1442 /* non-orphan, ignored or new */ 1443 return; 1444 } 1445 1446 /* 1447 * Increase the object's reference count (number of pointers to the 1448 * memory block). If this count reaches the required minimum, the 1449 * object's color will become gray and it will be added to the 1450 * gray_list. 1451 */ 1452 object->count++; 1453 if (color_gray(object)) { 1454 /* referenced after all, no longer a suspect */ 1455 if (object->flags & OBJECT_SUSPECT) { 1456 object->flags &= ~OBJECT_SUSPECT; 1457 nr_suspects--; 1458 } 1459 /* put_object() called when removing from gray_list */ 1460 WARN_ON(!get_object(object)); 1461 list_add_tail(&object->gray_list, &gray_list); 1462 } 1463 } 1464 1465 static void pointer_update_refs(struct kmemleak_object *scanned, 1466 unsigned long pointer, unsigned int objflags) 1467 { 1468 struct kmemleak_object *object; 1469 unsigned long untagged_ptr; 1470 unsigned long excess_ref; 1471 1472 untagged_ptr = (unsigned long)kasan_reset_tag((void *)pointer); 1473 if (objflags & OBJECT_PERCPU) { 1474 if (untagged_ptr < min_percpu_addr || untagged_ptr >= max_percpu_addr) 1475 return; 1476 } else { 1477 if (untagged_ptr < min_addr || untagged_ptr >= max_addr) 1478 return; 1479 } 1480 1481 /* 1482 * No need for get_object() here since we hold kmemleak_lock. 1483 * object->use_count cannot be dropped to 0 while the object 1484 * is still present in object_tree_root and object_list 1485 * (with updates protected by kmemleak_lock). 1486 */ 1487 object = __lookup_object(pointer, 1, objflags); 1488 if (!object) 1489 return; 1490 if (object == scanned) 1491 /* self referenced, ignore */ 1492 return; 1493 1494 /* 1495 * Avoid the lockdep recursive warning on object->lock being 1496 * previously acquired in scan_object(). These locks are 1497 * enclosed by scan_mutex. 1498 */ 1499 raw_spin_lock_nested(&object->lock, SINGLE_DEPTH_NESTING); 1500 /* only pass surplus references (object already gray) */ 1501 if (color_gray(object)) { 1502 excess_ref = object->excess_ref; 1503 /* no need for update_refs() if object already gray */ 1504 } else { 1505 excess_ref = 0; 1506 update_refs(object); 1507 } 1508 raw_spin_unlock(&object->lock); 1509 1510 if (excess_ref) { 1511 object = lookup_object(excess_ref, 0); 1512 if (!object) 1513 return; 1514 if (object == scanned) 1515 /* circular reference, ignore */ 1516 return; 1517 raw_spin_lock_nested(&object->lock, SINGLE_DEPTH_NESTING); 1518 update_refs(object); 1519 raw_spin_unlock(&object->lock); 1520 } 1521 } 1522 1523 /* 1524 * Memory scanning is a long process and it needs to be interruptible. This 1525 * function checks whether such interrupt condition occurred. 1526 */ 1527 static int scan_should_stop(void) 1528 { 1529 if (!kmemleak_enabled) 1530 return 1; 1531 1532 /* 1533 * This function may be called from either process or kthread context, 1534 * hence the need to check for both stop conditions. 1535 */ 1536 if (current->flags & PF_KTHREAD) 1537 return kthread_should_stop(); 1538 1539 return signal_pending(current); 1540 } 1541 1542 /* 1543 * Scan a memory block (exclusive range) for valid pointers and add those 1544 * found to the gray list. Return non-zero if the scan was interrupted. 1545 */ 1546 static int scan_block(void *_start, void *_end, 1547 struct kmemleak_object *scanned) 1548 { 1549 unsigned long *ptr; 1550 unsigned long *start = PTR_ALIGN(_start, BYTES_PER_POINTER); 1551 unsigned long *end = _end - (BYTES_PER_POINTER - 1); 1552 unsigned long flags; 1553 int stop = 0; 1554 1555 raw_spin_lock_irqsave(&kmemleak_lock, flags); 1556 for (ptr = start; ptr < end; ptr++) { 1557 unsigned long pointer; 1558 1559 if (scan_should_stop()) { 1560 stop = 1; 1561 break; 1562 } 1563 1564 kasan_disable_current(); 1565 pointer = *(unsigned long *)kasan_reset_tag((void *)ptr); 1566 kasan_enable_current(); 1567 1568 pointer_update_refs(scanned, pointer, 0); 1569 pointer_update_refs(scanned, pointer, OBJECT_PERCPU); 1570 } 1571 raw_spin_unlock_irqrestore(&kmemleak_lock, flags); 1572 1573 return stop; 1574 } 1575 1576 /* 1577 * Scan a large memory block in MAX_SCAN_SIZE chunks to reduce the latency. 1578 * Return non-zero if the scan was interrupted. 1579 */ 1580 #ifdef CONFIG_SMP 1581 static int scan_large_block(void *start, void *end) 1582 { 1583 void *next; 1584 1585 while (start < end) { 1586 next = min(start + MAX_SCAN_SIZE, end); 1587 if (scan_block(start, next, NULL)) 1588 return 1; 1589 start = next; 1590 cond_resched_tasks_rcu_qs(); 1591 } 1592 1593 return 0; 1594 } 1595 #endif 1596 1597 /* 1598 * Scan a memory block corresponding to a kmemleak_object. A condition is 1599 * that object->use_count >= 1. 1600 */ 1601 static void scan_object(struct kmemleak_object *object) 1602 { 1603 struct kmemleak_scan_area *area; 1604 unsigned long flags; 1605 1606 /* 1607 * Once the object->lock is acquired, the corresponding memory block 1608 * cannot be freed (the same lock is acquired in delete_object). 1609 */ 1610 raw_spin_lock_irqsave(&object->lock, flags); 1611 if (object->flags & OBJECT_NO_SCAN) 1612 goto out; 1613 if (!(object->flags & OBJECT_ALLOCATED)) 1614 /* already freed object */ 1615 goto out; 1616 1617 if (object->flags & OBJECT_PERCPU) { 1618 unsigned int cpu; 1619 1620 for_each_possible_cpu(cpu) { 1621 void *start = per_cpu_ptr((void __percpu *)object->pointer, cpu); 1622 void *end = start + object->size; 1623 1624 scan_block(start, end, object); 1625 1626 raw_spin_unlock_irqrestore(&object->lock, flags); 1627 cond_resched_tasks_rcu_qs(); 1628 raw_spin_lock_irqsave(&object->lock, flags); 1629 if (!(object->flags & OBJECT_ALLOCATED)) 1630 break; 1631 } 1632 } else if (hlist_empty(&object->area_list) || 1633 object->flags & OBJECT_FULL_SCAN) { 1634 void *start = object->flags & OBJECT_PHYS ? 1635 __va((phys_addr_t)object->pointer) : 1636 (void *)object->pointer; 1637 void *end = start + object->size; 1638 void *next; 1639 1640 do { 1641 next = min(start + MAX_SCAN_SIZE, end); 1642 scan_block(start, next, object); 1643 1644 start = next; 1645 if (start >= end) 1646 break; 1647 1648 raw_spin_unlock_irqrestore(&object->lock, flags); 1649 cond_resched_tasks_rcu_qs(); 1650 raw_spin_lock_irqsave(&object->lock, flags); 1651 } while (object->flags & OBJECT_ALLOCATED); 1652 } else { 1653 hlist_for_each_entry(area, &object->area_list, node) 1654 scan_block((void *)area->start, 1655 (void *)(area->start + area->size), 1656 object); 1657 } 1658 out: 1659 raw_spin_unlock_irqrestore(&object->lock, flags); 1660 } 1661 1662 /* 1663 * Scan the objects already referenced (gray objects). More objects will be 1664 * referenced and, if there are no memory leaks, all the objects are scanned. 1665 */ 1666 static void scan_gray_list(void) 1667 { 1668 struct kmemleak_object *object, *tmp; 1669 1670 /* 1671 * The list traversal is safe for both tail additions and removals 1672 * from inside the loop. The kmemleak objects cannot be freed from 1673 * outside the loop because their use_count was incremented. 1674 */ 1675 object = list_entry(gray_list.next, typeof(*object), gray_list); 1676 while (&object->gray_list != &gray_list) { 1677 cond_resched_tasks_rcu_qs(); 1678 1679 /* may add new objects to the list */ 1680 if (!scan_should_stop()) 1681 scan_object(object); 1682 1683 tmp = list_entry(object->gray_list.next, typeof(*object), 1684 gray_list); 1685 1686 /* remove the object from the list and release it */ 1687 list_del(&object->gray_list); 1688 put_object(object); 1689 1690 object = tmp; 1691 } 1692 WARN_ON(!list_empty(&gray_list)); 1693 } 1694 1695 /* 1696 * Conditionally call resched() in an object iteration loop while making sure 1697 * that the given object won't go away without RCU read lock by performing a 1698 * get_object() if necessaary. 1699 */ 1700 static void kmemleak_cond_resched(struct kmemleak_object *object) 1701 { 1702 if (!get_object(object)) 1703 return; /* Try next object */ 1704 1705 raw_spin_lock_irq(&kmemleak_lock); 1706 if (object->del_state & DELSTATE_REMOVED) 1707 goto unlock_put; /* Object removed */ 1708 object->del_state |= DELSTATE_NO_DELETE; 1709 raw_spin_unlock_irq(&kmemleak_lock); 1710 1711 rcu_read_unlock(); 1712 cond_resched_tasks_rcu_qs(); 1713 rcu_read_lock(); 1714 1715 raw_spin_lock_irq(&kmemleak_lock); 1716 if (object->del_state & DELSTATE_REMOVED) 1717 list_del_rcu(&object->object_list); 1718 object->del_state &= ~DELSTATE_NO_DELETE; 1719 unlock_put: 1720 raw_spin_unlock_irq(&kmemleak_lock); 1721 put_object(object); 1722 } 1723 1724 /* 1725 * Scan all task kernel stacks, rescheduling between tasks. Each task is looked 1726 * up and pinned within its own RCU read-side section, so no lock is held across 1727 * the scan and the walk cannot trip the soft lockup watchdog. 1728 */ 1729 static void kmemleak_scan_task_stacks(void) 1730 { 1731 struct pid *pid; 1732 int nr = 1; 1733 int stop = 0; 1734 1735 do { 1736 struct task_struct *p = NULL; 1737 1738 rcu_read_lock(); 1739 pid = find_ge_pid(nr, &init_pid_ns); 1740 if (pid) { 1741 nr = pid_nr(pid) + 1; 1742 p = pid_task(pid, PIDTYPE_PID); 1743 if (p) 1744 get_task_struct(p); 1745 } 1746 rcu_read_unlock(); 1747 1748 if (p) { 1749 void *stack = try_get_task_stack(p); 1750 1751 if (stack) { 1752 stop = scan_block(stack, stack + THREAD_SIZE, NULL); 1753 put_task_stack(p); 1754 } 1755 put_task_struct(p); 1756 } 1757 cond_resched_tasks_rcu_qs(); 1758 } while (pid && !stop); 1759 } 1760 1761 /* 1762 * Print one leak inline. The hex dump is gated on OBJECT_ALLOCATED so it 1763 * does not touch user memory that was freed concurrently; the rest of the 1764 * report (backtrace, comm, pid) is always emitted since the kmemleak_object 1765 * metadata is pinned by the caller. 1766 */ 1767 static void print_leak_locked(struct kmemleak_object *object, bool hex_dump) 1768 { 1769 raw_spin_lock_irq(&object->lock); 1770 __print_unreferenced(NULL, object, 1771 hex_dump && (object->flags & OBJECT_ALLOCATED)); 1772 raw_spin_unlock_irq(&object->lock); 1773 } 1774 1775 /* 1776 * Per-scan dedup table for verbose leak printing. The xarray is keyed by 1777 * stackdepot trace_handle and stores a pointer to the representative 1778 * kmemleak_object. The per-scan repeat count lives in object->dup_count. 1779 * 1780 * dedup_record() must run outside object->lock: xa_store() may take 1781 * mutexes (xa_node slab allocation) which lockdep would flag against the 1782 * raw spinlock object->lock. 1783 */ 1784 static void dedup_record(struct xarray *dedup, struct kmemleak_object *object, 1785 depot_stack_handle_t trace_handle) 1786 { 1787 struct kmemleak_object *rep; 1788 void *old; 1789 1790 /* 1791 * No stack trace to dedup against: early-boot allocation tracked 1792 * before kmemleak_init() set up object_cache, or stack_depot_save() 1793 * failure under memory pressure. 1794 */ 1795 if (!trace_handle) { 1796 print_leak_locked(object, true); 1797 return; 1798 } 1799 1800 /* stack is available, now we can de-dup */ 1801 rep = xa_load(dedup, trace_handle); 1802 if (rep) { 1803 rep->dup_count++; 1804 return; 1805 } 1806 1807 /* 1808 * Object is being torn down (use_count already hit zero); the 1809 * tracked memory at object->pointer is unsafe to read, so skip. 1810 */ 1811 if (!get_object(object)) 1812 return; 1813 1814 object->dup_count = 1; 1815 old = xa_store(dedup, trace_handle, object, GFP_ATOMIC); 1816 if (xa_is_err(old)) { 1817 /* xa_node allocation failed; fall back to inline print. */ 1818 print_leak_locked(object, true); 1819 put_object(object); 1820 return; 1821 } 1822 /* 1823 * scan_mutex serialises all writers to the dedup xarray, so xa_store() 1824 * after a NULL xa_load() must always overwrite an empty slot. 1825 */ 1826 WARN_ON_ONCE(old); 1827 } 1828 1829 /* 1830 * Drain the dedup table. Re-acquires object->lock and re-checks 1831 * OBJECT_ALLOCATED before printing: while get_object() pins the 1832 * kmemleak_object metadata, the underlying tracked allocation may have 1833 * been freed since the scan walked it (kmemleak_free clears 1834 * OBJECT_ALLOCATED under object->lock before the user memory goes away). 1835 * The hex dump is skipped for coalesced entries since the bytes would 1836 * differ across objects anyway. 1837 */ 1838 static void dedup_flush(struct xarray *dedup) 1839 { 1840 struct kmemleak_object *object; 1841 unsigned long idx; 1842 unsigned int dup; 1843 bool coalesced; 1844 1845 xa_for_each(dedup, idx, object) { 1846 dup = object->dup_count; 1847 coalesced = dup > 1; 1848 1849 print_leak_locked(object, !coalesced); 1850 if (coalesced) 1851 pr_warn(" ... and %u more object(s) with the same backtrace\n", 1852 dup - 1); 1853 put_object(object); 1854 xa_erase(dedup, idx); 1855 } 1856 } 1857 1858 /* 1859 * Scan the struct pages of a zone, skipping memory holes, pages that belong to 1860 * another zone and pages that are not in use. Returns 1 if the scan should be 1861 * stopped. 1862 */ 1863 static int scan_zone_pages(struct zone *zone) 1864 { 1865 const unsigned int max_batch = MAX_SCAN_SIZE / sizeof(struct page); 1866 unsigned long start_pfn = zone->zone_start_pfn; 1867 unsigned long end_pfn = zone_end_pfn(zone); 1868 struct page *first = NULL, *last = NULL; 1869 unsigned int batch = 0; 1870 unsigned long pfn; 1871 1872 for (pfn = start_pfn; pfn < end_pfn; pfn++) { 1873 struct page *page = pfn_to_online_page(pfn); 1874 1875 if (!(pfn & 63)) 1876 cond_resched_tasks_rcu_qs(); 1877 1878 /* only scan in-use pages belonging to this zone */ 1879 if (page && (page_zone(page) != zone || 1880 page_count(page) == 0)) 1881 page = NULL; 1882 1883 if (page && first && page == last + 1 && 1884 batch < max_batch) { 1885 last = page; 1886 batch++; 1887 continue; 1888 } 1889 1890 if (first && scan_block(first, last + 1, NULL)) 1891 return 1; 1892 1893 first = page; 1894 last = page; 1895 batch = page ? 1 : 0; 1896 } 1897 1898 if (first && scan_block(first, last + 1, NULL)) 1899 return 1; 1900 1901 return 0; 1902 } 1903 1904 /* 1905 * Scan data sections and all the referenced memory blocks allocated via the 1906 * kernel's standard allocators. This function must be called with the 1907 * scan_mutex held. 1908 */ 1909 static int __kmemleak_scan(bool full) 1910 { 1911 struct kmemleak_object *object; 1912 struct zone *zone; 1913 int __maybe_unused i; 1914 int stop = 0; 1915 1916 jiffies_last_scan = jiffies; 1917 if (full) 1918 nr_suspects = 0; 1919 1920 /* prepare the kmemleak_object's */ 1921 rcu_read_lock(); 1922 list_for_each_entry_rcu(object, &object_list, object_list) { 1923 raw_spin_lock_irq(&object->lock); 1924 #ifdef DEBUG 1925 /* 1926 * With a few exceptions there should be a maximum of 1927 * 1 reference to any object at this point. 1928 */ 1929 if (atomic_read(&object->use_count) > 1) { 1930 pr_debug("object->use_count = %d\n", 1931 atomic_read(&object->use_count)); 1932 dump_object_info(object); 1933 } 1934 #endif 1935 1936 /* ignore objects outside lowmem (paint them black) */ 1937 if ((object->flags & OBJECT_PHYS) && 1938 !(object->flags & OBJECT_NO_SCAN)) { 1939 unsigned long phys = object->pointer; 1940 1941 if (PHYS_PFN(phys) < min_low_pfn || 1942 PHYS_PFN(phys + object->size) > max_low_pfn) 1943 __paint_it(object, KMEMLEAK_BLACK); 1944 } 1945 1946 /* referenced last scan: restart the unreferenced run */ 1947 if (!color_white(object)) 1948 object->unref_scans = 0; 1949 /* reset the reference count (whiten the object) */ 1950 object->count = 0; 1951 if (full) 1952 object->flags &= ~OBJECT_SUSPECT; 1953 if (color_gray(object) && get_object(object)) 1954 list_add_tail(&object->gray_list, &gray_list); 1955 1956 raw_spin_unlock_irq(&object->lock); 1957 1958 if (need_resched()) 1959 kmemleak_cond_resched(object); 1960 } 1961 rcu_read_unlock(); 1962 1963 #ifdef CONFIG_SMP 1964 /* per-cpu sections scanning */ 1965 for_each_possible_cpu(i) { 1966 if (scan_large_block(__per_cpu_start + per_cpu_offset(i), 1967 __per_cpu_end + per_cpu_offset(i))) 1968 goto scan_gray; 1969 } 1970 #endif 1971 1972 /* 1973 * Struct page scanning for each node. 1974 */ 1975 get_online_mems(); 1976 for_each_populated_zone(zone) { 1977 stop = scan_zone_pages(zone); 1978 if (stop) 1979 break; 1980 } 1981 put_online_mems(); 1982 if (stop) 1983 goto scan_gray; 1984 1985 /* 1986 * Scanning the task stacks (may introduce false negatives). 1987 */ 1988 if (kmemleak_stack_scan) 1989 kmemleak_scan_task_stacks(); 1990 1991 /* 1992 * Scan the objects already referenced from the sections scanned 1993 * above. 1994 */ 1995 scan_gray: 1996 scan_gray_list(); 1997 1998 /* a confirmation scan does not look for modified objects */ 1999 if (!full) 2000 return nr_suspects; 2001 2002 /* 2003 * Check for new or unreferenced objects modified since the previous 2004 * scan and color them gray until the next scan. 2005 */ 2006 rcu_read_lock(); 2007 list_for_each_entry_rcu(object, &object_list, object_list) { 2008 if (need_resched()) 2009 kmemleak_cond_resched(object); 2010 2011 /* 2012 * This is racy but we can save the overhead of lock/unlock 2013 * calls. The missed objects, if any, should be caught in 2014 * the next scan. 2015 */ 2016 if (!color_white(object)) 2017 continue; 2018 raw_spin_lock_irq(&object->lock); 2019 if (color_white(object) && (object->flags & OBJECT_ALLOCATED) 2020 && update_checksum(object) && get_object(object)) { 2021 /* color it gray temporarily */ 2022 object->count = object->min_count; 2023 list_add_tail(&object->gray_list, &gray_list); 2024 } else if (unreferenced_object(object) && 2025 !(object->flags & OBJECT_REPORTED)) { 2026 /* flag the objects left unreferenced by this scan */ 2027 object->flags |= OBJECT_SUSPECT; 2028 nr_suspects++; 2029 } 2030 raw_spin_unlock_irq(&object->lock); 2031 } 2032 rcu_read_unlock(); 2033 2034 /* 2035 * Re-scan the gray list for modified unreferenced objects. 2036 */ 2037 scan_gray_list(); 2038 2039 return nr_suspects; 2040 } 2041 2042 /* 2043 * Promote a suspected object to a reported leak once it has stayed 2044 * unreferenced for min_unref_scans consecutive scans. Called with 2045 * object->lock held; returns true when the object is newly reported. 2046 */ 2047 static bool confirm_leak(struct kmemleak_object *object) 2048 { 2049 if (!unreferenced_object(object) || 2050 !(object->flags & OBJECT_SUSPECT) || 2051 (object->flags & OBJECT_REPORTED)) 2052 return false; 2053 2054 object->unref_scans += 1; 2055 if (object->unref_scans < min_unref_scans) 2056 return false; 2057 2058 object->flags |= OBJECT_REPORTED; 2059 return true; 2060 } 2061 2062 /* 2063 * Scan the memory and report the unreferenced objects as leaks. Must be 2064 * called with the scan_mutex held. 2065 */ 2066 static void kmemleak_scan(void) 2067 { 2068 struct kmemleak_object *object; 2069 struct xarray dedup; 2070 int new_leaks = 0; 2071 2072 /* 2073 * Full scan. Objects left unreferenced are flagged OBJECT_SUSPECT and 2074 * counted in the return value; nothing to confirm or report otherwise. 2075 */ 2076 if (!__kmemleak_scan(true)) 2077 return; 2078 2079 /* 2080 * If scanning was stopped do not report any new unreferenced objects. 2081 */ 2082 if (scan_should_stop()) 2083 return; 2084 2085 /* 2086 * A live object whose only reference is moved by, for example, a 2087 * concurrent RCU update can be missed for one scan and reported as a 2088 * transient false positive. Scan again and only report the objects 2089 * left unreferenced (still flagged OBJECT_SUSPECT) by both scans. 2090 */ 2091 __kmemleak_scan(false); 2092 if (scan_should_stop()) 2093 return; 2094 2095 /* 2096 * Scanning result reporting. When verbose printing is enabled, dedupe 2097 * by stackdepot trace_handle so each unique backtrace is logged once 2098 * per scan, annotated with the number of objects that share it. The 2099 * per-leak count below still reflects every object, and 2100 * /sys/kernel/debug/kmemleak still lists them individually. 2101 */ 2102 xa_init(&dedup); 2103 rcu_read_lock(); 2104 list_for_each_entry_rcu(object, &object_list, object_list) { 2105 depot_stack_handle_t trace_handle; 2106 bool dedup_print; 2107 2108 if (need_resched()) 2109 kmemleak_cond_resched(object); 2110 2111 /* 2112 * This is racy but we can save the overhead of lock/unlock 2113 * calls. The missed objects, if any, should be caught in 2114 * the next scan. 2115 */ 2116 if (!color_white(object)) 2117 continue; 2118 raw_spin_lock_irq(&object->lock); 2119 trace_handle = 0; 2120 dedup_print = false; 2121 2122 if (confirm_leak(object)) { 2123 if (kmemleak_verbose) { 2124 trace_handle = object->trace_handle; 2125 dedup_print = true; 2126 } 2127 new_leaks++; 2128 } 2129 raw_spin_unlock_irq(&object->lock); 2130 2131 /* 2132 * Defer the verbose print outside object->lock: xa_store() 2133 * may take xa_node slab locks at a higher wait-context level 2134 * which lockdep would flag against the raw_spinlock_t 2135 * object->lock. rcu_read_lock() keeps the kmemleak_object 2136 * alive across the call. 2137 */ 2138 if (dedup_print) 2139 dedup_record(&dedup, object, trace_handle); 2140 } 2141 rcu_read_unlock(); 2142 /* Flush'em all */ 2143 dedup_flush(&dedup); 2144 xa_destroy(&dedup); 2145 2146 if (new_leaks) { 2147 kmemleak_found_leaks = true; 2148 2149 pr_info("%d new suspected memory leaks (see /sys/kernel/debug/kmemleak)\n", 2150 new_leaks); 2151 } 2152 2153 } 2154 2155 /* 2156 * Thread function performing automatic memory scanning. Unreferenced objects 2157 * at the end of a memory scan are reported but only the first time. 2158 */ 2159 static int kmemleak_scan_thread(void *arg) 2160 { 2161 static int first_run = IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN); 2162 2163 pr_info("Automatic memory scanning thread started\n"); 2164 set_user_nice(current, 10); 2165 2166 /* 2167 * Wait before the first scan to allow the system to fully initialize. 2168 */ 2169 if (first_run) { 2170 signed long timeout = secs_to_jiffies(SECS_FIRST_SCAN); 2171 first_run = 0; 2172 while (timeout && !kthread_should_stop()) 2173 timeout = schedule_timeout_interruptible(timeout); 2174 } 2175 2176 while (!kthread_should_stop()) { 2177 signed long timeout = READ_ONCE(jiffies_scan_wait); 2178 2179 mutex_lock(&scan_mutex); 2180 kmemleak_scan(); 2181 mutex_unlock(&scan_mutex); 2182 2183 /* wait before the next scan */ 2184 while (timeout && !kthread_should_stop()) 2185 timeout = schedule_timeout_interruptible(timeout); 2186 } 2187 2188 pr_info("Automatic memory scanning thread ended\n"); 2189 2190 return 0; 2191 } 2192 2193 /* 2194 * Start the automatic memory scanning thread. This function must be called 2195 * with the scan_mutex held. 2196 */ 2197 static void start_scan_thread(void) 2198 { 2199 if (scan_thread) 2200 return; 2201 scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak"); 2202 if (IS_ERR(scan_thread)) { 2203 pr_warn("Failed to create the scan thread\n"); 2204 scan_thread = NULL; 2205 } 2206 } 2207 2208 /* 2209 * Stop the automatic memory scanning thread. 2210 */ 2211 static void stop_scan_thread(void) 2212 { 2213 if (scan_thread) { 2214 kthread_stop(scan_thread); 2215 scan_thread = NULL; 2216 } 2217 } 2218 2219 /* 2220 * Iterate over the object_list and return the first valid object at or after 2221 * the required position with its use_count incremented. The function triggers 2222 * a memory scanning when the pos argument points to the first position. 2223 */ 2224 static void *kmemleak_seq_start(struct seq_file *seq, loff_t *pos) 2225 { 2226 struct kmemleak_object *object; 2227 loff_t n = *pos; 2228 int err; 2229 2230 err = mutex_lock_interruptible(&scan_mutex); 2231 if (err < 0) 2232 return ERR_PTR(err); 2233 2234 rcu_read_lock(); 2235 list_for_each_entry_rcu(object, &object_list, object_list) { 2236 if (n-- > 0) 2237 continue; 2238 if (get_object(object)) 2239 goto out; 2240 } 2241 object = NULL; 2242 out: 2243 return object; 2244 } 2245 2246 /* 2247 * Return the next object in the object_list. The function decrements the 2248 * use_count of the previous object and increases that of the next one. 2249 */ 2250 static void *kmemleak_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2251 { 2252 struct kmemleak_object *prev_obj = v; 2253 struct kmemleak_object *next_obj = NULL; 2254 struct kmemleak_object *obj = prev_obj; 2255 2256 ++(*pos); 2257 2258 list_for_each_entry_continue_rcu(obj, &object_list, object_list) { 2259 if (get_object(obj)) { 2260 next_obj = obj; 2261 break; 2262 } 2263 } 2264 2265 put_object(prev_obj); 2266 return next_obj; 2267 } 2268 2269 /* 2270 * Decrement the use_count of the last object required, if any. 2271 */ 2272 static void kmemleak_seq_stop(struct seq_file *seq, void *v) 2273 { 2274 if (!IS_ERR(v)) { 2275 /* 2276 * kmemleak_seq_start may return ERR_PTR if the scan_mutex 2277 * waiting was interrupted, so only release it if !IS_ERR. 2278 */ 2279 rcu_read_unlock(); 2280 mutex_unlock(&scan_mutex); 2281 if (v) 2282 put_object(v); 2283 } 2284 } 2285 2286 /* 2287 * Print the information for an unreferenced object to the seq file. 2288 */ 2289 static int kmemleak_seq_show(struct seq_file *seq, void *v) 2290 { 2291 struct kmemleak_object *object = v; 2292 unsigned long flags; 2293 2294 raw_spin_lock_irqsave(&object->lock, flags); 2295 if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object)) 2296 print_unreferenced(seq, object); 2297 raw_spin_unlock_irqrestore(&object->lock, flags); 2298 return 0; 2299 } 2300 2301 static const struct seq_operations kmemleak_seq_ops = { 2302 .start = kmemleak_seq_start, 2303 .next = kmemleak_seq_next, 2304 .stop = kmemleak_seq_stop, 2305 .show = kmemleak_seq_show, 2306 }; 2307 2308 static int kmemleak_open(struct inode *inode, struct file *file) 2309 { 2310 return seq_open(file, &kmemleak_seq_ops); 2311 } 2312 2313 static bool __dump_str_object_info(unsigned long addr, unsigned int objflags) 2314 { 2315 unsigned long flags; 2316 struct kmemleak_object *object; 2317 2318 object = __find_and_get_object(addr, 1, objflags); 2319 if (!object) 2320 return false; 2321 2322 raw_spin_lock_irqsave(&object->lock, flags); 2323 dump_object_info(object); 2324 raw_spin_unlock_irqrestore(&object->lock, flags); 2325 2326 put_object(object); 2327 2328 return true; 2329 } 2330 2331 static int dump_str_object_info(const char *str) 2332 { 2333 unsigned long addr; 2334 bool found = false; 2335 2336 if (kstrtoul(str, 0, &addr)) 2337 return -EINVAL; 2338 2339 found |= __dump_str_object_info(addr, 0); 2340 found |= __dump_str_object_info(addr, OBJECT_PHYS); 2341 found |= __dump_str_object_info(addr, OBJECT_PERCPU); 2342 2343 if (!found) { 2344 pr_info("Unknown object at 0x%08lx\n", addr); 2345 return -EINVAL; 2346 } 2347 2348 return 0; 2349 } 2350 2351 /* 2352 * We use grey instead of black to ensure we can do future scans on the same 2353 * objects. If we did not do future scans these black objects could 2354 * potentially contain references to newly allocated objects in the future and 2355 * we'd end up with false positives. 2356 */ 2357 static void kmemleak_clear(void) 2358 { 2359 struct kmemleak_object *object; 2360 2361 rcu_read_lock(); 2362 list_for_each_entry_rcu(object, &object_list, object_list) { 2363 raw_spin_lock_irq(&object->lock); 2364 if ((object->flags & OBJECT_REPORTED) && 2365 unreferenced_object(object)) 2366 __paint_it(object, KMEMLEAK_GREY); 2367 raw_spin_unlock_irq(&object->lock); 2368 } 2369 rcu_read_unlock(); 2370 2371 kmemleak_found_leaks = false; 2372 } 2373 2374 static void __kmemleak_do_cleanup(void); 2375 2376 /* 2377 * File write operation to configure kmemleak at run-time. The following 2378 * commands can be written to the /sys/kernel/debug/kmemleak file: 2379 * off - disable kmemleak (irreversible) 2380 * stack=on - enable the task stacks scanning 2381 * stack=off - disable the tasks stacks scanning 2382 * scan=on - start the automatic memory scanning thread 2383 * scan=off - stop the automatic memory scanning thread 2384 * scan=... - set the automatic memory scanning period in seconds (0 to 2385 * disable it) 2386 * scan - trigger a memory scan 2387 * clear - mark all current reported unreferenced kmemleak objects as 2388 * grey to ignore printing them, or free all kmemleak objects 2389 * if kmemleak has been disabled. 2390 * dump=... - dump information about the object found at the given address 2391 */ 2392 static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, 2393 size_t size, loff_t *ppos) 2394 { 2395 char buf[64]; 2396 int buf_size; 2397 int ret; 2398 2399 buf_size = min(size, (sizeof(buf) - 1)); 2400 if (strncpy_from_user(buf, user_buf, buf_size) < 0) 2401 return -EFAULT; 2402 buf[buf_size] = 0; 2403 2404 ret = mutex_lock_interruptible(&scan_mutex); 2405 if (ret < 0) 2406 return ret; 2407 2408 if (strncmp(buf, "clear", 5) == 0) { 2409 if (kmemleak_enabled) 2410 kmemleak_clear(); 2411 else 2412 __kmemleak_do_cleanup(); 2413 goto out; 2414 } 2415 2416 if (!kmemleak_enabled) { 2417 ret = -EPERM; 2418 goto out; 2419 } 2420 2421 if (strncmp(buf, "off", 3) == 0) 2422 kmemleak_disable(); 2423 else if (strncmp(buf, "stack=on", 8) == 0) 2424 kmemleak_stack_scan = 1; 2425 else if (strncmp(buf, "stack=off", 9) == 0) 2426 kmemleak_stack_scan = 0; 2427 else if (strncmp(buf, "scan=on", 7) == 0) 2428 start_scan_thread(); 2429 else if (strncmp(buf, "scan=off", 8) == 0) 2430 stop_scan_thread(); 2431 else if (strncmp(buf, "scan=", 5) == 0) { 2432 unsigned secs; 2433 unsigned long msecs; 2434 2435 ret = kstrtouint(buf + 5, 0, &secs); 2436 if (ret < 0) 2437 goto out; 2438 2439 msecs = secs * MSEC_PER_SEC; 2440 if (msecs > UINT_MAX) 2441 msecs = UINT_MAX; 2442 2443 stop_scan_thread(); 2444 if (msecs) { 2445 WRITE_ONCE(jiffies_scan_wait, msecs_to_jiffies(msecs)); 2446 start_scan_thread(); 2447 } 2448 } else if (strncmp(buf, "scan", 4) == 0) 2449 kmemleak_scan(); 2450 else if (strncmp(buf, "dump=", 5) == 0) 2451 ret = dump_str_object_info(buf + 5); 2452 else 2453 ret = -EINVAL; 2454 2455 out: 2456 mutex_unlock(&scan_mutex); 2457 if (ret < 0) 2458 return ret; 2459 2460 /* ignore the rest of the buffer, only one command at a time */ 2461 *ppos += size; 2462 return size; 2463 } 2464 2465 static const struct file_operations kmemleak_fops = { 2466 .owner = THIS_MODULE, 2467 .open = kmemleak_open, 2468 .read = seq_read, 2469 .write = kmemleak_write, 2470 .llseek = seq_lseek, 2471 .release = seq_release, 2472 }; 2473 2474 static void __kmemleak_do_cleanup(void) 2475 { 2476 struct kmemleak_object *object, *tmp; 2477 unsigned int cnt = 0; 2478 2479 /* 2480 * Kmemleak has already been disabled, no need for RCU list traversal 2481 * or kmemleak_lock held. 2482 */ 2483 list_for_each_entry_safe(object, tmp, &object_list, object_list) { 2484 __remove_object(object); 2485 __delete_object(object); 2486 2487 /* Call cond_resched() once per 64 iterations to avoid soft lockup */ 2488 if (!(++cnt & 0x3f)) 2489 cond_resched(); 2490 } 2491 } 2492 2493 /* 2494 * Stop the memory scanning thread and free the kmemleak internal objects if 2495 * no previous scan thread (otherwise, kmemleak may still have some useful 2496 * information on memory leaks). 2497 */ 2498 static void kmemleak_do_cleanup(struct work_struct *work) 2499 { 2500 stop_scan_thread(); 2501 2502 mutex_lock(&scan_mutex); 2503 /* 2504 * Once it is made sure that kmemleak_scan has stopped, it is safe to no 2505 * longer track object freeing. Ordering of the scan thread stopping and 2506 * the memory accesses below is guaranteed by the kthread_stop() 2507 * function. 2508 */ 2509 kmemleak_free_enabled = 0; 2510 mutex_unlock(&scan_mutex); 2511 2512 if (!kmemleak_found_leaks) 2513 __kmemleak_do_cleanup(); 2514 else 2515 pr_info("Kmemleak disabled without freeing internal data. Reclaim the memory with \"echo clear > /sys/kernel/debug/kmemleak\".\n"); 2516 } 2517 2518 static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup); 2519 2520 /* 2521 * Disable kmemleak. No memory allocation/freeing will be traced once this 2522 * function is called. Disabling kmemleak is an irreversible operation. 2523 */ 2524 static void kmemleak_disable(void) 2525 { 2526 /* atomically check whether it was already invoked */ 2527 if (cmpxchg(&kmemleak_error, 0, 1)) 2528 return; 2529 2530 /* stop any memory operation tracing */ 2531 kmemleak_enabled = 0; 2532 2533 /* check whether it is too early for a kernel thread */ 2534 if (kmemleak_late_initialized) 2535 schedule_work(&cleanup_work); 2536 else 2537 kmemleak_free_enabled = 0; 2538 2539 pr_info("Kernel memory leak detector disabled\n"); 2540 } 2541 2542 /* 2543 * Allow boot-time kmemleak disabling (enabled by default). 2544 */ 2545 static int __init kmemleak_boot_config(char *str) 2546 { 2547 if (!str) 2548 return -EINVAL; 2549 if (strcmp(str, "off") == 0) 2550 kmemleak_disable(); 2551 else if (strcmp(str, "on") == 0) { 2552 kmemleak_skip_disable = 1; 2553 stack_depot_request_early_init(); 2554 } 2555 else 2556 return -EINVAL; 2557 return 0; 2558 } 2559 early_param("kmemleak", kmemleak_boot_config); 2560 2561 /* 2562 * Kmemleak initialization. 2563 */ 2564 void __init kmemleak_init(void) 2565 { 2566 #ifdef CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF 2567 if (!kmemleak_skip_disable) { 2568 kmemleak_disable(); 2569 return; 2570 } 2571 #endif 2572 2573 if (kmemleak_error) 2574 return; 2575 2576 jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE); 2577 jiffies_scan_wait = secs_to_jiffies(SECS_SCAN_WAIT); 2578 2579 object_cache = KMEM_CACHE(kmemleak_object, SLAB_NOLEAKTRACE); 2580 scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE); 2581 2582 /* register the data/bss sections */ 2583 create_object((unsigned long)_sdata, _edata - _sdata, 2584 KMEMLEAK_GREY, GFP_ATOMIC); 2585 create_object((unsigned long)__bss_start, __bss_stop - __bss_start, 2586 KMEMLEAK_GREY, GFP_ATOMIC); 2587 /* only register .data..ro_after_init if not within .data */ 2588 if (&__start_ro_after_init < &_sdata || &__end_ro_after_init > &_edata) 2589 create_object((unsigned long)__start_ro_after_init, 2590 __end_ro_after_init - __start_ro_after_init, 2591 KMEMLEAK_GREY, GFP_ATOMIC); 2592 } 2593 2594 /* 2595 * Late initialization function. 2596 */ 2597 static int __init kmemleak_late_init(void) 2598 { 2599 kmemleak_late_initialized = 1; 2600 2601 debugfs_create_file("kmemleak", 0644, NULL, NULL, &kmemleak_fops); 2602 2603 if (kmemleak_error) { 2604 /* 2605 * Some error occurred and kmemleak was disabled. There is a 2606 * small chance that kmemleak_disable() was called immediately 2607 * after setting kmemleak_late_initialized and we may end up with 2608 * two clean-up threads but serialized by scan_mutex. 2609 */ 2610 schedule_work(&cleanup_work); 2611 return -ENOMEM; 2612 } 2613 2614 if (IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN)) { 2615 mutex_lock(&scan_mutex); 2616 start_scan_thread(); 2617 mutex_unlock(&scan_mutex); 2618 } 2619 2620 pr_info("Kernel memory leak detector initialized (mem pool available: %d)\n", 2621 mem_pool_free_count); 2622 2623 return 0; 2624 } 2625 late_initcall(kmemleak_late_init); 2626