blob: 7109f886e739ea4764884d0ffe68be69570522fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/mmap.c
3 *
4 * Written by obz.
5 *
Alan Cox046c6882009-01-05 14:06:29 +00006 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Cyril Hrubise8420a82013-04-29 15:08:33 -070011#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070013#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070015#include <linux/vmacache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/shm.h>
17#include <linux/mman.h>
18#include <linux/pagemap.h>
19#include <linux/swap.h>
20#include <linux/syscalls.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/file.h>
24#include <linux/fs.h>
25#include <linux/personality.h>
26#include <linux/security.h>
27#include <linux/hugetlb.h>
Hugh Dickinsc01d5b32016-07-26 15:26:15 -070028#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/profile.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040030#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/mount.h>
32#include <linux/mempolicy.h>
33#include <linux/rmap.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070034#include <linux/mmu_notifier.h>
Konstantin Khlebnikov82f71ae2014-08-06 16:06:36 -070035#include <linux/mmdebug.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020036#include <linux/perf_event.h>
Al Viro120a7952010-10-30 02:54:44 -040037#include <linux/audit.h>
Andrea Arcangelib15d00b2011-01-13 15:46:59 -080038#include <linux/khugepaged.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053039#include <linux/uprobes.h>
Michel Lespinassed3737182012-12-11 16:01:38 -080040#include <linux/rbtree_augmented.h>
Andrew Shewmaker16408792013-04-29 15:08:12 -070041#include <linux/notifier.h>
42#include <linux/memory.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070043#include <linux/printk.h>
Andrea Arcangeli19a809a2015-09-04 15:46:24 -070044#include <linux/userfaultfd_k.h>
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -080045#include <linux/moduleparam.h>
Dave Hansen62b5f7d2016-02-12 13:02:40 -080046#include <linux/pkeys.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/uaccess.h>
49#include <asm/cacheflush.h>
50#include <asm/tlb.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020051#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jan Beulich42b77722008-07-23 21:27:10 -070053#include "internal.h"
54
Kirill Korotaev3a459752006-09-07 14:17:04 +040055#ifndef arch_mmap_check
56#define arch_mmap_check(addr, len, flags) (0)
57#endif
58
Daniel Cashmand07e2252016-01-14 15:19:53 -080059#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
60const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
61const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
62int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
63#endif
64#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
65const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
66const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
67int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
68#endif
69
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -070070static bool ignore_rlimit_data;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -080071core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644);
Daniel Cashmand07e2252016-01-14 15:19:53 -080072
Hugh Dickinse0da3822005-04-19 13:29:15 -070073static void unmap_region(struct mm_struct *mm,
74 struct vm_area_struct *vma, struct vm_area_struct *prev,
75 unsigned long start, unsigned long end);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* description of effects of mapping type and prot in current implementation.
78 * this is due to the limited x86 page protection hardware. The expected
79 * behavior is in parens:
80 *
81 * map_type prot
82 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
83 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
84 * w: (no) no w: (no) no w: (yes) yes w: (no) no
85 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
vishnu.pscc71aba2014-10-09 15:26:29 -070086 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
88 * w: (no) no w: (no) no w: (copy) copy w: (no) no
89 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91pgprot_t protection_map[16] = {
92 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
93 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
94};
95
Hugh Dickins804af2c2006-07-26 21:39:49 +010096pgprot_t vm_get_page_prot(unsigned long vm_flags)
97{
Dave Kleikampb845f312008-07-08 00:28:51 +100098 return __pgprot(pgprot_val(protection_map[vm_flags &
99 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
100 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +0100101}
102EXPORT_SYMBOL(vm_get_page_prot);
103
Peter Feiner64e45502014-10-13 15:55:46 -0700104static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags)
105{
106 return pgprot_modify(oldprot, vm_get_page_prot(vm_flags));
107}
108
109/* Update vma->vm_page_prot to reflect vma->vm_flags. */
110void vma_set_page_prot(struct vm_area_struct *vma)
111{
112 unsigned long vm_flags = vma->vm_flags;
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700113 pgprot_t vm_page_prot;
Peter Feiner64e45502014-10-13 15:55:46 -0700114
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700115 vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags);
116 if (vma_wants_writenotify(vma, vm_page_prot)) {
Peter Feiner64e45502014-10-13 15:55:46 -0700117 vm_flags &= ~VM_SHARED;
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700118 vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags);
Peter Feiner64e45502014-10-13 15:55:46 -0700119 }
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700120 /* remove_protection_ptes reads vma->vm_page_prot without mmap_sem */
121 WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
Peter Feiner64e45502014-10-13 15:55:46 -0700122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800125 * Requires inode->i_mapping->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127static void __remove_shared_vm_struct(struct vm_area_struct *vma,
128 struct file *file, struct address_space *mapping)
129{
130 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500131 atomic_inc(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700133 mapping_unmap_writable(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 flush_dcache_mmap_lock(mapping);
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800136 vma_interval_tree_remove(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 flush_dcache_mmap_unlock(mapping);
138}
139
140/*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700141 * Unlink a file-based vm structure from its interval tree, to hide
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700142 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700144void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct file *file = vma->vm_file;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (file) {
149 struct address_space *mapping = file->f_mapping;
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800150 i_mmap_lock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 __remove_shared_vm_struct(vma, file, mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800152 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700154}
155
156/*
157 * Close a vm structure and free it, returning the next.
158 */
159static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
160{
161 struct vm_area_struct *next = vma->vm_next;
162
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700163 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (vma->vm_ops && vma->vm_ops->close)
165 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700166 if (vma->vm_file)
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700167 fput(vma->vm_file);
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700168 mpol_put(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 kmem_cache_free(vm_area_cachep, vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700170 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Linus Torvalds5d22fc22016-05-27 15:57:31 -0700173static int do_brk(unsigned long addr, unsigned long len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -0700174
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100175SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700177 unsigned long retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned long newbrk, oldbrk;
179 struct mm_struct *mm = current->mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700180 struct vm_area_struct *next;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700181 unsigned long min_brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800182 bool populate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700184 if (down_write_killable(&mm->mmap_sem))
185 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700187#ifdef CONFIG_COMPAT_BRK
Jiri Kosina5520e892011-01-13 15:47:23 -0800188 /*
189 * CONFIG_COMPAT_BRK can still be overridden by setting
190 * randomize_va_space to 2, which will still cause mm->start_brk
191 * to be arbitrarily shifted
192 */
Jiri Kosina4471a672011-04-14 15:22:09 -0700193 if (current->brk_randomized)
Jiri Kosina5520e892011-01-13 15:47:23 -0800194 min_brk = mm->start_brk;
195 else
196 min_brk = mm->end_data;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700197#else
198 min_brk = mm->start_brk;
199#endif
200 if (brk < min_brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700202
203 /*
204 * Check against rlimit here. If this check is done later after the test
205 * of oldbrk with newbrk then it can escape the test and let the data
206 * segment grow beyond its set limit the in case where the limit is
207 * not page aligned -Ram Gupta
208 */
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700209 if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk,
210 mm->end_data, mm->start_data))
Ram Gupta1e624192006-04-10 22:52:57 -0700211 goto out;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 newbrk = PAGE_ALIGN(brk);
214 oldbrk = PAGE_ALIGN(mm->brk);
215 if (oldbrk == newbrk)
216 goto set_brk;
217
218 /* Always allow shrinking brk. */
219 if (brk <= mm->brk) {
220 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
221 goto set_brk;
222 goto out;
223 }
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* Check against existing mmap mappings. */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700226 next = find_vma(mm, oldbrk);
227 if (next && newbrk + PAGE_SIZE > vm_start_gap(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto out;
229
230 /* Ok, looks good - let it rip. */
Linus Torvalds5d22fc22016-05-27 15:57:31 -0700231 if (do_brk(oldbrk, newbrk-oldbrk) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto out;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234set_brk:
235 mm->brk = brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800236 populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0;
237 up_write(&mm->mmap_sem);
238 if (populate)
239 mm_populate(oldbrk, newbrk - oldbrk);
240 return brk;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242out:
243 retval = mm->brk;
244 up_write(&mm->mmap_sem);
245 return retval;
246}
247
Michel Lespinassed3737182012-12-11 16:01:38 -0800248static long vma_compute_subtree_gap(struct vm_area_struct *vma)
249{
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700250 unsigned long max, prev_end, subtree_gap;
251
252 /*
253 * Note: in the rare case of a VM_GROWSDOWN above a VM_GROWSUP, we
254 * allow two stack_guard_gaps between them here, and when choosing
255 * an unmapped area; whereas when expanding we only require one.
256 * That's a little inconsistent, but keeps the code here simpler.
257 */
258 max = vm_start_gap(vma);
259 if (vma->vm_prev) {
260 prev_end = vm_end_gap(vma->vm_prev);
261 if (max > prev_end)
262 max -= prev_end;
263 else
264 max = 0;
265 }
Michel Lespinassed3737182012-12-11 16:01:38 -0800266 if (vma->vm_rb.rb_left) {
267 subtree_gap = rb_entry(vma->vm_rb.rb_left,
268 struct vm_area_struct, vm_rb)->rb_subtree_gap;
269 if (subtree_gap > max)
270 max = subtree_gap;
271 }
272 if (vma->vm_rb.rb_right) {
273 subtree_gap = rb_entry(vma->vm_rb.rb_right,
274 struct vm_area_struct, vm_rb)->rb_subtree_gap;
275 if (subtree_gap > max)
276 max = subtree_gap;
277 }
278 return max;
279}
280
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700281#ifdef CONFIG_DEBUG_VM_RB
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800282static int browse_rb(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800284 struct rb_root *root = &mm->mm_rb;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800285 int i = 0, j, bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct rb_node *nd, *pn = NULL;
287 unsigned long prev = 0, pend = 0;
288
289 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
290 struct vm_area_struct *vma;
291 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800292 if (vma->vm_start < prev) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700293 pr_emerg("vm_start %lx < prev %lx\n",
294 vma->vm_start, prev);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800295 bug = 1;
296 }
297 if (vma->vm_start < pend) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700298 pr_emerg("vm_start %lx < pend %lx\n",
299 vma->vm_start, pend);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800300 bug = 1;
301 }
302 if (vma->vm_start > vma->vm_end) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700303 pr_emerg("vm_start %lx > vm_end %lx\n",
304 vma->vm_start, vma->vm_end);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800305 bug = 1;
306 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800307 spin_lock(&mm->page_table_lock);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800308 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700309 pr_emerg("free gap %lx, correct %lx\n",
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800310 vma->rb_subtree_gap,
311 vma_compute_subtree_gap(vma));
312 bug = 1;
313 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800314 spin_unlock(&mm->page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 i++;
316 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800317 prev = vma->vm_start;
318 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 j = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800321 for (nd = pn; nd; nd = rb_prev(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 j++;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800323 if (i != j) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700324 pr_emerg("backwards %d, forwards %d\n", j, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800325 bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800327 return bug ? -1 : i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Michel Lespinassed3737182012-12-11 16:01:38 -0800330static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
331{
332 struct rb_node *nd;
333
334 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
335 struct vm_area_struct *vma;
336 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Sasha Levin96dad672014-10-09 15:28:39 -0700337 VM_BUG_ON_VMA(vma != ignore &&
338 vma->rb_subtree_gap != vma_compute_subtree_gap(vma),
339 vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Rashika Kheriaeafd4dc2014-04-03 14:48:03 -0700343static void validate_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 int bug = 0;
346 int i = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800347 unsigned long highest_address = 0;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700348 struct vm_area_struct *vma = mm->mmap;
Andrew Mortonff26f702014-10-09 15:28:19 -0700349
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700350 while (vma) {
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -0800351 struct anon_vma *anon_vma = vma->anon_vma;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700352 struct anon_vma_chain *avc;
Andrew Mortonff26f702014-10-09 15:28:19 -0700353
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -0800354 if (anon_vma) {
355 anon_vma_lock_read(anon_vma);
356 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
357 anon_vma_interval_tree_verify(avc);
358 anon_vma_unlock_read(anon_vma);
359 }
360
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700361 highest_address = vm_end_gap(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700362 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 i++;
364 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800365 if (i != mm->map_count) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700366 pr_emerg("map_count %d vm_next %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800367 bug = 1;
368 }
369 if (highest_address != mm->highest_vm_end) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700370 pr_emerg("mm->highest_vm_end %lx, found %lx\n",
Andrew Mortonff26f702014-10-09 15:28:19 -0700371 mm->highest_vm_end, highest_address);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800372 bug = 1;
373 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800374 i = browse_rb(mm);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800375 if (i != mm->map_count) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700376 if (i != -1)
377 pr_emerg("map_count %d rb %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800378 bug = 1;
379 }
Sasha Levin96dad672014-10-09 15:28:39 -0700380 VM_BUG_ON_MM(bug, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382#else
Michel Lespinassed3737182012-12-11 16:01:38 -0800383#define validate_mm_rb(root, ignore) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#define validate_mm(mm) do { } while (0)
385#endif
386
Michel Lespinassed3737182012-12-11 16:01:38 -0800387RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
388 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
389
390/*
391 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
392 * vma->vm_prev->vm_end values changed, without modifying the vma's position
393 * in the rbtree.
394 */
395static void vma_gap_update(struct vm_area_struct *vma)
396{
397 /*
398 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
399 * function that does exacltly what we want.
400 */
401 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
402}
403
404static inline void vma_rb_insert(struct vm_area_struct *vma,
405 struct rb_root *root)
406{
407 /* All rb_subtree_gap values must be consistent prior to insertion */
408 validate_mm_rb(root, NULL);
409
410 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
411}
412
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700413static void __vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
414{
415 /*
416 * Note rb_erase_augmented is a fairly large inline function,
417 * so make sure we instantiate it only once with our desired
418 * augmented rbtree callbacks.
419 */
420 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
421}
422
423static __always_inline void vma_rb_erase_ignore(struct vm_area_struct *vma,
424 struct rb_root *root,
425 struct vm_area_struct *ignore)
426{
427 /*
428 * All rb_subtree_gap values must be consistent prior to erase,
429 * with the possible exception of the "next" vma being erased if
430 * next->vm_start was reduced.
431 */
432 validate_mm_rb(root, ignore);
433
434 __vma_rb_erase(vma, root);
435}
436
437static __always_inline void vma_rb_erase(struct vm_area_struct *vma,
438 struct rb_root *root)
Michel Lespinassed3737182012-12-11 16:01:38 -0800439{
440 /*
441 * All rb_subtree_gap values must be consistent prior to erase,
442 * with the possible exception of the vma being erased.
443 */
444 validate_mm_rb(root, vma);
445
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700446 __vma_rb_erase(vma, root);
Michel Lespinassed3737182012-12-11 16:01:38 -0800447}
448
Michel Lespinassebf181b92012-10-08 16:31:39 -0700449/*
450 * vma has some anon_vma assigned, and is already inserted on that
451 * anon_vma's interval trees.
452 *
453 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
454 * vma must be removed from the anon_vma's interval trees using
455 * anon_vma_interval_tree_pre_update_vma().
456 *
457 * After the update, the vma will be reinserted using
458 * anon_vma_interval_tree_post_update_vma().
459 *
460 * The entire update must be protected by exclusive mmap_sem and by
461 * the root anon_vma's mutex.
462 */
463static inline void
464anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
465{
466 struct anon_vma_chain *avc;
467
468 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
469 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
470}
471
472static inline void
473anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
474{
475 struct anon_vma_chain *avc;
476
477 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
478 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
479}
480
Hugh Dickins6597d782012-10-08 16:29:07 -0700481static int find_vma_links(struct mm_struct *mm, unsigned long addr,
482 unsigned long end, struct vm_area_struct **pprev,
483 struct rb_node ***rb_link, struct rb_node **rb_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Hugh Dickins6597d782012-10-08 16:29:07 -0700485 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 __rb_link = &mm->mm_rb.rb_node;
488 rb_prev = __rb_parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 while (*__rb_link) {
491 struct vm_area_struct *vma_tmp;
492
493 __rb_parent = *__rb_link;
494 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
495
496 if (vma_tmp->vm_end > addr) {
Hugh Dickins6597d782012-10-08 16:29:07 -0700497 /* Fail if an existing vma overlaps the area */
498 if (vma_tmp->vm_start < end)
499 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 __rb_link = &__rb_parent->rb_left;
501 } else {
502 rb_prev = __rb_parent;
503 __rb_link = &__rb_parent->rb_right;
504 }
505 }
506
507 *pprev = NULL;
508 if (rb_prev)
509 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
510 *rb_link = __rb_link;
511 *rb_parent = __rb_parent;
Hugh Dickins6597d782012-10-08 16:29:07 -0700512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Cyril Hrubise8420a82013-04-29 15:08:33 -0700515static unsigned long count_vma_pages_range(struct mm_struct *mm,
516 unsigned long addr, unsigned long end)
517{
518 unsigned long nr_pages = 0;
519 struct vm_area_struct *vma;
520
521 /* Find first overlaping mapping */
522 vma = find_vma_intersection(mm, addr, end);
523 if (!vma)
524 return 0;
525
526 nr_pages = (min(end, vma->vm_end) -
527 max(addr, vma->vm_start)) >> PAGE_SHIFT;
528
529 /* Iterate over the rest of the overlaps */
530 for (vma = vma->vm_next; vma; vma = vma->vm_next) {
531 unsigned long overlap_len;
532
533 if (vma->vm_start > end)
534 break;
535
536 overlap_len = min(end, vma->vm_end) - vma->vm_start;
537 nr_pages += overlap_len >> PAGE_SHIFT;
538 }
539
540 return nr_pages;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
544 struct rb_node **rb_link, struct rb_node *rb_parent)
545{
Michel Lespinassed3737182012-12-11 16:01:38 -0800546 /* Update tracking information for the gap following the new vma. */
547 if (vma->vm_next)
548 vma_gap_update(vma->vm_next);
549 else
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700550 mm->highest_vm_end = vm_end_gap(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800551
552 /*
553 * vma->vm_prev wasn't known when we followed the rbtree to find the
554 * correct insertion point for that vma. As a result, we could not
555 * update the vma vm_rb parents rb_subtree_gap values on the way down.
556 * So, we first insert the vma with a zero rb_subtree_gap value
557 * (to be consistent with what we did on the way down), and then
558 * immediately update the gap to the correct value. Finally we
559 * rebalance the rbtree after all augmented values have been set.
560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
Michel Lespinassed3737182012-12-11 16:01:38 -0800562 vma->rb_subtree_gap = 0;
563 vma_gap_update(vma);
564 vma_rb_insert(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700567static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
ZhenwenXu48aae422009-01-06 14:40:21 -0800569 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 file = vma->vm_file;
572 if (file) {
573 struct address_space *mapping = file->f_mapping;
574
575 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500576 atomic_dec(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700578 atomic_inc(&mapping->i_mmap_writable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 flush_dcache_mmap_lock(mapping);
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800581 vma_interval_tree_insert(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 flush_dcache_mmap_unlock(mapping);
583 }
584}
585
586static void
587__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
588 struct vm_area_struct *prev, struct rb_node **rb_link,
589 struct rb_node *rb_parent)
590{
591 __vma_link_list(mm, vma, prev, rb_parent);
592 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
596 struct vm_area_struct *prev, struct rb_node **rb_link,
597 struct rb_node *rb_parent)
598{
599 struct address_space *mapping = NULL;
600
Huang Shijie64ac4942014-06-04 16:07:33 -0700601 if (vma->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 mapping = vma->vm_file->f_mapping;
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800603 i_mmap_lock_write(mapping);
Huang Shijie64ac4942014-06-04 16:07:33 -0700604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 __vma_link(mm, vma, prev, rb_link, rb_parent);
607 __vma_link_file(vma);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (mapping)
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800610 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 mm->map_count++;
613 validate_mm(mm);
614}
615
616/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700617 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700618 * mm's list and rbtree. It has already been inserted into the interval tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800620static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Hugh Dickins6597d782012-10-08 16:29:07 -0700622 struct vm_area_struct *prev;
ZhenwenXu48aae422009-01-06 14:40:21 -0800623 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Hugh Dickins6597d782012-10-08 16:29:07 -0700625 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
626 &prev, &rb_link, &rb_parent))
627 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 __vma_link(mm, vma, prev, rb_link, rb_parent);
629 mm->map_count++;
630}
631
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700632static __always_inline void __vma_unlink_common(struct mm_struct *mm,
633 struct vm_area_struct *vma,
634 struct vm_area_struct *prev,
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700635 bool has_prev,
636 struct vm_area_struct *ignore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Michel Lespinassed3737182012-12-11 16:01:38 -0800638 struct vm_area_struct *next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700639
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700640 vma_rb_erase_ignore(vma, &mm->mm_rb, ignore);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700641 next = vma->vm_next;
642 if (has_prev)
643 prev->vm_next = next;
644 else {
645 prev = vma->vm_prev;
646 if (prev)
647 prev->vm_next = next;
648 else
649 mm->mmap = next;
650 }
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700651 if (next)
652 next->vm_prev = prev;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700653
654 /* Kill the cache */
655 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700658static inline void __vma_unlink_prev(struct mm_struct *mm,
659 struct vm_area_struct *vma,
660 struct vm_area_struct *prev)
661{
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700662 __vma_unlink_common(mm, vma, prev, true, vma);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
666 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
667 * is already present in an i_mmap tree without adjusting the tree.
668 * The following helper function should be used when such adjustments
669 * are necessary. The "insert" vma (if any) is to be inserted
670 * before we drop the necessary locks.
671 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700672int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
673 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
674 struct vm_area_struct *expand)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700677 struct vm_area_struct *next = vma->vm_next, *orig_vma = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct address_space *mapping = NULL;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700679 struct rb_root *root = NULL;
Rik van Riel012f18002010-08-09 17:18:40 -0700680 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct file *file = vma->vm_file;
Michel Lespinassed3737182012-12-11 16:01:38 -0800682 bool start_changed = false, end_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 long adjust_next = 0;
684 int remove_next = 0;
685
686 if (next && !insert) {
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700687 struct vm_area_struct *exporter = NULL, *importer = NULL;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (end >= next->vm_end) {
690 /*
691 * vma expands, overlapping all the next, and
692 * perhaps the one after too (mprotect case 6).
Andrea Arcangeli86d12e42016-10-07 17:01:34 -0700693 * The only other cases that gets here are
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700694 * case 1, case 7 and case 8.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700696 if (next == expand) {
697 /*
698 * The only case where we don't expand "vma"
699 * and we expand "next" instead is case 8.
700 */
701 VM_WARN_ON(end != next->vm_end);
702 /*
703 * remove_next == 3 means we're
704 * removing "vma" and that to do so we
705 * swapped "vma" and "next".
706 */
707 remove_next = 3;
708 VM_WARN_ON(file != next->vm_file);
709 swap(vma, next);
710 } else {
711 VM_WARN_ON(expand != vma);
712 /*
713 * case 1, 6, 7, remove_next == 2 is case 6,
714 * remove_next == 1 is case 1 or 7.
715 */
716 remove_next = 1 + (end > next->vm_end);
717 VM_WARN_ON(remove_next == 2 &&
718 end != next->vm_next->vm_end);
719 VM_WARN_ON(remove_next == 1 &&
720 end != next->vm_end);
721 /* trim end to next, for case 6 first pass */
722 end = next->vm_end;
723 }
724
Linus Torvalds287d97a2010-04-10 15:22:30 -0700725 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 importer = vma;
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700727
728 /*
729 * If next doesn't have anon_vma, import from vma after
730 * next, if the vma overlaps with it.
731 */
Andrea Arcangeli97a42cd2016-10-07 17:01:31 -0700732 if (remove_next == 2 && !next->anon_vma)
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700733 exporter = next->vm_next;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 } else if (end > next->vm_start) {
736 /*
737 * vma expands, overlapping part of the next:
738 * mprotect case 5 shifting the boundary up.
739 */
740 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700741 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 importer = vma;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700743 VM_WARN_ON(expand != importer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 } else if (end < vma->vm_end) {
745 /*
746 * vma shrinks, and !insert tells it's not
747 * split_vma inserting another: so it must be
748 * mprotect case 4 shifting the boundary down.
749 */
vishnu.pscc71aba2014-10-09 15:26:29 -0700750 adjust_next = -((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700751 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 importer = next;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700753 VM_WARN_ON(expand != importer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Rik van Riel5beb4932010-03-05 13:42:07 -0800756 /*
757 * Easily overlooked: when mprotect shifts the boundary,
758 * make sure the expanding vma has anon_vma set if the
759 * shrinking vma had, to cover any anon pages imported.
760 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700761 if (exporter && exporter->anon_vma && !importer->anon_vma) {
Daniel Forrestc4ea95d2014-12-02 15:59:42 -0800762 int error;
763
Linus Torvalds287d97a2010-04-10 15:22:30 -0700764 importer->anon_vma = exporter->anon_vma;
Konstantin Khlebnikovb800c912015-01-11 16:54:06 +0300765 error = anon_vma_clone(importer, exporter);
Leon Yu3fe89b32015-03-25 15:55:11 -0700766 if (error)
Konstantin Khlebnikovb800c912015-01-11 16:54:06 +0300767 return error;
Rik van Riel5beb4932010-03-05 13:42:07 -0800768 }
769 }
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700770again:
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700771 vma_adjust_trans_huge(orig_vma, start, end, adjust_next);
Kirill A. Shutemov37f9f552016-07-26 15:25:48 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (file) {
774 mapping = file->f_mapping;
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800775 root = &mapping->i_mmap;
776 uprobe_munmap(vma, vma->vm_start, vma->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530777
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800778 if (adjust_next)
779 uprobe_munmap(next, next->vm_start, next->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530780
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800781 i_mmap_lock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700784 * Put into interval tree now, so instantiated pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * are visible to arm/parisc __flush_dcache_page
786 * throughout; but we cannot insert into address
787 * space until vma start or end is updated.
788 */
789 __vma_link_file(insert);
790 }
791 }
792
Michel Lespinassebf181b92012-10-08 16:31:39 -0700793 anon_vma = vma->anon_vma;
794 if (!anon_vma && adjust_next)
795 anon_vma = next->anon_vma;
796 if (anon_vma) {
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700797 VM_WARN_ON(adjust_next && next->anon_vma &&
798 anon_vma != next->anon_vma);
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000799 anon_vma_lock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700800 anon_vma_interval_tree_pre_update_vma(vma);
801 if (adjust_next)
802 anon_vma_interval_tree_pre_update_vma(next);
803 }
Rik van Riel012f18002010-08-09 17:18:40 -0700804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (root) {
806 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700807 vma_interval_tree_remove(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700809 vma_interval_tree_remove(next, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
Michel Lespinassed3737182012-12-11 16:01:38 -0800812 if (start != vma->vm_start) {
813 vma->vm_start = start;
814 start_changed = true;
815 }
816 if (end != vma->vm_end) {
817 vma->vm_end = end;
818 end_changed = true;
819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 vma->vm_pgoff = pgoff;
821 if (adjust_next) {
822 next->vm_start += adjust_next << PAGE_SHIFT;
823 next->vm_pgoff += adjust_next;
824 }
825
826 if (root) {
827 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700828 vma_interval_tree_insert(next, root);
829 vma_interval_tree_insert(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 flush_dcache_mmap_unlock(mapping);
831 }
832
833 if (remove_next) {
834 /*
835 * vma_merge has merged next into vma, and needs
836 * us to remove next before dropping the locks.
837 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700838 if (remove_next != 3)
839 __vma_unlink_prev(mm, next, vma);
840 else
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700841 /*
842 * vma is not before next if they've been
843 * swapped.
844 *
845 * pre-swap() next->vm_start was reduced so
846 * tell validate_mm_rb to ignore pre-swap()
847 * "next" (which is stored in post-swap()
848 * "vma").
849 */
850 __vma_unlink_common(mm, next, NULL, false, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (file)
852 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } else if (insert) {
854 /*
855 * split_vma has split insert from vma, and needs
856 * us to insert it before dropping the locks
857 * (it may either follow vma or precede it).
858 */
859 __insert_vm_struct(mm, insert);
Michel Lespinassed3737182012-12-11 16:01:38 -0800860 } else {
861 if (start_changed)
862 vma_gap_update(vma);
863 if (end_changed) {
864 if (!next)
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700865 mm->highest_vm_end = vm_end_gap(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800866 else if (!adjust_next)
867 vma_gap_update(next);
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
Michel Lespinassebf181b92012-10-08 16:31:39 -0700871 if (anon_vma) {
872 anon_vma_interval_tree_post_update_vma(vma);
873 if (adjust_next)
874 anon_vma_interval_tree_post_update_vma(next);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -0800875 anon_vma_unlock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (mapping)
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800878 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530880 if (root) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100881 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530882
883 if (adjust_next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100884 uprobe_mmap(next);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530885 }
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (remove_next) {
Matt Helsley925d1c42008-04-29 01:01:36 -0700888 if (file) {
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530889 uprobe_munmap(next, next->vm_start, next->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 fput(file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700891 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800892 if (next->anon_vma)
893 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 mm->map_count--;
Oleg Nesterov3964acd2013-07-31 13:53:28 -0700895 mpol_put(vma_policy(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 kmem_cache_free(vm_area_cachep, next);
897 /*
898 * In mprotect's case 6 (see comments on vma_merge),
899 * we must remove another next too. It would clutter
900 * up the code too much to do both in one go.
901 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700902 if (remove_next != 3) {
903 /*
904 * If "next" was removed and vma->vm_end was
905 * expanded (up) over it, in turn
906 * "next->vm_prev->vm_end" changed and the
907 * "vma->vm_next" gap must be updated.
908 */
909 next = vma->vm_next;
910 } else {
911 /*
912 * For the scope of the comment "next" and
913 * "vma" considered pre-swap(): if "vma" was
914 * removed, next->vm_start was expanded (down)
915 * over it and the "next" gap must be updated.
916 * Because of the swap() the post-swap() "vma"
917 * actually points to pre-swap() "next"
918 * (post-swap() "next" as opposed is now a
919 * dangling pointer).
920 */
921 next = vma;
922 }
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700923 if (remove_next == 2) {
924 remove_next = 1;
925 end = next->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 goto again;
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700927 }
Michel Lespinassed3737182012-12-11 16:01:38 -0800928 else if (next)
929 vma_gap_update(next);
Andrea Arcangelifb8c41e2016-10-07 17:01:25 -0700930 else {
931 /*
932 * If remove_next == 2 we obviously can't
933 * reach this path.
934 *
935 * If remove_next == 3 we can't reach this
936 * path because pre-swap() next is always not
937 * NULL. pre-swap() "next" is not being
938 * removed and its next->vm_end is not altered
939 * (and furthermore "end" already matches
940 * next->vm_end in remove_next == 3).
941 *
942 * We reach this only in the remove_next == 1
943 * case if the "next" vma that was removed was
944 * the highest vma of the mm. However in such
945 * case next->vm_end == "end" and the extended
946 * "vma" has vma->vm_end == next->vm_end so
947 * mm->highest_vm_end doesn't need any update
948 * in remove_next == 1 case.
949 */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700950 VM_WARN_ON(mm->highest_vm_end != vm_end_gap(vma));
Andrea Arcangelifb8c41e2016-10-07 17:01:25 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530953 if (insert && file)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100954 uprobe_mmap(insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -0800957
958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/*
962 * If the vma has a ->close operation then the driver probably needs to release
963 * per-vma resources, so we don't attempt to merge those.
964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static inline int is_mergeable_vma(struct vm_area_struct *vma,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700966 struct file *file, unsigned long vm_flags,
967 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Cyrill Gorcunov34228d42014-01-23 15:53:42 -0800969 /*
970 * VM_SOFTDIRTY should not prevent from VMA merging, if we
971 * match the flags but dirty bit -- the caller should mark
972 * merged VMA as dirty. If dirty bit won't be excluded from
973 * comparison, we increase pressue on the memory system forcing
974 * the kernel to generate new VMAs when old one could be
975 * extended instead.
976 */
977 if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
979 if (vma->vm_file != file)
980 return 0;
981 if (vma->vm_ops && vma->vm_ops->close)
982 return 0;
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700983 if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx))
984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 1;
986}
987
988static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -0700989 struct anon_vma *anon_vma2,
990 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Shaohua Li965f55d2011-05-24 17:11:20 -0700992 /*
993 * The list_is_singular() test is to avoid merging VMA cloned from
994 * parents. This can improve scalability caused by anon_vma lock.
995 */
996 if ((!anon_vma1 || !anon_vma2) && (!vma ||
997 list_is_singular(&vma->anon_vma_chain)))
998 return 1;
999 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002/*
1003 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
1004 * in front of (at a lower virtual address and file offset than) the vma.
1005 *
1006 * We cannot merge two vmas if they have differently assigned (non-NULL)
1007 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
1008 *
1009 * We don't check here for the merged mmap wrapping around the end of pagecache
1010 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
1011 * wrap, nor mmaps which cover the final page at index -1UL.
1012 */
1013static int
1014can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001015 struct anon_vma *anon_vma, struct file *file,
1016 pgoff_t vm_pgoff,
1017 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001019 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
Shaohua Li965f55d2011-05-24 17:11:20 -07001020 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (vma->vm_pgoff == vm_pgoff)
1022 return 1;
1023 }
1024 return 0;
1025}
1026
1027/*
1028 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
1029 * beyond (at a higher virtual address and file offset than) the vma.
1030 *
1031 * We cannot merge two vmas if they have differently assigned (non-NULL)
1032 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
1033 */
1034static int
1035can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001036 struct anon_vma *anon_vma, struct file *file,
1037 pgoff_t vm_pgoff,
1038 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001040 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
Shaohua Li965f55d2011-05-24 17:11:20 -07001041 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 pgoff_t vm_pglen;
Libind6e93212013-07-03 15:01:26 -07001043 vm_pglen = vma_pages(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
1045 return 1;
1046 }
1047 return 0;
1048}
1049
1050/*
1051 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
1052 * whether that can be merged with its predecessor or its successor.
1053 * Or both (it neatly fills a hole).
1054 *
1055 * In most cases - when called for mmap, brk or mremap - [addr,end) is
1056 * certain not to be mapped by the time vma_merge is called; but when
1057 * called for mprotect, it is certain to be already mapped (either at
1058 * an offset within prev, or at the start of next), and the flags of
1059 * this area are about to be changed to vm_flags - and the no-change
1060 * case has already been eliminated.
1061 *
1062 * The following mprotect cases have to be considered, where AAAA is
1063 * the area passed down from mprotect_fixup, never extending beyond one
1064 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
1065 *
1066 * AAAA AAAA AAAA AAAA
1067 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
1068 * cannot merge might become might become might become
1069 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
1070 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001071 * mremap move: PPPPXXXXXXXX 8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 * AAAA
1073 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
1074 * might become case 1 below case 2 below case 3 below
1075 *
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001076 * It is important for case 8 that the the vma NNNN overlapping the
1077 * region AAAA is never going to extended over XXXX. Instead XXXX must
1078 * be extended in region AAAA and NNNN must be removed. This way in
1079 * all cases where vma_merge succeeds, the moment vma_adjust drops the
1080 * rmap_locks, the properties of the merged vma will be already
1081 * correct for the whole merged range. Some of those properties like
1082 * vm_page_prot/vm_flags may be accessed by rmap_walks and they must
1083 * be correct for the whole merged range immediately after the
1084 * rmap_locks are released. Otherwise if XXXX would be removed and
1085 * NNNN would be extended over the XXXX range, remove_migration_ptes
1086 * or other rmap walkers (if working on addresses beyond the "end"
1087 * parameter) may establish ptes with the wrong permissions of NNNN
1088 * instead of the right permissions of XXXX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
1090struct vm_area_struct *vma_merge(struct mm_struct *mm,
1091 struct vm_area_struct *prev, unsigned long addr,
1092 unsigned long end, unsigned long vm_flags,
vishnu.pscc71aba2014-10-09 15:26:29 -07001093 struct anon_vma *anon_vma, struct file *file,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001094 pgoff_t pgoff, struct mempolicy *policy,
1095 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
1098 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -08001099 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /*
1102 * We later require that vma->vm_flags == vm_flags,
1103 * so this tests vma->vm_flags & VM_SPECIAL, too.
1104 */
1105 if (vm_flags & VM_SPECIAL)
1106 return NULL;
1107
1108 if (prev)
1109 next = prev->vm_next;
1110 else
1111 next = mm->mmap;
1112 area = next;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001113 if (area && area->vm_end == end) /* cases 6, 7, 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 next = next->vm_next;
1115
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001116 /* verify some invariant that must be enforced by the caller */
1117 VM_WARN_ON(prev && addr <= prev->vm_start);
1118 VM_WARN_ON(area && end > area->vm_end);
1119 VM_WARN_ON(addr >= end);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /*
1122 * Can it merge with the predecessor?
1123 */
1124 if (prev && prev->vm_end == addr &&
vishnu.pscc71aba2014-10-09 15:26:29 -07001125 mpol_equal(vma_policy(prev), policy) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 can_vma_merge_after(prev, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001127 anon_vma, file, pgoff,
1128 vm_userfaultfd_ctx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * OK, it can. Can we now merge in the successor as well?
1131 */
1132 if (next && end == next->vm_start &&
1133 mpol_equal(policy, vma_policy(next)) &&
1134 can_vma_merge_before(next, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001135 anon_vma, file,
1136 pgoff+pglen,
1137 vm_userfaultfd_ctx) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -07001139 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* cases 1, 6 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001141 err = __vma_adjust(prev, prev->vm_start,
1142 next->vm_end, prev->vm_pgoff, NULL,
1143 prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 } else /* cases 2, 5, 7 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001145 err = __vma_adjust(prev, prev->vm_start,
1146 end, prev->vm_pgoff, NULL, prev);
Rik van Riel5beb4932010-03-05 13:42:07 -08001147 if (err)
1148 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001149 khugepaged_enter_vma_merge(prev, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return prev;
1151 }
1152
1153 /*
1154 * Can this new request be merged in front of next?
1155 */
1156 if (next && end == next->vm_start &&
vishnu.pscc71aba2014-10-09 15:26:29 -07001157 mpol_equal(policy, vma_policy(next)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 can_vma_merge_before(next, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001159 anon_vma, file, pgoff+pglen,
1160 vm_userfaultfd_ctx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (prev && addr < prev->vm_end) /* case 4 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001162 err = __vma_adjust(prev, prev->vm_start,
1163 addr, prev->vm_pgoff, NULL, next);
1164 else { /* cases 3, 8 */
1165 err = __vma_adjust(area, addr, next->vm_end,
1166 next->vm_pgoff - pglen, NULL, next);
1167 /*
1168 * In case 3 area is already equal to next and
1169 * this is a noop, but in case 8 "area" has
1170 * been removed and next was expanded over it.
1171 */
1172 area = next;
1173 }
Rik van Riel5beb4932010-03-05 13:42:07 -08001174 if (err)
1175 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001176 khugepaged_enter_vma_merge(area, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return area;
1178 }
1179
1180 return NULL;
1181}
1182
1183/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001184 * Rough compatbility check to quickly see if it's even worth looking
1185 * at sharing an anon_vma.
1186 *
1187 * They need to have the same vm_file, and the flags can only differ
1188 * in things that mprotect may change.
1189 *
1190 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1191 * we can merge the two vma's. For example, we refuse to merge a vma if
1192 * there is a vm_ops->close() function, because that indicates that the
1193 * driver is doing some kind of reference counting. But that doesn't
1194 * really matter for the anon_vma sharing case.
1195 */
1196static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1197{
1198 return a->vm_end == b->vm_start &&
1199 mpol_equal(vma_policy(a), vma_policy(b)) &&
1200 a->vm_file == b->vm_file &&
Cyrill Gorcunov34228d42014-01-23 15:53:42 -08001201 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) &&
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001202 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1203}
1204
1205/*
1206 * Do some basic sanity checking to see if we can re-use the anon_vma
1207 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1208 * the same as 'old', the other will be the new one that is trying
1209 * to share the anon_vma.
1210 *
1211 * NOTE! This runs with mm_sem held for reading, so it is possible that
1212 * the anon_vma of 'old' is concurrently in the process of being set up
1213 * by another page fault trying to merge _that_. But that's ok: if it
1214 * is being set up, that automatically means that it will be a singleton
1215 * acceptable for merging, so we can do all of this optimistically. But
Jason Low4db0c3c2015-04-15 16:14:08 -07001216 * we do that READ_ONCE() to make sure that we never re-load the pointer.
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001217 *
1218 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1219 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1220 * is to return an anon_vma that is "complex" due to having gone through
1221 * a fork).
1222 *
1223 * We also make sure that the two vma's are compatible (adjacent,
1224 * and with the same memory policies). That's all stable, even with just
1225 * a read lock on the mm_sem.
1226 */
1227static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1228{
1229 if (anon_vma_compatible(a, b)) {
Jason Low4db0c3c2015-04-15 16:14:08 -07001230 struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001231
1232 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1233 return anon_vma;
1234 }
1235 return NULL;
1236}
1237
1238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1240 * neighbouring vmas for a suitable anon_vma, before it goes off
1241 * to allocate a new anon_vma. It checks because a repetitive
1242 * sequence of mprotects and faults may otherwise lead to distinct
1243 * anon_vmas being allocated, preventing vma merge in subsequent
1244 * mprotect.
1245 */
1246struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1247{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001248 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 near = vma->vm_next;
1252 if (!near)
1253 goto try_prev;
1254
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001255 anon_vma = reusable_anon_vma(near, vma, near);
1256 if (anon_vma)
1257 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -07001259 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (!near)
1261 goto none;
1262
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001263 anon_vma = reusable_anon_vma(near, near, vma);
1264 if (anon_vma)
1265 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266none:
1267 /*
1268 * There's no absolute need to look only at touching neighbours:
1269 * we could search further afield for "compatible" anon_vmas.
1270 * But it would probably just be a waste of time searching,
1271 * or lead to too many vmas hanging off the same anon_vma.
1272 * We're trying to allow mprotect remerging later on,
1273 * not trying to minimize memory used for anon_vmas.
1274 */
1275 return NULL;
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278/*
Al Viro40401532012-02-13 03:58:52 +00001279 * If a hint addr is less than mmap_min_addr change hint to be as
1280 * low as possible but still greater than mmap_min_addr
1281 */
1282static inline unsigned long round_hint_to_min(unsigned long hint)
1283{
1284 hint &= PAGE_MASK;
1285 if (((void *)hint != NULL) &&
1286 (hint < mmap_min_addr))
1287 return PAGE_ALIGN(mmap_min_addr);
1288 return hint;
1289}
1290
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001291static inline int mlock_future_check(struct mm_struct *mm,
1292 unsigned long flags,
1293 unsigned long len)
1294{
1295 unsigned long locked, lock_limit;
1296
1297 /* mlock MCL_FUTURE? */
1298 if (flags & VM_LOCKED) {
1299 locked = len >> PAGE_SHIFT;
1300 locked += mm->locked_vm;
1301 lock_limit = rlimit(RLIMIT_MEMLOCK);
1302 lock_limit >>= PAGE_SHIFT;
1303 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1304 return -EAGAIN;
1305 }
1306 return 0;
1307}
1308
Linus Torvalds7a403742018-05-11 09:52:01 -07001309static inline u64 file_mmap_size_max(struct file *file, struct inode *inode)
1310{
1311 if (S_ISREG(inode->i_mode))
Linus Torvalds4be65292018-05-19 09:29:11 -07001312 return MAX_LFS_FILESIZE;
Linus Torvalds7a403742018-05-11 09:52:01 -07001313
1314 if (S_ISBLK(inode->i_mode))
1315 return MAX_LFS_FILESIZE;
1316
1317 /* Special "we do even unsigned file positions" case */
1318 if (file->f_mode & FMODE_UNSIGNED_OFFSET)
1319 return 0;
1320
1321 /* Yes, random drivers might want more. But I'm tired of buggy drivers */
1322 return ULONG_MAX;
1323}
1324
1325static inline bool file_mmap_ok(struct file *file, struct inode *inode,
1326 unsigned long pgoff, unsigned long len)
1327{
1328 u64 maxsize = file_mmap_size_max(file, inode);
1329
1330 if (maxsize && len > maxsize)
1331 return false;
1332 maxsize -= len;
1333 if (pgoff > maxsize >> PAGE_SHIFT)
1334 return false;
1335 return true;
1336}
1337
Al Viro40401532012-02-13 03:58:52 +00001338/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001339 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001341unsigned long do_mmap(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 unsigned long len, unsigned long prot,
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001343 unsigned long flags, vm_flags_t vm_flags,
1344 unsigned long pgoff, unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
vishnu.pscc71aba2014-10-09 15:26:29 -07001346 struct mm_struct *mm = current->mm;
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001347 int pkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Michel Lespinasse41badc12013-02-22 16:32:47 -08001349 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001350
Piotr Kwapulinskie37609b2015-06-24 16:58:39 -07001351 if (!len)
1352 return -EINVAL;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /*
1355 * Does the application expect PROT_READ to imply PROT_EXEC?
1356 *
1357 * (the exception is when the underlying filesystem is noexec
1358 * mounted, in which case we dont add PROT_EXEC.)
1359 */
1360 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Eric W. Biederman90f85722015-06-29 14:42:03 -05001361 if (!(file && path_noexec(&file->f_path)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 prot |= PROT_EXEC;
1363
Eric Paris7cd94142007-11-26 18:47:40 -05001364 if (!(flags & MAP_FIXED))
1365 addr = round_hint_to_min(addr);
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Careful about overflows.. */
1368 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001369 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -ENOMEM;
1371
1372 /* offset overflow? */
1373 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
vishnu.pscc71aba2014-10-09 15:26:29 -07001374 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* Too many mappings? */
1377 if (mm->map_count > sysctl_max_map_count)
1378 return -ENOMEM;
1379
1380 /* Obtain the address to map to. we verify (or select) it and ensure
1381 * that it represents a valid section of the address space.
1382 */
1383 addr = get_unmapped_area(file, addr, len, pgoff, flags);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001384 if (offset_in_page(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return addr;
1386
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001387 if (prot == PROT_EXEC) {
1388 pkey = execute_only_pkey(mm);
1389 if (pkey < 0)
1390 pkey = 0;
1391 }
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /* Do simple checking here so the lower-level routines won't have
1394 * to. we assume access permissions have been handled by the open
1395 * of the memory object, so we don't do any here.
1396 */
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001397 vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1399
Huang Shijiecdf7b342009-09-21 17:03:36 -07001400 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (!can_do_mlock())
1402 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001403
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001404 if (mlock_future_check(mm, vm_flags, len))
1405 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (file) {
Oleg Nesterov077bf222013-09-11 14:20:19 -07001408 struct inode *inode = file_inode(file);
1409
Linus Torvalds7a403742018-05-11 09:52:01 -07001410 if (!file_mmap_ok(file, inode, pgoff, len))
1411 return -EOVERFLOW;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 switch (flags & MAP_TYPE) {
1414 case MAP_SHARED:
1415 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1416 return -EACCES;
1417
1418 /*
1419 * Make sure we don't allow writing to an append-only
1420 * file..
1421 */
1422 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1423 return -EACCES;
1424
1425 /*
1426 * Make sure there are no mandatory locks on the file.
1427 */
Jeff Laytond7a06982014-03-10 09:54:15 -04001428 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return -EAGAIN;
1430
1431 vm_flags |= VM_SHARED | VM_MAYSHARE;
1432 if (!(file->f_mode & FMODE_WRITE))
1433 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1434
1435 /* fall through */
1436 case MAP_PRIVATE:
1437 if (!(file->f_mode & FMODE_READ))
1438 return -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001439 if (path_noexec(&file->f_path)) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001440 if (vm_flags & VM_EXEC)
1441 return -EPERM;
1442 vm_flags &= ~VM_MAYEXEC;
1443 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001444
Al Viro72c2d532013-09-22 16:27:52 -04001445 if (!file->f_op->mmap)
Linus Torvalds80c56062006-10-15 14:09:55 -07001446 return -ENODEV;
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001447 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 break;
1450
1451 default:
1452 return -EINVAL;
1453 }
1454 } else {
1455 switch (flags & MAP_TYPE) {
1456 case MAP_SHARED:
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001457 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1458 return -EINVAL;
Tejun Heoce363942008-09-03 16:09:47 +02001459 /*
1460 * Ignore pgoff.
1461 */
1462 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 vm_flags |= VM_SHARED | VM_MAYSHARE;
1464 break;
1465 case MAP_PRIVATE:
1466 /*
1467 * Set pgoff according to addr for anon_vma.
1468 */
1469 pgoff = addr >> PAGE_SHIFT;
1470 break;
1471 default:
1472 return -EINVAL;
1473 }
1474 }
1475
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001476 /*
1477 * Set 'VM_NORESERVE' if we should not account for the
1478 * memory use of this mapping.
1479 */
1480 if (flags & MAP_NORESERVE) {
1481 /* We honor MAP_NORESERVE if allowed to overcommit */
1482 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1483 vm_flags |= VM_NORESERVE;
1484
1485 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1486 if (file && is_file_hugepages(file))
1487 vm_flags |= VM_NORESERVE;
1488 }
1489
1490 addr = mmap_region(file, addr, len, vm_flags, pgoff);
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -07001491 if (!IS_ERR_VALUE(addr) &&
1492 ((vm_flags & VM_LOCKED) ||
1493 (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
Michel Lespinasse41badc12013-02-22 16:32:47 -08001494 *populate = len;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001495 return addr;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001496}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001497
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001498SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1499 unsigned long, prot, unsigned long, flags,
1500 unsigned long, fd, unsigned long, pgoff)
1501{
1502 struct file *file = NULL;
Chen Gang1e3ee142015-11-05 18:48:35 -08001503 unsigned long retval;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001504
1505 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001506 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001507 file = fget(fd);
1508 if (!file)
Chen Gang1e3ee142015-11-05 18:48:35 -08001509 return -EBADF;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001510 if (is_file_hugepages(file))
1511 len = ALIGN(len, huge_page_size(hstate_file(file)));
Jörn Engel493af572013-07-08 16:00:26 -07001512 retval = -EINVAL;
1513 if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
1514 goto out_fput;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001515 } else if (flags & MAP_HUGETLB) {
1516 struct user_struct *user = NULL;
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001517 struct hstate *hs;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001518
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001519 hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & SHM_HUGE_MASK);
Li Zefan091d0d52013-05-09 15:08:15 +08001520 if (!hs)
1521 return -EINVAL;
1522
1523 len = ALIGN(len, huge_page_size(hs));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001524 /*
1525 * VM_NORESERVE is used because the reservations will be
1526 * taken when vm_ops->mmap() is called
1527 * A dummy user value is used because we are not locking
1528 * memory so no accounting is necessary
1529 */
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001530 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
Andi Kleen42d73952012-12-11 16:01:34 -08001531 VM_NORESERVE,
1532 &user, HUGETLB_ANONHUGE_INODE,
1533 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001534 if (IS_ERR(file))
1535 return PTR_ERR(file);
1536 }
1537
1538 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1539
Michal Hocko9fbeb5a2016-05-23 16:25:30 -07001540 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Jörn Engel493af572013-07-08 16:00:26 -07001541out_fput:
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001542 if (file)
1543 fput(file);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001544 return retval;
1545}
1546
Christoph Hellwiga4679372010-03-10 15:21:15 -08001547#ifdef __ARCH_WANT_SYS_OLD_MMAP
1548struct mmap_arg_struct {
1549 unsigned long addr;
1550 unsigned long len;
1551 unsigned long prot;
1552 unsigned long flags;
1553 unsigned long fd;
1554 unsigned long offset;
1555};
1556
1557SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1558{
1559 struct mmap_arg_struct a;
1560
1561 if (copy_from_user(&a, arg, sizeof(a)))
1562 return -EFAULT;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001563 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001564 return -EINVAL;
1565
1566 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1567 a.offset >> PAGE_SHIFT);
1568}
1569#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1570
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001571/*
1572 * Some shared mappigns will want the pages marked read-only
1573 * to track write events. If so, we'll downgrade vm_page_prot
1574 * to the private version (using protection_map[] without the
1575 * VM_SHARED bit).
1576 */
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07001577int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001578{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001579 vm_flags_t vm_flags = vma->vm_flags;
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001580 const struct vm_operations_struct *vm_ops = vma->vm_ops;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001581
1582 /* If it was private or non-writable, the write bit is already clear */
1583 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1584 return 0;
1585
1586 /* The backer wishes to know when pages are first written to? */
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001587 if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001588 return 1;
1589
Peter Feiner64e45502014-10-13 15:55:46 -07001590 /* The open routine did something to the protections that pgprot_modify
1591 * won't preserve? */
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07001592 if (pgprot_val(vm_page_prot) !=
1593 pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001594 return 0;
1595
Peter Feiner64e45502014-10-13 15:55:46 -07001596 /* Do we need to track softdirty? */
1597 if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY))
1598 return 1;
1599
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001600 /* Specialty mapping? */
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001601 if (vm_flags & VM_PFNMAP)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001602 return 0;
1603
1604 /* Can the mapping track the dirty pages? */
1605 return vma->vm_file && vma->vm_file->f_mapping &&
1606 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1607}
1608
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001609/*
1610 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001611 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001612 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001613static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001614{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001615 /*
1616 * hugetlb has its own accounting separate from the core VM
1617 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1618 */
1619 if (file && is_file_hugepages(file))
1620 return 0;
1621
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001622 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1623}
1624
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001625unsigned long mmap_region(struct file *file, unsigned long addr,
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001626 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001627{
1628 struct mm_struct *mm = current->mm;
1629 struct vm_area_struct *vma, *prev;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001630 int error;
1631 struct rb_node **rb_link, *rb_parent;
1632 unsigned long charged = 0;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001633
Cyril Hrubise8420a82013-04-29 15:08:33 -07001634 /* Check against address space limit. */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001635 if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) {
Cyril Hrubise8420a82013-04-29 15:08:33 -07001636 unsigned long nr_pages;
1637
1638 /*
1639 * MAP_FIXED may remove pages of mappings that intersects with
1640 * requested mapping. Account for the pages it would unmap.
1641 */
Cyril Hrubise8420a82013-04-29 15:08:33 -07001642 nr_pages = count_vma_pages_range(mm, addr, addr + len);
1643
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001644 if (!may_expand_vm(mm, vm_flags,
1645 (len >> PAGE_SHIFT) - nr_pages))
Cyril Hrubise8420a82013-04-29 15:08:33 -07001646 return -ENOMEM;
1647 }
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /* Clear old maps */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07001650 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
1651 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (do_munmap(mm, addr, len))
1653 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001656 /*
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001657 * Private writable mapping: check memory availability
1658 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001659 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001660 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001661 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001662 return -ENOMEM;
1663 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
1666 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001667 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001669 vma = vma_merge(mm, prev, addr, addr + len, vm_flags,
1670 NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX);
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001671 if (vma)
1672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /*
1675 * Determine the object being mapped and call the appropriate
1676 * specific mapper. the address has already been validated, but
1677 * not unmapped, but the maps are removed from the list.
1678 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001679 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (!vma) {
1681 error = -ENOMEM;
1682 goto unacct_error;
1683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 vma->vm_mm = mm;
1686 vma->vm_start = addr;
1687 vma->vm_end = addr + len;
1688 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001689 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 vma->vm_pgoff = pgoff;
Rik van Riel5beb4932010-03-05 13:42:07 -08001691 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (vm_flags & VM_DENYWRITE) {
1695 error = deny_write_access(file);
1696 if (error)
1697 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001699 if (vm_flags & VM_SHARED) {
1700 error = mapping_map_writable(file->f_mapping);
1701 if (error)
1702 goto allow_write_and_free_vma;
1703 }
1704
1705 /* ->mmap() can change vma->vm_file, but must guarantee that
1706 * vma_link() below can deny write-access if VM_DENYWRITE is set
1707 * and map writably if VM_SHARED is set. This usually means the
1708 * new file must not have been exposed to user-space, yet.
1709 */
Al Virocb0942b2012-08-27 14:48:26 -04001710 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 error = file->f_op->mmap(file, vma);
1712 if (error)
1713 goto unmap_and_free_vma;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001714
1715 /* Can addr have changed??
1716 *
1717 * Answer: Yes, several device drivers can do it in their
1718 * f_op->mmap method. -DaveM
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001719 * Bug: If addr is changed, prev, rb_link, rb_parent should
1720 * be updated for vma_link()
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001721 */
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001722 WARN_ON_ONCE(addr != vma->vm_start);
1723
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001724 addr = vma->vm_start;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001725 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 } else if (vm_flags & VM_SHARED) {
1727 error = shmem_zero_setup(vma);
1728 if (error)
1729 goto free_vma;
1730 }
1731
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001732 vma_link(mm, vma, prev, rb_link, rb_parent);
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001733 /* Once vma denies write, undo our temporary denial count */
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001734 if (file) {
1735 if (vm_flags & VM_SHARED)
1736 mapping_unmap_writable(file->f_mapping);
1737 if (vm_flags & VM_DENYWRITE)
1738 allow_write_access(file);
1739 }
Oleg Nesterove8686772013-09-11 14:20:20 -07001740 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001741out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001743
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001744 vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (vm_flags & VM_LOCKED) {
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001746 if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
1747 vma == get_gate_vma(current->mm)))
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001748 mm->locked_vm += (len >> PAGE_SHIFT);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001749 else
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001750 vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001751 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301752
Oleg Nesterovc7a3a882012-08-19 19:10:42 +02001753 if (file)
1754 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301755
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001756 /*
1757 * New (or expanded) vma always get soft dirty status.
1758 * Otherwise user-space soft-dirty page tracker won't
1759 * be able to distinguish situation when vma area unmapped,
1760 * then new mapped in-place (which must be aimed as
1761 * a completely new data area).
1762 */
1763 vma->vm_flags |= VM_SOFTDIRTY;
1764
Peter Feiner64e45502014-10-13 15:55:46 -07001765 vma_set_page_prot(vma);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return addr;
1768
1769unmap_and_free_vma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 vma->vm_file = NULL;
1771 fput(file);
1772
1773 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001774 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1775 charged = 0;
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001776 if (vm_flags & VM_SHARED)
1777 mapping_unmap_writable(file->f_mapping);
1778allow_write_and_free_vma:
1779 if (vm_flags & VM_DENYWRITE)
1780 allow_write_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781free_vma:
1782 kmem_cache_free(vm_area_cachep, vma);
1783unacct_error:
1784 if (charged)
1785 vm_unacct_memory(charged);
1786 return error;
1787}
1788
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001789unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1790{
1791 /*
1792 * We implement the search by looking for an rbtree node that
1793 * immediately follows a suitable gap. That is,
1794 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1795 * - gap_end = vma->vm_start >= info->low_limit + length;
1796 * - gap_end - gap_start >= length
1797 */
1798
1799 struct mm_struct *mm = current->mm;
1800 struct vm_area_struct *vma;
1801 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1802
1803 /* Adjust search length to account for worst case alignment overhead */
1804 length = info->length + info->align_mask;
1805 if (length < info->length)
1806 return -ENOMEM;
1807
1808 /* Adjust search limits by the desired length */
1809 if (info->high_limit < length)
1810 return -ENOMEM;
1811 high_limit = info->high_limit - length;
1812
1813 if (info->low_limit > high_limit)
1814 return -ENOMEM;
1815 low_limit = info->low_limit + length;
1816
1817 /* Check if rbtree root looks promising */
1818 if (RB_EMPTY_ROOT(&mm->mm_rb))
1819 goto check_highest;
1820 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1821 if (vma->rb_subtree_gap < length)
1822 goto check_highest;
1823
1824 while (true) {
1825 /* Visit left subtree if it looks promising */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001826 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001827 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1828 struct vm_area_struct *left =
1829 rb_entry(vma->vm_rb.rb_left,
1830 struct vm_area_struct, vm_rb);
1831 if (left->rb_subtree_gap >= length) {
1832 vma = left;
1833 continue;
1834 }
1835 }
1836
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001837 gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001838check_current:
1839 /* Check if current node has a suitable gap */
1840 if (gap_start > high_limit)
1841 return -ENOMEM;
Hugh Dickinsce7fe852017-06-20 02:10:44 -07001842 if (gap_end >= low_limit &&
1843 gap_end > gap_start && gap_end - gap_start >= length)
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001844 goto found;
1845
1846 /* Visit right subtree if it looks promising */
1847 if (vma->vm_rb.rb_right) {
1848 struct vm_area_struct *right =
1849 rb_entry(vma->vm_rb.rb_right,
1850 struct vm_area_struct, vm_rb);
1851 if (right->rb_subtree_gap >= length) {
1852 vma = right;
1853 continue;
1854 }
1855 }
1856
1857 /* Go back up the rbtree to find next candidate node */
1858 while (true) {
1859 struct rb_node *prev = &vma->vm_rb;
1860 if (!rb_parent(prev))
1861 goto check_highest;
1862 vma = rb_entry(rb_parent(prev),
1863 struct vm_area_struct, vm_rb);
1864 if (prev == vma->vm_rb.rb_left) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001865 gap_start = vm_end_gap(vma->vm_prev);
1866 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001867 goto check_current;
1868 }
1869 }
1870 }
1871
1872check_highest:
1873 /* Check highest gap, which does not precede any rbtree node */
1874 gap_start = mm->highest_vm_end;
1875 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1876 if (gap_start > high_limit)
1877 return -ENOMEM;
1878
1879found:
1880 /* We found a suitable gap. Clip it with the original low_limit. */
1881 if (gap_start < info->low_limit)
1882 gap_start = info->low_limit;
1883
1884 /* Adjust gap address to the desired alignment */
1885 gap_start += (info->align_offset - gap_start) & info->align_mask;
1886
1887 VM_BUG_ON(gap_start + info->length > info->high_limit);
1888 VM_BUG_ON(gap_start + info->length > gap_end);
1889 return gap_start;
1890}
1891
1892unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1893{
1894 struct mm_struct *mm = current->mm;
1895 struct vm_area_struct *vma;
1896 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1897
1898 /* Adjust search length to account for worst case alignment overhead */
1899 length = info->length + info->align_mask;
1900 if (length < info->length)
1901 return -ENOMEM;
1902
1903 /*
1904 * Adjust search limits by the desired length.
1905 * See implementation comment at top of unmapped_area().
1906 */
1907 gap_end = info->high_limit;
1908 if (gap_end < length)
1909 return -ENOMEM;
1910 high_limit = gap_end - length;
1911
1912 if (info->low_limit > high_limit)
1913 return -ENOMEM;
1914 low_limit = info->low_limit + length;
1915
1916 /* Check highest gap, which does not precede any rbtree node */
1917 gap_start = mm->highest_vm_end;
1918 if (gap_start <= high_limit)
1919 goto found_highest;
1920
1921 /* Check if rbtree root looks promising */
1922 if (RB_EMPTY_ROOT(&mm->mm_rb))
1923 return -ENOMEM;
1924 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1925 if (vma->rb_subtree_gap < length)
1926 return -ENOMEM;
1927
1928 while (true) {
1929 /* Visit right subtree if it looks promising */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001930 gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001931 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
1932 struct vm_area_struct *right =
1933 rb_entry(vma->vm_rb.rb_right,
1934 struct vm_area_struct, vm_rb);
1935 if (right->rb_subtree_gap >= length) {
1936 vma = right;
1937 continue;
1938 }
1939 }
1940
1941check_current:
1942 /* Check if current node has a suitable gap */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001943 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001944 if (gap_end < low_limit)
1945 return -ENOMEM;
Hugh Dickinsce7fe852017-06-20 02:10:44 -07001946 if (gap_start <= high_limit &&
1947 gap_end > gap_start && gap_end - gap_start >= length)
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001948 goto found;
1949
1950 /* Visit left subtree if it looks promising */
1951 if (vma->vm_rb.rb_left) {
1952 struct vm_area_struct *left =
1953 rb_entry(vma->vm_rb.rb_left,
1954 struct vm_area_struct, vm_rb);
1955 if (left->rb_subtree_gap >= length) {
1956 vma = left;
1957 continue;
1958 }
1959 }
1960
1961 /* Go back up the rbtree to find next candidate node */
1962 while (true) {
1963 struct rb_node *prev = &vma->vm_rb;
1964 if (!rb_parent(prev))
1965 return -ENOMEM;
1966 vma = rb_entry(rb_parent(prev),
1967 struct vm_area_struct, vm_rb);
1968 if (prev == vma->vm_rb.rb_right) {
1969 gap_start = vma->vm_prev ?
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001970 vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001971 goto check_current;
1972 }
1973 }
1974 }
1975
1976found:
1977 /* We found a suitable gap. Clip it with the original high_limit. */
1978 if (gap_end > info->high_limit)
1979 gap_end = info->high_limit;
1980
1981found_highest:
1982 /* Compute highest gap address at the desired alignment */
1983 gap_end -= info->length;
1984 gap_end -= (gap_end - info->align_offset) & info->align_mask;
1985
1986 VM_BUG_ON(gap_end < info->low_limit);
1987 VM_BUG_ON(gap_end < gap_start);
1988 return gap_end;
1989}
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991/* Get an address range which is currently unmapped.
1992 * For shmat() with addr=0.
1993 *
1994 * Ugly calling convention alert:
1995 * Return value with the low bits set means error value,
1996 * ie
1997 * if (ret & ~PAGE_MASK)
1998 * error = ret;
1999 *
2000 * This function "knows" that -ENOMEM has the bits set.
2001 */
2002#ifndef HAVE_ARCH_UNMAPPED_AREA
2003unsigned long
2004arch_get_unmapped_area(struct file *filp, unsigned long addr,
2005 unsigned long len, unsigned long pgoff, unsigned long flags)
2006{
2007 struct mm_struct *mm = current->mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002008 struct vm_area_struct *vma, *prev;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002009 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002011 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return -ENOMEM;
2013
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002014 if (flags & MAP_FIXED)
2015 return addr;
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (addr) {
2018 addr = PAGE_ALIGN(addr);
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002019 vma = find_vma_prev(mm, addr, &prev);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002020 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002021 (!vma || addr + len <= vm_start_gap(vma)) &&
2022 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return addr;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002026 info.flags = 0;
2027 info.length = len;
Heiko Carstens4e99b022013-11-12 15:07:54 -08002028 info.low_limit = mm->mmap_base;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002029 info.high_limit = TASK_SIZE;
2030 info.align_mask = 0;
2031 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
vishnu.pscc71aba2014-10-09 15:26:29 -07002033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/*
2036 * This mmap-allocator allocates new areas top-down from below the
2037 * stack's low limit (the base):
2038 */
2039#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
2040unsigned long
2041arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
2042 const unsigned long len, const unsigned long pgoff,
2043 const unsigned long flags)
2044{
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002045 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 struct mm_struct *mm = current->mm;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002047 unsigned long addr = addr0;
2048 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 /* requested length too big for entire address space */
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002051 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return -ENOMEM;
2053
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002054 if (flags & MAP_FIXED)
2055 return addr;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 /* requesting a specific address */
2058 if (addr) {
2059 addr = PAGE_ALIGN(addr);
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002060 vma = find_vma_prev(mm, addr, &prev);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002061 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002062 (!vma || addr + len <= vm_start_gap(vma)) &&
2063 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return addr;
2065 }
2066
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002067 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
2068 info.length = len;
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002069 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002070 info.high_limit = mm->mmap_base;
2071 info.align_mask = 0;
2072 addr = vm_unmapped_area(&info);
Xiao Guangrongb716ad92012-03-21 16:33:56 -07002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /*
2075 * A failed mmap() very likely causes application failure,
2076 * so fall back to the bottom-up function here. This scenario
2077 * can happen with large stack limits and large mmap()
2078 * allocations.
2079 */
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002080 if (offset_in_page(addr)) {
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002081 VM_BUG_ON(addr != -ENOMEM);
2082 info.flags = 0;
2083 info.low_limit = TASK_UNMAPPED_BASE;
2084 info.high_limit = TASK_SIZE;
2085 addr = vm_unmapped_area(&info);
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 return addr;
2089}
2090#endif
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092unsigned long
2093get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
2094 unsigned long pgoff, unsigned long flags)
2095{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002096 unsigned long (*get_area)(struct file *, unsigned long,
2097 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Al Viro9206de92009-12-03 15:23:11 -05002099 unsigned long error = arch_mmap_check(addr, len, flags);
2100 if (error)
2101 return error;
2102
2103 /* Careful about overflows.. */
2104 if (len > TASK_SIZE)
2105 return -ENOMEM;
2106
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002107 get_area = current->mm->get_unmapped_area;
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002108 if (file) {
2109 if (file->f_op->get_unmapped_area)
2110 get_area = file->f_op->get_unmapped_area;
2111 } else if (flags & MAP_SHARED) {
2112 /*
2113 * mmap_region() will call shmem_zero_setup() to create a file,
2114 * so use shmem's get_unmapped_area in case it can be huge.
2115 * do_mmap_pgoff() will clear pgoff, so match alignment.
2116 */
2117 pgoff = 0;
2118 get_area = shmem_get_unmapped_area;
2119 }
2120
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002121 addr = get_area(file, addr, len, pgoff, flags);
2122 if (IS_ERR_VALUE(addr))
2123 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Linus Torvalds07ab67c2005-05-19 22:43:37 -07002125 if (addr > TASK_SIZE - len)
2126 return -ENOMEM;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002127 if (offset_in_page(addr))
Linus Torvalds07ab67c2005-05-19 22:43:37 -07002128 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002129
Al Viro9ac4ed42012-05-30 17:13:15 -04002130 error = security_mmap_addr(addr);
2131 return error ? error : addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
2134EXPORT_SYMBOL(get_unmapped_area);
2135
2136/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
ZhenwenXu48aae422009-01-06 14:40:21 -08002137struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002139 struct rb_node *rb_node;
2140 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Rajman Mekaco841e31e2012-05-29 15:06:21 -07002142 /* Check the cache first. */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002143 vma = vmacache_find(mm, addr);
2144 if (likely(vma))
2145 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002147 rb_node = mm->mm_rb.rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002149 while (rb_node) {
2150 struct vm_area_struct *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002152 tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
Rajman Mekaco841e31e2012-05-29 15:06:21 -07002153
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002154 if (tmp->vm_end > addr) {
2155 vma = tmp;
2156 if (tmp->vm_start <= addr)
2157 break;
2158 rb_node = rb_node->rb_left;
2159 } else
2160 rb_node = rb_node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002162
2163 if (vma)
2164 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return vma;
2166}
2167
2168EXPORT_SYMBOL(find_vma);
2169
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002170/*
2171 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173struct vm_area_struct *
2174find_vma_prev(struct mm_struct *mm, unsigned long addr,
2175 struct vm_area_struct **pprev)
2176{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002177 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002179 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05002180 if (vma) {
2181 *pprev = vma->vm_prev;
2182 } else {
2183 struct rb_node *rb_node = mm->mm_rb.rb_node;
2184 *pprev = NULL;
2185 while (rb_node) {
2186 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
2187 rb_node = rb_node->rb_right;
2188 }
2189 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002190 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
2193/*
2194 * Verify that the stack growth is acceptable and
2195 * update accounting. This is shared with both the
2196 * grow-up and grow-down cases.
2197 */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002198static int acct_stack_growth(struct vm_area_struct *vma,
2199 unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 struct mm_struct *mm = vma->vm_mm;
2202 struct rlimit *rlim = current->signal->rlim;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002203 unsigned long new_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 /* address space limit tests */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002206 if (!may_expand_vm(mm, vma->vm_flags, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return -ENOMEM;
2208
2209 /* Stack limit test */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002210 if (size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return -ENOMEM;
2212
2213 /* mlock limit tests */
2214 if (vma->vm_flags & VM_LOCKED) {
2215 unsigned long locked;
2216 unsigned long limit;
2217 locked = mm->locked_vm + grow;
Jason Low4db0c3c2015-04-15 16:14:08 -07002218 limit = READ_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
Jiri Slaby59e99e52010-03-05 13:41:44 -08002219 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (locked > limit && !capable(CAP_IPC_LOCK))
2221 return -ENOMEM;
2222 }
2223
Adam Litke0d59a012007-01-30 14:35:39 -08002224 /* Check to ensure the stack will not grow into a hugetlb-only region */
2225 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
2226 vma->vm_end - size;
2227 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
2228 return -EFAULT;
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /*
2231 * Overcommit.. This must be the final test, as it will
2232 * update security statistics.
2233 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01002234 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return -ENOMEM;
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238}
2239
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002240#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002242 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2243 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002245int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Oleg Nesterov09357812015-11-05 18:48:17 -08002247 struct mm_struct *mm = vma->vm_mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002248 struct vm_area_struct *next;
2249 unsigned long gap_addr;
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002250 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 if (!(vma->vm_flags & VM_GROWSUP))
2253 return -EFAULT;
2254
Helge Deller5d10ad622017-06-19 17:34:05 +02002255 /* Guard against exceeding limits of the address space. */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002256 address &= PAGE_MASK;
Helge Deller38dfd2e2017-07-14 14:49:38 -07002257 if (address >= (TASK_SIZE & PAGE_MASK))
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002258 return -ENOMEM;
Helge Deller5d10ad622017-06-19 17:34:05 +02002259 address += PAGE_SIZE;
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002260
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002261 /* Enforce stack_guard_gap */
2262 gap_addr = address + stack_guard_gap;
Helge Deller5d10ad622017-06-19 17:34:05 +02002263
2264 /* Guard against overflow */
2265 if (gap_addr < address || gap_addr > TASK_SIZE)
2266 gap_addr = TASK_SIZE;
2267
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002268 next = vma->vm_next;
Michal Hockoc57664b2017-07-10 15:49:51 -07002269 if (next && next->vm_start < gap_addr &&
2270 (next->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002271 if (!(next->vm_flags & VM_GROWSUP))
2272 return -ENOMEM;
2273 /* Check that both stack segments have the same anon_vma? */
2274 }
2275
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002276 /* We must make sure the anon_vma is allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (unlikely(anon_vma_prepare(vma)))
2278 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 /*
2281 * vma->vm_start/vm_end cannot change under us because the caller
2282 * is required to hold the mmap_sem in read mode. We need the
2283 * anon_vma lock to serialize against concurrent expand_stacks.
2284 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002285 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 /* Somebody else might have raced and expanded it already */
2288 if (address > vma->vm_end) {
2289 unsigned long size, grow;
2290
2291 size = address - vma->vm_start;
2292 grow = (address - vma->vm_end) >> PAGE_SHIFT;
2293
Hugh Dickins42c36f62011-05-09 17:44:42 -07002294 error = -ENOMEM;
2295 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
2296 error = acct_stack_growth(vma, size, grow);
2297 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002298 /*
2299 * vma_gap_update() doesn't support concurrent
2300 * updates, but we only hold a shared mmap_sem
2301 * lock here, so we need to protect against
2302 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002303 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002304 * we don't guarantee that all growable vmas
2305 * in a mm share the same root anon vma.
2306 * So, we reuse mm->page_table_lock to guard
2307 * against concurrent vma expansions.
2308 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002309 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002310 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002311 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002312 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002313 anon_vma_interval_tree_pre_update_vma(vma);
Hugh Dickins42c36f62011-05-09 17:44:42 -07002314 vma->vm_end = address;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002315 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002316 if (vma->vm_next)
2317 vma_gap_update(vma->vm_next);
2318 else
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002319 mm->highest_vm_end = vm_end_gap(vma);
Oleg Nesterov09357812015-11-05 18:48:17 -08002320 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002321
Hugh Dickins42c36f62011-05-09 17:44:42 -07002322 perf_event_mmap(vma);
2323 }
Eric B Munson3af9e852010-05-18 15:30:49 +01002324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002326 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002327 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002328 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return error;
2330}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002331#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333/*
2334 * vma is the first one with address < vma->vm_start. Have to extend vma.
2335 */
Michal Hockod05f3162011-05-24 17:11:44 -07002336int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002337 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Oleg Nesterov09357812015-11-05 18:48:17 -08002339 struct mm_struct *mm = vma->vm_mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002340 struct vm_area_struct *prev;
2341 unsigned long gap_addr;
Jann Horn331fc4d2019-02-27 21:29:52 +01002342 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Eric Paris88694772007-11-26 18:47:26 -05002344 address &= PAGE_MASK;
Jann Horn331fc4d2019-02-27 21:29:52 +01002345 if (address < mmap_min_addr)
2346 return -EPERM;
Eric Paris88694772007-11-26 18:47:26 -05002347
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002348 /* Enforce stack_guard_gap */
2349 gap_addr = address - stack_guard_gap;
2350 if (gap_addr > address)
2351 return -ENOMEM;
2352 prev = vma->vm_prev;
Michal Hockoc57664b2017-07-10 15:49:51 -07002353 if (prev && prev->vm_end > gap_addr &&
2354 (prev->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002355 if (!(prev->vm_flags & VM_GROWSDOWN))
2356 return -ENOMEM;
2357 /* Check that both stack segments have the same anon_vma? */
2358 }
2359
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002360 /* We must make sure the anon_vma is allocated. */
2361 if (unlikely(anon_vma_prepare(vma)))
2362 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 /*
2365 * vma->vm_start/vm_end cannot change under us because the caller
2366 * is required to hold the mmap_sem in read mode. We need the
2367 * anon_vma lock to serialize against concurrent expand_stacks.
2368 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002369 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 /* Somebody else might have raced and expanded it already */
2372 if (address < vma->vm_start) {
2373 unsigned long size, grow;
2374
2375 size = vma->vm_end - address;
2376 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2377
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002378 error = -ENOMEM;
2379 if (grow <= vma->vm_pgoff) {
2380 error = acct_stack_growth(vma, size, grow);
2381 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002382 /*
2383 * vma_gap_update() doesn't support concurrent
2384 * updates, but we only hold a shared mmap_sem
2385 * lock here, so we need to protect against
2386 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002387 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002388 * we don't guarantee that all growable vmas
2389 * in a mm share the same root anon vma.
2390 * So, we reuse mm->page_table_lock to guard
2391 * against concurrent vma expansions.
2392 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002393 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002394 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002395 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002396 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002397 anon_vma_interval_tree_pre_update_vma(vma);
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002398 vma->vm_start = address;
2399 vma->vm_pgoff -= grow;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002400 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002401 vma_gap_update(vma);
Oleg Nesterov09357812015-11-05 18:48:17 -08002402 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002403
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002404 perf_event_mmap(vma);
2405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002408 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002409 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002410 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return error;
2412}
2413
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002414/* enforced gap between the expanding stack and other mappings. */
2415unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
2416
2417static int __init cmdline_parse_stack_guard_gap(char *p)
2418{
2419 unsigned long val;
2420 char *endptr;
2421
2422 val = simple_strtoul(p, &endptr, 10);
2423 if (!*endptr)
2424 stack_guard_gap = val << PAGE_SHIFT;
2425
2426 return 0;
2427}
2428__setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
2429
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002430#ifdef CONFIG_STACK_GROWSUP
2431int expand_stack(struct vm_area_struct *vma, unsigned long address)
2432{
2433 return expand_upwards(vma, address);
2434}
2435
2436struct vm_area_struct *
2437find_extend_vma(struct mm_struct *mm, unsigned long addr)
2438{
2439 struct vm_area_struct *vma, *prev;
2440
2441 addr &= PAGE_MASK;
2442 vma = find_vma_prev(mm, addr, &prev);
2443 if (vma && (vma->vm_start <= addr))
2444 return vma;
Andrea Arcangeli16903f12019-04-18 17:50:52 -07002445 /* don't alter vm_end if the coredump is running */
2446 if (!prev || !mmget_still_valid(mm) || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002447 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002448 if (prev->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002449 populate_vma_page_range(prev, addr, prev->vm_end, NULL);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002450 return prev;
2451}
2452#else
2453int expand_stack(struct vm_area_struct *vma, unsigned long address)
2454{
2455 return expand_downwards(vma, address);
2456}
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458struct vm_area_struct *
vishnu.pscc71aba2014-10-09 15:26:29 -07002459find_extend_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
vishnu.pscc71aba2014-10-09 15:26:29 -07002461 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 unsigned long start;
2463
2464 addr &= PAGE_MASK;
vishnu.pscc71aba2014-10-09 15:26:29 -07002465 vma = find_vma(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (!vma)
2467 return NULL;
2468 if (vma->vm_start <= addr)
2469 return vma;
2470 if (!(vma->vm_flags & VM_GROWSDOWN))
2471 return NULL;
Andrea Arcangeli16903f12019-04-18 17:50:52 -07002472 /* don't alter vm_start if the coredump is running */
2473 if (!mmget_still_valid(mm))
2474 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 start = vma->vm_start;
2476 if (expand_stack(vma, addr))
2477 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002478 if (vma->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002479 populate_vma_page_range(vma, addr, start, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return vma;
2481}
2482#endif
2483
Jesse Barnese1d6d012014-12-12 16:55:27 -08002484EXPORT_SYMBOL_GPL(find_extend_vma);
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002487 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002489 *
2490 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002492static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002494 unsigned long nr_accounted = 0;
2495
Hugh Dickins365e9c872005-10-29 18:16:18 -07002496 /* Update high watermark before we lower total_vm */
2497 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002499 long nrpages = vma_pages(vma);
2500
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002501 if (vma->vm_flags & VM_ACCOUNT)
2502 nr_accounted += nrpages;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002503 vm_stat_account(mm, vma->vm_flags, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002504 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002505 } while (vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002506 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 validate_mm(mm);
2508}
2509
2510/*
2511 * Get rid of page table information in the indicated region.
2512 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002513 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 */
2515static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002516 struct vm_area_struct *vma, struct vm_area_struct *prev,
2517 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
vishnu.pscc71aba2014-10-09 15:26:29 -07002519 struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002520 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07002523 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002524 update_hiwater_rss(mm);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002525 unmap_vmas(&tlb, vma, start, end);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002526 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins6ee86302013-04-29 15:07:44 -07002527 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002528 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
2531/*
2532 * Create a list of vma's touched by the unmap, removing them from the mm's
2533 * vma list as we go..
2534 */
2535static void
2536detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2537 struct vm_area_struct *prev, unsigned long end)
2538{
2539 struct vm_area_struct **insertion_point;
2540 struct vm_area_struct *tail_vma = NULL;
2541
2542 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002543 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 do {
Michel Lespinassed3737182012-12-11 16:01:38 -08002545 vma_rb_erase(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 mm->map_count--;
2547 tail_vma = vma;
2548 vma = vma->vm_next;
2549 } while (vma && vma->vm_start < end);
2550 *insertion_point = vma;
Michel Lespinassed3737182012-12-11 16:01:38 -08002551 if (vma) {
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002552 vma->vm_prev = prev;
Michel Lespinassed3737182012-12-11 16:01:38 -08002553 vma_gap_update(vma);
2554 } else
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002555 mm->highest_vm_end = prev ? vm_end_gap(prev) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 tail_vma->vm_next = NULL;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002557
2558 /* Kill the cache */
2559 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
2562/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002563 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2564 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 */
vishnu.pscc71aba2014-10-09 15:26:29 -07002566static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 unsigned long addr, int new_below)
2568{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct vm_area_struct *new;
Chen Gange3975892015-09-08 15:03:38 -07002570 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Dan Williamscebe1392017-11-29 16:10:28 -08002572 if (vma->vm_ops && vma->vm_ops->split) {
2573 err = vma->vm_ops->split(vma, addr);
2574 if (err)
2575 return err;
2576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Christoph Lametere94b1762006-12-06 20:33:17 -08002578 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (!new)
Chen Gange3975892015-09-08 15:03:38 -07002580 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 /* most fields are the same, copy all, and then fixup */
2583 *new = *vma;
2584
Rik van Riel5beb4932010-03-05 13:42:07 -08002585 INIT_LIST_HEAD(&new->anon_vma_chain);
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (new_below)
2588 new->vm_end = addr;
2589 else {
2590 new->vm_start = addr;
2591 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2592 }
2593
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002594 err = vma_dup_policy(vma, new);
2595 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002596 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Daniel Forrestc4ea95d2014-12-02 15:59:42 -08002598 err = anon_vma_clone(new, vma);
2599 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002600 goto out_free_mpol;
2601
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002602 if (new->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 get_file(new->vm_file);
2604
2605 if (new->vm_ops && new->vm_ops->open)
2606 new->vm_ops->open(new);
2607
2608 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002609 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2611 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002612 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Rik van Riel5beb4932010-03-05 13:42:07 -08002614 /* Success. */
2615 if (!err)
2616 return 0;
2617
2618 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002619 if (new->vm_ops && new->vm_ops->close)
2620 new->vm_ops->close(new);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002621 if (new->vm_file)
Rik van Riel5beb4932010-03-05 13:42:07 -08002622 fput(new->vm_file);
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002623 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002624 out_free_mpol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002625 mpol_put(vma_policy(new));
Rik van Riel5beb4932010-03-05 13:42:07 -08002626 out_free_vma:
2627 kmem_cache_free(vm_area_cachep, new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002628 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002631/*
2632 * Split a vma into two pieces at address 'addr', a new vma is allocated
2633 * either for the first part or the tail.
2634 */
2635int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2636 unsigned long addr, int new_below)
2637{
2638 if (mm->map_count >= sysctl_max_map_count)
2639 return -ENOMEM;
2640
2641 return __split_vma(mm, vma, addr, new_below);
2642}
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644/* Munmap is split into 2 main parts -- this part which finds
2645 * what needs doing, and the areas themselves, which do the
2646 * work. This now handles partial unmappings.
2647 * Jeremy Fitzhardinge <jeremy@goop.org>
2648 */
2649int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2650{
2651 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002652 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002654 if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 return -EINVAL;
2656
vishnu.pscc71aba2014-10-09 15:26:29 -07002657 len = PAGE_ALIGN(len);
2658 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return -EINVAL;
2660
2661 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002662 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002663 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002665 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002666 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 /* if it doesn't overlap, we have nothing.. */
2669 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002670 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return 0;
2672
2673 /*
2674 * If we need to split any vma, do it now to save pain later.
2675 *
2676 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2677 * unmapped vm_area_struct will remain in use: so lower split_vma
2678 * places tmp vma above, and higher split_vma places tmp vma below.
2679 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002680 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002681 int error;
2682
2683 /*
2684 * Make sure that map_count on return from munmap() will
2685 * not exceed its limit; but let map_count go just above
2686 * its limit temporarily, to help free resources as expected.
2687 */
2688 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2689 return -ENOMEM;
2690
2691 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (error)
2693 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002694 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
2696
2697 /* Does it split the last one? */
2698 last = find_vma(mm, end);
2699 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002700 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (error)
2702 return error;
2703 }
vishnu.pscc71aba2014-10-09 15:26:29 -07002704 vma = prev ? prev->vm_next : mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002707 * unlock any mlock()ed ranges before detaching vmas
2708 */
2709 if (mm->locked_vm) {
2710 struct vm_area_struct *tmp = vma;
2711 while (tmp && tmp->vm_start < end) {
2712 if (tmp->vm_flags & VM_LOCKED) {
2713 mm->locked_vm -= vma_pages(tmp);
2714 munlock_vma_pages_all(tmp);
2715 }
2716 tmp = tmp->vm_next;
2717 }
2718 }
2719
2720 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * Remove the vma's, and unmap the actual pages
2722 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002723 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2724 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Dave Hansen1de4fa12014-11-14 07:18:31 -08002726 arch_unmap(mm, vma, start, end);
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002729 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 return 0;
2732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Al Virobfce2812012-04-20 21:57:04 -04002734int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002735{
2736 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002737 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002738
Michal Hockoae798782016-05-23 16:25:33 -07002739 if (down_write_killable(&mm->mmap_sem))
2740 return -EINTR;
2741
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002742 ret = do_munmap(mm, start, len);
2743 up_write(&mm->mmap_sem);
2744 return ret;
2745}
2746EXPORT_SYMBOL(vm_munmap);
2747
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002748SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002750 int ret;
2751 struct mm_struct *mm = current->mm;
2752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 profile_munmap(addr);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002754 if (down_write_killable(&mm->mmap_sem))
2755 return -EINTR;
2756 ret = do_munmap(mm, addr, len);
2757 up_write(&mm->mmap_sem);
2758 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002761
2762/*
2763 * Emulation of deprecated remap_file_pages() syscall.
2764 */
2765SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
2766 unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
2767{
2768
2769 struct mm_struct *mm = current->mm;
2770 struct vm_area_struct *vma;
2771 unsigned long populate = 0;
2772 unsigned long ret = -EINVAL;
2773 struct file *file;
2774
Joe Perches756a025f02016-03-17 14:19:47 -07002775 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
2776 current->comm, current->pid);
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002777
2778 if (prot)
2779 return ret;
2780 start = start & PAGE_MASK;
2781 size = size & PAGE_MASK;
2782
2783 if (start + size <= start)
2784 return ret;
2785
2786 /* Does pgoff wrap? */
2787 if (pgoff + (size >> PAGE_SHIFT) < pgoff)
2788 return ret;
2789
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002790 if (down_write_killable(&mm->mmap_sem))
2791 return -EINTR;
2792
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002793 vma = find_vma(mm, start);
2794
2795 if (!vma || !(vma->vm_flags & VM_SHARED))
2796 goto out;
2797
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002798 if (start < vma->vm_start)
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002799 goto out;
2800
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002801 if (start + size > vma->vm_end) {
2802 struct vm_area_struct *next;
2803
2804 for (next = vma->vm_next; next; next = next->vm_next) {
2805 /* hole between vmas ? */
2806 if (next->vm_start != next->vm_prev->vm_end)
2807 goto out;
2808
2809 if (next->vm_file != vma->vm_file)
2810 goto out;
2811
2812 if (next->vm_flags != vma->vm_flags)
2813 goto out;
2814
2815 if (start + size <= next->vm_end)
2816 break;
2817 }
2818
2819 if (!next)
2820 goto out;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002821 }
2822
2823 prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
2824 prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
2825 prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
2826
2827 flags &= MAP_NONBLOCK;
2828 flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
2829 if (vma->vm_flags & VM_LOCKED) {
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002830 struct vm_area_struct *tmp;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002831 flags |= MAP_LOCKED;
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002832
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002833 /* drop PG_Mlocked flag for over-mapped range */
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002834 for (tmp = vma; tmp->vm_start >= start + size;
2835 tmp = tmp->vm_next) {
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07002836 /*
2837 * Split pmd and munlock page on the border
2838 * of the range.
2839 */
2840 vma_adjust_trans_huge(tmp, start, start + size, 0);
2841
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002842 munlock_vma_pages_range(tmp,
2843 max(tmp->vm_start, start),
2844 min(tmp->vm_end, start + size));
2845 }
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002846 }
2847
2848 file = get_file(vma->vm_file);
2849 ret = do_mmap_pgoff(vma->vm_file, start, size,
2850 prot, flags, pgoff, &populate);
2851 fput(file);
2852out:
2853 up_write(&mm->mmap_sem);
2854 if (populate)
2855 mm_populate(ret, populate);
2856 if (!IS_ERR_VALUE(ret))
2857 ret = 0;
2858 return ret;
2859}
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861static inline void verify_mm_writelocked(struct mm_struct *mm)
2862{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002863#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2865 WARN_ON(1);
2866 up_read(&mm->mmap_sem);
2867 }
2868#endif
2869}
2870
2871/*
2872 * this is really a simplified "do_mmap". it only handles
2873 * anonymous maps. eventually we may be able to do some
2874 * brk-specific accounting here.
2875 */
Michal Hocko1a55a712018-11-13 16:41:56 +00002876static int do_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
vishnu.pscc71aba2014-10-09 15:26:29 -07002878 struct mm_struct *mm = current->mm;
2879 struct vm_area_struct *vma, *prev;
Michal Hocko1a55a712018-11-13 16:41:56 +00002880 unsigned long flags;
vishnu.pscc71aba2014-10-09 15:26:29 -07002881 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002883 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Kirill Korotaev3a459752006-09-07 14:17:04 +04002885 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2886
Al Viro2c6a1012009-12-03 19:40:46 -05002887 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002888 if (offset_in_page(error))
Kirill Korotaev3a459752006-09-07 14:17:04 +04002889 return error;
2890
Davidlohr Bueso363ee172014-01-21 15:49:15 -08002891 error = mlock_future_check(mm, mm->def_flags, len);
2892 if (error)
2893 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 /*
2896 * mm->mmap_sem is required to protect against another thread
2897 * changing the mappings in case we sleep.
2898 */
2899 verify_mm_writelocked(mm);
2900
2901 /*
2902 * Clear old maps. this also does some error checking for us
2903 */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07002904 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
2905 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (do_munmap(mm, addr, len))
2907 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
2909
2910 /* Check against address space limits *after* clearing old maps... */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002911 if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return -ENOMEM;
2913
2914 if (mm->map_count > sysctl_max_map_count)
2915 return -ENOMEM;
2916
Al Viro191c5422012-02-13 03:58:52 +00002917 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return -ENOMEM;
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07002921 vma = vma_merge(mm, prev, addr, addr + len, flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07002922 NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX);
Rik van Rielba470de2008-10-18 20:26:50 -07002923 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 goto out;
2925
2926 /*
2927 * create a vma struct for an anonymous mapping
2928 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002929 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (!vma) {
2931 vm_unacct_memory(len >> PAGE_SHIFT);
2932 return -ENOMEM;
2933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Rik van Riel5beb4932010-03-05 13:42:07 -08002935 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 vma->vm_mm = mm;
2937 vma->vm_start = addr;
2938 vma->vm_end = addr + len;
2939 vma->vm_pgoff = pgoff;
2940 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07002941 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 vma_link(mm, vma, prev, rb_link, rb_parent);
2943out:
Eric B Munson3af9e852010-05-18 15:30:49 +01002944 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 mm->total_vm += len >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002946 mm->data_vm += len >> PAGE_SHIFT;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002947 if (flags & VM_LOCKED)
2948 mm->locked_vm += (len >> PAGE_SHIFT);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07002949 vma->vm_flags |= VM_SOFTDIRTY;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
Michal Hocko1a55a712018-11-13 16:41:56 +00002953int vm_brk(unsigned long addr, unsigned long request)
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002954{
2955 struct mm_struct *mm = current->mm;
Michal Hocko1a55a712018-11-13 16:41:56 +00002956 unsigned long len;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002957 int ret;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002958 bool populate;
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002959
Michal Hocko1a55a712018-11-13 16:41:56 +00002960 len = PAGE_ALIGN(request);
2961 if (len < request)
2962 return -ENOMEM;
2963 if (!len)
2964 return 0;
2965
Michal Hocko2d6c9282016-05-23 16:25:42 -07002966 if (down_write_killable(&mm->mmap_sem))
2967 return -EINTR;
2968
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002969 ret = do_brk(addr, len);
Michel Lespinasse128557f2013-02-22 16:32:40 -08002970 populate = ((mm->def_flags & VM_LOCKED) != 0);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002971 up_write(&mm->mmap_sem);
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002972 if (populate && !ret)
Michel Lespinasse128557f2013-02-22 16:32:40 -08002973 mm_populate(addr, len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002974 return ret;
2975}
2976EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978/* Release all mmaps. */
2979void exit_mmap(struct mm_struct *mm)
2980{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002981 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07002982 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 unsigned long nr_accounted = 0;
2984
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002985 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002986 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002987
Rik van Rielba470de2008-10-18 20:26:50 -07002988 if (mm->locked_vm) {
2989 vma = mm->mmap;
2990 while (vma) {
2991 if (vma->vm_flags & VM_LOCKED)
2992 munlock_vma_pages_all(vma);
2993 vma = vma->vm_next;
2994 }
2995 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002996
2997 arch_exit_mmap(mm);
2998
Rik van Rielba470de2008-10-18 20:26:50 -07002999 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08003000 if (!vma) /* Can happen if dup_mmap() received an OOM */
3001 return;
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 flush_cache_mm(mm);
Linus Torvalds2b047252013-08-15 11:42:25 -07003005 tlb_gather_mmu(&tlb, mm, 0, -1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08003006 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07003007 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003008 unmap_vmas(&tlb, vma, 0, -1);
Hugh Dickins9ba69292009-09-21 17:02:20 -07003009
Hugh Dickins6ee86302013-04-29 15:07:44 -07003010 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05003011 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07003014 * Walk the list again, actually closing and freeing it,
3015 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003017 while (vma) {
3018 if (vma->vm_flags & VM_ACCOUNT)
3019 nr_accounted += vma_pages(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07003020 vma = remove_vma(vma);
Paul E. McKenney261da4b2020-04-16 16:46:10 -07003021 cond_resched();
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003022 }
3023 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024}
3025
3026/* Insert vm structure into process list sorted by address
3027 * and into the inode's i_mmap tree. If vm_file is non-NULL
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003028 * then i_mmap_rwsem is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 */
Hugh Dickins6597d782012-10-08 16:29:07 -07003030int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Hugh Dickins6597d782012-10-08 16:29:07 -07003032 struct vm_area_struct *prev;
3033 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Chen Gangc9d13f52015-09-08 15:04:08 -07003035 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
3036 &prev, &rb_link, &rb_parent))
3037 return -ENOMEM;
3038 if ((vma->vm_flags & VM_ACCOUNT) &&
3039 security_vm_enough_memory_mm(mm, vma_pages(vma)))
3040 return -ENOMEM;
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /*
3043 * The vm_pgoff of a purely anonymous vma should be irrelevant
3044 * until its first write fault, when page's anon_vma and index
3045 * are set. But now set the vm_pgoff it will almost certainly
3046 * end up with (unless mremap moves it elsewhere before that
3047 * first wfault), so /proc/pid/maps tells a consistent story.
3048 *
3049 * By setting it to reflect the virtual start address of the
3050 * vma, merges and splits can happen in a seamless way, just
3051 * using the existing file pgoff checks and manipulations.
3052 * Similarly in do_mmap_pgoff and in do_brk.
3053 */
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07003054 if (vma_is_anonymous(vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 BUG_ON(vma->anon_vma);
3056 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
3057 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05303058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 vma_link(mm, vma, prev, rb_link, rb_parent);
3060 return 0;
3061}
3062
3063/*
3064 * Copy the vma structure to a new location in the same mm,
3065 * prior to moving page table entries, to effect an mremap move.
3066 */
3067struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
Michel Lespinasse38a76012012-10-08 16:31:50 -07003068 unsigned long addr, unsigned long len, pgoff_t pgoff,
3069 bool *need_rmap_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
3071 struct vm_area_struct *vma = *vmap;
3072 unsigned long vma_start = vma->vm_start;
3073 struct mm_struct *mm = vma->vm_mm;
3074 struct vm_area_struct *new_vma, *prev;
3075 struct rb_node **rb_link, *rb_parent;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003076 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 /*
3079 * If anonymous vma has not yet been faulted, update new pgoff
3080 * to match new location, to increase its chance of merging.
3081 */
Oleg Nesterovce757992015-09-08 14:58:34 -07003082 if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003084 faulted_in_anon_vma = false;
3085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Hugh Dickins6597d782012-10-08 16:29:07 -07003087 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
3088 return NULL; /* should never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07003090 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
3091 vma->vm_userfaultfd_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 if (new_vma) {
3093 /*
3094 * Source vma may have been merged into new_vma
3095 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003096 if (unlikely(vma_start >= new_vma->vm_start &&
3097 vma_start < new_vma->vm_end)) {
3098 /*
3099 * The only way we can get a vma_merge with
3100 * self during an mremap is if the vma hasn't
3101 * been faulted in yet and we were allowed to
3102 * reset the dst vma->vm_pgoff to the
3103 * destination address of the mremap to allow
3104 * the merge to happen. mremap must change the
3105 * vm_pgoff linearity between src and dst vmas
3106 * (in turn preventing a vma_merge) to be
3107 * safe. It is only safe to keep the vm_pgoff
3108 * linear if there are no pages mapped yet.
3109 */
Sasha Levin81d1b092014-10-09 15:28:10 -07003110 VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
Michel Lespinasse38a76012012-10-08 16:31:50 -07003111 *vmap = vma = new_vma;
Michel Lespinasse108d6642012-10-08 16:31:36 -07003112 }
Michel Lespinasse38a76012012-10-08 16:31:50 -07003113 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08003115 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Chen Gange3975892015-09-08 15:03:38 -07003116 if (!new_vma)
3117 goto out;
3118 *new_vma = *vma;
3119 new_vma->vm_start = addr;
3120 new_vma->vm_end = addr + len;
3121 new_vma->vm_pgoff = pgoff;
3122 if (vma_dup_policy(vma, new_vma))
3123 goto out_free_vma;
3124 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
3125 if (anon_vma_clone(new_vma, vma))
3126 goto out_free_mempol;
3127 if (new_vma->vm_file)
3128 get_file(new_vma->vm_file);
3129 if (new_vma->vm_ops && new_vma->vm_ops->open)
3130 new_vma->vm_ops->open(new_vma);
3131 vma_link(mm, new_vma, prev, rb_link, rb_parent);
3132 *need_rmap_locks = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
3134 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003135
Chen Gange3975892015-09-08 15:03:38 -07003136out_free_mempol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07003137 mpol_put(vma_policy(new_vma));
Chen Gange3975892015-09-08 15:03:38 -07003138out_free_vma:
Rik van Riel5beb4932010-03-05 13:42:07 -08003139 kmem_cache_free(vm_area_cachep, new_vma);
Chen Gange3975892015-09-08 15:03:38 -07003140out:
Rik van Riel5beb4932010-03-05 13:42:07 -08003141 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
akpm@osdl.org119f6572005-05-01 08:58:35 -07003143
3144/*
3145 * Return true if the calling process may expand its vm space by the passed
3146 * number of pages
3147 */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003148bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
akpm@osdl.org119f6572005-05-01 08:58:35 -07003149{
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003150 if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
3151 return false;
akpm@osdl.org119f6572005-05-01 08:58:35 -07003152
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003153 if (is_data_mapping(flags) &&
3154 mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07003155 /* Workaround for Valgrind */
3156 if (rlimit(RLIMIT_DATA) == 0 &&
3157 mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT)
3158 return true;
3159 if (!ignore_rlimit_data) {
3160 pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits or use boot option ignore_rlimit_data.\n",
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003161 current->comm, current->pid,
3162 (mm->data_vm + npages) << PAGE_SHIFT,
3163 rlimit(RLIMIT_DATA));
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003164 return false;
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07003165 }
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003166 }
akpm@osdl.org119f6572005-05-01 08:58:35 -07003167
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003168 return true;
3169}
3170
3171void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
3172{
3173 mm->total_vm += npages;
3174
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003175 if (is_exec_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003176 mm->exec_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003177 else if (is_stack_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003178 mm->stack_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003179 else if (is_data_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003180 mm->data_vm += npages;
akpm@osdl.org119f6572005-05-01 08:58:35 -07003181}
Roland McGrathfa5dc222007-02-08 14:20:41 -08003182
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003183static int special_mapping_fault(struct vm_area_struct *vma,
3184 struct vm_fault *vmf);
3185
3186/*
3187 * Having a close hook prevents vma merging regardless of flags.
3188 */
3189static void special_mapping_close(struct vm_area_struct *vma)
3190{
3191}
3192
3193static const char *special_mapping_name(struct vm_area_struct *vma)
3194{
3195 return ((struct vm_special_mapping *)vma->vm_private_data)->name;
3196}
3197
Dmitry Safonovb059a452016-06-28 14:35:38 +03003198static int special_mapping_mremap(struct vm_area_struct *new_vma)
3199{
3200 struct vm_special_mapping *sm = new_vma->vm_private_data;
3201
3202 if (sm->mremap)
3203 return sm->mremap(sm, new_vma);
3204 return 0;
3205}
3206
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003207static const struct vm_operations_struct special_mapping_vmops = {
3208 .close = special_mapping_close,
3209 .fault = special_mapping_fault,
Dmitry Safonovb059a452016-06-28 14:35:38 +03003210 .mremap = special_mapping_mremap,
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003211 .name = special_mapping_name,
3212};
3213
3214static const struct vm_operations_struct legacy_special_mapping_vmops = {
3215 .close = special_mapping_close,
3216 .fault = special_mapping_fault,
3217};
Roland McGrathfa5dc222007-02-08 14:20:41 -08003218
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003219static int special_mapping_fault(struct vm_area_struct *vma,
3220 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003221{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003222 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003223 struct page **pages;
3224
Andy Lutomirskif872f542015-12-29 20:12:19 -08003225 if (vma->vm_ops == &legacy_special_mapping_vmops) {
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003226 pages = vma->vm_private_data;
Andy Lutomirskif872f542015-12-29 20:12:19 -08003227 } else {
3228 struct vm_special_mapping *sm = vma->vm_private_data;
3229
3230 if (sm->fault)
3231 return sm->fault(sm, vma, vmf);
3232
3233 pages = sm->pages;
3234 }
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003235
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07003236 for (pgoff = vmf->pgoff; pgoff && *pages; ++pages)
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003237 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003238
3239 if (*pages) {
3240 struct page *page = *pages;
3241 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003242 vmf->page = page;
3243 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003244 }
3245
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003246 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003247}
3248
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003249static struct vm_area_struct *__install_special_mapping(
3250 struct mm_struct *mm,
3251 unsigned long addr, unsigned long len,
Chen Gang27f28b92015-11-05 18:48:41 -08003252 unsigned long vm_flags, void *priv,
3253 const struct vm_operations_struct *ops)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003254{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003255 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003256 struct vm_area_struct *vma;
3257
3258 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
3259 if (unlikely(vma == NULL))
Stefani Seibold3935ed62014-03-17 23:22:02 +01003260 return ERR_PTR(-ENOMEM);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003261
Rik van Riel5beb4932010-03-05 13:42:07 -08003262 INIT_LIST_HEAD(&vma->anon_vma_chain);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003263 vma->vm_mm = mm;
3264 vma->vm_start = addr;
3265 vma->vm_end = addr + len;
3266
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07003267 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY;
Coly Li3ed75eb2007-10-18 23:39:15 -07003268 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003269
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003270 vma->vm_ops = ops;
3271 vma->vm_private_data = priv;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003272
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003273 ret = insert_vm_struct(mm, vma);
3274 if (ret)
3275 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003276
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003277 vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003278
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003279 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02003280
Stefani Seibold3935ed62014-03-17 23:22:02 +01003281 return vma;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003282
3283out:
3284 kmem_cache_free(vm_area_cachep, vma);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003285 return ERR_PTR(ret);
3286}
3287
Dmitry Safonov2eefd872016-09-05 16:33:05 +03003288bool vma_is_special_mapping(const struct vm_area_struct *vma,
3289 const struct vm_special_mapping *sm)
3290{
3291 return vma->vm_private_data == sm &&
3292 (vma->vm_ops == &special_mapping_vmops ||
3293 vma->vm_ops == &legacy_special_mapping_vmops);
3294}
3295
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003296/*
3297 * Called with mm->mmap_sem held for writing.
3298 * Insert a new vma covering the given region, with the given flags.
3299 * Its pages are supplied by the given array of struct page *.
3300 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3301 * The region past the last page supplied will always produce SIGBUS.
3302 * The array pointer and the pages it points to are assumed to stay alive
3303 * for as long as this mapping might exist.
3304 */
3305struct vm_area_struct *_install_special_mapping(
3306 struct mm_struct *mm,
3307 unsigned long addr, unsigned long len,
3308 unsigned long vm_flags, const struct vm_special_mapping *spec)
3309{
Chen Gang27f28b92015-11-05 18:48:41 -08003310 return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
3311 &special_mapping_vmops);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003312}
3313
Stefani Seibold3935ed62014-03-17 23:22:02 +01003314int install_special_mapping(struct mm_struct *mm,
3315 unsigned long addr, unsigned long len,
3316 unsigned long vm_flags, struct page **pages)
3317{
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003318 struct vm_area_struct *vma = __install_special_mapping(
Chen Gang27f28b92015-11-05 18:48:41 -08003319 mm, addr, len, vm_flags, (void *)pages,
3320 &legacy_special_mapping_vmops);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003321
Duan Jiong14bd5b42014-06-04 16:07:05 -07003322 return PTR_ERR_OR_ZERO(vma);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003323}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003324
3325static DEFINE_MUTEX(mm_all_locks_mutex);
3326
Peter Zijlstra454ed842008-08-11 09:30:25 +02003327static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003328{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003329 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003330 /*
3331 * The LSB of head.next can't change from under us
3332 * because we hold the mm_all_locks_mutex.
3333 */
Jiri Kosina572043c2013-01-11 14:31:59 -08003334 down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003335 /*
3336 * We can safely modify head.next after taking the
Ingo Molnar5a505082012-12-02 19:56:46 +00003337 * anon_vma->root->rwsem. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003338 * the same anon_vma we won't take it again.
3339 *
3340 * No need of atomic instructions here, head.next
3341 * can't change from under us thanks to the
Ingo Molnar5a505082012-12-02 19:56:46 +00003342 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003343 */
3344 if (__test_and_set_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003345 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003346 BUG();
3347 }
3348}
3349
Peter Zijlstra454ed842008-08-11 09:30:25 +02003350static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003351{
3352 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3353 /*
3354 * AS_MM_ALL_LOCKS can't change from under us because
3355 * we hold the mm_all_locks_mutex.
3356 *
3357 * Operations on ->flags have to be atomic because
3358 * even if AS_MM_ALL_LOCKS is stable thanks to the
3359 * mm_all_locks_mutex, there may be other cpus
3360 * changing other bitflags in parallel to us.
3361 */
3362 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
3363 BUG();
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003364 down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003365 }
3366}
3367
3368/*
3369 * This operation locks against the VM for all pte/vma/mm related
3370 * operations that could ever happen on a certain mm. This includes
3371 * vmtruncate, try_to_unmap, and all page faults.
3372 *
3373 * The caller must take the mmap_sem in write mode before calling
3374 * mm_take_all_locks(). The caller isn't allowed to release the
3375 * mmap_sem until mm_drop_all_locks() returns.
3376 *
3377 * mmap_sem in write mode is required in order to block all operations
3378 * that could modify pagetables and free pages without need of
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -08003379 * altering the vma layout. It's also needed in write mode to avoid new
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003380 * anon_vmas to be associated with existing vmas.
3381 *
3382 * A single task can't take more than one mm_take_all_locks() in a row
3383 * or it would deadlock.
3384 *
Michel Lespinassebf181b92012-10-08 16:31:39 -07003385 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003386 * mapping->flags avoid to take the same lock twice, if more than one
3387 * vma in this mm is backed by the same anon_vma or address_space.
3388 *
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003389 * We take locks in following order, accordingly to comment at beginning
3390 * of mm/rmap.c:
3391 * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
3392 * hugetlb mapping);
3393 * - all i_mmap_rwsem locks;
3394 * - all anon_vma->rwseml
3395 *
3396 * We can take all locks within these types randomly because the VM code
3397 * doesn't nest them and we protected from parallel mm_take_all_locks() by
3398 * mm_all_locks_mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003399 *
3400 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3401 * that may have to take thousand of locks.
3402 *
3403 * mm_take_all_locks() can fail if it's interrupted by signals.
3404 */
3405int mm_take_all_locks(struct mm_struct *mm)
3406{
3407 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003408 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003409
3410 BUG_ON(down_read_trylock(&mm->mmap_sem));
3411
3412 mutex_lock(&mm_all_locks_mutex);
3413
3414 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3415 if (signal_pending(current))
3416 goto out_unlock;
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003417 if (vma->vm_file && vma->vm_file->f_mapping &&
3418 is_vm_hugetlb_page(vma))
3419 vm_lock_mapping(mm, vma->vm_file->f_mapping);
3420 }
3421
3422 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3423 if (signal_pending(current))
3424 goto out_unlock;
3425 if (vma->vm_file && vma->vm_file->f_mapping &&
3426 !is_vm_hugetlb_page(vma))
Peter Zijlstra454ed842008-08-11 09:30:25 +02003427 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003428 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003429
3430 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3431 if (signal_pending(current))
3432 goto out_unlock;
3433 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003434 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3435 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003436 }
3437
Kautuk Consul584cff52011-10-31 17:08:59 -07003438 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003439
3440out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07003441 mm_drop_all_locks(mm);
3442 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003443}
3444
3445static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
3446{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003447 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003448 /*
3449 * The LSB of head.next can't change to 0 from under
3450 * us because we hold the mm_all_locks_mutex.
3451 *
3452 * We must however clear the bitflag before unlocking
Michel Lespinassebf181b92012-10-08 16:31:39 -07003453 * the vma so the users using the anon_vma->rb_root will
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003454 * never see our bitflag.
3455 *
3456 * No need of atomic instructions here, head.next
3457 * can't change from under us until we release the
Ingo Molnar5a505082012-12-02 19:56:46 +00003458 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003459 */
3460 if (!__test_and_clear_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003461 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003462 BUG();
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08003463 anon_vma_unlock_write(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003464 }
3465}
3466
3467static void vm_unlock_mapping(struct address_space *mapping)
3468{
3469 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3470 /*
3471 * AS_MM_ALL_LOCKS can't change to 0 from under us
3472 * because we hold the mm_all_locks_mutex.
3473 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003474 i_mmap_unlock_write(mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003475 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
3476 &mapping->flags))
3477 BUG();
3478 }
3479}
3480
3481/*
3482 * The mmap_sem cannot be released by the caller until
3483 * mm_drop_all_locks() returns.
3484 */
3485void mm_drop_all_locks(struct mm_struct *mm)
3486{
3487 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003488 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003489
3490 BUG_ON(down_read_trylock(&mm->mmap_sem));
3491 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
3492
3493 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3494 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003495 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3496 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003497 if (vma->vm_file && vma->vm_file->f_mapping)
3498 vm_unlock_mapping(vma->vm_file->f_mapping);
3499 }
3500
3501 mutex_unlock(&mm_all_locks_mutex);
3502}
David Howells8feae132009-01-08 12:04:47 +00003503
3504/*
3505 * initialise the VMA slab
3506 */
3507void __init mmap_init(void)
3508{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003509 int ret;
3510
Tejun Heo908c7f12014-09-08 09:51:29 +09003511 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003512 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00003513}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003514
3515/*
3516 * Initialise sysctl_user_reserve_kbytes.
3517 *
3518 * This is intended to prevent a user from starting a single memory hogging
3519 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3520 * mode.
3521 *
3522 * The default value is min(3% of free memory, 128MB)
3523 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3524 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003525static int init_user_reserve(void)
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003526{
3527 unsigned long free_kbytes;
3528
3529 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3530
3531 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
3532 return 0;
3533}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003534subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003535
3536/*
3537 * Initialise sysctl_admin_reserve_kbytes.
3538 *
3539 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3540 * to log in and kill a memory hogging process.
3541 *
3542 * Systems with more than 256MB will reserve 8MB, enough to recover
3543 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3544 * only reserve 3% of free pages by default.
3545 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003546static int init_admin_reserve(void)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003547{
3548 unsigned long free_kbytes;
3549
3550 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3551
3552 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
3553 return 0;
3554}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003555subsys_initcall(init_admin_reserve);
Andrew Shewmaker16408792013-04-29 15:08:12 -07003556
3557/*
3558 * Reinititalise user and admin reserves if memory is added or removed.
3559 *
3560 * The default user reserve max is 128MB, and the default max for the
3561 * admin reserve is 8MB. These are usually, but not always, enough to
3562 * enable recovery from a memory hogging process using login/sshd, a shell,
3563 * and tools like top. It may make sense to increase or even disable the
3564 * reserve depending on the existence of swap or variations in the recovery
3565 * tools. So, the admin may have changed them.
3566 *
3567 * If memory is added and the reserves have been eliminated or increased above
3568 * the default max, then we'll trust the admin.
3569 *
3570 * If memory is removed and there isn't enough free memory, then we
3571 * need to reset the reserves.
3572 *
3573 * Otherwise keep the reserve set by the admin.
3574 */
3575static int reserve_mem_notifier(struct notifier_block *nb,
3576 unsigned long action, void *data)
3577{
3578 unsigned long tmp, free_kbytes;
3579
3580 switch (action) {
3581 case MEM_ONLINE:
3582 /* Default max is 128MB. Leave alone if modified by operator. */
3583 tmp = sysctl_user_reserve_kbytes;
3584 if (0 < tmp && tmp < (1UL << 17))
3585 init_user_reserve();
3586
3587 /* Default max is 8MB. Leave alone if modified by operator. */
3588 tmp = sysctl_admin_reserve_kbytes;
3589 if (0 < tmp && tmp < (1UL << 13))
3590 init_admin_reserve();
3591
3592 break;
3593 case MEM_OFFLINE:
3594 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3595
3596 if (sysctl_user_reserve_kbytes > free_kbytes) {
3597 init_user_reserve();
3598 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3599 sysctl_user_reserve_kbytes);
3600 }
3601
3602 if (sysctl_admin_reserve_kbytes > free_kbytes) {
3603 init_admin_reserve();
3604 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3605 sysctl_admin_reserve_kbytes);
3606 }
3607 break;
3608 default:
3609 break;
3610 }
3611 return NOTIFY_OK;
3612}
3613
3614static struct notifier_block reserve_mem_nb = {
3615 .notifier_call = reserve_mem_notifier,
3616};
3617
3618static int __meminit init_reserve_notifier(void)
3619{
3620 if (register_hotmemory_notifier(&reserve_mem_nb))
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07003621 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
Andrew Shewmaker16408792013-04-29 15:08:12 -07003622
3623 return 0;
3624}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003625subsys_initcall(init_reserve_notifier);