collapse_file(9)
π collapse_file β bestie, we're making a huge page
okay so this one's from the Kernel API Manual (yes, collapse_file(9), we're in the kernel-adjacent group chat now π«Ά) and it's genuinely one of those functions that sounds simple and then hits you with "details are more complex" like a plot twist.
β¨ NAME β¨
collapse_file β the girlies are collapsing filemap/tmpfs/shmem pages into one single huge page. iconic behavior, no cap. π
πΈ SYNOPSIS πΈ
enum scan_result collapse_file (struct mm_struct *mm , unsigned long
addr , struct file *file , pgoff_t start , struct collapse_control *cc
);
like, yes, that's the whole signature. type it exactly, bestie, don't get cute with it π
π ARGUMENTS π
mmβ the process address space where the collapse is happening. this is the main character's turf.addrβ the virtual collapse start address. where the magic begins, literally.fileβ the file that we're collapsing on. yes, that file.startβ collapse start address. (yes, it's also called start. they really said "two starts, one function" π just pass the right one, iykyk.)ccβ the collapse context and scratchpad. it's giving "notes app during finals" β this is where the working memory lives.
π¦ DESCRIPTION π¦
okay so the basic scheme is simple, but the details are more complex β the manpage literally says that, and honestly? respect for the honesty. π
the rundown, in order:
- allocate and lock a new huge page β¨ (fresh page, locked, ready to slay)
- scan the page cache, locking old pages + swap/gup in pages if necessary
- copy data to the new page
- handle shmem holes + re-validate that those holes weren't filled by someone else + check for
userfaultfd(this is the "wait, did someone touch my stuff" part, and yes it's as paranoid as it sounds) - finalize updates to the page cache
- if replacing succeeds:
- unlock the huge page
- free the old pages
- if replacing failed:
- unlock the old pages
- unlock and free the huge page
fr fr, do not skip the re-validation step in #4 β the whole point is that a hole might've gotten filled by someone else in the meantime, so check it, and check for userfaultfd too. that's not a vibe, that's a correctness thing. π
the way this function does a whole allocateβscanβcopyβvalidateβfinalize pipeline and then branches on success or failure at the endβ¦ that's main character energy, periodt. π₯
π SEE ALSO π
- Kernel file
./mm/khugepaged.c struct khugepaged_scan(9)collapse_max_ptes_none(9)collapse_max_ptes_shared(9)collapse_max_ptes_swap(9)collapse_pte_mapped_thp(9)
go read khugepaged.c if you want the full lore, bestie, it's right there in ./mm π§
π and that's the page! brought to you by the source ./mm, dated October 2026, and yes it's still collapse_file(9) at the bottom where the footer lives. mwah, go collapse some pages responsibly πβ¨
π¦ 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