Skip to content

Commit 1f287a4

Browse files
committed
KVM: TDX: Move TDX hardware setup from main.c to tdx.c
Move TDX hardware setup to tdx.c, as the code is obviously TDX specific, co-locating the setup with tdx_bringup() makes it easier to see and document the success_disable_tdx "error" path, and configuring the TDX specific hooks in tdx.c reduces the number of globally visible TDX symbols. Reviewed-by: Kai Huang <kai.huang@intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Link: https://lore.kernel.org/r/20250523001138.3182794-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent ffced89 commit 1f287a4

File tree

4 files changed

+35
-57
lines changed

4 files changed

+35
-57
lines changed

arch/x86/kvm/vmx/main.c

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,8 @@ static __init int vt_hardware_setup(void)
2929
if (ret)
3030
return ret;
3131

32-
/*
33-
* Update vt_x86_ops::vm_size here so it is ready before
34-
* kvm_ops_update() is called in kvm_x86_vendor_init().
35-
*
36-
* Note, the actual bringing up of TDX must be done after
37-
* kvm_ops_update() because enabling TDX requires enabling
38-
* hardware virtualization first, i.e., all online CPUs must
39-
* be in post-VMXON state. This means the @vm_size here
40-
* may be updated to TDX's size but TDX may fail to enable
41-
* at later time.
42-
*
43-
* The VMX/VT code could update kvm_x86_ops::vm_size again
44-
* after bringing up TDX, but this would require exporting
45-
* either kvm_x86_ops or kvm_ops_update() from the base KVM
46-
* module, which looks overkill. Anyway, the worst case here
47-
* is KVM may allocate couple of more bytes than needed for
48-
* each VM.
49-
*/
50-
if (enable_tdx) {
51-
vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size,
52-
sizeof(struct kvm_tdx));
53-
/*
54-
* Note, TDX may fail to initialize in a later time in
55-
* vt_init(), in which case it is not necessary to setup
56-
* those callbacks. But making them valid here even
57-
* when TDX fails to init later is fine because those
58-
* callbacks won't be called if the VM isn't TDX guest.
59-
*/
60-
vt_x86_ops.link_external_spt = tdx_sept_link_private_spt;
61-
vt_x86_ops.set_external_spte = tdx_sept_set_private_spte;
62-
vt_x86_ops.free_external_spt = tdx_sept_free_private_spt;
63-
vt_x86_ops.remove_external_spte = tdx_sept_remove_private_spte;
64-
vt_x86_ops.protected_apic_has_interrupt = tdx_protected_apic_has_interrupt;
65-
}
32+
if (enable_tdx)
33+
tdx_hardware_setup();
6634

6735
return 0;
6836
}

arch/x86/kvm/vmx/tdx.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ bool tdx_interrupt_allowed(struct kvm_vcpu *vcpu)
745745
!to_tdx(vcpu)->vp_enter_args.r12;
746746
}
747747

748-
bool tdx_protected_apic_has_interrupt(struct kvm_vcpu *vcpu)
748+
static bool tdx_protected_apic_has_interrupt(struct kvm_vcpu *vcpu)
749749
{
750750
u64 vcpu_state_details;
751751

@@ -1642,8 +1642,8 @@ static int tdx_mem_page_record_premap_cnt(struct kvm *kvm, gfn_t gfn,
16421642
return 0;
16431643
}
16441644

1645-
int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
1646-
enum pg_level level, kvm_pfn_t pfn)
1645+
static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
1646+
enum pg_level level, kvm_pfn_t pfn)
16471647
{
16481648
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
16491649
struct page *page = pfn_to_page(pfn);
@@ -1723,8 +1723,8 @@ static int tdx_sept_drop_private_spte(struct kvm *kvm, gfn_t gfn,
17231723
return 0;
17241724
}
17251725

1726-
int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn,
1727-
enum pg_level level, void *private_spt)
1726+
static int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn,
1727+
enum pg_level level, void *private_spt)
17281728
{
17291729
int tdx_level = pg_level_to_tdx_sept_level(level);
17301730
gpa_t gpa = gfn_to_gpa(gfn);
@@ -1859,8 +1859,8 @@ static void tdx_track(struct kvm *kvm)
18591859
kvm_make_all_cpus_request(kvm, KVM_REQ_OUTSIDE_GUEST_MODE);
18601860
}
18611861

