Support for Hyper-V's HvExtCallGetBootZeroedMemory()
v2 cover Support for Hyper-V's HvExtCallGetBootZeroedMemory()
This patch series implements two new Hyper-V hypercalls. The primary goal is to support "zeroed memory" enlightenments 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.
Note that as discussed, a full solution should not only take into account memory that QEMU touched, but also memory that the guest itself touched. This will require a new KVM ioctl. This patch series currently does not implement that part, but it should be relatively straightforward to add that to the get_boot_zeroed_memory hypercall handler once it exists.
RFC v2
- added hv-ext-query-caps option, tracking hv-boot-zeroed-mem (and future extended hypercall options) via kvm_hyperv_properties[]
- added tracking for guest pages touched by QEMU
Florian Schmidt (2):
Add HvExtCallQueryCapabilities
Add HvExtCallGetBootZeroedMemory
docs/system/i386/hyperv.rst | 14 +++ hw/hyperv/hyperv.c | 149 +++++++++++++++++++++++++++++++ include/hw/hyperv/hyperv-proto.h | 12 +++ include/hw/hyperv/hyperv.h | 12 +++ include/system/physmem.h | 8 ++ system/memory.c | 7 +- system/physmem.c | 52 +++++++++++ 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 | 29 ++++++ target/i386/kvm/kvm_i386.h | 2 + 13 files changed, 309 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.3
RFC PATCH v2 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
@@ -256,6 +256,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 it's required to be enabled + to use any extended hypercalls. + Supplementary features ----------------------
hw/hyperv/hyperv.c +28 -0
@@ -703,6 +703,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
@@ -10760,6 +10760,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
@@ -1136,6 +1136,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,
@@ -1433,6 +1440,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
RFC PATCH v2 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 currently don't track memory that the VM itself touched. For this, we will need KVM support, and likely a new ioctl that helps us track memory mapped by the guest.
--- docs/system/i386/hyperv.rst | 7 ++ hw/hyperv/hyperv.c | 121 +++++++++++++++++++++++++++++++ include/hw/hyperv/hyperv-proto.h | 11 +++ include/hw/hyperv/hyperv.h | 7 ++ include/system/physmem.h | 8 ++ system/memory.c | 7 +- system/physmem.c | 52 +++++++++++++ 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 | 17 +++++ 12 files changed, 244 insertions(+), 2 deletions(-)
docs/system/i386/hyperv.rst +7 -0
@@ -263,6 +263,13 @@ Existing enlightenments provide any useful new functionality, but it's it's required to be enabled to use any extended hypercalls. +``hv-boot-zeroed-mem`` + Enables the HvExtGetBootZeroedMemory 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 +121 -0
149 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 +7 -0
@@ -102,11 +102,18 @@ 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 ); +void hyperv_boot_zeroed_setup (void ); + typedef enum HvSynthDbgMsgType { HV_SYNDBG_MSG_CONNECTION_INFO, HV_SYNDBG_MSG_SEND,
include/system/physmem.h +8 -0
@@ -53,4 +53,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
system/memory.c +5 -2
@@ -2188,9 +2188,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 +52 -0
64 lines not on this page · raw message
target/i386/cpu.c +2 -0
@@ -10762,6 +10762,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 +17 -0
@@ -1143,6 +1143,14 @@ static struct { .bits = HV_ENABLE_EXT_HYPERCALLS} } }, + [HYPERV_FEAT_BOOT_ZEROED_MEMORY ] = { + .desc ="enlighten guest about pre-zeroed memory (hv-boot-zeroed-mem)" , + .flags = { + {.func =HV_EXT_CALL_QUERY_CAPABILITIES , .reg =0 , + .bits =HV_EXT_CAP_GET_BOOT_ZEROED_MEMORY } + }, + .dependencies =BIT (HYPERV_FEAT_EXT_CALLS ) + }, }; static struct kvm_cpuid2 *try_get_hv_cpuid(CPUState *cs, int max,
@@ -1378,6 +1386,11 @@ static bool hyperv_feature_supported(CPUState *cs, int feature)continue ; } +if (func ==HV_EXT_CALL_QUERY_CAPABILITIES ) { +/* These do not correspond to host CPUID feature bits. */ +return true; + } +if ((hv_cpuid_get_host (cs, func, reg) & bits) != bits) {return false; }
@@ -1819,6 +1832,10 @@ static int hyperv_init_vcpu(X86CPU *cpu)hyperv_x86_set_vmbus_recommended_features_enabled (); } +if (hyperv_feat_enabled (cpu,HYPERV_FEAT_BOOT_ZEROED_MEMORY )) { +hyperv_boot_zeroed_setup (); + } +return 0 ; }
Signature · 1 line
2.47.3