]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: x86: Move kvm_rebooting to x86
authorSean Christopherson <seanjc@google.com>
Sat, 14 Feb 2026 01:26:47 +0000 (17:26 -0800)
committerSean Christopherson <seanjc@google.com>
Wed, 4 Mar 2026 16:52:19 +0000 (08:52 -0800)
Move kvm_rebooting, which is only read by x86, to KVM x86 so that it can
be moved again to core x86 code.  Add a "shutdown" arch hook to facilate
setting the flag in KVM x86, along with a pile of comments to provide more
context around what KVM x86 is doing and why.

Reviewed-by: Chao Gao <chao.gao@intel.com>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Tested-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Tested-by: Sagi Shahar <sagis@google.com>
Link: https://patch.msgid.link/20260214012702.2368778-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/x86.c
arch/x86/kvm/x86.h
include/linux/kvm_host.h
virt/kvm/kvm_main.c

index a03530795707797b73c701b0c952d46d27389e8d..7ac3578e6ec005a5796744a2cc7d6a4331275b3e 100644 (file)
@@ -700,6 +700,9 @@ static void drop_user_return_notifiers(void)
                kvm_on_user_return(&msrs->urn);
 }
 
+__visible bool kvm_rebooting;
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_rebooting);
+
 /*
  * Handle a fault on a hardware virtualization (VMX or SVM) instruction.
  *
@@ -13177,6 +13180,25 @@ int kvm_arch_enable_virtualization_cpu(void)
        return 0;
 }
 
+void kvm_arch_shutdown(void)
+{
+       /*
+        * Set kvm_rebooting to indicate that KVM has asynchronously disabled
+        * hardware virtualization, i.e. that errors and/or exceptions on SVM
+        * and VMX instructions are expected and should be ignored.
+        */
+       kvm_rebooting = true;
+
+       /*
+        * Ensure kvm_rebooting is visible before IPIs are sent to other CPUs
+        * to disable virtualization.  Effectively pairs with the reception of
+        * the IPI (kvm_rebooting is read in task/exception context, but only
+        * _needs_ to be read as %true after the IPI function callback disables
+        * virtualization).
+        */
+       smp_wmb();
+}
+
 void kvm_arch_disable_virtualization_cpu(void)
 {
        kvm_x86_call(disable_virtualization_cpu)();
index 94d4f07aaaa09e9ac6c401ca18ee9cf504f69d07..b314649e5c025019ab859ed6f3efc3bb6d8130b2 100644 (file)
@@ -54,6 +54,7 @@ struct kvm_host_values {
        u64 arch_capabilities;
 };
 
+extern bool kvm_rebooting;
 void kvm_spurious_fault(void);
 
 #define SIZE_OF_MEMSLOTS_HASHTABLE \
index 34759a262b2892b54d2bef78029c06b55fff58e1..7c4ebd5210ecc09182b4a4eaa47a949376e8d21f 100644 (file)
@@ -1627,6 +1627,13 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}
 #endif
 
 #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
+/*
+ * kvm_arch_shutdown() is invoked immediately prior to forcefully disabling
+ * hardware virtualization on all CPUs via IPI function calls (in preparation
+ * for shutdown or reboot), e.g. to allow arch code to prepare for disabling
+ * virtualization while KVM may be actively running vCPUs.
+ */
+void kvm_arch_shutdown(void);
 /*
  * kvm_arch_{enable,disable}_virtualization() are called on one CPU, under
  * kvm_usage_lock, immediately after/before 0=>1 and 1=>0 transitions of
@@ -2313,7 +2320,6 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
 
 #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
 extern bool enable_virt_at_load;
-extern bool kvm_rebooting;
 #endif
 
 extern unsigned int halt_poll_ns;
index 1bc1da66b4b02bab32e75ad6414b630e4d196af4..d27bf2488b125a15efc1103147f718bcc6c87498 100644 (file)
@@ -5578,13 +5578,15 @@ bool enable_virt_at_load = true;
 module_param(enable_virt_at_load, bool, 0444);
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(enable_virt_at_load);
 
-__visible bool kvm_rebooting;
-EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_rebooting);
-
 static DEFINE_PER_CPU(bool, virtualization_enabled);
 static DEFINE_MUTEX(kvm_usage_lock);
 static int kvm_usage_count;
 
+__weak void kvm_arch_shutdown(void)
+{
+
+}
+
 __weak void kvm_arch_enable_virtualization(void)
 {
 
@@ -5638,10 +5640,9 @@ static int kvm_offline_cpu(unsigned int cpu)
 
 static void kvm_shutdown(void *data)
 {
+       kvm_arch_shutdown();
+
        /*
-        * Disable hardware virtualization and set kvm_rebooting to indicate
-        * that KVM has asynchronously disabled hardware virtualization, i.e.
-        * that relevant errors and exceptions aren't entirely unexpected.
         * Some flavors of hardware virtualization need to be disabled before
         * transferring control to firmware (to perform shutdown/reboot), e.g.
         * on x86, virtualization can block INIT interrupts, which are used by
@@ -5650,7 +5651,6 @@ static void kvm_shutdown(void *data)
         * 100% comprehensive.
         */
        pr_info("kvm: exiting hardware virtualization\n");
-       kvm_rebooting = true;
        on_each_cpu(kvm_disable_virtualization_cpu, NULL, 1);
 }