kvm: mmu: lazy collapse small sptes into large sptes
PATCH v2 1/1 kvm: mmu: lazy collapse small sptes into large sptes
There are two scenarios for the requirement of collapsing small sptes into large sptes.
- dirty logging tracks sptes in 4k granularity, so large sptes are split, the large sptes will be reallocated in the destination machine and the guest in the source machine will be destroyed when live migration successfully. However, the guest in the source machine will continue to run if live migration fail due to some reasons, the sptes still keep small which lead to bad performance.
- our customers write tools to track the dirty speed of guests by EPT D bit/PML in order to determine the most appropriate one to be live migrated, however sptes will still keep small after tracking dirty speed.
This patch introduce lazy collapse small sptes into large sptes, the memory region will be scanned on the ioctl context when dirty log is stopped, the ones which can be collapsed into large pages will be dropped during the scan, it depends the on later #PF to reallocate all large sptes.
---
v1 -> v2:
* use 'bool' instead of 'int'
* add more comments
* fix can not get the next spte after drop the current spte
arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.c | 71 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 19 +++++++++++ 3 files changed, 92 insertions(+)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -854,6 +854,8 @@ void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,void kvm_mmu_reset_context (struct kvm_vcpu *vcpu);void kvm_mmu_slot_remove_write_access (struct kvm *kvm,struct kvm_memory_slot *memslot); +void kvm_mmu_zap_collapsible_sptes (struct kvm *kvm, +struct kvm_memory_slot *memslot);void kvm_mmu_slot_leaf_clear_dirty (struct kvm *kvm,struct kvm_memory_slot *memslot); void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,
arch/x86/kvm/mmu.c +71 -0
77 lines not on this page · raw message
arch/x86/kvm/x86.c +19 -0
@@ -7647,6 +7647,25 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, new =id_to_memslot (kvm->memslots, mem->slot);/* +* Dirty logging tracks sptes in 4k granularity, so large sptes are +* split, the large sptes will be reallocated in the destination +* machine and the guest in the source machine will be destroyed +* when live migration successfully. However, the guest in the source +* machine will continue to run if live migration fail due to some +* reasons, the sptes still keep small which lead to bad performance. +* +* Lazy collapse small sptes into large sptes is intended to handle +* this, the memory region will be scanned on the ioctl context when +* dirty log is stopped, the ones which can be collapsed into large +* pages will be dropped during the scan, it depends the on later #PF +* to reallocate all large sptes. +*/ +if ((change !=KVM_MR_DELETE ) && + (old->flags &KVM_MEM_LOG_DIRTY_PAGES ) && + !(new->flags &KVM_MEM_LOG_DIRTY_PAGES )) +kvm_mmu_zap_collapsible_sptes (kvm, new); + +/* * Set up write protection and/or dirty logging for the new slot. * * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
Signature · 1 line
1.9.1
Quoted 59 lines · Wanpeng Li
There are two scenarios for the requirement of collapsing small sptes into large sptes.
- dirty logging tracks sptes in 4k granularity, so large sptes are split, the large sptes will be reallocated in the destination machine and the guest in the source machine will be destroyed when live migration successfully. However, the guest in the source machine will continue to run if live migration fail due to some reasons, the sptes still keep small which lead to bad performance.
- our customers write tools to track the dirty speed of guests by EPT D bit/PML in order to determine the most appropriate one to be live migrated, however sptes will still keep small after tracking dirty speed.This patch introduce lazy collapse small sptes into large sptes, the memory region will be scanned on the ioctl context when dirty log is stopped, the ones which can be collapsed into large pages will be dropped during the scan, it depends the on later #PF to reallocate all large sptes.
Signed-off-by Wanpeng Li <[email protected]>---
v1 -> v2:
* use 'bool' instead of 'int'
* add more comments
* fix can not get the next spte after drop the current sptearch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.c | 71 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 19 +++++++++++ 3 files changed, 92 insertions(+)arch/x86/include/asm/kvm_host.h +2
-0@@ -854,6 +854,8 @@ void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,void kvm_mmu_reset_context (struct kvm_vcpu *vcpu);void kvm_mmu_slot_remove_write_access (struct kvm *kvm,struct kvm_memory_slot *memslot); +void kvm_mmu_zap_collapsible_sptes (struct kvm *kvm, +struct kvm_memory_slot *memslot);void kvm_mmu_slot_leaf_clear_dirty (struct kvm *kvm,struct kvm_memory_slot *memslot); void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,arch/x86/kvm/mmu.c +9
-0@@ -4465,6 +4465,77 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,kvm_flush_remote_tlbs (kvm); } +static bool kvm_mmu_zap_collapsible_spte (struct kvm *kvm, +unsigned long *rmapp) +{ +u64 *sptep; +struct rmap_iterator iter; +int need_tlb_flush =0 ; +pfn_t pfn; +struct kvm_mmu_page *sp; +
arch/x86/kvm/mmu.c void kvm_mmu_slot_remove_write_access(struct kvm *kvm, · line 4486
Quoted 10 lines
arch/x86/kvm/mmu.c +10
-0+while ((sptep =rmap_get_first (*rmapp, &iter))) { +BUG_ON (!(*sptep &PT_PRESENT_MASK )); + + sp =page_header (__pa (sptep)); + pfn =spte_to_pfn (*sptep); + +/* +* Let support EPT only now, an efficient way need to be figure +* out to let these code be aware what mapping level used in +* guest.
This English seems strange... but i am not good at it. :)
arch/x86/kvm/mmu.c void kvm_mmu_slot_remove_write_access(struct kvm *kvm, · line 4493
Quoted 7 lines
arch/x86/kvm/mmu.c +7
-0+*/ +if (sp->role.direct && + !kvm_is_reserved_pfn (pfn) && +PageTransCompound (pfn_to_page (pfn))) { +drop_spte (kvm, sptep); + need_tlb_flush =1 ; + }
If the conditions are not comfortable, it does loop forever...
Otherwise, it looks good to me.
Quoted 66 lines · Xiao Guangrong
Quoted 63 lines · Wanpeng Li
There are two scenarios for the requirement of collapsing small sptes into large sptes.
- dirty logging tracks sptes in 4k granularity, so large sptes are split, the large sptes will be reallocated in the destination machine and the guest in the source machine will be destroyed when live migration successfully. However, the guest in the source machine will continue to run if live migration fail due to some reasons, the sptes still keep small which lead to bad performance.
- our customers write tools to track the dirty speed of guests by EPT D bit/PML in order to determine the most appropriate one to be live migrated, however sptes will still keep small after tracking dirty speed.This patch introduce lazy collapse small sptes into large sptes, the memory region will be scanned on the ioctl context when dirty log is stopped, the ones which can be collapsed into large pages will be dropped during the scan, it depends the on later #PF to reallocate all large sptes.
Signed-off-by Wanpeng Li <[email protected]>---
v1 -> v2:
* use 'bool' instead of 'int'
* add more comments
* fix can not get the next spte after drop the current sptearch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.c | 71 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 19 +++++++++++ 3 files changed, 92 insertions(+)arch/x86/include/asm/kvm_host.h +2
-0@@ -854,6 +854,8 @@ void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,void kvm_mmu_reset_context (struct kvm_vcpu *vcpu);void kvm_mmu_slot_remove_write_access (struct kvm *kvm,struct kvm_memory_slot *memslot); +void kvm_mmu_zap_collapsible_sptes (struct kvm *kvm, +struct kvm_memory_slot *memslot);void kvm_mmu_slot_leaf_clear_dirty (struct kvm *kvm,struct kvm_memory_slot *memslot); void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,arch/x86/kvm/mmu.c +13
-0@@ -4465,6 +4465,77 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,kvm_flush_remote_tlbs (kvm); } +static bool kvm_mmu_zap_collapsible_spte (struct kvm *kvm, +unsigned long *rmapp) +{ +u64 *sptep; +struct rmap_iterator iter; +int need_tlb_flush =0 ; +pfn_t pfn; +struct kvm_mmu_page *sp; + +while ((sptep =rmap_get_first (*rmapp, &iter))) { +BUG_ON (!(*sptep &PT_PRESENT_MASK )); + + sp =page_header (__pa (sptep));
Quoted 8 lines
arch/x86/kvm/mmu.c void kvm_mmu_slot_remove_write_access(struct kvm *kvm, · line 4486
Quoted 6 lines
arch/x86/kvm/mmu.c +6
-0+ pfn =spte_to_pfn (*sptep); + +/* +* Let support EPT only now, an efficient way need to be figure +* out to let these code be aware what mapping level used in +* guest. This English seems strange... but i am not good at it. :)
I'm also not good at English, anyway, update it in v3. :)
Quoted 10 lines
arch/x86/kvm/mmu.c void kvm_mmu_slot_remove_write_access(struct kvm *kvm, · line 4493
Quoted 8 lines
arch/x86/kvm/mmu.c +7
-0+*/ +if (sp->role.direct && + !kvm_is_reserved_pfn (pfn) && +PageTransCompound (pfn_to_page (pfn))) { +drop_spte (kvm, sptep); + need_tlb_flush =1 ; + }If the conditions are not comfortable, it does loop forever...
Fix it in v3.
Quoted 4 lines
Otherwise, it looks good to me.
Reviewed-by Xiao Guangrong <[email protected]>
Thanks for your review. :)
Regards,
Wanpeng Li