mm: reuse old anon_vma if it's lost all vmas From: Konstantin Khlebnikov Signed-off-by: Konstantin Khlebnikov --- include/linux/rmap.h | 2 ++ mm/rmap.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index c0c2bce..d40ca08 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -36,6 +36,8 @@ struct anon_vma { */ atomic_t refcount; + int nr_vmas; /* Number of direct references from vmas */ + /* * NOTE: the LSB of the rb_root.rb_node is set by * mm_take_all_locks() _after_ taking the above lock. So the diff --git a/mm/rmap.c b/mm/rmap.c index 19886fb..ced4754 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -72,6 +72,7 @@ static inline struct anon_vma *anon_vma_alloc(void) anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL); if (anon_vma) { atomic_set(&anon_vma->refcount, 1); + anon_vma->nr_vmas = 1; /* * Initialise the anon_vma root to point to itself. If called * from fork, the root will be reset to the parents anon_vma. @@ -256,7 +257,11 @@ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src) anon_vma = pavc->anon_vma; root = lock_anon_vma_root(root, anon_vma); anon_vma_chain_link(dst, avc, anon_vma); + if (!dst->anon_vma && !anon_vma->nr_vmas) + dst->anon_vma = anon_vma; } + if (dst->anon_vma) + dst->anon_vma->nr_vmas++; unlock_anon_vma_root(root); return 0; @@ -279,6 +284,9 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) if (!pvma->anon_vma) return 0; + /* Drop parent anon_vma, we want find or allocate our own. */ + vma->anon_vma = NULL; + /* * First, attach the new VMA to the parent VMA's anon_vmas, * so rmap can find non-COWed pages in child processes. @@ -286,6 +294,10 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma) if (anon_vma_clone(vma, pvma)) return -ENOMEM; + /* Old anon_vma has been reused. */ + if (vma->anon_vma) + return 0; + /* Then add our own anon_vma. */ anon_vma = anon_vma_alloc(); if (!anon_vma) @@ -345,6 +357,8 @@ void unlink_anon_vmas(struct vm_area_struct *vma) list_del(&avc->same_vma); anon_vma_chain_free(avc); } + if (vma->anon_vma) + vma->anon_vma->nr_vmas--; unlock_anon_vma_root(root); /*