Commit edc8deb0 authored by Sean Christopherson's avatar Sean Christopherson
Browse files

x86/reboot: Hoist "disable virt" helpers above "emergency reboot" path



Move the various "disable virtualization" helpers above the emergency
reboot path so that emergency_reboot_disable_virtualization() can be
stubbed out in a future patch if neither KVM_INTEL nor KVM_AMD is enabled,
i.e. if there is no in-tree user of CPU virtualization.

No functional change intended.

Reviewed-by: default avatarKai Huang <kai.huang@intel.com>
Link: https://lore.kernel.org/r/20230721201859.2307736-7-seanjc@google.com


Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
parent ad93c1a7
Loading
Loading
Loading
Loading
+45 −45
Original line number Diff line number Diff line
@@ -530,6 +530,51 @@ static inline void kb_wait(void)

static inline void nmi_shootdown_cpus_on_restart(void);

/* RCU-protected callback to disable virtualization prior to reboot. */
static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback;

void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback)
{
	if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback)))
		return;

	rcu_assign_pointer(cpu_emergency_virt_callback, callback);
}
EXPORT_SYMBOL_GPL(cpu_emergency_register_virt_callback);

void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback)
{
	if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback) != callback))
		return;

	rcu_assign_pointer(cpu_emergency_virt_callback, NULL);
	synchronize_rcu();
}
EXPORT_SYMBOL_GPL(cpu_emergency_unregister_virt_callback);

/*
 * Disable virtualization, i.e. VMX or SVM, to ensure INIT is recognized during
 * reboot.  VMX blocks INIT if the CPU is post-VMXON, and SVM blocks INIT if
 * GIF=0, i.e. if the crash occurred between CLGI and STGI.
 */
void cpu_emergency_disable_virtualization(void)
{
	cpu_emergency_virt_cb *callback;

	/*
	 * IRQs must be disabled as KVM enables virtualization in hardware via
	 * function call IPIs, i.e. IRQs need to be disabled to guarantee
	 * virtualization stays disabled.
	 */
	lockdep_assert_irqs_disabled();

	rcu_read_lock();
	callback = rcu_dereference(cpu_emergency_virt_callback);
	if (callback)
		callback();
	rcu_read_unlock();
}

static void emergency_reboot_disable_virtualization(void)
{
	local_irq_disable();
@@ -786,54 +831,9 @@ void machine_crash_shutdown(struct pt_regs *regs)
}
#endif

/* RCU-protected callback to disable virtualization prior to reboot. */
static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback;

void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback)
{
	if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback)))
		return;

	rcu_assign_pointer(cpu_emergency_virt_callback, callback);
}
EXPORT_SYMBOL_GPL(cpu_emergency_register_virt_callback);

void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback)
{
	if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback) != callback))
		return;

	rcu_assign_pointer(cpu_emergency_virt_callback, NULL);
	synchronize_rcu();
}
EXPORT_SYMBOL_GPL(cpu_emergency_unregister_virt_callback);

/* This is the CPU performing the emergency shutdown work. */
int crashing_cpu = -1;

/*
 * Disable virtualization, i.e. VMX or SVM, to ensure INIT is recognized during
 * reboot.  VMX blocks INIT if the CPU is post-VMXON, and SVM blocks INIT if
 * GIF=0, i.e. if the crash occurred between CLGI and STGI.
 */
void cpu_emergency_disable_virtualization(void)
{
	cpu_emergency_virt_cb *callback;

	/*
	 * IRQs must be disabled as KVM enables virtualization in hardware via
	 * function call IPIs, i.e. IRQs need to be disabled to guarantee
	 * virtualization stays disabled.
	 */
	lockdep_assert_irqs_disabled();

	rcu_read_lock();
	callback = rcu_dereference(cpu_emergency_virt_callback);
	if (callback)
		callback();
	rcu_read_unlock();
}

#if defined(CONFIG_SMP)

static nmi_shootdown_cb shootdown_callback;