Lines Matching refs:object

40  *   Note that the kmemleak_object.use_count is incremented when an object is
48 * scan_mutex [-> object->lock] -> kmemleak_lock -> other_object->lock (SINGLE_DEPTH_NESTING)
50 * No kmemleak_lock and object->lock nesting is allowed outside scan_mutex
112 #define MSECS_MIN_AGE 5000 /* minimum object age for reporting */
138 * object->lock. Insertions or deletions from object_list, gray_list or
145 unsigned int flags; /* object status flags */
150 /* object usage count; object freed when use_count == 0 */
159 /* the total number of pointers found pointing to this object */
163 /* memory ranges to be scanned inside an object (empty for all) */
173 /* flag set after the first reporting of an unreference object */
175 /* flag set to not scan the object */
177 /* flag set to fully scan the object when scan_area allocation failed */
179 /* flag set for object allocated with physical address */
205 /* search tree for object boundaries */
207 /* search tree for object (with OBJECT_PHYS flag) boundaries */
293 * with the object->lock held.
296 struct kmemleak_object *object)
298 const u8 *ptr = (const u8 *)object->pointer;
301 if (WARN_ON_ONCE(object->flags & OBJECT_PHYS))
305 len = min_t(size_t, object->size, HEX_MAX_LINES * HEX_ROW_SIZE);
316 * - white - orphan object, not enough references to it (count < min_count)
321 * Newly created objects don't have any color assigned (object->count == -1)
324 static bool color_white(const struct kmemleak_object *object)
326 return object->count != KMEMLEAK_BLACK &&
327 object->count < object->min_count;
330 static bool color_gray(const struct kmemleak_object *object)
332 return object->min_count != KMEMLEAK_BLACK &&
333 object->count >= object->min_count;
341 static bool unreferenced_object(struct kmemleak_object *object)
343 return (color_white(object) && object->flags & OBJECT_ALLOCATED) &&
344 time_before_eq(object->jiffies + jiffies_min_age,
350 * print_unreferenced function must be called with the object->lock held.
353 struct kmemleak_object *object)
358 unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies);
360 nr_entries = stack_depot_fetch(object->trace_handle, &entries);
361 warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
362 object->pointer, object->size);
364 object->comm, object->pid, object->jiffies,
366 hex_dump_object(seq, object);
378 * the object->lock held.
380 static void dump_object_info(struct kmemleak_object *object)
383 object->pointer, object->size);
385 object->comm, object->pid, object->jiffies);
386 pr_notice(" min_count = %d\n", object->min_count);
387 pr_notice(" count = %d\n", object->count);
388 pr_notice(" flags = 0x%x\n", object->flags);
389 pr_notice(" checksum = %u\n", object->checksum);
391 if (object->trace_handle)
392 stack_depot_print(object->trace_handle);
396 * Look-up a memory block metadata (kmemleak_object) in the object search
409 struct kmemleak_object *object;
412 object = rb_entry(rb, struct kmemleak_object, rb_node);
413 untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
416 rb = object->rb_node.rb_left;
417 else if (untagged_objp + object->size <= untagged_ptr)
418 rb = object->rb_node.rb_right;
420 return object;
422 kmemleak_warn("Found object by alias at 0x%08lx\n",
424 dump_object_info(object);
431 /* Look-up a kmemleak object which allocated with virtual address. */
438 * Increment the object use_count. Return 1 if successful or 0 otherwise. Note
439 * that once an object's use_count reached 0, the RCU freeing was already
440 * registered and the object should no longer be used. This function must be
443 static int get_object(struct kmemleak_object *object)
445 return atomic_inc_not_zero(&object->use_count);
454 struct kmemleak_object *object;
458 object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
459 if (object)
460 return object;
465 object = list_first_entry_or_null(&mem_pool_free_list,
466 typeof(*object), object_list);
467 if (object)
468 list_del(&object->object_list);
470 object = &mem_pool[--mem_pool_free_count];
475 return object;
479 * Return the object to either the slab allocator or the memory pool.
481 static void mem_pool_free(struct kmemleak_object *object)
485 if (object < mem_pool || object >= mem_pool + ARRAY_SIZE(mem_pool)) {
486 kmem_cache_free(object_cache, object);
490 /* add the object to the memory pool free list */
492 list_add(&object->object_list, &mem_pool_free_list);
503 struct kmemleak_object *object =
508 * code accessing this object, hence no need for locking.
510 hlist_for_each_entry_safe(area, tmp, &object->area_list, node) {
514 mem_pool_free(object);
518 * Decrement the object use_count. Once the count is 0, free the object using
524 static void put_object(struct kmemleak_object *object)
526 if (!atomic_dec_and_test(&object->use_count))
530 WARN_ON(object->flags & OBJECT_ALLOCATED);
535 * came from the memory pool. Free the object directly.
538 call_rcu(&object->rcu, free_object_rcu);
540 free_object_rcu(&object->rcu);
544 * Look up an object in the object search tree and increase its use_count.
550 struct kmemleak_object *object;
554 object = __lookup_object(ptr, alias, is_phys);
557 /* check whether the object is still available */
558 if (object && !get_object(object))
559 object = NULL;
562 return object;
565 /* Look up and get an object which allocated with virtual address. */
572 * Remove an object from the object_tree_root (or object_phys_tree_root)
576 static void __remove_object(struct kmemleak_object *object)
578 rb_erase(&object->rb_node, object->flags & OBJECT_PHYS ?
581 if (!(object->del_state & DELSTATE_NO_DELETE))
582 list_del_rcu(&object->object_list);
583 object->del_state |= DELSTATE_REMOVED;
587 * Look up an object in the object search tree and remove it from both
589 * returned object's use_count should be at least 1, as initially set
596 struct kmemleak_object *object;
599 object = __lookup_object(ptr, alias, is_phys);
600 if (object)
601 __remove_object(object);
604 return object;
635 struct kmemleak_object *object, *parent;
640 object = mem_pool_alloc(gfp);
641 if (!object) {
647 INIT_LIST_HEAD(&object->object_list);
648 INIT_LIST_HEAD(&object->gray_list);
649 INIT_HLIST_HEAD(&object->area_list);
650 raw_spin_lock_init(&object->lock);
651 atomic_set(&object->use_count, 1);
652 object->flags = OBJECT_ALLOCATED | (is_phys ? OBJECT_PHYS : 0);
653 object->pointer = ptr;
654 object->size = kfence_ksize((void *)ptr) ?: size;
655 object->excess_ref = 0;
656 object->min_count = min_count;
657 object->count = 0; /* white color initially */
658 object->jiffies = jiffies;
659 object->checksum = 0;
660 object->del_state = 0;
664 object->pid = 0;
665 strncpy(object->comm, "hardirq", sizeof(object->comm));
667 object->pid = 0;
668 strncpy(object->comm, "softirq", sizeof(object->comm));
670 object->pid = current->pid;
677 strncpy(object->comm, current->comm, sizeof(object->comm));
681 object->trace_handle = set_track_prepare();
687 * Only update min_addr and max_addr with object
706 kmemleak_stop("Cannot insert 0x%lx into the object search tree (overlaps existing)\n",
713 kmem_cache_free(object_cache, object);
717 rb_link_node(&object->rb_node, rb_parent, link);
718 rb_insert_color(&object->rb_node, is_phys ? &object_phys_tree_root :
720 list_add_tail_rcu(&object->object_list, &object_list);
725 /* Create kmemleak object which allocated with virtual address. */
732 /* Create kmemleak object which allocated with physical address. */
740 * Mark the object as not allocated and schedule RCU freeing via put_object().
742 static void __delete_object(struct kmemleak_object *object)
746 WARN_ON(!(object->flags & OBJECT_ALLOCATED));
747 WARN_ON(atomic_read(&object->use_count) < 1);
753 raw_spin_lock_irqsave(&object->lock, flags);
754 object->flags &= ~OBJECT_ALLOCATED;
755 raw_spin_unlock_irqrestore(&object->lock, flags);
756 put_object(object);
765 struct kmemleak_object *object;
767 object = find_and_remove_object(ptr, 0, false);
768 if (!object) {
770 kmemleak_warn("Freeing unknown object at 0x%08lx\n",
775 __delete_object(object);
785 struct kmemleak_object *object;
788 object = find_and_remove_object(ptr, 1, is_phys);
789 if (!object) {
791 kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
802 start = object->pointer;
803 end = object->pointer + object->size;
805 __create_object(start, ptr - start, object->min_count,
808 __create_object(ptr + size, end - ptr - size, object->min_count,
811 __delete_object(object);
814 static void __paint_it(struct kmemleak_object *object, int color)
816 object->min_count = color;
818 object->flags |= OBJECT_NO_SCAN;
821 static void paint_it(struct kmemleak_object *object, int color)
825 raw_spin_lock_irqsave(&object->lock, flags);
826 __paint_it(object, color);
827 raw_spin_unlock_irqrestore(&object->lock, flags);
832 struct kmemleak_object *object;
834 object = __find_and_get_object(ptr, 0, is_phys);
835 if (!object) {
836 kmemleak_warn("Trying to color unknown object at 0x%08lx as %s\n",
842 paint_it(object, color);
843 put_object(object);
847 * Mark an object permanently as gray-colored so that it can no longer be
856 * Mark the object as black-colored so that it is ignored from scans and
865 * Add a scanning area to the object. If at least one such area is added,
871 struct kmemleak_object *object;
876 object = find_and_get_object(ptr, 1);
877 if (!object) {
878 kmemleak_warn("Adding scan area to unknown object at 0x%08lx\n",
884 untagged_objp = (unsigned long)kasan_reset_tag((void *)object->pointer);
889 raw_spin_lock_irqsave(&object->lock, flags);
891 pr_warn_once("Cannot allocate a scan area, scanning the full object\n");
892 /* mark the object for full scan to avoid false positives */
893 object->flags |= OBJECT_FULL_SCAN;
897 size = untagged_objp + object->size - untagged_ptr;
898 } else if (untagged_ptr + size > untagged_objp + object->size) {
899 kmemleak_warn("Scan area larger than object 0x%08lx\n", ptr);
900 dump_object_info(object);
909 hlist_add_head(&area->node, &object->area_list);
911 raw_spin_unlock_irqrestore(&object->lock, flags);
912 put_object(object);
916 * Any surplus references (object already gray) to 'ptr' are passed to
918 * vm_struct may be used as an alternative reference to the vmalloc'ed object
924 struct kmemleak_object *object;
926 object = find_and_get_object(ptr, 0);
927 if (!object) {
928 kmemleak_warn("Setting excess_ref on unknown object at 0x%08lx\n",
933 raw_spin_lock_irqsave(&object->lock, flags);
934 object->excess_ref = excess_ref;
935 raw_spin_unlock_irqrestore(&object->lock, flags);
936 put_object(object);
940 * Set the OBJECT_NO_SCAN flag for the object corresponding to the give
941 * pointer. Such object will not be scanned by kmemleak but references to it
947 struct kmemleak_object *object;
949 object = find_and_get_object(ptr, 0);
950 if (!object) {
951 kmemleak_warn("Not scanning unknown object at 0x%08lx\n", ptr);
955 raw_spin_lock_irqsave(&object->lock, flags);
956 object->flags |= OBJECT_NO_SCAN;
957 raw_spin_unlock_irqrestore(&object->lock, flags);
958 put_object(object);
962 * kmemleak_alloc - register a newly allocated object
963 * @ptr: pointer to beginning of the object
964 * @size: size of the object
965 * @min_count: minimum number of references to this object. If during memory
967 * the object is reported as a memory leak. If @min_count is 0,
968 * the object is never reported as a leak. If @min_count is -1,
969 * the object is ignored (not scanned and not reported as a leak)
972 * This function is called from the kernel allocators when a new object
986 * kmemleak_alloc_percpu - register a newly allocated __percpu object
987 * @ptr: __percpu pointer to beginning of the object
988 * @size: size of the object
991 * This function is called from the kernel percpu allocator when a new object
1013 * kmemleak_vmalloc - register a newly vmalloc'ed object
1015 * @size: size of the object
1019 * object (memory block) is allocated.
1038 * kmemleak_free - unregister a previously registered object
1039 * @ptr: pointer to beginning of the object
1041 * This function is called from the kernel allocators when an object (memory
1054 * kmemleak_free_part - partially unregister a previously registered object
1055 * @ptr: pointer to the beginning or inside the object. This also
1072 * kmemleak_free_percpu - unregister a previously registered __percpu object
1073 * @ptr: __percpu pointer to beginning of the object
1075 * This function is called from the kernel percpu allocator when an object
1092 * kmemleak_update_trace - update object allocation stack trace
1093 * @ptr: pointer to beginning of the object
1095 * Override the object allocation stack trace for cases where the actual
1100 struct kmemleak_object *object;
1108 object = find_and_get_object((unsigned long)ptr, 1);
1109 if (!object) {
1111 kmemleak_warn("Updating stack trace for unknown object at %p\n",
1117 raw_spin_lock_irqsave(&object->lock, flags);
1118 object->trace_handle = set_track_prepare();
1119 raw_spin_unlock_irqrestore(&object->lock, flags);
1121 put_object(object);
1126 * kmemleak_not_leak - mark an allocated object as false positive
1127 * @ptr: pointer to beginning of the object
1129 * Calling this function on an object will cause the memory block to no longer
1142 * kmemleak_ignore - ignore an allocated object
1143 * @ptr: pointer to beginning of the object
1145 * Calling this function on an object will cause the memory block to be
1160 * kmemleak_scan_area - limit the range to be scanned in an allocated object
1161 * @ptr: pointer to beginning or inside the object. This also
1166 * This function is used when it is known that only certain parts of an object
1180 * kmemleak_no_scan - do not scan an allocated object
1181 * @ptr: pointer to beginning of the object
1184 * in situations where it is known that the given object does not contain any
1200 * @phys: physical address of the object
1201 * @size: size of the object
1210 * Create object with OBJECT_PHYS flag and
1220 * @phys: physical address if the beginning or inside an object. This
1236 * @phys: physical address of the object
1248 * Update an object's checksum and return true if it was modified.
1250 static bool update_checksum(struct kmemleak_object *object)
1252 u32 old_csum = object->checksum;
1254 if (WARN_ON_ONCE(object->flags & OBJECT_PHYS))
1259 object->checksum = crc32(0, kasan_reset_tag((void *)object->pointer), object->size);
1263 return object->checksum != old_csum;
1267 * Update an object's references. object->lock must be held by the caller.
1269 static void update_refs(struct kmemleak_object *object)
1271 if (!color_white(object)) {
1277 * Increase the object's reference count (number of pointers to the
1279 * object's color will become gray and it will be added to the
1282 object->count++;
1283 if (color_gray(object)) {
1285 WARN_ON(!get_object(object));
1286 list_add_tail(&object->gray_list, &gray_list);
1326 struct kmemleak_object *object;
1343 * object->use_count cannot be dropped to 0 while the object
1347 object = lookup_object(pointer, 1);
1348 if (!object)
1350 if (object == scanned)
1355 * Avoid the lockdep recursive warning on object->lock being
1359 raw_spin_lock_nested(&object->lock, SINGLE_DEPTH_NESTING);
1360 /* only pass surplus references (object already gray) */
1361 if (color_gray(object)) {
1362 excess_ref = object->excess_ref;
1363 /* no need for update_refs() if object already gray */
1366 update_refs(object);
1368 raw_spin_unlock(&object->lock);
1371 object = lookup_object(excess_ref, 0);
1372 if (!object)
1374 if (object == scanned)
1377 raw_spin_lock_nested(&object->lock, SINGLE_DEPTH_NESTING);
1378 update_refs(object);
1379 raw_spin_unlock(&object->lock);
1404 * that object->use_count >= 1.
1406 static void scan_object(struct kmemleak_object *object)
1413 * Once the object->lock is acquired, the corresponding memory block
1416 raw_spin_lock_irqsave(&object->lock, flags);
1417 if (object->flags & OBJECT_NO_SCAN)
1419 if (!(object->flags & OBJECT_ALLOCATED))
1420 /* already freed object */
1423 obj_ptr = object->flags & OBJECT_PHYS ?
1424 __va((phys_addr_t)object->pointer) :
1425 (void *)object->pointer;
1427 if (hlist_empty(&object->area_list) ||
1428 object->flags & OBJECT_FULL_SCAN) {
1430 void *end = obj_ptr + object->size;
1435 scan_block(start, next, object);
1441 raw_spin_unlock_irqrestore(&object->lock, flags);
1443 raw_spin_lock_irqsave(&object->lock, flags);
1444 } while (object->flags & OBJECT_ALLOCATED);
1446 hlist_for_each_entry(area, &object->area_list, node)
1449 object);
1451 raw_spin_unlock_irqrestore(&object->lock, flags);
1460 struct kmemleak_object *object, *tmp;
1467 object = list_entry(gray_list.next, typeof(*object), gray_list);
1468 while (&object->gray_list != &gray_list) {
1473 scan_object(object);
1475 tmp = list_entry(object->gray_list.next, typeof(*object),
1478 /* remove the object from the list and release it */
1479 list_del(&object->gray_list);
1480 put_object(object);
1482 object = tmp;
1488 * Conditionally call resched() in an object iteration loop while making sure
1489 * that the given object won't go away without RCU read lock by performing a
1492 static void kmemleak_cond_resched(struct kmemleak_object *object)
1494 if (!get_object(object))
1495 return; /* Try next object */
1498 if (object->del_state & DELSTATE_REMOVED)
1500 object->del_state |= DELSTATE_NO_DELETE;
1508 if (object->del_state & DELSTATE_REMOVED)
1509 list_del_rcu(&object->object_list);
1510 object->del_state &= ~DELSTATE_NO_DELETE;
1513 put_object(object);
1523 struct kmemleak_object *object;
1532 list_for_each_entry_rcu(object, &object_list, object_list) {
1533 raw_spin_lock_irq(&object->lock);
1537 * 1 reference to any object at this point.
1539 if (atomic_read(&object->use_count) > 1) {
1540 pr_debug("object->use_count = %d\n",
1541 atomic_read(&object->use_count));
1542 dump_object_info(object);
1547 if ((object->flags & OBJECT_PHYS) &&
1548 !(object->flags & OBJECT_NO_SCAN)) {
1549 unsigned long phys = object->pointer;
1552 PHYS_PFN(phys + object->size) >= max_low_pfn)
1553 __paint_it(object, KMEMLEAK_BLACK);
1556 /* reset the reference count (whiten the object) */
1557 object->count = 0;
1558 if (color_gray(object) && get_object(object))
1559 list_add_tail(&object->gray_list, &gray_list);
1561 raw_spin_unlock_irq(&object->lock);
1564 kmemleak_cond_resched(object);
1632 list_for_each_entry_rcu(object, &object_list, object_list) {
1634 kmemleak_cond_resched(object);
1641 if (!color_white(object))
1643 raw_spin_lock_irq(&object->lock);
1644 if (color_white(object) && (object->flags & OBJECT_ALLOCATED)
1645 && update_checksum(object) && get_object(object)) {
1647 object->count = object->min_count;
1648 list_add_tail(&object->gray_list, &gray_list);
1650 raw_spin_unlock_irq(&object->lock);
1669 list_for_each_entry_rcu(object, &object_list, object_list) {
1671 kmemleak_cond_resched(object);
1678 if (!color_white(object))
1680 raw_spin_lock_irq(&object->lock);
1681 if (unreferenced_object(object) &&
1682 !(object->flags & OBJECT_REPORTED)) {
1683 object->flags |= OBJECT_REPORTED;
1686 print_unreferenced(NULL, object);
1690 raw_spin_unlock_irq(&object->lock);
1768 * Iterate over the object_list and return the first valid object at or after
1774 struct kmemleak_object *object;
1783 list_for_each_entry_rcu(object, &object_list, object_list) {
1786 if (get_object(object))
1789 object = NULL;
1791 return object;
1795 * Return the next object in the object_list. The function decrements the
1796 * use_count of the previous object and increases that of the next one.
1818 * Decrement the use_count of the last object required, if any.
1835 * Print the information for an unreferenced object to the seq file.
1839 struct kmemleak_object *object = v;
1842 raw_spin_lock_irqsave(&object->lock, flags);
1843 if ((object->flags & OBJECT_REPORTED) && unreferenced_object(object))
1844 print_unreferenced(seq, object);
1845 raw_spin_unlock_irqrestore(&object->lock, flags);
1864 struct kmemleak_object *object;
1869 object = find_and_get_object(addr, 0);
1870 if (!object) {
1871 pr_info("Unknown object at 0x%08lx\n", addr);
1875 raw_spin_lock_irqsave(&object->lock, flags);
1876 dump_object_info(object);
1877 raw_spin_unlock_irqrestore(&object->lock, flags);
1879 put_object(object);
1891 struct kmemleak_object *object;
1894 list_for_each_entry_rcu(object, &object_list, object_list) {
1895 raw_spin_lock_irq(&object->lock);
1896 if ((object->flags & OBJECT_REPORTED) &&
1897 unreferenced_object(object))
1898 __paint_it(object, KMEMLEAK_GREY);
1899 raw_spin_unlock_irq(&object->lock);
1922 * dump=... - dump information about the object found at the given address
2008 struct kmemleak_object *object, *tmp;
2014 list_for_each_entry_safe(object, tmp, &object_list, object_list) {
2015 __remove_object(object);
2016 __delete_object(object);
2032 * longer track object freeing. Ordering of the scan thread stopping and