Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932337AbbGGRKA (ORCPT ); Tue, 7 Jul 2015 13:10:00 -0400 Received: from a23-79-238-175.deploy.static.akamaitechnologies.com ([23.79.238.175]:64739 "EHLO prod-mail-xrelay07.akamai.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1757213AbbGGRJw (ORCPT ); Tue, 7 Jul 2015 13:09:52 -0400 From: Eric B Munson To: Andrew Morton Cc: Eric B Munson , Michal Hocko , Vlastimil Babka , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH V3 1/5] mm: mlock: Refactor mlock, munlock, and munlockall code Date: Tue, 7 Jul 2015 13:03:39 -0400 Message-Id: <1436288623-13007-2-git-send-email-emunson@akamai.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1436288623-13007-1-git-send-email-emunson@akamai.com> References: <1436288623-13007-1-git-send-email-emunson@akamai.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4371 Lines: 156 With the exception of mlockall() none of the mlock family of system calls take a flags argument so they are not extensible. A later patch in this set will extend the mlock family to support a middle ground between pages that are locked and faulted in immediately and unlocked pages. To pave the way for the new system calls, the code needs some reorganization so that all the actual entry points handle is checking input and translating to VMA flags. This patch mostly moves code around with the exception of do_munlockall(). All three functions are changed to support a follow on patch which introduces new system calls that allow the user to specify flags for these calls. Signed-off-by: Eric B Munson Cc: Michal Hocko Cc: Vlastimil Babka Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/mlock.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/mm/mlock.c b/mm/mlock.c index 6fd2cf1..8e52c23 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -553,7 +553,8 @@ out: return ret; } -static int do_mlock(unsigned long start, size_t len, int on) +static int apply_vma_flags(unsigned long start, size_t len, + vm_flags_t flags, bool add_flags) { unsigned long nstart, end, tmp; struct vm_area_struct * vma, * prev; @@ -579,9 +580,11 @@ static int do_mlock(unsigned long start, size_t len, int on) /* Here we know that vma->vm_start <= nstart < vma->vm_end. */ - newflags = vma->vm_flags & ~VM_LOCKED; - if (on) - newflags |= VM_LOCKED; + newflags = vma->vm_flags; + if (add_flags) + newflags |= flags; + else + newflags &= ~flags; tmp = vma->vm_end; if (tmp > end) @@ -604,7 +607,7 @@ static int do_mlock(unsigned long start, size_t len, int on) return error; } -SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len) +static int do_mlock(unsigned long start, size_t len, vm_flags_t flags) { unsigned long locked; unsigned long lock_limit; @@ -628,7 +631,7 @@ SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len) /* check against resource limits */ if ((locked <= lock_limit) || capable(CAP_IPC_LOCK)) - error = do_mlock(start, len, 1); + error = apply_vma_flags(start, len, flags, true); up_write(¤t->mm->mmap_sem); if (error) @@ -640,7 +643,12 @@ SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len) return 0; } -SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len) +SYSCALL_DEFINE2(mlock, unsigned long, start, size_t, len) +{ + return do_mlock(start, len, VM_LOCKED); +} + +static int do_munlock(unsigned long start, size_t len, vm_flags_t flags) { int ret; @@ -648,20 +656,23 @@ SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len) start &= PAGE_MASK; down_write(¤t->mm->mmap_sem); - ret = do_mlock(start, len, 0); + ret = apply_vma_flags(start, len, flags, false); up_write(¤t->mm->mmap_sem); return ret; } +SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len) +{ + return do_munlock(start, len, VM_LOCKED); +} + static int do_mlockall(int flags) { struct vm_area_struct * vma, * prev = NULL; if (flags & MCL_FUTURE) current->mm->def_flags |= VM_LOCKED; - else - current->mm->def_flags &= ~VM_LOCKED; if (flags == MCL_FUTURE) goto out; @@ -711,12 +722,36 @@ out: return ret; } +static int do_munlockall(int flags) +{ + struct vm_area_struct * vma, * prev = NULL; + + if (flags & MCL_FUTURE) + current->mm->def_flags &= ~VM_LOCKED; + if (flags == MCL_FUTURE) + goto out; + + for (vma = current->mm->mmap; vma ; vma = prev->vm_next) { + vm_flags_t newflags; + + newflags = vma->vm_flags; + if (flags & MCL_CURRENT) + newflags &= ~VM_LOCKED; + + /* Ignore errors */ + mlock_fixup(vma, &prev, vma->vm_start, vma->vm_end, newflags); + cond_resched_rcu_qs(); + } +out: + return 0; +} + SYSCALL_DEFINE0(munlockall) { int ret; down_write(¤t->mm->mmap_sem); - ret = do_mlockall(0); + ret = do_munlockall(MCL_CURRENT | MCL_FUTURE); up_write(¤t->mm->mmap_sem); return ret; } -- 1.9.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/