x86/tdx: Route safe halt execution via tdx_safe_halt()
v4 cover x86/tdx: Route safe halt execution via tdx_safe_halt()
Direct HLT instruction execution causes #VEs for TDX VMs which is routed to hypervisor via TDCALL. safe_halt() routines execute HLT in STI-shadow so IRQs need to remain disabled until the TDCALL to ensure that pending IRQs are correctly treated as wake events. So "sti;hlt" sequence needs to be replaced with "TDCALL; raw_local_irq_enable()" for TDX VMs.
Changes introduced by the series include:
- Move *halt() variants outside CONFIG_PARAVIRT_XXL and under CONFIG_PARAVIRT [1].
- Route "sti; hlt" sequences via tdx_safe_halt() for reliability.
- Route "hlt" sequences via tdx_halt() to avoid unnecessary #VEs.
- Add explicit dependency on CONFIG_PARAVIRT for TDX VMs.
- Warn and fail emulation if HLT #VE emulation executes with interrupts enabled.
- Clean up TDX specific idle routine override.
Changes since v3:
1) Addressed comments from Sean.
2) Included [1] in the series to fix the scenarios where CONFIG_PARAVIRT_XXL could be disabled.
v3: https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/lkml/20210517235008.257241-1-sathyanarayanan.kuppuswamy@linux.intel.com/
Kirill A. Shutemov (1):
x86/paravirt: Move halt paravirt calls under CONFIG_PARAVIRT
Vishal Annapurve (3):
x86/tdx: Route safe halt execution via tdx_safe_halt() x86/tdx: Emit warning if IRQs are enabled during HLT #VE handling x86/tdx: Remove TDX specific idle routine
arch/x86/Kconfig | 1 + arch/x86/coco/tdx/tdx.c | 26 ++++++++++++++++- arch/x86/include/asm/irqflags.h | 40 +++++++++++++++------------ arch/x86/include/asm/paravirt.h | 20 +++++++------- arch/x86/include/asm/paravirt_types.h | 3 +- arch/x86/include/asm/tdx.h | 2 -- arch/x86/kernel/paravirt.c | 14 ++++++---- arch/x86/kernel/process.c | 3 -- 8 files changed, 67 insertions(+), 42 deletions(-)
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
PATCH v4 1/4 x86/paravirt: Move halt paravirt calls under CONFIG_PARAVIRT
From: "Kirill A. Shutemov" <[email protected]>
CONFIG_PARAVIRT_XXL is mainly defined/used by XEN PV guests. For other VM guest types, features supported under CONFIG_PARAVIRT are self sufficient. CONFIG_PARAVIRT mainly provides support for TLB flush operations and time related operations.
For TDX guest as well, paravirt calls under CONFIG_PARVIRT meets most of its requirement except the need of HLT and SAFE_HLT paravirt calls, which is currently defined under CONFIG_PARAVIRT_XXL.
Since enabling CONFIG_PARAVIRT_XXL is too bloated for TDX guest like platforms, move HLT and SAFE_HLT paravirt calls under CONFIG_PARAVIRT.
Moving HLT and SAFE_HLT paravirt calls are not fatal and should not break any functionality for current users of CONFIG_PARAVIRT.
--- arch/x86/include/asm/irqflags.h | 40 +++++++++++++++------------ arch/x86/include/asm/paravirt.h | 20 +++++++------- arch/x86/include/asm/paravirt_types.h | 3 +- arch/x86/kernel/paravirt.c | 14 ++++++---- 4 files changed, 41 insertions(+), 36 deletions(-)
arch/x86/include/asm/irqflags.h +22 -18
@@ -76,6 +76,28 @@ static __always_inline void native_local_irq_restore(unsigned long flags)#endif +#ifndef CONFIG_PARAVIRT +#ifndef __ASSEMBLY__ +/* +* Used in the idle loop; sti takes one instruction cycle +* to complete: +*/ +static __always_inline void arch_safe_halt(void) +{ +native_safe_halt (); +} + +/* +* Used when interrupts are already enabled or to +* shutdown the processor: +*/ +static __always_inline void halt(void) +{ +native_halt (); +} +#endif /* __ASSEMBLY__ */ +#endif /* CONFIG_PARAVIRT */ +#ifdef CONFIG_PARAVIRT_XXL #include <asm/paravirt.h> #else
@@ -97,24 +119,6 @@ static __always_inline void arch_local_irq_enable(void)native_irq_enable (); } -/* -* Used in the idle loop; sti takes one instruction cycle -* to complete: -*/ -static __always_inline void arch_safe_halt(void) -{ -native_safe_halt (); -} - -/* -* Used when interrupts are already enabled or to -* shutdown the processor: -*/ -static __always_inline void halt(void) -{ -native_halt (); -} -/* * For spinlocks, etc: */
arch/x86/include/asm/paravirt.h +10 -10
@@ -107,6 +107,16 @@ static inline void notify_page_enc_status_changed(unsigned long pfn,PVOP_VCALL3 (mmu.notify_page_enc_status_changed, pfn, npages, enc); } +static __always_inline void arch_safe_halt(void) +{ +PVOP_VCALL0 (irq.safe_halt); +} + +static inline void halt (void ) +{ +PVOP_VCALL0 (irq.halt); +} +#ifdef CONFIG_PARAVIRT_XXL static inline void load_sp0 (unsigned long sp0) {
@@ -170,16 +180,6 @@ static inline void __write_cr4(unsigned long x)PVOP_VCALL1 (cpu.write_cr4, x); } -static __always_inline void arch_safe_halt(void) -{ -PVOP_VCALL0 (irq.safe_halt); -} - -static inline void halt (void ) -{ -PVOP_VCALL0 (irq.halt); -} -static inline u64 paravirt_read_msr (unsigned msr) {return PVOP_CALL1 (u64, cpu.read_msr, msr);
arch/x86/include/asm/paravirt_types.h +1 -2
@@ -120,10 +120,9 @@ struct pv_irq_ops {struct paravirt_callee_save save_fl;struct paravirt_callee_save irq_disable;struct paravirt_callee_save irq_enable; - +#endif void (*safe_halt)(void );void (*halt)(void ); -#endif } __no_randomize_layout;struct pv_mmu_ops {
arch/x86/kernel/paravirt.c +8 -6
@@ -110,6 +110,11 @@ int paravirt_disable_iospace(void)return request_resource (&ioport_resource, &reserve_ioports); } +static noinstr void pv_native_safe_halt(void) +{ +native_safe_halt (); +} +#ifdef CONFIG_PARAVIRT_XXL static noinstr void pv_native_write_cr2(unsigned long val) {
@@ -125,11 +130,6 @@ static noinstr void pv_native_set_debugreg(int regno, unsigned long val) {native_set_debugreg (regno, val); } - -static noinstr void pv_native_safe_halt(void) -{ -native_safe_halt (); -}#endif struct pv_info pv_info = {
@@ -186,9 +186,11 @@ struct paravirt_patch_template pv_ops = { .irq.save_fl = __PV_IS_CALLEE_SAVE(pv_native_save_fl), .irq.irq_disable = __PV_IS_CALLEE_SAVE(pv_native_irq_disable), .irq.irq_enable = __PV_IS_CALLEE_SAVE(pv_native_irq_enable), +#endif /* CONFIG_PARAVIRT_XXL */ + +/* Irq HLT ops. */ .irq.safe_halt = pv_native_safe_halt, .irq.halt = native_halt, -#endif /* CONFIG_PARAVIRT_XXL */ /* Mmu ops. */ .mmu.flush_tlb_user = native_flush_tlb_local,
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
PATCH v4 2/4 x86/tdx: Route safe halt execution via tdx_safe_halt()
In reply to Vishal AnnapurveDirect HLT instruction execution causes #VEs for TDX VMs which is routed to hypervisor via TDCALL. safe_halt() routines execute HLT in STI-shadow so IRQs need to remain disabled until the TDCALL to ensure that pending IRQs are correctly treated as wake events. So "sti;hlt" sequence needs to be replaced with "TDCALL; raw_local_irq_enable()" for TDX VMs.
Commit bfe6ed0c6727 ("x86/tdx: Add HLT support for TDX guests") prevented the idle routines from using "sti;hlt". But it missed the paravirt routine which can be reached like this as an example:
acpi_safe_halt() =>
raw_safe_halt() =>
arch_safe_halt() =>
irq.safe_halt() =>
pv_native_safe_halt()Modify tdx_safe_halt() to implement the sequence "TDCALL; raw_local_irq_enable()" and invoke tdx_halt() from idle routine which just executes TDCALL without changing state of interrupts. Introduce dependency on CONFIG_PARAVIRT and override paravirt halt()/safe_halt() routines for TDX VMs.
--- arch/x86/Kconfig | 1 + arch/x86/coco/tdx/tdx.c | 18 +++++++++++++++++- arch/x86/include/asm/tdx.h | 2 +- arch/x86/kernel/process.c | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-)
arch/x86/Kconfig +1 -0
@@ -902,6 +902,7 @@ config INTEL_TDX_GUEST depends on X86_64 && CPU_SUP_INTEL depends on X86_X2APIC depends on EFI_STUB + depends on PARAVIRT select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select X86_MCE
arch/x86/coco/tdx/tdx.c +17 -1
@@ -14,6 +14,7 @@#include <asm/ia32.h> #include <asm/insn.h> #include <asm/insn-eval.h> +#include <asm/paravirt_types.h> #include <asm/pgtable.h> #include <asm/set_memory.h> #include <asm/traps.h>
@@ -398,7 +399,7 @@ static int handle_halt(struct ve_info *ve)return ve_instr_len (ve); } -void __cpuidle tdx_safe_halt(void) +void __cpuidle tdx_halt(void) {const bool irq_disabled = false;
@@ -409,6 +410,12 @@ void __cpuidle tdx_safe_halt(void)WARN_ONCE (1 ,"HLT instruction emulation failed\n" ); } +static void __cpuidle tdx_safe_halt(void) +{ +tdx_halt (); +raw_local_irq_enable (); +} +static int read_msr (struct pt_regs *regs,struct ve_info *ve) {struct tdx_module_args args = {
@@ -1109,6 +1116,15 @@ void __init tdx_early_init(void) x86_platform.guest.enc_kexec_begin = tdx_kexec_begin; x86_platform.guest.enc_kexec_finish = tdx_kexec_finish; +/* +* "sti;hlt" execution in TDX guests will induce a #VE in the STI-shadow +* which will enable interrupts before HLT TDCALL inocation possibly +* resulting in missed wakeup events. Modify all possible HLT +* execution paths to use TDCALL for performance/reliability reasons. +*/ + pv_ops.irq.safe_halt = tdx_safe_halt; + pv_ops.irq.halt = tdx_halt; +/* * TDX intercepts the RDMSR to read the X2APIC ID in the parallel * bringup low level code. That raises #VE which cannot be handled
arch/x86/include/asm/tdx.h +1 -1
@@ -58,7 +58,7 @@ void tdx_get_ve_info(struct ve_info *ve);bool tdx_handle_virt_exception (struct pt_regs *regs,struct ve_info *ve); -void tdx_safe_halt (void ); +void tdx_halt (void );bool tdx_early_handle_ve (struct pt_regs *regs);
arch/x86/kernel/process.c +1 -1
@@ -934,7 +934,7 @@ void __init select_idle_routine(void)static_call_update (x86_idle, mwait_idle); }else if (cpu_feature_enabled (X86_FEATURE_TDX_GUEST )) {pr_info ("using TDX aware idle routine\n" ); -static_call_update (x86_idle, tdx_safe_halt); +static_call_update (x86_idle, tdx_halt); }else {static_call_update (x86_idle, default_idle); }
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
Quoted 5 lines · Vishal Annapurve
Direct HLT instruction execution causes #VEs for TDX VMs which is routed to hypervisor via TDCALL.
safe_halt()routines execute HLT in STI-shadow so IRQs need to remain disabled until the TDCALL to ensure that pending IRQs are correctly treated as wake events. So "sti;hlt" sequence needs to be replaced with "TDCALL;raw_local_irq_enable()" for TDX VMs.
The last sentence is somewhat confusing.
Maybe drop it and add explanation that #VE handler doesn't have info about STI shadow, enables interrupts before TDCALL which can lead to missed wakeup events.
arch/x86/coco/tdx/tdx.c void __cpuidle tdx_safe_halt(void) · line 416
Quoted 8 lines
arch/x86/coco/tdx/tdx.c +4
-0@@ -409,6 +410,12 @@ void __cpuidle tdx_safe_halt(void)WARN_ONCE (1 ,"HLT instruction emulation failed\n" ); } +static void __cpuidle tdx_safe_halt(void) +{ +tdx_halt (); +raw_local_irq_enable ();
What is justification for raw_? Why local_irq_enable() is not enough?
To very least, it has to be explained.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Quoted 8 lines · Kirill A. Shutemov
Quoted 5 lines · Vishal Annapurve
Direct HLT instruction execution causes #VEs for TDX VMs which is routed to hypervisor via TDCALL.
safe_halt()routines execute HLT in STI-shadow so IRQs need to remain disabled until the TDCALL to ensure that pending IRQs are correctly treated as wake events. So "sti;hlt" sequence needs to be replaced with "TDCALL;raw_local_irq_enable()" for TDX VMs.
Quoted 5 lines
The last sentence is somewhat confusing.
Maybe drop it and add explanation that #VE handler doesn't have info about STI shadow, enables interrupts before TDCALL which can lead to missed wakeup events.
Ack, will fix it in the next version.
Quoted 5 lines
Quoted 5 lines
arch/x86/coco/tdx/tdx.c +0
-0@@ -409,6 +410,12 @@ void __cpuidle tdx_safe_halt(void)WARN_ONCE (1 ,"HLT instruction emulation failed\n" ); }
Quoted 8 lines
arch/x86/coco/tdx/tdx.c void __cpuidle tdx_safe_halt(void) · line 416
Quoted 4 lines
arch/x86/coco/tdx/tdx.c +4
-0+static void __cpuidle tdx_safe_halt(void) +{ +tdx_halt (); +raw_local_irq_enable ();What is justification for
raw_? Whylocal_irq_enable()is not enough?To very least, it has to be explained.
Let me replace it with a more suitable arch specific <>_irq_enable() function in the next version. Intention here is to just enable interrupts.
Quoted 3 lines
--
Kiryl Shutsemau / Kirill A. Shutemov
PATCH v4 3/4 x86/tdx: Emit warning if IRQs are enabled during HLT #VE handling
In reply to Vishal AnnapurveDirect HLT instruction execution causes #VEs for TDX VMs which is routed to hypervisor via TDCALL. safe_halt() routines execute HLT in STI-shadow so IRQs need to remain disabled until the TDCALL to ensure that pending IRQs are correctly treated as wake events.
Emit warning and fail emulation if IRQs are enabled during HLT #VE handling to avoid running into scenarios where IRQ wake events are lost resulting in indefinite HLT execution times.
--- arch/x86/coco/tdx/tdx.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/coco/tdx/tdx.c +8 -0
@@ -393,6 +393,14 @@ static int handle_halt(struct ve_info *ve) {const bool irq_disabled =irqs_disabled (); +/* +* HLT with IRQs enabled is unsafe, as an IRQ that is intended to be a +* wake event may be consumed before requesting HLT emulation, leaving +* the vCPU blocking indefinitely. +*/ +if (WARN_ONCE (!irq_disabled,"HLT emulation with IRQs enabled" )) +return -EIO ; +if (__halt (irq_disabled))return -EIO ;
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
PATCH v4 4/4 x86/tdx: Remove TDX specific idle routine
In reply to Vishal AnnapurveWith explicit dependency on CONFIG_PARAVIRT and TDX specific halt()/safe_halt() routines in place, default_idle() is safe to execute for TDX VMs. Remove TDX specific idle routine override which is now redundant.
--- arch/x86/coco/tdx/tdx.c | 2 +- arch/x86/include/asm/tdx.h | 2 -- arch/x86/kernel/process.c | 3 --- 3 files changed, 1 insertion(+), 6 deletions(-)
arch/x86/coco/tdx/tdx.c +1 -1
@@ -407,7 +407,7 @@ static int handle_halt(struct ve_info *ve)return ve_instr_len (ve); } -void __cpuidle tdx_halt(void) +static void __cpuidle tdx_halt(void) {const bool irq_disabled = false;
arch/x86/include/asm/tdx.h +0 -2
@@ -58,8 +58,6 @@ void tdx_get_ve_info(struct ve_info *ve);bool tdx_handle_virt_exception (struct pt_regs *regs,struct ve_info *ve); -void tdx_halt (void ); -bool tdx_early_handle_ve (struct pt_regs *regs);int tdx_mcall_get_report0 (u8 *reportdata,u8 *tdreport);
arch/x86/kernel/process.c +0 -3
@@ -932,9 +932,6 @@ void __init select_idle_routine(void)if (prefer_mwait_c1_over_halt ()) {pr_info ("using mwait in idle threads\n" );static_call_update (x86_idle, mwait_idle); - }else if (cpu_feature_enabled (X86_FEATURE_TDX_GUEST )) { -pr_info ("using TDX aware idle routine\n" ); -static_call_update (x86_idle, tdx_halt); }else {static_call_update (x86_idle, default_idle); }
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
Re: [PATCH V4 4/4] x86/tdx: Remove TDX specific idle routine
Quoted 4 lines · Vishal Annapurve
With explicit dependency on
CONFIG_PARAVIRTand TDX specific halt()/safe_halt() routines in place,default_idle()is safe to execute for TDX VMs. Remove TDX specific idle routine override which is now redundant.
I am not convinced that it is good idea.
It adds two needless flipping of IF in the hot path: first enabling interrupts in tdx_safe_halt() and disabling it back in default_idle().
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCH V4 4/4] x86/tdx: Remove TDX specific idle routine
Quoted 7 lines · Kirill A. Shutemov
Quoted 4 lines · Vishal Annapurve
With explicit dependency on
CONFIG_PARAVIRTand TDX specific halt()/safe_halt() routines in place,default_idle()is safe to execute for TDX VMs. Remove TDX specific idle routine override which is now redundant.
Quoted 5 lines
I am not convinced that it is good idea.
It adds two needless flipping of IF in the hot path: first enabling interrupts in
tdx_safe_halt()and disabling it back indefault_idle().
IIUC, the aspect of flipping IF in the hot path twice is also applicable to default_idle() that gets executed for non-confidential VMs. I agree that it's redundant for TDX VMs and if there is strong consensus here, I can drop this cleanup in the next version.
Quoted 2 lines
--
Kiryl Shutsemau / Kirill A. Shutemov