mm/kmemleak.c
Source file repositories/reference/linux-study-clean/mm/kmemleak.c
File Facts
- System
- Linux kernel
- Corpus path
mm/kmemleak.c- Extension
.c- Size
- 71605 bytes
- Lines
- 2477
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/init.hlinux/kernel.hlinux/list.hlinux/sched/signal.hlinux/sched/task.hlinux/sched/task_stack.hlinux/jiffies.hlinux/delay.hlinux/export.hlinux/kthread.hlinux/rbtree.hlinux/fs.hlinux/debugfs.hlinux/seq_file.hlinux/cpumask.hlinux/spinlock.hlinux/module.hlinux/mutex.hlinux/rcupdate.hlinux/stacktrace.hlinux/stackdepot.hlinux/cache.hlinux/percpu.hlinux/memblock.hlinux/pfn.hlinux/mmzone.hlinux/slab.hlinux/thread_info.hlinux/err.hlinux/uaccess.hlinux/string.hlinux/nodemask.h
Detected Declarations
struct kmemleak_scan_areastruct kmemleak_objectfunction warn_or_seq_hex_dumpfunction hex_dump_objectfunction itfunction color_grayfunction unreferenced_objectfunction __print_unreferencedfunction print_unreferencedfunction dump_object_infofunction rcu_read_lockfunction mem_pool_freefunction free_object_rcufunction delete_objectfunction __remove_objectfunction set_track_preparefunction __link_objectfunction metadatafunction create_objectfunction create_object_physfunction create_object_percpufunction put_objectfunction metadatafunction metadatafunction __paint_itfunction paint_itfunction paint_ptrfunction make_gray_objectfunction make_black_objectfunction reset_checksumfunction add_scan_areafunction referencesfunction object_no_scanfunction ignoredfunction kmemleak_alloc_percpufunction vmallocfunction objectfunction kmemleak_free_partfunction kmemleak_free_percpufunction kmemleak_update_tracefunction kmemleak_not_leakfunction kmemleak_transient_leakfunction kmemleak_ignore_percpufunction ignoredfunction kmemleak_scan_areafunction kmemleak_no_scanfunction kmemleak_alloc_physfunction kmemleak_free_part_phys
Annotated Snippet
static const struct file_operations kmemleak_fops = {
.owner = THIS_MODULE,
.open = kmemleak_open,
.read = seq_read,
.write = kmemleak_write,
.llseek = seq_lseek,
.release = seq_release,
};
static void __kmemleak_do_cleanup(void)
{
struct kmemleak_object *object, *tmp;
unsigned int cnt = 0;
/*
* Kmemleak has already been disabled, no need for RCU list traversal
* or kmemleak_lock held.
*/
list_for_each_entry_safe(object, tmp, &object_list, object_list) {
__remove_object(object);
__delete_object(object);
/* Call cond_resched() once per 64 iterations to avoid soft lockup */
if (!(++cnt & 0x3f))
cond_resched();
}
}
/*
* Stop the memory scanning thread and free the kmemleak internal objects if
* no previous scan thread (otherwise, kmemleak may still have some useful
* information on memory leaks).
*/
static void kmemleak_do_cleanup(struct work_struct *work)
{
stop_scan_thread();
mutex_lock(&scan_mutex);
/*
* Once it is made sure that kmemleak_scan has stopped, it is safe to no
* longer track object freeing. Ordering of the scan thread stopping and
* the memory accesses below is guaranteed by the kthread_stop()
* function.
*/
kmemleak_free_enabled = 0;
mutex_unlock(&scan_mutex);
if (!kmemleak_found_leaks)
__kmemleak_do_cleanup();
else
pr_info("Kmemleak disabled without freeing internal data. Reclaim the memory with \"echo clear > /sys/kernel/debug/kmemleak\".\n");
}
static DECLARE_WORK(cleanup_work, kmemleak_do_cleanup);
/*
* Disable kmemleak. No memory allocation/freeing will be traced once this
* function is called. Disabling kmemleak is an irreversible operation.
*/
static void kmemleak_disable(void)
{
/* atomically check whether it was already invoked */
if (cmpxchg(&kmemleak_error, 0, 1))
return;
/* stop any memory operation tracing */
kmemleak_enabled = 0;
/* check whether it is too early for a kernel thread */
if (kmemleak_late_initialized)
schedule_work(&cleanup_work);
else
kmemleak_free_enabled = 0;
pr_info("Kernel memory leak detector disabled\n");
}
/*
* Allow boot-time kmemleak disabling (enabled by default).
*/
static int __init kmemleak_boot_config(char *str)
{
if (!str)
return -EINVAL;
if (strcmp(str, "off") == 0)
kmemleak_disable();
else if (strcmp(str, "on") == 0) {
kmemleak_skip_disable = 1;
stack_depot_request_early_init();
}
Annotation
- Immediate include surface: `linux/init.h`, `linux/kernel.h`, `linux/list.h`, `linux/sched/signal.h`, `linux/sched/task.h`, `linux/sched/task_stack.h`, `linux/jiffies.h`, `linux/delay.h`.
- Detected declarations: `struct kmemleak_scan_area`, `struct kmemleak_object`, `function warn_or_seq_hex_dump`, `function hex_dump_object`, `function it`, `function color_gray`, `function unreferenced_object`, `function __print_unreferenced`, `function print_unreferenced`, `function dump_object_info`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: pattern implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.