Skip to contentSkip to conversation

Support volatile for anonymous range

RFC Superseded Minchan Kim 3 participants 10 messages Dec 18, 2012 – Jan 4, 2013

Subsystems (11)
Memory management x86 MM core MGLRU +7 more subsystems

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Support volatile for anonymous range

This is still RFC because we need more input from user-space people and discussion about interface/reclaim policy of volatile pages and I want to expand this concept to tmpfs volatile range if it is possbile without big performance drop of anonymous volatile rnage (Let's define our term. anon volatile VS tmpfs volatile? John?)

NOTE: I didn't consider THP/KSM so for test, you should disable them.

I hope more inputs from user-space allocator people and test patch with their allocator because it might need design change of arena management for getting real vaule.

Changelog from v4

* Add new system call mvolatile/mnovolatile
* Add sigbus when user try to access volatile range
* Rebased on v3.7
* Applied bug fix from John Stultz, Thanks!

Changelog from v3

* Removing madvise(addr, length, MADV_NOVOLATILE).
* add vmstat about the number of discarded volatile pages
* discard volatile pages without promotion in reclaim path

This is based on v3.7

- What's the mvolatile(addr, length)?

It's a hint that user deliver to kernel so kernel can *discard* pages in a range anytime.

- What happens if user access page(ie, virtual address) discarded by kernel?

The user can encounter SIGBUS.

- What should user do for avoding SIGBUS?
He should call mnovolatie(addr, length) before accessing the range which was called by mvolatile.

- What happens if user access page(ie, virtual address) doesn't discarded by kernel?

The user can see old data without page fault.

- What's different with madvise(DONTNEED)?

System call semantic

DONTNEED makes sure user always can see zero-fill pages after he calls madvise while mvolatile can see old data or encounter SIGBUS.

Internal implementation

The madvise(DONTNEED) should zap all mapped pages in range so overhead is increased linearly with the number of mapped pages. Even, if user access zapped pages as write mode, page fault + page allocation + memset should be happened.

The mvolatile just marks the flag in a range(ie, VMA) instead of zapping all of pte in the vma so it doesn't touch ptes any more.

- What's the benefit compared to DONTNEED?

1. The system call overhead is smaller because mvolatile just marks the flag to VMA instead of zapping all the page in a range so overhead should be very small.

2. It has a chance to eliminate overheads (ex, zapping pte + page fault
+ page allocation + memset(PAGE_SIZE)) if memory pressure isn't severe.

3. It has a potential to zap all ptes and free the pages if memory pressure is severe so reclaim overhead could be disappear - TODO

- Isn't there any drawback?

Madvise(DONTNEED) doesn't need exclusive mmap_sem so concurrent page fault of other threads could be allowed. But m[no]volatile needs exclusive mmap_sem so other thread would be blocked if they try to access not-yet-mapped pages. That's why I design m[no]volatile overhead should be small as far as possible.

It could suffer from max rss usage increasement because madvise(DONTNEED) deallocates pages instantly when the system call is issued while mvoatile delays it until memory pressure happens so if memory pressure is severe by max rss incresement, system would suffer. First of all, allocator needs some balance logic for that or kernel might handle it by zapping pages although user calls mvolatile if memory pressure is severe. The problem is how we know memory pressure is severe.
One of solution is to see kswapd is active or not. Another solution is Anton's mempressure so allocator can handle it.

- What's for targetting?

Firstly, user-space allocator like ptmalloc, tcmalloc or heap management of virtual machine like Dalvik. Also, it comes in handy for embedded which doesn't have swap device so they can't reclaim anonymous pages. By discarding instead of swapout, it could be used in the non-swap system. For it, we have to age anon lru list although we don't have swap because I don't want to discard volatile pages by top priority when memory pressure happens as volatile in this patch means "We don't need to swap out because user can handle the situation which data are disappear suddenly", NOT "They are useless so hurry up to reclaim them". So I want to apply same aging rule of nomal pages to them.

Anonymous page background aging of non-swap system would be a trade-off for getting good feature. Even, we had done it two years ago until merge
[1] and I believe gain of this patch will beat loss of anon lru aging's overead once all of allocator start to use madvise.
(This patch doesn't include background aging in case of non-swap system but it's trivial if we decide)

As another choice, we can zap the range like madvise(DONTNEED) when mvolatile is called if we don't have swap space.

- Stupid performance test
I attach test program/script which are utter crap and I don't expect current smart allocator never have done it so we need more practical data with real allocator.

KVM - 8 core, 2G

VOLATILE test
13.16user 7.58system 0:06.04elapsed 343%CPU (0avgtext+0avgdata 2624096maxresident)k 0inputs+0outputs (0major+164050minor)pagefaults 0swaps

DONTNEED test
23.30user 228.92system 0:33.10elapsed 762%CPU (0avgtext+0avgdata 213088maxresident)k 0inputs+0outputs (0major+16384210minor)pagefaults 0swaps

x86-64 - 12 core, 2G

VOLATILE test
33.38user 0.44system 0:02.87elapsed 1178%CPU (0avgtext+0avgdata 3935008maxresident)k 0inputs+0outputs (0major+245989minor)pagefaults 0swaps

DONTNEED test
28.02user 41.25system 0:05.80elapsed 1192%CPU (0avgtext+0avgdata 387776maxresident)k

[1] 74e3f3c3, vmscan: prevent background aging of anon page in no swap system

Any comments are welcome!

Cc Michael Kerrisk <[email protected]>
Cc Arun Sharma <[email protected]>
Cc Paul Turner <[email protected]>
CC David Rientjes <[email protected]>
Cc John Stultz <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Android Kernel Team <[email protected]>
Cc Robert Love <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Neil Brown <[email protected]>
Cc Mike Hommey <[email protected]>
Cc Taras Glek <[email protected]>
Cc KOSAKI Motohiro <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>

Minchan Kim (3):
Introduce new system call mvolatile
Discard volatile page
add PGVOLATILE vmstat count

 arch/x86/syscalls/syscall_64.tbl |    3 +-
 include/linux/mm.h               |    1 +
 include/linux/mm_types.h         |    2 +
 include/linux/rmap.h             |    3 +
 include/linux/syscalls.h         |    2 +
 include/linux/vm_event_item.h    |    2 +-
 mm/Makefile                      |    4 +-
 mm/huge_memory.c                 |    9 +-
 mm/ksm.c                         |    3 +-
 mm/memory.c                      |    2 +
 mm/migrate.c                     |    6 +-
 mm/mlock.c                       |    5 +-
 mm/mmap.c                        |    2 +-
 mm/mvolatile.c                   |  396 ++++++++++++++++++++++++++++++++++++++
 mm/rmap.c                        |   97 +++++++++-
 mm/vmscan.c                      |    4 +
 mm/vmstat.c                      |    1 +
 17 files changed, 527 insertions(+), 15 deletions(-)
 create mode 100644 mm/mvolatile.c

================== 8< =============================

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>

#define SYS_mvolatile 313
#define SYS_mnovolatile 314

#define ALLOC_SIZE (8 << 20)
#define MAP_SIZE (ALLOC_SIZE * 10)
#define PAGE_SIZE (1 << 12)
#define RETRY 100

pthread_barrier_t barrier;
int mode;
#define VOLATILE_MODE 1

static int mvolatile(void *addr, size_t length)

{
	return syscall(SYS_mvolatile, addr, length);
}

static int mnovolatile(void *addr, size_t length)

{
	return syscall(SYS_mnovolatile, addr, length);
}

void *thread_entry(void *data)

{
	unsigned long i;
	cpu_set_t set;
	int cpu = *(int*)data;
	void *mmap_area;
	int retry = RETRY;

	CPU_ZERO(&set);
	CPU_SET(cpu, &set);
	sched_setaffinity(0, sizeof(set), &set);

	mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
	mmap_area = mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE,
					MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
	if (mmap_area == MAP_FAILED) {
		fprintf(stderr, "Fail to mmap [%d]\n", *(int*)data);
		exit(1);
	}

	pthread_barrier_wait(&barrier);

	while(retry--) {
		if (mode == VOLATILE_MODE) {
			mvolatile(mmap_area, MAP_SIZE);
			for (i = 0; i < MAP_SIZE; i+= ALLOC_SIZE) {
				mnovolatile(mmap_area + i, ALLOC_SIZE);
				memset(mmap_area + i, i, ALLOC_SIZE);
				mvolatile(mmap_area + i, ALLOC_SIZE);
			}
		} else {
			for (i = 0; i < MAP_SIZE; i += ALLOC_SIZE) {
				memset(mmap_area + i, i, ALLOC_SIZE);
				madvise(mmap_area + i, ALLOC_SIZE, MADV_DONTNEED);
			}
		}
	}
	return NULL;
}

int main(int argc, char *argv[])

{
	int i, nr_thread;
	int *data;

	if (argc < 3)
		return 1;

	nr_thread = atoi(argv[1]);
	mode = atoi(argv[2]);

	pthread_t *thread = malloc(sizeof(pthread_t) * nr_thread);
	data = malloc(sizeof(int) * nr_thread);
	pthread_barrier_init(&barrier, NULL, nr_thread);

	for (i = 0; i < nr_thread; i++) {
		data[i] = i;
		if (pthread_create(&thread[i], NULL, thread_entry, &data[i])) {
			perror("Fail to create thread\n");
			exit(1);
		}
	}

	for (i = 0; i < nr_thread; i++) {
		if (pthread_join(thread[i], NULL))
			perror("Fail to join thread\n");
		printf("[%d] thread done\n", i);
	}

	return 0;
}
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

RFC v4 1/3 Introduce new system call mvolatile

This patch adds new system call m[no]volatile. If some user asks is_volatile system call, it could, too.

The reason why I introduced new system call instead of madvise is m[no]volatile vma handling is totally different with madvise's vma handling.

1) The m[no]volatile should be successful although the range includes unmapped or non-volatile range. It just skips such range without stop with returning error although it encounter invalid range.
It makes user convenient without calling several calling of small range.
- Suggested by John Stultz

