mm/memory.c
Source file repositories/reference/linux-study-clean/mm/memory.c
File Facts
- System
- Linux kernel
- Corpus path
mm/memory.c- Extension
.c- Size
- 216641 bytes
- Lines
- 7556
- 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.
- 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/kernel_stat.hlinux/mm.hlinux/mm_inline.hlinux/sched/mm.hlinux/sched/numa_balancing.hlinux/sched/task.hlinux/hugetlb.hlinux/mman.hlinux/swap.hlinux/highmem.hlinux/pagemap.hlinux/memremap.hlinux/kmsan.hlinux/ksm.hlinux/rmap.hlinux/export.hlinux/delayacct.hlinux/init.hlinux/writeback.hlinux/memcontrol.hlinux/mmu_notifier.hlinux/leafops.hlinux/elf.hlinux/gfp.hlinux/migrate.hlinux/string.hlinux/shmem_fs.hlinux/memory-tiers.hlinux/debugfs.hlinux/userfaultfd_k.hlinux/dax.hlinux/oom.h
Detected Declarations
struct copy_subpage_argfunction vmf_orig_pte_uffd_wpfunction init_mm_sysctlfunction arch_wants_old_prefaulted_ptefunction disable_randmapsfunction mm_trace_rss_statfunction free_pte_rangefunction free_pmd_rangefunction free_pud_rangefunction free_p4d_rangefunction free_pgd_rangefunction free_pgtablesfunction pmd_installfunction __pte_allocfunction __pte_alloc_kernelfunction init_rss_vecfunction add_mm_rss_vecfunction is_bad_page_map_ratelimitedfunction __print_bad_page_map_pgtablefunction entryfunction pgtable_level_has_pxx_specialfunction specialfunction pte_specialfunction vm_normal_pagefunction vm_normal_foliofunction vm_normal_page_pmdfunction vm_normal_folio_pmdfunction vm_normal_page_pudfunction modificationsfunction try_restore_exclusive_ptefunction copy_nonpresent_ptefunction involvedfunction copy_present_pagefunction __copy_present_ptesfunction copy_present_ptesfunction copy_pte_rangefunction copy_pmd_rangefunction copy_pud_rangefunction copy_p4d_rangefunction forkfunction copy_page_rangefunction should_zap_cowsfunction should_zap_foliofunction zap_drop_markersfunction zap_install_uffd_wp_if_neededfunction zap_present_folio_ptesfunction processedfunction zap_nonpresent_ptes
Annotated Snippet
vm_fault_t handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
unsigned int flags, struct pt_regs *regs)
{
/* If the fault handler drops the mmap_lock, vma may be freed */
struct mm_struct *mm = vma->vm_mm;
vm_fault_t ret;
bool is_droppable;
__set_current_state(TASK_RUNNING);
ret = sanitize_fault_flags(vma, &flags);
if (ret)
goto out;
if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
flags & FAULT_FLAG_INSTRUCTION,
flags & FAULT_FLAG_REMOTE)) {
ret = VM_FAULT_SIGSEGV;
goto out;
}
is_droppable = !!(vma->vm_flags & VM_DROPPABLE);
/*
* Enable the memcg OOM handling for faults triggered in user
* space. Kernel faults are handled more gracefully.
*/
if (flags & FAULT_FLAG_USER)
mem_cgroup_enter_user_fault();
lru_gen_enter_fault(vma);
if (unlikely(is_vm_hugetlb_page(vma)))
ret = hugetlb_fault(vma->vm_mm, vma, address, flags);
else
ret = __handle_mm_fault(vma, address, flags);
/*
* Warning: It is no longer safe to dereference vma-> after this point,
* because mmap_lock might have been dropped by __handle_mm_fault(), so
* vma might be destroyed from underneath us.
*/
lru_gen_exit_fault();
/* If the mapping is droppable, then errors due to OOM aren't fatal. */
if (is_droppable)
ret &= ~VM_FAULT_OOM;
if (flags & FAULT_FLAG_USER) {
mem_cgroup_exit_user_fault();
/*
* The task may have entered a memcg OOM situation but
* if the allocation error was handled gracefully (no
* VM_FAULT_OOM), there is no need to kill anything.
* Just clean up the OOM state peacefully.
*/
if (task_in_memcg_oom(current) && !(ret & VM_FAULT_OOM))
mem_cgroup_oom_synchronize(false);
}
out:
mm_account_fault(mm, regs, address, flags, ret);
return ret;
}
EXPORT_SYMBOL_GPL(handle_mm_fault);
#ifndef __PAGETABLE_P4D_FOLDED
/*
* Allocate p4d page table.
* We've already handled the fast-path in-line.
*/
int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
{
p4d_t *new = p4d_alloc_one(mm, address);
if (!new)
return -ENOMEM;
spin_lock(&mm->page_table_lock);
if (pgd_present(*pgd)) { /* Another has populated it */
p4d_free(mm, new);
} else {
smp_wmb(); /* See comment in pmd_install() */
pgd_populate(mm, pgd, new);
}
spin_unlock(&mm->page_table_lock);
return 0;
}
#endif /* __PAGETABLE_P4D_FOLDED */
Annotation
- Immediate include surface: `linux/kernel_stat.h`, `linux/mm.h`, `linux/mm_inline.h`, `linux/sched/mm.h`, `linux/sched/numa_balancing.h`, `linux/sched/task.h`, `linux/hugetlb.h`, `linux/mman.h`.
- Detected declarations: `struct copy_subpage_arg`, `function vmf_orig_pte_uffd_wp`, `function init_mm_sysctl`, `function arch_wants_old_prefaulted_pte`, `function disable_randmaps`, `function mm_trace_rss_stat`, `function free_pte_range`, `function free_pmd_range`, `function free_pud_range`, `function free_p4d_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.
- 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.