blob: 86b18f334f4f759d8342a5220f8ac850c770ab03 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/mmap.c
3 *
4 * Written by obz.
5 *
Alan Cox046c6882009-01-05 14:06:29 +00006 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Cyril Hrubise8420a82013-04-29 15:08:33 -070011#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070013#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
Davidlohr Bueso615d6e82014-04-07 15:37:25 -070015#include <linux/vmacache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/shm.h>
17#include <linux/mman.h>
18#include <linux/pagemap.h>
19#include <linux/swap.h>
20#include <linux/syscalls.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/file.h>
24#include <linux/fs.h>
25#include <linux/personality.h>
26#include <linux/security.h>
27#include <linux/hugetlb.h>
Hugh Dickinsc01d5b32016-07-26 15:26:15 -070028#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/profile.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040030#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/mount.h>
32#include <linux/mempolicy.h>
33#include <linux/rmap.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070034#include <linux/mmu_notifier.h>
Konstantin Khlebnikov82f71ae2014-08-06 16:06:36 -070035#include <linux/mmdebug.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020036#include <linux/perf_event.h>
Al Viro120a7952010-10-30 02:54:44 -040037#include <linux/audit.h>
Andrea Arcangelib15d00b2011-01-13 15:46:59 -080038#include <linux/khugepaged.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053039#include <linux/uprobes.h>
Michel Lespinassed3737182012-12-11 16:01:38 -080040#include <linux/rbtree_augmented.h>
Andrew Shewmaker16408792013-04-29 15:08:12 -070041#include <linux/notifier.h>
42#include <linux/memory.h>
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -070043#include <linux/printk.h>
Andrea Arcangeli19a809a2015-09-04 15:46:24 -070044#include <linux/userfaultfd_k.h>
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -080045#include <linux/moduleparam.h>
Dave Hansen62b5f7d2016-02-12 13:02:40 -080046#include <linux/pkeys.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/uaccess.h>
49#include <asm/cacheflush.h>
50#include <asm/tlb.h>
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +020051#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jan Beulich42b77722008-07-23 21:27:10 -070053#include "internal.h"
54
Kirill Korotaev3a459752006-09-07 14:17:04 +040055#ifndef arch_mmap_check
56#define arch_mmap_check(addr, len, flags) (0)
57#endif
58
Daniel Cashmand07e2252016-01-14 15:19:53 -080059#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
60const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
61const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
62int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
63#endif
64#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
65const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
66const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
67int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
68#endif
69
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -070070static bool ignore_rlimit_data;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -080071core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644);
Daniel Cashmand07e2252016-01-14 15:19:53 -080072
Hugh Dickinse0da3822005-04-19 13:29:15 -070073static void unmap_region(struct mm_struct *mm,
74 struct vm_area_struct *vma, struct vm_area_struct *prev,
75 unsigned long start, unsigned long end);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* description of effects of mapping type and prot in current implementation.
78 * this is due to the limited x86 page protection hardware. The expected
79 * behavior is in parens:
80 *
81 * map_type prot
82 * PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
83 * MAP_SHARED r: (no) no r: (yes) yes r: (no) yes r: (no) yes
84 * w: (no) no w: (no) no w: (yes) yes w: (no) no
85 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
vishnu.pscc71aba2014-10-09 15:26:29 -070086 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * MAP_PRIVATE r: (no) no r: (yes) yes r: (no) yes r: (no) yes
88 * w: (no) no w: (no) no w: (copy) copy w: (no) no
89 * x: (no) no x: (no) yes x: (no) yes x: (yes) yes
90 *
91 */
92pgprot_t protection_map[16] = {
93 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
94 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
95};
96
Hugh Dickins804af2c2006-07-26 21:39:49 +010097pgprot_t vm_get_page_prot(unsigned long vm_flags)
98{
Dave Kleikampb845f312008-07-08 00:28:51 +100099 return __pgprot(pgprot_val(protection_map[vm_flags &
100 (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]) |
101 pgprot_val(arch_vm_get_page_prot(vm_flags)));
Hugh Dickins804af2c2006-07-26 21:39:49 +0100102}
103EXPORT_SYMBOL(vm_get_page_prot);
104
Peter Feiner64e45502014-10-13 15:55:46 -0700105static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags)
106{
107 return pgprot_modify(oldprot, vm_get_page_prot(vm_flags));
108}
109
110/* Update vma->vm_page_prot to reflect vma->vm_flags. */
111void vma_set_page_prot(struct vm_area_struct *vma)
112{
113 unsigned long vm_flags = vma->vm_flags;
114
115 vma->vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags);
116 if (vma_wants_writenotify(vma)) {
117 vm_flags &= ~VM_SHARED;
118 vma->vm_page_prot = vm_pgprot_modify(vma->vm_page_prot,
119 vm_flags);
120 }
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800124 * Requires inode->i_mapping->i_mmap_rwsem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
126static void __remove_shared_vm_struct(struct vm_area_struct *vma,
127 struct file *file, struct address_space *mapping)
128{
129 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500130 atomic_inc(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700132 mapping_unmap_writable(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 flush_dcache_mmap_lock(mapping);
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800135 vma_interval_tree_remove(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 flush_dcache_mmap_unlock(mapping);
137}
138
139/*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700140 * Unlink a file-based vm structure from its interval tree, to hide
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700141 * vma from rmap and vmtruncate before freeing its page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700143void unlink_file_vma(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct file *file = vma->vm_file;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (file) {
148 struct address_space *mapping = file->f_mapping;
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800149 i_mmap_lock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 __remove_shared_vm_struct(vma, file, mapping);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800151 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700153}
154
155/*
156 * Close a vm structure and free it, returning the next.
157 */
158static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
159{
160 struct vm_area_struct *next = vma->vm_next;
161
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700162 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (vma->vm_ops && vma->vm_ops->close)
164 vma->vm_ops->close(vma);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -0700165 if (vma->vm_file)
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700166 fput(vma->vm_file);
Lee Schermerhornf0be3d32008-04-28 02:13:08 -0700167 mpol_put(vma_policy(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 kmem_cache_free(vm_area_cachep, vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -0700169 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Linus Torvalds5d22fc22016-05-27 15:57:31 -0700172static int do_brk(unsigned long addr, unsigned long len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -0700173
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100174SYSCALL_DEFINE1(brk, unsigned long, brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700176 unsigned long retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned long newbrk, oldbrk;
178 struct mm_struct *mm = current->mm;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700179 unsigned long min_brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800180 bool populate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700182 if (down_write_killable(&mm->mmap_sem))
183 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700185#ifdef CONFIG_COMPAT_BRK
Jiri Kosina5520e892011-01-13 15:47:23 -0800186 /*
187 * CONFIG_COMPAT_BRK can still be overridden by setting
188 * randomize_va_space to 2, which will still cause mm->start_brk
189 * to be arbitrarily shifted
190 */
Jiri Kosina4471a672011-04-14 15:22:09 -0700191 if (current->brk_randomized)
Jiri Kosina5520e892011-01-13 15:47:23 -0800192 min_brk = mm->start_brk;
193 else
194 min_brk = mm->end_data;
Jiri Kosinaa5b45922008-06-05 22:46:05 -0700195#else
196 min_brk = mm->start_brk;
197#endif
198 if (brk < min_brk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto out;
Ram Gupta1e624192006-04-10 22:52:57 -0700200
201 /*
202 * Check against rlimit here. If this check is done later after the test
203 * of oldbrk with newbrk then it can escape the test and let the data
204 * segment grow beyond its set limit the in case where the limit is
205 * not page aligned -Ram Gupta
206 */
Cyrill Gorcunov8764b332014-10-09 15:27:32 -0700207 if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk,
208 mm->end_data, mm->start_data))
Ram Gupta1e624192006-04-10 22:52:57 -0700209 goto out;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 newbrk = PAGE_ALIGN(brk);
212 oldbrk = PAGE_ALIGN(mm->brk);
213 if (oldbrk == newbrk)
214 goto set_brk;
215
216 /* Always allow shrinking brk. */
217 if (brk <= mm->brk) {
218 if (!do_munmap(mm, newbrk, oldbrk-newbrk))
219 goto set_brk;
220 goto out;
221 }
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Check against existing mmap mappings. */
224 if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
225 goto out;
226
227 /* Ok, looks good - let it rip. */
Linus Torvalds5d22fc22016-05-27 15:57:31 -0700228 if (do_brk(oldbrk, newbrk-oldbrk) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto out;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231set_brk:
232 mm->brk = brk;
Michel Lespinasse128557f2013-02-22 16:32:40 -0800233 populate = newbrk > oldbrk && (mm->def_flags & VM_LOCKED) != 0;
234 up_write(&mm->mmap_sem);
235 if (populate)
236 mm_populate(oldbrk, newbrk - oldbrk);
237 return brk;
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239out:
240 retval = mm->brk;
241 up_write(&mm->mmap_sem);
242 return retval;
243}
244
Michel Lespinassed3737182012-12-11 16:01:38 -0800245static long vma_compute_subtree_gap(struct vm_area_struct *vma)
246{
247 unsigned long max, subtree_gap;
248 max = vma->vm_start;
249 if (vma->vm_prev)
250 max -= vma->vm_prev->vm_end;
251 if (vma->vm_rb.rb_left) {
252 subtree_gap = rb_entry(vma->vm_rb.rb_left,
253 struct vm_area_struct, vm_rb)->rb_subtree_gap;
254 if (subtree_gap > max)
255 max = subtree_gap;
256 }
257 if (vma->vm_rb.rb_right) {
258 subtree_gap = rb_entry(vma->vm_rb.rb_right,
259 struct vm_area_struct, vm_rb)->rb_subtree_gap;
260 if (subtree_gap > max)
261 max = subtree_gap;
262 }
263 return max;
264}
265
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700266#ifdef CONFIG_DEBUG_VM_RB
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800267static int browse_rb(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800269 struct rb_root *root = &mm->mm_rb;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800270 int i = 0, j, bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct rb_node *nd, *pn = NULL;
272 unsigned long prev = 0, pend = 0;
273
274 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
275 struct vm_area_struct *vma;
276 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800277 if (vma->vm_start < prev) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700278 pr_emerg("vm_start %lx < prev %lx\n",
279 vma->vm_start, prev);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800280 bug = 1;
281 }
282 if (vma->vm_start < pend) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700283 pr_emerg("vm_start %lx < pend %lx\n",
284 vma->vm_start, pend);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800285 bug = 1;
286 }
287 if (vma->vm_start > vma->vm_end) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700288 pr_emerg("vm_start %lx > vm_end %lx\n",
289 vma->vm_start, vma->vm_end);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800290 bug = 1;
291 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800292 spin_lock(&mm->page_table_lock);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800293 if (vma->rb_subtree_gap != vma_compute_subtree_gap(vma)) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700294 pr_emerg("free gap %lx, correct %lx\n",
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800295 vma->rb_subtree_gap,
296 vma_compute_subtree_gap(vma));
297 bug = 1;
298 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800299 spin_unlock(&mm->page_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 i++;
301 pn = nd;
David Millerd1af65d2007-02-28 20:13:13 -0800302 prev = vma->vm_start;
303 pend = vma->vm_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 j = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800306 for (nd = pn; nd; nd = rb_prev(nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 j++;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800308 if (i != j) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700309 pr_emerg("backwards %d, forwards %d\n", j, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800310 bug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800312 return bug ? -1 : i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Michel Lespinassed3737182012-12-11 16:01:38 -0800315static void validate_mm_rb(struct rb_root *root, struct vm_area_struct *ignore)
316{
317 struct rb_node *nd;
318
319 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
320 struct vm_area_struct *vma;
321 vma = rb_entry(nd, struct vm_area_struct, vm_rb);
Sasha Levin96dad672014-10-09 15:28:39 -0700322 VM_BUG_ON_VMA(vma != ignore &&
323 vma->rb_subtree_gap != vma_compute_subtree_gap(vma),
324 vma);
Michel Lespinassed3737182012-12-11 16:01:38 -0800325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Rashika Kheriaeafd4dc2014-04-03 14:48:03 -0700328static void validate_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int bug = 0;
331 int i = 0;
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800332 unsigned long highest_address = 0;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700333 struct vm_area_struct *vma = mm->mmap;
Andrew Mortonff26f702014-10-09 15:28:19 -0700334
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700335 while (vma) {
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -0800336 struct anon_vma *anon_vma = vma->anon_vma;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700337 struct anon_vma_chain *avc;
Andrew Mortonff26f702014-10-09 15:28:19 -0700338
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -0800339 if (anon_vma) {
340 anon_vma_lock_read(anon_vma);
341 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
342 anon_vma_interval_tree_verify(avc);
343 anon_vma_unlock_read(anon_vma);
344 }
345
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800346 highest_address = vma->vm_end;
Michel Lespinasseed8ea812012-10-08 16:31:45 -0700347 vma = vma->vm_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 i++;
349 }
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800350 if (i != mm->map_count) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700351 pr_emerg("map_count %d vm_next %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800352 bug = 1;
353 }
354 if (highest_address != mm->highest_vm_end) {
Sasha Levin8542bdf2014-09-09 14:50:59 -0700355 pr_emerg("mm->highest_vm_end %lx, found %lx\n",
Andrew Mortonff26f702014-10-09 15:28:19 -0700356 mm->highest_vm_end, highest_address);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800357 bug = 1;
358 }
Andrea Arcangeliacf128d2016-02-05 15:36:13 -0800359 i = browse_rb(mm);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800360 if (i != mm->map_count) {
Andrew Mortonff26f702014-10-09 15:28:19 -0700361 if (i != -1)
362 pr_emerg("map_count %d rb %d\n", mm->map_count, i);
Michel Lespinasse5a0768f2012-12-11 16:01:42 -0800363 bug = 1;
364 }
Sasha Levin96dad672014-10-09 15:28:39 -0700365 VM_BUG_ON_MM(bug, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367#else
Michel Lespinassed3737182012-12-11 16:01:38 -0800368#define validate_mm_rb(root, ignore) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#define validate_mm(mm) do { } while (0)
370#endif
371
Michel Lespinassed3737182012-12-11 16:01:38 -0800372RB_DECLARE_CALLBACKS(static, vma_gap_callbacks, struct vm_area_struct, vm_rb,
373 unsigned long, rb_subtree_gap, vma_compute_subtree_gap)
374
375/*
376 * Update augmented rbtree rb_subtree_gap values after vma->vm_start or
377 * vma->vm_prev->vm_end values changed, without modifying the vma's position
378 * in the rbtree.
379 */
380static void vma_gap_update(struct vm_area_struct *vma)
381{
382 /*
383 * As it turns out, RB_DECLARE_CALLBACKS() already created a callback
384 * function that does exacltly what we want.
385 */
386 vma_gap_callbacks_propagate(&vma->vm_rb, NULL);
387}
388
389static inline void vma_rb_insert(struct vm_area_struct *vma,
390 struct rb_root *root)
391{
392 /* All rb_subtree_gap values must be consistent prior to insertion */
393 validate_mm_rb(root, NULL);
394
395 rb_insert_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
396}
397
398static void vma_rb_erase(struct vm_area_struct *vma, struct rb_root *root)
399{
400 /*
401 * All rb_subtree_gap values must be consistent prior to erase,
402 * with the possible exception of the vma being erased.
403 */
404 validate_mm_rb(root, vma);
405
406 /*
407 * Note rb_erase_augmented is a fairly large inline function,
408 * so make sure we instantiate it only once with our desired
409 * augmented rbtree callbacks.
410 */
411 rb_erase_augmented(&vma->vm_rb, root, &vma_gap_callbacks);
412}
413
Michel Lespinassebf181b92012-10-08 16:31:39 -0700414/*
415 * vma has some anon_vma assigned, and is already inserted on that
416 * anon_vma's interval trees.
417 *
418 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
419 * vma must be removed from the anon_vma's interval trees using
420 * anon_vma_interval_tree_pre_update_vma().
421 *
422 * After the update, the vma will be reinserted using
423 * anon_vma_interval_tree_post_update_vma().
424 *
425 * The entire update must be protected by exclusive mmap_sem and by
426 * the root anon_vma's mutex.
427 */
428static inline void
429anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
430{
431 struct anon_vma_chain *avc;
432
433 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
434 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
435}
436
437static inline void
438anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
439{
440 struct anon_vma_chain *avc;
441
442 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
443 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
444}
445
Hugh Dickins6597d782012-10-08 16:29:07 -0700446static int find_vma_links(struct mm_struct *mm, unsigned long addr,
447 unsigned long end, struct vm_area_struct **pprev,
448 struct rb_node ***rb_link, struct rb_node **rb_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Hugh Dickins6597d782012-10-08 16:29:07 -0700450 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 __rb_link = &mm->mm_rb.rb_node;
453 rb_prev = __rb_parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 while (*__rb_link) {
456 struct vm_area_struct *vma_tmp;
457
458 __rb_parent = *__rb_link;
459 vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);
460
461 if (vma_tmp->vm_end > addr) {
Hugh Dickins6597d782012-10-08 16:29:07 -0700462 /* Fail if an existing vma overlaps the area */
463 if (vma_tmp->vm_start < end)
464 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 __rb_link = &__rb_parent->rb_left;
466 } else {
467 rb_prev = __rb_parent;
468 __rb_link = &__rb_parent->rb_right;
469 }
470 }
471
472 *pprev = NULL;
473 if (rb_prev)
474 *pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
475 *rb_link = __rb_link;
476 *rb_parent = __rb_parent;
Hugh Dickins6597d782012-10-08 16:29:07 -0700477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Cyril Hrubise8420a82013-04-29 15:08:33 -0700480static unsigned long count_vma_pages_range(struct mm_struct *mm,
481 unsigned long addr, unsigned long end)
482{
483 unsigned long nr_pages = 0;
484 struct vm_area_struct *vma;
485
486 /* Find first overlaping mapping */
487 vma = find_vma_intersection(mm, addr, end);
488 if (!vma)
489 return 0;
490
491 nr_pages = (min(end, vma->vm_end) -
492 max(addr, vma->vm_start)) >> PAGE_SHIFT;
493
494 /* Iterate over the rest of the overlaps */
495 for (vma = vma->vm_next; vma; vma = vma->vm_next) {
496 unsigned long overlap_len;
497
498 if (vma->vm_start > end)
499 break;
500
501 overlap_len = min(end, vma->vm_end) - vma->vm_start;
502 nr_pages += overlap_len >> PAGE_SHIFT;
503 }
504
505 return nr_pages;
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
509 struct rb_node **rb_link, struct rb_node *rb_parent)
510{
Michel Lespinassed3737182012-12-11 16:01:38 -0800511 /* Update tracking information for the gap following the new vma. */
512 if (vma->vm_next)
513 vma_gap_update(vma->vm_next);
514 else
515 mm->highest_vm_end = vma->vm_end;
516
517 /*
518 * vma->vm_prev wasn't known when we followed the rbtree to find the
519 * correct insertion point for that vma. As a result, we could not
520 * update the vma vm_rb parents rb_subtree_gap values on the way down.
521 * So, we first insert the vma with a zero rb_subtree_gap value
522 * (to be consistent with what we did on the way down), and then
523 * immediately update the gap to the correct value. Finally we
524 * rebalance the rbtree after all augmented values have been set.
525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 rb_link_node(&vma->vm_rb, rb_parent, rb_link);
Michel Lespinassed3737182012-12-11 16:01:38 -0800527 vma->rb_subtree_gap = 0;
528 vma_gap_update(vma);
529 vma_rb_insert(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Denys Vlasenkocb8f4882008-10-18 20:27:01 -0700532static void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
ZhenwenXu48aae422009-01-06 14:40:21 -0800534 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 file = vma->vm_file;
537 if (file) {
538 struct address_space *mapping = file->f_mapping;
539
540 if (vma->vm_flags & VM_DENYWRITE)
Al Viro496ad9a2013-01-23 17:07:38 -0500541 atomic_dec(&file_inode(file)->i_writecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (vma->vm_flags & VM_SHARED)
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700543 atomic_inc(&mapping->i_mmap_writable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 flush_dcache_mmap_lock(mapping);
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800546 vma_interval_tree_insert(vma, &mapping->i_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 flush_dcache_mmap_unlock(mapping);
548 }
549}
550
551static void
552__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
553 struct vm_area_struct *prev, struct rb_node **rb_link,
554 struct rb_node *rb_parent)
555{
556 __vma_link_list(mm, vma, prev, rb_parent);
557 __vma_link_rb(mm, vma, rb_link, rb_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
561 struct vm_area_struct *prev, struct rb_node **rb_link,
562 struct rb_node *rb_parent)
563{
564 struct address_space *mapping = NULL;
565
Huang Shijie64ac4942014-06-04 16:07:33 -0700566 if (vma->vm_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 mapping = vma->vm_file->f_mapping;
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800568 i_mmap_lock_write(mapping);
Huang Shijie64ac4942014-06-04 16:07:33 -0700569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 __vma_link(mm, vma, prev, rb_link, rb_parent);
572 __vma_link_file(vma);
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (mapping)
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800575 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 mm->map_count++;
578 validate_mm(mm);
579}
580
581/*
Kautuk Consul88f6b4c2012-03-21 16:34:16 -0700582 * Helper for vma_adjust() in the split_vma insert case: insert a vma into the
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700583 * mm's list and rbtree. It has already been inserted into the interval tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
ZhenwenXu48aae422009-01-06 14:40:21 -0800585static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Hugh Dickins6597d782012-10-08 16:29:07 -0700587 struct vm_area_struct *prev;
ZhenwenXu48aae422009-01-06 14:40:21 -0800588 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Hugh Dickins6597d782012-10-08 16:29:07 -0700590 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
591 &prev, &rb_link, &rb_parent))
592 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 __vma_link(mm, vma, prev, rb_link, rb_parent);
594 mm->map_count++;
595}
596
597static inline void
598__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
599 struct vm_area_struct *prev)
600{
Michel Lespinassed3737182012-12-11 16:01:38 -0800601 struct vm_area_struct *next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700602
Michel Lespinassed3737182012-12-11 16:01:38 -0800603 vma_rb_erase(vma, &mm->mm_rb);
604 prev->vm_next = next = vma->vm_next;
Linus Torvalds297c5ee2010-08-20 16:24:55 -0700605 if (next)
606 next->vm_prev = prev;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -0700607
608 /* Kill the cache */
609 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/*
613 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
614 * is already present in an i_mmap tree without adjusting the tree.
615 * The following helper function should be used when such adjustments
616 * are necessary. The "insert" vma (if any) is to be inserted
617 * before we drop the necessary locks.
618 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800619int vma_adjust(struct vm_area_struct *vma, unsigned long start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
621{
622 struct mm_struct *mm = vma->vm_mm;
623 struct vm_area_struct *next = vma->vm_next;
624 struct vm_area_struct *importer = NULL;
625 struct address_space *mapping = NULL;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700626 struct rb_root *root = NULL;
Rik van Riel012f18002010-08-09 17:18:40 -0700627 struct anon_vma *anon_vma = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct file *file = vma->vm_file;
Michel Lespinassed3737182012-12-11 16:01:38 -0800629 bool start_changed = false, end_changed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 long adjust_next = 0;
631 int remove_next = 0;
632
633 if (next && !insert) {
Linus Torvalds287d97a2010-04-10 15:22:30 -0700634 struct vm_area_struct *exporter = NULL;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (end >= next->vm_end) {
637 /*
638 * vma expands, overlapping all the next, and
639 * perhaps the one after too (mprotect case 6).
640 */
641again: remove_next = 1 + (end > next->vm_end);
642 end = next->vm_end;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700643 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 importer = vma;
645 } else if (end > next->vm_start) {
646 /*
647 * vma expands, overlapping part of the next:
648 * mprotect case 5 shifting the boundary up.
649 */
650 adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
Linus Torvalds287d97a2010-04-10 15:22:30 -0700651 exporter = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 importer = vma;
653 } else if (end < vma->vm_end) {
654 /*
655 * vma shrinks, and !insert tells it's not
656 * split_vma inserting another: so it must be
657 * mprotect case 4 shifting the boundary down.
658 */
vishnu.pscc71aba2014-10-09 15:26:29 -0700659 adjust_next = -((vma->vm_end - end) >> PAGE_SHIFT);
Linus Torvalds287d97a2010-04-10 15:22:30 -0700660 exporter = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 importer = next;
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Rik van Riel5beb4932010-03-05 13:42:07 -0800664 /*
665 * Easily overlooked: when mprotect shifts the boundary,
666 * make sure the expanding vma has anon_vma set if the
667 * shrinking vma had, to cover any anon pages imported.
668 */
Linus Torvalds287d97a2010-04-10 15:22:30 -0700669 if (exporter && exporter->anon_vma && !importer->anon_vma) {
Daniel Forrestc4ea95d2014-12-02 15:59:42 -0800670 int error;
671
Linus Torvalds287d97a2010-04-10 15:22:30 -0700672 importer->anon_vma = exporter->anon_vma;
Konstantin Khlebnikovb800c912015-01-11 16:54:06 +0300673 error = anon_vma_clone(importer, exporter);
Leon Yu3fe89b32015-03-25 15:55:11 -0700674 if (error)
Konstantin Khlebnikovb800c912015-01-11 16:54:06 +0300675 return error;
Rik van Riel5beb4932010-03-05 13:42:07 -0800676 }
677 }
678
Kirill A. Shutemov37f9f552016-07-26 15:25:48 -0700679 vma_adjust_trans_huge(vma, start, end, adjust_next);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (file) {
682 mapping = file->f_mapping;
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800683 root = &mapping->i_mmap;
684 uprobe_munmap(vma, vma->vm_start, vma->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530685
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -0800686 if (adjust_next)
687 uprobe_munmap(next, next->vm_start, next->vm_end);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530688
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800689 i_mmap_lock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (insert) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700692 * Put into interval tree now, so instantiated pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * are visible to arm/parisc __flush_dcache_page
694 * throughout; but we cannot insert into address
695 * space until vma start or end is updated.
696 */
697 __vma_link_file(insert);
698 }
699 }
700
Michel Lespinassebf181b92012-10-08 16:31:39 -0700701 anon_vma = vma->anon_vma;
702 if (!anon_vma && adjust_next)
703 anon_vma = next->anon_vma;
704 if (anon_vma) {
Sasha Levin81d1b092014-10-09 15:28:10 -0700705 VM_BUG_ON_VMA(adjust_next && next->anon_vma &&
706 anon_vma != next->anon_vma, next);
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +0000707 anon_vma_lock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700708 anon_vma_interval_tree_pre_update_vma(vma);
709 if (adjust_next)
710 anon_vma_interval_tree_pre_update_vma(next);
711 }
Rik van Riel012f18002010-08-09 17:18:40 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (root) {
714 flush_dcache_mmap_lock(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700715 vma_interval_tree_remove(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700717 vma_interval_tree_remove(next, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Michel Lespinassed3737182012-12-11 16:01:38 -0800720 if (start != vma->vm_start) {
721 vma->vm_start = start;
722 start_changed = true;
723 }
724 if (end != vma->vm_end) {
725 vma->vm_end = end;
726 end_changed = true;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 vma->vm_pgoff = pgoff;
729 if (adjust_next) {
730 next->vm_start += adjust_next << PAGE_SHIFT;
731 next->vm_pgoff += adjust_next;
732 }
733
734 if (root) {
735 if (adjust_next)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700736 vma_interval_tree_insert(next, root);
737 vma_interval_tree_insert(vma, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 flush_dcache_mmap_unlock(mapping);
739 }
740
741 if (remove_next) {
742 /*
743 * vma_merge has merged next into vma, and needs
744 * us to remove next before dropping the locks.
745 */
746 __vma_unlink(mm, next, vma);
747 if (file)
748 __remove_shared_vm_struct(next, file, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 } else if (insert) {
750 /*
751 * split_vma has split insert from vma, and needs
752 * us to insert it before dropping the locks
753 * (it may either follow vma or precede it).
754 */
755 __insert_vm_struct(mm, insert);
Michel Lespinassed3737182012-12-11 16:01:38 -0800756 } else {
757 if (start_changed)
758 vma_gap_update(vma);
759 if (end_changed) {
760 if (!next)
761 mm->highest_vm_end = end;
762 else if (!adjust_next)
763 vma_gap_update(next);
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
Michel Lespinassebf181b92012-10-08 16:31:39 -0700767 if (anon_vma) {
768 anon_vma_interval_tree_post_update_vma(vma);
769 if (adjust_next)
770 anon_vma_interval_tree_post_update_vma(next);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -0800771 anon_vma_unlock_write(anon_vma);
Michel Lespinassebf181b92012-10-08 16:31:39 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (mapping)
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -0800774 i_mmap_unlock_write(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530776 if (root) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100777 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530778
779 if (adjust_next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100780 uprobe_mmap(next);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530781 }
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (remove_next) {
Matt Helsley925d1c42008-04-29 01:01:36 -0700784 if (file) {
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530785 uprobe_munmap(next, next->vm_start, next->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 fput(file);
Matt Helsley925d1c42008-04-29 01:01:36 -0700787 }
Rik van Riel5beb4932010-03-05 13:42:07 -0800788 if (next->anon_vma)
789 anon_vma_merge(vma, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 mm->map_count--;
Oleg Nesterov3964acd2013-07-31 13:53:28 -0700791 mpol_put(vma_policy(next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 kmem_cache_free(vm_area_cachep, next);
793 /*
794 * In mprotect's case 6 (see comments on vma_merge),
795 * we must remove another next too. It would clutter
796 * up the code too much to do both in one go.
797 */
Michel Lespinassed3737182012-12-11 16:01:38 -0800798 next = vma->vm_next;
799 if (remove_next == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto again;
Michel Lespinassed3737182012-12-11 16:01:38 -0800801 else if (next)
802 vma_gap_update(next);
803 else
804 mm->highest_vm_end = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530806 if (insert && file)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100807 uprobe_mmap(insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 validate_mm(mm);
Rik van Riel5beb4932010-03-05 13:42:07 -0800810
811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814/*
815 * If the vma has a ->close operation then the driver probably needs to release
816 * per-vma resources, so we don't attempt to merge those.
817 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818static inline int is_mergeable_vma(struct vm_area_struct *vma,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700819 struct file *file, unsigned long vm_flags,
820 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Cyrill Gorcunov34228d42014-01-23 15:53:42 -0800822 /*
823 * VM_SOFTDIRTY should not prevent from VMA merging, if we
824 * match the flags but dirty bit -- the caller should mark
825 * merged VMA as dirty. If dirty bit won't be excluded from
826 * comparison, we increase pressue on the memory system forcing
827 * the kernel to generate new VMAs when old one could be
828 * extended instead.
829 */
830 if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
832 if (vma->vm_file != file)
833 return 0;
834 if (vma->vm_ops && vma->vm_ops->close)
835 return 0;
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700836 if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx))
837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 1;
839}
840
841static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
Shaohua Li965f55d2011-05-24 17:11:20 -0700842 struct anon_vma *anon_vma2,
843 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Shaohua Li965f55d2011-05-24 17:11:20 -0700845 /*
846 * The list_is_singular() test is to avoid merging VMA cloned from
847 * parents. This can improve scalability caused by anon_vma lock.
848 */
849 if ((!anon_vma1 || !anon_vma2) && (!vma ||
850 list_is_singular(&vma->anon_vma_chain)))
851 return 1;
852 return anon_vma1 == anon_vma2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/*
856 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
857 * in front of (at a lower virtual address and file offset than) the vma.
858 *
859 * We cannot merge two vmas if they have differently assigned (non-NULL)
860 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
861 *
862 * We don't check here for the merged mmap wrapping around the end of pagecache
863 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
864 * wrap, nor mmaps which cover the final page at index -1UL.
865 */
866static int
867can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700868 struct anon_vma *anon_vma, struct file *file,
869 pgoff_t vm_pgoff,
870 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700872 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700873 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (vma->vm_pgoff == vm_pgoff)
875 return 1;
876 }
877 return 0;
878}
879
880/*
881 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
882 * beyond (at a higher virtual address and file offset than) the vma.
883 *
884 * We cannot merge two vmas if they have differently assigned (non-NULL)
885 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
886 */
887static int
888can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700889 struct anon_vma *anon_vma, struct file *file,
890 pgoff_t vm_pgoff,
891 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700893 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx) &&
Shaohua Li965f55d2011-05-24 17:11:20 -0700894 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 pgoff_t vm_pglen;
Libind6e93212013-07-03 15:01:26 -0700896 vm_pglen = vma_pages(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
898 return 1;
899 }
900 return 0;
901}
902
903/*
904 * Given a mapping request (addr,end,vm_flags,file,pgoff), figure out
905 * whether that can be merged with its predecessor or its successor.
906 * Or both (it neatly fills a hole).
907 *
908 * In most cases - when called for mmap, brk or mremap - [addr,end) is
909 * certain not to be mapped by the time vma_merge is called; but when
910 * called for mprotect, it is certain to be already mapped (either at
911 * an offset within prev, or at the start of next), and the flags of
912 * this area are about to be changed to vm_flags - and the no-change
913 * case has already been eliminated.
914 *
915 * The following mprotect cases have to be considered, where AAAA is
916 * the area passed down from mprotect_fixup, never extending beyond one
917 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
918 *
919 * AAAA AAAA AAAA AAAA
920 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPNNNNNN PPPPNNNNXXXX
921 * cannot merge might become might become might become
922 * PPNNNNNNNNNN PPPPPPPPPPNN PPPPPPPPPPPP 6 or
923 * mmap, brk or case 4 below case 5 below PPPPPPPPXXXX 7 or
924 * mremap move: PPPPNNNNNNNN 8
925 * AAAA
926 * PPPP NNNN PPPPPPPPPPPP PPPPPPPPNNNN PPPPNNNNNNNN
927 * might become case 1 below case 2 below case 3 below
928 *
929 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
930 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
931 */
932struct vm_area_struct *vma_merge(struct mm_struct *mm,
933 struct vm_area_struct *prev, unsigned long addr,
934 unsigned long end, unsigned long vm_flags,
vishnu.pscc71aba2014-10-09 15:26:29 -0700935 struct anon_vma *anon_vma, struct file *file,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700936 pgoff_t pgoff, struct mempolicy *policy,
937 struct vm_userfaultfd_ctx vm_userfaultfd_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
940 struct vm_area_struct *area, *next;
Rik van Riel5beb4932010-03-05 13:42:07 -0800941 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /*
944 * We later require that vma->vm_flags == vm_flags,
945 * so this tests vma->vm_flags & VM_SPECIAL, too.
946 */
947 if (vm_flags & VM_SPECIAL)
948 return NULL;
949
950 if (prev)
951 next = prev->vm_next;
952 else
953 next = mm->mmap;
954 area = next;
955 if (next && next->vm_end == end) /* cases 6, 7, 8 */
956 next = next->vm_next;
957
958 /*
959 * Can it merge with the predecessor?
960 */
961 if (prev && prev->vm_end == addr &&
vishnu.pscc71aba2014-10-09 15:26:29 -0700962 mpol_equal(vma_policy(prev), policy) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 can_vma_merge_after(prev, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700964 anon_vma, file, pgoff,
965 vm_userfaultfd_ctx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /*
967 * OK, it can. Can we now merge in the successor as well?
968 */
969 if (next && end == next->vm_start &&
970 mpol_equal(policy, vma_policy(next)) &&
971 can_vma_merge_before(next, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700972 anon_vma, file,
973 pgoff+pglen,
974 vm_userfaultfd_ctx) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 is_mergeable_anon_vma(prev->anon_vma,
Shaohua Li965f55d2011-05-24 17:11:20 -0700976 next->anon_vma, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* cases 1, 6 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800978 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 next->vm_end, prev->vm_pgoff, NULL);
980 } else /* cases 2, 5, 7 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800981 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 end, prev->vm_pgoff, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -0800983 if (err)
984 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -0700985 khugepaged_enter_vma_merge(prev, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return prev;
987 }
988
989 /*
990 * Can this new request be merged in front of next?
991 */
992 if (next && end == next->vm_start &&
vishnu.pscc71aba2014-10-09 15:26:29 -0700993 mpol_equal(policy, vma_policy(next)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 can_vma_merge_before(next, vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700995 anon_vma, file, pgoff+pglen,
996 vm_userfaultfd_ctx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (prev && addr < prev->vm_end) /* case 4 */
Rik van Riel5beb4932010-03-05 13:42:07 -0800998 err = vma_adjust(prev, prev->vm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 addr, prev->vm_pgoff, NULL);
1000 else /* cases 3, 8 */
Rik van Riel5beb4932010-03-05 13:42:07 -08001001 err = vma_adjust(area, addr, next->vm_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 next->vm_pgoff - pglen, NULL);
Rik van Riel5beb4932010-03-05 13:42:07 -08001003 if (err)
1004 return NULL;
David Rientjes6d50e602014-10-29 14:50:31 -07001005 khugepaged_enter_vma_merge(area, vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return area;
1007 }
1008
1009 return NULL;
1010}
1011
1012/*
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001013 * Rough compatbility check to quickly see if it's even worth looking
1014 * at sharing an anon_vma.
1015 *
1016 * They need to have the same vm_file, and the flags can only differ
1017 * in things that mprotect may change.
1018 *
1019 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1020 * we can merge the two vma's. For example, we refuse to merge a vma if
1021 * there is a vm_ops->close() function, because that indicates that the
1022 * driver is doing some kind of reference counting. But that doesn't
1023 * really matter for the anon_vma sharing case.
1024 */
1025static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1026{
1027 return a->vm_end == b->vm_start &&
1028 mpol_equal(vma_policy(a), vma_policy(b)) &&
1029 a->vm_file == b->vm_file &&
Cyrill Gorcunov34228d42014-01-23 15:53:42 -08001030 !((a->vm_flags ^ b->vm_flags) & ~(VM_READ|VM_WRITE|VM_EXEC|VM_SOFTDIRTY)) &&
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001031 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1032}
1033
1034/*
1035 * Do some basic sanity checking to see if we can re-use the anon_vma
1036 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1037 * the same as 'old', the other will be the new one that is trying
1038 * to share the anon_vma.
1039 *
1040 * NOTE! This runs with mm_sem held for reading, so it is possible that
1041 * the anon_vma of 'old' is concurrently in the process of being set up
1042 * by another page fault trying to merge _that_. But that's ok: if it
1043 * is being set up, that automatically means that it will be a singleton
1044 * acceptable for merging, so we can do all of this optimistically. But
Jason Low4db0c3c2015-04-15 16:14:08 -07001045 * we do that READ_ONCE() to make sure that we never re-load the pointer.
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001046 *
1047 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1048 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1049 * is to return an anon_vma that is "complex" due to having gone through
1050 * a fork).
1051 *
1052 * We also make sure that the two vma's are compatible (adjacent,
1053 * and with the same memory policies). That's all stable, even with just
1054 * a read lock on the mm_sem.
1055 */
1056static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1057{
1058 if (anon_vma_compatible(a, b)) {
Jason Low4db0c3c2015-04-15 16:14:08 -07001059 struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001060
1061 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1062 return anon_vma;
1063 }
1064 return NULL;
1065}
1066
1067/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1069 * neighbouring vmas for a suitable anon_vma, before it goes off
1070 * to allocate a new anon_vma. It checks because a repetitive
1071 * sequence of mprotects and faults may otherwise lead to distinct
1072 * anon_vmas being allocated, preventing vma merge in subsequent
1073 * mprotect.
1074 */
1075struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1076{
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001077 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct vm_area_struct *near;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 near = vma->vm_next;
1081 if (!near)
1082 goto try_prev;
1083
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001084 anon_vma = reusable_anon_vma(near, vma, near);
1085 if (anon_vma)
1086 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087try_prev:
Linus Torvalds9be34c92011-06-16 00:35:09 -07001088 near = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (!near)
1090 goto none;
1091
Linus Torvaldsd0e9fe12010-04-10 10:36:19 -07001092 anon_vma = reusable_anon_vma(near, near, vma);
1093 if (anon_vma)
1094 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095none:
1096 /*
1097 * There's no absolute need to look only at touching neighbours:
1098 * we could search further afield for "compatible" anon_vmas.
1099 * But it would probably just be a waste of time searching,
1100 * or lead to too many vmas hanging off the same anon_vma.
1101 * We're trying to allow mprotect remerging later on,
1102 * not trying to minimize memory used for anon_vmas.
1103 */
1104 return NULL;
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
Al Viro40401532012-02-13 03:58:52 +00001108 * If a hint addr is less than mmap_min_addr change hint to be as
1109 * low as possible but still greater than mmap_min_addr
1110 */
1111static inline unsigned long round_hint_to_min(unsigned long hint)
1112{
1113 hint &= PAGE_MASK;
1114 if (((void *)hint != NULL) &&
1115 (hint < mmap_min_addr))
1116 return PAGE_ALIGN(mmap_min_addr);
1117 return hint;
1118}
1119
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001120static inline int mlock_future_check(struct mm_struct *mm,
1121 unsigned long flags,
1122 unsigned long len)
1123{
1124 unsigned long locked, lock_limit;
1125
1126 /* mlock MCL_FUTURE? */
1127 if (flags & VM_LOCKED) {
1128 locked = len >> PAGE_SHIFT;
1129 locked += mm->locked_vm;
1130 lock_limit = rlimit(RLIMIT_MEMLOCK);
1131 lock_limit >>= PAGE_SHIFT;
1132 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
1133 return -EAGAIN;
1134 }
1135 return 0;
1136}
1137
Al Viro40401532012-02-13 03:58:52 +00001138/*
Jianjun Kong27f5de72009-09-17 19:26:26 -07001139 * The caller must hold down_write(&current->mm->mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001141unsigned long do_mmap(struct file *file, unsigned long addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 unsigned long len, unsigned long prot,
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -07001143 unsigned long flags, vm_flags_t vm_flags,
1144 unsigned long pgoff, unsigned long *populate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
vishnu.pscc71aba2014-10-09 15:26:29 -07001146 struct mm_struct *mm = current->mm;
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001147 int pkey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Michel Lespinasse41badc12013-02-22 16:32:47 -08001149 *populate = 0;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001150
Piotr Kwapulinskie37609b2015-06-24 16:58:39 -07001151 if (!len)
1152 return -EINVAL;
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /*
1155 * Does the application expect PROT_READ to imply PROT_EXEC?
1156 *
1157 * (the exception is when the underlying filesystem is noexec
1158 * mounted, in which case we dont add PROT_EXEC.)
1159 */
1160 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
Eric W. Biederman90f85722015-06-29 14:42:03 -05001161 if (!(file && path_noexec(&file->f_path)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 prot |= PROT_EXEC;
1163
Eric Paris7cd94142007-11-26 18:47:40 -05001164 if (!(flags & MAP_FIXED))
1165 addr = round_hint_to_min(addr);
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /* Careful about overflows.. */
1168 len = PAGE_ALIGN(len);
Al Viro9206de92009-12-03 15:23:11 -05001169 if (!len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return -ENOMEM;
1171
1172 /* offset overflow? */
1173 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
vishnu.pscc71aba2014-10-09 15:26:29 -07001174 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* Too many mappings? */
1177 if (mm->map_count > sysctl_max_map_count)
1178 return -ENOMEM;
1179
1180 /* Obtain the address to map to. we verify (or select) it and ensure
1181 * that it represents a valid section of the address space.
1182 */
1183 addr = get_unmapped_area(file, addr, len, pgoff, flags);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001184 if (offset_in_page(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return addr;
1186
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001187 if (prot == PROT_EXEC) {
1188 pkey = execute_only_pkey(mm);
1189 if (pkey < 0)
1190 pkey = 0;
1191 }
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* Do simple checking here so the lower-level routines won't have
1194 * to. we assume access permissions have been handled by the open
1195 * of the memory object, so we don't do any here.
1196 */
Dave Hansen62b5f7d2016-02-12 13:02:40 -08001197 vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1199
Huang Shijiecdf7b342009-09-21 17:03:36 -07001200 if (flags & MAP_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!can_do_mlock())
1202 return -EPERM;
Rik van Rielba470de2008-10-18 20:26:50 -07001203
Davidlohr Bueso363ee172014-01-21 15:49:15 -08001204 if (mlock_future_check(mm, vm_flags, len))
1205 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (file) {
Oleg Nesterov077bf222013-09-11 14:20:19 -07001208 struct inode *inode = file_inode(file);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 switch (flags & MAP_TYPE) {
1211 case MAP_SHARED:
1212 if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
1213 return -EACCES;
1214
1215 /*
1216 * Make sure we don't allow writing to an append-only
1217 * file..
1218 */
1219 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1220 return -EACCES;
1221
1222 /*
1223 * Make sure there are no mandatory locks on the file.
1224 */
Jeff Laytond7a06982014-03-10 09:54:15 -04001225 if (locks_verify_locked(file))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return -EAGAIN;
1227
1228 vm_flags |= VM_SHARED | VM_MAYSHARE;
1229 if (!(file->f_mode & FMODE_WRITE))
1230 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
1231
1232 /* fall through */
1233 case MAP_PRIVATE:
1234 if (!(file->f_mode & FMODE_READ))
1235 return -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -05001236 if (path_noexec(&file->f_path)) {
Linus Torvalds80c56062006-10-15 14:09:55 -07001237 if (vm_flags & VM_EXEC)
1238 return -EPERM;
1239 vm_flags &= ~VM_MAYEXEC;
1240 }
Linus Torvalds80c56062006-10-15 14:09:55 -07001241
Al Viro72c2d532013-09-22 16:27:52 -04001242 if (!file->f_op->mmap)
Linus Torvalds80c56062006-10-15 14:09:55 -07001243 return -ENODEV;
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001244 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1245 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 break;
1247
1248 default:
1249 return -EINVAL;
1250 }
1251 } else {
1252 switch (flags & MAP_TYPE) {
1253 case MAP_SHARED:
Oleg Nesterovb2c56e42013-09-11 14:20:18 -07001254 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1255 return -EINVAL;
Tejun Heoce363942008-09-03 16:09:47 +02001256 /*
1257 * Ignore pgoff.
1258 */
1259 pgoff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 vm_flags |= VM_SHARED | VM_MAYSHARE;
1261 break;
1262 case MAP_PRIVATE:
1263 /*
1264 * Set pgoff according to addr for anon_vma.
1265 */
1266 pgoff = addr >> PAGE_SHIFT;
1267 break;
1268 default:
1269 return -EINVAL;
1270 }
1271 }
1272
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001273 /*
1274 * Set 'VM_NORESERVE' if we should not account for the
1275 * memory use of this mapping.
1276 */
1277 if (flags & MAP_NORESERVE) {
1278 /* We honor MAP_NORESERVE if allowed to overcommit */
1279 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1280 vm_flags |= VM_NORESERVE;
1281
1282 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1283 if (file && is_file_hugepages(file))
1284 vm_flags |= VM_NORESERVE;
1285 }
1286
1287 addr = mmap_region(file, addr, len, vm_flags, pgoff);
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -07001288 if (!IS_ERR_VALUE(addr) &&
1289 ((vm_flags & VM_LOCKED) ||
1290 (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
Michel Lespinasse41badc12013-02-22 16:32:47 -08001291 *populate = len;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001292 return addr;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001293}
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001294
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001295SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1296 unsigned long, prot, unsigned long, flags,
1297 unsigned long, fd, unsigned long, pgoff)
1298{
1299 struct file *file = NULL;
Chen Gang1e3ee142015-11-05 18:48:35 -08001300 unsigned long retval;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001301
1302 if (!(flags & MAP_ANONYMOUS)) {
Al Viro120a7952010-10-30 02:54:44 -04001303 audit_mmap_fd(fd, flags);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001304 file = fget(fd);
1305 if (!file)
Chen Gang1e3ee142015-11-05 18:48:35 -08001306 return -EBADF;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001307 if (is_file_hugepages(file))
1308 len = ALIGN(len, huge_page_size(hstate_file(file)));
Jörn Engel493af572013-07-08 16:00:26 -07001309 retval = -EINVAL;
1310 if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
1311 goto out_fput;
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001312 } else if (flags & MAP_HUGETLB) {
1313 struct user_struct *user = NULL;
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001314 struct hstate *hs;
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001315
Andrew Mortonc103a4d2013-07-08 16:01:08 -07001316 hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & SHM_HUGE_MASK);
Li Zefan091d0d52013-05-09 15:08:15 +08001317 if (!hs)
1318 return -EINVAL;
1319
1320 len = ALIGN(len, huge_page_size(hs));
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001321 /*
1322 * VM_NORESERVE is used because the reservations will be
1323 * taken when vm_ops->mmap() is called
1324 * A dummy user value is used because we are not locking
1325 * memory so no accounting is necessary
1326 */
Naoya Horiguchiaf73e4d2013-05-07 16:18:13 -07001327 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
Andi Kleen42d73952012-12-11 16:01:34 -08001328 VM_NORESERVE,
1329 &user, HUGETLB_ANONHUGE_INODE,
1330 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001331 if (IS_ERR(file))
1332 return PTR_ERR(file);
1333 }
1334
1335 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1336
Michal Hocko9fbeb5a2016-05-23 16:25:30 -07001337 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
Jörn Engel493af572013-07-08 16:00:26 -07001338out_fput:
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001339 if (file)
1340 fput(file);
Hugh Dickins66f0dc42009-12-30 20:17:34 +00001341 return retval;
1342}
1343
Christoph Hellwiga4679372010-03-10 15:21:15 -08001344#ifdef __ARCH_WANT_SYS_OLD_MMAP
1345struct mmap_arg_struct {
1346 unsigned long addr;
1347 unsigned long len;
1348 unsigned long prot;
1349 unsigned long flags;
1350 unsigned long fd;
1351 unsigned long offset;
1352};
1353
1354SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1355{
1356 struct mmap_arg_struct a;
1357
1358 if (copy_from_user(&a, arg, sizeof(a)))
1359 return -EFAULT;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001360 if (offset_in_page(a.offset))
Christoph Hellwiga4679372010-03-10 15:21:15 -08001361 return -EINVAL;
1362
1363 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1364 a.offset >> PAGE_SHIFT);
1365}
1366#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1367
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001368/*
1369 * Some shared mappigns will want the pages marked read-only
1370 * to track write events. If so, we'll downgrade vm_page_prot
1371 * to the private version (using protection_map[] without the
1372 * VM_SHARED bit).
1373 */
1374int vma_wants_writenotify(struct vm_area_struct *vma)
1375{
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001376 vm_flags_t vm_flags = vma->vm_flags;
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001377 const struct vm_operations_struct *vm_ops = vma->vm_ops;
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001378
1379 /* If it was private or non-writable, the write bit is already clear */
1380 if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED)))
1381 return 0;
1382
1383 /* The backer wishes to know when pages are first written to? */
Kirill A. Shutemov8a044462015-09-22 14:59:12 -07001384 if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001385 return 1;
1386
Peter Feiner64e45502014-10-13 15:55:46 -07001387 /* The open routine did something to the protections that pgprot_modify
1388 * won't preserve? */
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001389 if (pgprot_val(vma->vm_page_prot) !=
Peter Feiner64e45502014-10-13 15:55:46 -07001390 pgprot_val(vm_pgprot_modify(vma->vm_page_prot, vm_flags)))
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001391 return 0;
1392
Peter Feiner64e45502014-10-13 15:55:46 -07001393 /* Do we need to track softdirty? */
1394 if (IS_ENABLED(CONFIG_MEM_SOFT_DIRTY) && !(vm_flags & VM_SOFTDIRTY))
1395 return 1;
1396
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001397 /* Specialty mapping? */
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001398 if (vm_flags & VM_PFNMAP)
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04001399 return 0;
1400
1401 /* Can the mapping track the dirty pages? */
1402 return vma->vm_file && vma->vm_file->f_mapping &&
1403 mapping_cap_account_dirty(vma->vm_file->f_mapping);
1404}
1405
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001406/*
1407 * We account for memory if it's a private writeable mapping,
Mel Gorman5a6fe122009-02-10 14:02:27 +00001408 * not hugepages and VM_NORESERVE wasn't set.
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001409 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09001410static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001411{
Mel Gorman5a6fe122009-02-10 14:02:27 +00001412 /*
1413 * hugetlb has its own accounting separate from the core VM
1414 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1415 */
1416 if (file && is_file_hugepages(file))
1417 return 0;
1418
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001419 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1420}
1421
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001422unsigned long mmap_region(struct file *file, unsigned long addr,
Michel Lespinassec22c0d62013-02-22 16:32:43 -08001423 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff)
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001424{
1425 struct mm_struct *mm = current->mm;
1426 struct vm_area_struct *vma, *prev;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001427 int error;
1428 struct rb_node **rb_link, *rb_parent;
1429 unsigned long charged = 0;
Miklos Szeredi0165ab42007-07-15 23:38:26 -07001430
Cyril Hrubise8420a82013-04-29 15:08:33 -07001431 /* Check against address space limit. */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001432 if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) {
Cyril Hrubise8420a82013-04-29 15:08:33 -07001433 unsigned long nr_pages;
1434
1435 /*
1436 * MAP_FIXED may remove pages of mappings that intersects with
1437 * requested mapping. Account for the pages it would unmap.
1438 */
Cyril Hrubise8420a82013-04-29 15:08:33 -07001439 nr_pages = count_vma_pages_range(mm, addr, addr + len);
1440
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001441 if (!may_expand_vm(mm, vm_flags,
1442 (len >> PAGE_SHIFT) - nr_pages))
Cyril Hrubise8420a82013-04-29 15:08:33 -07001443 return -ENOMEM;
1444 }
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* Clear old maps */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07001447 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
1448 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (do_munmap(mm, addr, len))
1450 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001453 /*
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001454 * Private writable mapping: check memory availability
1455 */
Mel Gorman5a6fe122009-02-10 14:02:27 +00001456 if (accountable_mapping(file, vm_flags)) {
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001457 charged = len >> PAGE_SHIFT;
Al Viro191c5422012-02-13 03:58:52 +00001458 if (security_vm_enough_memory_mm(mm, charged))
Linus Torvaldsfc8744a2009-01-31 15:08:56 -08001459 return -ENOMEM;
1460 vm_flags |= VM_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
1463 /*
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001464 * Can we just expand an old mapping?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 */
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07001466 vma = vma_merge(mm, prev, addr, addr + len, vm_flags,
1467 NULL, file, pgoff, NULL, NULL_VM_UFFD_CTX);
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001468 if (vma)
1469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /*
1472 * Determine the object being mapped and call the appropriate
1473 * specific mapper. the address has already been validated, but
1474 * not unmapped, but the maps are removed from the list.
1475 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08001476 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (!vma) {
1478 error = -ENOMEM;
1479 goto unacct_error;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 vma->vm_mm = mm;
1483 vma->vm_start = addr;
1484 vma->vm_end = addr + len;
1485 vma->vm_flags = vm_flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07001486 vma->vm_page_prot = vm_get_page_prot(vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 vma->vm_pgoff = pgoff;
Rik van Riel5beb4932010-03-05 13:42:07 -08001488 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (vm_flags & VM_DENYWRITE) {
1492 error = deny_write_access(file);
1493 if (error)
1494 goto free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001496 if (vm_flags & VM_SHARED) {
1497 error = mapping_map_writable(file->f_mapping);
1498 if (error)
1499 goto allow_write_and_free_vma;
1500 }
1501
1502 /* ->mmap() can change vma->vm_file, but must guarantee that
1503 * vma_link() below can deny write-access if VM_DENYWRITE is set
1504 * and map writably if VM_SHARED is set. This usually means the
1505 * new file must not have been exposed to user-space, yet.
1506 */
Al Virocb0942b2012-08-27 14:48:26 -04001507 vma->vm_file = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 error = file->f_op->mmap(file, vma);
1509 if (error)
1510 goto unmap_and_free_vma;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001511
1512 /* Can addr have changed??
1513 *
1514 * Answer: Yes, several device drivers can do it in their
1515 * f_op->mmap method. -DaveM
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001516 * Bug: If addr is changed, prev, rb_link, rb_parent should
1517 * be updated for vma_link()
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001518 */
Joonsoo Kim2897b4d2012-12-12 13:51:53 -08001519 WARN_ON_ONCE(addr != vma->vm_start);
1520
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001521 addr = vma->vm_start;
Huang Shijief8dbf0a72009-09-21 17:03:41 -07001522 vm_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 } else if (vm_flags & VM_SHARED) {
1524 error = shmem_zero_setup(vma);
1525 if (error)
1526 goto free_vma;
1527 }
1528
Linus Torvaldsde33c8d2009-01-29 17:46:42 -08001529 vma_link(mm, vma, prev, rb_link, rb_parent);
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001530 /* Once vma denies write, undo our temporary denial count */
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001531 if (file) {
1532 if (vm_flags & VM_SHARED)
1533 mapping_unmap_writable(file->f_mapping);
1534 if (vm_flags & VM_DENYWRITE)
1535 allow_write_access(file);
1536 }
Oleg Nesterove8686772013-09-11 14:20:20 -07001537 file = vma->vm_file;
Oleg Nesterov4d3d5b42008-04-28 02:12:10 -07001538out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001539 perf_event_mmap(vma);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02001540
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001541 vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (vm_flags & VM_LOCKED) {
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001543 if (!((vm_flags & VM_SPECIAL) || is_vm_hugetlb_page(vma) ||
1544 vma == get_gate_vma(current->mm)))
KOSAKI Motohiro06f9d8c2010-03-05 13:41:43 -08001545 mm->locked_vm += (len >> PAGE_SHIFT);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001546 else
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001547 vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -08001548 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301549
Oleg Nesterovc7a3a882012-08-19 19:10:42 +02001550 if (file)
1551 uprobe_mmap(vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301552
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07001553 /*
1554 * New (or expanded) vma always get soft dirty status.
1555 * Otherwise user-space soft-dirty page tracker won't
1556 * be able to distinguish situation when vma area unmapped,
1557 * then new mapped in-place (which must be aimed as
1558 * a completely new data area).
1559 */
1560 vma->vm_flags |= VM_SOFTDIRTY;
1561
Peter Feiner64e45502014-10-13 15:55:46 -07001562 vma_set_page_prot(vma);
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return addr;
1565
1566unmap_and_free_vma:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 vma->vm_file = NULL;
1568 fput(file);
1569
1570 /* Undo any partial mapping done by a device driver. */
Hugh Dickinse0da3822005-04-19 13:29:15 -07001571 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
1572 charged = 0;
David Herrmann4bb5f5d2014-08-08 14:25:25 -07001573 if (vm_flags & VM_SHARED)
1574 mapping_unmap_writable(file->f_mapping);
1575allow_write_and_free_vma:
1576 if (vm_flags & VM_DENYWRITE)
1577 allow_write_access(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578free_vma:
1579 kmem_cache_free(vm_area_cachep, vma);
1580unacct_error:
1581 if (charged)
1582 vm_unacct_memory(charged);
1583 return error;
1584}
1585
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001586unsigned long unmapped_area(struct vm_unmapped_area_info *info)
1587{
1588 /*
1589 * We implement the search by looking for an rbtree node that
1590 * immediately follows a suitable gap. That is,
1591 * - gap_start = vma->vm_prev->vm_end <= info->high_limit - length;
1592 * - gap_end = vma->vm_start >= info->low_limit + length;
1593 * - gap_end - gap_start >= length
1594 */
1595
1596 struct mm_struct *mm = current->mm;
1597 struct vm_area_struct *vma;
1598 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1599
1600 /* Adjust search length to account for worst case alignment overhead */
1601 length = info->length + info->align_mask;
1602 if (length < info->length)
1603 return -ENOMEM;
1604
1605 /* Adjust search limits by the desired length */
1606 if (info->high_limit < length)
1607 return -ENOMEM;
1608 high_limit = info->high_limit - length;
1609
1610 if (info->low_limit > high_limit)
1611 return -ENOMEM;
1612 low_limit = info->low_limit + length;
1613
1614 /* Check if rbtree root looks promising */
1615 if (RB_EMPTY_ROOT(&mm->mm_rb))
1616 goto check_highest;
1617 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1618 if (vma->rb_subtree_gap < length)
1619 goto check_highest;
1620
1621 while (true) {
1622 /* Visit left subtree if it looks promising */
1623 gap_end = vma->vm_start;
1624 if (gap_end >= low_limit && vma->vm_rb.rb_left) {
1625 struct vm_area_struct *left =
1626 rb_entry(vma->vm_rb.rb_left,
1627 struct vm_area_struct, vm_rb);
1628 if (left->rb_subtree_gap >= length) {
1629 vma = left;
1630 continue;
1631 }
1632 }
1633
1634 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1635check_current:
1636 /* Check if current node has a suitable gap */
1637 if (gap_start > high_limit)
1638 return -ENOMEM;
1639 if (gap_end >= low_limit && gap_end - gap_start >= length)
1640 goto found;
1641
1642 /* Visit right subtree if it looks promising */
1643 if (vma->vm_rb.rb_right) {
1644 struct vm_area_struct *right =
1645 rb_entry(vma->vm_rb.rb_right,
1646 struct vm_area_struct, vm_rb);
1647 if (right->rb_subtree_gap >= length) {
1648 vma = right;
1649 continue;
1650 }
1651 }
1652
1653 /* Go back up the rbtree to find next candidate node */
1654 while (true) {
1655 struct rb_node *prev = &vma->vm_rb;
1656 if (!rb_parent(prev))
1657 goto check_highest;
1658 vma = rb_entry(rb_parent(prev),
1659 struct vm_area_struct, vm_rb);
1660 if (prev == vma->vm_rb.rb_left) {
1661 gap_start = vma->vm_prev->vm_end;
1662 gap_end = vma->vm_start;
1663 goto check_current;
1664 }
1665 }
1666 }
1667
1668check_highest:
1669 /* Check highest gap, which does not precede any rbtree node */
1670 gap_start = mm->highest_vm_end;
1671 gap_end = ULONG_MAX; /* Only for VM_BUG_ON below */
1672 if (gap_start > high_limit)
1673 return -ENOMEM;
1674
1675found:
1676 /* We found a suitable gap. Clip it with the original low_limit. */
1677 if (gap_start < info->low_limit)
1678 gap_start = info->low_limit;
1679
1680 /* Adjust gap address to the desired alignment */
1681 gap_start += (info->align_offset - gap_start) & info->align_mask;
1682
1683 VM_BUG_ON(gap_start + info->length > info->high_limit);
1684 VM_BUG_ON(gap_start + info->length > gap_end);
1685 return gap_start;
1686}
1687
1688unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
1689{
1690 struct mm_struct *mm = current->mm;
1691 struct vm_area_struct *vma;
1692 unsigned long length, low_limit, high_limit, gap_start, gap_end;
1693
1694 /* Adjust search length to account for worst case alignment overhead */
1695 length = info->length + info->align_mask;
1696 if (length < info->length)
1697 return -ENOMEM;
1698
1699 /*
1700 * Adjust search limits by the desired length.
1701 * See implementation comment at top of unmapped_area().
1702 */
1703 gap_end = info->high_limit;
1704 if (gap_end < length)
1705 return -ENOMEM;
1706 high_limit = gap_end - length;
1707
1708 if (info->low_limit > high_limit)
1709 return -ENOMEM;
1710 low_limit = info->low_limit + length;
1711
1712 /* Check highest gap, which does not precede any rbtree node */
1713 gap_start = mm->highest_vm_end;
1714 if (gap_start <= high_limit)
1715 goto found_highest;
1716
1717 /* Check if rbtree root looks promising */
1718 if (RB_EMPTY_ROOT(&mm->mm_rb))
1719 return -ENOMEM;
1720 vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
1721 if (vma->rb_subtree_gap < length)
1722 return -ENOMEM;
1723
1724 while (true) {
1725 /* Visit right subtree if it looks promising */
1726 gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
1727 if (gap_start <= high_limit && vma->vm_rb.rb_right) {
1728 struct vm_area_struct *right =
1729 rb_entry(vma->vm_rb.rb_right,
1730 struct vm_area_struct, vm_rb);
1731 if (right->rb_subtree_gap >= length) {
1732 vma = right;
1733 continue;
1734 }
1735 }
1736
1737check_current:
1738 /* Check if current node has a suitable gap */
1739 gap_end = vma->vm_start;
1740 if (gap_end < low_limit)
1741 return -ENOMEM;
1742 if (gap_start <= high_limit && gap_end - gap_start >= length)
1743 goto found;
1744
1745 /* Visit left subtree if it looks promising */
1746 if (vma->vm_rb.rb_left) {
1747 struct vm_area_struct *left =
1748 rb_entry(vma->vm_rb.rb_left,
1749 struct vm_area_struct, vm_rb);
1750 if (left->rb_subtree_gap >= length) {
1751 vma = left;
1752 continue;
1753 }
1754 }
1755
1756 /* Go back up the rbtree to find next candidate node */
1757 while (true) {
1758 struct rb_node *prev = &vma->vm_rb;
1759 if (!rb_parent(prev))
1760 return -ENOMEM;
1761 vma = rb_entry(rb_parent(prev),
1762 struct vm_area_struct, vm_rb);
1763 if (prev == vma->vm_rb.rb_right) {
1764 gap_start = vma->vm_prev ?
1765 vma->vm_prev->vm_end : 0;
1766 goto check_current;
1767 }
1768 }
1769 }
1770
1771found:
1772 /* We found a suitable gap. Clip it with the original high_limit. */
1773 if (gap_end > info->high_limit)
1774 gap_end = info->high_limit;
1775
1776found_highest:
1777 /* Compute highest gap address at the desired alignment */
1778 gap_end -= info->length;
1779 gap_end -= (gap_end - info->align_offset) & info->align_mask;
1780
1781 VM_BUG_ON(gap_end < info->low_limit);
1782 VM_BUG_ON(gap_end < gap_start);
1783 return gap_end;
1784}
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786/* Get an address range which is currently unmapped.
1787 * For shmat() with addr=0.
1788 *
1789 * Ugly calling convention alert:
1790 * Return value with the low bits set means error value,
1791 * ie
1792 * if (ret & ~PAGE_MASK)
1793 * error = ret;
1794 *
1795 * This function "knows" that -ENOMEM has the bits set.
1796 */
1797#ifndef HAVE_ARCH_UNMAPPED_AREA
1798unsigned long
1799arch_get_unmapped_area(struct file *filp, unsigned long addr,
1800 unsigned long len, unsigned long pgoff, unsigned long flags)
1801{
1802 struct mm_struct *mm = current->mm;
1803 struct vm_area_struct *vma;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001804 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001806 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return -ENOMEM;
1808
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001809 if (flags & MAP_FIXED)
1810 return addr;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (addr) {
1813 addr = PAGE_ALIGN(addr);
1814 vma = find_vma(mm, addr);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001815 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 (!vma || addr + len <= vma->vm_start))
1817 return addr;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001820 info.flags = 0;
1821 info.length = len;
Heiko Carstens4e99b022013-11-12 15:07:54 -08001822 info.low_limit = mm->mmap_base;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001823 info.high_limit = TASK_SIZE;
1824 info.align_mask = 0;
1825 return vm_unmapped_area(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
vishnu.pscc71aba2014-10-09 15:26:29 -07001827#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829/*
1830 * This mmap-allocator allocates new areas top-down from below the
1831 * stack's low limit (the base):
1832 */
1833#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1834unsigned long
1835arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
1836 const unsigned long len, const unsigned long pgoff,
1837 const unsigned long flags)
1838{
1839 struct vm_area_struct *vma;
1840 struct mm_struct *mm = current->mm;
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001841 unsigned long addr = addr0;
1842 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* requested length too big for entire address space */
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001845 if (len > TASK_SIZE - mmap_min_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -ENOMEM;
1847
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001848 if (flags & MAP_FIXED)
1849 return addr;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /* requesting a specific address */
1852 if (addr) {
1853 addr = PAGE_ALIGN(addr);
1854 vma = find_vma(mm, addr);
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001855 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 (!vma || addr + len <= vma->vm_start))
1857 return addr;
1858 }
1859
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001860 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
1861 info.length = len;
Akira Takeuchi2afc7452013-11-12 15:08:21 -08001862 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001863 info.high_limit = mm->mmap_base;
1864 info.align_mask = 0;
1865 addr = vm_unmapped_area(&info);
Xiao Guangrongb716ad92012-03-21 16:33:56 -07001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /*
1868 * A failed mmap() very likely causes application failure,
1869 * so fall back to the bottom-up function here. This scenario
1870 * can happen with large stack limits and large mmap()
1871 * allocations.
1872 */
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001873 if (offset_in_page(addr)) {
Michel Lespinassedb4fbfb2012-12-11 16:01:49 -08001874 VM_BUG_ON(addr != -ENOMEM);
1875 info.flags = 0;
1876 info.low_limit = TASK_UNMAPPED_BASE;
1877 info.high_limit = TASK_SIZE;
1878 addr = vm_unmapped_area(&info);
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 return addr;
1882}
1883#endif
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885unsigned long
1886get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1887 unsigned long pgoff, unsigned long flags)
1888{
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001889 unsigned long (*get_area)(struct file *, unsigned long,
1890 unsigned long, unsigned long, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Al Viro9206de92009-12-03 15:23:11 -05001892 unsigned long error = arch_mmap_check(addr, len, flags);
1893 if (error)
1894 return error;
1895
1896 /* Careful about overflows.. */
1897 if (len > TASK_SIZE)
1898 return -ENOMEM;
1899
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001900 get_area = current->mm->get_unmapped_area;
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07001901 if (file) {
1902 if (file->f_op->get_unmapped_area)
1903 get_area = file->f_op->get_unmapped_area;
1904 } else if (flags & MAP_SHARED) {
1905 /*
1906 * mmap_region() will call shmem_zero_setup() to create a file,
1907 * so use shmem's get_unmapped_area in case it can be huge.
1908 * do_mmap_pgoff() will clear pgoff, so match alignment.
1909 */
1910 pgoff = 0;
1911 get_area = shmem_get_unmapped_area;
1912 }
1913
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001914 addr = get_area(file, addr, len, pgoff, flags);
1915 if (IS_ERR_VALUE(addr))
1916 return addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001918 if (addr > TASK_SIZE - len)
1919 return -ENOMEM;
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08001920 if (offset_in_page(addr))
Linus Torvalds07ab67c2005-05-19 22:43:37 -07001921 return -EINVAL;
Benjamin Herrenschmidt06abdfb2007-05-06 14:50:13 -07001922
Al Viro9ac4ed42012-05-30 17:13:15 -04001923 error = security_mmap_addr(addr);
1924 return error ? error : addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927EXPORT_SYMBOL(get_unmapped_area);
1928
1929/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
ZhenwenXu48aae422009-01-06 14:40:21 -08001930struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001932 struct rb_node *rb_node;
1933 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001935 /* Check the cache first. */
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001936 vma = vmacache_find(mm, addr);
1937 if (likely(vma))
1938 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001940 rb_node = mm->mm_rb.rb_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001942 while (rb_node) {
1943 struct vm_area_struct *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001945 tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);
Rajman Mekaco841e31e2012-05-29 15:06:21 -07001946
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001947 if (tmp->vm_end > addr) {
1948 vma = tmp;
1949 if (tmp->vm_start <= addr)
1950 break;
1951 rb_node = rb_node->rb_left;
1952 } else
1953 rb_node = rb_node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07001955
1956 if (vma)
1957 vmacache_update(addr, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return vma;
1959}
1960
1961EXPORT_SYMBOL(find_vma);
1962
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001963/*
1964 * Same as find_vma, but also return a pointer to the previous VMA in *pprev.
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001965 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966struct vm_area_struct *
1967find_vma_prev(struct mm_struct *mm, unsigned long addr,
1968 struct vm_area_struct **pprev)
1969{
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001970 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001972 vma = find_vma(mm, addr);
Mikulas Patocka83cd9042012-03-04 19:52:03 -05001973 if (vma) {
1974 *pprev = vma->vm_prev;
1975 } else {
1976 struct rb_node *rb_node = mm->mm_rb.rb_node;
1977 *pprev = NULL;
1978 while (rb_node) {
1979 *pprev = rb_entry(rb_node, struct vm_area_struct, vm_rb);
1980 rb_node = rb_node->rb_right;
1981 }
1982 }
KOSAKI Motohiro6bd48372012-01-10 15:08:07 -08001983 return vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
1986/*
1987 * Verify that the stack growth is acceptable and
1988 * update accounting. This is shared with both the
1989 * grow-up and grow-down cases.
1990 */
ZhenwenXu48aae422009-01-06 14:40:21 -08001991static int acct_stack_growth(struct vm_area_struct *vma, unsigned long size, unsigned long grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct mm_struct *mm = vma->vm_mm;
1994 struct rlimit *rlim = current->signal->rlim;
Linus Torvalds690eac52015-01-11 11:33:57 -08001995 unsigned long new_start, actual_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* address space limit tests */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08001998 if (!may_expand_vm(mm, vma->vm_flags, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return -ENOMEM;
2000
2001 /* Stack limit test */
Linus Torvalds690eac52015-01-11 11:33:57 -08002002 actual_size = size;
2003 if (size && (vma->vm_flags & (VM_GROWSUP | VM_GROWSDOWN)))
2004 actual_size -= PAGE_SIZE;
Jason Low4db0c3c2015-04-15 16:14:08 -07002005 if (actual_size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return -ENOMEM;
2007
2008 /* mlock limit tests */
2009 if (vma->vm_flags & VM_LOCKED) {
2010 unsigned long locked;
2011 unsigned long limit;
2012 locked = mm->locked_vm + grow;
Jason Low4db0c3c2015-04-15 16:14:08 -07002013 limit = READ_ONCE(rlim[RLIMIT_MEMLOCK].rlim_cur);
Jiri Slaby59e99e52010-03-05 13:41:44 -08002014 limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (locked > limit && !capable(CAP_IPC_LOCK))
2016 return -ENOMEM;
2017 }
2018
Adam Litke0d59a012007-01-30 14:35:39 -08002019 /* Check to ensure the stack will not grow into a hugetlb-only region */
2020 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
2021 vma->vm_end - size;
2022 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
2023 return -EFAULT;
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 /*
2026 * Overcommit.. This must be the final test, as it will
2027 * update security statistics.
2028 */
Hugh Dickins05fa1992009-04-16 21:58:12 +01002029 if (security_vm_enough_memory_mm(mm, grow))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return -ENOMEM;
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return 0;
2033}
2034
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002035#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/*
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002037 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
2038 * vma is the last one with address > vma->vm_end. Have to extend vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002040int expand_upwards(struct vm_area_struct *vma, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Oleg Nesterov09357812015-11-05 18:48:17 -08002042 struct mm_struct *mm = vma->vm_mm;
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002043 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 if (!(vma->vm_flags & VM_GROWSUP))
2046 return -EFAULT;
2047
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002048 /* Guard against wrapping around to address 0. */
2049 if (address < PAGE_ALIGN(address+4))
2050 address = PAGE_ALIGN(address+4);
2051 else
2052 return -ENOMEM;
2053
2054 /* We must make sure the anon_vma is allocated. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (unlikely(anon_vma_prepare(vma)))
2056 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 /*
2059 * vma->vm_start/vm_end cannot change under us because the caller
2060 * is required to hold the mmap_sem in read mode. We need the
2061 * anon_vma lock to serialize against concurrent expand_stacks.
2062 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002063 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 /* Somebody else might have raced and expanded it already */
2066 if (address > vma->vm_end) {
2067 unsigned long size, grow;
2068
2069 size = address - vma->vm_start;
2070 grow = (address - vma->vm_end) >> PAGE_SHIFT;
2071
Hugh Dickins42c36f62011-05-09 17:44:42 -07002072 error = -ENOMEM;
2073 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
2074 error = acct_stack_growth(vma, size, grow);
2075 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002076 /*
2077 * vma_gap_update() doesn't support concurrent
2078 * updates, but we only hold a shared mmap_sem
2079 * lock here, so we need to protect against
2080 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002081 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002082 * we don't guarantee that all growable vmas
2083 * in a mm share the same root anon vma.
2084 * So, we reuse mm->page_table_lock to guard
2085 * against concurrent vma expansions.
2086 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002087 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002088 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002089 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002090 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002091 anon_vma_interval_tree_pre_update_vma(vma);
Hugh Dickins42c36f62011-05-09 17:44:42 -07002092 vma->vm_end = address;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002093 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002094 if (vma->vm_next)
2095 vma_gap_update(vma->vm_next);
2096 else
Oleg Nesterov09357812015-11-05 18:48:17 -08002097 mm->highest_vm_end = address;
2098 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002099
Hugh Dickins42c36f62011-05-09 17:44:42 -07002100 perf_event_mmap(vma);
2101 }
Eric B Munson3af9e852010-05-18 15:30:49 +01002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002104 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002105 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002106 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return error;
2108}
Hugh Dickins46dea3d2005-10-29 18:16:20 -07002109#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111/*
2112 * vma is the first one with address < vma->vm_start. Have to extend vma.
2113 */
Michal Hockod05f3162011-05-24 17:11:44 -07002114int expand_downwards(struct vm_area_struct *vma,
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002115 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Oleg Nesterov09357812015-11-05 18:48:17 -08002117 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int error;
2119
Eric Paris88694772007-11-26 18:47:26 -05002120 address &= PAGE_MASK;
Al Viroe5467852012-05-30 13:30:51 -04002121 error = security_mmap_addr(address);
Eric Paris88694772007-11-26 18:47:26 -05002122 if (error)
2123 return error;
2124
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002125 /* We must make sure the anon_vma is allocated. */
2126 if (unlikely(anon_vma_prepare(vma)))
2127 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 /*
2130 * vma->vm_start/vm_end cannot change under us because the caller
2131 * is required to hold the mmap_sem in read mode. We need the
2132 * anon_vma lock to serialize against concurrent expand_stacks.
2133 */
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002134 anon_vma_lock_write(vma->anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /* Somebody else might have raced and expanded it already */
2137 if (address < vma->vm_start) {
2138 unsigned long size, grow;
2139
2140 size = vma->vm_end - address;
2141 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2142
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002143 error = -ENOMEM;
2144 if (grow <= vma->vm_pgoff) {
2145 error = acct_stack_growth(vma, size, grow);
2146 if (!error) {
Michel Lespinasse41289972012-12-12 13:52:25 -08002147 /*
2148 * vma_gap_update() doesn't support concurrent
2149 * updates, but we only hold a shared mmap_sem
2150 * lock here, so we need to protect against
2151 * concurrent vma expansions.
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002152 * anon_vma_lock_write() doesn't help here, as
Michel Lespinasse41289972012-12-12 13:52:25 -08002153 * we don't guarantee that all growable vmas
2154 * in a mm share the same root anon vma.
2155 * So, we reuse mm->page_table_lock to guard
2156 * against concurrent vma expansions.
2157 */
Oleg Nesterov09357812015-11-05 18:48:17 -08002158 spin_lock(&mm->page_table_lock);
Oleg Nesterov87e88272015-11-05 18:48:14 -08002159 if (vma->vm_flags & VM_LOCKED)
Oleg Nesterov09357812015-11-05 18:48:17 -08002160 mm->locked_vm += grow;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002161 vm_stat_account(mm, vma->vm_flags, grow);
Michel Lespinassebf181b92012-10-08 16:31:39 -07002162 anon_vma_interval_tree_pre_update_vma(vma);
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002163 vma->vm_start = address;
2164 vma->vm_pgoff -= grow;
Michel Lespinassebf181b92012-10-08 16:31:39 -07002165 anon_vma_interval_tree_post_update_vma(vma);
Michel Lespinassed3737182012-12-11 16:01:38 -08002166 vma_gap_update(vma);
Oleg Nesterov09357812015-11-05 18:48:17 -08002167 spin_unlock(&mm->page_table_lock);
Michel Lespinasse41289972012-12-12 13:52:25 -08002168
Linus Torvaldsa626ca62011-04-13 08:07:28 -07002169 perf_event_mmap(vma);
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 }
Konstantin Khlebnikov12352d32016-02-05 15:36:50 -08002173 anon_vma_unlock_write(vma->anon_vma);
David Rientjes6d50e602014-10-29 14:50:31 -07002174 khugepaged_enter_vma_merge(vma, vma->vm_flags);
Oleg Nesterov09357812015-11-05 18:48:17 -08002175 validate_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return error;
2177}
2178
Linus Torvalds09884962013-02-27 08:36:04 -08002179/*
2180 * Note how expand_stack() refuses to expand the stack all the way to
2181 * abut the next virtual mapping, *unless* that mapping itself is also
2182 * a stack mapping. We want to leave room for a guard page, after all
2183 * (the guard page itself is not added here, that is done by the
2184 * actual page faulting logic)
2185 *
2186 * This matches the behavior of the guard page logic (see mm/memory.c:
2187 * check_stack_guard_page()), which only allows the guard page to be
2188 * removed under these circumstances.
2189 */
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002190#ifdef CONFIG_STACK_GROWSUP
2191int expand_stack(struct vm_area_struct *vma, unsigned long address)
2192{
Linus Torvalds09884962013-02-27 08:36:04 -08002193 struct vm_area_struct *next;
2194
2195 address &= PAGE_MASK;
2196 next = vma->vm_next;
2197 if (next && next->vm_start == address + PAGE_SIZE) {
2198 if (!(next->vm_flags & VM_GROWSUP))
2199 return -ENOMEM;
2200 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002201 return expand_upwards(vma, address);
2202}
2203
2204struct vm_area_struct *
2205find_extend_vma(struct mm_struct *mm, unsigned long addr)
2206{
2207 struct vm_area_struct *vma, *prev;
2208
2209 addr &= PAGE_MASK;
2210 vma = find_vma_prev(mm, addr, &prev);
2211 if (vma && (vma->vm_start <= addr))
2212 return vma;
Denys Vlasenko1c127182008-11-12 01:24:41 +01002213 if (!prev || expand_stack(prev, addr))
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002214 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002215 if (prev->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002216 populate_vma_page_range(prev, addr, prev->vm_end, NULL);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002217 return prev;
2218}
2219#else
2220int expand_stack(struct vm_area_struct *vma, unsigned long address)
2221{
Linus Torvalds09884962013-02-27 08:36:04 -08002222 struct vm_area_struct *prev;
2223
2224 address &= PAGE_MASK;
2225 prev = vma->vm_prev;
2226 if (prev && prev->vm_end == address) {
2227 if (!(prev->vm_flags & VM_GROWSDOWN))
2228 return -ENOMEM;
2229 }
Ollie Wildb6a2fea2007-07-19 01:48:16 -07002230 return expand_downwards(vma, address);
2231}
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233struct vm_area_struct *
vishnu.pscc71aba2014-10-09 15:26:29 -07002234find_extend_vma(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
vishnu.pscc71aba2014-10-09 15:26:29 -07002236 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 unsigned long start;
2238
2239 addr &= PAGE_MASK;
vishnu.pscc71aba2014-10-09 15:26:29 -07002240 vma = find_vma(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (!vma)
2242 return NULL;
2243 if (vma->vm_start <= addr)
2244 return vma;
2245 if (!(vma->vm_flags & VM_GROWSDOWN))
2246 return NULL;
2247 start = vma->vm_start;
2248 if (expand_stack(vma, addr))
2249 return NULL;
Michel Lespinassecea10a12013-02-22 16:32:44 -08002250 if (vma->vm_flags & VM_LOCKED)
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -07002251 populate_vma_page_range(vma, addr, start, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return vma;
2253}
2254#endif
2255
Jesse Barnese1d6d012014-12-12 16:55:27 -08002256EXPORT_SYMBOL_GPL(find_extend_vma);
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258/*
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002259 * Ok - we have the memory areas we should free on the vma list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 * so release them, and do the vma updates.
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002261 *
2262 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002264static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002266 unsigned long nr_accounted = 0;
2267
Hugh Dickins365e9c872005-10-29 18:16:18 -07002268 /* Update high watermark before we lower total_vm */
2269 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 do {
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002271 long nrpages = vma_pages(vma);
2272
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002273 if (vma->vm_flags & VM_ACCOUNT)
2274 nr_accounted += nrpages;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002275 vm_stat_account(mm, vma->vm_flags, -nrpages);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002276 vma = remove_vma(vma);
Hugh Dickins146425a2005-04-19 13:29:18 -07002277 } while (vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002278 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 validate_mm(mm);
2280}
2281
2282/*
2283 * Get rid of page table information in the indicated region.
2284 *
Paolo 'Blaisorblade' Giarrussof10df682005-09-21 09:55:37 -07002285 * Called with the mm semaphore held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 */
2287static void unmap_region(struct mm_struct *mm,
Hugh Dickinse0da3822005-04-19 13:29:15 -07002288 struct vm_area_struct *vma, struct vm_area_struct *prev,
2289 unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
vishnu.pscc71aba2014-10-09 15:26:29 -07002291 struct vm_area_struct *next = prev ? prev->vm_next : mm->mmap;
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002292 struct mmu_gather tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 lru_add_drain();
Linus Torvalds2b047252013-08-15 11:42:25 -07002295 tlb_gather_mmu(&tlb, mm, start, end);
Hugh Dickins365e9c872005-10-29 18:16:18 -07002296 update_hiwater_rss(mm);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002297 unmap_vmas(&tlb, vma, start, end);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002298 free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
Hugh Dickins6ee86302013-04-29 15:07:44 -07002299 next ? next->vm_start : USER_PGTABLES_CEILING);
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002300 tlb_finish_mmu(&tlb, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/*
2304 * Create a list of vma's touched by the unmap, removing them from the mm's
2305 * vma list as we go..
2306 */
2307static void
2308detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
2309 struct vm_area_struct *prev, unsigned long end)
2310{
2311 struct vm_area_struct **insertion_point;
2312 struct vm_area_struct *tail_vma = NULL;
2313
2314 insertion_point = (prev ? &prev->vm_next : &mm->mmap);
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002315 vma->vm_prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 do {
Michel Lespinassed3737182012-12-11 16:01:38 -08002317 vma_rb_erase(vma, &mm->mm_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 mm->map_count--;
2319 tail_vma = vma;
2320 vma = vma->vm_next;
2321 } while (vma && vma->vm_start < end);
2322 *insertion_point = vma;
Michel Lespinassed3737182012-12-11 16:01:38 -08002323 if (vma) {
Linus Torvalds297c5ee2010-08-20 16:24:55 -07002324 vma->vm_prev = prev;
Michel Lespinassed3737182012-12-11 16:01:38 -08002325 vma_gap_update(vma);
2326 } else
2327 mm->highest_vm_end = prev ? prev->vm_end : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 tail_vma->vm_next = NULL;
Davidlohr Bueso615d6e82014-04-07 15:37:25 -07002329
2330 /* Kill the cache */
2331 vmacache_invalidate(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334/*
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002335 * __split_vma() bypasses sysctl_max_map_count checking. We use this on the
2336 * munmap path where it doesn't make sense to fail.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 */
vishnu.pscc71aba2014-10-09 15:26:29 -07002338static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 unsigned long addr, int new_below)
2340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct vm_area_struct *new;
Chen Gange3975892015-09-08 15:03:38 -07002342 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Andi Kleena5516432008-07-23 21:27:41 -07002344 if (is_vm_hugetlb_page(vma) && (addr &
2345 ~(huge_page_mask(hstate_vma(vma)))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return -EINVAL;
2347
Christoph Lametere94b1762006-12-06 20:33:17 -08002348 new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (!new)
Chen Gange3975892015-09-08 15:03:38 -07002350 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 /* most fields are the same, copy all, and then fixup */
2353 *new = *vma;
2354
Rik van Riel5beb4932010-03-05 13:42:07 -08002355 INIT_LIST_HEAD(&new->anon_vma_chain);
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (new_below)
2358 new->vm_end = addr;
2359 else {
2360 new->vm_start = addr;
2361 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2362 }
2363
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002364 err = vma_dup_policy(vma, new);
2365 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002366 goto out_free_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Daniel Forrestc4ea95d2014-12-02 15:59:42 -08002368 err = anon_vma_clone(new, vma);
2369 if (err)
Rik van Riel5beb4932010-03-05 13:42:07 -08002370 goto out_free_mpol;
2371
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002372 if (new->vm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 get_file(new->vm_file);
2374
2375 if (new->vm_ops && new->vm_ops->open)
2376 new->vm_ops->open(new);
2377
2378 if (new_below)
Rik van Riel5beb4932010-03-05 13:42:07 -08002379 err = vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 ((addr - new->vm_start) >> PAGE_SHIFT), new);
2381 else
Rik van Riel5beb4932010-03-05 13:42:07 -08002382 err = vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Rik van Riel5beb4932010-03-05 13:42:07 -08002384 /* Success. */
2385 if (!err)
2386 return 0;
2387
2388 /* Clean everything up if vma_adjust failed. */
Rik van Riel58927532010-04-26 12:33:03 -04002389 if (new->vm_ops && new->vm_ops->close)
2390 new->vm_ops->close(new);
Konstantin Khlebnikove9714ac2012-10-08 16:28:54 -07002391 if (new->vm_file)
Rik van Riel5beb4932010-03-05 13:42:07 -08002392 fput(new->vm_file);
Andrea Arcangeli2aeadc32010-09-22 13:05:12 -07002393 unlink_anon_vmas(new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002394 out_free_mpol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002395 mpol_put(vma_policy(new));
Rik van Riel5beb4932010-03-05 13:42:07 -08002396 out_free_vma:
2397 kmem_cache_free(vm_area_cachep, new);
Rik van Riel5beb4932010-03-05 13:42:07 -08002398 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002401/*
2402 * Split a vma into two pieces at address 'addr', a new vma is allocated
2403 * either for the first part or the tail.
2404 */
2405int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
2406 unsigned long addr, int new_below)
2407{
2408 if (mm->map_count >= sysctl_max_map_count)
2409 return -ENOMEM;
2410
2411 return __split_vma(mm, vma, addr, new_below);
2412}
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414/* Munmap is split into 2 main parts -- this part which finds
2415 * what needs doing, and the areas themselves, which do the
2416 * work. This now handles partial unmappings.
2417 * Jeremy Fitzhardinge <jeremy@goop.org>
2418 */
2419int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
2420{
2421 unsigned long end;
Hugh Dickins146425a2005-04-19 13:29:18 -07002422 struct vm_area_struct *vma, *prev, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002424 if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return -EINVAL;
2426
vishnu.pscc71aba2014-10-09 15:26:29 -07002427 len = PAGE_ALIGN(len);
2428 if (len == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return -EINVAL;
2430
2431 /* Find the first overlapping VMA */
Linus Torvalds9be34c92011-06-16 00:35:09 -07002432 vma = find_vma(mm, start);
Hugh Dickins146425a2005-04-19 13:29:18 -07002433 if (!vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return 0;
Linus Torvalds9be34c92011-06-16 00:35:09 -07002435 prev = vma->vm_prev;
Hugh Dickins146425a2005-04-19 13:29:18 -07002436 /* we have start < vma->vm_end */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 /* if it doesn't overlap, we have nothing.. */
2439 end = start + len;
Hugh Dickins146425a2005-04-19 13:29:18 -07002440 if (vma->vm_start >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return 0;
2442
2443 /*
2444 * If we need to split any vma, do it now to save pain later.
2445 *
2446 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2447 * unmapped vm_area_struct will remain in use: so lower split_vma
2448 * places tmp vma above, and higher split_vma places tmp vma below.
2449 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002450 if (start > vma->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002451 int error;
2452
2453 /*
2454 * Make sure that map_count on return from munmap() will
2455 * not exceed its limit; but let map_count go just above
2456 * its limit temporarily, to help free resources as expected.
2457 */
2458 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
2459 return -ENOMEM;
2460
2461 error = __split_vma(mm, vma, start, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (error)
2463 return error;
Hugh Dickins146425a2005-04-19 13:29:18 -07002464 prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
2466
2467 /* Does it split the last one? */
2468 last = find_vma(mm, end);
2469 if (last && end > last->vm_start) {
KOSAKI Motohiro659ace52009-12-14 17:57:56 -08002470 int error = __split_vma(mm, last, end, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (error)
2472 return error;
2473 }
vishnu.pscc71aba2014-10-09 15:26:29 -07002474 vma = prev ? prev->vm_next : mm->mmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 /*
Rik van Rielba470de2008-10-18 20:26:50 -07002477 * unlock any mlock()ed ranges before detaching vmas
2478 */
2479 if (mm->locked_vm) {
2480 struct vm_area_struct *tmp = vma;
2481 while (tmp && tmp->vm_start < end) {
2482 if (tmp->vm_flags & VM_LOCKED) {
2483 mm->locked_vm -= vma_pages(tmp);
2484 munlock_vma_pages_all(tmp);
2485 }
2486 tmp = tmp->vm_next;
2487 }
2488 }
2489
2490 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 * Remove the vma's, and unmap the actual pages
2492 */
Hugh Dickins146425a2005-04-19 13:29:18 -07002493 detach_vmas_to_be_unmapped(mm, vma, prev, end);
2494 unmap_region(mm, vma, prev, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Dave Hansen1de4fa12014-11-14 07:18:31 -08002496 arch_unmap(mm, vma, start, end);
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /* Fix up all other VM information */
Hugh Dickins2c0b3812005-10-29 18:15:56 -07002499 remove_vma_list(mm, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 return 0;
2502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Al Virobfce2812012-04-20 21:57:04 -04002504int vm_munmap(unsigned long start, size_t len)
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002505{
2506 int ret;
Al Virobfce2812012-04-20 21:57:04 -04002507 struct mm_struct *mm = current->mm;
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002508
Michal Hockoae798782016-05-23 16:25:33 -07002509 if (down_write_killable(&mm->mmap_sem))
2510 return -EINTR;
2511
Linus Torvaldsa46ef992012-04-20 16:20:01 -07002512 ret = do_munmap(mm, start, len);
2513 up_write(&mm->mmap_sem);
2514 return ret;
2515}
2516EXPORT_SYMBOL(vm_munmap);
2517
Heiko Carstens6a6160a2009-01-14 14:14:15 +01002518SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002520 int ret;
2521 struct mm_struct *mm = current->mm;
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 profile_munmap(addr);
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002524 if (down_write_killable(&mm->mmap_sem))
2525 return -EINTR;
2526 ret = do_munmap(mm, addr, len);
2527 up_write(&mm->mmap_sem);
2528 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002531
2532/*
2533 * Emulation of deprecated remap_file_pages() syscall.
2534 */
2535SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
2536 unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
2537{
2538
2539 struct mm_struct *mm = current->mm;
2540 struct vm_area_struct *vma;
2541 unsigned long populate = 0;
2542 unsigned long ret = -EINVAL;
2543 struct file *file;
2544
Joe Perches756a025f02016-03-17 14:19:47 -07002545 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
2546 current->comm, current->pid);
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002547
2548 if (prot)
2549 return ret;
2550 start = start & PAGE_MASK;
2551 size = size & PAGE_MASK;
2552
2553 if (start + size <= start)
2554 return ret;
2555
2556 /* Does pgoff wrap? */
2557 if (pgoff + (size >> PAGE_SHIFT) < pgoff)
2558 return ret;
2559
Michal Hockodc0ef0d2016-05-23 16:25:27 -07002560 if (down_write_killable(&mm->mmap_sem))
2561 return -EINTR;
2562
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002563 vma = find_vma(mm, start);
2564
2565 if (!vma || !(vma->vm_flags & VM_SHARED))
2566 goto out;
2567
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002568 if (start < vma->vm_start)
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002569 goto out;
2570
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002571 if (start + size > vma->vm_end) {
2572 struct vm_area_struct *next;
2573
2574 for (next = vma->vm_next; next; next = next->vm_next) {
2575 /* hole between vmas ? */
2576 if (next->vm_start != next->vm_prev->vm_end)
2577 goto out;
2578
2579 if (next->vm_file != vma->vm_file)
2580 goto out;
2581
2582 if (next->vm_flags != vma->vm_flags)
2583 goto out;
2584
2585 if (start + size <= next->vm_end)
2586 break;
2587 }
2588
2589 if (!next)
2590 goto out;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002591 }
2592
2593 prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
2594 prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
2595 prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
2596
2597 flags &= MAP_NONBLOCK;
2598 flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
2599 if (vma->vm_flags & VM_LOCKED) {
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002600 struct vm_area_struct *tmp;
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002601 flags |= MAP_LOCKED;
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002602
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002603 /* drop PG_Mlocked flag for over-mapped range */
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002604 for (tmp = vma; tmp->vm_start >= start + size;
2605 tmp = tmp->vm_next) {
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07002606 /*
2607 * Split pmd and munlock page on the border
2608 * of the range.
2609 */
2610 vma_adjust_trans_huge(tmp, start, start + size, 0);
2611
Kirill A. Shutemov48f7df32016-02-17 13:11:15 -08002612 munlock_vma_pages_range(tmp,
2613 max(tmp->vm_start, start),
2614 min(tmp->vm_end, start + size));
2615 }
Kirill A. Shutemovc8d78c12015-02-10 14:09:46 -08002616 }
2617
2618 file = get_file(vma->vm_file);
2619 ret = do_mmap_pgoff(vma->vm_file, start, size,
2620 prot, flags, pgoff, &populate);
2621 fput(file);
2622out:
2623 up_write(&mm->mmap_sem);
2624 if (populate)
2625 mm_populate(ret, populate);
2626 if (!IS_ERR_VALUE(ret))
2627 ret = 0;
2628 return ret;
2629}
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631static inline void verify_mm_writelocked(struct mm_struct *mm)
2632{
Paul E. McKenneya241ec62005-10-30 15:03:12 -08002633#ifdef CONFIG_DEBUG_VM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (unlikely(down_read_trylock(&mm->mmap_sem))) {
2635 WARN_ON(1);
2636 up_read(&mm->mmap_sem);
2637 }
2638#endif
2639}
2640
2641/*
2642 * this is really a simplified "do_mmap". it only handles
2643 * anonymous maps. eventually we may be able to do some
2644 * brk-specific accounting here.
2645 */
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002646static int do_brk(unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
vishnu.pscc71aba2014-10-09 15:26:29 -07002648 struct mm_struct *mm = current->mm;
2649 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 unsigned long flags;
vishnu.pscc71aba2014-10-09 15:26:29 -07002651 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 pgoff_t pgoff = addr >> PAGE_SHIFT;
Kirill Korotaev3a459752006-09-07 14:17:04 +04002653 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 len = PAGE_ALIGN(len);
2656 if (!len)
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Kirill Korotaev3a459752006-09-07 14:17:04 +04002659 flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
2660
Al Viro2c6a1012009-12-03 19:40:46 -05002661 error = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
Alexander Kuleshovde1741a2015-11-05 18:46:54 -08002662 if (offset_in_page(error))
Kirill Korotaev3a459752006-09-07 14:17:04 +04002663 return error;
2664
Davidlohr Bueso363ee172014-01-21 15:49:15 -08002665 error = mlock_future_check(mm, mm->def_flags, len);
2666 if (error)
2667 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /*
2670 * mm->mmap_sem is required to protect against another thread
2671 * changing the mappings in case we sleep.
2672 */
2673 verify_mm_writelocked(mm);
2674
2675 /*
2676 * Clear old maps. this also does some error checking for us
2677 */
Rasmus Villemoes9fcd1452015-04-15 16:14:32 -07002678 while (find_vma_links(mm, addr, addr + len, &prev, &rb_link,
2679 &rb_parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (do_munmap(mm, addr, len))
2681 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683
2684 /* Check against address space limits *after* clearing old maps... */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002685 if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return -ENOMEM;
2687
2688 if (mm->map_count > sysctl_max_map_count)
2689 return -ENOMEM;
2690
Al Viro191c5422012-02-13 03:58:52 +00002691 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return -ENOMEM;
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 /* Can we just expand an old private anonymous mapping? */
Rik van Rielba470de2008-10-18 20:26:50 -07002695 vma = vma_merge(mm, prev, addr, addr + len, flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07002696 NULL, NULL, pgoff, NULL, NULL_VM_UFFD_CTX);
Rik van Rielba470de2008-10-18 20:26:50 -07002697 if (vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 goto out;
2699
2700 /*
2701 * create a vma struct for an anonymous mapping
2702 */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002703 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (!vma) {
2705 vm_unacct_memory(len >> PAGE_SHIFT);
2706 return -ENOMEM;
2707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Rik van Riel5beb4932010-03-05 13:42:07 -08002709 INIT_LIST_HEAD(&vma->anon_vma_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 vma->vm_mm = mm;
2711 vma->vm_start = addr;
2712 vma->vm_end = addr + len;
2713 vma->vm_pgoff = pgoff;
2714 vma->vm_flags = flags;
Coly Li3ed75eb2007-10-18 23:39:15 -07002715 vma->vm_page_prot = vm_get_page_prot(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 vma_link(mm, vma, prev, rb_link, rb_parent);
2717out:
Eric B Munson3af9e852010-05-18 15:30:49 +01002718 perf_event_mmap(vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 mm->total_vm += len >> PAGE_SHIFT;
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002720 mm->data_vm += len >> PAGE_SHIFT;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002721 if (flags & VM_LOCKED)
2722 mm->locked_vm += (len >> PAGE_SHIFT);
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07002723 vma->vm_flags |= VM_SOFTDIRTY;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002727int vm_brk(unsigned long addr, unsigned long len)
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002728{
2729 struct mm_struct *mm = current->mm;
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002730 int ret;
Michel Lespinasse128557f2013-02-22 16:32:40 -08002731 bool populate;
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002732
Michal Hocko2d6c9282016-05-23 16:25:42 -07002733 if (down_write_killable(&mm->mmap_sem))
2734 return -EINTR;
2735
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002736 ret = do_brk(addr, len);
Michel Lespinasse128557f2013-02-22 16:32:40 -08002737 populate = ((mm->def_flags & VM_LOCKED) != 0);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002738 up_write(&mm->mmap_sem);
Linus Torvalds5d22fc22016-05-27 15:57:31 -07002739 if (populate && !ret)
Michel Lespinasse128557f2013-02-22 16:32:40 -08002740 mm_populate(addr, len);
Linus Torvaldse4eb1ff2012-04-20 15:35:40 -07002741 return ret;
2742}
2743EXPORT_SYMBOL(vm_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745/* Release all mmaps. */
2746void exit_mmap(struct mm_struct *mm)
2747{
Peter Zijlstrad16dfc52011-05-24 17:11:45 -07002748 struct mmu_gather tlb;
Rik van Rielba470de2008-10-18 20:26:50 -07002749 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 unsigned long nr_accounted = 0;
2751
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002752 /* mm's last user has gone, and its about to be pulled down */
Andrea Arcangelicddb8a52008-07-28 15:46:29 -07002753 mmu_notifier_release(mm);
Jeremy Fitzhardinged6dd61c2007-05-02 19:27:14 +02002754
Rik van Rielba470de2008-10-18 20:26:50 -07002755 if (mm->locked_vm) {
2756 vma = mm->mmap;
2757 while (vma) {
2758 if (vma->vm_flags & VM_LOCKED)
2759 munlock_vma_pages_all(vma);
2760 vma = vma->vm_next;
2761 }
2762 }
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002763
2764 arch_exit_mmap(mm);
2765
Rik van Rielba470de2008-10-18 20:26:50 -07002766 vma = mm->mmap;
Jeremy Fitzhardinge9480c532009-02-11 13:04:41 -08002767 if (!vma) /* Can happen if dup_mmap() received an OOM */
2768 return;
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 lru_add_drain();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 flush_cache_mm(mm);
Linus Torvalds2b047252013-08-15 11:42:25 -07002772 tlb_gather_mmu(&tlb, mm, 0, -1);
Oleg Nesterov901608d2009-01-06 14:40:29 -08002773 /* update_hiwater_rss(mm) here? but nobody should be looking */
Hugh Dickinse0da3822005-04-19 13:29:15 -07002774 /* Use -1 here to ensure all VMAs in the mm are unmapped */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002775 unmap_vmas(&tlb, vma, 0, -1);
Hugh Dickins9ba69292009-09-21 17:02:20 -07002776
Hugh Dickins6ee86302013-04-29 15:07:44 -07002777 free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING);
Al Viro853f5e22012-03-05 14:03:47 -05002778 tlb_finish_mmu(&tlb, 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 /*
Hugh Dickins8f4f8c12005-10-29 18:16:29 -07002781 * Walk the list again, actually closing and freeing it,
2782 * with preemption enabled, without holding any MM locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 */
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002784 while (vma) {
2785 if (vma->vm_flags & VM_ACCOUNT)
2786 nr_accounted += vma_pages(vma);
Hugh Dickinsa8fb5612005-10-29 18:15:57 -07002787 vma = remove_vma(vma);
Linus Torvalds4f74d2c2012-05-06 13:54:06 -07002788 }
2789 vm_unacct_memory(nr_accounted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
2792/* Insert vm structure into process list sorted by address
2793 * and into the inode's i_mmap tree. If vm_file is non-NULL
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08002794 * then i_mmap_rwsem is taken here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 */
Hugh Dickins6597d782012-10-08 16:29:07 -07002796int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
Hugh Dickins6597d782012-10-08 16:29:07 -07002798 struct vm_area_struct *prev;
2799 struct rb_node **rb_link, *rb_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Chen Gangc9d13f52015-09-08 15:04:08 -07002801 if (find_vma_links(mm, vma->vm_start, vma->vm_end,
2802 &prev, &rb_link, &rb_parent))
2803 return -ENOMEM;
2804 if ((vma->vm_flags & VM_ACCOUNT) &&
2805 security_vm_enough_memory_mm(mm, vma_pages(vma)))
2806 return -ENOMEM;
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /*
2809 * The vm_pgoff of a purely anonymous vma should be irrelevant
2810 * until its first write fault, when page's anon_vma and index
2811 * are set. But now set the vm_pgoff it will almost certainly
2812 * end up with (unless mremap moves it elsewhere before that
2813 * first wfault), so /proc/pid/maps tells a consistent story.
2814 *
2815 * By setting it to reflect the virtual start address of the
2816 * vma, merges and splits can happen in a seamless way, just
2817 * using the existing file pgoff checks and manipulations.
2818 * Similarly in do_mmap_pgoff and in do_brk.
2819 */
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07002820 if (vma_is_anonymous(vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 BUG_ON(vma->anon_vma);
2822 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
2823 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 vma_link(mm, vma, prev, rb_link, rb_parent);
2826 return 0;
2827}
2828
2829/*
2830 * Copy the vma structure to a new location in the same mm,
2831 * prior to moving page table entries, to effect an mremap move.
2832 */
2833struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
Michel Lespinasse38a76012012-10-08 16:31:50 -07002834 unsigned long addr, unsigned long len, pgoff_t pgoff,
2835 bool *need_rmap_locks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 struct vm_area_struct *vma = *vmap;
2838 unsigned long vma_start = vma->vm_start;
2839 struct mm_struct *mm = vma->vm_mm;
2840 struct vm_area_struct *new_vma, *prev;
2841 struct rb_node **rb_link, *rb_parent;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002842 bool faulted_in_anon_vma = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 /*
2845 * If anonymous vma has not yet been faulted, update new pgoff
2846 * to match new location, to increase its chance of merging.
2847 */
Oleg Nesterovce757992015-09-08 14:58:34 -07002848 if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 pgoff = addr >> PAGE_SHIFT;
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002850 faulted_in_anon_vma = false;
2851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Hugh Dickins6597d782012-10-08 16:29:07 -07002853 if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent))
2854 return NULL; /* should never get here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -07002856 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
2857 vma->vm_userfaultfd_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (new_vma) {
2859 /*
2860 * Source vma may have been merged into new_vma
2861 */
Andrea Arcangeli948f0172012-01-10 15:08:05 -08002862 if (unlikely(vma_start >= new_vma->vm_start &&
2863 vma_start < new_vma->vm_end)) {
2864 /*
2865 * The only way we can get a vma_merge with
2866 * self during an mremap is if the vma hasn't
2867 * been faulted in yet and we were allowed to
2868 * reset the dst vma->vm_pgoff to the
2869 * destination address of the mremap to allow
2870 * the merge to happen. mremap must change the
2871 * vm_pgoff linearity between src and dst vmas
2872 * (in turn preventing a vma_merge) to be
2873 * safe. It is only safe to keep the vm_pgoff
2874 * linear if there are no pages mapped yet.
2875 */
Sasha Levin81d1b092014-10-09 15:28:10 -07002876 VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
Michel Lespinasse38a76012012-10-08 16:31:50 -07002877 *vmap = vma = new_vma;
Michel Lespinasse108d6642012-10-08 16:31:36 -07002878 }
Michel Lespinasse38a76012012-10-08 16:31:50 -07002879 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 } else {
Christoph Lametere94b1762006-12-06 20:33:17 -08002881 new_vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
Chen Gange3975892015-09-08 15:03:38 -07002882 if (!new_vma)
2883 goto out;
2884 *new_vma = *vma;
2885 new_vma->vm_start = addr;
2886 new_vma->vm_end = addr + len;
2887 new_vma->vm_pgoff = pgoff;
2888 if (vma_dup_policy(vma, new_vma))
2889 goto out_free_vma;
2890 INIT_LIST_HEAD(&new_vma->anon_vma_chain);
2891 if (anon_vma_clone(new_vma, vma))
2892 goto out_free_mempol;
2893 if (new_vma->vm_file)
2894 get_file(new_vma->vm_file);
2895 if (new_vma->vm_ops && new_vma->vm_ops->open)
2896 new_vma->vm_ops->open(new_vma);
2897 vma_link(mm, new_vma, prev, rb_link, rb_parent);
2898 *need_rmap_locks = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
2900 return new_vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08002901
Chen Gange3975892015-09-08 15:03:38 -07002902out_free_mempol:
Oleg Nesterovef0855d2013-09-11 14:20:14 -07002903 mpol_put(vma_policy(new_vma));
Chen Gange3975892015-09-08 15:03:38 -07002904out_free_vma:
Rik van Riel5beb4932010-03-05 13:42:07 -08002905 kmem_cache_free(vm_area_cachep, new_vma);
Chen Gange3975892015-09-08 15:03:38 -07002906out:
Rik van Riel5beb4932010-03-05 13:42:07 -08002907 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
akpm@osdl.org119f6572005-05-01 08:58:35 -07002909
2910/*
2911 * Return true if the calling process may expand its vm space by the passed
2912 * number of pages
2913 */
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002914bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
akpm@osdl.org119f6572005-05-01 08:58:35 -07002915{
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002916 if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
2917 return false;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002918
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002919 if (is_data_mapping(flags) &&
2920 mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07002921 /* Workaround for Valgrind */
2922 if (rlimit(RLIMIT_DATA) == 0 &&
2923 mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT)
2924 return true;
2925 if (!ignore_rlimit_data) {
2926 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 -08002927 current->comm, current->pid,
2928 (mm->data_vm + npages) << PAGE_SHIFT,
2929 rlimit(RLIMIT_DATA));
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002930 return false;
Konstantin Khlebnikovf4fcd552016-05-20 16:57:45 -07002931 }
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002932 }
akpm@osdl.org119f6572005-05-01 08:58:35 -07002933
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002934 return true;
2935}
2936
2937void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
2938{
2939 mm->total_vm += npages;
2940
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002941 if (is_exec_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002942 mm->exec_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002943 else if (is_stack_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002944 mm->stack_vm += npages;
Konstantin Khlebnikovd977d562016-02-02 16:57:43 -08002945 else if (is_data_mapping(flags))
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08002946 mm->data_vm += npages;
akpm@osdl.org119f6572005-05-01 08:58:35 -07002947}
Roland McGrathfa5dc222007-02-08 14:20:41 -08002948
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07002949static int special_mapping_fault(struct vm_area_struct *vma,
2950 struct vm_fault *vmf);
2951
2952/*
2953 * Having a close hook prevents vma merging regardless of flags.
2954 */
2955static void special_mapping_close(struct vm_area_struct *vma)
2956{
2957}
2958
2959static const char *special_mapping_name(struct vm_area_struct *vma)
2960{
2961 return ((struct vm_special_mapping *)vma->vm_private_data)->name;
2962}
2963
Dmitry Safonovb059a452016-06-28 14:35:38 +03002964static int special_mapping_mremap(struct vm_area_struct *new_vma)
2965{
2966 struct vm_special_mapping *sm = new_vma->vm_private_data;
2967
2968 if (sm->mremap)
2969 return sm->mremap(sm, new_vma);
2970 return 0;
2971}
2972
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07002973static const struct vm_operations_struct special_mapping_vmops = {
2974 .close = special_mapping_close,
2975 .fault = special_mapping_fault,
Dmitry Safonovb059a452016-06-28 14:35:38 +03002976 .mremap = special_mapping_mremap,
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07002977 .name = special_mapping_name,
2978};
2979
2980static const struct vm_operations_struct legacy_special_mapping_vmops = {
2981 .close = special_mapping_close,
2982 .fault = special_mapping_fault,
2983};
Roland McGrathfa5dc222007-02-08 14:20:41 -08002984
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002985static int special_mapping_fault(struct vm_area_struct *vma,
2986 struct vm_fault *vmf)
Roland McGrathfa5dc222007-02-08 14:20:41 -08002987{
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01002988 pgoff_t pgoff;
Roland McGrathfa5dc222007-02-08 14:20:41 -08002989 struct page **pages;
2990
Andy Lutomirskif872f542015-12-29 20:12:19 -08002991 if (vma->vm_ops == &legacy_special_mapping_vmops) {
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07002992 pages = vma->vm_private_data;
Andy Lutomirskif872f542015-12-29 20:12:19 -08002993 } else {
2994 struct vm_special_mapping *sm = vma->vm_private_data;
2995
2996 if (sm->fault)
2997 return sm->fault(sm, vma, vmf);
2998
2999 pages = sm->pages;
3000 }
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003001
Oleg Nesterov8a9cc3b2015-09-08 14:58:31 -07003002 for (pgoff = vmf->pgoff; pgoff && *pages; ++pages)
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003003 pgoff--;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003004
3005 if (*pages) {
3006 struct page *page = *pages;
3007 get_page(page);
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003008 vmf->page = page;
3009 return 0;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003010 }
3011
Nick Pigginb1d0e4f2008-02-09 01:15:19 +01003012 return VM_FAULT_SIGBUS;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003013}
3014
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003015static struct vm_area_struct *__install_special_mapping(
3016 struct mm_struct *mm,
3017 unsigned long addr, unsigned long len,
Chen Gang27f28b92015-11-05 18:48:41 -08003018 unsigned long vm_flags, void *priv,
3019 const struct vm_operations_struct *ops)
Roland McGrathfa5dc222007-02-08 14:20:41 -08003020{
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003021 int ret;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003022 struct vm_area_struct *vma;
3023
3024 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
3025 if (unlikely(vma == NULL))
Stefani Seibold3935ed62014-03-17 23:22:02 +01003026 return ERR_PTR(-ENOMEM);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003027
Rik van Riel5beb4932010-03-05 13:42:07 -08003028 INIT_LIST_HEAD(&vma->anon_vma_chain);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003029 vma->vm_mm = mm;
3030 vma->vm_start = addr;
3031 vma->vm_end = addr + len;
3032
Cyrill Gorcunovd9104d12013-09-11 14:22:24 -07003033 vma->vm_flags = vm_flags | mm->def_flags | VM_DONTEXPAND | VM_SOFTDIRTY;
Coly Li3ed75eb2007-10-18 23:39:15 -07003034 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003035
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003036 vma->vm_ops = ops;
3037 vma->vm_private_data = priv;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003038
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003039 ret = insert_vm_struct(mm, vma);
3040 if (ret)
3041 goto out;
Roland McGrathfa5dc222007-02-08 14:20:41 -08003042
Konstantin Khlebnikov84638332016-01-14 15:22:07 -08003043 vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003045 perf_event_mmap(vma);
Peter Zijlstra089dd792009-06-05 14:04:55 +02003046
Stefani Seibold3935ed62014-03-17 23:22:02 +01003047 return vma;
Tavis Ormandy462e635e2010-12-09 15:29:42 +01003048
3049out:
3050 kmem_cache_free(vm_area_cachep, vma);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003051 return ERR_PTR(ret);
3052}
3053
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003054/*
3055 * Called with mm->mmap_sem held for writing.
3056 * Insert a new vma covering the given region, with the given flags.
3057 * Its pages are supplied by the given array of struct page *.
3058 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3059 * The region past the last page supplied will always produce SIGBUS.
3060 * The array pointer and the pages it points to are assumed to stay alive
3061 * for as long as this mapping might exist.
3062 */
3063struct vm_area_struct *_install_special_mapping(
3064 struct mm_struct *mm,
3065 unsigned long addr, unsigned long len,
3066 unsigned long vm_flags, const struct vm_special_mapping *spec)
3067{
Chen Gang27f28b92015-11-05 18:48:41 -08003068 return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
3069 &special_mapping_vmops);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003070}
3071
Stefani Seibold3935ed62014-03-17 23:22:02 +01003072int install_special_mapping(struct mm_struct *mm,
3073 unsigned long addr, unsigned long len,
3074 unsigned long vm_flags, struct page **pages)
3075{
Andy Lutomirskia62c34b2014-05-19 15:58:33 -07003076 struct vm_area_struct *vma = __install_special_mapping(
Chen Gang27f28b92015-11-05 18:48:41 -08003077 mm, addr, len, vm_flags, (void *)pages,
3078 &legacy_special_mapping_vmops);
Stefani Seibold3935ed62014-03-17 23:22:02 +01003079
Duan Jiong14bd5b42014-06-04 16:07:05 -07003080 return PTR_ERR_OR_ZERO(vma);
Roland McGrathfa5dc222007-02-08 14:20:41 -08003081}
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003082
3083static DEFINE_MUTEX(mm_all_locks_mutex);
3084
Peter Zijlstra454ed842008-08-11 09:30:25 +02003085static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003086{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003087 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003088 /*
3089 * The LSB of head.next can't change from under us
3090 * because we hold the mm_all_locks_mutex.
3091 */
Jiri Kosina572043c2013-01-11 14:31:59 -08003092 down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003093 /*
3094 * We can safely modify head.next after taking the
Ingo Molnar5a505082012-12-02 19:56:46 +00003095 * anon_vma->root->rwsem. If some other vma in this mm shares
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003096 * the same anon_vma we won't take it again.
3097 *
3098 * No need of atomic instructions here, head.next
3099 * can't change from under us thanks to the
Ingo Molnar5a505082012-12-02 19:56:46 +00003100 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003101 */
3102 if (__test_and_set_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003103 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003104 BUG();
3105 }
3106}
3107
Peter Zijlstra454ed842008-08-11 09:30:25 +02003108static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003109{
3110 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3111 /*
3112 * AS_MM_ALL_LOCKS can't change from under us because
3113 * we hold the mm_all_locks_mutex.
3114 *
3115 * Operations on ->flags have to be atomic because
3116 * even if AS_MM_ALL_LOCKS is stable thanks to the
3117 * mm_all_locks_mutex, there may be other cpus
3118 * changing other bitflags in parallel to us.
3119 */
3120 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
3121 BUG();
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003122 down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_sem);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003123 }
3124}
3125
3126/*
3127 * This operation locks against the VM for all pte/vma/mm related
3128 * operations that could ever happen on a certain mm. This includes
3129 * vmtruncate, try_to_unmap, and all page faults.
3130 *
3131 * The caller must take the mmap_sem in write mode before calling
3132 * mm_take_all_locks(). The caller isn't allowed to release the
3133 * mmap_sem until mm_drop_all_locks() returns.
3134 *
3135 * mmap_sem in write mode is required in order to block all operations
3136 * that could modify pagetables and free pages without need of
Kirill A. Shutemov27ba0642015-02-10 14:09:59 -08003137 * altering the vma layout. It's also needed in write mode to avoid new
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003138 * anon_vmas to be associated with existing vmas.
3139 *
3140 * A single task can't take more than one mm_take_all_locks() in a row
3141 * or it would deadlock.
3142 *
Michel Lespinassebf181b92012-10-08 16:31:39 -07003143 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003144 * mapping->flags avoid to take the same lock twice, if more than one
3145 * vma in this mm is backed by the same anon_vma or address_space.
3146 *
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003147 * We take locks in following order, accordingly to comment at beginning
3148 * of mm/rmap.c:
3149 * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
3150 * hugetlb mapping);
3151 * - all i_mmap_rwsem locks;
3152 * - all anon_vma->rwseml
3153 *
3154 * We can take all locks within these types randomly because the VM code
3155 * doesn't nest them and we protected from parallel mm_take_all_locks() by
3156 * mm_all_locks_mutex.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003157 *
3158 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3159 * that may have to take thousand of locks.
3160 *
3161 * mm_take_all_locks() can fail if it's interrupted by signals.
3162 */
3163int mm_take_all_locks(struct mm_struct *mm)
3164{
3165 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003166 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003167
3168 BUG_ON(down_read_trylock(&mm->mmap_sem));
3169
3170 mutex_lock(&mm_all_locks_mutex);
3171
3172 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3173 if (signal_pending(current))
3174 goto out_unlock;
Kirill A. Shutemov88f306b2016-01-15 16:57:31 -08003175 if (vma->vm_file && vma->vm_file->f_mapping &&
3176 is_vm_hugetlb_page(vma))
3177 vm_lock_mapping(mm, vma->vm_file->f_mapping);
3178 }
3179
3180 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3181 if (signal_pending(current))
3182 goto out_unlock;
3183 if (vma->vm_file && vma->vm_file->f_mapping &&
3184 !is_vm_hugetlb_page(vma))
Peter Zijlstra454ed842008-08-11 09:30:25 +02003185 vm_lock_mapping(mm, vma->vm_file->f_mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003186 }
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003187
3188 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3189 if (signal_pending(current))
3190 goto out_unlock;
3191 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003192 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3193 vm_lock_anon_vma(mm, avc->anon_vma);
Peter Zijlstra7cd5a022008-08-11 09:30:25 +02003194 }
3195
Kautuk Consul584cff52011-10-31 17:08:59 -07003196 return 0;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003197
3198out_unlock:
Kautuk Consul584cff52011-10-31 17:08:59 -07003199 mm_drop_all_locks(mm);
3200 return -EINTR;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003201}
3202
3203static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
3204{
Michel Lespinassebf181b92012-10-08 16:31:39 -07003205 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_node)) {
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003206 /*
3207 * The LSB of head.next can't change to 0 from under
3208 * us because we hold the mm_all_locks_mutex.
3209 *
3210 * We must however clear the bitflag before unlocking
Michel Lespinassebf181b92012-10-08 16:31:39 -07003211 * the vma so the users using the anon_vma->rb_root will
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003212 * never see our bitflag.
3213 *
3214 * No need of atomic instructions here, head.next
3215 * can't change from under us until we release the
Ingo Molnar5a505082012-12-02 19:56:46 +00003216 * anon_vma->root->rwsem.
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003217 */
3218 if (!__test_and_clear_bit(0, (unsigned long *)
Michel Lespinassebf181b92012-10-08 16:31:39 -07003219 &anon_vma->root->rb_root.rb_node))
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003220 BUG();
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08003221 anon_vma_unlock_write(anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003222 }
3223}
3224
3225static void vm_unlock_mapping(struct address_space *mapping)
3226{
3227 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3228 /*
3229 * AS_MM_ALL_LOCKS can't change to 0 from under us
3230 * because we hold the mm_all_locks_mutex.
3231 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003232 i_mmap_unlock_write(mapping);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003233 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
3234 &mapping->flags))
3235 BUG();
3236 }
3237}
3238
3239/*
3240 * The mmap_sem cannot be released by the caller until
3241 * mm_drop_all_locks() returns.
3242 */
3243void mm_drop_all_locks(struct mm_struct *mm)
3244{
3245 struct vm_area_struct *vma;
Rik van Riel5beb4932010-03-05 13:42:07 -08003246 struct anon_vma_chain *avc;
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003247
3248 BUG_ON(down_read_trylock(&mm->mmap_sem));
3249 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
3250
3251 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3252 if (vma->anon_vma)
Rik van Riel5beb4932010-03-05 13:42:07 -08003253 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3254 vm_unlock_anon_vma(avc->anon_vma);
Andrea Arcangeli7906d002008-07-28 15:46:26 -07003255 if (vma->vm_file && vma->vm_file->f_mapping)
3256 vm_unlock_mapping(vma->vm_file->f_mapping);
3257 }
3258
3259 mutex_unlock(&mm_all_locks_mutex);
3260}
David Howells8feae132009-01-08 12:04:47 +00003261
3262/*
3263 * initialise the VMA slab
3264 */
3265void __init mmap_init(void)
3266{
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003267 int ret;
3268
Tejun Heo908c7f12014-09-08 09:51:29 +09003269 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
KOSAKI Motohiro00a62ce2009-04-30 15:08:51 -07003270 VM_BUG_ON(ret);
David Howells8feae132009-01-08 12:04:47 +00003271}
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003272
3273/*
3274 * Initialise sysctl_user_reserve_kbytes.
3275 *
3276 * This is intended to prevent a user from starting a single memory hogging
3277 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3278 * mode.
3279 *
3280 * The default value is min(3% of free memory, 128MB)
3281 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3282 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003283static int init_user_reserve(void)
Andrew Shewmakerc9b1d092013-04-29 15:08:10 -07003284{
3285 unsigned long free_kbytes;
3286
3287 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3288
3289 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
3290 return 0;
3291}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003292subsys_initcall(init_user_reserve);
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003293
3294/*
3295 * Initialise sysctl_admin_reserve_kbytes.
3296 *
3297 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3298 * to log in and kill a memory hogging process.
3299 *
3300 * Systems with more than 256MB will reserve 8MB, enough to recover
3301 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3302 * only reserve 3% of free pages by default.
3303 */
Andrew Shewmaker16408792013-04-29 15:08:12 -07003304static int init_admin_reserve(void)
Andrew Shewmaker4eeab4f2013-04-29 15:08:11 -07003305{
3306 unsigned long free_kbytes;
3307
3308 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3309
3310 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
3311 return 0;
3312}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003313subsys_initcall(init_admin_reserve);
Andrew Shewmaker16408792013-04-29 15:08:12 -07003314
3315/*
3316 * Reinititalise user and admin reserves if memory is added or removed.
3317 *
3318 * The default user reserve max is 128MB, and the default max for the
3319 * admin reserve is 8MB. These are usually, but not always, enough to
3320 * enable recovery from a memory hogging process using login/sshd, a shell,
3321 * and tools like top. It may make sense to increase or even disable the
3322 * reserve depending on the existence of swap or variations in the recovery
3323 * tools. So, the admin may have changed them.
3324 *
3325 * If memory is added and the reserves have been eliminated or increased above
3326 * the default max, then we'll trust the admin.
3327 *
3328 * If memory is removed and there isn't enough free memory, then we
3329 * need to reset the reserves.
3330 *
3331 * Otherwise keep the reserve set by the admin.
3332 */
3333static int reserve_mem_notifier(struct notifier_block *nb,
3334 unsigned long action, void *data)
3335{
3336 unsigned long tmp, free_kbytes;
3337
3338 switch (action) {
3339 case MEM_ONLINE:
3340 /* Default max is 128MB. Leave alone if modified by operator. */
3341 tmp = sysctl_user_reserve_kbytes;
3342 if (0 < tmp && tmp < (1UL << 17))
3343 init_user_reserve();
3344
3345 /* Default max is 8MB. Leave alone if modified by operator. */
3346 tmp = sysctl_admin_reserve_kbytes;
3347 if (0 < tmp && tmp < (1UL << 13))
3348 init_admin_reserve();
3349
3350 break;
3351 case MEM_OFFLINE:
3352 free_kbytes = global_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
3353
3354 if (sysctl_user_reserve_kbytes > free_kbytes) {
3355 init_user_reserve();
3356 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3357 sysctl_user_reserve_kbytes);
3358 }
3359
3360 if (sysctl_admin_reserve_kbytes > free_kbytes) {
3361 init_admin_reserve();
3362 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3363 sysctl_admin_reserve_kbytes);
3364 }
3365 break;
3366 default:
3367 break;
3368 }
3369 return NOTIFY_OK;
3370}
3371
3372static struct notifier_block reserve_mem_nb = {
3373 .notifier_call = reserve_mem_notifier,
3374};
3375
3376static int __meminit init_reserve_notifier(void)
3377{
3378 if (register_hotmemory_notifier(&reserve_mem_nb))
Mitchel Humpherysb1de0d12014-06-06 14:38:30 -07003379 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
Andrew Shewmaker16408792013-04-29 15:08:12 -07003380
3381 return 0;
3382}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -08003383subsys_initcall(init_reserve_notifier);