KVM: Add tlb_remote_flush_with_range callback in kvm_x86_ops
Subsystems (11)
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
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
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 | 69 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 20 deletions(-)
arch/x86/kvm/vmx.c +49 -20
@@ -1567,7 +1567,38 @@ 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; + +/* If ept_pointer is invalid pointer, bypass flush request. */ +if (!VALID_PAGE (ept_pointer)) +return 0 ; + +/* +* 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;
@@ -1577,30 +1608,23 @@ 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) { -if (!VALID_PAGE (to_vmx (vcpu)->ept_pointer)) -return 0 ; - - ret |=hyperv_flush_guest_mapping ( -to_vmx (vcpu)->ept_pointer &PAGE_MASK ); - } +kvm_for_each_vcpu (i, vcpu, kvm) + ret |=__hv_remote_flush_tlb_with_range ( + kvm, vcpu, range); }else { -if (!VALID_PAGE (to_vmx (kvm_get_vcpu (kvm,0 ))->ept_pointer)) -return 0 ; - - 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) {}
@@ -7957,8 +7981,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 ()) {
@@ -11567,6 +11594,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);
@@ -5680,7 +5709,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 ;
@@ -5681,7 +5688,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,
@@ -5751,7 +5759,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);
@@ -5769,7 +5778,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);
@@ -5786,7 +5796,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
@@ -1509,7 +1509,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
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
@@ -5633,8 +5633,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);
@@ -5646,12 +5651,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