Skip to content

Commit e0ceec2

Browse files
sean-jcbonzini
authored andcommitted
KVM: Don't enable hardware after a restart/shutdown is initiated
Reject hardware enabling, i.e. VM creation, if a restart/shutdown has been initiated to avoid re-enabling hardware between kvm_reboot() and machine_{halt,power_off,restart}(). The restart case is especially problematic (for x86) as enabling VMX (or clearing GIF in KVM_RUN on SVM) blocks INIT, which results in the restart/reboot hanging as BIOS is unable to wake and rendezvous with APs. Note, this bug, and the original issue that motivated the addition of kvm_reboot(), is effectively limited to a forced reboot, e.g. `reboot -f`. In a "normal" reboot, userspace will gracefully teardown userspace before triggering the kernel reboot (modulo bugs, errors, etc), i.e. any process that might do ioctl(KVM_CREATE_VM) is long gone. Fixes: 8e1c181 ("KVM: VMX: Disable VMX when system shutdown") Signed-off-by: Sean Christopherson <seanjc@google.com> Acked-by: Marc Zyngier <maz@kernel.org> Message-Id: <20230512233127.804012-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 6735150 commit e0ceec2

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

virt/kvm/kvm_main.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5184,7 +5184,20 @@ static void hardware_disable_all(void)
51845184
static int hardware_enable_all(void)
51855185
{
51865186
atomic_t failed = ATOMIC_INIT(0);
5187-
int r = 0;
5187+
int r;
5188+
5189+
/*
5190+
* Do not enable hardware virtualization if the system is going down.
5191+
* If userspace initiated a forced reboot, e.g. reboot -f, then it's
5192+
* possible for an in-flight KVM_CREATE_VM to trigger hardware enabling
5193+
* after kvm_reboot() is called. Note, this relies on system_state
5194+
* being set _before_ kvm_reboot(), which is why KVM uses a syscore ops
5195+
* hook instead of registering a dedicated reboot notifier (the latter
5196+
* runs before system_state is updated).
5197+
*/
5198+
if (system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF ||
5199+
system_state == SYSTEM_RESTART)
5200+
return -EBUSY;
51885201

51895202
/*
51905203
* When onlining a CPU, cpu_online_mask is set before kvm_online_cpu()
@@ -5197,6 +5210,8 @@ static int hardware_enable_all(void)
51975210
cpus_read_lock();
51985211
mutex_lock(&kvm_lock);
51995212

5213+
r = 0;
5214+
52005215
kvm_usage_count++;
52015216
if (kvm_usage_count == 1) {
52025217
on_each_cpu(hardware_enable_nolock, &failed, 1);

0 commit comments

Comments
 (0)