Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * mm/mmap.c | |
3 | * | |
4 | * Written by obz. | |
5 | * | |
046c6884 | 6 | * Address space accounting code <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
7 | */ |
8 | ||
9 | #include <linux/slab.h> | |
4af3c9cc | 10 | #include <linux/backing-dev.h> |
1da177e4 LT |
11 | #include <linux/mm.h> |
12 | #include <linux/shm.h> | |
13 | #include <linux/mman.h> | |
14 | #include <linux/pagemap.h> | |
15 | #include <linux/swap.h> | |
16 | #include <linux/syscalls.h> | |
c59ede7b | 17 | #include <linux/capability.h> |
1da177e4 LT |
18 | #include <linux/init.h> |
19 | #include <linux/file.h> | |
20 | #include <linux/fs.h> | |
21 | #include <linux/personality.h> | |
22 | #include <linux/security.h> | |
23 | #include <linux/hugetlb.h> | |
24 | #include <linux/profile.h> | |
b95f1b31 | 25 | #include <linux/export.h> |
1da177e4 LT |
26 | #include <linux/mount.h> |
27 | #include <linux/mempolicy.h> | |
28 | #include <linux/rmap.h> | |
cddb8a5c | 29 | #include <linux/mmu_notifier.h> |
cdd6c482 | 30 | #include <linux/perf_event.h> |
120a795d | 31 | #include <linux/audit.h> |
b15d00b6 | 32 | #include <linux/khugepaged.h> |
2b144498 | 33 | #include <linux/uprobes.h> |
d3737187 | 34 | #include <linux/rbtree_augmented.h> |
cf4aebc2 | 35 | #include <linux/sched/sysctl.h> |
1da177e4 LT |
36 | |
37 | #include <asm/uaccess.h> | |
38 | #include <asm/cacheflush.h> | |
39 | #include <asm/tlb.h> | |
d6dd61c8 | 40 | #include <asm/mmu_context.h> |
1da177e4 | 41 | |
42b77728 JB |
42 | #include "internal.h" |
43 | ||
3a459756 KK |
44 | #ifndef arch_mmap_check |
45 | #define arch_mmap_check(addr, len, flags) (0) | |
46 | #endif | |
47 | ||
08e7d9b5 MS |
48 | #ifndef arch_rebalance_pgtables |
49 | #define arch_rebalance_pgtables(addr, len) (addr) | |
50 | #endif | |
51 | ||
e0da382c HD |
52 | static void unmap_region(struct mm_struct *mm, |
53 | struct vm_area_struct *vma, struct vm_area_struct *prev, | |
54 | unsigned long start, unsigned long end); | |
55 | ||
1da177e4 LT |
56 | /* description of effects of mapping type and prot in current implementation. |
57 | * this is due to the limited x86 page protection hardware. The expected | |
58 | * behavior is in parens: | |
59 | * | |
60 | * map_type prot | |
61 | * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC | |
62 | * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes | |
63 | * w: (no) no w: (no) no w: (yes) yes w: (no) no | |
64 | * x: (no) no x: (no) yes x: (no) yes x: (yes) yes | |
65 | * | |
66 | * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes | |
67 | * w: (no) no w: (no) no w: (copy) copy w: (no) no | |
68 | * x: (no) no x: (no) yes x: (no) yes x: (yes) yes | |
69 | * | |
70 | */ | |
71 | pgprot_t protection_map[16] = { | |
72 | __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111, | |
73 | __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111 | |
74 | }; | |
75 | ||
804af2cf HD |
76 | pgprot_t vm_get_page_prot(unsigned long vm_flags) |
77 | { | |
b845f313 DK |
78 | return __pgprot(pgprot_val(protection_map[vm_flags & |
79 | (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) | | |
80 | pgprot_val(arch_vm_get_page_prot(vm_flags))); | |
804af2cf HD |
81 | } |
82 | EXPORT_SYMBOL(vm_get_page_prot); | |
83 | ||
34679d7e SL |
84 | int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */ |
85 | int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */ | |
c3d8c141 | 86 | int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT; |
c9b1d098 | 87 | unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */ |
4eeab4f5 | 88 | unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */ |
34679d7e SL |
89 | /* |
90 | * Make sure vm_committed_as in one cacheline and not cacheline shared with | |
91 | * other variables. It can be updated by several CPUs frequently. | |
92 | */ | |
93 | struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp; | |
1da177e4 | 94 | |
997071bc S |
95 | /* |
96 | * The global memory commitment made in the system can be a metric | |
97 | * that can be used to drive ballooning decisions when Linux is hosted | |
98 | * as a guest. On Hyper-V, the host implements a policy engine for dynamically | |
99 | * balancing memory across competing virtual machines that are hosted. | |
100 | * Several metrics drive this policy engine including the guest reported | |
101 | * memory commitment. | |
102 | */ | |
103 | unsigned long vm_memory_committed(void) | |
104 | { | |
105 | return percpu_counter_read_positive(&vm_committed_as); | |
106 | } | |
107 | EXPORT_SYMBOL_GPL(vm_memory_committed); | |
108 | ||
1da177e4 LT |
109 | /* |
110 | * Check that a process has enough memory to allocate a new virtual | |
111 | * mapping. 0 means there is enough memory for the allocation to | |
112 | * succeed and -ENOMEM implies there is not. | |
113 | * | |
114 | * We currently support three overcommit policies, which are set via the | |
115 | * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting | |
116 | * | |
117 | * Strict overcommit modes added 2002 Feb 26 by Alan Cox. | |
118 | * Additional code 2002 Jul 20 by Robert Love. | |
119 | * | |
120 | * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. | |
121 | * | |
122 | * Note this is a helper function intended to be used by LSMs which | |
123 | * wish to use this logic. | |
124 | */ | |
34b4e4aa | 125 | int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin) |
1da177e4 | 126 | { |
c9b1d098 | 127 | unsigned long free, allowed, reserve; |
1da177e4 LT |
128 | |
129 | vm_acct_memory(pages); | |
130 | ||
131 | /* | |
132 | * Sometimes we want to use more memory than we have | |
133 | */ | |
134 | if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) | |
135 | return 0; | |
136 | ||
137 | if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { | |
c15bef30 DF |
138 | free = global_page_state(NR_FREE_PAGES); |
139 | free += global_page_state(NR_FILE_PAGES); | |
140 | ||
141 | /* | |
142 | * shmem pages shouldn't be counted as free in this | |
143 | * case, they can't be purged, only swapped out, and | |
144 | * that won't affect the overall amount of available | |
145 | * memory in the system. | |
146 | */ | |
147 | free -= global_page_state(NR_SHMEM); | |
1da177e4 | 148 | |
ec8acf20 | 149 | free += get_nr_swap_pages(); |
1da177e4 LT |
150 | |
151 | /* | |
152 | * Any slabs which are created with the | |
153 | * SLAB_RECLAIM_ACCOUNT flag claim to have contents | |
154 | * which are reclaimable, under pressure. The dentry | |
155 | * cache and most inode caches should fall into this | |
156 | */ | |
972d1a7b | 157 | free += global_page_state(NR_SLAB_RECLAIMABLE); |
1da177e4 | 158 | |
6d9f7839 HA |
159 | /* |
160 | * Leave reserved pages. The pages are not for anonymous pages. | |
161 | */ | |
c15bef30 | 162 | if (free <= totalreserve_pages) |
6d9f7839 HA |
163 | goto error; |
164 | else | |
c15bef30 | 165 | free -= totalreserve_pages; |
6d9f7839 HA |
166 | |
167 | /* | |
4eeab4f5 | 168 | * Reserve some for root |
6d9f7839 | 169 | */ |
1da177e4 | 170 | if (!cap_sys_admin) |
4eeab4f5 | 171 | free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10); |
1da177e4 LT |
172 | |
173 | if (free > pages) | |
174 | return 0; | |
6d9f7839 HA |
175 | |
176 | goto error; | |
1da177e4 LT |
177 | } |
178 | ||
179 | allowed = (totalram_pages - hugetlb_total_pages()) | |
180 | * sysctl_overcommit_ratio / 100; | |
181 | /* | |
4eeab4f5 | 182 | * Reserve some for root |
1da177e4 LT |
183 | */ |
184 | if (!cap_sys_admin) | |
4eeab4f5 | 185 | allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10); |
1da177e4 LT |
186 | allowed += total_swap_pages; |
187 | ||
c9b1d098 AS |
188 | /* |
189 | * Don't let a single process grow so big a user can't recover | |
190 | */ | |
191 | if (mm) { | |
192 | reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10); | |
193 | allowed -= min(mm->total_vm / 32, reserve); | |
194 | } | |
1da177e4 | 195 | |
00a62ce9 | 196 | if (percpu_counter_read_positive(&vm_committed_as) < allowed) |
1da177e4 | 197 | return 0; |
6d9f7839 | 198 | error: |
1da177e4 LT |
199 | vm_unacct_memory(pages); |
200 | ||
201 | return -ENOMEM; | |
202 | } | |
203 | ||
1da177e4 | 204 | /* |
3d48ae45 | 205 | * Requires inode->i_mapping->i_mmap_mutex |
1da177e4 LT |
206 | */ |
207 | static void __remove_shared_vm_struct(struct vm_area_struct *vma, | |
208 | struct file *file, struct address_space *mapping) | |
209 | { | |
210 | if (vma->vm_flags & VM_DENYWRITE) | |
496ad9aa | 211 | atomic_inc(&file_inode(file)->i_writecount); |
1da177e4 LT |
212 | if (vma->vm_flags & VM_SHARED) |
213 | mapping->i_mmap_writable--; | |
214 | ||
215 | flush_dcache_mmap_lock(mapping); | |
216 | if (unlikely(vma->vm_flags & VM_NONLINEAR)) | |
6b2dbba8 | 217 | list_del_init(&vma->shared.nonlinear); |
1da177e4 | 218 | else |
6b2dbba8 | 219 | vma_interval_tree_remove(vma, &mapping->i_mmap); |
1da177e4 LT |
220 | flush_dcache_mmap_unlock(mapping); |
221 | } | |
222 | ||
223 | /* | |
6b2dbba8 | 224 | * Unlink a file-based vm structure from its interval tree, to hide |
a8fb5618 | 225 | * vma from rmap and vmtruncate before freeing its page tables. |
1da177e4 | 226 | */ |
a8fb5618 | 227 | void unlink_file_vma(struct vm_area_struct *vma) |
1da177e4 LT |
228 | { |
229 | struct file *file = vma->vm_file; | |
230 | ||
1da177e4 LT |
231 | if (file) { |
232 | struct address_space *mapping = file->f_mapping; | |
3d48ae45 | 233 | mutex_lock(&mapping->i_mmap_mutex); |
1da177e4 | 234 | __remove_shared_vm_struct(vma, file, mapping); |
3d48ae45 | 235 | mutex_unlock(&mapping->i_mmap_mutex); |
1da177e4 | 236 | } |
a8fb5618 HD |
237 | } |
238 | ||
239 | /* | |
240 | * Close a vm structure and free it, returning the next. | |
241 | */ | |
242 | static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) | |
243 | { | |
244 | struct vm_area_struct *next = vma->vm_next; | |
245 | ||
a8fb5618 | 246 | might_sleep(); |
1da177e4 LT |
247 | if (vma->vm_ops && vma->vm_ops->close) |
248 | vma->vm_ops->close(vma); | |
e9714acf | 249 | if (vma->vm_file) |
a8fb5618 | 250 | fput(vma->vm_file); |
f0be3d32 | 251 | mpol_put(vma_policy(vma)); |
1da177e4 | 252 | kmem_cache_free(vm_area_cachep, vma); |
a8fb5618 | 253 | return next; |
1da177e4 LT |
254 | } |
255 | ||
e4eb1ff6 LT |
256 | static unsigned long do_brk(unsigned long addr, unsigned long len); |
257 | ||
6a6160a7 | 258 | SYSCALL_DEFINE1(brk, unsigned long, brk) |
1da177e4 LT |
259 | { |
260 | unsigned long rlim, retval; | |
261 | unsigned long newbrk, oldbrk; | |
262 | struct mm_struct *mm = current->mm; | |
a5b4592c | 263 | unsigned long min_brk; |
128557ff | 264 | bool populate; |
1da177e4 LT |
265 | |
266 | down_write(&mm->mmap_sem); | |
267 | ||
a5b4592c | 268 | #ifdef CONFIG_COMPAT_BRK |
5520e894 JK |
269 | /* |
270 | * CONFIG_COMPAT_BRK can still be overridden by setting | |
271 | * randomize_va_space to 2, which will still cause mm->start_brk | |
272 | * to be arbitrarily shifted | |
273 | */ | |
4471a675 | 274 | if (current->brk_randomized) |
5520e894 JK |
275 | min_brk = mm->start_brk; |
276 | else | |
277 | min_brk = mm->end_data; | |
a5b4592c JK |
278 | #else |
279 | min_brk = mm->start_brk; | |
280 | #endif | |
281 | if (brk < min_brk) | |
1da177e4 | 282 | goto out; |
1e624196 RG |
283 | |
284 | /* | |
285 | * Check against rlimit here. If this check is done later after the test | |
286 | * of oldbrk with newbrk then it can escape the test and let the data | |
287 | * segment grow beyond its set limit the in case where the limit is | |
288 | * not page aligned -Ram Gupta | |
289 | */ | |
59e99e5b | 290 | rlim = rlimit(RLIMIT_DATA); |
c1d171a0 JK |
291 | if (rlim < RLIM_INFINITY && (brk - mm->start_brk) + |
292 | (mm->end_data - mm->start_data) > rlim) | |
1e624196 RG |
293 | goto out; |
294 | ||
1da177e4 LT |
295 | newbrk = PAGE_ALIGN(brk); |
296 | oldbrk = PAGE_ALIGN(mm->brk); | |
297 | if (oldbrk == newbrk) | |
298 | goto set_brk; | |
299 | ||
300 | /* Always allow shrinking brk. */ | |
301 | if (brk <= mm->brk) { | |
302 | if (!do_munmap(mm, newbrk, oldbrk-newbrk)) | |
303 | goto set_brk; | |
304 | goto out; | |
305 | } | |
306 | ||
1da177e4 LT |
307 | /* Check against existing mmap mappings. */ |
308 | if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE)) | |
309 | goto out; | |
310 | ||
311 | /* Ok, looks good - let it rip. */ | |
312 | if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk) | |
313 | goto out; | |
128557ff | 314 | |
1da177e4 LT |
315 | set_brk: |
316 | mm->brk = brk; | |
128557ff ML |
317 | populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0; |
318 | up_write(&mm->mmap_sem); | |
319 | if (populate) | |
320 | mm_populate(oldbrk, newbrk - oldbrk); | |
321 | return brk; | |
322 | ||
1da177e4 LT |
323 | out: |
324 | retval = mm->brk; | |
325 | up_write(&mm->mmap_sem); | |
326 | return retval; | |
327 | } | |
328 | ||
d3737187 ML |
329 | static long vma_compute_subtree_gap(struct vm_area_struct *vma) |
330 | { | |
331 | unsigned long max, subtree_gap; | |
332 | max = vma->vm_start; | |
333 | if (vma->vm_prev) | |
334 | max -= vma->vm_prev->vm_end; | |
335 | if (vma->vm_rb.rb_left) { | |
336 | subtree_gap = rb_entry(vma->vm_rb.rb_left, | |
337 | struct vm_area_struct, vm_rb)->rb_subtree_gap; | |
338 | if (subtree_gap > max) | |
339 | max = subtree_gap; | |
340 | } | |
341 | if (vma->vm_rb.rb_right) { | |
342 | subtree_gap = rb_entry(vma->vm_rb.rb_right, | |
343 | struct vm_area_struct, vm_rb)->rb_subtree_gap; | |
344 | if (subtree_gap > max) | |
345 | max = subtree_gap; | |
346 | } | |
347 | return max; | |
348 | } | |
349 | ||
ed8ea815 | 350 | #ifdef CONFIG_DEBUG_VM_RB |
1da177e4 LT |
351 | static int browse_rb(struct rb_root *root) |
352 | { | |
5a0768f6 | 353 | int i = 0, j, bug = 0; |
1da177e4 LT |
354 | struct rb_node *nd, *pn = NULL; |
355 | unsigned long prev = 0, pend = 0; | |
356 | ||
357 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
358 | struct vm_area_struct *vma; | |
359 | vma = rb_entry(nd, struct vm_area_struct, vm_rb); | |
5a0768f6 ML |
360 | if (vma->vm_start < prev) { |
361 | printk("vm_start %lx prev %lx\n", vma->vm_start, prev); | |
362 | bug = 1; | |
363 | } | |
364 | if (vma->vm_start < pend) { | |
1da177e4 | 365 | printk("vm_start %lx pend %lx\n", vma->vm_start, pend); |
5a0768f6 ML |
366 | bug = 1; |
367 | } | |
368 | if (vma->vm_start > vma->vm_end) { | |
369 | printk("vm_end %lx < vm_start %lx\n", | |
370 | vma->vm_end, vma->vm_start); | |
371 | bug = 1; | |
372 | } | |
373 | if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) { | |
374 | printk("free gap %lx, correct %lx\n", | |
375 | vma->rb_subtree_gap, | |
376 | vma_compute_subtree_gap(vma)); | |
377 | bug = 1; | |
378 | } | |
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 ML |
387 | if (i != j) { |
388 | printk("backwards %d, forwards %d\n", j, i); | |
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); | |
401 | BUG_ON(vma != ignore && | |
402 | vma->rb_subtree_gap != vma_compute_subtree_gap(vma)); | |
1da177e4 | 403 | } |
1da177e4 LT |
404 | } |
405 | ||
406 | void validate_mm(struct mm_struct *mm) | |
407 | { | |
408 | int bug = 0; | |
409 | int i = 0; | |
5a0768f6 | 410 | unsigned long highest_address = 0; |
ed8ea815 ML |
411 | struct vm_area_struct *vma = mm->mmap; |
412 | while (vma) { | |
413 | struct anon_vma_chain *avc; | |
63c3b902 | 414 | vma_lock_anon_vma(vma); |
ed8ea815 ML |
415 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
416 | anon_vma_interval_tree_verify(avc); | |
63c3b902 | 417 | vma_unlock_anon_vma(vma); |
5a0768f6 | 418 | highest_address = vma->vm_end; |
ed8ea815 | 419 | vma = vma->vm_next; |
1da177e4 LT |
420 | i++; |
421 | } | |
5a0768f6 ML |
422 | if (i != mm->map_count) { |
423 | printk("map_count %d vm_next %d\n", mm->map_count, i); | |
424 | bug = 1; | |
425 | } | |
426 | if (highest_address != mm->highest_vm_end) { | |
427 | printk("mm->highest_vm_end %lx, found %lx\n", | |
428 | mm->highest_vm_end, highest_address); | |
429 | bug = 1; | |
430 | } | |
1da177e4 | 431 | i = browse_rb(&mm->mm_rb); |
5a0768f6 ML |
432 | if (i != mm->map_count) { |
433 | printk("map_count %d rb %d\n", mm->map_count, i); | |
434 | bug = 1; | |
435 | } | |
46a350ef | 436 | BUG_ON(bug); |
1da177e4 LT |
437 | } |
438 | #else | |
d3737187 | 439 | #define validate_mm_rb(root, ignore) do { } while (0) |
1da177e4 LT |
440 | #define validate_mm(mm) do { } while (0) |
441 | #endif | |
442 | ||
d3737187 ML |
443 | RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb, |
444 | unsigned long, rb_subtree_gap, vma_compute_subtree_gap) | |
445 | ||
446 | /* | |
447 | * Update augmented rbtree rb_subtree_gap values after vma->vm_start or | |
448 | * vma->vm_prev->vm_end values changed, without modifying the vma's position | |
449 | * in the rbtree. | |
450 | */ | |
451 | static void vma_gap_update(struct vm_area_struct *vma) | |
452 | { | |
453 | /* | |
454 | * As it turns out, RB_DECLARE_CALLBACKS() already created a callback | |
455 | * function that does exacltly what we want. | |
456 | */ | |
457 | vma_gap_callbacks_propagate(&vma->vm_rb, NULL); | |
458 | } | |
459 | ||
460 | static inline void vma_rb_insert(struct vm_area_struct *vma, | |
461 | struct rb_root *root) | |
462 | { | |
463 | /* All rb_subtree_gap values must be consistent prior to insertion */ | |
464 | validate_mm_rb(root, NULL); | |
465 | ||
466 | rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks); | |
467 | } | |
468 | ||
469 | static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root) | |
470 | { | |
471 | /* | |
472 | * All rb_subtree_gap values must be consistent prior to erase, | |
473 | * with the possible exception of the vma being erased. | |
474 | */ | |
475 | validate_mm_rb(root, vma); | |
476 | ||
477 | /* | |
478 | * Note rb_erase_augmented is a fairly large inline function, | |
479 | * so make sure we instantiate it only once with our desired | |
480 | * augmented rbtree callbacks. | |
481 | */ | |
482 | rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks); | |
483 | } | |
484 | ||
bf181b9f ML |
485 | /* |
486 | * vma has some anon_vma assigned, and is already inserted on that | |
487 | * anon_vma's interval trees. | |
488 | * | |
489 | * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the | |
490 | * vma must be removed from the anon_vma's interval trees using | |
491 | * anon_vma_interval_tree_pre_update_vma(). | |
492 | * | |
493 | * After the update, the vma will be reinserted using | |
494 | * anon_vma_interval_tree_post_update_vma(). | |
495 | * | |
496 | * The entire update must be protected by exclusive mmap_sem and by | |
497 | * the root anon_vma's mutex. | |
498 | */ | |
499 | static inline void | |
500 | anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma) | |
501 | { | |
502 | struct anon_vma_chain *avc; | |
503 | ||
504 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) | |
505 | anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root); | |
506 | } | |
507 | ||
508 | static inline void | |
509 | anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma) | |
510 | { | |
511 | struct anon_vma_chain *avc; | |
512 | ||
513 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) | |
514 | anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root); | |
515 | } | |
516 | ||
6597d783 HD |
517 | static int find_vma_links(struct mm_struct *mm, unsigned long addr, |
518 | unsigned long end, struct vm_area_struct **pprev, | |
519 | struct rb_node ***rb_link, struct rb_node **rb_parent) | |
1da177e4 | 520 | { |
6597d783 | 521 | struct rb_node **__rb_link, *__rb_parent, *rb_prev; |
1da177e4 LT |
522 | |
523 | __rb_link = &mm->mm_rb.rb_node; | |
524 | rb_prev = __rb_parent = NULL; | |
1da177e4 LT |
525 | |
526 | while (*__rb_link) { | |
527 | struct vm_area_struct *vma_tmp; | |
528 | ||
529 | __rb_parent = *__rb_link; | |
530 | vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb); | |
531 | ||
532 | if (vma_tmp->vm_end > addr) { | |
6597d783 HD |
533 | /* Fail if an existing vma overlaps the area */ |
534 | if (vma_tmp->vm_start < end) | |
535 | return -ENOMEM; | |
1da177e4 LT |
536 | __rb_link = &__rb_parent->rb_left; |
537 | } else { | |
538 | rb_prev = __rb_parent; | |
539 | __rb_link = &__rb_parent->rb_right; | |
540 | } | |
541 | } | |
542 | ||
543 | *pprev = NULL; | |
544 | if (rb_prev) | |
545 | *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb); | |
546 | *rb_link = __rb_link; | |
547 | *rb_parent = __rb_parent; | |
6597d783 | 548 | return 0; |
1da177e4 LT |
549 | } |
550 | ||
1da177e4 LT |
551 | void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma, |
552 | struct rb_node **rb_link, struct rb_node *rb_parent) | |
553 | { | |
d3737187 ML |
554 | /* Update tracking information for the gap following the new vma. */ |
555 | if (vma->vm_next) | |
556 | vma_gap_update(vma->vm_next); | |
557 | else | |
558 | mm->highest_vm_end = vma->vm_end; | |
559 | ||
560 | /* | |
561 | * vma->vm_prev wasn't known when we followed the rbtree to find the | |
562 | * correct insertion point for that vma. As a result, we could not | |
563 | * update the vma vm_rb parents rb_subtree_gap values on the way down. | |
564 | * So, we first insert the vma with a zero rb_subtree_gap value | |
565 | * (to be consistent with what we did on the way down), and then | |
566 | * immediately update the gap to the correct value. Finally we | |
567 | * rebalance the rbtree after all augmented values have been set. | |
568 | */ | |
1da177e4 | 569 | rb_link_node(&vma->vm_rb, rb_parent, rb_link); |
d3737187 ML |
570 | vma->rb_subtree_gap = 0; |
571 | vma_gap_update(vma); | |
572 | vma_rb_insert(vma, &mm->mm_rb); | |
1da177e4 LT |
573 | } |
574 | ||
cb8f488c | 575 | static void __vma_link_file(struct vm_area_struct *vma) |
1da177e4 | 576 | { |
48aae425 | 577 | struct file *file; |
1da177e4 LT |
578 | |
579 | file = vma->vm_file; | |
580 | if (file) { | |
581 | struct address_space *mapping = file->f_mapping; | |
582 | ||
583 | if (vma->vm_flags & VM_DENYWRITE) | |
496ad9aa | 584 | atomic_dec(&file_inode(file)->i_writecount); |
1da177e4 LT |
585 | if (vma->vm_flags & VM_SHARED) |
586 | mapping->i_mmap_writable++; | |
587 | ||
588 | flush_dcache_mmap_lock(mapping); | |
589 | if (unlikely(vma->vm_flags & VM_NONLINEAR)) | |
590 | vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear); | |
591 | else | |
6b2dbba8 | 592 | vma_interval_tree_insert(vma, &mapping->i_mmap); |
1da177e4 LT |
593 | flush_dcache_mmap_unlock(mapping); |
594 | } | |
595 | } | |
596 | ||
597 | static void | |
598 | __vma_link(struct mm_struct *mm, struct vm_area_struct *vma, | |
599 | struct vm_area_struct *prev, struct rb_node **rb_link, | |
600 | struct rb_node *rb_parent) | |
601 | { | |
602 | __vma_link_list(mm, vma, prev, rb_parent); | |
603 | __vma_link_rb(mm, vma, rb_link, rb_parent); | |
1da177e4 LT |
604 | } |
605 | ||
606 | static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma, | |
607 | struct vm_area_struct *prev, struct rb_node **rb_link, | |
608 | struct rb_node *rb_parent) | |
609 | { | |
610 | struct address_space *mapping = NULL; | |
611 | ||
612 | if (vma->vm_file) | |
613 | mapping = vma->vm_file->f_mapping; | |
614 | ||
97a89413 | 615 | if (mapping) |
3d48ae45 | 616 | mutex_lock(&mapping->i_mmap_mutex); |
1da177e4 LT |
617 | |
618 | __vma_link(mm, vma, prev, rb_link, rb_parent); | |
619 | __vma_link_file(vma); | |
620 | ||
1da177e4 | 621 | if (mapping) |
3d48ae45 | 622 | mutex_unlock(&mapping->i_mmap_mutex); |
1da177e4 LT |
623 | |
624 | mm->map_count++; | |
625 | validate_mm(mm); | |
626 | } | |
627 | ||
628 | /* | |
88f6b4c3 | 629 | * Helper for vma_adjust() in the split_vma insert case: insert a vma into the |
6b2dbba8 | 630 | * mm's list and rbtree. It has already been inserted into the interval tree. |
1da177e4 | 631 | */ |
48aae425 | 632 | static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) |
1da177e4 | 633 | { |
6597d783 | 634 | struct vm_area_struct *prev; |
48aae425 | 635 | struct rb_node **rb_link, *rb_parent; |
1da177e4 | 636 | |
6597d783 HD |
637 | if (find_vma_links(mm, vma->vm_start, vma->vm_end, |
638 | &prev, &rb_link, &rb_parent)) | |
639 | BUG(); | |
1da177e4 LT |
640 | __vma_link(mm, vma, prev, rb_link, rb_parent); |
641 | mm->map_count++; | |
642 | } | |
643 | ||
644 | static inline void | |
645 | __vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma, | |
646 | struct vm_area_struct *prev) | |
647 | { | |
d3737187 | 648 | struct vm_area_struct *next; |
297c5eee | 649 | |
d3737187 ML |
650 | vma_rb_erase(vma, &mm->mm_rb); |
651 | prev->vm_next = next = vma->vm_next; | |
297c5eee LT |
652 | if (next) |
653 | next->vm_prev = prev; | |
1da177e4 LT |
654 | if (mm->mmap_cache == vma) |
655 | mm->mmap_cache = prev; | |
656 | } | |
657 | ||
658 | /* | |
659 | * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that | |
660 | * is already present in an i_mmap tree without adjusting the tree. | |
661 | * The following helper function should be used when such adjustments | |
662 | * are necessary. The "insert" vma (if any) is to be inserted | |
663 | * before we drop the necessary locks. | |
664 | */ | |
5beb4930 | 665 | int vma_adjust(struct vm_area_struct *vma, unsigned long start, |
1da177e4 LT |
666 | unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert) |
667 | { | |
668 | struct mm_struct *mm = vma->vm_mm; | |
669 | struct vm_area_struct *next = vma->vm_next; | |
670 | struct vm_area_struct *importer = NULL; | |
671 | struct address_space *mapping = NULL; | |
6b2dbba8 | 672 | struct rb_root *root = NULL; |
012f1800 | 673 | struct anon_vma *anon_vma = NULL; |
1da177e4 | 674 | struct file *file = vma->vm_file; |
d3737187 | 675 | bool start_changed = false, end_changed = false; |
1da177e4 LT |
676 | long adjust_next = 0; |
677 | int remove_next = 0; | |
678 | ||
679 | if (next && !insert) { | |
287d97ac LT |
680 | struct vm_area_struct *exporter = NULL; |
681 | ||
1da177e4 LT |
682 | if (end >= next->vm_end) { |
683 | /* | |
684 | * vma expands, overlapping all the next, and | |
685 | * perhaps the one after too (mprotect case 6). | |
686 | */ | |
687 | again: remove_next = 1 + (end > next->vm_end); | |
688 | end = next->vm_end; | |
287d97ac | 689 | exporter = next; |
1da177e4 LT |
690 | importer = vma; |
691 | } else if (end > next->vm_start) { | |
692 | /* | |
693 | * vma expands, overlapping part of the next: | |
694 | * mprotect case 5 shifting the boundary up. | |
695 | */ | |
696 | adjust_next = (end - next->vm_start) >> PAGE_SHIFT; | |
287d97ac | 697 | exporter = next; |
1da177e4 LT |
698 | importer = vma; |
699 | } else if (end < vma->vm_end) { | |
700 | /* | |
701 | * vma shrinks, and !insert tells it's not | |
702 | * split_vma inserting another: so it must be | |
703 | * mprotect case 4 shifting the boundary down. | |
704 | */ | |
705 | adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT); | |
287d97ac | 706 | exporter = vma; |
1da177e4 LT |
707 | importer = next; |
708 | } | |
1da177e4 | 709 | |
5beb4930 RR |
710 | /* |
711 | * Easily overlooked: when mprotect shifts the boundary, | |
712 | * make sure the expanding vma has anon_vma set if the | |
713 | * shrinking vma had, to cover any anon pages imported. | |
714 | */ | |
287d97ac LT |
715 | if (exporter && exporter->anon_vma && !importer->anon_vma) { |
716 | if (anon_vma_clone(importer, exporter)) | |
5beb4930 | 717 | return -ENOMEM; |
287d97ac | 718 | importer->anon_vma = exporter->anon_vma; |
5beb4930 RR |
719 | } |
720 | } | |
721 | ||
1da177e4 LT |
722 | if (file) { |
723 | mapping = file->f_mapping; | |
682968e0 | 724 | if (!(vma->vm_flags & VM_NONLINEAR)) { |
1da177e4 | 725 | root = &mapping->i_mmap; |
cbc91f71 | 726 | uprobe_munmap(vma, vma->vm_start, vma->vm_end); |
682968e0 SD |
727 | |
728 | if (adjust_next) | |
cbc91f71 SD |
729 | uprobe_munmap(next, next->vm_start, |
730 | next->vm_end); | |
682968e0 SD |
731 | } |
732 | ||
3d48ae45 | 733 | mutex_lock(&mapping->i_mmap_mutex); |
1da177e4 | 734 | if (insert) { |
1da177e4 | 735 | /* |
6b2dbba8 | 736 | * Put into interval tree now, so instantiated pages |
1da177e4 LT |
737 | * are visible to arm/parisc __flush_dcache_page |
738 | * throughout; but we cannot insert into address | |
739 | * space until vma start or end is updated. | |
740 | */ | |
741 | __vma_link_file(insert); | |
742 | } | |
743 | } | |
744 | ||
94fcc585 AA |
745 | vma_adjust_trans_huge(vma, start, end, adjust_next); |
746 | ||
bf181b9f ML |
747 | anon_vma = vma->anon_vma; |
748 | if (!anon_vma && adjust_next) | |
749 | anon_vma = next->anon_vma; | |
750 | if (anon_vma) { | |
ca42b26a ML |
751 | VM_BUG_ON(adjust_next && next->anon_vma && |
752 | anon_vma != next->anon_vma); | |
4fc3f1d6 | 753 | anon_vma_lock_write(anon_vma); |
bf181b9f ML |
754 | anon_vma_interval_tree_pre_update_vma(vma); |
755 | if (adjust_next) | |
756 | anon_vma_interval_tree_pre_update_vma(next); | |
757 | } | |
012f1800 | 758 | |
1da177e4 LT |
759 | if (root) { |
760 | flush_dcache_mmap_lock(mapping); | |
6b2dbba8 | 761 | vma_interval_tree_remove(vma, root); |
1da177e4 | 762 | if (adjust_next) |
6b2dbba8 | 763 | vma_interval_tree_remove(next, root); |
1da177e4 LT |
764 | } |
765 | ||
d3737187 ML |
766 | if (start != vma->vm_start) { |
767 | vma->vm_start = start; | |
768 | start_changed = true; | |
769 | } | |
770 | if (end != vma->vm_end) { | |
771 | vma->vm_end = end; | |
772 | end_changed = true; | |
773 | } | |
1da177e4 LT |
774 | vma->vm_pgoff = pgoff; |
775 | if (adjust_next) { | |
776 | next->vm_start += adjust_next << PAGE_SHIFT; | |
777 | next->vm_pgoff += adjust_next; | |
778 | } | |
779 | ||
780 | if (root) { | |
781 | if (adjust_next) | |
6b2dbba8 ML |
782 | vma_interval_tree_insert(next, root); |
783 | vma_interval_tree_insert(vma, root); | |
1da177e4 LT |
784 | flush_dcache_mmap_unlock(mapping); |
785 | } | |
786 | ||
787 | if (remove_next) { | |
788 | /* | |
789 | * vma_merge has merged next into vma, and needs | |
790 | * us to remove next before dropping the locks. | |
791 | */ | |
792 | __vma_unlink(mm, next, vma); | |
793 | if (file) | |
794 | __remove_shared_vm_struct(next, file, mapping); | |
1da177e4 LT |
795 | } else if (insert) { |
796 | /* | |
797 | * split_vma has split insert from vma, and needs | |
798 | * us to insert it before dropping the locks | |
799 | * (it may either follow vma or precede it). | |
800 | */ | |
801 | __insert_vm_struct(mm, insert); | |
d3737187 ML |
802 | } else { |
803 | if (start_changed) | |
804 | vma_gap_update(vma); | |
805 | if (end_changed) { | |
806 | if (!next) | |
807 | mm->highest_vm_end = end; | |
808 | else if (!adjust_next) | |
809 | vma_gap_update(next); | |
810 | } | |
1da177e4 LT |
811 | } |
812 | ||
bf181b9f ML |
813 | if (anon_vma) { |
814 | anon_vma_interval_tree_post_update_vma(vma); | |
815 | if (adjust_next) | |
816 | anon_vma_interval_tree_post_update_vma(next); | |
08b52706 | 817 | anon_vma_unlock_write(anon_vma); |
bf181b9f | 818 | } |
1da177e4 | 819 | if (mapping) |
3d48ae45 | 820 | mutex_unlock(&mapping->i_mmap_mutex); |
1da177e4 | 821 | |
2b144498 | 822 | if (root) { |
7b2d81d4 | 823 | uprobe_mmap(vma); |
2b144498 SD |
824 | |
825 | if (adjust_next) | |
7b2d81d4 | 826 | uprobe_mmap(next); |
2b144498 SD |
827 | } |
828 | ||
1da177e4 | 829 | if (remove_next) { |
925d1c40 | 830 | if (file) { |
cbc91f71 | 831 | uprobe_munmap(next, next->vm_start, next->vm_end); |
1da177e4 | 832 | fput(file); |
925d1c40 | 833 | } |
5beb4930 RR |
834 | if (next->anon_vma) |
835 | anon_vma_merge(vma, next); | |
1da177e4 | 836 | mm->map_count--; |
1444f92c | 837 | vma_set_policy(vma, vma_policy(next)); |
1da177e4 LT |
838 | kmem_cache_free(vm_area_cachep, next); |
839 | /* | |
840 | * In mprotect's case 6 (see comments on vma_merge), | |
841 | * we must remove another next too. It would clutter | |
842 | * up the code too much to do both in one go. | |
843 | */ | |
d3737187 ML |
844 | next = vma->vm_next; |
845 | if (remove_next == 2) | |
1da177e4 | 846 | goto again; |
d3737187 ML |
847 | else if (next) |
848 | vma_gap_update(next); | |
849 | else | |
850 | mm->highest_vm_end = end; | |
1da177e4 | 851 | } |
2b144498 | 852 | if (insert && file) |
7b2d81d4 | 853 | uprobe_mmap(insert); |
1da177e4 LT |
854 | |
855 | validate_mm(mm); | |
5beb4930 RR |
856 | |
857 | return 0; | |
1da177e4 LT |
858 | } |
859 | ||
860 | /* | |
861 | * If the vma has a ->close operation then the driver probably needs to release | |
862 | * per-vma resources, so we don't attempt to merge those. | |
863 | */ | |
1da177e4 LT |
864 | static inline int is_mergeable_vma(struct vm_area_struct *vma, |
865 | struct file *file, unsigned long vm_flags) | |
866 | { | |
0b173bc4 | 867 | if (vma->vm_flags ^ vm_flags) |
1da177e4 LT |
868 | return 0; |
869 | if (vma->vm_file != file) | |
870 | return 0; | |
871 | if (vma->vm_ops && vma->vm_ops->close) | |
872 | return 0; | |
873 | return 1; | |
874 | } | |
875 | ||
876 | static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1, | |
965f55de SL |
877 | struct anon_vma *anon_vma2, |
878 | struct vm_area_struct *vma) | |
1da177e4 | 879 | { |
965f55de SL |
880 | /* |
881 | * The list_is_singular() test is to avoid merging VMA cloned from | |
882 | * parents. This can improve scalability caused by anon_vma lock. | |
883 | */ | |
884 | if ((!anon_vma1 || !anon_vma2) && (!vma || | |
885 | list_is_singular(&vma->anon_vma_chain))) | |
886 | return 1; | |
887 | return anon_vma1 == anon_vma2; | |
1da177e4 LT |
888 | } |
889 | ||
890 | /* | |
891 | * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) | |
892 | * in front of (at a lower virtual address and file offset than) the vma. | |
893 | * | |
894 | * We cannot merge two vmas if they have differently assigned (non-NULL) | |
895 | * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. | |
896 | * | |
897 | * We don't check here for the merged mmap wrapping around the end of pagecache | |
898 | * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which | |
899 | * wrap, nor mmaps which cover the final page at index -1UL. | |
900 | */ | |
901 | static int | |
902 | can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags, | |
903 | struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff) | |
904 | { | |
905 | if (is_mergeable_vma(vma, file, vm_flags) && | |
965f55de | 906 | is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { |
1da177e4 LT |
907 | if (vma->vm_pgoff == vm_pgoff) |
908 | return 1; | |
909 | } | |
910 | return 0; | |
911 | } | |
912 | ||
913 | /* | |
914 | * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff) | |
915 | * beyond (at a higher virtual address and file offset than) the vma. | |
916 | * | |
917 | * We cannot merge two vmas if they have differently assigned (non-NULL) | |
918 | * anon_vmas, nor if same anon_vma is assigned but offsets incompatible. | |
919 | */ | |
920 | static int | |
921 | can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags, | |
922 | struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff) | |
923 | { | |
924 | if (is_mergeable_vma(vma, file, vm_flags) && | |
965f55de | 925 | is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) { |
1da177e4 LT |
926 | pgoff_t vm_pglen; |
927 | vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; | |
928 | if (vma->vm_pgoff + vm_pglen == vm_pgoff) | |
929 | return 1; | |
930 | } | |
931 | return 0; | |
932 | } | |
933 | ||
934 | /* | |
935 | * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out | |
936 | * whether that can be merged with its predecessor or its successor. | |
937 | * Or both (it neatly fills a hole). | |
938 | * | |
939 | * In most cases - when called for mmap, brk or mremap - [addr,end) is | |
940 | * certain not to be mapped by the time vma_merge is called; but when | |
941 | * called for mprotect, it is certain to be already mapped (either at | |
942 | * an offset within prev, or at the start of next), and the flags of | |
943 | * this area are about to be changed to vm_flags - and the no-change | |
944 | * case has already been eliminated. | |
945 | * | |
946 | * The following mprotect cases have to be considered, where AAAA is | |
947 | * the area passed down from mprotect_fixup, never extending beyond one | |
948 | * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after: | |
949 | * | |
950 | * AAAA AAAA AAAA AAAA | |
951 | * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX | |
952 | * cannot merge might become might become might become | |
953 | * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or | |
954 | * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or | |
955 | * mremap move: PPPPNNNNNNNN 8 | |
956 | * AAAA | |
957 | * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN | |
958 | * might become case 1 below case 2 below case 3 below | |
959 | * | |
960 | * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX: | |
961 | * mprotect_fixup updates vm_flags & vm_page_prot on successful return. | |
962 | */ | |
963 | struct vm_area_struct *vma_merge(struct mm_struct *mm, | |
964 | struct vm_area_struct *prev, unsigned long addr, | |
965 | unsigned long end, unsigned long vm_flags, | |
966 | struct anon_vma *anon_vma, struct file *file, | |
967 | pgoff_t pgoff, struct mempolicy *policy) | |
968 | { | |
969 | pgoff_t pglen = (end - addr) >> PAGE_SHIFT; | |
970 | struct vm_area_struct *area, *next; | |
5beb4930 | 971 | int err; |
1da177e4 LT |
972 | |
973 | /* | |
974 | * We later require that vma->vm_flags == vm_flags, | |
975 | * so this tests vma->vm_flags & VM_SPECIAL, too. | |
976 | */ | |
977 | if (vm_flags & VM_SPECIAL) | |
978 | return NULL; | |
979 | ||
980 | if (prev) | |
981 | next = prev->vm_next; | |
982 | else | |
983 | next = mm->mmap; | |
984 | area = next; | |
985 | if (next && next->vm_end == end) /* cases 6, 7, 8 */ | |
986 | next = next->vm_next; | |
987 | ||
988 | /* | |
989 | * Can it merge with the predecessor? | |
990 | */ | |
991 | if (prev && prev->vm_end == addr && | |
992 | mpol_equal(vma_policy(prev), policy) && | |
993 | can_vma_merge_after(prev, vm_flags, | |
994 | anon_vma, file, pgoff)) { | |
995 | /* | |
996 | * OK, it can. Can we now merge in the successor as well? | |
997 | */ | |
998 | if (next && end == next->vm_start && | |
999 | mpol_equal(policy, vma_policy(next)) && | |
1000 | can_vma_merge_before(next, vm_flags, | |
1001 | anon_vma, file, pgoff+pglen) && | |
1002 | is_mergeable_anon_vma(prev->anon_vma, | |
965f55de | 1003 | next->anon_vma, NULL)) { |
1da177e4 | 1004 | /* cases 1, 6 */ |
5beb4930 | 1005 | err = vma_adjust(prev, prev->vm_start, |
1da177e4 LT |
1006 | next->vm_end, prev->vm_pgoff, NULL); |
1007 | } else /* cases 2, 5, 7 */ | |
5beb4930 | 1008 | err = vma_adjust(prev, prev->vm_start, |
1da177e4 | 1009 | end, prev->vm_pgoff, NULL); |
5beb4930 RR |
1010 | if (err) |
1011 | return NULL; | |
b15d00b6 | 1012 | khugepaged_enter_vma_merge(prev); |
1da177e4 LT |
1013 | return prev; |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * Can this new request be merged in front of next? | |
1018 | */ | |
1019 | if (next && end == next->vm_start && | |
1020 | mpol_equal(policy, vma_policy(next)) && | |
1021 | can_vma_merge_before(next, vm_flags, | |
1022 | anon_vma, file, pgoff+pglen)) { | |
1023 | if (prev && addr < prev->vm_end) /* case 4 */ | |
5beb4930 | 1024 | err = vma_adjust(prev, prev->vm_start, |
1da177e4 LT |
1025 | addr, prev->vm_pgoff, NULL); |
1026 | else /* cases 3, 8 */ | |
5beb4930 | 1027 | err = vma_adjust(area, addr, next->vm_end, |
1da177e4 | 1028 | next->vm_pgoff - pglen, NULL); |
5beb4930 RR |
1029 | if (err) |
1030 | return NULL; | |
b15d00b6 | 1031 | khugepaged_enter_vma_merge(area); |
1da177e4 LT |
1032 | return area; |
1033 | } | |
1034 | ||
1035 | return NULL; | |
1036 | } | |
1037 | ||
d0e9fe17 LT |
1038 | /* |
1039 | * Rough compatbility check to quickly see if it's even worth looking | |
1040 | * at sharing an anon_vma. | |
1041 | * | |
1042 | * They need to have the same vm_file, and the flags can only differ | |
1043 | * in things that mprotect may change. | |
1044 | * | |
1045 | * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that | |
1046 | * we can merge the two vma's. For example, we refuse to merge a vma if | |
1047 | * there is a vm_ops->close() function, because that indicates that the | |
1048 | * driver is doing some kind of reference counting. But that doesn't | |
1049 | * really matter for the anon_vma sharing case. | |
1050 | */ | |
1051 | static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b) | |
1052 | { | |
1053 | return a->vm_end == b->vm_start && | |
1054 | mpol_equal(vma_policy(a), vma_policy(b)) && | |
1055 | a->vm_file == b->vm_file && | |
1056 | !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) && | |
1057 | b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT); | |
1058 | } | |
1059 | ||
1060 | /* | |
1061 | * Do some basic sanity checking to see if we can re-use the anon_vma | |
1062 | * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be | |
1063 | * the same as 'old', the other will be the new one that is trying | |
1064 | * to share the anon_vma. | |
1065 | * | |
1066 | * NOTE! This runs with mm_sem held for reading, so it is possible that | |
1067 | * the anon_vma of 'old' is concurrently in the process of being set up | |
1068 | * by another page fault trying to merge _that_. But that's ok: if it | |
1069 | * is being set up, that automatically means that it will be a singleton | |
1070 | * acceptable for merging, so we can do all of this optimistically. But | |
1071 | * we do that ACCESS_ONCE() to make sure that we never re-load the pointer. | |
1072 | * | |
1073 | * IOW: that the "list_is_singular()" test on the anon_vma_chain only | |
1074 | * matters for the 'stable anon_vma' case (ie the thing we want to avoid | |
1075 | * is to return an anon_vma that is "complex" due to having gone through | |
1076 | * a fork). | |
1077 | * | |
1078 | * We also make sure that the two vma's are compatible (adjacent, | |
1079 | * and with the same memory policies). That's all stable, even with just | |
1080 | * a read lock on the mm_sem. | |
1081 | */ | |
1082 | static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b) | |
1083 | { | |
1084 | if (anon_vma_compatible(a, b)) { | |
1085 | struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma); | |
1086 | ||
1087 | if (anon_vma && list_is_singular(&old->anon_vma_chain)) | |
1088 | return anon_vma; | |
1089 | } | |
1090 | return NULL; | |
1091 | } | |
1092 | ||
1da177e4 LT |
1093 | /* |
1094 | * find_mergeable_anon_vma is used by anon_vma_prepare, to check | |
1095 | * neighbouring vmas for a suitable anon_vma, before it goes off | |
1096 | * to allocate a new anon_vma. It checks because a repetitive | |
1097 | * sequence of mprotects and faults may otherwise lead to distinct | |
1098 | * anon_vmas being allocated, preventing vma merge in subsequent | |
1099 | * mprotect. | |
1100 | */ | |
1101 | struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) | |
1102 | { | |
d0e9fe17 | 1103 | struct anon_vma *anon_vma; |
1da177e4 | 1104 | struct vm_area_struct *near; |
1da177e4 LT |
1105 | |
1106 | near = vma->vm_next; | |
1107 | if (!near) | |
1108 | goto try_prev; | |
1109 | ||
d0e9fe17 LT |
1110 | anon_vma = reusable_anon_vma(near, vma, near); |
1111 | if (anon_vma) | |
1112 | return anon_vma; | |
1da177e4 | 1113 | try_prev: |
9be34c9d | 1114 | near = vma->vm_prev; |
1da177e4 LT |
1115 | if (!near) |
1116 | goto none; | |
1117 | ||
d0e9fe17 LT |
1118 | anon_vma = reusable_anon_vma(near, near, vma); |
1119 | if (anon_vma) | |
1120 | return anon_vma; | |
1da177e4 LT |
1121 | none: |
1122 | /* | |
1123 | * There's no absolute need to look only at touching neighbours: | |
1124 | * we could search further afield for "compatible" anon_vmas. | |
1125 | * But it would probably just be a waste of time searching, | |
1126 | * or lead to too many vmas hanging off the same anon_vma. | |
1127 | * We're trying to allow mprotect remerging later on, | |
1128 | * not trying to minimize memory used for anon_vmas. | |
1129 | */ | |
1130 | return NULL; | |
1131 | } | |
1132 | ||
1133 | #ifdef CONFIG_PROC_FS | |
ab50b8ed | 1134 | void vm_stat_account(struct mm_struct *mm, unsigned long flags, |
1da177e4 LT |
1135 | struct file *file, long pages) |
1136 | { | |
1137 | const unsigned long stack_flags | |
1138 | = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN); | |
1139 | ||
44de9d0c HS |
1140 | mm->total_vm += pages; |
1141 | ||
1da177e4 LT |
1142 | if (file) { |
1143 | mm->shared_vm += pages; | |
1144 | if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC) | |
1145 | mm->exec_vm += pages; | |
1146 | } else if (flags & stack_flags) | |
1147 | mm->stack_vm += pages; | |
1da177e4 LT |
1148 | } |
1149 | #endif /* CONFIG_PROC_FS */ | |
1150 | ||
40401530 AV |
1151 | /* |
1152 | * If a hint addr is less than mmap_min_addr change hint to be as | |
1153 | * low as possible but still greater than mmap_min_addr | |
1154 | */ | |
1155 | static inline unsigned long round_hint_to_min(unsigned long hint) | |
1156 | { | |
1157 | hint &= PAGE_MASK; | |
1158 | if (((void *)hint != NULL) && | |
1159 | (hint < mmap_min_addr)) | |
1160 | return PAGE_ALIGN(mmap_min_addr); | |
1161 | return hint; | |
1162 | } | |
1163 | ||
1da177e4 | 1164 | /* |
27f5de79 | 1165 | * The caller must hold down_write(¤t->mm->mmap_sem). |
1da177e4 LT |
1166 | */ |
1167 | ||
e3fc629d | 1168 | unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, |
1da177e4 | 1169 | unsigned long len, unsigned long prot, |
bebeb3d6 | 1170 | unsigned long flags, unsigned long pgoff, |
41badc15 | 1171 | unsigned long *populate) |
1da177e4 LT |
1172 | { |
1173 | struct mm_struct * mm = current->mm; | |
1da177e4 | 1174 | struct inode *inode; |
ca16d140 | 1175 | vm_flags_t vm_flags; |
1da177e4 | 1176 | |
41badc15 | 1177 | *populate = 0; |
bebeb3d6 | 1178 | |
1da177e4 LT |
1179 | /* |
1180 | * Does the application expect PROT_READ to imply PROT_EXEC? | |
1181 | * | |
1182 | * (the exception is when the underlying filesystem is noexec | |
1183 | * mounted, in which case we dont add PROT_EXEC.) | |
1184 | */ | |
1185 | if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC)) | |
d3ac7f89 | 1186 | if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC))) |
1da177e4 LT |
1187 | prot |= PROT_EXEC; |
1188 | ||
1189 | if (!len) | |
1190 | return -EINVAL; | |
1191 | ||
7cd94146 EP |
1192 | if (!(flags & MAP_FIXED)) |
1193 | addr = round_hint_to_min(addr); | |
1194 | ||
1da177e4 LT |
1195 | /* Careful about overflows.. */ |
1196 | len = PAGE_ALIGN(len); | |
9206de95 | 1197 | if (!len) |
1da177e4 LT |
1198 | return -ENOMEM; |
1199 | ||
1200 | /* offset overflow? */ | |
1201 | if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) | |
1202 | return -EOVERFLOW; | |
1203 | ||
1204 | /* Too many mappings? */ | |
1205 | if (mm->map_count > sysctl_max_map_count) | |
1206 | return -ENOMEM; | |
1207 | ||
1208 | /* Obtain the address to map to. we verify (or select) it and ensure | |
1209 | * that it represents a valid section of the address space. | |
1210 | */ | |
1211 | addr = get_unmapped_area(file, addr, len, pgoff, flags); | |
1212 | if (addr & ~PAGE_MASK) | |
1213 | return addr; | |
1214 | ||
1215 | /* Do simple checking here so the lower-level routines won't have | |
1216 | * to. we assume access permissions have been handled by the open | |
1217 | * of the memory object, so we don't do any here. | |
1218 | */ | |
1219 | vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) | | |
1220 | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; | |
1221 | ||
cdf7b341 | 1222 | if (flags & MAP_LOCKED) |
1da177e4 LT |
1223 | if (!can_do_mlock()) |
1224 | return -EPERM; | |
ba470de4 | 1225 | |
1da177e4 LT |
1226 | /* mlock MCL_FUTURE? */ |
1227 | if (vm_flags & VM_LOCKED) { | |
1228 | unsigned long locked, lock_limit; | |
93ea1d0a CW |
1229 | locked = len >> PAGE_SHIFT; |
1230 | locked += mm->locked_vm; | |
59e99e5b | 1231 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
93ea1d0a | 1232 | lock_limit >>= PAGE_SHIFT; |
1da177e4 LT |
1233 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) |
1234 | return -EAGAIN; | |
1235 | } | |
1236 | ||
496ad9aa | 1237 | inode = file ? file_inode(file) : NULL; |
1da177e4 LT |
1238 | |
1239 | if (file) { | |
1240 | switch (flags & MAP_TYPE) { | |
1241 | case MAP_SHARED: | |
1242 | if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE)) | |
1243 | return -EACCES; | |
1244 | ||
1245 | /* | |
1246 | * Make sure we don't allow writing to an append-only | |
1247 | * file.. | |
1248 | */ | |
1249 | if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE)) | |
1250 | return -EACCES; | |
1251 | ||
1252 | /* | |
1253 | * Make sure there are no mandatory locks on the file. | |
1254 | */ | |
1255 | if (locks_verify_locked(inode)) | |
1256 | return -EAGAIN; | |
1257 | ||
1258 | vm_flags |= VM_SHARED | VM_MAYSHARE; | |
1259 | if (!(file->f_mode & FMODE_WRITE)) | |
1260 | vm_flags &= ~(VM_MAYWRITE | VM_SHARED); | |
1261 | ||
1262 | /* fall through */ | |
1263 | case MAP_PRIVATE: | |
1264 | if (!(file->f_mode & FMODE_READ)) | |
1265 | return -EACCES; | |
d3ac7f89 | 1266 | if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) { |
80c5606c LT |
1267 | if (vm_flags & VM_EXEC) |
1268 | return -EPERM; | |
1269 | vm_flags &= ~VM_MAYEXEC; | |
1270 | } | |
80c5606c LT |
1271 | |
1272 | if (!file->f_op || !file->f_op->mmap) | |
1273 | return -ENODEV; | |
1da177e4 LT |
1274 | break; |
1275 | ||
1276 | default: | |
1277 | return -EINVAL; | |
1278 | } | |
1279 | } else { | |
1280 | switch (flags & MAP_TYPE) { | |
1281 | case MAP_SHARED: | |
ce363942 TH |
1282 | /* |
1283 | * Ignore pgoff. | |
1284 | */ | |
1285 | pgoff = 0; | |
1da177e4 LT |
1286 | vm_flags |= VM_SHARED | VM_MAYSHARE; |
1287 | break; | |
1288 | case MAP_PRIVATE: | |
1289 | /* | |
1290 | * Set pgoff according to addr for anon_vma. | |
1291 | */ | |
1292 | pgoff = addr >> PAGE_SHIFT; | |
1293 | break; | |
1294 | default: | |
1295 | return -EINVAL; | |
1296 | } | |
1297 | } | |
1298 | ||
c22c0d63 ML |
1299 | /* |
1300 | * Set 'VM_NORESERVE' if we should not account for the | |
1301 | * memory use of this mapping. | |
1302 | */ | |
1303 | if (flags & MAP_NORESERVE) { | |
1304 | /* We honor MAP_NORESERVE if allowed to overcommit */ | |
1305 | if (sysctl_overcommit_memory != OVERCOMMIT_NEVER) | |
1306 | vm_flags |= VM_NORESERVE; | |
1307 | ||
1308 | /* hugetlb applies strict overcommit unless MAP_NORESERVE */ | |
1309 | if (file && is_file_hugepages(file)) | |
1310 | vm_flags |= VM_NORESERVE; | |
1311 | } | |
1312 | ||
1313 | addr = mmap_region(file, addr, len, vm_flags, pgoff); | |
09a9f1d2 ML |
1314 | if (!IS_ERR_VALUE(addr) && |
1315 | ((vm_flags & VM_LOCKED) || | |
1316 | (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE)) | |
41badc15 | 1317 | *populate = len; |
bebeb3d6 | 1318 | return addr; |
0165ab44 | 1319 | } |
6be5ceb0 | 1320 | |
66f0dc48 HD |
1321 | SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, |
1322 | unsigned long, prot, unsigned long, flags, | |
1323 | unsigned long, fd, unsigned long, pgoff) | |
1324 | { | |
1325 | struct file *file = NULL; | |
1326 | unsigned long retval = -EBADF; | |
1327 | ||
1328 | if (!(flags & MAP_ANONYMOUS)) { | |
120a795d | 1329 | audit_mmap_fd(fd, flags); |
66f0dc48 HD |
1330 | if (unlikely(flags & MAP_HUGETLB)) |
1331 | return -EINVAL; | |
1332 | file = fget(fd); | |
1333 | if (!file) | |
1334 | goto out; | |
1335 | } else if (flags & MAP_HUGETLB) { | |
1336 | struct user_struct *user = NULL; | |
1337 | /* | |
1338 | * VM_NORESERVE is used because the reservations will be | |
1339 | * taken when vm_ops->mmap() is called | |
1340 | * A dummy user value is used because we are not locking | |
1341 | * memory so no accounting is necessary | |
1342 | */ | |
40716e29 | 1343 | file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len, |
42d7395f AK |
1344 | VM_NORESERVE, |
1345 | &user, HUGETLB_ANONHUGE_INODE, | |
1346 | (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); | |
66f0dc48 HD |
1347 | if (IS_ERR(file)) |
1348 | return PTR_ERR(file); | |
1349 | } | |
1350 | ||
1351 | flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); | |
1352 | ||
eb36c587 | 1353 | retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
66f0dc48 HD |
1354 | if (file) |
1355 | fput(file); | |
1356 | out: | |
1357 | return retval; | |
1358 | } | |
1359 | ||
a4679373 CH |
1360 | #ifdef __ARCH_WANT_SYS_OLD_MMAP |
1361 | struct mmap_arg_struct { | |
1362 | unsigned long addr; | |
1363 | unsigned long len; | |
1364 | unsigned long prot; | |
1365 | unsigned long flags; | |
1366 | unsigned long fd; | |
1367 | unsigned long offset; | |
1368 | }; | |
1369 | ||
1370 | SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) | |
1371 | { | |
1372 | struct mmap_arg_struct a; | |
1373 | ||
1374 | if (copy_from_user(&a, arg, sizeof(a))) | |
1375 | return -EFAULT; | |
1376 | if (a.offset & ~PAGE_MASK) | |
1377 | return -EINVAL; | |
1378 | ||
1379 | return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, | |
1380 | a.offset >> PAGE_SHIFT); | |
1381 | } | |
1382 | #endif /* __ARCH_WANT_SYS_OLD_MMAP */ | |
1383 | ||
4e950f6f AD |
1384 | /* |
1385 | * Some shared mappigns will want the pages marked read-only | |
1386 | * to track write events. If so, we'll downgrade vm_page_prot | |
1387 | * to the private version (using protection_map[] without the | |
1388 | * VM_SHARED bit). | |
1389 | */ | |
1390 | int vma_wants_writenotify(struct vm_area_struct *vma) | |
1391 | { | |
ca16d140 | 1392 | vm_flags_t vm_flags = vma->vm_flags; |
4e950f6f AD |
1393 | |
1394 | /* If it was private or non-writable, the write bit is already clear */ | |
1395 | if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) | |
1396 | return 0; | |
1397 | ||
1398 | /* The backer wishes to know when pages are first written to? */ | |
1399 | if (vma->vm_ops && vma->vm_ops->page_mkwrite) | |
1400 | return 1; | |
1401 | ||
1402 | /* The open routine did something to the protections already? */ | |
1403 | if (pgprot_val(vma->vm_page_prot) != | |
3ed75eb8 | 1404 | pgprot_val(vm_get_page_prot(vm_flags))) |
4e950f6f AD |
1405 | return 0; |
1406 | ||
1407 | /* Specialty mapping? */ | |
4b6e1e37 | 1408 | if (vm_flags & VM_PFNMAP) |
4e950f6f AD |
1409 | return 0; |
1410 | ||
1411 | /* Can the mapping track the dirty pages? */ | |
1412 | return vma->vm_file && vma->vm_file->f_mapping && | |
1413 | mapping_cap_account_dirty(vma->vm_file->f_mapping); | |
1414 | } | |
1415 | ||
fc8744ad LT |
1416 | /* |
1417 | * We account for memory if it's a private writeable mapping, | |
5a6fe125 | 1418 | * not hugepages and VM_NORESERVE wasn't set. |
fc8744ad | 1419 | */ |
ca16d140 | 1420 | static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) |
fc8744ad | 1421 | { |
5a6fe125 MG |
1422 | /* |
1423 | * hugetlb has its own accounting separate from the core VM | |
1424 | * VM_HUGETLB may not be set yet so we cannot check for that flag. | |
1425 | */ | |
1426 | if (file && is_file_hugepages(file)) | |
1427 | return 0; | |
1428 | ||
fc8744ad LT |
1429 | return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; |
1430 | } | |
1431 | ||
0165ab44 | 1432 | unsigned long mmap_region(struct file *file, unsigned long addr, |
c22c0d63 | 1433 | unsigned long len, vm_flags_t vm_flags, unsigned long pgoff) |
0165ab44 MS |
1434 | { |
1435 | struct mm_struct *mm = current->mm; | |
1436 | struct vm_area_struct *vma, *prev; | |
1437 | int correct_wcount = 0; | |
1438 | int error; | |
1439 | struct rb_node **rb_link, *rb_parent; | |
1440 | unsigned long charged = 0; | |
496ad9aa | 1441 | struct inode *inode = file ? file_inode(file) : NULL; |
0165ab44 | 1442 | |
1da177e4 LT |
1443 | /* Clear old maps */ |
1444 | error = -ENOMEM; | |
1445 | munmap_back: | |
6597d783 | 1446 | if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) { |
1da177e4 LT |
1447 | if (do_munmap(mm, addr, len)) |
1448 | return -ENOMEM; | |
1449 | goto munmap_back; | |
1450 | } | |
1451 | ||
1452 | /* Check against address space limit. */ | |
119f657c | 1453 | if (!may_expand_vm(mm, len >> PAGE_SHIFT)) |
1da177e4 LT |
1454 | return -ENOMEM; |
1455 | ||
fc8744ad LT |
1456 | /* |
1457 | * Private writable mapping: check memory availability | |
1458 | */ | |
5a6fe125 | 1459 | if (accountable_mapping(file, vm_flags)) { |
fc8744ad | 1460 | charged = len >> PAGE_SHIFT; |
191c5424 | 1461 | if (security_vm_enough_memory_mm(mm, charged)) |
fc8744ad LT |
1462 | return -ENOMEM; |
1463 | vm_flags |= VM_ACCOUNT; | |
1da177e4 LT |
1464 | } |
1465 | ||
1466 | /* | |
de33c8db | 1467 | * Can we just expand an old mapping? |
1da177e4 | 1468 | */ |
de33c8db LT |
1469 | vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL); |
1470 | if (vma) | |
1471 | goto out; | |
1da177e4 LT |
1472 | |
1473 | /* | |
1474 | * Determine the object being mapped and call the appropriate | |
1475 | * specific mapper. the address has already been validated, but | |
1476 | * not unmapped, but the maps are removed from the list. | |
1477 | */ | |
c5e3b83e | 1478 | vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); |
1da177e4 LT |
1479 | if (!vma) { |
1480 | error = -ENOMEM; | |
1481 | goto unacct_error; | |
1482 | } | |
1da177e4 LT |
1483 | |
1484 | vma->vm_mm = mm; | |
1485 | vma->vm_start = addr; | |
1486 | vma->vm_end = addr + len; | |
1487 | vma->vm_flags = vm_flags; | |
3ed75eb8 | 1488 | vma->vm_page_prot = vm_get_page_prot(vm_flags); |
1da177e4 | 1489 | vma->vm_pgoff = pgoff; |
5beb4930 | 1490 | INIT_LIST_HEAD(&vma->anon_vma_chain); |
1da177e4 | 1491 | |
ce8fea7a HD |
1492 | error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */ |
1493 | ||
1da177e4 | 1494 | if (file) { |
1da177e4 LT |
1495 | if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP)) |
1496 | goto free_vma; | |
1497 | if (vm_flags & VM_DENYWRITE) { | |
1498 | error = deny_write_access(file); | |
1499 | if (error) | |
1500 | goto free_vma; | |
1501 | correct_wcount = 1; | |
1502 | } | |
cb0942b8 | 1503 | vma->vm_file = get_file(file); |
1da177e4 LT |
1504 | error = file->f_op->mmap(file, vma); |
1505 | if (error) | |
1506 | goto unmap_and_free_vma; | |
f8dbf0a7 HS |
1507 | |
1508 | /* Can addr have changed?? | |
1509 | * | |
1510 | * Answer: Yes, several device drivers can do it in their | |
1511 | * f_op->mmap method. -DaveM | |
2897b4d2 JK |
1512 | * Bug: If addr is changed, prev, rb_link, rb_parent should |
1513 | * be updated for vma_link() | |
f8dbf0a7 | 1514 | */ |
2897b4d2 JK |
1515 | WARN_ON_ONCE(addr != vma->vm_start); |
1516 | ||
f8dbf0a7 HS |
1517 | addr = vma->vm_start; |
1518 | pgoff = vma->vm_pgoff; | |
1519 | vm_flags = vma->vm_flags; | |
1da177e4 | 1520 | } else if (vm_flags & VM_SHARED) { |
835ee797 AV |
1521 | if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP))) |
1522 | goto free_vma; | |
1da177e4 LT |
1523 | error = shmem_zero_setup(vma); |
1524 | if (error) | |
1525 | goto free_vma; | |
1526 | } | |
1527 | ||
c9d0bf24 MD |
1528 | if (vma_wants_writenotify(vma)) { |
1529 | pgprot_t pprot = vma->vm_page_prot; | |
1530 | ||
1531 | /* Can vma->vm_page_prot have changed?? | |
1532 | * | |
1533 | * Answer: Yes, drivers may have changed it in their | |
1534 | * f_op->mmap method. | |
1535 | * | |
1536 | * Ensures that vmas marked as uncached stay that way. | |
1537 | */ | |
1ddd439e | 1538 | vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED); |
c9d0bf24 MD |
1539 | if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot))) |
1540 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
1541 | } | |
d08b3851 | 1542 | |
de33c8db LT |
1543 | vma_link(mm, vma, prev, rb_link, rb_parent); |
1544 | file = vma->vm_file; | |
4d3d5b41 ON |
1545 | |
1546 | /* Once vma denies write, undo our temporary denial count */ | |
1547 | if (correct_wcount) | |
1548 | atomic_inc(&inode->i_writecount); | |
1549 | out: | |
cdd6c482 | 1550 | perf_event_mmap(vma); |
0a4a9391 | 1551 | |
ab50b8ed | 1552 | vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT); |
1da177e4 | 1553 | if (vm_flags & VM_LOCKED) { |
bebeb3d6 ML |
1554 | if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) || |
1555 | vma == get_gate_vma(current->mm))) | |
06f9d8c2 | 1556 | mm->locked_vm += (len >> PAGE_SHIFT); |
bebeb3d6 ML |
1557 | else |
1558 | vma->vm_flags &= ~VM_LOCKED; | |
1559 | } | |
2b144498 | 1560 | |
c7a3a88c ON |
1561 | if (file) |
1562 | uprobe_mmap(vma); | |
2b144498 | 1563 | |
1da177e4 LT |
1564 | return addr; |
1565 | ||
1566 | unmap_and_free_vma: | |
1567 | if (correct_wcount) | |
1568 | atomic_inc(&inode->i_writecount); | |
1569 | vma->vm_file = NULL; | |
1570 | fput(file); | |
1571 | ||
1572 | /* Undo any partial mapping done by a device driver. */ | |
e0da382c HD |
1573 | unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); |
1574 | charged = 0; | |
1da177e4 LT |
1575 | free_vma: |
1576 | kmem_cache_free(vm_area_cachep, vma); | |
1577 | unacct_error: | |
1578 | if (charged) | |
1579 | vm_unacct_memory(charged); | |
1580 | return error; | |
1581 | } | |
1582 | ||
db4fbfb9 ML |
1583 | unsigned long unmapped_area(struct vm_unmapped_area_info *info) |
1584 | { | |
1585 | /* | |
1586 | * We implement the search by looking for an rbtree node that | |
1587 | * immediately follows a suitable gap. That is, | |
1588 | * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length; | |
1589 | * - gap_end = vma->vm_start >= info->low_limit + length; | |
1590 | * - gap_end - gap_start >= length | |
1591 | */ | |
1592 | ||
1593 | struct mm_struct *mm = current->mm; | |
1594 | struct vm_area_struct *vma; | |
1595 | unsigned long length, low_limit, high_limit, gap_start, gap_end; | |
1596 | ||
1597 | /* Adjust search length to account for worst case alignment overhead */ | |
1598 | length = info->length + info->align_mask; | |
1599 | if (length < info->length) | |
1600 | return -ENOMEM; | |
1601 | ||
1602 | /* Adjust search limits by the desired length */ | |
1603 | if (info->high_limit < length) | |
1604 | return -ENOMEM; | |
1605 | high_limit = info->high_limit - length; | |
1606 | ||
1607 | if (info->low_limit > high_limit) | |
1608 | return -ENOMEM; | |
1609 | low_limit = info->low_limit + length; | |
1610 | ||
1611 | /* Check if rbtree root looks promising */ | |
1612 | if (RB_EMPTY_ROOT(&mm->mm_rb)) | |
1613 | goto check_highest; | |
1614 | vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb); | |
1615 | if (vma->rb_subtree_gap < length) | |
1616 | goto check_highest; | |
1617 | ||
1618 | while (true) { | |
1619 | /* Visit left subtree if it looks promising */ | |
1620 | gap_end = vma->vm_start; | |
1621 | if (gap_end >= low_limit && vma->vm_rb.rb_left) { | |
1622 | struct vm_area_struct *left = | |
1623 | rb_entry(vma->vm_rb.rb_left, | |
1624 | struct vm_area_struct, vm_rb); | |
1625 | if (left->rb_subtree_gap >= length) { | |
1626 | vma = left; | |
1627 | continue; | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0; | |
1632 | check_current: | |
1633 | /* Check if current node has a suitable gap */ | |
1634 | if (gap_start > high_limit) | |
1635 | return -ENOMEM; | |
1636 | if (gap_end >= low_limit && gap_end - gap_start >= length) | |
1637 | goto found; | |
1638 | ||
1639 | /* Visit right subtree if it looks promising */ | |
1640 | if (vma->vm_rb.rb_right) { | |
1641 | struct vm_area_struct *right = | |
1642 | rb_entry(vma->vm_rb.rb_right, | |
1643 | struct vm_area_struct, vm_rb); | |
1644 | if (right->rb_subtree_gap >= length) { | |
1645 | vma = right; | |
1646 | continue; | |
1647 | } | |
1648 | } | |
1649 | ||
1650 | /* Go back up the rbtree to find next candidate node */ | |
1651 | while (true) { | |
1652 | struct rb_node *prev = &vma->vm_rb; | |
1653 | if (!rb_parent(prev)) | |
1654 | goto check_highest; | |
1655 | vma = rb_entry(rb_parent(prev), | |
1656 | struct vm_area_struct, vm_rb); | |
1657 | if (prev == vma->vm_rb.rb_left) { | |
1658 | gap_start = vma->vm_prev->vm_end; | |
1659 | gap_end = vma->vm_start; | |
1660 | goto check_current; | |
1661 | } | |
1662 | } | |
1663 | } | |
1664 | ||
1665 | check_highest: | |
1666 | /* Check highest gap, which does not precede any rbtree node */ | |
1667 | gap_start = mm->highest_vm_end; | |
1668 | gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */ | |
1669 | if (gap_start > high_limit) | |
1670 | return -ENOMEM; | |
1671 | ||
1672 | found: | |
1673 | /* We found a suitable gap. Clip it with the original low_limit. */ | |
1674 | if (gap_start < info->low_limit) | |
1675 | gap_start = info->low_limit; | |
1676 | ||
1677 | /* Adjust gap address to the desired alignment */ | |
1678 | gap_start += (info->align_offset - gap_start) & info->align_mask; | |
1679 | ||
1680 | VM_BUG_ON(gap_start + info->length > info->high_limit); | |
1681 | VM_BUG_ON(gap_start + info->length > gap_end); | |
1682 | return gap_start; | |
1683 | } | |
1684 | ||
1685 | unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) | |
1686 | { | |
1687 | struct mm_struct *mm = current->mm; | |
1688 | struct vm_area_struct *vma; | |
1689 | unsigned long length, low_limit, high_limit, gap_start, gap_end; | |
1690 | ||
1691 | /* Adjust search length to account for worst case alignment overhead */ | |
1692 | length = info->length + info->align_mask; | |
1693 | if (length < info->length) | |
1694 | return -ENOMEM; | |
1695 | ||
1696 | /* | |
1697 | * Adjust search limits by the desired length. | |
1698 | * See implementation comment at top of unmapped_area(). | |
1699 | */ | |
1700 | gap_end = info->high_limit; | |
1701 | if (gap_end < length) | |
1702 | return -ENOMEM; | |
1703 | high_limit = gap_end - length; | |
1704 | ||
1705 | if (info->low_limit > high_limit) | |
1706 | return -ENOMEM; | |
1707 | low_limit = info->low_limit + length; | |
1708 | ||
1709 | /* Check highest gap, which does not precede any rbtree node */ | |
1710 | gap_start = mm->highest_vm_end; | |
1711 | if (gap_start <= high_limit) | |
1712 | goto found_highest; | |
1713 | ||
1714 | /* Check if rbtree root looks promising */ | |
1715 | if (RB_EMPTY_ROOT(&mm->mm_rb)) | |
1716 | return -ENOMEM; | |
1717 | vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb); | |
1718 | if (vma->rb_subtree_gap < length) | |
1719 | return -ENOMEM; | |
1720 | ||
1721 | while (true) { | |
1722 | /* Visit right subtree if it looks promising */ | |
1723 | gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0; | |
1724 | if (gap_start <= high_limit && vma->vm_rb.rb_right) { | |
1725 | struct vm_area_struct *right = | |
1726 | rb_entry(vma->vm_rb.rb_right, | |
1727 | struct vm_area_struct, vm_rb); | |
1728 | if (right->rb_subtree_gap >= length) { | |
1729 | vma = right; | |
1730 | continue; | |
1731 | } | |
1732 | } | |
1733 | ||
1734 | check_current: | |
1735 | /* Check if current node has a suitable gap */ | |
1736 | gap_end = vma->vm_start; | |
1737 | if (gap_end < low_limit) | |
1738 | return -ENOMEM; | |
1739 | if (gap_start <= high_limit && gap_end - gap_start >= length) | |
1740 | goto found; | |
1741 | ||
1742 | /* Visit left subtree if it looks promising */ | |
1743 | if (vma->vm_rb.rb_left) { | |
1744 | struct vm_area_struct *left = | |
1745 | rb_entry(vma->vm_rb.rb_left, | |
1746 | struct vm_area_struct, vm_rb); | |
1747 | if (left->rb_subtree_gap >= length) { | |
1748 | vma = left; | |
1749 | continue; | |
1750 | } | |
1751 | } | |
1752 | ||
1753 | /* Go back up the rbtree to find next candidate node */ | |
1754 | while (true) { | |
1755 | struct rb_node *prev = &vma->vm_rb; | |
1756 | if (!rb_parent(prev)) | |
1757 | return -ENOMEM; | |
1758 | vma = rb_entry(rb_parent(prev), | |
1759 | struct vm_area_struct, vm_rb); | |
1760 | if (prev == vma->vm_rb.rb_right) { | |
1761 | gap_start = vma->vm_prev ? | |
1762 | vma->vm_prev->vm_end : 0; | |
1763 | goto check_current; | |
1764 | } | |
1765 | } | |
1766 | } | |
1767 | ||
1768 | found: | |
1769 | /* We found a suitable gap. Clip it with the original high_limit. */ | |
1770 | if (gap_end > info->high_limit) | |
1771 | gap_end = info->high_limit; | |
1772 | ||
1773 | found_highest: | |
1774 | /* Compute highest gap address at the desired alignment */ | |
1775 | gap_end -= info->length; | |
1776 | gap_end -= (gap_end - info->align_offset) & info->align_mask; | |
1777 | ||
1778 | VM_BUG_ON(gap_end < info->low_limit); | |
1779 | VM_BUG_ON(gap_end < gap_start); | |
1780 | return gap_end; | |
1781 | } | |
1782 | ||
1da177e4 LT |
1783 | /* Get an address range which is currently unmapped. |
1784 | * For shmat() with addr=0. | |
1785 | * | |
1786 | * Ugly calling convention alert: | |
1787 | * Return value with the low bits set means error value, | |
1788 | * ie | |
1789 | * if (ret & ~PAGE_MASK) | |
1790 | * error = ret; | |
1791 | * | |
1792 | * This function "knows" that -ENOMEM has the bits set. | |
1793 | */ | |
1794 | #ifndef HAVE_ARCH_UNMAPPED_AREA | |
1795 | unsigned long | |
1796 | arch_get_unmapped_area(struct file *filp, unsigned long addr, | |
1797 | unsigned long len, unsigned long pgoff, unsigned long flags) | |
1798 | { | |
1799 | struct mm_struct *mm = current->mm; | |
1800 | struct vm_area_struct *vma; | |
db4fbfb9 | 1801 | struct vm_unmapped_area_info info; |
1da177e4 LT |
1802 | |
1803 | if (len > TASK_SIZE) | |
1804 | return -ENOMEM; | |
1805 | ||
06abdfb4 BH |
1806 | if (flags & MAP_FIXED) |
1807 | return addr; | |
1808 | ||
1da177e4 LT |
1809 | if (addr) { |
1810 | addr = PAGE_ALIGN(addr); | |
1811 | vma = find_vma(mm, addr); | |
1812 | if (TASK_SIZE - len >= addr && | |
1813 | (!vma || addr + len <= vma->vm_start)) | |
1814 | return addr; | |
1815 | } | |
1da177e4 | 1816 | |
db4fbfb9 ML |
1817 | info.flags = 0; |
1818 | info.length = len; | |
1819 | info.low_limit = TASK_UNMAPPED_BASE; | |
1820 | info.high_limit = TASK_SIZE; | |
1821 | info.align_mask = 0; | |
1822 | return vm_unmapped_area(&info); | |
1da177e4 LT |
1823 | } |
1824 | #endif | |
1825 | ||
1363c3cd | 1826 | void arch_unmap_area(struct mm_struct *mm, unsigned long addr) |
1da177e4 LT |
1827 | { |
1828 | /* | |
1829 | * Is this a new hole at the lowest possible address? | |
1830 | */ | |
f44d2198 | 1831 | if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache) |
1363c3cd | 1832 | mm->free_area_cache = addr; |
1da177e4 LT |
1833 | } |
1834 | ||
1835 | /* | |
1836 | * This mmap-allocator allocates new areas top-down from below the | |
1837 | * stack's low limit (the base): | |
1838 | */ | |
1839 | #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN | |
1840 | unsigned long | |
1841 | arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, | |
1842 | const unsigned long len, const unsigned long pgoff, | |
1843 | const unsigned long flags) | |
1844 | { | |
1845 | struct vm_area_struct *vma; | |
1846 | struct mm_struct *mm = current->mm; | |
db4fbfb9 ML |
1847 | unsigned long addr = addr0; |
1848 | struct vm_unmapped_area_info info; | |
1da177e4 LT |
1849 | |
1850 | /* requested length too big for entire address space */ | |
1851 | if (len > TASK_SIZE) | |
1852 | return -ENOMEM; | |
1853 | ||
06abdfb4 BH |
1854 | if (flags & MAP_FIXED) |
1855 | return addr; | |
1856 | ||
1da177e4 LT |
1857 | /* requesting a specific address */ |
1858 | if (addr) { | |
1859 | addr = PAGE_ALIGN(addr); | |
1860 | vma = find_vma(mm, addr); | |
1861 | if (TASK_SIZE - len >= addr && | |
1862 | (!vma || addr + len <= vma->vm_start)) | |
1863 | return addr; | |
1864 | } | |
1865 | ||
db4fbfb9 ML |
1866 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
1867 | info.length = len; | |
1868 | info.low_limit = PAGE_SIZE; | |
1869 | info.high_limit = mm->mmap_base; | |
1870 | info.align_mask = 0; | |
1871 | addr = vm_unmapped_area(&info); | |
b716ad95 | 1872 | |
1da177e4 LT |
1873 | /* |
1874 | * A failed mmap() very likely causes application failure, | |
1875 | * so fall back to the bottom-up function here. This scenario | |
1876 | * can happen with large stack limits and large mmap() | |
1877 | * allocations. | |
1878 | */ | |
db4fbfb9 ML |
1879 | if (addr & ~PAGE_MASK) { |
1880 | VM_BUG_ON(addr != -ENOMEM); | |
1881 | info.flags = 0; | |
1882 | info.low_limit = TASK_UNMAPPED_BASE; | |
1883 | info.high_limit = TASK_SIZE; | |
1884 | addr = vm_unmapped_area(&info); | |
1885 | } | |
1da177e4 LT |
1886 | |
1887 | return addr; | |
1888 | } | |
1889 | #endif | |
1890 | ||
1363c3cd | 1891 | void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr) |
1da177e4 LT |
1892 | { |
1893 | /* | |
1894 | * Is this a new hole at the highest possible address? | |
1895 | */ | |
1363c3cd WW |
1896 | if (addr > mm->free_area_cache) |
1897 | mm->free_area_cache = addr; | |
1da177e4 LT |
1898 | |
1899 | /* dont allow allocations above current base */ | |
1363c3cd WW |
1900 | if (mm->free_area_cache > mm->mmap_base) |
1901 | mm->free_area_cache = mm->mmap_base; | |
1da177e4 LT |
1902 | } |
1903 | ||
1904 | unsigned long | |
1905 | get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, | |
1906 | unsigned long pgoff, unsigned long flags) | |
1907 | { | |
06abdfb4 BH |
1908 | unsigned long (*get_area)(struct file *, unsigned long, |
1909 | unsigned long, unsigned long, unsigned long); | |
1910 | ||
9206de95 AV |
1911 | unsigned long error = arch_mmap_check(addr, len, flags); |
1912 | if (error) | |
1913 | return error; | |
1914 | ||
1915 | /* Careful about overflows.. */ | |
1916 | if (len > TASK_SIZE) | |
1917 | return -ENOMEM; | |
1918 | ||
06abdfb4 BH |
1919 | get_area = current->mm->get_unmapped_area; |
1920 | if (file && file->f_op && file->f_op->get_unmapped_area) | |
1921 | get_area = file->f_op->get_unmapped_area; | |
1922 | addr = get_area(file, addr, len, pgoff, flags); | |
1923 | if (IS_ERR_VALUE(addr)) | |
1924 | return addr; | |
1da177e4 | 1925 | |
07ab67c8 LT |
1926 | if (addr > TASK_SIZE - len) |
1927 | return -ENOMEM; | |
1928 | if (addr & ~PAGE_MASK) | |
1929 | return -EINVAL; | |
06abdfb4 | 1930 | |
9ac4ed4b AV |
1931 | addr = arch_rebalance_pgtables(addr, len); |
1932 | error = security_mmap_addr(addr); | |
1933 | return error ? error : addr; | |
1da177e4 LT |
1934 | } |
1935 | ||
1936 | EXPORT_SYMBOL(get_unmapped_area); | |
1937 | ||
1938 | /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ | |
48aae425 | 1939 | struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) |
1da177e4 LT |
1940 | { |
1941 | struct vm_area_struct *vma = NULL; | |
1942 | ||
841e31e5 RM |
1943 | /* Check the cache first. */ |
1944 | /* (Cache hit rate is typically around 35%.) */ | |
b6a9b7f6 | 1945 | vma = ACCESS_ONCE(mm->mmap_cache); |
841e31e5 RM |
1946 | if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) { |
1947 | struct rb_node *rb_node; | |
1948 | ||
1949 | rb_node = mm->mm_rb.rb_node; | |
1950 | vma = NULL; | |
1951 | ||
1952 | while (rb_node) { | |
1953 | struct vm_area_struct *vma_tmp; | |
1954 | ||
1955 | vma_tmp = rb_entry(rb_node, | |
1956 | struct vm_area_struct, vm_rb); | |
1957 | ||
1958 | if (vma_tmp->vm_end > addr) { | |
1959 | vma = vma_tmp; | |
1960 | if (vma_tmp->vm_start <= addr) | |
1961 | break; | |
1962 | rb_node = rb_node->rb_left; | |
1963 | } else | |
1964 | rb_node = rb_node->rb_right; | |
1da177e4 | 1965 | } |
841e31e5 RM |
1966 | if (vma) |
1967 | mm->mmap_cache = vma; | |
1da177e4 LT |
1968 | } |
1969 | return vma; | |
1970 | } | |
1971 | ||
1972 | EXPORT_SYMBOL(find_vma); | |
1973 | ||
6bd4837d KM |
1974 | /* |
1975 | * Same as find_vma, but also return a pointer to the previous VMA in *pprev. | |
6bd4837d | 1976 | */ |
1da177e4 LT |
1977 | struct vm_area_struct * |
1978 | find_vma_prev(struct mm_struct *mm, unsigned long addr, | |
1979 | struct vm_area_struct **pprev) | |
1980 | { | |
6bd4837d | 1981 | struct vm_area_struct *vma; |
1da177e4 | 1982 | |
6bd4837d | 1983 | vma = find_vma(mm, addr); |
83cd904d MP |
1984 | if (vma) { |
1985 | *pprev = vma->vm_prev; | |
1986 | } else { | |
1987 | struct rb_node *rb_node = mm->mm_rb.rb_node; | |
1988 | *pprev = NULL; | |
1989 | while (rb_node) { | |
1990 | *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb); | |
1991 | rb_node = rb_node->rb_right; | |
1992 | } | |
1993 | } | |
6bd4837d | 1994 | return vma; |
1da177e4 LT |
1995 | } |
1996 | ||
1997 | /* | |
1998 | * Verify that the stack growth is acceptable and | |
1999 | * update accounting. This is shared with both the | |
2000 | * grow-up and grow-down cases. | |
2001 | */ | |
48aae425 | 2002 | static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow) |
1da177e4 LT |
2003 | { |
2004 | struct mm_struct *mm = vma->vm_mm; | |
2005 | struct rlimit *rlim = current->signal->rlim; | |
0d59a01b | 2006 | unsigned long new_start; |
1da177e4 LT |
2007 | |
2008 | /* address space limit tests */ | |
119f657c | 2009 | if (!may_expand_vm(mm, grow)) |
1da177e4 LT |
2010 | return -ENOMEM; |
2011 | ||
2012 | /* Stack limit test */ | |
59e99e5b | 2013 | if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur)) |
1da177e4 LT |
2014 | return -ENOMEM; |
2015 | ||
2016 | /* mlock limit tests */ | |
2017 | if (vma->vm_flags & VM_LOCKED) { | |
2018 | unsigned long locked; | |
2019 | unsigned long limit; | |
2020 | locked = mm->locked_vm + grow; | |
59e99e5b JS |
2021 | limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur); |
2022 | limit >>= PAGE_SHIFT; | |
1da177e4 LT |
2023 | if (locked > limit && !capable(CAP_IPC_LOCK)) |
2024 | return -ENOMEM; | |
2025 | } | |
2026 | ||
0d59a01b AL |
2027 | /* Check to ensure the stack will not grow into a hugetlb-only region */ |
2028 | new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : | |
2029 | vma->vm_end - size; | |
2030 | if (is_hugepage_only_range(vma->vm_mm, new_start, size)) | |
2031 | return -EFAULT; | |
2032 | ||
1da177e4 LT |
2033 | /* |
2034 | * Overcommit.. This must be the final test, as it will | |
2035 | * update security statistics. | |
2036 | */ | |
05fa199d | 2037 | if (security_vm_enough_memory_mm(mm, grow)) |
1da177e4 LT |
2038 | return -ENOMEM; |
2039 | ||
2040 | /* Ok, everything looks good - let it rip */ | |
1da177e4 LT |
2041 | if (vma->vm_flags & VM_LOCKED) |
2042 | mm->locked_vm += grow; | |
ab50b8ed | 2043 | vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow); |
1da177e4 LT |
2044 | return 0; |
2045 | } | |
2046 | ||
46dea3d0 | 2047 | #if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64) |
1da177e4 | 2048 | /* |
46dea3d0 HD |
2049 | * PA-RISC uses this for its stack; IA64 for its Register Backing Store. |
2050 | * vma is the last one with address > vma->vm_end. Have to extend vma. | |
1da177e4 | 2051 | */ |
46dea3d0 | 2052 | int expand_upwards(struct vm_area_struct *vma, unsigned long address) |
1da177e4 LT |
2053 | { |
2054 | int error; | |
2055 | ||
2056 | if (!(vma->vm_flags & VM_GROWSUP)) | |
2057 | return -EFAULT; | |
2058 | ||
2059 | /* | |
2060 | * We must make sure the anon_vma is allocated | |
2061 | * so that the anon_vma locking is not a noop. | |
2062 | */ | |
2063 | if (unlikely(anon_vma_prepare(vma))) | |
2064 | return -ENOMEM; | |
bb4a340e | 2065 | vma_lock_anon_vma(vma); |
1da177e4 LT |
2066 | |
2067 | /* | |
2068 | * vma->vm_start/vm_end cannot change under us because the caller | |
2069 | * is required to hold the mmap_sem in read mode. We need the | |
2070 | * anon_vma lock to serialize against concurrent expand_stacks. | |
06b32f3a | 2071 | * Also guard against wrapping around to address 0. |
1da177e4 | 2072 | */ |
06b32f3a HD |
2073 | if (address < PAGE_ALIGN(address+4)) |
2074 | address = PAGE_ALIGN(address+4); | |
2075 | else { | |
bb4a340e | 2076 | vma_unlock_anon_vma(vma); |
06b32f3a HD |
2077 | return -ENOMEM; |
2078 | } | |
1da177e4 LT |
2079 | error = 0; |
2080 | ||
2081 | /* Somebody else might have raced and expanded it already */ | |
2082 | if (address > vma->vm_end) { | |
2083 | unsigned long size, grow; | |
2084 | ||
2085 | size = address - vma->vm_start; | |
2086 | grow = (address - vma->vm_end) >> PAGE_SHIFT; | |
2087 | ||
42c36f63 HD |
2088 | error = -ENOMEM; |
2089 | if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) { | |
2090 | error = acct_stack_growth(vma, size, grow); | |
2091 | if (!error) { | |
4128997b ML |
2092 | /* |
2093 | * vma_gap_update() doesn't support concurrent | |
2094 | * updates, but we only hold a shared mmap_sem | |
2095 | * lock here, so we need to protect against | |
2096 | * concurrent vma expansions. | |
2097 | * vma_lock_anon_vma() doesn't help here, as | |
2098 | * we don't guarantee that all growable vmas | |
2099 | * in a mm share the same root anon vma. | |
2100 | * So, we reuse mm->page_table_lock to guard | |
2101 | * against concurrent vma expansions. | |
2102 | */ | |
2103 | spin_lock(&vma->vm_mm->page_table_lock); | |
bf181b9f | 2104 | anon_vma_interval_tree_pre_update_vma(vma); |
42c36f63 | 2105 | vma->vm_end = address; |
bf181b9f | 2106 | anon_vma_interval_tree_post_update_vma(vma); |
d3737187 ML |
2107 | if (vma->vm_next) |
2108 | vma_gap_update(vma->vm_next); | |
2109 | else | |
2110 | vma->vm_mm->highest_vm_end = address; | |
4128997b ML |
2111 | spin_unlock(&vma->vm_mm->page_table_lock); |
2112 | ||
42c36f63 HD |
2113 | perf_event_mmap(vma); |
2114 | } | |
3af9e859 | 2115 | } |
1da177e4 | 2116 | } |
bb4a340e | 2117 | vma_unlock_anon_vma(vma); |
b15d00b6 | 2118 | khugepaged_enter_vma_merge(vma); |
ed8ea815 | 2119 | validate_mm(vma->vm_mm); |
1da177e4 LT |
2120 | return error; |
2121 | } | |
46dea3d0 HD |
2122 | #endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */ |
2123 | ||
1da177e4 LT |
2124 | /* |
2125 | * vma is the first one with address < vma->vm_start. Have to extend vma. | |
2126 | */ | |
d05f3169 | 2127 | int expand_downwards(struct vm_area_struct *vma, |
b6a2fea3 | 2128 | unsigned long address) |
1da177e4 LT |
2129 | { |
2130 | int error; | |
2131 | ||
2132 | /* | |
2133 | * We must make sure the anon_vma is allocated | |
2134 | * so that the anon_vma locking is not a noop. | |
2135 | */ | |
2136 | if (unlikely(anon_vma_prepare(vma))) | |
2137 | return -ENOMEM; | |
8869477a EP |
2138 | |
2139 | address &= PAGE_MASK; | |
e5467859 | 2140 | error = security_mmap_addr(address); |
8869477a EP |
2141 | if (error) |
2142 | return error; | |
2143 | ||
bb4a340e | 2144 | vma_lock_anon_vma(vma); |
1da177e4 LT |
2145 | |
2146 | /* | |
2147 | * vma->vm_start/vm_end cannot change under us because the caller | |
2148 | * is required to hold the mmap_sem in read mode. We need the | |
2149 | * anon_vma lock to serialize against concurrent expand_stacks. | |
2150 | */ | |
1da177e4 LT |
2151 | |
2152 | /* Somebody else might have raced and expanded it already */ | |
2153 | if (address < vma->vm_start) { | |
2154 | unsigned long size, grow; | |
2155 | ||
2156 | size = vma->vm_end - address; | |
2157 | grow = (vma->vm_start - address) >> PAGE_SHIFT; | |
2158 | ||
a626ca6a LT |
2159 | error = -ENOMEM; |
2160 | if (grow <= vma->vm_pgoff) { | |
2161 | error = acct_stack_growth(vma, size, grow); | |
2162 | if (!error) { | |
4128997b ML |
2163 | /* |
2164 | * vma_gap_update() doesn't support concurrent | |
2165 | * updates, but we only hold a shared mmap_sem | |
2166 | * lock here, so we need to protect against | |
2167 | * concurrent vma expansions. | |
2168 | * vma_lock_anon_vma() doesn't help here, as | |
2169 | * we don't guarantee that all growable vmas | |
2170 | * in a mm share the same root anon vma. | |
2171 | * So, we reuse mm->page_table_lock to guard | |
2172 | * against concurrent vma expansions. | |
2173 | */ | |
2174 | spin_lock(&vma->vm_mm->page_table_lock); | |
bf181b9f | 2175 | anon_vma_interval_tree_pre_update_vma(vma); |
a626ca6a LT |
2176 | vma->vm_start = address; |
2177 | vma->vm_pgoff -= grow; | |
bf181b9f | 2178 | anon_vma_interval_tree_post_update_vma(vma); |
d3737187 | 2179 | vma_gap_update(vma); |
4128997b ML |
2180 | spin_unlock(&vma->vm_mm->page_table_lock); |
2181 | ||
a626ca6a LT |
2182 | perf_event_mmap(vma); |
2183 | } | |
1da177e4 LT |
2184 | } |
2185 | } | |
bb4a340e | 2186 | vma_unlock_anon_vma(vma); |
b15d00b6 | 2187 | khugepaged_enter_vma_merge(vma); |
ed8ea815 | 2188 | validate_mm(vma->vm_mm); |
1da177e4 LT |
2189 | return error; |
2190 | } | |
2191 | ||
09884964 LT |
2192 | /* |
2193 | * Note how expand_stack() refuses to expand the stack all the way to | |
2194 | * abut the next virtual mapping, *unless* that mapping itself is also | |
2195 | * a stack mapping. We want to leave room for a guard page, after all | |
2196 | * (the guard page itself is not added here, that is done by the | |
2197 | * actual page faulting logic) | |
2198 | * | |
2199 | * This matches the behavior of the guard page logic (see mm/memory.c: | |
2200 | * check_stack_guard_page()), which only allows the guard page to be | |
2201 | * removed under these circumstances. | |
2202 | */ | |
b6a2fea3 OW |
2203 | #ifdef CONFIG_STACK_GROWSUP |
2204 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | |
2205 | { | |
09884964 LT |
2206 | struct vm_area_struct *next; |
2207 | ||
2208 | address &= PAGE_MASK; | |
2209 | next = vma->vm_next; | |
2210 | if (next && next->vm_start == address + PAGE_SIZE) { | |
2211 | if (!(next->vm_flags & VM_GROWSUP)) | |
2212 | return -ENOMEM; | |
2213 | } | |
b6a2fea3 OW |
2214 | return expand_upwards(vma, address); |
2215 | } | |
2216 | ||
2217 | struct vm_area_struct * | |
2218 | find_extend_vma(struct mm_struct *mm, unsigned long addr) | |
2219 | { | |
2220 | struct vm_area_struct *vma, *prev; | |
2221 | ||
2222 | addr &= PAGE_MASK; | |
2223 | vma = find_vma_prev(mm, addr, &prev); | |
2224 | if (vma && (vma->vm_start <= addr)) | |
2225 | return vma; | |
1c127185 | 2226 | if (!prev || expand_stack(prev, addr)) |
b6a2fea3 | 2227 | return NULL; |
cea10a19 ML |
2228 | if (prev->vm_flags & VM_LOCKED) |
2229 | __mlock_vma_pages_range(prev, addr, prev->vm_end, NULL); | |
b6a2fea3 OW |
2230 | return prev; |
2231 | } | |
2232 | #else | |
2233 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | |
2234 | { | |
09884964 LT |
2235 | struct vm_area_struct *prev; |
2236 | ||
2237 | address &= PAGE_MASK; | |
2238 | prev = vma->vm_prev; | |
2239 | if (prev && prev->vm_end == address) { | |
2240 | if (!(prev->vm_flags & VM_GROWSDOWN)) | |
2241 | return -ENOMEM; | |
2242 | } | |
b6a2fea3 OW |
2243 | return expand_downwards(vma, address); |
2244 | } | |
2245 | ||
1da177e4 LT |
2246 | struct vm_area_struct * |
2247 | find_extend_vma(struct mm_struct * mm, unsigned long addr) | |
2248 | { | |
2249 | struct vm_area_struct * vma; | |
2250 | unsigned long start; | |
2251 | ||
2252 | addr &= PAGE_MASK; | |
2253 | vma = find_vma(mm,addr); | |
2254 | if (!vma) | |
2255 | return NULL; | |
2256 | if (vma->vm_start <= addr) | |
2257 | return vma; | |
2258 | if (!(vma->vm_flags & VM_GROWSDOWN)) | |
2259 | return NULL; | |
2260 | start = vma->vm_start; | |
2261 | if (expand_stack(vma, addr)) | |
2262 | return NULL; | |
cea10a19 ML |
2263 | if (vma->vm_flags & VM_LOCKED) |
2264 | __mlock_vma_pages_range(vma, addr, start, NULL); | |
1da177e4 LT |
2265 | return vma; |
2266 | } | |
2267 | #endif | |
2268 | ||
1da177e4 | 2269 | /* |
2c0b3814 | 2270 | * Ok - we have the memory areas we should free on the vma list, |
1da177e4 | 2271 | * so release them, and do the vma updates. |
2c0b3814 HD |
2272 | * |
2273 | * Called with the mm semaphore held. | |
1da177e4 | 2274 | */ |
2c0b3814 | 2275 | static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma) |
1da177e4 | 2276 | { |
4f74d2c8 LT |
2277 | unsigned long nr_accounted = 0; |
2278 | ||
365e9c87 HD |
2279 | /* Update high watermark before we lower total_vm */ |
2280 | update_hiwater_vm(mm); | |
1da177e4 | 2281 | do { |
2c0b3814 HD |
2282 | long nrpages = vma_pages(vma); |
2283 | ||
4f74d2c8 LT |
2284 | if (vma->vm_flags & VM_ACCOUNT) |
2285 | nr_accounted += nrpages; | |
2c0b3814 | 2286 | vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages); |
a8fb5618 | 2287 | vma = remove_vma(vma); |
146425a3 | 2288 | } while (vma); |
4f74d2c8 | 2289 | vm_unacct_memory(nr_accounted); |
1da177e4 LT |
2290 | validate_mm(mm); |
2291 | } | |
2292 | ||
2293 | /* | |
2294 | * Get rid of page table information in the indicated region. | |
2295 | * | |
f10df686 | 2296 | * Called with the mm semaphore held. |
1da177e4 LT |
2297 | */ |
2298 | static void unmap_region(struct mm_struct *mm, | |
e0da382c HD |
2299 | struct vm_area_struct *vma, struct vm_area_struct *prev, |
2300 | unsigned long start, unsigned long end) | |
1da177e4 | 2301 | { |
e0da382c | 2302 | struct vm_area_struct *next = prev? prev->vm_next: mm->mmap; |
d16dfc55 | 2303 | struct mmu_gather tlb; |
1da177e4 LT |
2304 | |
2305 | lru_add_drain(); | |
d16dfc55 | 2306 | tlb_gather_mmu(&tlb, mm, 0); |
365e9c87 | 2307 | update_hiwater_rss(mm); |
4f74d2c8 | 2308 | unmap_vmas(&tlb, vma, start, end); |
d16dfc55 | 2309 | free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, |
6ee8630e | 2310 | next ? next->vm_start : USER_PGTABLES_CEILING); |
d16dfc55 | 2311 | tlb_finish_mmu(&tlb, start, end); |
1da177e4 LT |
2312 | } |
2313 | ||
2314 | /* | |
2315 | * Create a list of vma's touched by the unmap, removing them from the mm's | |
2316 | * vma list as we go.. | |
2317 | */ | |
2318 | static void | |
2319 | detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, | |
2320 | struct vm_area_struct *prev, unsigned long end) | |
2321 | { | |
2322 | struct vm_area_struct **insertion_point; | |
2323 | struct vm_area_struct *tail_vma = NULL; | |
1363c3cd | 2324 | unsigned long addr; |
1da177e4 LT |
2325 | |
2326 | insertion_point = (prev ? &prev->vm_next : &mm->mmap); | |
297c5eee | 2327 | vma->vm_prev = NULL; |
1da177e4 | 2328 | do { |
d3737187 | 2329 | vma_rb_erase(vma, &mm->mm_rb); |
1da177e4 LT |
2330 | mm->map_count--; |
2331 | tail_vma = vma; | |
2332 | vma = vma->vm_next; | |
2333 | } while (vma && vma->vm_start < end); | |
2334 | *insertion_point = vma; | |
d3737187 | 2335 | if (vma) { |
297c5eee | 2336 | vma->vm_prev = prev; |
d3737187 ML |
2337 | vma_gap_update(vma); |
2338 | } else | |
2339 | mm->highest_vm_end = prev ? prev->vm_end : 0; | |
1da177e4 | 2340 | tail_vma->vm_next = NULL; |
1363c3cd WW |
2341 | if (mm->unmap_area == arch_unmap_area) |
2342 | addr = prev ? prev->vm_end : mm->mmap_base; | |
2343 | else | |
2344 | addr = vma ? vma->vm_start : mm->mmap_base; | |
2345 | mm->unmap_area(mm, addr); | |
1da177e4 LT |
2346 | mm->mmap_cache = NULL; /* Kill the cache. */ |
2347 | } | |
2348 | ||
2349 | /* | |
659ace58 KM |
2350 | * __split_vma() bypasses sysctl_max_map_count checking. We use this on the |
2351 | * munmap path where it doesn't make sense to fail. | |
1da177e4 | 2352 | */ |
659ace58 | 2353 | static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma, |
1da177e4 LT |
2354 | unsigned long addr, int new_below) |
2355 | { | |
2356 | struct mempolicy *pol; | |
2357 | struct vm_area_struct *new; | |
5beb4930 | 2358 | int err = -ENOMEM; |
1da177e4 | 2359 | |
a5516438 AK |
2360 | if (is_vm_hugetlb_page(vma) && (addr & |
2361 | ~(huge_page_mask(hstate_vma(vma))))) | |
1da177e4 LT |
2362 | return -EINVAL; |
2363 | ||
e94b1766 | 2364 | new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
1da177e4 | 2365 | if (!new) |
5beb4930 | 2366 | goto out_err; |
1da177e4 LT |
2367 | |
2368 | /* most fields are the same, copy all, and then fixup */ | |
2369 | *new = *vma; | |
2370 | ||
5beb4930 RR |
2371 | INIT_LIST_HEAD(&new->anon_vma_chain); |
2372 | ||
1da177e4 LT |
2373 | if (new_below) |
2374 | new->vm_end = addr; | |
2375 | else { | |
2376 | new->vm_start = addr; | |
2377 | new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT); | |
2378 | } | |
2379 | ||
846a16bf | 2380 | pol = mpol_dup(vma_policy(vma)); |
1da177e4 | 2381 | if (IS_ERR(pol)) { |
5beb4930 RR |
2382 | err = PTR_ERR(pol); |
2383 | goto out_free_vma; | |
1da177e4 LT |
2384 | } |
2385 | vma_set_policy(new, pol); | |
2386 | ||
5beb4930 RR |
2387 | if (anon_vma_clone(new, vma)) |
2388 | goto out_free_mpol; | |
2389 | ||
e9714acf | 2390 | if (new->vm_file) |
1da177e4 LT |
2391 | get_file(new->vm_file); |
2392 | ||
2393 | if (new->vm_ops && new->vm_ops->open) | |
2394 | new->vm_ops->open(new); | |
2395 | ||
2396 | if (new_below) | |
5beb4930 | 2397 | err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff + |
1da177e4 LT |
2398 | ((addr - new->vm_start) >> PAGE_SHIFT), new); |
2399 | else | |
5beb4930 | 2400 | err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new); |
1da177e4 | 2401 | |
5beb4930 RR |
2402 | /* Success. */ |
2403 | if (!err) | |
2404 | return 0; | |
2405 | ||
2406 | /* Clean everything up if vma_adjust failed. */ | |
58927533 RR |
2407 | if (new->vm_ops && new->vm_ops->close) |
2408 | new->vm_ops->close(new); | |
e9714acf | 2409 | if (new->vm_file) |
5beb4930 | 2410 | fput(new->vm_file); |
2aeadc30 | 2411 | unlink_anon_vmas(new); |
5beb4930 RR |
2412 | out_free_mpol: |
2413 | mpol_put(pol); | |
2414 | out_free_vma: | |
2415 | kmem_cache_free(vm_area_cachep, new); | |
2416 | out_err: | |
2417 | return err; | |
1da177e4 LT |
2418 | } |
2419 | ||
659ace58 KM |
2420 | /* |
2421 | * Split a vma into two pieces at address 'addr', a new vma is allocated | |
2422 | * either for the first part or the tail. | |
2423 | */ | |
2424 | int split_vma(struct mm_struct *mm, struct vm_area_struct *vma, | |
2425 | unsigned long addr, int new_below) | |
2426 | { | |
2427 | if (mm->map_count >= sysctl_max_map_count) | |
2428 | return -ENOMEM; | |
2429 | ||
2430 | return __split_vma(mm, vma, addr, new_below); | |
2431 | } | |
2432 | ||
1da177e4 LT |
2433 | /* Munmap is split into 2 main parts -- this part which finds |
2434 | * what needs doing, and the areas themselves, which do the | |
2435 | * work. This now handles partial unmappings. | |
2436 | * Jeremy Fitzhardinge <jeremy@goop.org> | |
2437 | */ | |
2438 | int do_munmap(struct mm_struct *mm, unsigned long start, size_t len) | |
2439 | { | |
2440 | unsigned long end; | |
146425a3 | 2441 | struct vm_area_struct *vma, *prev, *last; |
1da177e4 LT |
2442 | |
2443 | if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start) | |
2444 | return -EINVAL; | |
2445 | ||
2446 | if ((len = PAGE_ALIGN(len)) == 0) | |
2447 | return -EINVAL; | |
2448 | ||
2449 | /* Find the first overlapping VMA */ | |
9be34c9d | 2450 | vma = find_vma(mm, start); |
146425a3 | 2451 | if (!vma) |
1da177e4 | 2452 | return 0; |
9be34c9d | 2453 | prev = vma->vm_prev; |
146425a3 | 2454 | /* we have start < vma->vm_end */ |
1da177e4 LT |
2455 | |
2456 | /* if it doesn't overlap, we have nothing.. */ | |
2457 | end = start + len; | |
146425a3 | 2458 | if (vma->vm_start >= end) |
1da177e4 LT |
2459 | return 0; |
2460 | ||
2461 | /* | |
2462 | * If we need to split any vma, do it now to save pain later. | |
2463 | * | |
2464 | * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially | |
2465 | * unmapped vm_area_struct will remain in use: so lower split_vma | |
2466 | * places tmp vma above, and higher split_vma places tmp vma below. | |
2467 | */ | |
146425a3 | 2468 | if (start > vma->vm_start) { |
659ace58 KM |
2469 | int error; |
2470 | ||
2471 | /* | |
2472 | * Make sure that map_count on return from munmap() will | |
2473 | * not exceed its limit; but let map_count go just above | |
2474 | * its limit temporarily, to help free resources as expected. | |
2475 | */ | |
2476 | if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) | |
2477 | return -ENOMEM; | |
2478 | ||
2479 | error = __split_vma(mm, vma, start, 0); | |
1da177e4 LT |
2480 | if (error) |
2481 | return error; | |
146425a3 | 2482 | prev = vma; |
1da177e4 LT |
2483 | } |
2484 | ||
2485 | /* Does it split the last one? */ | |
2486 | last = find_vma(mm, end); | |
2487 | if (last && end > last->vm_start) { | |
659ace58 | 2488 | int error = __split_vma(mm, last, end, 1); |
1da177e4 LT |
2489 | if (error) |
2490 | return error; | |
2491 | } | |
146425a3 | 2492 | vma = prev? prev->vm_next: mm->mmap; |
1da177e4 | 2493 | |
ba470de4 RR |
2494 | /* |
2495 | * unlock any mlock()ed ranges before detaching vmas | |
2496 | */ | |
2497 | if (mm->locked_vm) { | |
2498 | struct vm_area_struct *tmp = vma; | |
2499 | while (tmp && tmp->vm_start < end) { | |
2500 | if (tmp->vm_flags & VM_LOCKED) { | |
2501 | mm->locked_vm -= vma_pages(tmp); | |
2502 | munlock_vma_pages_all(tmp); | |
2503 | } | |
2504 | tmp = tmp->vm_next; | |
2505 | } | |
2506 | } | |
2507 | ||
1da177e4 LT |
2508 | /* |
2509 | * Remove the vma's, and unmap the actual pages | |
2510 | */ | |
146425a3 HD |
2511 | detach_vmas_to_be_unmapped(mm, vma, prev, end); |
2512 | unmap_region(mm, vma, prev, start, end); | |
1da177e4 LT |
2513 | |
2514 | /* Fix up all other VM information */ | |
2c0b3814 | 2515 | remove_vma_list(mm, vma); |
1da177e4 LT |
2516 | |
2517 | return 0; | |
2518 | } | |
1da177e4 | 2519 | |
bfce281c | 2520 | int vm_munmap(unsigned long start, size_t len) |
1da177e4 LT |
2521 | { |
2522 | int ret; | |
bfce281c | 2523 | struct mm_struct *mm = current->mm; |
1da177e4 LT |
2524 | |
2525 | down_write(&mm->mmap_sem); | |
a46ef99d | 2526 | ret = do_munmap(mm, start, len); |
1da177e4 LT |
2527 | up_write(&mm->mmap_sem); |
2528 | return ret; | |
2529 | } | |
a46ef99d LT |
2530 | EXPORT_SYMBOL(vm_munmap); |
2531 | ||
2532 | SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) | |
2533 | { | |
2534 | profile_munmap(addr); | |
bfce281c | 2535 | return vm_munmap(addr, len); |
a46ef99d | 2536 | } |
1da177e4 LT |
2537 | |
2538 | static inline void verify_mm_writelocked(struct mm_struct *mm) | |
2539 | { | |
a241ec65 | 2540 | #ifdef CONFIG_DEBUG_VM |
1da177e4 LT |
2541 | if (unlikely(down_read_trylock(&mm->mmap_sem))) { |
2542 | WARN_ON(1); | |
2543 | up_read(&mm->mmap_sem); | |
2544 | } | |
2545 | #endif | |
2546 | } | |
2547 | ||
2548 | /* | |
2549 | * this is really a simplified "do_mmap". it only handles | |
2550 | * anonymous maps. eventually we may be able to do some | |
2551 | * brk-specific accounting here. | |
2552 | */ | |
e4eb1ff6 | 2553 | static unsigned long do_brk(unsigned long addr, unsigned long len) |
1da177e4 LT |
2554 | { |
2555 | struct mm_struct * mm = current->mm; | |
2556 | struct vm_area_struct * vma, * prev; | |
2557 | unsigned long flags; | |
2558 | struct rb_node ** rb_link, * rb_parent; | |
2559 | pgoff_t pgoff = addr >> PAGE_SHIFT; | |
3a459756 | 2560 | int error; |
1da177e4 LT |
2561 | |
2562 | len = PAGE_ALIGN(len); | |
2563 | if (!len) | |
2564 | return addr; | |
2565 | ||
3a459756 KK |
2566 | flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags; |
2567 | ||
2c6a1016 AV |
2568 | error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED); |
2569 | if (error & ~PAGE_MASK) | |
3a459756 KK |
2570 | return error; |
2571 | ||
1da177e4 LT |
2572 | /* |
2573 | * mlock MCL_FUTURE? | |
2574 | */ | |
2575 | if (mm->def_flags & VM_LOCKED) { | |
2576 | unsigned long locked, lock_limit; | |
93ea1d0a CW |
2577 | locked = len >> PAGE_SHIFT; |
2578 | locked += mm->locked_vm; | |
59e99e5b | 2579 | lock_limit = rlimit(RLIMIT_MEMLOCK); |
93ea1d0a | 2580 | lock_limit >>= PAGE_SHIFT; |
1da177e4 LT |
2581 | if (locked > lock_limit && !capable(CAP_IPC_LOCK)) |
2582 | return -EAGAIN; | |
2583 | } | |
2584 | ||
2585 | /* | |
2586 | * mm->mmap_sem is required to protect against another thread | |
2587 | * changing the mappings in case we sleep. | |
2588 | */ | |
2589 | verify_mm_writelocked(mm); | |
2590 | ||
2591 | /* | |
2592 | * Clear old maps. this also does some error checking for us | |
2593 | */ | |
2594 | munmap_back: | |
6597d783 | 2595 | if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) { |
1da177e4 LT |
2596 | if (do_munmap(mm, addr, len)) |
2597 | return -ENOMEM; | |
2598 | goto munmap_back; | |
2599 | } | |
2600 | ||
2601 | /* Check against address space limits *after* clearing old maps... */ | |
119f657c | 2602 | if (!may_expand_vm(mm, len >> PAGE_SHIFT)) |
1da177e4 LT |
2603 | return -ENOMEM; |
2604 | ||
2605 | if (mm->map_count > sysctl_max_map_count) | |
2606 | return -ENOMEM; | |
2607 | ||
191c5424 | 2608 | if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) |
1da177e4 LT |
2609 | return -ENOMEM; |
2610 | ||
1da177e4 | 2611 | /* Can we just expand an old private anonymous mapping? */ |
ba470de4 RR |
2612 | vma = vma_merge(mm, prev, addr, addr + len, flags, |
2613 | NULL, NULL, pgoff, NULL); | |
2614 | if (vma) | |
1da177e4 LT |
2615 | goto out; |
2616 | ||
2617 | /* | |
2618 | * create a vma struct for an anonymous mapping | |
2619 | */ | |
c5e3b83e | 2620 | vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); |
1da177e4 LT |
2621 | if (!vma) { |
2622 | vm_unacct_memory(len >> PAGE_SHIFT); | |
2623 | return -ENOMEM; | |
2624 | } | |
1da177e4 | 2625 | |
5beb4930 | 2626 | INIT_LIST_HEAD(&vma->anon_vma_chain); |
1da177e4 LT |
2627 | vma->vm_mm = mm; |
2628 | vma->vm_start = addr; | |
2629 | vma->vm_end = addr + len; | |
2630 | vma->vm_pgoff = pgoff; | |
2631 | vma->vm_flags = flags; | |
3ed75eb8 | 2632 | vma->vm_page_prot = vm_get_page_prot(flags); |
1da177e4 LT |
2633 | vma_link(mm, vma, prev, rb_link, rb_parent); |
2634 | out: | |
3af9e859 | 2635 | perf_event_mmap(vma); |
1da177e4 | 2636 | mm->total_vm += len >> PAGE_SHIFT; |
128557ff ML |
2637 | if (flags & VM_LOCKED) |
2638 | mm->locked_vm += (len >> PAGE_SHIFT); | |
1da177e4 LT |
2639 | return addr; |
2640 | } | |
2641 | ||
e4eb1ff6 LT |
2642 | unsigned long vm_brk(unsigned long addr, unsigned long len) |
2643 | { | |
2644 | struct mm_struct *mm = current->mm; | |
2645 | unsigned long ret; | |
128557ff | 2646 | bool populate; |
e4eb1ff6 LT |
2647 | |
2648 | down_write(&mm->mmap_sem); | |
2649 | ret = do_brk(addr, len); | |
128557ff | 2650 | populate = ((mm->def_flags & VM_LOCKED) != 0); |
e4eb1ff6 | 2651 | up_write(&mm->mmap_sem); |
128557ff ML |
2652 | if (populate) |
2653 | mm_populate(addr, len); | |
e4eb1ff6 LT |
2654 | return ret; |
2655 | } | |
2656 | EXPORT_SYMBOL(vm_brk); | |
1da177e4 LT |
2657 | |
2658 | /* Release all mmaps. */ | |
2659 | void exit_mmap(struct mm_struct *mm) | |
2660 | { | |
d16dfc55 | 2661 | struct mmu_gather tlb; |
ba470de4 | 2662 | struct vm_area_struct *vma; |
1da177e4 LT |
2663 | unsigned long nr_accounted = 0; |
2664 | ||
d6dd61c8 | 2665 | /* mm's last user has gone, and its about to be pulled down */ |
cddb8a5c | 2666 | mmu_notifier_release(mm); |
d6dd61c8 | 2667 | |
ba470de4 RR |
2668 | if (mm->locked_vm) { |
2669 | vma = mm->mmap; | |
2670 | while (vma) { | |
2671 | if (vma->vm_flags & VM_LOCKED) | |
2672 | munlock_vma_pages_all(vma); | |
2673 | vma = vma->vm_next; | |
2674 | } | |
2675 | } | |
9480c53e JF |
2676 | |
2677 | arch_exit_mmap(mm); | |
2678 | ||
ba470de4 | 2679 | vma = mm->mmap; |
9480c53e JF |
2680 | if (!vma) /* Can happen if dup_mmap() received an OOM */ |
2681 | return; | |
2682 | ||
1da177e4 | 2683 | lru_add_drain(); |
1da177e4 | 2684 | flush_cache_mm(mm); |
d16dfc55 | 2685 | tlb_gather_mmu(&tlb, mm, 1); |
901608d9 | 2686 | /* update_hiwater_rss(mm) here? but nobody should be looking */ |
e0da382c | 2687 | /* Use -1 here to ensure all VMAs in the mm are unmapped */ |
4f74d2c8 | 2688 | unmap_vmas(&tlb, vma, 0, -1); |
9ba69294 | 2689 | |
6ee8630e | 2690 | free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); |
853f5e26 | 2691 | tlb_finish_mmu(&tlb, 0, -1); |
1da177e4 | 2692 | |
1da177e4 | 2693 | /* |
8f4f8c16 HD |
2694 | * Walk the list again, actually closing and freeing it, |
2695 | * with preemption enabled, without holding any MM locks. | |
1da177e4 | 2696 | */ |
4f74d2c8 LT |
2697 | while (vma) { |
2698 | if (vma->vm_flags & VM_ACCOUNT) | |
2699 | nr_accounted += vma_pages(vma); | |
a8fb5618 | 2700 | vma = remove_vma(vma); |
4f74d2c8 LT |
2701 | } |
2702 | vm_unacct_memory(nr_accounted); | |
e0da382c | 2703 | |
f9aed62a | 2704 | WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT); |
1da177e4 LT |
2705 | } |
2706 | ||
2707 | /* Insert vm structure into process list sorted by address | |
2708 | * and into the inode's i_mmap tree. If vm_file is non-NULL | |
3d48ae45 | 2709 | * then i_mmap_mutex is taken here. |
1da177e4 | 2710 | */ |
6597d783 | 2711 | int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma) |
1da177e4 | 2712 | { |
6597d783 HD |
2713 | struct vm_area_struct *prev; |
2714 | struct rb_node **rb_link, *rb_parent; | |
1da177e4 LT |
2715 | |
2716 | /* | |
2717 | * The vm_pgoff of a purely anonymous vma should be irrelevant | |
2718 | * until its first write fault, when page's anon_vma and index | |
2719 | * are set. But now set the vm_pgoff it will almost certainly | |
2720 | * end up with (unless mremap moves it elsewhere before that | |
2721 | * first wfault), so /proc/pid/maps tells a consistent story. | |
2722 | * | |
2723 | * By setting it to reflect the virtual start address of the | |
2724 | * vma, merges and splits can happen in a seamless way, just | |
2725 | * using the existing file pgoff checks and manipulations. | |
2726 | * Similarly in do_mmap_pgoff and in do_brk. | |
2727 | */ | |
2728 | if (!vma->vm_file) { | |
2729 | BUG_ON(vma->anon_vma); | |
2730 | vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT; | |
2731 | } | |
6597d783 HD |
2732 | if (find_vma_links(mm, vma->vm_start, vma->vm_end, |
2733 | &prev, &rb_link, &rb_parent)) | |
1da177e4 | 2734 | return -ENOMEM; |
2fd4ef85 | 2735 | if ((vma->vm_flags & VM_ACCOUNT) && |
34b4e4aa | 2736 | security_vm_enough_memory_mm(mm, vma_pages(vma))) |
2fd4ef85 | 2737 | return -ENOMEM; |
2b144498 | 2738 | |
1da177e4 LT |
2739 | vma_link(mm, vma, prev, rb_link, rb_parent); |
2740 | return 0; | |
2741 | } | |
2742 | ||
2743 | /* | |
2744 | * Copy the vma structure to a new location in the same mm, | |
2745 | * prior to moving page table entries, to effect an mremap move. | |
2746 | */ | |
2747 | struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, | |
38a76013 ML |
2748 | unsigned long addr, unsigned long len, pgoff_t pgoff, |
2749 | bool *need_rmap_locks) | |
1da177e4 LT |
2750 | { |
2751 | struct vm_area_struct *vma = *vmap; | |
2752 | unsigned long vma_start = vma->vm_start; | |
2753 | struct mm_struct *mm = vma->vm_mm; | |
2754 | struct vm_area_struct *new_vma, *prev; | |
2755 | struct rb_node **rb_link, *rb_parent; | |
2756 | struct mempolicy *pol; | |
948f017b | 2757 | bool faulted_in_anon_vma = true; |
1da177e4 LT |
2758 | |
2759 | /* | |
2760 | * If anonymous vma has not yet been faulted, update new pgoff | |
2761 | * to match new location, to increase its chance of merging. | |
2762 | */ | |
948f017b | 2763 | if (unlikely(!vma->vm_file && !vma->anon_vma)) { |
1da177e4 | 2764 | pgoff = addr >> PAGE_SHIFT; |
948f017b AA |
2765 | faulted_in_anon_vma = false; |
2766 | } | |
1da177e4 | 2767 | |
6597d783 HD |
2768 | if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) |
2769 | return NULL; /* should never get here */ | |
1da177e4 LT |
2770 | new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags, |
2771 | vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma)); | |
2772 | if (new_vma) { | |
2773 | /* | |
2774 | * Source vma may have been merged into new_vma | |
2775 | */ | |
948f017b AA |
2776 | if (unlikely(vma_start >= new_vma->vm_start && |
2777 | vma_start < new_vma->vm_end)) { | |
2778 | /* | |
2779 | * The only way we can get a vma_merge with | |
2780 | * self during an mremap is if the vma hasn't | |
2781 | * been faulted in yet and we were allowed to | |
2782 | * reset the dst vma->vm_pgoff to the | |
2783 | * destination address of the mremap to allow | |
2784 | * the merge to happen. mremap must change the | |
2785 | * vm_pgoff linearity between src and dst vmas | |
2786 | * (in turn preventing a vma_merge) to be | |
2787 | * safe. It is only safe to keep the vm_pgoff | |
2788 | * linear if there are no pages mapped yet. | |
2789 | */ | |
2790 | VM_BUG_ON(faulted_in_anon_vma); | |
38a76013 | 2791 | *vmap = vma = new_vma; |
108d6642 | 2792 | } |
38a76013 | 2793 | *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff); |
1da177e4 | 2794 | } else { |
e94b1766 | 2795 | new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
1da177e4 LT |
2796 | if (new_vma) { |
2797 | *new_vma = *vma; | |
523d4e20 ML |
2798 | new_vma->vm_start = addr; |
2799 | new_vma->vm_end = addr + len; | |
2800 | new_vma->vm_pgoff = pgoff; | |
846a16bf | 2801 | pol = mpol_dup(vma_policy(vma)); |
5beb4930 RR |
2802 | if (IS_ERR(pol)) |
2803 | goto out_free_vma; | |
523d4e20 | 2804 | vma_set_policy(new_vma, pol); |
5beb4930 RR |
2805 | INIT_LIST_HEAD(&new_vma->anon_vma_chain); |
2806 | if (anon_vma_clone(new_vma, vma)) | |
2807 | goto out_free_mempol; | |
e9714acf | 2808 | if (new_vma->vm_file) |
1da177e4 LT |
2809 | get_file(new_vma->vm_file); |
2810 | if (new_vma->vm_ops && new_vma->vm_ops->open) | |
2811 | new_vma->vm_ops->open(new_vma); | |
2812 | vma_link(mm, new_vma, prev, rb_link, rb_parent); | |
38a76013 | 2813 | *need_rmap_locks = false; |
1da177e4 LT |
2814 | } |
2815 | } | |
2816 | return new_vma; | |
5beb4930 RR |
2817 | |
2818 | out_free_mempol: | |
2819 | mpol_put(pol); | |
2820 | out_free_vma: | |
2821 | kmem_cache_free(vm_area_cachep, new_vma); | |
2822 | return NULL; | |
1da177e4 | 2823 | } |
119f657c | 2824 | |
2825 | /* | |
2826 | * Return true if the calling process may expand its vm space by the passed | |
2827 | * number of pages | |
2828 | */ | |
2829 | int may_expand_vm(struct mm_struct *mm, unsigned long npages) | |
2830 | { | |
2831 | unsigned long cur = mm->total_vm; /* pages */ | |
2832 | unsigned long lim; | |
2833 | ||
59e99e5b | 2834 | lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT; |
119f657c | 2835 | |
2836 | if (cur + npages > lim) | |
2837 | return 0; | |
2838 | return 1; | |
2839 | } | |
fa5dc22f RM |
2840 | |
2841 | ||
b1d0e4f5 NP |
2842 | static int special_mapping_fault(struct vm_area_struct *vma, |
2843 | struct vm_fault *vmf) | |
fa5dc22f | 2844 | { |
b1d0e4f5 | 2845 | pgoff_t pgoff; |
fa5dc22f RM |
2846 | struct page **pages; |
2847 | ||
b1d0e4f5 NP |
2848 | /* |
2849 | * special mappings have no vm_file, and in that case, the mm | |
2850 | * uses vm_pgoff internally. So we have to subtract it from here. | |
2851 | * We are allowed to do this because we are the mm; do not copy | |
2852 | * this code into drivers! | |
2853 | */ | |
2854 | pgoff = vmf->pgoff - vma->vm_pgoff; | |
fa5dc22f | 2855 | |
b1d0e4f5 NP |
2856 | for (pages = vma->vm_private_data; pgoff && *pages; ++pages) |
2857 | pgoff--; | |
fa5dc22f RM |
2858 | |
2859 | if (*pages) { | |
2860 | struct page *page = *pages; | |
2861 | get_page(page); | |
b1d0e4f5 NP |
2862 | vmf->page = page; |
2863 | return 0; | |
fa5dc22f RM |
2864 | } |
2865 | ||
b1d0e4f5 | 2866 | return VM_FAULT_SIGBUS; |
fa5dc22f RM |
2867 | } |
2868 | ||
2869 | /* | |
2870 | * Having a close hook prevents vma merging regardless of flags. | |
2871 | */ | |
2872 | static void special_mapping_close(struct vm_area_struct *vma) | |
2873 | { | |
2874 | } | |
2875 | ||
f0f37e2f | 2876 | static const struct vm_operations_struct special_mapping_vmops = { |
fa5dc22f | 2877 | .close = special_mapping_close, |
b1d0e4f5 | 2878 | .fault = special_mapping_fault, |
fa5dc22f RM |
2879 | }; |
2880 | ||
2881 | /* | |
2882 | * Called with mm->mmap_sem held for writing. | |
2883 | * Insert a new vma covering the given region, with the given flags. | |
2884 | * Its pages are supplied by the given array of struct page *. | |
2885 | * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated. | |
2886 | * The region past the last page supplied will always produce SIGBUS. | |
2887 | * The array pointer and the pages it points to are assumed to stay alive | |
2888 | * for as long as this mapping might exist. | |
2889 | */ | |
2890 | int install_special_mapping(struct mm_struct *mm, | |
2891 | unsigned long addr, unsigned long len, | |
2892 | unsigned long vm_flags, struct page **pages) | |
2893 | { | |
462e635e | 2894 | int ret; |
fa5dc22f RM |
2895 | struct vm_area_struct *vma; |
2896 | ||
2897 | vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL); | |
2898 | if (unlikely(vma == NULL)) | |
2899 | return -ENOMEM; | |
2900 | ||
5beb4930 | 2901 | INIT_LIST_HEAD(&vma->anon_vma_chain); |
fa5dc22f RM |
2902 | vma->vm_mm = mm; |
2903 | vma->vm_start = addr; | |
2904 | vma->vm_end = addr + len; | |
2905 | ||
2f98735c | 2906 | vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND; |
3ed75eb8 | 2907 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
fa5dc22f RM |
2908 | |
2909 | vma->vm_ops = &special_mapping_vmops; | |
2910 | vma->vm_private_data = pages; | |
2911 | ||
462e635e TO |
2912 | ret = insert_vm_struct(mm, vma); |
2913 | if (ret) | |
2914 | goto out; | |
fa5dc22f RM |
2915 | |
2916 | mm->total_vm += len >> PAGE_SHIFT; | |
2917 | ||
cdd6c482 | 2918 | perf_event_mmap(vma); |
089dd79d | 2919 | |
fa5dc22f | 2920 | return 0; |
462e635e TO |
2921 | |
2922 | out: | |
2923 | kmem_cache_free(vm_area_cachep, vma); | |
2924 | return ret; | |
fa5dc22f | 2925 | } |
7906d00c AA |
2926 | |
2927 | static DEFINE_MUTEX(mm_all_locks_mutex); | |
2928 | ||
454ed842 | 2929 | static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma) |
7906d00c | 2930 | { |
bf181b9f | 2931 | if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) { |
7906d00c AA |
2932 | /* |
2933 | * The LSB of head.next can't change from under us | |
2934 | * because we hold the mm_all_locks_mutex. | |
2935 | */ | |
572043c9 | 2936 | down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem); |
7906d00c AA |
2937 | /* |
2938 | * We can safely modify head.next after taking the | |
5a505085 | 2939 | * anon_vma->root->rwsem. If some other vma in this mm shares |
7906d00c AA |
2940 | * the same anon_vma we won't take it again. |
2941 | * | |
2942 | * No need of atomic instructions here, head.next | |
2943 | * can't change from under us thanks to the | |
5a505085 | 2944 | * anon_vma->root->rwsem. |
7906d00c AA |
2945 | */ |
2946 | if (__test_and_set_bit(0, (unsigned long *) | |
bf181b9f | 2947 | &anon_vma->root->rb_root.rb_node)) |
7906d00c AA |
2948 | BUG(); |
2949 | } | |
2950 | } | |
2951 | ||
454ed842 | 2952 | static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping) |
7906d00c AA |
2953 | { |
2954 | if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { | |
2955 | /* | |
2956 | * AS_MM_ALL_LOCKS can't change from under us because | |
2957 | * we hold the mm_all_locks_mutex. | |
2958 | * | |
2959 | * Operations on ->flags have to be atomic because | |
2960 | * even if AS_MM_ALL_LOCKS is stable thanks to the | |
2961 | * mm_all_locks_mutex, there may be other cpus | |
2962 | * changing other bitflags in parallel to us. | |
2963 | */ | |
2964 | if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags)) | |
2965 | BUG(); | |
3d48ae45 | 2966 | mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem); |
7906d00c AA |
2967 | } |
2968 | } | |
2969 | ||
2970 | /* | |
2971 | * This operation locks against the VM for all pte/vma/mm related | |
2972 | * operations that could ever happen on a certain mm. This includes | |
2973 | * vmtruncate, try_to_unmap, and all page faults. | |
2974 | * | |
2975 | * The caller must take the mmap_sem in write mode before calling | |
2976 | * mm_take_all_locks(). The caller isn't allowed to release the | |
2977 | * mmap_sem until mm_drop_all_locks() returns. | |
2978 | * | |
2979 | * mmap_sem in write mode is required in order to block all operations | |
2980 | * that could modify pagetables and free pages without need of | |
2981 | * altering the vma layout (for example populate_range() with | |
2982 | * nonlinear vmas). It's also needed in write mode to avoid new | |
2983 | * anon_vmas to be associated with existing vmas. | |
2984 | * | |
2985 | * A single task can't take more than one mm_take_all_locks() in a row | |
2986 | * or it would deadlock. | |
2987 | * | |
bf181b9f | 2988 | * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in |
7906d00c AA |
2989 | * mapping->flags avoid to take the same lock twice, if more than one |
2990 | * vma in this mm is backed by the same anon_vma or address_space. | |
2991 | * | |
2992 | * We can take all the locks in random order because the VM code | |
631b0cfd | 2993 | * taking i_mmap_mutex or anon_vma->rwsem outside the mmap_sem never |
7906d00c AA |
2994 | * takes more than one of them in a row. Secondly we're protected |
2995 | * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex. | |
2996 | * | |
2997 | * mm_take_all_locks() and mm_drop_all_locks are expensive operations | |
2998 | * that may have to take thousand of locks. | |
2999 | * | |
3000 | * mm_take_all_locks() can fail if it's interrupted by signals. | |
3001 | */ | |
3002 | int mm_take_all_locks(struct mm_struct *mm) | |
3003 | { | |
3004 | struct vm_area_struct *vma; | |
5beb4930 | 3005 | struct anon_vma_chain *avc; |
7906d00c AA |
3006 | |
3007 | BUG_ON(down_read_trylock(&mm->mmap_sem)); | |
3008 | ||
3009 | mutex_lock(&mm_all_locks_mutex); | |
3010 | ||
3011 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
3012 | if (signal_pending(current)) | |
3013 | goto out_unlock; | |
7906d00c | 3014 | if (vma->vm_file && vma->vm_file->f_mapping) |
454ed842 | 3015 | vm_lock_mapping(mm, vma->vm_file->f_mapping); |
7906d00c | 3016 | } |
7cd5a02f PZ |
3017 | |
3018 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
3019 | if (signal_pending(current)) | |
3020 | goto out_unlock; | |
3021 | if (vma->anon_vma) | |
5beb4930 RR |
3022 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
3023 | vm_lock_anon_vma(mm, avc->anon_vma); | |
7906d00c | 3024 | } |
7cd5a02f | 3025 | |
584cff54 | 3026 | return 0; |
7906d00c AA |
3027 | |
3028 | out_unlock: | |
584cff54 KC |
3029 | mm_drop_all_locks(mm); |
3030 | return -EINTR; | |
7906d00c AA |
3031 | } |
3032 | ||
3033 | static void vm_unlock_anon_vma(struct anon_vma *anon_vma) | |
3034 | { | |
bf181b9f | 3035 | if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) { |
7906d00c AA |
3036 | /* |
3037 | * The LSB of head.next can't change to 0 from under | |
3038 | * us because we hold the mm_all_locks_mutex. | |
3039 | * | |
3040 | * We must however clear the bitflag before unlocking | |
bf181b9f | 3041 | * the vma so the users using the anon_vma->rb_root will |
7906d00c AA |
3042 | * never see our bitflag. |
3043 | * | |
3044 | * No need of atomic instructions here, head.next | |
3045 | * can't change from under us until we release the | |
5a505085 | 3046 | * anon_vma->root->rwsem. |
7906d00c AA |
3047 | */ |
3048 | if (!__test_and_clear_bit(0, (unsigned long *) | |
bf181b9f | 3049 | &anon_vma->root->rb_root.rb_node)) |
7906d00c | 3050 | BUG(); |
08b52706 | 3051 | anon_vma_unlock_write(anon_vma); |
7906d00c AA |
3052 | } |
3053 | } | |
3054 | ||
3055 | static void vm_unlock_mapping(struct address_space *mapping) | |
3056 | { | |
3057 | if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) { | |
3058 | /* | |
3059 | * AS_MM_ALL_LOCKS can't change to 0 from under us | |
3060 | * because we hold the mm_all_locks_mutex. | |
3061 | */ | |
3d48ae45 | 3062 | mutex_unlock(&mapping->i_mmap_mutex); |
7906d00c AA |
3063 | if (!test_and_clear_bit(AS_MM_ALL_LOCKS, |
3064 | &mapping->flags)) | |
3065 | BUG(); | |
3066 | } | |
3067 | } | |
3068 | ||
3069 | /* | |
3070 | * The mmap_sem cannot be released by the caller until | |
3071 | * mm_drop_all_locks() returns. | |
3072 | */ | |
3073 | void mm_drop_all_locks(struct mm_struct *mm) | |
3074 | { | |
3075 | struct vm_area_struct *vma; | |
5beb4930 | 3076 | struct anon_vma_chain *avc; |
7906d00c AA |
3077 | |
3078 | BUG_ON(down_read_trylock(&mm->mmap_sem)); | |
3079 | BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); | |
3080 | ||
3081 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
3082 | if (vma->anon_vma) | |
5beb4930 RR |
3083 | list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) |
3084 | vm_unlock_anon_vma(avc->anon_vma); | |
7906d00c AA |
3085 | if (vma->vm_file && vma->vm_file->f_mapping) |
3086 | vm_unlock_mapping(vma->vm_file->f_mapping); | |
3087 | } | |
3088 | ||
3089 | mutex_unlock(&mm_all_locks_mutex); | |
3090 | } | |
8feae131 DH |
3091 | |
3092 | /* | |
3093 | * initialise the VMA slab | |
3094 | */ | |
3095 | void __init mmap_init(void) | |
3096 | { | |
00a62ce9 KM |
3097 | int ret; |
3098 | ||
3099 | ret = percpu_counter_init(&vm_committed_as, 0); | |
3100 | VM_BUG_ON(ret); | |
8feae131 | 3101 | } |
c9b1d098 AS |
3102 | |
3103 | /* | |
3104 | * Initialise sysctl_user_reserve_kbytes. | |
3105 | * | |
3106 | * This is intended to prevent a user from starting a single memory hogging | |
3107 | * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER | |
3108 | * mode. | |
3109 | * | |
3110 | * The default value is min(3% of free memory, 128MB) | |
3111 | * 128MB is enough to recover with sshd/login, bash, and top/kill. | |
3112 | */ | |
3113 | static int __meminit init_user_reserve(void) | |
3114 | { | |
3115 | unsigned long free_kbytes; | |
3116 | ||
3117 | free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); | |
3118 | ||
3119 | sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17); | |
3120 | return 0; | |
3121 | } | |
3122 | module_init(init_user_reserve) | |
4eeab4f5 AS |
3123 | |
3124 | /* | |
3125 | * Initialise sysctl_admin_reserve_kbytes. | |
3126 | * | |
3127 | * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin | |
3128 | * to log in and kill a memory hogging process. | |
3129 | * | |
3130 | * Systems with more than 256MB will reserve 8MB, enough to recover | |
3131 | * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will | |
3132 | * only reserve 3% of free pages by default. | |
3133 | */ | |
3134 | static int __meminit init_admin_reserve(void) | |
3135 | { | |
3136 | unsigned long free_kbytes; | |
3137 | ||
3138 | free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10); | |
3139 | ||
3140 | sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13); | |
3141 | return 0; | |
3142 | } | |
3143 | module_init(init_admin_reserve) |