8 #include <linux/capability.h>
9 #include <linux/mman.h>
14 #include <linux/mempolicy.h>
16 #include <linux/sched.h>
17 #include <linux/export.h>
56 if (!TestClearPageMlocked(page))
68 if (PageUnevictable(page))
81 if (!TestSetPageMlocked(page)) {
107 BUG_ON(!PageLocked(page));
109 if (TestClearPageMlocked(page)) {
120 if (page_mapcount(page) > 1)
138 if (PageUnevictable(page))
159 unsigned long start,
unsigned long end,
173 gup_flags = FOLL_TOUCH | FOLL_MLOCK;
179 if ((vma->
vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
180 gup_flags |= FOLL_WRITE;
186 if (vma->
vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
187 gup_flags |= FOLL_FORCE;
196 static int __mlock_posix_error_return(
long retval)
200 else if (retval == -
ENOMEM)
219 unsigned long start,
unsigned long end)
227 if (vma->
vm_flags & (VM_IO | VM_PFNMAP))
230 if (!((vma->
vm_flags & VM_DONTEXPAND) ||
231 is_vm_hugetlb_page(vma) ||
234 __mlock_vma_pages_range(vma, start, end,
NULL);
274 unsigned long start,
unsigned long end)
290 page =
follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
291 if (page && !IS_ERR(page)) {
311 unsigned long start,
unsigned long end,
vm_flags_t newflags)
317 int lock = !!(newflags & VM_LOCKED);
349 nr_pages = -nr_pages;
368 static int do_mlock(
unsigned long start,
size_t len,
int on)
370 unsigned long nstart,
end,
tmp;
389 for (nstart = start ; ; ) {
394 newflags = vma->
vm_flags | VM_LOCKED;
396 newflags &= ~VM_LOCKED;
401 error = mlock_fixup(vma, &prev, nstart, tmp, newflags);
405 if (nstart < prev->
vm_end)
411 if (!vma || vma->
vm_start != nstart) {
419 static int do_mlock_pages(
unsigned long start,
size_t len,
int ignore_errors)
422 unsigned long end, nstart, nend;
431 for (nstart = start; nstart <
end; nstart = nend) {
440 }
else if (nstart >= vma->
vm_end)
449 if (vma->
vm_flags & (VM_IO | VM_PFNMAP))
458 ret = __mlock_vma_pages_range(vma, nstart, nend, &locked);
464 ret = __mlock_posix_error_return(ret);
478 unsigned long lock_limit;
491 locked +=
current->mm->locked_vm;
498 error = do_mlock(start, len, 1);
501 error = do_mlock_pages(start, len, 0);
512 ret = do_mlock(start, len, 0);
517 static int do_mlockall(
int flags)
520 unsigned int def_flags = 0;
523 def_flags = VM_LOCKED;
524 current->mm->def_flags = def_flags;
525 if (flags == MCL_FUTURE)
531 newflags = vma->
vm_flags | VM_LOCKED;
533 newflags &= ~VM_LOCKED;
544 unsigned long lock_limit;
547 if (!flags || (flags & ~(
MCL_CURRENT | MCL_FUTURE)))
563 if (!(flags & MCL_CURRENT) || (
current->mm->total_vm <= lock_limit) ||
565 ret = do_mlockall(flags);
567 if (!ret && (flags & MCL_CURRENT)) {
580 ret = do_mlockall(0);
593 unsigned long lock_limit,
locked;
601 spin_lock(&shmlock_user_lock);
609 spin_unlock(&shmlock_user_lock);
615 spin_lock(&shmlock_user_lock);
617 spin_unlock(&shmlock_user_lock);