find_or_create_page(9)
β¨ find_or_create_page(9) β¨
okay so bestie, welcome to the Kernel API Manual π« β this one is a little kernel function called find_or_create_page and honestly it does exactly what it says on the tin. it finds a page or it creates one. icon behavior, no notes π
π NAME π
find_or_create_page β locate or add a pagecache page β¨
πΈ SYNOPSIS πΈ
struct page * find_or_create_page (struct address_space *mapping ,
pgoff_t index , gfp_t gfp_mask );
π¦ ARGUMENTS π¦
mappingβ the page'saddress_space. that's the vibe check of where this page lives πΊοΈindexβ the page's index into the mapping. it's giving "which slot in the drawer" ποΈgfp_maskβ page allocation mode. this is the "how are we allowed to grab memory" setting π±
π DESCRIPTION π
so here's the tea β: it looks up the page cache slot at mapping & offset. and if there's already a page cache page sitting there? it gets returned locked and with an increased refcount β like, it's yours now, and it's already locked and everything ππ
but if the page is not present? then a brand-new page gets allocated using gfp_mask and added to the page cache and to the VM's LRU list. that page also comes back to you locked and with an increased refcount, because we're consistent like that β¨
on memory exhaustion, NULL is returned π so likeβ¦ check for that, bestie.
β οΈ real talk:find_or_create_pagemay sleep, even ifgfp_flagsspecifies an atomic allocation! so do not come crying to me if you call it somewhere it can't sleep. that's canon, it's in the source, it's not me being dramatic π
π SEE ALSO π
kernel file ./include/linux/pagemap.h β and the whole friend group π«Ά:
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)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)folio_lock(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 π this lil gem lives in ./include/linux and the version is dated October 2026 β so yeah, find_or_create_page(9), October 2026, ./include/linux. go forth and lock your 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