]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - mm/mlock.c
mm: munlock: remove unnecessary call to lru_add_drain()
[mirror_ubuntu-zesty-kernel.git] / mm / mlock.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/mlock.c
3 *
4 * (C) Copyright 1995 Linus Torvalds
5 * (C) Copyright 2002 Christoph Hellwig
6 */
7
c59ede7b 8#include <linux/capability.h>
1da177e4
LT
9#include <linux/mman.h>
10#include <linux/mm.h>
b291f000
NP
11#include <linux/swap.h>
12#include <linux/swapops.h>
13#include <linux/pagemap.h>
1da177e4
LT
14#include <linux/mempolicy.h>
15#include <linux/syscalls.h>
e8edc6e0 16#include <linux/sched.h>
b95f1b31 17#include <linux/export.h>
b291f000
NP
18#include <linux/rmap.h>
19#include <linux/mmzone.h>
20#include <linux/hugetlb.h>
21
22#include "internal.h"
1da177e4 23
e8edc6e0
AD
24int can_do_mlock(void)
25{
26 if (capable(CAP_IPC_LOCK))
27 return 1;
59e99e5b 28 if (rlimit(RLIMIT_MEMLOCK) != 0)
e8edc6e0
AD
29 return 1;
30 return 0;
31}
32EXPORT_SYMBOL(can_do_mlock);
1da177e4 33
b291f000
NP
34/*
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 */
e6c509f8 54void clear_page_mlock(struct page *page)
b291f000 55{
e6c509f8 56 if (!TestClearPageMlocked(page))
b291f000 57 return;
b291f000 58
8449d21f
DR
59 mod_zone_page_state(page_zone(page), NR_MLOCK,
60 -hpage_nr_pages(page));
5344b7e6 61 count_vm_event(UNEVICTABLE_PGCLEARED);
b291f000
NP
62 if (!isolate_lru_page(page)) {
63 putback_lru_page(page);
64 } else {
65 /*
8891d6da 66 * We lost the race. the page already moved to evictable list.
b291f000 67 */
8891d6da 68 if (PageUnevictable(page))
5344b7e6 69 count_vm_event(UNEVICTABLE_PGSTRANDED);
b291f000
NP
70 }
71}
72
73/*
74 * Mark page as mlocked if not already.
75 * If page on LRU, isolate and putback to move to unevictable list.
76 */
77void mlock_vma_page(struct page *page)
78{
79 BUG_ON(!PageLocked(page));
80
5344b7e6 81 if (!TestSetPageMlocked(page)) {
8449d21f
DR
82 mod_zone_page_state(page_zone(page), NR_MLOCK,
83 hpage_nr_pages(page));
5344b7e6
NP
84 count_vm_event(UNEVICTABLE_PGMLOCKED);
85 if (!isolate_lru_page(page))
86 putback_lru_page(page);
87 }
b291f000
NP
88}
89
6927c1dd
LS
90/**
91 * munlock_vma_page - munlock a vma page
92 * @page - page to be unlocked
b291f000 93 *
6927c1dd
LS
94 * called from munlock()/munmap() path with page supposedly on the LRU.
95 * When we munlock a page, because the vma where we found the page is being
96 * munlock()ed or munmap()ed, we want to check whether other vmas hold the
97 * page locked so that we can leave it on the unevictable lru list and not
98 * bother vmscan with it. However, to walk the page's rmap list in
99 * try_to_munlock() we must isolate the page from the LRU. If some other
100 * task has removed the page from the LRU, we won't be able to do that.
101 * So we clear the PageMlocked as we might not get another chance. If we
102 * can't isolate the page, we leave it for putback_lru_page() and vmscan
103 * [page_referenced()/try_to_unmap()] to deal with.
b291f000 104 */
ff6a6da6 105unsigned int munlock_vma_page(struct page *page)
b291f000 106{
ff6a6da6
ML
107 unsigned int page_mask = 0;
108
b291f000
NP
109 BUG_ON(!PageLocked(page));
110
5344b7e6 111 if (TestClearPageMlocked(page)) {
ff6a6da6
ML
112 unsigned int nr_pages = hpage_nr_pages(page);
113 mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
114 page_mask = nr_pages - 1;
5344b7e6 115 if (!isolate_lru_page(page)) {
3d470fc3
HD
116 int ret = SWAP_AGAIN;
117
118 /*
119 * Optimization: if the page was mapped just once,
120 * that's our mapping and we don't need to check all the
121 * other vmas.
122 */
123 if (page_mapcount(page) > 1)
124 ret = try_to_munlock(page);
5344b7e6
NP
125 /*
126 * did try_to_unlock() succeed or punt?
127 */
53f79acb 128 if (ret != SWAP_MLOCK)
5344b7e6
NP
129 count_vm_event(UNEVICTABLE_PGMUNLOCKED);
130
131 putback_lru_page(page);
132 } else {
133 /*
6927c1dd
LS
134 * Some other task has removed the page from the LRU.
135 * putback_lru_page() will take care of removing the
136 * page from the unevictable list, if necessary.
137 * vmscan [page_referenced()] will move the page back
138 * to the unevictable list if some other vma has it
139 * mlocked.
5344b7e6
NP
140 */
141 if (PageUnevictable(page))
142 count_vm_event(UNEVICTABLE_PGSTRANDED);
143 else
144 count_vm_event(UNEVICTABLE_PGMUNLOCKED);
145 }
b291f000 146 }
ff6a6da6
ML
147
148 return page_mask;
b291f000
NP
149}
150
ba470de4 151/**
408e82b7 152 * __mlock_vma_pages_range() - mlock a range of pages in the vma.
ba470de4
RR
153 * @vma: target vma
154 * @start: start address
155 * @end: end address
ba470de4 156 *
408e82b7 157 * This takes care of making the pages present too.
b291f000 158 *
ba470de4 159 * return 0 on success, negative error code on error.
b291f000 160 *
ba470de4 161 * vma->vm_mm->mmap_sem must be held for at least read.
b291f000 162 */
cea10a19
ML
163long __mlock_vma_pages_range(struct vm_area_struct *vma,
164 unsigned long start, unsigned long end, int *nonblocking)
b291f000
NP
165{
166 struct mm_struct *mm = vma->vm_mm;
28a35716 167 unsigned long nr_pages = (end - start) / PAGE_SIZE;
408e82b7 168 int gup_flags;
ba470de4
RR
169
170 VM_BUG_ON(start & ~PAGE_MASK);
171 VM_BUG_ON(end & ~PAGE_MASK);
172 VM_BUG_ON(start < vma->vm_start);
173 VM_BUG_ON(end > vma->vm_end);
408e82b7 174 VM_BUG_ON(!rwsem_is_locked(&mm->mmap_sem));
b291f000 175
a1fde08c 176 gup_flags = FOLL_TOUCH | FOLL_MLOCK;
5ecfda04
ML
177 /*
178 * We want to touch writable mappings with a write fault in order
179 * to break COW, except for shared mappings because these don't COW
180 * and we would not want to dirty them for nothing.
181 */
182 if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
58fa879e 183 gup_flags |= FOLL_WRITE;
b291f000 184
fdf4c587
ML
185 /*
186 * We want mlock to succeed for regions that have any permissions
187 * other than PROT_NONE.
188 */
189 if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
190 gup_flags |= FOLL_FORCE;
191
4805b02e
JW
192 /*
193 * We made sure addr is within a VMA, so the following will
194 * not result in a stack expansion that recurses back here.
195 */
ff6a6da6 196 return __get_user_pages(current, mm, start, nr_pages, gup_flags,
53a7706d 197 NULL, NULL, nonblocking);
9978ad58
LS
198}
199
200/*
201 * convert get_user_pages() return value to posix mlock() error
202 */
203static int __mlock_posix_error_return(long retval)
204{
205 if (retval == -EFAULT)
206 retval = -ENOMEM;
207 else if (retval == -ENOMEM)
208 retval = -EAGAIN;
209 return retval;
b291f000
NP
210}
211
b291f000 212/*
ba470de4
RR
213 * munlock_vma_pages_range() - munlock all pages in the vma range.'
214 * @vma - vma containing range to be munlock()ed.
215 * @start - start address in @vma of the range
216 * @end - end of range in @vma.
217 *
218 * For mremap(), munmap() and exit().
219 *
220 * Called with @vma VM_LOCKED.
221 *
222 * Returns with VM_LOCKED cleared. Callers must be prepared to
223 * deal with this.
224 *
225 * We don't save and restore VM_LOCKED here because pages are
226 * still on lru. In unmap path, pages might be scanned by reclaim
227 * and re-mlocked by try_to_{munlock|unmap} before we unmap and
228 * free them. This will result in freeing mlocked pages.
b291f000 229 */
ba470de4 230void munlock_vma_pages_range(struct vm_area_struct *vma,
408e82b7 231 unsigned long start, unsigned long end)
b291f000
NP
232{
233 vma->vm_flags &= ~VM_LOCKED;
408e82b7 234
ff6a6da6 235 while (start < end) {
6e919717 236 struct page *page;
ff6a6da6
ML
237 unsigned int page_mask, page_increm;
238
6e919717
HD
239 /*
240 * Although FOLL_DUMP is intended for get_dump_page(),
241 * it just so happens that its special treatment of the
242 * ZERO_PAGE (returning an error instead of doing get_page)
243 * suits munlock very well (and if somehow an abnormal page
244 * has sneaked into the range, we won't oops here: great).
245 */
ff6a6da6
ML
246 page = follow_page_mask(vma, start, FOLL_GET | FOLL_DUMP,
247 &page_mask);
6e919717 248 if (page && !IS_ERR(page)) {
408e82b7 249 lock_page(page);
ff6a6da6
ML
250 /*
251 * Any THP page found by follow_page_mask() may have
252 * gotten split before reaching munlock_vma_page(),
253 * so we need to recompute the page_mask here.
254 */
255 page_mask = munlock_vma_page(page);
408e82b7
HD
256 unlock_page(page);
257 put_page(page);
258 }
ff6a6da6
ML
259 page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
260 start += page_increm * PAGE_SIZE;
408e82b7
HD
261 cond_resched();
262 }
b291f000
NP
263}
264
265/*
266 * mlock_fixup - handle mlock[all]/munlock[all] requests.
267 *
268 * Filters out "special" vmas -- VM_LOCKED never gets set for these, and
269 * munlock is a no-op. However, for some special vmas, we go ahead and
cea10a19 270 * populate the ptes.
b291f000
NP
271 *
272 * For vmas that pass the filters, merge/split as appropriate.
273 */
1da177e4 274static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
ca16d140 275 unsigned long start, unsigned long end, vm_flags_t newflags)
1da177e4 276{
b291f000 277 struct mm_struct *mm = vma->vm_mm;
1da177e4 278 pgoff_t pgoff;
b291f000 279 int nr_pages;
1da177e4 280 int ret = 0;
ca16d140 281 int lock = !!(newflags & VM_LOCKED);
1da177e4 282
fed067da 283 if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
31db58b3 284 is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm))
b291f000
NP
285 goto out; /* don't set VM_LOCKED, don't count */
286
1da177e4
LT
287 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
288 *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
289 vma->vm_file, pgoff, vma_policy(vma));
290 if (*prev) {
291 vma = *prev;
292 goto success;
293 }
294
1da177e4
LT
295 if (start != vma->vm_start) {
296 ret = split_vma(mm, vma, start, 1);
297 if (ret)
298 goto out;
299 }
300
301 if (end != vma->vm_end) {
302 ret = split_vma(mm, vma, end, 0);
303 if (ret)
304 goto out;
305 }
306
307success:
b291f000
NP
308 /*
309 * Keep track of amount of locked VM.
310 */
311 nr_pages = (end - start) >> PAGE_SHIFT;
312 if (!lock)
313 nr_pages = -nr_pages;
314 mm->locked_vm += nr_pages;
315
1da177e4
LT
316 /*
317 * vm_flags is protected by the mmap_sem held in write mode.
318 * It's okay if try_to_unmap_one unmaps a page just after we
b291f000 319 * set VM_LOCKED, __mlock_vma_pages_range will bring it back.
1da177e4 320 */
1da177e4 321
fed067da 322 if (lock)
408e82b7 323 vma->vm_flags = newflags;
fed067da 324 else
408e82b7 325 munlock_vma_pages_range(vma, start, end);
1da177e4 326
1da177e4 327out:
b291f000 328 *prev = vma;
1da177e4
LT
329 return ret;
330}
331
332static int do_mlock(unsigned long start, size_t len, int on)
333{
334 unsigned long nstart, end, tmp;
335 struct vm_area_struct * vma, * prev;
336 int error;
337
fed067da
ML
338 VM_BUG_ON(start & ~PAGE_MASK);
339 VM_BUG_ON(len != PAGE_ALIGN(len));
1da177e4
LT
340 end = start + len;
341 if (end < start)
342 return -EINVAL;
343 if (end == start)
344 return 0;
097d5910 345 vma = find_vma(current->mm, start);
1da177e4
LT
346 if (!vma || vma->vm_start > start)
347 return -ENOMEM;
348
097d5910 349 prev = vma->vm_prev;
1da177e4
LT
350 if (start > vma->vm_start)
351 prev = vma;
352
353 for (nstart = start ; ; ) {
ca16d140 354 vm_flags_t newflags;
1da177e4
LT
355
356 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
357
18693050
ML
358 newflags = vma->vm_flags & ~VM_LOCKED;
359 if (on)
09a9f1d2 360 newflags |= VM_LOCKED;
1da177e4
LT
361
362 tmp = vma->vm_end;
363 if (tmp > end)
364 tmp = end;
365 error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
366 if (error)
367 break;
368 nstart = tmp;
369 if (nstart < prev->vm_end)
370 nstart = prev->vm_end;
371 if (nstart >= end)
372 break;
373
374 vma = prev->vm_next;
375 if (!vma || vma->vm_start != nstart) {
376 error = -ENOMEM;
377 break;
378 }
379 }
380 return error;
381}
382
bebeb3d6
ML
383/*
384 * __mm_populate - populate and/or mlock pages within a range of address space.
385 *
386 * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
387 * flags. VMAs must be already marked with the desired vm_flags, and
388 * mmap_sem must not be held.
389 */
390int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
fed067da
ML
391{
392 struct mm_struct *mm = current->mm;
393 unsigned long end, nstart, nend;
394 struct vm_area_struct *vma = NULL;
53a7706d 395 int locked = 0;
28a35716 396 long ret = 0;
fed067da
ML
397
398 VM_BUG_ON(start & ~PAGE_MASK);
399 VM_BUG_ON(len != PAGE_ALIGN(len));
400 end = start + len;
401
fed067da
ML
402 for (nstart = start; nstart < end; nstart = nend) {
403 /*
404 * We want to fault in pages for [nstart; end) address range.
405 * Find first corresponding VMA.
406 */
53a7706d
ML
407 if (!locked) {
408 locked = 1;
409 down_read(&mm->mmap_sem);
fed067da 410 vma = find_vma(mm, nstart);
53a7706d 411 } else if (nstart >= vma->vm_end)
fed067da
ML
412 vma = vma->vm_next;
413 if (!vma || vma->vm_start >= end)
414 break;
415 /*
416 * Set [nstart; nend) to intersection of desired address
417 * range with the first VMA. Also, skip undesirable VMA types.
418 */
419 nend = min(end, vma->vm_end);
09a9f1d2 420 if (vma->vm_flags & (VM_IO | VM_PFNMAP))
fed067da
ML
421 continue;
422 if (nstart < vma->vm_start)
423 nstart = vma->vm_start;
424 /*
53a7706d
ML
425 * Now fault in a range of pages. __mlock_vma_pages_range()
426 * double checks the vma flags, so that it won't mlock pages
427 * if the vma was already munlocked.
fed067da 428 */
53a7706d
ML
429 ret = __mlock_vma_pages_range(vma, nstart, nend, &locked);
430 if (ret < 0) {
431 if (ignore_errors) {
432 ret = 0;
433 continue; /* continue at next VMA */
434 }
5fdb2002
ML
435 ret = __mlock_posix_error_return(ret);
436 break;
437 }
53a7706d
ML
438 nend = nstart + ret * PAGE_SIZE;
439 ret = 0;
fed067da 440 }
53a7706d
ML
441 if (locked)
442 up_read(&mm->mmap_sem);
fed067da
ML
443 return ret; /* 0 or negative error code */
444}
445
6a6160a7 446SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
1da177e4
LT
447{
448 unsigned long locked;
449 unsigned long lock_limit;
450 int error = -ENOMEM;
451
452 if (!can_do_mlock())
453 return -EPERM;
454
8891d6da
KM
455 lru_add_drain_all(); /* flush pagevec */
456
1da177e4
LT
457 down_write(&current->mm->mmap_sem);
458 len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
459 start &= PAGE_MASK;
460
461 locked = len >> PAGE_SHIFT;
462 locked += current->mm->locked_vm;
463
59e99e5b 464 lock_limit = rlimit(RLIMIT_MEMLOCK);
1da177e4
LT
465 lock_limit >>= PAGE_SHIFT;
466
467 /* check against resource limits */
468 if ((locked <= lock_limit) || capable(CAP_IPC_LOCK))
469 error = do_mlock(start, len, 1);
470 up_write(&current->mm->mmap_sem);
fed067da 471 if (!error)
bebeb3d6 472 error = __mm_populate(start, len, 0);
1da177e4
LT
473 return error;
474}
475
6a6160a7 476SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
1da177e4
LT
477{
478 int ret;
479
480 down_write(&current->mm->mmap_sem);
481 len = PAGE_ALIGN(len + (start & ~PAGE_MASK));
482 start &= PAGE_MASK;
483 ret = do_mlock(start, len, 0);
484 up_write(&current->mm->mmap_sem);
485 return ret;
486}
487
488static int do_mlockall(int flags)
489{
490 struct vm_area_struct * vma, * prev = NULL;
1da177e4
LT
491
492 if (flags & MCL_FUTURE)
09a9f1d2 493 current->mm->def_flags |= VM_LOCKED;
9977f0f1 494 else
09a9f1d2 495 current->mm->def_flags &= ~VM_LOCKED;
1da177e4
LT
496 if (flags == MCL_FUTURE)
497 goto out;
498
499 for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
ca16d140 500 vm_flags_t newflags;
1da177e4 501
18693050
ML
502 newflags = vma->vm_flags & ~VM_LOCKED;
503 if (flags & MCL_CURRENT)
09a9f1d2 504 newflags |= VM_LOCKED;
1da177e4
LT
505
506 /* Ignore errors */
507 mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
508 }
509out:
510 return 0;
511}
512
3480b257 513SYSCALL_DEFINE1(mlockall, int, flags)
1da177e4
LT
514{
515 unsigned long lock_limit;
516 int ret = -EINVAL;
517
518 if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE)))
519 goto out;
520
521 ret = -EPERM;
522 if (!can_do_mlock())
523 goto out;
524
df9d6985
CL
525 if (flags & MCL_CURRENT)
526 lru_add_drain_all(); /* flush pagevec */
8891d6da 527
1da177e4
LT
528 down_write(&current->mm->mmap_sem);
529
59e99e5b 530 lock_limit = rlimit(RLIMIT_MEMLOCK);
1da177e4
LT
531 lock_limit >>= PAGE_SHIFT;
532
533 ret = -ENOMEM;
534 if (!(flags & MCL_CURRENT) || (current->mm->total_vm <= lock_limit) ||
535 capable(CAP_IPC_LOCK))
536 ret = do_mlockall(flags);
537 up_write(&current->mm->mmap_sem);
bebeb3d6
ML
538 if (!ret && (flags & MCL_CURRENT))
539 mm_populate(0, TASK_SIZE);
1da177e4
LT
540out:
541 return ret;
542}
543
3480b257 544SYSCALL_DEFINE0(munlockall)
1da177e4
LT
545{
546 int ret;
547
548 down_write(&current->mm->mmap_sem);
549 ret = do_mlockall(0);
550 up_write(&current->mm->mmap_sem);
551 return ret;
552}
553
554/*
555 * Objects with different lifetime than processes (SHM_LOCK and SHM_HUGETLB
556 * shm segments) get accounted against the user_struct instead.
557 */
558static DEFINE_SPINLOCK(shmlock_user_lock);
559
560int user_shm_lock(size_t size, struct user_struct *user)
561{
562 unsigned long lock_limit, locked;
563 int allowed = 0;
564
565 locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
59e99e5b 566 lock_limit = rlimit(RLIMIT_MEMLOCK);
5ed44a40
HB
567 if (lock_limit == RLIM_INFINITY)
568 allowed = 1;
1da177e4
LT
569 lock_limit >>= PAGE_SHIFT;
570 spin_lock(&shmlock_user_lock);
5ed44a40
HB
571 if (!allowed &&
572 locked + user->locked_shm > lock_limit && !capable(CAP_IPC_LOCK))
1da177e4
LT
573 goto out;
574 get_uid(user);
575 user->locked_shm += locked;
576 allowed = 1;
577out:
578 spin_unlock(&shmlock_user_lock);
579 return allowed;
580}
581
582void user_shm_unlock(size_t size, struct user_struct *user)
583{
584 spin_lock(&shmlock_user_lock);
585 user->locked_shm -= (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
586 spin_unlock(&shmlock_user_lock);
587 free_uid(user);
588}