folio_lock(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… folio_lock β€” from the Kernel API Manual, bestie ✨

Okay so first things first, this page lives in the Kernel API Manual (section 9, the kernel-internals girlies πŸŽ€), and the star of the show is folio_lock(9). Buckle up.


✨ NAME ✨

folio_lock β€” literally just locks this folio. That's the whole vibe. πŸ”’


🌸 SYNOPSIS 🌸

void folio_lock (struct folio *folio );

πŸ¦‹ ARGUMENTS πŸ¦‹

  • folio β€” the folio you wanna lock. That's it. Hand it over, it's getting locked. πŸ’…

πŸ‘‘ DESCRIPTION πŸ‘‘

Okay so the folio lock protects against… a lot of things. Honestly? Probably more than it should 😭 β€” it's giving overachiever, it's giving main character energy, we love her anyway.

The big one: it's primarily held while a folio is being brought uptodate, whether that's from its backing file or from swap. ✨

It's also held while a folio is being truncated from its address_space β€” so the way that works is, if you're holding the lock, that's sufficient to keep folio->mapping stable. No cap. 🫢

And not done yet! The folio lock is also held while write is modifying the page, which is what gives you the POSIX atomicity guarantees β€” but only as long as the write does not cross a page boundary. That caveat is doing a lot of heavy lifting, so don't forget it. πŸ’€

Heads up bestie: other modifications to the data in the folio do not hold the folio lock, and they can totally race with writes β€” we're talking eg DMA and stores to mapped pages. So like… be aware. πŸ™ˆ

πŸ”₯ CONTEXT πŸ”₯

  • May sleep. 😴 So, y'know, don't do this in a context where you can't sleep. (iykyk)
  • If you need to acquire the locks of two or more folios, they must be in order of ascending index β€” but only if they're in the same address_space.
  • If they're in different address_spaces, acquire the lock of the folio which belongs to the address_space which has the lowest address in memory first. 🧚 Lowest address first, periodt.

πŸ’– SEE ALSO πŸ’–

The kernel file ./include/linux/pagemap.h, plus this whole squad πŸ‘‘:


and that's the whole page, bestie πŸ³οΈβ€πŸŒˆπŸ’… β€” it ships from ./include/linux and this version is dated October 2026. Go forth and lock responsibly πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from