Support for GB pages in KVM
Subsystems (5)
cover Support for GB pages in KVM
Hi,
this patchset extends the KVM MMU implementation to support 1GB pages as supported by AMD family 16 processors. These patches enable support for 1 GB pages with Nested Paging. Support for these pages in the shadow paging code was also developed but does not run stable yet. The patch for shadow-paging support is not included in this series and will be sent out seperatly.
Joerg
git diff --stat avi/master..
arch/ia64/kvm/process.c | 5 - arch/ia64/kvm/vmm_ivt.S | 18 ++-- arch/ia64/kvm/vtlb.c | 3 +- arch/x86/include/asm/kvm_host.h | 29 ++++- arch/x86/kvm/mmu.c | 232 +++++++++++++++++++++++++++------------ arch/x86/kvm/paging_tmpl.h | 48 ++++++--- arch/x86/kvm/svm.c | 7 + arch/x86/kvm/vmx.c | 19 ++- arch/x86/kvm/x86.c | 6 +- include/linux/kvm.h | 1 + include/linux/kvm_host.h | 2 +- mm/hugetlb.c | 1 + virt/kvm/kvm_main.c | 26 +++++
PATCH 1/7 hugetlb: export vma_kernel_pagesize to modules
This function is required in the KVM module to determine the size of the backing page size.
--- mm/hugetlb.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
mm/hugetlb.c +1 -0
@@ -234,6 +234,7 @@ unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)return 1UL << (hstate->order +PAGE_SHIFT ); } +EXPORT_SYMBOL_GPL (vma_kernel_pagesize);/* * Return the page size being used by the MMU to back a VMA. In the majority
Signature · 1 line
1.5.6.4
PATCH 2/7 kvm mmu: infrastructure changes for multiple huge page support
In reply to Joerg RoedelThis patch includes most of the necessary changes to the KVM SoftMMU for supporting more than one huge page size. The changes in this patch include:
* introduce 'enum kvm_page_size' which is used to represent the page size used
* change boolean is_largepage_backed() function to backing_size() which returns the largest page size KVM can use to map a gfn
* change the other largepage flags to 'enum kvm_page_size'
--- arch/x86/include/asm/kvm_host.h | 17 ++++-- arch/x86/kvm/mmu.c | 111 +++++++++++++++++++++------------------ arch/x86/kvm/paging_tmpl.h | 22 ++++---- 3 files changed, 83 insertions(+), 67 deletions(-)
arch/x86/include/asm/kvm_host.h +12 -5
@@ -52,11 +52,13 @@#define UNMAPPED_GVA (~(gpa_t)0)/* shadow tables are PAE even on non-PAE hosts */ -#define KVM_HPAGE_SHIFT 21 -#define KVM_HPAGE_SIZE (1UL << KVM_HPAGE_SHIFT) -#define KVM_HPAGE_MASK (~(KVM_HPAGE_SIZE - 1)) +#define KVM_2M_PAGE_SHIFT 21 +#define KVM_2M_PAGE_SIZE (1UL << KVM_2M_PAGE_SHIFT) +#define KVM_2M_PAGE_MASK (~(KVM_2M_PAGE_SIZE - 1)) -#define KVM_PAGES_PER_HPAGE (KVM_HPAGE_SIZE / PAGE_SIZE) +#define KVM_PAGES_PER_2M_PAGE (KVM_2M_PAGE_SIZE / PAGE_SIZE) + +#define KVM_PAGES_PER_HPAGE KVM_PAGES_PER_2M_PAGE#define DE_VECTOR 0#define DB_VECTOR 1
@@ -263,6 +265,11 @@ struct kvm_mmu {u64 *pae_root; }; +enum kvm_page_size { + KVM_PAGE_SIZE_4k = (1 <<12 ), +KVM_PAGE_SIZE_2M = (1 <<21 ), +}; +struct kvm_vcpu_arch {u64 host_tsc;int interrupt_window_open;
@@ -310,7 +317,7 @@ struct kvm_vcpu_arch {struct {gfn_t gfn;/* presumed gfn during guest pte update */ pfn_t pfn;/* pfn corresponding to that gfn */ -int largepage; +enum kvm_page_size page_size;unsigned long mmu_seq; } update_pte;
arch/x86/kvm/mmu.c +59 -52
307 lines not on this page · raw message
arch/x86/kvm/paging_tmpl.h +12 -10
@@ -241,7 +241,7 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,pt_element_t gpte;unsigned pte_access;pfn_t pfn; -int largepage = vcpu->arch.update_pte.largepage; +enum kvm_page_size psize = vcpu->arch.update_pte.page_size; gpte = *(const pt_element_t *)pte;if (~gpte & (PT_PRESENT_MASK |PT_ACCESSED_MASK )) {
@@ -260,7 +260,7 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,return ;kvm_get_pfn (pfn);mmu_set_spte (vcpu, spte, page->role.access, pte_access,0 ,0 , - gpte &PT_DIRTY_MASK ,NULL , largepage, + gpte &PT_DIRTY_MASK ,NULL , psize, gpte &PT_GLOBAL_MASK ,gpte_to_gfn (gpte), pfn, true); }
@@ -270,7 +270,8 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,*/ static u64 *FNAME (fetch )(struct kvm_vcpu *vcpu,gva_t addr,struct guest_walker *gw, -int user_fault,int write_fault,int largepage, +int user_fault,int write_fault, +enum kvm_page_size psize,int *ptwrite,pfn_t pfn) {unsigned access = gw->pt_access;
@@ -290,12 +291,13 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, level = iterator.level; sptep = iterator.sptep;if (level ==PT_PAGE_TABLE_LEVEL - || (largepage && level ==PT_DIRECTORY_LEVEL )) { + || (psize ==KVM_PAGE_SIZE_2M && + level ==PT_DIRECTORY_LEVEL )) {mmu_set_spte (vcpu, sptep, access, gw->pte_access & access, user_fault, write_fault, gw->ptes[gw->level-1 ] &PT_DIRTY_MASK , - ptwrite, largepage, + ptwrite, psize, gw->ptes[gw->level-1 ] &PT_GLOBAL_MASK , gw->gfn, pfn, false);break ;
@@ -368,7 +370,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,int write_pt =0 ;int r;pfn_t pfn; -int largepage =0 ; +enum kvm_page_size psize = KVM_PAGE_SIZE_4k;unsigned long mmu_seq;pgprintk ("%s: addr %lx err %x\n" , __func__, addr, error_code);
@@ -396,10 +398,10 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,if (walker.level ==PT_DIRECTORY_LEVEL ) {gfn_t large_gfn; - large_gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE -1 ); -if (is_largepage_backed (vcpu, large_gfn)) { + large_gfn = walker.gfn & ~(KVM_PAGES_PER_2M_PAGE -1 ); +if (backing_size (vcpu, large_gfn) != KVM_PAGE_SIZE_4k) { walker.gfn = large_gfn; - largepage =1 ; + psize =KVM_PAGE_SIZE_2M ; } } mmu_seq = vcpu->kvm->mmu_notifier_seq;
@@ -418,7 +420,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, goto out_unlock;kvm_mmu_free_some_pages (vcpu); shadow_pte =FNAME (fetch)(vcpu, addr, &walker, user_fault, write_fault, - largepage, &write_pt, pfn); + psize, &write_pt, pfn);pgprintk ("%s: shadow pte %p %llx ptwrite %d\n" , __func__, shadow_pte, *shadow_pte, write_pt);
Signature · 1 line
1.5.6.4
Re: [PATCH 2/7] kvm mmu: infrastructure changes for multiple huge page support
Quoted 7 lines · Joerg Roedel
This patch includes most of the necessary changes to the KVM SoftMMU for supporting more than one huge page size. The changes in this patch include:
* introduce 'enum
kvm_page_size' which is used to represent the page size used
How about the concept of page_level instead, which returns the level of a page in terms of the paging hierarchy? Would be easier to compare against in the various walkers.
I've been thinking for a while to make levels zero-based, so 0 would be a 4K page, 1 a 2M page, and 2 a 1GB page.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 3/7 kvm mmu: add page size parameter to rmap_remove
In reply to Joerg RoedelTwo reasons for this:
* To make rmap_remove interface consistent with rmap_add which already has a page size parameter
* When supporting more than one huge page size rmap_remove can't guess the huge page size anymore
--- arch/x86/kvm/mmu.c | 40 ++++++++++++++++++++++------------------ arch/x86/kvm/paging_tmpl.h | 10 +++++++--- 2 files changed, 29 insertions(+), 21 deletions(-)
arch/x86/kvm/mmu.c +22 -18
@@ -556,7 +556,7 @@ static void rmap_desc_remove_entry(unsigned long *rmapp,mmu_free_rmap_desc (desc); } -static void rmap_remove (struct kvm *kvm,u64 *spte) +static void rmap_remove (struct kvm *kvm,u64 *spte,enum kvm_page_size psize) {struct kvm_rmap_desc *desc;struct kvm_rmap_desc *prev_desc;
@@ -564,7 +564,6 @@ static void rmap_remove(struct kvm *kvm, u64 *spte)pfn_t pfn;unsigned long *rmapp;int i; -enum kvm_page_size psize;if (!is_rmap_pte (*spte))return ;
@@ -576,7 +575,6 @@ static void rmap_remove(struct kvm *kvm, u64 *spte)kvm_release_pfn_dirty (pfn); elsekvm_release_pfn_clean (pfn ); - psize =is_large_pte (*spte) ?KVM_PAGE_SIZE_2M : KVM_PAGE_SIZE_4k; rmapp =gfn_to_rmap (kvm, sp->gfns[spte - sp->spt], psize);if (!*rmapp) {printk (KERN_ERR "rmap_remove: %p %llx 0->BUG\n" , spte, *spte);
@@ -673,7 +671,7 @@ static int rmap_write_protect(struct kvm *kvm, u64 gfn)BUG_ON ((*spte & (PT_PAGE_SIZE_MASK |PT_PRESENT_MASK )) != (PT_PAGE_SIZE_MASK |PT_PRESENT_MASK ));pgprintk ("rmap_write_protect(large): spte %p %llx %lld\n" , spte, *spte, gfn);if (is_writeble_pte (*spte)) { -rmap_remove (kvm, spte); +rmap_remove (kvm, spte,KVM_PAGE_SIZE_2M ); --kvm->stat.lpages;set_shadow_pte (spte, shadow_trap_nonpresent_pte); spte =NULL ;
@@ -685,7 +683,8 @@ static int rmap_write_protect(struct kvm *kvm, u64 gfn)return write_protected; } -static int kvm_unmap_rmapp (struct kvm *kvm,unsigned long *rmapp) +static int kvm_unmap_rmapp (struct kvm *kvm,unsigned long *rmapp, +enum kvm_page_size psize) {u64 *spte;int need_tlb_flush =0 ;
@@ -693,7 +692,7 @@ static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)while ((spte =rmap_next (kvm, rmapp,NULL ))) {BUG_ON (!(*spte &PT_PRESENT_MASK ));rmap_printk ("kvm_rmap_unmap_hva: spte %p %llx\n" , spte, *spte); -rmap_remove (kvm, spte); +rmap_remove (kvm, spte, psize);set_shadow_pte (spte, shadow_trap_nonpresent_pte); need_tlb_flush =1 ; }
@@ -701,7 +700,8 @@ static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp) }static int kvm_handle_hva (struct kvm *kvm,unsigned long hva, -int (*handler)(struct kvm *kvm,unsigned long *rmapp)) +int (*handler)(struct kvm *kvm,unsigned long *rmapp, +enum kvm_page_size psize)) {int i;int retval =0 ;
@@ -722,11 +722,12 @@ static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, end = start + (memslot->npages <<PAGE_SHIFT );if (hva >= start && hva < end) {gfn_t gfn_offset = (hva - start) >>PAGE_SHIFT ; - retval |=handler (kvm, &memslot->rmap[gfn_offset]); +unsigned long lidx = gfn_offset /KVM_PAGES_PER_2M_PAGE ; + retval |=handler (kvm, &memslot->rmap[gfn_offset], + KVM_PAGE_SIZE_4k); retval |=handler (kvm, - &memslot->lpage_info[ - gfn_offset / -KVM_PAGES_PER_2M_PAGE ].rmap_pde); + &memslot->lpage_info[lidx].rmap_pde, +KVM_PAGE_SIZE_2M ); } }
@@ -738,7 +739,8 @@ int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)return kvm_handle_hva (kvm, hva, kvm_unmap_rmapp); } -static int kvm_age_rmapp (struct kvm *kvm,unsigned long *rmapp) +static int kvm_age_rmapp (struct kvm *kvm,unsigned long *rmapp, +enum kvm_page_size psize) {u64 *spte;int young =0 ;
@@ -1312,7 +1314,7 @@ static void kvm_mmu_page_unlink_children(struct kvm *kvm,if (sp->role.level ==PT_PAGE_TABLE_LEVEL ) {for (i =0 ; i <PT64_ENT_PER_PAGE ; ++i) {if (is_shadow_present_pte (pt[i])) -rmap_remove (kvm, &pt[i]); +rmap_remove (kvm, &pt[i], KVM_PAGE_SIZE_4k); pt[i] = shadow_trap_nonpresent_pte; }return ;
@@ -1328,7 +1330,7 @@ static void kvm_mmu_page_unlink_children(struct kvm *kvm, &pt[i]); }else { --kvm->stat.lpages; -rmap_remove (kvm, &pt[i]); +rmap_remove (kvm, &pt[i],KVM_PAGE_SIZE_2M ); } } pt[i] = shadow_trap_nonpresent_pte;
@@ -1798,7 +1800,7 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, }else if (pfn !=spte_to_pfn (*shadow_pte)) {pgprintk ("hfn old %lx new %lx\n" ,spte_to_pfn (*shadow_pte), pfn); -rmap_remove (vcpu->kvm, shadow_pte); +rmap_remove (vcpu->kvm, shadow_pte, psize); }else was_rmapped =1 ; }
@@ -2320,9 +2322,11 @@ static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, pte = *spte;if (is_shadow_present_pte (pte)) { -if (sp->role.level ==PT_PAGE_TABLE_LEVEL || -is_large_pte (pte)) -rmap_remove (vcpu->kvm, spte); +if (sp->role.level ==PT_PAGE_TABLE_LEVEL ) +rmap_remove (vcpu->kvm, spte, KVM_PAGE_SIZE_4k); +else if (is_large_pte (pte) && + sp->role.level ==PT_DIRECTORY_LEVEL ) +rmap_remove (vcpu->kvm, spte,KVM_PAGE_SIZE_2M );else { child =page_header (pte &PT64_BASE_ADDR_MASK );mmu_page_remove_parent_pte (child, spte);
arch/x86/kvm/paging_tmpl.h +7 -3
@@ -307,7 +307,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,continue ;if (is_large_pte (*sptep)) { -rmap_remove (vcpu->kvm, sptep); +rmap_remove (vcpu->kvm, sptep,KVM_PAGE_SIZE_2M );set_shadow_pte (sptep, shadow_trap_nonpresent_pte);kvm_flush_remote_tlbs (vcpu->kvm); }
@@ -459,12 +459,16 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)if (level ==PT_PAGE_TABLE_LEVEL || ((level ==PT_DIRECTORY_LEVEL ) &&is_large_pte (*sptep))) {struct kvm_mmu_page *sp =page_header (__pa (sptep)); +enum kvm_page_size psize = KVM_PAGE_SIZE_4k; + +if (level ==PT_DIRECTORY_LEVEL ) + psize =KVM_PAGE_SIZE_2M ; pte_gpa = (sp->gfn <<PAGE_SHIFT ); pte_gpa += (sptep - sp->spt) *sizeof (pt_element_t);if (is_shadow_present_pte (*sptep)) { -rmap_remove (vcpu->kvm, sptep); +rmap_remove (vcpu->kvm, sptep, psize);if (is_large_pte (*sptep)) --vcpu->kvm->stat.lpages; need_flush =1 ;
@@ -575,7 +579,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) !(gpte & PT_ACCESSED_MASK)) {u64 nonpresent; -rmap_remove (vcpu->kvm, &sp->spt[i]); +rmap_remove (vcpu->kvm, &sp->spt[i], KVM_PAGE_SIZE_4k);if (is_present_pte (gpte)) nonpresent = shadow_trap_nonpresent_pte;else
Signature · 1 line
1.5.6.4
PATCH 4/7 kvm mmu: implement necessary data structures for second huge page accounting
In reply to Joerg RoedelThis patch adds the necessary data structures to take care of write protections in place within a second huge page sized page.
--- arch/x86/include/asm/kvm_host.h | 10 ++++++++++ arch/x86/kvm/mmu.c | 33 +++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 26 ++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletions(-)
arch/x86/include/asm/kvm_host.h +10 -0
@@ -58,7 +58,16 @@#define KVM_PAGES_PER_2M_PAGE (KVM_2M_PAGE_SIZE / PAGE_SIZE) +#define KVM_1G_PAGE_SHIFT 30 +#define KVM_1G_PAGE_SIZE (1UL << KVM_1G_PAGE_SHIFT) +#define KVM_1G_PAGE_MASK (~(KVM_1G_PAGE_SIZE - 1)) + +#define KVM_PAGES_PER_1G_PAGE (KVM_1G_PAGE_SIZE / PAGE_SIZE) + +/* Huge Page */ #define KVM_PAGES_PER_HPAGE KVM_PAGES_PER_2M_PAGE +/* Large Huge Page ;-) */ +#define KVM_PAGES_PER_LHPAGE KVM_PAGES_PER_1G_PAGE#define DE_VECTOR 0#define DB_VECTOR 1
@@ -268,6 +277,7 @@ struct kvm_mmu {enum kvm_page_size { KVM_PAGE_SIZE_4k = (1 <<12 ),KVM_PAGE_SIZE_2M = (1 <<21 ), +KVM_PAGE_SIZE_1G = (1 <<30 ), };struct kvm_vcpu_arch {
arch/x86/kvm/mmu.c +33 -0
@@ -390,6 +390,15 @@ static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)return &slot->lpage_info[idx].write_count; } +static int *slot_hugepage_idx (gfn_t gfn,struct kvm_memory_slot *slot) +{ +unsigned long idx; + + idx = (gfn /KVM_PAGES_PER_1G_PAGE ) - + (slot->base_gfn /KVM_PAGES_PER_1G_PAGE ); +return &slot->hpage_info[idx].write_count; +} +static void account_shadowed (struct kvm *kvm,gfn_t gfn) {int *write_count;
@@ -398,6 +407,10 @@ static void account_shadowed(struct kvm *kvm, gfn_t gfn) write_count =slot_largepage_idx (gfn,gfn_to_memslot_unaliased (kvm, gfn)); *write_count +=1 ; + + write_count =slot_hugepage_idx (gfn, +gfn_to_memslot_unaliased (kvm, gfn)); + *write_count +=1 ; }static void unaccount_shadowed (struct kvm *kvm,gfn_t gfn)
@@ -409,6 +422,11 @@ static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn) gfn_to_memslot_unaliased(kvm, gfn)); *write_count -=1 ;WARN_ON (*write_count <0 ); + + write_count =slot_hugepage_idx (gfn, +gfn_to_memslot_unaliased (kvm, gfn)); + *write_count -=1 ; +WARN_ON (*write_count <0 ); }static int has_wrprotected_page (struct kvm *kvm,gfn_t gfn)
@@ -426,6 +444,21 @@ static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)return 1 ; } +static int has_wrprotected_largepage (struct kvm *kvm,gfn_t gfn) +{ +struct kvm_memory_slot *slot; +int *hugepage_idx; + + gfn =unalias_gfn (kvm, gfn); + slot =gfn_to_memslot_unaliased (kvm, gfn); +if (slot) { + hugepage_idx =slot_hugepage_idx (gfn, slot); +return *hugepage_idx; + } + +return 1 ; +} +static enum kvm_page_size host_page_size (struct kvm *kvm,gfn_t gfn) {struct vm_area_struct *vma;
include/linux/kvm_host.h +1 -1
@@ -103,7 +103,7 @@ struct kvm_memory_slot {struct {unsigned long rmap_pde;int write_count; - } *lpage_info; + } *lpage_info, *hpage_info;unsigned long userspace_addr;int user_alloc; };
virt/kvm/kvm_main.c +26 -0
@@ -1001,10 +1001,14 @@ static void kvm_free_physmem_slot(struct kvm_memory_slot *free,if (!dont || free->lpage_info != dont->lpage_info)vfree (free->lpage_info); +if (!dont || free->hpage_info != dont->hpage_info) +vfree (free->hpage_info); + free->npages =0 ; free->dirty_bitmap =NULL ; free->rmap =NULL ; free->lpage_info =NULL ; + free->hpage_info =NULL ; }void kvm_free_physmem (struct kvm *kvm)
@@ -1170,6 +1174,28 @@ int __kvm_set_memory_region(struct kvm *kvm, new.lpage_info[largepages-1 ].write_count =1 ; } +#ifdef KVM_PAGES_PER_LHPAGE +if (npages && !new.hpage_info) { +int hugepages = npages /KVM_PAGES_PER_LHPAGE ; +if (npages %KVM_PAGES_PER_LHPAGE ) + hugepages++; +if (base_gfn %KVM_PAGES_PER_LHPAGE ) + hugepages++; + + new.hpage_info =vmalloc (hugepages *sizeof (*new.hpage_info)); + +if (!new.hpage_info) + goto out_free; + +memset (new.hpage_info,0 , hugepages *sizeof (*new.hpage_info)); + +if (base_gfn %KVM_PAGES_PER_LHPAGE ) + new.hpage_info[0 ].write_count =1 ; +if ((base_gfn+npages) %KVM_PAGES_PER_LHPAGE ) + new.hpage_info[hugepages-1 ].write_count =1 ; + } +#endif +/* Allocate page dirty bitmap if needed */ if ((new.flags &KVM_MEM_LOG_DIRTY_PAGES ) && !new.dirty_bitmap) {unsigned dirty_bytes =ALIGN (npages,BITS_PER_LONG ) /8 ;
Signature · 1 line
1.5.6.4
Re: [PATCH 4/7] kvm mmu: implement necessary data structures for second huge page accounting
Quoted 12 lines · Joerg Roedel
This patch adds the necessary data structures to take care of write protections in place within a second huge page sized page.
struct
kvm_vcpu_arch{arch/x86/kvm/mmu.c +0
-0@@ -390,6 +390,15 @@ static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)return &slot->lpage_info[idx].write_count; }
arch/x86/kvm/mmu.c static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot) · line 401
Quoted 10 lines
arch/x86/kvm/mmu.c +8
-0+static int *slot_hugepage_idx (gfn_t gfn,struct kvm_memory_slot *slot) +{ +unsigned long idx; + + idx = (gfn /KVM_PAGES_PER_1G_PAGE ) - + (slot->base_gfn /KVM_PAGES_PER_1G_PAGE ); +return &slot->hpage_info[idx].write_count; +}
A page level argument would remove the need for this duplication, as well as all the constants.
arch/x86/kvm/mmu.c static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn) · line 455
Quoted 11 lines
arch/x86/kvm/mmu.c +9
-0+static int has_wrprotected_largepage (struct kvm *kvm,gfn_t gfn) +{ +struct kvm_memory_slot *slot; +int *hugepage_idx; + + gfn =unalias_gfn (kvm, gfn); + slot =gfn_to_memslot_unaliased (kvm, gfn); +if (slot) { + hugepage_idx =slot_hugepage_idx (gfn, slot);
slot_largepage_idx() here?
I don't think we ever write protect large pages, so why is this needed?
Quoted 17 lines
arch/x86/kvm/mmu.c +6
-0+return *hugepage_idx; + } + +return 1 ; +} +static enum kvm_page_size host_page_size (struct kvm *kvm,gfn_t gfn) {struct vm_area_struct *vma;include/linux/kvm_host.h +0
-0
include/linux/kvm_host.h struct kvm_memory_slot { · line 107
Quoted 6 lines
include/linux/kvm_host.h +1
-1@@ -103,7 +103,7 @@ struct kvm_memory_slot {struct {unsigned long rmap_pde;int write_count; - } *lpage_info; + } *lpage_info, *hpage_info;
} * lpage_info[KVM_NR_PAGE_LEVELS];
virt/kvm/kvm_main.c static void kvm_free_physmem_slot(struct kvm_memory_slot *free, · line 1006
Quoted 11 lines
virt/kvm/kvm_main.c +2
-0@@ -1001,10 +1001,14 @@ static void kvm_free_physmem_slot(struct kvm_memory_slot *free,if (!dont || free->lpage_info != dont->lpage_info)vfree (free->lpage_info); +if (!dont || free->hpage_info != dont->hpage_info) +vfree (free->hpage_info);
loop
Quoted 17 lines
virt/kvm/kvm_main.c +12
-0void kvm_free_physmem (struct kvm *kvm)@@ -1170,6 +1174,28 @@ int __kvm_set_memory_region(struct kvm *kvm, new.lpage_info[largepages-1 ].write_count =1 ; } +#ifdef KVM_PAGES_PER_LHPAGE +if (npages && !new.hpage_info) { +int hugepages = npages /KVM_PAGES_PER_LHPAGE ; +if (npages %KVM_PAGES_PER_LHPAGE ) + hugepages++; +if (base_gfn %KVM_PAGES_PER_LHPAGE ) + hugepages++; + + new.hpage_info =vmalloc (hugepages *sizeof (*new.hpage_info)); + +if (!new.hpage_info) + goto out_free;
virt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 1198
Quoted 10 lines
virt/kvm/kvm_main.c +9
-0+ +memset (new.hpage_info,0 , hugepages *sizeof (*new.hpage_info)); + +if (base_gfn %KVM_PAGES_PER_LHPAGE ) + new.hpage_info[0 ].write_count =1 ; +if ((base_gfn+npages) %KVM_PAGES_PER_LHPAGE ) + new.hpage_info[hugepages-1].write_count = 1; + } +#endif
Loop, KVM_NR_PAGE_LEVELS defined per arch.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 4/7] kvm mmu: implement necessary data structures for second huge page accounting
Quoted 39 lines · Avi Kivity
Quoted 11 lines · Joerg Roedel
This patch adds the necessary data structures to take care of write protections in place within a second huge page sized page.
struct kvm_vcpu_arch {arch/x86/kvm/mmu.c +0
-0@@ -390,6 +390,15 @@ static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)return &slot->lpage_info[idx].write_count; }arch/x86/kvm/mmu.c static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot) · line 401
Quoted 10 lines
arch/x86/kvm/mmu.c +7
-0+static int *slot_hugepage_idx(gfn_t gfn, struct kvm_memory_slot *slot) +{ +unsigned long idx; + + idx = (gfn /KVM_PAGES_PER_1G_PAGE ) - + (slot->base_gfn /KVM_PAGES_PER_1G_PAGE ); +return &slot->hpage_info[idx].write_count; +}A page level argument would remove the need for this duplication, as well as all the constants.
arch/x86/kvm/mmu.c static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn) · line 455
Quoted 10 lines
arch/x86/kvm/mmu.c +8
-0+static int has_wrprotected_largepage(struct kvm *kvm, gfn_t gfn) +{ +struct kvm_memory_slot *slot; +int *hugepage_idx; + + gfn =unalias_gfn (kvm, gfn); + slot =gfn_to_memslot_unaliased (kvm, gfn); +if (slot) { + hugepage_idx =slot_hugepage_idx (gfn, slot);
slot_largepage_idx()here?
Quoted 1 line
I don't think we ever write protect large pages, so why is this needed?
For 2mb pages we need to check if there is a write-protected 4k page in it before we map a 2mb page for writing. If there is any write-protected 4k page in a 2mb area this 2mb page is considered write-protected. These 'write-protected' 2mb pages are accounted in the account_shadow() function. This information is taken into account when we decide if we can map a guest 1gb page as a 1gb page on the host too.
Joerg
Re: [PATCH 4/7] kvm mmu: implement necessary data structures for second huge page accounting
Quoted 17 lines · Joerg Roedel
Quoted 14 lines
arch/x86/kvm/mmu.c static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn) · line 455
Quoted 12 lines
+static int
has_wrprotected_largepage(struct kvm *kvm, gfn_t gfn)arch/x86/kvm/mmu.c +8
-0+{ +struct kvm_memory_slot *slot; +int *hugepage_idx; + + gfn =unalias_gfn (kvm, gfn); + slot =gfn_to_memslot_unaliased (kvm, gfn); +if (slot) { + hugepage_idx =slot_hugepage_idx (gfn, slot);
slot_largepage_idx()here?Quoted 2 lines
I don't think we ever write protect large pages, so why is this needed?
Quoted 7 lines
For 2mb pages we need to check if there is a write-protected 4k page in it before we map a 2mb page for writing. If there is any write-protected 4k page in a 2mb area this 2mb page is considered write-protected. These 'write-protected' 2mb pages are accounted in the
account_shadow()function. This information is taken into account when we decide if we can map a guest 1gb page as a 1gb page on the host too.
account_shadowed() actually increments a hugepage write_count by 1 for every 4K page, not 2M page, if I read the code correctly. The code I commented on is right though.
The naming is confusing. I suggest has_wrprotected_page_in_{large,huge}page(). although with the a level parameter we can keep has_wrprotected_page().
btw, if we implement account_shadow() as you describe it (only account hugepages on largepage transition 0->1 or 1->0) we save a potential cacheline bounce on the hugepage write_count accounting array.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 4/7] kvm mmu: implement necessary data structures for second huge page accounting
Quoted 25 lines · Avi Kivity
Quoted 16 lines · Joerg Roedel
Quoted 13 lines
arch/x86/kvm/mmu.c static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn) · line 455
Quoted 11 lines
+static int
has_wrprotected_largepage(struct kvm *kvm, gfn_t gfn)arch/x86/kvm/mmu.c +8
-0+{ +struct kvm_memory_slot *slot; +int *hugepage_idx; + + gfn =unalias_gfn (kvm, gfn); + slot =gfn_to_memslot_unaliased (kvm, gfn); +if (slot) { + hugepage_idx =slot_hugepage_idx (gfn, slot);
slot_largepage_idx()here?Quoted 2 lines
I don't think we ever write protect large pages, so why is this needed?
Quoted 7 lines
For 2mb pages we need to check if there is a write-protected 4k page in it before we map a 2mb page for writing. If there is any write-protected 4k page in a 2mb area this 2mb page is considered write-protected. These 'write-protected' 2mb pages are accounted in the
account_shadow()function. This information is taken into account when we decide if we can map a guest 1gb page as a 1gb page on the host too.
Quoted 7 lines
account_shadowed()actually increments a hugepagewrite_countby 1 for every 4K page, not 2M page, if I read the code correctly. The code I commented on is right though.The naming is confusing. I suggest has_wrprotected_page_in_{large,huge}page(). although with the a level parameter we can keep
has_wrprotected_page().
Yeah true, the name is a bit confusing. I think a level parameter for has_wrprotected_page() is the best solution.
Joerg
Re: [PATCH 4/7] kvm mmu: implement necessary data structures for second huge page accounting
In reply to Joerg Roedelvirt/kvm/kvm_main.c int __kvm_set_memory_region(struct kvm *kvm, · line 1183
Quoted 12 lines · Joerg Roedel
This patch adds the necessary data structures to take care of write protections in place within a second huge page sized page.
virt/kvm/kvm_main.c +7
-0+#ifdef KVM_PAGES_PER_LHPAGE +if (npages && !new.hpage_info) { +int hugepages = npages /KVM_PAGES_PER_LHPAGE ; +if (npages %KVM_PAGES_PER_LHPAGE ) + hugepages++; +if (base_gfn %KVM_PAGES_PER_LHPAGE ) + hugepages++;
Consider a slot with base_gfn == 1 and npages == 1. This will have hugepages == 2, which is wrong.
I think the right calculation is
((base_gfn + npages - 1) / N) - (base_gfn / N) + 1
i.e. index of last page, plus one so we can store it.
The small huge page calculation is off as well.
Quoted 16 lines
virt/kvm/kvm_main.c +15
-0+ + new.hpage_info =vmalloc (hugepages *sizeof (*new.hpage_info)); + +if (!new.hpage_info) + goto out_free; + +memset (new.hpage_info,0 , hugepages *sizeof (*new.hpage_info)); + +if (base_gfn %KVM_PAGES_PER_LHPAGE ) + new.hpage_info[0 ].write_count =1 ; +if ((base_gfn+npages) %KVM_PAGES_PER_LHPAGE ) + new.hpage_info[hugepages-1].write_count = 1; + } +#endif +
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 4/7] kvm mmu: implement necessary data structures for second huge page accounting
Quoted 16 lines · Avi Kivity
Review
Quoted 12 lines · Joerg Roedel
This patch adds the necessary data structures to take care of write protections in place within a second huge page sized page.
(quoted diff) +7
-0+#ifdef KVM_PAGES_PER_LHPAGE + if (npages && !new.hpage_info) { + int hugepages = npages / KVM_PAGES_PER_LHPAGE; + if (npages % KVM_PAGES_PER_LHPAGE) + hugepages++; + if (base_gfn % KVM_PAGES_PER_LHPAGE) + hugepages++;Consider a slot with
base_gfn== 1 and npages == 1. This will have hugepages == 2, which is wrong.
Quoted 8 lines
I think the right calculation is
((
base_gfn+ npages - 1) / N) - (base_gfn/ N) + 1i.e. index of last page, plus one so we can store it.
The small huge page calculation is off as well.
I fixed the existing case with
commit 1a967084dbe97a2f4be84139d14e2d958d7ffc46
Author: Avi Kivity <[email protected]> Date: Sun Mar 29 16:31:25 2009 +0300
KVM: MMU: Fix off-by-one calculating large page count
The large page initialization code concludes there are two large
pages spanned
by a slot covering 1 (small) page starting at gfn 1. This is
incorrect, and
also results in incorrect write_count initialization in some cases
(base = 1,
npages = 513 for example).
virt/kvm/kvm_main.c +3 -5
@@ -1076,6 +1076,7 @@ int __kvm_set_memory_region(struct kvm *kvm,int r;gfn_t base_gfn;unsigned long npages; +int largepages;unsigned long i;struct kvm_memory_slot *memslot;struct kvm_memory_slot old, new;
@@ -1151,11 +1152,8 @@ int __kvm_set_memory_region(struct kvm *kvm, new.userspace_addr =0 ; }if (npages && !new.lpage_info) { -int largepages = npages /KVM_PAGES_PER_HPAGE ; -if (npages %KVM_PAGES_PER_HPAGE ) - largepages++; -if (base_gfn %KVM_PAGES_PER_HPAGE ) - largepages++; + largepages =1 + (base_gfn + npages -1 ) /KVM_PAGES_PER_HPAGE ; + largepages -= base_gfn / npages; new.lpage_info = vmalloc(largepages *
sizeof(*new.lpage_info));
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 5/7 kvm mmu: add support for 1GB pages to direct mapping paths
In reply to Joerg RoedelThis patch makes the MMU path for TDP aware of 1GB pages.
--- arch/x86/kvm/mmu.c | 56 +++++++++++++++++++++++++++++++++++-------- arch/x86/kvm/paging_tmpl.h | 14 +++++++++++ 2 files changed, 59 insertions(+), 11 deletions(-)
arch/x86/kvm/mmu.c +45 -11
@@ -116,6 +116,11 @@ module_param(oos_shadow, bool, 0644);#define PT64_DIR_BASE_ADDR_MASK \ (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) +#define PT64_MID_BASE_ADDR_MASK (PT64_BASE_ADDR_MASK & \ + ~((1ULL << (PAGE_SHIFT + (2 * PT64_LEVEL_BITS))) - 1)) +#define PT64_MID_GFN_DELTA_MASK (PT64_BASE_ADDR_MASK & (((1ULL << \ + (2 * PT64_LEVEL_BITS)) - 1) << PAGE_SHIFT)) +#define PT32_BASE_ADDR_MASK PAGE_MASK#define PT32_DIR_BASE_ADDR_MASK \ (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
@@ -128,6 +133,7 @@ module_param(oos_shadow, bool, 0644);#define PFERR_USER_MASK (1U << 2)#define PFERR_FETCH_MASK (1U << 4) +#define PT_MIDDLE_LEVEL 3#define PT_DIRECTORY_LEVEL 2#define PT_PAGE_TABLE_LEVEL 1
@@ -507,16 +513,29 @@ static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, enum kvm_page_size psize) {struct kvm_memory_slot *slot; -unsigned long idx; +unsigned long idx, *ret; slot =gfn_to_memslot (kvm, gfn); -if (psize == KVM_PAGE_SIZE_4k) -return &slot->rmap[gfn - slot->base_gfn]; - idx = (gfn /KVM_PAGES_PER_2M_PAGE ) - - (slot->base_gfn /KVM_PAGES_PER_2M_PAGE ); +switch (psize) { +case KVM_PAGE_SIZE_4k: + ret = &slot->rmap[gfn - slot->base_gfn]; +break ; +case KVM_PAGE_SIZE_2M : + idx = (gfn /KVM_PAGES_PER_2M_PAGE ) - + (slot->base_gfn /KVM_PAGES_PER_2M_PAGE ); + ret = &slot->lpage_info[idx].rmap_pde; +break ; +case KVM_PAGE_SIZE_1G : + idx = (gfn /KVM_PAGES_PER_1G_PAGE ) - + (slot->base_gfn /KVM_PAGES_PER_1G_PAGE ); + ret = &slot->hpage_info[idx].rmap_pde; +break ; +default : +BUG (); + } -return &slot->lpage_info[idx].rmap_pde; +return ret; }/*
@@ -1363,7 +1382,10 @@ static void kvm_mmu_page_unlink_children(struct kvm *kvm, &pt[i]); }else { --kvm->stat.lpages; -rmap_remove (kvm, &pt[i],KVM_PAGE_SIZE_2M ); +if (sp->role.level ==PT_DIRECTORY_LEVEL ) +rmap_remove (kvm, &pt[i],KVM_PAGE_SIZE_2M ); +else +rmap_remove (kvm, &pt[i],KVM_PAGE_SIZE_1G ); } } pt[i] = shadow_trap_nonpresent_pte;
@@ -1769,8 +1791,10 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,if ((pte_access &ACC_WRITE_MASK ) || (write_fault && !is_write_protection (vcpu) && !user_fault)) { -if (psize > KVM_PAGE_SIZE_4k && -has_wrprotected_page (vcpu->kvm, gfn)) { +if ((psize ==KVM_PAGE_SIZE_2M && +has_wrprotected_page (vcpu->kvm, gfn)) || + (psize ==KVM_PAGE_SIZE_1G && +has_wrprotected_largepage (vcpu->kvm, gfn))) { ret =1 ; spte = shadow_trap_nonpresent_pte; goto set_pte;
@@ -1884,7 +1908,9 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,for_each_shadow_entry (vcpu, (u64 )gfn <<PAGE_SHIFT , iterator) {if (iterator.level ==PT_PAGE_TABLE_LEVEL || (psize ==KVM_PAGE_SIZE_2M && - iterator.level ==PT_DIRECTORY_LEVEL )) { + iterator.level ==PT_DIRECTORY_LEVEL ) + || (psize ==KVM_PAGE_SIZE_1G && + iterator.level ==PT_MIDDLE_LEVEL )) {mmu_set_spte (vcpu, iterator.sptep,ACC_ALL ,ACC_ALL ,0 , write,1 , &pt_write, psize,0 , gfn, pfn, false);
@@ -1919,8 +1945,14 @@ static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)unsigned long mmu_seq;enum kvm_page_size psize =backing_size (vcpu, gfn); -if (psize ==KVM_PAGE_SIZE_2M ) +if (psize >=KVM_PAGE_SIZE_2M ) { +/* +* nonpaging mode uses pae page tables - so we +* can't use gbpages here - take care of this +*/ gfn &= ~(KVM_PAGES_PER_2M_PAGE -1 ); + psize =KVM_PAGE_SIZE_2M ; + } mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb ();
@@ -2123,6 +2155,8 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, psize =backing_size (vcpu, gfn);if (psize ==KVM_PAGE_SIZE_2M ) gfn &= ~(KVM_PAGES_PER_2M_PAGE -1 ); +else if (psize ==KVM_PAGE_SIZE_1G ) + gfn &= ~(KVM_PAGES_PER_1G_PAGE -1 ); mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); pfn =gfn_to_pfn (vcpu->kvm, gfn);
arch/x86/kvm/paging_tmpl.h +14 -0
@@ -55,6 +55,7 @@#define gpte_to_gfn FNAME(gpte_to_gfn)#define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde) +#define gpte_to_gfn_pmd FNAME(gpte_to_gfn_pmd)/* * The guest_walker structure emulates the behavior of the hardware page
@@ -81,6 +82,11 @@ static gfn_t gpte_to_gfn_pde(pt_element_t gpte)return (gpte &PT_DIR_BASE_ADDR_MASK ) >>PAGE_SHIFT ; } +static gfn_t gpte_to_gfn_pmd (pt_element_t gpte) +{ +return (gpte &PT64_MID_BASE_ADDR_MASK ) >>PAGE_SHIFT ; +} +static bool FNAME (cmpxchg_gpte )(struct kvm *kvm,gfn_t table_gfn,unsigned index,pt_element_t orig_pte,pt_element_t new_pte)
@@ -196,6 +202,14 @@ walk:break ; } +if (walker->level ==PT_MIDDLE_LEVEL && + (pte &PT_PAGE_SIZE_MASK ) && +is_long_mode (vcpu)) { + walker->gfn =gpte_to_gfn_pmd (pte); + walker->gfn += (addr &PT64_MID_GFN_DELTA_MASK ) >>PAGE_SHIFT ; +break ; + } + pt_access = pte_access; --walker->level; }
Signature · 1 line
1.5.6.4
Re: [PATCH 5/7] kvm mmu: add support for 1GB pages to direct mapping paths
Quoted 12 lines · Joerg Roedel
This patch makes the MMU path for TDP aware of 1GB pages.
arch/x86/kvm/mmu.c +5
-0+#define PT64_MID_BASE_ADDR_MASK (PT64_BASE_ADDR_MASK & \ + ~((1ULL << (PAGE_SHIFT + (2 * PT64_LEVEL_BITS))) - 1)) +#define PT64_MID_GFN_DELTA_MASK (PT64_BASE_ADDR_MASK & (((1ULL << \ + (2 * PT64_LEVEL_BITS)) - 1) << PAGE_SHIFT)) +#define PT32_BASE_ADDR_MASK PAGE_MASK#define PT32_DIR_BASE_ADDR_MASK \ (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
arch/x86/kvm/mmu.c module_param(oos_shadow, bool, 0644); · line 137
Quoted 5 lines
arch/x86/kvm/mmu.c +1
-0@@ -128,6 +133,7 @@ module_param(oos_shadow, bool, 0644);#define PFERR_USER_MASK (1U << 2)#define PFERR_FETCH_MASK (1U << 4) +#define PT_MIDDLE_LEVEL 3
I prefer the architectural names to the Linux names (since we're talking about the guest), so PDPT here (even though the Linux names make a bit more sense).
Quoted 24 lines
arch/x86/kvm/mmu.c +9
-5#define PT_DIRECTORY_LEVEL 2#define PT_PAGE_TABLE_LEVEL 1@@ -507,16 +513,29 @@ static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, enum kvm_page_size psize) {struct kvm_memory_slot *slot; -unsigned long idx; +unsigned long idx, *ret; slot =gfn_to_memslot (kvm, gfn); -if (psize == KVM_PAGE_SIZE_4k) -return &slot->rmap[gfn - slot->base_gfn]; - idx = (gfn /KVM_PAGES_PER_2M_PAGE ) - - (slot->base_gfn /KVM_PAGES_PER_2M_PAGE ); +switch (psize) { +case KVM_PAGE_SIZE_4k: + ret = &slot->rmap[gfn - slot->base_gfn]; +break ; +case KVM_PAGE_SIZE_2M : + idx = (gfn /KVM_PAGES_PER_2M_PAGE ) - + (slot->base_gfn /KVM_PAGES_PER_2M_PAGE ); + ret = &slot->lpage_info[idx].rmap_pde;
arch/x86/kvm/mmu.c static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, · line 537
Quoted 10 lines
arch/x86/kvm/mmu.c +9
-0+break ; +case KVM_PAGE_SIZE_1G : + idx = (gfn /KVM_PAGES_PER_1G_PAGE ) - + (slot->base_gfn /KVM_PAGES_PER_1G_PAGE ); + ret = &slot->hpage_info[idx].rmap_pde; +break ; +default : +BUG (); + }
Ah, page_level would really make sense here.
Quoted 7 lines
arch/x86/kvm/mmu.c +1
-1-return &slot->lpage_info[idx].rmap_pde; +return ret; }/*
arch/x86/kvm/mmu.c static void kvm_mmu_page_unlink_children(struct kvm *kvm, · line 1390
Quoted 10 lines
arch/x86/kvm/mmu.c +4
-1@@ -1363,7 +1382,10 @@ static void kvm_mmu_page_unlink_children(struct kvm *kvm, &pt[i]); }else { --kvm->stat.lpages; -rmap_remove (kvm, &pt[i],KVM_PAGE_SIZE_2M ); +if (sp->role.level ==PT_DIRECTORY_LEVEL ) +rmap_remove (kvm, &pt[i],KVM_PAGE_SIZE_2M ); +else +rmap_remove (kvm, &pt[i],KVM_PAGE_SIZE_1G ); }
And here.
Quoted 4 lines
(quoted diff) +0
-0}arch/x86/kvm/mmu.c +0
-0pt[i] = shadow_trap_nonpresent_pte;@@ -1769,8 +1791,10 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, if ((pte_access & ACC_WRITE_MASK)
arch/x86/kvm/mmu.c static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, · line 1799
Quoted 10 lines
arch/x86/kvm/mmu.c +4
-2|| (write_fault && !is_write_protection(vcpu) && !user_fault)) { -if (psize > KVM_PAGE_SIZE_4k && -has_wrprotected_page (vcpu->kvm, gfn)) { +if ((psize ==KVM_PAGE_SIZE_2M && +has_wrprotected_page (vcpu->kvm, gfn)) || + (psize ==KVM_PAGE_SIZE_1G && +has_wrprotected_largepage (vcpu->kvm, gfn))) { ret =1 ;
And here. I'm in complete agreement with myself here.
Quoted 43 lines
arch/x86/kvm/mmu.c +0
-0spte = shadow_trap_nonpresent_pte;arch/x86/kvm/mmu.c +12
-2goto set_pte;@@ -1884,7 +1908,9 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,for_each_shadow_entry (vcpu, (u64 )gfn <<PAGE_SHIFT , iterator) {if (iterator.level ==PT_PAGE_TABLE_LEVEL || (psize ==KVM_PAGE_SIZE_2M && - iterator.level ==PT_DIRECTORY_LEVEL )) { + iterator.level ==PT_DIRECTORY_LEVEL ) + || (psize ==KVM_PAGE_SIZE_1G && + iterator.level ==PT_MIDDLE_LEVEL )) {mmu_set_spte (vcpu, iterator.sptep,ACC_ALL ,ACC_ALL ,0 , write,1 , &pt_write, psize,0 , gfn, pfn, false);@@ -1919,8 +1945,14 @@ static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)unsigned long mmu_seq;enum kvm_page_size psize =backing_size (vcpu, gfn); -if (psize ==KVM_PAGE_SIZE_2M ) +if (psize >=KVM_PAGE_SIZE_2M ) { +/* +* nonpaging mode uses pae page tables - so we +* can't use gbpages here - take care of this +*/ gfn &= ~(KVM_PAGES_PER_2M_PAGE -1 ); + psize =KVM_PAGE_SIZE_2M ; + } mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb ();@@ -2123,6 +2155,8 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, psize =backing_size (vcpu, gfn);if (psize ==KVM_PAGE_SIZE_2M ) gfn &= ~(KVM_PAGES_PER_2M_PAGE -1 ); +else if (psize ==KVM_PAGE_SIZE_1G ) + gfn &= ~(KVM_PAGES_PER_1G_PAGE -1 ); mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); pfn =gfn_to_pfn (vcpu->kvm, gfn);arch/x86/kvm/paging_tmpl.h +0
-0
arch/x86/kvm/paging_tmpl.h line 59
Quoted 5 lines
arch/x86/kvm/paging_tmpl.h +1
-0@@ -55,6 +55,7 @@#define gpte_to_gfn FNAME(gpte_to_gfn)#define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde) +#define gpte_to_gfn_pmd FNAME(gpte_to_gfn_pmd)
gpte_to_gfn(gpte, level)?
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 6/7 kvm mmu: enabling 1GB pages by extending backing_size funtion
In reply to Joerg RoedelThis patch enables support for 1GB pages in KVM by implementing the support in backing_size().
--- arch/x86/kvm/mmu.c | 30 ++++++++++++++++++++++-------- 1 files changed, 22 insertions(+), 8 deletions(-)
arch/x86/kvm/mmu.c +22 -8
@@ -479,7 +479,9 @@ static enum kvm_page_size host_page_size(struct kvm *kvm, gfn_t gfn) vma =find_vma (current->mm, addr);if (vma) { size =vma_kernel_pagesize (vma); -if (size >=KVM_PAGE_SIZE_2M ) +if (size ==KVM_PAGE_SIZE_1G ) + ret =KVM_PAGE_SIZE_1G ; +else if (size >=KVM_PAGE_SIZE_2M ) ret =KVM_PAGE_SIZE_2M ; }up_read (¤t->mm->mmap_sem);
@@ -490,18 +492,30 @@ static enum kvm_page_size host_page_size(struct kvm *kvm, gfn_t gfn)static enum kvm_page_size backing_size (struct kvm_vcpu *vcpu,gfn_t gfn) {struct kvm_memory_slot *slot; - -if (has_wrprotected_page (vcpu->kvm, gfn)) -return KVM_PAGE_SIZE_4k; - -if (host_page_size (vcpu->kvm, gfn) <KVM_PAGE_SIZE_2M ) -return KVM_PAGE_SIZE_4k; +enum kvm_page_size host_size, ret; slot =gfn_to_memslot (vcpu->kvm, gfn);if (slot && slot->dirty_bitmap)return KVM_PAGE_SIZE_4k; -return KVM_PAGE_SIZE_2M ; + host_size =host_page_size (vcpu->kvm, gfn); + +switch (host_size) { +case KVM_PAGE_SIZE_1G : +if (!has_wrprotected_largepage (vcpu->kvm, gfn)) { + ret =KVM_PAGE_SIZE_1G ; +break ; + } +case KVM_PAGE_SIZE_2M : +if (!has_wrprotected_page (vcpu->kvm, gfn)) { + ret =KVM_PAGE_SIZE_2M ; +break ; + } +default : + ret = KVM_PAGE_SIZE_4k; + } + +return ret; }/*
Signature · 1 line
1.5.6.4
Re: [PATCH 6/7] kvm mmu: enabling 1GB pages by extending backing_size funtion
Quoted 19 lines · Joerg Roedel
This patch enables support for 1GB pages in KVM by implementing the support in
backing_size().arch/x86/kvm/mmu.c +1
-6@@ -490,18 +492,30 @@ static enum kvm_page_size host_page_size(struct kvm *kvm, gfn_t gfn)static enum kvm_page_size backing_size (struct kvm_vcpu *vcpu,gfn_t gfn) {struct kvm_memory_slot *slot; - -if (has_wrprotected_page (vcpu->kvm, gfn)) -return KVM_PAGE_SIZE_4k; - -if (host_page_size (vcpu->kvm, gfn) <KVM_PAGE_SIZE_2M ) -return KVM_PAGE_SIZE_4k; +enum kvm_page_size host_size, ret; slot =gfn_to_memslot (vcpu->kvm, gfn);if (slot && slot->dirty_bitmap)return KVM_PAGE_SIZE_4k;
arch/x86/kvm/mmu.c static enum kvm_page_size host_page_size(struct kvm *kvm, gfn_t gfn) · line 509
Quoted 10 lines
arch/x86/kvm/mmu.c +8
-1-return KVM_PAGE_SIZE_2M ; + host_size =host_page_size (vcpu->kvm, gfn); + +switch (host_size) { +case KVM_PAGE_SIZE_1G : +if (!has_wrprotected_largepage (vcpu->kvm, gfn)) { + ret =KVM_PAGE_SIZE_1G ; +break ; + }
What if there's a wrprotected_page in there?
Quoted 14 lines
arch/x86/kvm/mmu.c +10
-0+case KVM_PAGE_SIZE_2M : +if (!has_wrprotected_page (vcpu->kvm, gfn)) { + ret =KVM_PAGE_SIZE_2M ; +break ; + } +default : + ret = KVM_PAGE_SIZE_4k; + } + +return ret; }/*
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH 7/7 kvm x86: report 1GB page support to userspace
In reply to Joerg RoedelIf userspace knows that the kernel part supports 1GB pages it can enable the corresponding cpuid bit so that guests actually use GB pages.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm.c | 7 +++++++ arch/x86/kvm/vmx.c | 7 +++++++ arch/x86/kvm/x86.c | 6 +++++- include/linux/kvm.h | 1 + 5 files changed, 22 insertions(+), 1 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -542,6 +542,8 @@ struct kvm_x86_ops {int (*set_tss_addr)(struct kvm *kvm,unsigned int addr);int (*get_tdp_level)(void );int (*get_mt_mask_shift)(void ); + +bool (*gb_page_enable)(void ); };extern struct kvm_x86_ops *kvm_x86_ops;
arch/x86/kvm/svm.c +7 -0
@@ -2604,6 +2604,11 @@ static int svm_get_mt_mask_shift(void)return 0 ; } +static bool svm_gb_page_enable (void ) +{ +return npt_enabled; +} +static struct kvm_x86_ops svm_x86_ops = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -2661,6 +2666,8 @@ static struct kvm_x86_ops svm_x86_ops = { .set_tss_addr = svm_set_tss_addr, .get_tdp_level = get_npt_level, .get_mt_mask_shift = svm_get_mt_mask_shift, + + .gb_page_enable = svm_gb_page_enable, };static int __init svm_init (void )
arch/x86/kvm/vmx.c +7 -0
@@ -3647,6 +3647,11 @@ static int vmx_get_mt_mask_shift(void)return VMX_EPT_MT_EPTE_SHIFT ; } +static bool vmx_gb_page_enable (void ) +{ +return false; +} +static struct kvm_x86_ops vmx_x86_ops = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios,
@@ -3702,6 +3707,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .set_tss_addr = vmx_set_tss_addr, .get_tdp_level = get_ept_level, .get_mt_mask_shift = vmx_get_mt_mask_shift, + + .gb_page_enable = vmx_gb_page_enable, };static int __init vmx_init (void )
arch/x86/kvm/x86.c +5 -1
@@ -1009,7 +1009,7 @@ out:int kvm_dev_ioctl_check_extension (long ext) { -int r; +int r =0 ;switch (ext) {case KVM_CAP_IRQCHIP :
@@ -1027,6 +1027,10 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_ASSIGN_DEV_IRQ : r =1 ;break ; +case KVM_CAP_1GB_PAGES : +if (kvm_x86_ops->gb_page_enable ()) + r =1 ; +break ;case KVM_CAP_COALESCED_MMIO : r =KVM_COALESCED_MMIO_PAGE_OFFSET ;break ;
include/linux/kvm.h +1 -0
@@ -413,6 +413,7 @@ struct kvm_trace_rec {#define KVM_CAP_DEVICE_MSIX 28#endif #define KVM_CAP_ASSIGN_DEV_IRQ 29 +#define KVM_CAP_1GB_PAGES 30#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
1.5.6.4
Re: [PATCH 7/7] kvm x86: report 1GB page support to userspace
Quoted 9 lines · Joerg Roedel
If userspace knows that the kernel part supports 1GB pages it can enable the corresponding cpuid bit so that guests actually use GB pages.
arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 548
Quoted 7 lines
arch/x86/include/asm/kvm_host.h +2
-0@@ -542,6 +542,8 @@ struct kvm_x86_ops {int (*set_tss_addr)(struct kvm *kvm,unsigned int addr);int (*get_tdp_level)(void );int (*get_mt_mask_shift)(void ); + +bool (*gb_page_enable)(void ); };
Should enable unconditionally. Of course we need to find the shadow bug first, may be the has_wrprotected thingy.
include/linux/kvm.h struct kvm_trace_rec { · line 419
Quoted 13 lines
include/linux/kvm.h +1
-0@@ -413,6 +413,7 @@ struct kvm_trace_rec {#define KVM_CAP_DEVICE_MSIX 28#endif #define KVM_CAP_ASSIGN_DEV_IRQ 29 +#define KVM_CAP_1GB_PAGES 30#ifdef KVM_CAP_IRQ_ROUTING
Need KVM_GET_SUPPORTED_CPUID2 support as well.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 7/7] kvm x86: report 1GB page support to userspace
Quoted 5 lines · Avi Kivity
Quoted 3 lines · Joerg Roedel
If userspace knows that the kernel part supports 1GB pages it can enable the corresponding cpuid bit so that guests actually use GB pages.
Quoted 15 lines
arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 548
Quoted 12 lines
arch/x86/include/asm/kvm_host.h +2
-0@@ -542,6 +542,8 @@ struct kvm_x86_ops {int (*set_tss_addr)(struct kvm *kvm,unsigned int addr);int (*get_tdp_level)(void );int (*get_mt_mask_shift)(void ); + +bool (*gb_page_enable)(void ); };Should enable unconditionally. Of course we need to find the shadow bug first, may be the
has_wrprotectedthingy.
This was the original plan. But how about VMX with EPT enabled? I am not sure but I think this configuration will not support gbpages?
Joerg
Re: [PATCH 7/7] kvm x86: report 1GB page support to userspace
Quoted 11 lines · Joerg Roedel
Quoted 10 lines
arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 547
Quoted 8 lines
arch/x86/include/asm/kvm_host.h +2
-0int (*set_tss_addr)(struct kvm *kvm,unsigned int addr);int (*get_tdp_level)(void );int (*get_mt_mask_shift)(void ); + +bool (*gb_page_enable)(void ); };Should enable unconditionally. Of course we need to find the shadow bug first, may be the
has_wrprotectedthingy.
Quoted 4 lines
This was the original plan. But how about VMX with EPT enabled? I am not sure but I think this configuration will not support gbpages?
You're right. Let's have a ->max_host_page_level() to handle that. It's 0.5T pages ready, too.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 7/7] kvm x86: report 1GB page support to userspace
Quoted 12 lines · Avi Kivity
Quoted 11 lines · Joerg Roedel
Quoted 10 lines
arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 547
Quoted 7 lines
arch/x86/include/asm/kvm_host.h +2
-0int (*set_tss_addr)(struct kvm *kvm,unsigned int addr);int (*get_tdp_level)(void );int (*get_mt_mask_shift)(void ); + +bool (*gb_page_enable)(void ); };Should enable unconditionally. Of course we need to find the shadow bug first, may be the
has_wrprotectedthingy.
Quoted 7 lines
Quoted 4 lines
This was the original plan. But how about VMX with EPT enabled? I am not sure but I think this configuration will not support gbpages?
You're right. Let's have a ->max_host_page_level() to handle that. It's 0.5T pages ready, too.
Ok I will change that together with the page_size -> page_level chhanges. But I doubt that there will ever be 0.5T pages ;)
Joerg
Re: [PATCH 7/7] kvm x86: report 1GB page support to userspace
Quoted 3 lines · Joerg Roedel
Ok I will change that together with the
page_size->page_levelchhanges. But I doubt that there will ever be 0.5T pages ;)
We're bloating at a rate of 1 bit per 1-2 years, so we have 8-16 years to prepare.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 8 lines · Joerg Roedel
Hi,
this patchset extends the KVM MMU implementation to support 1GB pages as supported by AMD family 16 processors. These patches enable support for 1 GB pages with Nested Paging. Support for these pages in the shadow paging code was also developed but does not run stable yet. The patch for shadow-paging support is not included in this series and will be sent out seperatly.
Looks generally sane. I'm not sure its even worthwhile to support GBpages with softmmu, because the chance of finding an area without shadowed (write protected) pages is much smaller than with 2MB pages.
Have any numbers to share?
Quoted 10 lines · Marcelo Tosatti
Quoted 8 lines · Joerg Roedel
Hi,
this patchset extends the KVM MMU implementation to support 1GB pages as supported by AMD family 16 processors. These patches enable support for 1 GB pages with Nested Paging. Support for these pages in the shadow paging code was also developed but does not run stable yet. The patch for shadow-paging support is not included in this series and will be sent out seperatly.
Quoted 3 lines
Looks generally sane. I'm not sure its even worthwhile to support GBpages with softmmu, because the chance of finding an area without shadowed (write protected) pages is much smaller than with 2MB pages.
Thanks for your review.
The idea behind GB pages in softmmu code was to provide GB pages to the guest even if hardware does not support it. This would work better with live migration (Only case where we wouldn't have gbpages then would be vmx with ept enabled).
Quoted 1 line
Have any numbers to share?
No numbers I fully trust by now. I measured a 32% improvement in kernbench using nested pages backed with gb pages. I will do some more measurements and share some more solid numbers.
Joerg
Quoted 26 lines · Joerg Roedel
Quoted 11 lines · Marcelo Tosatti
Quoted 9 lines · Joerg Roedel
Hi,
this patchset extends the KVM MMU implementation to support 1GB pages as supported by AMD family 16 processors. These patches enable support for 1 GB pages with Nested Paging. Support for these pages in the shadow paging code was also developed but does not run stable yet. The patch for shadow-paging support is not included in this series and will be sent out seperatly.
Quoted 4 lines
Looks generally sane. I'm not sure its even worthwhile to support GBpages with softmmu, because the chance of finding an area without shadowed (write protected) pages is much smaller than with 2MB pages.
Thanks for your review.
The idea behind GB pages in softmmu code was to provide GB pages to the guest even if hardware does not support it. This would work better with live migration (Only case where we wouldn't have gbpages then would be vmx with ept enabled).
Quoted 8 lines
Quoted 2 lines
Have any numbers to share?
No numbers I fully trust by now. I measured a 32% improvement in kernbench using nested pages backed with gb pages. I will do some more measurements and share some more solid numbers.
Compared to 2M pages? But we're already close to native here.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 30 lines · Avi Kivity
Quoted 29 lines · Joerg Roedel
Quoted 11 lines · Marcelo Tosatti
Quoted 9 lines · Joerg Roedel
Hi,
this patchset extends the KVM MMU implementation to support 1GB pages as supported by AMD family 16 processors. These patches enable support for 1 GB pages with Nested Paging. Support for these pages in the shadow paging code was also developed but does not run stable yet. The patch for shadow-paging support is not included in this series and will be sent out seperatly.
Quoted 4 lines
Looks generally sane. I'm not sure its even worthwhile to support GBpages with softmmu, because the chance of finding an area without shadowed (write protected) pages is much smaller than with 2MB pages.
Thanks for your review.
The idea behind GB pages in softmmu code was to provide GB pages to the guest even if hardware does not support it. This would work better with live migration (Only case where we wouldn't have gbpages then would be vmx with ept enabled).
Quoted 2 lines
Have any numbers to share?
Quoted 7 lines
Quoted 5 lines
No numbers I fully trust by now. I measured a 32% improvement in kernbench using nested pages backed with gb pages. I will do some more measurements and share some more solid numbers.
Compared to 2M pages? But we're already close to native here.
Yes, thats why I don't trust those numbers. I'll find out what went wrong and provide more solid numbers.
Joerg
Quoted 12 lines · Marcelo Tosatti
Quoted 9 lines · Joerg Roedel
Hi,
this patchset extends the KVM MMU implementation to support 1GB pages as supported by AMD family 16 processors. These patches enable support for 1 GB pages with Nested Paging. Support for these pages in the shadow paging code was also developed but does not run stable yet. The patch for shadow-paging support is not included in this series and will be sent out seperatly.
Quoted 4 lines
Looks generally sane. I'm not sure its even worthwhile to support GBpages with softmmu, because the chance of finding an area without shadowed (write protected) pages is much smaller than with 2MB pages.
If the guest uses 1GB pages in userspace, then these pages would not have any write protected subpages.
Signature · 1 line
error compiling committee.c: too many arguments to function