Skip to contentSkip to conversation

x86/tdx: Mark TSC reliable

PATCH Superseded Kirill A. Shutemov 4 participants 5 messages Aug 22, 2023 – Aug 23, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/tdx: Mark TSC reliable

In x86 virtualization environments, including TDX, RDTSC instruction is handled without causing a VM exit, resulting in minimal overhead and jitters. On the other hand, other clock sources (such as HPET, ACPI timer, APIC, etc.) necessitate VM exits to implement, resulting in more fluctuating measurements compared to TSC. Thus, those clock sources are not effective for calibrating TSC.

In TD guests, TSC is virtualized by the TDX module, which ensures:

- Virtual TSC values are consistent among all the TD’s VCPUs;
- Monotonously incrementing for any single VCPU;
- The frequency is determined by TD configuration. The host TSC is invariant on platforms where TDX is available.

Reliable TSC is architectural guarantee for the TDX platform and it must work for any sane TDX implementation.

Use TSC as the only reliable clock source in TD guests, bypassing unstable calibration.

---
v2:
- Slightly updated commit message;

---
 arch/x86/coco/tdx/tdx.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/coco/tdx/tdx.c +3 -0
index 1d6b863c42b0..1583ec64d92e 100644
@@ -769,6 +769,9 @@ void __init tdx_early_init(void)
 
 	setup_force_cpu_cap(X86_FEATURE_TDX_GUEST);
 
+	/* TSC is the only reliable clock in TDX guest */
+	setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
+
 	cc_vendor = CC_VENDOR_INTEL;
 	tdx_parse_tdinfo(&cc_mask);
 	cc_set_mask(cc_mask);
Signature · 1 line
2.41.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 17 lines · Kirill A. Shutemov

In x86 virtualization environments, including TDX, RDTSC instruction is handled without causing a VM exit, resulting in minimal overhead and jitters. On the other hand, other clock sources (such as HPET, ACPI timer, APIC, etc.) necessitate VM exits to implement, resulting in more fluctuating measurements compared to TSC. Thus, those clock sources are not effective for calibrating TSC.

In TD guests, TSC is virtualized by the TDX module, which ensures:

- Virtual TSC values are consistent among all the TD’s VCPUs;
- Monotonously incrementing for any single VCPU;
- The frequency is determined by TD configuration. The host TSC is invariant on platforms where TDX is available.

Reliable TSC is architectural guarantee for the TDX platform and it must work for any sane TDX implementation.

Quoted 8 lines

Use TSC as the only reliable clock source in TD guests, bypassing unstable calibration.

---
v2:
- Slightly updated commit message;
---

Looks good to me.

Quoted 17 lines
 arch/x86/coco/tdx/tdx.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/coco/tdx/tdx.c +3 -0
index 1d6b863c42b0..1583ec64d92e 100644
@@ -769,6 +769,9 @@ void __init tdx_early_init(void)
 
 	setup_force_cpu_cap(X86_FEATURE_TDX_GUEST);
 
+	/* TSC is the only reliable clock in TDX guest */
+	setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
+
 	cc_vendor = CC_VENDOR_INTEL;
 	tdx_parse_tdinfo(&cc_mask);
 	cc_set_mask(cc_mask);
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Acked-by on v2 1/1
#2 lore raw
In reply to Kirill A. Shutemov
Quoted 11 lines · Kirill A. Shutemov

In x86 virtualization environments, including TDX, RDTSC instruction is handled without causing a VM exit, resulting in minimal overhead and jitters. On the other hand, other clock sources (such as HPET, ACPI timer, APIC, etc.) necessitate VM exits to implement, resulting in more fluctuating measurements compared to TSC. Thus, those clock sources are not effective for calibrating TSC.

In TD guests, TSC is virtualized by the TDX module, which ensures:

- Virtual TSC values are consistent among all the TD’s VCPUs;
- Monotonously incrementing for any single VCPU;

Nit:

- Virtual TSC is monotonously incrementing ...

Otherwise here "incrementing" looks like a noun, which means you need to use "monotonous" rather than "monotonously".

Quoted 10 lines

- The frequency is determined by TD configuration. The host TSC is invariant on platforms where TDX is available.

Reliable TSC is architectural guarantee for the TDX platform and it must work for any sane TDX implementation.

Use TSC as the only reliable clock source in TD guests, bypassing unstable calibration.

#3 lore raw
Quoted 13 lines · Kai
Quoted 11 lines · Kirill A. Shutemov

In x86 virtualization environments, including TDX, RDTSC instruction is handled without causing a VM exit, resulting in minimal overhead and jitters. On the other hand, other clock sources (such as HPET, ACPI timer, APIC, etc.) necessitate VM exits to implement, resulting in more fluctuating measurements compared to TSC. Thus, those clock sources are not effective for calibrating TSC.

In TD guests, TSC is virtualized by the TDX module, which ensures:

- Virtual TSC values are consistent among all the TD’s VCPUs;
- Monotonously incrementing for any single VCPU;

Quoted 6 lines

Nit:

- Virtual TSC is monotonously incrementing ...

Otherwise here "incrementing" looks like a noun, which means you need to use "monotonous" rather than "monotonously".

Okay, will update.

Quoted 6 lines
Quoted 6 lines

- The frequency is determined by TD configuration. The host TSC is invariant on platforms where TDX is available.

Reliable TSC is architectural guarantee for the TDX platform and it must work for any sane TDX implementation.

Quoted 6 lines
Quoted 4 lines

Use TSC as the only reliable clock source in TD guests, bypassing unstable calibration.

Thanks.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
Reviewed-by on v2 1/1
#4 lore raw
In reply to Kirill A. Shutemov
Quoted 46 lines · Kirill A. Shutemov

In x86 virtualization environments, including TDX, RDTSC instruction is handled without causing a VM exit, resulting in minimal overhead and jitters. On the other hand, other clock sources (such as HPET, ACPI timer, APIC, etc.) necessitate VM exits to implement, resulting in more fluctuating measurements compared to TSC. Thus, those clock sources are not effective for calibrating TSC.

In TD guests, TSC is virtualized by the TDX module, which ensures:

- Virtual TSC values are consistent among all the TD’s VCPUs;
- Monotonously incrementing for any single VCPU;
- The frequency is determined by TD configuration. The host TSC is invariant on platforms where TDX is available.

Reliable TSC is architectural guarantee for the TDX platform and it must work for any sane TDX implementation.

Use TSC as the only reliable clock source in TD guests, bypassing unstable calibration.

---
v2:
- Slightly updated commit message;

---
 arch/x86/coco/tdx/tdx.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/coco/tdx/tdx.c +3 -0
index 1d6b863c42b0..1583ec64d92e 100644
@@ -769,6 +769,9 @@ void __init tdx_early_init(void)
 
 	setup_force_cpu_cap(X86_FEATURE_TDX_GUEST);
 
+	/* TSC is the only reliable clock in TDX guest */
+	setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
+
 	cc_vendor = CC_VENDOR_INTEL;
 	tdx_parse_tdinfo(&cc_mask);
 	cc_set_mask(cc_mask);
Signature · 1 line
2.41.0
Signature · 1 line
Isaku Yamahata <[email protected]>