Added _heavy_ lamport locks. A heavy lamport lock has its own dedicated lamport...
[projects/modsched/linux.git] / mm / mremap.c
1 /*
2  *      mm/mremap.c
3  *
4  *      (C) Copyright 1996 Linus Torvalds
5  *
6  *      Address space accounting code   <alan@lxorguk.ukuu.org.uk>
7  *      (C) Copyright 2002 Red Hat Inc, All Rights Reserved
8  */
9
10 #include <linux/mm.h>
11 #include <linux/hugetlb.h>
12 #include <linux/shm.h>
13 #include <linux/ksm.h>
14 #include <linux/mman.h>
15 #include <linux/swap.h>
16 #include <linux/capability.h>
17 #include <linux/fs.h>
18 #include <linux/highmem.h>
19 #include <linux/security.h>
20 #include <linux/syscalls.h>
21 #include <linux/mmu_notifier.h>
22 #include <linux/process_server.h>
23
24 #include <asm/uaccess.h>
25 #include <asm/cacheflush.h>
26 #include <asm/tlbflush.h>
27
28 #include "internal.h"
29
30 static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
31 {
32         pgd_t *pgd;
33         pud_t *pud;
34         pmd_t *pmd;
35
36         pgd = pgd_offset(mm, addr);
37         if (pgd_none_or_clear_bad(pgd))
38                 return NULL;
39
40         pud = pud_offset(pgd, addr);
41         if (pud_none_or_clear_bad(pud))
42                 return NULL;
43
44         pmd = pmd_offset(pud, addr);
45         if (pmd_none(*pmd))
46                 return NULL;
47
48         return pmd;
49 }
50
51 static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
52                             unsigned long addr)
53 {
54         pgd_t *pgd;
55         pud_t *pud;
56         pmd_t *pmd;
57
58         pgd = pgd_offset(mm, addr);
59         pud = pud_alloc(mm, pgd, addr);
60         if (!pud)
61                 return NULL;
62
63         pmd = pmd_alloc(mm, pud, addr);
64         if (!pmd)
65                 return NULL;
66
67         VM_BUG_ON(pmd_trans_huge(*pmd));
68
69         return pmd;
70 }
71
72 static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
73                 unsigned long old_addr, unsigned long old_end,
74                 struct vm_area_struct *new_vma, pmd_t *new_pmd,
75                 unsigned long new_addr)
76 {
77         struct address_space *mapping = NULL;
78         struct mm_struct *mm = vma->vm_mm;
79         pte_t *old_pte, *new_pte, pte;
80         spinlock_t *old_ptl, *new_ptl;
81
82         if (vma->vm_file) {
83                 /*
84                  * Subtle point from Rajesh Venkatasubramanian: before
85                  * moving file-based ptes, we must lock truncate_pagecache
86                  * out, since it might clean the dst vma before the src vma,
87                  * and we propagate stale pages into the dst afterward.
88                  */
89                 mapping = vma->vm_file->f_mapping;
90                 mutex_lock(&mapping->i_mmap_mutex);
91         }
92
93         /*
94          * We don't have to worry about the ordering of src and dst
95          * pte locks because exclusive mmap_sem prevents deadlock.
96          */
97         old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
98         new_pte = pte_offset_map(new_pmd, new_addr);
99         new_ptl = pte_lockptr(mm, new_pmd);
100         if (new_ptl != old_ptl)
101                 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
102         arch_enter_lazy_mmu_mode();
103
104         for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
105                                    new_pte++, new_addr += PAGE_SIZE) {
106                 if (pte_none(*old_pte))
107                         continue;
108                 pte = ptep_get_and_clear(mm, old_addr, old_pte);
109                 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
110                 set_pte_at(mm, new_addr, new_pte, pte);
111         }
112
113         arch_leave_lazy_mmu_mode();
114         if (new_ptl != old_ptl)
115                 spin_unlock(new_ptl);
116         pte_unmap(new_pte - 1);
117         pte_unmap_unlock(old_pte - 1, old_ptl);
118         if (mapping)
119                 mutex_unlock(&mapping->i_mmap_mutex);
120 }
121
122 #define LATENCY_LIMIT   (64 * PAGE_SIZE)
123
124 unsigned long move_page_tables(struct vm_area_struct *vma,
125                 unsigned long old_addr, struct vm_area_struct *new_vma,
126                 unsigned long new_addr, unsigned long len)
127 {
128         unsigned long extent, next, old_end;
129         pmd_t *old_pmd, *new_pmd;
130         bool need_flush = false;
131
132         old_end = old_addr + len;
133         flush_cache_range(vma, old_addr, old_end);
134
135         mmu_notifier_invalidate_range_start(vma->vm_mm, old_addr, old_end);
136
137         for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
138                 cond_resched();
139                 next = (old_addr + PMD_SIZE) & PMD_MASK;
140                 /* even if next overflowed, extent below will be ok */
141                 extent = next - old_addr;
142                 if (extent > old_end - old_addr)
143                         extent = old_end - old_addr;
144                 old_pmd = get_old_pmd(vma->vm_mm, old_addr);
145                 if (!old_pmd)
146                         continue;
147                 new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
148                 if (!new_pmd)
149                         break;
150                 if (pmd_trans_huge(*old_pmd)) {
151                         int err = 0;
152                         if (extent == HPAGE_PMD_SIZE)
153                                 err = move_huge_pmd(vma, new_vma, old_addr,
154                                                     new_addr, old_end,
155                                                     old_pmd, new_pmd);
156                         if (err > 0) {
157                                 need_flush = true;
158                                 continue;
159                         } else if (!err) {
160                                 split_huge_page_pmd(vma->vm_mm, old_pmd);
161                         }
162                         VM_BUG_ON(pmd_trans_huge(*old_pmd));
163                 }
164                 if (pmd_none(*new_pmd) && __pte_alloc(new_vma->vm_mm, new_vma,
165                                                       new_pmd, new_addr))
166                         break;
167                 next = (new_addr + PMD_SIZE) & PMD_MASK;
168                 if (extent > next - new_addr)
169                         extent = next - new_addr;
170                 if (extent > LATENCY_LIMIT)
171                         extent = LATENCY_LIMIT;
172                 move_ptes(vma, old_pmd, old_addr, old_addr + extent,
173                                 new_vma, new_pmd, new_addr);
174                 need_flush = true;
175         }
176         if (likely(need_flush))
177                 flush_tlb_range(vma, old_end-len, old_addr);
178
179         mmu_notifier_invalidate_range_end(vma->vm_mm, old_end-len, old_end);
180
181         return len + old_addr - old_end;        /* how much done */
182 }
183
184 static unsigned long move_vma(struct vm_area_struct *vma,
185                 unsigned long old_addr, unsigned long old_len,
186                 unsigned long new_len, unsigned long new_addr)
187 {
188         struct mm_struct *mm = vma->vm_mm;
189         struct vm_area_struct *new_vma;
190         unsigned long vm_flags = vma->vm_flags;
191         unsigned long new_pgoff;
192         unsigned long moved_len;
193         unsigned long excess = 0;
194         unsigned long hiwater_vm;
195         int split = 0;
196         int err;
197
198         /*
199          * We'd prefer to avoid failure later on in do_munmap:
200          * which may split one vma into three before unmapping.
201          */
202         if (mm->map_count >= sysctl_max_map_count - 3)
203                 return -ENOMEM;
204
205         /*
206          * Advise KSM to break any KSM pages in the area to be moved:
207          * it would be confusing if they were to turn up at the new
208          * location, where they happen to coincide with different KSM
209          * pages recently unmapped.  But leave vma->vm_flags as it was,
210          * so KSM can come around to merge on vma and new_vma afterwards.
211          */
212         err = ksm_madvise(vma, old_addr, old_addr + old_len,
213                                                 MADV_UNMERGEABLE, &vm_flags);
214         if (err)
215                 return err;
216
217         new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
218         new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
219         if (!new_vma)
220                 return -ENOMEM;
221
222         moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len);
223         if (moved_len < old_len) {
224                 /*
225                  * On error, move entries back from new area to old,
226                  * which will succeed since page tables still there,
227                  * and then proceed to unmap new area instead of old.
228                  */
229                 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len);
230                 vma = new_vma;
231                 old_len = new_len;
232                 old_addr = new_addr;
233                 new_addr = -ENOMEM;
234         }
235
236         /* Conceal VM_ACCOUNT so old reservation is not undone */
237         if (vm_flags & VM_ACCOUNT) {
238                 vma->vm_flags &= ~VM_ACCOUNT;
239                 excess = vma->vm_end - vma->vm_start - old_len;
240                 if (old_addr > vma->vm_start &&
241                     old_addr + old_len < vma->vm_end)
242                         split = 1;
243         }
244
245         /*
246          * If we failed to move page tables we still do total_vm increment
247          * since do_munmap() will decrement it by old_len == new_len.
248          *
249          * Since total_vm is about to be raised artificially high for a
250          * moment, we need to restore high watermark afterwards: if stats
251          * are taken meanwhile, total_vm and hiwater_vm appear too high.
252          * If this were a serious issue, we'd add a flag to do_munmap().
253          */
254         hiwater_vm = mm->hiwater_vm;
255         mm->total_vm += new_len >> PAGE_SHIFT;
256         vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
257
258         if (do_munmap(mm, old_addr, old_len) < 0) {
259                 /* OOM: unable to split vma, just get accounts right */
260                 vm_unacct_memory(excess >> PAGE_SHIFT);
261                 excess = 0;
262         }
263         mm->hiwater_vm = hiwater_vm;
264
265         /* Restore VM_ACCOUNT if one or two pieces of vma left */
266         if (excess) {
267                 vma->vm_flags |= VM_ACCOUNT;
268                 if (split)
269                         vma->vm_next->vm_flags |= VM_ACCOUNT;
270         }
271
272         if (vm_flags & VM_LOCKED) {
273                 mm->locked_vm += new_len >> PAGE_SHIFT;
274                 if (new_len > old_len)
275                         mlock_vma_pages_range(new_vma, new_addr + old_len,
276                                                        new_addr + new_len);
277         }
278
279         return new_addr;
280 }
281
282 static struct vm_area_struct *vma_to_resize(unsigned long addr,
283         unsigned long old_len, unsigned long new_len, unsigned long *p)
284 {
285         struct mm_struct *mm = current->mm;
286         struct vm_area_struct *vma = find_vma(mm, addr);
287
288         if (!vma || vma->vm_start > addr)
289                 goto Efault;
290
291         if (is_vm_hugetlb_page(vma))
292                 goto Einval;
293
294         /* We can't remap across vm area boundaries */
295         if (old_len > vma->vm_end - addr)
296                 goto Efault;
297
298         /* Need to be careful about a growing mapping */
299         if (new_len > old_len) {
300                 unsigned long pgoff;
301
302                 if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
303                         goto Efault;
304                 pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
305                 pgoff += vma->vm_pgoff;
306                 if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
307                         goto Einval;
308         }
309
310         if (vma->vm_flags & VM_LOCKED) {
311                 unsigned long locked, lock_limit;
312                 locked = mm->locked_vm << PAGE_SHIFT;
313                 lock_limit = rlimit(RLIMIT_MEMLOCK);
314                 locked += new_len - old_len;
315                 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
316                         goto Eagain;
317         }
318
319         if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT))
320                 goto Enomem;
321
322         if (vma->vm_flags & VM_ACCOUNT) {
323                 unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
324                 if (security_vm_enough_memory(charged))
325                         goto Efault;
326                 *p = charged;
327         }
328
329         return vma;
330
331 Efault: /* very odd choice for most of the cases, but... */
332         return ERR_PTR(-EFAULT);
333 Einval:
334         return ERR_PTR(-EINVAL);
335 Enomem:
336         return ERR_PTR(-ENOMEM);
337 Eagain:
338         return ERR_PTR(-EAGAIN);
339 }
340
341 static unsigned long mremap_to(unsigned long addr,
342         unsigned long old_len, unsigned long new_addr,
343         unsigned long new_len)
344 {
345         struct mm_struct *mm = current->mm;
346         struct vm_area_struct *vma;
347         unsigned long ret = -EINVAL;
348         unsigned long charged = 0;
349         unsigned long map_flags;
350
351         if (new_addr & ~PAGE_MASK)
352                 goto out;
353
354         if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
355                 goto out;
356
357         /* Check if the location we're moving into overlaps the
358          * old location at all, and fail if it does.
359          */
360         if ((new_addr <= addr) && (new_addr+new_len) > addr)
361                 goto out;
362
363         if ((addr <= new_addr) && (addr+old_len) > new_addr)
364                 goto out;
365
366         ret = security_file_mmap(NULL, 0, 0, 0, new_addr, 1);
367         if (ret)
368                 goto out;
369
370         ret = do_munmap(mm, new_addr, new_len);
371         if (ret)
372                 goto out;
373
374         if (old_len >= new_len) {
375                 ret = do_munmap(mm, addr+new_len, old_len - new_len);
376                 if (ret && old_len != new_len)
377                         goto out;
378                 old_len = new_len;
379         }
380
381         vma = vma_to_resize(addr, old_len, new_len, &charged);
382         if (IS_ERR(vma)) {
383                 ret = PTR_ERR(vma);
384                 goto out;
385         }
386
387         map_flags = MAP_FIXED;
388         if (vma->vm_flags & VM_MAYSHARE)
389                 map_flags |= MAP_SHARED;
390
391         ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
392                                 ((addr - vma->vm_start) >> PAGE_SHIFT),
393                                 map_flags);
394         if (ret & ~PAGE_MASK)
395                 goto out1;
396
397         ret = move_vma(vma, addr, old_len, new_len, new_addr);
398         if (!(ret & ~PAGE_MASK))
399                 goto out;
400 out1:
401         vm_unacct_memory(charged);
402
403 out:
404         return ret;
405 }
406
407 static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
408 {
409         unsigned long end = vma->vm_end + delta;
410         if (end < vma->vm_end) /* overflow */
411                 return 0;
412         if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
413                 return 0;
414         if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
415                               0, MAP_FIXED) & ~PAGE_MASK)
416                 return 0;
417         return 1;
418 }
419
420 /*
421  * Expand (or shrink) an existing mapping, potentially moving it at the
422  * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
423  *
424  * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
425  * This option implies MREMAP_MAYMOVE.
426  */
427 unsigned long do_mremap(unsigned long addr,
428         unsigned long old_len, unsigned long new_len,
429         unsigned long flags, unsigned long new_addr)
430 {
431         struct mm_struct *mm = current->mm;
432         struct vm_area_struct *vma;
433         unsigned long ret = -EINVAL;
434         unsigned long charged = 0;
435     int original_enable_distributed_munmap = current->enable_distributed_munmap;
436     unsigned long a;
437     current->enable_distributed_munmap = 0;
438
439     // This is kind of tricky.  We have to lock the old range
440     // and the new range.
441     // Also, recursion is not an issue for mremap, since 
442     // process_server does not ever attempt to do distributed
443     // remaps, it is naughty, and just does a distributed
444     // munmap (except locally).  That should probably change.
445 #ifdef PROCESS_SERVER_ENFORCE_VMA_MOD_ATOMICITY
446     up_write(&mm->mmap_sem);
447 #ifdef PROCESS_SERVER_USE_HEAVY_LOCK
448     process_server_acquire_heavy_lock();
449 #elif defined(PROCESS_SERVER_USE_DISTRIBUTED_MM_LOCK)
450     process_server_acquire_distributed_mm_lock();
451 #else 
452     {
453     unsigned long old_start = addr;
454     unsigned long old_end   = addr + old_len;
455     unsigned long new_start = new_addr;
456     unsigned long new_end   = new_addr + new_len;
457     if(old_end <= new_start || new_end <= old_start) {
458         process_server_acquire_page_lock_range(old_start,old_len);
459         process_server_acquire_page_lock_range(new_start,new_len);
460     } else {
461         unsigned long min_start = old_start < new_start? old_start : new_start;
462         unsigned long max_end   = old_end > new_end? old_end : new_end;
463         process_server_acquire_page_lock_range(min_start,max_end - min_start);
464     }
465     }
466 #endif
467     down_write(&mm->mmap_sem);
468 #endif
469
470     // Pull in all remote mappings so nothing is lost later.
471     for(a = addr & PAGE_MASK; a < addr + old_len; a+= PAGE_SIZE) {
472         struct vm_area_struct *vma_out = NULL;
473         process_server_pull_remote_mappings(current->mm,
474                                             NULL,
475                                             a,
476                                             NULL,
477                                             &vma_out,
478                                             NULL);
479
480     }
481
482         if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
483                 goto out;
484
485         if (addr & ~PAGE_MASK)
486                 goto out;
487
488         old_len = PAGE_ALIGN(old_len);
489         new_len = PAGE_ALIGN(new_len);
490
491         /*
492          * We allow a zero old-len as a special case
493          * for DOS-emu "duplicate shm area" thing. But
494          * a zero new-len is nonsensical.
495          */
496         if (!new_len)
497                 goto out;
498
499         if (flags & MREMAP_FIXED) {
500                 if (flags & MREMAP_MAYMOVE)
501                         ret = mremap_to(addr, old_len, new_addr, new_len);
502                 goto out;
503         }
504
505         /*
506          * Always allow a shrinking remap: that just unmaps
507          * the unnecessary pages..
508          * do_munmap does all the needed commit accounting
509          */
510         if (old_len >= new_len) {
511                 ret = do_munmap(mm, addr+new_len, old_len - new_len);
512                 if (ret && old_len != new_len)
513                         goto out;
514                 ret = addr;
515                 goto out;
516         }
517
518         /*
519          * Ok, we need to grow..
520          */
521         vma = vma_to_resize(addr, old_len, new_len, &charged);
522         if (IS_ERR(vma)) {
523                 ret = PTR_ERR(vma);
524                 goto out;
525         }
526
527         /* old_len exactly to the end of the area..
528          */
529         if (old_len == vma->vm_end - addr) {
530                 /* can we just expand the current mapping? */
531                 if (vma_expandable(vma, new_len - old_len)) {
532                         int pages = (new_len - old_len) >> PAGE_SHIFT;
533
534                         if (vma_adjust(vma, vma->vm_start, addr + new_len,
535                                        vma->vm_pgoff, NULL)) {
536                                 ret = -ENOMEM;
537                                 goto out;
538                         }
539
540                         mm->total_vm += pages;
541                         vm_stat_account(mm, vma->vm_flags, vma->vm_file, pages);
542                         if (vma->vm_flags & VM_LOCKED) {
543                                 mm->locked_vm += pages;
544                                 mlock_vma_pages_range(vma, addr + old_len,
545                                                    addr + new_len);
546                         }
547                         ret = addr;
548                         goto out;
549                 }
550         }
551
552         /*
553          * We weren't able to just expand or shrink the area,
554          * we need to create a new one and move it..
555          */
556         ret = -ENOMEM;
557         if (flags & MREMAP_MAYMOVE) {
558                 unsigned long map_flags = 0;
559                 if (vma->vm_flags & VM_MAYSHARE)
560                         map_flags |= MAP_SHARED;
561
562                 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
563                                         vma->vm_pgoff +
564                                         ((addr - vma->vm_start) >> PAGE_SHIFT),
565                                         map_flags);
566                 if (new_addr & ~PAGE_MASK) {
567                         ret = new_addr;
568                         goto out;
569                 }
570
571                 ret = security_file_mmap(NULL, 0, 0, 0, new_addr, 1);
572                 if (ret)
573                         goto out;
574                 ret = move_vma(vma, addr, old_len, new_len, new_addr);
575         
576         /*
577          * Multikernel - In this case, we want to unload the old mapping
578          * from all remote kernels.  To do this, pretend we're doing a munmap
579          * operation, and notify all remotes of a munmap.  If they want to access
580          * the new space, they will fault and re-acquire the mapping.
581          */
582         current->enable_distributed_munmap = original_enable_distributed_munmap;
583         process_server_do_munmap(mm, addr, old_len);
584         process_server_do_munmap(mm, new_addr, new_len);
585         current->enable_distributed_munmap = 0;
586
587         }
588 out:
589         if (ret & ~PAGE_MASK)
590                 vm_unacct_memory(charged);
591 #ifdef PROCESS_SERVER_ENFORCE_VMA_MOD_ATOMICITY
592 #ifdef PROCESS_SERVER_USE_HEAVY_LOCK
593     process_server_release_heavy_lock();
594 #elif defined(PROCESS_SERVER_USE_DISTRIBUTED_MM_LOCK)
595     process_server_release_distributed_mm_lock();
596 #else
597     {
598     unsigned long old_start = addr;
599     unsigned long old_end   = addr + old_len;
600     unsigned long new_start = new_addr;
601     unsigned long new_end   = new_addr + new_len;
602     if(old_end <= new_start || new_end <= old_start) {
603         process_server_release_page_lock_range(old_start,old_len);
604         process_server_release_page_lock_range(new_start,new_len);
605     } else {
606         unsigned long min_start = old_start < new_start? old_start : new_start;
607         unsigned long max_end   = old_end > new_end? old_end : new_end;
608         process_server_release_page_lock_range(min_start,max_end - min_start);
609     }
610
611     }
612 #endif
613 #endif
614
615     current->enable_distributed_munmap = original_enable_distributed_munmap;
616
617         return ret;
618 }
619
620 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
621                 unsigned long, new_len, unsigned long, flags,
622                 unsigned long, new_addr)
623 {
624         unsigned long ret;
625
626         down_write(&current->mm->mmap_sem);
627         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
628         up_write(&current->mm->mmap_sem);
629         return ret;
630 }