]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
x86/virt/tdx: Pull kexec cache flush logic into arch/x86
authorRick Edgecombe <rick.p.edgecombe@intel.com>
Thu, 2 Apr 2026 06:32:02 +0000 (00:32 -0600)
committerDave Hansen <dave.hansen@linux.intel.com>
Mon, 27 Apr 2026 22:13:38 +0000 (15:13 -0700)
KVM tries to take care of some required cache flushing earlier in the
kexec path in order to be kind to some long standing races that can occur
later in the operation. Until recently, VMXOFF was handled within KVM.
Since VMX being enabled is required to make a SEAMCALL, it had the best
per-cpu scoped operation to plug the flushing into. So it is kicked off
from there.

This early kexec cache flushing in KVM happens via a syscore shutdown
callback. Now that VMX enablement control has moved to arch/x86, which has
grown its own syscore shutdown callback, it no longer make sense for it to
live in KVM. It fits better with the TDX enablement managing code.

In addition, future changes will add a SEAMCALL that happens immediately
before VMXOFF, which means the cache flush in KVM will be too late to
flush the cache before the last SEAMCALL. So move it to the newly added TDX
arch/x86 syscore shutdown handler.

Since tdx_cpu_flush_cache_for_kexec() is no longer needed by KVM, make it
static and remove the export. Since it is also not part of an operation
spread across disparate components, remove the redundant comments and
verbose naming.

In the existing KVM based code, CPU offline also funnels through
tdx_cpu_flush_cache_for_kexec(). Add an explicit WBINVD in
tdx_offline_cpu() as well, even though it may be redundant with WBINVD
done elsewhere during CPU offline (e.g. hlt_play_dead()). This avoids
relying on fragile code ordering for cache coherency safety.

[Vishal: add explicit WBINVD in tdx_offline_cpu()]

Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Acked-by: Kai Huang <kai.huang@intel.com>
Acked-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
Acked-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20260402-fuller_tdx_kexec_support-v3-2-34438d7094bf@intel.com
arch/x86/include/asm/tdx.h
arch/x86/kvm/vmx/tdx.c
arch/x86/virt/vmx/tdx/tdx.c

index a149740b24e8b1ef7d1dd0738254a4e8833ae223..bf83a974a0d53d0549afb15bee5ade7e8df46479 100644 (file)
@@ -226,11 +226,5 @@ static inline const char *tdx_dump_mce_info(struct mce *m) { return NULL; }
 static inline const struct tdx_sys_info *tdx_get_sysinfo(void) { return NULL; }
 #endif /* CONFIG_INTEL_TDX_HOST */
 
-#ifdef CONFIG_KEXEC_CORE
-void tdx_cpu_flush_cache_for_kexec(void);
-#else
-static inline void tdx_cpu_flush_cache_for_kexec(void) { }
-#endif
-
 #endif /* !__ASSEMBLER__ */
 #endif /* _ASM_X86_TDX_H */
index 04ce321ebdf396848b464e306b86292d0a740db0..ed12805bbb4444dfa192baace26ac4015b6a0439 100644 (file)
@@ -440,16 +440,6 @@ void tdx_disable_virtualization_cpu(void)
                tdx_flush_vp(&arg);
        }
        local_irq_restore(flags);
-
-       /*
-        * Flush cache now if kexec is possible: this is necessary to avoid
-        * having dirty private memory cachelines when the new kernel boots,
-        * but WBINVD is a relatively expensive operation and doing it during
-        * kexec can exacerbate races in native_stop_other_cpus().  Do it
-        * now, since this is a safe moment and there is going to be no more
-        * TDX activity on this CPU from this point on.
-        */
-       tdx_cpu_flush_cache_for_kexec();
 }
 
 #define TDX_SEAMCALL_RETRIES 10000
index cb9b3210ab710ff60c107e34a551b9551b25469f..1b2d854ba6648e2bbd6f1a2c3e574dc9f309f02a 100644 (file)
@@ -184,6 +184,17 @@ static int tdx_online_cpu(unsigned int cpu)
        return ret;
 }
 
+static void tdx_cpu_flush_cache(void)
+{
+       lockdep_assert_preemption_disabled();
+
+       if (!this_cpu_read(cache_state_incoherent))
+               return;
+
+       wbinvd();
+       this_cpu_write(cache_state_incoherent, false);
+}
+
 static int tdx_offline_cpu(unsigned int cpu)
 {
        int i;
@@ -220,12 +231,28 @@ static int tdx_offline_cpu(unsigned int cpu)
        return -EBUSY;
 
 done:
+       /*
+        * Flush cache on the CPU going offline to ensure no dirty
+        * cachelines of TDX private memory remain. This may be
+        * redundant with WBINVD done elsewhere during CPU offline
+        * (e.g. hlt_play_dead()), but do it explicitly for safety.
+        */
+       tdx_cpu_flush_cache();
        x86_virt_put_ref(X86_FEATURE_VMX);
        return 0;
 }
 
 static void tdx_shutdown_cpu(void *ign)
 {
+       /*
+        * Flush cache in preparation for kexec - this is necessary to avoid
+        * having dirty private memory cachelines when the new kernel boots,
+        * but WBINVD is a relatively expensive operation and doing it during
+        * kexec can exacerbate races in native_stop_other_cpus().  Do it
+        * now, since this is a safe moment and there is going to be no more
+        * TDX activity on this CPU from this point on.
+        */
+       tdx_cpu_flush_cache();
        x86_virt_put_ref(X86_FEATURE_VMX);
 }
 
@@ -1920,22 +1947,3 @@ u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, struct page *page)
        return seamcall(TDH_PHYMEM_PAGE_WBINVD, &args);
 }
 EXPORT_SYMBOL_FOR_KVM(tdh_phymem_page_wbinvd_hkid);
-
-#ifdef CONFIG_KEXEC_CORE
-void tdx_cpu_flush_cache_for_kexec(void)
-{
-       lockdep_assert_preemption_disabled();
-
-       if (!this_cpu_read(cache_state_incoherent))
-               return;
-
-       /*
-        * Private memory cachelines need to be clean at the time of
-        * kexec.  Write them back now, as the caller promises that
-        * there should be no more SEAMCALLs on this CPU.
-        */
-       wbinvd();
-       this_cpu_write(cache_state_incoherent, false);
-}
-EXPORT_SYMBOL_FOR_KVM(tdx_cpu_flush_cache_for_kexec);
-#endif