KVM: x86/tdx: Have TDX handle VMXON during bringup
Subsystems (6)
Sean Christopherson
maintainerv2 cover KVM: x86/tdx: Have TDX handle VMXON during bringup
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.
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.
Decently tested, and it seems like the core idea is sound, so I dropped the RFC. But the side of things definitely needs testing.
Note, this is based on kvm-x86/next, which doesn't have EXPORT_SYMBOL_FOR_KVM(), and so the virt/hw.c exports need to be fixed up. I'm sending now instead of waiting for -rc1 because I'm assuming I'll need to spin at least v3 anyways :-)
v2:
- Initialize the TDX-Module via subsys initcall instead of during tdx_init(). [Rick]
- Isolate the __init and __ro_after_init changes. [Rick]
- Use ida_is_empty() instead of manually tracking HKID usage. [Dan]
- Don't do weird things with the refcounts when virt_rebooting is true. [Chao]
- Drop unnecessary setting of virt_rebooting in KVM code. [Chao]
- Rework things to have less X86_FEATURE_FOO code. [Rick]
- Consolidate the CPU hotplug callbacks. [Chao]
v1 (RFC):
- https://lore.kernel.org/all/[email protected]
Chao Gao (1):
x86/virt/tdx: KVM: Consolidate TDX CPU hotplug handling
Sean Christopherson (6):
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 subsys
init
x86/virt/tdx: Tag a pile of functions as __init, and globals as
__ro_after_init
x86/virt/tdx: Use ida_is_empty() to detect if any TDs may be running
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/kvm_host.h | 3 +- arch/x86/include/asm/reboot.h | 11 - arch/x86/include/asm/tdx.h | 4 - arch/x86/include/asm/virt.h | 26 ++ arch/x86/include/asm/vmx.h | 11 + arch/x86/kernel/cpu/common.c | 2 + arch/x86/kernel/crash.c | 3 +- arch/x86/kernel/reboot.c | 63 +--- arch/x86/kernel/smp.c | 5 +- arch/x86/kvm/svm/svm.c | 34 +- arch/x86/kvm/svm/vmenter.S | 10 +- arch/x86/kvm/vmx/tdx.c | 209 ++---------- arch/x86/kvm/vmx/vmcs.h | 11 - arch/x86/kvm/vmx/vmenter.S | 2 +- arch/x86/kvm/vmx/vmx.c | 127 +------- arch/x86/kvm/x86.c | 20 +- arch/x86/virt/Makefile | 2 + arch/x86/virt/hw.c | 340 ++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 315 ++++++++++-------- 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 +- 25 files changed, 657 insertions(+), 627 deletions(-) create mode 100644 arch/x86/include/asm/virt.h create mode 100644 arch/x86/virt/hw.c
base-commit: 5d3e2d9ba9ed68576c70c127e4f7446d896f2af2
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Sean Christopherson
maintainerPATCH v2 1/7 KVM: x86: Move kvm_rebooting to x86
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.
--- 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
@@ -694,6 +694,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. *
@@ -13100,6 +13103,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
@@ -1621,6 +1621,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
@@ -2302,7 +2303,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
@@ -5586,13 +5586,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 ) {
@@ -5646,10 +5648,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
@@ -5658,7 +5659,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.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v2 1/7] KVM: x86: Move kvm_rebooting to x86
Quoted 5 lines · Sean Christopherson
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.Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v2 1/7] KVM: x86: Move kvm_rebooting to x86
In reply to Sean ChristophersonQuoted 3 lines · Sean Christopherson
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.
For the pure code move:
That said, I wouldn't mind some clarification around 'kvm_rebooting' and a small bit of documentation about what kvm_arch_shutdown() is conceptually.
It doesn't have to be in this set, but maybe:
/*
* Override this to make global, arch-specific changes which
* prepare the system to disable hardware virtualization support. */__weak void kvm_arch_shutdown(void)
{
}
and something like:
void kvm_arch_shutdown(void)
{
/*
* Indicate that hardware virtualization support will soon be
* disabled asynchronously. Attempts to use hardware
* virtualization will start generating errors and exceptions.
*
* Start being more tolerant of those conditions.
*/
kvm_rebooting = true;
smp_wmb();
}The barrier is almost certainly not needed in practice. But I do find it helpful as an indicator that other CPUs are going to be reading this thing and we have zero clue what they're doing.
Sean Christopherson
maintainerPATCH v2 2/7 KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean ChristophersonMove the innermost VMXON and EFER.SVME management logic out of KVM and into to core x86 so that TDX can force VMXON without having to rely on KVM being loaded, e.g. to do SEAMCALLs during initialization.
Implement a per-CPU refcounting scheme so that "users", e.g. KVM and the future TDX code, can co-exist without pulling the rug out from under each other.
To avoid having to choose between SVM and VMX, simply refuse to enable either if both are somehow supported. No known CPU supports both SVM and VMX, and it's comically unlikely such a CPU will ever exist.
For lack of a better name, call the new file "hw.c", to yield "virt hardware" when combined with its parent directory.
--- arch/x86/events/intel/pt.c | 1 - arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/include/asm/reboot.h | 11 -- arch/x86/include/asm/virt.h | 26 +++ arch/x86/include/asm/vmx.h | 11 ++ arch/x86/kernel/cpu/common.c | 2 + arch/x86/kernel/crash.c | 3 +- arch/x86/kernel/reboot.c | 63 +----- arch/x86/kernel/smp.c | 5 +- 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 | 127 +----------- arch/x86/kvm/x86.c | 17 +- arch/x86/kvm/x86.h | 1 - arch/x86/virt/Makefile | 2 + arch/x86/virt/hw.c | 340 ++++++++++++++++++++++++++++++++ 19 files changed, 422 insertions(+), 250 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/kvm_host.h +2 -1
@@ -40,7 +40,8 @@#include <asm/irq_remapping.h> #include <asm/kvm_page_track.h> #include <asm/kvm_vcpu_regs.h> -#include <asm/reboot.h> +#include <asm/virt.h> +#include <hyperv/hvhdk.h> #define __KVM_HAVE_ARCH_VCPU_DEBUGFS
arch/x86/include/asm/reboot.h +0 -11
@@ -25,17 +25,6 @@ void __noreturn machine_real_restart(unsigned int type);#define MRR_BIOS 0#define MRR_APM 1 -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 */ -typedef void (*nmi_shootdown_cb )(int ,struct pt_regs *);void nmi_shootdown_cpus (nmi_shootdown_cb callback);void run_crash_ipi_callback (struct pt_regs *regs);
arch/x86/include/asm/virt.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASM_X86_VIRT_H +#define _ASM_X86_VIRT_H + +#include <asm/reboot.h> + +typedef void (cpu_emergency_virt_cb )(void ); + +#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); + +int x86_virt_emergency_disable_virtualization_cpu (void ); + +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) {} +static inline int x86_virt_emergency_disable_virtualization_cpu (void ) {return -ENOENT ; } +#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>
@@ -2124,6 +2125,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/crash.c +2 -1
@@ -42,6 +42,7 @@#include <asm/crash.h> #include <asm/cmdline.h> #include <asm/sev.h> +#include <asm/virt.h> /* Used while preparing memory map entries for second kernel */ struct crash_memmap_data {
@@ -111,7 +112,7 @@ void native_machine_crash_shutdown(struct pt_regs *regs)crash_smp_send_stop (); -cpu_emergency_disable_virtualization (); +x86_virt_emergency_disable_virtualization_cpu ();/* * Disable Intel PT to stop its logging
arch/x86/kernel/reboot.c +7 -56
90 lines not on this page · raw message
arch/x86/kernel/smp.c +3 -2
@@ -35,6 +35,7 @@#include <asm/trace/irq_vectors.h> #include <asm/kexec.h> #include <asm/reboot.h> +#include <asm/virt.h> /* * Some notes on x86 processor bugs affecting SMP operation:
@@ -124,7 +125,7 @@ static int smp_stop_nmi_callback(unsigned int val, struct pt_regs *regs)if (raw_smp_processor_id () ==atomic_read (&stopping_cpu))return NMI_HANDLED ; -cpu_emergency_disable_virtualization (); +x86_virt_emergency_disable_virtualization_cpu ();stop_this_cpu (NULL );return NMI_HANDLED ;
@@ -136,7 +137,7 @@ static int smp_stop_nmi_callback(unsigned int val, struct pt_regs *regs)DEFINE_IDTENTRY_SYSVEC (sysvec_reboot) {apic_eoi (); -cpu_emergency_disable_virtualization (); +x86_virt_emergency_disable_virtualization_cpu ();stop_this_cpu (NULL ); }
arch/x86/kvm/svm/svm.c +7 -27
@@ -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>
@@ -476,27 +477,9 @@ 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; - -kvm_cpu_svm_disable (); +wrmsrq (MSR_VM_HSAVE_PA ,0 ); }static void svm_disable_virtualization_cpu (void )
@@ -505,7 +488,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 (); }
@@ -514,12 +497,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 ;
@@ -527,8 +510,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 )) {
@@ -539,7 +520,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
@@ -298,16 +298,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
@@ -394,7 +394,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"
@@ -1994,7 +1995,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
@@ -310,7 +310,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 +4 -123
203 lines not on this page · raw message
arch/x86/kvm/x86.c +8 -9
@@ -83,6 +83,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
@@ -694,9 +696,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. *
@@ -707,7 +706,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);
@@ -12999,12 +12998,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 )
@@ -13106,11 +13105,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 )
@@ -13127,7 +13126,7 @@ void kvm_arch_disable_virtualization_cpu(void)* disable virtualization arrives. Handle the extreme edge case here * instead of trying to account for it in the normal flows. */ -if (in_task () ||WARN_ON_ONCE (!kvm_rebooting)) +if (in_task () ||WARN_ON_ONCE (!virt_rebooting))drop_user_return_notifiers ();else __module_get (THIS_MODULE );
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 +340 -0
340 lines not on this page · raw message
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 8 lines · Sean Christopherson
Move the innermost VMXON and EFER.SVME management logic out of KVM and into to core x86 so that TDX can force VMXON without having to rely on KVM being loaded, e.g. to do SEAMCALLs during initialization.
Implement a per-CPU refcounting scheme so that "users", e.g. KVM and the future TDX code, can co-exist without pulling the rug out from under each other.
Quoted 8 lines
To avoid having to choose between SVM and VMX, simply refuse to enable either if both are somehow supported. No known CPU supports both SVM and VMX, and it's comically unlikely such a CPU will ever exist.
For lack of a better name, call the new file "
hw.c", to yield "virt hardware" when combined with its parent directory.Signed-off-by Sean Christopherson <[email protected]>
[..]
Only patch organization and naming choice questions from me. I only looked at the VMX side of things since I previously made an attempt at moving that. I gave a cursory look at the SVM details.
Quoted 14 lines
arch/x86/kvm/x86.c +2
-0@@ -83,6 +83,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
arch/x86/kvm/x86.c static void drop_user_return_notifiers(void) · line 698
Quoted 5 lines
arch/x86/kvm/x86.c +0
-2@@ -694,9 +696,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);
...a short stay for this symbol in kvm/x86.c? It raises my curiosity why patch1 is separate. Patch1 looked like the start of a series of incremental conversions, patch2 is a combo move. I am ok either way, just questioning consistency. I.e. if combo move then patch1 folds in here, if incremental, perhaps split out other combo conversions like emergency_disable_virtualization_cpu()? The aspect of "this got moved twice in the same patchset" is what poked me.
[..]
Quoted 17 lines
arch/x86/virt/hw.c new file +11
-0@@ -0,0 +1,340 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/cpu.h> +#include <linux/cpumask.h> +#include <linux/errno.h> +#include <linux/kvm_types.h> +#include <linux/list.h> +#include <linux/percpu.h> + +#include <asm/perf_event.h> +#include <asm/processor.h> +#include <asm/virt.h>
arch/x86/virt/hw.c line 21
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+#include <asm/vmx.h> + +static int x86_virt_feature__ro_after_init ; + +__visible bool virt_rebooting; +EXPORT_SYMBOL_GPL (virt_rebooting); + +static DEFINE_PER_CPU(int, virtualization_nr_users); + +static cpu_emergency_virt_cb __rcu *kvm_emergency_callback;
Hmm, why kvm_ and not virt_?
[..]
arch/x86/virt/hw.c line 52
Quoted 2 lines
arch/x86/virt/hw.c new file +2
-0+#if IS_ENABLED (CONFIG_KVM_INTEL ) +static DEFINE_PER_CPU(struct vmcs *, root_vmcs);
Perhaps introduce a CONFIG_INTEL_VMX for this? For example, KVM need not be enabled if all one wants to do is use TDX to setup PCIe Link Encryption. ...or were you expecting?
#if IS_ENABLED(CONFIG_KVM_INTEL) || IS_ENABLED(...<other VMX users>...)
Sean Christopherson
maintainerRe: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 10 lines · On Sat, Dec 06, 2025, [email protected] wrote:
arch/x86/kvm/x86.c static void drop_user_return_notifiers(void) · line 698
Quoted 6 lines · Sean Christopherson
arch/x86/kvm/x86.c +0
-2@@ -694,9 +696,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);...a short stay for this symbol in
kvm/x86.c? It raises my curiosity why patch1 is separate.
Because it affects non-x86 architectures. It should be a complete nop, but I wanted to isolate what I could.
Quoted 5 lines
Patch1 looked like the start of a series of incremental conversions, patch2 is a combo move. I am ok either way, just questioning consistency. I.e. if combo move then patch1 folds in here, if incremental, perhaps split out other combo conversions like
emergency_disable_virtualization_cpu()? The aspect of "this got moved twice in the same patchset" is what poked me.
Yeah, I got lazy to a large extent. I'm not super optimistic that we won't end up with one big "move all this stuff" patch, but I agree it doesn't need to be _this_ big.
Quoted 22 lines
[..]
Quoted 21 lines
arch/x86/virt/hw.c new file +15
-0@@ -0,0 +1,340 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/cpu.h> +#include <linux/cpumask.h> +#include <linux/errno.h> +#include <linux/kvm_types.h> +#include <linux/list.h> +#include <linux/percpu.h> + +#include <asm/perf_event.h> +#include <asm/processor.h> +#include <asm/virt.h> +#include <asm/vmx.h> + +static int x86_virt_feature__ro_after_init ; +
Quoted 8 lines
arch/x86/virt/hw.c line 21
Quoted 6 lines
arch/x86/virt/hw.c new file +6
-0+__visible bool virt_rebooting; +EXPORT_SYMBOL_GPL (virt_rebooting); + +static DEFINE_PER_CPU(int, virtualization_nr_users); + +static cpu_emergency_virt_cb __rcu *kvm_emergency_callback;Hmm, why
kvm_and notvirt_?
I was trying to capture that this callback can _only_ be used by KVM, because KVM is the only in-tree hypervisor. That's also why the exports are only for KVM (and will use EXPORT_SYMBOL_FOR_KVM() when I post the next version).
Quoted 9 lines
[..]
arch/x86/virt/hw.c line 52
Quoted 2 lines
arch/x86/virt/hw.c new file +2
-0+#if IS_ENABLED (CONFIG_KVM_INTEL ) +static DEFINE_PER_CPU(struct vmcs *, root_vmcs);Perhaps introduce a
CONFIG_INTEL_VMXfor this? For example, KVM need not be enabled if all one wants to do is use TDX to setup PCIe Link Encryption. ...or were you expecting?#if
IS_ENABLED(CONFIG_KVM_INTEL)||IS_ENABLED(...<other VMX users>...)
I don't think we need anything at this time. INTEL_TDX_HOST depends on KVM_INTEL, and so without a user that needs VMXON without KVM_INTEL, I think we're good as-is.
config INTEL_TDX_HOST
bool "Intel Trust Domain Extensions (TDX) host support" depends on CPU_SUP_INTEL
depends on X86_64
depends on KVM_INTEL
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · On Sat, Dec 06, 2025, [email protected] wrote:
Quoted 4 lines · Sean Christopherson
arch/x86/kvm/x86.c +0
-0@@ -694,9 +696,6 @@ static void drop_user_return_notifiers(void)kvm_on_user_return (&msrs->urn); }
Quoted 8 lines
Quoted 5 lines
arch/x86/kvm/x86.c static void drop_user_return_notifiers(void) · line 698
Quoted 2 lines
arch/x86/kvm/x86.c +0
-2-__visible bool kvm_rebooting; -EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_rebooting);...a short stay for this symbol in
kvm/x86.c? It raises my curiosity why patch1 is separate.Because it affects non-x86 architectures. It should be a complete nop, but I wanted to isolate what I could.
Ok.
[..]
Quoted 7 lines
Quoted 3 lines
arch/x86/virt/hw.c line 21
Quoted 1 line
arch/x86/virt/hw.c new file +1
-0+static cpu_emergency_virt_cb __rcu *kvm_emergency_callback;Hmm, why
kvm_and notvirt_?I was trying to capture that this callback can _only_ be used by KVM, because KVM is the only in-tree hypervisor. That's also why the exports are only for KVM (and will use
EXPORT_SYMBOL_FOR_KVM()when I post the next version).
Oh, true, that makes sense.
Quoted 10 lines
Quoted 9 lines
[..]
arch/x86/virt/hw.c line 52
Quoted 2 lines
arch/x86/virt/hw.c new file +2
-0+#if IS_ENABLED (CONFIG_KVM_INTEL ) +static DEFINE_PER_CPU(struct vmcs *, root_vmcs);Perhaps introduce a
CONFIG_INTEL_VMXfor this? For example, KVM need not be enabled if all one wants to do is use TDX to setup PCIe Link Encryption. ...or were you expecting?#if
IS_ENABLED(CONFIG_KVM_INTEL)||IS_ENABLED(...<other VMX users>...)
Quoted 8 lines
I don't think we need anything at this time.
INTEL_TDX_HOSTdepends onKVM_INTEL, and so without a user that needs VMXON withoutKVM_INTEL, I think we're good as-is.config
INTEL_TDX_HOST
bool "Intel Trust Domain Extensions (TDX) host support" depends onCPU_SUP_INTEL
depends onX86_64
depends onKVM_INTEL
...but INTEL_TDX_HOST, it turns out, does not have any functional dependencies on KVM_INTEL. At least, not since I last checked. Yes, it would be silly and result in dead code today to do a build with:
CONFIG_INTEL_TDX_HOST=yCONFIG_KVM_INTEL=n
However, when the TDX Connect support arrives you could have:
CONFIG_INTEL_TDX_HOST=yCONFIG_KVM_INTEL=nCONFIG_TDX_HOST_SERVICES=y
Where "TDX Host Services" is a driver for PCIe Link Encryption and TDX Module update. Whether such configuration freedom has any practical value is a separate question.
I am ok if the answer is, "wait until someone shows up who really wants PCIe Link Encryption without KVM".
Sean Christopherson
maintainerRe: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 24 lines · On Wed, Dec 10, 2025, [email protected] wrote:
Quoted 9 lines · Sean Christopherson
On Sat, Dec 06, 2025, [email protected] wrote:
I don't think we need anything at this time.INTEL_TDX_HOSTdepends onKVM_INTEL, and so without a user that needs VMXON withoutKVM_INTEL, I think we're good as-is.config
INTEL_TDX_HOST
bool "Intel Trust Domain Extensions (TDX) host support" depends onCPU_SUP_INTEL
depends onX86_64
depends onKVM_INTEL...but
INTEL_TDX_HOST, it turns out, does not have any functional dependencies onKVM_INTEL. At least, not since I last checked. Yes, it would be silly and result in dead code today to do a build with:
CONFIG_INTEL_TDX_HOST=yCONFIG_KVM_INTEL=nHowever, when the TDX Connect support arrives you could have:
CONFIG_INTEL_TDX_HOST=yCONFIG_KVM_INTEL=nCONFIG_TDX_HOST_SERVICES=y
Quoted 6 lines
Where "TDX Host Services" is a driver for PCIe Link Encryption and TDX Module update. Whether such configuration freedom has any practical value is a separate question.
I am ok if the answer is, "wait until someone shows up who really wants PCIe Link Encryption without KVM".
Ya, that's my answer. At the very least, wait until TDX_HOST_SERVICES comes along.
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 29 lines · Sean Christopherson
Quoted 28 lines · On Wed, Dec 10, 2025, [email protected] wrote:
Quoted 9 lines · Sean Christopherson
On Sat, Dec 06, 2025, [email protected] wrote:
I don't think we need anything at this time.INTEL_TDX_HOSTdepends onKVM_INTEL, and so without a user that needs VMXON withoutKVM_INTEL, I think we're good as-is.config
INTEL_TDX_HOST
bool "Intel Trust Domain Extensions (TDX) host support" depends onCPU_SUP_INTEL
depends onX86_64
depends onKVM_INTEL...but
INTEL_TDX_HOST, it turns out, does not have any functional dependencies onKVM_INTEL. At least, not since I last checked. Yes, it would be silly and result in dead code today to do a build with:
CONFIG_INTEL_TDX_HOST=yCONFIG_KVM_INTEL=nHowever, when the TDX Connect support arrives you could have:
CONFIG_INTEL_TDX_HOST=yCONFIG_KVM_INTEL=nCONFIG_TDX_HOST_SERVICES=yWhere "TDX Host Services" is a driver for PCIe Link Encryption and TDX Module update. Whether such configuration freedom has any practical value is a separate question.
Quoted 5 lines
Quoted 2 lines
I am ok if the answer is, "wait until someone shows up who really wants PCIe Link Encryption without KVM".
Ya, that's my answer. At the very least, wait until
TDX_HOST_SERVICEScomes along.
I've tested the PCIe Link Encryption without KVM, with the kernel config:
CONFIG_INTEL_TDX_HOST=y
CONFIG_KVM_INTEL=n
CONFIG_TDX_HOST_SERVICES=y
and
drivers/virt/coco/tdx-host/Kconfig new file +4 -0
@@ -0,0 +1,10 @@ +config TDX_HOST_SERVICES + tristate "TDX Host Services Driver" + depends on INTEL_TDX_HOST + default m
Finally I enabled the combination successfully with a patch below, do we need the change when TDX_HOST_SERVICES comes?
------------8<----------------------
arch/x86/Kconfig +0 -1
@@ -1898,7 +1898,6 @@ config INTEL_TDX_HOST bool "Intel Trust Domain Extensions (TDX) host support" depends on CPU_SUP_INTEL depends on X86_64 - depends on KVM_INTEL depends on X86_X2APIC select ARCH_KEEP_MEMBLOCK depends on CONTIG_ALLOC
arch/x86/include/asm/virt.h +1 -1
@@ -6,7 +6,7 @@typedef void (cpu_emergency_virt_cb )(void ); -#if IS_ENABLED (CONFIG_KVM_X86 ) +#if IS_ENABLED (CONFIG_KVM_X86 ) ||IS_ENABLED (CONFIG_INTEL_TDX_HOST )extern bool virt_rebooting;void __init x86_virt_init (void );
arch/x86/kvm/Kconfig +1 -1
@@ -134,7 +134,7 @@ config X86_SGX_KVM config KVM_INTEL_TDX bool "Intel Trust Domain Extensions (TDX) support" default y - depends on INTEL_TDX_HOST + depends on INTEL_TDX_HOST && KVM_INTEL select KVM_GENERIC_MEMORY_ATTRIBUTES select HAVE_KVM_ARCH_GMEM_POPULATE help
arch/x86/virt/Makefile +2 -1
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += svm/ vmx/ -obj-$(subst m,y,$(CONFIG_KVM_X86)) += hw.o +obj-$(CONFIG_INTEL_TDX_HOST) += hw.o +obj-$(subst m,y,$(CONFIG_KVM_X86)) += hw.o
arch/x86/virt/hw.c +3 -2
@@ -48,7 +48,7 @@ static void x86_virt_invoke_kvm_emergency_callback(void)kvm_callback (); } -#if IS_ENABLED (CONFIG_KVM_INTEL ) +#if IS_ENABLED (CONFIG_KVM_INTEL ) ||IS_ENABLED (CONFIG_INTEL_TDX_HOST ) static DEFINE_PER_CPU(struct vmcs *, root_vmcs);static int x86_virt_cpu_vmxon (void )
@@ -257,7 +257,8 @@ static __init int x86_svm_init(void) { return -EOPNOTSUPP; } ({ \ int __r; \ \ - if (IS_ENABLED(CONFIG_KVM_INTEL) && \ + if ((IS_ENABLED(CONFIG_KVM_INTEL) || \ + IS_ENABLED(CONFIG_INTEL_TDX_HOST)) && \ cpu_feature_enabled(X86_FEATURE_VMX)) \ __r = x86_vmx_##fn(); \ else if (IS_ENABLED(CONFIG_KVM_AMD) && \
Quoted 1 line
Sean Christopherson
maintainerRe: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 53 lines · Xu Yilun
Quoted 29 lines · Sean Christopherson
Quoted 28 lines · On Wed, Dec 10, 2025, [email protected] wrote:
Quoted 9 lines · Sean Christopherson
On Sat, Dec 06, 2025, [email protected] wrote:
I don't think we need anything at this time.INTEL_TDX_HOSTdepends onKVM_INTEL, and so without a user that needs VMXON withoutKVM_INTEL, I think we're good as-is.config
INTEL_TDX_HOST
bool "Intel Trust Domain Extensions (TDX) host support" depends onCPU_SUP_INTEL
depends onX86_64
depends onKVM_INTEL...but
INTEL_TDX_HOST, it turns out, does not have any functional dependencies onKVM_INTEL. At least, not since I last checked. Yes, it would be silly and result in dead code today to do a build with:
CONFIG_INTEL_TDX_HOST=yCONFIG_KVM_INTEL=nHowever, when the TDX Connect support arrives you could have:
CONFIG_INTEL_TDX_HOST=yCONFIG_KVM_INTEL=nCONFIG_TDX_HOST_SERVICES=yWhere "TDX Host Services" is a driver for PCIe Link Encryption and TDX Module update. Whether such configuration freedom has any practical value is a separate question.
Quoted 5 lines
Quoted 2 lines
I am ok if the answer is, "wait until someone shows up who really wants PCIe Link Encryption without KVM".
Ya, that's my answer. At the very least, wait until
TDX_HOST_SERVICEScomes along.I've tested the PCIe Link Encryption without KVM, with the kernel config:
CONFIG_INTEL_TDX_HOST=y
CONFIG_KVM_INTEL=n
CONFIG_TDX_HOST_SERVICES=yand
drivers/virt/coco/tdx-host/Kconfig new file +4
-0@@ -0,0 +1,10 @@ +config TDX_HOST_SERVICES + tristate "TDX Host Services Driver" + depends on INTEL_TDX_HOST + default m
Quoted 2 lines
Finally I enabled the combination successfully with a patch below, do we need the change when
TDX_HOST_SERVICEScomes?
Ya, we'll need something along those lines. What exactly we want the Kconfig soup to look like is TBD though, e.g. it may or may not make sense to have a common config that says "I want virtualization!"?
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean Christophersonarch/x86/include/asm/virt.h line 5
Quoted 8 lines
arch/x86/include/asm/virt.h new file +5
-0@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASM_X86_VIRT_H +#define _ASM_X86_VIRT_H + +#include <asm/reboot.h>
asm/reboot.h isn't used.
Quoted 7 lines
arch/x86/include/asm/virt.h new file +7
-0+ +typedef void (cpu_emergency_virt_cb )(void ); + +#if IS_ENABLED (CONFIG_KVM_X86 ) +extern bool virt_rebooting; + +void __init x86_virt_init (void );
arch/x86/include/asm/virt.h line 22
Quoted 10 lines
arch/x86/include/asm/virt.h new file +10
-0+ +int x86_virt_get_cpu (int feat); +void x86_virt_put_cpu (int feat); + +int x86_virt_emergency_disable_virtualization_cpu (void ); + +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) {}
Why does this need to be "__always_inline" rather than just "inline"?
arch/x86/include/asm/virt.h static void emergency_reboot_disable_virtualization(void) · line 547
Quoted 11 lines
static void
emergency_reboot_disable_virtualization(void){ local_irq_disable();arch/x86/include/asm/virt.h new file +2
-2@@ -587,16 +543,11 @@ static void emergency_reboot_disable_virtualization(void)* We can't take any locks and we may be on an inconsistent state, so * use NMIs as IPIs to tell the other CPUs to disable VMX/SVM and halt. * -* Do the NMI shootdown even if virtualization is off on _this_ CPU, as -* other CPUs may have virtualization enabled. +* Safely force _this_ CPU out of VMX/SVM operation, and if necessary, +* blast NMIs to force other CPUs out of VMX/SVM as well.k
^ stray "k".
I don't understand the "if necessary" part. My understanding is this code issues NMIs if CPUs support VMX or SVM. If so, I think the code snippet below would be more readable:
if (cpu_feature_enabled(X86_FEATURE_VMX) ||
cpu_feature_enabled(X86_FEATURE_SVM)) {
x86_virt_emergency_disable_virtualization_cpu();
nmi_shootdown_cpus_on_restart();
}Then x86_virt_emergency_disable_virtualization_cpu() wouldn't need to return anything. And readers wouldn't need to trace down the function to understand when NMIs are "necessary" and when they are not.
Quoted 10 lines
arch/x86/include/asm/virt.h new file +1
-6*/ -if (rcu_access_pointer (cpu_emergency_virt_callback)) { -/* Safely force _this_ CPU out of VMX/SVM operation. */ -cpu_emergency_disable_virtualization (); - -/* Disable VMX/SVM and halt on other CPUs. */ +if (!x86_virt_emergency_disable_virtualization_cpu ())nmi_shootdown_cpus_on_restart (); - } }
<snip>
Quoted 15 lines
arch/x86/include/asm/virt.h new file +15
-0+#define x86_virt_call (fn) \ +({ \ + int __r; \ + \ + if (IS_ENABLED(CONFIG_KVM_INTEL) && \ + cpu_feature_enabled(X86_FEATURE_VMX)) \ + __r = x86_vmx_##fn(); \ + else if (IS_ENABLED(CONFIG_KVM_AMD) && \ + cpu_feature_enabled(X86_FEATURE_SVM)) \ + __r = x86_svm_##fn(); \ + else \ + __r = -EOPNOTSUPP; \ + \ + __r; \ +})
arch/x86/include/asm/virt.h
Quoted 10 lines
arch/x86/include/asm/virt.h new file +10
-0+ +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 ;
Should we assert that preemption is disabled? Calling this API when preemption is enabled is wrong.
Maybe use __this_cpu_inc_return(), which already verifies preemption status.
<snip>
Quoted 9 lines
arch/x86/include/asm/virt.h new file +9
-0+int x86_virt_emergency_disable_virtualization_cpu (void ) +{ +if (!x86_virt_feature) +return -EOPNOTSUPP ; + +/* +* IRQs must be disabled as virtualization is enabled in hardware via +* function call IPIs, i.e. IRQs need to be disabled to guarantee +* virtualization stays disabled.
arch/x86/include/asm/virt.h
Quoted 10 lines
arch/x86/include/asm/virt.h new file +10
-0+*/ +lockdep_assert_irqs_disabled (); + +/* +* Do the NMI shootdown even if virtualization is off on _this_ CPU, as +* other CPUs may have virtualization enabled. +* +* TODO: Track whether or not virtualization might be enabled on other +* CPUs? May not be worth avoiding the NMI shootdown... +*/
This comment is misplaced. NMIs are issued by the caller.
Quoted 3 lines
arch/x86/include/asm/virt.h new file +3
-0+ (void )x86_virt_call (emergency_disable_virtualization_cpu); +return 0 ; +}
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 29 lines
Quoted 15 lines
arch/x86/virt/hw.c new file +15
-0+#define x86_virt_call (fn) \ +({ \ + int __r; \ + \ + if (IS_ENABLED(CONFIG_KVM_INTEL) && \ + cpu_feature_enabled(X86_FEATURE_VMX)) \ + __r = x86_vmx_##fn(); \ + else if (IS_ENABLED(CONFIG_KVM_AMD) && \ + cpu_feature_enabled(X86_FEATURE_SVM)) \ + __r = x86_svm_##fn(); \ + else \ + __r = -EOPNOTSUPP; \ + \ + __r; \ +})arch/x86/virt/hw.c line 280
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+ +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 ;Should we assert that preemption is disabled? Calling this API when preemption is enabled is wrong.
Quoted 2 lines
Maybe use
__this_cpu_inc_return(), which already verifies preemption status.
Is it better we explicitly assert the preemption for x86_virt_get_cpu() rather than embed the check in __this_cpu_inc_return()? We are not just protecting the racing for the reference counter. We should ensure the "counter increase + x86_virt_call(get_cpu)" can't be preempted.
Thanks,
Yilun
Sean Christopherson
maintainerRe: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 29 lines · Xu Yilun
Quoted 29 lines
Quoted 15 lines
arch/x86/virt/hw.c new file +15
-0+#define x86_virt_call (fn) \ +({ \ + int __r; \ + \ + if (IS_ENABLED(CONFIG_KVM_INTEL) && \ + cpu_feature_enabled(X86_FEATURE_VMX)) \ + __r = x86_vmx_##fn(); \ + else if (IS_ENABLED(CONFIG_KVM_AMD) && \ + cpu_feature_enabled(X86_FEATURE_SVM)) \ + __r = x86_svm_##fn(); \ + else \ + __r = -EOPNOTSUPP; \ + \ + __r; \ +})arch/x86/virt/hw.c line 280
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+ +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 ;Should we assert that preemption is disabled? Calling this API when preemption is enabled is wrong.
Quoted 1 line
Quoted 1 line
Maybe use
__this_cpu_inc_return(), which already verifies preemption status.
I always forget that the double-underscores have the checks.
Quoted 4 lines
Is it better we explicitly assert the preemption for
x86_virt_get_cpu()rather than embed the check in__this_cpu_inc_return()? We are not just protecting the racing for the reference counter. We should ensure the "counter increase +x86_virt_call(get_cpu)" can't be preempted.
I don't have a strong preference. Using __this_cpu_inc_return() without any nearby preemption_{enable,disable}() calls makes it quite clears that preemption is expected to be disabled by the caller. But I'm also ok being explicit.
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 34 lines · Sean Christopherson
Quoted 29 lines · Xu Yilun
Quoted 29 lines
Quoted 15 lines
arch/x86/virt/hw.c new file +15
-0+#define x86_virt_call (fn) \ +({ \ + int __r; \ + \ + if (IS_ENABLED(CONFIG_KVM_INTEL) && \ + cpu_feature_enabled(X86_FEATURE_VMX)) \ + __r = x86_vmx_##fn(); \ + else if (IS_ENABLED(CONFIG_KVM_AMD) && \ + cpu_feature_enabled(X86_FEATURE_SVM)) \ + __r = x86_svm_##fn(); \ + else \ + __r = -EOPNOTSUPP; \ + \ + __r; \ +})arch/x86/virt/hw.c line 280
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+ +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 ;Should we assert that preemption is disabled? Calling this API when preemption is enabled is wrong.
Quoted 1 line
Quoted 1 line
Maybe use
__this_cpu_inc_return(), which already verifies preemption status.I always forget that the double-underscores have the checks.
Quoted 8 lines
Quoted 4 lines
Is it better we explicitly assert the preemption for
x86_virt_get_cpu()rather than embed the check in__this_cpu_inc_return()? We are not just protecting the racing for the reference counter. We should ensure the "counter increase +x86_virt_call(get_cpu)" can't be preempted.I don't have a strong preference. Using
__this_cpu_inc_return()without any nearby preemption_{enable,disable}() calls makes it quite clears that preemption is expected to be disabled by the caller. But I'm also ok being explicit.
Looking into __this_cpu_inc_return(), it finally calls check_preemption_disabled() which doesn't strictly requires preemption. It only ensures the context doesn't switch to another CPU. If the caller is in cpuhp context, preemption is possible.
But in this x86_virt_get_cpu(), we need to ensure preemption disabled, otherwise caller A increases counter but hasn't do actual VMXON yet and get preempted. Caller B opts in and get the wrong info that VMX is already on, and fails on following vmx operations.
On a second thought, maybe we disable preemption inside x86_virt_get_cpu() to protect the counter-vmxon racing, this is pure internal thing for this kAPI.
Thanks,
Yilun
Sean Christopherson
maintainerRe: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 7 lines · Xu Yilun
Quoted 6 lines · Sean Christopherson
Quoted 4 lines · Xu Yilun
Is it better we explicitly assert the preemption for
x86_virt_get_cpu()rather than embed the check in__this_cpu_inc_return()? We are not just protecting the racing for the reference counter. We should ensure the "counter increase +x86_virt_call(get_cpu)" can't be preempted.
Quoted 8 lines
Quoted 3 lines
I don't have a strong preference. Using
__this_cpu_inc_return()without any nearby preemption_{enable,disable}() calls makes it quite clears that preemption is expected to be disabled by the caller. But I'm also ok being explicit.Looking into
__this_cpu_inc_return(), it finally callscheck_preemption_disabled()which doesn't strictly requires preemption. It only ensures the context doesn't switch to another CPU. If the caller is in cpuhp context, preemption is possible.
Hmm, right, the cpuhp thread is is_percpu_thread(), and KVM's hooks aren't considered atomic and so run with IRQs enabled. In practice, it's "fine", because TDX also exclusively does x86_virt_get_cpu() from cpuhp, i.e. the two users are mutually exclusive, but relying on that behavior is gross.
Quoted 8 lines
But in this
x86_virt_get_cpu(), we need to ensure preemption disabled, otherwise caller A increases counter but hasn't do actual VMXON yet and get preempted. Caller B opts in and get the wrong info that VMX is already on, and fails on following vmx operations.On a second thought, maybe we disable preemption inside
x86_virt_get_cpu()to protect the counter-vmxon racing, this is pure internal thing for this kAPI.
Ya, that'd be my preference.
Kai, question for you (or anyone else that might know):
Is there any **need** for tdx_cpu_enable() and try_init_module_global() to run with IRQs disabled? AFAICT, the lockdep_assert_irqs_disabled() checks added by commit 6162b310bc21 ("x86/virt/tdx: Add skeleton to enable TDX on demand") were purely because, _when the code was written_, KVM enabled virtualization via IPI function calls.
But by the time the KVM code landed upstream in commit fcdbdf63431c ("KVM: VMX:
Initialize TDX during KVM module load"), that was no longer true, thanks to commit 9a798b1337af ("KVM: Register cpuhp and syscore callbacks when enabling hardware") setting the stage for doing everything from task context.
However, rather than update the kernel side, e.g. to drop the lockdep assertions and related comments, commit 9a798b1337af instead did this:
local_irq_save(flags); r = tdx_cpu_enable(); local_irq_restore(flags);
Somewhat frustratingly, I poked at this when the reworked code was first posted
(https://lore.kernel.org/all/[email protected]), but it just got swept under the rug :-(
: > > + /* tdx_cpu_enable() must be called with IRQ disabled */
: >
: > I don't find this comment helpfu. If it explained _why_ tdx_cpu_enable() requires : > IRQs to be disabled, then I'd feel differently, but as is, IMO it doesn't add value. :
: I'll remove the comment.
:
: >
: > > + local_irq_save(flags);
: > > + r = tdx_cpu_enable();
: > > + local_irq_restore(flags);
Unless TDX _needs_ IRQs to be disabled, I would strongly prefer to drop that code in prep patches so that it doesn't become even harder to disentagle the history to figure out why tdx_online_cpu() disables IRQs:
static int tdx_online_cpu(unsigned int cpu)
{
int ret;
guard(irqsave)(); <=============== why is this here!?!?!
ret = x86_virt_get_cpu(X86_FEATURE_VMX);
if (ret)
return ret;
ret = tdx_cpu_enable();
if (ret)
x86_virt_put_cpu(X86_FEATURE_VMX);
return ret;}
Side topic, KVM's change in behavior also means this comment is stale (though I think it's worth keeping the assertion, but with a comment saying it's hardening and paranoia, not a strick requirement).
/* * IRQs must be disabled as virtualization is enabled in hardware via * function call IPIs, i.e. IRQs need to be disabled to guarantee * virtualization stays disabled. */ lockdep_assert_irqs_disabled();
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 5 lines · Sean Christopherson
Is there any **need** for
tdx_cpu_enable()andtry_init_module_global()to run with IRQs disabled? AFAICT, thelockdep_assert_irqs_disabled()checks added by commit6162b310bc21("x86/virt/tdx: Add skeleton to enable TDX on demand") were purely because,_whenthe code waswritten_, KVM enabled virtualization via IPI function calls.
Yeah, the intent was to prevent "rmmod kvm_intel" from running while TDX was being initialized. The way it ended up, though, I think TDX init would have been called in a KVM path _anyway_, which makes this double useless.
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean ChristophersonQuoted 8 lines · Sean Christopherson
Kai, question for you (or anyone else that might know):
Is there any **need** for
tdx_cpu_enable()andtry_init_module_global()to run with IRQs disabled? AFAICT, thelockdep_assert_irqs_disabled()checks added by commit6162b310bc21("x86/virt/tdx: Add skeleton to enable TDX on demand") were purely because,_whenthe code waswritten_, KVM enabled virtualization via IPI function calls.
Quoted 4 lines
But by the time the KVM code landed upstream in commit
fcdbdf63431c("KVM: VMX:
Initialize TDX during KVM module load"), that was no longer true, thanks to commit9a798b1337af("KVM: Register cpuhp and syscore callbacks when enabling hardware") setting the stage for doing everything from task context.
The other intention was to make tdx_cpu_enable() as IRQ safe, since it was supposed to be able to be called by multiple in-kernel users but not just KVM (i.e., also for TDX connect).
But right currently we don't need to call them with IRQ disabled.
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean ChristophersonQuoted 17 lines · Sean Christopherson
Quoted 7 lines · Xu Yilun
Quoted 6 lines · Sean Christopherson
Quoted 4 lines · Xu Yilun
Is it better we explicitly assert the preemption for
x86_virt_get_cpu()rather than embed the check in__this_cpu_inc_return()? We are not just protecting the racing for the reference counter. We should ensure the "counter increase +x86_virt_call(get_cpu)" can't be preempted.Quoted 8 lines
Quoted 3 lines
I don't have a strong preference. Using
__this_cpu_inc_return()without any nearby preemption_{enable,disable}() calls makes it quite clears that preemption is expected to be disabled by the caller. But I'm also ok being explicit.Looking into
__this_cpu_inc_return(), it finally callscheck_preemption_disabled()which doesn't strictly requires preemption. It only ensures the context doesn't switch to another CPU. If the caller is in cpuhp context, preemption is possible.
Quoted 5 lines
Hmm, right, the cpuhp thread is
is_percpu_thread(), and KVM's hooks aren't considered atomic and so run with IRQs enabled. In practice, it's "fine", because TDX also exclusively doesx86_virt_get_cpu()from cpuhp, i.e. the two users are mutually exclusive, but relying on that behavior is gross.
Side topic:
Isn't the name of check_preemption_disabled() confusing and arguably misleading?
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
In reply to Sean ChristophersonQuoted 11 lines · Sean Christopherson
Move the innermost VMXON and EFER.SVME management logic out of KVM and into to core x86 so that TDX can force VMXON without having to rely on KVM being loaded, e.g. to do SEAMCALLs during initialization.
Implement a per-CPU refcounting scheme so that "users", e.g. KVM and the future TDX code, can co-exist without pulling the rug out from under each other.
To avoid having to choose between SVM and VMX, simply refuse to enable either if both are somehow supported. No known CPU supports both SVM and VMX, and it's comically unlikely such a CPU will ever exist.
Yeah, that's totally a "please share your drugs" moment, if it happens.
Quoted 7 lines
arch/x86/virt/hw.c new file +7
-0+static int x86_vmx_get_cpu (void ) +{ +int r; + +if (cr4_read_shadow () &X86_CR4_VMXE ) +return -EBUSY ; +
arch/x86/virt/hw.c line 91
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+intel_pt_handle_vmx (1 ); + + r =x86_virt_cpu_vmxon (); +if (r) { +intel_pt_handle_vmx (0 ); +return r; + } + +return 0 ; +}
...> +#define x86_virt_call(fn) \
Quoted 4 lines
arch/x86/virt/hw.c new file +4
-0+({ \ + int __r; \ + \ + if (IS_ENABLED(CONFIG_KVM_INTEL) && \
arch/x86/virt/hw.c line 270
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+ cpu_feature_enabled(X86_FEATURE_VMX)) \ + __r = x86_vmx_##fn(); \ + else if (IS_ENABLED(CONFIG_KVM_AMD) && \ + cpu_feature_enabled(X86_FEATURE_SVM)) \ + __r = x86_svm_##fn(); \ + else \ + __r = -EOPNOTSUPP; \ + \ + __r; \ +})
I'm not a super big fan of this. I know you KVM folks love your macros and wrapping function calls in them because you hate grep. ;)
I don't like a foo_get_cpu() call having such fundamentally different semantics than good old get_cpu() itself. *Especially* when the calls look like:
r = x86_virt_call(get_cpu);
and get_cpu() itself it not invovled one bit. This 100% looks like it's some kind of virt-specific call for get_cpu().
I think it's probably OK to make this get_hw_ref() or inc_hw_ref() or something to get it away from getting confused with get_cpu().
IMNHO, the macro magic is overkill. A couple of global function pointers would probably be fine because none of this code is even remotely performance sensitive. A couple static_call()s would be fine too because those at least make it blatantly obvious that the thing being called is variable. A good ol' ops structure would also make things obvious, but are probably also overkill-adjecent for this.
P.S. In a perfect world, the renames would also be in their own patches, but I think I can live with it as-is.
Sean Christopherson
maintainerRe: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 28 lines · Dave Hansen
Quoted 7 lines · Sean Christopherson
arch/x86/virt/hw.c new file +7
-0+static int x86_vmx_get_cpu (void ) +{ +int r; + +if (cr4_read_shadow () &X86_CR4_VMXE ) +return -EBUSY ; +arch/x86/virt/hw.c line 91
Quoted 10 lines
arch/x86/virt/hw.c new file +10
-0+intel_pt_handle_vmx (1 ); + + r =x86_virt_cpu_vmxon (); +if (r) { +intel_pt_handle_vmx (0 ); +return r; + } + +return 0 ; +}...> +#define
x86_virt_call(fn)\Quoted 9 lines
arch/x86/virt/hw.c new file +9
-0+({ \ + int __r; \ + \ + if (IS_ENABLED(CONFIG_KVM_INTEL) && \ + cpu_feature_enabled(X86_FEATURE_VMX)) \ + __r = x86_vmx_##fn(); \ + else if (IS_ENABLED(CONFIG_KVM_AMD) && \ + cpu_feature_enabled(X86_FEATURE_SVM)) \ + __r = x86_svm_##fn(); \
Quoted 8 lines
arch/x86/virt/hw.c line 270
Quoted 5 lines
arch/x86/virt/hw.c new file +5
-0+ else \ + __r = -EOPNOTSUPP ; \ + \ + __r; \ +})I'm not a super big fan of this. I know you KVM folks love your macros and wrapping function calls in them because you hate grep. ;)
Heh, kvm_x86_call() exists _because_ I like grep and search functionality. The number of times I couldn't find something because I was searching for full words and forgot about the kvm_x86_ prefix...
Quoted 11 lines
I don't like a
foo_get_cpu()call having such fundamentally different semantics than good oldget_cpu()itself. *Especially* when the calls look like:r = x86_virt_call(get_cpu);and
get_cpu()itself it not invovled one bit. This 100% looks like it's some kind of virt-specific call forget_cpu().I think it's probably OK to make this
get_hw_ref()orinc_hw_ref()or something to get it away from getting confused withget_cpu().
Oof, yeah, didn't think about a collision with {get,put}_cpu(). How about x86_virt_{get,put}_ref()? I like how the callers read, e.g. "get a reference to VMX or SVM":
x86_virt_get_ref(X86_FEATURE_VMX); x86_virt_put_ref(X86_FEATURE_VMX); x86_virt_get_ref(X86_FEATURE_SVM); x86_virt_put_ref(X86_FEATURE_SVM);
Quoted 6 lines
IMNHO, the macro magic is overkill. A couple of global function pointers would probably be fine because none of this code is even remotely performance sensitive. A couple static_call()s would be fine too because those at least make it blatantly obvious that the thing being called is variable. A good ol' ops structure would also make things obvious, but are probably also overkill-adjecent for this.
Agreed. I'm not even entirely sure why I took this approach. I suspect I carried over the basic concept from code that wanted to run before wiring up function pointers, and never revisited the implementation once the dust settled.
I haven't tested yet, but I've got this:
struct x86_virt_ops {
int feature;
int (*enable_virtualization_cpu)(void);
int (*disable_virtualization_cpu)(void);
void (*emergency_disable_virtualization_cpu)(void); };
static struct x86_virt_ops virt_ops __ro_after_init;
and then usage like:
int x86_virt_get_ref(int feat)
{
int r;
if (!virt_ops.feature || virt_ops.feature != feat)
return -EOPNOTSUPP;
if (this_cpu_inc_return(virtualization_nr_users) > 1)
return 0;
r = virt_ops.enable_virtualization_cpu();
if (r)
WARN_ON_ONCE(this_cpu_dec_return(virtualization_nr_users));
return r; }
EXPORT_SYMBOL_GPL(x86_virt_get_ref);
void x86_virt_put_ref(int feat)
{
if (WARN_ON_ONCE(!this_cpu_read(virtualization_nr_users)) ||
this_cpu_dec_return(virtualization_nr_users))
return;
BUG_ON(virt_ops.disable_virtualization_cpu() && !virt_rebooting); }
EXPORT_SYMBOL_GPL(x86_virt_put_ref);
Quoted 2 lines
P.S. In a perfect world, the renames would also be in their own patches, but I think I can live with it as-is.
Ya, I'll chunk the patch up.
Re: [PATCH v2 2/7] KVM: x86: Extract VMXON and EFER.SVME enablement to kernel
Quoted 27 lines · Sean Christopherson
I haven't tested yet, but I've got this:
struct x86_virt_ops { int feature; int (*enable_virtualization_cpu)(void); int (*disable_virtualization_cpu)(void); void (*emergency_disable_virtualization_cpu)(void);};
static structx86_virt_opsvirt_ops__ro_after_init;and then usage like:
int
x86_virt_get_ref(int feat){ int r; if (!virt_ops.feature || virt_ops.feature != feat) return -EOPNOTSUPP; if (this_cpu_inc_return(virtualization_nr_users) > 1) return 0; r = virt_ops.enable_virtualization_cpu(); if (r) WARN_ON_ONCE(this_cpu_dec_return(virtualization_nr_users)); return r;
Quoted 2 lines
}
EXPORT_SYMBOL_GPL(x86_virt_get_ref);
Yeah, that's much easier to follow, and fixes the naming collision thanks!
Sean Christopherson
maintainerPATCH v2 3/7 KVM: x86/tdx: Do VMXON and TDX-Module initialization during subsys init
In reply to Sean ChristophersonNow that VMXON can be done without bouncing through KVM, do TDX-Module initialization during subsys init (specifically before module_init() so that it runs before KVM when both are built-in). Aside from the obvious benefits of separating core TDX code from KVM, this will allow tagging a pile of TDX functions and globals as being __init and __ro_after_init.
--- Documentation/arch/x86/tdx.rst | 26 ----- arch/x86/include/asm/tdx.h | 4 - arch/x86/kvm/vmx/tdx.c | 169 ++++++-------------------------- arch/x86/virt/vmx/tdx/tdx.c | 170 ++++++++++++++++++--------------- arch/x86/virt/vmx/tdx/tdx.h | 8 -- 5 files changed, 124 insertions(+), 253 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
@@ -145,8 +145,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 );
@@ -223,8 +221,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 +31 -138
236 lines not on this page · raw message
arch/x86/virt/vmx/tdx/tdx.c +93 -77
233 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;
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v2 3/7] KVM: x86/tdx: Do VMXON and TDX-Module initialization during subsys init
Quoted 6 lines · Sean Christopherson
Now that VMXON can be done without bouncing through KVM, do TDX-Module initialization during subsys init (specifically before
module_init()so that it runs before KVM when both are built-in). Aside from the obvious benefits of separating core TDX code from KVM, this will allow tagging a pile of TDX functions and globals as being__initand__ro_after_init.
Quoted 8 lines
Signed-off-by Sean Christopherson <[email protected]>--- Documentation/arch/x86/tdx.rst | 26 ----- arch/x86/include/asm/tdx.h | 4 - arch/x86/kvm/vmx/tdx.c | 169 ++++++-------------------------- arch/x86/virt/vmx/tdx/tdx.c | 170 ++++++++++++++++++--------------- arch/x86/virt/vmx/tdx/tdx.h | 8 -- 5 files changed, 124 insertions(+), 253 deletions(-)
yes!
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +0
-3@@ -3304,17 +3304,7 @@ int tdx_gmem_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, bool is_private)static int tdx_online_cpu (unsigned int cpu) { -unsigned long flags; -int r; -
arch/x86/kvm/vmx/tdx.c int tdx_gmem_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn, bool is_private) · line 3308
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +1
-8-/* Sanity check CPU is already in post-VMXON */ -WARN_ON_ONCE (!(cr4_read_shadow () &X86_CR4_VMXE )); - -local_irq_save (flags); - r =tdx_cpu_enable (); -local_irq_restore (flags); - -return r; +return 0 ; }
Given this routine now has nothing to do...
arch/x86/kvm/vmx/tdx.c static int __init __tdx_bringup(void) · line 3424
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +6
-0+* 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);
...the @startup param can be NULL. That also saves some grep pain no more multiple implementations of a "tdx_online_cpu".
Along those lines, should tdx_offline_cpu() become kvm_tdx_offline_cpu()?
[..]
arch/x86/virt/vmx/tdx/tdx.c static int init_tdx_module(void) · line 1197
Quoted 8 lines
/*
* Add a memory region as a TDX memory block. The caller must make surearch/x86/virt/vmx/tdx/tdx.c +1
-1@@ -1156,67 +1194,50 @@ static int init_tdx_module(void) goto out_put_tdxmem; } -static int __tdx_enable (void ) +static int tdx_enable (void )
Almost commented about this being able to be __init now, but then I see you have a combo patch for that later.
With or without the additional tdx_{on,off}line_cpu fixups:
Sean Christopherson
maintainerRe: [PATCH v2 3/7] KVM: x86/tdx: Do VMXON and TDX-Module initialization during subsys init
Quoted 24 lines · On Sat, Dec 06, 2025, [email protected] wrote:
Sean Christopherson wrote:
Given this routine now has nothing to do...arch/x86/kvm/vmx/tdx.c static int __init __tdx_bringup(void) · line 3424
Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +6
-0+* 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);...the @startup param can be NULL. That also saves some grep pain no more multiple implementations of a "
tdx_online_cpu".Along those lines, should
tdx_offline_cpu()becomekvm_tdx_offline_cpu()?[..]
Quoted 6 lines
/*
* Add a memory region as a TDX memory block. The caller must make surearch/x86/virt/vmx/tdx/tdx.c +0
-0@@ -1156,67 +1194,50 @@ static int init_tdx_module(void) goto out_put_tdxmem; }
Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c static int init_tdx_module(void) · line 1197
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +1
-1-static int __tdx_enable (void ) +static int tdx_enable (void )Almost commented about this being able to be
__initnow, but then I see you have a combo patch for that later.With or without the additional tdx_{on,off}line_cpu fixups:
I think the fixups you're looking for are in patch 5, "/virt/tdx: KVM: Consolidate TDX CPU hotplug handling", or did I misunderstand?
Re: [PATCH v2 3/7] KVM: x86/tdx: Do VMXON and TDX-Module initialization during subsys init
Sean Christopherson wrote:
[..]
Quoted 4 lines
Quoted 1 line
With or without the additional tdx_{on,off}line_cpu fixups:
I think the fixups you're looking for are in patch 5, "/virt/tdx: KVM: Consolidate TDX CPU hotplug handling", or did I misunderstand?
Oh, yes, all addressed there. Should have finished going through it before replying.
Re: [PATCH v2 3/7] KVM: x86/tdx: Do VMXON and TDX-Module initialization during subsys init
In reply to Sean ChristophersonQuoted 39 lines · Sean Christopherson
Now that VMXON can be done without bouncing through KVM, do TDX-Module initialization during subsys init (specifically before
module_init()so that it runs before KVM when both are built-in). Aside from the obvious benefits of separating core TDX code from KVM, this will allow tagging a pile of TDX functions and globals as being__initand__ro_after_init.Signed-off-by Sean Christopherson <[email protected]>---
Documentation/arch/x86/tdx.rst | 26 ----- arch/x86/include/asm/tdx.h | 4 - arch/x86/kvm/vmx/tdx.c | 169 ++++++-------------------------- arch/x86/virt/vmx/tdx/tdx.c | 170 ++++++++++++++++++--------------- arch/x86/virt/vmx/tdx/tdx.h | 8 -- 5 files changed, 124 insertions(+), 253 deletions(-)Documentation/arch/x86/tdx.rst +0
-0@@ -60,32 +60,6 @@ Besides initializing the TDX module, a per-cpu initialization SEAMCALLmust be done on one cpu before any other SEAMCALLs can be made on that cpu.
Documentation/arch/x86/tdx.rst +0
-16-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();
Documentation/arch/x86/tdx.rst
Quoted 10 lines
Documentation/arch/x86/tdx.rst +0
-10- 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. -
With this patch applied, other parts of the document will be stale and need updates, i.e.,:
Documentation/arch/x86/tdx.rst +2 -7
@@ -66,12 +66,12 @@ If the TDX module is initialized successfully, dmesg shows something like below:: [..] virt/tdx: 262668 KBs allocated for PAMT - [..] virt/tdx: module initialized + [..] virt/tdx: TDX-Module initialized If the TDX module failed to initialize, dmesg also shows it failed to initialize:: - [..] virt/tdx: module initialization failed ... + [..] virt/tdx: TDX-Module initialization failed ... TDX Interaction to Other Kernel Components ------------------------------------------
@@ -102,11 +102,6 @@ removal but depends on the BIOS to behave correctly. CPU Hotplug ~~~~~~~~~~~ -TDX module requires the per-cpu initialization SEAMCALL must be done on -one cpu before any other SEAMCALLs can be made on that cpu. The kernel -provides tdx_cpu_enable() to let the user of TDX to do it when the user -wants to use a new cpu for TDX task. - TDX doesn't support physical (ACPI) CPU hotplug. During machine boot, TDX verifies all boot-time present logical CPUs are TDX compatible before enabling TDX. A non-buggy BIOS should never support hot-add/removal of
Quoted 29 lines
static int
__init__tdx_bringup(void){ const struct tdx_sys_info_td_conf *td_conf;Documentation/arch/x86/tdx.rst +2
-18@@ -3417,34 +3362,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;
...
Quoted 17 lines
Documentation/arch/x86/tdx.rst +0
-13- /* - * 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();Documentation/arch/x86/tdx.rst +0
-3- if (r) { - /* - * Disable TDX only but don't fail to load module if the TDX
Documentation/arch/x86/tdx.rst
Quoted 10 lines
Documentation/arch/x86/tdx.rst +0
-10- * 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, - * 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;
Previously, loading kvm-intel.ko (with tdx=1) would succeed even if there was no TDX module loaded by BIOS. IIUC, the behavior changes here; the lack of TDX module becomes fatal and kvm-intel.ko loading would fail.
Is this intentional?
Quoted 10 lines
Documentation/arch/x86/tdx.rst +1
-1- + if (r)enable_tdx = 0;Documentation/arch/x86/tdx.rst +0
-1- }return r;Documentation/arch/x86/tdx.rst +0
-0@@ -3596,6 +3480,15 @@ int __init tdx_bringup(void) return 0; }
Sean Christopherson
maintainerRe: [PATCH v2 3/7] KVM: x86/tdx: Do VMXON and TDX-Module initialization during subsys init
Quoted 65 lines · Chao Gao
Quoted 29 lines · Sean Christopherson
static int
__init__tdx_bringup(void){ const struct tdx_sys_info_td_conf *td_conf;(quoted diff) +2
-18@@ -3417,34 +3362,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;...
Quoted 17 lines
(quoted diff) +0
-13- /* - * 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();(quoted diff) +0
-3- if (r) { - /* - * Disable TDX only but don't fail to load module if the TDXReview
Quoted 10 lines
(quoted diff) +0
-10- * 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, - * 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;Previously, loading kvm-intel.ko (with tdx=1) would succeed even if there was no TDX module loaded by BIOS. IIUC, the behavior changes here; the lack of TDX module becomes fatal and kvm-intel.ko loading would fail.
Quoted 1 line
Is this intentional?
Nope, definitely not intentional. I think this as fixup?
arch/x86/kvm/vmx/tdx.c +15 -3
@@ -3365,7 +3365,7 @@ static int __init __tdx_bringup(void)/* Get TDX global information for later use */ tdx_sysinfo =tdx_get_sysinfo ();if (!tdx_sysinfo) -return -EINVAL ; +return -ENODEV ;/* Check TDX module and KVM capabilities */ if (!tdx_get_supported_attrs(&tdx_sysinfo->td_conf) ||
@@ -3470,8 +3470,20 @@ int __init tdx_bringup(void) } r =__tdx_bringup (); -if (r) - enable_tdx =0 ; +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, +* 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; + }return r;
Sean Christopherson
maintainerPATCH v2 4/7 x86/virt/tdx: Tag a pile of functions as __init, and globals as __ro_after_init
In reply to Sean ChristophersonNow that TDX-Module initialization is done during subsys init, tag all related functions as __init, and relevant data as __ro_after_init.
--- arch/x86/virt/vmx/tdx/tdx.c | 115 ++++++++++---------- arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 10 +- 2 files changed, 64 insertions(+), 61 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +59 -56
@@ -55,8 +55,8 @@ static struct tdmr_info_list tdx_tdmr_list;/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */ static LIST_HEAD (tdx_memlist); -static struct tdx_sys_info tdx_sysinfo; -static bool tdx_module_initialized; +static struct tdx_sys_info tdx_sysinfo__ro_after_init ; +static bool tdx_module_initialized__ro_after_init ;typedef void (*sc_err_func_t )(u64 fn,u64 err,struct tdx_module_args *args);
@@ -226,8 +226,9 @@ static struct syscore_ops tdx_syscore_ops = {* all memory regions are added in address ascending order and don't * overlap. */ -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) {struct tdx_memblock *tmb;
@@ -245,7 +246,7 @@ static int add_tdx_memblock(struct list_head *tmb_list, unsigned long start_pfn,return 0 ; } -static void free_tdx_memlist (struct list_head *tmb_list) +static __init void free_tdx_memlist (struct list_head *tmb_list) {/* @tmb_list is protected by mem_hotplug_lock */ while (!list_empty (tmb_list)) {
@@ -263,7 +264,7 @@ static void free_tdx_memlist(struct list_head *tmb_list)* ranges off in a secondary structure because memblock is modified * in memory hotplug while TDX memory regions are fixed. */ -static int build_tdx_memlist (struct list_head *tmb_list) +static __init int build_tdx_memlist (struct list_head *tmb_list) {unsigned long start_pfn, end_pfn;int i, nid, ret;
@@ -295,7 +296,7 @@ static int build_tdx_memlist(struct list_head *tmb_list)return ret; } -static int read_sys_metadata_field (u64 field_id,u64 *data) +static __init int read_sys_metadata_field (u64 field_id,u64 *data) {struct tdx_module_args args = {};int ret;
@@ -317,7 +318,7 @@ static int read_sys_metadata_field(u64 field_id, u64 *data)#include "tdx_global_metadata.c" -static int check_features (struct tdx_sys_info *sysinfo) +static __init int check_features (struct tdx_sys_info *sysinfo) {u64 tdx_features0 = sysinfo->features.tdx_features0;
@@ -330,7 +331,7 @@ static int check_features(struct tdx_sys_info *sysinfo) }/* Calculate the actual TDMR size */ -static int tdmr_size_single (u16 max_reserved_per_tdmr) +static __init int tdmr_size_single (u16 max_reserved_per_tdmr) {int tdmr_sz;
@@ -344,8 +345,8 @@ static int tdmr_size_single(u16 max_reserved_per_tdmr)return ALIGN (tdmr_sz,TDMR_INFO_ALIGNMENT ); } -static int alloc_tdmr_list (struct tdmr_info_list *tdmr_list, -struct tdx_sys_info_tdmr *sysinfo_tdmr) +static __init int alloc_tdmr_list (struct tdmr_info_list *tdmr_list, +struct tdx_sys_info_tdmr *sysinfo_tdmr) {size_t tdmr_sz, tdmr_array_sz;void *tdmr_array;
@@ -376,7 +377,7 @@ static int alloc_tdmr_list(struct tdmr_info_list *tdmr_list,return 0 ; } -static void free_tdmr_list (struct tdmr_info_list *tdmr_list) +static __init void free_tdmr_list (struct tdmr_info_list *tdmr_list) {free_pages_exact (tdmr_list->tdmrs, tdmr_list->max_tdmrs * tdmr_list->tdmr_sz);
@@ -405,8 +406,8 @@ static inline u64 tdmr_end(struct tdmr_info *tdmr)* preallocated @tdmr_list, following all the special alignment * and size rules for TDMR. */ -static int fill_out_tdmrs (struct list_head *tmb_list, -struct tdmr_info_list *tdmr_list) +static __init int fill_out_tdmrs (struct list_head *tmb_list, +struct tdmr_info_list *tdmr_list) {struct tdx_memblock *tmb;int tdmr_idx =0 ;
@@ -482,8 +483,8 @@ static int fill_out_tdmrs(struct list_head *tmb_list,* Calculate PAMT size given a TDMR and a page size. The returned * PAMT size is always aligned up to 4K page boundary. */ -static unsigned long tdmr_get_pamt_sz (struct tdmr_info *tdmr,int pgsz, -u16 pamt_entry_size) +static __init unsigned long tdmr_get_pamt_sz (struct tdmr_info *tdmr,int pgsz, +u16 pamt_entry_size) {unsigned long pamt_sz, nr_pamt_entries;
@@ -514,7 +515,7 @@ static unsigned long tdmr_get_pamt_sz(struct tdmr_info *tdmr, int pgsz,* PAMT. This node will have some memory covered by the TDMR. The * relative amount of memory covered is not considered. */ -static int tdmr_get_nid (struct tdmr_info *tdmr,struct list_head *tmb_list) +static __init int tdmr_get_nid (struct tdmr_info *tdmr,struct list_head *tmb_list) {struct tdx_memblock *tmb;
@@ -543,9 +544,9 @@ static int tdmr_get_nid(struct tdmr_info *tdmr, struct list_head *tmb_list)* Allocate PAMTs from the local NUMA node of some memory in @tmb_list * within @tdmr, and set up PAMTs for @tdmr. */ -static int tdmr_set_up_pamt (struct tdmr_info *tdmr, -struct list_head *tmb_list, -u16 pamt_entry_size[]) +static __init int tdmr_set_up_pamt (struct tdmr_info *tdmr, +struct list_head *tmb_list, +u16 pamt_entry_size[]) {unsigned long pamt_base[TDX_PS_NR ];unsigned long pamt_size[TDX_PS_NR ];
@@ -615,7 +616,7 @@ static void tdmr_get_pamt(struct tdmr_info *tdmr, unsigned long *pamt_base, *pamt_size = pamt_sz; } -static void tdmr_do_pamt_func (struct tdmr_info *tdmr, +static __init void tdmr_do_pamt_func (struct tdmr_info *tdmr,void (*pamt_func)(unsigned long base,unsigned long size)) {unsigned long pamt_base, pamt_size;
@@ -632,17 +633,17 @@ static void tdmr_do_pamt_func(struct tdmr_info *tdmr,pamt_func (pamt_base, pamt_size); } -static void free_pamt (unsigned long pamt_base,unsigned long pamt_size) +static __init void free_pamt (unsigned long pamt_base,unsigned long pamt_size) {free_contig_range (pamt_base >>PAGE_SHIFT , pamt_size >>PAGE_SHIFT ); } -static void tdmr_free_pamt (struct tdmr_info *tdmr) +static __init void tdmr_free_pamt (struct tdmr_info *tdmr) {tdmr_do_pamt_func (tdmr, free_pamt); } -static void tdmrs_free_pamt_all (struct tdmr_info_list *tdmr_list) +static __init void tdmrs_free_pamt_all (struct tdmr_info_list *tdmr_list) {int i;
@@ -651,9 +652,9 @@ static void tdmrs_free_pamt_all(struct tdmr_info_list *tdmr_list) }/* Allocate and set up PAMTs for all TDMRs */ -static int tdmrs_set_up_pamt_all (struct tdmr_info_list *tdmr_list, -struct list_head *tmb_list, -u16 pamt_entry_size[]) +static __init int tdmrs_set_up_pamt_all (struct tdmr_info_list *tdmr_list, +struct list_head *tmb_list, +u16 pamt_entry_size[]) {int i, ret =0 ;
@@ -702,12 +703,13 @@ void tdx_quirk_reset_page(struct page *page) }EXPORT_SYMBOL_GPL (tdx_quirk_reset_page); -static void tdmr_quirk_reset_pamt (struct tdmr_info *tdmr) +static __init void tdmr_quirk_reset_pamt (struct tdmr_info *tdmr) + {tdmr_do_pamt_func (tdmr, tdx_quirk_reset_paddr); } -static void tdmrs_quirk_reset_pamt_all (struct tdmr_info_list *tdmr_list) +static __init void tdmrs_quirk_reset_pamt_all (struct tdmr_info_list *tdmr_list) {int i;
@@ -715,7 +717,7 @@ static void tdmrs_quirk_reset_pamt_all(struct tdmr_info_list *tdmr_list)tdmr_quirk_reset_pamt (tdmr_entry (tdmr_list, i)); } -static unsigned long tdmrs_count_pamt_kb (struct tdmr_info_list *tdmr_list) +static __init unsigned long tdmrs_count_pamt_kb (struct tdmr_info_list *tdmr_list) {unsigned long pamt_size =0 ;int i;
@@ -730,8 +732,8 @@ static unsigned long tdmrs_count_pamt_kb(struct tdmr_info_list *tdmr_list)return pamt_size /1024 ; } -static int tdmr_add_rsvd_area (struct tdmr_info *tdmr,int *p_idx,u64 addr, -u64 size,u16 max_reserved_per_tdmr) +static __init int tdmr_add_rsvd_area (struct tdmr_info *tdmr,int *p_idx, +u64 addr,u64 size,u16 max_reserved_per_tdmr) {struct tdmr_reserved_area *rsvd_areas = tdmr->reserved_areas;int idx = *p_idx;
@@ -764,10 +766,10 @@ static int tdmr_add_rsvd_area(struct tdmr_info *tdmr, int *p_idx, u64 addr,* those holes fall within @tdmr, set up a TDMR reserved area to cover * the hole. */ -static int tdmr_populate_rsvd_holes (struct list_head *tmb_list, -struct tdmr_info *tdmr, -int *rsvd_idx, -u16 max_reserved_per_tdmr) +static __init int tdmr_populate_rsvd_holes (struct list_head *tmb_list, +struct tdmr_info *tdmr, +int *rsvd_idx, +u16 max_reserved_per_tdmr) {struct tdx_memblock *tmb;u64 prev_end;
@@ -828,10 +830,10 @@ static int tdmr_populate_rsvd_holes(struct list_head *tmb_list,* overlaps with @tdmr, set up a TDMR reserved area to cover the * overlapping part. */ -static int tdmr_populate_rsvd_pamts (struct tdmr_info_list *tdmr_list, -struct tdmr_info *tdmr, -int *rsvd_idx, -u16 max_reserved_per_tdmr) +static __init int tdmr_populate_rsvd_pamts (struct tdmr_info_list *tdmr_list, +struct tdmr_info *tdmr, +int *rsvd_idx, +u16 max_reserved_per_tdmr) {int i, ret;
@@ -866,7 +868,7 @@ static int tdmr_populate_rsvd_pamts(struct tdmr_info_list *tdmr_list, }/* Compare function called by sort() for TDMR reserved areas */ -static int rsvd_area_cmp_func (const void *a,const void *b) +static __init int rsvd_area_cmp_func (const void *a,const void *b) {struct tdmr_reserved_area *r1 = (struct tdmr_reserved_area *)a;struct tdmr_reserved_area *r2 = (struct tdmr_reserved_area *)b;
@@ -885,10 +887,10 @@ static int rsvd_area_cmp_func(const void *a, const void *b)* Populate reserved areas for the given @tdmr, including memory holes * (via @tmb_list) and PAMTs (via @tdmr_list). */ -static int tdmr_populate_rsvd_areas (struct tdmr_info *tdmr, -struct list_head *tmb_list, -struct tdmr_info_list *tdmr_list, -u16 max_reserved_per_tdmr) +static __init int tdmr_populate_rsvd_areas (struct tdmr_info *tdmr, +struct list_head *tmb_list, +struct tdmr_info_list *tdmr_list, +u16 max_reserved_per_tdmr) {int ret, rsvd_idx =0 ;
@@ -913,9 +915,9 @@ static int tdmr_populate_rsvd_areas(struct tdmr_info *tdmr,* Populate reserved areas for all TDMRs in @tdmr_list, including memory * holes (via @tmb_list) and PAMTs. */ -static int tdmrs_populate_rsvd_areas_all (struct tdmr_info_list *tdmr_list, -struct list_head *tmb_list, -u16 max_reserved_per_tdmr) +static __init int tdmrs_populate_rsvd_areas_all (struct tdmr_info_list *tdmr_list, +struct list_head *tmb_list, +u16 max_reserved_per_tdmr) {int i;
@@ -936,9 +938,9 @@ static int tdmrs_populate_rsvd_areas_all(struct tdmr_info_list *tdmr_list,* to cover all TDX memory regions in @tmb_list based on the TDX module * TDMR global information in @sysinfo_tdmr. */ -static int construct_tdmrs (struct list_head *tmb_list, -struct tdmr_info_list *tdmr_list, -struct tdx_sys_info_tdmr *sysinfo_tdmr) +static __init int construct_tdmrs (struct list_head *tmb_list, +struct tdmr_info_list *tdmr_list, +struct tdx_sys_info_tdmr *sysinfo_tdmr) {u16 pamt_entry_size[TDX_PS_NR ] = { sysinfo_tdmr->pamt_4k_entry_size,
@@ -970,7 +972,8 @@ static int construct_tdmrs(struct list_head *tmb_list,return ret; } -static int config_tdx_module (struct tdmr_info_list *tdmr_list,u64 global_keyid) +static __init int config_tdx_module (struct tdmr_info_list *tdmr_list, +u64 global_keyid) {struct tdx_module_args args = {};u64 *tdmr_pa_array;
@@ -1063,7 +1066,7 @@ static int config_global_keyid(void)return ret; } -static int init_tdmr (struct tdmr_info *tdmr) +static __init int init_tdmr (struct tdmr_info *tdmr) {u64 next;
@@ -1094,7 +1097,7 @@ static int init_tdmr(struct tdmr_info *tdmr)return 0 ; } -static int init_tdmrs (struct tdmr_info_list *tdmr_list) +static __init int init_tdmrs (struct tdmr_info_list *tdmr_list) {int i;
@@ -1113,7 +1116,7 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list)return 0 ; } -static int init_tdx_module (void ) +static __init int init_tdx_module (void ) {int ret;
@@ -1194,7 +1197,7 @@ static int init_tdx_module(void) goto out_put_tdxmem; } -static int tdx_enable (void ) +static __init int tdx_enable (void ) {enum cpuhp_state state;int ret;
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.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v2 4/7] x86/virt/tdx: Tag a pile of functions as __init, and globals as __ro_after_init
Quoted 4 lines · Sean Christopherson
Now that TDX-Module initialization is done during subsys init, tag all related functions as
__init, and relevant data as__ro_after_init.Signed-off-by Sean Christopherson <[email protected]>
Looks good,
Re: [PATCH v2 4/7] x86/virt/tdx: Tag a pile of functions as __init, and globals as __ro_after_init
In reply to Sean ChristophersonQuoted 4 lines · Sean Christopherson
Now that TDX-Module initialization is done during subsys init, tag all related functions as
__init, and relevant data as__ro_after_init.Signed-off-by Sean Christopherson <[email protected]>
Two nits:
1. do_global_key_config() and config_global_keyid() can be tagged as __init.
2. Double space after the __init tag of free_pamt()
arch/x86/virt/vmx/tdx/tdx.c static void tdmr_do_pamt_func(struct tdmr_info *tdmr, · line 636
Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +1
-1-static void free_pamt (unsigned long pamt_base,unsigned long pamt_size) +static __init void free_pamt (unsigned long pamt_base,unsigned long pamt_size)
^^
Quoted 4 lines
{
free_contig_range(pamt_base >> PAGE_SHIFT, pamt_size >> PAGE_SHIFT);
}Sean Christopherson
maintainerPATCH v2 5/7 x86/virt/tdx: KVM: Consolidate TDX CPU hotplug handling
In reply to Sean ChristophersonFrom: Chao Gao <[email protected]>
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 +1 -66
@@ -59,8 +59,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)
@@ -219,8 +217,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 ;
@@ -268,7 +264,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 ;
@@ -2399,8 +2394,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;
@@ -3302,51 +3295,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++) {/*
@@ -3414,23 +3366,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 )
@@ -3486,7 +3422,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 +46 -3
73 lines not on this page · raw message
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v2 5/7] x86/virt/tdx: KVM: Consolidate TDX CPU hotplug handling
Quoted 9 lines · Sean Christopherson
From: Chao Gao <[email protected]>
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.
Quoted 6 lines
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.
Looks good,
Sean Christopherson
maintainerPATCH v2 6/7 x86/virt/tdx: Use ida_is_empty() to detect if any TDs may be running
In reply to Sean ChristophersonDrop nr_configured_hkid and instead use ida_is_empty() to detect if any HKIDs have been allocated/configured.
--- arch/x86/virt/vmx/tdx/tdx.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)
arch/x86/virt/vmx/tdx/tdx.c +4 -13
@@ -58,8 +58,6 @@ 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)
@@ -195,7 +193,7 @@ 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)) +if (ida_is_empty (&tdx_guest_keyid_pool)) goto done;/*
@@ -1542,22 +1540,15 @@ EXPORT_SYMBOL_GPL(tdx_get_nr_guest_keyids);int tdx_guest_keyid_alloc (void ) { -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; +return ida_alloc_range (&tdx_guest_keyid_pool, tdx_guest_keyid_start, + tdx_guest_keyid_start + tdx_nr_guest_keyids -1 , +GFP_KERNEL ); }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.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v2 6/7] x86/virt/tdx: Use ida_is_empty() to detect if any TDs may be running
Quoted 5 lines · Sean Christopherson
Drop
nr_configured_hkidand instead useida_is_empty()to detect if any HKIDs have been allocated/configured.Suggested-by Dan Williams <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Re: [PATCH v2 6/7] x86/virt/tdx: Use ida_is_empty() to detect if any TDs may be running
In reply to Sean ChristophersonQuoted 5 lines · Sean Christopherson
Drop
nr_configured_hkidand instead useida_is_empty()to detect if any HKIDs have been allocated/configured.Suggested-by Dan Williams <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v2 7/7 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
@@ -2596,12 +2596,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 ();
@@ -5693,7 +5696,7 @@ static struct syscore_ops kvm_syscore_ops = { .shutdown = kvm_shutdown, }; -int kvm_enable_virtualization (void ) +static int kvm_enable_virtualization (void ) {int r;
@@ -5738,9 +5741,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);
@@ -5751,7 +5753,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 ) {
@@ -5767,6 +5768,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.52.0.223.gf5cc29aaa4-goog
Re: [PATCH v2 7/7] KVM: Bury kvm_{en,dis}able_virtualization() in kvm_main.c once more
Quoted 6 lines · Sean Christopherson
Now 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.
Signed-off-by Sean Christopherson <[email protected]>
Looks good,
Re: [PATCH v2 7/7] KVM: Bury kvm_{en,dis}able_virtualization() in kvm_main.c once more
In reply to Sean ChristophersonQuoted 6 lines · Sean Christopherson
Now 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.
Signed-off-by Sean Christopherson <[email protected]>
Quoted 9 lines · Sean Christopherson
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.
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.
Decently tested, and it seems like the core idea is sound, so I dropped the RFC. But the side of things definitely needs testing.
I ran tests on an EMR system, including performing CPU hot-{un}plug, unloading/reloading kvm-intel.ko and launching TDs. No issue found. So,