MEMBLOCK OVERVIEW(9)
π memblock overview(9) π
Kernel API Manual Β· the one where memory gets managed before anything else is ready
Okay so picture this: it's early boot, the usual kernel memory allocators are not up and running yet β like, nobody's at the party, the lights are off, the DJ hasn't shown. And memblock is there anyway. Memblock is literally the method of managing memory regions during the early boot period, before the real allocators wake up. Main character energy fr. π«Ά
β¨ the collections β¨
Memblock views the system memory as collections of contiguous regions. There are several types of these collections:
memoryβ describes the physical memory available to the kernel. this may differ from the actual physical memory installed in the system, for instance when the memory is restricted withmem=command line parameterreservedβ describes the regions that were allocatedphysmemβ describes the actual physical memory available during boot regardless of the possible restrictions and memory hot(un)plug; thephysmemtype is only available on some architectures
πΈ the structs, iykyk πΈ
Each region is represented by struct memblock_region, which defines the region extents, its attributes and NUMA node id on NUMA systems. Every memory type is described by the struct memblock_type, which contains an array of memory regions along with the allocator metadata. The memory and reserved types are nicely wrapped with struct memblock β cute, tidy, wrapped up. π
This structure is statically initialized at build time. The region arrays are initially sized to INIT_MEMBLOCK_MEMORY_REGIONS for memory and INIT_MEMBLOCK_RESERVED_REGIONS for reserved. The region array for physmem is initially sized to INIT_PHYSMEM_REGIONS.
Then there's memblock_allow_resize, which enables automatic resizing of the region arrays during addition of new regions.
β οΈ bestie, this feature should be used with care so that memory allocated for the region array will not overlap with areas that should be reserved, for example initrd. like pls be careful, the vibes are delicate here.
π¦ the early setup era π¦
The early architecture setup should tell memblock what the physical memory layout is by using memblock_add or memblock_add_node functions.
The first function does not assign the region to a NUMA node and it is appropriate for UMA systems. Yet β and this is the slay part β it is possible to use it on NUMA systems as well, and assign the region to a NUMA node later in the setup process using memblock_set_node. The memblock_add_node performs such an assignment directly. no extra steps, no notes app, just done. π
π₯ allocating: pick your variant π₯
Once memblock is setup, the memory can be allocated using one of the API variants:
memblock_phys_alloc*()β these functions return the physical address of the allocated memorymemblock_alloc*()β these functions return the virtual address of the allocated memory
Note that both API variants use implicit assumptions about allowed memory ranges and the fallback methods. Consult the documentation of memblock_alloc_internal and memblock_alloc_range_nid functions for more elaborate description β they said it best, go read them. π
π§ the inevitable ending π§
As the system boot progresses, the architecture specific mem_init function frees all the memory to the buddy page allocator. passing the torch, iconic behaviour. β¨
Unless an architecture enables CONFIG_ARCH_KEEP_MEMBLOCK, the memblock data structures (except physmem) will be discarded after the system initialization completes. gone but not forgotten ngl π
β¨ SEE ALSO β¨
Kernel file ./mm/memblock.c β and literally all of these, go wild:
__memblock_find_range_bottom_up(9)__memblock_find_range_top_down(9)memblock_find_in_range_node(9)memblock_find_in_range(9)memblock_discard(9)memblock_double_array(9)memblock_merge_regions(9)memblock_insert_region(9)memblock_add_range(9)memblock_add_node(9)memblock_add(9)memblock_validate_numa_coverage(9)memblock_isolate_range(9)memblock_free(9)memblock_phys_free(9)memblock_setclr_flag(9)memblock_mark_hotplug(9)memblock_clear_hotplug(9)memblock_mark_mirror(9)memblock_mark_nomap(9)memblock_clear_nomap(9)memblock_reserved_mark_noinit(9)memblock_reserved_mark_kern(9)memblock_mark_kho_scratch(9)memblock_clear_kho_scratch(9)__next_mem_range(9)__next_mem_range_rev(9)memblock_set_node(9)memblock_alloc_range_nid(9)memblock_phys_alloc_range(9)memblock_phys_alloc_try_nid(9)memblock_alloc_internal(9)memblock_alloc_exact_nid_raw(9)memblock_alloc_try_nid_raw(9)memblock_alloc_hugetlb(9)memblock_alloc_try_nid(9)__memblock_alloc_or_panic(9)memblock_estimated_nr_free_pages(9)memblock_is_region_memory(9)memblock_is_region_reserved(9)memblock_free_all(9)reserve_mem_find_by_name(9)reserve_mem_release_by_name(9)
and that's the whole page bestie π this one's from ./mm, dated October 2026, and it's called memblock overview(9) β go forth and manage your early boot memory, ily, mwah π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms