Added _heavy_ lamport locks. A heavy lamport lock has its own dedicated lamport...
[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         //printk("%s: doing lock\n",__func__);
266 #ifdef PROCESS_SERVER_USE_HEAVY_LOCK
267         process_server_acquire_heavy_lock();
268 #elif defined(PROCESS_SERVER_USE_DISTRIBUTED_MM_LOCK)
269         process_server_acquire_distributed_mm_lock();
270 #else
271         process_server_acquire_page_lock_range(start,len);
272 #endif
273     }
274 #endif
275
276         reqprot = prot;
277         /*
278          * Does the application expect PROT_READ to imply PROT_EXEC:
279          */
280         if (task && (prot & PROT_READ) && (task->personality & READ_IMPLIES_EXEC))
281                 prot |= PROT_EXEC;
282
283         vm_flags = calc_vm_prot_bits(prot);
284
285         down_write(&task_mm->mmap_sem);
286
287         vma = find_vma_prev(task_mm, start, &prev);
288         error = -ENOMEM;
289         if (!vma)
290                 goto out;
291         if (unlikely(grows & PROT_GROWSDOWN)) {
292                 if (vma->vm_start >= end)
293                         goto out;
294                 start = vma->vm_start;
295                 error = -EINVAL;
296                 if (!(vma->vm_flags & VM_GROWSDOWN))
297                         goto out;
298         }
299         else {
300                 if (vma->vm_start > start)
301                         goto out;
302                 if (unlikely(grows & PROT_GROWSUP)) {
303                         end = vma->vm_end;
304                         error = -EINVAL;
305                         if (!(vma->vm_flags & VM_GROWSUP))
306                                 goto out;
307                 }
308         }
309         if (start > vma->vm_start)
310                 prev = vma;
311
312         for (nstart = start ; ; ) {
313                 unsigned long newflags;
314
315                 /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
316
317                 newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
318
319                 /* newflags >> 4 shift VM_MAY% in place of VM_% */
320                 if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
321                         error = -EACCES;
322                         goto out;
323                 }
324
325                 error = security_file_mprotect(vma, reqprot, prot);
326                 if (error)
327                         goto out;
328
329                 tmp = vma->vm_end;
330                 if (tmp > end)
331                         tmp = end;
332                 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
333                 if (error)
334                         goto out;
335                 nstart = tmp;
336
337                 if (nstart < prev->vm_end)
338                         nstart = prev->vm_end;
339                 if (nstart >= end)
340                         goto out;
341
342                 vma = prev->vm_next;
343                 if (!vma || vma->vm_start != nstart) {
344                         error = -ENOMEM;
345                         goto out;
346                 }
347         }
348 out:
349         up_write(&task_mm->mmap_sem);
350
351     /*
352      * Multikernel.  Change remote mappings as well before returning.
353      */
354     if(!error && do_remote && task) {
355         process_server_do_mprotect(task,start,len,prot);
356     }
357
358 #ifdef PROCESS_SERVER_ENFORCE_VMA_MOD_ATOMICITY
359     if(do_remote) {
360 #ifdef PROCESS_SERVER_USE_HEAVY_LOCK
361         process_server_release_heavy_lock();
362 #elif defined(PROCESS_SERVER_USE_DISTRIBUTED_MM_LOCK)
363         process_server_release_distributed_mm_lock();
364 #else
365         process_server_release_page_lock_range(start,len);
366 #endif
367     }
368 #endif
369
370         return error;
371
372 }
373
374 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
375                 unsigned long, prot)
376 {
377     return do_mprotect(current, current->mm, start, len, prot, 1);
378 }