KVM: x86: optimize for guest page written
cover KVM: x86: optimize for guest page written
Too keep shadow page consistency, we should write-protect the guest page if if it is a page structure. Unfortunately, even if the guest page structure is tear-down and is used for other usage, we still write-protect it and cause page fault if it is written, in this case, we need to zap the corresponding shadow page and let the guest page became normal as possible, that is just what kvm_mmu_pte_write does, however, sometimes, it does not work well:
- kvm_mmu_pte_write is unsafe since we need to alloc pte_list_desc in the function when spte is prefetched, unfortunately, we can not know how many spte need to be prefetched on this path, that means we can use out of the free pte_list_desc object in the cache, and BUG_ON() is triggered, also some path does not fill the cache, such as INS instruction emulated that does not trigger page fault.
- we usually use repeat string instructions to clear the page, for example, we call memset to clear a page table, 'stosb' is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible.
- Sometimes, we only modify the last one byte of a pte to update status bit, for example, clear_bit is used to clear r/w bit in linux kernel and 'andb' instruction is used in this function, in this case, kvm_mmu_pte_write will treat it as misaligned access, and the shadow page table is zapped.
- detecting write-flooding does not work well, when we handle page written, if the last speculative spte is not accessed, we treat the page is write-flooding, however, we can speculative spte on many path, such as pte prefetch, page synced, that means the last speculative spte may be not point to the written page and the written page can be accessed via other sptes, so depends on the Accessed bit of the last speculative spte is not enough.
In this patchset, we fixed/avoided these issues:
- instead of filling the cache in page fault path, we do it in kvm_mmu_pte_write, and do not prefetch the spte if it dose not have free pte_list_desc object in the cache.
- if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest then, the mapping can became writable and directly write the page
- do not zap the shadow page if it only modify the last byte of pte.
- Instead of detected page accessed, we can detect whether the spte is accessed or not, if the spte is not accessed but it is written frequently, we treat is not a page table or it not used for a long time.
Performance test result:
the performance is obvious improved tested by kernebench:
Before patchset After patchset
3m0.094s 2m50.177s 3m1.813s 2m52.774s 3m6.239 2m51.512
PATCH 1/11 KVM: MMU: avoid pte_list_desc run out in kvm_mmu_pte_write
kvm_mmu_pte_write is unsafe since we need to alloc pte_list_desc in the function when spte is prefetched, unfortunately, we can not know how many spte need to be prefetched on this path, that means we can use out of the free pte_list_desc object in the cache, and BUG_ON() is triggered, also some path does not fill the cache, such as INS instruction emulated that does not trigger page fault
--- arch/x86/kvm/mmu.c | 20 +++++++++++++++----- 1 files changed, 15 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu.c +15 -5
@@ -593,6 +593,11 @@ static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,return 0 ; } +static int mmu_memory_cache_free_object (struct kvm_mmu_memory_cache *cache) +{ +return cache->nobjs; +} +static void mmu_free_memory_cache (struct kvm_mmu_memory_cache *mc,struct kmem_cache *cache) {
@@ -3526,6 +3531,14 @@ static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)set_bit (PT_ACCESSED_SHIFT , (unsigned long *)spte); } +static int get_free_pte_list_desc_nr (struct kvm_vcpu *vcpu) +{ +struct kvm_mmu_memory_cache *cache; + + cache = &vcpu->arch.mmu_pte_list_desc_cache; +return mmu_memory_cache_free_object (cache); +} +void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new,int bytes,bool guest_initiated)
@@ -3583,6 +3596,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,break ; } +mmu_topup_memory_caches (vcpu);spin_lock (&vcpu->kvm->mmu_lock);if (atomic_read (&vcpu->kvm->arch.invlpg_counter) != invlpg_counter) gentry =0 ;
@@ -3653,7 +3667,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,mmu_page_zap_pte (vcpu->kvm, sp, spte);if (gentry && !((sp->role.word ^ vcpu->arch.mmu.base_role.word) - & mask.word)) + & mask.word) &&get_free_pte_list_desc_nr (vcpu))mmu_pte_write_new_pte (vcpu, sp, spte, &gentry);if (!remote_flush &&need_remote_flush (entry, *spte)) remote_flush = true;
@@ -3714,10 +3728,6 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code, goto out; } - r =mmu_topup_memory_caches (vcpu); -if (r) - goto out; - er =x86_emulate_instruction (vcpu, cr2,0 , insn, insn_len);switch (er) {
Signature · 1 line
1.7.5.4
Re: [PATCH 01/11] KVM: MMU: avoid pte_list_desc run out in kvm_mmu_pte_write
Quoted 12 lines · Xiao Guangrong
kvm_mmu_pte_writeis unsafe since we need to allocpte_list_descin the function when spte is prefetched, unfortunately, we can not know how many spte need to be prefetched on this path, that means we can use out of the freepte_list_descobject in the cache, andBUG_ON()is triggered, also some path does not fill the cache, such as INS instruction emulated that does not trigger page faultvoid kvm_mmu_pte_write(struct
kvm_vcpu*vcpu,gpa_tgpa,
const u8 *new, int bytes, boolguest_initiated)
arch/x86/kvm/mmu.c void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, · line 3599
Quoted 4 lines
arch/x86/kvm/mmu.c +1
-0@@ -3583,6 +3596,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,break ; } +mmu_topup_memory_caches (vcpu);
Please add a comment here describing why it's okay to ignore the error return.
arch/x86/kvm/mmu.c void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, · line 3671
Quoted 10 lines
arch/x86/kvm/mmu.c +1
-1spin_lock (&vcpu->kvm->mmu_lock);if (atomic_read (&vcpu->kvm->arch.invlpg_counter) != invlpg_counter) gentry =0 ;@@ -3653,7 +3667,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,mmu_page_zap_pte (vcpu->kvm, sp, spte);if (gentry&& !((sp->role.word ^ vcpu->arch.mmu.base_role.word) - & mask.word)) + & mask.word)&&get_free_pte_list_desc_nr (vcpu))mmu_pte_write_new_pte (vcpu, sp, spte,&gentry);
Wow, this bug was here since 2.6.23. Good catch.
Please wrap or rename get_free_pte_list_desc_nr() in rmap_can_add(vcpu) so it's clearer why we're doing this.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 01/11] KVM: MMU: avoid pte_list_desc run out in kvm_mmu_pte_write
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Xiao Guangrong
kvm_mmu_pte_writeis unsafe since we need to allocpte_list_descin the function when spte is prefetched, unfortunately, we can not know how many spte need to be prefetched on this path, that means we can use out of the freepte_list_descobject in the cache, andBUG_ON()is triggered, also some path does not fill the cache, such as INS instruction emulated that does not trigger page faultvoid kvm_mmu_pte_write(struct
kvm_vcpu*vcpu,gpa_tgpa,
const u8 *new, int bytes,
boolguest_initiated)(quoted diff) +0
-0@@ -3583,6 +3596,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, break; }
Quoted 4 lines
arch/x86/kvm/mmu.c void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, · line 3599
Quoted 1 line
arch/x86/kvm/mmu.c +1
-0+mmu_topup_memory_caches (vcpu);Please add a comment here describing why it's okay to ignore the error return.
OK
Quoted 13 lines
arch/x86/kvm/mmu.c void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, · line 3671
Quoted 10 lines
arch/x86/kvm/mmu.c +1
-1spin_lock (&vcpu->kvm->mmu_lock);if (atomic_read (&vcpu->kvm->arch.invlpg_counter) != invlpg_counter) gentry =0 ;@@ -3653,7 +3667,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,mmu_page_zap_pte (vcpu->kvm, sp, spte);if (gentry&& !((sp->role.word ^ vcpu->arch.mmu.base_role.word) - & mask.word)) + & mask.word)&&get_free_pte_list_desc_nr (vcpu))mmu_pte_write_new_pte (vcpu, sp, spte,&gentry);Wow, this bug was here since 2.6.23. Good catch.
Quoted 2 lines
Please wrap or rename
get_free_pte_list_desc_nr()inrmap_can_add(vcpu)so it's clearer why we're doing this.
OK, will do, thanks!
PATCH 2/11 KVM: x86: cleanup pio/pout emulated
In reply to Xiao GuangrongRemove the same code between emulator_pio_in_emulated and emulator_pio_out_emulated
--- arch/x86/kvm/x86.c | 59 ++++++++++++++++++++++----------------------------- 1 files changed, 26 insertions(+), 33 deletions(-)
arch/x86/kvm/x86.c +26 -33
@@ -4320,32 +4320,24 @@ static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)return r; } - -static int emulator_pio_in_emulated (struct x86_emulate_ctxt *ctxt, -int size,unsigned short port,void *val, -unsigned int count) +static int emulator_pio_in_out (struct kvm_vcpu *vcpu,int size, +unsigned short port,void *val, +unsigned int count,bool in) { -struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); - -if (vcpu->arch.pio.count) - goto data_avail; - -trace_kvm_pio (0 , port, size, count); +trace_kvm_pio (!in, port, size, count); vcpu->arch.pio.port = port; - vcpu->arch.pio.in =1 ; + vcpu->arch.pio.in = in; vcpu->arch.pio.count = count; vcpu->arch.pio.size = size;if (!kernel_pio (vcpu, vcpu->arch.pio_data)) { - data_avail: -memcpy (val, vcpu->arch.pio_data, size * count); vcpu->arch.pio.count =0 ;return 1 ; } vcpu->run->exit_reason =KVM_EXIT_IO ; - vcpu->run->io.direction =KVM_EXIT_IO_IN ; + vcpu->run->io.direction = in ?KVM_EXIT_IO_IN :KVM_EXIT_IO_OUT ; vcpu->run->io.size = size; vcpu->run->io.data_offset =KVM_PIO_PAGE_OFFSET *PAGE_SIZE ; vcpu->run->io.count = count;
@@ -4354,36 +4346,37 @@ static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,return 0 ; } -static int emulator_pio_out_emulated (struct x86_emulate_ctxt *ctxt, -int size,unsigned short port, -const void *val,unsigned int count) +static int emulator_pio_in_emulated (struct x86_emulate_ctxt *ctxt, +int size,unsigned short port,void *val, +unsigned int count) {struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); +int ret; -trace_kvm_pio (1 , port, size, count); - - vcpu->arch.pio.port = port; - vcpu->arch.pio.in =0 ; - vcpu->arch.pio.count = count; - vcpu->arch.pio.size = size; - -memcpy (vcpu->arch.pio_data, val, size * count); +if (vcpu->arch.pio.count) + goto data_avail; -if (!kernel_pio (vcpu, vcpu->arch.pio_data)) { + ret =emulator_pio_in_out (vcpu, size, port, val, count, true); +if (ret) { +data_avail: +memcpy (val, vcpu->arch.pio_data, size * count); vcpu->arch.pio.count =0 ;return 1 ; } - vcpu->run->exit_reason =KVM_EXIT_IO ; - vcpu->run->io.direction =KVM_EXIT_IO_OUT ; - vcpu->run->io.size = size; - vcpu->run->io.data_offset =KVM_PIO_PAGE_OFFSET *PAGE_SIZE ; - vcpu->run->io.count = count; - vcpu->run->io.port = port; -return 0 ; } +static int emulator_pio_out_emulated (struct x86_emulate_ctxt *ctxt, +int size,unsigned short port, +const void *val,unsigned int count) +{ +struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); + +memcpy (vcpu->arch.pio_data, val, size * count); +return emulator_pio_in_out (vcpu, size, port, (void *)val, count, false); +} +static unsigned long get_segment_base (struct kvm_vcpu *vcpu,int seg) {return kvm_x86_ops->get_segment_base (vcpu, seg);
Signature · 1 line
1.7.5.4
PATCH 3/11 KVM: x86: fast emulate repeat string write instructions
In reply to Xiao GuangrongWe usually use repeat string instructions to clear the page, for example, we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/emulate.c | 5 +++-- arch/x86/kvm/mmu.c | 4 ++-- arch/x86/kvm/paging_tmpl.h | 3 ++- arch/x86/kvm/x86.c | 9 +++++++-- 6 files changed, 16 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -244,6 +244,7 @@ struct x86_emulate_ctxt {bool guest_mode;/* guest running a nested guest */ bool perm_ok;/* do not check permissions if true */ bool only_vendor_specific_insn; +bool pio_mmio_emulate;/* it is the pio/mmio access if true */ bool have_exception;struct x86_exception exception;
arch/x86/include/asm/kvm_host.h +1 -1
@@ -752,7 +752,7 @@ int fx_init(struct kvm_vcpu *vcpu);void kvm_mmu_flush_tlb (struct kvm_vcpu *vcpu);void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new,int bytes, -bool guest_initiated); +bool guest_initiated,bool repeat_write);int kvm_mmu_unprotect_page_virt (struct kvm_vcpu *vcpu,gva_t gva);void __kvm_mmu_free_some_pages (struct kvm_vcpu *vcpu);int kvm_mmu_load (struct kvm_vcpu *vcpu);
arch/x86/kvm/emulate.c +3 -2
@@ -4009,8 +4009,9 @@ writeback:* Re-enter guest when pio read ahead buffer is empty * or, if it is not used, after each 1024 iteration. */ -if ((r->end !=0 || ctxt->regs[VCPU_REGS_RCX ] &0x3ff ) && - (r->end ==0 || r->end != r->pos)) { +if ((r->end !=0 || ctxt->regs[VCPU_REGS_RCX ] &0x3ff ) + && (r->end ==0 || r->end != r->pos) && + ctxt->pio_mmio_emulate) {/* * Reset read cache. Usually happens before * decode, but since instruction is restarted
arch/x86/kvm/mmu.c +2 -2
@@ -3541,7 +3541,7 @@ static int get_free_pte_list_desc_nr(struct kvm_vcpu *vcpu)void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new,int bytes, -bool guest_initiated) +bool guest_initiated,bool repeat_write) {gfn_t gfn = gpa >>PAGE_SHIFT ;union kvm_mmu_page_role mask = { .word =0 };
@@ -3622,7 +3622,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, pte_size = sp->role.cr4_pae ?8 :4 ; misaligned = (offset ^ (offset + bytes -1 )) & ~(pte_size -1 ); misaligned |= bytes <4 ; -if (misaligned || flooded) { +if (misaligned || flooded || repeat_write) {/* * Misaligned accesses are too much trouble to fix * up; also, they usually indicate a page is not used
arch/x86/kvm/paging_tmpl.h +2 -1
@@ -710,7 +710,8 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)if (mmu_topup_memory_caches (vcpu))return ; -kvm_mmu_pte_write (vcpu, pte_gpa,NULL ,sizeof (pt_element_t),0 ); +kvm_mmu_pte_write (vcpu, pte_gpa,NULL ,sizeof (pt_element_t), + false, false); } static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
arch/x86/kvm/x86.c +7 -2
@@ -4058,7 +4058,8 @@ int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, ret =kvm_write_guest (vcpu->kvm, gpa, val, bytes);if (ret <0 )return 0 ; -kvm_mmu_pte_write (vcpu, gpa, val, bytes,1 ); +kvm_mmu_pte_write (vcpu, gpa, val, bytes, true, + vcpu->arch.emulate_ctxt.rep_prefix);return 1 ; }
@@ -4161,6 +4162,8 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,return X86EMUL_CONTINUE ; mmio: + vcpu->arch.emulate_ctxt.pio_mmio_emulate = true; +/* * Is this MMIO handled locally? */
@@ -4295,7 +4298,7 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,if (!exchanged)return X86EMUL_CMPXCHG_FAILED ; -kvm_mmu_pte_write (vcpu, gpa, new, bytes,1 ); +kvm_mmu_pte_write (vcpu, gpa, new, bytes, true, false);return X86EMUL_CONTINUE ;
@@ -4326,6 +4329,7 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, {trace_kvm_pio (!in, port, size, count); + vcpu->arch.emulate_ctxt.pio_mmio_emulate = true; vcpu->arch.pio.port = port; vcpu->arch.pio.in = in; vcpu->arch.pio.count = count;
@@ -4817,6 +4821,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->perm_ok = false; + ctxt->pio_mmio_emulate = false; ctxt->only_vendor_specific_insn = emulation_type &EMULTYPE_TRAP_UD ;
Signature · 1 line
1.7.5.4
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
Quoted 1 line · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example,
By "we" do you mean Linux guest?
Quoted 8 lines
we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
So this patch does two independent things as far as I can see. First it stops reentering guest if rep instruction is done on memory and second it drops shadow pages if access to shadowed page table is rep. Why not separate those in different patches? BTW not entering guest periodically increases interrupt latencies. Why not zap shadow, make page writable and reenter the guest instead of emulation, it should be much faster (do we care to optimize for old cpus by complicating the code anyway?).
Quoted 142 lines
Signed-off-by Xiao Guangrong <[email protected]>--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/emulate.c | 5 +++-- arch/x86/kvm/mmu.c | 4 ++-- arch/x86/kvm/paging_tmpl.h | 3 ++- arch/x86/kvm/x86.c | 9 +++++++-- 6 files changed, 16 insertions(+), 8 deletions(-)arch/x86/include/asm/kvm_emulate.h +1
-0@@ -244,6 +244,7 @@ struct x86_emulate_ctxt {bool guest_mode;/* guest running a nested guest */ bool perm_ok;/* do not check permissions if true */ bool only_vendor_specific_insn; +bool pio_mmio_emulate;/* it is the pio/mmio access if true */ bool have_exception;struct x86_exception exception;arch/x86/include/asm/kvm_host.h +1
-1@@ -752,7 +752,7 @@ int fx_init(struct kvm_vcpu *vcpu);void kvm_mmu_flush_tlb (struct kvm_vcpu *vcpu);void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new,int bytes, -bool guest_initiated); +bool guest_initiated,bool repeat_write);int kvm_mmu_unprotect_page_virt (struct kvm_vcpu *vcpu,gva_t gva);void __kvm_mmu_free_some_pages (struct kvm_vcpu *vcpu);int kvm_mmu_load (struct kvm_vcpu *vcpu);arch/x86/kvm/emulate.c +3
-2@@ -4009,8 +4009,9 @@ writeback:* Re-enter guest when pio read ahead buffer is empty * or, if it is not used, after each 1024 iteration. */ -if ((r->end !=0 || ctxt->regs[VCPU_REGS_RCX ] &0x3ff ) && - (r->end ==0 || r->end != r->pos)) { +if ((r->end !=0 || ctxt->regs[VCPU_REGS_RCX ] &0x3ff ) + && (r->end ==0 || r->end != r->pos) && + ctxt->pio_mmio_emulate) {/* * Reset read cache. Usually happens before * decode, but since instruction is restarted arch/x86/kvm/mmu.c +2
-2@@ -3541,7 +3541,7 @@ static int get_free_pte_list_desc_nr(struct kvm_vcpu *vcpu)void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new,int bytes, -bool guest_initiated) +bool guest_initiated,bool repeat_write) {gfn_t gfn = gpa >>PAGE_SHIFT ;union kvm_mmu_page_role mask = { .word =0 };@@ -3622,7 +3622,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, pte_size = sp->role.cr4_pae ?8 :4 ; misaligned = (offset ^ (offset + bytes -1 )) & ~(pte_size -1 ); misaligned |= bytes <4 ; -if (misaligned || flooded) { +if (misaligned || flooded || repeat_write) {/* * Misaligned accesses are too much trouble to fix * up; also, they usually indicate a page is not used arch/x86/kvm/paging_tmpl.h +2
-1@@ -710,7 +710,8 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)if (mmu_topup_memory_caches (vcpu))return ; -kvm_mmu_pte_write (vcpu, pte_gpa,NULL ,sizeof (pt_element_t),0 ); +kvm_mmu_pte_write (vcpu, pte_gpa,NULL ,sizeof (pt_element_t), + false, false); } static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,arch/x86/kvm/x86.c +7
-2@@ -4058,7 +4058,8 @@ int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, ret =kvm_write_guest (vcpu->kvm, gpa, val, bytes);if (ret <0 )return 0 ; -kvm_mmu_pte_write (vcpu, gpa, val, bytes,1 ); +kvm_mmu_pte_write (vcpu, gpa, val, bytes, true, + vcpu->arch.emulate_ctxt.rep_prefix);return 1 ; }@@ -4161,6 +4162,8 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,return X86EMUL_CONTINUE ; mmio: + vcpu->arch.emulate_ctxt.pio_mmio_emulate = true; +/* * Is this MMIO handled locally? */ @@ -4295,7 +4298,7 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,if (!exchanged)return X86EMUL_CMPXCHG_FAILED ; -kvm_mmu_pte_write (vcpu, gpa, new, bytes,1 ); +kvm_mmu_pte_write (vcpu, gpa, new, bytes, true, false);return X86EMUL_CONTINUE ;@@ -4326,6 +4329,7 @@ static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, {trace_kvm_pio (!in, port, size, count); + vcpu->arch.emulate_ctxt.pio_mmio_emulate = true; vcpu->arch.pio.port = port; vcpu->arch.pio.in = in; vcpu->arch.pio.count = count;@@ -4817,6 +4821,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, ctxt->interruptibility =0 ; ctxt->have_exception = false; ctxt->perm_ok = false; + ctxt->pio_mmio_emulate = false; ctxt->only_vendor_specific_insn = emulation_type &EMULTYPE_TRAP_UD ;Signature · 6 lines
1.7.5.4 -- 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
--
Gleb.
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
Quoted 12 lines · Gleb Natapov
Quoted 1 line · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example,
By "we" do you mean Linux guest?
Quoted 8 lines
we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
Quoted 8 lines
So this patch does two independent things as far as I can see. First it stops reentering guest if rep instruction is done on memory and second it drops shadow pages if access to shadowed page table is rep. Why not separate those in different patches? BTW not entering guest periodically increases interrupt latencies. Why not zap shadow, make page writable and reenter the guest instead of emulation, it should be much faster (do we care to optimize for old cpus by complicating the code anyway?).
The second thing is mentioned on the TODO list in a more general way: tag instructions that are typically used to modify the page tables, and drop shadow if any other instruction is used. Since MOVS is typically not used to update pagetables, it would not be tagged.
The list would include, I'd guess, and, or, bts, btc, mov, xchg, cmpxchg, and cmpxchg8b. Anything else?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
In reply to Gleb NatapovQuoted 4 lines · Gleb Natapov
Quoted 1 line · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example,
By "we" do you mean Linux guest?
I do not know other guests except linux, but, generally rep instruction is not used to update a page table which is been using.
Quoted 4 lines
Quoted 4 lines
we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
Quoted 8 lines
Quoted 4 lines
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
So this patch does two independent things as far as I can see. First it stops reentering guest if rep instruction is done on memory and second
No.
Oppositely, it enters guest as soon as possible if rep instruction is done on memory ;-)
After this patch, we only need to emulate one time for a rep instruction.
Quoted 2 lines
it drops shadow pages if access to shadowed page table is rep. Why not separate those in different patches?
Umm, i will zap shadow page firstly and stop emulation rep instruction in the second patch.
Quoted 2 lines
BTW not entering guest periodically
increases interrupt latencies.
Oppositely, It reduces the latencies. :-)
Quoted 4 lines
Why not zap shadow, make page writable
and reenter the guest instead of emulation, it should be much faster (do we care to optimize for old cpus by complicating the code anyway?).
We do better lazily update the mapping to writable, it can be done by the later access.
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
Quoted 19 lines · Xiao Guangrong
Quoted 4 lines · Gleb Natapov
Quoted 1 line · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example,
By "we" do you mean Linux guest?
I do not know other guests except linux, but, generally rep instruction is not used to update a page table which is been using.
Quoted 8 lines
Quoted 8 lines
we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
Quoted 6 lines
Quoted 2 lines
So this patch does two independent things as far as I can see. First it stops reentering guest if rep instruction is done on memory and second
No.
Oppositely, it enters guest as soon as possible if rep instruction is done on memory ;-)
Oops. Indeed. I read it other way around. So why not just return X86EMUL_UNHANDLEABLE from emulator_write_emulated_onepage() which should have the same effect?
Quoted 11 lines
After this patch, we only need to emulate one time for a rep instruction.
Quoted 2 lines
it drops shadow pages if access to shadowed page table is rep. Why not separate those in different patches?
Umm, i will zap shadow page firstly and stop emulation rep instruction in the second patch.
Quoted 2 lines
BTW not entering guest periodically
increases interrupt latencies.
Quoted 8 lines
Oppositely, It reduces the latencies. :-)
Quoted 4 lines
Why not zap shadow, make page writable
and reenter the guest instead of emulation, it should be much faster (do we care to optimize for old cpus by complicating the code anyway?).We do better lazily update the mapping to writable, it can be done by the later access.
--
Gleb.
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
Quoted 23 lines · Gleb Natapov
Quoted 22 lines · Xiao Guangrong
Quoted 4 lines · Gleb Natapov
Quoted 1 line · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example,
By "we" do you mean Linux guest?
I do not know other guests except linux, but, generally rep instruction is not used to update a page table which is been using.
Quoted 12 lines
Quoted 8 lines
we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
So this patch does two independent things as far as I can see. First it stops reentering guest if rep instruction is done on memory and second
Quoted 7 lines
Quoted 3 lines
No.
Oppositely, it enters guest as soon as possible if rep instruction is done on memory ;-)Oops. Indeed. I read it other way around. So why not just return
X86EMUL_UNHANDLEABLEfromemulator_write_emulated_onepage()which should have the same effect?
It seams not, the count register(RCX) is not decreased, and redundant work need to be done by handling EMULATION_FAILED.
So, emulator_write_emulated_onepage() is not a good place i think. :-)
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
Quoted 27 lines · Xiao Guangrong
Quoted 26 lines · Gleb Natapov
Quoted 25 lines · Xiao Guangrong
Quoted 4 lines · Gleb Natapov
Quoted 1 line · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example,
By "we" do you mean Linux guest?
I do not know other guests except linux, but, generally rep instruction is not used to update a page table which is been using.
Quoted 4 lines
Quoted 4 lines
we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
Quoted 8 lines
Quoted 4 lines
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
So this patch does two independent things as far as I can see. First it stops reentering guest if rep instruction is done on memory and second
No.
Oppositely, it enters guest as soon as possible if rep instruction is done on memory ;-)
Quoted 7 lines
Quoted 4 lines
Oops. Indeed. I read it other way around. So why not just return
X86EMUL_UNHANDLEABLEfromemulator_write_emulated_onepage()which should have the same effect?It seams not, the count register(RCX) is not decreased, and redundant work need to be done by handling
EMULATION_FAILED.
The only difference is that with your approach one rep is emulated and then control goes back to a guest. With EMULATION_FAILED kvm returns to a guest immediately, so RCX shouldn't be decreased. There shouldn't a be big difference performance wise and if there is it is likely on EMULATION_FAILED side. Last but not least emulate.c knows nothing about the hack.
Quoted 1 line
So,
emulator_write_emulated_onepage()is not a good place i think. :-)
--
Gleb.
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
Quoted 33 lines · Gleb Natapov
Quoted 32 lines · Xiao Guangrong
Quoted 30 lines · Gleb Natapov
Quoted 19 lines · Xiao Guangrong
Quoted 4 lines · Gleb Natapov
Quoted 1 line · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example,
By "we" do you mean Linux guest?
I do not know other guests except linux, but, generally rep instruction is not used to update a page table which is been using.
Quoted 8 lines
Quoted 8 lines
we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
Quoted 6 lines
Quoted 2 lines
So this patch does two independent things as far as I can see. First it stops reentering guest if rep instruction is done on memory and second
No.
Oppositely, it enters guest as soon as possible if rep instruction is done on memory ;-)Oops. Indeed. I read it other way around. So why not just return
X86EMUL_UNHANDLEABLEfromemulator_write_emulated_onepage()which should have the same effect?
Quoted 8 lines
Quoted 2 lines
It seams not, the count register(RCX) is not decreased, and redundant work need to be done by handling
EMULATION_FAILED.The only difference is that with your approach one rep is emulated and then control goes back to a guest. With
EMULATION_FAILEDkvm returns to a guest immediately, so RCX shouldn't be decreased. There shouldn't a be big difference performance wise and if there is it is likely onEMULATION_FAILEDside. Last but not leastemulate.cknows nothing about the hack.
Umm. it is reasonable, i'll update this patch. Thanks, Gleb!
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
In reply to Xiao GuangrongQuoted 8 lines · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example, we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
Please generalize this to fail emulation on all non-page-table instructions when emulating due to a write protected page that we can unprotect.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 03/11] KVM: x86: fast emulate repeat string write instructions
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Xiao Guangrong
We usually use repeat string instructions to clear the page, for example, we call memset to clear a page table, stosb is used in this function, and repeated for 1024 times, that means we should occupy mmu lock for 1024 times and walking shadow page cache for 1024 times, it is terrible
Quoted 6 lines
Quoted 3 lines
In fact, if it is the repeat string instructions emulated and it is not a IO/MMIO access, we can zap all the corresponding shadow pages and return to the guest, then the mapping can became writable and directly write the page
Please generalize this to fail emulation on all non-page-table instructions when emulating due to a write protected page that we can unprotect.
OK, will do it in the next version, thanks!
PATCH 4/11 KVM: MMU: do not mark access bit on pte write path
In reply to Xiao GuangrongIn current code, the accessed bit is always set when page fault occurred, do not need to set it on pte write path
--- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/mmu.c | 22 +--------------------- 2 files changed, 1 insertions(+), 22 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
@@ -355,7 +355,6 @@ struct kvm_vcpu_arch {gfn_t last_pt_write_gfn;int last_pt_write_count;u64 *last_pte_updated; -gfn_t last_pte_gfn;struct fpu guest_fpu;u64 xcr0;
arch/x86/kvm/mmu.c +1 -21
@@ -2196,11 +2196,6 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,if (set_mmio_spte (sptep, gfn, pfn, pte_access))return 0 ; -/* -* We don't set the accessed bit, since we sometimes want to see -* whether the guest actually used the pte (in order to detect -* demand paging). -*/ spte =PT_PRESENT_MASK ;if (!speculative) spte |= shadow_accessed_mask;
@@ -2351,10 +2346,8 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, } }kvm_release_pfn_clean (pfn); -if (speculative) { +if (speculative) vcpu->arch.last_pte_updated = sptep; - vcpu->arch.last_pte_gfn = gfn; - } }static void nonpaging_new_cr3 (struct kvm_vcpu *vcpu)
@@ -3519,18 +3512,6 @@ static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)return !!(spte && (*spte & shadow_accessed_mask)); } -static void kvm_mmu_access_page (struct kvm_vcpu *vcpu,gfn_t gfn) -{ -u64 *spte = vcpu->arch.last_pte_updated; - -if (spte - && vcpu->arch.last_pte_gfn == gfn - && shadow_accessed_mask - && !(*spte & shadow_accessed_mask) - &&is_shadow_present_pte (*spte)) -set_bit (PT_ACCESSED_SHIFT , (unsigned long *)spte); -} -static int get_free_pte_list_desc_nr (struct kvm_vcpu *vcpu) {struct kvm_mmu_memory_cache *cache;
@@ -3604,7 +3585,6 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, ++vcpu->kvm->stat.mmu_pte_write;trace_kvm_mmu_audit (vcpu,AUDIT_PRE_PTE_WRITE );if (guest_initiated) { -kvm_mmu_access_page (vcpu, gfn);if (gfn == vcpu->arch.last_pt_write_gfn && !last_updated_pte_accessed (vcpu)) { ++vcpu->arch.last_pt_write_count;
Signature · 1 line
1.7.5.4
Re: [PATCH 04/11] KVM: MMU: do not mark access bit on pte write path
Quoted 3 lines · Xiao Guangrong
In current code, the accessed bit is always set when page fault occurred, do not need to set it on pte write path
Is this true? a write with pte.w=pte.a=0 sets pte.a?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 04/11] KVM: MMU: do not mark access bit on pte write path
Quoted 7 lines · Avi Kivity
Quoted 3 lines · Xiao Guangrong
In current code, the accessed bit is always set when page fault occurred, do not need to set it on pte write path
Is this true? a write with pte.w=pte.a=0 sets pte.a?
Generally, we will call set_spte() with speculative = false, then Accessed bit is set on page fault path, but there has two case:
- if pte.d = 1, everything is ok
- if pte.d = 0, so this write can change pte.d = 1, the access permission of shadow page can be changed from read-only to writable. So, we will find a new sp to establish the mapping. In this case, the original spte is not set Accessed bit.
However, we used Accessed bit to detect write flooding, so the first write can set the dirty bit, and the later write can not cause sp changed. I think it is not too bad.
PATCH 5/11 KVM: MMU: cleanup FNAME(invlpg)
In reply to Xiao GuangrongDirectly Use mmu_page_zap_pte to zap spte in FNAME(invlpg), also remove the same code between FNAME(invlpg) and FNAME(sync_page)
--- arch/x86/kvm/mmu.c | 16 ++++++++++------ arch/x86/kvm/paging_tmpl.h | 42 +++++++++++++++--------------------------- 2 files changed, 25 insertions(+), 33 deletions(-)
arch/x86/kvm/mmu.c +10 -6
@@ -1801,7 +1801,7 @@ static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep, } } -static void mmu_page_zap_pte (struct kvm *kvm,struct kvm_mmu_page *sp, +static bool mmu_page_zap_pte (struct kvm *kvm,struct kvm_mmu_page *sp,u64 *spte) {u64 pte;
@@ -1809,17 +1809,21 @@ static void mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp, pte = *spte;if (is_shadow_present_pte (pte)) { -if (is_last_spte (pte, sp->role.level)) +if (is_last_spte (pte, sp->role.level)) {drop_spte (kvm, spte); -else { +if (is_large_pte (pte)) + --kvm->stat.lpages; + }else { child =page_header (pte &PT64_BASE_ADDR_MASK );drop_parent_pte (child, spte); } - }else if (is_mmio_spte (pte)) +return true; + } + +if (is_mmio_spte (pte))mmu_spte_clear_no_track (spte); -if (is_large_pte (pte)) - --kvm->stat.lpages; +return false; } static void kvm_mmu_page_unlink_children(struct kvm *kvm,
arch/x86/kvm/paging_tmpl.h +15 -27
83 lines not on this page · raw message
Signature · 1 line
1.7.5.4
PATCH 6/11 KVM: MMU: fast prefetch spte on invlpg path
In reply to Xiao GuangrongFast prefetch spte for the unsync shadow page on invlpg path
--- arch/x86/include/asm/kvm_host.h | 4 +--- arch/x86/kvm/mmu.c | 38 +++++++++++++++----------------------- arch/x86/kvm/paging_tmpl.h | 23 ++++++++++------------- arch/x86/kvm/x86.c | 4 ++-- 4 files changed, 28 insertions(+), 41 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -3
@@ -456,7 +456,6 @@ struct kvm_arch {unsigned int n_requested_mmu_pages;unsigned int n_max_mmu_pages;unsigned int indirect_shadow_pages; -atomic_t invlpg_counter;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ];/* * Hash table of struct kvm_mmu_page.
@@ -750,8 +749,7 @@ int fx_init(struct kvm_vcpu *vcpu);void kvm_mmu_flush_tlb (struct kvm_vcpu *vcpu);void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa, -const u8 *new,int bytes, -bool guest_initiated,bool repeat_write); +const u8 *new,int bytes,bool repeat_write);int kvm_mmu_unprotect_page_virt (struct kvm_vcpu *vcpu,gva_t gva);void __kvm_mmu_free_some_pages (struct kvm_vcpu *vcpu);int kvm_mmu_load (struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu.c +15 -23
@@ -3525,8 +3525,7 @@ static int get_free_pte_list_desc_nr(struct kvm_vcpu *vcpu) }void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa, -const u8 *new,int bytes, -bool guest_initiated,bool repeat_write) +const u8 *new,int bytes,bool repeat_write) {gfn_t gfn = gpa >>PAGE_SHIFT ;union kvm_mmu_page_role mask = { .word =0 };
@@ -3535,7 +3534,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,LIST_HEAD (invalid_list);u64 entry, gentry, *spte;unsigned pte_size, page_offset, misaligned, quadrant, offset; -int level, npte, invlpg_counter, r, flooded =0 ; +int level, npte, r, flooded =0 ;bool remote_flush, local_flush, zap_page;/*
@@ -3550,19 +3549,16 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,pgprintk ("%s: gpa %llx bytes %d\n" , __func__, gpa, bytes); - invlpg_counter =atomic_read (&vcpu->kvm->arch.invlpg_counter); -/* * Assume that the pte write on a page table of the same type * as the current vcpu paging mode since we update the sptes only * when they have the same mode. */ -if ((is_pae (vcpu) && bytes ==4 ) || !new) { +if (is_pae (vcpu) && bytes ==4 ) {/* Handle a 32-bit guest writing two halves of a 64-bit gpte */ -if (is_pae (vcpu)) { - gpa &= ~(gpa_t )7 ; - bytes =8 ; - } + gpa &= ~(gpa_t )7 ; + bytes =8 ; + r =kvm_read_guest (vcpu->kvm, gpa, &gentry,min (bytes,8 ));if (r) gentry =0 ;
@@ -3583,22 +3579,18 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,mmu_topup_memory_caches (vcpu);spin_lock (&vcpu->kvm->mmu_lock); -if (atomic_read (&vcpu->kvm->arch.invlpg_counter) != invlpg_counter) - gentry =0 ;kvm_mmu_free_some_pages (vcpu); ++vcpu->kvm->stat.mmu_pte_write;trace_kvm_mmu_audit (vcpu,AUDIT_PRE_PTE_WRITE ); -if (guest_initiated) { -if (gfn == vcpu->arch.last_pt_write_gfn - && !last_updated_pte_accessed (vcpu)) { - ++vcpu->arch.last_pt_write_count; -if (vcpu->arch.last_pt_write_count >=3 ) - flooded =1 ; - }else { - vcpu->arch.last_pt_write_gfn = gfn; - vcpu->arch.last_pt_write_count =1 ; - vcpu->arch.last_pte_updated =NULL ; - } +if (gfn == vcpu->arch.last_pt_write_gfn + && !last_updated_pte_accessed (vcpu)) { + ++vcpu->arch.last_pt_write_count; +if (vcpu->arch.last_pt_write_count >=3 ) + flooded =1 ; + }else { + vcpu->arch.last_pt_write_gfn = gfn; + vcpu->arch.last_pt_write_count =1 ; + vcpu->arch.last_pte_updated =NULL ; } mask.cr0_wp = mask.cr4_pae = mask.nxe =1 ;
arch/x86/kvm/paging_tmpl.h +10 -13
@@ -666,20 +666,22 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva) {struct kvm_shadow_walk_iterator iterator;struct kvm_mmu_page *sp; -gpa_t pte_gpa =-1 ;int level;u64 *sptep;vcpu_clear_mmio_info (vcpu, gva); +mmu_topup_memory_caches (vcpu);spin_lock (&vcpu->kvm->mmu_lock); -for_each_shadow_entry (vcpu, gva, iterator) { level = iterator.level; sptep = iterator.sptep; sp =page_header (__pa (sptep));if (is_last_spte (*sptep, level)) { +pt_element_t gpte; +gpa_t pte_gpa; +if (!sp->unsync)break ;
@@ -688,23 +690,18 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)if (mmu_page_zap_pte (vcpu->kvm, sp, sptep))kvm_flush_remote_tlbs (vcpu->kvm); + +if (kvm_read_guest_atomic (vcpu->kvm, pte_gpa, &gpte, +sizeof (pt_element_t))) +break ; + +FNAME (update_pte)(vcpu, sp, sptep, &gpte); }if (!is_shadow_present_pte (*sptep) || !sp->unsync_children)break ; } - -atomic_inc (&vcpu->kvm->arch.invlpg_counter); -spin_unlock (&vcpu->kvm->mmu_lock); - -if (pte_gpa ==-1 ) -return ; - -if (mmu_topup_memory_caches (vcpu)) -return ; -kvm_mmu_pte_write (vcpu, pte_gpa,NULL ,sizeof (pt_element_t), - false, false); } static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
arch/x86/kvm/x86.c +2 -2
@@ -4058,7 +4058,7 @@ int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, ret =kvm_write_guest (vcpu->kvm, gpa, val, bytes);if (ret <0 )return 0 ; -kvm_mmu_pte_write (vcpu, gpa, val, bytes, true, +kvm_mmu_pte_write (vcpu, gpa, val, bytes, vcpu->arch.emulate_ctxt.rep_prefix);return 1 ; }
@@ -4298,7 +4298,7 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,if (!exchanged)return X86EMUL_CMPXCHG_FAILED ; -kvm_mmu_pte_write (vcpu, gpa, new, bytes, true, false); +kvm_mmu_pte_write (vcpu, gpa, new, bytes, false);return X86EMUL_CONTINUE ;
Signature · 1 line
1.7.5.4
PATCH 7/11 KVM: MMU: remove unnecessary kvm_mmu_free_some_pages
In reply to Xiao GuangrongIn kvm_mmu_pte_write, we do not need to alloc shadow page, so calling kvm_mmu_free_some_pages is really unnecessary
--- arch/x86/kvm/mmu.c | 1 - 1 files changed, 0 insertions(+), 1 deletions(-)
arch/x86/kvm/mmu.c +0 -1
@@ -3579,7 +3579,6 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,mmu_topup_memory_caches (vcpu);spin_lock (&vcpu->kvm->mmu_lock); -kvm_mmu_free_some_pages (vcpu); ++vcpu->kvm->stat.mmu_pte_write;trace_kvm_mmu_audit (vcpu,AUDIT_PRE_PTE_WRITE ); if (gfn == vcpu->arch.last_pt_write_gfn
Signature · 1 line
1.7.5.4
PATCH 8/11 KVM: MMU: split kvm_mmu_pte_write function
In reply to Xiao Guangrongkvm_mmu_pte_write is too long, we split it for better readable
--- arch/x86/kvm/mmu.c | 180 ++++++++++++++++++++++++++++++++-------------------- 1 files changed, 111 insertions(+), 69 deletions(-)
arch/x86/kvm/mmu.c +111 -69
@@ -3524,48 +3524,29 @@ static int get_free_pte_list_desc_nr(struct kvm_vcpu *vcpu)return mmu_memory_cache_free_object (cache); } -void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa, -const u8 *new,int bytes,bool repeat_write) +static u64 mmu_pte_write_fetch_gpte (struct kvm_vcpu *vcpu,gpa_t *gpa, +const u8 *new,int *bytes) { -gfn_t gfn = gpa >>PAGE_SHIFT ; -union kvm_mmu_page_role mask = { .word =0 }; -struct kvm_mmu_page *sp; -struct hlist_node *node; -LIST_HEAD (invalid_list); -u64 entry, gentry, *spte; -unsigned pte_size, page_offset, misaligned, quadrant, offset; -int level, npte, r, flooded =0 ; -bool remote_flush, local_flush, zap_page; - -/* -* If we don't have indirect shadow pages, it means no page is -* write-protected, so we can exit simply. -*/ -if (!ACCESS_ONCE (vcpu->kvm->arch.indirect_shadow_pages)) -return ; - - zap_page = remote_flush = local_flush = false; - offset =offset_in_page (gpa); - -pgprintk ("%s: gpa %llx bytes %d\n" , __func__, gpa, bytes); +u64 gentry; +int r;/* * Assume that the pte write on a page table of the same type * as the current vcpu paging mode since we update the sptes only * when they have the same mode. */ -if (is_pae (vcpu) && bytes ==4 ) { +if (is_pae (vcpu) && *bytes ==4 ) {/* Handle a 32-bit guest writing two halves of a 64-bit gpte */ - gpa &= ~(gpa_t )7 ; - bytes =8 ; + *gpa &= ~(gpa_t )7 ; + *bytes =8 ; - r =kvm_read_guest (vcpu->kvm, gpa, &gentry,min (bytes,8 )); + r =kvm_read_guest (vcpu->kvm, *gpa, &gentry,min (*bytes,8 ));if (r) gentry =0 ; new = (const u8 *)&gentry; } -switch (bytes) { +switch (*bytes) {case 4 : gentry = *(const u32 *)new;break ;
@@ -3577,66 +3558,127 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,break ; } -mmu_topup_memory_caches (vcpu); -spin_lock (&vcpu->kvm->mmu_lock); - ++vcpu->kvm->stat.mmu_pte_write; -trace_kvm_mmu_audit (vcpu,AUDIT_PRE_PTE_WRITE ); +return gentry; +} + +/* +* If we're seeing too many writes to a page, it may no longer be a page table, +* or we may be forking, in which case it is better to unmap the page. +*/ +static bool detect_write_flooding (struct kvm_vcpu *vcpu,gfn_t gfn) +{ +bool flooded = false; +if (gfn == vcpu->arch.last_pt_write_gfn && !last_updated_pte_accessed (vcpu)) { ++vcpu->arch.last_pt_write_count;if (vcpu->arch.last_pt_write_count >=3 ) - flooded =1 ; + flooded = true; }else { vcpu->arch.last_pt_write_gfn = gfn; vcpu->arch.last_pt_write_count =1 ; vcpu->arch.last_pte_updated =NULL ; } +return flooded; +} + +/* +* Misaligned accesses are too much trouble to fix up; also, they usually +* indicate a page is not used as a page table. +*/ +static bool detect_write_misaligned (struct kvm_mmu_page *sp,gpa_t gpa, +int bytes) +{ +unsigned offset, pte_size, misaligned; + +pgprintk ("misaligned: gpa %llx bytes %d role %x\n" , + gpa, bytes, sp->role.word); + + offset =offset_in_page (gpa); + pte_size = sp->role.cr4_pae ?8 :4 ; + misaligned = (offset ^ (offset + bytes -1 )) & ~(pte_size -1 ); + misaligned |= bytes <4 ; + +return misaligned; +} + +static u64 *get_written_sptes (struct kvm_mmu_page *sp,gpa_t gpa,int *nspte) +{ +unsigned page_offset, quadrant; +u64 *spte; +int level; + + page_offset =offset_in_page (gpa); + level = sp->role.level; + *nspte =1 ; +if (!sp->role.cr4_pae) { + page_offset <<=1 ;/* 32->64 */ +/* +* A 32-bit pde maps 4MB while the shadow pdes map +* only 2MB. So we need to double the offset again +* and zap two pdes instead of one. +*/ +if (level ==PT32_ROOT_LEVEL ) { + page_offset &= ~7 ;/* kill rounding error */ + page_offset <<=1 ; + *nspte =2 ; + } + quadrant = page_offset >>PAGE_SHIFT ; + page_offset &= ~PAGE_MASK ; +if (quadrant != sp->role.quadrant) +return NULL ; + } + + spte = &sp->spt[page_offset /sizeof (*spte)]; +return spte; +} + +void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa, +const u8 *new,int bytes,bool repeat_write) +{ +gfn_t gfn = gpa >>PAGE_SHIFT ; +union kvm_mmu_page_role mask = { .word =0 }; +struct kvm_mmu_page *sp; +struct hlist_node *node; +LIST_HEAD (invalid_list); +u64 entry, gentry, *spte; +int npte; +bool remote_flush, local_flush, zap_page, flooded, misaligned; + +/* +* If we don't have indirect shadow pages, it means no page is +* write-protected, so we can exit simply. +*/ +if (!ACCESS_ONCE (vcpu->kvm->arch.indirect_shadow_pages)) +return ; + + zap_page = remote_flush = local_flush = false; + +pgprintk ("%s: gpa %llx bytes %d\n" , __func__, gpa, bytes); + + gentry =mmu_pte_write_fetch_gpte (vcpu, &gpa, new, &bytes); +mmu_topup_memory_caches (vcpu); +spin_lock (&vcpu->kvm->mmu_lock); + ++vcpu->kvm->stat.mmu_pte_write; +trace_kvm_mmu_audit (vcpu,AUDIT_PRE_PTE_WRITE ); + + flooded =detect_write_flooding (vcpu, gfn); mask.cr0_wp = mask.cr4_pae = mask.nxe =1 ;for_each_gfn_indirect_valid_sp (vcpu->kvm, sp, gfn, node) { - pte_size = sp->role.cr4_pae ?8 :4 ; - misaligned = (offset ^ (offset + bytes -1 )) & ~(pte_size -1 ); - misaligned |= bytes <4 ; + misaligned =detect_write_misaligned (sp, gpa, bytes);if (misaligned || flooded || repeat_write) { -/* -* Misaligned accesses are too much trouble to fix -* up; also, they usually indicate a page is not used -* as a page table. -* -* If we're seeing too many writes to a page, -* it may no longer be a page table, or we may be -* forking, in which case it is better to unmap the -* page. -*/ -pgprintk ("misaligned: gpa %llx bytes %d role %x\n" , - gpa, bytes, sp->role.word); zap_page |= !!kvm_mmu_prepare_zap_page (vcpu->kvm, sp, &invalid_list); ++vcpu->kvm->stat.mmu_flooded;continue ; } - page_offset = offset; - level = sp->role.level; - npte =1 ; -if (!sp->role.cr4_pae) { - page_offset <<=1 ;/* 32->64 */ -/* -* A 32-bit pde maps 4MB while the shadow pdes map -* only 2MB. So we need to double the offset again -* and zap two pdes instead of one. -*/ -if (level ==PT32_ROOT_LEVEL ) { - page_offset &= ~7 ;/* kill rounding error */ - page_offset <<=1 ; - npte =2 ; - } - quadrant = page_offset >>PAGE_SHIFT ; - page_offset &= ~PAGE_MASK ; -if (quadrant != sp->role.quadrant) -continue ; - } + + spte =get_written_sptes (sp, gpa, &npte); +if (!spte) +continue ; + local_flush = true; - spte = &sp->spt[page_offset /sizeof (*spte)];while (npte--) { entry = *spte;mmu_page_zap_pte (vcpu->kvm, sp, spte);
Signature · 1 line
1.7.5.4
PATCH 9/11 KVM: MMU: remove the mismatch shadow page
In reply to Xiao GuangrongIf the shadow page has different cpu mode with current vcpu, we do better remove them since the OS does not changes cpu mode frequently
--- arch/x86/kvm/mmu.c | 26 +++++++++++++++++++------- 1 files changed, 19 insertions(+), 7 deletions(-)
arch/x86/kvm/mmu.c +19 -7
@@ -3603,6 +3603,18 @@ static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,return misaligned; } +/* +* The OS hardly changes cpu mode after boot, we can zap the shadow page if +* it is mismatched with the current vcpu. +*/ +static bool detect_mismatch_sp (struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) +{ +union kvm_mmu_page_role mask; + + mask.cr0_wp = mask.cr4_pae = mask.nxe =1 ; +return (sp->role.word ^ vcpu->arch.mmu.base_role.word) & mask.word; +} +static u64 *get_written_sptes (struct kvm_mmu_page *sp,gpa_t gpa,int *nspte) {unsigned page_offset, quadrant;
@@ -3638,13 +3650,12 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, int bytes, bool repeat_write) { gfn_t gfn = gpa >> PAGE_SHIFT; -union kvm_mmu_page_role mask = { .word =0 };struct kvm_mmu_page *sp;struct hlist_node *node;LIST_HEAD (invalid_list);u64 entry, gentry, *spte;int npte; -bool remote_flush, local_flush, zap_page, flooded, misaligned; +bool remote_flush, local_flush, zap_page, flooded;/* * If we don't have indirect shadow pages, it means no page is
@@ -3664,10 +3675,13 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,trace_kvm_mmu_audit (vcpu,AUDIT_PRE_PTE_WRITE ); flooded =detect_write_flooding (vcpu, gfn); - mask.cr0_wp = mask.cr4_pae = mask.nxe =1 ;for_each_gfn_indirect_valid_sp (vcpu->kvm, sp, gfn, node) { +bool mismatch, misaligned; + misaligned =detect_write_misaligned (sp, gpa, bytes); -if (misaligned || flooded || repeat_write) { + mismatch =detect_mismatch_sp (vcpu, sp); + +if (misaligned || mismatch || flooded || repeat_write) { zap_page |= !!kvm_mmu_prepare_zap_page (vcpu->kvm, sp, &invalid_list); ++vcpu->kvm->stat.mmu_flooded;
@@ -3682,9 +3696,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,while (npte--) { entry = *spte;mmu_page_zap_pte (vcpu->kvm, sp, spte); -if (gentry && - !((sp->role.word ^ vcpu->arch.mmu.base_role.word) - & mask.word) &&get_free_pte_list_desc_nr (vcpu)) +if (gentry &&get_free_pte_list_desc_nr (vcpu))mmu_pte_write_new_pte (vcpu, sp, spte, &gentry);if (!remote_flush &&need_remote_flush (entry, *spte)) remote_flush = true;
Signature · 1 line
1.7.5.4
Re: [PATCH 09/11] KVM: MMU: remove the mismatch shadow page
Quoted 3 lines · Xiao Guangrong
If the shadow page has different cpu mode with current vcpu, we do better remove them since the OS does not changes cpu mode frequently
This is true, but is it really needed? Won't such pages be torn down very quickly anyway, and will never be reestablished?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 09/11] KVM: MMU: remove the mismatch shadow page
Quoted 6 lines · Avi Kivity
Quoted 3 lines · Xiao Guangrong
If the shadow page has different cpu mode with current vcpu, we do better remove them since the OS does not changes cpu mode frequently
This is true
It's actually not true. With nested virtualization the cpu changes modes all the time.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 09/11] KVM: MMU: remove the mismatch shadow page
Quoted 10 lines · Avi Kivity
Quoted 6 lines · Avi Kivity
Quoted 3 lines · Xiao Guangrong
If the shadow page has different cpu mode with current vcpu, we do better remove them since the OS does not changes cpu mode frequently
This is true
It's actually not true. With nested virtualization the cpu changes modes all the time.
Oh, yes, i forgot this, thanks point it out!
PATCH 10/11 KVM: MMU: fix detecting misaligned accessed
In reply to Xiao GuangrongSometimes, we only modify the last one byte of a pte to update status bit, for example, clear_bit is used to clear r/w bit in linux kernel and 'andb' instruction is used in this function, in this case, kvm_mmu_pte_write will treat it as misaligned access, and the shadow page table is zapped
--- arch/x86/kvm/mmu.c | 8 ++++++++ 1 files changed, 8 insertions(+), 0 deletions(-)
arch/x86/kvm/mmu.c +8 -0
@@ -3597,6 +3597,14 @@ static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa, offset =offset_in_page (gpa); pte_size = sp->role.cr4_pae ?8 :4 ; + +/* +* Sometimes, the OS only writes the last one bytes to update status +* bits, for example, in linux, andb instruction is used in clear_bit(). +*/ +if (sp->role.level ==1 && !(offset & (pte_size -1 )) && bytes ==1 ) +return false; + misaligned = (offset ^ (offset + bytes -1 )) & ~(pte_size -1 ); misaligned |= bytes <4 ;
Signature · 1 line
1.7.5.4
Re: [PATCH 10/11] KVM: MMU: fix detecting misaligned accessed
Quoted 7 lines · Xiao Guangrong
Sometimes, we only modify the last one byte of a pte to update status bit, for example,
clear_bitis used to clear r/w bit in linux kernel and 'andb' instruction is used in this function, in this case,kvm_mmu_pte_writewill treat it as misaligned access, and the shadow page table is zappedarch/x86/kvm/mmu.c +0
-0@@ -3597,6 +3597,14 @@ static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
arch/x86/kvm/mmu.c static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa, · line 3607
Quoted 10 lines
arch/x86/kvm/mmu.c +8
-0offset =offset_in_page (gpa); pte_size = sp->role.cr4_pae ?8 :4 ; + +/* +* Sometimes, the OS only writes the last one bytes to update status +* bits, for example, in linux, andb instruction is used in clear_bit(). +*/ +if (sp->role.level ==1 && !(offset& (pte_size -1 ))&& bytes ==1 ) +return false; +
Could be true for level > 1, no?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 10/11] KVM: MMU: fix detecting misaligned accessed
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Xiao Guangrong
Sometimes, we only modify the last one byte of a pte to update status bit, for example,
clear_bitis used to clear r/w bit in linux kernel and 'andb' instruction is used in this function, in this case,kvm_mmu_pte_writewill treat it as misaligned access, and the shadow page table is zapped(quoted diff) +0
-0@@ -3597,6 +3597,14 @@ static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
Quoted 13 lines
arch/x86/kvm/mmu.c static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa, · line 3607
Quoted 10 lines
arch/x86/kvm/mmu.c +8
-0offset =offset_in_page (gpa); pte_size = sp->role.cr4_pae ?8 :4 ; + +/* +* Sometimes, the OS only writes the last one bytes to update status +* bits, for example, in linux, andb instruction is used in clear_bit(). +*/ +if (sp->role.level ==1 && !(offset& (pte_size -1 ))&& bytes ==1 ) +return false; +Could be true for level > 1, no?
In my origin mind, i thought one-byte-instruction is usually used to update the last pte, but we do better remove this restriction. Will fix it in the next version, thanks!
PATCH 11/11 KVM: MMU: improve write flooding detected
In reply to Xiao GuangrongDetecting write-flooding does not work well, when we handle page written, if the last speculative spte is not accessed, we treat the page is write-flooding, however, we can speculative spte on many path, such as pte prefetch, page synced, that means the last speculative spte may be not point to the written page and the written page can be accessed via other sptes, so depends on the Accessed bit of the last speculative spte is not enough
Instead of detected page accessed, we can detect whether the spte is accessed or not, if the spte is not accessed but it is written frequently, we treat is not a page table or it not used for a long time
--- arch/x86/include/asm/kvm_host.h | 6 +--- arch/x86/kvm/mmu.c | 53 +++++++++------------------------------ arch/x86/kvm/paging_tmpl.h | 9 +----- 3 files changed, 16 insertions(+), 52 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -4
@@ -239,6 +239,8 @@ struct kvm_mmu_page {int clear_spte_count;#endif +int write_flooding_count; +struct rcu_head rcu; };
@@ -352,10 +354,6 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_page_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; -gfn_t last_pt_write_gfn; -int last_pt_write_count; -u64 *last_pte_updated; -struct fpu guest_fpu;u64 xcr0;
arch/x86/kvm/mmu.c +12 -41
111 lines not on this page · raw message
arch/x86/kvm/paging_tmpl.h +2 -7
@@ -595,11 +595,9 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,*/ if (!r) {pgprintk ("%s: guest page fault\n" , __func__); -if (!prefault) { +if (!prefault)inject_page_fault (vcpu, &walker.fault); -/* reset fork detector */ - vcpu->arch.last_pt_write_count =0 ; - } +return 0 ; }
@@ -637,9 +635,6 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,pgprintk ("%s: shadow pte %p %llx emulate %d\n" , __func__, sptep, *sptep, emulate); -if (!emulate) - vcpu->arch.last_pt_write_count =0 ;/* reset fork detector */ - ++vcpu->stat.pf_fixed;trace_kvm_mmu_audit (vcpu,AUDIT_POST_PAGE_FAULT );spin_unlock (&vcpu->kvm->mmu_lock);
Signature · 1 line
1.7.5.4
Re: [PATCH 11/11] KVM: MMU: improve write flooding detected
Quoted 31 lines · Xiao Guangrong
Detecting write-flooding does not work well, when we handle page written, if the last speculative spte is not accessed, we treat the page is write-flooding, however, we can speculative spte on many path, such as pte prefetch, page synced, that means the last speculative spte may be not point to the written page and the written page can be accessed via other sptes, so depends on the Accessed bit of the last speculative spte is not enough
Instead of detected page accessed, we can detect whether the spte is accessed or not, if the spte is not accessed but it is written frequently, we treat is not a page table or it not used for a long time
static int get_free_pte_list_desc_nr(struct kvm_vcpu *vcpu) { struct kvm_mmu_memory_cache *cache;arch/x86/kvm/mmu.c +1
-11@@ -3565,22 +3547,14 @@ static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,* If we're seeing too many writes to a page, it may no longer be a page table, * or we may be forking, in which case it is better to unmap the page. */ -static bool detect_write_flooding (struct kvm_vcpu *vcpu,gfn_t gfn) +static bool detect_write_flooding (struct kvm_mmu_page *sp,u64 *spte) { -bool flooded = false; - -if (gfn == vcpu->arch.last_pt_write_gfn - && !last_updated_pte_accessed (vcpu)) { - ++vcpu->arch.last_pt_write_count; -if (vcpu->arch.last_pt_write_count>=3 ) - flooded = true; - }else { - vcpu->arch.last_pt_write_gfn = gfn; - vcpu->arch.last_pt_write_count =1 ;
arch/x86/kvm/mmu.c static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa, · line 3558
Quoted 10 lines
arch/x86/kvm/mmu.c +5
-3- vcpu->arch.last_pte_updated =NULL ; - } +if (spte&& !(*spte& shadow_accessed_mask)) + sp->write_flooding_count++; +else + sp->write_flooding_count =0 ; -return flooded; +return sp->write_flooding_count>=3 ; }
I think this is a little dangerous. A guest kernel may be instantiating multiple gptes on a page fault, but guest userspace hits only one of them (the one which caused the page fault) - I think Windows does this, but I'm not sure.
Maybe we should inspect parent_ptes instead?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 11/11] KVM: MMU: improve write flooding detected
Quoted 39 lines · Avi Kivity
Quoted 38 lines · Xiao Guangrong
Detecting write-flooding does not work well, when we handle page written, if the last speculative spte is not accessed, we treat the page is write-flooding, however, we can speculative spte on many path, such as pte prefetch, page synced, that means the last speculative spte may be not point to the written page and the written page can be accessed via other sptes, so depends on the Accessed bit of the last speculative spte is not enough
Instead of detected page accessed, we can detect whether the spte is accessed or not, if the spte is not accessed but it is written frequently, we treat is not a page table or it not used for a long time
static int get_free_pte_list_desc_nr(struct kvm_vcpu *vcpu) { struct kvm_mmu_memory_cache *cache;arch/x86/kvm/mmu.c +5
-13@@ -3565,22 +3547,14 @@ static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,* If we're seeing too many writes to a page, it may no longer be a page table, * or we may be forking, in which case it is better to unmap the page. */ -static bool detect_write_flooding (struct kvm_vcpu *vcpu,gfn_t gfn) +static bool detect_write_flooding (struct kvm_mmu_page *sp,u64 *spte) { -bool flooded = false; - -if (gfn == vcpu->arch.last_pt_write_gfn - && !last_updated_pte_accessed (vcpu)) { - ++vcpu->arch.last_pt_write_count; -if (vcpu->arch.last_pt_write_count>=3 ) - flooded = true; - }else { - vcpu->arch.last_pt_write_gfn = gfn; - vcpu->arch.last_pt_write_count =1 ; - vcpu->arch.last_pte_updated =NULL ; - } +if (spte&& !(*spte& shadow_accessed_mask)) + sp->write_flooding_count++; +else + sp->write_flooding_count =0 ;
Quoted 6 lines
arch/x86/kvm/mmu.c static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa, · line 3558
Quoted 3 lines
arch/x86/kvm/mmu.c +1
-1-return flooded; +return sp->write_flooding_count>=3 ; }I think this is a little dangerous. A guest kernel may be instantiating multiple gptes on a page fault, but guest userspace hits only one of them (the one which caused the page fault) - I think Windows does this, but I'm not sure.
I think this case is not bad: if the guest kernel need to write multiple gptes (>=3), it will cause many page fault, we do better zap the shadow page and let it become writable as soon as possible.
(And, we have pte-fetch, it can quickly establish the mapping for a new shadow page)
Re: [PATCH 11/11] KVM: MMU: improve write flooding detected
Quoted 9 lines · Xiao Guangrong
Quoted 4 lines
Quoted 1 line
}I think this is a little dangerous. A guest kernel may be instantiating multiple gptes on a page fault, but guest userspace hits only one of them (the one which caused the page fault) - I think Windows does this, but I'm not sure.
I think this case is not bad: if the guest kernel need to write multiple gptes (>=3), it will cause many page fault, we do better zap the shadow page and let it become writable as soon as possible.
(And, we have pte-fetch, it can quickly establish the mapping for a new shadow page)
Actually, what should save us is unsync pages. Why are we hitting this path at all?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH 11/11] KVM: MMU: improve write flooding detected
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Xiao Guangrong
Quoted 4 lines
Quoted 1 line
}I think this is a little dangerous. A guest kernel may be instantiating multiple gptes on a page fault, but guest userspace hits only one of them (the one which caused the page fault) - I think Windows does this, but I'm not sure.
Quoted 7 lines
Quoted 4 lines
I think this case is not bad: if the guest kernel need to write multiple gptes (>=3), it will cause many page fault, we do better zap the shadow page and let it become writable as soon as possible.
(And, we have pte-fetch, it can quickly establish the mapping for a new shadow page)Actually, what should save us is unsync pages. Why are we hitting this path at all?
Avi,
The shadow page can not became unsync if it has other sp that sp.gfn = gfn && sp.role.level != 1, for example:
- if the gfn is not only used for the last page structure(PTE page)
or
- gfn was used for upper page structure before but we do not zap the old shadow pages
So, if this gfn is written, #PF is generated, we hope that these sp can be zapped earlier, the later #PF can detect this gfn is not have shadow pages, and the mapping can became writable.