blob: f54959786573297473755db586e1a270aa29645d [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
Al Viro40401532012-02-13 03:58:52 +00001401/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001402 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001404unsigned long do_mmap(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 unsigned long len, unsigned long prot,
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001406 unsigned long flags, vm_flags_t vm_flags,
1407 unsigned long pgoff, unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
vishnu.pscc71aba2014-10-09 15:26:29 -07001409 struct mm_struct *mm = current->mm;
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001410 int pkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Michel Lespinasse41badc12013-02-22 16:32:47 -08001412 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001413
Piotr Kwapulinskie37609b2015-06-24 16:58:39 -07001414 if (!len)
1415 return -EINVAL;
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /*
1418 * Does the application expect PROT_READ to imply PROT_EXEC?
1419 *
1420 * (the exception is when the underlying filesystem is noexec
1421 * mounted, in which case we dont add PROT_EXEC.)
1422 */
1423 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Eric W. Biederman90f85722015-06-29 14:42:03 -05001424 if (!(file && path_noexec(&file->f_path)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 prot |= PROT_EXEC;
1426
Eric Paris7cd94142007-11-26 18:47:40 -05001427 if (!(flags & MAP_FIXED))
1428 addr = round_hint_to_min(addr);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* Careful about overflows.. */
1431 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001432 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return -ENOMEM;
1434
1435 /* offset overflow? */
1436 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
vishnu.pscc71aba2014-10-09 15:26:29 -07001437 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /* Too many mappings? */
1440 if (mm->map_count > sysctl_max_map_count)
1441 return -ENOMEM;
1442
1443 /* Obtain the address to map to. we verify (or select) it and ensure
1444 * that it represents a valid section of the address space.
1445 */
1446 addr = get_unmapped_area(file, addr, len, pgoff, flags);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001447 if (offset_in_page(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return addr;
1449
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001450 if (prot == PROT_EXEC) {
1451 pkey = execute_only_pkey(mm);
1452 if (pkey < 0)
1453 pkey = 0;
1454 }
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /* Do simple checking here so the lower-level routines won't have
1457 * to. we assume access permissions have been handled by the open
1458 * of the memory object, so we don't do any here.
1459 */
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001460 vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1462
Huang Shijiecdf7b342009-09-21 17:03:36 -07001463 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (!can_do_mlock())
1465 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001466
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001467 if (mlock_future_check(mm, vm_flags, len))
1468 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (file) {
Oleg Nesterov077bf222013-09-11 14:20:19 -07001471 struct inode *inode = file_inode(file);
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 switch (flags & MAP_TYPE) {
1474 case MAP_SHARED:
1475 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1476 return -EACCES;
1477
1478 /*
1479 * Make sure we don't allow writing to an append-only
1480 * file..
1481 */
1482 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1483 return -EACCES;
1484
1485 /*
1486 * Make sure there are no mandatory locks on the file.
1487 */
Jeff Laytond7a06982014-03-10 09:54:15 -04001488 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return -EAGAIN;
1490
1491 vm_flags |= VM_SHARED | VM_MAYSHARE;
1492 if (!(file->f_mode & FMODE_WRITE))
1493 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1494
1495 /* fall through */
1496 case MAP_PRIVATE:
1497 if (!(file->f_mode & FMODE_READ))
1498 return -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001499 if (path_noexec(&file->f_path)) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001500 if (vm_flags & VM_EXEC)
1501 return -EPERM;
1502 vm_flags &= ~VM_MAYEXEC;
1503 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001504
Al Viro72c2d532013-09-22 16:27:52 -04001505 if (!file->f_op->mmap)
Linus Torvalds80c56062006-10-15 14:09:55 -07001506 return -ENODEV;
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001507 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1508 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
1510
1511 default:
1512 return -EINVAL;
1513 }
1514 } else {
1515 switch (flags & MAP_TYPE) {
1516 case MAP_SHARED:
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001517 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1518 return -EINVAL;
Tejun Heoce363942008-09-03 16:09:47 +02001519 /*
1520 * Ignore pgoff.
1521 */
1522 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 vm_flags |= VM_SHARED | VM_MAYSHARE;
1524 break;
1525 case MAP_PRIVATE:
1526 /*
1527 * Set pgoff according to addr for anon_vma.
1528 */
1529 pgoff = addr >> PAGE_SHIFT;
1530 break;
1531 default:
1532 return -EINVAL;
1533 }
1534 }
1535
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001536 /*
1537 * Set 'VM_NORESERVE' if we should not account for the
1538 * memory use of this mapping.
1539 */
1540 if (flags & MAP_NORESERVE) {
1541 /* We honor MAP_NORESERVE if allowed to overcommit */
1542 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1543 vm_flags |= VM_NORESERVE;
1544
1545 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1546 if (file && is_file_hugepages(file))
1547 vm_flags |= VM_NORESERVE;
1548 }
1549
1550 addr = mmap_region(file, addr, len, vm_flags, pgoff);
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -07001551 if (!IS_ERR_VALUE(addr) &&
1552 ((vm_flags & VM_LOCKED) ||
1553 (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
Michel Lespinasse41badc12013-02-22 16:32:47 -08001554 *populate = len;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001555 return addr;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001556}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001557
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001558SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1559 unsigned long, prot, unsigned long, flags,
1560 unsigned long, fd, unsigned long, pgoff)
1561{
1562 struct file *file = NULL;
Chen Gang1e3ee142015-11-05 18:48:35 -08001563 unsigned long retval;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001564
1565 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001566 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001567 file = fget(fd);
1568 if (!file)
Chen Gang1e3ee142015-11-05 18:48:35 -08001569 return -EBADF;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001570 if (is_file_hugepages(file))
1571 len = ALIGN(len, huge_page_size(hstate_file(file)));
Jörn Engel493af572013-07-08 16:00:26 -07001572 retval = -EINVAL;
1573 if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
1574 goto out_fput;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001575 } else if (flags & MAP_HUGETLB) {
1576 struct user_struct *user = NULL;
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001577 struct hstate *hs;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001578
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001579 hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & SHM_HUGE_MASK);
Li Zefan091d0d52013-05-09 15:08:15 +08001580 if (!hs)
1581 return -EINVAL;
1582
1583 len = ALIGN(len, huge_page_size(hs));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001584 /*
1585 * VM_NORESERVE is used because the reservations will be
1586 * taken when vm_ops->mmap() is called
1587 * A dummy user value is used because we are not locking
1588 * memory so no accounting is necessary
1589 */
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001590 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
Andi Kleen42d73952012-12-11 16:01:34 -08001591 VM_NORESERVE,
1592 &user, HUGETLB_ANONHUGE_INODE,
1593 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001594 if (IS_ERR(file))
1595 return PTR_ERR(file);
1596 }
1597
1598 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1599
Michal Hocko9fbeb5a2016-05-23 16:25:30 -07001600 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Jörn Engel493af572013-07-08 16:00:26 -07001601out_fput:
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001602 if (file)
1603 fput(file);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001604 return retval;
1605}
1606
Christoph Hellwiga4679372010-03-10 15:21:15 -08001607#ifdef __ARCH_WANT_SYS_OLD_MMAP
1608struct mmap_arg_struct {
1609 unsigned long addr;
1610 unsigned long len;
1611 unsigned long prot;
1612 unsigned long flags;
1613 unsigned long fd;
1614 unsigned long offset;
1615};
1616
1617SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1618{
1619 struct mmap_arg_struct a;
1620
1621 if (copy_from_user(&a, arg, sizeof(a)))
1622 return -EFAULT;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001623 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001624 return -EINVAL;
1625
1626 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1627 a.offset >> PAGE_SHIFT);
1628}
1629#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1630
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001631/*
1632 * Some shared mappigns will want the pages marked read-only
1633 * to track write events. If so, we'll downgrade vm_page_prot
1634 * to the private version (using protection_map[] without the
1635 * VM_SHARED bit).
1636 */
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07001637int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001638{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001639 vm_flags_t vm_flags = vma->vm_flags;
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001640 const struct vm_operations_struct *vm_ops = vma->vm_ops;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001641
1642 /* If it was private or non-writable, the write bit is already clear */
1643 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1644 return 0;
1645
1646 /* The backer wishes to know when pages are first written to? */
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001647 if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001648 return 1;
1649
Peter Feiner64e45502014-10-13 15:55:46 -07001650 /* The open routine did something to the protections that pgprot_modify
1651 * won't preserve? */
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07001652 if (pgprot_val(vm_page_prot) !=
1653 pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001654 return 0;
1655
Peter Feiner64e45502014-10-13 15:55:46 -07001656 /* Do we need to track softdirty? */
1657 if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY))
1658 return 1;
1659
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001660 /* Specialty mapping? */
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001661 if (vm_flags & VM_PFNMAP)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001662 return 0;
1663
1664 /* Can the mapping track the dirty pages? */
1665 return vma->vm_file && vma->vm_file->f_mapping &&
1666 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1667}
1668
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001669/*
1670 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001671 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001672 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001673static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001674{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001675 /*
1676 * hugetlb has its own accounting separate from the core VM
1677 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1678 */
1679 if (file && is_file_hugepages(file))
1680 return 0;
1681
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001682 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1683}
1684
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001685unsigned long mmap_region(struct file *file, unsigned long addr,
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001686 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001687{
1688 struct mm_struct *mm = current->mm;
1689 struct vm_area_struct *vma, *prev;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001690 int error;
1691 struct rb_node **rb_link, *rb_parent;
1692 unsigned long charged = 0;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001693
Cyril Hrubise8420a82013-04-29 15:08:33 -07001694 /* Check against address space limit. */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001695 if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) {
Cyril Hrubise8420a82013-04-29 15:08:33 -07001696 unsigned long nr_pages;
1697
1698 /*
1699 * MAP_FIXED may remove pages of mappings that intersects with
1700 * requested mapping. Account for the pages it would unmap.
1701 */
Cyril Hrubise8420a82013-04-29 15:08:33 -07001702 nr_pages = count_vma_pages_range(mm, addr, addr + len);
1703
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001704 if (!may_expand_vm(mm, vm_flags,
1705 (len >> PAGE_SHIFT) - nr_pages))
Cyril Hrubise8420a82013-04-29 15:08:33 -07001706 return -ENOMEM;
1707 }
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /* Clear old maps */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07001710 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
1711 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (do_munmap(mm, addr, len))
1713 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001716 /*
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001717 * Private writable mapping: check memory availability
1718 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001719 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001720 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001721 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001722 return -ENOMEM;
1723 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
1726 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001727 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001729 vma = vma_merge(mm, prev, addr, addr + len, vm_flags,
Colin Cross3e4578f2015-10-27 16:42:08 -07001730 NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX, NULL);
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001731 if (vma)
1732 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /*
1735 * Determine the object being mapped and call the appropriate
1736 * specific mapper. the address has already been validated, but
1737 * not unmapped, but the maps are removed from the list.
1738 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001739 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (!vma) {
1741 error = -ENOMEM;
1742 goto unacct_error;
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 vma->vm_mm = mm;
1746 vma->vm_start = addr;
1747 vma->vm_end = addr + len;
1748 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001749 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 vma->vm_pgoff = pgoff;
Laurent Dufour6315ccc2018-04-17 16:33:13 +02001751 INIT_VMA(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (vm_flags & VM_DENYWRITE) {
1755 error = deny_write_access(file);
1756 if (error)
1757 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001759 if (vm_flags & VM_SHARED) {
1760 error = mapping_map_writable(file->f_mapping);
1761 if (error)
1762 goto allow_write_and_free_vma;
1763 }
1764
1765 /* ->mmap() can change vma->vm_file, but must guarantee that
1766 * vma_link() below can deny write-access if VM_DENYWRITE is set
1767 * and map writably if VM_SHARED is set. This usually means the
1768 * new file must not have been exposed to user-space, yet.
1769 */
Al Virocb0942b2012-08-27 14:48:26 -04001770 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 error = file->f_op->mmap(file, vma);
1772 if (error)
1773 goto unmap_and_free_vma;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001774
1775 /* Can addr have changed??
1776 *
1777 * Answer: Yes, several device drivers can do it in their
1778 * f_op->mmap method. -DaveM
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001779 * Bug: If addr is changed, prev, rb_link, rb_parent should
1780 * be updated for vma_link()
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001781 */
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001782 WARN_ON_ONCE(addr != vma->vm_start);
1783
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001784 addr = vma->vm_start;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001785 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 } else if (vm_flags & VM_SHARED) {
1787 error = shmem_zero_setup(vma);
1788 if (error)
1789 goto free_vma;
1790 }
1791
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001792 vma_link(mm, vma, prev, rb_link, rb_parent);
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001793 /* Once vma denies write, undo our temporary denial count */
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001794 if (file) {
1795 if (vm_flags & VM_SHARED)
1796 mapping_unmap_writable(file->f_mapping);
1797 if (vm_flags & VM_DENYWRITE)
1798 allow_write_access(file);
1799 }
Oleg Nesterove8686772013-09-11 14:20:20 -07001800 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001801out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001802 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001803
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001804 vm_write_begin(vma);
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001805 vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (vm_flags & VM_LOCKED) {
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001807 if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
1808 vma == get_gate_vma(current->mm)))
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001809 mm->locked_vm += (len >> PAGE_SHIFT);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001810 else
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001811 WRITE_ONCE(vma->vm_flags,
1812 vma->vm_flags & VM_LOCKED_CLEAR_MASK);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001813 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301814
Oleg Nesterovc7a3a882012-08-19 19:10:42 +02001815 if (file)
1816 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301817
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001818 /*
1819 * New (or expanded) vma always get soft dirty status.
1820 * Otherwise user-space soft-dirty page tracker won't
1821 * be able to distinguish situation when vma area unmapped,
1822 * then new mapped in-place (which must be aimed as
1823 * a completely new data area).
1824 */
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001825 WRITE_ONCE(vma->vm_flags, vma->vm_flags | VM_SOFTDIRTY);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001826
Peter Feiner64e45502014-10-13 15:55:46 -07001827 vma_set_page_prot(vma);
Laurent Dufourdd2b4652018-04-17 16:33:15 +02001828 vm_write_end(vma);
Peter Feiner64e45502014-10-13 15:55:46 -07001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return addr;
1831
1832unmap_and_free_vma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 vma->vm_file = NULL;
1834 fput(file);
1835
1836 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001837 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1838 charged = 0;
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001839 if (vm_flags & VM_SHARED)
1840 mapping_unmap_writable(file->f_mapping);
1841allow_write_and_free_vma:
1842 if (vm_flags & VM_DENYWRITE)
1843 allow_write_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844free_vma:
1845 kmem_cache_free(vm_area_cachep, vma);
1846unacct_error:
1847 if (charged)
1848 vm_unacct_memory(charged);
1849 return error;
1850}
1851
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001852unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1853{
1854 /*
1855 * We implement the search by looking for an rbtree node that
1856 * immediately follows a suitable gap. That is,
1857 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1858 * - gap_end = vma->vm_start >= info->low_limit + length;
1859 * - gap_end - gap_start >= length
1860 */
1861
1862 struct mm_struct *mm = current->mm;
1863 struct vm_area_struct *vma;
1864 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1865
1866 /* Adjust search length to account for worst case alignment overhead */
1867 length = info->length + info->align_mask;
1868 if (length < info->length)
1869 return -ENOMEM;
1870
1871 /* Adjust search limits by the desired length */
1872 if (info->high_limit < length)
1873 return -ENOMEM;
1874 high_limit = info->high_limit - length;
1875
1876 if (info->low_limit > high_limit)
1877 return -ENOMEM;
1878 low_limit = info->low_limit + length;
1879
1880 /* Check if rbtree root looks promising */
1881 if (RB_EMPTY_ROOT(&mm->mm_rb))
1882 goto check_highest;
1883 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1884 if (vma->rb_subtree_gap < length)
1885 goto check_highest;
1886
1887 while (true) {
1888 /* Visit left subtree if it looks promising */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001889 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001890 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1891 struct vm_area_struct *left =
1892 rb_entry(vma->vm_rb.rb_left,
1893 struct vm_area_struct, vm_rb);
1894 if (left->rb_subtree_gap >= length) {
1895 vma = left;
1896 continue;
1897 }
1898 }
1899
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001900 gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001901check_current:
1902 /* Check if current node has a suitable gap */
1903 if (gap_start > high_limit)
1904 return -ENOMEM;
Hugh Dickinsce7fe852017-06-20 02:10:44 -07001905 if (gap_end >= low_limit &&
1906 gap_end > gap_start && gap_end - gap_start >= length)
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001907 goto found;
1908
1909 /* Visit right subtree if it looks promising */
1910 if (vma->vm_rb.rb_right) {
1911 struct vm_area_struct *right =
1912 rb_entry(vma->vm_rb.rb_right,
1913 struct vm_area_struct, vm_rb);
1914 if (right->rb_subtree_gap >= length) {
1915 vma = right;
1916 continue;
1917 }
1918 }
1919
1920 /* Go back up the rbtree to find next candidate node */
1921 while (true) {
1922 struct rb_node *prev = &vma->vm_rb;
1923 if (!rb_parent(prev))
1924 goto check_highest;
1925 vma = rb_entry(rb_parent(prev),
1926 struct vm_area_struct, vm_rb);
1927 if (prev == vma->vm_rb.rb_left) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001928 gap_start = vm_end_gap(vma->vm_prev);
1929 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001930 goto check_current;
1931 }
1932 }
1933 }
1934
1935check_highest:
1936 /* Check highest gap, which does not precede any rbtree node */
1937 gap_start = mm->highest_vm_end;
1938 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1939 if (gap_start > high_limit)
1940 return -ENOMEM;
1941
1942found:
1943 /* We found a suitable gap. Clip it with the original low_limit. */
1944 if (gap_start < info->low_limit)
1945 gap_start = info->low_limit;
1946
1947 /* Adjust gap address to the desired alignment */
1948 gap_start += (info->align_offset - gap_start) & info->align_mask;
1949
1950 VM_BUG_ON(gap_start + info->length > info->high_limit);
1951 VM_BUG_ON(gap_start + info->length > gap_end);
1952 return gap_start;
1953}
1954
1955unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1956{
1957 struct mm_struct *mm = current->mm;
1958 struct vm_area_struct *vma;
1959 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1960
1961 /* Adjust search length to account for worst case alignment overhead */
1962 length = info->length + info->align_mask;
1963 if (length < info->length)
1964 return -ENOMEM;
1965
1966 /*
1967 * Adjust search limits by the desired length.
1968 * See implementation comment at top of unmapped_area().
1969 */
1970 gap_end = info->high_limit;
1971 if (gap_end < length)
1972 return -ENOMEM;
1973 high_limit = gap_end - length;
1974
1975 if (info->low_limit > high_limit)
1976 return -ENOMEM;
1977 low_limit = info->low_limit + length;
1978
1979 /* Check highest gap, which does not precede any rbtree node */
1980 gap_start = mm->highest_vm_end;
1981 if (gap_start <= high_limit)
1982 goto found_highest;
1983
1984 /* Check if rbtree root looks promising */
1985 if (RB_EMPTY_ROOT(&mm->mm_rb))
1986 return -ENOMEM;
1987 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1988 if (vma->rb_subtree_gap < length)
1989 return -ENOMEM;
1990
1991 while (true) {
1992 /* Visit right subtree if it looks promising */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07001993 gap_start = vma->vm_prev ? vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001994 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
1995 struct vm_area_struct *right =
1996 rb_entry(vma->vm_rb.rb_right,
1997 struct vm_area_struct, vm_rb);
1998 if (right->rb_subtree_gap >= length) {
1999 vma = right;
2000 continue;
2001 }
2002 }
2003
2004check_current:
2005 /* Check if current node has a suitable gap */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002006 gap_end = vm_start_gap(vma);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002007 if (gap_end < low_limit)
2008 return -ENOMEM;
Hugh Dickinsce7fe852017-06-20 02:10:44 -07002009 if (gap_start <= high_limit &&
2010 gap_end > gap_start && gap_end - gap_start >= length)
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002011 goto found;
2012
2013 /* Visit left subtree if it looks promising */
2014 if (vma->vm_rb.rb_left) {
2015 struct vm_area_struct *left =
2016 rb_entry(vma->vm_rb.rb_left,
2017 struct vm_area_struct, vm_rb);
2018 if (left->rb_subtree_gap >= length) {
2019 vma = left;
2020 continue;
2021 }
2022 }
2023
2024 /* Go back up the rbtree to find next candidate node */
2025 while (true) {
2026 struct rb_node *prev = &vma->vm_rb;
2027 if (!rb_parent(prev))
2028 return -ENOMEM;
2029 vma = rb_entry(rb_parent(prev),
2030 struct vm_area_struct, vm_rb);
2031 if (prev == vma->vm_rb.rb_right) {
2032 gap_start = vma->vm_prev ?
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002033 vm_end_gap(vma->vm_prev) : 0;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002034 goto check_current;
2035 }
2036 }
2037 }
2038
2039found:
2040 /* We found a suitable gap. Clip it with the original high_limit. */
2041 if (gap_end > info->high_limit)
2042 gap_end = info->high_limit;
2043
2044found_highest:
2045 /* Compute highest gap address at the desired alignment */
2046 gap_end -= info->length;
2047 gap_end -= (gap_end - info->align_offset) & info->align_mask;
2048
2049 VM_BUG_ON(gap_end < info->low_limit);
2050 VM_BUG_ON(gap_end < gap_start);
2051 return gap_end;
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054/* Get an address range which is currently unmapped.
2055 * For shmat() with addr=0.
2056 *
2057 * Ugly calling convention alert:
2058 * Return value with the low bits set means error value,
2059 * ie
2060 * if (ret & ~PAGE_MASK)
2061 * error = ret;
2062 *
2063 * This function "knows" that -ENOMEM has the bits set.
2064 */
2065#ifndef HAVE_ARCH_UNMAPPED_AREA
2066unsigned long
2067arch_get_unmapped_area(struct file *filp, unsigned long addr,
2068 unsigned long len, unsigned long pgoff, unsigned long flags)
2069{
2070 struct mm_struct *mm = current->mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002071 struct vm_area_struct *vma, *prev;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002072 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002074 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return -ENOMEM;
2076
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002077 if (flags & MAP_FIXED)
2078 return addr;
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (addr) {
2081 addr = PAGE_ALIGN(addr);
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002082 vma = find_vma_prev(mm, addr, &prev);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002083 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002084 (!vma || addr + len <= vm_start_gap(vma)) &&
2085 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return addr;
2087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002089 info.flags = 0;
2090 info.length = len;
Heiko Carstens4e99b022013-11-12 15:07:54 -08002091 info.low_limit = mm->mmap_base;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002092 info.high_limit = TASK_SIZE;
2093 info.align_mask = 0;
2094 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
vishnu.pscc71aba2014-10-09 15:26:29 -07002096#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098/*
2099 * This mmap-allocator allocates new areas top-down from below the
2100 * stack's low limit (the base):
2101 */
2102#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
2103unsigned long
2104arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
2105 const unsigned long len, const unsigned long pgoff,
2106 const unsigned long flags)
2107{
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002108 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct mm_struct *mm = current->mm;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002110 unsigned long addr = addr0;
2111 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 /* requested length too big for entire address space */
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002114 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return -ENOMEM;
2116
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002117 if (flags & MAP_FIXED)
2118 return addr;
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 /* requesting a specific address */
2121 if (addr) {
2122 addr = PAGE_ALIGN(addr);
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002123 vma = find_vma_prev(mm, addr, &prev);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002124 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002125 (!vma || addr + len <= vm_start_gap(vma)) &&
2126 (!prev || addr >= vm_end_gap(prev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return addr;
2128 }
2129
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002130 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
2131 info.length = len;
Akira Takeuchi2afc7452013-11-12 15:08:21 -08002132 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002133 info.high_limit = mm->mmap_base;
2134 info.align_mask = 0;
2135 addr = vm_unmapped_area(&info);
Xiao Guangrongb716ad92012-03-21 16:33:56 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /*
2138 * A failed mmap() very likely causes application failure,
2139 * so fall back to the bottom-up function here. This scenario
2140 * can happen with large stack limits and large mmap()
2141 * allocations.
2142 */
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002143 if (offset_in_page(addr)) {
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08002144 VM_BUG_ON(addr != -ENOMEM);
2145 info.flags = 0;
2146 info.low_limit = TASK_UNMAPPED_BASE;
2147 info.high_limit = TASK_SIZE;
2148 addr = vm_unmapped_area(&info);
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 return addr;
2152}
2153#endif
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155unsigned long
2156get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
2157 unsigned long pgoff, unsigned long flags)
2158{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002159 unsigned long (*get_area)(struct file *, unsigned long,
2160 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Al Viro9206de92009-12-03 15:23:11 -05002162 unsigned long error = arch_mmap_check(addr, len, flags);
2163 if (error)
2164 return error;
2165
2166 /* Careful about overflows.. */
2167 if (len > TASK_SIZE)
2168 return -ENOMEM;
2169
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002170 get_area = current->mm->get_unmapped_area;
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002171 if (file) {
2172 if (file->f_op->get_unmapped_area)
2173 get_area = file->f_op->get_unmapped_area;
2174 } else if (flags & MAP_SHARED) {
2175 /*
2176 * mmap_region() will call shmem_zero_setup() to create a file,
2177 * so use shmem's get_unmapped_area in case it can be huge.
2178 * do_mmap_pgoff() will clear pgoff, so match alignment.
2179 */
2180 pgoff = 0;
2181 get_area = shmem_get_unmapped_area;
2182 }
2183
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002184 addr = get_area(file, addr, len, pgoff, flags);
2185 if (IS_ERR_VALUE(addr))
2186 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Linus Torvalds07ab67c2005-05-19 22:43:37 -07002188 if (addr > TASK_SIZE - len)
2189 return -ENOMEM;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002190 if (offset_in_page(addr))
Linus Torvalds07ab67c2005-05-19 22:43:37 -07002191 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07002192
Al Viro9ac4ed42012-05-30 17:13:15 -04002193 error = security_mmap_addr(addr);
2194 return error ? error : addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197EXPORT_SYMBOL(get_unmapped_area);
2198
2199/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
Laurent Dufourd8173842018-04-17 16:33:23 +02002200static struct vm_area_struct *__find_vma(struct mm_struct *mm,
2201 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002203 struct rb_node *rb_node;
Laurent Dufourd8173842018-04-17 16:33:23 +02002204 struct vm_area_struct *vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002206 rb_node = mm->mm_rb.rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002208 while (rb_node) {
2209 struct vm_area_struct *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002211 tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
Rajman Mekaco841e31e2012-05-29 15:06:21 -07002212
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002213 if (tmp->vm_end > addr) {
2214 vma = tmp;
2215 if (tmp->vm_start <= addr)
2216 break;
2217 rb_node = rb_node->rb_left;
2218 } else
2219 rb_node = rb_node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002221
Laurent Dufourd8173842018-04-17 16:33:23 +02002222 return vma;
2223}
2224
2225struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
2226{
2227 struct vm_area_struct *vma;
2228
2229 /* Check the cache first. */
2230 vma = vmacache_find(mm, addr);
2231 if (likely(vma))
2232 return vma;
2233
2234 vma = __find_vma(mm, addr);
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002235 if (vma)
2236 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return vma;
2238}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239EXPORT_SYMBOL(find_vma);
2240
Laurent Dufourd8173842018-04-17 16:33:23 +02002241#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
2242struct vm_area_struct *get_vma(struct mm_struct *mm, unsigned long addr)
2243{
2244 struct vm_area_struct *vma = NULL;
2245
2246 read_lock(&mm->mm_rb_lock);
2247 vma = __find_vma(mm, addr);
2248 if (vma)
2249 atomic_inc(&vma->vm_ref_count);
2250 read_unlock(&mm->mm_rb_lock);
2251
2252 return vma;
2253}
2254#endif
2255
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002256/*
2257 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259struct vm_area_struct *
2260find_vma_prev(struct mm_struct *mm, unsigned long addr,
2261 struct vm_area_struct **pprev)
2262{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002263 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002265 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05002266 if (vma) {
2267 *pprev = vma->vm_prev;
2268 } else {
2269 struct rb_node *rb_node = mm->mm_rb.rb_node;
2270 *pprev = NULL;
2271 while (rb_node) {
2272 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
2273 rb_node = rb_node->rb_right;
2274 }
2275 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08002276 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
2279/*
2280 * Verify that the stack growth is acceptable and
2281 * update accounting. This is shared with both the
2282 * grow-up and grow-down cases.
2283 */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002284static int acct_stack_growth(struct vm_area_struct *vma,
2285 unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 struct mm_struct *mm = vma->vm_mm;
2288 struct rlimit *rlim = current->signal->rlim;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002289 unsigned long new_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 /* address space limit tests */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002292 if (!may_expand_vm(mm, vma->vm_flags, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return -ENOMEM;
2294
2295 /* Stack limit test */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002296 if (size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return -ENOMEM;
2298
2299 /* mlock limit tests */
2300 if (vma->vm_flags & VM_LOCKED) {
2301 unsigned long locked;
2302 unsigned long limit;
2303 locked = mm->locked_vm + grow;
Jason Low4db0c3c2015-04-15 16:14:08 -07002304 limit = READ_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
Jiri Slaby59e99e52010-03-05 13:41:44 -08002305 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (locked > limit && !capable(CAP_IPC_LOCK))
2307 return -ENOMEM;
2308 }
2309
Adam Litke0d59a012007-01-30 14:35:39 -08002310 /* Check to ensure the stack will not grow into a hugetlb-only region */
2311 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
2312 vma->vm_end - size;
2313 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
2314 return -EFAULT;
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /*
2317 * Overcommit.. This must be the final test, as it will
2318 * update security statistics.
2319 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01002320 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return -ENOMEM;
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return 0;
2324}
2325
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002326#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002328 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2329 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002331int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Oleg Nesterov09357812015-11-05 18:48:17 -08002333 struct mm_struct *mm = vma->vm_mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002334 struct vm_area_struct *next;
2335 unsigned long gap_addr;
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002336 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 if (!(vma->vm_flags & VM_GROWSUP))
2339 return -EFAULT;
2340
Helge Deller5d10ad622017-06-19 17:34:05 +02002341 /* Guard against exceeding limits of the address space. */
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002342 address &= PAGE_MASK;
Helge Deller38dfd2e2017-07-14 14:49:38 -07002343 if (address >= (TASK_SIZE & PAGE_MASK))
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002344 return -ENOMEM;
Helge Deller5d10ad622017-06-19 17:34:05 +02002345 address += PAGE_SIZE;
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002346
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002347 /* Enforce stack_guard_gap */
2348 gap_addr = address + stack_guard_gap;
Helge Deller5d10ad622017-06-19 17:34:05 +02002349
2350 /* Guard against overflow */
2351 if (gap_addr < address || gap_addr > TASK_SIZE)
2352 gap_addr = TASK_SIZE;
2353
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002354 next = vma->vm_next;
Michal Hockoc57664b2017-07-10 15:49:51 -07002355 if (next && next->vm_start < gap_addr &&
2356 (next->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002357 if (!(next->vm_flags & VM_GROWSUP))
2358 return -ENOMEM;
2359 /* Check that both stack segments have the same anon_vma? */
2360 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002361
2362 /* We must make sure the anon_vma is allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (unlikely(anon_vma_prepare(vma)))
2364 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 /*
2367 * vma->vm_start/vm_end cannot change under us because the caller
2368 * is required to hold the mmap_sem in read mode. We need the
2369 * anon_vma lock to serialize against concurrent expand_stacks.
2370 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002371 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 /* Somebody else might have raced and expanded it already */
2374 if (address > vma->vm_end) {
2375 unsigned long size, grow;
2376
2377 size = address - vma->vm_start;
2378 grow = (address - vma->vm_end) >> PAGE_SHIFT;
2379
Hugh Dickins42c36f62011-05-09 17:44:42 -07002380 error = -ENOMEM;
2381 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
2382 error = acct_stack_growth(vma, size, grow);
2383 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002384 /*
2385 * vma_gap_update() doesn't support concurrent
2386 * updates, but we only hold a shared mmap_sem
2387 * lock here, so we need to protect against
2388 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002389 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002390 * we don't guarantee that all growable vmas
2391 * in a mm share the same root anon vma.
2392 * So, we reuse mm->page_table_lock to guard
2393 * against concurrent vma expansions.
2394 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002395 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002396 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002397 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002398 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002399 anon_vma_interval_tree_pre_update_vma(vma);
Hugh Dickins42c36f62011-05-09 17:44:42 -07002400 vma->vm_end = address;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002401 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002402 if (vma->vm_next)
2403 vma_gap_update(vma->vm_next);
2404 else
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002405 mm->highest_vm_end = vm_end_gap(vma);
Oleg Nesterov09357812015-11-05 18:48:17 -08002406 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002407
Hugh Dickins42c36f62011-05-09 17:44:42 -07002408 perf_event_mmap(vma);
2409 }
Eric B Munson3af9e852010-05-18 15:30:49 +01002410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002412 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002413 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002414 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return error;
2416}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002417#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419/*
2420 * vma is the first one with address < vma->vm_start. Have to extend vma.
2421 */
Michal Hockod05f3162011-05-24 17:11:44 -07002422int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002423 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Oleg Nesterov09357812015-11-05 18:48:17 -08002425 struct mm_struct *mm = vma->vm_mm;
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002426 struct vm_area_struct *prev;
2427 unsigned long gap_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 int error;
2429
Eric Paris88694772007-11-26 18:47:26 -05002430 address &= PAGE_MASK;
Al Viroe5467852012-05-30 13:30:51 -04002431 error = security_mmap_addr(address);
Eric Paris88694772007-11-26 18:47:26 -05002432 if (error)
2433 return error;
2434
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002435 /* Enforce stack_guard_gap */
2436 gap_addr = address - stack_guard_gap;
2437 if (gap_addr > address)
2438 return -ENOMEM;
2439 prev = vma->vm_prev;
Michal Hockoc57664b2017-07-10 15:49:51 -07002440 if (prev && prev->vm_end > gap_addr &&
2441 (prev->vm_flags & (VM_WRITE|VM_READ|VM_EXEC))) {
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002442 if (!(prev->vm_flags & VM_GROWSDOWN))
2443 return -ENOMEM;
2444 /* Check that both stack segments have the same anon_vma? */
2445 }
2446
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002447 /* We must make sure the anon_vma is allocated. */
2448 if (unlikely(anon_vma_prepare(vma)))
2449 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /*
2452 * vma->vm_start/vm_end cannot change under us because the caller
2453 * is required to hold the mmap_sem in read mode. We need the
2454 * anon_vma lock to serialize against concurrent expand_stacks.
2455 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002456 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 /* Somebody else might have raced and expanded it already */
2459 if (address < vma->vm_start) {
2460 unsigned long size, grow;
2461
2462 size = vma->vm_end - address;
2463 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2464
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002465 error = -ENOMEM;
2466 if (grow <= vma->vm_pgoff) {
2467 error = acct_stack_growth(vma, size, grow);
2468 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002469 /*
2470 * vma_gap_update() doesn't support concurrent
2471 * updates, but we only hold a shared mmap_sem
2472 * lock here, so we need to protect against
2473 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002474 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002475 * we don't guarantee that all growable vmas
2476 * in a mm share the same root anon vma.
2477 * So, we reuse mm->page_table_lock to guard
2478 * against concurrent vma expansions.
2479 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002480 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002481 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002482 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002483 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002484 anon_vma_interval_tree_pre_update_vma(vma);
Laurent Dufourdd2b4652018-04-17 16:33:15 +02002485 WRITE_ONCE(vma->vm_start, address);
2486 WRITE_ONCE(vma->vm_pgoff, vma->vm_pgoff - grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002487 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002488 vma_gap_update(vma);
Oleg Nesterov09357812015-11-05 18:48:17 -08002489 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002490
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002491 perf_event_mmap(vma);
2492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002495 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002496 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002497 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return error;
2499}
2500
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002501/* enforced gap between the expanding stack and other mappings. */
2502unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
2503
2504static int __init cmdline_parse_stack_guard_gap(char *p)
2505{
2506 unsigned long val;
2507 char *endptr;
2508
2509 val = simple_strtoul(p, &endptr, 10);
2510 if (!*endptr)
2511 stack_guard_gap = val << PAGE_SHIFT;
2512
2513 return 0;
2514}
2515__setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
2516
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002517#ifdef CONFIG_STACK_GROWSUP
2518int expand_stack(struct vm_area_struct *vma, unsigned long address)
2519{
2520 return expand_upwards(vma, address);
2521}
2522
2523struct vm_area_struct *
2524find_extend_vma(struct mm_struct *mm, unsigned long addr)
2525{
2526 struct vm_area_struct *vma, *prev;
2527
2528 addr &= PAGE_MASK;
2529 vma = find_vma_prev(mm, addr, &prev);
2530 if (vma && (vma->vm_start <= addr))
2531 return vma;
Denys Vlasenko1c127182008-11-12 01:24:41 +01002532 if (!prev || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002533 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002534 if (prev->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002535 populate_vma_page_range(prev, addr, prev->vm_end, NULL);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002536 return prev;
2537}
2538#else
2539int expand_stack(struct vm_area_struct *vma, unsigned long address)
2540{
2541 return expand_downwards(vma, address);
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544struct vm_area_struct *
vishnu.pscc71aba2014-10-09 15:26:29 -07002545find_extend_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
vishnu.pscc71aba2014-10-09 15:26:29 -07002547 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 unsigned long start;
2549
2550 addr &= PAGE_MASK;
vishnu.pscc71aba2014-10-09 15:26:29 -07002551 vma = find_vma(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (!vma)
2553 return NULL;
2554 if (vma->vm_start <= addr)
2555 return vma;
2556 if (!(vma->vm_flags & VM_GROWSDOWN))
2557 return NULL;
2558 start = vma->vm_start;
2559 if (expand_stack(vma, addr))
2560 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002561 if (vma->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002562 populate_vma_page_range(vma, addr, start, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return vma;
2564}
2565#endif
2566
Jesse Barnese1d6d012014-12-12 16:55:27 -08002567EXPORT_SYMBOL_GPL(find_extend_vma);
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002570 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002572 *
2573 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002575static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002577 unsigned long nr_accounted = 0;
2578
Hugh Dickins365e9c872005-10-29 18:16:18 -07002579 /* Update high watermark before we lower total_vm */
2580 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002582 long nrpages = vma_pages(vma);
2583
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002584 if (vma->vm_flags & VM_ACCOUNT)
2585 nr_accounted += nrpages;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002586 vm_stat_account(mm, vma->vm_flags, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002587 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002588 } while (vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002589 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 validate_mm(mm);
2591}
2592
2593/*
2594 * Get rid of page table information in the indicated region.
2595 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002596 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 */
2598static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002599 struct vm_area_struct *vma, struct vm_area_struct *prev,
2600 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
vishnu.pscc71aba2014-10-09 15:26:29 -07002602 struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002603 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07002606 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002607 update_hiwater_rss(mm);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002608 unmap_vmas(&tlb, vma, start, end);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002609 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins6ee86302013-04-29 15:07:44 -07002610 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002611 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
2614/*
2615 * Create a list of vma's touched by the unmap, removing them from the mm's
2616 * vma list as we go..
2617 */
2618static void
2619detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2620 struct vm_area_struct *prev, unsigned long end)
2621{
2622 struct vm_area_struct **insertion_point;
2623 struct vm_area_struct *tail_vma = NULL;
2624
2625 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002626 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 do {
Laurent Dufourd8173842018-04-17 16:33:23 +02002628 vma_rb_erase(vma, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 mm->map_count--;
2630 tail_vma = vma;
2631 vma = vma->vm_next;
2632 } while (vma && vma->vm_start < end);
2633 *insertion_point = vma;
Michel Lespinassed3737182012-12-11 16:01:38 -08002634 if (vma) {
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002635 vma->vm_prev = prev;
Michel Lespinassed3737182012-12-11 16:01:38 -08002636 vma_gap_update(vma);
2637 } else
Hugh Dickinscfc0eb402017-06-19 04:03:24 -07002638 mm->highest_vm_end = prev ? vm_end_gap(prev) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 tail_vma->vm_next = NULL;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002640
2641 /* Kill the cache */
2642 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
2644
2645/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002646 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2647 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 */
vishnu.pscc71aba2014-10-09 15:26:29 -07002649static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 unsigned long addr, int new_below)
2651{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct vm_area_struct *new;
Chen Gange3975892015-09-08 15:03:38 -07002653 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Dan Williamscebe1392017-11-29 16:10:28 -08002655 if (vma->vm_ops && vma->vm_ops->split) {
2656 err = vma->vm_ops->split(vma, addr);
2657 if (err)
2658 return err;
2659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Christoph Lametere94b1762006-12-06 20:33:17 -08002661 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (!new)
Chen Gange3975892015-09-08 15:03:38 -07002663 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 /* most fields are the same, copy all, and then fixup */
2666 *new = *vma;
2667
Laurent Dufour6315ccc2018-04-17 16:33:13 +02002668 INIT_VMA(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (new_below)
2671 new->vm_end = addr;
2672 else {
2673 new->vm_start = addr;
2674 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2675 }
2676
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002677 err = vma_dup_policy(vma, new);
2678 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002679 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Daniel Forrestc4ea95d2014-12-02 15:59:42 -08002681 err = anon_vma_clone(new, vma);
2682 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002683 goto out_free_mpol;
2684
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002685 if (new->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 get_file(new->vm_file);
2687
2688 if (new->vm_ops && new->vm_ops->open)
2689 new->vm_ops->open(new);
2690
2691 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002692 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2694 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002695 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Rik van Riel5beb4932010-03-05 13:42:07 -08002697 /* Success. */
2698 if (!err)
2699 return 0;
2700
2701 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002702 if (new->vm_ops && new->vm_ops->close)
2703 new->vm_ops->close(new);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002704 if (new->vm_file)
Rik van Riel5beb4932010-03-05 13:42:07 -08002705 fput(new->vm_file);
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002706 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002707 out_free_mpol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002708 mpol_put(vma_policy(new));
Rik van Riel5beb4932010-03-05 13:42:07 -08002709 out_free_vma:
2710 kmem_cache_free(vm_area_cachep, new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002711 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002714/*
2715 * Split a vma into two pieces at address 'addr', a new vma is allocated
2716 * either for the first part or the tail.
2717 */
2718int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2719 unsigned long addr, int new_below)
2720{
2721 if (mm->map_count >= sysctl_max_map_count)
2722 return -ENOMEM;
2723
2724 return __split_vma(mm, vma, addr, new_below);
2725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727/* Munmap is split into 2 main parts -- this part which finds
2728 * what needs doing, and the areas themselves, which do the
2729 * work. This now handles partial unmappings.
2730 * Jeremy Fitzhardinge <jeremy@goop.org>
2731 */
2732int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2733{
2734 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002735 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002737 if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return -EINVAL;
2739
vishnu.pscc71aba2014-10-09 15:26:29 -07002740 len = PAGE_ALIGN(len);
2741 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return -EINVAL;
2743
2744 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002745 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002746 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002748 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002749 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 /* if it doesn't overlap, we have nothing.. */
2752 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002753 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return 0;
2755
2756 /*
2757 * If we need to split any vma, do it now to save pain later.
2758 *
2759 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2760 * unmapped vm_area_struct will remain in use: so lower split_vma
2761 * places tmp vma above, and higher split_vma places tmp vma below.
2762 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002763 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002764 int error;
2765
2766 /*
2767 * Make sure that map_count on return from munmap() will
2768 * not exceed its limit; but let map_count go just above
2769 * its limit temporarily, to help free resources as expected.
2770 */
2771 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2772 return -ENOMEM;
2773
2774 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (error)
2776 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002777 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
2779
2780 /* Does it split the last one? */
2781 last = find_vma(mm, end);
2782 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002783 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (error)
2785 return error;
2786 }
vishnu.pscc71aba2014-10-09 15:26:29 -07002787 vma = prev ? prev->vm_next : mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002790 * unlock any mlock()ed ranges before detaching vmas
2791 */
2792 if (mm->locked_vm) {
2793 struct vm_area_struct *tmp = vma;
2794 while (tmp && tmp->vm_start < end) {
2795 if (tmp->vm_flags & VM_LOCKED) {
2796 mm->locked_vm -= vma_pages(tmp);
2797 munlock_vma_pages_all(tmp);
2798 }
2799 tmp = tmp->vm_next;
2800 }
2801 }
2802
2803 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 * Remove the vma's, and unmap the actual pages
2805 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002806 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2807 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Dave Hansen1de4fa12014-11-14 07:18:31 -08002809 arch_unmap(mm, vma, start, end);
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002812 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 return 0;
2815}
Guenter Roeckabfb4862016-03-24 10:39:14 -07002816EXPORT_SYMBOL(do_munmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Al Virobfce2812012-04-20 21:57:04 -04002818int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002819{
2820 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002821 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002822
Michal Hockoae798782016-05-23 16:25:33 -07002823 if (down_write_killable(&mm->mmap_sem))
2824 return -EINTR;
2825
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002826 ret = do_munmap(mm, start, len);
2827 up_write(&mm->mmap_sem);
2828 return ret;
2829}
2830EXPORT_SYMBOL(vm_munmap);
2831
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002832SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002834 int ret;
2835 struct mm_struct *mm = current->mm;
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 profile_munmap(addr);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002838 if (down_write_killable(&mm->mmap_sem))
2839 return -EINTR;
2840 ret = do_munmap(mm, addr, len);
2841 up_write(&mm->mmap_sem);
2842 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002845
2846/*
2847 * Emulation of deprecated remap_file_pages() syscall.
2848 */
2849SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
2850 unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
2851{
2852
2853 struct mm_struct *mm = current->mm;
2854 struct vm_area_struct *vma;
2855 unsigned long populate = 0;
2856 unsigned long ret = -EINVAL;
2857 struct file *file;
2858
Joe Perches756a025f02016-03-17 14:19:47 -07002859 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
2860 current->comm, current->pid);
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002861
2862 if (prot)
2863 return ret;
2864 start = start & PAGE_MASK;
2865 size = size & PAGE_MASK;
2866
2867 if (start + size <= start)
2868 return ret;
2869
2870 /* Does pgoff wrap? */
2871 if (pgoff + (size >> PAGE_SHIFT) < pgoff)
2872 return ret;
2873
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002874 if (down_write_killable(&mm->mmap_sem))
2875 return -EINTR;
2876
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002877 vma = find_vma(mm, start);
2878
2879 if (!vma || !(vma->vm_flags & VM_SHARED))
2880 goto out;
2881
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002882 if (start < vma->vm_start)
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002883 goto out;
2884
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002885 if (start + size > vma->vm_end) {
2886 struct vm_area_struct *next;
2887
2888 for (next = vma->vm_next; next; next = next->vm_next) {
2889 /* hole between vmas ? */
2890 if (next->vm_start != next->vm_prev->vm_end)
2891 goto out;
2892
2893 if (next->vm_file != vma->vm_file)
2894 goto out;
2895
2896 if (next->vm_flags != vma->vm_flags)
2897 goto out;
2898
2899 if (start + size <= next->vm_end)
2900 break;
2901 }
2902
2903 if (!next)
2904 goto out;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002905 }
2906
2907 prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
2908 prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
2909 prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
2910
2911 flags &= MAP_NONBLOCK;
2912 flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
2913 if (vma->vm_flags & VM_LOCKED) {
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002914 struct vm_area_struct *tmp;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002915 flags |= MAP_LOCKED;
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002916
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002917 /* drop PG_Mlocked flag for over-mapped range */
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002918 for (tmp = vma; tmp->vm_start >= start + size;
2919 tmp = tmp->vm_next) {
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07002920 /*
2921 * Split pmd and munlock page on the border
2922 * of the range.
2923 */
2924 vma_adjust_trans_huge(tmp, start, start + size, 0);
2925
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002926 munlock_vma_pages_range(tmp,
2927 max(tmp->vm_start, start),
2928 min(tmp->vm_end, start + size));
2929 }
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002930 }
2931
2932 file = get_file(vma->vm_file);
2933 ret = do_mmap_pgoff(vma->vm_file, start, size,
2934 prot, flags, pgoff, &populate);
2935 fput(file);
2936out:
2937 up_write(&mm->mmap_sem);
2938 if (populate)
2939 mm_populate(ret, populate);
2940 if (!IS_ERR_VALUE(ret))
2941 ret = 0;
2942 return ret;
2943}
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945static inline void verify_mm_writelocked(struct mm_struct *mm)
2946{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002947#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2949 WARN_ON(1);
2950 up_read(&mm->mmap_sem);
2951 }
2952#endif
2953}
2954
2955/*
2956 * this is really a simplified "do_mmap". it only handles
2957 * anonymous maps. eventually we may be able to do some
2958 * brk-specific accounting here.
2959 */
Kees Cookba093a62016-08-02 14:04:54 -07002960static int do_brk(unsigned long addr, unsigned long request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
vishnu.pscc71aba2014-10-09 15:26:29 -07002962 struct mm_struct *mm = current->mm;
2963 struct vm_area_struct *vma, *prev;
Kees Cookba093a62016-08-02 14:04:54 -07002964 unsigned long flags, len;
vishnu.pscc71aba2014-10-09 15:26:29 -07002965 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002967 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Kees Cookba093a62016-08-02 14:04:54 -07002969 len = PAGE_ALIGN(request);
2970 if (len < request)
2971 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (!len)
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Kirill Korotaev3a459752006-09-07 14:17:04 +04002975 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2976
Al Viro2c6a1012009-12-03 19:40:46 -05002977 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002978 if (offset_in_page(error))
Kirill Korotaev3a459752006-09-07 14:17:04 +04002979 return error;
2980
Davidlohr Bueso363ee172014-01-21 15:49:15 -08002981 error = mlock_future_check(mm, mm->def_flags, len);
2982 if (error)
2983 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 /*
2986 * mm->mmap_sem is required to protect against another thread
2987 * changing the mappings in case we sleep.
2988 */
2989 verify_mm_writelocked(mm);
2990
2991 /*
2992 * Clear old maps. this also does some error checking for us
2993 */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07002994 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
2995 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (do_munmap(mm, addr, len))
2997 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
2999
3000 /* Check against address space limits *after* clearing old maps... */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003001 if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return -ENOMEM;
3003
3004 if (mm->map_count > sysctl_max_map_count)
3005 return -ENOMEM;
3006
Al Viro191c5422012-02-13 03:58:52 +00003007 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return -ENOMEM;
3009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07003011 vma = vma_merge(mm, prev, addr, addr + len, flags,
Colin Cross3e4578f2015-10-27 16:42:08 -07003012 NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX, NULL);
Rik van Rielba470de2008-10-18 20:26:50 -07003013 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 goto out;
3015
3016 /*
3017 * create a vma struct for an anonymous mapping
3018 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08003019 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 if (!vma) {
3021 vm_unacct_memory(len >> PAGE_SHIFT);
3022 return -ENOMEM;
3023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Laurent Dufour6315ccc2018-04-17 16:33:13 +02003025 INIT_VMA(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 vma->vm_mm = mm;
3027 vma->vm_start = addr;
3028 vma->vm_end = addr + len;
3029 vma->vm_pgoff = pgoff;
3030 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07003031 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 vma_link(mm, vma, prev, rb_link, rb_parent);
3033out:
Eric B Munson3af9e852010-05-18 15:30:49 +01003034 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 mm->total_vm += len >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003036 mm->data_vm += len >> PAGE_SHIFT;
Michel Lespinasse128557f2013-02-22 16:32:40 -08003037 if (flags & VM_LOCKED)
3038 mm->locked_vm += (len >> PAGE_SHIFT);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07003039 vma->vm_flags |= VM_SOFTDIRTY;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003043int vm_brk(unsigned long addr, unsigned long len)
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003044{
3045 struct mm_struct *mm = current->mm;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003046 int ret;
Michel Lespinasse128557f2013-02-22 16:32:40 -08003047 bool populate;
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003048
Michal Hocko2d6c9282016-05-23 16:25:42 -07003049 if (down_write_killable(&mm->mmap_sem))
3050 return -EINTR;
3051
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003052 ret = do_brk(addr, len);
Michel Lespinasse128557f2013-02-22 16:32:40 -08003053 populate = ((mm->def_flags & VM_LOCKED) != 0);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003054 up_write(&mm->mmap_sem);
Linus Torvalds5d22fc22016-05-27 15:57:31 -07003055 if (populate && !ret)
Michel Lespinasse128557f2013-02-22 16:32:40 -08003056 mm_populate(addr, len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07003057 return ret;
3058}
3059EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061/* Release all mmaps. */
3062void exit_mmap(struct mm_struct *mm)
3063{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07003064 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07003065 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 unsigned long nr_accounted = 0;
3067
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02003068 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07003069 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02003070
Rik van Rielba470de2008-10-18 20:26:50 -07003071 if (mm->locked_vm) {
3072 vma = mm->mmap;
3073 while (vma) {
3074 if (vma->vm_flags & VM_LOCKED)
3075 munlock_vma_pages_all(vma);
3076 vma = vma->vm_next;
3077 }
3078 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08003079
3080 arch_exit_mmap(mm);
3081
Rik van Rielba470de2008-10-18 20:26:50 -07003082 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08003083 if (!vma) /* Can happen if dup_mmap() received an OOM */
3084 return;
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 flush_cache_mm(mm);
Linus Torvalds2b047252013-08-15 11:42:25 -07003088 tlb_gather_mmu(&tlb, mm, 0, -1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08003089 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07003090 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003091 unmap_vmas(&tlb, vma, 0, -1);
Hugh Dickins9ba69292009-09-21 17:02:20 -07003092
Michal Hocko065e76d2017-12-14 15:33:15 -08003093 if (unlikely(mm_is_oom_victim(mm))) {
Andrea Arcangeli07b039e92017-09-06 16:25:00 -07003094 /*
3095 * Wait for oom_reap_task() to stop working on this
3096 * mm. Because MMF_OOM_SKIP is already set before
3097 * calling down_read(), oom_reap_task() will not run
3098 * on this "mm" post up_write().
3099 *
Michal Hocko065e76d2017-12-14 15:33:15 -08003100 * mm_is_oom_victim() cannot be set from under us
3101 * either because victim->mm is already set to NULL
Andrea Arcangeli07b039e92017-09-06 16:25:00 -07003102 * under task_lock before calling mmput and oom_mm is
Michal Hocko065e76d2017-12-14 15:33:15 -08003103 * set not NULL by the OOM killer only if victim->mm
Andrea Arcangeli07b039e92017-09-06 16:25:00 -07003104 * is found not NULL while holding the task_lock.
3105 */
Michal Hocko065e76d2017-12-14 15:33:15 -08003106 set_bit(MMF_OOM_SKIP, &mm->flags);
Andrea Arcangeli07b039e92017-09-06 16:25:00 -07003107 down_write(&mm->mmap_sem);
3108 up_write(&mm->mmap_sem);
3109 }
Hugh Dickins6ee86302013-04-29 15:07:44 -07003110 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05003111 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07003114 * Walk the list again, actually closing and freeing it,
3115 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003117 while (vma) {
3118 if (vma->vm_flags & VM_ACCOUNT)
3119 nr_accounted += vma_pages(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07003120 vma = remove_vma(vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07003121 }
3122 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
3125/* Insert vm structure into process list sorted by address
3126 * and into the inode's i_mmap tree. If vm_file is non-NULL
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003127 * then i_mmap_rwsem is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 */
Hugh Dickins6597d782012-10-08 16:29:07 -07003129int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Hugh Dickins6597d782012-10-08 16:29:07 -07003131 struct vm_area_struct *prev;
3132 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Chen Gangc9d13f52015-09-08 15:04:08 -07003134 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
3135 &prev, &rb_link, &rb_parent))
3136 return -ENOMEM;
3137 if ((vma->vm_flags & VM_ACCOUNT) &&
3138 security_vm_enough_memory_mm(mm, vma_pages(vma)))
3139 return -ENOMEM;
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 /*
3142 * The vm_pgoff of a purely anonymous vma should be irrelevant
3143 * until its first write fault, when page's anon_vma and index
3144 * are set. But now set the vm_pgoff it will almost certainly
3145 * end up with (unless mremap moves it elsewhere before that
3146 * first wfault), so /proc/pid/maps tells a consistent story.
3147 *
3148 * By setting it to reflect the virtual start address of the
3149 * vma, merges and splits can happen in a seamless way, just
3150 * using the existing file pgoff checks and manipulations.
3151 * Similarly in do_mmap_pgoff and in do_brk.
3152 */
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07003153 if (vma_is_anonymous(vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 BUG_ON(vma->anon_vma);
3155 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
3156 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05303157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 vma_link(mm, vma, prev, rb_link, rb_parent);
3159 return 0;
3160}
3161
3162/*
3163 * Copy the vma structure to a new location in the same mm,
3164 * prior to moving page table entries, to effect an mremap move.
3165 */
3166struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
Michel Lespinasse38a76012012-10-08 16:31:50 -07003167 unsigned long addr, unsigned long len, pgoff_t pgoff,
3168 bool *need_rmap_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 struct vm_area_struct *vma = *vmap;
3171 unsigned long vma_start = vma->vm_start;
3172 struct mm_struct *mm = vma->vm_mm;
3173 struct vm_area_struct *new_vma, *prev;
3174 struct rb_node **rb_link, *rb_parent;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003175 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 /*
3178 * If anonymous vma has not yet been faulted, update new pgoff
3179 * to match new location, to increase its chance of merging.
3180 */
Oleg Nesterovce757992015-09-08 14:58:34 -07003181 if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003183 faulted_in_anon_vma = false;
3184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Hugh Dickins6597d782012-10-08 16:29:07 -07003186 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
3187 return NULL; /* should never get here */
Laurent Dufour3545b7e2018-04-17 16:33:16 +02003188
3189 /* There is 3 cases to manage here in
3190 * AAAA AAAA AAAA AAAA
3191 * PPPP.... PPPP......NNNN PPPP....NNNN PP........NN
3192 * PPPPPPPP(A) PPPP..NNNNNNNN(B) PPPPPPPPPPPP(1) NULL
3193 * PPPPPPPPNNNN(2)
3194 * PPPPNNNNNNNN(3)
3195 *
3196 * new_vma == prev in case A,1,2
3197 * new_vma == next in case B,3
3198 */
3199 new_vma = __vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
3200 vma->anon_vma, vma->vm_file, pgoff,
3201 vma_policy(vma), vma->vm_userfaultfd_ctx,
3202 vma_get_anon_name(vma), true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (new_vma) {
3204 /*
3205 * Source vma may have been merged into new_vma
3206 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08003207 if (unlikely(vma_start >= new_vma->vm_start &&
3208 vma_start < new_vma->vm_end)) {
3209 /*
3210 * The only way we can get a vma_merge with
3211 * self during an mremap is if the vma hasn't
3212 * been faulted in yet and we were allowed to
3213 * reset the dst vma->vm_pgoff to the
3214 * destination address of the mremap to allow
3215 * the merge to happen. mremap must change the
3216 * vm_pgoff linearity between src and dst vmas
3217 * (in turn preventing a vma_merge) to be
3218 * safe. It is only safe to keep the vm_pgoff
3219 * linear if there are no pages mapped yet.
3220 */
Sasha Levin81d1b092014-10-09 15:28:10 -07003221 VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
Michel Lespinasse38a76012012-10-08 16:31:50 -07003222 *vmap = vma = new_vma;
Michel Lespinasse108d6642012-10-08 16:31:36 -07003223 }
Michel Lespinasse38a76012012-10-08 16:31:50 -07003224 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08003226 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Chen Gange3975892015-09-08 15:03:38 -07003227 if (!new_vma)
3228 goto out;
3229 *new_vma = *vma;
3230 new_vma->vm_start = addr;
3231 new_vma->vm_end = addr + len;
3232 new_vma->vm_pgoff = pgoff;
3233 if (vma_dup_policy(vma, new_vma))
3234 goto out_free_vma;
Laurent Dufour6315ccc2018-04-17 16:33:13 +02003235 INIT_VMA(new_vma);
Chen Gange3975892015-09-08 15:03:38 -07003236 if (anon_vma_clone(new_vma, vma))
3237 goto out_free_mempol;
3238 if (new_vma->vm_file)
3239 get_file(new_vma->vm_file);
3240 if (new_vma->vm_ops && new_vma->vm_ops->open)
3241 new_vma->vm_ops->open(new_vma);
Laurent Dufour3545b7e2018-04-17 16:33:16 +02003242 /*
3243 * As the VMA is linked right now, it may be hit by the
3244 * speculative page fault handler. But we don't want it to
3245 * to start mapping page in this area until the caller has
3246 * potentially move the pte from the moved VMA. To prevent
3247 * that we protect it right now, and let the caller unprotect
3248 * it once the move is done.
3249 */
3250 vm_raw_write_begin(new_vma);
Chen Gange3975892015-09-08 15:03:38 -07003251 vma_link(mm, new_vma, prev, rb_link, rb_parent);
3252 *need_rmap_locks = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
3254 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003255
Chen Gange3975892015-09-08 15:03:38 -07003256out_free_mempol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07003257 mpol_put(vma_policy(new_vma));
Chen Gange3975892015-09-08 15:03:38 -07003258out_free_vma:
Rik van Riel5beb4932010-03-05 13:42:07 -08003259 kmem_cache_free(vm_area_cachep, new_vma);
Chen Gange3975892015-09-08 15:03:38 -07003260out:
Rik van Riel5beb4932010-03-05 13:42:07 -08003261 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262}
akpm@osdl.org119f6572005-05-01 08:58:35 -07003263
3264/*
3265 * Return true if the calling process may expand its vm space by the passed
3266 * number of pages
3267 */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003268bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
akpm@osdl.org119f6572005-05-01 08:58:35 -07003269{
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003270 if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
3271 return false;
akpm@osdl.org119f6572005-05-01 08:58:35 -07003272
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003273 if (is_data_mapping(flags) &&
3274 mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07003275 /* Workaround for Valgrind */
3276 if (rlimit(RLIMIT_DATA) == 0 &&
3277 mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT)
3278 return true;
3279 if (!ignore_rlimit_data) {
3280 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 -08003281 current->comm, current->pid,
3282 (mm->data_vm + npages) << PAGE_SHIFT,
3283 rlimit(RLIMIT_DATA));
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003284 return false;
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07003285 }
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003286 }
akpm@osdl.org119f6572005-05-01 08:58:35 -07003287
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003288 return true;
3289}
3290
3291void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
3292{
3293 mm->total_vm += npages;
3294
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003295 if (is_exec_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003296 mm->exec_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003297 else if (is_stack_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003298 mm->stack_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08003299 else if (is_data_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003300 mm->data_vm += npages;
akpm@osdl.org119f6572005-05-01 08:58:35 -07003301}
Roland McGrathfa5dc222007-02-08 14:20:41 -08003302
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003303static int special_mapping_fault(struct vm_area_struct *vma,
3304 struct vm_fault *vmf);
3305
3306/*
3307 * Having a close hook prevents vma merging regardless of flags.
3308 */
3309static void special_mapping_close(struct vm_area_struct *vma)
3310{
3311}
3312
3313static const char *special_mapping_name(struct vm_area_struct *vma)
3314{
3315 return ((struct vm_special_mapping *)vma->vm_private_data)->name;
3316}
3317
Dmitry Safonovb059a452016-06-28 14:35:38 +03003318static int special_mapping_mremap(struct vm_area_struct *new_vma)
3319{
3320 struct vm_special_mapping *sm = new_vma->vm_private_data;
3321
3322 if (sm->mremap)
3323 return sm->mremap(sm, new_vma);
3324 return 0;
3325}
3326
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003327static const struct vm_operations_struct special_mapping_vmops = {
3328 .close = special_mapping_close,
3329 .fault = special_mapping_fault,
Dmitry Safonovb059a452016-06-28 14:35:38 +03003330 .mremap = special_mapping_mremap,
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003331 .name = special_mapping_name,
3332};
3333
3334static const struct vm_operations_struct legacy_special_mapping_vmops = {
3335 .close = special_mapping_close,
3336 .fault = special_mapping_fault,
3337};
Roland McGrathfa5dc222007-02-08 14:20:41 -08003338
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003339static int special_mapping_fault(struct vm_area_struct *vma,
3340 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003341{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003342 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003343 struct page **pages;
3344
Andy Lutomirskif872f542015-12-29 20:12:19 -08003345 if (vma->vm_ops == &legacy_special_mapping_vmops) {
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003346 pages = vma->vm_private_data;
Andy Lutomirskif872f542015-12-29 20:12:19 -08003347 } else {
3348 struct vm_special_mapping *sm = vma->vm_private_data;
3349
3350 if (sm->fault)
3351 return sm->fault(sm, vma, vmf);
3352
3353 pages = sm->pages;
3354 }
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003355
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07003356 for (pgoff = vmf->pgoff; pgoff && *pages; ++pages)
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003357 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003358
3359 if (*pages) {
3360 struct page *page = *pages;
3361 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003362 vmf->page = page;
3363 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003364 }
3365
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003366 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003367}
3368
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003369static struct vm_area_struct *__install_special_mapping(
3370 struct mm_struct *mm,
3371 unsigned long addr, unsigned long len,
Chen Gang27f28b92015-11-05 18:48:41 -08003372 unsigned long vm_flags, void *priv,
3373 const struct vm_operations_struct *ops)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003374{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003375 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003376 struct vm_area_struct *vma;
3377
3378 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
3379 if (unlikely(vma == NULL))
Stefani Seibold3935ed62014-03-17 23:22:02 +01003380 return ERR_PTR(-ENOMEM);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003381
Laurent Dufour6315ccc2018-04-17 16:33:13 +02003382 INIT_VMA(vma);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003383 vma->vm_mm = mm;
3384 vma->vm_start = addr;
3385 vma->vm_end = addr + len;
3386
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07003387 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY;
Coly Li3ed75eb2007-10-18 23:39:15 -07003388 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003389
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003390 vma->vm_ops = ops;
3391 vma->vm_private_data = priv;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003392
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003393 ret = insert_vm_struct(mm, vma);
3394 if (ret)
3395 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003396
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003397 vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003398
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003399 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02003400
Stefani Seibold3935ed62014-03-17 23:22:02 +01003401 return vma;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003402
3403out:
3404 kmem_cache_free(vm_area_cachep, vma);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003405 return ERR_PTR(ret);
3406}
3407
Dmitry Safonov2eefd872016-09-05 16:33:05 +03003408bool vma_is_special_mapping(const struct vm_area_struct *vma,
3409 const struct vm_special_mapping *sm)
3410{
3411 return vma->vm_private_data == sm &&
3412 (vma->vm_ops == &special_mapping_vmops ||
3413 vma->vm_ops == &legacy_special_mapping_vmops);
3414}
3415
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003416/*
3417 * Called with mm->mmap_sem held for writing.
3418 * Insert a new vma covering the given region, with the given flags.
3419 * Its pages are supplied by the given array of struct page *.
3420 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3421 * The region past the last page supplied will always produce SIGBUS.
3422 * The array pointer and the pages it points to are assumed to stay alive
3423 * for as long as this mapping might exist.
3424 */
3425struct vm_area_struct *_install_special_mapping(
3426 struct mm_struct *mm,
3427 unsigned long addr, unsigned long len,
3428 unsigned long vm_flags, const struct vm_special_mapping *spec)
3429{
Chen Gang27f28b92015-11-05 18:48:41 -08003430 return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
3431 &special_mapping_vmops);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003432}
3433
Stefani Seibold3935ed62014-03-17 23:22:02 +01003434int install_special_mapping(struct mm_struct *mm,
3435 unsigned long addr, unsigned long len,
3436 unsigned long vm_flags, struct page **pages)
3437{
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003438 struct vm_area_struct *vma = __install_special_mapping(
Chen Gang27f28b92015-11-05 18:48:41 -08003439 mm, addr, len, vm_flags, (void *)pages,
3440 &legacy_special_mapping_vmops);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003441
Duan Jiong14bd5b42014-06-04 16:07:05 -07003442 return PTR_ERR_OR_ZERO(vma);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003443}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003444
3445static DEFINE_MUTEX(mm_all_locks_mutex);
3446
Peter Zijlstra454ed842008-08-11 09:30:25 +02003447static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003448{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003449 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003450 /*
3451 * The LSB of head.next can't change from under us
3452 * because we hold the mm_all_locks_mutex.
3453 */
Jiri Kosina572043c2013-01-11 14:31:59 -08003454 down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003455 /*
3456 * We can safely modify head.next after taking the
Ingo Molnar5a505082012-12-02 19:56:46 +00003457 * anon_vma->root->rwsem. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003458 * the same anon_vma we won't take it again.
3459 *
3460 * No need of atomic instructions here, head.next
3461 * can't change from under us thanks to the
Ingo Molnar5a505082012-12-02 19:56:46 +00003462 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003463 */
3464 if (__test_and_set_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003465 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003466 BUG();
3467 }
3468}
3469
Peter Zijlstra454ed842008-08-11 09:30:25 +02003470static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003471{
3472 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3473 /*
3474 * AS_MM_ALL_LOCKS can't change from under us because
3475 * we hold the mm_all_locks_mutex.
3476 *
3477 * Operations on ->flags have to be atomic because
3478 * even if AS_MM_ALL_LOCKS is stable thanks to the
3479 * mm_all_locks_mutex, there may be other cpus
3480 * changing other bitflags in parallel to us.
3481 */
3482 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
3483 BUG();
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003484 down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003485 }
3486}
3487
3488/*
3489 * This operation locks against the VM for all pte/vma/mm related
3490 * operations that could ever happen on a certain mm. This includes
3491 * vmtruncate, try_to_unmap, and all page faults.
3492 *
3493 * The caller must take the mmap_sem in write mode before calling
3494 * mm_take_all_locks(). The caller isn't allowed to release the
3495 * mmap_sem until mm_drop_all_locks() returns.
3496 *
3497 * mmap_sem in write mode is required in order to block all operations
3498 * that could modify pagetables and free pages without need of
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -08003499 * altering the vma layout. It's also needed in write mode to avoid new
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003500 * anon_vmas to be associated with existing vmas.
3501 *
3502 * A single task can't take more than one mm_take_all_locks() in a row
3503 * or it would deadlock.
3504 *
Michel Lespinassebf181b92012-10-08 16:31:39 -07003505 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003506 * mapping->flags avoid to take the same lock twice, if more than one
3507 * vma in this mm is backed by the same anon_vma or address_space.
3508 *
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003509 * We take locks in following order, accordingly to comment at beginning
3510 * of mm/rmap.c:
3511 * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
3512 * hugetlb mapping);
3513 * - all i_mmap_rwsem locks;
3514 * - all anon_vma->rwseml
3515 *
3516 * We can take all locks within these types randomly because the VM code
3517 * doesn't nest them and we protected from parallel mm_take_all_locks() by
3518 * mm_all_locks_mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003519 *
3520 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3521 * that may have to take thousand of locks.
3522 *
3523 * mm_take_all_locks() can fail if it's interrupted by signals.
3524 */
3525int mm_take_all_locks(struct mm_struct *mm)
3526{
3527 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003528 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003529
3530 BUG_ON(down_read_trylock(&mm->mmap_sem));
3531
3532 mutex_lock(&mm_all_locks_mutex);
3533
3534 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3535 if (signal_pending(current))
3536 goto out_unlock;
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003537 if (vma->vm_file && vma->vm_file->f_mapping &&
3538 is_vm_hugetlb_page(vma))
3539 vm_lock_mapping(mm, vma->vm_file->f_mapping);
3540 }
3541
3542 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3543 if (signal_pending(current))
3544 goto out_unlock;
3545 if (vma->vm_file && vma->vm_file->f_mapping &&
3546 !is_vm_hugetlb_page(vma))
Peter Zijlstra454ed842008-08-11 09:30:25 +02003547 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003548 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003549
3550 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3551 if (signal_pending(current))
3552 goto out_unlock;
3553 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003554 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3555 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003556 }
3557
Kautuk Consul584cff52011-10-31 17:08:59 -07003558 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003559
3560out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07003561 mm_drop_all_locks(mm);
3562 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003563}
3564
3565static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
3566{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003567 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003568 /*
3569 * The LSB of head.next can't change to 0 from under
3570 * us because we hold the mm_all_locks_mutex.
3571 *
3572 * We must however clear the bitflag before unlocking
Michel Lespinassebf181b92012-10-08 16:31:39 -07003573 * the vma so the users using the anon_vma->rb_root will
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003574 * never see our bitflag.
3575 *
3576 * No need of atomic instructions here, head.next
3577 * can't change from under us until we release the
Ingo Molnar5a505082012-12-02 19:56:46 +00003578 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003579 */
3580 if (!__test_and_clear_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003581 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003582 BUG();
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08003583 anon_vma_unlock_write(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003584 }
3585}
3586
3587static void vm_unlock_mapping(struct address_space *mapping)
3588{
3589 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3590 /*
3591 * AS_MM_ALL_LOCKS can't change to 0 from under us
3592 * because we hold the mm_all_locks_mutex.
3593 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003594 i_mmap_unlock_write(mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003595 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
3596 &mapping->flags))
3597 BUG();
3598 }
3599}
3600
3601/*
3602 * The mmap_sem cannot be released by the caller until
3603 * mm_drop_all_locks() returns.
3604 */
3605void mm_drop_all_locks(struct mm_struct *mm)
3606{
3607 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003608 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003609
3610 BUG_ON(down_read_trylock(&mm->mmap_sem));
3611 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
3612
3613 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3614 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003615 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3616 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003617 if (vma->vm_file && vma->vm_file->f_mapping)
3618 vm_unlock_mapping(vma->vm_file->f_mapping);
3619 }
3620
3621 mutex_unlock(&mm_all_locks_mutex);
3622}
David Howells8feae132009-01-08 12:04:47 +00003623
3624/*
3625 * initialise the VMA slab
3626 */
3627void __init mmap_init(void)
3628{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003629 int ret;
3630
Tejun Heo908c7f12014-09-08 09:51:29 +09003631 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003632 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00003633}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003634
3635/*
3636 * Initialise sysctl_user_reserve_kbytes.
3637 *
3638 * This is intended to prevent a user from starting a single memory hogging
3639 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3640 * mode.
3641 *
3642 * The default value is min(3% of free memory, 128MB)
3643 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3644 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003645static int init_user_reserve(void)
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003646{
3647 unsigned long free_kbytes;
3648
3649 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3650
3651 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
3652 return 0;
3653}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003654subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003655
3656/*
3657 * Initialise sysctl_admin_reserve_kbytes.
3658 *
3659 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3660 * to log in and kill a memory hogging process.
3661 *
3662 * Systems with more than 256MB will reserve 8MB, enough to recover
3663 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3664 * only reserve 3% of free pages by default.
3665 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003666static int init_admin_reserve(void)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003667{
3668 unsigned long free_kbytes;
3669
3670 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3671
3672 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
3673 return 0;
3674}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003675subsys_initcall(init_admin_reserve);
Andrew Shewmaker16408792013-04-29 15:08:12 -07003676
3677/*
3678 * Reinititalise user and admin reserves if memory is added or removed.
3679 *
3680 * The default user reserve max is 128MB, and the default max for the
3681 * admin reserve is 8MB. These are usually, but not always, enough to
3682 * enable recovery from a memory hogging process using login/sshd, a shell,
3683 * and tools like top. It may make sense to increase or even disable the
3684 * reserve depending on the existence of swap or variations in the recovery
3685 * tools. So, the admin may have changed them.
3686 *
3687 * If memory is added and the reserves have been eliminated or increased above
3688 * the default max, then we'll trust the admin.
3689 *
3690 * If memory is removed and there isn't enough free memory, then we
3691 * need to reset the reserves.
3692 *
3693 * Otherwise keep the reserve set by the admin.
3694 */
3695static int reserve_mem_notifier(struct notifier_block *nb,
3696 unsigned long action, void *data)
3697{
3698 unsigned long tmp, free_kbytes;
3699
3700 switch (action) {
3701 case MEM_ONLINE:
3702 /* Default max is 128MB. Leave alone if modified by operator. */
3703 tmp = sysctl_user_reserve_kbytes;
3704 if (0 < tmp && tmp < (1UL << 17))
3705 init_user_reserve();
3706
3707 /* Default max is 8MB. Leave alone if modified by operator. */
3708 tmp = sysctl_admin_reserve_kbytes;
3709 if (0 < tmp && tmp < (1UL << 13))
3710 init_admin_reserve();
3711
3712 break;
3713 case MEM_OFFLINE:
3714 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3715
3716 if (sysctl_user_reserve_kbytes > free_kbytes) {
3717 init_user_reserve();
3718 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3719 sysctl_user_reserve_kbytes);
3720 }
3721
3722 if (sysctl_admin_reserve_kbytes > free_kbytes) {
3723 init_admin_reserve();
3724 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3725 sysctl_admin_reserve_kbytes);
3726 }
3727 break;
3728 default:
3729 break;
3730 }
3731 return NOTIFY_OK;
3732}
3733
3734static struct notifier_block reserve_mem_nb = {
3735 .notifier_call = reserve_mem_notifier,
3736};
3737
3738static int __meminit init_reserve_notifier(void)
3739{
3740 if (register_hotmemory_notifier(&reserve_mem_nb))
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07003741 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
Andrew Shewmaker16408792013-04-29 15:08:12 -07003742
3743 return 0;
3744}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003745subsys_initcall(init_reserve_notifier);