2) The propagation of purged state between vmas should be atomic between m[no]volatile and reclaim. For it, we need to tweak vma_merge/split_vma's anon_vma handling. It's very common operation and I don't want to add unnecessary overhead and code if it is possbile.

3) The purged state of volatile range should be propagated out to user with mnovolatile operation and it should be atomic with reclaim, too.

For meeting above requirements, I introudced new system call m[no]volatile. It doesn't change vma_merge/split and repair vmas after vma operation.

So mvolatile(start, len)'s semantics is following as.

1) It makes range(start, len) as volatile although the range includes unmapped area, speacial mapping and mlocked area which are just skipped. Now it doesn't support Hugepage and KSM. - TODO
Return -EINVAL if range doesn't include a right vma at all. Return -ENOMEM with interrupting range opeartion if memory is not enough to merge/split vmas. In this case, some range would be volatile and others not. So user have to recall mvolatile after he cancel all range by mnovolatile.
Return 0 if range consists of only proper vmas.
Return 1 if part of range includes hole/huge/ksm/mlock/special area.

2) If user calls mvolatile to the range which was already volatile VMA and even purged state, VOLATILE attributes still remains but purged state is reset. I expect some user want to split volatile vma into smaller ranges. Although he can do it for mnovlatile(whole range) and serveral calling with movlatile(smaller range), this function can avoid mnovolatile if he doesn't care purged state. I'm not sure we really need this function so I hope listen opinions. Unfortunately, current implemenation doesn't split volatile VMA with new range in this case. I forgot implementing it in this version but decide to send it to listen opinions because implementing is rather trivial if we decided.

mnovolatile(start, len)'s semantics is following as.

1) It makes range(start, len) as volatile although the range includes unmapped area, speacial mapping and non-volatile range which are just skipped.

2) If the range is purged, it will return 1 regardless of including invalid range.

3) It returns -ENOMEM if system doesn't have enough memory for vma operation.

4) It returns -EINVAL if range doesn't include a right vma at all.

5) If user try to access purged range without mnovoatile call, it encounters SIGBUS which would show up next patch.

Cc Michael Kerrisk <[email protected]>
Cc Arun Sharma <[email protected]>
Cc Paul Turner <[email protected]>
CC David Rientjes <[email protected]>
Cc John Stultz <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Android Kernel Team <[email protected]>
Cc Robert Love <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Neil Brown <[email protected]>
Cc Mike Hommey <[email protected]>
Cc Taras Glek <[email protected]>
Cc KOSAKI Motohiro <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
---
 arch/x86/syscalls/syscall_64.tbl |    3 +-
 include/linux/mm.h               |    1 +
 include/linux/mm_types.h         |    2 +
 include/linux/syscalls.h         |    2 +
 mm/Makefile                      |    4 +-
 mm/huge_memory.c                 |    9 +-
 mm/ksm.c                         |    3 +-
 mm/mlock.c                       |    5 +-
 mm/mmap.c                        |    2 +-
 mm/mvolatile.c                   |  396 ++++++++++++++++++++++++++++++++++++++
 mm/rmap.c                        |    2 +
 11 files changed, 419 insertions(+), 10 deletions(-)
 create mode 100644 mm/mvolatile.c
