blob: 60ba08e971fb60b1d4a34d0258ed0d849e89a9ee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/mmap.c
3 *
4 * Written by obz.
5 *
Alan Cox046c6882009-01-05 14:06:29 +00006 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Cyril Hrubise8420a82013-04-29 15:08:33 -070011#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070013#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070015#include <linux/vmacache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/shm.h>
17#include <linux/mman.h>
18#include <linux/pagemap.h>
19#include <linux/swap.h>
20#include <linux/syscalls.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/file.h>
24#include <linux/fs.h>
25#include <linux/personality.h>
26#include <linux/security.h>
27#include <linux/hugetlb.h>
28#include <linux/profile.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040029#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mount.h>
31#include <linux/mempolicy.h>
32#include <linux/rmap.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070033#include <linux/mmu_notifier.h>
Konstantin Khlebnikov82f71ae2014-08-06 16:06:36 -070034#include <linux/mmdebug.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020035#include <linux/perf_event.h>
Al Viro120a7952010-10-30 02:54:44 -040036#include <linux/audit.h>
Andrea Arcangelib15d00b2011-01-13 15:46:59 -080037#include <linux/khugepaged.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053038#include <linux/uprobes.h>
Michel Lespinassed3737182012-12-11 16:01:38 -080039#include <linux/rbtree_augmented.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -060040#include <linux/sched/sysctl.h>
Andrew Shewmaker16408792013-04-29 15:08:12 -070041#include <linux/notifier.h>
42#include <linux/memory.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070043#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
46#include <asm/cacheflush.h>
47#include <asm/tlb.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020048#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jan Beulich42b77722008-07-23 21:27:10 -070050#include "internal.h"
51
Kirill Korotaev3a459752006-09-07 14:17:04 +040052#ifndef arch_mmap_check
53#define arch_mmap_check(addr, len, flags) (0)
54#endif
55
Martin Schwidefsky08e7d9b2008-02-04 22:29:16 -080056#ifndef arch_rebalance_pgtables
57#define arch_rebalance_pgtables(addr, len) (addr)
58#endif
59
Hugh Dickinse0da3822005-04-19 13:29:15 -070060static void unmap_region(struct mm_struct *mm,
61 struct vm_area_struct *vma, struct vm_area_struct *prev,
62 unsigned long start, unsigned long end);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* description of effects of mapping type and prot in current implementation.
65 * this is due to the limited x86 page protection hardware. The expected
66 * behavior is in parens:
67 *
68 * map_type prot
69 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
70 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
71 * w: (no) no w: (no) no w: (yes) yes w: (no) no
72 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
vishnu.pscc71aba2014-10-09 15:26:29 -070073 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
75 * w: (no) no w: (no) no w: (copy) copy w: (no) no
76 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
77 *
78 */
79pgprot_t protection_map[16] = {
80 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
81 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
82};
83
Hugh Dickins804af2c2006-07-26 21:39:49 +010084pgprot_t vm_get_page_prot(unsigned long vm_flags)
85{
Dave Kleikampb845f312008-07-08 00:28:51 +100086 return __pgprot(pgprot_val(protection_map[vm_flags &
87 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
88 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +010089}
90EXPORT_SYMBOL(vm_get_page_prot);
91
Peter Feiner64e45502014-10-13 15:55:46 -070092static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags)
93{
94 return pgprot_modify(oldprot, vm_get_page_prot(vm_flags));
95}
96
97/* Update vma->vm_page_prot to reflect vma->vm_flags. */
98void vma_set_page_prot(struct vm_area_struct *vma)
99{
100 unsigned long vm_flags = vma->vm_flags;
101
102 vma->vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags);
103 if (vma_wants_writenotify(vma)) {
104 vm_flags &= ~VM_SHARED;
105 vma->vm_page_prot = vm_pgprot_modify(vma->vm_page_prot,
106 vm_flags);
107 }
108}
109
110
Shaohua Li34679d72011-05-24 17:11:18 -0700111int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */
112int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */
Jerome Marchand49f0ce52014-01-21 15:49:14 -0800113unsigned long sysctl_overcommit_kbytes __read_mostly;
Christoph Lameterc3d8c142005-09-06 15:16:33 -0700114int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -0700115unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700116unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
Shaohua Li34679d72011-05-24 17:11:18 -0700117/*
118 * Make sure vm_committed_as in one cacheline and not cacheline shared with
119 * other variables. It can be updated by several CPUs frequently.
120 */
121struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
K. Y. Srinivasan997071b2012-11-15 14:34:42 -0800124 * The global memory commitment made in the system can be a metric
125 * that can be used to drive ballooning decisions when Linux is hosted
126 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
127 * balancing memory across competing virtual machines that are hosted.
128 * Several metrics drive this policy engine including the guest reported
129 * memory commitment.
130 */
131unsigned long vm_memory_committed(void)
132{
133 return percpu_counter_read_positive(&vm_committed_as);
134}
135EXPORT_SYMBOL_GPL(vm_memory_committed);
136
137/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * Check that a process has enough memory to allocate a new virtual
139 * mapping. 0 means there is enough memory for the allocation to
140 * succeed and -ENOMEM implies there is not.
141 *
142 * We currently support three overcommit policies, which are set via the
143 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
144 *
145 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
146 * Additional code 2002 Jul 20 by Robert Love.
147 *
148 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
149 *
150 * Note this is a helper function intended to be used by LSMs which
151 * wish to use this logic.
152 */
Alan Cox34b4e4a2007-08-22 14:01:28 -0700153int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Roman Gushchin64ac320522015-02-11 15:28:39 -0800155 long free, allowed, reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Konstantin Khlebnikov82f71ae2014-08-06 16:06:36 -0700157 VM_WARN_ONCE(percpu_counter_read(&vm_committed_as) <
158 -(s64)vm_committed_as_batch * num_online_cpus(),
159 "memory commitment underflow");
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 vm_acct_memory(pages);
162
163 /*
164 * Sometimes we want to use more memory than we have
165 */
166 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
167 return 0;
168
169 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -0700170 free = global_page_state(NR_FREE_PAGES);
171 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Dmitry Finkc15bef32011-07-25 17:12:19 -0700173 /*
174 * shmem pages shouldn't be counted as free in this
175 * case, they can't be purged, only swapped out, and
176 * that won't affect the overall amount of available
177 * memory in the system.
178 */
179 free -= global_page_state(NR_SHMEM);
180
Shaohua Liec8acf22013-02-22 16:34:38 -0800181 free += get_nr_swap_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /*
184 * Any slabs which are created with the
185 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
186 * which are reclaimable, under pressure. The dentry
187 * cache and most inode caches should fall into this
188 */
Christoph Lameter972d1a72006-09-25 23:31:51 -0700189 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -0700192 * Leave reserved pages. The pages are not for anonymous pages.
193 */
194 if (free <= totalreserve_pages)
195 goto error;
196 else
197 free -= totalreserve_pages;
198
199 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700200 * Reserve some for root
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
202 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700203 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (free > pages)
206 return 0;
207
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700208 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Jerome Marchand00619bc2013-11-12 15:08:31 -0800211 allowed = vm_commit_limit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700213 * Reserve some for root
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215 if (!cap_sys_admin)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -0700216 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -0700218 /*
219 * Don't let a single process grow so big a user can't recover
220 */
221 if (mm) {
222 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
Roman Gushchin64ac320522015-02-11 15:28:39 -0800223 allowed -= min_t(long, mm->total_vm / 32, reserve);
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -0700224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700226 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700228error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 vm_unacct_memory(pages);
230
231 return -ENOMEM;
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700235 * Requires inode->i_mapping->i_mmap_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
237static void __remove_shared_vm_struct(struct vm_area_struct *vma,
238 struct file *file, struct address_space *mapping)
239{
240 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500241 atomic_inc(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700243 mapping_unmap_writable(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 flush_dcache_mmap_lock(mapping);
246 if (unlikely(vma->vm_flags & VM_NONLINEAR))
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700247 list_del_init(&vma->shared.nonlinear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 else
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700249 vma_interval_tree_remove(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 flush_dcache_mmap_unlock(mapping);
251}
252
253/*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700254 * Unlink a file-based vm structure from its interval tree, to hide
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700255 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700257void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct file *file = vma->vm_file;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (file) {
262 struct address_space *mapping = file->f_mapping;
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700263 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 __remove_shared_vm_struct(vma, file, mapping);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700265 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700267}
268
269/*
270 * Close a vm structure and free it, returning the next.
271 */
272static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
273{
274 struct vm_area_struct *next = vma->vm_next;
275
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700276 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (vma->vm_ops && vma->vm_ops->close)
278 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700279 if (vma->vm_file)
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700280 fput(vma->vm_file);
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700281 mpol_put(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 kmem_cache_free(vm_area_cachep, vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700283 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -0700286static unsigned long do_brk(unsigned long addr, unsigned long len);
287
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100288SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700290 unsigned long retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned long newbrk, oldbrk;
292 struct mm_struct *mm = current->mm;
Hugh Dickinsd4712eb2017-06-19 04:03:24 -0700293 struct vm_area_struct *next;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700294 unsigned long min_brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800295 bool populate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 down_write(&mm->mmap_sem);
298
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700299#ifdef CONFIG_COMPAT_BRK
Jiri Kosina5520e892011-01-13 15:47:23 -0800300 /*
301 * CONFIG_COMPAT_BRK can still be overridden by setting
302 * randomize_va_space to 2, which will still cause mm->start_brk
303 * to be arbitrarily shifted
304 */
Jiri Kosina4471a672011-04-14 15:22:09 -0700305 if (current->brk_randomized)
Jiri Kosina5520e892011-01-13 15:47:23 -0800306 min_brk = mm->start_brk;
307 else
308 min_brk = mm->end_data;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700309#else
310 min_brk = mm->start_brk;
311#endif
312 if (brk < min_brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700314
315 /*
316 * Check against rlimit here. If this check is done later after the test
317 * of oldbrk with newbrk then it can escape the test and let the data
318 * segment grow beyond its set limit the in case where the limit is
319 * not page aligned -Ram Gupta
320 */
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700321 if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk,
322 mm->end_data, mm->start_data))
Ram Gupta1e624192006-04-10 22:52:57 -0700323 goto out;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 newbrk = PAGE_ALIGN(brk);
326 oldbrk = PAGE_ALIGN(mm->brk);
327 if (oldbrk == newbrk)
328 goto set_brk;
329
330 /* Always allow shrinking brk. */
331 if (brk <= mm->brk) {
332 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
333 goto set_brk;
334 goto out;
335 }
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* Check against existing mmap mappings. */
Hugh Dickinsd4712eb2017-06-19 04:03:24 -0700338 next = find_vma(mm, oldbrk);
339 if (next && newbrk + PAGE_SIZE > vm_start_gap(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto out;
341
342 /* Ok, looks good - let it rip. */
343 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
344 goto out;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346set_brk:
347 mm->brk = brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800348 populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0;
349 up_write(&mm->mmap_sem);
350 if (populate)
351 mm_populate(oldbrk, newbrk - oldbrk);
352 return brk;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354out:
355 retval = mm->brk;
356 up_write(&mm->mmap_sem);
357 return retval;
358}
359
Michel Lespinassed3737182012-12-11 16:01:38 -0800360static long vma_compute_subtree_gap(struct vm_area_struct *vma)
361{
Hugh Dickinsd4712eb2017-06-19 04:03:24 -0700362 unsigned long max, prev_end, subtree_gap;
363
364 /*
365 * Note: in the rare case of a VM_GROWSDOWN above a VM_GROWSUP, we
366 * allow two stack_guard_gaps between them here, and when choosing
367 * an unmapped area; whereas when expanding we only require one.
368 * That's a little inconsistent, but keeps the code here simpler.
369 */
370 max = vm_start_gap(vma);
371 if (vma->vm_prev) {
372 prev_end = vm_end_gap(vma->vm_prev);
373 if (max > prev_end)
374 max -= prev_end;
375 else
376 max = 0;
377 }
Michel Lespinassed3737182012-12-11 16:01:38 -0800378 if (vma->vm_rb.rb_left) {
379 subtree_gap = rb_entry(vma->vm_rb.rb_left,
380 struct vm_area_struct, vm_rb)->rb_subtree_gap;
381 if (subtree_gap > max)
382 max = subtree_gap;
383 }
384 if (vma->vm_rb.rb_right) {
385 subtree_gap = rb_entry(vma->vm_rb.rb_right,
386 struct vm_area_struct, vm_rb)->rb_subtree_gap;
387 if (subtree_gap > max)
388 max = subtree_gap;
389 }
390 return max;
391}
392
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700393#ifdef CONFIG_DEBUG_VM_RB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static int browse_rb(struct rb_root *root)
395{
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800396 int i = 0, j, bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct rb_node *nd, *pn = NULL;
398 unsigned long prev = 0, pend = 0;
399
400 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
401 struct vm_area_struct *vma;
402 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800403 if (vma->vm_start < prev) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700404 pr_emerg("vm_start %lx < prev %lx\n",
405 vma->vm_start, prev);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800406 bug = 1;
407 }
408 if (vma->vm_start < pend) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700409 pr_emerg("vm_start %lx < pend %lx\n",
410 vma->vm_start, pend);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800411 bug = 1;
412 }
413 if (vma->vm_start > vma->vm_end) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700414 pr_emerg("vm_start %lx > vm_end %lx\n",
415 vma->vm_start, vma->vm_end);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800416 bug = 1;
417 }
418 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700419 pr_emerg("free gap %lx, correct %lx\n",
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800420 vma->rb_subtree_gap,
421 vma_compute_subtree_gap(vma));
422 bug = 1;
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 i++;
425 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800426 prev = vma->vm_start;
427 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 j = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800430 for (nd = pn; nd; nd = rb_prev(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 j++;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800432 if (i != j) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700433 pr_emerg("backwards %d, forwards %d\n", j, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800434 bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800436 return bug ? -1 : i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Michel Lespinassed3737182012-12-11 16:01:38 -0800439static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
440{
441 struct rb_node *nd;
442
443 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
444 struct vm_area_struct *vma;
445 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Sasha Levin96dad672014-10-09 15:28:39 -0700446 VM_BUG_ON_VMA(vma != ignore &&
447 vma->rb_subtree_gap != vma_compute_subtree_gap(vma),
448 vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Rashika Kheriaeafd4dc2014-04-03 14:48:03 -0700452static void validate_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 int bug = 0;
455 int i = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800456 unsigned long highest_address = 0;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700457 struct vm_area_struct *vma = mm->mmap;
Andrew Mortonff26f702014-10-09 15:28:19 -0700458
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700459 while (vma) {
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -0800460 struct anon_vma *anon_vma = vma->anon_vma;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700461 struct anon_vma_chain *avc;
Andrew Mortonff26f702014-10-09 15:28:19 -0700462
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -0800463 if (anon_vma) {
464 anon_vma_lock_read(anon_vma);
465 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
466 anon_vma_interval_tree_verify(avc);
467 anon_vma_unlock_read(anon_vma);
468 }
469
Hugh Dickinsd4712eb2017-06-19 04:03:24 -0700470 highest_address = vm_end_gap(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700471 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 i++;
473 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800474 if (i != mm->map_count) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700475 pr_emerg("map_count %d vm_next %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800476 bug = 1;
477 }
478 if (highest_address != mm->highest_vm_end) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700479 pr_emerg("mm->highest_vm_end %lx, found %lx\n",
Andrew Mortonff26f702014-10-09 15:28:19 -0700480 mm->highest_vm_end, highest_address);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800481 bug = 1;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 i = browse_rb(&mm->mm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800484 if (i != mm->map_count) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700485 if (i != -1)
486 pr_emerg("map_count %d rb %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800487 bug = 1;
488 }
Sasha Levin96dad672014-10-09 15:28:39 -0700489 VM_BUG_ON_MM(bug, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491#else
Michel Lespinassed3737182012-12-11 16:01:38 -0800492#define validate_mm_rb(root, ignore) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#define validate_mm(mm) do { } while (0)
494#endif
495
Michel Lespinassed3737182012-12-11 16:01:38 -0800496RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
497 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
498
499/*
500 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
501 * vma->vm_prev->vm_end values changed, without modifying the vma's position
502 * in the rbtree.
503 */
504static void vma_gap_update(struct vm_area_struct *vma)
505{
506 /*
507 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
508 * function that does exacltly what we want.
509 */
510 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
511}
512
513static inline void vma_rb_insert(struct vm_area_struct *vma,
514 struct rb_root *root)
515{
516 /* All rb_subtree_gap values must be consistent prior to insertion */
517 validate_mm_rb(root, NULL);
518
519 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
520}
521
522static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
523{
524 /*
525 * All rb_subtree_gap values must be consistent prior to erase,
526 * with the possible exception of the vma being erased.
527 */
528 validate_mm_rb(root, vma);
529
530 /*
531 * Note rb_erase_augmented is a fairly large inline function,
532 * so make sure we instantiate it only once with our desired
533 * augmented rbtree callbacks.
534 */
535 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
536}
537
Michel Lespinassebf181b92012-10-08 16:31:39 -0700538/*
539 * vma has some anon_vma assigned, and is already inserted on that
540 * anon_vma's interval trees.
541 *
542 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
543 * vma must be removed from the anon_vma's interval trees using
544 * anon_vma_interval_tree_pre_update_vma().
545 *
546 * After the update, the vma will be reinserted using
547 * anon_vma_interval_tree_post_update_vma().
548 *
549 * The entire update must be protected by exclusive mmap_sem and by
550 * the root anon_vma's mutex.
551 */
552static inline void
553anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
554{
555 struct anon_vma_chain *avc;
556
557 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
558 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
559}
560
561static inline void
562anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
563{
564 struct anon_vma_chain *avc;
565
566 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
567 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
568}
569
Hugh Dickins6597d782012-10-08 16:29:07 -0700570static int find_vma_links(struct mm_struct *mm, unsigned long addr,
571 unsigned long end, struct vm_area_struct **pprev,
572 struct rb_node ***rb_link, struct rb_node **rb_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Hugh Dickins6597d782012-10-08 16:29:07 -0700574 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 __rb_link = &mm->mm_rb.rb_node;
577 rb_prev = __rb_parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 while (*__rb_link) {
580 struct vm_area_struct *vma_tmp;
581
582 __rb_parent = *__rb_link;
583 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
584
585 if (vma_tmp->vm_end > addr) {
Hugh Dickins6597d782012-10-08 16:29:07 -0700586 /* Fail if an existing vma overlaps the area */
587 if (vma_tmp->vm_start < end)
588 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 __rb_link = &__rb_parent->rb_left;
590 } else {
591 rb_prev = __rb_parent;
592 __rb_link = &__rb_parent->rb_right;
593 }
594 }
595
596 *pprev = NULL;
597 if (rb_prev)
598 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
599 *rb_link = __rb_link;
600 *rb_parent = __rb_parent;
Hugh Dickins6597d782012-10-08 16:29:07 -0700601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Cyril Hrubise8420a82013-04-29 15:08:33 -0700604static unsigned long count_vma_pages_range(struct mm_struct *mm,
605 unsigned long addr, unsigned long end)
606{
607 unsigned long nr_pages = 0;
608 struct vm_area_struct *vma;
609
610 /* Find first overlaping mapping */
611 vma = find_vma_intersection(mm, addr, end);
612 if (!vma)
613 return 0;
614
615 nr_pages = (min(end, vma->vm_end) -
616 max(addr, vma->vm_start)) >> PAGE_SHIFT;
617
618 /* Iterate over the rest of the overlaps */
619 for (vma = vma->vm_next; vma; vma = vma->vm_next) {
620 unsigned long overlap_len;
621
622 if (vma->vm_start > end)
623 break;
624
625 overlap_len = min(end, vma->vm_end) - vma->vm_start;
626 nr_pages += overlap_len >> PAGE_SHIFT;
627 }
628
629 return nr_pages;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
633 struct rb_node **rb_link, struct rb_node *rb_parent)
634{
Michel Lespinassed3737182012-12-11 16:01:38 -0800635 /* Update tracking information for the gap following the new vma. */
636 if (vma->vm_next)
637 vma_gap_update(vma->vm_next);
638 else
Hugh Dickinsd4712eb2017-06-19 04:03:24 -0700639 mm->highest_vm_end = vm_end_gap(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800640
641 /*
642 * vma->vm_prev wasn't known when we followed the rbtree to find the
643 * correct insertion point for that vma. As a result, we could not
644 * update the vma vm_rb parents rb_subtree_gap values on the way down.
645 * So, we first insert the vma with a zero rb_subtree_gap value
646 * (to be consistent with what we did on the way down), and then
647 * immediately update the gap to the correct value. Finally we
648 * rebalance the rbtree after all augmented values have been set.
649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
Michel Lespinassed3737182012-12-11 16:01:38 -0800651 vma->rb_subtree_gap = 0;
652 vma_gap_update(vma);
653 vma_rb_insert(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700656static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
ZhenwenXu48aae422009-01-06 14:40:21 -0800658 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 file = vma->vm_file;
661 if (file) {
662 struct address_space *mapping = file->f_mapping;
663
664 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500665 atomic_dec(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700667 atomic_inc(&mapping->i_mmap_writable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 flush_dcache_mmap_lock(mapping);
670 if (unlikely(vma->vm_flags & VM_NONLINEAR))
671 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
672 else
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700673 vma_interval_tree_insert(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 flush_dcache_mmap_unlock(mapping);
675 }
676}
677
678static void
679__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
680 struct vm_area_struct *prev, struct rb_node **rb_link,
681 struct rb_node *rb_parent)
682{
683 __vma_link_list(mm, vma, prev, rb_parent);
684 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
688 struct vm_area_struct *prev, struct rb_node **rb_link,
689 struct rb_node *rb_parent)
690{
691 struct address_space *mapping = NULL;
692
Huang Shijie64ac4942014-06-04 16:07:33 -0700693 if (vma->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 mapping = vma->vm_file->f_mapping;
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700695 mutex_lock(&mapping->i_mmap_mutex);
Huang Shijie64ac4942014-06-04 16:07:33 -0700696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 __vma_link(mm, vma, prev, rb_link, rb_parent);
699 __vma_link_file(vma);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700702 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 mm->map_count++;
705 validate_mm(mm);
706}
707
708/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700709 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700710 * mm's list and rbtree. It has already been inserted into the interval tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800712static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Hugh Dickins6597d782012-10-08 16:29:07 -0700714 struct vm_area_struct *prev;
ZhenwenXu48aae422009-01-06 14:40:21 -0800715 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Hugh Dickins6597d782012-10-08 16:29:07 -0700717 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
718 &prev, &rb_link, &rb_parent))
719 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 __vma_link(mm, vma, prev, rb_link, rb_parent);
721 mm->map_count++;
722}
723
724static inline void
725__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
726 struct vm_area_struct *prev)
727{
Michel Lespinassed3737182012-12-11 16:01:38 -0800728 struct vm_area_struct *next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700729
Michel Lespinassed3737182012-12-11 16:01:38 -0800730 vma_rb_erase(vma, &mm->mm_rb);
731 prev->vm_next = next = vma->vm_next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700732 if (next)
733 next->vm_prev = prev;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700734
735 /* Kill the cache */
736 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739/*
740 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
741 * is already present in an i_mmap tree without adjusting the tree.
742 * The following helper function should be used when such adjustments
743 * are necessary. The "insert" vma (if any) is to be inserted
744 * before we drop the necessary locks.
745 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800746int vma_adjust(struct vm_area_struct *vma, unsigned long start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
748{
749 struct mm_struct *mm = vma->vm_mm;
750 struct vm_area_struct *next = vma->vm_next;
751 struct vm_area_struct *importer = NULL;
752 struct address_space *mapping = NULL;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700753 struct rb_root *root = NULL;
Rik van Riel012f18002010-08-09 17:18:40 -0700754 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 struct file *file = vma->vm_file;
Michel Lespinassed3737182012-12-11 16:01:38 -0800756 bool start_changed = false, end_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 long adjust_next = 0;
758 int remove_next = 0;
759
760 if (next && !insert) {
Linus Torvalds287d97a2010-04-10 15:22:30 -0700761 struct vm_area_struct *exporter = NULL;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (end >= next->vm_end) {
764 /*
765 * vma expands, overlapping all the next, and
766 * perhaps the one after too (mprotect case 6).
767 */
768again: remove_next = 1 + (end > next->vm_end);
769 end = next->vm_end;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700770 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 importer = vma;
772 } else if (end > next->vm_start) {
773 /*
774 * vma expands, overlapping part of the next:
775 * mprotect case 5 shifting the boundary up.
776 */
777 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700778 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 importer = vma;
780 } else if (end < vma->vm_end) {
781 /*
782 * vma shrinks, and !insert tells it's not
783 * split_vma inserting another: so it must be
784 * mprotect case 4 shifting the boundary down.
785 */
vishnu.pscc71aba2014-10-09 15:26:29 -0700786 adjust_next = -((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700787 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 importer = next;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Rik van Riel5beb4932010-03-05 13:42:07 -0800791 /*
792 * Easily overlooked: when mprotect shifts the boundary,
793 * make sure the expanding vma has anon_vma set if the
794 * shrinking vma had, to cover any anon pages imported.
795 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700796 if (exporter && exporter->anon_vma && !importer->anon_vma) {
Daniel Forrestc4ea95d2014-12-02 15:59:42 -0800797 int error;
798
Linus Torvalds287d97a2010-04-10 15:22:30 -0700799 importer->anon_vma = exporter->anon_vma;
Konstantin Khlebnikovf94faaa2015-01-11 16:54:06 +0300800 error = anon_vma_clone(importer, exporter);
Leon Yu5bc650f2015-03-25 15:55:11 -0700801 if (error)
Konstantin Khlebnikovf94faaa2015-01-11 16:54:06 +0300802 return error;
Rik van Riel5beb4932010-03-05 13:42:07 -0800803 }
804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (file) {
807 mapping = file->f_mapping;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530808 if (!(vma->vm_flags & VM_NONLINEAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 root = &mapping->i_mmap;
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530810 uprobe_munmap(vma, vma->vm_start, vma->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530811
812 if (adjust_next)
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530813 uprobe_munmap(next, next->vm_start,
814 next->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530815 }
816
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700817 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700820 * Put into interval tree now, so instantiated pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * are visible to arm/parisc __flush_dcache_page
822 * throughout; but we cannot insert into address
823 * space until vma start or end is updated.
824 */
825 __vma_link_file(insert);
826 }
827 }
828
Andrea Arcangeli94fcc582011-01-13 15:47:08 -0800829 vma_adjust_trans_huge(vma, start, end, adjust_next);
830
Michel Lespinassebf181b92012-10-08 16:31:39 -0700831 anon_vma = vma->anon_vma;
832 if (!anon_vma && adjust_next)
833 anon_vma = next->anon_vma;
834 if (anon_vma) {
Sasha Levin81d1b092014-10-09 15:28:10 -0700835 VM_BUG_ON_VMA(adjust_next && next->anon_vma &&
836 anon_vma != next->anon_vma, next);
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000837 anon_vma_lock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700838 anon_vma_interval_tree_pre_update_vma(vma);
839 if (adjust_next)
840 anon_vma_interval_tree_pre_update_vma(next);
841 }
Rik van Riel012f18002010-08-09 17:18:40 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (root) {
844 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700845 vma_interval_tree_remove(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700847 vma_interval_tree_remove(next, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Michel Lespinassed3737182012-12-11 16:01:38 -0800850 if (start != vma->vm_start) {
851 vma->vm_start = start;
852 start_changed = true;
853 }
854 if (end != vma->vm_end) {
855 vma->vm_end = end;
856 end_changed = true;
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 vma->vm_pgoff = pgoff;
859 if (adjust_next) {
860 next->vm_start += adjust_next << PAGE_SHIFT;
861 next->vm_pgoff += adjust_next;
862 }
863
864 if (root) {
865 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700866 vma_interval_tree_insert(next, root);
867 vma_interval_tree_insert(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 flush_dcache_mmap_unlock(mapping);
869 }
870
871 if (remove_next) {
872 /*
873 * vma_merge has merged next into vma, and needs
874 * us to remove next before dropping the locks.
875 */
876 __vma_unlink(mm, next, vma);
877 if (file)
878 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else if (insert) {
880 /*
881 * split_vma has split insert from vma, and needs
882 * us to insert it before dropping the locks
883 * (it may either follow vma or precede it).
884 */
885 __insert_vm_struct(mm, insert);
Michel Lespinassed3737182012-12-11 16:01:38 -0800886 } else {
887 if (start_changed)
888 vma_gap_update(vma);
889 if (end_changed) {
890 if (!next)
Hugh Dickinsd4712eb2017-06-19 04:03:24 -0700891 mm->highest_vm_end = vm_end_gap(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800892 else if (!adjust_next)
893 vma_gap_update(next);
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
Michel Lespinassebf181b92012-10-08 16:31:39 -0700897 if (anon_vma) {
898 anon_vma_interval_tree_post_update_vma(vma);
899 if (adjust_next)
900 anon_vma_interval_tree_post_update_vma(next);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -0800901 anon_vma_unlock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700904 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530906 if (root) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100907 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530908
909 if (adjust_next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100910 uprobe_mmap(next);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530911 }
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (remove_next) {
Matt Helsley925d1c42008-04-29 01:01:36 -0700914 if (file) {
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530915 uprobe_munmap(next, next->vm_start, next->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 fput(file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700917 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800918 if (next->anon_vma)
919 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 mm->map_count--;
Oleg Nesterov3964acd2013-07-31 13:53:28 -0700921 mpol_put(vma_policy(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 kmem_cache_free(vm_area_cachep, next);
923 /*
924 * In mprotect's case 6 (see comments on vma_merge),
925 * we must remove another next too. It would clutter
926 * up the code too much to do both in one go.
927 */
Michel Lespinassed3737182012-12-11 16:01:38 -0800928 next = vma->vm_next;
929 if (remove_next == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto again;
Michel Lespinassed3737182012-12-11 16:01:38 -0800931 else if (next)
932 vma_gap_update(next);
933 else
Hugh Dickinsd4712eb2017-06-19 04:03:24 -0700934 VM_WARN_ON(mm->highest_vm_end != vm_end_gap(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530936 if (insert && file)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100937 uprobe_mmap(insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -0800940
941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
945 * If the vma has a ->close operation then the driver probably needs to release
946 * per-vma resources, so we don't attempt to merge those.
947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948static inline int is_mergeable_vma(struct vm_area_struct *vma,
949 struct file *file, unsigned long vm_flags)
950{
Cyrill Gorcunov34228d42014-01-23 15:53:42 -0800951 /*
952 * VM_SOFTDIRTY should not prevent from VMA merging, if we
953 * match the flags but dirty bit -- the caller should mark
954 * merged VMA as dirty. If dirty bit won't be excluded from
955 * comparison, we increase pressue on the memory system forcing
956 * the kernel to generate new VMAs when old one could be
957 * extended instead.
958 */
959 if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961 if (vma->vm_file != file)
962 return 0;
963 if (vma->vm_ops && vma->vm_ops->close)
964 return 0;
965 return 1;
966}
967
968static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -0700969 struct anon_vma *anon_vma2,
970 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Shaohua Li965f55d2011-05-24 17:11:20 -0700972 /*
973 * The list_is_singular() test is to avoid merging VMA cloned from
974 * parents. This can improve scalability caused by anon_vma lock.
975 */
976 if ((!anon_vma1 || !anon_vma2) && (!vma ||
977 list_is_singular(&vma->anon_vma_chain)))
978 return 1;
979 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/*
983 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
984 * in front of (at a lower virtual address and file offset than) the vma.
985 *
986 * We cannot merge two vmas if they have differently assigned (non-NULL)
987 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
988 *
989 * We don't check here for the merged mmap wrapping around the end of pagecache
990 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
991 * wrap, nor mmaps which cover the final page at index -1UL.
992 */
993static int
994can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
995 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
996{
997 if (is_mergeable_vma(vma, file, vm_flags) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700998 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (vma->vm_pgoff == vm_pgoff)
1000 return 1;
1001 }
1002 return 0;
1003}
1004
1005/*
1006 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
1007 * beyond (at a higher virtual address and file offset than) the vma.
1008 *
1009 * We cannot merge two vmas if they have differently assigned (non-NULL)
1010 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
1011 */
1012static int
1013can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
1014 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
1015{
1016 if (is_mergeable_vma(vma, file, vm_flags) &&
Shaohua Li965f55d2011-05-24 17:11:20 -07001017 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 pgoff_t vm_pglen;
Libind6e93212013-07-03 15:01:26 -07001019 vm_pglen = vma_pages(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
1021 return 1;
1022 }
1023 return 0;
1024}
1025
1026/*
1027 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
1028 * whether that can be merged with its predecessor or its successor.
1029 * Or both (it neatly fills a hole).
1030 *
1031 * In most cases - when called for mmap, brk or mremap - [addr,end) is
1032 * certain not to be mapped by the time vma_merge is called; but when
1033 * called for mprotect, it is certain to be already mapped (either at
1034 * an offset within prev, or at the start of next), and the flags of
1035 * this area are about to be changed to vm_flags - and the no-change
1036 * case has already been eliminated.
1037 *
1038 * The following mprotect cases have to be considered, where AAAA is
1039 * the area passed down from mprotect_fixup, never extending beyond one
1040 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
1041 *
1042 * AAAA AAAA AAAA AAAA
1043 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
1044 * cannot merge might become might become might become
1045 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
1046 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
1047 * mremap move: PPPPNNNNNNNN 8
1048 * AAAA
1049 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
1050 * might become case 1 below case 2 below case 3 below
1051 *
1052 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
1053 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
1054 */
1055struct vm_area_struct *vma_merge(struct mm_struct *mm,
1056 struct vm_area_struct *prev, unsigned long addr,
1057 unsigned long end, unsigned long vm_flags,
vishnu.pscc71aba2014-10-09 15:26:29 -07001058 struct anon_vma *anon_vma, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 pgoff_t pgoff, struct mempolicy *policy)
1060{
1061 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
1062 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -08001063 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /*
1066 * We later require that vma->vm_flags == vm_flags,
1067 * so this tests vma->vm_flags & VM_SPECIAL, too.
1068 */
1069 if (vm_flags & VM_SPECIAL)
1070 return NULL;
1071
1072 if (prev)
1073 next = prev->vm_next;
1074 else
1075 next = mm->mmap;
1076 area = next;
1077 if (next && next->vm_end == end) /* cases 6, 7, 8 */
1078 next = next->vm_next;
1079
1080 /*
1081 * Can it merge with the predecessor?
1082 */
1083 if (prev && prev->vm_end == addr &&
vishnu.pscc71aba2014-10-09 15:26:29 -07001084 mpol_equal(vma_policy(prev), policy) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 can_vma_merge_after(prev, vm_flags,
1086 anon_vma, file, pgoff)) {
1087 /*
1088 * OK, it can. Can we now merge in the successor as well?
1089 */
1090 if (next && end == next->vm_start &&
1091 mpol_equal(policy, vma_policy(next)) &&
1092 can_vma_merge_before(next, vm_flags,
1093 anon_vma, file, pgoff+pglen) &&
1094 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -07001095 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* cases 1, 6 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001097 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 next->vm_end, prev->vm_pgoff, NULL);
1099 } else /* cases 2, 5, 7 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001100 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 end, prev->vm_pgoff, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -08001102 if (err)
1103 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001104 khugepaged_enter_vma_merge(prev, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return prev;
1106 }
1107
1108 /*
1109 * Can this new request be merged in front of next?
1110 */
1111 if (next && end == next->vm_start &&
vishnu.pscc71aba2014-10-09 15:26:29 -07001112 mpol_equal(policy, vma_policy(next)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 can_vma_merge_before(next, vm_flags,
1114 anon_vma, file, pgoff+pglen)) {
1115 if (prev && addr < prev->vm_end) /* case 4 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001116 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 addr, prev->vm_pgoff, NULL);
1118 else /* cases 3, 8 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001119 err = vma_adjust(area, addr, next->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 next->vm_pgoff - pglen, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -08001121 if (err)
1122 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001123 khugepaged_enter_vma_merge(area, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return area;
1125 }
1126
1127 return NULL;
1128}
1129
1130/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001131 * Rough compatbility check to quickly see if it's even worth looking
1132 * at sharing an anon_vma.
1133 *
1134 * They need to have the same vm_file, and the flags can only differ
1135 * in things that mprotect may change.
1136 *
1137 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1138 * we can merge the two vma's. For example, we refuse to merge a vma if
1139 * there is a vm_ops->close() function, because that indicates that the
1140 * driver is doing some kind of reference counting. But that doesn't
1141 * really matter for the anon_vma sharing case.
1142 */
1143static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1144{
1145 return a->vm_end == b->vm_start &&
1146 mpol_equal(vma_policy(a), vma_policy(b)) &&
1147 a->vm_file == b->vm_file &&
Cyrill Gorcunov34228d42014-01-23 15:53:42 -08001148 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) &&
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001149 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1150}
1151
1152/*
1153 * Do some basic sanity checking to see if we can re-use the anon_vma
1154 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1155 * the same as 'old', the other will be the new one that is trying
1156 * to share the anon_vma.
1157 *
1158 * NOTE! This runs with mm_sem held for reading, so it is possible that
1159 * the anon_vma of 'old' is concurrently in the process of being set up
1160 * by another page fault trying to merge _that_. But that's ok: if it
1161 * is being set up, that automatically means that it will be a singleton
1162 * acceptable for merging, so we can do all of this optimistically. But
1163 * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
1164 *
1165 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1166 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1167 * is to return an anon_vma that is "complex" due to having gone through
1168 * a fork).
1169 *
1170 * We also make sure that the two vma's are compatible (adjacent,
1171 * and with the same memory policies). That's all stable, even with just
1172 * a read lock on the mm_sem.
1173 */
1174static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1175{
1176 if (anon_vma_compatible(a, b)) {
1177 struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
1178
1179 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1180 return anon_vma;
1181 }
1182 return NULL;
1183}
1184
1185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1187 * neighbouring vmas for a suitable anon_vma, before it goes off
1188 * to allocate a new anon_vma. It checks because a repetitive
1189 * sequence of mprotects and faults may otherwise lead to distinct
1190 * anon_vmas being allocated, preventing vma merge in subsequent
1191 * mprotect.
1192 */
1193struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1194{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001195 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 near = vma->vm_next;
1199 if (!near)
1200 goto try_prev;
1201
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001202 anon_vma = reusable_anon_vma(near, vma, near);
1203 if (anon_vma)
1204 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -07001206 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (!near)
1208 goto none;
1209
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001210 anon_vma = reusable_anon_vma(near, near, vma);
1211 if (anon_vma)
1212 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213none:
1214 /*
1215 * There's no absolute need to look only at touching neighbours:
1216 * we could search further afield for "compatible" anon_vmas.
1217 * But it would probably just be a waste of time searching,
1218 * or lead to too many vmas hanging off the same anon_vma.
1219 * We're trying to allow mprotect remerging later on,
1220 * not trying to minimize memory used for anon_vmas.
1221 */
1222 return NULL;
1223}
1224
1225#ifdef CONFIG_PROC_FS
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001226void vm_stat_account(struct mm_struct *mm, unsigned long flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct file *file, long pages)
1228{
1229 const unsigned long stack_flags
1230 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
1231
Huang Shijie44de9d02012-07-31 16:41:49 -07001232 mm->total_vm += pages;
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (file) {
1235 mm->shared_vm += pages;
1236 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
1237 mm->exec_vm += pages;
1238 } else if (flags & stack_flags)
1239 mm->stack_vm += pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241#endif /* CONFIG_PROC_FS */
1242
1243/*
Al Viro40401532012-02-13 03:58:52 +00001244 * If a hint addr is less than mmap_min_addr change hint to be as
1245 * low as possible but still greater than mmap_min_addr
1246 */
1247static inline unsigned long round_hint_to_min(unsigned long hint)
1248{
1249 hint &= PAGE_MASK;
1250 if (((void *)hint != NULL) &&
1251 (hint < mmap_min_addr))
1252 return PAGE_ALIGN(mmap_min_addr);
1253 return hint;
1254}
1255
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001256static inline int mlock_future_check(struct mm_struct *mm,
1257 unsigned long flags,
1258 unsigned long len)
1259{
1260 unsigned long locked, lock_limit;
1261
1262 /* mlock MCL_FUTURE? */
1263 if (flags & VM_LOCKED) {
1264 locked = len >> PAGE_SHIFT;
1265 locked += mm->locked_vm;
1266 lock_limit = rlimit(RLIMIT_MEMLOCK);
1267 lock_limit >>= PAGE_SHIFT;
1268 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1269 return -EAGAIN;
1270 }
1271 return 0;
1272}
1273
Al Viro40401532012-02-13 03:58:52 +00001274/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001275 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
1277
Al Viroe3fc6292012-05-30 20:08:42 -04001278unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 unsigned long len, unsigned long prot,
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001280 unsigned long flags, unsigned long pgoff,
Michel Lespinasse41badc12013-02-22 16:32:47 -08001281 unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
vishnu.pscc71aba2014-10-09 15:26:29 -07001283 struct mm_struct *mm = current->mm;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001284 vm_flags_t vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Michel Lespinasse41badc12013-02-22 16:32:47 -08001286 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /*
1289 * Does the application expect PROT_READ to imply PROT_EXEC?
1290 *
1291 * (the exception is when the underlying filesystem is noexec
1292 * mounted, in which case we dont add PROT_EXEC.)
1293 */
1294 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001295 if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 prot |= PROT_EXEC;
1297
1298 if (!len)
1299 return -EINVAL;
1300
Eric Paris7cd94142007-11-26 18:47:40 -05001301 if (!(flags & MAP_FIXED))
1302 addr = round_hint_to_min(addr);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /* Careful about overflows.. */
1305 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001306 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -ENOMEM;
1308
1309 /* offset overflow? */
1310 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
vishnu.pscc71aba2014-10-09 15:26:29 -07001311 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* Too many mappings? */
1314 if (mm->map_count > sysctl_max_map_count)
1315 return -ENOMEM;
1316
1317 /* Obtain the address to map to. we verify (or select) it and ensure
1318 * that it represents a valid section of the address space.
1319 */
1320 addr = get_unmapped_area(file, addr, len, pgoff, flags);
1321 if (addr & ~PAGE_MASK)
1322 return addr;
1323
1324 /* Do simple checking here so the lower-level routines won't have
1325 * to. we assume access permissions have been handled by the open
1326 * of the memory object, so we don't do any here.
1327 */
1328 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
1329 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1330
Huang Shijiecdf7b342009-09-21 17:03:36 -07001331 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (!can_do_mlock())
1333 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001334
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001335 if (mlock_future_check(mm, vm_flags, len))
1336 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (file) {
Oleg Nesterov077bf222013-09-11 14:20:19 -07001339 struct inode *inode = file_inode(file);
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 switch (flags & MAP_TYPE) {
1342 case MAP_SHARED:
1343 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1344 return -EACCES;
1345
1346 /*
1347 * Make sure we don't allow writing to an append-only
1348 * file..
1349 */
1350 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1351 return -EACCES;
1352
1353 /*
1354 * Make sure there are no mandatory locks on the file.
1355 */
Jeff Laytond7a06982014-03-10 09:54:15 -04001356 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EAGAIN;
1358
1359 vm_flags |= VM_SHARED | VM_MAYSHARE;
1360 if (!(file->f_mode & FMODE_WRITE))
1361 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1362
1363 /* fall through */
1364 case MAP_PRIVATE:
1365 if (!(file->f_mode & FMODE_READ))
1366 return -EACCES;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001367 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001368 if (vm_flags & VM_EXEC)
1369 return -EPERM;
1370 vm_flags &= ~VM_MAYEXEC;
1371 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001372
Al Viro72c2d532013-09-22 16:27:52 -04001373 if (!file->f_op->mmap)
Linus Torvalds80c56062006-10-15 14:09:55 -07001374 return -ENODEV;
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001375 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1376 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 break;
1378
1379 default:
1380 return -EINVAL;
1381 }
1382 } else {
1383 switch (flags & MAP_TYPE) {
1384 case MAP_SHARED:
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001385 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1386 return -EINVAL;
Tejun Heoce363942008-09-03 16:09:47 +02001387 /*
1388 * Ignore pgoff.
1389 */
1390 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 vm_flags |= VM_SHARED | VM_MAYSHARE;
1392 break;
1393 case MAP_PRIVATE:
1394 /*
1395 * Set pgoff according to addr for anon_vma.
1396 */
1397 pgoff = addr >> PAGE_SHIFT;
1398 break;
1399 default:
1400 return -EINVAL;
1401 }
1402 }
1403
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001404 /*
1405 * Set 'VM_NORESERVE' if we should not account for the
1406 * memory use of this mapping.
1407 */
1408 if (flags & MAP_NORESERVE) {
1409 /* We honor MAP_NORESERVE if allowed to overcommit */
1410 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1411 vm_flags |= VM_NORESERVE;
1412
1413 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1414 if (file && is_file_hugepages(file))
1415 vm_flags |= VM_NORESERVE;
1416 }
1417
1418 addr = mmap_region(file, addr, len, vm_flags, pgoff);
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -07001419 if (!IS_ERR_VALUE(addr) &&
1420 ((vm_flags & VM_LOCKED) ||
1421 (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
Michel Lespinasse41badc12013-02-22 16:32:47 -08001422 *populate = len;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001423 return addr;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001424}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001425
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001426SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1427 unsigned long, prot, unsigned long, flags,
1428 unsigned long, fd, unsigned long, pgoff)
1429{
1430 struct file *file = NULL;
1431 unsigned long retval = -EBADF;
1432
1433 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001434 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001435 file = fget(fd);
1436 if (!file)
1437 goto out;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001438 if (is_file_hugepages(file))
1439 len = ALIGN(len, huge_page_size(hstate_file(file)));
Jörn Engel493af572013-07-08 16:00:26 -07001440 retval = -EINVAL;
1441 if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
1442 goto out_fput;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001443 } else if (flags & MAP_HUGETLB) {
1444 struct user_struct *user = NULL;
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001445 struct hstate *hs;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001446
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001447 hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & SHM_HUGE_MASK);
Li Zefan091d0d52013-05-09 15:08:15 +08001448 if (!hs)
1449 return -EINVAL;
1450
1451 len = ALIGN(len, huge_page_size(hs));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001452 /*
1453 * VM_NORESERVE is used because the reservations will be
1454 * taken when vm_ops->mmap() is called
1455 * A dummy user value is used because we are not locking
1456 * memory so no accounting is necessary
1457 */
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001458 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
Andi Kleen42d73952012-12-11 16:01:34 -08001459 VM_NORESERVE,
1460 &user, HUGETLB_ANONHUGE_INODE,
1461 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001462 if (IS_ERR(file))
1463 return PTR_ERR(file);
1464 }
1465
1466 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1467
Al Viroeb36c582012-05-30 20:17:35 -04001468 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Jörn Engel493af572013-07-08 16:00:26 -07001469out_fput:
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001470 if (file)
1471 fput(file);
1472out:
1473 return retval;
1474}
1475
Christoph Hellwiga4679372010-03-10 15:21:15 -08001476#ifdef __ARCH_WANT_SYS_OLD_MMAP
1477struct mmap_arg_struct {
1478 unsigned long addr;
1479 unsigned long len;
1480 unsigned long prot;
1481 unsigned long flags;
1482 unsigned long fd;
1483 unsigned long offset;
1484};
1485
1486SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1487{
1488 struct mmap_arg_struct a;
1489
1490 if (copy_from_user(&a, arg, sizeof(a)))
1491 return -EFAULT;
1492 if (a.offset & ~PAGE_MASK)
1493 return -EINVAL;
1494
1495 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1496 a.offset >> PAGE_SHIFT);
1497}
1498#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1499
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001500/*
1501 * Some shared mappigns will want the pages marked read-only
1502 * to track write events. If so, we'll downgrade vm_page_prot
1503 * to the private version (using protection_map[] without the
1504 * VM_SHARED bit).
1505 */
1506int vma_wants_writenotify(struct vm_area_struct *vma)
1507{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001508 vm_flags_t vm_flags = vma->vm_flags;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001509
1510 /* If it was private or non-writable, the write bit is already clear */
1511 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1512 return 0;
1513
1514 /* The backer wishes to know when pages are first written to? */
1515 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1516 return 1;
1517
Peter Feiner64e45502014-10-13 15:55:46 -07001518 /* The open routine did something to the protections that pgprot_modify
1519 * won't preserve? */
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001520 if (pgprot_val(vma->vm_page_prot) !=
Peter Feiner64e45502014-10-13 15:55:46 -07001521 pgprot_val(vm_pgprot_modify(vma->vm_page_prot, vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001522 return 0;
1523
Peter Feiner64e45502014-10-13 15:55:46 -07001524 /* Do we need to track softdirty? */
1525 if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY))
1526 return 1;
1527
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001528 /* Specialty mapping? */
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001529 if (vm_flags & VM_PFNMAP)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001530 return 0;
1531
1532 /* Can the mapping track the dirty pages? */
1533 return vma->vm_file && vma->vm_file->f_mapping &&
1534 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1535}
1536
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001537/*
1538 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001539 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001540 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001541static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001542{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001543 /*
1544 * hugetlb has its own accounting separate from the core VM
1545 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1546 */
1547 if (file && is_file_hugepages(file))
1548 return 0;
1549
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001550 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1551}
1552
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001553unsigned long mmap_region(struct file *file, unsigned long addr,
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001554 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001555{
1556 struct mm_struct *mm = current->mm;
1557 struct vm_area_struct *vma, *prev;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001558 int error;
1559 struct rb_node **rb_link, *rb_parent;
1560 unsigned long charged = 0;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001561
Cyril Hrubise8420a82013-04-29 15:08:33 -07001562 /* Check against address space limit. */
1563 if (!may_expand_vm(mm, len >> PAGE_SHIFT)) {
1564 unsigned long nr_pages;
1565
1566 /*
1567 * MAP_FIXED may remove pages of mappings that intersects with
1568 * requested mapping. Account for the pages it would unmap.
1569 */
1570 if (!(vm_flags & MAP_FIXED))
1571 return -ENOMEM;
1572
1573 nr_pages = count_vma_pages_range(mm, addr, addr + len);
1574
1575 if (!may_expand_vm(mm, (len >> PAGE_SHIFT) - nr_pages))
1576 return -ENOMEM;
1577 }
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* Clear old maps */
1580 error = -ENOMEM;
1581munmap_back:
Hugh Dickins6597d782012-10-08 16:29:07 -07001582 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (do_munmap(mm, addr, len))
1584 return -ENOMEM;
1585 goto munmap_back;
1586 }
1587
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001588 /*
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001589 * Private writable mapping: check memory availability
1590 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001591 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001592 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001593 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001594 return -ENOMEM;
1595 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
1597
1598 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001599 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 */
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001601 vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
1602 if (vma)
1603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 /*
1606 * Determine the object being mapped and call the appropriate
1607 * specific mapper. the address has already been validated, but
1608 * not unmapped, but the maps are removed from the list.
1609 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001610 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (!vma) {
1612 error = -ENOMEM;
1613 goto unacct_error;
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 vma->vm_mm = mm;
1617 vma->vm_start = addr;
1618 vma->vm_end = addr + len;
1619 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001620 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 vma->vm_pgoff = pgoff;
Rik van Riel5beb4932010-03-05 13:42:07 -08001622 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (vm_flags & VM_DENYWRITE) {
1626 error = deny_write_access(file);
1627 if (error)
1628 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001630 if (vm_flags & VM_SHARED) {
1631 error = mapping_map_writable(file->f_mapping);
1632 if (error)
1633 goto allow_write_and_free_vma;
1634 }
1635
1636 /* ->mmap() can change vma->vm_file, but must guarantee that
1637 * vma_link() below can deny write-access if VM_DENYWRITE is set
1638 * and map writably if VM_SHARED is set. This usually means the
1639 * new file must not have been exposed to user-space, yet.
1640 */
Al Virocb0942b2012-08-27 14:48:26 -04001641 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 error = file->f_op->mmap(file, vma);
1643 if (error)
1644 goto unmap_and_free_vma;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001645
1646 /* Can addr have changed??
1647 *
1648 * Answer: Yes, several device drivers can do it in their
1649 * f_op->mmap method. -DaveM
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001650 * Bug: If addr is changed, prev, rb_link, rb_parent should
1651 * be updated for vma_link()
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001652 */
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001653 WARN_ON_ONCE(addr != vma->vm_start);
1654
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001655 addr = vma->vm_start;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001656 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 } else if (vm_flags & VM_SHARED) {
1658 error = shmem_zero_setup(vma);
1659 if (error)
1660 goto free_vma;
1661 }
1662
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001663 vma_link(mm, vma, prev, rb_link, rb_parent);
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001664 /* Once vma denies write, undo our temporary denial count */
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001665 if (file) {
1666 if (vm_flags & VM_SHARED)
1667 mapping_unmap_writable(file->f_mapping);
1668 if (vm_flags & VM_DENYWRITE)
1669 allow_write_access(file);
1670 }
Oleg Nesterove8686772013-09-11 14:20:20 -07001671 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001672out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001673 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001674
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001675 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (vm_flags & VM_LOCKED) {
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001677 if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
1678 vma == get_gate_vma(current->mm)))
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001679 mm->locked_vm += (len >> PAGE_SHIFT);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001680 else
1681 vma->vm_flags &= ~VM_LOCKED;
1682 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301683
Oleg Nesterovc7a3a882012-08-19 19:10:42 +02001684 if (file)
1685 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301686
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001687 /*
1688 * New (or expanded) vma always get soft dirty status.
1689 * Otherwise user-space soft-dirty page tracker won't
1690 * be able to distinguish situation when vma area unmapped,
1691 * then new mapped in-place (which must be aimed as
1692 * a completely new data area).
1693 */
1694 vma->vm_flags |= VM_SOFTDIRTY;
1695
Peter Feiner64e45502014-10-13 15:55:46 -07001696 vma_set_page_prot(vma);
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return addr;
1699
1700unmap_and_free_vma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 vma->vm_file = NULL;
1702 fput(file);
1703
1704 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001705 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1706 charged = 0;
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001707 if (vm_flags & VM_SHARED)
1708 mapping_unmap_writable(file->f_mapping);
1709allow_write_and_free_vma:
1710 if (vm_flags & VM_DENYWRITE)
1711 allow_write_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712free_vma:
1713 kmem_cache_free(vm_area_cachep, vma);
1714unacct_error:
1715 if (charged)
1716 vm_unacct_memory(charged);
1717 return error;
1718}
1719
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001720unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1721{
1722 /*
1723 * We implement the search by looking for an rbtree node that
1724 * immediately follows a suitable gap. That is,
1725 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1726 * - gap_end = vma->vm_start >= info->low_limit + length;
1727 * - gap_end - gap_start >= length
1728 */
1729
1730 struct mm_struct *mm = current->mm;
1731 struct vm_area_struct *vma;
1732 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1733
1734 /* Adjust search length to account for worst case alignment overhead */
1735 length = info->length + info->align_mask;
1736 if (length < info->length)
1737 return -ENOMEM;
1738
1739 /* Adjust search limits by the desired length */
1740 if (info->high_limit < length)
1741 return -ENOMEM;
1742 high_limit = info->high_limit - length;
1743
1744 if (info->low_limit > high_limit)
1745 return -ENOMEM;
1746 low_limit = info->low_limit + length;
1747
1748 /* Check if rbtree root looks promising */
1749 if (RB_EMPTY_ROOT(&mm->mm_rb))
1750 goto check_highest;
1751 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1752 if (vma->rb_subtree_gap < length)
1753 goto check_highest;
1754
1755 while (true) {
1756 /* Visit left subtree if it looks promising */
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001757 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001758 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1759 struct vm_area_struct *left =
1760 rb_entry(vma->vm_rb.rb_left,
1761 struct vm_area_struct, vm_rb);
1762 if (left->rb_subtree_gap >= length) {
1763 vma = left;
1764 continue;
1765 }
1766 }
1767
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001768 gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001769check_current:
1770 /* Check if current node has a suitable gap */
1771 if (gap_start > high_limit)
1772 return -ENOMEM;
Hugh Dickins509f8f12017-06-20 02:10:44 -07001773 if (gap_end >= low_limit &&
1774 gap_end > gap_start && gap_end - gap_start >= length)
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001775 goto found;
1776
1777 /* Visit right subtree if it looks promising */
1778 if (vma->vm_rb.rb_right) {
1779 struct vm_area_struct *right =
1780 rb_entry(vma->vm_rb.rb_right,
1781 struct vm_area_struct, vm_rb);
1782 if (right->rb_subtree_gap >= length) {
1783 vma = right;
1784 continue;
1785 }
1786 }
1787
1788 /* Go back up the rbtree to find next candidate node */
1789 while (true) {
1790 struct rb_node *prev = &vma->vm_rb;
1791 if (!rb_parent(prev))
1792 goto check_highest;
1793 vma = rb_entry(rb_parent(prev),
1794 struct vm_area_struct, vm_rb);
1795 if (prev == vma->vm_rb.rb_left) {
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001796 gap_start = vm_end_gap(vma->vm_prev);
1797 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001798 goto check_current;
1799 }
1800 }
1801 }
1802
1803check_highest:
1804 /* Check highest gap, which does not precede any rbtree node */
1805 gap_start = mm->highest_vm_end;
1806 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1807 if (gap_start > high_limit)
1808 return -ENOMEM;
1809
1810found:
1811 /* We found a suitable gap. Clip it with the original low_limit. */
1812 if (gap_start < info->low_limit)
1813 gap_start = info->low_limit;
1814
1815 /* Adjust gap address to the desired alignment */
1816 gap_start += (info->align_offset - gap_start) & info->align_mask;
1817
1818 VM_BUG_ON(gap_start + info->length > info->high_limit);
1819 VM_BUG_ON(gap_start + info->length > gap_end);
1820 return gap_start;
1821}
1822
1823unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1824{
1825 struct mm_struct *mm = current->mm;
1826 struct vm_area_struct *vma;
1827 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1828
1829 /* Adjust search length to account for worst case alignment overhead */
1830 length = info->length + info->align_mask;
1831 if (length < info->length)
1832 return -ENOMEM;
1833
1834 /*
1835 * Adjust search limits by the desired length.
1836 * See implementation comment at top of unmapped_area().
1837 */
1838 gap_end = info->high_limit;
1839 if (gap_end < length)
1840 return -ENOMEM;
1841 high_limit = gap_end - length;
1842
1843 if (info->low_limit > high_limit)
1844 return -ENOMEM;
1845 low_limit = info->low_limit + length;
1846
1847 /* Check highest gap, which does not precede any rbtree node */
1848 gap_start = mm->highest_vm_end;
1849 if (gap_start <= high_limit)
1850 goto found_highest;
1851
1852 /* Check if rbtree root looks promising */
1853 if (RB_EMPTY_ROOT(&mm->mm_rb))
1854 return -ENOMEM;
1855 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1856 if (vma->rb_subtree_gap < length)
1857 return -ENOMEM;
1858
1859 while (true) {
1860 /* Visit right subtree if it looks promising */
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001861 gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001862 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
1863 struct vm_area_struct *right =
1864 rb_entry(vma->vm_rb.rb_right,
1865 struct vm_area_struct, vm_rb);
1866 if (right->rb_subtree_gap >= length) {
1867 vma = right;
1868 continue;
1869 }
1870 }
1871
1872check_current:
1873 /* Check if current node has a suitable gap */
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001874 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001875 if (gap_end < low_limit)
1876 return -ENOMEM;
Hugh Dickins509f8f12017-06-20 02:10:44 -07001877 if (gap_start <= high_limit &&
1878 gap_end > gap_start && gap_end - gap_start >= length)
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001879 goto found;
1880
1881 /* Visit left subtree if it looks promising */
1882 if (vma->vm_rb.rb_left) {
1883 struct vm_area_struct *left =
1884 rb_entry(vma->vm_rb.rb_left,
1885 struct vm_area_struct, vm_rb);
1886 if (left->rb_subtree_gap >= length) {
1887 vma = left;
1888 continue;
1889 }
1890 }
1891
1892 /* Go back up the rbtree to find next candidate node */
1893 while (true) {
1894 struct rb_node *prev = &vma->vm_rb;
1895 if (!rb_parent(prev))
1896 return -ENOMEM;
1897 vma = rb_entry(rb_parent(prev),
1898 struct vm_area_struct, vm_rb);
1899 if (prev == vma->vm_rb.rb_right) {
1900 gap_start = vma->vm_prev ?
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001901 vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001902 goto check_current;
1903 }
1904 }
1905 }
1906
1907found:
1908 /* We found a suitable gap. Clip it with the original high_limit. */
1909 if (gap_end > info->high_limit)
1910 gap_end = info->high_limit;
1911
1912found_highest:
1913 /* Compute highest gap address at the desired alignment */
1914 gap_end -= info->length;
1915 gap_end -= (gap_end - info->align_offset) & info->align_mask;
1916
1917 VM_BUG_ON(gap_end < info->low_limit);
1918 VM_BUG_ON(gap_end < gap_start);
1919 return gap_end;
1920}
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922/* Get an address range which is currently unmapped.
1923 * For shmat() with addr=0.
1924 *
1925 * Ugly calling convention alert:
1926 * Return value with the low bits set means error value,
1927 * ie
1928 * if (ret & ~PAGE_MASK)
1929 * error = ret;
1930 *
1931 * This function "knows" that -ENOMEM has the bits set.
1932 */
1933#ifndef HAVE_ARCH_UNMAPPED_AREA
1934unsigned long
1935arch_get_unmapped_area(struct file *filp, unsigned long addr,
1936 unsigned long len, unsigned long pgoff, unsigned long flags)
1937{
1938 struct mm_struct *mm = current->mm;
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001939 struct vm_area_struct *vma, *prev;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001940 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001942 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return -ENOMEM;
1944
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001945 if (flags & MAP_FIXED)
1946 return addr;
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (addr) {
1949 addr = PAGE_ALIGN(addr);
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001950 vma = find_vma_prev(mm, addr, &prev);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001951 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001952 (!vma || addr + len <= vm_start_gap(vma)) &&
1953 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return addr;
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001957 info.flags = 0;
1958 info.length = len;
Heiko Carstens4e99b022013-11-12 15:07:54 -08001959 info.low_limit = mm->mmap_base;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001960 info.high_limit = TASK_SIZE;
1961 info.align_mask = 0;
1962 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
vishnu.pscc71aba2014-10-09 15:26:29 -07001964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * This mmap-allocator allocates new areas top-down from below the
1968 * stack's low limit (the base):
1969 */
1970#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1971unsigned long
1972arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1973 const unsigned long len, const unsigned long pgoff,
1974 const unsigned long flags)
1975{
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001976 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 struct mm_struct *mm = current->mm;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001978 unsigned long addr = addr0;
1979 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 /* requested length too big for entire address space */
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001982 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return -ENOMEM;
1984
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001985 if (flags & MAP_FIXED)
1986 return addr;
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /* requesting a specific address */
1989 if (addr) {
1990 addr = PAGE_ALIGN(addr);
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001991 vma = find_vma_prev(mm, addr, &prev);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001992 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07001993 (!vma || addr + len <= vm_start_gap(vma)) &&
1994 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return addr;
1996 }
1997
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001998 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
1999 info.length = len;
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002000 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002001 info.high_limit = mm->mmap_base;
2002 info.align_mask = 0;
2003 addr = vm_unmapped_area(&info);
Xiao Guangrongb716ad92012-03-21 16:33:56 -07002004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /*
2006 * A failed mmap() very likely causes application failure,
2007 * so fall back to the bottom-up function here. This scenario
2008 * can happen with large stack limits and large mmap()
2009 * allocations.
2010 */
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002011 if (addr & ~PAGE_MASK) {
2012 VM_BUG_ON(addr != -ENOMEM);
2013 info.flags = 0;
2014 info.low_limit = TASK_UNMAPPED_BASE;
2015 info.high_limit = TASK_SIZE;
2016 addr = vm_unmapped_area(&info);
2017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 return addr;
2020}
2021#endif
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023unsigned long
2024get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
2025 unsigned long pgoff, unsigned long flags)
2026{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002027 unsigned long (*get_area)(struct file *, unsigned long,
2028 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Al Viro9206de92009-12-03 15:23:11 -05002030 unsigned long error = arch_mmap_check(addr, len, flags);
2031 if (error)
2032 return error;
2033
2034 /* Careful about overflows.. */
2035 if (len > TASK_SIZE)
2036 return -ENOMEM;
2037
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002038 get_area = current->mm->get_unmapped_area;
Al Viro72c2d532013-09-22 16:27:52 -04002039 if (file && file->f_op->get_unmapped_area)
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002040 get_area = file->f_op->get_unmapped_area;
2041 addr = get_area(file, addr, len, pgoff, flags);
2042 if (IS_ERR_VALUE(addr))
2043 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Linus Torvalds07ab67c2005-05-19 22:43:37 -07002045 if (addr > TASK_SIZE - len)
2046 return -ENOMEM;
2047 if (addr & ~PAGE_MASK)
2048 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002049
Al Viro9ac4ed42012-05-30 17:13:15 -04002050 addr = arch_rebalance_pgtables(addr, len);
2051 error = security_mmap_addr(addr);
2052 return error ? error : addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
2055EXPORT_SYMBOL(get_unmapped_area);
2056
2057/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
ZhenwenXu48aae422009-01-06 14:40:21 -08002058struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002060 struct rb_node *rb_node;
2061 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Rajman Mekaco841e31e2012-05-29 15:06:21 -07002063 /* Check the cache first. */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002064 vma = vmacache_find(mm, addr);
2065 if (likely(vma))
2066 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002068 rb_node = mm->mm_rb.rb_node;
2069 vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002071 while (rb_node) {
2072 struct vm_area_struct *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002074 tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
Rajman Mekaco841e31e2012-05-29 15:06:21 -07002075
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002076 if (tmp->vm_end > addr) {
2077 vma = tmp;
2078 if (tmp->vm_start <= addr)
2079 break;
2080 rb_node = rb_node->rb_left;
2081 } else
2082 rb_node = rb_node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002084
2085 if (vma)
2086 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return vma;
2088}
2089
2090EXPORT_SYMBOL(find_vma);
2091
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002092/*
2093 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095struct vm_area_struct *
2096find_vma_prev(struct mm_struct *mm, unsigned long addr,
2097 struct vm_area_struct **pprev)
2098{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002099 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002101 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05002102 if (vma) {
2103 *pprev = vma->vm_prev;
2104 } else {
2105 struct rb_node *rb_node = mm->mm_rb.rb_node;
2106 *pprev = NULL;
2107 while (rb_node) {
2108 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
2109 rb_node = rb_node->rb_right;
2110 }
2111 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002112 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115/*
2116 * Verify that the stack growth is acceptable and
2117 * update accounting. This is shared with both the
2118 * grow-up and grow-down cases.
2119 */
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002120static int acct_stack_growth(struct vm_area_struct *vma,
2121 unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 struct mm_struct *mm = vma->vm_mm;
2124 struct rlimit *rlim = current->signal->rlim;
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002125 unsigned long new_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /* address space limit tests */
akpm@osdl.org119f6572005-05-01 08:58:35 -07002128 if (!may_expand_vm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return -ENOMEM;
2130
2131 /* Stack limit test */
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002132 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return -ENOMEM;
2134
2135 /* mlock limit tests */
2136 if (vma->vm_flags & VM_LOCKED) {
2137 unsigned long locked;
2138 unsigned long limit;
2139 locked = mm->locked_vm + grow;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002140 limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
2141 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (locked > limit && !capable(CAP_IPC_LOCK))
2143 return -ENOMEM;
2144 }
2145
Adam Litke0d59a012007-01-30 14:35:39 -08002146 /* Check to ensure the stack will not grow into a hugetlb-only region */
2147 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
2148 vma->vm_end - size;
2149 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
2150 return -EFAULT;
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /*
2153 * Overcommit.. This must be the final test, as it will
2154 * update security statistics.
2155 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01002156 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return -ENOMEM;
2158
2159 /* Ok, everything looks good - let it rip */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (vma->vm_flags & VM_LOCKED)
2161 mm->locked_vm += grow;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07002162 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return 0;
2164}
2165
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002166#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002168 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2169 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002171int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002173 struct vm_area_struct *next;
2174 unsigned long gap_addr;
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002175 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 if (!(vma->vm_flags & VM_GROWSUP))
2178 return -EFAULT;
2179
Helge Dellerc6aeba62017-06-19 17:34:05 +02002180 /* Guard against exceeding limits of the address space. */
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002181 address &= PAGE_MASK;
Helge Deller07b65f62017-07-14 14:49:38 -07002182 if (address >= (TASK_SIZE & PAGE_MASK))
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002183 return -ENOMEM;
Helge Dellerc6aeba62017-06-19 17:34:05 +02002184 address += PAGE_SIZE;
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002185
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002186 /* Enforce stack_guard_gap */
2187 gap_addr = address + stack_guard_gap;
Helge Dellerc6aeba62017-06-19 17:34:05 +02002188
2189 /* Guard against overflow */
2190 if (gap_addr < address || gap_addr > TASK_SIZE)
2191 gap_addr = TASK_SIZE;
2192
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002193 next = vma->vm_next;
Michal Hocko3e0f3342017-07-10 15:49:51 -07002194 if (next && next->vm_start < gap_addr &&
2195 (next->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002196 if (!(next->vm_flags & VM_GROWSUP))
2197 return -ENOMEM;
2198 /* Check that both stack segments have the same anon_vma? */
2199 }
2200
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002201 /* We must make sure the anon_vma is allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (unlikely(anon_vma_prepare(vma)))
2203 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 /*
2206 * vma->vm_start/vm_end cannot change under us because the caller
2207 * is required to hold the mmap_sem in read mode. We need the
2208 * anon_vma lock to serialize against concurrent expand_stacks.
2209 */
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002210 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /* Somebody else might have raced and expanded it already */
2213 if (address > vma->vm_end) {
2214 unsigned long size, grow;
2215
2216 size = address - vma->vm_start;
2217 grow = (address - vma->vm_end) >> PAGE_SHIFT;
2218
Hugh Dickins42c36f62011-05-09 17:44:42 -07002219 error = -ENOMEM;
2220 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
2221 error = acct_stack_growth(vma, size, grow);
2222 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002223 /*
2224 * vma_gap_update() doesn't support concurrent
2225 * updates, but we only hold a shared mmap_sem
2226 * lock here, so we need to protect against
2227 * concurrent vma expansions.
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002228 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002229 * we don't guarantee that all growable vmas
2230 * in a mm share the same root anon vma.
2231 * So, we reuse mm->page_table_lock to guard
2232 * against concurrent vma expansions.
2233 */
2234 spin_lock(&vma->vm_mm->page_table_lock);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002235 anon_vma_interval_tree_pre_update_vma(vma);
Hugh Dickins42c36f62011-05-09 17:44:42 -07002236 vma->vm_end = address;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002237 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002238 if (vma->vm_next)
2239 vma_gap_update(vma->vm_next);
2240 else
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002241 vma->vm_mm->highest_vm_end = vm_end_gap(vma);
Michel Lespinasse41289972012-12-12 13:52:25 -08002242 spin_unlock(&vma->vm_mm->page_table_lock);
2243
Hugh Dickins42c36f62011-05-09 17:44:42 -07002244 perf_event_mmap(vma);
2245 }
Eric B Munson3af9e852010-05-18 15:30:49 +01002246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002248 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002249 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Michel Lespinasseed8ea812012-10-08 16:31:45 -07002250 validate_mm(vma->vm_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return error;
2252}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002253#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255/*
2256 * vma is the first one with address < vma->vm_start. Have to extend vma.
2257 */
Michal Hockod05f3162011-05-24 17:11:44 -07002258int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002259 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002261 struct vm_area_struct *prev;
2262 unsigned long gap_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 int error;
2264
Eric Paris88694772007-11-26 18:47:26 -05002265 address &= PAGE_MASK;
Al Viroe5467852012-05-30 13:30:51 -04002266 error = security_mmap_addr(address);
Eric Paris88694772007-11-26 18:47:26 -05002267 if (error)
2268 return error;
2269
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002270 /* Enforce stack_guard_gap */
2271 gap_addr = address - stack_guard_gap;
2272 if (gap_addr > address)
2273 return -ENOMEM;
2274 prev = vma->vm_prev;
Michal Hocko3e0f3342017-07-10 15:49:51 -07002275 if (prev && prev->vm_end > gap_addr &&
2276 (prev->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002277 if (!(prev->vm_flags & VM_GROWSDOWN))
2278 return -ENOMEM;
2279 /* Check that both stack segments have the same anon_vma? */
2280 }
2281
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002282 /* We must make sure the anon_vma is allocated. */
2283 if (unlikely(anon_vma_prepare(vma)))
2284 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 /*
2287 * vma->vm_start/vm_end cannot change under us because the caller
2288 * is required to hold the mmap_sem in read mode. We need the
2289 * anon_vma lock to serialize against concurrent expand_stacks.
2290 */
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002291 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 /* Somebody else might have raced and expanded it already */
2294 if (address < vma->vm_start) {
2295 unsigned long size, grow;
2296
2297 size = vma->vm_end - address;
2298 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2299
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002300 error = -ENOMEM;
2301 if (grow <= vma->vm_pgoff) {
2302 error = acct_stack_growth(vma, size, grow);
2303 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002304 /*
2305 * vma_gap_update() doesn't support concurrent
2306 * updates, but we only hold a shared mmap_sem
2307 * lock here, so we need to protect against
2308 * concurrent vma expansions.
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002309 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002310 * we don't guarantee that all growable vmas
2311 * in a mm share the same root anon vma.
2312 * So, we reuse mm->page_table_lock to guard
2313 * against concurrent vma expansions.
2314 */
2315 spin_lock(&vma->vm_mm->page_table_lock);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002316 anon_vma_interval_tree_pre_update_vma(vma);
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002317 vma->vm_start = address;
2318 vma->vm_pgoff -= grow;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002319 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002320 vma_gap_update(vma);
Michel Lespinasse41289972012-12-12 13:52:25 -08002321 spin_unlock(&vma->vm_mm->page_table_lock);
2322
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002323 perf_event_mmap(vma);
2324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326 }
Konstantin Khlebnikov936c7912016-02-05 15:36:50 -08002327 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002328 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Michel Lespinasseed8ea812012-10-08 16:31:45 -07002329 validate_mm(vma->vm_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return error;
2331}
2332
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002333/* enforced gap between the expanding stack and other mappings. */
2334unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
2335
2336static int __init cmdline_parse_stack_guard_gap(char *p)
2337{
2338 unsigned long val;
2339 char *endptr;
2340
2341 val = simple_strtoul(p, &endptr, 10);
2342 if (!*endptr)
2343 stack_guard_gap = val << PAGE_SHIFT;
2344
2345 return 0;
2346}
2347__setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
2348
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002349#ifdef CONFIG_STACK_GROWSUP
2350int expand_stack(struct vm_area_struct *vma, unsigned long address)
2351{
2352 return expand_upwards(vma, address);
2353}
2354
2355struct vm_area_struct *
2356find_extend_vma(struct mm_struct *mm, unsigned long addr)
2357{
2358 struct vm_area_struct *vma, *prev;
2359
2360 addr &= PAGE_MASK;
2361 vma = find_vma_prev(mm, addr, &prev);
2362 if (vma && (vma->vm_start <= addr))
2363 return vma;
Denys Vlasenko1c127182008-11-12 01:24:41 +01002364 if (!prev || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002365 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002366 if (prev->vm_flags & VM_LOCKED)
2367 __mlock_vma_pages_range(prev, addr, prev->vm_end, NULL);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002368 return prev;
2369}
2370#else
2371int expand_stack(struct vm_area_struct *vma, unsigned long address)
2372{
2373 return expand_downwards(vma, address);
2374}
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376struct vm_area_struct *
vishnu.pscc71aba2014-10-09 15:26:29 -07002377find_extend_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
vishnu.pscc71aba2014-10-09 15:26:29 -07002379 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 unsigned long start;
2381
2382 addr &= PAGE_MASK;
vishnu.pscc71aba2014-10-09 15:26:29 -07002383 vma = find_vma(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (!vma)
2385 return NULL;
2386 if (vma->vm_start <= addr)
2387 return vma;
2388 if (!(vma->vm_flags & VM_GROWSDOWN))
2389 return NULL;
2390 start = vma->vm_start;
2391 if (expand_stack(vma, addr))
2392 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002393 if (vma->vm_flags & VM_LOCKED)
2394 __mlock_vma_pages_range(vma, addr, start, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return vma;
2396}
2397#endif
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002400 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002402 *
2403 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002405static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002407 unsigned long nr_accounted = 0;
2408
Hugh Dickins365e9c872005-10-29 18:16:18 -07002409 /* Update high watermark before we lower total_vm */
2410 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002412 long nrpages = vma_pages(vma);
2413
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002414 if (vma->vm_flags & VM_ACCOUNT)
2415 nr_accounted += nrpages;
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002416 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002417 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002418 } while (vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002419 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 validate_mm(mm);
2421}
2422
2423/*
2424 * Get rid of page table information in the indicated region.
2425 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002426 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 */
2428static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002429 struct vm_area_struct *vma, struct vm_area_struct *prev,
2430 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
vishnu.pscc71aba2014-10-09 15:26:29 -07002432 struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002433 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07002436 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002437 update_hiwater_rss(mm);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002438 unmap_vmas(&tlb, vma, start, end);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002439 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins6ee86302013-04-29 15:07:44 -07002440 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002441 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
2444/*
2445 * Create a list of vma's touched by the unmap, removing them from the mm's
2446 * vma list as we go..
2447 */
2448static void
2449detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2450 struct vm_area_struct *prev, unsigned long end)
2451{
2452 struct vm_area_struct **insertion_point;
2453 struct vm_area_struct *tail_vma = NULL;
2454
2455 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002456 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 do {
Michel Lespinassed3737182012-12-11 16:01:38 -08002458 vma_rb_erase(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 mm->map_count--;
2460 tail_vma = vma;
2461 vma = vma->vm_next;
2462 } while (vma && vma->vm_start < end);
2463 *insertion_point = vma;
Michel Lespinassed3737182012-12-11 16:01:38 -08002464 if (vma) {
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002465 vma->vm_prev = prev;
Michel Lespinassed3737182012-12-11 16:01:38 -08002466 vma_gap_update(vma);
2467 } else
Hugh Dickinsd4712eb2017-06-19 04:03:24 -07002468 mm->highest_vm_end = prev ? vm_end_gap(prev) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 tail_vma->vm_next = NULL;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002470
2471 /* Kill the cache */
2472 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
2475/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002476 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2477 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 */
vishnu.pscc71aba2014-10-09 15:26:29 -07002479static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 unsigned long addr, int new_below)
2481{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 struct vm_area_struct *new;
Rik van Riel5beb4932010-03-05 13:42:07 -08002483 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Andi Kleena5516432008-07-23 21:27:41 -07002485 if (is_vm_hugetlb_page(vma) && (addr &
2486 ~(huge_page_mask(hstate_vma(vma)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return -EINVAL;
2488
Christoph Lametere94b1762006-12-06 20:33:17 -08002489 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (!new)
Rik van Riel5beb4932010-03-05 13:42:07 -08002491 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 /* most fields are the same, copy all, and then fixup */
2494 *new = *vma;
2495
Rik van Riel5beb4932010-03-05 13:42:07 -08002496 INIT_LIST_HEAD(&new->anon_vma_chain);
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (new_below)
2499 new->vm_end = addr;
2500 else {
2501 new->vm_start = addr;
2502 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2503 }
2504
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002505 err = vma_dup_policy(vma, new);
2506 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002507 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Daniel Forrestc4ea95d2014-12-02 15:59:42 -08002509 err = anon_vma_clone(new, vma);
2510 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002511 goto out_free_mpol;
2512
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002513 if (new->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 get_file(new->vm_file);
2515
2516 if (new->vm_ops && new->vm_ops->open)
2517 new->vm_ops->open(new);
2518
2519 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002520 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2522 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002523 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Rik van Riel5beb4932010-03-05 13:42:07 -08002525 /* Success. */
2526 if (!err)
2527 return 0;
2528
2529 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002530 if (new->vm_ops && new->vm_ops->close)
2531 new->vm_ops->close(new);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002532 if (new->vm_file)
Rik van Riel5beb4932010-03-05 13:42:07 -08002533 fput(new->vm_file);
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002534 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002535 out_free_mpol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002536 mpol_put(vma_policy(new));
Rik van Riel5beb4932010-03-05 13:42:07 -08002537 out_free_vma:
2538 kmem_cache_free(vm_area_cachep, new);
2539 out_err:
2540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002543/*
2544 * Split a vma into two pieces at address 'addr', a new vma is allocated
2545 * either for the first part or the tail.
2546 */
2547int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2548 unsigned long addr, int new_below)
2549{
2550 if (mm->map_count >= sysctl_max_map_count)
2551 return -ENOMEM;
2552
2553 return __split_vma(mm, vma, addr, new_below);
2554}
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556/* Munmap is split into 2 main parts -- this part which finds
2557 * what needs doing, and the areas themselves, which do the
2558 * work. This now handles partial unmappings.
2559 * Jeremy Fitzhardinge <jeremy@goop.org>
2560 */
2561int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2562{
2563 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002564 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
2567 return -EINVAL;
2568
vishnu.pscc71aba2014-10-09 15:26:29 -07002569 len = PAGE_ALIGN(len);
2570 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return -EINVAL;
2572
2573 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002574 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002575 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002577 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002578 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 /* if it doesn't overlap, we have nothing.. */
2581 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002582 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return 0;
2584
2585 /*
2586 * If we need to split any vma, do it now to save pain later.
2587 *
2588 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2589 * unmapped vm_area_struct will remain in use: so lower split_vma
2590 * places tmp vma above, and higher split_vma places tmp vma below.
2591 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002592 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002593 int error;
2594
2595 /*
2596 * Make sure that map_count on return from munmap() will
2597 * not exceed its limit; but let map_count go just above
2598 * its limit temporarily, to help free resources as expected.
2599 */
2600 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2601 return -ENOMEM;
2602
2603 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (error)
2605 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002606 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608
2609 /* Does it split the last one? */
2610 last = find_vma(mm, end);
2611 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002612 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (error)
2614 return error;
2615 }
vishnu.pscc71aba2014-10-09 15:26:29 -07002616 vma = prev ? prev->vm_next : mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002619 * unlock any mlock()ed ranges before detaching vmas
2620 */
2621 if (mm->locked_vm) {
2622 struct vm_area_struct *tmp = vma;
2623 while (tmp && tmp->vm_start < end) {
2624 if (tmp->vm_flags & VM_LOCKED) {
2625 mm->locked_vm -= vma_pages(tmp);
2626 munlock_vma_pages_all(tmp);
2627 }
2628 tmp = tmp->vm_next;
2629 }
2630 }
2631
2632 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 * Remove the vma's, and unmap the actual pages
2634 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002635 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2636 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002639 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 return 0;
2642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Al Virobfce2812012-04-20 21:57:04 -04002644int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002645{
2646 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002647 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002648
2649 down_write(&mm->mmap_sem);
2650 ret = do_munmap(mm, start, len);
2651 up_write(&mm->mmap_sem);
2652 return ret;
2653}
2654EXPORT_SYMBOL(vm_munmap);
2655
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002656SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 profile_munmap(addr);
Al Virobfce2812012-04-20 21:57:04 -04002659 return vm_munmap(addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
2662static inline void verify_mm_writelocked(struct mm_struct *mm)
2663{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002664#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2666 WARN_ON(1);
2667 up_read(&mm->mmap_sem);
2668 }
2669#endif
2670}
2671
2672/*
2673 * this is really a simplified "do_mmap". it only handles
2674 * anonymous maps. eventually we may be able to do some
2675 * brk-specific accounting here.
2676 */
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002677static unsigned long do_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
vishnu.pscc71aba2014-10-09 15:26:29 -07002679 struct mm_struct *mm = current->mm;
2680 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 unsigned long flags;
vishnu.pscc71aba2014-10-09 15:26:29 -07002682 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002684 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 len = PAGE_ALIGN(len);
2687 if (!len)
2688 return addr;
2689
Kirill Korotaev3a459752006-09-07 14:17:04 +04002690 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2691
Al Viro2c6a1012009-12-03 19:40:46 -05002692 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
2693 if (error & ~PAGE_MASK)
Kirill Korotaev3a459752006-09-07 14:17:04 +04002694 return error;
2695
Davidlohr Bueso363ee172014-01-21 15:49:15 -08002696 error = mlock_future_check(mm, mm->def_flags, len);
2697 if (error)
2698 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 /*
2701 * mm->mmap_sem is required to protect against another thread
2702 * changing the mappings in case we sleep.
2703 */
2704 verify_mm_writelocked(mm);
2705
2706 /*
2707 * Clear old maps. this also does some error checking for us
2708 */
2709 munmap_back:
Hugh Dickins6597d782012-10-08 16:29:07 -07002710 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (do_munmap(mm, addr, len))
2712 return -ENOMEM;
2713 goto munmap_back;
2714 }
2715
2716 /* Check against address space limits *after* clearing old maps... */
akpm@osdl.org119f6572005-05-01 08:58:35 -07002717 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return -ENOMEM;
2719
2720 if (mm->map_count > sysctl_max_map_count)
2721 return -ENOMEM;
2722
Al Viro191c5422012-02-13 03:58:52 +00002723 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return -ENOMEM;
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07002727 vma = vma_merge(mm, prev, addr, addr + len, flags,
2728 NULL, NULL, pgoff, NULL);
2729 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 goto out;
2731
2732 /*
2733 * create a vma struct for an anonymous mapping
2734 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002735 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 if (!vma) {
2737 vm_unacct_memory(len >> PAGE_SHIFT);
2738 return -ENOMEM;
2739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Rik van Riel5beb4932010-03-05 13:42:07 -08002741 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 vma->vm_mm = mm;
2743 vma->vm_start = addr;
2744 vma->vm_end = addr + len;
2745 vma->vm_pgoff = pgoff;
2746 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07002747 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 vma_link(mm, vma, prev, rb_link, rb_parent);
2749out:
Eric B Munson3af9e852010-05-18 15:30:49 +01002750 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 mm->total_vm += len >> PAGE_SHIFT;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002752 if (flags & VM_LOCKED)
2753 mm->locked_vm += (len >> PAGE_SHIFT);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07002754 vma->vm_flags |= VM_SOFTDIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return addr;
2756}
2757
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002758unsigned long vm_brk(unsigned long addr, unsigned long len)
2759{
2760 struct mm_struct *mm = current->mm;
2761 unsigned long ret;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002762 bool populate;
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002763
2764 down_write(&mm->mmap_sem);
2765 ret = do_brk(addr, len);
Michel Lespinasse128557f2013-02-22 16:32:40 -08002766 populate = ((mm->def_flags & VM_LOCKED) != 0);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002767 up_write(&mm->mmap_sem);
Michel Lespinasse128557f2013-02-22 16:32:40 -08002768 if (populate)
2769 mm_populate(addr, len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002770 return ret;
2771}
2772EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774/* Release all mmaps. */
2775void exit_mmap(struct mm_struct *mm)
2776{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002777 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07002778 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 unsigned long nr_accounted = 0;
2780
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002781 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002782 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002783
Rik van Rielba470de2008-10-18 20:26:50 -07002784 if (mm->locked_vm) {
2785 vma = mm->mmap;
2786 while (vma) {
2787 if (vma->vm_flags & VM_LOCKED)
2788 munlock_vma_pages_all(vma);
2789 vma = vma->vm_next;
2790 }
2791 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002792
2793 arch_exit_mmap(mm);
2794
Rik van Rielba470de2008-10-18 20:26:50 -07002795 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002796 if (!vma) /* Can happen if dup_mmap() received an OOM */
2797 return;
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 flush_cache_mm(mm);
Linus Torvalds2b047252013-08-15 11:42:25 -07002801 tlb_gather_mmu(&tlb, mm, 0, -1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08002802 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07002803 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002804 unmap_vmas(&tlb, vma, 0, -1);
Hugh Dickins9ba69292009-09-21 17:02:20 -07002805
Hugh Dickins6ee86302013-04-29 15:07:44 -07002806 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05002807 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07002810 * Walk the list again, actually closing and freeing it,
2811 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002813 while (vma) {
2814 if (vma->vm_flags & VM_ACCOUNT)
2815 nr_accounted += vma_pages(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002816 vma = remove_vma(vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002817 }
2818 vm_unacct_memory(nr_accounted);
Hugh Dickinse0da3822005-04-19 13:29:15 -07002819
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -08002820 WARN_ON(atomic_long_read(&mm->nr_ptes) >
2821 (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
2824/* Insert vm structure into process list sorted by address
2825 * and into the inode's i_mmap tree. If vm_file is non-NULL
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002826 * then i_mmap_mutex is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 */
Hugh Dickins6597d782012-10-08 16:29:07 -07002828int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Hugh Dickins6597d782012-10-08 16:29:07 -07002830 struct vm_area_struct *prev;
2831 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 /*
2834 * The vm_pgoff of a purely anonymous vma should be irrelevant
2835 * until its first write fault, when page's anon_vma and index
2836 * are set. But now set the vm_pgoff it will almost certainly
2837 * end up with (unless mremap moves it elsewhere before that
2838 * first wfault), so /proc/pid/maps tells a consistent story.
2839 *
2840 * By setting it to reflect the virtual start address of the
2841 * vma, merges and splits can happen in a seamless way, just
2842 * using the existing file pgoff checks and manipulations.
2843 * Similarly in do_mmap_pgoff and in do_brk.
2844 */
2845 if (!vma->vm_file) {
2846 BUG_ON(vma->anon_vma);
2847 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2848 }
Hugh Dickins6597d782012-10-08 16:29:07 -07002849 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
2850 &prev, &rb_link, &rb_parent))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return -ENOMEM;
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002852 if ((vma->vm_flags & VM_ACCOUNT) &&
Alan Cox34b4e4a2007-08-22 14:01:28 -07002853 security_vm_enough_memory_mm(mm, vma_pages(vma)))
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002854 return -ENOMEM;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 vma_link(mm, vma, prev, rb_link, rb_parent);
2857 return 0;
2858}
2859
2860/*
2861 * Copy the vma structure to a new location in the same mm,
2862 * prior to moving page table entries, to effect an mremap move.
2863 */
2864struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
Michel Lespinasse38a76012012-10-08 16:31:50 -07002865 unsigned long addr, unsigned long len, pgoff_t pgoff,
2866 bool *need_rmap_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 struct vm_area_struct *vma = *vmap;
2869 unsigned long vma_start = vma->vm_start;
2870 struct mm_struct *mm = vma->vm_mm;
2871 struct vm_area_struct *new_vma, *prev;
2872 struct rb_node **rb_link, *rb_parent;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002873 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 /*
2876 * If anonymous vma has not yet been faulted, update new pgoff
2877 * to match new location, to increase its chance of merging.
2878 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002879 if (unlikely(!vma->vm_file && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002881 faulted_in_anon_vma = false;
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Hugh Dickins6597d782012-10-08 16:29:07 -07002884 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
2885 return NULL; /* should never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2887 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2888 if (new_vma) {
2889 /*
2890 * Source vma may have been merged into new_vma
2891 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002892 if (unlikely(vma_start >= new_vma->vm_start &&
2893 vma_start < new_vma->vm_end)) {
2894 /*
2895 * The only way we can get a vma_merge with
2896 * self during an mremap is if the vma hasn't
2897 * been faulted in yet and we were allowed to
2898 * reset the dst vma->vm_pgoff to the
2899 * destination address of the mremap to allow
2900 * the merge to happen. mremap must change the
2901 * vm_pgoff linearity between src and dst vmas
2902 * (in turn preventing a vma_merge) to be
2903 * safe. It is only safe to keep the vm_pgoff
2904 * linear if there are no pages mapped yet.
2905 */
Sasha Levin81d1b092014-10-09 15:28:10 -07002906 VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
Michel Lespinasse38a76012012-10-08 16:31:50 -07002907 *vmap = vma = new_vma;
Michel Lespinasse108d6642012-10-08 16:31:36 -07002908 }
Michel Lespinasse38a76012012-10-08 16:31:50 -07002909 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08002911 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (new_vma) {
2913 *new_vma = *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 new_vma->vm_start = addr;
2915 new_vma->vm_end = addr + len;
2916 new_vma->vm_pgoff = pgoff;
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002917 if (vma_dup_policy(vma, new_vma))
Michel Lespinasse523d4e22012-10-08 16:31:48 -07002918 goto out_free_vma;
Michel Lespinasse523d4e22012-10-08 16:31:48 -07002919 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2920 if (anon_vma_clone(new_vma, vma))
2921 goto out_free_mempol;
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002922 if (new_vma->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 get_file(new_vma->vm_file);
2924 if (new_vma->vm_ops && new_vma->vm_ops->open)
2925 new_vma->vm_ops->open(new_vma);
2926 vma_link(mm, new_vma, prev, rb_link, rb_parent);
Michel Lespinasse38a76012012-10-08 16:31:50 -07002927 *need_rmap_locks = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
2929 }
2930 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002931
2932 out_free_mempol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002933 mpol_put(vma_policy(new_vma));
Rik van Riel5beb4932010-03-05 13:42:07 -08002934 out_free_vma:
2935 kmem_cache_free(vm_area_cachep, new_vma);
2936 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
akpm@osdl.org119f6572005-05-01 08:58:35 -07002938
2939/*
2940 * Return true if the calling process may expand its vm space by the passed
2941 * number of pages
2942 */
2943int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2944{
2945 unsigned long cur = mm->total_vm; /* pages */
2946 unsigned long lim;
2947
Jiri Slaby59e99e52010-03-05 13:41:44 -08002948 lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002949
2950 if (cur + npages > lim)
2951 return 0;
2952 return 1;
2953}
Roland McGrathfa5dc222007-02-08 14:20:41 -08002954
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07002955static int special_mapping_fault(struct vm_area_struct *vma,
2956 struct vm_fault *vmf);
2957
2958/*
2959 * Having a close hook prevents vma merging regardless of flags.
2960 */
2961static void special_mapping_close(struct vm_area_struct *vma)
2962{
2963}
2964
2965static const char *special_mapping_name(struct vm_area_struct *vma)
2966{
2967 return ((struct vm_special_mapping *)vma->vm_private_data)->name;
2968}
2969
2970static const struct vm_operations_struct special_mapping_vmops = {
2971 .close = special_mapping_close,
2972 .fault = special_mapping_fault,
2973 .name = special_mapping_name,
2974};
2975
2976static const struct vm_operations_struct legacy_special_mapping_vmops = {
2977 .close = special_mapping_close,
2978 .fault = special_mapping_fault,
2979};
Roland McGrathfa5dc222007-02-08 14:20:41 -08002980
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002981static int special_mapping_fault(struct vm_area_struct *vma,
2982 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08002983{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002984 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002985 struct page **pages;
2986
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002987 /*
2988 * special mappings have no vm_file, and in that case, the mm
2989 * uses vm_pgoff internally. So we have to subtract it from here.
2990 * We are allowed to do this because we are the mm; do not copy
2991 * this code into drivers!
2992 */
2993 pgoff = vmf->pgoff - vma->vm_pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002994
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07002995 if (vma->vm_ops == &legacy_special_mapping_vmops)
2996 pages = vma->vm_private_data;
2997 else
2998 pages = ((struct vm_special_mapping *)vma->vm_private_data)->
2999 pages;
3000
3001 for (; pgoff && *pages; ++pages)
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003002 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003003
3004 if (*pages) {
3005 struct page *page = *pages;
3006 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003007 vmf->page = page;
3008 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003009 }
3010
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003011 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003012}
3013
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003014static struct vm_area_struct *__install_special_mapping(
3015 struct mm_struct *mm,
3016 unsigned long addr, unsigned long len,
3017 unsigned long vm_flags, const struct vm_operations_struct *ops,
3018 void *priv)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003019{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003020 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003021 struct vm_area_struct *vma;
3022
3023 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
3024 if (unlikely(vma == NULL))
Stefani Seibold3935ed62014-03-17 23:22:02 +01003025 return ERR_PTR(-ENOMEM);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003026
Rik van Riel5beb4932010-03-05 13:42:07 -08003027 INIT_LIST_HEAD(&vma->anon_vma_chain);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003028 vma->vm_mm = mm;
3029 vma->vm_start = addr;
3030 vma->vm_end = addr + len;
3031
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07003032 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY;
Coly Li3ed75eb2007-10-18 23:39:15 -07003033 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003034
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003035 vma->vm_ops = ops;
3036 vma->vm_private_data = priv;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003037
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003038 ret = insert_vm_struct(mm, vma);
3039 if (ret)
3040 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003041
3042 mm->total_vm += len >> PAGE_SHIFT;
3043
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02003045
Stefani Seibold3935ed62014-03-17 23:22:02 +01003046 return vma;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003047
3048out:
3049 kmem_cache_free(vm_area_cachep, vma);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003050 return ERR_PTR(ret);
3051}
3052
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003053/*
3054 * Called with mm->mmap_sem held for writing.
3055 * Insert a new vma covering the given region, with the given flags.
3056 * Its pages are supplied by the given array of struct page *.
3057 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3058 * The region past the last page supplied will always produce SIGBUS.
3059 * The array pointer and the pages it points to are assumed to stay alive
3060 * for as long as this mapping might exist.
3061 */
3062struct vm_area_struct *_install_special_mapping(
3063 struct mm_struct *mm,
3064 unsigned long addr, unsigned long len,
3065 unsigned long vm_flags, const struct vm_special_mapping *spec)
3066{
3067 return __install_special_mapping(mm, addr, len, vm_flags,
3068 &special_mapping_vmops, (void *)spec);
3069}
3070
Stefani Seibold3935ed62014-03-17 23:22:02 +01003071int install_special_mapping(struct mm_struct *mm,
3072 unsigned long addr, unsigned long len,
3073 unsigned long vm_flags, struct page **pages)
3074{
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003075 struct vm_area_struct *vma = __install_special_mapping(
3076 mm, addr, len, vm_flags, &legacy_special_mapping_vmops,
3077 (void *)pages);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003078
Duan Jiong14bd5b42014-06-04 16:07:05 -07003079 return PTR_ERR_OR_ZERO(vma);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003080}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003081
3082static DEFINE_MUTEX(mm_all_locks_mutex);
3083
Peter Zijlstra454ed842008-08-11 09:30:25 +02003084static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003085{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003086 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003087 /*
3088 * The LSB of head.next can't change from under us
3089 * because we hold the mm_all_locks_mutex.
3090 */
Jiri Kosina572043c2013-01-11 14:31:59 -08003091 down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003092 /*
3093 * We can safely modify head.next after taking the
Ingo Molnar5a505082012-12-02 19:56:46 +00003094 * anon_vma->root->rwsem. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003095 * the same anon_vma we won't take it again.
3096 *
3097 * No need of atomic instructions here, head.next
3098 * can't change from under us thanks to the
Ingo Molnar5a505082012-12-02 19:56:46 +00003099 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003100 */
3101 if (__test_and_set_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003102 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003103 BUG();
3104 }
3105}
3106
Peter Zijlstra454ed842008-08-11 09:30:25 +02003107static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003108{
3109 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3110 /*
3111 * AS_MM_ALL_LOCKS can't change from under us because
3112 * we hold the mm_all_locks_mutex.
3113 *
3114 * Operations on ->flags have to be atomic because
3115 * even if AS_MM_ALL_LOCKS is stable thanks to the
3116 * mm_all_locks_mutex, there may be other cpus
3117 * changing other bitflags in parallel to us.
3118 */
3119 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
3120 BUG();
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07003121 mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003122 }
3123}
3124
3125/*
3126 * This operation locks against the VM for all pte/vma/mm related
3127 * operations that could ever happen on a certain mm. This includes
3128 * vmtruncate, try_to_unmap, and all page faults.
3129 *
3130 * The caller must take the mmap_sem in write mode before calling
3131 * mm_take_all_locks(). The caller isn't allowed to release the
3132 * mmap_sem until mm_drop_all_locks() returns.
3133 *
3134 * mmap_sem in write mode is required in order to block all operations
3135 * that could modify pagetables and free pages without need of
3136 * altering the vma layout (for example populate_range() with
3137 * nonlinear vmas). It's also needed in write mode to avoid new
3138 * anon_vmas to be associated with existing vmas.
3139 *
3140 * A single task can't take more than one mm_take_all_locks() in a row
3141 * or it would deadlock.
3142 *
Michel Lespinassebf181b92012-10-08 16:31:39 -07003143 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003144 * mapping->flags avoid to take the same lock twice, if more than one
3145 * vma in this mm is backed by the same anon_vma or address_space.
3146 *
3147 * We can take all the locks in random order because the VM code
Yuanhan Liu631b0cf2013-02-04 14:28:48 -08003148 * taking i_mmap_mutex or anon_vma->rwsem outside the mmap_sem never
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003149 * takes more than one of them in a row. Secondly we're protected
3150 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
3151 *
3152 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3153 * that may have to take thousand of locks.
3154 *
3155 * mm_take_all_locks() can fail if it's interrupted by signals.
3156 */
3157int mm_take_all_locks(struct mm_struct *mm)
3158{
3159 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003160 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003161
3162 BUG_ON(down_read_trylock(&mm->mmap_sem));
3163
3164 mutex_lock(&mm_all_locks_mutex);
3165
3166 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3167 if (signal_pending(current))
3168 goto out_unlock;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003169 if (vma->vm_file && vma->vm_file->f_mapping)
Peter Zijlstra454ed842008-08-11 09:30:25 +02003170 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003171 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003172
3173 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3174 if (signal_pending(current))
3175 goto out_unlock;
3176 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003177 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3178 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003179 }
3180
Kautuk Consul584cff52011-10-31 17:08:59 -07003181 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003182
3183out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07003184 mm_drop_all_locks(mm);
3185 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003186}
3187
3188static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
3189{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003190 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003191 /*
3192 * The LSB of head.next can't change to 0 from under
3193 * us because we hold the mm_all_locks_mutex.
3194 *
3195 * We must however clear the bitflag before unlocking
Michel Lespinassebf181b92012-10-08 16:31:39 -07003196 * the vma so the users using the anon_vma->rb_root will
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003197 * never see our bitflag.
3198 *
3199 * No need of atomic instructions here, head.next
3200 * can't change from under us until we release the
Ingo Molnar5a505082012-12-02 19:56:46 +00003201 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003202 */
3203 if (!__test_and_clear_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003204 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003205 BUG();
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08003206 anon_vma_unlock_write(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003207 }
3208}
3209
3210static void vm_unlock_mapping(struct address_space *mapping)
3211{
3212 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3213 /*
3214 * AS_MM_ALL_LOCKS can't change to 0 from under us
3215 * because we hold the mm_all_locks_mutex.
3216 */
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07003217 mutex_unlock(&mapping->i_mmap_mutex);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003218 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
3219 &mapping->flags))
3220 BUG();
3221 }
3222}
3223
3224/*
3225 * The mmap_sem cannot be released by the caller until
3226 * mm_drop_all_locks() returns.
3227 */
3228void mm_drop_all_locks(struct mm_struct *mm)
3229{
3230 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003231 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003232
3233 BUG_ON(down_read_trylock(&mm->mmap_sem));
3234 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
3235
3236 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3237 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003238 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3239 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003240 if (vma->vm_file && vma->vm_file->f_mapping)
3241 vm_unlock_mapping(vma->vm_file->f_mapping);
3242 }
3243
3244 mutex_unlock(&mm_all_locks_mutex);
3245}
David Howells8feae132009-01-08 12:04:47 +00003246
3247/*
3248 * initialise the VMA slab
3249 */
3250void __init mmap_init(void)
3251{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003252 int ret;
3253
Tejun Heo908c7f12014-09-08 09:51:29 +09003254 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003255 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00003256}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003257
3258/*
3259 * Initialise sysctl_user_reserve_kbytes.
3260 *
3261 * This is intended to prevent a user from starting a single memory hogging
3262 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3263 * mode.
3264 *
3265 * The default value is min(3% of free memory, 128MB)
3266 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3267 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003268static int init_user_reserve(void)
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003269{
3270 unsigned long free_kbytes;
3271
3272 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3273
3274 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
3275 return 0;
3276}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003277subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003278
3279/*
3280 * Initialise sysctl_admin_reserve_kbytes.
3281 *
3282 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3283 * to log in and kill a memory hogging process.
3284 *
3285 * Systems with more than 256MB will reserve 8MB, enough to recover
3286 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3287 * only reserve 3% of free pages by default.
3288 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003289static int init_admin_reserve(void)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003290{
3291 unsigned long free_kbytes;
3292
3293 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3294
3295 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
3296 return 0;
3297}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003298subsys_initcall(init_admin_reserve);
Andrew Shewmaker16408792013-04-29 15:08:12 -07003299
3300/*
3301 * Reinititalise user and admin reserves if memory is added or removed.
3302 *
3303 * The default user reserve max is 128MB, and the default max for the
3304 * admin reserve is 8MB. These are usually, but not always, enough to
3305 * enable recovery from a memory hogging process using login/sshd, a shell,
3306 * and tools like top. It may make sense to increase or even disable the
3307 * reserve depending on the existence of swap or variations in the recovery
3308 * tools. So, the admin may have changed them.
3309 *
3310 * If memory is added and the reserves have been eliminated or increased above
3311 * the default max, then we'll trust the admin.
3312 *
3313 * If memory is removed and there isn't enough free memory, then we
3314 * need to reset the reserves.
3315 *
3316 * Otherwise keep the reserve set by the admin.
3317 */
3318static int reserve_mem_notifier(struct notifier_block *nb,
3319 unsigned long action, void *data)
3320{
3321 unsigned long tmp, free_kbytes;
3322
3323 switch (action) {
3324 case MEM_ONLINE:
3325 /* Default max is 128MB. Leave alone if modified by operator. */
3326 tmp = sysctl_user_reserve_kbytes;
3327 if (0 < tmp && tmp < (1UL << 17))
3328 init_user_reserve();
3329
3330 /* Default max is 8MB. Leave alone if modified by operator. */
3331 tmp = sysctl_admin_reserve_kbytes;
3332 if (0 < tmp && tmp < (1UL << 13))
3333 init_admin_reserve();
3334
3335 break;
3336 case MEM_OFFLINE:
3337 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3338
3339 if (sysctl_user_reserve_kbytes > free_kbytes) {
3340 init_user_reserve();
3341 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3342 sysctl_user_reserve_kbytes);
3343 }
3344
3345 if (sysctl_admin_reserve_kbytes > free_kbytes) {
3346 init_admin_reserve();
3347 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3348 sysctl_admin_reserve_kbytes);
3349 }
3350 break;
3351 default:
3352 break;
3353 }
3354 return NOTIFY_OK;
3355}
3356
3357static struct notifier_block reserve_mem_nb = {
3358 .notifier_call = reserve_mem_notifier,
3359};
3360
3361static int __meminit init_reserve_notifier(void)
3362{
3363 if (register_hotmemory_notifier(&reserve_mem_nb))
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07003364 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
Andrew Shewmaker16408792013-04-29 15:08:12 -07003365
3366 return 0;
3367}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003368subsys_initcall(init_reserve_notifier);