blob: 9ba15d8242cc6b784507cef185bf1f2635797314 [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>
Andrea Arcangeli07b039e92017-09-06 16:25:00 -070047#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/uaccess.h>
50#include <asm/cacheflush.h>
51#include <asm/tlb.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020052#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jan Beulich42b77722008-07-23 21:27:10 -070054#include "internal.h"
55
Kirill Korotaev3a459752006-09-07 14:17:04 +040056#ifndef arch_mmap_check
57#define arch_mmap_check(addr, len, flags) (0)
58#endif
59
Daniel Cashmand07e2252016-01-14 15:19:53 -080060#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
61const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
62const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
63int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
64#endif
65#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
66const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
67const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
68int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
69#endif
70
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -070071static bool ignore_rlimit_data;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -080072core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644);
Daniel Cashmand07e2252016-01-14 15:19:53 -080073
Hugh Dickinse0da3822005-04-19 13:29:15 -070074static void unmap_region(struct mm_struct *mm,
75 struct vm_area_struct *vma, struct vm_area_struct *prev,
76 unsigned long start, unsigned long end);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* description of effects of mapping type and prot in current implementation.
79 * this is due to the limited x86 page protection hardware. The expected
80 * behavior is in parens:
81 *
82 * map_type prot
83 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
84 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
85 * w: (no) no w: (no) no w: (yes) yes w: (no) no
86 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
vishnu.pscc71aba2014-10-09 15:26:29 -070087 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
89 * w: (no) no w: (no) no w: (copy) copy w: (no) no
90 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
91 *
Catalin Marinascab15ce2016-08-11 18:44:50 +010092 * On arm64, PROT_EXEC has the following behaviour for both MAP_SHARED and
93 * MAP_PRIVATE:
94 * r: (no) no
95 * w: (no) no
96 * x: (yes) yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98pgprot_t protection_map[16] = {
99 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
100 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
101};
102
Hugh Dickins804af2c2006-07-26 21:39:49 +0100103pgprot_t vm_get_page_prot(unsigned long vm_flags)
104{
Dave Kleikampb845f312008-07-08 00:28:51 +1000105 return __pgprot(pgprot_val(protection_map[vm_flags &
106 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
107 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +0100108}
109EXPORT_SYMBOL(vm_get_page_prot);
110
Peter Feiner64e45502014-10-13 15:55:46 -0700111static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags)
112{
113 return pgprot_modify(oldprot, vm_get_page_prot(vm_flags));
114}
115
116/* Update vma->vm_page_prot to reflect vma->vm_flags. */
117void vma_set_page_prot(struct vm_area_struct *vma)
118{
119 unsigned long vm_flags = vma->vm_flags;
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700120 pgprot_t vm_page_prot;
Peter Feiner64e45502014-10-13 15:55:46 -0700121
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700122 vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags);
123 if (vma_wants_writenotify(vma, vm_page_prot)) {
Peter Feiner64e45502014-10-13 15:55:46 -0700124 vm_flags &= ~VM_SHARED;
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700125 vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags);
Peter Feiner64e45502014-10-13 15:55:46 -0700126 }
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -0700127 /* remove_protection_ptes reads vma->vm_page_prot without mmap_sem */
128 WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
Peter Feiner64e45502014-10-13 15:55:46 -0700129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800132 * Requires inode->i_mapping->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134static void __remove_shared_vm_struct(struct vm_area_struct *vma,
135 struct file *file, struct address_space *mapping)
136{
137 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500138 atomic_inc(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700140 mapping_unmap_writable(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 flush_dcache_mmap_lock(mapping);
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800143 vma_interval_tree_remove(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 flush_dcache_mmap_unlock(mapping);
145}
146
147/*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700148 * Unlink a file-based vm structure from its interval tree, to hide
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700149 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700151void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct file *file = vma->vm_file;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (file) {
156 struct address_space *mapping = file->f_mapping;
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800157 i_mmap_lock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 __remove_shared_vm_struct(vma, file, mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800159 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700161}
162
Laurent Dufourd8173842018-04-17 16:33:23 +0200163static void __free_vma(struct vm_area_struct *vma)
164{
165 if (vma->vm_file)
166 fput(vma->vm_file);
167 mpol_put(vma_policy(vma));
168 kmem_cache_free(vm_area_cachep, vma);
169}
170
171#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
172void put_vma(struct vm_area_struct *vma)
173{
174 if (atomic_dec_and_test(&vma->vm_ref_count))
175 __free_vma(vma);
176}
177#else
178static inline void put_vma(struct vm_area_struct *vma)
179{
180 __free_vma(vma);
181}
182#endif
183
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700184/*
185 * Close a vm structure and free it, returning the next.
186 */
187static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
188{
189 struct vm_area_struct *next = vma->vm_next;
190
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700191 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (vma->vm_ops && vma->vm_ops->close)
193 vma->vm_ops->close(vma);
Laurent Dufourd8173842018-04-17 16:33:23 +0200194 put_vma(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700195 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Linus Torvalds5d22fc22016-05-27 15:57:31 -0700198static int do_brk(unsigned long addr, unsigned long len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -0700199
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100200SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700202 unsigned long retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 unsigned long newbrk, oldbrk;
204 struct mm_struct *mm = current->mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700205 struct vm_area_struct *next;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700206 unsigned long min_brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800207 bool populate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700209 if (down_write_killable(&mm->mmap_sem))
210 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700212#ifdef CONFIG_COMPAT_BRK
Jiri Kosina5520e892011-01-13 15:47:23 -0800213 /*
214 * CONFIG_COMPAT_BRK can still be overridden by setting
215 * randomize_va_space to 2, which will still cause mm->start_brk
216 * to be arbitrarily shifted
217 */
Jiri Kosina4471a672011-04-14 15:22:09 -0700218 if (current->brk_randomized)
Jiri Kosina5520e892011-01-13 15:47:23 -0800219 min_brk = mm->start_brk;
220 else
221 min_brk = mm->end_data;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700222#else
223 min_brk = mm->start_brk;
224#endif
225 if (brk < min_brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700227
228 /*
229 * Check against rlimit here. If this check is done later after the test
230 * of oldbrk with newbrk then it can escape the test and let the data
231 * segment grow beyond its set limit the in case where the limit is
232 * not page aligned -Ram Gupta
233 */
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700234 if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk,
235 mm->end_data, mm->start_data))
Ram Gupta1e624192006-04-10 22:52:57 -0700236 goto out;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 newbrk = PAGE_ALIGN(brk);
239 oldbrk = PAGE_ALIGN(mm->brk);
240 if (oldbrk == newbrk)
241 goto set_brk;
242
243 /* Always allow shrinking brk. */
244 if (brk <= mm->brk) {
245 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
246 goto set_brk;
247 goto out;
248 }
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* Check against existing mmap mappings. */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700251 next = find_vma(mm, oldbrk);
252 if (next && newbrk + PAGE_SIZE > vm_start_gap(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto out;
254
255 /* Ok, looks good - let it rip. */
Linus Torvalds5d22fc22016-05-27 15:57:31 -0700256 if (do_brk(oldbrk, newbrk-oldbrk) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto out;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259set_brk:
260 mm->brk = brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800261 populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0;
262 up_write(&mm->mmap_sem);
263 if (populate)
264 mm_populate(oldbrk, newbrk - oldbrk);
265 return brk;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267out:
268 retval = mm->brk;
269 up_write(&mm->mmap_sem);
270 return retval;
271}
272
Michel Lespinassed3737182012-12-11 16:01:38 -0800273static long vma_compute_subtree_gap(struct vm_area_struct *vma)
274{
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700275 unsigned long max, prev_end, subtree_gap;
276
277 /*
278 * Note: in the rare case of a VM_GROWSDOWN above a VM_GROWSUP, we
279 * allow two stack_guard_gaps between them here, and when choosing
280 * an unmapped area; whereas when expanding we only require one.
281 * That's a little inconsistent, but keeps the code here simpler.
282 */
283 max = vm_start_gap(vma);
284 if (vma->vm_prev) {
285 prev_end = vm_end_gap(vma->vm_prev);
286 if (max > prev_end)
287 max -= prev_end;
288 else
289 max = 0;
290 }
Michel Lespinassed3737182012-12-11 16:01:38 -0800291 if (vma->vm_rb.rb_left) {
292 subtree_gap = rb_entry(vma->vm_rb.rb_left,
293 struct vm_area_struct, vm_rb)->rb_subtree_gap;
294 if (subtree_gap > max)
295 max = subtree_gap;
296 }
297 if (vma->vm_rb.rb_right) {
298 subtree_gap = rb_entry(vma->vm_rb.rb_right,
299 struct vm_area_struct, vm_rb)->rb_subtree_gap;
300 if (subtree_gap > max)
301 max = subtree_gap;
302 }
303 return max;
304}
305
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700306#ifdef CONFIG_DEBUG_VM_RB
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800307static int browse_rb(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800309 struct rb_root *root = &mm->mm_rb;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800310 int i = 0, j, bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct rb_node *nd, *pn = NULL;
312 unsigned long prev = 0, pend = 0;
313
314 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
315 struct vm_area_struct *vma;
316 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800317 if (vma->vm_start < prev) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700318 pr_emerg("vm_start %lx < prev %lx\n",
319 vma->vm_start, prev);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800320 bug = 1;
321 }
322 if (vma->vm_start < pend) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700323 pr_emerg("vm_start %lx < pend %lx\n",
324 vma->vm_start, pend);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800325 bug = 1;
326 }
327 if (vma->vm_start > vma->vm_end) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700328 pr_emerg("vm_start %lx > vm_end %lx\n",
329 vma->vm_start, vma->vm_end);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800330 bug = 1;
331 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800332 spin_lock(&mm->page_table_lock);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800333 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700334 pr_emerg("free gap %lx, correct %lx\n",
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800335 vma->rb_subtree_gap,
336 vma_compute_subtree_gap(vma));
337 bug = 1;
338 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800339 spin_unlock(&mm->page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 i++;
341 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800342 prev = vma->vm_start;
343 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 j = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800346 for (nd = pn; nd; nd = rb_prev(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 j++;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800348 if (i != j) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700349 pr_emerg("backwards %d, forwards %d\n", j, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800350 bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800352 return bug ? -1 : i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Michel Lespinassed3737182012-12-11 16:01:38 -0800355static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
356{
357 struct rb_node *nd;
358
359 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
360 struct vm_area_struct *vma;
361 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Sasha Levin96dad672014-10-09 15:28:39 -0700362 VM_BUG_ON_VMA(vma != ignore &&
363 vma->rb_subtree_gap != vma_compute_subtree_gap(vma),
364 vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Rashika Kheriaeafd4dc2014-04-03 14:48:03 -0700368static void validate_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 int bug = 0;
371 int i = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800372 unsigned long highest_address = 0;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700373 struct vm_area_struct *vma = mm->mmap;
Andrew Mortonff26f702014-10-09 15:28:19 -0700374
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700375 while (vma) {
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -0800376 struct anon_vma *anon_vma = vma->anon_vma;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700377 struct anon_vma_chain *avc;
Andrew Mortonff26f702014-10-09 15:28:19 -0700378
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -0800379 if (anon_vma) {
380 anon_vma_lock_read(anon_vma);
381 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
382 anon_vma_interval_tree_verify(avc);
383 anon_vma_unlock_read(anon_vma);
384 }
385
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700386 highest_address = vm_end_gap(vma);
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700387 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 i++;
389 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800390 if (i != mm->map_count) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700391 pr_emerg("map_count %d vm_next %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800392 bug = 1;
393 }
394 if (highest_address != mm->highest_vm_end) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700395 pr_emerg("mm->highest_vm_end %lx, found %lx\n",
Andrew Mortonff26f702014-10-09 15:28:19 -0700396 mm->highest_vm_end, highest_address);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800397 bug = 1;
398 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800399 i = browse_rb(mm);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800400 if (i != mm->map_count) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700401 if (i != -1)
402 pr_emerg("map_count %d rb %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800403 bug = 1;
404 }
Sasha Levin96dad672014-10-09 15:28:39 -0700405 VM_BUG_ON_MM(bug, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407#else
Michel Lespinassed3737182012-12-11 16:01:38 -0800408#define validate_mm_rb(root, ignore) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#define validate_mm(mm) do { } while (0)
410#endif
411
Laurent Dufourd8173842018-04-17 16:33:23 +0200412#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
413#define mm_rb_write_lock(mm) write_lock(&(mm)->mm_rb_lock)
414#define mm_rb_write_unlock(mm) write_unlock(&(mm)->mm_rb_lock)
415#else
416#define mm_rb_write_lock(mm) do { } while (0)
417#define mm_rb_write_unlock(mm) do { } while (0)
418#endif /* CONFIG_SPECULATIVE_PAGE_FAULT */
419
Michel Lespinassed3737182012-12-11 16:01:38 -0800420RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
421 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
422
423/*
424 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
425 * vma->vm_prev->vm_end values changed, without modifying the vma's position
426 * in the rbtree.
427 */
428static void vma_gap_update(struct vm_area_struct *vma)
429{
430 /*
431 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
432 * function that does exacltly what we want.
433 */
434 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
435}
436
437static inline void vma_rb_insert(struct vm_area_struct *vma,
Laurent Dufourd8173842018-04-17 16:33:23 +0200438 struct mm_struct *mm)
Michel Lespinassed3737182012-12-11 16:01:38 -0800439{
Laurent Dufourd8173842018-04-17 16:33:23 +0200440 struct rb_root *root = &mm->mm_rb;
441
Michel Lespinassed3737182012-12-11 16:01:38 -0800442 /* All rb_subtree_gap values must be consistent prior to insertion */
443 validate_mm_rb(root, NULL);
444
445 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
446}
447
Laurent Dufourd8173842018-04-17 16:33:23 +0200448static void __vma_rb_erase(struct vm_area_struct *vma, struct mm_struct *mm)
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700449{
Laurent Dufourd8173842018-04-17 16:33:23 +0200450 struct rb_root *root = &mm->mm_rb;
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700451 /*
452 * Note rb_erase_augmented is a fairly large inline function,
453 * so make sure we instantiate it only once with our desired
454 * augmented rbtree callbacks.
455 */
Laurent Dufourd8173842018-04-17 16:33:23 +0200456 mm_rb_write_lock(mm);
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700457 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
Laurent Dufourd8173842018-04-17 16:33:23 +0200458 mm_rb_write_unlock(mm); /* wmb */
459
460 /*
461 * Ensure the removal is complete before clearing the node.
462 * Matched by vma_has_changed()/handle_speculative_fault().
463 */
464 RB_CLEAR_NODE(&vma->vm_rb);
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700465}
466
467static __always_inline void vma_rb_erase_ignore(struct vm_area_struct *vma,
Laurent Dufourd8173842018-04-17 16:33:23 +0200468 struct mm_struct *mm,
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700469 struct vm_area_struct *ignore)
470{
471 /*
472 * All rb_subtree_gap values must be consistent prior to erase,
473 * with the possible exception of the "next" vma being erased if
474 * next->vm_start was reduced.
475 */
Laurent Dufourd8173842018-04-17 16:33:23 +0200476 validate_mm_rb(&mm->mm_rb, ignore);
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700477
Laurent Dufourd8173842018-04-17 16:33:23 +0200478 __vma_rb_erase(vma, mm);
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700479}
480
481static __always_inline void vma_rb_erase(struct vm_area_struct *vma,
Laurent Dufourd8173842018-04-17 16:33:23 +0200482 struct mm_struct *mm)
Michel Lespinassed3737182012-12-11 16:01:38 -0800483{
484 /*
485 * All rb_subtree_gap values must be consistent prior to erase,
486 * with the possible exception of the vma being erased.
487 */
Laurent Dufourd8173842018-04-17 16:33:23 +0200488 validate_mm_rb(&mm->mm_rb, vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800489
Laurent Dufourd8173842018-04-17 16:33:23 +0200490 __vma_rb_erase(vma, mm);
Michel Lespinassed3737182012-12-11 16:01:38 -0800491}
492
Michel Lespinassebf181b92012-10-08 16:31:39 -0700493/*
494 * vma has some anon_vma assigned, and is already inserted on that
495 * anon_vma's interval trees.
496 *
497 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
498 * vma must be removed from the anon_vma's interval trees using
499 * anon_vma_interval_tree_pre_update_vma().
500 *
501 * After the update, the vma will be reinserted using
502 * anon_vma_interval_tree_post_update_vma().
503 *
504 * The entire update must be protected by exclusive mmap_sem and by
505 * the root anon_vma's mutex.
506 */
507static inline void
508anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
509{
510 struct anon_vma_chain *avc;
511
512 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
513 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
514}
515
516static inline void
517anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
518{
519 struct anon_vma_chain *avc;
520
521 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
522 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
523}
524
Hugh Dickins6597d782012-10-08 16:29:07 -0700525static int find_vma_links(struct mm_struct *mm, unsigned long addr,
526 unsigned long end, struct vm_area_struct **pprev,
527 struct rb_node ***rb_link, struct rb_node **rb_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Hugh Dickins6597d782012-10-08 16:29:07 -0700529 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 __rb_link = &mm->mm_rb.rb_node;
532 rb_prev = __rb_parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 while (*__rb_link) {
535 struct vm_area_struct *vma_tmp;
536
537 __rb_parent = *__rb_link;
538 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
539
540 if (vma_tmp->vm_end > addr) {
Hugh Dickins6597d782012-10-08 16:29:07 -0700541 /* Fail if an existing vma overlaps the area */
542 if (vma_tmp->vm_start < end)
543 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 __rb_link = &__rb_parent->rb_left;
545 } else {
546 rb_prev = __rb_parent;
547 __rb_link = &__rb_parent->rb_right;
548 }
549 }
550
551 *pprev = NULL;
552 if (rb_prev)
553 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
554 *rb_link = __rb_link;
555 *rb_parent = __rb_parent;
Hugh Dickins6597d782012-10-08 16:29:07 -0700556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Cyril Hrubise8420a82013-04-29 15:08:33 -0700559static unsigned long count_vma_pages_range(struct mm_struct *mm,
560 unsigned long addr, unsigned long end)
561{
562 unsigned long nr_pages = 0;
563 struct vm_area_struct *vma;
564
565 /* Find first overlaping mapping */
566 vma = find_vma_intersection(mm, addr, end);
567 if (!vma)
568 return 0;
569
570 nr_pages = (min(end, vma->vm_end) -
571 max(addr, vma->vm_start)) >> PAGE_SHIFT;
572
573 /* Iterate over the rest of the overlaps */
574 for (vma = vma->vm_next; vma; vma = vma->vm_next) {
575 unsigned long overlap_len;
576
577 if (vma->vm_start > end)
578 break;
579
580 overlap_len = min(end, vma->vm_end) - vma->vm_start;
581 nr_pages += overlap_len >> PAGE_SHIFT;
582 }
583
584 return nr_pages;
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
588 struct rb_node **rb_link, struct rb_node *rb_parent)
589{
Michel Lespinassed3737182012-12-11 16:01:38 -0800590 /* Update tracking information for the gap following the new vma. */
591 if (vma->vm_next)
592 vma_gap_update(vma->vm_next);
593 else
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700594 mm->highest_vm_end = vm_end_gap(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800595
596 /*
597 * vma->vm_prev wasn't known when we followed the rbtree to find the
598 * correct insertion point for that vma. As a result, we could not
599 * update the vma vm_rb parents rb_subtree_gap values on the way down.
600 * So, we first insert the vma with a zero rb_subtree_gap value
601 * (to be consistent with what we did on the way down), and then
602 * immediately update the gap to the correct value. Finally we
603 * rebalance the rbtree after all augmented values have been set.
604 */
Laurent Dufourd8173842018-04-17 16:33:23 +0200605 mm_rb_write_lock(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
Michel Lespinassed3737182012-12-11 16:01:38 -0800607 vma->rb_subtree_gap = 0;
608 vma_gap_update(vma);
Laurent Dufourd8173842018-04-17 16:33:23 +0200609 vma_rb_insert(vma, mm);
610 mm_rb_write_unlock(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700613static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
ZhenwenXu48aae422009-01-06 14:40:21 -0800615 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 file = vma->vm_file;
618 if (file) {
619 struct address_space *mapping = file->f_mapping;
620
621 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500622 atomic_dec(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700624 atomic_inc(&mapping->i_mmap_writable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 flush_dcache_mmap_lock(mapping);
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800627 vma_interval_tree_insert(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 flush_dcache_mmap_unlock(mapping);
629 }
630}
631
632static void
633__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
634 struct vm_area_struct *prev, struct rb_node **rb_link,
635 struct rb_node *rb_parent)
636{
637 __vma_link_list(mm, vma, prev, rb_parent);
638 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
642 struct vm_area_struct *prev, struct rb_node **rb_link,
643 struct rb_node *rb_parent)
644{
645 struct address_space *mapping = NULL;
646
Huang Shijie64ac4942014-06-04 16:07:33 -0700647 if (vma->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 mapping = vma->vm_file->f_mapping;
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800649 i_mmap_lock_write(mapping);
Huang Shijie64ac4942014-06-04 16:07:33 -0700650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 __vma_link(mm, vma, prev, rb_link, rb_parent);
653 __vma_link_file(vma);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (mapping)
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800656 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 mm->map_count++;
659 validate_mm(mm);
660}
661
662/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700663 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700664 * mm's list and rbtree. It has already been inserted into the interval tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800666static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Hugh Dickins6597d782012-10-08 16:29:07 -0700668 struct vm_area_struct *prev;
ZhenwenXu48aae422009-01-06 14:40:21 -0800669 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Hugh Dickins6597d782012-10-08 16:29:07 -0700671 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
672 &prev, &rb_link, &rb_parent))
673 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 __vma_link(mm, vma, prev, rb_link, rb_parent);
675 mm->map_count++;
676}
677
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700678static __always_inline void __vma_unlink_common(struct mm_struct *mm,
679 struct vm_area_struct *vma,
680 struct vm_area_struct *prev,
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700681 bool has_prev,
682 struct vm_area_struct *ignore)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Michel Lespinassed3737182012-12-11 16:01:38 -0800684 struct vm_area_struct *next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700685
Laurent Dufourd8173842018-04-17 16:33:23 +0200686 vma_rb_erase_ignore(vma, mm, ignore);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700687 next = vma->vm_next;
688 if (has_prev)
689 prev->vm_next = next;
690 else {
691 prev = vma->vm_prev;
692 if (prev)
693 prev->vm_next = next;
694 else
695 mm->mmap = next;
696 }
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700697 if (next)
698 next->vm_prev = prev;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700699
700 /* Kill the cache */
701 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700704static inline void __vma_unlink_prev(struct mm_struct *mm,
705 struct vm_area_struct *vma,
706 struct vm_area_struct *prev)
707{
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700708 __vma_unlink_common(mm, vma, prev, true, vma);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
712 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
713 * is already present in an i_mmap tree without adjusting the tree.
714 * The following helper function should be used when such adjustments
715 * are necessary. The "insert" vma (if any) is to be inserted
716 * before we drop the necessary locks.
717 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700718int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
719 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
Laurent Dufour3545b7e2018-04-17 16:33:16 +0200720 struct vm_area_struct *expand, bool keep_locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700723 struct vm_area_struct *next = vma->vm_next, *orig_vma = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct address_space *mapping = NULL;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700725 struct rb_root *root = NULL;
Rik van Riel012f18002010-08-09 17:18:40 -0700726 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct file *file = vma->vm_file;
Michel Lespinassed3737182012-12-11 16:01:38 -0800728 bool start_changed = false, end_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 long adjust_next = 0;
730 int remove_next = 0;
731
Peter Zijlstra365031a2018-04-17 16:33:14 +0200732 /*
733 * Why using vm_raw_write*() functions here to avoid lockdep's warning ?
734 *
735 * Locked is complaining about a theoretical lock dependency, involving
736 * 3 locks:
737 * mapping->i_mmap_rwsem --> vma->vm_sequence --> fs_reclaim
738 *
739 * Here are the major path leading to this dependency :
740 * 1. __vma_adjust() mmap_sem -> vm_sequence -> i_mmap_rwsem
741 * 2. move_vmap() mmap_sem -> vm_sequence -> fs_reclaim
742 * 3. __alloc_pages_nodemask() fs_reclaim -> i_mmap_rwsem
743 * 4. unmap_mapping_range() i_mmap_rwsem -> vm_sequence
744 *
745 * So there is no way to solve this easily, especially because in
746 * unmap_mapping_range() the i_mmap_rwsem is grab while the impacted
747 * VMAs are not yet known.
748 * However, the way the vm_seq is used is guarantying that we will
749 * never block on it since we just check for its value and never wait
750 * for it to move, see vma_has_changed() and handle_speculative_fault().
751 */
752 vm_raw_write_begin(vma);
753 if (next)
754 vm_raw_write_begin(next);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (next && !insert) {
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700757 struct vm_area_struct *exporter = NULL, *importer = NULL;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (end >= next->vm_end) {
760 /*
761 * vma expands, overlapping all the next, and
762 * perhaps the one after too (mprotect case 6).
Andrea Arcangeli86d12e42016-10-07 17:01:34 -0700763 * The only other cases that gets here are
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700764 * case 1, case 7 and case 8.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700766 if (next == expand) {
767 /*
768 * The only case where we don't expand "vma"
769 * and we expand "next" instead is case 8.
770 */
771 VM_WARN_ON(end != next->vm_end);
772 /*
773 * remove_next == 3 means we're
774 * removing "vma" and that to do so we
775 * swapped "vma" and "next".
776 */
777 remove_next = 3;
778 VM_WARN_ON(file != next->vm_file);
779 swap(vma, next);
780 } else {
781 VM_WARN_ON(expand != vma);
782 /*
783 * case 1, 6, 7, remove_next == 2 is case 6,
784 * remove_next == 1 is case 1 or 7.
785 */
786 remove_next = 1 + (end > next->vm_end);
787 VM_WARN_ON(remove_next == 2 &&
788 end != next->vm_next->vm_end);
789 VM_WARN_ON(remove_next == 1 &&
790 end != next->vm_end);
791 /* trim end to next, for case 6 first pass */
792 end = next->vm_end;
793 }
794
Linus Torvalds287d97a2010-04-10 15:22:30 -0700795 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 importer = vma;
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700797
798 /*
799 * If next doesn't have anon_vma, import from vma after
800 * next, if the vma overlaps with it.
801 */
Andrea Arcangeli97a42cd2016-10-07 17:01:31 -0700802 if (remove_next == 2 && !next->anon_vma)
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700803 exporter = next->vm_next;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } else if (end > next->vm_start) {
806 /*
807 * vma expands, overlapping part of the next:
808 * mprotect case 5 shifting the boundary up.
809 */
810 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700811 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 importer = vma;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700813 VM_WARN_ON(expand != importer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else if (end < vma->vm_end) {
815 /*
816 * vma shrinks, and !insert tells it's not
817 * split_vma inserting another: so it must be
818 * mprotect case 4 shifting the boundary down.
819 */
vishnu.pscc71aba2014-10-09 15:26:29 -0700820 adjust_next = -((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700821 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 importer = next;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700823 VM_WARN_ON(expand != importer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Rik van Riel5beb4932010-03-05 13:42:07 -0800826 /*
827 * Easily overlooked: when mprotect shifts the boundary,
828 * make sure the expanding vma has anon_vma set if the
829 * shrinking vma had, to cover any anon pages imported.
830 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700831 if (exporter && exporter->anon_vma && !importer->anon_vma) {
Daniel Forrestc4ea95d2014-12-02 15:59:42 -0800832 int error;
833
Linus Torvalds287d97a2010-04-10 15:22:30 -0700834 importer->anon_vma = exporter->anon_vma;
Konstantin Khlebnikovb800c912015-01-11 16:54:06 +0300835 error = anon_vma_clone(importer, exporter);
Laurent Dufour3545b7e2018-04-17 16:33:16 +0200836 if (error) {
837 if (next && next != vma)
838 vm_raw_write_end(next);
839 vm_raw_write_end(vma);
Konstantin Khlebnikovb800c912015-01-11 16:54:06 +0300840 return error;
Laurent Dufour3545b7e2018-04-17 16:33:16 +0200841 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800842 }
843 }
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700844again:
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700845 vma_adjust_trans_huge(orig_vma, start, end, adjust_next);
Kirill A. Shutemov37f9f552016-07-26 15:25:48 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (file) {
848 mapping = file->f_mapping;
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800849 root = &mapping->i_mmap;
850 uprobe_munmap(vma, vma->vm_start, vma->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530851
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800852 if (adjust_next)
853 uprobe_munmap(next, next->vm_start, next->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530854
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800855 i_mmap_lock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700858 * Put into interval tree now, so instantiated pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * are visible to arm/parisc __flush_dcache_page
860 * throughout; but we cannot insert into address
861 * space until vma start or end is updated.
862 */
863 __vma_link_file(insert);
864 }
865 }
866
Michel Lespinassebf181b92012-10-08 16:31:39 -0700867 anon_vma = vma->anon_vma;
868 if (!anon_vma && adjust_next)
869 anon_vma = next->anon_vma;
870 if (anon_vma) {
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700871 VM_WARN_ON(adjust_next && next->anon_vma &&
872 anon_vma != next->anon_vma);
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000873 anon_vma_lock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700874 anon_vma_interval_tree_pre_update_vma(vma);
875 if (adjust_next)
876 anon_vma_interval_tree_pre_update_vma(next);
877 }
Rik van Riel012f18002010-08-09 17:18:40 -0700878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (root) {
880 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700881 vma_interval_tree_remove(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700883 vma_interval_tree_remove(next, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Michel Lespinassed3737182012-12-11 16:01:38 -0800886 if (start != vma->vm_start) {
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200887 WRITE_ONCE(vma->vm_start, start);
Michel Lespinassed3737182012-12-11 16:01:38 -0800888 start_changed = true;
889 }
890 if (end != vma->vm_end) {
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200891 WRITE_ONCE(vma->vm_end, end);
Michel Lespinassed3737182012-12-11 16:01:38 -0800892 end_changed = true;
893 }
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200894 WRITE_ONCE(vma->vm_pgoff, pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (adjust_next) {
Laurent Dufourdd2b4652018-04-17 16:33:15 +0200896 WRITE_ONCE(next->vm_start,
897 next->vm_start + (adjust_next << PAGE_SHIFT));
898 WRITE_ONCE(next->vm_pgoff, next->vm_pgoff + adjust_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
901 if (root) {
902 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700903 vma_interval_tree_insert(next, root);
904 vma_interval_tree_insert(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 flush_dcache_mmap_unlock(mapping);
906 }
907
908 if (remove_next) {
909 /*
910 * vma_merge has merged next into vma, and needs
911 * us to remove next before dropping the locks.
912 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700913 if (remove_next != 3)
914 __vma_unlink_prev(mm, next, vma);
915 else
Andrea Arcangeli8f26e0b2016-10-07 17:01:37 -0700916 /*
917 * vma is not before next if they've been
918 * swapped.
919 *
920 * pre-swap() next->vm_start was reduced so
921 * tell validate_mm_rb to ignore pre-swap()
922 * "next" (which is stored in post-swap()
923 * "vma").
924 */
925 __vma_unlink_common(mm, next, NULL, false, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (file)
927 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 } else if (insert) {
929 /*
930 * split_vma has split insert from vma, and needs
931 * us to insert it before dropping the locks
932 * (it may either follow vma or precede it).
933 */
934 __insert_vm_struct(mm, insert);
Michel Lespinassed3737182012-12-11 16:01:38 -0800935 } else {
936 if (start_changed)
937 vma_gap_update(vma);
938 if (end_changed) {
939 if (!next)
Hugh Dickinscfc0eb402017-06-19 04:03:24 -0700940 mm->highest_vm_end = vm_end_gap(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800941 else if (!adjust_next)
942 vma_gap_update(next);
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
Michel Lespinassebf181b92012-10-08 16:31:39 -0700946 if (anon_vma) {
947 anon_vma_interval_tree_post_update_vma(vma);
948 if (adjust_next)
949 anon_vma_interval_tree_post_update_vma(next);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -0800950 anon_vma_unlock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (mapping)
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800953 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530955 if (root) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100956 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530957
958 if (adjust_next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100959 uprobe_mmap(next);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530960 }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (remove_next) {
Laurent Dufourd8173842018-04-17 16:33:23 +0200963 if (file)
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530964 uprobe_munmap(next, next->vm_start, next->vm_end);
Rik van Riel5beb4932010-03-05 13:42:07 -0800965 if (next->anon_vma)
966 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 mm->map_count--;
Peter Zijlstra365031a2018-04-17 16:33:14 +0200968 vm_raw_write_end(next);
Laurent Dufourd8173842018-04-17 16:33:23 +0200969 put_vma(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /*
971 * In mprotect's case 6 (see comments on vma_merge),
972 * we must remove another next too. It would clutter
973 * up the code too much to do both in one go.
974 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700975 if (remove_next != 3) {
976 /*
977 * If "next" was removed and vma->vm_end was
978 * expanded (up) over it, in turn
979 * "next->vm_prev->vm_end" changed and the
980 * "vma->vm_next" gap must be updated.
981 */
982 next = vma->vm_next;
Peter Zijlstra365031a2018-04-17 16:33:14 +0200983 if (next)
984 vm_raw_write_begin(next);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -0700985 } else {
986 /*
987 * For the scope of the comment "next" and
988 * "vma" considered pre-swap(): if "vma" was
989 * removed, next->vm_start was expanded (down)
990 * over it and the "next" gap must be updated.
991 * Because of the swap() the post-swap() "vma"
992 * actually points to pre-swap() "next"
993 * (post-swap() "next" as opposed is now a
994 * dangling pointer).
995 */
996 next = vma;
997 }
Kirill A. Shutemov734537c2016-07-28 15:49:01 -0700998 if (remove_next == 2) {
999 remove_next = 1;
1000 end = next->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto again;
Kirill A. Shutemov734537c2016-07-28 15:49:01 -07001002 }
Michel Lespinassed3737182012-12-11 16:01:38 -08001003 else if (next)
1004 vma_gap_update(next);
Andrea Arcangelifb8c41e2016-10-07 17:01:25 -07001005 else {
1006 /*
1007 * If remove_next == 2 we obviously can't
1008 * reach this path.
1009 *
1010 * If remove_next == 3 we can't reach this
1011 * path because pre-swap() next is always not
1012 * NULL. pre-swap() "next" is not being
1013 * removed and its next->vm_end is not altered
1014 * (and furthermore "end" already matches
1015 * next->vm_end in remove_next == 3).
1016 *
1017 * We reach this only in the remove_next == 1
1018 * case if the "next" vma that was removed was
1019 * the highest vma of the mm. However in such
1020 * case next->vm_end == "end" and the extended
1021 * "vma" has vma->vm_end == next->vm_end so
1022 * mm->highest_vm_end doesn't need any update
1023 * in remove_next == 1 case.
1024 */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001025 VM_WARN_ON(mm->highest_vm_end != vm_end_gap(vma));
Andrea Arcangelifb8c41e2016-10-07 17:01:25 -07001026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301028 if (insert && file)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001029 uprobe_mmap(insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Peter Zijlstra365031a2018-04-17 16:33:14 +02001031 if (next && next != vma)
1032 vm_raw_write_end(next);
Laurent Dufour3545b7e2018-04-17 16:33:16 +02001033 if (!keep_locked)
1034 vm_raw_write_end(vma);
Peter Zijlstra365031a2018-04-17 16:33:14 +02001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -08001037
1038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041/*
1042 * If the vma has a ->close operation then the driver probably needs to release
1043 * per-vma resources, so we don't attempt to merge those.
1044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045static inline int is_mergeable_vma(struct vm_area_struct *vma,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001046 struct file *file, unsigned long vm_flags,
Colin Cross3e4578f2015-10-27 16:42:08 -07001047 struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
1048 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Cyrill Gorcunov34228d42014-01-23 15:53:42 -08001050 /*
1051 * VM_SOFTDIRTY should not prevent from VMA merging, if we
1052 * match the flags but dirty bit -- the caller should mark
1053 * merged VMA as dirty. If dirty bit won't be excluded from
1054 * comparison, we increase pressue on the memory system forcing
1055 * the kernel to generate new VMAs when old one could be
1056 * extended instead.
1057 */
1058 if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060 if (vma->vm_file != file)
1061 return 0;
1062 if (vma->vm_ops && vma->vm_ops->close)
1063 return 0;
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001064 if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx))
1065 return 0;
Colin Cross3e4578f2015-10-27 16:42:08 -07001066 if (vma_get_anon_name(vma) != anon_name)
1067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return 1;
1069}
1070
1071static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -07001072 struct anon_vma *anon_vma2,
1073 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Shaohua Li965f55d2011-05-24 17:11:20 -07001075 /*
1076 * The list_is_singular() test is to avoid merging VMA cloned from
1077 * parents. This can improve scalability caused by anon_vma lock.
1078 */
1079 if ((!anon_vma1 || !anon_vma2) && (!vma ||
1080 list_is_singular(&vma->anon_vma_chain)))
1081 return 1;
1082 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085/*
1086 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
1087 * in front of (at a lower virtual address and file offset than) the vma.
1088 *
1089 * We cannot merge two vmas if they have differently assigned (non-NULL)
1090 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
1091 *
1092 * We don't check here for the merged mmap wrapping around the end of pagecache
1093 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
1094 * wrap, nor mmaps which cover the final page at index -1UL.
1095 */
1096static int
1097can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001098 struct anon_vma *anon_vma, struct file *file,
1099 pgoff_t vm_pgoff,
Colin Cross3e4578f2015-10-27 16:42:08 -07001100 struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
1101 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Colin Cross3e4578f2015-10-27 16:42:08 -07001103 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) &&
Shaohua Li965f55d2011-05-24 17:11:20 -07001104 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (vma->vm_pgoff == vm_pgoff)
1106 return 1;
1107 }
1108 return 0;
1109}
1110
1111/*
1112 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
1113 * beyond (at a higher virtual address and file offset than) the vma.
1114 *
1115 * We cannot merge two vmas if they have differently assigned (non-NULL)
1116 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
1117 */
1118static int
1119can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001120 struct anon_vma *anon_vma, struct file *file,
1121 pgoff_t vm_pgoff,
Colin Cross3e4578f2015-10-27 16:42:08 -07001122 struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
1123 const char __user *anon_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Colin Cross3e4578f2015-10-27 16:42:08 -07001125 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name) &&
Shaohua Li965f55d2011-05-24 17:11:20 -07001126 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 pgoff_t vm_pglen;
Libind6e93212013-07-03 15:01:26 -07001128 vm_pglen = vma_pages(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
1130 return 1;
1131 }
1132 return 0;
1133}
1134
1135/*
Colin Cross3e4578f2015-10-27 16:42:08 -07001136 * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name),
1137 * figure out whether that can be merged with its predecessor or its
1138 * successor. Or both (it neatly fills a hole).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 *
1140 * In most cases - when called for mmap, brk or mremap - [addr,end) is
1141 * certain not to be mapped by the time vma_merge is called; but when
1142 * called for mprotect, it is certain to be already mapped (either at
1143 * an offset within prev, or at the start of next), and the flags of
1144 * this area are about to be changed to vm_flags - and the no-change
1145 * case has already been eliminated.
1146 *
1147 * The following mprotect cases have to be considered, where AAAA is
1148 * the area passed down from mprotect_fixup, never extending beyond one
1149 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
1150 *
1151 * AAAA AAAA AAAA AAAA
1152 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
1153 * cannot merge might become might become might become
1154 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
1155 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001156 * mremap move: PPPPXXXXXXXX 8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 * AAAA
1158 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
1159 * might become case 1 below case 2 below case 3 below
1160 *
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001161 * It is important for case 8 that the the vma NNNN overlapping the
1162 * region AAAA is never going to extended over XXXX. Instead XXXX must
1163 * be extended in region AAAA and NNNN must be removed. This way in
1164 * all cases where vma_merge succeeds, the moment vma_adjust drops the
1165 * rmap_locks, the properties of the merged vma will be already
1166 * correct for the whole merged range. Some of those properties like
1167 * vm_page_prot/vm_flags may be accessed by rmap_walks and they must
1168 * be correct for the whole merged range immediately after the
1169 * rmap_locks are released. Otherwise if XXXX would be removed and
1170 * NNNN would be extended over the XXXX range, remove_migration_ptes
1171 * or other rmap walkers (if working on addresses beyond the "end"
1172 * parameter) may establish ptes with the wrong permissions of NNNN
1173 * instead of the right permissions of XXXX.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
Laurent Dufour3545b7e2018-04-17 16:33:16 +02001175struct vm_area_struct *__vma_merge(struct mm_struct *mm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct vm_area_struct *prev, unsigned long addr,
1177 unsigned long end, unsigned long vm_flags,
vishnu.pscc71aba2014-10-09 15:26:29 -07001178 struct anon_vma *anon_vma, struct file *file,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001179 pgoff_t pgoff, struct mempolicy *policy,
Colin Cross3e4578f2015-10-27 16:42:08 -07001180 struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
Laurent Dufour3545b7e2018-04-17 16:33:16 +02001181 const char __user *anon_name, bool keep_locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
1184 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -08001185 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 /*
1188 * We later require that vma->vm_flags == vm_flags,
1189 * so this tests vma->vm_flags & VM_SPECIAL, too.
1190 */
1191 if (vm_flags & VM_SPECIAL)
1192 return NULL;
1193
1194 if (prev)
1195 next = prev->vm_next;
1196 else
1197 next = mm->mmap;
1198 area = next;
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001199 if (area && area->vm_end == end) /* cases 6, 7, 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 next = next->vm_next;
1201
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001202 /* verify some invariant that must be enforced by the caller */
1203 VM_WARN_ON(prev && addr <= prev->vm_start);
1204 VM_WARN_ON(area && end > area->vm_end);
1205 VM_WARN_ON(addr >= end);
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /*
1208 * Can it merge with the predecessor?
1209 */
1210 if (prev && prev->vm_end == addr &&
vishnu.pscc71aba2014-10-09 15:26:29 -07001211 mpol_equal(vma_policy(prev), policy) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 can_vma_merge_after(prev, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001213 anon_vma, file, pgoff,
Colin Cross3e4578f2015-10-27 16:42:08 -07001214 vm_userfaultfd_ctx,
1215 anon_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /*
1217 * OK, it can. Can we now merge in the successor as well?
1218 */
1219 if (next && end == next->vm_start &&
1220 mpol_equal(policy, vma_policy(next)) &&
1221 can_vma_merge_before(next, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001222 anon_vma, file,
1223 pgoff+pglen,
Colin Cross3e4578f2015-10-27 16:42:08 -07001224 vm_userfaultfd_ctx,
1225 anon_name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -07001227 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /* cases 1, 6 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001229 err = __vma_adjust(prev, prev->vm_start,
1230 next->vm_end, prev->vm_pgoff, NULL,
Laurent Dufour3545b7e2018-04-17 16:33:16 +02001231 prev, keep_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 } else /* cases 2, 5, 7 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001233 err = __vma_adjust(prev, prev->vm_start,
Laurent Dufour3545b7e2018-04-17 16:33:16 +02001234 end, prev->vm_pgoff, NULL, prev,
1235 keep_locked);
Rik van Riel5beb4932010-03-05 13:42:07 -08001236 if (err)
1237 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001238 khugepaged_enter_vma_merge(prev, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return prev;
1240 }
1241
1242 /*
1243 * Can this new request be merged in front of next?
1244 */
1245 if (next && end == next->vm_start &&
vishnu.pscc71aba2014-10-09 15:26:29 -07001246 mpol_equal(policy, vma_policy(next)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 can_vma_merge_before(next, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001248 anon_vma, file, pgoff+pglen,
Colin Cross3e4578f2015-10-27 16:42:08 -07001249 vm_userfaultfd_ctx,
1250 anon_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (prev && addr < prev->vm_end) /* case 4 */
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001252 err = __vma_adjust(prev, prev->vm_start,
Laurent Dufour3545b7e2018-04-17 16:33:16 +02001253 addr, prev->vm_pgoff, NULL, next,
1254 keep_locked);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001255 else { /* cases 3, 8 */
1256 err = __vma_adjust(area, addr, next->vm_end,
Laurent Dufour3545b7e2018-04-17 16:33:16 +02001257 next->vm_pgoff - pglen, NULL, next,
1258 keep_locked);
Andrea Arcangelie86f15e2016-10-07 17:01:28 -07001259 /*
1260 * In case 3 area is already equal to next and
1261 * this is a noop, but in case 8 "area" has
1262 * been removed and next was expanded over it.
1263 */
1264 area = next;
1265 }
Rik van Riel5beb4932010-03-05 13:42:07 -08001266 if (err)
1267 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001268 khugepaged_enter_vma_merge(area, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return area;
1270 }
1271
1272 return NULL;
1273}
1274
1275/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001276 * Rough compatbility check to quickly see if it's even worth looking
1277 * at sharing an anon_vma.
1278 *
1279 * They need to have the same vm_file, and the flags can only differ
1280 * in things that mprotect may change.
1281 *
1282 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1283 * we can merge the two vma's. For example, we refuse to merge a vma if
1284 * there is a vm_ops->close() function, because that indicates that the
1285 * driver is doing some kind of reference counting. But that doesn't
1286 * really matter for the anon_vma sharing case.
1287 */
1288static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1289{
1290 return a->vm_end == b->vm_start &&
1291 mpol_equal(vma_policy(a), vma_policy(b)) &&
1292 a->vm_file == b->vm_file &&
Cyrill Gorcunov34228d42014-01-23 15:53:42 -08001293 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) &&
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001294 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1295}
1296
1297/*
1298 * Do some basic sanity checking to see if we can re-use the anon_vma
1299 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1300 * the same as 'old', the other will be the new one that is trying
1301 * to share the anon_vma.
1302 *
1303 * NOTE! This runs with mm_sem held for reading, so it is possible that
1304 * the anon_vma of 'old' is concurrently in the process of being set up
1305 * by another page fault trying to merge _that_. But that's ok: if it
1306 * is being set up, that automatically means that it will be a singleton
1307 * acceptable for merging, so we can do all of this optimistically. But
Jason Low4db0c3c2015-04-15 16:14:08 -07001308 * we do that READ_ONCE() to make sure that we never re-load the pointer.
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001309 *
1310 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1311 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1312 * is to return an anon_vma that is "complex" due to having gone through
1313 * a fork).
1314 *
1315 * We also make sure that the two vma's are compatible (adjacent,
1316 * and with the same memory policies). That's all stable, even with just
1317 * a read lock on the mm_sem.
1318 */
1319static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1320{
1321 if (anon_vma_compatible(a, b)) {
Jason Low4db0c3c2015-04-15 16:14:08 -07001322 struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001323
1324 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1325 return anon_vma;
1326 }
1327 return NULL;
1328}
1329
1330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1332 * neighbouring vmas for a suitable anon_vma, before it goes off
1333 * to allocate a new anon_vma. It checks because a repetitive
1334 * sequence of mprotects and faults may otherwise lead to distinct
1335 * anon_vmas being allocated, preventing vma merge in subsequent
1336 * mprotect.
1337 */
1338struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1339{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001340 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 near = vma->vm_next;
1344 if (!near)
1345 goto try_prev;
1346
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001347 anon_vma = reusable_anon_vma(near, vma, near);
1348 if (anon_vma)
1349 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -07001351 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!near)
1353 goto none;
1354
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001355 anon_vma = reusable_anon_vma(near, near, vma);
1356 if (anon_vma)
1357 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358none:
1359 /*
1360 * There's no absolute need to look only at touching neighbours:
1361 * we could search further afield for "compatible" anon_vmas.
1362 * But it would probably just be a waste of time searching,
1363 * or lead to too many vmas hanging off the same anon_vma.
1364 * We're trying to allow mprotect remerging later on,
1365 * not trying to minimize memory used for anon_vmas.
1366 */
1367 return NULL;
1368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370/*
Al Viro40401532012-02-13 03:58:52 +00001371 * If a hint addr is less than mmap_min_addr change hint to be as
1372 * low as possible but still greater than mmap_min_addr
1373 */
1374static inline unsigned long round_hint_to_min(unsigned long hint)
1375{
1376 hint &= PAGE_MASK;
1377 if (((void *)hint != NULL) &&
1378 (hint < mmap_min_addr))
1379 return PAGE_ALIGN(mmap_min_addr);
1380 return hint;
1381}
1382
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001383static inline int mlock_future_check(struct mm_struct *mm,
1384 unsigned long flags,
1385 unsigned long len)
1386{
1387 unsigned long locked, lock_limit;
1388
1389 /* mlock MCL_FUTURE? */
1390 if (flags & VM_LOCKED) {
1391 locked = len >> PAGE_SHIFT;
1392 locked += mm->locked_vm;
1393 lock_limit = rlimit(RLIMIT_MEMLOCK);
1394 lock_limit >>= PAGE_SHIFT;
1395 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1396 return -EAGAIN;
1397 }
1398 return 0;
1399}
1400
Linus Torvalds7a403742018-05-11 09:52:01 -07001401static inline u64 file_mmap_size_max(struct file *file, struct inode *inode)
1402{
1403 if (S_ISREG(inode->i_mode))
Linus Torvalds4be65292018-05-19 09:29:11 -07001404 return MAX_LFS_FILESIZE;
Linus Torvalds7a403742018-05-11 09:52:01 -07001405
1406 if (S_ISBLK(inode->i_mode))
1407 return MAX_LFS_FILESIZE;
1408
1409 /* Special "we do even unsigned file positions" case */
1410 if (file->f_mode & FMODE_UNSIGNED_OFFSET)
1411 return 0;
1412
1413 /* Yes, random drivers might want more. But I'm tired of buggy drivers */
1414 return ULONG_MAX;
1415}
1416
1417static inline bool file_mmap_ok(struct file *file, struct inode *inode,
1418 unsigned long pgoff, unsigned long len)
1419{
1420 u64 maxsize = file_mmap_size_max(file, inode);
1421
1422 if (maxsize && len > maxsize)
1423 return false;
1424 maxsize -= len;
1425 if (pgoff > maxsize >> PAGE_SHIFT)
1426 return false;
1427 return true;
1428}
1429
Al Viro40401532012-02-13 03:58:52 +00001430/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001431 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001433unsigned long do_mmap(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 unsigned long len, unsigned long prot,
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001435 unsigned long flags, vm_flags_t vm_flags,
1436 unsigned long pgoff, unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
vishnu.pscc71aba2014-10-09 15:26:29 -07001438 struct mm_struct *mm = current->mm;
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001439 int pkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Michel Lespinasse41badc12013-02-22 16:32:47 -08001441 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001442
Piotr Kwapulinskie37609b2015-06-24 16:58:39 -07001443 if (!len)
1444 return -EINVAL;
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /*
1447 * Does the application expect PROT_READ to imply PROT_EXEC?
1448 *
1449 * (the exception is when the underlying filesystem is noexec
1450 * mounted, in which case we dont add PROT_EXEC.)
1451 */
1452 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Eric W. Biederman90f85722015-06-29 14:42:03 -05001453 if (!(file && path_noexec(&file->f_path)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 prot |= PROT_EXEC;
1455
Eric Paris7cd94142007-11-26 18:47:40 -05001456 if (!(flags & MAP_FIXED))
1457 addr = round_hint_to_min(addr);
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /* Careful about overflows.. */
1460 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001461 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -ENOMEM;
1463
1464 /* offset overflow? */
1465 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
vishnu.pscc71aba2014-10-09 15:26:29 -07001466 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 /* Too many mappings? */
1469 if (mm->map_count > sysctl_max_map_count)
1470 return -ENOMEM;
1471
1472 /* Obtain the address to map to. we verify (or select) it and ensure
1473 * that it represents a valid section of the address space.
1474 */
1475 addr = get_unmapped_area(file, addr, len, pgoff, flags);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001476 if (offset_in_page(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return addr;
1478
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001479 if (prot == PROT_EXEC) {
1480 pkey = execute_only_pkey(mm);
1481 if (pkey < 0)
1482 pkey = 0;
1483 }
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /* Do simple checking here so the lower-level routines won't have
1486 * to. we assume access permissions have been handled by the open
1487 * of the memory object, so we don't do any here.
1488 */
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001489 vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1491
Huang Shijiecdf7b342009-09-21 17:03:36 -07001492 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!can_do_mlock())
1494 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001495
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001496 if (mlock_future_check(mm, vm_flags, len))
1497 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (file) {
Oleg Nesterov077bf222013-09-11 14:20:19 -07001500 struct inode *inode = file_inode(file);
1501
Linus Torvalds7a403742018-05-11 09:52:01 -07001502 if (!file_mmap_ok(file, inode, pgoff, len))
1503 return -EOVERFLOW;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 switch (flags & MAP_TYPE) {
1506 case MAP_SHARED:
1507 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1508 return -EACCES;
1509
1510 /*
1511 * Make sure we don't allow writing to an append-only
1512 * file..
1513 */
1514 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1515 return -EACCES;
1516
1517 /*
1518 * Make sure there are no mandatory locks on the file.
1519 */
Jeff Laytond7a06982014-03-10 09:54:15 -04001520 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return -EAGAIN;
1522
1523 vm_flags |= VM_SHARED | VM_MAYSHARE;
1524 if (!(file->f_mode & FMODE_WRITE))
1525 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1526
1527 /* fall through */
1528 case MAP_PRIVATE:
1529 if (!(file->f_mode & FMODE_READ))
1530 return -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001531 if (path_noexec(&file->f_path)) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001532 if (vm_flags & VM_EXEC)
1533 return -EPERM;
1534 vm_flags &= ~VM_MAYEXEC;
1535 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001536
Al Viro72c2d532013-09-22 16:27:52 -04001537 if (!file->f_op->mmap)
Linus Torvalds80c56062006-10-15 14:09:55 -07001538 return -ENODEV;
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001539 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1540 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 break;
1542
1543 default:
1544 return -EINVAL;
1545 }
1546 } else {
1547 switch (flags & MAP_TYPE) {
1548 case MAP_SHARED:
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001549 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1550 return -EINVAL;
Tejun Heoce363942008-09-03 16:09:47 +02001551 /*
1552 * Ignore pgoff.
1553 */
1554 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 vm_flags |= VM_SHARED | VM_MAYSHARE;
1556 break;
1557 case MAP_PRIVATE:
1558 /*
1559 * Set pgoff according to addr for anon_vma.
1560 */
1561 pgoff = addr >> PAGE_SHIFT;
1562 break;
1563 default:
1564 return -EINVAL;
1565 }
1566 }
1567
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001568 /*
1569 * Set 'VM_NORESERVE' if we should not account for the
1570 * memory use of this mapping.
1571 */
1572 if (flags & MAP_NORESERVE) {
1573 /* We honor MAP_NORESERVE if allowed to overcommit */
1574 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1575 vm_flags |= VM_NORESERVE;
1576
1577 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1578 if (file && is_file_hugepages(file))
1579 vm_flags |= VM_NORESERVE;
1580 }
1581
1582 addr = mmap_region(file, addr, len, vm_flags, pgoff);
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -07001583 if (!IS_ERR_VALUE(addr) &&
1584 ((vm_flags & VM_LOCKED) ||
1585 (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
Michel Lespinasse41badc12013-02-22 16:32:47 -08001586 *populate = len;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001587 return addr;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001588}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001589
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001590SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1591 unsigned long, prot, unsigned long, flags,
1592 unsigned long, fd, unsigned long, pgoff)
1593{
1594 struct file *file = NULL;
Chen Gang1e3ee142015-11-05 18:48:35 -08001595 unsigned long retval;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001596
1597 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001598 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001599 file = fget(fd);
1600 if (!file)
Chen Gang1e3ee142015-11-05 18:48:35 -08001601 return -EBADF;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001602 if (is_file_hugepages(file))
1603 len = ALIGN(len, huge_page_size(hstate_file(file)));
Jörn Engel493af572013-07-08 16:00:26 -07001604 retval = -EINVAL;
1605 if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
1606 goto out_fput;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001607 } else if (flags & MAP_HUGETLB) {
1608 struct user_struct *user = NULL;
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001609 struct hstate *hs;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001610
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001611 hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & SHM_HUGE_MASK);
Li Zefan091d0d52013-05-09 15:08:15 +08001612 if (!hs)
1613 return -EINVAL;
1614
1615 len = ALIGN(len, huge_page_size(hs));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001616 /*
1617 * VM_NORESERVE is used because the reservations will be
1618 * taken when vm_ops->mmap() is called
1619 * A dummy user value is used because we are not locking
1620 * memory so no accounting is necessary
1621 */
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001622 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
Andi Kleen42d73952012-12-11 16:01:34 -08001623 VM_NORESERVE,
1624 &user, HUGETLB_ANONHUGE_INODE,
1625 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001626 if (IS_ERR(file))
1627 return PTR_ERR(file);
1628 }
1629
1630 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1631
Michal Hocko9fbeb5a2016-05-23 16:25:30 -07001632 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Jörn Engel493af572013-07-08 16:00:26 -07001633out_fput:
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001634 if (file)
1635 fput(file);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001636 return retval;
1637}
1638
Christoph Hellwiga4679372010-03-10 15:21:15 -08001639#ifdef __ARCH_WANT_SYS_OLD_MMAP
1640struct mmap_arg_struct {
1641 unsigned long addr;
1642 unsigned long len;
1643 unsigned long prot;
1644 unsigned long flags;
1645 unsigned long fd;
1646 unsigned long offset;
1647};
1648
1649SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1650{
1651 struct mmap_arg_struct a;
1652
1653 if (copy_from_user(&a, arg, sizeof(a)))
1654 return -EFAULT;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001655 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001656 return -EINVAL;
1657
1658 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1659 a.offset >> PAGE_SHIFT);
1660}
1661#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1662
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001663/*
1664 * Some shared mappigns will want the pages marked read-only
1665 * to track write events. If so, we'll downgrade vm_page_prot
1666 * to the private version (using protection_map[] without the
1667 * VM_SHARED bit).
1668 */
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07001669int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001670{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001671 vm_flags_t vm_flags = vma->vm_flags;
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001672 const struct vm_operations_struct *vm_ops = vma->vm_ops;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001673
1674 /* If it was private or non-writable, the write bit is already clear */
1675 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1676 return 0;
1677
1678 /* The backer wishes to know when pages are first written to? */
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001679 if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001680 return 1;
1681
Peter Feiner64e45502014-10-13 15:55:46 -07001682 /* The open routine did something to the protections that pgprot_modify
1683 * won't preserve? */
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07001684 if (pgprot_val(vm_page_prot) !=
1685 pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001686 return 0;
1687
Peter Feiner64e45502014-10-13 15:55:46 -07001688 /* Do we need to track softdirty? */
1689 if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY))
1690 return 1;
1691
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001692 /* Specialty mapping? */
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001693 if (vm_flags & VM_PFNMAP)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001694 return 0;
1695
1696 /* Can the mapping track the dirty pages? */
1697 return vma->vm_file && vma->vm_file->f_mapping &&
1698 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1699}
1700
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001701/*
1702 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001703 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001704 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001705static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001706{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001707 /*
1708 * hugetlb has its own accounting separate from the core VM
1709 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1710 */
1711 if (file && is_file_hugepages(file))
1712 return 0;
1713
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001714 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1715}
1716
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001717unsigned long mmap_region(struct file *file, unsigned long addr,
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001718 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001719{
1720 struct mm_struct *mm = current->mm;
1721 struct vm_area_struct *vma, *prev;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001722 int error;
1723 struct rb_node **rb_link, *rb_parent;
1724 unsigned long charged = 0;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001725
Cyril Hrubise8420a82013-04-29 15:08:33 -07001726 /* Check against address space limit. */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001727 if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) {
Cyril Hrubise8420a82013-04-29 15:08:33 -07001728 unsigned long nr_pages;
1729
1730 /*
1731 * MAP_FIXED may remove pages of mappings that intersects with
1732 * requested mapping. Account for the pages it would unmap.
1733 */
Cyril Hrubise8420a82013-04-29 15:08:33 -07001734 nr_pages = count_vma_pages_range(mm, addr, addr + len);
1735
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001736 if (!may_expand_vm(mm, vm_flags,
1737 (len >> PAGE_SHIFT) - nr_pages))
Cyril Hrubise8420a82013-04-29 15:08:33 -07001738 return -ENOMEM;
1739 }
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* Clear old maps */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07001742 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
1743 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (do_munmap(mm, addr, len))
1745 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001748 /*
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001749 * Private writable mapping: check memory availability
1750 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001751 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001752 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001753 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001754 return -ENOMEM;
1755 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757
1758 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001759 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001761 vma = vma_merge(mm, prev, addr, addr + len, vm_flags,
Colin Cross3e4578f2015-10-27 16:42:08 -07001762 NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX, NULL);
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001763 if (vma)
1764 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /*
1767 * Determine the object being mapped and call the appropriate
1768 * specific mapper. the address has already been validated, but
1769 * not unmapped, but the maps are removed from the list.
1770 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001771 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (!vma) {
1773 error = -ENOMEM;
1774 goto unacct_error;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 vma->vm_mm = mm;
1778 vma->vm_start = addr;
1779 vma->vm_end = addr + len;
1780 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001781 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 vma->vm_pgoff = pgoff;
Laurent Dufour6315ccc2018-04-17 16:33:13 +02001783 INIT_VMA(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (vm_flags & VM_DENYWRITE) {
1787 error = deny_write_access(file);
1788 if (error)
1789 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001791 if (vm_flags & VM_SHARED) {
1792 error = mapping_map_writable(file->f_mapping);
1793 if (error)
1794 goto allow_write_and_free_vma;
1795 }
1796
1797 /* ->mmap() can change vma->vm_file, but must guarantee that
1798 * vma_link() below can deny write-access if VM_DENYWRITE is set
1799 * and map writably if VM_SHARED is set. This usually means the
1800 * new file must not have been exposed to user-space, yet.
1801 */
Al Virocb0942b2012-08-27 14:48:26 -04001802 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 error = file->f_op->mmap(file, vma);
1804 if (error)
1805 goto unmap_and_free_vma;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001806
1807 /* Can addr have changed??
1808 *
1809 * Answer: Yes, several device drivers can do it in their
1810 * f_op->mmap method. -DaveM
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001811 * Bug: If addr is changed, prev, rb_link, rb_parent should
1812 * be updated for vma_link()
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001813 */
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001814 WARN_ON_ONCE(addr != vma->vm_start);
1815
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001816 addr = vma->vm_start;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001817 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 } else if (vm_flags & VM_SHARED) {
1819 error = shmem_zero_setup(vma);
1820 if (error)
1821 goto free_vma;
1822 }
1823
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001824 vma_link(mm, vma, prev, rb_link, rb_parent);
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001825 /* Once vma denies write, undo our temporary denial count */
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001826 if (file) {
1827 if (vm_flags & VM_SHARED)
1828 mapping_unmap_writable(file->f_mapping);
1829 if (vm_flags & VM_DENYWRITE)
1830 allow_write_access(file);
1831 }
Oleg Nesterove8686772013-09-11 14:20:20 -07001832 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001833out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001834 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001835
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001836 vm_write_begin(vma);
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001837 vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (vm_flags & VM_LOCKED) {
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001839 if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
1840 vma == get_gate_vma(current->mm)))
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001841 mm->locked_vm += (len >> PAGE_SHIFT);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001842 else
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001843 WRITE_ONCE(vma->vm_flags,
1844 vma->vm_flags & VM_LOCKED_CLEAR_MASK);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001845 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301846
Oleg Nesterovc7a3a882012-08-19 19:10:42 +02001847 if (file)
1848 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301849
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001850 /*
1851 * New (or expanded) vma always get soft dirty status.
1852 * Otherwise user-space soft-dirty page tracker won't
1853 * be able to distinguish situation when vma area unmapped,
1854 * then new mapped in-place (which must be aimed as
1855 * a completely new data area).
1856 */
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001857 WRITE_ONCE(vma->vm_flags, vma->vm_flags | VM_SOFTDIRTY);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001858
Peter Feiner64e45502014-10-13 15:55:46 -07001859 vma_set_page_prot(vma);
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001860 vm_write_end(vma);
Peter Feiner64e45502014-10-13 15:55:46 -07001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return addr;
1863
1864unmap_and_free_vma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 vma->vm_file = NULL;
1866 fput(file);
1867
1868 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001869 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1870 charged = 0;
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001871 if (vm_flags & VM_SHARED)
1872 mapping_unmap_writable(file->f_mapping);
1873allow_write_and_free_vma:
1874 if (vm_flags & VM_DENYWRITE)
1875 allow_write_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876free_vma:
1877 kmem_cache_free(vm_area_cachep, vma);
1878unacct_error:
1879 if (charged)
1880 vm_unacct_memory(charged);
1881 return error;
1882}
1883
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001884unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1885{
1886 /*
1887 * We implement the search by looking for an rbtree node that
1888 * immediately follows a suitable gap. That is,
1889 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1890 * - gap_end = vma->vm_start >= info->low_limit + length;
1891 * - gap_end - gap_start >= length
1892 */
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 /* Adjust search limits by the desired length */
1904 if (info->high_limit < length)
1905 return -ENOMEM;
1906 high_limit = info->high_limit - length;
1907
1908 if (info->low_limit > high_limit)
1909 return -ENOMEM;
1910 low_limit = info->low_limit + length;
1911
1912 /* Check if rbtree root looks promising */
1913 if (RB_EMPTY_ROOT(&mm->mm_rb))
1914 goto check_highest;
1915 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1916 if (vma->rb_subtree_gap < length)
1917 goto check_highest;
1918
1919 while (true) {
1920 /* Visit left subtree if it looks promising */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001921 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001922 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1923 struct vm_area_struct *left =
1924 rb_entry(vma->vm_rb.rb_left,
1925 struct vm_area_struct, vm_rb);
1926 if (left->rb_subtree_gap >= length) {
1927 vma = left;
1928 continue;
1929 }
1930 }
1931
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001932 gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001933check_current:
1934 /* Check if current node has a suitable gap */
1935 if (gap_start > high_limit)
1936 return -ENOMEM;
Hugh Dickinsce7fe852017-06-20 02:10:44 -07001937 if (gap_end >= low_limit &&
1938 gap_end > gap_start && gap_end - gap_start >= length)
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001939 goto found;
1940
1941 /* Visit right subtree if it looks promising */
1942 if (vma->vm_rb.rb_right) {
1943 struct vm_area_struct *right =
1944 rb_entry(vma->vm_rb.rb_right,
1945 struct vm_area_struct, vm_rb);
1946 if (right->rb_subtree_gap >= length) {
1947 vma = right;
1948 continue;
1949 }
1950 }
1951
1952 /* Go back up the rbtree to find next candidate node */
1953 while (true) {
1954 struct rb_node *prev = &vma->vm_rb;
1955 if (!rb_parent(prev))
1956 goto check_highest;
1957 vma = rb_entry(rb_parent(prev),
1958 struct vm_area_struct, vm_rb);
1959 if (prev == vma->vm_rb.rb_left) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001960 gap_start = vm_end_gap(vma->vm_prev);
1961 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001962 goto check_current;
1963 }
1964 }
1965 }
1966
1967check_highest:
1968 /* Check highest gap, which does not precede any rbtree node */
1969 gap_start = mm->highest_vm_end;
1970 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1971 if (gap_start > high_limit)
1972 return -ENOMEM;
1973
1974found:
1975 /* We found a suitable gap. Clip it with the original low_limit. */
1976 if (gap_start < info->low_limit)
1977 gap_start = info->low_limit;
1978
1979 /* Adjust gap address to the desired alignment */
1980 gap_start += (info->align_offset - gap_start) & info->align_mask;
1981
1982 VM_BUG_ON(gap_start + info->length > info->high_limit);
1983 VM_BUG_ON(gap_start + info->length > gap_end);
1984 return gap_start;
1985}
1986
1987unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1988{
1989 struct mm_struct *mm = current->mm;
1990 struct vm_area_struct *vma;
1991 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1992
1993 /* Adjust search length to account for worst case alignment overhead */
1994 length = info->length + info->align_mask;
1995 if (length < info->length)
1996 return -ENOMEM;
1997
1998 /*
1999 * Adjust search limits by the desired length.
2000 * See implementation comment at top of unmapped_area().
2001 */
2002 gap_end = info->high_limit;
2003 if (gap_end < length)
2004 return -ENOMEM;
2005 high_limit = gap_end - length;
2006
2007 if (info->low_limit > high_limit)
2008 return -ENOMEM;
2009 low_limit = info->low_limit + length;
2010
2011 /* Check highest gap, which does not precede any rbtree node */
2012 gap_start = mm->highest_vm_end;
2013 if (gap_start <= high_limit)
2014 goto found_highest;
2015
2016 /* Check if rbtree root looks promising */
2017 if (RB_EMPTY_ROOT(&mm->mm_rb))
2018 return -ENOMEM;
2019 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
2020 if (vma->rb_subtree_gap < length)
2021 return -ENOMEM;
2022
2023 while (true) {
2024 /* Visit right subtree if it looks promising */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002025 gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002026 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
2027 struct vm_area_struct *right =
2028 rb_entry(vma->vm_rb.rb_right,
2029 struct vm_area_struct, vm_rb);
2030 if (right->rb_subtree_gap >= length) {
2031 vma = right;
2032 continue;
2033 }
2034 }
2035
2036check_current:
2037 /* Check if current node has a suitable gap */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002038 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002039 if (gap_end < low_limit)
2040 return -ENOMEM;
Hugh Dickinsce7fe852017-06-20 02:10:44 -07002041 if (gap_start <= high_limit &&
2042 gap_end > gap_start && gap_end - gap_start >= length)
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002043 goto found;
2044
2045 /* Visit left subtree if it looks promising */
2046 if (vma->vm_rb.rb_left) {
2047 struct vm_area_struct *left =
2048 rb_entry(vma->vm_rb.rb_left,
2049 struct vm_area_struct, vm_rb);
2050 if (left->rb_subtree_gap >= length) {
2051 vma = left;
2052 continue;
2053 }
2054 }
2055
2056 /* Go back up the rbtree to find next candidate node */
2057 while (true) {
2058 struct rb_node *prev = &vma->vm_rb;
2059 if (!rb_parent(prev))
2060 return -ENOMEM;
2061 vma = rb_entry(rb_parent(prev),
2062 struct vm_area_struct, vm_rb);
2063 if (prev == vma->vm_rb.rb_right) {
2064 gap_start = vma->vm_prev ?
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002065 vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002066 goto check_current;
2067 }
2068 }
2069 }
2070
2071found:
2072 /* We found a suitable gap. Clip it with the original high_limit. */
2073 if (gap_end > info->high_limit)
2074 gap_end = info->high_limit;
2075
2076found_highest:
2077 /* Compute highest gap address at the desired alignment */
2078 gap_end -= info->length;
2079 gap_end -= (gap_end - info->align_offset) & info->align_mask;
2080
2081 VM_BUG_ON(gap_end < info->low_limit);
2082 VM_BUG_ON(gap_end < gap_start);
2083 return gap_end;
2084}
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086/* Get an address range which is currently unmapped.
2087 * For shmat() with addr=0.
2088 *
2089 * Ugly calling convention alert:
2090 * Return value with the low bits set means error value,
2091 * ie
2092 * if (ret & ~PAGE_MASK)
2093 * error = ret;
2094 *
2095 * This function "knows" that -ENOMEM has the bits set.
2096 */
2097#ifndef HAVE_ARCH_UNMAPPED_AREA
2098unsigned long
2099arch_get_unmapped_area(struct file *filp, unsigned long addr,
2100 unsigned long len, unsigned long pgoff, unsigned long flags)
2101{
2102 struct mm_struct *mm = current->mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002103 struct vm_area_struct *vma, *prev;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002104 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002106 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return -ENOMEM;
2108
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002109 if (flags & MAP_FIXED)
2110 return addr;
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (addr) {
2113 addr = PAGE_ALIGN(addr);
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002114 vma = find_vma_prev(mm, addr, &prev);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002115 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002116 (!vma || addr + len <= vm_start_gap(vma)) &&
2117 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return addr;
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002121 info.flags = 0;
2122 info.length = len;
Heiko Carstens4e99b022013-11-12 15:07:54 -08002123 info.low_limit = mm->mmap_base;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002124 info.high_limit = TASK_SIZE;
2125 info.align_mask = 0;
2126 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
vishnu.pscc71aba2014-10-09 15:26:29 -07002128#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130/*
2131 * This mmap-allocator allocates new areas top-down from below the
2132 * stack's low limit (the base):
2133 */
2134#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
2135unsigned long
2136arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
2137 const unsigned long len, const unsigned long pgoff,
2138 const unsigned long flags)
2139{
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002140 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct mm_struct *mm = current->mm;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002142 unsigned long addr = addr0;
2143 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* requested length too big for entire address space */
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002146 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return -ENOMEM;
2148
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002149 if (flags & MAP_FIXED)
2150 return addr;
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /* requesting a specific address */
2153 if (addr) {
2154 addr = PAGE_ALIGN(addr);
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002155 vma = find_vma_prev(mm, addr, &prev);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002156 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002157 (!vma || addr + len <= vm_start_gap(vma)) &&
2158 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return addr;
2160 }
2161
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002162 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
2163 info.length = len;
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002164 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002165 info.high_limit = mm->mmap_base;
2166 info.align_mask = 0;
2167 addr = vm_unmapped_area(&info);
Xiao Guangrongb716ad92012-03-21 16:33:56 -07002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /*
2170 * A failed mmap() very likely causes application failure,
2171 * so fall back to the bottom-up function here. This scenario
2172 * can happen with large stack limits and large mmap()
2173 * allocations.
2174 */
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002175 if (offset_in_page(addr)) {
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002176 VM_BUG_ON(addr != -ENOMEM);
2177 info.flags = 0;
2178 info.low_limit = TASK_UNMAPPED_BASE;
2179 info.high_limit = TASK_SIZE;
2180 addr = vm_unmapped_area(&info);
2181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 return addr;
2184}
2185#endif
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187unsigned long
2188get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
2189 unsigned long pgoff, unsigned long flags)
2190{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002191 unsigned long (*get_area)(struct file *, unsigned long,
2192 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Al Viro9206de92009-12-03 15:23:11 -05002194 unsigned long error = arch_mmap_check(addr, len, flags);
2195 if (error)
2196 return error;
2197
2198 /* Careful about overflows.. */
2199 if (len > TASK_SIZE)
2200 return -ENOMEM;
2201
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002202 get_area = current->mm->get_unmapped_area;
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002203 if (file) {
2204 if (file->f_op->get_unmapped_area)
2205 get_area = file->f_op->get_unmapped_area;
2206 } else if (flags & MAP_SHARED) {
2207 /*
2208 * mmap_region() will call shmem_zero_setup() to create a file,
2209 * so use shmem's get_unmapped_area in case it can be huge.
2210 * do_mmap_pgoff() will clear pgoff, so match alignment.
2211 */
2212 pgoff = 0;
2213 get_area = shmem_get_unmapped_area;
2214 }
2215
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002216 addr = get_area(file, addr, len, pgoff, flags);
2217 if (IS_ERR_VALUE(addr))
2218 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Linus Torvalds07ab67c2005-05-19 22:43:37 -07002220 if (addr > TASK_SIZE - len)
2221 return -ENOMEM;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002222 if (offset_in_page(addr))
Linus Torvalds07ab67c2005-05-19 22:43:37 -07002223 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002224
Al Viro9ac4ed42012-05-30 17:13:15 -04002225 error = security_mmap_addr(addr);
2226 return error ? error : addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229EXPORT_SYMBOL(get_unmapped_area);
2230
2231/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
Laurent Dufourd8173842018-04-17 16:33:23 +02002232static struct vm_area_struct *__find_vma(struct mm_struct *mm,
2233 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002235 struct rb_node *rb_node;
Laurent Dufourd8173842018-04-17 16:33:23 +02002236 struct vm_area_struct *vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002238 rb_node = mm->mm_rb.rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002240 while (rb_node) {
2241 struct vm_area_struct *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002243 tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
Rajman Mekaco841e31e2012-05-29 15:06:21 -07002244
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002245 if (tmp->vm_end > addr) {
2246 vma = tmp;
2247 if (tmp->vm_start <= addr)
2248 break;
2249 rb_node = rb_node->rb_left;
2250 } else
2251 rb_node = rb_node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002253
Laurent Dufourd8173842018-04-17 16:33:23 +02002254 return vma;
2255}
2256
2257struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
2258{
2259 struct vm_area_struct *vma;
2260
2261 /* Check the cache first. */
2262 vma = vmacache_find(mm, addr);
2263 if (likely(vma))
2264 return vma;
2265
2266 vma = __find_vma(mm, addr);
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002267 if (vma)
2268 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return vma;
2270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271EXPORT_SYMBOL(find_vma);
2272
Laurent Dufourd8173842018-04-17 16:33:23 +02002273#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
2274struct vm_area_struct *get_vma(struct mm_struct *mm, unsigned long addr)
2275{
2276 struct vm_area_struct *vma = NULL;
2277
2278 read_lock(&mm->mm_rb_lock);
2279 vma = __find_vma(mm, addr);
2280 if (vma)
2281 atomic_inc(&vma->vm_ref_count);
2282 read_unlock(&mm->mm_rb_lock);
2283
2284 return vma;
2285}
2286#endif
2287
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002288/*
2289 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291struct vm_area_struct *
2292find_vma_prev(struct mm_struct *mm, unsigned long addr,
2293 struct vm_area_struct **pprev)
2294{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002295 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002297 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05002298 if (vma) {
2299 *pprev = vma->vm_prev;
2300 } else {
2301 struct rb_node *rb_node = mm->mm_rb.rb_node;
2302 *pprev = NULL;
2303 while (rb_node) {
2304 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
2305 rb_node = rb_node->rb_right;
2306 }
2307 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002308 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
2311/*
2312 * Verify that the stack growth is acceptable and
2313 * update accounting. This is shared with both the
2314 * grow-up and grow-down cases.
2315 */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002316static int acct_stack_growth(struct vm_area_struct *vma,
2317 unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 struct mm_struct *mm = vma->vm_mm;
2320 struct rlimit *rlim = current->signal->rlim;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002321 unsigned long new_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 /* address space limit tests */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002324 if (!may_expand_vm(mm, vma->vm_flags, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return -ENOMEM;
2326
2327 /* Stack limit test */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002328 if (size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return -ENOMEM;
2330
2331 /* mlock limit tests */
2332 if (vma->vm_flags & VM_LOCKED) {
2333 unsigned long locked;
2334 unsigned long limit;
2335 locked = mm->locked_vm + grow;
Jason Low4db0c3c2015-04-15 16:14:08 -07002336 limit = READ_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
Jiri Slaby59e99e52010-03-05 13:41:44 -08002337 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (locked > limit && !capable(CAP_IPC_LOCK))
2339 return -ENOMEM;
2340 }
2341
Adam Litke0d59a012007-01-30 14:35:39 -08002342 /* Check to ensure the stack will not grow into a hugetlb-only region */
2343 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
2344 vma->vm_end - size;
2345 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
2346 return -EFAULT;
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /*
2349 * Overcommit.. This must be the final test, as it will
2350 * update security statistics.
2351 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01002352 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return -ENOMEM;
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return 0;
2356}
2357
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002358#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002360 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2361 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002363int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Oleg Nesterov09357812015-11-05 18:48:17 -08002365 struct mm_struct *mm = vma->vm_mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002366 struct vm_area_struct *next;
2367 unsigned long gap_addr;
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002368 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 if (!(vma->vm_flags & VM_GROWSUP))
2371 return -EFAULT;
2372
Helge Deller5d10ad622017-06-19 17:34:05 +02002373 /* Guard against exceeding limits of the address space. */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002374 address &= PAGE_MASK;
Helge Deller38dfd2e2017-07-14 14:49:38 -07002375 if (address >= (TASK_SIZE & PAGE_MASK))
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002376 return -ENOMEM;
Helge Deller5d10ad622017-06-19 17:34:05 +02002377 address += PAGE_SIZE;
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002378
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002379 /* Enforce stack_guard_gap */
2380 gap_addr = address + stack_guard_gap;
Helge Deller5d10ad622017-06-19 17:34:05 +02002381
2382 /* Guard against overflow */
2383 if (gap_addr < address || gap_addr > TASK_SIZE)
2384 gap_addr = TASK_SIZE;
2385
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002386 next = vma->vm_next;
Michal Hockoc57664b2017-07-10 15:49:51 -07002387 if (next && next->vm_start < gap_addr &&
2388 (next->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002389 if (!(next->vm_flags & VM_GROWSUP))
2390 return -ENOMEM;
2391 /* Check that both stack segments have the same anon_vma? */
2392 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002393
2394 /* We must make sure the anon_vma is allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if (unlikely(anon_vma_prepare(vma)))
2396 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 /*
2399 * vma->vm_start/vm_end cannot change under us because the caller
2400 * is required to hold the mmap_sem in read mode. We need the
2401 * anon_vma lock to serialize against concurrent expand_stacks.
2402 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002403 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 /* Somebody else might have raced and expanded it already */
2406 if (address > vma->vm_end) {
2407 unsigned long size, grow;
2408
2409 size = address - vma->vm_start;
2410 grow = (address - vma->vm_end) >> PAGE_SHIFT;
2411
Hugh Dickins42c36f62011-05-09 17:44:42 -07002412 error = -ENOMEM;
2413 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
2414 error = acct_stack_growth(vma, size, grow);
2415 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002416 /*
2417 * vma_gap_update() doesn't support concurrent
2418 * updates, but we only hold a shared mmap_sem
2419 * lock here, so we need to protect against
2420 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002421 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002422 * we don't guarantee that all growable vmas
2423 * in a mm share the same root anon vma.
2424 * So, we reuse mm->page_table_lock to guard
2425 * against concurrent vma expansions.
2426 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002427 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002428 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002429 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002430 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002431 anon_vma_interval_tree_pre_update_vma(vma);
Hugh Dickins42c36f62011-05-09 17:44:42 -07002432 vma->vm_end = address;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002433 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002434 if (vma->vm_next)
2435 vma_gap_update(vma->vm_next);
2436 else
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002437 mm->highest_vm_end = vm_end_gap(vma);
Oleg Nesterov09357812015-11-05 18:48:17 -08002438 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002439
Hugh Dickins42c36f62011-05-09 17:44:42 -07002440 perf_event_mmap(vma);
2441 }
Eric B Munson3af9e852010-05-18 15:30:49 +01002442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002444 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002445 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002446 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return error;
2448}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002449#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/*
2452 * vma is the first one with address < vma->vm_start. Have to extend vma.
2453 */
Michal Hockod05f3162011-05-24 17:11:44 -07002454int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002455 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Oleg Nesterov09357812015-11-05 18:48:17 -08002457 struct mm_struct *mm = vma->vm_mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002458 struct vm_area_struct *prev;
2459 unsigned long gap_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 int error;
2461
Eric Paris88694772007-11-26 18:47:26 -05002462 address &= PAGE_MASK;
Al Viroe5467852012-05-30 13:30:51 -04002463 error = security_mmap_addr(address);
Eric Paris88694772007-11-26 18:47:26 -05002464 if (error)
2465 return error;
2466
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002467 /* Enforce stack_guard_gap */
2468 gap_addr = address - stack_guard_gap;
2469 if (gap_addr > address)
2470 return -ENOMEM;
2471 prev = vma->vm_prev;
Michal Hockoc57664b2017-07-10 15:49:51 -07002472 if (prev && prev->vm_end > gap_addr &&
2473 (prev->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002474 if (!(prev->vm_flags & VM_GROWSDOWN))
2475 return -ENOMEM;
2476 /* Check that both stack segments have the same anon_vma? */
2477 }
2478
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002479 /* We must make sure the anon_vma is allocated. */
2480 if (unlikely(anon_vma_prepare(vma)))
2481 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 /*
2484 * vma->vm_start/vm_end cannot change under us because the caller
2485 * is required to hold the mmap_sem in read mode. We need the
2486 * anon_vma lock to serialize against concurrent expand_stacks.
2487 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002488 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 /* Somebody else might have raced and expanded it already */
2491 if (address < vma->vm_start) {
2492 unsigned long size, grow;
2493
2494 size = vma->vm_end - address;
2495 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2496
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002497 error = -ENOMEM;
2498 if (grow <= vma->vm_pgoff) {
2499 error = acct_stack_growth(vma, size, grow);
2500 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002501 /*
2502 * vma_gap_update() doesn't support concurrent
2503 * updates, but we only hold a shared mmap_sem
2504 * lock here, so we need to protect against
2505 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002506 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002507 * we don't guarantee that all growable vmas
2508 * in a mm share the same root anon vma.
2509 * So, we reuse mm->page_table_lock to guard
2510 * against concurrent vma expansions.
2511 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002512 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002513 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002514 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002515 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002516 anon_vma_interval_tree_pre_update_vma(vma);
Laurent Dufourdd2b4652018-04-17 16:33:15 +02002517 WRITE_ONCE(vma->vm_start, address);
2518 WRITE_ONCE(vma->vm_pgoff, vma->vm_pgoff - grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002519 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002520 vma_gap_update(vma);
Oleg Nesterov09357812015-11-05 18:48:17 -08002521 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002522
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002523 perf_event_mmap(vma);
2524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
2526 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002527 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002528 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002529 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return error;
2531}
2532
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002533/* enforced gap between the expanding stack and other mappings. */
2534unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
2535
2536static int __init cmdline_parse_stack_guard_gap(char *p)
2537{
2538 unsigned long val;
2539 char *endptr;
2540
2541 val = simple_strtoul(p, &endptr, 10);
2542 if (!*endptr)
2543 stack_guard_gap = val << PAGE_SHIFT;
2544
2545 return 0;
2546}
2547__setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
2548
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002549#ifdef CONFIG_STACK_GROWSUP
2550int expand_stack(struct vm_area_struct *vma, unsigned long address)
2551{
2552 return expand_upwards(vma, address);
2553}
2554
2555struct vm_area_struct *
2556find_extend_vma(struct mm_struct *mm, unsigned long addr)
2557{
2558 struct vm_area_struct *vma, *prev;
2559
2560 addr &= PAGE_MASK;
2561 vma = find_vma_prev(mm, addr, &prev);
2562 if (vma && (vma->vm_start <= addr))
2563 return vma;
Denys Vlasenko1c127182008-11-12 01:24:41 +01002564 if (!prev || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002565 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002566 if (prev->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002567 populate_vma_page_range(prev, addr, prev->vm_end, NULL);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002568 return prev;
2569}
2570#else
2571int expand_stack(struct vm_area_struct *vma, unsigned long address)
2572{
2573 return expand_downwards(vma, address);
2574}
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576struct vm_area_struct *
vishnu.pscc71aba2014-10-09 15:26:29 -07002577find_extend_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
vishnu.pscc71aba2014-10-09 15:26:29 -07002579 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 unsigned long start;
2581
2582 addr &= PAGE_MASK;
vishnu.pscc71aba2014-10-09 15:26:29 -07002583 vma = find_vma(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (!vma)
2585 return NULL;
2586 if (vma->vm_start <= addr)
2587 return vma;
2588 if (!(vma->vm_flags & VM_GROWSDOWN))
2589 return NULL;
2590 start = vma->vm_start;
2591 if (expand_stack(vma, addr))
2592 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002593 if (vma->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002594 populate_vma_page_range(vma, addr, start, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return vma;
2596}
2597#endif
2598
Jesse Barnese1d6d012014-12-12 16:55:27 -08002599EXPORT_SYMBOL_GPL(find_extend_vma);
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002602 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002604 *
2605 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002607static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002609 unsigned long nr_accounted = 0;
2610
Hugh Dickins365e9c872005-10-29 18:16:18 -07002611 /* Update high watermark before we lower total_vm */
2612 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002614 long nrpages = vma_pages(vma);
2615
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002616 if (vma->vm_flags & VM_ACCOUNT)
2617 nr_accounted += nrpages;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002618 vm_stat_account(mm, vma->vm_flags, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002619 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002620 } while (vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002621 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 validate_mm(mm);
2623}
2624
2625/*
2626 * Get rid of page table information in the indicated region.
2627 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002628 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 */
2630static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002631 struct vm_area_struct *vma, struct vm_area_struct *prev,
2632 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
vishnu.pscc71aba2014-10-09 15:26:29 -07002634 struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002635 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07002638 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002639 update_hiwater_rss(mm);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002640 unmap_vmas(&tlb, vma, start, end);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002641 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins6ee86302013-04-29 15:07:44 -07002642 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002643 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
2646/*
2647 * Create a list of vma's touched by the unmap, removing them from the mm's
2648 * vma list as we go..
2649 */
2650static void
2651detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2652 struct vm_area_struct *prev, unsigned long end)
2653{
2654 struct vm_area_struct **insertion_point;
2655 struct vm_area_struct *tail_vma = NULL;
2656
2657 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002658 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 do {
Laurent Dufourd8173842018-04-17 16:33:23 +02002660 vma_rb_erase(vma, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 mm->map_count--;
2662 tail_vma = vma;
2663 vma = vma->vm_next;
2664 } while (vma && vma->vm_start < end);
2665 *insertion_point = vma;
Michel Lespinassed3737182012-12-11 16:01:38 -08002666 if (vma) {
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002667 vma->vm_prev = prev;
Michel Lespinassed3737182012-12-11 16:01:38 -08002668 vma_gap_update(vma);
2669 } else
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002670 mm->highest_vm_end = prev ? vm_end_gap(prev) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 tail_vma->vm_next = NULL;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002672
2673 /* Kill the cache */
2674 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
2677/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002678 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2679 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 */
vishnu.pscc71aba2014-10-09 15:26:29 -07002681static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 unsigned long addr, int new_below)
2683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 struct vm_area_struct *new;
Chen Gange3975892015-09-08 15:03:38 -07002685 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Dan Williamscebe1392017-11-29 16:10:28 -08002687 if (vma->vm_ops && vma->vm_ops->split) {
2688 err = vma->vm_ops->split(vma, addr);
2689 if (err)
2690 return err;
2691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Christoph Lametere94b1762006-12-06 20:33:17 -08002693 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (!new)
Chen Gange3975892015-09-08 15:03:38 -07002695 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 /* most fields are the same, copy all, and then fixup */
2698 *new = *vma;
2699
Laurent Dufour6315ccc2018-04-17 16:33:13 +02002700 INIT_VMA(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (new_below)
2703 new->vm_end = addr;
2704 else {
2705 new->vm_start = addr;
2706 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2707 }
2708
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002709 err = vma_dup_policy(vma, new);
2710 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002711 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Daniel Forrestc4ea95d2014-12-02 15:59:42 -08002713 err = anon_vma_clone(new, vma);
2714 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002715 goto out_free_mpol;
2716
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002717 if (new->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 get_file(new->vm_file);
2719
2720 if (new->vm_ops && new->vm_ops->open)
2721 new->vm_ops->open(new);
2722
2723 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002724 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2726 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002727 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Rik van Riel5beb4932010-03-05 13:42:07 -08002729 /* Success. */
2730 if (!err)
2731 return 0;
2732
2733 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002734 if (new->vm_ops && new->vm_ops->close)
2735 new->vm_ops->close(new);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002736 if (new->vm_file)
Rik van Riel5beb4932010-03-05 13:42:07 -08002737 fput(new->vm_file);
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002738 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002739 out_free_mpol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002740 mpol_put(vma_policy(new));
Rik van Riel5beb4932010-03-05 13:42:07 -08002741 out_free_vma:
2742 kmem_cache_free(vm_area_cachep, new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002743 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002746/*
2747 * Split a vma into two pieces at address 'addr', a new vma is allocated
2748 * either for the first part or the tail.
2749 */
2750int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2751 unsigned long addr, int new_below)
2752{
2753 if (mm->map_count >= sysctl_max_map_count)
2754 return -ENOMEM;
2755
2756 return __split_vma(mm, vma, addr, new_below);
2757}
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759/* Munmap is split into 2 main parts -- this part which finds
2760 * what needs doing, and the areas themselves, which do the
2761 * work. This now handles partial unmappings.
2762 * Jeremy Fitzhardinge <jeremy@goop.org>
2763 */
2764int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2765{
2766 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002767 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002769 if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return -EINVAL;
2771
vishnu.pscc71aba2014-10-09 15:26:29 -07002772 len = PAGE_ALIGN(len);
2773 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return -EINVAL;
2775
2776 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002777 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002778 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002780 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002781 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 /* if it doesn't overlap, we have nothing.. */
2784 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002785 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 0;
2787
2788 /*
2789 * If we need to split any vma, do it now to save pain later.
2790 *
2791 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2792 * unmapped vm_area_struct will remain in use: so lower split_vma
2793 * places tmp vma above, and higher split_vma places tmp vma below.
2794 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002795 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002796 int error;
2797
2798 /*
2799 * Make sure that map_count on return from munmap() will
2800 * not exceed its limit; but let map_count go just above
2801 * its limit temporarily, to help free resources as expected.
2802 */
2803 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2804 return -ENOMEM;
2805
2806 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (error)
2808 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002809 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811
2812 /* Does it split the last one? */
2813 last = find_vma(mm, end);
2814 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002815 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (error)
2817 return error;
2818 }
vishnu.pscc71aba2014-10-09 15:26:29 -07002819 vma = prev ? prev->vm_next : mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002822 * unlock any mlock()ed ranges before detaching vmas
2823 */
2824 if (mm->locked_vm) {
2825 struct vm_area_struct *tmp = vma;
2826 while (tmp && tmp->vm_start < end) {
2827 if (tmp->vm_flags & VM_LOCKED) {
2828 mm->locked_vm -= vma_pages(tmp);
2829 munlock_vma_pages_all(tmp);
2830 }
2831 tmp = tmp->vm_next;
2832 }
2833 }
2834
2835 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 * Remove the vma's, and unmap the actual pages
2837 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002838 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2839 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Dave Hansen1de4fa12014-11-14 07:18:31 -08002841 arch_unmap(mm, vma, start, end);
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002844 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 return 0;
2847}
Guenter Roeckabfb4862016-03-24 10:39:14 -07002848EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Al Virobfce2812012-04-20 21:57:04 -04002850int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002851{
2852 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002853 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002854
Michal Hockoae798782016-05-23 16:25:33 -07002855 if (down_write_killable(&mm->mmap_sem))
2856 return -EINTR;
2857
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002858 ret = do_munmap(mm, start, len);
2859 up_write(&mm->mmap_sem);
2860 return ret;
2861}
2862EXPORT_SYMBOL(vm_munmap);
2863
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002864SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002866 int ret;
2867 struct mm_struct *mm = current->mm;
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 profile_munmap(addr);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002870 if (down_write_killable(&mm->mmap_sem))
2871 return -EINTR;
2872 ret = do_munmap(mm, addr, len);
2873 up_write(&mm->mmap_sem);
2874 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002877
2878/*
2879 * Emulation of deprecated remap_file_pages() syscall.
2880 */
2881SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
2882 unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
2883{
2884
2885 struct mm_struct *mm = current->mm;
2886 struct vm_area_struct *vma;
2887 unsigned long populate = 0;
2888 unsigned long ret = -EINVAL;
2889 struct file *file;
2890
Joe Perches756a025f02016-03-17 14:19:47 -07002891 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
2892 current->comm, current->pid);
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002893
2894 if (prot)
2895 return ret;
2896 start = start & PAGE_MASK;
2897 size = size & PAGE_MASK;
2898
2899 if (start + size <= start)
2900 return ret;
2901
2902 /* Does pgoff wrap? */
2903 if (pgoff + (size >> PAGE_SHIFT) < pgoff)
2904 return ret;
2905
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002906 if (down_write_killable(&mm->mmap_sem))
2907 return -EINTR;
2908
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002909 vma = find_vma(mm, start);
2910
2911 if (!vma || !(vma->vm_flags & VM_SHARED))
2912 goto out;
2913
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002914 if (start < vma->vm_start)
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002915 goto out;
2916
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002917 if (start + size > vma->vm_end) {
2918 struct vm_area_struct *next;
2919
2920 for (next = vma->vm_next; next; next = next->vm_next) {
2921 /* hole between vmas ? */
2922 if (next->vm_start != next->vm_prev->vm_end)
2923 goto out;
2924
2925 if (next->vm_file != vma->vm_file)
2926 goto out;
2927
2928 if (next->vm_flags != vma->vm_flags)
2929 goto out;
2930
2931 if (start + size <= next->vm_end)
2932 break;
2933 }
2934
2935 if (!next)
2936 goto out;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002937 }
2938
2939 prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
2940 prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
2941 prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
2942
2943 flags &= MAP_NONBLOCK;
2944 flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
2945 if (vma->vm_flags & VM_LOCKED) {
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002946 struct vm_area_struct *tmp;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002947 flags |= MAP_LOCKED;
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002948
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002949 /* drop PG_Mlocked flag for over-mapped range */
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002950 for (tmp = vma; tmp->vm_start >= start + size;
2951 tmp = tmp->vm_next) {
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07002952 /*
2953 * Split pmd and munlock page on the border
2954 * of the range.
2955 */
2956 vma_adjust_trans_huge(tmp, start, start + size, 0);
2957
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002958 munlock_vma_pages_range(tmp,
2959 max(tmp->vm_start, start),
2960 min(tmp->vm_end, start + size));
2961 }
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002962 }
2963
2964 file = get_file(vma->vm_file);
2965 ret = do_mmap_pgoff(vma->vm_file, start, size,
2966 prot, flags, pgoff, &populate);
2967 fput(file);
2968out:
2969 up_write(&mm->mmap_sem);
2970 if (populate)
2971 mm_populate(ret, populate);
2972 if (!IS_ERR_VALUE(ret))
2973 ret = 0;
2974 return ret;
2975}
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977static inline void verify_mm_writelocked(struct mm_struct *mm)
2978{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002979#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2981 WARN_ON(1);
2982 up_read(&mm->mmap_sem);
2983 }
2984#endif
2985}
2986
2987/*
2988 * this is really a simplified "do_mmap". it only handles
2989 * anonymous maps. eventually we may be able to do some
2990 * brk-specific accounting here.
2991 */
Kees Cookba093a62016-08-02 14:04:54 -07002992static int do_brk(unsigned long addr, unsigned long request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
vishnu.pscc71aba2014-10-09 15:26:29 -07002994 struct mm_struct *mm = current->mm;
2995 struct vm_area_struct *vma, *prev;
Kees Cookba093a62016-08-02 14:04:54 -07002996 unsigned long flags, len;
vishnu.pscc71aba2014-10-09 15:26:29 -07002997 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002999 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Kees Cookba093a62016-08-02 14:04:54 -07003001 len = PAGE_ALIGN(request);
3002 if (len < request)
3003 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (!len)
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Kirill Korotaev3a459752006-09-07 14:17:04 +04003007 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
3008
Al Viro2c6a1012009-12-03 19:40:46 -05003009 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08003010 if (offset_in_page(error))
Kirill Korotaev3a459752006-09-07 14:17:04 +04003011 return error;
3012
Davidlohr Bueso363ee172014-01-21 15:49:15 -08003013 error = mlock_future_check(mm, mm->def_flags, len);
3014 if (error)
3015 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 /*
3018 * mm->mmap_sem is required to protect against another thread
3019 * changing the mappings in case we sleep.
3020 */
3021 verify_mm_writelocked(mm);
3022
3023 /*
3024 * Clear old maps. this also does some error checking for us
3025 */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07003026 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
3027 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if (do_munmap(mm, addr, len))
3029 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
3031
3032 /* Check against address space limits *after* clearing old maps... */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003033 if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return -ENOMEM;
3035
3036 if (mm->map_count > sysctl_max_map_count)
3037 return -ENOMEM;
3038
Al Viro191c5422012-02-13 03:58:52 +00003039 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return -ENOMEM;
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07003043 vma = vma_merge(mm, prev, addr, addr + len, flags,
Colin Cross3e4578f2015-10-27 16:42:08 -07003044 NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX, NULL);
Rik van Rielba470de2008-10-18 20:26:50 -07003045 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 goto out;
3047
3048 /*
3049 * create a vma struct for an anonymous mapping
3050 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08003051 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 if (!vma) {
3053 vm_unacct_memory(len >> PAGE_SHIFT);
3054 return -ENOMEM;
3055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Laurent Dufour6315ccc2018-04-17 16:33:13 +02003057 INIT_VMA(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 vma->vm_mm = mm;
3059 vma->vm_start = addr;
3060 vma->vm_end = addr + len;
3061 vma->vm_pgoff = pgoff;
3062 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07003063 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 vma_link(mm, vma, prev, rb_link, rb_parent);
3065out:
Eric B Munson3af9e852010-05-18 15:30:49 +01003066 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 mm->total_vm += len >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003068 mm->data_vm += len >> PAGE_SHIFT;
Michel Lespinasse128557f2013-02-22 16:32:40 -08003069 if (flags & VM_LOCKED)
3070 mm->locked_vm += (len >> PAGE_SHIFT);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07003071 vma->vm_flags |= VM_SOFTDIRTY;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003075int vm_brk(unsigned long addr, unsigned long len)
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003076{
3077 struct mm_struct *mm = current->mm;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003078 int ret;
Michel Lespinasse128557f2013-02-22 16:32:40 -08003079 bool populate;
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003080
Michal Hocko2d6c9282016-05-23 16:25:42 -07003081 if (down_write_killable(&mm->mmap_sem))
3082 return -EINTR;
3083
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003084 ret = do_brk(addr, len);
Michel Lespinasse128557f2013-02-22 16:32:40 -08003085 populate = ((mm->def_flags & VM_LOCKED) != 0);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003086 up_write(&mm->mmap_sem);
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003087 if (populate && !ret)
Michel Lespinasse128557f2013-02-22 16:32:40 -08003088 mm_populate(addr, len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003089 return ret;
3090}
3091EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093/* Release all mmaps. */
3094void exit_mmap(struct mm_struct *mm)
3095{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07003096 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07003097 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 unsigned long nr_accounted = 0;
3099
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02003100 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07003101 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02003102
Rik van Rielba470de2008-10-18 20:26:50 -07003103 if (mm->locked_vm) {
3104 vma = mm->mmap;
3105 while (vma) {
3106 if (vma->vm_flags & VM_LOCKED)
3107 munlock_vma_pages_all(vma);
3108 vma = vma->vm_next;
3109 }
3110 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08003111
3112 arch_exit_mmap(mm);
3113
Rik van Rielba470de2008-10-18 20:26:50 -07003114 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08003115 if (!vma) /* Can happen if dup_mmap() received an OOM */
3116 return;
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 flush_cache_mm(mm);
Linus Torvalds2b047252013-08-15 11:42:25 -07003120 tlb_gather_mmu(&tlb, mm, 0, -1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08003121 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07003122 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003123 unmap_vmas(&tlb, vma, 0, -1);
Hugh Dickins9ba69292009-09-21 17:02:20 -07003124
Michal Hocko065e76d2017-12-14 15:33:15 -08003125 if (unlikely(mm_is_oom_victim(mm))) {
Andrea Arcangeli07b039e92017-09-06 16:25:00 -07003126 /*
3127 * Wait for oom_reap_task() to stop working on this
3128 * mm. Because MMF_OOM_SKIP is already set before
3129 * calling down_read(), oom_reap_task() will not run
3130 * on this "mm" post up_write().
3131 *
Michal Hocko065e76d2017-12-14 15:33:15 -08003132 * mm_is_oom_victim() cannot be set from under us
3133 * either because victim->mm is already set to NULL
Andrea Arcangeli07b039e92017-09-06 16:25:00 -07003134 * under task_lock before calling mmput and oom_mm is
Michal Hocko065e76d2017-12-14 15:33:15 -08003135 * set not NULL by the OOM killer only if victim->mm
Andrea Arcangeli07b039e92017-09-06 16:25:00 -07003136 * is found not NULL while holding the task_lock.
3137 */
Michal Hocko065e76d2017-12-14 15:33:15 -08003138 set_bit(MMF_OOM_SKIP, &mm->flags);
Andrea Arcangeli07b039e92017-09-06 16:25:00 -07003139 down_write(&mm->mmap_sem);
3140 up_write(&mm->mmap_sem);
3141 }
Hugh Dickins6ee86302013-04-29 15:07:44 -07003142 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05003143 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07003146 * Walk the list again, actually closing and freeing it,
3147 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003149 while (vma) {
3150 if (vma->vm_flags & VM_ACCOUNT)
3151 nr_accounted += vma_pages(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07003152 vma = remove_vma(vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003153 }
3154 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
3157/* Insert vm structure into process list sorted by address
3158 * and into the inode's i_mmap tree. If vm_file is non-NULL
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003159 * then i_mmap_rwsem is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 */
Hugh Dickins6597d782012-10-08 16:29:07 -07003161int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Hugh Dickins6597d782012-10-08 16:29:07 -07003163 struct vm_area_struct *prev;
3164 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Chen Gangc9d13f52015-09-08 15:04:08 -07003166 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
3167 &prev, &rb_link, &rb_parent))
3168 return -ENOMEM;
3169 if ((vma->vm_flags & VM_ACCOUNT) &&
3170 security_vm_enough_memory_mm(mm, vma_pages(vma)))
3171 return -ENOMEM;
3172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /*
3174 * The vm_pgoff of a purely anonymous vma should be irrelevant
3175 * until its first write fault, when page's anon_vma and index
3176 * are set. But now set the vm_pgoff it will almost certainly
3177 * end up with (unless mremap moves it elsewhere before that
3178 * first wfault), so /proc/pid/maps tells a consistent story.
3179 *
3180 * By setting it to reflect the virtual start address of the
3181 * vma, merges and splits can happen in a seamless way, just
3182 * using the existing file pgoff checks and manipulations.
3183 * Similarly in do_mmap_pgoff and in do_brk.
3184 */
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07003185 if (vma_is_anonymous(vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 BUG_ON(vma->anon_vma);
3187 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
3188 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05303189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 vma_link(mm, vma, prev, rb_link, rb_parent);
3191 return 0;
3192}
3193
3194/*
3195 * Copy the vma structure to a new location in the same mm,
3196 * prior to moving page table entries, to effect an mremap move.
3197 */
3198struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
Michel Lespinasse38a76012012-10-08 16:31:50 -07003199 unsigned long addr, unsigned long len, pgoff_t pgoff,
3200 bool *need_rmap_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 struct vm_area_struct *vma = *vmap;
3203 unsigned long vma_start = vma->vm_start;
3204 struct mm_struct *mm = vma->vm_mm;
3205 struct vm_area_struct *new_vma, *prev;
3206 struct rb_node **rb_link, *rb_parent;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003207 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 /*
3210 * If anonymous vma has not yet been faulted, update new pgoff
3211 * to match new location, to increase its chance of merging.
3212 */
Oleg Nesterovce757992015-09-08 14:58:34 -07003213 if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003215 faulted_in_anon_vma = false;
3216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Hugh Dickins6597d782012-10-08 16:29:07 -07003218 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
3219 return NULL; /* should never get here */
Laurent Dufour3545b7e2018-04-17 16:33:16 +02003220
3221 /* There is 3 cases to manage here in
3222 * AAAA AAAA AAAA AAAA
3223 * PPPP.... PPPP......NNNN PPPP....NNNN PP........NN
3224 * PPPPPPPP(A) PPPP..NNNNNNNN(B) PPPPPPPPPPPP(1) NULL
3225 * PPPPPPPPNNNN(2)
3226 * PPPPNNNNNNNN(3)
3227 *
3228 * new_vma == prev in case A,1,2
3229 * new_vma == next in case B,3
3230 */
3231 new_vma = __vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
3232 vma->anon_vma, vma->vm_file, pgoff,
3233 vma_policy(vma), vma->vm_userfaultfd_ctx,
3234 vma_get_anon_name(vma), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (new_vma) {
3236 /*
3237 * Source vma may have been merged into new_vma
3238 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003239 if (unlikely(vma_start >= new_vma->vm_start &&
3240 vma_start < new_vma->vm_end)) {
3241 /*
3242 * The only way we can get a vma_merge with
3243 * self during an mremap is if the vma hasn't
3244 * been faulted in yet and we were allowed to
3245 * reset the dst vma->vm_pgoff to the
3246 * destination address of the mremap to allow
3247 * the merge to happen. mremap must change the
3248 * vm_pgoff linearity between src and dst vmas
3249 * (in turn preventing a vma_merge) to be
3250 * safe. It is only safe to keep the vm_pgoff
3251 * linear if there are no pages mapped yet.
3252 */
Sasha Levin81d1b092014-10-09 15:28:10 -07003253 VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
Michel Lespinasse38a76012012-10-08 16:31:50 -07003254 *vmap = vma = new_vma;
Michel Lespinasse108d6642012-10-08 16:31:36 -07003255 }
Michel Lespinasse38a76012012-10-08 16:31:50 -07003256 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08003258 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Chen Gange3975892015-09-08 15:03:38 -07003259 if (!new_vma)
3260 goto out;
3261 *new_vma = *vma;
3262 new_vma->vm_start = addr;
3263 new_vma->vm_end = addr + len;
3264 new_vma->vm_pgoff = pgoff;
3265 if (vma_dup_policy(vma, new_vma))
3266 goto out_free_vma;
Laurent Dufour6315ccc2018-04-17 16:33:13 +02003267 INIT_VMA(new_vma);
Chen Gange3975892015-09-08 15:03:38 -07003268 if (anon_vma_clone(new_vma, vma))
3269 goto out_free_mempol;
3270 if (new_vma->vm_file)
3271 get_file(new_vma->vm_file);
3272 if (new_vma->vm_ops && new_vma->vm_ops->open)
3273 new_vma->vm_ops->open(new_vma);
Laurent Dufour3545b7e2018-04-17 16:33:16 +02003274 /*
3275 * As the VMA is linked right now, it may be hit by the
3276 * speculative page fault handler. But we don't want it to
3277 * to start mapping page in this area until the caller has
3278 * potentially move the pte from the moved VMA. To prevent
3279 * that we protect it right now, and let the caller unprotect
3280 * it once the move is done.
3281 */
3282 vm_raw_write_begin(new_vma);
Chen Gange3975892015-09-08 15:03:38 -07003283 vma_link(mm, new_vma, prev, rb_link, rb_parent);
3284 *need_rmap_locks = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 }
3286 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003287
Chen Gange3975892015-09-08 15:03:38 -07003288out_free_mempol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07003289 mpol_put(vma_policy(new_vma));
Chen Gange3975892015-09-08 15:03:38 -07003290out_free_vma:
Rik van Riel5beb4932010-03-05 13:42:07 -08003291 kmem_cache_free(vm_area_cachep, new_vma);
Chen Gange3975892015-09-08 15:03:38 -07003292out:
Rik van Riel5beb4932010-03-05 13:42:07 -08003293 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
akpm@osdl.org119f6572005-05-01 08:58:35 -07003295
3296/*
3297 * Return true if the calling process may expand its vm space by the passed
3298 * number of pages
3299 */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003300bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
akpm@osdl.org119f6572005-05-01 08:58:35 -07003301{
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003302 if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
3303 return false;
akpm@osdl.org119f6572005-05-01 08:58:35 -07003304
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003305 if (is_data_mapping(flags) &&
3306 mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07003307 /* Workaround for Valgrind */
3308 if (rlimit(RLIMIT_DATA) == 0 &&
3309 mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT)
3310 return true;
3311 if (!ignore_rlimit_data) {
3312 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 -08003313 current->comm, current->pid,
3314 (mm->data_vm + npages) << PAGE_SHIFT,
3315 rlimit(RLIMIT_DATA));
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003316 return false;
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07003317 }
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003318 }
akpm@osdl.org119f6572005-05-01 08:58:35 -07003319
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003320 return true;
3321}
3322
3323void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
3324{
3325 mm->total_vm += npages;
3326
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003327 if (is_exec_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003328 mm->exec_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003329 else if (is_stack_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003330 mm->stack_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003331 else if (is_data_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003332 mm->data_vm += npages;
akpm@osdl.org119f6572005-05-01 08:58:35 -07003333}
Roland McGrathfa5dc222007-02-08 14:20:41 -08003334
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003335static int special_mapping_fault(struct vm_area_struct *vma,
3336 struct vm_fault *vmf);
3337
3338/*
3339 * Having a close hook prevents vma merging regardless of flags.
3340 */
3341static void special_mapping_close(struct vm_area_struct *vma)
3342{
3343}
3344
3345static const char *special_mapping_name(struct vm_area_struct *vma)
3346{
3347 return ((struct vm_special_mapping *)vma->vm_private_data)->name;
3348}
3349
Dmitry Safonovb059a452016-06-28 14:35:38 +03003350static int special_mapping_mremap(struct vm_area_struct *new_vma)
3351{
3352 struct vm_special_mapping *sm = new_vma->vm_private_data;
3353
3354 if (sm->mremap)
3355 return sm->mremap(sm, new_vma);
3356 return 0;
3357}
3358
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003359static const struct vm_operations_struct special_mapping_vmops = {
3360 .close = special_mapping_close,
3361 .fault = special_mapping_fault,
Dmitry Safonovb059a452016-06-28 14:35:38 +03003362 .mremap = special_mapping_mremap,
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003363 .name = special_mapping_name,
3364};
3365
3366static const struct vm_operations_struct legacy_special_mapping_vmops = {
3367 .close = special_mapping_close,
3368 .fault = special_mapping_fault,
3369};
Roland McGrathfa5dc222007-02-08 14:20:41 -08003370
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003371static int special_mapping_fault(struct vm_area_struct *vma,
3372 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003373{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003374 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003375 struct page **pages;
3376
Andy Lutomirskif872f542015-12-29 20:12:19 -08003377 if (vma->vm_ops == &legacy_special_mapping_vmops) {
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003378 pages = vma->vm_private_data;
Andy Lutomirskif872f542015-12-29 20:12:19 -08003379 } else {
3380 struct vm_special_mapping *sm = vma->vm_private_data;
3381
3382 if (sm->fault)
3383 return sm->fault(sm, vma, vmf);
3384
3385 pages = sm->pages;
3386 }
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003387
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07003388 for (pgoff = vmf->pgoff; pgoff && *pages; ++pages)
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003389 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003390
3391 if (*pages) {
3392 struct page *page = *pages;
3393 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003394 vmf->page = page;
3395 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003396 }
3397
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003398 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003399}
3400
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003401static struct vm_area_struct *__install_special_mapping(
3402 struct mm_struct *mm,
3403 unsigned long addr, unsigned long len,
Chen Gang27f28b92015-11-05 18:48:41 -08003404 unsigned long vm_flags, void *priv,
3405 const struct vm_operations_struct *ops)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003406{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003407 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003408 struct vm_area_struct *vma;
3409
3410 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
3411 if (unlikely(vma == NULL))
Stefani Seibold3935ed62014-03-17 23:22:02 +01003412 return ERR_PTR(-ENOMEM);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003413
Laurent Dufour6315ccc2018-04-17 16:33:13 +02003414 INIT_VMA(vma);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003415 vma->vm_mm = mm;
3416 vma->vm_start = addr;
3417 vma->vm_end = addr + len;
3418
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07003419 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY;
Coly Li3ed75eb2007-10-18 23:39:15 -07003420 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003421
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003422 vma->vm_ops = ops;
3423 vma->vm_private_data = priv;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003424
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003425 ret = insert_vm_struct(mm, vma);
3426 if (ret)
3427 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003428
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003429 vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003430
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003431 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02003432
Stefani Seibold3935ed62014-03-17 23:22:02 +01003433 return vma;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003434
3435out:
3436 kmem_cache_free(vm_area_cachep, vma);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003437 return ERR_PTR(ret);
3438}
3439
Dmitry Safonov2eefd872016-09-05 16:33:05 +03003440bool vma_is_special_mapping(const struct vm_area_struct *vma,
3441 const struct vm_special_mapping *sm)
3442{
3443 return vma->vm_private_data == sm &&
3444 (vma->vm_ops == &special_mapping_vmops ||
3445 vma->vm_ops == &legacy_special_mapping_vmops);
3446}
3447
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003448/*
3449 * Called with mm->mmap_sem held for writing.
3450 * Insert a new vma covering the given region, with the given flags.
3451 * Its pages are supplied by the given array of struct page *.
3452 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3453 * The region past the last page supplied will always produce SIGBUS.
3454 * The array pointer and the pages it points to are assumed to stay alive
3455 * for as long as this mapping might exist.
3456 */
3457struct vm_area_struct *_install_special_mapping(
3458 struct mm_struct *mm,
3459 unsigned long addr, unsigned long len,
3460 unsigned long vm_flags, const struct vm_special_mapping *spec)
3461{
Chen Gang27f28b92015-11-05 18:48:41 -08003462 return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
3463 &special_mapping_vmops);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003464}
3465
Stefani Seibold3935ed62014-03-17 23:22:02 +01003466int install_special_mapping(struct mm_struct *mm,
3467 unsigned long addr, unsigned long len,
3468 unsigned long vm_flags, struct page **pages)
3469{
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003470 struct vm_area_struct *vma = __install_special_mapping(
Chen Gang27f28b92015-11-05 18:48:41 -08003471 mm, addr, len, vm_flags, (void *)pages,
3472 &legacy_special_mapping_vmops);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003473
Duan Jiong14bd5b42014-06-04 16:07:05 -07003474 return PTR_ERR_OR_ZERO(vma);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003475}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003476
3477static DEFINE_MUTEX(mm_all_locks_mutex);
3478
Peter Zijlstra454ed842008-08-11 09:30:25 +02003479static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003480{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003481 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003482 /*
3483 * The LSB of head.next can't change from under us
3484 * because we hold the mm_all_locks_mutex.
3485 */
Jiri Kosina572043c2013-01-11 14:31:59 -08003486 down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003487 /*
3488 * We can safely modify head.next after taking the
Ingo Molnar5a505082012-12-02 19:56:46 +00003489 * anon_vma->root->rwsem. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003490 * the same anon_vma we won't take it again.
3491 *
3492 * No need of atomic instructions here, head.next
3493 * can't change from under us thanks to the
Ingo Molnar5a505082012-12-02 19:56:46 +00003494 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003495 */
3496 if (__test_and_set_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003497 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003498 BUG();
3499 }
3500}
3501
Peter Zijlstra454ed842008-08-11 09:30:25 +02003502static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003503{
3504 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3505 /*
3506 * AS_MM_ALL_LOCKS can't change from under us because
3507 * we hold the mm_all_locks_mutex.
3508 *
3509 * Operations on ->flags have to be atomic because
3510 * even if AS_MM_ALL_LOCKS is stable thanks to the
3511 * mm_all_locks_mutex, there may be other cpus
3512 * changing other bitflags in parallel to us.
3513 */
3514 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
3515 BUG();
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003516 down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003517 }
3518}
3519
3520/*
3521 * This operation locks against the VM for all pte/vma/mm related
3522 * operations that could ever happen on a certain mm. This includes
3523 * vmtruncate, try_to_unmap, and all page faults.
3524 *
3525 * The caller must take the mmap_sem in write mode before calling
3526 * mm_take_all_locks(). The caller isn't allowed to release the
3527 * mmap_sem until mm_drop_all_locks() returns.
3528 *
3529 * mmap_sem in write mode is required in order to block all operations
3530 * that could modify pagetables and free pages without need of
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -08003531 * altering the vma layout. It's also needed in write mode to avoid new
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003532 * anon_vmas to be associated with existing vmas.
3533 *
3534 * A single task can't take more than one mm_take_all_locks() in a row
3535 * or it would deadlock.
3536 *
Michel Lespinassebf181b92012-10-08 16:31:39 -07003537 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003538 * mapping->flags avoid to take the same lock twice, if more than one
3539 * vma in this mm is backed by the same anon_vma or address_space.
3540 *
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003541 * We take locks in following order, accordingly to comment at beginning
3542 * of mm/rmap.c:
3543 * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
3544 * hugetlb mapping);
3545 * - all i_mmap_rwsem locks;
3546 * - all anon_vma->rwseml
3547 *
3548 * We can take all locks within these types randomly because the VM code
3549 * doesn't nest them and we protected from parallel mm_take_all_locks() by
3550 * mm_all_locks_mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003551 *
3552 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3553 * that may have to take thousand of locks.
3554 *
3555 * mm_take_all_locks() can fail if it's interrupted by signals.
3556 */
3557int mm_take_all_locks(struct mm_struct *mm)
3558{
3559 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003560 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003561
3562 BUG_ON(down_read_trylock(&mm->mmap_sem));
3563
3564 mutex_lock(&mm_all_locks_mutex);
3565
3566 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3567 if (signal_pending(current))
3568 goto out_unlock;
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003569 if (vma->vm_file && vma->vm_file->f_mapping &&
3570 is_vm_hugetlb_page(vma))
3571 vm_lock_mapping(mm, vma->vm_file->f_mapping);
3572 }
3573
3574 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3575 if (signal_pending(current))
3576 goto out_unlock;
3577 if (vma->vm_file && vma->vm_file->f_mapping &&
3578 !is_vm_hugetlb_page(vma))
Peter Zijlstra454ed842008-08-11 09:30:25 +02003579 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003580 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003581
3582 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3583 if (signal_pending(current))
3584 goto out_unlock;
3585 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003586 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3587 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003588 }
3589
Kautuk Consul584cff52011-10-31 17:08:59 -07003590 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003591
3592out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07003593 mm_drop_all_locks(mm);
3594 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003595}
3596
3597static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
3598{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003599 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003600 /*
3601 * The LSB of head.next can't change to 0 from under
3602 * us because we hold the mm_all_locks_mutex.
3603 *
3604 * We must however clear the bitflag before unlocking
Michel Lespinassebf181b92012-10-08 16:31:39 -07003605 * the vma so the users using the anon_vma->rb_root will
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003606 * never see our bitflag.
3607 *
3608 * No need of atomic instructions here, head.next
3609 * can't change from under us until we release the
Ingo Molnar5a505082012-12-02 19:56:46 +00003610 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003611 */
3612 if (!__test_and_clear_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003613 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003614 BUG();
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08003615 anon_vma_unlock_write(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003616 }
3617}
3618
3619static void vm_unlock_mapping(struct address_space *mapping)
3620{
3621 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3622 /*
3623 * AS_MM_ALL_LOCKS can't change to 0 from under us
3624 * because we hold the mm_all_locks_mutex.
3625 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003626 i_mmap_unlock_write(mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003627 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
3628 &mapping->flags))
3629 BUG();
3630 }
3631}
3632
3633/*
3634 * The mmap_sem cannot be released by the caller until
3635 * mm_drop_all_locks() returns.
3636 */
3637void mm_drop_all_locks(struct mm_struct *mm)
3638{
3639 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003640 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003641
3642 BUG_ON(down_read_trylock(&mm->mmap_sem));
3643 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
3644
3645 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3646 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003647 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3648 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003649 if (vma->vm_file && vma->vm_file->f_mapping)
3650 vm_unlock_mapping(vma->vm_file->f_mapping);
3651 }
3652
3653 mutex_unlock(&mm_all_locks_mutex);
3654}
David Howells8feae132009-01-08 12:04:47 +00003655
3656/*
3657 * initialise the VMA slab
3658 */
3659void __init mmap_init(void)
3660{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003661 int ret;
3662
Tejun Heo908c7f12014-09-08 09:51:29 +09003663 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003664 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00003665}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003666
3667/*
3668 * Initialise sysctl_user_reserve_kbytes.
3669 *
3670 * This is intended to prevent a user from starting a single memory hogging
3671 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3672 * mode.
3673 *
3674 * The default value is min(3% of free memory, 128MB)
3675 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3676 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003677static int init_user_reserve(void)
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003678{
3679 unsigned long free_kbytes;
3680
3681 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3682
3683 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
3684 return 0;
3685}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003686subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003687
3688/*
3689 * Initialise sysctl_admin_reserve_kbytes.
3690 *
3691 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3692 * to log in and kill a memory hogging process.
3693 *
3694 * Systems with more than 256MB will reserve 8MB, enough to recover
3695 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3696 * only reserve 3% of free pages by default.
3697 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003698static int init_admin_reserve(void)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003699{
3700 unsigned long free_kbytes;
3701
3702 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3703
3704 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
3705 return 0;
3706}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003707subsys_initcall(init_admin_reserve);
Andrew Shewmaker16408792013-04-29 15:08:12 -07003708
3709/*
3710 * Reinititalise user and admin reserves if memory is added or removed.
3711 *
3712 * The default user reserve max is 128MB, and the default max for the
3713 * admin reserve is 8MB. These are usually, but not always, enough to
3714 * enable recovery from a memory hogging process using login/sshd, a shell,
3715 * and tools like top. It may make sense to increase or even disable the
3716 * reserve depending on the existence of swap or variations in the recovery
3717 * tools. So, the admin may have changed them.
3718 *
3719 * If memory is added and the reserves have been eliminated or increased above
3720 * the default max, then we'll trust the admin.
3721 *
3722 * If memory is removed and there isn't enough free memory, then we
3723 * need to reset the reserves.
3724 *
3725 * Otherwise keep the reserve set by the admin.
3726 */
3727static int reserve_mem_notifier(struct notifier_block *nb,
3728 unsigned long action, void *data)
3729{
3730 unsigned long tmp, free_kbytes;
3731
3732 switch (action) {
3733 case MEM_ONLINE:
3734 /* Default max is 128MB. Leave alone if modified by operator. */
3735 tmp = sysctl_user_reserve_kbytes;
3736 if (0 < tmp && tmp < (1UL << 17))
3737 init_user_reserve();
3738
3739 /* Default max is 8MB. Leave alone if modified by operator. */
3740 tmp = sysctl_admin_reserve_kbytes;
3741 if (0 < tmp && tmp < (1UL << 13))
3742 init_admin_reserve();
3743
3744 break;
3745 case MEM_OFFLINE:
3746 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3747
3748 if (sysctl_user_reserve_kbytes > free_kbytes) {
3749 init_user_reserve();
3750 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3751 sysctl_user_reserve_kbytes);
3752 }
3753
3754 if (sysctl_admin_reserve_kbytes > free_kbytes) {
3755 init_admin_reserve();
3756 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3757 sysctl_admin_reserve_kbytes);
3758 }
3759 break;
3760 default:
3761 break;
3762 }
3763 return NOTIFY_OK;
3764}
3765
3766static struct notifier_block reserve_mem_nb = {
3767 .notifier_call = reserve_mem_notifier,
3768};
3769
3770static int __meminit init_reserve_notifier(void)
3771{
3772 if (register_hotmemory_notifier(&reserve_mem_nb))
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07003773 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
Andrew Shewmaker16408792013-04-29 15:08:12 -07003774
3775 return 0;
3776}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003777subsys_initcall(init_reserve_notifier);