Skip to content

Commit d9d332e

Browse files
committed
anon_vma_prepare: properly lock even newly allocated entries
The anon_vma code is very subtle, and we end up doing optimistic lookups of anon_vmas under RCU in page_lock_anon_vma() with no locking. Other CPU's can also see the newly allocated entry immediately after we've exposed it by setting "vma->anon_vma" to the new value. We protect against the anon_vma being destroyed by having the SLAB marked as SLAB_DESTROY_BY_RCU, so the RCU lookup can depend on the allocation not being destroyed - but it might still be free'd and re-allocated here to a new vma. As a result, we should not do the anon_vma list ops on a newly allocated vma without proper locking. Acked-by: Nick Piggin <npiggin@suse.de> Acked-by: Hugh Dickins <hugh@veritas.com> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0cfd810 commit d9d332e

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

‎mm/rmap.c‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,51 @@
5555

5656
struct kmem_cache *anon_vma_cachep;
5757

58-
/* This must be called under the mmap_sem. */
58+
/**
59+
* anon_vma_prepare - attach an anon_vma to a memory region
60+
* @vma: the memory region in question
61+
*
62+
* This makes sure the memory mapping described by 'vma' has
63+
* an 'anon_vma' attached to it, so that we can associate the
64+
* anonymous pages mapped into it with that anon_vma.
65+
*
66+
* The common case will be that we already have one, but if
67+
* if not we either need to find an adjacent mapping that we
68+
* can re-use the anon_vma from (very common when the only
69+
* reason for splitting a vma has been mprotect()), or we
70+
* allocate a new one.
71+
*
72+
* Anon-vma allocations are very subtle, because we may have
73+
* optimistically looked up an anon_vma in page_lock_anon_vma()
74+
* and that may actually touch the spinlock even in the newly
75+
* allocated vma (it depends on RCU to make sure that the
76+
* anon_vma isn't actually destroyed).
77+
*
78+
* As a result, we need to do proper anon_vma locking even
79+
* for the new allocation. At the same time, we do not want
80+
* to do any locking for the common case of already having
81+
* an anon_vma.
82+
*
83+
* This must be called with the mmap_sem held for reading.
84+
*/
5985
int anon_vma_prepare(struct vm_area_struct *vma)
6086
{
6187
struct anon_vma *anon_vma = vma->anon_vma;
6288

6389
might_sleep();
6490
if (unlikely(!anon_vma)) {
6591
struct mm_struct *mm = vma->vm_mm;
66-
struct anon_vma *allocated, *locked;
92+
struct anon_vma *allocated;
6793

6894
anon_vma = find_mergeable_anon_vma(vma);
69-
if (anon_vma) {
70-
allocated = NULL;
71-
locked = anon_vma;
72-
spin_lock(&locked->lock);
73-
} else {
95+
allocated = NULL;
96+
if (!anon_vma) {
7497
anon_vma = anon_vma_alloc();
7598
if (unlikely(!anon_vma))
7699
return -ENOMEM;
77100
allocated = anon_vma;
78-
locked = NULL;
79101
}
102+
spin_lock(&anon_vma->lock);
80103

81104
/* page_table_lock to protect against threads */
82105
spin_lock(&mm->page_table_lock);
@@ -87,8 +110,7 @@ int anon_vma_prepare(struct vm_area_struct *vma)
87110
}
88111
spin_unlock(&mm->page_table_lock);
89112

90-
if (locked)
91-
spin_unlock(&locked->lock);
113+
spin_unlock(&anon_vma->lock);
92114
if (unlikely(allocated))
93115
anon_vma_free(allocated);
94116
}

0 commit comments

Comments
 (0)