1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/mm/mlock.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * (C) Copyright 1995 Linus Torvalds
61da177e4SLinus Torvalds * (C) Copyright 2002 Christoph Hellwig
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
9c59ede7bSRandy.Dunlap #include <linux/capability.h>
101da177e4SLinus Torvalds #include <linux/mman.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
128703e8a4SIngo Molnar #include <linux/sched/user.h>
13b291f000SNick Piggin #include <linux/swap.h>
14b291f000SNick Piggin #include <linux/swapops.h>
15b291f000SNick Piggin #include <linux/pagemap.h>
167225522bSVlastimil Babka #include <linux/pagevec.h>
1734b67923SHugh Dickins #include <linux/pagewalk.h>
181da177e4SLinus Torvalds #include <linux/mempolicy.h>
191da177e4SLinus Torvalds #include <linux/syscalls.h>
20e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
21b95f1b31SPaul Gortmaker #include <linux/export.h>
22b291f000SNick Piggin #include <linux/rmap.h>
23b291f000SNick Piggin #include <linux/mmzone.h>
24b291f000SNick Piggin #include <linux/hugetlb.h>
257225522bSVlastimil Babka #include <linux/memcontrol.h>
267225522bSVlastimil Babka #include <linux/mm_inline.h>
271507f512SMike Rapoport #include <linux/secretmem.h>
28b291f000SNick Piggin
29b291f000SNick Piggin #include "internal.h"
301da177e4SLinus Torvalds
3190d07210SLorenzo Stoakes struct mlock_fbatch {
32adb11e78SSebastian Andrzej Siewior local_lock_t lock;
3390d07210SLorenzo Stoakes struct folio_batch fbatch;
34adb11e78SSebastian Andrzej Siewior };
35adb11e78SSebastian Andrzej Siewior
3690d07210SLorenzo Stoakes static DEFINE_PER_CPU(struct mlock_fbatch, mlock_fbatch) = {
37adb11e78SSebastian Andrzej Siewior .lock = INIT_LOCAL_LOCK(lock),
38adb11e78SSebastian Andrzej Siewior };
392fbb0c10SHugh Dickins
can_do_mlock(void)407f43add4SWang Xiaoqiang bool can_do_mlock(void)
41e8edc6e0SAlexey Dobriyan {
4259e99e5bSJiri Slaby if (rlimit(RLIMIT_MEMLOCK) != 0)
437f43add4SWang Xiaoqiang return true;
44a5a6579dSJeff Vander Stoep if (capable(CAP_IPC_LOCK))
457f43add4SWang Xiaoqiang return true;
467f43add4SWang Xiaoqiang return false;
47e8edc6e0SAlexey Dobriyan }
48e8edc6e0SAlexey Dobriyan EXPORT_SYMBOL(can_do_mlock);
491da177e4SLinus Torvalds
50b291f000SNick Piggin /*
5190d07210SLorenzo Stoakes * Mlocked folios are marked with the PG_mlocked flag for efficient testing
52b291f000SNick Piggin * in vmscan and, possibly, the fault path; and to support semi-accurate
53b291f000SNick Piggin * statistics.
54b291f000SNick Piggin *
5590d07210SLorenzo Stoakes * An mlocked folio [folio_test_mlocked(folio)] is unevictable. As such, it
5690d07210SLorenzo Stoakes * will be ostensibly placed on the LRU "unevictable" list (actually no such
5790d07210SLorenzo Stoakes * list exists), rather than the [in]active lists. PG_unevictable is set to
5890d07210SLorenzo Stoakes * indicate the unevictable state.
59b291f000SNick Piggin */
60b291f000SNick Piggin
__mlock_folio(struct folio * folio,struct lruvec * lruvec)6190d07210SLorenzo Stoakes static struct lruvec *__mlock_folio(struct folio *folio, struct lruvec *lruvec)
62b291f000SNick Piggin {
6307ca7606SHugh Dickins /* There is nothing more we can do while it's off LRU */
6490d07210SLorenzo Stoakes if (!folio_test_clear_lru(folio))
652fbb0c10SHugh Dickins return lruvec;
660964730bSHugh Dickins
6790d07210SLorenzo Stoakes lruvec = folio_lruvec_relock_irq(folio, lruvec);
68b291f000SNick Piggin
6990d07210SLorenzo Stoakes if (unlikely(folio_evictable(folio))) {
709c4e6b1aSShakeel Butt /*
7190d07210SLorenzo Stoakes * This is a little surprising, but quite possible: PG_mlocked
7290d07210SLorenzo Stoakes * must have got cleared already by another CPU. Could this
7390d07210SLorenzo Stoakes * folio be unevictable? I'm not sure, but move it now if so.
749c4e6b1aSShakeel Butt */
7590d07210SLorenzo Stoakes if (folio_test_unevictable(folio)) {
7690d07210SLorenzo Stoakes lruvec_del_folio(lruvec, folio);
7790d07210SLorenzo Stoakes folio_clear_unevictable(folio);
7890d07210SLorenzo Stoakes lruvec_add_folio(lruvec, folio);
7990d07210SLorenzo Stoakes
802fbb0c10SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED,
8190d07210SLorenzo Stoakes folio_nr_pages(folio));
82b291f000SNick Piggin }
832fbb0c10SHugh Dickins goto out;
84b291f000SNick Piggin }
85b291f000SNick Piggin
8690d07210SLorenzo Stoakes if (folio_test_unevictable(folio)) {
8790d07210SLorenzo Stoakes if (folio_test_mlocked(folio))
8890d07210SLorenzo Stoakes folio->mlock_count++;
8907ca7606SHugh Dickins goto out;
9007ca7606SHugh Dickins }
9107ca7606SHugh Dickins
9290d07210SLorenzo Stoakes lruvec_del_folio(lruvec, folio);
9390d07210SLorenzo Stoakes folio_clear_active(folio);
9490d07210SLorenzo Stoakes folio_set_unevictable(folio);
9590d07210SLorenzo Stoakes folio->mlock_count = !!folio_test_mlocked(folio);
9690d07210SLorenzo Stoakes lruvec_add_folio(lruvec, folio);
9790d07210SLorenzo Stoakes __count_vm_events(UNEVICTABLE_PGCULLED, folio_nr_pages(folio));
9807ca7606SHugh Dickins out:
9990d07210SLorenzo Stoakes folio_set_lru(folio);
1002fbb0c10SHugh Dickins return lruvec;
1015344b7e6SNick Piggin }
102b291f000SNick Piggin
__mlock_new_folio(struct folio * folio,struct lruvec * lruvec)10390d07210SLorenzo Stoakes static struct lruvec *__mlock_new_folio(struct folio *folio, struct lruvec *lruvec)
104b291f000SNick Piggin {
10590d07210SLorenzo Stoakes VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
106b291f000SNick Piggin
10790d07210SLorenzo Stoakes lruvec = folio_lruvec_relock_irq(folio, lruvec);
108b291f000SNick Piggin
1092fbb0c10SHugh Dickins /* As above, this is a little surprising, but possible */
11090d07210SLorenzo Stoakes if (unlikely(folio_evictable(folio)))
1112fbb0c10SHugh Dickins goto out;
112b291f000SNick Piggin
11390d07210SLorenzo Stoakes folio_set_unevictable(folio);
11490d07210SLorenzo Stoakes folio->mlock_count = !!folio_test_mlocked(folio);
11590d07210SLorenzo Stoakes __count_vm_events(UNEVICTABLE_PGCULLED, folio_nr_pages(folio));
1162fbb0c10SHugh Dickins out:
11790d07210SLorenzo Stoakes lruvec_add_folio(lruvec, folio);
11890d07210SLorenzo Stoakes folio_set_lru(folio);
1192fbb0c10SHugh Dickins return lruvec;
120b291f000SNick Piggin }
121b291f000SNick Piggin
__munlock_folio(struct folio * folio,struct lruvec * lruvec)12290d07210SLorenzo Stoakes static struct lruvec *__munlock_folio(struct folio *folio, struct lruvec *lruvec)
1237225522bSVlastimil Babka {
12490d07210SLorenzo Stoakes int nr_pages = folio_nr_pages(folio);
1252fbb0c10SHugh Dickins bool isolated = false;
1260964730bSHugh Dickins
12790d07210SLorenzo Stoakes if (!folio_test_clear_lru(folio))
1282fbb0c10SHugh Dickins goto munlock;
12907ca7606SHugh Dickins
1302fbb0c10SHugh Dickins isolated = true;
13190d07210SLorenzo Stoakes lruvec = folio_lruvec_relock_irq(folio, lruvec);
1322fbb0c10SHugh Dickins
13390d07210SLorenzo Stoakes if (folio_test_unevictable(folio)) {
13407ca7606SHugh Dickins /* Then mlock_count is maintained, but might undercount */
13590d07210SLorenzo Stoakes if (folio->mlock_count)
13690d07210SLorenzo Stoakes folio->mlock_count--;
13790d07210SLorenzo Stoakes if (folio->mlock_count)
13807ca7606SHugh Dickins goto out;
1397225522bSVlastimil Babka }
14007ca7606SHugh Dickins /* else assume that was the last mlock: reclaim will fix it if not */
14107ca7606SHugh Dickins
1422fbb0c10SHugh Dickins munlock:
14390d07210SLorenzo Stoakes if (folio_test_clear_mlocked(folio)) {
14490d07210SLorenzo Stoakes __zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages);
14590d07210SLorenzo Stoakes if (isolated || !folio_test_unevictable(folio))
1466927c1ddSLee Schermerhorn __count_vm_events(UNEVICTABLE_PGMUNLOCKED, nr_pages);
14707ca7606SHugh Dickins else
14807ca7606SHugh Dickins __count_vm_events(UNEVICTABLE_PGSTRANDED, nr_pages);
1497225522bSVlastimil Babka }
15007ca7606SHugh Dickins
15190d07210SLorenzo Stoakes /* folio_evictable() has to be checked *after* clearing Mlocked */
15290d07210SLorenzo Stoakes if (isolated && folio_test_unevictable(folio) && folio_evictable(folio)) {
15390d07210SLorenzo Stoakes lruvec_del_folio(lruvec, folio);
15490d07210SLorenzo Stoakes folio_clear_unevictable(folio);
15590d07210SLorenzo Stoakes lruvec_add_folio(lruvec, folio);
15607ca7606SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, nr_pages);
15707ca7606SHugh Dickins }
15807ca7606SHugh Dickins out:
1592fbb0c10SHugh Dickins if (isolated)
16090d07210SLorenzo Stoakes folio_set_lru(folio);
1612fbb0c10SHugh Dickins return lruvec;
1622fbb0c10SHugh Dickins }
1632fbb0c10SHugh Dickins
1642fbb0c10SHugh Dickins /*
16590d07210SLorenzo Stoakes * Flags held in the low bits of a struct folio pointer on the mlock_fbatch.
1662fbb0c10SHugh Dickins */
16790d07210SLorenzo Stoakes #define LRU_FOLIO 0x1
16890d07210SLorenzo Stoakes #define NEW_FOLIO 0x2
mlock_lru(struct folio * folio)16990d07210SLorenzo Stoakes static inline struct folio *mlock_lru(struct folio *folio)
1702fbb0c10SHugh Dickins {
17190d07210SLorenzo Stoakes return (struct folio *)((unsigned long)folio + LRU_FOLIO);
1722fbb0c10SHugh Dickins }
1732fbb0c10SHugh Dickins
mlock_new(struct folio * folio)17490d07210SLorenzo Stoakes static inline struct folio *mlock_new(struct folio *folio)
1752fbb0c10SHugh Dickins {
17690d07210SLorenzo Stoakes return (struct folio *)((unsigned long)folio + NEW_FOLIO);
1772fbb0c10SHugh Dickins }
1782fbb0c10SHugh Dickins
1792fbb0c10SHugh Dickins /*
18090d07210SLorenzo Stoakes * mlock_folio_batch() is derived from folio_batch_move_lru(): perhaps that can
18190d07210SLorenzo Stoakes * make use of such folio pointer flags in future, but for now just keep it for
18290d07210SLorenzo Stoakes * mlock. We could use three separate folio batches instead, but one feels
18390d07210SLorenzo Stoakes * better (munlocking a full folio batch does not need to drain mlocking folio
18490d07210SLorenzo Stoakes * batches first).
1852fbb0c10SHugh Dickins */
mlock_folio_batch(struct folio_batch * fbatch)18690d07210SLorenzo Stoakes static void mlock_folio_batch(struct folio_batch *fbatch)
1872fbb0c10SHugh Dickins {
1882fbb0c10SHugh Dickins struct lruvec *lruvec = NULL;
1892fbb0c10SHugh Dickins unsigned long mlock;
19090d07210SLorenzo Stoakes struct folio *folio;
1912fbb0c10SHugh Dickins int i;
1922fbb0c10SHugh Dickins
19390d07210SLorenzo Stoakes for (i = 0; i < folio_batch_count(fbatch); i++) {
19490d07210SLorenzo Stoakes folio = fbatch->folios[i];
19590d07210SLorenzo Stoakes mlock = (unsigned long)folio & (LRU_FOLIO | NEW_FOLIO);
19690d07210SLorenzo Stoakes folio = (struct folio *)((unsigned long)folio - mlock);
19790d07210SLorenzo Stoakes fbatch->folios[i] = folio;
1982fbb0c10SHugh Dickins
19990d07210SLorenzo Stoakes if (mlock & LRU_FOLIO)
20090d07210SLorenzo Stoakes lruvec = __mlock_folio(folio, lruvec);
20190d07210SLorenzo Stoakes else if (mlock & NEW_FOLIO)
20290d07210SLorenzo Stoakes lruvec = __mlock_new_folio(folio, lruvec);
2032fbb0c10SHugh Dickins else
20490d07210SLorenzo Stoakes lruvec = __munlock_folio(folio, lruvec);
2052fbb0c10SHugh Dickins }
2062fbb0c10SHugh Dickins
2072fbb0c10SHugh Dickins if (lruvec)
20807ca7606SHugh Dickins unlock_page_lruvec_irq(lruvec);
2092bd7f621SQi Zheng folios_put(fbatch->folios, folio_batch_count(fbatch));
21090d07210SLorenzo Stoakes folio_batch_reinit(fbatch);
2112fbb0c10SHugh Dickins }
2122fbb0c10SHugh Dickins
mlock_drain_local(void)21396f97c43SLorenzo Stoakes void mlock_drain_local(void)
2142fbb0c10SHugh Dickins {
21590d07210SLorenzo Stoakes struct folio_batch *fbatch;
2162fbb0c10SHugh Dickins
21790d07210SLorenzo Stoakes local_lock(&mlock_fbatch.lock);
21890d07210SLorenzo Stoakes fbatch = this_cpu_ptr(&mlock_fbatch.fbatch);
21990d07210SLorenzo Stoakes if (folio_batch_count(fbatch))
22090d07210SLorenzo Stoakes mlock_folio_batch(fbatch);
22190d07210SLorenzo Stoakes local_unlock(&mlock_fbatch.lock);
222adb11e78SSebastian Andrzej Siewior }
223adb11e78SSebastian Andrzej Siewior
mlock_drain_remote(int cpu)22496f97c43SLorenzo Stoakes void mlock_drain_remote(int cpu)
225adb11e78SSebastian Andrzej Siewior {
22690d07210SLorenzo Stoakes struct folio_batch *fbatch;
227adb11e78SSebastian Andrzej Siewior
228adb11e78SSebastian Andrzej Siewior WARN_ON_ONCE(cpu_online(cpu));
22990d07210SLorenzo Stoakes fbatch = &per_cpu(mlock_fbatch.fbatch, cpu);
23090d07210SLorenzo Stoakes if (folio_batch_count(fbatch))
23190d07210SLorenzo Stoakes mlock_folio_batch(fbatch);
2322fbb0c10SHugh Dickins }
2332fbb0c10SHugh Dickins
need_mlock_drain(int cpu)23496f97c43SLorenzo Stoakes bool need_mlock_drain(int cpu)
2352fbb0c10SHugh Dickins {
23690d07210SLorenzo Stoakes return folio_batch_count(&per_cpu(mlock_fbatch.fbatch, cpu));
237b291f000SNick Piggin }
238ff6a6da6SMichel Lespinasse
239b291f000SNick Piggin /**
240dcc5d337SMatthew Wilcox (Oracle) * mlock_folio - mlock a folio already on (or temporarily off) LRU
241dcc5d337SMatthew Wilcox (Oracle) * @folio: folio to be mlocked.
2423d470fc3SHugh Dickins */
mlock_folio(struct folio * folio)243dcc5d337SMatthew Wilcox (Oracle) void mlock_folio(struct folio *folio)
2443d470fc3SHugh Dickins {
24590d07210SLorenzo Stoakes struct folio_batch *fbatch;
246adb11e78SSebastian Andrzej Siewior
24790d07210SLorenzo Stoakes local_lock(&mlock_fbatch.lock);
24890d07210SLorenzo Stoakes fbatch = this_cpu_ptr(&mlock_fbatch.fbatch);
2493d470fc3SHugh Dickins
250dcc5d337SMatthew Wilcox (Oracle) if (!folio_test_set_mlocked(folio)) {
251dcc5d337SMatthew Wilcox (Oracle) int nr_pages = folio_nr_pages(folio);
2525344b7e6SNick Piggin
253dcc5d337SMatthew Wilcox (Oracle) zone_stat_mod_folio(folio, NR_MLOCK, nr_pages);
2542fbb0c10SHugh Dickins __count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
2552fbb0c10SHugh Dickins }
2562fbb0c10SHugh Dickins
257dcc5d337SMatthew Wilcox (Oracle) folio_get(folio);
25890d07210SLorenzo Stoakes if (!folio_batch_add(fbatch, mlock_lru(folio)) ||
259dcc5d337SMatthew Wilcox (Oracle) folio_test_large(folio) || lru_cache_disabled())
26090d07210SLorenzo Stoakes mlock_folio_batch(fbatch);
26190d07210SLorenzo Stoakes local_unlock(&mlock_fbatch.lock);
2622fbb0c10SHugh Dickins }
2632fbb0c10SHugh Dickins
2642fbb0c10SHugh Dickins /**
26596f97c43SLorenzo Stoakes * mlock_new_folio - mlock a newly allocated folio not yet on LRU
26696f97c43SLorenzo Stoakes * @folio: folio to be mlocked, either normal or a THP head.
2672fbb0c10SHugh Dickins */
mlock_new_folio(struct folio * folio)26896f97c43SLorenzo Stoakes void mlock_new_folio(struct folio *folio)
2692fbb0c10SHugh Dickins {
27090d07210SLorenzo Stoakes struct folio_batch *fbatch;
27190d07210SLorenzo Stoakes int nr_pages = folio_nr_pages(folio);
2722fbb0c10SHugh Dickins
27390d07210SLorenzo Stoakes local_lock(&mlock_fbatch.lock);
27490d07210SLorenzo Stoakes fbatch = this_cpu_ptr(&mlock_fbatch.fbatch);
27590d07210SLorenzo Stoakes folio_set_mlocked(folio);
27690d07210SLorenzo Stoakes
27790d07210SLorenzo Stoakes zone_stat_mod_folio(folio, NR_MLOCK, nr_pages);
2782fbb0c10SHugh Dickins __count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
2792fbb0c10SHugh Dickins
28090d07210SLorenzo Stoakes folio_get(folio);
28190d07210SLorenzo Stoakes if (!folio_batch_add(fbatch, mlock_new(folio)) ||
28290d07210SLorenzo Stoakes folio_test_large(folio) || lru_cache_disabled())
28390d07210SLorenzo Stoakes mlock_folio_batch(fbatch);
28490d07210SLorenzo Stoakes local_unlock(&mlock_fbatch.lock);
2852fbb0c10SHugh Dickins }
2862fbb0c10SHugh Dickins
2872fbb0c10SHugh Dickins /**
28896f97c43SLorenzo Stoakes * munlock_folio - munlock a folio
28996f97c43SLorenzo Stoakes * @folio: folio to be munlocked, either normal or a THP head.
2902fbb0c10SHugh Dickins */
munlock_folio(struct folio * folio)29196f97c43SLorenzo Stoakes void munlock_folio(struct folio *folio)
2922fbb0c10SHugh Dickins {
29390d07210SLorenzo Stoakes struct folio_batch *fbatch;
2942fbb0c10SHugh Dickins
29590d07210SLorenzo Stoakes local_lock(&mlock_fbatch.lock);
29690d07210SLorenzo Stoakes fbatch = this_cpu_ptr(&mlock_fbatch.fbatch);
2972fbb0c10SHugh Dickins /*
29890d07210SLorenzo Stoakes * folio_test_clear_mlocked(folio) must be left to __munlock_folio(),
29990d07210SLorenzo Stoakes * which will check whether the folio is multiply mlocked.
3002fbb0c10SHugh Dickins */
30190d07210SLorenzo Stoakes folio_get(folio);
30290d07210SLorenzo Stoakes if (!folio_batch_add(fbatch, folio) ||
30390d07210SLorenzo Stoakes folio_test_large(folio) || lru_cache_disabled())
30490d07210SLorenzo Stoakes mlock_folio_batch(fbatch);
30590d07210SLorenzo Stoakes local_unlock(&mlock_fbatch.lock);
30656afe477SVlastimil Babka }
30756afe477SVlastimil Babka
mlock_pte_range(pmd_t * pmd,unsigned long addr,unsigned long end,struct mm_walk * walk)30834b67923SHugh Dickins static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
30934b67923SHugh Dickins unsigned long end, struct mm_walk *walk)
310408e82b7SHugh Dickins
31134b67923SHugh Dickins {
31234b67923SHugh Dickins struct vm_area_struct *vma = walk->vma;
31334b67923SHugh Dickins spinlock_t *ptl;
31434b67923SHugh Dickins pte_t *start_pte, *pte;
315c33c7948SRyan Roberts pte_t ptent;
31696f97c43SLorenzo Stoakes struct folio *folio;
31734b67923SHugh Dickins
31834b67923SHugh Dickins ptl = pmd_trans_huge_lock(pmd, vma);
31934b67923SHugh Dickins if (ptl) {
32034b67923SHugh Dickins if (!pmd_present(*pmd))
32134b67923SHugh Dickins goto out;
32234b67923SHugh Dickins if (is_huge_zero_pmd(*pmd))
32334b67923SHugh Dickins goto out;
32496f97c43SLorenzo Stoakes folio = page_folio(pmd_page(*pmd));
32534b67923SHugh Dickins if (vma->vm_flags & VM_LOCKED)
32696f97c43SLorenzo Stoakes mlock_folio(folio);
32734b67923SHugh Dickins else
32896f97c43SLorenzo Stoakes munlock_folio(folio);
32934b67923SHugh Dickins goto out;
33034b67923SHugh Dickins }
33134b67923SHugh Dickins
33234b67923SHugh Dickins start_pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
3337780d040SHugh Dickins if (!start_pte) {
3347780d040SHugh Dickins walk->action = ACTION_AGAIN;
3357780d040SHugh Dickins return 0;
3367780d040SHugh Dickins }
33734b67923SHugh Dickins for (pte = start_pte; addr != end; pte++, addr += PAGE_SIZE) {
338c33c7948SRyan Roberts ptent = ptep_get(pte);
339c33c7948SRyan Roberts if (!pte_present(ptent))
34034b67923SHugh Dickins continue;
341c33c7948SRyan Roberts folio = vm_normal_folio(vma, addr, ptent);
34296f97c43SLorenzo Stoakes if (!folio || folio_is_zone_device(folio))
34334b67923SHugh Dickins continue;
34496f97c43SLorenzo Stoakes if (folio_test_large(folio))
34534b67923SHugh Dickins continue;
34634b67923SHugh Dickins if (vma->vm_flags & VM_LOCKED)
34796f97c43SLorenzo Stoakes mlock_folio(folio);
34834b67923SHugh Dickins else
34996f97c43SLorenzo Stoakes munlock_folio(folio);
35034b67923SHugh Dickins }
35134b67923SHugh Dickins pte_unmap(start_pte);
35234b67923SHugh Dickins out:
35334b67923SHugh Dickins spin_unlock(ptl);
35434b67923SHugh Dickins cond_resched();
3555344b7e6SNick Piggin return 0;
3565344b7e6SNick Piggin }
3575344b7e6SNick Piggin
3585344b7e6SNick Piggin /*
35934b67923SHugh Dickins * mlock_vma_pages_range() - mlock any pages already in the range,
36034b67923SHugh Dickins * or munlock all pages in the range.
36134b67923SHugh Dickins * @vma - vma containing range to be mlock()ed or munlock()ed
362ba470de4SRik van Riel * @start - start address in @vma of the range
36334b67923SHugh Dickins * @end - end of range in @vma
36434b67923SHugh Dickins * @newflags - the new set of flags for @vma.
365ba470de4SRik van Riel *
36634b67923SHugh Dickins * Called for mlock(), mlock2() and mlockall(), to set @vma VM_LOCKED;
36734b67923SHugh Dickins * called for munlock() and munlockall(), to clear VM_LOCKED from @vma.
368b291f000SNick Piggin */
mlock_vma_pages_range(struct vm_area_struct * vma,unsigned long start,unsigned long end,vm_flags_t newflags)36934b67923SHugh Dickins static void mlock_vma_pages_range(struct vm_area_struct *vma,
37034b67923SHugh Dickins unsigned long start, unsigned long end, vm_flags_t newflags)
371b291f000SNick Piggin {
37234b67923SHugh Dickins static const struct mm_walk_ops mlock_walk_ops = {
37334b67923SHugh Dickins .pmd_entry = mlock_pte_range,
37449b06385SSuren Baghdasaryan .walk_lock = PGWALK_WRLOCK_VERIFY,
37534b67923SHugh Dickins };
3767a8010cdSVlastimil Babka
3777a8010cdSVlastimil Babka /*
37834b67923SHugh Dickins * There is a slight chance that concurrent page migration,
37934b67923SHugh Dickins * or page reclaim finding a page of this now-VM_LOCKED vma,
3807efecffbSMatthew Wilcox (Oracle) * will call mlock_vma_folio() and raise page's mlock_count:
38134b67923SHugh Dickins * double counting, leaving the page unevictable indefinitely.
3827efecffbSMatthew Wilcox (Oracle) * Communicate this danger to mlock_vma_folio() with VM_IO,
38334b67923SHugh Dickins * which is a VM_SPECIAL flag not allowed on VM_LOCKED vmas.
38434b67923SHugh Dickins * mmap_lock is held in write mode here, so this weird
38534b67923SHugh Dickins * combination should not be visible to other mmap_lock users;
38634b67923SHugh Dickins * but WRITE_ONCE so rmap walkers must see VM_IO if VM_LOCKED.
3877a8010cdSVlastimil Babka */
38834b67923SHugh Dickins if (newflags & VM_LOCKED)
38934b67923SHugh Dickins newflags |= VM_IO;
390*60081bf1SSuren Baghdasaryan vma_start_write(vma);
391601c3c29SSuren Baghdasaryan vm_flags_reset_once(vma, newflags);
39234b67923SHugh Dickins
39334b67923SHugh Dickins lru_add_drain();
39434b67923SHugh Dickins walk_page_range(vma->vm_mm, start, end, &mlock_walk_ops, NULL);
39534b67923SHugh Dickins lru_add_drain();
39634b67923SHugh Dickins
39734b67923SHugh Dickins if (newflags & VM_IO) {
39834b67923SHugh Dickins newflags &= ~VM_IO;
399601c3c29SSuren Baghdasaryan vm_flags_reset_once(vma, newflags);
400408e82b7SHugh Dickins }
401b291f000SNick Piggin }
402b291f000SNick Piggin
403b291f000SNick Piggin /*
404b291f000SNick Piggin * mlock_fixup - handle mlock[all]/munlock[all] requests.
405b291f000SNick Piggin *
406b291f000SNick Piggin * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
407b291f000SNick Piggin * munlock is a no-op. However, for some special vmas, we go ahead and
408cea10a19SMichel Lespinasse * populate the ptes.
409b291f000SNick Piggin *
410b291f000SNick Piggin * For vmas that pass the filters, merge/split as appropriate.
411b291f000SNick Piggin */
mlock_fixup(struct vma_iterator * vmi,struct vm_area_struct * vma,struct vm_area_struct ** prev,unsigned long start,unsigned long end,vm_flags_t newflags)41237598f5aSLiam R. Howlett static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma,
41337598f5aSLiam R. Howlett struct vm_area_struct **prev, unsigned long start,
41437598f5aSLiam R. Howlett unsigned long end, vm_flags_t newflags)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm;
4171da177e4SLinus Torvalds pgoff_t pgoff;
418b291f000SNick Piggin int nr_pages;
4191da177e4SLinus Torvalds int ret = 0;
42034b67923SHugh Dickins vm_flags_t oldflags = vma->vm_flags;
4211da177e4SLinus Torvalds
42234b67923SHugh Dickins if (newflags == oldflags || (oldflags & VM_SPECIAL) ||
423e1fb4a08SDave Jiang is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm) ||
4241507f512SMike Rapoport vma_is_dax(vma) || vma_is_secretmem(vma))
425b0f205c2SEric B Munson /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
426b0f205c2SEric B Munson goto out;
427b291f000SNick Piggin
4281da177e4SLinus Torvalds pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
4299760ebffSLiam R. Howlett *prev = vma_merge(vmi, mm, *prev, start, end, newflags,
43037598f5aSLiam R. Howlett vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
4315c26f6acSSuren Baghdasaryan vma->vm_userfaultfd_ctx, anon_vma_name(vma));
4321da177e4SLinus Torvalds if (*prev) {
4331da177e4SLinus Torvalds vma = *prev;
4341da177e4SLinus Torvalds goto success;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds
4371da177e4SLinus Torvalds if (start != vma->vm_start) {
4389760ebffSLiam R. Howlett ret = split_vma(vmi, vma, start, 1);
4391da177e4SLinus Torvalds if (ret)
4401da177e4SLinus Torvalds goto out;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds
4431da177e4SLinus Torvalds if (end != vma->vm_end) {
4449760ebffSLiam R. Howlett ret = split_vma(vmi, vma, end, 0);
4451da177e4SLinus Torvalds if (ret)
4461da177e4SLinus Torvalds goto out;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds success:
4501da177e4SLinus Torvalds /*
451b291f000SNick Piggin * Keep track of amount of locked VM.
452b291f000SNick Piggin */
453b291f000SNick Piggin nr_pages = (end - start) >> PAGE_SHIFT;
45434b67923SHugh Dickins if (!(newflags & VM_LOCKED))
455b291f000SNick Piggin nr_pages = -nr_pages;
45634b67923SHugh Dickins else if (oldflags & VM_LOCKED)
457b155b4fdSSimon Guo nr_pages = 0;
458b291f000SNick Piggin mm->locked_vm += nr_pages;
459b291f000SNick Piggin
460b291f000SNick Piggin /*
461c1e8d7c6SMichel Lespinasse * vm_flags is protected by the mmap_lock held in write mode.
4621da177e4SLinus Torvalds * It's okay if try_to_unmap_one unmaps a page just after we
463fc05f566SKirill A. Shutemov * set VM_LOCKED, populate_vma_page_range will bring it back.
4641da177e4SLinus Torvalds */
46534b67923SHugh Dickins if ((newflags & VM_LOCKED) && (oldflags & VM_LOCKED)) {
46634b67923SHugh Dickins /* No work to do, and mlocking twice would be wrong */
467*60081bf1SSuren Baghdasaryan vma_start_write(vma);
4681c71222eSSuren Baghdasaryan vm_flags_reset(vma, newflags);
46934b67923SHugh Dickins } else {
47034b67923SHugh Dickins mlock_vma_pages_range(vma, start, end, newflags);
47134b67923SHugh Dickins }
4721da177e4SLinus Torvalds out:
473b291f000SNick Piggin *prev = vma;
4741da177e4SLinus Torvalds return ret;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds
apply_vma_lock_flags(unsigned long start,size_t len,vm_flags_t flags)4771aab92ecSEric B Munson static int apply_vma_lock_flags(unsigned long start, size_t len,
4781aab92ecSEric B Munson vm_flags_t flags)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds unsigned long nstart, end, tmp;
4811da177e4SLinus Torvalds struct vm_area_struct *vma, *prev;
48237598f5aSLiam R. Howlett VMA_ITERATOR(vmi, current->mm, start);
4831da177e4SLinus Torvalds
4848fd9e488SAlexander Kuleshov VM_BUG_ON(offset_in_page(start));
485fed067daSMichel Lespinasse VM_BUG_ON(len != PAGE_ALIGN(len));
4861da177e4SLinus Torvalds end = start + len;
4871da177e4SLinus Torvalds if (end < start)
4881da177e4SLinus Torvalds return -EINVAL;
4891da177e4SLinus Torvalds if (end == start)
4901da177e4SLinus Torvalds return 0;
49137598f5aSLiam R. Howlett vma = vma_iter_load(&vmi);
49233108b05SMatthew Wilcox (Oracle) if (!vma)
4931da177e4SLinus Torvalds return -ENOMEM;
4941da177e4SLinus Torvalds
49537598f5aSLiam R. Howlett prev = vma_prev(&vmi);
4961da177e4SLinus Torvalds if (start > vma->vm_start)
4971da177e4SLinus Torvalds prev = vma;
4981da177e4SLinus Torvalds
49937598f5aSLiam R. Howlett nstart = start;
50037598f5aSLiam R. Howlett tmp = vma->vm_start;
50137598f5aSLiam R. Howlett for_each_vma_range(vmi, vma, end) {
5022658f94dSLiam R. Howlett int error;
50337598f5aSLiam R. Howlett vm_flags_t newflags;
5041aab92ecSEric B Munson
50537598f5aSLiam R. Howlett if (vma->vm_start != tmp)
50637598f5aSLiam R. Howlett return -ENOMEM;
50737598f5aSLiam R. Howlett
508e430a95aSSuren Baghdasaryan newflags = vma->vm_flags & ~VM_LOCKED_MASK;
5091aab92ecSEric B Munson newflags |= flags;
5101da177e4SLinus Torvalds /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
5111da177e4SLinus Torvalds tmp = vma->vm_end;
5121da177e4SLinus Torvalds if (tmp > end)
5131da177e4SLinus Torvalds tmp = end;
51437598f5aSLiam R. Howlett error = mlock_fixup(&vmi, vma, &prev, nstart, tmp, newflags);
5151da177e4SLinus Torvalds if (error)
5162658f94dSLiam R. Howlett return error;
5172658f94dSLiam R. Howlett tmp = vma_iter_end(&vmi);
5181da177e4SLinus Torvalds nstart = tmp;
51937598f5aSLiam R. Howlett }
5201da177e4SLinus Torvalds
5212658f94dSLiam R. Howlett if (tmp < end)
52237598f5aSLiam R. Howlett return -ENOMEM;
52337598f5aSLiam R. Howlett
5242658f94dSLiam R. Howlett return 0;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds
5270cf2f6f6SSimon Guo /*
5280cf2f6f6SSimon Guo * Go through vma areas and sum size of mlocked
5290cf2f6f6SSimon Guo * vma pages, as return value.
5300cf2f6f6SSimon Guo * Note deferred memory locking case(mlock2(,,MLOCK_ONFAULT)
5310cf2f6f6SSimon Guo * is also counted.
5320cf2f6f6SSimon Guo * Return value: previously mlocked page counts
5330cf2f6f6SSimon Guo */
count_mm_mlocked_page_nr(struct mm_struct * mm,unsigned long start,size_t len)5340874bb49Sswkhack static unsigned long count_mm_mlocked_page_nr(struct mm_struct *mm,
5350cf2f6f6SSimon Guo unsigned long start, size_t len)
5360cf2f6f6SSimon Guo {
5370cf2f6f6SSimon Guo struct vm_area_struct *vma;
5380874bb49Sswkhack unsigned long count = 0;
53933108b05SMatthew Wilcox (Oracle) unsigned long end;
54033108b05SMatthew Wilcox (Oracle) VMA_ITERATOR(vmi, mm, start);
5410cf2f6f6SSimon Guo
54233108b05SMatthew Wilcox (Oracle) /* Don't overflow past ULONG_MAX */
54333108b05SMatthew Wilcox (Oracle) if (unlikely(ULONG_MAX - len < start))
54433108b05SMatthew Wilcox (Oracle) end = ULONG_MAX;
54533108b05SMatthew Wilcox (Oracle) else
54633108b05SMatthew Wilcox (Oracle) end = start + len;
54766071896SLiam Howlett
54833108b05SMatthew Wilcox (Oracle) for_each_vma_range(vmi, vma, end) {
5490cf2f6f6SSimon Guo if (vma->vm_flags & VM_LOCKED) {
5500cf2f6f6SSimon Guo if (start > vma->vm_start)
5510cf2f6f6SSimon Guo count -= (start - vma->vm_start);
55233108b05SMatthew Wilcox (Oracle) if (end < vma->vm_end) {
55333108b05SMatthew Wilcox (Oracle) count += end - vma->vm_start;
5540cf2f6f6SSimon Guo break;
5550cf2f6f6SSimon Guo }
5560cf2f6f6SSimon Guo count += vma->vm_end - vma->vm_start;
5570cf2f6f6SSimon Guo }
5580cf2f6f6SSimon Guo }
5590cf2f6f6SSimon Guo
5600cf2f6f6SSimon Guo return count >> PAGE_SHIFT;
5610cf2f6f6SSimon Guo }
5620cf2f6f6SSimon Guo
563ebcbc6eaSHugh Dickins /*
564ebcbc6eaSHugh Dickins * convert get_user_pages() return value to posix mlock() error
565ebcbc6eaSHugh Dickins */
__mlock_posix_error_return(long retval)566ebcbc6eaSHugh Dickins static int __mlock_posix_error_return(long retval)
567ebcbc6eaSHugh Dickins {
568ebcbc6eaSHugh Dickins if (retval == -EFAULT)
569ebcbc6eaSHugh Dickins retval = -ENOMEM;
570ebcbc6eaSHugh Dickins else if (retval == -ENOMEM)
571ebcbc6eaSHugh Dickins retval = -EAGAIN;
572ebcbc6eaSHugh Dickins return retval;
573ebcbc6eaSHugh Dickins }
574ebcbc6eaSHugh Dickins
do_mlock(unsigned long start,size_t len,vm_flags_t flags)575dc0ef0dfSMichal Hocko static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t flags)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds unsigned long locked;
5781da177e4SLinus Torvalds unsigned long lock_limit;
5791da177e4SLinus Torvalds int error = -ENOMEM;
5801da177e4SLinus Torvalds
581057d3389SAndrey Konovalov start = untagged_addr(start);
582057d3389SAndrey Konovalov
5831da177e4SLinus Torvalds if (!can_do_mlock())
5841da177e4SLinus Torvalds return -EPERM;
5851da177e4SLinus Torvalds
5868fd9e488SAlexander Kuleshov len = PAGE_ALIGN(len + (offset_in_page(start)));
5871da177e4SLinus Torvalds start &= PAGE_MASK;
5881da177e4SLinus Torvalds
58959e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK);
5901da177e4SLinus Torvalds lock_limit >>= PAGE_SHIFT;
5911f1cd705SDavidlohr Bueso locked = len >> PAGE_SHIFT;
5921f1cd705SDavidlohr Bueso
593d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(current->mm))
594dc0ef0dfSMichal Hocko return -EINTR;
5951f1cd705SDavidlohr Bueso
5961f1cd705SDavidlohr Bueso locked += current->mm->locked_vm;
5970cf2f6f6SSimon Guo if ((locked > lock_limit) && (!capable(CAP_IPC_LOCK))) {
5980cf2f6f6SSimon Guo /*
5990cf2f6f6SSimon Guo * It is possible that the regions requested intersect with
6000cf2f6f6SSimon Guo * previously mlocked areas, that part area in "mm->locked_vm"
6010cf2f6f6SSimon Guo * should not be counted to new mlock increment count. So check
6020cf2f6f6SSimon Guo * and adjust locked count if necessary.
6030cf2f6f6SSimon Guo */
6040cf2f6f6SSimon Guo locked -= count_mm_mlocked_page_nr(current->mm,
6050cf2f6f6SSimon Guo start, len);
6060cf2f6f6SSimon Guo }
6071da177e4SLinus Torvalds
6081da177e4SLinus Torvalds /* check against resource limits */
6091da177e4SLinus Torvalds if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
6101aab92ecSEric B Munson error = apply_vma_lock_flags(start, len, flags);
6111f1cd705SDavidlohr Bueso
612d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
613c561259cSKirill A. Shutemov if (error)
6141da177e4SLinus Torvalds return error;
615c561259cSKirill A. Shutemov
616c561259cSKirill A. Shutemov error = __mm_populate(start, len, 0);
617c561259cSKirill A. Shutemov if (error)
618c561259cSKirill A. Shutemov return __mlock_posix_error_return(error);
619c561259cSKirill A. Shutemov return 0;
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds
SYSCALL_DEFINE2(mlock,unsigned long,start,size_t,len)6221aab92ecSEric B Munson SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
6231aab92ecSEric B Munson {
6241aab92ecSEric B Munson return do_mlock(start, len, VM_LOCKED);
6251aab92ecSEric B Munson }
6261aab92ecSEric B Munson
SYSCALL_DEFINE3(mlock2,unsigned long,start,size_t,len,int,flags)627a8ca5d0eSEric B Munson SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags)
628a8ca5d0eSEric B Munson {
629b0f205c2SEric B Munson vm_flags_t vm_flags = VM_LOCKED;
630b0f205c2SEric B Munson
631b0f205c2SEric B Munson if (flags & ~MLOCK_ONFAULT)
632a8ca5d0eSEric B Munson return -EINVAL;
633a8ca5d0eSEric B Munson
634b0f205c2SEric B Munson if (flags & MLOCK_ONFAULT)
635b0f205c2SEric B Munson vm_flags |= VM_LOCKONFAULT;
636b0f205c2SEric B Munson
637b0f205c2SEric B Munson return do_mlock(start, len, vm_flags);
638a8ca5d0eSEric B Munson }
639a8ca5d0eSEric B Munson
SYSCALL_DEFINE2(munlock,unsigned long,start,size_t,len)6406a6160a7SHeiko Carstens SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds int ret;
6431da177e4SLinus Torvalds
644057d3389SAndrey Konovalov start = untagged_addr(start);
645057d3389SAndrey Konovalov
6468fd9e488SAlexander Kuleshov len = PAGE_ALIGN(len + (offset_in_page(start)));
6471da177e4SLinus Torvalds start &= PAGE_MASK;
6481f1cd705SDavidlohr Bueso
649d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(current->mm))
650dc0ef0dfSMichal Hocko return -EINTR;
6511aab92ecSEric B Munson ret = apply_vma_lock_flags(start, len, 0);
652d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
6531f1cd705SDavidlohr Bueso
6541da177e4SLinus Torvalds return ret;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds
657b0f205c2SEric B Munson /*
658b0f205c2SEric B Munson * Take the MCL_* flags passed into mlockall (or 0 if called from munlockall)
659b0f205c2SEric B Munson * and translate into the appropriate modifications to mm->def_flags and/or the
660b0f205c2SEric B Munson * flags for all current VMAs.
661b0f205c2SEric B Munson *
662b0f205c2SEric B Munson * There are a couple of subtleties with this. If mlockall() is called multiple
663b0f205c2SEric B Munson * times with different flags, the values do not necessarily stack. If mlockall
664b0f205c2SEric B Munson * is called once including the MCL_FUTURE flag and then a second time without
665b0f205c2SEric B Munson * it, VM_LOCKED and VM_LOCKONFAULT will be cleared from mm->def_flags.
666b0f205c2SEric B Munson */
apply_mlockall_flags(int flags)6671aab92ecSEric B Munson static int apply_mlockall_flags(int flags)
6681da177e4SLinus Torvalds {
66937598f5aSLiam R. Howlett VMA_ITERATOR(vmi, current->mm, 0);
6701da177e4SLinus Torvalds struct vm_area_struct *vma, *prev = NULL;
671b0f205c2SEric B Munson vm_flags_t to_add = 0;
6721da177e4SLinus Torvalds
673e430a95aSSuren Baghdasaryan current->mm->def_flags &= ~VM_LOCKED_MASK;
674b0f205c2SEric B Munson if (flags & MCL_FUTURE) {
67509a9f1d2SMichel Lespinasse current->mm->def_flags |= VM_LOCKED;
6761aab92ecSEric B Munson
677b0f205c2SEric B Munson if (flags & MCL_ONFAULT)
678b0f205c2SEric B Munson current->mm->def_flags |= VM_LOCKONFAULT;
679b0f205c2SEric B Munson
680b0f205c2SEric B Munson if (!(flags & MCL_CURRENT))
6811da177e4SLinus Torvalds goto out;
682b0f205c2SEric B Munson }
683b0f205c2SEric B Munson
684b0f205c2SEric B Munson if (flags & MCL_CURRENT) {
685b0f205c2SEric B Munson to_add |= VM_LOCKED;
686b0f205c2SEric B Munson if (flags & MCL_ONFAULT)
687b0f205c2SEric B Munson to_add |= VM_LOCKONFAULT;
688b0f205c2SEric B Munson }
6891da177e4SLinus Torvalds
69037598f5aSLiam R. Howlett for_each_vma(vmi, vma) {
691ca16d140SKOSAKI Motohiro vm_flags_t newflags;
6921da177e4SLinus Torvalds
693e430a95aSSuren Baghdasaryan newflags = vma->vm_flags & ~VM_LOCKED_MASK;
694b0f205c2SEric B Munson newflags |= to_add;
6951da177e4SLinus Torvalds
6961da177e4SLinus Torvalds /* Ignore errors */
69737598f5aSLiam R. Howlett mlock_fixup(&vmi, vma, &prev, vma->vm_start, vma->vm_end,
69837598f5aSLiam R. Howlett newflags);
69950d4fb78SPaul E. McKenney cond_resched();
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds out:
7021da177e4SLinus Torvalds return 0;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds
SYSCALL_DEFINE1(mlockall,int,flags)7053480b257SHeiko Carstens SYSCALL_DEFINE1(mlockall, int, flags)
7061da177e4SLinus Torvalds {
7071da177e4SLinus Torvalds unsigned long lock_limit;
70886d2adccSAlexey Klimov int ret;
7091da177e4SLinus Torvalds
710dedca635SPotyra, Stefan if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT)) ||
711dedca635SPotyra, Stefan flags == MCL_ONFAULT)
71286d2adccSAlexey Klimov return -EINVAL;
7131da177e4SLinus Torvalds
7141da177e4SLinus Torvalds if (!can_do_mlock())
71586d2adccSAlexey Klimov return -EPERM;
7161da177e4SLinus Torvalds
71759e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK);
7181da177e4SLinus Torvalds lock_limit >>= PAGE_SHIFT;
7191da177e4SLinus Torvalds
720d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(current->mm))
721dc0ef0dfSMichal Hocko return -EINTR;
7221f1cd705SDavidlohr Bueso
723dc0ef0dfSMichal Hocko ret = -ENOMEM;
7241da177e4SLinus Torvalds if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
7251da177e4SLinus Torvalds capable(CAP_IPC_LOCK))
7261aab92ecSEric B Munson ret = apply_mlockall_flags(flags);
727d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
728bebeb3d6SMichel Lespinasse if (!ret && (flags & MCL_CURRENT))
729bebeb3d6SMichel Lespinasse mm_populate(0, TASK_SIZE);
73086d2adccSAlexey Klimov
7311da177e4SLinus Torvalds return ret;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds
SYSCALL_DEFINE0(munlockall)7343480b257SHeiko Carstens SYSCALL_DEFINE0(munlockall)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds int ret;
7371da177e4SLinus Torvalds
738d8ed45c5SMichel Lespinasse if (mmap_write_lock_killable(current->mm))
739dc0ef0dfSMichal Hocko return -EINTR;
7401aab92ecSEric B Munson ret = apply_mlockall_flags(0);
741d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm);
7421da177e4SLinus Torvalds return ret;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds
7451da177e4SLinus Torvalds /*
7461da177e4SLinus Torvalds * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
7471da177e4SLinus Torvalds * shm segments) get accounted against the user_struct instead.
7481da177e4SLinus Torvalds */
7491da177e4SLinus Torvalds static DEFINE_SPINLOCK(shmlock_user_lock);
7501da177e4SLinus Torvalds
user_shm_lock(size_t size,struct ucounts * ucounts)751d7c9e99aSAlexey Gladkov int user_shm_lock(size_t size, struct ucounts *ucounts)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds unsigned long lock_limit, locked;
754d7c9e99aSAlexey Gladkov long memlock;
7551da177e4SLinus Torvalds int allowed = 0;
7561da177e4SLinus Torvalds
7571da177e4SLinus Torvalds locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
75859e99e5bSJiri Slaby lock_limit = rlimit(RLIMIT_MEMLOCK);
759e97824ffSMiaohe Lin if (lock_limit != RLIM_INFINITY)
7601da177e4SLinus Torvalds lock_limit >>= PAGE_SHIFT;
7611da177e4SLinus Torvalds spin_lock(&shmlock_user_lock);
762d7c9e99aSAlexey Gladkov memlock = inc_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
763d7c9e99aSAlexey Gladkov
764e97824ffSMiaohe Lin if ((memlock == LONG_MAX || memlock > lock_limit) && !capable(CAP_IPC_LOCK)) {
765d7c9e99aSAlexey Gladkov dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
7661da177e4SLinus Torvalds goto out;
767d7c9e99aSAlexey Gladkov }
768d7c9e99aSAlexey Gladkov if (!get_ucounts(ucounts)) {
769d7c9e99aSAlexey Gladkov dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
7705c2a956cSMiaohe Lin allowed = 0;
771d7c9e99aSAlexey Gladkov goto out;
772d7c9e99aSAlexey Gladkov }
7731da177e4SLinus Torvalds allowed = 1;
7741da177e4SLinus Torvalds out:
7751da177e4SLinus Torvalds spin_unlock(&shmlock_user_lock);
7761da177e4SLinus Torvalds return allowed;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds
user_shm_unlock(size_t size,struct ucounts * ucounts)779d7c9e99aSAlexey Gladkov void user_shm_unlock(size_t size, struct ucounts *ucounts)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds spin_lock(&shmlock_user_lock);
782d7c9e99aSAlexey Gladkov dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
7831da177e4SLinus Torvalds spin_unlock(&shmlock_user_lock);
784d7c9e99aSAlexey Gladkov put_ucounts(ucounts);
7851da177e4SLinus Torvalds }
786