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.
Mukesh R (2):
Drivers: hv: Add new function hv_isolation_hw_cvm()
Drivers: hv: Fix bugs and issues around repeat hypercall
arch/arm64/include/asm/mshyperv.h | 1 + arch/x86/hyperv/ivm.c | 7 +++- arch/x86/include/asm/mshyperv.h | 1 + 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 | 59 +++++++++++++++++++++---------- include/hyperv/hvgdk_mini.h | 2 +- 9 files changed, 87 insertions(+), 40 deletions(-)
Signature · 1 line
2.51.2.vfs.0.1
PATCH 1/2 Drivers: hv: Add new function hv_isolation_hw_cvm()
Add a new function hv_isolation_hw_cvm() for convenient check if a VM is running in HW confidential mode, ie, TDX or SNP.
--- 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_hw_cvm (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_hw_cvm ()) { 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_hw_cvm (void ) +{ +return false; +} +EXPORT_SYMBOL_GPL (hv_isolation_hw_cvm); +void __weak hv_setup_vmbus_handler (void (*handler)(void )) { }
include/asm-generic/mshyperv.h +7 -0
@@ -87,6 +87,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_hw_cvm (void ); +#else +static inline bool hv_isolation_hw_cvm (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
RE: [PATCH V0 1/2] Drivers: hv: Add new function hv_isolation_hw_cvm()
From: Mukesh R <[email protected]> Sent: Tuesday, November 10, 2026 6:21 PM
Quoted 29 lines
Add a new function
hv_isolation_hw_cvm()for convenient check if a VM is running in HW confidential mode, ie, TDX or SNP.Signed-off-by Mukesh R <[email protected]>--- 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_hw_cvm (void ) +{ +return hv_isolation_type_snp () ||hv_isolation_type_tdx (); +}drivers/hv/hv_common.c +0
-0
drivers/hv/hv_common.c int hv_common_cpu_init(unsigned int cpu) · line 531
Quoted 6 lines
drivers/hv/hv_common.c +1
-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_hw_cvm ()) {
Any reason not to use hv_is_isolation_supported()? Your new function seems conceptually equivalent, though at a detailed level it handles VBS isolation differently from hv_is_isolation_supported(). But I don't think including VBS isolation here is a problem since set_memory_decrypted() is a no-op with VBS isolation.
Also, Kameron Carr has a patch set in flight that collides on exactly this code. See [1] for the patch and my comments.
Michael
Quoted 38 lines
drivers/hv/hv_common.c +6
-0ret =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_hw_cvm (void ) +{ +return false; +} +EXPORT_SYMBOL_GPL (hv_isolation_hw_cvm); +void __weak hv_setup_vmbus_handler (void (*handler)(void )) { }include/asm-generic/mshyperv.h +7
-0@@ -87,6 +87,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_hw_cvm (void ); +#else +static inline bool hv_isolation_hw_cvm (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.
--
2.51.2.vfs.0.1
Re: [PATCH V0 1/2] Drivers: hv: Add new function hv_isolation_hw_cvm()
Quoted 44 lines · Michael Kelley
From: Mukesh R <[email protected]> Sent: Tuesday, November 10, 2026 6:21 PM
Quoted 29 lines
Add a new function
hv_isolation_hw_cvm()for convenient check if a VM is running in HW confidential mode, ie, TDX or SNP.Signed-off-by Mukesh R <[email protected]>--- 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_hw_cvm (void ) +{ +return hv_isolation_type_snp () ||hv_isolation_type_tdx (); +}drivers/hv/hv_common.c +0
-0drivers/hv/hv_common.c int hv_common_cpu_init(unsigned int cpu) · line 531
Quoted 6 lines
drivers/hv/hv_common.c +1
-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_hw_cvm ()) {Any reason not to use
hv_is_isolation_supported()? Your new function seems conceptually equivalent, though at a detailed level it handles VBS isolation differently fromhv_is_isolation_supported(). But I don't think including VBS isolation here is a problem sinceset_memory_decrypted()is a no-op with VBS isolation.
Quoted 4 lines
Also, Kameron Carr has a patch set in flight that collides on exactly this code. See [1] for the patch and my comments.
Michael
Hi Michael,
The issues my patch addresses only apply to hardware isolations, like TDX and SNP, hence I called the function: hv_isolation_hw_cvm()
IOW, hv_is_isolation_supported() cannot be used in my case.
If there is conflict, I guess, one of us will have to rebase depending on who merges first.
Thanks,
-Mukesh
Quoted 41 lines
Quoted 38 lines
drivers/hv/hv_common.c +6
-0ret =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_hw_cvm (void ) +{ +return false; +} +EXPORT_SYMBOL_GPL (hv_isolation_hw_cvm); +void __weak hv_setup_vmbus_handler (void (*handler)(void )) { }include/asm-generic/mshyperv.h +7
-0@@ -87,6 +87,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_hw_cvm (void ); +#else +static inline bool hv_isolation_hw_cvm (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.
--
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.
--- 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 | 59 +++++++++++++++++++++++----------- include/hyperv/hvgdk_mini.h | 2 +- 7 files changed, 73 insertions(+), 38 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
@@ -291,6 +291,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);
@@ -298,11 +299,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) {
@@ -408,20 +411,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 +41 -18
99 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