find_or_create_page(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

✨ 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's address_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_page may sleep, even if gfp_flags specifies 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 🫢:


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