update objrmap-core, add anon-vma
[linux-flexiantxendom0-3.2.10.git] / mm / mprotect.c
1 /*
2  *  mm/mprotect.c
3  *
4  *  (C) Copyright 1994 Linus Torvalds
5  *  (C) Copyright 2002 Christoph Hellwig
6  *
7  *  Address space accounting code       <alan@redhat.com>
8  *  (C) Copyright 2002 Red Hat Inc, All Rights Reserved
9  */
10
11 #include <linux/mm.h>
12 #include <linux/hugetlb.h>
13 #include <linux/slab.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
16 #include <linux/fs.h>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19
20 #include <asm/uaccess.h>
21 #include <asm/pgalloc.h>
22 #include <asm/pgtable.h>
23 #include <asm/cacheflush.h>
24 #include <asm/tlbflush.h>
25
26 static inline void
27 change_pte_range(pmd_t *pmd, unsigned long address,
28                 unsigned long size, pgprot_t newprot)
29 {
30         pte_t * pte;
31         unsigned long end;
32
33         if (pmd_none(*pmd))
34                 return;
35         if (pmd_bad(*pmd)) {
36                 pmd_ERROR(*pmd);
37                 pmd_clear(pmd);
38                 return;
39         }
40         pte = pte_offset_map(pmd, address);
41         address &= ~PMD_MASK;
42         end = address + size;
43         if (end > PMD_SIZE)
44                 end = PMD_SIZE;
45         do {
46                 if (pte_present(*pte)) {
47                         pte_t entry;
48
49                         /* Avoid an SMP race with hardware updated dirty/clean
50                          * bits by wiping the pte and then setting the new pte
51                          * into place.
52                          */
53                         entry = ptep_get_and_clear(pte);
54                         set_pte(pte, pte_modify(entry, newprot));
55                 }
56                 address += PAGE_SIZE;
57                 pte++;
58         } while (address && (address < end));
59         pte_unmap(pte - 1);
60 }
61
62 static inline void
63 change_pmd_range(pgd_t *pgd, unsigned long address,
64                 unsigned long size, pgprot_t newprot)
65 {
66         pmd_t * pmd;
67         unsigned long end;
68
69         if (pgd_none(*pgd))
70                 return;
71         if (pgd_bad(*pgd)) {
72                 pgd_ERROR(*pgd);
73                 pgd_clear(pgd);
74                 return;
75         }
76         pmd = pmd_offset(pgd, address);
77         address &= ~PGDIR_MASK;
78         end = address + size;
79         if (end > PGDIR_SIZE)
80                 end = PGDIR_SIZE;
81         do {
82                 change_pte_range(pmd, address, end - address, newprot);
83                 address = (address + PMD_SIZE) & PMD_MASK;
84                 pmd++;
85         } while (address && (address < end));
86 }
87
88 static void
89 change_protection(struct vm_area_struct *vma, unsigned long start,
90                 unsigned long end, pgprot_t newprot)
91 {
92         pgd_t *dir;
93         unsigned long beg = start;
94
95         dir = pgd_offset(current->mm, start);
96         flush_cache_range(vma, beg, end);
97         if (start >= end)
98                 BUG();
99         spin_lock(&current->mm->page_table_lock);
100         do {
101                 change_pmd_range(dir, start, end - start, newprot);
102                 start = (start + PGDIR_SIZE) & PGDIR_MASK;
103                 dir++;
104         } while (start && (start < end));
105         flush_tlb_range(vma, beg, end);
106         spin_unlock(&current->mm->page_table_lock);
107         return;
108 }
109
110 #if VMA_MERGING_FIXUP
111 /*
112  * Try to merge a vma with the previous flag, return 1 if successful or 0 if it
113  * was impossible.
114  */
115 static int
116 mprotect_attempt_merge(struct vm_area_struct *vma, struct vm_area_struct *prev,
117                 unsigned long end, int newflags)
118 {
119         struct mm_struct * mm = vma->vm_mm;
120
121         if (!prev || !vma)
122                 return 0;
123         if (prev->vm_end != vma->vm_start)
124                 return 0;
125         if (!can_vma_merge(prev, newflags))
126                 return 0;
127         if (vma->vm_file || (vma->vm_flags & VM_SHARED))
128                 return 0;
129
130         /*
131          * If the whole area changes to the protection of the previous one
132          * we can just get rid of it.
133          */
134         if (end == vma->vm_end) {
135                 spin_lock(&mm->page_table_lock);
136                 prev->vm_end = end;
137                 __vma_unlink(mm, vma, prev);
138                 spin_unlock(&mm->page_table_lock);
139
140                 kmem_cache_free(vm_area_cachep, vma);
141                 mm->map_count--;
142                 return 1;
143         } 
144
145         /*
146          * Otherwise extend it.
147          */
148         spin_lock(&mm->page_table_lock);
149         prev->vm_end = end;
150         vma->vm_start = end;
151         spin_unlock(&mm->page_table_lock);
152         return 1;
153 }
154 #endif
155
156 static int
157 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
158         unsigned long start, unsigned long end, unsigned int newflags)
159 {
160         struct mm_struct * mm = vma->vm_mm;
161         unsigned long charged = 0;
162         pgprot_t newprot;
163         int error;
164
165         if (newflags == vma->vm_flags) {
166                 *pprev = vma;
167                 return 0;
168         }
169
170         /*
171          * If we make a private mapping writable we increase our commit;
172          * but (without finer accounting) cannot reduce our commit if we
173          * make it unwritable again.
174          *
175          * FIXME? We haven't defined a VM_NORESERVE flag, so mprotecting
176          * a MAP_NORESERVE private mapping to writable will now reserve.
177          */
178         if (newflags & VM_WRITE) {
179                 if (!(vma->vm_flags & (VM_ACCOUNT|VM_WRITE|VM_SHARED))) {
180                         charged = (end - start) >> PAGE_SHIFT;
181                         if (security_vm_enough_memory(charged))
182                                 return -ENOMEM;
183                         newflags |= VM_ACCOUNT;
184                 }
185         }
186
187         newprot = protection_map[newflags & 0xf];
188
189         if (start == vma->vm_start) {
190 #if VMA_MERGING_FIXUP
191                 /*
192                  * Try to merge with the previous vma.
193                  */
194                 if (mprotect_attempt_merge(vma, *pprev, end, newflags)) {
195                         vma = *pprev;
196                         goto success;
197                 }
198 #endif
199         } else {
200                 error = split_vma(mm, vma, start, 1);
201                 if (error)
202                         goto fail;
203         }
204         /*
205          * Unless it returns an error, this function always sets *pprev to
206          * the first vma for which vma->vm_end >= end.
207          */
208         *pprev = vma;
209
210         if (end != vma->vm_end) {
211                 error = split_vma(mm, vma, end, 0);
212                 if (error)
213                         goto fail;
214         }
215
216         spin_lock(&mm->page_table_lock);
217         vma->vm_flags = newflags;
218         vma->vm_page_prot = newprot;
219         spin_unlock(&mm->page_table_lock);
220 #if VMA_MERGING_FIXUP
221 success:
222 #endif
223         change_protection(vma, start, end, newprot);
224         return 0;
225
226 fail:
227         vm_unacct_memory(charged);
228         return error;
229 }
230
231 long
232 do_mprotect(struct mm_struct *mm, unsigned long start, size_t len, 
233              unsigned long prot)
234 {
235         unsigned long vm_flags, nstart, end, tmp;
236         struct vm_area_struct * vma, * next, * prev;
237         int error = -EINVAL;
238         const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
239         prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
240         if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
241                 return -EINVAL;
242
243         if (start & ~PAGE_MASK)
244                 return -EINVAL;
245         len = PAGE_ALIGN(len);
246         end = start + len;
247         if (end < start)
248                 return -EINVAL;
249         if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
250                 return -EINVAL;
251         if (end == start)
252                 return 0;
253
254         vm_flags = calc_vm_prot_bits(prot);
255
256         down_write(&mm->mmap_sem);
257
258         vma = find_vma_prev(mm, start, &prev);
259         error = -ENOMEM;
260         if (!vma)
261                 goto out;
262         if (unlikely(grows & PROT_GROWSDOWN)) {
263                 if (vma->vm_start >= end)
264                         goto out;
265                 start = vma->vm_start;
266                 error = -EINVAL;
267                 if (!(vma->vm_flags & VM_GROWSDOWN))
268                         goto out;
269         }
270         else {
271                 if (vma->vm_start > start)
272                         goto out;
273                 if (unlikely(grows & PROT_GROWSUP)) {
274                         end = vma->vm_end;
275                         error = -EINVAL;
276                         if (!(vma->vm_flags & VM_GROWSUP))
277                                 goto out;
278                 }
279         }
280
281         for (nstart = start ; ; ) {
282                 unsigned int newflags;
283                 int last = 0;
284
285                 /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
286
287                 if (is_vm_hugetlb_page(vma)) {
288                         error = -EACCES;
289                         goto out;
290                 }
291
292                 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
293
294                 if ((newflags & ~(newflags >> 4)) & 0xf) {
295                         error = -EACCES;
296                         goto out;
297                 }
298
299                 error = security_file_mprotect(vma, prot);
300                 if (error)
301                         goto out;
302
303                 if (vma->vm_end > end) {
304                         error = mprotect_fixup(vma, &prev, nstart, end, newflags);
305                         goto out;
306                 }
307                 if (vma->vm_end == end)
308                         last = 1;
309
310                 tmp = vma->vm_end;
311                 next = vma->vm_next;
312                 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
313                 if (error)
314                         goto out;
315                 if (last)
316                         break;
317                 nstart = tmp;
318                 vma = next;
319                 if (!vma || vma->vm_start != nstart) {
320                         error = -ENOMEM;
321                         goto out;
322                 }
323         }
324
325 #if VMA_MERGING_FIXUP
326         if (next && prev->vm_end == next->vm_start &&
327                         can_vma_merge(next, prev->vm_flags) &&
328                         !prev->vm_file && !(prev->vm_flags & VM_SHARED)) {
329                 spin_lock(&prev->vm_mm->page_table_lock);
330                 prev->vm_end = next->vm_end;
331                 __vma_unlink(prev->vm_mm, next, prev);
332                 spin_unlock(&prev->vm_mm->page_table_lock);
333
334                 kmem_cache_free(vm_area_cachep, next);
335                 prev->vm_mm->map_count--;
336         }
337 #endif
338 out:
339         up_write(&mm->mmap_sem);
340         return error;
341 }
342
343 asmlinkage long sys_mprotect(unsigned long start, size_t len, unsigned long prot)
344 {
345         return(do_mprotect(current->mm, start, len, prot));
346 }