Skip to content

Commit 6735150

Browse files
sean-jcbonzini
authored andcommitted
KVM: Use syscore_ops instead of reboot_notifier to hook restart/shutdown
Use syscore_ops.shutdown to disable hardware virtualization during a reboot instead of using the dedicated reboot_notifier so that KVM disables virtualization _after_ system_state has been updated. This will allow fixing a race in KVM's handling of a forced reboot where KVM can end up enabling hardware virtualization between kernel_restart_prepare() and machine_restart(). Rename KVM's hook to match the syscore op to avoid any possible confusion from wiring up a "reboot" helper to a "shutdown" hook (neither "shutdown nor "reboot" is completely accurate as the hook handles both). Opportunistically rewrite kvm_shutdown()'s comment to make it less VMX specific, and to explain why kvm_rebooting exists. Cc: Marc Zyngier <maz@kernel.org> Cc: Oliver Upton <oliver.upton@linux.dev> Cc: James Morse <james.morse@arm.com> Cc: Suzuki K Poulose <suzuki.poulose@arm.com> Cc: Zenghui Yu <yuzenghui@huawei.com> Cc: kvmarm@lists.linux.dev Cc: Huacai Chen <chenhuacai@kernel.org> Cc: Aleksandar Markovic <aleksandar.qemu.devel@gmail.com> Cc: Anup Patel <anup@brainfault.org> Cc: Atish Patra <atishp@atishpatra.org> Cc: kvm-riscv@lists.infradead.org Signed-off-by: Sean Christopherson <seanjc@google.com> Acked-by: Marc Zyngier <maz@kernel.org> Message-Id: <20230512233127.804012-2-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 39428f6 commit 6735150

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

virt/kvm/kvm_main.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,26 +5213,24 @@ static int hardware_enable_all(void)
52135213
return r;
52145214
}
52155215

5216-
static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
5217-
void *v)
5216+
static void kvm_shutdown(void)
52185217
{
52195218
/*
5220-
* Some (well, at least mine) BIOSes hang on reboot if
5221-
* in vmx root mode.
5222-
*
5223-
* And Intel TXT required VMX off for all cpu when system shutdown.
5219+
* Disable hardware virtualization and set kvm_rebooting to indicate
5220+
* that KVM has asynchronously disabled hardware virtualization, i.e.
5221+
* that relevant errors and exceptions aren't entirely unexpected.
5222+
* Some flavors of hardware virtualization need to be disabled before
5223+
* transferring control to firmware (to perform shutdown/reboot), e.g.
5224+
* on x86, virtualization can block INIT interrupts, which are used by
5225+
* firmware to pull APs back under firmware control. Note, this path
5226+
* is used for both shutdown and reboot scenarios, i.e. neither name is
5227+
* 100% comprehensive.
52245228
*/
52255229
pr_info("kvm: exiting hardware virtualization\n");
52265230
kvm_rebooting = true;
52275231
on_each_cpu(hardware_disable_nolock, NULL, 1);
5228-
return NOTIFY_OK;
52295232
}
52305233

5231-
static struct notifier_block kvm_reboot_notifier = {
5232-
.notifier_call = kvm_reboot,
5233-
.priority = 0,
5234-
};
5235-
52365234
static int kvm_suspend(void)
52375235
{
52385236
/*
@@ -5263,6 +5261,7 @@ static void kvm_resume(void)
52635261
static struct syscore_ops kvm_syscore_ops = {
52645262
.suspend = kvm_suspend,
52655263
.resume = kvm_resume,
5264+
.shutdown = kvm_shutdown,
52665265
};
52675266
#else /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */
52685267
static int hardware_enable_all(void)
@@ -5967,7 +5966,6 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
59675966
if (r)
59685967
return r;
59695968

5970-
register_reboot_notifier(&kvm_reboot_notifier);
59715969
register_syscore_ops(&kvm_syscore_ops);
59725970
#endif
59735971

@@ -6039,7 +6037,6 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
60396037
err_vcpu_cache:
60406038
#ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
60416039
unregister_syscore_ops(&kvm_syscore_ops);
6042-
unregister_reboot_notifier(&kvm_reboot_notifier);
60436040
cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE);
60446041
#endif
60456042
return r;
@@ -6065,7 +6062,6 @@ void kvm_exit(void)
60656062
kvm_async_pf_deinit();
60666063
#ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
60676064
unregister_syscore_ops(&kvm_syscore_ops);
6068-
unregister_reboot_notifier(&kvm_reboot_notifier);
60696065
cpuhp_remove_state_nocalls(CPUHP_AP_KVM_ONLINE);
60706066
#endif
60716067
kvm_irqfd_exit();

0 commit comments

Comments
 (0)