]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * mm/mmap.c | |
4 | * | |
5 | * Written by obz. | |
6 | * | |
046c6884 | 7 | * Address space accounting code <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
8 | */ |
9 | ||
b1de0d13 MH |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
11 | ||
e8420a8e | 12 | #include <linux/kernel.h> |
1da177e4 | 13 | #include <linux/slab.h> |
4af3c9cc | 14 | #include <linux/backing-dev.h> |
1da177e4 | 15 | #include <linux/mm.h> |
17fca131 | 16 | #include <linux/mm_inline.h> |
1da177e4 LT |
17 | #include <linux/shm.h> |
18 | #include <linux/mman.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/swap.h> | |
21 | #include <linux/syscalls.h> | |
c59ede7b | 22 | #include <linux/capability.h> |
1da177e4 LT |
23 | #include <linux/init.h> |
24 | #include <linux/file.h> | |
25 | #include <linux/fs.h> | |
26 | #include <linux/personality.h> | |
27 | #include <linux/security.h> | |
28 | #include <linux/hugetlb.h> | |
c01d5b30 | 29 | #include <linux/shmem_fs.h> |
1da177e4 | 30 | #include <linux/profile.h> |
b95f1b31 | 31 | #include <linux/export.h> |
1da177e4 LT |
32 | #include <linux/mount.h> |
33 | #include <linux/mempolicy.h> | |
34 | #include <linux/rmap.h> | |
cddb8a5c | 35 | #include <linux/mmu_notifier.h> |
82f71ae4 | 36 | #include <linux/mmdebug.h> |
cdd6c482 | 37 | #include <linux/perf_event.h> |
120a795d | 38 | #include <linux/audit.h> |
b15d00b6 | 39 | #include <linux/khugepaged.h> |
2b144498 | 40 | #include <linux/uprobes.h> |
1640879a AS |
41 | #include <linux/notifier.h> |
42 | #include <linux/memory.h> | |
b1de0d13 | 43 | #include <linux/printk.h> |
19a809af | 44 | #include <linux/userfaultfd_k.h> |
d977d56c | 45 | #include <linux/moduleparam.h> |
62b5f7d0 | 46 | #include <linux/pkeys.h> |
21292580 | 47 | #include <linux/oom.h> |
04f5866e | 48 | #include <linux/sched/mm.h> |
1da177e4 | 49 | |
7c0f6ba6 | 50 | #include <linux/uaccess.h> |
1da177e4 LT |
51 | #include <asm/cacheflush.h> |
52 | #include <asm/tlb.h> | |
d6dd61c8 | 53 | #include <asm/mmu_context.h> |
1da177e4 | 54 | |
df529cab JK |
55 | #define CREATE_TRACE_POINTS |
56 | #include <trace/events/mmap.h> | |
57 | ||
42b77728 JB |
58 | #include "internal.h" |
59 | ||
3a459756 KK |
60 | #ifndef arch_mmap_check |
61 | #define arch_mmap_check(addr, len, flags) (0) | |
62 | #endif | |
63 | ||
d07e2259 DC |
64 | #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS |
65 | const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN; | |
66 | const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX; | |
67 | int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS; | |
68 | #endif | |
69 | #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS | |
70 | const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN; | |
71 | const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX; | |
72 | int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS; | |
73 | #endif | |
74 | ||
f4fcd558 | 75 | static bool ignore_rlimit_data; |
d977d56c | 76 | core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644); |
d07e2259 | 77 | |
763ecb03 | 78 | static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, |
e0da382c | 79 | struct vm_area_struct *vma, struct vm_area_struct *prev, |
763ecb03 | 80 | struct vm_area_struct *next, unsigned long start, |
68f48381 | 81 | unsigned long end, bool mm_wr_locked); |
e0da382c | 82 | |
64e45507 PF |
83 | static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags) |
84 | { | |
85 | return pgprot_modify(oldprot, vm_get_page_prot(vm_flags)); | |
86 | } | |
87 | ||
88 | /* Update vma->vm_page_prot to reflect vma->vm_flags. */ | |
89 | void vma_set_page_prot(struct vm_area_struct *vma) | |
90 | { | |
91 | unsigned long vm_flags = vma->vm_flags; | |
6d2329f8 | 92 | pgprot_t vm_page_prot; |
64e45507 | 93 | |
6d2329f8 AA |
94 | vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags); |
95 | if (vma_wants_writenotify(vma, vm_page_prot)) { | |
64e45507 | 96 | vm_flags &= ~VM_SHARED; |
6d2329f8 | 97 | vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags); |
64e45507 | 98 | } |
c1e8d7c6 | 99 | /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */ |
6d2329f8 | 100 | WRITE_ONCE(vma->vm_page_prot, vm_page_prot); |
64e45507 PF |
101 | } |
102 | ||
1da177e4 | 103 | /* |
c8c06efa | 104 | * Requires inode->i_mapping->i_mmap_rwsem |
1da177e4 LT |
105 | */ |
106 | static void __remove_shared_vm_struct(struct vm_area_struct *vma, | |
107 | struct file *file, struct address_space *mapping) | |
108 | { | |
1da177e4 | 109 | if (vma->vm_flags & VM_SHARED) |
4bb5f5d9 | 110 | mapping_unmap_writable(mapping); |
1da177e4 LT |
111 | |
112 | flush_dcache_mmap_lock(mapping); | |
27ba0644 | 113 | vma_interval_tree_remove(vma, &mapping->i_mmap); |
1da177e4 LT |
114 | flush_dcache_mmap_unlock(mapping); |
115 | } | |
116 | ||
117 | /* | |
6b2dbba8 | 118 | * Unlink a file-based vm structure from its interval tree, to hide |
a8fb5618 | 119 | * vma from rmap and vmtruncate before freeing its page tables. |
1da177e4 | 120 | */ |
a8fb5618 | 121 | void unlink_file_vma(struct vm_area_struct *vma) |
1da177e4 LT |
122 | { |
123 | struct file *file = vma->vm_file; | |
124 | ||
1da177e4 LT |
125 | if (file) { |
126 | struct address_space *mapping = file->f_mapping; | |
83cde9e8 | 127 | i_mmap_lock_write(mapping); |
1da177e4 | 128 | __remove_shared_vm_struct(vma, file, mapping); |
83cde9e8 | 129 | i_mmap_unlock_write(mapping); |
1da177e4 | 130 | } |
a8fb5618 HD |
131 | } |
132 | ||
133 | /* | |
763ecb03 | 134 | * Close a vm structure and free it. |
a8fb5618 | 135 | */ |
763ecb03 | 136 | static void remove_vma(struct vm_area_struct *vma) |
a8fb5618 | 137 | { |
a8fb5618 | 138 | might_sleep(); |
1da177e4 LT |
139 | if (vma->vm_ops && vma->vm_ops->close) |
140 | vma->vm_ops->close(vma); | |
e9714acf | 141 | if (vma->vm_file) |
a8fb5618 | 142 | fput(vma->vm_file); |
f0be3d32 | 143 | mpol_put(vma_policy(vma)); |
3928d4f5 | 144 | vm_area_free(vma); |
1da177e4 LT |
145 | } |
146 | ||
b62b633e LH |
147 | static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi, |
148 | unsigned long min) | |
149 | { | |
150 | return mas_prev(&vmi->mas, min); | |
151 | } | |
152 | ||
153 | static inline int vma_iter_clear_gfp(struct vma_iterator *vmi, | |
154 | unsigned long start, unsigned long end, gfp_t gfp) | |
155 | { | |
156 | vmi->mas.index = start; | |
157 | vmi->mas.last = end - 1; | |
158 | mas_store_gfp(&vmi->mas, NULL, gfp); | |
159 | if (unlikely(mas_is_err(&vmi->mas))) | |
160 | return -ENOMEM; | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
2e7ce7d3 LH |
165 | /* |
166 | * check_brk_limits() - Use platform specific check of range & verify mlock | |
167 | * limits. | |
168 | * @addr: The address to check | |
169 | * @len: The size of increase. | |
170 | * | |
171 | * Return: 0 on success. | |
172 | */ | |
173 | static int check_brk_limits(unsigned long addr, unsigned long len) | |
174 | { | |
175 | unsigned long mapped_addr; | |
176 | ||
177 | mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); | |
178 | if (IS_ERR_VALUE(mapped_addr)) | |
179 | return mapped_addr; | |
180 | ||
181 | return mlock_future_check(current->mm, current->mm->def_flags, len); | |
182 | } | |
92fed820 | 183 | static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, |
763ecb03 | 184 | unsigned long addr, unsigned long request, unsigned long flags); |
6a6160a7 | 185 | SYSCALL_DEFINE1(brk, unsigned long, brk) |
1da177e4 | 186 | { |
9bc8039e | 187 | unsigned long newbrk, oldbrk, origbrk; |
1da177e4 | 188 | struct mm_struct *mm = current->mm; |
2e7ce7d3 | 189 | struct vm_area_struct *brkvma, *next = NULL; |
a5b4592c | 190 | unsigned long min_brk; |
128557ff | 191 | bool populate; |
9bc8039e | 192 | bool downgraded = false; |
897ab3e0 | 193 | LIST_HEAD(uf); |
92fed820 | 194 | struct vma_iterator vmi; |
1da177e4 | 195 | |
d8ed45c5 | 196 | if (mmap_write_lock_killable(mm)) |
dc0ef0df | 197 | return -EINTR; |
1da177e4 | 198 | |
9bc8039e YS |
199 | origbrk = mm->brk; |
200 | ||
a5b4592c | 201 | #ifdef CONFIG_COMPAT_BRK |
5520e894 JK |
202 | /* |
203 | * CONFIG_COMPAT_BRK can still be overridden by setting | |
204 | * randomize_va_space to 2, which will still cause mm->start_brk | |
205 | * to be arbitrarily shifted | |
206 | */ | |
4471a675 | 207 | if (current->brk_randomized) |
5520e894 JK |
208 | min_brk = mm->start_brk; |
209 | else | |
210 | min_brk = mm->end_data; | |
a5b4592c JK |
211 | #else |
212 | min_brk = mm->start_brk; | |
213 | #endif | |
214 | if (brk < min_brk) | |
1da177e4 | 215 | goto out; |
1e624196 RG |
216 | |
217 | /* | |
218 | * Check against rlimit here. If this check is done later after the test | |
219 | * of oldbrk with newbrk then it can escape the test and let the data | |
220 | * segment grow beyond its set limit the in case where the limit is | |
221 | * not page aligned -Ram Gupta | |
222 | */ | |
8764b338 CG |
223 | if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk, |
224 | mm->end_data, mm->start_data)) | |
1e624196 RG |
225 | goto out; |
226 | ||
1da177e4 LT |
227 | newbrk = PAGE_ALIGN(brk); |
228 | oldbrk = PAGE_ALIGN(mm->brk); | |
9bc8039e YS |
229 | if (oldbrk == newbrk) { |
230 | mm->brk = brk; | |
231 | goto success; | |
232 | } | |
1da177e4 | 233 | |
9bc8039e YS |
234 | /* |
235 | * Always allow shrinking brk. | |
27b26701 | 236 | * do_vma_munmap() may downgrade mmap_lock to read. |
9bc8039e | 237 | */ |
1da177e4 | 238 | if (brk <= mm->brk) { |
9bc8039e YS |
239 | int ret; |
240 | ||
2e7ce7d3 | 241 | /* Search one past newbrk */ |
92fed820 LH |
242 | vma_iter_init(&vmi, mm, newbrk); |
243 | brkvma = vma_find(&vmi, oldbrk); | |
f5ad5083 | 244 | if (!brkvma || brkvma->vm_start >= oldbrk) |
2e7ce7d3 | 245 | goto out; /* mapping intersects with an existing non-brk vma. */ |
9bc8039e | 246 | /* |
2e7ce7d3 | 247 | * mm->brk must be protected by write mmap_lock. |
27b26701 LH |
248 | * do_vma_munmap() may downgrade the lock, so update it |
249 | * before calling do_vma_munmap(). | |
9bc8039e YS |
250 | */ |
251 | mm->brk = brk; | |
27b26701 | 252 | ret = do_vma_munmap(&vmi, brkvma, newbrk, oldbrk, &uf, true); |
2e7ce7d3 | 253 | if (ret == 1) { |
9bc8039e | 254 | downgraded = true; |
2e7ce7d3 LH |
255 | goto success; |
256 | } else if (!ret) | |
257 | goto success; | |
258 | ||
259 | mm->brk = origbrk; | |
260 | goto out; | |
1da177e4 LT |
261 | } |
262 | ||
2e7ce7d3 LH |
263 | if (check_brk_limits(oldbrk, newbrk - oldbrk)) |
264 | goto out; | |
265 | ||
266 | /* | |
267 | * Only check if the next VMA is within the stack_guard_gap of the | |
268 | * expansion area | |
269 | */ | |
92fed820 LH |
270 | vma_iter_init(&vmi, mm, oldbrk); |
271 | next = vma_find(&vmi, newbrk + PAGE_SIZE + stack_guard_gap); | |
1be7107f | 272 | if (next && newbrk + PAGE_SIZE > vm_start_gap(next)) |
1da177e4 LT |
273 | goto out; |
274 | ||
92fed820 | 275 | brkvma = vma_prev_limit(&vmi, mm->start_brk); |
1da177e4 | 276 | /* Ok, looks good - let it rip. */ |
92fed820 | 277 | if (do_brk_flags(&vmi, brkvma, oldbrk, newbrk - oldbrk, 0) < 0) |
1da177e4 | 278 | goto out; |
2e7ce7d3 | 279 | |
1da177e4 | 280 | mm->brk = brk; |
9bc8039e YS |
281 | |
282 | success: | |
128557ff | 283 | populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; |
9bc8039e | 284 | if (downgraded) |
d8ed45c5 | 285 | mmap_read_unlock(mm); |
9bc8039e | 286 | else |
d8ed45c5 | 287 | mmap_write_unlock(mm); |
897ab3e0 | 288 | userfaultfd_unmap_complete(mm, &uf); |
128557ff ML |
289 | if (populate) |
290 | mm_populate(oldbrk, newbrk - oldbrk); | |
291 | return brk; | |
292 | ||
1da177e4 | 293 | out: |
d8ed45c5 | 294 | mmap_write_unlock(mm); |
b7204006 | 295 | return origbrk; |
1da177e4 LT |
296 | } |
297 | ||
d4af56c5 LH |
298 | #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) |
299 | extern void mt_validate(struct maple_tree *mt); | |
300 | extern void mt_dump(const struct maple_tree *mt); | |
1da177e4 | 301 | |
d4af56c5 LH |
302 | /* Validate the maple tree */ |
303 | static void validate_mm_mt(struct mm_struct *mm) | |
304 | { | |
305 | struct maple_tree *mt = &mm->mm_mt; | |
763ecb03 | 306 | struct vm_area_struct *vma_mt; |
d4af56c5 LH |
307 | |
308 | MA_STATE(mas, mt, 0, 0); | |
309 | ||
310 | mt_validate(&mm->mm_mt); | |
311 | mas_for_each(&mas, vma_mt, ULONG_MAX) { | |
763ecb03 LH |
312 | if ((vma_mt->vm_start != mas.index) || |
313 | (vma_mt->vm_end - 1 != mas.last)) { | |
d4af56c5 LH |
314 | pr_emerg("issue in %s\n", current->comm); |
315 | dump_stack(); | |
d4af56c5 | 316 | dump_vma(vma_mt); |
d4af56c5 LH |
317 | pr_emerg("mt piv: %p %lu - %lu\n", vma_mt, |
318 | mas.index, mas.last); | |
319 | pr_emerg("mt vma: %p %lu - %lu\n", vma_mt, | |
320 | vma_mt->vm_start, vma_mt->vm_end); | |
d4af56c5 LH |
321 | |
322 | mt_dump(mas.tree); | |
323 | if (vma_mt->vm_end != mas.last + 1) { | |
324 | pr_err("vma: %p vma_mt %lu-%lu\tmt %lu-%lu\n", | |
325 | mm, vma_mt->vm_start, vma_mt->vm_end, | |
326 | mas.index, mas.last); | |
327 | mt_dump(mas.tree); | |
328 | } | |
329 | VM_BUG_ON_MM(vma_mt->vm_end != mas.last + 1, mm); | |
330 | if (vma_mt->vm_start != mas.index) { | |
331 | pr_err("vma: %p vma_mt %p %lu - %lu doesn't match\n", | |
332 | mm, vma_mt, vma_mt->vm_start, vma_mt->vm_end); | |
333 | mt_dump(mas.tree); | |
334 | } | |
335 | VM_BUG_ON_MM(vma_mt->vm_start != mas.index, mm); | |
336 | } | |
d4af56c5 | 337 | } |
d4af56c5 | 338 | } |
1da177e4 | 339 | |
eafd4dc4 | 340 | static void validate_mm(struct mm_struct *mm) |
1da177e4 LT |
341 | { |
342 | int bug = 0; | |
343 | int i = 0; | |
763ecb03 LH |
344 | struct vm_area_struct *vma; |
345 | MA_STATE(mas, &mm->mm_mt, 0, 0); | |
ff26f70f | 346 | |
524e00b3 LH |
347 | validate_mm_mt(mm); |
348 | ||
763ecb03 | 349 | mas_for_each(&mas, vma, ULONG_MAX) { |
524e00b3 | 350 | #ifdef CONFIG_DEBUG_VM_RB |
12352d3c | 351 | struct anon_vma *anon_vma = vma->anon_vma; |
ed8ea815 | 352 | struct anon_vma_chain *avc; |
ff26f70f | 353 | |
12352d3c KK |
354 | if (anon_vma) { |
355 | anon_vma_lock_read(anon_vma); | |
356 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) | |
357 | anon_vma_interval_tree_verify(avc); | |
358 | anon_vma_unlock_read(anon_vma); | |
359 | } | |
524e00b3 | 360 | #endif |
1da177e4 LT |
361 | i++; |
362 | } | |
5a0768f6 | 363 | if (i != mm->map_count) { |
763ecb03 | 364 | pr_emerg("map_count %d mas_for_each %d\n", mm->map_count, i); |
5a0768f6 ML |
365 | bug = 1; |
366 | } | |
96dad67f | 367 | VM_BUG_ON_MM(bug, mm); |
1da177e4 | 368 | } |
524e00b3 LH |
369 | |
370 | #else /* !CONFIG_DEBUG_VM_MAPLE_TREE */ | |
d4af56c5 | 371 | #define validate_mm_mt(root) do { } while (0) |
1da177e4 | 372 | #define validate_mm(mm) do { } while (0) |
524e00b3 | 373 | #endif /* CONFIG_DEBUG_VM_MAPLE_TREE */ |
8f26e0b1 | 374 | |
bf181b9f ML |
375 | /* |
376 | * vma has some anon_vma assigned, and is already inserted on that | |
377 | * anon_vma's interval trees. | |
378 | * | |
379 | * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the | |
380 | * vma must be removed from the anon_vma's interval trees using | |
381 | * anon_vma_interval_tree_pre_update_vma(). | |
382 | * | |
383 | * After the update, the vma will be reinserted using | |
384 | * anon_vma_interval_tree_post_update_vma(). | |
385 | * | |
c1e8d7c6 | 386 | * The entire update must be protected by exclusive mmap_lock and by |
bf181b9f ML |
387 | * the root anon_vma's mutex. |
388 | */ | |
389 | static inline void | |
390 | anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) | |
391 | { | |
392 | struct anon_vma_chain *avc; | |
393 | ||
394 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) | |
395 | anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); | |
396 | } | |
397 | ||
398 | static inline void | |
399 | anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) | |
400 | { | |
401 | struct anon_vma_chain *avc; | |
402 | ||
403 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) | |
404 | anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); | |
405 | } | |
406 | ||
e8420a8e CH |
407 | static unsigned long count_vma_pages_range(struct mm_struct *mm, |
408 | unsigned long addr, unsigned long end) | |
409 | { | |
2e3af1db | 410 | VMA_ITERATOR(vmi, mm, addr); |
e8420a8e | 411 | struct vm_area_struct *vma; |
2e3af1db | 412 | unsigned long nr_pages = 0; |
e8420a8e | 413 | |
2e3af1db MWO |
414 | for_each_vma_range(vmi, vma, end) { |
415 | unsigned long vm_start = max(addr, vma->vm_start); | |
416 | unsigned long vm_end = min(end, vma->vm_end); | |
e8420a8e | 417 | |
2e3af1db | 418 | nr_pages += PHYS_PFN(vm_end - vm_start); |
e8420a8e CH |
419 | } |
420 | ||
421 | return nr_pages; | |
422 | } | |
423 | ||
c154124f LH |
424 | static void __vma_link_file(struct vm_area_struct *vma, |
425 | struct address_space *mapping) | |
1da177e4 | 426 | { |
c154124f LH |
427 | if (vma->vm_flags & VM_SHARED) |
428 | mapping_allow_writable(mapping); | |
1da177e4 | 429 | |
c154124f LH |
430 | flush_dcache_mmap_lock(mapping); |
431 | vma_interval_tree_insert(vma, &mapping->i_mmap); | |
432 | flush_dcache_mmap_unlock(mapping); | |
1da177e4 LT |
433 | } |
434 | ||
763ecb03 | 435 | static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) |
1da177e4 | 436 | { |
79e4f2ca | 437 | VMA_ITERATOR(vmi, mm, 0); |
1da177e4 LT |
438 | struct address_space *mapping = NULL; |
439 | ||
79e4f2ca | 440 | if (vma_iter_prealloc(&vmi)) |
d4af56c5 LH |
441 | return -ENOMEM; |
442 | ||
64ac4940 | 443 | if (vma->vm_file) { |
1da177e4 | 444 | mapping = vma->vm_file->f_mapping; |
83cde9e8 | 445 | i_mmap_lock_write(mapping); |
64ac4940 | 446 | } |
1da177e4 | 447 | |
79e4f2ca | 448 | vma_iter_store(&vmi, vma); |
1da177e4 | 449 | |
c154124f LH |
450 | if (mapping) { |
451 | __vma_link_file(vma, mapping); | |
83cde9e8 | 452 | i_mmap_unlock_write(mapping); |
c154124f | 453 | } |
1da177e4 LT |
454 | |
455 | mm->map_count++; | |
456 | validate_mm(mm); | |
d4af56c5 | 457 | return 0; |
1da177e4 LT |
458 | } |
459 | ||
68cefec5 LH |
460 | /* |
461 | * init_multi_vma_prep() - Initializer for struct vma_prepare | |
462 | * @vp: The vma_prepare struct | |
463 | * @vma: The vma that will be altered once locked | |
464 | * @next: The next vma if it is to be adjusted | |
465 | * @remove: The first vma to be removed | |
466 | * @remove2: The second vma to be removed | |
467 | */ | |
468 | static inline void init_multi_vma_prep(struct vma_prepare *vp, | |
469 | struct vm_area_struct *vma, struct vm_area_struct *next, | |
470 | struct vm_area_struct *remove, struct vm_area_struct *remove2) | |
471 | { | |
472 | memset(vp, 0, sizeof(struct vma_prepare)); | |
473 | vp->vma = vma; | |
474 | vp->anon_vma = vma->anon_vma; | |
475 | vp->remove = remove; | |
476 | vp->remove2 = remove2; | |
477 | vp->adj_next = next; | |
478 | if (!vp->anon_vma && next) | |
479 | vp->anon_vma = next->anon_vma; | |
480 | ||
481 | vp->file = vma->vm_file; | |
482 | if (vp->file) | |
483 | vp->mapping = vma->vm_file->f_mapping; | |
484 | ||
485 | } | |
486 | ||
487 | /* | |
488 | * init_vma_prep() - Initializer wrapper for vma_prepare struct | |
489 | * @vp: The vma_prepare struct | |
490 | * @vma: The vma that will be altered once locked | |
491 | */ | |
492 | static inline void init_vma_prep(struct vma_prepare *vp, | |
493 | struct vm_area_struct *vma) | |
494 | { | |
495 | init_multi_vma_prep(vp, vma, NULL, NULL, NULL); | |
496 | } | |
497 | ||
498 | ||
440703e0 LH |
499 | /* |
500 | * vma_prepare() - Helper function for handling locking VMAs prior to altering | |
501 | * @vp: The initialized vma_prepare struct | |
502 | */ | |
503 | static inline void vma_prepare(struct vma_prepare *vp) | |
504 | { | |
505 | if (vp->file) { | |
506 | uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end); | |
507 | ||
508 | if (vp->adj_next) | |
509 | uprobe_munmap(vp->adj_next, vp->adj_next->vm_start, | |
510 | vp->adj_next->vm_end); | |
511 | ||
512 | i_mmap_lock_write(vp->mapping); | |
513 | if (vp->insert && vp->insert->vm_file) { | |
514 | /* | |
515 | * Put into interval tree now, so instantiated pages | |
516 | * are visible to arm/parisc __flush_dcache_page | |
517 | * throughout; but we cannot insert into address | |
518 | * space until vma start or end is updated. | |
519 | */ | |
520 | __vma_link_file(vp->insert, | |
521 | vp->insert->vm_file->f_mapping); | |
522 | } | |
523 | } | |
524 | ||
525 | if (vp->anon_vma) { | |
526 | anon_vma_lock_write(vp->anon_vma); | |
527 | anon_vma_interval_tree_pre_update_vma(vp->vma); | |
528 | if (vp->adj_next) | |
529 | anon_vma_interval_tree_pre_update_vma(vp->adj_next); | |
530 | } | |
531 | ||
532 | if (vp->file) { | |
533 | flush_dcache_mmap_lock(vp->mapping); | |
534 | vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap); | |
535 | if (vp->adj_next) | |
536 | vma_interval_tree_remove(vp->adj_next, | |
537 | &vp->mapping->i_mmap); | |
538 | } | |
539 | ||
540 | } | |
541 | ||
542 | /* | |
543 | * vma_complete- Helper function for handling the unlocking after altering VMAs, | |
544 | * or for inserting a VMA. | |
545 | * | |
546 | * @vp: The vma_prepare struct | |
547 | * @vmi: The vma iterator | |
548 | * @mm: The mm_struct | |
549 | */ | |
550 | static inline void vma_complete(struct vma_prepare *vp, | |
551 | struct vma_iterator *vmi, struct mm_struct *mm) | |
552 | { | |
553 | if (vp->file) { | |
554 | if (vp->adj_next) | |
555 | vma_interval_tree_insert(vp->adj_next, | |
556 | &vp->mapping->i_mmap); | |
557 | vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap); | |
558 | flush_dcache_mmap_unlock(vp->mapping); | |
559 | } | |
560 | ||
561 | if (vp->remove && vp->file) { | |
562 | __remove_shared_vm_struct(vp->remove, vp->file, vp->mapping); | |
563 | if (vp->remove2) | |
564 | __remove_shared_vm_struct(vp->remove2, vp->file, | |
565 | vp->mapping); | |
566 | } else if (vp->insert) { | |
567 | /* | |
568 | * split_vma has split insert from vma, and needs | |
569 | * us to insert it before dropping the locks | |
570 | * (it may either follow vma or precede it). | |
571 | */ | |
572 | vma_iter_store(vmi, vp->insert); | |
573 | mm->map_count++; | |
574 | } | |
575 | ||
576 | if (vp->anon_vma) { | |
577 | anon_vma_interval_tree_post_update_vma(vp->vma); | |
578 | if (vp->adj_next) | |
579 | anon_vma_interval_tree_post_update_vma(vp->adj_next); | |
580 | anon_vma_unlock_write(vp->anon_vma); | |
581 | } | |
582 | ||
583 | if (vp->file) { | |
584 | i_mmap_unlock_write(vp->mapping); | |
585 | uprobe_mmap(vp->vma); | |
586 | ||
587 | if (vp->adj_next) | |
588 | uprobe_mmap(vp->adj_next); | |
589 | } | |
590 | ||
591 | if (vp->remove) { | |
592 | again: | |
593 | if (vp->file) { | |
594 | uprobe_munmap(vp->remove, vp->remove->vm_start, | |
595 | vp->remove->vm_end); | |
596 | fput(vp->file); | |
597 | } | |
598 | if (vp->remove->anon_vma) | |
599 | anon_vma_merge(vp->vma, vp->remove); | |
600 | mm->map_count--; | |
601 | mpol_put(vma_policy(vp->remove)); | |
602 | if (!vp->remove2) | |
603 | WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end); | |
604 | vm_area_free(vp->remove); | |
605 | ||
606 | /* | |
607 | * In mprotect's case 6 (see comments on vma_merge), | |
5ff783f1 | 608 | * we are removing both mid and next vmas |
440703e0 LH |
609 | */ |
610 | if (vp->remove2) { | |
611 | vp->remove = vp->remove2; | |
612 | vp->remove2 = NULL; | |
613 | goto again; | |
614 | } | |
615 | } | |
616 | if (vp->insert && vp->file) | |
617 | uprobe_mmap(vp->insert); | |
618 | } | |
619 | ||
04241ffe LH |
620 | /* |
621 | * dup_anon_vma() - Helper function to duplicate anon_vma | |
622 | * @dst: The destination VMA | |
623 | * @src: The source VMA | |
624 | * | |
625 | * Returns: 0 on success. | |
626 | */ | |
627 | static inline int dup_anon_vma(struct vm_area_struct *dst, | |
628 | struct vm_area_struct *src) | |
629 | { | |
630 | /* | |
631 | * Easily overlooked: when mprotect shifts the boundary, make sure the | |
632 | * expanding vma has anon_vma set if the shrinking vma had, to cover any | |
633 | * anon pages imported. | |
634 | */ | |
635 | if (src->anon_vma && !dst->anon_vma) { | |
636 | dst->anon_vma = src->anon_vma; | |
637 | return anon_vma_clone(dst, src); | |
638 | } | |
639 | ||
640 | return 0; | |
641 | } | |
642 | ||
9303d3e1 LH |
643 | /* |
644 | * vma_expand - Expand an existing VMA | |
645 | * | |
646 | * @vmi: The vma iterator | |
647 | * @vma: The vma to expand | |
648 | * @start: The start of the vma | |
649 | * @end: The exclusive end of the vma | |
650 | * @pgoff: The page offset of vma | |
651 | * @next: The current of next vma. | |
652 | * | |
653 | * Expand @vma to @start and @end. Can expand off the start and end. Will | |
654 | * expand over @next if it's different from @vma and @end == @next->vm_end. | |
655 | * Checking if the @vma can expand and merge with @next needs to be handled by | |
656 | * the caller. | |
657 | * | |
658 | * Returns: 0 on success | |
659 | */ | |
7c9813e8 LH |
660 | int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, |
661 | unsigned long start, unsigned long end, pgoff_t pgoff, | |
662 | struct vm_area_struct *next) | |
9303d3e1 | 663 | { |
68cefec5 | 664 | bool remove_next = false; |
9303d3e1 LH |
665 | struct vma_prepare vp; |
666 | ||
9303d3e1 | 667 | if (next && (vma != next) && (end == next->vm_end)) { |
04241ffe | 668 | int ret; |
9303d3e1 | 669 | |
04241ffe LH |
670 | remove_next = true; |
671 | ret = dup_anon_vma(vma, next); | |
672 | if (ret) | |
673 | return ret; | |
9303d3e1 LH |
674 | } |
675 | ||
68cefec5 | 676 | init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL); |
9303d3e1 LH |
677 | /* Not merging but overwriting any part of next is not handled. */ |
678 | VM_WARN_ON(next && !vp.remove && | |
679 | next != vma && end > next->vm_start); | |
680 | /* Only handles expanding */ | |
681 | VM_WARN_ON(vma->vm_start < start || vma->vm_end > end); | |
682 | ||
683 | if (vma_iter_prealloc(vmi)) | |
684 | goto nomem; | |
685 | ||
686 | vma_adjust_trans_huge(vma, start, end, 0); | |
9303d3e1 LH |
687 | /* VMA iterator points to previous, so set to start if necessary */ |
688 | if (vma_iter_addr(vmi) != start) | |
689 | vma_iter_set(vmi, start); | |
690 | ||
691 | vma_prepare(&vp); | |
692 | vma->vm_start = start; | |
693 | vma->vm_end = end; | |
694 | vma->vm_pgoff = pgoff; | |
695 | /* Note: mas must be pointing to the expanding VMA */ | |
696 | vma_iter_store(vmi, vma); | |
697 | ||
698 | vma_complete(&vp, vmi, vma->vm_mm); | |
699 | validate_mm(vma->vm_mm); | |
700 | return 0; | |
701 | ||
702 | nomem: | |
703 | return -ENOMEM; | |
704 | } | |
cf51e86d LH |
705 | |
706 | /* | |
707 | * vma_shrink() - Reduce an existing VMAs memory area | |
708 | * @vmi: The vma iterator | |
709 | * @vma: The VMA to modify | |
710 | * @start: The new start | |
711 | * @end: The new end | |
712 | * | |
713 | * Returns: 0 on success, -ENOMEM otherwise | |
714 | */ | |
715 | int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, | |
716 | unsigned long start, unsigned long end, pgoff_t pgoff) | |
717 | { | |
718 | struct vma_prepare vp; | |
719 | ||
720 | WARN_ON((vma->vm_start != start) && (vma->vm_end != end)); | |
721 | ||
722 | if (vma_iter_prealloc(vmi)) | |
723 | return -ENOMEM; | |
724 | ||
725 | init_vma_prep(&vp, vma); | |
726 | vma_adjust_trans_huge(vma, start, end, 0); | |
727 | vma_prepare(&vp); | |
728 | ||
729 | if (vma->vm_start < start) | |
730 | vma_iter_clear(vmi, vma->vm_start, start); | |
731 | ||
732 | if (vma->vm_end > end) | |
733 | vma_iter_clear(vmi, end, vma->vm_end); | |
734 | ||
735 | vma->vm_start = start; | |
736 | vma->vm_end = end; | |
737 | vma->vm_pgoff = pgoff; | |
738 | vma_complete(&vp, vmi, vma->vm_mm); | |
739 | validate_mm(vma->vm_mm); | |
740 | return 0; | |
741 | } | |
742 | ||
1da177e4 LT |
743 | /* |
744 | * If the vma has a ->close operation then the driver probably needs to release | |
745 | * per-vma resources, so we don't attempt to merge those. | |
746 | */ | |
1da177e4 | 747 | static inline int is_mergeable_vma(struct vm_area_struct *vma, |
b2b3b886 LH |
748 | struct file *file, unsigned long vm_flags, |
749 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, | |
750 | struct anon_vma_name *anon_name) | |
1da177e4 | 751 | { |
34228d47 CG |
752 | /* |
753 | * VM_SOFTDIRTY should not prevent from VMA merging, if we | |
754 | * match the flags but dirty bit -- the caller should mark | |
755 | * merged VMA as dirty. If dirty bit won't be excluded from | |
8bb4e7a2 | 756 | * comparison, we increase pressure on the memory system forcing |
34228d47 CG |
757 | * the kernel to generate new VMAs when old one could be |
758 | * extended instead. | |
759 | */ | |
760 | if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY) | |
1da177e4 LT |
761 | return 0; |
762 | if (vma->vm_file != file) | |
763 | return 0; | |
764 | if (vma->vm_ops && vma->vm_ops->close) | |
765 | return 0; | |
19a809af AA |
766 | if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx)) |
767 | return 0; | |
5c26f6ac | 768 | if (!anon_vma_name_eq(anon_vma_name(vma), anon_name)) |
9a10064f | 769 | return 0; |
1da177e4 LT |
770 | return 1; |
771 | } | |
772 | ||
773 | static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, | |
965f55de SL |
774 | struct anon_vma *anon_vma2, |
775 | struct vm_area_struct *vma) | |
1da177e4 | 776 | { |
965f55de SL |
777 | /* |
778 | * The list_is_singular() test is to avoid merging VMA cloned from | |
779 | * parents. This can improve scalability caused by anon_vma lock. | |
780 | */ | |
781 | if ((!anon_vma1 || !anon_vma2) && (!vma || | |
782 | list_is_singular(&vma->anon_vma_chain))) | |
783 | return 1; | |
784 | return anon_vma1 == anon_vma2; | |
1da177e4 LT |
785 | } |
786 | ||
787 | /* | |
788 | * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) | |
789 | * in front of (at a lower virtual address and file offset than) the vma. | |
790 | * | |
791 | * We cannot merge two vmas if they have differently assigned (non-NULL) | |
792 | * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. | |
793 | * | |
794 | * We don't check here for the merged mmap wrapping around the end of pagecache | |
45e55300 | 795 | * indices (16TB on ia32) because do_mmap() does not permit mmap's which |
1da177e4 LT |
796 | * wrap, nor mmaps which cover the final page at index -1UL. |
797 | */ | |
798 | static int | |
799 | can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, | |
19a809af AA |
800 | struct anon_vma *anon_vma, struct file *file, |
801 | pgoff_t vm_pgoff, | |
9a10064f | 802 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
5c26f6ac | 803 | struct anon_vma_name *anon_name) |
1da177e4 | 804 | { |
9a10064f | 805 | if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && |
965f55de | 806 | is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { |
1da177e4 LT |
807 | if (vma->vm_pgoff == vm_pgoff) |
808 | return 1; | |
809 | } | |
810 | return 0; | |
811 | } | |
812 | ||
813 | /* | |
814 | * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) | |
815 | * beyond (at a higher virtual address and file offset than) the vma. | |
816 | * | |
817 | * We cannot merge two vmas if they have differently assigned (non-NULL) | |
818 | * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. | |
819 | */ | |
820 | static int | |
821 | can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, | |
19a809af AA |
822 | struct anon_vma *anon_vma, struct file *file, |
823 | pgoff_t vm_pgoff, | |
9a10064f | 824 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
5c26f6ac | 825 | struct anon_vma_name *anon_name) |
1da177e4 | 826 | { |
9a10064f | 827 | if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) && |
965f55de | 828 | is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { |
1da177e4 | 829 | pgoff_t vm_pglen; |
d6e93217 | 830 | vm_pglen = vma_pages(vma); |
1da177e4 LT |
831 | if (vma->vm_pgoff + vm_pglen == vm_pgoff) |
832 | return 1; | |
833 | } | |
834 | return 0; | |
835 | } | |
836 | ||
837 | /* | |
9a10064f CC |
838 | * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name), |
839 | * figure out whether that can be merged with its predecessor or its | |
840 | * successor. Or both (it neatly fills a hole). | |
1da177e4 LT |
841 | * |
842 | * In most cases - when called for mmap, brk or mremap - [addr,end) is | |
843 | * certain not to be mapped by the time vma_merge is called; but when | |
844 | * called for mprotect, it is certain to be already mapped (either at | |
845 | * an offset within prev, or at the start of next), and the flags of | |
846 | * this area are about to be changed to vm_flags - and the no-change | |
847 | * case has already been eliminated. | |
848 | * | |
849 | * The following mprotect cases have to be considered, where AAAA is | |
850 | * the area passed down from mprotect_fixup, never extending beyond one | |
9e8a39d2 VB |
851 | * vma, PPPP is the previous vma, NNNN is a vma that starts at the same |
852 | * address as AAAA and is of the same or larger span, and XXXX the next | |
853 | * vma after AAAA: | |
1da177e4 | 854 | * |
5d42ab29 | 855 | * AAAA AAAA AAAA |
9e8a39d2 | 856 | * PPPPPPXXXXXX PPPPPPXXXXXX PPPPPPNNNNNN |
5d42ab29 | 857 | * cannot merge might become might become |
183b7a60 | 858 | * PPXXXXXXXXXX PPPPPPPPPPNN |
5d42ab29 WY |
859 | * mmap, brk or case 4 below case 5 below |
860 | * mremap move: | |
861 | * AAAA AAAA | |
097d70c6 | 862 | * PPPP XXXX PPPPNNNNXXXX |
5d42ab29 WY |
863 | * might become might become |
864 | * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or | |
097d70c6 VB |
865 | * PPPPPPPPXXXX 2 or PPPPPPPPXXXX 7 or |
866 | * PPPPXXXXXXXX 3 PPPPXXXXXXXX 8 | |
1da177e4 | 867 | * |
8bb4e7a2 | 868 | * It is important for case 8 that the vma NNNN overlapping the |
e86f15ee AA |
869 | * region AAAA is never going to extended over XXXX. Instead XXXX must |
870 | * be extended in region AAAA and NNNN must be removed. This way in | |
0503ea8f | 871 | * all cases where vma_merge succeeds, the moment vma_merge drops the |
e86f15ee AA |
872 | * rmap_locks, the properties of the merged vma will be already |
873 | * correct for the whole merged range. Some of those properties like | |
874 | * vm_page_prot/vm_flags may be accessed by rmap_walks and they must | |
875 | * be correct for the whole merged range immediately after the | |
876 | * rmap_locks are released. Otherwise if XXXX would be removed and | |
877 | * NNNN would be extended over the XXXX range, remove_migration_ptes | |
878 | * or other rmap walkers (if working on addresses beyond the "end" | |
879 | * parameter) may establish ptes with the wrong permissions of NNNN | |
880 | * instead of the right permissions of XXXX. | |
0503ea8f LH |
881 | * |
882 | * In the code below: | |
883 | * PPPP is represented by *prev | |
9e8a39d2 VB |
884 | * NNNN is represented by *mid or not represented at all (NULL) |
885 | * XXXX is represented by *next or not represented at all (NULL) | |
886 | * AAAA is not represented - it will be merged and the vma containing the | |
887 | * area is returned, or the function will return NULL | |
1da177e4 | 888 | */ |
9760ebff | 889 | struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, |
1da177e4 LT |
890 | struct vm_area_struct *prev, unsigned long addr, |
891 | unsigned long end, unsigned long vm_flags, | |
cc71aba3 | 892 | struct anon_vma *anon_vma, struct file *file, |
19a809af | 893 | pgoff_t pgoff, struct mempolicy *policy, |
9a10064f | 894 | struct vm_userfaultfd_ctx vm_userfaultfd_ctx, |
5c26f6ac | 895 | struct anon_vma_name *anon_name) |
1da177e4 LT |
896 | { |
897 | pgoff_t pglen = (end - addr) >> PAGE_SHIFT; | |
0503ea8f | 898 | pgoff_t vma_pgoff; |
9760ebff | 899 | struct vm_area_struct *mid, *next, *res = NULL; |
0503ea8f | 900 | struct vm_area_struct *vma, *adjust, *remove, *remove2; |
eef19944 JM |
901 | int err = -1; |
902 | bool merge_prev = false; | |
903 | bool merge_next = false; | |
0503ea8f LH |
904 | bool vma_expanded = false; |
905 | struct vma_prepare vp; | |
906 | unsigned long vma_end = end; | |
907 | long adj_next = 0; | |
908 | unsigned long vma_start = addr; | |
1da177e4 | 909 | |
0503ea8f | 910 | validate_mm(mm); |
1da177e4 LT |
911 | /* |
912 | * We later require that vma->vm_flags == vm_flags, | |
913 | * so this tests vma->vm_flags & VM_SPECIAL, too. | |
914 | */ | |
915 | if (vm_flags & VM_SPECIAL) | |
916 | return NULL; | |
917 | ||
5cd70b96 VB |
918 | mid = find_vma(mm, prev ? prev->vm_end : 0); |
919 | if (mid && mid->vm_end == end) /* cases 6, 7, 8 */ | |
920 | next = find_vma(mm, mid->vm_end); | |
921 | else | |
922 | next = mid; | |
1da177e4 | 923 | |
9e8a39d2 VB |
924 | /* In cases 1 - 4 there's no NNNN vma */ |
925 | if (mid && end <= mid->vm_start) | |
926 | mid = NULL; | |
927 | ||
e86f15ee AA |
928 | /* verify some invariant that must be enforced by the caller */ |
929 | VM_WARN_ON(prev && addr <= prev->vm_start); | |
eef19944 | 930 | VM_WARN_ON(mid && end > mid->vm_end); |
e86f15ee AA |
931 | VM_WARN_ON(addr >= end); |
932 | ||
0503ea8f LH |
933 | if (prev) { |
934 | res = prev; | |
935 | vma = prev; | |
936 | vma_start = prev->vm_start; | |
937 | vma_pgoff = prev->vm_pgoff; | |
938 | /* Can we merge the predecessor? */ | |
939 | if (prev->vm_end == addr && mpol_equal(vma_policy(prev), policy) | |
940 | && can_vma_merge_after(prev, vm_flags, anon_vma, file, | |
941 | pgoff, vm_userfaultfd_ctx, anon_name)) { | |
942 | merge_prev = true; | |
18b098af | 943 | vma_prev(vmi); |
0503ea8f | 944 | } |
1da177e4 | 945 | } |
eef19944 | 946 | /* Can we merge the successor? */ |
1da177e4 | 947 | if (next && end == next->vm_start && |
cc71aba3 | 948 | mpol_equal(policy, vma_policy(next)) && |
1da177e4 | 949 | can_vma_merge_before(next, vm_flags, |
19a809af | 950 | anon_vma, file, pgoff+pglen, |
9a10064f | 951 | vm_userfaultfd_ctx, anon_name)) { |
eef19944 JM |
952 | merge_next = true; |
953 | } | |
0503ea8f LH |
954 | |
955 | remove = remove2 = adjust = NULL; | |
eef19944 JM |
956 | /* Can we merge both the predecessor and the successor? */ |
957 | if (merge_prev && merge_next && | |
0503ea8f | 958 | is_mergeable_anon_vma(prev->anon_vma, next->anon_vma, NULL)) { |
5ff783f1 | 959 | remove = next; /* case 1 */ |
0503ea8f | 960 | vma_end = next->vm_end; |
5ff783f1 | 961 | err = dup_anon_vma(prev, next); |
9e8a39d2 | 962 | if (mid) { /* case 6 */ |
5ff783f1 | 963 | remove = mid; |
0503ea8f | 964 | remove2 = next; |
5ff783f1 VB |
965 | if (!next->anon_vma) |
966 | err = dup_anon_vma(prev, mid); | |
0503ea8f LH |
967 | } |
968 | } else if (merge_prev) { | |
969 | err = 0; /* case 2 */ | |
9e8a39d2 | 970 | if (mid) { |
50dac011 | 971 | err = dup_anon_vma(prev, mid); |
0503ea8f LH |
972 | if (end == mid->vm_end) { /* case 7 */ |
973 | remove = mid; | |
974 | } else { /* case 5 */ | |
975 | adjust = mid; | |
976 | adj_next = (end - mid->vm_start); | |
977 | } | |
978 | } | |
eef19944 | 979 | } else if (merge_next) { |
eef19944 | 980 | res = next; |
0503ea8f LH |
981 | if (prev && addr < prev->vm_end) { /* case 4 */ |
982 | vma_end = addr; | |
183b7a60 | 983 | adjust = next; |
50dac011 | 984 | adj_next = -(prev->vm_end - addr); |
183b7a60 | 985 | err = dup_anon_vma(next, prev); |
0503ea8f LH |
986 | } else { |
987 | vma = next; /* case 3 */ | |
988 | vma_start = addr; | |
989 | vma_end = next->vm_end; | |
097d70c6 | 990 | vma_pgoff = next->vm_pgoff; |
0503ea8f | 991 | err = 0; |
9e8a39d2 | 992 | if (mid) { /* case 8 */ |
097d70c6 | 993 | vma_pgoff = mid->vm_pgoff; |
0503ea8f | 994 | remove = mid; |
50dac011 | 995 | err = dup_anon_vma(next, mid); |
0503ea8f LH |
996 | } |
997 | } | |
1da177e4 LT |
998 | } |
999 | ||
0503ea8f | 1000 | /* Cannot merge or error in anon_vma clone */ |
eef19944 JM |
1001 | if (err) |
1002 | return NULL; | |
0503ea8f LH |
1003 | |
1004 | if (vma_iter_prealloc(vmi)) | |
1005 | return NULL; | |
1006 | ||
1007 | vma_adjust_trans_huge(vma, vma_start, vma_end, adj_next); | |
1008 | init_multi_vma_prep(&vp, vma, adjust, remove, remove2); | |
1009 | VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma && | |
1010 | vp.anon_vma != adjust->anon_vma); | |
1011 | ||
1012 | vma_prepare(&vp); | |
1013 | if (vma_start < vma->vm_start || vma_end > vma->vm_end) | |
1014 | vma_expanded = true; | |
1015 | ||
1016 | vma->vm_start = vma_start; | |
1017 | vma->vm_end = vma_end; | |
1018 | vma->vm_pgoff = vma_pgoff; | |
1019 | ||
1020 | if (vma_expanded) | |
1021 | vma_iter_store(vmi, vma); | |
1022 | ||
1023 | if (adj_next) { | |
1024 | adjust->vm_start += adj_next; | |
1025 | adjust->vm_pgoff += adj_next >> PAGE_SHIFT; | |
1026 | if (adj_next < 0) { | |
1027 | WARN_ON(vma_expanded); | |
1028 | vma_iter_store(vmi, next); | |
1029 | } | |
1030 | } | |
1031 | ||
1032 | vma_complete(&vp, vmi, mm); | |
1033 | vma_iter_free(vmi); | |
1034 | validate_mm(mm); | |
eef19944 | 1035 | khugepaged_enter_vma(res, vm_flags); |
1da177e4 | 1036 | |
9760ebff | 1037 | return res; |
f2ebfe43 LH |
1038 | } |
1039 | ||
d0e9fe17 | 1040 | /* |
b4f315b4 | 1041 | * Rough compatibility check to quickly see if it's even worth looking |
d0e9fe17 LT |
1042 | * at sharing an anon_vma. |
1043 | * | |
1044 | * They need to have the same vm_file, and the flags can only differ | |
1045 | * in things that mprotect may change. | |
1046 | * | |
1047 | * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that | |
1048 | * we can merge the two vma's. For example, we refuse to merge a vma if | |
1049 | * there is a vm_ops->close() function, because that indicates that the | |
1050 | * driver is doing some kind of reference counting. But that doesn't | |
1051 | * really matter for the anon_vma sharing case. | |
1052 | */ | |
1053 | static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) | |
1054 | { | |
1055 | return a->vm_end == b->vm_start && | |
1056 | mpol_equal(vma_policy(a), vma_policy(b)) && | |
1057 | a->vm_file == b->vm_file && | |
6cb4d9a2 | 1058 | !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) && |
d0e9fe17 LT |
1059 | b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * Do some basic sanity checking to see if we can re-use the anon_vma | |
1064 | * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be | |
1065 | * the same as 'old', the other will be the new one that is trying | |
1066 | * to share the anon_vma. | |
1067 | * | |
5b449489 | 1068 | * NOTE! This runs with mmap_lock held for reading, so it is possible that |
d0e9fe17 LT |
1069 | * the anon_vma of 'old' is concurrently in the process of being set up |
1070 | * by another page fault trying to merge _that_. But that's ok: if it | |
1071 | * is being set up, that automatically means that it will be a singleton | |
1072 | * acceptable for merging, so we can do all of this optimistically. But | |
4db0c3c2 | 1073 | * we do that READ_ONCE() to make sure that we never re-load the pointer. |
d0e9fe17 LT |
1074 | * |
1075 | * IOW: that the "list_is_singular()" test on the anon_vma_chain only | |
1076 | * matters for the 'stable anon_vma' case (ie the thing we want to avoid | |
1077 | * is to return an anon_vma that is "complex" due to having gone through | |
1078 | * a fork). | |
1079 | * | |
1080 | * We also make sure that the two vma's are compatible (adjacent, | |
1081 | * and with the same memory policies). That's all stable, even with just | |
5b449489 | 1082 | * a read lock on the mmap_lock. |
d0e9fe17 LT |
1083 | */ |
1084 | static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) | |
1085 | { | |
1086 | if (anon_vma_compatible(a, b)) { | |
4db0c3c2 | 1087 | struct anon_vma *anon_vma = READ_ONCE(old->anon_vma); |
d0e9fe17 LT |
1088 | |
1089 | if (anon_vma && list_is_singular(&old->anon_vma_chain)) | |
1090 | return anon_vma; | |
1091 | } | |
1092 | return NULL; | |
1093 | } | |
1094 | ||
1da177e4 LT |
1095 | /* |
1096 | * find_mergeable_anon_vma is used by anon_vma_prepare, to check | |
1097 | * neighbouring vmas for a suitable anon_vma, before it goes off | |
1098 | * to allocate a new anon_vma. It checks because a repetitive | |
1099 | * sequence of mprotects and faults may otherwise lead to distinct | |
1100 | * anon_vmas being allocated, preventing vma merge in subsequent | |
1101 | * mprotect. | |
1102 | */ | |
1103 | struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) | |
1104 | { | |
763ecb03 | 1105 | MA_STATE(mas, &vma->vm_mm->mm_mt, vma->vm_end, vma->vm_end); |
a67c8caa | 1106 | struct anon_vma *anon_vma = NULL; |
763ecb03 | 1107 | struct vm_area_struct *prev, *next; |
a67c8caa ML |
1108 | |
1109 | /* Try next first. */ | |
763ecb03 LH |
1110 | next = mas_walk(&mas); |
1111 | if (next) { | |
1112 | anon_vma = reusable_anon_vma(next, vma, next); | |
a67c8caa ML |
1113 | if (anon_vma) |
1114 | return anon_vma; | |
1115 | } | |
1116 | ||
763ecb03 LH |
1117 | prev = mas_prev(&mas, 0); |
1118 | VM_BUG_ON_VMA(prev != vma, vma); | |
1119 | prev = mas_prev(&mas, 0); | |
a67c8caa | 1120 | /* Try prev next. */ |
763ecb03 LH |
1121 | if (prev) |
1122 | anon_vma = reusable_anon_vma(prev, prev, vma); | |
a67c8caa | 1123 | |
1da177e4 | 1124 | /* |
a67c8caa ML |
1125 | * We might reach here with anon_vma == NULL if we can't find |
1126 | * any reusable anon_vma. | |
1da177e4 LT |
1127 | * There's no absolute need to look only at touching neighbours: |
1128 | * we could search further afield for "compatible" anon_vmas. | |
1129 | * But it would probably just be a waste of time searching, | |
1130 | * or lead to too many vmas hanging off the same anon_vma. | |
1131 | * We're trying to allow mprotect remerging later on, | |
1132 | * not trying to minimize memory used for anon_vmas. | |
1133 | */ | |
a67c8caa | 1134 | return anon_vma; |
1da177e4 LT |
1135 | } |
1136 | ||
40401530 AV |
1137 | /* |
1138 | * If a hint addr is less than mmap_min_addr change hint to be as | |
1139 | * low as possible but still greater than mmap_min_addr | |
1140 | */ | |
1141 | static inline unsigned long round_hint_to_min(unsigned long hint) | |
1142 | { | |
1143 | hint &= PAGE_MASK; | |
1144 | if (((void *)hint != NULL) && | |
1145 | (hint < mmap_min_addr)) | |
1146 | return PAGE_ALIGN(mmap_min_addr); | |
1147 | return hint; | |
1148 | } | |
1149 | ||
6aeb2542 MR |
1150 | int mlock_future_check(struct mm_struct *mm, unsigned long flags, |
1151 | unsigned long len) | |
363ee17f DB |
1152 | { |
1153 | unsigned long locked, lock_limit; | |
1154 | ||
1155 | /* mlock MCL_FUTURE? */ | |
1156 | if (flags & VM_LOCKED) { | |
1157 | locked = len >> PAGE_SHIFT; | |
1158 | locked += mm->locked_vm; | |
1159 | lock_limit = rlimit(RLIMIT_MEMLOCK); | |
1160 | lock_limit >>= PAGE_SHIFT; | |
1161 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) | |
1162 | return -EAGAIN; | |
1163 | } | |
1164 | return 0; | |
1165 | } | |
1166 | ||
be83bbf8 LT |
1167 | static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) |
1168 | { | |
1169 | if (S_ISREG(inode->i_mode)) | |
423913ad | 1170 | return MAX_LFS_FILESIZE; |
be83bbf8 LT |
1171 | |
1172 | if (S_ISBLK(inode->i_mode)) | |
1173 | return MAX_LFS_FILESIZE; | |
1174 | ||
76f34950 IK |
1175 | if (S_ISSOCK(inode->i_mode)) |
1176 | return MAX_LFS_FILESIZE; | |
1177 | ||
be83bbf8 LT |
1178 | /* Special "we do even unsigned file positions" case */ |
1179 | if (file->f_mode & FMODE_UNSIGNED_OFFSET) | |
1180 | return 0; | |
1181 | ||
1182 | /* Yes, random drivers might want more. But I'm tired of buggy drivers */ | |
1183 | return ULONG_MAX; | |
1184 | } | |
1185 | ||
1186 | static inline bool file_mmap_ok(struct file *file, struct inode *inode, | |
1187 | unsigned long pgoff, unsigned long len) | |
1188 | { | |
1189 | u64 maxsize = file_mmap_size_max(file, inode); | |
1190 | ||
1191 | if (maxsize && len > maxsize) | |
1192 | return false; | |
1193 | maxsize -= len; | |
1194 | if (pgoff > maxsize >> PAGE_SHIFT) | |
1195 | return false; | |
1196 | return true; | |
1197 | } | |
1198 | ||
1da177e4 | 1199 | /* |
3e4e28c5 | 1200 | * The caller must write-lock current->mm->mmap_lock. |
1da177e4 | 1201 | */ |
1fcfd8db | 1202 | unsigned long do_mmap(struct file *file, unsigned long addr, |
1da177e4 | 1203 | unsigned long len, unsigned long prot, |
45e55300 PC |
1204 | unsigned long flags, unsigned long pgoff, |
1205 | unsigned long *populate, struct list_head *uf) | |
1da177e4 | 1206 | { |
cc71aba3 | 1207 | struct mm_struct *mm = current->mm; |
45e55300 | 1208 | vm_flags_t vm_flags; |
62b5f7d0 | 1209 | int pkey = 0; |
1da177e4 | 1210 | |
524e00b3 | 1211 | validate_mm(mm); |
41badc15 | 1212 | *populate = 0; |
bebeb3d6 | 1213 | |
e37609bb PK |
1214 | if (!len) |
1215 | return -EINVAL; | |
1216 | ||
1da177e4 LT |
1217 | /* |
1218 | * Does the application expect PROT_READ to imply PROT_EXEC? | |
1219 | * | |
1220 | * (the exception is when the underlying filesystem is noexec | |
1221 | * mounted, in which case we dont add PROT_EXEC.) | |
1222 | */ | |
1223 | if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) | |
90f8572b | 1224 | if (!(file && path_noexec(&file->f_path))) |
1da177e4 LT |
1225 | prot |= PROT_EXEC; |
1226 | ||
a4ff8e86 MH |
1227 | /* force arch specific MAP_FIXED handling in get_unmapped_area */ |
1228 | if (flags & MAP_FIXED_NOREPLACE) | |
1229 | flags |= MAP_FIXED; | |
1230 | ||
7cd94146 EP |
1231 | if (!(flags & MAP_FIXED)) |
1232 | addr = round_hint_to_min(addr); | |
1233 | ||
1da177e4 LT |
1234 | /* Careful about overflows.. */ |
1235 | len = PAGE_ALIGN(len); | |
9206de95 | 1236 | if (!len) |
1da177e4 LT |
1237 | return -ENOMEM; |
1238 | ||
1239 | /* offset overflow? */ | |
1240 | if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) | |
cc71aba3 | 1241 | return -EOVERFLOW; |
1da177e4 LT |
1242 | |
1243 | /* Too many mappings? */ | |
1244 | if (mm->map_count > sysctl_max_map_count) | |
1245 | return -ENOMEM; | |
1246 | ||
1247 | /* Obtain the address to map to. we verify (or select) it and ensure | |
1248 | * that it represents a valid section of the address space. | |
1249 | */ | |
1250 | addr = get_unmapped_area(file, addr, len, pgoff, flags); | |
ff68dac6 | 1251 | if (IS_ERR_VALUE(addr)) |
1da177e4 LT |
1252 | return addr; |
1253 | ||
a4ff8e86 | 1254 | if (flags & MAP_FIXED_NOREPLACE) { |
35e43c5f | 1255 | if (find_vma_intersection(mm, addr, addr + len)) |
a4ff8e86 MH |
1256 | return -EEXIST; |
1257 | } | |
1258 | ||
62b5f7d0 DH |
1259 | if (prot == PROT_EXEC) { |
1260 | pkey = execute_only_pkey(mm); | |
1261 | if (pkey < 0) | |
1262 | pkey = 0; | |
1263 | } | |
1264 | ||
1da177e4 LT |
1265 | /* Do simple checking here so the lower-level routines won't have |
1266 | * to. we assume access permissions have been handled by the open | |
1267 | * of the memory object, so we don't do any here. | |
1268 | */ | |
45e55300 | 1269 | vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | |
1da177e4 LT |
1270 | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
1271 | ||
cdf7b341 | 1272 | if (flags & MAP_LOCKED) |
1da177e4 LT |
1273 | if (!can_do_mlock()) |
1274 | return -EPERM; | |
ba470de4 | 1275 | |
363ee17f DB |
1276 | if (mlock_future_check(mm, vm_flags, len)) |
1277 | return -EAGAIN; | |
1da177e4 | 1278 | |
1da177e4 | 1279 | if (file) { |
077bf22b | 1280 | struct inode *inode = file_inode(file); |
1c972597 DW |
1281 | unsigned long flags_mask; |
1282 | ||
be83bbf8 LT |
1283 | if (!file_mmap_ok(file, inode, pgoff, len)) |
1284 | return -EOVERFLOW; | |
1285 | ||
1c972597 | 1286 | flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags; |
077bf22b | 1287 | |
1da177e4 LT |
1288 | switch (flags & MAP_TYPE) { |
1289 | case MAP_SHARED: | |
1c972597 DW |
1290 | /* |
1291 | * Force use of MAP_SHARED_VALIDATE with non-legacy | |
1292 | * flags. E.g. MAP_SYNC is dangerous to use with | |
1293 | * MAP_SHARED as you don't know which consistency model | |
1294 | * you will get. We silently ignore unsupported flags | |
1295 | * with MAP_SHARED to preserve backward compatibility. | |
1296 | */ | |
1297 | flags &= LEGACY_MAP_MASK; | |
e4a9bc58 | 1298 | fallthrough; |
1c972597 DW |
1299 | case MAP_SHARED_VALIDATE: |
1300 | if (flags & ~flags_mask) | |
1301 | return -EOPNOTSUPP; | |
dc617f29 DW |
1302 | if (prot & PROT_WRITE) { |
1303 | if (!(file->f_mode & FMODE_WRITE)) | |
1304 | return -EACCES; | |
1305 | if (IS_SWAPFILE(file->f_mapping->host)) | |
1306 | return -ETXTBSY; | |
1307 | } | |
1da177e4 LT |
1308 | |
1309 | /* | |
1310 | * Make sure we don't allow writing to an append-only | |
1311 | * file.. | |
1312 | */ | |
1313 | if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) | |
1314 | return -EACCES; | |
1315 | ||
1da177e4 LT |
1316 | vm_flags |= VM_SHARED | VM_MAYSHARE; |
1317 | if (!(file->f_mode & FMODE_WRITE)) | |
1318 | vm_flags &= ~(VM_MAYWRITE | VM_SHARED); | |
e4a9bc58 | 1319 | fallthrough; |
1da177e4 LT |
1320 | case MAP_PRIVATE: |
1321 | if (!(file->f_mode & FMODE_READ)) | |
1322 | return -EACCES; | |
90f8572b | 1323 | if (path_noexec(&file->f_path)) { |
80c5606c LT |
1324 | if (vm_flags & VM_EXEC) |
1325 | return -EPERM; | |
1326 | vm_flags &= ~VM_MAYEXEC; | |
1327 | } | |
80c5606c | 1328 | |
72c2d531 | 1329 | if (!file->f_op->mmap) |
80c5606c | 1330 | return -ENODEV; |
b2c56e4f ON |
1331 | if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) |
1332 | return -EINVAL; | |
1da177e4 LT |
1333 | break; |
1334 | ||
1335 | default: | |
1336 | return -EINVAL; | |
1337 | } | |
1338 | } else { | |
1339 | switch (flags & MAP_TYPE) { | |
1340 | case MAP_SHARED: | |
b2c56e4f ON |
1341 | if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) |
1342 | return -EINVAL; | |
ce363942 TH |
1343 | /* |
1344 | * Ignore pgoff. | |
1345 | */ | |
1346 | pgoff = 0; | |
1da177e4 LT |
1347 | vm_flags |= VM_SHARED | VM_MAYSHARE; |
1348 | break; | |
1349 | case MAP_PRIVATE: | |
1350 | /* | |
1351 | * Set pgoff according to addr for anon_vma. | |
1352 | */ | |
1353 | pgoff = addr >> PAGE_SHIFT; | |
1354 | break; | |
1355 | default: | |
1356 | return -EINVAL; | |
1357 | } | |
1358 | } | |
1359 | ||
c22c0d63 ML |
1360 | /* |
1361 | * Set 'VM_NORESERVE' if we should not account for the | |
1362 | * memory use of this mapping. | |
1363 | */ | |
1364 | if (flags & MAP_NORESERVE) { | |
1365 | /* We honor MAP_NORESERVE if allowed to overcommit */ | |
1366 | if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) | |
1367 | vm_flags |= VM_NORESERVE; | |
1368 | ||
1369 | /* hugetlb applies strict overcommit unless MAP_NORESERVE */ | |
1370 | if (file && is_file_hugepages(file)) | |
1371 | vm_flags |= VM_NORESERVE; | |
1372 | } | |
1373 | ||
897ab3e0 | 1374 | addr = mmap_region(file, addr, len, vm_flags, pgoff, uf); |
09a9f1d2 ML |
1375 | if (!IS_ERR_VALUE(addr) && |
1376 | ((vm_flags & VM_LOCKED) || | |
1377 | (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) | |
41badc15 | 1378 | *populate = len; |
bebeb3d6 | 1379 | return addr; |
0165ab44 | 1380 | } |
6be5ceb0 | 1381 | |
a90f590a DB |
1382 | unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len, |
1383 | unsigned long prot, unsigned long flags, | |
1384 | unsigned long fd, unsigned long pgoff) | |
66f0dc48 HD |
1385 | { |
1386 | struct file *file = NULL; | |
1e3ee14b | 1387 | unsigned long retval; |
66f0dc48 HD |
1388 | |
1389 | if (!(flags & MAP_ANONYMOUS)) { | |
120a795d | 1390 | audit_mmap_fd(fd, flags); |
66f0dc48 HD |
1391 | file = fget(fd); |
1392 | if (!file) | |
1e3ee14b | 1393 | return -EBADF; |
7bba8f0e | 1394 | if (is_file_hugepages(file)) { |
af73e4d9 | 1395 | len = ALIGN(len, huge_page_size(hstate_file(file))); |
7bba8f0e ZL |
1396 | } else if (unlikely(flags & MAP_HUGETLB)) { |
1397 | retval = -EINVAL; | |
493af578 | 1398 | goto out_fput; |
7bba8f0e | 1399 | } |
66f0dc48 | 1400 | } else if (flags & MAP_HUGETLB) { |
c103a4dc | 1401 | struct hstate *hs; |
af73e4d9 | 1402 | |
20ac2893 | 1403 | hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); |
091d0d55 LZ |
1404 | if (!hs) |
1405 | return -EINVAL; | |
1406 | ||
1407 | len = ALIGN(len, huge_page_size(hs)); | |
66f0dc48 HD |
1408 | /* |
1409 | * VM_NORESERVE is used because the reservations will be | |
1410 | * taken when vm_ops->mmap() is called | |
66f0dc48 | 1411 | */ |
af73e4d9 | 1412 | file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, |
42d7395f | 1413 | VM_NORESERVE, |
83c1fd76 | 1414 | HUGETLB_ANONHUGE_INODE, |
42d7395f | 1415 | (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); |
66f0dc48 HD |
1416 | if (IS_ERR(file)) |
1417 | return PTR_ERR(file); | |
1418 | } | |
1419 | ||
9fbeb5ab | 1420 | retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
493af578 | 1421 | out_fput: |
66f0dc48 HD |
1422 | if (file) |
1423 | fput(file); | |
66f0dc48 HD |
1424 | return retval; |
1425 | } | |
1426 | ||
a90f590a DB |
1427 | SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, |
1428 | unsigned long, prot, unsigned long, flags, | |
1429 | unsigned long, fd, unsigned long, pgoff) | |
1430 | { | |
1431 | return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff); | |
1432 | } | |
1433 | ||
a4679373 CH |
1434 | #ifdef __ARCH_WANT_SYS_OLD_MMAP |
1435 | struct mmap_arg_struct { | |
1436 | unsigned long addr; | |
1437 | unsigned long len; | |
1438 | unsigned long prot; | |
1439 | unsigned long flags; | |
1440 | unsigned long fd; | |
1441 | unsigned long offset; | |
1442 | }; | |
1443 | ||
1444 | SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) | |
1445 | { | |
1446 | struct mmap_arg_struct a; | |
1447 | ||
1448 | if (copy_from_user(&a, arg, sizeof(a))) | |
1449 | return -EFAULT; | |
de1741a1 | 1450 | if (offset_in_page(a.offset)) |
a4679373 CH |
1451 | return -EINVAL; |
1452 | ||
a90f590a DB |
1453 | return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, |
1454 | a.offset >> PAGE_SHIFT); | |
a4679373 CH |
1455 | } |
1456 | #endif /* __ARCH_WANT_SYS_OLD_MMAP */ | |
1457 | ||
4e950f6f | 1458 | /* |
8bb4e7a2 | 1459 | * Some shared mappings will want the pages marked read-only |
4e950f6f AD |
1460 | * to track write events. If so, we'll downgrade vm_page_prot |
1461 | * to the private version (using protection_map[] without the | |
1462 | * VM_SHARED bit). | |
1463 | */ | |
6d2329f8 | 1464 | int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) |
4e950f6f | 1465 | { |
ca16d140 | 1466 | vm_flags_t vm_flags = vma->vm_flags; |
8a04446a | 1467 | const struct vm_operations_struct *vm_ops = vma->vm_ops; |
4e950f6f AD |
1468 | |
1469 | /* If it was private or non-writable, the write bit is already clear */ | |
1470 | if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) | |
1471 | return 0; | |
1472 | ||
1473 | /* The backer wishes to know when pages are first written to? */ | |
8a04446a | 1474 | if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) |
4e950f6f AD |
1475 | return 1; |
1476 | ||
64e45507 PF |
1477 | /* The open routine did something to the protections that pgprot_modify |
1478 | * won't preserve? */ | |
6d2329f8 AA |
1479 | if (pgprot_val(vm_page_prot) != |
1480 | pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) | |
4e950f6f AD |
1481 | return 0; |
1482 | ||
f96f7a40 DH |
1483 | /* |
1484 | * Do we need to track softdirty? hugetlb does not support softdirty | |
1485 | * tracking yet. | |
1486 | */ | |
1487 | if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma)) | |
64e45507 PF |
1488 | return 1; |
1489 | ||
51d3d5eb DH |
1490 | /* Do we need write faults for uffd-wp tracking? */ |
1491 | if (userfaultfd_wp(vma)) | |
1492 | return 1; | |
1493 | ||
4e950f6f | 1494 | /* Specialty mapping? */ |
4b6e1e37 | 1495 | if (vm_flags & VM_PFNMAP) |
4e950f6f AD |
1496 | return 0; |
1497 | ||
1498 | /* Can the mapping track the dirty pages? */ | |
1499 | return vma->vm_file && vma->vm_file->f_mapping && | |
f56753ac | 1500 | mapping_can_writeback(vma->vm_file->f_mapping); |
4e950f6f AD |
1501 | } |
1502 | ||
fc8744ad LT |
1503 | /* |
1504 | * We account for memory if it's a private writeable mapping, | |
5a6fe125 | 1505 | * not hugepages and VM_NORESERVE wasn't set. |
fc8744ad | 1506 | */ |
ca16d140 | 1507 | static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) |
fc8744ad | 1508 | { |
5a6fe125 MG |
1509 | /* |
1510 | * hugetlb has its own accounting separate from the core VM | |
1511 | * VM_HUGETLB may not be set yet so we cannot check for that flag. | |
1512 | */ | |
1513 | if (file && is_file_hugepages(file)) | |
1514 | return 0; | |
1515 | ||
fc8744ad LT |
1516 | return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; |
1517 | } | |
1518 | ||
3499a131 LH |
1519 | /** |
1520 | * unmapped_area() - Find an area between the low_limit and the high_limit with | |
1521 | * the correct alignment and offset, all from @info. Note: current->mm is used | |
1522 | * for the search. | |
1523 | * | |
82b24936 VY |
1524 | * @info: The unmapped area information including the range [low_limit - |
1525 | * high_limit), the alignment offset and mask. | |
3499a131 LH |
1526 | * |
1527 | * Return: A memory address or -ENOMEM. | |
1528 | */ | |
baceaf1c | 1529 | static unsigned long unmapped_area(struct vm_unmapped_area_info *info) |
db4fbfb9 | 1530 | { |
3499a131 | 1531 | unsigned long length, gap; |
db4fbfb9 | 1532 | |
3499a131 | 1533 | MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); |
db4fbfb9 ML |
1534 | |
1535 | /* Adjust search length to account for worst case alignment overhead */ | |
1536 | length = info->length + info->align_mask; | |
1537 | if (length < info->length) | |
1538 | return -ENOMEM; | |
1539 | ||
3499a131 LH |
1540 | if (mas_empty_area(&mas, info->low_limit, info->high_limit - 1, |
1541 | length)) | |
db4fbfb9 ML |
1542 | return -ENOMEM; |
1543 | ||
3499a131 LH |
1544 | gap = mas.index; |
1545 | gap += (info->align_offset - gap) & info->align_mask; | |
1546 | return gap; | |
db4fbfb9 ML |
1547 | } |
1548 | ||
3499a131 LH |
1549 | /** |
1550 | * unmapped_area_topdown() - Find an area between the low_limit and the | |
82b24936 | 1551 | * high_limit with the correct alignment and offset at the highest available |
3499a131 LH |
1552 | * address, all from @info. Note: current->mm is used for the search. |
1553 | * | |
82b24936 VY |
1554 | * @info: The unmapped area information including the range [low_limit - |
1555 | * high_limit), the alignment offset and mask. | |
3499a131 LH |
1556 | * |
1557 | * Return: A memory address or -ENOMEM. | |
1558 | */ | |
baceaf1c | 1559 | static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) |
db4fbfb9 | 1560 | { |
3499a131 | 1561 | unsigned long length, gap; |
db4fbfb9 | 1562 | |
3499a131 | 1563 | MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); |
db4fbfb9 ML |
1564 | /* Adjust search length to account for worst case alignment overhead */ |
1565 | length = info->length + info->align_mask; | |
1566 | if (length < info->length) | |
1567 | return -ENOMEM; | |
1568 | ||
3499a131 LH |
1569 | if (mas_empty_area_rev(&mas, info->low_limit, info->high_limit - 1, |
1570 | length)) | |
db4fbfb9 | 1571 | return -ENOMEM; |
db4fbfb9 | 1572 | |
3499a131 LH |
1573 | gap = mas.last + 1 - info->length; |
1574 | gap -= (gap - info->align_offset) & info->align_mask; | |
1575 | return gap; | |
db4fbfb9 ML |
1576 | } |
1577 | ||
baceaf1c JK |
1578 | /* |
1579 | * Search for an unmapped address range. | |
1580 | * | |
1581 | * We are looking for a range that: | |
1582 | * - does not intersect with any VMA; | |
1583 | * - is contained within the [low_limit, high_limit) interval; | |
1584 | * - is at least the desired size. | |
1585 | * - satisfies (begin_addr & align_mask) == (align_offset & align_mask) | |
1586 | */ | |
1587 | unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info) | |
1588 | { | |
df529cab JK |
1589 | unsigned long addr; |
1590 | ||
baceaf1c | 1591 | if (info->flags & VM_UNMAPPED_AREA_TOPDOWN) |
df529cab | 1592 | addr = unmapped_area_topdown(info); |
baceaf1c | 1593 | else |
df529cab JK |
1594 | addr = unmapped_area(info); |
1595 | ||
1596 | trace_vm_unmapped_area(addr, info); | |
1597 | return addr; | |
baceaf1c | 1598 | } |
f6795053 | 1599 | |
1da177e4 LT |
1600 | /* Get an address range which is currently unmapped. |
1601 | * For shmat() with addr=0. | |
1602 | * | |
1603 | * Ugly calling convention alert: | |
1604 | * Return value with the low bits set means error value, | |
1605 | * ie | |
1606 | * if (ret & ~PAGE_MASK) | |
1607 | * error = ret; | |
1608 | * | |
1609 | * This function "knows" that -ENOMEM has the bits set. | |
1610 | */ | |
1da177e4 | 1611 | unsigned long |
4b439e25 CL |
1612 | generic_get_unmapped_area(struct file *filp, unsigned long addr, |
1613 | unsigned long len, unsigned long pgoff, | |
1614 | unsigned long flags) | |
1da177e4 LT |
1615 | { |
1616 | struct mm_struct *mm = current->mm; | |
1be7107f | 1617 | struct vm_area_struct *vma, *prev; |
db4fbfb9 | 1618 | struct vm_unmapped_area_info info; |
2cb4de08 | 1619 | const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); |
1da177e4 | 1620 | |
f6795053 | 1621 | if (len > mmap_end - mmap_min_addr) |
1da177e4 LT |
1622 | return -ENOMEM; |
1623 | ||
06abdfb4 BH |
1624 | if (flags & MAP_FIXED) |
1625 | return addr; | |
1626 | ||
1da177e4 LT |
1627 | if (addr) { |
1628 | addr = PAGE_ALIGN(addr); | |
1be7107f | 1629 | vma = find_vma_prev(mm, addr, &prev); |
f6795053 | 1630 | if (mmap_end - len >= addr && addr >= mmap_min_addr && |
1be7107f HD |
1631 | (!vma || addr + len <= vm_start_gap(vma)) && |
1632 | (!prev || addr >= vm_end_gap(prev))) | |
1da177e4 LT |
1633 | return addr; |
1634 | } | |
1da177e4 | 1635 | |
db4fbfb9 ML |
1636 | info.flags = 0; |
1637 | info.length = len; | |
4e99b021 | 1638 | info.low_limit = mm->mmap_base; |
f6795053 | 1639 | info.high_limit = mmap_end; |
db4fbfb9 | 1640 | info.align_mask = 0; |
09ef5283 | 1641 | info.align_offset = 0; |
db4fbfb9 | 1642 | return vm_unmapped_area(&info); |
1da177e4 | 1643 | } |
4b439e25 CL |
1644 | |
1645 | #ifndef HAVE_ARCH_UNMAPPED_AREA | |
1646 | unsigned long | |
1647 | arch_get_unmapped_area(struct file *filp, unsigned long addr, | |
1648 | unsigned long len, unsigned long pgoff, | |
1649 | unsigned long flags) | |
1650 | { | |
1651 | return generic_get_unmapped_area(filp, addr, len, pgoff, flags); | |
1652 | } | |
cc71aba3 | 1653 | #endif |
1da177e4 | 1654 | |
1da177e4 LT |
1655 | /* |
1656 | * This mmap-allocator allocates new areas top-down from below the | |
1657 | * stack's low limit (the base): | |
1658 | */ | |
1da177e4 | 1659 | unsigned long |
4b439e25 CL |
1660 | generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, |
1661 | unsigned long len, unsigned long pgoff, | |
1662 | unsigned long flags) | |
1da177e4 | 1663 | { |
1be7107f | 1664 | struct vm_area_struct *vma, *prev; |
1da177e4 | 1665 | struct mm_struct *mm = current->mm; |
db4fbfb9 | 1666 | struct vm_unmapped_area_info info; |
2cb4de08 | 1667 | const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); |
1da177e4 LT |
1668 | |
1669 | /* requested length too big for entire address space */ | |
f6795053 | 1670 | if (len > mmap_end - mmap_min_addr) |
1da177e4 LT |
1671 | return -ENOMEM; |
1672 | ||
06abdfb4 BH |
1673 | if (flags & MAP_FIXED) |
1674 | return addr; | |
1675 | ||
1da177e4 LT |
1676 | /* requesting a specific address */ |
1677 | if (addr) { | |
1678 | addr = PAGE_ALIGN(addr); | |
1be7107f | 1679 | vma = find_vma_prev(mm, addr, &prev); |
f6795053 | 1680 | if (mmap_end - len >= addr && addr >= mmap_min_addr && |
1be7107f HD |
1681 | (!vma || addr + len <= vm_start_gap(vma)) && |
1682 | (!prev || addr >= vm_end_gap(prev))) | |
1da177e4 LT |
1683 | return addr; |
1684 | } | |
1685 | ||
db4fbfb9 ML |
1686 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
1687 | info.length = len; | |
2afc745f | 1688 | info.low_limit = max(PAGE_SIZE, mmap_min_addr); |
f6795053 | 1689 | info.high_limit = arch_get_mmap_base(addr, mm->mmap_base); |
db4fbfb9 | 1690 | info.align_mask = 0; |
09ef5283 | 1691 | info.align_offset = 0; |
db4fbfb9 | 1692 | addr = vm_unmapped_area(&info); |
b716ad95 | 1693 | |
1da177e4 LT |
1694 | /* |
1695 | * A failed mmap() very likely causes application failure, | |
1696 | * so fall back to the bottom-up function here. This scenario | |
1697 | * can happen with large stack limits and large mmap() | |
1698 | * allocations. | |
1699 | */ | |
de1741a1 | 1700 | if (offset_in_page(addr)) { |
db4fbfb9 ML |
1701 | VM_BUG_ON(addr != -ENOMEM); |
1702 | info.flags = 0; | |
1703 | info.low_limit = TASK_UNMAPPED_BASE; | |
f6795053 | 1704 | info.high_limit = mmap_end; |
db4fbfb9 ML |
1705 | addr = vm_unmapped_area(&info); |
1706 | } | |
1da177e4 LT |
1707 | |
1708 | return addr; | |
1709 | } | |
4b439e25 CL |
1710 | |
1711 | #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN | |
1712 | unsigned long | |
1713 | arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, | |
1714 | unsigned long len, unsigned long pgoff, | |
1715 | unsigned long flags) | |
1716 | { | |
1717 | return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); | |
1718 | } | |
1da177e4 LT |
1719 | #endif |
1720 | ||
1da177e4 LT |
1721 | unsigned long |
1722 | get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, | |
1723 | unsigned long pgoff, unsigned long flags) | |
1724 | { | |
06abdfb4 BH |
1725 | unsigned long (*get_area)(struct file *, unsigned long, |
1726 | unsigned long, unsigned long, unsigned long); | |
1727 | ||
9206de95 AV |
1728 | unsigned long error = arch_mmap_check(addr, len, flags); |
1729 | if (error) | |
1730 | return error; | |
1731 | ||
1732 | /* Careful about overflows.. */ | |
1733 | if (len > TASK_SIZE) | |
1734 | return -ENOMEM; | |
1735 | ||
06abdfb4 | 1736 | get_area = current->mm->get_unmapped_area; |
c01d5b30 HD |
1737 | if (file) { |
1738 | if (file->f_op->get_unmapped_area) | |
1739 | get_area = file->f_op->get_unmapped_area; | |
1740 | } else if (flags & MAP_SHARED) { | |
1741 | /* | |
1742 | * mmap_region() will call shmem_zero_setup() to create a file, | |
1743 | * so use shmem's get_unmapped_area in case it can be huge. | |
45e55300 | 1744 | * do_mmap() will clear pgoff, so match alignment. |
c01d5b30 HD |
1745 | */ |
1746 | pgoff = 0; | |
1747 | get_area = shmem_get_unmapped_area; | |
1748 | } | |
1749 | ||
06abdfb4 BH |
1750 | addr = get_area(file, addr, len, pgoff, flags); |
1751 | if (IS_ERR_VALUE(addr)) | |
1752 | return addr; | |
1da177e4 | 1753 | |
07ab67c8 LT |
1754 | if (addr > TASK_SIZE - len) |
1755 | return -ENOMEM; | |
de1741a1 | 1756 | if (offset_in_page(addr)) |
07ab67c8 | 1757 | return -EINVAL; |
06abdfb4 | 1758 | |
9ac4ed4b AV |
1759 | error = security_mmap_addr(addr); |
1760 | return error ? error : addr; | |
1da177e4 LT |
1761 | } |
1762 | ||
1763 | EXPORT_SYMBOL(get_unmapped_area); | |
1764 | ||
abdba2dd LH |
1765 | /** |
1766 | * find_vma_intersection() - Look up the first VMA which intersects the interval | |
1767 | * @mm: The process address space. | |
1768 | * @start_addr: The inclusive start user address. | |
1769 | * @end_addr: The exclusive end user address. | |
1770 | * | |
1771 | * Returns: The first VMA within the provided range, %NULL otherwise. Assumes | |
1772 | * start_addr < end_addr. | |
1773 | */ | |
1774 | struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, | |
1775 | unsigned long start_addr, | |
1776 | unsigned long end_addr) | |
1777 | { | |
abdba2dd LH |
1778 | unsigned long index = start_addr; |
1779 | ||
1780 | mmap_assert_locked(mm); | |
7964cf8c | 1781 | return mt_find(&mm->mm_mt, &index, end_addr - 1); |
abdba2dd LH |
1782 | } |
1783 | EXPORT_SYMBOL(find_vma_intersection); | |
1784 | ||
be8432e7 LH |
1785 | /** |
1786 | * find_vma() - Find the VMA for a given address, or the next VMA. | |
1787 | * @mm: The mm_struct to check | |
1788 | * @addr: The address | |
1789 | * | |
1790 | * Returns: The VMA associated with addr, or the next VMA. | |
1791 | * May return %NULL in the case of no VMA at addr or above. | |
1792 | */ | |
48aae425 | 1793 | struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) |
1da177e4 | 1794 | { |
be8432e7 | 1795 | unsigned long index = addr; |
1da177e4 | 1796 | |
5b78ed24 | 1797 | mmap_assert_locked(mm); |
7964cf8c | 1798 | return mt_find(&mm->mm_mt, &index, ULONG_MAX); |
1da177e4 | 1799 | } |
1da177e4 LT |
1800 | EXPORT_SYMBOL(find_vma); |
1801 | ||
7fdbd37d LH |
1802 | /** |
1803 | * find_vma_prev() - Find the VMA for a given address, or the next vma and | |
1804 | * set %pprev to the previous VMA, if any. | |
1805 | * @mm: The mm_struct to check | |
1806 | * @addr: The address | |
1807 | * @pprev: The pointer to set to the previous VMA | |
1808 | * | |
1809 | * Note that RCU lock is missing here since the external mmap_lock() is used | |
1810 | * instead. | |
1811 | * | |
1812 | * Returns: The VMA associated with @addr, or the next vma. | |
1813 | * May return %NULL in the case of no vma at addr or above. | |
6bd4837d | 1814 | */ |
1da177e4 LT |
1815 | struct vm_area_struct * |
1816 | find_vma_prev(struct mm_struct *mm, unsigned long addr, | |
1817 | struct vm_area_struct **pprev) | |
1818 | { | |
6bd4837d | 1819 | struct vm_area_struct *vma; |
7fdbd37d | 1820 | MA_STATE(mas, &mm->mm_mt, addr, addr); |
1da177e4 | 1821 | |
7fdbd37d LH |
1822 | vma = mas_walk(&mas); |
1823 | *pprev = mas_prev(&mas, 0); | |
1824 | if (!vma) | |
1825 | vma = mas_next(&mas, ULONG_MAX); | |
6bd4837d | 1826 | return vma; |
1da177e4 LT |
1827 | } |
1828 | ||
1829 | /* | |
1830 | * Verify that the stack growth is acceptable and | |
1831 | * update accounting. This is shared with both the | |
1832 | * grow-up and grow-down cases. | |
1833 | */ | |
1be7107f HD |
1834 | static int acct_stack_growth(struct vm_area_struct *vma, |
1835 | unsigned long size, unsigned long grow) | |
1da177e4 LT |
1836 | { |
1837 | struct mm_struct *mm = vma->vm_mm; | |
1be7107f | 1838 | unsigned long new_start; |
1da177e4 LT |
1839 | |
1840 | /* address space limit tests */ | |
84638335 | 1841 | if (!may_expand_vm(mm, vma->vm_flags, grow)) |
1da177e4 LT |
1842 | return -ENOMEM; |
1843 | ||
1844 | /* Stack limit test */ | |
24c79d8e | 1845 | if (size > rlimit(RLIMIT_STACK)) |
1da177e4 LT |
1846 | return -ENOMEM; |
1847 | ||
1848 | /* mlock limit tests */ | |
c5d8a364 ML |
1849 | if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) |
1850 | return -ENOMEM; | |
1da177e4 | 1851 | |
0d59a01b AL |
1852 | /* Check to ensure the stack will not grow into a hugetlb-only region */ |
1853 | new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : | |
1854 | vma->vm_end - size; | |
1855 | if (is_hugepage_only_range(vma->vm_mm, new_start, size)) | |
1856 | return -EFAULT; | |
1857 | ||
1da177e4 LT |
1858 | /* |
1859 | * Overcommit.. This must be the final test, as it will | |
1860 | * update security statistics. | |
1861 | */ | |
05fa199d | 1862 | if (security_vm_enough_memory_mm(mm, grow)) |
1da177e4 LT |
1863 | return -ENOMEM; |
1864 | ||
1da177e4 LT |
1865 | return 0; |
1866 | } | |
1867 | ||
46dea3d0 | 1868 | #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) |
1da177e4 | 1869 | /* |
46dea3d0 HD |
1870 | * PA-RISC uses this for its stack; IA64 for its Register Backing Store. |
1871 | * vma is the last one with address > vma->vm_end. Have to extend vma. | |
1da177e4 | 1872 | */ |
46dea3d0 | 1873 | int expand_upwards(struct vm_area_struct *vma, unsigned long address) |
1da177e4 | 1874 | { |
09357814 | 1875 | struct mm_struct *mm = vma->vm_mm; |
1be7107f HD |
1876 | struct vm_area_struct *next; |
1877 | unsigned long gap_addr; | |
12352d3c | 1878 | int error = 0; |
d4af56c5 | 1879 | MA_STATE(mas, &mm->mm_mt, 0, 0); |
1da177e4 LT |
1880 | |
1881 | if (!(vma->vm_flags & VM_GROWSUP)) | |
1882 | return -EFAULT; | |
1883 | ||
bd726c90 | 1884 | /* Guard against exceeding limits of the address space. */ |
1be7107f | 1885 | address &= PAGE_MASK; |
37511fb5 | 1886 | if (address >= (TASK_SIZE & PAGE_MASK)) |
12352d3c | 1887 | return -ENOMEM; |
bd726c90 | 1888 | address += PAGE_SIZE; |
12352d3c | 1889 | |
1be7107f HD |
1890 | /* Enforce stack_guard_gap */ |
1891 | gap_addr = address + stack_guard_gap; | |
bd726c90 HD |
1892 | |
1893 | /* Guard against overflow */ | |
1894 | if (gap_addr < address || gap_addr > TASK_SIZE) | |
1895 | gap_addr = TASK_SIZE; | |
1896 | ||
763ecb03 LH |
1897 | next = find_vma_intersection(mm, vma->vm_end, gap_addr); |
1898 | if (next && vma_is_accessible(next)) { | |
1be7107f HD |
1899 | if (!(next->vm_flags & VM_GROWSUP)) |
1900 | return -ENOMEM; | |
1901 | /* Check that both stack segments have the same anon_vma? */ | |
1902 | } | |
1903 | ||
c5d5546e | 1904 | if (mas_preallocate(&mas, GFP_KERNEL)) |
d4af56c5 LH |
1905 | return -ENOMEM; |
1906 | ||
12352d3c | 1907 | /* We must make sure the anon_vma is allocated. */ |
d4af56c5 LH |
1908 | if (unlikely(anon_vma_prepare(vma))) { |
1909 | mas_destroy(&mas); | |
1da177e4 | 1910 | return -ENOMEM; |
d4af56c5 | 1911 | } |
1da177e4 LT |
1912 | |
1913 | /* | |
1914 | * vma->vm_start/vm_end cannot change under us because the caller | |
c1e8d7c6 | 1915 | * is required to hold the mmap_lock in read mode. We need the |
1da177e4 LT |
1916 | * anon_vma lock to serialize against concurrent expand_stacks. |
1917 | */ | |
12352d3c | 1918 | anon_vma_lock_write(vma->anon_vma); |
1da177e4 LT |
1919 | |
1920 | /* Somebody else might have raced and expanded it already */ | |
1921 | if (address > vma->vm_end) { | |
1922 | unsigned long size, grow; | |
1923 | ||
1924 | size = address - vma->vm_start; | |
1925 | grow = (address - vma->vm_end) >> PAGE_SHIFT; | |
1926 | ||
42c36f63 HD |
1927 | error = -ENOMEM; |
1928 | if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { | |
1929 | error = acct_stack_growth(vma, size, grow); | |
1930 | if (!error) { | |
4128997b | 1931 | /* |
524e00b3 LH |
1932 | * We only hold a shared mmap_lock lock here, so |
1933 | * we need to protect against concurrent vma | |
1934 | * expansions. anon_vma_lock_write() doesn't | |
1935 | * help here, as we don't guarantee that all | |
1936 | * growable vmas in a mm share the same root | |
1937 | * anon vma. So, we reuse mm->page_table_lock | |
1938 | * to guard against concurrent vma expansions. | |
4128997b | 1939 | */ |
09357814 | 1940 | spin_lock(&mm->page_table_lock); |
87e8827b | 1941 | if (vma->vm_flags & VM_LOCKED) |
09357814 | 1942 | mm->locked_vm += grow; |
84638335 | 1943 | vm_stat_account(mm, vma->vm_flags, grow); |
bf181b9f | 1944 | anon_vma_interval_tree_pre_update_vma(vma); |
42c36f63 | 1945 | vma->vm_end = address; |
d4af56c5 | 1946 | /* Overwrite old entry in mtree. */ |
fbcc3104 LH |
1947 | mas_set_range(&mas, vma->vm_start, address - 1); |
1948 | mas_store_prealloc(&mas, vma); | |
bf181b9f | 1949 | anon_vma_interval_tree_post_update_vma(vma); |
09357814 | 1950 | spin_unlock(&mm->page_table_lock); |
4128997b | 1951 | |
42c36f63 HD |
1952 | perf_event_mmap(vma); |
1953 | } | |
3af9e859 | 1954 | } |
1da177e4 | 1955 | } |
12352d3c | 1956 | anon_vma_unlock_write(vma->anon_vma); |
c791576c | 1957 | khugepaged_enter_vma(vma, vma->vm_flags); |
d4af56c5 | 1958 | mas_destroy(&mas); |
1da177e4 LT |
1959 | return error; |
1960 | } | |
46dea3d0 HD |
1961 | #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ |
1962 | ||
1da177e4 LT |
1963 | /* |
1964 | * vma is the first one with address < vma->vm_start. Have to extend vma. | |
1965 | */ | |
524e00b3 | 1966 | int expand_downwards(struct vm_area_struct *vma, unsigned long address) |
1da177e4 | 1967 | { |
09357814 | 1968 | struct mm_struct *mm = vma->vm_mm; |
763ecb03 | 1969 | MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start); |
1be7107f | 1970 | struct vm_area_struct *prev; |
0a1d5299 | 1971 | int error = 0; |
1da177e4 | 1972 | |
8869477a | 1973 | address &= PAGE_MASK; |
0a1d5299 JH |
1974 | if (address < mmap_min_addr) |
1975 | return -EPERM; | |
8869477a | 1976 | |
1be7107f | 1977 | /* Enforce stack_guard_gap */ |
763ecb03 | 1978 | prev = mas_prev(&mas, 0); |
32e4e6d5 ON |
1979 | /* Check that both stack segments have the same anon_vma? */ |
1980 | if (prev && !(prev->vm_flags & VM_GROWSDOWN) && | |
3122e80e | 1981 | vma_is_accessible(prev)) { |
32e4e6d5 | 1982 | if (address - prev->vm_end < stack_guard_gap) |
1be7107f | 1983 | return -ENOMEM; |
1be7107f HD |
1984 | } |
1985 | ||
c5d5546e | 1986 | if (mas_preallocate(&mas, GFP_KERNEL)) |
d4af56c5 LH |
1987 | return -ENOMEM; |
1988 | ||
12352d3c | 1989 | /* We must make sure the anon_vma is allocated. */ |
d4af56c5 LH |
1990 | if (unlikely(anon_vma_prepare(vma))) { |
1991 | mas_destroy(&mas); | |
12352d3c | 1992 | return -ENOMEM; |
d4af56c5 | 1993 | } |
1da177e4 LT |
1994 | |
1995 | /* | |
1996 | * vma->vm_start/vm_end cannot change under us because the caller | |
c1e8d7c6 | 1997 | * is required to hold the mmap_lock in read mode. We need the |
1da177e4 LT |
1998 | * anon_vma lock to serialize against concurrent expand_stacks. |
1999 | */ | |
12352d3c | 2000 | anon_vma_lock_write(vma->anon_vma); |
1da177e4 LT |
2001 | |
2002 | /* Somebody else might have raced and expanded it already */ | |
2003 | if (address < vma->vm_start) { | |
2004 | unsigned long size, grow; | |
2005 | ||
2006 | size = vma->vm_end - address; | |
2007 | grow = (vma->vm_start - address) >> PAGE_SHIFT; | |
2008 | ||
a626ca6a LT |
2009 | error = -ENOMEM; |
2010 | if (grow <= vma->vm_pgoff) { | |
2011 | error = acct_stack_growth(vma, size, grow); | |
2012 | if (!error) { | |
4128997b | 2013 | /* |
524e00b3 LH |
2014 | * We only hold a shared mmap_lock lock here, so |
2015 | * we need to protect against concurrent vma | |
2016 | * expansions. anon_vma_lock_write() doesn't | |
2017 | * help here, as we don't guarantee that all | |
2018 | * growable vmas in a mm share the same root | |
2019 | * anon vma. So, we reuse mm->page_table_lock | |
2020 | * to guard against concurrent vma expansions. | |
4128997b | 2021 | */ |
09357814 | 2022 | spin_lock(&mm->page_table_lock); |
87e8827b | 2023 | if (vma->vm_flags & VM_LOCKED) |
09357814 | 2024 | mm->locked_vm += grow; |
84638335 | 2025 | vm_stat_account(mm, vma->vm_flags, grow); |
bf181b9f | 2026 | anon_vma_interval_tree_pre_update_vma(vma); |
a626ca6a LT |
2027 | vma->vm_start = address; |
2028 | vma->vm_pgoff -= grow; | |
d4af56c5 | 2029 | /* Overwrite old entry in mtree. */ |
fbcc3104 LH |
2030 | mas_set_range(&mas, address, vma->vm_end - 1); |
2031 | mas_store_prealloc(&mas, vma); | |
bf181b9f | 2032 | anon_vma_interval_tree_post_update_vma(vma); |
09357814 | 2033 | spin_unlock(&mm->page_table_lock); |
4128997b | 2034 | |
a626ca6a LT |
2035 | perf_event_mmap(vma); |
2036 | } | |
1da177e4 LT |
2037 | } |
2038 | } | |
12352d3c | 2039 | anon_vma_unlock_write(vma->anon_vma); |
c791576c | 2040 | khugepaged_enter_vma(vma, vma->vm_flags); |
d4af56c5 | 2041 | mas_destroy(&mas); |
1da177e4 LT |
2042 | return error; |
2043 | } | |
2044 | ||
1be7107f HD |
2045 | /* enforced gap between the expanding stack and other mappings. */ |
2046 | unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT; | |
2047 | ||
2048 | static int __init cmdline_parse_stack_guard_gap(char *p) | |
2049 | { | |
2050 | unsigned long val; | |
2051 | char *endptr; | |
2052 | ||
2053 | val = simple_strtoul(p, &endptr, 10); | |
2054 | if (!*endptr) | |
2055 | stack_guard_gap = val << PAGE_SHIFT; | |
2056 | ||
e6d09493 | 2057 | return 1; |
1be7107f HD |
2058 | } |
2059 | __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap); | |
2060 | ||
b6a2fea3 OW |
2061 | #ifdef CONFIG_STACK_GROWSUP |
2062 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | |
2063 | { | |
2064 | return expand_upwards(vma, address); | |
2065 | } | |
2066 | ||
2067 | struct vm_area_struct * | |
2068 | find_extend_vma(struct mm_struct *mm, unsigned long addr) | |
2069 | { | |
2070 | struct vm_area_struct *vma, *prev; | |
2071 | ||
2072 | addr &= PAGE_MASK; | |
2073 | vma = find_vma_prev(mm, addr, &prev); | |
2074 | if (vma && (vma->vm_start <= addr)) | |
2075 | return vma; | |
4d45e75a | 2076 | if (!prev || expand_stack(prev, addr)) |
b6a2fea3 | 2077 | return NULL; |
cea10a19 | 2078 | if (prev->vm_flags & VM_LOCKED) |
fc05f566 | 2079 | populate_vma_page_range(prev, addr, prev->vm_end, NULL); |
b6a2fea3 OW |
2080 | return prev; |
2081 | } | |
2082 | #else | |
2083 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | |
2084 | { | |
2085 | return expand_downwards(vma, address); | |
2086 | } | |
2087 | ||
1da177e4 | 2088 | struct vm_area_struct * |
cc71aba3 | 2089 | find_extend_vma(struct mm_struct *mm, unsigned long addr) |
1da177e4 | 2090 | { |
cc71aba3 | 2091 | struct vm_area_struct *vma; |
1da177e4 LT |
2092 | unsigned long start; |
2093 | ||
2094 | addr &= PAGE_MASK; | |
cc71aba3 | 2095 | vma = find_vma(mm, addr); |
1da177e4 LT |
2096 | if (!vma) |
2097 | return NULL; | |
2098 | if (vma->vm_start <= addr) | |
2099 | return vma; | |
2100 | if (!(vma->vm_flags & VM_GROWSDOWN)) | |
2101 | return NULL; | |
2102 | start = vma->vm_start; | |
2103 | if (expand_stack(vma, addr)) | |
2104 | return NULL; | |
cea10a19 | 2105 | if (vma->vm_flags & VM_LOCKED) |
fc05f566 | 2106 | populate_vma_page_range(vma, addr, start, NULL); |
1da177e4 LT |
2107 | return vma; |
2108 | } | |
2109 | #endif | |
2110 | ||
e1d6d01a JB |
2111 | EXPORT_SYMBOL_GPL(find_extend_vma); |
2112 | ||
1da177e4 | 2113 | /* |
763ecb03 LH |
2114 | * Ok - we have the memory areas we should free on a maple tree so release them, |
2115 | * and do the vma updates. | |
2c0b3814 HD |
2116 | * |
2117 | * Called with the mm semaphore held. | |
1da177e4 | 2118 | */ |
763ecb03 | 2119 | static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas) |
1da177e4 | 2120 | { |
4f74d2c8 | 2121 | unsigned long nr_accounted = 0; |
763ecb03 | 2122 | struct vm_area_struct *vma; |
4f74d2c8 | 2123 | |
365e9c87 HD |
2124 | /* Update high watermark before we lower total_vm */ |
2125 | update_hiwater_vm(mm); | |
763ecb03 | 2126 | mas_for_each(mas, vma, ULONG_MAX) { |
2c0b3814 HD |
2127 | long nrpages = vma_pages(vma); |
2128 | ||
4f74d2c8 LT |
2129 | if (vma->vm_flags & VM_ACCOUNT) |
2130 | nr_accounted += nrpages; | |
84638335 | 2131 | vm_stat_account(mm, vma->vm_flags, -nrpages); |
763ecb03 LH |
2132 | remove_vma(vma); |
2133 | } | |
4f74d2c8 | 2134 | vm_unacct_memory(nr_accounted); |
1da177e4 LT |
2135 | validate_mm(mm); |
2136 | } | |
2137 | ||
2138 | /* | |
2139 | * Get rid of page table information in the indicated region. | |
2140 | * | |
f10df686 | 2141 | * Called with the mm semaphore held. |
1da177e4 | 2142 | */ |
763ecb03 | 2143 | static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, |
e0da382c | 2144 | struct vm_area_struct *vma, struct vm_area_struct *prev, |
763ecb03 | 2145 | struct vm_area_struct *next, |
68f48381 | 2146 | unsigned long start, unsigned long end, bool mm_wr_locked) |
1da177e4 | 2147 | { |
d16dfc55 | 2148 | struct mmu_gather tlb; |
1da177e4 LT |
2149 | |
2150 | lru_add_drain(); | |
a72afd87 | 2151 | tlb_gather_mmu(&tlb, mm); |
365e9c87 | 2152 | update_hiwater_rss(mm); |
68f48381 | 2153 | unmap_vmas(&tlb, mt, vma, start, end, mm_wr_locked); |
763ecb03 | 2154 | free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, |
6ee8630e | 2155 | next ? next->vm_start : USER_PGTABLES_CEILING); |
ae8eba8b | 2156 | tlb_finish_mmu(&tlb); |
1da177e4 LT |
2157 | } |
2158 | ||
1da177e4 | 2159 | /* |
def5efe0 DR |
2160 | * __split_vma() bypasses sysctl_max_map_count checking. We use this where it |
2161 | * has already been checked or doesn't make sense to fail. | |
0fd5a9e2 | 2162 | * VMA Iterator will point to the end VMA. |
1da177e4 | 2163 | */ |
9760ebff | 2164 | int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, |
def5efe0 | 2165 | unsigned long addr, int new_below) |
1da177e4 | 2166 | { |
b2b3b886 | 2167 | struct vma_prepare vp; |
1da177e4 | 2168 | struct vm_area_struct *new; |
e3975891 | 2169 | int err; |
9760ebff LH |
2170 | |
2171 | validate_mm_mt(vma->vm_mm); | |
1da177e4 | 2172 | |
b2b3b886 LH |
2173 | WARN_ON(vma->vm_start >= addr); |
2174 | WARN_ON(vma->vm_end <= addr); | |
2175 | ||
dd3b614f DS |
2176 | if (vma->vm_ops && vma->vm_ops->may_split) { |
2177 | err = vma->vm_ops->may_split(vma, addr); | |
31383c68 DW |
2178 | if (err) |
2179 | return err; | |
2180 | } | |
1da177e4 | 2181 | |
3928d4f5 | 2182 | new = vm_area_dup(vma); |
1da177e4 | 2183 | if (!new) |
e3975891 | 2184 | return -ENOMEM; |
1da177e4 | 2185 | |
b2b3b886 LH |
2186 | err = -ENOMEM; |
2187 | if (vma_iter_prealloc(vmi)) | |
2188 | goto out_free_vma; | |
2189 | ||
2190 | if (new_below) { | |
1da177e4 | 2191 | new->vm_end = addr; |
b2b3b886 | 2192 | } else { |
1da177e4 LT |
2193 | new->vm_start = addr; |
2194 | new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); | |
2195 | } | |
2196 | ||
ef0855d3 ON |
2197 | err = vma_dup_policy(vma, new); |
2198 | if (err) | |
b2b3b886 | 2199 | goto out_free_vmi; |
1da177e4 | 2200 | |
c4ea95d7 DF |
2201 | err = anon_vma_clone(new, vma); |
2202 | if (err) | |
5beb4930 RR |
2203 | goto out_free_mpol; |
2204 | ||
e9714acf | 2205 | if (new->vm_file) |
1da177e4 LT |
2206 | get_file(new->vm_file); |
2207 | ||
2208 | if (new->vm_ops && new->vm_ops->open) | |
2209 | new->vm_ops->open(new); | |
2210 | ||
b2b3b886 LH |
2211 | vma_adjust_trans_huge(vma, vma->vm_start, addr, 0); |
2212 | init_vma_prep(&vp, vma); | |
2213 | vp.insert = new; | |
2214 | vma_prepare(&vp); | |
1da177e4 | 2215 | |
b2b3b886 LH |
2216 | if (new_below) { |
2217 | vma->vm_start = addr; | |
2218 | vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT; | |
2219 | } else { | |
2220 | vma->vm_end = addr; | |
9760ebff | 2221 | } |
5beb4930 | 2222 | |
b2b3b886 LH |
2223 | /* vma_complete stores the new vma */ |
2224 | vma_complete(&vp, vmi, vma->vm_mm); | |
2225 | ||
2226 | /* Success. */ | |
2227 | if (new_below) | |
2228 | vma_next(vmi); | |
2229 | validate_mm_mt(vma->vm_mm); | |
2230 | return 0; | |
2231 | ||
2232 | out_free_mpol: | |
ef0855d3 | 2233 | mpol_put(vma_policy(new)); |
b2b3b886 LH |
2234 | out_free_vmi: |
2235 | vma_iter_free(vmi); | |
2236 | out_free_vma: | |
3928d4f5 | 2237 | vm_area_free(new); |
9760ebff | 2238 | validate_mm_mt(vma->vm_mm); |
5beb4930 | 2239 | return err; |
1da177e4 LT |
2240 | } |
2241 | ||
659ace58 KM |
2242 | /* |
2243 | * Split a vma into two pieces at address 'addr', a new vma is allocated | |
2244 | * either for the first part or the tail. | |
2245 | */ | |
9760ebff | 2246 | int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, |
659ace58 KM |
2247 | unsigned long addr, int new_below) |
2248 | { | |
9760ebff | 2249 | if (vma->vm_mm->map_count >= sysctl_max_map_count) |
659ace58 KM |
2250 | return -ENOMEM; |
2251 | ||
9760ebff | 2252 | return __split_vma(vmi, vma, addr, new_below); |
f2ebfe43 LH |
2253 | } |
2254 | ||
763ecb03 LH |
2255 | static inline int munmap_sidetree(struct vm_area_struct *vma, |
2256 | struct ma_state *mas_detach) | |
1da177e4 | 2257 | { |
763ecb03 LH |
2258 | mas_set_range(mas_detach, vma->vm_start, vma->vm_end - 1); |
2259 | if (mas_store_gfp(mas_detach, vma, GFP_KERNEL)) | |
2260 | return -ENOMEM; | |
1da177e4 | 2261 | |
763ecb03 LH |
2262 | if (vma->vm_flags & VM_LOCKED) |
2263 | vma->vm_mm->locked_vm -= vma_pages(vma); | |
1da177e4 | 2264 | |
763ecb03 | 2265 | return 0; |
11f9a21a | 2266 | } |
5a28fc94 | 2267 | |
11f9a21a | 2268 | /* |
183654ce LH |
2269 | * do_vmi_align_munmap() - munmap the aligned region from @start to @end. |
2270 | * @vmi: The vma iterator | |
11f9a21a LH |
2271 | * @vma: The starting vm_area_struct |
2272 | * @mm: The mm_struct | |
2273 | * @start: The aligned start address to munmap. | |
2274 | * @end: The aligned end address to munmap. | |
2275 | * @uf: The userfaultfd list_head | |
8651a137 | 2276 | * @downgrade: Set to true to attempt a write downgrade of the mmap_lock |
11f9a21a LH |
2277 | * |
2278 | * If @downgrade is true, check return code for potential release of the lock. | |
2279 | */ | |
2280 | static int | |
183654ce | 2281 | do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, |
11f9a21a LH |
2282 | struct mm_struct *mm, unsigned long start, |
2283 | unsigned long end, struct list_head *uf, bool downgrade) | |
2284 | { | |
763ecb03 LH |
2285 | struct vm_area_struct *prev, *next = NULL; |
2286 | struct maple_tree mt_detach; | |
2287 | int count = 0; | |
11f9a21a | 2288 | int error = -ENOMEM; |
763ecb03 LH |
2289 | MA_STATE(mas_detach, &mt_detach, 0, 0); |
2290 | mt_init_flags(&mt_detach, MT_FLAGS_LOCK_EXTERN); | |
2291 | mt_set_external_lock(&mt_detach, &mm->mmap_lock); | |
d4af56c5 | 2292 | |
1da177e4 LT |
2293 | /* |
2294 | * If we need to split any vma, do it now to save pain later. | |
2295 | * | |
2296 | * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially | |
2297 | * unmapped vm_area_struct will remain in use: so lower split_vma | |
2298 | * places tmp vma above, and higher split_vma places tmp vma below. | |
2299 | */ | |
763ecb03 LH |
2300 | |
2301 | /* Does it split the first one? */ | |
146425a3 | 2302 | if (start > vma->vm_start) { |
659ace58 KM |
2303 | |
2304 | /* | |
2305 | * Make sure that map_count on return from munmap() will | |
2306 | * not exceed its limit; but let map_count go just above | |
2307 | * its limit temporarily, to help free resources as expected. | |
2308 | */ | |
2309 | if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) | |
d4af56c5 | 2310 | goto map_count_exceeded; |
659ace58 | 2311 | |
9760ebff | 2312 | error = __split_vma(vmi, vma, start, 0); |
1da177e4 | 2313 | if (error) |
763ecb03 | 2314 | goto start_split_failed; |
11f9a21a | 2315 | |
0fd5a9e2 | 2316 | vma = vma_iter_load(vmi); |
1da177e4 LT |
2317 | } |
2318 | ||
183654ce | 2319 | prev = vma_prev(vmi); |
763ecb03 | 2320 | if (unlikely((!prev))) |
183654ce | 2321 | vma_iter_set(vmi, start); |
763ecb03 LH |
2322 | |
2323 | /* | |
2324 | * Detach a range of VMAs from the mm. Using next as a temp variable as | |
2325 | * it is always overwritten. | |
2326 | */ | |
183654ce | 2327 | for_each_vma_range(*vmi, next, end) { |
763ecb03 LH |
2328 | /* Does it split the end? */ |
2329 | if (next->vm_end > end) { | |
6b73cff2 | 2330 | error = __split_vma(vmi, next, end, 0); |
763ecb03 LH |
2331 | if (error) |
2332 | goto end_split_failed; | |
763ecb03 LH |
2333 | } |
2334 | error = munmap_sidetree(next, &mas_detach); | |
1da177e4 | 2335 | if (error) |
763ecb03 | 2336 | goto munmap_sidetree_failed; |
11f9a21a | 2337 | |
763ecb03 LH |
2338 | count++; |
2339 | #ifdef CONFIG_DEBUG_VM_MAPLE_TREE | |
2340 | BUG_ON(next->vm_start < start); | |
2341 | BUG_ON(next->vm_start > end); | |
2342 | #endif | |
1da177e4 | 2343 | } |
1da177e4 | 2344 | |
6b73cff2 | 2345 | next = vma_next(vmi); |
2376dd7c AA |
2346 | if (unlikely(uf)) { |
2347 | /* | |
2348 | * If userfaultfd_unmap_prep returns an error the vmas | |
f0953a1b | 2349 | * will remain split, but userland will get a |
2376dd7c AA |
2350 | * highly unexpected error anyway. This is no |
2351 | * different than the case where the first of the two | |
2352 | * __split_vma fails, but we don't undo the first | |
2353 | * split, despite we could. This is unlikely enough | |
2354 | * failure that it's not worth optimizing it for. | |
2355 | */ | |
69dbe6da | 2356 | error = userfaultfd_unmap_prep(mm, start, end, uf); |
11f9a21a | 2357 | |
2376dd7c | 2358 | if (error) |
d4af56c5 | 2359 | goto userfaultfd_error; |
2376dd7c AA |
2360 | } |
2361 | ||
763ecb03 LH |
2362 | #if defined(CONFIG_DEBUG_VM_MAPLE_TREE) |
2363 | /* Make sure no VMAs are about to be lost. */ | |
2364 | { | |
2365 | MA_STATE(test, &mt_detach, start, end - 1); | |
2366 | struct vm_area_struct *vma_mas, *vma_test; | |
2367 | int test_count = 0; | |
2368 | ||
183654ce | 2369 | vma_iter_set(vmi, start); |
763ecb03 LH |
2370 | rcu_read_lock(); |
2371 | vma_test = mas_find(&test, end - 1); | |
183654ce | 2372 | for_each_vma_range(*vmi, vma_mas, end) { |
763ecb03 LH |
2373 | BUG_ON(vma_mas != vma_test); |
2374 | test_count++; | |
2375 | vma_test = mas_next(&test, end - 1); | |
2376 | } | |
2377 | rcu_read_unlock(); | |
2378 | BUG_ON(count != test_count); | |
763ecb03 LH |
2379 | } |
2380 | #endif | |
0378c0a0 | 2381 | /* Point of no return */ |
183654ce LH |
2382 | vma_iter_set(vmi, start); |
2383 | if (vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL)) | |
0378c0a0 LH |
2384 | return -ENOMEM; |
2385 | ||
763ecb03 | 2386 | mm->map_count -= count; |
11f9a21a LH |
2387 | /* |
2388 | * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or | |
2389 | * VM_GROWSUP VMA. Such VMAs can change their size under | |
2390 | * down_read(mmap_lock) and collide with the VMA we are about to unmap. | |
2391 | */ | |
2392 | if (downgrade) { | |
763ecb03 | 2393 | if (next && (next->vm_flags & VM_GROWSDOWN)) |
11f9a21a LH |
2394 | downgrade = false; |
2395 | else if (prev && (prev->vm_flags & VM_GROWSUP)) | |
2396 | downgrade = false; | |
2397 | else | |
2398 | mmap_write_downgrade(mm); | |
2399 | } | |
dd2283f2 | 2400 | |
68f48381 SB |
2401 | /* |
2402 | * We can free page tables without write-locking mmap_lock because VMAs | |
2403 | * were isolated before we downgraded mmap_lock. | |
2404 | */ | |
2405 | unmap_region(mm, &mt_detach, vma, prev, next, start, end, !downgrade); | |
763ecb03 LH |
2406 | /* Statistics and freeing VMAs */ |
2407 | mas_set(&mas_detach, start); | |
2408 | remove_mt(mm, &mas_detach); | |
2409 | __mt_destroy(&mt_detach); | |
1da177e4 | 2410 | |
524e00b3 LH |
2411 | |
2412 | validate_mm(mm); | |
dd2283f2 | 2413 | return downgrade ? 1 : 0; |
d4af56c5 | 2414 | |
d4af56c5 | 2415 | userfaultfd_error: |
763ecb03 LH |
2416 | munmap_sidetree_failed: |
2417 | end_split_failed: | |
2418 | __mt_destroy(&mt_detach); | |
2419 | start_split_failed: | |
2420 | map_count_exceeded: | |
d4af56c5 | 2421 | return error; |
1da177e4 | 2422 | } |
1da177e4 | 2423 | |
11f9a21a | 2424 | /* |
183654ce LH |
2425 | * do_vmi_munmap() - munmap a given range. |
2426 | * @vmi: The vma iterator | |
11f9a21a LH |
2427 | * @mm: The mm_struct |
2428 | * @start: The start address to munmap | |
2429 | * @len: The length of the range to munmap | |
2430 | * @uf: The userfaultfd list_head | |
2431 | * @downgrade: set to true if the user wants to attempt to write_downgrade the | |
8651a137 | 2432 | * mmap_lock |
11f9a21a LH |
2433 | * |
2434 | * This function takes a @mas that is either pointing to the previous VMA or set | |
2435 | * to MA_START and sets it up to remove the mapping(s). The @len will be | |
2436 | * aligned and any arch_unmap work will be preformed. | |
2437 | * | |
2438 | * Returns: -EINVAL on failure, 1 on success and unlock, 0 otherwise. | |
2439 | */ | |
183654ce | 2440 | int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, |
11f9a21a LH |
2441 | unsigned long start, size_t len, struct list_head *uf, |
2442 | bool downgrade) | |
2443 | { | |
2444 | unsigned long end; | |
2445 | struct vm_area_struct *vma; | |
2446 | ||
2447 | if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start) | |
2448 | return -EINVAL; | |
2449 | ||
2450 | end = start + PAGE_ALIGN(len); | |
2451 | if (end == start) | |
2452 | return -EINVAL; | |
2453 | ||
2454 | /* arch_unmap() might do unmaps itself. */ | |
2455 | arch_unmap(mm, start, end); | |
2456 | ||
2457 | /* Find the first overlapping VMA */ | |
183654ce | 2458 | vma = vma_find(vmi, end); |
11f9a21a LH |
2459 | if (!vma) |
2460 | return 0; | |
2461 | ||
183654ce | 2462 | return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); |
11f9a21a LH |
2463 | } |
2464 | ||
2465 | /* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls. | |
2466 | * @mm: The mm_struct | |
2467 | * @start: The start address to munmap | |
2468 | * @len: The length to be munmapped. | |
2469 | * @uf: The userfaultfd list_head | |
2470 | */ | |
dd2283f2 YS |
2471 | int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, |
2472 | struct list_head *uf) | |
2473 | { | |
183654ce | 2474 | VMA_ITERATOR(vmi, mm, start); |
11f9a21a | 2475 | |
183654ce | 2476 | return do_vmi_munmap(&vmi, mm, start, len, uf, false); |
dd2283f2 YS |
2477 | } |
2478 | ||
e99668a5 LH |
2479 | unsigned long mmap_region(struct file *file, unsigned long addr, |
2480 | unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, | |
2481 | struct list_head *uf) | |
2482 | { | |
2483 | struct mm_struct *mm = current->mm; | |
2484 | struct vm_area_struct *vma = NULL; | |
2485 | struct vm_area_struct *next, *prev, *merge; | |
2486 | pgoff_t pglen = len >> PAGE_SHIFT; | |
2487 | unsigned long charged = 0; | |
2488 | unsigned long end = addr + len; | |
2489 | unsigned long merge_start = addr, merge_end = end; | |
2490 | pgoff_t vm_pgoff; | |
2491 | int error; | |
183654ce | 2492 | VMA_ITERATOR(vmi, mm, addr); |
e99668a5 LH |
2493 | |
2494 | /* Check against address space limit. */ | |
2495 | if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { | |
2496 | unsigned long nr_pages; | |
2497 | ||
2498 | /* | |
2499 | * MAP_FIXED may remove pages of mappings that intersects with | |
2500 | * requested mapping. Account for the pages it would unmap. | |
2501 | */ | |
2502 | nr_pages = count_vma_pages_range(mm, addr, end); | |
2503 | ||
2504 | if (!may_expand_vm(mm, vm_flags, | |
2505 | (len >> PAGE_SHIFT) - nr_pages)) | |
2506 | return -ENOMEM; | |
2507 | } | |
2508 | ||
2509 | /* Unmap any existing mapping in the area */ | |
183654ce | 2510 | if (do_vmi_munmap(&vmi, mm, addr, len, uf, false)) |
e99668a5 LH |
2511 | return -ENOMEM; |
2512 | ||
2513 | /* | |
2514 | * Private writable mapping: check memory availability | |
2515 | */ | |
2516 | if (accountable_mapping(file, vm_flags)) { | |
2517 | charged = len >> PAGE_SHIFT; | |
2518 | if (security_vm_enough_memory_mm(mm, charged)) | |
2519 | return -ENOMEM; | |
2520 | vm_flags |= VM_ACCOUNT; | |
2521 | } | |
2522 | ||
183654ce LH |
2523 | next = vma_next(&vmi); |
2524 | prev = vma_prev(&vmi); | |
e99668a5 LH |
2525 | if (vm_flags & VM_SPECIAL) |
2526 | goto cannot_expand; | |
2527 | ||
2528 | /* Attempt to expand an old mapping */ | |
2529 | /* Check next */ | |
2530 | if (next && next->vm_start == end && !vma_policy(next) && | |
2531 | can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen, | |
2532 | NULL_VM_UFFD_CTX, NULL)) { | |
2533 | merge_end = next->vm_end; | |
2534 | vma = next; | |
2535 | vm_pgoff = next->vm_pgoff - pglen; | |
2536 | } | |
2537 | ||
2538 | /* Check prev */ | |
2539 | if (prev && prev->vm_end == addr && !vma_policy(prev) && | |
2540 | (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file, | |
2541 | pgoff, vma->vm_userfaultfd_ctx, NULL) : | |
2542 | can_vma_merge_after(prev, vm_flags, NULL, file, pgoff, | |
2543 | NULL_VM_UFFD_CTX, NULL))) { | |
2544 | merge_start = prev->vm_start; | |
2545 | vma = prev; | |
2546 | vm_pgoff = prev->vm_pgoff; | |
2547 | } | |
2548 | ||
2549 | ||
2550 | /* Actually expand, if possible */ | |
2551 | if (vma && | |
3c441ab7 | 2552 | !vma_expand(&vmi, vma, merge_start, merge_end, vm_pgoff, next)) { |
e99668a5 LH |
2553 | khugepaged_enter_vma(vma, vm_flags); |
2554 | goto expanded; | |
2555 | } | |
2556 | ||
e99668a5 LH |
2557 | cannot_expand: |
2558 | /* | |
2559 | * Determine the object being mapped and call the appropriate | |
2560 | * specific mapper. the address has already been validated, but | |
2561 | * not unmapped, but the maps are removed from the list. | |
2562 | */ | |
2563 | vma = vm_area_alloc(mm); | |
2564 | if (!vma) { | |
2565 | error = -ENOMEM; | |
2566 | goto unacct_error; | |
2567 | } | |
2568 | ||
0fd5a9e2 | 2569 | vma_iter_set(&vmi, addr); |
e99668a5 LH |
2570 | vma->vm_start = addr; |
2571 | vma->vm_end = end; | |
1c71222e | 2572 | vm_flags_init(vma, vm_flags); |
e99668a5 LH |
2573 | vma->vm_page_prot = vm_get_page_prot(vm_flags); |
2574 | vma->vm_pgoff = pgoff; | |
2575 | ||
2576 | if (file) { | |
2577 | if (vm_flags & VM_SHARED) { | |
2578 | error = mapping_map_writable(file->f_mapping); | |
2579 | if (error) | |
2580 | goto free_vma; | |
2581 | } | |
2582 | ||
2583 | vma->vm_file = get_file(file); | |
2584 | error = call_mmap(file, vma); | |
2585 | if (error) | |
2586 | goto unmap_and_free_vma; | |
2587 | ||
a57b7051 LH |
2588 | /* |
2589 | * Expansion is handled above, merging is handled below. | |
2590 | * Drivers should not alter the address of the VMA. | |
e99668a5 | 2591 | */ |
cc8d1b09 LH |
2592 | error = -EINVAL; |
2593 | if (WARN_ON((addr != vma->vm_start))) | |
a57b7051 | 2594 | goto close_and_free_vma; |
e99668a5 | 2595 | |
cc8d1b09 | 2596 | vma_iter_set(&vmi, addr); |
e99668a5 LH |
2597 | /* |
2598 | * If vm_flags changed after call_mmap(), we should try merge | |
2599 | * vma again as we may succeed this time. | |
2600 | */ | |
2601 | if (unlikely(vm_flags != vma->vm_flags && prev)) { | |
9760ebff LH |
2602 | merge = vma_merge(&vmi, mm, prev, vma->vm_start, |
2603 | vma->vm_end, vma->vm_flags, NULL, | |
2604 | vma->vm_file, vma->vm_pgoff, NULL, | |
2605 | NULL_VM_UFFD_CTX, NULL); | |
e99668a5 LH |
2606 | if (merge) { |
2607 | /* | |
2608 | * ->mmap() can change vma->vm_file and fput | |
2609 | * the original file. So fput the vma->vm_file | |
2610 | * here or we would add an extra fput for file | |
2611 | * and cause general protection fault | |
2612 | * ultimately. | |
2613 | */ | |
2614 | fput(vma->vm_file); | |
2615 | vm_area_free(vma); | |
2616 | vma = merge; | |
2617 | /* Update vm_flags to pick up the change. */ | |
e99668a5 LH |
2618 | vm_flags = vma->vm_flags; |
2619 | goto unmap_writable; | |
2620 | } | |
2621 | } | |
2622 | ||
2623 | vm_flags = vma->vm_flags; | |
2624 | } else if (vm_flags & VM_SHARED) { | |
2625 | error = shmem_zero_setup(vma); | |
2626 | if (error) | |
2627 | goto free_vma; | |
2628 | } else { | |
2629 | vma_set_anonymous(vma); | |
2630 | } | |
2631 | ||
b507808e JG |
2632 | if (map_deny_write_exec(vma, vma->vm_flags)) { |
2633 | error = -EACCES; | |
6bbf1090 | 2634 | goto close_and_free_vma; |
b507808e JG |
2635 | } |
2636 | ||
e99668a5 | 2637 | /* Allow architectures to sanity-check the vm_flags */ |
cc8d1b09 LH |
2638 | error = -EINVAL; |
2639 | if (!arch_validate_flags(vma->vm_flags)) | |
2640 | goto close_and_free_vma; | |
e99668a5 | 2641 | |
cc8d1b09 LH |
2642 | error = -ENOMEM; |
2643 | if (vma_iter_prealloc(&vmi)) | |
2644 | goto close_and_free_vma; | |
e99668a5 LH |
2645 | |
2646 | if (vma->vm_file) | |
2647 | i_mmap_lock_write(vma->vm_file->f_mapping); | |
2648 | ||
183654ce | 2649 | vma_iter_store(&vmi, vma); |
e99668a5 LH |
2650 | mm->map_count++; |
2651 | if (vma->vm_file) { | |
2652 | if (vma->vm_flags & VM_SHARED) | |
2653 | mapping_allow_writable(vma->vm_file->f_mapping); | |
2654 | ||
2655 | flush_dcache_mmap_lock(vma->vm_file->f_mapping); | |
2656 | vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); | |
2657 | flush_dcache_mmap_unlock(vma->vm_file->f_mapping); | |
2658 | i_mmap_unlock_write(vma->vm_file->f_mapping); | |
2659 | } | |
2660 | ||
2661 | /* | |
2662 | * vma_merge() calls khugepaged_enter_vma() either, the below | |
2663 | * call covers the non-merge case. | |
2664 | */ | |
2665 | khugepaged_enter_vma(vma, vma->vm_flags); | |
2666 | ||
2667 | /* Once vma denies write, undo our temporary denial count */ | |
2668 | unmap_writable: | |
2669 | if (file && vm_flags & VM_SHARED) | |
2670 | mapping_unmap_writable(file->f_mapping); | |
2671 | file = vma->vm_file; | |
2672 | expanded: | |
2673 | perf_event_mmap(vma); | |
2674 | ||
2675 | vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); | |
2676 | if (vm_flags & VM_LOCKED) { | |
2677 | if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || | |
2678 | is_vm_hugetlb_page(vma) || | |
2679 | vma == get_gate_vma(current->mm)) | |
e430a95a | 2680 | vm_flags_clear(vma, VM_LOCKED_MASK); |
e99668a5 LH |
2681 | else |
2682 | mm->locked_vm += (len >> PAGE_SHIFT); | |
2683 | } | |
2684 | ||
2685 | if (file) | |
2686 | uprobe_mmap(vma); | |
2687 | ||
2688 | /* | |
2689 | * New (or expanded) vma always get soft dirty status. | |
2690 | * Otherwise user-space soft-dirty page tracker won't | |
2691 | * be able to distinguish situation when vma area unmapped, | |
2692 | * then new mapped in-place (which must be aimed as | |
2693 | * a completely new data area). | |
2694 | */ | |
1c71222e | 2695 | vm_flags_set(vma, VM_SOFTDIRTY); |
e99668a5 LH |
2696 | |
2697 | vma_set_page_prot(vma); | |
2698 | ||
2699 | validate_mm(mm); | |
2700 | return addr; | |
2701 | ||
deb0f656 | 2702 | close_and_free_vma: |
cc8d1b09 | 2703 | if (file && vma->vm_ops && vma->vm_ops->close) |
deb0f656 | 2704 | vma->vm_ops->close(vma); |
cc8d1b09 LH |
2705 | |
2706 | if (file || vma->vm_file) { | |
e99668a5 | 2707 | unmap_and_free_vma: |
cc8d1b09 LH |
2708 | fput(vma->vm_file); |
2709 | vma->vm_file = NULL; | |
e99668a5 | 2710 | |
cc8d1b09 LH |
2711 | /* Undo any partial mapping done by a device driver. */ |
2712 | unmap_region(mm, &mm->mm_mt, vma, prev, next, vma->vm_start, | |
68f48381 | 2713 | vma->vm_end, true); |
cc8d1b09 | 2714 | } |
cc674ab3 | 2715 | if (file && (vm_flags & VM_SHARED)) |
e99668a5 LH |
2716 | mapping_unmap_writable(file->f_mapping); |
2717 | free_vma: | |
2718 | vm_area_free(vma); | |
2719 | unacct_error: | |
2720 | if (charged) | |
2721 | vm_unacct_memory(charged); | |
2722 | validate_mm(mm); | |
2723 | return error; | |
2724 | } | |
2725 | ||
dd2283f2 | 2726 | static int __vm_munmap(unsigned long start, size_t len, bool downgrade) |
1da177e4 LT |
2727 | { |
2728 | int ret; | |
bfce281c | 2729 | struct mm_struct *mm = current->mm; |
897ab3e0 | 2730 | LIST_HEAD(uf); |
183654ce | 2731 | VMA_ITERATOR(vmi, mm, start); |
1da177e4 | 2732 | |
d8ed45c5 | 2733 | if (mmap_write_lock_killable(mm)) |
ae798783 MH |
2734 | return -EINTR; |
2735 | ||
183654ce | 2736 | ret = do_vmi_munmap(&vmi, mm, start, len, &uf, downgrade); |
dd2283f2 | 2737 | /* |
c1e8d7c6 | 2738 | * Returning 1 indicates mmap_lock is downgraded. |
dd2283f2 YS |
2739 | * But 1 is not legal return value of vm_munmap() and munmap(), reset |
2740 | * it to 0 before return. | |
2741 | */ | |
2742 | if (ret == 1) { | |
d8ed45c5 | 2743 | mmap_read_unlock(mm); |
dd2283f2 YS |
2744 | ret = 0; |
2745 | } else | |
d8ed45c5 | 2746 | mmap_write_unlock(mm); |
dd2283f2 | 2747 | |
897ab3e0 | 2748 | userfaultfd_unmap_complete(mm, &uf); |
1da177e4 LT |
2749 | return ret; |
2750 | } | |
dd2283f2 YS |
2751 | |
2752 | int vm_munmap(unsigned long start, size_t len) | |
2753 | { | |
2754 | return __vm_munmap(start, len, false); | |
2755 | } | |
a46ef99d LT |
2756 | EXPORT_SYMBOL(vm_munmap); |
2757 | ||
2758 | SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) | |
2759 | { | |
ce18d171 | 2760 | addr = untagged_addr(addr); |
dd2283f2 | 2761 | return __vm_munmap(addr, len, true); |
a46ef99d | 2762 | } |
1da177e4 | 2763 | |
c8d78c18 KS |
2764 | |
2765 | /* | |
2766 | * Emulation of deprecated remap_file_pages() syscall. | |
2767 | */ | |
2768 | SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, | |
2769 | unsigned long, prot, unsigned long, pgoff, unsigned long, flags) | |
2770 | { | |
2771 | ||
2772 | struct mm_struct *mm = current->mm; | |
2773 | struct vm_area_struct *vma; | |
2774 | unsigned long populate = 0; | |
2775 | unsigned long ret = -EINVAL; | |
2776 | struct file *file; | |
2777 | ||
ee65728e | 2778 | pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n", |
756a025f | 2779 | current->comm, current->pid); |
c8d78c18 KS |
2780 | |
2781 | if (prot) | |
2782 | return ret; | |
2783 | start = start & PAGE_MASK; | |
2784 | size = size & PAGE_MASK; | |
2785 | ||
2786 | if (start + size <= start) | |
2787 | return ret; | |
2788 | ||
2789 | /* Does pgoff wrap? */ | |
2790 | if (pgoff + (size >> PAGE_SHIFT) < pgoff) | |
2791 | return ret; | |
2792 | ||
d8ed45c5 | 2793 | if (mmap_write_lock_killable(mm)) |
dc0ef0df MH |
2794 | return -EINTR; |
2795 | ||
9b593cb2 | 2796 | vma = vma_lookup(mm, start); |
c8d78c18 KS |
2797 | |
2798 | if (!vma || !(vma->vm_flags & VM_SHARED)) | |
2799 | goto out; | |
2800 | ||
48f7df32 | 2801 | if (start + size > vma->vm_end) { |
763ecb03 LH |
2802 | VMA_ITERATOR(vmi, mm, vma->vm_end); |
2803 | struct vm_area_struct *next, *prev = vma; | |
48f7df32 | 2804 | |
763ecb03 | 2805 | for_each_vma_range(vmi, next, start + size) { |
48f7df32 | 2806 | /* hole between vmas ? */ |
763ecb03 | 2807 | if (next->vm_start != prev->vm_end) |
48f7df32 KS |
2808 | goto out; |
2809 | ||
2810 | if (next->vm_file != vma->vm_file) | |
2811 | goto out; | |
2812 | ||
2813 | if (next->vm_flags != vma->vm_flags) | |
2814 | goto out; | |
2815 | ||
1db43d3f LH |
2816 | if (start + size <= next->vm_end) |
2817 | break; | |
2818 | ||
763ecb03 | 2819 | prev = next; |
48f7df32 KS |
2820 | } |
2821 | ||
2822 | if (!next) | |
2823 | goto out; | |
c8d78c18 KS |
2824 | } |
2825 | ||
2826 | prot |= vma->vm_flags & VM_READ ? PROT_READ : 0; | |
2827 | prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0; | |
2828 | prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0; | |
2829 | ||
2830 | flags &= MAP_NONBLOCK; | |
2831 | flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE; | |
fce000b1 | 2832 | if (vma->vm_flags & VM_LOCKED) |
c8d78c18 | 2833 | flags |= MAP_LOCKED; |
48f7df32 | 2834 | |
c8d78c18 | 2835 | file = get_file(vma->vm_file); |
45e55300 | 2836 | ret = do_mmap(vma->vm_file, start, size, |
897ab3e0 | 2837 | prot, flags, pgoff, &populate, NULL); |
c8d78c18 KS |
2838 | fput(file); |
2839 | out: | |
d8ed45c5 | 2840 | mmap_write_unlock(mm); |
c8d78c18 KS |
2841 | if (populate) |
2842 | mm_populate(ret, populate); | |
2843 | if (!IS_ERR_VALUE(ret)) | |
2844 | ret = 0; | |
2845 | return ret; | |
2846 | } | |
2847 | ||
1da177e4 | 2848 | /* |
27b26701 LH |
2849 | * do_vma_munmap() - Unmap a full or partial vma. |
2850 | * @vmi: The vma iterator pointing at the vma | |
2851 | * @vma: The first vma to be munmapped | |
2852 | * @start: the start of the address to unmap | |
2853 | * @end: The end of the address to unmap | |
2e7ce7d3 | 2854 | * @uf: The userfaultfd list_head |
27b26701 | 2855 | * @downgrade: Attempt to downgrade or not |
2e7ce7d3 | 2856 | * |
27b26701 LH |
2857 | * Returns: 0 on success and not downgraded, 1 on success and downgraded. |
2858 | * unmaps a VMA mapping when the vma iterator is already in position. | |
2859 | * Does not handle alignment. | |
1da177e4 | 2860 | */ |
27b26701 LH |
2861 | int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, |
2862 | unsigned long start, unsigned long end, | |
2863 | struct list_head *uf, bool downgrade) | |
1da177e4 | 2864 | { |
2e7ce7d3 LH |
2865 | struct mm_struct *mm = vma->vm_mm; |
2866 | int ret; | |
3a459756 | 2867 | |
27b26701 LH |
2868 | arch_unmap(mm, start, end); |
2869 | ret = do_vmi_align_munmap(vmi, vma, mm, start, end, uf, downgrade); | |
2e7ce7d3 LH |
2870 | validate_mm_mt(mm); |
2871 | return ret; | |
2872 | } | |
1da177e4 | 2873 | |
2e7ce7d3 LH |
2874 | /* |
2875 | * do_brk_flags() - Increase the brk vma if the flags match. | |
92fed820 | 2876 | * @vmi: The vma iterator |
2e7ce7d3 LH |
2877 | * @addr: The start address |
2878 | * @len: The length of the increase | |
2879 | * @vma: The vma, | |
2880 | * @flags: The VMA Flags | |
2881 | * | |
2882 | * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags | |
2883 | * do not match then create a new anonymous VMA. Eventually we may be able to | |
2884 | * do some brk-specific accounting here. | |
2885 | */ | |
92fed820 | 2886 | static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, |
763ecb03 | 2887 | unsigned long addr, unsigned long len, unsigned long flags) |
2e7ce7d3 LH |
2888 | { |
2889 | struct mm_struct *mm = current->mm; | |
287051b1 | 2890 | struct vma_prepare vp; |
1da177e4 | 2891 | |
2e7ce7d3 LH |
2892 | validate_mm_mt(mm); |
2893 | /* | |
2894 | * Check against address space limits by the changed size | |
2895 | * Note: This happens *after* clearing old mappings in some code paths. | |
2896 | */ | |
2897 | flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; | |
84638335 | 2898 | if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT)) |
1da177e4 LT |
2899 | return -ENOMEM; |
2900 | ||
2901 | if (mm->map_count > sysctl_max_map_count) | |
2902 | return -ENOMEM; | |
2903 | ||
191c5424 | 2904 | if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) |
1da177e4 LT |
2905 | return -ENOMEM; |
2906 | ||
1da177e4 | 2907 | /* |
2e7ce7d3 LH |
2908 | * Expand the existing vma if possible; Note that singular lists do not |
2909 | * occur after forking, so the expand will only happen on new VMAs. | |
1da177e4 | 2910 | */ |
6c28ca64 LH |
2911 | if (vma && vma->vm_end == addr && !vma_policy(vma) && |
2912 | can_vma_merge_after(vma, flags, NULL, NULL, | |
2913 | addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) { | |
92fed820 | 2914 | if (vma_iter_prealloc(vmi)) |
675eaca1 | 2915 | goto unacct_fail; |
28c5609f LH |
2916 | |
2917 | vma_adjust_trans_huge(vma, vma->vm_start, addr + len, 0); | |
287051b1 LH |
2918 | init_vma_prep(&vp, vma); |
2919 | vma_prepare(&vp); | |
2e7ce7d3 | 2920 | vma->vm_end = addr + len; |
1c71222e | 2921 | vm_flags_set(vma, VM_SOFTDIRTY); |
92fed820 | 2922 | vma_iter_store(vmi, vma); |
2e7ce7d3 | 2923 | |
287051b1 | 2924 | vma_complete(&vp, vmi, mm); |
2e7ce7d3 LH |
2925 | khugepaged_enter_vma(vma, flags); |
2926 | goto out; | |
1da177e4 | 2927 | } |
2e7ce7d3 LH |
2928 | |
2929 | /* create a vma struct for an anonymous mapping */ | |
2930 | vma = vm_area_alloc(mm); | |
2931 | if (!vma) | |
675eaca1 | 2932 | goto unacct_fail; |
1da177e4 | 2933 | |
bfd40eaf | 2934 | vma_set_anonymous(vma); |
1da177e4 LT |
2935 | vma->vm_start = addr; |
2936 | vma->vm_end = addr + len; | |
2e7ce7d3 | 2937 | vma->vm_pgoff = addr >> PAGE_SHIFT; |
1c71222e | 2938 | vm_flags_init(vma, flags); |
3ed75eb8 | 2939 | vma->vm_page_prot = vm_get_page_prot(flags); |
92fed820 | 2940 | if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) |
2e7ce7d3 | 2941 | goto mas_store_fail; |
d4af56c5 | 2942 | |
2e7ce7d3 | 2943 | mm->map_count++; |
1da177e4 | 2944 | out: |
3af9e859 | 2945 | perf_event_mmap(vma); |
1da177e4 | 2946 | mm->total_vm += len >> PAGE_SHIFT; |
84638335 | 2947 | mm->data_vm += len >> PAGE_SHIFT; |
128557ff ML |
2948 | if (flags & VM_LOCKED) |
2949 | mm->locked_vm += (len >> PAGE_SHIFT); | |
1c71222e | 2950 | vm_flags_set(vma, VM_SOFTDIRTY); |
763ecb03 | 2951 | validate_mm(mm); |
5d22fc25 | 2952 | return 0; |
d4af56c5 | 2953 | |
2e7ce7d3 | 2954 | mas_store_fail: |
d4af56c5 | 2955 | vm_area_free(vma); |
675eaca1 | 2956 | unacct_fail: |
2e7ce7d3 LH |
2957 | vm_unacct_memory(len >> PAGE_SHIFT); |
2958 | return -ENOMEM; | |
1da177e4 LT |
2959 | } |
2960 | ||
bb177a73 | 2961 | int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags) |
e4eb1ff6 LT |
2962 | { |
2963 | struct mm_struct *mm = current->mm; | |
2e7ce7d3 | 2964 | struct vm_area_struct *vma = NULL; |
bb177a73 | 2965 | unsigned long len; |
5d22fc25 | 2966 | int ret; |
128557ff | 2967 | bool populate; |
897ab3e0 | 2968 | LIST_HEAD(uf); |
92fed820 | 2969 | VMA_ITERATOR(vmi, mm, addr); |
e4eb1ff6 | 2970 | |
bb177a73 MH |
2971 | len = PAGE_ALIGN(request); |
2972 | if (len < request) | |
2973 | return -ENOMEM; | |
2974 | if (!len) | |
2975 | return 0; | |
2976 | ||
d8ed45c5 | 2977 | if (mmap_write_lock_killable(mm)) |
2d6c9282 MH |
2978 | return -EINTR; |
2979 | ||
2e7ce7d3 LH |
2980 | /* Until we need other flags, refuse anything except VM_EXEC. */ |
2981 | if ((flags & (~VM_EXEC)) != 0) | |
2982 | return -EINVAL; | |
2983 | ||
2984 | ret = check_brk_limits(addr, len); | |
2985 | if (ret) | |
2986 | goto limits_failed; | |
2987 | ||
183654ce | 2988 | ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0); |
2e7ce7d3 LH |
2989 | if (ret) |
2990 | goto munmap_failed; | |
2991 | ||
92fed820 LH |
2992 | vma = vma_prev(&vmi); |
2993 | ret = do_brk_flags(&vmi, vma, addr, len, flags); | |
128557ff | 2994 | populate = ((mm->def_flags & VM_LOCKED) != 0); |
d8ed45c5 | 2995 | mmap_write_unlock(mm); |
897ab3e0 | 2996 | userfaultfd_unmap_complete(mm, &uf); |
5d22fc25 | 2997 | if (populate && !ret) |
128557ff | 2998 | mm_populate(addr, len); |
e4eb1ff6 | 2999 | return ret; |
2e7ce7d3 LH |
3000 | |
3001 | munmap_failed: | |
3002 | limits_failed: | |
3003 | mmap_write_unlock(mm); | |
3004 | return ret; | |
e4eb1ff6 | 3005 | } |
16e72e9b DV |
3006 | EXPORT_SYMBOL(vm_brk_flags); |
3007 | ||
3008 | int vm_brk(unsigned long addr, unsigned long len) | |
3009 | { | |
3010 | return vm_brk_flags(addr, len, 0); | |
3011 | } | |
e4eb1ff6 | 3012 | EXPORT_SYMBOL(vm_brk); |
1da177e4 LT |
3013 | |
3014 | /* Release all mmaps. */ | |
3015 | void exit_mmap(struct mm_struct *mm) | |
3016 | { | |
d16dfc55 | 3017 | struct mmu_gather tlb; |
ba470de4 | 3018 | struct vm_area_struct *vma; |
1da177e4 | 3019 | unsigned long nr_accounted = 0; |
763ecb03 LH |
3020 | MA_STATE(mas, &mm->mm_mt, 0, 0); |
3021 | int count = 0; | |
1da177e4 | 3022 | |
d6dd61c8 | 3023 | /* mm's last user has gone, and its about to be pulled down */ |
cddb8a5c | 3024 | mmu_notifier_release(mm); |
d6dd61c8 | 3025 | |
bf3980c8 | 3026 | mmap_read_lock(mm); |
9480c53e JF |
3027 | arch_exit_mmap(mm); |
3028 | ||
763ecb03 | 3029 | vma = mas_find(&mas, ULONG_MAX); |
64591e86 SB |
3030 | if (!vma) { |
3031 | /* Can happen if dup_mmap() received an OOM */ | |
bf3980c8 | 3032 | mmap_read_unlock(mm); |
9480c53e | 3033 | return; |
64591e86 | 3034 | } |
9480c53e | 3035 | |
1da177e4 | 3036 | lru_add_drain(); |
1da177e4 | 3037 | flush_cache_mm(mm); |
d8b45053 | 3038 | tlb_gather_mmu_fullmm(&tlb, mm); |
901608d9 | 3039 | /* update_hiwater_rss(mm) here? but nobody should be looking */ |
763ecb03 | 3040 | /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */ |
68f48381 | 3041 | unmap_vmas(&tlb, &mm->mm_mt, vma, 0, ULONG_MAX, false); |
bf3980c8 SB |
3042 | mmap_read_unlock(mm); |
3043 | ||
3044 | /* | |
3045 | * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper | |
b3541d91 | 3046 | * because the memory has been already freed. |
bf3980c8 SB |
3047 | */ |
3048 | set_bit(MMF_OOM_SKIP, &mm->flags); | |
3049 | mmap_write_lock(mm); | |
763ecb03 LH |
3050 | free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, |
3051 | USER_PGTABLES_CEILING); | |
ae8eba8b | 3052 | tlb_finish_mmu(&tlb); |
1da177e4 | 3053 | |
763ecb03 LH |
3054 | /* |
3055 | * Walk the list again, actually closing and freeing it, with preemption | |
3056 | * enabled, without holding any MM locks besides the unreachable | |
3057 | * mmap_write_lock. | |
3058 | */ | |
3059 | do { | |
4f74d2c8 LT |
3060 | if (vma->vm_flags & VM_ACCOUNT) |
3061 | nr_accounted += vma_pages(vma); | |
763ecb03 LH |
3062 | remove_vma(vma); |
3063 | count++; | |
0a3b3c25 | 3064 | cond_resched(); |
763ecb03 LH |
3065 | } while ((vma = mas_find(&mas, ULONG_MAX)) != NULL); |
3066 | ||
3067 | BUG_ON(count != mm->map_count); | |
d4af56c5 LH |
3068 | |
3069 | trace_exit_mmap(mm); | |
3070 | __mt_destroy(&mm->mm_mt); | |
64591e86 | 3071 | mmap_write_unlock(mm); |
4f74d2c8 | 3072 | vm_unacct_memory(nr_accounted); |
1da177e4 LT |
3073 | } |
3074 | ||
3075 | /* Insert vm structure into process list sorted by address | |
3076 | * and into the inode's i_mmap tree. If vm_file is non-NULL | |
c8c06efa | 3077 | * then i_mmap_rwsem is taken here. |
1da177e4 | 3078 | */ |
6597d783 | 3079 | int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) |
1da177e4 | 3080 | { |
d4af56c5 | 3081 | unsigned long charged = vma_pages(vma); |
1da177e4 | 3082 | |
d4af56c5 | 3083 | |
d0601a50 | 3084 | if (find_vma_intersection(mm, vma->vm_start, vma->vm_end)) |
c9d13f5f | 3085 | return -ENOMEM; |
d4af56c5 | 3086 | |
c9d13f5f | 3087 | if ((vma->vm_flags & VM_ACCOUNT) && |
d4af56c5 | 3088 | security_vm_enough_memory_mm(mm, charged)) |
c9d13f5f CG |
3089 | return -ENOMEM; |
3090 | ||
1da177e4 LT |
3091 | /* |
3092 | * The vm_pgoff of a purely anonymous vma should be irrelevant | |
3093 | * until its first write fault, when page's anon_vma and index | |
3094 | * are set. But now set the vm_pgoff it will almost certainly | |
3095 | * end up with (unless mremap moves it elsewhere before that | |
3096 | * first wfault), so /proc/pid/maps tells a consistent story. | |
3097 | * | |
3098 | * By setting it to reflect the virtual start address of the | |
3099 | * vma, merges and splits can happen in a seamless way, just | |
3100 | * using the existing file pgoff checks and manipulations. | |
8332326e | 3101 | * Similarly in do_mmap and in do_brk_flags. |
1da177e4 | 3102 | */ |
8a9cc3b5 | 3103 | if (vma_is_anonymous(vma)) { |
1da177e4 LT |
3104 | BUG_ON(vma->anon_vma); |
3105 | vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; | |
3106 | } | |
2b144498 | 3107 | |
763ecb03 | 3108 | if (vma_link(mm, vma)) { |
d4af56c5 LH |
3109 | vm_unacct_memory(charged); |
3110 | return -ENOMEM; | |
3111 | } | |
3112 | ||
1da177e4 LT |
3113 | return 0; |
3114 | } | |
3115 | ||
3116 | /* | |
3117 | * Copy the vma structure to a new location in the same mm, | |
3118 | * prior to moving page table entries, to effect an mremap move. | |
3119 | */ | |
3120 | struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, | |
38a76013 ML |
3121 | unsigned long addr, unsigned long len, pgoff_t pgoff, |
3122 | bool *need_rmap_locks) | |
1da177e4 LT |
3123 | { |
3124 | struct vm_area_struct *vma = *vmap; | |
3125 | unsigned long vma_start = vma->vm_start; | |
3126 | struct mm_struct *mm = vma->vm_mm; | |
3127 | struct vm_area_struct *new_vma, *prev; | |
948f017b | 3128 | bool faulted_in_anon_vma = true; |
076f16bf | 3129 | VMA_ITERATOR(vmi, mm, addr); |
1da177e4 | 3130 | |
d4af56c5 | 3131 | validate_mm_mt(mm); |
1da177e4 LT |
3132 | /* |
3133 | * If anonymous vma has not yet been faulted, update new pgoff | |
3134 | * to match new location, to increase its chance of merging. | |
3135 | */ | |
ce75799b | 3136 | if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) { |
1da177e4 | 3137 | pgoff = addr >> PAGE_SHIFT; |
948f017b AA |
3138 | faulted_in_anon_vma = false; |
3139 | } | |
1da177e4 | 3140 | |
763ecb03 LH |
3141 | new_vma = find_vma_prev(mm, addr, &prev); |
3142 | if (new_vma && new_vma->vm_start < addr + len) | |
6597d783 | 3143 | return NULL; /* should never get here */ |
524e00b3 | 3144 | |
9760ebff | 3145 | new_vma = vma_merge(&vmi, mm, prev, addr, addr + len, vma->vm_flags, |
19a809af | 3146 | vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), |
5c26f6ac | 3147 | vma->vm_userfaultfd_ctx, anon_vma_name(vma)); |
1da177e4 LT |
3148 | if (new_vma) { |
3149 | /* | |
3150 | * Source vma may have been merged into new_vma | |
3151 | */ | |
948f017b AA |
3152 | if (unlikely(vma_start >= new_vma->vm_start && |
3153 | vma_start < new_vma->vm_end)) { | |
3154 | /* | |
3155 | * The only way we can get a vma_merge with | |
3156 | * self during an mremap is if the vma hasn't | |
3157 | * been faulted in yet and we were allowed to | |
3158 | * reset the dst vma->vm_pgoff to the | |
3159 | * destination address of the mremap to allow | |
3160 | * the merge to happen. mremap must change the | |
3161 | * vm_pgoff linearity between src and dst vmas | |
3162 | * (in turn preventing a vma_merge) to be | |
3163 | * safe. It is only safe to keep the vm_pgoff | |
3164 | * linear if there are no pages mapped yet. | |
3165 | */ | |
81d1b09c | 3166 | VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma); |
38a76013 | 3167 | *vmap = vma = new_vma; |
108d6642 | 3168 | } |
38a76013 | 3169 | *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); |
1da177e4 | 3170 | } else { |
3928d4f5 | 3171 | new_vma = vm_area_dup(vma); |
e3975891 CG |
3172 | if (!new_vma) |
3173 | goto out; | |
e3975891 CG |
3174 | new_vma->vm_start = addr; |
3175 | new_vma->vm_end = addr + len; | |
3176 | new_vma->vm_pgoff = pgoff; | |
3177 | if (vma_dup_policy(vma, new_vma)) | |
3178 | goto out_free_vma; | |
e3975891 CG |
3179 | if (anon_vma_clone(new_vma, vma)) |
3180 | goto out_free_mempol; | |
3181 | if (new_vma->vm_file) | |
3182 | get_file(new_vma->vm_file); | |
3183 | if (new_vma->vm_ops && new_vma->vm_ops->open) | |
3184 | new_vma->vm_ops->open(new_vma); | |
763ecb03 | 3185 | if (vma_link(mm, new_vma)) |
524e00b3 | 3186 | goto out_vma_link; |
e3975891 | 3187 | *need_rmap_locks = false; |
1da177e4 | 3188 | } |
d4af56c5 | 3189 | validate_mm_mt(mm); |
1da177e4 | 3190 | return new_vma; |
5beb4930 | 3191 | |
524e00b3 LH |
3192 | out_vma_link: |
3193 | if (new_vma->vm_ops && new_vma->vm_ops->close) | |
3194 | new_vma->vm_ops->close(new_vma); | |
92b73996 LH |
3195 | |
3196 | if (new_vma->vm_file) | |
3197 | fput(new_vma->vm_file); | |
3198 | ||
3199 | unlink_anon_vmas(new_vma); | |
e3975891 | 3200 | out_free_mempol: |
ef0855d3 | 3201 | mpol_put(vma_policy(new_vma)); |
e3975891 | 3202 | out_free_vma: |
3928d4f5 | 3203 | vm_area_free(new_vma); |
e3975891 | 3204 | out: |
d4af56c5 | 3205 | validate_mm_mt(mm); |
5beb4930 | 3206 | return NULL; |
1da177e4 | 3207 | } |
119f657c | 3208 | |
3209 | /* | |
3210 | * Return true if the calling process may expand its vm space by the passed | |
3211 | * number of pages | |
3212 | */ | |
84638335 | 3213 | bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages) |
119f657c | 3214 | { |
84638335 KK |
3215 | if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT) |
3216 | return false; | |
119f657c | 3217 | |
d977d56c KK |
3218 | if (is_data_mapping(flags) && |
3219 | mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) { | |
f4fcd558 KK |
3220 | /* Workaround for Valgrind */ |
3221 | if (rlimit(RLIMIT_DATA) == 0 && | |
3222 | mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT) | |
3223 | return true; | |
57a7702b DW |
3224 | |
3225 | pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n", | |
3226 | current->comm, current->pid, | |
3227 | (mm->data_vm + npages) << PAGE_SHIFT, | |
3228 | rlimit(RLIMIT_DATA), | |
3229 | ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data"); | |
3230 | ||
3231 | if (!ignore_rlimit_data) | |
d977d56c KK |
3232 | return false; |
3233 | } | |
119f657c | 3234 | |
84638335 KK |
3235 | return true; |
3236 | } | |
3237 | ||
3238 | void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages) | |
3239 | { | |
7866076b | 3240 | WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages); |
84638335 | 3241 | |
d977d56c | 3242 | if (is_exec_mapping(flags)) |
84638335 | 3243 | mm->exec_vm += npages; |
d977d56c | 3244 | else if (is_stack_mapping(flags)) |
84638335 | 3245 | mm->stack_vm += npages; |
d977d56c | 3246 | else if (is_data_mapping(flags)) |
84638335 | 3247 | mm->data_vm += npages; |
119f657c | 3248 | } |
fa5dc22f | 3249 | |
b3ec9f33 | 3250 | static vm_fault_t special_mapping_fault(struct vm_fault *vmf); |
a62c34bd AL |
3251 | |
3252 | /* | |
3253 | * Having a close hook prevents vma merging regardless of flags. | |
3254 | */ | |
3255 | static void special_mapping_close(struct vm_area_struct *vma) | |
3256 | { | |
3257 | } | |
3258 | ||
3259 | static const char *special_mapping_name(struct vm_area_struct *vma) | |
3260 | { | |
3261 | return ((struct vm_special_mapping *)vma->vm_private_data)->name; | |
3262 | } | |
3263 | ||
14d07113 | 3264 | static int special_mapping_mremap(struct vm_area_struct *new_vma) |
b059a453 DS |
3265 | { |
3266 | struct vm_special_mapping *sm = new_vma->vm_private_data; | |
3267 | ||
280e87e9 DS |
3268 | if (WARN_ON_ONCE(current->mm != new_vma->vm_mm)) |
3269 | return -EFAULT; | |
3270 | ||
b059a453 DS |
3271 | if (sm->mremap) |
3272 | return sm->mremap(sm, new_vma); | |
280e87e9 | 3273 | |
b059a453 DS |
3274 | return 0; |
3275 | } | |
3276 | ||
871402e0 DS |
3277 | static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr) |
3278 | { | |
3279 | /* | |
3280 | * Forbid splitting special mappings - kernel has expectations over | |
3281 | * the number of pages in mapping. Together with VM_DONTEXPAND | |
3282 | * the size of vma should stay the same over the special mapping's | |
3283 | * lifetime. | |
3284 | */ | |
3285 | return -EINVAL; | |
3286 | } | |
3287 | ||
a62c34bd AL |
3288 | static const struct vm_operations_struct special_mapping_vmops = { |
3289 | .close = special_mapping_close, | |
3290 | .fault = special_mapping_fault, | |
b059a453 | 3291 | .mremap = special_mapping_mremap, |
a62c34bd | 3292 | .name = special_mapping_name, |
af34ebeb DS |
3293 | /* vDSO code relies that VVAR can't be accessed remotely */ |
3294 | .access = NULL, | |
871402e0 | 3295 | .may_split = special_mapping_split, |
a62c34bd AL |
3296 | }; |
3297 | ||
3298 | static const struct vm_operations_struct legacy_special_mapping_vmops = { | |
3299 | .close = special_mapping_close, | |
3300 | .fault = special_mapping_fault, | |
3301 | }; | |
fa5dc22f | 3302 | |
b3ec9f33 | 3303 | static vm_fault_t special_mapping_fault(struct vm_fault *vmf) |
fa5dc22f | 3304 | { |
11bac800 | 3305 | struct vm_area_struct *vma = vmf->vma; |
b1d0e4f5 | 3306 | pgoff_t pgoff; |
fa5dc22f RM |
3307 | struct page **pages; |
3308 | ||
f872f540 | 3309 | if (vma->vm_ops == &legacy_special_mapping_vmops) { |
a62c34bd | 3310 | pages = vma->vm_private_data; |
f872f540 AL |
3311 | } else { |
3312 | struct vm_special_mapping *sm = vma->vm_private_data; | |
3313 | ||
3314 | if (sm->fault) | |
11bac800 | 3315 | return sm->fault(sm, vmf->vma, vmf); |
f872f540 AL |
3316 | |
3317 | pages = sm->pages; | |
3318 | } | |
a62c34bd | 3319 | |
8a9cc3b5 | 3320 | for (pgoff = vmf->pgoff; pgoff && *pages; ++pages) |
b1d0e4f5 | 3321 | pgoff--; |
fa5dc22f RM |
3322 | |
3323 | if (*pages) { | |
3324 | struct page *page = *pages; | |
3325 | get_page(page); | |
b1d0e4f5 NP |
3326 | vmf->page = page; |
3327 | return 0; | |
fa5dc22f RM |
3328 | } |
3329 | ||
b1d0e4f5 | 3330 | return VM_FAULT_SIGBUS; |
fa5dc22f RM |
3331 | } |
3332 | ||
a62c34bd AL |
3333 | static struct vm_area_struct *__install_special_mapping( |
3334 | struct mm_struct *mm, | |
3335 | unsigned long addr, unsigned long len, | |
27f28b97 CG |
3336 | unsigned long vm_flags, void *priv, |
3337 | const struct vm_operations_struct *ops) | |
fa5dc22f | 3338 | { |
462e635e | 3339 | int ret; |
fa5dc22f RM |
3340 | struct vm_area_struct *vma; |
3341 | ||
d4af56c5 | 3342 | validate_mm_mt(mm); |
490fc053 | 3343 | vma = vm_area_alloc(mm); |
fa5dc22f | 3344 | if (unlikely(vma == NULL)) |
3935ed6a | 3345 | return ERR_PTR(-ENOMEM); |
fa5dc22f | 3346 | |
fa5dc22f RM |
3347 | vma->vm_start = addr; |
3348 | vma->vm_end = addr + len; | |
3349 | ||
e430a95a SB |
3350 | vm_flags_init(vma, (vm_flags | mm->def_flags | |
3351 | VM_DONTEXPAND | VM_SOFTDIRTY) & ~VM_LOCKED_MASK); | |
3ed75eb8 | 3352 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
fa5dc22f | 3353 | |
a62c34bd AL |
3354 | vma->vm_ops = ops; |
3355 | vma->vm_private_data = priv; | |
fa5dc22f | 3356 | |
462e635e TO |
3357 | ret = insert_vm_struct(mm, vma); |
3358 | if (ret) | |
3359 | goto out; | |
fa5dc22f | 3360 | |
84638335 | 3361 | vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT); |
fa5dc22f | 3362 | |
cdd6c482 | 3363 | perf_event_mmap(vma); |
089dd79d | 3364 | |
d4af56c5 | 3365 | validate_mm_mt(mm); |
3935ed6a | 3366 | return vma; |
462e635e TO |
3367 | |
3368 | out: | |
3928d4f5 | 3369 | vm_area_free(vma); |
d4af56c5 | 3370 | validate_mm_mt(mm); |
3935ed6a SS |
3371 | return ERR_PTR(ret); |
3372 | } | |
3373 | ||
2eefd878 DS |
3374 | bool vma_is_special_mapping(const struct vm_area_struct *vma, |
3375 | const struct vm_special_mapping *sm) | |
3376 | { | |
3377 | return vma->vm_private_data == sm && | |
3378 | (vma->vm_ops == &special_mapping_vmops || | |
3379 | vma->vm_ops == &legacy_special_mapping_vmops); | |
3380 | } | |
3381 | ||
a62c34bd | 3382 | /* |
c1e8d7c6 | 3383 | * Called with mm->mmap_lock held for writing. |
a62c34bd AL |
3384 | * Insert a new vma covering the given region, with the given flags. |
3385 | * Its pages are supplied by the given array of struct page *. | |
3386 | * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. | |
3387 | * The region past the last page supplied will always produce SIGBUS. | |
3388 | * The array pointer and the pages it points to are assumed to stay alive | |
3389 | * for as long as this mapping might exist. | |
3390 | */ | |
3391 | struct vm_area_struct *_install_special_mapping( | |
3392 | struct mm_struct *mm, | |
3393 | unsigned long addr, unsigned long len, | |
3394 | unsigned long vm_flags, const struct vm_special_mapping *spec) | |
3395 | { | |
27f28b97 CG |
3396 | return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec, |
3397 | &special_mapping_vmops); | |
a62c34bd AL |
3398 | } |
3399 | ||
3935ed6a SS |
3400 | int install_special_mapping(struct mm_struct *mm, |
3401 | unsigned long addr, unsigned long len, | |
3402 | unsigned long vm_flags, struct page **pages) | |
3403 | { | |
a62c34bd | 3404 | struct vm_area_struct *vma = __install_special_mapping( |
27f28b97 CG |
3405 | mm, addr, len, vm_flags, (void *)pages, |
3406 | &legacy_special_mapping_vmops); | |
3935ed6a | 3407 | |
14bd5b45 | 3408 | return PTR_ERR_OR_ZERO(vma); |
fa5dc22f | 3409 | } |
7906d00c AA |
3410 | |
3411 | static DEFINE_MUTEX(mm_all_locks_mutex); | |
3412 | ||
454ed842 | 3413 | static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) |
7906d00c | 3414 | { |
f808c13f | 3415 | if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { |
7906d00c AA |
3416 | /* |
3417 | * The LSB of head.next can't change from under us | |
3418 | * because we hold the mm_all_locks_mutex. | |
3419 | */ | |
da1c55f1 | 3420 | down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock); |
7906d00c AA |
3421 | /* |
3422 | * We can safely modify head.next after taking the | |
5a505085 | 3423 | * anon_vma->root->rwsem. If some other vma in this mm shares |
7906d00c AA |
3424 | * the same anon_vma we won't take it again. |
3425 | * | |
3426 | * No need of atomic instructions here, head.next | |
3427 | * can't change from under us thanks to the | |
5a505085 | 3428 | * anon_vma->root->rwsem. |
7906d00c AA |
3429 | */ |
3430 | if (__test_and_set_bit(0, (unsigned long *) | |
f808c13f | 3431 | &anon_vma->root->rb_root.rb_root.rb_node)) |
7906d00c AA |
3432 | BUG(); |
3433 | } | |
3434 | } | |
3435 | ||
454ed842 | 3436 | static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) |
7906d00c AA |
3437 | { |
3438 | if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { | |
3439 | /* | |
3440 | * AS_MM_ALL_LOCKS can't change from under us because | |
3441 | * we hold the mm_all_locks_mutex. | |
3442 | * | |
3443 | * Operations on ->flags have to be atomic because | |
3444 | * even if AS_MM_ALL_LOCKS is stable thanks to the | |
3445 | * mm_all_locks_mutex, there may be other cpus | |
3446 | * changing other bitflags in parallel to us. | |
3447 | */ | |
3448 | if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) | |
3449 | BUG(); | |
da1c55f1 | 3450 | down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock); |
7906d00c AA |
3451 | } |
3452 | } | |
3453 | ||
3454 | /* | |
3455 | * This operation locks against the VM for all pte/vma/mm related | |
3456 | * operations that could ever happen on a certain mm. This includes | |
3457 | * vmtruncate, try_to_unmap, and all page faults. | |
3458 | * | |
c1e8d7c6 | 3459 | * The caller must take the mmap_lock in write mode before calling |
7906d00c | 3460 | * mm_take_all_locks(). The caller isn't allowed to release the |
c1e8d7c6 | 3461 | * mmap_lock until mm_drop_all_locks() returns. |
7906d00c | 3462 | * |
c1e8d7c6 | 3463 | * mmap_lock in write mode is required in order to block all operations |
7906d00c | 3464 | * that could modify pagetables and free pages without need of |
27ba0644 | 3465 | * altering the vma layout. It's also needed in write mode to avoid new |
7906d00c AA |
3466 | * anon_vmas to be associated with existing vmas. |
3467 | * | |
3468 | * A single task can't take more than one mm_take_all_locks() in a row | |
3469 | * or it would deadlock. | |
3470 | * | |
bf181b9f | 3471 | * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in |
7906d00c AA |
3472 | * mapping->flags avoid to take the same lock twice, if more than one |
3473 | * vma in this mm is backed by the same anon_vma or address_space. | |
3474 | * | |
88f306b6 KS |
3475 | * We take locks in following order, accordingly to comment at beginning |
3476 | * of mm/rmap.c: | |
3477 | * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for | |
3478 | * hugetlb mapping); | |
3479 | * - all i_mmap_rwsem locks; | |
3480 | * - all anon_vma->rwseml | |
3481 | * | |
3482 | * We can take all locks within these types randomly because the VM code | |
3483 | * doesn't nest them and we protected from parallel mm_take_all_locks() by | |
3484 | * mm_all_locks_mutex. | |
7906d00c AA |
3485 | * |
3486 | * mm_take_all_locks() and mm_drop_all_locks are expensive operations | |
3487 | * that may have to take thousand of locks. | |
3488 | * | |
3489 | * mm_take_all_locks() can fail if it's interrupted by signals. | |
3490 | */ | |
3491 | int mm_take_all_locks(struct mm_struct *mm) | |
3492 | { | |
3493 | struct vm_area_struct *vma; | |
5beb4930 | 3494 | struct anon_vma_chain *avc; |
763ecb03 | 3495 | MA_STATE(mas, &mm->mm_mt, 0, 0); |
7906d00c | 3496 | |
325bca1f | 3497 | mmap_assert_write_locked(mm); |
7906d00c AA |
3498 | |
3499 | mutex_lock(&mm_all_locks_mutex); | |
3500 | ||
763ecb03 | 3501 | mas_for_each(&mas, vma, ULONG_MAX) { |
7906d00c AA |
3502 | if (signal_pending(current)) |
3503 | goto out_unlock; | |
88f306b6 KS |
3504 | if (vma->vm_file && vma->vm_file->f_mapping && |
3505 | is_vm_hugetlb_page(vma)) | |
3506 | vm_lock_mapping(mm, vma->vm_file->f_mapping); | |
3507 | } | |
3508 | ||
763ecb03 LH |
3509 | mas_set(&mas, 0); |
3510 | mas_for_each(&mas, vma, ULONG_MAX) { | |
88f306b6 KS |
3511 | if (signal_pending(current)) |
3512 | goto out_unlock; | |
3513 | if (vma->vm_file && vma->vm_file->f_mapping && | |
3514 | !is_vm_hugetlb_page(vma)) | |
454ed842 | 3515 | vm_lock_mapping(mm, vma->vm_file->f_mapping); |
7906d00c | 3516 | } |
7cd5a02f | 3517 | |
763ecb03 LH |
3518 | mas_set(&mas, 0); |
3519 | mas_for_each(&mas, vma, ULONG_MAX) { | |
7cd5a02f PZ |
3520 | if (signal_pending(current)) |
3521 | goto out_unlock; | |
3522 | if (vma->anon_vma) | |
5beb4930 RR |
3523 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
3524 | vm_lock_anon_vma(mm, avc->anon_vma); | |
7906d00c | 3525 | } |
7cd5a02f | 3526 | |
584cff54 | 3527 | return 0; |
7906d00c AA |
3528 | |
3529 | out_unlock: | |
584cff54 KC |
3530 | mm_drop_all_locks(mm); |
3531 | return -EINTR; | |
7906d00c AA |
3532 | } |
3533 | ||
3534 | static void vm_unlock_anon_vma(struct anon_vma *anon_vma) | |
3535 | { | |
f808c13f | 3536 | if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) { |
7906d00c AA |
3537 | /* |
3538 | * The LSB of head.next can't change to 0 from under | |
3539 | * us because we hold the mm_all_locks_mutex. | |
3540 | * | |
3541 | * We must however clear the bitflag before unlocking | |
bf181b9f | 3542 | * the vma so the users using the anon_vma->rb_root will |
7906d00c AA |
3543 | * never see our bitflag. |
3544 | * | |
3545 | * No need of atomic instructions here, head.next | |
3546 | * can't change from under us until we release the | |
5a505085 | 3547 | * anon_vma->root->rwsem. |
7906d00c AA |
3548 | */ |
3549 | if (!__test_and_clear_bit(0, (unsigned long *) | |
f808c13f | 3550 | &anon_vma->root->rb_root.rb_root.rb_node)) |
7906d00c | 3551 | BUG(); |
08b52706 | 3552 | anon_vma_unlock_write(anon_vma); |
7906d00c AA |
3553 | } |
3554 | } | |
3555 | ||
3556 | static void vm_unlock_mapping(struct address_space *mapping) | |
3557 | { | |
3558 | if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { | |
3559 | /* | |
3560 | * AS_MM_ALL_LOCKS can't change to 0 from under us | |
3561 | * because we hold the mm_all_locks_mutex. | |
3562 | */ | |
83cde9e8 | 3563 | i_mmap_unlock_write(mapping); |
7906d00c AA |
3564 | if (!test_and_clear_bit(AS_MM_ALL_LOCKS, |
3565 | &mapping->flags)) | |
3566 | BUG(); | |
3567 | } | |
3568 | } | |
3569 | ||
3570 | /* | |
c1e8d7c6 | 3571 | * The mmap_lock cannot be released by the caller until |
7906d00c AA |
3572 | * mm_drop_all_locks() returns. |
3573 | */ | |
3574 | void mm_drop_all_locks(struct mm_struct *mm) | |
3575 | { | |
3576 | struct vm_area_struct *vma; | |
5beb4930 | 3577 | struct anon_vma_chain *avc; |
763ecb03 | 3578 | MA_STATE(mas, &mm->mm_mt, 0, 0); |
7906d00c | 3579 | |
325bca1f | 3580 | mmap_assert_write_locked(mm); |
7906d00c AA |
3581 | BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); |
3582 | ||
763ecb03 | 3583 | mas_for_each(&mas, vma, ULONG_MAX) { |
7906d00c | 3584 | if (vma->anon_vma) |
5beb4930 RR |
3585 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
3586 | vm_unlock_anon_vma(avc->anon_vma); | |
7906d00c AA |
3587 | if (vma->vm_file && vma->vm_file->f_mapping) |
3588 | vm_unlock_mapping(vma->vm_file->f_mapping); | |
3589 | } | |
3590 | ||
3591 | mutex_unlock(&mm_all_locks_mutex); | |
3592 | } | |
8feae131 DH |
3593 | |
3594 | /* | |
3edf41d8 | 3595 | * initialise the percpu counter for VM |
8feae131 DH |
3596 | */ |
3597 | void __init mmap_init(void) | |
3598 | { | |
00a62ce9 KM |
3599 | int ret; |
3600 | ||
908c7f19 | 3601 | ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL); |
00a62ce9 | 3602 | VM_BUG_ON(ret); |
8feae131 | 3603 | } |
c9b1d098 AS |
3604 | |
3605 | /* | |
3606 | * Initialise sysctl_user_reserve_kbytes. | |
3607 | * | |
3608 | * This is intended to prevent a user from starting a single memory hogging | |
3609 | * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER | |
3610 | * mode. | |
3611 | * | |
3612 | * The default value is min(3% of free memory, 128MB) | |
3613 | * 128MB is enough to recover with sshd/login, bash, and top/kill. | |
3614 | */ | |
1640879a | 3615 | static int init_user_reserve(void) |
c9b1d098 AS |
3616 | { |
3617 | unsigned long free_kbytes; | |
3618 | ||
c41f012a | 3619 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
c9b1d098 AS |
3620 | |
3621 | sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); | |
3622 | return 0; | |
3623 | } | |
a64fb3cd | 3624 | subsys_initcall(init_user_reserve); |
4eeab4f5 AS |
3625 | |
3626 | /* | |
3627 | * Initialise sysctl_admin_reserve_kbytes. | |
3628 | * | |
3629 | * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin | |
3630 | * to log in and kill a memory hogging process. | |
3631 | * | |
3632 | * Systems with more than 256MB will reserve 8MB, enough to recover | |
3633 | * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will | |
3634 | * only reserve 3% of free pages by default. | |
3635 | */ | |
1640879a | 3636 | static int init_admin_reserve(void) |
4eeab4f5 AS |
3637 | { |
3638 | unsigned long free_kbytes; | |
3639 | ||
c41f012a | 3640 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
4eeab4f5 AS |
3641 | |
3642 | sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); | |
3643 | return 0; | |
3644 | } | |
a64fb3cd | 3645 | subsys_initcall(init_admin_reserve); |
1640879a AS |
3646 | |
3647 | /* | |
3648 | * Reinititalise user and admin reserves if memory is added or removed. | |
3649 | * | |
3650 | * The default user reserve max is 128MB, and the default max for the | |
3651 | * admin reserve is 8MB. These are usually, but not always, enough to | |
3652 | * enable recovery from a memory hogging process using login/sshd, a shell, | |
3653 | * and tools like top. It may make sense to increase or even disable the | |
3654 | * reserve depending on the existence of swap or variations in the recovery | |
3655 | * tools. So, the admin may have changed them. | |
3656 | * | |
3657 | * If memory is added and the reserves have been eliminated or increased above | |
3658 | * the default max, then we'll trust the admin. | |
3659 | * | |
3660 | * If memory is removed and there isn't enough free memory, then we | |
3661 | * need to reset the reserves. | |
3662 | * | |
3663 | * Otherwise keep the reserve set by the admin. | |
3664 | */ | |
3665 | static int reserve_mem_notifier(struct notifier_block *nb, | |
3666 | unsigned long action, void *data) | |
3667 | { | |
3668 | unsigned long tmp, free_kbytes; | |
3669 | ||
3670 | switch (action) { | |
3671 | case MEM_ONLINE: | |
3672 | /* Default max is 128MB. Leave alone if modified by operator. */ | |
3673 | tmp = sysctl_user_reserve_kbytes; | |
3674 | if (0 < tmp && tmp < (1UL << 17)) | |
3675 | init_user_reserve(); | |
3676 | ||
3677 | /* Default max is 8MB. Leave alone if modified by operator. */ | |
3678 | tmp = sysctl_admin_reserve_kbytes; | |
3679 | if (0 < tmp && tmp < (1UL << 13)) | |
3680 | init_admin_reserve(); | |
3681 | ||
3682 | break; | |
3683 | case MEM_OFFLINE: | |
c41f012a | 3684 | free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); |
1640879a AS |
3685 | |
3686 | if (sysctl_user_reserve_kbytes > free_kbytes) { | |
3687 | init_user_reserve(); | |
3688 | pr_info("vm.user_reserve_kbytes reset to %lu\n", | |
3689 | sysctl_user_reserve_kbytes); | |
3690 | } | |
3691 | ||
3692 | if (sysctl_admin_reserve_kbytes > free_kbytes) { | |
3693 | init_admin_reserve(); | |
3694 | pr_info("vm.admin_reserve_kbytes reset to %lu\n", | |
3695 | sysctl_admin_reserve_kbytes); | |
3696 | } | |
3697 | break; | |
3698 | default: | |
3699 | break; | |
3700 | } | |
3701 | return NOTIFY_OK; | |
3702 | } | |
3703 | ||
1640879a AS |
3704 | static int __meminit init_reserve_notifier(void) |
3705 | { | |
1eeaa4fd | 3706 | if (hotplug_memory_notifier(reserve_mem_notifier, DEFAULT_CALLBACK_PRI)) |
b1de0d13 | 3707 | pr_err("Failed registering memory add/remove notifier for admin reserve\n"); |
1640879a AS |
3708 | |
3709 | return 0; | |
3710 | } | |
a64fb3cd | 3711 | subsys_initcall(init_reserve_notifier); |