[PATCH] i386: Make 'hv-reenlightenment' require explicit 'tsc-frequency' setting

Vitaly Kuznetsov posted 1 patch 3 years, 1 month ago
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20210330123603.284354-1-vkuznets@redhat.com
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Eduardo Habkost <ehabkost@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
There is a newer version of this series
docs/hyperv.txt   |  1 +
hw/i386/pc.c      |  1 +
target/i386/cpu.c | 23 +++++++++++++++++++++--
target/i386/cpu.h |  1 +
4 files changed, 24 insertions(+), 2 deletions(-)
[PATCH] i386: Make 'hv-reenlightenment' require explicit 'tsc-frequency' setting
Posted by Vitaly Kuznetsov 3 years, 1 month ago
Commit 561dbb41b1d7 "i386: Make migration fail when Hyper-V reenlightenment
was enabled but 'user_tsc_khz' is unset" forbade migrations with when guest
has opted for reenlightenment notifications but 'tsc-frequency' wasn't set
explicitly on the command line. This works but the migration fail late and
this may come as an unpleasant surprise. To make things more explicit,
require 'tsc-frequency=' on the command line when 'hv-reenlightenment' was
enabled. Make the change affect 6.0+ machine types only to preserve
previously-valid configurations valid.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 docs/hyperv.txt   |  1 +
 hw/i386/pc.c      |  1 +
 target/i386/cpu.c | 23 +++++++++++++++++++++--
 target/i386/cpu.h |  1 +
 4 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/docs/hyperv.txt b/docs/hyperv.txt
index e53c581f4586..5b02d341ab25 100644
--- a/docs/hyperv.txt
+++ b/docs/hyperv.txt
@@ -165,6 +165,7 @@ emulate TSC accesses after migration so 'tsc-frequency=' CPU option also has to
 be specified to make migration succeed. The destination host has to either have
 the same TSC frequency or support TSC scaling CPU feature.
 
+Requires: tsc-frequency
 Recommended: hv-frequencies
 
 3.16. hv-evmcs
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 8a84b25a031e..47b79e949ad7 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -98,6 +98,7 @@
 
 GlobalProperty pc_compat_5_2[] = {
     { "ICH9-LPC", "x-smi-cpu-hotunplug", "off" },
+    { TYPE_X86_CPU, "x-hv-reenlightenment-requires-tscfreq", "off"},
 };
 const size_t pc_compat_5_2_len = G_N_ELEMENTS(pc_compat_5_2);
 
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 6b3e9467f177..751636bafac5 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -6647,10 +6647,23 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)
     }
 }
 
