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