collapse_file(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… 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:

  1. allocate and lock a new huge page ✨ (fresh page, locked, ready to slay)
  2. scan the page cache, locking old pages + swap/gup in pages if necessary
  3. copy data to the new page
  4. 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)
  5. finalize updates to the page cache
  6. if replacing succeeds:
    • unlock the huge page
    • free the old pages
  7. 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 🌈

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