KVM: x86/tdx: Have TDX handle VMXON during bringup
Subsystems (6)
Sean Christopherson
maintainercover KVM: x86/tdx: Have TDX handle VMXON during bringup
This is a sort of middle ground between fully yanking core virtualization support out of KVM, and unconditionally doing VMXON during boot[0].
I got quite far long on rebasing some internal patches we have to extract the core virtualization bits out of KVM x86, but as I paged back in all of the things we had punted on (because they were waaay out of scope for our needs), I realized more and more that providing truly generic virtualization instrastructure is vastly different than providing infrastructure that can be shared by multiple instances of KVM (or things very similar to KVM)[1].
So while I still don't want to blindly do VMXON, I also think that trying to actually support another in-tree hypervisor, without an imminent user to drive the development, is a waste of resources, and would saddle KVM with a pile of pointless complexity.
The idea here is to extract _only_ VMXON+VMXOFF and EFER.SVME toggling. AFAIK there's no second user of SVM, i.e. no equivalent to TDX, but I wanted to keep things as symmetrical as possible.
Emphasis on "only", because leaving VMCS tracking and clearing in KVM is another key difference from Xin's series. The "light bulb" moment on that front is that TDX isn't a hypervisor, and isn't trying to be a hypervisor. Specifically, TDX should _never_ have it's own VMCSes (that are visible to the host; the TDX-Module has it's own VMCSes to do SEAMCALL/SEAMRET), and so there is simply no reason to move that functionality out of KVM.
With that out of the way, dealing with VMXON/VMXOFF and EFER.SVME is a fairly simple refcounting game.
Oh, and I didn't bother looking to see if it would work, but if TDX only needs VMXON during boot, then the TDX use of VMXON could be transient. I.e. TDX could simply blast on_each_cpu() and forego the cpuhp and syscore hooks (a non-emergency reboot during init isn't possible). I don't particuarly care what TDX does, as it's a fairly minor detail all things concerned. I went with the "harder" approach, e.g. to validate keeping the VMXON users count elevated would do the right thing with respect to CPU offlining, etc.
Lightly tested (see the hacks below to verify the TDX side appears to do what it's supposed to do), but it seems to work? Heavily RFC, e.g. the third patch in particular needs to be chunked up, I'm sure there's polishing to be done, etc.
[0] https://lore.kernel.org/all/[email protected]
[1] https://lore.kernel.org/all/[email protected]
Sean Christopherson (4):
KVM: x86: Move kvm_rebooting to x86
KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
KVM: Bury kvm_{en,dis}able_virtualization() in kvm_main.c once more
Documentation/arch/x86/tdx.rst | 26 -- arch/x86/events/intel/pt.c | 1 - arch/x86/include/asm/reboot.h | 3 - arch/x86/include/asm/tdx.h | 4 - arch/x86/include/asm/virt.h | 21 ++ arch/x86/include/asm/vmx.h | 11 + arch/x86/kernel/cpu/common.c | 2 + arch/x86/kernel/reboot.c | 11 - arch/x86/kvm/svm/svm.c | 34 +- arch/x86/kvm/svm/vmenter.S | 10 +- arch/x86/kvm/vmx/tdx.c | 190 +++--------- arch/x86/kvm/vmx/vmcs.h | 11 - arch/x86/kvm/vmx/vmenter.S | 2 +- arch/x86/kvm/vmx/vmx.c | 128 +------- arch/x86/kvm/x86.c | 18 +- arch/x86/virt/Makefile | 2 + arch/x86/virt/hw.c | 327 ++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 292 +++++++++-------- arch/x86/virt/vmx/tdx/tdx.h | 8 - arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 10 +- include/linux/kvm_host.h | 10 +- virt/kvm/kvm_main.c | 31 +- 22 files changed, 622 insertions(+), 530 deletions(-) create mode 100644 arch/x86/include/asm/virt.h create mode 100644 arch/x86/virt/hw.c
base-commit: efcebc8f7aeeba15feb1a5bde70af74d96bf1a76
Signature · 1 line
2.51.0.740.g6adb054d12-goog
Sean Christopherson
maintainerRFC PATCH 1/4 KVM: x86: Move kvm_rebooting to x86
--- arch/x86/kvm/x86.c | 13 +++++++++++++ arch/x86/kvm/x86.h | 1 + include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 14 +++++++------- 4 files changed, 22 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +13 -0
@@ -705,6 +705,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. *
@@ -13076,6 +13079,16 @@ 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 relevant errors and exceptions +* aren't entirely unexpected. +*/ + kvm_rebooting = true; +} +void kvm_arch_disable_virtualization_cpu (void ) {kvm_x86_call (disable_virtualization_cpu)();
arch/x86/kvm/x86.h +1 -0
@@ -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 \
include/linux/kvm_host.h +1 -1
@@ -1619,6 +1619,7 @@ static inline void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) {}#endif #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING +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
@@ -2300,7 +2301,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;
virt/kvm/kvm_main.c +7 -7
@@ -5571,13 +5571,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 ) {
@@ -5631,10 +5633,9 @@ static int kvm_offline_cpu(unsigned int cpu)static void kvm_shutdown (void ) { +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
@@ -5643,7 +5644,6 @@ static void kvm_shutdown(void)* 100% comprehensive. */ pr_info ("kvm: exiting hardware virtualization\n" ); - kvm_rebooting = true;on_each_cpu (kvm_disable_virtualization_cpu,NULL ,1 ); }
Signature · 1 line
2.51.0.740.g6adb054d12-goog
Sean Christopherson
maintainerRFC PATCH 2/4 KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean Christopherson--- arch/x86/events/intel/pt.c | 1 - arch/x86/include/asm/reboot.h | 3 - arch/x86/include/asm/virt.h | 21 +++ arch/x86/include/asm/vmx.h | 11 ++ arch/x86/kernel/cpu/common.c | 2 + arch/x86/kernel/reboot.c | 11 -- arch/x86/kvm/svm/svm.c | 34 +--- arch/x86/kvm/svm/vmenter.S | 10 +- arch/x86/kvm/vmx/tdx.c | 3 +- arch/x86/kvm/vmx/vmcs.h | 11 -- arch/x86/kvm/vmx/vmenter.S | 2 +- arch/x86/kvm/vmx/vmx.c | 128 +------------ arch/x86/kvm/x86.c | 15 +- arch/x86/kvm/x86.h | 1 - arch/x86/virt/Makefile | 2 + arch/x86/virt/hw.c | 327 ++++++++++++++++++++++++++++++++++ 16 files changed, 392 insertions(+), 190 deletions(-) create mode 100644 arch/x86/include/asm/virt.h create mode 100644 arch/x86/virt/hw.c
arch/x86/events/intel/pt.c +0 -1
@@ -1590,7 +1590,6 @@ void intel_pt_handle_vmx(int on)local_irq_restore (flags); } -EXPORT_SYMBOL_GPL (intel_pt_handle_vmx);/* * PMU callbacks
arch/x86/include/asm/reboot.h +0 -3
@@ -28,11 +28,8 @@ void __noreturn machine_real_restart(unsigned int type);typedef void (cpu_emergency_virt_cb )(void );#if IS_ENABLED (CONFIG_KVM_X86 )void cpu_emergency_register_virt_callback (cpu_emergency_virt_cb *callback); -void cpu_emergency_unregister_virt_callback (cpu_emergency_virt_cb *callback);void cpu_emergency_disable_virtualization (void );#else -static inline void cpu_emergency_register_virt_callback (cpu_emergency_virt_cb *callback) {} -static inline void cpu_emergency_unregister_virt_callback (cpu_emergency_virt_cb *callback) {}static inline void cpu_emergency_disable_virtualization (void ) {}#endif /* CONFIG_KVM_X86 */
arch/x86/include/asm/virt.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASM_X86_VIRT_H +#define _ASM_X86_VIRT_H + +#include <asm/reboot.h> + +#if IS_ENABLED (CONFIG_KVM_X86 ) +extern bool virt_rebooting; + +void __init x86_virt_init (void ); + +int x86_virt_get_cpu (int feat); +void x86_virt_put_cpu (int feat); + +void x86_virt_register_emergency_callback (cpu_emergency_virt_cb *callback); +void x86_virt_unregister_emergency_callback (cpu_emergency_virt_cb *callback); +#else +static __always_inline void x86_virt_init(void) {} +#endif + +#endif /* _ASM_X86_VIRT_H */
arch/x86/include/asm/vmx.h +11 -0
@@ -20,6 +20,17 @@#include <asm/trapnr.h> #include <asm/vmxfeatures.h> +struct vmcs_hdr { +u32 revision_id:31 ; +u32 shadow_vmcs:1 ; +}; + +struct vmcs { +struct vmcs_hdr hdr; +u32 abort; +char data[]; +}; +#define VMCS_CONTROL_BIT (x) BIT(VMX_FEATURE_##x & 0x1f)/*
arch/x86/kernel/cpu/common.c +2 -0
@@ -70,6 +70,7 @@#include <asm/traps.h> #include <asm/sev.h> #include <asm/tdx.h> +#include <asm/virt.h> #include <asm/posted_intr.h> #include <asm/runtime-const.h>
@@ -2119,6 +2120,7 @@ static __init void identify_boot_cpu(void)cpu_detect_tlb (&boot_cpu_data);setup_cr_pinning (); +x86_virt_init ();tsx_init ();tdx_init ();lkgs_init ();
arch/x86/kernel/reboot.c +0 -11
@@ -541,17 +541,6 @@ void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback)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
arch/x86/kvm/svm/svm.c +8 -26
@@ -44,6 +44,7 @@#include <asm/traps.h> #include <asm/reboot.h> #include <asm/fpu/api.h> +#include <asm/virt.h> #include <trace/events/ipi.h>
@@ -473,27 +474,11 @@ static __always_inline struct sev_es_save_area *sev_es_host_save_area(struct svmreturn &sd->save_area->host_sev_es_save; } -static inline void kvm_cpu_svm_disable (void ) -{ -uint64_t efer; - -wrmsrq (MSR_VM_HSAVE_PA ,0 ); -rdmsrq (MSR_EFER , efer); -if (efer &EFER_SVME ) { -/* -* Force GIF=1 prior to disabling SVM, e.g. to ensure INIT and -* NMI aren't blocked. -*/ -stgi (); -wrmsrq (MSR_EFER , efer & ~EFER_SVME ); - } -} -static void svm_emergency_disable_virtualization_cpu (void ) { - kvm_rebooting = true; + virt_rebooting = true; -kvm_cpu_svm_disable (); +wrmsrq (MSR_VM_HSAVE_PA ,0 ); }static void svm_disable_virtualization_cpu (void )
@@ -502,7 +487,7 @@ static void svm_disable_virtualization_cpu(void)if (tsc_scaling)__svm_write_tsc_multiplier (SVM_TSC_RATIO_DEFAULT ); -kvm_cpu_svm_disable (); +x86_virt_put_cpu (X86_FEATURE_SVM );amd_pmu_disable_virt (); }
@@ -511,12 +496,12 @@ static int svm_enable_virtualization_cpu(void) {struct svm_cpu_data *sd; -uint64_t efer;int me =raw_smp_processor_id (); +int r; -rdmsrq (MSR_EFER , efer); -if (efer &EFER_SVME ) -return -EBUSY ; + r =x86_virt_get_cpu (X86_FEATURE_SVM ); +if (r) +return r; sd =per_cpu_ptr (&svm_data, me); sd->asid_generation =1 ;
@@ -524,8 +509,6 @@ static int svm_enable_virtualization_cpu(void) sd->next_asid = sd->max_asid +1 ; sd->min_asid = max_sev_asid +1 ; -wrmsrq (MSR_EFER , efer |EFER_SVME ); -wrmsrq (MSR_VM_HSAVE_PA , sd->save_area_pa);if (static_cpu_has (X86_FEATURE_TSCRATEMSR )) {
@@ -536,7 +519,6 @@ static int svm_enable_virtualization_cpu(void)__svm_write_tsc_multiplier (SVM_TSC_RATIO_DEFAULT ); } -/* * Get OSVW bits. *
arch/x86/kvm/svm/vmenter.S +5 -5
@@ -269,16 +269,16 @@ SYM_FUNC_START(__svm_vcpu_run) RESTORE_GUEST_SPEC_CTRL_BODY RESTORE_HOST_SPEC_CTRL_BODY (%_ASM_SP) -10: cmpb $0, _ASM_RIP(kvm_rebooting) +10: cmpb $0, _ASM_RIP(virt_rebooting) jne 2b ud2 -30: cmpb $0, _ASM_RIP(kvm_rebooting) +30: cmpb $0, _ASM_RIP(virt_rebooting) jne 4b ud2 -50: cmpb $0, _ASM_RIP(kvm_rebooting) +50: cmpb $0, _ASM_RIP(virt_rebooting) jne 6b ud2 -70: cmpb $0, _ASM_RIP(kvm_rebooting) +70: cmpb $0, _ASM_RIP(virt_rebooting) jne 8b ud2
@@ -365,7 +365,7 @@ SYM_FUNC_START(__svm_sev_es_vcpu_run) RESTORE_GUEST_SPEC_CTRL_BODY RESTORE_HOST_SPEC_CTRL_BODY %sil -3: cmpb $0, kvm_rebooting(%rip) +3: cmpb $0, virt_rebooting(%rip) jne 2b ud2
arch/x86/kvm/vmx/tdx.c +2 -1
@@ -6,6 +6,7 @@#include <linux/misc_cgroup.h> #include <linux/mmu_context.h> #include <asm/tdx.h> +#include <asm/virt.h> #include "capabilities.h" #include "mmu.h" #include "x86_ops.h"
@@ -2069,7 +2070,7 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)* TDX_SEAMCALL_VMFAILINVALID. */ if (unlikely ((vp_enter_ret &TDX_SW_ERROR ) ==TDX_SW_ERROR )) { -KVM_BUG_ON (!kvm_rebooting, vcpu->kvm); +KVM_BUG_ON (!virt_rebooting, vcpu->kvm); goto unhandled_exit; }
arch/x86/kvm/vmx/vmcs.h +0 -11
@@ -13,17 +13,6 @@#define ROL16 (val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) -struct vmcs_hdr { -u32 revision_id:31 ; -u32 shadow_vmcs:1 ; -}; - -struct vmcs { -struct vmcs_hdr hdr; -u32 abort; -char data[]; -}; - DECLARE_PER_CPU(struct vmcs *, current_vmcs);/*
arch/x86/kvm/vmx/vmenter.S +1 -1
@@ -293,7 +293,7 @@ SYM_INNER_LABEL_ALIGN(vmx_vmexit, SYM_L_GLOBAL) RET .Lfixup: - cmpb $0, _ASM_RIP(kvm_rebooting) + cmpb $0, _ASM_RIP(virt_rebooting) jne .Lvmfail ud2 .Lvmfail:
arch/x86/kvm/vmx/vmx.c +6 -122
204 lines not on this page · raw message
arch/x86/kvm/x86.c +7 -8
@@ -84,6 +84,8 @@#include <asm/intel_pt.h> #include <asm/emulate_prefix.h> #include <asm/sgx.h> +#include <asm/virt.h> +#include <clocksource/hyperv_timer.h> #define CREATE_TRACE_POINTS
@@ -705,9 +707,6 @@ 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. *
@@ -718,7 +717,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_rebooting); noinstr void kvm_spurious_fault(void) {/* Fault while not rebooting. We want the trace. */ -BUG_ON (!kvm_rebooting); +BUG_ON (!virt_rebooting); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_spurious_fault);
@@ -12975,12 +12974,12 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_deliver_sipi_vector);void kvm_arch_enable_virtualization (void ) { -cpu_emergency_register_virt_callback (kvm_x86_ops.emergency_disable_virtualization_cpu); +x86_virt_register_emergency_callback (kvm_x86_ops.emergency_disable_virtualization_cpu); }void kvm_arch_disable_virtualization (void ) { -cpu_emergency_unregister_virt_callback (kvm_x86_ops.emergency_disable_virtualization_cpu); +x86_virt_unregister_emergency_callback (kvm_x86_ops.emergency_disable_virtualization_cpu); }int kvm_arch_enable_virtualization_cpu (void )
@@ -13082,11 +13081,11 @@ int kvm_arch_enable_virtualization_cpu(void)void kvm_arch_shutdown (void ) {/* -* Set kvm_rebooting to indicate that KVM has asynchronously disabled +* Set virt_rebooting to indicate that KVM has asynchronously disabled * hardware virtualization, i.e. that relevant errors and exceptions * aren't entirely unexpected. */ - kvm_rebooting = true; + virt_rebooting = true; }void kvm_arch_disable_virtualization_cpu (void )
arch/x86/kvm/x86.h +0 -1
@@ -54,7 +54,6 @@ struct kvm_host_values {u64 arch_capabilities; }; -extern bool kvm_rebooting;void kvm_spurious_fault (void );#define SIZE_OF_MEMSLOTS_HASHTABLE \
arch/x86/virt/Makefile +2 -0
@@ -1,2 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += svm/ vmx/ + +obj-$(subst m,y,$(CONFIG_KVM_X86)) += hw.o
arch/x86/virt/hw.c new file +327 -0
327 lines not on this page · raw message
Signature · 1 line
2.51.0.740.g6adb054d12-goog
Sean Christopherson
maintainerRFC PATCH 3/4 KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
In reply to Sean ChristophersonTODO: Split this up, write changelogs.
--- Documentation/arch/x86/tdx.rst | 26 -- arch/x86/include/asm/tdx.h | 4 - arch/x86/kvm/vmx/tdx.c | 187 +++---------- arch/x86/virt/vmx/tdx/tdx.c | 292 +++++++++++--------- arch/x86/virt/vmx/tdx/tdx.h | 8 - arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 10 +- 6 files changed, 201 insertions(+), 326 deletions(-)
Documentation/arch/x86/tdx.rst +0 -26
@@ -60,32 +60,6 @@ Besides initializing the TDX module, a per-cpu initialization SEAMCALL must be done on one cpu before any other SEAMCALLs can be made on that cpu. -The kernel provides two functions, tdx_enable() and tdx_cpu_enable() to -allow the user of TDX to enable the TDX module and enable TDX on local -cpu respectively. - -Making SEAMCALL requires VMXON has been done on that CPU. Currently only -KVM implements VMXON. For now both tdx_enable() and tdx_cpu_enable() -don't do VMXON internally (not trivial), but depends on the caller to -guarantee that. - -To enable TDX, the caller of TDX should: 1) temporarily disable CPU -hotplug; 2) do VMXON and tdx_enable_cpu() on all online cpus; 3) call -tdx_enable(). For example:: - - cpus_read_lock(); - on_each_cpu(vmxon_and_tdx_cpu_enable()); - ret = tdx_enable(); - cpus_read_unlock(); - if (ret) - goto no_tdx; - // TDX is ready to use - -And the caller of TDX must guarantee the tdx_cpu_enable() has been -successfully done on any cpu before it wants to run any other SEAMCALL. -A typical usage is do both VMXON and tdx_cpu_enable() in CPU hotplug -online callback, and refuse to online if tdx_cpu_enable() fails. - User can consult dmesg to see whether the TDX module has been initialized. If the TDX module is initialized successfully, dmesg shows something
arch/x86/include/asm/tdx.h +0 -4
@@ -122,8 +122,6 @@ static __always_inline u64 sc_retry(sc_func_t func, u64 fn,#define seamcall (_fn, _args) sc_retry(__seamcall, (_fn), (_args))#define seamcall_ret (_fn, _args) sc_retry(__seamcall_ret, (_fn), (_args))#define seamcall_saved_ret (_fn, _args) sc_retry(__seamcall_saved_ret, (_fn), (_args)) -int tdx_cpu_enable (void ); -int tdx_enable (void );const char *tdx_dump_mce_info (struct mce *m);const struct tdx_sys_info *tdx_get_sysinfo (void );
@@ -196,8 +194,6 @@ u64 tdh_phymem_page_wbinvd_tdr(struct tdx_td *td);u64 tdh_phymem_page_wbinvd_hkid (u64 hkid,struct page *page);#else static inline void tdx_init (void ) { } -static inline int tdx_cpu_enable (void ) {return -ENODEV ; } -static inline int tdx_enable (void ) {return -ENODEV ; }static inline u32 tdx_get_nr_guest_keyids (void ) {return 0 ; }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 ; }
arch/x86/kvm/vmx/tdx.c +40 -147
256 lines not on this page · raw message
arch/x86/virt/vmx/tdx/tdx.c +156 -136
548 lines not on this page · raw message
arch/x86/virt/vmx/tdx/tdx.h +0 -8
@@ -91,14 +91,6 @@ struct tdmr_info {* Do not put any hardware-defined TDX structure representations below * this comment! */ - -/* Kernel defined TDX module status during module initialization. */ -enum tdx_module_status_t { -TDX_MODULE_UNINITIALIZED , -TDX_MODULE_INITIALIZED , -TDX_MODULE_ERROR -}; -struct tdx_memblock {struct list_head list;unsigned long start_pfn;
arch/x86/virt/vmx/tdx/tdx_global_metadata.c +5 -5
@@ -7,7 +7,7 @@* Include this file to other C file instead. */ -static int get_tdx_sys_info_features (struct tdx_sys_info_features *sysinfo_features) +static __init int get_tdx_sys_info_features (struct tdx_sys_info_features *sysinfo_features) {int ret =0 ;u64 val;
@@ -18,7 +18,7 @@ static int get_tdx_sys_info_features(struct tdx_sys_info_features *sysinfo_featureturn ret; } -static int get_tdx_sys_info_tdmr (struct tdx_sys_info_tdmr *sysinfo_tdmr) +static __init int get_tdx_sys_info_tdmr (struct tdx_sys_info_tdmr *sysinfo_tdmr) {int ret =0 ;u64 val;
@@ -37,7 +37,7 @@ static int get_tdx_sys_info_tdmr(struct tdx_sys_info_tdmr *sysinfo_tdmr)return ret; } -static int get_tdx_sys_info_td_ctrl (struct tdx_sys_info_td_ctrl *sysinfo_td_ctrl) +static __init int get_tdx_sys_info_td_ctrl (struct tdx_sys_info_td_ctrl *sysinfo_td_ctrl) {int ret =0 ;u64 val;
@@ -52,7 +52,7 @@ static int get_tdx_sys_info_td_ctrl(struct tdx_sys_info_td_ctrl *sysinfo_td_ctrlreturn ret; } -static int get_tdx_sys_info_td_conf (struct tdx_sys_info_td_conf *sysinfo_td_conf) +static __init int get_tdx_sys_info_td_conf (struct tdx_sys_info_td_conf *sysinfo_td_conf) {int ret =0 ;u64 val;
@@ -85,7 +85,7 @@ static int get_tdx_sys_info_td_conf(struct tdx_sys_info_td_conf *sysinfo_td_confreturn ret; } -static int get_tdx_sys_info (struct tdx_sys_info *sysinfo) +static __init int get_tdx_sys_info (struct tdx_sys_info *sysinfo) {int ret =0 ;
Signature · 1 line
2.51.0.740.g6adb054d12-goog
Sean Christopherson
maintainerRFC PATCH 4/4 KVM: Bury kvm_{en,dis}able_virtualization() in kvm_main.c once more
In reply to Sean ChristophersonNow that TDX handles doing VMXON without KVM's involvement, bury the top-level APIs to enable and disable virtualization back in kvm_main.c.
No functional change intended.
--- include/linux/kvm_host.h | 8 -------- virt/kvm/kvm_main.c | 17 +++++++++++++---- 2 files changed, 13 insertions(+), 12 deletions(-)
include/linux/kvm_host.h +0 -8
@@ -2595,12 +2595,4 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range);#endif -#ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING -int kvm_enable_virtualization (void ); -void kvm_disable_virtualization (void ); -#else -static inline int kvm_enable_virtualization (void ) {return 0 ; } -static inline void kvm_disable_virtualization (void ) { } -#endif -#endif
virt/kvm/kvm_main.c +13 -4
@@ -1111,6 +1111,9 @@ static inline struct kvm_io_bus *kvm_get_bus_for_destruction(struct kvm *kvm, !refcount_read(&kvm->users_count)); } +static int kvm_enable_virtualization (void ); +static void kvm_disable_virtualization (void ); +static struct kvm *kvm_create_vm (unsigned long type,const char *fdname) {struct kvm *kvm =kvm_arch_alloc_vm ();
@@ -5678,7 +5681,7 @@ static struct syscore_ops kvm_syscore_ops = { .shutdown = kvm_shutdown, }; -int kvm_enable_virtualization (void ) +static int kvm_enable_virtualization (void ) {int r;
@@ -5723,9 +5726,8 @@ int kvm_enable_virtualization(void) --kvm_usage_count;return r; } -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_enable_virtualization); -void kvm_disable_virtualization (void ) +static void kvm_disable_virtualization (void ) {guard (mutex)(&kvm_usage_lock);
@@ -5736,7 +5738,6 @@ void kvm_disable_virtualization(void)cpuhp_remove_state (CPUHP_AP_KVM_ONLINE );kvm_arch_disable_virtualization (); } -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_disable_virtualization);static int kvm_init_virtualization (void ) {
@@ -5752,6 +5753,14 @@ static void kvm_uninit_virtualization(void) kvm_disable_virtualization(); }#else /* CONFIG_KVM_GENERIC_HARDWARE_ENABLING */ +static int kvm_enable_virtualization (void ) +{ +return 0 ; +} +static void kvm_disable_virtualization (void ) +{ + +}static int kvm_init_virtualization (void ) {return 0 ;
Signature · 1 line
2.51.0.740.g6adb054d12-goog
Re: [RFC PATCH 3/4] KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
In reply to Sean ChristophersonQuoted 27 lines
arch/x86/virt/vmx/tdx/tdx.c +1
-1-static int __tdx_enable (void ) +static __init int tdx_enable (void ){
arch/x86/virt/vmx/tdx/tdx.c +1
-0+enum cpuhp_state state;int ret;arch/x86/virt/vmx/tdx/tdx.c +20
-0+if (!cpu_feature_enabled (X86_FEATURE_XSAVE )) { +pr_err ("XSAVE is required for TDX\n" ); +return -EINVAL ; + } + +if (!cpu_feature_enabled (X86_FEATURE_MOVDIR64B )) { +pr_err ("MOVDIR64B is required for TDX\n" ); +return -EINVAL ; + } + +if (!cpu_feature_enabled (X86_FEATURE_SELFSNOOP )) { +pr_err ("Self-snoop is required for TDX\n" ); +return -ENODEV ; + } + + state =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"virt/tdx:online" , + tdx_online_cpu, tdx_offline_cpu); +if (state <0 ) +return state; +ret = init_tdx_module();
...
Quoted 13 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-5@@ -1445,11 +1462,6 @@ void __init tdx_init(void)return ; } -#if defined(CONFIG_ACPI ) && defined(CONFIG_SUSPEND ) -pr_info ("Disable ACPI S3. Turn off TDX in the BIOS to use ACPI S3.\n" ); - acpi_suspend_lowlevel =NULL ; -#endif -/* * Just use the first TDX KeyID as the 'global KeyID' and * leave the rest for TDX guests.
arch/x86/virt/vmx/tdx/tdx.c void __init tdx_init(void) · line 1475
Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +3
-0@@ -1458,22 +1470,30 @@ void __init tdx_init(void) tdx_guest_keyid_start = tdx_keyid_start +1 ; tdx_nr_guest_keyids = nr_tdx_keyids -1 ; + err =tdx_enable (); +if (err) + goto err_enable;
IIRC, existing TDX modules require all CPUs to have completed per-CPU initialization before TDMR/PAMT initialization.
But at this point, APs are not online, so tdx_enable() will fail here.
Re: [RFC PATCH 2/4] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean Christophersonarch/x86/virt/hw.c line 240
Quoted 12 lines
arch/x86/virt/hw.c new file +12
-0+static void x86_svm_emergency_disable_virtualization_cpu (void ) +{ + virt_rebooting = true; + +/* +* Note, CR4.VMXE can be _cleared_ in NMI context, but it can only be +* set in task context. If this races with VMX being disabled via NMI, +* VMCLEAR and VMXOFF may #UD, but the kernel will eat those faults due +* to virt_rebooting being set. +*/ +if (!(__read_cr4 () &X86_CR4_VMXE )) +return ;
copy-paste error.
Quoted 7 lines
arch/x86/virt/hw.c new file +7
-0+void __init x86_virt_init (void ) +{ +cpu_emergency_virt_cb *vmx_cb =NULL , *svm_cb =NULL ; + +if (x86_virt_is_vmx ()) + vmx_cb =x86_vmx_init (); +
arch/x86/virt/hw.c line 325
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+if (x86_virt_is_svm ()) + svm_cb =x86_svm_init (); + +if (!vmx_cb && !svm_cb) +return ; + +if (WARN_ON_ONCE (vmx_cb && svm_cb)) +return ; + +cpu_emergency_register_virt_callback (vmx_cb ? : svm_cb);
To be consistent with x86_virt_{get,put}_cpu(), perhaps we can have a common emergency callback and let reboot.c call it directly, with the common callback routing to svm/vmx code according to the hardware type.
Quoted 5 lines
arch/x86/virt/hw.c new file +2
-0+ x86_virt_initialized = true; +}Signature · 1 line
2.51.0.740.g6adb054d12-goog
Sean Christopherson
maintainerRe: [RFC PATCH 3/4] KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
In reply to Chao GaoQuoted 46 lines · Chao Gao
Quoted 27 lines
arch/x86/virt/vmx/tdx/tdx.c +1
-1-static int __tdx_enable (void ) +static __init int tdx_enable (void ){
arch/x86/virt/vmx/tdx/tdx.c +1
-0+enum cpuhp_state state;int ret;arch/x86/virt/vmx/tdx/tdx.c +20
-0+if (!cpu_feature_enabled (X86_FEATURE_XSAVE )) { +pr_err ("XSAVE is required for TDX\n" ); +return -EINVAL ; + } + +if (!cpu_feature_enabled (X86_FEATURE_MOVDIR64B )) { +pr_err ("MOVDIR64B is required for TDX\n" ); +return -EINVAL ; + } + +if (!cpu_feature_enabled (X86_FEATURE_SELFSNOOP )) { +pr_err ("Self-snoop is required for TDX\n" ); +return -ENODEV ; + } + + state =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"virt/tdx:online" , + tdx_online_cpu, tdx_offline_cpu); +if (state <0 ) +return state; +ret = init_tdx_module();...
Quoted 16 lines
arch/x86/virt/vmx/tdx/tdx.c +0
-5@@ -1445,11 +1462,6 @@ void __init tdx_init(void)return ; } -#if defined(CONFIG_ACPI ) && defined(CONFIG_SUSPEND ) -pr_info ("Disable ACPI S3. Turn off TDX in the BIOS to use ACPI S3.\n" ); - acpi_suspend_lowlevel =NULL ; -#endif -/* * Just use the first TDX KeyID as the 'global KeyID' and * leave the rest for TDX guests. @@ -1458,22 +1470,30 @@ void __init tdx_init(void) tdx_guest_keyid_start = tdx_keyid_start +1 ; tdx_nr_guest_keyids = nr_tdx_keyids -1 ;
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c void __init tdx_init(void) · line 1475
Quoted 3 lines
arch/x86/virt/vmx/tdx/tdx.c +3
-0+ err =tdx_enable (); +if (err) + goto err_enable;IIRC, existing TDX modules require all CPUs to have completed per-CPU initialization before TDMR/PAMT initialization.
But at this point, APs are not online, so
tdx_enable()will fail here.
Ah. Maybe invoke tdx_enable() through a subsys_initcall() then?
Sean Christopherson
maintainerRe: [RFC PATCH 2/4] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Chao GaoQuoted 13 lines · Chao Gao
Quoted 13 lines
arch/x86/virt/hw.c new file +13
-0+void __init x86_virt_init (void ) +{ +cpu_emergency_virt_cb *vmx_cb =NULL , *svm_cb =NULL ; + +if (x86_virt_is_vmx ()) + vmx_cb =x86_vmx_init (); + +if (x86_virt_is_svm ()) + svm_cb =x86_svm_init (); + +if (!vmx_cb && !svm_cb) +return ; +
Quoted 8 lines
arch/x86/virt/hw.c line 325
Quoted 4 lines
arch/x86/virt/hw.c new file +4
-0+if (WARN_ON_ONCE (vmx_cb && svm_cb)) +return ; + +cpu_emergency_register_virt_callback (vmx_cb ? : svm_cb);To be consistent with x86_virt_{get,put}_cpu(), perhaps we can have a common emergency callback and let
reboot.ccall it directly, with the common callback routing to svm/vmx code according to the hardware type.
Oh, yeah, that's a much better idea, especially if x86_virt_init() runs unconditionally during boot (as proposed). cpu_emergency_disable_virtualization() can be dropped entirely (it'd just be a one-line wrapper).
Edgecombe, Rick P
maintainerRe: [RFC PATCH 3/4] KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
In reply to Sean ChristophersonQuoted 10 lines · Sean Christopherson
TODO: Split this up, write changelogs.
Cc Chao Gao <[email protected]>Cc Dan Williams <[email protected]>Cc Xin Li (Intel) <[email protected]>Cc Kai Huang <[email protected]>Cc Adrian Hunter <[email protected]>Signed-off-by Sean Christopherson <[email protected]>---
[snip]
Quoted 55 lines
arch/x86/kvm/vmx/tdx.c +6
-23-static int __init __tdx_bringup (void ) {const struct tdx_sys_info_td_conf *td_conf;@@ -3462,34 +3407,18 @@ static int __init __tdx_bringup(void) } } -/* -* Enabling TDX requires enabling hardware virtualization first, -* as making SEAMCALLs requires CPU being in post-VMXON state. -*/ - r =kvm_enable_virtualization (); -if (r) -return r; - -cpus_read_lock (); - r =__do_tdx_bringup (); -cpus_read_unlock (); - -if (r) - goto tdx_bringup_err; - - r = -EINVAL ;/* Get TDX global information for later use */ tdx_sysinfo =tdx_get_sysinfo (); -if (WARN_ON_ONCE (!tdx_sysinfo)) - goto get_sysinfo_err; +if (!tdx_sysinfo) +return -EINVAL ;/* Check TDX module and KVM capabilities */ if (!tdx_get_supported_attrs (&tdx_sysinfo->td_conf) || !tdx_get_supported_xfam (&tdx_sysinfo->td_conf)) - goto get_sysinfo_err; +return -EINVAL ;if (!(tdx_sysinfo->features.tdx_features0 &MD_FIELD_ID_FEATURES0_TOPOLOGY_ENUM )) - goto get_sysinfo_err; +return -EINVAL ;/* * TDX has its own limit of maximum vCPUs it can support for all @@ -3524,34 +3453,31 @@ static int __init __tdx_bringup(void)if (td_conf->max_vcpus_per_td <num_present_cpus ()) {pr_err ("Disable TDX: MAX_VCPU_PER_TD (%u) smaller than number of logical CPUs (%u).\n" , td_conf->max_vcpus_per_td,num_present_cpus ()); - goto get_sysinfo_err; +return -EINVAL ; }if (misc_cg_set_capacity (MISC_CG_RES_TDX ,tdx_get_nr_guest_keyids ())) - goto get_sysinfo_err; +return -EINVAL ;
arch/x86/kvm/vmx/tdx.c static int __init __tdx_bringup(void) · line 3469
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +6
-2/* -* Leave hardware virtualization enabled after TDX is enabled -* successfully. TDX CPU hotplug depends on this. +* TDX-specific cpuhp callback to disallow offlining the last CPU in a +* packing while KVM is running one or more TDs. Reclaiming HKIDs +* requires doing PAGE.WBINVD on every package, i.e. offlining all CPUs +* of a package would prevent reclaiming the HKID. */ + r =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"kvm/cpu/tdx:online" , + tdx_online_cpu, tdx_offline_cpu);
Could pass NULL instead of tdx_online_cpu() and delete this version of tdx_online_cpu(). Also could remove the error handling too.
Also, can we name the two tdx_offline_cpu()'s differently? This one is all about keyid's being in use. tdx_hkid_offline_cpu()?
Quoted 29 lines
arch/x86/kvm/vmx/tdx.c +6
-13+if (r <0 ) + goto err_cpuhup; + + tdx_cpuhp_state = r;return 0 ; -get_sysinfo_err: -__tdx_cleanup (); -tdx_bringup_err: -kvm_disable_virtualization (); +err_cpuhup: +misc_cg_set_capacity (MISC_CG_RES_TDX ,0 );return r; } -void tdx_cleanup (void ) -{ -if (enable_tdx) { -misc_cg_set_capacity (MISC_CG_RES_TDX ,0 ); -__tdx_cleanup (); -kvm_disable_virtualization (); - } -} -int __init tdx_bringup (void ) {int r, i;@@ -3563,6 +3489,16 @@ int __init tdx_bringup(void)if (!enable_tdx)
arch/x86/kvm/vmx/tdx.c int __init tdx_bringup(void) · line 3499
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +8
-0return 0 ; +if (!cpu_feature_enabled (X86_FEATURE_TDX_HOST_PLATFORM )) { +pr_err ("TDX not supported by the host platform\n" ); + goto success_disable_tdx; + } + +if (!cpu_feature_enabled (X86_FEATURE_OSXSAVE )) { +pr_err ("OSXSAVE is required for TDX\n" ); +return -EINVAL ;
Why change this condition from goto success_disable_tdx?
Quoted 56 lines
arch/x86/kvm/vmx/tdx.c +2
-46+ } +if (!enable_ept) {pr_err ("EPT is required for TDX\n" ); goto success_disable_tdx;@@ -3578,61 +3514,9 @@ int __init tdx_bringup(void) goto success_disable_tdx; } -if (!cpu_feature_enabled (X86_FEATURE_OSXSAVE )) { -pr_err ("tdx: OSXSAVE is required for TDX\n" ); - goto success_disable_tdx; - } - -if (!cpu_feature_enabled (X86_FEATURE_MOVDIR64B )) { -pr_err ("tdx: MOVDIR64B is required for TDX\n" ); - goto success_disable_tdx; - } - -if (!cpu_feature_enabled (X86_FEATURE_SELFSNOOP )) { -pr_err ("Self-snoop is required for TDX\n" ); - goto success_disable_tdx; - } - -if (!cpu_feature_enabled (X86_FEATURE_TDX_HOST_PLATFORM )) { -pr_err ("tdx: no TDX private KeyIDs available\n" ); - goto success_disable_tdx; - } - -if (!enable_virt_at_load) { -pr_err ("tdx: tdx requires kvm.enable_virt_at_load=1\n" ); - goto success_disable_tdx; - } - -/* -* Ideally KVM should probe whether TDX module has been loaded -* first and then try to bring it up. But TDX needs to use SEAMCALL -* to probe whether the module is loaded (there is no CPUID or MSR -* for that), and making SEAMCALL requires enabling virtualization -* first, just like the rest steps of bringing up TDX module. -* -* So, for simplicity do everything in __tdx_bringup(); the first -* SEAMCALL will return -ENODEV when the module is not loaded. The -* only complication is having to make sure that initialization -* SEAMCALLs don't return TDX_SEAMCALL_VMFAILINVALID in other -* cases. -*/ r =__tdx_bringup (); -if (r) { -/* -* Disable TDX only but don't fail to load module if the TDX -* module could not be loaded. No need to print message saying -* "module is not loaded" because it was printed when the first -* SEAMCALL failed. Don't bother unwinding the S-EPT hooks or -* vm_size, as kvm_x86_ops have already been finalized (and are -* intentionally not exported). The S-EPT code is unreachable,
arch/x86/kvm/vmx/tdx.c int __init tdx_bringup(void) · line 3520
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +1
-7-* and allocating a few more bytes per VM in a should-be-rare -* failure scenario is a non-issue. -*/ -if (r == -ENODEV ) - goto success_disable_tdx; - +if (r) enable_tdx =0 ; - }
I think the previous discussion was that there should be a probe and enable step. We could not fail KVM init if TDX is not supported (probe), but not try to cleanly handle any other unexpected error (fail enabled).
The existing code mostly has the "probe" type checks in tdx_bringup(), and the "enable" type checks in __tdx_bringup(). But now the gutted __tdx_bringup() is very probe-y. Ideally we could separate these into named "install" and "probe" functions. I don't know if this would be good to do this as part of this series or later though.
Quoted 4 lines
(quoted diff) +0
-0return r;
[snip]
arch/x86/virt/vmx/tdx/tdx.c int tdx_cpu_enable(void) · line 231
Quoted 11 lines
/*
* Add a memory region as a TDX memory block. The caller must make sure
* all memory regions are added in address ascending order and don't
* overlap.
*/arch/x86/virt/vmx/tdx/tdx.c +3
-2-static int add_tdx_memblock (struct list_head *tmb_list,unsigned long start_pfn, -unsigned long end_pfn,int nid) +static __init int add_tdx_memblock (struct list_head *tmb_list, +unsigned long start_pfn, +unsigned long end_pfn,int nid)
One easy thing to break this up would be to do this __init adjustments in a follow on patch.
Sean Christopherson
maintainerRe: [RFC PATCH 3/4] KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
Quoted 13 lines · Rick P Edgecombe
Quoted 12 lines · Sean Christopherson
arch/x86/kvm/vmx/tdx.c +2
-2@@ -3524,34 +3453,31 @@ static int __init __tdx_bringup(void)if (td_conf->max_vcpus_per_td <num_present_cpus ()) {pr_err ("Disable TDX: MAX_VCPU_PER_TD (%u) smaller than number of logical CPUs (%u).\n" , td_conf->max_vcpus_per_td,num_present_cpus ()); - goto get_sysinfo_err; +return -EINVAL ; }if (misc_cg_set_capacity (MISC_CG_RES_TDX ,tdx_get_nr_guest_keyids ())) - goto get_sysinfo_err; +return -EINVAL ;
Quoted 13 lines
arch/x86/kvm/vmx/tdx.c static int __init __tdx_bringup(void) · line 3469
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +6
-2/* -* Leave hardware virtualization enabled after TDX is enabled -* successfully. TDX CPU hotplug depends on this. +* TDX-specific cpuhp callback to disallow offlining the last CPU in a +* packing while KVM is running one or more TDs. Reclaiming HKIDs +* requires doing PAGE.WBINVD on every package, i.e. offlining all CPUs +* of a package would prevent reclaiming the HKID. */ + r =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"kvm/cpu/tdx:online" , + tdx_online_cpu, tdx_offline_cpu);Could pass NULL instead of
tdx_online_cpu()and delete this version oftdx_online_cpu().
Oh, nice, I didn't realize (or forgot) the startup call is optional.
Quoted 1 line
Also could remove the error handling too.
No. Partly on prinicple, but also because CPUHP_AP_ONLINE_DYN can fail if the kernel runs out of dynamic entries (currently limited to 40). The kernel WARNs if it runs out of entries, but KVM should still do the right thing.
Quoted 2 lines
Also, can we name the two tdx_offline_cpu()'s differently? This one is all about keyid's being in use.
tdx_hkid_offline_cpu()?
Ya. And change the description to "kvm/cpu/tdx:hkid_packages"? Or something like that.
Quoted 31 lines
Quoted 31 lines
arch/x86/kvm/vmx/tdx.c +6
-13+if (r <0 ) + goto err_cpuhup; + + tdx_cpuhp_state = r;return 0 ; -get_sysinfo_err: -__tdx_cleanup (); -tdx_bringup_err: -kvm_disable_virtualization (); +err_cpuhup: +misc_cg_set_capacity (MISC_CG_RES_TDX ,0 );return r; } -void tdx_cleanup (void ) -{ -if (enable_tdx) { -misc_cg_set_capacity (MISC_CG_RES_TDX ,0 ); -__tdx_cleanup (); -kvm_disable_virtualization (); - } -} -int __init tdx_bringup (void ) {int r, i;@@ -3563,6 +3489,16 @@ int __init tdx_bringup(void)if (!enable_tdx)return 0 ;
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c int __init tdx_bringup(void) · line 3499
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +8
-0+if (!cpu_feature_enabled (X86_FEATURE_TDX_HOST_PLATFORM )) { +pr_err ("TDX not supported by the host platform\n" ); + goto success_disable_tdx; + } + +if (!cpu_feature_enabled (X86_FEATURE_OSXSAVE )) { +pr_err ("OSXSAVE is required for TDX\n" ); +return -EINVAL ;Why change this condition from goto
success_disable_tdx?
Ah, a copy+paste goof. I originally moved the code to tdx_enable(), then realized it as checking OSXSAVE, not XSAVE, and so needed to be done later in boot. When I copied it back to KVM, I forgot to restore the goto.
Quoted 38 lines
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +0
-7r =__tdx_bringup (); -if (r) { -/* -* Disable TDX only but don't fail to load module if the TDX -* module could not be loaded. No need to print message saying -* "module is not loaded" because it was printed when the first -* SEAMCALL failed. Don't bother unwinding the S-EPT hooks or -* vm_size, as kvm_x86_ops have already been finalized (and are arch/x86/kvm/vmx/tdx.c int __init tdx_bringup(void) · line 3635
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +1
-8-* intentionally not exported). The S-EPT code is unreachable, -* and allocating a few more bytes per VM in a should-be-rare -* failure scenario is a non-issue. -*/ -if (r == -ENODEV ) - goto success_disable_tdx; - +if (r) enable_tdx =0 ; - }I think the previous discussion was that there should be a probe and enable step. We could not fail KVM init if TDX is not supported (probe), but not try to cleanly handle any other unexpected error (fail enabled).
The existing code mostly has the "probe" type checks in
tdx_bringup(), and the "enable" type checks in__tdx_bringup(). But now the gutted__tdx_bringup()is very probe-y. Ideally we could separate these into named "install" and "probe" functions. I don't know if this would be good to do this as part of this series or later though.Quoted 4 lines
return r;
[snip]
Quoted 1 line
Quoted 13 lines
arch/x86/virt/vmx/tdx/tdx.c int tdx_cpu_enable(void) · line 231
Quoted 10 lines
/*
* Add a memory region as a TDX memory block. The caller must make sure
* all memory regions are added in address ascending order and don't
* overlap.
*/arch/x86/virt/vmx/tdx/tdx.c +3
-2-static int add_tdx_memblock (struct list_head *tmb_list,unsigned long start_pfn, -unsigned long end_pfn,int nid) +static __init int add_tdx_memblock (struct list_head *tmb_list, +unsigned long start_pfn, +unsigned long end_pfn,int nid)One easy thing to break this up would be to do this
__initadjustments in a follow on patch.
Ya, for sure.
Edgecombe, Rick P
maintainerRe: [RFC PATCH 2/4] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean ChristophersonQuoted 14 lines · Sean Christopherson
arch/x86/virt/hw.c new file +14
-0+ +int x86_virt_get_cpu (int feat) +{ +int r; + +if (!x86_virt_initialized) +return -EOPNOTSUPP ; + +if (this_cpu_inc_return (virtualization_nr_users) >1 ) +return 0 ; + +if (x86_virt_is_vmx () && feat ==X86_FEATURE_VMX ) + r =x86_vmx_get_cpu (); +else if (x86_virt_is_svm () && feat ==X86_FEATURE_SVM )
arch/x86/virt/hw.c line 290
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+ r =x86_svm_get_cpu (); + else + r = -EOPNOTSUPP ; + +if (r) +WARN_ON_ONCE (this_cpu_dec_return (virtualization_nr_users)); + +return r; +} +EXPORT_SYMBOL_GPL (x86_virt_get_cpu);
Not sure if I missed some previous discussion or future plans, but doing this via X86_FEATURE_FOO seems excessive. We could just have x86_virt_get_cpu(void) afaict? Curious if there is a plan for other things to go here?
[ Add Alexey for question below about SEV-TIO needing to enable SNP from the PSP driver? ]
Quoted 2 lines · Sean Christopherson
This is a sort of middle ground between fully yanking core virtualization support out of KVM, and unconditionally doing VMXON during boot[0].
Thanks for this, Sean!
Quoted 7 lines
I got quite far long on rebasing some internal patches we have to extract the core virtualization bits out of KVM x86, but as I paged back in all of the things we had punted on (because they were waaay out of scope for our needs), I realized more and more that providing truly generic virtualization instrastructure is vastly different than providing infrastructure that can be shared by multiple instances of KVM (or things very similar to KVM)[1].
Quoted 8 lines
So while I still don't want to blindly do VMXON, I also think that trying to actually support another in-tree hypervisor, without an imminent user to drive the development, is a waste of resources, and would saddle KVM with a pile of pointless complexity.
The idea here is to extract _only_ VMXON+VMXOFF and EFER.SVME toggling. AFAIK there's no second user of SVM, i.e. no equivalent to TDX, but I wanted to keep things as symmetrical as possible.
Alexey did mention in the TEE I/O call that the PSP driver does need to turn on SVM. Added him to the Cc to clarify if SEV-TIO needs at least SVM enabled outside of KVM in some cases.
Quoted 7 lines
Emphasis on "only", because leaving VMCS tracking and clearing in KVM is another key difference from Xin's series. The "light bulb" moment on that front is that TDX isn't a hypervisor, and isn't trying to be a hypervisor. Specifically, TDX should _never_ have it's own VMCSes (that are visible to the host; the TDX-Module has it's own VMCSes to do SEAMCALL/SEAMRET), and so there is simply no reason to move that functionality out of KVM.
Quoted 5 lines
With that out of the way, dealing with VMXON/VMXOFF and EFER.SVME is a fairly simple refcounting game.
Oh, and I didn't bother looking to see if it would work, but if TDX only needs VMXON during boot, then the TDX use of VMXON could be transient.
With the work-in-progress "Host Services", the expectation is that VMX would remain on especially because there is no current way to de-init TDX.
Now, the "TDX always-on even outside of Host Services" this series is proposing gives me slight pause. I.e. Any resources that TDX gobbles, or features that TDX is incompatible (ACPI S3), need a trip through a BIOS menu to turn off. However, if that becomes a problem in practice we can circle back later to fix that up.
Quoted 10 lines
could simply blast
on_each_cpu()and forego the cpuhp and syscore hooks (a non-emergency reboot during init isn't possible). I don't particuarly care what TDX does, as it's a fairly minor detail all things concerned. I went with the "harder" approach, e.g. to validate keeping the VMXON users count elevated would do the right thing with respect to CPU offlining, etc.Lightly tested (see the hacks below to verify the TDX side appears to do what it's supposed to do), but it seems to work? Heavily RFC, e.g. the third patch in particular needs to be chunked up, I'm sure there's polishing to be done, etc.
Sounds good and I read this as "hey, this is the form I would like to see, when someone else cleans this up and sends it back to me as a non-RFC".
Thanks again!
Sean Christopherson
maintainerQuoted 10 lines · On Mon, Oct 13, 2025, [email protected] wrote:
Quoted 10 lines
Emphasis on "only", because leaving VMCS tracking and clearing in KVM is another key difference from Xin's series. The "light bulb" moment on that front is that TDX isn't a hypervisor, and isn't trying to be a hypervisor. Specifically, TDX should _never_ have it's own VMCSes (that are visible to the host; the TDX-Module has it's own VMCSes to do SEAMCALL/SEAMRET), and so there is simply no reason to move that functionality out of KVM.
With that out of the way, dealing with VMXON/VMXOFF and EFER.SVME is a fairly simple refcounting game.
Quoted 6 lines
Quoted 2 lines
Oh, and I didn't bother looking to see if it would work, but if TDX only needs VMXON during boot, then the TDX use of VMXON could be transient.
With the work-in-progress "Host Services", the expectation is that VMX would remain on especially because there is no current way to de-init TDX.
What are Host Services?
Quoted 5 lines
Now, the "TDX always-on even outside of Host Services" this series is proposing gives me slight pause. I.e. Any resources that TDX gobbles, or features that TDX is incompatible (ACPI S3), need a trip through a BIOS menu to turn off. However, if that becomes a problem in practice we can circle back later to fix that up.
Oooh, by "TDX always-on" you mean invoking tdx_enable() during boot, as opposed to throwing it into a loadable module. To be honest, I completely missed the whole PAMT allocation and imcompatible features side of things.
And Rick already pointed out that doing tdx_enable() during tdx_init() would be far too early.
So it seems like the simple answer is to continue to have __tdx_bringup() invoke tdx_enable(), but without all the caveats about the caller needed to hold the CPUs lock, be post-VMXON, etc.
Quoted 6 lines
Quoted 6 lines
could simply blast
on_each_cpu()and forego the cpuhp and syscore hooks (a non-emergency reboot during init isn't possible). I don't particuarly care what TDX does, as it's a fairly minor detail all things concerned. I went with the "harder" approach, e.g. to validate keeping the VMXON users count elevated would do the right thing with respect to CPU offlining, etc.
Quoted 8 lines
Quoted 4 lines
Lightly tested (see the hacks below to verify the TDX side appears to do what it's supposed to do), but it seems to work? Heavily RFC, e.g. the third patch in particular needs to be chunked up, I'm sure there's polishing to be done, etc.
Sounds good and I read this as "hey, this is the form I would like to see, when someone else cleans this up and sends it back to me as a non-RFC".
Actually, I think I can take it forward. Knock wood, but I don't think there's all that much left to be done. Heck, even writing the code for the initial RFC was a pretty short adventure once I had my head wrapped around the concept.
Sean Christopherson
maintainerRe: [RFC PATCH 2/4] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Edgecombe, Rick PQuoted 22 lines · Rick P Edgecombe
Quoted 20 lines · Sean Christopherson
arch/x86/virt/hw.c new file +20
-0+ +int x86_virt_get_cpu (int feat) +{ +int r; + +if (!x86_virt_initialized) +return -EOPNOTSUPP ; + +if (this_cpu_inc_return (virtualization_nr_users) >1 ) +return 0 ; + +if (x86_virt_is_vmx () && feat ==X86_FEATURE_VMX ) + r =x86_vmx_get_cpu (); +else if (x86_virt_is_svm () && feat ==X86_FEATURE_SVM ) + r =x86_svm_get_cpu (); +else + r = -EOPNOTSUPP ; + +if (r) +WARN_ON_ONCE (this_cpu_dec_return (virtualization_nr_users));
Quoted 8 lines
arch/x86/virt/hw.c line 290
Quoted 4 lines
arch/x86/virt/hw.c new file +4
-0+ +return r; +} +EXPORT_SYMBOL_GPL (x86_virt_get_cpu);Not sure if I missed some previous discussion or future plans, but doing this via
X86_FEATURE_FOOseems excessive. We could just havex86_virt_get_cpu(void)afaict? Curious if there is a plan for other things to go here?
I want to avoid potential problems due to kvm-amd.ko doing x86_virt_get_cpu() and succeeding on an Intel CPU, and vice versa. The obvious alternative would be to have wrappers for VMX and SVM and then do whatever internally, but we'd need some form of tracking internally no matter what, and I prefer X86_FEATURE_{SVM,VMX} over one or more booleans.
FWIW, after Chao's feedback, this is what I have locally (a little less foo), where x86_virt_feature is set during x86_virt_init().
int x86_virt_get_cpu(int feat)
{
int r;
if (!x86_virt_feature || x86_virt_feature != feat)
return -EOPNOTSUPP;
if (this_cpu_inc_return(virtualization_nr_users) > 1)
return 0;
r = x86_virt_call(get_cpu);
if (r)
WARN_ON_ONCE(this_cpu_dec_return(virtualization_nr_users));
return r;
}EXPORT_SYMBOL_GPL(x86_virt_get_cpu);
Quoted 11 lines · Sean Christopherson
Quoted 10 lines · On Mon, Oct 13, 2025, [email protected] wrote:
Quoted 10 lines
Emphasis on "only", because leaving VMCS tracking and clearing in KVM is another key difference from Xin's series. The "light bulb" moment on that front is that TDX isn't a hypervisor, and isn't trying to be a hypervisor. Specifically, TDX should _never_ have it's own VMCSes (that are visible to the host; the TDX-Module has it's own VMCSes to do SEAMCALL/SEAMRET), and so there is simply no reason to move that functionality out of KVM.
With that out of the way, dealing with VMXON/VMXOFF and EFER.SVME is a fairly simple refcounting game.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Oh, and I didn't bother looking to see if it would work, but if TDX only needs VMXON during boot, then the TDX use of VMXON could be transient.
With the work-in-progress "Host Services", the expectation is that VMX would remain on especially because there is no current way to de-init TDX.
What are Host Services?
That is my catch all name for TDX things that are independent of VMs. Also called "tdx-host" in the preview patches [1]. This is capabilities like updating the TDX Module at runtime, and the TEE I/O (TDX Connect) stuff like establishing PCI device link encryption even if you never assign that device to a VM.
[1]: http://lore.kernel.org/[email protected]
Quoted 10 lines
Quoted 5 lines
Now, the "TDX always-on even outside of Host Services" this series is proposing gives me slight pause. I.e. Any resources that TDX gobbles, or features that TDX is incompatible (ACPI S3), need a trip through a BIOS menu to turn off. However, if that becomes a problem in practice we can circle back later to fix that up.
Oooh, by "TDX always-on" you mean invoking
tdx_enable()during boot, as opposed to throwing it into a loadable module. To be honest, I completely missed the whole PAMT allocation and imcompatible features side of things.
Quoted 6 lines
And Rick already pointed out that doing
tdx_enable()duringtdx_init()would be far too early.So it seems like the simple answer is to continue to have
__tdx_bringup()invoketdx_enable(), but without all the caveats about the caller needed to hold the CPUs lock, be post-VMXON, etc.
Yeah, I like the option to hold off on paying any costs until absolutely necessary.
The tdx-host driver will also be a direct tdx_enable() consumer, and it is already prepared for resolving the "multiple consumers to race to enable" case.
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
non-emergency reboot during init isn't possible). I don't particuarly care what TDX does, as it's a fairly minor detail all things concerned. I went with the "harder" approach, e.g. to validate keeping the VMXON users count elevated would do the right thing with respect to CPU offlining, etc.
Lightly tested (see the hacks below to verify the TDX side appears to do what it's supposed to do), but it seems to work? Heavily RFC, e.g. the third patch in particular needs to be chunked up, I'm sure there's polishing to be done, etc.
Quoted 7 lines
Quoted 3 lines
Sounds good and I read this as "hey, this is the form I would like to see, when someone else cleans this up and sends it back to me as a non-RFC".
Actually, I think I can take it forward. Knock wood, but I don't think there's all that much left to be done. Heck, even writing the code for the initial RFC was a pretty short adventure once I had my head wrapped around the concept.
Ack.
Quoted 21 lines · On 14/10/25 09:22, [email protected] wrote:
[ Add Alexey for question below about SEV-TIO needing to enable SNP from the PSP driver? ]
Quoted 2 lines · Sean Christopherson
This is a sort of middle ground between fully yanking core virtualization support out of KVM, and unconditionally doing VMXON during boot[0].
Thanks for this, Sean!
Quoted 12 lines
I got quite far long on rebasing some internal patches we have to extract the core virtualization bits out of KVM x86, but as I paged back in all of the things we had punted on (because they were waaay out of scope for our needs), I realized more and more that providing truly generic virtualization instrastructure is vastly different than providing infrastructure that can be shared by multiple instances of KVM (or things very similar to KVM)[1].
So while I still don't want to blindly do VMXON, I also think that trying to actually support another in-tree hypervisor, without an imminent user to drive the development, is a waste of resources, and would saddle KVM with a pile of pointless complexity.
Quoted 7 lines
Quoted 3 lines
The idea here is to extract _only_ VMXON+VMXOFF and EFER.SVME toggling. AFAIK there's no second user of SVM, i.e. no equivalent to TDX, but I wanted to keep things as symmetrical as possible.
Alexey did mention in the TEE I/O call that the PSP driver does need to turn on SVM. Added him to the Cc to clarify if SEV-TIO needs at least SVM enabled outside of KVM in some cases.
Nah, the PSP driver needs to enable those encrypted flavors of KVM (SEV, SEV-ES, SEV-SNP) in the PSP (set up memory keys or RMP) but the basic SVM does not need that, and EFER.SVME enables just it. When those SEV* are needed - KVM calls the PSP driver to enable those in the PSP, and shuts SEV* down when the last SEV* VM stops (or when kvm_amd unloads?). And the PSP cannot see EFER.SVME at any moment to act differently.
So until KVM tries VMRUN'ing a vCPU (+few more instructions), EFER.SVME does not matter afaict. Thanks,
Signature · 1 line
Alexey
Re: [RFC PATCH 3/4] KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
In reply to Sean ChristophersonQuoted 36 lines · Sean Christopherson
Quoted 13 lines · Rick P Edgecombe
Quoted 12 lines · Sean Christopherson
(quoted diff) +2
-2@@ -3524,34 +3453,31 @@ static int __init __tdx_bringup(void) if (td_conf->max_vcpus_per_td < num_present_cpus()) { pr_err("Disable TDX: MAX_VCPU_PER_TD (%u) smaller than number of logical CPUs (%u).\n", td_conf->max_vcpus_per_td, num_present_cpus()); - goto get_sysinfo_err; + return -EINVAL; } if (misc_cg_set_capacity(MISC_CG_RES_TDX, tdx_get_nr_guest_keyids())) - goto get_sysinfo_err; + return -EINVAL;Quoted 13 lines
Review static int __init __tdx_bringup(void) · line 3469
Quoted 10 lines
(quoted diff) +6
-2/* - * Leave hardware virtualization enabled after TDX is enabled - * successfully. TDX CPU hotplug depends on this. + * TDX-specific cpuhp callback to disallow offlining the last CPU in a + * packing while KVM is running one or more TDs. Reclaiming HKIDs + * requires doing PAGE.WBINVD on every package, i.e. offlining all CPUs + * of a package would prevent reclaiming the HKID. */ + r = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "kvm/cpu/tdx:online", + tdx_online_cpu, tdx_offline_cpu);Could pass NULL instead of
tdx_online_cpu()and delete this version oftdx_online_cpu().Oh, nice, I didn't realize (or forgot) the startup call is optional.
Quoted 1 line
Also could remove the error handling too.
No. Partly on prinicple, but also because
CPUHP_AP_ONLINE_DYNcan fail if the kernel runs out of dynamic entries (currently limited to 40). The kernel WARNs if it runs out of entries, but KVM should still do the right thing.
Quoted 6 lines
Quoted 2 lines
Also, can we name the two tdx_offline_cpu()'s differently? This one is all about keyid's being in use.
tdx_hkid_offline_cpu()?Ya. And change the description to "kvm/cpu/tdx:hkid_packages"? Or something like that.
Is it a good idea to consolidate the two tdx_offline_cpu() functions, i.e., integrate KVM's version into x86 core?
From 97165f9933f48d588f5390e2d543d9880c03532d Mon Sep 17 00:00:00 2001
From: Chao Gao <[email protected]>
Date: Tue, 14 Oct 2025 01:00:06 -0700
Subject: [PATCH] x86/virt/tdx: Consolidate TDX CPU hotplug handling
The core kernel registers a CPU hotplug callback to do VMX and TDX init and deinit while KVM registers a separate CPU offline callback to block offlining the last online CPU in a socket.
Splitting TDX-related CPU hotplug handling across two components is odd and adds unnecessary complexity.
Consolidate TDX-related CPU hotplug handling by integrating KVM's tdx_offline_cpu() to the one in the core kernel.
Also move nr_configured_hkid to the core kernel because tdx_offline_cpu() references it. Since HKID allocation and free are handled in the core kernel, it's more natural to track used HKIDs there.
--- arch/x86/kvm/vmx/tdx.c | 67 +------------------------------------ arch/x86/virt/vmx/tdx/tdx.c | 49 +++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 69 deletions(-)
arch/x86/kvm/vmx/tdx.c +0 -4
@@ -46,8 +46,6 @@ module_param_named(tdx, enable_tdx, bool, 0444);#define TDX_SHARED_BIT_PWL_5 gpa_to_gfn(BIT_ULL(51))#define TDX_SHARED_BIT_PWL_4 gpa_to_gfn(BIT_ULL(47)) -static enum cpuhp_state tdx_cpuhp_state__ro_after_init ; -static const struct tdx_sys_info *tdx_sysinfo;void tdh_vp_rd_failed (struct vcpu_tdx *tdx,char *uclass,u32 field,u64 err)
@@ -206,8 +204,6 @@ static int init_kvm_tdx_caps(const struct tdx_sys_info_td_conf *td_conf,*/ static DEFINE_MUTEX (tdx_lock); -static atomic_t nr_configured_hkid; -static bool tdx_operand_busy (u64 err) {
return (err & TDX_SEAMCALL_STATUS_MASK) == TDX_OPERAND_BUSY;
@@ -255,7 +251,6 @@ static inline void tdx_hkid_free(struct kvm_tdx *kvm_tdx)
{
tdx_guest_keyid_free(kvm_tdx->hkid);
kvm_tdx->hkid = -1;
- atomic_dec(&nr_configured_hkid);
misc_cg_uncharge(MISC_CG_RES_TDX, kvm_tdx->misc_cg, 1);
put_misc_cg(kvm_tdx->misc_cg);
kvm_tdx->misc_cg = NULL;@@ -2487,8 +2482,6 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
ret = -ENOMEM;
- atomic_inc(&nr_configured_hkid);
-
tdr_page = alloc_page(GFP_KERNEL); if (!tdr_page) goto free_hkid; @@ -3343,51 +3336,10 @@ int tdx_gmem_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, bool is_private) return PG_LEVEL_4K;
}
-static int tdx_online_cpu(unsigned int cpu)
-{
- return 0;
-}
-
-static int tdx_offline_cpu(unsigned int cpu)
-{
- int i;
-
- /* No TD is running. Allow any cpu to be offline. */
- if (!atomic_read(&nr_configured_hkid))
- return 0;
-
- /*
- * In order to reclaim TDX HKID, (i.e. when deleting guest TD), need to
- * call TDH.PHYMEM.PAGE.WBINVD on all packages to program all memory
- * controller with pconfig. If we have active TDX HKID, refuse to
- * offline the last online cpu.
- */
- for_each_online_cpu(i) {
- /*
- * Found another online cpu on the same package.
- * Allow to offline.
- */
- if (i != cpu && topology_physical_package_id(i) ==
- topology_physical_package_id(cpu))
- return 0;
- }
-
- /*
- * This is the last cpu of this package. Don't offline it.
- *
- * Because it's hard for human operator to understand the
- * reason, warn it.
- */
-#define MSG_ALLPKG_ONLINE \
- "TDX requires all packages to have an online CPU. Delete all TDs in order to offline all CPUs of a package.\n"
- pr_warn_ratelimited(MSG_ALLPKG_ONLINE);
- return -EBUSY;
-}
-
static int __init __tdx_bringup(void)
{
const struct tdx_sys_info_td_conf *td_conf;- int r, i;
+ int i;
for (i = 0; i < ARRAY_SIZE(tdx_uret_msrs); i++) {
/*
@@ -3459,23 +3411,7 @@ static int __init __tdx_bringup(void)
if (misc_cg_set_capacity(MISC_CG_RES_TDX, tdx_get_nr_guest_keyids()))
return -EINVAL;- /*
- * TDX-specific cpuhp callback to disallow offlining the last CPU in a
- * packing while KVM is running one or more TDs. Reclaiming HKIDs
- * requires doing PAGE.WBINVD on every package, i.e. offlining all CPUs
- * of a package would prevent reclaiming the HKID.
- */
- r = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "kvm/cpu/tdx:online",
- tdx_online_cpu, tdx_offline_cpu);
- if (r < 0)
- goto err_cpuhup;
-
- tdx_cpuhp_state = r; return 0;
-
-err_cpuhup:
- misc_cg_set_capacity(MISC_CG_RES_TDX, 0);
- return r;
}
int __init tdx_bringup(void)
@@ -3531,7 +3467,6 @@ void tdx_cleanup(void)
return; misc_cg_set_capacity(MISC_CG_RES_TDX, 0);
- cpuhp_remove_state(tdx_cpuhp_state);
}
void __init tdx_hardware_setup(void)
arch/x86/virt/vmx/tdx/tdx.c +36 -0
@@ -58,6 +58,8 @@ static LIST_HEAD(tdx_memlist);static struct tdx_sys_info tdx_sysinfo__ro_after_init ;static bool tdx_module_initialized__ro_after_init ; +static atomic_t nr_configured_hkid; +typedef void (*sc_err_func_t )(u64 fn,u64 err,struct tdx_module_args *args);static inline void seamcall_err (u64 fn,u64 err,struct tdx_module_args *args)
@@ -190,6 +192,40 @@ static int tdx_online_cpu(unsigned int cpu)static int tdx_offline_cpu (unsigned int cpu) { +int i; + +/* No TD is running. Allow any cpu to be offline. */ +if (!atomic_read (&nr_configured_hkid)) + goto done; + +/* +* In order to reclaim TDX HKID, (i.e. when deleting guest TD), need to +* call TDH.PHYMEM.PAGE.WBINVD on all packages to program all memory +* controller with pconfig. If we have active TDX HKID, refuse to +* offline the last online cpu. +*/ +for_each_online_cpu (i) { +/* +* Found another online cpu on the same package. +* Allow to offline. +*/ +if (i != cpu &&topology_physical_package_id (i) == +topology_physical_package_id (cpu)) + goto done; + } + +/* +* This is the last cpu of this package. Don't offline it. +* +* Because it's hard for human operator to understand the +* reason, warn it. +*/ +#define MSG_ALLPKG_ONLINE \ + "TDX requires all packages to have an online CPU. Delete all TDs in order to offline all CPUs of a package.\n" +pr_warn_ratelimited (MSG_ALLPKG_ONLINE ); +return -EBUSY ; + +done:
x86_virt_put_cpu(X86_FEATURE_VMX); return 0;
}
@@ -1505,15 +1541,22 @@ EXPORT_SYMBOL_GPL(tdx_get_nr_guest_keyids);
int tdx_guest_keyid_alloc(void)
{
- return ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start,
- tdx_guest_keyid_start + tdx_nr_guest_keyids - 1,
- GFP_KERNEL);
+ int ret;
+
+ ret = ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start,
+ tdx_guest_keyid_start + tdx_nr_guest_keyids - 1,
+ GFP_KERNEL);
+ if (ret >= 0)
+ atomic_inc(&nr_configured_hkid);
+
+ return ret;
}
EXPORT_SYMBOL_GPL(tdx_guest_keyid_alloc);
void tdx_guest_keyid_free(unsigned int keyid)
{
ida_free(&tdx_guest_keyid_pool, keyid);+ atomic_dec(&nr_configured_hkid);
}
EXPORT_SYMBOL_GPL(tdx_guest_keyid_free);
Signature · 1 line
2.47.3
Re: [RFC PATCH 3/4] KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
Quoted 40 lines · Chao Gao
Quoted 39 lines · Sean Christopherson
Quoted 13 lines · Rick P Edgecombe
Quoted 12 lines · Sean Christopherson
(quoted diff) +2
-2@@ -3524,34 +3453,31 @@ static int __init __tdx_bringup(void) if (td_conf->max_vcpus_per_td < num_present_cpus()) { pr_err("Disable TDX: MAX_VCPU_PER_TD (%u) smaller than number of logical CPUs (%u).\n", td_conf->max_vcpus_per_td, num_present_cpus()); - goto get_sysinfo_err; + return -EINVAL; } if (misc_cg_set_capacity(MISC_CG_RES_TDX, tdx_get_nr_guest_keyids())) - goto get_sysinfo_err; + return -EINVAL;Quoted 13 lines
Review static int __init __tdx_bringup(void) · line 3469
Quoted 10 lines
(quoted diff) +6
-2/* - * Leave hardware virtualization enabled after TDX is enabled - * successfully. TDX CPU hotplug depends on this. + * TDX-specific cpuhp callback to disallow offlining the last CPU in a + * packing while KVM is running one or more TDs. Reclaiming HKIDs + * requires doing PAGE.WBINVD on every package, i.e. offlining all CPUs + * of a package would prevent reclaiming the HKID. */ + r = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "kvm/cpu/tdx:online", + tdx_online_cpu, tdx_offline_cpu);Could pass NULL instead of
tdx_online_cpu()and delete this version oftdx_online_cpu().Oh, nice, I didn't realize (or forgot) the startup call is optional.
Quoted 1 line
Also could remove the error handling too.
No. Partly on prinicple, but also because
CPUHP_AP_ONLINE_DYNcan fail if the kernel runs out of dynamic entries (currently limited to 40). The kernel WARNs if it runs out of entries, but KVM should still do the right thing.Quoted 2 lines
Also, can we name the two tdx_offline_cpu()'s differently? This one is all about keyid's being in use.
tdx_hkid_offline_cpu()?
Quoted 6 lines
Quoted 3 lines
Ya. And change the description to "kvm/cpu/tdx:hkid_packages"? Or something like that.
Is it a good idea to consolidate the two
tdx_offline_cpu()functions, i.e., integrate KVM's version into x86 core?
This looks good to me, some additional cleanup opportunities below:
Quoted 25 lines
From
97165f9933f48d588f5390e2d543d9880c03532dMon Sep 17 00:00:00 2001
From: Chao Gao <[email protected]>
Date: Tue, 14 Oct 2025 01:00:06 -0700
Subject: [PATCH] x86/virt/tdx: Consolidate TDX CPU hotplug handlingThe core kernel registers a CPU hotplug callback to do VMX and TDX init and deinit while KVM registers a separate CPU offline callback to block offlining the last online CPU in a socket.
Splitting TDX-related CPU hotplug handling across two components is odd and adds unnecessary complexity.
Consolidate TDX-related CPU hotplug handling by integrating KVM's
tdx_offline_cpu()to the one in the core kernel.Also move
nr_configured_hkidto the core kernel becausetdx_offline_cpu()references it. Since HKID allocation and free are handled in the core kernel, it's more natural to track used HKIDs there.Signed-off-by Chao Gao <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 67 +------------------------------------ arch/x86/virt/vmx/tdx/tdx.c | 49 +++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 69 deletions(-)
[..]
arch/x86/virt/vmx/tdx/tdx.c static int tdx_online_cpu(unsigned int cpu) · line 225
Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +4
-0+*/ +#define MSG_ALLPKG_ONLINE \ + "TDX requires all packages to have an online CPU. Delete all TDs in order to offline all CPUs of a package.\n" +pr_warn_ratelimited (MSG_ALLPKG_ONLINE );
Why the define?
Quoted 19 lines
arch/x86/virt/vmx/tdx/tdx.c +3
-0+return -EBUSY ; + +done:x86_virt_put_cpu(X86_FEATURE_VMX); return 0;}
(quoted diff) +6
-3@@ -1505,15 +1541,22 @@ EXPORT_SYMBOL_GPL(tdx_get_nr_guest_keyids); int tdx_guest_keyid_alloc(void) { - return ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start, - tdx_guest_keyid_start + tdx_nr_guest_keyids - 1, - GFP_KERNEL); + int ret; + + ret = ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start, + tdx_guest_keyid_start + tdx_nr_guest_keyids - 1, + GFP_KERNEL); + if (ret >= 0)
Review EXPORT_SYMBOL_GPL(tdx_get_nr_guest_keyids); · line 1559
Quoted 10 lines
(quoted diff) +4
-0+ atomic_inc(&nr_configured_hkid); + + return ret; } EXPORT_SYMBOL_GPL(tdx_guest_keyid_alloc); void tdx_guest_keyid_free(unsigned int keyid) { ida_free(&tdx_guest_keyid_pool, keyid); + atomic_dec(&nr_configured_hkid);
So, ida has an ida_is_empty() helper. I believe you can just use that in the offline helper and delete @nr_configured_hkid.
Sean Christopherson
maintainerRe: [RFC PATCH 3/4] KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
Quoted 4 lines · On Tue, Oct 14, 2025, [email protected] wrote:
Quoted 3 lines · Chao Gao
Quoted 3 lines
Quoted 2 lines
Also, can we name the two tdx_offline_cpu()'s differently? This one is all about keyid's being in use.
tdx_hkid_offline_cpu()?
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Ya. And change the description to "kvm/cpu/tdx:hkid_packages"? Or something like that.
Is it a good idea to consolidate the two
tdx_offline_cpu()functions, i.e., integrate KVM's version into x86 core?This looks good to me, some additional cleanup opportunities below:
+1. This crossed my mind as well, but for once I reeled myself in as I'm very susceptible to self-induced scope creep :-)
Edgecombe, Rick P
maintainerRe: [RFC PATCH 3/4] KVM: x86/tdx: Do VMXON and TDX-Module initialization during tdx_init()
In reply to Chao GaoQuoted 2 lines · Chao Gao
Is it a good idea to consolidate the two
tdx_offline_cpu()functions, i.e., integrate KVM's version into x86 core?
+1
It fixes a bug too. Currently in __tdx_td_init() if misc_cg_try_charge() fails, it will decrement nr_configured_hkid in the error path before it got incremented.
And the cgroups stuff that got plucked from the past seems to have the same problem. Ideally we could move MISC_CG_RES_TDX accounting too with this change because it also fits, and it would fix that too.
Re: [RFC PATCH 2/4] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean ChristophersonQuoted 7 lines
void
vmx_emergency_disable_virtualization_cpu(void){ int cpu = raw_smp_processor_id(); struct loaded_vmcs *v;arch/x86/kvm/vmx/vmx.c +0
-2- kvm_rebooting = true; -
arch/x86/kvm/vmx/vmx.c static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx, · line 684
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +2
-8-/* -* Note, CR4.VMXE can be _cleared_ in NMI context, but it can only be -* set in task context. If this races with VMX is disabled by an NMI, -* VMCLEAR and VMXOFF may #UD, but KVM will eat those faults due to -* kvm_rebooting set. -*/ -if (!(__read_cr4 () &X86_CR4_VMXE )) -return ; +WARN_ON_ONCE (!virt_rebooting); + virt_rebooting = true;
This is unnecessary as virt_rebooting has been set to true ...
arch/x86/virt/hw.c line 126
Quoted 3 lines
arch/x86/virt/hw.c new file +3
-0+static void x86_vmx_emergency_disable_virtualization_cpu (void ) +{ + virt_rebooting = true;
... here.
and ditto for SVM.
Quoted 15 lines
arch/x86/virt/hw.c new file +15
-0+ +/* +* Note, CR4.VMXE can be _cleared_ in NMI context, but it can only be +* set in task context. If this races with VMX being disabled via NMI, +* VMCLEAR and VMXOFF may #UD, but the kernel will eat those faults due +* to virt_rebooting being set. +*/ +if (!(__read_cr4 () &X86_CR4_VMXE )) +return ; + +x86_virt_invoke_kvm_emergency_callback (); + +x86_vmx_cpu_vmxoff (); +} +
<snip>
arch/x86/virt/hw.c line 298
Quoted 7 lines
arch/x86/virt/hw.c new file +7
-0+void x86_virt_put_cpu (int feat) +{ +if (WARN_ON_ONCE (!this_cpu_read (virtualization_nr_users))) +return ; + +if (this_cpu_dec_return (virtualization_nr_users) && !virt_rebooting) +return ;
any reason to check virt_rebooting here?
It seems unnecessary because both the emergency reboot case and shutdown case work fine without it, and keeping it might prevent us from discovering real bugs, e.g., KVM or TDX failing to decrease the refcount.
Quoted 9 lines
arch/x86/virt/hw.c new file +9
-0+ +if (x86_virt_is_vmx () && feat ==X86_FEATURE_VMX ) +x86_vmx_put_cpu (); +else if (x86_virt_is_svm () && feat ==X86_FEATURE_SVM ) +x86_svm_put_cpu (); +else +WARN_ON_ONCE (1 ); +} +EXPORT_SYMBOL_GPL (x86_virt_put_cpu);
Sean Christopherson
maintainerRe: [RFC PATCH 2/4] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 20 lines · Chao Gao
Quoted 7 lines
void
vmx_emergency_disable_virtualization_cpu(void){ int cpu = raw_smp_processor_id(); struct loaded_vmcs *v;arch/x86/kvm/vmx/vmx.c +0
-2- kvm_rebooting = true; -arch/x86/kvm/vmx/vmx.c static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx, · line 684
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +2
-8-/* -* Note, CR4.VMXE can be _cleared_ in NMI context, but it can only be -* set in task context. If this races with VMX is disabled by an NMI, -* VMCLEAR and VMXOFF may #UD, but KVM will eat those faults due to -* kvm_rebooting set. -*/ -if (!(__read_cr4 () &X86_CR4_VMXE )) -return ; +WARN_ON_ONCE (!virt_rebooting); + virt_rebooting = true;This is unnecessary as
virt_rebootinghas been set to true ...
Quoted 7 lines
arch/x86/virt/hw.c line 126
Quoted 3 lines
arch/x86/virt/hw.c new file +3
-0+static void x86_vmx_emergency_disable_virtualization_cpu (void ) +{ + virt_rebooting = true;... here.
and ditto for SVM.
Yeah, I wasn't sure what to do. I agree it's redundant, but it's harmless, whereas not having virt_rebooting set would be Very Bad (TM). I think you're probably right, and we should just assume we aren't terrible at programming. Setting the flag in KVM could even hide latent bugs, e.g. if code runs before x86_virt_invoke_kvm_emergency_callback().
Quoted 27 lines
Quoted 14 lines
arch/x86/virt/hw.c new file +14
-0+/* +* Note, CR4.VMXE can be _cleared_ in NMI context, but it can only be +* set in task context. If this races with VMX being disabled via NMI, +* VMCLEAR and VMXOFF may #UD, but the kernel will eat those faults due +* to virt_rebooting being set. +*/ +if (!(__read_cr4 () &X86_CR4_VMXE )) +return ; + +x86_virt_invoke_kvm_emergency_callback (); + +x86_vmx_cpu_vmxoff (); +} +<snip>
arch/x86/virt/hw.c line 298
Quoted 7 lines
arch/x86/virt/hw.c new file +7
-0+void x86_virt_put_cpu (int feat) +{ +if (WARN_ON_ONCE (!this_cpu_read (virtualization_nr_users))) +return ; + +if (this_cpu_dec_return (virtualization_nr_users) && !virt_rebooting) +return ;any reason to check
virt_rebootinghere?
Quoted 3 lines
It seems unnecessary because both the emergency reboot case and shutdown case work fine without it, and keeping it might prevent us from discovering real bugs, e.g., KVM or TDX failing to decrease the refcount.
*sigh*
I simply misread my own code (and I suspect I pivoted on what I was doing). I just spent ~10 minutes typing up various responses about how the emergency code needs to _force_ VMX/SVM off, but I kept overlooking the fact that the emergency hooks bypass the refcounting (which is obviously very intentional). /facepalm
So yeah, I agree that exempting the refcount on virt_rebooting is bad here. E.g. if kvm_shutdown() runs before tdx_shutdown(), then KVM will pull the rug out from under TDX, and hw/virt.c will attempt to disable virtualization twice. Which is "fine" thanks to the hardening, but gross and unnecessary.
Thanks so much!
Quoted 9 lines
Quoted 9 lines
arch/x86/virt/hw.c new file +9
-0+ +if (x86_virt_is_vmx () && feat ==X86_FEATURE_VMX ) +x86_vmx_put_cpu (); +else if (x86_virt_is_svm () && feat ==X86_FEATURE_SVM ) +x86_svm_put_cpu (); +else +WARN_ON_ONCE (1 ); +} +EXPORT_SYMBOL_GPL (x86_virt_put_cpu);
dan.j.williams@ wrote:
[..]
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
Sounds good and I read this as "hey, this is the form I would like to see, when someone else cleans this up and sends it back to me as a non-RFC".
Actually, I think I can take it forward. Knock wood, but I don't think there's all that much left to be done. Heck, even writing the code for the initial RFC was a pretty short adventure once I had my head wrapped around the concept.
Ack.
FYI, this series is now included in tsm.git#staging [1]. Chao had one fixup to it [2].
Recall that tsm.git#staging is where all of us working on PCI Device Security (TDX, SEV, and CCA) can start tripping over each others implementations [3] in a unified tree.
The initial core work in that tree is a v6.19 candidate as long as at least one arch implementation is also ready. SEV looks nearly ready [4]. CCA will sit out as it is going through a specification update. TDX is ready save for this vmxon dependency.
I recall being concerned about the new TDX always-on stance, but I now think it is ok. Just puts more pressure on the dynamic-PAMT work to land. In the meantime, disabling TDX in the BIOS is a stopgap for those that can not tolerate the static-PAMT overhead.
[1]: https://git.kernel.org/pub/scm/linux/kernel/git/devsec/tsm.git/log/?h=staging
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/devsec/tsm.git/commit/?id=406cd719d2a2
[3]: https://git.kernel.org/pub/scm/linux/kernel/git/devsec/tsm.git/commit/?id=e3d238ddeec0
[4]: http://lore.kernel.org/[email protected]