__free_pages(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

🌸 __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 that alloc_pages gave 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:


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