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