Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/virt: Eat faults on VMXOFF in reboot flows

Silently ignore all faults on VMXOFF in the reboot flows as such faults
are all but guaranteed to be due to the CPU not being in VMX root.
Because (a) VMXOFF may be executed in NMI context, e.g. after VMXOFF but
before CR4.VMXE is cleared, (b) there's no way to query the CPU's VMX
state without faulting, and (c) the whole point is to get out of VMX
root, eating faults is the simplest way to achieve the desired behaior.

Technically, VMXOFF can fault (or fail) for other reasons, but all other
fault and failure scenarios are mode related, i.e. the kernel would have
to magically end up in RM, V86, compat mode, at CPL>0, or running with
the SMI Transfer Monitor active. The kernel is beyond hosed if any of
those scenarios are encountered; trying to do something fancy in the
error path to handle them cleanly is pointless.

Fixes: 1e9931146c74 ("x86: asm/virtext.h: add cpu_vmxoff() inline function")
Reported-by: David P. Reed <dpreed@deepplum.com>
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20201231002702.2223707-2-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Sean Christopherson and committed by
Paolo Bonzini
aec511ad b3646477

+12 -5
+12 -5
arch/x86/include/asm/virtext.h
··· 30 30 } 31 31 32 32 33 - /** Disable VMX on the current CPU 33 + /** 34 + * cpu_vmxoff() - Disable VMX on the current CPU 34 35 * 35 - * vmxoff causes a undefined-opcode exception if vmxon was not run 36 - * on the CPU previously. Only call this function if you know VMX 37 - * is enabled. 36 + * Disable VMX and clear CR4.VMXE (even if VMXOFF faults) 37 + * 38 + * Note, VMXOFF causes a #UD if the CPU is !post-VMXON, but it's impossible to 39 + * atomically track post-VMXON state, e.g. this may be called in NMI context. 40 + * Eat all faults as all other faults on VMXOFF faults are mode related, i.e. 41 + * faults are guaranteed to be due to the !post-VMXON check unless the CPU is 42 + * magically in RM, VM86, compat mode, or at CPL>0. 38 43 */ 39 44 static inline void cpu_vmxoff(void) 40 45 { 41 - asm volatile ("vmxoff"); 46 + asm_volatile_goto("1: vmxoff\n\t" 47 + _ASM_EXTABLE(1b, %l[fault]) :::: fault); 48 + fault: 42 49 cr4_clear_bits(X86_CR4_VMXE); 43 50 } 44 51