__anon_vma_prepare(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– __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):


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