LCOV - code coverage report
Current view: top level - mm - mmap.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 14 1142 1.2 %
Date: 2023-04-06 08:38:28 Functions: 4 84 4.8 %

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

Generated by: LCOV version 1.14