blob: ff93f6c8436c465bf9dd701c3d4dc5fb6e8ae176 [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
9#include <linux/slab.h>
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070010#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/shm.h>
13#include <linux/mman.h>
14#include <linux/pagemap.h>
15#include <linux/swap.h>
16#include <linux/syscalls.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080017#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/file.h>
20#include <linux/fs.h>
21#include <linux/personality.h>
22#include <linux/security.h>
23#include <linux/hugetlb.h>
24#include <linux/profile.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040025#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mount.h>
27#include <linux/mempolicy.h>
28#include <linux/rmap.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070029#include <linux/mmu_notifier.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020030#include <linux/perf_event.h>
Al Viro120a7952010-10-30 02:54:44 -040031#include <linux/audit.h>
Andrea Arcangelib15d00b2011-01-13 15:46:59 -080032#include <linux/khugepaged.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053033#include <linux/uprobes.h>
Michel Lespinassed3737182012-12-11 16:01:38 -080034#include <linux/rbtree_augmented.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
Hugh Dickinse0da3822005-04-19 13:29:15 -070051static void unmap_region(struct mm_struct *mm,
52 struct vm_area_struct *vma, struct vm_area_struct *prev,
53 unsigned long start, unsigned long end);
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* description of effects of mapping type and prot in current implementation.
56 * this is due to the limited x86 page protection hardware. The expected
57 * behavior is in parens:
58 *
59 * map_type prot
60 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
61 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
62 * w: (no) no w: (no) no w: (yes) yes w: (no) no
63 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
64 *
65 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
66 * w: (no) no w: (no) no w: (copy) copy w: (no) no
67 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
68 *
69 */
70pgprot_t protection_map[16] = {
71 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
72 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
73};
74
Hugh Dickins804af2c2006-07-26 21:39:49 +010075pgprot_t vm_get_page_prot(unsigned long vm_flags)
76{
Dave Kleikampb845f312008-07-08 00:28:51 +100077 return __pgprot(pgprot_val(protection_map[vm_flags &
78 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
79 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +010080}
81EXPORT_SYMBOL(vm_get_page_prot);
82
Shaohua Li34679d72011-05-24 17:11:18 -070083int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS; /* heuristic overcommit */
84int sysctl_overcommit_ratio __read_mostly = 50; /* default is 50% */
Christoph Lameterc3d8c142005-09-06 15:16:33 -070085int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
Shaohua Li34679d72011-05-24 17:11:18 -070086/*
87 * Make sure vm_committed_as in one cacheline and not cacheline shared with
88 * other variables. It can be updated by several CPUs frequently.
89 */
90struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * Check that a process has enough memory to allocate a new virtual
94 * mapping. 0 means there is enough memory for the allocation to
95 * succeed and -ENOMEM implies there is not.
96 *
97 * We currently support three overcommit policies, which are set via the
98 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
99 *
100 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
101 * Additional code 2002 Jul 20 by Robert Love.
102 *
103 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
104 *
105 * Note this is a helper function intended to be used by LSMs which
106 * wish to use this logic.
107 */
Alan Cox34b4e4a2007-08-22 14:01:28 -0700108int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 unsigned long free, allowed;
111
112 vm_acct_memory(pages);
113
114 /*
115 * Sometimes we want to use more memory than we have
116 */
117 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
118 return 0;
119
120 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
Dmitry Finkc15bef32011-07-25 17:12:19 -0700121 free = global_page_state(NR_FREE_PAGES);
122 free += global_page_state(NR_FILE_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Dmitry Finkc15bef32011-07-25 17:12:19 -0700124 /*
125 * shmem pages shouldn't be counted as free in this
126 * case, they can't be purged, only swapped out, and
127 * that won't affect the overall amount of available
128 * memory in the system.
129 */
130 free -= global_page_state(NR_SHMEM);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 free += nr_swap_pages;
133
134 /*
135 * Any slabs which are created with the
136 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
137 * which are reclaimable, under pressure. The dentry
138 * cache and most inode caches should fall into this
139 */
Christoph Lameter972d1a72006-09-25 23:31:51 -0700140 free += global_page_state(NR_SLAB_RECLAIMABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /*
Dmitry Finkc15bef32011-07-25 17:12:19 -0700143 * Leave reserved pages. The pages are not for anonymous pages.
144 */
145 if (free <= totalreserve_pages)
146 goto error;
147 else
148 free -= totalreserve_pages;
149
150 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * Leave the last 3% for root
152 */
153 if (!cap_sys_admin)
154 free -= free / 32;
155
156 if (free > pages)
157 return 0;
158
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700159 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 allowed = (totalram_pages - hugetlb_total_pages())
163 * sysctl_overcommit_ratio / 100;
164 /*
165 * Leave the last 3% for root
166 */
167 if (!cap_sys_admin)
168 allowed -= allowed / 32;
169 allowed += total_swap_pages;
170
171 /* Don't let a single process grow too big:
172 leave 3% of the size of this process for other processes */
Alan Cox731572d2008-10-29 14:01:20 -0700173 if (mm)
174 allowed -= mm->total_vm / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -0700176 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 0;
Hideo AOKI6d9f7832006-04-10 22:53:00 -0700178error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 vm_unacct_memory(pages);
180
181 return -ENOMEM;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700185 * Requires inode->i_mapping->i_mmap_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
187static void __remove_shared_vm_struct(struct vm_area_struct *vma,
188 struct file *file, struct address_space *mapping)
189{
190 if (vma->vm_flags & VM_DENYWRITE)
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800191 atomic_inc(&file->f_path.dentry->d_inode->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (vma->vm_flags & VM_SHARED)
193 mapping->i_mmap_writable--;
194
195 flush_dcache_mmap_lock(mapping);
196 if (unlikely(vma->vm_flags & VM_NONLINEAR))
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700197 list_del_init(&vma->shared.nonlinear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 else
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700199 vma_interval_tree_remove(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 flush_dcache_mmap_unlock(mapping);
201}
202
203/*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700204 * Unlink a file-based vm structure from its interval tree, to hide
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700205 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700207void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 struct file *file = vma->vm_file;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (file) {
212 struct address_space *mapping = file->f_mapping;
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700213 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 __remove_shared_vm_struct(vma, file, mapping);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700215 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700217}
218
219/*
220 * Close a vm structure and free it, returning the next.
221 */
222static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
223{
224 struct vm_area_struct *next = vma->vm_next;
225
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700226 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (vma->vm_ops && vma->vm_ops->close)
228 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700229 if (vma->vm_file)
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700230 fput(vma->vm_file);
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700231 mpol_put(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 kmem_cache_free(vm_area_cachep, vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700233 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -0700236static unsigned long do_brk(unsigned long addr, unsigned long len);
237
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100238SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 unsigned long rlim, retval;
241 unsigned long newbrk, oldbrk;
242 struct mm_struct *mm = current->mm;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700243 unsigned long min_brk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 down_write(&mm->mmap_sem);
246
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700247#ifdef CONFIG_COMPAT_BRK
Jiri Kosina5520e892011-01-13 15:47:23 -0800248 /*
249 * CONFIG_COMPAT_BRK can still be overridden by setting
250 * randomize_va_space to 2, which will still cause mm->start_brk
251 * to be arbitrarily shifted
252 */
Jiri Kosina4471a672011-04-14 15:22:09 -0700253 if (current->brk_randomized)
Jiri Kosina5520e892011-01-13 15:47:23 -0800254 min_brk = mm->start_brk;
255 else
256 min_brk = mm->end_data;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700257#else
258 min_brk = mm->start_brk;
259#endif
260 if (brk < min_brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700262
263 /*
264 * Check against rlimit here. If this check is done later after the test
265 * of oldbrk with newbrk then it can escape the test and let the data
266 * segment grow beyond its set limit the in case where the limit is
267 * not page aligned -Ram Gupta
268 */
Jiri Slaby59e99e52010-03-05 13:41:44 -0800269 rlim = rlimit(RLIMIT_DATA);
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100270 if (rlim < RLIM_INFINITY && (brk - mm->start_brk) +
271 (mm->end_data - mm->start_data) > rlim)
Ram Gupta1e624192006-04-10 22:52:57 -0700272 goto out;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 newbrk = PAGE_ALIGN(brk);
275 oldbrk = PAGE_ALIGN(mm->brk);
276 if (oldbrk == newbrk)
277 goto set_brk;
278
279 /* Always allow shrinking brk. */
280 if (brk <= mm->brk) {
281 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
282 goto set_brk;
283 goto out;
284 }
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Check against existing mmap mappings. */
287 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
288 goto out;
289
290 /* Ok, looks good - let it rip. */
291 if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
292 goto out;
293set_brk:
294 mm->brk = brk;
295out:
296 retval = mm->brk;
297 up_write(&mm->mmap_sem);
298 return retval;
299}
300
Michel Lespinassed3737182012-12-11 16:01:38 -0800301static long vma_compute_subtree_gap(struct vm_area_struct *vma)
302{
303 unsigned long max, subtree_gap;
304 max = vma->vm_start;
305 if (vma->vm_prev)
306 max -= vma->vm_prev->vm_end;
307 if (vma->vm_rb.rb_left) {
308 subtree_gap = rb_entry(vma->vm_rb.rb_left,
309 struct vm_area_struct, vm_rb)->rb_subtree_gap;
310 if (subtree_gap > max)
311 max = subtree_gap;
312 }
313 if (vma->vm_rb.rb_right) {
314 subtree_gap = rb_entry(vma->vm_rb.rb_right,
315 struct vm_area_struct, vm_rb)->rb_subtree_gap;
316 if (subtree_gap > max)
317 max = subtree_gap;
318 }
319 return max;
320}
321
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700322#ifdef CONFIG_DEBUG_VM_RB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static int browse_rb(struct rb_root *root)
324{
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800325 int i = 0, j, bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct rb_node *nd, *pn = NULL;
327 unsigned long prev = 0, pend = 0;
328
329 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
330 struct vm_area_struct *vma;
331 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800332 if (vma->vm_start < prev) {
333 printk("vm_start %lx prev %lx\n", vma->vm_start, prev);
334 bug = 1;
335 }
336 if (vma->vm_start < pend) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800338 bug = 1;
339 }
340 if (vma->vm_start > vma->vm_end) {
341 printk("vm_end %lx < vm_start %lx\n",
342 vma->vm_end, vma->vm_start);
343 bug = 1;
344 }
345 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
346 printk("free gap %lx, correct %lx\n",
347 vma->rb_subtree_gap,
348 vma_compute_subtree_gap(vma));
349 bug = 1;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 i++;
352 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800353 prev = vma->vm_start;
354 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 j = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800357 for (nd = pn; nd; nd = rb_prev(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 j++;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800359 if (i != j) {
360 printk("backwards %d, forwards %d\n", j, i);
361 bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800363 return bug ? -1 : i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Michel Lespinassed3737182012-12-11 16:01:38 -0800366static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
367{
368 struct rb_node *nd;
369
370 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
371 struct vm_area_struct *vma;
372 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
373 BUG_ON(vma != ignore &&
374 vma->rb_subtree_gap != vma_compute_subtree_gap(vma));
375 }
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378void validate_mm(struct mm_struct *mm)
379{
380 int bug = 0;
381 int i = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800382 unsigned long highest_address = 0;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700383 struct vm_area_struct *vma = mm->mmap;
384 while (vma) {
385 struct anon_vma_chain *avc;
Michel Lespinasse63c3b902012-11-16 14:14:47 -0800386 vma_lock_anon_vma(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700387 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
388 anon_vma_interval_tree_verify(avc);
Michel Lespinasse63c3b902012-11-16 14:14:47 -0800389 vma_unlock_anon_vma(vma);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800390 highest_address = vma->vm_end;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700391 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 i++;
393 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800394 if (i != mm->map_count) {
395 printk("map_count %d vm_next %d\n", mm->map_count, i);
396 bug = 1;
397 }
398 if (highest_address != mm->highest_vm_end) {
399 printk("mm->highest_vm_end %lx, found %lx\n",
400 mm->highest_vm_end, highest_address);
401 bug = 1;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 i = browse_rb(&mm->mm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800404 if (i != mm->map_count) {
405 printk("map_count %d rb %d\n", mm->map_count, i);
406 bug = 1;
407 }
Eric Sesterhenn46a350e2006-04-01 01:23:29 +0200408 BUG_ON(bug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410#else
Michel Lespinassed3737182012-12-11 16:01:38 -0800411#define validate_mm_rb(root, ignore) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#define validate_mm(mm) do { } while (0)
413#endif
414
Michel Lespinassed3737182012-12-11 16:01:38 -0800415RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
416 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
417
418/*
419 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
420 * vma->vm_prev->vm_end values changed, without modifying the vma's position
421 * in the rbtree.
422 */
423static void vma_gap_update(struct vm_area_struct *vma)
424{
425 /*
426 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
427 * function that does exacltly what we want.
428 */
429 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
430}
431
432static inline void vma_rb_insert(struct vm_area_struct *vma,
433 struct rb_root *root)
434{
435 /* All rb_subtree_gap values must be consistent prior to insertion */
436 validate_mm_rb(root, NULL);
437
438 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
439}
440
441static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
442{
443 /*
444 * All rb_subtree_gap values must be consistent prior to erase,
445 * with the possible exception of the vma being erased.
446 */
447 validate_mm_rb(root, vma);
448
449 /*
450 * Note rb_erase_augmented is a fairly large inline function,
451 * so make sure we instantiate it only once with our desired
452 * augmented rbtree callbacks.
453 */
454 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
455}
456
Michel Lespinassebf181b92012-10-08 16:31:39 -0700457/*
458 * vma has some anon_vma assigned, and is already inserted on that
459 * anon_vma's interval trees.
460 *
461 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
462 * vma must be removed from the anon_vma's interval trees using
463 * anon_vma_interval_tree_pre_update_vma().
464 *
465 * After the update, the vma will be reinserted using
466 * anon_vma_interval_tree_post_update_vma().
467 *
468 * The entire update must be protected by exclusive mmap_sem and by
469 * the root anon_vma's mutex.
470 */
471static inline void
472anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
473{
474 struct anon_vma_chain *avc;
475
476 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
477 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
478}
479
480static inline void
481anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
482{
483 struct anon_vma_chain *avc;
484
485 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
486 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
487}
488
Hugh Dickins6597d782012-10-08 16:29:07 -0700489static int find_vma_links(struct mm_struct *mm, unsigned long addr,
490 unsigned long end, struct vm_area_struct **pprev,
491 struct rb_node ***rb_link, struct rb_node **rb_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Hugh Dickins6597d782012-10-08 16:29:07 -0700493 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 __rb_link = &mm->mm_rb.rb_node;
496 rb_prev = __rb_parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 while (*__rb_link) {
499 struct vm_area_struct *vma_tmp;
500
501 __rb_parent = *__rb_link;
502 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
503
504 if (vma_tmp->vm_end > addr) {
Hugh Dickins6597d782012-10-08 16:29:07 -0700505 /* Fail if an existing vma overlaps the area */
506 if (vma_tmp->vm_start < end)
507 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 __rb_link = &__rb_parent->rb_left;
509 } else {
510 rb_prev = __rb_parent;
511 __rb_link = &__rb_parent->rb_right;
512 }
513 }
514
515 *pprev = NULL;
516 if (rb_prev)
517 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
518 *rb_link = __rb_link;
519 *rb_parent = __rb_parent;
Hugh Dickins6597d782012-10-08 16:29:07 -0700520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
524 struct rb_node **rb_link, struct rb_node *rb_parent)
525{
Michel Lespinassed3737182012-12-11 16:01:38 -0800526 /* Update tracking information for the gap following the new vma. */
527 if (vma->vm_next)
528 vma_gap_update(vma->vm_next);
529 else
530 mm->highest_vm_end = vma->vm_end;
531
532 /*
533 * vma->vm_prev wasn't known when we followed the rbtree to find the
534 * correct insertion point for that vma. As a result, we could not
535 * update the vma vm_rb parents rb_subtree_gap values on the way down.
536 * So, we first insert the vma with a zero rb_subtree_gap value
537 * (to be consistent with what we did on the way down), and then
538 * immediately update the gap to the correct value. Finally we
539 * rebalance the rbtree after all augmented values have been set.
540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
Michel Lespinassed3737182012-12-11 16:01:38 -0800542 vma->rb_subtree_gap = 0;
543 vma_gap_update(vma);
544 vma_rb_insert(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700547static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
ZhenwenXu48aae422009-01-06 14:40:21 -0800549 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 file = vma->vm_file;
552 if (file) {
553 struct address_space *mapping = file->f_mapping;
554
555 if (vma->vm_flags & VM_DENYWRITE)
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -0800556 atomic_dec(&file->f_path.dentry->d_inode->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (vma->vm_flags & VM_SHARED)
558 mapping->i_mmap_writable++;
559
560 flush_dcache_mmap_lock(mapping);
561 if (unlikely(vma->vm_flags & VM_NONLINEAR))
562 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
563 else
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700564 vma_interval_tree_insert(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 flush_dcache_mmap_unlock(mapping);
566 }
567}
568
569static void
570__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
571 struct vm_area_struct *prev, struct rb_node **rb_link,
572 struct rb_node *rb_parent)
573{
574 __vma_link_list(mm, vma, prev, rb_parent);
575 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
579 struct vm_area_struct *prev, struct rb_node **rb_link,
580 struct rb_node *rb_parent)
581{
582 struct address_space *mapping = NULL;
583
584 if (vma->vm_file)
585 mapping = vma->vm_file->f_mapping;
586
Peter Zijlstra97a89412011-05-24 17:12:04 -0700587 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700588 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 __vma_link(mm, vma, prev, rb_link, rb_parent);
591 __vma_link_file(vma);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700594 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 mm->map_count++;
597 validate_mm(mm);
598}
599
600/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700601 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700602 * mm's list and rbtree. It has already been inserted into the interval tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800604static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Hugh Dickins6597d782012-10-08 16:29:07 -0700606 struct vm_area_struct *prev;
ZhenwenXu48aae422009-01-06 14:40:21 -0800607 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Hugh Dickins6597d782012-10-08 16:29:07 -0700609 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
610 &prev, &rb_link, &rb_parent))
611 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 __vma_link(mm, vma, prev, rb_link, rb_parent);
613 mm->map_count++;
614}
615
616static inline void
617__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
618 struct vm_area_struct *prev)
619{
Michel Lespinassed3737182012-12-11 16:01:38 -0800620 struct vm_area_struct *next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700621
Michel Lespinassed3737182012-12-11 16:01:38 -0800622 vma_rb_erase(vma, &mm->mm_rb);
623 prev->vm_next = next = vma->vm_next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700624 if (next)
625 next->vm_prev = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (mm->mmap_cache == vma)
627 mm->mmap_cache = prev;
628}
629
630/*
631 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
632 * is already present in an i_mmap tree without adjusting the tree.
633 * The following helper function should be used when such adjustments
634 * are necessary. The "insert" vma (if any) is to be inserted
635 * before we drop the necessary locks.
636 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800637int vma_adjust(struct vm_area_struct *vma, unsigned long start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
639{
640 struct mm_struct *mm = vma->vm_mm;
641 struct vm_area_struct *next = vma->vm_next;
642 struct vm_area_struct *importer = NULL;
643 struct address_space *mapping = NULL;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700644 struct rb_root *root = NULL;
Rik van Riel012f18002010-08-09 17:18:40 -0700645 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct file *file = vma->vm_file;
Michel Lespinassed3737182012-12-11 16:01:38 -0800647 bool start_changed = false, end_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 long adjust_next = 0;
649 int remove_next = 0;
650
651 if (next && !insert) {
Linus Torvalds287d97a2010-04-10 15:22:30 -0700652 struct vm_area_struct *exporter = NULL;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (end >= next->vm_end) {
655 /*
656 * vma expands, overlapping all the next, and
657 * perhaps the one after too (mprotect case 6).
658 */
659again: remove_next = 1 + (end > next->vm_end);
660 end = next->vm_end;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700661 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 importer = vma;
663 } else if (end > next->vm_start) {
664 /*
665 * vma expands, overlapping part of the next:
666 * mprotect case 5 shifting the boundary up.
667 */
668 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700669 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 importer = vma;
671 } else if (end < vma->vm_end) {
672 /*
673 * vma shrinks, and !insert tells it's not
674 * split_vma inserting another: so it must be
675 * mprotect case 4 shifting the boundary down.
676 */
677 adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700678 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 importer = next;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Rik van Riel5beb4932010-03-05 13:42:07 -0800682 /*
683 * Easily overlooked: when mprotect shifts the boundary,
684 * make sure the expanding vma has anon_vma set if the
685 * shrinking vma had, to cover any anon pages imported.
686 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700687 if (exporter && exporter->anon_vma && !importer->anon_vma) {
688 if (anon_vma_clone(importer, exporter))
Rik van Riel5beb4932010-03-05 13:42:07 -0800689 return -ENOMEM;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700690 importer->anon_vma = exporter->anon_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -0800691 }
692 }
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (file) {
695 mapping = file->f_mapping;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530696 if (!(vma->vm_flags & VM_NONLINEAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 root = &mapping->i_mmap;
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530698 uprobe_munmap(vma, vma->vm_start, vma->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530699
700 if (adjust_next)
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530701 uprobe_munmap(next, next->vm_start,
702 next->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530703 }
704
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700705 mutex_lock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700708 * Put into interval tree now, so instantiated pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * are visible to arm/parisc __flush_dcache_page
710 * throughout; but we cannot insert into address
711 * space until vma start or end is updated.
712 */
713 __vma_link_file(insert);
714 }
715 }
716
Andrea Arcangeli94fcc582011-01-13 15:47:08 -0800717 vma_adjust_trans_huge(vma, start, end, adjust_next);
718
Michel Lespinassebf181b92012-10-08 16:31:39 -0700719 anon_vma = vma->anon_vma;
720 if (!anon_vma && adjust_next)
721 anon_vma = next->anon_vma;
722 if (anon_vma) {
Michel Lespinasseca42b262012-10-08 16:30:01 -0700723 VM_BUG_ON(adjust_next && next->anon_vma &&
724 anon_vma != next->anon_vma);
Rik van Riel012f18002010-08-09 17:18:40 -0700725 anon_vma_lock(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700726 anon_vma_interval_tree_pre_update_vma(vma);
727 if (adjust_next)
728 anon_vma_interval_tree_pre_update_vma(next);
729 }
Rik van Riel012f18002010-08-09 17:18:40 -0700730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (root) {
732 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700733 vma_interval_tree_remove(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700735 vma_interval_tree_remove(next, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
Michel Lespinassed3737182012-12-11 16:01:38 -0800738 if (start != vma->vm_start) {
739 vma->vm_start = start;
740 start_changed = true;
741 }
742 if (end != vma->vm_end) {
743 vma->vm_end = end;
744 end_changed = true;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 vma->vm_pgoff = pgoff;
747 if (adjust_next) {
748 next->vm_start += adjust_next << PAGE_SHIFT;
749 next->vm_pgoff += adjust_next;
750 }
751
752 if (root) {
753 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700754 vma_interval_tree_insert(next, root);
755 vma_interval_tree_insert(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 flush_dcache_mmap_unlock(mapping);
757 }
758
759 if (remove_next) {
760 /*
761 * vma_merge has merged next into vma, and needs
762 * us to remove next before dropping the locks.
763 */
764 __vma_unlink(mm, next, vma);
765 if (file)
766 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 } else if (insert) {
768 /*
769 * split_vma has split insert from vma, and needs
770 * us to insert it before dropping the locks
771 * (it may either follow vma or precede it).
772 */
773 __insert_vm_struct(mm, insert);
Michel Lespinassed3737182012-12-11 16:01:38 -0800774 } else {
775 if (start_changed)
776 vma_gap_update(vma);
777 if (end_changed) {
778 if (!next)
779 mm->highest_vm_end = end;
780 else if (!adjust_next)
781 vma_gap_update(next);
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Michel Lespinassebf181b92012-10-08 16:31:39 -0700785 if (anon_vma) {
786 anon_vma_interval_tree_post_update_vma(vma);
787 if (adjust_next)
788 anon_vma_interval_tree_post_update_vma(next);
Rik van Riel012f18002010-08-09 17:18:40 -0700789 anon_vma_unlock(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (mapping)
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700792 mutex_unlock(&mapping->i_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530794 if (root) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100795 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530796
797 if (adjust_next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100798 uprobe_mmap(next);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (remove_next) {
Matt Helsley925d1c42008-04-29 01:01:36 -0700802 if (file) {
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530803 uprobe_munmap(next, next->vm_start, next->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 fput(file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700805 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800806 if (next->anon_vma)
807 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 mm->map_count--;
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700809 mpol_put(vma_policy(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 kmem_cache_free(vm_area_cachep, next);
811 /*
812 * In mprotect's case 6 (see comments on vma_merge),
813 * we must remove another next too. It would clutter
814 * up the code too much to do both in one go.
815 */
Michel Lespinassed3737182012-12-11 16:01:38 -0800816 next = vma->vm_next;
817 if (remove_next == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto again;
Michel Lespinassed3737182012-12-11 16:01:38 -0800819 else if (next)
820 vma_gap_update(next);
821 else
822 mm->highest_vm_end = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530824 if (insert && file)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100825 uprobe_mmap(insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -0800828
829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*
833 * If the vma has a ->close operation then the driver probably needs to release
834 * per-vma resources, so we don't attempt to merge those.
835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static inline int is_mergeable_vma(struct vm_area_struct *vma,
837 struct file *file, unsigned long vm_flags)
838{
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -0700839 if (vma->vm_flags ^ vm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841 if (vma->vm_file != file)
842 return 0;
843 if (vma->vm_ops && vma->vm_ops->close)
844 return 0;
845 return 1;
846}
847
848static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -0700849 struct anon_vma *anon_vma2,
850 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Shaohua Li965f55d2011-05-24 17:11:20 -0700852 /*
853 * The list_is_singular() test is to avoid merging VMA cloned from
854 * parents. This can improve scalability caused by anon_vma lock.
855 */
856 if ((!anon_vma1 || !anon_vma2) && (!vma ||
857 list_is_singular(&vma->anon_vma_chain)))
858 return 1;
859 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862/*
863 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
864 * in front of (at a lower virtual address and file offset than) the vma.
865 *
866 * We cannot merge two vmas if they have differently assigned (non-NULL)
867 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
868 *
869 * We don't check here for the merged mmap wrapping around the end of pagecache
870 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
871 * wrap, nor mmaps which cover the final page at index -1UL.
872 */
873static int
874can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
875 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
876{
877 if (is_mergeable_vma(vma, file, vm_flags) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700878 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (vma->vm_pgoff == vm_pgoff)
880 return 1;
881 }
882 return 0;
883}
884
885/*
886 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
887 * beyond (at a higher virtual address and file offset than) the vma.
888 *
889 * We cannot merge two vmas if they have differently assigned (non-NULL)
890 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
891 */
892static int
893can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
894 struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
895{
896 if (is_mergeable_vma(vma, file, vm_flags) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700897 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 pgoff_t vm_pglen;
899 vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
900 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
901 return 1;
902 }
903 return 0;
904}
905
906/*
907 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
908 * whether that can be merged with its predecessor or its successor.
909 * Or both (it neatly fills a hole).
910 *
911 * In most cases - when called for mmap, brk or mremap - [addr,end) is
912 * certain not to be mapped by the time vma_merge is called; but when
913 * called for mprotect, it is certain to be already mapped (either at
914 * an offset within prev, or at the start of next), and the flags of
915 * this area are about to be changed to vm_flags - and the no-change
916 * case has already been eliminated.
917 *
918 * The following mprotect cases have to be considered, where AAAA is
919 * the area passed down from mprotect_fixup, never extending beyond one
920 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
921 *
922 * AAAA AAAA AAAA AAAA
923 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
924 * cannot merge might become might become might become
925 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
926 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
927 * mremap move: PPPPNNNNNNNN 8
928 * AAAA
929 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
930 * might become case 1 below case 2 below case 3 below
931 *
932 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
933 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
934 */
935struct vm_area_struct *vma_merge(struct mm_struct *mm,
936 struct vm_area_struct *prev, unsigned long addr,
937 unsigned long end, unsigned long vm_flags,
938 struct anon_vma *anon_vma, struct file *file,
939 pgoff_t pgoff, struct mempolicy *policy)
940{
941 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
942 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800943 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /*
946 * We later require that vma->vm_flags == vm_flags,
947 * so this tests vma->vm_flags & VM_SPECIAL, too.
948 */
949 if (vm_flags & VM_SPECIAL)
950 return NULL;
951
952 if (prev)
953 next = prev->vm_next;
954 else
955 next = mm->mmap;
956 area = next;
957 if (next && next->vm_end == end) /* cases 6, 7, 8 */
958 next = next->vm_next;
959
960 /*
961 * Can it merge with the predecessor?
962 */
963 if (prev && prev->vm_end == addr &&
964 mpol_equal(vma_policy(prev), policy) &&
965 can_vma_merge_after(prev, vm_flags,
966 anon_vma, file, pgoff)) {
967 /*
968 * OK, it can. Can we now merge in the successor as well?
969 */
970 if (next && end == next->vm_start &&
971 mpol_equal(policy, vma_policy(next)) &&
972 can_vma_merge_before(next, vm_flags,
973 anon_vma, file, pgoff+pglen) &&
974 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -0700975 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* cases 1, 6 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800977 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 next->vm_end, prev->vm_pgoff, NULL);
979 } else /* cases 2, 5, 7 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800980 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 end, prev->vm_pgoff, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -0800982 if (err)
983 return NULL;
Andrea Arcangelib15d00b2011-01-13 15:46:59 -0800984 khugepaged_enter_vma_merge(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return prev;
986 }
987
988 /*
989 * Can this new request be merged in front of next?
990 */
991 if (next && end == next->vm_start &&
992 mpol_equal(policy, vma_policy(next)) &&
993 can_vma_merge_before(next, vm_flags,
994 anon_vma, file, pgoff+pglen)) {
995 if (prev && addr < prev->vm_end) /* case 4 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800996 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 addr, prev->vm_pgoff, NULL);
998 else /* cases 3, 8 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800999 err = vma_adjust(area, addr, next->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 next->vm_pgoff - pglen, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -08001001 if (err)
1002 return NULL;
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08001003 khugepaged_enter_vma_merge(area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return area;
1005 }
1006
1007 return NULL;
1008}
1009
1010/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001011 * Rough compatbility check to quickly see if it's even worth looking
1012 * at sharing an anon_vma.
1013 *
1014 * They need to have the same vm_file, and the flags can only differ
1015 * in things that mprotect may change.
1016 *
1017 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1018 * we can merge the two vma's. For example, we refuse to merge a vma if
1019 * there is a vm_ops->close() function, because that indicates that the
1020 * driver is doing some kind of reference counting. But that doesn't
1021 * really matter for the anon_vma sharing case.
1022 */
1023static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1024{
1025 return a->vm_end == b->vm_start &&
1026 mpol_equal(vma_policy(a), vma_policy(b)) &&
1027 a->vm_file == b->vm_file &&
1028 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC)) &&
1029 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1030}
1031
1032/*
1033 * Do some basic sanity checking to see if we can re-use the anon_vma
1034 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1035 * the same as 'old', the other will be the new one that is trying
1036 * to share the anon_vma.
1037 *
1038 * NOTE! This runs with mm_sem held for reading, so it is possible that
1039 * the anon_vma of 'old' is concurrently in the process of being set up
1040 * by another page fault trying to merge _that_. But that's ok: if it
1041 * is being set up, that automatically means that it will be a singleton
1042 * acceptable for merging, so we can do all of this optimistically. But
1043 * we do that ACCESS_ONCE() to make sure that we never re-load the pointer.
1044 *
1045 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1046 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1047 * is to return an anon_vma that is "complex" due to having gone through
1048 * a fork).
1049 *
1050 * We also make sure that the two vma's are compatible (adjacent,
1051 * and with the same memory policies). That's all stable, even with just
1052 * a read lock on the mm_sem.
1053 */
1054static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1055{
1056 if (anon_vma_compatible(a, b)) {
1057 struct anon_vma *anon_vma = ACCESS_ONCE(old->anon_vma);
1058
1059 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1060 return anon_vma;
1061 }
1062 return NULL;
1063}
1064
1065/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1067 * neighbouring vmas for a suitable anon_vma, before it goes off
1068 * to allocate a new anon_vma. It checks because a repetitive
1069 * sequence of mprotects and faults may otherwise lead to distinct
1070 * anon_vmas being allocated, preventing vma merge in subsequent
1071 * mprotect.
1072 */
1073struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1074{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001075 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 near = vma->vm_next;
1079 if (!near)
1080 goto try_prev;
1081
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001082 anon_vma = reusable_anon_vma(near, vma, near);
1083 if (anon_vma)
1084 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -07001086 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (!near)
1088 goto none;
1089
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001090 anon_vma = reusable_anon_vma(near, near, vma);
1091 if (anon_vma)
1092 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093none:
1094 /*
1095 * There's no absolute need to look only at touching neighbours:
1096 * we could search further afield for "compatible" anon_vmas.
1097 * But it would probably just be a waste of time searching,
1098 * or lead to too many vmas hanging off the same anon_vma.
1099 * We're trying to allow mprotect remerging later on,
1100 * not trying to minimize memory used for anon_vmas.
1101 */
1102 return NULL;
1103}
1104
1105#ifdef CONFIG_PROC_FS
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001106void vm_stat_account(struct mm_struct *mm, unsigned long flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct file *file, long pages)
1108{
1109 const unsigned long stack_flags
1110 = VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);
1111
Huang Shijie44de9d02012-07-31 16:41:49 -07001112 mm->total_vm += pages;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (file) {
1115 mm->shared_vm += pages;
1116 if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
1117 mm->exec_vm += pages;
1118 } else if (flags & stack_flags)
1119 mm->stack_vm += pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121#endif /* CONFIG_PROC_FS */
1122
1123/*
Al Viro40401532012-02-13 03:58:52 +00001124 * If a hint addr is less than mmap_min_addr change hint to be as
1125 * low as possible but still greater than mmap_min_addr
1126 */
1127static inline unsigned long round_hint_to_min(unsigned long hint)
1128{
1129 hint &= PAGE_MASK;
1130 if (((void *)hint != NULL) &&
1131 (hint < mmap_min_addr))
1132 return PAGE_ALIGN(mmap_min_addr);
1133 return hint;
1134}
1135
1136/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001137 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
1139
Al Viroe3fc6292012-05-30 20:08:42 -04001140unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 unsigned long len, unsigned long prot,
1142 unsigned long flags, unsigned long pgoff)
1143{
1144 struct mm_struct * mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 struct inode *inode;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001146 vm_flags_t vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * Does the application expect PROT_READ to imply PROT_EXEC?
1150 *
1151 * (the exception is when the underlying filesystem is noexec
1152 * mounted, in which case we dont add PROT_EXEC.)
1153 */
1154 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001155 if (!(file && (file->f_path.mnt->mnt_flags & MNT_NOEXEC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 prot |= PROT_EXEC;
1157
1158 if (!len)
1159 return -EINVAL;
1160
Eric Paris7cd94142007-11-26 18:47:40 -05001161 if (!(flags & MAP_FIXED))
1162 addr = round_hint_to_min(addr);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* Careful about overflows.. */
1165 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001166 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return -ENOMEM;
1168
1169 /* offset overflow? */
1170 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
1171 return -EOVERFLOW;
1172
1173 /* Too many mappings? */
1174 if (mm->map_count > sysctl_max_map_count)
1175 return -ENOMEM;
1176
1177 /* Obtain the address to map to. we verify (or select) it and ensure
1178 * that it represents a valid section of the address space.
1179 */
1180 addr = get_unmapped_area(file, addr, len, pgoff, flags);
1181 if (addr & ~PAGE_MASK)
1182 return addr;
1183
1184 /* Do simple checking here so the lower-level routines won't have
1185 * to. we assume access permissions have been handled by the open
1186 * of the memory object, so we don't do any here.
1187 */
1188 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
1189 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1190
Huang Shijiecdf7b342009-09-21 17:03:36 -07001191 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!can_do_mlock())
1193 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* mlock MCL_FUTURE? */
1196 if (vm_flags & VM_LOCKED) {
1197 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07001198 locked = len >> PAGE_SHIFT;
1199 locked += mm->locked_vm;
Jiri Slaby59e99e52010-03-05 13:41:44 -08001200 lock_limit = rlimit(RLIMIT_MEMLOCK);
Chris Wright93ea1d02005-05-01 08:58:38 -07001201 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1203 return -EAGAIN;
1204 }
1205
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001206 inode = file ? file->f_path.dentry->d_inode : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (file) {
1209 switch (flags & MAP_TYPE) {
1210 case MAP_SHARED:
1211 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1212 return -EACCES;
1213
1214 /*
1215 * Make sure we don't allow writing to an append-only
1216 * file..
1217 */
1218 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1219 return -EACCES;
1220
1221 /*
1222 * Make sure there are no mandatory locks on the file.
1223 */
1224 if (locks_verify_locked(inode))
1225 return -EAGAIN;
1226
1227 vm_flags |= VM_SHARED | VM_MAYSHARE;
1228 if (!(file->f_mode & FMODE_WRITE))
1229 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1230
1231 /* fall through */
1232 case MAP_PRIVATE:
1233 if (!(file->f_mode & FMODE_READ))
1234 return -EACCES;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001235 if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001236 if (vm_flags & VM_EXEC)
1237 return -EPERM;
1238 vm_flags &= ~VM_MAYEXEC;
1239 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001240
1241 if (!file->f_op || !file->f_op->mmap)
1242 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 break;
1244
1245 default:
1246 return -EINVAL;
1247 }
1248 } else {
1249 switch (flags & MAP_TYPE) {
1250 case MAP_SHARED:
Tejun Heoce363942008-09-03 16:09:47 +02001251 /*
1252 * Ignore pgoff.
1253 */
1254 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 vm_flags |= VM_SHARED | VM_MAYSHARE;
1256 break;
1257 case MAP_PRIVATE:
1258 /*
1259 * Set pgoff according to addr for anon_vma.
1260 */
1261 pgoff = addr >> PAGE_SHIFT;
1262 break;
1263 default:
1264 return -EINVAL;
1265 }
1266 }
1267
Mel Gorman5a6fe122009-02-10 14:02:27 +00001268 return mmap_region(file, addr, len, flags, vm_flags, pgoff);
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001269}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001270
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001271SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1272 unsigned long, prot, unsigned long, flags,
1273 unsigned long, fd, unsigned long, pgoff)
1274{
1275 struct file *file = NULL;
1276 unsigned long retval = -EBADF;
1277
1278 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001279 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001280 if (unlikely(flags & MAP_HUGETLB))
1281 return -EINVAL;
1282 file = fget(fd);
1283 if (!file)
1284 goto out;
1285 } else if (flags & MAP_HUGETLB) {
1286 struct user_struct *user = NULL;
1287 /*
1288 * VM_NORESERVE is used because the reservations will be
1289 * taken when vm_ops->mmap() is called
1290 * A dummy user value is used because we are not locking
1291 * memory so no accounting is necessary
1292 */
Steven Truelove40716e22012-03-21 16:34:14 -07001293 file = hugetlb_file_setup(HUGETLB_ANON_FILE, addr, len,
Andi Kleen42d73952012-12-11 16:01:34 -08001294 VM_NORESERVE,
1295 &user, HUGETLB_ANONHUGE_INODE,
1296 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001297 if (IS_ERR(file))
1298 return PTR_ERR(file);
1299 }
1300
1301 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1302
Al Viroeb36c582012-05-30 20:17:35 -04001303 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001304 if (file)
1305 fput(file);
1306out:
1307 return retval;
1308}
1309
Christoph Hellwiga4679372010-03-10 15:21:15 -08001310#ifdef __ARCH_WANT_SYS_OLD_MMAP
1311struct mmap_arg_struct {
1312 unsigned long addr;
1313 unsigned long len;
1314 unsigned long prot;
1315 unsigned long flags;
1316 unsigned long fd;
1317 unsigned long offset;
1318};
1319
1320SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1321{
1322 struct mmap_arg_struct a;
1323
1324 if (copy_from_user(&a, arg, sizeof(a)))
1325 return -EFAULT;
1326 if (a.offset & ~PAGE_MASK)
1327 return -EINVAL;
1328
1329 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1330 a.offset >> PAGE_SHIFT);
1331}
1332#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1333
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001334/*
1335 * Some shared mappigns will want the pages marked read-only
1336 * to track write events. If so, we'll downgrade vm_page_prot
1337 * to the private version (using protection_map[] without the
1338 * VM_SHARED bit).
1339 */
1340int vma_wants_writenotify(struct vm_area_struct *vma)
1341{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001342 vm_flags_t vm_flags = vma->vm_flags;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001343
1344 /* If it was private or non-writable, the write bit is already clear */
1345 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1346 return 0;
1347
1348 /* The backer wishes to know when pages are first written to? */
1349 if (vma->vm_ops && vma->vm_ops->page_mkwrite)
1350 return 1;
1351
1352 /* The open routine did something to the protections already? */
1353 if (pgprot_val(vma->vm_page_prot) !=
Coly Li3ed75eb2007-10-18 23:39:15 -07001354 pgprot_val(vm_get_page_prot(vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001355 return 0;
1356
1357 /* Specialty mapping? */
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001358 if (vm_flags & VM_PFNMAP)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001359 return 0;
1360
1361 /* Can the mapping track the dirty pages? */
1362 return vma->vm_file && vma->vm_file->f_mapping &&
1363 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1364}
1365
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001366/*
1367 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001368 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001369 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001370static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001371{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001372 /*
1373 * hugetlb has its own accounting separate from the core VM
1374 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1375 */
1376 if (file && is_file_hugepages(file))
1377 return 0;
1378
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001379 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1380}
1381
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001382unsigned long mmap_region(struct file *file, unsigned long addr,
1383 unsigned long len, unsigned long flags,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001384 vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001385{
1386 struct mm_struct *mm = current->mm;
1387 struct vm_area_struct *vma, *prev;
1388 int correct_wcount = 0;
1389 int error;
1390 struct rb_node **rb_link, *rb_parent;
1391 unsigned long charged = 0;
1392 struct inode *inode = file ? file->f_path.dentry->d_inode : NULL;
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* Clear old maps */
1395 error = -ENOMEM;
1396munmap_back:
Hugh Dickins6597d782012-10-08 16:29:07 -07001397 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (do_munmap(mm, addr, len))
1399 return -ENOMEM;
1400 goto munmap_back;
1401 }
1402
1403 /* Check against address space limit. */
akpm@osdl.org119f6572005-05-01 08:58:35 -07001404 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -ENOMEM;
1406
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001407 /*
1408 * Set 'VM_NORESERVE' if we should not account for the
Mel Gorman5a6fe122009-02-10 14:02:27 +00001409 * memory use of this mapping.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001410 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001411 if ((flags & MAP_NORESERVE)) {
1412 /* We honor MAP_NORESERVE if allowed to overcommit */
1413 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1414 vm_flags |= VM_NORESERVE;
1415
1416 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1417 if (file && is_file_hugepages(file))
1418 vm_flags |= VM_NORESERVE;
1419 }
Andy Whitcroftcdfd4322008-07-23 21:27:28 -07001420
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001421 /*
1422 * Private writable mapping: check memory availability
1423 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001424 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001425 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001426 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001427 return -ENOMEM;
1428 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001432 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 */
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001434 vma = vma_merge(mm, prev, addr, addr + len, vm_flags, NULL, file, pgoff, NULL);
1435 if (vma)
1436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 /*
1439 * Determine the object being mapped and call the appropriate
1440 * specific mapper. the address has already been validated, but
1441 * not unmapped, but the maps are removed from the list.
1442 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001443 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (!vma) {
1445 error = -ENOMEM;
1446 goto unacct_error;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 vma->vm_mm = mm;
1450 vma->vm_start = addr;
1451 vma->vm_end = addr + len;
1452 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001453 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 vma->vm_pgoff = pgoff;
Rik van Riel5beb4932010-03-05 13:42:07 -08001455 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Hugh Dickinsce8fea72012-03-06 12:28:52 -08001457 error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1461 goto free_vma;
1462 if (vm_flags & VM_DENYWRITE) {
1463 error = deny_write_access(file);
1464 if (error)
1465 goto free_vma;
1466 correct_wcount = 1;
1467 }
Al Virocb0942b2012-08-27 14:48:26 -04001468 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 error = file->f_op->mmap(file, vma);
1470 if (error)
1471 goto unmap_and_free_vma;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001472
1473 /* Can addr have changed??
1474 *
1475 * Answer: Yes, several device drivers can do it in their
1476 * f_op->mmap method. -DaveM
1477 */
1478 addr = vma->vm_start;
1479 pgoff = vma->vm_pgoff;
1480 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 } else if (vm_flags & VM_SHARED) {
Al Viro835ee792012-03-05 06:39:47 +00001482 if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
1483 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 error = shmem_zero_setup(vma);
1485 if (error)
1486 goto free_vma;
1487 }
1488
Magnus Dammc9d0bf22009-12-14 17:59:49 -08001489 if (vma_wants_writenotify(vma)) {
1490 pgprot_t pprot = vma->vm_page_prot;
1491
1492 /* Can vma->vm_page_prot have changed??
1493 *
1494 * Answer: Yes, drivers may have changed it in their
1495 * f_op->mmap method.
1496 *
1497 * Ensures that vmas marked as uncached stay that way.
1498 */
Hugh Dickins1ddd4392007-10-22 20:45:12 -07001499 vma->vm_page_prot = vm_get_page_prot(vm_flags & ~VM_SHARED);
Magnus Dammc9d0bf22009-12-14 17:59:49 -08001500 if (pgprot_val(pprot) == pgprot_val(pgprot_noncached(pprot)))
1501 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1502 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -07001503
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001504 vma_link(mm, vma, prev, rb_link, rb_parent);
1505 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001506
1507 /* Once vma denies write, undo our temporary denial count */
1508 if (correct_wcount)
1509 atomic_inc(&inode->i_writecount);
1510out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001512
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001513 vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (vm_flags & VM_LOCKED) {
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001515 if (!mlock_vma_pages_range(vma, addr, addr + len))
1516 mm->locked_vm += (len >> PAGE_SHIFT);
Rik van Rielba470de2008-10-18 20:26:50 -07001517 } else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
Nick Piggin54cb8822007-07-19 01:46:59 -07001518 make_pages_present(addr, addr + len);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301519
Oleg Nesterovc7a3a882012-08-19 19:10:42 +02001520 if (file)
1521 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return addr;
1524
1525unmap_and_free_vma:
1526 if (correct_wcount)
1527 atomic_inc(&inode->i_writecount);
1528 vma->vm_file = NULL;
1529 fput(file);
1530
1531 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001532 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1533 charged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534free_vma:
1535 kmem_cache_free(vm_area_cachep, vma);
1536unacct_error:
1537 if (charged)
1538 vm_unacct_memory(charged);
1539 return error;
1540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542/* Get an address range which is currently unmapped.
1543 * For shmat() with addr=0.
1544 *
1545 * Ugly calling convention alert:
1546 * Return value with the low bits set means error value,
1547 * ie
1548 * if (ret & ~PAGE_MASK)
1549 * error = ret;
1550 *
1551 * This function "knows" that -ENOMEM has the bits set.
1552 */
1553#ifndef HAVE_ARCH_UNMAPPED_AREA
1554unsigned long
1555arch_get_unmapped_area(struct file *filp, unsigned long addr,
1556 unsigned long len, unsigned long pgoff, unsigned long flags)
1557{
1558 struct mm_struct *mm = current->mm;
1559 struct vm_area_struct *vma;
1560 unsigned long start_addr;
1561
1562 if (len > TASK_SIZE)
1563 return -ENOMEM;
1564
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001565 if (flags & MAP_FIXED)
1566 return addr;
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (addr) {
1569 addr = PAGE_ALIGN(addr);
1570 vma = find_vma(mm, addr);
1571 if (TASK_SIZE - len >= addr &&
1572 (!vma || addr + len <= vma->vm_start))
1573 return addr;
1574 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001575 if (len > mm->cached_hole_size) {
1576 start_addr = addr = mm->free_area_cache;
1577 } else {
1578 start_addr = addr = TASK_UNMAPPED_BASE;
1579 mm->cached_hole_size = 0;
1580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582full_search:
1583 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
1584 /* At this point: (!vma || addr < vma->vm_end). */
1585 if (TASK_SIZE - len < addr) {
1586 /*
1587 * Start a new search - just in case we missed
1588 * some holes.
1589 */
1590 if (start_addr != TASK_UNMAPPED_BASE) {
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001591 addr = TASK_UNMAPPED_BASE;
1592 start_addr = addr;
1593 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 goto full_search;
1595 }
1596 return -ENOMEM;
1597 }
1598 if (!vma || addr + len <= vma->vm_start) {
1599 /*
1600 * Remember the place where we stopped the search:
1601 */
1602 mm->free_area_cache = addr + len;
1603 return addr;
1604 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001605 if (addr + mm->cached_hole_size < vma->vm_start)
1606 mm->cached_hole_size = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 addr = vma->vm_end;
1608 }
1609}
1610#endif
1611
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001612void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 /*
1615 * Is this a new hole at the lowest possible address?
1616 */
Xiao Guangrongf44d2192012-03-21 16:33:56 -07001617 if (addr >= TASK_UNMAPPED_BASE && addr < mm->free_area_cache)
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001618 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621/*
1622 * This mmap-allocator allocates new areas top-down from below the
1623 * stack's low limit (the base):
1624 */
1625#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1626unsigned long
1627arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1628 const unsigned long len, const unsigned long pgoff,
1629 const unsigned long flags)
1630{
1631 struct vm_area_struct *vma;
1632 struct mm_struct *mm = current->mm;
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001633 unsigned long addr = addr0, start_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 /* requested length too big for entire address space */
1636 if (len > TASK_SIZE)
1637 return -ENOMEM;
1638
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001639 if (flags & MAP_FIXED)
1640 return addr;
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 /* requesting a specific address */
1643 if (addr) {
1644 addr = PAGE_ALIGN(addr);
1645 vma = find_vma(mm, addr);
1646 if (TASK_SIZE - len >= addr &&
1647 (!vma || addr + len <= vma->vm_start))
1648 return addr;
1649 }
1650
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001651 /* check if free_area_cache is useful for us */
1652 if (len <= mm->cached_hole_size) {
1653 mm->cached_hole_size = 0;
1654 mm->free_area_cache = mm->mmap_base;
1655 }
1656
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001657try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /* either no address requested or can't fit in requested address hole */
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001659 start_addr = addr = mm->free_area_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001661 if (addr < len)
1662 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001664 addr -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 do {
1666 /*
1667 * Lookup failure means no vma is above this address,
1668 * else if new region fits below vma->vm_start,
1669 * return with success:
1670 */
1671 vma = find_vma(mm, addr);
1672 if (!vma || addr+len <= vma->vm_start)
1673 /* remember the address as a hint for next time */
1674 return (mm->free_area_cache = addr);
1675
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001676 /* remember the largest hole we saw so far */
1677 if (addr + mm->cached_hole_size < vma->vm_start)
1678 mm->cached_hole_size = vma->vm_start - addr;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* try just below the current vma->vm_start */
1681 addr = vma->vm_start-len;
Linus Torvalds49a43872005-05-18 15:39:33 -07001682 } while (len < vma->vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001684fail:
1685 /*
1686 * if hint left us with no space for the requested
1687 * mapping then try again:
1688 *
1689 * Note: this is different with the case of bottomup
1690 * which does the fully line-search, but we use find_vma
1691 * here that causes some holes skipped.
1692 */
1693 if (start_addr != mm->mmap_base) {
1694 mm->free_area_cache = mm->mmap_base;
1695 mm->cached_hole_size = 0;
1696 goto try_again;
1697 }
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /*
1700 * A failed mmap() very likely causes application failure,
1701 * so fall back to the bottom-up function here. This scenario
1702 * can happen with large stack limits and large mmap()
1703 * allocations.
1704 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001705 mm->cached_hole_size = ~0UL;
1706 mm->free_area_cache = TASK_UNMAPPED_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
1708 /*
1709 * Restore the topdown base:
1710 */
1711 mm->free_area_cache = mm->mmap_base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001712 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 return addr;
1715}
1716#endif
1717
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001718void arch_unmap_area_topdown(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 /*
1721 * Is this a new hole at the highest possible address?
1722 */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001723 if (addr > mm->free_area_cache)
1724 mm->free_area_cache = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /* dont allow allocations above current base */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001727 if (mm->free_area_cache > mm->mmap_base)
1728 mm->free_area_cache = mm->mmap_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731unsigned long
1732get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1733 unsigned long pgoff, unsigned long flags)
1734{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001735 unsigned long (*get_area)(struct file *, unsigned long,
1736 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Al Viro9206de92009-12-03 15:23:11 -05001738 unsigned long error = arch_mmap_check(addr, len, flags);
1739 if (error)
1740 return error;
1741
1742 /* Careful about overflows.. */
1743 if (len > TASK_SIZE)
1744 return -ENOMEM;
1745
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001746 get_area = current->mm->get_unmapped_area;
1747 if (file && file->f_op && file->f_op->get_unmapped_area)
1748 get_area = file->f_op->get_unmapped_area;
1749 addr = get_area(file, addr, len, pgoff, flags);
1750 if (IS_ERR_VALUE(addr))
1751 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001753 if (addr > TASK_SIZE - len)
1754 return -ENOMEM;
1755 if (addr & ~PAGE_MASK)
1756 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001757
Al Viro9ac4ed42012-05-30 17:13:15 -04001758 addr = arch_rebalance_pgtables(addr, len);
1759 error = security_mmap_addr(addr);
1760 return error ? error : addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
1763EXPORT_SYMBOL(get_unmapped_area);
1764
1765/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
ZhenwenXu48aae422009-01-06 14:40:21 -08001766struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 struct vm_area_struct *vma = NULL;
1769
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001770 if (WARN_ON_ONCE(!mm)) /* Remove this in linux-3.6 */
1771 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001773 /* Check the cache first. */
1774 /* (Cache hit rate is typically around 35%.) */
1775 vma = mm->mmap_cache;
1776 if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
1777 struct rb_node *rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001779 rb_node = mm->mm_rb.rb_node;
1780 vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001782 while (rb_node) {
1783 struct vm_area_struct *vma_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001785 vma_tmp = rb_entry(rb_node,
1786 struct vm_area_struct, vm_rb);
1787
1788 if (vma_tmp->vm_end > addr) {
1789 vma = vma_tmp;
1790 if (vma_tmp->vm_start <= addr)
1791 break;
1792 rb_node = rb_node->rb_left;
1793 } else
1794 rb_node = rb_node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001796 if (vma)
1797 mm->mmap_cache = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 return vma;
1800}
1801
1802EXPORT_SYMBOL(find_vma);
1803
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001804/*
1805 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807struct vm_area_struct *
1808find_vma_prev(struct mm_struct *mm, unsigned long addr,
1809 struct vm_area_struct **pprev)
1810{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001811 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001813 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05001814 if (vma) {
1815 *pprev = vma->vm_prev;
1816 } else {
1817 struct rb_node *rb_node = mm->mm_rb.rb_node;
1818 *pprev = NULL;
1819 while (rb_node) {
1820 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1821 rb_node = rb_node->rb_right;
1822 }
1823 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001824 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827/*
1828 * Verify that the stack growth is acceptable and
1829 * update accounting. This is shared with both the
1830 * grow-up and grow-down cases.
1831 */
ZhenwenXu48aae422009-01-06 14:40:21 -08001832static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 struct mm_struct *mm = vma->vm_mm;
1835 struct rlimit *rlim = current->signal->rlim;
Adam Litke0d59a012007-01-30 14:35:39 -08001836 unsigned long new_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /* address space limit tests */
akpm@osdl.org119f6572005-05-01 08:58:35 -07001839 if (!may_expand_vm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return -ENOMEM;
1841
1842 /* Stack limit test */
Jiri Slaby59e99e52010-03-05 13:41:44 -08001843 if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return -ENOMEM;
1845
1846 /* mlock limit tests */
1847 if (vma->vm_flags & VM_LOCKED) {
1848 unsigned long locked;
1849 unsigned long limit;
1850 locked = mm->locked_vm + grow;
Jiri Slaby59e99e52010-03-05 13:41:44 -08001851 limit = ACCESS_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
1852 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (locked > limit && !capable(CAP_IPC_LOCK))
1854 return -ENOMEM;
1855 }
1856
Adam Litke0d59a012007-01-30 14:35:39 -08001857 /* Check to ensure the stack will not grow into a hugetlb-only region */
1858 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
1859 vma->vm_end - size;
1860 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
1861 return -EFAULT;
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /*
1864 * Overcommit.. This must be the final test, as it will
1865 * update security statistics.
1866 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01001867 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return -ENOMEM;
1869
1870 /* Ok, everything looks good - let it rip */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (vma->vm_flags & VM_LOCKED)
1872 mm->locked_vm += grow;
Hugh Dickinsab50b8e2005-10-29 18:15:56 -07001873 vm_stat_account(mm, vma->vm_flags, vma->vm_file, grow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return 0;
1875}
1876
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001877#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001879 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1880 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001882int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 int error;
1885
1886 if (!(vma->vm_flags & VM_GROWSUP))
1887 return -EFAULT;
1888
1889 /*
1890 * We must make sure the anon_vma is allocated
1891 * so that the anon_vma locking is not a noop.
1892 */
1893 if (unlikely(anon_vma_prepare(vma)))
1894 return -ENOMEM;
Rik van Rielbb4a3402010-08-09 17:18:37 -07001895 vma_lock_anon_vma(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /*
1898 * vma->vm_start/vm_end cannot change under us because the caller
1899 * is required to hold the mmap_sem in read mode. We need the
1900 * anon_vma lock to serialize against concurrent expand_stacks.
Helge Deller06b32f32006-12-19 19:28:33 +01001901 * Also guard against wrapping around to address 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
Helge Deller06b32f32006-12-19 19:28:33 +01001903 if (address < PAGE_ALIGN(address+4))
1904 address = PAGE_ALIGN(address+4);
1905 else {
Rik van Rielbb4a3402010-08-09 17:18:37 -07001906 vma_unlock_anon_vma(vma);
Helge Deller06b32f32006-12-19 19:28:33 +01001907 return -ENOMEM;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 error = 0;
1910
1911 /* Somebody else might have raced and expanded it already */
1912 if (address > vma->vm_end) {
1913 unsigned long size, grow;
1914
1915 size = address - vma->vm_start;
1916 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1917
Hugh Dickins42c36f62011-05-09 17:44:42 -07001918 error = -ENOMEM;
1919 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
1920 error = acct_stack_growth(vma, size, grow);
1921 if (!error) {
Michel Lespinassebf181b92012-10-08 16:31:39 -07001922 anon_vma_interval_tree_pre_update_vma(vma);
Hugh Dickins42c36f62011-05-09 17:44:42 -07001923 vma->vm_end = address;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001924 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08001925 if (vma->vm_next)
1926 vma_gap_update(vma->vm_next);
1927 else
1928 vma->vm_mm->highest_vm_end = address;
Hugh Dickins42c36f62011-05-09 17:44:42 -07001929 perf_event_mmap(vma);
1930 }
Eric B Munson3af9e852010-05-18 15:30:49 +01001931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
Rik van Rielbb4a3402010-08-09 17:18:37 -07001933 vma_unlock_anon_vma(vma);
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08001934 khugepaged_enter_vma_merge(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -07001935 validate_mm(vma->vm_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return error;
1937}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07001938#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940/*
1941 * vma is the first one with address < vma->vm_start. Have to extend vma.
1942 */
Michal Hockod05f3162011-05-24 17:11:44 -07001943int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001944 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 int error;
1947
1948 /*
1949 * We must make sure the anon_vma is allocated
1950 * so that the anon_vma locking is not a noop.
1951 */
1952 if (unlikely(anon_vma_prepare(vma)))
1953 return -ENOMEM;
Eric Paris88694772007-11-26 18:47:26 -05001954
1955 address &= PAGE_MASK;
Al Viroe5467852012-05-30 13:30:51 -04001956 error = security_mmap_addr(address);
Eric Paris88694772007-11-26 18:47:26 -05001957 if (error)
1958 return error;
1959
Rik van Rielbb4a3402010-08-09 17:18:37 -07001960 vma_lock_anon_vma(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 /*
1963 * vma->vm_start/vm_end cannot change under us because the caller
1964 * is required to hold the mmap_sem in read mode. We need the
1965 * anon_vma lock to serialize against concurrent expand_stacks.
1966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /* Somebody else might have raced and expanded it already */
1969 if (address < vma->vm_start) {
1970 unsigned long size, grow;
1971
1972 size = vma->vm_end - address;
1973 grow = (vma->vm_start - address) >> PAGE_SHIFT;
1974
Linus Torvaldsa626ca62011-04-13 08:07:28 -07001975 error = -ENOMEM;
1976 if (grow <= vma->vm_pgoff) {
1977 error = acct_stack_growth(vma, size, grow);
1978 if (!error) {
Michel Lespinassebf181b92012-10-08 16:31:39 -07001979 anon_vma_interval_tree_pre_update_vma(vma);
Linus Torvaldsa626ca62011-04-13 08:07:28 -07001980 vma->vm_start = address;
1981 vma->vm_pgoff -= grow;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001982 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08001983 vma_gap_update(vma);
Linus Torvaldsa626ca62011-04-13 08:07:28 -07001984 perf_event_mmap(vma);
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 }
Rik van Rielbb4a3402010-08-09 17:18:37 -07001988 vma_unlock_anon_vma(vma);
Andrea Arcangelib15d00b2011-01-13 15:46:59 -08001989 khugepaged_enter_vma_merge(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -07001990 validate_mm(vma->vm_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return error;
1992}
1993
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001994#ifdef CONFIG_STACK_GROWSUP
1995int expand_stack(struct vm_area_struct *vma, unsigned long address)
1996{
1997 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;
Denys Vlasenko1c127182008-11-12 01:24:41 +01002009 if (!prev || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002010 return NULL;
Rik van Rielba470de2008-10-18 20:26:50 -07002011 if (prev->vm_flags & VM_LOCKED) {
KOSAKI Motohiroc58267c2010-03-05 13:41:43 -08002012 mlock_vma_pages_range(prev, addr, prev->vm_end);
Rik van Rielba470de2008-10-18 20:26:50 -07002013 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002014 return prev;
2015}
2016#else
2017int expand_stack(struct vm_area_struct *vma, unsigned long address)
2018{
2019 return expand_downwards(vma, address);
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022struct vm_area_struct *
2023find_extend_vma(struct mm_struct * mm, unsigned long addr)
2024{
2025 struct vm_area_struct * vma;
2026 unsigned long start;
2027
2028 addr &= PAGE_MASK;
2029 vma = find_vma(mm,addr);
2030 if (!vma)
2031 return NULL;
2032 if (vma->vm_start <= addr)
2033 return vma;
2034 if (!(vma->vm_flags & VM_GROWSDOWN))
2035 return NULL;
2036 start = vma->vm_start;
2037 if (expand_stack(vma, addr))
2038 return NULL;
Rik van Rielba470de2008-10-18 20:26:50 -07002039 if (vma->vm_flags & VM_LOCKED) {
KOSAKI Motohiroc58267c2010-03-05 13:41:43 -08002040 mlock_vma_pages_range(vma, addr, start);
Rik van Rielba470de2008-10-18 20:26:50 -07002041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return vma;
2043}
2044#endif
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002047 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002049 *
2050 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002052static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002054 unsigned long nr_accounted = 0;
2055
Hugh Dickins365e9c872005-10-29 18:16:18 -07002056 /* Update high watermark before we lower total_vm */
2057 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002059 long nrpages = vma_pages(vma);
2060
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002061 if (vma->vm_flags & VM_ACCOUNT)
2062 nr_accounted += nrpages;
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002063 vm_stat_account(mm, vma->vm_flags, vma->vm_file, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002064 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002065 } while (vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002066 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 validate_mm(mm);
2068}
2069
2070/*
2071 * Get rid of page table information in the indicated region.
2072 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002073 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
2075static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002076 struct vm_area_struct *vma, struct vm_area_struct *prev,
2077 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Hugh Dickinse0da3822005-04-19 13:29:15 -07002079 struct vm_area_struct *next = prev? prev->vm_next: mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002080 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 lru_add_drain();
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002083 tlb_gather_mmu(&tlb, mm, 0);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002084 update_hiwater_rss(mm);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002085 unmap_vmas(&tlb, vma, start, end);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002086 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
2087 next ? next->vm_start : 0);
2088 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091/*
2092 * Create a list of vma's touched by the unmap, removing them from the mm's
2093 * vma list as we go..
2094 */
2095static void
2096detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2097 struct vm_area_struct *prev, unsigned long end)
2098{
2099 struct vm_area_struct **insertion_point;
2100 struct vm_area_struct *tail_vma = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07002101 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002104 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 do {
Michel Lespinassed3737182012-12-11 16:01:38 -08002106 vma_rb_erase(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 mm->map_count--;
2108 tail_vma = vma;
2109 vma = vma->vm_next;
2110 } while (vma && vma->vm_start < end);
2111 *insertion_point = vma;
Michel Lespinassed3737182012-12-11 16:01:38 -08002112 if (vma) {
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002113 vma->vm_prev = prev;
Michel Lespinassed3737182012-12-11 16:01:38 -08002114 vma_gap_update(vma);
2115 } else
2116 mm->highest_vm_end = prev ? prev->vm_end : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 tail_vma->vm_next = NULL;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07002118 if (mm->unmap_area == arch_unmap_area)
2119 addr = prev ? prev->vm_end : mm->mmap_base;
2120 else
2121 addr = vma ? vma->vm_start : mm->mmap_base;
2122 mm->unmap_area(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 mm->mmap_cache = NULL; /* Kill the cache. */
2124}
2125
2126/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002127 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2128 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 */
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002130static int __split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 unsigned long addr, int new_below)
2132{
2133 struct mempolicy *pol;
2134 struct vm_area_struct *new;
Rik van Riel5beb4932010-03-05 13:42:07 -08002135 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Andi Kleena5516432008-07-23 21:27:41 -07002137 if (is_vm_hugetlb_page(vma) && (addr &
2138 ~(huge_page_mask(hstate_vma(vma)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return -EINVAL;
2140
Christoph Lametere94b1762006-12-06 20:33:17 -08002141 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (!new)
Rik van Riel5beb4932010-03-05 13:42:07 -08002143 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* most fields are the same, copy all, and then fixup */
2146 *new = *vma;
2147
Rik van Riel5beb4932010-03-05 13:42:07 -08002148 INIT_LIST_HEAD(&new->anon_vma_chain);
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (new_below)
2151 new->vm_end = addr;
2152 else {
2153 new->vm_start = addr;
2154 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2155 }
2156
Lee Schermerhorn846a16b2008-04-28 02:13:09 -07002157 pol = mpol_dup(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (IS_ERR(pol)) {
Rik van Riel5beb4932010-03-05 13:42:07 -08002159 err = PTR_ERR(pol);
2160 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162 vma_set_policy(new, pol);
2163
Rik van Riel5beb4932010-03-05 13:42:07 -08002164 if (anon_vma_clone(new, vma))
2165 goto out_free_mpol;
2166
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002167 if (new->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 get_file(new->vm_file);
2169
2170 if (new->vm_ops && new->vm_ops->open)
2171 new->vm_ops->open(new);
2172
2173 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002174 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2176 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002177 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Rik van Riel5beb4932010-03-05 13:42:07 -08002179 /* Success. */
2180 if (!err)
2181 return 0;
2182
2183 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002184 if (new->vm_ops && new->vm_ops->close)
2185 new->vm_ops->close(new);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002186 if (new->vm_file)
Rik van Riel5beb4932010-03-05 13:42:07 -08002187 fput(new->vm_file);
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002188 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002189 out_free_mpol:
2190 mpol_put(pol);
2191 out_free_vma:
2192 kmem_cache_free(vm_area_cachep, new);
2193 out_err:
2194 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002197/*
2198 * Split a vma into two pieces at address 'addr', a new vma is allocated
2199 * either for the first part or the tail.
2200 */
2201int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2202 unsigned long addr, int new_below)
2203{
2204 if (mm->map_count >= sysctl_max_map_count)
2205 return -ENOMEM;
2206
2207 return __split_vma(mm, vma, addr, new_below);
2208}
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210/* Munmap is split into 2 main parts -- this part which finds
2211 * what needs doing, and the areas themselves, which do the
2212 * work. This now handles partial unmappings.
2213 * Jeremy Fitzhardinge <jeremy@goop.org>
2214 */
2215int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2216{
2217 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002218 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
2221 return -EINVAL;
2222
2223 if ((len = PAGE_ALIGN(len)) == 0)
2224 return -EINVAL;
2225
2226 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002227 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002228 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002230 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002231 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 /* if it doesn't overlap, we have nothing.. */
2234 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002235 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return 0;
2237
2238 /*
2239 * If we need to split any vma, do it now to save pain later.
2240 *
2241 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2242 * unmapped vm_area_struct will remain in use: so lower split_vma
2243 * places tmp vma above, and higher split_vma places tmp vma below.
2244 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002245 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002246 int error;
2247
2248 /*
2249 * Make sure that map_count on return from munmap() will
2250 * not exceed its limit; but let map_count go just above
2251 * its limit temporarily, to help free resources as expected.
2252 */
2253 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2254 return -ENOMEM;
2255
2256 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (error)
2258 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002259 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
2262 /* Does it split the last one? */
2263 last = find_vma(mm, end);
2264 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002265 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (error)
2267 return error;
2268 }
Hugh Dickins146425a2005-04-19 13:29:18 -07002269 vma = prev? prev->vm_next: mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002272 * unlock any mlock()ed ranges before detaching vmas
2273 */
2274 if (mm->locked_vm) {
2275 struct vm_area_struct *tmp = vma;
2276 while (tmp && tmp->vm_start < end) {
2277 if (tmp->vm_flags & VM_LOCKED) {
2278 mm->locked_vm -= vma_pages(tmp);
2279 munlock_vma_pages_all(tmp);
2280 }
2281 tmp = tmp->vm_next;
2282 }
2283 }
2284
2285 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 * Remove the vma's, and unmap the actual pages
2287 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002288 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2289 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002292 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 return 0;
2295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Al Virobfce2812012-04-20 21:57:04 -04002297int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002298{
2299 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002300 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002301
2302 down_write(&mm->mmap_sem);
2303 ret = do_munmap(mm, start, len);
2304 up_write(&mm->mmap_sem);
2305 return ret;
2306}
2307EXPORT_SYMBOL(vm_munmap);
2308
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002309SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 profile_munmap(addr);
Al Virobfce2812012-04-20 21:57:04 -04002312 return vm_munmap(addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
2315static inline void verify_mm_writelocked(struct mm_struct *mm)
2316{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002317#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2319 WARN_ON(1);
2320 up_read(&mm->mmap_sem);
2321 }
2322#endif
2323}
2324
2325/*
2326 * this is really a simplified "do_mmap". it only handles
2327 * anonymous maps. eventually we may be able to do some
2328 * brk-specific accounting here.
2329 */
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002330static unsigned long do_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 struct mm_struct * mm = current->mm;
2333 struct vm_area_struct * vma, * prev;
2334 unsigned long flags;
2335 struct rb_node ** rb_link, * rb_parent;
2336 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002337 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 len = PAGE_ALIGN(len);
2340 if (!len)
2341 return addr;
2342
Kirill Korotaev3a459752006-09-07 14:17:04 +04002343 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2344
Al Viro2c6a1012009-12-03 19:40:46 -05002345 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
2346 if (error & ~PAGE_MASK)
Kirill Korotaev3a459752006-09-07 14:17:04 +04002347 return error;
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /*
2350 * mlock MCL_FUTURE?
2351 */
2352 if (mm->def_flags & VM_LOCKED) {
2353 unsigned long locked, lock_limit;
Chris Wright93ea1d02005-05-01 08:58:38 -07002354 locked = len >> PAGE_SHIFT;
2355 locked += mm->locked_vm;
Jiri Slaby59e99e52010-03-05 13:41:44 -08002356 lock_limit = rlimit(RLIMIT_MEMLOCK);
Chris Wright93ea1d02005-05-01 08:58:38 -07002357 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
2359 return -EAGAIN;
2360 }
2361
2362 /*
2363 * mm->mmap_sem is required to protect against another thread
2364 * changing the mappings in case we sleep.
2365 */
2366 verify_mm_writelocked(mm);
2367
2368 /*
2369 * Clear old maps. this also does some error checking for us
2370 */
2371 munmap_back:
Hugh Dickins6597d782012-10-08 16:29:07 -07002372 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (do_munmap(mm, addr, len))
2374 return -ENOMEM;
2375 goto munmap_back;
2376 }
2377
2378 /* Check against address space limits *after* clearing old maps... */
akpm@osdl.org119f6572005-05-01 08:58:35 -07002379 if (!may_expand_vm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return -ENOMEM;
2381
2382 if (mm->map_count > sysctl_max_map_count)
2383 return -ENOMEM;
2384
Al Viro191c5422012-02-13 03:58:52 +00002385 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return -ENOMEM;
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07002389 vma = vma_merge(mm, prev, addr, addr + len, flags,
2390 NULL, NULL, pgoff, NULL);
2391 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 goto out;
2393
2394 /*
2395 * create a vma struct for an anonymous mapping
2396 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002397 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (!vma) {
2399 vm_unacct_memory(len >> PAGE_SHIFT);
2400 return -ENOMEM;
2401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Rik van Riel5beb4932010-03-05 13:42:07 -08002403 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 vma->vm_mm = mm;
2405 vma->vm_start = addr;
2406 vma->vm_end = addr + len;
2407 vma->vm_pgoff = pgoff;
2408 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07002409 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 vma_link(mm, vma, prev, rb_link, rb_parent);
2411out:
Eric B Munson3af9e852010-05-18 15:30:49 +01002412 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 mm->total_vm += len >> PAGE_SHIFT;
2414 if (flags & VM_LOCKED) {
Rik van Rielba470de2008-10-18 20:26:50 -07002415 if (!mlock_vma_pages_range(vma, addr, addr + len))
2416 mm->locked_vm += (len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 return addr;
2419}
2420
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002421unsigned long vm_brk(unsigned long addr, unsigned long len)
2422{
2423 struct mm_struct *mm = current->mm;
2424 unsigned long ret;
2425
2426 down_write(&mm->mmap_sem);
2427 ret = do_brk(addr, len);
2428 up_write(&mm->mmap_sem);
2429 return ret;
2430}
2431EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433/* Release all mmaps. */
2434void exit_mmap(struct mm_struct *mm)
2435{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002436 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07002437 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 unsigned long nr_accounted = 0;
2439
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002440 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002441 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002442
Rik van Rielba470de2008-10-18 20:26:50 -07002443 if (mm->locked_vm) {
2444 vma = mm->mmap;
2445 while (vma) {
2446 if (vma->vm_flags & VM_LOCKED)
2447 munlock_vma_pages_all(vma);
2448 vma = vma->vm_next;
2449 }
2450 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002451
2452 arch_exit_mmap(mm);
2453
Rik van Rielba470de2008-10-18 20:26:50 -07002454 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002455 if (!vma) /* Can happen if dup_mmap() received an OOM */
2456 return;
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 flush_cache_mm(mm);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002460 tlb_gather_mmu(&tlb, mm, 1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08002461 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07002462 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002463 unmap_vmas(&tlb, vma, 0, -1);
Hugh Dickins9ba69292009-09-21 17:02:20 -07002464
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002465 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
Al Viro853f5e22012-03-05 14:03:47 -05002466 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07002469 * Walk the list again, actually closing and freeing it,
2470 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002472 while (vma) {
2473 if (vma->vm_flags & VM_ACCOUNT)
2474 nr_accounted += vma_pages(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002475 vma = remove_vma(vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002476 }
2477 vm_unacct_memory(nr_accounted);
Hugh Dickinse0da3822005-04-19 13:29:15 -07002478
Hugh Dickinsf9aed622012-08-21 16:15:45 -07002479 WARN_ON(mm->nr_ptes > (FIRST_USER_ADDRESS+PMD_SIZE-1)>>PMD_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
2482/* Insert vm structure into process list sorted by address
2483 * and into the inode's i_mmap tree. If vm_file is non-NULL
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002484 * then i_mmap_mutex is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 */
Hugh Dickins6597d782012-10-08 16:29:07 -07002486int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Hugh Dickins6597d782012-10-08 16:29:07 -07002488 struct vm_area_struct *prev;
2489 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 /*
2492 * The vm_pgoff of a purely anonymous vma should be irrelevant
2493 * until its first write fault, when page's anon_vma and index
2494 * are set. But now set the vm_pgoff it will almost certainly
2495 * end up with (unless mremap moves it elsewhere before that
2496 * first wfault), so /proc/pid/maps tells a consistent story.
2497 *
2498 * By setting it to reflect the virtual start address of the
2499 * vma, merges and splits can happen in a seamless way, just
2500 * using the existing file pgoff checks and manipulations.
2501 * Similarly in do_mmap_pgoff and in do_brk.
2502 */
2503 if (!vma->vm_file) {
2504 BUG_ON(vma->anon_vma);
2505 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2506 }
Hugh Dickins6597d782012-10-08 16:29:07 -07002507 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
2508 &prev, &rb_link, &rb_parent))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return -ENOMEM;
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002510 if ((vma->vm_flags & VM_ACCOUNT) &&
Alan Cox34b4e4a2007-08-22 14:01:28 -07002511 security_vm_enough_memory_mm(mm, vma_pages(vma)))
Hugh Dickins2fd4ef82005-09-14 06:13:02 +01002512 return -ENOMEM;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 vma_link(mm, vma, prev, rb_link, rb_parent);
2515 return 0;
2516}
2517
2518/*
2519 * Copy the vma structure to a new location in the same mm,
2520 * prior to moving page table entries, to effect an mremap move.
2521 */
2522struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
Michel Lespinasse38a76012012-10-08 16:31:50 -07002523 unsigned long addr, unsigned long len, pgoff_t pgoff,
2524 bool *need_rmap_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 struct vm_area_struct *vma = *vmap;
2527 unsigned long vma_start = vma->vm_start;
2528 struct mm_struct *mm = vma->vm_mm;
2529 struct vm_area_struct *new_vma, *prev;
2530 struct rb_node **rb_link, *rb_parent;
2531 struct mempolicy *pol;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002532 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 /*
2535 * If anonymous vma has not yet been faulted, update new pgoff
2536 * to match new location, to increase its chance of merging.
2537 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002538 if (unlikely(!vma->vm_file && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002540 faulted_in_anon_vma = false;
2541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Hugh Dickins6597d782012-10-08 16:29:07 -07002543 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
2544 return NULL; /* should never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
2546 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2547 if (new_vma) {
2548 /*
2549 * Source vma may have been merged into new_vma
2550 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002551 if (unlikely(vma_start >= new_vma->vm_start &&
2552 vma_start < new_vma->vm_end)) {
2553 /*
2554 * The only way we can get a vma_merge with
2555 * self during an mremap is if the vma hasn't
2556 * been faulted in yet and we were allowed to
2557 * reset the dst vma->vm_pgoff to the
2558 * destination address of the mremap to allow
2559 * the merge to happen. mremap must change the
2560 * vm_pgoff linearity between src and dst vmas
2561 * (in turn preventing a vma_merge) to be
2562 * safe. It is only safe to keep the vm_pgoff
2563 * linear if there are no pages mapped yet.
2564 */
2565 VM_BUG_ON(faulted_in_anon_vma);
Michel Lespinasse38a76012012-10-08 16:31:50 -07002566 *vmap = vma = new_vma;
Michel Lespinasse108d6642012-10-08 16:31:36 -07002567 }
Michel Lespinasse38a76012012-10-08 16:31:50 -07002568 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08002570 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (new_vma) {
2572 *new_vma = *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 new_vma->vm_start = addr;
2574 new_vma->vm_end = addr + len;
2575 new_vma->vm_pgoff = pgoff;
Michel Lespinasse523d4e22012-10-08 16:31:48 -07002576 pol = mpol_dup(vma_policy(vma));
2577 if (IS_ERR(pol))
2578 goto out_free_vma;
2579 vma_set_policy(new_vma, pol);
2580 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2581 if (anon_vma_clone(new_vma, vma))
2582 goto out_free_mempol;
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002583 if (new_vma->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 get_file(new_vma->vm_file);
2585 if (new_vma->vm_ops && new_vma->vm_ops->open)
2586 new_vma->vm_ops->open(new_vma);
2587 vma_link(mm, new_vma, prev, rb_link, rb_parent);
Michel Lespinasse38a76012012-10-08 16:31:50 -07002588 *need_rmap_locks = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 }
2591 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002592
2593 out_free_mempol:
2594 mpol_put(pol);
2595 out_free_vma:
2596 kmem_cache_free(vm_area_cachep, new_vma);
2597 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
akpm@osdl.org119f6572005-05-01 08:58:35 -07002599
2600/*
2601 * Return true if the calling process may expand its vm space by the passed
2602 * number of pages
2603 */
2604int may_expand_vm(struct mm_struct *mm, unsigned long npages)
2605{
2606 unsigned long cur = mm->total_vm; /* pages */
2607 unsigned long lim;
2608
Jiri Slaby59e99e52010-03-05 13:41:44 -08002609 lim = rlimit(RLIMIT_AS) >> PAGE_SHIFT;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002610
2611 if (cur + npages > lim)
2612 return 0;
2613 return 1;
2614}
Roland McGrathfa5dc222007-02-08 14:20:41 -08002615
2616
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002617static int special_mapping_fault(struct vm_area_struct *vma,
2618 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08002619{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002620 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002621 struct page **pages;
2622
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002623 /*
2624 * special mappings have no vm_file, and in that case, the mm
2625 * uses vm_pgoff internally. So we have to subtract it from here.
2626 * We are allowed to do this because we are the mm; do not copy
2627 * this code into drivers!
2628 */
2629 pgoff = vmf->pgoff - vma->vm_pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002630
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002631 for (pages = vma->vm_private_data; pgoff && *pages; ++pages)
2632 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002633
2634 if (*pages) {
2635 struct page *page = *pages;
2636 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002637 vmf->page = page;
2638 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002639 }
2640
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002641 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002642}
2643
2644/*
2645 * Having a close hook prevents vma merging regardless of flags.
2646 */
2647static void special_mapping_close(struct vm_area_struct *vma)
2648{
2649}
2650
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002651static const struct vm_operations_struct special_mapping_vmops = {
Roland McGrathfa5dc222007-02-08 14:20:41 -08002652 .close = special_mapping_close,
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002653 .fault = special_mapping_fault,
Roland McGrathfa5dc222007-02-08 14:20:41 -08002654};
2655
2656/*
2657 * Called with mm->mmap_sem held for writing.
2658 * Insert a new vma covering the given region, with the given flags.
2659 * Its pages are supplied by the given array of struct page *.
2660 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
2661 * The region past the last page supplied will always produce SIGBUS.
2662 * The array pointer and the pages it points to are assumed to stay alive
2663 * for as long as this mapping might exist.
2664 */
2665int install_special_mapping(struct mm_struct *mm,
2666 unsigned long addr, unsigned long len,
2667 unsigned long vm_flags, struct page **pages)
2668{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002669 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002670 struct vm_area_struct *vma;
2671
2672 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
2673 if (unlikely(vma == NULL))
2674 return -ENOMEM;
2675
Rik van Riel5beb4932010-03-05 13:42:07 -08002676 INIT_LIST_HEAD(&vma->anon_vma_chain);
Roland McGrathfa5dc222007-02-08 14:20:41 -08002677 vma->vm_mm = mm;
2678 vma->vm_start = addr;
2679 vma->vm_end = addr + len;
2680
Nick Piggin2f987352008-02-02 03:08:53 +01002681 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND;
Coly Li3ed75eb2007-10-18 23:39:15 -07002682 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08002683
2684 vma->vm_ops = &special_mapping_vmops;
2685 vma->vm_private_data = pages;
2686
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002687 ret = insert_vm_struct(mm, vma);
2688 if (ret)
2689 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002690
2691 mm->total_vm += len >> PAGE_SHIFT;
2692
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002693 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02002694
Roland McGrathfa5dc222007-02-08 14:20:41 -08002695 return 0;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01002696
2697out:
2698 kmem_cache_free(vm_area_cachep, vma);
2699 return ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002700}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002701
2702static DEFINE_MUTEX(mm_all_locks_mutex);
2703
Peter Zijlstra454ed842008-08-11 09:30:25 +02002704static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002705{
Michel Lespinassebf181b92012-10-08 16:31:39 -07002706 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002707 /*
2708 * The LSB of head.next can't change from under us
2709 * because we hold the mm_all_locks_mutex.
2710 */
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002711 mutex_lock_nest_lock(&anon_vma->root->mutex, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002712 /*
2713 * We can safely modify head.next after taking the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002714 * anon_vma->root->mutex. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002715 * the same anon_vma we won't take it again.
2716 *
2717 * No need of atomic instructions here, head.next
2718 * can't change from under us thanks to the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002719 * anon_vma->root->mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002720 */
2721 if (__test_and_set_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07002722 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002723 BUG();
2724 }
2725}
2726
Peter Zijlstra454ed842008-08-11 09:30:25 +02002727static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002728{
2729 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2730 /*
2731 * AS_MM_ALL_LOCKS can't change from under us because
2732 * we hold the mm_all_locks_mutex.
2733 *
2734 * Operations on ->flags have to be atomic because
2735 * even if AS_MM_ALL_LOCKS is stable thanks to the
2736 * mm_all_locks_mutex, there may be other cpus
2737 * changing other bitflags in parallel to us.
2738 */
2739 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
2740 BUG();
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002741 mutex_lock_nest_lock(&mapping->i_mmap_mutex, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002742 }
2743}
2744
2745/*
2746 * This operation locks against the VM for all pte/vma/mm related
2747 * operations that could ever happen on a certain mm. This includes
2748 * vmtruncate, try_to_unmap, and all page faults.
2749 *
2750 * The caller must take the mmap_sem in write mode before calling
2751 * mm_take_all_locks(). The caller isn't allowed to release the
2752 * mmap_sem until mm_drop_all_locks() returns.
2753 *
2754 * mmap_sem in write mode is required in order to block all operations
2755 * that could modify pagetables and free pages without need of
2756 * altering the vma layout (for example populate_range() with
2757 * nonlinear vmas). It's also needed in write mode to avoid new
2758 * anon_vmas to be associated with existing vmas.
2759 *
2760 * A single task can't take more than one mm_take_all_locks() in a row
2761 * or it would deadlock.
2762 *
Michel Lespinassebf181b92012-10-08 16:31:39 -07002763 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002764 * mapping->flags avoid to take the same lock twice, if more than one
2765 * vma in this mm is backed by the same anon_vma or address_space.
2766 *
2767 * We can take all the locks in random order because the VM code
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002768 * taking i_mmap_mutex or anon_vma->mutex outside the mmap_sem never
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002769 * takes more than one of them in a row. Secondly we're protected
2770 * against a concurrent mm_take_all_locks() by the mm_all_locks_mutex.
2771 *
2772 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
2773 * that may have to take thousand of locks.
2774 *
2775 * mm_take_all_locks() can fail if it's interrupted by signals.
2776 */
2777int mm_take_all_locks(struct mm_struct *mm)
2778{
2779 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002780 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002781
2782 BUG_ON(down_read_trylock(&mm->mmap_sem));
2783
2784 mutex_lock(&mm_all_locks_mutex);
2785
2786 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2787 if (signal_pending(current))
2788 goto out_unlock;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002789 if (vma->vm_file && vma->vm_file->f_mapping)
Peter Zijlstra454ed842008-08-11 09:30:25 +02002790 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002791 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02002792
2793 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2794 if (signal_pending(current))
2795 goto out_unlock;
2796 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08002797 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2798 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02002799 }
2800
Kautuk Consul584cff52011-10-31 17:08:59 -07002801 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002802
2803out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07002804 mm_drop_all_locks(mm);
2805 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002806}
2807
2808static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
2809{
Michel Lespinassebf181b92012-10-08 16:31:39 -07002810 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002811 /*
2812 * The LSB of head.next can't change to 0 from under
2813 * us because we hold the mm_all_locks_mutex.
2814 *
2815 * We must however clear the bitflag before unlocking
Michel Lespinassebf181b92012-10-08 16:31:39 -07002816 * the vma so the users using the anon_vma->rb_root will
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002817 * never see our bitflag.
2818 *
2819 * No need of atomic instructions here, head.next
2820 * can't change from under us until we release the
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07002821 * anon_vma->root->mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002822 */
2823 if (!__test_and_clear_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07002824 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002825 BUG();
Rik van Rielcba48b92010-08-09 17:18:38 -07002826 anon_vma_unlock(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002827 }
2828}
2829
2830static void vm_unlock_mapping(struct address_space *mapping)
2831{
2832 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
2833 /*
2834 * AS_MM_ALL_LOCKS can't change to 0 from under us
2835 * because we hold the mm_all_locks_mutex.
2836 */
Peter Zijlstra3d48ae42011-05-24 17:12:06 -07002837 mutex_unlock(&mapping->i_mmap_mutex);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002838 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
2839 &mapping->flags))
2840 BUG();
2841 }
2842}
2843
2844/*
2845 * The mmap_sem cannot be released by the caller until
2846 * mm_drop_all_locks() returns.
2847 */
2848void mm_drop_all_locks(struct mm_struct *mm)
2849{
2850 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002851 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002852
2853 BUG_ON(down_read_trylock(&mm->mmap_sem));
2854 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
2855
2856 for (vma = mm->mmap; vma; vma = vma->vm_next) {
2857 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08002858 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
2859 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07002860 if (vma->vm_file && vma->vm_file->f_mapping)
2861 vm_unlock_mapping(vma->vm_file->f_mapping);
2862 }
2863
2864 mutex_unlock(&mm_all_locks_mutex);
2865}
David Howells8feae132009-01-08 12:04:47 +00002866
2867/*
2868 * initialise the VMA slab
2869 */
2870void __init mmap_init(void)
2871{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07002872 int ret;
2873
2874 ret = percpu_counter_init(&vm_committed_as, 0);
2875 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00002876}