__free_pages(9)
πΈ __free_pages β the kernel API manual, bestie edition πΈ
okay so this is a page from the Kernel API Manual and honestly? she's giving cleanup crew main character energy. let's get into it π
β¨ NAME β¨
__free_pages β frees the pages you allocated with alloc_pages(). that's it, that's the vibe. recycling era π
π¦ SYNOPSIS π¦
void __free_pages (struct page *page , unsigned int order );
literally two things: a struct page *page and an unsigned int order. iconic signature, no notes β¨
π ARGUMENTS π
pageβ the page pointer thatalloc_pagesgave you back. the og receipt, keep it safe π«Άorderβ the order of the allocation. yes you have to remember it. no she will not check it for you (more on that in a sec π)
π₯ DESCRIPTION π₯
so here's the tea: this function can free multi-page allocations that are not compound pages. very demure, very mindful of your non-compound situation.
β οΈ but bestie β she does not check that the order you pass in actually matches the order of the allocation. so it is easy to leak memory. and if you free more memory than was allocated, it will probably emit a warning. consider yourself warned, no cap π
now the important lore π§: if the last reference to this page is speculative, it's gonna get released by put_page, and put_page only frees the first page of a non-compound allocation. so to stop the remaining pages from being leaked, we free the subsequent pages right here. slay, problem solved β¨
but listen β if you want to use the page's reference count to decide when to free the allocation, then you should allocate a compound page, and use put_page instead of __free_pages. periodt. that's the whole rule π
π CONTEXT π
where can you call her? she may be called in interrupt context or while holding a normal spinlock. but NOT in NMI context, and NOT while holding a raw spinlock. respect the boundaries π«Ά
π SEE ALSO π
the whole friend group, in the kernel file ./mm/page_alloc.c:
__get_pfnblock_flags_mask(9)get_pfnblock_bit(9)get_pfnblock_migratetype(9)__set_pfnblock_flags_mask(9)set_pfnblock_bit(9)clear_pfnblock_bit(9)set_pageblock_migratetype(9)__move_freepages_block_isolate(9)__putback_isolated_page(9)free_pages(9)alloc_pages_exact(9)alloc_pages_exact_nid(9)free_pages_exact(9)nr_free_zone_pages(9)nr_free_buffer_pages(9)find_next_best_node(9)free_reserved_pages(9)setup_per_zone_wmarks(9)__free_contig_range(9)alloc_contig_frozen_range(9)alloc_contig_range(9)alloc_contig_frozen_pages(9)alloc_contig_pages(9)free_contig_frozen_range(9)free_contig_range(9)alloc_pages_nolock(9)
and that's the page!! π¦β¨ this lil baddie lives in ./mm, dated October 2026, and she answers to the name __free_pages(9). 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