xref: /openbmc/linux/mm/mremap.c (revision 4e1a33b1)
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 		struct list_head *uf_unmap)
257 {
258 	struct mm_struct *mm = vma->vm_mm;
259 	struct vm_area_struct *new_vma;
260 	unsigned long vm_flags = vma->vm_flags;
261 	unsigned long new_pgoff;
262 	unsigned long moved_len;
263 	unsigned long excess = 0;
264 	unsigned long hiwater_vm;
265 	int split = 0;
266 	int err;
267 	bool need_rmap_locks;
268 
269 	/*
270 	 * We'd prefer to avoid failure later on in do_munmap:
271 	 * which may split one vma into three before unmapping.
272 	 */
273 	if (mm->map_count >= sysctl_max_map_count - 3)
274 		return -ENOMEM;
275 
276 	/*
277 	 * Advise KSM to break any KSM pages in the area to be moved:
278 	 * it would be confusing if they were to turn up at the new
279 	 * location, where they happen to coincide with different KSM
280 	 * pages recently unmapped.  But leave vma->vm_flags as it was,
281 	 * so KSM can come around to merge on vma and new_vma afterwards.
282 	 */
283 	err = ksm_madvise(vma, old_addr, old_addr + old_len,
284 						MADV_UNMERGEABLE, &vm_flags);
285 	if (err)
286 		return err;
287 
288 	new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
289 	new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
290 			   &need_rmap_locks);
291 	if (!new_vma)
292 		return -ENOMEM;
293 
294 	moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
295 				     need_rmap_locks);
296 	if (moved_len < old_len) {
297 		err = -ENOMEM;
298 	} else if (vma->vm_ops && vma->vm_ops->mremap) {
299 		err = vma->vm_ops->mremap(new_vma);
300 	}
301 
302 	if (unlikely(err)) {
303 		/*
304 		 * On error, move entries back from new area to old,
305 		 * which will succeed since page tables still there,
306 		 * and then proceed to unmap new area instead of old.
307 		 */
308 		move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
309 				 true);
310 		vma = new_vma;
311 		old_len = new_len;
312 		old_addr = new_addr;
313 		new_addr = err;
314 	} else {
315 		mremap_userfaultfd_prep(new_vma, uf);
316 		arch_remap(mm, old_addr, old_addr + old_len,
317 			   new_addr, new_addr + new_len);
318 	}
319 
320 	/* Conceal VM_ACCOUNT so old reservation is not undone */
321 	if (vm_flags & VM_ACCOUNT) {
322 		vma->vm_flags &= ~VM_ACCOUNT;
323 		excess = vma->vm_end - vma->vm_start - old_len;
324 		if (old_addr > vma->vm_start &&
325 		    old_addr + old_len < vma->vm_end)
326 			split = 1;
327 	}
328 
329 	/*
330 	 * If we failed to move page tables we still do total_vm increment
331 	 * since do_munmap() will decrement it by old_len == new_len.
332 	 *
333 	 * Since total_vm is about to be raised artificially high for a
334 	 * moment, we need to restore high watermark afterwards: if stats
335 	 * are taken meanwhile, total_vm and hiwater_vm appear too high.
336 	 * If this were a serious issue, we'd add a flag to do_munmap().
337 	 */
338 	hiwater_vm = mm->hiwater_vm;
339 	vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
340 
341 	/* Tell pfnmap has moved from this vma */
342 	if (unlikely(vma->vm_flags & VM_PFNMAP))
343 		untrack_pfn_moved(vma);
344 
345 	if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) {
346 		/* OOM: unable to split vma, just get accounts right */
347 		vm_unacct_memory(excess >> PAGE_SHIFT);
348 		excess = 0;
349 	}
350 	mm->hiwater_vm = hiwater_vm;
351 
352 	/* Restore VM_ACCOUNT if one or two pieces of vma left */
353 	if (excess) {
354 		vma->vm_flags |= VM_ACCOUNT;
355 		if (split)
356 			vma->vm_next->vm_flags |= VM_ACCOUNT;
357 	}
358 
359 	if (vm_flags & VM_LOCKED) {
360 		mm->locked_vm += new_len >> PAGE_SHIFT;
361 		*locked = true;
362 	}
363 
364 	return new_addr;
365 }
366 
367 static struct vm_area_struct *vma_to_resize(unsigned long addr,
368 	unsigned long old_len, unsigned long new_len, unsigned long *p)
369 {
370 	struct mm_struct *mm = current->mm;
371 	struct vm_area_struct *vma = find_vma(mm, addr);
372 	unsigned long pgoff;
373 
374 	if (!vma || vma->vm_start > addr)
375 		return ERR_PTR(-EFAULT);
376 
377 	if (is_vm_hugetlb_page(vma))
378 		return ERR_PTR(-EINVAL);
379 
380 	/* We can't remap across vm area boundaries */
381 	if (old_len > vma->vm_end - addr)
382 		return ERR_PTR(-EFAULT);
383 
384 	if (new_len == old_len)
385 		return vma;
386 
387 	/* Need to be careful about a growing mapping */
388 	pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
389 	pgoff += vma->vm_pgoff;
390 	if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
391 		return ERR_PTR(-EINVAL);
392 
393 	if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
394 		return ERR_PTR(-EFAULT);
395 
396 	if (vma->vm_flags & VM_LOCKED) {
397 		unsigned long locked, lock_limit;
398 		locked = mm->locked_vm << PAGE_SHIFT;
399 		lock_limit = rlimit(RLIMIT_MEMLOCK);
400 		locked += new_len - old_len;
401 		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
402 			return ERR_PTR(-EAGAIN);
403 	}
404 
405 	if (!may_expand_vm(mm, vma->vm_flags,
406 				(new_len - old_len) >> PAGE_SHIFT))
407 		return ERR_PTR(-ENOMEM);
408 
409 	if (vma->vm_flags & VM_ACCOUNT) {
410 		unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
411 		if (security_vm_enough_memory_mm(mm, charged))
412 			return ERR_PTR(-ENOMEM);
413 		*p = charged;
414 	}
415 
416 	return vma;
417 }
418 
419 static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
420 		unsigned long new_addr, unsigned long new_len, bool *locked,
421 		struct vm_userfaultfd_ctx *uf,
422 		struct list_head *uf_unmap)
423 {
424 	struct mm_struct *mm = current->mm;
425 	struct vm_area_struct *vma;
426 	unsigned long ret = -EINVAL;
427 	unsigned long charged = 0;
428 	unsigned long map_flags;
429 
430 	if (offset_in_page(new_addr))
431 		goto out;
432 
433 	if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
434 		goto out;
435 
436 	/* Ensure the old/new locations do not overlap */
437 	if (addr + old_len > new_addr && new_addr + new_len > addr)
438 		goto out;
439 
440 	ret = do_munmap(mm, new_addr, new_len, NULL);
441 	if (ret)
442 		goto out;
443 
444 	if (old_len >= new_len) {
445 		ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
446 		if (ret && old_len != new_len)
447 			goto out;
448 		old_len = new_len;
449 	}
450 
451 	vma = vma_to_resize(addr, old_len, new_len, &charged);
452 	if (IS_ERR(vma)) {
453 		ret = PTR_ERR(vma);
454 		goto out;
455 	}
456 
457 	map_flags = MAP_FIXED;
458 	if (vma->vm_flags & VM_MAYSHARE)
459 		map_flags |= MAP_SHARED;
460 
461 	ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
462 				((addr - vma->vm_start) >> PAGE_SHIFT),
463 				map_flags);
464 	if (offset_in_page(ret))
465 		goto out1;
466 
467 	ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, uf,
468 		       uf_unmap);
469 	if (!(offset_in_page(ret)))
470 		goto out;
471 out1:
472 	vm_unacct_memory(charged);
473 
474 out:
475 	return ret;
476 }
477 
478 static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
479 {
480 	unsigned long end = vma->vm_end + delta;
481 	if (end < vma->vm_end) /* overflow */
482 		return 0;
483 	if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
484 		return 0;
485 	if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
486 			      0, MAP_FIXED) & ~PAGE_MASK)
487 		return 0;
488 	return 1;
489 }
490 
491 /*
492  * Expand (or shrink) an existing mapping, potentially moving it at the
493  * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
494  *
495  * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
496  * This option implies MREMAP_MAYMOVE.
497  */
498 SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
499 		unsigned long, new_len, unsigned long, flags,
500 		unsigned long, new_addr)
501 {
502 	struct mm_struct *mm = current->mm;
503 	struct vm_area_struct *vma;
504 	unsigned long ret = -EINVAL;
505 	unsigned long charged = 0;
506 	bool locked = false;
507 	struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX;
508 	LIST_HEAD(uf_unmap);
509 
510 	if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
511 		return ret;
512 
513 	if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
514 		return ret;
515 
516 	if (offset_in_page(addr))
517 		return ret;
518 
519 	old_len = PAGE_ALIGN(old_len);
520 	new_len = PAGE_ALIGN(new_len);
521 
522 	/*
523 	 * We allow a zero old-len as a special case
524 	 * for DOS-emu "duplicate shm area" thing. But
525 	 * a zero new-len is nonsensical.
526 	 */
527 	if (!new_len)
528 		return ret;
529 
530 	if (down_write_killable(&current->mm->mmap_sem))
531 		return -EINTR;
532 
533 	if (flags & MREMAP_FIXED) {
534 		ret = mremap_to(addr, old_len, new_addr, new_len,
535 				&locked, &uf, &uf_unmap);
536 		goto out;
537 	}
538 
539 	/*
540 	 * Always allow a shrinking remap: that just unmaps
541 	 * the unnecessary pages..
542 	 * do_munmap does all the needed commit accounting
543 	 */
544 	if (old_len >= new_len) {
545 		ret = do_munmap(mm, addr+new_len, old_len - new_len, &uf_unmap);
546 		if (ret && old_len != new_len)
547 			goto out;
548 		ret = addr;
549 		goto out;
550 	}
551 
552 	/*
553 	 * Ok, we need to grow..
554 	 */
555 	vma = vma_to_resize(addr, old_len, new_len, &charged);
556 	if (IS_ERR(vma)) {
557 		ret = PTR_ERR(vma);
558 		goto out;
559 	}
560 
561 	/* old_len exactly to the end of the area..
562 	 */
563 	if (old_len == vma->vm_end - addr) {
564 		/* can we just expand the current mapping? */
565 		if (vma_expandable(vma, new_len - old_len)) {
566 			int pages = (new_len - old_len) >> PAGE_SHIFT;
567 
568 			if (vma_adjust(vma, vma->vm_start, addr + new_len,
569 				       vma->vm_pgoff, NULL)) {
570 				ret = -ENOMEM;
571 				goto out;
572 			}
573 
574 			vm_stat_account(mm, vma->vm_flags, pages);
575 			if (vma->vm_flags & VM_LOCKED) {
576 				mm->locked_vm += pages;
577 				locked = true;
578 				new_addr = addr;
579 			}
580 			ret = addr;
581 			goto out;
582 		}
583 	}
584 
585 	/*
586 	 * We weren't able to just expand or shrink the area,
587 	 * we need to create a new one and move it..
588 	 */
589 	ret = -ENOMEM;
590 	if (flags & MREMAP_MAYMOVE) {
591 		unsigned long map_flags = 0;
592 		if (vma->vm_flags & VM_MAYSHARE)
593 			map_flags |= MAP_SHARED;
594 
595 		new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
596 					vma->vm_pgoff +
597 					((addr - vma->vm_start) >> PAGE_SHIFT),
598 					map_flags);
599 		if (offset_in_page(new_addr)) {
600 			ret = new_addr;
601 			goto out;
602 		}
603 
604 		ret = move_vma(vma, addr, old_len, new_len, new_addr,
605 			       &locked, &uf, &uf_unmap);
606 	}
607 out:
608 	if (offset_in_page(ret)) {
609 		vm_unacct_memory(charged);
610 		locked = 0;
611 	}
612 	up_write(&current->mm->mmap_sem);
613 	if (locked && new_len > old_len)
614 		mm_populate(new_addr + old_len, new_len - old_len);
615 	mremap_userfaultfd_complete(&uf, addr, new_addr, old_len);
616 	userfaultfd_unmap_complete(mm, &uf_unmap);
617 	return ret;
618 }
619