]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - mm/mremap.c
mm/mremap: convert huge PUD move to separate helper
[mirror_ubuntu-jammy-kernel.git] / mm / mremap.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * mm/mremap.c
4 *
5 * (C) Copyright 1996 Linus Torvalds
6 *
046c6884 7 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
8 * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
9 */
10
11#include <linux/mm.h>
12#include <linux/hugetlb.h>
1da177e4 13#include <linux/shm.h>
1ff82995 14#include <linux/ksm.h>
1da177e4
LT
15#include <linux/mman.h>
16#include <linux/swap.h>
c59ede7b 17#include <linux/capability.h>
1da177e4 18#include <linux/fs.h>
6dec97dc 19#include <linux/swapops.h>
1da177e4
LT
20#include <linux/highmem.h>
21#include <linux/security.h>
22#include <linux/syscalls.h>
cddb8a5c 23#include <linux/mmu_notifier.h>
2581d202 24#include <linux/uaccess.h>
72f87654 25#include <linux/userfaultfd_k.h>
1da177e4 26
1da177e4
LT
27#include <asm/cacheflush.h>
28#include <asm/tlbflush.h>
29
ba470de4
RR
30#include "internal.h"
31
c49dd340 32static pud_t *get_old_pud(struct mm_struct *mm, unsigned long addr)
1da177e4
LT
33{
34 pgd_t *pgd;
c2febafc 35 p4d_t *p4d;
1da177e4 36 pud_t *pud;
1da177e4
LT
37
38 pgd = pgd_offset(mm, addr);
39 if (pgd_none_or_clear_bad(pgd))
40 return NULL;
41
c2febafc
KS
42 p4d = p4d_offset(pgd, addr);
43 if (p4d_none_or_clear_bad(p4d))
44 return NULL;
45
46 pud = pud_offset(p4d, addr);
1da177e4
LT
47 if (pud_none_or_clear_bad(pud))
48 return NULL;
49
c49dd340
KS
50 return pud;
51}
52
53static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
54{
55 pud_t *pud;
56 pmd_t *pmd;
57
58 pud = get_old_pud(mm, addr);
59 if (!pud)
60 return NULL;
61
1da177e4 62 pmd = pmd_offset(pud, addr);
37a1c49a 63 if (pmd_none(*pmd))
1da177e4
LT
64 return NULL;
65
7be7a546 66 return pmd;
1da177e4
LT
67}
68
c49dd340 69static pud_t *alloc_new_pud(struct mm_struct *mm, struct vm_area_struct *vma,
8ac1f832 70 unsigned long addr)
1da177e4
LT
71{
72 pgd_t *pgd;
c2febafc 73 p4d_t *p4d;
1da177e4
LT
74
75 pgd = pgd_offset(mm, addr);
c2febafc
KS
76 p4d = p4d_alloc(mm, pgd, addr);
77 if (!p4d)
78 return NULL;
c49dd340
KS
79
80 return pud_alloc(mm, p4d, addr);
81}
82
83static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
84 unsigned long addr)
85{
86 pud_t *pud;
87 pmd_t *pmd;
88
89 pud = alloc_new_pud(mm, vma, addr);
1da177e4 90 if (!pud)
c74df32c 91 return NULL;
7be7a546 92
1da177e4 93 pmd = pmd_alloc(mm, pud, addr);
57a8f0cd 94 if (!pmd)
c74df32c 95 return NULL;
7be7a546 96
8ac1f832 97 VM_BUG_ON(pmd_trans_huge(*pmd));
c74df32c 98
7be7a546 99 return pmd;
1da177e4
LT
100}
101
1d069b7d
HD
102static void take_rmap_locks(struct vm_area_struct *vma)
103{
104 if (vma->vm_file)
105 i_mmap_lock_write(vma->vm_file->f_mapping);
106 if (vma->anon_vma)
107 anon_vma_lock_write(vma->anon_vma);
108}
109
110static void drop_rmap_locks(struct vm_area_struct *vma)
111{
112 if (vma->anon_vma)
113 anon_vma_unlock_write(vma->anon_vma);
114 if (vma->vm_file)
115 i_mmap_unlock_write(vma->vm_file->f_mapping);
116}
117
6dec97dc
CG
118static pte_t move_soft_dirty_pte(pte_t pte)
119{
120 /*
121 * Set soft dirty bit so we can notice
122 * in userspace the ptes were moved.
123 */
124#ifdef CONFIG_MEM_SOFT_DIRTY
125 if (pte_present(pte))
126 pte = pte_mksoft_dirty(pte);
127 else if (is_swap_pte(pte))
128 pte = pte_swp_mksoft_dirty(pte);
6dec97dc
CG
129#endif
130 return pte;
131}
132
7be7a546
HD
133static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
134 unsigned long old_addr, unsigned long old_end,
135 struct vm_area_struct *new_vma, pmd_t *new_pmd,
eb66ae03 136 unsigned long new_addr, bool need_rmap_locks)
1da177e4 137{
1da177e4 138 struct mm_struct *mm = vma->vm_mm;
7be7a546 139 pte_t *old_pte, *new_pte, pte;
4c21e2f2 140 spinlock_t *old_ptl, *new_ptl;
5d190420
AL
141 bool force_flush = false;
142 unsigned long len = old_end - old_addr;
1da177e4 143
38a76013 144 /*
c8c06efa 145 * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
38a76013
ML
146 * locks to ensure that rmap will always observe either the old or the
147 * new ptes. This is the easiest way to avoid races with
148 * truncate_pagecache(), page migration, etc...
149 *
150 * When need_rmap_locks is false, we use other ways to avoid
151 * such races:
152 *
153 * - During exec() shift_arg_pages(), we use a specially tagged vma
222100ee 154 * which rmap call sites look for using vma_is_temporary_stack().
38a76013
ML
155 *
156 * - During mremap(), new_vma is often known to be placed after vma
157 * in rmap traversal order. This ensures rmap will always observe
158 * either the old pte, or the new pte, or both (the page table locks
159 * serialize access to individual ptes, but only rmap traversal
160 * order guarantees that we won't miss both the old and new ptes).
161 */
1d069b7d
HD
162 if (need_rmap_locks)
163 take_rmap_locks(vma);
1da177e4 164
4c21e2f2
HD
165 /*
166 * We don't have to worry about the ordering of src and dst
c1e8d7c6 167 * pte locks because exclusive mmap_lock prevents deadlock.
4c21e2f2 168 */
c74df32c 169 old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
ece0e2b6 170 new_pte = pte_offset_map(new_pmd, new_addr);
4c21e2f2
HD
171 new_ptl = pte_lockptr(mm, new_pmd);
172 if (new_ptl != old_ptl)
f20dc5f7 173 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
3ea27719 174 flush_tlb_batched_pending(vma->vm_mm);
6606c3e0 175 arch_enter_lazy_mmu_mode();
7be7a546
HD
176
177 for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
178 new_pte++, new_addr += PAGE_SIZE) {
179 if (pte_none(*old_pte))
180 continue;
5d190420 181
a2ce2666 182 pte = ptep_get_and_clear(mm, old_addr, old_pte);
5d190420 183 /*
eb66ae03 184 * If we are remapping a valid PTE, make sure
a2ce2666 185 * to flush TLB before we drop the PTL for the
eb66ae03 186 * PTE.
a2ce2666 187 *
eb66ae03
LT
188 * NOTE! Both old and new PTL matter: the old one
189 * for racing with page_mkclean(), the new one to
190 * make sure the physical page stays valid until
191 * the TLB entry for the old mapping has been
192 * flushed.
5d190420 193 */
eb66ae03 194 if (pte_present(pte))
5d190420 195 force_flush = true;
7be7a546 196 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
6dec97dc
CG
197 pte = move_soft_dirty_pte(pte);
198 set_pte_at(mm, new_addr, new_pte, pte);
1da177e4 199 }
7be7a546 200
6606c3e0 201 arch_leave_lazy_mmu_mode();
eb66ae03
LT
202 if (force_flush)
203 flush_tlb_range(vma, old_end - len, old_end);
4c21e2f2
HD
204 if (new_ptl != old_ptl)
205 spin_unlock(new_ptl);
ece0e2b6 206 pte_unmap(new_pte - 1);
c74df32c 207 pte_unmap_unlock(old_pte - 1, old_ptl);
1d069b7d
HD
208 if (need_rmap_locks)
209 drop_rmap_locks(vma);
1da177e4
LT
210}
211
2c91bd4a
JFG
212#ifdef CONFIG_HAVE_MOVE_PMD
213static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr,
b8aa9d9d 214 unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
2c91bd4a
JFG
215{
216 spinlock_t *old_ptl, *new_ptl;
217 struct mm_struct *mm = vma->vm_mm;
218 pmd_t pmd;
219
2c91bd4a
JFG
220 /*
221 * The destination pmd shouldn't be established, free_pgtables()
f81fdd0c
LT
222 * should have released it.
223 *
224 * However, there's a case during execve() where we use mremap
225 * to move the initial stack, and in that case the target area
226 * may overlap the source area (always moving down).
227 *
228 * If everything is PMD-aligned, that works fine, as moving
229 * each pmd down will clear the source pmd. But if we first
230 * have a few 4kB-only pages that get moved down, and then
231 * hit the "now the rest is PMD-aligned, let's do everything
232 * one pmd at a time", we will still have the old (now empty
233 * of any 4kB pages, but still there) PMD in the page table
234 * tree.
235 *
236 * Warn on it once - because we really should try to figure
237 * out how to do this better - but then say "I won't move
238 * this pmd".
239 *
240 * One alternative might be to just unmap the target pmd at
241 * this point, and verify that it really is empty. We'll see.
2c91bd4a 242 */
f81fdd0c 243 if (WARN_ON_ONCE(!pmd_none(*new_pmd)))
2c91bd4a
JFG
244 return false;
245
246 /*
247 * We don't have to worry about the ordering of src and dst
c1e8d7c6 248 * ptlocks because exclusive mmap_lock prevents deadlock.
2c91bd4a
JFG
249 */
250 old_ptl = pmd_lock(vma->vm_mm, old_pmd);
251 new_ptl = pmd_lockptr(mm, new_pmd);
252 if (new_ptl != old_ptl)
253 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
254
255 /* Clear the pmd */
256 pmd = *old_pmd;
257 pmd_clear(old_pmd);
258
259 VM_BUG_ON(!pmd_none(*new_pmd));
260
261 /* Set the new pmd */
262 set_pmd_at(mm, new_addr, new_pmd, pmd);
263 flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
264 if (new_ptl != old_ptl)
265 spin_unlock(new_ptl);
266 spin_unlock(old_ptl);
267
268 return true;
269}
c49dd340
KS
270#else
271static inline bool move_normal_pmd(struct vm_area_struct *vma,
272 unsigned long old_addr, unsigned long new_addr, pmd_t *old_pmd,
273 pmd_t *new_pmd)
274{
275 return false;
276}
277#endif
278
279#ifdef CONFIG_HAVE_MOVE_PUD
280static bool move_normal_pud(struct vm_area_struct *vma, unsigned long old_addr,
281 unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
282{
283 spinlock_t *old_ptl, *new_ptl;
284 struct mm_struct *mm = vma->vm_mm;
285 pud_t pud;
286
287 /*
288 * The destination pud shouldn't be established, free_pgtables()
289 * should have released it.
290 */
291 if (WARN_ON_ONCE(!pud_none(*new_pud)))
292 return false;
293
294 /*
295 * We don't have to worry about the ordering of src and dst
296 * ptlocks because exclusive mmap_lock prevents deadlock.
297 */
298 old_ptl = pud_lock(vma->vm_mm, old_pud);
299 new_ptl = pud_lockptr(mm, new_pud);
300 if (new_ptl != old_ptl)
301 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
302
303 /* Clear the pud */
304 pud = *old_pud;
305 pud_clear(old_pud);
306
307 VM_BUG_ON(!pud_none(*new_pud));
308
309 /* Set the new pud */
310 set_pud_at(mm, new_addr, new_pud, pud);
311 flush_tlb_range(vma, old_addr, old_addr + PUD_SIZE);
312 if (new_ptl != old_ptl)
313 spin_unlock(new_ptl);
314 spin_unlock(old_ptl);
315
316 return true;
317}
318#else
319static inline bool move_normal_pud(struct vm_area_struct *vma,
320 unsigned long old_addr, unsigned long new_addr, pud_t *old_pud,
321 pud_t *new_pud)
322{
323 return false;
324}
2c91bd4a
JFG
325#endif
326
7d846db7
AK
327#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
328static bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr,
329 unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
330{
331 spinlock_t *old_ptl, *new_ptl;
332 struct mm_struct *mm = vma->vm_mm;
333 pud_t pud;
334
335 /*
336 * The destination pud shouldn't be established, free_pgtables()
337 * should have released it.
338 */
339 if (WARN_ON_ONCE(!pud_none(*new_pud)))
340 return false;
341
342 /*
343 * We don't have to worry about the ordering of src and dst
344 * ptlocks because exclusive mmap_lock prevents deadlock.
345 */
346 old_ptl = pud_lock(vma->vm_mm, old_pud);
347 new_ptl = pud_lockptr(mm, new_pud);
348 if (new_ptl != old_ptl)
349 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
350
351 /* Clear the pud */
352 pud = *old_pud;
353 pud_clear(old_pud);
354
355 VM_BUG_ON(!pud_none(*new_pud));
356
357 /* Set the new pud */
358 /* mark soft_ditry when we add pud level soft dirty support */
359 set_pud_at(mm, new_addr, new_pud, pud);
360 flush_pud_tlb_range(vma, old_addr, old_addr + HPAGE_PUD_SIZE);
361 if (new_ptl != old_ptl)
362 spin_unlock(new_ptl);
363 spin_unlock(old_ptl);
364
365 return true;
366}
367#else
368static bool move_huge_pud(struct vm_area_struct *vma, unsigned long old_addr,
369 unsigned long new_addr, pud_t *old_pud, pud_t *new_pud)
370{
371 WARN_ON_ONCE(1);
372 return false;
373
374}
375#endif
376
c49dd340
KS
377enum pgt_entry {
378 NORMAL_PMD,
379 HPAGE_PMD,
380 NORMAL_PUD,
7d846db7 381 HPAGE_PUD,
c49dd340
KS
382};
383
384/*
385 * Returns an extent of the corresponding size for the pgt_entry specified if
386 * valid. Else returns a smaller extent bounded by the end of the source and
387 * destination pgt_entry.
388 */
a30a2909
AB
389static __always_inline unsigned long get_extent(enum pgt_entry entry,
390 unsigned long old_addr, unsigned long old_end,
391 unsigned long new_addr)
c49dd340
KS
392{
393 unsigned long next, extent, mask, size;
394
395 switch (entry) {
396 case HPAGE_PMD:
397 case NORMAL_PMD:
398 mask = PMD_MASK;
399 size = PMD_SIZE;
400 break;
7d846db7 401 case HPAGE_PUD:
c49dd340
KS
402 case NORMAL_PUD:
403 mask = PUD_MASK;
404 size = PUD_SIZE;
405 break;
406 default:
407 BUILD_BUG();
408 break;
409 }
410
411 next = (old_addr + size) & mask;
412 /* even if next overflowed, extent below will be ok */
e05986ee
KS
413 extent = next - old_addr;
414 if (extent > old_end - old_addr)
415 extent = old_end - old_addr;
c49dd340
KS
416 next = (new_addr + size) & mask;
417 if (extent > next - new_addr)
418 extent = next - new_addr;
419 return extent;
420}
421
422/*
423 * Attempts to speedup the move by moving entry at the level corresponding to
424 * pgt_entry. Returns true if the move was successful, else false.
425 */
426static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma,
427 unsigned long old_addr, unsigned long new_addr,
428 void *old_entry, void *new_entry, bool need_rmap_locks)
429{
430 bool moved = false;
431
432 /* See comment in move_ptes() */
433 if (need_rmap_locks)
434 take_rmap_locks(vma);
435
436 switch (entry) {
437 case NORMAL_PMD:
438 moved = move_normal_pmd(vma, old_addr, new_addr, old_entry,
439 new_entry);
440 break;
441 case NORMAL_PUD:
442 moved = move_normal_pud(vma, old_addr, new_addr, old_entry,
443 new_entry);
444 break;
445 case HPAGE_PMD:
446 moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
447 move_huge_pmd(vma, old_addr, new_addr, old_entry,
448 new_entry);
449 break;
7d846db7
AK
450 case HPAGE_PUD:
451 moved = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
452 move_huge_pud(vma, old_addr, new_addr, old_entry,
453 new_entry);
454 break;
455
c49dd340
KS
456 default:
457 WARN_ON_ONCE(1);
458 break;
459 }
460
461 if (need_rmap_locks)
462 drop_rmap_locks(vma);
463
464 return moved;
465}
466
b6a2fea3 467unsigned long move_page_tables(struct vm_area_struct *vma,
1da177e4 468 unsigned long old_addr, struct vm_area_struct *new_vma,
38a76013
ML
469 unsigned long new_addr, unsigned long len,
470 bool need_rmap_locks)
1da177e4 471{
c49dd340 472 unsigned long extent, old_end;
ac46d4f3 473 struct mmu_notifier_range range;
7be7a546 474 pmd_t *old_pmd, *new_pmd;
7d846db7 475 pud_t *old_pud, *new_pud;
1da177e4 476
7be7a546
HD
477 old_end = old_addr + len;
478 flush_cache_range(vma, old_addr, old_end);
1da177e4 479
6f4f13e8
JG
480 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
481 old_addr, old_end);
ac46d4f3 482 mmu_notifier_invalidate_range_start(&range);
7b6efc2b 483
7be7a546 484 for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
1da177e4 485 cond_resched();
c49dd340
KS
486 /*
487 * If extent is PUD-sized try to speed up the move by moving at the
488 * PUD level if possible.
489 */
490 extent = get_extent(NORMAL_PUD, old_addr, old_end, new_addr);
c49dd340 491
7d846db7
AK
492 old_pud = get_old_pud(vma->vm_mm, old_addr);
493 if (!old_pud)
494 continue;
495 new_pud = alloc_new_pud(vma->vm_mm, vma, new_addr);
496 if (!new_pud)
497 break;
498 if (pud_trans_huge(*old_pud) || pud_devmap(*old_pud)) {
499 if (extent == HPAGE_PUD_SIZE) {
500 move_pgt_entry(HPAGE_PUD, vma, old_addr, new_addr,
501 old_pud, new_pud, need_rmap_locks);
502 /* We ignore and continue on error? */
c49dd340 503 continue;
7d846db7
AK
504 }
505 } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PUD) && extent == PUD_SIZE) {
506
c49dd340
KS
507 if (move_pgt_entry(NORMAL_PUD, vma, old_addr, new_addr,
508 old_pud, new_pud, need_rmap_locks))
509 continue;
510 }
511
512 extent = get_extent(NORMAL_PMD, old_addr, old_end, new_addr);
7be7a546
HD
513 old_pmd = get_old_pmd(vma->vm_mm, old_addr);
514 if (!old_pmd)
515 continue;
8ac1f832 516 new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
7be7a546
HD
517 if (!new_pmd)
518 break;
c49dd340
KS
519 if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd) ||
520 pmd_devmap(*old_pmd)) {
521 if (extent == HPAGE_PMD_SIZE &&
522 move_pgt_entry(HPAGE_PMD, vma, old_addr, new_addr,
523 old_pmd, new_pmd, need_rmap_locks))
524 continue;
4b471e88 525 split_huge_pmd(vma, old_pmd, old_addr);
337d9abf 526 if (pmd_trans_unstable(old_pmd))
6b9116a6 527 continue;
c49dd340
KS
528 } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PMD) &&
529 extent == PMD_SIZE) {
2c91bd4a
JFG
530 /*
531 * If the extent is PMD-sized, try to speed the move by
532 * moving at the PMD level if possible.
533 */
c49dd340
KS
534 if (move_pgt_entry(NORMAL_PMD, vma, old_addr, new_addr,
535 old_pmd, new_pmd, need_rmap_locks))
2c91bd4a 536 continue;
37a1c49a 537 }
2c91bd4a 538
4cf58924 539 if (pte_alloc(new_vma->vm_mm, new_pmd))
37a1c49a 540 break;
5d190420 541 move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma,
eb66ae03 542 new_pmd, new_addr, need_rmap_locks);
1da177e4 543 }
7b6efc2b 544
ac46d4f3 545 mmu_notifier_invalidate_range_end(&range);
7be7a546
HD
546
547 return len + old_addr - old_end; /* how much done */
1da177e4
LT
548}
549
550static unsigned long move_vma(struct vm_area_struct *vma,
551 unsigned long old_addr, unsigned long old_len,
72f87654 552 unsigned long new_len, unsigned long new_addr,
e346b381
BG
553 bool *locked, unsigned long flags,
554 struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap)
1da177e4
LT
555{
556 struct mm_struct *mm = vma->vm_mm;
557 struct vm_area_struct *new_vma;
558 unsigned long vm_flags = vma->vm_flags;
559 unsigned long new_pgoff;
560 unsigned long moved_len;
561 unsigned long excess = 0;
365e9c87 562 unsigned long hiwater_vm;
1da177e4 563 int split = 0;
73d5e062 564 int err = 0;
38a76013 565 bool need_rmap_locks;
1da177e4
LT
566
567 /*
568 * We'd prefer to avoid failure later on in do_munmap:
569 * which may split one vma into three before unmapping.
570 */
571 if (mm->map_count >= sysctl_max_map_count - 3)
572 return -ENOMEM;
573
73d5e062
DS
574 if (vma->vm_ops && vma->vm_ops->may_split) {
575 if (vma->vm_start != old_addr)
576 err = vma->vm_ops->may_split(vma, old_addr);
577 if (!err && vma->vm_end != old_addr + old_len)
578 err = vma->vm_ops->may_split(vma, old_addr + old_len);
579 if (err)
580 return err;
581 }
582
1ff82995
HD
583 /*
584 * Advise KSM to break any KSM pages in the area to be moved:
585 * it would be confusing if they were to turn up at the new
586 * location, where they happen to coincide with different KSM
587 * pages recently unmapped. But leave vma->vm_flags as it was,
588 * so KSM can come around to merge on vma and new_vma afterwards.
589 */
7103ad32
HD
590 err = ksm_madvise(vma, old_addr, old_addr + old_len,
591 MADV_UNMERGEABLE, &vm_flags);
592 if (err)
593 return err;
1ff82995 594
ad8ee77e
DS
595 if (unlikely(flags & MREMAP_DONTUNMAP && vm_flags & VM_ACCOUNT)) {
596 if (security_vm_enough_memory_mm(mm, new_len >> PAGE_SHIFT))
597 return -ENOMEM;
598 }
599
1da177e4 600 new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
38a76013
ML
601 new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
602 &need_rmap_locks);
ad8ee77e
DS
603 if (!new_vma) {
604 if (unlikely(flags & MREMAP_DONTUNMAP && vm_flags & VM_ACCOUNT))
605 vm_unacct_memory(new_len >> PAGE_SHIFT);
1da177e4 606 return -ENOMEM;
ad8ee77e 607 }
1da177e4 608
38a76013
ML
609 moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
610 need_rmap_locks);
1da177e4 611 if (moved_len < old_len) {
df1eab30 612 err = -ENOMEM;
5477e70a 613 } else if (vma->vm_ops && vma->vm_ops->mremap) {
14d07113 614 err = vma->vm_ops->mremap(new_vma);
df1eab30
ON
615 }
616
617 if (unlikely(err)) {
1da177e4
LT
618 /*
619 * On error, move entries back from new area to old,
620 * which will succeed since page tables still there,
621 * and then proceed to unmap new area instead of old.
622 */
38a76013
ML
623 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
624 true);
1da177e4
LT
625 vma = new_vma;
626 old_len = new_len;
627 old_addr = new_addr;
df1eab30 628 new_addr = err;
4abad2ca 629 } else {
72f87654 630 mremap_userfaultfd_prep(new_vma, uf);
b2edffdd 631 }
1da177e4
LT
632
633 /* Conceal VM_ACCOUNT so old reservation is not undone */
ad8ee77e 634 if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) {
1da177e4
LT
635 vma->vm_flags &= ~VM_ACCOUNT;
636 excess = vma->vm_end - vma->vm_start - old_len;
637 if (old_addr > vma->vm_start &&
638 old_addr + old_len < vma->vm_end)
639 split = 1;
640 }
641
71799062 642 /*
365e9c87
HD
643 * If we failed to move page tables we still do total_vm increment
644 * since do_munmap() will decrement it by old_len == new_len.
645 *
646 * Since total_vm is about to be raised artificially high for a
647 * moment, we need to restore high watermark afterwards: if stats
648 * are taken meanwhile, total_vm and hiwater_vm appear too high.
649 * If this were a serious issue, we'd add a flag to do_munmap().
71799062 650 */
365e9c87 651 hiwater_vm = mm->hiwater_vm;
84638335 652 vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
71799062 653
d9fe4fab
TK
654 /* Tell pfnmap has moved from this vma */
655 if (unlikely(vma->vm_flags & VM_PFNMAP))
656 untrack_pfn_moved(vma);
657
e346b381 658 if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) {
e346b381
BG
659 /* We always clear VM_LOCKED[ONFAULT] on the old vma */
660 vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
661
1583aa27
LX
662 /*
663 * anon_vma links of the old vma is no longer needed after its page
664 * table has been moved.
665 */
666 if (new_vma != vma && vma->vm_start == old_addr &&
667 vma->vm_end == (old_addr + old_len))
668 unlink_anon_vmas(vma);
669
e346b381 670 /* Because we won't unmap we don't need to touch locked_vm */
ad8ee77e 671 return new_addr;
e346b381
BG
672 }
673
897ab3e0 674 if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) {
1da177e4 675 /* OOM: unable to split vma, just get accounts right */
ad8ee77e 676 if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP))
51df7bcb 677 vm_acct_memory(new_len >> PAGE_SHIFT);
1da177e4
LT
678 excess = 0;
679 }
e346b381
BG
680
681 if (vm_flags & VM_LOCKED) {
682 mm->locked_vm += new_len >> PAGE_SHIFT;
683 *locked = true;
684 }
ad8ee77e 685
365e9c87 686 mm->hiwater_vm = hiwater_vm;
1da177e4
LT
687
688 /* Restore VM_ACCOUNT if one or two pieces of vma left */
689 if (excess) {
690 vma->vm_flags |= VM_ACCOUNT;
691 if (split)
692 vma->vm_next->vm_flags |= VM_ACCOUNT;
693 }
694
1da177e4
LT
695 return new_addr;
696}
697
54f5de70 698static struct vm_area_struct *vma_to_resize(unsigned long addr,
e346b381
BG
699 unsigned long old_len, unsigned long new_len, unsigned long flags,
700 unsigned long *p)
54f5de70
AV
701{
702 struct mm_struct *mm = current->mm;
5aaf07f0 703 struct vm_area_struct *vma;
1d391686 704 unsigned long pgoff;
54f5de70 705
5aaf07f0
LH
706 vma = vma_lookup(mm, addr);
707 if (!vma)
6cd57613 708 return ERR_PTR(-EFAULT);
54f5de70 709
dba58d3b
MK
710 /*
711 * !old_len is a special case where an attempt is made to 'duplicate'
712 * a mapping. This makes no sense for private mappings as it will
713 * instead create a fresh/new mapping unrelated to the original. This
714 * is contrary to the basic idea of mremap which creates new mappings
715 * based on the original. There are no known use cases for this
716 * behavior. As a result, fail such attempts.
717 */
718 if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
719 pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid);
720 return ERR_PTR(-EINVAL);
721 }
722
a4609387
BG
723 if ((flags & MREMAP_DONTUNMAP) &&
724 (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)))
e346b381
BG
725 return ERR_PTR(-EINVAL);
726
54f5de70 727 if (is_vm_hugetlb_page(vma))
6cd57613 728 return ERR_PTR(-EINVAL);
54f5de70
AV
729
730 /* We can't remap across vm area boundaries */
731 if (old_len > vma->vm_end - addr)
6cd57613 732 return ERR_PTR(-EFAULT);
54f5de70 733
1d391686
ON
734 if (new_len == old_len)
735 return vma;
736
982134ba 737 /* Need to be careful about a growing mapping */
1d391686
ON
738 pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
739 pgoff += vma->vm_pgoff;
740 if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
741 return ERR_PTR(-EINVAL);
742
743 if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
744 return ERR_PTR(-EFAULT);
54f5de70
AV
745
746 if (vma->vm_flags & VM_LOCKED) {
747 unsigned long locked, lock_limit;
748 locked = mm->locked_vm << PAGE_SHIFT;
59e99e5b 749 lock_limit = rlimit(RLIMIT_MEMLOCK);
54f5de70
AV
750 locked += new_len - old_len;
751 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
6cd57613 752 return ERR_PTR(-EAGAIN);
54f5de70
AV
753 }
754
84638335
KK
755 if (!may_expand_vm(mm, vma->vm_flags,
756 (new_len - old_len) >> PAGE_SHIFT))
6cd57613 757 return ERR_PTR(-ENOMEM);
54f5de70
AV
758
759 if (vma->vm_flags & VM_ACCOUNT) {
760 unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
191c5424 761 if (security_vm_enough_memory_mm(mm, charged))
6cd57613 762 return ERR_PTR(-ENOMEM);
54f5de70
AV
763 *p = charged;
764 }
765
766 return vma;
54f5de70
AV
767}
768
81909b84 769static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
72f87654 770 unsigned long new_addr, unsigned long new_len, bool *locked,
e346b381 771 unsigned long flags, struct vm_userfaultfd_ctx *uf,
b2282371 772 struct list_head *uf_unmap_early,
897ab3e0 773 struct list_head *uf_unmap)
ecc1a899
AV
774{
775 struct mm_struct *mm = current->mm;
776 struct vm_area_struct *vma;
777 unsigned long ret = -EINVAL;
778 unsigned long charged = 0;
e346b381 779 unsigned long map_flags = 0;
ecc1a899 780
f19cb115 781 if (offset_in_page(new_addr))
ecc1a899
AV
782 goto out;
783
784 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
785 goto out;
786
9943242c
ON
787 /* Ensure the old/new locations do not overlap */
788 if (addr + old_len > new_addr && new_addr + new_len > addr)
ecc1a899
AV
789 goto out;
790
ea2c3f6f
OS
791 /*
792 * move_vma() need us to stay 4 maps below the threshold, otherwise
793 * it will bail out at the very beginning.
794 * That is a problem if we have already unmaped the regions here
795 * (new_addr, and old_addr), because userspace will not know the
796 * state of the vma's after it gets -ENOMEM.
797 * So, to avoid such scenario we can pre-compute if the whole
798 * operation has high chances to success map-wise.
799 * Worst-scenario case is when both vma's (new_addr and old_addr) get
f0953a1b 800 * split in 3 before unmapping it.
ea2c3f6f
OS
801 * That means 2 more maps (1 for each) to the ones we already hold.
802 * Check whether current map count plus 2 still leads us to 4 maps below
803 * the threshold, otherwise return -ENOMEM here to be more safe.
804 */
805 if ((mm->map_count + 2) >= sysctl_max_map_count - 3)
806 return -ENOMEM;
807
e346b381
BG
808 if (flags & MREMAP_FIXED) {
809 ret = do_munmap(mm, new_addr, new_len, uf_unmap_early);
810 if (ret)
811 goto out;
812 }
ecc1a899
AV
813
814 if (old_len >= new_len) {
897ab3e0 815 ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
ecc1a899
AV
816 if (ret && old_len != new_len)
817 goto out;
818 old_len = new_len;
819 }
820
e346b381 821 vma = vma_to_resize(addr, old_len, new_len, flags, &charged);
ecc1a899
AV
822 if (IS_ERR(vma)) {
823 ret = PTR_ERR(vma);
824 goto out;
825 }
826
e346b381
BG
827 /* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */
828 if (flags & MREMAP_DONTUNMAP &&
829 !may_expand_vm(mm, vma->vm_flags, old_len >> PAGE_SHIFT)) {
830 ret = -ENOMEM;
831 goto out;
832 }
833
834 if (flags & MREMAP_FIXED)
835 map_flags |= MAP_FIXED;
836
097eed10
AV
837 if (vma->vm_flags & VM_MAYSHARE)
838 map_flags |= MAP_SHARED;
9206de95 839
097eed10
AV
840 ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
841 ((addr - vma->vm_start) >> PAGE_SHIFT),
842 map_flags);
ff68dac6 843 if (IS_ERR_VALUE(ret))
097eed10
AV
844 goto out1;
845
e346b381
BG
846 /* We got a new mapping */
847 if (!(flags & MREMAP_FIXED))
848 new_addr = ret;
849
850 ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, flags, uf,
897ab3e0 851 uf_unmap);
e346b381 852
f19cb115 853 if (!(offset_in_page(ret)))
097eed10 854 goto out;
e346b381 855
097eed10
AV
856out1:
857 vm_unacct_memory(charged);
ecc1a899
AV
858
859out:
860 return ret;
861}
862
1a0ef85f
AV
863static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
864{
f106af4e 865 unsigned long end = vma->vm_end + delta;
9206de95 866 if (end < vma->vm_end) /* overflow */
f106af4e 867 return 0;
9206de95 868 if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
f106af4e
AV
869 return 0;
870 if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
871 0, MAP_FIXED) & ~PAGE_MASK)
1a0ef85f 872 return 0;
1a0ef85f
AV
873 return 1;
874}
875
1da177e4
LT
876/*
877 * Expand (or shrink) an existing mapping, potentially moving it at the
878 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
879 *
880 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
881 * This option implies MREMAP_MAYMOVE.
882 */
63a81db1
AV
883SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
884 unsigned long, new_len, unsigned long, flags,
885 unsigned long, new_addr)
1da177e4 886{
d0de32d9 887 struct mm_struct *mm = current->mm;
1da177e4
LT
888 struct vm_area_struct *vma;
889 unsigned long ret = -EINVAL;
890 unsigned long charged = 0;
81909b84 891 bool locked = false;
85a06835 892 bool downgraded = false;
72f87654 893 struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX;
b2282371 894 LIST_HEAD(uf_unmap_early);
897ab3e0 895 LIST_HEAD(uf_unmap);
1da177e4 896
b2a84de2
WD
897 /*
898 * There is a deliberate asymmetry here: we strip the pointer tag
899 * from the old address but leave the new address alone. This is
900 * for consistency with mmap(), where we prevent the creation of
901 * aliasing mappings in userspace by leaving the tag bits of the
902 * mapping address intact. A non-zero tag will cause the subsequent
903 * range checks to reject the address as invalid.
904 *
905 * See Documentation/arm64/tagged-address-abi.rst for more information.
906 */
057d3389
AK
907 addr = untagged_addr(addr);
908
e346b381 909 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP))
9a2458a6
RV
910 return ret;
911
912 if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
913 return ret;
1da177e4 914
e346b381
BG
915 /*
916 * MREMAP_DONTUNMAP is always a move and it does not allow resizing
917 * in the process.
918 */
919 if (flags & MREMAP_DONTUNMAP &&
920 (!(flags & MREMAP_MAYMOVE) || old_len != new_len))
921 return ret;
922
923
f19cb115 924 if (offset_in_page(addr))
9a2458a6 925 return ret;
1da177e4
LT
926
927 old_len = PAGE_ALIGN(old_len);
928 new_len = PAGE_ALIGN(new_len);
929
930 /*
931 * We allow a zero old-len as a special case
932 * for DOS-emu "duplicate shm area" thing. But
933 * a zero new-len is nonsensical.
934 */
935 if (!new_len)
9a2458a6
RV
936 return ret;
937
d8ed45c5 938 if (mmap_write_lock_killable(current->mm))
dc0ef0df 939 return -EINTR;
1da177e4 940
e346b381 941 if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) {
9a2458a6 942 ret = mremap_to(addr, old_len, new_addr, new_len,
e346b381
BG
943 &locked, flags, &uf, &uf_unmap_early,
944 &uf_unmap);
ecc1a899 945 goto out;
1da177e4
LT
946 }
947
948 /*
949 * Always allow a shrinking remap: that just unmaps
950 * the unnecessary pages..
85a06835 951 * __do_munmap does all the needed commit accounting, and
c1e8d7c6 952 * downgrades mmap_lock to read if so directed.
1da177e4
LT
953 */
954 if (old_len >= new_len) {
85a06835
YS
955 int retval;
956
957 retval = __do_munmap(mm, addr+new_len, old_len - new_len,
958 &uf_unmap, true);
959 if (retval < 0 && old_len != new_len) {
960 ret = retval;
1da177e4 961 goto out;
c1e8d7c6 962 /* Returning 1 indicates mmap_lock is downgraded to read. */
85a06835
YS
963 } else if (retval == 1)
964 downgraded = true;
1da177e4 965 ret = addr;
ecc1a899 966 goto out;
1da177e4
LT
967 }
968
969 /*
ecc1a899 970 * Ok, we need to grow..
1da177e4 971 */
e346b381 972 vma = vma_to_resize(addr, old_len, new_len, flags, &charged);
54f5de70
AV
973 if (IS_ERR(vma)) {
974 ret = PTR_ERR(vma);
1da177e4 975 goto out;
119f657c 976 }
1da177e4 977
1da177e4 978 /* old_len exactly to the end of the area..
1da177e4 979 */
ecc1a899 980 if (old_len == vma->vm_end - addr) {
1da177e4 981 /* can we just expand the current mapping? */
1a0ef85f 982 if (vma_expandable(vma, new_len - old_len)) {
1da177e4
LT
983 int pages = (new_len - old_len) >> PAGE_SHIFT;
984
5beb4930
RR
985 if (vma_adjust(vma, vma->vm_start, addr + new_len,
986 vma->vm_pgoff, NULL)) {
987 ret = -ENOMEM;
988 goto out;
989 }
1da177e4 990
84638335 991 vm_stat_account(mm, vma->vm_flags, pages);
1da177e4 992 if (vma->vm_flags & VM_LOCKED) {
d0de32d9 993 mm->locked_vm += pages;
81909b84
ML
994 locked = true;
995 new_addr = addr;
1da177e4
LT
996 }
997 ret = addr;
998 goto out;
999 }
1000 }
1001
1002 /*
1003 * We weren't able to just expand or shrink the area,
1004 * we need to create a new one and move it..
1005 */
1006 ret = -ENOMEM;
1007 if (flags & MREMAP_MAYMOVE) {
ecc1a899
AV
1008 unsigned long map_flags = 0;
1009 if (vma->vm_flags & VM_MAYSHARE)
1010 map_flags |= MAP_SHARED;
1011
1012 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
93587414
AV
1013 vma->vm_pgoff +
1014 ((addr - vma->vm_start) >> PAGE_SHIFT),
1015 map_flags);
ff68dac6 1016 if (IS_ERR_VALUE(new_addr)) {
ecc1a899
AV
1017 ret = new_addr;
1018 goto out;
1da177e4 1019 }
ecc1a899 1020
72f87654 1021 ret = move_vma(vma, addr, old_len, new_len, new_addr,
e346b381 1022 &locked, flags, &uf, &uf_unmap);
1da177e4
LT
1023 }
1024out:
f19cb115 1025 if (offset_in_page(ret)) {
1da177e4 1026 vm_unacct_memory(charged);
fa1f68cc 1027 locked = false;
d456fb9e 1028 }
85a06835 1029 if (downgraded)
d8ed45c5 1030 mmap_read_unlock(current->mm);
85a06835 1031 else
d8ed45c5 1032 mmap_write_unlock(current->mm);
81909b84
ML
1033 if (locked && new_len > old_len)
1034 mm_populate(new_addr + old_len, new_len - old_len);
b2282371 1035 userfaultfd_unmap_complete(mm, &uf_unmap_early);
d1564926 1036 mremap_userfaultfd_complete(&uf, addr, ret, old_len);
897ab3e0 1037 userfaultfd_unmap_complete(mm, &uf_unmap);
1da177e4
LT
1038 return ret;
1039}