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.
- Allocators - Describe in detail how dynamic memory allocation works, the trade-offs, the algorithms - essentially the what, why and how of memory allocation.
- 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
- 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.
- 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.
- Swap - Internals (the description for this chapter has been swapped out).
- Slab allocator - slab/slub (not slob), kmalloc()/kfree().
- 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.
- Memory pressure/reclaim - What memory pressure is, how it fits in with demand paging, higher order page starvation, compaction, reclaim, etc.
- Compaction and migration - How direct and indirect page compaction works, kcompactd and page migration.
- NUMA - Nodes in much more detail, NUMA rebalance, internals, etc.
- Out of memory killer - How the OOM killer works, how to decode an OOM dmesg report, analysis of code, oom_score_adj, etc.
- cgroups - How to use memory cgroups, how they are implemented internally, etc.
- 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.
- Huge pages - Why they're useful, Transparent Huge Pages/hugetlb internals, tuneables.
- Early memory management - memblock, bitmap, ELF sections marked init, transition to core memory manager, algorithms.
- Debugging the memory manager - Use of CONFIG_DEBUG_PAGEALLOC, kasan and friends to discover bugs.
- Appendix: Page table flags - A list of all page table flags with descriptions.