Re: [PATCH v13 003/113] KVM: TDX: Initialize the TDX module when loading the KVM intel kernel module
From: Huang, Kai
Date: Mon Mar 13 2023 - 22:38:21 EST
On Sun, 2023-03-12 at 10:55 -0700, isaku.yamahata@xxxxxxxxx wrote:
> +int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops)
> +{
> + int r;
> +
> + if (!enable_ept) {
> + pr_warn("Cannot enable TDX with EPT disabled\n");
> + return -EINVAL;
> + }
> +
> + /* tdx_enable() in tdx_module_setup() requires cpus lock. */
> + cpus_read_lock();
> + /* TDX requires VMX. */
> + r = vmxon_all();
Why not using hardware_enable_all()?
> + if (!r) {
> + int cpu;
> +
> + /*
> + * Because tdx_cpu_enabel() acquire spin locks, on_each_cpu()
> + * can't be used.
> + */
> + for_each_online_cpu(cpu) {
> + if (smp_call_on_cpu(cpu, tdx_cpu_enable_cpu, NULL, false))
> + r = -EIO;
> + }
> + if (!r)
> + r = tdx_module_setup();
> + }
> + vmxoff_all();
> + cpus_read_unlock();
> +
> + return r;
> +}
I think you should use hardware_enable_all(), and just do something similar to
below in vmx_hardware_enable():
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 7eec0226d56a..b7b3f99c0095 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -2504,6 +2504,15 @@ static int vmx_hardware_enable(void)
return r;
}
+ if (enable_tdx) {
+ r = tdx_cpu_enable();
+ if (r) {
+ cpu_vmxoff();
+ intel_pt_handle_vmx(0);
+ return r;
+ }
+ }
+
if (enable_ept)
ept_sync_global();
It handles two cases automatically:
1) When user wants to use TDX while loading KVM module, if above fails, then
hardware_enable_all() returns error, and you just give up initializing TDX
module and mark enable_tdx as false.
2) When a new cpu becomes online, and when TDX is being used by KVM, then if
above fails, it automatically rejects the CPU which isn't TDX-runnable although
it is VMX-runnable.