arch/x86/syscalls/syscall_64.tbl +2 -1
index a582bfe..7da9c4a 100644
@@ -319,7 +319,8 @@
 310	64	process_vm_readv	sys_process_vm_readv
 311	64	process_vm_writev	sys_process_vm_writev
 312	common	kcmp			sys_kcmp
-
+313	common	mvolatile		sys_mvolatile
+314	common	mnovolatile		sys_mnovolatile
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
 # for native 64-bit operation.
include/linux/mm.h +1 -0
index bcaab4e..94742c4 100644
@@ -87,6 +87,7 @@ extern unsigned int kobjsize(const void *objp);
 #define VM_PFNMAP	0x00000400	/* Page-ranges managed without "struct page", just pure PFN */
 #define VM_DENYWRITE	0x00000800	/* ETXTBSY on write attempts.. */
 
+#define VM_VOLATILE	0x00001000	/* Pages could be discarded without swapout */
 #define VM_LOCKED	0x00002000
 #define VM_IO           0x00004000	/* Memory mapped I/O or similar */
 
include/linux/mm_types.h +2 -0
index 31f8a3a..ef2a4a4 100644
@@ -275,6 +275,8 @@ struct vm_area_struct {
 #ifdef CONFIG_NUMA
 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
 #endif
+	/* Page in this vma was discarded*/
+	bool purged;			/* Serialized by anon_vma's mutex */
 };
 
 struct core_thread {
include/linux/syscalls.h +2 -0
index 727f0cd..a8ded1c 100644
@@ -470,6 +470,8 @@ asmlinkage long sys_munlock(unsigned long start, size_t len);
 asmlinkage long sys_mlockall(int flags);
 asmlinkage long sys_munlockall(void);
 asmlinkage long sys_madvise(unsigned long start, size_t len, int behavior);
+asmlinkage long sys_mvolatile(unsigned long start, size_t len);
+asmlinkage long sys_mnovolatile(unsigned long start, size_t len);
 asmlinkage long sys_mincore(unsigned long start, size_t len,
 				unsigned char __user * vec);
 
mm/Makefile +2 -2
index 6b025f8..962b69f 100644
@@ -4,8 +4,8 @@
 
 mmu-y			:= nommu.o
 mmu-$(CONFIG_MMU)	:= fremap.o highmem.o madvise.o memory.o mincore.o \
-			   mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \
-			   vmalloc.o pagewalk.o pgtable-generic.o
+			   mvolatile.o mlock.o mmap.o mprotect.o mremap.o msync.o \
+			   rmap.o vmalloc.o pagewalk.o pgtable-generic.o
 
 ifdef CONFIG_CROSS_MEMORY_ATTACH
 mmu-$(CONFIG_MMU)	+= process_vm_access.o
mm/huge_memory.c +6 -3
index 40f17c3..3fe062d 100644
@@ -1477,7 +1477,7 @@ out:
 	return ret;
 }
 
-#define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
+#define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE|VM_VOLATILE)
 
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
@@ -1641,8 +1641,11 @@ int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
 		 * page fault if needed.
 		 */
 		return 0;
-	if (vma->vm_ops)
-		/* khugepaged not yet working on file or special mappings */
+	if (vma->vm_ops || vma->vm_flags & VM_VOLATILE)
+		/*
+		 * khugepaged not yet working on file,special mappings
+		 * and volatile.
+		 */
 		return 0;
 	VM_BUG_ON(vma->vm_flags & VM_NO_THP);
 	hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
mm/ksm.c +2 -1
index ae539f0..2775f59 100644
@@ -1486,7 +1486,8 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
 		 */
 		if (*vm_flags & (VM_MERGEABLE | VM_SHARED  | VM_MAYSHARE   |
 				 VM_PFNMAP    | VM_IO      | VM_DONTEXPAND |
-				 VM_HUGETLB | VM_NONLINEAR | VM_MIXEDMAP))
+				 VM_HUGETLB | VM_NONLINEAR | VM_MIXEDMAP   |
+				 VM_VOLATILE))
 			return 0;		/* just ignore the advice */
 
 #ifdef VM_SAO
mm/mlock.c +3 -2
index f0b9ce5..db3a477 100644
@@ -316,8 +316,9 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,
 	int ret = 0;
 	int lock = !!(newflags & VM_LOCKED);
 
-	if (newflags == vma->vm_flags || (vma->vm_flags & VM_SPECIAL) ||
-	    is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm))
+	if (newflags == vma->vm_flags || (vma->vm_flags &
+		 (VM_SPECIAL|VM_VOLATILE)) || is_vm_hugetlb_page(vma) ||
+		 vma == get_gate_vma(current->mm))
 		goto out;	/* don't set VM_LOCKED,  don't count */
 
 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
