Skip to contentSkip to conversation

i386/hyperv: fully control Hyper-V features in CPUID

PATCH Superseded Roman Kagan 2 participants 7 messages Mar 28, 2018 – Mar 29, 2018

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover i386/hyperv: fully control Hyper-V features in CPUID

In order to guarantee compatibility on migration, QEMU should have complete control over the features it announces to the guest via CPUID.

However, a number of Hyper-V-related features happen to depend on the support in the underlying KVM, with no regard to QEMU configuration.

Make QEMU regain control over what Hyper-V features it announces to the guest.

Note #1: the patches are also being proposed[*] for stable-2.11, even though one of them introduces a new cpu property. This is done to minimize the number of published QEMU releases where the behavior of the features is unpredictable, with potentially fatal consequences for the guest.

Note #2: there are other problems in the surrounding code, like ugly error reporting or inconsistent population of MSRs. I think this can be put off to post-2.12.

[*] for the stable branch the second patch will have error returns replaced with warnings; I'll post a separate series.

v1 -> v2:
- indicate what flag requested the feature that can't be enabled in the error message
- fix a typo in the error message for VP_RUNTIME

Roman Kagan (2):
i386/hyperv: add hv-frequencies cpu property i386/hyperv: error out if features requested but unsupported

 target/i386/cpu.h |  1 +
 target/i386/cpu.c |  1 +
 target/i386/kvm.c | 45 +++++++++++++++++++++++++++++++++++++--------
 3 files changed, 39 insertions(+), 8 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH v2 1/2 i386/hyperv: add hv-frequencies cpu property

In order to guarantee compatibility on migration, QEMU should have complete control over the features it announces to the guest via CPUID.

However, the availability of Hyper-V frequency MSRs (HV_X64_MSR_TSC_FREQUENCY and HV_X64_MSR_APIC_FREQUENCY) depends solely on the support for them in the underlying KVM.

Introduce "hv-frequencies" cpu property (off by default) which gives QEMU full control over whether these MSRs are announced.

While at this, drop the redundant check of the cpu tsc frequency, and decouple this feature from hv-time.

---
v1 -> v2:
- indicate what flag requested the feature that can't be enabled in the error message

 target/i386/cpu.h |  1 +
 target/i386/cpu.c |  1 +
 target/i386/kvm.c | 13 +++++++++----
 3 files changed, 11 insertions(+), 4 deletions(-)
