KVM: VMX: Clean up Hyper-V PV TLB flush
v3 cover KVM: VMX: Clean up Hyper-V PV TLB flush
Clean up KVM's PV TLB flushing when running with EPT on Hyper-V, i.e. as a nested VMM. No real goal in mind other than the sole patch in v1, which is a minor change to avoid a future mixup when TDX also wants to define .remote_flush_tlb. Everything else is opportunistic clean up.
Patch 1 legitimately tested on VMX (no SVM), everything else effectively build tested only.
v3:
- Add a patch to pass the root_hpa instead of pgd to vmx_load_mmu_pgd() and retrieve the active PCID only when necessary. [Vitaly]
- Selectively collects reviews (skipped a few due to changes). [Vitaly]
- Explicitly invalidate hv_tlb_eptp instead of leaving it valid when the mismatch tracker "knows" it's invalid. [Vitaly]
- Change the last patch to use "hv_root_ept" instead of "hv_tlb_pgd" to better reflect what is actually being tracked.
v2: Rewrite everything.
Sean Christopherson (11):
KVM: x86: Get active PCID only when writing a CR3 value
KVM: VMX: Track common EPTP for Hyper-V's paravirt TLB flush
KVM: VMX: Stash kvm_vmx in a local variable for Hyper-V paravirt TLB
flush
KVM: VMX: Fold Hyper-V EPTP checking into it's only caller
KVM: VMX: Do Hyper-V TLB flush iff vCPU's EPTP hasn't been flushed
KVM: VMX: Invalidate hv_tlb_eptp to denote an EPTP mismatch
KVM: VMX: Don't invalidate hv_tlb_eptp if the new EPTP matches
KVM: VMX: Explicitly check for hv_remote_flush_tlb when loading pgd
KVM: VMX: Define Hyper-V paravirt TLB flush fields iff Hyper-V is
enabled
KVM: VMX: Skip additional Hyper-V TLB EPTP flushes if one fails
KVM: VMX: Track root HPA instead of EPTP for paravirt Hyper-V TLB
flush
arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/svm/svm.c | 4 +- arch/x86/kvm/vmx/vmx.c | 134 ++++++++++++++++++-------------- arch/x86/kvm/vmx/vmx.h | 19 ++--- 5 files changed, 87 insertions(+), 76 deletions(-)
Signature · 1 line
2.28.0
PATCH v3 1/11 KVM: x86: Get active PCID only when writing a CR3 value
Retrieve the active PCID only when writing a guest CR3 value, i.e. don't get the PCID when using EPT. The PCID is not used when EPT is enabled, and must be manually stripped, which is annoying and unnecessary. And on VMX, getting the active PCID also involves reading the guest's CR3 and CR4.PCIDE, i.e. may add pointless VMREADs.
Opportunistically rename the pgd/pgd_level params to root_hpa and root_level to better reflect their new roles. Keep the function names, as "load the guest PGD" is still accurate/correct.
Last, and probably least, pass root_hpa as a hpa_t/u64 instead of an unsigned long. The EPTP holds a 64-bit value, even in 32-bit mode, so in theory EPT could support HIGHMEM for 32-bit KVM. Never mind that doing so would require changing the MMU page allocators and reworking the MMU to use kmap().
--- arch/x86/include/asm/kvm_host.h | 4 ++-- arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/vmx/vmx.c | 13 ++++++------- arch/x86/kvm/vmx/vmx.h | 3 +-- 5 files changed, 12 insertions(+), 14 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -2
@@ -1181,8 +1181,8 @@ struct kvm_x86_ops {int (*set_identity_map_addr)(struct kvm *kvm,u64 ident_addr);u64 (*get_mt_mask)(struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio); -void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,unsigned long pgd, -int pgd_level); +void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa, +int root_level);bool (*has_wbinvd_exit)(void );
arch/x86/kvm/mmu.h +1 -1
@@ -95,7 +95,7 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu)if (!VALID_PAGE (root_hpa))return ; - kvm_x86_ops.load_mmu_pgd (vcpu, root_hpa |kvm_get_active_pcid (vcpu), + kvm_x86_ops.load_mmu_pgd (vcpu, root_hpa, vcpu->arch.mmu->shadow_root_level); }
arch/x86/kvm/svm/svm.c +2 -2
@@ -3667,13 +3667,13 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)return svm_exit_handlers_fastpath (vcpu); } -static void svm_load_mmu_pgd (struct kvm_vcpu *vcpu,unsigned long root, +static void svm_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {struct vcpu_svm *svm =to_svm (vcpu);unsigned long cr3; - cr3 =__sme_set (root); + cr3 =__sme_set (root_hpa) |kvm_get_active_pcid (vcpu);if (npt_enabled) { svm->vmcb->control.nested_cr3 = cr3;vmcb_mark_dirty (svm->vmcb,VMCB_NPT );
arch/x86/kvm/vmx/vmx.c +6 -7
@@ -3043,8 +3043,7 @@ static int vmx_get_max_tdp_level(void)return 4 ; } -u64 construct_eptp (struct kvm_vcpu *vcpu,unsigned long root_hpa, -int root_level) +u64 construct_eptp (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {u64 eptp =VMX_EPTP_MT_WB ;
@@ -3053,13 +3052,13 @@ u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,if (enable_ept_ad_bits && (!is_guest_mode (vcpu) ||nested_ept_ad_enabled (vcpu))) eptp |=VMX_EPTP_AD_ENABLE_BIT ; - eptp |= (root_hpa &PAGE_MASK ); + eptp |= root_hpa;return eptp; } -static void vmx_load_mmu_pgd (struct kvm_vcpu *vcpu,unsigned long pgd, -int pgd_level) +static void vmx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int root_level) {struct kvm *kvm = vcpu->kvm;bool update_guest_cr3 = true;
@@ -3067,7 +3066,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,u64 eptp;if (enable_ept) { - eptp =construct_eptp (vcpu, pgd, pgd_level); + eptp =construct_eptp (vcpu, root_hpa, root_level);vmcs_write64 (EPT_POINTER , eptp);if (kvm_x86_ops.tlb_remote_flush) {
@@ -3086,7 +3085,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd, update_guest_cr3 = false;vmx_ept_load_pdptrs (vcpu); }else { - guest_cr3 = pgd; + guest_cr3 = root_hpa |kvm_get_active_pcid (vcpu); }if (update_guest_cr3)
arch/x86/kvm/vmx/vmx.h +1 -2
@@ -326,8 +326,7 @@ void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);void ept_save_pdptrs (struct kvm_vcpu *vcpu);void vmx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);void vmx_set_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg); -u64 construct_eptp (struct kvm_vcpu *vcpu,unsigned long root_hpa, -int root_level); +u64 construct_eptp (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);void update_exception_bitmap (struct kvm_vcpu *vcpu);void vmx_update_msr_bitmap (struct kvm_vcpu *vcpu);
Signature · 1 line
2.28.0
Re: [PATCH v3 01/11] KVM: x86: Get active PCID only when writing a CR3 value
Quoted 88 lines · Sean Christopherson
Retrieve the active PCID only when writing a guest CR3 value, i.e. don't get the PCID when using EPT. The PCID is not used when EPT is enabled, and must be manually stripped, which is annoying and unnecessary. And on VMX, getting the active PCID also involves reading the guest's CR3 and CR4.PCIDE, i.e. may add pointless VMREADs.
Opportunistically rename the pgd/pgd_level params to
root_hpaandroot_levelto better reflect their new roles. Keep the function names, as "load the guest PGD" is still accurate/correct.Last, and probably least, pass
root_hpaas a hpa_t/u64 instead of an unsigned long. The EPTP holds a 64-bit value, even in 32-bit mode, so in theory EPT could support HIGHMEM for 32-bit KVM. Never mind that doing so would require changing the MMU page allocators and reworking the MMU to usekmap().Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_host.h | 4 ++-- arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/svm/svm.c | 4 ++-- arch/x86/kvm/vmx/vmx.c | 13 ++++++------- arch/x86/kvm/vmx/vmx.h | 3 +-- 5 files changed, 12 insertions(+), 14 deletions(-)arch/x86/include/asm/kvm_host.h +2
-2@@ -1181,8 +1181,8 @@ struct kvm_x86_ops {int (*set_identity_map_addr)(struct kvm *kvm,u64 ident_addr);u64 (*get_mt_mask)(struct kvm_vcpu *vcpu,gfn_t gfn,bool is_mmio); -void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,unsigned long pgd, -int pgd_level); +void (*load_mmu_pgd)(struct kvm_vcpu *vcpu,hpa_t root_hpa, +int root_level);bool (*has_wbinvd_exit)(void );arch/x86/kvm/mmu.h +1
-1@@ -95,7 +95,7 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu)if (!VALID_PAGE (root_hpa))return ; - kvm_x86_ops.load_mmu_pgd (vcpu, root_hpa |kvm_get_active_pcid (vcpu), + kvm_x86_ops.load_mmu_pgd (vcpu, root_hpa, vcpu->arch.mmu->shadow_root_level); }arch/x86/kvm/svm/svm.c +2
-2@@ -3667,13 +3667,13 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)return svm_exit_handlers_fastpath (vcpu); } -static void svm_load_mmu_pgd (struct kvm_vcpu *vcpu,unsigned long root, +static void svm_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {struct vcpu_svm *svm =to_svm (vcpu);unsigned long cr3; - cr3 =__sme_set (root); + cr3 =__sme_set (root_hpa) |kvm_get_active_pcid (vcpu);if (npt_enabled) { svm->vmcb->control.nested_cr3 = cr3;vmcb_mark_dirty (svm->vmcb,VMCB_NPT );arch/x86/kvm/vmx/vmx.c +1
-2@@ -3043,8 +3043,7 @@ static int vmx_get_max_tdp_level(void)return 4 ; } -u64 construct_eptp (struct kvm_vcpu *vcpu,unsigned long root_hpa, -int root_level) +u64 construct_eptp (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {u64 eptp =VMX_EPTP_MT_WB ;
arch/x86/kvm/vmx/vmx.c u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa, · line 3055
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -3053,13 +3052,13 @@ u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,if (enable_ept_ad_bits && (!is_guest_mode (vcpu) ||nested_ept_ad_enabled (vcpu))) eptp |=VMX_EPTP_AD_ENABLE_BIT ; - eptp |= (root_hpa &PAGE_MASK ); + eptp |= root_hpa;
(Nitpicking according to personal taste):
It looks a bit weird that we start building 'eptp' from flags and add pointer as the last step, I'd suggest we re-write the function as:
u64 construct_eptp(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level)
{
u64 eptp = root_hpa | VMX_EPTP_MT_WB;
eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
if (enable_ept_ad_bits &&
(!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
eptp |= VMX_EPTP_AD_ENABLE_BIT;
return eptp;}
Quoted 34 lines
arch/x86/kvm/vmx/vmx.c +4
-4return eptp; } -static void vmx_load_mmu_pgd (struct kvm_vcpu *vcpu,unsigned long pgd, -int pgd_level) +static void vmx_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa, +int root_level) {struct kvm *kvm = vcpu->kvm;bool update_guest_cr3 = true;@@ -3067,7 +3066,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,u64 eptp;if (enable_ept) { - eptp =construct_eptp (vcpu, pgd, pgd_level); + eptp =construct_eptp (vcpu, root_hpa, root_level);vmcs_write64 (EPT_POINTER , eptp);if (kvm_x86_ops.tlb_remote_flush) {@@ -3086,7 +3085,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd, update_guest_cr3 = false;vmx_ept_load_pdptrs (vcpu); }else { - guest_cr3 = pgd; + guest_cr3 = root_hpa |kvm_get_active_pcid (vcpu); }if (update_guest_cr3)arch/x86/kvm/vmx/vmx.h +0
-0
arch/x86/kvm/vmx/vmx.h void set_cr4_guest_host_mask(struct vcpu_vmx *vmx); · line 332
Quoted 9 lines
arch/x86/kvm/vmx/vmx.h +1
-2@@ -326,8 +326,7 @@ void set_cr4_guest_host_mask(struct vcpu_vmx *vmx);void ept_save_pdptrs (struct kvm_vcpu *vcpu);void vmx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);void vmx_set_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg); -u64 construct_eptp (struct kvm_vcpu *vcpu,unsigned long root_hpa, -int root_level); +u64 construct_eptp (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level);void update_exception_bitmap (struct kvm_vcpu *vcpu);void vmx_update_msr_bitmap (struct kvm_vcpu *vcpu);
Signature · 1 line
Vitaly
Paolo Bonzini
maintainerRe: [PATCH v3 01/11] KVM: x86: Get active PCID only when writing a CR3 value
In reply to Sean Christophersonarch/x86/kvm/svm/svm.c static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) · line 3678
Quoted 11 lines · Sean Christopherson
arch/x86/kvm/svm/svm.c +2
-1+static void svm_load_mmu_pgd (struct kvm_vcpu *vcpu,hpa_t root_hpa,int root_level) {struct vcpu_svm *svm =to_svm (vcpu);unsigned long cr3; - cr3 =__sme_set (root); + cr3 =__sme_set (root_hpa) |kvm_get_active_pcid (vcpu);if (npt_enabled) { svm->vmcb->control.nested_cr3 = cr3;
SVM uses the name "nested CR3" so this variable actually could represent an NPT value that does not need the PCID.
Therefore, this change must be done in an else branch, which I've done on applying the patch.
Paolo
PATCH v3 2/11 KVM: VMX: Track common EPTP for Hyper-V's paravirt TLB flush
In reply to Sean ChristophersonExplicitly track the EPTP that is common to all vCPUs instead of grabbing vCPU0's EPTP when invoking Hyper-V's paravirt TLB flush. Tracking the EPTP will allow optimizing the checks when loading a new EPTP and will also allow dropping ept_pointer_match, e.g. by marking the common EPTP as invalid.
This also technically fixes a bug where KVM could theoretically flush an invalid GPA if all vCPUs have an invalid root. In practice, it's likely impossible to trigger a remote TLB flush in such a scenario. In any case, the superfluous flush is completely benign.
--- arch/x86/kvm/vmx/vmx.c | 20 +++++++++----------- arch/x86/kvm/vmx/vmx.h | 1 + 2 files changed, 10 insertions(+), 11 deletions(-)
arch/x86/kvm/vmx/vmx.c +9 -11
@@ -483,12 +483,14 @@ static void check_ept_pointer_match(struct kvm *kvm)if (!VALID_PAGE (tmp_eptp)) { tmp_eptp =to_vmx (vcpu)->ept_pointer; }else if (tmp_eptp !=to_vmx (vcpu)->ept_pointer) { +to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ;to_kvm_vmx (kvm)->ept_pointers_match =EPT_POINTERS_MISMATCH ;return ; } } +to_kvm_vmx (kvm)->hv_tlb_eptp = tmp_eptp;to_kvm_vmx (kvm)->ept_pointers_match =EPT_POINTERS_MATCH ; }
@@ -501,21 +503,18 @@ static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush range->pages); } -static inline int __hv_remote_flush_tlb_with_range (struct kvm *kvm, -struct kvm_vcpu *vcpu,struct kvm_tlb_range *range) +static inline int hv_remote_flush_eptp (u64 eptp,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 , +return hyperv_flush_guest_mapping_range (eptp &PAGE_MASK , kvm_fill_hv_flush_list_func, (void *)range);else -return hyperv_flush_guest_mapping (ept_pointer &PAGE_MASK ); +return hyperv_flush_guest_mapping (eptp &PAGE_MASK ); } static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
@@ -533,12 +532,11 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm,kvm_for_each_vcpu (i, vcpu, kvm) {/* 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); + ret |=hv_remote_flush_eptp (to_vmx (vcpu)->ept_pointer, + range); } - }else { - ret =__hv_remote_flush_tlb_with_range (kvm, -kvm_get_vcpu (kvm,0 ), range); + }else if (VALID_PAGE (to_kvm_vmx (kvm)->hv_tlb_eptp)) { + ret =hv_remote_flush_eptp (to_kvm_vmx (kvm)->hv_tlb_eptp, range); }spin_unlock (&to_kvm_vmx (kvm)->ept_pointer_lock);
arch/x86/kvm/vmx/vmx.h +1 -0
@@ -301,6 +301,7 @@ struct kvm_vmx {bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +hpa_t hv_tlb_eptp;enum ept_pointers_status ept_pointers_match;spinlock_t ept_pointer_lock; };
Signature · 1 line
2.28.0
Re: [PATCH v3 02/11] KVM: VMX: Track common EPTP for Hyper-V's paravirt TLB flush
Quoted 83 lines · Sean Christopherson
Explicitly track the EPTP that is common to all vCPUs instead of grabbing vCPU0's EPTP when invoking Hyper-V's paravirt TLB flush. Tracking the EPTP will allow optimizing the checks when loading a new EPTP and will also allow dropping
ept_pointer_match, e.g. by marking the common EPTP as invalid.This also technically fixes a bug where KVM could theoretically flush an invalid GPA if all vCPUs have an invalid root. In practice, it's likely impossible to trigger a remote TLB flush in such a scenario. In any case, the superfluous flush is completely benign.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 20 +++++++++----------- arch/x86/kvm/vmx/vmx.h | 1 + 2 files changed, 10 insertions(+), 11 deletions(-)arch/x86/kvm/vmx/vmx.c +9
-11@@ -483,12 +483,14 @@ static void check_ept_pointer_match(struct kvm *kvm)if (!VALID_PAGE (tmp_eptp)) { tmp_eptp =to_vmx (vcpu)->ept_pointer; }else if (tmp_eptp !=to_vmx (vcpu)->ept_pointer) { +to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ;to_kvm_vmx (kvm)->ept_pointers_match =EPT_POINTERS_MISMATCH ;return ; } } +to_kvm_vmx (kvm)->hv_tlb_eptp = tmp_eptp;to_kvm_vmx (kvm)->ept_pointers_match =EPT_POINTERS_MATCH ; }@@ -501,21 +503,18 @@ static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush range->pages); } -static inline int __hv_remote_flush_tlb_with_range (struct kvm *kvm, -struct kvm_vcpu *vcpu,struct kvm_tlb_range *range) +static inline int hv_remote_flush_eptp (u64 eptp,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 , +return hyperv_flush_guest_mapping_range (eptp &PAGE_MASK , kvm_fill_hv_flush_list_func, (void *)range);else -return hyperv_flush_guest_mapping (ept_pointer &PAGE_MASK ); +return hyperv_flush_guest_mapping (eptp &PAGE_MASK ); } static int hv_remote_flush_tlb_with_range(struct kvm *kvm,@@ -533,12 +532,11 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm,kvm_for_each_vcpu (i, vcpu, kvm) {/* 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); + ret |=hv_remote_flush_eptp (to_vmx (vcpu)->ept_pointer, + range); } - }else { - ret =__hv_remote_flush_tlb_with_range (kvm, -kvm_get_vcpu (kvm,0 ), range); + }else if (VALID_PAGE (to_kvm_vmx (kvm)->hv_tlb_eptp)) { + ret =hv_remote_flush_eptp (to_kvm_vmx (kvm)->hv_tlb_eptp, range); }spin_unlock (&to_kvm_vmx (kvm)->ept_pointer_lock);arch/x86/kvm/vmx/vmx.h +0
-0
arch/x86/kvm/vmx/vmx.h struct kvm_vmx { · line 307
Quoted 7 lines
arch/x86/kvm/vmx/vmx.h +1
-0@@ -301,6 +301,7 @@ struct kvm_vmx {bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +hpa_t hv_tlb_eptp;enum ept_pointers_status ept_pointers_match;spinlock_t ept_pointer_lock; };
Signature · 1 line
Vitaly
PATCH v3 3/11 KVM: VMX: Stash kvm_vmx in a local variable for Hyper-V paravirt TLB flush
In reply to Sean ChristophersonCapture kvm_vmx in a local variable instead of polluting hv_remote_flush_tlb_with_range() with to_kvm_vmx(kvm).
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/vmx.c +7 -6
@@ -520,26 +520,27 @@ static inline int hv_remote_flush_eptp(u64 eptp, struct kvm_tlb_range *range)static int hv_remote_flush_tlb_with_range (struct kvm *kvm,struct kvm_tlb_range *range) { +struct kvm_vmx *kvm_vmx =to_kvm_vmx (kvm);struct kvm_vcpu *vcpu;int ret =0 , i; -spin_lock (&to_kvm_vmx (kvm)->ept_pointer_lock); +spin_lock (&kvm_vmx->ept_pointer_lock); -if (to_kvm_vmx (kvm)->ept_pointers_match ==EPT_POINTERS_CHECK ) +if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_CHECK )check_ept_pointer_match (kvm); -if (to_kvm_vmx (kvm)->ept_pointers_match !=EPT_POINTERS_MATCH ) { +if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) {kvm_for_each_vcpu (i, vcpu, kvm) {/* If ept_pointer is invalid pointer, bypass flush request. */ if (VALID_PAGE (to_vmx (vcpu)->ept_pointer)) ret |=hv_remote_flush_eptp (to_vmx (vcpu)->ept_pointer, range); } - }else if (VALID_PAGE (to_kvm_vmx (kvm)->hv_tlb_eptp)) { - ret =hv_remote_flush_eptp (to_kvm_vmx (kvm)->hv_tlb_eptp, range); + }else if (VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { + ret =hv_remote_flush_eptp (kvm_vmx->hv_tlb_eptp, range); } -spin_unlock (&to_kvm_vmx (kvm)->ept_pointer_lock); +spin_unlock (&kvm_vmx->ept_pointer_lock);return ret; }static int hv_remote_flush_tlb (struct kvm *kvm)
Signature · 1 line
2.28.0
PATCH v3 4/11 KVM: VMX: Fold Hyper-V EPTP checking into it's only caller
In reply to Sean ChristophersonFold check_ept_pointer_match() into hv_remote_flush_tlb_with_range() in preparation for combining the kvm_for_each_vcpu loops of the ==CHECK and !=MATCH statements.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 44 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 24 deletions(-)
arch/x86/kvm/vmx/vmx.c +20 -24
@@ -472,28 +472,6 @@ static const u32 vmx_uret_msrs_list[] = {static bool __read_mostly enlightened_vmcs = true;module_param (enlightened_vmcs, bool,0444 ); -/* check_ept_pointer() should be under protection of ept_pointer_lock. */ -static void check_ept_pointer_match (struct kvm *kvm) -{ -struct kvm_vcpu *vcpu; -u64 tmp_eptp =INVALID_PAGE ; -int i; - -kvm_for_each_vcpu (i, vcpu, kvm) { -if (!VALID_PAGE (tmp_eptp)) { - tmp_eptp =to_vmx (vcpu)->ept_pointer; - }else if (tmp_eptp !=to_vmx (vcpu)->ept_pointer) { -to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ; -to_kvm_vmx (kvm)->ept_pointers_match - =EPT_POINTERS_MISMATCH ; -return ; - } - } - -to_kvm_vmx (kvm)->hv_tlb_eptp = tmp_eptp; -to_kvm_vmx (kvm)->ept_pointers_match =EPT_POINTERS_MATCH ; -} -static int kvm_fill_hv_flush_list_func (struct hv_guest_mapping_flush_list *flush,void *data) {
@@ -523,11 +501,29 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm,struct kvm_vmx *kvm_vmx =to_kvm_vmx (kvm);struct kvm_vcpu *vcpu;int ret =0 , i; +u64 tmp_eptp;spin_lock (&kvm_vmx->ept_pointer_lock); -if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_CHECK ) -check_ept_pointer_match (kvm); +if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_CHECK ) { + kvm_vmx->ept_pointers_match =EPT_POINTERS_MATCH ; + kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; + +kvm_for_each_vcpu (i, vcpu, kvm) { + tmp_eptp =to_vmx (vcpu)->ept_pointer; +if (!VALID_PAGE (tmp_eptp)) +continue ; + +if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { + kvm_vmx->hv_tlb_eptp = tmp_eptp; + }else if (kvm_vmx->hv_tlb_eptp != tmp_eptp) { + kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; + kvm_vmx->ept_pointers_match + =EPT_POINTERS_MISMATCH ; +break ; + } + } + }if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) {kvm_for_each_vcpu (i, vcpu, kvm) {
Signature · 1 line
2.28.0
Re: [PATCH v3 04/11] KVM: VMX: Fold Hyper-V EPTP checking into it's only caller
Quoted 66 lines · Sean Christopherson
Fold
check_ept_pointer_match()intohv_remote_flush_tlb_with_range()in preparation for combining thekvm_for_each_vcpuloops of the ==CHECK and !=MATCH statements.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 44 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 24 deletions(-)arch/x86/kvm/vmx/vmx.c +13
-24@@ -472,28 +472,6 @@ static const u32 vmx_uret_msrs_list[] = {static bool __read_mostly enlightened_vmcs = true;module_param (enlightened_vmcs, bool,0444 ); -/* check_ept_pointer() should be under protection of ept_pointer_lock. */ -static void check_ept_pointer_match (struct kvm *kvm) -{ -struct kvm_vcpu *vcpu; -u64 tmp_eptp =INVALID_PAGE ; -int i; - -kvm_for_each_vcpu (i, vcpu, kvm) { -if (!VALID_PAGE (tmp_eptp)) { - tmp_eptp =to_vmx (vcpu)->ept_pointer; - }else if (tmp_eptp !=to_vmx (vcpu)->ept_pointer) { -to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ; -to_kvm_vmx (kvm)->ept_pointers_match - =EPT_POINTERS_MISMATCH ; -return ; - } - } - -to_kvm_vmx (kvm)->hv_tlb_eptp = tmp_eptp; -to_kvm_vmx (kvm)->ept_pointers_match =EPT_POINTERS_MATCH ; -} -static int kvm_fill_hv_flush_list_func (struct hv_guest_mapping_flush_list *flush,void *data) {@@ -523,11 +501,29 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm,struct kvm_vmx *kvm_vmx =to_kvm_vmx (kvm);struct kvm_vcpu *vcpu;int ret =0 , i; +u64 tmp_eptp;spin_lock (&kvm_vmx->ept_pointer_lock); -if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_CHECK ) -check_ept_pointer_match (kvm); +if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_CHECK ) { + kvm_vmx->ept_pointers_match =EPT_POINTERS_MATCH ; + kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; + +kvm_for_each_vcpu (i, vcpu, kvm) { + tmp_eptp =to_vmx (vcpu)->ept_pointer; +if (!VALID_PAGE (tmp_eptp)) +continue ; + +if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { + kvm_vmx->hv_tlb_eptp = tmp_eptp; + }else if (kvm_vmx->hv_tlb_eptp != tmp_eptp) {
arch/x86/kvm/vmx/vmx.c static int hv_remote_flush_tlb_with_range(struct kvm *kvm, · line 529
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +7
-0+ kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; + kvm_vmx->ept_pointers_match + =EPT_POINTERS_MISMATCH ; +break ; + } + } + }if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) {kvm_for_each_vcpu (i, vcpu, kvm) {
Signature · 1 line
Vitaly
PATCH v3 5/11 KVM: VMX: Do Hyper-V TLB flush iff vCPU's EPTP hasn't been flushed
In reply to Sean ChristophersonCombine the for-loops for Hyper-V TLB EPTP checking and flushing, and in doing so skip flushes for vCPUs whose EPTP matches the target EPTP.
--- arch/x86/kvm/vmx/vmx.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-)
arch/x86/kvm/vmx/vmx.c +8 -15
@@ -505,33 +505,26 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm,spin_lock (&kvm_vmx->ept_pointer_lock); -if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_CHECK ) { +if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) { kvm_vmx->ept_pointers_match =EPT_POINTERS_MATCH ; kvm_vmx->hv_tlb_eptp =INVALID_PAGE ;kvm_for_each_vcpu (i, vcpu, kvm) { tmp_eptp =to_vmx (vcpu)->ept_pointer; -if (!VALID_PAGE (tmp_eptp)) +if (!VALID_PAGE (tmp_eptp) || + tmp_eptp == kvm_vmx->hv_tlb_eptp)continue ; -if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { +if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) kvm_vmx->hv_tlb_eptp = tmp_eptp; - }else if (kvm_vmx->hv_tlb_eptp != tmp_eptp) { - kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; +else kvm_vmx->ept_pointers_match =EPT_POINTERS_MISMATCH ; -break ; - } - } - } -if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) { -kvm_for_each_vcpu (i, vcpu, kvm) { -/* If ept_pointer is invalid pointer, bypass flush request. */ -if (VALID_PAGE (to_vmx (vcpu)->ept_pointer)) - ret |=hv_remote_flush_eptp (to_vmx (vcpu)->ept_pointer, - range); + ret |=hv_remote_flush_eptp (tmp_eptp, range); } +if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_MISMATCH ) + kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; }else if (VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { ret =hv_remote_flush_eptp (kvm_vmx->hv_tlb_eptp, range); }
Signature · 1 line
2.28.0
Re: [PATCH v3 05/11] KVM: VMX: Do Hyper-V TLB flush iff vCPU's EPTP hasn't been flushed
Quoted 44 lines · Sean Christopherson
Combine the for-loops for Hyper-V TLB EPTP checking and flushing, and in doing so skip flushes for vCPUs whose EPTP matches the target EPTP.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-)arch/x86/kvm/vmx/vmx.c +5
-12@@ -505,33 +505,26 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm,spin_lock (&kvm_vmx->ept_pointer_lock); -if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_CHECK ) { +if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) { kvm_vmx->ept_pointers_match =EPT_POINTERS_MATCH ; kvm_vmx->hv_tlb_eptp =INVALID_PAGE ;kvm_for_each_vcpu (i, vcpu, kvm) { tmp_eptp =to_vmx (vcpu)->ept_pointer; -if (!VALID_PAGE (tmp_eptp)) +if (!VALID_PAGE (tmp_eptp) || + tmp_eptp == kvm_vmx->hv_tlb_eptp)continue ; -if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { +if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) kvm_vmx->hv_tlb_eptp = tmp_eptp; - }else if (kvm_vmx->hv_tlb_eptp != tmp_eptp) { - kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; +else kvm_vmx->ept_pointers_match =EPT_POINTERS_MISMATCH ; -break ; - } - } - } -if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) { -kvm_for_each_vcpu (i, vcpu, kvm) { -/* If ept_pointer is invalid pointer, bypass flush request. */
arch/x86/kvm/vmx/vmx.c static int hv_remote_flush_tlb_with_range(struct kvm *kvm, · line 530
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +3
-3-if (VALID_PAGE (to_vmx (vcpu)->ept_pointer)) - ret |=hv_remote_flush_eptp (to_vmx (vcpu)->ept_pointer, - range); + ret |=hv_remote_flush_eptp (tmp_eptp, range); } +if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_MISMATCH ) + kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; }else if (VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { ret =hv_remote_flush_eptp (kvm_vmx->hv_tlb_eptp, range); }
It seems this patch makes EPT_POINTERS_MISMATCH an alias for EPT_POINTERS_CHECK but this all is gone in the next patch, so
Signature · 1 line
Vitaly
PATCH v3 6/11 KVM: VMX: Invalidate hv_tlb_eptp to denote an EPTP mismatch
In reply to Sean ChristophersonDrop the dedicated 'ept_pointers_match' field in favor of stuffing 'hv_tlb_eptp' with INVALID_PAGE to mark it as invalid, i.e. to denote that there is at least one EPTP mismatch. Use a local variable to track whether or not a mismatch is detected so that hv_tlb_eptp can be used to skip redundant flushes.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 35 +++++++++++++++++++++++------------ arch/x86/kvm/vmx/vmx.h | 7 ------- 2 files changed, 23 insertions(+), 19 deletions(-)
arch/x86/kvm/vmx/vmx.c +23 -12
@@ -500,32 +500,44 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm, {struct kvm_vmx *kvm_vmx =to_kvm_vmx (kvm);struct kvm_vcpu *vcpu; -int ret =0 , i; +int ret =0 , i, nr_unique_valid_eptps;u64 tmp_eptp;spin_lock (&kvm_vmx->ept_pointer_lock); -if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) { - kvm_vmx->ept_pointers_match =EPT_POINTERS_MATCH ; - kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; +if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { + nr_unique_valid_eptps =0 ; +/* +* Flush all valid EPTPs, and see if all vCPUs have converged +* on a common EPTP, in which case future flushes can skip the +* loop and flush the common EPTP. +*/ kvm_for_each_vcpu (i, vcpu, kvm) { tmp_eptp =to_vmx (vcpu)->ept_pointer;if (!VALID_PAGE (tmp_eptp) || tmp_eptp == kvm_vmx->hv_tlb_eptp)continue ; -if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) +/* +* Set the tracked EPTP to the first valid EPTP. Keep +* this EPTP for the entirety of the loop even if more +* EPTPs are encountered as a low effort optimization +* to avoid flushing the same (first) EPTP again. +*/ +if (++nr_unique_valid_eptps ==1 ) kvm_vmx->hv_tlb_eptp = tmp_eptp; -else - kvm_vmx->ept_pointers_match - =EPT_POINTERS_MISMATCH ; ret |=hv_remote_flush_eptp (tmp_eptp, range); } -if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_MISMATCH ) + +/* +* The optimized flush of a single EPTP can't be used if there +* are multiple valid EPTPs (obviously). +*/ +if (nr_unique_valid_eptps >1 ) kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; - }else if (VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { + }else { ret =hv_remote_flush_eptp (kvm_vmx->hv_tlb_eptp, range); }
@@ -3060,8 +3072,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,if (kvm_x86_ops.tlb_remote_flush) {spin_lock (&to_kvm_vmx (kvm)->ept_pointer_lock);to_vmx (vcpu)->ept_pointer = eptp; -to_kvm_vmx (kvm)->ept_pointers_match - =EPT_POINTERS_CHECK ; +to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ;spin_unlock (&to_kvm_vmx (kvm)->ept_pointer_lock); }
arch/x86/kvm/vmx/vmx.h +0 -7
@@ -288,12 +288,6 @@ struct vcpu_vmx { } shadow_msr_intercept; }; -enum ept_pointers_status { -EPT_POINTERS_CHECK =0 , -EPT_POINTERS_MATCH =1 , -EPT_POINTERS_MISMATCH =2 -}; -struct kvm_vmx {struct kvm kvm;
@@ -302,7 +296,6 @@ struct kvm_vmx {gpa_t ept_identity_map_addr;hpa_t hv_tlb_eptp; -enum ept_pointers_status ept_pointers_match;spinlock_t ept_pointer_lock; };
Signature · 1 line
2.28.0
Re: [PATCH v3 06/11] KVM: VMX: Invalidate hv_tlb_eptp to denote an EPTP mismatch
Quoted 101 lines · Sean Christopherson
Drop the dedicated '
ept_pointers_match' field in favor of stuffing 'hv_tlb_eptp' withINVALID_PAGEto mark it as invalid, i.e. to denote that there is at least one EPTP mismatch. Use a local variable to track whether or not a mismatch is detected so thathv_tlb_eptpcan be used to skip redundant flushes.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 35 +++++++++++++++++++++++------------ arch/x86/kvm/vmx/vmx.h | 7 ------- 2 files changed, 23 insertions(+), 19 deletions(-)arch/x86/kvm/vmx/vmx.c +23
-12@@ -500,32 +500,44 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm, {struct kvm_vmx *kvm_vmx =to_kvm_vmx (kvm);struct kvm_vcpu *vcpu; -int ret =0 , i; +int ret =0 , i, nr_unique_valid_eptps;u64 tmp_eptp;spin_lock (&kvm_vmx->ept_pointer_lock); -if (kvm_vmx->ept_pointers_match !=EPT_POINTERS_MATCH ) { - kvm_vmx->ept_pointers_match =EPT_POINTERS_MATCH ; - kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; +if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { + nr_unique_valid_eptps =0 ; +/* +* Flush all valid EPTPs, and see if all vCPUs have converged +* on a common EPTP, in which case future flushes can skip the +* loop and flush the common EPTP. +*/ kvm_for_each_vcpu (i, vcpu, kvm) { tmp_eptp =to_vmx (vcpu)->ept_pointer;if (!VALID_PAGE (tmp_eptp) || tmp_eptp == kvm_vmx->hv_tlb_eptp)continue ; -if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) +/* +* Set the tracked EPTP to the first valid EPTP. Keep +* this EPTP for the entirety of the loop even if more +* EPTPs are encountered as a low effort optimization +* to avoid flushing the same (first) EPTP again. +*/ +if (++nr_unique_valid_eptps ==1 ) kvm_vmx->hv_tlb_eptp = tmp_eptp; -else - kvm_vmx->ept_pointers_match - =EPT_POINTERS_MISMATCH ; ret |=hv_remote_flush_eptp (tmp_eptp, range); } -if (kvm_vmx->ept_pointers_match ==EPT_POINTERS_MISMATCH ) + +/* +* The optimized flush of a single EPTP can't be used if there +* are multiple valid EPTPs (obviously). +*/ +if (nr_unique_valid_eptps >1 ) kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; - }else if (VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { + }else { ret =hv_remote_flush_eptp (kvm_vmx->hv_tlb_eptp, range); }@@ -3060,8 +3072,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,if (kvm_x86_ops.tlb_remote_flush) {spin_lock (&to_kvm_vmx (kvm)->ept_pointer_lock);to_vmx (vcpu)->ept_pointer = eptp; -to_kvm_vmx (kvm)->ept_pointers_match - =EPT_POINTERS_CHECK ; +to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ;spin_unlock (&to_kvm_vmx (kvm)->ept_pointer_lock); }arch/x86/kvm/vmx/vmx.h +0
-6@@ -288,12 +288,6 @@ struct vcpu_vmx { } shadow_msr_intercept; }; -enum ept_pointers_status { -EPT_POINTERS_CHECK =0 , -EPT_POINTERS_MATCH =1 , -EPT_POINTERS_MISMATCH =2 -}; -struct kvm_vmx {struct kvm kvm;
arch/x86/kvm/vmx/vmx.h struct kvm_vmx { · line 300
Quoted 6 lines
arch/x86/kvm/vmx/vmx.h +0
-1@@ -302,7 +296,6 @@ struct kvm_vmx {gpa_t ept_identity_map_addr;hpa_t hv_tlb_eptp; -enum ept_pointers_status ept_pointers_match;spinlock_t ept_pointer_lock; };
This looks really neat and straighforward now, thanks!
Signature · 1 line
Vitaly
PATCH v3 7/11 KVM: VMX: Don't invalidate hv_tlb_eptp if the new EPTP matches
In reply to Sean ChristophersonDon't invalidate the common EPTP, and thus trigger rechecking of EPTPs across all vCPUs, if the new EPTP matches the old/common EPTP. In all likelihood this is a meaningless optimization, but there are (uncommon) scenarios where KVM can reload the same EPTP.
--- arch/x86/kvm/vmx/vmx.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +2 -1
@@ -3072,7 +3072,8 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,if (kvm_x86_ops.tlb_remote_flush) {spin_lock (&to_kvm_vmx (kvm)->ept_pointer_lock);to_vmx (vcpu)->ept_pointer = eptp; -to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ; +if (eptp !=to_kvm_vmx (kvm)->hv_tlb_eptp) +to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ;spin_unlock (&to_kvm_vmx (kvm)->ept_pointer_lock); }
Signature · 1 line
2.28.0
PATCH v3 8/11 KVM: VMX: Explicitly check for hv_remote_flush_tlb when loading pgd
In reply to Sean ChristophersonExplicitly check that kvm_x86_ops.tlb_remote_flush() points at Hyper-V's implementation for PV flushing instead of assuming that a non-NULL implementation means running on Hyper-V. Wrap the related logic in ifdeffery as hv_remote_flush_tlb() is defined iff CONFIG_HYPERV!=n.
Short term, the explicit check makes it more obvious why a non-NULL tlb_remote_flush() triggers EPTP shenanigans. Long term, this will allow TDX to define its own implementation of tlb_remote_flush() without running afoul of Hyper-V.
--- arch/x86/kvm/vmx/vmx.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/vmx.c +16 -7
@@ -576,6 +576,21 @@ static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)#endif /* IS_ENABLED(CONFIG_HYPERV) */ +static void hv_load_mmu_eptp (struct kvm_vcpu *vcpu,u64 eptp) +{ +#if IS_ENABLED (CONFIG_HYPERV ) +struct kvm_vmx *kvm_vmx =to_kvm_vmx (vcpu->kvm); + +if (kvm_x86_ops.tlb_remote_flush == hv_remote_flush_tlb) { +spin_lock (&kvm_vmx->ept_pointer_lock); +to_vmx (vcpu)->ept_pointer = eptp; +if (eptp != kvm_vmx->hv_tlb_eptp) + kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; +spin_unlock (&kvm_vmx->ept_pointer_lock); + } +#endif +} +/* * Comment's format: document - errata name - stepping - processor name. * Refer from
@@ -3069,13 +3084,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, eptp =construct_eptp (vcpu, root_hpa, root_level);vmcs_write64 (EPT_POINTER , eptp); -if (kvm_x86_ops.tlb_remote_flush) { -spin_lock (&to_kvm_vmx (kvm)->ept_pointer_lock); -to_vmx (vcpu)->ept_pointer = eptp; -if (eptp !=to_kvm_vmx (kvm)->hv_tlb_eptp) -to_kvm_vmx (kvm)->hv_tlb_eptp =INVALID_PAGE ; -spin_unlock (&to_kvm_vmx (kvm)->ept_pointer_lock); - } +hv_load_mmu_eptp (vcpu, eptp);if (!enable_unrestricted_guest && !is_paging (vcpu)) guest_cr3 =to_kvm_vmx (kvm)->ept_identity_map_addr;
Signature · 1 line
2.28.0
PATCH v3 9/11 KVM: VMX: Define Hyper-V paravirt TLB flush fields iff Hyper-V is enabled
In reply to Sean ChristophersonIfdef away the Hyper-V specific fields in structs kvm_vmx and vcpu_vmx as each field has only a single reference outside of the struct itself that isn't already wrapped in ifdeffery (and both are initialization).
vcpu_vmx.ept_pointer in particular should be wrapped as it is valid if and only if Hyper-v is active, i.e. non-Hyper-V code cannot rely on it to actually track the current EPTP (without additional code changes).
--- arch/x86/kvm/vmx/vmx.c | 5 ++++- arch/x86/kvm/vmx/vmx.h | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +4 -1
@@ -6955,8 +6955,9 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) vmx->pi_desc.nv =POSTED_INTR_VECTOR ; vmx->pi_desc.sn =1 ; +#if IS_ENABLED (CONFIG_HYPERV ) vmx->ept_pointer =INVALID_PAGE ; - +#endif return 0 ; free_vmcs:
@@ -6973,7 +6974,9 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu)static int vmx_vm_init (struct kvm *kvm) { +#if IS_ENABLED (CONFIG_HYPERV )spin_lock_init (&to_kvm_vmx (kvm)->ept_pointer_lock); +#endif if (!ple_gap) kvm->arch.pause_in_guest = true;
arch/x86/kvm/vmx/vmx.h +4 -0
@@ -276,7 +276,9 @@ struct vcpu_vmx {*/ u64 msr_ia32_feature_control;u64 msr_ia32_feature_control_valid_bits; +#if IS_ENABLED (CONFIG_HYPERV )u64 ept_pointer; +#endif struct pt_desc pt_desc;
@@ -295,8 +297,10 @@ struct kvm_vmx {bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +#if IS_ENABLED (CONFIG_HYPERV )hpa_t hv_tlb_eptp;spinlock_t ept_pointer_lock; +#endif };bool nested_vmx_allowed (struct kvm_vcpu *vcpu);
Signature · 1 line
2.28.0
Re: [PATCH v3 09/11] KVM: VMX: Define Hyper-V paravirt TLB flush fields iff Hyper-V is enabled
Quoted 54 lines · Sean Christopherson
Ifdef away the Hyper-V specific fields in structs
kvm_vmxandvcpu_vmxas each field has only a single reference outside of the struct itself that isn't already wrapped in ifdeffery (and both are initialization).vcpu_vmx.ept_pointer in particular should be wrapped as it is valid if and only if Hyper-v is active, i.e. non-Hyper-V code cannot rely on it to actually track the current EPTP (without additional code changes).
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 5 ++++- arch/x86/kvm/vmx/vmx.h | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/vmx.c +4
-1@@ -6955,8 +6955,9 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) vmx->pi_desc.nv =POSTED_INTR_VECTOR ; vmx->pi_desc.sn =1 ; +#if IS_ENABLED (CONFIG_HYPERV ) vmx->ept_pointer =INVALID_PAGE ; - +#endif return 0 ; free_vmcs:@@ -6973,7 +6974,9 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu)static int vmx_vm_init (struct kvm *kvm) { +#if IS_ENABLED (CONFIG_HYPERV )spin_lock_init (&to_kvm_vmx (kvm)->ept_pointer_lock); +#endif if (!ple_gap) kvm->arch.pause_in_guest = true;arch/x86/kvm/vmx/vmx.h +2
-0@@ -276,7 +276,9 @@ struct vcpu_vmx {*/ u64 msr_ia32_feature_control;u64 msr_ia32_feature_control_valid_bits; +#if IS_ENABLED (CONFIG_HYPERV )u64 ept_pointer; +#endif struct pt_desc pt_desc;
arch/x86/kvm/vmx/vmx.h struct kvm_vmx { · line 306
Quoted 10 lines
arch/x86/kvm/vmx/vmx.h +2
-0@@ -295,8 +297,10 @@ struct kvm_vmx {bool ept_identity_pagetable_done;gpa_t ept_identity_map_addr; +#if IS_ENABLED (CONFIG_HYPERV )hpa_t hv_tlb_eptp;spinlock_t ept_pointer_lock; +#endif };bool nested_vmx_allowed (struct kvm_vcpu *vcpu);
Signature · 1 line
Vitaly
PATCH v3 10/11 KVM: VMX: Skip additional Hyper-V TLB EPTP flushes if one fails
In reply to Sean ChristophersonSkip additional EPTP flushes if one fails when processing EPTPs for Hyper-V's paravirt TLB flushing. If _any_ flush fails, KVM falls back to a full global flush, i.e. additional flushes are unnecessary (and will likely fail anyways).
Continue processing the loop unless a mismatch was already detected, e.g. to handle the case where the first flush fails and there is a yet-to-be-detected mismatch.
--- arch/x86/kvm/vmx/vmx.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +9 -1
@@ -528,7 +528,15 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm,if (++nr_unique_valid_eptps ==1 ) kvm_vmx->hv_tlb_eptp = tmp_eptp; - ret |=hv_remote_flush_eptp (tmp_eptp, range); +if (!ret) + ret =hv_remote_flush_eptp (tmp_eptp, range); + +/* +* Stop processing EPTPs if a failure occurred and +* there is already a detected EPTP mismatch. +*/ +if (ret && nr_unique_valid_eptps >1 ) +break ; }/*
Signature · 1 line
2.28.0
Re: [PATCH v3 10/11] KVM: VMX: Skip additional Hyper-V TLB EPTP flushes if one fails
Quoted 25 lines · Sean Christopherson
Skip additional EPTP flushes if one fails when processing EPTPs for Hyper-V's paravirt TLB flushing. If _any_ flush fails, KVM falls back to a full global flush, i.e. additional flushes are unnecessary (and will likely fail anyways).
Continue processing the loop unless a mismatch was already detected, e.g. to handle the case where the first flush fails and there is a yet-to-be-detected mismatch.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/vmx.c +2
-1@@ -528,7 +528,15 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm,if (++nr_unique_valid_eptps ==1 ) kvm_vmx->hv_tlb_eptp = tmp_eptp; - ret |=hv_remote_flush_eptp (tmp_eptp, range); +if (!ret) + ret =hv_remote_flush_eptp (tmp_eptp, range);
arch/x86/kvm/vmx/vmx.c static int hv_remote_flush_tlb_with_range(struct kvm *kvm, · line 542
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +7
-0+ +/* +* Stop processing EPTPs if a failure occurred and +* there is already a detected EPTP mismatch. +*/ +if (ret && nr_unique_valid_eptps >1 ) +break ; }/*
This should never happen (famous last words) but why not optimize the impossibility :-)
Signature · 1 line
Vitaly
PATCH v3 11/11 KVM: VMX: Track root HPA instead of EPTP for paravirt Hyper-V TLB flush
In reply to Sean ChristophersonTrack the address of the top-level EPT struct, a.k.a. the root HPA, instead of the EPTP itself for Hyper-V's paravirt TLB flush. The paravirt API takes only the address, not the full EPTP, and in theory tracking the EPTP could lead to false negatives, e.g. if the HPA matched but the attributes in the EPTP do not. In practice, such a mismatch is extremely unlikely, if not flat out impossible, given how KVM generates the EPTP.
Opportunsitically rename the related fields to use the 'root' nomenclature, and to prefix them with 'hv_' to connect them to Hyper-V's paravirt TLB flushing.
--- arch/x86/kvm/vmx/vmx.c | 83 ++++++++++++++++++++---------------------- arch/x86/kvm/vmx/vmx.h | 6 +-- 2 files changed, 42 insertions(+), 47 deletions(-)
arch/x86/kvm/vmx/vmx.c +39 -44
@@ -481,18 +481,14 @@ static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush range->pages); } -static inline int hv_remote_flush_eptp (u64 eptp,struct kvm_tlb_range *range) +static inline int hv_remote_flush_root_ept (hpa_t root_ept, +struct kvm_tlb_range *range) { -/* -* 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 (eptp &PAGE_MASK , +return hyperv_flush_guest_mapping_range (root_ept, kvm_fill_hv_flush_list_func, (void *)range);else -return hyperv_flush_guest_mapping (eptp &PAGE_MASK ); +return hyperv_flush_guest_mapping (root_ept); } static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
@@ -500,56 +496,55 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm, {struct kvm_vmx *kvm_vmx =to_kvm_vmx (kvm);struct kvm_vcpu *vcpu; -int ret =0 , i, nr_unique_valid_eptps; -u64 tmp_eptp; +int ret =0 , i, nr_unique_valid_roots; +hpa_t root; -spin_lock (&kvm_vmx->ept_pointer_lock); +spin_lock (&kvm_vmx->hv_root_ept_lock); -if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { - nr_unique_valid_eptps =0 ; +if (!VALID_PAGE (kvm_vmx->hv_root_ept)) { + nr_unique_valid_roots =0 ;/* -* Flush all valid EPTPs, and see if all vCPUs have converged -* on a common EPTP, in which case future flushes can skip the -* loop and flush the common EPTP. +* Flush all valid roots, and see if all vCPUs have converged +* on a common root, in which case future flushes can skip the +* loop and flush the common root. */ kvm_for_each_vcpu (i, vcpu, kvm) { - tmp_eptp =to_vmx (vcpu)->ept_pointer; -if (!VALID_PAGE (tmp_eptp) || - tmp_eptp == kvm_vmx->hv_tlb_eptp) + root =to_vmx (vcpu)->hv_root_ept; +if (!VALID_PAGE (root) || root == kvm_vmx->hv_root_ept)continue ;/* -* Set the tracked EPTP to the first valid EPTP. Keep -* this EPTP for the entirety of the loop even if more -* EPTPs are encountered as a low effort optimization -* to avoid flushing the same (first) EPTP again. +* Set the tracked root to the first valid root. Keep +* this root for the entirety of the loop even if more +* roots are encountered as a low effort optimization +* to avoid flushing the same (first) root again. */ -if (++nr_unique_valid_eptps ==1 ) - kvm_vmx->hv_tlb_eptp = tmp_eptp; +if (++nr_unique_valid_roots ==1 ) + kvm_vmx->hv_root_ept = root;if (!ret) - ret =hv_remote_flush_eptp (tmp_eptp, range); + ret =hv_remote_flush_root_ept (root, range);/* -* Stop processing EPTPs if a failure occurred and -* there is already a detected EPTP mismatch. +* Stop processing roots if a failure occurred and +* multiple valid roots have already been detected. */ -if (ret && nr_unique_valid_eptps >1 ) +if (ret && nr_unique_valid_roots >1 )break ; }/* -* The optimized flush of a single EPTP can't be used if there -* are multiple valid EPTPs (obviously). +* The optimized flush of a single root can't be used if there +* are multiple valid roots (obviously). */ -if (nr_unique_valid_eptps >1 ) - kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; +if (nr_unique_valid_roots >1 ) + kvm_vmx->hv_root_ept =INVALID_PAGE ; }else { - ret =hv_remote_flush_eptp (kvm_vmx->hv_tlb_eptp, range); + ret =hv_remote_flush_root_ept (kvm_vmx->hv_root_ept, range); } -spin_unlock (&kvm_vmx->ept_pointer_lock); +spin_unlock (&kvm_vmx->hv_root_ept_lock);return ret; }static int hv_remote_flush_tlb (struct kvm *kvm)
@@ -584,17 +579,17 @@ static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)#endif /* IS_ENABLED(CONFIG_HYPERV) */ -static void hv_load_mmu_eptp (struct kvm_vcpu *vcpu,u64 eptp) +static void hv_track_root_ept (struct kvm_vcpu *vcpu,hpa_t root_ept) {#if IS_ENABLED (CONFIG_HYPERV )struct kvm_vmx *kvm_vmx =to_kvm_vmx (vcpu->kvm);if (kvm_x86_ops.tlb_remote_flush == hv_remote_flush_tlb) { -spin_lock (&kvm_vmx->ept_pointer_lock); -to_vmx (vcpu)->ept_pointer = eptp; -if (eptp != kvm_vmx->hv_tlb_eptp) - kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; -spin_unlock (&kvm_vmx->ept_pointer_lock); +spin_lock (&kvm_vmx->hv_root_ept_lock); +to_vmx (vcpu)->hv_root_ept = root_ept; +if (root_ept != kvm_vmx->hv_root_ept) + kvm_vmx->hv_root_ept =INVALID_PAGE ; +spin_unlock (&kvm_vmx->hv_root_ept_lock); }#endif }
@@ -3092,7 +3087,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, eptp =construct_eptp (vcpu, root_hpa, root_level);vmcs_write64 (EPT_POINTER , eptp); -hv_load_mmu_eptp (vcpu, eptp); +hv_track_root_ept (vcpu, root_hpa);if (!enable_unrestricted_guest && !is_paging (vcpu)) guest_cr3 =to_kvm_vmx (kvm)->ept_identity_map_addr;
@@ -6964,7 +6959,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) vmx->pi_desc.sn =1 ;#if IS_ENABLED (CONFIG_HYPERV ) - vmx->ept_pointer =INVALID_PAGE ; + vmx->hv_root_ept =INVALID_PAGE ;#endif return 0 ;
@@ -6983,7 +6978,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu)static int vmx_vm_init (struct kvm *kvm) {#if IS_ENABLED (CONFIG_HYPERV ) -spin_lock_init (&to_kvm_vmx (kvm)->ept_pointer_lock); +spin_lock_init (&to_kvm_vmx (kvm)->hv_root_ept_lock);#endif if (!ple_gap)
arch/x86/kvm/vmx/vmx.h +3 -3
@@ -277,7 +277,7 @@ struct vcpu_vmx {u64 msr_ia32_feature_control;u64 msr_ia32_feature_control_valid_bits;#if IS_ENABLED (CONFIG_HYPERV ) -u64 ept_pointer; +u64 hv_root_ept;#endif struct pt_desc pt_desc;
@@ -298,8 +298,8 @@ struct kvm_vmx {gpa_t ept_identity_map_addr;#if IS_ENABLED (CONFIG_HYPERV ) -hpa_t hv_tlb_eptp; -spinlock_t ept_pointer_lock; +hpa_t hv_root_ept; +spinlock_t hv_root_ept_lock;#endif };
Signature · 1 line
2.28.0
Re: [PATCH v3 11/11] KVM: VMX: Track root HPA instead of EPTP for paravirt Hyper-V TLB flush
Quoted 193 lines · Sean Christopherson
Track the address of the top-level EPT struct, a.k.a. the root HPA, instead of the EPTP itself for Hyper-V's paravirt TLB flush. The paravirt API takes only the address, not the full EPTP, and in theory tracking the EPTP could lead to false negatives, e.g. if the HPA matched but the attributes in the EPTP do not. In practice, such a mismatch is extremely unlikely, if not flat out impossible, given how KVM generates the EPTP.
Opportunsitically rename the related fields to use the 'root' nomenclature, and to prefix them with '
hv_' to connect them to Hyper-V's paravirt TLB flushing.Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 83 ++++++++++++++++++++---------------------- arch/x86/kvm/vmx/vmx.h | 6 +-- 2 files changed, 42 insertions(+), 47 deletions(-)arch/x86/kvm/vmx/vmx.c +39
-44@@ -481,18 +481,14 @@ static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush range->pages); } -static inline int hv_remote_flush_eptp (u64 eptp,struct kvm_tlb_range *range) +static inline int hv_remote_flush_root_ept (hpa_t root_ept, +struct kvm_tlb_range *range) { -/* -* 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 (eptp &PAGE_MASK , +return hyperv_flush_guest_mapping_range (root_ept, kvm_fill_hv_flush_list_func, (void *)range);else -return hyperv_flush_guest_mapping (eptp &PAGE_MASK ); +return hyperv_flush_guest_mapping (root_ept); } static int hv_remote_flush_tlb_with_range(struct kvm *kvm,@@ -500,56 +496,55 @@ static int hv_remote_flush_tlb_with_range(struct kvm *kvm, {struct kvm_vmx *kvm_vmx =to_kvm_vmx (kvm);struct kvm_vcpu *vcpu; -int ret =0 , i, nr_unique_valid_eptps; -u64 tmp_eptp; +int ret =0 , i, nr_unique_valid_roots; +hpa_t root; -spin_lock (&kvm_vmx->ept_pointer_lock); +spin_lock (&kvm_vmx->hv_root_ept_lock); -if (!VALID_PAGE (kvm_vmx->hv_tlb_eptp)) { - nr_unique_valid_eptps =0 ; +if (!VALID_PAGE (kvm_vmx->hv_root_ept)) { + nr_unique_valid_roots =0 ;/* -* Flush all valid EPTPs, and see if all vCPUs have converged -* on a common EPTP, in which case future flushes can skip the -* loop and flush the common EPTP. +* Flush all valid roots, and see if all vCPUs have converged +* on a common root, in which case future flushes can skip the +* loop and flush the common root. */ kvm_for_each_vcpu (i, vcpu, kvm) { - tmp_eptp =to_vmx (vcpu)->ept_pointer; -if (!VALID_PAGE (tmp_eptp) || - tmp_eptp == kvm_vmx->hv_tlb_eptp) + root =to_vmx (vcpu)->hv_root_ept; +if (!VALID_PAGE (root) || root == kvm_vmx->hv_root_ept)continue ;/* -* Set the tracked EPTP to the first valid EPTP. Keep -* this EPTP for the entirety of the loop even if more -* EPTPs are encountered as a low effort optimization -* to avoid flushing the same (first) EPTP again. +* Set the tracked root to the first valid root. Keep +* this root for the entirety of the loop even if more +* roots are encountered as a low effort optimization +* to avoid flushing the same (first) root again. */ -if (++nr_unique_valid_eptps ==1 ) - kvm_vmx->hv_tlb_eptp = tmp_eptp; +if (++nr_unique_valid_roots ==1 ) + kvm_vmx->hv_root_ept = root;if (!ret) - ret =hv_remote_flush_eptp (tmp_eptp, range); + ret =hv_remote_flush_root_ept (root, range);/* -* Stop processing EPTPs if a failure occurred and -* there is already a detected EPTP mismatch. +* Stop processing roots if a failure occurred and +* multiple valid roots have already been detected. */ -if (ret && nr_unique_valid_eptps >1 ) +if (ret && nr_unique_valid_roots >1 )break ; }/* -* The optimized flush of a single EPTP can't be used if there -* are multiple valid EPTPs (obviously). +* The optimized flush of a single root can't be used if there +* are multiple valid roots (obviously). */ -if (nr_unique_valid_eptps >1 ) - kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; +if (nr_unique_valid_roots >1 ) + kvm_vmx->hv_root_ept =INVALID_PAGE ; }else { - ret =hv_remote_flush_eptp (kvm_vmx->hv_tlb_eptp, range); + ret =hv_remote_flush_root_ept (kvm_vmx->hv_root_ept, range); } -spin_unlock (&kvm_vmx->ept_pointer_lock); +spin_unlock (&kvm_vmx->hv_root_ept_lock);return ret; }static int hv_remote_flush_tlb (struct kvm *kvm)@@ -584,17 +579,17 @@ static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)#endif /* IS_ENABLED(CONFIG_HYPERV) */ -static void hv_load_mmu_eptp (struct kvm_vcpu *vcpu,u64 eptp) +static void hv_track_root_ept (struct kvm_vcpu *vcpu,hpa_t root_ept) {#if IS_ENABLED (CONFIG_HYPERV )struct kvm_vmx *kvm_vmx =to_kvm_vmx (vcpu->kvm);if (kvm_x86_ops.tlb_remote_flush == hv_remote_flush_tlb) { -spin_lock (&kvm_vmx->ept_pointer_lock); -to_vmx (vcpu)->ept_pointer = eptp; -if (eptp != kvm_vmx->hv_tlb_eptp) - kvm_vmx->hv_tlb_eptp =INVALID_PAGE ; -spin_unlock (&kvm_vmx->ept_pointer_lock); +spin_lock (&kvm_vmx->hv_root_ept_lock); +to_vmx (vcpu)->hv_root_ept = root_ept; +if (root_ept != kvm_vmx->hv_root_ept) + kvm_vmx->hv_root_ept =INVALID_PAGE ; +spin_unlock (&kvm_vmx->hv_root_ept_lock); }#endif }@@ -3092,7 +3087,7 @@ static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, eptp =construct_eptp (vcpu, root_hpa, root_level);vmcs_write64 (EPT_POINTER , eptp); -hv_load_mmu_eptp (vcpu, eptp); +hv_track_root_ept (vcpu, root_hpa);if (!enable_unrestricted_guest && !is_paging (vcpu)) guest_cr3 =to_kvm_vmx (kvm)->ept_identity_map_addr;@@ -6964,7 +6959,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) vmx->pi_desc.sn =1 ;#if IS_ENABLED (CONFIG_HYPERV ) - vmx->ept_pointer =INVALID_PAGE ; + vmx->hv_root_ept =INVALID_PAGE ;#endif return 0 ;@@ -6983,7 +6978,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu)static int vmx_vm_init (struct kvm *kvm) {#if IS_ENABLED (CONFIG_HYPERV ) -spin_lock_init (&to_kvm_vmx (kvm)->ept_pointer_lock); +spin_lock_init (&to_kvm_vmx (kvm)->hv_root_ept_lock);#endif if (!ple_gap)arch/x86/kvm/vmx/vmx.h +1
-1@@ -277,7 +277,7 @@ struct vcpu_vmx {u64 msr_ia32_feature_control;u64 msr_ia32_feature_control_valid_bits;#if IS_ENABLED (CONFIG_HYPERV ) -u64 ept_pointer; +u64 hv_root_ept;#endif struct pt_desc pt_desc;
arch/x86/kvm/vmx/vmx.h struct kvm_vmx { · line 304
Quoted 9 lines
arch/x86/kvm/vmx/vmx.h +2
-2@@ -298,8 +298,8 @@ struct kvm_vmx {gpa_t ept_identity_map_addr;#if IS_ENABLED (CONFIG_HYPERV ) -hpa_t hv_tlb_eptp; -spinlock_t ept_pointer_lock; +hpa_t hv_root_ept; +spinlock_t hv_root_ept_lock;#endif };
Signature · 1 line
Vitaly
Quoted 35 lines · Sean Christopherson
Clean up KVM's PV TLB flushing when running with EPT on Hyper-V, i.e. as a nested VMM. No real goal in mind other than the sole patch in v1, which is a minor change to avoid a future mixup when TDX also wants to define .remote_flush_tlb. Everything else is opportunistic clean up.
Patch 1 legitimately tested on VMX (no SVM), everything else effectively build tested only.
v3:
- Add a patch to pass theroot_hpainstead of pgd tovmx_load_mmu_pgd()and retrieve the active PCID only when necessary. [Vitaly]
- Selectively collects reviews (skipped a few due to changes). [Vitaly]
- Explicitly invalidatehv_tlb_eptpinstead of leaving it valid when the mismatch tracker "knows" it's invalid. [Vitaly]
- Change the last patch to use "hv_root_ept" instead of "hv_tlb_pgd" to better reflect what is actually being tracked.v2: Rewrite everything.
Sean Christopherson (11):
KVM: x86: Get active PCID only when writing a CR3 value
KVM: VMX: Track common EPTP for Hyper-V's paravirt TLB flush
KVM: VMX: Stashkvm_vmxin a local variable for Hyper-V paravirt TLB
flush
KVM: VMX: Fold Hyper-V EPTP checking into it's only caller
KVM: VMX: Do Hyper-V TLB flush iff vCPU's EPTP hasn't been flushed
KVM: VMX: Invalidatehv_tlb_eptpto denote an EPTP mismatch
KVM: VMX: Don't invalidatehv_tlb_eptpif the new EPTP matches
KVM: VMX: Explicitly check forhv_remote_flush_tlbwhen loading pgd
KVM: VMX: Define Hyper-V paravirt TLB flush fields iff Hyper-V is
enabled
KVM: VMX: Skip additional Hyper-V TLB EPTP flushes if one fails
KVM: VMX: Track root HPA instead of EPTP for paravirt Hyper-V TLB
flush
Quoted 7 lines
arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/svm/svm.c | 4 +- arch/x86/kvm/vmx/vmx.c | 134 ++++++++++++++++++-------------- arch/x86/kvm/vmx/vmx.h | 19 ++--- 5 files changed, 87 insertions(+), 76 deletions(-)
Queued, thanks.
Paolo
Sean Christopherson
maintainerQuoted 43 lines · Paolo Bonzini
Quoted 34 lines · Sean Christopherson
Clean up KVM's PV TLB flushing when running with EPT on Hyper-V, i.e. as a nested VMM. No real goal in mind other than the sole patch in v1, which is a minor change to avoid a future mixup when TDX also wants to define .remote_flush_tlb. Everything else is opportunistic clean up.
Patch 1 legitimately tested on VMX (no SVM), everything else effectively build tested only.
v3:
- Add a patch to pass theroot_hpainstead of pgd tovmx_load_mmu_pgd()and retrieve the active PCID only when necessary. [Vitaly]
- Selectively collects reviews (skipped a few due to changes). [Vitaly]
- Explicitly invalidatehv_tlb_eptpinstead of leaving it valid when the mismatch tracker "knows" it's invalid. [Vitaly]
- Change the last patch to use "hv_root_ept" instead of "hv_tlb_pgd" to better reflect what is actually being tracked.v2: Rewrite everything.
Sean Christopherson (11):
KVM: x86: Get active PCID only when writing a CR3 value
KVM: VMX: Track common EPTP for Hyper-V's paravirt TLB flush
KVM: VMX: Stashkvm_vmxin a local variable for Hyper-V paravirt TLB
flush
KVM: VMX: Fold Hyper-V EPTP checking into it's only caller
KVM: VMX: Do Hyper-V TLB flush iff vCPU's EPTP hasn't been flushed
KVM: VMX: Invalidatehv_tlb_eptpto denote an EPTP mismatch
KVM: VMX: Don't invalidatehv_tlb_eptpif the new EPTP matches
KVM: VMX: Explicitly check forhv_remote_flush_tlbwhen loading pgd
KVM: VMX: Define Hyper-V paravirt TLB flush fields iff Hyper-V is
enabled
KVM: VMX: Skip additional Hyper-V TLB EPTP flushes if one fails
KVM: VMX: Track root HPA instead of EPTP for paravirt Hyper-V TLB
flushQuoted 7 lines
arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/mmu.h | 2 +- arch/x86/kvm/svm/svm.c | 4 +- arch/x86/kvm/vmx/vmx.c | 134 ++++++++++++++++++-------------- arch/x86/kvm/vmx/vmx.h | 19 ++--- 5 files changed, 87 insertions(+), 76 deletions(-)
Quoted 1 line
Queued, thanks.
Looks like this got shadow-banned, I'll send v4.