__anon_vma_prepare(9)
π __anon_vma_prepare π
okay so like, this is a Kernel API Manual page for __anon_vma_prepare, and it is genuinely so important, bestie, please stay with me π«Ά
β¨ NAME β¨
__anon_vma_prepare β attach an anon_vma to a memory region π
πΈ SYNOPSIS πΈ
int __anon_vma_prepare (struct vm_area_struct *vma );
π ARGUMENTS π
vmaβ the memory region in question. that's the one we're talking about π
π¦ DESCRIPTION π¦
So the whole point of this function: it makes sure that the memory mapping described by vma has an anon_vma attached to it, so that we can associate the anonymous pages mapped into it with that anon_vma. like, it's giving matchmaker energy, fr fr π
The common case will be that we already have one β and that case is handled inline by anon_vma_prepare, no drama, no fuss π
. But if not, we either need to find an adjacent mapping that we can re-use the anon_vma from, or we allocate a new one. And bestie, the adjacent-mapping thing is very common when the only reason for splitting a vma has been mprotect β ikykw π₯
Now here's where it gets spicy. Anon-vma allocations are very subtle, because we may have optimistically looked up an anon_vma in folio_lock_anon_vma_read and that may actually touch the rwsem even in the newly allocated vma β it depends on RCU to make sure that the anon_vma isn't actually destroyed π
As a result (and this is the important part, don't skip it), we need to do proper anon_vma locking even for the new allocation. At the same time, we do not want to do any locking for the common case of already having an anon_vma β because who has the time, honestly β
ngl this one is lowkey delicate β locking is doing real work here, so don't go "optimizing" it away π
π SEE ALSO π
Kernel file ./mm/rmap.c and its whole friend group π (yes, all of them, every single one, no cap):
anon_vma_clone(9)unlink_anon_vmas(9)page_address_in_vma(9)folio_referenced(9)mapping_wrprotect_range(9)pfn_mkclean_range(9)folio_move_anon_rmap(9)__folio_set_anon(9)__page_check_anon_rmap(9)folio_add_anon_rmap_ptes(9)folio_add_anon_rmap_pmd(9)folio_add_new_anon_rmap(9)folio_add_file_rmap_ptes(9)folio_add_file_rmap_pmd(9)folio_add_file_rmap_pud(9)folio_remove_rmap_ptes(9)folio_remove_rmap_pmd(9)folio_remove_rmap_pud(9)try_to_unmap(9)try_to_migrate(9)make_device_exclusive(9)__rmap_walk_file(9)
and that's the whole page, bestie π§π this one lives in the ./mm source tree and it's dated October 2026 β go forth and attach those anon_vmas, periodt π
π
π¦ 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