blob: bb10498ae49d5cd0433d19e77cf32b04b1661316 [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
Cyril Hrubis3e55f212013-04-29 15:08:33 -07009#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/slab.h>
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070011#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/shm.h>
14#include <linux/mman.h>
15#include <linux/pagemap.h>
16#include <linux/swap.h>
17#include <linux/syscalls.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include <linux/file.h>
21#include <linux/fs.h>
22#include <linux/personality.h>
23#include <linux/security.h>
24#include <linux/hugetlb.h>
25#include <linux/profile.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mount.h>
28#include <linux/mempolicy.h>
29#include <linux/rmap.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070030#include <linux/mmu_notifier.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/perf_event.h>
Al Viro120a7952010-10-30 02:54:44 -040032#include <linux/audit.h>
Andrea Arcangelib15d00b2011-01-13 15:46:59 -080033#include <linux/khugepaged.h>
Andrea Arcangelieb01a292019-04-18 17:50:52 -070034#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/uaccess.h>
37#include <asm/cacheflush.h>
38#include <asm/tlb.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020039#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jan Beulich42b77722008-07-23 21:27:10 -070041#include "internal.h"
42
Kirill Korotaev3a459752006-09-07 14:17:04 +040043#ifndef arch_mmap_check
44#define arch_mmap_check(addr, len, flags) (0)
45#endif
46
Martin Schwidefsky08e7d9b2008-02-04 22:29:16 -080047#ifndef arch_rebalance_pgtables
48#define arch_rebalance_pgtables(addr, len) (addr)
49#endif
50
dcashmanaa5b5c52015-12-29 14:24:39 -080051#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
52const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
53const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
54int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
55#endif
56#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
57const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
58const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
59int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
60#endif
61
62
Hugh Dickinse0da3822005-04-19 13:29:15 -070063static void unmap_region(struct mm_struct *mm,
64 struct vm_area_struct *vma, struct vm_area_struct *prev,
65 unsigned long start, unsigned long end);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * WARNING: the debugging will use recursive algorithms so never enable this
69 * unless you know what you are doing.
70 */
71#undef DEBUG_MM_RB
72
73/* description of effects of mapping type and prot in current implementation.
74 * this is due to the limited x86 page protection hardware. The expected
75 * behavior is in parens:
76 *
77 * map_type prot
78 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
79 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
80 * w: (no) no w: (no) no w: (yes) yes w: (no) no
81 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
82 *
83 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
84 * w: (no) no w: (no) no w: (copy) copy w: (no) no
85 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
86 *
87 */
88pgprot_t protection_map[16] = {
89 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
90 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
91};
92
Hugh Dickins804af2c2006-07-26 21:39:49 +010093pgprot_t vm_get_page_prot(unsigned long vm_flags)
94{
Dave Kleikampb845f312008-07-08 00:28:51 +100095 return __pgprot(pgprot_val(protection_map[vm_flags &
96 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
97 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +010098}
99EXPORT_SYMBOL(vm_get_page_prot);
100
Shaohua Li34679d72011-05-24 17:11:18 -0700101int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */
102int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */
Christoph Lameterc3d8c142005-09-06 15:16:33 -0700103int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
Shaohua Li34679d72011-05-24 17:11:18 -0700104/*
105 * Make sure vm_committed_as in one cacheline and not cacheline shared with
106 * other variables. It can be updated by several CPUs frequently.
107 */
108struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/*
111 * Check that a process has enough memory to allocate a new virtual
112 * mapping. 0 means there is enough memory for the allocation to
113 * succeed and -ENOMEM implies there is not.
114 *
115 * We currently support three overcommit policies, which are set via the
116 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
117 *
118 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
119 * Additional code 2002 Jul 20 by Robert Love.
120 *
121 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
122 *
123 * Note this is a helper function intended to be used by LSMs which
124 * wish to use this logic.
125 */
Alan Cox34b4e4a2007-08-22 14:01:28 -0700126int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Roman Gushchind931a992015-02-11 15:28:39 -0800128 long free, allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 vm_acct_memory(pages);
131
132 /*
133 * Sometimes we want to use more memory than we have
134 */
135 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
136 return 0;
137
138 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -0700139 free = global_page_state(NR_FREE_PAGES);
140 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Dmitry Finkc15bef32011-07-25 17:12:19 -0700142 /*
143 * shmem pages shouldn't be counted as free in this
144 * case, they can't be purged, only swapped out, and
145 * that won't affect the overall amount of available
146 * memory in the system.
147 */
148 free -= global_page_state(NR_SHMEM);
149
Shaohua Li1abbca72013-02-22 16:34:38 -0800150 free += get_nr_swap_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /*
153 * Any slabs which are created with the
154 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
155 * which are reclaimable, under pressure. The dentry
156 * cache and most inode caches should fall into this
157 */
Christoph Lameter972d1a72006-09-25 23:31:51 -0700158 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -0700161 * Leave reserved pages. The pages are not for anonymous pages.
162 */
163 if (free <= totalreserve_pages)
164 goto error;
165 else
166 free -= totalreserve_pages;
167
168 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * Leave the last 3% for root
170 */
171 if (!cap_sys_admin)
172 free -= free / 32;
173
174 if (free > pages)
175 return 0;
176
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700177 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
180 allowed = (totalram_pages - hugetlb_total_pages())
181 * sysctl_overcommit_ratio / 100;
182 /*
183 * Leave the last 3% for root
184 */
185 if (!cap_sys_admin)
186 allowed -= allowed / 32;
187 allowed += total_swap_pages;
188
189 /* Don't let a single process grow too big:
190 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -0700191 if (mm)
192 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700194 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700196error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 vm_unacct_memory(pages);
198
199 return -ENOMEM;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700203 * Requires inode->i_mapping->i_mmap_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
205static void __remove_shared_vm_struct(struct vm_area_struct *vma,
206 struct file *file, struct address_space *mapping)
207{
208 if (vma->vm_flags & VM_DENYWRITE)
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800209 atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (vma->vm_flags & VM_SHARED)
David Herrmanna4d6f932014-08-08 14:25:25 -0700211 mapping_unmap_writable(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 flush_dcache_mmap_lock(mapping);
214 if (unlikely(vma->vm_flags & VM_NONLINEAR))
215 list_del_init(&vma->shared.vm_set.list);
216 else
217 vma_prio_tree_remove(vma, &mapping->i_mmap);
218 flush_dcache_mmap_unlock(mapping);
219}
220
221/*
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700222 * Unlink a file-based vm structure from its prio_tree, to hide
223 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700225void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct file *file = vma->vm_file;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (file) {
230 struct address_space *mapping = file->f_mapping;
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700231 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 __remove_shared_vm_struct(vma, file, mapping);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700233 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700235}
236
237/*
238 * Close a vm structure and free it, returning the next.
239 */
240static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
241{
242 struct vm_area_struct *next = vma->vm_next;
243
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700244 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (vma->vm_ops && vma->vm_ops->close)
246 vma->vm_ops->close(vma);
Matt Helsley925d1c42008-04-29 01:01:36 -0700247 if (vma->vm_file) {
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700248 fput(vma->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700249 if (vma->vm_flags & VM_EXECUTABLE)
250 removed_exe_file_vma(vma->vm_mm);
251 }
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700252 mpol_put(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 kmem_cache_free(vm_area_cachep, vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700254 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -0700257static unsigned long do_brk(unsigned long addr, unsigned long len);
258
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100259SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 unsigned long rlim, retval;
262 unsigned long newbrk, oldbrk;
263 struct mm_struct *mm = current->mm;
Hugh Dickinse8a93762017-06-19 04:03:24 -0700264 struct vm_area_struct *next;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700265 unsigned long min_brk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 down_write(&mm->mmap_sem);
268
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700269#ifdef CONFIG_COMPAT_BRK
Jiri Kosina5520e892011-01-13 15:47:23 -0800270 /*
271 * CONFIG_COMPAT_BRK can still be overridden by setting
272 * randomize_va_space to 2, which will still cause mm->start_brk
273 * to be arbitrarily shifted
274 */
Jiri Kosina4471a672011-04-14 15:22:09 -0700275 if (current->brk_randomized)
Jiri Kosina5520e892011-01-13 15:47:23 -0800276 min_brk = mm->start_brk;
277 else
278 min_brk = mm->end_data;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700279#else
280 min_brk = mm->start_brk;
281#endif
282 if (brk < min_brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700284
285 /*
286 * Check against rlimit here. If this check is done later after the test
287 * of oldbrk with newbrk then it can escape the test and let the data
288 * segment grow beyond its set limit the in case where the limit is
289 * not page aligned -Ram Gupta
290 */
Jiri Slaby59e99e52010-03-05 13:41:44 -0800291 rlim = rlimit(RLIMIT_DATA);
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100292 if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
293 (mm->end_data - mm->start_data) > rlim)
Ram Gupta1e624192006-04-10 22:52:57 -0700294 goto out;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 newbrk = PAGE_ALIGN(brk);
297 oldbrk = PAGE_ALIGN(mm->brk);
298 if (oldbrk == newbrk)
299 goto set_brk;
300
301 /* Always allow shrinking brk. */
302 if (brk <= mm->brk) {
303 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
304 goto set_brk;
305 goto out;
306 }
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* Check against existing mmap mappings. */
Hugh Dickinse8a93762017-06-19 04:03:24 -0700309 next = find_vma(mm, oldbrk);
310 if (next && newbrk + PAGE_SIZE > vm_start_gap(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto out;
312
313 /* Ok, looks good - let it rip. */
314 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
315 goto out;
316set_brk:
317 mm->brk = brk;
318out:
319 retval = mm->brk;
320 up_write(&mm->mmap_sem);
321 return retval;
322}
323
324#ifdef DEBUG_MM_RB
325static int browse_rb(struct rb_root *root)
326{
327 int i = 0, j;
328 struct rb_node *nd, *pn = NULL;
329 unsigned long prev = 0, pend = 0;
330
331 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
332 struct vm_area_struct *vma;
333 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
334 if (vma->vm_start < prev)
335 printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
336 if (vma->vm_start < pend)
337 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
338 if (vma->vm_start > vma->vm_end)
339 printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
340 i++;
341 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800342 prev = vma->vm_start;
343 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 j = 0;
346 for (nd = pn; nd; nd = rb_prev(nd)) {
347 j++;
348 }
349 if (i != j)
350 printk("backwards %d, forwards %d\n", j, i), i = 0;
351 return i;
352}
353
354void validate_mm(struct mm_struct *mm)
355{
356 int bug = 0;
357 int i = 0;
358 struct vm_area_struct *tmp = mm->mmap;
359 while (tmp) {
360 tmp = tmp->vm_next;
361 i++;
362 }
363 if (i != mm->map_count)
364 printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
365 i = browse_rb(&mm->mm_rb);
366 if (i != mm->map_count)
367 printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200368 BUG_ON(bug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370#else
371#define validate_mm(mm) do { } while (0)
372#endif
373
374static struct vm_area_struct *
375find_vma_prepare(struct mm_struct *mm, unsigned long addr,
376 struct vm_area_struct **pprev, struct rb_node ***rb_link,
377 struct rb_node ** rb_parent)
378{
379 struct vm_area_struct * vma;
380 struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
381
382 __rb_link = &mm->mm_rb.rb_node;
383 rb_prev = __rb_parent = NULL;
384 vma = NULL;
385
386 while (*__rb_link) {
387 struct vm_area_struct *vma_tmp;
388
389 __rb_parent = *__rb_link;
390 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
391
392 if (vma_tmp->vm_end > addr) {
393 vma = vma_tmp;
394 if (vma_tmp->vm_start <= addr)
Benny Halevydfe195f2008-08-05 13:01:41 -0700395 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 __rb_link = &__rb_parent->rb_left;
397 } else {
398 rb_prev = __rb_parent;
399 __rb_link = &__rb_parent->rb_right;
400 }
401 }
402
403 *pprev = NULL;
404 if (rb_prev)
405 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
406 *rb_link = __rb_link;
407 *rb_parent = __rb_parent;
408 return vma;
409}
410
Cyril Hrubis3e55f212013-04-29 15:08:33 -0700411static unsigned long count_vma_pages_range(struct mm_struct *mm,
412 unsigned long addr, unsigned long end)
413{
414 unsigned long nr_pages = 0;
415 struct vm_area_struct *vma;
416
417 /* Find first overlaping mapping */
418 vma = find_vma_intersection(mm, addr, end);
419 if (!vma)
420 return 0;
421
422 nr_pages = (min(end, vma->vm_end) -
423 max(addr, vma->vm_start)) >> PAGE_SHIFT;
424
425 /* Iterate over the rest of the overlaps */
426 for (vma = vma->vm_next; vma; vma = vma->vm_next) {
427 unsigned long overlap_len;
428
429 if (vma->vm_start > end)
430 break;
431
432 overlap_len = min(end, vma->vm_end) - vma->vm_start;
433 nr_pages += overlap_len >> PAGE_SHIFT;
434 }
435
436 return nr_pages;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
440 struct rb_node **rb_link, struct rb_node *rb_parent)
441{
442 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
443 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
444}
445
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700446static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
ZhenwenXu48aae422009-01-06 14:40:21 -0800448 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 file = vma->vm_file;
451 if (file) {
452 struct address_space *mapping = file->f_mapping;
453
454 if (vma->vm_flags & VM_DENYWRITE)
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800455 atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (vma->vm_flags & VM_SHARED)
David Herrmanna4d6f932014-08-08 14:25:25 -0700457 atomic_inc(&mapping->i_mmap_writable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 flush_dcache_mmap_lock(mapping);
460 if (unlikely(vma->vm_flags & VM_NONLINEAR))
461 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
462 else
463 vma_prio_tree_insert(vma, &mapping->i_mmap);
464 flush_dcache_mmap_unlock(mapping);
465 }
466}
467
468static void
469__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
470 struct vm_area_struct *prev, struct rb_node **rb_link,
471 struct rb_node *rb_parent)
472{
473 __vma_link_list(mm, vma, prev, rb_parent);
474 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
478 struct vm_area_struct *prev, struct rb_node **rb_link,
479 struct rb_node *rb_parent)
480{
481 struct address_space *mapping = NULL;
482
483 if (vma->vm_file)
484 mapping = vma->vm_file->f_mapping;
485
Peter Zijlstra97a89412011-05-24 17:12:04 -0700486 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700487 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 __vma_link(mm, vma, prev, rb_link, rb_parent);
490 __vma_link_file(vma);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700493 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 mm->map_count++;
496 validate_mm(mm);
497}
498
499/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700500 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
501 * mm's list and rbtree. It has already been inserted into the prio_tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800503static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
ZhenwenXu48aae422009-01-06 14:40:21 -0800505 struct vm_area_struct *__vma, *prev;
506 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 __vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200509 BUG_ON(__vma && __vma->vm_start < vma->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 __vma_link(mm, vma, prev, rb_link, rb_parent);
511 mm->map_count++;
512}
513
514static inline void
515__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
516 struct vm_area_struct *prev)
517{
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700518 struct vm_area_struct *next = vma->vm_next;
519
520 prev->vm_next = next;
521 if (next)
522 next->vm_prev = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 rb_erase(&vma->vm_rb, &mm->mm_rb);
524 if (mm->mmap_cache == vma)
525 mm->mmap_cache = prev;
526}
527
528/*
529 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
530 * is already present in an i_mmap tree without adjusting the tree.
531 * The following helper function should be used when such adjustments
532 * are necessary. The "insert" vma (if any) is to be inserted
533 * before we drop the necessary locks.
534 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800535int vma_adjust(struct vm_area_struct *vma, unsigned long start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
537{
538 struct mm_struct *mm = vma->vm_mm;
539 struct vm_area_struct *next = vma->vm_next;
540 struct vm_area_struct *importer = NULL;
541 struct address_space *mapping = NULL;
542 struct prio_tree_root *root = NULL;
Rik van Riel012f1802010-08-09 17:18:40 -0700543 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct file *file = vma->vm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 long adjust_next = 0;
546 int remove_next = 0;
547
548 if (next && !insert) {
Linus Torvalds287d97a2010-04-10 15:22:30 -0700549 struct vm_area_struct *exporter = NULL;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (end >= next->vm_end) {
552 /*
553 * vma expands, overlapping all the next, and
554 * perhaps the one after too (mprotect case 6).
555 */
556again: remove_next = 1 + (end > next->vm_end);
557 end = next->vm_end;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700558 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 importer = vma;
560 } else if (end > next->vm_start) {
561 /*
562 * vma expands, overlapping part of the next:
563 * mprotect case 5 shifting the boundary up.
564 */
565 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700566 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 importer = vma;
568 } else if (end < vma->vm_end) {
569 /*
570 * vma shrinks, and !insert tells it's not
571 * split_vma inserting another: so it must be
572 * mprotect case 4 shifting the boundary down.
573 */
574 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700575 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 importer = next;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Rik van Riel5beb4932010-03-05 13:42:07 -0800579 /*
580 * Easily overlooked: when mprotect shifts the boundary,
581 * make sure the expanding vma has anon_vma set if the
582 * shrinking vma had, to cover any anon pages imported.
583 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700584 if (exporter && exporter->anon_vma && !importer->anon_vma) {
Konstantin Khlebnikovae3e8222015-01-11 16:54:06 +0300585 int error;
586
Linus Torvalds287d97a2010-04-10 15:22:30 -0700587 importer->anon_vma = exporter->anon_vma;
Konstantin Khlebnikovae3e8222015-01-11 16:54:06 +0300588 error = anon_vma_clone(importer, exporter);
Leon Yu7ac06e32015-03-25 15:55:11 -0700589 if (error)
Konstantin Khlebnikovae3e8222015-01-11 16:54:06 +0300590 return error;
Rik van Riel5beb4932010-03-05 13:42:07 -0800591 }
592 }
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (file) {
595 mapping = file->f_mapping;
596 if (!(vma->vm_flags & VM_NONLINEAR))
597 root = &mapping->i_mmap;
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700598 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
601 * Put into prio_tree now, so instantiated pages
602 * are visible to arm/parisc __flush_dcache_page
603 * throughout; but we cannot insert into address
604 * space until vma start or end is updated.
605 */
606 __vma_link_file(insert);
607 }
608 }
609
Andrea Arcangeli94fcc582011-01-13 15:47:08 -0800610 vma_adjust_trans_huge(vma, start, end, adjust_next);
611
Rik van Riel012f1802010-08-09 17:18:40 -0700612 /*
613 * When changing only vma->vm_end, we don't really need anon_vma
614 * lock. This is a fairly rare case by itself, but the anon_vma
615 * lock may be shared between many sibling processes. Skipping
616 * the lock for brk adjustments makes a difference sometimes.
617 */
Shaohua Li5f70b962011-05-24 17:11:19 -0700618 if (vma->anon_vma && (importer || start != vma->vm_start)) {
Rik van Riel012f1802010-08-09 17:18:40 -0700619 anon_vma = vma->anon_vma;
620 anon_vma_lock(anon_vma);
621 }
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (root) {
624 flush_dcache_mmap_lock(mapping);
625 vma_prio_tree_remove(vma, root);
626 if (adjust_next)
627 vma_prio_tree_remove(next, root);
628 }
629
630 vma->vm_start = start;
631 vma->vm_end = end;
632 vma->vm_pgoff = pgoff;
633 if (adjust_next) {
634 next->vm_start += adjust_next << PAGE_SHIFT;
635 next->vm_pgoff += adjust_next;
636 }
637
638 if (root) {
639 if (adjust_next)
640 vma_prio_tree_insert(next, root);
641 vma_prio_tree_insert(vma, root);
642 flush_dcache_mmap_unlock(mapping);
643 }
644
645 if (remove_next) {
646 /*
647 * vma_merge has merged next into vma, and needs
648 * us to remove next before dropping the locks.
649 */
650 __vma_unlink(mm, next, vma);
651 if (file)
652 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 } else if (insert) {
654 /*
655 * split_vma has split insert from vma, and needs
656 * us to insert it before dropping the locks
657 * (it may either follow vma or precede it).
658 */
659 __insert_vm_struct(mm, insert);
660 }
661
Rik van Riel012f1802010-08-09 17:18:40 -0700662 if (anon_vma)
663 anon_vma_unlock(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700665 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (remove_next) {
Matt Helsley925d1c42008-04-29 01:01:36 -0700668 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 fput(file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700670 if (next->vm_flags & VM_EXECUTABLE)
671 removed_exe_file_vma(mm);
672 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800673 if (next->anon_vma)
674 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 mm->map_count--;
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700676 mpol_put(vma_policy(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 kmem_cache_free(vm_area_cachep, next);
678 /*
679 * In mprotect's case 6 (see comments on vma_merge),
680 * we must remove another next too. It would clutter
681 * up the code too much to do both in one go.
682 */
683 if (remove_next == 2) {
684 next = vma->vm_next;
685 goto again;
686 }
687 }
688
689 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -0800690
691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694/*
695 * If the vma has a ->close operation then the driver probably needs to release
696 * per-vma resources, so we don't attempt to merge those.
697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static inline int is_mergeable_vma(struct vm_area_struct *vma,
Colin Crossa9e6b182013-06-26 17:26:01 -0700699 struct file *file, unsigned long vm_flags,
700 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Hugh Dickins8314c4f2009-09-21 17:02:25 -0700702 /* VM_CAN_NONLINEAR may get set later by f_op->mmap() */
703 if ((vma->vm_flags ^ vm_flags) & ~VM_CAN_NONLINEAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705 if (vma->vm_file != file)
706 return 0;
707 if (vma->vm_ops && vma->vm_ops->close)
708 return 0;
Colin Crossa9e6b182013-06-26 17:26:01 -0700709 if (vma_get_anon_name(vma) != anon_name)
710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return 1;
712}
713
714static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -0700715 struct anon_vma *anon_vma2,
716 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Shaohua Li965f55d2011-05-24 17:11:20 -0700718 /*
719 * The list_is_singular() test is to avoid merging VMA cloned from
720 * parents. This can improve scalability caused by anon_vma lock.
721 */
722 if ((!anon_vma1 || !anon_vma2) && (!vma ||
723 list_is_singular(&vma->anon_vma_chain)))
724 return 1;
725 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/*
729 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
730 * in front of (at a lower virtual address and file offset than) the vma.
731 *
732 * We cannot merge two vmas if they have differently assigned (non-NULL)
733 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
734 *
735 * We don't check here for the merged mmap wrapping around the end of pagecache
736 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
737 * wrap, nor mmaps which cover the final page at index -1UL.
738 */
739static int
740can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
Colin Crossa9e6b182013-06-26 17:26:01 -0700741 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff,
742 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Colin Crossa9e6b182013-06-26 17:26:01 -0700744 if (is_mergeable_vma(vma, file, vm_flags, anon_name) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700745 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (vma->vm_pgoff == vm_pgoff)
747 return 1;
748 }
749 return 0;
750}
751
752/*
753 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
754 * beyond (at a higher virtual address and file offset than) the vma.
755 *
756 * We cannot merge two vmas if they have differently assigned (non-NULL)
757 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
758 */
759static int
760can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
Colin Crossa9e6b182013-06-26 17:26:01 -0700761 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff,
762 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Colin Crossa9e6b182013-06-26 17:26:01 -0700764 if (is_mergeable_vma(vma, file, vm_flags, anon_name) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700765 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 pgoff_t vm_pglen;
767 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
768 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
769 return 1;
770 }
771 return 0;
772}
773
774/*
Colin Crossa9e6b182013-06-26 17:26:01 -0700775 * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name),
776 * figure out whether that can be merged with its predecessor or its
777 * successor. Or both (it neatly fills a hole).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 *
779 * In most cases - when called for mmap, brk or mremap - [addr,end) is
780 * certain not to be mapped by the time vma_merge is called; but when
781 * called for mprotect, it is certain to be already mapped (either at
782 * an offset within prev, or at the start of next), and the flags of
783 * this area are about to be changed to vm_flags - and the no-change
784 * case has already been eliminated.
785 *
786 * The following mprotect cases have to be considered, where AAAA is
787 * the area passed down from mprotect_fixup, never extending beyond one
788 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
789 *
790 * AAAA AAAA AAAA AAAA
791 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
792 * cannot merge might become might become might become
793 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
794 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
795 * mremap move: PPPPNNNNNNNN 8
796 * AAAA
797 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
798 * might become case 1 below case 2 below case 3 below
799 *
800 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
801 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
802 */
803struct vm_area_struct *vma_merge(struct mm_struct *mm,
804 struct vm_area_struct *prev, unsigned long addr,
805 unsigned long end, unsigned long vm_flags,
806 struct anon_vma *anon_vma, struct file *file,
Colin Crossa9e6b182013-06-26 17:26:01 -0700807 pgoff_t pgoff, struct mempolicy *policy,
808 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
811 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800812 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /*
815 * We later require that vma->vm_flags == vm_flags,
816 * so this tests vma->vm_flags & VM_SPECIAL, too.
817 */
818 if (vm_flags & VM_SPECIAL)
819 return NULL;
820
821 if (prev)
822 next = prev->vm_next;
823 else
824 next = mm->mmap;
825 area = next;
826 if (next && next->vm_end == end) /* cases 6, 7, 8 */
827 next = next->vm_next;
828
829 /*
830 * Can it merge with the predecessor?
831 */
832 if (prev && prev->vm_end == addr &&
833 mpol_equal(vma_policy(prev), policy) &&
Colin Crossa9e6b182013-06-26 17:26:01 -0700834 can_vma_merge_after(prev, vm_flags, anon_vma,
835 file, pgoff, anon_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /*
837 * OK, it can. Can we now merge in the successor as well?
838 */
839 if (next && end == next->vm_start &&
840 mpol_equal(policy, vma_policy(next)) &&
Colin Crossa9e6b182013-06-26 17:26:01 -0700841 can_vma_merge_before(next, vm_flags, anon_vma,
842 file, pgoff+pglen, anon_name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -0700844 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* cases 1, 6 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800846 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 next->vm_end, prev->vm_pgoff, NULL);
848 } else /* cases 2, 5, 7 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800849 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 end, prev->vm_pgoff, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -0800851 if (err)
852 return NULL;
David Rientjes8c948ea2014-10-29 14:50:31 -0700853 khugepaged_enter_vma_merge(prev, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return prev;
855 }
856
857 /*
858 * Can this new request be merged in front of next?
859 */
860 if (next && end == next->vm_start &&
861 mpol_equal(policy, vma_policy(next)) &&
Colin Crossa9e6b182013-06-26 17:26:01 -0700862 can_vma_merge_before(next, vm_flags, anon_vma,
863 file, pgoff+pglen, anon_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (prev && addr < prev->vm_end) /* case 4 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800865 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 addr, prev->vm_pgoff, NULL);
867 else /* cases 3, 8 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800868 err = vma_adjust(area, addr, next->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 next->vm_pgoff - pglen, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -0800870 if (err)
871 return NULL;
David Rientjes8c948ea2014-10-29 14:50:31 -0700872 khugepaged_enter_vma_merge(area, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return area;
874 }
875
876 return NULL;
877}
878
879/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -0700880 * Rough compatbility check to quickly see if it's even worth looking
881 * at sharing an anon_vma.
882 *
883 * They need to have the same vm_file, and the flags can only differ
884 * in things that mprotect may change.
885 *
886 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
887 * we can merge the two vma's. For example, we refuse to merge a vma if
888 * there is a vm_ops->close() function, because that indicates that the
889 * driver is doing some kind of reference counting. But that doesn't
890 * really matter for the anon_vma sharing case.
891 */
892static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
893{
894 return a->vm_end == b->vm_start &&
895 mpol_equal(vma_policy(a), vma_policy(b)) &&
896 a->vm_file == b->vm_file &&
897 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) &&
898 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
899}
900
901/*
902 * Do some basic sanity checking to see if we can re-use the anon_vma
903 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
904 * the same as 'old', the other will be the new one that is trying
905 * to share the anon_vma.
906 *
907 * NOTE! This runs with mm_sem held for reading, so it is possible that
908 * the anon_vma of 'old' is concurrently in the process of being set up
909 * by another page fault trying to merge _that_. But that's ok: if it
910 * is being set up, that automatically means that it will be a singleton
911 * acceptable for merging, so we can do all of this optimistically. But
912 * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
913 *
914 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
915 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
916 * is to return an anon_vma that is "complex" due to having gone through
917 * a fork).
918 *
919 * We also make sure that the two vma's are compatible (adjacent,
920 * and with the same memory policies). That's all stable, even with just
921 * a read lock on the mm_sem.
922 */
923static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
924{
925 if (anon_vma_compatible(a, b)) {
926 struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
927
928 if (anon_vma && list_is_singular(&old->anon_vma_chain))
929 return anon_vma;
930 }
931 return NULL;
932}
933
934/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
936 * neighbouring vmas for a suitable anon_vma, before it goes off
937 * to allocate a new anon_vma. It checks because a repetitive
938 * sequence of mprotects and faults may otherwise lead to distinct
939 * anon_vmas being allocated, preventing vma merge in subsequent
940 * mprotect.
941 */
942struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
943{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -0700944 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 near = vma->vm_next;
948 if (!near)
949 goto try_prev;
950
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -0700951 anon_vma = reusable_anon_vma(near, vma, near);
952 if (anon_vma)
953 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -0700955 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (!near)
957 goto none;
958
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -0700959 anon_vma = reusable_anon_vma(near, near, vma);
960 if (anon_vma)
961 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962none:
963 /*
964 * There's no absolute need to look only at touching neighbours:
965 * we could search further afield for "compatible" anon_vmas.
966 * But it would probably just be a waste of time searching,
967 * or lead to too many vmas hanging off the same anon_vma.
968 * We're trying to allow mprotect remerging later on,
969 * not trying to minimize memory used for anon_vmas.
970 */
971 return NULL;
972}
973
974#ifdef CONFIG_PROC_FS
Hugh Dickinsab50b8e2005-10-29 18:15:56 -0700975void vm_stat_account(struct mm_struct *mm, unsigned long flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct file *file, long pages)
977{
978 const unsigned long stack_flags
979 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (file) {
982 mm->shared_vm += pages;
983 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
984 mm->exec_vm += pages;
985 } else if (flags & stack_flags)
986 mm->stack_vm += pages;
987 if (flags & (VM_RESERVED|VM_IO))
988 mm->reserved_vm += pages;
989}
990#endif /* CONFIG_PROC_FS */
991
992/*
Al Viro40401532012-02-13 03:58:52 +0000993 * If a hint addr is less than mmap_min_addr change hint to be as
994 * low as possible but still greater than mmap_min_addr
995 */
996static inline unsigned long round_hint_to_min(unsigned long hint)
997{
998 hint &= PAGE_MASK;
999 if (((void *)hint != NULL) &&
1000 (hint < mmap_min_addr))
1001 return PAGE_ALIGN(mmap_min_addr);
1002 return hint;
1003}
1004
1005/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001006 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
1008
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001009static unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned long len, unsigned long prot,
1011 unsigned long flags, unsigned long pgoff)
1012{
1013 struct mm_struct * mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct inode *inode;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001015 vm_flags_t vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int error;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001017 unsigned long reqprot = prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
1020 * Does the application expect PROT_READ to imply PROT_EXEC?
1021 *
1022 * (the exception is when the underlying filesystem is noexec
1023 * mounted, in which case we dont add PROT_EXEC.)
1024 */
1025 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001026 if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 prot |= PROT_EXEC;
1028
1029 if (!len)
1030 return -EINVAL;
1031
Eric Paris7cd94142007-11-26 18:47:40 -05001032 if (!(flags & MAP_FIXED))
1033 addr = round_hint_to_min(addr);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* Careful about overflows.. */
1036 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001037 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -ENOMEM;
1039
1040 /* offset overflow? */
1041 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
1042 return -EOVERFLOW;
1043
1044 /* Too many mappings? */
1045 if (mm->map_count > sysctl_max_map_count)
1046 return -ENOMEM;
1047
1048 /* Obtain the address to map to. we verify (or select) it and ensure
1049 * that it represents a valid section of the address space.
1050 */
1051 addr = get_unmapped_area(file, addr, len, pgoff, flags);
1052 if (addr & ~PAGE_MASK)
1053 return addr;
1054
1055 /* Do simple checking here so the lower-level routines won't have
1056 * to. we assume access permissions have been handled by the open
1057 * of the memory object, so we don't do any here.
1058 */
1059 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
1060 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1061
Huang Shijiecdf7b342009-09-21 17:03:36 -07001062 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!can_do_mlock())
1064 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* mlock MCL_FUTURE? */
1067 if (vm_flags & VM_LOCKED) {
1068 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07001069 locked = len >> PAGE_SHIFT;
1070 locked += mm->locked_vm;
Jiri Slaby59e99e52010-03-05 13:41:44 -08001071 lock_limit = rlimit(RLIMIT_MEMLOCK);
Chris Wright93ea1d02005-05-01 08:58:38 -07001072 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1074 return -EAGAIN;
1075 }
1076
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001077 inode = file ? file->f_path.dentry->d_inode : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (file) {
1080 switch (flags & MAP_TYPE) {
1081 case MAP_SHARED:
1082 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1083 return -EACCES;
1084
1085 /*
1086 * Make sure we don't allow writing to an append-only
1087 * file..
1088 */
1089 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1090 return -EACCES;
1091
1092 /*
1093 * Make sure there are no mandatory locks on the file.
1094 */
1095 if (locks_verify_locked(inode))
1096 return -EAGAIN;
1097
1098 vm_flags |= VM_SHARED | VM_MAYSHARE;
1099 if (!(file->f_mode & FMODE_WRITE))
1100 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1101
1102 /* fall through */
1103 case MAP_PRIVATE:
1104 if (!(file->f_mode & FMODE_READ))
1105 return -EACCES;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001106 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001107 if (vm_flags & VM_EXEC)
1108 return -EPERM;
1109 vm_flags &= ~VM_MAYEXEC;
1110 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001111
1112 if (!file->f_op || !file->f_op->mmap)
1113 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
1115
1116 default:
1117 return -EINVAL;
1118 }
1119 } else {
1120 switch (flags & MAP_TYPE) {
1121 case MAP_SHARED:
Tejun Heoce363942008-09-03 16:09:47 +02001122 /*
1123 * Ignore pgoff.
1124 */
1125 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 vm_flags |= VM_SHARED | VM_MAYSHARE;
1127 break;
1128 case MAP_PRIVATE:
1129 /*
1130 * Set pgoff according to addr for anon_vma.
1131 */
1132 pgoff = addr >> PAGE_SHIFT;
1133 break;
1134 default:
1135 return -EINVAL;
1136 }
1137 }
1138
Eric Parised032182007-06-28 15:55:21 -04001139 error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (error)
1141 return error;
Eric Parised032182007-06-28 15:55:21 -04001142
Mel Gorman5a6fe122009-02-10 14:02:27 +00001143 return mmap_region(file, addr, len, flags, vm_flags, pgoff);
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001144}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001145
1146unsigned long do_mmap(struct file *file, unsigned long addr,
1147 unsigned long len, unsigned long prot,
1148 unsigned long flag, unsigned long offset)
1149{
1150 if (unlikely(offset + PAGE_ALIGN(len) < offset))
1151 return -EINVAL;
1152 if (unlikely(offset & ~PAGE_MASK))
1153 return -EINVAL;
1154 return do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
1155}
1156EXPORT_SYMBOL(do_mmap);
1157
1158unsigned long vm_mmap(struct file *file, unsigned long addr,
1159 unsigned long len, unsigned long prot,
1160 unsigned long flag, unsigned long offset)
1161{
1162 unsigned long ret;
1163 struct mm_struct *mm = current->mm;
1164
1165 down_write(&mm->mmap_sem);
1166 ret = do_mmap(file, addr, len, prot, flag, offset);
1167 up_write(&mm->mmap_sem);
1168 return ret;
1169}
1170EXPORT_SYMBOL(vm_mmap);
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001171
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001172SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1173 unsigned long, prot, unsigned long, flags,
1174 unsigned long, fd, unsigned long, pgoff)
1175{
1176 struct file *file = NULL;
1177 unsigned long retval = -EBADF;
1178
1179 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001180 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001181 if (unlikely(flags & MAP_HUGETLB))
1182 return -EINVAL;
1183 file = fget(fd);
1184 if (!file)
1185 goto out;
Naoya Horiguchif8597ae2013-05-07 16:18:13 -07001186 if (is_file_hugepages(file))
1187 len = ALIGN(len, huge_page_size(hstate_file(file)));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001188 } else if (flags & MAP_HUGETLB) {
1189 struct user_struct *user = NULL;
Naoya Horiguchif8597ae2013-05-07 16:18:13 -07001190
1191 len = ALIGN(len, huge_page_size(&default_hstate));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001192 /*
1193 * VM_NORESERVE is used because the reservations will be
1194 * taken when vm_ops->mmap() is called
1195 * A dummy user value is used because we are not locking
1196 * memory so no accounting is necessary
1197 */
Naoya Horiguchif8597ae2013-05-07 16:18:13 -07001198 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
Steven Truelove40716e22012-03-21 16:34:14 -07001199 VM_NORESERVE, &user,
1200 HUGETLB_ANONHUGE_INODE);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001201 if (IS_ERR(file))
1202 return PTR_ERR(file);
1203 }
1204
1205 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1206
1207 down_write(&current->mm->mmap_sem);
1208 retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1209 up_write(&current->mm->mmap_sem);
1210
1211 if (file)
1212 fput(file);
1213out:
1214 return retval;
1215}
1216
Christoph Hellwiga4679372010-03-10 15:21:15 -08001217#ifdef __ARCH_WANT_SYS_OLD_MMAP
1218struct mmap_arg_struct {
1219 unsigned long addr;
1220 unsigned long len;
1221 unsigned long prot;
1222 unsigned long flags;
1223 unsigned long fd;
1224 unsigned long offset;
1225};
1226
1227SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1228{
1229 struct mmap_arg_struct a;
1230
1231 if (copy_from_user(&a, arg, sizeof(a)))
1232 return -EFAULT;
1233 if (a.offset & ~PAGE_MASK)
1234 return -EINVAL;
1235
1236 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1237 a.offset >> PAGE_SHIFT);
1238}
1239#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1240
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001241/*
1242 * Some shared mappigns will want the pages marked read-only
1243 * to track write events. If so, we'll downgrade vm_page_prot
1244 * to the private version (using protection_map[] without the
1245 * VM_SHARED bit).
1246 */
1247int vma_wants_writenotify(struct vm_area_struct *vma)
1248{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001249 vm_flags_t vm_flags = vma->vm_flags;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001250
1251 /* If it was private or non-writable, the write bit is already clear */
1252 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1253 return 0;
1254
1255 /* The backer wishes to know when pages are first written to? */
1256 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1257 return 1;
1258
1259 /* The open routine did something to the protections already? */
1260 if (pgprot_val(vma->vm_page_prot) !=
Coly Li3ed75eb2007-10-18 23:39:15 -07001261 pgprot_val(vm_get_page_prot(vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001262 return 0;
1263
1264 /* Specialty mapping? */
1265 if (vm_flags & (VM_PFNMAP|VM_INSERTPAGE))
1266 return 0;
1267
1268 /* Can the mapping track the dirty pages? */
1269 return vma->vm_file && vma->vm_file->f_mapping &&
1270 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1271}
1272
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001273/*
1274 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001275 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001276 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001277static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001278{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001279 /*
1280 * hugetlb has its own accounting separate from the core VM
1281 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1282 */
1283 if (file && is_file_hugepages(file))
1284 return 0;
1285
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001286 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1287}
1288
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001289unsigned long mmap_region(struct file *file, unsigned long addr,
1290 unsigned long len, unsigned long flags,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001291 vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001292{
1293 struct mm_struct *mm = current->mm;
1294 struct vm_area_struct *vma, *prev;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001295 int error;
1296 struct rb_node **rb_link, *rb_parent;
1297 unsigned long charged = 0;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001298
Cyril Hrubis3e55f212013-04-29 15:08:33 -07001299 /* Check against address space limit. */
1300 if (!may_expand_vm(mm, len >> PAGE_SHIFT)) {
1301 unsigned long nr_pages;
1302
1303 /*
1304 * MAP_FIXED may remove pages of mappings that intersects with
1305 * requested mapping. Account for the pages it would unmap.
1306 */
1307 if (!(vm_flags & MAP_FIXED))
1308 return -ENOMEM;
1309
1310 nr_pages = count_vma_pages_range(mm, addr, addr + len);
1311
1312 if (!may_expand_vm(mm, (len >> PAGE_SHIFT) - nr_pages))
1313 return -ENOMEM;
1314 }
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* Clear old maps */
1317 error = -ENOMEM;
1318munmap_back:
1319 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1320 if (vma && vma->vm_start < addr + len) {
1321 if (do_munmap(mm, addr, len))
1322 return -ENOMEM;
1323 goto munmap_back;
1324 }
1325
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001326 /*
1327 * Set 'VM_NORESERVE' if we should not account for the
Mel Gorman5a6fe122009-02-10 14:02:27 +00001328 * memory use of this mapping.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001329 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001330 if ((flags & MAP_NORESERVE)) {
1331 /* We honor MAP_NORESERVE if allowed to overcommit */
1332 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1333 vm_flags |= VM_NORESERVE;
1334
1335 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1336 if (file && is_file_hugepages(file))
1337 vm_flags |= VM_NORESERVE;
1338 }
Andy Whitcroftcdfd4322008-07-23 21:27:28 -07001339
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001340 /*
1341 * Private writable mapping: check memory availability
1342 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001343 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001344 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001345 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001346 return -ENOMEM;
1347 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
1350 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001351 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 */
Colin Crossa9e6b182013-06-26 17:26:01 -07001353 vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff,
1354 NULL, NULL);
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001355 if (vma)
1356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /*
1359 * Determine the object being mapped and call the appropriate
1360 * specific mapper. the address has already been validated, but
1361 * not unmapped, but the maps are removed from the list.
1362 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001363 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (!vma) {
1365 error = -ENOMEM;
1366 goto unacct_error;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 vma->vm_mm = mm;
1370 vma->vm_start = addr;
1371 vma->vm_end = addr + len;
1372 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001373 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 vma->vm_pgoff = pgoff;
Rik van Riel5beb4932010-03-05 13:42:07 -08001375 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Hugh Dickinsce8fea72012-03-06 12:28:52 -08001377 error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1381 goto free_vma;
1382 if (vm_flags & VM_DENYWRITE) {
1383 error = deny_write_access(file);
1384 if (error)
1385 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
David Herrmanna4d6f932014-08-08 14:25:25 -07001387 if (vm_flags & VM_SHARED) {
1388 error = mapping_map_writable(file->f_mapping);
1389 if (error)
1390 goto allow_write_and_free_vma;
1391 }
1392
1393 /* ->mmap() can change vma->vm_file, but must guarantee that
1394 * vma_link() below can deny write-access if VM_DENYWRITE is set
1395 * and map writably if VM_SHARED is set. This usually means the
1396 * new file must not have been exposed to user-space, yet.
1397 */
Al Virob8d12952012-08-27 14:48:26 -04001398 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 error = file->f_op->mmap(file, vma);
1400 if (error)
1401 goto unmap_and_free_vma;
Matt Helsley925d1c42008-04-29 01:01:36 -07001402 if (vm_flags & VM_EXECUTABLE)
1403 added_exe_file_vma(mm);
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001404
1405 /* Can addr have changed??
1406 *
1407 * Answer: Yes, several device drivers can do it in their
1408 * f_op->mmap method. -DaveM
1409 */
1410 addr = vma->vm_start;
1411 pgoff = vma->vm_pgoff;
1412 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 } else if (vm_flags & VM_SHARED) {
Al Viro835ee792012-03-05 06:39:47 +00001414 if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
1415 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 error = shmem_zero_setup(vma);
1417 if (error)
1418 goto free_vma;
1419 }
1420
Magnus Dammc9d0bf22009-12-14 17:59:49 -08001421 if (vma_wants_writenotify(vma)) {
1422 pgprot_t pprot = vma->vm_page_prot;
1423
1424 /* Can vma->vm_page_prot have changed??
1425 *
1426 * Answer: Yes, drivers may have changed it in their
1427 * f_op->mmap method.
1428 *
1429 * Ensures that vmas marked as uncached stay that way.
1430 */
Hugh Dickins1ddd4392007-10-22 20:45:12 -07001431 vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
Magnus Dammc9d0bf22009-12-14 17:59:49 -08001432 if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
1433 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1434 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -07001435
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001436 vma_link(mm, vma, prev, rb_link, rb_parent);
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001437 /* Once vma denies write, undo our temporary denial count */
David Herrmanna4d6f932014-08-08 14:25:25 -07001438 if (file) {
1439 if (vm_flags & VM_SHARED)
1440 mapping_unmap_writable(file->f_mapping);
1441 if (vm_flags & VM_DENYWRITE)
1442 allow_write_access(file);
1443 }
Oleg Nesterov18a87532013-09-11 14:20:20 -07001444 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001445out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001446 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 mm->total_vm += len >> PAGE_SHIFT;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001449 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (vm_flags & VM_LOCKED) {
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001451 if (!mlock_vma_pages_range(vma, addr, addr + len))
1452 mm->locked_vm += (len >> PAGE_SHIFT);
Rik van Rielba470de2008-10-18 20:26:50 -07001453 } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
Nick Piggin54cb8822007-07-19 01:46:59 -07001454 make_pages_present(addr, addr + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return addr;
1456
1457unmap_and_free_vma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 vma->vm_file = NULL;
1459 fput(file);
1460
1461 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001462 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1463 charged = 0;
David Herrmanna4d6f932014-08-08 14:25:25 -07001464 if (vm_flags & VM_SHARED)
1465 mapping_unmap_writable(file->f_mapping);
1466allow_write_and_free_vma:
1467 if (vm_flags & VM_DENYWRITE)
1468 allow_write_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469free_vma:
1470 kmem_cache_free(vm_area_cachep, vma);
1471unacct_error:
1472 if (charged)
1473 vm_unacct_memory(charged);
1474 return error;
1475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477/* Get an address range which is currently unmapped.
1478 * For shmat() with addr=0.
1479 *
1480 * Ugly calling convention alert:
1481 * Return value with the low bits set means error value,
1482 * ie
1483 * if (ret & ~PAGE_MASK)
1484 * error = ret;
1485 *
1486 * This function "knows" that -ENOMEM has the bits set.
1487 */
1488#ifndef HAVE_ARCH_UNMAPPED_AREA
1489unsigned long
1490arch_get_unmapped_area(struct file *filp, unsigned long addr,
1491 unsigned long len, unsigned long pgoff, unsigned long flags)
1492{
1493 struct mm_struct *mm = current->mm;
Hugh Dickinse8a93762017-06-19 04:03:24 -07001494 struct vm_area_struct *vma, *prev;
1495 unsigned long start_addr, vm_start, prev_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if (len > TASK_SIZE)
1498 return -ENOMEM;
1499
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001500 if (flags & MAP_FIXED)
1501 return addr;
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (addr) {
1504 addr = PAGE_ALIGN(addr);
Hugh Dickinse8a93762017-06-19 04:03:24 -07001505 vma = find_vma_prev(mm, addr, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (TASK_SIZE - len >= addr &&
Hugh Dickinse8a93762017-06-19 04:03:24 -07001507 (!vma || addr + len <= vm_start_gap(vma)) &&
1508 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return addr;
1510 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001511 if (len > mm->cached_hole_size) {
1512 start_addr = addr = mm->free_area_cache;
1513 } else {
1514 start_addr = addr = TASK_UNMAPPED_BASE;
1515 mm->cached_hole_size = 0;
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518full_search:
Hugh Dickinse8a93762017-06-19 04:03:24 -07001519 for (vma = find_vma_prev(mm, addr, &prev); ; prev = vma,
1520 vma = vma->vm_next) {
1521 if (prev) {
1522 prev_end = vm_end_gap(prev);
1523 if (addr < prev_end) {
1524 addr = prev_end;
1525 /* If vma already violates gap, forget it */
1526 if (vma && addr > vma->vm_start)
1527 addr = vma->vm_start;
1528 }
1529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* At this point: (!vma || addr < vma->vm_end). */
1531 if (TASK_SIZE - len < addr) {
1532 /*
1533 * Start a new search - just in case we missed
1534 * some holes.
1535 */
1536 if (start_addr != TASK_UNMAPPED_BASE) {
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001537 addr = TASK_UNMAPPED_BASE;
1538 start_addr = addr;
1539 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 goto full_search;
1541 }
1542 return -ENOMEM;
1543 }
Hugh Dickinse8a93762017-06-19 04:03:24 -07001544 vm_start = vma ? vm_start_gap(vma) : TASK_SIZE;
1545 if (addr + len <= vm_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /*
1547 * Remember the place where we stopped the search:
1548 */
1549 mm->free_area_cache = addr + len;
1550 return addr;
1551 }
Hugh Dickinse8a93762017-06-19 04:03:24 -07001552 if (addr + mm->cached_hole_size < vm_start)
1553 mm->cached_hole_size = vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555}
1556#endif
1557
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001558void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 /*
1561 * Is this a new hole at the lowest possible address?
1562 */
Xiao Guangrongf44d2192012-03-21 16:33:56 -07001563 if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001564 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
1568 * This mmap-allocator allocates new areas top-down from below the
1569 * stack's low limit (the base):
1570 */
1571#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1572unsigned long
1573arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1574 const unsigned long len, const unsigned long pgoff,
1575 const unsigned long flags)
1576{
Hugh Dickinse8a93762017-06-19 04:03:24 -07001577 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 struct mm_struct *mm = current->mm;
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001579 unsigned long addr = addr0, start_addr;
Hugh Dickinse8a93762017-06-19 04:03:24 -07001580 unsigned long vm_start, prev_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /* requested length too big for entire address space */
1583 if (len > TASK_SIZE)
1584 return -ENOMEM;
1585
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001586 if (flags & MAP_FIXED)
1587 return addr;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* requesting a specific address */
1590 if (addr) {
1591 addr = PAGE_ALIGN(addr);
Hugh Dickinse8a93762017-06-19 04:03:24 -07001592 vma = find_vma_prev(mm, addr, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (TASK_SIZE - len >= addr &&
Hugh Dickinse8a93762017-06-19 04:03:24 -07001594 (!vma || addr + len <= vm_start_gap(vma)) &&
1595 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return addr;
1597 }
1598
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001599 /* check if free_area_cache is useful for us */
1600 if (len <= mm->cached_hole_size) {
1601 mm->cached_hole_size = 0;
1602 mm->free_area_cache = mm->mmap_base;
1603 }
1604
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001605try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /* either no address requested or can't fit in requested address hole */
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001607 start_addr = addr = mm->free_area_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001609 if (addr < len)
1610 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001612 addr -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 do {
1614 /*
1615 * Lookup failure means no vma is above this address,
1616 * else if new region fits below vma->vm_start,
1617 * return with success:
1618 */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001619 vma = find_vma_prev(mm, addr, &prev);
1620 vm_start = vma ? vm_start_gap(vma) : mm->mmap_base;
1621 prev_end = vm_end_gap(prev);
1622
1623 if (addr + len <= vm_start && addr >= prev_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* remember the address as a hint for next time */
1625 return (mm->free_area_cache = addr);
1626
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001627 /* remember the largest hole we saw so far */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001628 if (addr + mm->cached_hole_size < vm_start)
1629 mm->cached_hole_size = vm_start - addr;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /* try just below the current vma->vm_start */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001632 addr = vm_start - len;
1633 } while (len < vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001635fail:
1636 /*
1637 * if hint left us with no space for the requested
1638 * mapping then try again:
1639 *
1640 * Note: this is different with the case of bottomup
1641 * which does the fully line-search, but we use find_vma
1642 * here that causes some holes skipped.
1643 */
1644 if (start_addr != mm->mmap_base) {
1645 mm->free_area_cache = mm->mmap_base;
1646 mm->cached_hole_size = 0;
1647 goto try_again;
1648 }
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 /*
1651 * A failed mmap() very likely causes application failure,
1652 * so fall back to the bottom-up function here. This scenario
1653 * can happen with large stack limits and large mmap()
1654 * allocations.
1655 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001656 mm->cached_hole_size = ~0UL;
1657 mm->free_area_cache = TASK_UNMAPPED_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1659 /*
1660 * Restore the topdown base:
1661 */
1662 mm->free_area_cache = mm->mmap_base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001663 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 return addr;
1666}
1667#endif
1668
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001669void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 /*
1672 * Is this a new hole at the highest possible address?
1673 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001674 if (addr > mm->free_area_cache)
1675 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* dont allow allocations above current base */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001678 if (mm->free_area_cache > mm->mmap_base)
1679 mm->free_area_cache = mm->mmap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682unsigned long
1683get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1684 unsigned long pgoff, unsigned long flags)
1685{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001686 unsigned long (*get_area)(struct file *, unsigned long,
1687 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Al Viro9206de92009-12-03 15:23:11 -05001689 unsigned long error = arch_mmap_check(addr, len, flags);
1690 if (error)
1691 return error;
1692
1693 /* Careful about overflows.. */
1694 if (len > TASK_SIZE)
1695 return -ENOMEM;
1696
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001697 get_area = current->mm->get_unmapped_area;
1698 if (file && file->f_op && file->f_op->get_unmapped_area)
1699 get_area = file->f_op->get_unmapped_area;
1700 addr = get_area(file, addr, len, pgoff, flags);
1701 if (IS_ERR_VALUE(addr))
1702 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001704 if (addr > TASK_SIZE - len)
1705 return -ENOMEM;
1706 if (addr & ~PAGE_MASK)
1707 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001708
Martin Schwidefsky08e7d9b2008-02-04 22:29:16 -08001709 return arch_rebalance_pgtables(addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712EXPORT_SYMBOL(get_unmapped_area);
1713
1714/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
ZhenwenXu48aae422009-01-06 14:40:21 -08001715struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 struct vm_area_struct *vma = NULL;
1718
1719 if (mm) {
1720 /* Check the cache first. */
1721 /* (Cache hit rate is typically around 35%.) */
Jan Stancek9ee73982013-04-08 13:00:02 -07001722 vma = ACCESS_ONCE(mm->mmap_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1724 struct rb_node * rb_node;
1725
1726 rb_node = mm->mm_rb.rb_node;
1727 vma = NULL;
1728
1729 while (rb_node) {
1730 struct vm_area_struct * vma_tmp;
1731
1732 vma_tmp = rb_entry(rb_node,
1733 struct vm_area_struct, vm_rb);
1734
1735 if (vma_tmp->vm_end > addr) {
1736 vma = vma_tmp;
1737 if (vma_tmp->vm_start <= addr)
1738 break;
1739 rb_node = rb_node->rb_left;
1740 } else
1741 rb_node = rb_node->rb_right;
1742 }
1743 if (vma)
1744 mm->mmap_cache = vma;
1745 }
1746 }
1747 return vma;
1748}
1749
1750EXPORT_SYMBOL(find_vma);
1751
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001752/*
1753 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755struct vm_area_struct *
1756find_vma_prev(struct mm_struct *mm, unsigned long addr,
1757 struct vm_area_struct **pprev)
1758{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001759 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001761 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05001762 if (vma) {
1763 *pprev = vma->vm_prev;
1764 } else {
1765 struct rb_node *rb_node = mm->mm_rb.rb_node;
1766 *pprev = NULL;
1767 while (rb_node) {
1768 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1769 rb_node = rb_node->rb_right;
1770 }
1771 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001772 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
1775/*
1776 * Verify that the stack growth is acceptable and
1777 * update accounting. This is shared with both the
1778 * grow-up and grow-down cases.
1779 */
ZhenwenXu48aae422009-01-06 14:40:21 -08001780static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 struct mm_struct *mm = vma->vm_mm;
1783 struct rlimit *rlim = current->signal->rlim;
Linus Torvaldsd1f72aa2015-01-11 11:33:57 -08001784 unsigned long new_start, actual_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /* address space limit tests */
akpm@osdl.org119f6572005-05-01 08:58:35 -07001787 if (!may_expand_vm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return -ENOMEM;
1789
1790 /* Stack limit test */
Linus Torvaldsd1f72aa2015-01-11 11:33:57 -08001791 actual_size = size;
1792 if (size && (vma->vm_flags & (VM_GROWSUP | VM_GROWSDOWN)))
1793 actual_size -= PAGE_SIZE;
1794 if (actual_size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return -ENOMEM;
1796
1797 /* mlock limit tests */
1798 if (vma->vm_flags & VM_LOCKED) {
1799 unsigned long locked;
1800 unsigned long limit;
1801 locked = mm->locked_vm + grow;
Jiri Slaby59e99e52010-03-05 13:41:44 -08001802 limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
1803 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (locked > limit && !capable(CAP_IPC_LOCK))
1805 return -ENOMEM;
1806 }
1807
Adam Litke0d59a012007-01-30 14:35:39 -08001808 /* Check to ensure the stack will not grow into a hugetlb-only region */
1809 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
1810 vma->vm_end - size;
1811 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
1812 return -EFAULT;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /*
1815 * Overcommit.. This must be the final test, as it will
1816 * update security statistics.
1817 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01001818 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return -ENOMEM;
1820
1821 /* Ok, everything looks good - let it rip */
1822 mm->total_vm += grow;
1823 if (vma->vm_flags & VM_LOCKED)
1824 mm->locked_vm += grow;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001825 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return 0;
1827}
1828
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001829#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001831 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1832 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001834int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Hugh Dickinse8a93762017-06-19 04:03:24 -07001836 struct vm_area_struct *next;
1837 unsigned long gap_addr;
1838 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (!(vma->vm_flags & VM_GROWSUP))
1841 return -EFAULT;
1842
1843 /*
1844 * We must make sure the anon_vma is allocated
1845 * so that the anon_vma locking is not a noop.
1846 */
Helge Deller036d4232017-06-19 17:34:05 +02001847 /* Guard against exceeding limits of the address space. */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001848 address &= PAGE_MASK;
Helge Deller036d4232017-06-19 17:34:05 +02001849 if (address >= TASK_SIZE)
Hugh Dickinse8a93762017-06-19 04:03:24 -07001850 return -ENOMEM;
Helge Deller036d4232017-06-19 17:34:05 +02001851 address += PAGE_SIZE;
Hugh Dickinse8a93762017-06-19 04:03:24 -07001852
1853 /* Enforce stack_guard_gap */
1854 gap_addr = address + stack_guard_gap;
Helge Deller036d4232017-06-19 17:34:05 +02001855
1856 /* Guard against overflow */
1857 if (gap_addr < address || gap_addr > TASK_SIZE)
1858 gap_addr = TASK_SIZE;
1859
Hugh Dickinse8a93762017-06-19 04:03:24 -07001860 next = vma->vm_next;
1861 if (next && next->vm_start < gap_addr) {
1862 if (!(next->vm_flags & VM_GROWSUP))
1863 return -ENOMEM;
1864 /* Check that both stack segments have the same anon_vma? */
1865 }
1866
1867 /* We must make sure the anon_vma is allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (unlikely(anon_vma_prepare(vma)))
1869 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 /*
1872 * vma->vm_start/vm_end cannot change under us because the caller
1873 * is required to hold the mmap_sem in read mode. We need the
1874 * anon_vma lock to serialize against concurrent expand_stacks.
1875 */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001876 vma_lock_anon_vma(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 /* Somebody else might have raced and expanded it already */
1879 if (address > vma->vm_end) {
1880 unsigned long size, grow;
1881
1882 size = address - vma->vm_start;
1883 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1884
Hugh Dickins42c36f62011-05-09 17:44:42 -07001885 error = -ENOMEM;
1886 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
1887 error = acct_stack_growth(vma, size, grow);
1888 if (!error) {
1889 vma->vm_end = address;
1890 perf_event_mmap(vma);
1891 }
Eric B Munson3af9e852010-05-18 15:30:49 +01001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Rik van Rielbb4a3402010-08-09 17:18:37 -07001894 vma_unlock_anon_vma(vma);
David Rientjes8c948ea2014-10-29 14:50:31 -07001895 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return error;
1897}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001898#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900/*
1901 * vma is the first one with address < vma->vm_start. Have to extend vma.
1902 */
Michal Hockod05f3162011-05-24 17:11:44 -07001903int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001904 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Hugh Dickinse8a93762017-06-19 04:03:24 -07001906 struct vm_area_struct *prev;
1907 unsigned long gap_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int error;
1909
Eric Paris88694772007-11-26 18:47:26 -05001910 address &= PAGE_MASK;
Richard Knutsson88c3f7a2007-12-08 12:02:48 +01001911 error = security_file_mmap(NULL, 0, 0, 0, address, 1);
Eric Paris88694772007-11-26 18:47:26 -05001912 if (error)
1913 return error;
1914
Hugh Dickinse8a93762017-06-19 04:03:24 -07001915 /* Enforce stack_guard_gap */
1916 gap_addr = address - stack_guard_gap;
1917 if (gap_addr > address)
1918 return -ENOMEM;
1919 prev = vma->vm_prev;
1920 if (prev && prev->vm_end > gap_addr) {
1921 if (!(prev->vm_flags & VM_GROWSDOWN))
1922 return -ENOMEM;
1923 /* Check that both stack segments have the same anon_vma? */
1924 }
1925
1926 /* We must make sure the anon_vma is allocated. */
1927 if (unlikely(anon_vma_prepare(vma)))
1928 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /*
1931 * vma->vm_start/vm_end cannot change under us because the caller
1932 * is required to hold the mmap_sem in read mode. We need the
1933 * anon_vma lock to serialize against concurrent expand_stacks.
1934 */
Hugh Dickinse8a93762017-06-19 04:03:24 -07001935 vma_lock_anon_vma(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /* Somebody else might have raced and expanded it already */
1938 if (address < vma->vm_start) {
1939 unsigned long size, grow;
1940
1941 size = vma->vm_end - address;
1942 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1943
Linus Torvaldsa626ca62011-04-13 08:07:28 -07001944 error = -ENOMEM;
1945 if (grow <= vma->vm_pgoff) {
1946 error = acct_stack_growth(vma, size, grow);
1947 if (!error) {
1948 vma->vm_start = address;
1949 vma->vm_pgoff -= grow;
1950 perf_event_mmap(vma);
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953 }
Rik van Rielbb4a3402010-08-09 17:18:37 -07001954 vma_unlock_anon_vma(vma);
David Rientjes8c948ea2014-10-29 14:50:31 -07001955 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return error;
1957}
1958
Hugh Dickinse8a93762017-06-19 04:03:24 -07001959/* enforced gap between the expanding stack and other mappings. */
1960unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
1961
1962static int __init cmdline_parse_stack_guard_gap(char *p)
1963{
1964 unsigned long val;
1965 char *endptr;
1966
1967 val = simple_strtoul(p, &endptr, 10);
1968 if (!*endptr)
1969 stack_guard_gap = val << PAGE_SHIFT;
1970
1971 return 0;
1972}
1973__setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
1974
Linus Torvaldsd21452e2013-02-27 08:36:04 -08001975/*
1976 * Note how expand_stack() refuses to expand the stack all the way to
1977 * abut the next virtual mapping, *unless* that mapping itself is also
1978 * a stack mapping. We want to leave room for a guard page, after all
1979 * (the guard page itself is not added here, that is done by the
1980 * actual page faulting logic)
1981 *
1982 * This matches the behavior of the guard page logic (see mm/memory.c:
1983 * check_stack_guard_page()), which only allows the guard page to be
1984 * removed under these circumstances.
1985 */
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001986#ifdef CONFIG_STACK_GROWSUP
1987int expand_stack(struct vm_area_struct *vma, unsigned long address)
1988{
Linus Torvaldsd21452e2013-02-27 08:36:04 -08001989 struct vm_area_struct *next;
1990
1991 address &= PAGE_MASK;
1992 next = vma->vm_next;
1993 if (next && next->vm_start == address + PAGE_SIZE) {
1994 if (!(next->vm_flags & VM_GROWSUP))
1995 return -ENOMEM;
1996 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001997 return expand_upwards(vma, address);
1998}
1999
2000struct vm_area_struct *
2001find_extend_vma(struct mm_struct *mm, unsigned long addr)
2002{
2003 struct vm_area_struct *vma, *prev;
2004
2005 addr &= PAGE_MASK;
2006 vma = find_vma_prev(mm, addr, &prev);
2007 if (vma && (vma->vm_start <= addr))
2008 return vma;
Andrea Arcangelieb01a292019-04-18 17:50:52 -07002009 /* don't alter vm_end if the coredump is running */
2010 if (!prev || !mmget_still_valid(mm) || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002011 return NULL;
Rik van Rielba470de2008-10-18 20:26:50 -07002012 if (prev->vm_flags & VM_LOCKED) {
KOSAKI Motohiroc58267c2010-03-05 13:41:43 -08002013 mlock_vma_pages_range(prev, addr, prev->vm_end);
Rik van Rielba470de2008-10-18 20:26:50 -07002014 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002015 return prev;
2016}
2017#else
2018int expand_stack(struct vm_area_struct *vma, unsigned long address)
2019{
Linus Torvaldsd21452e2013-02-27 08:36:04 -08002020 struct vm_area_struct *prev;
2021
2022 address &= PAGE_MASK;
2023 prev = vma->vm_prev;
2024 if (prev && prev->vm_end == address) {
2025 if (!(prev->vm_flags & VM_GROWSDOWN))
2026 return -ENOMEM;
2027 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002028 return expand_downwards(vma, address);
2029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031struct vm_area_struct *
2032find_extend_vma(struct mm_struct * mm, unsigned long addr)
2033{
2034 struct vm_area_struct * vma;
2035 unsigned long start;
2036
2037 addr &= PAGE_MASK;
2038 vma = find_vma(mm,addr);
2039 if (!vma)
2040 return NULL;
2041 if (vma->vm_start <= addr)
2042 return vma;
2043 if (!(vma->vm_flags & VM_GROWSDOWN))
2044 return NULL;
Andrea Arcangelieb01a292019-04-18 17:50:52 -07002045 /* don't alter vm_start if the coredump is running */
2046 if (!mmget_still_valid(mm))
2047 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 start = vma->vm_start;
2049 if (expand_stack(vma, addr))
2050 return NULL;
Rik van Rielba470de2008-10-18 20:26:50 -07002051 if (vma->vm_flags & VM_LOCKED) {
KOSAKI Motohiroc58267c2010-03-05 13:41:43 -08002052 mlock_vma_pages_range(vma, addr, start);
Rik van Rielba470de2008-10-18 20:26:50 -07002053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return vma;
2055}
2056#endif
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002059 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002061 *
2062 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002064static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Hugh Dickins365e9c872005-10-29 18:16:18 -07002066 /* Update high watermark before we lower total_vm */
2067 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002069 long nrpages = vma_pages(vma);
2070
2071 mm->total_vm -= nrpages;
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002072 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002073 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002074 } while (vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 validate_mm(mm);
2076}
2077
2078/*
2079 * Get rid of page table information in the indicated region.
2080 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002081 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 */
2083static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002084 struct vm_area_struct *vma, struct vm_area_struct *prev,
2085 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Hugh Dickinse0da3822005-04-19 13:29:15 -07002087 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002088 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unsigned long nr_accounted = 0;
2090
2091 lru_add_drain();
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002092 tlb_gather_mmu(&tlb, mm, 0);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002093 update_hiwater_rss(mm);
Hugh Dickins508034a2005-10-29 18:16:30 -07002094 unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 vm_unacct_memory(nr_accounted);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002096 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins188b13f2013-04-29 15:07:44 -07002097 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002098 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
2101/*
2102 * Create a list of vma's touched by the unmap, removing them from the mm's
2103 * vma list as we go..
2104 */
2105static void
2106detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2107 struct vm_area_struct *prev, unsigned long end)
2108{
2109 struct vm_area_struct **insertion_point;
2110 struct vm_area_struct *tail_vma = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07002111 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002114 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 do {
2116 rb_erase(&vma->vm_rb, &mm->mm_rb);
2117 mm->map_count--;
2118 tail_vma = vma;
2119 vma = vma->vm_next;
2120 } while (vma && vma->vm_start < end);
2121 *insertion_point = vma;
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002122 if (vma)
2123 vma->vm_prev = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 tail_vma->vm_next = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07002125 if (mm->unmap_area == arch_unmap_area)
2126 addr = prev ? prev->vm_end : mm->mmap_base;
2127 else
2128 addr = vma ? vma->vm_start : mm->mmap_base;
2129 mm->unmap_area(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 mm->mmap_cache = NULL; /* Kill the cache. */
2131}
2132
2133/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002134 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2135 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 */
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002137static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 unsigned long addr, int new_below)
2139{
2140 struct mempolicy *pol;
2141 struct vm_area_struct *new;
Rik van Riel5beb4932010-03-05 13:42:07 -08002142 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Andi Kleena5516432008-07-23 21:27:41 -07002144 if (is_vm_hugetlb_page(vma) && (addr &
2145 ~(huge_page_mask(hstate_vma(vma)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return -EINVAL;
2147
Christoph Lametere94b1762006-12-06 20:33:17 -08002148 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (!new)
Rik van Riel5beb4932010-03-05 13:42:07 -08002150 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 /* most fields are the same, copy all, and then fixup */
2153 *new = *vma;
2154
Rik van Riel5beb4932010-03-05 13:42:07 -08002155 INIT_LIST_HEAD(&new->anon_vma_chain);
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (new_below)
2158 new->vm_end = addr;
2159 else {
2160 new->vm_start = addr;
2161 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2162 }
2163
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07002164 pol = mpol_dup(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (IS_ERR(pol)) {
Rik van Riel5beb4932010-03-05 13:42:07 -08002166 err = PTR_ERR(pol);
2167 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169 vma_set_policy(new, pol);
2170
Rik van Riel5beb4932010-03-05 13:42:07 -08002171 if (anon_vma_clone(new, vma))
2172 goto out_free_mpol;
2173
Matt Helsley925d1c42008-04-29 01:01:36 -07002174 if (new->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 get_file(new->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -07002176 if (vma->vm_flags & VM_EXECUTABLE)
2177 added_exe_file_vma(mm);
2178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 if (new->vm_ops && new->vm_ops->open)
2181 new->vm_ops->open(new);
2182
2183 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002184 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2186 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002187 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Rik van Riel5beb4932010-03-05 13:42:07 -08002189 /* Success. */
2190 if (!err)
2191 return 0;
2192
2193 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002194 if (new->vm_ops && new->vm_ops->close)
2195 new->vm_ops->close(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002196 if (new->vm_file) {
2197 if (vma->vm_flags & VM_EXECUTABLE)
2198 removed_exe_file_vma(mm);
2199 fput(new->vm_file);
2200 }
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002201 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002202 out_free_mpol:
2203 mpol_put(pol);
2204 out_free_vma:
2205 kmem_cache_free(vm_area_cachep, new);
2206 out_err:
2207 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002210/*
2211 * Split a vma into two pieces at address 'addr', a new vma is allocated
2212 * either for the first part or the tail.
2213 */
2214int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2215 unsigned long addr, int new_below)
2216{
2217 if (mm->map_count >= sysctl_max_map_count)
2218 return -ENOMEM;
2219
2220 return __split_vma(mm, vma, addr, new_below);
2221}
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223/* Munmap is split into 2 main parts -- this part which finds
2224 * what needs doing, and the areas themselves, which do the
2225 * work. This now handles partial unmappings.
2226 * Jeremy Fitzhardinge <jeremy@goop.org>
2227 */
2228int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2229{
2230 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002231 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
2234 return -EINVAL;
2235
2236 if ((len = PAGE_ALIGN(len)) == 0)
2237 return -EINVAL;
2238
2239 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002240 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002241 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002243 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002244 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 /* if it doesn't overlap, we have nothing.. */
2247 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002248 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return 0;
2250
2251 /*
2252 * If we need to split any vma, do it now to save pain later.
2253 *
2254 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2255 * unmapped vm_area_struct will remain in use: so lower split_vma
2256 * places tmp vma above, and higher split_vma places tmp vma below.
2257 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002258 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002259 int error;
2260
2261 /*
2262 * Make sure that map_count on return from munmap() will
2263 * not exceed its limit; but let map_count go just above
2264 * its limit temporarily, to help free resources as expected.
2265 */
2266 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2267 return -ENOMEM;
2268
2269 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (error)
2271 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002272 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274
2275 /* Does it split the last one? */
2276 last = find_vma(mm, end);
2277 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002278 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (error)
2280 return error;
2281 }
Hugh Dickins146425a2005-04-19 13:29:18 -07002282 vma = prev? prev->vm_next: mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002285 * unlock any mlock()ed ranges before detaching vmas
2286 */
2287 if (mm->locked_vm) {
2288 struct vm_area_struct *tmp = vma;
2289 while (tmp && tmp->vm_start < end) {
2290 if (tmp->vm_flags & VM_LOCKED) {
2291 mm->locked_vm -= vma_pages(tmp);
2292 munlock_vma_pages_all(tmp);
2293 }
2294 tmp = tmp->vm_next;
2295 }
2296 }
2297
2298 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 * Remove the vma's, and unmap the actual pages
2300 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002301 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2302 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002305 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 return 0;
2308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309EXPORT_SYMBOL(do_munmap);
2310
Al Virobfce2812012-04-20 21:57:04 -04002311int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002312{
2313 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002314 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002315
2316 down_write(&mm->mmap_sem);
2317 ret = do_munmap(mm, start, len);
2318 up_write(&mm->mmap_sem);
2319 return ret;
2320}
2321EXPORT_SYMBOL(vm_munmap);
2322
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002323SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 profile_munmap(addr);
Al Virobfce2812012-04-20 21:57:04 -04002326 return vm_munmap(addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
2329static inline void verify_mm_writelocked(struct mm_struct *mm)
2330{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002331#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2333 WARN_ON(1);
2334 up_read(&mm->mmap_sem);
2335 }
2336#endif
2337}
2338
2339/*
2340 * this is really a simplified "do_mmap". it only handles
2341 * anonymous maps. eventually we may be able to do some
2342 * brk-specific accounting here.
2343 */
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002344static unsigned long do_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 struct mm_struct * mm = current->mm;
2347 struct vm_area_struct * vma, * prev;
2348 unsigned long flags;
2349 struct rb_node ** rb_link, * rb_parent;
2350 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002351 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 len = PAGE_ALIGN(len);
2354 if (!len)
2355 return addr;
2356
Richard Knutsson88c3f7a2007-12-08 12:02:48 +01002357 error = security_file_mmap(NULL, 0, 0, 0, addr, 1);
Eric Paris5a211a52007-12-04 11:06:55 -05002358 if (error)
2359 return error;
2360
Kirill Korotaev3a459752006-09-07 14:17:04 +04002361 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2362
Al Viro2c6a10162009-12-03 19:40:46 -05002363 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
2364 if (error & ~PAGE_MASK)
Kirill Korotaev3a459752006-09-07 14:17:04 +04002365 return error;
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /*
2368 * mlock MCL_FUTURE?
2369 */
2370 if (mm->def_flags & VM_LOCKED) {
2371 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07002372 locked = len >> PAGE_SHIFT;
2373 locked += mm->locked_vm;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002374 lock_limit = rlimit(RLIMIT_MEMLOCK);
Chris Wright93ea1d02005-05-01 08:58:38 -07002375 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
2377 return -EAGAIN;
2378 }
2379
2380 /*
2381 * mm->mmap_sem is required to protect against another thread
2382 * changing the mappings in case we sleep.
2383 */
2384 verify_mm_writelocked(mm);
2385
2386 /*
2387 * Clear old maps. this also does some error checking for us
2388 */
2389 munmap_back:
2390 vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2391 if (vma && vma->vm_start < addr + len) {
2392 if (do_munmap(mm, addr, len))
2393 return -ENOMEM;
2394 goto munmap_back;
2395 }
2396
2397 /* Check against address space limits *after* clearing old maps... */
akpm@osdl.org119f6572005-05-01 08:58:35 -07002398 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return -ENOMEM;
2400
2401 if (mm->map_count > sysctl_max_map_count)
2402 return -ENOMEM;
2403
Al Viro191c5422012-02-13 03:58:52 +00002404 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return -ENOMEM;
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07002408 vma = vma_merge(mm, prev, addr, addr + len, flags,
Colin Crossa9e6b182013-06-26 17:26:01 -07002409 NULL, NULL, pgoff, NULL, NULL);
Rik van Rielba470de2008-10-18 20:26:50 -07002410 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 goto out;
2412
2413 /*
2414 * create a vma struct for an anonymous mapping
2415 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002416 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (!vma) {
2418 vm_unacct_memory(len >> PAGE_SHIFT);
2419 return -ENOMEM;
2420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Rik van Riel5beb4932010-03-05 13:42:07 -08002422 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 vma->vm_mm = mm;
2424 vma->vm_start = addr;
2425 vma->vm_end = addr + len;
2426 vma->vm_pgoff = pgoff;
2427 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07002428 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 vma_link(mm, vma, prev, rb_link, rb_parent);
2430out:
Eric B Munson3af9e852010-05-18 15:30:49 +01002431 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 mm->total_vm += len >> PAGE_SHIFT;
2433 if (flags & VM_LOCKED) {
Rik van Rielba470de2008-10-18 20:26:50 -07002434 if (!mlock_vma_pages_range(vma, addr, addr + len))
2435 mm->locked_vm += (len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437 return addr;
2438}
2439
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002440unsigned long vm_brk(unsigned long addr, unsigned long len)
2441{
2442 struct mm_struct *mm = current->mm;
2443 unsigned long ret;
2444
2445 down_write(&mm->mmap_sem);
2446 ret = do_brk(addr, len);
2447 up_write(&mm->mmap_sem);
2448 return ret;
2449}
2450EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452/* Release all mmaps. */
2453void exit_mmap(struct mm_struct *mm)
2454{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002455 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07002456 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 unsigned long nr_accounted = 0;
2458
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002459 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002460 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002461
Rik van Rielba470de2008-10-18 20:26:50 -07002462 if (mm->locked_vm) {
2463 vma = mm->mmap;
2464 while (vma) {
2465 if (vma->vm_flags & VM_LOCKED)
2466 munlock_vma_pages_all(vma);
2467 vma = vma->vm_next;
2468 }
2469 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002470
2471 arch_exit_mmap(mm);
2472
Rik van Rielba470de2008-10-18 20:26:50 -07002473 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002474 if (!vma) /* Can happen if dup_mmap() received an OOM */
2475 return;
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 flush_cache_mm(mm);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002479 tlb_gather_mmu(&tlb, mm, 1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08002480 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07002481 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Al Viro6e8bb012012-03-05 13:41:15 -05002482 unmap_vmas(&tlb, vma, 0, -1, &nr_accounted, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 vm_unacct_memory(nr_accounted);
Hugh Dickins9ba69292009-09-21 17:02:20 -07002484
Hugh Dickins188b13f2013-04-29 15:07:44 -07002485 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05002486 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07002489 * Walk the list again, actually closing and freeing it,
2490 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002492 while (vma)
2493 vma = remove_vma(vma);
Hugh Dickinse0da3822005-04-19 13:29:15 -07002494
Hugh Dickinse2cdef82005-04-19 13:29:19 -07002495 BUG_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
2498/* Insert vm structure into process list sorted by address
2499 * and into the inode's i_mmap tree. If vm_file is non-NULL
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002500 * then i_mmap_mutex is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 */
2502int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
2503{
2504 struct vm_area_struct * __vma, * prev;
2505 struct rb_node ** rb_link, * rb_parent;
2506
2507 /*
2508 * The vm_pgoff of a purely anonymous vma should be irrelevant
2509 * until its first write fault, when page's anon_vma and index
2510 * are set. But now set the vm_pgoff it will almost certainly
2511 * end up with (unless mremap moves it elsewhere before that
2512 * first wfault), so /proc/pid/maps tells a consistent story.
2513 *
2514 * By setting it to reflect the virtual start address of the
2515 * vma, merges and splits can happen in a seamless way, just
2516 * using the existing file pgoff checks and manipulations.
2517 * Similarly in do_mmap_pgoff and in do_brk.
2518 */
2519 if (!vma->vm_file) {
2520 BUG_ON(vma->anon_vma);
2521 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2522 }
2523 __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
2524 if (__vma && __vma->vm_start < vma->vm_end)
2525 return -ENOMEM;
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002526 if ((vma->vm_flags & VM_ACCOUNT) &&
Alan Cox34b4e4a2007-08-22 14:01:28 -07002527 security_vm_enough_memory_mm(mm, vma_pages(vma)))
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002528 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 vma_link(mm, vma, prev, rb_link, rb_parent);
2530 return 0;
2531}
2532
2533/*
2534 * Copy the vma structure to a new location in the same mm,
2535 * prior to moving page table entries, to effect an mremap move.
2536 */
2537struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
2538 unsigned long addr, unsigned long len, pgoff_t pgoff)
2539{
2540 struct vm_area_struct *vma = *vmap;
2541 unsigned long vma_start = vma->vm_start;
2542 struct mm_struct *mm = vma->vm_mm;
2543 struct vm_area_struct *new_vma, *prev;
2544 struct rb_node **rb_link, *rb_parent;
2545 struct mempolicy *pol;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002546 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 /*
2549 * If anonymous vma has not yet been faulted, update new pgoff
2550 * to match new location, to increase its chance of merging.
2551 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002552 if (unlikely(!vma->vm_file && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002554 faulted_in_anon_vma = false;
2555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
2558 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
Colin Crossa9e6b182013-06-26 17:26:01 -07002559 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
2560 vma_get_anon_name(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (new_vma) {
2562 /*
2563 * Source vma may have been merged into new_vma
2564 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002565 if (unlikely(vma_start >= new_vma->vm_start &&
2566 vma_start < new_vma->vm_end)) {
2567 /*
2568 * The only way we can get a vma_merge with
2569 * self during an mremap is if the vma hasn't
2570 * been faulted in yet and we were allowed to
2571 * reset the dst vma->vm_pgoff to the
2572 * destination address of the mremap to allow
2573 * the merge to happen. mremap must change the
2574 * vm_pgoff linearity between src and dst vmas
2575 * (in turn preventing a vma_merge) to be
2576 * safe. It is only safe to keep the vm_pgoff
2577 * linear if there are no pages mapped yet.
2578 */
2579 VM_BUG_ON(faulted_in_anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 *vmap = new_vma;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002581 } else
2582 anon_vma_moveto_tail(new_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08002584 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (new_vma) {
2586 *new_vma = *vma;
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07002587 pol = mpol_dup(vma_policy(vma));
Rik van Riel5beb4932010-03-05 13:42:07 -08002588 if (IS_ERR(pol))
2589 goto out_free_vma;
2590 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2591 if (anon_vma_clone(new_vma, vma))
2592 goto out_free_mempol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 vma_set_policy(new_vma, pol);
2594 new_vma->vm_start = addr;
2595 new_vma->vm_end = addr + len;
2596 new_vma->vm_pgoff = pgoff;
Matt Helsley925d1c42008-04-29 01:01:36 -07002597 if (new_vma->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 get_file(new_vma->vm_file);
Matt Helsley925d1c42008-04-29 01:01:36 -07002599 if (vma->vm_flags & VM_EXECUTABLE)
2600 added_exe_file_vma(mm);
2601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (new_vma->vm_ops && new_vma->vm_ops->open)
2603 new_vma->vm_ops->open(new_vma);
2604 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2605 }
2606 }
2607 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002608
2609 out_free_mempol:
2610 mpol_put(pol);
2611 out_free_vma:
2612 kmem_cache_free(vm_area_cachep, new_vma);
2613 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
akpm@osdl.org119f6572005-05-01 08:58:35 -07002615
2616/*
2617 * Return true if the calling process may expand its vm space by the passed
2618 * number of pages
2619 */
2620int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2621{
2622 unsigned long cur = mm->total_vm; /* pages */
2623 unsigned long lim;
2624
Jiri Slaby59e99e52010-03-05 13:41:44 -08002625 lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002626
2627 if (cur + npages > lim)
2628 return 0;
2629 return 1;
2630}
Roland McGrathfa5dc222007-02-08 14:20:41 -08002631
2632
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002633static int special_mapping_fault(struct vm_area_struct *vma,
2634 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08002635{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002636 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002637 struct page **pages;
2638
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002639 /*
2640 * special mappings have no vm_file, and in that case, the mm
2641 * uses vm_pgoff internally. So we have to subtract it from here.
2642 * We are allowed to do this because we are the mm; do not copy
2643 * this code into drivers!
2644 */
2645 pgoff = vmf->pgoff - vma->vm_pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002646
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002647 for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2648 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002649
2650 if (*pages) {
2651 struct page *page = *pages;
2652 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002653 vmf->page = page;
2654 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002655 }
2656
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002657 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002658}
2659
2660/*
2661 * Having a close hook prevents vma merging regardless of flags.
2662 */
2663static void special_mapping_close(struct vm_area_struct *vma)
2664{
2665}
2666
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002667static const struct vm_operations_struct special_mapping_vmops = {
Roland McGrathfa5dc222007-02-08 14:20:41 -08002668 .close = special_mapping_close,
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002669 .fault = special_mapping_fault,
Roland McGrathfa5dc222007-02-08 14:20:41 -08002670};
2671
2672/*
2673 * Called with mm->mmap_sem held for writing.
2674 * Insert a new vma covering the given region, with the given flags.
2675 * Its pages are supplied by the given array of struct page *.
2676 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2677 * The region past the last page supplied will always produce SIGBUS.
2678 * The array pointer and the pages it points to are assumed to stay alive
2679 * for as long as this mapping might exist.
2680 */
2681int install_special_mapping(struct mm_struct *mm,
2682 unsigned long addr, unsigned long len,
2683 unsigned long vm_flags, struct page **pages)
2684{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002685 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002686 struct vm_area_struct *vma;
2687
2688 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2689 if (unlikely(vma == NULL))
2690 return -ENOMEM;
2691
Rik van Riel5beb4932010-03-05 13:42:07 -08002692 INIT_LIST_HEAD(&vma->anon_vma_chain);
Roland McGrathfa5dc222007-02-08 14:20:41 -08002693 vma->vm_mm = mm;
2694 vma->vm_start = addr;
2695 vma->vm_end = addr + len;
2696
Nick Piggin2f987352008-02-02 03:08:53 +01002697 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
Coly Li3ed75eb2007-10-18 23:39:15 -07002698 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08002699
2700 vma->vm_ops = &special_mapping_vmops;
2701 vma->vm_private_data = pages;
2702
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002703 ret = security_file_mmap(NULL, 0, 0, 0, vma->vm_start, 1);
2704 if (ret)
2705 goto out;
2706
2707 ret = insert_vm_struct(mm, vma);
2708 if (ret)
2709 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002710
2711 mm->total_vm += len >> PAGE_SHIFT;
2712
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002713 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02002714
Roland McGrathfa5dc222007-02-08 14:20:41 -08002715 return 0;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002716
2717out:
2718 kmem_cache_free(vm_area_cachep, vma);
2719 return ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002720}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002721
2722static DEFINE_MUTEX(mm_all_locks_mutex);
2723
Peter Zijlstra454ed842008-08-11 09:30:25 +02002724static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002725{
Rik van Riel012f1802010-08-09 17:18:40 -07002726 if (!test_bit(0, (unsigned long *) &anon_vma->root->head.next)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002727 /*
2728 * The LSB of head.next can't change from under us
2729 * because we hold the mm_all_locks_mutex.
2730 */
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002731 mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002732 /*
2733 * We can safely modify head.next after taking the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002734 * anon_vma->root->mutex. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002735 * the same anon_vma we won't take it again.
2736 *
2737 * No need of atomic instructions here, head.next
2738 * can't change from under us thanks to the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002739 * anon_vma->root->mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002740 */
2741 if (__test_and_set_bit(0, (unsigned long *)
Rik van Riel012f1802010-08-09 17:18:40 -07002742 &anon_vma->root->head.next))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002743 BUG();
2744 }
2745}
2746
Peter Zijlstra454ed842008-08-11 09:30:25 +02002747static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002748{
2749 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2750 /*
2751 * AS_MM_ALL_LOCKS can't change from under us because
2752 * we hold the mm_all_locks_mutex.
2753 *
2754 * Operations on ->flags have to be atomic because
2755 * even if AS_MM_ALL_LOCKS is stable thanks to the
2756 * mm_all_locks_mutex, there may be other cpus
2757 * changing other bitflags in parallel to us.
2758 */
2759 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
2760 BUG();
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002761 mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002762 }
2763}
2764
2765/*
2766 * This operation locks against the VM for all pte/vma/mm related
2767 * operations that could ever happen on a certain mm. This includes
2768 * vmtruncate, try_to_unmap, and all page faults.
2769 *
2770 * The caller must take the mmap_sem in write mode before calling
2771 * mm_take_all_locks(). The caller isn't allowed to release the
2772 * mmap_sem until mm_drop_all_locks() returns.
2773 *
2774 * mmap_sem in write mode is required in order to block all operations
2775 * that could modify pagetables and free pages without need of
2776 * altering the vma layout (for example populate_range() with
2777 * nonlinear vmas). It's also needed in write mode to avoid new
2778 * anon_vmas to be associated with existing vmas.
2779 *
2780 * A single task can't take more than one mm_take_all_locks() in a row
2781 * or it would deadlock.
2782 *
2783 * The LSB in anon_vma->head.next and the AS_MM_ALL_LOCKS bitflag in
2784 * mapping->flags avoid to take the same lock twice, if more than one
2785 * vma in this mm is backed by the same anon_vma or address_space.
2786 *
2787 * We can take all the locks in random order because the VM code
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002788 * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002789 * takes more than one of them in a row. Secondly we're protected
2790 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
2791 *
2792 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2793 * that may have to take thousand of locks.
2794 *
2795 * mm_take_all_locks() can fail if it's interrupted by signals.
2796 */
2797int mm_take_all_locks(struct mm_struct *mm)
2798{
2799 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002800 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002801
2802 BUG_ON(down_read_trylock(&mm->mmap_sem));
2803
2804 mutex_lock(&mm_all_locks_mutex);
2805
2806 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2807 if (signal_pending(current))
2808 goto out_unlock;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002809 if (vma->vm_file && vma->vm_file->f_mapping)
Peter Zijlstra454ed842008-08-11 09:30:25 +02002810 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002811 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02002812
2813 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2814 if (signal_pending(current))
2815 goto out_unlock;
2816 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08002817 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2818 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02002819 }
2820
Kautuk Consul584cff52011-10-31 17:08:59 -07002821 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002822
2823out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07002824 mm_drop_all_locks(mm);
2825 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002826}
2827
2828static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
2829{
Rik van Riel012f1802010-08-09 17:18:40 -07002830 if (test_bit(0, (unsigned long *) &anon_vma->root->head.next)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002831 /*
2832 * The LSB of head.next can't change to 0 from under
2833 * us because we hold the mm_all_locks_mutex.
2834 *
2835 * We must however clear the bitflag before unlocking
2836 * the vma so the users using the anon_vma->head will
2837 * never see our bitflag.
2838 *
2839 * No need of atomic instructions here, head.next
2840 * can't change from under us until we release the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002841 * anon_vma->root->mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002842 */
2843 if (!__test_and_clear_bit(0, (unsigned long *)
Rik van Riel012f1802010-08-09 17:18:40 -07002844 &anon_vma->root->head.next))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002845 BUG();
Rik van Rielcba48b92010-08-09 17:18:38 -07002846 anon_vma_unlock(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002847 }
2848}
2849
2850static void vm_unlock_mapping(struct address_space *mapping)
2851{
2852 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2853 /*
2854 * AS_MM_ALL_LOCKS can't change to 0 from under us
2855 * because we hold the mm_all_locks_mutex.
2856 */
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002857 mutex_unlock(&mapping->i_mmap_mutex);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002858 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
2859 &mapping->flags))
2860 BUG();
2861 }
2862}
2863
2864/*
2865 * The mmap_sem cannot be released by the caller until
2866 * mm_drop_all_locks() returns.
2867 */
2868void mm_drop_all_locks(struct mm_struct *mm)
2869{
2870 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002871 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002872
2873 BUG_ON(down_read_trylock(&mm->mmap_sem));
2874 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
2875
2876 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2877 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08002878 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2879 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002880 if (vma->vm_file && vma->vm_file->f_mapping)
2881 vm_unlock_mapping(vma->vm_file->f_mapping);
2882 }
2883
2884 mutex_unlock(&mm_all_locks_mutex);
2885}
David Howells8feae132009-01-08 12:04:47 +00002886
2887/*
2888 * initialise the VMA slab
2889 */
2890void __init mmap_init(void)
2891{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07002892 int ret;
2893
2894 ret = percpu_counter_init(&vm_committed_as, 0);
2895 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00002896}