]> git.ipfire.org Git - thirdparty/linux.git/blame - mm/mmap.c
maple_tree: relax lockdep checks for on-stack trees
[thirdparty/linux.git] / mm / mmap.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * mm/mmap.c
4 *
5 * Written by obz.
6 *
046c6884 7 * Address space accounting code <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
8 */
9
b1de0d13
MH
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
e8420a8e 12#include <linux/kernel.h>
1da177e4 13#include <linux/slab.h>
4af3c9cc 14#include <linux/backing-dev.h>
1da177e4 15#include <linux/mm.h>
17fca131 16#include <linux/mm_inline.h>
1da177e4
LT
17#include <linux/shm.h>
18#include <linux/mman.h>
19#include <linux/pagemap.h>
20#include <linux/swap.h>
21#include <linux/syscalls.h>
c59ede7b 22#include <linux/capability.h>
1da177e4
LT
23#include <linux/init.h>
24#include <linux/file.h>
25#include <linux/fs.h>
26#include <linux/personality.h>
27#include <linux/security.h>
28#include <linux/hugetlb.h>
c01d5b30 29#include <linux/shmem_fs.h>
1da177e4 30#include <linux/profile.h>
b95f1b31 31#include <linux/export.h>
1da177e4
LT
32#include <linux/mount.h>
33#include <linux/mempolicy.h>
34#include <linux/rmap.h>
cddb8a5c 35#include <linux/mmu_notifier.h>
82f71ae4 36#include <linux/mmdebug.h>
cdd6c482 37#include <linux/perf_event.h>
120a795d 38#include <linux/audit.h>
b15d00b6 39#include <linux/khugepaged.h>
2b144498 40#include <linux/uprobes.h>
1640879a
AS
41#include <linux/notifier.h>
42#include <linux/memory.h>
b1de0d13 43#include <linux/printk.h>
19a809af 44#include <linux/userfaultfd_k.h>
d977d56c 45#include <linux/moduleparam.h>
62b5f7d0 46#include <linux/pkeys.h>
21292580 47#include <linux/oom.h>
04f5866e 48#include <linux/sched/mm.h>
d7597f59 49#include <linux/ksm.h>
1da177e4 50
7c0f6ba6 51#include <linux/uaccess.h>
1da177e4
LT
52#include <asm/cacheflush.h>
53#include <asm/tlb.h>
d6dd61c8 54#include <asm/mmu_context.h>
1da177e4 55
df529cab
JK
56#define CREATE_TRACE_POINTS
57#include <trace/events/mmap.h>
58
42b77728
JB
59#include "internal.h"
60
3a459756
KK
61#ifndef arch_mmap_check
62#define arch_mmap_check(addr, len, flags) (0)
63#endif
64
d07e2259
DC
65#ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
66const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
67const int mmap_rnd_bits_max = CONFIG_ARCH_MMAP_RND_BITS_MAX;
68int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
69#endif
70#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
71const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
72const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
73int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
74#endif
75
f4fcd558 76static bool ignore_rlimit_data;
d977d56c 77core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644);
d07e2259 78
763ecb03 79static void unmap_region(struct mm_struct *mm, struct maple_tree *mt,
e0da382c 80 struct vm_area_struct *vma, struct vm_area_struct *prev,
763ecb03 81 struct vm_area_struct *next, unsigned long start,
68f48381 82 unsigned long end, bool mm_wr_locked);
e0da382c 83
64e45507
PF
84static pgprot_t vm_pgprot_modify(pgprot_t oldprot, unsigned long vm_flags)
85{
86 return pgprot_modify(oldprot, vm_get_page_prot(vm_flags));
87}
88
89/* Update vma->vm_page_prot to reflect vma->vm_flags. */
90void vma_set_page_prot(struct vm_area_struct *vma)
91{
92 unsigned long vm_flags = vma->vm_flags;
6d2329f8 93 pgprot_t vm_page_prot;
64e45507 94
6d2329f8
AA
95 vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags);
96 if (vma_wants_writenotify(vma, vm_page_prot)) {
64e45507 97 vm_flags &= ~VM_SHARED;
6d2329f8 98 vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags);
64e45507 99 }
c1e8d7c6 100 /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */
6d2329f8 101 WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
64e45507
PF
102}
103
1da177e4 104/*
c8c06efa 105 * Requires inode->i_mapping->i_mmap_rwsem
1da177e4
LT
106 */
107static void __remove_shared_vm_struct(struct vm_area_struct *vma,
108 struct file *file, struct address_space *mapping)
109{
1da177e4 110 if (vma->vm_flags & VM_SHARED)
4bb5f5d9 111 mapping_unmap_writable(mapping);
1da177e4
LT
112
113 flush_dcache_mmap_lock(mapping);
27ba0644 114 vma_interval_tree_remove(vma, &mapping->i_mmap);
1da177e4
LT
115 flush_dcache_mmap_unlock(mapping);
116}
117
118/*
6b2dbba8 119 * Unlink a file-based vm structure from its interval tree, to hide
a8fb5618 120 * vma from rmap and vmtruncate before freeing its page tables.
1da177e4 121 */
a8fb5618 122void unlink_file_vma(struct vm_area_struct *vma)
1da177e4
LT
123{
124 struct file *file = vma->vm_file;
125
1da177e4
LT
126 if (file) {
127 struct address_space *mapping = file->f_mapping;
83cde9e8 128 i_mmap_lock_write(mapping);
1da177e4 129 __remove_shared_vm_struct(vma, file, mapping);
83cde9e8 130 i_mmap_unlock_write(mapping);
1da177e4 131 }
a8fb5618
HD
132}
133
134/*
763ecb03 135 * Close a vm structure and free it.
a8fb5618 136 */
0d2ebf9c 137static void remove_vma(struct vm_area_struct *vma, bool unreachable)
a8fb5618 138{
a8fb5618 139 might_sleep();
1da177e4
LT
140 if (vma->vm_ops && vma->vm_ops->close)
141 vma->vm_ops->close(vma);
e9714acf 142 if (vma->vm_file)
a8fb5618 143 fput(vma->vm_file);
f0be3d32 144 mpol_put(vma_policy(vma));
0d2ebf9c
SB
145 if (unreachable)
146 __vm_area_free(vma);
147 else
148 vm_area_free(vma);
1da177e4
LT
149}
150
b62b633e
LH
151static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi,
152 unsigned long min)
153{
154 return mas_prev(&vmi->mas, min);
155}
156
157static inline int vma_iter_clear_gfp(struct vma_iterator *vmi,
158 unsigned long start, unsigned long end, gfp_t gfp)
159{
160 vmi->mas.index = start;
161 vmi->mas.last = end - 1;
162 mas_store_gfp(&vmi->mas, NULL, gfp);
163 if (unlikely(mas_is_err(&vmi->mas)))
164 return -ENOMEM;
165
166 return 0;
167}
168
2e7ce7d3
LH
169/*
170 * check_brk_limits() - Use platform specific check of range & verify mlock
171 * limits.
172 * @addr: The address to check
173 * @len: The size of increase.
174 *
175 * Return: 0 on success.
176 */
177static int check_brk_limits(unsigned long addr, unsigned long len)
178{
179 unsigned long mapped_addr;
180
181 mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
182 if (IS_ERR_VALUE(mapped_addr))
183 return mapped_addr;
184
b0cc5e89 185 return mlock_future_ok(current->mm, current->mm->def_flags, len)
3c54a298 186 ? 0 : -EAGAIN;
2e7ce7d3 187}
92fed820 188static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma,
763ecb03 189 unsigned long addr, unsigned long request, unsigned long flags);
6a6160a7 190SYSCALL_DEFINE1(brk, unsigned long, brk)
1da177e4 191{
9bc8039e 192 unsigned long newbrk, oldbrk, origbrk;
1da177e4 193 struct mm_struct *mm = current->mm;
2e7ce7d3 194 struct vm_area_struct *brkvma, *next = NULL;
a5b4592c 195 unsigned long min_brk;
408579cd 196 bool populate = false;
897ab3e0 197 LIST_HEAD(uf);
92fed820 198 struct vma_iterator vmi;
1da177e4 199
d8ed45c5 200 if (mmap_write_lock_killable(mm))
dc0ef0df 201 return -EINTR;
1da177e4 202
9bc8039e
YS
203 origbrk = mm->brk;
204
a5b4592c 205#ifdef CONFIG_COMPAT_BRK
5520e894
JK
206 /*
207 * CONFIG_COMPAT_BRK can still be overridden by setting
208 * randomize_va_space to 2, which will still cause mm->start_brk
209 * to be arbitrarily shifted
210 */
4471a675 211 if (current->brk_randomized)
5520e894
JK
212 min_brk = mm->start_brk;
213 else
214 min_brk = mm->end_data;
a5b4592c
JK
215#else
216 min_brk = mm->start_brk;
217#endif
218 if (brk < min_brk)
1da177e4 219 goto out;
1e624196
RG
220
221 /*
222 * Check against rlimit here. If this check is done later after the test
223 * of oldbrk with newbrk then it can escape the test and let the data
224 * segment grow beyond its set limit the in case where the limit is
225 * not page aligned -Ram Gupta
226 */
8764b338
CG
227 if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk,
228 mm->end_data, mm->start_data))
1e624196
RG
229 goto out;
230
1da177e4
LT
231 newbrk = PAGE_ALIGN(brk);
232 oldbrk = PAGE_ALIGN(mm->brk);
9bc8039e
YS
233 if (oldbrk == newbrk) {
234 mm->brk = brk;
235 goto success;
236 }
1da177e4 237
408579cd 238 /* Always allow shrinking brk. */
1da177e4 239 if (brk <= mm->brk) {
2e7ce7d3 240 /* Search one past newbrk */
92fed820
LH
241 vma_iter_init(&vmi, mm, newbrk);
242 brkvma = vma_find(&vmi, oldbrk);
f5ad5083 243 if (!brkvma || brkvma->vm_start >= oldbrk)
2e7ce7d3 244 goto out; /* mapping intersects with an existing non-brk vma. */
9bc8039e 245 /*
2e7ce7d3 246 * mm->brk must be protected by write mmap_lock.
408579cd 247 * do_vma_munmap() will drop the lock on success, so update it
27b26701 248 * before calling do_vma_munmap().
9bc8039e
YS
249 */
250 mm->brk = brk;
408579cd
LH
251 if (do_vma_munmap(&vmi, brkvma, newbrk, oldbrk, &uf, true))
252 goto out;
253
254 goto success_unlocked;
1da177e4
LT
255 }
256
2e7ce7d3
LH
257 if (check_brk_limits(oldbrk, newbrk - oldbrk))
258 goto out;
259
260 /*
261 * Only check if the next VMA is within the stack_guard_gap of the
262 * expansion area
263 */
92fed820
LH
264 vma_iter_init(&vmi, mm, oldbrk);
265 next = vma_find(&vmi, newbrk + PAGE_SIZE + stack_guard_gap);
1be7107f 266 if (next && newbrk + PAGE_SIZE > vm_start_gap(next))
1da177e4
LT
267 goto out;
268
92fed820 269 brkvma = vma_prev_limit(&vmi, mm->start_brk);
1da177e4 270 /* Ok, looks good - let it rip. */
92fed820 271 if (do_brk_flags(&vmi, brkvma, oldbrk, newbrk - oldbrk, 0) < 0)
1da177e4 272 goto out;
2e7ce7d3 273
1da177e4 274 mm->brk = brk;
408579cd
LH
275 if (mm->def_flags & VM_LOCKED)
276 populate = true;
9bc8039e
YS
277
278success:
408579cd
LH
279 mmap_write_unlock(mm);
280success_unlocked:
897ab3e0 281 userfaultfd_unmap_complete(mm, &uf);
128557ff
ML
282 if (populate)
283 mm_populate(oldbrk, newbrk - oldbrk);
284 return brk;
285
1da177e4 286out:
408579cd 287 mm->brk = origbrk;
d8ed45c5 288 mmap_write_unlock(mm);
b7204006 289 return origbrk;
1da177e4
LT
290}
291
d4af56c5 292#if defined(CONFIG_DEBUG_VM_MAPLE_TREE)
eafd4dc4 293static void validate_mm(struct mm_struct *mm)
1da177e4
LT
294{
295 int bug = 0;
296 int i = 0;
763ecb03 297 struct vm_area_struct *vma;
b50e195f 298 VMA_ITERATOR(vmi, mm, 0);
524e00b3 299
b50e195f
LH
300 mt_validate(&mm->mm_mt);
301 for_each_vma(vmi, vma) {
524e00b3 302#ifdef CONFIG_DEBUG_VM_RB
12352d3c 303 struct anon_vma *anon_vma = vma->anon_vma;
ed8ea815 304 struct anon_vma_chain *avc;
b50e195f
LH
305#endif
306 unsigned long vmi_start, vmi_end;
307 bool warn = 0;
ff26f70f 308
b50e195f
LH
309 vmi_start = vma_iter_addr(&vmi);
310 vmi_end = vma_iter_end(&vmi);
311 if (VM_WARN_ON_ONCE_MM(vma->vm_end != vmi_end, mm))
312 warn = 1;
ff26f70f 313
b50e195f
LH
314 if (VM_WARN_ON_ONCE_MM(vma->vm_start != vmi_start, mm))
315 warn = 1;
316
317 if (warn) {
318 pr_emerg("issue in %s\n", current->comm);
319 dump_stack();
320 dump_vma(vma);
321 pr_emerg("tree range: %px start %lx end %lx\n", vma,
322 vmi_start, vmi_end - 1);
323 vma_iter_dump_tree(&vmi);
324 }
325
326#ifdef CONFIG_DEBUG_VM_RB
12352d3c
KK
327 if (anon_vma) {
328 anon_vma_lock_read(anon_vma);
329 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
330 anon_vma_interval_tree_verify(avc);
331 anon_vma_unlock_read(anon_vma);
332 }
524e00b3 333#endif
1da177e4
LT
334 i++;
335 }
5a0768f6 336 if (i != mm->map_count) {
b50e195f 337 pr_emerg("map_count %d vma iterator %d\n", mm->map_count, i);
5a0768f6
ML
338 bug = 1;
339 }
96dad67f 340 VM_BUG_ON_MM(bug, mm);
1da177e4 341}
524e00b3
LH
342
343#else /* !CONFIG_DEBUG_VM_MAPLE_TREE */
1da177e4 344#define validate_mm(mm) do { } while (0)
524e00b3 345#endif /* CONFIG_DEBUG_VM_MAPLE_TREE */
8f26e0b1 346
bf181b9f
ML
347/*
348 * vma has some anon_vma assigned, and is already inserted on that
349 * anon_vma's interval trees.
350 *
351 * Before updating the vma's vm_start / vm_end / vm_pgoff fields, the
352 * vma must be removed from the anon_vma's interval trees using
353 * anon_vma_interval_tree_pre_update_vma().
354 *
355 * After the update, the vma will be reinserted using
356 * anon_vma_interval_tree_post_update_vma().
357 *
c1e8d7c6 358 * The entire update must be protected by exclusive mmap_lock and by
bf181b9f
ML
359 * the root anon_vma's mutex.
360 */
361static inline void
362anon_vma_interval_tree_pre_update_vma(struct vm_area_struct *vma)
363{
364 struct anon_vma_chain *avc;
365
366 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
367 anon_vma_interval_tree_remove(avc, &avc->anon_vma->rb_root);
368}
369
370static inline void
371anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
372{
373 struct anon_vma_chain *avc;
374
375 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
376 anon_vma_interval_tree_insert(avc, &avc->anon_vma->rb_root);
377}
378
e8420a8e
CH
379static unsigned long count_vma_pages_range(struct mm_struct *mm,
380 unsigned long addr, unsigned long end)
381{
2e3af1db 382 VMA_ITERATOR(vmi, mm, addr);
e8420a8e 383 struct vm_area_struct *vma;
2e3af1db 384 unsigned long nr_pages = 0;
e8420a8e 385
2e3af1db
MWO
386 for_each_vma_range(vmi, vma, end) {
387 unsigned long vm_start = max(addr, vma->vm_start);
388 unsigned long vm_end = min(end, vma->vm_end);
e8420a8e 389
2e3af1db 390 nr_pages += PHYS_PFN(vm_end - vm_start);
e8420a8e
CH
391 }
392
393 return nr_pages;
394}
395
c154124f
LH
396static void __vma_link_file(struct vm_area_struct *vma,
397 struct address_space *mapping)
1da177e4 398{
c154124f
LH
399 if (vma->vm_flags & VM_SHARED)
400 mapping_allow_writable(mapping);
1da177e4 401
c154124f
LH
402 flush_dcache_mmap_lock(mapping);
403 vma_interval_tree_insert(vma, &mapping->i_mmap);
404 flush_dcache_mmap_unlock(mapping);
1da177e4
LT
405}
406
763ecb03 407static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)
1da177e4 408{
79e4f2ca 409 VMA_ITERATOR(vmi, mm, 0);
1da177e4
LT
410 struct address_space *mapping = NULL;
411
79e4f2ca 412 if (vma_iter_prealloc(&vmi))
d4af56c5
LH
413 return -ENOMEM;
414
6852c46c
YM
415 vma_iter_store(&vmi, vma);
416
64ac4940 417 if (vma->vm_file) {
1da177e4 418 mapping = vma->vm_file->f_mapping;
83cde9e8 419 i_mmap_lock_write(mapping);
c154124f 420 __vma_link_file(vma, mapping);
83cde9e8 421 i_mmap_unlock_write(mapping);
c154124f 422 }
1da177e4
LT
423
424 mm->map_count++;
425 validate_mm(mm);
d4af56c5 426 return 0;
1da177e4
LT
427}
428
68cefec5
LH
429/*
430 * init_multi_vma_prep() - Initializer for struct vma_prepare
431 * @vp: The vma_prepare struct
432 * @vma: The vma that will be altered once locked
433 * @next: The next vma if it is to be adjusted
434 * @remove: The first vma to be removed
435 * @remove2: The second vma to be removed
436 */
437static inline void init_multi_vma_prep(struct vma_prepare *vp,
438 struct vm_area_struct *vma, struct vm_area_struct *next,
439 struct vm_area_struct *remove, struct vm_area_struct *remove2)
440{
441 memset(vp, 0, sizeof(struct vma_prepare));
442 vp->vma = vma;
443 vp->anon_vma = vma->anon_vma;
444 vp->remove = remove;
445 vp->remove2 = remove2;
446 vp->adj_next = next;
447 if (!vp->anon_vma && next)
448 vp->anon_vma = next->anon_vma;
449
450 vp->file = vma->vm_file;
451 if (vp->file)
452 vp->mapping = vma->vm_file->f_mapping;
453
454}
455
456/*
457 * init_vma_prep() - Initializer wrapper for vma_prepare struct
458 * @vp: The vma_prepare struct
459 * @vma: The vma that will be altered once locked
460 */
461static inline void init_vma_prep(struct vma_prepare *vp,
462 struct vm_area_struct *vma)
463{
464 init_multi_vma_prep(vp, vma, NULL, NULL, NULL);
465}
466
467
440703e0
LH
468/*
469 * vma_prepare() - Helper function for handling locking VMAs prior to altering
470 * @vp: The initialized vma_prepare struct
471 */
472static inline void vma_prepare(struct vma_prepare *vp)
473{
10fca64a
SB
474 vma_start_write(vp->vma);
475 if (vp->adj_next)
476 vma_start_write(vp->adj_next);
477 /* vp->insert is always a newly created VMA, no need for locking */
478 if (vp->remove)
479 vma_start_write(vp->remove);
480 if (vp->remove2)
481 vma_start_write(vp->remove2);
482
440703e0
LH
483 if (vp->file) {
484 uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end);
485
486 if (vp->adj_next)
487 uprobe_munmap(vp->adj_next, vp->adj_next->vm_start,
488 vp->adj_next->vm_end);
489
490 i_mmap_lock_write(vp->mapping);
491 if (vp->insert && vp->insert->vm_file) {
492 /*
493 * Put into interval tree now, so instantiated pages
494 * are visible to arm/parisc __flush_dcache_page
495 * throughout; but we cannot insert into address
496 * space until vma start or end is updated.
497 */
498 __vma_link_file(vp->insert,
499 vp->insert->vm_file->f_mapping);
500 }
501 }
502
503 if (vp->anon_vma) {
504 anon_vma_lock_write(vp->anon_vma);
505 anon_vma_interval_tree_pre_update_vma(vp->vma);
506 if (vp->adj_next)
507 anon_vma_interval_tree_pre_update_vma(vp->adj_next);
508 }
509
510 if (vp->file) {
511 flush_dcache_mmap_lock(vp->mapping);
512 vma_interval_tree_remove(vp->vma, &vp->mapping->i_mmap);
513 if (vp->adj_next)
514 vma_interval_tree_remove(vp->adj_next,
515 &vp->mapping->i_mmap);
516 }
517
518}
519
520/*
521 * vma_complete- Helper function for handling the unlocking after altering VMAs,
522 * or for inserting a VMA.
523 *
524 * @vp: The vma_prepare struct
525 * @vmi: The vma iterator
526 * @mm: The mm_struct
527 */
528static inline void vma_complete(struct vma_prepare *vp,
529 struct vma_iterator *vmi, struct mm_struct *mm)
530{
531 if (vp->file) {
532 if (vp->adj_next)
533 vma_interval_tree_insert(vp->adj_next,
534 &vp->mapping->i_mmap);
535 vma_interval_tree_insert(vp->vma, &vp->mapping->i_mmap);
536 flush_dcache_mmap_unlock(vp->mapping);
537 }
538
539 if (vp->remove && vp->file) {
540 __remove_shared_vm_struct(vp->remove, vp->file, vp->mapping);
541 if (vp->remove2)
542 __remove_shared_vm_struct(vp->remove2, vp->file,
543 vp->mapping);
544 } else if (vp->insert) {
545 /*
546 * split_vma has split insert from vma, and needs
547 * us to insert it before dropping the locks
548 * (it may either follow vma or precede it).
549 */
550 vma_iter_store(vmi, vp->insert);
551 mm->map_count++;
552 }
553
554 if (vp->anon_vma) {
555 anon_vma_interval_tree_post_update_vma(vp->vma);
556 if (vp->adj_next)
557 anon_vma_interval_tree_post_update_vma(vp->adj_next);
558 anon_vma_unlock_write(vp->anon_vma);
559 }
560
561 if (vp->file) {
562 i_mmap_unlock_write(vp->mapping);
563 uprobe_mmap(vp->vma);
564
565 if (vp->adj_next)
566 uprobe_mmap(vp->adj_next);
567 }
568
569 if (vp->remove) {
570again:
457f67be 571 vma_mark_detached(vp->remove, true);
440703e0
LH
572 if (vp->file) {
573 uprobe_munmap(vp->remove, vp->remove->vm_start,
574 vp->remove->vm_end);
575 fput(vp->file);
576 }
577 if (vp->remove->anon_vma)
578 anon_vma_merge(vp->vma, vp->remove);
579 mm->map_count--;
580 mpol_put(vma_policy(vp->remove));
581 if (!vp->remove2)
582 WARN_ON_ONCE(vp->vma->vm_end < vp->remove->vm_end);
583 vm_area_free(vp->remove);
584
585 /*
586 * In mprotect's case 6 (see comments on vma_merge),
5ff783f1 587 * we are removing both mid and next vmas
440703e0
LH
588 */
589 if (vp->remove2) {
590 vp->remove = vp->remove2;
591 vp->remove2 = NULL;
592 goto again;
593 }
594 }
595 if (vp->insert && vp->file)
596 uprobe_mmap(vp->insert);
2574d5e4 597 validate_mm(mm);
440703e0
LH
598}
599
04241ffe
LH
600/*
601 * dup_anon_vma() - Helper function to duplicate anon_vma
602 * @dst: The destination VMA
603 * @src: The source VMA
604 *
605 * Returns: 0 on success.
606 */
607static inline int dup_anon_vma(struct vm_area_struct *dst,
608 struct vm_area_struct *src)
609{
610 /*
611 * Easily overlooked: when mprotect shifts the boundary, make sure the
612 * expanding vma has anon_vma set if the shrinking vma had, to cover any
613 * anon pages imported.
614 */
615 if (src->anon_vma && !dst->anon_vma) {
d8ab9f7b 616 vma_start_write(dst);
04241ffe
LH
617 dst->anon_vma = src->anon_vma;
618 return anon_vma_clone(dst, src);
619 }
620
621 return 0;
622}
623
9303d3e1
LH
624/*
625 * vma_expand - Expand an existing VMA
626 *
627 * @vmi: The vma iterator
628 * @vma: The vma to expand
629 * @start: The start of the vma
630 * @end: The exclusive end of the vma
631 * @pgoff: The page offset of vma
632 * @next: The current of next vma.
633 *
634 * Expand @vma to @start and @end. Can expand off the start and end. Will
635 * expand over @next if it's different from @vma and @end == @next->vm_end.
636 * Checking if the @vma can expand and merge with @next needs to be handled by
637 * the caller.
638 *
639 * Returns: 0 on success
640 */
7c9813e8
LH
641int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
642 unsigned long start, unsigned long end, pgoff_t pgoff,
643 struct vm_area_struct *next)
9303d3e1 644{
68cefec5 645 bool remove_next = false;
9303d3e1
LH
646 struct vma_prepare vp;
647
9303d3e1 648 if (next && (vma != next) && (end == next->vm_end)) {
04241ffe 649 int ret;
9303d3e1 650
04241ffe
LH
651 remove_next = true;
652 ret = dup_anon_vma(vma, next);
653 if (ret)
654 return ret;
9303d3e1
LH
655 }
656
68cefec5 657 init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL);
9303d3e1
LH
658 /* Not merging but overwriting any part of next is not handled. */
659 VM_WARN_ON(next && !vp.remove &&
660 next != vma && end > next->vm_start);
661 /* Only handles expanding */
662 VM_WARN_ON(vma->vm_start < start || vma->vm_end > end);
663
664 if (vma_iter_prealloc(vmi))
665 goto nomem;
666
ccf1d78d 667 vma_prepare(&vp);
9303d3e1 668 vma_adjust_trans_huge(vma, start, end, 0);
9303d3e1
LH
669 /* VMA iterator points to previous, so set to start if necessary */
670 if (vma_iter_addr(vmi) != start)
671 vma_iter_set(vmi, start);
672
9303d3e1
LH
673 vma->vm_start = start;
674 vma->vm_end = end;
675 vma->vm_pgoff = pgoff;
676 /* Note: mas must be pointing to the expanding VMA */
677 vma_iter_store(vmi, vma);
678
679 vma_complete(&vp, vmi, vma->vm_mm);
9303d3e1
LH
680 return 0;
681
682nomem:
683 return -ENOMEM;
684}
cf51e86d
LH
685
686/*
687 * vma_shrink() - Reduce an existing VMAs memory area
688 * @vmi: The vma iterator
689 * @vma: The VMA to modify
690 * @start: The new start
691 * @end: The new end
692 *
693 * Returns: 0 on success, -ENOMEM otherwise
694 */
695int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma,
696 unsigned long start, unsigned long end, pgoff_t pgoff)
697{
698 struct vma_prepare vp;
699
700 WARN_ON((vma->vm_start != start) && (vma->vm_end != end));
701
702 if (vma_iter_prealloc(vmi))
703 return -ENOMEM;
704
705 init_vma_prep(&vp, vma);
cf51e86d 706 vma_prepare(&vp);
ccf1d78d 707 vma_adjust_trans_huge(vma, start, end, 0);
cf51e86d
LH
708
709 if (vma->vm_start < start)
710 vma_iter_clear(vmi, vma->vm_start, start);
711
712 if (vma->vm_end > end)
713 vma_iter_clear(vmi, end, vma->vm_end);
714
715 vma->vm_start = start;
716 vma->vm_end = end;
717 vma->vm_pgoff = pgoff;
718 vma_complete(&vp, vmi, vma->vm_mm);
cf51e86d
LH
719 return 0;
720}
721
1da177e4
LT
722/*
723 * If the vma has a ->close operation then the driver probably needs to release
714965ca
VB
724 * per-vma resources, so we don't attempt to merge those if the caller indicates
725 * the current vma may be removed as part of the merge.
1da177e4 726 */
2dbf4010
VB
727static inline bool is_mergeable_vma(struct vm_area_struct *vma,
728 struct file *file, unsigned long vm_flags,
729 struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
714965ca 730 struct anon_vma_name *anon_name, bool may_remove_vma)
1da177e4 731{
34228d47
CG
732 /*
733 * VM_SOFTDIRTY should not prevent from VMA merging, if we
734 * match the flags but dirty bit -- the caller should mark
735 * merged VMA as dirty. If dirty bit won't be excluded from
8bb4e7a2 736 * comparison, we increase pressure on the memory system forcing
34228d47
CG
737 * the kernel to generate new VMAs when old one could be
738 * extended instead.
739 */
740 if ((vma->vm_flags ^ vm_flags) & ~VM_SOFTDIRTY)
2dbf4010 741 return false;
1da177e4 742 if (vma->vm_file != file)
2dbf4010 743 return false;
714965ca 744 if (may_remove_vma && vma->vm_ops && vma->vm_ops->close)
2dbf4010 745 return false;
19a809af 746 if (!is_mergeable_vm_userfaultfd_ctx(vma, vm_userfaultfd_ctx))
2dbf4010 747 return false;
5c26f6ac 748 if (!anon_vma_name_eq(anon_vma_name(vma), anon_name))
2dbf4010
VB
749 return false;
750 return true;
1da177e4
LT
751}
752
2dbf4010
VB
753static inline bool is_mergeable_anon_vma(struct anon_vma *anon_vma1,
754 struct anon_vma *anon_vma2, struct vm_area_struct *vma)
1da177e4 755{
965f55de
SL
756 /*
757 * The list_is_singular() test is to avoid merging VMA cloned from
758 * parents. This can improve scalability caused by anon_vma lock.
759 */
760 if ((!anon_vma1 || !anon_vma2) && (!vma ||
761 list_is_singular(&vma->anon_vma_chain)))
2dbf4010 762 return true;
965f55de 763 return anon_vma1 == anon_vma2;
1da177e4
LT
764}
765
766/*
767 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
768 * in front of (at a lower virtual address and file offset than) the vma.
769 *
770 * We cannot merge two vmas if they have differently assigned (non-NULL)
771 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
772 *
773 * We don't check here for the merged mmap wrapping around the end of pagecache
45e55300 774 * indices (16TB on ia32) because do_mmap() does not permit mmap's which
1da177e4 775 * wrap, nor mmaps which cover the final page at index -1UL.
714965ca
VB
776 *
777 * We assume the vma may be removed as part of the merge.
1da177e4 778 */
2dbf4010 779static bool
1da177e4 780can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
2dbf4010
VB
781 struct anon_vma *anon_vma, struct file *file,
782 pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
783 struct anon_vma_name *anon_name)
1da177e4 784{
714965ca 785 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name, true) &&
965f55de 786 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
1da177e4 787 if (vma->vm_pgoff == vm_pgoff)
2dbf4010 788 return true;
1da177e4 789 }
2dbf4010 790 return false;
1da177e4
LT
791}
792
793/*
794 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
795 * beyond (at a higher virtual address and file offset than) the vma.
796 *
797 * We cannot merge two vmas if they have differently assigned (non-NULL)
798 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
714965ca
VB
799 *
800 * We assume that vma is not removed as part of the merge.
1da177e4 801 */
2dbf4010 802static bool
1da177e4 803can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
2dbf4010
VB
804 struct anon_vma *anon_vma, struct file *file,
805 pgoff_t vm_pgoff, struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
806 struct anon_vma_name *anon_name)
1da177e4 807{
714965ca 808 if (is_mergeable_vma(vma, file, vm_flags, vm_userfaultfd_ctx, anon_name, false) &&
965f55de 809 is_mergeable_anon_vma(anon_vma, vma->anon_vma, vma)) {
1da177e4 810 pgoff_t vm_pglen;
d6e93217 811 vm_pglen = vma_pages(vma);
1da177e4 812 if (vma->vm_pgoff + vm_pglen == vm_pgoff)
2dbf4010 813 return true;
1da177e4 814 }
2dbf4010 815 return false;
1da177e4
LT
816}
817
818/*
9a10064f
CC
819 * Given a mapping request (addr,end,vm_flags,file,pgoff,anon_name),
820 * figure out whether that can be merged with its predecessor or its
821 * successor. Or both (it neatly fills a hole).
1da177e4
LT
822 *
823 * In most cases - when called for mmap, brk or mremap - [addr,end) is
824 * certain not to be mapped by the time vma_merge is called; but when
825 * called for mprotect, it is certain to be already mapped (either at
826 * an offset within prev, or at the start of next), and the flags of
827 * this area are about to be changed to vm_flags - and the no-change
828 * case has already been eliminated.
829 *
fcfccd91 830 * The following mprotect cases have to be considered, where **** is
1da177e4 831 * the area passed down from mprotect_fixup, never extending beyond one
fcfccd91
LS
832 * vma, PPPP is the previous vma, CCCC is a concurrent vma that starts
833 * at the same address as **** and is of the same or larger span, and
834 * NNNN the next vma after ****:
1da177e4 835 *
fcfccd91
LS
836 * **** **** ****
837 * PPPPPPNNNNNN PPPPPPNNNNNN PPPPPPCCCCCC
5d42ab29 838 * cannot merge might become might become
fcfccd91 839 * PPNNNNNNNNNN PPPPPPPPPPCC
5d42ab29
WY
840 * mmap, brk or case 4 below case 5 below
841 * mremap move:
fcfccd91
LS
842 * **** ****
843 * PPPP NNNN PPPPCCCCNNNN
5d42ab29
WY
844 * might become might become
845 * PPPPPPPPPPPP 1 or PPPPPPPPPPPP 6 or
fcfccd91
LS
846 * PPPPPPPPNNNN 2 or PPPPPPPPNNNN 7 or
847 * PPPPNNNNNNNN 3 PPPPNNNNNNNN 8
1da177e4 848 *
fcfccd91
LS
849 * It is important for case 8 that the vma CCCC overlapping the
850 * region **** is never going to extended over NNNN. Instead NNNN must
851 * be extended in region **** and CCCC must be removed. This way in
0503ea8f 852 * all cases where vma_merge succeeds, the moment vma_merge drops the
e86f15ee
AA
853 * rmap_locks, the properties of the merged vma will be already
854 * correct for the whole merged range. Some of those properties like
855 * vm_page_prot/vm_flags may be accessed by rmap_walks and they must
856 * be correct for the whole merged range immediately after the
fcfccd91
LS
857 * rmap_locks are released. Otherwise if NNNN would be removed and
858 * CCCC would be extended over the NNNN range, remove_migration_ptes
e86f15ee 859 * or other rmap walkers (if working on addresses beyond the "end"
fcfccd91
LS
860 * parameter) may establish ptes with the wrong permissions of CCCC
861 * instead of the right permissions of NNNN.
0503ea8f
LH
862 *
863 * In the code below:
864 * PPPP is represented by *prev
fcfccd91
LS
865 * CCCC is represented by *curr or not represented at all (NULL)
866 * NNNN is represented by *next or not represented at all (NULL)
867 * **** is not represented - it will be merged and the vma containing the
9e8a39d2 868 * area is returned, or the function will return NULL
1da177e4 869 */
9760ebff 870struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm,
1da177e4
LT
871 struct vm_area_struct *prev, unsigned long addr,
872 unsigned long end, unsigned long vm_flags,
cc71aba3 873 struct anon_vma *anon_vma, struct file *file,
19a809af 874 pgoff_t pgoff, struct mempolicy *policy,
9a10064f 875 struct vm_userfaultfd_ctx vm_userfaultfd_ctx,
5c26f6ac 876 struct anon_vma_name *anon_name)
1da177e4 877{
b0729ae0 878 struct vm_area_struct *curr, *next, *res;
0503ea8f 879 struct vm_area_struct *vma, *adjust, *remove, *remove2;
0173db4f
LS
880 struct vma_prepare vp;
881 pgoff_t vma_pgoff;
882 int err = 0;
eef19944
JM
883 bool merge_prev = false;
884 bool merge_next = false;
0503ea8f 885 bool vma_expanded = false;
0503ea8f 886 unsigned long vma_start = addr;
0503ea8f 887 unsigned long vma_end = end;
0173db4f 888 pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
1e76454f 889 long adj_start = 0;
1da177e4
LT
890
891 /*
892 * We later require that vma->vm_flags == vm_flags,
893 * so this tests vma->vm_flags & VM_SPECIAL, too.
894 */
895 if (vm_flags & VM_SPECIAL)
896 return NULL;
897
00cd00a6
LS
898 /* Does the input range span an existing VMA? (cases 5 - 8) */
899 curr = find_vma_intersection(mm, prev ? prev->vm_end : 0, end);
1da177e4 900
00cd00a6
LS
901 if (!curr || /* cases 1 - 4 */
902 end == curr->vm_end) /* cases 6 - 8, adjacent VMA */
903 next = vma_lookup(mm, end);
904 else
905 next = NULL; /* case 5 */
e86f15ee 906
0503ea8f 907 if (prev) {
0503ea8f
LH
908 vma_start = prev->vm_start;
909 vma_pgoff = prev->vm_pgoff;
0173db4f 910
0503ea8f 911 /* Can we merge the predecessor? */
0173db4f 912 if (addr == prev->vm_end && mpol_equal(vma_policy(prev), policy)
0503ea8f 913 && can_vma_merge_after(prev, vm_flags, anon_vma, file,
0173db4f 914 pgoff, vm_userfaultfd_ctx, anon_name)) {
0503ea8f 915 merge_prev = true;
18b098af 916 vma_prev(vmi);
0503ea8f 917 }
1da177e4 918 }
b0729ae0 919
eef19944 920 /* Can we merge the successor? */
00cd00a6 921 if (next && mpol_equal(policy, vma_policy(next)) &&
0173db4f 922 can_vma_merge_before(next, vm_flags, anon_vma, file, pgoff+pglen,
00cd00a6 923 vm_userfaultfd_ctx, anon_name)) {
eef19944
JM
924 merge_next = true;
925 }
0503ea8f 926
29417d29
LS
927 /* Verify some invariant that must be enforced by the caller. */
928 VM_WARN_ON(prev && addr <= prev->vm_start);
929 VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end));
930 VM_WARN_ON(addr >= end);
931
0173db4f
LS
932 if (!merge_prev && !merge_next)
933 return NULL; /* Not mergeable. */
934
935 res = vma = prev;
0503ea8f 936 remove = remove2 = adjust = NULL;
0173db4f 937
eef19944
JM
938 /* Can we merge both the predecessor and the successor? */
939 if (merge_prev && merge_next &&
0503ea8f 940 is_mergeable_anon_vma(prev->anon_vma, next->anon_vma, NULL)) {
5ff783f1 941 remove = next; /* case 1 */
0503ea8f 942 vma_end = next->vm_end;
5ff783f1 943 err = dup_anon_vma(prev, next);
fcfccd91
LS
944 if (curr) { /* case 6 */
945 remove = curr;
0503ea8f 946 remove2 = next;
5ff783f1 947 if (!next->anon_vma)
fcfccd91 948 err = dup_anon_vma(prev, curr);
0503ea8f 949 }
0173db4f 950 } else if (merge_prev) { /* case 2 */
fcfccd91
LS
951 if (curr) {
952 err = dup_anon_vma(prev, curr);
953 if (end == curr->vm_end) { /* case 7 */
954 remove = curr;
0503ea8f 955 } else { /* case 5 */
fcfccd91
LS
956 adjust = curr;
957 adj_start = (end - curr->vm_start);
0503ea8f
LH
958 }
959 }
0173db4f 960 } else { /* merge_next */
eef19944 961 res = next;
0503ea8f
LH
962 if (prev && addr < prev->vm_end) { /* case 4 */
963 vma_end = addr;
183b7a60 964 adjust = next;
1e76454f 965 adj_start = -(prev->vm_end - addr);
183b7a60 966 err = dup_anon_vma(next, prev);
0503ea8f 967 } else {
b0729ae0
LS
968 /*
969 * Note that cases 3 and 8 are the ONLY ones where prev
970 * is permitted to be (but is not necessarily) NULL.
971 */
0503ea8f
LH
972 vma = next; /* case 3 */
973 vma_start = addr;
974 vma_end = next->vm_end;
7e775787 975 vma_pgoff = next->vm_pgoff - pglen;
fcfccd91
LS
976 if (curr) { /* case 8 */
977 vma_pgoff = curr->vm_pgoff;
978 remove = curr;
979 err = dup_anon_vma(next, curr);
0503ea8f
LH
980 }
981 }
1da177e4
LT
982 }
983
0173db4f 984 /* Error in anon_vma clone. */
eef19944
JM
985 if (err)
986 return NULL;
0503ea8f
LH
987
988 if (vma_iter_prealloc(vmi))
989 return NULL;
990
0503ea8f
LH
991 init_multi_vma_prep(&vp, vma, adjust, remove, remove2);
992 VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma &&
993 vp.anon_vma != adjust->anon_vma);
994
995 vma_prepare(&vp);
ccf1d78d 996 vma_adjust_trans_huge(vma, vma_start, vma_end, adj_start);
0503ea8f
LH
997 if (vma_start < vma->vm_start || vma_end > vma->vm_end)
998 vma_expanded = true;
999
1000 vma->vm_start = vma_start;
1001 vma->vm_end = vma_end;
1002 vma->vm_pgoff = vma_pgoff;
1003
1004 if (vma_expanded)
1005 vma_iter_store(vmi, vma);
1006
1e76454f
VB
1007 if (adj_start) {
1008 adjust->vm_start += adj_start;
1009 adjust->vm_pgoff += adj_start >> PAGE_SHIFT;
1010 if (adj_start < 0) {
0503ea8f
LH
1011 WARN_ON(vma_expanded);
1012 vma_iter_store(vmi, next);
1013 }
1014 }
1015
1016 vma_complete(&vp, vmi, mm);
eef19944 1017 khugepaged_enter_vma(res, vm_flags);
9760ebff 1018 return res;
f2ebfe43
LH
1019}
1020
d0e9fe17 1021/*
b4f315b4 1022 * Rough compatibility check to quickly see if it's even worth looking
d0e9fe17
LT
1023 * at sharing an anon_vma.
1024 *
1025 * They need to have the same vm_file, and the flags can only differ
1026 * in things that mprotect may change.
1027 *
1028 * NOTE! The fact that we share an anon_vma doesn't _have_ to mean that
1029 * we can merge the two vma's. For example, we refuse to merge a vma if
1030 * there is a vm_ops->close() function, because that indicates that the
1031 * driver is doing some kind of reference counting. But that doesn't
1032 * really matter for the anon_vma sharing case.
1033 */
1034static int anon_vma_compatible(struct vm_area_struct *a, struct vm_area_struct *b)
1035{
1036 return a->vm_end == b->vm_start &&
1037 mpol_equal(vma_policy(a), vma_policy(b)) &&
1038 a->vm_file == b->vm_file &&
6cb4d9a2 1039 !((a->vm_flags ^ b->vm_flags) & ~(VM_ACCESS_FLAGS | VM_SOFTDIRTY)) &&
d0e9fe17
LT
1040 b->vm_pgoff == a->vm_pgoff + ((b->vm_start - a->vm_start) >> PAGE_SHIFT);
1041}
1042
1043/*
1044 * Do some basic sanity checking to see if we can re-use the anon_vma
1045 * from 'old'. The 'a'/'b' vma's are in VM order - one of them will be
1046 * the same as 'old', the other will be the new one that is trying
1047 * to share the anon_vma.
1048 *
5b449489 1049 * NOTE! This runs with mmap_lock held for reading, so it is possible that
d0e9fe17
LT
1050 * the anon_vma of 'old' is concurrently in the process of being set up
1051 * by another page fault trying to merge _that_. But that's ok: if it
1052 * is being set up, that automatically means that it will be a singleton
1053 * acceptable for merging, so we can do all of this optimistically. But
4db0c3c2 1054 * we do that READ_ONCE() to make sure that we never re-load the pointer.
d0e9fe17
LT
1055 *
1056 * IOW: that the "list_is_singular()" test on the anon_vma_chain only
1057 * matters for the 'stable anon_vma' case (ie the thing we want to avoid
1058 * is to return an anon_vma that is "complex" due to having gone through
1059 * a fork).
1060 *
1061 * We also make sure that the two vma's are compatible (adjacent,
1062 * and with the same memory policies). That's all stable, even with just
5b449489 1063 * a read lock on the mmap_lock.
d0e9fe17
LT
1064 */
1065static struct anon_vma *reusable_anon_vma(struct vm_area_struct *old, struct vm_area_struct *a, struct vm_area_struct *b)
1066{
1067 if (anon_vma_compatible(a, b)) {
4db0c3c2 1068 struct anon_vma *anon_vma = READ_ONCE(old->anon_vma);
d0e9fe17
LT
1069
1070 if (anon_vma && list_is_singular(&old->anon_vma_chain))
1071 return anon_vma;
1072 }
1073 return NULL;
1074}
1075
1da177e4
LT
1076/*
1077 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
1078 * neighbouring vmas for a suitable anon_vma, before it goes off
1079 * to allocate a new anon_vma. It checks because a repetitive
1080 * sequence of mprotects and faults may otherwise lead to distinct
1081 * anon_vmas being allocated, preventing vma merge in subsequent
1082 * mprotect.
1083 */
1084struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
1085{
763ecb03 1086 MA_STATE(mas, &vma->vm_mm->mm_mt, vma->vm_end, vma->vm_end);
a67c8caa 1087 struct anon_vma *anon_vma = NULL;
763ecb03 1088 struct vm_area_struct *prev, *next;
a67c8caa
ML
1089
1090 /* Try next first. */
763ecb03
LH
1091 next = mas_walk(&mas);
1092 if (next) {
1093 anon_vma = reusable_anon_vma(next, vma, next);
a67c8caa
ML
1094 if (anon_vma)
1095 return anon_vma;
1096 }
1097
763ecb03
LH
1098 prev = mas_prev(&mas, 0);
1099 VM_BUG_ON_VMA(prev != vma, vma);
1100 prev = mas_prev(&mas, 0);
a67c8caa 1101 /* Try prev next. */
763ecb03
LH
1102 if (prev)
1103 anon_vma = reusable_anon_vma(prev, prev, vma);
a67c8caa 1104
1da177e4 1105 /*
a67c8caa
ML
1106 * We might reach here with anon_vma == NULL if we can't find
1107 * any reusable anon_vma.
1da177e4
LT
1108 * There's no absolute need to look only at touching neighbours:
1109 * we could search further afield for "compatible" anon_vmas.
1110 * But it would probably just be a waste of time searching,
1111 * or lead to too many vmas hanging off the same anon_vma.
1112 * We're trying to allow mprotect remerging later on,
1113 * not trying to minimize memory used for anon_vmas.
1114 */
a67c8caa 1115 return anon_vma;
1da177e4
LT
1116}
1117
40401530
AV
1118/*
1119 * If a hint addr is less than mmap_min_addr change hint to be as
1120 * low as possible but still greater than mmap_min_addr
1121 */
1122static inline unsigned long round_hint_to_min(unsigned long hint)
1123{
1124 hint &= PAGE_MASK;
1125 if (((void *)hint != NULL) &&
1126 (hint < mmap_min_addr))
1127 return PAGE_ALIGN(mmap_min_addr);
1128 return hint;
1129}
1130
b0cc5e89 1131bool mlock_future_ok(struct mm_struct *mm, unsigned long flags,
3c54a298 1132 unsigned long bytes)
363ee17f 1133{
3c54a298 1134 unsigned long locked_pages, limit_pages;
363ee17f 1135
3c54a298
LS
1136 if (!(flags & VM_LOCKED) || capable(CAP_IPC_LOCK))
1137 return true;
1138
1139 locked_pages = bytes >> PAGE_SHIFT;
1140 locked_pages += mm->locked_vm;
1141
1142 limit_pages = rlimit(RLIMIT_MEMLOCK);
1143 limit_pages >>= PAGE_SHIFT;
1144
1145 return locked_pages <= limit_pages;
363ee17f
DB
1146}
1147
be83bbf8
LT
1148static inline u64 file_mmap_size_max(struct file *file, struct inode *inode)
1149{
1150 if (S_ISREG(inode->i_mode))
423913ad 1151 return MAX_LFS_FILESIZE;
be83bbf8
LT
1152
1153 if (S_ISBLK(inode->i_mode))
1154 return MAX_LFS_FILESIZE;
1155
76f34950
IK
1156 if (S_ISSOCK(inode->i_mode))
1157 return MAX_LFS_FILESIZE;
1158
be83bbf8
LT
1159 /* Special "we do even unsigned file positions" case */
1160 if (file->f_mode & FMODE_UNSIGNED_OFFSET)
1161 return 0;
1162
1163 /* Yes, random drivers might want more. But I'm tired of buggy drivers */
1164 return ULONG_MAX;
1165}
1166
1167static inline bool file_mmap_ok(struct file *file, struct inode *inode,
1168 unsigned long pgoff, unsigned long len)
1169{
1170 u64 maxsize = file_mmap_size_max(file, inode);
1171
1172 if (maxsize && len > maxsize)
1173 return false;
1174 maxsize -= len;
1175 if (pgoff > maxsize >> PAGE_SHIFT)
1176 return false;
1177 return true;
1178}
1179
1da177e4 1180/*
3e4e28c5 1181 * The caller must write-lock current->mm->mmap_lock.
1da177e4 1182 */
1fcfd8db 1183unsigned long do_mmap(struct file *file, unsigned long addr,
1da177e4 1184 unsigned long len, unsigned long prot,
45e55300
PC
1185 unsigned long flags, unsigned long pgoff,
1186 unsigned long *populate, struct list_head *uf)
1da177e4 1187{
cc71aba3 1188 struct mm_struct *mm = current->mm;
45e55300 1189 vm_flags_t vm_flags;
62b5f7d0 1190 int pkey = 0;
1da177e4 1191
41badc15 1192 *populate = 0;
bebeb3d6 1193
e37609bb
PK
1194 if (!len)
1195 return -EINVAL;
1196
1da177e4
LT
1197 /*
1198 * Does the application expect PROT_READ to imply PROT_EXEC?
1199 *
1200 * (the exception is when the underlying filesystem is noexec
1201 * mounted, in which case we dont add PROT_EXEC.)
1202 */
1203 if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
90f8572b 1204 if (!(file && path_noexec(&file->f_path)))
1da177e4
LT
1205 prot |= PROT_EXEC;
1206
a4ff8e86
MH
1207 /* force arch specific MAP_FIXED handling in get_unmapped_area */
1208 if (flags & MAP_FIXED_NOREPLACE)
1209 flags |= MAP_FIXED;
1210
7cd94146
EP
1211 if (!(flags & MAP_FIXED))
1212 addr = round_hint_to_min(addr);
1213
1da177e4
LT
1214 /* Careful about overflows.. */
1215 len = PAGE_ALIGN(len);
9206de95 1216 if (!len)
1da177e4
LT
1217 return -ENOMEM;
1218
1219 /* offset overflow? */
1220 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
cc71aba3 1221 return -EOVERFLOW;
1da177e4
LT
1222
1223 /* Too many mappings? */
1224 if (mm->map_count > sysctl_max_map_count)
1225 return -ENOMEM;
1226
1227 /* Obtain the address to map to. we verify (or select) it and ensure
1228 * that it represents a valid section of the address space.
1229 */
1230 addr = get_unmapped_area(file, addr, len, pgoff, flags);
ff68dac6 1231 if (IS_ERR_VALUE(addr))
1da177e4
LT
1232 return addr;
1233
a4ff8e86 1234 if (flags & MAP_FIXED_NOREPLACE) {
35e43c5f 1235 if (find_vma_intersection(mm, addr, addr + len))
a4ff8e86
MH
1236 return -EEXIST;
1237 }
1238
62b5f7d0
DH
1239 if (prot == PROT_EXEC) {
1240 pkey = execute_only_pkey(mm);
1241 if (pkey < 0)
1242 pkey = 0;
1243 }
1244
1da177e4
LT
1245 /* Do simple checking here so the lower-level routines won't have
1246 * to. we assume access permissions have been handled by the open
1247 * of the memory object, so we don't do any here.
1248 */
45e55300 1249 vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
1da177e4
LT
1250 mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1251
cdf7b341 1252 if (flags & MAP_LOCKED)
1da177e4
LT
1253 if (!can_do_mlock())
1254 return -EPERM;
ba470de4 1255
b0cc5e89 1256 if (!mlock_future_ok(mm, vm_flags, len))
363ee17f 1257 return -EAGAIN;
1da177e4 1258
1da177e4 1259 if (file) {
077bf22b 1260 struct inode *inode = file_inode(file);
1c972597
DW
1261 unsigned long flags_mask;
1262
be83bbf8
LT
1263 if (!file_mmap_ok(file, inode, pgoff, len))
1264 return -EOVERFLOW;
1265
1c972597 1266 flags_mask = LEGACY_MAP_MASK | file->f_op->mmap_supported_flags;
077bf22b 1267
1da177e4
LT
1268 switch (flags & MAP_TYPE) {
1269 case MAP_SHARED:
1c972597
DW
1270 /*
1271 * Force use of MAP_SHARED_VALIDATE with non-legacy
1272 * flags. E.g. MAP_SYNC is dangerous to use with
1273 * MAP_SHARED as you don't know which consistency model
1274 * you will get. We silently ignore unsupported flags
1275 * with MAP_SHARED to preserve backward compatibility.
1276 */
1277 flags &= LEGACY_MAP_MASK;
e4a9bc58 1278 fallthrough;
1c972597
DW
1279 case MAP_SHARED_VALIDATE:
1280 if (flags & ~flags_mask)
1281 return -EOPNOTSUPP;
dc617f29
DW
1282 if (prot & PROT_WRITE) {
1283 if (!(file->f_mode & FMODE_WRITE))
1284 return -EACCES;
1285 if (IS_SWAPFILE(file->f_mapping->host))
1286 return -ETXTBSY;
1287 }
1da177e4
LT
1288
1289 /*
1290 * Make sure we don't allow writing to an append-only
1291 * file..
1292 */
1293 if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
1294 return -EACCES;
1295
1da177e4
LT
1296 vm_flags |= VM_SHARED | VM_MAYSHARE;
1297 if (!(file->f_mode & FMODE_WRITE))
1298 vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
e4a9bc58 1299 fallthrough;
1da177e4
LT
1300 case MAP_PRIVATE:
1301 if (!(file->f_mode & FMODE_READ))
1302 return -EACCES;
90f8572b 1303 if (path_noexec(&file->f_path)) {
80c5606c
LT
1304 if (vm_flags & VM_EXEC)
1305 return -EPERM;
1306 vm_flags &= ~VM_MAYEXEC;
1307 }
80c5606c 1308
72c2d531 1309 if (!file->f_op->mmap)
80c5606c 1310 return -ENODEV;
b2c56e4f
ON
1311 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1312 return -EINVAL;
1da177e4
LT
1313 break;
1314
1315 default:
1316 return -EINVAL;
1317 }
1318 } else {
1319 switch (flags & MAP_TYPE) {
1320 case MAP_SHARED:
b2c56e4f
ON
1321 if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
1322 return -EINVAL;
ce363942
TH
1323 /*
1324 * Ignore pgoff.
1325 */
1326 pgoff = 0;
1da177e4
LT
1327 vm_flags |= VM_SHARED | VM_MAYSHARE;
1328 break;
1329 case MAP_PRIVATE:
1330 /*
1331 * Set pgoff according to addr for anon_vma.
1332 */
1333 pgoff = addr >> PAGE_SHIFT;
1334 break;
1335 default:
1336 return -EINVAL;
1337 }
1338 }
1339
c22c0d63
ML
1340 /*
1341 * Set 'VM_NORESERVE' if we should not account for the
1342 * memory use of this mapping.
1343 */
1344 if (flags & MAP_NORESERVE) {
1345 /* We honor MAP_NORESERVE if allowed to overcommit */
1346 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1347 vm_flags |= VM_NORESERVE;
1348
1349 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1350 if (file && is_file_hugepages(file))
1351 vm_flags |= VM_NORESERVE;
1352 }
1353
897ab3e0 1354 addr = mmap_region(file, addr, len, vm_flags, pgoff, uf);
09a9f1d2
ML
1355 if (!IS_ERR_VALUE(addr) &&
1356 ((vm_flags & VM_LOCKED) ||
1357 (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
41badc15 1358 *populate = len;
bebeb3d6 1359 return addr;
0165ab44 1360}
6be5ceb0 1361
a90f590a
DB
1362unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1363 unsigned long prot, unsigned long flags,
1364 unsigned long fd, unsigned long pgoff)
66f0dc48
HD
1365{
1366 struct file *file = NULL;
1e3ee14b 1367 unsigned long retval;
66f0dc48
HD
1368
1369 if (!(flags & MAP_ANONYMOUS)) {
120a795d 1370 audit_mmap_fd(fd, flags);
66f0dc48
HD
1371 file = fget(fd);
1372 if (!file)
1e3ee14b 1373 return -EBADF;
7bba8f0e 1374 if (is_file_hugepages(file)) {
af73e4d9 1375 len = ALIGN(len, huge_page_size(hstate_file(file)));
7bba8f0e
ZL
1376 } else if (unlikely(flags & MAP_HUGETLB)) {
1377 retval = -EINVAL;
493af578 1378 goto out_fput;
7bba8f0e 1379 }
66f0dc48 1380 } else if (flags & MAP_HUGETLB) {
c103a4dc 1381 struct hstate *hs;
af73e4d9 1382
20ac2893 1383 hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
091d0d55
LZ
1384 if (!hs)
1385 return -EINVAL;
1386
1387 len = ALIGN(len, huge_page_size(hs));
66f0dc48
HD
1388 /*
1389 * VM_NORESERVE is used because the reservations will be
1390 * taken when vm_ops->mmap() is called
66f0dc48 1391 */
af73e4d9 1392 file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
42d7395f 1393 VM_NORESERVE,
83c1fd76 1394 HUGETLB_ANONHUGE_INODE,
42d7395f 1395 (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
66f0dc48
HD
1396 if (IS_ERR(file))
1397 return PTR_ERR(file);
1398 }
1399
9fbeb5ab 1400 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
493af578 1401out_fput:
66f0dc48
HD
1402 if (file)
1403 fput(file);
66f0dc48
HD
1404 return retval;
1405}
1406
a90f590a
DB
1407SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1408 unsigned long, prot, unsigned long, flags,
1409 unsigned long, fd, unsigned long, pgoff)
1410{
1411 return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1412}
1413
a4679373
CH
1414#ifdef __ARCH_WANT_SYS_OLD_MMAP
1415struct mmap_arg_struct {
1416 unsigned long addr;
1417 unsigned long len;
1418 unsigned long prot;
1419 unsigned long flags;
1420 unsigned long fd;
1421 unsigned long offset;
1422};
1423
1424SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1425{
1426 struct mmap_arg_struct a;
1427
1428 if (copy_from_user(&a, arg, sizeof(a)))
1429 return -EFAULT;
de1741a1 1430 if (offset_in_page(a.offset))
a4679373
CH
1431 return -EINVAL;
1432
a90f590a
DB
1433 return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1434 a.offset >> PAGE_SHIFT);
a4679373
CH
1435}
1436#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1437
54cbbbf3
LS
1438static bool vm_ops_needs_writenotify(const struct vm_operations_struct *vm_ops)
1439{
1440 return vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite);
1441}
1442
1443static bool vma_is_shared_writable(struct vm_area_struct *vma)
1444{
1445 return (vma->vm_flags & (VM_WRITE | VM_SHARED)) ==
1446 (VM_WRITE | VM_SHARED);
1447}
1448
1449static bool vma_fs_can_writeback(struct vm_area_struct *vma)
1450{
1451 /* No managed pages to writeback. */
1452 if (vma->vm_flags & VM_PFNMAP)
1453 return false;
1454
1455 return vma->vm_file && vma->vm_file->f_mapping &&
1456 mapping_can_writeback(vma->vm_file->f_mapping);
1457}
1458
1459/*
1460 * Does this VMA require the underlying folios to have their dirty state
1461 * tracked?
1462 */
1463bool vma_needs_dirty_tracking(struct vm_area_struct *vma)
1464{
1465 /* Only shared, writable VMAs require dirty tracking. */
1466 if (!vma_is_shared_writable(vma))
1467 return false;
1468
1469 /* Does the filesystem need to be notified? */
1470 if (vm_ops_needs_writenotify(vma->vm_ops))
1471 return true;
1472
1473 /*
1474 * Even if the filesystem doesn't indicate a need for writenotify, if it
1475 * can writeback, dirty tracking is still required.
1476 */
1477 return vma_fs_can_writeback(vma);
1478}
1479
4e950f6f 1480/*
8bb4e7a2 1481 * Some shared mappings will want the pages marked read-only
4e950f6f
AD
1482 * to track write events. If so, we'll downgrade vm_page_prot
1483 * to the private version (using protection_map[] without the
1484 * VM_SHARED bit).
1485 */
6d2329f8 1486int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot)
4e950f6f 1487{
4e950f6f 1488 /* If it was private or non-writable, the write bit is already clear */
54cbbbf3 1489 if (!vma_is_shared_writable(vma))
4e950f6f
AD
1490 return 0;
1491
1492 /* The backer wishes to know when pages are first written to? */
54cbbbf3 1493 if (vm_ops_needs_writenotify(vma->vm_ops))
4e950f6f
AD
1494 return 1;
1495
64e45507
PF
1496 /* The open routine did something to the protections that pgprot_modify
1497 * won't preserve? */
6d2329f8 1498 if (pgprot_val(vm_page_prot) !=
54cbbbf3 1499 pgprot_val(vm_pgprot_modify(vm_page_prot, vma->vm_flags)))
4e950f6f
AD
1500 return 0;
1501
f96f7a40
DH
1502 /*
1503 * Do we need to track softdirty? hugetlb does not support softdirty
1504 * tracking yet.
1505 */
1506 if (vma_soft_dirty_enabled(vma) && !is_vm_hugetlb_page(vma))
64e45507
PF
1507 return 1;
1508
51d3d5eb
DH
1509 /* Do we need write faults for uffd-wp tracking? */
1510 if (userfaultfd_wp(vma))
1511 return 1;
1512
4e950f6f 1513 /* Can the mapping track the dirty pages? */
54cbbbf3 1514 return vma_fs_can_writeback(vma);
4e950f6f
AD
1515}
1516
fc8744ad
LT
1517/*
1518 * We account for memory if it's a private writeable mapping,
5a6fe125 1519 * not hugepages and VM_NORESERVE wasn't set.
fc8744ad 1520 */
ca16d140 1521static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)
fc8744ad 1522{
5a6fe125
MG
1523 /*
1524 * hugetlb has its own accounting separate from the core VM
1525 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1526 */
1527 if (file && is_file_hugepages(file))
1528 return 0;
1529
fc8744ad
LT
1530 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1531}
1532
3499a131
LH
1533/**
1534 * unmapped_area() - Find an area between the low_limit and the high_limit with
1535 * the correct alignment and offset, all from @info. Note: current->mm is used
1536 * for the search.
1537 *
82b24936
VY
1538 * @info: The unmapped area information including the range [low_limit -
1539 * high_limit), the alignment offset and mask.
3499a131
LH
1540 *
1541 * Return: A memory address or -ENOMEM.
1542 */
baceaf1c 1543static unsigned long unmapped_area(struct vm_unmapped_area_info *info)
db4fbfb9 1544{
6b008640
LT
1545 unsigned long length, gap;
1546 unsigned long low_limit, high_limit;
58c5d0d6 1547 struct vm_area_struct *tmp;
db4fbfb9 1548
3499a131 1549 MA_STATE(mas, &current->mm->mm_mt, 0, 0);
db4fbfb9
ML
1550
1551 /* Adjust search length to account for worst case alignment overhead */
1552 length = info->length + info->align_mask;
1553 if (length < info->length)
1554 return -ENOMEM;
1555
58c5d0d6 1556 low_limit = info->low_limit;
6b008640
LT
1557 if (low_limit < mmap_min_addr)
1558 low_limit = mmap_min_addr;
1559 high_limit = info->high_limit;
58c5d0d6 1560retry:
6b008640 1561 if (mas_empty_area(&mas, low_limit, high_limit - 1, length))
db4fbfb9
ML
1562 return -ENOMEM;
1563
3499a131
LH
1564 gap = mas.index;
1565 gap += (info->align_offset - gap) & info->align_mask;
58c5d0d6
LH
1566 tmp = mas_next(&mas, ULONG_MAX);
1567 if (tmp && (tmp->vm_flags & VM_GROWSDOWN)) { /* Avoid prev check if possible */
1568 if (vm_start_gap(tmp) < gap + length - 1) {
1569 low_limit = tmp->vm_end;
1570 mas_reset(&mas);
1571 goto retry;
1572 }
1573 } else {
1574 tmp = mas_prev(&mas, 0);
1575 if (tmp && vm_end_gap(tmp) > gap) {
1576 low_limit = vm_end_gap(tmp);
1577 mas_reset(&mas);
1578 goto retry;
1579 }
1580 }
1581
3499a131 1582 return gap;
db4fbfb9
ML
1583}
1584
3499a131
LH
1585/**
1586 * unmapped_area_topdown() - Find an area between the low_limit and the
82b24936 1587 * high_limit with the correct alignment and offset at the highest available
3499a131
LH
1588 * address, all from @info. Note: current->mm is used for the search.
1589 *
82b24936
VY
1590 * @info: The unmapped area information including the range [low_limit -
1591 * high_limit), the alignment offset and mask.
3499a131
LH
1592 *
1593 * Return: A memory address or -ENOMEM.
1594 */
baceaf1c 1595static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)
db4fbfb9 1596{
6b008640
LT
1597 unsigned long length, gap, gap_end;
1598 unsigned long low_limit, high_limit;
58c5d0d6 1599 struct vm_area_struct *tmp;
db4fbfb9 1600
3499a131 1601 MA_STATE(mas, &current->mm->mm_mt, 0, 0);
db4fbfb9
ML
1602 /* Adjust search length to account for worst case alignment overhead */
1603 length = info->length + info->align_mask;
1604 if (length < info->length)
1605 return -ENOMEM;
1606
6b008640
LT
1607 low_limit = info->low_limit;
1608 if (low_limit < mmap_min_addr)
1609 low_limit = mmap_min_addr;
58c5d0d6
LH
1610 high_limit = info->high_limit;
1611retry:
6b008640 1612 if (mas_empty_area_rev(&mas, low_limit, high_limit - 1, length))
db4fbfb9 1613 return -ENOMEM;
db4fbfb9 1614
3499a131
LH
1615 gap = mas.last + 1 - info->length;
1616 gap -= (gap - info->align_offset) & info->align_mask;
58c5d0d6
LH
1617 gap_end = mas.last;
1618 tmp = mas_next(&mas, ULONG_MAX);
1619 if (tmp && (tmp->vm_flags & VM_GROWSDOWN)) { /* Avoid prev check if possible */
1620 if (vm_start_gap(tmp) <= gap_end) {
1621 high_limit = vm_start_gap(tmp);
1622 mas_reset(&mas);
1623 goto retry;
1624 }
1625 } else {
1626 tmp = mas_prev(&mas, 0);
1627 if (tmp && vm_end_gap(tmp) > gap) {
1628 high_limit = tmp->vm_start;
1629 mas_reset(&mas);
1630 goto retry;
1631 }
1632 }
1633
3499a131 1634 return gap;
db4fbfb9
ML
1635}
1636
baceaf1c
JK
1637/*
1638 * Search for an unmapped address range.
1639 *
1640 * We are looking for a range that:
1641 * - does not intersect with any VMA;
1642 * - is contained within the [low_limit, high_limit) interval;
1643 * - is at least the desired size.
1644 * - satisfies (begin_addr & align_mask) == (align_offset & align_mask)
1645 */
1646unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info)
1647{
df529cab
JK
1648 unsigned long addr;
1649
baceaf1c 1650 if (info->flags & VM_UNMAPPED_AREA_TOPDOWN)
df529cab 1651 addr = unmapped_area_topdown(info);
baceaf1c 1652 else
df529cab
JK
1653 addr = unmapped_area(info);
1654
1655 trace_vm_unmapped_area(addr, info);
1656 return addr;
baceaf1c 1657}
f6795053 1658
1da177e4
LT
1659/* Get an address range which is currently unmapped.
1660 * For shmat() with addr=0.
1661 *
1662 * Ugly calling convention alert:
1663 * Return value with the low bits set means error value,
1664 * ie
1665 * if (ret & ~PAGE_MASK)
1666 * error = ret;
1667 *
1668 * This function "knows" that -ENOMEM has the bits set.
1669 */
1da177e4 1670unsigned long
4b439e25
CL
1671generic_get_unmapped_area(struct file *filp, unsigned long addr,
1672 unsigned long len, unsigned long pgoff,
1673 unsigned long flags)
1da177e4
LT
1674{
1675 struct mm_struct *mm = current->mm;
1be7107f 1676 struct vm_area_struct *vma, *prev;
db4fbfb9 1677 struct vm_unmapped_area_info info;
2cb4de08 1678 const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);
1da177e4 1679
f6795053 1680 if (len > mmap_end - mmap_min_addr)
1da177e4
LT
1681 return -ENOMEM;
1682
06abdfb4
BH
1683 if (flags & MAP_FIXED)
1684 return addr;
1685
1da177e4
LT
1686 if (addr) {
1687 addr = PAGE_ALIGN(addr);
1be7107f 1688 vma = find_vma_prev(mm, addr, &prev);
f6795053 1689 if (mmap_end - len >= addr && addr >= mmap_min_addr &&
1be7107f
HD
1690 (!vma || addr + len <= vm_start_gap(vma)) &&
1691 (!prev || addr >= vm_end_gap(prev)))
1da177e4
LT
1692 return addr;
1693 }
1da177e4 1694
db4fbfb9
ML
1695 info.flags = 0;
1696 info.length = len;
4e99b021 1697 info.low_limit = mm->mmap_base;
f6795053 1698 info.high_limit = mmap_end;
db4fbfb9 1699 info.align_mask = 0;
09ef5283 1700 info.align_offset = 0;
db4fbfb9 1701 return vm_unmapped_area(&info);
1da177e4 1702}
4b439e25
CL
1703
1704#ifndef HAVE_ARCH_UNMAPPED_AREA
1705unsigned long
1706arch_get_unmapped_area(struct file *filp, unsigned long addr,
1707 unsigned long len, unsigned long pgoff,
1708 unsigned long flags)
1709{
1710 return generic_get_unmapped_area(filp, addr, len, pgoff, flags);
1711}
cc71aba3 1712#endif
1da177e4 1713
1da177e4
LT
1714/*
1715 * This mmap-allocator allocates new areas top-down from below the
1716 * stack's low limit (the base):
1717 */
1da177e4 1718unsigned long
4b439e25
CL
1719generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
1720 unsigned long len, unsigned long pgoff,
1721 unsigned long flags)
1da177e4 1722{
1be7107f 1723 struct vm_area_struct *vma, *prev;
1da177e4 1724 struct mm_struct *mm = current->mm;
db4fbfb9 1725 struct vm_unmapped_area_info info;
2cb4de08 1726 const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);
1da177e4
LT
1727
1728 /* requested length too big for entire address space */
f6795053 1729 if (len > mmap_end - mmap_min_addr)
1da177e4
LT
1730 return -ENOMEM;
1731
06abdfb4
BH
1732 if (flags & MAP_FIXED)
1733 return addr;
1734
1da177e4
LT
1735 /* requesting a specific address */
1736 if (addr) {
1737 addr = PAGE_ALIGN(addr);
1be7107f 1738 vma = find_vma_prev(mm, addr, &prev);
f6795053 1739 if (mmap_end - len >= addr && addr >= mmap_min_addr &&
1be7107f
HD
1740 (!vma || addr + len <= vm_start_gap(vma)) &&
1741 (!prev || addr >= vm_end_gap(prev)))
1da177e4
LT
1742 return addr;
1743 }
1744
db4fbfb9
ML
1745 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
1746 info.length = len;
6b008640 1747 info.low_limit = PAGE_SIZE;
f6795053 1748 info.high_limit = arch_get_mmap_base(addr, mm->mmap_base);
db4fbfb9 1749 info.align_mask = 0;
09ef5283 1750 info.align_offset = 0;
db4fbfb9 1751 addr = vm_unmapped_area(&info);
b716ad95 1752
1da177e4
LT
1753 /*
1754 * A failed mmap() very likely causes application failure,
1755 * so fall back to the bottom-up function here. This scenario
1756 * can happen with large stack limits and large mmap()
1757 * allocations.
1758 */
de1741a1 1759 if (offset_in_page(addr)) {
db4fbfb9
ML
1760 VM_BUG_ON(addr != -ENOMEM);
1761 info.flags = 0;
1762 info.low_limit = TASK_UNMAPPED_BASE;
f6795053 1763 info.high_limit = mmap_end;
db4fbfb9
ML
1764 addr = vm_unmapped_area(&info);
1765 }
1da177e4
LT
1766
1767 return addr;
1768}
4b439e25
CL
1769
1770#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1771unsigned long
1772arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
1773 unsigned long len, unsigned long pgoff,
1774 unsigned long flags)
1775{
1776 return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags);
1777}
1da177e4
LT
1778#endif
1779
1da177e4
LT
1780unsigned long
1781get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1782 unsigned long pgoff, unsigned long flags)
1783{
06abdfb4
BH
1784 unsigned long (*get_area)(struct file *, unsigned long,
1785 unsigned long, unsigned long, unsigned long);
1786
9206de95
AV
1787 unsigned long error = arch_mmap_check(addr, len, flags);
1788 if (error)
1789 return error;
1790
1791 /* Careful about overflows.. */
1792 if (len > TASK_SIZE)
1793 return -ENOMEM;
1794
06abdfb4 1795 get_area = current->mm->get_unmapped_area;
c01d5b30
HD
1796 if (file) {
1797 if (file->f_op->get_unmapped_area)
1798 get_area = file->f_op->get_unmapped_area;
1799 } else if (flags & MAP_SHARED) {
1800 /*
1801 * mmap_region() will call shmem_zero_setup() to create a file,
1802 * so use shmem's get_unmapped_area in case it can be huge.
45e55300 1803 * do_mmap() will clear pgoff, so match alignment.
c01d5b30
HD
1804 */
1805 pgoff = 0;
1806 get_area = shmem_get_unmapped_area;
1807 }
1808
06abdfb4
BH
1809 addr = get_area(file, addr, len, pgoff, flags);
1810 if (IS_ERR_VALUE(addr))
1811 return addr;
1da177e4 1812
07ab67c8
LT
1813 if (addr > TASK_SIZE - len)
1814 return -ENOMEM;
de1741a1 1815 if (offset_in_page(addr))
07ab67c8 1816 return -EINVAL;
06abdfb4 1817
9ac4ed4b
AV
1818 error = security_mmap_addr(addr);
1819 return error ? error : addr;
1da177e4
LT
1820}
1821
1822EXPORT_SYMBOL(get_unmapped_area);
1823
abdba2dd
LH
1824/**
1825 * find_vma_intersection() - Look up the first VMA which intersects the interval
1826 * @mm: The process address space.
1827 * @start_addr: The inclusive start user address.
1828 * @end_addr: The exclusive end user address.
1829 *
1830 * Returns: The first VMA within the provided range, %NULL otherwise. Assumes
1831 * start_addr < end_addr.
1832 */
1833struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
1834 unsigned long start_addr,
1835 unsigned long end_addr)
1836{
abdba2dd
LH
1837 unsigned long index = start_addr;
1838
1839 mmap_assert_locked(mm);
7964cf8c 1840 return mt_find(&mm->mm_mt, &index, end_addr - 1);
abdba2dd
LH
1841}
1842EXPORT_SYMBOL(find_vma_intersection);
1843
be8432e7
LH
1844/**
1845 * find_vma() - Find the VMA for a given address, or the next VMA.
1846 * @mm: The mm_struct to check
1847 * @addr: The address
1848 *
1849 * Returns: The VMA associated with addr, or the next VMA.
1850 * May return %NULL in the case of no VMA at addr or above.
1851 */
48aae425 1852struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
1da177e4 1853{
be8432e7 1854 unsigned long index = addr;
1da177e4 1855
5b78ed24 1856 mmap_assert_locked(mm);
7964cf8c 1857 return mt_find(&mm->mm_mt, &index, ULONG_MAX);
1da177e4 1858}
1da177e4
LT
1859EXPORT_SYMBOL(find_vma);
1860
7fdbd37d
LH
1861/**
1862 * find_vma_prev() - Find the VMA for a given address, or the next vma and
1863 * set %pprev to the previous VMA, if any.
1864 * @mm: The mm_struct to check
1865 * @addr: The address
1866 * @pprev: The pointer to set to the previous VMA
1867 *
1868 * Note that RCU lock is missing here since the external mmap_lock() is used
1869 * instead.
1870 *
1871 * Returns: The VMA associated with @addr, or the next vma.
1872 * May return %NULL in the case of no vma at addr or above.
6bd4837d 1873 */
1da177e4
LT
1874struct vm_area_struct *
1875find_vma_prev(struct mm_struct *mm, unsigned long addr,
1876 struct vm_area_struct **pprev)
1877{
6bd4837d 1878 struct vm_area_struct *vma;
7fdbd37d 1879 MA_STATE(mas, &mm->mm_mt, addr, addr);
1da177e4 1880
7fdbd37d
LH
1881 vma = mas_walk(&mas);
1882 *pprev = mas_prev(&mas, 0);
1883 if (!vma)
1884 vma = mas_next(&mas, ULONG_MAX);
6bd4837d 1885 return vma;
1da177e4
LT
1886}
1887
1888/*
1889 * Verify that the stack growth is acceptable and
1890 * update accounting. This is shared with both the
1891 * grow-up and grow-down cases.
1892 */
1be7107f
HD
1893static int acct_stack_growth(struct vm_area_struct *vma,
1894 unsigned long size, unsigned long grow)
1da177e4
LT
1895{
1896 struct mm_struct *mm = vma->vm_mm;
1be7107f 1897 unsigned long new_start;
1da177e4
LT
1898
1899 /* address space limit tests */
84638335 1900 if (!may_expand_vm(mm, vma->vm_flags, grow))
1da177e4
LT
1901 return -ENOMEM;
1902
1903 /* Stack limit test */
24c79d8e 1904 if (size > rlimit(RLIMIT_STACK))
1da177e4
LT
1905 return -ENOMEM;
1906
1907 /* mlock limit tests */
b0cc5e89 1908 if (!mlock_future_ok(mm, vma->vm_flags, grow << PAGE_SHIFT))
c5d8a364 1909 return -ENOMEM;
1da177e4 1910
0d59a01b
AL
1911 /* Check to ensure the stack will not grow into a hugetlb-only region */
1912 new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start :
1913 vma->vm_end - size;
1914 if (is_hugepage_only_range(vma->vm_mm, new_start, size))
1915 return -EFAULT;
1916
1da177e4
LT
1917 /*
1918 * Overcommit.. This must be the final test, as it will
1919 * update security statistics.
1920 */
05fa199d 1921 if (security_vm_enough_memory_mm(mm, grow))
1da177e4
LT
1922 return -ENOMEM;
1923
1da177e4
LT
1924 return 0;
1925}
1926
46dea3d0 1927#if defined(CONFIG_STACK_GROWSUP) || defined(CONFIG_IA64)
1da177e4 1928/*
46dea3d0
HD
1929 * PA-RISC uses this for its stack; IA64 for its Register Backing Store.
1930 * vma is the last one with address > vma->vm_end. Have to extend vma.
1da177e4 1931 */
8d7071af 1932static int expand_upwards(struct vm_area_struct *vma, unsigned long address)
1da177e4 1933{
09357814 1934 struct mm_struct *mm = vma->vm_mm;
1be7107f
HD
1935 struct vm_area_struct *next;
1936 unsigned long gap_addr;
12352d3c 1937 int error = 0;
d4af56c5 1938 MA_STATE(mas, &mm->mm_mt, 0, 0);
1da177e4
LT
1939
1940 if (!(vma->vm_flags & VM_GROWSUP))
1941 return -EFAULT;
1942
bd726c90 1943 /* Guard against exceeding limits of the address space. */
1be7107f 1944 address &= PAGE_MASK;
37511fb5 1945 if (address >= (TASK_SIZE & PAGE_MASK))
12352d3c 1946 return -ENOMEM;
bd726c90 1947 address += PAGE_SIZE;
12352d3c 1948
1be7107f
HD
1949 /* Enforce stack_guard_gap */
1950 gap_addr = address + stack_guard_gap;
bd726c90
HD
1951
1952 /* Guard against overflow */
1953 if (gap_addr < address || gap_addr > TASK_SIZE)
1954 gap_addr = TASK_SIZE;
1955
763ecb03
LH
1956 next = find_vma_intersection(mm, vma->vm_end, gap_addr);
1957 if (next && vma_is_accessible(next)) {
1be7107f
HD
1958 if (!(next->vm_flags & VM_GROWSUP))
1959 return -ENOMEM;
1960 /* Check that both stack segments have the same anon_vma? */
1961 }
1962
c5d5546e 1963 if (mas_preallocate(&mas, GFP_KERNEL))
d4af56c5
LH
1964 return -ENOMEM;
1965
12352d3c 1966 /* We must make sure the anon_vma is allocated. */
d4af56c5
LH
1967 if (unlikely(anon_vma_prepare(vma))) {
1968 mas_destroy(&mas);
1da177e4 1969 return -ENOMEM;
d4af56c5 1970 }
1da177e4 1971
c137381f
SB
1972 /* Lock the VMA before expanding to prevent concurrent page faults */
1973 vma_start_write(vma);
1da177e4
LT
1974 /*
1975 * vma->vm_start/vm_end cannot change under us because the caller
c1e8d7c6 1976 * is required to hold the mmap_lock in read mode. We need the
1da177e4
LT
1977 * anon_vma lock to serialize against concurrent expand_stacks.
1978 */
12352d3c 1979 anon_vma_lock_write(vma->anon_vma);
1da177e4
LT
1980
1981 /* Somebody else might have raced and expanded it already */
1982 if (address > vma->vm_end) {
1983 unsigned long size, grow;
1984
1985 size = address - vma->vm_start;
1986 grow = (address - vma->vm_end) >> PAGE_SHIFT;
1987
42c36f63
HD
1988 error = -ENOMEM;
1989 if (vma->vm_pgoff + (size >> PAGE_SHIFT) >= vma->vm_pgoff) {
1990 error = acct_stack_growth(vma, size, grow);
1991 if (!error) {
4128997b 1992 /*
524e00b3
LH
1993 * We only hold a shared mmap_lock lock here, so
1994 * we need to protect against concurrent vma
1995 * expansions. anon_vma_lock_write() doesn't
1996 * help here, as we don't guarantee that all
1997 * growable vmas in a mm share the same root
1998 * anon vma. So, we reuse mm->page_table_lock
1999 * to guard against concurrent vma expansions.
4128997b 2000 */
09357814 2001 spin_lock(&mm->page_table_lock);
87e8827b 2002 if (vma->vm_flags & VM_LOCKED)
09357814 2003 mm->locked_vm += grow;
84638335 2004 vm_stat_account(mm, vma->vm_flags, grow);
bf181b9f 2005 anon_vma_interval_tree_pre_update_vma(vma);
42c36f63 2006 vma->vm_end = address;
d4af56c5 2007 /* Overwrite old entry in mtree. */
fbcc3104
LH
2008 mas_set_range(&mas, vma->vm_start, address - 1);
2009 mas_store_prealloc(&mas, vma);
bf181b9f 2010 anon_vma_interval_tree_post_update_vma(vma);
09357814 2011 spin_unlock(&mm->page_table_lock);
4128997b 2012
42c36f63
HD
2013 perf_event_mmap(vma);
2014 }
3af9e859 2015 }
1da177e4 2016 }
12352d3c 2017 anon_vma_unlock_write(vma->anon_vma);
c791576c 2018 khugepaged_enter_vma(vma, vma->vm_flags);
d4af56c5 2019 mas_destroy(&mas);
2574d5e4 2020 validate_mm(mm);
1da177e4
LT
2021 return error;
2022}
46dea3d0
HD
2023#endif /* CONFIG_STACK_GROWSUP || CONFIG_IA64 */
2024
1da177e4
LT
2025/*
2026 * vma is the first one with address < vma->vm_start. Have to extend vma.
8d7071af 2027 * mmap_lock held for writing.
1da177e4 2028 */
524e00b3 2029int expand_downwards(struct vm_area_struct *vma, unsigned long address)
1da177e4 2030{
09357814 2031 struct mm_struct *mm = vma->vm_mm;
763ecb03 2032 MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start);
1be7107f 2033 struct vm_area_struct *prev;
0a1d5299 2034 int error = 0;
1da177e4 2035
8d7071af
LT
2036 if (!(vma->vm_flags & VM_GROWSDOWN))
2037 return -EFAULT;
2038
8869477a 2039 address &= PAGE_MASK;
8b35ca3e 2040 if (address < mmap_min_addr || address < FIRST_USER_ADDRESS)
0a1d5299 2041 return -EPERM;
8869477a 2042
1be7107f 2043 /* Enforce stack_guard_gap */
763ecb03 2044 prev = mas_prev(&mas, 0);
32e4e6d5 2045 /* Check that both stack segments have the same anon_vma? */
f440fa1a
LH
2046 if (prev) {
2047 if (!(prev->vm_flags & VM_GROWSDOWN) &&
2048 vma_is_accessible(prev) &&
2049 (address - prev->vm_end < stack_guard_gap))
1be7107f 2050 return -ENOMEM;
1be7107f
HD
2051 }
2052
c5d5546e 2053 if (mas_preallocate(&mas, GFP_KERNEL))
d4af56c5
LH
2054 return -ENOMEM;
2055
12352d3c 2056 /* We must make sure the anon_vma is allocated. */
d4af56c5
LH
2057 if (unlikely(anon_vma_prepare(vma))) {
2058 mas_destroy(&mas);
12352d3c 2059 return -ENOMEM;
d4af56c5 2060 }
1da177e4 2061
c137381f
SB
2062 /* Lock the VMA before expanding to prevent concurrent page faults */
2063 vma_start_write(vma);
1da177e4
LT
2064 /*
2065 * vma->vm_start/vm_end cannot change under us because the caller
c1e8d7c6 2066 * is required to hold the mmap_lock in read mode. We need the
1da177e4
LT
2067 * anon_vma lock to serialize against concurrent expand_stacks.
2068 */
12352d3c 2069 anon_vma_lock_write(vma->anon_vma);
1da177e4
LT
2070
2071 /* Somebody else might have raced and expanded it already */
2072 if (address < vma->vm_start) {
2073 unsigned long size, grow;
2074
2075 size = vma->vm_end - address;
2076 grow = (vma->vm_start - address) >> PAGE_SHIFT;
2077
a626ca6a
LT
2078 error = -ENOMEM;
2079 if (grow <= vma->vm_pgoff) {
2080 error = acct_stack_growth(vma, size, grow);
2081 if (!error) {
4128997b 2082 /*
524e00b3
LH
2083 * We only hold a shared mmap_lock lock here, so
2084 * we need to protect against concurrent vma
2085 * expansions. anon_vma_lock_write() doesn't
2086 * help here, as we don't guarantee that all
2087 * growable vmas in a mm share the same root
2088 * anon vma. So, we reuse mm->page_table_lock
2089 * to guard against concurrent vma expansions.
4128997b 2090 */
09357814 2091 spin_lock(&mm->page_table_lock);
87e8827b 2092 if (vma->vm_flags & VM_LOCKED)
09357814 2093 mm->locked_vm += grow;
84638335 2094 vm_stat_account(mm, vma->vm_flags, grow);
bf181b9f 2095 anon_vma_interval_tree_pre_update_vma(vma);
a626ca6a
LT
2096 vma->vm_start = address;
2097 vma->vm_pgoff -= grow;
d4af56c5 2098 /* Overwrite old entry in mtree. */
fbcc3104
LH
2099 mas_set_range(&mas, address, vma->vm_end - 1);
2100 mas_store_prealloc(&mas, vma);
bf181b9f 2101 anon_vma_interval_tree_post_update_vma(vma);
09357814 2102 spin_unlock(&mm->page_table_lock);
4128997b 2103
a626ca6a
LT
2104 perf_event_mmap(vma);
2105 }
1da177e4
LT
2106 }
2107 }
12352d3c 2108 anon_vma_unlock_write(vma->anon_vma);
c791576c 2109 khugepaged_enter_vma(vma, vma->vm_flags);
d4af56c5 2110 mas_destroy(&mas);
2574d5e4 2111 validate_mm(mm);
1da177e4
LT
2112 return error;
2113}
2114
1be7107f
HD
2115/* enforced gap between the expanding stack and other mappings. */
2116unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
2117
2118static int __init cmdline_parse_stack_guard_gap(char *p)
2119{
2120 unsigned long val;
2121 char *endptr;
2122
2123 val = simple_strtoul(p, &endptr, 10);
2124 if (!*endptr)
2125 stack_guard_gap = val << PAGE_SHIFT;
2126
e6d09493 2127 return 1;
1be7107f
HD
2128}
2129__setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
2130
b6a2fea3 2131#ifdef CONFIG_STACK_GROWSUP
8d7071af 2132int expand_stack_locked(struct vm_area_struct *vma, unsigned long address)
b6a2fea3
OW
2133{
2134 return expand_upwards(vma, address);
2135}
2136
8d7071af 2137struct vm_area_struct *find_extend_vma_locked(struct mm_struct *mm, unsigned long addr)
b6a2fea3
OW
2138{
2139 struct vm_area_struct *vma, *prev;
2140
2141 addr &= PAGE_MASK;
2142 vma = find_vma_prev(mm, addr, &prev);
2143 if (vma && (vma->vm_start <= addr))
2144 return vma;
f440fa1a
LH
2145 if (!prev)
2146 return NULL;
8d7071af 2147 if (expand_stack_locked(prev, addr))
b6a2fea3 2148 return NULL;
cea10a19 2149 if (prev->vm_flags & VM_LOCKED)
fc05f566 2150 populate_vma_page_range(prev, addr, prev->vm_end, NULL);
b6a2fea3
OW
2151 return prev;
2152}
2153#else
8d7071af 2154int expand_stack_locked(struct vm_area_struct *vma, unsigned long address)
b6a2fea3 2155{
f440fa1a
LH
2156 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
2157 return -EINVAL;
b6a2fea3
OW
2158 return expand_downwards(vma, address);
2159}
2160
8d7071af 2161struct vm_area_struct *find_extend_vma_locked(struct mm_struct *mm, unsigned long addr)
1da177e4 2162{
cc71aba3 2163 struct vm_area_struct *vma;
1da177e4
LT
2164 unsigned long start;
2165
2166 addr &= PAGE_MASK;
cc71aba3 2167 vma = find_vma(mm, addr);
1da177e4
LT
2168 if (!vma)
2169 return NULL;
2170 if (vma->vm_start <= addr)
2171 return vma;
1da177e4 2172 start = vma->vm_start;
8d7071af 2173 if (expand_stack_locked(vma, addr))
1da177e4 2174 return NULL;
cea10a19 2175 if (vma->vm_flags & VM_LOCKED)
fc05f566 2176 populate_vma_page_range(vma, addr, start, NULL);
1da177e4
LT
2177 return vma;
2178}
2179#endif
2180
8d7071af
LT
2181/*
2182 * IA64 has some horrid mapping rules: it can expand both up and down,
2183 * but with various special rules.
2184 *
2185 * We'll get rid of this architecture eventually, so the ugliness is
2186 * temporary.
2187 */
2188#ifdef CONFIG_IA64
2189static inline bool vma_expand_ok(struct vm_area_struct *vma, unsigned long addr)
2190{
2191 return REGION_NUMBER(addr) == REGION_NUMBER(vma->vm_start) &&
2192 REGION_OFFSET(addr) < RGN_MAP_LIMIT;
2193}
2194
2195/*
2196 * IA64 stacks grow down, but there's a special register backing store
2197 * that can grow up. Only sequentially, though, so the new address must
2198 * match vm_end.
2199 */
2200static inline int vma_expand_up(struct vm_area_struct *vma, unsigned long addr)
2201{
2202 if (!vma_expand_ok(vma, addr))
2203 return -EFAULT;
2204 if (vma->vm_end != (addr & PAGE_MASK))
2205 return -EFAULT;
2206 return expand_upwards(vma, addr);
2207}
2208
2209static inline bool vma_expand_down(struct vm_area_struct *vma, unsigned long addr)
2210{
2211 if (!vma_expand_ok(vma, addr))
2212 return -EFAULT;
2213 return expand_downwards(vma, addr);
2214}
2215
2216#elif defined(CONFIG_STACK_GROWSUP)
2217
2218#define vma_expand_up(vma,addr) expand_upwards(vma, addr)
2219#define vma_expand_down(vma, addr) (-EFAULT)
2220
2221#else
2222
2223#define vma_expand_up(vma,addr) (-EFAULT)
2224#define vma_expand_down(vma, addr) expand_downwards(vma, addr)
2225
2226#endif
2227
2228/*
2229 * expand_stack(): legacy interface for page faulting. Don't use unless
2230 * you have to.
2231 *
2232 * This is called with the mm locked for reading, drops the lock, takes
2233 * the lock for writing, tries to look up a vma again, expands it if
2234 * necessary, and downgrades the lock to reading again.
2235 *
2236 * If no vma is found or it can't be expanded, it returns NULL and has
2237 * dropped the lock.
2238 */
2239struct vm_area_struct *expand_stack(struct mm_struct *mm, unsigned long addr)
f440fa1a 2240{
8d7071af
LT
2241 struct vm_area_struct *vma, *prev;
2242
2243 mmap_read_unlock(mm);
2244 if (mmap_write_lock_killable(mm))
2245 return NULL;
2246
2247 vma = find_vma_prev(mm, addr, &prev);
2248 if (vma && vma->vm_start <= addr)
2249 goto success;
2250
2251 if (prev && !vma_expand_up(prev, addr)) {
2252 vma = prev;
2253 goto success;
2254 }
2255
2256 if (vma && !vma_expand_down(vma, addr))
2257 goto success;
2258
2259 mmap_write_unlock(mm);
2260 return NULL;
2261
2262success:
2263 mmap_write_downgrade(mm);
2264 return vma;
f440fa1a 2265}
e1d6d01a 2266
1da177e4 2267/*
763ecb03
LH
2268 * Ok - we have the memory areas we should free on a maple tree so release them,
2269 * and do the vma updates.
2c0b3814
HD
2270 *
2271 * Called with the mm semaphore held.
1da177e4 2272 */
763ecb03 2273static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas)
1da177e4 2274{
4f74d2c8 2275 unsigned long nr_accounted = 0;
763ecb03 2276 struct vm_area_struct *vma;
4f74d2c8 2277
365e9c87
HD
2278 /* Update high watermark before we lower total_vm */
2279 update_hiwater_vm(mm);
763ecb03 2280 mas_for_each(mas, vma, ULONG_MAX) {
2c0b3814
HD
2281 long nrpages = vma_pages(vma);
2282
4f74d2c8
LT
2283 if (vma->vm_flags & VM_ACCOUNT)
2284 nr_accounted += nrpages;
84638335 2285 vm_stat_account(mm, vma->vm_flags, -nrpages);
0d2ebf9c 2286 remove_vma(vma, false);
763ecb03 2287 }
4f74d2c8 2288 vm_unacct_memory(nr_accounted);
1da177e4
LT
2289}
2290
2291/*
2292 * Get rid of page table information in the indicated region.
2293 *
f10df686 2294 * Called with the mm semaphore held.
1da177e4 2295 */
763ecb03 2296static void unmap_region(struct mm_struct *mm, struct maple_tree *mt,
e0da382c 2297 struct vm_area_struct *vma, struct vm_area_struct *prev,
763ecb03 2298 struct vm_area_struct *next,
68f48381 2299 unsigned long start, unsigned long end, bool mm_wr_locked)
1da177e4 2300{
d16dfc55 2301 struct mmu_gather tlb;
1da177e4
LT
2302
2303 lru_add_drain();
a72afd87 2304 tlb_gather_mmu(&tlb, mm);
365e9c87 2305 update_hiwater_rss(mm);
68f48381 2306 unmap_vmas(&tlb, mt, vma, start, end, mm_wr_locked);
763ecb03 2307 free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS,
98e51a22
SB
2308 next ? next->vm_start : USER_PGTABLES_CEILING,
2309 mm_wr_locked);
ae8eba8b 2310 tlb_finish_mmu(&tlb);
1da177e4
LT
2311}
2312
1da177e4 2313/*
def5efe0
DR
2314 * __split_vma() bypasses sysctl_max_map_count checking. We use this where it
2315 * has already been checked or doesn't make sense to fail.
0fd5a9e2 2316 * VMA Iterator will point to the end VMA.
1da177e4 2317 */
9760ebff 2318int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
def5efe0 2319 unsigned long addr, int new_below)
1da177e4 2320{
b2b3b886 2321 struct vma_prepare vp;
1da177e4 2322 struct vm_area_struct *new;
e3975891 2323 int err;
9760ebff 2324
b2b3b886
LH
2325 WARN_ON(vma->vm_start >= addr);
2326 WARN_ON(vma->vm_end <= addr);
2327
dd3b614f
DS
2328 if (vma->vm_ops && vma->vm_ops->may_split) {
2329 err = vma->vm_ops->may_split(vma, addr);
31383c68
DW
2330 if (err)
2331 return err;
2332 }
1da177e4 2333
3928d4f5 2334 new = vm_area_dup(vma);
1da177e4 2335 if (!new)
e3975891 2336 return -ENOMEM;
1da177e4 2337
b2b3b886
LH
2338 err = -ENOMEM;
2339 if (vma_iter_prealloc(vmi))
2340 goto out_free_vma;
2341
2342 if (new_below) {
1da177e4 2343 new->vm_end = addr;
b2b3b886 2344 } else {
1da177e4
LT
2345 new->vm_start = addr;
2346 new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
2347 }
2348
ef0855d3
ON
2349 err = vma_dup_policy(vma, new);
2350 if (err)
b2b3b886 2351 goto out_free_vmi;
1da177e4 2352
c4ea95d7
DF
2353 err = anon_vma_clone(new, vma);
2354 if (err)
5beb4930
RR
2355 goto out_free_mpol;
2356
e9714acf 2357 if (new->vm_file)
1da177e4
LT
2358 get_file(new->vm_file);
2359
2360 if (new->vm_ops && new->vm_ops->open)
2361 new->vm_ops->open(new);
2362
b2b3b886
LH
2363 init_vma_prep(&vp, vma);
2364 vp.insert = new;
2365 vma_prepare(&vp);
ccf1d78d 2366 vma_adjust_trans_huge(vma, vma->vm_start, addr, 0);
1da177e4 2367
b2b3b886
LH
2368 if (new_below) {
2369 vma->vm_start = addr;
2370 vma->vm_pgoff += (addr - new->vm_start) >> PAGE_SHIFT;
2371 } else {
2372 vma->vm_end = addr;
9760ebff 2373 }
5beb4930 2374
b2b3b886
LH
2375 /* vma_complete stores the new vma */
2376 vma_complete(&vp, vmi, vma->vm_mm);
2377
2378 /* Success. */
2379 if (new_below)
2380 vma_next(vmi);
b2b3b886
LH
2381 return 0;
2382
2383out_free_mpol:
ef0855d3 2384 mpol_put(vma_policy(new));
b2b3b886
LH
2385out_free_vmi:
2386 vma_iter_free(vmi);
2387out_free_vma:
3928d4f5 2388 vm_area_free(new);
5beb4930 2389 return err;
1da177e4
LT
2390}
2391
659ace58
KM
2392/*
2393 * Split a vma into two pieces at address 'addr', a new vma is allocated
2394 * either for the first part or the tail.
2395 */
9760ebff 2396int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
659ace58
KM
2397 unsigned long addr, int new_below)
2398{
9760ebff 2399 if (vma->vm_mm->map_count >= sysctl_max_map_count)
659ace58
KM
2400 return -ENOMEM;
2401
9760ebff 2402 return __split_vma(vmi, vma, addr, new_below);
f2ebfe43
LH
2403}
2404
11f9a21a 2405/*
183654ce
LH
2406 * do_vmi_align_munmap() - munmap the aligned region from @start to @end.
2407 * @vmi: The vma iterator
11f9a21a
LH
2408 * @vma: The starting vm_area_struct
2409 * @mm: The mm_struct
2410 * @start: The aligned start address to munmap.
2411 * @end: The aligned end address to munmap.
2412 * @uf: The userfaultfd list_head
408579cd
LH
2413 * @unlock: Set to true to drop the mmap_lock. unlocking only happens on
2414 * success.
11f9a21a 2415 *
408579cd
LH
2416 * Return: 0 on success and drops the lock if so directed, error and leaves the
2417 * lock held otherwise.
11f9a21a
LH
2418 */
2419static int
183654ce 2420do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
11f9a21a 2421 struct mm_struct *mm, unsigned long start,
408579cd 2422 unsigned long end, struct list_head *uf, bool unlock)
11f9a21a 2423{
763ecb03
LH
2424 struct vm_area_struct *prev, *next = NULL;
2425 struct maple_tree mt_detach;
2426 int count = 0;
11f9a21a 2427 int error = -ENOMEM;
606c812e 2428 unsigned long locked_vm = 0;
763ecb03 2429 MA_STATE(mas_detach, &mt_detach, 0, 0);
3dd44325 2430 mt_init_flags(&mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_MASK);
763ecb03 2431 mt_set_external_lock(&mt_detach, &mm->mmap_lock);
d4af56c5 2432
1da177e4
LT
2433 /*
2434 * If we need to split any vma, do it now to save pain later.
2435 *
2436 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
2437 * unmapped vm_area_struct will remain in use: so lower split_vma
2438 * places tmp vma above, and higher split_vma places tmp vma below.
2439 */
763ecb03
LH
2440
2441 /* Does it split the first one? */
146425a3 2442 if (start > vma->vm_start) {
659ace58
KM
2443
2444 /*
2445 * Make sure that map_count on return from munmap() will
2446 * not exceed its limit; but let map_count go just above
2447 * its limit temporarily, to help free resources as expected.
2448 */
2449 if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count)
d4af56c5 2450 goto map_count_exceeded;
659ace58 2451
9760ebff 2452 error = __split_vma(vmi, vma, start, 0);
1da177e4 2453 if (error)
763ecb03 2454 goto start_split_failed;
11f9a21a 2455
0fd5a9e2 2456 vma = vma_iter_load(vmi);
1da177e4
LT
2457 }
2458
183654ce 2459 prev = vma_prev(vmi);
763ecb03 2460 if (unlikely((!prev)))
183654ce 2461 vma_iter_set(vmi, start);
763ecb03
LH
2462
2463 /*
2464 * Detach a range of VMAs from the mm. Using next as a temp variable as
2465 * it is always overwritten.
2466 */
183654ce 2467 for_each_vma_range(*vmi, next, end) {
763ecb03
LH
2468 /* Does it split the end? */
2469 if (next->vm_end > end) {
6b73cff2 2470 error = __split_vma(vmi, next, end, 0);
763ecb03
LH
2471 if (error)
2472 goto end_split_failed;
763ecb03 2473 }
606c812e
LH
2474 vma_start_write(next);
2475 mas_set_range(&mas_detach, next->vm_start, next->vm_end - 1);
6c26bd43
DW
2476 error = mas_store_gfp(&mas_detach, next, GFP_KERNEL);
2477 if (error)
606c812e
LH
2478 goto munmap_gather_failed;
2479 vma_mark_detached(next, true);
2480 if (next->vm_flags & VM_LOCKED)
2481 locked_vm += vma_pages(next);
11f9a21a 2482
763ecb03 2483 count++;
65ac1320
LH
2484 if (unlikely(uf)) {
2485 /*
2486 * If userfaultfd_unmap_prep returns an error the vmas
2487 * will remain split, but userland will get a
2488 * highly unexpected error anyway. This is no
2489 * different than the case where the first of the two
2490 * __split_vma fails, but we don't undo the first
2491 * split, despite we could. This is unlikely enough
2492 * failure that it's not worth optimizing it for.
2493 */
2494 error = userfaultfd_unmap_prep(next, start, end, uf);
2495
2496 if (error)
2497 goto userfaultfd_error;
2498 }
763ecb03
LH
2499#ifdef CONFIG_DEBUG_VM_MAPLE_TREE
2500 BUG_ON(next->vm_start < start);
2501 BUG_ON(next->vm_start > end);
2502#endif
1da177e4 2503 }
1da177e4 2504
15c0c60b
LH
2505 if (vma_iter_end(vmi) > end)
2506 next = vma_iter_load(vmi);
11f9a21a 2507
15c0c60b
LH
2508 if (!next)
2509 next = vma_next(vmi);
2376dd7c 2510
763ecb03
LH
2511#if defined(CONFIG_DEBUG_VM_MAPLE_TREE)
2512 /* Make sure no VMAs are about to be lost. */
2513 {
2514 MA_STATE(test, &mt_detach, start, end - 1);
2515 struct vm_area_struct *vma_mas, *vma_test;
2516 int test_count = 0;
2517
183654ce 2518 vma_iter_set(vmi, start);
763ecb03
LH
2519 rcu_read_lock();
2520 vma_test = mas_find(&test, end - 1);
183654ce 2521 for_each_vma_range(*vmi, vma_mas, end) {
763ecb03
LH
2522 BUG_ON(vma_mas != vma_test);
2523 test_count++;
2524 vma_test = mas_next(&test, end - 1);
2525 }
2526 rcu_read_unlock();
2527 BUG_ON(count != test_count);
763ecb03
LH
2528 }
2529#endif
183654ce 2530 vma_iter_set(vmi, start);
6c26bd43
DW
2531 error = vma_iter_clear_gfp(vmi, start, end, GFP_KERNEL);
2532 if (error)
606c812e 2533 goto clear_tree_failed;
0378c0a0 2534
6c26bd43 2535 /* Point of no return */
606c812e 2536 mm->locked_vm -= locked_vm;
763ecb03 2537 mm->map_count -= count;
408579cd 2538 if (unlock)
e4bd84c0 2539 mmap_write_downgrade(mm);
dd2283f2 2540
68f48381
SB
2541 /*
2542 * We can free page tables without write-locking mmap_lock because VMAs
2543 * were isolated before we downgraded mmap_lock.
2544 */
408579cd 2545 unmap_region(mm, &mt_detach, vma, prev, next, start, end, !unlock);
763ecb03
LH
2546 /* Statistics and freeing VMAs */
2547 mas_set(&mas_detach, start);
2548 remove_mt(mm, &mas_detach);
2549 __mt_destroy(&mt_detach);
ae80b404 2550 validate_mm(mm);
408579cd
LH
2551 if (unlock)
2552 mmap_read_unlock(mm);
1da177e4 2553
408579cd 2554 return 0;
d4af56c5 2555
606c812e 2556clear_tree_failed:
d4af56c5 2557userfaultfd_error:
606c812e 2558munmap_gather_failed:
763ecb03 2559end_split_failed:
606c812e
LH
2560 mas_set(&mas_detach, 0);
2561 mas_for_each(&mas_detach, next, end)
2562 vma_mark_detached(next, false);
2563
763ecb03
LH
2564 __mt_destroy(&mt_detach);
2565start_split_failed:
2566map_count_exceeded:
b5641a5d 2567 validate_mm(mm);
d4af56c5 2568 return error;
1da177e4 2569}
1da177e4 2570
11f9a21a 2571/*
183654ce
LH
2572 * do_vmi_munmap() - munmap a given range.
2573 * @vmi: The vma iterator
11f9a21a
LH
2574 * @mm: The mm_struct
2575 * @start: The start address to munmap
2576 * @len: The length of the range to munmap
2577 * @uf: The userfaultfd list_head
408579cd 2578 * @unlock: set to true if the user wants to drop the mmap_lock on success
11f9a21a
LH
2579 *
2580 * This function takes a @mas that is either pointing to the previous VMA or set
2581 * to MA_START and sets it up to remove the mapping(s). The @len will be
2582 * aligned and any arch_unmap work will be preformed.
2583 *
408579cd
LH
2584 * Return: 0 on success and drops the lock if so directed, error and leaves the
2585 * lock held otherwise.
11f9a21a 2586 */
183654ce 2587int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm,
11f9a21a 2588 unsigned long start, size_t len, struct list_head *uf,
408579cd 2589 bool unlock)
11f9a21a
LH
2590{
2591 unsigned long end;
2592 struct vm_area_struct *vma;
2593
2594 if ((offset_in_page(start)) || start > TASK_SIZE || len > TASK_SIZE-start)
2595 return -EINVAL;
2596
2597 end = start + PAGE_ALIGN(len);
2598 if (end == start)
2599 return -EINVAL;
2600
2601 /* arch_unmap() might do unmaps itself. */
2602 arch_unmap(mm, start, end);
2603
2604 /* Find the first overlapping VMA */
183654ce 2605 vma = vma_find(vmi, end);
408579cd
LH
2606 if (!vma) {
2607 if (unlock)
2608 mmap_write_unlock(mm);
11f9a21a 2609 return 0;
408579cd 2610 }
11f9a21a 2611
408579cd 2612 return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock);
11f9a21a
LH
2613}
2614
2615/* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls.
2616 * @mm: The mm_struct
2617 * @start: The start address to munmap
2618 * @len: The length to be munmapped.
2619 * @uf: The userfaultfd list_head
408579cd
LH
2620 *
2621 * Return: 0 on success, error otherwise.
11f9a21a 2622 */
dd2283f2
YS
2623int do_munmap(struct mm_struct *mm, unsigned long start, size_t len,
2624 struct list_head *uf)
2625{
183654ce 2626 VMA_ITERATOR(vmi, mm, start);
11f9a21a 2627
183654ce 2628 return do_vmi_munmap(&vmi, mm, start, len, uf, false);
dd2283f2
YS
2629}
2630
e99668a5
LH
2631unsigned long mmap_region(struct file *file, unsigned long addr,
2632 unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
2633 struct list_head *uf)
2634{
2635 struct mm_struct *mm = current->mm;
2636 struct vm_area_struct *vma = NULL;
2637 struct vm_area_struct *next, *prev, *merge;
2638 pgoff_t pglen = len >> PAGE_SHIFT;
2639 unsigned long charged = 0;
2640 unsigned long end = addr + len;
2641 unsigned long merge_start = addr, merge_end = end;
2642 pgoff_t vm_pgoff;
2643 int error;
183654ce 2644 VMA_ITERATOR(vmi, mm, addr);
e99668a5
LH
2645
2646 /* Check against address space limit. */
2647 if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) {
2648 unsigned long nr_pages;
2649
2650 /*
2651 * MAP_FIXED may remove pages of mappings that intersects with
2652 * requested mapping. Account for the pages it would unmap.
2653 */
2654 nr_pages = count_vma_pages_range(mm, addr, end);
2655
2656 if (!may_expand_vm(mm, vm_flags,
2657 (len >> PAGE_SHIFT) - nr_pages))
2658 return -ENOMEM;
2659 }
2660
2661 /* Unmap any existing mapping in the area */
183654ce 2662 if (do_vmi_munmap(&vmi, mm, addr, len, uf, false))
e99668a5
LH
2663 return -ENOMEM;
2664
2665 /*
2666 * Private writable mapping: check memory availability
2667 */
2668 if (accountable_mapping(file, vm_flags)) {
2669 charged = len >> PAGE_SHIFT;
2670 if (security_vm_enough_memory_mm(mm, charged))
2671 return -ENOMEM;
2672 vm_flags |= VM_ACCOUNT;
2673 }
2674
183654ce
LH
2675 next = vma_next(&vmi);
2676 prev = vma_prev(&vmi);
e99668a5
LH
2677 if (vm_flags & VM_SPECIAL)
2678 goto cannot_expand;
2679
2680 /* Attempt to expand an old mapping */
2681 /* Check next */
2682 if (next && next->vm_start == end && !vma_policy(next) &&
2683 can_vma_merge_before(next, vm_flags, NULL, file, pgoff+pglen,
2684 NULL_VM_UFFD_CTX, NULL)) {
2685 merge_end = next->vm_end;
2686 vma = next;
2687 vm_pgoff = next->vm_pgoff - pglen;
2688 }
2689
2690 /* Check prev */
2691 if (prev && prev->vm_end == addr && !vma_policy(prev) &&
2692 (vma ? can_vma_merge_after(prev, vm_flags, vma->anon_vma, file,
2693 pgoff, vma->vm_userfaultfd_ctx, NULL) :
2694 can_vma_merge_after(prev, vm_flags, NULL, file, pgoff,
2695 NULL_VM_UFFD_CTX, NULL))) {
2696 merge_start = prev->vm_start;
2697 vma = prev;
2698 vm_pgoff = prev->vm_pgoff;
2699 }
2700
2701
2702 /* Actually expand, if possible */
2703 if (vma &&
3c441ab7 2704 !vma_expand(&vmi, vma, merge_start, merge_end, vm_pgoff, next)) {
e99668a5
LH
2705 khugepaged_enter_vma(vma, vm_flags);
2706 goto expanded;
2707 }
2708
e99668a5 2709cannot_expand:
5c1c03de
LH
2710 if (prev)
2711 vma_iter_next_range(&vmi);
2712
e99668a5
LH
2713 /*
2714 * Determine the object being mapped and call the appropriate
2715 * specific mapper. the address has already been validated, but
2716 * not unmapped, but the maps are removed from the list.
2717 */
2718 vma = vm_area_alloc(mm);
2719 if (!vma) {
2720 error = -ENOMEM;
2721 goto unacct_error;
2722 }
2723
0fd5a9e2 2724 vma_iter_set(&vmi, addr);
e99668a5
LH
2725 vma->vm_start = addr;
2726 vma->vm_end = end;
1c71222e 2727 vm_flags_init(vma, vm_flags);
e99668a5
LH
2728 vma->vm_page_prot = vm_get_page_prot(vm_flags);
2729 vma->vm_pgoff = pgoff;
2730
2731 if (file) {
2732 if (vm_flags & VM_SHARED) {
2733 error = mapping_map_writable(file->f_mapping);
2734 if (error)
2735 goto free_vma;
2736 }
2737
2738 vma->vm_file = get_file(file);
2739 error = call_mmap(file, vma);
2740 if (error)
2741 goto unmap_and_free_vma;
2742
a57b7051
LH
2743 /*
2744 * Expansion is handled above, merging is handled below.
2745 * Drivers should not alter the address of the VMA.
e99668a5 2746 */
cc8d1b09
LH
2747 error = -EINVAL;
2748 if (WARN_ON((addr != vma->vm_start)))
a57b7051 2749 goto close_and_free_vma;
e99668a5 2750
cc8d1b09 2751 vma_iter_set(&vmi, addr);
e99668a5
LH
2752 /*
2753 * If vm_flags changed after call_mmap(), we should try merge
2754 * vma again as we may succeed this time.
2755 */
2756 if (unlikely(vm_flags != vma->vm_flags && prev)) {
9760ebff
LH
2757 merge = vma_merge(&vmi, mm, prev, vma->vm_start,
2758 vma->vm_end, vma->vm_flags, NULL,
2759 vma->vm_file, vma->vm_pgoff, NULL,
2760 NULL_VM_UFFD_CTX, NULL);
e99668a5
LH
2761 if (merge) {
2762 /*
2763 * ->mmap() can change vma->vm_file and fput
2764 * the original file. So fput the vma->vm_file
2765 * here or we would add an extra fput for file
2766 * and cause general protection fault
2767 * ultimately.
2768 */
2769 fput(vma->vm_file);
2770 vm_area_free(vma);
2771 vma = merge;
2772 /* Update vm_flags to pick up the change. */
e99668a5
LH
2773 vm_flags = vma->vm_flags;
2774 goto unmap_writable;
2775 }
2776 }
2777
2778 vm_flags = vma->vm_flags;
2779 } else if (vm_flags & VM_SHARED) {
2780 error = shmem_zero_setup(vma);
2781 if (error)
2782 goto free_vma;
2783 } else {
2784 vma_set_anonymous(vma);
2785 }
2786
b507808e
JG
2787 if (map_deny_write_exec(vma, vma->vm_flags)) {
2788 error = -EACCES;
6bbf1090 2789 goto close_and_free_vma;
b507808e
JG
2790 }
2791
e99668a5 2792 /* Allow architectures to sanity-check the vm_flags */
cc8d1b09
LH
2793 error = -EINVAL;
2794 if (!arch_validate_flags(vma->vm_flags))
2795 goto close_and_free_vma;
e99668a5 2796
cc8d1b09
LH
2797 error = -ENOMEM;
2798 if (vma_iter_prealloc(&vmi))
2799 goto close_and_free_vma;
e99668a5 2800
1c7873e3
HD
2801 /* Lock the VMA since it is modified after insertion into VMA tree */
2802 vma_start_write(vma);
183654ce 2803 vma_iter_store(&vmi, vma);
e99668a5
LH
2804 mm->map_count++;
2805 if (vma->vm_file) {
6852c46c 2806 i_mmap_lock_write(vma->vm_file->f_mapping);
e99668a5
LH
2807 if (vma->vm_flags & VM_SHARED)
2808 mapping_allow_writable(vma->vm_file->f_mapping);
2809
2810 flush_dcache_mmap_lock(vma->vm_file->f_mapping);
2811 vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap);
2812 flush_dcache_mmap_unlock(vma->vm_file->f_mapping);
2813 i_mmap_unlock_write(vma->vm_file->f_mapping);
2814 }
2815
2816 /*
2817 * vma_merge() calls khugepaged_enter_vma() either, the below
2818 * call covers the non-merge case.
2819 */
2820 khugepaged_enter_vma(vma, vma->vm_flags);
2821
2822 /* Once vma denies write, undo our temporary denial count */
2823unmap_writable:
2824 if (file && vm_flags & VM_SHARED)
2825 mapping_unmap_writable(file->f_mapping);
2826 file = vma->vm_file;
d7597f59 2827 ksm_add_vma(vma);
e99668a5
LH
2828expanded:
2829 perf_event_mmap(vma);
2830
2831 vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT);
2832 if (vm_flags & VM_LOCKED) {
2833 if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) ||
2834 is_vm_hugetlb_page(vma) ||
2835 vma == get_gate_vma(current->mm))
e430a95a 2836 vm_flags_clear(vma, VM_LOCKED_MASK);
e99668a5
LH
2837 else
2838 mm->locked_vm += (len >> PAGE_SHIFT);
2839 }
2840
2841 if (file)
2842 uprobe_mmap(vma);
2843
2844 /*
2845 * New (or expanded) vma always get soft dirty status.
2846 * Otherwise user-space soft-dirty page tracker won't
2847 * be able to distinguish situation when vma area unmapped,
2848 * then new mapped in-place (which must be aimed as
2849 * a completely new data area).
2850 */
1c71222e 2851 vm_flags_set(vma, VM_SOFTDIRTY);
e99668a5
LH
2852
2853 vma_set_page_prot(vma);
2854
2855 validate_mm(mm);
2856 return addr;
2857
deb0f656 2858close_and_free_vma:
cc8d1b09 2859 if (file && vma->vm_ops && vma->vm_ops->close)
deb0f656 2860 vma->vm_ops->close(vma);
cc8d1b09
LH
2861
2862 if (file || vma->vm_file) {
e99668a5 2863unmap_and_free_vma:
cc8d1b09
LH
2864 fput(vma->vm_file);
2865 vma->vm_file = NULL;
e99668a5 2866
cc8d1b09
LH
2867 /* Undo any partial mapping done by a device driver. */
2868 unmap_region(mm, &mm->mm_mt, vma, prev, next, vma->vm_start,
68f48381 2869 vma->vm_end, true);
cc8d1b09 2870 }
cc674ab3 2871 if (file && (vm_flags & VM_SHARED))
e99668a5
LH
2872 mapping_unmap_writable(file->f_mapping);
2873free_vma:
2874 vm_area_free(vma);
2875unacct_error:
2876 if (charged)
2877 vm_unacct_memory(charged);
2878 validate_mm(mm);
2879 return error;
2880}
2881
408579cd 2882static int __vm_munmap(unsigned long start, size_t len, bool unlock)
1da177e4
LT
2883{
2884 int ret;
bfce281c 2885 struct mm_struct *mm = current->mm;
897ab3e0 2886 LIST_HEAD(uf);
183654ce 2887 VMA_ITERATOR(vmi, mm, start);
1da177e4 2888
d8ed45c5 2889 if (mmap_write_lock_killable(mm))
ae798783
MH
2890 return -EINTR;
2891
408579cd
LH
2892 ret = do_vmi_munmap(&vmi, mm, start, len, &uf, unlock);
2893 if (ret || !unlock)
d8ed45c5 2894 mmap_write_unlock(mm);
dd2283f2 2895
897ab3e0 2896 userfaultfd_unmap_complete(mm, &uf);
1da177e4
LT
2897 return ret;
2898}
dd2283f2
YS
2899
2900int vm_munmap(unsigned long start, size_t len)
2901{
2902 return __vm_munmap(start, len, false);
2903}
a46ef99d
LT
2904EXPORT_SYMBOL(vm_munmap);
2905
2906SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
2907{
ce18d171 2908 addr = untagged_addr(addr);
dd2283f2 2909 return __vm_munmap(addr, len, true);
a46ef99d 2910}
1da177e4 2911
c8d78c18
KS
2912
2913/*
2914 * Emulation of deprecated remap_file_pages() syscall.
2915 */
2916SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
2917 unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
2918{
2919
2920 struct mm_struct *mm = current->mm;
2921 struct vm_area_struct *vma;
2922 unsigned long populate = 0;
2923 unsigned long ret = -EINVAL;
2924 struct file *file;
2925
ee65728e 2926 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
756a025f 2927 current->comm, current->pid);
c8d78c18
KS
2928
2929 if (prot)
2930 return ret;
2931 start = start & PAGE_MASK;
2932 size = size & PAGE_MASK;
2933
2934 if (start + size <= start)
2935 return ret;
2936
2937 /* Does pgoff wrap? */
2938 if (pgoff + (size >> PAGE_SHIFT) < pgoff)
2939 return ret;
2940
d8ed45c5 2941 if (mmap_write_lock_killable(mm))
dc0ef0df
MH
2942 return -EINTR;
2943
9b593cb2 2944 vma = vma_lookup(mm, start);
c8d78c18
KS
2945
2946 if (!vma || !(vma->vm_flags & VM_SHARED))
2947 goto out;
2948
48f7df32 2949 if (start + size > vma->vm_end) {
763ecb03
LH
2950 VMA_ITERATOR(vmi, mm, vma->vm_end);
2951 struct vm_area_struct *next, *prev = vma;
48f7df32 2952
763ecb03 2953 for_each_vma_range(vmi, next, start + size) {
48f7df32 2954 /* hole between vmas ? */
763ecb03 2955 if (next->vm_start != prev->vm_end)
48f7df32
KS
2956 goto out;
2957
2958 if (next->vm_file != vma->vm_file)
2959 goto out;
2960
2961 if (next->vm_flags != vma->vm_flags)
2962 goto out;
2963
1db43d3f
LH
2964 if (start + size <= next->vm_end)
2965 break;
2966
763ecb03 2967 prev = next;
48f7df32
KS
2968 }
2969
2970 if (!next)
2971 goto out;
c8d78c18
KS
2972 }
2973
2974 prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
2975 prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
2976 prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
2977
2978 flags &= MAP_NONBLOCK;
2979 flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
fce000b1 2980 if (vma->vm_flags & VM_LOCKED)
c8d78c18 2981 flags |= MAP_LOCKED;
48f7df32 2982
c8d78c18 2983 file = get_file(vma->vm_file);
45e55300 2984 ret = do_mmap(vma->vm_file, start, size,
897ab3e0 2985 prot, flags, pgoff, &populate, NULL);
c8d78c18
KS
2986 fput(file);
2987out:
d8ed45c5 2988 mmap_write_unlock(mm);
c8d78c18
KS
2989 if (populate)
2990 mm_populate(ret, populate);
2991 if (!IS_ERR_VALUE(ret))
2992 ret = 0;
2993 return ret;
2994}
2995
1da177e4 2996/*
27b26701
LH
2997 * do_vma_munmap() - Unmap a full or partial vma.
2998 * @vmi: The vma iterator pointing at the vma
2999 * @vma: The first vma to be munmapped
3000 * @start: the start of the address to unmap
3001 * @end: The end of the address to unmap
2e7ce7d3 3002 * @uf: The userfaultfd list_head
408579cd 3003 * @unlock: Drop the lock on success
2e7ce7d3 3004 *
27b26701
LH
3005 * unmaps a VMA mapping when the vma iterator is already in position.
3006 * Does not handle alignment.
408579cd
LH
3007 *
3008 * Return: 0 on success drops the lock of so directed, error on failure and will
3009 * still hold the lock.
1da177e4 3010 */
27b26701 3011int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
408579cd
LH
3012 unsigned long start, unsigned long end, struct list_head *uf,
3013 bool unlock)
1da177e4 3014{
2e7ce7d3 3015 struct mm_struct *mm = vma->vm_mm;
3a459756 3016
27b26701 3017 arch_unmap(mm, start, end);
b5641a5d 3018 return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock);
2e7ce7d3 3019}
1da177e4 3020
2e7ce7d3
LH
3021/*
3022 * do_brk_flags() - Increase the brk vma if the flags match.
92fed820 3023 * @vmi: The vma iterator
2e7ce7d3
LH
3024 * @addr: The start address
3025 * @len: The length of the increase
3026 * @vma: The vma,
3027 * @flags: The VMA Flags
3028 *
3029 * Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags
3030 * do not match then create a new anonymous VMA. Eventually we may be able to
3031 * do some brk-specific accounting here.
3032 */
92fed820 3033static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,
763ecb03 3034 unsigned long addr, unsigned long len, unsigned long flags)
2e7ce7d3
LH
3035{
3036 struct mm_struct *mm = current->mm;
287051b1 3037 struct vma_prepare vp;
1da177e4 3038
2e7ce7d3
LH
3039 /*
3040 * Check against address space limits by the changed size
3041 * Note: This happens *after* clearing old mappings in some code paths.
3042 */
3043 flags |= VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
84638335 3044 if (!may_expand_vm(mm, flags, len >> PAGE_SHIFT))
1da177e4
LT
3045 return -ENOMEM;
3046
3047 if (mm->map_count > sysctl_max_map_count)
3048 return -ENOMEM;
3049
191c5424 3050 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
1da177e4
LT
3051 return -ENOMEM;
3052
1da177e4 3053 /*
2e7ce7d3
LH
3054 * Expand the existing vma if possible; Note that singular lists do not
3055 * occur after forking, so the expand will only happen on new VMAs.
1da177e4 3056 */
6c28ca64
LH
3057 if (vma && vma->vm_end == addr && !vma_policy(vma) &&
3058 can_vma_merge_after(vma, flags, NULL, NULL,
3059 addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) {
92fed820 3060 if (vma_iter_prealloc(vmi))
675eaca1 3061 goto unacct_fail;
28c5609f 3062
287051b1
LH
3063 init_vma_prep(&vp, vma);
3064 vma_prepare(&vp);
ccf1d78d 3065 vma_adjust_trans_huge(vma, vma->vm_start, addr + len, 0);
2e7ce7d3 3066 vma->vm_end = addr + len;
1c71222e 3067 vm_flags_set(vma, VM_SOFTDIRTY);
92fed820 3068 vma_iter_store(vmi, vma);
2e7ce7d3 3069
287051b1 3070 vma_complete(&vp, vmi, mm);
2e7ce7d3
LH
3071 khugepaged_enter_vma(vma, flags);
3072 goto out;
1da177e4 3073 }
2e7ce7d3
LH
3074
3075 /* create a vma struct for an anonymous mapping */
3076 vma = vm_area_alloc(mm);
3077 if (!vma)
675eaca1 3078 goto unacct_fail;
1da177e4 3079
bfd40eaf 3080 vma_set_anonymous(vma);
1da177e4
LT
3081 vma->vm_start = addr;
3082 vma->vm_end = addr + len;
2e7ce7d3 3083 vma->vm_pgoff = addr >> PAGE_SHIFT;
1c71222e 3084 vm_flags_init(vma, flags);
3ed75eb8 3085 vma->vm_page_prot = vm_get_page_prot(flags);
92fed820 3086 if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL))
2e7ce7d3 3087 goto mas_store_fail;
d4af56c5 3088
2e7ce7d3 3089 mm->map_count++;
2574d5e4 3090 validate_mm(mm);
d7597f59 3091 ksm_add_vma(vma);
1da177e4 3092out:
3af9e859 3093 perf_event_mmap(vma);
1da177e4 3094 mm->total_vm += len >> PAGE_SHIFT;
84638335 3095 mm->data_vm += len >> PAGE_SHIFT;
128557ff
ML
3096 if (flags & VM_LOCKED)
3097 mm->locked_vm += (len >> PAGE_SHIFT);
1c71222e 3098 vm_flags_set(vma, VM_SOFTDIRTY);
5d22fc25 3099 return 0;
d4af56c5 3100
2e7ce7d3 3101mas_store_fail:
d4af56c5 3102 vm_area_free(vma);
675eaca1 3103unacct_fail:
2e7ce7d3
LH
3104 vm_unacct_memory(len >> PAGE_SHIFT);
3105 return -ENOMEM;
1da177e4
LT
3106}
3107
bb177a73 3108int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags)
e4eb1ff6
LT
3109{
3110 struct mm_struct *mm = current->mm;
2e7ce7d3 3111 struct vm_area_struct *vma = NULL;
bb177a73 3112 unsigned long len;
5d22fc25 3113 int ret;
128557ff 3114 bool populate;
897ab3e0 3115 LIST_HEAD(uf);
92fed820 3116 VMA_ITERATOR(vmi, mm, addr);
e4eb1ff6 3117
bb177a73
MH
3118 len = PAGE_ALIGN(request);
3119 if (len < request)
3120 return -ENOMEM;
3121 if (!len)
3122 return 0;
3123
d8ed45c5 3124 if (mmap_write_lock_killable(mm))
2d6c9282
MH
3125 return -EINTR;
3126
2e7ce7d3
LH
3127 /* Until we need other flags, refuse anything except VM_EXEC. */
3128 if ((flags & (~VM_EXEC)) != 0)
3129 return -EINVAL;
3130
3131 ret = check_brk_limits(addr, len);
3132 if (ret)
3133 goto limits_failed;
3134
183654ce 3135 ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0);
2e7ce7d3
LH
3136 if (ret)
3137 goto munmap_failed;
3138
92fed820
LH
3139 vma = vma_prev(&vmi);
3140 ret = do_brk_flags(&vmi, vma, addr, len, flags);
128557ff 3141 populate = ((mm->def_flags & VM_LOCKED) != 0);
d8ed45c5 3142 mmap_write_unlock(mm);
897ab3e0 3143 userfaultfd_unmap_complete(mm, &uf);
5d22fc25 3144 if (populate && !ret)
128557ff 3145 mm_populate(addr, len);
e4eb1ff6 3146 return ret;
2e7ce7d3
LH
3147
3148munmap_failed:
3149limits_failed:
3150 mmap_write_unlock(mm);
3151 return ret;
e4eb1ff6 3152}
16e72e9b
DV
3153EXPORT_SYMBOL(vm_brk_flags);
3154
3155int vm_brk(unsigned long addr, unsigned long len)
3156{
3157 return vm_brk_flags(addr, len, 0);
3158}
e4eb1ff6 3159EXPORT_SYMBOL(vm_brk);
1da177e4
LT
3160
3161/* Release all mmaps. */
3162void exit_mmap(struct mm_struct *mm)
3163{
d16dfc55 3164 struct mmu_gather tlb;
ba470de4 3165 struct vm_area_struct *vma;
1da177e4 3166 unsigned long nr_accounted = 0;
763ecb03
LH
3167 MA_STATE(mas, &mm->mm_mt, 0, 0);
3168 int count = 0;
1da177e4 3169
d6dd61c8 3170 /* mm's last user has gone, and its about to be pulled down */
cddb8a5c 3171 mmu_notifier_release(mm);
d6dd61c8 3172
bf3980c8 3173 mmap_read_lock(mm);
9480c53e
JF
3174 arch_exit_mmap(mm);
3175
763ecb03 3176 vma = mas_find(&mas, ULONG_MAX);
64591e86
SB
3177 if (!vma) {
3178 /* Can happen if dup_mmap() received an OOM */
bf3980c8 3179 mmap_read_unlock(mm);
9480c53e 3180 return;
64591e86 3181 }
9480c53e 3182
1da177e4 3183 lru_add_drain();
1da177e4 3184 flush_cache_mm(mm);
d8b45053 3185 tlb_gather_mmu_fullmm(&tlb, mm);
901608d9 3186 /* update_hiwater_rss(mm) here? but nobody should be looking */
763ecb03 3187 /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */
68f48381 3188 unmap_vmas(&tlb, &mm->mm_mt, vma, 0, ULONG_MAX, false);
bf3980c8
SB
3189 mmap_read_unlock(mm);
3190
3191 /*
3192 * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper
b3541d91 3193 * because the memory has been already freed.
bf3980c8
SB
3194 */
3195 set_bit(MMF_OOM_SKIP, &mm->flags);
3196 mmap_write_lock(mm);
3dd44325 3197 mt_clear_in_rcu(&mm->mm_mt);
763ecb03 3198 free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS,
98e51a22 3199 USER_PGTABLES_CEILING, true);
ae8eba8b 3200 tlb_finish_mmu(&tlb);
1da177e4 3201
763ecb03
LH
3202 /*
3203 * Walk the list again, actually closing and freeing it, with preemption
3204 * enabled, without holding any MM locks besides the unreachable
3205 * mmap_write_lock.
3206 */
3207 do {
4f74d2c8
LT
3208 if (vma->vm_flags & VM_ACCOUNT)
3209 nr_accounted += vma_pages(vma);
0d2ebf9c 3210 remove_vma(vma, true);
763ecb03 3211 count++;
0a3b3c25 3212 cond_resched();
763ecb03
LH
3213 } while ((vma = mas_find(&mas, ULONG_MAX)) != NULL);
3214
3215 BUG_ON(count != mm->map_count);
d4af56c5
LH
3216
3217 trace_exit_mmap(mm);
3218 __mt_destroy(&mm->mm_mt);
64591e86 3219 mmap_write_unlock(mm);
4f74d2c8 3220 vm_unacct_memory(nr_accounted);
1da177e4
LT
3221}
3222
3223/* Insert vm structure into process list sorted by address
3224 * and into the inode's i_mmap tree. If vm_file is non-NULL
c8c06efa 3225 * then i_mmap_rwsem is taken here.
1da177e4 3226 */
6597d783 3227int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
1da177e4 3228{
d4af56c5 3229 unsigned long charged = vma_pages(vma);
1da177e4 3230
d4af56c5 3231
d0601a50 3232 if (find_vma_intersection(mm, vma->vm_start, vma->vm_end))
c9d13f5f 3233 return -ENOMEM;
d4af56c5 3234
c9d13f5f 3235 if ((vma->vm_flags & VM_ACCOUNT) &&
d4af56c5 3236 security_vm_enough_memory_mm(mm, charged))
c9d13f5f
CG
3237 return -ENOMEM;
3238
1da177e4
LT
3239 /*
3240 * The vm_pgoff of a purely anonymous vma should be irrelevant
3241 * until its first write fault, when page's anon_vma and index
3242 * are set. But now set the vm_pgoff it will almost certainly
3243 * end up with (unless mremap moves it elsewhere before that
3244 * first wfault), so /proc/pid/maps tells a consistent story.
3245 *
3246 * By setting it to reflect the virtual start address of the
3247 * vma, merges and splits can happen in a seamless way, just
3248 * using the existing file pgoff checks and manipulations.
8332326e 3249 * Similarly in do_mmap and in do_brk_flags.
1da177e4 3250 */
8a9cc3b5 3251 if (vma_is_anonymous(vma)) {
1da177e4
LT
3252 BUG_ON(vma->anon_vma);
3253 vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
3254 }
2b144498 3255
763ecb03 3256 if (vma_link(mm, vma)) {
d4af56c5
LH
3257 vm_unacct_memory(charged);
3258 return -ENOMEM;
3259 }
3260
1da177e4
LT
3261 return 0;
3262}
3263
3264/*
3265 * Copy the vma structure to a new location in the same mm,
3266 * prior to moving page table entries, to effect an mremap move.
3267 */
3268struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
38a76013
ML
3269 unsigned long addr, unsigned long len, pgoff_t pgoff,
3270 bool *need_rmap_locks)
1da177e4
LT
3271{
3272 struct vm_area_struct *vma = *vmap;
3273 unsigned long vma_start = vma->vm_start;
3274 struct mm_struct *mm = vma->vm_mm;
3275 struct vm_area_struct *new_vma, *prev;
948f017b 3276 bool faulted_in_anon_vma = true;
076f16bf 3277 VMA_ITERATOR(vmi, mm, addr);
1da177e4
LT
3278
3279 /*
3280 * If anonymous vma has not yet been faulted, update new pgoff
3281 * to match new location, to increase its chance of merging.
3282 */
ce75799b 3283 if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) {
1da177e4 3284 pgoff = addr >> PAGE_SHIFT;
948f017b
AA
3285 faulted_in_anon_vma = false;
3286 }
1da177e4 3287
763ecb03
LH
3288 new_vma = find_vma_prev(mm, addr, &prev);
3289 if (new_vma && new_vma->vm_start < addr + len)
6597d783 3290 return NULL; /* should never get here */
524e00b3 3291
9760ebff 3292 new_vma = vma_merge(&vmi, mm, prev, addr, addr + len, vma->vm_flags,
19a809af 3293 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
5c26f6ac 3294 vma->vm_userfaultfd_ctx, anon_vma_name(vma));
1da177e4
LT
3295 if (new_vma) {
3296 /*
3297 * Source vma may have been merged into new_vma
3298 */
948f017b
AA
3299 if (unlikely(vma_start >= new_vma->vm_start &&
3300 vma_start < new_vma->vm_end)) {
3301 /*
3302 * The only way we can get a vma_merge with
3303 * self during an mremap is if the vma hasn't
3304 * been faulted in yet and we were allowed to
3305 * reset the dst vma->vm_pgoff to the
3306 * destination address of the mremap to allow
3307 * the merge to happen. mremap must change the
3308 * vm_pgoff linearity between src and dst vmas
3309 * (in turn preventing a vma_merge) to be
3310 * safe. It is only safe to keep the vm_pgoff
3311 * linear if there are no pages mapped yet.
3312 */
81d1b09c 3313 VM_BUG_ON_VMA(faulted_in_anon_vma, new_vma);
38a76013 3314 *vmap = vma = new_vma;
108d6642 3315 }
38a76013 3316 *need_rmap_locks = (new_vma->vm_pgoff <= vma->vm_pgoff);
1da177e4 3317 } else {
3928d4f5 3318 new_vma = vm_area_dup(vma);
e3975891
CG
3319 if (!new_vma)
3320 goto out;
e3975891
CG
3321 new_vma->vm_start = addr;
3322 new_vma->vm_end = addr + len;
3323 new_vma->vm_pgoff = pgoff;
3324 if (vma_dup_policy(vma, new_vma))
3325 goto out_free_vma;
e3975891
CG
3326 if (anon_vma_clone(new_vma, vma))
3327 goto out_free_mempol;
3328 if (new_vma->vm_file)
3329 get_file(new_vma->vm_file);
3330 if (new_vma->vm_ops && new_vma->vm_ops->open)
3331 new_vma->vm_ops->open(new_vma);
d6ac235d 3332 vma_start_write(new_vma);
763ecb03 3333 if (vma_link(mm, new_vma))
524e00b3 3334 goto out_vma_link;
e3975891 3335 *need_rmap_locks = false;
1da177e4
LT
3336 }
3337 return new_vma;
5beb4930 3338
524e00b3
LH
3339out_vma_link:
3340 if (new_vma->vm_ops && new_vma->vm_ops->close)
3341 new_vma->vm_ops->close(new_vma);
92b73996
LH
3342
3343 if (new_vma->vm_file)
3344 fput(new_vma->vm_file);
3345
3346 unlink_anon_vmas(new_vma);
e3975891 3347out_free_mempol:
ef0855d3 3348 mpol_put(vma_policy(new_vma));
e3975891 3349out_free_vma:
3928d4f5 3350 vm_area_free(new_vma);
e3975891 3351out:
5beb4930 3352 return NULL;
1da177e4 3353}
119f657c 3354
3355/*
3356 * Return true if the calling process may expand its vm space by the passed
3357 * number of pages
3358 */
84638335 3359bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
119f657c 3360{
84638335
KK
3361 if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
3362 return false;
119f657c 3363
d977d56c
KK
3364 if (is_data_mapping(flags) &&
3365 mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
f4fcd558
KK
3366 /* Workaround for Valgrind */
3367 if (rlimit(RLIMIT_DATA) == 0 &&
3368 mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT)
3369 return true;
57a7702b
DW
3370
3371 pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n",
3372 current->comm, current->pid,
3373 (mm->data_vm + npages) << PAGE_SHIFT,
3374 rlimit(RLIMIT_DATA),
3375 ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data");
3376
3377 if (!ignore_rlimit_data)
d977d56c
KK
3378 return false;
3379 }
119f657c 3380
84638335
KK
3381 return true;
3382}
3383
3384void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
3385{
7866076b 3386 WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages);
84638335 3387
d977d56c 3388 if (is_exec_mapping(flags))
84638335 3389 mm->exec_vm += npages;
d977d56c 3390 else if (is_stack_mapping(flags))
84638335 3391 mm->stack_vm += npages;
d977d56c 3392 else if (is_data_mapping(flags))
84638335 3393 mm->data_vm += npages;
119f657c 3394}
fa5dc22f 3395
b3ec9f33 3396static vm_fault_t special_mapping_fault(struct vm_fault *vmf);
a62c34bd
AL
3397
3398/*
3399 * Having a close hook prevents vma merging regardless of flags.
3400 */
3401static void special_mapping_close(struct vm_area_struct *vma)
3402{
3403}
3404
3405static const char *special_mapping_name(struct vm_area_struct *vma)
3406{
3407 return ((struct vm_special_mapping *)vma->vm_private_data)->name;
3408}
3409
14d07113 3410static int special_mapping_mremap(struct vm_area_struct *new_vma)
b059a453
DS
3411{
3412 struct vm_special_mapping *sm = new_vma->vm_private_data;
3413
280e87e9
DS
3414 if (WARN_ON_ONCE(current->mm != new_vma->vm_mm))
3415 return -EFAULT;
3416
b059a453
DS
3417 if (sm->mremap)
3418 return sm->mremap(sm, new_vma);
280e87e9 3419
b059a453
DS
3420 return 0;
3421}
3422
871402e0
DS
3423static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr)
3424{
3425 /*
3426 * Forbid splitting special mappings - kernel has expectations over
3427 * the number of pages in mapping. Together with VM_DONTEXPAND
3428 * the size of vma should stay the same over the special mapping's
3429 * lifetime.
3430 */
3431 return -EINVAL;
3432}
3433
a62c34bd
AL
3434static const struct vm_operations_struct special_mapping_vmops = {
3435 .close = special_mapping_close,
3436 .fault = special_mapping_fault,
b059a453 3437 .mremap = special_mapping_mremap,
a62c34bd 3438 .name = special_mapping_name,
af34ebeb
DS
3439 /* vDSO code relies that VVAR can't be accessed remotely */
3440 .access = NULL,
871402e0 3441 .may_split = special_mapping_split,
a62c34bd
AL
3442};
3443
3444static const struct vm_operations_struct legacy_special_mapping_vmops = {
3445 .close = special_mapping_close,
3446 .fault = special_mapping_fault,
3447};
fa5dc22f 3448
b3ec9f33 3449static vm_fault_t special_mapping_fault(struct vm_fault *vmf)
fa5dc22f 3450{
11bac800 3451 struct vm_area_struct *vma = vmf->vma;
b1d0e4f5 3452 pgoff_t pgoff;
fa5dc22f
RM
3453 struct page **pages;
3454
f872f540 3455 if (vma->vm_ops == &legacy_special_mapping_vmops) {
a62c34bd 3456 pages = vma->vm_private_data;
f872f540
AL
3457 } else {
3458 struct vm_special_mapping *sm = vma->vm_private_data;
3459
3460 if (sm->fault)
11bac800 3461 return sm->fault(sm, vmf->vma, vmf);
f872f540
AL
3462
3463 pages = sm->pages;
3464 }
a62c34bd 3465
8a9cc3b5 3466 for (pgoff = vmf->pgoff; pgoff && *pages; ++pages)
b1d0e4f5 3467 pgoff--;
fa5dc22f
RM
3468
3469 if (*pages) {
3470 struct page *page = *pages;
3471 get_page(page);
b1d0e4f5
NP
3472 vmf->page = page;
3473 return 0;
fa5dc22f
RM
3474 }
3475
b1d0e4f5 3476 return VM_FAULT_SIGBUS;
fa5dc22f
RM
3477}
3478
a62c34bd
AL
3479static struct vm_area_struct *__install_special_mapping(
3480 struct mm_struct *mm,
3481 unsigned long addr, unsigned long len,
27f28b97
CG
3482 unsigned long vm_flags, void *priv,
3483 const struct vm_operations_struct *ops)
fa5dc22f 3484{
462e635e 3485 int ret;
fa5dc22f
RM
3486 struct vm_area_struct *vma;
3487
490fc053 3488 vma = vm_area_alloc(mm);
fa5dc22f 3489 if (unlikely(vma == NULL))
3935ed6a 3490 return ERR_PTR(-ENOMEM);
fa5dc22f 3491
fa5dc22f
RM
3492 vma->vm_start = addr;
3493 vma->vm_end = addr + len;
3494
e430a95a
SB
3495 vm_flags_init(vma, (vm_flags | mm->def_flags |
3496 VM_DONTEXPAND | VM_SOFTDIRTY) & ~VM_LOCKED_MASK);
3ed75eb8 3497 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
fa5dc22f 3498
a62c34bd
AL
3499 vma->vm_ops = ops;
3500 vma->vm_private_data = priv;
fa5dc22f 3501
462e635e
TO
3502 ret = insert_vm_struct(mm, vma);
3503 if (ret)
3504 goto out;
fa5dc22f 3505
84638335 3506 vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT);
fa5dc22f 3507
cdd6c482 3508 perf_event_mmap(vma);
089dd79d 3509
3935ed6a 3510 return vma;
462e635e
TO
3511
3512out:
3928d4f5 3513 vm_area_free(vma);
3935ed6a
SS
3514 return ERR_PTR(ret);
3515}
3516
2eefd878
DS
3517bool vma_is_special_mapping(const struct vm_area_struct *vma,
3518 const struct vm_special_mapping *sm)
3519{
3520 return vma->vm_private_data == sm &&
3521 (vma->vm_ops == &special_mapping_vmops ||
3522 vma->vm_ops == &legacy_special_mapping_vmops);
3523}
3524
a62c34bd 3525/*
c1e8d7c6 3526 * Called with mm->mmap_lock held for writing.
a62c34bd
AL
3527 * Insert a new vma covering the given region, with the given flags.
3528 * Its pages are supplied by the given array of struct page *.
3529 * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
3530 * The region past the last page supplied will always produce SIGBUS.
3531 * The array pointer and the pages it points to are assumed to stay alive
3532 * for as long as this mapping might exist.
3533 */
3534struct vm_area_struct *_install_special_mapping(
3535 struct mm_struct *mm,
3536 unsigned long addr, unsigned long len,
3537 unsigned long vm_flags, const struct vm_special_mapping *spec)
3538{
27f28b97
CG
3539 return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
3540 &special_mapping_vmops);
a62c34bd
AL
3541}
3542
3935ed6a
SS
3543int install_special_mapping(struct mm_struct *mm,
3544 unsigned long addr, unsigned long len,
3545 unsigned long vm_flags, struct page **pages)
3546{
a62c34bd 3547 struct vm_area_struct *vma = __install_special_mapping(
27f28b97
CG
3548 mm, addr, len, vm_flags, (void *)pages,
3549 &legacy_special_mapping_vmops);
3935ed6a 3550
14bd5b45 3551 return PTR_ERR_OR_ZERO(vma);
fa5dc22f 3552}
7906d00c
AA
3553
3554static DEFINE_MUTEX(mm_all_locks_mutex);
3555
454ed842 3556static void vm_lock_anon_vma(struct mm_struct *mm, struct anon_vma *anon_vma)
7906d00c 3557{
f808c13f 3558 if (!test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
7906d00c
AA
3559 /*
3560 * The LSB of head.next can't change from under us
3561 * because we hold the mm_all_locks_mutex.
3562 */
da1c55f1 3563 down_write_nest_lock(&anon_vma->root->rwsem, &mm->mmap_lock);
7906d00c
AA
3564 /*
3565 * We can safely modify head.next after taking the
5a505085 3566 * anon_vma->root->rwsem. If some other vma in this mm shares
7906d00c
AA
3567 * the same anon_vma we won't take it again.
3568 *
3569 * No need of atomic instructions here, head.next
3570 * can't change from under us thanks to the
5a505085 3571 * anon_vma->root->rwsem.
7906d00c
AA
3572 */
3573 if (__test_and_set_bit(0, (unsigned long *)
f808c13f 3574 &anon_vma->root->rb_root.rb_root.rb_node))
7906d00c
AA
3575 BUG();
3576 }
3577}
3578
454ed842 3579static void vm_lock_mapping(struct mm_struct *mm, struct address_space *mapping)
7906d00c
AA
3580{
3581 if (!test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3582 /*
3583 * AS_MM_ALL_LOCKS can't change from under us because
3584 * we hold the mm_all_locks_mutex.
3585 *
3586 * Operations on ->flags have to be atomic because
3587 * even if AS_MM_ALL_LOCKS is stable thanks to the
3588 * mm_all_locks_mutex, there may be other cpus
3589 * changing other bitflags in parallel to us.
3590 */
3591 if (test_and_set_bit(AS_MM_ALL_LOCKS, &mapping->flags))
3592 BUG();
da1c55f1 3593 down_write_nest_lock(&mapping->i_mmap_rwsem, &mm->mmap_lock);
7906d00c
AA
3594 }
3595}
3596
3597/*
3598 * This operation locks against the VM for all pte/vma/mm related
3599 * operations that could ever happen on a certain mm. This includes
3600 * vmtruncate, try_to_unmap, and all page faults.
3601 *
c1e8d7c6 3602 * The caller must take the mmap_lock in write mode before calling
7906d00c 3603 * mm_take_all_locks(). The caller isn't allowed to release the
c1e8d7c6 3604 * mmap_lock until mm_drop_all_locks() returns.
7906d00c 3605 *
c1e8d7c6 3606 * mmap_lock in write mode is required in order to block all operations
7906d00c 3607 * that could modify pagetables and free pages without need of
27ba0644 3608 * altering the vma layout. It's also needed in write mode to avoid new
7906d00c
AA
3609 * anon_vmas to be associated with existing vmas.
3610 *
3611 * A single task can't take more than one mm_take_all_locks() in a row
3612 * or it would deadlock.
3613 *
bf181b9f 3614 * The LSB in anon_vma->rb_root.rb_node and the AS_MM_ALL_LOCKS bitflag in
7906d00c
AA
3615 * mapping->flags avoid to take the same lock twice, if more than one
3616 * vma in this mm is backed by the same anon_vma or address_space.
3617 *
88f306b6
KS
3618 * We take locks in following order, accordingly to comment at beginning
3619 * of mm/rmap.c:
3620 * - all hugetlbfs_i_mmap_rwsem_key locks (aka mapping->i_mmap_rwsem for
3621 * hugetlb mapping);
eeff9a5d 3622 * - all vmas marked locked
88f306b6
KS
3623 * - all i_mmap_rwsem locks;
3624 * - all anon_vma->rwseml
3625 *
3626 * We can take all locks within these types randomly because the VM code
3627 * doesn't nest them and we protected from parallel mm_take_all_locks() by
3628 * mm_all_locks_mutex.
7906d00c
AA
3629 *
3630 * mm_take_all_locks() and mm_drop_all_locks are expensive operations
3631 * that may have to take thousand of locks.
3632 *
3633 * mm_take_all_locks() can fail if it's interrupted by signals.
3634 */
3635int mm_take_all_locks(struct mm_struct *mm)
3636{
3637 struct vm_area_struct *vma;
5beb4930 3638 struct anon_vma_chain *avc;
763ecb03 3639 MA_STATE(mas, &mm->mm_mt, 0, 0);
7906d00c 3640
325bca1f 3641 mmap_assert_write_locked(mm);
7906d00c
AA
3642
3643 mutex_lock(&mm_all_locks_mutex);
3644
eeff9a5d
SB
3645 mas_for_each(&mas, vma, ULONG_MAX) {
3646 if (signal_pending(current))
3647 goto out_unlock;
3648 vma_start_write(vma);
3649 }
3650
3651 mas_set(&mas, 0);
763ecb03 3652 mas_for_each(&mas, vma, ULONG_MAX) {
7906d00c
AA
3653 if (signal_pending(current))
3654 goto out_unlock;
88f306b6
KS
3655 if (vma->vm_file && vma->vm_file->f_mapping &&
3656 is_vm_hugetlb_page(vma))
3657 vm_lock_mapping(mm, vma->vm_file->f_mapping);
3658 }
3659
763ecb03
LH
3660 mas_set(&mas, 0);
3661 mas_for_each(&mas, vma, ULONG_MAX) {
88f306b6
KS
3662 if (signal_pending(current))
3663 goto out_unlock;
3664 if (vma->vm_file && vma->vm_file->f_mapping &&
3665 !is_vm_hugetlb_page(vma))
454ed842 3666 vm_lock_mapping(mm, vma->vm_file->f_mapping);
7906d00c 3667 }
7cd5a02f 3668
763ecb03
LH
3669 mas_set(&mas, 0);
3670 mas_for_each(&mas, vma, ULONG_MAX) {
7cd5a02f
PZ
3671 if (signal_pending(current))
3672 goto out_unlock;
3673 if (vma->anon_vma)
5beb4930
RR
3674 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3675 vm_lock_anon_vma(mm, avc->anon_vma);
7906d00c 3676 }
7cd5a02f 3677
584cff54 3678 return 0;
7906d00c
AA
3679
3680out_unlock:
584cff54
KC
3681 mm_drop_all_locks(mm);
3682 return -EINTR;
7906d00c
AA
3683}
3684
3685static void vm_unlock_anon_vma(struct anon_vma *anon_vma)
3686{
f808c13f 3687 if (test_bit(0, (unsigned long *) &anon_vma->root->rb_root.rb_root.rb_node)) {
7906d00c
AA
3688 /*
3689 * The LSB of head.next can't change to 0 from under
3690 * us because we hold the mm_all_locks_mutex.
3691 *
3692 * We must however clear the bitflag before unlocking
bf181b9f 3693 * the vma so the users using the anon_vma->rb_root will
7906d00c
AA
3694 * never see our bitflag.
3695 *
3696 * No need of atomic instructions here, head.next
3697 * can't change from under us until we release the
5a505085 3698 * anon_vma->root->rwsem.
7906d00c
AA
3699 */
3700 if (!__test_and_clear_bit(0, (unsigned long *)
f808c13f 3701 &anon_vma->root->rb_root.rb_root.rb_node))
7906d00c 3702 BUG();
08b52706 3703 anon_vma_unlock_write(anon_vma);
7906d00c
AA
3704 }
3705}
3706
3707static void vm_unlock_mapping(struct address_space *mapping)
3708{
3709 if (test_bit(AS_MM_ALL_LOCKS, &mapping->flags)) {
3710 /*
3711 * AS_MM_ALL_LOCKS can't change to 0 from under us
3712 * because we hold the mm_all_locks_mutex.
3713 */
83cde9e8 3714 i_mmap_unlock_write(mapping);
7906d00c
AA
3715 if (!test_and_clear_bit(AS_MM_ALL_LOCKS,
3716 &mapping->flags))
3717 BUG();
3718 }
3719}
3720
3721/*
c1e8d7c6 3722 * The mmap_lock cannot be released by the caller until
7906d00c
AA
3723 * mm_drop_all_locks() returns.
3724 */
3725void mm_drop_all_locks(struct mm_struct *mm)
3726{
3727 struct vm_area_struct *vma;
5beb4930 3728 struct anon_vma_chain *avc;
763ecb03 3729 MA_STATE(mas, &mm->mm_mt, 0, 0);
7906d00c 3730
325bca1f 3731 mmap_assert_write_locked(mm);
7906d00c
AA
3732 BUG_ON(!mutex_is_locked(&mm_all_locks_mutex));
3733
763ecb03 3734 mas_for_each(&mas, vma, ULONG_MAX) {
7906d00c 3735 if (vma->anon_vma)
5beb4930
RR
3736 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma)
3737 vm_unlock_anon_vma(avc->anon_vma);
7906d00c
AA
3738 if (vma->vm_file && vma->vm_file->f_mapping)
3739 vm_unlock_mapping(vma->vm_file->f_mapping);
3740 }
eeff9a5d 3741 vma_end_write_all(mm);
7906d00c
AA
3742
3743 mutex_unlock(&mm_all_locks_mutex);
3744}
8feae131
DH
3745
3746/*
3edf41d8 3747 * initialise the percpu counter for VM
8feae131
DH
3748 */
3749void __init mmap_init(void)
3750{
00a62ce9
KM
3751 int ret;
3752
908c7f19 3753 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
00a62ce9 3754 VM_BUG_ON(ret);
8feae131 3755}
c9b1d098
AS
3756
3757/*
3758 * Initialise sysctl_user_reserve_kbytes.
3759 *
3760 * This is intended to prevent a user from starting a single memory hogging
3761 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
3762 * mode.
3763 *
3764 * The default value is min(3% of free memory, 128MB)
3765 * 128MB is enough to recover with sshd/login, bash, and top/kill.
3766 */
1640879a 3767static int init_user_reserve(void)
c9b1d098
AS
3768{
3769 unsigned long free_kbytes;
3770
c41f012a 3771 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
c9b1d098
AS
3772
3773 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
3774 return 0;
3775}
a64fb3cd 3776subsys_initcall(init_user_reserve);
4eeab4f5
AS
3777
3778/*
3779 * Initialise sysctl_admin_reserve_kbytes.
3780 *
3781 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
3782 * to log in and kill a memory hogging process.
3783 *
3784 * Systems with more than 256MB will reserve 8MB, enough to recover
3785 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
3786 * only reserve 3% of free pages by default.
3787 */
1640879a 3788static int init_admin_reserve(void)
4eeab4f5
AS
3789{
3790 unsigned long free_kbytes;
3791
c41f012a 3792 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
4eeab4f5
AS
3793
3794 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
3795 return 0;
3796}
a64fb3cd 3797subsys_initcall(init_admin_reserve);
1640879a
AS
3798
3799/*
3800 * Reinititalise user and admin reserves if memory is added or removed.
3801 *
3802 * The default user reserve max is 128MB, and the default max for the
3803 * admin reserve is 8MB. These are usually, but not always, enough to
3804 * enable recovery from a memory hogging process using login/sshd, a shell,
3805 * and tools like top. It may make sense to increase or even disable the
3806 * reserve depending on the existence of swap or variations in the recovery
3807 * tools. So, the admin may have changed them.
3808 *
3809 * If memory is added and the reserves have been eliminated or increased above
3810 * the default max, then we'll trust the admin.
3811 *
3812 * If memory is removed and there isn't enough free memory, then we
3813 * need to reset the reserves.
3814 *
3815 * Otherwise keep the reserve set by the admin.
3816 */
3817static int reserve_mem_notifier(struct notifier_block *nb,
3818 unsigned long action, void *data)
3819{
3820 unsigned long tmp, free_kbytes;
3821
3822 switch (action) {
3823 case MEM_ONLINE:
3824 /* Default max is 128MB. Leave alone if modified by operator. */
3825 tmp = sysctl_user_reserve_kbytes;
3826 if (0 < tmp && tmp < (1UL << 17))
3827 init_user_reserve();
3828
3829 /* Default max is 8MB. Leave alone if modified by operator. */
3830 tmp = sysctl_admin_reserve_kbytes;
3831 if (0 < tmp && tmp < (1UL << 13))
3832 init_admin_reserve();
3833
3834 break;
3835 case MEM_OFFLINE:
c41f012a 3836 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
1640879a
AS
3837
3838 if (sysctl_user_reserve_kbytes > free_kbytes) {
3839 init_user_reserve();
3840 pr_info("vm.user_reserve_kbytes reset to %lu\n",
3841 sysctl_user_reserve_kbytes);
3842 }
3843
3844 if (sysctl_admin_reserve_kbytes > free_kbytes) {
3845 init_admin_reserve();
3846 pr_info("vm.admin_reserve_kbytes reset to %lu\n",
3847 sysctl_admin_reserve_kbytes);
3848 }
3849 break;
3850 default:
3851 break;
3852 }
3853 return NOTIFY_OK;
3854}
3855
1640879a
AS
3856static int __meminit init_reserve_notifier(void)
3857{
1eeaa4fd 3858 if (hotplug_memory_notifier(reserve_mem_notifier, DEFAULT_CALLBACK_PRI))
b1de0d13 3859 pr_err("Failed registering memory add/remove notifier for admin reserve\n");
1640879a
AS
3860
3861 return 0;
3862}
a64fb3cd 3863subsys_initcall(init_reserve_notifier);