KVM: x86: TLB flushing fixes and enhancements
v2 cover KVM: x86: TLB flushing fixes and enhancements
This is "v2" of the VMX TLB flushing cleanup series, but over 70% of the patches are new in v2. The new growth stems from two related revelations:
1) Nested VMX doesn't properly flush all ASIDs/contexts on system events, e.g. on mmu_notifier invalidate all contexts for L1 *and* L2 need to be invalidated, but KVM generally only flushes L1 or L2 (or just L1).
2) #1 is largely benign because nested VMX always flushes the new context on nested VM-Entry/VM-Exit.
High level overview:
a) Fix the main TLB flushing bug with a big hammer.
b) Fix a few other flushing related bugs.
c) Clean up vmx_tlb_flush(), i.e. what was v1 of this series.
d) Reintroduce current-ASID/context flushing to regain some of the precision that got blasted away by the big hammer in #1.
e) Fix random code paths that unnecessarily trigger TLB flushes on nested VMX transitions.
f) Stop flushing on every nested VMX transition.
v2:
- Basically a new series.
v1:
- https://patchwork.kernel.org/cover/11394987/
Junaid Shahid (2):
KVM: nVMX: Invalidate all L2 roots when emulating INVVPID without EPT
KVM: x86: Sync SPTEs when injecting page/EPT fault into L1
Sean Christopherson (30):
KVM: VMX: Flush all EPTP/VPID contexts on remote TLB flush
KVM: nVMX: Validate the EPTP when emulating INVEPT(EXTENT_CONTEXT)
KVM: nVMX: Invalidate all EPTP contexts when emulating INVEPT for L1
KVM: x86: Export kvm_propagate_fault() (as
kvm_inject_emulated_page_fault)
KVM: x86: Consolidate logic for injecting page faults to L1
KVM: VMX: Skip global INVVPID fallback if vpid==0 in
vpid_sync_context()
KVM: VMX: Use vpid_sync_context() directly when possible
KVM: VMX: Move vpid_sync_vcpu_addr() down a few lines
KVM: VMX: Handle INVVPID fallback logic in vpid_sync_vcpu_addr()
KVM: VMX: Drop redundant capability checks in low level INVVPID
helpers
KVM: nVMX: Use vpid_sync_vcpu_addr() to emulate INVVPID with address
KVM: x86: Move "flush guest's TLB" logic to separate kvm_x86_ops hook
KVM: VMX: Clean up vmx_flush_tlb_gva()
KVM: x86: Drop @invalidate_gpa param from kvm_x86_ops' tlb_flush()
KVM: SVM: Wire up ->tlb_flush_guest() directly to svm_flush_tlb()
KVM: VMX: Move vmx_flush_tlb() to vmx.c
KVM: nVMX: Move nested_get_vpid02() to vmx/nested.h
KVM: VMX: Introduce vmx_flush_tlb_current()
KVM: SVM: Document the ASID logic in svm_flush_tlb()
KVM: x86: Rename ->tlb_flush() to ->tlb_flush_all()
KVM: nVMX: Add helper to handle TLB flushes on nested VM-Enter/VM-Exit
KVM: x86: Introduce KVM_REQ_TLB_FLUSH_CURRENT to flush current ASID
KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes
KVM: nVMX: Selectively use TLB_FLUSH_CURRENT for nested
VM-Enter/VM-Exit
KVM: nVMX: Reload APIC access page on nested VM-Exit only if necessary
KVM: VMX: Retrieve APIC access page HPA only when necessary
KVM: VMX: Don't reload APIC access page if its control is disabled
KVM: x86/mmu: Add module param to force TLB flush on root reuse
KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
KVM: nVMX: Free only the affected contexts when emulating INVEPT
arch/x86/include/asm/kvm_host.h | 16 ++- arch/x86/kvm/mmu/mmu.c | 26 ++-- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/svm.c | 19 ++- arch/x86/kvm/vmx/nested.c | 202 ++++++++++++++++++++------------ arch/x86/kvm/vmx/nested.h | 7 ++ arch/x86/kvm/vmx/ops.h | 32 +++-- arch/x86/kvm/vmx/vmx.c | 110 ++++++++++++++--- arch/x86/kvm/vmx/vmx.h | 19 +-- arch/x86/kvm/x86.c | 65 ++++++---- arch/x86/kvm/x86.h | 6 + 11 files changed, 334 insertions(+), 170 deletions(-)
Signature · 1 line
2.24.1
PATCH v2 1/32 KVM: VMX: Flush all EPTP/VPID contexts on remote TLB flush
Flush all EPTP/VPID contexts if a TLB flush _may_ have been triggered by a remote or deferred TLB flush, i.e. by KVM_REQ_TLB_FLUSH. Remote TLB flushes require all contexts to be invalidated, not just the active contexts, e.g. all mappings in all contexts for a given HVA need to be invalidated on a mmu_notifier invalidation. Similarly, the instigator of the deferred TLB flush may be expecting all contexts to be flushed, e.g. vmx_vcpu_load_vmcs().
Without nested VMX, flushing only the current EPTP/VPID context isn't problematic because KVM uses a constant VPID for each vCPU, and mmu_alloc_direct_roots() all but guarantees KVM will use a single EPTP for L1. In the rare case where a different EPTP is created or reused, KVM (currently) unconditionally flushes the new EPTP context prior to entering the guest.
With nested VMX, KVM conditionally uses a different VPID for L2, and unconditionally uses a different EPTP for L2. Because KVM doesn't _intentionally_ guarantee L2's EPTP/VPID context is flushed on nested VM-Enter, it'd be possible for a malicious L1 to attack the host and/or different VMs by exploiting the lack of flushing for L2.
1) Launch nested guest from malicious L1.
2) Nested VM-Enter to L2.
3) Access target GPA 'g'. CPU inserts TLB entry tagged with L2's ASID mapping 'g' to host PFN 'x'.
2) Nested VM-Exit to L1.
3) L1 triggers kernel same-page merging (ksm) by duplicating/zeroing the page for PFN 'x'.
4) Host kernel merges PFN 'x' with PFN 'y', i.e. unmaps PFN 'x' and remaps the page to PFN 'y'. mmu_notifier sends invalidate command, KVM flushes TLB only for L1's ASID.
4) Host kernel reallocates PFN 'x' to some other task/guest.
5) Nested VM-Enter to L2. KVM does not invalidate L2's EPTP or VPID.
6) L2 accesses GPA 'g' and gains read/write access to PFN 'x' via its stale TLB entry.
However, current KVM unconditionally flushes L1's EPTP/VPID context on nested VM-Exit. But, that behavior is mostly unintentional, KVM doesn't go out of its way to flush EPTP/VPID on nested VM-Enter/VM-Exit, rather a TLB flush is guaranteed to occur prior to re-entering L1 due to __kvm_mmu_new_cr3() always being called with skip_tlb_flush=false. On nested VM-Enter, this happens via kvm_init_shadow_ept_mmu() (nested EPT enabled) or in nested_vmx_load_cr3() (nested EPT disabled). On nested VM-Exit it occurs via nested_vmx_load_cr3().
This also fixes a bug where a deferred TLB flush in the context of L2, with EPT disabled, would flush L1's VPID instead of L2's VPID, as vmx_flush_tlb() flushes L1's VPID regardless of is_guest_mode().
--- arch/x86/kvm/vmx/vmx.h | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.h +27 -1
@@ -518,7 +518,33 @@ static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,static inline void vmx_flush_tlb (struct kvm_vcpu *vcpu,bool invalidate_gpa) { -__vmx_flush_tlb (vcpu,to_vmx (vcpu)->vpid, invalidate_gpa); +struct vcpu_vmx *vmx =to_vmx (vcpu); + +/* +* Flush all EPTP/VPID contexts if the TLB flush _may_ have been +* invoked via kvm_flush_remote_tlbs(), which always passes %true for +* @invalidate_gpa. Flushing remote TLBs requires all contexts to be +* flushed, not just the active context. +* +* Note, this also ensures a deferred TLB flush with VPID enabled and +* EPT disabled invalidates the "correct" VPID, by nuking both L1 and +* L2's VPIDs. +*/ +if (invalidate_gpa) { +if (enable_ept) { +ept_sync_global (); + }else if (enable_vpid) { +if (cpu_has_vmx_invvpid_global ()) { +vpid_sync_vcpu_global (); + }else { +WARN_ON_ONCE (!cpu_has_vmx_invvpid_single ()); +vpid_sync_vcpu_single (vmx->vpid); +vpid_sync_vcpu_single (vmx->nested.vpid02); + } + } + }else { +__vmx_flush_tlb (vcpu, vmx->vpid, false); + } }static inline void decache_tsc_multiplier (struct vcpu_vmx *vmx)
Signature · 1 line
2.24.1
PATCH v2 2/32 KVM: nVMX: Validate the EPTP when emulating INVEPT(EXTENT_CONTEXT)
In reply to Sean ChristophersonSignal VM-Fail for the single-context variant of INVEPT if the specified EPTP is invalid. Per the INEVPT pseudocode in Intel's SDM, it's subject to the standard EPT checks:
If VM entry with the "enable EPT" VM execution control set to 1 would fail due to the EPTP value then VMfail(Invalid operand to INVEPT/INVVPID);
--- arch/x86/kvm/vmx/nested.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/nested.c +5 -1
@@ -5156,8 +5156,12 @@ static int handle_invept(struct kvm_vcpu *vcpu) }switch (type) { -case VMX_EPT_EXTENT_GLOBAL :case VMX_EPT_EXTENT_CONTEXT : +if (!nested_vmx_check_eptp (vcpu, operand.eptp)) +return nested_vmx_failValid (vcpu, +VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID ); + fallthrough; +case VMX_EPT_EXTENT_GLOBAL :/* * TODO: Sync the necessary shadow EPT roots here, rather than * at the next emulated VM-entry.
Signature · 1 line
2.24.1
PATCH v2 3/32 KVM: nVMX: Invalidate all EPTP contexts when emulating INVEPT for L1
In reply to Sean ChristophersonFree all L2 (guest_mmu) roots when emulating INVEPT for L1. Outstanding changes to the EPT tables managed by L1 need to be recognized, and relying on KVM to always flush L2's EPTP context on nested VM-Enter is dangerous.
Similar to handle_invpcid(), rely on kvm_mmu_free_roots() to do a remote TLB flush if necessary, e.g. if L1 has never entered L2 then there is nothing to be done.
Nuking all L2 roots is overkill for the single-context variant, but it's the safe and easy bet. A more precise zap mechanism will be added in the future. Add a TODO to call out that KVM only needs to invalidate affected contexts.
--- arch/x86/kvm/vmx/nested.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/nested.c +4 -4
@@ -5160,12 +5160,12 @@ static int handle_invept(struct kvm_vcpu *vcpu)if (!nested_vmx_check_eptp (vcpu, operand.eptp))return nested_vmx_failValid (vcpu,VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID ); + +/* TODO: sync only the target EPTP context. */ fallthrough;case VMX_EPT_EXTENT_GLOBAL : -/* -* TODO: Sync the necessary shadow EPT roots here, rather than -* at the next emulated VM-entry. -*/ +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu, +KVM_MMU_ROOTS_ALL );break ;default :BUG_ON (1 );
Signature · 1 line
2.24.1
PATCH v2 4/32 KVM: nVMX: Invalidate all L2 roots when emulating INVVPID without EPT
In reply to Sean ChristophersonFrom: Junaid Shahid <[email protected]>
Free all L2 (guest_mmu) roots when emulating INVVPID for L1 and EPT is disabled, as outstanding changes to the page tables managed by L1 need to be recognized.
Similar to handle_invpcid() and handle_invept(), rely on kvm_mmu_free_roots() to do a remote TLB flush if necessary, e.g. if L1 has never entered L2 then there is nothing to be done.
--- arch/x86/kvm/vmx/nested.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/x86/kvm/vmx/nested.c +11 -0
@@ -5250,6 +5250,17 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)return kvm_skip_emulated_instruction (vcpu); } +/* +* Sync L2's shadow page tables if EPT is disabled, L1 is effectively +* invalidating linear mappings for L2 (tagged with L2's VPID). Sync +* all roots as VPIDs are not tracked in the MMU role. +* +* TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. +*/ +if (!enable_ept) +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu, +KVM_MMU_ROOTS_ALL ); +return nested_vmx_succeed (vcpu); }
Signature · 1 line
2.24.1
Re: [PATCH v2 04/32] KVM: nVMX: Invalidate all L2 roots when emulating INVVPID without EPT
Quoted 25 lines · Sean Christopherson
From: Junaid Shahid <[email protected]>
Free all L2 (
guest_mmu) roots when emulating INVVPID for L1 and EPT is disabled, as outstanding changes to the page tables managed by L1 need to be recognized.Similar to
handle_invpcid()andhandle_invept(), rely onkvm_mmu_free_roots()to do a remote TLB flush if necessary, e.g. if L1 has never entered L2 then there is nothing to be done.Fixes 5c614b3583e7b ("KVM: nVMX: nested VPID emulation")Signed-off-by Junaid Shahid <[email protected]>[sean: ported to upstream KVM, reworded the comment and changelog]Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/nested.c | 11 +++++++++++ 1 file changed, 11 insertions(+)arch/x86/kvm/vmx/nested.c +0
-0@@ -5250,6 +5250,17 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)return kvm_skip_emulated_instruction (vcpu); }
arch/x86/kvm/vmx/nested.c static int handle_invvpid(struct kvm_vcpu *vcpu) · line 5261
Quoted 10 lines
arch/x86/kvm/vmx/nested.c +9
-0+/* +* Sync L2's shadow page tables if EPT is disabled, L1 is effectively +* invalidating linear mappings for L2 (tagged with L2's VPID). Sync +* all roots as VPIDs are not tracked in the MMU role. +* +* TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. +*/ +if (!enable_ept) + kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu,
Aha! This is wrong. guest_mmu is only used if EPT is enabled. The silver lining is that I have confirmed that this code is needed and works as intended (when the correct mmu is nuked).
Quoted 8 lines
arch/x86/kvm/vmx/nested.c +2
-0+ KVM_MMU_ROOTS_ALL); +return nested_vmx_succeed (vcpu); }Signature · 1 line
2.24.1
PATCH v2 5/32 KVM: x86: Export kvm_propagate_fault() (as kvm_inject_emulated_page_fault)
In reply to Sean ChristophersonExport the page fault propagation helper so that VMX can use it to correctly emulate TLB invalidation on page faults in an upcoming patch.
In the (hopefully) not-too-distant future, SGX virtualization will also want access to the helper for injecting page faults to the correct level (L1 vs. L2) when emulating ENCLS instructions.
Rename the function to kvm_inject_emulated_page_fault() to clarify that it is (a) injecting a fault and (b) only for page faults. WARN if it's invoked with an exception other than PF_VECTOR.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1447,6 +1447,8 @@ void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);void kvm_requeue_exception (struct kvm_vcpu *vcpu,unsigned nr);void kvm_requeue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code);void kvm_inject_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault); +bool kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu, +struct x86_exception *fault);int kvm_read_guest_page_mmu (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu,gfn_t gfn,void *data,int offset,int len,u32 access);
arch/x86/kvm/x86.c +6 -2
@@ -611,8 +611,11 @@ void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) }EXPORT_SYMBOL_GPL (kvm_inject_page_fault); -static bool kvm_propagate_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault) +bool kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu, +struct x86_exception *fault) { +WARN_ON_ONCE (fault->vector !=PF_VECTOR ); +if (mmu_is_nested (vcpu) && !fault->nested_page_fault) vcpu->arch.nested_mmu.inject_page_fault (vcpu, fault);else
@@ -620,6 +623,7 @@ static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *faureturn fault->nested_page_fault; } +EXPORT_SYMBOL_GPL (kvm_inject_emulated_page_fault);void kvm_inject_nmi (struct kvm_vcpu *vcpu) {
@@ -6373,7 +6377,7 @@ static bool inject_emulated_exception(struct kvm_vcpu *vcpu) {struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;if (ctxt->exception.vector ==PF_VECTOR ) -return kvm_propagate_fault (vcpu, &ctxt->exception); +return kvm_inject_emulated_page_fault (vcpu, &ctxt->exception);if (ctxt->exception.error_code_valid) kvm_queue_exception_e(vcpu, ctxt->exception.vector,
Signature · 1 line
2.24.1
PATCH v2 6/32 KVM: x86: Consolidate logic for injecting page faults to L1
In reply to Sean ChristophersonMove the MMU's inject_page_fault(), which is used to inject page faults encountered when walking L1's page tables, to x86.c and use it to handle the non-nested path of kvm_inject_emulated_page_fault(). Using a common helper will reduce duplicate code in a future patch to sync SPTEs on emulated page faults, and also eliminates the rather confusing function name "inject_page_fault", which collides with struct kvm_mmu's hook.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 6 ------ arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/x86.c | 8 +++++++- 4 files changed, 10 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1447,6 +1447,8 @@ void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);void kvm_requeue_exception (struct kvm_vcpu *vcpu,unsigned nr);void kvm_requeue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code);void kvm_inject_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault); +void kvm_inject_l1_page_fault (struct kvm_vcpu *vcpu, +struct x86_exception *fault);bool kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault); int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
arch/x86/kvm/mmu/mmu.c +0 -6
@@ -4357,12 +4357,6 @@ static unsigned long get_cr3(struct kvm_vcpu *vcpu)return kvm_read_cr3 (vcpu); } -static void inject_page_fault (struct kvm_vcpu *vcpu, -struct x86_exception *fault) -{ - vcpu->arch.mmu->inject_page_fault (vcpu, fault); -} -static bool sync_mmio_spte (struct kvm_vcpu *vcpu,u64 *sptep,gfn_t gfn,unsigned int access,int *nr_present) {
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -812,7 +812,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gpa_t addr, u32 error_code,if (!r) {pgprintk ("%s: guest page fault\n" , __func__);if (!prefault) -inject_page_fault (vcpu, &walker.fault); +kvm_inject_l1_page_fault (vcpu, &walker.fault);return RET_PF_RETRY ; }
arch/x86/kvm/x86.c +7 -1
@@ -611,6 +611,12 @@ void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) }EXPORT_SYMBOL_GPL (kvm_inject_page_fault); +void kvm_inject_l1_page_fault (struct kvm_vcpu *vcpu, +struct x86_exception *fault) +{ + vcpu->arch.mmu->inject_page_fault (vcpu, fault); +} +bool kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault) {
@@ -619,7 +625,7 @@ bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,if (mmu_is_nested (vcpu) && !fault->nested_page_fault) vcpu->arch.nested_mmu.inject_page_fault (vcpu, fault);else - vcpu->arch.mmu->inject_page_fault (vcpu, fault); +kvm_inject_l1_page_fault (vcpu, fault);return fault->nested_page_fault; }
Signature · 1 line
2.24.1
PATCH v2 7/32 KVM: x86: Sync SPTEs when injecting page/EPT fault into L1
In reply to Sean ChristophersonFrom: Junaid Shahid <[email protected]>
When injecting a page fault or EPT violation/misconfiguration, invoke ->invlpg() to sync any shadow PTEs associated with the faulting address, including those in previous MMUs that are associated with L1's current EPTP (in a nested EPT scenario). Skip the sync (which incurs a costly retpoline) if the MMU can't have unsync'd SPTEs for the address.
In addition, flush any hardware TLB entries associated with the faulting address if the fault is the result of emulation, i.e. not an async page fault. !PRESENT and RSVD page faults are exempt from the flushing as the CPU is not allowed to cache such translations.
--- arch/x86/kvm/vmx/nested.c | 44 +++++++++++++++++++++++++++++---------- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/x86.c | 17 +++++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-)
arch/x86/kvm/vmx/nested.c +33 -11
@@ -323,6 +323,14 @@ void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)vcpu_put (vcpu); } +#define EPTP_PA_MASK GENMASK_ULL(51, 12) + +static bool nested_ept_root_matches (hpa_t root_hpa,u64 root_eptp,u64 eptp) +{ +return VALID_PAGE (root_hpa) && + ((root_eptp &EPTP_PA_MASK ) == (eptp &EPTP_PA_MASK )); +} +static void nested_ept_inject_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault) {
@@ -330,18 +338,32 @@ static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,struct vcpu_vmx *vmx =to_vmx (vcpu);u32 exit_reason;unsigned long exit_qualification = vcpu->arch.exit_qualification; +struct kvm_mmu_root_info *prev; +u64 gpa = fault->address; +int i;if (vmx->nested.pml_full) { exit_reason =EXIT_REASON_PML_FULL ; vmx->nested.pml_full = false; exit_qualification &=INTR_INFO_UNBLOCK_NMI ; - }else if (fault->error_code &PFERR_RSVD_MASK ) - exit_reason =EXIT_REASON_EPT_MISCONFIG ; -else - exit_reason =EXIT_REASON_EPT_VIOLATION ; + }else { +if (fault->error_code &PFERR_RSVD_MASK ) + exit_reason =EXIT_REASON_EPT_MISCONFIG ; +else + exit_reason =EXIT_REASON_EPT_VIOLATION ; + +/* Sync SPTEs in cached MMUs that track the current L1 EPTP. */ +for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) { + prev = &vcpu->arch.mmu->prev_roots[i]; + +if (nested_ept_root_matches (prev->hpa, prev->cr3, + vmcs12->ept_pointer)) + vcpu->arch.mmu->invlpg (vcpu, gpa, prev->hpa); + } + }nested_vmx_vmexit (vcpu, exit_reason,0 , exit_qualification); - vmcs12->guest_physical_address = fault->address; + vmcs12->guest_physical_address = gpa; }static void nested_ept_init_mmu_context (struct kvm_vcpu *vcpu)
@@ -4559,7 +4581,7 @@ static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)return 1 ;if (kvm_read_guest_virt (vcpu, gva, vmpointer,sizeof (*vmpointer), &e)) { -kvm_inject_page_fault (vcpu, &e); +kvm_inject_emulated_page_fault (vcpu, &e);return 1 ; }
@@ -4868,7 +4890,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu)return 1 ;/* _system ok, nested_vmx_check_permission has verified cpl=0 */ if (kvm_write_guest_virt_system (vcpu, gva, &value, len, &e)) { -kvm_inject_page_fault (vcpu, &e); +kvm_inject_emulated_page_fault (vcpu, &e);return 1 ; } }
@@ -4942,7 +4964,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) instr_info, false, len, &gva))return 1 ;if (kvm_read_guest_virt (vcpu, gva, &value, len, &e)) { -kvm_inject_page_fault (vcpu, &e); +kvm_inject_emulated_page_fault (vcpu, &e);return 1 ; } }
@@ -5107,7 +5129,7 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu)/* *_system ok, nested_vmx_check_permission has verified cpl=0 */ if (kvm_write_guest_virt_system (vcpu, gva, (void *)¤t_vmptr,sizeof (gpa_t), &e)) { -kvm_inject_page_fault (vcpu, &e); +kvm_inject_emulated_page_fault (vcpu, &e);return 1 ; }return nested_vmx_succeed (vcpu);
@@ -5151,7 +5173,7 @@ static int handle_invept(struct kvm_vcpu *vcpu) vmx_instruction_info, false, sizeof(operand), &gva))return 1 ;if (kvm_read_guest_virt (vcpu, gva, &operand,sizeof (operand), &e)) { -kvm_inject_page_fault (vcpu, &e); +kvm_inject_emulated_page_fault (vcpu, &e);return 1 ; }
@@ -5215,7 +5237,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) vmx_instruction_info, false, sizeof(operand), &gva))return 1 ;if (kvm_read_guest_virt (vcpu, gva, &operand,sizeof (operand), &e)) { -kvm_inject_page_fault (vcpu, &e); +kvm_inject_emulated_page_fault (vcpu, &e);return 1 ; }if (operand.vpid >>16 )
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -5404,7 +5404,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu)return 1 ;if (kvm_read_guest_virt (vcpu, gva, &operand,sizeof (operand), &e)) { -kvm_inject_page_fault (vcpu, &e); +kvm_inject_emulated_page_fault (vcpu, &e);return 1 ; }
arch/x86/kvm/x86.c +17 -0
@@ -614,6 +614,11 @@ EXPORT_SYMBOL_GPL(kvm_inject_page_fault);void kvm_inject_l1_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault) { +if (!vcpu->arch.mmu->direct_map && + (fault->error_code &PFERR_PRESENT_MASK )) + vcpu->arch.mmu->invlpg (vcpu, fault->address, + vcpu->arch.mmu->root_hpa); + vcpu->arch.mmu->inject_page_fault (vcpu, fault); }
@@ -622,7 +627,19 @@ bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu, {WARN_ON_ONCE (fault->vector !=PF_VECTOR ); +/* +* Invalidate the TLB entry for the faulting address, if one can exist, +* else the access will fault indefinitely (and to emulate hardware). +*/ +if ((fault->error_code &PFERR_PRESENT_MASK ) && + !(fault->error_code &PFERR_RSVD_MASK )) + kvm_x86_ops->tlb_flush_gva (vcpu, fault->address); +if (mmu_is_nested (vcpu) && !fault->nested_page_fault) +/* +* No need to sync SPTEs, the fault is being injected into L2, +* whose page tables are not being shadowed. +*/ vcpu->arch.nested_mmu.inject_page_fault (vcpu, fault);else kvm_inject_l1_page_fault (vcpu, fault);
Signature · 1 line
2.24.1
PATCH v2 8/32 KVM: VMX: Skip global INVVPID fallback if vpid==0 in vpid_sync_context()
In reply to Sean ChristophersonSkip the global INVVPID in the unlikely scenario that vpid==0 and the SINGLE_CONTEXT variant of INVVPID is unsupported. If vpid==0, there's no need to INVVPID as it's impossible to do VM-Enter with VPID enabled and vmcs.VPID==0, i.e. there can't be any TLB entries for the vCPU with vpid==0. The fact that the SINGLE_CONTEXT variant isn't supported is irrelevant.
--- arch/x86/kvm/vmx/ops.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/ops.h +1 -1
@@ -285,7 +285,7 @@ static inline void vpid_sync_context(int vpid) {if (cpu_has_vmx_invvpid_single ())vpid_sync_vcpu_single (vpid); -else +else if (vpid !=0 )vpid_sync_vcpu_global (); }
Signature · 1 line
2.24.1
PATCH v2 9/32 KVM: VMX: Use vpid_sync_context() directly when possible
In reply to Sean ChristophersonUse vpid_sync_context() directly for flows that run if and only if enable_vpid=1, or more specifically, nested VMX flows that are gated by vmx->nested.msrs.secondary_ctls_high.SECONDARY_EXEC_ENABLE_VPID being set, which is allowed if and only if enable_vpid=1. Because these flows call __vmx_flush_tlb() with @invalidate_gpa=false, the if-statement that decides between INVEPT and INVVPID will always go down the INVVPID path, i.e. call vpid_sync_context() because
"enable_ept && (invalidate_gpa || !enable_vpid)" always evaluates false.
This helps pave the way toward removing @invalidate_gpa and @vpid from __vmx_flush_tlb() and its callers.
Opportunstically drop unnecessary brackets in handle_invvpid() around an affected __vmx_flush_tlb()->vpid_sync_context() conversion.
No functional change intended.
--- arch/x86/kvm/vmx/nested.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/nested.c +6 -6
@@ -2481,7 +2481,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,if (nested_cpu_has_vpid (vmcs12) &&nested_has_guest_tlb_tag (vcpu)) {if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { vmx->nested.last_vpid = vmcs12->virtual_processor_id; -__vmx_flush_tlb (vcpu,nested_get_vpid02 (vcpu), false); +vpid_sync_context (nested_get_vpid02 (vcpu)); } }else {/*
@@ -5251,21 +5251,21 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) is_noncanonical_address(operand.gla, vcpu))return nested_vmx_failValid (vcpu,VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID ); -if (cpu_has_vmx_invvpid_individual_addr ()) { +if (cpu_has_vmx_invvpid_individual_addr ())__invvpid (VMX_VPID_EXTENT_INDIVIDUAL_ADDR , vpid02, operand.gla); - }else -__vmx_flush_tlb (vcpu, vpid02, false); +else +vpid_sync_context (vpid02);break ;case VMX_VPID_EXTENT_SINGLE_CONTEXT :case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL :if (!operand.vpid)return nested_vmx_failValid (vcpu,VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID ); -__vmx_flush_tlb (vcpu, vpid02, false); +vpid_sync_context (vpid02);break ;case VMX_VPID_EXTENT_ALL_CONTEXT : -__vmx_flush_tlb (vcpu, vpid02, false); +vpid_sync_context (vpid02);break ;default :WARN_ON_ONCE (1 );
Signature · 1 line
2.24.1
PATCH v2 10/32 KVM: VMX: Move vpid_sync_vcpu_addr() down a few lines
In reply to Sean ChristophersonMove vpid_sync_vcpu_addr() below vpid_sync_context() so that it can be refactored in a future patch to call vpid_sync_context() directly when the "individual address" INVVPID variant isn't supported.
No functional change intended.
--- arch/x86/kvm/vmx/ops.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
arch/x86/kvm/vmx/ops.h +13 -13
@@ -253,19 +253,6 @@ static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)vmx_asm2 (invept,"r" (ext),"m" (operand), ext, eptp, gpa); } -static inline bool vpid_sync_vcpu_addr (int vpid,gva_t addr) -{ -if (vpid ==0 ) -return true; - -if (cpu_has_vmx_invvpid_individual_addr ()) { -__invvpid (VMX_VPID_EXTENT_INDIVIDUAL_ADDR , vpid, addr); -return true; - } - -return false; -} -static inline void vpid_sync_vcpu_single (int vpid) {if (vpid ==0 )
@@ -289,6 +276,19 @@ static inline void vpid_sync_context(int vpid)vpid_sync_vcpu_global (); } +static inline bool vpid_sync_vcpu_addr (int vpid,gva_t addr) +{ +if (vpid ==0 ) +return true; + +if (cpu_has_vmx_invvpid_individual_addr ()) { +__invvpid (VMX_VPID_EXTENT_INDIVIDUAL_ADDR , vpid, addr); +return true; + } + +return false; +} +static inline void ept_sync_global (void ) {__invept (VMX_EPT_EXTENT_GLOBAL ,0 ,0 );
Signature · 1 line
2.24.1
PATCH v2 11/32 KVM: VMX: Handle INVVPID fallback logic in vpid_sync_vcpu_addr()
In reply to Sean ChristophersonDirectly invoke vpid_sync_context() to do a global INVVPID when the individual address variant is not supported instead of deferring such behavior to the caller. This allows for additional consolidation of code as the logic is basically identical to the emulation of the individual address variant in handle_invvpid().
No functional change intended.
--- arch/x86/kvm/vmx/ops.h | 12 +++++------- arch/x86/kvm/vmx/vmx.c | 3 +-- 2 files changed, 6 insertions(+), 9 deletions(-)
arch/x86/kvm/vmx/ops.h +5 -7
@@ -276,17 +276,15 @@ static inline void vpid_sync_context(int vpid)vpid_sync_vcpu_global (); } -static inline bool vpid_sync_vcpu_addr (int vpid,gva_t addr) +static inline void vpid_sync_vcpu_addr (int vpid,gva_t addr) {if (vpid ==0 ) -return true; +return ; -if (cpu_has_vmx_invvpid_individual_addr ()) { +if (cpu_has_vmx_invvpid_individual_addr ())__invvpid (VMX_VPID_EXTENT_INDIVIDUAL_ADDR , vpid, addr); -return true; - } - -return false; +else +vpid_sync_context (vpid); }static inline void ept_sync_global (void )
arch/x86/kvm/vmx/vmx.c +1 -2
@@ -2853,8 +2853,7 @@ static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr) {int vpid =to_vmx (vcpu)->vpid; -if (!vpid_sync_vcpu_addr (vpid, addr)) -vpid_sync_context (vpid); +vpid_sync_vcpu_addr (vpid, addr);/* * If VPIDs are not supported or enabled, then the above is a no-op.
Signature · 1 line
2.24.1
PATCH v2 12/32 KVM: VMX: Drop redundant capability checks in low level INVVPID helpers
In reply to Sean ChristophersonRemove the INVVPID capabilities checks from vpid_sync_vcpu_single() and vpid_sync_vcpu_global() now that all callers ensure the INVVPID variant is supported. Note, in some cases the guarantee is provided in concert with hardware_setup(), which enables VPID if and only if at least of invvpid_single() or invvpid_global() is supported.
Drop the WARN_ON_ONCE() from vmx_flush_tlb() as vpid_sync_vcpu_single() will trigger a WARN() on INVVPID failure, i.e. if SINGLE_CONTEXT isn't supported.
--- arch/x86/kvm/vmx/ops.h | 6 ++---- arch/x86/kvm/vmx/vmx.h | 1 - 2 files changed, 2 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/ops.h +2 -4
@@ -258,14 +258,12 @@ static inline void vpid_sync_vcpu_single(int vpid)if (vpid ==0 )return ; -if (cpu_has_vmx_invvpid_single ()) -__invvpid (VMX_VPID_EXTENT_SINGLE_CONTEXT , vpid,0 ); +__invvpid (VMX_VPID_EXTENT_SINGLE_CONTEXT , vpid,0 ); }static inline void vpid_sync_vcpu_global (void ) { -if (cpu_has_vmx_invvpid_global ()) -__invvpid (VMX_VPID_EXTENT_ALL_CONTEXT ,0 ,0 ); +__invvpid (VMX_VPID_EXTENT_ALL_CONTEXT ,0 ,0 ); }static inline void vpid_sync_context (int vpid)
arch/x86/kvm/vmx/vmx.h +0 -1
@@ -537,7 +537,6 @@ static inline void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)if (cpu_has_vmx_invvpid_global ()) {vpid_sync_vcpu_global (); }else { -WARN_ON_ONCE (!cpu_has_vmx_invvpid_single ());vpid_sync_vcpu_single (vmx->vpid);vpid_sync_vcpu_single (vmx->nested.vpid02); }
Signature · 1 line
2.24.1
PATCH v2 13/32 KVM: nVMX: Use vpid_sync_vcpu_addr() to emulate INVVPID with address
In reply to Sean ChristophersonUse vpid_sync_vcpu_addr() to emulate the "individual address" variant of INVVPID now that said function handles the fallback case of the (host) CPU not supporting "individual address".
Note, the "vpid == 0" checks in the vpid_sync_*() helpers aren't actually redundant with the "!operand.vpid" check in handle_invvpid(), as the vpid passed to vpid_sync_vcpu_addr() is a KVM (host) controlled value, i.e. vpid02 can be zero even if operand.vpid is non-zero.
No functional change intended.
--- arch/x86/kvm/vmx/nested.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -5
@@ -5251,11 +5251,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) is_noncanonical_address(operand.gla, vcpu))return nested_vmx_failValid (vcpu,VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID ); -if (cpu_has_vmx_invvpid_individual_addr ()) -__invvpid (VMX_VPID_EXTENT_INDIVIDUAL_ADDR , - vpid02, operand.gla); -else -vpid_sync_context (vpid02); +vpid_sync_vcpu_addr (vpid02, operand.gla);break ;case VMX_VPID_EXTENT_SINGLE_CONTEXT :case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL :
Signature · 1 line
2.24.1
PATCH v2 14/32 KVM: x86: Move "flush guest's TLB" logic to separate kvm_x86_ops hook
In reply to Sean ChristophersonAdd a dedicated hook to handle flushing TLB entries on behalf of the guest, i.e. for a paravirtualized TLB flush, and use it directly instead of bouncing through kvm_vcpu_flush_tlb().
For VMX, change the effective implementation implementation to never do INVEPT and flush only the current context, i.e. to always flush via INVVPID(SINGLE_CONTEXT). The INVEPT performed by __vmx_flush_tlb() when @invalidate_gpa=false and enable_vpid=0 is unnecessary, as it will only flush guest-physical mappings; linear and combined mappings are flushed by VM-Enter when VPID is disabled, and changes in the guest pages tables do not affect guest-physical mappings.
When EPT and VPID are enabled, doing INVVPID is not required (by Intel's architecture) to invalidate guest-physical mappings, i.e. TLB entries that cache guest-physical mappings can live across INVVPID as the mappings are associated with an EPTP, not a VPID. The intent of @invalidate_gpa is to inform vmx_flush_tlb() that it must "invalidate gpa mappings", i.e. do INVEPT and not simply INVVPID. Other than nested VPID handling, which now calls vpid_sync_context() directly, the only scenario where KVM can safely do INVVPID instead of INVEPT (when EPT is enabled) is if KVM is flushing TLB entries from the guest's perspective, i.e. is only required to invalidate linear mappings.
For SVM, flushing TLB entries from the guest's perspective can be done by flushing the current ASID, as changes to the guest's page tables are associated only with the current ASID.
Adding a dedicated ->tlb_flush_guest() paves the way toward removing @invalidate_gpa, which is a potentially dangerous control flag as its meaning is not exactly crystal clear, even for those who are familiar with the subtleties of what mappings Intel CPUs are/aren't allowed to keep across various invalidation scenarios.
--- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/svm.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 13 +++++++++++++ arch/x86/kvm/x86.c | 2 +- 4 files changed, 26 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +6 -0
@@ -1118,6 +1118,12 @@ struct kvm_x86_ops {*/ void (*tlb_flush_gva)(struct kvm_vcpu *vcpu,gva_t addr); +/* +* Flush any TLB entries created by the guest. Like tlb_flush_gva(), +* does not need to flush GPA->HPA mappings. +*/ +void (*tlb_flush_guest)(struct kvm_vcpu *vcpu); +void (*run)(struct kvm_vcpu *vcpu);int (*handle_exit)(struct kvm_vcpu *vcpu,enum exit_fastpath_completion exit_fastpath);
arch/x86/kvm/svm.c +6 -0
@@ -5643,6 +5643,11 @@ static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)invlpga (gva, svm->vmcb->control.asid); } +static void svm_flush_tlb_guest (struct kvm_vcpu *vcpu) +{ +svm_flush_tlb (vcpu, false); +} +static void svm_prepare_guest_switch (struct kvm_vcpu *vcpu) { }
@@ -7400,6 +7405,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .tlb_flush = svm_flush_tlb, .tlb_flush_gva = svm_flush_tlb_gva, + .tlb_flush_guest = svm_flush_tlb_guest, .run = svm_vcpu_run, .handle_exit = handle_exit,
arch/x86/kvm/vmx/vmx.c +13 -0
@@ -2862,6 +2862,18 @@ static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)*/ } +static void vmx_flush_tlb_guest (struct kvm_vcpu *vcpu) +{ +/* +* vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0 +* or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit +* are required to flush GVA->{G,H}PA mappings from the TLB if vpid is +* disabled (VM-Enter with vpid enabled and vpid==0 is disallowed), +* i.e. no explicit INVVPID is necessary. +*/ +vpid_sync_context (to_vmx (vcpu)->vpid); +} +static void vmx_decache_cr0_guest_bits (struct kvm_vcpu *vcpu) {ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
@@ -7875,6 +7887,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .tlb_flush = vmx_flush_tlb, .tlb_flush_gva = vmx_flush_tlb_gva, + .tlb_flush_guest = vmx_flush_tlb_guest, .run = vmx_vcpu_run, .handle_exit = vmx_handle_exit,
arch/x86/kvm/x86.c +1 -1
@@ -2725,7 +2725,7 @@ static void record_steal_time(struct kvm_vcpu *vcpu)trace_kvm_pv_tlb_flush (vcpu->vcpu_id, st->preempted &KVM_VCPU_FLUSH_TLB );if (xchg (&st->preempted,0 ) &KVM_VCPU_FLUSH_TLB ) -kvm_vcpu_flush_tlb (vcpu, false); + kvm_x86_ops->tlb_flush_guest (vcpu); vcpu->arch.st.preempted =0 ;
Signature · 1 line
2.24.1
PATCH v2 15/32 KVM: VMX: Clean up vmx_flush_tlb_gva()
In reply to Sean ChristophersonRefactor vmx_flush_tlb_gva() to remove a superfluous local variable and clean up its comment, which is oddly located below the code it is commenting.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/vmx.c +3 -7
@@ -2851,15 +2851,11 @@ static void exit_lmode(struct kvm_vcpu *vcpu)static void vmx_flush_tlb_gva (struct kvm_vcpu *vcpu,gva_t addr) { -int vpid =to_vmx (vcpu)->vpid; - -vpid_sync_vcpu_addr (vpid, addr); -/* -* If VPIDs are not supported or enabled, then the above is a no-op. -* But we don't really need a TLB flush in that case anyway, because -* each VM entry/exit includes an implicit flush when VPID is 0. +* vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in +* vmx_flush_tlb_guest() for an explanation of why this is ok. */ +vpid_sync_vcpu_addr (to_vmx (vcpu)->vpid, addr); }static void vmx_flush_tlb_guest (struct kvm_vcpu *vcpu)
Signature · 1 line
2.24.1
PATCH v2 16/32 KVM: x86: Drop @invalidate_gpa param from kvm_x86_ops' tlb_flush()
In reply to Sean ChristophersonDrop @invalidate_gpa from ->tlb_flush() and kvm_vcpu_flush_tlb() now that all callers pass %true for said param, or ignore the param (SVM has an internal call to svm_flush_tlb() in svm_flush_tlb_guest that somewhat arbitrarily passes %false).
Remove __vmx_flush_tlb() as it is no longer used.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/svm.c | 10 ++++---- arch/x86/kvm/vmx/vmx.c | 4 ++-- arch/x86/kvm/vmx/vmx.h | 42 ++++++++++----------------------- arch/x86/kvm/x86.c | 6 ++--- 6 files changed, 24 insertions(+), 42 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1105,7 +1105,7 @@ struct kvm_x86_ops {unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags); -void (*tlb_flush)(struct kvm_vcpu *vcpu,bool invalidate_gpa); +void (*tlb_flush)(struct kvm_vcpu *vcpu);int (*tlb_remote_flush)(struct kvm *kvm);int (*tlb_remote_flush_with_range)(struct kvm *kvm,struct kvm_tlb_range *range);
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -5177,7 +5177,7 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu)if (r) goto out;kvm_mmu_load_pgd (vcpu); - kvm_x86_ops->tlb_flush (vcpu, true); + kvm_x86_ops->tlb_flush (vcpu); out:return r; }
arch/x86/kvm/svm.c +5 -5
@@ -385,7 +385,7 @@ module_param(dump_invalid_vmcb, bool, 0644);static u8 rsm_ins_bytes[] ="\x0f\xaa" ;static void svm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0); -static void svm_flush_tlb (struct kvm_vcpu *vcpu,bool invalidate_gpa); +static void svm_flush_tlb (struct kvm_vcpu *vcpu);static void svm_complete_interrupts (struct vcpu_svm *svm);static void svm_toggle_avic_for_irq_window (struct kvm_vcpu *vcpu,bool activate);static inline void avic_post_state_restore (struct kvm_vcpu *vcpu);
@@ -2692,7 +2692,7 @@ static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ;if (npt_enabled && ((old_cr4 ^ cr4) &X86_CR4_PGE )) -svm_flush_tlb (vcpu, true); +svm_flush_tlb (vcpu); vcpu->arch.cr4 = cr4;if (!npt_enabled)
@@ -3630,7 +3630,7 @@ static void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa, svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; svm->nested.intercept = nested_vmcb->control.intercept; -svm_flush_tlb (&svm->vcpu, true); +svm_flush_tlb (&svm->vcpu); svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl |V_INTR_MASKING_MASK ;if (nested_vmcb->control.int_ctl &V_INTR_MASKING_MASK ) svm->vcpu.arch.hflags |=HF_VINTR_MASK ;
@@ -5626,7 +5626,7 @@ static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)return 0 ; } -static void svm_flush_tlb (struct kvm_vcpu *vcpu,bool invalidate_gpa) +static void svm_flush_tlb (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -5645,7 +5645,7 @@ static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)static void svm_flush_tlb_guest (struct kvm_vcpu *vcpu) { -svm_flush_tlb (vcpu, false); +svm_flush_tlb (vcpu); }static void svm_prepare_guest_switch (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +2 -2
@@ -6079,7 +6079,7 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)if (flexpriority_enabled) { sec_exec_control |=SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES ; -vmx_flush_tlb (vcpu, true); +vmx_flush_tlb (vcpu); }break ;case LAPIC_MODE_X2APIC :
@@ -6097,7 +6097,7 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) {if (!is_guest_mode (vcpu)) {vmcs_write64 (APIC_ACCESS_ADDR , hpa); -vmx_flush_tlb (vcpu, true); +vmx_flush_tlb (vcpu); } }
arch/x86/kvm/vmx/vmx.h +12 -30
58 lines not on this page · raw message
arch/x86/kvm/x86.c +3 -3
@@ -2696,10 +2696,10 @@ static void kvmclock_reset(struct kvm_vcpu *vcpu) vcpu->arch.time =0 ; } -static void kvm_vcpu_flush_tlb (struct kvm_vcpu *vcpu,bool invalidate_gpa) +static void kvm_vcpu_flush_tlb (struct kvm_vcpu *vcpu) { ++vcpu->stat.tlb_flush; - kvm_x86_ops->tlb_flush (vcpu, invalidate_gpa); + kvm_x86_ops->tlb_flush (vcpu); }static void record_steal_time (struct kvm_vcpu *vcpu)
@@ -8223,7 +8223,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_LOAD_MMU_PGD , vcpu))kvm_mmu_load_pgd (vcpu);if (kvm_check_request (KVM_REQ_TLB_FLUSH , vcpu)) -kvm_vcpu_flush_tlb (vcpu, true); +kvm_vcpu_flush_tlb (vcpu);if (kvm_check_request (KVM_REQ_REPORT_TPR_ACCESS , vcpu)) { vcpu->run->exit_reason =KVM_EXIT_TPR_ACCESS ; r =0 ;
Signature · 1 line
2.24.1
PATCH v2 17/32 KVM: SVM: Wire up ->tlb_flush_guest() directly to svm_flush_tlb()
In reply to Sean ChristophersonUse svm_flush_tlb() directly for kvm_x86_ops->tlb_flush_guest() now that the @invalidate_gpa param to ->tlb_flush() is gone, i.e. the wrapper for ->tlb_flush_guest() is no longer necessary.
No functional change intended.
--- arch/x86/kvm/svm.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
arch/x86/kvm/svm.c +1 -6
@@ -5643,11 +5643,6 @@ static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)invlpga (gva, svm->vmcb->control.asid); } -static void svm_flush_tlb_guest (struct kvm_vcpu *vcpu) -{ -svm_flush_tlb (vcpu); -} -static void svm_prepare_guest_switch (struct kvm_vcpu *vcpu) { }
@@ -7405,7 +7400,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .tlb_flush = svm_flush_tlb, .tlb_flush_gva = svm_flush_tlb_gva, - .tlb_flush_guest = svm_flush_tlb_guest, + .tlb_flush_guest = svm_flush_tlb, .run = svm_vcpu_run, .handle_exit = handle_exit,
Signature · 1 line
2.24.1
PATCH v2 18/32 KVM: VMX: Move vmx_flush_tlb() to vmx.c
In reply to Sean ChristophersonMove vmx_flush_tlb() to vmx.c and make it non-inline static now that all its callers live in vmx.c.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 25 +++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.h | 25 ------------------------- 2 files changed, 25 insertions(+), 25 deletions(-)
arch/x86/kvm/vmx/vmx.c +25 -0
@@ -2849,6 +2849,31 @@ static void exit_lmode(struct kvm_vcpu *vcpu)#endif +static void vmx_flush_tlb (struct kvm_vcpu *vcpu) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); + +/* +* Flush all EPTP/VPID contexts, as the TLB flush _may_ have been +* invoked via kvm_flush_remote_tlbs(). Flushing remote TLBs requires +* all contexts to be flushed, not just the active context. +* +* Note, this also ensures a deferred TLB flush with VPID enabled and +* EPT disabled invalidates the "correct" VPID, by nuking both L1 and +* L2's VPIDs. +*/ +if (enable_ept) { +ept_sync_global (); + }else if (enable_vpid) { +if (cpu_has_vmx_invvpid_global ()) { +vpid_sync_vcpu_global (); + }else { +vpid_sync_vcpu_single (vmx->vpid); +vpid_sync_vcpu_single (vmx->nested.vpid02); + } + } +} +static void vmx_flush_tlb_gva (struct kvm_vcpu *vcpu,gva_t addr) {/*
arch/x86/kvm/vmx/vmx.h +0 -25
@@ -503,31 +503,6 @@ static inline struct vmcs *alloc_vmcs(bool shadow)u64 construct_eptp (struct kvm_vcpu *vcpu,unsigned long root_hpa); -static inline void vmx_flush_tlb (struct kvm_vcpu *vcpu) -{ -struct vcpu_vmx *vmx =to_vmx (vcpu); - -/* -* Flush all EPTP/VPID contexts, as the TLB flush _may_ have been -* invoked via kvm_flush_remote_tlbs(). Flushing remote TLBs requires -* all contexts to be flushed, not just the active context. -* -* Note, this also ensures a deferred TLB flush with VPID enabled and -* EPT disabled invalidates the "correct" VPID, by nuking both L1 and -* L2's VPIDs. -*/ -if (enable_ept) { -ept_sync_global (); - }else if (enable_vpid) { -if (cpu_has_vmx_invvpid_global ()) { -vpid_sync_vcpu_global (); - }else { -vpid_sync_vcpu_single (vmx->vpid); -vpid_sync_vcpu_single (vmx->nested.vpid02); - } - } -} -static inline void decache_tsc_multiplier (struct vcpu_vmx *vmx) { vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
Signature · 1 line
2.24.1
PATCH v2 19/32 KVM: nVMX: Move nested_get_vpid02() to vmx/nested.h
In reply to Sean ChristophersonMove nested_get_vpid02() to vmx/nested.h so that a future patch can reference it from vmx.c to implement context-specific TLB flushing.
No functional change intended.
--- arch/x86/kvm/vmx/nested.c | 7 ------- arch/x86/kvm/vmx/nested.h | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/nested.c +0 -7
@@ -1154,13 +1154,6 @@ static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) (nested_cpu_has_vpid (vmcs12) &&to_vmx (vcpu)->nested.vpid02); } -static u16 nested_get_vpid02 (struct kvm_vcpu *vcpu) -{ -struct vcpu_vmx *vmx =to_vmx (vcpu); - -return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid; -} -static bool is_bitwise_subset (u64 superset,u64 subset,u64 mask) { superset &= mask;
arch/x86/kvm/vmx/nested.h +7 -0
@@ -60,6 +60,13 @@ static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu) vmx->nested.hv_evmcs; } +static inline u16 nested_get_vpid02 (struct kvm_vcpu *vcpu) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); + +return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid; +} +static inline unsigned long nested_ept_get_eptp (struct kvm_vcpu *vcpu) {/* return the page table to be shadowed - in our case, EPT12 */
Signature · 1 line
2.24.1
PATCH v2 20/32 KVM: VMX: Introduce vmx_flush_tlb_current()
In reply to Sean ChristophersonAdd a helper to flush TLB entries only for the current EPTP/VPID context and use it for the existing direct invocations of vmx_flush_tlb(). TLB flushes that are specific to the current vCPU state do not need to flush other contexts.
Note, both converted call sites happen to be related to the APIC access page, this is purely coincidental.
--- arch/x86/kvm/vmx/vmx.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +18 -2
@@ -2874,6 +2874,22 @@ static void vmx_flush_tlb(struct kvm_vcpu *vcpu) } } +static void vmx_flush_tlb_current (struct kvm_vcpu *vcpu) +{ +u64 root_hpa = vcpu->arch.mmu->root_hpa; + +/* No flush required if the current context is invalid. */ +if (!VALID_PAGE (root_hpa)) +return ; + +if (enable_ept) +ept_sync_context (construct_eptp (vcpu, root_hpa)); +else if (!is_guest_mode (vcpu)) +vpid_sync_context (to_vmx (vcpu)->vpid); +else +vpid_sync_context (nested_get_vpid02 (vcpu)); +} +static void vmx_flush_tlb_gva (struct kvm_vcpu *vcpu,gva_t addr) {/*
@@ -6104,7 +6120,7 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)if (flexpriority_enabled) { sec_exec_control |=SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES ; -vmx_flush_tlb (vcpu); +vmx_flush_tlb_current (vcpu); }break ;case LAPIC_MODE_X2APIC :
@@ -6122,7 +6138,7 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) {if (!is_guest_mode (vcpu)) {vmcs_write64 (APIC_ACCESS_ADDR , hpa); -vmx_flush_tlb (vcpu); +vmx_flush_tlb_current (vcpu); } }
Signature · 1 line
2.24.1
PATCH v2 21/32 KVM: SVM: Document the ASID logic in svm_flush_tlb()
In reply to Sean ChristophersonAdd a comment in svm_flush_tlb() to document why it flushes only the current ASID, even when it is invoked when flushing remote TLBs.
--- arch/x86/kvm/svm.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/svm.c +7 -0
@@ -5630,6 +5630,13 @@ static void svm_flush_tlb(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); +/* +* Flush only the current ASID even if the TLB flush was invoked via +* kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all +* ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and +* unconditionally does a TLB flush on both nested VM-Enter and nested +* VM-Exit (via kvm_mmu_reset_context()). +*/ if (static_cpu_has (X86_FEATURE_FLUSHBYASID )) svm->vmcb->control.tlb_ctl =TLB_CONTROL_FLUSH_ASID ;else
Signature · 1 line
2.24.1
PATCH v2 22/32 KVM: x86: Rename ->tlb_flush() to ->tlb_flush_all()
In reply to Sean ChristophersonRename ->tlb_flush() to ->tlb_flush_all() in preparation for adding a new hook to flush only the current ASID/context.
Opportunstically replace the comment in vmx_flush_tlb() that explains why it flushes all EPTP/VPID contexts with a comment explaining why it unconditionally uses INVEPT when EPT is enabled. I.e. rely on the "all" part of the name to clarify why it does global INVEPT/INVVPID.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 16 +++++++--------- arch/x86/kvm/x86.c | 6 +++--- 5 files changed, 13 insertions(+), 15 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1105,7 +1105,7 @@ struct kvm_x86_ops {unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags); -void (*tlb_flush)(struct kvm_vcpu *vcpu); +void (*tlb_flush_all)(struct kvm_vcpu *vcpu);int (*tlb_remote_flush)(struct kvm *kvm);int (*tlb_remote_flush_with_range)(struct kvm *kvm,struct kvm_tlb_range *range);
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -5177,7 +5177,7 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu)if (r) goto out;kvm_mmu_load_pgd (vcpu); - kvm_x86_ops->tlb_flush (vcpu); + kvm_x86_ops->tlb_flush_all (vcpu); out:return r; }
arch/x86/kvm/svm.c +1 -1
@@ -7405,7 +7405,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .get_rflags = svm_get_rflags, .set_rflags = svm_set_rflags, - .tlb_flush = svm_flush_tlb, + .tlb_flush_all = svm_flush_tlb, .tlb_flush_gva = svm_flush_tlb_gva, .tlb_flush_guest = svm_flush_tlb,
arch/x86/kvm/vmx/vmx.c +7 -9
@@ -2849,18 +2849,16 @@ static void exit_lmode(struct kvm_vcpu *vcpu)#endif -static void vmx_flush_tlb (struct kvm_vcpu *vcpu) +static void vmx_flush_tlb_all (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);/* -* Flush all EPTP/VPID contexts, as the TLB flush _may_ have been -* invoked via kvm_flush_remote_tlbs(). Flushing remote TLBs requires -* all contexts to be flushed, not just the active context. -* -* Note, this also ensures a deferred TLB flush with VPID enabled and -* EPT disabled invalidates the "correct" VPID, by nuking both L1 and -* L2's VPIDs. +* INVEPT must be issued when EPT is enabled, irrespective of VPID, as +* the CPU is not required to invalidate guest-physical mappings on +* VM-Entry, even if VPID is disabled. Guest-physical mappings are +* associated with the root EPT structure and not any particular VPID +* (INVVPID also isn't required to invalidate guest-physical mappings). */ if (enable_ept) {ept_sync_global ();
@@ -7922,7 +7920,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .get_rflags = vmx_get_rflags, .set_rflags = vmx_set_rflags, - .tlb_flush = vmx_flush_tlb, + .tlb_flush_all = vmx_flush_tlb_all, .tlb_flush_gva = vmx_flush_tlb_gva, .tlb_flush_guest = vmx_flush_tlb_guest,
arch/x86/kvm/x86.c +3 -3
@@ -2696,10 +2696,10 @@ static void kvmclock_reset(struct kvm_vcpu *vcpu) vcpu->arch.time =0 ; } -static void kvm_vcpu_flush_tlb (struct kvm_vcpu *vcpu) +static void kvm_vcpu_flush_tlb_all (struct kvm_vcpu *vcpu) { ++vcpu->stat.tlb_flush; - kvm_x86_ops->tlb_flush (vcpu); + kvm_x86_ops->tlb_flush_all (vcpu); }static void record_steal_time (struct kvm_vcpu *vcpu)
@@ -8223,7 +8223,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_LOAD_MMU_PGD , vcpu))kvm_mmu_load_pgd (vcpu);if (kvm_check_request (KVM_REQ_TLB_FLUSH , vcpu)) -kvm_vcpu_flush_tlb (vcpu); +kvm_vcpu_flush_tlb_all (vcpu);if (kvm_check_request (KVM_REQ_REPORT_TPR_ACCESS , vcpu)) { vcpu->run->exit_reason =KVM_EXIT_TPR_ACCESS ; r =0 ;
Signature · 1 line
2.24.1
PATCH v2 23/32 KVM: nVMX: Add helper to handle TLB flushes on nested VM-Enter/VM-Exit
In reply to Sean ChristophersonAdd a helper to determine whether or not a full TLB flush needs to be performed on nested VM-Enter/VM-Exit, as the logic is identical for both flows and needs a fairly beefy comment to boot. This also provides a common point to make future adjustments to the logic.
Skip the INVVPID if a TLB flush is pending, which mostly preserves the existing logic, but also skips INVVPID in the unlikely event that a TLB flush was requested for some other reason.
Remove the explicit enable_vpid from prepare_vmcs02() as its implied by nested_cpu_has_vpid(), which can return true if and only if VPID is enabled in KVM (L0).
--- arch/x86/kvm/vmx/nested.c | 83 +++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 43 deletions(-)
arch/x86/kvm/vmx/nested.c +40 -43
@@ -1154,6 +1154,31 @@ static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) (nested_cpu_has_vpid (vmcs12) &&to_vmx (vcpu)->nested.vpid02); } +static void nested_vmx_transition_tlb_flush (struct kvm_vcpu *vcpu, +struct vmcs12 *vmcs12) +{ +/* +* If VPID is disabled, linear and combined mappings are flushed on +* VM-Enter/VM-Exit, and guest-physical mappings are valid only for +* their associated EPTP. +* +* If vmcs12 doesn't use VPID, L1 expects linear and combined mappings +* for *all* contexts to be flushed on VM-Enter/VM-Exit. +* +* If VPID is enabled and used by vmc12, but L2 does not have a unique +* TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate +* a VPID for L2, flush the TLB as the effective ASID is common to both +* L1 and L2. +* +* Defer the flush so that it runs after vmcs02.EPTP has been set by +* KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid +* redundant flushes further down the nested pipeline. +*/ +if (enable_vpid && + (!nested_cpu_has_vpid (vmcs12) || !nested_has_guest_tlb_tag (vcpu))) +kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +} +static bool is_bitwise_subset (u64 superset,u64 subset,u64 mask) { superset &= mask;
@@ -2462,31 +2487,20 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,if (kvm_has_tsc_control)decache_tsc_multiplier (vmx); -if (enable_vpid) { -/* -* There is no direct mapping between vpid02 and vpid12, the -* vpid02 is per-vCPU for L0 and reused while the value of -* vpid12 is changed w/ one invvpid during nested vmentry. -* The vpid12 is allocated by L1 for L2, so it will not -* influence global bitmap(for vpid01 and vpid02 allocation) -* even if spawn a lot of nested vCPUs. -*/ -if (nested_cpu_has_vpid (vmcs12) &&nested_has_guest_tlb_tag (vcpu)) { -if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { - vmx->nested.last_vpid = vmcs12->virtual_processor_id; -vpid_sync_context (nested_get_vpid02 (vcpu)); - } - }else { -/* -* If L1 use EPT, then L0 needs to execute INVEPT on -* EPTP02 instead of EPTP01. Therefore, delay TLB -* flush until vmcs02->eptp is fully updated by -* KVM_REQ_LOAD_MMU_PGD. Note that this assumes -* KVM_REQ_TLB_FLUSH is evaluated after -* KVM_REQ_LOAD_MMU_PGD in vcpu_enter_guest(). -*/ -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); - } +nested_vmx_transition_tlb_flush (vcpu, vmcs12); + +/* +* There is no direct mapping between vpid02 and vpid12, vpid02 is +* per-vCPU and reused for all nested vCPUs. If vpid12 is changing +* then the new "virtual" VPID will reuse the same "real" VPID, +* vpid02, and so needs to be sync'd. Skip the sync if a TLB flush +* has already been requested, but always update the last used VPID. +*/ +if (nested_cpu_has_vpid (vmcs12) &&nested_has_guest_tlb_tag (vcpu) && + vmcs12->virtual_processor_id != vmx->nested.last_vpid) { + vmx->nested.last_vpid = vmcs12->virtual_processor_id; +if (!kvm_test_request (KVM_REQ_TLB_FLUSH , vcpu)) +vpid_sync_context (nested_get_vpid02 (vcpu)); }if (nested_cpu_has_ept (vmcs12))
@@ -4054,24 +4068,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,if (!enable_ept) vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; -/* -* If vmcs01 doesn't use VPID, CPU flushes TLB on every -* VMEntry/VMExit. Thus, no need to flush TLB. -* -* If vmcs12 doesn't use VPID, L1 expects TLB to be -* flushed on every VMEntry/VMExit. -* -* Otherwise, we can preserve TLB entries as long as we are -* able to tag L1 TLB entries differently than L2 TLB entries. -* -* If vmcs12 uses EPT, we need to execute this flush on EPTP01 -* and therefore we request the TLB flush to happen only after VMCS EPTP -* has been set by KVM_REQ_LOAD_MMU_PGD. -*/ -if (enable_vpid && - (!nested_cpu_has_vpid (vmcs12) || !nested_has_guest_tlb_tag (vcpu))) { -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); - } +nested_vmx_transition_tlb_flush (vcpu, vmcs12);vmcs_write32 (GUEST_SYSENTER_CS , vmcs12->host_ia32_sysenter_cs);vmcs_writel (GUEST_SYSENTER_ESP , vmcs12->host_ia32_sysenter_esp);
Signature · 1 line
2.24.1
Paolo Bonzini
maintainerRe: [PATCH v2 23/32] KVM: nVMX: Add helper to handle TLB flushes on nested VM-Enter/VM-Exit
Quoted 5 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +5
-0+nested_vmx_transition_tlb_flush (vcpu, vmcs12); + +/* +* There is no direct mapping between vpid02 and vpid12, vpid02 is +* per-vCPU and reused for all nested vCPUs. If vpid12 is changing
arch/x86/kvm/vmx/nested.c static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, · line 2504
Quoted 10 lines
arch/x86/kvm/vmx/nested.c +9
-0+* then the new "virtual" VPID will reuse the same "real" VPID, +* vpid02, and so needs to be sync'd. Skip the sync if a TLB flush +* has already been requested, but always update the last used VPID. +*/ +if (nested_cpu_has_vpid (vmcs12) &&nested_has_guest_tlb_tag (vcpu) && + vmcs12->virtual_processor_id != vmx->nested.last_vpid) { + vmx->nested.last_vpid = vmcs12->virtual_processor_id; +if (!kvm_test_request (KVM_REQ_TLB_FLUSH , vcpu)) +vpid_sync_context (nested_get_vpid02 (vcpu)); }
Would it make sense to move nested_vmx_transition_tlb_flush into an "else" branch? And should this also test that KVM_REQ_TLB_FLUSH_CURRENT is not set?
Paolo
Re: [PATCH v2 23/32] KVM: nVMX: Add helper to handle TLB flushes on nested VM-Enter/VM-Exit
Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +10
-0+nested_vmx_transition_tlb_flush (vcpu, vmcs12); + +/* +* There is no direct mapping between vpid02 and vpid12, vpid02 is +* per-vCPU and reused for all nested vCPUs. If vpid12 is changing +* then the new "virtual" VPID will reuse the same "real" VPID, +* vpid02, and so needs to be sync'd. Skip the sync if a TLB flush +* has already been requested, but always update the last used VPID. +*/ + if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu) &&
Quoted 8 lines
arch/x86/kvm/vmx/nested.c static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, · line 2504
Quoted 5 lines
arch/x86/kvm/vmx/nested.c +4
-0+ vmcs12->virtual_processor_id != vmx->nested.last_vpid) { + vmx->nested.last_vpid = vmcs12->virtual_processor_id; +if (!kvm_test_request (KVM_REQ_TLB_FLUSH , vcpu)) +vpid_sync_context (nested_get_vpid02 (vcpu)); }Would it make sense to move
nested_vmx_transition_tlb_flushinto an "else" branch?
Maybe? I tried that at one point, but didn't like making the call to nested_vmx_transition_tlb_flush() conditional. My intent is to have the ...tlb_flush() call be standalone, i.e. logic that is common to all nested transitions, so that someone can look at the code can easily (relatively speaking) understand the basic rules for TLB flushing on nested transitions.
I also tried the oppositie, i.e. putting the above code in an else-branch, with nested_vmx_transition_tlb_flush() returning true if it requested a flush. But that required updating vmx->nested.last_vpid in a separate flow, which was quite awkward.
Quoted 1 line
And should this also test that
KVM_REQ_TLB_FLUSH_CURRENTis not set?
Doh, yes.
Paolo Bonzini
maintainerRe: [PATCH v2 23/32] KVM: nVMX: Add helper to handle TLB flushes on nested VM-Enter/VM-Exit
Quoted 21 lines · Sean Christopherson
Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +10
-0+nested_vmx_transition_tlb_flush (vcpu, vmcs12); + +/* +* There is no direct mapping between vpid02 and vpid12, vpid02 is +* per-vCPU and reused for all nested vCPUs. If vpid12 is changing +* then the new "virtual" VPID will reuse the same "real" VPID, +* vpid02, and so needs to be sync'd. Skip the sync if a TLB flush +* has already been requested, but always update the last used VPID. +*/ + if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu) &&Quoted 8 lines
arch/x86/kvm/vmx/nested.c static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, · line 2504
Quoted 5 lines
arch/x86/kvm/vmx/nested.c +4
-0+ vmcs12->virtual_processor_id != vmx->nested.last_vpid) { + vmx->nested.last_vpid = vmcs12->virtual_processor_id; +if (!kvm_test_request (KVM_REQ_TLB_FLUSH , vcpu)) +vpid_sync_context (nested_get_vpid02 (vcpu)); }Would it make sense to move
nested_vmx_transition_tlb_flushinto an "else" branch?
Quoted 6 lines
Maybe? I tried that at one point, but didn't like making the call to
nested_vmx_transition_tlb_flush()conditional. My intent is to have the ...tlb_flush() call be standalone, i.e. logic that is common to all nested transitions, so that someone can look at the code can easily (relatively speaking) understand the basic rules for TLB flushing on nested transitions.
I think it's clear from the above code that we're handling a TLB flush in a way that doesn't require nested_vmx_transition_tlb_flush. But perhaps I didn't understand what you mean by "logic that is common to all nested transitions" and why you named it nested_vmx_transition_tlb_flush.
Perhaps nested_vmx_transition_tlb_flush could grow a vmentry/vmexit bool argument instead?
Quoted 4 lines
I also tried the oppositie, i.e. putting the above code in an else-branch, with
nested_vmx_transition_tlb_flush()returning true if it requested a flush. But that required updatingvmx->nested.last_vpidin a separate flow, which was quite awkward.
No, that's awkward indeed.
Paolo
Quoted 4 lines
Quoted 1 line
And should this also test that
KVM_REQ_TLB_FLUSH_CURRENTis not set?Doh, yes.
Re: [PATCH v2 23/32] KVM: nVMX: Add helper to handle TLB flushes on nested VM-Enter/VM-Exit
Quoted 2 lines · Paolo Bonzini
Perhaps
nested_vmx_transition_tlb_flushcould grow a vmentry/vmexit bool argument instead?
I'll give that a shot.
PATCH v2 24/32 KVM: x86: Introduce KVM_REQ_TLB_FLUSH_CURRENT to flush current ASID
In reply to Sean ChristophersonAdd KVM_REQ_TLB_FLUSH_CURRENT to allow optimized TLB flushing of VMX's EPTP/VPID contexts[*] from the KVM MMU and/or in a deferred manner, e.g. to flush L2's context during nested VM-Enter.
Convert KVM_REQ_TLB_FLUSH to KVM_REQ_TLB_FLUSH_CURRENT in flows where the flush is directly associated with vCPU-scoped instruction emulation, i.e. MOV CR3 and INVPCID.
Add a comment in vmx_vcpu_load_vmcs() above its KVM_REQ_TLB_FLUSH to make it clear that it deliberately requests a flush of all contexts.
Service any pending flush request on nested VM-Exit as it's possible a nested VM-Exit could occur after requesting a flush for L2. Add the same logic for nested VM-Enter even though it's _extremely_ unlikely for flush to be pending on nested VM-Enter, but theoretically possible (in the future) due to RSM (SMM) emulation.
[*] Intel also has an Address Space Identifier (ASID) concept, e.g.
EPTP+VPID+PCID == ASID, it's just not documented in the SDM because the rules of invalidation are different based on which piece of the ASID is being changed, i.e. whether the EPTP, VPID, or PCID context must be invalidated.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm.c | 1 + arch/x86/kvm/vmx/nested.c | 7 +++++++ arch/x86/kvm/vmx/vmx.c | 7 ++++++- arch/x86/kvm/x86.c | 11 +++++++++-- arch/x86/kvm/x86.h | 6 ++++++ 6 files changed, 31 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -83,6 +83,7 @@#define KVM_REQ_GET_VMCS12_PAGES KVM_ARCH_REQ(24)#define KVM_REQ_APICV_UPDATE \ KVM_ARCH_REQ_FLAGS(25, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_TLB_FLUSH_CURRENT KVM_ARCH_REQ(26)#define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
@@ -1106,6 +1107,7 @@ struct kvm_x86_ops {void (*set_rflags)(struct kvm_vcpu *vcpu,unsigned long rflags);void (*tlb_flush_all)(struct kvm_vcpu *vcpu); +void (*tlb_flush_current)(struct kvm_vcpu *vcpu);int (*tlb_remote_flush)(struct kvm *kvm);int (*tlb_remote_flush_with_range)(struct kvm *kvm,struct kvm_tlb_range *range);
arch/x86/kvm/svm.c +1 -0
@@ -7406,6 +7406,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .set_rflags = svm_set_rflags, .tlb_flush_all = svm_flush_tlb, + .tlb_flush_current = svm_flush_tlb, .tlb_flush_gva = svm_flush_tlb_gva, .tlb_flush_guest = svm_flush_tlb,
arch/x86/kvm/vmx/nested.c +7 -0
@@ -3227,6 +3227,9 @@ enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,u32 exit_reason =EXIT_REASON_INVALID_STATE ;u32 exit_qual; +if (kvm_check_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu)) +kvm_vcpu_flush_tlb_current (vcpu); + evaluate_pending_interrupts =exec_controls_get (vmx) & (CPU_BASED_INTR_WINDOW_EXITING |CPU_BASED_NMI_WINDOW_EXITING );if (likely (!evaluate_pending_interrupts) &&kvm_vcpu_apicv_active (vcpu))
@@ -4292,6 +4295,10 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,/* trying to cancel vmlaunch/vmresume is a bug */ WARN_ON_ONCE (vmx->nested.nested_run_pending); +/* Service the TLB flush request for L2 before switching to L1. */ +if (kvm_check_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu)) +kvm_vcpu_flush_tlb_current (vcpu); +leave_guest_mode (vcpu);if (nested_cpu_has_preemption_timer (vmcs12))
arch/x86/kvm/vmx/vmx.c +6 -1
@@ -1367,6 +1367,10 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu)void *gdt =get_current_gdt_ro ();unsigned long sysenter_esp; +/* +* Flush all EPTP/VPID contexts, the new pCPU may have stale +* TLB entries from its previous association with the vCPU. +*/ kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu);/*
@@ -5479,7 +5483,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu)if (kvm_get_active_pcid (vcpu) == operand.pcid) {kvm_mmu_sync_roots (vcpu); -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); }for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++)
@@ -7921,6 +7925,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .set_rflags = vmx_set_rflags, .tlb_flush_all = vmx_flush_tlb_all, + .tlb_flush_current = vmx_flush_tlb_current, .tlb_flush_gva = vmx_flush_tlb_gva, .tlb_flush_guest = vmx_flush_tlb_guest,
arch/x86/kvm/x86.c +9 -2
@@ -1033,7 +1033,7 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)if (cr3 ==kvm_read_cr3 (vcpu) && !pdptrs_changed (vcpu)) {if (!skip_tlb_flush) {kvm_mmu_sync_roots (vcpu); -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); }return 0 ; }
@@ -8222,8 +8222,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)kvm_mmu_sync_roots (vcpu);if (kvm_check_request (KVM_REQ_LOAD_MMU_PGD , vcpu))kvm_mmu_load_pgd (vcpu); -if (kvm_check_request (KVM_REQ_TLB_FLUSH , vcpu)) +if (kvm_check_request (KVM_REQ_TLB_FLUSH , vcpu)) {kvm_vcpu_flush_tlb_all (vcpu); + +/* Flushing all ASIDs flushes the current ASID... */ +kvm_clear_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); + } +if (kvm_check_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu)) +kvm_vcpu_flush_tlb_current (vcpu); +if (kvm_check_request (KVM_REQ_REPORT_TPR_ACCESS , vcpu)) { vcpu->run->exit_reason =KVM_EXIT_TPR_ACCESS ; r =0 ;
arch/x86/kvm/x86.h +6 -0
@@ -125,6 +125,12 @@ static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu; } +static inline void kvm_vcpu_flush_tlb_current (struct kvm_vcpu *vcpu) +{ + ++vcpu->stat.tlb_flush; + kvm_x86_ops->tlb_flush_current (vcpu); +} +static inline int is_pae (struct kvm_vcpu *vcpu) {return kvm_read_cr4_bits (vcpu,X86_CR4_PAE );
Signature · 1 line
2.24.1
PATCH v2 25/32 KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes
In reply to Sean ChristophersonFlush only the current ASID/context when requesting a TLB flush due to a change in the current vCPU's MMU to avoid blasting away TLB entries associated with other ASIDs/contexts, e.g. entries cached for L1 when a change in L2's MMU requires a flush.
--- arch/x86/kvm/mmu/mmu.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu/mmu.c +6 -6
@@ -2313,7 +2313,7 @@ static void kvm_mmu_flush_or_zap(struct kvm_vcpu *vcpu,return ;if (local_flush) -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); }#ifdef CONFIG_KVM_MMU_AUDIT
@@ -2520,11 +2520,11 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,break ;WARN_ON (!list_empty (&invalid_list)); -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); }if (sp->unsync_children) -kvm_make_request (KVM_REQ_MMU_SYNC , vcpu); +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu);__clear_sp_write_flooding_count (sp);trace_kvm_mmu_get_page (sp, false);
@@ -3125,7 +3125,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,if (set_spte_ret &SET_SPTE_WRITE_PROTECTED_PT ) {if (write_fault) ret =RET_PF_EMULATE ; -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); }if (set_spte_ret &SET_SPTE_NEED_REMOTE_TLB_FLUSH || flush)
@@ -4314,7 +4314,7 @@ static bool fast_cr3_switch(struct kvm_vcpu *vcpu, gpa_t new_cr3,kvm_make_request (KVM_REQ_LOAD_MMU_PGD , vcpu);if (!skip_tlb_flush) {kvm_make_request (KVM_REQ_MMU_SYNC , vcpu); -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); }/*
@@ -5177,7 +5177,7 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu)if (r) goto out;kvm_mmu_load_pgd (vcpu); - kvm_x86_ops->tlb_flush_all (vcpu); + kvm_x86_ops->tlb_flush_current (vcpu); out:return r; }
Signature · 1 line
2.24.1
PATCH v2 26/32 KVM: nVMX: Selectively use TLB_FLUSH_CURRENT for nested VM-Enter/VM-Exit
In reply to Sean ChristophersonFlush only the current context, as opposed to all contexts, when requesting a TLB flush to handle the scenario where a L1 does not expect a TLB flush, but one is required because L1 and L2 shared an ASID. This occurs if EPT is disabled (no per-EPTP tag), VPID is enabled (hardware doesn't flush unconditionally) and vmcs02 does not have its own VPID due to exhaustion of available VPIDs.
--- arch/x86/kvm/vmx/nested.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/nested.c +8 -5
@@ -1167,16 +1167,19 @@ static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,* * If VPID is enabled and used by vmc12, but L2 does not have a unique * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate -* a VPID for L2, flush the TLB as the effective ASID is common to both -* L1 and L2. +* a VPID for L2, flush the current context as the effective ASID is +* common to both L1 and L2. * * Defer the flush so that it runs after vmcs02.EPTP has been set by * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid * redundant flushes further down the nested pipeline. */ -if (enable_vpid && - (!nested_cpu_has_vpid (vmcs12) || !nested_has_guest_tlb_tag (vcpu))) -kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +if (enable_vpid) { +if (!nested_cpu_has_vpid (vmcs12)) +kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu); +else if (!nested_has_guest_tlb_tag (vcpu)) +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); + } }static bool is_bitwise_subset (u64 superset,u64 subset,u64 mask)
Signature · 1 line
2.24.1
PATCH v2 27/32 KVM: nVMX: Reload APIC access page on nested VM-Exit only if necessary
In reply to Sean ChristophersonDefer reloading L1's APIC page by logging the need for a reload and processing it during nested VM-Exit instead of unconditionally reloading the APIC page on nested VM-Exit. This eliminates a TLB flush on the majority of VM-Exits as the APIC page rarely needs to be reloaded.
--- arch/x86/kvm/vmx/nested.c | 9 ++++----- arch/x86/kvm/vmx/vmx.c | 10 +++++++--- arch/x86/kvm/vmx/vmx.h | 1 + 3 files changed, 12 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx/nested.c +4 -5
@@ -4365,11 +4365,10 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,kvm_vcpu_unmap (vcpu, &vmx->nested.pi_desc_map, true); vmx->nested.pi_desc =NULL ; -/* -* We are now running in L2, mmu_notifier will force to reload the -* page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. -*/ -kvm_make_request (KVM_REQ_APIC_PAGE_RELOAD , vcpu); +if (vmx->nested.reload_vmcs01_apic_access_page) { + vmx->nested.reload_vmcs01_apic_access_page = false; +kvm_make_request (KVM_REQ_APIC_PAGE_RELOAD , vcpu); + }if ((exit_reason !=-1 ) && (enable_shadow_vmcs || vmx->nested.hv_evmcs)) vmx->nested.need_vmcs12_to_shadow_sync = true;
arch/x86/kvm/vmx/vmx.c +7 -3
@@ -6138,10 +6138,14 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)static void vmx_set_apic_access_page_addr (struct kvm_vcpu *vcpu,hpa_t hpa) { -if (!is_guest_mode (vcpu)) { -vmcs_write64 (APIC_ACCESS_ADDR , hpa); -vmx_flush_tlb_current (vcpu); +/* Defer reload until vmcs01 is the current VMCS. */ +if (is_guest_mode (vcpu)) { +to_vmx (vcpu)->nested.reload_vmcs01_apic_access_page = true; +return ; } + +vmcs_write64 (APIC_ACCESS_ADDR , hpa); +vmx_flush_tlb_current (vcpu); }static void vmx_hwapic_isr_update (struct kvm_vcpu *vcpu,int max_isr)
arch/x86/kvm/vmx/vmx.h +1 -0
@@ -138,6 +138,7 @@ struct nested_vmx {bool vmcs02_initialized;bool change_vmcs01_virtual_apic_mode; +bool reload_vmcs01_apic_access_page;/* * Enlightened VMCS has been enabled. It does not mean that L1 has to
Signature · 1 line
2.24.1
PATCH v2 28/32 KVM: VMX: Retrieve APIC access page HPA only when necessary
In reply to Sean ChristophersonMove the retrieval of the HPA associated with L1's APIC access page into VMX code to avoid unnecessarily calling gfn_to_page(), e.g. when the vCPU is in guest mode (L2). Alternatively, the optimization logic in VMX could be mirrored into the common x86 code, but that will get ugly fast when further optimizations are introduced.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/vmx/vmx.c | 16 ++++++++++++++-- arch/x86/kvm/x86.c | 13 +------------ 3 files changed, 16 insertions(+), 15 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1154,7 +1154,7 @@ struct kvm_x86_ops {bool (*guest_apic_has_interrupt)(struct kvm_vcpu *vcpu);void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu,u64 *eoi_exit_bitmap);void (*set_virtual_apic_mode)(struct kvm_vcpu *vcpu); -void (*set_apic_access_page_addr)(struct kvm_vcpu *vcpu,hpa_t hpa); +void (*set_apic_access_page_addr)(struct kvm_vcpu *vcpu);int (*deliver_posted_interrupt)(struct kvm_vcpu *vcpu,int vector);int (*sync_pir_to_irr)(struct kvm_vcpu *vcpu);int (*set_tss_addr)(struct kvm *kvm,unsigned int addr);
arch/x86/kvm/vmx/vmx.c +14 -2
@@ -6136,16 +6136,28 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)vmx_update_msr_bitmap (vcpu); } -static void vmx_set_apic_access_page_addr (struct kvm_vcpu *vcpu,hpa_t hpa) +static void vmx_set_apic_access_page_addr (struct kvm_vcpu *vcpu) { +struct page *page; +/* Defer reload until vmcs01 is the current VMCS. */ if (is_guest_mode (vcpu)) {to_vmx (vcpu)->nested.reload_vmcs01_apic_access_page = true;return ; } -vmcs_write64 (APIC_ACCESS_ADDR , hpa); + page =gfn_to_page (vcpu->kvm,APIC_DEFAULT_PHYS_BASE >>PAGE_SHIFT ); +if (is_error_page (page)) +return ; + +vmcs_write64 (APIC_ACCESS_ADDR ,page_to_phys (page));vmx_flush_tlb_current (vcpu); + +/* +* Do not pin apic access page in memory, the MMU notifier +* will call us again if it is migrated or swapped out. +*/ +put_page (page); }static void vmx_hwapic_isr_update (struct kvm_vcpu *vcpu,int max_isr)
arch/x86/kvm/x86.c +1 -12
@@ -8157,24 +8157,13 @@ int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,void kvm_vcpu_reload_apic_access_page (struct kvm_vcpu *vcpu) { -struct page *page =NULL ; -if (!lapic_in_kernel (vcpu))return ;if (!kvm_x86_ops->set_apic_access_page_addr)return ; - page =gfn_to_page (vcpu->kvm,APIC_DEFAULT_PHYS_BASE >>PAGE_SHIFT ); -if (is_error_page (page)) -return ; - kvm_x86_ops->set_apic_access_page_addr (vcpu,page_to_phys (page)); - -/* -* Do not pin apic access page in memory, the MMU notifier -* will call us again if it is migrated or swapped out. -*/ -put_page (page); + kvm_x86_ops->set_apic_access_page_addr (vcpu); }void __kvm_request_immediate_exit (struct kvm_vcpu *vcpu)
Signature · 1 line
2.24.1
PATCH v2 29/32 KVM: VMX: Don't reload APIC access page if its control is disabled
In reply to Sean ChristophersonDon't reload the APIC access page if its control is disabled, e.g. if the guest is running with x2APIC (likely) or with the local APIC disabled (unlikely), to avoid unnecessary TLB flushes and VMWRITEs. Unconditionally reload the APIC access page and flush the TLB when the guest's virtual APIC transitions to "xAPIC enabled", as any changes to the APIC access page's mapping will not be recorded while the guest's virtual APIC is disabled.
--- arch/x86/kvm/vmx/vmx.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +13 -1
@@ -6122,7 +6122,15 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)if (flexpriority_enabled) { sec_exec_control |=SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES ; -vmx_flush_tlb_current (vcpu); +kvm_make_request (KVM_REQ_APIC_PAGE_RELOAD , vcpu); + +/* +* Flush the TLB, reloading the APIC access page will +* only do so if its physical address has changed, but +* the guest may have inserted a non-APIC mapping into +* the TLB while the APIC access page was disabled. +*/ +kvm_make_request (KVM_REQ_TLB_FLUSH_CURRENT , vcpu); }break ;case LAPIC_MODE_X2APIC :
@@ -6146,6 +6154,10 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)return ; } +if (!(secondary_exec_controls_get (to_vmx (vcpu)) & +SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES )) +return ; + page =gfn_to_page (vcpu->kvm,APIC_DEFAULT_PHYS_BASE >>PAGE_SHIFT );if (is_error_page (page))return ;
Signature · 1 line
2.24.1
PATCH v2 30/32 KVM: x86/mmu: Add module param to force TLB flush on root reuse
In reply to Sean ChristophersonAdd a module param, flush_on_reuse, to override skip_tlb_flush when performing a so called "fast cr3 switch", i.e. when reusing a cached root. The primary motiviation for the control is to provide a fallback mechanism in the event that TLB flushing bugs are exposed/introduced by upcoming changes to stop unconditionally flushing on nested VMX transitions.
--- arch/x86/kvm/mmu/mmu.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kvm/mmu/mmu.c +6 -0
@@ -78,6 +78,9 @@ module_param_cb(nx_huge_pages_recovery_ratio, &nx_huge_pages_recovery_ratio_ops, &nx_huge_pages_recovery_ratio, 0644);__MODULE_PARM_TYPE (nx_huge_pages_recovery_ratio,"uint" ); +static bool __read_mostly force_tlb_flush_on_reuse; +module_param_named (flush_on_reuse, force_tlb_flush_on_reuse, bool,0644 ); +/* * When setting this variable to true it enables Two-Dimensional-Paging * where the hardware walks 2 page tables:
@@ -4340,6 +4343,9 @@ static void __kvm_mmu_new_cr3(struct kvm_vcpu *vcpu, gpa_t new_cr3,union kvm_mmu_page_role new_role, bool skip_tlb_flush) { + if (force_tlb_flush_on_reuse) + skip_tlb_flush = false; +if (!fast_cr3_switch (vcpu, new_cr3, new_role, skip_tlb_flush))kvm_mmu_free_roots (vcpu, vcpu->arch.mmu,KVM_MMU_ROOT_CURRENT );
Signature · 1 line
2.24.1
PATCH v2 31/32 KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
In reply to Sean ChristophersonDon't flush the TLB if a cached EPTP root can be reused for nested VM-Enter/VM-Exit. Each unique EPTP constitutes a different ASID, cached roots are invalidated on eviction, and KVM invalidates all EPTP contexts on global events, e.g. mmu_notifier invalidate or memslot updates.
--- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/vmx/nested.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -5047,7 +5047,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,kvm_calc_shadow_ept_root_page_role (vcpu, accessed_dirty, execonly, level); -__kvm_mmu_new_cr3 (vcpu, new_eptp, new_role.base, false); +__kvm_mmu_new_cr3 (vcpu, new_eptp, new_role.base, true);if (new_role.as_u64 == context->mmu_role.as_u64)return ;
arch/x86/kvm/vmx/nested.c +1 -1
@@ -1123,7 +1123,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne }if (!nested_ept) -kvm_mmu_new_cr3 (vcpu, cr3, false); +kvm_mmu_new_cr3 (vcpu, cr3, enable_ept); vcpu->arch.cr3 = cr3;kvm_register_mark_available (vcpu,VCPU_EXREG_CR3 );
Signature · 1 line
2.24.1
Paolo Bonzini
maintainerRe: [PATCH v2 31/32] KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
arch/x86/kvm/vmx/nested.c static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne · line 1126
Quoted 10 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +1
-1@@ -1123,7 +1123,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne }if (!nested_ept) -kvm_mmu_new_cr3 (vcpu, cr3, false); +kvm_mmu_new_cr3 (vcpu, cr3, enable_ept);
Even if enable_ept == false, we could have already scheduled or flushed the TLB soon due to one of 1) nested_vmx_transition_tlb_flush 2) vpid_sync_context in prepare_vmcs02 3) the processor doing it for !enable_vpid.
So for !enable_ept only KVM_REQ_MMU_SYNC is needed, not KVM_REQ_TLB_FLUSH_CURRENT I think. Worth adding a TODO?
Paolo
Quoted 3 lines
arch/x86/kvm/vmx/nested.c +0
-1vcpu->arch.cr3 = cr3;kvm_register_mark_available (vcpu,VCPU_EXREG_CR3 ); -- 2.24.1
Re: [PATCH v2 31/32] KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
Quoted 17 lines · Paolo Bonzini
arch/x86/kvm/vmx/nested.c static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne · line 1126
Quoted 10 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +1
-1@@ -1123,7 +1123,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne }if (!nested_ept) -kvm_mmu_new_cr3 (vcpu, cr3, false); +kvm_mmu_new_cr3 (vcpu, cr3, enable_ept);Even if
enable_ept== false, we could have already scheduled or flushed the TLB soon due to one of 1)nested_vmx_transition_tlb_flush2)vpid_sync_contextinprepare_vmcs023) the processor doing it for !enable_vpid.
Quoted 2 lines
So for !enable_ept only
KVM_REQ_MMU_SYNCis needed, notKVM_REQ_TLB_FLUSH_CURRENTI think. Worth adding a TODO?
Now that you point it out, I think it makes sense to unconditionally pass %true here, i.e. rely 100% on nested_vmx_transition_tlb_flush() to do the right thing.
Paolo Bonzini
maintainerRe: [PATCH v2 31/32] KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Paolo Bonzini
arch/x86/kvm/vmx/nested.c static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne · line 1126
Quoted 10 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +1
-1@@ -1123,7 +1123,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne }if (!nested_ept) -kvm_mmu_new_cr3 (vcpu, cr3, false); +kvm_mmu_new_cr3 (vcpu, cr3, enable_ept);Even if
enable_ept== false, we could have already scheduled or flushed the TLB soon due to one of 1)nested_vmx_transition_tlb_flush2)vpid_sync_contextinprepare_vmcs023) the processor doing it for !enable_vpid.
Quoted 6 lines
Quoted 2 lines
So for !enable_ept only
KVM_REQ_MMU_SYNCis needed, notKVM_REQ_TLB_FLUSH_CURRENTI think. Worth adding a TODO?Now that you point it out, I think it makes sense to unconditionally pass %true here, i.e. rely 100% on
nested_vmx_transition_tlb_flush()to do the right thing.
Why doesn't it need KVM_REQ_MMU_SYNC either? All this should be in a comment as well, of course.
(All patches I didn't comment on look good).
Paolo
Re: [PATCH v2 31/32] KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
Quoted 19 lines · Paolo Bonzini
Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Paolo Bonzini
arch/x86/kvm/vmx/nested.c static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne · line 1126
Quoted 10 lines · Sean Christopherson
arch/x86/kvm/vmx/nested.c +1
-1@@ -1123,7 +1123,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne }if (!nested_ept) -kvm_mmu_new_cr3 (vcpu, cr3, false); +kvm_mmu_new_cr3 (vcpu, cr3, enable_ept);Even if
enable_ept== false, we could have already scheduled or flushed the TLB soon due to one of 1)nested_vmx_transition_tlb_flush2)vpid_sync_contextinprepare_vmcs023) the processor doing it for !enable_vpid.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
So for !enable_ept only
KVM_REQ_MMU_SYNCis needed, notKVM_REQ_TLB_FLUSH_CURRENTI think. Worth adding a TODO?Now that you point it out, I think it makes sense to unconditionally pass %true here, i.e. rely 100% on
nested_vmx_transition_tlb_flush()to do the right thing.Why doesn't it need
KVM_REQ_MMU_SYNCeither?
Hmm, so if L1 is using VPID, we're ok without a sync. Junaid's INVVPID patch earlier in this series ensures cached roots won't retain unsync'd SPTEs when L1 does INVVPID. If L1 doesn't flush L2's TLB on VM-Entry, it can't expect L2 to recognize changes in the PTEs since the last INVVPID.
Per Intel's SDM, INVLPG (and INVPCID) are only required to invalidate entries for the current VPID, i.e. virtual VPID=0 when executed by L1.
Operations that architecturally invalidate entries in the TLBs or paging-structure caches independent of VMX operation (e.g., the INVLPG and INVPCID instructions) invalidate linear mappings and combined mappings. They are required to do so only for the current VPID.
^^^^^^^^^^^^^^^^^^^^^^^^^
If L1 isn't using VPID and L0 isn't using EPT, then a sync is required as L1 would expect PTE changes to be recognized without an explicit INVLPG prior to VM-Ennter.
So something like this?
if (!nested_ept) kvm_mmu_new_cr3(vcpu, cr3, enable_ept || nested_cpu_has_vpid(vmcs12));
The KVM_REQ_TLB_FLUSH_CURRENT request would be redundant with nested_vmx_transition_tlb_flush() when VPID is enabled, and is a (big) nop when VPID is disabled. In either case the overhead is negligible. Ideally this logic would tie into nested_vmx_transition_tlb_flush() in some way, but making that happen may be wishful thinking.
Quoted 1 line
All this should be in a comment as well, of course.
Heh, in hindsight that's painfully obvious.
Paolo Bonzini
maintainerRe: [PATCH v2 31/32] KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
Quoted 5 lines · Sean Christopherson
So something like this?
if (!nested_ept) kvm_mmu_new_cr3(vcpu, cr3, enable_ept || nested_cpu_has_vpid(vmcs12));
... which is exactly nested_has_guest_tlb_tag(vcpu). Well, not exactly but it's a bug in your code above. :)
It completely makes sense to use that as the third argument, and while a comment is still needed it will be much smaller.
Paolo
Re: [PATCH v2 31/32] KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
Quoted 9 lines · Paolo Bonzini
Quoted 5 lines · Sean Christopherson
So something like this?
if (!nested_ept) kvm_mmu_new_cr3(vcpu, cr3, enable_ept || nested_cpu_has_vpid(vmcs12));... which is exactly
nested_has_guest_tlb_tag(vcpu). Well, not exactly but it's a bug in your code above. :)
I don't think it's a bug, it's intentionally different. When enable_ept=0, nested_has_guest_tlb_tag() returns true if and only if L1 has enabled VPID for L2 *and* L2 has been assigned a unique VPID by L0.
For sync purposes, whether or not L2 has been assigned a unique VPID is irrelevant. L0 needs to invalidate TLB entries to prevent resuing L1's entries (assuming L1 has been assigned a VPID), but L0 doesn't need to sync SPTEs because L2 doesn't expect them to be refreshed.
Quoted 2 lines
It completely makes sense to use that as the third argument, and while a comment is still needed it will be much smaller.
Ya, agreed.
Paolo Bonzini
maintainerRe: [PATCH v2 31/32] KVM: nVMX: Don't flush TLB on nested VM transition with EPT enabled
Quoted 8 lines · Sean Christopherson
Quoted 7 lines
Quoted 4 lines
if (!nested_ept) kvm_mmu_new_cr3(vcpu, cr3, enable_ept || nested_cpu_has_vpid(vmcs12));... which is exactly
nested_has_guest_tlb_tag(vcpu). Well, not exactly but it's a bug in your code above. :)
Quoted 8 lines
I don't think it's a bug, it's intentionally different. When enable_ept=0,
nested_has_guest_tlb_tag()returns true if and only if L1 has enabled VPID for L2 *and* L2 has been assigned a unique VPID by L0.For sync purposes, whether or not L2 has been assigned a unique VPID is irrelevant. L0 needs to invalidate TLB entries to prevent resuing L1's entries (assuming L1 has been assigned a VPID), but L0 doesn't need to sync SPTEs because L2 doesn't expect them to be refreshed.
^^
L1Yes you're right. So I would say keep your code, but we can simplify the comment. Something like:
/*
* We can skip the TLB flush if we have EPT enabled (because...) and
* also if L1 is using VPID, because then it doesn't expect SPTEs for L2
* to be refreshed.
*
* This is almost the same as nested_has_guest_tlb_tag(vcpu), but here
* we don't care if L2 has been assigned a unique VPID; L1 doesn't know,
* and will nevertheless do INVVPID to avoid reuse of stale page
* table entries.
*/
Nevertheless it's scary in that this is a potential attack vector for reusing stale L0 SPTEs, so we should make sure it's all properly commented.
Thanks,
Paolo
Quoted 4 lines
Quoted 2 lines
It completely makes sense to use that as the third argument, and while a comment is still needed it will be much smaller.
Ya, agreed.
PATCH v2 32/32 KVM: nVMX: Free only the affected contexts when emulating INVEPT
In reply to Sean ChristophersonAdd logic to handle_invept() to free only those roots that match the target EPT context when emulating a single-context INVEPT.
--- arch/x86/kvm/vmx/nested.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/nested.c +21 -5
@@ -5139,12 +5139,14 @@ static int handle_invept(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);u32 vmx_instruction_info, types; -unsigned long type; +unsigned long type, roots_to_free; +struct kvm_mmu *mmu;gva_t gva;struct x86_exception e;struct {u64 eptp, gpa; } operand; +int i; if (!(vmx->nested.msrs.secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
@@ -5176,23 +5178,37 @@ static int handle_invept(struct kvm_vcpu *vcpu)return 1 ; } + mmu = &vcpu->arch.guest_mmu; +switch (type) {case VMX_EPT_EXTENT_CONTEXT :if (!nested_vmx_check_eptp (vcpu, operand.eptp))return nested_vmx_failValid (vcpu,VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID ); -/* TODO: sync only the target EPTP context. */ - fallthrough; + roots_to_free =0 ; +if (nested_ept_root_matches (mmu->root_hpa, mmu->root_cr3, + operand.eptp)) + roots_to_free |=KVM_MMU_ROOT_CURRENT ; + +for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) { +if (nested_ept_root_matches (mmu->prev_roots[i].hpa, + mmu->prev_roots[i].cr3, + operand.eptp)) + roots_to_free |=KVM_MMU_ROOT_PREVIOUS (i); + } +break ;case VMX_EPT_EXTENT_GLOBAL : -kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu, -KVM_MMU_ROOTS_ALL ); + roots_to_free =KVM_MMU_ROOTS_ALL ;break ;default :BUG_ON (1 );break ; } +if (roots_to_free) +kvm_mmu_free_roots (vcpu, mmu, roots_to_free); +return nested_vmx_succeed (vcpu); }
Signature · 1 line
2.24.1