530f5b5e5b307ecbf8cfb3377f3b69d5b48e8d39
[projects/modsched/linux.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@lxorguk.ukuu.org.uk>
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/shm.h>
14 #include <linux/mman.h>
15 #include <linux/fs.h>
16 #include <linux/highmem.h>
17 #include <linux/security.h>
18 #include <linux/mempolicy.h>
19 #include <linux/personality.h>
20 #include <linux/syscalls.h>
21 #include <linux/swap.h>
22 #include <linux/swapops.h>
23 #include <linux/mmu_notifier.h>
24 #include <linux/migrate.h>
25 #include <linux/perf_event.h>
26 #include <linux/process_server.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/cacheflush.h>
30 #include <asm/tlbflush.h>
31
32 #ifndef pgprot_modify
33 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
34 {
35         return newprot;
36 }
37 #endif
38
39 static void change_pte_range(struct mm_struct *mm, pmd_t *pmd,
40                 unsigned long addr, unsigned long end, pgprot_t newprot,
41                 int dirty_accountable)
42 {
43         pte_t *pte, oldpte;
44         spinlock_t *ptl;
45
46         pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
47         arch_enter_lazy_mmu_mode();
48         do {
49                 oldpte = *pte;
50                 if (pte_present(oldpte)) {
51                         pte_t ptent;
52
53                         ptent = ptep_modify_prot_start(mm, addr, pte);
54                         ptent = pte_modify(ptent, newprot);
55
56                         /*
57                          * Avoid taking write faults for pages we know to be
58                          * dirty.
59                          */
60                         if (dirty_accountable && pte_dirty(ptent))
61                                 ptent = pte_mkwrite(ptent);
62
63                         ptep_modify_prot_commit(mm, addr, pte, ptent);
64                 } else if (PAGE_MIGRATION && !pte_file(oldpte)) {
65                         swp_entry_t entry = pte_to_swp_entry(oldpte);
66
67                         if (is_write_migration_entry(entry)) {
68                                 /*
69                                  * A protection check is difficult so
70                                  * just be safe and disable write
71                                  */
72                                 make_migration_entry_read(&entry);
73                                 set_pte_at(mm, addr, pte,
74                                         swp_entry_to_pte(entry));
75                         }
76                 }
77         } while (pte++, addr += PAGE_SIZE, addr != end);
78         arch_leave_lazy_mmu_mode();
79         pte_unmap_unlock(pte - 1, ptl);
80 }
81
82 static inline void change_pmd_range(struct vm_area_struct *vma, pud_t *pud,
83                 unsigned long addr, unsigned long end, pgprot_t newprot,
84                 int dirty_accountable)
85 {
86         pmd_t *pmd;
87         unsigned long next;
88
89         pmd = pmd_offset(pud, addr);
90         do {
91                 next = pmd_addr_end(addr, end);
92                 if (pmd_trans_huge(*pmd)) {
93                         if (next - addr != HPAGE_PMD_SIZE)
94                                 split_huge_page_pmd(vma->vm_mm, pmd);
95                         else if (change_huge_pmd(vma, pmd, addr, newprot))
96                                 continue;
97                         /* fall through */
98                 }
99                 if (pmd_none_or_clear_bad(pmd))
100                         continue;
101                 change_pte_range(vma->vm_mm, pmd, addr, next, newprot,
102                                  dirty_accountable);
103         } while (pmd++, addr = next, addr != end);
104 }
105
106 static inline void change_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
107                 unsigned long addr, unsigned long end, pgprot_t newprot,
108                 int dirty_accountable)
109 {
110         pud_t *pud;
111         unsigned long next;
112
113         pud = pud_offset(pgd, addr);
114         do {
115                 next = pud_addr_end(addr, end);
116                 if (pud_none_or_clear_bad(pud))
117                         continue;
118                 change_pmd_range(vma, pud, addr, next, newprot,
119                                  dirty_accountable);
120         } while (pud++, addr = next, addr != end);
121 }
122
123 static void change_protection(struct vm_area_struct *vma,
124                 unsigned long addr, unsigned long end, pgprot_t newprot,
125                 int dirty_accountable)
126 {
127         struct mm_struct *mm = vma->vm_mm;
128         pgd_t *pgd;
129         unsigned long next;
130         unsigned long start = addr;
131
132         BUG_ON(addr >= end);
133         pgd = pgd_offset(mm, addr);
134         flush_cache_range(vma, addr, end);
135         do {
136                 next = pgd_addr_end(addr, end);
137                 if (pgd_none_or_clear_bad(pgd))
138                         continue;
139                 change_pud_range(vma, pgd, addr, next, newprot,
140                                  dirty_accountable);
141         } while (pgd++, addr = next, addr != end);
142         flush_tlb_range(vma, start, end);
143 }
144
145 int
146 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
147         unsigned long start, unsigned long end, unsigned long newflags)
148 {
149         struct mm_struct *mm = vma->vm_mm;
150         unsigned long oldflags = vma->vm_flags;
151         long nrpages = (end - start) >> PAGE_SHIFT;
152         unsigned long charged = 0;
153         pgoff_t pgoff;
154         int error;
155         int dirty_accountable = 0;
156
157         if (newflags == oldflags) {
158                 *pprev = vma;
159                 return 0;
160         }
161
162         /*
163          * If we make a private mapping writable we increase our commit;
164          * but (without finer accounting) cannot reduce our commit if we
165          * make it unwritable again. hugetlb mapping were accounted for
166          * even if read-only so there is no need to account for them here
167          */
168         if (newflags & VM_WRITE) {
169                 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
170                                                 VM_SHARED|VM_NORESERVE))) {
171                         charged = nrpages;
172                         if (security_vm_enough_memory(charged))
173                                 return -ENOMEM;
174                         newflags |= VM_ACCOUNT;
175                 }
176         }
177
178         /*
179          * First try to merge with previous and/or next vma.
180          */
181         pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
182         *pprev = vma_merge(mm, *pprev, start, end, newflags,
183                         vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
184         if (*pprev) {
185                 vma = *pprev;
186                 goto success;
187         }
188
189         *pprev = vma;
190
191         if (start != vma->vm_start) {
192                 error = split_vma(mm, vma, start, 1);
193                 if (error)
194                         goto fail;
195         }
196
197         if (end != vma->vm_end) {
198                 error = split_vma(mm, vma, end, 0);
199                 if (error)
200                         goto fail;
201         }
202
203 success:
204         /*
205          * vm_flags and vm_page_prot are protected by the mmap_sem
206          * held in write mode.
207          */
208         vma->vm_flags = newflags;
209         vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
210                                           vm_get_page_prot(newflags));
211
212         if (vma_wants_writenotify(vma)) {
213                 vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
214                 dirty_accountable = 1;
215         }
216
217         mmu_notifier_invalidate_range_start(mm, start, end);
218         if (is_vm_hugetlb_page(vma))
219                 hugetlb_change_protection(vma, start, end, vma->vm_page_prot);
220         else
221                 change_protection(vma, start, end, vma->vm_page_prot, dirty_accountable);
222         mmu_notifier_invalidate_range_end(mm, start, end);
223         vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
224         vm_stat_account(mm, newflags, vma->vm_file, nrpages);
225         perf_event_mmap(vma);
226         return 0;
227
228 fail:
229         vm_unacct_memory(charged);
230         return error;
231 }
232
233 /**
234  * Multikernel.  Added this inner implementation of mprotect syscall in order to
235  * add the do_remote parameter. Also so that I could specify the task. 
236  *
237  * task - the task to operate on.  For the normal syscall, this should be "current"
238  * do_remote - 1 = ask process_server to notify all remote thread members
239  *             0 = do not
240  */
241 int do_mprotect(struct task_struct* task, struct mm_struct* mm, unsigned long start, size_t len, unsigned long prot, int do_remote) {
242         unsigned long vm_flags, nstart, end, tmp, reqprot;
243         struct vm_area_struct *vma, *prev;
244     struct mm_struct* task_mm = task? task->mm : mm;
245         int error = -EINVAL;
246     unsigned long a;
247         const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
248         prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
249         if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
250                 return -EINVAL;
251
252         if (start & ~PAGE_MASK)
253                 return -EINVAL;
254         if (!len)
255                 return 0;
256         len = PAGE_ALIGN(len);
257         end = start + len;
258         if (end <= start)
259                 return -ENOMEM;
260         if (!arch_validate_prot(prot))
261                 return -EINVAL;
262
263 #ifdef PROCESS_SERVER_ENFORCE_VMA_MOD_ATOMICITY
264     if(do_remote) {
265 #ifdef PROCESS_SERVER_USE_HEAVY_LOCK
266         process_server_acquire_heavy_lock();
267 #else
268         process_server_acquire_page_lock_range(start,len);
269 #endif
270     }
271 #endif
272
273         reqprot = prot;
274         /*
275          * Does the application expect PROT_READ to imply PROT_EXEC:
276          */
277         if (task && (prot & PROT_READ) && (task->personality & READ_IMPLIES_EXEC))
278                 prot |= PROT_EXEC;
279
280         vm_flags = calc_vm_prot_bits(prot);
281
282         down_write(&task_mm->mmap_sem);
283
284         vma = find_vma_prev(task_mm, start, &prev);
285         error = -ENOMEM;
286         if (!vma)
287                 goto out;
288         if (unlikely(grows & PROT_GROWSDOWN)) {
289                 if (vma->vm_start >= end)
290                         goto out;
291                 start = vma->vm_start;
292                 error = -EINVAL;
293                 if (!(vma->vm_flags & VM_GROWSDOWN))
294                         goto out;
295         }
296         else {
297                 if (vma->vm_start > start)
298                         goto out;
299                 if (unlikely(grows & PROT_GROWSUP)) {
300                         end = vma->vm_end;
301                         error = -EINVAL;
302                         if (!(vma->vm_flags & VM_GROWSUP))
303                                 goto out;
304                 }
305         }
306         if (start > vma->vm_start)
307                 prev = vma;
308
309         for (nstart = start ; ; ) {
310                 unsigned long newflags;
311
312                 /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
313
314                 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
315
316                 /* newflags >> 4 shift VM_MAY% in place of VM_% */
317                 if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
318                         error = -EACCES;
319                         goto out;
320                 }
321
322                 error = security_file_mprotect(vma, reqprot, prot);
323                 if (error)
324                         goto out;
325
326                 tmp = vma->vm_end;
327                 if (tmp > end)
328                         tmp = end;
329                 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
330                 if (error)
331                         goto out;
332                 nstart = tmp;
333
334                 if (nstart < prev->vm_end)
335                         nstart = prev->vm_end;
336                 if (nstart >= end)
337                         goto out;
338
339                 vma = prev->vm_next;
340                 if (!vma || vma->vm_start != nstart) {
341                         error = -ENOMEM;
342                         goto out;
343                 }
344         }
345 out:
346         up_write(&task_mm->mmap_sem);
347
348     /*
349      * Multikernel.  Change remote mappings as well before returning.
350      */
351     if(!error && do_remote && task) {
352         process_server_do_mprotect(task,start,len,prot);
353     }
354
355 #ifdef PROCESS_SERVER_ENFORCE_VMA_MOD_ATOMICITY
356     if(do_remote) {
357 #ifdef PROCESS_SERVER_USE_HEAVY_LOCK
358         process_server_release_heavy_lock();
359 #else
360         process_server_release_page_lock_range(start,len);
361 #endif
362     }
363 #endif
364
365         return error;
366
367 }
368
369 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
370                 unsigned long, prot)
371 {
372     return do_mprotect(current, current->mm, start, len, prot, 1);
373 }