target/i386/cpu.h +1 -0
index 78db1b833a..1b219fafc4 100644
@@ -1296,6 +1296,7 @@ struct X86CPU {
     bool hyperv_runtime;
     bool hyperv_synic;
     bool hyperv_stimer;
+    bool hyperv_frequencies;
     bool check_cpuid;
     bool enforce_cpuid;
     bool expose_kvm;
target/i386/cpu.c +1 -0
index 555ae79d29..1a6b082b6f 100644
@@ -4761,6 +4761,7 @@ static Property x86_cpu_properties[] = {
     DEFINE_PROP_BOOL("hv-runtime", X86CPU, hyperv_runtime, false),
     DEFINE_PROP_BOOL("hv-synic", X86CPU, hyperv_synic, false),
     DEFINE_PROP_BOOL("hv-stimer", X86CPU, hyperv_stimer, false),
+    DEFINE_PROP_BOOL("hv-frequencies", X86CPU, hyperv_frequencies, false),
     DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, true),
     DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
     DEFINE_PROP_BOOL("kvm", X86CPU, expose_kvm, true),
target/i386/kvm.c +9 -4
index d23fff12f5..b35623ae24 100644
@@ -648,11 +648,16 @@ static int hyperv_handle_properties(CPUState *cs)
         env->features[FEAT_HYPERV_EAX] |= HV_HYPERCALL_AVAILABLE;
         env->features[FEAT_HYPERV_EAX] |= HV_TIME_REF_COUNT_AVAILABLE;
         env->features[FEAT_HYPERV_EAX] |= HV_REFERENCE_TSC_AVAILABLE;
-
-        if (has_msr_hv_frequencies && tsc_is_stable_and_known(env)) {
-            env->features[FEAT_HYPERV_EAX] |= HV_ACCESS_FREQUENCY_MSRS;
-            env->features[FEAT_HYPERV_EDX] |= HV_FREQUENCY_MSRS_AVAILABLE;
+    }
+    if (cpu->hyperv_frequencies) {
+        if (!has_msr_hv_frequencies) {
+            fprintf(stderr, "Hyper-V frequency MSRs "
+                    "(requested by 'hv-frequencies' cpu flag) "
+                    "are not supported by kernel\n");
+            return -ENOSYS;
         }
+        env->features[FEAT_HYPERV_EAX] |= HV_ACCESS_FREQUENCY_MSRS;
+        env->features[FEAT_HYPERV_EDX] |= HV_FREQUENCY_MSRS_AVAILABLE;
     }
     if (cpu->hyperv_crash && has_msr_hv_crash) {
         env->features[FEAT_HYPERV_EDX] |= HV_GUEST_CRASH_MSR_AVAILABLE;
Signature · 1 line
2.14.3
Reviewed-by on v2 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH for-2.12 v2 1/2] i386/hyperv: add hv-frequencies cpu property

Quoted 57 lines · Roman Kagan

In order to guarantee compatibility on migration, QEMU should have complete control over the features it announces to the guest via CPUID.

However, the availability of Hyper-V frequency MSRs (HV_X64_MSR_TSC_FREQUENCY and HV_X64_MSR_APIC_FREQUENCY) depends solely on the support for them in the underlying KVM.

Introduce "hv-frequencies" cpu property (off by default) which gives QEMU full control over whether these MSRs are announced.

While at this, drop the redundant check of the cpu tsc frequency, and decouple this feature from hv-time.

---
v1 -> v2:
- indicate what flag requested the feature that can't be enabled in the error message

 target/i386/cpu.h |  1 +
 target/i386/cpu.c |  1 +
 target/i386/kvm.c | 13 +++++++++----
 3 files changed, 11 insertions(+), 4 deletions(-)
target/i386/cpu.h +1 -0
index 78db1b833a..1b219fafc4 100644
@@ -1296,6 +1296,7 @@ struct X86CPU {
     bool hyperv_runtime;
     bool hyperv_synic;
     bool hyperv_stimer;
+    bool hyperv_frequencies;
     bool check_cpuid;
     bool enforce_cpuid;
     bool expose_kvm;
target/i386/cpu.c +1 -0
index 555ae79d29..1a6b082b6f 100644
@@ -4761,6 +4761,7 @@ static Property x86_cpu_properties[] = {
     DEFINE_PROP_BOOL("hv-runtime", X86CPU, hyperv_runtime, false),
     DEFINE_PROP_BOOL("hv-synic", X86CPU, hyperv_synic, false),
     DEFINE_PROP_BOOL("hv-stimer", X86CPU, hyperv_stimer, false),
+    DEFINE_PROP_BOOL("hv-frequencies", X86CPU, hyperv_frequencies, false),
     DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, true),
     DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
     DEFINE_PROP_BOOL("kvm", X86CPU, expose_kvm, true),
target/i386/kvm.c +0 -1
index d23fff12f5..b35623ae24 100644
@@ -648,11 +648,16 @@ static int hyperv_handle_properties(CPUState *cs)
         env->features[FEAT_HYPERV_EAX] |= HV_HYPERCALL_AVAILABLE;
         env->features[FEAT_HYPERV_EAX] |= HV_TIME_REF_COUNT_AVAILABLE;
         env->features[FEAT_HYPERV_EAX] |= HV_REFERENCE_TSC_AVAILABLE;
-

target/i386/kvm.c static int hyperv_handle_properties(CPUState *cs) · line 657

Quoted 10 lines
target/i386/kvm.c +7 -3
-        if (has_msr_hv_frequencies && tsc_is_stable_and_known(env)) {
-            env->features[FEAT_HYPERV_EAX] |= HV_ACCESS_FREQUENCY_MSRS;
-            env->features[FEAT_HYPERV_EDX] |= HV_FREQUENCY_MSRS_AVAILABLE;
+    }
+    if (cpu->hyperv_frequencies) {
+        if (!has_msr_hv_frequencies) {
+            fprintf(stderr, "Hyper-V frequency MSRs "
+                    "(requested by 'hv-frequencies' cpu flag) "
+                    "are not supported by kernel\n");
+            return -ENOSYS;

I would like to move this to x86_cpu_filter_features(), but while we don't refactor the Hyper-V CPUID code, this is good enough for now.

Quoted 9 lines
target/i386/kvm.c +2 -0
         }
+        env->features[FEAT_HYPERV_EAX] |= HV_ACCESS_FREQUENCY_MSRS;
+        env->features[FEAT_HYPERV_EDX] |= HV_FREQUENCY_MSRS_AVAILABLE;
     }
     if (cpu->hyperv_crash && has_msr_hv_crash) {
         env->features[FEAT_HYPERV_EDX] |= HV_GUEST_CRASH_MSR_AVAILABLE;
Signature · 1 line
2.14.3
Signature · 1 line
Eduardo
#3 lore raw

PATCH v2 2/2 i386/hyperv: error out if features requested but unsupported

In reply to Roman Kagan

In order to guarantee compatibility on migration, QEMU should have complete control over the features it announces to the guest via CPUID.

However, for a number of Hyper-V-related cpu properties, if the corresponding feature is not supported by the underlying KVM, the propery is silently ignored and the feature is not announced to the guest.

Refuse to start with an error instead.

---
v1 -> v2:
- indicate what flag requested the feature that can't be enabled in the error message
- fix a typo in the error message for VP_RUNTIME

 target/i386/kvm.c | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)
target/i386/kvm.c +28 -4
index b35623ae24..113926aff2 100644
@@ -659,17 +659,41 @@ static int hyperv_handle_properties(CPUState *cs)
         env->features[FEAT_HYPERV_EAX] |= HV_ACCESS_FREQUENCY_MSRS;
         env->features[FEAT_HYPERV_EDX] |= HV_FREQUENCY_MSRS_AVAILABLE;
     }
-    if (cpu->hyperv_crash && has_msr_hv_crash) {
+    if (cpu->hyperv_crash) {
+        if (!has_msr_hv_crash) {
+            fprintf(stderr, "Hyper-V crash MSRs "
+                    "(requested by 'hv-crash' cpu flag) "
+                    "are not supported by kernel\n");
+            return -ENOSYS;
+        }
         env->features[FEAT_HYPERV_EDX] |= HV_GUEST_CRASH_MSR_AVAILABLE;
     }
     env->features[FEAT_HYPERV_EDX] |= HV_CPU_DYNAMIC_PARTITIONING_AVAILABLE;
-    if (cpu->hyperv_reset && has_msr_hv_reset) {
+    if (cpu->hyperv_reset) {
+        if (!has_msr_hv_reset) {
+            fprintf(stderr, "Hyper-V reset MSR "
+                    "(requested by 'hv-reset' cpu flag) "
+                    "is not supported by kernel\n");
+            return -ENOSYS;
+        }
         env->features[FEAT_HYPERV_EAX] |= HV_RESET_AVAILABLE;
     }
-    if (cpu->hyperv_vpindex && has_msr_hv_vpindex) {
+    if (cpu->hyperv_vpindex) {
+        if (!has_msr_hv_vpindex) {
+            fprintf(stderr, "Hyper-V VP_INDEX MSR "
+                    "(requested by 'hv-vpindex' cpu flag) "
+                    "is not supported by kernel\n");
+            return -ENOSYS;
+        }
         env->features[FEAT_HYPERV_EAX] |= HV_VP_INDEX_AVAILABLE;
     }
-    if (cpu->hyperv_runtime && has_msr_hv_runtime) {
+    if (cpu->hyperv_runtime) {
+        if (!has_msr_hv_runtime) {
+            fprintf(stderr, "Hyper-V VP_RUNTIME MSR "
+                    "(requested by 'hv-runtime' cpu flag) "
+                    "is not supported by kernel\n");
+            return -ENOSYS;
+        }
         env->features[FEAT_HYPERV_EAX] |= HV_VP_RUNTIME_AVAILABLE;
     }
     if (cpu->hyperv_synic) {
Signature · 1 line
2.14.3
#4 lore raw

Re: [Qemu-devel] [PATCH for-2.12 v2 2/2] i386/hyperv: error out if features requested but unsupported

Quoted 11 lines · Roman Kagan

In order to guarantee compatibility on migration, QEMU should have complete control over the features it announces to the guest via CPUID.

However, for a number of Hyper-V-related cpu properties, if the corresponding feature is not supported by the underlying KVM, the propery is silently ignored and the feature is not announced to the guest.

Refuse to start with an error instead.

Something I didn't consider before:

Will this block migration before it even starts, or will crash the VM only after all migration data was sent to the destination?

I didn't test it, but kvm_arch_init_vcpu() seems to be too late to block an invalid/unsupport configuration.

Maybe we can simply call hyperv_handle_properties() earlier, inside x86_cpu_realizefn()?

(I know it's very late for this kind of intrusive change in v2.12, but I still think it's a good idea to fix this as soon as possible.)

Quoted 62 lines

---
v1 -> v2:
- indicate what flag requested the feature that can't be enabled in the error message
- fix a typo in the error message for VP_RUNTIME

 target/i386/kvm.c | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)
target/i386/kvm.c +28 -4
index b35623ae24..113926aff2 100644
@@ -659,17 +659,41 @@ static int hyperv_handle_properties(CPUState *cs)
         env->features[FEAT_HYPERV_EAX] |= HV_ACCESS_FREQUENCY_MSRS;
         env->features[FEAT_HYPERV_EDX] |= HV_FREQUENCY_MSRS_AVAILABLE;
     }
-    if (cpu->hyperv_crash && has_msr_hv_crash) {
+    if (cpu->hyperv_crash) {
+        if (!has_msr_hv_crash) {
+            fprintf(stderr, "Hyper-V crash MSRs "
+                    "(requested by 'hv-crash' cpu flag) "
+                    "are not supported by kernel\n");
+            return -ENOSYS;
+        }
         env->features[FEAT_HYPERV_EDX] |= HV_GUEST_CRASH_MSR_AVAILABLE;
     }
     env->features[FEAT_HYPERV_EDX] |= HV_CPU_DYNAMIC_PARTITIONING_AVAILABLE;
-    if (cpu->hyperv_reset && has_msr_hv_reset) {
+    if (cpu->hyperv_reset) {
+        if (!has_msr_hv_reset) {
+            fprintf(stderr, "Hyper-V reset MSR "
+                    "(requested by 'hv-reset' cpu flag) "
+                    "is not supported by kernel\n");
+            return -ENOSYS;
+        }
         env->features[FEAT_HYPERV_EAX] |= HV_RESET_AVAILABLE;
     }
-    if (cpu->hyperv_vpindex && has_msr_hv_vpindex) {
+    if (cpu->hyperv_vpindex) {
+        if (!has_msr_hv_vpindex) {
+            fprintf(stderr, "Hyper-V VP_INDEX MSR "
+                    "(requested by 'hv-vpindex' cpu flag) "
+                    "is not supported by kernel\n");
+            return -ENOSYS;
+        }
         env->features[FEAT_HYPERV_EAX] |= HV_VP_INDEX_AVAILABLE;
     }
-    if (cpu->hyperv_runtime && has_msr_hv_runtime) {
+    if (cpu->hyperv_runtime) {
+        if (!has_msr_hv_runtime) {
+            fprintf(stderr, "Hyper-V VP_RUNTIME MSR "
+                    "(requested by 'hv-runtime' cpu flag) "
+                    "is not supported by kernel\n");
+            return -ENOSYS;
+        }
         env->features[FEAT_HYPERV_EAX] |= HV_VP_RUNTIME_AVAILABLE;
     }
     if (cpu->hyperv_synic) {
Signature · 1 line
2.14.3
Signature · 1 line
Eduardo
#5 lore raw

Re: [Qemu-devel] [PATCH for-2.12 v2 2/2] i386/hyperv: error out if features requested but unsupported

Quoted 13 lines · Eduardo Habkost
Quoted 11 lines · Roman Kagan

In order to guarantee compatibility on migration, QEMU should have complete control over the features it announces to the guest via CPUID.

However, for a number of Hyper-V-related cpu properties, if the corresponding feature is not supported by the underlying KVM, the propery is silently ignored and the feature is not announced to the guest.

Refuse to start with an error instead.

Quoted 7 lines

Something I didn't consider before:

Will this block migration before it even starts, or will crash the VM only after all migration data was sent to the destination?

I didn't test it, but kvm_arch_init_vcpu() seems to be too late to block an invalid/unsupport configuration.

I just did a simple test, force-failing one of the checks and starting QEMU with -incoming defer. It refused to start with the expected error message. IOW in the migration case the destination will abort before the source have started to send any data.

Quoted 2 lines

Maybe we can simply call hyperv_handle_properties() earlier, inside x86_cpu_realizefn()?

Now it's

...
x86_cpu_realizefn
qemu_init_vcpu
qemu_kvm_start_vcpu
qemu_thread_create(qemu_kvm_cpu_thread_fn)
[in vcpu thread] qemu_kvm_cpu_thread_fn
kvm_init_vcpu
kvm_arch_init_vcpu
hyperv_handle_properties
[error return]
[error return]
[error return]
exit(1)

Quoted 3 lines

(I know it's very late for this kind of intrusive change in v2.12, but I still think it's a good idea to fix this as soon as possible.)

I agree that the current hyperv flag handling begs for cleanup but I think this can wait for post-2.12.

Quoted 5 lines
Quoted 5 lines

---
v1 -> v2:
- indicate what flag requested the feature that can't be enabled in the error message
- fix a typo in the error message for VP_RUNTIME

I just noticed that I missed hv-time being silently cleared, too (just in a slightly different pattern), so I'll have to respin.

Thanks,
Roman.

#6 lore raw

Re: [Qemu-devel] [PATCH for-2.12 v2 2/2] i386/hyperv: error out if features requested but unsupported

Quoted 32 lines · Roman Kagan
Quoted 13 lines · Eduardo Habkost
Quoted 11 lines · Roman Kagan

In order to guarantee compatibility on migration, QEMU should have complete control over the features it announces to the guest via CPUID.

However, for a number of Hyper-V-related cpu properties, if the corresponding feature is not supported by the underlying KVM, the propery is silently ignored and the feature is not announced to the guest.

Refuse to start with an error instead.

Quoted 7 lines

Something I didn't consider before:

Will this block migration before it even starts, or will crash the VM only after all migration data was sent to the destination?

I didn't test it, but kvm_arch_init_vcpu() seems to be too late to block an invalid/unsupport configuration.

I just did a simple test, force-failing one of the checks and starting QEMU with -incoming defer. It refused to start with the expected error message. IOW in the migration case the destination will abort before the source have started to send any data.

Quoted 2 lines

Maybe we can simply call hyperv_handle_properties() earlier, inside x86_cpu_realizefn()?

Now it's

Quoted 7 lines

...
x86_cpu_realizefn
qemu_init_vcpu
qemu_kvm_start_vcpu
qemu_thread_create(qemu_kvm_cpu_thread_fn)
[in vcpu thread] qemu_kvm_cpu_thread_fn

Oh, this is the part that I missed. I thought the vCPU thread wouldn't start until migration was finished.

This means the patch is OK as-is. Sorry for the confusion.

Quoted 26 lines
	    kvm_init_vcpu
	      kvm_arch_init_vcpu
	        hyperv_handle_properties
		  [error return]
		[error return]
	      [error return]
	    exit(1)
Quoted 3 lines

(I know it's very late for this kind of intrusive change in v2.12, but I still think it's a good idea to fix this as soon as possible.)

I agree that the current hyperv flag handling begs for cleanup but I think this can wait for post-2.12.

Quoted 5 lines
Quoted 5 lines

---
v1 -> v2:
- indicate what flag requested the feature that can't be enabled in the error message
- fix a typo in the error message for VP_RUNTIME

I just noticed that I missed hv-time being silently cleared, too (just in a slightly different pattern), so I'll have to respin.

Thanks,
Roman.

Signature · 1 line
Eduardo