When the TDX-Module supports quoting, disable TDX if there are not enough quoting sessions or threads for the whole system. This avoids cross-TD contention for quoting resources in the normal setup, where TDs aren't oversubscribed [1]. Quote generation is a long-running operation in the TDX-Module, and the quoting sessions and threads come from a fixed system-wide pool shared by all TDs. Inside the module, a session stores a Quote request's state, and a thread runs the request on a logical CPU. Not having enough quoting resources creates a noisy-neighbor problem, where a TD's Quote request could fail or wait indefinitely just because other TDs are also getting their Quotes. Oversubscribing TDs, i.e. more TDs than there are logical CPUs, is less typical, so only require enough quoting resources for the normal setup. Expect one Quote request per TD at a time, and guarantee that: - When TDs aren't oversubscribed, there is always a free session for a Quote request. - A quoting thread can always be scheduled on any available CPU. Each quoting session and thread currently needs roughly 40KB of pre-allocated TDX-Module memory. This adds up to tens of megabytes on a large system with more than 1024 logical CPUs [2]. Since a platform with TDX enabled doesn't support CPU hotplug, use num_present_cpus() for the number of logical CPUs. Signed-off-by: Peter Fang Link: https://lore.kernel.org/all/arRXqpG_4hCrhQLM@intel.com/ # [1] Link: https://lore.kernel.org/all/DS4PPFA84FA90FC58F88D79B0EF19B34596E7832@DS4PPFA84FA90FC.namprd11.prod.outlook.com/ # [2] --- v3: - New patch. --- arch/x86/kvm/vmx/tdx.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 8a593159c9b7c..4ccb41f002dff 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -3435,6 +3435,8 @@ void tdx_hardware_unsetup(void) static int __init __tdx_hardware_setup(void) { const struct tdx_sys_info_td_conf *td_conf; + u16 quoting_sessions, quoting_threads; + u32 quoting_max_size; int i; for (i = 0; i < ARRAY_SIZE(tdx_uret_msrs); i++) { @@ -3500,6 +3502,26 @@ static int __init __tdx_hardware_setup(void) return -EINVAL; } + /* + * KVM only allows one quoting session per TD. Make sure a free session + * is always available as long as there is no oversubscription of TDs, + * i.e. no more TDs than logical CPUs, and that a quoting thread can + * always be scheduled on any logical CPU. + */ + if (tdx_quoting_enabled("ing_max_size, "ing_sessions, + "ing_threads)) { + if (quoting_sessions < num_present_cpus()) { + pr_err("Disable TDX: QUOTING_NUM_SESSIONS (%u) smaller than number of logical CPUs (%u).\n", + quoting_sessions, num_present_cpus()); + return -EINVAL; + } + if (quoting_threads < num_present_cpus()) { + pr_err("Disable TDX: QUOTING_NUM_THREADS (%u) smaller than number of logical CPUs (%u).\n", + quoting_threads, num_present_cpus()); + return -EINVAL; + } + } + if (misc_cg_set_capacity(MISC_CG_RES_TDX, tdx_get_nr_guest_keyids())) return -EINVAL; -- 2.53.0