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