Handle timeouts for x86 CVMs
Subsystems (6)
cover Handle timeouts for x86 CVMs
This tiny series handles timeouts for confidential VMs. Last commit desribes the issue and the solution.
Thanks,
-Mukesh
Mukesh R (3):
Drivers: hv: Rename HV_STATUS_TIME_OUT
Drivers: hv: Create REP_COUNT define
x86/hyperv: Handle hypercall timeouts on hw confidential VMs
arch/x86/hyperv/ivm.c | 52 +++++++++++++++++++++++++++------- arch/x86/kvm/hyperv.c | 2 +- drivers/hv/hv_common.c | 2 +- include/asm-generic/mshyperv.h | 14 ++++++++- include/hyperv/hvgdk_mini.h | 10 +++++-- 5 files changed, 63 insertions(+), 17 deletions(-)
Signature · 1 line
2.51.2.vfs.0.1
PATCH 1/3 Drivers: hv: Rename HV_STATUS_TIME_OUT
Rename HV_STATUS_TIME_OUT to HV_STATUS_TIMEOUT to match the hypervisor public header files. There is really no reason for it to be different.
--- arch/x86/hyperv/ivm.c | 2 +- drivers/hv/hv_common.c | 2 +- include/hyperv/hvgdk_mini.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/hyperv/ivm.c +1 -1
@@ -365,7 +365,7 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)do { ret =hv_do_hypercall (HVCALL_START_VP , start_vp_input,NULL ); - }while (hv_result (ret) ==HV_STATUS_TIME_OUT && retry--); + }while (hv_result (ret) ==HV_STATUS_TIMEOUT && retry--);local_irq_restore (flags);
drivers/hv/hv_common.c +1 -1
@@ -839,7 +839,7 @@ static const struct hv_status_info hv_status_infos[] = {_STATUS_INFO (HV_STATUS_INVALID_REGISTER_VALUE , -EINVAL ),_STATUS_INFO (HV_STATUS_DEVICE_ALREADY_IN_DOMAIN , -EEXIST ),_STATUS_INFO (HV_STATUS_OPERATION_FAILED , -EIO ), -_STATUS_INFO (HV_STATUS_TIME_OUT , -EIO ), +_STATUS_INFO (HV_STATUS_TIMEOUT , -EIO ),_STATUS_INFO (HV_STATUS_CALL_PENDING , -EIO ),_STATUS_INFO (HV_STATUS_VTL_ALREADY_ENABLED , -EIO ), #undef _STATUS_INFO
include/hyperv/hvgdk_mini.h +1 -1
@@ -41,7 +41,7 @@ struct hv_u128 {#define HV_STATUS_OPERATION_FAILED 0x71#define HV_STATUS_INSUFFICIENT_ROOT_MEMORY 0x73#define HV_STATUS_INSUFFICIENT_CONTIGUOUS_MEMORY 0x75 -#define HV_STATUS_TIME_OUT 0x78 +#define HV_STATUS_TIMEOUT 0x78#define HV_STATUS_CALL_PENDING 0x79#define HV_STATUS_INSUFFICIENT_CONTIGUOUS_ROOT_MEMORY 0x83#define HV_STATUS_VTL_ALREADY_ENABLED 0x86
Signature · 1 line
2.51.2.vfs.0.1
PATCH 2/3 Drivers: hv: Create REP_COUNT define
In reply to Mukesh RWhen a repeat hypercall completes, it returns completions as part of the status in bits 43-32, and HV_HYPERCALL_REP_COMP_MASK is used to extract and examine it. When issuing a hypercall, the actual repeat count is part of the input struct at same bits. However, the same COMP_MASK is used to set it. This maybe confusing for anyone reading the code. Add HV_HYPERCALL_REP_COUNT_MASK and use it for counts instead of REP_COMP.
--- arch/x86/kvm/hyperv.c | 2 +- include/asm-generic/mshyperv.h | 2 +- include/hyperv/hvgdk_mini.h | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-)
arch/x86/kvm/hyperv.c +1 -1
@@ -2579,7 +2579,7 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) hc.code = hc.param &0xffff ; hc.var_cnt = (hc.param &HV_HYPERCALL_VARHEAD_MASK ) >>HV_HYPERCALL_VARHEAD_OFFSET ; hc.fast = !!(hc.param &HV_HYPERCALL_FAST_BIT ); - hc.rep_cnt = (hc.param >>HV_HYPERCALL_REP_COMP_OFFSET ) &0xfff ; + hc.rep_cnt = (hc.param >>HV_HYPERCALL_REP_COUNT_OFFSET ) &0xfff ; hc.rep_idx = (hc.param >>HV_HYPERCALL_REP_START_OFFSET ) &0xfff ; hc.rep = !!(hc.rep_cnt || hc.rep_idx);
include/asm-generic/mshyperv.h +1 -1
@@ -143,7 +143,7 @@ static inline u64 hv_do_rep_hypercall_ex(u16 code, u16 rep_count,u16 rep_comp; control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; - control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; + control |= (u64 )rep_count <<HV_HYPERCALL_REP_COUNT_OFFSET ; control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; do {
include/hyperv/hvgdk_mini.h +6 -2
@@ -521,9 +521,9 @@ union hv_vp_assist_msr_contents { /* HV_REGISTER_VP_ASSIST_PAGE */#define HV_HYPERCALL_VARHEAD_MASK GENMASK_ULL(26, 17)#define HV_HYPERCALL_RSVD0_MASK GENMASK_ULL(31, 27)#define HV_HYPERCALL_NESTED BIT_ULL(31) -#define HV_HYPERCALL_REP_COMP_OFFSET 32#define HV_HYPERCALL_REP_COMP_1 BIT_ULL(32) -#define HV_HYPERCALL_REP_COMP_MASK GENMASK_ULL(43, 32) +#define HV_HYPERCALL_REP_COUNT_OFFSET 32 +#define HV_HYPERCALL_REP_COUNT_MASK GENMASK_ULL(43, 32)#define HV_HYPERCALL_RSVD1_MASK GENMASK_ULL(47, 44)#define HV_HYPERCALL_REP_START_OFFSET 48#define HV_HYPERCALL_REP_START_MASK GENMASK_ULL(59, 48)
@@ -532,6 +532,10 @@ union hv_vp_assist_msr_contents { /* HV_REGISTER_VP_ASSIST_PAGE */ HV_HYPERCALL_RSVD1_MASK | \ HV_HYPERCALL_RSVD2_MASK) +/* Hypercall output status */ +#define HV_HYPERCALL_REP_COMP_OFFSET 32 +#define HV_HYPERCALL_REP_COMP_MASK GENMASK_ULL(43, 32) +/* HvFlushGuestPhysicalAddressSpace hypercalls */ struct hv_guest_mapping_flush {u64 address_space;
Signature · 1 line
2.51.2.vfs.0.1
[email protected]
AI reviewRe: [PATCH 2/3] Drivers: hv: Create REP_COUNT define
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] hv: commit message misstates REP_COUNT macro additions and usage --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] hv: commit message misstates REP_COUNT macro additions and usage --
commit d129b966732e944a64145e4d392d66acc76d0cf3
Author: Mukesh R <[email protected]>
Drivers: hv: Create REP_COUNT define
This commit adds HV_HYPERCALL_REP_COUNT_OFFSET and HV_HYPERCALL_REP_COUNT_MASK for Hyper-V repeat hypercall inputs and updates kvm_hv_hypercall() and hv_do_rep_hypercall_ex() to use HV_HYPERCALL_REP_COUNT_OFFSET.
Quoted 4 lines
count is part of the input struct at same bits. However, the same
COMP_MASKis used to set it. This maybe confusing for anyone reading the code. AddHV_HYPERCALL_REP_COUNT_MASKand use it for counts instead ofREP_COMP.
[Severity: Low]
Does the commit message accurately describe the macros added and updated here?
hv_do_rep_hypercall_ex() and kvm_hv_hypercall() previously used HV_HYPERCALL_REP_COMP_OFFSET, not HV_HYPERCALL_REP_COMP_MASK (which is only used for output status in hv_repcomp()).
This patch adds both HV_HYPERCALL_REP_COUNT_OFFSET and HV_HYPERCALL_REP_COUNT_MASK, and updates those two call sites to use HV_HYPERCALL_REP_COUNT_OFFSET rather than HV_HYPERCALL_REP_COUNT_MASK.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/3 x86/hyperv: Handle hypercall timeouts on hw confidential VMs
In reply to Mukesh ROn Hyperv when running on hw enabled confidential computing like TDX/SNP, any hypercall can return with a timeout. In that case the hypercall must be re-issued. If it is a repeat hypercall, then the caller is expected to adjust the repeat start index in the input control parameter. Note, this does not apply when running with paravisors. A paravisor will never return with timeouts.
Easiest is to modify the bottom most leaf function to handle the timeouts with special handling for repeat hypercalls.
--- arch/x86/hyperv/ivm.c | 52 +++++++++++++++++++++++++++------- include/asm-generic/mshyperv.h | 12 ++++++++ 2 files changed, 53 insertions(+), 11 deletions(-)
arch/x86/hyperv/ivm.c +41 -11
@@ -365,7 +365,7 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)do { ret =hv_do_hypercall (HVCALL_START_VP , start_vp_input,NULL ); - }while (hv_result (ret) ==HV_STATUS_TIMEOUT && retry--); + }while (hv_result_timeout (ret) && retry--);local_irq_restore (flags);
@@ -389,12 +389,26 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)u64 hv_snp_hypercall (u64 control,u64 param1,u64 param2) {u64 hv_status; +bool rep_hcall = !!hv_repcount (control); - register u64 __r8 asm("r8") = param2; - asmvolatile ("vmmcall" - :"=a" (hv_status), -"+c" (control),"+d" (param1),"+r" (__r8) - : :"cc" ,"memory" ,"r9" ,"r10" ,"r11" ); +for (;;) { + register u64 __r8 asm("r8") = param2; + asmvolatile ("vmmcall" + :"=a" (hv_status), +"+c" (control),"+d" (param1),"+r" (__r8) + : :"cc" ,"memory" ,"r9" ,"r10" ,"r11" ); + +if (!hv_result_timeout (hv_status)) +break ; + +if (rep_hcall) { +u16 rep_comp =hv_repcomp (hv_status); + + control &= ~HV_HYPERCALL_REP_START_MASK ; + control |= + (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ; + } + }return hv_status; }
@@ -438,15 +452,31 @@ static void hv_tdx_msr_read(u64 msr, u64 *val)u64 hv_tdx_hypercall (u64 control,u64 param1,u64 param2) { +u64 hv_status; +bool rep_hcall = !!hv_repcount (control);struct tdx_module_args args = { }; - args.r10 = control; - args.rdx = param1; - args.r8 = param2; +for (;;) { + args.r10 = control; + args.rdx = param1; + args.r8 = param2; - (void )__tdx_hypercall (&args); + (void )__tdx_hypercall (&args); + hv_status = args.r11; -return args.r11; +if (!hv_result_timeout (hv_status)) +break ; + +if (rep_hcall) { +u16 rep_comp =hv_repcomp (hv_status); + + control &= ~HV_HYPERCALL_REP_START_MASK ; + control |= + (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ; + } + } + +return hv_status; }#else
include/asm-generic/mshyperv.h +12 -0
@@ -122,6 +122,11 @@ static inline bool hv_result_success(u64 status)return hv_result (status) ==HV_STATUS_SUCCESS ; } +static inline bool hv_result_timeout (u64 status) +{ +return hv_result (status) ==HV_STATUS_TIMEOUT ; +} +static inline unsigned int hv_repcomp (u64 status) {/* Bits [43:32] of status have 'Reps completed' data. */
@@ -129,6 +134,13 @@ static inline unsigned int hv_repcomp(u64 status)HV_HYPERCALL_REP_COMP_OFFSET ; } +static inline unsigned int hv_repcount (u64 control) +{ +/* Bits [43:32] of input control have 'Reps count' data. */ +return (control &HV_HYPERCALL_REP_COUNT_MASK ) >> +HV_HYPERCALL_REP_COUNT_OFFSET ; +} +/* * Rep hypercalls. Callers of this functions are supposed to ensure that * rep_count, varhead_size, and rep_start comply with Hyper-V hypercall
Signature · 1 line
2.51.2.vfs.0.1
[email protected]
AI reviewRe: [PATCH 3/3] x86/hyperv: Handle hypercall timeouts on hw confidential VMs
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] x86/hyperv: unbounded timeout retry loops without watchdog or rep_comp checks - [Medium] x86/hyperv…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] x86/hyperv: unbounded timeout retry loops without watchdog or rep_comp checks
- [Medium] x86/hyperv: reuse of dirty tdx_module_args on retry in hv_tdx_hypercall()
- [Medium] x86/hyperv: missing timeout retry in hv_ghcb_hypercall() for SNP paravisor
- [Medium] x86/hyperv: dead retry loop and lost retry cap in hv_snp_boot_ap()
--
commit 6d87e857871bbab96f5f0cd1dc1cd6280f5a9405
Author: Mukesh R <[email protected]>
x86/hyperv: Handle hypercall timeouts on hw confidential VMs
This commit adds retry loops for HV_STATUS_TIMEOUT to hv_snp_hypercall() and hv_tdx_hypercall(), updating the repeat start index on repeat hypercalls, and introduces hv_result_timeout() and hv_repcount() helpers.
Quoted 3 lines
to adjust the repeat start index in the input control parameter. Note, this does not apply when running with paravisors. A paravisor will never return with timeouts.
[Severity: Medium]
Does hv_ghcb_hypercall() also need timeout retry handling for SNP guests running with a paravisor?
When ms_hyperv.paravisor_present is true, hv_post_message() (for non-confidential VMBus) and vmbus_set_event() bypass the paravisor and call the host hypervisor directly via hv_tdx_hypercall() on TDX and hv_ghcb_hypercall() on SNP. Unlike hv_tdx_hypercall(), hv_ghcb_hypercall() is not updated to retry on HV_STATUS_TIMEOUT. If the host returns a timeout there, vmbus_post_msg() fails immediately with -EINVAL and vmbus_set_event() silently drops the event signal.
arch/x86/hyperv/ivm.c int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu) · line 370
Quoted 12 lines
arch/x86/hyperv/ivm.c +1
-1@@ -365,7 +365,7 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)do { ret =hv_do_hypercall (HVCALL_START_VP , start_vp_input,NULL ); - }while (hv_result (ret) ==HV_STATUS_TIMEOUT && retry--); + }while (hv_result_timeout (ret) && retry--);local_irq_restore (flags);
[Severity: Medium]
Can hv_result_timeout(ret) ever be true here now?
hv_snp_boot_ap() is only registered on SEV-SNP guests without a paravisor, where hv_do_hypercall() dispatches to hv_snp_hypercall(). Because hv_snp_hypercall() now loops until the status is no longer HV_STATUS_TIMEOUT, the retry = 5 loop becomes dead code, and persistent HVCALL_START_VP timeouts spin forever with interrupts disabled instead of aborting and cleaning up the VMSA page via snp_cleanup_vmsa().
Quoted 22 lines
arch/x86/hyperv/ivm.c +12
-5@@ -389,12 +389,26 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)u64 hv_snp_hypercall (u64 control,u64 param1,u64 param2) {u64 hv_status; +bool rep_hcall = !!hv_repcount (control); - register u64 __r8 asm("r8") = param2; - asmvolatile ("vmmcall" - :"=a" (hv_status), -"+c" (control),"+d" (param1),"+r" (__r8) - : :"cc" ,"memory" ,"r9" ,"r10" ,"r11" ); +for (;;) { + register u64 __r8 asm("r8") = param2; + asmvolatile ("vmmcall" + :"=a" (hv_status), +"+c" (control),"+d" (param1),"+r" (__r8) + : :"cc" ,"memory" ,"r9" ,"r10" ,"r11" ); + +if (!hv_result_timeout (hv_status)) +break ; + +if (rep_hcall) {
arch/x86/hyperv/ivm.c int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu) · line 414
Quoted 10 lines
arch/x86/hyperv/ivm.c +7
-0+u16 rep_comp =hv_repcomp (hv_status); + + control &= ~HV_HYPERCALL_REP_START_MASK ; + control |= + (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ; + } + }return hv_status; }
[Severity: High]
Can this unbounded retry loop (and the matching one in hv_tdx_hypercall() below) trigger NMI hardlockup watchdog panics or hang the vCPU with IRQs disabled?
Long IRQ-disabled repeat hypercalls such as hv_mark_gpa_visibility() rely on touch_nmi_watchdog() in hv_do_rep_hypercall_ex() across continuation slices, which is bypassed while spinning inside these leaf loops.
In addition, rep_comp from the hypervisor is not checked against rep_count or the previous rep_start for forward progress, and zero-progress retries are unbounded. Persistent timeouts or a non-advancing rep_comp will wedge the vCPU indefinitely instead of returning an error to callers that have recovery or native fallback paths (such as native_flush_tlb_multi()).
Quoted 9 lines
arch/x86/hyperv/ivm.c +2
-1@@ -438,15 +452,31 @@ static void hv_tdx_msr_read(u64 msr, u64 *val)u64 hv_tdx_hypercall (u64 control,u64 param1,u64 param2) { +u64 hv_status; +bool rep_hcall = !!hv_repcount (control);struct tdx_module_args args = { }; - args.r10 = control;
arch/x86/hyperv/ivm.c static void hv_tdx_msr_read(u64 msr, u64 *val) · line 465
Quoted 10 lines
arch/x86/hyperv/ivm.c +6
-3- args.rdx = param1; - args.r8 = param2; +for (;;) { + args.r10 = control; + args.rdx = param1; + args.r8 = param2; - (void )__tdx_hypercall (&args); + (void )__tdx_hypercall (&args); + hv_status = args.r11;
[Severity: Medium]
Should struct tdx_module_args args be re-initialized inside the retry loop?
__tdx_hypercall() loads all 13 register fields from args (exposed via TDVMCALL_EXPOSE_REGS_MASK) and writes VMM output back into all of them on return. On a timeout retry, only r10, rdx, and r8 (plus rcx in __tdx_hypercall()) are rewritten, leaving stale VMM-returned values including HV_STATUS_TIMEOUT in args.r11 across iterations.
Because the return value of __tdx_hypercall() is ignored, if a retry fails without the VMM updating r11, hv_status = args.r11 will observe the stale HV_STATUS_TIMEOUT and spin indefinitely. Declaring and zero-initializing args inside the loop (as tdx_map_gpa() does) avoids reusing dirty state.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3