Re: [PATCH v3 07/18] x86/reboot: Hoist "disable virt" helpers above "emergency reboot" path

From: Huang, Kai
Date: Mon May 22 2023 - 09:11:32 EST


On Fri, 2023-05-12 at 16:50 -0700, Sean Christopherson wrote:
> 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.
>
> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>

Personally I think this patch can be merged to next one. Doing so may also
result in a more readable patch but I am not sure.

Anyway,

Reviewed-by: Kai Huang <kai.huang@xxxxxxxxx>

> ---
> arch/x86/kernel/reboot.c | 90 ++++++++++++++++++++--------------------
> 1 file changed, 45 insertions(+), 45 deletions(-)
>
> diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c
> index fddfea5f1d20..493d66e59a4f 100644
> --- a/arch/x86/kernel/reboot.c
> +++ b/arch/x86/kernel/reboot.c
> @@ -529,6 +529,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();
> @@ -785,54 +830,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;
> --
> 2.40.1.606.ga4b1b128d6-goog
>