1862-
int tdx_sept_free_private_spt(struct kvm *kvm, gfn_t gfn,
1863-
enum pg_level level, void *private_spt)
1862+
static int tdx_sept_free_private_spt(struct kvm *kvm, gfn_t gfn,
1863+
enum pg_level level, void *private_spt)
18641864
{
18651865
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
18661866

@@ -1882,8 +1882,8 @@ int tdx_sept_free_private_spt(struct kvm *kvm, gfn_t gfn,
18821882
return tdx_reclaim_page(virt_to_page(private_spt));
18831883
}
18841884

1885-
int tdx_sept_remove_private_spte(struct kvm *kvm, gfn_t gfn,
1886-
enum pg_level level, kvm_pfn_t pfn)
1885+
static int tdx_sept_remove_private_spte(struct kvm *kvm, gfn_t gfn,
1886+
enum pg_level level, kvm_pfn_t pfn)
18871887
{
18881888
struct page *page = pfn_to_page(pfn);
18891889
int ret;
@@ -3606,10 +3606,14 @@ int __init tdx_bringup(void)
36063606
r = __tdx_bringup();
36073607
if (r) {
36083608
/*
3609-
* Disable TDX only but don't fail to load module if
3610-
* the TDX module could not be loaded. No need to print
3611-
* message saying "module is not loaded" because it was
3612-
* printed when the first SEAMCALL failed.
3609+
* Disable TDX only but don't fail to load module if the TDX
3610+
* module could not be loaded. No need to print message saying
3611+
* "module is not loaded" because it was printed when the first
3612+
* SEAMCALL failed. Don't bother unwinding the S-EPT hooks or
3613+
* vm_size, as kvm_x86_ops have already been finalized (and are
3614+
* intentionally not exported). The S-EPT code is unreachable,
3615+
* and allocating a few more bytes per VM in a should-be-rare
3616+
* failure scenario is a non-issue.
36133617
*/
36143618
if (r == -ENODEV)
36153619
goto success_disable_tdx;
@@ -3623,3 +3627,18 @@ int __init tdx_bringup(void)
36233627
enable_tdx = 0;
36243628
return 0;
36253629
}
3630+
3631+
void __init tdx_hardware_setup(void)
3632+
{
3633+
/*
3634+
* Note, if the TDX module can't be loaded, KVM TDX support will be
3635+
* disabled but KVM will continue loading (see tdx_bringup()).
3636+
*/
3637+
vt_x86_ops.vm_size = max_t(unsigned int, vt_x86_ops.vm_size, sizeof(struct kvm_tdx));
3638+
3639+
vt_x86_ops.link_external_spt = tdx_sept_link_private_spt;
3640+
vt_x86_ops.set_external_spte = tdx_sept_set_private_spte;
3641+
vt_x86_ops.free_external_spt = tdx_sept_free_private_spt;
3642+
vt_x86_ops.remove_external_spte = tdx_sept_remove_private_spte;
3643+
vt_x86_ops.protected_apic_has_interrupt = tdx_protected_apic_has_interrupt;
3644+
}

arch/x86/kvm/vmx/tdx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifdef CONFIG_KVM_INTEL_TDX
99
#include "common.h"
1010

11+
void tdx_hardware_setup(void);
1112
int tdx_bringup(void);
1213
void tdx_cleanup(void);
1314

arch/x86/kvm/vmx/x86_ops.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu);
136136
fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit);
137137
void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
138138
void tdx_vcpu_put(struct kvm_vcpu *vcpu);
139-
bool tdx_protected_apic_has_interrupt(struct kvm_vcpu *vcpu);
140139
int tdx_handle_exit(struct kvm_vcpu *vcpu,
141140
enum exit_fastpath_completion fastpath);
142141

@@ -151,15 +150,6 @@ int tdx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
151150

152151
int tdx_vcpu_ioctl(struct kvm_vcpu *vcpu, void __user *argp);
153152

154-
int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn,
155-
enum pg_level level, void *private_spt);
156-
int tdx_sept_free_private_spt(struct kvm *kvm, gfn_t gfn,
157-
enum pg_level level, void *private_spt);
158-
int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn,
159-
enum pg_level level, kvm_pfn_t pfn);
160-
int tdx_sept_remove_private_spte(struct kvm *kvm, gfn_t gfn,
161-
enum pg_level level, kvm_pfn_t pfn);
162-
163153
void tdx_flush_tlb_current(struct kvm_vcpu *vcpu);
164154
void tdx_flush_tlb_all(struct kvm_vcpu *vcpu);
165155
void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level);

0 commit comments

Comments
 (0)