From: Eric B Munson <hidden> Date: 2015-08-09 05:24:04
mlock() allows a user to control page out of program memory, but this
comes at the cost of faulting in the entire mapping when it is
allocated. For large mappings where the entire area is not necessary
this is not ideal. Instead of forcing all locked pages to be present
when they are allocated, this set creates a middle ground. Pages are
marked to be placed on the unevictable LRU (locked) when they are first
used, but they are not faulted in by the mlock call.
This series introduces a new mlock() system call that takes a flags
argument along with the start address and size. This flags argument
gives the caller the ability to request memory be locked in the
traditional way, or to be locked after the page is faulted in. A new
MCL flag is added to mirror the lock on fault behavior from mlock() in
mlockall().
There are two main use cases that this set covers. The first is the
security focussed mlock case. A buffer is needed that cannot be written
to swap. The maximum size is known, but on average the memory used is
significantly less than this maximum. With lock on fault, the buffer
is guaranteed to never be paged out without consuming the maximum size
every time such a buffer is created.
The second use case is focussed on performance. Portions of a large
file are needed and we want to keep the used portions in memory once
accessed. This is the case for large graphical models where the path
through the graph is not known until run time. The entire graph is
unlikely to be used in a given invocation, but once a node has been
used it needs to stay resident for further processing. Given these
constraints we have a number of options. We can potentially waste a
large amount of memory by mlocking the entire region (this can also
cause a significant stall at startup as the entire file is read in).
We can mlock every page as we access them without tracking if the page
is already resident but this introduces large overhead for each access.
The third option is mapping the entire region with PROT_NONE and using
a signal handler for SIGSEGV to mprotect(PROT_READ) and mlock() the
needed page. Doing this page at a time adds a significant performance
penalty. Batching can be used to mitigate this overhead, but in order
to safely avoid trying to mprotect pages outside of the mapping, the
boundaries of each mapping to be used in this way must be tracked and
available to the signal handler. This is precisely what the mm system
in the kernel should already be doing.
For mlock(MLOCK_ONFAULT) the user is charged against RLIMIT_MEMLOCK as
if mlock(MLOCK_LOCKED) or mmap(MAP_LOCKED) was used, so when the VMA is
created not when the pages are faulted in. For mlockall(MCL_ONFAULT)
the user is charged as if MCL_FUTURE was used. This decision was made
to keep the accounting checks out of the page fault path.
To illustrate the benefit of this set I wrote a test program that mmaps
a 5 GB file filled with random data and then makes 15,000,000 accesses
to random addresses in that mapping. The test program was run 20 times
for each setup. Results are reported for two program portions, setup
and execution. The setup phase is calling mmap and optionally mlock on
the entire region. For most experiments this is trivial, but it
highlights the cost of faulting in the entire region. Results are
averages across the 20 runs in milliseconds.
mmap with mlock(MLOCK_LOCKED) on entire range:
Setup avg: 8228.666
Processing avg: 8274.257
mmap with mlock(MLOCK_LOCKED) before each access:
Setup avg: 0.113
Processing avg: 90993.552
mmap with PROT_NONE and signal handler and batch size of 1 page:
With the default value in max_map_count, this gets ENOMEM as I attempt
to change the permissions, after upping the sysctl significantly I get:
Setup avg: 0.058
Processing avg: 69488.073
mmap with PROT_NONE and signal handler and batch size of 8 pages:
Setup avg: 0.068
Processing avg: 38204.116
mmap with PROT_NONE and signal handler and batch size of 16 pages:
Setup avg: 0.044
Processing avg: 29671.180
mmap with mlock(MLOCK_ONFAULT) on entire range:
Setup avg: 0.189
Processing avg: 17904.899
The signal handler in the batch cases faulted in memory in two steps to
avoid having to know the start and end of the faulting mapping. The
first step covers the page that caused the fault as we know that it will
be possible to lock. The second step speculatively tries to mlock and
mprotect the batch size - 1 pages that follow. There may be a clever
way to avoid this without having the program track each mapping to be
covered by this handeler in a globally accessible structure, but I could
not find it. It should be noted that with a large enough batch size
this two step fault handler can still cause the program to crash if it
reaches far beyond the end of the mapping.
These results show that if the developer knows that a majority of the
mapping will be used, it is better to try and fault it in at once,
otherwise mlock(MLOCK_ONFAULT) is significantly faster.
The performance cost of these patches are minimal on the two benchmarks
I have tested (stream and kernbench). The following are the average
values across 20 runs of stream and 10 runs of kernbench after a warmup
run whose results were discarded.
Avg throughput in MB/s from stream using 1000000 element arrays
Test 4.2-rc1 4.2-rc1+lock-on-fault
Copy: 10,566.5 10,421
Scale: 10,685 10,503.5
Add: 12,044.1 11,814.2
Triad: 12,064.8 11,846.3
Kernbench optimal load
4.2-rc1 4.2-rc1+lock-on-fault
Elapsed Time 78.453 78.991
User Time 64.2395 65.2355
System Time 9.7335 9.7085
Context Switches 22211.5 22412.1
Sleeps 14965.3 14956.1
---
Changes from V6:
* Bump the x86 system call number to avoid collision with userfaultfd
* Fix FOLL_POPULATE and FOLL_MLOCK usage when mmap is called with
MAP_POPULATE
* Add documentation for the proc smaps change
* checkpatch fixes
Changes from V5:
Drop MLOCK_LOCKED flag
* MLOCK_ONFAULT and MCL_ONFAULT are treated as a modifier to other locking
operations, mirroring the relationship between VM_LOCKED and
VM_LOCKONFAULT
* Drop mmap flag and related tests
* Fix clearing of MCL_CURRENT when mlockall is called with MCL_FUTURE,
mlockall behavoir now matches the old behavior WRT to ordering
Changes from V4:
Drop all architectures for new sys call entries except x86[_64] and MIPS
Drop munlock2 and munlockall2
Make VM_LOCKONFAULT a modifier to VM_LOCKED only to simplify book keeping
Adjust tests to match
Changes from V3:
Ensure that pages present when mlock2(MLOCK_ONFAULT) is called are locked
Ensure that VM_LOCKONFAULT is handled in cases that used to only check VM_LOCKED
Add tests for new system calls
Add missing syscall entries, fix NR_syscalls on multiple arch's
Add missing MAP_LOCKONFAULT for tile
Changes from V2:
Added new system calls for mlock, munlock, and munlockall with added
flags arguments for controlling how memory is locked or unlocked.
Eric B Munson (6):
mm: mlock: Refactor mlock, munlock, and munlockall code
mm: mlock: Add new mlock system call
mm: Introduce VM_LOCKONFAULT
mm: mlock: Add mlock flags to enable VM_LOCKONFAULT usage
selftests: vm: Add tests for lock on fault
mips: Add entry for new mlock2 syscall
Documentation/filesystems/proc.txt | 1 +
arch/alpha/include/uapi/asm/mman.h | 3 +
arch/mips/include/uapi/asm/mman.h | 6 +
arch/mips/include/uapi/asm/unistd.h | 15 +-
arch/mips/kernel/scall32-o32.S | 1 +
arch/mips/kernel/scall64-64.S | 1 +
arch/mips/kernel/scall64-n32.S | 1 +
arch/mips/kernel/scall64-o32.S | 1 +
arch/parisc/include/uapi/asm/mman.h | 3 +
arch/powerpc/include/uapi/asm/mman.h | 1 +
arch/sparc/include/uapi/asm/mman.h | 1 +
arch/tile/include/uapi/asm/mman.h | 1 +
arch/x86/entry/syscalls/syscall_32.tbl | 1 +
arch/x86/entry/syscalls/syscall_64.tbl | 1 +
arch/xtensa/include/uapi/asm/mman.h | 6 +
drivers/gpu/drm/drm_vm.c | 8 +-
fs/proc/task_mmu.c | 1 +
include/linux/mm.h | 2 +
include/linux/syscalls.h | 2 +
include/uapi/asm-generic/mman-common.h | 5 +
include/uapi/asm-generic/mman.h | 1 +
include/uapi/asm-generic/unistd.h | 4 +-
kernel/fork.c | 2 +-
kernel/sys_ni.c | 1 +
mm/debug.c | 1 +
mm/gup.c | 10 +-
mm/huge_memory.c | 2 +-
mm/hugetlb.c | 4 +-
mm/mlock.c | 87 +++-
mm/mmap.c | 2 +-
mm/rmap.c | 6 +-
tools/testing/selftests/vm/Makefile | 2 +
tools/testing/selftests/vm/mlock2-tests.c | 661 ++++++++++++++++++++++++++++
tools/testing/selftests/vm/on-fault-limit.c | 47 ++
tools/testing/selftests/vm/run_vmtests | 22 +
35 files changed, 872 insertions(+), 41 deletions(-)
create mode 100644 tools/testing/selftests/vm/mlock2-tests.c
create mode 100644 tools/testing/selftests/vm/on-fault-limit.c
Cc: Shuah Khan <redacted>
Cc: Michal Hocko <redacted>
Cc: Michael Kerrisk <redacted>
Cc: Vlastimil Babka <redacted>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Ralf Baechle <redacted>
Cc: Andrea Arcangeli <redacted>
Cc: linux-alpha@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-mips@linux-mips.org
Cc: linux-parisc@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: sparclinux@vger.kernel.org
Cc: linux-xtensa@linux-xtensa.org
Cc: linux-mm@kvack.org
Cc: linux-arch@vger.kernel.org
Cc: linux-api@vger.kernel.org
--
1.9.1
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Eric B Munson <hidden> Date: 2015-08-09 05:23:44
The cost of faulting in all memory to be locked can be very high when
working with large mappings. If only portions of the mapping will be
used this can incur a high penalty for locking.
For the example of a large file, this is the usage pattern for a large
statical language model (probably applies to other statical or graphical
models as well). For the security example, any application transacting
in data that cannot be swapped out (credit card data, medical records,
etc).
This patch introduces the ability to request that pages are not
pre-faulted, but are placed on the unevictable LRU when they are finally
faulted in. The VM_LOCKONFAULT flag will be used together with
VM_LOCKED and has no effect when set without VM_LOCKED. Setting the
VM_LOCKONFAULT flag for a VMA will cause pages faulted into that VMA to
be added to the unevictable LRU when they are faulted or if they are
already present, but will not cause any missing pages to be faulted in.
Exposing this new lock state means that we cannot overload the meaning
of the FOLL_POPULATE flag any longer. Prior to this patch it was used
to mean that the VMA for a fault was locked. This means we need the
new FOLL_MLOCK flag to communicate the locked state of a VMA.
FOLL_POPULATE will now only control if the VMA should be populated and
in the case of VM_LOCKONFAULT, it will not be set.
Signed-off-by: Eric B Munson <redacted>
Acked-by: Kirill A. Shutemov <redacted>
Cc: Michal Hocko <redacted>
Cc: Vlastimil Babka <redacted>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: "Kirill A. Shutemov" <redacted>
Cc: linux-kernel@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org
Cc: linux-mm@kvack.org
Cc: linux-api@vger.kernel.org
---
Documentation/filesystems/proc.txt | 1 +
drivers/gpu/drm/drm_vm.c | 8 +++++++-
fs/proc/task_mmu.c | 1 +
include/linux/mm.h | 2 ++
kernel/fork.c | 2 +-
mm/debug.c | 1 +
mm/gup.c | 10 ++++++++--
mm/huge_memory.c | 2 +-
mm/hugetlb.c | 4 ++--
mm/mlock.c | 2 +-
mm/mmap.c | 2 +-
mm/rmap.c | 6 ++++--
12 files changed, 30 insertions(+), 11 deletions(-)
@@ -463,6 +463,7 @@ manner. The codes are the following: rr - random read advise provided dc - do not copy area on fork de - do not expand area on remapping+ lf - mark area to lock pages when faulted in, do not pre-populate ac - area is accountable nr - swap space is not reserved for the area ht - area uses huge tlb pages
@@ -136,6 +136,7 @@ extern unsigned int kobjsize(const void *objp);#define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */#define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */+#define VM_LOCKONFAULT 0x00080000 /* Lock the pages covered when they are faulted in */#define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */#define VM_NORESERVE 0x00200000 /* should the VM suppress accounting */#define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */
@@ -2043,6 +2044,7 @@ static inline struct page *follow_page(struct vm_area_struct *vma,#define FOLL_NUMA 0x200 /* force NUMA hinting page fault */#define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */#define FOLL_TRIED 0x800 /* a retry, previous pass started an IO */+#define FOLL_MLOCK 0x1000 /* lock present pages */typedefint(*pte_fn_t)(pte_t*pte,pgtable_ttoken,unsignedlongaddr,void*data);
@@ -265,6 +265,9 @@ static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,unsignedintfault_flags=0;intret;+/* mlock all present pages, but do not fault in new pages */+if((*flags&(FOLL_POPULATE|FOLL_MLOCK))==FOLL_MLOCK)+return-ENOENT;/* For mm_populate(), just skip the stack guard page. */if((*flags&FOLL_POPULATE)&&(stack_guard_page_start(vma,address)||
@@ -850,7 +853,10 @@ long populate_vma_page_range(struct vm_area_struct *vma,VM_BUG_ON_VMA(end>vma->vm_end,vma);VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem),mm);-gup_flags=FOLL_TOUCH|FOLL_POPULATE;+gup_flags=FOLL_TOUCH|FOLL_POPULATE|FOLL_MLOCK;+if(vma->vm_flags&VM_LOCKONFAULT)+gup_flags&=~FOLL_POPULATE;+/**Wewanttotouchwritablemappingswithawritefaultinorder*tobreakCOW,exceptforsharedmappingsbecausethesedon'tCOW
@@ -3764,8 +3764,8 @@ static unsigned long page_table_shareable(struct vm_area_struct *svma,unsignedlongs_end=sbase+PUD_SIZE;/* Allow segments to share if only one is marked locked */-unsignedlongvm_flags=vma->vm_flags&~VM_LOCKED;-unsignedlongsvm_flags=svma->vm_flags&~VM_LOCKED;+unsignedlongvm_flags=vma->vm_flags&~(VM_LOCKED|VM_LOCKONFAULT);+unsignedlongsvm_flags=svma->vm_flags&~(VM_LOCKED|VM_LOCKONFAULT);/**matchthevirtualaddresses,permissionandthealignmentofthe
@@ -744,7 +744,8 @@ static int page_referenced_one(struct page *page, struct vm_area_struct *vma,if(vma->vm_flags&VM_LOCKED){spin_unlock(ptl);-pra->vm_flags|=VM_LOCKED;+pra->vm_flags|=+(vma->vm_flags&(VM_LOCKED|VM_LOCKONFAULT));returnSWAP_FAIL;/* To break the loop */}
@@ -765,7 +766,8 @@ static int page_referenced_one(struct page *page, struct vm_area_struct *vma,if(vma->vm_flags&VM_LOCKED){pte_unmap_unlock(pte,ptl);-pra->vm_flags|=VM_LOCKED;+pra->vm_flags|=+(vma->vm_flags&(VM_LOCKED|VM_LOCKONFAULT));returnSWAP_FAIL;/* To break the loop */}
--
1.9.1
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Eric B Munson <hidden> Date: 2015-08-09 05:23:50
Test the mmap() flag, and the mlockall() flag. These tests ensure that
pages are not faulted in until they are accessed, that the pages are
unevictable once faulted in, and that VMA splitting and merging works
with the new VM flag. The second test ensures that mlock limits are
respected. Note that the limit test needs to be run a normal user.
Also add tests to use the new mlock2 family of system calls.
Signed-off-by: Eric B Munson <redacted>
Cc: Shuah Khan <redacted>
Cc: Michal Hocko <redacted>
Cc: Vlastimil Babka <redacted>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-api@vger.kernel.org
---
tools/testing/selftests/vm/Makefile | 2 +
tools/testing/selftests/vm/mlock2-tests.c | 661 ++++++++++++++++++++++++++++
tools/testing/selftests/vm/on-fault-limit.c | 47 ++
tools/testing/selftests/vm/run_vmtests | 22 +
4 files changed, 732 insertions(+)
create mode 100644 tools/testing/selftests/vm/mlock2-tests.c
create mode 100644 tools/testing/selftests/vm/on-fault-limit.c
@@ -0,0 +1,661 @@+#include<sys/mman.h>+#include<stdio.h>+#include<stdlib.h>+#include<unistd.h>+#include<string.h>+#include<sys/time.h>+#include<sys/resource.h>+#include<errno.h>+#include<stdbool.h>++#ifndef MLOCK_ONFAULT+#define MLOCK_ONFAULT 1+#endif++#ifndef MCL_ONFAULT+#define MCL_ONFAULT (MCL_FUTURE << 1)+#endif++staticintmlock2_(void*start,size_tlen,intflags)+{+#ifdef __NR_mlock2+returnsyscall(__NR_mlock2,start,len,flags);+#else+errno=ENOSYS;+return-1;+#endif+}++structvm_boundaries{+unsignedlongstart;+unsignedlongend;+};++staticintget_vm_area(unsignedlongaddr,structvm_boundaries*area)+{+FILE*file;+intret=1;+charline[1024]={0};+char*end_addr;+char*stop;+unsignedlongstart;+unsignedlongend;++if(!area)+returnret;++file=fopen("/proc/self/maps","r");+if(!file){+perror("fopen");+returnret;+}++memset(area,0,sizeof(structvm_boundaries));++while(fgets(line,1024,file)){+end_addr=strchr(line,'-');+if(!end_addr){+printf("cannot parse /proc/self/maps\n");+gotoout;+}+*end_addr='\0';+end_addr++;+stop=strchr(end_addr,' ');+if(!stop){+printf("cannot parse /proc/self/maps\n");+gotoout;+}+stop='\0';++sscanf(line,"%lx",&start);+sscanf(end_addr,"%lx",&end);++if(start<=addr&&end>addr){+area->start=start;+area->end=end;+ret=0;+gotoout;+}+}+out:+fclose(file);+returnret;+}++staticunsignedlongget_pageflags(unsignedlongaddr)+{+FILE*file;+unsignedlongpfn;+unsignedlongoffset;++file=fopen("/proc/self/pagemap","r");+if(!file){+perror("fopen pagemap");+_exit(1);+}++offset=addr/getpagesize()*sizeof(unsignedlong);+if(fseek(file,offset,SEEK_SET)){+perror("fseek pagemap");+_exit(1);+}++if(fread(&pfn,sizeof(unsignedlong),1,file)!=1){+perror("fread pagemap");+_exit(1);+}++fclose(file);+returnpfn;+}++staticunsignedlongget_kpageflags(unsignedlongpfn)+{+unsignedlongflags;+FILE*file;++file=fopen("/proc/kpageflags","r");+if(!file){+perror("fopen kpageflags");+_exit(1);+}++if(fseek(file,pfn*sizeof(unsignedlong),SEEK_SET)){+perror("fseek kpageflags");+_exit(1);+}++if(fread(&flags,sizeof(unsignedlong),1,file)!=1){+perror("fread kpageflags");+_exit(1);+}++fclose(file);+returnflags;+}++#define VMFLAGS "VmFlags:"++staticboolfind_flag(FILE*file,constchar*vmflag)+{+char*line=NULL;+char*flags;+size_tsize=0;+boolret=false;++while(getline(&line,&size,file)>0){+if(!strstr(line,VMFLAGS)){+free(line);+line=NULL;+size=0;+continue;+}++flags=line+strlen(VMFLAGS);+ret=(strstr(flags,vmflag)!=NULL);+gotoout;+}++out:+free(line);+returnret;+}++staticboolis_vmflag_set(unsignedlongaddr,constchar*vmflag)+{+FILE*file;+char*line=NULL;+size_tsize=0;+boolret=false;+unsignedlongstart,end;+charperms[5];+unsignedlongoffset;+chardev[32];+unsignedlonginode;+charpath[BUFSIZ];++file=fopen("/proc/self/smaps","r");+if(!file){+perror("fopen smaps");+_exit(1);+}++while(getline(&line,&size,file)>0){+if(sscanf(line,"%lx-%lx %s %lx %s %lu %s\n",+&start,&end,perms,&offset,dev,&inode,path)<6)+gotonext;++if(start<=addr&&addr<end){+ret=find_flag(file,vmflag);+gotoout;+}++next:+free(line);+line=NULL;+size=0;+}++out:+free(line);+fclose(file);+returnret;+}++#define PRESENT_BIT 0x8000000000000000+#define PFN_MASK 0x007FFFFFFFFFFFFF+#define UNEVICTABLE_BIT (1UL << 18)++#define LOCKED "lo"+#define LOCKEDONFAULT "lf"++staticintlock_check(char*map)+{+unsignedlongpage1_flags;+unsignedlongpage2_flags;+unsignedlongpage_size=getpagesize();++page1_flags=get_pageflags((unsignedlong)map);+page2_flags=get_pageflags((unsignedlong)map+page_size);++/* Both pages should be present */+if(((page1_flags&PRESENT_BIT)==0)||+((page2_flags&PRESENT_BIT)==0)){+printf("Failed to make both pages present\n");+return1;+}++page1_flags=get_kpageflags(page1_flags&PFN_MASK);+page2_flags=get_kpageflags(page2_flags&PFN_MASK);++/* Both pages should be unevictable */+if(((page1_flags&UNEVICTABLE_BIT)==0)||+((page2_flags&UNEVICTABLE_BIT)==0)){+printf("Failed to make both pages unevictable\n");+return1;+}++if(!is_vmflag_set((unsignedlong)map,LOCKED)||+!is_vmflag_set((unsignedlong)map+page_size,LOCKED)){+printf("VMA flag %s is missing\n",LOCKED);+return1;+}++return0;+}++staticintunlock_lock_check(char*map)+{+unsignedlongpage1_flags;+unsignedlongpage2_flags;+unsignedlongpage_size=getpagesize();++page1_flags=get_pageflags((unsignedlong)map);+page2_flags=get_pageflags((unsignedlong)map+page_size);+page1_flags=get_kpageflags(page1_flags&PFN_MASK);+page2_flags=get_kpageflags(page2_flags&PFN_MASK);++if((page1_flags&UNEVICTABLE_BIT)||(page2_flags&UNEVICTABLE_BIT)){+printf("A page is still marked unevictable after unlock\n");+return1;+}++if(is_vmflag_set((unsignedlong)map,LOCKED)||+is_vmflag_set((unsignedlong)map+page_size,LOCKED)){+printf("VMA flag %s is still set after unlock\n",LOCKED);+return1;+}++return0;+}++staticinttest_mlock_lock()+{+char*map;+intret=1;+unsignedlongpage_size=getpagesize();++map=mmap(NULL,2*page_size,PROT_READ|PROT_WRITE,+MAP_ANONYMOUS|MAP_PRIVATE,0,0);+if(map==MAP_FAILED){+perror("test_mlock_locked mmap");+gotoout;+}++if(mlock2_(map,2*page_size,0)){+if(errno==ENOSYS){+printf("Cannot call new mlock family, skipping test\n");+_exit(0);+}+perror("mlock2(0)");+gotounmap;+}++if(lock_check(map))+gotounmap;++/* Now unlock and recheck attributes */+if(munlock(map,2*page_size)){+perror("munlock()");+gotounmap;+}++ret=unlock_lock_check(map);++unmap:+munmap(map,2*page_size);+out:+returnret;+}++staticintonfault_check(char*map)+{+unsignedlongpage1_flags;+unsignedlongpage2_flags;+unsignedlongpage_size=getpagesize();++page1_flags=get_pageflags((unsignedlong)map);+page2_flags=get_pageflags((unsignedlong)map+page_size);++/* Neither page should be present */+if((page1_flags&PRESENT_BIT)||(page2_flags&PRESENT_BIT)){+printf("Pages were made present by MLOCK_ONFAULT\n");+return1;+}++*map='a';+page1_flags=get_pageflags((unsignedlong)map);+page2_flags=get_pageflags((unsignedlong)map+page_size);++/* Only page 1 should be present */+if((page1_flags&PRESENT_BIT)==0){+printf("Page 1 is not present after fault\n");+return1;+}elseif(page2_flags&PRESENT_BIT){+printf("Page 2 was made present\n");+return1;+}++page1_flags=get_kpageflags(page1_flags&PFN_MASK);++/* Page 1 should be unevictable */+if((page1_flags&UNEVICTABLE_BIT)==0){+printf("Failed to make faulted page unevictable\n");+return1;+}++if(!is_vmflag_set((unsignedlong)map,LOCKEDONFAULT)||+!is_vmflag_set((unsignedlong)map+page_size,LOCKEDONFAULT)){+printf("VMA flag %s is missing\n",LOCKEDONFAULT);+return1;+}++return0;+}++staticintunlock_onfault_check(char*map)+{+unsignedlongpage1_flags;+unsignedlongpage2_flags;+unsignedlongpage_size=getpagesize();++page1_flags=get_pageflags((unsignedlong)map);+page1_flags=get_kpageflags(page1_flags&PFN_MASK);++if(page1_flags&UNEVICTABLE_BIT){+printf("Page 1 is still marked unevictable after unlock\n");+return1;+}++if(is_vmflag_set((unsignedlong)map,LOCKEDONFAULT)||+is_vmflag_set((unsignedlong)map+page_size,LOCKEDONFAULT)){+printf("VMA flag %s is still set after unlock\n",LOCKEDONFAULT);+return1;+}++return0;+}++staticinttest_mlock_onfault()+{+char*map;+intret=1;+unsignedlongpage_size=getpagesize();++map=mmap(NULL,2*page_size,PROT_READ|PROT_WRITE,+MAP_ANONYMOUS|MAP_PRIVATE,0,0);+if(map==MAP_FAILED){+perror("test_mlock_locked mmap");+gotoout;+}++if(mlock2_(map,2*page_size,MLOCK_ONFAULT)){+if(errno==ENOSYS){+printf("Cannot call new mlock family, skipping test\n");+_exit(0);+}+perror("mlock2(MLOCK_ONFAULT)");+gotounmap;+}++if(onfault_check(map))+gotounmap;++/* Now unlock and recheck attributes */+if(munlock(map,2*page_size)){+if(errno==ENOSYS){+printf("Cannot call new mlock family, skipping test\n");+_exit(0);+}+perror("munlock()");+gotounmap;+}++ret=unlock_onfault_check(map);+unmap:+munmap(map,2*page_size);+out:+returnret;+}++staticinttest_lock_onfault_of_present()+{+char*map;+intret=1;+unsignedlongpage1_flags;+unsignedlongpage2_flags;+unsignedlongpage_size=getpagesize();++map=mmap(NULL,2*page_size,PROT_READ|PROT_WRITE,+MAP_ANONYMOUS|MAP_PRIVATE,0,0);+if(map==MAP_FAILED){+perror("test_mlock_locked mmap");+gotoout;+}++*map='a';++if(mlock2_(map,2*page_size,MLOCK_ONFAULT)){+if(errno==ENOSYS){+printf("Cannot call new mlock family, skipping test\n");+_exit(0);+}+perror("mlock2(MLOCK_ONFAULT)");+gotounmap;+}++page1_flags=get_pageflags((unsignedlong)map);+page2_flags=get_pageflags((unsignedlong)map+page_size);+page1_flags=get_kpageflags(page1_flags&PFN_MASK);+page2_flags=get_kpageflags(page2_flags&PFN_MASK);++/* Page 1 should be unevictable */+if((page1_flags&UNEVICTABLE_BIT)==0){+printf("Failed to make present page unevictable\n");+gotounmap;+}++if(!is_vmflag_set((unsignedlong)map,LOCKEDONFAULT)||+!is_vmflag_set((unsignedlong)map+page_size,LOCKEDONFAULT)){+printf("VMA flag %s is missing for one of the pages\n",LOCKEDONFAULT);+gotounmap;+}+ret=0;+unmap:+munmap(map,2*page_size);+out:+returnret;+}++staticinttest_munlockall()+{+char*map;+intret=1;+unsignedlongpage1_flags;+unsignedlongpage2_flags;+unsignedlongpage_size=getpagesize();++map=mmap(NULL,2*page_size,PROT_READ|PROT_WRITE,+MAP_ANONYMOUS|MAP_PRIVATE,0,0);++if(map==MAP_FAILED){+perror("test_munlockall mmap");+gotoout;+}++if(mlockall(MCL_CURRENT)){+perror("mlockall(MCL_CURRENT)");+gotoout;+}++if(lock_check(map))+gotounmap;++if(munlockall()){+perror("munlockall()");+gotounmap;+}++if(unlock_lock_check(map))+gotounmap;++munmap(map,2*page_size);++map=mmap(NULL,2*page_size,PROT_READ|PROT_WRITE,+MAP_ANONYMOUS|MAP_PRIVATE,0,0);++if(map==MAP_FAILED){+perror("test_munlockall second mmap");+gotoout;+}++if(mlockall(MCL_CURRENT|MCL_ONFAULT)){+perror("mlockall(MCL_CURRENT | MCL_ONFAULT)");+gotounmap;+}++if(onfault_check(map))+gotounmap;++if(munlockall()){+perror("munlockall()");+gotounmap;+}++if(unlock_onfault_check(map))+gotounmap;++if(mlockall(MCL_CURRENT|MCL_FUTURE)){+perror("mlockall(MCL_CURRENT | MCL_FUTURE)");+gotoout;+}++if(lock_check(map))+gotounmap;++if(munlockall()){+perror("munlockall()");+gotounmap;+}++ret=unlock_lock_check(map);++unmap:+munmap(map,2*page_size);+out:+munlockall();+returnret;+}++staticinttest_vma_management(boolcall_mlock)+{+intret=1;+void*map;+unsignedlongpage_size=getpagesize();+structvm_boundariespage1;+structvm_boundariespage2;+structvm_boundariespage3;++map=mmap(NULL,3*page_size,PROT_READ|PROT_WRITE,+MAP_ANONYMOUS|MAP_PRIVATE,0,0);+if(map==MAP_FAILED){+perror("mmap()");+returnret;+}++if(call_mlock&&mlock2_(map,3*page_size,MLOCK_ONFAULT)){+if(errno==ENOSYS){+printf("Cannot call new mlock family, skipping test\n");+_exit(0);+}+perror("mlock(ONFAULT)\n");+gotoout;+}++if(get_vm_area((unsignedlong)map,&page1)||+get_vm_area((unsignedlong)map+page_size,&page2)||+get_vm_area((unsignedlong)map+page_size*2,&page3)){+printf("couldn't find mapping in /proc/self/maps\n");+gotoout;+}++/*+*Beforeweunlockaportion,weneedtothatallthreepagesarein+*thesameVMA.Iftheyarenotweabortthistest(Notethatthisis+*notafailure)+*/+if(page1.start!=page2.start||page2.start!=page3.start){+printf("VMAs are not merged to start, aborting test\n");+ret=0;+gotoout;+}++if(munlock(map+page_size,page_size)){+perror("munlock()");+gotoout;+}++if(get_vm_area((unsignedlong)map,&page1)||+get_vm_area((unsignedlong)map+page_size,&page2)||+get_vm_area((unsignedlong)map+page_size*2,&page3)){+printf("couldn't find mapping in /proc/self/maps\n");+gotoout;+}++/* All three VMAs should be different */+if(page1.start==page2.start||page2.start==page3.start){+printf("failed to split VMA for munlock\n");+gotoout;+}++/* Now unlock the first and third page and check the VMAs again */+if(munlock(map,page_size*3)){+perror("munlock()");+gotoout;+}++if(get_vm_area((unsignedlong)map,&page1)||+get_vm_area((unsignedlong)map+page_size,&page2)||+get_vm_area((unsignedlong)map+page_size*2,&page3)){+printf("couldn't find mapping in /proc/self/maps\n");+gotoout;+}++/* Now all three VMAs should be the same */+if(page1.start!=page2.start||page2.start!=page3.start){+printf("failed to merge VMAs after munlock\n");+gotoout;+}++ret=0;+out:+munmap(map,3*page_size);+returnret;+}++staticinttest_mlockall(int(test_function)(boolcall_mlock))+{+intret=1;++if(mlockall(MCL_CURRENT|MCL_ONFAULT|MCL_FUTURE)){+perror("mlockall");+returnret;+}++ret=test_function(false);+munlockall();+returnret;+}++intmain(char**argv,intargc)+{+intret=0;+ret+=test_mlock_lock();+ret+=test_mlock_onfault();+ret+=test_munlockall();+ret+=test_lock_onfault_of_present();+ret+=test_vma_management(true);+ret+=test_mlockall(test_vma_management);+returnret;+}+
@@ -0,0 +1,47 @@+#include<sys/mman.h>+#include<stdio.h>+#include<unistd.h>+#include<string.h>+#include<sys/time.h>+#include<sys/resource.h>++#ifndef MCL_ONFAULT+#define MCL_ONFAULT (MCL_FUTURE << 1)+#endif++staticinttest_limit(void)+{+intret=1;+structrlimitlims;+void*map;++if(getrlimit(RLIMIT_MEMLOCK,&lims)){+perror("getrlimit");+returnret;+}++if(mlockall(MCL_CURRENT|MCL_ONFAULT|MCL_FUTURE)){+perror("mlockall");+returnret;+}++map=mmap(NULL,2*lims.rlim_max,PROT_READ|PROT_WRITE,+MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE,0,0);+if(map!=MAP_FAILED)+printf("mmap should have failed, but didn't\n");+else{+ret=0;+munmap(map,2*lims.rlim_max);+}++munlockall();+returnret;+}++intmain(intargc,char**argv)+{+intret=0;++ret+=test_limit();+returnret;+}
--
1.9.1
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Eric B Munson <hidden> Date: 2015-08-09 05:23:57
With the refactored mlock code, introduce a new system call for mlock.
The new call will allow the user to specify what lock states are being
added. mlock2 is trivial at the moment, but a follow on patch will add
a new mlock state making it useful.
Signed-off-by: Eric B Munson <redacted>
Acked-by: Vlastimil Babka <redacted>
Cc: Michal Hocko <redacted>
Cc: Vlastimil Babka <redacted>
Cc: Heiko Carstens <redacted>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Stephen Rothwell <redacted>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Andrea Arcangeli <redacted>
Cc: linux-alpha@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: adi-buildroot-devel@lists.sourceforge.net
Cc: linux-cris-kernel@axis.com
Cc: linux-ia64@vger.kernel.org
Cc: linux-m68k@lists.linux-m68k.org
Cc: linux-am33-list@redhat.com
Cc: linux-parisc@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: linux-s390@vger.kernel.org
Cc: linux-sh@vger.kernel.org
Cc: sparclinux@vger.kernel.org
Cc: linux-xtensa@linux-xtensa.org
Cc: linux-api@vger.kernel.org
Cc: linux-arch@vger.kernel.org
Cc: linux-mm@kvack.org
---
arch/x86/entry/syscalls/syscall_32.tbl | 1 +
arch/x86/entry/syscalls/syscall_64.tbl | 1 +
include/linux/syscalls.h | 2 ++
include/uapi/asm-generic/unistd.h | 4 +++-
kernel/sys_ni.c | 1 +
mm/mlock.c | 8 ++++++++
6 files changed, 16 insertions(+), 1 deletion(-)
--
1.9.1
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Eric B Munson <hidden> Date: 2015-08-09 05:41:52
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 <redacted>
Acked-by: Vlastimil Babka <redacted>
Cc: Michal Hocko <redacted>
Cc: Vlastimil Babka <redacted>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: "Kirill A. Shutemov" <redacted>
Cc: linux-alpha@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-mips@linux-mips.org
Cc: linux-parisc@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: sparclinux@vger.kernel.org
Cc: linux-xtensa@linux-xtensa.org
Cc: linux-arch@vger.kernel.org
Cc: linux-api@vger.kernel.org
Cc: linux-mm@kvack.org
---
arch/alpha/include/uapi/asm/mman.h | 3 ++
arch/mips/include/uapi/asm/mman.h | 6 ++++
arch/parisc/include/uapi/asm/mman.h | 3 ++
arch/powerpc/include/uapi/asm/mman.h | 1 +
arch/sparc/include/uapi/asm/mman.h | 1 +
arch/tile/include/uapi/asm/mman.h | 1 +
arch/xtensa/include/uapi/asm/mman.h | 6 ++++
include/uapi/asm-generic/mman-common.h | 5 ++++
include/uapi/asm-generic/mman.h | 1 +
mm/mlock.c | 53 +++++++++++++++++++++++++---------
10 files changed, 67 insertions(+), 13 deletions(-)
@@ -37,6 +37,9 @@#define MCL_CURRENT 8192 /* lock all currently mapped pages */#define MCL_FUTURE 16384 /* lock all additions to address space */+#define MCL_ONFAULT 32768 /* lock all pages that are faulted in */++#define MLOCK_ONFAULT 0x01 /* Lock pages in range after they are faulted in, do not prefault */#define MADV_NORMAL 0 /* no further special treatment */#define MADV_RANDOM 1 /* expect random page references */
@@ -61,6 +61,12 @@*/#define MCL_CURRENT 1 /* lock all current mappings */#define MCL_FUTURE 2 /* lock all future mappings */+#define MCL_ONFAULT 4 /* lock all pages that are faulted in */++/*+*Flagsformlock+*/+#define MLOCK_ONFAULT 0x01 /* Lock pages in range after they are faulted in, do not prefault */#define MADV_NORMAL 0 /* no further special treatment */#define MADV_RANDOM 1 /* expect random page references */
@@ -31,6 +31,9 @@#define MCL_CURRENT 1 /* lock all current mappings */#define MCL_FUTURE 2 /* lock all future mappings */+#define MCL_ONFAULT 4 /* lock all pages that are faulted in */++#define MLOCK_ONFAULT 0x01 /* Lock pages in range after they are faulted in, do not prefault */#define MADV_NORMAL 0 /* no further special treatment */#define MADV_RANDOM 1 /* expect random page references */
@@ -22,6 +22,7 @@#define MCL_CURRENT 0x2000 /* lock all currently mapped pages */#define MCL_FUTURE 0x4000 /* lock all additions to address space */+#define MCL_ONFAULT 0x8000 /* lock all pages that are faulted in */#define MAP_POPULATE 0x8000 /* populate (prefault) pagetables */#define MAP_NONBLOCK 0x10000 /* do not block on IO */
@@ -17,6 +17,7 @@#define MCL_CURRENT 0x2000 /* lock all currently mapped pages */#define MCL_FUTURE 0x4000 /* lock all additions to address space */+#define MCL_ONFAULT 0x8000 /* lock all pages that are faulted in */#define MAP_POPULATE 0x8000 /* populate (prefault) pagetables */#define MAP_NONBLOCK 0x10000 /* do not block on IO */
@@ -36,6 +36,7 @@*/#define MCL_CURRENT 1 /* lock all current mappings */#define MCL_FUTURE 2 /* lock all future mappings */+#define MCL_ONFAULT 4 /* lock all pages that are faulted in */#endif /* _ASM_TILE_MMAN_H */
@@ -74,6 +74,12 @@*/#define MCL_CURRENT 1 /* lock all current mappings */#define MCL_FUTURE 2 /* lock all future mappings */+#define MCL_ONFAULT 4 /* lock all pages that are faulted in */++/*+*Flagsformlock+*/+#define MLOCK_ONFAULT 0x01 /* Lock pages in range after they are faulted in, do not prefault */#define MADV_NORMAL 0 /* no further special treatment */#define MADV_RANDOM 1 /* expect random page references */
@@ -25,6 +25,11 @@# define MAP_UNINITIALIZED 0x0 /* Don't support this flag */#endif+/*+*Flagsformlock+*/+#define MLOCK_ONFAULT 0x01 /* Lock pages in range after they are faulted in, do not prefault */+#define MS_ASYNC 1 /* sync memory asynchronously */#define MS_INVALIDATE 2 /* invalidate the caches */#define MS_SYNC 4 /* synchronous memory sync */
@@ -17,5 +17,6 @@#define MCL_CURRENT 1 /* lock all current mappings */#define MCL_FUTURE 2 /* lock all future mappings */+#define MCL_ONFAULT 4 /* lock all pages that are faulted in */#endif /* __ASM_GENERIC_MMAN_H */
--
1.9.1
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Michal Hocko <mhocko@kernel.org> Date: 2015-08-12 09:46:08
On Sun 09-08-15 01:22:52, Eric B Munson wrote:
With the refactored mlock code, introduce a new system call for mlock.
The new call will allow the user to specify what lock states are being
added. mlock2 is trivial at the moment, but a follow on patch will add
a new mlock state making it useful.
Looks good to me
Acked-by: Michal Hocko <mhocko@suse.com>
--
Michal Hocko
SUSE Labs
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Michal Hocko <mhocko@kernel.org> Date: 2015-08-12 11:59:17
On Sun 09-08-15 01:22:53, Eric B Munson wrote:
The cost of faulting in all memory to be locked can be very high when
working with large mappings. If only portions of the mapping will be
used this can incur a high penalty for locking.
For the example of a large file, this is the usage pattern for a large
statical language model (probably applies to other statical or graphical
models as well). For the security example, any application transacting
in data that cannot be swapped out (credit card data, medical records,
etc).
This patch introduces the ability to request that pages are not
pre-faulted, but are placed on the unevictable LRU when they are finally
faulted in. The VM_LOCKONFAULT flag will be used together with
VM_LOCKED and has no effect when set without VM_LOCKED.
I do not like this very much to be honest. We have only few bits
left there and it seems this is not really necessary. I thought that
LOCKONFAULT acts as a modifier to the mlock call to tell whether to
poppulate or not. The only place we have to persist it is
mlockall(MCL_FUTURE) AFAICS. And this can be handled by an additional
field in the mm_struct. This could be handled at __mm_populate level.
So unless I am missing something this would be much more easier
in the end we no new bit in VM flags would be necessary.
This would obviously mean that the LOCKONFAULT couldn't be exported to
the userspace but is this really necessary?
--
Michal Hocko
SUSE Labs
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Eric B Munson <hidden> Date: 2015-08-19 21:33:50
On Wed, 12 Aug 2015, Michal Hocko wrote:
On Sun 09-08-15 01:22:53, Eric B Munson wrote:
quoted
The cost of faulting in all memory to be locked can be very high when
working with large mappings. If only portions of the mapping will be
used this can incur a high penalty for locking.
For the example of a large file, this is the usage pattern for a large
statical language model (probably applies to other statical or graphical
models as well). For the security example, any application transacting
in data that cannot be swapped out (credit card data, medical records,
etc).
This patch introduces the ability to request that pages are not
pre-faulted, but are placed on the unevictable LRU when they are finally
faulted in. The VM_LOCKONFAULT flag will be used together with
VM_LOCKED and has no effect when set without VM_LOCKED.
I do not like this very much to be honest. We have only few bits
left there and it seems this is not really necessary. I thought that
LOCKONFAULT acts as a modifier to the mlock call to tell whether to
poppulate or not. The only place we have to persist it is
mlockall(MCL_FUTURE) AFAICS. And this can be handled by an additional
field in the mm_struct. This could be handled at __mm_populate level.
So unless I am missing something this would be much more easier
in the end we no new bit in VM flags would be necessary.
This would obviously mean that the LOCKONFAULT couldn't be exported to
the userspace but is this really necessary?
Sorry for the latency here, I was on vacation and am now at plumbers.
I am not sure that growing the mm_struct by another flags field instead
of using available bits in the vm_flags is the right choice. After this
patch, we still have 3 free bits on 32 bit architectures (2 after the
userfaultfd set IIRC). The group which asked for this feature here
wants the ability to distinguish between LOCKED and LOCKONFAULT regions
and without the VMA flag there isn't a way to do that.
Do we know that these last two open flags are needed right now or is
this speculation that they will be and that none of the other VMA flags
can be reclaimed?
On Sun 09-08-15 01:22:53, Eric B Munson wrote:
I do not like this very much to be honest. We have only few bits
left there and it seems this is not really necessary. I thought that
LOCKONFAULT acts as a modifier to the mlock call to tell whether to
poppulate or not. The only place we have to persist it is
mlockall(MCL_FUTURE) AFAICS. And this can be handled by an additional
field in the mm_struct. This could be handled at __mm_populate level.
So unless I am missing something this would be much more easier
in the end we no new bit in VM flags would be necessary.
This would obviously mean that the LOCKONFAULT couldn't be exported to
the userspace but is this really necessary?
Sorry for the latency here, I was on vacation and am now at plumbers.
I am not sure that growing the mm_struct by another flags field instead
of using available bits in the vm_flags is the right choice.
I was making the same objection on one of the earlier versions and since you sticked with a new vm flag, I thought it doesn't matter, as we could change it later if we run out of bits. But now I realize that since you export this difference to userspace (and below you say that it's by request), we won't be able to change it later. So it's a more difficult choice.
After this
patch, we still have 3 free bits on 32 bit architectures (2 after the
userfaultfd set IIRC). The group which asked for this feature here
wants the ability to distinguish between LOCKED and LOCKONFAULT regions
and without the VMA flag there isn't a way to do that.
Do we know that these last two open flags are needed right now or is
this speculation that they will be and that none of the other VMA flags
can be reclaimed?
I think it's the latter, we can expect that flags will be added rather than removed, as removal is hard or impossible.
From: Michal Hocko <mhocko@kernel.org> Date: 2015-08-20 07:56:15
On Wed 19-08-15 17:33:45, Eric B Munson wrote:
[...]
The group which asked for this feature here
wants the ability to distinguish between LOCKED and LOCKONFAULT regions
and without the VMA flag there isn't a way to do that.
Could you be more specific on why this is needed?
Do we know that these last two open flags are needed right now or is
this speculation that they will be and that none of the other VMA flags
can be reclaimed?
I do not think they are needed by anybody right now but that is not a
reason why it should be used without a really strong justification.
If the discoverability is really needed then fair enough but I haven't
seen any justification for that yet.
--
Michal Hocko
SUSE Labs
From: Eric B Munson <hidden> Date: 2015-08-20 17:03:14
On Thu, 20 Aug 2015, Michal Hocko wrote:
On Wed 19-08-15 17:33:45, Eric B Munson wrote:
[...]
quoted
The group which asked for this feature here
wants the ability to distinguish between LOCKED and LOCKONFAULT regions
and without the VMA flag there isn't a way to do that.
Could you be more specific on why this is needed?
They want to keep metrics on the amount of memory used in a LOCKONFAULT
region versus the address space of the region.
quoted
Do we know that these last two open flags are needed right now or is
this speculation that they will be and that none of the other VMA flags
can be reclaimed?
I do not think they are needed by anybody right now but that is not a
reason why it should be used without a really strong justification.
If the discoverability is really needed then fair enough but I haven't
seen any justification for that yet.
To be completely clear you believe that if the metrics collection is
not a strong enough justification, it is better to expand the mm_struct
by another unsigned long than to use one of these bits right?
From: Michal Hocko <mhocko@kernel.org> Date: 2015-08-21 07:25:56
On Thu 20-08-15 13:03:09, Eric B Munson wrote:
On Thu, 20 Aug 2015, Michal Hocko wrote:
quoted
On Wed 19-08-15 17:33:45, Eric B Munson wrote:
[...]
quoted
The group which asked for this feature here
wants the ability to distinguish between LOCKED and LOCKONFAULT regions
and without the VMA flag there isn't a way to do that.
Could you be more specific on why this is needed?
They want to keep metrics on the amount of memory used in a LOCKONFAULT
region versus the address space of the region.
/proc/<pid>/smaps already exports that information AFAICS. It exports
VMA flags including VM_LOCKED and if rss < size then this is clearly
LOCKONFAULT because the standard mlock semantic is to populate. Would
that be sufficient?
Now, it is true that LOCKONFAULT wouldn't be distinguishable from
MAP_LOCKED which failed to populate but does that really matter? It is
LOCKONFAULT in a way as well.
quoted
quoted
Do we know that these last two open flags are needed right now or is
this speculation that they will be and that none of the other VMA flags
can be reclaimed?
I do not think they are needed by anybody right now but that is not a
reason why it should be used without a really strong justification.
If the discoverability is really needed then fair enough but I haven't
seen any justification for that yet.
To be completely clear you believe that if the metrics collection is
not a strong enough justification, it is better to expand the mm_struct
by another unsigned long than to use one of these bits right?
A simple bool is sufficient for that. And yes I think we should go with
per mm_struct flag rather than the additional vma flag if it has only
the global (whole address space) scope - which would be the case if the
LOCKONFAULT is always an mlock modifier and the persistance is needed
only for MCL_FUTURE. Which is imho a sane semantic.
--
Michal Hocko
SUSE Labs
From: Eric B Munson <hidden> Date: 2015-08-21 18:31:37
On Fri, 21 Aug 2015, Michal Hocko wrote:
On Thu 20-08-15 13:03:09, Eric B Munson wrote:
quoted
On Thu, 20 Aug 2015, Michal Hocko wrote:
quoted
On Wed 19-08-15 17:33:45, Eric B Munson wrote:
[...]
quoted
The group which asked for this feature here
wants the ability to distinguish between LOCKED and LOCKONFAULT regions
and without the VMA flag there isn't a way to do that.
Could you be more specific on why this is needed?
They want to keep metrics on the amount of memory used in a LOCKONFAULT
region versus the address space of the region.
/proc/<pid>/smaps already exports that information AFAICS. It exports
VMA flags including VM_LOCKED and if rss < size then this is clearly
LOCKONFAULT because the standard mlock semantic is to populate. Would
that be sufficient?
Now, it is true that LOCKONFAULT wouldn't be distinguishable from
MAP_LOCKED which failed to populate but does that really matter? It is
LOCKONFAULT in a way as well.
Does that matter to my users? No, they do not use MAP_LOCKED at all so
any VMA with VM_LOCKED set and rss < size is lock on fault. Will it
matter to others? I suspect so, but these are likely to be the same
group of users which will be suprised to learn that MAP_LOCKED does not
guarantee that the entire range is faulted in on return from mmap.
quoted
quoted
quoted
Do we know that these last two open flags are needed right now or is
this speculation that they will be and that none of the other VMA flags
can be reclaimed?
I do not think they are needed by anybody right now but that is not a
reason why it should be used without a really strong justification.
If the discoverability is really needed then fair enough but I haven't
seen any justification for that yet.
To be completely clear you believe that if the metrics collection is
not a strong enough justification, it is better to expand the mm_struct
by another unsigned long than to use one of these bits right?
A simple bool is sufficient for that. And yes I think we should go with
per mm_struct flag rather than the additional vma flag if it has only
the global (whole address space) scope - which would be the case if the
LOCKONFAULT is always an mlock modifier and the persistance is needed
only for MCL_FUTURE. Which is imho a sane semantic.
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
From: Konstantin Khlebnikov <hidden> Date: 2015-08-24 10:17:53
On Fri, Aug 21, 2015 at 9:31 PM, Eric B Munson [off-list ref] wrote:
On Fri, 21 Aug 2015, Michal Hocko wrote:
quoted
On Thu 20-08-15 13:03:09, Eric B Munson wrote:
quoted
On Thu, 20 Aug 2015, Michal Hocko wrote:
quoted
On Wed 19-08-15 17:33:45, Eric B Munson wrote:
[...]
quoted
The group which asked for this feature here
wants the ability to distinguish between LOCKED and LOCKONFAULT regions
and without the VMA flag there isn't a way to do that.
Could you be more specific on why this is needed?
They want to keep metrics on the amount of memory used in a LOCKONFAULT
region versus the address space of the region.
/proc/<pid>/smaps already exports that information AFAICS. It exports
VMA flags including VM_LOCKED and if rss < size then this is clearly
LOCKONFAULT because the standard mlock semantic is to populate. Would
that be sufficient?
Now, it is true that LOCKONFAULT wouldn't be distinguishable from
MAP_LOCKED which failed to populate but does that really matter? It is
LOCKONFAULT in a way as well.
Does that matter to my users? No, they do not use MAP_LOCKED at all so
any VMA with VM_LOCKED set and rss < size is lock on fault. Will it
matter to others? I suspect so, but these are likely to be the same
group of users which will be suprised to learn that MAP_LOCKED does not
guarantee that the entire range is faulted in on return from mmap.
quoted
quoted
quoted
quoted
Do we know that these last two open flags are needed right now or is
this speculation that they will be and that none of the other VMA flags
can be reclaimed?
I do not think they are needed by anybody right now but that is not a
reason why it should be used without a really strong justification.
If the discoverability is really needed then fair enough but I haven't
seen any justification for that yet.
To be completely clear you believe that if the metrics collection is
not a strong enough justification, it is better to expand the mm_struct
by another unsigned long than to use one of these bits right?
A simple bool is sufficient for that. And yes I think we should go with
per mm_struct flag rather than the additional vma flag if it has only
the global (whole address space) scope - which would be the case if the
LOCKONFAULT is always an mlock modifier and the persistance is needed
only for MCL_FUTURE. Which is imho a sane semantic.
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
There might be a problem after failed populate: remap will handle them
as lock on fault. In this case we can fill ptes with swap-like non-present
entries to remember that fact and count them as should-be-locked pages.
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it just prepare the page tables and that's it?
There might be a problem after failed populate: remap will handle them
as lock on fault. In this case we can fill ptes with swap-like non-present
entries to remember that fact and count them as should-be-locked pages.
I don't think we should strive to have mremap try to fix the inherent unreliability of mmap (MAP_POPULATE)?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Konstantin Khlebnikov <hidden> Date: 2015-08-24 13:50:48
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
quoted
There might be a problem after failed populate: remap will handle them
as lock on fault. In this case we can fill ptes with swap-like non-present
entries to remember that fact and count them as should-be-locked pages.
I don't think we should strive to have mremap try to fix the inherent
unreliability of mmap (MAP_POPULATE)?
I don't think so. MAP_POPULATE works only when mmap happens.
Flag MREMAP_POPULATE might be a good idea. Just for symmetry.
On 08/24/2015 03:50 PM, Konstantin Khlebnikov wrote:
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
quoted
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
Oh, right... Well that looks like a convincing argument for having a sticky VM_LOCKONFAULT after all. Having mremap guess by scanning existing pte's would slow it down, and be unreliable (was the area completely populated because MLOCK_ONFAULT was not used or because the process aulted it already? Was it not populated because MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to populate it all?).
The only sane alternative is to populate always for mremap() of VM_LOCKED areas, and document this loss of MLOCK_ONFAULT information as a limitation of mlock2(MLOCK_ONFAULT). Which might or might not be enough for Eric's usecase, but it's somewhat ugly.
quoted
quoted
There might be a problem after failed populate: remap will handle them
as lock on fault. In this case we can fill ptes with swap-like non-present
entries to remember that fact and count them as should-be-locked pages.
I don't think we should strive to have mremap try to fix the inherent
unreliability of mmap (MAP_POPULATE)?
I don't think so. MAP_POPULATE works only when mmap happens.
Flag MREMAP_POPULATE might be a good idea. Just for symmetry.
From: Eric B Munson <hidden> Date: 2015-08-24 15:09:17
On Mon, 24 Aug 2015, Vlastimil Babka wrote:
On 08/24/2015 03:50 PM, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
quoted
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
Oh, right... Well that looks like a convincing argument for having a
sticky VM_LOCKONFAULT after all. Having mremap guess by scanning
existing pte's would slow it down, and be unreliable (was the area
completely populated because MLOCK_ONFAULT was not used or because
the process aulted it already? Was it not populated because
MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to
populate it all?).
Given this, I am going to stop working in v8 and leave the vma flag in
place.
The only sane alternative is to populate always for mremap() of
VM_LOCKED areas, and document this loss of MLOCK_ONFAULT information
as a limitation of mlock2(MLOCK_ONFAULT). Which might or might not
be enough for Eric's usecase, but it's somewhat ugly.
I don't think that this is the right solution, I would be really
surprised as a user if an area I locked with MLOCK_ONFAULT was then
fully locked and prepopulated after mremap().
quoted
quoted
quoted
There might be a problem after failed populate: remap will handle them
as lock on fault. In this case we can fill ptes with swap-like non-present
entries to remember that fact and count them as should-be-locked pages.
I don't think we should strive to have mremap try to fix the inherent
unreliability of mmap (MAP_POPULATE)?
I don't think so. MAP_POPULATE works only when mmap happens.
Flag MREMAP_POPULATE might be a good idea. Just for symmetry.
Maybe, but please do it as a separate series.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Konstantin Khlebnikov <hidden> Date: 2015-08-24 15:46:50
On Mon, Aug 24, 2015 at 6:09 PM, Eric B Munson [off-list ref] wrote:
On Mon, 24 Aug 2015, Vlastimil Babka wrote:
quoted
On 08/24/2015 03:50 PM, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
quoted
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
Oh, right... Well that looks like a convincing argument for having a
sticky VM_LOCKONFAULT after all. Having mremap guess by scanning
existing pte's would slow it down, and be unreliable (was the area
completely populated because MLOCK_ONFAULT was not used or because
the process aulted it already? Was it not populated because
MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to
populate it all?).
Given this, I am going to stop working in v8 and leave the vma flag in
place.
quoted
The only sane alternative is to populate always for mremap() of
VM_LOCKED areas, and document this loss of MLOCK_ONFAULT information
as a limitation of mlock2(MLOCK_ONFAULT). Which might or might not
be enough for Eric's usecase, but it's somewhat ugly.
I don't think that this is the right solution, I would be really
surprised as a user if an area I locked with MLOCK_ONFAULT was then
fully locked and prepopulated after mremap().
If mremap is the only problem then we can add opposite flag for it:
"MREMAP_NOPOPULATE"
- do not populate new segment of locked areas
- do not copy normal areas if possible (anonymous/special must be copied)
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
addr2 = mremap(addr, len, 2 * len, MREMAP_NOPOPULATE);
...
quoted
quoted
quoted
quoted
There might be a problem after failed populate: remap will handle them
as lock on fault. In this case we can fill ptes with swap-like non-present
entries to remember that fact and count them as should-be-locked pages.
I don't think we should strive to have mremap try to fix the inherent
unreliability of mmap (MAP_POPULATE)?
I don't think so. MAP_POPULATE works only when mmap happens.
Flag MREMAP_POPULATE might be a good idea. Just for symmetry.
Maybe, but please do it as a separate series.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Eric B Munson <hidden> Date: 2015-08-24 15:55:08
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
On Mon, Aug 24, 2015 at 6:09 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Vlastimil Babka wrote:
quoted
On 08/24/2015 03:50 PM, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
quoted
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
Oh, right... Well that looks like a convincing argument for having a
sticky VM_LOCKONFAULT after all. Having mremap guess by scanning
existing pte's would slow it down, and be unreliable (was the area
completely populated because MLOCK_ONFAULT was not used or because
the process aulted it already? Was it not populated because
MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to
populate it all?).
Given this, I am going to stop working in v8 and leave the vma flag in
place.
quoted
The only sane alternative is to populate always for mremap() of
VM_LOCKED areas, and document this loss of MLOCK_ONFAULT information
as a limitation of mlock2(MLOCK_ONFAULT). Which might or might not
be enough for Eric's usecase, but it's somewhat ugly.
I don't think that this is the right solution, I would be really
surprised as a user if an area I locked with MLOCK_ONFAULT was then
fully locked and prepopulated after mremap().
If mremap is the only problem then we can add opposite flag for it:
"MREMAP_NOPOPULATE"
- do not populate new segment of locked areas
- do not copy normal areas if possible (anonymous/special must be copied)
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
addr2 = mremap(addr, len, 2 * len, MREMAP_NOPOPULATE);
...
But with this, the user must remember what areas are locked with
MLOCK_LOCKONFAULT and which are locked the with prepopulate so the
correct mremap flags can be used.
From: Konstantin Khlebnikov <hidden> Date: 2015-08-24 16:22:51
On Mon, Aug 24, 2015 at 6:55 PM, Eric B Munson [off-list ref] wrote:
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 6:09 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Vlastimil Babka wrote:
quoted
On 08/24/2015 03:50 PM, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
quoted
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
Oh, right... Well that looks like a convincing argument for having a
sticky VM_LOCKONFAULT after all. Having mremap guess by scanning
existing pte's would slow it down, and be unreliable (was the area
completely populated because MLOCK_ONFAULT was not used or because
the process aulted it already? Was it not populated because
MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to
populate it all?).
Given this, I am going to stop working in v8 and leave the vma flag in
place.
quoted
The only sane alternative is to populate always for mremap() of
VM_LOCKED areas, and document this loss of MLOCK_ONFAULT information
as a limitation of mlock2(MLOCK_ONFAULT). Which might or might not
be enough for Eric's usecase, but it's somewhat ugly.
I don't think that this is the right solution, I would be really
surprised as a user if an area I locked with MLOCK_ONFAULT was then
fully locked and prepopulated after mremap().
If mremap is the only problem then we can add opposite flag for it:
"MREMAP_NOPOPULATE"
- do not populate new segment of locked areas
- do not copy normal areas if possible (anonymous/special must be copied)
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
addr2 = mremap(addr, len, 2 * len, MREMAP_NOPOPULATE);
...
But with this, the user must remember what areas are locked with
MLOCK_LOCKONFAULT and which are locked the with prepopulate so the
correct mremap flags can be used.
Yep. Shouldn't be hard. You anyway have to do some changes in user-space.
Much simpler for users-pace solution is a mm-wide flag which turns all further
mlocks and MAP_LOCKED into lock-on-fault. Something like
mlockall(MCL_NOPOPULATE_LOCKED).
From: Eric B Munson <hidden> Date: 2015-08-24 17:00:31
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
On Mon, Aug 24, 2015 at 6:55 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 6:09 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Vlastimil Babka wrote:
quoted
On 08/24/2015 03:50 PM, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
quoted
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
Oh, right... Well that looks like a convincing argument for having a
sticky VM_LOCKONFAULT after all. Having mremap guess by scanning
existing pte's would slow it down, and be unreliable (was the area
completely populated because MLOCK_ONFAULT was not used or because
the process aulted it already? Was it not populated because
MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to
populate it all?).
Given this, I am going to stop working in v8 and leave the vma flag in
place.
quoted
The only sane alternative is to populate always for mremap() of
VM_LOCKED areas, and document this loss of MLOCK_ONFAULT information
as a limitation of mlock2(MLOCK_ONFAULT). Which might or might not
be enough for Eric's usecase, but it's somewhat ugly.
I don't think that this is the right solution, I would be really
surprised as a user if an area I locked with MLOCK_ONFAULT was then
fully locked and prepopulated after mremap().
If mremap is the only problem then we can add opposite flag for it:
"MREMAP_NOPOPULATE"
- do not populate new segment of locked areas
- do not copy normal areas if possible (anonymous/special must be copied)
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
addr2 = mremap(addr, len, 2 * len, MREMAP_NOPOPULATE);
...
But with this, the user must remember what areas are locked with
MLOCK_LOCKONFAULT and which are locked the with prepopulate so the
correct mremap flags can be used.
Yep. Shouldn't be hard. You anyway have to do some changes in user-space.
Sorry if I wasn't clear enough in my last reply, I think forcing
userspace to track this is the wrong choice. The VM system is
responsible for tracking these attributes and should continue to be.
Much simpler for users-pace solution is a mm-wide flag which turns all further
mlocks and MAP_LOCKED into lock-on-fault. Something like
mlockall(MCL_NOPOPULATE_LOCKED).
This set certainly adds the foundation for such a change if you think it
would be useful. That particular behavior was not part of my inital use
case though.
From: Konstantin Khlebnikov <hidden> Date: 2015-08-24 18:53:41
On Mon, Aug 24, 2015 at 8:00 PM, Eric B Munson [off-list ref] wrote:
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 6:55 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 6:09 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Vlastimil Babka wrote:
quoted
On 08/24/2015 03:50 PM, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
quoted
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
Oh, right... Well that looks like a convincing argument for having a
sticky VM_LOCKONFAULT after all. Having mremap guess by scanning
existing pte's would slow it down, and be unreliable (was the area
completely populated because MLOCK_ONFAULT was not used or because
the process aulted it already? Was it not populated because
MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to
populate it all?).
Given this, I am going to stop working in v8 and leave the vma flag in
place.
quoted
The only sane alternative is to populate always for mremap() of
VM_LOCKED areas, and document this loss of MLOCK_ONFAULT information
as a limitation of mlock2(MLOCK_ONFAULT). Which might or might not
be enough for Eric's usecase, but it's somewhat ugly.
I don't think that this is the right solution, I would be really
surprised as a user if an area I locked with MLOCK_ONFAULT was then
fully locked and prepopulated after mremap().
If mremap is the only problem then we can add opposite flag for it:
"MREMAP_NOPOPULATE"
- do not populate new segment of locked areas
- do not copy normal areas if possible (anonymous/special must be copied)
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
addr2 = mremap(addr, len, 2 * len, MREMAP_NOPOPULATE);
...
But with this, the user must remember what areas are locked with
MLOCK_LOCKONFAULT and which are locked the with prepopulate so the
correct mremap flags can be used.
Yep. Shouldn't be hard. You anyway have to do some changes in user-space.
Sorry if I wasn't clear enough in my last reply, I think forcing
userspace to track this is the wrong choice. The VM system is
responsible for tracking these attributes and should continue to be.
Userspace tracks addresses and sizes of these areas. Plus mremap obviously
works only with page granularity so memory allocator in userspace have to know
a lot about these structures. So keeping one more bit isn't a rocket science.
quoted
Much simpler for users-pace solution is a mm-wide flag which turns all further
mlocks and MAP_LOCKED into lock-on-fault. Something like
mlockall(MCL_NOPOPULATE_LOCKED).
This set certainly adds the foundation for such a change if you think it
would be useful. That particular behavior was not part of my inital use
case though.
This looks like much easier solution: you don't need new syscall and after
enabling that lock-on-fault mode userspace still can get old behaviour simply
by touching newly locked area.
From: Eric B Munson <hidden> Date: 2015-08-24 20:28:43
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
On Mon, Aug 24, 2015 at 8:00 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 6:55 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 6:09 PM, Eric B Munson [off-list ref] wrote:
quoted
On Mon, 24 Aug 2015, Vlastimil Babka wrote:
quoted
On 08/24/2015 03:50 PM, Konstantin Khlebnikov wrote:
quoted
On Mon, Aug 24, 2015 at 4:30 PM, Vlastimil Babka [off-list ref] wrote:
quoted
On 08/24/2015 12:17 PM, Konstantin Khlebnikov wrote:
quoted
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
remap can count filled ptes and prefault only completely populated areas.
Does (and should) mremap really prefault non-present pages? Shouldn't it
just prepare the page tables and that's it?
As I see mremap prefaults pages when it extends mlocked area.
Also quote from manpage
: If the memory segment specified by old_address and old_size is locked
: (using mlock(2) or similar), then this lock is maintained when the segment is
: resized and/or relocated. As a consequence, the amount of memory locked
: by the process may change.
Oh, right... Well that looks like a convincing argument for having a
sticky VM_LOCKONFAULT after all. Having mremap guess by scanning
existing pte's would slow it down, and be unreliable (was the area
completely populated because MLOCK_ONFAULT was not used or because
the process aulted it already? Was it not populated because
MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to
populate it all?).
Given this, I am going to stop working in v8 and leave the vma flag in
place.
quoted
The only sane alternative is to populate always for mremap() of
VM_LOCKED areas, and document this loss of MLOCK_ONFAULT information
as a limitation of mlock2(MLOCK_ONFAULT). Which might or might not
be enough for Eric's usecase, but it's somewhat ugly.
I don't think that this is the right solution, I would be really
surprised as a user if an area I locked with MLOCK_ONFAULT was then
fully locked and prepopulated after mremap().
If mremap is the only problem then we can add opposite flag for it:
"MREMAP_NOPOPULATE"
- do not populate new segment of locked areas
- do not copy normal areas if possible (anonymous/special must be copied)
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
addr2 = mremap(addr, len, 2 * len, MREMAP_NOPOPULATE);
...
But with this, the user must remember what areas are locked with
MLOCK_LOCKONFAULT and which are locked the with prepopulate so the
correct mremap flags can be used.
Yep. Shouldn't be hard. You anyway have to do some changes in user-space.
Sorry if I wasn't clear enough in my last reply, I think forcing
userspace to track this is the wrong choice. The VM system is
responsible for tracking these attributes and should continue to be.
Userspace tracks addresses and sizes of these areas. Plus mremap obviously
works only with page granularity so memory allocator in userspace have to know
a lot about these structures. So keeping one more bit isn't a rocket science.
Fair enough, however, my current implementation does not require that
userspace keep track of any extra information. With the VM_LOCKONFAULT
flag mremap() keeps the properties that were set with mlock() or
equivalent across remaps.
quoted
quoted
Much simpler for users-pace solution is a mm-wide flag which turns all further
mlocks and MAP_LOCKED into lock-on-fault. Something like
mlockall(MCL_NOPOPULATE_LOCKED).
This set certainly adds the foundation for such a change if you think it
would be useful. That particular behavior was not part of my inital use
case though.
This looks like much easier solution: you don't need new syscall and after
enabling that lock-on-fault mode userspace still can get old behaviour simply
by touching newly locked area.
Again, this suggestion requires that userspace know more about VM than
with my implementation and will require it to walk an entire mapping
before use to fault it in if required. With the current implementation,
mlock continues to function as it has, with the additional flexibility
of being able to request that areas not be prepopulated.
From: Michal Hocko <mhocko@kernel.org> Date: 2015-08-25 13:42:00
On Fri 21-08-15 14:31:32, Eric B Munson wrote:
[...]
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
Yes mremap is a problem and it is very much similar to mmap(MAP_LOCKED).
It doesn't guarantee the full mlock semantic because it leaves partially
populated ranges behind without reporting any error.
Considering the current behavior I do not thing it would be terrible
thing to do what Konstantin was suggesting and populate only the full
ranges in a best effort mode (it is done so anyway) and document the
behavior properly.
"
If the memory segment specified by old_address and old_size is
locked (using mlock(2) or similar), then this lock is maintained
when the segment is resized and/or relocated. As a consequence,
the amount of memory locked by the process may change.
If the range is already fully populated and the range is
enlarged the new range is attempted to be fully populated
as well to preserve the full mlock semantic but there is no
guarantee this will succeed. Partially populated (e.g. created by
mlock(MLOCK_ONFAULT)) ranges do not have the full mlock semantic
so they are not populated on resize.
"
So what we have as a result is that partially populated ranges are
preserved and fully populated ones work in the best effort mode the same
way as they are now.
Does that sound at least remotely reasonably?
--
Michal Hocko
SUSE Labs
On Fri 21-08-15 14:31:32, Eric B Munson wrote:
[...]
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
Yes mremap is a problem and it is very much similar to mmap(MAP_LOCKED).
It doesn't guarantee the full mlock semantic because it leaves partially
populated ranges behind without reporting any error.
Hm, that's right.
Considering the current behavior I do not thing it would be terrible
thing to do what Konstantin was suggesting and populate only the full
ranges in a best effort mode (it is done so anyway) and document the
behavior properly.
"
If the memory segment specified by old_address and old_size is
locked (using mlock(2) or similar), then this lock is maintained
when the segment is resized and/or relocated. As a consequence,
the amount of memory locked by the process may change.
If the range is already fully populated and the range is
enlarged the new range is attempted to be fully populated
as well to preserve the full mlock semantic but there is no
guarantee this will succeed. Partially populated (e.g. created by
mlock(MLOCK_ONFAULT)) ranges do not have the full mlock semantic
so they are not populated on resize.
"
So what we have as a result is that partially populated ranges are
preserved and fully populated ones work in the best effort mode the same
way as they are now.
Does that sound at least remotely reasonably?
I'll basically repeat what I said earlier:
- mremap scanning existing pte's to figure out the population would slow it down for no good reason
- it would be unreliable anyway:
- example: was the area completely populated because MLOCK_ONFAULT was not used or because the process faulted it already
- example: was the area not completely populated because MLOCK_ONFAULT was used, or because mmap(MAP_LOCKED) failed to populate it fully?
I think the first point is a pointless regression for workloads that use just plain mlock() and don't want the onfault semantics. Unless there's some shortcut? Does vma have a counter of how much is populated? (I don't think so?)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Konstantin Khlebnikov <hidden> Date: 2015-08-25 13:58:52
On Tue, Aug 25, 2015 at 4:41 PM, Michal Hocko [off-list ref] wrote:
On Fri 21-08-15 14:31:32, Eric B Munson wrote:
[...]
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
Yes mremap is a problem and it is very much similar to mmap(MAP_LOCKED).
It doesn't guarantee the full mlock semantic because it leaves partially
populated ranges behind without reporting any error.
Considering the current behavior I do not thing it would be terrible
thing to do what Konstantin was suggesting and populate only the full
ranges in a best effort mode (it is done so anyway) and document the
behavior properly.
"
If the memory segment specified by old_address and old_size is
locked (using mlock(2) or similar), then this lock is maintained
when the segment is resized and/or relocated. As a consequence,
the amount of memory locked by the process may change.
If the range is already fully populated and the range is
enlarged the new range is attempted to be fully populated
as well to preserve the full mlock semantic but there is no
guarantee this will succeed. Partially populated (e.g. created by
mlock(MLOCK_ONFAULT)) ranges do not have the full mlock semantic
so they are not populated on resize.
"
So what we have as a result is that partially populated ranges are
preserved and fully populated ones work in the best effort mode the same
way as they are now.
Does that sound at least remotely reasonably?
The problem is that mremap have to scan ptes to detect that and old behaviour
becomes very fragile: one fail and mremap will never populate that vma again.
For now I think new flag "MREMAP_NOPOPULATE" is a better option.
--
Michal Hocko
SUSE Labs
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo-Bw31MaZKKs0EbZ0PF+XxCw@public.gmane.org For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org"> email-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org </a>
From: Eric B Munson <hidden> Date: 2015-08-25 14:29:07
On Tue, 25 Aug 2015, Michal Hocko wrote:
On Fri 21-08-15 14:31:32, Eric B Munson wrote:
[...]
quoted
I am in the middle of implementing lock on fault this way, but I cannot
see how we will hanlde mremap of a lock on fault region. Say we have
the following:
addr = mmap(len, MAP_ANONYMOUS, ...);
mlock(addr, len, MLOCK_ONFAULT);
...
mremap(addr, len, 2 * len, ...)
There is no way for mremap to know that the area being remapped was lock
on fault so it will be locked and prefaulted by remap. How can we avoid
this without tracking per vma if it was locked with lock or lock on
fault?
Yes mremap is a problem and it is very much similar to mmap(MAP_LOCKED).
It doesn't guarantee the full mlock semantic because it leaves partially
populated ranges behind without reporting any error.
This was not my concern. Instead, I was wondering how to keep lock on
fault sematics with mremap if we do not have a VMA flag. As a user, it
would surprise me if a region I mlocked with lock on fault and then
remapped to a larger size was fully populated and locked by the mremap
call.
Considering the current behavior I do not thing it would be terrible
thing to do what Konstantin was suggesting and populate only the full
ranges in a best effort mode (it is done so anyway) and document the
behavior properly.
"
If the memory segment specified by old_address and old_size is
locked (using mlock(2) or similar), then this lock is maintained
when the segment is resized and/or relocated. As a consequence,
the amount of memory locked by the process may change.
If the range is already fully populated and the range is
enlarged the new range is attempted to be fully populated
as well to preserve the full mlock semantic but there is no
guarantee this will succeed. Partially populated (e.g. created by
mlock(MLOCK_ONFAULT)) ranges do not have the full mlock semantic
so they are not populated on resize.
"
You are proposing that mremap would scan the PTEs as Vlastimil has
suggested?
So what we have as a result is that partially populated ranges are
preserved and fully populated ones work in the best effort mode the same
way as they are now.
Does that sound at least remotely reasonably?
--
Michal Hocko
SUSE Labs
From: Michal Hocko <mhocko@kernel.org> Date: 2015-08-25 14:29:21
On Tue 25-08-15 15:55:46, Vlastimil Babka wrote:
On 08/25/2015 03:41 PM, Michal Hocko wrote:
[...]
quoted
So what we have as a result is that partially populated ranges are
preserved and fully populated ones work in the best effort mode the same
way as they are now.
Does that sound at least remotely reasonably?
I'll basically repeat what I said earlier:
- mremap scanning existing pte's to figure out the population would slow it
down for no good reason
So do we really need to populate the enlarged range? All the man page is
saying is that the lock is maintained. Which will be still the case. It
is true that the failure is unlikely (unless you are running in the
memcg) but you cannot rely on the full mlock semantic so what would be a
problem?
- it would be unreliable anyway:
- example: was the area completely populated because MLOCK_ONFAULT was not
used or because the process faulted it already
OK, I see this as being a problem. Especially if the buffer is increase
2*original_len
- example: was the area not completely populated because MLOCK_ONFAULT was
used, or because mmap(MAP_LOCKED) failed to populate it fully?
What would be the difference? Both are ONFAULT now.
I think the first point is a pointless regression for workloads that use
just plain mlock() and don't want the onfault semantics. Unless there's some
shortcut? Does vma have a counter of how much is populated? (I don't think
so?)
--
Michal Hocko
SUSE Labs
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
From: Michal Hocko <mhocko@kernel.org> Date: 2015-08-25 18:58:35
On Tue 25-08-15 10:29:02, Eric B Munson wrote:
On Tue, 25 Aug 2015, Michal Hocko wrote:
[...]
quoted
Considering the current behavior I do not thing it would be terrible
thing to do what Konstantin was suggesting and populate only the full
ranges in a best effort mode (it is done so anyway) and document the
behavior properly.
"
If the memory segment specified by old_address and old_size is
locked (using mlock(2) or similar), then this lock is maintained
when the segment is resized and/or relocated. As a consequence,
the amount of memory locked by the process may change.
If the range is already fully populated and the range is
enlarged the new range is attempted to be fully populated
as well to preserve the full mlock semantic but there is no
guarantee this will succeed. Partially populated (e.g. created by
mlock(MLOCK_ONFAULT)) ranges do not have the full mlock semantic
so they are not populated on resize.
"
You are proposing that mremap would scan the PTEs as Vlastimil has
suggested?
As Vlastimil pointed out this would be unnecessarily too costly. But I
am wondering whether we should populate at all during mremap considering
the full mlock semantic is not guaranteed anyway. Man page mentions only
that the lock is maintained which will be true without population as
well.
If somebody really depends on the current (and broken) implementation we
can offer MREMAP_POPULATE which would do a best effort population. This
would be independent on the locked state and would be usable for other
mappings as well (the usecase would be to save page fault overhead by
batching them).
If this would be seen as an unacceptable user visible change of behavior
then we can go with the VMA flag but I would still prefer to not export
it to the userspace so that we have a way to change this in future.
--
Michal Hocko
SUSE Labs
From: Eric B Munson <hidden> Date: 2015-08-25 19:03:08
On Tue, 25 Aug 2015, Michal Hocko wrote:
On Tue 25-08-15 10:29:02, Eric B Munson wrote:
quoted
On Tue, 25 Aug 2015, Michal Hocko wrote:
[...]
quoted
quoted
Considering the current behavior I do not thing it would be terrible
thing to do what Konstantin was suggesting and populate only the full
ranges in a best effort mode (it is done so anyway) and document the
behavior properly.
"
If the memory segment specified by old_address and old_size is
locked (using mlock(2) or similar), then this lock is maintained
when the segment is resized and/or relocated. As a consequence,
the amount of memory locked by the process may change.
If the range is already fully populated and the range is
enlarged the new range is attempted to be fully populated
as well to preserve the full mlock semantic but there is no
guarantee this will succeed. Partially populated (e.g. created by
mlock(MLOCK_ONFAULT)) ranges do not have the full mlock semantic
so they are not populated on resize.
"
You are proposing that mremap would scan the PTEs as Vlastimil has
suggested?
As Vlastimil pointed out this would be unnecessarily too costly. But I
am wondering whether we should populate at all during mremap considering
the full mlock semantic is not guaranteed anyway. Man page mentions only
that the lock is maintained which will be true without population as
well.
If somebody really depends on the current (and broken) implementation we
can offer MREMAP_POPULATE which would do a best effort population. This
would be independent on the locked state and would be usable for other
mappings as well (the usecase would be to save page fault overhead by
batching them).
If this would be seen as an unacceptable user visible change of behavior
then we can go with the VMA flag but I would still prefer to not export
it to the userspace so that we have a way to change this in future.
Would you drop your objections to the VMA flag if I drop the portions of
the patch that expose it to userspace?
The rework to not use the VMA flag is pretty sizeable and is much more
ugly IMO. I know that you are not wild about using bit 30 of 32 for
this, but perhaps we can settle on not exporting it to userspace so we
can reclaim it if we really need it in the future? I can teach the
folks here to check for size vs RSS of the locked mappings for stats on
lock on fault usage so from my point of view, the proc changes are not
necessary.
From: Michal Hocko <mhocko@kernel.org> Date: 2015-08-26 07:20:22
On Tue 25-08-15 15:03:00, Eric B Munson wrote:
[...]
Would you drop your objections to the VMA flag if I drop the portions of
the patch that expose it to userspace?
The rework to not use the VMA flag is pretty sizeable and is much more
ugly IMO. I know that you are not wild about using bit 30 of 32 for
this, but perhaps we can settle on not exporting it to userspace so we
can reclaim it if we really need it in the future?
Yes, that would be definitely more acceptable for me. I do understand
that you are not wild about changing mremap behavior.
Anyway, I would really prefer if the vma flag was really used only at
few places - when we are clearing it along with VM_LOCKED (which could
be hidden in VM_LOCKED_CLEAR_MASK or something like that) and when we
decide whether the populate or not (this should be __mm_populate). But
maybe I am missing some call paths where gup is called unconditionally,
I haven't checked that.
--
Michal Hocko
SUSE Labs
On Tue 25-08-15 15:03:00, Eric B Munson wrote:
[...]
quoted
Would you drop your objections to the VMA flag if I drop the portions of
the patch that expose it to userspace?
The rework to not use the VMA flag is pretty sizeable and is much more
ugly IMO. I know that you are not wild about using bit 30 of 32 for
this, but perhaps we can settle on not exporting it to userspace so we
can reclaim it if we really need it in the future?
Yes, that would be definitely more acceptable for me. I do understand
that you are not wild about changing mremap behavior.
+1
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>