Support for Hyper-V's HvExtCallGetBootZeroedMemory()
cover Support for Hyper-V's HvExtCallGetBootZeroedMemory()
This patch series implements two new Hyper-V hypercalls. The primary goal is to support "zeroed memory" enlightenment via HvExtCallGetBootZeroedMemory(). To do so, we also need to implement HvExtCallQueryCapabilities(). While HvExtCallQueryCapabilities() uses another bit in EBX on CPUID leaf 0x40000003, it then allows the guest to query for further "extended" hypercalls via the return value of that call.
HvExtCallGetBootZeroedMemory() allows a Windows guest to enquire which areas of memory that were provided by the hypervisor are already zeroed out, meaning that there's no need for the guest to do so again. This has obvious performance benefits.
To get a rough idea of the performance benefit, looking at a 4-core, 64GB Windows 11 guest, we can see that an otherwise idle guest takes a bit less than 90 seconds to finish zeroing and settle down from the time the qemu process is started. If we look at CPU usage for each vCPU task 90 seconds after starting, we get:
1559 498
1294 343
1451 314
4015 2729
Conversely, after enabling HvExtCallGetBootZeroedMemory(), CPU usage is reduced:
1583 458
1441 361
1279 312
1337 264
These are taken from the respective /proc/<pid>/task/<tid>/stat entries, denoting user and system time per "CPU X/KVM" task, in ticks. We can clearly see which to which vCPU fell the tasks of zeroing.
It also has the benefit of not touching all pages at boot. This can be useful if, for example, the VM is started with a balloon target already set to overcommit memory on the host. In this case, we don't risk memory usage spikes on the host if the VM touches all memory before the balloon can catch up.
Major changes since RFC v2:
* integrated KVM ioctl (under review in parallel) into HvExtCallBootZeroedMemory handler.
Florian Schmidt (2):
Add HvExtCallQueryCapabilities
Add HvExtCallGetBootZeroedMemory
docs/system/i386/hyperv.rst | 14 +++ hw/hyperv/hyperv.c | 202 +++++++++++++++++++++++++++++++ include/hw/hyperv/hyperv-proto.h | 12 ++ include/hw/hyperv/hyperv.h | 13 ++ include/system/physmem.h | 8 ++ linux-headers/linux/kvm.h | 15 +++ system/memory.c | 7 +- system/physmem.c | 50 ++++++++ target/i386/cpu.c | 4 + target/i386/cpu.h | 2 + target/i386/kvm/hyperv-proto.h | 6 + target/i386/kvm/hyperv.c | 14 +++ target/i386/kvm/kvm.c | 91 ++++++++++++++ target/i386/kvm/kvm_i386.h | 2 + 14 files changed, 438 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.3
PATCH 1/2 Add HvExtCallQueryCapabilities
On CPUID leaf 0x40000003, EBX bit 20 signals that we support the HvExtCallQueryCapabilities hypercall. This returns a bit field which signals which further extended hypercalls are supported (as a way to conserve CPUID leaf bits).
We'll track such capabilities in kvm_hyperv_properties like all the others, but they will have a ".reg" entry of 0, since they aren't returned via CPUID and so don't have a corresponding register.
For now, we don't support any extended hypercalls, but we'll add HvExtCallGetBootZeroedMemory in a followup patch.
--- docs/system/i386/hyperv.rst | 7 +++++++ hw/hyperv/hyperv.c | 28 ++++++++++++++++++++++++++++ include/hw/hyperv/hyperv-proto.h | 1 + include/hw/hyperv/hyperv.h | 5 +++++ target/i386/cpu.c | 2 ++ target/i386/cpu.h | 1 + target/i386/kvm/hyperv-proto.h | 1 + target/i386/kvm/hyperv.c | 6 ++++++ target/i386/kvm/kvm.c | 12 ++++++++++++ target/i386/kvm/kvm_i386.h | 2 ++ 10 files changed, 65 insertions(+)
docs/system/i386/hyperv.rst +7 -0
@@ -259,6 +259,13 @@ Existing enlightenments Recommended: ``hv-evmcs`` (Intel) +``hv-ext-query-caps`` + Certain "extended hypercalls" are not signaled to the guest directly via + CPUID bits, but use a separate hypercall that returns a bitfield to signal + support for them. This enables this hypercall. On its own, it does not + provide any useful new functionality, but it's required to be enabled to use + any extended hypercalls. + Supplementary features ----------------------
hw/hyperv/hyperv.c +28 -0
@@ -702,6 +702,34 @@ int hyperv_set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)return set_event_flag_handler (conn_id, notifier); } +uint16_t hyperv_ext_hcall_query_caps (uint64_t sup,uint64_t outgpa,bool fast) +{ +uint16_t ret; +uint64_t *supported =NULL ; +hwaddr len; + +if (fast) { + ret =HV_STATUS_INVALID_HYPERCALL_CODE ; + goto cleanup; + } + + len =sizeof (*supported); + supported =cpu_physical_memory_map (outgpa, &len,1 ); +if (!supported || len <sizeof (*supported)) { + ret =HV_STATUS_INSUFFICIENT_MEMORY ; + goto cleanup; + } + + *supported = sup; + ret =HV_STATUS_SUCCESS ; + +cleanup: +if (supported) { +cpu_physical_memory_unmap (supported,sizeof (*supported),1 , len); + } +return ret; +} +uint16_t hyperv_hcall_signal_event (uint64_t param,bool fast) {EventFlagHandler *handler;
include/hw/hyperv/hyperv-proto.h +1 -0
@@ -35,6 +35,7 @@#define HV_POST_DEBUG_DATA 0x0069#define HV_RETRIEVE_DEBUG_DATA 0x006a#define HV_RESET_DEBUG_SESSION 0x006b +#define HV_EXT_CALL_QUERY_CAPABILITIES 0x8001#define HV_HYPERCALL_FAST (1u << 16)/*
include/hw/hyperv/hyperv.h +5 -0
@@ -97,6 +97,11 @@ uint16_t hyperv_hcall_retreive_dbg_data(uint64_t ingpa, uint64_t outgpa,*/ uint16_t hyperv_hcall_post_dbg_data (uint64_t ingpa,uint64_t outgpa,bool fast); +/* +* Process HVCALL_EXT_QUERY_CAPABILITIES hypercall. +*/ +uint16_t hyperv_ext_hcall_query_caps (uint64_t sup,uint64_t outgpa,bool fast); +uint32_t hyperv_syndbg_send (uint64_t ingpa,uint32_t count);uint32_t hyperv_syndbg_recv (uint64_t ingpa,uint32_t count);void hyperv_syndbg_set_pending_page (uint64_t ingpa);
target/i386/cpu.c +2 -0
@@ -10768,6 +10768,8 @@ static const Property x86_cpu_properties[] = { HYPERV_FEAT_TLBFLUSH_EXT, 0),DEFINE_PROP_BIT64 ("hv-tlbflush-direct" ,X86CPU , hyperv_features,HYPERV_FEAT_TLBFLUSH_DIRECT ,0 ), +DEFINE_PROP_BIT64 ("hv-ext-query-caps" ,X86CPU , hyperv_features, +HYPERV_FEAT_EXT_CALLS ,0 ),DEFINE_PROP_ON_OFF_AUTO ("hv-no-nonarch-coresharing" ,X86CPU , hyperv_no_nonarch_cs,ON_OFF_AUTO_OFF ), #ifdef CONFIG_SYNDBG
target/i386/cpu.h +1 -0
@@ -1480,6 +1480,7 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w);#define HYPERV_FEAT_XMM_INPUT 18#define HYPERV_FEAT_TLBFLUSH_EXT 19#define HYPERV_FEAT_TLBFLUSH_DIRECT 20 +#define HYPERV_FEAT_EXT_CALLS 21#ifndef HYPERV_SPINLOCK_NEVER_NOTIFY #define HYPERV_SPINLOCK_NEVER_NOTIFY 0xFFFFFFFF
target/i386/kvm/hyperv-proto.h +1 -0
@@ -46,6 +46,7 @@*/ #define HV_POST_MESSAGES (1u << 4)#define HV_SIGNAL_EVENTS (1u << 5) +#define HV_ENABLE_EXT_HYPERCALLS (1u << 20)/* * HV_CPUID_FEATURES.EDX bits
target/i386/kvm/hyperv.c +6 -0
@@ -17,6 +17,7 @@#include "hyperv.h" #include "hw/hyperv/hyperv.h" #include "hyperv-proto.h" +#include "kvm_i386.h" int hyperv_x86_synic_add (X86CPU *cpu) {
@@ -117,6 +118,11 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit) exit->u.hcall.result =hyperv_hcall_reset_dbg_session (out_param);break ; +case HV_EXT_CALL_QUERY_CAPABILITIES : + exit->u.hcall.result = +hyperv_ext_hcall_query_caps (hv_build_ext_call_caps (CPU (cpu)), + out_param, fast); +break ;default : exit->u.hcall.result =HV_STATUS_INVALID_HYPERCALL_CODE ; }
target/i386/kvm/kvm.c +12 -0
@@ -1137,6 +1137,13 @@ static struct { }, .dependencies = BIT(HYPERV_FEAT_VAPIC) }, + [HYPERV_FEAT_EXT_CALLS] = { + .desc = "query availability of extended hypercalls (hv-ext-query-caps)", + .flags = { + {.func = HV_CPUID_FEATURES, .reg = R_EBX, + .bits = HV_ENABLE_EXT_HYPERCALLS} + } + }, }; static struct kvm_cpuid2 *try_get_hv_cpuid(CPUState *cs, int max,
@@ -1434,6 +1441,11 @@ static uint32_t hv_build_cpuid_leaf(CPUState *cs, uint32_t func, int reg)return r; } +uint64_t hv_build_ext_call_caps (CPUState *cs) +{ +return hv_build_cpuid_leaf (cs,HV_EXT_CALL_QUERY_CAPABILITIES ,0 ); +} +/* * Expand Hyper-V CPU features. In partucular, check that all the requested * features are supported by the host and the sanity of the configuration
target/i386/kvm/kvm_i386.h +2 -0
@@ -72,6 +72,8 @@ struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid,uint32_t cpuid_entry_get_reg (struct kvm_cpuid_entry2 *entry,int reg);uint32_t kvm_x86_build_cpuid (CPUX86State *env,struct kvm_cpuid_entry2 *entries,uint32_t cpuid_i); + +uint64_t hv_build_ext_call_caps (CPUState *cs);#endif /* CONFIG_KVM */ void kvm_smm_cpu_address_space_init (X86CPU *cpu);
Signature · 1 line
2.47.3
PATCH 2/2 Add HvExtCallGetBootZeroedMemory
In reply to Florian SchmidtThis call allows a guest to ask the hypervisor which of its (guest physical) memory ranges were already zeroed out by the hypervisor, which means there's no need for the guest to zero them out again at boot.
To do so, we have to track what memory is not zero any more. The conservative estimate for this is to consider all memory that we ever touched to not be zero any more. To track this, we introduce a new bitmap, independent of the existing dirty tracking maps. This is for several reasons:
* Dirty memory tracking bitmaps are always allocated. This is wasteful when we only want this bitmap for one specific use case.
* Dirty memory tracking is tightly aligned with target page sizes. We want to track at a much coarser granularity, which reduces the bitmap size, and will also make our hypercall reply have fewer fragmented areas, without losing too much of the benefit.
Note that there are limitations of what we track: we only track initial memory. Memory hotplugged later will not be tracked, and so never reported as pre-zeroed. We also don't track non-zero memory over migrations. Both of these are fine with respect to the use case: Windows only enquires about zeroed memory relatively early during boot, where both of these operations are uncommon. For those cases, we simply report "memory is not pre-zeroed".
Finally, we can't track memory that the VM itself touched. For this, we use a new KVM ioctl that tracks memory mapped in by the guest. This enlightnement depends on its availability, and returns areas marked as untouched by both QEMU and KVM.
NOTE: the KVM capability and ioctl numbers may change until the KVM side is settled.
--- docs/system/i386/hyperv.rst | 7 ++ hw/hyperv/hyperv.c | 178 ++++++++++++++++++++++++++++++- include/hw/hyperv/hyperv-proto.h | 11 ++ include/hw/hyperv/hyperv.h | 8 ++ include/system/physmem.h | 8 ++ linux-headers/linux/kvm.h | 15 +++ system/memory.c | 7 +- system/physmem.c | 50 +++++++++ target/i386/cpu.c | 2 + target/i386/cpu.h | 1 + target/i386/kvm/hyperv-proto.h | 5 + target/i386/kvm/hyperv.c | 8 ++ target/i386/kvm/kvm.c | 79 ++++++++++++++ 13 files changed, 375 insertions(+), 4 deletions(-)
docs/system/i386/hyperv.rst +7 -0
@@ -266,6 +266,13 @@ Existing enlightenments provide any useful new functionality, but it's required to be enabled to use any extended hypercalls. +``hv-boot-zeroed-mem`` + Enables the HvExtCallGetBootZeroedMemory hypercall. This allows a Windows + guest to inquire which memory has already been zeroed out by the host and + thus doesn't need to be zeroed out at boot again. + + Requires: ``hv-ext-query-caps`` + Supplementary features ----------------------
hw/hyperv/hyperv.c +176 -2
214 lines not on this page · raw message
include/hw/hyperv/hyperv-proto.h +11 -0
@@ -36,6 +36,7 @@#define HV_RETRIEVE_DEBUG_DATA 0x006a#define HV_RESET_DEBUG_SESSION 0x006b#define HV_EXT_CALL_QUERY_CAPABILITIES 0x8001 +#define HV_EXT_CALL_GET_BOOT_ZEROED_MEMORY 0x8002#define HV_HYPERCALL_FAST (1u << 16)/*
@@ -192,4 +193,14 @@ struct hyperv_retrieve_debug_data_output {uint32_t retrieved_count;uint32_t remaining_count; } __attribute__ ((__packed__)); + +struct hyperv_get_boot_zeroed_memory_range { +uint64_t start_pfn; +uint64_t page_count; +} __attribute__ ((__packed__ )); + +struct hyperv_get_boot_zeroed_memory_output { +uint64_t range_count; +struct hyperv_get_boot_zeroed_memory_range ranges[255 ]; +} __attribute__ ((__packed__ ));#endif
include/hw/hyperv/hyperv.h +8 -0
@@ -102,11 +102,19 @@ uint16_t hyperv_hcall_post_dbg_data(uint64_t ingpa, uint64_t outgpa, bool fast);*/ uint16_t hyperv_ext_hcall_query_caps (uint64_t sup,uint64_t outgpa,bool fast); +/* +* Process HVCALL_EXT_GET_BOOT_ZEROED_MEMORY hypercall. +*/ +uint16_t hyperv_ext_hcall_get_boot_zeroed_memory (uint64_t outgpa,bool fast); +uint32_t hyperv_syndbg_send (uint64_t ingpa,uint32_t count);uint32_t hyperv_syndbg_recv (uint64_t ingpa,uint32_t count);void hyperv_syndbg_set_pending_page (uint64_t ingpa);uint64_t hyperv_syndbg_query_options (void ); +bool hyperv_boot_zeroed_setup (void ); +void hyperv_boot_zeroed_set_max_gpa (uint64_t max_gpa); + typedef enum HvSynthDbgMsgType { HV_SYNDBG_MSG_CONNECTION_INFO, HV_SYNDBG_MSG_SEND,
include/system/physmem.h +8 -0
@@ -103,4 +103,12 @@ bool physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap, ram_addr_t length);int ram_block_rebind (Error **errp); +void physical_memory_init_mapped_tracker (unsigned long num_pages, +unsigned int order); + +void physical_memory_set_mapped_range (ram_addr_t addr,ram_addr_t length); + +const unsigned long *physical_memory_get_mapped_ranges (unsigned long *len, +unsigned int *order); +#endif
linux-headers/linux/kvm.h +15 -0
@@ -986,6 +986,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248#define KVM_CAP_S390_HPAGE_2G 249 +#define KVM_CAP_EVER_MAPPED 250struct kvm_irq_routing_irqchip {__u32 irqchip;
@@ -1655,4 +1656,18 @@ struct kvm_pre_fault_memory { __u64 padding[5]; }; +#define KVM_GET_EVER_MAPPED_LOG _IOW(KVMIO, 0xd6, struct kvm_ever_mapped_log) + +struct kvm_ever_mapped_log { +__u64 first_granule; +__u64 num_granules; +__u32 granule_shift; +__u32 flags; +union { +void *bitmap; +__u64 padding; + }; +__u64 reserved[4 ]; +}; +#endif /* __LINUX_KVM_H */
system/memory.c +5 -2
@@ -2173,9 +2173,12 @@ void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)void memory_region_set_dirty (MemoryRegion *mr,hwaddr addr,hwaddr size) { +ram_addr_t ramaddr; +assert (mr->ram_block); -physical_memory_set_dirty_range (memory_region_get_ram_addr (mr) + addr, - size, + ramaddr =memory_region_get_ram_addr (mr); +physical_memory_set_mapped_range (ramaddr + addr, size); +physical_memory_set_dirty_range (ramaddr + addr, size,memory_region_get_dirty_log_mask (mr)); }
system/physmem.c +50 -0
62 lines not on this page · raw message
target/i386/cpu.c +2 -0
@@ -10770,6 +10770,8 @@ static const Property x86_cpu_properties[] = { HYPERV_FEAT_TLBFLUSH_DIRECT, 0),DEFINE_PROP_BIT64 ("hv-ext-query-caps" ,X86CPU , hyperv_features,HYPERV_FEAT_EXT_CALLS ,0 ), +DEFINE_PROP_BIT64 ("hv-boot-zeroed-mem" ,X86CPU , hyperv_features, +HYPERV_FEAT_BOOT_ZEROED_MEMORY ,0 ),DEFINE_PROP_ON_OFF_AUTO ("hv-no-nonarch-coresharing" ,X86CPU , hyperv_no_nonarch_cs,ON_OFF_AUTO_OFF ), #ifdef CONFIG_SYNDBG
target/i386/cpu.h +1 -0
@@ -1481,6 +1481,7 @@ uint64_t x86_cpu_get_supported_feature_word(X86CPU *cpu, FeatureWord w);#define HYPERV_FEAT_TLBFLUSH_EXT 19#define HYPERV_FEAT_TLBFLUSH_DIRECT 20#define HYPERV_FEAT_EXT_CALLS 21 +#define HYPERV_FEAT_BOOT_ZEROED_MEMORY 22#ifndef HYPERV_SPINLOCK_NEVER_NOTIFY #define HYPERV_SPINLOCK_NEVER_NOTIFY 0xFFFFFFFF
target/i386/kvm/hyperv-proto.h +5 -0
@@ -94,6 +94,11 @@#define HV_NESTED_DIRECT_FLUSH (1u << 17)#define HV_NESTED_MSR_BITMAP (1u << 19) +/* +* HV_EXT_CALL_QUERY_CAPABILITIES bits +*/ +#define HV_EXT_CAP_GET_BOOT_ZEROED_MEMORY (1u << 0) +/* * Basic virtualized MSRs */
target/i386/kvm/hyperv.c +8 -0
@@ -123,6 +123,14 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)hyperv_ext_hcall_query_caps (hv_build_ext_call_caps (CPU (cpu)), out_param, fast);break ; +case HV_EXT_CALL_GET_BOOT_ZEROED_MEMORY : +if (!hyperv_feat_enabled (cpu,HYPERV_FEAT_BOOT_ZEROED_MEMORY )) { + exit->u.hcall.result =HV_STATUS_INVALID_HYPERCALL_CODE ; + }else { + exit->u.hcall.result = +hyperv_ext_hcall_get_boot_zeroed_memory (out_param, fast); + } +break ;default : exit->u.hcall.result =HV_STATUS_INVALID_HYPERCALL_CODE ; }
target/i386/kvm/kvm.c +79 -0
109 lines not on this page · raw message
Signature · 1 line
2.47.3
Quoted 31 lines · Florian Schmidt
This patch series implements two new Hyper-V hypercalls. The primary goal is to support "zeroed memory" enlightenment via
HvExtCallGetBootZeroedMemory(). To do so, we also need to implementHvExtCallQueryCapabilities(). WhileHvExtCallQueryCapabilities()uses another bit in EBX on CPUID leaf0x40000003, it then allows the guest to query for further "extended" hypercalls via the return value of that call.
HvExtCallGetBootZeroedMemory()allows a Windows guest to enquire which areas of memory that were provided by the hypervisor are already zeroed out, meaning that there's no need for the guest to do so again. This has obvious performance benefits.To get a rough idea of the performance benefit, looking at a 4-core, 64GB Windows 11 guest, we can see that an otherwise idle guest takes a bit less than 90 seconds to finish zeroing and settle down from the time the qemu process is started. If we look at CPU usage for each vCPU task 90 seconds after starting, we get:
1559 498
1294 343
1451 314
4015 2729
Conversely, after enablingHvExtCallGetBootZeroedMemory(), CPU usage is reduced:
1583 458
1441 361
1279 312
1337 264
These are taken from the respective /proc/<pid>/task/<tid>/stat entries, denoting user and system time per "CPU X/KVM" task, in ticks. We can clearly see which to which vCPU fell the tasks of zeroing.
Quoted 8 lines
It also has the benefit of not touching all pages at boot. This can be useful if, for example, the VM is started with a balloon target already set to overcommit memory on the host. In this case, we don't risk memory usage spikes on the host if the VM touches all memory before the balloon can catch up.
Major changes since RFC v2:
* integrated KVM ioctl (under review in parallel) into HvExtCallBootZeroedMemory handler.
Hi Florian,
was there any result from the community call in September?
I was looking at this and found that windows only uses the hypercall in winload.efi once and then passes the result on to the kernel. Maybe it would be fine to track until this is called with userfaultfd UFFD_FEATURE_WP_ASYNC setup and PAGEMAP_SCAN in the first call of the hypercall and report/monitor nothing afterwards?
I also took a look at virtualbox. It seems to report all normal memory as zeroed:
https://github.com/VirtualBox/virtualbox/blob/b6bea72111fcff48f31c57c9f66365b1d3301bb9/src/VBox/VMM/VMMR3/PGMR3Phys.cpp#L1513
This seems wrong, but it is interesting that they have had it enabled for some time (Looks like at least 06.2024 for the current version).
Thanks
Steffen
Quoted 20 lines
Florian Schmidt (2):
Add HvExtCallQueryCapabilities
Add HvExtCallGetBootZeroedMemorydocs/system/i386/hyperv.rst | 14 +++ hw/hyperv/hyperv.c | 202 +++++++++++++++++++++++++++++++ include/hw/hyperv/hyperv-proto.h | 12 ++ include/hw/hyperv/hyperv.h | 13 ++ include/system/physmem.h | 8 ++ linux-headers/linux/kvm.h | 15 +++ system/memory.c | 7 +- system/physmem.c | 50 ++++++++ target/i386/cpu.c | 4 + target/i386/cpu.h | 2 + target/i386/kvm/hyperv-proto.h | 6 + target/i386/kvm/hyperv.c | 14 +++ target/i386/kvm/kvm.c | 91 ++++++++++++++ target/i386/kvm/kvm_i386.h | 2 + 14 files changed, 438 insertions(+), 2 deletions(-)