Lorenzo Stoakes

The Linux Memory Manager

I am writing a book about the Linux memory management subsystem which I anticipate will be ready around mid-2024. The intent is to provide as complete a description of how memory management functions in linux as I can, diving deep into the subject with a primary focus on kernel implementation.

While I had hoped to write the book in the open, I decided on balance it would be better to keep things private. I will however keep updating my development diary as work proceeds. I also maintain a twitter account which has more frequent updates.

Proposed Chapters

  1. Allocators - Describe in detail how dynamic memory allocation works, the trade-offs, the algorithms - essentially the what, why and how of memory allocation.

  2. Physical memory allocation - The buddy allocator, struct page*, struct folio*, the kernel functions that provide memory (e.g. alloc_pages()), nodes, zones (in brief, more detailed in NUMA chapter), watermarks, migrate types, page blocks, GFP flags, migrate types

  3. Virtual memory - The why, what and how, page tables, page table flags, page table sizes, virtual memory layout, direct mapping, kernel/userland split. vmalloc(), kernel process address space.

  4. Userland memory - Process VMAs, page faults, demand paging, dynamic stack allocation, how memory is copied between userland/kernel, how malloc() actually feeds into the kernel (e.g. sbrk(), mmap()). Copy-on-write, forking, basics of huge pages (covered in more detail in huge pages chapter), rmaps, pagevecs, lru + lruvecs. GUP.

  5. Swap - Internals (the description for this chapter has been swapped out).

  6. Slab allocator - slab/slub (not slob), kmalloc()/kfree().

  7. Page cache - Discussion of the page cache and how this is maintained and operated (and which tunables impact it), dirty pages, internals and interactions with issues such as memory pressure.

  8. Memory pressure/reclaim - What memory pressure is, how it fits in with demand paging, higher order page starvation, compaction, reclaim, etc.

  9. Compaction and migration - How direct and indirect page compaction works, kcompactd and page migration.

  10. NUMA - Nodes in much more detail, NUMA rebalance, internals, etc.

  11. Out of memory killer - How the OOM killer works, how to decode an OOM dmesg report, analysis of code, oom_score_adj, etc.

  12. cgroups - How to use memory cgroups, how they are implemented internally, etc.

  13. procfs and other memory management interfaces - A description of procfs/sysfs/sysrq-m interfaces and how to use and decode them, generally a practical 'how to' for sysadmins/developers. Additionally covering DAMON and (possibly?) eBPF.

  14. Huge pages - Why they're useful, Transparent Huge Pages/hugetlb internals, tuneables.

  15. Early memory management - memblock, bitmap, ELF sections marked init, transition to core memory manager, algorithms.

  16. Debugging the memory manager - Use of CONFIG_DEBUG_PAGEALLOC, kasan and friends to discover bugs.
  17. Appendix: Page table flags - A list of all page table flags with descriptions.