blob: 96309eeed8606d602d579ad5d7d2b2d91c1a0077 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/mm/mlock.c
4 *
5 * (C) Copyright 1995 Linus Torvalds
6 * (C) Copyright 2002 Christoph Hellwig
7 */
8
Randy.Dunlapc59ede72006-01-11 12:17:46 -08009#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mman.h>
11#include <linux/mm.h>
Ingo Molnar8703e8a2017-02-08 18:51:30 +010012#include <linux/sched/user.h>
Nick Pigginb291f002008-10-18 20:26:44 -070013#include <linux/swap.h>
14#include <linux/swapops.h>
15#include <linux/pagemap.h>
Kalesh Singhd83231e2024-04-22 14:24:59 -070016#include <linux/pgsize_migration.h>
Vlastimil Babka72255222013-09-11 14:22:29 -070017#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mempolicy.h>
19#include <linux/syscalls.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040020#include <linux/sched.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040021#include <linux/export.h>
Nick Pigginb291f002008-10-18 20:26:44 -070022#include <linux/rmap.h>
23#include <linux/mmzone.h>
24#include <linux/hugetlb.h>
Vlastimil Babka72255222013-09-11 14:22:29 -070025#include <linux/memcontrol.h>
26#include <linux/mm_inline.h>
Mike Rapoport1507f512021-07-07 18:08:03 -070027#include <linux/secretmem.h>
Jianxiong Pan07622f02022-09-22 17:25:35 +080028#ifdef CONFIG_AMLOGIC_PIN_LOCKED_FILE
29#include <linux/amlogic/pin_file.h>
30#endif
Nick Pigginb291f002008-10-18 20:26:44 -070031
32#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Wang Xiaoqiang7f43add2016-01-15 16:57:22 -080034bool can_do_mlock(void)
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040035{
Jiri Slaby59e99e52010-03-05 13:41:44 -080036 if (rlimit(RLIMIT_MEMLOCK) != 0)
Wang Xiaoqiang7f43add2016-01-15 16:57:22 -080037 return true;
Jeff Vander Stoepa5a65792015-03-12 16:26:17 -070038 if (capable(CAP_IPC_LOCK))
Wang Xiaoqiang7f43add2016-01-15 16:57:22 -080039 return true;
40 return false;
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040041}
42EXPORT_SYMBOL(can_do_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Nick Pigginb291f002008-10-18 20:26:44 -070044/*
45 * Mlocked pages are marked with PageMlocked() flag for efficient testing
46 * in vmscan and, possibly, the fault path; and to support semi-accurate
47 * statistics.
48 *
49 * An mlocked page [PageMlocked(page)] is unevictable. As such, it will
50 * be placed on the LRU "unevictable" list, rather than the [in]active lists.
51 * The unevictable list is an LRU sibling list to the [in]active lists.
52 * PageUnevictable is set to indicate the unevictable state.
53 *
54 * When lazy mlocking via vmscan, it is important to ensure that the
55 * vma's VM_LOCKED status is not concurrently being modified, otherwise we
56 * may have mlocked a page that is being munlocked. So lazy mlock must take
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070057 * the mmap_lock for read, and verify that the vma really is locked
Nick Pigginb291f002008-10-18 20:26:44 -070058 * (see mm/rmap.c).
59 */
60
61/*
62 * LRU accounting for clear_page_mlock()
63 */
Hugh Dickinse6c509f2012-10-08 16:33:19 -070064void clear_page_mlock(struct page *page)
Nick Pigginb291f002008-10-18 20:26:44 -070065{
Hugh Dickins09647302020-09-18 21:20:15 -070066 int nr_pages;
67
Hugh Dickinse6c509f2012-10-08 16:33:19 -070068 if (!TestClearPageMlocked(page))
Nick Pigginb291f002008-10-18 20:26:44 -070069 return;
Nick Pigginb291f002008-10-18 20:26:44 -070070
Hugh Dickins09647302020-09-18 21:20:15 -070071 nr_pages = thp_nr_pages(page);
72 mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
73 count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages);
Shakeel Butt9c4e6b12018-02-21 14:45:28 -080074 /*
75 * The previous TestClearPageMlocked() corresponds to the smp_mb()
76 * in __pagevec_lru_add_fn().
77 *
78 * See __pagevec_lru_add_fn for more explanation.
79 */
Nick Pigginb291f002008-10-18 20:26:44 -070080 if (!isolate_lru_page(page)) {
81 putback_lru_page(page);
82 } else {
83 /*
KOSAKI Motohiro8891d6d2008-11-12 13:26:53 -080084 * We lost the race. the page already moved to evictable list.
Nick Pigginb291f002008-10-18 20:26:44 -070085 */
KOSAKI Motohiro8891d6d2008-11-12 13:26:53 -080086 if (PageUnevictable(page))
Hugh Dickins09647302020-09-18 21:20:15 -070087 count_vm_events(UNEVICTABLE_PGSTRANDED, nr_pages);
Nick Pigginb291f002008-10-18 20:26:44 -070088 }
89}
90
91/*
92 * Mark page as mlocked if not already.
93 * If page on LRU, isolate and putback to move to unevictable list.
94 */
95void mlock_vma_page(struct page *page)
96{
Vlastimil Babka57e68e92014-04-07 15:37:50 -070097 /* Serialize with page migration */
Nick Pigginb291f002008-10-18 20:26:44 -070098 BUG_ON(!PageLocked(page));
99
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800100 VM_BUG_ON_PAGE(PageTail(page), page);
101 VM_BUG_ON_PAGE(PageCompound(page) && PageDoubleMap(page), page);
102
Nick Piggin5344b7e2008-10-18 20:26:51 -0700103 if (!TestSetPageMlocked(page)) {
Hugh Dickins09647302020-09-18 21:20:15 -0700104 int nr_pages = thp_nr_pages(page);
105
106 mod_zone_page_state(page_zone(page), NR_MLOCK, nr_pages);
107 count_vm_events(UNEVICTABLE_PGMLOCKED, nr_pages);
Nick Piggin5344b7e2008-10-18 20:26:51 -0700108 if (!isolate_lru_page(page))
109 putback_lru_page(page);
110 }
Nick Pigginb291f002008-10-18 20:26:44 -0700111}
112
Vlastimil Babka72255222013-09-11 14:22:29 -0700113/*
114 * Finish munlock after successful page isolation
115 *
Alistair Popplecd627342021-06-30 18:54:12 -0700116 * Page must be locked. This is a wrapper for page_mlock()
Vlastimil Babka72255222013-09-11 14:22:29 -0700117 * and putback_lru_page() with munlock accounting.
118 */
119static void __munlock_isolated_page(struct page *page)
120{
Vlastimil Babka72255222013-09-11 14:22:29 -0700121 /*
122 * Optimization: if the page was mapped just once, that's our mapping
123 * and we don't need to check all the other vmas.
124 */
125 if (page_mapcount(page) > 1)
Alistair Popplecd627342021-06-30 18:54:12 -0700126 page_mlock(page);
Vlastimil Babka72255222013-09-11 14:22:29 -0700127
128 /* Did try_to_unlock() succeed or punt? */
Minchan Kim192d7232017-05-03 14:54:10 -0700129 if (!PageMlocked(page))
Hugh Dickins09647302020-09-18 21:20:15 -0700130 count_vm_events(UNEVICTABLE_PGMUNLOCKED, thp_nr_pages(page));
Vlastimil Babka72255222013-09-11 14:22:29 -0700131
132 putback_lru_page(page);
133}
134
135/*
136 * Accounting for page isolation fail during munlock
137 *
138 * Performs accounting when page isolation fails in munlock. There is nothing
139 * else to do because it means some other task has already removed the page
140 * from the LRU. putback_lru_page() will take care of removing the page from
141 * the unevictable list, if necessary. vmscan [page_referenced()] will move
142 * the page back to the unevictable list if some other vma has it mlocked.
143 */
144static void __munlock_isolation_failed(struct page *page)
145{
Hugh Dickins09647302020-09-18 21:20:15 -0700146 int nr_pages = thp_nr_pages(page);
147
Vlastimil Babka72255222013-09-11 14:22:29 -0700148 if (PageUnevictable(page))
Hugh Dickins09647302020-09-18 21:20:15 -0700149 __count_vm_events(UNEVICTABLE_PGSTRANDED, nr_pages);
Vlastimil Babka72255222013-09-11 14:22:29 -0700150 else
Hugh Dickins09647302020-09-18 21:20:15 -0700151 __count_vm_events(UNEVICTABLE_PGMUNLOCKED, nr_pages);
Vlastimil Babka72255222013-09-11 14:22:29 -0700152}
153
Lee Schermerhorn6927c1d2009-12-14 17:59:55 -0800154/**
155 * munlock_vma_page - munlock a vma page
Mike Rapoportb7701a52018-02-06 15:42:13 -0800156 * @page: page to be unlocked, either a normal page or THP page head
Vlastimil Babkac424be12014-01-02 12:58:43 -0800157 *
158 * returns the size of the page as a page mask (0 for normal page,
159 * HPAGE_PMD_NR - 1 for THP head page)
Nick Pigginb291f002008-10-18 20:26:44 -0700160 *
Lee Schermerhorn6927c1d2009-12-14 17:59:55 -0800161 * called from munlock()/munmap() path with page supposedly on the LRU.
162 * When we munlock a page, because the vma where we found the page is being
163 * munlock()ed or munmap()ed, we want to check whether other vmas hold the
164 * page locked so that we can leave it on the unevictable lru list and not
165 * bother vmscan with it. However, to walk the page's rmap list in
Alistair Popplecd627342021-06-30 18:54:12 -0700166 * page_mlock() we must isolate the page from the LRU. If some other
Lee Schermerhorn6927c1d2009-12-14 17:59:55 -0800167 * task has removed the page from the LRU, we won't be able to do that.
168 * So we clear the PageMlocked as we might not get another chance. If we
169 * can't isolate the page, we leave it for putback_lru_page() and vmscan
170 * [page_referenced()/try_to_unmap()] to deal with.
Nick Pigginb291f002008-10-18 20:26:44 -0700171 */
Michel Lespinasseff6a6da2013-02-27 17:02:44 -0800172unsigned int munlock_vma_page(struct page *page)
Nick Pigginb291f002008-10-18 20:26:44 -0700173{
Kirill A. Shutemov7162a1e2016-01-21 16:40:27 -0800174 int nr_pages;
Michel Lespinasseff6a6da2013-02-27 17:02:44 -0800175
Alistair Popplecd627342021-06-30 18:54:12 -0700176 /* For page_mlock() and to serialize with page migration */
Nick Pigginb291f002008-10-18 20:26:44 -0700177 BUG_ON(!PageLocked(page));
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800178 VM_BUG_ON_PAGE(PageTail(page), page);
179
Kirill A. Shutemov655548b2016-11-30 15:54:05 -0800180 if (!TestClearPageMlocked(page)) {
181 /* Potentially, PTE-mapped THP: do not skip the rest PTEs */
Alex Shi3db19aa2020-12-15 12:34:07 -0800182 return 0;
Kirill A. Shutemov655548b2016-11-30 15:54:05 -0800183 }
Vlastimil Babka01cc2e52014-01-23 15:52:50 -0800184
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -0700185 nr_pages = thp_nr_pages(page);
Alex Shi3db19aa2020-12-15 12:34:07 -0800186 mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
Vlastimil Babka01cc2e52014-01-23 15:52:50 -0800187
Alex Shi3db19aa2020-12-15 12:34:07 -0800188 if (!isolate_lru_page(page))
Vlastimil Babka01cc2e52014-01-23 15:52:50 -0800189 __munlock_isolated_page(page);
Alex Shi3db19aa2020-12-15 12:34:07 -0800190 else
191 __munlock_isolation_failed(page);
Vlastimil Babka01cc2e52014-01-23 15:52:50 -0800192
Vlastimil Babkac424be12014-01-02 12:58:43 -0800193 return nr_pages - 1;
Nick Pigginb291f002008-10-18 20:26:44 -0700194}
195
Lee Schermerhorn9978ad52008-10-18 20:26:56 -0700196/*
197 * convert get_user_pages() return value to posix mlock() error
198 */
Jianxiong Pan07622f02022-09-22 17:25:35 +0800199#ifndef CONFIG_AMLOGIC_PIN_LOCKED_FILE_V2
Lee Schermerhorn9978ad52008-10-18 20:26:56 -0700200static int __mlock_posix_error_return(long retval)
201{
202 if (retval == -EFAULT)
203 retval = -ENOMEM;
204 else if (retval == -ENOMEM)
205 retval = -EAGAIN;
206 return retval;
Nick Pigginb291f002008-10-18 20:26:44 -0700207}
Jianxiong Pan07622f02022-09-22 17:25:35 +0800208#endif
Nick Pigginb291f002008-10-18 20:26:44 -0700209
Nick Pigginb291f002008-10-18 20:26:44 -0700210/*
Vlastimil Babka56afe472013-09-11 14:22:32 -0700211 * Prepare page for fast batched LRU putback via putback_lru_evictable_pagevec()
212 *
213 * The fast path is available only for evictable pages with single mapping.
214 * Then we can bypass the per-cpu pvec and get better performance.
Alistair Popplecd627342021-06-30 18:54:12 -0700215 * when mapcount > 1 we need page_mlock() which can fail.
Vlastimil Babka56afe472013-09-11 14:22:32 -0700216 * when !page_evictable(), we need the full redo logic of putback_lru_page to
217 * avoid leaving evictable page in unevictable list.
218 *
219 * In case of success, @page is added to @pvec and @pgrescued is incremented
220 * in case that the page was previously unevictable. @page is also unlocked.
221 */
222static bool __putback_lru_fast_prepare(struct page *page, struct pagevec *pvec,
223 int *pgrescued)
224{
Sasha Levin309381fea2014-01-23 15:52:54 -0800225 VM_BUG_ON_PAGE(PageLRU(page), page);
226 VM_BUG_ON_PAGE(!PageLocked(page), page);
Vlastimil Babka56afe472013-09-11 14:22:32 -0700227
228 if (page_mapcount(page) <= 1 && page_evictable(page)) {
229 pagevec_add(pvec, page);
230 if (TestClearPageUnevictable(page))
231 (*pgrescued)++;
232 unlock_page(page);
233 return true;
234 }
235
236 return false;
237}
238
239/*
240 * Putback multiple evictable pages to the LRU
241 *
242 * Batched putback of evictable pages that bypasses the per-cpu pvec. Some of
243 * the pages might have meanwhile become unevictable but that is OK.
244 */
245static void __putback_lru_fast(struct pagevec *pvec, int pgrescued)
246{
247 count_vm_events(UNEVICTABLE_PGMUNLOCKED, pagevec_count(pvec));
248 /*
249 *__pagevec_lru_add() calls release_pages() so we don't call
250 * put_page() explicitly
251 */
252 __pagevec_lru_add(pvec);
253 count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
254}
255
256/*
Vlastimil Babka72255222013-09-11 14:22:29 -0700257 * Munlock a batch of pages from the same zone
258 *
259 * The work is split to two main phases. First phase clears the Mlocked flag
260 * and attempts to isolate the pages, all under a single zone lru lock.
261 * The second phase finishes the munlock only for pages where isolation
262 * succeeded.
263 *
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700264 * Note that the pagevec may be modified during the process.
Vlastimil Babka72255222013-09-11 14:22:29 -0700265 */
266static void __munlock_pagevec(struct pagevec *pvec, struct zone *zone)
267{
268 int i;
269 int nr = pagevec_count(pvec);
Yisheng Xie70feee02017-06-02 14:46:43 -0700270 int delta_munlocked = -nr;
Vlastimil Babka56afe472013-09-11 14:22:32 -0700271 struct pagevec pvec_putback;
Alex Shi6168d0d2020-12-15 12:34:29 -0800272 struct lruvec *lruvec = NULL;
Vlastimil Babka56afe472013-09-11 14:22:32 -0700273 int pgrescued = 0;
Vlastimil Babka72255222013-09-11 14:22:29 -0700274
Mel Gorman86679822017-11-15 17:37:52 -0800275 pagevec_init(&pvec_putback);
Vlastimil Babka3b25df92014-01-02 12:58:44 -0800276
Vlastimil Babka72255222013-09-11 14:22:29 -0700277 /* Phase 1: page isolation */
Vlastimil Babka72255222013-09-11 14:22:29 -0700278 for (i = 0; i < nr; i++) {
279 struct page *page = pvec->pages[i];
280
281 if (TestClearPageMlocked(page)) {
Vlastimil Babka72255222013-09-11 14:22:29 -0700282 /*
Vlastimil Babka01cc2e52014-01-23 15:52:50 -0800283 * We already have pin from follow_page_mask()
284 * so we can spare the get_page() here.
Vlastimil Babka72255222013-09-11 14:22:29 -0700285 */
Alex Shid25b5bd2020-12-15 12:34:16 -0800286 if (TestClearPageLRU(page)) {
Alexander Duyck2a5e4e32020-12-15 12:34:33 -0800287 lruvec = relock_page_lruvec_irq(page, lruvec);
Yu Zhao46ae6b22021-02-24 12:08:25 -0800288 del_page_from_lru_list(page, lruvec);
Vlastimil Babka01cc2e52014-01-23 15:52:50 -0800289 continue;
Alex Shi13805a82020-12-15 12:34:11 -0800290 } else
Vlastimil Babka01cc2e52014-01-23 15:52:50 -0800291 __munlock_isolation_failed(page);
Yisheng Xie70feee02017-06-02 14:46:43 -0700292 } else {
293 delta_munlocked++;
Vlastimil Babka72255222013-09-11 14:22:29 -0700294 }
Vlastimil Babka01cc2e52014-01-23 15:52:50 -0800295
296 /*
297 * We won't be munlocking this page in the next phase
298 * but we still need to release the follow_page_mask()
299 * pin. We cannot do it under lru_lock however. If it's
300 * the last pin, __page_cache_release() would deadlock.
301 */
302 pagevec_add(&pvec_putback, pvec->pages[i]);
303 pvec->pages[i] = NULL;
Vlastimil Babka72255222013-09-11 14:22:29 -0700304 }
Alex Shi6168d0d2020-12-15 12:34:29 -0800305 if (lruvec) {
306 __mod_zone_page_state(zone, NR_MLOCK, delta_munlocked);
307 unlock_page_lruvec_irq(lruvec);
308 } else if (delta_munlocked) {
309 mod_zone_page_state(zone, NR_MLOCK, delta_munlocked);
310 }
Vlastimil Babka72255222013-09-11 14:22:29 -0700311
Vlastimil Babka3b25df92014-01-02 12:58:44 -0800312 /* Now we can release pins of pages that we are not munlocking */
313 pagevec_release(&pvec_putback);
314
Vlastimil Babka56afe472013-09-11 14:22:32 -0700315 /* Phase 2: page munlock */
Vlastimil Babka72255222013-09-11 14:22:29 -0700316 for (i = 0; i < nr; i++) {
317 struct page *page = pvec->pages[i];
318
319 if (page) {
320 lock_page(page);
Vlastimil Babka56afe472013-09-11 14:22:32 -0700321 if (!__putback_lru_fast_prepare(page, &pvec_putback,
322 &pgrescued)) {
Vlastimil Babka5b409982013-09-11 14:22:33 -0700323 /*
324 * Slow path. We don't want to lose the last
325 * pin before unlock_page()
326 */
327 get_page(page); /* for putback_lru_page() */
Vlastimil Babka56afe472013-09-11 14:22:32 -0700328 __munlock_isolated_page(page);
329 unlock_page(page);
Vlastimil Babka5b409982013-09-11 14:22:33 -0700330 put_page(page); /* from follow_page_mask() */
Vlastimil Babka56afe472013-09-11 14:22:32 -0700331 }
Vlastimil Babka72255222013-09-11 14:22:29 -0700332 }
333 }
Vlastimil Babka56afe472013-09-11 14:22:32 -0700334
Vlastimil Babka5b409982013-09-11 14:22:33 -0700335 /*
336 * Phase 3: page putback for pages that qualified for the fast path
337 * This will also call put_page() to return pin from follow_page_mask()
338 */
Vlastimil Babka56afe472013-09-11 14:22:32 -0700339 if (pagevec_count(&pvec_putback))
340 __putback_lru_fast(&pvec_putback, pgrescued);
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700341}
Vlastimil Babka56afe472013-09-11 14:22:32 -0700342
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700343/*
344 * Fill up pagevec for __munlock_pagevec using pte walk
345 *
346 * The function expects that the struct page corresponding to @start address is
347 * a non-TPH page already pinned and in the @pvec, and that it belongs to @zone.
348 *
349 * The rest of @pvec is filled by subsequent pages within the same pmd and same
350 * zone, as long as the pte's are present and vm_normal_page() succeeds. These
351 * pages also get pinned.
352 *
353 * Returns the address of the next page that should be scanned. This equals
354 * @start + PAGE_SIZE when no page could be added by the pte walk.
355 */
356static unsigned long __munlock_pagevec_fill(struct pagevec *pvec,
Joonsoo Kim9472f232017-09-08 16:12:59 -0700357 struct vm_area_struct *vma, struct zone *zone,
358 unsigned long start, unsigned long end)
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700359{
360 pte_t *pte;
361 spinlock_t *ptl;
362
363 /*
364 * Initialize pte walk starting at the already pinned page where we
Vlastimil Babkaeadb41a2013-09-30 13:45:18 -0700365 * are sure that there is a pte, as it was pinned under the same
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700366 * mmap_lock write op.
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700367 */
368 pte = get_locked_pte(vma->vm_mm, start, &ptl);
Vlastimil Babkaeadb41a2013-09-30 13:45:18 -0700369 /* Make sure we do not cross the page table boundary */
370 end = pgd_addr_end(start, end);
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +0300371 end = p4d_addr_end(start, end);
Vlastimil Babkaeadb41a2013-09-30 13:45:18 -0700372 end = pud_addr_end(start, end);
373 end = pmd_addr_end(start, end);
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700374
375 /* The page next to the pinned page is the first we will try to get */
376 start += PAGE_SIZE;
377 while (start < end) {
378 struct page *page = NULL;
379 pte++;
380 if (pte_present(*pte))
381 page = vm_normal_page(vma, start, *pte);
382 /*
383 * Break if page could not be obtained or the page's node+zone does not
384 * match
385 */
Joonsoo Kim9472f232017-09-08 16:12:59 -0700386 if (!page || page_zone(page) != zone)
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700387 break;
388
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800389 /*
390 * Do not use pagevec for PTE-mapped THP,
391 * munlock_vma_pages_range() will handle them.
392 */
393 if (PageTransCompound(page))
394 break;
395
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700396 get_page(page);
397 /*
398 * Increase the address that will be returned *before* the
399 * eventual break due to pvec becoming full by adding the page
400 */
401 start += PAGE_SIZE;
402 if (pagevec_add(pvec, page) == 0)
403 break;
404 }
405 pte_unmap_unlock(pte, ptl);
406 return start;
Vlastimil Babka72255222013-09-11 14:22:29 -0700407}
408
409/*
Rik van Rielba470de2008-10-18 20:26:50 -0700410 * munlock_vma_pages_range() - munlock all pages in the vma range.'
411 * @vma - vma containing range to be munlock()ed.
412 * @start - start address in @vma of the range
413 * @end - end of range in @vma.
414 *
415 * For mremap(), munmap() and exit().
416 *
417 * Called with @vma VM_LOCKED.
418 *
419 * Returns with VM_LOCKED cleared. Callers must be prepared to
420 * deal with this.
421 *
422 * We don't save and restore VM_LOCKED here because pages are
423 * still on lru. In unmap path, pages might be scanned by reclaim
Alistair Popplecd627342021-06-30 18:54:12 -0700424 * and re-mlocked by page_mlock/try_to_unmap before we unmap and
Rik van Rielba470de2008-10-18 20:26:50 -0700425 * free them. This will result in freeing mlocked pages.
Nick Pigginb291f002008-10-18 20:26:44 -0700426 */
Rik van Rielba470de2008-10-18 20:26:50 -0700427void munlock_vma_pages_range(struct vm_area_struct *vma,
Hugh Dickins408e82b2009-09-21 17:03:23 -0700428 unsigned long start, unsigned long end)
Nick Pigginb291f002008-10-18 20:26:44 -0700429{
Eric B Munsonde60f5f2015-11-05 18:51:36 -0800430 vma->vm_flags &= VM_LOCKED_CLEAR_MASK;
Hugh Dickins408e82b2009-09-21 17:03:23 -0700431
Michel Lespinasseff6a6da2013-02-27 17:02:44 -0800432 while (start < end) {
Alexey Klimovab7a5af2016-01-14 15:18:24 -0800433 struct page *page;
Kirill A. Shutemov6ebb4a12017-03-09 16:17:23 -0800434 unsigned int page_mask = 0;
Vlastimil Babkac424be12014-01-02 12:58:43 -0800435 unsigned long page_increm;
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700436 struct pagevec pvec;
437 struct zone *zone;
Michel Lespinasseff6a6da2013-02-27 17:02:44 -0800438
Mel Gorman86679822017-11-15 17:37:52 -0800439 pagevec_init(&pvec);
Hugh Dickins6e919712009-09-21 17:03:32 -0700440 /*
441 * Although FOLL_DUMP is intended for get_dump_page(),
442 * it just so happens that its special treatment of the
443 * ZERO_PAGE (returning an error instead of doing get_page)
444 * suits munlock very well (and if somehow an abnormal page
445 * has sneaked into the range, we won't oops here: great).
446 */
Kirill A. Shutemov6ebb4a12017-03-09 16:17:23 -0800447 page = follow_page(vma, start, FOLL_GET | FOLL_DUMP);
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700448
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800449 if (page && !IS_ERR(page)) {
450 if (PageTransTail(page)) {
451 VM_BUG_ON_PAGE(PageMlocked(page), page);
452 put_page(page); /* follow_page_mask() */
453 } else if (PageTransHuge(page)) {
454 lock_page(page);
455 /*
456 * Any THP page found by follow_page_mask() may
457 * have gotten split before reaching
Kirill A. Shutemov6ebb4a12017-03-09 16:17:23 -0800458 * munlock_vma_page(), so we need to compute
459 * the page_mask here instead.
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800460 */
461 page_mask = munlock_vma_page(page);
462 unlock_page(page);
463 put_page(page); /* follow_page_mask() */
464 } else {
465 /*
466 * Non-huge pages are handled in batches via
467 * pagevec. The pin from follow_page_mask()
468 * prevents them from collapsing by THP.
469 */
470 pagevec_add(&pvec, page);
471 zone = page_zone(page);
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700472
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800473 /*
474 * Try to fill the rest of pagevec using fast
475 * pte walk. This will also update start to
476 * the next page to process. Then munlock the
477 * pagevec.
478 */
479 start = __munlock_pagevec_fill(&pvec, vma,
Joonsoo Kim9472f232017-09-08 16:12:59 -0700480 zone, start, end);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -0800481 __munlock_pagevec(&pvec, zone);
482 goto next;
483 }
Hugh Dickins408e82b2009-09-21 17:03:23 -0700484 }
Vlastimil Babkac424be12014-01-02 12:58:43 -0800485 page_increm = 1 + page_mask;
Michel Lespinasseff6a6da2013-02-27 17:02:44 -0800486 start += page_increm * PAGE_SIZE;
Vlastimil Babka7a8010c2013-09-11 14:22:35 -0700487next:
Hugh Dickins408e82b2009-09-21 17:03:23 -0700488 cond_resched();
489 }
Nick Pigginb291f002008-10-18 20:26:44 -0700490}
491
492/*
493 * mlock_fixup - handle mlock[all]/munlock[all] requests.
494 *
495 * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
496 * munlock is a no-op. However, for some special vmas, we go ahead and
Michel Lespinassecea10a12013-02-22 16:32:44 -0800497 * populate the ptes.
Nick Pigginb291f002008-10-18 20:26:44 -0700498 *
499 * For vmas that pass the filters, merge/split as appropriate.
500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900502 unsigned long start, unsigned long end, vm_flags_t newflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Nick Pigginb291f002008-10-18 20:26:44 -0700504 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 pgoff_t pgoff;
Nick Pigginb291f002008-10-18 20:26:44 -0700506 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int ret = 0;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900508 int lock = !!(newflags & VM_LOCKED);
Simon Guob155b4f2016-10-07 16:59:40 -0700509 vm_flags_t old_flags = vma->vm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Michel Lespinassefed067d2011-01-13 15:46:10 -0800511 if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
Dave Jiange1fb4a02018-08-17 15:43:40 -0700512 is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm) ||
Mike Rapoport1507f512021-07-07 18:08:03 -0700513 vma_is_dax(vma) || vma_is_secretmem(vma))
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800514 /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
515 goto out;
Nick Pigginb291f002008-10-18 20:26:44 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
518 *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
Andrea Arcangeli19a809a2015-09-04 15:46:24 -0700519 vma->vm_file, pgoff, vma_policy(vma),
Suren Baghdasaryan0fd37222022-03-04 20:28:51 -0800520 vma->vm_userfaultfd_ctx, anon_vma_name(vma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (*prev) {
522 vma = *prev;
523 goto success;
524 }
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (start != vma->vm_start) {
527 ret = split_vma(mm, vma, start, 1);
528 if (ret)
529 goto out;
530 }
531
532 if (end != vma->vm_end) {
533 ret = split_vma(mm, vma, end, 0);
534 if (ret)
535 goto out;
536 }
537
538success:
539 /*
Nick Pigginb291f002008-10-18 20:26:44 -0700540 * Keep track of amount of locked VM.
541 */
542 nr_pages = (end - start) >> PAGE_SHIFT;
543 if (!lock)
544 nr_pages = -nr_pages;
Simon Guob155b4f2016-10-07 16:59:40 -0700545 else if (old_flags & VM_LOCKED)
546 nr_pages = 0;
Nick Pigginb291f002008-10-18 20:26:44 -0700547 mm->locked_vm += nr_pages;
548
549 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700550 * vm_flags is protected by the mmap_lock held in write mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * It's okay if try_to_unmap_one unmaps a page just after we
Kirill A. Shutemovfc05f562015-04-14 15:44:39 -0700552 * set VM_LOCKED, populate_vma_page_range will bring it back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Michel Lespinassefed067d2011-01-13 15:46:10 -0800555 if (lock)
Kalesh Singhd83231e2024-04-22 14:24:59 -0700556 vma->vm_flags = vma_pad_fixup_flags(vma, newflags);
Michel Lespinassefed067d2011-01-13 15:46:10 -0800557 else
Hugh Dickins408e82b2009-09-21 17:03:23 -0700558 munlock_vma_pages_range(vma, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560out:
Nick Pigginb291f002008-10-18 20:26:44 -0700561 *prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return ret;
563}
564
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800565static int apply_vma_lock_flags(unsigned long start, size_t len,
566 vm_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 unsigned long nstart, end, tmp;
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -0700569 struct vm_area_struct *vma, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int error;
571
Alexander Kuleshov8fd9e482015-11-05 18:46:49 -0800572 VM_BUG_ON(offset_in_page(start));
Michel Lespinassefed067d2011-01-13 15:46:10 -0800573 VM_BUG_ON(len != PAGE_ALIGN(len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 end = start + len;
575 if (end < start)
576 return -EINVAL;
577 if (end == start)
578 return 0;
Linus Torvalds097d5912012-03-06 18:23:36 -0800579 vma = find_vma(current->mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!vma || vma->vm_start > start)
581 return -ENOMEM;
582
Linus Torvalds097d5912012-03-06 18:23:36 -0800583 prev = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (start > vma->vm_start)
585 prev = vma;
586
587 for (nstart = start ; ; ) {
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800588 vm_flags_t newflags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800589
590 newflags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 tmp = vma->vm_end;
594 if (tmp > end)
595 tmp = end;
Jianxiong Pan07622f02022-09-22 17:25:35 +0800596 #ifdef CONFIG_AMLOGIC_PIN_LOCKED_FILE
597 reset_page_vma_flags(vma, flags);
598 #endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
600 if (error)
601 break;
602 nstart = tmp;
603 if (nstart < prev->vm_end)
604 nstart = prev->vm_end;
605 if (nstart >= end)
606 break;
607
608 vma = prev->vm_next;
609 if (!vma || vma->vm_start != nstart) {
610 error = -ENOMEM;
611 break;
612 }
613 }
614 return error;
615}
616
Simon Guo0cf2f6f2016-10-07 16:59:36 -0700617/*
618 * Go through vma areas and sum size of mlocked
619 * vma pages, as return value.
620 * Note deferred memory locking case(mlock2(,,MLOCK_ONFAULT)
621 * is also counted.
622 * Return value: previously mlocked page counts
623 */
swkhack0874bb42019-06-13 15:56:08 -0700624static unsigned long count_mm_mlocked_page_nr(struct mm_struct *mm,
Simon Guo0cf2f6f2016-10-07 16:59:36 -0700625 unsigned long start, size_t len)
626{
627 struct vm_area_struct *vma;
swkhack0874bb42019-06-13 15:56:08 -0700628 unsigned long count = 0;
Simon Guo0cf2f6f2016-10-07 16:59:36 -0700629
630 if (mm == NULL)
631 mm = current->mm;
632
633 vma = find_vma(mm, start);
634 if (vma == NULL)
Miaohe Lin48b03ee2021-02-25 17:17:49 -0800635 return 0;
Simon Guo0cf2f6f2016-10-07 16:59:36 -0700636
637 for (; vma ; vma = vma->vm_next) {
638 if (start >= vma->vm_end)
639 continue;
640 if (start + len <= vma->vm_start)
641 break;
642 if (vma->vm_flags & VM_LOCKED) {
643 if (start > vma->vm_start)
644 count -= (start - vma->vm_start);
645 if (start + len < vma->vm_end) {
646 count += start + len - vma->vm_start;
647 break;
648 }
649 count += vma->vm_end - vma->vm_start;
650 }
651 }
652
653 return count >> PAGE_SHIFT;
654}
655
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700656static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 unsigned long locked;
659 unsigned long lock_limit;
660 int error = -ENOMEM;
661
Andrey Konovalov057d33892019-09-25 16:48:30 -0700662 start = untagged_addr(start);
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!can_do_mlock())
665 return -EPERM;
666
Alexander Kuleshov8fd9e482015-11-05 18:46:49 -0800667 len = PAGE_ALIGN(len + (offset_in_page(start)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 start &= PAGE_MASK;
669
Jiri Slaby59e99e52010-03-05 13:41:44 -0800670 lock_limit = rlimit(RLIMIT_MEMLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 lock_limit >>= PAGE_SHIFT;
Davidlohr Bueso1f1cd702014-01-21 15:49:16 -0800672 locked = len >> PAGE_SHIFT;
673
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700674 if (mmap_write_lock_killable(current->mm))
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700675 return -EINTR;
Davidlohr Bueso1f1cd702014-01-21 15:49:16 -0800676
677 locked += current->mm->locked_vm;
Simon Guo0cf2f6f2016-10-07 16:59:36 -0700678 if ((locked > lock_limit) && (!capable(CAP_IPC_LOCK))) {
679 /*
680 * It is possible that the regions requested intersect with
681 * previously mlocked areas, that part area in "mm->locked_vm"
682 * should not be counted to new mlock increment count. So check
683 * and adjust locked count if necessary.
684 */
685 locked -= count_mm_mlocked_page_nr(current->mm,
686 start, len);
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /* check against resource limits */
690 if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800691 error = apply_vma_lock_flags(start, len, flags);
Davidlohr Bueso1f1cd702014-01-21 15:49:16 -0800692
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700693 mmap_write_unlock(current->mm);
Kirill A. Shutemovc5612592015-04-14 15:44:42 -0700694 if (error)
695 return error;
696
Jianxiong Pan07622f02022-09-22 17:25:35 +0800697#ifndef CONFIG_AMLOGIC_PIN_LOCKED_FILE_V2
Kirill A. Shutemovc5612592015-04-14 15:44:42 -0700698 error = __mm_populate(start, len, 0);
699 if (error)
700 return __mlock_posix_error_return(error);
Jianxiong Pan07622f02022-09-22 17:25:35 +0800701#endif
Kirill A. Shutemovc5612592015-04-14 15:44:42 -0700702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800705SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
706{
707 return do_mlock(start, len, VM_LOCKED);
708}
709
Eric B Munsona8ca5d02015-11-05 18:51:33 -0800710SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags)
711{
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800712 vm_flags_t vm_flags = VM_LOCKED;
713
714 if (flags & ~MLOCK_ONFAULT)
Eric B Munsona8ca5d02015-11-05 18:51:33 -0800715 return -EINVAL;
716
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800717 if (flags & MLOCK_ONFAULT)
718 vm_flags |= VM_LOCKONFAULT;
719
720 return do_mlock(start, len, vm_flags);
Eric B Munsona8ca5d02015-11-05 18:51:33 -0800721}
722
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100723SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 int ret;
726
Andrey Konovalov057d33892019-09-25 16:48:30 -0700727 start = untagged_addr(start);
728
Alexander Kuleshov8fd9e482015-11-05 18:46:49 -0800729 len = PAGE_ALIGN(len + (offset_in_page(start)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 start &= PAGE_MASK;
Davidlohr Bueso1f1cd702014-01-21 15:49:16 -0800731
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700732 if (mmap_write_lock_killable(current->mm))
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700733 return -EINTR;
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800734 ret = apply_vma_lock_flags(start, len, 0);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700735 mmap_write_unlock(current->mm);
Davidlohr Bueso1f1cd702014-01-21 15:49:16 -0800736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return ret;
738}
739
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800740/*
741 * Take the MCL_* flags passed into mlockall (or 0 if called from munlockall)
742 * and translate into the appropriate modifications to mm->def_flags and/or the
743 * flags for all current VMAs.
744 *
745 * There are a couple of subtleties with this. If mlockall() is called multiple
746 * times with different flags, the values do not necessarily stack. If mlockall
747 * is called once including the MCL_FUTURE flag and then a second time without
748 * it, VM_LOCKED and VM_LOCKONFAULT will be cleared from mm->def_flags.
749 */
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800750static int apply_mlockall_flags(int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -0700752 struct vm_area_struct *vma, *prev = NULL;
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800753 vm_flags_t to_add = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800755 current->mm->def_flags &= VM_LOCKED_CLEAR_MASK;
756 if (flags & MCL_FUTURE) {
Michel Lespinasse09a9f1d2013-03-28 16:26:23 -0700757 current->mm->def_flags |= VM_LOCKED;
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800758
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800759 if (flags & MCL_ONFAULT)
760 current->mm->def_flags |= VM_LOCKONFAULT;
761
762 if (!(flags & MCL_CURRENT))
763 goto out;
764 }
765
766 if (flags & MCL_CURRENT) {
767 to_add |= VM_LOCKED;
768 if (flags & MCL_ONFAULT)
769 to_add |= VM_LOCKONFAULT;
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900773 vm_flags_t newflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Eric B Munsonb0f205c2015-11-05 18:51:39 -0800775 newflags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
776 newflags |= to_add;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /* Ignore errors */
779 mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
Paul E. McKenney50d4fb72017-10-24 08:22:18 -0700780 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782out:
783 return 0;
784}
785
Heiko Carstens3480b252009-01-14 14:14:16 +0100786SYSCALL_DEFINE1(mlockall, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 unsigned long lock_limit;
Alexey Klimov86d2adcc2015-11-05 18:46:00 -0800789 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Potyra, Stefandedca632019-06-13 15:55:55 -0700791 if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT)) ||
792 flags == MCL_ONFAULT)
Alexey Klimov86d2adcc2015-11-05 18:46:00 -0800793 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!can_do_mlock())
Alexey Klimov86d2adcc2015-11-05 18:46:00 -0800796 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Jiri Slaby59e99e52010-03-05 13:41:44 -0800798 lock_limit = rlimit(RLIMIT_MEMLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 lock_limit >>= PAGE_SHIFT;
800
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700801 if (mmap_write_lock_killable(current->mm))
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700802 return -EINTR;
Davidlohr Bueso1f1cd702014-01-21 15:49:16 -0800803
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700804 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
806 capable(CAP_IPC_LOCK))
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800807 ret = apply_mlockall_flags(flags);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700808 mmap_write_unlock(current->mm);
Michel Lespinassebebeb3d2013-02-22 16:32:37 -0800809 if (!ret && (flags & MCL_CURRENT))
810 mm_populate(0, TASK_SIZE);
Alexey Klimov86d2adcc2015-11-05 18:46:00 -0800811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return ret;
813}
814
Heiko Carstens3480b252009-01-14 14:14:16 +0100815SYSCALL_DEFINE0(munlockall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 int ret;
818
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700819 if (mmap_write_lock_killable(current->mm))
Michal Hockodc0ef0d2016-05-23 16:25:27 -0700820 return -EINTR;
Eric B Munson1aab92ec2015-11-05 18:51:29 -0800821 ret = apply_mlockall_flags(0);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700822 mmap_write_unlock(current->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return ret;
824}
825
826/*
827 * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
828 * shm segments) get accounted against the user_struct instead.
829 */
830static DEFINE_SPINLOCK(shmlock_user_lock);
831
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200832int user_shm_lock(size_t size, struct ucounts *ucounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 unsigned long lock_limit, locked;
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200835 long memlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int allowed = 0;
837
838 locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Jiri Slaby59e99e52010-03-05 13:41:44 -0800839 lock_limit = rlimit(RLIMIT_MEMLOCK);
Miaohe Lin025a7cc2022-03-22 16:09:18 +0800840 if (lock_limit != RLIM_INFINITY)
841 lock_limit >>= PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 spin_lock(&shmlock_user_lock);
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200843 memlock = inc_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
844
Miaohe Lin025a7cc2022-03-22 16:09:18 +0800845 if ((memlock == LONG_MAX || memlock > lock_limit) && !capable(CAP_IPC_LOCK)) {
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200846 dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto out;
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200848 }
849 if (!get_ucounts(ucounts)) {
850 dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked);
Miaohe Lin954c78e2022-03-22 14:44:56 -0700851 allowed = 0;
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200852 goto out;
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 allowed = 1;
855out:
856 spin_unlock(&shmlock_user_lock);
857 return allowed;
858}
859
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200860void user_shm_unlock(size_t size, struct ucounts *ucounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 spin_lock(&shmlock_user_lock);
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200863 dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 spin_unlock(&shmlock_user_lock);
Alexey Gladkovd7c9e992021-04-22 14:27:14 +0200865 put_ucounts(ucounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}