summaryrefslogtreecommitdiffstats
path: root/mm/mlock.c
diff options
context:
space:
mode:
authorEric B Munson <emunson@akamai.com>2015-11-05 21:51:39 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2015-11-05 22:34:48 -0500
commitb0f205c2a3082dd9081f9a94e50658c5fa906ff1 (patch)
treec2de47f26d57919ed73e8930f251d46ac0f4dc7a /mm/mlock.c
parentde60f5f10c58d4f34b68622442c0e04180367f3f (diff)
mm: mlock: add mlock flags to enable VM_LOCKONFAULT usage
The previous patch introduced a flag that specified pages in a VMA should be placed on the unevictable LRU, but they should not be made present when the area is created. This patch adds the ability to set this state via the new mlock system calls. We add MLOCK_ONFAULT for mlock2 and MCL_ONFAULT for mlockall. MLOCK_ONFAULT will set the VM_LOCKONFAULT modifier for VM_LOCKED. MCL_ONFAULT should be used as a modifier to the two other mlockall flags. When used with MCL_CURRENT, all current mappings will be marked with VM_LOCKED | VM_LOCKONFAULT. When used with MCL_FUTURE, the mm->def_flags will be marked with VM_LOCKED | VM_LOCKONFAULT. When used with both MCL_CURRENT and MCL_FUTURE, all current mappings and mm->def_flags will be marked with VM_LOCKED | VM_LOCKONFAULT. Prior to this patch, mlockall() will unconditionally clear the mm->def_flags any time it is called without MCL_FUTURE. This behavior is maintained after adding MCL_ONFAULT. If a call to mlockall(MCL_FUTURE) is followed by mlockall(MCL_CURRENT), the mm->def_flags will be cleared and new VMAs will be unlocked. This remains true with or without MCL_ONFAULT in either mlockall() invocation. munlock() will unconditionally clear both vma flags. munlockall() unconditionally clears for VMA flags on all VMAs and in the mm->def_flags field. Signed-off-by: Eric B Munson <emunson@akamai.com> Acked-by: Michal Hocko <mhocko@suse.com> Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Guenter Roeck <linux@roeck-us.net> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Cc: Ralf Baechle <ralf@linux-mips.org> Cc: Shuah Khan <shuahkh@osg.samsung.com> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/mlock.c')
-rw-r--r--mm/mlock.c51
1 files changed, 38 insertions, 13 deletions
diff --git a/mm/mlock.c b/mm/mlock.c
index ca3894113b97..339d9e0949b6 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -506,7 +506,8 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
506 506
507 if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) || 507 if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
508 is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm)) 508 is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm))
509 goto out; /* don't set VM_LOCKED, don't count */ 509 /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */
510 goto out;
510 511
511 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); 512 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
512 *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma, 513 *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
@@ -577,7 +578,7 @@ static int apply_vma_lock_flags(unsigned long start, size_t len,
577 prev = vma; 578 prev = vma;
578 579
579 for (nstart = start ; ; ) { 580 for (nstart = start ; ; ) {
580 vm_flags_t newflags = vma->vm_flags & ~VM_LOCKED; 581 vm_flags_t newflags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
581 582
582 newflags |= flags; 583 newflags |= flags;
583 584
@@ -646,10 +647,15 @@ SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len)
646 647
647SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags) 648SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags)
648{ 649{
649 if (flags) 650 vm_flags_t vm_flags = VM_LOCKED;
651
652 if (flags & ~MLOCK_ONFAULT)
650 return -EINVAL; 653 return -EINVAL;
651 654
652 return do_mlock(start, len, VM_LOCKED); 655 if (flags & MLOCK_ONFAULT)
656 vm_flags |= VM_LOCKONFAULT;
657
658 return do_mlock(start, len, vm_flags);
653} 659}
654 660
655SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len) 661SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
@@ -666,24 +672,43 @@ SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
666 return ret; 672 return ret;
667} 673}
668 674
675/*
676 * Take the MCL_* flags passed into mlockall (or 0 if called from munlockall)
677 * and translate into the appropriate modifications to mm->def_flags and/or the
678 * flags for all current VMAs.
679 *
680 * There are a couple of subtleties with this. If mlockall() is called multiple
681 * times with different flags, the values do not necessarily stack. If mlockall
682 * is called once including the MCL_FUTURE flag and then a second time without
683 * it, VM_LOCKED and VM_LOCKONFAULT will be cleared from mm->def_flags.
684 */
669static int apply_mlockall_flags(int flags) 685static int apply_mlockall_flags(int flags)
670{ 686{
671 struct vm_area_struct * vma, * prev = NULL; 687 struct vm_area_struct * vma, * prev = NULL;
688 vm_flags_t to_add = 0;
672 689
673 if (flags & MCL_FUTURE) 690 current->mm->def_flags &= VM_LOCKED_CLEAR_MASK;
691 if (flags & MCL_FUTURE) {
674 current->mm->def_flags |= VM_LOCKED; 692 current->mm->def_flags |= VM_LOCKED;
675 else
676 current->mm->def_flags &= ~VM_LOCKED;
677 693
678 if (flags == MCL_FUTURE) 694 if (flags & MCL_ONFAULT)
679 goto out; 695 current->mm->def_flags |= VM_LOCKONFAULT;
696
697 if (!(flags & MCL_CURRENT))
698 goto out;
699 }
700
701 if (flags & MCL_CURRENT) {
702 to_add |= VM_LOCKED;
703 if (flags & MCL_ONFAULT)
704 to_add |= VM_LOCKONFAULT;
705 }
680 706
681 for (vma = current->mm->mmap; vma ; vma = prev->vm_next) { 707 for (vma = current->mm->mmap; vma ; vma = prev->vm_next) {
682 vm_flags_t newflags; 708 vm_flags_t newflags;
683 709
684 newflags = vma->vm_flags & ~VM_LOCKED; 710 newflags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
685 if (flags & MCL_CURRENT) 711 newflags |= to_add;
686 newflags |= VM_LOCKED;
687 712
688 /* Ignore errors */ 713 /* Ignore errors */
689 mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags); 714 mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags);
@@ -698,7 +723,7 @@ SYSCALL_DEFINE1(mlockall, int, flags)
698 unsigned long lock_limit; 723 unsigned long lock_limit;
699 int ret; 724 int ret;
700 725
701 if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE))) 726 if (!flags || (flags & ~(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT)))
702 return -EINVAL; 727 return -EINVAL;
703 728
704 if (!can_do_mlock()) 729 if (!can_do_mlock())