Open-Source Wikis

/

Linux

/

Subsystems

/

Memory management

torvalds/linux

Memory management

Purpose

mm/ implements every aspect of virtual and physical memory: the page allocator, the slab allocator, virtual memory areas, page cache, swap, transparent huge pages, NUMA balancing, memory cgroups, OOM, KSM, DAMON, and a number of diagnostic and hardening features.

This is one of the densest, most performance-critical, and historically conservative parts of the kernel.

Directory layout

mm/
├── Kconfig, Kconfig.debug, Makefile
├── memory.c            # Page-fault handler, vm_ops glue
├── mmap.c              # mmap, munmap, brk, mmap_lock
├── mlock.c             # mlock, mlockall
├── page_alloc.c        # The buddy / per-CPU page allocator
├── slub.c              # The slub small-object allocator (default)
├── slab_common.c       # Shared slab API
├── vmalloc.c           # vmalloc / vmap
├── vmscan.c            # Page reclaim, kswapd, the LRU list manager
├── compaction.c        # Memory compaction
├── migrate.c, migrate_device.c   # Page migration
├── memcontrol.c        # Memory cgroup
├── memory-failure.c    # Hardware-error handling
├── memory_hotplug.c    # Memory hotplug add/remove
├── mempolicy.c         # NUMA mempolicy
├── numa.c, numa_emulation.c, numa_memblks.c   # NUMA
├── huge_memory.c       # Transparent huge pages
├── hugetlb.c, hugetlb_*.c, hugetlb_cgroup.c   # hugetlbfs (explicit)
├── ksm.c               # Kernel Same-page Merging
├── khugepaged.c        # THP scanner
├── damon/              # DAMON (data-access monitor)
├── userfaultfd.c       # userfaultfd
├── shmem.c             # tmpfs / shmem
├── swap.c, swapfile.c, swap_state.c, swap_slots.c, page_io.c, swap_cgroup.c
├── filemap.c, readahead.c   # Page cache, readahead
├── truncate.c, fadvise.c
├── mremap.c, mprotect.c, msync.c, madvise.c, mincore.c
├── oom_kill.c          # OOM killer
├── page_owner.c, page_table_check.c, page_poison.c, page_idle.c
├── kasan/              # KASAN runtime
├── kfence/, kmsan/     # KFENCE, KMSAN
├── memblock.c          # Early boot memblock allocator
├── early_ioremap.c, dmapool.c, ioremap.c, util.c
├── mempool.c, percpu.c, pagewalk.c
├── zswap.c, zsmalloc.c, zpool.c, zbud.c, z3fold.c   # Compressed swap (z*)
├── workingset.c        # Refault distance accounting
└── ...

Key abstractions

Symbol File Purpose
struct page include/linux/mm_types.h Per physical page metadata.
struct folio include/linux/mm_types.h A multi-page unit (head page) used by modern VFS / filesystem code.
struct mm_struct include/linux/mm_types.h Per-process address-space state.
struct vm_area_struct (vma) include/linux/mm_types.h A contiguous virtual range with one set of permissions.
__alloc_pages() mm/page_alloc.c The page allocator entry point.
kmem_cache_alloc() mm/slub.c, mm/slab_common.c Slab allocator.
vmalloc(), vmap() mm/vmalloc.c Virtually contiguous allocations.
try_to_free_pages() mm/vmscan.c Reclaim.
handle_mm_fault() mm/memory.c Page-fault dispatcher.
mem_cgroup_charge() mm/memcontrol.c Memcg accounting.
out_of_memory() mm/oom_kill.c OOM killer.
damon_* mm/damon/ DAMON access-pattern tracker.

How it works

graph LR
    PG_FAULT[Page fault] -->|arch trap| HANDLE[handle_mm_fault]
    HANDLE --> ALLOC["__alloc_pages()"]
    ALLOC -->|fast path| PCP[Per-CPU page lists]
    ALLOC -->|slow path| BUDDY[Buddy free lists]
    BUDDY -->|reclaim if low| VMSCAN[vmscan.c]
    VMSCAN --> KSWAPD[kswapd]
    VMSCAN -->|swap pages| SWAP[swap.c]
    HANDLE -->|cache miss| FILEMAP[page cache filemap.c]
    FILEMAP -->|read| FS[fs/]
    FS -->|via bio| BLOCK[block/]

Physical-memory allocators

  • Page allocator (mm/page_alloc.c) is a buddy allocator. It manages every physical page through struct page and groups them by zone (DMA, DMA32, NORMAL, HIGHMEM where relevant) and by NUMA node. Per-CPU page sets (pcp) provide a fast-path for single-page allocations.
  • Slab allocator is slub (the default; slab was removed). Objects of fixed size live in kmem_caches; each cache has per-CPU partial slabs and a per-node list.
  • vmalloc allocates virtually contiguous, physically discontiguous memory. Used for module text, large buffers.
  • Memblock (mm/memblock.c) is the early-boot allocator used before the page allocator is up.

