Hyper-V: Fix bugs and issues around repeat hypercall
Subsystems (4)
cover Hyper-V: Fix bugs and issues around repeat hypercall
This short series addresses few issues around repeat hypercall, most importantly the bug around not checking completed entries in case of failure.
V1:
o Rename hv_isolation_hw_cvm() to hv_isolation_type_hw()
o Add HV_REP_COUNT_MAX in case this goes in before the pre-commit patch that adds it.
Mukesh R (2):
Drivers: hv: Add new function hv_isolation_type_hw()
Drivers: hv: Fix bugs and issues around repeat hypercall
arch/x86/hyperv/ivm.c | 7 +++- drivers/hv/hv_common.c | 11 ++++-- drivers/hv/mshv_common.c | 19 +++++++--- drivers/hv/mshv_root_hv_call.c | 21 +++++++---- drivers/hv/mshv_root_main.c | 6 +-- include/asm-generic/mshyperv.h | 67 +++++++++++++++++++++++++--------- include/hyperv/hvgdk_mini.h | 2 +- 7 files changed, 94 insertions(+), 39 deletions(-)
Signature · 1 line
2.51.2.vfs.0.1
PATCH 1/2 Drivers: hv: Add new function hv_isolation_type_hw()
Add a new function hv_isolation_type_hw() for convenient check of whether a VM is running in HW confidential mode, ie, TDX or SNP on x86 or RME on ARM.
--- arch/x86/hyperv/ivm.c | 5 +++++ drivers/hv/hv_common.c | 9 +++++++-- include/asm-generic/mshyperv.h | 7 +++++++ 3 files changed, 19 insertions(+), 2 deletions(-)
arch/x86/hyperv/ivm.c +5 -0
@@ -944,3 +944,8 @@ bool hv_isolation_type_tdx(void) {return static_branch_unlikely (&isolation_type_tdx); } + +bool hv_isolation_type_hw (void ) +{ +return hv_isolation_type_snp () ||hv_isolation_type_tdx (); +}
drivers/hv/hv_common.c +7 -2
@@ -528,8 +528,7 @@ int hv_common_cpu_init(unsigned int cpu) *outputarg = (char *)mem +HV_HYP_PAGE_SIZE ; } -if (!ms_hyperv.paravisor_present && - (hv_isolation_type_snp () ||hv_isolation_type_tdx ())) { +if (!ms_hyperv.paravisor_present &&hv_isolation_type_hw ()) { ret =set_memory_decrypted ((unsigned long )mem, pgcount);if (ret) {/* It may be unsafe to free 'mem' */
@@ -696,6 +695,12 @@ bool __weak hv_isolation_type_tdx(void) }EXPORT_SYMBOL_GPL (hv_isolation_type_tdx); +bool __weak hv_isolation_type_hw (void ) +{ +return false; +} +EXPORT_SYMBOL_GPL (hv_isolation_type_hw); +void __weak hv_setup_vmbus_handler (void (*handler)(void )) { }
include/asm-generic/mshyperv.h +7 -0
@@ -84,6 +84,13 @@ u64 hv_do_fast_hypercall16(u16 control, u64 input1, u64 input2);bool hv_isolation_type_snp (void );bool hv_isolation_type_tdx (void ); +/* This needs to be before hv_do_rep_hypercall() below */ +#if IS_ENABLED (CONFIG_HYPERV ) +bool hv_isolation_type_hw (void ); +#else +static inline bool hv_isolation_type_hw (void ) {return false; } +#endif +/* * On architectures where Hyper-V doesn't support AEOI (e.g., ARM64), * it doesn't provide a recommendation flag and AEOI must be disabled.
Signature · 1 line
2.51.2.vfs.0.1
PATCH 2/2 Drivers: hv: Fix bugs and issues around repeat hypercall
In reply to Mukesh RThere are few issues/bugs around repeat hypercall:
o If the hypercall fails, it could still have processed some entries. Fix places where it assumes none of the entries were processed and will try to undo entries already processed or print incorrect completions.
o Hypercall returns back with timeouts only for HW confidential VMs. In that case it should be retried instead of returning back. Returning back results in error conditions and take down of VMs in most cases.
o For non confidential VMs, the hypercall could be streamlined as it will never return back with timeouts.
o Rename HV_STATUS_TIME_OUT to HV_STATUS_TIMEOUT to match hypervisor public header file. There is no reason for it to be different.
o Minor formatting fix for checkpatch. Error printing missing in couple places when rep hypercall fails.
--- arch/x86/hyperv/ivm.c | 2 +- drivers/hv/hv_common.c | 2 +- drivers/hv/mshv_common.c | 19 +++++++---- drivers/hv/mshv_root_hv_call.c | 21 +++++++----- drivers/hv/mshv_root_main.c | 6 ++-- include/asm-generic/mshyperv.h | 60 ++++++++++++++++++++++++---------- include/hyperv/hvgdk_mini.h | 2 +- 7 files changed, 75 insertions(+), 37 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
@@ -835,7 +835,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
drivers/hv/mshv_common.c +13 -6
@@ -56,16 +56,20 @@ int hv_call_get_vp_registers(u32 vp_index, u64 partition_id, u16 count, status =hv_do_rep_hypercall (HVCALL_GET_VP_REGISTERS , rep_count,0 , input_page, output_page); -if (!hv_result_success (status)) -break ; - completed =hv_repcomp (status);for (i =0 ; i < completed; ++i) registers[i].value = output_page[i]; registers += completed; remaining -= completed; + +if (!hv_result_success (status)) { +pr_err ("%s: count:%d comp:%d st:0x%llx\n" , __func__, + count, completed, status); +break ; + } } +local_irq_restore (flags);return hv_result_to_errno (status);
@@ -99,12 +103,15 @@ int hv_call_set_vp_registers(u32 vp_index, u64 partition_id, u16 count, status =hv_do_rep_hypercall (HVCALL_SET_VP_REGISTERS , rep_count,0 , input_page,NULL ); -if (!hv_result_success (status)) -break ; - completed =hv_repcomp (status); registers += completed; remaining -= completed; + +if (!hv_result_success (status)) { +pr_err ("%s: count:%d comp:%d st:0x%llx\n" , __func__, + count, completed, status); +break ; + } }local_irq_restore (flags);
drivers/hv/mshv_root_hv_call.c +13 -8
@@ -239,6 +239,7 @@ static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count,local_irq_restore (irq_flags); completed =hv_repcomp (status); + done += completed;if (hv_result_needs_memory (status)) { ret =hv_deposit_memory (partition_id, status);
@@ -246,11 +247,13 @@ static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count,break ; }else if (!hv_result_success (status)) { +pr_err ("%s: failed to map pages at gfn %#llx: done %u/%llu, flags=%#x, status=%#llx (%s)\n" , + __func__, gfn, done, page_count, flags, status, +hv_result_to_string (hv_result (status))); + ret =hv_result_to_errno (status);break ; } - - done += completed; }if (ret && done) {
@@ -356,20 +359,22 @@ int hv_call_get_gpa_access_states(u64 partition_id, u32 count, u64 gpa_base_pfn, input_page->flags = state_flags; rep_count =min (remaining,HV_GET_GPA_ACCESS_STATES_BATCH_SIZE ); - status =hv_do_rep_hypercall (HVCALL_GET_GPA_PAGES_ACCESS_STATES , rep_count, -0 , input_page, output_page); -if (!hv_result_success (status)) { -local_irq_restore (flags); -break ; - } + status =hv_do_rep_hypercall (HVCALL_GET_GPA_PAGES_ACCESS_STATES , + rep_count,0 , input_page, + output_page); + completed =hv_repcomp (status);for (i =0 ; i < completed; ++i) states[i].as_uint8 = output_page[i].as_uint8;local_irq_restore (flags); + states += completed; *written_total += completed; remaining -= completed; + +if (!hv_result_success (status)) +break ; }return hv_result_to_errno (status);
drivers/hv/mshv_root_main.c +3 -3
@@ -231,9 +231,9 @@ static int mshv_ioctl_passthru_hvcall(struct mshv_partition *partition, reps_completed =0 ; do { if (args.reps) { - status = hv_do_rep_hypercall_ex(args.code, args.reps, - 0, reps_completed, - input_pg, output_pg); + status = hv_do_rep_hypercall_reg(args.code, args.reps, + 0, reps_completed, + input_pg, output_pg); reps_completed = hv_repcomp(status); } else { status = hv_do_hypercall(args.code, input_pg, output_pg);
include/asm-generic/mshyperv.h +43 -17
100 lines not on this page · raw message
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
[email protected]
AI reviewRe: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] Drivers: hv: unused HV_REP_COUNT_MAX macro and dead return in mshyperv.h --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] Drivers: hv: unused HV_REP_COUNT_MAX macro and dead return in mshyperv.h
--
commit 9765fd900ad842b6359a24f592cd624ffdc92be5
Author: Mukesh R <[email protected]>
Drivers: hv: Fix bugs and issues around repeat hypercall
This commit updates repeat hypercall handling to account for partial completions on failure, splits repeat hypercall helpers between hardware confidential VMs and regular VMs, and renames HV_STATUS_TIME_OUT to HV_STATUS_TIMEOUT.
Quoted 12 lines
include/asm-generic/mshyperv.h +3
-0@@ -28,6 +28,9 @@#define VTPM_BASE_ADDRESS 0xfed40000 +#define HV_REP_COUNT_MAX \ + (HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET) +enum hv_partition_type {
[ ... ]
Quoted 49 lines
include/asm-generic/mshyperv.h +20
-12@@ -135,45 +143,63 @@ static inline unsigned int hv_repcomp(u64 status)/* Bits [43:32] of status have 'Reps completed' data. */ return (status &HV_HYPERCALL_REP_COMP_MASK ) >>HV_HYPERCALL_REP_COMP_OFFSET ; } -/* -* Rep hypercalls. Callers of this functions are supposed to ensure that -* rep_count, varhead_size, and rep_start comply with Hyper-V hypercall -* definition. -*/ -static inline u64 hv_do_rep_hypercall_ex (u16 code,u16 rep_count, -u16 varhead_size,u16 rep_start, -void *input,void *output) +static inline u64 hv_do_rep_hypercall_reg (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output) {u64 control = code; -u64 status; -u16 rep_comp; control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; - do { +return hv_do_hypercall (control, input, output); +} + +static inline u64 hv_do_rep_hypercall_cvm (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output) +{ +u64 status, control = code; +u16 rep_comp =0 ; + + control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; + control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; + control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; + +for (;;) { status =hv_do_hypercall (control, input, output); - if (!hv_result_success(status)) + +if (!hv_result_timeout (status))return status; rep_comp =hv_repcomp (status);
include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 186
Quoted 10 lines
include/asm-generic/mshyperv.h +1
-2- control &= ~HV_HYPERCALL_REP_START_MASK ; control |= (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ;touch_nmi_watchdog (); - }while (rep_comp < rep_count); + }return status; }
[Severity: Low]
Is the trailing return status; in hv_do_rep_hypercall_cvm() needed now that the loop is for (;;) with no break statement? Also, HV_REP_COUNT_MAX added earlier in this file has no users in the tree.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RE: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
In reply to Mukesh RFrom: Mukesh R <[email protected]> Sent: Friday, November 13, 2026 6:53 PM
Quoted 20 lines
There are few issues/bugs around repeat hypercall:
o If the hypercall fails, it could still have processed some entries. Fix places where it assumes none of the entries were processed and will try to undo entries already processed or print incorrect completions.o Hypercall returns back with timeouts only for HW confidential VMs. In that case it should be retried instead of returning back. Returning back results in error conditions and take down of VMs in most cases.
o For non confidential VMs, the hypercall could be streamlined as it will never return back with timeouts.
o Rename
HV_STATUS_TIME_OUTtoHV_STATUS_TIMEOUTto match hypervisor public header file. There is no reason for it to be different.o Minor formatting fix for checkpatch. Error printing missing in couple places when rep hypercall fails.
Signed-off-by Mukesh R <[email protected]>
[snip]
include/asm-generic/mshyperv.h line 32
Quoted 10 lines
include/asm-generic/mshyperv.h +2
-0@@ -28,6 +28,9 @@#define VTPM_BASE_ADDRESS 0xfed40000 +#define HV_REP_COUNT_MAX \ + (HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)
Sashiko noted that this new #define is never used anywhere.
Quoted 31 lines
include/asm-generic/mshyperv.h +9
-8+enum hv_partition_type {HV_PARTITION_TYPE_GUEST ,HV_PARTITION_TYPE_ROOT ,@@ -128,6 +131,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. */ @@ -135,45 +143,63 @@ static inline unsigned int hv_repcomp(u64 status)HV_HYPERCALL_REP_COMP_OFFSET ; } -/* -* Rep hypercalls. Callers of this functions are supposed to ensure that -* rep_count, varhead_size, and rep_start comply with Hyper-V hypercall -* definition. -*/ -static inline u64 hv_do_rep_hypercall_ex (u16 code,u16 rep_count, -u16 varhead_size,u16 rep_start, -void *input,void *output) +static inline u64 hv_do_rep_hypercall_reg (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output)
include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 155
Quoted 10 lines
include/asm-generic/mshyperv.h +0
-3{u64 control = code; -u64 status; -u16 rep_comp; control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; - do {
Dropped the looping behavior here may be much more significant than just "streamlining" as described in the commit message. The looping behavior was included when rep hypercalls were originally added in commit 806c89273bab in kernel 4.14, long before any SNP/TDX/CCA functionality was added. So it was present for "normal" VMs doing hypercalls on x86/x64 the normal way. The looping ensured that rep hypercalls could not return with no errors but with the completed rep count less than the requested rep count.
However, in my mind it *is* debatable whether that scenario could ever occur. The Hyper-V TLFS describes "Hypercall Continuation" [1] as essentially transparent to the caller. If a rep hypercall stops processing reps because of hitting the 50 microsecond time limit, it should restart and continue with the next set of reps without the caller having to loop. Commit 806c89273bab mentions hypercall continuation, but does not describe why the explicit looping was added or how that aligns with the TLFS statements. Vitaly -- it was a long time ago, but do you have any recollection?
Have you gotten an explicit assurance from the Hyper-V team that the transparent hypercall continuation really does work, and the explicit looping has all along been unnecessary? This assurance would be needed back to and including Windows Server 2016, which is now the oldest Hyper-V version supported by Linux. Also note that the rep hypercall code is architecture independent, so the transparent hypercall continuation would also need to work on arm64 when the hypercall is invoked via the standard path of using the HVC instruction (the TLFS doesn't cover arm64, so this is unclear).
If you have such assurance, then I'm good with the removal of the looping. But please make the removal a separate patch, with sufficient discussion in the commit message to justify why it's OK to do the removal (including that evidently the original commit misinterpreted how continuation worked). And instead of looping, perhaps do a sanity check that the completed rep count matches the requested rep count.
Without such assurance, I think the looping needs to stay. Callers are not prepared for a rep hypercall to return with no errors, but with the completed rep count less than the requested rep count.
Quoted 21 lines
include/asm-generic/mshyperv.h +17
-1+return hv_do_hypercall (control, input, output); +} + +static inline u64 hv_do_rep_hypercall_cvm (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output) +{ +u64 status, control = code; +u16 rep_comp =0 ; + + control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; + control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; + control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; + +for (;;) { status =hv_do_hypercall (control, input, output); -if (!hv_result_success (status)) + +if (!hv_result_timeout (status))return status;
include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 183
Quoted 10 lines
include/asm-generic/mshyperv.h +1
-2rep_comp =hv_repcomp (status); - control &= ~HV_HYPERCALL_REP_START_MASK ; control |= (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ;touch_nmi_watchdog (); - }while (rep_comp < rep_count); + }return status;
And Sashiko noted that this "return status" is dead code. The only way to exit this function is via the "return status" up in the loop.
Quoted 7 lines
include/asm-generic/mshyperv.h +4
-1} -/* For the typical case where rep_start is 0 */ +/* +* Rep hypercalls for HW confidential VMs return back when timeouts occur, +* whereas for regular VMs they return only on success or failure. In case +* of successful completion, entire rep_count is done. But a hypercall could
include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 197
Quoted 10 lines
include/asm-generic/mshyperv.h +5
-2+* fail with partial completions, hence callers must always check that +* in case of failure. Lastly, a rep_count value of 0 is allowed for some +* hypercalls. +*/ static inline u64 hv_do_rep_hypercall (u16 code,u16 rep_count,u16 varhead_size,void *input,void *output) { -return hv_do_rep_hypercall_ex (code, rep_count, varhead_size,0 , - input, output); +if (hv_isolation_type_hw ())
I don't think this is the right test, so let me explain. You don't describe the context for when HV_STATUS_TIMEOUT may be returned, and there's no TLFS documentation of this status, but I'm assuming the context is hypercall continuation. This isn't the typical "something isn't responding" timeout. I suspect it means the hypervisor hit the 50 us limit, and returned to the guest VM with some reps still to be done. In a normal VM when a hypercall is initiated by the hypercall page on x86/x64 or the HVC instruction on arm64, the transparent hypercall continuation mechanism makes a TIMEOUT status unnecessary. But with SNP, TDX, and CCA, hypercall invocation may be piggybacked on other transports, and transparent continuation isn't available. So Hyper-V returns HV_STATUS_TIMEOUT with the expectation that the invoking code will loop to do the remaining reps.
The key factor is whether the hypercall is piggybacked on other transports. The tests in hv_isolation_type_hw() don't capture that. For example, if a paravisor is present, hv_isolation_type_snp() will return "true" even though hypercalls are invoked via the standard x86/x64 hypercall page. The hypercalls trap to the paravisor. If the paravisor is functioning properly, it should do transparent hypercall continuation just like the hypervisor would. But this is something to be confirmed with the hypervisor and paravisor teams.
Piggybacking is done only when no paravisor is present, so the hv_do_rep_hypercall_cvm() path should be taken only in that circumstance. Your current code works, but it goes down the more expensive hv_do_rep_hypercall_cvm() path when it doesn't need to. (I'm not completely clear on how arm64 works with and without a paravisor, so my statement applies only to SNP and TDX. Need to sort out the CCA case with Kameron.)
Given the non-trivial complexity in sorting all this out, I wonder if distinguishing the _cvm and _reg cases is really worth it. The current code for the _reg case is admittedly inefficient because it does work for the looping case before likely deciding that no looping is needed. But that could easily be fixed. When no looping is necessary, which is the vast majority of cases, the looping code could end up being only a couple of instructions slower than a separately coded non-looping version (I did some builds and looked at the generated code). Since there's overhead in deciding which to use, there may not be any performance win from having both versions.
Michael
Quoted 7 lines
include/asm-generic/mshyperv.h +5
-0+return hv_do_rep_hypercall_cvm (code, rep_count, varhead_size,0 , + input, output); + else + return hv_do_rep_hypercall_reg(code, rep_count, varhead_size, 0, + input, output); }
RE: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
Michael Kelley <[email protected]> writes:
...
Quoted 41 lines
include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 155
Quoted 2 lines
include/asm-generic/mshyperv.h +0
-1- do {Dropped the looping behavior here may be much more significant than just "streamlining" as described in the commit message. The looping behavior was included when rep hypercalls were originally added in commit
806c89273babin kernel 4.14, long before any SNP/TDX/CCA functionality was added. So it was present for "normal" VMs doing hypercalls on x86/x64 the normal way. The looping ensured that rep hypercalls could not return with no errors but with the completed rep count less than the requested rep count.However, in my mind it *is* debatable whether that scenario could ever occur. The Hyper-V TLFS describes "Hypercall Continuation" [1] as essentially transparent to the caller. If a rep hypercall stops processing reps because of hitting the 50 microsecond time limit, it should restart and continue with the next set of reps without the caller having to loop. Commit
806c89273babmentions hypercall continuation, but does not describe why the explicit looping was added or how that aligns with the TLFS statements. Vitaly -- it was a long time ago, but do you have any recollection?Have you gotten an explicit assurance from the Hyper-V team that the transparent hypercall continuation really does work, and the explicit looping has all along been unnecessary? This assurance would be needed back to and including Windows Server 2016, which is now the oldest Hyper-V version supported by Linux. Also note that the rep hypercall code is architecture independent, so the transparent hypercall continuation would also need to work on arm64 when the hypercall is invoked via the standard path of using the HVC instruction (the TLFS doesn't cover arm64, so this is unclear).
If you have such assurance, then I'm good with the removal of the looping. But please make the removal a separate patch, with sufficient discussion in the commit message to justify why it's OK to do the removal (including that evidently the original commit misinterpreted how continuation worked). And instead of looping, perhaps do a sanity check that the completed rep count matches the requested rep count.
Quoted 6 lines
Without such assurance, I think the looping needs to stay. Callers are not prepared for a rep hypercall to return with no errors, but with the completed rep count less than the requested rep count.
It's been almost a decade since the looping behavior was added, it is very possible that I was using WS2012 for development back then. The commit says that the feature was required for HvFlushVirtualAddressList; I don't have any evidence left but I'm pretty sure 'incomplete' returns were observed. It is, of course, possible that I only observed them in synthetic testing when stuffing hypercall input to the maximum by some random data.
Overall, I agree with the proposal: keep the looping unless we get an explicit assurance from the host team that it can be dropped.
Signature · 1 line
Vitaly
Re: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
In reply to Michael KelleyQuoted 98 lines · Michael Kelley
From: Mukesh R <[email protected]> Sent: Friday, November 13, 2026 6:53 PM
Quoted 20 lines
There are few issues/bugs around repeat hypercall:
o If the hypercall fails, it could still have processed some entries. Fix places where it assumes none of the entries were processed and will try to undo entries already processed or print incorrect completions.o Hypercall returns back with timeouts only for HW confidential VMs. In that case it should be retried instead of returning back. Returning back results in error conditions and take down of VMs in most cases.
o For non confidential VMs, the hypercall could be streamlined as it will never return back with timeouts.
o Rename
HV_STATUS_TIME_OUTtoHV_STATUS_TIMEOUTto match hypervisor public header file. There is no reason for it to be different.o Minor formatting fix for checkpatch. Error printing missing in couple places when rep hypercall fails.
Signed-off-by Mukesh R <[email protected]>[snip]
include/asm-generic/mshyperv.h line 32
Quoted 10 lines
include/asm-generic/mshyperv.h +2
-0@@ -28,6 +28,9 @@#define VTPM_BASE_ADDRESS 0xfed40000 +#define HV_REP_COUNT_MAX \ + (HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)Sashiko noted that this new #define is never used anywhere.
Quoted 31 lines
include/asm-generic/mshyperv.h +9
-8+enum hv_partition_type {HV_PARTITION_TYPE_GUEST ,HV_PARTITION_TYPE_ROOT ,@@ -128,6 +131,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. */ @@ -135,45 +143,63 @@ static inline unsigned int hv_repcomp(u64 status)HV_HYPERCALL_REP_COMP_OFFSET ; } -/* -* Rep hypercalls. Callers of this functions are supposed to ensure that -* rep_count, varhead_size, and rep_start comply with Hyper-V hypercall -* definition. -*/ -static inline u64 hv_do_rep_hypercall_ex (u16 code,u16 rep_count, -u16 varhead_size,u16 rep_start, -void *input,void *output) +static inline u64 hv_do_rep_hypercall_reg (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output)include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 155
Quoted 10 lines
include/asm-generic/mshyperv.h +0
-3{u64 control = code; -u64 status; -u16 rep_comp; control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; - do {Dropped the looping behavior here may be much more significant than just "streamlining" as described in the commit message. The looping behavior was included when rep hypercalls were originally added in commit
806c89273babin kernel 4.14, long before any SNP/TDX/CCA functionality was added. So it was present for "normal" VMs doing hypercalls on x86/x64 the normal way. The looping ensured that rep hypercalls could not return with no errors but with the completed rep count less than the requested rep count.However, in my mind it *is* debatable whether that scenario could ever occur. The Hyper-V TLFS describes "Hypercall Continuation" [1] as essentially transparent to the caller. If a rep hypercall stops processing reps because of hitting the 50 microsecond time limit, it should restart and continue with the next set of reps without the caller having to loop. Commit
806c89273babmentions hypercall continuation, but does not describe why the explicit looping was added or how that aligns with the TLFS statements. Vitaly -- it was a long time ago, but do you have any recollection?
Quoted 3 lines
Have you gotten an explicit assurance from the Hyper-V team that the transparent hypercall continuation really does work, and the explicit looping has all along been unnecessary? This
Correct, hence the patch. Waiting on confirmation that it is same behavior all the way back to server 2016.
Basically, the hypervisor will just adjust the RIP back to the vmcall upon internal timeouts, and reschedule the vcpu. But for hw confidential vms, ie, tdx/snp/cca, it cannot adjust the RIP, hence return back to the guest with TIMEOUT, so cvm has to do in a loop.
Now, for paravisor support, I was just told by a paravisor team dev, that it will never return with TIMEOUTs. My view on that set aside, I guess for paravisor we'll just call the _reg function then. Thanks for bringing attention to that...
Quoted 7 lines
assurance would be needed back to and including Windows Server 2016, which is now the oldest Hyper-V version supported by Linux. Also note that the rep hypercall code is architecture independent, so the transparent hypercall continuation would also need to work on arm64 when the hypercall is invoked via the standard path of using the HVC instruction (the TLFS doesn't cover arm64, so this is unclear).
Both x86 and arm are same for this issue.
Quoted 7 lines
If you have such assurance, then I'm good with the removal of the looping. But please make the removal a separate patch, with sufficient discussion in the commit message to justify why it's OK to do the removal (including that evidently the original commit misinterpreted how continuation worked). And instead of looping, perhaps do a sanity check that the completed rep count matches the requested rep count.
Not sure if it's worth the overhead of sanity check, looks like callers are checking completed and loop anyways... But I would be OK with BUG(), since it would be a bug for that to happen. FWIW, it's been running internally with couple months, and haven't seen any issues.
Quoted 87 lines
Without such assurance, I think the looping needs to stay. Callers are not prepared for a rep hypercall to return with no errors, but with the completed rep count less than the requested rep count.
Quoted 21 lines
include/asm-generic/mshyperv.h +17
-1+return hv_do_hypercall (control, input, output); +} + +static inline u64 hv_do_rep_hypercall_cvm (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output) +{ +u64 status, control = code; +u16 rep_comp =0 ; + + control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; + control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; + control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; + +for (;;) { status =hv_do_hypercall (control, input, output); -if (!hv_result_success (status)) + +if (!hv_result_timeout (status))return status;include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 183
Quoted 10 lines
include/asm-generic/mshyperv.h +1
-2rep_comp =hv_repcomp (status); - control &= ~HV_HYPERCALL_REP_START_MASK ; control |= (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ;touch_nmi_watchdog (); - }while (rep_comp < rep_count); + }return status;And Sashiko noted that this "return status" is dead code. The only way to exit this function is via the "return status" up in the loop.
Quoted 7 lines
include/asm-generic/mshyperv.h +4
-1} -/* For the typical case where rep_start is 0 */ +/* +* Rep hypercalls for HW confidential VMs return back when timeouts occur, +* whereas for regular VMs they return only on success or failure. In case +* of successful completion, entire rep_count is done. But a hypercall could include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 197
Quoted 10 lines
include/asm-generic/mshyperv.h +5
-2+* fail with partial completions, hence callers must always check that +* in case of failure. Lastly, a rep_count value of 0 is allowed for some +* hypercalls. +*/ static inline u64 hv_do_rep_hypercall (u16 code,u16 rep_count,u16 varhead_size,void *input,void *output) { -return hv_do_rep_hypercall_ex (code, rep_count, varhead_size,0 , - input, output); +if (hv_isolation_type_hw ())I don't think this is the right test, so let me explain. You don't describe the context for when
HV_STATUS_TIMEOUTmay be returned, and there's no TLFS documentation of this status, but I'm assuming the context is hypercall continuation. This isn't the typical "something isn't responding" timeout. I suspect it means the hypervisor hit the 50 us limit, and returned to the guest VM with some reps still to be done. In a normal VM when a hypercall is initiated by the hypercall page on x86/x64 or the HVC instruction on arm64, the transparent hypercall continuation mechanism makes a TIMEOUT status unnecessary. But with SNP, TDX, and CCA, hypercall invocation may be piggybacked on other transports, and transparent continuation isn't available. So Hyper-V returnsHV_STATUS_TIMEOUTwith the expectation that the invoking code will loop to do the remaining reps.The key factor is whether the hypercall is piggybacked on other transports. The tests in
hv_isolation_type_hw()don't capture that. For example, if a paravisor is present,hv_isolation_type_snp()will return "true" even though hypercalls are invoked via the standard x86/x64 hypercall page. The hypercalls trap to the paravisor. If the paravisor is functioning properly, it should do transparent hypercall continuation just like the hypervisor would. But this is something to be confirmed with the hypervisor and paravisor teams.Piggybacking is done only when no paravisor is present, so the
hv_do_rep_hypercall_cvm()path should be taken only in that circumstance. Your current code works, but it goes down the more expensivehv_do_rep_hypercall_cvm()path when it doesn't need to. (I'm not completely clear on how arm64 works with and without a paravisor, so my statement applies only to SNP and TDX. Need to sort out the CCA case with Kameron.)
Quoted 10 lines
Given the non-trivial complexity in sorting all this out, I wonder if distinguishing the
_cvmand_regcases is really worth it. The current code for the_regcase is admittedly inefficient because it does work for the looping case before likely deciding that no looping is needed. But that could easily be fixed. When no looping is necessary, which is the vast majority of cases, the looping code could end up being only a couple of instructions slower than a separately coded non-looping version (I did some builds and looked at the generated code). Since there's overhead in deciding which to use, there may not be any performance win from having both versions.
Well, to me it's correctness issue also. If the ABI says that it will not return with TIMEOUTs for non-cvms, then adding loop just masks it instead of opening a bug report against the hypervisor.
Hope that makes sense.
On a side note, to solicit your thoughts, i am not sure i like the touch_nmi_watchdog()... it appears the the WD is triggered in seconds, and if a hypercall is that long, schedule() really should be done in between. IOW, remove the touch, instead any very long running hypercalls should call schedule(). Of course, that would be a different patch.
Thanks,
-Mukesh
Quoted 9 lines
Michael
Quoted 7 lines
include/asm-generic/mshyperv.h +5
-0+return hv_do_rep_hypercall_cvm (code, rep_count, varhead_size,0 , + input, output); + else + return hv_do_rep_hypercall_reg(code, rep_count, varhead_size, 0, + input, output); }
RE: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
From: Mukesh R <[email protected]> Sent: Monday, November 16, 2026 6:29 PM
Quoted 13 lines · Michael Kelley
Quoted 11 lines
From: Mukesh R <[email protected]> Sent: Friday, November 13, 2026 6:53 PM
Quoted 10 lines
There are few issues/bugs around repeat hypercall:
o If the hypercall fails, it could still have processed some entries. Fix places where it assumes none of the entries were processed and will try to undo entries already processed or print incorrect completions.o Hypercall returns back with timeouts only for HW confidential VMs. In that case it should be retried instead of returning back. Returning back results in error conditions and take down of VMs in most cases.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
o For non confidential VMs, the hypercall could be streamlined as it will never return back with timeouts.
o Rename
HV_STATUS_TIME_OUTtoHV_STATUS_TIMEOUTto match hypervisor public header file. There is no reason for it to be different.o Minor formatting fix for checkpatch. Error printing missing in couple places when rep hypercall fails.
In my view, the changes to the call sites of hv_do_rep_hypercall() should be in a separate patch. These are just bugs in how the undo is handled in case of an error, and the fixes presumably want to backported.
Handling a TIMEOUT return should also be a separate patch. This is also a bug and the fix should be backported. The TIMEOUT is generated under a specific set of circumstances -- it's not a generic error. The patch should describe how and why those circumstances arise in the overall context of hypercall continuation. There's not any such documentation elsewhere, so the info should be captured in the patch commit message or code comments for future reference.
Finally, if the looping behavior in a normal VM is to be removed, that would be a third patch. Given that this changes long-standing behavior, some explanation of "why" is needed in the commit message. I don't think this change should be backported.
Quoted 79 lines
Quoted 79 lines
Quoted 2 lines
Signed-off-by Mukesh R <[email protected]>[snip]
include/asm-generic/mshyperv.h line 32
Quoted 10 lines
include/asm-generic/mshyperv.h +2
-0@@ -28,6 +28,9 @@#define VTPM_BASE_ADDRESS 0xfed40000 +#define HV_REP_COUNT_MAX \ + (HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)Sashiko noted that this new #define is never used anywhere.
Quoted 31 lines
include/asm-generic/mshyperv.h +9
-8+enum hv_partition_type {HV_PARTITION_TYPE_GUEST ,HV_PARTITION_TYPE_ROOT ,@@ -128,6 +131,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. */ @@ -135,45 +143,63 @@ static inline unsigned int hv_repcomp(u64 status)HV_HYPERCALL_REP_COMP_OFFSET ; } -/* -* Rep hypercalls. Callers of this functions are supposed to ensure that -* rep_count, varhead_size, and rep_start comply with Hyper-V hypercall -* definition. -*/ -static inline u64 hv_do_rep_hypercall_ex (u16 code,u16 rep_count, -u16 varhead_size,u16 rep_start, -void *input,void *output) +static inline u64 hv_do_rep_hypercall_reg (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output)include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 155
Quoted 10 lines
include/asm-generic/mshyperv.h +0
-3{u64 control = code; -u64 status; -u16 rep_comp; control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; - do {Dropped the looping behavior here may be much more significant than just "streamlining" as described in the commit message. The looping behavior was included when rep hypercalls were originally added in commit
806c89273babin kernel 4.14, long before any SNP/TDX/CCA functionality was added. So it was present for "normal" VMs doing hypercalls on x86/x64 the normal way. The looping ensured that rep hypercalls could not return with no errors but with the completed rep count less than the requested rep count.However, in my mind it *is* debatable whether that scenario could ever occur. The Hyper-V TLFS describes "Hypercall Continuation" [1] as essentially transparent to the caller. If a rep hypercall stops processing reps because of hitting the 50 microsecond time limit, it should restart and continue with the next set of reps without the caller having to loop. Commit
806c89273babmentions hypercall continuation, but does not describe why the explicit looping was added or how that aligns with the TLFS statements. Vitaly -- it was a long time ago, but do you have any recollection?
Quoted 6 lines
Quoted 3 lines
Have you gotten an explicit assurance from the Hyper-V team that the transparent hypercall continuation really does work, and the explicit looping has all along been unnecessary? This
Correct, hence the patch. Waiting on confirmation that it is same behavior all the way back to server 2016.
Sounds good. FWIW, old TLFS version 2.0A, which is specifically titled to cover Windows Server 2008 R2, has the paragraph about hypercall continuation. So that functionality was evidently present very early on. It is still curious that Vitaly found it necessary to include the looping behavior in the Linux guest code.
Quoted 11 lines
Basically, the hypervisor will just adjust the RIP back to the vmcall upon internal timeouts, and reschedule the vcpu. But for hw confidential vms, ie, tdx/snp/cca, it cannot adjust the RIP, hence return back to the guest with TIMEOUT, so cvm has to do in a loop.
Now, for paravisor support, I was just told by a paravisor team dev, that it will never return with TIMEOUTs. My view on that set aside, I guess for paravisor we'll just call the
_regfunction then. Thanks for bringing attention to that...
Yes, that would be what I expected.
Quoted 10 lines
Quoted 8 lines
assurance would be needed back to and including Windows Server 2016, which is now the oldest Hyper-V version supported by Linux. Also note that the rep hypercall code is architecture independent, so the transparent hypercall continuation would also need to work on arm64 when the hypercall is invoked via the standard path of using the HVC instruction (the TLFS doesn't cover arm64, so this is unclear).
Both x86 and arm are same for this issue.
OK, good.
Quoted 115 lines
Quoted 8 lines
If you have such assurance, then I'm good with the removal of the looping. But please make the removal a separate patch, with sufficient discussion in the commit message to justify why it's OK to do the removal (including that evidently the original commit misinterpreted how continuation worked). And instead of looping, perhaps do a sanity check that the completed rep count matches the requested rep count.
Not sure if it's worth the overhead of sanity check, looks like callers are checking completed and loop anyways... But I would be OK with
BUG(), since it would be a bug for that to happen. FWIW, it's been running internally with couple months, and haven't seen any issues.Quoted 5 lines
Without such assurance, I think the looping needs to stay. Callers are not prepared for a rep hypercall to return with no errors, but with the completed rep count less than the requested rep count.
[1] https://learn.microsoft.com/en-us/virtualization/hyper-v-on-
windows/tlfs/hypercall-interface#hypercall-continuation
Quoted 82 lines
Quoted 22 lines
include/asm-generic/mshyperv.h +17
-1+return hv_do_hypercall (control, input, output); +} + +static inline u64 hv_do_rep_hypercall_cvm (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output) +{ +u64 status, control = code; +u16 rep_comp =0 ; + + control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; + control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; + control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; + +for (;;) { status =hv_do_hypercall (control, input, output); -if (!hv_result_success (status)) + +if (!hv_result_timeout (status))return status;include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 183
Quoted 10 lines
include/asm-generic/mshyperv.h +1
-2rep_comp =hv_repcomp (status); - control &= ~HV_HYPERCALL_REP_START_MASK ; control |= (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ;touch_nmi_watchdog (); - }while (rep_comp < rep_count); + }return status;And Sashiko noted that this "return status" is dead code. The only way to exit this function is via the "return status" up in the loop.
Quoted 7 lines
include/asm-generic/mshyperv.h +4
-1} -/* For the typical case where rep_start is 0 */ +/* +* Rep hypercalls for HW confidential VMs return back when timeouts occur, +* whereas for regular VMs they return only on success or failure. In case +* of successful completion, entire rep_count is done. But a hypercall could include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 197
Quoted 10 lines
include/asm-generic/mshyperv.h +5
-2+* fail with partial completions, hence callers must always check that +* in case of failure. Lastly, a rep_count value of 0 is allowed for some +* hypercalls. +*/ static inline u64 hv_do_rep_hypercall (u16 code,u16 rep_count,u16 varhead_size,void *input,void *output) { -return hv_do_rep_hypercall_ex (code, rep_count, varhead_size,0 , - input, output); +if (hv_isolation_type_hw ())I don't think this is the right test, so let me explain. You don't describe the context for when
HV_STATUS_TIMEOUTmay be returned, and there's no TLFS documentation of this status, but I'm assuming the context is hypercall continuation. This isn't the typical "something isn't responding" timeout. I suspect it means the hypervisor hit the 50 us limit, and returned to the guest VM with some reps still to be done. In a normal VM when a hypercall is initiated by the hypercall page on x86/x64 or the HVC instruction on arm64, the transparent hypercall continuation mechanism makes a TIMEOUT status unnecessary. But with SNP, TDX, and CCA, hypercall invocation may be piggybacked on other transports, and transparent continuation isn't available. So Hyper-V returnsHV_STATUS_TIMEOUTwith the expectation that the invoking code will loop to do the remaining reps.The key factor is whether the hypercall is piggybacked on other transports. The tests in
hv_isolation_type_hw()don't capture that. For example, if a paravisor is present,hv_isolation_type_snp()will return "true" even though hypercalls are invoked via the standard x86/x64 hypercall page. The hypercalls trap to the paravisor. If the paravisor is functioning properly, it should do transparent hypercall continuation just like the hypervisor would. But this is something to be confirmed with the hypervisor and paravisor teams.Piggybacking is done only when no paravisor is present, so the
hv_do_rep_hypercall_cvm()path should be taken only in that circumstance. Your current code works, but it goes down the more expensivehv_do_rep_hypercall_cvm()path when it doesn't need to. (I'm not completely clear on how arm64 works with and without a paravisor, so my statement applies only to SNP and TDX. Need to sort out the CCA case with Kameron.)Quoted 10 lines
Given the non-trivial complexity in sorting all this out, I wonder if distinguishing the
_cvmand_regcases is really worth it. The current code for the_regcase is admittedly inefficient because it does work for the looping case before likely deciding that no looping is needed. But that could easily be fixed. When no looping is necessary, which is the vast majority of cases, the looping code could end up being only a couple of instructions slower than a separately coded non-looping version (I did some builds and looked at the generated code). Since there's overhead in deciding which to use, there may not be any performance win from having both versions.
Quoted 3 lines
Well, to me it's correctness issue also. If the ABI says that it will not return with TIMEOUTs for non-cvms, then adding loop just masks it instead of opening a bug report against the hypervisor.
I see your point, but would not call it a "correctness issue". A "correctness issue" is a case where the result is wrong, and there's no wrong result here. I've definitely fixed edge-case issues that might never occur in practice just in the interest of overall "correctness", but that's not this case.
Your point opens a different can-of-worms. The threat model for Linux guests on Hyper-V has historically been that the guest trusts the Hyper-V host to do the right thing. A buggy or malicious host can arbitrarily read or write guest memory, so there's really not much choice but for the guest to trust the host (of course, in a CoCo VM the threat model is different). As such, Linux code has never tried to fully validate what it gets back from Hyper-V. Various ad hoc validations have been added when it seemed helpful to do so, but it's not a priority or a directive. Doing such a validation here is not objectionable, but I wouldn't add a significant complexity to do so.
FWIW, here's pseudo-code for an alternate approach that doesn't require splitting into separate _reg and _cvm versions:
static inline u64 hv_do_rep_hypercall_ex(u16 code, u16 rep_count,
u16 varhead_size, u16 rep_start, void *input, void *output)
{
u64 status, control = code;
u16 rep_comp = 0;
control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
control |= (u64)rep_start << HV_HYPERCALL_REP_START_OFFSET;
for (;;) {
status = hv_do_hypercall(control, input, output);
if (!hv_result_timeout(status))
/* If result is success, could validate
completed reps == rep_count - rep_start */
return status;
if (! <hypercall is over alt transport> ) {
/* Should not get TIMEOUT with normal calling seq */
WARN();
return HV_STATUS_INVALID_PARAMETER; /* or some such */
}
rep_comp = hv_repcomp(status);
control &= ~HV_HYPERCALL_REP_START_MASK;
control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
}
}In almost all cases, the result is not TIMEOUT, so the code returns immediately with no extra overhead except to check for TIMEOUT. If it *is* TIMEOUT, then go to the trouble to figure out whether TIMEOUT is valid. If the VM is using the normal hypercall invocation mechanism, flag that as a hypervisor bug and don't loop.
Quoted 8 lines
Hope that makes sense.
On a side note, to solicit your thoughts, i am not sure i like the
touch_nmi_watchdog()... it appears the the WD is triggered in seconds, and if a hypercall is that long,schedule()really should be done in between. IOW, remove the touch, instead any very long running hypercalls should callschedule(). Of course, that would be a different patch.
I agree. I would be fine with removing the touch_nmi_watchdog().
Michael
Re: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
Quoted 35 lines · Michael Kelley
From: Mukesh R <[email protected]> Sent: Monday, November 16, 2026 6:29 PM
Quoted 13 lines · Michael Kelley
Quoted 11 lines
From: Mukesh R <[email protected]> Sent: Friday, November 13, 2026 6:53 PM
Quoted 10 lines
There are few issues/bugs around repeat hypercall:
o If the hypercall fails, it could still have processed some entries. Fix places where it assumes none of the entries were processed and will try to undo entries already processed or print incorrect completions.o Hypercall returns back with timeouts only for HW confidential VMs. In that case it should be retried instead of returning back. Returning back results in error conditions and take down of VMs in most cases.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
o For non confidential VMs, the hypercall could be streamlined as it will never return back with timeouts.
o Rename
HV_STATUS_TIME_OUTtoHV_STATUS_TIMEOUTto match hypervisor public header file. There is no reason for it to be different.o Minor formatting fix for checkpatch. Error printing missing in couple places when rep hypercall fails.
In my view, the changes to the call sites of
hv_do_rep_hypercall()should be in a separate patch. These are just bugs in how the undo is handled in case of an error, and the fixes presumably want to backported.Handling a TIMEOUT return should also be a separate patch. This is also a bug and the fix should be backported. The TIMEOUT is generated under a specific set of circumstances -- it's not a generic error. The patch should describe how and why those circumstances arise in the overall context of hypercall continuation. There's not any such documentation elsewhere, so the info should be captured in the patch commit message or code comments for future reference.
Quoted 4 lines
Finally, if the looping behavior in a normal VM is to be removed, that would be a third patch. Given that this changes long-standing behavior, some explanation of "why" is needed in the commit message. I don't think this change should be backported.
The looping behavior is tied to timeout, so I think it could be two instead of 3 splits...
Quoted 79 lines
Quoted 79 lines
Quoted 79 lines
Quoted 2 lines
Signed-off-by Mukesh R <[email protected]>[snip]
include/asm-generic/mshyperv.h line 32
Quoted 10 lines
include/asm-generic/mshyperv.h +2
-0@@ -28,6 +28,9 @@#define VTPM_BASE_ADDRESS 0xfed40000 +#define HV_REP_COUNT_MAX \ + (HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)Sashiko noted that this new #define is never used anywhere.
Quoted 31 lines
include/asm-generic/mshyperv.h +9
-8+enum hv_partition_type {HV_PARTITION_TYPE_GUEST ,HV_PARTITION_TYPE_ROOT ,@@ -128,6 +131,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. */ @@ -135,45 +143,63 @@ static inline unsigned int hv_repcomp(u64 status)HV_HYPERCALL_REP_COMP_OFFSET ; } -/* -* Rep hypercalls. Callers of this functions are supposed to ensure that -* rep_count, varhead_size, and rep_start comply with Hyper-V hypercall -* definition. -*/ -static inline u64 hv_do_rep_hypercall_ex (u16 code,u16 rep_count, -u16 varhead_size,u16 rep_start, -void *input,void *output) +static inline u64 hv_do_rep_hypercall_reg (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output)include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 155
Quoted 10 lines
include/asm-generic/mshyperv.h +0
-3{u64 control = code; -u64 status; -u16 rep_comp; control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; - do {Dropped the looping behavior here may be much more significant than just "streamlining" as described in the commit message. The looping behavior was included when rep hypercalls were originally added in commit
806c89273babin kernel 4.14, long before any SNP/TDX/CCA functionality was added. So it was present for "normal" VMs doing hypercalls on x86/x64 the normal way. The looping ensured that rep hypercalls could not return with no errors but with the completed rep count less than the requested rep count.However, in my mind it *is* debatable whether that scenario could ever occur. The Hyper-V TLFS describes "Hypercall Continuation" [1] as essentially transparent to the caller. If a rep hypercall stops processing reps because of hitting the 50 microsecond time limit, it should restart and continue with the next set of reps without the caller having to loop. Commit
806c89273babmentions hypercall continuation, but does not describe why the explicit looping was added or how that aligns with the TLFS statements. Vitaly -- it was a long time ago, but do you have any recollection?
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Have you gotten an explicit assurance from the Hyper-V team that the transparent hypercall continuation really does work, and the explicit looping has all along been unnecessary? This
Correct, hence the patch. Waiting on confirmation that it is same behavior all the way back to server 2016.
Confirming, that it is same behavior all the way back to 2016.
Quoted 6 lines
Sounds good. FWIW, old TLFS version 2.0A, which is specifically titled to cover Windows Server 2008 R2, has the paragraph about hypercall continuation. So that functionality was evidently present very early on. It is still curious that Vitaly found it necessary to include the looping behavior in the Linux guest code.
Maybe Vitaly was running into a genuine bug, not sure.
Quoted 142 lines
Quoted 11 lines
Basically, the hypervisor will just adjust the RIP back to the vmcall upon internal timeouts, and reschedule the vcpu. But for hw confidential vms, ie, tdx/snp/cca, it cannot adjust the RIP, hence return back to the guest with TIMEOUT, so cvm has to do in a loop.
Now, for paravisor support, I was just told by a paravisor team dev, that it will never return with TIMEOUTs. My view on that set aside, I guess for paravisor we'll just call the
_regfunction then. Thanks for bringing attention to that...Yes, that would be what I expected.
Quoted 10 lines
Quoted 8 lines
assurance would be needed back to and including Windows Server 2016, which is now the oldest Hyper-V version supported by Linux. Also note that the rep hypercall code is architecture independent, so the transparent hypercall continuation would also need to work on arm64 when the hypercall is invoked via the standard path of using the HVC instruction (the TLFS doesn't cover arm64, so this is unclear).
Both x86 and arm are same for this issue.
OK, good.
Quoted 115 lines
Quoted 8 lines
If you have such assurance, then I'm good with the removal of the looping. But please make the removal a separate patch, with sufficient discussion in the commit message to justify why it's OK to do the removal (including that evidently the original commit misinterpreted how continuation worked). And instead of looping, perhaps do a sanity check that the completed rep count matches the requested rep count.
Not sure if it's worth the overhead of sanity check, looks like callers are checking completed and loop anyways... But I would be OK with
BUG(), since it would be a bug for that to happen. FWIW, it's been running internally with couple months, and haven't seen any issues.Quoted 5 lines
Without such assurance, I think the looping needs to stay. Callers are not prepared for a rep hypercall to return with no errors, but with the completed rep count less than the requested rep count.
[1] https://learn.microsoft.com/en-us/virtualization/hyper-v-on-
windows/tlfs/hypercall-interface#hypercall-continuation
Quoted 92 lines
Quoted 22 lines
include/asm-generic/mshyperv.h +17
-1+return hv_do_hypercall (control, input, output); +} + +static inline u64 hv_do_rep_hypercall_cvm (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output) +{ +u64 status, control = code; +u16 rep_comp =0 ; + + control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; + control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; + control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; + +for (;;) { status =hv_do_hypercall (control, input, output); -if (!hv_result_success (status)) + +if (!hv_result_timeout (status))return status;include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 183
Quoted 10 lines
include/asm-generic/mshyperv.h +1
-2rep_comp =hv_repcomp (status); - control &= ~HV_HYPERCALL_REP_START_MASK ; control |= (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ;touch_nmi_watchdog (); - }while (rep_comp < rep_count); + }return status;And Sashiko noted that this "return status" is dead code. The only way to exit this function is via the "return status" up in the loop.
Quoted 7 lines
include/asm-generic/mshyperv.h +4
-1} -/* For the typical case where rep_start is 0 */ +/* +* Rep hypercalls for HW confidential VMs return back when timeouts occur, +* whereas for regular VMs they return only on success or failure. In case +* of successful completion, entire rep_count is done. But a hypercall could include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 197
Quoted 10 lines
include/asm-generic/mshyperv.h +5
-2+* fail with partial completions, hence callers must always check that +* in case of failure. Lastly, a rep_count value of 0 is allowed for some +* hypercalls. +*/ static inline u64 hv_do_rep_hypercall (u16 code,u16 rep_count,u16 varhead_size,void *input,void *output) { -return hv_do_rep_hypercall_ex (code, rep_count, varhead_size,0 , - input, output); +if (hv_isolation_type_hw ())I don't think this is the right test, so let me explain. You don't describe the context for when
HV_STATUS_TIMEOUTmay be returned, and there's no TLFS documentation of this status, but I'm assuming the context is hypercall continuation. This isn't the typical "something isn't responding" timeout. I suspect it means the hypervisor hit the 50 us limit, and returned to the guest VM with some reps still to be done. In a normal VM when a hypercall is initiated by the hypercall page on x86/x64 or the HVC instruction on arm64, the transparent hypercall continuation mechanism makes a TIMEOUT status unnecessary. But with SNP, TDX, and CCA, hypercall invocation may be piggybacked on other transports, and transparent continuation isn't available. So Hyper-V returnsHV_STATUS_TIMEOUTwith the expectation that the invoking code will loop to do the remaining reps.The key factor is whether the hypercall is piggybacked on other transports. The tests in
hv_isolation_type_hw()don't capture that. For example, if a paravisor is present,hv_isolation_type_snp()will return "true" even though hypercalls are invoked via the standard x86/x64 hypercall page. The hypercalls trap to the paravisor. If the paravisor is functioning properly, it should do transparent hypercall continuation just like the hypervisor would. But this is something to be confirmed with the hypervisor and paravisor teams.Piggybacking is done only when no paravisor is present, so the
hv_do_rep_hypercall_cvm()path should be taken only in that circumstance. Your current code works, but it goes down the more expensivehv_do_rep_hypercall_cvm()path when it doesn't need to. (I'm not completely clear on how arm64 works with and without a paravisor, so my statement applies only to SNP and TDX. Need to sort out the CCA case with Kameron.)Given the non-trivial complexity in sorting all this out, I wonder if distinguishing the
_cvmand_regcases is really worth it. The current code for the_regcase is admittedly inefficient because it does work for the looping case before likely deciding that no looping is needed. But that could easily be fixed. When no looping is necessary, which is the vast majority of cases, the looping code could end up being only a couple of instructions slower than a separately coded non-looping version (I did some builds and looked at the generated code). Since there's overhead in deciding which to use, there may not be any performance win from having both versions.
Quoted 8 lines
Quoted 3 lines
Well, to me it's correctness issue also. If the ABI says that it will not return with TIMEOUTs for non-cvms, then adding loop just masks it instead of opening a bug report against the hypervisor.
I see your point, but would not call it a "correctness issue". A "correctness issue" is a case where the result is wrong, and there's no wrong result here. I've definitely fixed edge-case issues that might never occur in practice just in the interest of overall "correctness", but that's not this case.
Well, imo, abi says it will not timeout. if it does, it is an "incorrect behavior". But anyways, not worth nit picking about :).
Quoted 10 lines
Your point opens a different can-of-worms. The threat model for Linux guests on Hyper-V has historically been that the guest trusts the Hyper-V host to do the right thing. A buggy or malicious host can arbitrarily read or write guest memory, so there's really not much choice but for the guest to trust the host (of course, in a CoCo VM the threat model is different). As such, Linux code has never tried to fully validate what it gets back from Hyper-V. Various ad hoc validations have been added when it seemed helpful to do so, but it's not a priority or a directive. Doing such a validation here is not objectionable, but I wouldn't add a significant complexity to do so.
Ok, I'm ok with adding a WARN() to catch incorrect behaviors...
Quoted 34 lines
FWIW, here's pseudo-code for an alternate approach that doesn't require splitting into separate
_regand_cvmversions:static inline u64 hv_do_rep_hypercall_ex(u16 code, u16
rep_count,
u16varhead_size, u16rep_start, void *input, void *output){ u64 status, control = code; u16 rep_comp = 0; control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; control |= (u64)rep_start << HV_HYPERCALL_REP_START_OFFSET; for (;;) { status = hv_do_hypercall(control, input, output); if (!hv_result_timeout(status)) /* If result is success, could validate completed reps == rep_count - rep_start */ return status; if (! <hypercall is over alt transport> ) { /* Should not get TIMEOUT with normal calling seq */ WARN(); return HV_STATUS_INVALID_PARAMETER; /* or some such */ } rep_comp = hv_repcomp(status); control &= ~HV_HYPERCALL_REP_START_MASK; control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; } }
Quoted 5 lines
In almost all cases, the result is not TIMEOUT, so the code returns immediately with no extra overhead except to check for TIMEOUT. If it *is* TIMEOUT, then go to the trouble to figure out whether TIMEOUT is valid. If the VM is using the normal hypercall invocation mechanism, flag that as a hypervisor bug and don't loop.
Well, the end result is almost the same, but I like different function (will not be _cvm anymore) because it tells the reader upfront of different behaviors of the repeat hypercalls. I'll experiment with both, and send the one that I like...
Thanks,
-Mukesh
Quoted 12 lines
Quoted 8 lines
Hope that makes sense.
On a side note, to solicit your thoughts, i am not sure i like the
touch_nmi_watchdog()... it appears the the WD is triggered in seconds, and if a hypercall is that long,schedule()really should be done in between. IOW, remove the touch, instead any very long running hypercalls should callschedule(). Of course, that would be a different patch.I agree. I would be fine with removing the
touch_nmi_watchdog().Michael
Re: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
Quoted 1 line · Mukesh R
On 11/17/26 09:41, Michael Kelley wrote:
...
Quoted 12 lines
Confirming, that it is same behavior all the way back to 2016.
Quoted 6 lines
Sounds good. FWIW, old TLFS version 2.0A, which is specifically titled to cover Windows Server 2008 R2, has the paragraph about hypercall continuation. So that functionality was evidently present very early on. It is still curious that Vitaly found it necessary to include the looping behavior in the Linux guest code.
Maybe Vitaly was running into a genuine bug, not sure.
I'm not sure either :-(
WS2012 was definitely around back then, not sure about 2008. It may also make sense to add at least a debug print in case we find ourselves running on something that old. AFAICS, even the TLFS version 2.0A mentions PV TLB flush hypercalls. If looping is still required for these old versions, we may introduce some very hard-to-find bugs if we drop it.
Signature · 1 line
Vitaly
RE: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
From: Vitaly Kuznetsov <[email protected]> Sent: Wednesday, November 18, 2026 6:02 AM
Quoted 19 lines
Quoted 1 line · Mukesh R
On 11/17/26 09:41, Michael Kelley wrote:
...
Quoted 11 lines
Confirming, that it is same behavior all the way back to 2016.
Quoted 6 lines
Sounds good. FWIW, old TLFS version 2.0A, which is specifically titled to cover Windows Server 2008 R2, has the paragraph about hypercall continuation. So that functionality was evidently present very early on. It is still curious that Vitaly found it necessary to include the looping behavior in the Linux guest code.
Maybe Vitaly was running into a genuine bug, not sure.
Quoted 9 lines
I'm not sure either :-(
WS2012 was definitely around back then, not sure about 2008. It may also make sense to add at least a debug print in case we find ourselves running on something that old. AFAICS, even the TLFS version 2.0A mentions PV TLB flush hypercalls. If looping is still required for these old versions, we may introduce some very hard-to-find bugs if we drop it.
Vitaly -- I'll point out that support for running Linux guests on any Hyper-V older than WS2016/Win10 has been removed from the upstream code tree as of 7.4. The removal is impactful enough that 7.4 and later kernels won't boot in a guest on these older Hyper-V versions. Hopefully whatever bug you encountered is no longer present in WS2016 and later. But even if the looping is removed, the code could still check and warn on a hypercall completion with no error but not all reps completed. Then at least we'd know there is a problem.
Michael
RE: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
Quoted 30 lines · Michael Kelley
From: Vitaly Kuznetsov <[email protected]> Sent: Wednesday, November 18, 2026 6:02 AM
Quoted 19 lines
Quoted 1 line · Mukesh R
On 11/17/26 09:41, Michael Kelley wrote:
...
Quoted 11 lines
Confirming, that it is same behavior all the way back to 2016.
Quoted 6 lines
Sounds good. FWIW, old TLFS version 2.0A, which is specifically titled to cover Windows Server 2008 R2, has the paragraph about hypercall continuation. So that functionality was evidently present very early on. It is still curious that Vitaly found it necessary to include the looping behavior in the Linux guest code.
Maybe Vitaly was running into a genuine bug, not sure.
Quoted 9 lines
I'm not sure either :-(
WS2012 was definitely around back then, not sure about 2008. It may also make sense to add at least a debug print in case we find ourselves running on something that old. AFAICS, even the TLFS version 2.0A mentions PV TLB flush hypercalls. If looping is still required for these old versions, we may introduce some very hard-to-find bugs if we drop it.
Quoted 3 lines
Vitaly -- I'll point out that support for running Linux guests on any Hyper-V older than WS2016/Win10 has been removed from the upstream code tree as of 7.4.
Missed that, thanks for pointing out!
Quoted 7 lines
The removal is
impactful enough that 7.4 and later kernels won't boot in a guest on these older Hyper-V versions. Hopefully whatever bug you encountered is no longer present in WS2016 and later. But even if the looping is removed, the code could still check and warn on a hypercall completion with no error but not all reps completed. Then at least we'd know there is a problem.
Yes, WARN[_ON_ONCE]() should be sufficient.
Signature · 1 line
Vitaly
Re: [PATCH V1 2/2] Drivers: hv: Fix bugs and issues around repeat hypercall
In reply to Michael KelleyQuoted 35 lines · Michael Kelley
From: Mukesh R <[email protected]> Sent: Monday, November 16, 2026 6:29 PM
Quoted 13 lines · Michael Kelley
Quoted 11 lines
From: Mukesh R <[email protected]> Sent: Friday, November 13, 2026 6:53 PM
Quoted 10 lines
There are few issues/bugs around repeat hypercall:
o If the hypercall fails, it could still have processed some entries. Fix places where it assumes none of the entries were processed and will try to undo entries already processed or print incorrect completions.o Hypercall returns back with timeouts only for HW confidential VMs. In that case it should be retried instead of returning back. Returning back results in error conditions and take down of VMs in most cases.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
o For non confidential VMs, the hypercall could be streamlined as it will never return back with timeouts.
o Rename
HV_STATUS_TIME_OUTtoHV_STATUS_TIMEOUTto match hypervisor public header file. There is no reason for it to be different.o Minor formatting fix for checkpatch. Error printing missing in couple places when rep hypercall fails.
In my view, the changes to the call sites of
hv_do_rep_hypercall()should be in a separate patch. These are just bugs in how the undo is handled in case of an error, and the fixes presumably want to backported.Handling a TIMEOUT return should also be a separate patch. This is also a bug and the fix should be backported. The TIMEOUT is generated under a specific set of circumstances -- it's not a generic error. The patch should describe how and why those circumstances arise in the overall context of hypercall continuation. There's not any such documentation elsewhere, so the info should be captured in the patch commit message or code comments for future reference.
Quoted 4 lines
Finally, if the looping behavior in a normal VM is to be removed, that would be a third patch. Given that this changes long-standing behavior, some explanation of "why" is needed in the commit message. I don't think this change should be backported.
Well, turns out the timeouts need to be handled for all hypercalls, not just repeat hypercalls.... so i think it is best to just split the series. I'll send separate patches now. JFYI.
Thanks,
-Mukesh
Quoted 307 lines
Quoted 79 lines
Quoted 79 lines
Quoted 2 lines
Signed-off-by Mukesh R <[email protected]>[snip]
include/asm-generic/mshyperv.h line 32
Quoted 10 lines
include/asm-generic/mshyperv.h +2
-0@@ -28,6 +28,9 @@#define VTPM_BASE_ADDRESS 0xfed40000 +#define HV_REP_COUNT_MAX \ + (HV_HYPERCALL_REP_COMP_MASK >> HV_HYPERCALL_REP_COMP_OFFSET)Sashiko noted that this new #define is never used anywhere.
Quoted 31 lines
include/asm-generic/mshyperv.h +9
-8+enum hv_partition_type {HV_PARTITION_TYPE_GUEST ,HV_PARTITION_TYPE_ROOT ,@@ -128,6 +131,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. */ @@ -135,45 +143,63 @@ static inline unsigned int hv_repcomp(u64 status)HV_HYPERCALL_REP_COMP_OFFSET ; } -/* -* Rep hypercalls. Callers of this functions are supposed to ensure that -* rep_count, varhead_size, and rep_start comply with Hyper-V hypercall -* definition. -*/ -static inline u64 hv_do_rep_hypercall_ex (u16 code,u16 rep_count, -u16 varhead_size,u16 rep_start, -void *input,void *output) +static inline u64 hv_do_rep_hypercall_reg (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output)include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 155
Quoted 10 lines
include/asm-generic/mshyperv.h +0
-3{u64 control = code; -u64 status; -u16 rep_comp; control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; - do {Dropped the looping behavior here may be much more significant than just "streamlining" as described in the commit message. The looping behavior was included when rep hypercalls were originally added in commit
806c89273babin kernel 4.14, long before any SNP/TDX/CCA functionality was added. So it was present for "normal" VMs doing hypercalls on x86/x64 the normal way. The looping ensured that rep hypercalls could not return with no errors but with the completed rep count less than the requested rep count.However, in my mind it *is* debatable whether that scenario could ever occur. The Hyper-V TLFS describes "Hypercall Continuation" [1] as essentially transparent to the caller. If a rep hypercall stops processing reps because of hitting the 50 microsecond time limit, it should restart and continue with the next set of reps without the caller having to loop. Commit
806c89273babmentions hypercall continuation, but does not describe why the explicit looping was added or how that aligns with the TLFS statements. Vitaly -- it was a long time ago, but do you have any recollection?Quoted 6 lines
Quoted 3 lines
Have you gotten an explicit assurance from the Hyper-V team that the transparent hypercall continuation really does work, and the explicit looping has all along been unnecessary? This
Correct, hence the patch. Waiting on confirmation that it is same behavior all the way back to server 2016.
Sounds good. FWIW, old TLFS version 2.0A, which is specifically titled to cover Windows Server 2008 R2, has the paragraph about hypercall continuation. So that functionality was evidently present very early on. It is still curious that Vitaly found it necessary to include the looping behavior in the Linux guest code.
Quoted 11 lines
Basically, the hypervisor will just adjust the RIP back to the vmcall upon internal timeouts, and reschedule the vcpu. But for hw confidential vms, ie, tdx/snp/cca, it cannot adjust the RIP, hence return back to the guest with TIMEOUT, so cvm has to do in a loop.
Now, for paravisor support, I was just told by a paravisor team dev, that it will never return with TIMEOUTs. My view on that set aside, I guess for paravisor we'll just call the
_regfunction then. Thanks for bringing attention to that...Yes, that would be what I expected.
Quoted 10 lines
Quoted 8 lines
assurance would be needed back to and including Windows Server 2016, which is now the oldest Hyper-V version supported by Linux. Also note that the rep hypercall code is architecture independent, so the transparent hypercall continuation would also need to work on arm64 when the hypercall is invoked via the standard path of using the HVC instruction (the TLFS doesn't cover arm64, so this is unclear).
Both x86 and arm are same for this issue.
OK, good.
Quoted 115 lines
Quoted 8 lines
If you have such assurance, then I'm good with the removal of the looping. But please make the removal a separate patch, with sufficient discussion in the commit message to justify why it's OK to do the removal (including that evidently the original commit misinterpreted how continuation worked). And instead of looping, perhaps do a sanity check that the completed rep count matches the requested rep count.
Not sure if it's worth the overhead of sanity check, looks like callers are checking completed and loop anyways... But I would be OK with
BUG(), since it would be a bug for that to happen. FWIW, it's been running internally with couple months, and haven't seen any issues.Quoted 5 lines
Without such assurance, I think the looping needs to stay. Callers are not prepared for a rep hypercall to return with no errors, but with the completed rep count less than the requested rep count.
[1] https://learn.microsoft.com/en-us/virtualization/hyper-v-on-
windows/tlfs/hypercall-interface#hypercall-continuation
Quoted 82 lines
Quoted 22 lines
include/asm-generic/mshyperv.h +17
-1+return hv_do_hypercall (control, input, output); +} + +static inline u64 hv_do_rep_hypercall_cvm (u16 code,u16 rep_count, +u16 varhead_size,u16 rep_start, +void *input,void *output) +{ +u64 status, control = code; +u16 rep_comp =0 ; + + control |= (u64 )varhead_size <<HV_HYPERCALL_VARHEAD_OFFSET ; + control |= (u64 )rep_count <<HV_HYPERCALL_REP_COMP_OFFSET ; + control |= (u64 )rep_start <<HV_HYPERCALL_REP_START_OFFSET ; + +for (;;) { status =hv_do_hypercall (control, input, output); -if (!hv_result_success (status)) + +if (!hv_result_timeout (status))return status;include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 183
Quoted 10 lines
include/asm-generic/mshyperv.h +1
-2rep_comp =hv_repcomp (status); - control &= ~HV_HYPERCALL_REP_START_MASK ; control |= (u64 )rep_comp <<HV_HYPERCALL_REP_START_OFFSET ;touch_nmi_watchdog (); - }while (rep_comp < rep_count); + }return status;And Sashiko noted that this "return status" is dead code. The only way to exit this function is via the "return status" up in the loop.
Quoted 7 lines
include/asm-generic/mshyperv.h +4
-1} -/* For the typical case where rep_start is 0 */ +/* +* Rep hypercalls for HW confidential VMs return back when timeouts occur, +* whereas for regular VMs they return only on success or failure. In case +* of successful completion, entire rep_count is done. But a hypercall could include/asm-generic/mshyperv.h static inline unsigned int hv_repcomp(u64 status) · line 197
Quoted 10 lines
include/asm-generic/mshyperv.h +5
-2+* fail with partial completions, hence callers must always check that +* in case of failure. Lastly, a rep_count value of 0 is allowed for some +* hypercalls. +*/ static inline u64 hv_do_rep_hypercall (u16 code,u16 rep_count,u16 varhead_size,void *input,void *output) { -return hv_do_rep_hypercall_ex (code, rep_count, varhead_size,0 , - input, output); +if (hv_isolation_type_hw ())I don't think this is the right test, so let me explain. You don't describe the context for when
HV_STATUS_TIMEOUTmay be returned, and there's no TLFS documentation of this status, but I'm assuming the context is hypercall continuation. This isn't the typical "something isn't responding" timeout. I suspect it means the hypervisor hit the 50 us limit, and returned to the guest VM with some reps still to be done. In a normal VM when a hypercall is initiated by the hypercall page on x86/x64 or the HVC instruction on arm64, the transparent hypercall continuation mechanism makes a TIMEOUT status unnecessary. But with SNP, TDX, and CCA, hypercall invocation may be piggybacked on other transports, and transparent continuation isn't available. So Hyper-V returnsHV_STATUS_TIMEOUTwith the expectation that the invoking code will loop to do the remaining reps.The key factor is whether the hypercall is piggybacked on other transports. The tests in
hv_isolation_type_hw()don't capture that. For example, if a paravisor is present,hv_isolation_type_snp()will return "true" even though hypercalls are invoked via the standard x86/x64 hypercall page. The hypercalls trap to the paravisor. If the paravisor is functioning properly, it should do transparent hypercall continuation just like the hypervisor would. But this is something to be confirmed with the hypervisor and paravisor teams.Piggybacking is done only when no paravisor is present, so the
hv_do_rep_hypercall_cvm()path should be taken only in that circumstance. Your current code works, but it goes down the more expensivehv_do_rep_hypercall_cvm()path when it doesn't need to. (I'm not completely clear on how arm64 works with and without a paravisor, so my statement applies only to SNP and TDX. Need to sort out the CCA case with Kameron.)Quoted 10 lines
Given the non-trivial complexity in sorting all this out, I wonder if distinguishing the
_cvmand_regcases is really worth it. The current code for the_regcase is admittedly inefficient because it does work for the looping case before likely deciding that no looping is needed. But that could easily be fixed. When no looping is necessary, which is the vast majority of cases, the looping code could end up being only a couple of instructions slower than a separately coded non-looping version (I did some builds and looked at the generated code). Since there's overhead in deciding which to use, there may not be any performance win from having both versions.Quoted 3 lines
Well, to me it's correctness issue also. If the ABI says that it will not return with TIMEOUTs for non-cvms, then adding loop just masks it instead of opening a bug report against the hypervisor.
I see your point, but would not call it a "correctness issue". A "correctness issue" is a case where the result is wrong, and there's no wrong result here. I've definitely fixed edge-case issues that might never occur in practice just in the interest of overall "correctness", but that's not this case.
Your point opens a different can-of-worms. The threat model for Linux guests on Hyper-V has historically been that the guest trusts the Hyper-V host to do the right thing. A buggy or malicious host can arbitrarily read or write guest memory, so there's really not much choice but for the guest to trust the host (of course, in a CoCo VM the threat model is different). As such, Linux code has never tried to fully validate what it gets back from Hyper-V. Various ad hoc validations have been added when it seemed helpful to do so, but it's not a priority or a directive. Doing such a validation here is not objectionable, but I wouldn't add a significant complexity to do so.
FWIW, here's pseudo-code for an alternate approach that doesn't require splitting into separate
_regand_cvmversions:static inline u64 hv_do_rep_hypercall_ex(u16 code, u16
rep_count,
u16varhead_size, u16rep_start, void *input, void *output){ u64 status, control = code; u16 rep_comp = 0; control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; control |= (u64)rep_start << HV_HYPERCALL_REP_START_OFFSET; for (;;) { status = hv_do_hypercall(control, input, output); if (!hv_result_timeout(status)) /* If result is success, could validate completed reps == rep_count - rep_start */ return status; if (! <hypercall is over alt transport> ) { /* Should not get TIMEOUT with normal calling seq */ WARN(); return HV_STATUS_INVALID_PARAMETER; /* or some such */ } rep_comp = hv_repcomp(status); control &= ~HV_HYPERCALL_REP_START_MASK; control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; } }In almost all cases, the result is not TIMEOUT, so the code returns immediately with no extra overhead except to check for TIMEOUT. If it *is* TIMEOUT, then go to the trouble to figure out whether TIMEOUT is valid. If the VM is using the normal hypercall invocation mechanism, flag that as a hypervisor bug and don't loop.
Quoted 8 lines
Hope that makes sense.
On a side note, to solicit your thoughts, i am not sure i like the
touch_nmi_watchdog()... it appears the the WD is triggered in seconds, and if a hypercall is that long,schedule()really should be done in between. IOW, remove the touch, instead any very long running hypercalls should callschedule(). Of course, that would be a different patch.I agree. I would be fine with removing the
touch_nmi_watchdog().Michael