blob: 860755dc6bb407194f6af5b80a38b2c1f1158a21 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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
23#include <asm/uaccess.h>
24#include <asm/cacheflush.h>
25#include <asm/tlbflush.h>
26
27#include "internal.h"
28
29static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
30{
31 pgd_t *pgd;
32 pud_t *pud;
33 pmd_t *pmd;
34
35 pgd = pgd_offset(mm, addr);
36 if (pgd_none_or_clear_bad(pgd))
37 return NULL;
38
39 pud = pud_offset(pgd, addr);
40 if (pud_none_or_clear_bad(pud))
41 return NULL;
42
43 pmd = pmd_offset(pud, addr);
44 if (pmd_none(*pmd))
45 return NULL;
46
47 return pmd;
48}
49
50static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
51 unsigned long addr)
52{
53 pgd_t *pgd;
54 pud_t *pud;
55 pmd_t *pmd;
56
57 pgd = pgd_offset(mm, addr);
58 pud = pud_alloc(mm, pgd, addr);
59 if (!pud)
60 return NULL;
61
62 pmd = pmd_alloc(mm, pud, addr);
63 if (!pmd)
64 return NULL;
65
66 VM_BUG_ON(pmd_trans_huge(*pmd));
67
68 return pmd;
69}
70
71static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
72 unsigned long old_addr, unsigned long old_end,
73 struct vm_area_struct *new_vma, pmd_t *new_pmd,
74 unsigned long new_addr)
75{
76 struct address_space *mapping = NULL;
77 struct mm_struct *mm = vma->vm_mm;
78 pte_t *old_pte, *new_pte, pte;
79 spinlock_t *old_ptl, *new_ptl;
80 bool force_flush = false;
81 unsigned long len = old_end - old_addr;
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
110 /*fix for hub CVE-2018-18281*/
111 if (pte_present(pte))
112 force_flush = true;
113 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
114 set_pte_at(mm, new_addr, new_pte, pte);
115 }
116
117 arch_leave_lazy_mmu_mode();
118 /*fix for hub CVE-2018-18281*/
119 if (force_flush)
120 flush_tlb_range(vma, old_end - len, old_end);
121 if (new_ptl != old_ptl)
122 spin_unlock(new_ptl);
123 pte_unmap(new_pte - 1);
124 pte_unmap_unlock(old_pte - 1, old_ptl);
125 if (mapping)
126 mutex_unlock(&mapping->i_mmap_mutex);
127}
128
129#define LATENCY_LIMIT (64 * PAGE_SIZE)
130
131unsigned long move_page_tables(struct vm_area_struct *vma,
132 unsigned long old_addr, struct vm_area_struct *new_vma,
133 unsigned long new_addr, unsigned long len)
134{
135 unsigned long extent, next, old_end;
136 pmd_t *old_pmd, *new_pmd;
137
138 old_end = old_addr + len;
139 flush_cache_range(vma, old_addr, old_end);
140
141 mmu_notifier_invalidate_range_start(vma->vm_mm, old_addr, old_end);
142
143 for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
144 cond_resched();
145 next = (old_addr + PMD_SIZE) & PMD_MASK;
146 /* even if next overflowed, extent below will be ok */
147 extent = next - old_addr;
148 if (extent > old_end - old_addr)
149 extent = old_end - old_addr;
150 old_pmd = get_old_pmd(vma->vm_mm, old_addr);
151 if (!old_pmd)
152 continue;
153 new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
154 if (!new_pmd)
155 break;
156 if (pmd_trans_huge(*old_pmd)) {
157 int err = 0;
158 if (extent == HPAGE_PMD_SIZE)
159 err = move_huge_pmd(vma, new_vma, old_addr,
160 new_addr, old_end,
161 old_pmd, new_pmd);
162 if (err > 0) {
163 continue;
164 } else if (!err) {
165 split_huge_page_pmd(vma->vm_mm, old_pmd);
166 }
167 VM_BUG_ON(pmd_trans_huge(*old_pmd));
168 }
169 if (pmd_none(*new_pmd) && __pte_alloc(new_vma->vm_mm, new_vma,
170 new_pmd, new_addr))
171 break;
172 next = (new_addr + PMD_SIZE) & PMD_MASK;
173 if (extent > next - new_addr)
174 extent = next - new_addr;
175 if (extent > LATENCY_LIMIT)
176 extent = LATENCY_LIMIT;
177 move_ptes(vma, old_pmd, old_addr, old_addr + extent,
178 new_vma, new_pmd, new_addr);
179 }
180
181
182 mmu_notifier_invalidate_range_end(vma->vm_mm, old_end-len, old_end);
183
184 return len + old_addr - old_end; /* how much done */
185}
186
187static unsigned long move_vma(struct vm_area_struct *vma,
188 unsigned long old_addr, unsigned long old_len,
189 unsigned long new_len, unsigned long new_addr)
190{
191 struct mm_struct *mm = vma->vm_mm;
192 struct vm_area_struct *new_vma;
193 unsigned long vm_flags = vma->vm_flags;
194 unsigned long new_pgoff;
195 unsigned long moved_len;
196 unsigned long excess = 0;
197 unsigned long hiwater_vm;
198 int split = 0;
199 int err;
200
201 /*
202 * We'd prefer to avoid failure later on in do_munmap:
203 * which may split one vma into three before unmapping.
204 */
205 if (mm->map_count >= sysctl_max_map_count - 3)
206 return -ENOMEM;
207
208 /*
209 * Advise KSM to break any KSM pages in the area to be moved:
210 * it would be confusing if they were to turn up at the new
211 * location, where they happen to coincide with different KSM
212 * pages recently unmapped. But leave vma->vm_flags as it was,
213 * so KSM can come around to merge on vma and new_vma afterwards.
214 */
215 err = ksm_madvise(vma, old_addr, old_addr + old_len,
216 MADV_UNMERGEABLE, &vm_flags);
217 if (err)
218 return err;
219
220 new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
221 new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff);
222 if (!new_vma)
223 return -ENOMEM;
224
225 moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len);
226 if (moved_len < old_len) {
227 /*
228 * Before moving the page tables from the new vma to
229 * the old vma, we need to be sure the old vma is
230 * queued after new vma in the same_anon_vma list to
231 * prevent SMP races with rmap_walk (that could lead
232 * rmap_walk to miss some page table).
233 */
234 anon_vma_moveto_tail(vma);
235
236 /*
237 * On error, move entries back from new area to old,
238 * which will succeed since page tables still there,
239 * and then proceed to unmap new area instead of old.
240 */
241 move_page_tables(new_vma, new_addr, vma, old_addr, moved_len);
242 vma = new_vma;
243 old_len = new_len;
244 old_addr = new_addr;
245 new_addr = -ENOMEM;
246 }
247
248 /* Conceal VM_ACCOUNT so old reservation is not undone */
249 if (vm_flags & VM_ACCOUNT) {
250 vma->vm_flags &= ~VM_ACCOUNT;
251 excess = vma->vm_end - vma->vm_start - old_len;
252 if (old_addr > vma->vm_start &&
253 old_addr + old_len < vma->vm_end)
254 split = 1;
255 }
256
257 /*
258 * If we failed to move page tables we still do total_vm increment
259 * since do_munmap() will decrement it by old_len == new_len.
260 *
261 * Since total_vm is about to be raised artificially high for a
262 * moment, we need to restore high watermark afterwards: if stats
263 * are taken meanwhile, total_vm and hiwater_vm appear too high.
264 * If this were a serious issue, we'd add a flag to do_munmap().
265 */
266 hiwater_vm = mm->hiwater_vm;
267 mm->total_vm += new_len >> PAGE_SHIFT;
268 vm_stat_account(mm, vma->vm_flags, vma->vm_file, new_len>>PAGE_SHIFT);
269
270 if (do_munmap(mm, old_addr, old_len) < 0) {
271 /* OOM: unable to split vma, just get accounts right */
272 vm_unacct_memory(excess >> PAGE_SHIFT);
273 excess = 0;
274 }
275 mm->hiwater_vm = hiwater_vm;
276
277 /* Restore VM_ACCOUNT if one or two pieces of vma left */
278 if (excess) {
279 vma->vm_flags |= VM_ACCOUNT;
280 if (split)
281 vma->vm_next->vm_flags |= VM_ACCOUNT;
282 }
283
284 if (vm_flags & VM_LOCKED) {
285 mm->locked_vm += new_len >> PAGE_SHIFT;
286 if (new_len > old_len)
287 mlock_vma_pages_range(new_vma, new_addr + old_len,
288 new_addr + new_len);
289 }
290
291 return new_addr;
292}
293
294static struct vm_area_struct *vma_to_resize(unsigned long addr,
295 unsigned long old_len, unsigned long new_len, unsigned long *p)
296{
297 struct mm_struct *mm = current->mm;
298 struct vm_area_struct *vma = find_vma(mm, addr);
299
300 if (!vma || vma->vm_start > addr)
301 goto Efault;
302
303 if (is_vm_hugetlb_page(vma))
304 goto Einval;
305
306 /* We can't remap across vm area boundaries */
307 if (old_len > vma->vm_end - addr)
308 goto Efault;
309
310 /* Need to be careful about a growing mapping */
311 if (new_len > old_len) {
312 unsigned long pgoff;
313
314 if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
315 goto Efault;
316 pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
317 pgoff += vma->vm_pgoff;
318 if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
319 goto Einval;
320 }
321
322 if (vma->vm_flags & VM_LOCKED) {
323 unsigned long locked, lock_limit;
324 locked = mm->locked_vm << PAGE_SHIFT;
325 lock_limit = rlimit(RLIMIT_MEMLOCK);
326 locked += new_len - old_len;
327 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
328 goto Eagain;
329 }
330
331 if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT))
332 goto Enomem;
333
334 if (vma->vm_flags & VM_ACCOUNT) {
335 unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
336 if (security_vm_enough_memory_mm(mm, charged))
337 goto Efault;
338 *p = charged;
339 }
340
341 return vma;
342
343Efault: /* very odd choice for most of the cases, but... */
344 return ERR_PTR(-EFAULT);
345Einval:
346 return ERR_PTR(-EINVAL);
347Enomem:
348 return ERR_PTR(-ENOMEM);
349Eagain:
350 return ERR_PTR(-EAGAIN);
351}
352
353static unsigned long mremap_to(unsigned long addr,
354 unsigned long old_len, unsigned long new_addr,
355 unsigned long new_len)
356{
357 struct mm_struct *mm = current->mm;
358 struct vm_area_struct *vma;
359 unsigned long ret = -EINVAL;
360 unsigned long charged = 0;
361 unsigned long map_flags;
362
363 if (new_addr & ~PAGE_MASK)
364 goto out;
365
366 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
367 goto out;
368
369 /* Check if the location we're moving into overlaps the
370 * old location at all, and fail if it does.
371 */
372 if ((new_addr <= addr) && (new_addr+new_len) > addr)
373 goto out;
374
375 if ((addr <= new_addr) && (addr+old_len) > new_addr)
376 goto out;
377
378 ret = security_file_mmap(NULL, 0, 0, 0, new_addr, 1);
379 if (ret)
380 goto out;
381
382 ret = do_munmap(mm, new_addr, new_len);
383 if (ret)
384 goto out;
385
386 if (old_len >= new_len) {
387 ret = do_munmap(mm, addr+new_len, old_len - new_len);
388 if (ret && old_len != new_len)
389 goto out;
390 old_len = new_len;
391 }
392
393 vma = vma_to_resize(addr, old_len, new_len, &charged);
394 if (IS_ERR(vma)) {
395 ret = PTR_ERR(vma);
396 goto out;
397 }
398
399 map_flags = MAP_FIXED;
400 if (vma->vm_flags & VM_MAYSHARE)
401 map_flags |= MAP_SHARED;
402
403 ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
404 ((addr - vma->vm_start) >> PAGE_SHIFT),
405 map_flags);
406 if (ret & ~PAGE_MASK)
407 goto out1;
408
409 ret = move_vma(vma, addr, old_len, new_len, new_addr);
410 if (!(ret & ~PAGE_MASK))
411 goto out;
412out1:
413 vm_unacct_memory(charged);
414
415out:
416 return ret;
417}
418
419static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
420{
421 unsigned long end = vma->vm_end + delta;
422 if (end < vma->vm_end) /* overflow */
423 return 0;
424 if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
425 return 0;
426 if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
427 0, MAP_FIXED) & ~PAGE_MASK)
428 return 0;
429 return 1;
430}
431
432/*
433 * Expand (or shrink) an existing mapping, potentially moving it at the
434 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
435 *
436 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
437 * This option implies MREMAP_MAYMOVE.
438 */
439unsigned long do_mremap(unsigned long addr,
440 unsigned long old_len, unsigned long new_len,
441 unsigned long flags, unsigned long new_addr)
442{
443 struct mm_struct *mm = current->mm;
444 struct vm_area_struct *vma;
445 unsigned long ret = -EINVAL;
446 unsigned long charged = 0;
447
448 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
449 goto out;
450
451 if (addr & ~PAGE_MASK)
452 goto out;
453
454 old_len = PAGE_ALIGN(old_len);
455 new_len = PAGE_ALIGN(new_len);
456
457 /*
458 * We allow a zero old-len as a special case
459 * for DOS-emu "duplicate shm area" thing. But
460 * a zero new-len is nonsensical.
461 */
462 if (!new_len)
463 goto out;
464
465 if (flags & MREMAP_FIXED) {
466 if (flags & MREMAP_MAYMOVE)
467 ret = mremap_to(addr, old_len, new_addr, new_len);
468 goto out;
469 }
470
471 /*
472 * Always allow a shrinking remap: that just unmaps
473 * the unnecessary pages..
474 * do_munmap does all the needed commit accounting
475 */
476 if (old_len >= new_len) {
477 ret = do_munmap(mm, addr+new_len, old_len - new_len);
478 if (ret && old_len != new_len)
479 goto out;
480 ret = addr;
481 goto out;
482 }
483
484 /*
485 * Ok, we need to grow..
486 */
487 vma = vma_to_resize(addr, old_len, new_len, &charged);
488 if (IS_ERR(vma)) {
489 ret = PTR_ERR(vma);
490 goto out;
491 }
492
493 /* old_len exactly to the end of the area..
494 */
495 if (old_len == vma->vm_end - addr) {
496 /* can we just expand the current mapping? */
497 if (vma_expandable(vma, new_len - old_len)) {
498 int pages = (new_len - old_len) >> PAGE_SHIFT;
499
500 if (vma_adjust(vma, vma->vm_start, addr + new_len,
501 vma->vm_pgoff, NULL)) {
502 ret = -ENOMEM;
503 goto out;
504 }
505
506 mm->total_vm += pages;
507 vm_stat_account(mm, vma->vm_flags, vma->vm_file, pages);
508 if (vma->vm_flags & VM_LOCKED) {
509 mm->locked_vm += pages;
510 mlock_vma_pages_range(vma, addr + old_len,
511 addr + new_len);
512 }
513 ret = addr;
514 goto out;
515 }
516 }
517
518 /*
519 * We weren't able to just expand or shrink the area,
520 * we need to create a new one and move it..
521 */
522 ret = -ENOMEM;
523 if (flags & MREMAP_MAYMOVE) {
524 unsigned long map_flags = 0;
525 if (vma->vm_flags & VM_MAYSHARE)
526 map_flags |= MAP_SHARED;
527
528 new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
529 vma->vm_pgoff +
530 ((addr - vma->vm_start) >> PAGE_SHIFT),
531 map_flags);
532 if (new_addr & ~PAGE_MASK) {
533 ret = new_addr;
534 goto out;
535 }
536
537 ret = security_file_mmap(NULL, 0, 0, 0, new_addr, 1);
538 if (ret)
539 goto out;
540 ret = move_vma(vma, addr, old_len, new_len, new_addr);
541 }
542out:
543 if (ret & ~PAGE_MASK)
544 vm_unacct_memory(charged);
545 return ret;
546}
547
548SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
549 unsigned long, new_len, unsigned long, flags,
550 unsigned long, new_addr)
551{
552 unsigned long ret;
553
554 down_write(&current->mm->mmap_sem);
555 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
556 up_write(&current->mm->mmap_sem);
557 return ret;
558}