mshv: add SEV-SNP support for MSHV root partitions
Subsystems (5)
v2 cover mshv: add SEV-SNP support for MSHV root partitions
This series adds support for creating and managing AMD SEV-SNP confidential virtual machines through the Microsoft Hypervisor root partition driver.
The series adds the required Hyper-V ABI and MSHV UAPI definitions, hypercall helpers, partition ioctls, capability discovery, processor feature handling, memory teardown, and crash-dump support.
It also adds the x86 EFI HvLoader handoff required to boot Linux as an MSHV root partition and corrects SynIC register ownership when the root partition is itself running nested.
The patches are based on Linux v7.2-rc5.
Testing:
- Built the complete 13-patch series on x86_64.
- Verified the installed MSHV UAPI compiles from userspace.
- Booted Linux as an MSHV root partition with SEV-SNP available.
- Passed Cloud Hypervisor's common_cvm::test_focal_simple_launch.
- Created and booted a four-vCPU SEV-SNP guest.
- Verified SNP panic-time page unlock for kdump.
Functional testing on the development host used an additional local nested-VMBus interrupt workaround required by that nested environment. That workaround is not part of this series.
Changes since v1:
- Make the isolated-import UAPI self-contained and translate it to the internal Hyper-V ABI type in the driver.
- Preserve the asynchronous import rep count across CALL_PENDING completion and reject invalid zero/oversized progress.
- Validate physical contiguity for large-page host-access requests, use kvcalloc() for userspace-sized page arrays, and restore IRQ state on defensive error paths.
- Keep encrypted memory regions registered until ordered partition teardown and retry transient SNP state-destruction failures.
- Make SEV-SNP capability queries informational so unsupported platforms retain ordinary MSHV functionality.
- Fully initialize partition creation properties on all architectures, stabilize the extended create-partition userspace fetch, and fix the VERW feature-field typo.
- Serialize panic-time memory-region traversal.
- Allocate the augmented EFI command line from EFI loader memory, add command-line bounds checking, correctly append setup_data, compact legacy resource entries, preserve partially parsed range state, and release temporary EFI allocations.
- Limit the EFI HvLoader implementation to x86_64; the unhooked ARM64 implementation from v1 was removed.
Wei Hu (4):
hyperv: fix hv_input_get_system_property layout for SNP status
mshv: unlock SNP pages on panic for crashdump collection
hyperv: add MSHV Dom0 root-partition boot enablement (EFI HvLoader)
mshv: set up own SynIC registers on a nested root partition
Wei Liu (9):
mshv: add SEV-SNP UAPI definitions
mshv: add SEV-SNP PSP request hypercall
mshv: add SEV-SNP isolated page hypercalls
mshv: wire SEV-SNP partition ioctls
mshv: detect and report SEV-SNP support at init
mshv: default to safe partition CPU features
mshv: accept partial CPU feature banks
mshv: define full processor and xsave feature masks
mshv: unmap SNP memory before state teardown
MAINTAINERS | 2 + arch/x86/hyperv/hv_init.c | 3 + arch/x86/include/asm/mshyperv.h | 1 + arch/x86/include/uapi/asm/setup_data.h | 3 +- arch/x86/kernel/cpu/mshyperv.c | 81 +- drivers/firmware/efi/libstub/Makefile | 5 +- .../firmware/efi/libstub/efi-mshv-common.c | 148 ++++ drivers/firmware/efi/libstub/efi-mshv.h | 109 +++ drivers/firmware/efi/libstub/x86-efi-mshv.c | 217 ++++++ drivers/firmware/efi/libstub/x86-stub.c | 18 +- drivers/hv/hv_common.c | 82 ++ drivers/hv/mshv_regions.c | 2 +- drivers/hv/mshv_root.h | 28 + drivers/hv/mshv_root_hv_call.c | 180 ++++- drivers/hv/mshv_root_main.c | 729 +++++++++++++++++- drivers/hv/mshv_synic.c | 16 +- include/asm-generic/mshyperv.h | 7 + include/hyperv/hvgdk_mini.h | 31 + include/hyperv/hvhdk.h | 124 ++- include/hyperv/hvhdk_mini.h | 56 +- include/linux/efi.h | 1 + include/uapi/linux/mshv.h | 104 ++- 22 files changed, 1914 insertions(+), 33 deletions(-) create mode 100644 drivers/firmware/efi/libstub/efi-mshv-common.c create mode 100644 drivers/firmware/efi/libstub/efi-mshv.h create mode 100644 drivers/firmware/efi/libstub/x86-efi-mshv.c
base-commit: f5098b6bae761e346ebcd9da7f95622c04733cff
Signature · 1 line
2.43.0
PATCH v2 1/13 mshv: add SEV-SNP UAPI definitions
From: Wei Liu <[email protected]>
Add the MSHV UAPI and Hyper-V ABI definitions needed by the SEV-SNP partition ioctls: the SNP isolation type, GPA host-access and isolated-page request structures, PSP request structures, and the SEV control register definitions.
Also add the SNP launch-data definitions (guest policy, ID block, ID auth info and launch-finish data) that form the isolated-import completion parameters, along with the SNP support-status enumeration.
--- include/hyperv/hvgdk_mini.h | 19 +++++++ include/hyperv/hvhdk.h | 45 ++++++++++++++++ include/hyperv/hvhdk_mini.h | 53 +++++++++++++++++++ include/uapi/linux/mshv.h | 101 ++++++++++++++++++++++++++++++++++++ 4 files changed, 218 insertions(+)
include/hyperv/hvgdk_mini.h +19 -0
@@ -497,6 +497,9 @@ union hv_vp_assist_msr_contents { /* HV_REGISTER_VP_ASSIST_PAGE */#define HVCALL_UNMAP_VP_STATE_PAGE 0x00e2#define HVCALL_GET_VP_STATE 0x00e3#define HVCALL_SET_VP_STATE 0x00e4 +#define HVCALL_IMPORT_ISOLATED_PAGES 0x00ef +#define HVCALL_COMPLETE_ISOLATED_IMPORT 0x00f1 +#define HVCALL_ISSUE_SNP_PSP_GUEST_REQUEST 0x00f2#define HVCALL_GET_VP_CPUID_VALUES 0x00f4#define HVCALL_GET_PARTITION_PROPERTY_EX 0x0101#define HVCALL_MMIO_READ 0x0106
@@ -1065,6 +1068,9 @@ enum hv_register_name { HV_REGISTER_VSM_PARTITION_CONFIG = 0x000D0007,#if defined(CONFIG_X86 ) +/* AMD SEV-SNP configuration register */ +HV_X64_REGISTER_SEV_CONTROL =0x00090040 , +/* X64 Debug Registers */ HV_X64_REGISTER_DR0 =0x00050000 , HV_X64_REGISTER_DR1 = 0x00050001,
@@ -1267,6 +1273,18 @@ union hv_x64_pending_interruption_register { } __packed; }; +#ifdef CONFIG_X86 +#define HV_SUPPORTS_SEV_SNP_GUESTS +union hv_x64_register_sev_control { +u64 as_uint64; +struct { +u64 enable_encrypted_state :1 ; +u64 reserved_z :11 ; +u64 vmsa_gpa_page_number :52 ; + } __packed; +}; +#endif +union hv_register_value {struct hv_u128 reg128;u64 reg64;
@@ -1286,6 +1304,7 @@ union hv_register_value {#ifdef CONFIG_X86 union hv_x64_interrupt_state_register interrupt_state;union hv_x64_pending_interruption_register pending_interruption; +union hv_x64_register_sev_control sev_control;#endif union hv_arm64_pending_synthetic_exception_event pending_synthetic_exception_event; };
include/hyperv/hvhdk.h +45 -0
49 lines not on this page · raw message
include/hyperv/hvhdk_mini.h +53 -0
65 lines not on this page · raw message
include/uapi/linux/mshv.h +101 -0
119 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 01/13] mshv: add SEV-SNP UAPI definitions
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] The UAPI header uses C bitfields in `union mshv_snp_guest_policy`, violating Linux UAPI design…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] The UAPI header uses C bitfields in union mshv_snp_guest_policy, violating Linux UAPI design guidelines and risking ABI instability.
- [High] Massively oversized ioctl argument passed by value (3.3 KB), risking kernel stack overflow.
- [High] Structures embed flexible array members (guest_pfns[]) but are passed by value in _IOW macros, concealing the true payload size from audit tools.
- [Medium] UAPI structs use __attribute__((packed)) without natural alignment padding, resulting in unaligned boundaries.
- [Medium] Missing page_size field in the UAPI struct mshv_import_isolated_pages.
--
--- Patch [1]: [PATCH v2 01/13] mshv: add SEV-SNP UAPI definitions --- commit c9fecd1e92fbda950177d18a68754c3dabf290f1
Author: Wei Liu <[email protected]>
mshv: add SEV-SNP UAPI definitions
Add the MSHV UAPI and Hyper-V ABI definitions needed by the SEV-SNP partition ioctls: the SNP isolation type, GPA host-access and isolated-page request structures, PSP request structures, and the SEV control register definitions.
Also add the SNP launch-data definitions (guest policy, ID block, ID auth info and launch-finish data) that form the isolated-import completion parameters, along with the SNP support-status enumeration.
Quoted 4 lines
include/hyperv/hvhdk.h +0
-0
[ ... ]
Quoted 25 lines
include/hyperv/hvhdk.h +21
-0@@ -953,4 +953,49 @@ struct hv_input_modify_sparse_spa_page_host_access {#define HV_MODIFY_SPA_PAGE_HOST_ACCESS_LARGE_PAGE 0x4#define HV_MODIFY_SPA_PAGE_HOST_ACCESS_HUGE_PAGE 0x8 +enum hv_isolated_page_type { +HV_ISOLATED_PAGE_TYPE_NORMAL , +HV_ISOLATED_PAGE_TYPE_VMSA , +HV_ISOLATED_PAGE_TYPE_ZERO , +HV_ISOLATED_PAGE_TYPE_UNMEASURED , +HV_ISOLATED_PAGE_TYPE_SECRETS , +HV_ISOLATED_PAGE_TYPE_CPUID , +HV_ISOLATED_PAGE_TYPE_COUNT +}; + +enum hv_isolated_page_size { +HV_ISOLATED_PAGE_SIZE_4KB , +HV_ISOLATED_PAGE_SIZE_2MB +}; + +struct hv_input_import_isolated_pages { +u64 partition_id; +u32 page_type; +u32 page_size; +u64 page_number[]; +} __packed;
[ ... ]
Quoted 4 lines
include/uapi/linux/mshv.h +0
-0
[ ... ]
Quoted 6 lines
include/uapi/linux/mshv.h +6
-0+struct mshv_modify_gpa_host_access { +__u8 flags; +__u8 rsvd[7 ]; +__u64 page_count; +__u64 guest_pfns[]; +};
[ ... ]
include/uapi/linux/mshv.h struct mshv_gpap_access_bitmap { · line 256
Quoted 6 lines
include/uapi/linux/mshv.h +6
-0+struct mshv_import_isolated_pages { +__u8 page_type; +__u8 rsvd[7 ]; +__u64 page_count; +__u64 guest_pfns[]; +};
[Severity: Medium]
Will this user-facing structure limit the ability to import 2MB pages?
The underlying kernel hypercall structure hv_input_import_isolated_pages supports both a page_type and a page_size. By shrinking page_type to a __u8 and omitting page_size in mshv_import_isolated_pages, is the ABI locked into a single default page size?
[ ... ]
include/uapi/linux/mshv.h struct mshv_gpap_access_bitmap { · line 279
Quoted 12 lines
include/uapi/linux/mshv.h +12
-0+union mshv_snp_guest_policy { +struct { +__u64 minor_version :8 ; +__u64 major_version :8 ; +__u64 smt_allowed :1 ; +__u64 vmpls_required :1 ; +__u64 migration_agent_allowed :1 ; +__u64 debug_allowed :1 ; +__u64 reserved :44 ; + } __attribute__((packed)); +__u64 as_uint64; +};
[Severity: High]
Could these bitfields introduce ABI instability by exposing compiler-dependent layouts to userspace?
Bitfield memory layout and packing order can vary depending on the compiler, its flags, and the architecture. Might this cause critical security policy flags like debug_allowed to be misinterpreted by the kernel or hypervisor if userspace is compiled differently?
[ ... ]
include/uapi/linux/mshv.h struct mshv_gpap_access_bitmap { · line 307
Quoted 7 lines
include/uapi/linux/mshv.h +7
-0+struct mshv_psp_launch_finish_data { +struct mshv_snp_id_block id_block; +struct mshv_snp_id_auth_info id_auth_info; +__u8 host_data[32 ]; +__u8 id_block_enabled; +__u8 author_key_enabled; +} __attribute__((packed));
[Severity: Medium]
Does using __attribute__((packed)) here create unaligned boundaries?
This strips natural alignment from the UAPI structures, creating arbitrary sizes that are not multiples of their largest member's alignment. Could this force unaligned accesses if pointers are cast or structures are arrayed in userspace code?
[ ... ]
Quoted 7 lines
include/uapi/linux/mshv.h +4
-0+struct mshv_complete_isolated_import { +union mshv_partition_complete_isolated_import_data import_data; +}; +/** * struct mshv_root_hvcall - arguments for MSHV_ROOT_HVCALL * @code: Hypercall code (HVCALL_*)
[ ... ]
include/uapi/linux/mshv.h struct mshv_root_hvcall { · line 354
Quoted 6 lines
include/uapi/linux/mshv.h +2
-0@@ -254,6 +350,11 @@ struct mshv_root_hvcall {#define MSHV_GET_GPAP_ACCESS_BITMAP _IOWR(MSHV_IOCTL, 0x06, struct mshv_gpap_access_bitmap)/* Generic hypercall */ #define MSHV_ROOT_HVCALL _IOWR(MSHV_IOCTL, 0x07, struct mshv_root_hvcall) +#define MSHV_MODIFY_GPA_HOST_ACCESS _IOW(MSHV_IOCTL, 0x09, struct mshv_modify_gpa_host_access) +#define MSHV_IMPORT_ISOLATED_PAGES _IOW(MSHV_IOCTL, 0x0A, struct mshv_import_isolated_pages)
[Severity: High]
Does encoding flexible array structures in _IOW macros conceal the true payload size from audit tools?
The _IOC_SIZE encoded in the resulting ioctl command number will only represent the size of the fixed headers. Standard tools like strace, seccomp, or eBPF that rely on _IOC_SIZE might truncate the memory access to just the header and miss the variable-length guest_pfns arrays.
Should these UAPI structures use a userspace pointer for the variable-length arrays instead?
include/uapi/linux/mshv.h struct mshv_root_hvcall { · line 355
Quoted 1 line
include/uapi/linux/mshv.h +1
-0+#define MSHV_COMPLETE_ISOLATED_IMPORT _IOW(MSHV_IOCTL, 0xF4, struct mshv_complete_isolated_import)
[Severity: High]
Does passing struct mshv_complete_isolated_import by value risk overflowing the kernel stack?
This structure is approximately 3.3 KB in size. Pressure on standard ioctl paths might encourage allocating this buffer on the kernel stack, which could trigger a stack overflow on systems with small stacks.
Would it be better to pass large payloads via a pointer in the ioctl structure instead of directly passing them by value?
Quoted 5 lines
include/uapi/linux/mshv.h +2
-0+#define MSHV_ISSUE_PSP_GUEST_REQUEST _IOW(MSHV_IOCTL, 0xF5, struct mshv_issue_psp_guest_request) +#define MSHV_SEV_SNP_AP_CREATE _IOW(MSHV_IOCTL, 0xF6, struct mshv_sev_snp_ap_create)/* ********************************
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/13 mshv: add SEV-SNP PSP request hypercall
In reply to Wei HuFrom: Wei Liu <[email protected]>
Add a helper for HVCALL_ISSUE_SNP_PSP_GUEST_REQUEST so MSHV can issue asynchronous PSP guest requests on behalf of encrypted partitions.
--- drivers/hv/mshv_root.h | 8 +++++++ drivers/hv/mshv_root_hv_call.c | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)
drivers/hv/mshv_root.h +8 -0
@@ -378,4 +378,12 @@ bool mshv_region_handle_gfn_fault(struct mshv_mem_region *region, u64 gfn);void mshv_region_movable_fini (struct mshv_mem_region *region);bool mshv_region_movable_init (struct mshv_mem_region *region); +#ifdef HV_SUPPORTS_SEV_SNP_GUESTS +int hv_call_issue_psp_guest_request (u64 partition_id,u64 req_pfn, +u64 rsp_pfn, +void (*completion_handler)(void *data, +u64 *status), +void *completion_data); +#endif +#endif /* _MSHV_ROOT_H_ */
drivers/hv/mshv_root_hv_call.c +39 -0
@@ -1009,6 +1009,45 @@ int hv_unmap_stats_page(enum hv_stats_object_type type,return ret; } +#ifdef HV_SUPPORTS_SEV_SNP_GUESTS +int hv_call_issue_psp_guest_request (u64 partition_id,u64 req_pfn, +u64 rsp_pfn, +void (*completion_handler)(void *data, +u64 *status), +void *completion_data) +{ +struct hv_input_issue_psp_guest_request *input; +unsigned long flags; +u64 status; + +if (!completion_handler) { +pr_err ("%s: missing completion handler\n" , __func__); +return -EINVAL ; + } + +local_irq_save (flags); + input = *this_cpu_ptr (hyperv_pcpu_input_arg); +memset (input,0 ,sizeof (*input)); + input->partition_id = partition_id; + input->request_page = req_pfn; + input->response_page = rsp_pfn; + status =hv_do_hypercall (HVCALL_ISSUE_SNP_PSP_GUEST_REQUEST , input, +NULL ); +local_irq_restore (flags); + +if (hv_result (status) ==HV_STATUS_CALL_PENDING ) +completion_handler (completion_data, &status); + +if (!hv_result_success (status)) { +pr_err ("%s: status=%s partition_id=%llu\n" , __func__, +hv_result_to_string (status), partition_id); +return hv_result_to_errno (status); + } + +return 0 ; +} +#endif +int hv_call_modify_spa_host_access (u64 partition_id,struct page **pages,u64 page_struct_count,u32 host_access,u32 flags,u8 acquire)
Signature · 1 line
2.43.0
PATCH v2 3/13 mshv: add SEV-SNP isolated page hypercalls
In reply to Wei HuFrom: Wei Liu <[email protected]>
Add helpers for importing isolated pages and completing isolated imports. These are used by SEV-SNP guest launch flows to transfer isolated page state to the hypervisor.
--- drivers/hv/mshv_root.h | 12 ++++ drivers/hv/mshv_root_hv_call.c | 102 +++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+)
drivers/hv/mshv_root.h +12 -0
@@ -379,6 +379,18 @@ void mshv_region_movable_fini(struct mshv_mem_region *region);bool mshv_region_movable_init (struct mshv_mem_region *region);#ifdef HV_SUPPORTS_SEV_SNP_GUESTS +int hv_call_import_isolated_pages (u64 partition_id,u64 *pages, +u64 num_pages, +enum hv_isolated_page_type page_type, +enum hv_isolated_page_size page_size, +void (*completion_handler)(void *data, +u64 *status), +void *completion_data); +int hv_call_complete_isolated_import (u64 partition_id, +union hv_partition_complete_isolated_import_data *import_data, +void (*completion_handler)(void *data, +u64 *status), +void *completion_data); int hv_call_issue_psp_guest_request(u64 partition_id, u64 req_pfn, u64 rsp_pfn, void (*completion_handler)(void *data,
drivers/hv/mshv_root_hv_call.c +102 -0
114 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 03/13] mshv: add SEV-SNP isolated page hypercalls
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The error log in `hv_call_import_isolated_pages` under-reports the number of successfully imported…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The error log in hv_call_import_isolated_pages under-reports the number of successfully imported pages if an error occurs mid-batch. --
--- Patch [3]: [PATCH v2 03/13] mshv: add SEV-SNP isolated page hypercalls --- commit 6412d57ab824c1079e692800b1ba551aa8b078c8
Author: Wei Liu <[email protected]>
mshv: add SEV-SNP isolated page hypercalls
This commit adds hypercall helpers for importing isolated pages and completing isolated imports to transfer state to the hypervisor. These are used by SEV-SNP guest launch flows.
Quoted 4 lines
drivers/hv/mshv_root_hv_call.c +0
-0
[ ... ]
Quoted 12 lines
drivers/hv/mshv_root_hv_call.c +8
-0@@ -1010,6 +1014,104 @@ int hv_unmap_stats_page(enum hv_stats_object_type type, }#ifdef HV_SUPPORTS_SEV_SNP_GUESTS +int hv_call_import_isolated_pages (u64 partition_id,u64 *pages, +u64 num_pages, +enum hv_isolated_page_type page_type, +enum hv_isolated_page_size page_size, +void (*completion_handler)(void *data, +u64 *status), +void *completion_data) +{
[ ... ]
drivers/hv/mshv_root_hv_call.c int hv_unmap_stats_page(enum hv_stats_object_type type, · line 1067
Quoted 11 lines
drivers/hv/mshv_root_hv_call.c +11
-0+ completed =hv_repcomp (status); +if (hv_result (status) ==HV_STATUS_CALL_PENDING ) { +completion_handler (completion_data, &status); +if (hv_repcomp (status)) + completed =hv_repcomp (status); + } + +if (!hv_result_success (status)) { +pr_err ("%s: completed %llu of %llu, %s\n" , __func__, + num_pages - remaining, num_pages, +hv_result_to_string (status));
[Severity: Low]
Does this error message under-report the number of completed pages?
If a rep hypercall fails after completing some iterations mid-batch, the iterations completed in the current batch are stored in the completed variable via hv_repcomp(status).
Because remaining is only decremented at the end of the loop, calculating num_pages - remaining here appears to omit the pages successfully completed in the current failing batch.
Quoted 2 lines
drivers/hv/mshv_root_hv_call.c +2
-0+return hv_result_to_errno (status); + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/13 mshv: wire SEV-SNP partition ioctls
In reply to Wei HuFrom: Wei Liu <[email protected]>
Wire the SEV-SNP partition ioctls for GPA host-access changes, isolated-page import, PSP guest requests, AP creation, and SNP partition teardown.
The GPA host-access ioctl converts the guest addresses to GFNs before issuing the hypercall. The PSP guest-request path restores host access to the request/response pages if the hypercall fails, so a failed request does not leave guest pages inaccessible to the host.
--- drivers/hv/mshv_root.h | 7 + drivers/hv/mshv_root_hv_call.c | 39 ++- drivers/hv/mshv_root_main.c | 436 +++++++++++++++++++++++++++++++++ include/hyperv/hvgdk_mini.h | 12 + 4 files changed, 493 insertions(+), 1 deletion(-)
drivers/hv/mshv_root.h +7 -0
@@ -12,6 +12,7 @@#include <linux/sched.h> #include <linux/srcu.h> #include <linux/wait.h> +#include <linux/workqueue.h> #include <linux/hashtable.h> #include <linux/dev_printk.h> #include <linux/build_bug.h>
@@ -128,6 +129,7 @@ struct mshv_partition {*/ struct completion async_hypercall;u64 async_hypercall_status; +struct delayed_work destroy_work;spinlock_t pt_irqfds_lock;struct hlist_head pt_irqfds_list;
@@ -378,6 +380,11 @@ bool mshv_region_handle_gfn_fault(struct mshv_mem_region *region, u64 gfn);void mshv_region_movable_fini (struct mshv_mem_region *region);bool mshv_region_movable_init (struct mshv_mem_region *region); +int hv_call_set_partition_property (u64 partition_id,u64 property_code, +u64 property_value, +void (*completion_handler)(void *,u64 *), +void *completion_data); +#ifdef HV_SUPPORTS_SEV_SNP_GUESTS int hv_call_import_isolated_pages(u64 partition_id, u64 *pages, u64 num_pages,
drivers/hv/mshv_root_hv_call.c +38 -1
@@ -1014,6 +1014,41 @@ int hv_unmap_stats_page(enum hv_stats_object_type type, }#ifdef HV_SUPPORTS_SEV_SNP_GUESTS +int hv_call_set_partition_property (u64 partition_id,u64 property_code, +u64 property_value, +void (*completion_handler)(void *,u64 *), +void *completion_data) +{ +u64 status; +unsigned long flags; +struct hv_input_set_partition_property *input; + +if (!completion_handler) { +pr_err ("%s: Missing completion handler\n" , __func__); +return -EINVAL ; + } + +local_irq_save (flags); + input = *this_cpu_ptr (hyperv_pcpu_input_arg); + +memset (input,0 ,sizeof (*input)); + input->partition_id = partition_id; + input->property_code = property_code; + input->property_value = property_value; + status =hv_do_hypercall (HVCALL_SET_PARTITION_PROPERTY , input,NULL ); +local_irq_restore (flags); + +if (unlikely (status ==HV_STATUS_CALL_PENDING )) +completion_handler (completion_data, &status); + +if (!hv_result_success (status)) { +pr_err ("%s: %s\n" , __func__,hv_result_to_string (status)); +return hv_result_to_errno (status); + } + +return 0 ; +} + int hv_call_import_isolated_pages(u64 partition_id, u64 *pages, u64 num_pages, enum hv_isolated_page_type page_type,
@@ -1192,8 +1227,10 @@ int hv_call_modify_spa_host_access(u64 partition_id, struct page **pages,for (i =0 ; i < rep_count; i++) {u64 index = (done + i) << large_shift; -if (index >= page_struct_count) +if (index >= page_struct_count) { +local_irq_restore (irq_flags);return -EINVAL ; + } input_page->spa_page_list[i] =page_to_pfn (pages[index]);
drivers/hv/mshv_root_main.c +436 -0
502 lines not on this page · raw message
include/hyperv/hvgdk_mini.h +12 -0
@@ -975,6 +975,7 @@ enum hv_register_name {HV_REGISTER_EXPLICIT_SUSPEND =0x00000000 ,HV_REGISTER_INTERCEPT_SUSPEND =0x00000001 ,HV_REGISTER_DISPATCH_SUSPEND =0x00000003 , +HV_REGISTER_INTERNAL_ACTIVITY_STATE =0x00000004 ,/* Version - 128-bit result same as CPUID 0x40000002 */ HV_REGISTER_HYPERVISOR_VERSION = 0x00000100,
@@ -1250,6 +1251,16 @@ union hv_arm64_pending_synthetic_exception_event { } __packed; }; +union hv_internal_activity_register { +u64 as_uint64; +struct { +u64 startup_suspend :1 ; +u64 halt_suspend :1 ; +u64 idle_suspend :1 ; +u64 rsvd_z :61 ; + } __packed; +}; +union hv_x64_interrupt_state_register {u64 as_uint64;struct {
@@ -1297,6 +1308,7 @@ union hv_register_value {union hv_explicit_suspend_register explicit_suspend;union hv_intercept_suspend_register intercept_suspend;union hv_dispatch_suspend_register dispatch_suspend; +union hv_internal_activity_register internal_activity;#ifdef CONFIG_ARM64 union hv_arm64_interrupt_state_register interrupt_state;union hv_arm64_pending_interruption_register pending_interruption;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 04/13] mshv: wire SEV-SNP partition ioctls
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
New issues:
- [Critical] Guest memory regions are unpinned and freed back to the host allocator before the hypervisor restores host access, leading to fatal RMP violations.
- [High] Inconsistent handling of the user-provided guest_pfns array between SEV-SNP ioctls, causing correct input to be corrupted.
- [High] Potential CPU stall in hv_call_import_isolated_pages() due to unbounded loop over user-provided page count without cond_resched().
- [High] Unbounded retry loop in destroy_partition() if destroy_snp_partition_state() fails.
- [High] Module unload race with the delayed_work 'destroy_work' causes a Use-After-Free kernel panic.
- [High] hv_call_set_partition_property compares the raw 64-bit status to HV_STATUS_CALL_PENDING without masking, breaking the completion handler.
Pre-existing issues:
- [High] Inverted logic in mshv_init_async_handler()'s completion check permanently bricks the partition on stray completions. --
--- Patch [4]: [PATCH v2 04/13] mshv: wire SEV-SNP partition ioctls --- commit 903e3452cece23d95187a14a4beb770df83a5add
Author: Wei Liu <[email protected]>
mshv: wire SEV-SNP partition ioctls
Wire the SEV-SNP partition ioctls for GPA host-access changes, isolated-page import, PSP guest requests, AP creation, and SNP partition teardown.
The GPA host-access ioctl converts the guest addresses to GFNs before issuing the hypercall. The PSP guest-request path restores host access to the request/response pages if the hypercall fails, so a failed request does not leave guest pages inaccessible to the host.
Quoted 4 lines
drivers/hv/mshv_root_hv_call.c +0
-0
[ ... ]
Quoted 9 lines
drivers/hv/mshv_root_hv_call.c +5
-0@@ -1014,6 +1014,41 @@ int hv_unmap_stats_page(enum hv_stats_object_type type, }#ifdef HV_SUPPORTS_SEV_SNP_GUESTS +int hv_call_set_partition_property (u64 partition_id,u64 property_code, +u64 property_value, +void (*completion_handler)(void *,u64 *), +void *completion_data) +{
[ ... ]
drivers/hv/mshv_root_hv_call.c int hv_unmap_stats_page(enum hv_stats_object_type type, · line 1041
Quoted 4 lines
drivers/hv/mshv_root_hv_call.c +4
-0+ status =hv_do_hypercall (HVCALL_SET_PARTITION_PROPERTY , input,NULL ); +local_irq_restore (flags); + +if (unlikely (status ==HV_STATUS_CALL_PENDING ))
[Severity: High]
Does this code incorrectly compare the raw 64-bit status to HV_STATUS_CALL_PENDING without masking it?
All other hypercalls use the macro hv_result(status) == HV_STATUS_CALL_PENDING to strip diagnostic and reserved bits. If this comparison fails because of upper bits being set, the hypercall completion handler is bypassed, causing the function to fail with an error when it should wait. This would lead the partition destruction into an infinite retry loop.
Quoted 1 line
drivers/hv/mshv_root_hv_call.c +1
-0+completion_handler (completion_data, &status);
[ ... ]
Quoted 4 lines
drivers/hv/mshv_root_main.c +0
-0
[ ... ]
Quoted 1 line
drivers/hv/mshv_root_main.c +0
-0@@ -1625,6 +1668,289 @@ mshv_partition_ioctl_initialize(struct mshv_partition *partition)
[ ... ]
Quoted 4 lines
drivers/hv/mshv_root_main.c +4
-0+static long +mshv_partition_ioctl_modify_gpa_host_access (struct mshv_partition *partition, +void __user *user_args) +{
[ ... ]
drivers/hv/mshv_root_main.c mshv_partition_ioctl_initialize(struct mshv_partition *partition) · line 1783
Quoted 3 lines
drivers/hv/mshv_root_main.c +3
-0+for (i =0 ; i < args.page_count; i++) + gpfns[i] =HVPFN_DOWN (gpfns[i]); +
[Severity: High]
Are we inconsistent in how we handle the user-provided guest_pfns array between SEV-SNP ioctls?
Here, the code applies HVPFN_DOWN() to a field named guest_pfns, treating it as a GPA instead of a PFN. This shifts the PFNs right by 12 bits, corrupting the values.
However, in mshv_partition_ioctl_import_isolated_pages(), the identically named guest_pfns array is passed directly to the hypercall without downshifting. This mismatch breaks the API contract and can lead to memory corruption or hypercall failures when incorrect physical addresses are provided to the hypervisor.
[ ... ]
Quoted 4 lines
drivers/hv/mshv_root_main.c +4
-0+static long +mshv_partition_ioctl_import_isolated_pages (struct mshv_partition *partition, +void __user *user_args) +{
[ ... ]
drivers/hv/mshv_root_main.c
Quoted 1 line
drivers/hv/mshv_root_main.c +1
-0+ ret =mshv_init_async_handler (partition);
[Severity: High]
This is a pre-existing issue, but does mshv_init_async_handler() have inverted logic in its completion check that permanently bricks the partition?
mshv_init_async_handler() has this check:
if (completion_done(&partition->async_hypercall)) {
pt_err(partition, "Cannot issue async hypercall while another one in progress!\n");
return -EPERM;
}completion_done() returns true if a completion is completed (done > 0). The function incorrectly assumes another operation is in progress, when in fact it means the completion is fully resolved. If a spurious completion fires, it permanently denies service for any further asynchronous hypercalls for the partition.
drivers/hv/mshv_root_main.c mshv_partition_ioctl_initialize(struct mshv_partition *partition) · line 1842
Quoted 8 lines
drivers/hv/mshv_root_main.c +8
-0+if (ret) + goto out; + + ret =hv_call_import_isolated_pages (partition->pt_id, pages, + args.page_count, args.page_type, +HV_ISOLATED_PAGE_SIZE_4KB , + mshv_async_hvcall_handler, + partition);
[Severity: High]
Can this lead to a CPU stall and kernel watchdog panic?
Userspace can invoke MSHV_IMPORT_ISOLATED_PAGES with a massive page_count. The while (remaining) loop inside hv_call_import_isolated_pages() will continuously issue synchronous hypercalls based on this unbounded user-supplied page count without ever calling cond_resched(), monopolizing the processor.
[ ... ]
Quoted 1 line
drivers/hv/mshv_root_main.c +0
-0@@ -1782,6 +2191,9 @@ static void destroy_partition(struct mshv_partition *partition)
[ ... ]
drivers/hv/mshv_root_main.c static void destroy_partition(struct mshv_partition *partition) · line 2214
Quoted 9 lines
drivers/hv/mshv_root_main.c +8
-0if (partition->pt_initialized) { +#ifdef HV_SUPPORTS_SEV_SNP_GUESTS +if (mshv_partition_encrypted (partition)) { +hlist_for_each_entry_safe (region, n, + &partition->pt_mem_regions, + hnode) { +hlist_del (®ion->hnode); +mshv_region_put (region); + }
[Severity: Critical]
Does this unpin and free guest memory regions back to the host allocator before the hypervisor restores host access?
The code calls mshv_region_put() to free the memory to the host buddy allocator BEFORE destroy_snp_partition_state() successfully transitions the partition state to INSECURE_DIRTY. If the host reallocates and writes to these pages before the transition completes, it triggers a fatal RMP violation and a Machine Check Exception (MCE).
drivers/hv/mshv_root_main.c static void destroy_partition(struct mshv_partition *partition) · line 2222
Quoted 8 lines
drivers/hv/mshv_root_main.c +8
-0+ + ret =destroy_snp_partition_state (partition); +if (ret) { +pt_err (partition, +"Failed to destroy SNP state: %d; retrying\n" , + ret); +schedule_delayed_work (&partition->destroy_work,HZ ); +return ;
[Severity: High]
Could this error path cause an unbounded infinite retry loop?
If the hypervisor rejects an idempotent state transition during partition destruction, the error path unconditionally schedules a retry without a maximum retry limit. Because it reschedules destroy_partition() entirely, previously successful state transitions will be repeated, potentially failing indefinitely.
[ ... ]
drivers/hv/mshv_root_main.c mshv_ioctl_create_partition(void __user *user_arg, struct device *module_dev) · line 2489
Quoted 6 lines
drivers/hv/mshv_root_main.c +2
-0@@ -2051,6 +2485,8 @@ mshv_ioctl_create_partition(void __user *user_arg, struct device *module_dev)mutex_init (&partition->pt_irq_lock);init_completion (&partition->async_hypercall); +INIT_DELAYED_WORK (&partition->destroy_work, + mshv_partition_destroy_work);
[Severity: High]
Might this delayed work cause a Use-After-Free kernel panic on module unload?
If partition destruction fails and defers via this destroy_work, and userspace closes the file descriptor, the module reference count will reach zero. The module exit routine mshv_parent_partition_exit() fails to iterate over remaining partitions or call cancel_delayed_work_sync(). If an administrator unloads the module before the delayed work fires, the workqueue will execute unmapped text.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v2 5/13 hyperv: fix hv_input_get_system_property layout for SNP status
In reply to Wei HuFrom: Wei Hu <[email protected]>
The processor-feature selector must sit at offset 8 (property_id + u32 reserved), matching the Microsoft Hypervisor ABI. Upstream 7.1's leaner struct placed it at offset 4, so HVCALL_GET_SYSTEM_PROPERTY(SNP_STATUS) failed and mshv aborted (no /dev/mshv). Restore reserved + u64 union.
--- include/hyperv/hvhdk_mini.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/hyperv/hvhdk_mini.h +2 -1
@@ -237,8 +237,9 @@ enum hv_dynamic_processor_feature_property {struct hv_input_get_system_property {u32 property_id;/* enum hv_system_property */ +u32 reserved;union { -u32 as_uint32; +u64 as_uint64;#if IS_ENABLED (CONFIG_X86 )/* enum hv_dynamic_processor_feature_property */ u32 hv_processor_feature;
Signature · 1 line
2.43.0
Wei Liu
maintainerRe: [PATCH v2 05/13] hyperv: fix hv_input_get_system_property layout for SNP status
Quoted 8 lines · Wei Hu
From: Wei Hu <[email protected]>
The processor-feature selector must sit at offset 8 (
property_id+ u32 reserved), matching the Microsoft Hypervisor ABI. Upstream 7.1's leaner struct placed it at offset 4, soHVCALL_GET_SYSTEM_PROPERTY(SNP_STATUS)failed and mshv aborted (no/dev/mshv). Restore reserved + u64 union.Signed-off-by Wei Hu <[email protected]>
This patch exists in upstream as:
commit d6f0248f04a96249660591e47fcf37ba98ac7ea3
Author: Wei Liu <[email protected]> AuthorDate: Tue Jun 30 14:57:54 2026 -0700 Commit: Wei Liu <[email protected]> CommitDate: Wed Jul 22 21:58:21 2026 +0000
mshv: fix hv_input_get_system_property struct
Keep it in sync with the correct definition.
The old code worked by chance.
You can drop this.
Wei
Quoted 22 lines
--- include/hyperv/hvhdk_mini.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/hyperv/hvhdk_mini.h +2
-1@@ -237,8 +237,9 @@ enum hv_dynamic_processor_feature_property {struct hv_input_get_system_property {u32 property_id;/* enum hv_system_property */ +u32 reserved;union { -u32 as_uint32; +u64 as_uint64;#if IS_ENABLED (CONFIG_X86 )/* enum hv_dynamic_processor_feature_property */ u32 hv_processor_feature;Signature · 1 line
2.43.0
PATCH v2 6/13 mshv: detect and report SEV-SNP support at init
In reply to Wei HuFrom: Wei Liu <[email protected]>
Query Hyper-V dynamic processor features during mshv initialization to determine SEV-SNP availability, and report the result: on success log the maximum number of encrypted partitions, otherwise log the decoded SNP status string so misconfigurations are diagnosable. Cloud Hypervisor expects the root driver to perform this capability check before SEV-SNP child partition setup.
--- drivers/hv/mshv_root_main.c | 96 +++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+)
drivers/hv/mshv_root_main.c +96 -0
@@ -2705,6 +2705,100 @@ static int mshv_root_scheduler_cleanup(unsigned int cpu) }/* Must be called after retrieving the scheduler type */ +#if defined(__x86_64__) +static const char *hv_snp_status_to_string (enum hv_snp_status status) +{ +switch (status) { +case HV_SNP_STATUS_NONE : +return "not available" ; +case HV_SNP_STATUS_AVAILABLE : +return "available" ; +case HV_SNP_STATUS_INCOMPATIBLE : +return "incompatible" ; +case HV_SNP_STATUS_PSP_UNAVAILABLE : +return "PSP unavailable" ; +case HV_SNP_STATUS_PSP_INIT_FAILED : +return "PSP init failed" ; +case HV_SNP_STATUS_PSP_BAD_FW_VERSION : +return "bad PSP firmware version" ; +case HV_SNP_STATUS_BAD_CONFIGURATION : +return "bad configuration" ; +case HV_SNP_STATUS_PSP_FW_UPDATE_IN_PROGRESS : +return "PSP firmware update in progress" ; +case HV_SNP_STATUS_PSP_RB_INIT_FAILED : +return "PSP ring buffer init failed" ; +case HV_SNP_STATUS_PSP_PLATFORM_STATUS_FAILED : +return "PSP platform status failed" ; +case HV_SNP_STATUS_PSP_INIT_LATE_FAILED : +return "PSP late init failed" ; +default : +return "unknown" ; + } +} + +static void mshv_print_max_sev_snp_partitions (struct device *dev) +{ +struct hv_input_get_system_property *input; +struct hv_output_get_system_property *output; +unsigned long flags; +u64 status; + +local_irq_save (flags); + input = *this_cpu_ptr (hyperv_pcpu_input_arg); + output = *this_cpu_ptr (hyperv_pcpu_output_arg); + +memset (input,0 ,sizeof (*input)); + input->property_id =HV_DYNAMIC_PROCESSOR_FEATURE_PROPERTY ; + input->hv_processor_feature = +HV_X64_DYNAMIC_PROCESSOR_FEATURE_MAX_ENCRYPTED_PARTITIONS ; + + status =hv_do_hypercall (HVCALL_GET_SYSTEM_PROPERTY , input, output); +local_irq_restore (flags); +if (!hv_result_success (status)) { +dev_warn (dev,"Failed to get max SNP partitions: %s\n" , +hv_result_to_string (status)); +return ; + } + +dev_info (dev,"Maximum supported SEV-SNP partitions are: %llu\n" , + output->hv_processor_feature_value); +} + +static void __init mshv_check_sev_snp_support (struct device *dev) +{ +struct hv_input_get_system_property *input; +struct hv_output_get_system_property *output; +unsigned long flags; +enum hv_snp_status snp_status; +u64 status; + +local_irq_save (flags); + input = *this_cpu_ptr (hyperv_pcpu_input_arg); + output = *this_cpu_ptr (hyperv_pcpu_output_arg); + +memset (input,0 ,sizeof (*input)); + input->property_id =HV_DYNAMIC_PROCESSOR_FEATURE_PROPERTY ; + input->hv_processor_feature =HV_X64_DYNAMIC_PROCESSOR_FEATURE_SNP_STATUS ; + + status =hv_do_hypercall (HVCALL_GET_SYSTEM_PROPERTY , input, output); +local_irq_restore (flags); +if (!hv_result_success (status)) { +dev_warn (dev,"Failed to get SNP support: %s\n" , +hv_result_to_string (status)); +return ; + } + + snp_status = output->hv_processor_feature_value; +dev_info (dev,"SEV-SNP support status: %s (%u)\n" , +hv_snp_status_to_string (snp_status), snp_status); + +if (snp_status ==HV_SNP_STATUS_AVAILABLE ) +mshv_print_max_sev_snp_partitions (dev); +} +#else +static void __init mshv_check_sev_snp_support (struct device *dev) {} +#endif +static int root_scheduler_init (struct device *dev) {
@@ -2808,6 +2902,8 @@ static int __init mshv_parent_partition_init(void)if (ret) goto synic_cleanup; +mshv_check_sev_snp_support (dev); + ret =root_scheduler_init (dev);if (ret) goto synic_cleanup;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 06/13] mshv: detect and report SEV-SNP support at init
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Medium] The shared per-CPU hypercall output buffer is read after local interrupts are restored, creating a race condition that can lead to data corruption.
Pre-existing issues:
- [High] Userspace can access the /dev/mshv character device before the root driver initialization is complete, exposing uninitialized structures. --
--- Patch [6]: [PATCH v2 06/13] mshv: detect and report SEV-SNP support at init --- commit e5cd5a244ce2b3b2547714151c9b94d944d9fd7b
Author: Wei Liu <[email protected]>
mshv: detect and report SEV-SNP support at init
Query Hyper-V dynamic processor features during mshv initialization to determine SEV-SNP availability, and report the result. On success, log the maximum number of encrypted partitions, otherwise log the decoded SNP status string so misconfigurations are diagnosable.
Quoted 4 lines
drivers/hv/mshv_root_main.c +0
-0
[ ... ]
Quoted 5 lines
drivers/hv/mshv_root_main.c +1
-0@@ -2705,6 +2705,100 @@ static int mshv_root_scheduler_cleanup(unsigned int cpu) }/* Must be called after retrieving the scheduler type */ +#if defined(__x86_64__)
[ ... ]
Quoted 16 lines
drivers/hv/mshv_root_main.c +16
-0+static void mshv_print_max_sev_snp_partitions (struct device *dev) +{ +struct hv_input_get_system_property *input; +struct hv_output_get_system_property *output; +unsigned long flags; +u64 status; + +local_irq_save (flags); + input = *this_cpu_ptr (hyperv_pcpu_input_arg); + output = *this_cpu_ptr (hyperv_pcpu_output_arg); + +memset (input,0 ,sizeof (*input)); + input->property_id =HV_DYNAMIC_PROCESSOR_FEATURE_PROPERTY ; + input->hv_processor_feature = +HV_X64_DYNAMIC_PROCESSOR_FEATURE_MAX_ENCRYPTED_PARTITIONS ; +
drivers/hv/mshv_root_main.c static int mshv_root_scheduler_cleanup(unsigned int cpu) · line 2764
Quoted 10 lines
drivers/hv/mshv_root_main.c +10
-0+ status =hv_do_hypercall (HVCALL_GET_SYSTEM_PROPERTY , input, output); +local_irq_restore (flags); +if (!hv_result_success (status)) { +dev_warn (dev,"Failed to get max SNP partitions: %s\n" , +hv_result_to_string (status)); +return ; + } + +dev_info (dev,"Maximum supported SEV-SNP partitions are: %llu\n" , + output->hv_processor_feature_value);
[Severity: Medium]
Could this result in a corrupted value being read from the output buffer?
By calling local_irq_restore(flags) before reading output->hv_processor_feature_value, an interrupt could occur immediately after IRQs are restored. If the interrupt handler executes another hypercall, the per-CPU output buffer (hyperv_pcpu_output_arg) will be overwritten before the value is logged.
Should the value be copied to a local variable before restoring local interrupts?
Quoted 17 lines
drivers/hv/mshv_root_main.c +17
-0+} + +static void __init mshv_check_sev_snp_support (struct device *dev) +{ +struct hv_input_get_system_property *input; +struct hv_output_get_system_property *output; +unsigned long flags; +enum hv_snp_status snp_status; +u64 status; + +local_irq_save (flags); + input = *this_cpu_ptr (hyperv_pcpu_input_arg); + output = *this_cpu_ptr (hyperv_pcpu_output_arg); + +memset (input,0 ,sizeof (*input)); + input->property_id =HV_DYNAMIC_PROCESSOR_FEATURE_PROPERTY ; + input->hv_processor_feature =HV_X64_DYNAMIC_PROCESSOR_FEATURE_SNP_STATUS ;
drivers/hv/mshv_root_main.c static int mshv_root_scheduler_cleanup(unsigned int cpu) · line 2791
Quoted 10 lines
drivers/hv/mshv_root_main.c +10
-0+ + status =hv_do_hypercall (HVCALL_GET_SYSTEM_PROPERTY , input, output); +local_irq_restore (flags); +if (!hv_result_success (status)) { +dev_warn (dev,"Failed to get SNP support: %s\n" , +hv_result_to_string (status)); +return ; + } + + snp_status = output->hv_processor_feature_value;
[Severity: Medium]
If an interrupt overwrites the buffer after local_irq_restore(flags) but before snp_status is assigned, snp_status could read a corrupted value.
Is there a similar race condition here as seen inmshv_print_max_sev_snp_partitions()?
Quoted 6 lines
drivers/hv/mshv_root_main.c +6
-0+dev_info (dev,"SEV-SNP support status: %s (%u)\n" , +hv_snp_status_to_string (snp_status), snp_status); + +if (snp_status ==HV_SNP_STATUS_AVAILABLE ) +mshv_print_max_sev_snp_partitions (dev); +}
[ ... ]
drivers/hv/mshv_root_main.c static int __init mshv_parent_partition_init(void) · line 2906
Quoted 6 lines
drivers/hv/mshv_root_main.c +2
-0@@ -2808,6 +2902,8 @@ static int __init mshv_parent_partition_init(void)if (ret) goto synic_cleanup; +mshv_check_sev_snp_support (dev); +
[Severity: High]
This is a pre-existing issue, but is it safe to expose the character device before the root driver initialization is complete?
Earlier in mshv_parent_partition_init(), misc_register(&mshv_dev) is called, which immediately exposes the device to userspace. A racing userspace thread could open the device and trigger the MSHV_CREATE_PARTITION ioctl before initialization finishes.
The ioctl invokes add_partition(), which attempts to acquire mshv_root.pt_ht_lock. The lock initialization spin_lock_init(&mshv_root.pt_ht_lock) isn't executed until much later in the setup sequence.
Wouldn't this trigger a kernel oops if accessed early?
Quoted 3 lines
drivers/hv/mshv_root_main.c +0
-0ret =root_scheduler_init (dev);if (ret) goto synic_cleanup;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Wei Liu
maintainerRe: [PATCH v2 06/13] mshv: detect and report SEV-SNP support at init
In reply to Wei HuQuoted 3 lines · Wei Hu
From: Wei Liu <[email protected]>
Query Hyper-V dynamic processor features during mshv initialization to
s/Hyper-V/MSHV/
Quoted 5 lines
determine SEV-SNP availability, and report the result: on success log the maximum number of encrypted partitions, otherwise log the decoded SNP status string so misconfigurations are diagnosable. Cloud Hypervisor expects the root driver to perform this capability check before SEV-SNP child partition setup.
No need to reference Cloud Hypervisor here. This applies to all VMMs.
Quoted 6 lines
Signed-off-by Wei Liu <[email protected]>--- drivers/hv/mshv_root_main.c | 96 +++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+)
[...]
Quoted 14 lines
drivers/hv/mshv_root_main.c +14
-0+static void __init mshv_check_sev_snp_support (struct device *dev) +{ +struct hv_input_get_system_property *input; +struct hv_output_get_system_property *output; +unsigned long flags; +enum hv_snp_status snp_status; +u64 status; + +local_irq_save (flags); + input = *this_cpu_ptr (hyperv_pcpu_input_arg); + output = *this_cpu_ptr (hyperv_pcpu_output_arg); + +memset (input,0 ,sizeof (*input)); + input->property_id =HV_DYNAMIC_PROCESSOR_FEATURE_PROPERTY ;
drivers/hv/mshv_root_main.c static int mshv_root_scheduler_cleanup(unsigned int cpu) · line 2790
Quoted 10 lines
drivers/hv/mshv_root_main.c +10
-0+ input->hv_processor_feature =HV_X64_DYNAMIC_PROCESSOR_FEATURE_SNP_STATUS ; + + status =hv_do_hypercall (HVCALL_GET_SYSTEM_PROPERTY , input, output); +local_irq_restore (flags); +if (!hv_result_success (status)) { +dev_warn (dev,"Failed to get SNP support: %s\n" , +hv_result_to_string (status)); +return ; + } +
Our internal testing has shown that running this on an L1VH partition gets an error.
The error should be non-fatal. Please squash my internal patch to this in the next revision.
Wei
PATCH v2 7/13 mshv: default to safe partition CPU features
In reply to Wei HuFrom: Wei Liu <[email protected]>
For legacy create-partition callers, disable all processor and xsave features first, then enable only the default feature set known to be supported. Passing an all-zero disabled feature mask can make SEV-SNP partition creation fail on Hyper-V.
--- drivers/hv/mshv_root_main.c | 99 ++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 11 deletions(-)
drivers/hv/mshv_root_main.c +88 -11
@@ -2373,15 +2373,102 @@ static long mshv_ioctl_process_pt_flags(void __user *user_arg, u64 *pt_flags, args.pt_isolation >= MSHV_PT_ISOLATION_COUNT)return -EINVAL ; +memset (cr_props,0 ,sizeof (*cr_props)); +memset (isol_props,0 ,sizeof (*isol_props)); disabled_procs = &cr_props->disabled_processor_features; disabled_xsave = &cr_props->disabled_processor_xsave_features; +#if IS_ENABLED (CONFIG_X86_64 ) +/* Disable all processor features first. */ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] =U64_MAX ; + +/* Enable default features that are known to be supported. */ + disabled_procs->sse3_support =0 ; + disabled_procs->lahf_sahf_support =0 ; + disabled_procs->ssse3_support =0 ; + disabled_procs->sse4_1_support =0 ; + disabled_procs->sse4_2_support =0 ; + disabled_procs->sse4a_support =0 ; + disabled_procs->xop_support =0 ; + disabled_procs->pop_cnt_support =0 ; + disabled_procs->cmpxchg16b_support =0 ; + disabled_procs->altmovcr8_support =0 ; + disabled_procs->lzcnt_support =0 ; + disabled_procs->mis_align_sse_support =0 ; + disabled_procs->mmx_ext_support =0 ; + disabled_procs->amd3dnow_support =0 ; + disabled_procs->extended_amd3dnow_support =0 ; + disabled_procs->page_1gb_support =0 ; + disabled_procs->aes_support =0 ; + disabled_procs->pclmulqdq_support =0 ; + disabled_procs->pcid_support =0 ; + disabled_procs->fma4_support =0 ; + disabled_procs->f16c_support =0 ; + disabled_procs->rd_rand_support =0 ; + disabled_procs->rd_wr_fs_gs_support =0 ; + disabled_procs->smep_support =0 ; + disabled_procs->enhanced_fast_string_support =0 ; + disabled_procs->bmi1_support =0 ; + disabled_procs->bmi2_support =0 ; + disabled_procs->hle_support_deprecated =0 ; + disabled_procs->rtm_support_deprecated =0 ; + disabled_procs->movbe_support =0 ; + disabled_procs->npiep1_support =0 ; + disabled_procs->dep_x87_fpu_save_support =0 ; + disabled_procs->rd_seed_support =0 ; + disabled_procs->adx_support =0 ; + disabled_procs->intel_prefetch_support =0 ; + disabled_procs->smap_support =0 ; + disabled_procs->hle_support =0 ; + disabled_procs->rtm_support =0 ; + disabled_procs->rdtscp_support =0 ; + disabled_procs->clflushopt_support =0 ; + disabled_procs->invpcid_support =0 ; + disabled_procs->ibrs_support =0 ; + disabled_procs->stibp_support =0 ; + disabled_procs->ibpb_support =0 ; + disabled_procs->unrestricted_guest_support =0 ; + disabled_procs->mdd_support =0 ; + disabled_procs->fast_short_rep_mov_support =0 ; + disabled_procs->l1dcache_flush_support =0 ; + disabled_procs->rdcl_no_support =0 ; + disabled_procs->ibrs_all_support =0 ; + disabled_procs->skip_l1df_support =0 ; + disabled_procs->ssb_no_support =0 ; + disabled_procs->rsb_a_no_support =0 ; + disabled_procs->virt_spec_ctrl_support =0 ; + disabled_procs->rd_pid_support =0 ; + disabled_procs->umip_support =0 ; + disabled_procs->mbs_no_support =0 ; + disabled_procs->mb_clear_support =0 ; + disabled_procs->taa_no_support =0 ; + disabled_procs->tsx_ctrl_support =0 ; + disabled_procs->tsc_invariant_support =0 ; + disabled_procs->rdpru_support =0 ; + disabled_procs->mbec_support =0 ; + disabled_procs->psfd_support =0 ; + disabled_procs->cet_ss_support =0 ; + disabled_procs->cet_ibt_support =0 ; + disabled_procs->vmx_exception_inject_support =0 ; + + disabled_xsave->as_uint64 =U64_MAX ; + disabled_xsave->xsave_support =0 ; + disabled_xsave->xsaveopt_support =0 ; + disabled_xsave->avx_support =0 ; +#endif +/* Check if user provided newer struct with feature fields */ if (args.pt_flags &BIT_ULL (MSHV_PT_BIT_CPU_AND_XSAVE_FEATURES )) { +u64 pt_flags = args.pt_flags; +u64 pt_isolation = args.pt_isolation; +if (copy_from_user (&args, user_arg,sizeof (args)))return -EFAULT ; -/* Re-validate v1 fields after second copy_from_user() */ +if (args.pt_flags != pt_flags || + args.pt_isolation != pt_isolation) +return -EAGAIN ;if ((args.pt_flags & ~MSHV_PT_FLAGS_MASK ) || args.pt_isolation >=MSHV_PT_ISOLATION_COUNT )return -EINVAL ;
@@ -2414,16 +2501,6 @@ static long mshv_ioctl_process_pt_flags(void __user *user_arg, u64 *pt_flags,if (mshv_field_nonzero (args, pt_rsvd2))return -EINVAL ;#endif - }else { -/* -* v1 behavior: try to enable everything. The hypervisor will -* disable features that are not supported. The banks can be -* queried via the get partition property hypercall. -*/ -for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) - disabled_procs->as_uint64[i] =0 ; - - disabled_xsave->as_uint64 =0 ; }/* Only support EXO partitions */
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 07/13] mshv: default to safe partition CPU features
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Legacy V1 API callers requesting nested virtualization will no longer receive the nested…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Legacy V1 API callers requesting nested virtualization will no longer receive the nested virtualization CPU feature, breaking ABI compatibility. --
--- Patch [7]: [PATCH v2 07/13] mshv: default to safe partition CPU features --- commit 01e208d51ef280f67802036c435ff766571437ae
Author: Wei Liu <[email protected]>
mshv: default to safe partition CPU features
For legacy create-partition callers, disable all processor and xsave features first, then enable only the default feature set known to be supported. Passing an all-zero disabled feature mask can make SEV-SNP partition creation fail on Hyper-V.
Quoted 20 lines
drivers/hv/mshv_root_main.c +9
-0@@ -2373,15 +2373,102 @@ static long mshv_ioctl_process_pt_flags(void __user *user_arg, u64 *pt_flags, args.pt_isolation >= MSHV_PT_ISOLATION_COUNT)return -EINVAL ; +memset (cr_props,0 ,sizeof (*cr_props)); +memset (isol_props,0 ,sizeof (*isol_props)); disabled_procs = &cr_props->disabled_processor_features; disabled_xsave = &cr_props->disabled_processor_xsave_features; +#if IS_ENABLED (CONFIG_X86_64 ) +/* Disable all processor features first. */ +for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) + disabled_procs->as_uint64[i] =U64_MAX ; + +/* Enable default features that are known to be supported. */ + disabled_procs->sse3_support =0 ;
[ ... ]
drivers/hv/mshv_root_main.c static long mshv_ioctl_process_pt_flags(void __user *user_arg, u64 *pt_flags, · line 2453
Quoted 3 lines
drivers/hv/mshv_root_main.c +3
-0+ disabled_procs->cet_ss_support =0 ; + disabled_procs->cet_ibt_support =0 ; + disabled_procs->vmx_exception_inject_support =0 ;
[Severity: High]
Does this omission break legacy callers requesting nested virtualization?
Since all features are disabled by default (U64_MAX), and disabled_procs->nested_virt_support is not explicitly set to 0 in this default list, won't nested virtualization remain disabled for the guest CPU even if the legacy caller sets MSHV_PT_BIT_NESTED_VIRTUALIZATION?
Later in mshv_ioctl_process_pt_flags(), the creation capability is enabled if requested:
if (args.pt_flags & BIT(MSHV_PT_BIT_NESTED_VIRTUALIZATION))
*pt_flags |= HV_PARTITION_CREATION_FLAG_NESTED_VIRTUALIZATION_CAPABLE;
However, because the CPU feature itself is left disabled by the new safe defaults list, it appears this will break nested virtualization for V1 ABI callers.
Quoted 6 lines
drivers/hv/mshv_root_main.c +6
-0+ + disabled_xsave->as_uint64 =U64_MAX ; + disabled_xsave->xsave_support =0 ; + disabled_xsave->xsaveopt_support =0 ; + disabled_xsave->avx_support =0 ; +#endif
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v2 8/13 mshv: accept partial CPU feature banks
In reply to Wei HuFrom: Wei Liu <[email protected]>
Allow userspace to provide fewer than MSHV_NUM_CPU_FEATURES_BANKS banks for the create-partition v2 ioctl. Preserve the default disabled-feature mask for omitted banks instead of rejecting the ioctl or overwriting all banks.
--- drivers/hv/mshv_root_main.c | 12 ++---------- include/uapi/linux/mshv.h | 3 ++- 2 files changed, 4 insertions(+), 11 deletions(-)
drivers/hv/mshv_root_main.c +2 -10
@@ -2473,20 +2473,12 @@ static long mshv_ioctl_process_pt_flags(void __user *user_arg, u64 *pt_flags, args.pt_isolation >= MSHV_PT_ISOLATION_COUNT)return -EINVAL ; -if (args.pt_num_cpu_fbanks !=MSHV_NUM_CPU_FEATURES_BANKS || +if (args.pt_num_cpu_fbanks >MSHV_NUM_CPU_FEATURES_BANKS ||mshv_field_nonzero (args, pt_rsvd) ||mshv_field_nonzero (args, pt_rsvd1))return -EINVAL ; -/* -* Note this assumes MSHV_NUM_CPU_FEATURES_BANKS will never -* change and equals HV_PARTITION_PROCESSOR_FEATURES_BANKS -* (i.e. 2). -* -* Further banks (index >= 2) will be modifiable as 'early' -* properties via the set partition property hypercall. -*/ -for (i =0 ; i <HV_PARTITION_PROCESSOR_FEATURES_BANKS ; i++) +for (i =0 ; i < args.pt_num_cpu_fbanks; i++) disabled_procs->as_uint64[i] = args.pt_cpu_fbanks[i];#if IS_ENABLED (CONFIG_X86_64 )
include/uapi/linux/mshv.h +2 -1
@@ -66,7 +66,8 @@ struct mshv_create_partition {* This is extended version of the above initial MSHV_CREATE_PARTITION * ioctl and allows for following additional parameters: * -* @pt_num_cpu_fbanks: Must be set to MSHV_NUM_CPU_FEATURES_BANKS. +* @pt_num_cpu_fbanks: Number of processor feature banks provided. Must not +* exceed MSHV_NUM_CPU_FEATURES_BANKS. * @pt_cpu_fbanks: Disabled processor feature banks array. * @pt_disabled_xsave: Disabled xsave feature bits. *
Signature · 1 line
2.43.0
PATCH v2 9/13 mshv: define full processor and xsave feature masks
In reply to Wei HuFrom: Wei Liu <[email protected]>
Add the missing processor and xsave feature bit definitions and use them when constructing the default disabled-feature mask, so legacy SNP partition creation applies a complete, well-defined feature mask.
--- drivers/hv/mshv_root_main.c | 10 +++++ include/hyperv/hvhdk.h | 79 ++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-)
drivers/hv/mshv_root_main.c +10 -0
@@ -2451,11 +2451,21 @@ static long mshv_ioctl_process_pt_flags(void __user *user_arg, u64 *pt_flags, disabled_procs->cet_ss_support =0 ; disabled_procs->cet_ibt_support =0 ; disabled_procs->vmx_exception_inject_support =0 ; + disabled_procs->fb_clear_support =0 ; + disabled_procs->gds_no_support =0 ; + disabled_procs->bhi_no_support =0 ; + disabled_procs->bhi_dis_support =0 ; + disabled_procs->btc_no_support =0 ; + disabled_procs->mitigation_ctrl_support =0 ; + disabled_procs->rfds_no_support =0 ; + disabled_procs->rfds_clear_support =0 ; disabled_xsave->as_uint64 =U64_MAX ; disabled_xsave->xsave_support =0 ; disabled_xsave->xsaveopt_support =0 ; disabled_xsave->avx_support =0 ; + disabled_xsave->xsave_comp_support =0 ; + disabled_xsave->xsave_supervisor_support =0 ;#endif /* Check if user provided newer struct with feature fields */
include/hyperv/hvhdk.h +77 -2
91 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 10/13 mshv: unmap SNP memory before state teardown
In reply to Wei HuFrom: Wei Liu <[email protected]>
Unmap memory regions before destroying SNP partition state, and only release regions after SNP state is torn down. Dropping regions first attempts to regain host access while the partition is still secure and can leave pages inaccessible.
--- drivers/hv/mshv_regions.c | 2 +- drivers/hv/mshv_root.h | 1 + drivers/hv/mshv_root_main.c | 6 ++---- 3 files changed, 4 insertions(+), 5 deletions(-)
drivers/hv/mshv_regions.c +1 -1
@@ -337,7 +337,7 @@ static int mshv_region_chunk_unmap(struct mshv_mem_region *region, page_count, flags); } -static int mshv_region_unmap (struct mshv_mem_region *region) +int mshv_region_unmap (struct mshv_mem_region *region) { return mshv_region_process_range(region, 0, 0, region->nr_pages,
drivers/hv/mshv_root.h +1 -0
@@ -372,6 +372,7 @@ struct mshv_mem_region *mshv_region_create(u64 guest_pfn, u64 nr_pages,int mshv_region_share (struct mshv_mem_region *region);int mshv_region_unshare (struct mshv_mem_region *region);int mshv_region_map (struct mshv_mem_region *region); +int mshv_region_unmap (struct mshv_mem_region *region);void mshv_region_invalidate (struct mshv_mem_region *region);int mshv_region_pin (struct mshv_mem_region *region);void mshv_region_put (struct mshv_mem_region *region);
drivers/hv/mshv_root_main.c +2 -4
@@ -2208,10 +2208,8 @@ static void destroy_partition(struct mshv_partition *partition)if (mshv_partition_encrypted (partition)) {hlist_for_each_entry_safe (region, n, &partition->pt_mem_regions, - hnode) { -hlist_del (®ion->hnode); -mshv_region_put (region); - } + hnode) +mshv_region_unmap (region); ret =destroy_snp_partition_state (partition);if (ret) {
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 10/13] mshv: unmap SNP memory before state teardown
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [Critical] The destroy_work delayed work is never explicitly canceled during module unload, leading to a potential use-after-free and kernel panic.
- [High] Retrying destroy_partition on failure causes partial unmap aborts, permanently leaving remaining chunks of a memory region mapped.
- [Low] Intentional early return in mshv_region_destroy permanently leaks the mshv_mem_region kernel tracking struct.
Pre-existing issues:
- [High] Direct kfree(vp) without waiting for RCU grace period allows Use-After-Free in concurrent ISRs. --
--- Patch [10]: [PATCH v2 10/13] mshv: unmap SNP memory before state teardown --- commit fe90624ce2dba03d1ad11c9e5fd88e8f71d8c3d0
Author: Wei Liu <[email protected]>
mshv: unmap SNP memory before state teardown
Unmap memory regions before destroying SNP partition state, and only release regions after SNP state is torn down. Dropping regions first attempts to regain host access while the partition is still secure and can leave pages inaccessible.
Quoted 4 lines
drivers/hv/mshv_regions.c +0
-0
[ ... ]
drivers/hv/mshv_regions.c static int mshv_region_chunk_unmap(struct mshv_mem_region *region, · line 342
Quoted 8 lines
drivers/hv/mshv_regions.c +1
-1@@ -337,7 +337,7 @@ static int mshv_region_chunk_unmap(struct mshv_mem_region *region, page_count, flags); } -static int mshv_region_unmap (struct mshv_mem_region *region) +int mshv_region_unmap (struct mshv_mem_region *region) { return mshv_region_process_range(region, 0,
[Severity: Low]
Does this error path leak the kernel tracking structure?
When tearing down an SNP encrypted partition and mshv_region_share() fails, the function returns early to intentionally leave guest memory pinned and prevent host crashes:
drivers/hv/mshv_regions.c:mshv_region_destroy() {
...
if (ret) {
pt_err(partition,
"Failed to regain access to memory... %d\n", ret);
return;
}
}
mshv_region_unmap(region);
mshv_region_invalidate(region);
vfree(region);
}Since the mshv_mem_region structure is already removed from the partition lists and its refcount is zero, could bypassing the vfree(region) call result in permanently leaking the mreg_pages array and tracking structure?
[ ... ]
Quoted 7 lines
drivers/hv/mshv_root_main.c +0
-0@@ -2208,10 +2208,8 @@ static void destroy_partition(struct mshv_partition *partition)if (mshv_partition_encrypted (partition)) { hlist_for_each_entry_safe(region, n,
drivers/hv/mshv_root_main.c static void destroy_partition(struct mshv_partition *partition) · line 2215
Quoted 10 lines
drivers/hv/mshv_root_main.c +2
-4&partition->pt_mem_regions, - hnode) { -hlist_del (®ion->hnode); -mshv_region_put (region); - } + hnode) +mshv_region_unmap (region); ret =destroy_snp_partition_state (partition);if (ret) {
[Severity: High]
Can this retry logic lead to a partial unmap regression when a memory region is processed a second time?
If destroy_snp_partition_state() fails, the teardown is retried via destroy_work:
drivers/hv/mshv_root_main.c:destroy_partition() {
...
ret = destroy_snp_partition_state(partition);
if (ret) {
...
schedule_delayed_work(&partition->destroy_work, HZ);
return;
}
}On the subsequent attempt, mshv_region_unmap() will iterate through the region chunks again starting from offset 0. Since mreg_pages isn't cleared during the initial successful unmap, the hypervisor will likely return an error when attempting to unmap the first chunk again.
mshv_region_process_range() aborts processing immediately on error:
drivers/hv/mshv_regions.c:mshv_region_process_range() {
...
ret = mshv_region_process_chunk(region, flags, page_offset, ...);
if (ret < 0)
return ret;
}Will this completely bypass the unmapping of any remaining chunks that were not reached in the previous pass?
[Severity: High]
Is it safe to schedule a delayed work task that lacks cancelation during module unload?
If destroy_snp_partition_state() fails, the teardown is retried via delayed work. If the partition file descriptor is closed, the module reference count drops to 0, which allows the administrator to unload the mshv module.
Looking at the exit path:
drivers/hv/mshv_root_main.c:mshv_parent_partition_exit() {
...
mshv_irqfd_wq_cleanup();
root_scheduler_deinit();
mshv_synic_exit();
}Since mshv_parent_partition_exit() executes without calling cancel_delayed_work_sync(&partition->destroy_work), can the timer fire after the kernel module is unloaded and execute freed memory?
[Severity: High]
This is a pre-existing issue, but would this direct free cause a use-after-free regression in concurrent interrupt service routines?
In destroy_partition(), vp structures are freed directly:
drivers/hv/mshv_root_main.c:destroy_partition() {
...
kfree(vp);
partition->pt_vp_array[i] = NULL;
...
}Concurrently, the intercept ISR reads these pointers under an RCU lock:
drivers/hv/mshv_synic.c:mshv_intercept_isr() {
...
vp = partition->pt_vp_array[vp_index];
if (unlikely(!vp)) {
...
}
kick_vp(vp);
...
}Since drain_all_vps() is skipped for non-root schedulers, intercept messages can still arrive during partition teardown. If destroy_partition() executes between the time mshv_intercept_isr() reads the vp pointer and dereferences it in kick_vp(), could this result in accessing freed memory because synchronize_rcu() or kfree_rcu() isn't used?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
PATCH v2 11/13 mshv: unlock SNP pages on panic for crashdump collection
In reply to Wei HuFrom: Wei Hu <[email protected]>
Add an SNP panic-path page-unlock feature so a root partition running confidential (SEV-SNP) guests can still collect a Linux root vmcore via kexec after a crash. On panic, guest pages of every encrypted partition are unmapped and shared back to the host so kexec can read them.
Register a panic notifier that walks mshv_root.pt_htable and, for each encrypted partition, unmaps each memory region and marks it shared again. The notifier is only registered when the hypervisor's own crash path is not active (hv_crash_enabled) and only built when CONFIG_CRASH_DUMP is enabled. Declare hv_crash_enabled in asm/mshyperv.h alongside the other MSHV crashdump symbols.
Tested on a SEV-SNP Dom0: the notifier only runs in its intended fallback path, i.e. when the hypervisor's own kdump support is inactive (hv_crash_enabled == false); when it is active the hypervisor stops the CPUs and kexecs before the Linux panic_notifier_list runs. To exercise the ported code that fallback was reproduced on a test kernel (hypervisor crash init disabled so hv_crash_enabled == false). With a confidential guest running, an intentional Dom0 panic showed the notifier fire, find the encrypted partition, and unlock its regions with no errors ("SNP pages are unlocked for panic", no "Unlock snp failed"), after which kdump collected a valid, readable vmcore. The default active-hypervisor path was also confirmed to boot cleanly with the notifier correctly not registered.
--- arch/x86/include/asm/mshyperv.h | 1 + drivers/hv/mshv_root_main.c | 78 +++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+)
arch/x86/include/asm/mshyperv.h +1 -0
@@ -240,6 +240,7 @@ static __always_inline u64 hv_raw_get_msr(unsigned int reg)int hv_apicid_to_vp_index (u32 apic_id);#if IS_ENABLED (CONFIG_MSHV_ROOT ) &&IS_ENABLED (CONFIG_CRASH_DUMP ) +extern bool hv_crash_enabled;void hv_root_crash_init (void );void hv_crash_asm32 (void );void hv_crash_asm64 (void );
drivers/hv/mshv_root_main.c +78 -0
96 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 11/13] mshv: unlock SNP pages on panic for crashdump collection
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Panic notifier fails to set crash_kexec_post_notifiers globally, bypassing memory unmap in default…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Panic notifier fails to set crash_kexec_post_notifiers globally, bypassing memory unmap in default configurations.
- [High] Acquiring spinlock_t via spin_trylock in a panic notifier is structurally unsafe and violates PREEMPT_RT wait-context rules.
- [Medium] Panic path memory unlocking aborts on the first chunk error, failing to execute best-effort vmcore recovery.
- [Low] Missing rcu_read_lock() during RCU hash table traversal.
- [Low] Misleading success message is logged unconditionally even if SNP unlocking completely aborts.
--
--- Patch [11]: [PATCH v2 11/13] mshv: unlock SNP pages on panic for crashdump collection --- commit b8503cbf714b3ffa7932540f4a80cc94da615bdc
Author: Wei Hu <[email protected]>
mshv: unlock SNP pages on panic for crashdump collection
Add an SNP panic-path page-unlock feature so a root partition running confidential (SEV-SNP) guests can still collect a Linux root vmcore via kexec after a crash. On panic, guest pages of every encrypted partition are unmapped and shared back to the host so kexec can read them.
Quoted 8 lines
drivers/hv/mshv_root_main.c +0
-0@@ -2941,6 +2941,81 @@ static int __init mshv_init_vmm_caps(struct device *dev)return 0 ; }
drivers/hv/mshv_root_main.c static int __init mshv_init_vmm_caps(struct device *dev) · line 2953
Quoted 10 lines
drivers/hv/mshv_root_main.c +10
-0+#if defined(CONFIG_X86_64 ) &&IS_ENABLED (CONFIG_CRASH_DUMP ) +static void mshv_panic_unlock_snp (struct mshv_partition *vm) +{ +struct mshv_mem_region *memreg; +int ret; + +if (!spin_trylock (&vm->pt_mem_regions_lock)) { +pt_err (vm,"Cannot lock SNP memory regions during panic\n" ); +return ; + }
[Severity: High]
Is it safe to acquire a spinlock_t via spin_trylock() in a panic notifier?
Calling spin_trylock() on a spinlock_t from a hardirq or NMI context violates PREEMPT_RT wait-context rules.
Additionally, if the CPU that panicked happens to hold this lock, the trylock will fail and bypass the memory unmap entirely, breaking the kexec crashdump.
drivers/hv/mshv_root_main.c static int __init mshv_init_vmm_caps(struct device *dev) · line 2957
Quoted 4 lines
drivers/hv/mshv_root_main.c +4
-0+ +hlist_for_each_entry (memreg, &vm->pt_mem_regions, hnode) { +mshv_region_unmap (memreg); + ret =mshv_region_share (memreg);
[Severity: Medium]
Will this abort prematurely if an error occurs on a single chunk?
The return value of mshv_region_unmap() is ignored, and both underlying functions call mshv_region_process_range() which aborts immediately on the first error.
Could a best-effort approach be used here to continue attempting to unlock remaining regions instead of halting on the first hypercall failure?
Quoted 6 lines
drivers/hv/mshv_root_main.c +6
-0+if (ret) +pt_err (vm,"Unlock snp failed. ret:0x%x gfn:%llx numpfns:%lld\n" , + ret, memreg->start_gfn, memreg->nr_pages); + } + +spin_unlock (&vm->pt_mem_regions_lock);
drivers/hv/mshv_root_main.c static int __init mshv_init_vmm_caps(struct device *dev) · line 2973
Quoted 10 lines
drivers/hv/mshv_root_main.c +10
-0+} + +static int mshv_root_panic_cb (struct notifier_block *this,unsigned long event, +void *ptr) +{ +int i, done =0 ; +struct mshv_partition *pt; +struct device *dev =NULL ; + +hash_for_each_rcu (mshv_root.pt_htable, i, pt, pt_hnode) {
[Severity: Low]
Should this RCU hash table traversal be protected by rcu_read_lock()?
While preemption is stopped in the panic path, RCU subsystem semantics still dictate explicitly delimiting the critical section to prevent static checker warnings.
drivers/hv/mshv_root_main.c static int __init mshv_init_vmm_caps(struct device *dev) · line 2982
Quoted 9 lines
drivers/hv/mshv_root_main.c +9
-0+if (!mshv_partition_encrypted (pt)) +continue ; + + done =1 ; +mshv_panic_unlock_snp (pt); + dev = pt->pt_module_dev; + } +if (done && dev) +dev_info (dev,"SNP pages are unlocked for panic\n" );
[Severity: Low]
Could this emit a misleading success message?
The done flag is set to 1 before mshv_panic_unlock_snp() is called. If the spin_trylock() inside mshv_panic_unlock_snp() fails and returns early, or if an error occurs during unmapping, this will still unconditionally log that SNP pages are unlocked.
Quoted 11 lines
drivers/hv/mshv_root_main.c +11
-0+ +return NOTIFY_DONE ; +} + +static struct notifier_block mshv_root_panic_blk = { + .notifier_call = mshv_root_panic_cb, +}; + +/* +* If mshv devirt setup failed during boot, or the feature itself is not +* available, allow the system to at least collect linux root vmcore. For
drivers/hv/mshv_root_main.c static int __init mshv_init_vmm_caps(struct device *dev) · line 3003
Quoted 10 lines
drivers/hv/mshv_root_main.c +10
-0+* that, snp guest pages must be made readable in the panic path so kexec can +* collect them. +*/ +static void mshv_crashdump_init (void ) +{ +if (hv_crash_enabled) +return ; + +atomic_notifier_chain_register (&panic_notifier_list, + &mshv_root_panic_blk);
[Severity: High]
Does this setup guarantee that the notifier will run before kexec boots?
By default, panic() calls __crash_kexec() before evaluating panic_notifier_list.
Without explicitly setting crash_kexec_post_notifiers to true here, the kdump kernel will boot immediately upon panic on standard configurations, skipping the notifier list and leaving SNP memory locked.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
PATCH v2 12/13 hyperv: add MSHV Dom0 root-partition boot enablement (EFI HvLoader)
In reply to Wei HuFrom: Wei Hu <[email protected]>
Port the EFI HvLoader protocol handshake (efi-mshv stub + SETUP_MSHV) and the hyperv_resvd_new memory reservation so an upstream kernel can boot as a Microsoft Hypervisor (MSHV) root partition. Mainline lacks this Linux-side enablement, so lxhvloader declines to hand off a stock kernel as root.
--- MAINTAINERS | 2 + arch/x86/hyperv/hv_init.c | 3 + arch/x86/include/uapi/asm/setup_data.h | 3 +- arch/x86/kernel/cpu/mshyperv.c | 81 ++++++- drivers/firmware/efi/libstub/Makefile | 5 +- .../firmware/efi/libstub/efi-mshv-common.c | 148 ++++++++++++ drivers/firmware/efi/libstub/efi-mshv.h | 109 +++++++++ drivers/firmware/efi/libstub/x86-efi-mshv.c | 217 ++++++++++++++++++ drivers/firmware/efi/libstub/x86-stub.c | 18 +- drivers/hv/hv_common.c | 82 +++++++ include/asm-generic/mshyperv.h | 7 + include/linux/efi.h | 1 + 12 files changed, 672 insertions(+), 4 deletions(-) create mode 100644 drivers/firmware/efi/libstub/efi-mshv-common.c create mode 100644 drivers/firmware/efi/libstub/efi-mshv.h create mode 100644 drivers/firmware/efi/libstub/x86-efi-mshv.c
MAINTAINERS +2 -0
@@ -12098,6 +12098,8 @@ F: arch/x86/include/asm/mshyperv.h F: arch/x86/include/asm/trace/hyperv.h F: arch/x86/kernel/cpu/mshyperv.c F: drivers/clocksource/hyperv_timer.c +F: drivers/firmware/efi/libstub/*-efi-mshv.c +F: drivers/firmware/efi/libstub/efi-mshv* F: drivers/hid/hid-hyperv.c F: drivers/hv/ F: drivers/input/serio/hyperv-keyboard.c
arch/x86/hyperv/hv_init.c +3 -0
@@ -557,6 +557,9 @@ void __init hyperv_init(void)hv_remap_tsc_clocksource ();hv_sleep_notifiers_register (); + +/* mark ram reserved for hypervisor as owned by hypervisor */ +hv_mark_resources (); }else { hypercall_msr.guest_physical_address =vmalloc_to_pfn (hv_hypercall_pg);wrmsrq (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64);
arch/x86/include/uapi/asm/setup_data.h +2 -1
@@ -14,7 +14,8 @@#define SETUP_IMA 8#define SETUP_RNG_SEED 9#define SETUP_KEXEC_KHO 10 -#define SETUP_ENUM_MAX SETUP_KEXEC_KHO +#define SETUP_MSHV 11 +#define SETUP_ENUM_MAX SETUP_MSHV#define SETUP_INDIRECT (1<<31)#define SETUP_TYPE_MAX (SETUP_ENUM_MAX | SETUP_INDIRECT)
arch/x86/kernel/cpu/mshyperv.c +80 -1
106 lines not on this page · raw message
drivers/firmware/efi/libstub/Makefile +4 -1
@@ -87,7 +87,10 @@ lib-$(CONFIG_X86) += x86-stub.o smbios.o lib-$(CONFIG_X86_64) += x86-5lvl.o lib-$(CONFIG_RISCV) += kaslr.o riscv.o riscv-stub.o lib-$(CONFIG_LOONGARCH) += loongarch.o loongarch-stub.o - +ifdef CONFIG_X86_64 +lib-$(subst m,y,$(CONFIG_MSHV_ROOT)) += efi-mshv-common.o +lib-$(CONFIG_MSHV_ROOT) += x86-efi-mshv.o +endif CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) zboot-obj-y := zboot-decompress-gzip.o
drivers/firmware/efi/libstub/efi-mshv-common.c new file +148 -0
148 lines not on this page · raw message
drivers/firmware/efi/libstub/efi-mshv.h new file +109 -0
109 lines not on this page · raw message
drivers/firmware/efi/libstub/x86-efi-mshv.c new file +217 -0
217 lines not on this page · raw message
drivers/firmware/efi/libstub/x86-stub.c +17 -1
@@ -20,6 +20,7 @@#include "efistub.h" #include "x86-stub.h" +#include "efi-mshv.h" extern char _bss[], _ebss[];
@@ -737,6 +738,7 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv) {const char *signature; +efi_status_t status;struct exit_boot_struct *p = priv; signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
@@ -751,6 +753,11 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, &p->efi->efi_memmap, &p->efi->efi_memmap_hi); p->efi->efi_memmap_size = map->map_size; +/* Notify hypervisor of efi runtime services pages */ + status =mshv_set_efi_rt_range (map); +if (status !=EFI_SUCCESS ) +return status; +return EFI_SUCCESS ; }
@@ -918,7 +925,7 @@ void __noreturn efi_stub_entry(efi_handle_t handle,const struct linux_efi_initrd *initrd =NULL ;unsigned long kernel_entry;struct setup_header *hdr; -efi_status_t status; +efi_status_t status, mshv_status; efi_system_table = sys_table_arg;/* Check if we were booted by the EFI firmware */
@@ -1011,6 +1018,8 @@ void __noreturn efi_stub_entry(efi_handle_t handle,/* Ask the firmware to clear memory on unclean shutdown */ efi_enable_reset_attack_mitigation (); + mshv_status =mshv_efi_setup (boot_params); +efi_random_get_seed ();efi_retrieve_eventlog ();
@@ -1035,6 +1044,13 @@ void __noreturn efi_stub_entry(efi_handle_t handle,*/ sev_enable (boot_params); +/* +* Launch the hypervisor before switching to 5 level paging. +* The hypervisor does not support being launched with LA57 enabled. +*/ +if (mshv_status ==EFI_SUCCESS ) + mshv_status =mshv_launch (); +efi_5level_switch ();enter_kernel (kernel_entry, boot_params);
drivers/hv/hv_common.c +82 -0
91 lines not on this page · raw message
include/asm-generic/mshyperv.h +7 -0
@@ -212,6 +212,13 @@ int hv_common_cpu_init(unsigned int cpu);int hv_common_cpu_die (unsigned int cpu);void hv_identify_partition_type (void ); +#define HV_MAX_RESVD_RANGES 32 +extern struct resource hv_mshv_res[HV_MAX_RESVD_RANGES ]; +extern u32 ranges_nr; + +void __init hv_dump_mshv_memory (void ); +void __init hv_mark_resources (void ); +/** * hv_cpu_number_to_vp_number() - Map CPU to VP. * @cpu_number: CPU number in Linux terms
include/linux/efi.h +1 -0
@@ -391,6 +391,7 @@ void efi_native_runtime_setup(void);#define EFI_LOAD_FILE_PROTOCOL_GUID EFI_GUID(0x56ec3091, 0x954c, 0x11d2, 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)#define EFI_LOAD_FILE2_PROTOCOL_GUID EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e, 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)#define EFI_RT_PROPERTIES_TABLE_GUID EFI_GUID(0xeb66918a, 0x7eef, 0x402a, 0x84, 0x2e, 0x93, 0x1d, 0x21, 0xc3, 0x8a, 0xe9) +#define EFI_MSHV_MEDIA_PROTOCOL_GUID EFI_GUID(0x098d423a, 0x6ca5, 0x4ad4, 0x90, 0xfa, 0x72, 0xc3, 0xce, 0x22, 0xc8, 0xd0)#define EFI_DXE_SERVICES_TABLE_GUID EFI_GUID(0x05ad34ba, 0x6f02, 0x4214, 0x95, 0x2e, 0x4d, 0xa0, 0x39, 0x8e, 0x2b, 0xb9)#define EFI_SMBIOS_PROTOCOL_GUID EFI_GUID(0x03583ff6, 0xcb36, 0x4940, 0x94, 0x7e, 0xb9, 0xb3, 0x9f, 0x4a, 0xfa, 0xf7)#define EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID EFI_GUID(0xf4560cf6, 0x40ec, 0x4b4a, 0xa1, 0x92, 0xbf, 0x1d, 0x57, 0xd0, 0xb1, 0x89)
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 12/13] hyperv: add MSHV Dom0 root-partition boot enablement (EFI HvLoader)
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [Critical] Secure Boot bypass and arbitrary memory overwrite via `hyperv_resvd=` early parameter.…
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [Critical] Secure Boot bypass and arbitrary memory overwrite via hyperv_resvd= early parameter.
- [High] Uninitialized stack variable ret used across trust boundary in mshv_launch.
- [High] EFI stub passes unmerged hypervisor memory ranges to the kernel, overflowing command-line and parser limits.
- [High] Logic error in hv_parse_hyperv_resvd_new rejects valid physical address 0.
- [Medium] Incorrect variadic argument forwarding in mshv_efi_reboot.
- [Medium] State corruption in hv_parse_hyperv_resvd_new overwriting head without clearing the tail.
- [Low] Incomplete implementation/Dead code: SETUP_MSHV is built but never parsed.
--
--- Patch [12]: [PATCH v2 12/13] hyperv: add MSHV Dom0 root-partition boot enablement (EFI HvLoader) --- commit d68b02782397b0c67109138fd120320cacae1537
Author: Wei Hu <[email protected]>
hyperv: add MSHV Dom0 root-partition boot enablement (EFI HvLoader)
Port the EFI HvLoader protocol handshake (efi-mshv stub + SETUP_MSHV) and the hyperv_resvd_new memory reservation so an upstream kernel can boot as a Microsoft Hypervisor (MSHV) root partition.
Quoted 4 lines
arch/x86/kernel/cpu/mshyperv.c +0
-0
[ ... ]
Quoted 4 lines
arch/x86/kernel/cpu/mshyperv.c +4
-0+static int __init hv_parse_hyperv_resvd (char *arg) +{ +int idx, max =ARRAY_SIZE (hv_resvd_ranges); +int i =0 ;
[ ... ]
Quoted 26 lines
arch/x86/kernel/cpu/mshyperv.c +26
-0+while (get_option (&arg, &idx)) { +if (i >= max) { +pr_err ("Hyper-V: resvd ranges tbl full %d\n" , idx); +break ; + } + + hv_resvd_ranges[i++] = idx; + } + +return 0 ; +} +early_param ("hyperv_resvd" , hv_parse_hyperv_resvd); + +/* +* Reserve memory that the hypervisor is using early on. The ranges are marked +* reserved by a custom bootloader, change that to usable and reserve that +* range. Note, the bootloader sanitizes the e820 before passing on here. +*/ +static void __init hv_resv_mshv_memory (void ) +{ +u64 start, end, size; +int i, idx, max =ARRAY_SIZE (hv_resvd_ranges); +int resource_index =0 ; + +for (i =0 ; i < max && hv_resvd_ranges[i] !=-1 ; i++) { + idx = hv_resvd_ranges[i];
[ ... ]
Quoted 4 lines
arch/x86/kernel/cpu/mshyperv.c +4
-0+ start = e820_table->entries[idx].addr; + size = e820_table->entries[idx].size; + end = start + size -1 ; +
arch/x86/kernel/cpu/mshyperv.c EXPORT_SYMBOL_GPL(hv_get_hypervisor_version); · line 570
Quoted 10 lines
arch/x86/kernel/cpu/mshyperv.c +10
-0+memblock_reserve (start, size); + e820_table->entries[idx].type =E820_TYPE_RAM ; +pr_info ("Hyper-V reserve [mem %#018Lx-%#018Lx]\n" , start, end); + + hv_mshv_res[resource_index].name ="Hypervisor Code and Data" ; + hv_mshv_res[resource_index].flags =IORESOURCE_BUSY |IORESOURCE_SYSTEM_RAM ; + hv_mshv_res[resource_index].start = start; + hv_mshv_res[resource_index++].end = end; + } +}
[Severity: Critical]
Does this allow bypassing kernel lockdown?
The hv_parse_hyperv_resvd() function takes an unauthenticated index from the kernel command line. Then hv_resv_mshv_memory() blindly converts that e820 entry to E820_TYPE_RAM and IORESOURCE_SYSTEM_RAM without bounds checks.
Could a user with access to the bootloader command line use this to target reserved ACPI or firmware regions, map them as System RAM, and overwrite them via kexec_file_load?
Quoted 4 lines
drivers/firmware/efi/libstub/efi-mshv-common.c new file +0
-0
[ ... ]
drivers/firmware/efi/libstub/efi-mshv-common.c line 148
Quoted 13 lines
drivers/firmware/efi/libstub/efi-mshv-common.c new file +13
-0+efi_status_t mshv_launch (void ) +{ +struct hvl_return_data ret; + +if (!efi_mshv) +return EFI_SUCCESS ; + + efi_mshv->launch_hv (NULL , &ret); +/* TODO: Where/how do we dump the hv loader logs? */ +if (ret.launch_data.launch_status !=0 ) +efi_rt_call (reset_system,EFI_RESET_COLD ,EFI_ABORTED ,0 ,NULL ); +return EFI_SUCCESS ; +}
[Severity: High]
Can this result in evaluating uninitialized stack memory?
The ret structure is allocated on the stack but not zeroed before being passed to efi_mshv->launch_hv(). If the hypervisor loader protocol fails early or neglects to fully populate the structure, does checking ret.launch_data.launch_status read stack garbage and trigger an unexpected cold reboot?
Quoted 4 lines
drivers/firmware/efi/libstub/efi-mshv.h new file +0
-0
[ ... ]
drivers/firmware/efi/libstub/efi-mshv.h line 22
Quoted 11 lines
drivers/firmware/efi/libstub/efi-mshv.h new file +11
-0+static inline void mshv_efi_reboot (const char *fmt, ...) +{ +va_list args; + +va_start (args, fmt); +efi_printk (fmt, args); +va_end (args); + +efi_bs_call (stall,5 *EFI_USEC_PER_SEC ); +efi_rt_call (reset_system,EFI_RESET_COLD ,EFI_ABORTED ,0 ,NULL ); +}
[Severity: Medium]
Is args correctly passed to efi_printk() here?
efi_printk() is a variadic function rather than a vprintf-style function. Passing a va_list directly to it as the first argument will cause it to interpret the va_list structure itself as the format argument, which prints garbage memory values instead of the actual error codes.
Quoted 4 lines
drivers/firmware/efi/libstub/x86-efi-mshv.c new file +0
-0
[ ... ]
Quoted 2 lines
drivers/firmware/efi/libstub/x86-efi-mshv.c new file +2
-0+efi_status_t mshv_efi_setup (struct boot_params *boot_params) +{
[ ... ]
Quoted 1 line
drivers/firmware/efi/libstub/x86-efi-mshv.c new file +1
-0+for (i =0 , nr_ranges =0 ; i < nr_desc; i++) {
[ ... ]
Quoted 7 lines
drivers/firmware/efi/libstub/x86-efi-mshv.c new file +7
-0+ mshv_range->name ="Hypervisor Code and Data" ; + mshv_range->flags =IORESOURCE_BUSY |IORESOURCE_SYSTEM_RAM ; + mshv_range->start = d->phys_addr; + mshv_range->end = d->phys_addr + (d->num_pages <<PAGE_SHIFT ) -1 ; + + prev = mshv_range++; + nr_ranges++;
[ ... ]
drivers/firmware/efi/libstub/x86-efi-mshv.c line 168
Quoted 3 lines
drivers/firmware/efi/libstub/x86-efi-mshv.c new file +3
-0+ } + + status =mshv_populate_ranges (boot_params, mem_map, map_sz, desc_sz);
[Severity: High]
Does this pass the unmerged EFI memory map to the command line?
The code successfully merges adjacent EFI descriptors into the mshv_reserved array above, but then passes the raw, unmerged mem_map array into mshv_populate_ranges().
Since mshv_efi_update_cmdline() serializes every descriptor, could this exceed the 32-range limit (HV_MAX_RESVD_RANGES) in hv_parse_hyperv_resvd_new() if the unmerged map is highly fragmented, causing the kernel to silently drop the remaining ranges?
Quoted 10 lines
drivers/firmware/efi/libstub/x86-efi-mshv.c new file +10
-0+if (status !=EFI_SUCCESS ) +mshv_efi_reboot ("failed to update command line with code %d" , + status); + +/* Build an indirect setup_data for each mshv reserved range. */ + status =efi_bs_call (allocate_pool,EFI_LOADER_DATA , + nr_ranges *sizeof (struct mshv_setup_data ), + (void **)&sd_block); +if (status !=EFI_SUCCESS ) +mshv_efi_reboot ("failed to allocate space for hv ranges: error code %d" , status);
[ ... ]
drivers/firmware/efi/libstub/x86-efi-mshv.c line 204
Quoted 13 lines
drivers/firmware/efi/libstub/x86-efi-mshv.c new file +13
-0+for (i =0 ; i < nr_ranges; i++) { + start = mshv_reserved[i].start; + end = mshv_reserved[i].end; + + sd_block[i].sd.type =SETUP_INDIRECT ; + sd_block[i].sd.len =sizeof (struct setup_indirect ); + sd_block[i].sd.next = (__u64)&sd_block[i +1 ]; + + sd_block[i].si.type =SETUP_MSHV ; + sd_block[i].si.reserved =0 ; + sd_block[i].si.len = end - start +1 ; + sd_block[i].si.addr = start; + }
[Severity: Low]
Is the SETUP_MSHV data parsed by the kernel later?
The stub builds a SETUP_INDIRECT chain wrapping SETUP_MSHV elements, but it doesn't look like there is any logic added in the kernel to parse SETUP_MSHV. Does this leave unused structures in boot memory?
Quoted 4 lines
drivers/hv/hv_common.c +0
-0
[ ... ]
Quoted 10 lines
drivers/hv/hv_common.c +10
-0+static int __init hv_parse_hyperv_resvd_new (char *arg) +{ +int i =0 ; + +if (is_kdump_kernel ()) +return 0 ; + +while (arg && *arg) { +unsigned long long region_start, region_sz; +char *range, *addr_str;
[ ... ]
drivers/hv/hv_common.c const char *hv_result_to_string(u64 status) · line 904
Quoted 2 lines
drivers/hv/hv_common.c +2
-0+if (kstrtoull (range,16 , ®ion_sz) || !region_sz || +kstrtoull (addr_str,16 , ®ion_start) || !region_start) {
[Severity: High]
Does the !region_start check reject physical address 0?
If the hypervisor reserves a range starting at the beginning of physical address space (page 0), region_start will be 0. This check will treat it as a validation failure and break out of the parsing loop.
Quoted 19 lines
drivers/hv/hv_common.c +19
-0+pr_err ("Hyper-V: invalid format for hyperv_resvd_new\n" ); +break ; + } + +memblock_reserve (region_start, region_sz); + + hv_mshv_res[i].name ="Hypervisor Code and Data" ; + hv_mshv_res[i].flags =IORESOURCE_BUSY |IORESOURCE_SYSTEM_RAM ; + hv_mshv_res[i].start = region_start; + hv_mshv_res[i].end = region_start + region_sz -1 ; + + ++i; + } + + ranges_nr = i; + +return 0 ; +} +early_param ("hyperv_resvd_new" , hv_parse_hyperv_resvd_new);
[ ... ]
drivers/hv/hv_common.c const char *hv_result_to_string(u64 status) · line 945
Quoted 5 lines
drivers/hv/hv_common.c +5
-0+void __init hv_mark_resources (void ) +{ +int i, max =ARRAY_SIZE (hv_mshv_res); + +for (i =0 ; i < max && hv_mshv_res[i].end; i++)
[Severity: Medium]
Can stale entries in hv_mshv_res be read during resource marking?
If hv_parse_hyperv_resvd_new() is invoked multiple times (e.g. appended by bootloader and supplied by user), the local index i resets to 0, overwriting the head of hv_mshv_res without clearing the tail.
Because hv_mark_resources() iterates until hv_mshv_res[i].end == 0 rather than using ranges_nr, it will process the remaining stale tail entries from the previous parse.
Quoted 2 lines
drivers/hv/hv_common.c +2
-0+insert_resource (&iomem_resource, &hv_mshv_res[i]); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Wei Liu
maintainerRe: [PATCH v2 12/13] hyperv: add MSHV Dom0 root-partition boot enablement (EFI HvLoader)
In reply to Wei HuQuoted 9 lines · Wei Hu
From: Wei Hu <[email protected]>
Port the EFI HvLoader protocol handshake (efi-mshv stub +
SETUP_MSHV) and thehyperv_resvd_newmemory reservation so an upstream kernel can boot as a Microsoft Hypervisor (MSHV) root partition. Mainline lacks this Linux-side enablement, so lxhvloader declines to hand off a stock kernel as root.Signed-off-by Wei Hu <[email protected]>
This patch should be dropped from this series.
I don't think we should modify the EFI stub yet. We should explore other methods first.
Thanks,
Wei
PATCH v2 13/13 mshv: set up own SynIC registers on a nested root partition
In reply to Wei HuFrom: Wei Hu <[email protected]>
Upstream mshv_synic_cpu_init()/_exit() skip programming the SynIC SIMP/SIEFP/SCONTROL registers when VMBus is active (hv_vmbus_exists()), assuming VMBus's hv_hyp_synic_enable_regs() already provisioned them.
That assumption is wrong for a *nested* root partition. There, VMBus programs the *nested* SynIC MSRs (HV_X64_MSR_NESTED_SIMP, ...) via hv_set_msr()'s nested remap, while mshv_synic reads/writes the *non-nested* SynIC MSRs (hv_get/set_non_nested_msr). As a result mshv maps the wrong message page, never receives the SYNIC intercept message that carries the async hypercall completion, and mshv_async_hvcall_handler blocks forever -- hanging SEV-SNP guest creation from user space.
Gate the "VMBus owns the SynIC registers" optimization on !hv_nested so that a nested root partition programs its own non-nested SynIC registers, restoring correct intercept/async-hypercall delivery. No change for a non-nested (bare-metal) root.
--- drivers/hv/mshv_synic.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/hv/mshv_synic.c +14 -2
@@ -472,7 +472,13 @@ static int mshv_synic_cpu_init(unsigned int cpu)* VMBus owns SIMP/SIEFP/SCONTROL when it is active. * See hv_hyp_synic_enable_regs() for that initialization. */ -bool vmbus_active =hv_vmbus_exists (); +/* +* On a nested root partition VMBus programs the *nested* SynIC MSRs +* (SIMP/SIEFP/SCONTROL) via hv_set_msr()'s nested remap, whereas the +* code below reads/writes the non-nested SynIC MSRs. So when nested, +* do not defer SynIC setup to VMBus -- set up our own registers. +*/ +bool vmbus_active =hv_vmbus_exists () && !hv_nested;/* * Map the SYNIC message page. When VMBus is not active the
@@ -593,7 +599,13 @@ static int mshv_synic_cpu_exit(unsigned int cpu)struct hv_synic_event_ring_page **event_ring_page = &spages->synic_event_ring_page;/* VMBus owns SIMP/SIEFP/SCONTROL when it is active */ -bool vmbus_active =hv_vmbus_exists (); +/* +* On a nested root partition VMBus programs the *nested* SynIC MSRs +* (SIMP/SIEFP/SCONTROL) via hv_set_msr()'s nested remap, whereas the +* code below reads/writes the non-nested SynIC MSRs. So when nested, +* do not defer SynIC setup to VMBus -- set up our own registers. +*/ +bool vmbus_active =hv_vmbus_exists () && !hv_nested;/* Disable the interrupt */ sint.as_uint64 =hv_get_non_nested_msr (HV_MSR_SINT0 +HV_SYNIC_INTERCEPTION_SINT_INDEX );
Signature · 1 line
2.43.0