mm/mmap.c +1 -1
index 9a796c4..e4ac12d 100644
@@ -808,7 +808,7 @@ struct vm_area_struct *vma_merge(struct mm_struct *mm,
 	 * We later require that vma->vm_flags == vm_flags,
 	 * so this tests vma->vm_flags & VM_SPECIAL, too.
 	 */
-	if (vm_flags & VM_SPECIAL)
+	if (vm_flags & (VM_SPECIAL|VM_VOLATILE))
 		return NULL;
 
 	if (prev)
mm/mvolatile.c new file +396 -0

396 lines not on this page · raw message

mm/rmap.c +2 -0
index 2ee1ef0..7f4493c 100644
@@ -308,6 +308,8 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
 	vma->anon_vma = anon_vma;
 	anon_vma_lock(anon_vma);
 	anon_vma_chain_link(vma, avc, anon_vma);
+	/* Propagate parent's purged state to child */
+	vma->purged = pvma->purged;
 	anon_vma_unlock(anon_vma);
 
 	return 0;
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

RFC v4 2/3 Discard volatile page

In reply to Minchan Kim

VM don't need to swap out volatile pages. Instead, it just discards pages and set true to the vma's purge state so if user try to access purged vma without calling mnovolatile, it will encounter SIGBUS.

Reclaimer reclaims volatile page when it reaches tail of LRU regardless of the recent reference. So when the memory pressure doesn't happen, it wouldn't be evicted so it can reduce the number of minor fault. Although memory pressure happens, it doesn't be evicted until it reaches tail of LRU. It could mitigate fault/data-regenaration overhead if memory pressure isn't severe. But it's not solid design and need more discussion.

Cc Michael Kerrisk <[email protected]>
Cc Arun Sharma <[email protected]>
Cc Paul Turner <[email protected]>
CC David Rientjes <[email protected]>
Cc John Stultz <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Android Kernel Team <[email protected]>
Cc Robert Love <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Neil Brown <[email protected]>
Cc Mike Hommey <[email protected]>
Cc Taras Glek <[email protected]>
Cc KOSAKI Motohiro <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
---
 include/linux/rmap.h |    3 ++
 mm/memory.c          |    2 ++
 mm/migrate.c         |    6 ++--
 mm/rmap.c            |   95 ++++++++++++++++++++++++++++++++++++++++++++++++--
 mm/vmscan.c          |    3 ++
 5 files changed, 105 insertions(+), 4 deletions(-)
include/linux/rmap.h +3 -0
index bfe1f47..ed263bb 100644
@@ -80,6 +80,8 @@ enum ttu_flags {
 	TTU_IGNORE_MLOCK = (1 << 8),	/* ignore mlock */
 	TTU_IGNORE_ACCESS = (1 << 9),	/* don't age */
 	TTU_IGNORE_HWPOISON = (1 << 10),/* corrupted page is recoverable */
+	/* ignore volatile. Should be revisit to handle migration entry */
+	TTU_IGNORE_VOLATILE = (1 << 11),
 };
 
 #ifdef CONFIG_MMU
@@ -261,5 +263,6 @@ static inline int page_mkclean(struct page *page)
 #define SWAP_AGAIN	1
 #define SWAP_FAIL	2
 #define SWAP_MLOCK	3
+#define SWAP_DISCARD	4
 
 #endif	/* _LINUX_RMAP_H */
mm/memory.c +2 -0
index 221fc9f..71e06fe 100644
@@ -3459,6 +3459,8 @@ int handle_pte_fault(struct mm_struct *mm,
 					return do_linear_fault(mm, vma, address,
 						pte, pmd, flags, entry);
 			}
+			if (unlikely(vma->vm_flags & VM_VOLATILE))
+				return VM_FAULT_SIGBUS;
 			return do_anonymous_page(mm, vma, address,
 						 pte, pmd, flags);
 		}
mm/migrate.c +4 -2
index 77ed2d7..bf9d76a 100644
@@ -800,7 +800,8 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	}
 
 	/* Establish migration ptes or remove ptes */
-	try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
+	try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|
+				TTU_IGNORE_ACCESS|TTU_IGNORE_VOLATILE);
 
 skip_unmap:
 	if (!page_mapped(page))
@@ -915,7 +916,8 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 	if (PageAnon(hpage))
 		anon_vma = page_get_anon_vma(hpage);
 
-	try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
+	try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|
+			TTU_IGNORE_ACCESS|TTU_IGNORE_VOLATILE);
 
 	if (!page_mapped(hpage))
 		rc = move_to_new_page(new_hpage, hpage, 1, mode);
mm/rmap.c +93 -2

132 lines not on this page · raw message

mm/vmscan.c +3 -0
index b7ed376..cfe95d3 100644
@@ -793,6 +793,8 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		 */
 		if (page_mapped(page) && mapping) {
 			switch (try_to_unmap(page, ttu_flags)) {
+			case SWAP_DISCARD:
+				goto discard_page;
 			case SWAP_FAIL:
 				goto activate_locked;
 			case SWAP_AGAIN:
@@ -861,6 +863,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 			}
 		}
 
