diff options
-rw-r--r-- | mm/rmap.c | 42 |
1 files changed, 32 insertions, 10 deletions
@@ -55,7 +55,33 @@ | |||
55 | 55 | ||
56 | struct kmem_cache *anon_vma_cachep; | 56 | struct kmem_cache *anon_vma_cachep; |
57 | 57 | ||
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 | */ | ||
59 | int anon_vma_prepare(struct vm_area_struct *vma) | 85 | int anon_vma_prepare(struct vm_area_struct *vma) |
60 | { | 86 | { |
61 | struct anon_vma *anon_vma = vma->anon_vma; | 87 | struct anon_vma *anon_vma = vma->anon_vma; |
@@ -63,20 +89,17 @@ int anon_vma_prepare(struct vm_area_struct *vma) | |||
63 | might_sleep(); | 89 | might_sleep(); |
64 | if (unlikely(!anon_vma)) { | 90 | if (unlikely(!anon_vma)) { |
65 | struct mm_struct *mm = vma->vm_mm; | 91 | struct mm_struct *mm = vma->vm_mm; |
66 | struct anon_vma *allocated, *locked; | 92 | struct anon_vma *allocated; |
67 | 93 | ||
68 | anon_vma = find_mergeable_anon_vma(vma); | 94 | anon_vma = find_mergeable_anon_vma(vma); |
69 | if (anon_vma) { | 95 | allocated = NULL; |
70 | allocated = NULL; | 96 | if (!anon_vma) { |
71 | locked = anon_vma; | ||
72 | spin_lock(&locked->lock); | ||
73 | } else { | ||
74 | anon_vma = anon_vma_alloc(); | 97 | anon_vma = anon_vma_alloc(); |
75 | if (unlikely(!anon_vma)) | 98 | if (unlikely(!anon_vma)) |
76 | return -ENOMEM; | 99 | return -ENOMEM; |
77 | allocated = anon_vma; | 100 | allocated = anon_vma; |
78 | locked = NULL; | ||
79 | } | 101 | } |
102 | spin_lock(&anon_vma->lock); | ||
80 | 103 | ||
81 | /* page_table_lock to protect against threads */ | 104 | /* page_table_lock to protect against threads */ |
82 | spin_lock(&mm->page_table_lock); | 105 | spin_lock(&mm->page_table_lock); |
@@ -87,8 +110,7 @@ int anon_vma_prepare(struct vm_area_struct *vma) | |||
87 | } | 110 | } |
88 | spin_unlock(&mm->page_table_lock); | 111 | spin_unlock(&mm->page_table_lock); |
89 | 112 | ||
90 | if (locked) | 113 | spin_unlock(&anon_vma->lock); |
91 | spin_unlock(&locked->lock); | ||
92 | if (unlikely(allocated)) | 114 | if (unlikely(allocated)) |
93 | anon_vma_free(allocated); | 115 | anon_vma_free(allocated); |
94 | } | 116 | } |