blob: de73215928978fe096b789075385e914fdb93105 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/mlock.c
3 *
4 * (C) Copyright 1995 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
6 */
7
Randy.Dunlapc59ede72006-01-11 12:17:46 -08008#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mman.h>
10#include <linux/mm.h>
Nick Pigginb291f002008-10-18 20:26:44 -070011#include <linux/swap.h>
12#include <linux/swapops.h>
13#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mempolicy.h>
15#include <linux/syscalls.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040017#include <linux/export.h>
Nick Pigginb291f002008-10-18 20:26:44 -070018#include <linux/rmap.h>
19#include <linux/mmzone.h>
20#include <linux/hugetlb.h>
21
22#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040024int can_do_mlock(void)
25{
26 if (capable(CAP_IPC_LOCK))
27 return 1;
Jiri Slaby59e99e52010-03-05 13:41:44 -080028 if (rlimit(RLIMIT_MEMLOCK) != 0)
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029 return 1;
30 return 0;
31}
32EXPORT_SYMBOL(can_do_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Nick Pigginb291f002008-10-18 20:26:44 -070034/*
35 * Mlocked pages are marked with PageMlocked() flag for efficient testing
36 * in vmscan and, possibly, the fault path; and to support semi-accurate
37 * statistics.
38 *
39 * An mlocked page [PageMlocked(page)] is unevictable. As such, it will
40 * be placed on the LRU "unevictable" list, rather than the [in]active lists.
41 * The unevictable list is an LRU sibling list to the [in]active lists.
42 * PageUnevictable is set to indicate the unevictable state.
43 *
44 * When lazy mlocking via vmscan, it is important to ensure that the
45 * vma's VM_LOCKED status is not concurrently being modified, otherwise we
46 * may have mlocked a page that is being munlocked. So lazy mlock must take
47 * the mmap_sem for read, and verify that the vma really is locked
48 * (see mm/rmap.c).
49 */
50
51/*
52 * LRU accounting for clear_page_mlock()
53 */
Hugh Dickinse6c509f2012-10-08 16:33:19 -070054void clear_page_mlock(struct page *page)
Nick Pigginb291f002008-10-18 20:26:44 -070055{
Hugh Dickinse6c509f2012-10-08 16:33:19 -070056 if (!TestClearPageMlocked(page))
Nick Pigginb291f002008-10-18 20:26:44 -070057 return;
Nick Pigginb291f002008-10-18 20:26:44 -070058
Nick Piggin5344b7e2008-10-18 20:26:51 -070059 dec_zone_page_state(page, NR_MLOCK);
60 count_vm_event(UNEVICTABLE_PGCLEARED);
Nick Pigginb291f002008-10-18 20:26:44 -070061 if (!isolate_lru_page(page)) {
62 putback_lru_page(page);
63 } else {
64 /*
KOSAKI Motohiro8891d6d2008-11-12 13:26:53 -080065 * We lost the race. the page already moved to evictable list.
Nick Pigginb291f002008-10-18 20:26:44 -070066 */
KOSAKI Motohiro8891d6d2008-11-12 13:26:53 -080067 if (PageUnevictable(page))
Nick Piggin5344b7e2008-10-18 20:26:51 -070068 count_vm_event(UNEVICTABLE_PGSTRANDED);
Nick Pigginb291f002008-10-18 20:26:44 -070069 }
70}
71
72/*
73 * Mark page as mlocked if not already.
74 * If page on LRU, isolate and putback to move to unevictable list.
75 */
76void mlock_vma_page(struct page *page)
77{
78 BUG_ON(!PageLocked(page));
79
Nick Piggin5344b7e2008-10-18 20:26:51 -070080 if (!TestSetPageMlocked(page)) {
81 inc_zone_page_state(page, NR_MLOCK);
82 count_vm_event(UNEVICTABLE_PGMLOCKED);
83 if (!isolate_lru_page(page))
84 putback_lru_page(page);
85 }
Nick Pigginb291f002008-10-18 20:26:44 -070086}
87
Lee Schermerhorn6927c1d2009-12-14 17:59:55 -080088/**
89 * munlock_vma_page - munlock a vma page
90 * @page - page to be unlocked
Nick Pigginb291f002008-10-18 20:26:44 -070091 *
Lee Schermerhorn6927c1d2009-12-14 17:59:55 -080092 * called from munlock()/munmap() path with page supposedly on the LRU.
93 * When we munlock a page, because the vma where we found the page is being
94 * munlock()ed or munmap()ed, we want to check whether other vmas hold the
95 * page locked so that we can leave it on the unevictable lru list and not
96 * bother vmscan with it. However, to walk the page's rmap list in
97 * try_to_munlock() we must isolate the page from the LRU. If some other
98 * task has removed the page from the LRU, we won't be able to do that.
99 * So we clear the PageMlocked as we might not get another chance. If we
100 * can't isolate the page, we leave it for putback_lru_page() and vmscan
101 * [page_referenced()/try_to_unmap()] to deal with.
Nick Pigginb291f002008-10-18 20:26:44 -0700102 */
Hugh Dickins73848b42009-12-14 17:59:22 -0800103void munlock_vma_page(struct page *page)
Nick Pigginb291f002008-10-18 20:26:44 -0700104{
105 BUG_ON(!PageLocked(page));
106
Nick Piggin5344b7e2008-10-18 20:26:51 -0700107 if (TestClearPageMlocked(page)) {
108 dec_zone_page_state(page, NR_MLOCK);
109 if (!isolate_lru_page(page)) {
Hugh Dickins3d470fc2011-10-31 17:09:43 -0700110 int ret = SWAP_AGAIN;
111
112 /*
113 * Optimization: if the page was mapped just once,
114 * that's our mapping and we don't need to check all the
115 * other vmas.
116 */
117 if (page_mapcount(page) > 1)
118 ret = try_to_munlock(page);
Nick Piggin5344b7e2008-10-18 20:26:51 -0700119 /*
120 * did try_to_unlock() succeed or punt?
121 */
Hugh Dickins53f79ac2009-12-14 17:58:58 -0800122 if (ret != SWAP_MLOCK)
Nick Piggin5344b7e2008-10-18 20:26:51 -0700123 count_vm_event(UNEVICTABLE_PGMUNLOCKED);
124
125 putback_lru_page(page);
126 } else {
127 /*
Lee Schermerhorn6927c1d2009-12-14 17:59:55 -0800128 * Some other task has removed the page from the LRU.
129 * putback_lru_page() will take care of removing the
130 * page from the unevictable list, if necessary.
131 * vmscan [page_referenced()] will move the page back
132 * to the unevictable list if some other vma has it
133 * mlocked.
Nick Piggin5344b7e2008-10-18 20:26:51 -0700134 */
135 if (PageUnevictable(page))
136 count_vm_event(UNEVICTABLE_PGSTRANDED);
137 else
138 count_vm_event(UNEVICTABLE_PGMUNLOCKED);
139 }
Nick Pigginb291f002008-10-18 20:26:44 -0700140 }
141}
142
Rik van Rielba470de2008-10-18 20:26:50 -0700143/**
Hugh Dickins408e82b2009-09-21 17:03:23 -0700144 * __mlock_vma_pages_range() - mlock a range of pages in the vma.
Rik van Rielba470de2008-10-18 20:26:50 -0700145 * @vma: target vma
146 * @start: start address
147 * @end: end address
Nick Pigginb291f002008-10-18 20:26:44 -0700148 *
Hugh Dickins408e82b2009-09-21 17:03:23 -0700149 * This takes care of making the pages present too.
Nick Pigginb291f002008-10-18 20:26:44 -0700150 *
Rik van Rielba470de2008-10-18 20:26:50 -0700151 * return 0 on success, negative error code on error.
152 *
153 * vma->vm_mm->mmap_sem must be held for at least read.
Nick Pigginb291f002008-10-18 20:26:44 -0700154 */
Rik van Rielba470de2008-10-18 20:26:50 -0700155static long __mlock_vma_pages_range(struct vm_area_struct *vma,
Michel Lespinasse53a77062011-01-13 15:46:14 -0800156 unsigned long start, unsigned long end,
157 int *nonblocking)
Nick Pigginb291f002008-10-18 20:26:44 -0700158{
159 struct mm_struct *mm = vma->vm_mm;
160 unsigned long addr = start;
Nick Pigginb291f002008-10-18 20:26:44 -0700161 int nr_pages = (end - start) / PAGE_SIZE;
Hugh Dickins408e82b2009-09-21 17:03:23 -0700162 int gup_flags;
Nick Pigginb291f002008-10-18 20:26:44 -0700163
Rik van Rielba470de2008-10-18 20:26:50 -0700164 VM_BUG_ON(start & ~PAGE_MASK);
165 VM_BUG_ON(end & ~PAGE_MASK);
166 VM_BUG_ON(start < vma->vm_start);
167 VM_BUG_ON(end > vma->vm_end);
Hugh Dickins408e82b2009-09-21 17:03:23 -0700168 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
Rik van Rielba470de2008-10-18 20:26:50 -0700169
Linus Torvaldsa1fde082011-05-04 21:30:28 -0700170 gup_flags = FOLL_TOUCH | FOLL_MLOCK;
Michel Lespinasse5ecfda02011-01-13 15:46:09 -0800171 /*
172 * We want to touch writable mappings with a write fault in order
173 * to break COW, except for shared mappings because these don't COW
174 * and we would not want to dirty them for nothing.
175 */
176 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
Hugh Dickins58fa8792009-09-21 17:03:31 -0700177 gup_flags |= FOLL_WRITE;
Nick Pigginb291f002008-10-18 20:26:44 -0700178
Michel Lespinassefdf4c582011-01-31 17:03:41 -0800179 /*
180 * We want mlock to succeed for regions that have any permissions
181 * other than PROT_NONE.
182 */
183 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
184 gup_flags |= FOLL_FORCE;
185
Michel Lespinasse53a77062011-01-13 15:46:14 -0800186 return __get_user_pages(current, mm, addr, nr_pages, gup_flags,
187 NULL, NULL, nonblocking);
Lee Schermerhorn9978ad52008-10-18 20:26:56 -0700188}
189
190/*
191 * convert get_user_pages() return value to posix mlock() error
192 */
193static int __mlock_posix_error_return(long retval)
194{
195 if (retval == -EFAULT)
196 retval = -ENOMEM;
197 else if (retval == -ENOMEM)
198 retval = -EAGAIN;
199 return retval;
Nick Pigginb291f002008-10-18 20:26:44 -0700200}
201
Rik van Rielba470de2008-10-18 20:26:50 -0700202/**
203 * mlock_vma_pages_range() - mlock pages in specified vma range.
204 * @vma - the vma containing the specfied address range
205 * @start - starting address in @vma to mlock
206 * @end - end address [+1] in @vma to mlock
207 *
208 * For mmap()/mremap()/expansion of mlocked vma.
209 *
210 * return 0 on success for "normal" vmas.
211 *
212 * return number of pages [> 0] to be removed from locked_vm on success
213 * of "special" vmas.
Nick Pigginb291f002008-10-18 20:26:44 -0700214 */
Rik van Rielba470de2008-10-18 20:26:50 -0700215long mlock_vma_pages_range(struct vm_area_struct *vma,
Nick Pigginb291f002008-10-18 20:26:44 -0700216 unsigned long start, unsigned long end)
217{
218 int nr_pages = (end - start) / PAGE_SIZE;
219 BUG_ON(!(vma->vm_flags & VM_LOCKED));
220
221 /*
222 * filter unlockable vmas
223 */
224 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
225 goto no_mlock;
226
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700227 if (!((vma->vm_flags & VM_DONTEXPAND) ||
Nick Pigginb291f002008-10-18 20:26:44 -0700228 is_vm_hugetlb_page(vma) ||
Stephen Wilson31db58b2011-03-13 15:49:15 -0400229 vma == get_gate_vma(current->mm))) {
Rik van Rielba470de2008-10-18 20:26:50 -0700230
Michel Lespinasse53a77062011-01-13 15:46:14 -0800231 __mlock_vma_pages_range(vma, start, end, NULL);
Hugh Dickinsd5b56232009-02-08 20:56:58 +0000232
233 /* Hide errors from mmap() and other callers */
234 return 0;
Lee Schermerhorn8edb08c2008-10-18 20:26:49 -0700235 }
Nick Pigginb291f002008-10-18 20:26:44 -0700236
237 /*
238 * User mapped kernel pages or huge pages:
239 * make these pages present to populate the ptes, but
240 * fall thru' to reset VM_LOCKED--no need to unlock, and
241 * return nr_pages so these don't get counted against task's
242 * locked limit. huge pages are already counted against
243 * locked vm limit.
244 */
245 make_pages_present(start, end);
246
247no_mlock:
248 vma->vm_flags &= ~VM_LOCKED; /* and don't come back! */
Rik van Rielba470de2008-10-18 20:26:50 -0700249 return nr_pages; /* error or pages NOT mlocked */
Nick Pigginb291f002008-10-18 20:26:44 -0700250}
251
Nick Pigginb291f002008-10-18 20:26:44 -0700252/*
Rik van Rielba470de2008-10-18 20:26:50 -0700253 * munlock_vma_pages_range() - munlock all pages in the vma range.'
254 * @vma - vma containing range to be munlock()ed.
255 * @start - start address in @vma of the range
256 * @end - end of range in @vma.
257 *
258 * For mremap(), munmap() and exit().
259 *
260 * Called with @vma VM_LOCKED.
261 *
262 * Returns with VM_LOCKED cleared. Callers must be prepared to
263 * deal with this.
264 *
265 * We don't save and restore VM_LOCKED here because pages are
266 * still on lru. In unmap path, pages might be scanned by reclaim
267 * and re-mlocked by try_to_{munlock|unmap} before we unmap and
268 * free them. This will result in freeing mlocked pages.
Nick Pigginb291f002008-10-18 20:26:44 -0700269 */
Rik van Rielba470de2008-10-18 20:26:50 -0700270void munlock_vma_pages_range(struct vm_area_struct *vma,
Hugh Dickins408e82b2009-09-21 17:03:23 -0700271 unsigned long start, unsigned long end)
Nick Pigginb291f002008-10-18 20:26:44 -0700272{
Hugh Dickins408e82b2009-09-21 17:03:23 -0700273 unsigned long addr;
274
275 lru_add_drain();
Nick Pigginb291f002008-10-18 20:26:44 -0700276 vma->vm_flags &= ~VM_LOCKED;
Hugh Dickins408e82b2009-09-21 17:03:23 -0700277
278 for (addr = start; addr < end; addr += PAGE_SIZE) {
Hugh Dickins6e919712009-09-21 17:03:32 -0700279 struct page *page;
280 /*
281 * Although FOLL_DUMP is intended for get_dump_page(),
282 * it just so happens that its special treatment of the
283 * ZERO_PAGE (returning an error instead of doing get_page)
284 * suits munlock very well (and if somehow an abnormal page
285 * has sneaked into the range, we won't oops here: great).
286 */
287 page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
288 if (page && !IS_ERR(page)) {
Hugh Dickins408e82b2009-09-21 17:03:23 -0700289 lock_page(page);
Hugh Dickinse6c509f2012-10-08 16:33:19 -0700290 munlock_vma_page(page);
Hugh Dickins408e82b2009-09-21 17:03:23 -0700291 unlock_page(page);
292 put_page(page);
293 }
294 cond_resched();
295 }
Nick Pigginb291f002008-10-18 20:26:44 -0700296}
297
298/*
299 * mlock_fixup - handle mlock[all]/munlock[all] requests.
300 *
301 * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
302 * munlock is a no-op. However, for some special vmas, we go ahead and
303 * populate the ptes via make_pages_present().
304 *
305 * For vmas that pass the filters, merge/split as appropriate.
306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900308 unsigned long start, unsigned long end, vm_flags_t newflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Nick Pigginb291f002008-10-18 20:26:44 -0700310 struct mm_struct *mm = vma->vm_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 pgoff_t pgoff;
Nick Pigginb291f002008-10-18 20:26:44 -0700312 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int ret = 0;
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900314 int lock = !!(newflags & VM_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Michel Lespinassefed067d2011-01-13 15:46:10 -0800316 if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
Stephen Wilson31db58b2011-03-13 15:49:15 -0400317 is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm))
Nick Pigginb291f002008-10-18 20:26:44 -0700318 goto out; /* don't set VM_LOCKED, don't count */
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
321 *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
322 vma->vm_file, pgoff, vma_policy(vma));
323 if (*prev) {
324 vma = *prev;
325 goto success;
326 }
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (start != vma->vm_start) {
329 ret = split_vma(mm, vma, start, 1);
330 if (ret)
331 goto out;
332 }
333
334 if (end != vma->vm_end) {
335 ret = split_vma(mm, vma, end, 0);
336 if (ret)
337 goto out;
338 }
339
340success:
341 /*
Nick Pigginb291f002008-10-18 20:26:44 -0700342 * Keep track of amount of locked VM.
343 */
344 nr_pages = (end - start) >> PAGE_SHIFT;
345 if (!lock)
346 nr_pages = -nr_pages;
347 mm->locked_vm += nr_pages;
348
349 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * vm_flags is protected by the mmap_sem held in write mode.
351 * It's okay if try_to_unmap_one unmaps a page just after we
Nick Pigginb291f002008-10-18 20:26:44 -0700352 * set VM_LOCKED, __mlock_vma_pages_range will bring it back.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Michel Lespinassefed067d2011-01-13 15:46:10 -0800355 if (lock)
Hugh Dickins408e82b2009-09-21 17:03:23 -0700356 vma->vm_flags = newflags;
Michel Lespinassefed067d2011-01-13 15:46:10 -0800357 else
Hugh Dickins408e82b2009-09-21 17:03:23 -0700358 munlock_vma_pages_range(vma, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360out:
Nick Pigginb291f002008-10-18 20:26:44 -0700361 *prev = vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return ret;
363}
364
365static int do_mlock(unsigned long start, size_t len, int on)
366{
367 unsigned long nstart, end, tmp;
368 struct vm_area_struct * vma, * prev;
369 int error;
370
Michel Lespinassefed067d2011-01-13 15:46:10 -0800371 VM_BUG_ON(start & ~PAGE_MASK);
372 VM_BUG_ON(len != PAGE_ALIGN(len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 end = start + len;
374 if (end < start)
375 return -EINVAL;
376 if (end == start)
377 return 0;
Linus Torvalds097d5912012-03-06 18:23:36 -0800378 vma = find_vma(current->mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!vma || vma->vm_start > start)
380 return -ENOMEM;
381
Linus Torvalds097d5912012-03-06 18:23:36 -0800382 prev = vma->vm_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (start > vma->vm_start)
384 prev = vma;
385
386 for (nstart = start ; ; ) {
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900387 vm_flags_t newflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
390
391 newflags = vma->vm_flags | VM_LOCKED;
392 if (!on)
393 newflags &= ~VM_LOCKED;
394
395 tmp = vma->vm_end;
396 if (tmp > end)
397 tmp = end;
398 error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
399 if (error)
400 break;
401 nstart = tmp;
402 if (nstart < prev->vm_end)
403 nstart = prev->vm_end;
404 if (nstart >= end)
405 break;
406
407 vma = prev->vm_next;
408 if (!vma || vma->vm_start != nstart) {
409 error = -ENOMEM;
410 break;
411 }
412 }
413 return error;
414}
415
Michel Lespinassefed067d2011-01-13 15:46:10 -0800416static int do_mlock_pages(unsigned long start, size_t len, int ignore_errors)
417{
418 struct mm_struct *mm = current->mm;
419 unsigned long end, nstart, nend;
420 struct vm_area_struct *vma = NULL;
Michel Lespinasse53a77062011-01-13 15:46:14 -0800421 int locked = 0;
Michel Lespinassefed067d2011-01-13 15:46:10 -0800422 int ret = 0;
423
424 VM_BUG_ON(start & ~PAGE_MASK);
425 VM_BUG_ON(len != PAGE_ALIGN(len));
426 end = start + len;
427
Michel Lespinassefed067d2011-01-13 15:46:10 -0800428 for (nstart = start; nstart < end; nstart = nend) {
429 /*
430 * We want to fault in pages for [nstart; end) address range.
431 * Find first corresponding VMA.
432 */
Michel Lespinasse53a77062011-01-13 15:46:14 -0800433 if (!locked) {
434 locked = 1;
435 down_read(&mm->mmap_sem);
Michel Lespinassefed067d2011-01-13 15:46:10 -0800436 vma = find_vma(mm, nstart);
Michel Lespinasse53a77062011-01-13 15:46:14 -0800437 } else if (nstart >= vma->vm_end)
Michel Lespinassefed067d2011-01-13 15:46:10 -0800438 vma = vma->vm_next;
439 if (!vma || vma->vm_start >= end)
440 break;
441 /*
442 * Set [nstart; nend) to intersection of desired address
443 * range with the first VMA. Also, skip undesirable VMA types.
444 */
445 nend = min(end, vma->vm_end);
446 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
447 continue;
448 if (nstart < vma->vm_start)
449 nstart = vma->vm_start;
450 /*
Michel Lespinasse53a77062011-01-13 15:46:14 -0800451 * Now fault in a range of pages. __mlock_vma_pages_range()
452 * double checks the vma flags, so that it won't mlock pages
453 * if the vma was already munlocked.
Michel Lespinassefed067d2011-01-13 15:46:10 -0800454 */
Michel Lespinasse53a77062011-01-13 15:46:14 -0800455 ret = __mlock_vma_pages_range(vma, nstart, nend, &locked);
456 if (ret < 0) {
457 if (ignore_errors) {
458 ret = 0;
459 continue; /* continue at next VMA */
460 }
Michel Lespinasse5fdb2002011-01-13 15:46:12 -0800461 ret = __mlock_posix_error_return(ret);
462 break;
463 }
Michel Lespinasse53a77062011-01-13 15:46:14 -0800464 nend = nstart + ret * PAGE_SIZE;
465 ret = 0;
Michel Lespinassefed067d2011-01-13 15:46:10 -0800466 }
Michel Lespinasse53a77062011-01-13 15:46:14 -0800467 if (locked)
468 up_read(&mm->mmap_sem);
Michel Lespinassefed067d2011-01-13 15:46:10 -0800469 return ret; /* 0 or negative error code */
470}
471
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100472SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 unsigned long locked;
475 unsigned long lock_limit;
476 int error = -ENOMEM;
477
478 if (!can_do_mlock())
479 return -EPERM;
480
KOSAKI Motohiro8891d6d2008-11-12 13:26:53 -0800481 lru_add_drain_all(); /* flush pagevec */
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 down_write(&current->mm->mmap_sem);
484 len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
485 start &= PAGE_MASK;
486
487 locked = len >> PAGE_SHIFT;
488 locked += current->mm->locked_vm;
489
Jiri Slaby59e99e52010-03-05 13:41:44 -0800490 lock_limit = rlimit(RLIMIT_MEMLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 lock_limit >>= PAGE_SHIFT;
492
493 /* check against resource limits */
494 if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
495 error = do_mlock(start, len, 1);
496 up_write(&current->mm->mmap_sem);
Michel Lespinassefed067d2011-01-13 15:46:10 -0800497 if (!error)
498 error = do_mlock_pages(start, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return error;
500}
501
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100502SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 int ret;
505
506 down_write(&current->mm->mmap_sem);
507 len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
508 start &= PAGE_MASK;
509 ret = do_mlock(start, len, 0);
510 up_write(&current->mm->mmap_sem);
511 return ret;
512}
513
514static int do_mlockall(int flags)
515{
516 struct vm_area_struct * vma, * prev = NULL;
517 unsigned int def_flags = 0;
518
519 if (flags & MCL_FUTURE)
520 def_flags = VM_LOCKED;
521 current->mm->def_flags = def_flags;
522 if (flags == MCL_FUTURE)
523 goto out;
524
525 for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
KOSAKI Motohiroca16d142011-05-26 19:16:19 +0900526 vm_flags_t newflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 newflags = vma->vm_flags | VM_LOCKED;
529 if (!(flags & MCL_CURRENT))
530 newflags &= ~VM_LOCKED;
531
532 /* Ignore errors */
533 mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
534 }
535out:
536 return 0;
537}
538
Heiko Carstens3480b252009-01-14 14:14:16 +0100539SYSCALL_DEFINE1(mlockall, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 unsigned long lock_limit;
542 int ret = -EINVAL;
543
544 if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
545 goto out;
546
547 ret = -EPERM;
548 if (!can_do_mlock())
549 goto out;
550
Christoph Lameterdf9d6982011-10-31 17:09:35 -0700551 if (flags & MCL_CURRENT)
552 lru_add_drain_all(); /* flush pagevec */
KOSAKI Motohiro8891d6d2008-11-12 13:26:53 -0800553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 down_write(&current->mm->mmap_sem);
555
Jiri Slaby59e99e52010-03-05 13:41:44 -0800556 lock_limit = rlimit(RLIMIT_MEMLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 lock_limit >>= PAGE_SHIFT;
558
559 ret = -ENOMEM;
560 if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
561 capable(CAP_IPC_LOCK))
562 ret = do_mlockall(flags);
563 up_write(&current->mm->mmap_sem);
Michel Lespinassefed067d2011-01-13 15:46:10 -0800564 if (!ret && (flags & MCL_CURRENT)) {
565 /* Ignore errors */
566 do_mlock_pages(0, TASK_SIZE, 1);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568out:
569 return ret;
570}
571
Heiko Carstens3480b252009-01-14 14:14:16 +0100572SYSCALL_DEFINE0(munlockall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int ret;
575
576 down_write(&current->mm->mmap_sem);
577 ret = do_mlockall(0);
578 up_write(&current->mm->mmap_sem);
579 return ret;
580}
581
582/*
583 * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
584 * shm segments) get accounted against the user_struct instead.
585 */
586static DEFINE_SPINLOCK(shmlock_user_lock);
587
588int user_shm_lock(size_t size, struct user_struct *user)
589{
590 unsigned long lock_limit, locked;
591 int allowed = 0;
592
593 locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Jiri Slaby59e99e52010-03-05 13:41:44 -0800594 lock_limit = rlimit(RLIMIT_MEMLOCK);
Herbert van den Bergh5ed44a42007-07-15 23:38:25 -0700595 if (lock_limit == RLIM_INFINITY)
596 allowed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 lock_limit >>= PAGE_SHIFT;
598 spin_lock(&shmlock_user_lock);
Herbert van den Bergh5ed44a42007-07-15 23:38:25 -0700599 if (!allowed &&
600 locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 goto out;
602 get_uid(user);
603 user->locked_shm += locked;
604 allowed = 1;
605out:
606 spin_unlock(&shmlock_user_lock);
607 return allowed;
608}
609
610void user_shm_unlock(size_t size, struct user_struct *user)
611{
612 spin_lock(&shmlock_user_lock);
613 user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
614 spin_unlock(&shmlock_user_lock);
615 free_uid(user);
616}