OSDN Git Service

hugetlbfs: fix kernel BUG at fs/hugetlbfs/inode.c:444!
[sagit-ice-cold/kernel_xiaomi_msm8998.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/swapops.h>
19 #include <linux/highmem.h>
20 #include <linux/security.h>
21 #include <linux/syscalls.h>
22 #include <linux/mmu_notifier.h>
23 #include <linux/sched/sysctl.h>
24 #include <linux/uaccess.h>
25 #include <linux/mm-arch-hooks.h>
26
27 #include <asm/cacheflush.h>
28 #include <asm/tlbflush.h>
29
30 #include "internal.h"
31
32 static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
33 {
34         pgd_t *pgd;
35         pud_t *pud;
36         pmd_t *pmd;
37
38         pgd = pgd_offset(mm, addr);
39         if (pgd_none_or_clear_bad(pgd))
40                 return NULL;
41
42         pud = pud_offset(pgd, addr);
43         if (pud_none_or_clear_bad(pud))
44                 return NULL;
45
46         pmd = pmd_offset(pud, addr);
47         if (pmd_none(*pmd))
48                 return NULL;
49
50         return pmd;
51 }
52
53 static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
54                             unsigned long addr)
55 {
56         pgd_t *pgd;
57         pud_t *pud;
58         pmd_t *pmd;
59
60         pgd = pgd_offset(mm, addr);
61         pud = pud_alloc(mm, pgd, addr);
62         if (!pud)
63                 return NULL;
64
65         pmd = pmd_alloc(mm, pud, addr);
66         if (!pmd)
67                 return NULL;
68
69         VM_BUG_ON(pmd_trans_huge(*pmd));
70
71         return pmd;
72 }
73
74 static pte_t move_soft_dirty_pte(pte_t pte)
75 {
76         /*
77          * Set soft dirty bit so we can notice
78          * in userspace the ptes were moved.
79          */
80 #ifdef CONFIG_MEM_SOFT_DIRTY
81         if (pte_present(pte))
82                 pte = pte_mksoft_dirty(pte);
83         else if (is_swap_pte(pte))
84                 pte = pte_swp_mksoft_dirty(pte);
85 #endif
86         return pte;
87 }
88
89 static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
90                 unsigned long old_addr, unsigned long old_end,
91                 struct vm_area_struct *new_vma, pmd_t *new_pmd,
92                 unsigned long new_addr, bool need_rmap_locks)
93 {
94         struct address_space *mapping = NULL;
95         struct anon_vma *anon_vma = NULL;
96         struct mm_struct *mm = vma->vm_mm;
97         pte_t *old_pte, *new_pte, pte;
98         spinlock_t *old_ptl, *new_ptl;
99         bool force_flush = false;
100         unsigned long len = old_end - old_addr;
101
102         /*
103          * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
104          * locks to ensure that rmap will always observe either the old or the
105          * new ptes. This is the easiest way to avoid races with
106          * truncate_pagecache(), page migration, etc...
107          *
108          * When need_rmap_locks is false, we use other ways to avoid
109          * such races:
110          *
111          * - During exec() shift_arg_pages(), we use a specially tagged vma
112          *   which rmap call sites look for using is_vma_temporary_stack().
113          *
114          * - During mremap(), new_vma is often known to be placed after vma
115          *   in rmap traversal order. This ensures rmap will always observe
116          *   either the old pte, or the new pte, or both (the page table locks
117          *   serialize access to individual ptes, but only rmap traversal
118          *   order guarantees that we won't miss both the old and new ptes).
119          */
120         if (need_rmap_locks) {
121                 if (vma->vm_file) {
122                         mapping = vma->vm_file->f_mapping;
123                         i_mmap_lock_write(mapping);
124                 }
125                 if (vma->anon_vma) {
126                         anon_vma = vma->anon_vma;
127                         anon_vma_lock_write(anon_vma);
128                 }
129         }
130
131         /*
132          * We don't have to worry about the ordering of src and dst
133          * pte locks because exclusive mmap_sem prevents deadlock.
134          */
135         old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
136         new_pte = pte_offset_map(new_pmd, new_addr);
137         new_ptl = pte_lockptr(mm, new_pmd);
138         if (new_ptl != old_ptl)
139                 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
140         flush_tlb_batched_pending(vma->vm_mm);
141         arch_enter_lazy_mmu_mode();
142
143         for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
144                                    new_pte++, new_addr += PAGE_SIZE) {
145                 if (pte_none(*old_pte))
146                         continue;
147                 pte = ptep_get_and_clear(mm, old_addr, old_pte);
148                 /*
149                  * If we are remapping a valid PTE, make sure
150                  * to flush TLB before we drop the PTL for the PTE.
151                  *
152                  * NOTE! Both old and new PTL matter: the old one
153                  * for racing with page_mkclean(), the new one to
154                  * make sure the physical page stays valid until
155                  * the TLB entry for the old mapping has been
156                  * flushed.
157                  */
158                 if (pte_present(pte))
159                         force_flush = true;
160                 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
161                 pte = move_soft_dirty_pte(pte);
162                 set_pte_at(mm, new_addr, new_pte, pte);
163         }
164
165         arch_leave_lazy_mmu_mode();
166         if (force_flush)
167                 flush_tlb_range(vma, old_end - len, old_end);
168         if (new_ptl != old_ptl)
169                 spin_unlock(new_ptl);
170         pte_unmap(new_pte - 1);
171         pte_unmap_unlock(old_pte - 1, old_ptl);
172         if (anon_vma)
173                 anon_vma_unlock_write(anon_vma);
174         if (mapping)
175                 i_mmap_unlock_write(mapping);
176 }
177
178 #define LATENCY_LIMIT   (64 * PAGE_SIZE)
179
180 unsigned long move_page_tables(struct vm_area_struct *vma,
181                 unsigned long old_addr, struct vm_area_struct *new_vma,
182                 unsigned long new_addr, unsigned long len,
183                 bool need_rmap_locks)
184 {
185         unsigned long extent, next, old_end;
186         pmd_t *old_pmd, *new_pmd;
187         unsigned long mmun_start;       /* For mmu_notifiers */
188         unsigned long mmun_end;         /* For mmu_notifiers */
189
190         old_end = old_addr + len;
191         flush_cache_range(vma, old_addr, old_end);
192
193         mmun_start = old_addr;
194         mmun_end   = old_end;
195         mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
196
197         for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
198                 cond_resched();
199                 next = (old_addr + PMD_SIZE) & PMD_MASK;
200                 /* even if next overflowed, extent below will be ok */
201                 extent = next - old_addr;
202                 if (extent > old_end - old_addr)
203                         extent = old_end - old_addr;
204                 old_pmd = get_old_pmd(vma->vm_mm, old_addr);
205                 if (!old_pmd)
206                         continue;
207                 new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
208                 if (!new_pmd)
209                         break;
210                 if (pmd_trans_huge(*old_pmd)) {
211                         int err = 0;
212                         if (extent == HPAGE_PMD_SIZE) {
213                                 VM_BUG_ON_VMA(vma->vm_file || !vma->anon_vma,
214                                               vma);
215                                 /* See comment in move_ptes() */
216                                 if (need_rmap_locks)
217                                         anon_vma_lock_write(vma->anon_vma);
218                                 err = move_huge_pmd(vma, new_vma, old_addr,
219                                                     new_addr, old_end,
220                                                     old_pmd, new_pmd);
221                                 if (need_rmap_locks)
222                                         anon_vma_unlock_write(vma->anon_vma);
223                         }
224                         if (err > 0) {
225                                 continue;
226                         } else if (!err) {
227                                 split_huge_page_pmd(vma, old_addr, old_pmd);
228                         }
229                         VM_BUG_ON(pmd_trans_huge(*old_pmd));
230                 }
231                 if (pmd_none(*new_pmd) && __pte_alloc(new_vma->vm_mm, new_vma,
232                                                       new_pmd, new_addr))
233                         break;
234                 next = (new_addr + PMD_SIZE) & PMD_MASK;
235                 if (extent > next - new_addr)
236                         extent = next - new_addr;
237                 if (extent > LATENCY_LIMIT)
238                         extent = LATENCY_LIMIT;
239                 move_ptes(vma, old_pmd, old_addr, old_addr + extent,
240                           new_vma, new_pmd, new_addr, need_rmap_locks);
241         }
242
243         mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
244
245         return len + old_addr - old_end;        /* how much done */
246 }
247
248 static unsigned long move_vma(struct vm_area_struct *vma,
249                 unsigned long old_addr, unsigned long old_len,
250                 unsigned long new_len, unsigned long new_addr, bool *locked)
251 {
252         struct mm_struct *mm = vma->vm_mm;
253         struct vm_area_struct *new_vma;
254         unsigned long vm_flags = vma->vm_flags;
255         unsigned long new_pgoff;
256         unsigned long moved_len;
257         unsigned long excess = 0;
258         unsigned long hiwater_vm;
259         int split = 0;
260         int err;
261         bool need_rmap_locks;
262
263         /*
264          * We'd prefer to avoid failure later on in do_munmap:
265          * which may split one vma into three before unmapping.
266          */
267         if (mm->map_count >= sysctl_max_map_count - 3)
268                 return -ENOMEM;
269
270         /*
271          * Advise KSM to break any KSM pages in the area to be moved:
272          * it would be confusing if they were to turn up at the new
273          * location, where they happen to coincide with different KSM
274          * pages recently unmapped.  But leave vma->vm_flags as it was,
275          * so KSM can come around to merge on vma and new_vma afterwards.
276          */
277         err = ksm_madvise(vma, old_addr, old_addr + old_len,
278                                                 MADV_UNMERGEABLE, &vm_flags);
279         if (err)
280                 return err;
281
282         new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
283         new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
284                            &need_rmap_locks);
285         if (!new_vma)
286                 return -ENOMEM;
287
288         moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
289                                      need_rmap_locks);
290         if (moved_len < old_len) {
291                 err = -ENOMEM;
292         } else if (vma->vm_ops && vma->vm_ops->mremap) {
293                 err = vma->vm_ops->mremap(new_vma);
294         }
295
296         if (unlikely(err)) {
297                 /*
298                  * On error, move entries back from new area to old,
299                  * which will succeed since page tables still there,
300                  * and then proceed to unmap new area instead of old.
301                  */
302                 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
303                                  true);
304                 vma = new_vma;
305                 old_len = new_len;
306                 old_addr = new_addr;
307                 new_addr = err;
308         } else {
309                 arch_remap(mm, old_addr, old_addr + old_len,
310                            new_addr, new_addr + new_len);
311         }
312
313         /* Conceal VM_ACCOUNT so old reservation is not undone */
314         if (vm_flags & VM_ACCOUNT) {
315                 vma->vm_flags &= ~VM_ACCOUNT;
316                 excess = vma->vm_end - vma->vm_start - old_len;
317                 if (old_addr > vma->vm_start &&
318                     old_addr + old_len < vma->vm_end)
319                         split = 1;
320         }
321
322         /*
323          * If we failed to move page tables we still do total_vm increment
324          * since do_munmap() will decrement it by old_len == new_len.
325          *
326          * Since total_vm is about to be raised artificially high for a
327          * moment, we need to restore high watermark afterwards: if stats
328          * are taken meanwhile, total_vm and hiwater_vm appear too high.
329          * If this were a serious issue, we'd add a flag to do_munmap().
330          */
331         hiwater_vm = mm->hiwater_vm;
332         vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
333
334         if (do_munmap(mm, old_addr, old_len) < 0) {
335                 /* OOM: unable to split vma, just get accounts right */
336                 vm_unacct_memory(excess >> PAGE_SHIFT);
337                 excess = 0;
338         }
339         mm->hiwater_vm = hiwater_vm;
340
341         /* Restore VM_ACCOUNT if one or two pieces of vma left */
342         if (excess) {
343                 vma->vm_flags |= VM_ACCOUNT;
344                 if (split)
345                         vma->vm_next->vm_flags |= VM_ACCOUNT;
346         }
347
348         if (vm_flags & VM_LOCKED) {
349                 mm->locked_vm += new_len >> PAGE_SHIFT;
350                 *locked = true;
351         }
352
353         return new_addr;
354 }
355
356 static struct vm_area_struct *vma_to_resize(unsigned long addr,
357         unsigned long old_len, unsigned long new_len, unsigned long *p)
358 {
359         struct mm_struct *mm = current->mm;
360         struct vm_area_struct *vma = find_vma(mm, addr);
361         unsigned long pgoff;
362
363         if (!vma || vma->vm_start > addr)
364                 return ERR_PTR(-EFAULT);
365
366         if (is_vm_hugetlb_page(vma))
367                 return ERR_PTR(-EINVAL);
368
369         /* We can't remap across vm area boundaries */
370         if (old_len > vma->vm_end - addr)
371                 return ERR_PTR(-EFAULT);
372
373         if (new_len == old_len)
374                 return vma;
375
376         /* Need to be careful about a growing mapping */
377         pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
378         pgoff += vma->vm_pgoff;
379         if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
380                 return ERR_PTR(-EINVAL);
381
382         if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
383                 return ERR_PTR(-EFAULT);
384
385         if (vma->vm_flags & VM_LOCKED) {
386                 unsigned long locked, lock_limit;
387                 locked = mm->locked_vm << PAGE_SHIFT;
388                 lock_limit = rlimit(RLIMIT_MEMLOCK);
389                 locked += new_len - old_len;
390                 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
391                         return ERR_PTR(-EAGAIN);
392         }
393
394         if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT))
395                 return ERR_PTR(-ENOMEM);
396
397         if (vma->vm_flags & VM_ACCOUNT) {
398                 unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
399                 if (security_vm_enough_memory_mm(mm, charged))
400                         return ERR_PTR(-ENOMEM);
401                 *p = charged;
402         }
403
404         return vma;
405 }
406
407 static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
408                 unsigned long new_addr, unsigned long new_len, bool *locked)
409 {
410         struct mm_struct *mm = current->mm;
411         struct vm_area_struct *vma;
412         unsigned long ret = -EINVAL;
413         unsigned long charged = 0;
414         unsigned long map_flags;
415
416         if (offset_in_page(new_addr))
417                 goto out;
418
419         if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
420                 goto out;
421
422         /* Ensure the old/new locations do not overlap */
423         if (addr + old_len > new_addr && new_addr + new_len > addr)
424                 goto out;
425
426         ret = do_munmap(mm, new_addr, new_len);
427         if (ret)
428                 goto out;
429
430         if (old_len >= new_len) {
431                 ret = do_munmap(mm, addr+new_len, old_len - new_len);
432                 if (ret && old_len != new_len)
433                         goto out;
434                 old_len = new_len;
435         }
436
437         vma = vma_to_resize(addr, old_len, new_len, &charged);
438         if (IS_ERR(vma)) {
439                 ret = PTR_ERR(vma);
440                 goto out;
441         }
442
443         map_flags = MAP_FIXED;
444         if (vma->vm_flags & VM_MAYSHARE)
445                 map_flags |= MAP_SHARED;
446
447         ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
448                                 ((addr - vma->vm_start) >> PAGE_SHIFT),
449                                 map_flags);
450         if (offset_in_page(ret))
451                 goto out1;
452
453         ret = move_vma(vma, addr, old_len, new_len, new_addr, locked);
454         if (!(offset_in_page(ret)))
455                 goto out;
456 out1:
457         vm_unacct_memory(charged);
458
459 out:
460         return ret;
461 }
462
463 static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
464 {
465         unsigned long end = vma->vm_end + delta;
466         if (end < vma->vm_end) /* overflow */
467                 return 0;
468         if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
469                 return 0;
470         if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
471                               0, MAP_FIXED) & ~PAGE_MASK)
472                 return 0;
473         return 1;
474 }
475
476 /*
477  * Expand (or shrink) an existing mapping, potentially moving it at the
478  * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
479  *
480  * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
481  * This option implies MREMAP_MAYMOVE.
482  */
483 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
484                 unsigned long, new_len, unsigned long, flags,
485                 unsigned long, new_addr)
486 {
487         struct mm_struct *mm = current->mm;
488         struct vm_area_struct *vma;
489         unsigned long ret = -EINVAL;
490         unsigned long charged = 0;
491         bool locked = false;
492
493         if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
494                 return ret;
495
496         if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
497                 return ret;
498
499         if (offset_in_page(addr))
500                 return ret;
501
502         old_len = PAGE_ALIGN(old_len);
503         new_len = PAGE_ALIGN(new_len);
504
505         /*
506          * We allow a zero old-len as a special case
507          * for DOS-emu "duplicate shm area" thing. But
508          * a zero new-len is nonsensical.
509          */
510         if (!new_len)
511                 return ret;
512
513         down_write(&current->mm->mmap_sem);
514
515         if (flags & MREMAP_FIXED) {
516                 ret = mremap_to(addr, old_len, new_addr, new_len,
517                                 &locked);
518                 goto out;
519         }
520
521         /*
522          * Always allow a shrinking remap: that just unmaps
523          * the unnecessary pages..
524          * do_munmap does all the needed commit accounting
525          */
526         if (old_len >= new_len) {
527                 ret = do_munmap(mm, addr+new_len, old_len - new_len);
528                 if (ret && old_len != new_len)
529                         goto out;
530                 ret = addr;
531                 goto out;
532         }
533
534         /*
535          * Ok, we need to grow..
536          */
537         vma = vma_to_resize(addr, old_len, new_len, &charged);
538         if (IS_ERR(vma)) {
539                 ret = PTR_ERR(vma);
540                 goto out;
541         }
542
543         /* old_len exactly to the end of the area..
544          */
545         if (old_len == vma->vm_end - addr) {
546                 /* can we just expand the current mapping? */
547                 if (vma_expandable(vma, new_len - old_len)) {
548                         int pages = (new_len - old_len) >> PAGE_SHIFT;
549
550                         if (vma_adjust(vma, vma->vm_start, addr + new_len,
551                                        vma->vm_pgoff, NULL)) {
552                                 ret = -ENOMEM;
553                                 goto out;
554                         }
555
556                         vm_stat_account(mm, vma->vm_flags, vma->vm_file, pages);
557                         if (vma->vm_flags & VM_LOCKED) {
558                                 mm->locked_vm += pages;
559                                 locked = true;
560                                 new_addr = addr;
561                         }
562                         ret = addr;
563                         goto out;
564                 }
565         }
566
567         /*
568          * We weren't able to just expand or shrink the area,
569          * we need to create a new one and move it..
570          */
571         ret = -ENOMEM;
572         if (flags & MREMAP_MAYMOVE) {
573                 unsigned long map_flags = 0;
574                 if (vma->vm_flags & VM_MAYSHARE)
575                         map_flags |= MAP_SHARED;
576
577                 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
578                                         vma->vm_pgoff +
579                                         ((addr - vma->vm_start) >> PAGE_SHIFT),
580                                         map_flags);
581                 if (offset_in_page(new_addr)) {
582                         ret = new_addr;
583                         goto out;
584                 }
585
586                 ret = move_vma(vma, addr, old_len, new_len, new_addr, &locked);
587         }
588 out:
589         if (offset_in_page(ret)) {
590                 vm_unacct_memory(charged);
591                 locked = 0;
592         }
593         up_write(&current->mm->mmap_sem);
594         if (locked && new_len > old_len)
595                 mm_populate(new_addr + old_len, new_len - old_len);
596         return ret;
597 }