-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
8 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef __LINUX_KSM_H | ||
#define __LINUX_KSM_H | ||
/* | ||
* Memory merging support. | ||
* | ||
* This code enables dynamic sharing of identical pages found in different | ||
* memory areas, even if they are not shared by fork(). | ||
*/ | ||
|
||
#include <linux/bitops.h> | ||
#include <linux/mm.h> | ||
#include <linux/sched.h> | ||
|
||
#ifdef CONFIG_KSM | ||
int ksm_madvise(struct vm_area_struct *vma, unsigned long start, | ||
unsigned long end, int advice, unsigned long *vm_flags); | ||
int __ksm_enter(struct mm_struct *mm); | ||
void __ksm_exit(struct mm_struct *mm); | ||
|
||
static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) | ||
{ | ||
if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) | ||
return __ksm_enter(mm); | ||
return 0; | ||
} | ||
|
||
static inline void ksm_exit(struct mm_struct *mm) | ||
{ | ||
if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) | ||
__ksm_exit(mm); | ||
} | ||
#else /* !CONFIG_KSM */ | ||
|
||
static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start, | ||
unsigned long end, int advice, unsigned long *vm_flags) | ||
{ | ||
return 0; | ||
} | ||
|
||
static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) | ||
{ | ||
return 0; | ||
} | ||
|
||
static inline void ksm_exit(struct mm_struct *mm) | ||
{ | ||
} | ||
#endif /* !CONFIG_KSM */ | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Initial dummy version just to illustrate KSM's interface to other files. | ||
*/ | ||
|
||
#include <linux/errno.h> | ||
#include <linux/mman.h> | ||
#include <linux/ksm.h> | ||
|
||
int ksm_madvise(struct vm_area_struct *vma, unsigned long start, | ||
unsigned long end, int advice, unsigned long *vm_flags) | ||
{ | ||
struct mm_struct *mm = vma->vm_mm; | ||
|
||
switch (advice) { | ||
case MADV_MERGEABLE: | ||
/* | ||
* Be somewhat over-protective for now! | ||
*/ | ||
if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE | | ||
VM_PFNMAP | VM_IO | VM_DONTEXPAND | | ||
VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE | | ||
VM_MIXEDMAP | VM_SAO)) | ||
return 0; /* just ignore the advice */ | ||
|
||
if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) | ||
if (__ksm_enter(mm) < 0) | ||
return -EAGAIN; | ||
|
||
*vm_flags |= VM_MERGEABLE; | ||
break; | ||
|
||
case MADV_UNMERGEABLE: | ||
if (!(*vm_flags & VM_MERGEABLE)) | ||
return 0; /* just ignore the advice */ | ||
|
||
/* Unmerge any merged pages here */ | ||
|
||
*vm_flags &= ~VM_MERGEABLE; | ||
break; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int __ksm_enter(struct mm_struct *mm) | ||
{ | ||
/* Allocate a structure to track mm and link it into KSM's list */ | ||
set_bit(MMF_VM_MERGEABLE, &mm->flags); | ||
return 0; | ||
} | ||
|
||
void __ksm_exit(struct mm_struct *mm) | ||
{ | ||
/* Unlink and free all KSM's structures which track this mm */ | ||
clear_bit(MMF_VM_MERGEABLE, &mm->flags); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters