Skip to contentSkip to conversation

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

PATCH Superseded Roman Kagan 3 participants 8 messages Mar 23, 2018 – Mar 28, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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: 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.

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 | 37 +++++++++++++++++++++++++++++--------
 3 files changed, 31 insertions(+), 8 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH 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 declared 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.

---
Note: this patch introduces a new cpu property, which is not what we normally do in stable branches. However, this appears to be the minimal effort/churn approach to reduce the number of published QEMU releases where the behavior of the feature is unpredictable, with potentially fatal consequences for the guest.

 target/i386/cpu.h |  1 +
 target/i386/cpu.c |  1 +
 target/i386/kvm.c | 12 ++++++++----
 3 files changed, 10 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 +8 -4
index d23fff12f5..fb20ff18c2 100644
@@ -648,11 +648,15 @@ 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 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
#3 lore raw

PATCH 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.

---
 target/i386/kvm.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
target/i386/kvm.c +21 -4
index fb20ff18c2..c9c359241c 100644
@@ -658,17 +658,34 @@ 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 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 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 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_INDEX 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
#2 lore raw

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

In reply to Roman Kagan
Quoted 6 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 declared 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.

So this problem was introduced fairly recently (in v2.11). This makes the decision to break compatibility of (some[1]) existing configurations that didn't specify "hv-frequency" a bit easier.

Quoted 3 lines

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

So, as we have two possible results when running QEMU-2.11, we need to make a guess and choose which half of our users will be affected:

a) People running "-machine pc-2.11 -cpu ...,+hv-time" on Linux 4.14+ (including commit 72c139bacfa3), that have hv-frequencies enabled automatically.
b) People running "-machine pc-2.11 -cpu ...,+hv-time" on Linux 4.13 and older (without commit 72c139bacfa3), that have hv-frequencies disabled.

If we set hv-frequencies=off by default on pc-2.11 (this patch), we will inconvenience group (a). The consequence for them is having hv-frequencies disabled suddenly on CPUID after updating QEMU. The MSRs will still be available to the guest, however (so the guest won't crash), and they can add hv-frequencies=on to their configuration manually.

If we set hv-frequencies=on by default on pc-2.11, we will inconvenience group (b). The consequence for them is having the VM not being runnable anymore until they change the machine-type or add hv-frequencies=off to their configuration.

So it looks like this patch is the safest solution, but I will get back to the "[PATCH v3 2/2] i386/kvm: lower requirements for Hyper-V frequency MSRs exposure" thread to be sure we are not missing anything.

Quoted 68 lines

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

---
Note: this patch introduces a new cpu property, which is not what we normally do in stable branches. However, this appears to be the minimal effort/churn approach to reduce the number of published QEMU releases where the behavior of the feature is unpredictable, with potentially fatal consequences for the guest.

 target/i386/cpu.h |  1 +
 target/i386/cpu.c |  1 +
 target/i386/kvm.c | 12 ++++++++----
 3 files changed, 10 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 +8 -4
index d23fff12f5..fb20ff18c2 100644
@@ -648,11 +648,15 @@ 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 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
Signature · 1 line
Eduardo
#4 lore raw

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

In reply to Roman Kagan
Quoted 8 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.

Quoted 4 lines

Refuse to start with an error instead.

I wonder if we should make these just warnings on -stable, and make them fatal errors only on 2.12. I wouldn't want to make existing running VMs not runnable on a stable update.

Quoted 9 lines
---
 target/i386/kvm.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
target/i386/kvm.c +0 -0
index fb20ff18c2..c9c359241c 100644

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

Quoted 8 lines
target/i386/kvm.c +4 -1
@@ -658,17 +658,34 @@ 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 are not supported by kernel\n");

I would mention the corresponding "hv-..." -cpu option explicitly, for clarity.

Quoted 33 lines
target/i386/kvm.c +17 -3
+            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 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 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_INDEX 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
#7 lore raw
In reply to Roman Kagan
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH for-2.12 0/2] i386/hyperv: fully control Hyper-V features in CPUID

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
2efbb18fe2 i386/hyperv: error out if features requested but unsupported 31e7467f4e i386/hyperv: add hv-frequencies cpu property

=== OUTPUT BEGIN ===
Checking PATCH 1/2: i386/hyperv: add hv-frequencies cpu property... Checking PATCH 2/2: i386/hyperv: error out if features requested but unsupported...
ERROR: suspect code indent for conditional statements (4, 7)
#39: FILE: target/i386/kvm.c:670:
+ if (cpu->hyperv_reset) {
+ if (!has_msr_hv_reset) {

total: 1 errors, 0 warnings, 38 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]

#5 lore raw

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

In reply to Eduardo Habkost
Quoted 9 lines · Eduardo Habkost
Quoted 8 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.

Quoted 8 lines
Quoted 4 lines

Refuse to start with an error instead.

I wonder if we should make these just warnings on -stable, and make them fatal errors only on 2.12. I wouldn't want to make existing running VMs not runnable on a stable update.

OK let's follow your approach and consider who would suffer more:

a) users who started a VM on a newer kernel and then migrated to an older one, and got a guest crash on an unsupported MSR (I'm not even sure they'd be able to see the warnings)

b) users who had a VM configuration with features which didn't actually work (but that wasn't apparently a problem for them), and suddenly couldn't start their VM after QEMU upgrade (the problem is not only cold-restarting per se, but also live migration to the upgraded
version: dunno if the management layer would allow to adjust the VM config to migrate successfully).

I don't have a strong opinion on this, will follow whatever direction you advise.

Quoted 19 lines
Quoted 8 lines
 target/i386/kvm.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
target/i386/kvm.c +0 -0
index fb20ff18c2..c9c359241c 100644

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

Quoted 8 lines
target/i386/kvm.c +4 -1
@@ -658,17 +658,34 @@ 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 are not supported by kernel\n");

I would mention the corresponding "hv-..." -cpu option explicitly, for clarity.

This sounds like a good idea, but I wonder if it should better be done uniformly for all hv_* options, together with transitioning to error_report(), and whether we want to do this at this point in the release cycle.

Thanks,
Roman.

#6 lore raw

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

Quoted 32 lines · Roman Kagan
Quoted 9 lines · Eduardo Habkost
Quoted 8 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.

Quoted 8 lines
Quoted 4 lines

Refuse to start with an error instead.

I wonder if we should make these just warnings on -stable, and make them fatal errors only on 2.12. I wouldn't want to make existing running VMs not runnable on a stable update.

OK let's follow your approach and consider who would suffer more:

a) users who started a VM on a newer kernel and then migrated to an older one, and got a guest crash on an unsupported MSR (I'm not even sure they'd be able to see the warnings)

b) users who had a VM configuration with features which didn't actually work (but that wasn't apparently a problem for them), and suddenly couldn't start their VM after QEMU upgrade (the problem is not only cold-restarting per se, but also live migration to the upgraded
version: dunno if the management layer would allow to adjust the VM config to migrate successfully).

Quoted 2 lines

I don't have a strong opinion on this, will follow whatever direction you advise.

(a) is an existing bug (and rare, it seems: I didn't see it being reported anywhere). (b) would be a regression in a -stable update. I'd prefer to be conservative on -stable.

Quoted 22 lines
Quoted 5 lines
Quoted 5 lines

 target/i386/kvm.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
Quoted 16 lines

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

Quoted 13 lines
target/i386/kvm.c +4 -1
index fb20ff18c2..c9c359241c 100644
@@ -658,17 +658,34 @@ 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 are not supported by kernel\n");

I would mention the corresponding "hv-..." -cpu option explicitly, for clarity.

Quoted 4 lines

This sounds like a good idea, but I wonder if it should better be done uniformly for all hv_* options, together with transitioning to error_report(), and whether we want to do this at this point in the release cycle.

I don't think it will be a problem if we mention the property names in the new messages, but not try to reword the existing ones.

Signature · 1 line
Eduardo