mm/kmsan/hooks.c
Source file repositories/reference/linux-study-clean/mm/kmsan/hooks.c
File Facts
- System
- Linux kernel
- Corpus path
mm/kmsan/hooks.c- Extension
.c- Size
- 12063 bytes
- Lines
- 443
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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/cacheflush.hlinux/dma-direction.hlinux/gfp.hlinux/kmsan.hlinux/mm.hlinux/mm_types.hlinux/scatterlist.hlinux/slab.hlinux/uaccess.hlinux/usb.h../internal.h../slab.hkmsan.h
Detected Declarations
function Copyrightfunction kmsan_task_exitfunction kmsan_slab_allocfunction kmsan_slab_freefunction kmsan_kmalloc_largefunction kmsan_kfree_largefunction vmalloc_shadowfunction vmalloc_originfunction kmsan_vunmap_range_noflushfunction kmsan_ioremap_page_rangefunction kmsan_iounmap_page_rangefunction kmsan_copy_to_userfunction kmsan_memmovefunction kmsan_handle_urbfunction kmsan_handle_dma_pagefunction kmsan_handle_dmafunction kmsan_handle_dma_sgfunction kmsan_poison_memoryfunction kmsan_poison_memoryfunction kmsan_unpoison_memoryfunction kmsan_check_memoryfunction kmsan_enable_currentfunction kmsan_disable_currentexport kmsan_copy_to_userexport kmsan_memmoveexport kmsan_handle_urbexport kmsan_handle_dmaexport kmsan_poison_memoryexport kmsan_unpoison_memoryexport kmsan_check_memoryexport kmsan_enable_currentexport kmsan_disable_current
Annotated Snippet
if (!shadow || !origin) {
err = -ENOMEM;
goto ret;
}
mapped = __vmap_pages_range_noflush(
vmalloc_shadow(start + off),
vmalloc_shadow(start + off + PAGE_SIZE), prot, &shadow,
PAGE_SHIFT);
if (mapped) {
err = mapped;
goto ret;
}
shadow = NULL;
mapped = __vmap_pages_range_noflush(
vmalloc_origin(start + off),
vmalloc_origin(start + off + PAGE_SIZE), prot, &origin,
PAGE_SHIFT);
if (mapped) {
__vunmap_range_noflush(
vmalloc_shadow(start + off),
vmalloc_shadow(start + off + PAGE_SIZE));
err = mapped;
goto ret;
}
origin = NULL;
}
/* Page mapping loop finished normally, nothing to clean up. */
clean = 0;
ret:
if (clean > 0) {
/*
* Something went wrong. Clean up shadow/origin pages allocated
* on the last loop iteration, then delete mappings created
* during the previous iterations.
*/
if (shadow)
__free_pages(shadow, 1);
if (origin)
__free_pages(origin, 1);
__vunmap_range_noflush(
vmalloc_shadow(start),
vmalloc_shadow(start + clean * PAGE_SIZE));
__vunmap_range_noflush(
vmalloc_origin(start),
vmalloc_origin(start + clean * PAGE_SIZE));
}
flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
kmsan_leave_runtime();
return err;
}
void kmsan_iounmap_page_range(unsigned long start, unsigned long end)
{
unsigned long v_shadow, v_origin;
struct page *shadow, *origin;
int nr;
if (!kmsan_enabled || kmsan_in_runtime())
return;
nr = (end - start) / PAGE_SIZE;
kmsan_enter_runtime();
v_shadow = (unsigned long)vmalloc_shadow(start);
v_origin = (unsigned long)vmalloc_origin(start);
for (int i = 0; i < nr;
i++, v_shadow += PAGE_SIZE, v_origin += PAGE_SIZE) {
shadow = kmsan_vmalloc_to_page_or_null((void *)v_shadow);
origin = kmsan_vmalloc_to_page_or_null((void *)v_origin);
__vunmap_range_noflush(v_shadow, vmalloc_shadow(end));
__vunmap_range_noflush(v_origin, vmalloc_origin(end));
if (shadow)
__free_pages(shadow, 1);
if (origin)
__free_pages(origin, 1);
}
flush_cache_vmap(vmalloc_shadow(start), vmalloc_shadow(end));
flush_cache_vmap(vmalloc_origin(start), vmalloc_origin(end));
kmsan_leave_runtime();
}
void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy,
size_t left)
{
unsigned long ua_flags;
if (!kmsan_enabled || kmsan_in_runtime())
return;
/*
Annotation
- Immediate include surface: `linux/cacheflush.h`, `linux/dma-direction.h`, `linux/gfp.h`, `linux/kmsan.h`, `linux/mm.h`, `linux/mm_types.h`, `linux/scatterlist.h`, `linux/slab.h`.
- Detected declarations: `function Copyright`, `function kmsan_task_exit`, `function kmsan_slab_alloc`, `function kmsan_slab_free`, `function kmsan_kmalloc_large`, `function kmsan_kfree_large`, `function vmalloc_shadow`, `function vmalloc_origin`, `function kmsan_vunmap_range_noflush`, `function kmsan_ioremap_page_range`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
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.