mirror of https://gitee.com/openkylin/linux.git
KVM: nVMX: add option to perform early consistency checks via H/W
KVM defers many VMX consistency checks to the CPU, ostensibly for performance reasons[1], including checks that result in VMFail (as opposed to VMExit). This behavior may be undesirable for some users since this means KVM detects certain classes of VMFail only after it has processed guest state, e.g. emulated MSR load-on-entry. Because there is a strict ordering between checks that cause VMFail and those that cause VMExit, i.e. all VMFail checks are performed before any checks that cause VMExit, we can detect (almost) all VMFail conditions via a dry run of sorts. The almost qualifier exists because some state in vmcs02 comes from L0, e.g. VPID, which means that hardware will never detect an invalid VPID in vmcs12 because it never sees said value. Software must (continue to) explicitly check such fields. After preparing vmcs02 with all state needed to pass the VMFail consistency checks, optionally do a "test" VMEnter with an invalid GUEST_RFLAGS. If the VMEnter results in a VMExit (due to bad guest state), then we can safely say that the nested VMEnter should not VMFail, i.e. any VMFail encountered in nested_vmx_vmexit() must be due to an L0 bug. GUEST_RFLAGS is used to induce VMExit as it is unconditionally loaded on all implementations of VMX, has an invalid value that is writable on a 32-bit system and its consistency check is performed relatively early in all implementations (the exact order of consistency checks is micro-architectural). Unfortunately, since the "passing" case causes a VMExit, KVM must be extra diligent to ensure that host state is restored, e.g. DR7 and RFLAGS are reset on VMExit. Failure to restore RFLAGS.IF is particularly fatal. And of course the extra VMEnter and VMExit impacts performance. The raw overhead of the early consistency checks is ~6% on modern hardware (though this could easily vary based on configuration), while the added latency observed from the L1 VMM is ~10%. The early consistency checks do not occur in a vacuum, e.g. spending more time in L0 can lead to more interrupts being serviced while emulating VMEnter, thereby increasing the latency observed by L1. Add a module param, early_consistency_checks, to provide control over whether or not VMX performs the early consistency checks. In addition to standard on/off behavior, the param accepts a value of -1, which is essentialy an "auto" setting whereby KVM does the early checks only when it thinks it's running on bare metal. When running nested, doing early checks is of dubious value since the resulting behavior is heavily dependent on L0. In the future, the "auto" setting could also be used to default to skipping the early hardware checks for certain configurations/platforms if KVM reaches a state where it has 100% coverage of VMFail conditions. [1] To my knowledge no one has implemented and tested full software emulation of the VMFail consistency checks. Until that happens, one can only speculate about the actual performance overhead of doing all VMFail consistency checks in software. Obviously any code is slower than no code, but in the grand scheme of nested virtualization it's entirely possible the overhead is negligible. Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
5a5e8a15d7
commit
52017608da
|
@ -110,6 +110,9 @@ module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
|
|||
static bool __read_mostly nested = 0;
|
||||
module_param(nested, bool, S_IRUGO);
|
||||
|
||||
static bool __read_mostly nested_early_check = 0;
|
||||
module_param(nested_early_check, bool, S_IRUGO);
|
||||
|
||||
static u64 __read_mostly host_xss;
|
||||
|
||||
static bool __read_mostly enable_pml = 1;
|
||||
|
@ -187,6 +190,7 @@ static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
|
|||
module_param(ple_window_max, uint, 0444);
|
||||
|
||||
extern const ulong vmx_return;
|
||||
extern const ulong vmx_early_consistency_check_return;
|
||||
|
||||
static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
|
||||
static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
|
||||
|
@ -11953,6 +11957,14 @@ static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
|
|||
return;
|
||||
vmx->nested.vmcs02_initialized = true;
|
||||
|
||||
/*
|
||||
* We don't care what the EPTP value is we just need to guarantee
|
||||
* it's valid so we don't get a false positive when doing early
|
||||
* consistency checks.
|
||||
*/
|
||||
if (enable_ept && nested_early_check)
|
||||
vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
|
||||
|
||||
/* All VMFUNCs are currently emulated through L0 vmexits. */
|
||||
if (cpu_has_vmx_vmfunc())
|
||||
vmcs_write64(VM_FUNCTION_CONTROL, 0);
|
||||
|
@ -12006,7 +12018,9 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
|
|||
* entry, but only if the current (host) sp changed from the value
|
||||
* we wrote last (vmx->host_rsp). This cache is no longer relevant
|
||||
* if we switch vmcs, and rather than hold a separate cache per vmcs,
|
||||
* here we just force the write to happen on entry.
|
||||
* here we just force the write to happen on entry. host_rsp will
|
||||
* also be written unconditionally by nested_vmx_check_vmentry_hw()
|
||||
* if we are doing early consistency checks via hardware.
|
||||
*/
|
||||
vmx->host_rsp = 0;
|
||||
|
||||
|
@ -12634,12 +12648,124 @@ static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
|
||||
{
|
||||
struct vcpu_vmx *vmx = to_vmx(vcpu);
|
||||
unsigned long cr3, cr4;
|
||||
|
||||
if (!nested_early_check)
|
||||
return 0;
|
||||
|
||||
if (vmx->msr_autoload.host.nr)
|
||||
vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
|
||||
if (vmx->msr_autoload.guest.nr)
|
||||
vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
|
||||
|
||||
preempt_disable();
|
||||
|
||||
vmx_prepare_switch_to_guest(vcpu);
|
||||
|
||||
/*
|
||||
* Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
|
||||
* which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
|
||||
* be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
|
||||
* there is no need to preserve other bits or save/restore the field.
|
||||
*/
|
||||
vmcs_writel(GUEST_RFLAGS, 0);
|
||||
|
||||
vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
|
||||
|
||||
cr3 = __get_current_cr3_fast();
|
||||
if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
|
||||
vmcs_writel(HOST_CR3, cr3);
|
||||
vmx->loaded_vmcs->host_state.cr3 = cr3;
|
||||
}
|
||||
|
||||
cr4 = cr4_read_shadow();
|
||||
if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
|
||||
vmcs_writel(HOST_CR4, cr4);
|
||||
vmx->loaded_vmcs->host_state.cr4 = cr4;
|
||||
}
|
||||
|
||||
vmx->__launched = vmx->loaded_vmcs->launched;
|
||||
|
||||
asm(
|
||||
/* Set HOST_RSP */
|
||||
__ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
|
||||
"mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
|
||||
|
||||
/* Check if vmlaunch of vmresume is needed */
|
||||
"cmpl $0, %c[launched](%0)\n\t"
|
||||
"je 1f\n\t"
|
||||
__ex(ASM_VMX_VMRESUME) "\n\t"
|
||||
"jmp 2f\n\t"
|
||||
"1: " __ex(ASM_VMX_VMLAUNCH) "\n\t"
|
||||
"jmp 2f\n\t"
|
||||
"2: "
|
||||
|
||||
/* Set vmx->fail accordingly */
|
||||
"setbe %c[fail](%0)\n\t"
|
||||
|
||||
".pushsection .rodata\n\t"
|
||||
".global vmx_early_consistency_check_return\n\t"
|
||||
"vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
|
||||
".popsection"
|
||||
:
|
||||
: "c"(vmx), "d"((unsigned long)HOST_RSP),
|
||||
[launched]"i"(offsetof(struct vcpu_vmx, __launched)),
|
||||
[fail]"i"(offsetof(struct vcpu_vmx, fail)),
|
||||
[host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
|
||||
: "rax", "cc", "memory"
|
||||
);
|
||||
|
||||
vmcs_writel(HOST_RIP, vmx_return);
|
||||
|
||||
preempt_enable();
|
||||
|
||||
if (vmx->msr_autoload.host.nr)
|
||||
vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
|
||||
if (vmx->msr_autoload.guest.nr)
|
||||
vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
|
||||
|
||||
if (vmx->fail) {
|
||||
WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
|
||||
VMXERR_ENTRY_INVALID_CONTROL_FIELD);
|
||||
vmx->fail = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* VMExit clears RFLAGS.IF and DR7, even on a consistency check.
|
||||
*/
|
||||
local_irq_enable();
|
||||
if (hw_breakpoint_active())
|
||||
set_debugreg(__this_cpu_read(cpu_dr7), 7);
|
||||
|
||||
/*
|
||||
* A non-failing VMEntry means we somehow entered guest mode with
|
||||
* an illegal RIP, and that's just the tip of the iceberg. There
|
||||
* is no telling what memory has been modified or what state has
|
||||
* been exposed to unknown code. Hitting this all but guarantees
|
||||
* a (very critical) hardware issue.
|
||||
*/
|
||||
WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
|
||||
VMX_EXIT_REASONS_FAILED_VMENTRY));
|
||||
|
||||
return 0;
|
||||
}
|
||||
STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
|
||||
|
||||
static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
|
||||
struct vmcs12 *vmcs12);
|
||||
|
||||
/*
|
||||
* If from_vmentry is false, this is being called from state restore (either RSM
|
||||
* or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
|
||||
+ *
|
||||
+ * Returns:
|
||||
+ * 0 - success, i.e. proceed with actual VMEnter
|
||||
+ * 1 - consistency check VMExit
|
||||
+ * -1 - consistency check VMFail
|
||||
*/
|
||||
static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
|
||||
bool from_vmentry)
|
||||
|
@ -12668,6 +12794,11 @@ static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
|
|||
if (from_vmentry) {
|
||||
nested_get_vmcs12_pages(vcpu);
|
||||
|
||||
if (nested_vmx_check_vmentry_hw(vcpu)) {
|
||||
vmx_switch_vmcs(vcpu, &vmx->vmcs01);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
|
||||
goto vmentry_fail_vmexit;
|
||||
}
|
||||
|
@ -12804,13 +12935,14 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
|
|||
* We're finally done with prerequisite checking, and can start with
|
||||
* the nested entry.
|
||||
*/
|
||||
|
||||
vmx->nested.nested_run_pending = 1;
|
||||
ret = nested_vmx_enter_non_root_mode(vcpu, true);
|
||||
if (ret) {
|
||||
vmx->nested.nested_run_pending = 0;
|
||||
vmx->nested.nested_run_pending = !ret;
|
||||
if (ret > 0)
|
||||
return 1;
|
||||
}
|
||||
else if (ret)
|
||||
return nested_vmx_failValid(vcpu,
|
||||
VMXERR_ENTRY_INVALID_CONTROL_FIELD);
|
||||
|
||||
/* Hide L1D cache contents from the nested guest. */
|
||||
vmx->vcpu.arch.l1tf_flush_l1d = true;
|
||||
|
|
Loading…
Reference in New Issue