x86/KVM/Hyper-v: Add HV ept tlb range flush hypercall support in KVM
Subsystems (11)
v5 cover x86/KVM/Hyper-v: Add HV ept tlb range flush hypercall support in KVM
From: Lan Tianyu <[email protected]>
For nested memory virtualization, Hyper-v doesn't set write-protect L1 hypervisor EPT page directory and page table node to track changes while it relies on guest to tell it changes via HvFlushGuestAddressLlist hypercall. HvFlushGuestAddressLlist hypercall provides a way to flush EPT page table with ranges which are specified by L1 hypervisor.
If L1 hypervisor uses INVEPT or HvFlushGuestAddressSpace hypercall to flush EPT tlb, Hyper-V will invalidate associated EPT shadow page table and sync L1's EPT table when next EPT page fault is triggered. HvFlushGuestAddressLlist hypercall helps to avoid such redundant EPT page fault and synchronization of shadow page table.
This patchset is based on the Patch "KVM/VMX: Check ept_pointer before flushing ept tlb"(https://marc.info/?l=kvm&m=154408169705686&w=2).
Change since v4:
1) Split flush address and flush list patches. This patchset only contains flush address patches. Will post flush list patches later.
2) Expose function hyperv_fill_flush_guest_mapping_list()
out of hyperv file
3) Adjust parameter of hyperv_flush_guest_mapping_range()
4) Reorder patchset and move Hyper-V and VMX changes ahead.
Change since v3:
1) Remove code of updating "tlbs_dirty" in kvm_flush_remote_tlbs_with_range()
2) Remove directly tlb flush in the kvm_handle_hva_range()
3) Move tlb flush in kvm_set_pte_rmapp() to kvm_mmu_notifier_change_pte()
4) Combine Vitaly's "don't pass EPT configuration info tovmx_hv_remote_flush_tlb()" fix
Change since v2:
1) Fix comment in the kvm_flush_remote_tlbs_with_range()
2) Move HV_MAX_FLUSH_PAGES and HV_MAX_FLUSH_REP_COUNT to hyperv-tlfs.h.
3) Calculate HV_MAX_FLUSH_REP_COUNT in the macro definition
4) Use HV_MAX_FLUSH_REP_COUNT to define length of gpa_list in struct hv_guest_mapping_flush_list.
Change since v1:
1) Convert "end_gfn" of struct kvm_tlb_range to "pages" in order to avoid confusion as to whether "end_gfn" is inclusive or exlusive.
2) Add hyperv tlb range struct and replace kvm tlb range struct with new struct in order to avoid using kvm struct in the hyperv code directly.
Lan Tianyu (10):
KVM: Add tlb_remote_flush_with_range callback in kvm_x86_ops x86/hyper-v: Add HvFlushGuestAddressList hypercall support x86/Hyper-v: Add trace in the
hyperv_nested_flush_guest_mapping_range()
KVM/VMX: Add hv tlb range flush support
KVM/MMU: Add tlb flush with range helper function
KVM: Replace old tlb flush function with new one to flush a specified
range.
KVM: Make kvm_set_spte_hva() return int
KVM/MMU: Move tlb flush in kvm_set_pte_rmapp() to
kvm_mmu_notifier_change_pte()
KVM/MMU: Flush tlb directly in the kvm_set_pte_rmapp() KVM/MMU: Flush tlb directly in the kvm_zap_gfn_range()
arch/arm/include/asm/kvm_host.h | 2 +- arch/arm64/include/asm/kvm_host.h | 2 +- arch/mips/include/asm/kvm_host.h | 2 +- arch/mips/kvm/mmu.c | 3 +- arch/powerpc/include/asm/kvm_host.h | 2 +- arch/powerpc/kvm/book3s.c | 3 +- arch/powerpc/kvm/e500_mmu_host.c | 3 +- arch/x86/hyperv/nested.c | 80 +++++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 32 +++++++++++++ arch/x86/include/asm/kvm_host.h | 9 +++- arch/x86/include/asm/mshyperv.h | 15 ++++++ arch/x86/include/asm/trace/hyperv.h | 14 ++++++ arch/x86/kvm/mmu.c | 96 +++++++++++++++++++++++++++++-------- arch/x86/kvm/paging_tmpl.h | 3 +- arch/x86/kvm/vmx.c | 63 +++++++++++++++++------- virt/kvm/arm/mmu.c | 6 ++- virt/kvm/kvm_main.c | 5 +- 17 files changed, 292 insertions(+), 48 deletions(-)
Signature · 1 line
2.14.4
PATCH RESEND v5 1/10 KVM: Add tlb_remote_flush_with_range callback in kvm_x86_ops
From: Lan Tianyu <[email protected]>
Add flush range call back in the kvm_x86_ops and platform can use it to register its associated function. The parameter "kvm_tlb_range" accepts a single range and flush list which contains a list of ranges.
---
Change since v1:
Change "end_gfn" to "pages" to aviod confusion as to whether
"end_gfn" is inclusive or exlusive.
--- arch/x86/include/asm/kvm_host.h | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/include/asm/kvm_host.h +7 -0
@@ -439,6 +439,11 @@ struct kvm_mmu {u64 pdptrs[4 ];/* pae */ }; +struct kvm_tlb_range { +u64 start_gfn; +u64 pages; +}; +enum pmc_type {KVM_PMC_GP =0 ,KVM_PMC_FIXED ,
@@ -1042,6 +1047,8 @@ struct kvm_x86_ops {void (*tlb_flush)(struct kvm_vcpu *vcpu,bool invalidate_gpa);int (*tlb_remote_flush)(struct kvm *kvm); +int (*tlb_remote_flush_with_range)(struct kvm *kvm, +struct kvm_tlb_range *range);/* * Flush any TLB entries associated with the given GVA.
Signature · 1 line
2.14.4
PATCH RESEND v5 2/10 x86/hyper-v: Add HvFlushGuestAddressList hypercall support
In reply to [email protected]From: Lan Tianyu <[email protected]>
Hyper-V provides HvFlushGuestAddressList() hypercall to flush EPT tlb with specified ranges. This patch is to add the hypercall support.
---
Change sincd v4:
- Expose function hyperv_fill_flush_guest_mapping_list() out of hyperv file
- Adjust parameter of hyperv_flush_guest_mapping_range()
Change since v2:
Fix some coding style issues
- Move HV_MAX_FLUSH_PAGES and HV_MAX_FLUSH_REP_COUNT to hyperv-tlfs.h.
- Calculate HV_MAX_FLUSH_REP_COUNT in the macro definition
- Use HV_MAX_FLUSH_REP_COUNT to define length of gpa_list in struct hv_guest_mapping_flush_list.
Change since v1:
Add hyperv tlb flush struct to avoid use kvm tlb flush struct
in the hyperv file.
--- arch/x86/hyperv/nested.c | 79 ++++++++++++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 32 +++++++++++++++ arch/x86/include/asm/mshyperv.h | 15 ++++++++ 3 files changed, 126 insertions(+)
arch/x86/hyperv/nested.c +79 -0
@@ -7,6 +7,7 @@* * Author : Lan Tianyu <[email protected]> */ +#define pr_fmt (fmt) "Hyper-V: " fmt#include <linux/types.h>
@@ -54,3 +55,81 @@ int hyperv_flush_guest_mapping(u64 as)return ret; }EXPORT_SYMBOL_GPL (hyperv_flush_guest_mapping); + +int hyperv_fill_flush_guest_mapping_list ( +struct hv_guest_mapping_flush_list *flush, +u64 start_gfn,u64 pages) +{ +u64 cur = start_gfn; +u64 additional_pages; +int gpa_n =0 ; + +do { +/* +* If flush requests exceed max flush count, go back to +* flush tlbs without range. +*/ +if (gpa_n >=HV_MAX_FLUSH_REP_COUNT ) +return -ENOSPC ; + + additional_pages =min_t (u64, pages,HV_MAX_FLUSH_PAGES ) -1 ; + + flush->gpa_list[gpa_n].page.additional_pages = additional_pages; + flush->gpa_list[gpa_n].page.largepage = false; + flush->gpa_list[gpa_n].page.basepfn = cur; + + pages -= additional_pages +1 ; + cur += additional_pages +1 ; + gpa_n++; + }while (pages >0 ); + +return gpa_n; +} +EXPORT_SYMBOL_GPL (hyperv_fill_flush_guest_mapping_list); + +int hyperv_flush_guest_mapping_range (u64 as, +hyperv_fill_flush_list_func fill_flush_list_func,void *data) +{ +struct hv_guest_mapping_flush_list **flush_pcpu; +struct hv_guest_mapping_flush_list *flush; +u64 status =0 ; +unsigned long flags; +int ret = -ENOTSUPP ; +int gpa_n =0 ; + +if (!hv_hypercall_pg || !fill_flush_list_func) + goto fault; + +local_irq_save (flags); + + flush_pcpu = (struct hv_guest_mapping_flush_list **) +this_cpu_ptr (hyperv_pcpu_input_arg); + + flush = *flush_pcpu; +if (unlikely (!flush)) { +local_irq_restore (flags); + goto fault; + } + + flush->address_space = as; + flush->flags =0 ; + + gpa_n =fill_flush_list_func (flush, data); +if (gpa_n <0 ) { +local_irq_restore (flags); + goto fault; + } + + status =hv_do_rep_hypercall (HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST , + gpa_n,0 , flush,NULL ); + +local_irq_restore (flags); + +if (!(status &HV_HYPERCALL_RESULT_MASK )) + ret =0 ; +else + ret = status; +fault: +return ret; +} +EXPORT_SYMBOL_GPL (hyperv_flush_guest_mapping_range);
arch/x86/include/asm/hyperv-tlfs.h +32 -0
@@ -10,6 +10,7 @@#define _ASM_X86_HYPERV_TLFS_H#include <linux/types.h> +#include <asm/page.h> /* * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
@@ -358,6 +359,7 @@ struct hv_tsc_emulation_status {#define HVCALL_POST_MESSAGE 0x005c#define HVCALL_SIGNAL_EVENT 0x005d#define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af +#define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0#define HV_X64_MSR_VP_ASSIST_PAGE_ENABLE 0x00000001#define HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT 12
@@ -757,6 +759,36 @@ struct hv_guest_mapping_flush {u64 flags; }; +/* +* HV_MAX_FLUSH_PAGES = "additional_pages" + 1. It's limited +* by the bitwidth of "additional_pages" in union hv_gpa_page_range. +*/ +#define HV_MAX_FLUSH_PAGES (2048) + +/* HvFlushGuestPhysicalAddressList hypercall */ +union hv_gpa_page_range { +u64 address_space; +struct { +u64 additional_pages:11 ; +u64 largepage:1 ; +u64 basepfn:52 ; + } page; +}; + +/* +* All input flush parameters should be in single page. The max flush +* count is equal with how many entries of union hv_gpa_page_range can +* be populated into the input parameter page. +*/ +#define HV_MAX_FLUSH_REP_COUNT (PAGE_SIZE - 2 * sizeof(u64) / \ + sizeof(union hv_gpa_page_range)) + +struct hv_guest_mapping_flush_list { +u64 address_space; +u64 flags; +union hv_gpa_page_range gpa_list[HV_MAX_FLUSH_REP_COUNT ]; +}; +/* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */ struct hv_tlb_flush {u64 address_space;
arch/x86/include/asm/mshyperv.h +15 -0
@@ -22,6 +22,11 @@ struct ms_hyperv_info {extern struct ms_hyperv_info ms_hyperv; + +typedef int (*hyperv_fill_flush_list_func )( +struct hv_guest_mapping_flush_list *flush, +void *data); +/* * Generate the guest ID. */
@@ -348,6 +353,11 @@ void set_hv_tscchange_cb(void (*cb)(void));void clear_hv_tscchange_cb (void );void hyperv_stop_tsc_emulation (void );int hyperv_flush_guest_mapping (u64 as); +int hyperv_flush_guest_mapping_range (u64 as, +hyperv_fill_flush_list_func fill_func,void *data); +int hyperv_fill_flush_guest_mapping_list ( +struct hv_guest_mapping_flush_list *flush, +u64 start_gfn,u64 end_gfn);#ifdef CONFIG_X86_64 void hv_apic_init (void );
@@ -370,6 +380,11 @@ static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) return NULL; }static inline int hyperv_flush_guest_mapping (u64 as) {return -1 ; } +static inline int hyperv_flush_guest_mapping_range (u64 as, +hyperv_fill_flush_list_func fill_func,void *data); +{ +return -1 ; +}#endif /* CONFIG_HYPERV */ #ifdef CONFIG_HYPERV_TSCPAGE
Signature · 1 line
2.14.4
Paolo Bonzini
maintainerRe: [Resend PATCH V5 2/10] x86/hyper-v: Add HvFlushGuestAddressList hypercall support
arch/x86/include/asm/mshyperv.h static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) · line 386
Quoted 5 lines · On 06/12/18 14:21, [email protected] wrote:
static inline int
hyperv_flush_guest_mapping(u64 as){ return -1; }arch/x86/include/asm/mshyperv.h +4
-0+static inline int hyperv_flush_guest_mapping_range (u64 as, +hyperv_fill_flush_list_func fill_func,void *data); +{ +return -1 ;
This part for !IS_ENABLED(CONFIG_HYPERV) does not compile.
No big deal, but please add that to your testing procedures.
Paolo
PATCH RESEND v5 3/10 x86/Hyper-v: Add trace in the hyperv_nested_flush_guest_mapping_range()
In reply to [email protected]From: Lan Tianyu <[email protected]>
This patch is to trace log in the hyperv_nested_flush_ guest_mapping_range().
--- arch/x86/hyperv/nested.c | 1 + arch/x86/include/asm/trace/hyperv.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+)
arch/x86/hyperv/nested.c +1 -0
@@ -130,6 +130,7 @@ int hyperv_flush_guest_mapping_range(u64 as,else ret = status; fault: +trace_hyperv_nested_flush_guest_mapping_range (as, ret);return ret; }EXPORT_SYMBOL_GPL (hyperv_flush_guest_mapping_range);
arch/x86/include/asm/trace/hyperv.h +14 -0
@@ -42,6 +42,20 @@ TRACE_EVENT(hyperv_nested_flush_guest_mapping,TP_printk ("address space %llx ret %d" , __entry->as, __entry->ret) ); +TRACE_EVENT (hyperv_nested_flush_guest_mapping_range, + TP_PROTO(u64 as, int ret), +TP_ARGS (as, ret), + +TP_STRUCT__entry ( + __field(u64, as) +__field (int, ret) + ), + TP_fast_assign(__entry->as = as; + __entry->ret = ret; + ), +TP_printk ("address space %llx ret %d" , __entry->as, __entry->ret) + ); + TRACE_EVENT(hyperv_send_ipi_mask, TP_PROTO(const struct cpumask *cpus, int vector),
Signature · 1 line
2.14.4
PATCH RESEND v5 4/10 KVM/VMX: Add hv tlb range flush support
In reply to [email protected]From: Lan Tianyu <[email protected]>
This patch is to register tlb_remote_flush_with_range callback with hv tlb range flush interface.
---
Change since v4:
- Use new function kvm_fill_hv_flush_list_func() to fill flush
request.
Change since v3:
- Merge Vitaly's don't pass EPT configuration info tovmx_hv_remote_flush_tlb() fix.
Change since v1:
- Pass flush range with new hyper-v tlb flush struct rather
than KVM tlb flush struct.
--- arch/x86/kvm/vmx.c | 63 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 17 deletions(-)
arch/x86/kvm/vmx.c +46 -17
@@ -1569,7 +1569,34 @@ static void check_ept_pointer_match(struct kvm *kvm)to_kvm_vmx (kvm)->ept_pointers_match =EPT_POINTERS_MATCH ; } -static int vmx_hv_remote_flush_tlb (struct kvm *kvm) +int kvm_fill_hv_flush_list_func (struct hv_guest_mapping_flush_list *flush, +void *data) +{ +struct kvm_tlb_range *range = data; + +return hyperv_fill_flush_guest_mapping_list (flush, range->start_gfn, + range->pages); +} + +static inline int __hv_remote_flush_tlb_with_range (struct kvm *kvm, +struct kvm_vcpu *vcpu,struct kvm_tlb_range *range) +{ +u64 ept_pointer =to_vmx (vcpu)->ept_pointer; + +/* +* FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address +* of the base of EPT PML4 table, strip off EPT configuration +* information. +*/ +if (range) +return hyperv_flush_guest_mapping_range (ept_pointer &PAGE_MASK , + kvm_fill_hv_flush_list_func, (void *)range); +else +return hyperv_flush_guest_mapping (ept_pointer &PAGE_MASK ); +} + +static int hv_remote_flush_tlb_with_range (struct kvm *kvm, +struct kvm_tlb_range *range) {struct kvm_vcpu *vcpu;int ret = -ENOTSUPP , i;
@@ -1579,29 +1606,26 @@ static int vmx_hv_remote_flush_tlb(struct kvm *kvm)if (to_kvm_vmx (kvm)->ept_pointers_match ==EPT_POINTERS_CHECK )check_ept_pointer_match (kvm); -/* -* FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the -* base of EPT PML4 table, strip off EPT configuration information. -* If ept_pointer is invalid pointer, bypass the flush request. -*/ if (to_kvm_vmx (kvm)->ept_pointers_match !=EPT_POINTERS_MATCH ) {kvm_for_each_vcpu (i, vcpu, kvm) { -u64 ept_pointer =to_vmx (vcpu)->ept_pointer; - -if (!VALID_PAGE (ept_pointer)) -continue ; - - ret |=hyperv_flush_guest_mapping ( - ept_pointer &PAGE_MASK ); +/* If ept_pointer is invalid pointer, bypass flush request. */ +if (VALID_PAGE (to_vmx (vcpu)->ept_pointer)) + ret |=__hv_remote_flush_tlb_with_range ( + kvm, vcpu, range); } }else { - ret =hyperv_flush_guest_mapping ( -to_vmx (kvm_get_vcpu (kvm,0 ))->ept_pointer &PAGE_MASK ); + ret =__hv_remote_flush_tlb_with_range (kvm, +kvm_get_vcpu (kvm,0 ), range); }spin_unlock (&to_kvm_vmx (kvm)->ept_pointer_lock); return ret; } + +static int hv_remote_flush_tlb (struct kvm *kvm) +{ +return hv_remote_flush_tlb_with_range (kvm,NULL ); +}#else /* !IS_ENABLED(CONFIG_HYPERV) */ static inline void evmcs_write64 (unsigned long field,u64 value) {}static inline void evmcs_write32 (unsigned long field,u32 value) {}
@@ -7971,8 +7995,11 @@ static __init int hardware_setup(void)#if IS_ENABLED (CONFIG_HYPERV )if (ms_hyperv.nested_features &HV_X64_NESTED_GUEST_MAPPING_FLUSH - && enable_ept) - kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb; + && enable_ept) { + kvm_x86_ops->tlb_remote_flush = hv_remote_flush_tlb; + kvm_x86_ops->tlb_remote_flush_with_range = + hv_remote_flush_tlb_with_range; + }#endif if (!cpu_has_vmx_ple ()) {
@@ -11612,6 +11639,8 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) vmx->nested.posted_intr_nv =-1 ; vmx->nested.current_vmptr =-1ull ; + vmx->ept_pointer =INVALID_PAGE ; + vmx->msr_ia32_feature_control_valid_bits =FEATURE_CONTROL_LOCKED ;/*
Signature · 1 line
2.14.4
PATCH RESEND v5 5/10 KVM/MMU: Add tlb flush with range helper function
In reply to [email protected]From: Lan Tianyu <[email protected]>
This patch is to add wrapper functions for tlb_remote_flush_with_range callback and flush tlb directly in kvm_mmu_zap_collapsible_spte(). kvm_mmu_zap_collapsible_spte() returns flush request to the slot_handle_leaf() and the latter does flush on demand. When range flush is available, make kvm_mmu_zap_collapsible_spte() to flush tlb with range directly to avoid returning range back to slot_handle_leaf().
---
Change since V4:
Remove flush list interface and flush tlb directly in kvm_mmu_zap_collapsible_spte().
Change since V3:
Remove code of updating "tlbs_dirty"
Change since V2:
Fix comment in the kvm_flush_remote_tlbs_with_range()
--- arch/x86/kvm/mmu.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
arch/x86/kvm/mmu.c +36 -1
@@ -264,6 +264,35 @@ static void mmu_spte_set(u64 *sptep, u64 spte);static union kvm_mmu_page_role kvm_mmu_calc_root_page_role (struct kvm_vcpu *vcpu); + +static inline bool kvm_available_flush_tlb_with_range (void ) +{ +return kvm_x86_ops->tlb_remote_flush_with_range; +} + +static void kvm_flush_remote_tlbs_with_range (struct kvm *kvm, +struct kvm_tlb_range *range) +{ +int ret = -ENOTSUPP ; + +if (range && kvm_x86_ops->tlb_remote_flush_with_range) + ret = kvm_x86_ops->tlb_remote_flush_with_range (kvm, range); + +if (ret) +kvm_flush_remote_tlbs (kvm); +} + +static void kvm_flush_remote_tlbs_with_address (struct kvm *kvm, +u64 start_gfn,u64 pages) +{ +struct kvm_tlb_range range; + + range.start_gfn = start_gfn; + range.pages = pages; + +kvm_flush_remote_tlbs_with_range (kvm, &range); +} +void kvm_mmu_set_mmio_spte_mask (u64 mmio_mask,u64 mmio_value) {BUG_ON ((mmio_mask & mmio_value) != mmio_value);
@@ -5671,7 +5700,13 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, !kvm_is_reserved_pfn(pfn) && PageTransCompoundMap(pfn_to_page(pfn))) {pte_list_remove (rmap_head, sptep); - need_tlb_flush =1 ; + +if (kvm_available_flush_tlb_with_range ()) +kvm_flush_remote_tlbs_with_address (kvm, sp->gfn, +KVM_PAGES_PER_HPAGE (sp->role.level)); +else + need_tlb_flush =1 ; + goto restart; } }
Signature · 1 line
2.14.4
PATCH RESEND v5 6/10 KVM: Replace old tlb flush function with new one to flush a specified range.
In reply to [email protected]From: Lan Tianyu <[email protected]>
This patch is to replace kvm_flush_remote_tlbs() with kvm_flush_ remote_tlbs_with_address() in some functions without logic change.
--- arch/x86/kvm/mmu.c | 31 +++++++++++++++++++++---------- arch/x86/kvm/paging_tmpl.h | 3 ++- 2 files changed, 23 insertions(+), 11 deletions(-)
arch/x86/kvm/mmu.c +21 -10
@@ -1485,8 +1485,12 @@ static bool __drop_large_spte(struct kvm *kvm, u64 *sptep)static void drop_large_spte (struct kvm_vcpu *vcpu,u64 *sptep) { -if (__drop_large_spte (vcpu->kvm, sptep)) -kvm_flush_remote_tlbs (vcpu->kvm); +if (__drop_large_spte (vcpu->kvm, sptep)) { +struct kvm_mmu_page *sp =page_header (__pa (sptep)); + +kvm_flush_remote_tlbs_with_address (vcpu->kvm, sp->gfn, +KVM_PAGES_PER_HPAGE (sp->role.level)); + } }/*
@@ -1954,7 +1958,8 @@ static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) rmap_head =gfn_to_rmap (vcpu->kvm, gfn, sp);kvm_unmap_rmapp (vcpu->kvm, rmap_head,NULL , gfn, sp->role.level,0 ); -kvm_flush_remote_tlbs (vcpu->kvm); +kvm_flush_remote_tlbs_with_address (vcpu->kvm, sp->gfn, +KVM_PAGES_PER_HPAGE (sp->role.level)); }int kvm_age_hva (struct kvm *kvm,unsigned long start,unsigned long end)
@@ -2470,7 +2475,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,account_shadowed (vcpu->kvm, sp);if (level ==PT_PAGE_TABLE_LEVEL &&rmap_write_protect (vcpu, gfn)) -kvm_flush_remote_tlbs (vcpu->kvm); +kvm_flush_remote_tlbs_with_address (vcpu->kvm, gfn,1 );if (level >PT_PAGE_TABLE_LEVEL && need_sync) flush |=kvm_sync_pages (vcpu, gfn, &invalid_list);
@@ -2590,7 +2595,7 @@ static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,return ;drop_parent_pte (child, sptep); -kvm_flush_remote_tlbs (vcpu->kvm); +kvm_flush_remote_tlbs_with_address (vcpu->kvm, child->gfn,1 ); } }
@@ -3014,8 +3019,10 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned pte_access, ret =RET_PF_EMULATE ;kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); } +if (set_spte_ret &SET_SPTE_NEED_REMOTE_TLB_FLUSH || flush) -kvm_flush_remote_tlbs (vcpu->kvm); +kvm_flush_remote_tlbs_with_address (vcpu->kvm, gfn, +KVM_PAGES_PER_HPAGE (level));if (unlikely (is_mmio_spte (*sptep))) ret =RET_PF_EMULATE ;
@@ -5672,7 +5679,8 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm,* on PT_WRITABLE_MASK anymore. */ if (flush) -kvm_flush_remote_tlbs (kvm); +kvm_flush_remote_tlbs_with_address (kvm, memslot->base_gfn, + memslot->npages); } static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
@@ -5742,7 +5750,8 @@ void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,* dirty_bitmap. */ if (flush) -kvm_flush_remote_tlbs (kvm); +kvm_flush_remote_tlbs_with_address (kvm, memslot->base_gfn, + memslot->npages); }EXPORT_SYMBOL_GPL (kvm_mmu_slot_leaf_clear_dirty);
@@ -5760,7 +5769,8 @@ void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,lockdep_assert_held (&kvm->slots_lock);if (flush) -kvm_flush_remote_tlbs (kvm); +kvm_flush_remote_tlbs_with_address (kvm, memslot->base_gfn, + memslot->npages); }EXPORT_SYMBOL_GPL (kvm_mmu_slot_largepage_remove_write_access);
@@ -5777,7 +5787,8 @@ void kvm_mmu_slot_set_dirty(struct kvm *kvm,/* see kvm_mmu_slot_leaf_clear_dirty */ if (flush) -kvm_flush_remote_tlbs (kvm); +kvm_flush_remote_tlbs_with_address (kvm, memslot->base_gfn, + memslot->npages); }EXPORT_SYMBOL_GPL (kvm_mmu_slot_set_dirty);
arch/x86/kvm/paging_tmpl.h +2 -1
@@ -894,7 +894,8 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa) pte_gpa += (sptep - sp->spt) *sizeof (pt_element_t);if (mmu_page_zap_pte (vcpu->kvm, sp, sptep)) -kvm_flush_remote_tlbs (vcpu->kvm); +kvm_flush_remote_tlbs_with_address (vcpu->kvm, + sp->gfn,KVM_PAGES_PER_HPAGE (sp->role.level));if (!rmap_can_add (vcpu))break ;
Signature · 1 line
2.14.4
PATCH RESEND v5 7/10 KVM: Make kvm_set_spte_hva() return int
In reply to [email protected]From: Lan Tianyu <[email protected]>
The patch is to make kvm_set_spte_hva() return int and caller can check return value to determine flush tlb or not.
--- arch/arm/include/asm/kvm_host.h | 2 +- arch/arm64/include/asm/kvm_host.h | 2 +- arch/mips/include/asm/kvm_host.h | 2 +- arch/mips/kvm/mmu.c | 3 ++- arch/powerpc/include/asm/kvm_host.h | 2 +- arch/powerpc/kvm/book3s.c | 3 ++- arch/powerpc/kvm/e500_mmu_host.c | 3 ++- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu.c | 3 ++- virt/kvm/arm/mmu.c | 6 ++++-- 10 files changed, 17 insertions(+), 11 deletions(-)
arch/arm/include/asm/kvm_host.h +1 -1
@@ -225,7 +225,7 @@ int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,#define KVM_ARCH_WANT_MMU_NOTIFIER int kvm_unmap_hva_range (struct kvm *kvm,unsigned long start,unsigned long end); -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte); +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte);unsigned long kvm_arm_num_regs (struct kvm_vcpu *vcpu);int kvm_arm_copy_reg_indices (struct kvm_vcpu *vcpu,u64 __user *indices);
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -360,7 +360,7 @@ int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,#define KVM_ARCH_WANT_MMU_NOTIFIER int kvm_unmap_hva_range (struct kvm *kvm,unsigned long start,unsigned long end); -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte); +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte);int kvm_age_hva (struct kvm *kvm,unsigned long start,unsigned long end);int kvm_test_age_hva (struct kvm *kvm,unsigned long hva);
arch/mips/include/asm/kvm_host.h +1 -1
@@ -933,7 +933,7 @@ enum kvm_mips_fault_result kvm_trap_emul_gva_fault(struct kvm_vcpu *vcpu,#define KVM_ARCH_WANT_MMU_NOTIFIER int kvm_unmap_hva_range (struct kvm *kvm,unsigned long start,unsigned long end); -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte); +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte);int kvm_age_hva (struct kvm *kvm,unsigned long start,unsigned long end);int kvm_test_age_hva (struct kvm *kvm,unsigned long hva);
arch/mips/kvm/mmu.c +2 -1
@@ -551,7 +551,7 @@ static int kvm_set_spte_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end, (pte_dirty (old_pte) && !pte_dirty (hva_pte)); } -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) {unsigned long end = hva +PAGE_SIZE ;int ret;
@@ -559,6 +559,7 @@ void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) ret =handle_hva_to_gpa (kvm, hva, end, &kvm_set_spte_handler, &pte);if (ret) kvm_mips_callbacks->flush_shadow_all (kvm); +return 0 ; } static int kvm_age_hva_handler(struct kvm *kvm, gfn_t gfn, gfn_t gfn_end,
arch/powerpc/include/asm/kvm_host.h +1 -1
@@ -72,7 +72,7 @@ extern int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end);extern int kvm_age_hva (struct kvm *kvm,unsigned long start,unsigned long end);extern int kvm_test_age_hva (struct kvm *kvm,unsigned long hva); -extern void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte); +extern int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte);#define HPTEG_CACHE_NUM (1 << 15)#define HPTEG_HASH_BITS_PTE 13
arch/powerpc/kvm/book3s.c +2 -1
@@ -850,9 +850,10 @@ int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)return kvm->arch.kvm_ops->test_age_hva (kvm, hva); } -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) { kvm->arch.kvm_ops->set_spte_hva (kvm, hva, pte); +return 0 ; }void kvmppc_mmu_destroy (struct kvm_vcpu *vcpu)
arch/powerpc/kvm/e500_mmu_host.c +2 -1
@@ -757,10 +757,11 @@ int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)return 0 ; } -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) {/* The page will get remapped properly on its next fault */ kvm_unmap_hva (kvm, hva); +return 0 ; }/*****************************************/
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1510,7 +1510,7 @@ asmlinkage void kvm_spurious_fault(void);int kvm_unmap_hva_range (struct kvm *kvm,unsigned long start,unsigned long end);int kvm_age_hva (struct kvm *kvm,unsigned long start,unsigned long end);int kvm_test_age_hva (struct kvm *kvm,unsigned long hva); -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte); +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte);int kvm_cpu_has_injectable_intr (struct kvm_vcpu *v);int kvm_cpu_has_interrupt (struct kvm_vcpu *vcpu);int kvm_arch_interrupt_allowed (struct kvm_vcpu *vcpu);
arch/x86/kvm/mmu.c +2 -1
@@ -1913,9 +1913,10 @@ int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)return kvm_handle_hva_range (kvm, start, end,0 , kvm_unmap_rmapp); } -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) {kvm_handle_hva (kvm, hva, (unsigned long )&pte, kvm_set_pte_rmapp); +return 0 ; } static int kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
virt/kvm/arm/mmu.c +4 -2
@@ -1849,14 +1849,14 @@ static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data } -void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) +int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) {unsigned long end = hva +PAGE_SIZE ;kvm_pfn_t pfn =pte_pfn (pte);pte_t stage2_pte;if (!kvm->arch.pgd) -return ; +return 0 ;trace_kvm_set_spte_hva (hva);
@@ -1867,6 +1867,8 @@ void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)clean_dcache_guest_page (pfn,PAGE_SIZE ); stage2_pte =pfn_pte (pfn,PAGE_S2 );handle_hva_to_gpa (kvm, hva, end, &kvm_set_spte_handler, &stage2_pte); + +return 0 ; }static int kvm_age_hva_handler (struct kvm *kvm,gpa_t gpa,u64 size,void *data)
Signature · 1 line
2.14.4
Re: [Resend PATCH V5 7/10] KVM: Make kvm_set_spte_hva() return int
Quoted 4 lines · On Thu, Dec 06, 2018 at 09:21:10PM +0800, [email protected] wrote:
From: Lan Tianyu <[email protected]>
The patch is to make
kvm_set_spte_hva()return int and caller can check return value to determine flush tlb or not.
It would be helpful if the patch description told the reader which return value(s) mean that the caller should flush the tlb. I would guess that non-zero means to do the flush, but you should make that explicit.
Quoted 1 line
Signed-off-by Lan Tianyu <[email protected]>
For the powerpc bits:
Re: [Resend PATCH V5 7/10] KVM: Make kvm_set_spte_hva() return int
Hi Paul:
Thanks for your review.
Quoted 11 lines · Paul Mackerras
Quoted 4 lines · On Thu, Dec 06, 2018 at 09:21:10PM +0800, [email protected] wrote:
From: Lan Tianyu <[email protected]>
The patch is to make
kvm_set_spte_hva()return int and caller can check return value to determine flush tlb or not.It would be helpful if the patch description told the reader which return value(s) mean that the caller should flush the tlb. I would guess that non-zero means to do the flush, but you should make that explicit.
OK. Thanks for suggestion and will update in the next version.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Lan Tianyu <[email protected]>For the powerpc bits:
Acked-by Paul Mackerras <[email protected]>
Signature · 2 lines
Best regards Tianyu Lan
PATCH RESEND v5 8/10 KVM/MMU: Move tlb flush in kvm_set_pte_rmapp() to kvm_mmu_notifier_change_pte()
In reply to [email protected]From: Lan Tianyu <[email protected]>
This patch is to move tlb flush in kvm_set_pte_rmapp() to kvm_mmu_notifier_change_pte() in order to avoid redundant tlb flush.
--- arch/x86/kvm/mmu.c | 8 ++------ virt/kvm/kvm_main.c | 5 ++++- 2 files changed, 6 insertions(+), 7 deletions(-)
arch/x86/kvm/mmu.c +2 -6
@@ -1776,10 +1776,7 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, } } -if (need_flush) -kvm_flush_remote_tlbs (kvm); - -return 0 ; +return need_flush; }struct slot_rmap_walk_iterator {
@@ -1915,8 +1912,7 @@ int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)int kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte) { -kvm_handle_hva (kvm, hva, (unsigned long )&pte, kvm_set_pte_rmapp); -return 0 ; +return kvm_handle_hva (kvm, hva, (unsigned long )&pte, kvm_set_pte_rmapp); } static int kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
virt/kvm/kvm_main.c +4 -1
@@ -354,7 +354,10 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock); kvm->mmu_notifier_seq++; -kvm_set_spte_hva (kvm, address, pte); + +if (kvm_set_spte_hva (kvm, address, pte)) +kvm_flush_remote_tlbs (kvm); +spin_unlock (&kvm->mmu_lock);srcu_read_unlock (&kvm->srcu, idx); }
Signature · 1 line
2.14.4
PATCH RESEND v5 9/10 KVM/MMU: Flush tlb directly in the kvm_set_pte_rmapp()
In reply to [email protected]From: Lan Tianyu <[email protected]>
This patch is to flush tlb directly in the kvm_set_pte_rmapp() and return 0.
--- arch/x86/kvm/mmu.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kvm/mmu.c +5 -0
@@ -1776,6 +1776,11 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, } } +if (need_flush &&kvm_available_flush_tlb_with_range ()) { +kvm_flush_remote_tlbs_with_address (kvm, gfn,1 ); +return 0 ; + } +return need_flush; }
Signature · 1 line
2.14.4
PATCH RESEND v5 10/10 KVM/MMU: Flush tlb directly in the kvm_zap_gfn_range()
In reply to [email protected]From: Lan Tianyu <[email protected]>
Originally, flush tlb is done by slot_handle_level_range(). This patch is to flush tlb directly in the kvm_zap_gfn_range() when range flush is available.
---
Change since v4:
Move operation of setting flush_tlb out of for loop.
--- arch/x86/kvm/mmu.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu.c +13 -3
@@ -5624,8 +5624,13 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) {struct kvm_memslots *slots;struct kvm_memory_slot *memslot; +bool flush_tlb = true; +bool flush = false;int i; +if (kvm_available_flush_tlb_with_range ()) + flush_tlb = false; +spin_lock (&kvm->mmu_lock);for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { slots =__kvm_memslots (kvm, i);
@@ -5637,12 +5642,17 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (start >= end)continue ; -slot_handle_level_range (kvm, memslot, kvm_zap_rmapp, -PT_PAGE_TABLE_LEVEL ,PT_MAX_HUGEPAGE_LEVEL , - start, end -1 , true); + flush |=slot_handle_level_range (kvm, memslot, + kvm_zap_rmapp,PT_PAGE_TABLE_LEVEL , +PT_MAX_HUGEPAGE_LEVEL , start, + end -1 , flush_tlb); } } +if (flush) +kvm_flush_remote_tlbs_with_address (kvm, gfn_start, + gfn_end - gfn_start +1 ); +spin_unlock (&kvm->mmu_lock); }
Signature · 1 line
2.14.4
Quoted 74 lines · On 06/12/18 14:21, [email protected] wrote:
From: Lan Tianyu <[email protected]>
For nested memory virtualization, Hyper-v doesn't set write-protect L1 hypervisor EPT page directory and page table node to track changes while it relies on guest to tell it changes via HvFlushGuestAddressLlist hypercall. HvFlushGuestAddressLlist hypercall provides a way to flush EPT page table with ranges which are specified by L1 hypervisor.
If L1 hypervisor uses INVEPT or HvFlushGuestAddressSpace hypercall to flush EPT tlb, Hyper-V will invalidate associated EPT shadow page table and sync L1's EPT table when next EPT page fault is triggered. HvFlushGuestAddressLlist hypercall helps to avoid such redundant EPT page fault and synchronization of shadow page table.
This patchset is based on the Patch "KVM/VMX: Check
ept_pointerbefore flushing ept tlb"(https://marc.info/?l=kvm&m=154408169705686&w=2).Change since v4:
1) Split flush address and flush list patches. This patchset only contains flush address patches. Will post flush list patches later.
2) Expose functionhyperv_fill_flush_guest_mapping_list()
out of hyperv file
3) Adjust parameter ofhyperv_flush_guest_mapping_range()
4) Reorder patchset and move Hyper-V and VMX changes ahead.Change since v3:
1) Remove code of updating "tlbs_dirty" inkvm_flush_remote_tlbs_with_range()
2) Remove directly tlb flush in thekvm_handle_hva_range()
3) Move tlb flush inkvm_set_pte_rmapp()tokvm_mmu_notifier_change_pte()
4) Combine Vitaly's "don't pass EPT configuration info tovmx_hv_remote_flush_tlb()" fixChange since v2:
1) Fix comment in thekvm_flush_remote_tlbs_with_range()
2) MoveHV_MAX_FLUSH_PAGESandHV_MAX_FLUSH_REP_COUNTtohyperv-tlfs.h.
3) CalculateHV_MAX_FLUSH_REP_COUNTin the macro definition
4) UseHV_MAX_FLUSH_REP_COUNTto define length ofgpa_listin structhv_guest_mapping_flush_list.Change since v1:
1) Convert "end_gfn" of structkvm_tlb_rangeto "pages" in order to avoid confusion as to whether "end_gfn" is inclusive or exlusive.
2) Add hyperv tlb range struct and replace kvm tlb range struct with new struct in order to avoid using kvm struct in the hyperv code directly.Lan Tianyu (10):
KVM: Addtlb_remote_flush_with_rangecallback inkvm_x86_opsx86/hyper-v: Add HvFlushGuestAddressList hypercall support x86/Hyper-v: Add trace in the
hyperv_nested_flush_guest_mapping_range()
KVM/VMX: Add hv tlb range flush support
KVM/MMU: Add tlb flush with range helper function
KVM: Replace old tlb flush function with new one to flush a specified
range.
KVM: Makekvm_set_spte_hva()return int
KVM/MMU: Move tlb flush inkvm_set_pte_rmapp()to
kvm_mmu_notifier_change_pte()
KVM/MMU: Flush tlb directly in thekvm_set_pte_rmapp()KVM/MMU: Flush tlb directly in thekvm_zap_gfn_range()arch/arm/include/asm/kvm_host.h | 2 +- arch/arm64/include/asm/kvm_host.h | 2 +- arch/mips/include/asm/kvm_host.h | 2 +- arch/mips/kvm/mmu.c | 3 +- arch/powerpc/include/asm/kvm_host.h | 2 +- arch/powerpc/kvm/book3s.c | 3 +- arch/powerpc/kvm/e500_mmu_host.c | 3 +- arch/x86/hyperv/nested.c | 80 +++++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 32 +++++++++++++ arch/x86/include/asm/kvm_host.h | 9 +++-
Quoted 9 lines
arch/x86/include/asm/mshyperv.h | 15 ++++++ arch/x86/include/asm/trace/hyperv.h | 14 ++++++ arch/x86/kvm/mmu.c | 96 +++++++++++++++++++++++++++++-------- arch/x86/kvm/paging_tmpl.h | 3 +- arch/x86/kvm/vmx.c | 63 +++++++++++++++++------- virt/kvm/arm/mmu.c | 6 ++- virt/kvm/kvm_main.c | 5 +- 17 files changed, 292 insertions(+), 48 deletions(-)
Queued, thanks.
Paolo