Virtual-memory layout

Every process has a struct mm_struct with a list/tree of vm_area_structs. Faults dispatch into handle_mm_fault() which reads the failing address from the architecture, finds the matching VMA, and dispatches to:

  • Anonymous fault → do_anonymous_page() (allocate, zero, install).
  • File-backed fault → do_fault() → calls into the filesystem vm_ops->fault.
  • Swap fault → bring page in from swap.
  • COW (copy-on-write) → duplicate the page.
  • Protection fault → check vma permissions, possibly handle write-fault.

Page cache

mm/filemap.c manages the cache of file contents in memory, indexed by (struct address_space, pgoff_t) (typically (inode_mapping, offset_in_pages)). Most file reads hit the page cache; misses go to the filesystem to populate. Folios (struct folio) are the unit increasingly used here, replacing the older "page" API in many paths.

Reclaim

mm/vmscan.c is the LRU manager. When free memory is low, kswapd or direct reclaim scans the inactive list, ages pages, and reclaims clean pages or sends dirty ones to the writeback daemon. The MGLRU (multi-generational LRU) extension is the modern algorithm; it reorganizes pages into multiple generations and uses access bit sampling.

Compaction

mm/compaction.c defragments physical memory by moving pages so that high-order allocations (huge pages) succeed. Triggered both proactively and on-demand.

Transparent Huge Pages

mm/huge_memory.c and mm/khugepaged.c. Combine 4 KiB pages into a 2 MiB (or 1 GiB) page transparently. Configuration via /sys/kernel/mm/transparent_hugepage/.

NUMA and memory policy

mm/mempolicy.c, mm/numa.c, and the autobalancing in mm/memory.c implement NUMA-aware placement. set_mempolicy(), mbind(), and numactl(1) configure per-task or per-vma policy.

Memory cgroup

mm/memcontrol.c is the memory cgroup controller — the largest controller in the kernel. Charges every page to a cgroup, supports limits, watermarks, OOM, and (with v2) memory pressure feedback.

Compressed swap

mm/zswap.c puts compressed pages into RAM (a writeback cache); mm/zsmalloc.c is a slab-like allocator for compressed objects; mm/zbud.c and mm/z3fold.c are alternatives.

DAMON

mm/damon/ is the Data Access Monitor. It samples regions of memory and produces hot/cold maps that user space can act on (e.g. proactively reclaim cold regions). damon-reclaim and damon-lru-sort are in-kernel actors.

Diagnostics and hardening

  • KASAN (mm/kasan/) — Kernel Address Sanitizer (out-of-bounds, UAF). Several variants: generic, software tags, hardware tags.
  • KFENCE (mm/kfence/) — sampling memory error detector with very low overhead.
  • KMSAN (mm/kmsan/) — Kernel Memory Sanitizer (uninitialized reads). Clang-only.
  • page_poison, page_table_check, page_owner — runtime invariants and provenance.

Integration points

  • fs/: VFS calls into filemap_* heavily. Filesystems implement address_space_operations.
  • block/: writeback / readahead funnel through here as bio submissions.
  • arch/: each arch implements pagetable types, TLB flush, large-page support, and NUMA topology callbacks.
  • kernel/cgroup/: memory cgroup is the big v2 controller.
  • drivers/iommu/, drivers/dma-buf/: integrate with mm for device address translation.
  • userfaultfd, mlock, mremap are syscalls implemented here.

Key source files

File Purpose
mm/page_alloc.c Buddy allocator.
mm/slub.c Slub slab allocator.
mm/vmalloc.c vmalloc / vmap.
mm/memory.c Page-fault dispatcher, COW, generic VMA ops.
mm/mmap.c mmap/munmap/brk, vma management.
mm/filemap.c Page cache.
mm/vmscan.c Reclaim, kswapd, MGLRU.
mm/swap.c, mm/swapfile.c Swap management.
mm/huge_memory.c Transparent huge pages.
mm/memcontrol.c Memory cgroup.
mm/oom_kill.c OOM killer.
mm/damon/ Data access monitor.
mm/userfaultfd.c userfaultfd.
mm/shmem.c tmpfs / shmem backing.

Entry points for modification

  • A new mm tunable: probably a /proc/sys/vm/ knob via mm/sysctl.c (in kernel/sysctl.c).
  • A new memory cgroup counter: extend mm/memcontrol.c and the v2 doc.
  • A new GFP flag: extremely rare; the bar is high. Read all of Documentation/core-api/memory-allocation.rst.
  • A new page operation in a filesystem: implement struct address_space_operations (in your filesystem) — mm/ calls into it via the standard hooks.
  • New allocator behavior: very high bar. Many proposed changes have been rejected; existing maintainers (Andrew Morton, Vlastimil Babka, Michal Hocko, et al.) want stress data and microbenchmarks.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Memory management – Linux wiki | Factory