folio_lock(9)
π 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 theaddress_spacewhich has the lowest address in memory first. π§ Lowest address first, periodt.
π SEE ALSO π
The kernel file ./include/linux/pagemap.h, plus this whole squad π:
filemap_set_wb_err(9)filemap_check_wb_err(9)filemap_sample_wb_err(9)file_sample_sb_err(9)mapping_set_error(9)mapping_set_large_folios(9)mapping_align_index(9)mapping_pmd_folio_support(9)folio_flush_mapping(9)folio_inode(9)folio_attach_private(9)folio_change_private(9)folio_detach_private(9)fgf_t(9)fgf_set_order(9)write_begin_get_folio(9)filemap_get_folio(9)filemap_lock_folio(9)filemap_grab_folio(9)find_get_page(9)find_lock_page(9)find_or_create_page(9)grab_cache_page_nowait(9)folio_next_index(9)folio_next_pos(9)folio_file_page(9)folio_contains(9)page_pgoff(9)folio_pos(9)linear_page_delta(9)linear_page_index(9)linear_anon_page_index(9)folio_trylock(9)lock_page(9)folio_lock_killable(9)filemap_range_needs_writeback(9)struct readahead_control(9)page_cache_sync_readahead(9)page_cache_async_readahead(9)readahead_folio(9)readahead_pos(9)readahead_length(9)readahead_index(9)readahead_count(9)readahead_batch_length(9)folio_mkwrite_check_truncate(9)i_blocks_per_folio(9)
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
- 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