__next_mem_range(9)
π« __next_mem_range β the kernel API manual, but make it make sense π«
okay bestie, so this is a page from the Kernel API Manual (the <italic>9</italic> section, iykyk) and today's main character is __next_mem_range. she's a helper function, she's lowkey the engine behind for_each_free_mem_range() and friends, and honestly? iconic. let's get into it π«Ά
β¨ NAME β¨
__next_mem_range β the next function for for_each_free_mem_range() etc.
like, literally the "give me the next one" function. that's the vibe. π
πΈ SYNOPSIS πΈ
void __next_mem_range (u64 *idx , int nid , enum memblock_flags flags ,
struct memblock_type *type_a , struct memblock_type *type_b ,
phys_addr_t *out_start , phys_addr_t *out_end , int *out_nid );
she returns void, so don't go looking for a return value, she gives you her answers through the out-params instead. main character energy fr. π
π¦ ARGUMENTS π¦
idxβ pointer to au64loop variable. this is the tracker, the little bookmark that remembers where we're up to. β¨nidβ the node selector. passNUMA_NO_NODEif you want all nodes, no filter, everybody welcome. πflagsβ pick which blocks you want based on their memory attributes. it's giving "choose your fighter". πtype_aβ pointer to thememblock_typethat the range is taken from. the source, the origin story. πtype_bβ pointer to thememblock_typethat excludes memory from being taken. the bouncer at the door, ngl. πout_startβ ptr to aphys_addr_tfor the start address of the range. can beNULL. βout_endβ ptr to aphys_addr_tfor the end address of the range. can beNULL. βout_nidβ ptr to anintfor the nid of the range. can beNULL. β
yes bestie, all three out-params are optional β if you don't need one, just pass NULL and she won't be offended π«Ά
π₯ DESCRIPTION π₯
so what does she actually do? she finds the first area starting from *idx that matches nid, fills in all those out parameters for you, and then updates *idx so the next iteration knows where to pick up. it's a whole relay race and she's passing the baton. πββοΈπ¨
now here's the crunchy part, and you're gonna want to focus for this one:
the lower 32 bits of *idx contain the index into type_a, and the upper 32 bits index the areas before each region in type_b. two indexes, one u64, no cap. π
for example, if the type_b regions look like this:
0:[0-16), 1:[32-48), 2:[128-130)
then the upper 32 bits index these regions:
0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
see how the gaps are the whole point? she's mapping the spaces between the type_b regions. the negative space. the lore. π¦β¨
and because both region arrays are sorted, the function advances the two indices in lockstep and returns each intersection. sorted input, synchronized walking, intersection delivered. she ate and left absolutely no crumbs. π
π SEE ALSO π
okay this is the part where the original page just goes absolutely feral with the cross-references, and I respect it too much to trim a single one. here's the whole squad:
- Kernel file
./mm/memblock.cmemblock overview(9) __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_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)
ngl that's a lot of friends, but every single one of them is invited to the group chat ππ
and that's the page!! this one came out of the ./mm source, dated October 2026, so go off and go read the memblock code bestie, she's waiting for you πΈβ¨ππ¦π
π¦ 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