mm/huge_memory.c
Source file repositories/reference/linux-study-clean/mm/huge_memory.c
File Facts
- System
- Linux kernel
- Corpus path
mm/huge_memory.c- Extension
.c- Size
- 138782 bytes
- Lines
- 5019
- 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.
- 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/mm.hlinux/sched.hlinux/sched/mm.hlinux/sched/numa_balancing.hlinux/highmem.hlinux/hugetlb.hlinux/mmu_notifier.hlinux/rmap.hlinux/swap.hlinux/list_lru.hlinux/shrinker.hlinux/mm_inline.hlinux/swapops.hlinux/backing-dev.hlinux/dax.hlinux/mm_types.hlinux/khugepaged.hlinux/freezer.hlinux/mman.hlinux/memremap.hlinux/pagemap.hlinux/debugfs.hlinux/migrate.hlinux/hashtable.hlinux/userfaultfd_k.hlinux/page_idle.hlinux/shmem_fs.hlinux/oom.hlinux/numa.hlinux/page_owner.hlinux/sched/sysctl.hlinux/memory-tiers.h
Detected Declarations
struct folio_or_pfnenum anon_enabled_modeenum global_enabled_modeenum defrag_modefunction file_thp_enabledfunction vma_is_special_hugefunction __thp_vma_allowable_ordersfunction get_huge_zero_foliofunction put_huge_zero_foliofunction mm_put_huge_zero_foliofunction shrink_huge_zero_folio_countfunction shrink_huge_zero_folio_scanfunction enabled_showfunction set_global_enabled_modefunction enabled_storefunction single_hugepage_flag_showfunction single_hugepage_flag_storefunction defrag_showfunction defrag_storefunction use_zero_page_showfunction use_zero_page_storefunction hpage_pmd_size_showfunction split_underused_thp_showfunction split_underused_thp_storefunction anon_enabled_showfunction set_anon_enabled_modefunction anon_enabled_storefunction sum_mthp_statfunction for_each_possible_cpufunction sysfs_add_groupfunction thpsize_releasefunction hugepage_init_sysfsfunction hugepage_exit_sysfsfunction list_for_each_entry_safefunction hugepage_init_sysfsfunction hugepage_exit_sysfsfunction thp_shrinker_initfunction thp_shrinker_exitfunction hugepage_initfunction setup_transparent_hugepagefunction setup_thp_anonfunction maybe_pmd_mkwritefunction is_transparent_hugepagefunction __thp_get_unmapped_areafunction thp_get_unmapped_area_vmflagsfunction thp_get_unmapped_areafunction map_anon_folio_pmd_nopffunction map_anon_folio_pmd_pf
Annotated Snippet
static const struct file_operations split_huge_pages_fops = {
.owner = THIS_MODULE,
.write = split_huge_pages_write,
};
static int __init split_huge_pages_debugfs(void)
{
debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
&split_huge_pages_fops);
return 0;
}
late_initcall(split_huge_pages_debugfs);
#endif
#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
struct page *page)
{
struct folio *folio = page_folio(page);
struct vm_area_struct *vma = pvmw->vma;
struct mm_struct *mm = vma->vm_mm;
unsigned long address = pvmw->address;
bool anon_exclusive, present, writable, softdirty, uffd_wp;
pmd_t pmdval;
swp_entry_t entry;
pmd_t pmdswp;
if (!(pvmw->pmd && !pvmw->pte))
return 0;
present = pmd_present(*pvmw->pmd);
if (likely(present)) {
flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
writable = pmd_write(pmdval);
softdirty = pmd_soft_dirty(pmdval);
uffd_wp = pmd_uffd_wp(pmdval);
} else {
softleaf_t old_entry;
pmdval = pmdp_huge_get_and_clear(vma->vm_mm, address, pvmw->pmd);
old_entry = softleaf_from_pmd(pmdval);
writable = softleaf_is_device_private_write(old_entry);
softdirty = pmd_swp_soft_dirty(pmdval);
uffd_wp = pmd_swp_uffd_wp(pmdval);
}
/* See folio_try_share_anon_rmap_pmd(): invalidate PMD first. */
anon_exclusive = folio_test_anon(folio) && PageAnonExclusive(page);
if (anon_exclusive && folio_try_share_anon_rmap_pmd(folio, page)) {
set_pmd_at(mm, address, pvmw->pmd, pmdval);
return -EBUSY;
}
/* Determine type of migration entry. */
if (writable)
entry = make_writable_migration_entry(page_to_pfn(page));
else if (anon_exclusive)
entry = make_readable_exclusive_migration_entry(page_to_pfn(page));
else
entry = make_readable_migration_entry(page_to_pfn(page));
/* Set A/D bits as necessary. */
if (present && pmd_young(pmdval))
entry = make_migration_entry_young(entry);
if (present && pmd_dirty(pmdval)) {
folio_mark_dirty(folio);
entry = make_migration_entry_dirty(entry);
}
/* Set PMD. */
pmdswp = swp_entry_to_pmd(entry);
if (softdirty)
pmdswp = pmd_swp_mksoft_dirty(pmdswp);
if (uffd_wp)
pmdswp = pmd_swp_mkuffd_wp(pmdswp);
set_pmd_at(mm, address, pvmw->pmd, pmdswp);
/* Migration entry installed: cleanup rmap, folio. */
folio_remove_rmap_pmd(folio, page, vma);
folio_put(folio);
trace_set_migration_pmd(address, pmd_val(pmdswp));
return 0;
}
void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
Annotation
- Immediate include surface: `linux/mm.h`, `linux/sched.h`, `linux/sched/mm.h`, `linux/sched/numa_balancing.h`, `linux/highmem.h`, `linux/hugetlb.h`, `linux/mmu_notifier.h`, `linux/rmap.h`.
- Detected declarations: `struct folio_or_pfn`, `enum anon_enabled_mode`, `enum global_enabled_mode`, `enum defrag_mode`, `function file_thp_enabled`, `function vma_is_special_huge`, `function __thp_vma_allowable_orders`, `function get_huge_zero_folio`, `function put_huge_zero_folio`, `function mm_put_huge_zero_folio`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: pattern 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.