+discard_page:
 		/*
 		 * If the page has buffers, try to free the buffer mappings
 		 * associated with this page. If we succeed we try to free
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw

RFC v4 3/3 add PGVOLATILE vmstat count

In reply to Minchan Kim

This patch add pgvolatile vmstat so admin can see how many of volatile pages are discarded by VM until now. It could be a good indicator of patch effect during test but still not sure we need it in real practice. Will rethink it.

Cc Michael Kerrisk <[email protected]>
Cc Arun Sharma <[email protected]>
Cc Paul Turner <[email protected]>
CC David Rientjes <[email protected]>
Cc John Stultz <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Android Kernel Team <[email protected]>
Cc Robert Love <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Neil Brown <[email protected]>
Cc Mike Hommey <[email protected]>
Cc Taras Glek <[email protected]>
Cc KOSAKI Motohiro <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
---
 include/linux/vm_event_item.h |    2 +-
 mm/vmscan.c                   |    1 +
 mm/vmstat.c                   |    1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
include/linux/vm_event_item.h +1 -1
index 3d31145..f83c3d2 100644
@@ -23,7 +23,7 @@
 
 enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
 		FOR_ALL_ZONES(PGALLOC),
-		PGFREE, PGACTIVATE, PGDEACTIVATE,
+		PGFREE, PGVOLATILE, PGACTIVATE, PGDEACTIVATE,
 		PGFAULT, PGMAJFAULT,
 		FOR_ALL_ZONES(PGREFILL),
 		FOR_ALL_ZONES(PGSTEAL_KSWAPD),
mm/vmscan.c +1 -0
index cfe95d3..1ec7345 100644
@@ -794,6 +794,7 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		if (page_mapped(page) && mapping) {
 			switch (try_to_unmap(page, ttu_flags)) {
 			case SWAP_DISCARD:
+				count_vm_event(PGVOLATILE);
 				goto discard_page;
 			case SWAP_FAIL:
 				goto activate_locked;
mm/vmstat.c +1 -0
index c737057..9fd8ead 100644
@@ -747,6 +747,7 @@ const char * const vmstat_text[] = {
 	TEXTS_FOR_ZONES("pgalloc")
 
 	"pgfree",
+	"pgvolatile",
 	"pgactivate",
 	"pgdeactivate",
 
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw
In reply to Minchan Kim
Quoted 3 lines · Minchan Kim

I hope more inputs from user-space allocator people and test patch with their allocator because it might need design change of arena management for getting real vaule.

jemalloc knows how to handle MADV_FREE on platforms that support it. This looks similar (we'll need a SIGBUS handler that does the right thing = zero the page + mark it as non-volatile in the common case).

All of this of course assumes that apps madvise the kernel through APIs exposed by the malloc implementation - not via a raw syscall.

In other words, some new user space code needs to be written to test this out fully. Sounds feasible though.

-Arun

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
Quoted 9 lines · Arun Sharma
Quoted 3 lines · Minchan Kim

I hope more inputs from user-space allocator people and test patch with their allocator because it might need design change of arena management for getting real vaule.

jemalloc knows how to handle MADV_FREE on platforms that support it. This looks similar (we'll need a SIGBUS handler that does the right thing = zero the page + mark it as non-volatile in the common case).

Don't work because it's too late to mark it as non-volatile in signal handler in case of malloc.

For example,
free(P1-P4) -> mvolatile(P1-P4) -> VM discard(P3) -> alloc(P1-P4) -> use P1 -> VM discard(P1) -> use P3 -> SIGBUS -> mark nonvolatile -> lost P1.

So, we should call mnovolatile before giving the free space to user.

Quoted 5 lines

All of this of course assumes that apps madvise the kernel through APIs exposed by the malloc implementation - not via a raw syscall.

In other words, some new user space code needs to be written to test

Agreed. I might want to design new allocator with this system calls if existing allocators cannot use this system calls efficiently because it might need allocator's design change. MADV_FREE/MADV_DONTNEED isn't cheap due to enumerating ptes/page descriptors in that range to mark something so I guess allocator avoids frequent calling of the such advise system call and even if they call it, they want to call the big range as batch. Just my imagine.

But mvolatile/mnovolatile is cheaper so you can call it more frequently with smaller range so VM could have easy-reclaimable pages easily. Another benefit of the mvolatile is it can change the behavior when memory pressure is severe where it can zap all pages like DONTNEED so it could work very flexible.
The downside of that approach is that if we call it with small range, it can increase the number of VMA so we might tune point for VMA size.

Quoted 1 line

this out fully. Sounds feasible though.

Thanks!

Quoted 8 lines

-Arun

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 8 lines
Kind regards,
Minchan Kim

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw
In reply to Minchan Kim
Quoted 25 lines · Minchan Kim

This is still RFC because we need more input from user-space people and discussion about interface/reclaim policy of volatile pages and I want to expand this concept to tmpfs volatile range if it is possbile without big performance drop of anonymous volatile rnage (Let's define our term. anon volatile VS tmpfs volatile? John?)

NOTE: I didn't consider THP/KSM so for test, you should disable them.

I hope more inputs from user-space allocator people and test patch with their allocator because it might need design change of arena management for getting real vaule.

Changelog from v4

* Add new system call mvolatile/mnovolatile
* Add sigbus when user try to access volatile range
* Rebased on v3.7
* Applied bug fix from John Stultz, Thanks!

Changelog from v3

* Removing madvise(addr, length, MADV_NOVOLATILE).
* add vmstat about the number of discarded volatile pages
* discard volatile pages without promotion in reclaim path

Quoted 7 lines

This is based on v3.7

- What's the mvolatile(addr, length)?

It's a hint that user deliver to kernel so kernel can *discard* pages in a range anytime.

This can work against both of PRIVATE and SHARED mapping ?

What happens at fork() ? VOLATILE ranges are copied ?

Quoted 9 lines

- What happens if user access page(ie, virtual address) discarded by kernel?

The user can encounter SIGBUS.

- What should user do for avoding SIGBUS?
He should call mnovolatie(addr, length) before accessing the range which was called by mvolatile.

Will mnovolatile() return whether the range is discarded or not ?

What the user should do in signal handler ?
Can the all expected opereations be done in signal-safe manner ? (IOW, can user do enough job easily without taking any locks in userland ?)

Quoted 5 lines

- What happens if user access page(ie, virtual address) doesn't discarded by kernel?

The user can see old data without page fault.

What happens when ther user calls mvolatile() against mlock()'d range or calling mlock() against mvolatile()'d range ?

Hm, by the way, the user need to attach pages to the process by causing page-fault (as you do by memset()) before calling mvolatile() ?

I think your approach is interesting, anyway.

Thanks,
-Kame

Quoted 245 lines

- What's different with madvise(DONTNEED)?

System call semantic

DONTNEED makes sure user always can see zero-fill pages after he calls madvise while mvolatile can see old data or encounter SIGBUS.

Internal implementation

The madvise(DONTNEED) should zap all mapped pages in range so overhead is increased linearly with the number of mapped pages. Even, if user access zapped pages as write mode, page fault + page allocation + memset should be happened.

The mvolatile just marks the flag in a range(ie, VMA) instead of zapping all of pte in the vma so it doesn't touch ptes any more.

- What's the benefit compared to DONTNEED?

1. The system call overhead is smaller because mvolatile just marks the flag to VMA instead of zapping all the page in a range so overhead should be very small.

2. It has a chance to eliminate overheads (ex, zapping pte + page fault
+ page allocation + memset(PAGE_SIZE)) if memory pressure isn't severe.

3. It has a potential to zap all ptes and free the pages if memory pressure is severe so reclaim overhead could be disappear - TODO

- Isn't there any drawback?

Madvise(DONTNEED) doesn't need exclusive mmap_sem so concurrent page fault of other threads could be allowed. But m[no]volatile needs exclusive mmap_sem so other thread would be blocked if they try to access not-yet-mapped pages. That's why I design m[no]volatile overhead should be small as far as possible.

It could suffer from max rss usage increasement because madvise(DONTNEED) deallocates pages instantly when the system call is issued while mvoatile delays it until memory pressure happens so if memory pressure is severe by max rss incresement, system would suffer. First of all, allocator needs some balance logic for that or kernel might handle it by zapping pages although user calls mvolatile if memory pressure is severe. The problem is how we know memory pressure is severe.
One of solution is to see kswapd is active or not. Another solution is Anton's mempressure so allocator can handle it.

- What's for targetting?

Firstly, user-space allocator like ptmalloc, tcmalloc or heap management of virtual machine like Dalvik. Also, it comes in handy for embedded which doesn't have swap device so they can't reclaim anonymous pages. By discarding instead of swapout, it could be used in the non-swap system. For it, we have to age anon lru list although we don't have swap because I don't want to discard volatile pages by top priority when memory pressure happens as volatile in this patch means "We don't need to swap out because user can handle the situation which data are disappear suddenly", NOT "They are useless so hurry up to reclaim them". So I want to apply same aging rule of nomal pages to them.

Anonymous page background aging of non-swap system would be a trade-off for getting good feature. Even, we had done it two years ago until merge
[1] and I believe gain of this patch will beat loss of anon lru aging's overead once all of allocator start to use madvise.
(This patch doesn't include background aging in case of non-swap system but it's trivial if we decide)

As another choice, we can zap the range like madvise(DONTNEED) when mvolatile is called if we don't have swap space.

- Stupid performance test
I attach test program/script which are utter crap and I don't expect current smart allocator never have done it so we need more practical data with real allocator.

KVM - 8 core, 2G

VOLATILE test
13.16user 7.58system 0:06.04elapsed 343%CPU (0avgtext+0avgdata 2624096maxresident)k 0inputs+0outputs (0major+164050minor)pagefaults 0swaps

DONTNEED test
23.30user 228.92system 0:33.10elapsed 762%CPU (0avgtext+0avgdata 213088maxresident)k 0inputs+0outputs (0major+16384210minor)pagefaults 0swaps

   x86-64 - 12 core, 2G

VOLATILE test
33.38user 0.44system 0:02.87elapsed 1178%CPU (0avgtext+0avgdata 3935008maxresident)k 0inputs+0outputs (0major+245989minor)pagefaults 0swaps

DONTNEED test
28.02user 41.25system 0:05.80elapsed 1192%CPU (0avgtext+0avgdata 387776maxresident)k

[1] 74e3f3c3, vmscan: prevent background aging of anon page in no swap system

Any comments are welcome!

Cc Michael Kerrisk <[email protected]>
Cc Arun Sharma <[email protected]>
Cc Paul Turner <[email protected]>
CC David Rientjes <[email protected]>
Cc John Stultz <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Android Kernel Team <[email protected]>
Cc Robert Love <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Neil Brown <[email protected]>
Cc Mike Hommey <[email protected]>
Cc Taras Glek <[email protected]>
Cc KOSAKI Motohiro <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>

Minchan Kim (3):
Introduce new system call mvolatile
Discard volatile page
add PGVOLATILE vmstat count

  arch/x86/syscalls/syscall_64.tbl |    3 +-
  include/linux/mm.h               |    1 +
  include/linux/mm_types.h         |    2 +
  include/linux/rmap.h             |    3 +
  include/linux/syscalls.h         |    2 +
  include/linux/vm_event_item.h    |    2 +-
  mm/Makefile                      |    4 +-
  mm/huge_memory.c                 |    9 +-
  mm/ksm.c                         |    3 +-
  mm/memory.c                      |    2 +
  mm/migrate.c                     |    6 +-
  mm/mlock.c                       |    5 +-
  mm/mmap.c                        |    2 +-
  mm/mvolatile.c                   |  396 ++++++++++++++++++++++++++++++++++++++
  mm/rmap.c                        |   97 +++++++++-
  mm/vmscan.c                      |    4 +
  mm/vmstat.c                      |    1 +
  17 files changed, 527 insertions(+), 15 deletions(-)

create mode 100644 mm/mvolatile.c

================== 8< =============================

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>

#define SYS_mvolatile 313
#define SYS_mnovolatile 314

#define ALLOC_SIZE (8 << 20)
#define MAP_SIZE (ALLOC_SIZE * 10)
#define PAGE_SIZE (1 << 12)
#define RETRY 100

pthread_barrier_t barrier;
int mode;
#define VOLATILE_MODE 1

static int mvolatile(void *addr, size_t length)

{
	return syscall(SYS_mvolatile, addr, length);
}

static int mnovolatile(void *addr, size_t length)

{
	return syscall(SYS_mnovolatile, addr, length);
}

void *thread_entry(void *data)

{
	unsigned long i;
	cpu_set_t set;
	int cpu = *(int*)data;
	void *mmap_area;
	int retry = RETRY;

	CPU_ZERO(&set);
	CPU_SET(cpu, &set);
	sched_setaffinity(0, sizeof(set), &set);

	mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
	mmap_area = mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE,
					MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
	if (mmap_area == MAP_FAILED) {
		fprintf(stderr, "Fail to mmap [%d]\n", *(int*)data);
		exit(1);
	}

	pthread_barrier_wait(&barrier);

	while(retry--) {
		if (mode == VOLATILE_MODE) {
			mvolatile(mmap_area, MAP_SIZE);
			for (i = 0; i < MAP_SIZE; i+= ALLOC_SIZE) {
				mnovolatile(mmap_area + i, ALLOC_SIZE);
				memset(mmap_area + i, i, ALLOC_SIZE);
				mvolatile(mmap_area + i, ALLOC_SIZE);
			}
		} else {
			for (i = 0; i < MAP_SIZE; i += ALLOC_SIZE) {
				memset(mmap_area + i, i, ALLOC_SIZE);
				madvise(mmap_area + i, ALLOC_SIZE, MADV_DONTNEED);
			}
		}
	}
	return NULL;
}

int main(int argc, char *argv[])

{
	int i, nr_thread;
	int *data;

	if (argc < 3)
		return 1;

	nr_thread = atoi(argv[1]);
	mode = atoi(argv[2]);

	pthread_t *thread = malloc(sizeof(pthread_t) * nr_thread);
	data = malloc(sizeof(int) * nr_thread);
	pthread_barrier_init(&barrier, NULL, nr_thread);

	for (i = 0; i < nr_thread; i++) {
		data[i] = i;
		if (pthread_create(&thread[i], NULL, thread_entry, &data[i])) {
			perror("Fail to create thread\n");
			exit(1);
		}
	}
Quoted 9 lines
	for (i = 0; i < nr_thread; i++) {
		if (pthread_join(thread[i], NULL))
			perror("Fail to join thread\n");
		printf("[%d] thread done\n", i);
	}

	return 0;
}

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

Hi Kame,

What are you doing these holiday season? :) I can't believe you sit down in front of computer.

Quoted 28 lines · Kamezawa Hiroyuki
Quoted 27 lines · Minchan Kim

This is still RFC because we need more input from user-space people and discussion about interface/reclaim policy of volatile pages and I want to expand this concept to tmpfs volatile range if it is possbile without big performance drop of anonymous volatile rnage (Let's define our term. anon volatile VS tmpfs volatile? John?)

NOTE: I didn't consider THP/KSM so for test, you should disable them.

I hope more inputs from user-space allocator people and test patch with their allocator because it might need design change of arena management for getting real vaule.

Changelog from v4

* Add new system call mvolatile/mnovolatile
* Add sigbus when user try to access volatile range
* Rebased on v3.7
* Applied bug fix from John Stultz, Thanks!

Changelog from v3

* Removing madvise(addr, length, MADV_NOVOLATILE).
* add vmstat about the number of discarded volatile pages
* discard volatile pages without promotion in reclaim path

This is based on v3.7

Quoted 7 lines
Quoted 5 lines

- What's the mvolatile(addr, length)?

It's a hint that user deliver to kernel so kernel can *discard* pages in a range anytime.

This can work against both of PRIVATE and SHARED mapping ?

Yes.

Quoted 2 lines

What happens at fork() ? VOLATILE ranges are copied ?

Just child vma would have a VM_VOLATILE flag.
If a page is shared like above, the page could be discarded only when all vmas pointing to the page are VM_VOLATILE.

Quoted 5 lines
Quoted 5 lines

- What happens if user access page(ie, virtual address) discarded by kernel?

Quoted 7 lines
Quoted 6 lines

The user can encounter SIGBUS.

- What should user do for avoding SIGBUS?
He should call mnovolatie(addr, length) before accessing the range which was called by mvolatile.

Will mnovolatile() return whether the range is discarded or not ?

Absolutely.

Quoted 2 lines

What the user should do in signal handler ?

It depends on usecase.
Please read John's mail. http://lwn.net/Articles/518130/
Quote from the link
"
But one interesting new tweak on this design, suggested by the Taras Glek and others at Mozilla, is as follows:

Instead of leaving volatile data access as being undefined , when accessing volatile data, either the data expected will be returned if it has not been purged, or the application will get a SIGBUS when it accesses volatile data that has been purged.

Everything else remains the same (error on marking non-volatile if data was purged, etc). This model allows applications to avoid having to unmark volatile data when it wants to access it, then immediately re-mark it as volatile when its done. It is in effect "lazy" with its marking, allowing the kernel to hit it with a signal when it gets unlucky and touches purged data. From the signal handler, the application can note the address it faulted on, unmark the range, and regenerate the needed data before returning to execution.

Since this approach avoids the more explicit unmark/access/mark pattern, it avoids the extra overhead required to ensure data is non-volatile before being accessed.

However, If applications don't want to deal with handling the sigbus, they can use the more straightforward (but more costly) unmark/access/mark pattern in the same way as my earlier proposals.

This allows folks to balance the cost vs complexity in their application appropriately.

So that's a general overview of how the idea I'm proposing could be used.
"

Quoted 2 lines

Can the all expected opereations be done in signal-safe manner ? (IOW, can user do enough job easily without taking any locks in userland ?)

It depends on design of user application but some user space guys want it so it could be done enoughly, I think. Expecially, Android have used it by ashmem where was another interface for same goal but it works only tmpfs pages but mine is normal anonymous page but the goal is to support both.

Quoted 9 lines
Quoted 6 lines

- What happens if user access page(ie, virtual address) doesn't discarded by kernel?

The user can see old data without page fault.

What happens when ther user calls mvolatile() against mlock()'d range or calling mlock() against mvolatile()'d range ?

-EINVAL

Quoted 3 lines

Hm, by the way, the user need to attach pages to the process by causing page-fault (as you do by memset()) before calling mvolatile() ?

For effectiveness, Yes.

Quoted 2 lines

I think your approach is interesting, anyway.

Thanks for your interest, Kame.

a??a??a? 3/4 a??a?|a??a??a??a??a??.

Quoted 266 lines

Thanks,
-Kame

Quoted 245 lines

- What's different with madvise(DONTNEED)?

System call semantic

DONTNEED makes sure user always can see zero-fill pages after he calls madvise while mvolatile can see old data or encounter SIGBUS.

Internal implementation

The madvise(DONTNEED) should zap all mapped pages in range so overhead is increased linearly with the number of mapped pages. Even, if user access zapped pages as write mode, page fault + page allocation + memset should be happened.

The mvolatile just marks the flag in a range(ie, VMA) instead of zapping all of pte in the vma so it doesn't touch ptes any more.

- What's the benefit compared to DONTNEED?

1. The system call overhead is smaller because mvolatile just marks the flag to VMA instead of zapping all the page in a range so overhead should be very small.

2. It has a chance to eliminate overheads (ex, zapping pte + page fault
+ page allocation + memset(PAGE_SIZE)) if memory pressure isn't severe.

3. It has a potential to zap all ptes and free the pages if memory pressure is severe so reclaim overhead could be disappear - TODO

- Isn't there any drawback?

Madvise(DONTNEED) doesn't need exclusive mmap_sem so concurrent page fault of other threads could be allowed. But m[no]volatile needs exclusive mmap_sem so other thread would be blocked if they try to access not-yet-mapped pages. That's why I design m[no]volatile overhead should be small as far as possible.

It could suffer from max rss usage increasement because madvise(DONTNEED) deallocates pages instantly when the system call is issued while mvoatile delays it until memory pressure happens so if memory pressure is severe by max rss incresement, system would suffer. First of all, allocator needs some balance logic for that or kernel might handle it by zapping pages although user calls mvolatile if memory pressure is severe. The problem is how we know memory pressure is severe.
One of solution is to see kswapd is active or not. Another solution is Anton's mempressure so allocator can handle it.

- What's for targetting?

Firstly, user-space allocator like ptmalloc, tcmalloc or heap management of virtual machine like Dalvik. Also, it comes in handy for embedded which doesn't have swap device so they can't reclaim anonymous pages. By discarding instead of swapout, it could be used in the non-swap system. For it, we have to age anon lru list although we don't have swap because I don't want to discard volatile pages by top priority when memory pressure happens as volatile in this patch means "We don't need to swap out because user can handle the situation which data are disappear suddenly", NOT "They are useless so hurry up to reclaim them". So I want to apply same aging rule of nomal pages to them.

Anonymous page background aging of non-swap system would be a trade-off for getting good feature. Even, we had done it two years ago until merge
[1] and I believe gain of this patch will beat loss of anon lru aging's overead once all of allocator start to use madvise.
(This patch doesn't include background aging in case of non-swap system but it's trivial if we decide)

As another choice, we can zap the range like madvise(DONTNEED) when mvolatile is called if we don't have swap space.

- Stupid performance test
I attach test program/script which are utter crap and I don't expect current smart allocator never have done it so we need more practical data with real allocator.

KVM - 8 core, 2G

VOLATILE test
13.16user 7.58system 0:06.04elapsed 343%CPU (0avgtext+0avgdata 2624096maxresident)k 0inputs+0outputs (0major+164050minor)pagefaults 0swaps

DONTNEED test
23.30user 228.92system 0:33.10elapsed 762%CPU (0avgtext+0avgdata 213088maxresident)k 0inputs+0outputs (0major+16384210minor)pagefaults 0swaps

   x86-64 - 12 core, 2G

VOLATILE test
33.38user 0.44system 0:02.87elapsed 1178%CPU (0avgtext+0avgdata 3935008maxresident)k 0inputs+0outputs (0major+245989minor)pagefaults 0swaps

DONTNEED test
28.02user 41.25system 0:05.80elapsed 1192%CPU (0avgtext+0avgdata 387776maxresident)k

[1] 74e3f3c3, vmscan: prevent background aging of anon page in no swap system

Any comments are welcome!

Cc Michael Kerrisk <[email protected]>
Cc Arun Sharma <[email protected]>
Cc Paul Turner <[email protected]>
CC David Rientjes <[email protected]>
Cc John Stultz <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Android Kernel Team <[email protected]>
Cc Robert Love <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Dave Chinner <[email protected]>
Cc Neil Brown <[email protected]>
Cc Mike Hommey <[email protected]>
Cc Taras Glek <[email protected]>
Cc KOSAKI Motohiro <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>

Minchan Kim (3):
Introduce new system call mvolatile
Discard volatile page
add PGVOLATILE vmstat count

  arch/x86/syscalls/syscall_64.tbl |    3 +-
  include/linux/mm.h               |    1 +
  include/linux/mm_types.h         |    2 +
  include/linux/rmap.h             |    3 +
  include/linux/syscalls.h         |    2 +
  include/linux/vm_event_item.h    |    2 +-
  mm/Makefile                      |    4 +-
  mm/huge_memory.c                 |    9 +-
  mm/ksm.c                         |    3 +-
  mm/memory.c                      |    2 +
  mm/migrate.c                     |    6 +-
  mm/mlock.c                       |    5 +-
  mm/mmap.c                        |    2 +-
  mm/mvolatile.c                   |  396 ++++++++++++++++++++++++++++++++++++++
  mm/rmap.c                        |   97 +++++++++-
  mm/vmscan.c                      |    4 +
  mm/vmstat.c                      |    1 +
  17 files changed, 527 insertions(+), 15 deletions(-)

create mode 100644 mm/mvolatile.c

================== 8< =============================

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>

#define SYS_mvolatile 313
#define SYS_mnovolatile 314

#define ALLOC_SIZE (8 << 20)
#define MAP_SIZE (ALLOC_SIZE * 10)
#define PAGE_SIZE (1 << 12)
#define RETRY 100

pthread_barrier_t barrier;
int mode;
#define VOLATILE_MODE 1

static int mvolatile(void *addr, size_t length)

{
	return syscall(SYS_mvolatile, addr, length);
}

static int mnovolatile(void *addr, size_t length)

{
	return syscall(SYS_mnovolatile, addr, length);
}

void *thread_entry(void *data)

{
	unsigned long i;
	cpu_set_t set;
	int cpu = *(int*)data;
	void *mmap_area;
	int retry = RETRY;

	CPU_ZERO(&set);
	CPU_SET(cpu, &set);
	sched_setaffinity(0, sizeof(set), &set);

	mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
	mmap_area = mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE,
					MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
	if (mmap_area == MAP_FAILED) {
		fprintf(stderr, "Fail to mmap [%d]\n", *(int*)data);
		exit(1);
	}

	pthread_barrier_wait(&barrier);

	while(retry--) {
		if (mode == VOLATILE_MODE) {
			mvolatile(mmap_area, MAP_SIZE);
			for (i = 0; i < MAP_SIZE; i+= ALLOC_SIZE) {
				mnovolatile(mmap_area + i, ALLOC_SIZE);
				memset(mmap_area + i, i, ALLOC_SIZE);
				mvolatile(mmap_area + i, ALLOC_SIZE);
			}
		} else {
			for (i = 0; i < MAP_SIZE; i += ALLOC_SIZE) {
				memset(mmap_area + i, i, ALLOC_SIZE);
				madvise(mmap_area + i, ALLOC_SIZE, MADV_DONTNEED);
			}
		}
	}
	return NULL;
}

int main(int argc, char *argv[])

{
	int i, nr_thread;
	int *data;

	if (argc < 3)
		return 1;

	nr_thread = atoi(argv[1]);
	mode = atoi(argv[2]);

	pthread_t *thread = malloc(sizeof(pthread_t) * nr_thread);
	data = malloc(sizeof(int) * nr_thread);
	pthread_barrier_init(&barrier, NULL, nr_thread);

	for (i = 0; i < nr_thread; i++) {
		data[i] = i;
		if (pthread_create(&thread[i], NULL, thread_entry, &data[i])) {
			perror("Fail to create thread\n");
			exit(1);
		}
	}
Quoted 9 lines
	for (i = 0; i < nr_thread; i++) {
		if (pthread_join(thread[i], NULL))
			perror("Fail to join thread\n");
		printf("[%d] thread done\n", i);
	}

	return 0;
}

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 8 lines
Kind regards,
Minchan Kim

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#8 lore raw
Quoted 5 lines · Minchan Kim

Hi Kame,

What are you doing these holiday season? :) I can't believe you sit down in front of computer.

Honestly, my holiday starts tomorrow ;) (but until 1/5 in the next year.)

Quoted 6 lines
Quoted 3 lines

Hm, by the way, the user need to attach pages to the process by causing page-fault (as you do by memset()) before calling mvolatile() ?

For effectiveness, Yes.

Isn't it better to make page-fault by get_user_pages() in mvolatile() ? Calling page fault in userland seems just to increase burden of apps.

Quoted 7 lines
Quoted 2 lines

I think your approach is interesting, anyway.

Thanks for your interest, Kame.

a??a??a? 3/4 a??a?|a??a??a??a??a??.

A happy new year.

Thanks,
-Kame

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw
Quoted 9 lines · Kamezawa Hiroyuki
Quoted 5 lines · Minchan Kim

Hi Kame,

What are you doing these holiday season? :) I can't believe you sit down in front of computer.

Honestly, my holiday starts tomorrow ;) (but until 1/5 in the next year.)

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Hm, by the way, the user need to attach pages to the process by causing page-fault (as you do by memset()) before calling mvolatile() ?

For effectiveness, Yes.

Isn't it better to make page-fault by get_user_pages() in mvolatile() ? Calling page fault in userland seems just to increase burden of apps.

It seems you misunderstood. Firstly, this patch's goal is to minimize minor fault + page allocation + memset_zero if possible on anon pages.

If someone(like allocator) calls madvise(DONTNEED)/munmap on range which has garbage collected memory, VM zaps all the pte so if user try to reuse that range, we can't avoid above overheads.

The mvolatile avoids them with not zapping ptes when memory pressure isn't severe while VM can discard pages without swapping out if memory pressure happens.

So, GUP in mvolatile isn't necessary.

Quoted 20 lines
Quoted 8 lines
Quoted 3 lines

I think your approach is interesting, anyway.

Thanks for your interest, Kame.

a??a??a? 3/4 a??a?|a??a??a??a??a??.

A happy new year.

Thanks,
-Kame

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 8 lines
Kind regards,
Minchan Kim

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>