ksm - dynamic page sharing driver for linux v2
Subsystems (6)
cover ksm - dynamic page sharing driver for linux v2
Quoted 1 line
From v1 to v2:
1)Fixed security issue found by Chris Wright:
Ksm was checking if page is a shared page by running !PageAnon. Beacuse that Ksm scan only anonymous memory, all !PageAnons inside ksm data strctures are shared page, however there might be a case for do_wp_page() when the VM_SHARED is used where do_wp_page() would instead of copying the page into new anonymos page, would reuse the page, it was fixed by adding check for the dirty_bit of the virtual addresses pointing into the shared page. I was not finding any VM code tha would clear the dirty bit from this virtual address (due to the fact that we allocate the page using page_alloc() - kernel allocated pages), ~but i still want confirmation about this from the vm guys - thanks.~
2)Moved to sysfs to control ksm:
It was requested as a better way to control the ksm scanning thread than ioctls.
the sysfs api:
dir: /sys/kernel/mm/ksm/
kernel_pages_allocated - information about how many kernel pages ksm have allocated, this pages are not swappable, and each page like that is used by ksm to share pages with identical content
pages_shared - how many pages were shared by ksm
run - set to 1 when you want ksm to run, 0 when no
max_kernel_pages - set the maximum amount of kernel pages to be allocated by ksm, set 0 for unlimited.
pages_to_scan - how many pages to scan before ksm will sleep
sleep - how much usecs ksm will sleep.
3)Add sysfs paramater to control the maximum kernel pages to be by ksm.
4)Add statistics about how much pages are really shared.
One issue still to be discussed:
There was a suggestion to use madvice(SHAREABLE) instead of using ioctls to register memory that need to be scanned by ksm. Such change is outside the area of ksm.c and would required adding new madvice api, and change some parts of the vm and the kernel code, so first thing to do, is realized if we really want this.
I dont know any other open issues.
Thanks.
This is from the first post:
(The kvm part, togather with the kvm-userspace part, was post with V1 before about a week, whoever want to test ksm may download the patch from lkml archive)
KSM is a linux driver that allows dynamicly sharing identical memory pages between one or more processes.
Unlike tradtional page sharing that is made at the allocation of the memory, ksm do it dynamicly after the memory was created. Memory is periodically scanned; identical pages are identified and merged.
The sharing is unnoticeable by the process that use this memory. (the shared pages are marked as readonly, and in case of write do_wp_page() take care to create new copy of the page)
To find identical pages ksm use algorithm that is split into three primery levels:
1) Ksm will start scan the memory and will calculate checksum for each page that is registred to be scanned.
(In the first round of the scanning, ksm would only calculate
this checksum for all the pages)
2) Ksm will go again on the whole memory and will recalculate the checmsum of the pages, pages that are found to have the same checksum value, would be considered "pages that are most likely wont changed"
Ksm will insert this pages into sorted by page content RB-tree that is called "unstable tree", the reason that this tree is called unstable is due to the fact that the page contents might changed while they are still inside the tree, and therefore the tree would become corrupted.
Due to this problem ksm take two more steps in addition to the checksum calculation:
a) Ksm will throw and recreate the entire unstable tree each round of memory scanning - so if we have corruption, it will be fixed when we will rebuild the tree.
b) Ksm is using RB-tree, that its balancing is made by the node color and not by the content, so even if the page get corrupted, it still would take the same amount of time to search on it.
3) In addition to the unstable tree, ksm hold another tree that is called "stable tree" - this tree is RB-tree that is sorted by the pages content and all its pages are write protected, and therefore it cant get corrupted.
Each time ksm will find two identcial pages using the unstable tree, it will create new write-protected shared page, and this page will be inserted into the stable tree, and would be saved there, the stable tree, unlike the unstable tree, is never throwen away, so each page that we find would be saved inside it.
Taking into account the three levels that described above, the algorithm work like that:
search primary tree (sorted by entire page contents, pages write protected)
- if match found, merge
- if no match found...
- search secondary tree (sorted by entire page contents, pages not write protected)
- if match found, merge
- remove from secondary tree and insert merged page into primary tree
- if no match found...
- checksum
- if checksum hasn't changed
- insert into secondary tree
- if it has, store updated checksum (note: first time this page is handled it won't have a checksum, so checksum will appear as "changed", so it takes two passes w/ no other matches to get into secondary tree)
- do not insert into any tree, will see it again on next pass
The basic idea of this algorithm, is that even if the unstable tree doesnt promise to us to find two identical pages in the first round, we would probably find them in the second or the third or the tenth round, then after we have found this two identical pages only once, we will insert them into the stable tree, and then they would be protected there forever. So the all idea of the unstable tree, is just to build the stable tree and then we will find the identical pages using it.
The current implemantion can be improved alot:
we dont have to calculate exspensive checksum, we can just use the host dirty bit.
currently we dont support shared pages swapping (other pages that are not shared can be swapped (all the pages that we didnt find to be identical to other pages...).
Walking on the tree, we keep call to get_user_pages(), we can optimized it by saving the pfn, and using mmu notifiers to know when the virtual address mapping was changed.
We currently scan just programs that were registred to be used by ksm, we would later want to add the abilaty to tell ksm to scan PIDS (so you can scan closed binary applications as well).
Right now ksm scanning is made by just one thread, multiple scanners support might would be needed.
This driver is very useful for KVM as in cases of runing multiple guests operation system of the same type.
(For desktop work loads we have achived more than x2 memory overcommit (more like x3))
This driver have found users other than KVM, for example CERN, Fons Rademakers:
"on many-core machines we run one large detector simulation program per core. These simulation programs are identical but run each in their own process and need about 2 - 2.5 GB RAM.
We typically buy machines with 2GB RAM per core and so have a problem to run one of these programs per core.
Of the 2 - 2.5 GB about 700MB is identical data in the form of magnetic field maps, detector geometry, etc.
Currently people have been trying to start one program, initialize the geometry and field maps and then fork it N times, to have the data shared. With KSM this would be done automatically by the system so it sounded extremely attractive when Andrea presented it."
I am sending another seires of patchs for kvm kernel and kvm-userspace that would allow users of kvm to test ksm with it.
The kvm patchs would apply to Avi git tree.
Izik Eidus (4):
MMU_NOTIFIERS: add set_pte_at_notify()
add page_wrprotect(): write protecting page. add replace_page(): change the page pte is pointing to. add ksm kernel shared memory driver.
include/linux/ksm.h | 48 ++ include/linux/miscdevice.h | 1 + include/linux/mm.h | 5 + include/linux/mmu_notifier.h | 34 + include/linux/rmap.h | 11 + mm/Kconfig | 6 + mm/Makefile | 1 + mm/ksm.c | 1668 ++++++++++++++++++++++++++++++++++++++++++ mm/memory.c | 90 +++- mm/mmu_notifier.c | 20 + mm/rmap.c | 139 ++++ 11 files changed, 2021 insertions(+), 2 deletions(-) create mode 100644 include/linux/ksm.h create mode 100644 mm/ksm.c
--
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>
PATCH 1/4 MMU_NOTIFIERS: add set_pte_at_notify()
this macro allow setting the pte in the shadow page tables directly instead of flushing the shadow page table entry and then get vmexit in order to set it.
This function is optimzation for kvm/users of mmu_notifiers for COW pages, it is useful for kvm when ksm is used beacuse it allow kvm not to have to recive VMEXIT and only then map the shared page into the mmu shadow pages, but instead map it directly at the same time linux map the page into the host page table.
this mmu notifer macro is working by calling to callback that will map directly the physical page into the shadow page tables.
(users of mmu_notifiers that didnt implement the set_pte_at_notify() call back will just recive the mmu_notifier_invalidate_page callback)
--- include/linux/mmu_notifier.h | 34 ++++++++++++++++++++++++++++++++++ mm/memory.c | 10 ++++++++-- mm/mmu_notifier.c | 20 ++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-)
include/linux/mmu_notifier.h +34 -0
@@ -61,6 +61,15 @@ struct mmu_notifier_ops {struct mm_struct *mm, unsigned long address); +/* +* change_pte is called in cases that pte mapping into page is changed +* for example when ksm mapped pte to point into a new shared page. +*/ +void (*change_pte)(struct mmu_notifier *mn, +struct mm_struct *mm, +unsigned long address, +pte_t pte); +/* * Before this is invoked any secondary MMU is still ok to * read/write to the page previously pointed to by the Linux
@@ -154,6 +163,8 @@ extern void __mmu_notifier_mm_destroy(struct mm_struct *mm);extern void __mmu_notifier_release (struct mm_struct *mm);extern int __mmu_notifier_clear_flush_young (struct mm_struct *mm,unsigned long address); +extern void __mmu_notifier_change_pte (struct mm_struct *mm, +unsigned long address,pte_t pte);extern void __mmu_notifier_invalidate_page (struct mm_struct *mm,unsigned long address); extern void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
@@ -175,6 +186,13 @@ static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,return 0 ; } +static inline void mmu_notifier_change_pte (struct mm_struct *mm, +unsigned long address,pte_t pte) +{ +if (mm_has_notifiers (mm)) +__mmu_notifier_change_pte (mm, address, pte); +} +static inline void mmu_notifier_invalidate_page (struct mm_struct *mm,unsigned long address) {
@@ -236,6 +254,16 @@ static inline void mmu_notifier_mm_destroy(struct mm_struct *mm) __young; \ }) +#define set_pte_at_notify (__mm, __address, __ptep, __pte) \ +({ \ + struct mm_struct *___mm = __mm; \ + unsigned long ___address = __address; \ + pte_t ___pte = __pte; \ + \ + set_pte_at(__mm, __address, __ptep, ___pte); \ + mmu_notifier_change_pte(___mm, ___address, ___pte); \ +}) +#else /* CONFIG_MMU_NOTIFIER */ static inline void mmu_notifier_release (struct mm_struct *mm)
@@ -248,6 +276,11 @@ static inline int mmu_notifier_clear_flush_young(struct mm_struct *mm,return 0 ; } +static inline void mmu_notifier_change_pte (struct mm_struct *mm, +unsigned long address,pte_t pte) +{ +} +static inline void mmu_notifier_invalidate_page (struct mm_struct *mm,unsigned long address) {
@@ -273,6 +306,7 @@ static inline void mmu_notifier_mm_destroy(struct mm_struct *mm)#define ptep_clear_flush_young_notify ptep_clear_flush_young#define ptep_clear_flush_notify ptep_clear_flush +#define set_pte_at_notify set_pte_at#endif /* CONFIG_MMU_NOTIFIER */
mm/memory.c +8 -2
@@ -2051,9 +2051,15 @@ gotten:* seen in the presence of one thread doing SMC and another * thread doing COW. */ -ptep_clear_flush_notify (vma, address, page_table); +ptep_clear_flush (vma, address, page_table);page_add_new_anon_rmap (new_page, vma, address); -set_pte_at (mm, address, page_table, entry); +/* +* We call here the notify macro beacuse in cases of using +* secondary mmu page table like kvm shadow page, tables we want +* the new page to be mapped directly into the secondary page +* table +*/ +set_pte_at_notify (mm, address, page_table, entry);update_mmu_cache (vma, address, entry);if (old_page) {/*
mm/mmu_notifier.c +20 -0
@@ -99,6 +99,26 @@ int __mmu_notifier_clear_flush_young(struct mm_struct *mm,return young; } +void __mmu_notifier_change_pte (struct mm_struct *mm,unsigned long address, +pte_t pte) +{ +struct mmu_notifier *mn; +struct hlist_node *n; + +rcu_read_lock (); +hlist_for_each_entry_rcu (mn, n, &mm->mmu_notifier_mm->list, hlist) { +if (mn->ops->change_pte) + mn->ops->change_pte (mn, mm, address, pte); +/* +* some drivers dont have change_pte and therefor we must call +* for invalidate_page in that case +*/ +else if (mn->ops->invalidate_page) + mn->ops->invalidate_page (mn, mm, address); + } +rcu_read_unlock (); +} +void __mmu_notifier_invalidate_page (struct mm_struct *mm,unsigned long address) {
Signature · 7 lines
1.5.6.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>
PATCH 2/4 add page_wrprotect(): write protecting page.
this patch add new function called page_wrprotect(), page_wrprotect() is used to take a page and mark all the pte that point into it as readonly.
The function is working by walking the rmap of the page, and setting each pte realted to the page as readonly.
The odirect_sync parameter is used to protect against possible races with odirect while we are marking the pte as readonly,
as noted by Andrea Arcanglei:
"While thinking at get_user_pages_fast I figured another worse way things can go wrong with ksm and o_direct: think a thread writing constantly to the last 512bytes of a page, while another thread read and writes to/from the first 512bytes of the page. We can lose O_DIRECT reads, the very moment we mark any pte wrprotected..."
--- include/linux/rmap.h | 11 ++++ mm/rmap.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 0 deletions(-)
include/linux/rmap.h +11 -0
@@ -118,6 +118,10 @@ static inline int try_to_munlock(struct page *page) }#endif +#if defined(CONFIG_KSM ) || defined(CONFIG_KSM_MODULE ) +int page_wrprotect (struct page *page,int *odirect_sync,int count_offset); +#endif +#else /* !CONFIG_MMU */ #define anon_vma_init () do {} while (0)
@@ -132,6 +136,13 @@ static inline int page_mkclean(struct page *page) return 0; } +#if defined(CONFIG_KSM ) || defined(CONFIG_KSM_MODULE ) +static inline int page_wrprotect (struct page *page,int *odirect_sync, +int count_offset) +{ +return 0 ; +} +#endif #endif /* CONFIG_MMU */
mm/rmap.c +139 -0
145 lines not on this page · raw message
Signature · 7 lines
1.5.6.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>
PATCH 3/4 add replace_page(): change the page pte is pointing to.
replace_page() allow changing the mapping of pte from one physical page into diffrent physical page.
this function is working by removing oldpage from the rmap and calling put_page on it, and by setting the pte to point into newpage and by inserting it to the rmap using page_add_file_rmap().
note: newpage must be non anonymous page, the reason for this is:replace_page() is built to allow mapping one page into more than one virtual addresses, the mapping of this page can happen in diffrent offsets inside each vma, and therefore we cannot trust the page->index anymore.
the side effect of this issue is that newpage cannot be anything but kernel allocated page that is not swappable.
--- include/linux/mm.h | 5 +++ mm/memory.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 0 deletions(-)
include/linux/mm.h +5 -0
@@ -1240,6 +1240,11 @@ int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,int vm_insert_mixed (struct vm_area_struct *vma,unsigned long addr,unsigned long pfn); +#if defined(CONFIG_KSM ) || defined(CONFIG_KSM_MODULE ) +int replace_page (struct vm_area_struct *vma,struct page *oldpage, +struct page *newpage,pte_t orig_pte,pgprot_t prot); +#endif +struct page *follow_page (struct vm_area_struct *,unsigned long address,unsigned int foll_flags);#define FOLL_WRITE 0x01/* check pte is writable */
mm/memory.c +80 -0
86 lines not on this page · raw message
Signature · 7 lines
1.5.6.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>
PATCH 4/4 add ksm kernel shared memory driver.
Ksm is driver that allow merging identical pages between one or more applications in way unvisible to the application that use it. Pages that are merged are marked as readonly and are COWed when any application try to change them.
Ksm is used for cases where using fork() is not suitable, one of this cases is where the pages of the application keep changing dynamicly and the application cannot know in advance what pages are going to be identical.
Ksm works by walking over the memory pages of the applications it scan in order to find identical pages.
It uses a two sorted data strctures called stable and unstable trees to find in effective way the identical pages.
When ksm finds two identical pages, it marks them as readonly and merges them into single one page,
after the pages are marked as readonly and merged into one page, linux will treat this pages as normal copy_on_write pages and will fork them when write access will happen to them.
Ksm scan just memory areas that were registred to be scanned by it.
Ksm api:
KSM_GET_API_VERSION:
Give the userspace the api version of the module.
KSM_CREATE_SHARED_MEMORY_AREA:
Create shared memory reagion fd, that latter allow the user to register the memory region to scan by using:KSM_REGISTER_MEMORY_REGION and KSM_REMOVE_MEMORY_REGION
KSM_REGISTER_MEMORY_REGION:
Register userspace virtual address range to be scanned by ksm. This ioctl is using the ksm_memory_region structure:ksm_memory_region:__u32 npages;
number of pages to share inside this memory region.__u32 pad;__u64 addr:
the begining of the virtual address of this region.__u64 reserved_bits;
reserved bits for future usage.
KSM_REMOVE_MEMORY_REGION:
Remove memory region from ksm.
--- include/linux/ksm.h | 48 ++ include/linux/miscdevice.h | 1 + mm/Kconfig | 6 + mm/Makefile | 1 + mm/ksm.c | 1668 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1724 insertions(+), 0 deletions(-) create mode 100644 include/linux/ksm.h create mode 100644 mm/ksm.c
include/linux/ksm.h new file +48 -0
@@ -0,0 +1,48 @@ +#ifndef __LINUX_KSM_H +#define __LINUX_KSM_H + +/* +* Userspace interface for /dev/ksm - kvm shared memory +*/ + +#include <linux/types.h> +#include <linux/ioctl.h> + +#include <asm/types.h> + +#define KSM_API_VERSION 1 + +#define ksm_control_flags_run 1 + +/* for KSM_REGISTER_MEMORY_REGION */ +struct ksm_memory_region { +__u32 npages;/* number of pages to share */ +__u32 pad; +__u64 addr;/* the begining of the virtual address */ +__u64 reserved_bits; +}; + +#define KSMIO 0xAB + +/* ioctls for /dev/ksm */ + +#define KSM_GET_API_VERSION _IO(KSMIO, 0x00) +/* +* KSM_CREATE_SHARED_MEMORY_AREA - create the shared memory reagion fd +*/ +#define KSM_CREATE_SHARED_MEMORY_AREA _IO(KSMIO, 0x01)/* return SMA fd */ + +/* ioctls for SMA fds */ + +/* +* KSM_REGISTER_MEMORY_REGION - register virtual address memory area to be +* scanned by kvm. +*/ +#define KSM_REGISTER_MEMORY_REGION _IOW(KSMIO, 0x20,\ + struct ksm_memory_region) +/* +* KSM_REMOVE_MEMORY_REGION - remove virtual address memory area from ksm. +*/ +#define KSM_REMOVE_MEMORY_REGION _IO(KSMIO, 0x21) + +#endif
include/linux/miscdevice.h +1 -0
@@ -30,6 +30,7 @@#define HPET_MINOR 228#define FUSE_MINOR 229#define KVM_MINOR 232 +#define KSM_MINOR 233#define MISC_DYNAMIC_MINOR 255struct device ;
mm/Kconfig +6 -0
@@ -223,3 +223,9 @@ config HAVE_MLOCKED_PAGE_BIT config MMU_NOTIFIER bool + +config KSM + tristate "Enable KSM for page sharing" + help + Enable the KSM kernel module to allow page sharing of equal pages + among different tasks.
mm/Makefile +1 -0
@@ -24,6 +24,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_TMPFS_POSIX_ACL) += shmem_acl.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o +obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += debug-pagealloc.o obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_SLUB) += slub.o
mm/ksm.c new file +1668 -0
1,668 lines not on this page · raw message
Signature · 7 lines
1.5.6.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>
Re: [PATCH 4/4] add ksm kernel shared memory driver.
On 094, 04 04, 2009 at 05:35:22PM +0300, Izik Eidus wrote:
<SNIP>
mm/ksm.c line 383
Quoted 5 lines
mm/ksm.c new file +5
-0+static inline u32 calc_checksum (struct page *page) +{ +u32 checksum; +void *addr =kmap_atomic (page,KM_USER0 ); + checksum =jhash (addr,PAGE_SIZE ,17 );
Why jhash2() is not used here ? It's faster and leads to smaller code size.
Quoted 3 lines
mm/ksm.c new file +3
-0+kunmap_atomic (addr,KM_USER0 ); +return checksum; +}
Re: [PATCH 4/4] add ksm kernel shared memory driver.
Quoted 5 lines · Andrey Panin
On 094, 04 04, 2009 at 05:35:22PM +0300, Izik Eidus wrote:
<SNIP>
Quoted 9 lines
mm/ksm.c line 383
Quoted 6 lines
mm/ksm.c new file +5
-0+static inline u32 calc_checksum (struct page *page) +{ +u32 checksum; +void *addr =kmap_atomic (page,KM_USER0 ); + checksum =jhash (addr,PAGE_SIZE ,17 );Why
jhash2()is not used here ? It's faster and leads to smaller code size.
Beacuse i didnt know, i will check that and change.
Thanks.
(We should really use in cpu crc for Intel Nehalem, and dirty bit for the rest of the architactures...)
Quoted 7 lines
mm/ksm.c line 386
Quoted 5 lines
mm/ksm.c new file +3
-0+kunmap_atomic (addr,KM_USER0 ); +return checksum; +}
--
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>
Quoted 4 lines · On Sunday 05 April 2009 01:35:18 Izik Eidus wrote:
This driver is very useful for KVM as in cases of runing multiple guests operation system of the same type.
(For desktop work loads we have achived more than x2 memory overcommit (more like x3))
Interesting that it is a desirable workload to have multiple guests each running MS office.
I wonder, can windows enter a paravirtualised guest mode for KVM? And can you detect page allocation/freeing events?
Quoted 5 lines
This driver have found users other than KVM, for example CERN, Fons Rademakers:
"on many-core machines we run one large detector simulation program per core. These simulation programs are identical but run each in their own process and need about 2 - 2.5 GB RAM.
Quoted 8 lines
We typically buy machines with 2GB RAM per core and so have a problem to run one of these programs per core.
Of the 2 - 2.5 GB about 700MB is identical data in the form of magnetic field maps, detector geometry, etc.
Currently people have been trying to start one program, initialize the geometry and field maps and then fork it N times, to have the data shared. With KSM this would be done automatically by the system so it sounded extremely attractive when Andrea presented it."
They should use a shared memory segment, or MAP_ANONYMOUS|MAP_SHARED etc. Presumably they will probably want to control it to interleave it over all numa nodes and use hugepages for it. It would be very little work.
Quoted 3 lines
I am sending another seires of patchs for kvm kernel and kvm-userspace that would allow users of kvm to test ksm with it.
The kvm patchs would apply to Avi git tree.
--
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>
Quoted 9 lines · Nick Piggin
Quoted 5 lines · On Sunday 05 April 2009 01:35:18 Izik Eidus wrote:
This driver is very useful for KVM as in cases of runing multiple guests operation system of the same type.
(For desktop work loads we have achived more than x2 memory overcommit (more like x3))
Quoted 4 lines
Interesting that it is a desirable workload to have multiple guests each running MS office.
I wonder, can windows enter a paravirtualised guest mode for KVM?
Windows has some support for paravirtualization, for example it can use hypercalls instead of tlb flush IPIs.
Quoted 3 lines
And can
you detect page allocation/freeing events?
Not that I know of.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- 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>
Quoted 12 lines · Nick Piggin
Quoted 5 lines · On Sunday 05 April 2009 01:35:18 Izik Eidus wrote:
This driver is very useful for KVM as in cases of runing multiple guests operation system of the same type.
(For desktop work loads we have achived more than x2 memory overcommit (more like x3))Interesting that it is a desirable workload to have multiple guests each running MS office.
This numbers are took from such workload, it is some kind of weird script that keep opening Word / Excel and write there like a user... I think in addition it open internet explorer and enter to random sites...
I can search for the script if wanted...
Quoted 3 lines
I wonder, can windows enter a paravirtualised guest mode for KVM? And can you detect page allocation/freeing events?
I Dont know.
Quoted 17 lines
Quoted 16 lines
This driver have found users other than KVM, for example CERN, Fons Rademakers:
"on many-core machines we run one large detector simulation program per core. These simulation programs are identical but run each in their own process and need about 2 - 2.5 GB RAM.
We typically buy machines with 2GB RAM per core and so have a problem to run one of these programs per core.
Of the 2 - 2.5 GB about 700MB is identical data in the form of magnetic field maps, detector geometry, etc.
Currently people have been trying to start one program, initialize the geometry and field maps and then fork it N times, to have the data shared. With KSM this would be done automatically by the system so it sounded extremely attractive when Andrea presented it."
Quoted 4 lines
They should use a shared memory segment, or MAP_ANONYMOUS|MAP_SHARED etc. Presumably they will probably want to control it to interleave it over all numa nodes and use hugepages for it. It would be very little work.
Agree about that, dont know their application to much, i know they had problems to do it.
Quoted 6 lines
Quoted 6 lines
I am sending another seires of patchs for kvm kernel and kvm-userspace that would allow users of kvm to test ksm with it.
The kvm patchs would apply to Avi git tree.
--
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>
Quoted 3 lines · Nick Piggin
They should use a shared memory segment, or MAP_ANONYMOUS|MAP_SHARED etc. Presumably they will probably want to control it to interleave it over all numa nodes and use hugepages for it. It would be very little work.
I thought it's the intermediate result of the computations that leads to lots of equal data too, in which case ksm is the only way to share it all.
--
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>
Hi Izik,
Is there some user documentation available? (apart from RTFS?:)) I've compiled kernel with v2 of Your patches, loaded ksm module, did echo 1 > /proc/sys/kernel/mm/ksm/run, but I think it didn't do anything, at least no pages were collected..
Could You advise me a bit?
thanks a lot in advance...
I can't wait to try it on our hosts runing 50-60 KVMs :) BR
nik
Quoted 202 lines · Izik Eidus
From v1 to v2:
1)Fixed security issue found by Chris Wright:
Ksm was checking if page is a shared page by running !PageAnon. Beacuse that Ksm scan only anonymous memory, all !PageAnons inside ksm data strctures are shared page, however there might be a case fordo_wp_page()when theVM_SHAREDis used wheredo_wp_page()would instead of copying the page into new anonymos page, would reuse the page, it was fixed by adding check for thedirty_bitof the virtual addresses pointing into the shared page. I was not finding any VM code tha would clear the dirty bit from this virtual address (due to the fact that we allocate the page usingpage_alloc()- kernel allocated pages), ~but i still want confirmation about this from the vm guys - thanks.~2)Moved to sysfs to control ksm:
It was requested as a better way to control the ksm scanning thread than ioctls.
the sysfs api:
dir:/sys/kernel/mm/ksm/
kernel_pages_allocated- information about how many kernel pages ksm have allocated, this pages are not swappable, and each page like that is used by ksm to share pages with identical content
pages_shared- how many pages were shared by ksmrun - set to 1 when you want ksm to run, 0 when no
max_kernel_pages- set the maximum amount of kernel pages to be allocated by ksm, set 0 for unlimited.
pages_to_scan- how many pages to scan before ksm will sleepsleep - how much usecs ksm will sleep.
3)Add sysfs paramater to control the maximum kernel pages to be by ksm.
4)Add statistics about how much pages are really shared.
One issue still to be discussed:
There was a suggestion to use madvice(SHAREABLE) instead of using ioctls to register memory that need to be scanned by ksm. Such change is outside the area ofksm.cand would required adding new madvice api, and change some parts of the vm and the kernel code, so first thing to do, is realized if we really want this.I dont know any other open issues.
Thanks.
This is from the first post:
(The kvm part, togather with the kvm-userspace part, was post with V1 before about a week, whoever want to test ksm may download the patch from lkml archive)KSM is a linux driver that allows dynamicly sharing identical memory pages between one or more processes.
Unlike tradtional page sharing that is made at the allocation of the memory, ksm do it dynamicly after the memory was created. Memory is periodically scanned; identical pages are identified and merged.
The sharing is unnoticeable by the process that use this memory. (the shared pages are marked as readonly, and in case of writedo_wp_page()take care to create new copy of the page)To find identical pages ksm use algorithm that is split into three primery levels:
1) Ksm will start scan the memory and will calculate checksum for each page that is registred to be scanned.
(In the first round of the scanning, ksm would only calculate
this checksum for all the pages)2) Ksm will go again on the whole memory and will recalculate the checmsum of the pages, pages that are found to have the same checksum value, would be considered "pages that are most likely wont changed"
Ksm will insert this pages into sorted by page content RB-tree that is called "unstable tree", the reason that this tree is called unstable is due to the fact that the page contents might changed while they are still inside the tree, and therefore the tree would become corrupted.
Due to this problem ksm take two more steps in addition to the checksum calculation:
a) Ksm will throw and recreate the entire unstable tree each round of memory scanning - so if we have corruption, it will be fixed when we will rebuild the tree.
b) Ksm is using RB-tree, that its balancing is made by the node color and not by the content, so even if the page get corrupted, it still would take the same amount of time to search on it.3) In addition to the unstable tree, ksm hold another tree that is called "stable tree" - this tree is RB-tree that is sorted by the pages content and all its pages are write protected, and therefore it cant get corrupted.
Each time ksm will find two identcial pages using the unstable tree, it will create new write-protected shared page, and this page will be inserted into the stable tree, and would be saved there, the stable tree, unlike the unstable tree, is never throwen away, so each page that we find would be saved inside it.Taking into account the three levels that described above, the algorithm work like that:
search primary tree (sorted by entire page contents, pages write protected)
- if match found, merge
- if no match found...
- search secondary tree (sorted by entire page contents, pages not write protected)
- if match found, merge
- remove from secondary tree and insert merged page into primary tree
- if no match found...
- checksum
- if checksum hasn't changed
- insert into secondary tree
- if it has, store updated checksum (note: first time this page is handled it won't have a checksum, so checksum will appear as "changed", so it takes two passes w/ no other matches to get into secondary tree)
- do not insert into any tree, will see it again on next passThe basic idea of this algorithm, is that even if the unstable tree doesnt promise to us to find two identical pages in the first round, we would probably find them in the second or the third or the tenth round, then after we have found this two identical pages only once, we will insert them into the stable tree, and then they would be protected there forever. So the all idea of the unstable tree, is just to build the stable tree and then we will find the identical pages using it.
The current implemantion can be improved alot:
we dont have to calculate exspensive checksum, we can just use the host dirty bit.currently we dont support shared pages swapping (other pages that are not shared can be swapped (all the pages that we didnt find to be identical to other pages...).
Walking on the tree, we keep call to
get_user_pages(), we can optimized it by saving the pfn, and using mmu notifiers to know when the virtual address mapping was changed.We currently scan just programs that were registred to be used by ksm, we would later want to add the abilaty to tell ksm to scan PIDS (so you can scan closed binary applications as well).
Right now ksm scanning is made by just one thread, multiple scanners support might would be needed.
This driver is very useful for KVM as in cases of runing multiple guests operation system of the same type.
(For desktop work loads we have achived more than x2 memory overcommit (more like x3))This driver have found users other than KVM, for example CERN, Fons Rademakers:
"on many-core machines we run one large detector simulation program per core. These simulation programs are identical but run each in their own process and need about 2 - 2.5 GB RAM.
We typically buy machines with 2GB RAM per core and so have a problem to run one of these programs per core.
Of the 2 - 2.5 GB about 700MB is identical data in the form of magnetic field maps, detector geometry, etc.
Currently people have been trying to start one program, initialize the geometry and field maps and then fork it N times, to have the data shared. With KSM this would be done automatically by the system so it sounded extremely attractive when Andrea presented it."I am sending another seires of patchs for kvm kernel and kvm-userspace that would allow users of kvm to test ksm with it.
The kvm patchs would apply to Avi git tree.Izik Eidus (4):
MMU_NOTIFIERS: addset_pte_at_notify()
addpage_wrprotect(): write protecting page. addreplace_page(): change the page pte is pointing to. add ksm kernel shared memory driver.include/linux/ksm.h | 48 ++ include/linux/miscdevice.h | 1 + include/linux/mm.h | 5 + include/linux/mmu_notifier.h | 34 + include/linux/rmap.h | 11 + mm/Kconfig | 6 + mm/Makefile | 1 + mm/ksm.c | 1668 ++++++++++++++++++++++++++++++++++++++++++ mm/memory.c | 90 +++- mm/mmu_notifier.c | 20 + mm/rmap.c | 139 ++++ 11 files changed, 2021 insertions(+), 2 deletions(-) create mode 100644 include/linux/ksm.h create mode 100644 mm/ksm.c--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 13 lines
------------------------------------- Nikola CIPRICH LinuxBox.cz, s.r.o. 28. rijna 168, 709 01 Ostrava tel.: +420 596 603 142 fax: +420 596 621 273 mobil: +420 777 093 799 www.linuxbox.cz mobil servis: +420 737 238 656 email servis: [email protected] -------------------------------------
Quoted 11 lines · Nikola Ciprich
Hi Izik,
Is there some user documentation available? (apart from RTFS?:)) I've compiled kernel with v2 of Your patches, loaded ksm module, did echo 1 >/proc/sys/kernel/mm/ksm/run, but I think it didn't do anything, at least no pages were collected..
Could You advise me a bit?
thanks a lot in advance...
I can't wait to try it on our hosts runing 50-60 KVMs :) BR
nik
You need the userspace / kvm patchs that i posted together with V1 about 1-2 weeks ago... What you should do is this:
Patch Linus kernel git with the ksm patchs (V2) (like you just did)
This patchs can be found at:
http://lkml.org/lkml/2009/4/4/77
Then patch Avi kernel git with the kvm patchs that were sent togather with V1 Patchs can be found at:
http://lkml.org/lkml/2009/3/30/534
and then Avi git userspace with this patchs:
http://lkml.org/lkml/2009/3/30/538
Now, after you finish patching the kernel, load the kvm modules from avi git, and then using patched userspace you can start using ksm:
set up the speed: (just number, you can change them to make it take less or more cpu) echo 400 > /sys/kernel/mm/ksm/pages_to_scan
echo 10000 > /sys/kernel/mm/ksm/sleep
echo 1 > /sys/kernel/mm/ksm/run
Dont raise all the VMS at once, beacuse then KSM wont be able to catch with the memory allocation... Raise few VMS, see that their memory get shared and your host free memory grow, then raise more VMS and so on...
Enjoy.
(You can check pages_shared for the number of pages that have been shared, you can run top as well)
Quoted 208 lines · Izik Eidus
Quoted 204 lines
From v1 to v2:
1)Fixed security issue found by Chris Wright:
Ksm was checking if page is a shared page by running !PageAnon. Beacuse that Ksm scan only anonymous memory, all !PageAnons inside ksm data strctures are shared page, however there might be a case fordo_wp_page()when theVM_SHAREDis used wheredo_wp_page()would instead of copying the page into new anonymos page, would reuse the page, it was fixed by adding check for thedirty_bitof the virtual addresses pointing into the shared page. I was not finding any VM code tha would clear the dirty bit from this virtual address (due to the fact that we allocate the page usingpage_alloc()- kernel allocated pages), ~but i still want confirmation about this from the vm guys - thanks.~2)Moved to sysfs to control ksm:
It was requested as a better way to control the ksm scanning thread than ioctls.
the sysfs api:
dir:/sys/kernel/mm/ksm/
kernel_pages_allocated- information about how many kernel pages ksm have allocated, this pages are not swappable, and each page like that is used by ksm to share pages with identical content
pages_shared- how many pages were shared by ksmrun - set to 1 when you want ksm to run, 0 when no
max_kernel_pages- set the maximum amount of kernel pages to be allocated by ksm, set 0 for unlimited.
pages_to_scan- how many pages to scan before ksm will sleepsleep - how much usecs ksm will sleep.
3)Add sysfs paramater to control the maximum kernel pages to be by ksm.
4)Add statistics about how much pages are really shared.
One issue still to be discussed:
There was a suggestion to use madvice(SHAREABLE) instead of using ioctls to register memory that need to be scanned by ksm. Such change is outside the area ofksm.cand would required adding new madvice api, and change some parts of the vm and the kernel code, so first thing to do, is realized if we really want this.I dont know any other open issues.
Thanks.
This is from the first post:
(The kvm part, togather with the kvm-userspace part, was post with V1 before about a week, whoever want to test ksm may download the patch from lkml archive)KSM is a linux driver that allows dynamicly sharing identical memory pages between one or more processes.
Unlike tradtional page sharing that is made at the allocation of the memory, ksm do it dynamicly after the memory was created. Memory is periodically scanned; identical pages are identified and merged.
The sharing is unnoticeable by the process that use this memory. (the shared pages are marked as readonly, and in case of writedo_wp_page()take care to create new copy of the page)To find identical pages ksm use algorithm that is split into three primery levels:
1) Ksm will start scan the memory and will calculate checksum for each page that is registred to be scanned.
(In the first round of the scanning, ksm would only calculate
this checksum for all the pages)2) Ksm will go again on the whole memory and will recalculate the checmsum of the pages, pages that are found to have the same checksum value, would be considered "pages that are most likely wont changed"
Ksm will insert this pages into sorted by page content RB-tree that is called "unstable tree", the reason that this tree is called unstable is due to the fact that the page contents might changed while they are still inside the tree, and therefore the tree would become corrupted.
Due to this problem ksm take two more steps in addition to the checksum calculation:
a) Ksm will throw and recreate the entire unstable tree each round of memory scanning - so if we have corruption, it will be fixed when we will rebuild the tree.
b) Ksm is using RB-tree, that its balancing is made by the node color and not by the content, so even if the page get corrupted, it still would take the same amount of time to search on it.3) In addition to the unstable tree, ksm hold another tree that is called "stable tree" - this tree is RB-tree that is sorted by the pages content and all its pages are write protected, and therefore it cant get corrupted.
Each time ksm will find two identcial pages using the unstable tree, it will create new write-protected shared page, and this page will be inserted into the stable tree, and would be saved there, the stable tree, unlike the unstable tree, is never throwen away, so each page that we find would be saved inside it.Taking into account the three levels that described above, the algorithm work like that:
search primary tree (sorted by entire page contents, pages write protected)
- if match found, merge
- if no match found...
- search secondary tree (sorted by entire page contents, pages not write protected)
- if match found, merge
- remove from secondary tree and insert merged page into primary tree
- if no match found...
- checksum
- if checksum hasn't changed
- insert into secondary tree
- if it has, store updated checksum (note: first time this page is handled it won't have a checksum, so checksum will appear as "changed", so it takes two passes w/ no other matches to get into secondary tree)
- do not insert into any tree, will see it again on next passThe basic idea of this algorithm, is that even if the unstable tree doesnt promise to us to find two identical pages in the first round, we would probably find them in the second or the third or the tenth round, then after we have found this two identical pages only once, we will insert them into the stable tree, and then they would be protected there forever. So the all idea of the unstable tree, is just to build the stable tree and then we will find the identical pages using it.
The current implemantion can be improved alot:
we dont have to calculate exspensive checksum, we can just use the host dirty bit.currently we dont support shared pages swapping (other pages that are not shared can be swapped (all the pages that we didnt find to be identical to other pages...).
Walking on the tree, we keep call to
get_user_pages(), we can optimized it by saving the pfn, and using mmu notifiers to know when the virtual address mapping was changed.We currently scan just programs that were registred to be used by ksm, we would later want to add the abilaty to tell ksm to scan PIDS (so you can scan closed binary applications as well).
Right now ksm scanning is made by just one thread, multiple scanners support might would be needed.
This driver is very useful for KVM as in cases of runing multiple guests operation system of the same type.
(For desktop work loads we have achived more than x2 memory overcommit (more like x3))This driver have found users other than KVM, for example CERN, Fons Rademakers:
"on many-core machines we run one large detector simulation program per core. These simulation programs are identical but run each in their own process and need about 2 - 2.5 GB RAM.
We typically buy machines with 2GB RAM per core and so have a problem to run one of these programs per core.
Of the 2 - 2.5 GB about 700MB is identical data in the form of magnetic field maps, detector geometry, etc.
Currently people have been trying to start one program, initialize the geometry and field maps and then fork it N times, to have the data shared. With KSM this would be done automatically by the system so it sounded extremely attractive when Andrea presented it."I am sending another seires of patchs for kvm kernel and kvm-userspace that would allow users of kvm to test ksm with it.
The kvm patchs would apply to Avi git tree.Izik Eidus (4):
MMU_NOTIFIERS: addset_pte_at_notify()
addpage_wrprotect(): write protecting page. addreplace_page(): change the page pte is pointing to. add ksm kernel shared memory driver.include/linux/ksm.h | 48 ++ include/linux/miscdevice.h | 1 + include/linux/mm.h | 5 + include/linux/mmu_notifier.h | 34 + include/linux/rmap.h | 11 + mm/Kconfig | 6 + mm/Makefile | 1 + mm/ksm.c | 1668 ++++++++++++++++++++++++++++++++++++++++++ mm/memory.c | 90 +++- mm/mmu_notifier.c | 20 + mm/rmap.c | 139 ++++ 11 files changed, 2021 insertions(+), 2 deletions(-) create mode 100644 include/linux/ksm.h create mode 100644 mm/ksm.c--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 14 lines · Izik Eidus
From v1 to v2:
1)Fixed security issue found by Chris Wright:
Ksm was checking if page is a shared page by running !PageAnon. Beacuse that Ksm scan only anonymous memory, all !PageAnons inside ksm data strctures are shared page, however there might be a case fordo_wp_page()when theVM_SHAREDis used wheredo_wp_page()would instead of copying the page into new anonymos page, would reuse the page, it was fixed by adding check for thedirty_bitof the virtual addresses pointing into the shared page. I was not finding any VM code tha would clear the dirty bit from this virtual address (due to the fact that we allocate the page usingpage_alloc()- kernel allocated pages), ~but i still want confirmation about this from the vm guys - thanks.~
As far as I can tell this wasn't a bug and this change is unnecessary. I already checked this bit but I may have missed something, so I ask here to be sure.
As far as I can tell when VM_SHARED is set, no anonymous page can ever be allocated by in that vma range, hence no KSM page can ever be generated in that vma either. MAP_SHARED|MAP_ANONYMOUS is only a different API for /dev/shm, IPCSHM backing, no anonymous pages can live there. It surely worked like that in older 2.6, reading latest code it seems to still work like that, but if something has changed Hugh will surely correct me in a jiffy ;).
I still see this in the file=null path.
} else if (vm_flags & VM_SHARED) {
error = shmem_zero_setup(vma);
if (error)
goto free_vma;
}So you can revert your change for now.
--
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>