-static void x86_cpu_hyperv_realize(X86CPU *cpu)
+static void x86_cpu_hyperv_realize(X86CPU *cpu, Error **errp)
 {
+    CPUX86State *env = &cpu->env;
     size_t len;
 
+    /*
+     * Reenlightenment requires explicit 'tsc-frequency' setting for successful
+     * migration (see hyperv_reenlightenment_post_load(). As 'hv-passthrough'
+     * mode is not migratable, we can loosen the restriction.
+     */
+    if (hyperv_feat_enabled(cpu, HYPERV_FEAT_REENLIGHTENMENT) &&
+        !cpu->hyperv_passthrough && !env->user_tsc_khz &&
+        cpu->hyperv_reenlightenment_requires_tscfreq) {
+        error_setg(errp, "'hv-reenlightenment' requires 'tsc-frequency=' to be set");
+        return;
+    }
+
     /* Hyper-V vendor id */
     if (!cpu->hyperv_vendor) {
         memcpy(cpu->hyperv_vendor_id, "Microsoft Hv", 12);
@@ -6846,7 +6859,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
     /* Process Hyper-V enlightenments */
-    x86_cpu_hyperv_realize(cpu);
+    x86_cpu_hyperv_realize(cpu, &local_err);
+    if (local_err != NULL) {
+        error_propagate(errp, local_err);
+        return;
+    }
 
     cpu_exec_realizefn(cs, &local_err);
     if (local_err != NULL) {
@@ -7374,6 +7391,8 @@ static Property x86_cpu_properties[] = {
     DEFINE_PROP_INT32("x-hv-max-vps", X86CPU, hv_max_vps, -1),
     DEFINE_PROP_BOOL("x-hv-synic-kvm-only", X86CPU, hyperv_synic_kvm_only,
                      false),
+    DEFINE_PROP_BOOL("x-hv-reenlightenment-requires-tscfreq", X86CPU,
+                     hyperv_reenlightenment_requires_tscfreq, true),
     DEFINE_PROP_BOOL("x-intel-pt-auto-level", X86CPU, intel_pt_auto_level,
                      true),
     DEFINE_PROP_END_OF_LIST()
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index 570f916878f9..0196a300f018 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -1677,6 +1677,7 @@ struct X86CPU {
     uint32_t hyperv_spinlock_attempts;
     char *hyperv_vendor;
     bool hyperv_synic_kvm_only;
+    bool hyperv_reenlightenment_requires_tscfreq;
     uint64_t hyperv_features;
     bool hyperv_passthrough;
     OnOffAuto hyperv_no_nonarch_cs;
-- 
2.30.2


Re: [PATCH] i386: Make 'hv-reenlightenment' require explicit 'tsc-frequency' setting
Posted by David Edmondson 3 years, 1 month ago
On Tuesday, 2021-03-30 at 14:36:03 +02, Vitaly Kuznetsov wrote:

> Commit 561dbb41b1d7 "i386: Make migration fail when Hyper-V reenlightenment
> was enabled but 'user_tsc_khz' is unset" forbade migrations with when guest
> has opted for reenlightenment notifications but 'tsc-frequency' wasn't set
> explicitly on the command line. This works but the migration fail late and

s/fail/fails/

> this may come as an unpleasant surprise. To make things more explicit,
> require 'tsc-frequency=' on the command line when 'hv-reenlightenment' was
> enabled. Make the change affect 6.0+ machine types only to preserve
> previously-valid configurations valid.

s/ valid//

>
> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> ---
>  docs/hyperv.txt   |  1 +
>  hw/i386/pc.c      |  1 +
>  target/i386/cpu.c | 23 +++++++++++++++++++++--
>  target/i386/cpu.h |  1 +
>  4 files changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/docs/hyperv.txt b/docs/hyperv.txt
> index e53c581f4586..5b02d341ab25 100644
> --- a/docs/hyperv.txt
> +++ b/docs/hyperv.txt
> @@ -165,6 +165,7 @@ emulate TSC accesses after migration so 'tsc-frequency=' CPU option also has to
>  be specified to make migration succeed. The destination host has to either have
>  the same TSC frequency or support TSC scaling CPU feature.
>  
> +Requires: tsc-frequency
>  Recommended: hv-frequencies
>  
>  3.16. hv-evmcs
> diff --git a/hw/i386/pc.c b/hw/i386/pc.c
> index 8a84b25a031e..47b79e949ad7 100644
> --- a/hw/i386/pc.c
> +++ b/hw/i386/pc.c
> @@ -98,6 +98,7 @@
>  
>  GlobalProperty pc_compat_5_2[] = {
>      { "ICH9-LPC", "x-smi-cpu-hotunplug", "off" },
> +    { TYPE_X86_CPU, "x-hv-reenlightenment-requires-tscfreq", "off"},
>  };
>  const size_t pc_compat_5_2_len = G_N_ELEMENTS(pc_compat_5_2);
>  
> diff --git a/target/i386/cpu.c b/target/i386/cpu.c
> index 6b3e9467f177..751636bafac5 100644
> --- a/target/i386/cpu.c
> +++ b/target/i386/cpu.c
> @@ -6647,10 +6647,23 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)
>      }
>  }
>  
> -static void x86_cpu_hyperv_realize(X86CPU *cpu)
> +static void x86_cpu_hyperv_realize(X86CPU *cpu, Error **errp)
>  {
> +    CPUX86State *env = &cpu->env;
>      size_t len;
>  
> +    /*
> +     * Reenlightenment requires explicit 'tsc-frequency' setting for successful
> +     * migration (see hyperv_reenlightenment_post_load(). As 'hv-passthrough'

s/post_load()/post_load())/

> +     * mode is not migratable, we can loosen the restriction.
> +     */
> +    if (hyperv_feat_enabled(cpu, HYPERV_FEAT_REENLIGHTENMENT) &&
> +        !cpu->hyperv_passthrough && !env->user_tsc_khz &&
> +        cpu->hyperv_reenlightenment_requires_tscfreq) {
> +        error_setg(errp, "'hv-reenlightenment' requires 'tsc-frequency=' to be set");

s/=//

> +        return;
> +    }
> +
>      /* Hyper-V vendor id */
>      if (!cpu->hyperv_vendor) {
>          memcpy(cpu->hyperv_vendor_id, "Microsoft Hv", 12);
> @@ -6846,7 +6859,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
>      }
>  
>      /* Process Hyper-V enlightenments */
> -    x86_cpu_hyperv_realize(cpu);
> +    x86_cpu_hyperv_realize(cpu, &local_err);
> +    if (local_err != NULL) {
> +        error_propagate(errp, local_err);
> +        return;
> +    }
>  
>      cpu_exec_realizefn(cs, &local_err);
>      if (local_err != NULL) {
> @@ -7374,6 +7391,8 @@ static Property x86_cpu_properties[] = {
>      DEFINE_PROP_INT32("x-hv-max-vps", X86CPU, hv_max_vps, -1),
>      DEFINE_PROP_BOOL("x-hv-synic-kvm-only", X86CPU, hyperv_synic_kvm_only,
>                       false),
> +    DEFINE_PROP_BOOL("x-hv-reenlightenment-requires-tscfreq", X86CPU,
> +                     hyperv_reenlightenment_requires_tscfreq, true),
>      DEFINE_PROP_BOOL("x-intel-pt-auto-level", X86CPU, intel_pt_auto_level,
>                       true),
>      DEFINE_PROP_END_OF_LIST()
> diff --git a/target/i386/cpu.h b/target/i386/cpu.h
> index 570f916878f9..0196a300f018 100644
> --- a/target/i386/cpu.h
> +++ b/target/i386/cpu.h
> @@ -1677,6 +1677,7 @@ struct X86CPU {
>      uint32_t hyperv_spinlock_attempts;
>      char *hyperv_vendor;
>      bool hyperv_synic_kvm_only;
> +    bool hyperv_reenlightenment_requires_tscfreq;
>      uint64_t hyperv_features;
>      bool hyperv_passthrough;
>      OnOffAuto hyperv_no_nonarch_cs;
> -- 
> 2.30.2

dme.
-- 
No proper time of day.

Re: [PATCH] i386: Make 'hv-reenlightenment' require explicit 'tsc-frequency' setting
Posted by Dr. David Alan Gilbert 3 years, 1 month ago
* Vitaly Kuznetsov (vkuznets@redhat.com) wrote:
> Commit 561dbb41b1d7 "i386: Make migration fail when Hyper-V reenlightenment
> was enabled but 'user_tsc_khz' is unset" forbade migrations with when guest
> has opted for reenlightenment notifications but 'tsc-frequency' wasn't set
> explicitly on the command line. This works but the migration fail late and
> this may come as an unpleasant surprise. To make things more explicit,
> require 'tsc-frequency=' on the command line when 'hv-reenlightenment' was
> enabled. Make the change affect 6.0+ machine types only to preserve
> previously-valid configurations valid.
> 
> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>

That looks better for me from a migration point of view:


Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>

> ---
>  docs/hyperv.txt   |  1 +
>  hw/i386/pc.c      |  1 +
>  target/i386/cpu.c | 23 +++++++++++++++++++++--
>  target/i386/cpu.h |  1 +
>  4 files changed, 24 insertions(+), 2 deletions(-)
> 
> diff --git a/docs/hyperv.txt b/docs/hyperv.txt
> index e53c581f4586..5b02d341ab25 100644
> --- a/docs/hyperv.txt
> +++ b/docs/hyperv.txt
> @@ -165,6 +165,7 @@ emulate TSC accesses after migration so 'tsc-frequency=' CPU option also has to
>  be specified to make migration succeed. The destination host has to either have
>  the same TSC frequency or support TSC scaling CPU feature.
>  
> +Requires: tsc-frequency
>  Recommended: hv-frequencies
>  
>  3.16. hv-evmcs
> diff --git a/hw/i386/pc.c b/hw/i386/pc.c
> index 8a84b25a031e..47b79e949ad7 100644
> --- a/hw/i386/pc.c
> +++ b/hw/i386/pc.c
> @@ -98,6 +98,7 @@
>  
>  GlobalProperty pc_compat_5_2[] = {
>      { "ICH9-LPC", "x-smi-cpu-hotunplug", "off" },
> +    { TYPE_X86_CPU, "x-hv-reenlightenment-requires-tscfreq", "off"},
>  };
>  const size_t pc_compat_5_2_len = G_N_ELEMENTS(pc_compat_5_2);
>  
> diff --git a/target/i386/cpu.c b/target/i386/cpu.c
> index 6b3e9467f177..751636bafac5 100644
> --- a/target/i386/cpu.c
> +++ b/target/i386/cpu.c
> @@ -6647,10 +6647,23 @@ static void x86_cpu_filter_features(X86CPU *cpu, bool verbose)
>      }
>  }
>  
> -static void x86_cpu_hyperv_realize(X86CPU *cpu)
> +static void x86_cpu_hyperv_realize(X86CPU *cpu, Error **errp)
>  {
> +    CPUX86State *env = &cpu->env;
>      size_t len;
>  
> +    /*
> +     * Reenlightenment requires explicit 'tsc-frequency' setting for successful
> +     * migration (see hyperv_reenlightenment_post_load(). As 'hv-passthrough'
> +     * mode is not migratable, we can loosen the restriction.
> +     */
> +    if (hyperv_feat_enabled(cpu, HYPERV_FEAT_REENLIGHTENMENT) &&
> +        !cpu->hyperv_passthrough && !env->user_tsc_khz &&
> +        cpu->hyperv_reenlightenment_requires_tscfreq) {
> +        error_setg(errp, "'hv-reenlightenment' requires 'tsc-frequency=' to be set");
> +        return;
> +    }
> +
>      /* Hyper-V vendor id */
>      if (!cpu->hyperv_vendor) {
>          memcpy(cpu->hyperv_vendor_id, "Microsoft Hv", 12);
> @@ -6846,7 +6859,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
>      }
>  
>      /* Process Hyper-V enlightenments */
> -    x86_cpu_hyperv_realize(cpu);
> +    x86_cpu_hyperv_realize(cpu, &local_err);
> +    if (local_err != NULL) {
> +        error_propagate(errp, local_err);
> +        return;
> +    }
>  
>      cpu_exec_realizefn(cs, &local_err);
>      if (local_err != NULL) {
> @@ -7374,6 +7391,8 @@ static Property x86_cpu_properties[] = {
>      DEFINE_PROP_INT32("x-hv-max-vps", X86CPU, hv_max_vps, -1),
>      DEFINE_PROP_BOOL("x-hv-synic-kvm-only", X86CPU, hyperv_synic_kvm_only,
>                       false),
> +    DEFINE_PROP_BOOL("x-hv-reenlightenment-requires-tscfreq", X86CPU,
> +                     hyperv_reenlightenment_requires_tscfreq, true),
>      DEFINE_PROP_BOOL("x-intel-pt-auto-level", X86CPU, intel_pt_auto_level,
>                       true),
>      DEFINE_PROP_END_OF_LIST()
> diff --git a/target/i386/cpu.h b/target/i386/cpu.h
> index 570f916878f9..0196a300f018 100644
> --- a/target/i386/cpu.h
> +++ b/target/i386/cpu.h
> @@ -1677,6 +1677,7 @@ struct X86CPU {
>      uint32_t hyperv_spinlock_attempts;
>      char *hyperv_vendor;
>      bool hyperv_synic_kvm_only;
> +    bool hyperv_reenlightenment_requires_tscfreq;
>      uint64_t hyperv_features;
>      bool hyperv_passthrough;
>      OnOffAuto hyperv_no_nonarch_cs;
> -- 
> 2.30.2
> 
-- 
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK