]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * mm/mremap.c | |
3 | * | |
4 | * (C) Copyright 1996 Linus Torvalds | |
5 | * | |
046c6884 | 6 | * Address space accounting code <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
7 | * (C) Copyright 2002 Red Hat Inc, All Rights Reserved |
8 | */ | |
9 | ||
10 | #include <linux/mm.h> | |
11 | #include <linux/hugetlb.h> | |
1da177e4 | 12 | #include <linux/shm.h> |
1ff82995 | 13 | #include <linux/ksm.h> |
1da177e4 LT |
14 | #include <linux/mman.h> |
15 | #include <linux/swap.h> | |
c59ede7b | 16 | #include <linux/capability.h> |
1da177e4 | 17 | #include <linux/fs.h> |
6dec97dc | 18 | #include <linux/swapops.h> |
1da177e4 LT |
19 | #include <linux/highmem.h> |
20 | #include <linux/security.h> | |
21 | #include <linux/syscalls.h> | |
cddb8a5c | 22 | #include <linux/mmu_notifier.h> |
2581d202 | 23 | #include <linux/uaccess.h> |
4abad2ca | 24 | #include <linux/mm-arch-hooks.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 | ||
7be7a546 | 32 | static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr) |
1da177e4 LT |
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); | |
37a1c49a | 47 | if (pmd_none(*pmd)) |
1da177e4 LT |
48 | return NULL; |
49 | ||
7be7a546 | 50 | return pmd; |
1da177e4 LT |
51 | } |
52 | ||
8ac1f832 AA |
53 | static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma, |
54 | unsigned long addr) | |
1da177e4 LT |
55 | { |
56 | pgd_t *pgd; | |
57 | pud_t *pud; | |
c74df32c | 58 | pmd_t *pmd; |
1da177e4 LT |
59 | |
60 | pgd = pgd_offset(mm, addr); | |
1da177e4 LT |
61 | pud = pud_alloc(mm, pgd, addr); |
62 | if (!pud) | |
c74df32c | 63 | return NULL; |
7be7a546 | 64 | |
1da177e4 | 65 | pmd = pmd_alloc(mm, pud, addr); |
57a8f0cd | 66 | if (!pmd) |
c74df32c | 67 | return NULL; |
7be7a546 | 68 | |
8ac1f832 | 69 | VM_BUG_ON(pmd_trans_huge(*pmd)); |
c74df32c | 70 | |
7be7a546 | 71 | return pmd; |
1da177e4 LT |
72 | } |
73 | ||
1d069b7d HD |
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 | ||
6dec97dc CG |
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); | |
6dec97dc CG |
101 | #endif |
102 | return pte; | |
103 | } | |
104 | ||
7be7a546 HD |
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, | |
5d190420 | 108 | unsigned long new_addr, bool need_rmap_locks, bool *need_flush) |
1da177e4 | 109 | { |
1da177e4 | 110 | struct mm_struct *mm = vma->vm_mm; |
7be7a546 | 111 | pte_t *old_pte, *new_pte, pte; |
4c21e2f2 | 112 | spinlock_t *old_ptl, *new_ptl; |
5d190420 AL |
113 | bool force_flush = false; |
114 | unsigned long len = old_end - old_addr; | |
1da177e4 | 115 | |
38a76013 | 116 | /* |
c8c06efa | 117 | * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma |
38a76013 ML |
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 | */ | |
1d069b7d HD |
134 | if (need_rmap_locks) |
135 | take_rmap_locks(vma); | |
1da177e4 | 136 | |
4c21e2f2 HD |
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 | */ | |
c74df32c | 141 | old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl); |
ece0e2b6 | 142 | new_pte = pte_offset_map(new_pmd, new_addr); |
4c21e2f2 HD |
143 | new_ptl = pte_lockptr(mm, new_pmd); |
144 | if (new_ptl != old_ptl) | |
f20dc5f7 | 145 | spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); |
6606c3e0 | 146 | arch_enter_lazy_mmu_mode(); |
7be7a546 HD |
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; | |
5d190420 | 152 | |
a2ce2666 | 153 | pte = ptep_get_and_clear(mm, old_addr, old_pte); |
5d190420 | 154 | /* |
a2ce2666 AL |
155 | * If we are remapping a dirty PTE, make sure |
156 | * to flush TLB before we drop the PTL for the | |
5d190420 | 157 | * old PTE or we may race with page_mkclean(). |
a2ce2666 AL |
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. | |
5d190420 | 162 | */ |
a2ce2666 | 163 | if (pte_present(pte) && pte_dirty(pte)) |
5d190420 | 164 | force_flush = true; |
7be7a546 | 165 | pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr); |
6dec97dc CG |
166 | pte = move_soft_dirty_pte(pte); |
167 | set_pte_at(mm, new_addr, new_pte, pte); | |
1da177e4 | 168 | } |
7be7a546 | 169 | |
6606c3e0 | 170 | arch_leave_lazy_mmu_mode(); |
4c21e2f2 HD |
171 | if (new_ptl != old_ptl) |
172 | spin_unlock(new_ptl); | |
ece0e2b6 | 173 | pte_unmap(new_pte - 1); |
5d190420 AL |
174 | if (force_flush) |
175 | flush_tlb_range(vma, old_end - len, old_end); | |
176 | else | |
177 | *need_flush = true; | |
c74df32c | 178 | pte_unmap_unlock(old_pte - 1, old_ptl); |
1d069b7d HD |
179 | if (need_rmap_locks) |
180 | drop_rmap_locks(vma); | |
1da177e4 LT |
181 | } |
182 | ||
7be7a546 HD |
183 | #define LATENCY_LIMIT (64 * PAGE_SIZE) |
184 | ||
b6a2fea3 | 185 | unsigned long move_page_tables(struct vm_area_struct *vma, |
1da177e4 | 186 | unsigned long old_addr, struct vm_area_struct *new_vma, |
38a76013 ML |
187 | unsigned long new_addr, unsigned long len, |
188 | bool need_rmap_locks) | |
1da177e4 | 189 | { |
7be7a546 HD |
190 | unsigned long extent, next, old_end; |
191 | pmd_t *old_pmd, *new_pmd; | |
7b6efc2b | 192 | bool need_flush = false; |
2ec74c3e SG |
193 | unsigned long mmun_start; /* For mmu_notifiers */ |
194 | unsigned long mmun_end; /* For mmu_notifiers */ | |
1da177e4 | 195 | |
7be7a546 HD |
196 | old_end = old_addr + len; |
197 | flush_cache_range(vma, old_addr, old_end); | |
1da177e4 | 198 | |
2ec74c3e SG |
199 | mmun_start = old_addr; |
200 | mmun_end = old_end; | |
201 | mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end); | |
7b6efc2b | 202 | |
7be7a546 | 203 | for (; old_addr < old_end; old_addr += extent, new_addr += extent) { |
1da177e4 | 204 | cond_resched(); |
7be7a546 | 205 | next = (old_addr + PMD_SIZE) & PMD_MASK; |
ebed4846 | 206 | /* even if next overflowed, extent below will be ok */ |
7be7a546 | 207 | extent = next - old_addr; |
ebed4846 AA |
208 | if (extent > old_end - old_addr) |
209 | extent = old_end - old_addr; | |
7be7a546 HD |
210 | old_pmd = get_old_pmd(vma->vm_mm, old_addr); |
211 | if (!old_pmd) | |
212 | continue; | |
8ac1f832 | 213 | new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr); |
7be7a546 HD |
214 | if (!new_pmd) |
215 | break; | |
37a1c49a | 216 | if (pmd_trans_huge(*old_pmd)) { |
dd18dbc2 | 217 | if (extent == HPAGE_PMD_SIZE) { |
4b471e88 | 218 | bool moved; |
dd18dbc2 KS |
219 | /* See comment in move_ptes() */ |
220 | if (need_rmap_locks) | |
1d069b7d | 221 | take_rmap_locks(vma); |
bf8616d5 | 222 | moved = move_huge_pmd(vma, old_addr, new_addr, |
5d190420 AL |
223 | old_end, old_pmd, new_pmd, |
224 | &need_flush); | |
dd18dbc2 | 225 | if (need_rmap_locks) |
1d069b7d | 226 | drop_rmap_locks(vma); |
5d190420 | 227 | if (moved) |
4b471e88 | 228 | continue; |
dd18dbc2 | 229 | } |
4b471e88 | 230 | split_huge_pmd(vma, old_pmd, old_addr); |
337d9abf | 231 | if (pmd_trans_unstable(old_pmd)) |
6b9116a6 | 232 | continue; |
37a1c49a | 233 | } |
3ed3a4f0 | 234 | if (pte_alloc(new_vma->vm_mm, new_pmd, new_addr)) |
37a1c49a | 235 | break; |
7be7a546 HD |
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; | |
5d190420 AL |
241 | move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma, |
242 | new_pmd, new_addr, need_rmap_locks, &need_flush); | |
1da177e4 | 243 | } |
5d190420 | 244 | if (need_flush) |
7b6efc2b AA |
245 | flush_tlb_range(vma, old_end-len, old_addr); |
246 | ||
2ec74c3e | 247 | mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end); |
7be7a546 HD |
248 | |
249 | return len + old_addr - old_end; /* how much done */ | |
1da177e4 LT |
250 | } |
251 | ||
252 | static unsigned long move_vma(struct vm_area_struct *vma, | |
253 | unsigned long old_addr, unsigned long old_len, | |
72f87654 | 254 | unsigned long new_len, unsigned long new_addr, |
897ab3e0 MR |
255 | bool *locked, struct vm_userfaultfd_ctx *uf, |
256 | struct list_head *uf_unmap) | |
1da177e4 LT |
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; | |
365e9c87 | 264 | unsigned long hiwater_vm; |
1da177e4 | 265 | int split = 0; |
7103ad32 | 266 | int err; |
38a76013 | 267 | bool need_rmap_locks; |
1da177e4 LT |
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 | ||
1ff82995 HD |
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 | */ | |
7103ad32 HD |
283 | err = ksm_madvise(vma, old_addr, old_addr + old_len, |
284 | MADV_UNMERGEABLE, &vm_flags); | |
285 | if (err) | |
286 | return err; | |
1ff82995 | 287 | |
1da177e4 | 288 | new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); |
38a76013 ML |
289 | new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, |
290 | &need_rmap_locks); | |
1da177e4 LT |
291 | if (!new_vma) |
292 | return -ENOMEM; | |
293 | ||
38a76013 ML |
294 | moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len, |
295 | need_rmap_locks); | |
1da177e4 | 296 | if (moved_len < old_len) { |
df1eab30 | 297 | err = -ENOMEM; |
5477e70a ON |
298 | } else if (vma->vm_ops && vma->vm_ops->mremap) { |
299 | err = vma->vm_ops->mremap(new_vma); | |
df1eab30 ON |
300 | } |
301 | ||
302 | if (unlikely(err)) { | |
1da177e4 LT |
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 | */ | |
38a76013 ML |
308 | move_page_tables(new_vma, new_addr, vma, old_addr, moved_len, |
309 | true); | |
1da177e4 LT |
310 | vma = new_vma; |
311 | old_len = new_len; | |
312 | old_addr = new_addr; | |
df1eab30 | 313 | new_addr = err; |
4abad2ca | 314 | } else { |
72f87654 | 315 | mremap_userfaultfd_prep(new_vma, uf); |
4abad2ca LD |
316 | arch_remap(mm, old_addr, old_addr + old_len, |
317 | new_addr, new_addr + new_len); | |
b2edffdd | 318 | } |
1da177e4 LT |
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 | ||
71799062 | 329 | /* |
365e9c87 HD |
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(). | |
71799062 | 337 | */ |
365e9c87 | 338 | hiwater_vm = mm->hiwater_vm; |
84638335 | 339 | vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT); |
71799062 | 340 | |
d9fe4fab TK |
341 | /* Tell pfnmap has moved from this vma */ |
342 | if (unlikely(vma->vm_flags & VM_PFNMAP)) | |
343 | untrack_pfn_moved(vma); | |
344 | ||
897ab3e0 | 345 | if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { |
1da177e4 LT |
346 | /* OOM: unable to split vma, just get accounts right */ |
347 | vm_unacct_memory(excess >> PAGE_SHIFT); | |
348 | excess = 0; | |
349 | } | |
365e9c87 | 350 | mm->hiwater_vm = hiwater_vm; |
1da177e4 LT |
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 | ||
1da177e4 LT |
359 | if (vm_flags & VM_LOCKED) { |
360 | mm->locked_vm += new_len >> PAGE_SHIFT; | |
81909b84 | 361 | *locked = true; |
1da177e4 LT |
362 | } |
363 | ||
364 | return new_addr; | |
365 | } | |
366 | ||
54f5de70 AV |
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); | |
1d391686 | 372 | unsigned long pgoff; |
54f5de70 AV |
373 | |
374 | if (!vma || vma->vm_start > addr) | |
6cd57613 | 375 | return ERR_PTR(-EFAULT); |
54f5de70 AV |
376 | |
377 | if (is_vm_hugetlb_page(vma)) | |
6cd57613 | 378 | return ERR_PTR(-EINVAL); |
54f5de70 AV |
379 | |
380 | /* We can't remap across vm area boundaries */ | |
381 | if (old_len > vma->vm_end - addr) | |
6cd57613 | 382 | return ERR_PTR(-EFAULT); |
54f5de70 | 383 | |
1d391686 ON |
384 | if (new_len == old_len) |
385 | return vma; | |
386 | ||
982134ba | 387 | /* Need to be careful about a growing mapping */ |
1d391686 ON |
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); | |
54f5de70 AV |
395 | |
396 | if (vma->vm_flags & VM_LOCKED) { | |
397 | unsigned long locked, lock_limit; | |
398 | locked = mm->locked_vm << PAGE_SHIFT; | |
59e99e5b | 399 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
54f5de70 AV |
400 | locked += new_len - old_len; |
401 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) | |
6cd57613 | 402 | return ERR_PTR(-EAGAIN); |
54f5de70 AV |
403 | } |
404 | ||
84638335 KK |
405 | if (!may_expand_vm(mm, vma->vm_flags, |
406 | (new_len - old_len) >> PAGE_SHIFT)) | |
6cd57613 | 407 | return ERR_PTR(-ENOMEM); |
54f5de70 AV |
408 | |
409 | if (vma->vm_flags & VM_ACCOUNT) { | |
410 | unsigned long charged = (new_len - old_len) >> PAGE_SHIFT; | |
191c5424 | 411 | if (security_vm_enough_memory_mm(mm, charged)) |
6cd57613 | 412 | return ERR_PTR(-ENOMEM); |
54f5de70 AV |
413 | *p = charged; |
414 | } | |
415 | ||
416 | return vma; | |
54f5de70 AV |
417 | } |
418 | ||
81909b84 | 419 | static unsigned long mremap_to(unsigned long addr, unsigned long old_len, |
72f87654 | 420 | unsigned long new_addr, unsigned long new_len, bool *locked, |
897ab3e0 MR |
421 | struct vm_userfaultfd_ctx *uf, |
422 | struct list_head *uf_unmap) | |
ecc1a899 AV |
423 | { |
424 | struct mm_struct *mm = current->mm; | |
425 | struct vm_area_struct *vma; | |
426 | unsigned long ret = -EINVAL; | |
427 | unsigned long charged = 0; | |
097eed10 | 428 | unsigned long map_flags; |
ecc1a899 | 429 | |
f19cb115 | 430 | if (offset_in_page(new_addr)) |
ecc1a899 AV |
431 | goto out; |
432 | ||
433 | if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len) | |
434 | goto out; | |
435 | ||
9943242c ON |
436 | /* Ensure the old/new locations do not overlap */ |
437 | if (addr + old_len > new_addr && new_addr + new_len > addr) | |
ecc1a899 AV |
438 | goto out; |
439 | ||
897ab3e0 | 440 | ret = do_munmap(mm, new_addr, new_len, NULL); |
ecc1a899 AV |
441 | if (ret) |
442 | goto out; | |
443 | ||
444 | if (old_len >= new_len) { | |
897ab3e0 | 445 | ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap); |
ecc1a899 AV |
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 | ||
097eed10 AV |
457 | map_flags = MAP_FIXED; |
458 | if (vma->vm_flags & VM_MAYSHARE) | |
459 | map_flags |= MAP_SHARED; | |
9206de95 | 460 | |
097eed10 AV |
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); | |
f19cb115 | 464 | if (offset_in_page(ret)) |
097eed10 AV |
465 | goto out1; |
466 | ||
897ab3e0 MR |
467 | ret = move_vma(vma, addr, old_len, new_len, new_addr, locked, uf, |
468 | uf_unmap); | |
f19cb115 | 469 | if (!(offset_in_page(ret))) |
097eed10 AV |
470 | goto out; |
471 | out1: | |
472 | vm_unacct_memory(charged); | |
ecc1a899 AV |
473 | |
474 | out: | |
475 | return ret; | |
476 | } | |
477 | ||
1a0ef85f AV |
478 | static int vma_expandable(struct vm_area_struct *vma, unsigned long delta) |
479 | { | |
f106af4e | 480 | unsigned long end = vma->vm_end + delta; |
9206de95 | 481 | if (end < vma->vm_end) /* overflow */ |
f106af4e | 482 | return 0; |
9206de95 | 483 | if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */ |
f106af4e AV |
484 | return 0; |
485 | if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start, | |
486 | 0, MAP_FIXED) & ~PAGE_MASK) | |
1a0ef85f | 487 | return 0; |
1a0ef85f AV |
488 | return 1; |
489 | } | |
490 | ||
1da177e4 LT |
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 | */ | |
63a81db1 AV |
498 | SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, |
499 | unsigned long, new_len, unsigned long, flags, | |
500 | unsigned long, new_addr) | |
1da177e4 | 501 | { |
d0de32d9 | 502 | struct mm_struct *mm = current->mm; |
1da177e4 LT |
503 | struct vm_area_struct *vma; |
504 | unsigned long ret = -EINVAL; | |
505 | unsigned long charged = 0; | |
81909b84 | 506 | bool locked = false; |
72f87654 | 507 | struct vm_userfaultfd_ctx uf = NULL_VM_UFFD_CTX; |
897ab3e0 | 508 | LIST_HEAD(uf_unmap); |
1da177e4 LT |
509 | |
510 | if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE)) | |
9a2458a6 RV |
511 | return ret; |
512 | ||
513 | if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE)) | |
514 | return ret; | |
1da177e4 | 515 | |
f19cb115 | 516 | if (offset_in_page(addr)) |
9a2458a6 | 517 | return ret; |
1da177e4 LT |
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) | |
9a2458a6 RV |
528 | return ret; |
529 | ||
dc0ef0df MH |
530 | if (down_write_killable(¤t->mm->mmap_sem)) |
531 | return -EINTR; | |
1da177e4 | 532 | |
1da177e4 | 533 | if (flags & MREMAP_FIXED) { |
9a2458a6 | 534 | ret = mremap_to(addr, old_len, new_addr, new_len, |
897ab3e0 | 535 | &locked, &uf, &uf_unmap); |
ecc1a899 | 536 | goto out; |
1da177e4 LT |
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) { | |
897ab3e0 | 545 | ret = do_munmap(mm, addr+new_len, old_len - new_len, &uf_unmap); |
1da177e4 LT |
546 | if (ret && old_len != new_len) |
547 | goto out; | |
548 | ret = addr; | |
ecc1a899 | 549 | goto out; |
1da177e4 LT |
550 | } |
551 | ||
552 | /* | |
ecc1a899 | 553 | * Ok, we need to grow.. |
1da177e4 | 554 | */ |
54f5de70 AV |
555 | vma = vma_to_resize(addr, old_len, new_len, &charged); |
556 | if (IS_ERR(vma)) { | |
557 | ret = PTR_ERR(vma); | |
1da177e4 | 558 | goto out; |
119f657c | 559 | } |
1da177e4 | 560 | |
1da177e4 | 561 | /* old_len exactly to the end of the area.. |
1da177e4 | 562 | */ |
ecc1a899 | 563 | if (old_len == vma->vm_end - addr) { |
1da177e4 | 564 | /* can we just expand the current mapping? */ |
1a0ef85f | 565 | if (vma_expandable(vma, new_len - old_len)) { |
1da177e4 LT |
566 | int pages = (new_len - old_len) >> PAGE_SHIFT; |
567 | ||
5beb4930 RR |
568 | if (vma_adjust(vma, vma->vm_start, addr + new_len, |
569 | vma->vm_pgoff, NULL)) { | |
570 | ret = -ENOMEM; | |
571 | goto out; | |
572 | } | |
1da177e4 | 573 | |
84638335 | 574 | vm_stat_account(mm, vma->vm_flags, pages); |
1da177e4 | 575 | if (vma->vm_flags & VM_LOCKED) { |
d0de32d9 | 576 | mm->locked_vm += pages; |
81909b84 ML |
577 | locked = true; |
578 | new_addr = addr; | |
1da177e4 LT |
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) { | |
ecc1a899 AV |
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, | |
93587414 AV |
596 | vma->vm_pgoff + |
597 | ((addr - vma->vm_start) >> PAGE_SHIFT), | |
598 | map_flags); | |
f19cb115 | 599 | if (offset_in_page(new_addr)) { |
ecc1a899 AV |
600 | ret = new_addr; |
601 | goto out; | |
1da177e4 | 602 | } |
ecc1a899 | 603 | |
72f87654 | 604 | ret = move_vma(vma, addr, old_len, new_len, new_addr, |
897ab3e0 | 605 | &locked, &uf, &uf_unmap); |
1da177e4 LT |
606 | } |
607 | out: | |
f19cb115 | 608 | if (offset_in_page(ret)) { |
1da177e4 | 609 | vm_unacct_memory(charged); |
d456fb9e ON |
610 | locked = 0; |
611 | } | |
1da177e4 | 612 | up_write(¤t->mm->mmap_sem); |
81909b84 ML |
613 | if (locked && new_len > old_len) |
614 | mm_populate(new_addr + old_len, new_len - old_len); | |
90794bf1 | 615 | mremap_userfaultfd_complete(&uf, addr, new_addr, old_len); |
897ab3e0 | 616 | userfaultfd_unmap_complete(mm, &uf_unmap); |
1da177e4 LT |
617 | return ret; |
618 | } |