Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/mm/mlock.c
0004  *
0005  *  (C) Copyright 1995 Linus Torvalds
0006  *  (C) Copyright 2002 Christoph Hellwig
0007  */
0008 
0009 #include <linux/capability.h>
0010 #include <linux/mman.h>
0011 #include <linux/mm.h>
0012 #include <linux/sched/user.h>
0013 #include <linux/swap.h>
0014 #include <linux/swapops.h>
0015 #include <linux/pagemap.h>
0016 #include <linux/pagevec.h>
0017 #include <linux/pagewalk.h>
0018 #include <linux/mempolicy.h>
0019 #include <linux/syscalls.h>
0020 #include <linux/sched.h>
0021 #include <linux/export.h>
0022 #include <linux/rmap.h>
0023 #include <linux/mmzone.h>
0024 #include <linux/hugetlb.h>
0025 #include <linux/memcontrol.h>
0026 #include <linux/mm_inline.h>
0027 #include <linux/secretmem.h>
0028 
0029 #include "internal.h"
0030 
0031 struct mlock_pvec {
0032     local_lock_t lock;
0033     struct pagevec vec;
0034 };
0035 
0036 static DEFINE_PER_CPU(struct mlock_pvec, mlock_pvec) = {
0037     .lock = INIT_LOCAL_LOCK(lock),
0038 };
0039 
0040 bool can_do_mlock(void)
0041 {
0042     if (rlimit(RLIMIT_MEMLOCK) != 0)
0043         return true;
0044     if (capable(CAP_IPC_LOCK))
0045         return true;
0046     return false;
0047 }
0048 EXPORT_SYMBOL(can_do_mlock);
0049 
0050 /*
0051  * Mlocked pages are marked with PageMlocked() flag for efficient testing
0052  * in vmscan and, possibly, the fault path; and to support semi-accurate
0053  * statistics.
0054  *
0055  * An mlocked page [PageMlocked(page)] is unevictable.  As such, it will
0056  * be placed on the LRU "unevictable" list, rather than the [in]active lists.
0057  * The unevictable list is an LRU sibling list to the [in]active lists.
0058  * PageUnevictable is set to indicate the unevictable state.
0059  */
0060 
0061 static struct lruvec *__mlock_page(struct page *page, struct lruvec *lruvec)
0062 {
0063     /* There is nothing more we can do while it's off LRU */
0064     if (!TestClearPageLRU(page))
0065         return lruvec;
0066 
0067     lruvec = folio_lruvec_relock_irq(page_folio(page), lruvec);
0068 
0069     if (unlikely(page_evictable(page))) {
0070         /*
0071          * This is a little surprising, but quite possible:
0072          * PageMlocked must have got cleared already by another CPU.
0073          * Could this page be on the Unevictable LRU?  I'm not sure,
0074          * but move it now if so.
0075          */
0076         if (PageUnevictable(page)) {
0077             del_page_from_lru_list(page, lruvec);
0078             ClearPageUnevictable(page);
0079             add_page_to_lru_list(page, lruvec);
0080             __count_vm_events(UNEVICTABLE_PGRESCUED,
0081                       thp_nr_pages(page));
0082         }
0083         goto out;
0084     }
0085 
0086     if (PageUnevictable(page)) {
0087         if (PageMlocked(page))
0088             page->mlock_count++;
0089         goto out;
0090     }
0091 
0092     del_page_from_lru_list(page, lruvec);
0093     ClearPageActive(page);
0094     SetPageUnevictable(page);
0095     page->mlock_count = !!PageMlocked(page);
0096     add_page_to_lru_list(page, lruvec);
0097     __count_vm_events(UNEVICTABLE_PGCULLED, thp_nr_pages(page));
0098 out:
0099     SetPageLRU(page);
0100     return lruvec;
0101 }
0102 
0103 static struct lruvec *__mlock_new_page(struct page *page, struct lruvec *lruvec)
0104 {
0105     VM_BUG_ON_PAGE(PageLRU(page), page);
0106 
0107     lruvec = folio_lruvec_relock_irq(page_folio(page), lruvec);
0108 
0109     /* As above, this is a little surprising, but possible */
0110     if (unlikely(page_evictable(page)))
0111         goto out;
0112 
0113     SetPageUnevictable(page);
0114     page->mlock_count = !!PageMlocked(page);
0115     __count_vm_events(UNEVICTABLE_PGCULLED, thp_nr_pages(page));
0116 out:
0117     add_page_to_lru_list(page, lruvec);
0118     SetPageLRU(page);
0119     return lruvec;
0120 }
0121 
0122 static struct lruvec *__munlock_page(struct page *page, struct lruvec *lruvec)
0123 {
0124     int nr_pages = thp_nr_pages(page);
0125     bool isolated = false;
0126 
0127     if (!TestClearPageLRU(page))
0128         goto munlock;
0129 
0130     isolated = true;
0131     lruvec = folio_lruvec_relock_irq(page_folio(page), lruvec);
0132 
0133     if (PageUnevictable(page)) {
0134         /* Then mlock_count is maintained, but might undercount */
0135         if (page->mlock_count)
0136             page->mlock_count--;
0137         if (page->mlock_count)
0138             goto out;
0139     }
0140     /* else assume that was the last mlock: reclaim will fix it if not */
0141 
0142 munlock:
0143     if (TestClearPageMlocked(page)) {
0144         __mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
0145         if (isolated || !PageUnevictable(page))
0146             __count_vm_events(UNEVICTABLE_PGMUNLOCKED, nr_pages);
0147         else
0148             __count_vm_events(UNEVICTABLE_PGSTRANDED, nr_pages);
0149     }
0150 
0151     /* page_evictable() has to be checked *after* clearing Mlocked */
0152     if (isolated && PageUnevictable(page) && page_evictable(page)) {
0153         del_page_from_lru_list(page, lruvec);
0154         ClearPageUnevictable(page);
0155         add_page_to_lru_list(page, lruvec);
0156         __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
0157     }
0158 out:
0159     if (isolated)
0160         SetPageLRU(page);
0161     return lruvec;
0162 }
0163 
0164 /*
0165  * Flags held in the low bits of a struct page pointer on the mlock_pvec.
0166  */
0167 #define LRU_PAGE 0x1
0168 #define NEW_PAGE 0x2
0169 static inline struct page *mlock_lru(struct page *page)
0170 {
0171     return (struct page *)((unsigned long)page + LRU_PAGE);
0172 }
0173 
0174 static inline struct page *mlock_new(struct page *page)
0175 {
0176     return (struct page *)((unsigned long)page + NEW_PAGE);
0177 }
0178 
0179 /*
0180  * mlock_pagevec() is derived from pagevec_lru_move_fn():
0181  * perhaps that can make use of such page pointer flags in future,
0182  * but for now just keep it for mlock.  We could use three separate
0183  * pagevecs instead, but one feels better (munlocking a full pagevec
0184  * does not need to drain mlocking pagevecs first).
0185  */
0186 static void mlock_pagevec(struct pagevec *pvec)
0187 {
0188     struct lruvec *lruvec = NULL;
0189     unsigned long mlock;
0190     struct page *page;
0191     int i;
0192 
0193     for (i = 0; i < pagevec_count(pvec); i++) {
0194         page = pvec->pages[i];
0195         mlock = (unsigned long)page & (LRU_PAGE | NEW_PAGE);
0196         page = (struct page *)((unsigned long)page - mlock);
0197         pvec->pages[i] = page;
0198 
0199         if (mlock & LRU_PAGE)
0200             lruvec = __mlock_page(page, lruvec);
0201         else if (mlock & NEW_PAGE)
0202             lruvec = __mlock_new_page(page, lruvec);
0203         else
0204             lruvec = __munlock_page(page, lruvec);
0205     }
0206 
0207     if (lruvec)
0208         unlock_page_lruvec_irq(lruvec);
0209     release_pages(pvec->pages, pvec->nr);
0210     pagevec_reinit(pvec);
0211 }
0212 
0213 void mlock_page_drain_local(void)
0214 {
0215     struct pagevec *pvec;
0216 
0217     local_lock(&mlock_pvec.lock);
0218     pvec = this_cpu_ptr(&mlock_pvec.vec);
0219     if (pagevec_count(pvec))
0220         mlock_pagevec(pvec);
0221     local_unlock(&mlock_pvec.lock);
0222 }
0223 
0224 void mlock_page_drain_remote(int cpu)
0225 {
0226     struct pagevec *pvec;
0227 
0228     WARN_ON_ONCE(cpu_online(cpu));
0229     pvec = &per_cpu(mlock_pvec.vec, cpu);
0230     if (pagevec_count(pvec))
0231         mlock_pagevec(pvec);
0232 }
0233 
0234 bool need_mlock_page_drain(int cpu)
0235 {
0236     return pagevec_count(&per_cpu(mlock_pvec.vec, cpu));
0237 }
0238 
0239 /**
0240  * mlock_folio - mlock a folio already on (or temporarily off) LRU
0241  * @folio: folio to be mlocked.
0242  */
0243 void mlock_folio(struct folio *folio)
0244 {
0245     struct pagevec *pvec;
0246 
0247     local_lock(&mlock_pvec.lock);
0248     pvec = this_cpu_ptr(&mlock_pvec.vec);
0249 
0250     if (!folio_test_set_mlocked(folio)) {
0251         int nr_pages = folio_nr_pages(folio);
0252 
0253         zone_stat_mod_folio(folio, NR_MLOCK, nr_pages);
0254         __count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
0255     }
0256 
0257     folio_get(folio);
0258     if (!pagevec_add(pvec, mlock_lru(&folio->page)) ||
0259         folio_test_large(folio) || lru_cache_disabled())
0260         mlock_pagevec(pvec);
0261     local_unlock(&mlock_pvec.lock);
0262 }
0263 
0264 /**
0265  * mlock_new_page - mlock a newly allocated page not yet on LRU
0266  * @page: page to be mlocked, either a normal page or a THP head.
0267  */
0268 void mlock_new_page(struct page *page)
0269 {
0270     struct pagevec *pvec;
0271     int nr_pages = thp_nr_pages(page);
0272 
0273     local_lock(&mlock_pvec.lock);
0274     pvec = this_cpu_ptr(&mlock_pvec.vec);
0275     SetPageMlocked(page);
0276     mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages);
0277     __count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
0278 
0279     get_page(page);
0280     if (!pagevec_add(pvec, mlock_new(page)) ||
0281         PageHead(page) || lru_cache_disabled())
0282         mlock_pagevec(pvec);
0283     local_unlock(&mlock_pvec.lock);
0284 }
0285 
0286 /**
0287  * munlock_page - munlock a page
0288  * @page: page to be munlocked, either a normal page or a THP head.
0289  */
0290 void munlock_page(struct page *page)
0291 {
0292     struct pagevec *pvec;
0293 
0294     local_lock(&mlock_pvec.lock);
0295     pvec = this_cpu_ptr(&mlock_pvec.vec);
0296     /*
0297      * TestClearPageMlocked(page) must be left to __munlock_page(),
0298      * which will check whether the page is multiply mlocked.
0299      */
0300 
0301     get_page(page);
0302     if (!pagevec_add(pvec, page) ||
0303         PageHead(page) || lru_cache_disabled())
0304         mlock_pagevec(pvec);
0305     local_unlock(&mlock_pvec.lock);
0306 }
0307 
0308 static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
0309                unsigned long end, struct mm_walk *walk)
0310 
0311 {
0312     struct vm_area_struct *vma = walk->vma;
0313     spinlock_t *ptl;
0314     pte_t *start_pte, *pte;
0315     struct page *page;
0316 
0317     ptl = pmd_trans_huge_lock(pmd, vma);
0318     if (ptl) {
0319         if (!pmd_present(*pmd))
0320             goto out;
0321         if (is_huge_zero_pmd(*pmd))
0322             goto out;
0323         page = pmd_page(*pmd);
0324         if (vma->vm_flags & VM_LOCKED)
0325             mlock_folio(page_folio(page));
0326         else
0327             munlock_page(page);
0328         goto out;
0329     }
0330 
0331     start_pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
0332     for (pte = start_pte; addr != end; pte++, addr += PAGE_SIZE) {
0333         if (!pte_present(*pte))
0334             continue;
0335         page = vm_normal_page(vma, addr, *pte);
0336         if (!page || is_zone_device_page(page))
0337             continue;
0338         if (PageTransCompound(page))
0339             continue;
0340         if (vma->vm_flags & VM_LOCKED)
0341             mlock_folio(page_folio(page));
0342         else
0343             munlock_page(page);
0344     }
0345     pte_unmap(start_pte);
0346 out:
0347     spin_unlock(ptl);
0348     cond_resched();
0349     return 0;
0350 }
0351 
0352 /*
0353  * mlock_vma_pages_range() - mlock any pages already in the range,
0354  *                           or munlock all pages in the range.
0355  * @vma - vma containing range to be mlock()ed or munlock()ed
0356  * @start - start address in @vma of the range
0357  * @end - end of range in @vma
0358  * @newflags - the new set of flags for @vma.
0359  *
0360  * Called for mlock(), mlock2() and mlockall(), to set @vma VM_LOCKED;
0361  * called for munlock() and munlockall(), to clear VM_LOCKED from @vma.
0362  */
0363 static void mlock_vma_pages_range(struct vm_area_struct *vma,
0364     unsigned long start, unsigned long end, vm_flags_t newflags)
0365 {
0366     static const struct mm_walk_ops mlock_walk_ops = {
0367         .pmd_entry = mlock_pte_range,
0368     };
0369 
0370     /*
0371      * There is a slight chance that concurrent page migration,
0372      * or page reclaim finding a page of this now-VM_LOCKED vma,
0373      * will call mlock_vma_page() and raise page's mlock_count:
0374      * double counting, leaving the page unevictable indefinitely.
0375      * Communicate this danger to mlock_vma_page() with VM_IO,
0376      * which is a VM_SPECIAL flag not allowed on VM_LOCKED vmas.
0377      * mmap_lock is held in write mode here, so this weird
0378      * combination should not be visible to other mmap_lock users;
0379      * but WRITE_ONCE so rmap walkers must see VM_IO if VM_LOCKED.
0380      */
0381     if (newflags & VM_LOCKED)
0382         newflags |= VM_IO;
0383     WRITE_ONCE(vma->vm_flags, newflags);
0384 
0385     lru_add_drain();
0386     walk_page_range(vma->vm_mm, start, end, &mlock_walk_ops, NULL);
0387     lru_add_drain();
0388 
0389     if (newflags & VM_IO) {
0390         newflags &= ~VM_IO;
0391         WRITE_ONCE(vma->vm_flags, newflags);
0392     }
0393 }
0394 
0395 /*
0396  * mlock_fixup  - handle mlock[all]/munlock[all] requests.
0397  *
0398  * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
0399  * munlock is a no-op.  However, for some special vmas, we go ahead and
0400  * populate the ptes.
0401  *
0402  * For vmas that pass the filters, merge/split as appropriate.
0403  */
0404 static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
0405     unsigned long start, unsigned long end, vm_flags_t newflags)
0406 {
0407     struct mm_struct *mm = vma->vm_mm;
0408     pgoff_t pgoff;
0409     int nr_pages;
0410     int ret = 0;
0411     vm_flags_t oldflags = vma->vm_flags;
0412 
0413     if (newflags == oldflags || (oldflags & VM_SPECIAL) ||
0414         is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm) ||
0415         vma_is_dax(vma) || vma_is_secretmem(vma))
0416         /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
0417         goto out;
0418 
0419     pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
0420     *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
0421               vma->vm_file, pgoff, vma_policy(vma),
0422               vma->vm_userfaultfd_ctx, anon_vma_name(vma));
0423     if (*prev) {
0424         vma = *prev;
0425         goto success;
0426     }
0427 
0428     if (start != vma->vm_start) {
0429         ret = split_vma(mm, vma, start, 1);
0430         if (ret)
0431             goto out;
0432     }
0433 
0434     if (end != vma->vm_end) {
0435         ret = split_vma(mm, vma, end, 0);
0436         if (ret)
0437             goto out;
0438     }
0439 
0440 success:
0441     /*
0442      * Keep track of amount of locked VM.
0443      */
0444     nr_pages = (end - start) >> PAGE_SHIFT;
0445     if (!(newflags & VM_LOCKED))
0446         nr_pages = -nr_pages;
0447     else if (oldflags & VM_LOCKED)
0448         nr_pages = 0;
0449     mm->locked_vm += nr_pages;
0450 
0451     /*
0452      * vm_flags is protected by the mmap_lock held in write mode.
0453      * It's okay if try_to_unmap_one unmaps a page just after we
0454      * set VM_LOCKED, populate_vma_page_range will bring it back.
0455      */
0456 
0457     if ((newflags & VM_LOCKED) && (oldflags & VM_LOCKED)) {
0458         /* No work to do, and mlocking twice would be wrong */
0459         vma->vm_flags = newflags;
0460     } else {
0461         mlock_vma_pages_range(vma, start, end, newflags);
0462     }
0463 out:
0464     *prev = vma;
0465     return ret;
0466 }
0467 
0468 static int apply_vma_lock_flags(unsigned long start, size_t len,
0469                 vm_flags_t flags)
0470 {
0471     unsigned long nstart, end, tmp;
0472     struct vm_area_struct *vma, *prev;
0473     int error;
0474 
0475     VM_BUG_ON(offset_in_page(start));
0476     VM_BUG_ON(len != PAGE_ALIGN(len));
0477     end = start + len;
0478     if (end < start)
0479         return -EINVAL;
0480     if (end == start)
0481         return 0;
0482     vma = find_vma(current->mm, start);
0483     if (!vma || vma->vm_start > start)
0484         return -ENOMEM;
0485 
0486     prev = vma->vm_prev;
0487     if (start > vma->vm_start)
0488         prev = vma;
0489 
0490     for (nstart = start ; ; ) {
0491         vm_flags_t newflags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
0492 
0493         newflags |= flags;
0494 
0495         /* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
0496         tmp = vma->vm_end;
0497         if (tmp > end)
0498             tmp = end;
0499         error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
0500         if (error)
0501             break;
0502         nstart = tmp;
0503         if (nstart < prev->vm_end)
0504             nstart = prev->vm_end;
0505         if (nstart >= end)
0506             break;
0507 
0508         vma = prev->vm_next;
0509         if (!vma || vma->vm_start != nstart) {
0510             error = -ENOMEM;
0511             break;
0512         }
0513     }
0514     return error;
0515 }
0516 
0517 /*
0518  * Go through vma areas and sum size of mlocked
0519  * vma pages, as return value.
0520  * Note deferred memory locking case(mlock2(,,MLOCK_ONFAULT)
0521  * is also counted.
0522  * Return value: previously mlocked page counts
0523  */
0524 static unsigned long count_mm_mlocked_page_nr(struct mm_struct *mm,
0525         unsigned long start, size_t len)
0526 {
0527     struct vm_area_struct *vma;
0528     unsigned long count = 0;
0529 
0530     if (mm == NULL)
0531         mm = current->mm;
0532 
0533     vma = find_vma(mm, start);
0534     if (vma == NULL)
0535         return 0;
0536 
0537     for (; vma ; vma = vma->vm_next) {
0538         if (start >= vma->vm_end)
0539             continue;
0540         if (start + len <=  vma->vm_start)
0541             break;
0542         if (vma->vm_flags & VM_LOCKED) {
0543             if (start > vma->vm_start)
0544                 count -= (start - vma->vm_start);
0545             if (start + len < vma->vm_end) {
0546                 count += start + len - vma->vm_start;
0547                 break;
0548             }
0549             count += vma->vm_end - vma->vm_start;
0550         }
0551     }
0552 
0553     return count >> PAGE_SHIFT;
0554 }
0555 
0556 /*
0557  * convert get_user_pages() return value to posix mlock() error
0558  */
0559 static int __mlock_posix_error_return(long retval)
0560 {
0561     if (retval == -EFAULT)
0562         retval = -ENOMEM;
0563     else if (retval == -ENOMEM)
0564         retval = -EAGAIN;
0565     return retval;
0566 }
0567 
0568 static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t flags)
0569 {
0570     unsigned long locked;
0571     unsigned long lock_limit;
0572     int error = -ENOMEM;
0573 
0574     start = untagged_addr(start);
0575 
0576     if (!can_do_mlock())
0577         return -EPERM;
0578 
0579     len = PAGE_ALIGN(len + (offset_in_page(start)));
0580     start &= PAGE_MASK;
0581 
0582     lock_limit = rlimit(RLIMIT_MEMLOCK);
0583     lock_limit >>= PAGE_SHIFT;
0584     locked = len >> PAGE_SHIFT;
0585 
0586     if (mmap_write_lock_killable(current->mm))
0587         return -EINTR;
0588 
0589     locked += current->mm->locked_vm;
0590     if ((locked > lock_limit) && (!capable(CAP_IPC_LOCK))) {
0591         /*
0592          * It is possible that the regions requested intersect with
0593          * previously mlocked areas, that part area in "mm->locked_vm"
0594          * should not be counted to new mlock increment count. So check
0595          * and adjust locked count if necessary.
0596          */
0597         locked -= count_mm_mlocked_page_nr(current->mm,
0598                 start, len);
0599     }
0600 
0601     /* check against resource limits */
0602     if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
0603         error = apply_vma_lock_flags(start, len, flags);
0604 
0605     mmap_write_unlock(current->mm);
0606     if (error)
0607         return error;
0608 
0609     error = __mm_populate(start, len, 0);
0610     if (error)
0611         return __mlock_posix_error_return(error);
0612     return 0;
0613 }
0614 
0615 SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
0616 {
0617     return do_mlock(start, len, VM_LOCKED);
0618 }
0619 
0620 SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags)
0621 {
0622     vm_flags_t vm_flags = VM_LOCKED;
0623 
0624     if (flags & ~MLOCK_ONFAULT)
0625         return -EINVAL;
0626 
0627     if (flags & MLOCK_ONFAULT)
0628         vm_flags |= VM_LOCKONFAULT;
0629 
0630     return do_mlock(start, len, vm_flags);
0631 }
0632 
0633 SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
0634 {
0635     int ret;
0636 
0637     start = untagged_addr(start);
0638 
0639     len = PAGE_ALIGN(len + (offset_in_page(start)));
0640     start &= PAGE_MASK;
0641 
0642     if (mmap_write_lock_killable(current->mm))
0643         return -EINTR;
0644     ret = apply_vma_lock_flags(start, len, 0);
0645     mmap_write_unlock(current->mm);
0646 
0647     return ret;
0648 }
0649 
0650 /*
0651  * Take the MCL_* flags passed into mlockall (or 0 if called from munlockall)
0652  * and translate into the appropriate modifications to mm->def_flags and/or the
0653  * flags for all current VMAs.
0654  *
0655  * There are a couple of subtleties with this.  If mlockall() is called multiple
0656  * times with different flags, the values do not necessarily stack.  If mlockall
0657  * is called once including the MCL_FUTURE flag and then a second time without
0658  * it, VM_LOCKED and VM_LOCKONFAULT will be cleared from mm->def_flags.
0659  */
0660 static int apply_mlockall_flags(int flags)
0661 {
0662     struct vm_area_struct *vma, *prev = NULL;
0663     vm_flags_t to_add = 0;
0664 
0665     current->mm->def_flags &= VM_LOCKED_CLEAR_MASK;
0666     if (flags & MCL_FUTURE) {
0667         current->mm->def_flags |= VM_LOCKED;
0668 
0669         if (flags & MCL_ONFAULT)
0670             current->mm->def_flags |= VM_LOCKONFAULT;
0671 
0672         if (!(flags & MCL_CURRENT))
0673             goto out;
0674     }
0675 
0676     if (flags & MCL_CURRENT) {
0677         to_add |= VM_LOCKED;
0678         if (flags & MCL_ONFAULT)
0679             to_add |= VM_LOCKONFAULT;
0680     }
0681 
0682     for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
0683         vm_flags_t newflags;
0684 
0685         newflags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
0686         newflags |= to_add;
0687 
0688         /* Ignore errors */
0689         mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
0690         cond_resched();
0691     }
0692 out:
0693     return 0;
0694 }
0695 
0696 SYSCALL_DEFINE1(mlockall, int, flags)
0697 {
0698     unsigned long lock_limit;
0699     int ret;
0700 
0701     if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT)) ||
0702         flags == MCL_ONFAULT)
0703         return -EINVAL;
0704 
0705     if (!can_do_mlock())
0706         return -EPERM;
0707 
0708     lock_limit = rlimit(RLIMIT_MEMLOCK);
0709     lock_limit >>= PAGE_SHIFT;
0710 
0711     if (mmap_write_lock_killable(current->mm))
0712         return -EINTR;
0713 
0714     ret = -ENOMEM;
0715     if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
0716         capable(CAP_IPC_LOCK))
0717         ret = apply_mlockall_flags(flags);
0718     mmap_write_unlock(current->mm);
0719     if (!ret && (flags & MCL_CURRENT))
0720         mm_populate(0, TASK_SIZE);
0721 
0722     return ret;
0723 }
0724 
0725 SYSCALL_DEFINE0(munlockall)
0726 {
0727     int ret;
0728 
0729     if (mmap_write_lock_killable(current->mm))
0730         return -EINTR;
0731     ret = apply_mlockall_flags(0);
0732     mmap_write_unlock(current->mm);
0733     return ret;
0734 }
0735 
0736 /*
0737  * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
0738  * shm segments) get accounted against the user_struct instead.
0739  */
0740 static DEFINE_SPINLOCK(shmlock_user_lock);
0741 
0742 int user_shm_lock(size_t size, struct ucounts *ucounts)
0743 {
0744     unsigned long lock_limit, locked;
0745     long memlock;
0746     int allowed = 0;
0747 
0748     locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
0749     lock_limit = rlimit(RLIMIT_MEMLOCK);
0750     if (lock_limit != RLIM_INFINITY)
0751         lock_limit >>= PAGE_SHIFT;
0752     spin_lock(&shmlock_user_lock);
0753     memlock = inc_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
0754 
0755     if ((memlock == LONG_MAX || memlock > lock_limit) && !capable(CAP_IPC_LOCK)) {
0756         dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
0757         goto out;
0758     }
0759     if (!get_ucounts(ucounts)) {
0760         dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
0761         allowed = 0;
0762         goto out;
0763     }
0764     allowed = 1;
0765 out:
0766     spin_unlock(&shmlock_user_lock);
0767     return allowed;
0768 }
0769 
0770 void user_shm_unlock(size_t size, struct ucounts *ucounts)
0771 {
0772     spin_lock(&shmlock_user_lock);
0773     dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
0774     spin_unlock(&shmlock_user_lock);
0775     put_ucounts(ucounts);
0776 }