KVM MMU: fix objects free
PATCH 1/5 KVM MMU: fix objects free
Where to alloc, where to free
Signed-off-by Xiao Guangrong <[email protected]>
--- arch/x86/kvm/mmu.c | 18 ++++++++++-------- 1 files changed, 10 insertions(+), 8 deletions(-)
arch/x86/kvm/mmu.c +10 -8
@@ -304,10 +304,11 @@ static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,return 0 ; } -static void mmu_free_memory_cache (struct kvm_mmu_memory_cache *mc) +static void mmu_free_memory_cache (struct kvm_mmu_memory_cache *mc, +struct kmem_cache *cache) {while (mc->nobjs) -kfree (mc->objects[--mc->nobjs]); +kmem_cache_free (cache, mc->objects[--mc->nobjs]); } static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
@@ -355,10 +356,11 @@ out:static void mmu_free_memory_caches (struct kvm_vcpu *vcpu) { -mmu_free_memory_cache (&vcpu->arch.mmu_pte_chain_cache); -mmu_free_memory_cache (&vcpu->arch.mmu_rmap_desc_cache); +mmu_free_memory_cache (&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache); +mmu_free_memory_cache (&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);mmu_free_memory_cache_page (&vcpu->arch.mmu_page_cache); -mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache); +mmu_free_memory_cache (&vcpu->arch.mmu_page_header_cache, + mmu_page_header_cache); } static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
@@ -379,7 +381,7 @@ static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)static void mmu_free_pte_chain (struct kvm_pte_chain *pc) { -kfree (pc); +kmem_cache_free (pte_chain_cache, pc); }static struct kvm_rmap_desc *mmu_alloc_rmap_desc (struct kvm_vcpu *vcpu)
@@ -390,7 +392,7 @@ static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)static void mmu_free_rmap_desc (struct kvm_rmap_desc *rd) { -kfree (rd); +kmem_cache_free (rmap_desc_cache, rd); }/*
@@ -897,7 +899,7 @@ static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)list_del (&sp->link);__free_page (virt_to_page (sp->spt));__free_page (virt_to_page (sp->gfns)); -kfree (sp); +kmem_cache_free (mmu_page_header_cache, sp); ++kvm->arch.n_free_mmu_pages; }
Signature · 1 line
1.6.1.2
PATCH 2/5 KVM MMU: remove rmap before clear spte
Remove rmap before clear spte otherwise it will trigger BUG_ON() in some functions such as rmap_write_protect()
Signed-off-by Xiao Guangrong <[email protected]>
--- arch/x86/kvm/mmu.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/kvm/mmu.c +1 -0
@@ -1812,6 +1812,7 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,if (level >PT_PAGE_TABLE_LEVEL &&has_wrprotected_page (vcpu->kvm, gfn, level)) { ret =1 ; +rmap_remove (vcpu->kvm, sptep); spte = shadow_trap_nonpresent_pte; goto set_pte; }
Signature · 1 line
1.6.1.2
PATCH 3/5 KVM MMU: fix for got gfn when sync shadow pages
sp->gfns[] are not mapping gfn since it has cooked by unalias_gfn()
Signed-off-by Xiao Guangrong <[email protected]>
--- arch/x86/kvm/paging_tmpl.h | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
arch/x86/kvm/paging_tmpl.h +4 -3
@@ -588,7 +588,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)unsigned pte_access;pt_element_t gpte;gpa_t pte_gpa; -gfn_t gfn = sp->gfns[i]; +gfn_t gfn;if (!is_shadow_present_pte (sp->spt[i]))continue ;
@@ -599,8 +599,9 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) sizeof(pt_element_t)))return -EINVAL ; -if (gpte_to_gfn (gpte) != gfn || !is_present_gpte (gpte) || - !(gpte &PT_ACCESSED_MASK )) { + gfn =gpte_to_gfn (gpte); +if (unalias_gfn (vcpu->kvm, gfn) != sp->gfns[i] || + !is_present_gpte (gpte) || !(gpte &PT_ACCESSED_MASK )) {u64 nonpresent;rmap_remove (vcpu->kvm, &sp->spt[i]);
Signature · 1 line
1.6.1.2
PATCH 4/5 KVM MMU: fix two typos
fix two typos in next branch
Signed-off-by Xiao Guangrong <[email protected]>
--- arch/x86/kvm/mmu.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu.c +2 -2
@@ -2070,7 +2070,7 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu) root_gfn =0 ; }spin_lock (&vcpu->kvm->mmu_lock); -kvm_mmu_free_some_pages (vcpu->kvm); +kvm_mmu_free_some_pages (vcpu); sp =kvm_mmu_get_page (vcpu, root_gfn,0 ,PT64_ROOT_LEVEL , direct,ACC_ALL ,NULL );
@@ -2101,7 +2101,7 @@ static int mmu_alloc_roots(struct kvm_vcpu *vcpu) root_gfn = i <<30 ; }spin_lock (&vcpu->kvm->mmu_lock); -kvm_mmu_free_some_pages (vcpu->kvm); +kvm_mmu_free_some_pages (vcpu); sp =kvm_mmu_get_page (vcpu, root_gfn, i <<30 ,PT32_ROOT_LEVEL , direct,ACC_ALL ,NULL );
Signature · 1 line
1.6.1.2
PATCH 5/5 KVM x86: cleanup unused local variable
fix:
arch/x86/kvm/x86.c: In function ‘handle_emulation_failure’:
arch/x86/kvm/x86.c:3844: warning: unused variable ‘ctxt’
Signed-off-by Xiao Guangrong <[email protected]>
--- arch/x86/kvm/x86.c | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -2
@@ -3841,8 +3841,6 @@ static void inject_emulated_exception(struct kvm_vcpu *vcpu)static int handle_emulation_failure (struct kvm_vcpu *vcpu) { -struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; - ++vcpu->stat.insn_emulation_fail;trace_kvm_emulate_insn_failed (vcpu); vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ;
Signature · 1 line
1.6.1.2
Marcelo Tosatti
AppliedQuoted 6 lines · Xiao Guangrong
Where to alloc, where to free
Signed-off-by Xiao Guangrong <[email protected]>--- arch/x86/kvm/mmu.c | 18 ++++++++++-------- 1 files changed, 10 insertions(+), 8 deletions(-)
Applied all (4 was already fixed), thanks.