From patchwork Thu Mar 13 22:21:08 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 26217 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ve0-f198.google.com (mail-ve0-f198.google.com [209.85.128.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5368F203AC for ; Thu, 13 Mar 2014 22:21:18 +0000 (UTC) Received: by mail-ve0-f198.google.com with SMTP id oz11sf3785946veb.9 for ; Thu, 13 Mar 2014 15:21:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=OMLCT+199iEE2ccLIxN9qSkbtjAOOrQa0wQKQliDMlo=; b=cgvKtP9LnV80OduEx8kKfF5mVReM/HM6BGUnQPLriQ8983O3Y3Uct/UxKL54hTNjyi tbZr6UhZ6drN5Cth2RmC3bvq1HljKoIQeAoek7zoRlf/laFcxLGHhDQFIOR6AG2APc0d aPPTyKpmobfno+WVKSD2ydK391Wyu5jiJqcrRoYcgFv+bBDreieyQKsAXfWRnZGdfExI MqnCsvRuYN+otmx4Q6R8tBPMM89uhRgly6IaSVGmNq0k8XFa/wcE/oFjdvmEP9t/QYk0 Y707ZqUcG14uBxIx8f4Jzm9BReIilgzxjaBH3K+0mfmja+U1QaoaV4iu9fNELbEClBup iUXw== X-Gm-Message-State: ALoCoQnUnNUqvH8kFY5HKCOJ0uFOBcy4X437tDN+yxxdOcWKCelP4eYU12QecCTWsAqGKECv7l1U X-Received: by 10.58.58.41 with SMTP id n9mr1624322veq.23.1394749278050; Thu, 13 Mar 2014 15:21:18 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.47.50 with SMTP id l47ls447791qga.49.gmail; Thu, 13 Mar 2014 15:21:17 -0700 (PDT) X-Received: by 10.58.57.42 with SMTP id f10mr3453918veq.1.1394749277949; Thu, 13 Mar 2014 15:21:17 -0700 (PDT) Received: from mail-ve0-f174.google.com (mail-ve0-f174.google.com [209.85.128.174]) by mx.google.com with ESMTPS id cl8si1352704vcb.1.2014.03.13.15.21.17 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Mar 2014 15:21:17 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.174 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.174; Received: by mail-ve0-f174.google.com with SMTP id oz11so1842157veb.33 for ; Thu, 13 Mar 2014 15:21:17 -0700 (PDT) X-Received: by 10.220.191.134 with SMTP id dm6mr3486546vcb.16.1394749277784; Thu, 13 Mar 2014 15:21:17 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.78.9 with SMTP id i9csp8831vck; Thu, 13 Mar 2014 15:21:17 -0700 (PDT) X-Received: by 10.68.139.137 with SMTP id qy9mr5192462pbb.11.1394749276385; Thu, 13 Mar 2014 15:21:16 -0700 (PDT) Received: from mail-pb0-f52.google.com (mail-pb0-f52.google.com [209.85.160.52]) by mx.google.com with ESMTPS id h3si2339460paw.209.2014.03.13.15.21.16 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Mar 2014 15:21:16 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.160.52 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) client-ip=209.85.160.52; Received: by mail-pb0-f52.google.com with SMTP id rr13so1729995pbb.39 for ; Thu, 13 Mar 2014 15:21:16 -0700 (PDT) X-Received: by 10.68.130.137 with SMTP id oe9mr5175740pbb.21.1394749275924; Thu, 13 Mar 2014 15:21:15 -0700 (PDT) Received: from buildbox.hsd1.or.comcast.net (c-67-170-153-23.hsd1.or.comcast.net. [67.170.153.23]) by mx.google.com with ESMTPSA id zb2sm10243072pbc.30.2014.03.13.15.21.14 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 13 Mar 2014 15:21:15 -0700 (PDT) From: John Stultz To: Dave Hansen Cc: John Stultz Subject: [PATCH 1/3] vrange: Add vrange syscall and handle splitting/merging and marking vmas Date: Thu, 13 Mar 2014 15:21:08 -0700 Message-Id: <1394749270-24750-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.8.3.2 X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.174 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This patch introduces the vrange() syscall, which allows for specifying ranges of memory as volatile, and able to be discarded by the system. This initial patch simply adds the syscall, and the vma handling, splitting and merging the vmas as needed, and marking them with VM_VOLATILE. No purging or discarding of volatile ranges is done at this point. Example man page: NAME vrange - Mark or unmark range of memory as volatile SYNOPSIS int vrange(unsigned_long start, size_t length, int mode, int *purged); DESCRIPTION Applications can use vrange(2) to advise the kernel how it should handle paging I/O in this VM area. The idea is to help the kernel discard pages of vrange instead of reclaiming when memory pressure happens. It means kernel doesn't discard any pages of vrange if there is no memory pressure. mode: VRANGE_VOLATILE hint to kernel so VM can discard in vrange pages when memory pressure happens. VRANGE_NONVOLATILE hint to kernel so VM doesn't discard vrange pages any more. If user try to access purged memory without VRANGE_NONVOLATILE call, he can encounter SIGBUS if the page was discarded by kernel. purged: Pointer to an integer which will return 1 if mode == VRANGE_NONVOLATILE and any page in the affected range was purged. If purged returns zero during a mode == VRANGE_NONVOLATILE call, it means all of the pages in the range are intact. RETURN VALUE On success vrange returns the number of bytes marked or unmarked. Similar to write(), it may return fewer bytes then specified if it ran into a problem. If an error is returned, no changes were made. ERRORS EINVAL This error can occur for the following reasons: * The value length is negative or not page size units. * addr is not page-aligned * mode not a valid value. ENOMEM Not enough memory EFAULT purged pointer is invalid This a simplified implementation which reuses some of the logic from Minchan's earlier efforts. So credit to Minchan for his work. Signed-off-by: John Stultz --- arch/x86/syscalls/syscall_64.tbl | 1 + include/linux/mm.h | 1 + include/linux/vrange.h | 7 ++ mm/Makefile | 2 +- mm/vrange.c | 152 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 include/linux/vrange.h create mode 100644 mm/vrange.c diff --git a/arch/x86/syscalls/syscall_64.tbl b/arch/x86/syscalls/syscall_64.tbl index a12bddc..7ae3940 100644 --- a/arch/x86/syscalls/syscall_64.tbl +++ b/arch/x86/syscalls/syscall_64.tbl @@ -322,6 +322,7 @@ 313 common finit_module sys_finit_module 314 common sched_setattr sys_sched_setattr 315 common sched_getattr sys_sched_getattr +316 common vrange sys_vrange # # x32-specific system call numbers start at 512 to avoid cache impact diff --git a/include/linux/mm.h b/include/linux/mm.h index c1b7414..a1f11da 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -117,6 +117,7 @@ extern unsigned int kobjsize(const void *objp); #define VM_IO 0x00004000 /* Memory mapped I/O or similar */ /* Used by sys_madvise() */ +#define VM_VOLATILE 0x00001000 /* VMA is volatile */ #define VM_SEQ_READ 0x00008000 /* App will access data sequentially */ #define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */ diff --git a/include/linux/vrange.h b/include/linux/vrange.h new file mode 100644 index 0000000..652396b --- /dev/null +++ b/include/linux/vrange.h @@ -0,0 +1,7 @@ +#ifndef _LINUX_VRANGE_H +#define _LINUX_VRANGE_H + +#define VRANGE_NONVOLATILE 0 +#define VRANGE_VOLATILE 1 + +#endif /* _LINUX_VRANGE_H */ diff --git a/mm/Makefile b/mm/Makefile index 310c90a..20229e2 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -16,7 +16,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ readahead.o swap.o truncate.o vmscan.o shmem.o \ util.o mmzone.o vmstat.o backing-dev.o \ mm_init.o mmu_context.o percpu.o slab_common.o \ - compaction.o balloon_compaction.o \ + compaction.o balloon_compaction.o vrange.o \ interval_tree.o list_lru.o $(mmu-y) obj-y += init-mm.o diff --git a/mm/vrange.c b/mm/vrange.c new file mode 100644 index 0000000..d9116b1 --- /dev/null +++ b/mm/vrange.c @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "internal.h" + +static ssize_t do_vrange(struct mm_struct *mm, unsigned long start, + unsigned long end, int mode, int *purged) +{ + struct vm_area_struct *vma, *prev; + unsigned long orig_start = start; + ssize_t count = 0, ret = 0; + int lpurged = 0; + + down_read(&mm->mmap_sem); + + vma = find_vma_prev(mm, start, &prev); + if (vma && start > vma->vm_start) + prev = vma; + + for (;;) { + unsigned long new_flags; + pgoff_t pgoff; + unsigned long tmp; + + if (!vma) + goto out; + + if (vma->vm_flags & (VM_SPECIAL|VM_LOCKED|VM_MIXEDMAP| + VM_HUGETLB)) + goto out; + + /* We don't support volatility on files for now */ + if (vma->vm_file) { + ret = -EINVAL; + goto out; + } + + new_flags = vma->vm_flags; + + if (start < vma->vm_start) { + start = vma->vm_start; + if (start >= end) + goto out; + } + tmp = vma->vm_end; + if (end < tmp) + tmp = end; + + switch(mode) { + case VRANGE_VOLATILE: + new_flags |= VM_VOLATILE; + break; + case VRANGE_NONVOLATILE: + new_flags &= ~VM_VOLATILE; + } + + pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); + prev = vma_merge(mm, prev, start, tmp, new_flags, + vma->anon_vma, vma->vm_file, pgoff, + vma_policy(vma)); + if (prev) { + goto success; + } + + + if (start != vma->vm_start) { + ret = split_vma(mm, vma, start, 1); + if (ret) + goto out; + } + + if (tmp != vma->vm_end) { + ret = split_vma(mm, vma, tmp, 0); + if (ret) + goto out; + } + + prev = vma; +success: + vma->vm_flags = new_flags; + *purged = lpurged; + + /* update count to distance covered so far*/ + count = tmp - orig_start; + + if (prev && start < prev->vm_end) + start = prev->vm_end; + if (start >= end) + goto out; + if (prev) + vma = prev->vm_next; + else /* madvise_remove dropped mmap_sem */ + vma = find_vma(mm, start); + } +out: + up_read(&mm->mmap_sem); + + /* report bytes successfully marked, even if we're exiting on error */ + if (count) + return count; + + return ret; +} + +SYSCALL_DEFINE4(vrange, unsigned long, start, + size_t, len, int, mode, int __user *, purged) +{ + unsigned long end; + struct mm_struct *mm = current->mm; + ssize_t ret = -EINVAL; + int p = 0; + + if (start & ~PAGE_MASK) + goto out; + + len &= PAGE_MASK; + if (!len) + goto out; + + end = start + len; + if (end < start) + goto out; + + if (start >= TASK_SIZE) + goto out; + + if (purged) { + /* Test pointer is valid before making any changes */ + if (put_user(p, purged)) + return -EFAULT; + } + + ret = do_vrange(mm, start, end, mode, &p); + + if (purged) { + if (put_user(p, purged)) { + /* + * This would be bad, since we've modified volatilty + * and the change in purged state would be lost. + */ + WARN_ONCE(1, "vrange: purge state possibly lost\n"); + } + } + +out: + return ret; +}