kvm support for ksm
Subsystems (4)
cover kvm support for ksm
apply it against Avi git tree.
Izik Eidus (3):
kvm: dont hold pagecount reference for mapped sptes pages.
kvm: add SPTE_HOST_WRITEABLE flag to the shadow ptes.
kvm: add support for change_pte mmu notifiers
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.c | 89 ++++++++++++++++++++++++++++++++------- arch/x86/kvm/paging_tmpl.h | 16 ++++++- virt/kvm/kvm_main.c | 14 ++++++ 4 files changed, 101 insertions(+), 19 deletions(-)
--
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/3 kvm: dont hold pagecount reference for mapped sptes pages.
When using mmu notifiers, we are allowed to remove the page count reference tooken by get_user_pages to a specific page that is mapped inside the shadow page tables.
This is needed so we can balance the pagecount against mapcount checking.
(Right now kvm increase the pagecount and does not increase the mapcount when mapping page into shadow page table entry, so when comparing pagecount against mapcount, you have no reliable result.)
--- arch/x86/kvm/mmu.c | 7 ++----- 1 files changed, 2 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu.c +2 -5
@@ -567,9 +567,7 @@ static void rmap_remove(struct kvm *kvm, u64 *spte)if (*spte & shadow_accessed_mask)kvm_set_pfn_accessed (pfn);if (is_writeble_pte (*spte)) -kvm_release_pfn_dirty (pfn); -else -kvm_release_pfn_clean (pfn); +kvm_set_pfn_dirty (pfn); rmapp =gfn_to_rmap (kvm, sp->gfns[spte - sp->spt],is_large_pte (*spte));if (!*rmapp) {printk (KERN_ERR "rmap_remove: %p %llx 0->BUG\n" , spte, *spte);
@@ -1812,8 +1810,7 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,page_header_update_slot (vcpu->kvm, shadow_pte, gfn);if (!was_rmapped) {rmap_add (vcpu, shadow_pte, gfn, largepage); -if (!is_rmap_pte (*shadow_pte)) -kvm_release_pfn_clean (pfn); +kvm_release_pfn_clean (pfn); }else {if (was_writeble)kvm_release_pfn_dirty (pfn);
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/3 kvm: add SPTE_HOST_WRITEABLE flag to the shadow ptes.
this flag notify that the host physical page we are pointing to from the spte is write protected, and therefore we cant change its access to be write unless we run get_user_pages(write = 1).
(this is needed for change_pte support in kvm)
--- arch/x86/kvm/mmu.c | 14 ++++++++++---- arch/x86/kvm/paging_tmpl.h | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-)
arch/x86/kvm/mmu.c +10 -4
@@ -138,6 +138,8 @@ module_param(oos_shadow, bool, 0644);#define ACC_USER_MASK PT_USER_MASK#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) +#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) +#define SHADOW_PT_INDEX (addr, level) PT64_INDEX(addr, level)struct kvm_rmap_desc {
@@ -1676,7 +1678,7 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, unsigned pte_access, int user_fault, int write_fault, int dirty, int largepage, int global, gfn_t gfn, pfn_t pfn, bool speculative, - bool can_unsync) + bool can_unsync, bool reset_host_protection) { u64 spte;int ret =0 ;
@@ -1719,6 +1721,8 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, kvm_x86_ops->get_mt_mask_shift (); spte |= mt_mask; } +if (reset_host_protection) + spte |=SPTE_HOST_WRITEABLE ; spte |= (u64 )pfn <<PAGE_SHIFT ;
@@ -1764,7 +1768,8 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, unsigned pt_access, unsigned pte_access, int user_fault, int write_fault, int dirty, int *ptwrite, int largepage, int global, - gfn_t gfn, pfn_t pfn, bool speculative) + gfn_t gfn, pfn_t pfn, bool speculative, + bool reset_host_protection) { int was_rmapped = 0;int was_writeble =is_writeble_pte (*shadow_pte);
@@ -1793,7 +1798,8 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, was_rmapped =1 ; }if (set_spte (vcpu, shadow_pte, pte_access, user_fault, write_fault, - dirty, largepage, global, gfn, pfn, speculative, true)) { + dirty, largepage, global, gfn, pfn, speculative, true, + reset_host_protection)) {if (write_fault) *ptwrite =1 ; kvm_x86_ops->tlb_flush (vcpu);
@@ -1840,7 +1846,7 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write, || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {mmu_set_spte (vcpu, iterator.sptep,ACC_ALL ,ACC_ALL ,0 , write,1 , &pt_write, - largepage,0 , gfn, pfn, false); + largepage,0 , gfn, pfn, false, true); ++vcpu->stat.pf_fixed;break ; }
arch/x86/kvm/paging_tmpl.h +13 -3
@@ -259,10 +259,14 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,if (mmu_notifier_retry (vcpu, vcpu->arch.update_pte.mmu_seq))return ;kvm_get_pfn (pfn); +/* +* we call mmu_set_spte() with reset_host_protection = true beacuse that +* vcpu->arch.update_pte.pfn was fetched from get_user_pages(write = 1). +*/ mmu_set_spte (vcpu, spte, page->role.access, pte_access,0 ,0 , gpte &PT_DIRTY_MASK ,NULL , largepage, gpte &PT_GLOBAL_MASK ,gpte_to_gfn (gpte), - pfn, true); + pfn, true, true); }/*
@@ -297,7 +301,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, gw->ptes[gw->level-1 ] &PT_DIRTY_MASK , ptwrite, largepage, gw->ptes[gw->level-1 ] &PT_GLOBAL_MASK , - gw->gfn, pfn, false); + gw->gfn, pfn, false, true);break ; }
@@ -547,6 +551,7 @@ static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,static int FNAME (sync_page )(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) {int i, offset, nr_present; +bool reset_host_protection =1 ; offset = nr_present =0 ;
@@ -584,9 +589,14 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) nr_present++; pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte); +if (!(sp->spt[i] &SPTE_HOST_WRITEABLE )) { + pte_access &= ~PT_WRITABLE_MASK ; + reset_host_protection =0 ; + }set_spte (vcpu, &sp->spt[i], pte_access,0 ,0 ,is_dirty_pte (gpte),0 , gpte &PT_GLOBAL_MASK , gfn, -spte_to_pfn (sp->spt[i]), true, false); +spte_to_pfn (sp->spt[i]), true, false, + reset_host_protection); }return !nr_present;
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/3 kvm: add support for change_pte mmu notifiers
this is needed for kvm if it want ksm to directly map pages into its shadow page tables.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.c | 68 +++++++++++++++++++++++++++++++++++---- virt/kvm/kvm_main.c | 14 ++++++++ 3 files changed, 76 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -791,5 +791,6 @@ asmlinkage void kvm_handle_fault_on_reboot(void);#define KVM_ARCH_WANT_MMU_NOTIFIER int kvm_unmap_hva (struct kvm *kvm,unsigned long hva);int kvm_age_hva (struct kvm *kvm,unsigned long hva); +void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte);#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/mmu.c +61 -7
110 lines not on this page · raw message
virt/kvm/kvm_main.c +14 -0
@@ -846,6 +846,19 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn, } +static void kvm_mmu_notifier_change_pte (struct mmu_notifier *mn, +struct mm_struct *mm, +unsigned long address, +pte_t pte) +{ +struct kvm *kvm =mmu_notifier_to_kvm (mn); + +spin_lock (&kvm->mmu_lock); + kvm->mmu_notifier_seq++; +kvm_set_spte_hva (kvm, address, pte); +spin_unlock (&kvm->mmu_lock); +} + static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start,
@@ -925,6 +938,7 @@ static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start, .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end, .clear_flush_young = kvm_mmu_notifier_clear_flush_young, + .change_pte = kvm_mmu_notifier_change_pte, .release = kvm_mmu_notifier_release, };#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
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 2/3] kvm: add SPTE_HOST_WRITEABLE flag to the shadow ptes.
In reply to Izik EidusQuoted 17 lines · Izik Eidus
this flag notify that the host physical page we are pointing to from the spte is write protected, and therefore we cant change its access to be write unless we run
get_user_pages(write = 1).(this is needed for
change_ptesupport in kvm)Signed-off-by Izik Eidus <[email protected]>arch/x86/kvm/paging_tmpl.h +1
-0@@ -547,6 +551,7 @@ static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,static int FNAME (sync_page )(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) {int i, offset, nr_present; +bool reset_host_protection =1 ; offset = nr_present =0 ;
arch/x86/kvm/paging_tmpl.h static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) · line 595
Quoted 7 lines
arch/x86/kvm/paging_tmpl.h +4
-0@@ -584,9 +589,14 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) nr_present++; pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte); +if (!(sp->spt[i] &SPTE_HOST_WRITEABLE )) { + pte_access &= ~PT_WRITABLE_MASK ; + reset_host_protection =0 ; + }
else
reset_host_protection = 1;Otherwise you clear SPTE_HOST_WRITEABLE for all sptes once you find a single one thats unset.
Also mind the tabs.
Re: [PATCH 2/3] kvm: add SPTE_HOST_WRITEABLE flag to the shadow ptes.
Quoted 33 lines · Marcelo Tosatti
Quoted 19 lines · Izik Eidus
this flag notify that the host physical page we are pointing to from the spte is write protected, and therefore we cant change its access to be write unless we run
get_user_pages(write = 1).(this is needed for
change_ptesupport in kvm)Signed-off-by Izik Eidus <[email protected]>arch/x86/kvm/paging_tmpl.h +1
-0@@ -547,6 +551,7 @@ static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,static int FNAME (sync_page )(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) {int i, offset, nr_present; +bool reset_host_protection =1 ; offset = nr_present =0 ;arch/x86/kvm/paging_tmpl.h static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) · line 596
Quoted 8 lines
arch/x86/kvm/paging_tmpl.h +4
-0@@ -584,9 +589,14 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) nr_present++; pte_access = sp->role.access &FNAME (gpte_access)(vcpu, gpte); +if (!(sp->spt[i] &SPTE_HOST_WRITEABLE )) { + pte_access &= ~PT_WRITABLE_MASK ; + reset_host_protection =0 ; + }else reset_host_protection = 1;
Quoted 5 lines
Otherwise you clear
SPTE_HOST_WRITEABLEfor all sptes once you find a single one thats unset.Also mind the tabs.
Yes, you are right wont work too effective ;-)
Anyway i will repost it as soon as we decide about PATCH 1/ 3 (the #IFDEF thing)
Thank Marcelo :-)
Re: [PATCH 1/3] kvm: dont hold pagecount reference for mapped sptes pages.
In reply to Izik EidusQuoted 11 lines · Izik Eidus
When using mmu notifiers, we are allowed to remove the page count reference tooken by
get_user_pagesto a specific page that is mapped inside the shadow page tables.This is needed so we can balance the pagecount against mapcount checking.
(Right now kvm increase the pagecount and does not increase the mapcount when mapping page into shadow page table entry, so when comparing pagecount against mapcount, you have no reliable result.)
IMO ifdef'ing CONFIG_MMU_NOTIFIERS here (and keeping the ref if unset) instead of in the backward compat code gives less room for headaches.
Re: [PATCH 1/3] kvm: dont hold pagecount reference for mapped sptes pages.
Quoted 9 lines · Marcelo Tosatti
Quoted 7 lines · Izik Eidus
When using mmu notifiers, we are allowed to remove the page count reference tooken by
get_user_pagesto a specific page that is mapped inside the shadow page tables.This is needed so we can balance the pagecount against mapcount checking.
Quoted 10 lines
Quoted 5 lines
(Right now kvm increase the pagecount and does not increase the mapcount when mapping page into shadow page table entry, so when comparing pagecount against mapcount, you have no reliable result.)
IMO ifdef'ing
CONFIG_MMU_NOTIFIERShere (and keeping the ref if unset) instead of in the backward compat code gives less room for headaches.
That was the first version of this patch, Avi preferred not to do it...
Re: [PATCH 1/3] kvm: dont hold pagecount reference for mapped sptes pages.
Quoted 16 lines · Izik Eidus
Quoted 15 lines · Marcelo Tosatti
Quoted 12 lines · Izik Eidus
When using mmu notifiers, we are allowed to remove the page count reference tooken by
get_user_pagesto a specific page that is mapped inside the shadow page tables.This is needed so we can balance the pagecount against mapcount checking.
(Right now kvm increase the pagecount and does not increase the mapcount when mapping page into shadow page table entry, so when comparing pagecount against mapcount, you have no reliable result.)
Quoted 6 lines
Quoted 4 lines
IMO ifdef'ing
CONFIG_MMU_NOTIFIERShere (and keeping the ref if unset) instead of in the backward compat code gives less room for headaches.That was the first version of this patch, Avi preferred not to do it...
Avi, You mind if i changed it to use the IFDEF ?
Re: [PATCH 1/3] kvm: dont hold pagecount reference for mapped sptes pages.
Quoted 17 lines · Izik Eidus
Quoted 16 lines · Izik Eidus
Quoted 15 lines · Marcelo Tosatti
Quoted 12 lines · Izik Eidus
When using mmu notifiers, we are allowed to remove the page count reference tooken by
get_user_pagesto a specific page that is mapped inside the shadow page tables.This is needed so we can balance the pagecount against mapcount checking.
(Right now kvm increase the pagecount and does not increase the mapcount when mapping page into shadow page table entry, so when comparing pagecount against mapcount, you have no reliable result.)
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
IMO ifdef'ing
CONFIG_MMU_NOTIFIERShere (and keeping the ref if unset) instead of in the backward compat code gives less room for headaches.That was the first version of this patch, Avi preferred not to do it...
Avi, You mind if i changed it to use the IFDEF ?
No, let's not have too many ifdefs in the code. I'll hack it in hack-module.awk.
Signature · 1 line
error compiling committee.c: too many arguments to function
Hi, I'm learning kvm-mmu codes, when I was reading codes from this patch,
I can't understand why we need to do special process for "writable pte".
Quoted 28 lines
(quoted diff) +28
-0+static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, + unsigned long data) +{ + int need_flush = 0; + u64 *spte, new_spte; + pte_t *ptep = (pte_t *)data; + pfn_t new_pfn; + + new_pfn = pte_pfn(ptep_val(ptep)); + spte = rmap_next(kvm, rmapp, NULL); + while (spte) { + BUG_ON(!is_shadow_present_pte(*spte)); + rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte); + need_flush = 1; + if (pte_write(ptep_val(ptep))) { + rmap_remove(kvm, spte); + set_shadow_pte(spte, shadow_trap_nonpresent_pte); + spte = rmap_next(kvm, rmapp, NULL); + } else { + new_spte = *spte &~ (PT64_BASE_ADDR_MASK); + new_spte |= new_pfn << PAGE_SHIFT; + + if (!pte_write(ptep_val(ptep))) { + new_spte &= ~PT_WRITABLE_MASK; + new_spte &= ~SPTE_HOST_WRITEABLE; + if (is_writeble_pte(*spte)) + kvm_set_pfn_dirty(spte_to_pfn(*spte)); + }
Review
Quoted 10 lines
(quoted diff) +10
-0+ set_shadow_pte(spte, new_spte); + spte = rmap_next(kvm, rmapp, spte); + } + } + if (need_flush) + kvm_flush_remote_tlbs(kvm); + + return 0; +} +
In my opinion, we can just regard writable pte same as readable/executable,
all the corresponding sptes will be set as write-protect, and when guest
access them, an EPT-violation occurs and we do this #PF in kvm.
Shall anyone has some hint ?
Quoted 20 lines · Izik Eidus
apply it against Avi git tree.
Izik Eidus (3):
kvm: dont hold pagecount reference for mapped sptes pages.
kvm: addSPTE_HOST_WRITEABLEflag to the shadow ptes.
kvm: add support forchange_ptemmu notifiersarch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.c | 89 ++++++++++++++++++++++++++++++++------- arch/x86/kvm/paging_tmpl.h | 16 ++++++- virt/kvm/kvm_main.c | 14 ++++++ 4 files changed, 101 insertions(+), 19 deletions(-)--
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 13 lines · Izik Eidus
apply it against Avi git tree.
Izik Eidus (3):
kvm: dont hold pagecount reference for mapped sptes pages.
kvm: addSPTE_HOST_WRITEABLEflag to the shadow ptes.
kvm: add support forchange_ptemmu notifiersarch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.c | 89 ++++++++++++++++++++++++++++++++------- arch/x86/kvm/paging_tmpl.h | 16 ++++++- virt/kvm/kvm_main.c | 14 ++++++ 4 files changed, 101 insertions(+), 19 deletions(-)
Quoted 7 lines
--
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, I'm learning kvm-mmu codes, when I was reading codes from this patch,
I can't understand why we need to do special process for "writable pte".
Quoted 28 lines
(quoted diff) +28
-0+static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, + unsigned long data) +{ + int need_flush = 0; + u64 *spte, new_spte; + pte_t *ptep = (pte_t *)data; + pfn_t new_pfn; + + new_pfn = pte_pfn(ptep_val(ptep)); + spte = rmap_next(kvm, rmapp, NULL); + while (spte) { + BUG_ON(!is_shadow_present_pte(*spte)); + rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte); + need_flush = 1; + if (pte_write(ptep_val(ptep))) { + rmap_remove(kvm, spte); + set_shadow_pte(spte, shadow_trap_nonpresent_pte); + spte = rmap_next(kvm, rmapp, NULL); + } else { + new_spte = *spte &~ (PT64_BASE_ADDR_MASK); + new_spte |= new_pfn << PAGE_SHIFT; + + if (!pte_write(ptep_val(ptep))) { + new_spte &= ~PT_WRITABLE_MASK; + new_spte &= ~SPTE_HOST_WRITEABLE; + if (is_writeble_pte(*spte)) + kvm_set_pfn_dirty(spte_to_pfn(*spte)); + }
Review
Quoted 10 lines
(quoted diff) +10
-0+ set_shadow_pte(spte, new_spte); + spte = rmap_next(kvm, rmapp, spte); + } + } + if (need_flush) + kvm_flush_remote_tlbs(kvm); + + return 0; +} +
In my opinion, we can just regard writable pte same as readable/executable,
all the corresponding sptes will be set as write-protect, and when guest
access them, an EPT-violation occurs and we do this #PF in kvm.
Shall anyone has some hint ?