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