diff options
author | Hugh Dickins <hugh.dickins@tiscali.co.uk> | 2009-09-21 20:01:57 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-09-22 10:17:31 -0400 |
commit | f8af4da3b4c14e7267c4ffb952079af3912c51c5 (patch) | |
tree | 17b0cfbd2d3d9abf9008f69e7fee5369cec7afa5 /mm | |
parent | d19f352484467a5e518639ddff0554669c10ffab (diff) |
ksm: the mm interface to ksm
This patch presents the mm interface to a dummy version of ksm.c, for
better scrutiny of that interface: the real ksm.c follows later.
When CONFIG_KSM is not set, madvise(2) reject MADV_MERGEABLE and
MADV_UNMERGEABLE with EINVAL, since that seems more helpful than
pretending that they can be serviced. But when CONFIG_KSM=y, accept them
even if KSM is not currently running, and even on areas which KSM will not
touch (e.g. hugetlb or shared file or special driver mappings).
Like other madvices, report ENOMEM despite success if any area in the
range is unmapped, and use EAGAIN to report out of memory.
Define vma flag VM_MERGEABLE to identify an area on which KSM may try
merging pages: leave it to ksm_madvise() to decide whether to set it.
Define mm flag MMF_VM_MERGEABLE to identify an mm which might contain
VM_MERGEABLE areas, to minimize callouts when forking or exiting.
Based upon earlier patches by Chris Wright and Izik Eidus.
Signed-off-by: Hugh Dickins <hugh.dickins@tiscali.co.uk>
Signed-off-by: Chris Wright <chrisw@redhat.com>
Signed-off-by: Izik Eidus <ieidus@redhat.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Cc: Hugh Dickins <hugh.dickins@tiscali.co.uk>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Lee Schermerhorn <lee.schermerhorn@hp.com>
Cc: Avi Kivity <avi@redhat.com>
Cc: Nick Piggin <nickpiggin@yahoo.com.au>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r-- | mm/Kconfig | 11 | ||||
-rw-r--r-- | mm/Makefile | 1 | ||||
-rw-r--r-- | mm/ksm.c | 56 | ||||
-rw-r--r-- | mm/madvise.c | 14 |
4 files changed, 82 insertions, 0 deletions
diff --git a/mm/Kconfig b/mm/Kconfig index 3aa519f52e18..c0b6afa178a1 100644 --- a/mm/Kconfig +++ b/mm/Kconfig | |||
@@ -214,6 +214,17 @@ config HAVE_MLOCKED_PAGE_BIT | |||
214 | config MMU_NOTIFIER | 214 | config MMU_NOTIFIER |
215 | bool | 215 | bool |
216 | 216 | ||
217 | config KSM | ||
218 | bool "Enable KSM for page merging" | ||
219 | depends on MMU | ||
220 | help | ||
221 | Enable Kernel Samepage Merging: KSM periodically scans those areas | ||
222 | of an application's address space that an app has advised may be | ||
223 | mergeable. When it finds pages of identical content, it replaces | ||
224 | the many instances by a single resident page with that content, so | ||
225 | saving memory until one or another app needs to modify the content. | ||
226 | Recommended for use with KVM, or with other duplicative applications. | ||
227 | |||
217 | config DEFAULT_MMAP_MIN_ADDR | 228 | config DEFAULT_MMAP_MIN_ADDR |
218 | int "Low address space to protect from user allocation" | 229 | int "Low address space to protect from user allocation" |
219 | default 4096 | 230 | default 4096 |
diff --git a/mm/Makefile b/mm/Makefile index ea4b18bd3960..a63bf59a0c77 100644 --- a/mm/Makefile +++ b/mm/Makefile | |||
@@ -25,6 +25,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o | |||
25 | obj-$(CONFIG_TMPFS_POSIX_ACL) += shmem_acl.o | 25 | obj-$(CONFIG_TMPFS_POSIX_ACL) += shmem_acl.o |
26 | obj-$(CONFIG_SLOB) += slob.o | 26 | obj-$(CONFIG_SLOB) += slob.o |
27 | obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o | 27 | obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o |
28 | obj-$(CONFIG_KSM) += ksm.o | ||
28 | obj-$(CONFIG_PAGE_POISONING) += debug-pagealloc.o | 29 | obj-$(CONFIG_PAGE_POISONING) += debug-pagealloc.o |
29 | obj-$(CONFIG_SLAB) += slab.o | 30 | obj-$(CONFIG_SLAB) += slab.o |
30 | obj-$(CONFIG_SLUB) += slub.o | 31 | obj-$(CONFIG_SLUB) += slub.o |
diff --git a/mm/ksm.c b/mm/ksm.c new file mode 100644 index 000000000000..8b76008fcd32 --- /dev/null +++ b/mm/ksm.c | |||
@@ -0,0 +1,56 @@ | |||
1 | /* | ||
2 | * Initial dummy version just to illustrate KSM's interface to other files. | ||
3 | */ | ||
4 | |||
5 | #include <linux/errno.h> | ||
6 | #include <linux/mman.h> | ||
7 | #include <linux/ksm.h> | ||
8 | |||
9 | int ksm_madvise(struct vm_area_struct *vma, unsigned long start, | ||
10 | unsigned long end, int advice, unsigned long *vm_flags) | ||
11 | { | ||
12 | struct mm_struct *mm = vma->vm_mm; | ||
13 | |||
14 | switch (advice) { | ||
15 | case MADV_MERGEABLE: | ||
16 | /* | ||
17 | * Be somewhat over-protective for now! | ||
18 | */ | ||
19 | if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE | | ||
20 | VM_PFNMAP | VM_IO | VM_DONTEXPAND | | ||
21 | VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE | | ||
22 | VM_MIXEDMAP | VM_SAO)) | ||
23 | return 0; /* just ignore the advice */ | ||
24 | |||
25 | if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) | ||
26 | if (__ksm_enter(mm) < 0) | ||
27 | return -EAGAIN; | ||
28 | |||
29 | *vm_flags |= VM_MERGEABLE; | ||
30 | break; | ||
31 | |||
32 | case MADV_UNMERGEABLE: | ||
33 | if (!(*vm_flags & VM_MERGEABLE)) | ||
34 | return 0; /* just ignore the advice */ | ||
35 | |||
36 | /* Unmerge any merged pages here */ | ||
37 | |||
38 | *vm_flags &= ~VM_MERGEABLE; | ||
39 | break; | ||
40 | } | ||
41 | |||
42 | return 0; | ||
43 | } | ||
44 | |||
45 | int __ksm_enter(struct mm_struct *mm) | ||
46 | { | ||
47 | /* Allocate a structure to track mm and link it into KSM's list */ | ||
48 | set_bit(MMF_VM_MERGEABLE, &mm->flags); | ||
49 | return 0; | ||
50 | } | ||
51 | |||
52 | void __ksm_exit(struct mm_struct *mm) | ||
53 | { | ||
54 | /* Unlink and free all KSM's structures which track this mm */ | ||
55 | clear_bit(MMF_VM_MERGEABLE, &mm->flags); | ||
56 | } | ||
diff --git a/mm/madvise.c b/mm/madvise.c index 66c31264f062..d9ae2067952e 100644 --- a/mm/madvise.c +++ b/mm/madvise.c | |||
@@ -11,6 +11,7 @@ | |||
11 | #include <linux/mempolicy.h> | 11 | #include <linux/mempolicy.h> |
12 | #include <linux/hugetlb.h> | 12 | #include <linux/hugetlb.h> |
13 | #include <linux/sched.h> | 13 | #include <linux/sched.h> |
14 | #include <linux/ksm.h> | ||
14 | 15 | ||
15 | /* | 16 | /* |
16 | * Any behaviour which results in changes to the vma->vm_flags needs to | 17 | * Any behaviour which results in changes to the vma->vm_flags needs to |
@@ -63,6 +64,12 @@ static long madvise_behavior(struct vm_area_struct * vma, | |||
63 | } | 64 | } |
64 | new_flags &= ~VM_DONTCOPY; | 65 | new_flags &= ~VM_DONTCOPY; |
65 | break; | 66 | break; |
67 | case MADV_MERGEABLE: | ||
68 | case MADV_UNMERGEABLE: | ||
69 | error = ksm_madvise(vma, start, end, behavior, &new_flags); | ||
70 | if (error) | ||
71 | goto out; | ||
72 | break; | ||
66 | } | 73 | } |
67 | 74 | ||
68 | if (new_flags == vma->vm_flags) { | 75 | if (new_flags == vma->vm_flags) { |
@@ -239,6 +246,10 @@ madvise_behavior_valid(int behavior) | |||
239 | case MADV_REMOVE: | 246 | case MADV_REMOVE: |
240 | case MADV_WILLNEED: | 247 | case MADV_WILLNEED: |
241 | case MADV_DONTNEED: | 248 | case MADV_DONTNEED: |
249 | #ifdef CONFIG_KSM | ||
250 | case MADV_MERGEABLE: | ||
251 | case MADV_UNMERGEABLE: | ||
252 | #endif | ||
242 | return 1; | 253 | return 1; |
243 | 254 | ||
244 | default: | 255 | default: |
@@ -273,6 +284,9 @@ madvise_behavior_valid(int behavior) | |||
273 | * MADV_DONTFORK - omit this area from child's address space when forking: | 284 | * MADV_DONTFORK - omit this area from child's address space when forking: |
274 | * typically, to avoid COWing pages pinned by get_user_pages(). | 285 | * typically, to avoid COWing pages pinned by get_user_pages(). |
275 | * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking. | 286 | * MADV_DOFORK - cancel MADV_DONTFORK: no longer omit this area when forking. |
287 | * MADV_MERGEABLE - the application recommends that KSM try to merge pages in | ||
288 | * this area with pages of identical content from other such areas. | ||
289 | * MADV_UNMERGEABLE- cancel MADV_MERGEABLE: no longer merge pages with others. | ||
276 | * | 290 | * |
277 | * return values: | 291 | * return values: |
278 | * zero - success | 292 | * zero - success |