All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH for-2.9 v2 0/2] i386: Don't override -cpu options on -cpu host/max
@ 2017-03-27 14:48 Eduardo Habkost
  2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 1/2] i386: Replace uint32_t* with FeatureWord on feature getter/setter Eduardo Habkost
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Eduardo Habkost @ 2017-03-27 14:48 UTC (permalink / raw)
  To: qemu-devel
  Cc: Paolo Bonzini, Igor Mammedov, Jiri Denemark, Collin L . Walling,
	Richard Henderson, Jason J . Herne

The existing code for "host" and "max" CPU models overrides every
single feature in the CPU object at realize time, even the ones
that were explicitly enabled or disabled by the user using
"feat=on" or "feat=off", while features set using +feat/-feat are
kept.

This means "-cpu host,+invtsc" works as expected, while
"-cpu host,invtsc=on" doesn't.

This was a known bug, already documented in a comment inside
x86_cpu_expand_features(). What makes this bug worse now is that
libvirt 3.0.0 and newer now use "feat=on|off" instead of
+feat/-feat when it detects a QEMU version that supports it (see
libvirt commit d47db7b16dd5422c7e487c8c8ee5b181a2f9cd66).

This series fixes the bug.

Changes v1 -> v2:
* Split changes in two patches (Igor)
* Don't clear existing bits on env->features (they are already
  supposed to be all zeroes) (Igor)
* Fix typo on comment (Igor)

v1 -> v2 diff:

  diff --git a/target/i386/cpu.c b/target/i386/cpu.c
  index 5f2addbf75..13c0985f11 100644
  --- a/target/i386/cpu.c
  +++ b/target/i386/cpu.c
  @@ -3380,10 +3380,9 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)
        */
       if (cpu->max_features) {
           for (w = 0; w < FEATURE_WORDS; w++) {
  -            /* Override only features that weren't not set explicitly
  +            /* Override only features that weren't set explicitly
                * by the user.
                */
  -            env->features[w] &= env->user_features[w];
               env->features[w] |=
                   x86_cpu_get_supported_feature_word(w, cpu->migratable) &
                   ~env->user_features[w];

Eduardo Habkost (2):
  i386: Replace uint32_t* with FeatureWord on feature getter/setter
  i386: Don't override -cpu options on -cpu host/max

 target/i386/cpu.h |  2 ++
 target/i386/cpu.c | 32 ++++++++++++++++++++------------
 2 files changed, 22 insertions(+), 12 deletions(-)

-- 
2.11.0.259.g40922b1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Qemu-devel] [PATCH for-2.9 v2 1/2] i386: Replace uint32_t* with FeatureWord on feature getter/setter
  2017-03-27 14:48 [Qemu-devel] [PATCH for-2.9 v2 0/2] i386: Don't override -cpu options on -cpu host/max Eduardo Habkost
@ 2017-03-27 14:48 ` Eduardo Habkost
  2017-03-28 10:25   ` Igor Mammedov
  2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 2/2] i386: Don't override -cpu options on -cpu host/max Eduardo Habkost
  2017-03-28 12:46 ` [Qemu-devel] [PATCH for-2.9 v2 0/2] " Jiri Denemark
  2 siblings, 1 reply; 6+ messages in thread
From: Eduardo Habkost @ 2017-03-27 14:48 UTC (permalink / raw)
  To: qemu-devel
  Cc: Paolo Bonzini, Igor Mammedov, Jiri Denemark, Collin L . Walling,
	Richard Henderson, Jason J . Herne

Instead of passing a pointer to the feature property getter and
setter functions, pass a FeatureWord enum so they can perform
other actions related to the feature flag.

This will be used to add a new "user_features" field to keep
track of features that were explicitly set by the user.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 target/i386/cpu.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index 7aa762245a..feefa5b8a4 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -3692,15 +3692,17 @@ static void x86_cpu_unrealizefn(DeviceState *dev, Error **errp)
 }
 
 typedef struct BitProperty {
-    uint32_t *ptr;
+    FeatureWord w;
     uint32_t mask;
 } BitProperty;
 
 static void x86_cpu_get_bit_prop(Object *obj, Visitor *v, const char *name,
                                  void *opaque, Error **errp)
 {
+    X86CPU *cpu = X86_CPU(obj);
     BitProperty *fp = opaque;
-    bool value = (*fp->ptr & fp->mask) == fp->mask;
+    uint32_t f = cpu->env.features[fp->w];
+    bool value = (f & fp->mask) == fp->mask;
     visit_type_bool(v, name, &value, errp);
 }
 
@@ -3708,6 +3710,7 @@ static void x86_cpu_set_bit_prop(Object *obj, Visitor *v, const char *name,
                                  void *opaque, Error **errp)
 {
     DeviceState *dev = DEVICE(obj);
+    X86CPU *cpu = X86_CPU(obj);
     BitProperty *fp = opaque;
     Error *local_err = NULL;
     bool value;
@@ -3724,9 +3727,9 @@ static void x86_cpu_set_bit_prop(Object *obj, Visitor *v, const char *name,
     }
 
     if (value) {
-        *fp->ptr |= fp->mask;
+        cpu->env.features[fp->w] |= fp->mask;
     } else {
-        *fp->ptr &= ~fp->mask;
+        cpu->env.features[fp->w] &= ~fp->mask;
     }
 }
 
@@ -3745,7 +3748,7 @@ static void x86_cpu_release_bit_prop(Object *obj, const char *name,
  */
 static void x86_cpu_register_bit_prop(X86CPU *cpu,
                                       const char *prop_name,
-                                      uint32_t *field,
+                                      FeatureWord w,
                                       int bitnr)
 {
     BitProperty *fp;
@@ -3755,11 +3758,11 @@ static void x86_cpu_register_bit_prop(X86CPU *cpu,
     op = object_property_find(OBJECT(cpu), prop_name, NULL);
     if (op) {
         fp = op->opaque;
-        assert(fp->ptr == field);
+        assert(fp->w == w);
         fp->mask |= mask;
     } else {
         fp = g_new0(BitProperty, 1);
-        fp->ptr = field;
+        fp->w = w;
         fp->mask = mask;
         object_property_add(OBJECT(cpu), prop_name, "bool",
                             x86_cpu_get_bit_prop,
@@ -3787,7 +3790,7 @@ static void x86_cpu_register_feature_bit_props(X86CPU *cpu,
     /* aliases don't use "|" delimiters anymore, they are registered
      * manually using object_property_add_alias() */
     assert(!strchr(name, '|'));
-    x86_cpu_register_bit_prop(cpu, name, &cpu->env.features[w], bitnr);
+    x86_cpu_register_bit_prop(cpu, name, w, bitnr);
 }
 
 static GuestPanicInformation *x86_cpu_get_crash_info(CPUState *cs)
-- 
2.11.0.259.g40922b1

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [Qemu-devel] [PATCH for-2.9 v2 2/2] i386: Don't override -cpu options on -cpu host/max
  2017-03-27 14:48 [Qemu-devel] [PATCH for-2.9 v2 0/2] i386: Don't override -cpu options on -cpu host/max Eduardo Habkost
  2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 1/2] i386: Replace uint32_t* with FeatureWord on feature getter/setter Eduardo Habkost
@ 2017-03-27 14:48 ` Eduardo Habkost
  2017-03-28 10:31   ` Igor Mammedov
  2017-03-28 12:46 ` [Qemu-devel] [PATCH for-2.9 v2 0/2] " Jiri Denemark
  2 siblings, 1 reply; 6+ messages in thread
From: Eduardo Habkost @ 2017-03-27 14:48 UTC (permalink / raw)
  To: qemu-devel
  Cc: Paolo Bonzini, Igor Mammedov, Jiri Denemark, Collin L . Walling,
	Richard Henderson, Jason J . Herne

The existing code for "host" and "max" CPU models overrides every
single feature in the CPU object at realize time, even the ones
that were explicitly enabled or disabled by the user using
"feat=on" or "feat=off", while features set using +feat/-feat are
kept.

This means "-cpu host,+invtsc" works as expected, while
"-cpu host,invtsc=on" doesn't.

This was a known bug, already documented in a comment inside
x86_cpu_expand_features(). What makes this bug worse now is that
libvirt 3.0.0 and newer now use "feat=on|off" instead of
+feat/-feat when it detects a QEMU version that supports it (see
libvirt commit d47db7b16dd5422c7e487c8c8ee5b181a2f9cd66).

Change the feature property getter/setter to set a
env->user_features field, to keep track of features that were
explicitly changed using QOM properties. Then make the
max_features code not override user features when handling "-cpu
host" and "-cpu max".

This will also allow us to remove the plus_features/minus_features
hack in the future, but I plan to do that after 2.9.0 is
released.

Reported-by: Jiri Denemark <jdenemar@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
Changes v1 -> v2:
* Don't clear existing bits on env->features (they are already
  supposed to be all zeroes) (Igor)
* Fix typo on comment (Igor)
* Moved feature word getter/setter parameter changes to separate
  patch (Igor)
---
 target/i386/cpu.h |  2 ++
 target/i386/cpu.c | 13 +++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index 07401ad9fe..c4602ca80d 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -1147,6 +1147,8 @@ typedef struct CPUX86State {
     uint32_t cpuid_vendor3;
     uint32_t cpuid_version;
     FeatureWordArray features;
+    /* Features that were explicitly enabled/disabled */
+    FeatureWordArray user_features;
     uint32_t cpuid_model[12];
 
     /* MTRRs */
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
index feefa5b8a4..13c0985f11 100644
--- a/target/i386/cpu.c
+++ b/target/i386/cpu.c
@@ -3373,15 +3373,19 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)
     GList *l;
     Error *local_err = NULL;
 
-    /*TODO: cpu->max_features incorrectly overwrites features
-     * set using "feat=on|off". Once we fix this, we can convert
+    /*TODO: Now cpu->max_features doesn't overwrite features
+     * set using QOM properties, and we can convert
      * plus_features & minus_features to global properties
      * inside x86_cpu_parse_featurestr() too.
      */
     if (cpu->max_features) {
         for (w = 0; w < FEATURE_WORDS; w++) {
-            env->features[w] =
-                x86_cpu_get_supported_feature_word(w, cpu->migratable);
+            /* Override only features that weren't set explicitly
+             * by the user.
+             */
+            env->features[w] |=
+                x86_cpu_get_supported_feature_word(w, cpu->migratable) &
+                ~env->user_features[w];
         }
     }
 
@@ -3731,6 +3735,7 @@ static void x86_cpu_set_bit_prop(Object *obj, Visitor *v, const char *name,
     } else {
         cpu->env.features[fp->w] &= ~fp->mask;
     }
+    cpu->env.user_features[fp->w] |= fp->mask;
 }
 
 static void x86_cpu_release_bit_prop(Object *obj, const char *name,
-- 
2.11.0.259.g40922b1

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH for-2.9 v2 1/2] i386: Replace uint32_t* with FeatureWord on feature getter/setter
  2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 1/2] i386: Replace uint32_t* with FeatureWord on feature getter/setter Eduardo Habkost
@ 2017-03-28 10:25   ` Igor Mammedov
  0 siblings, 0 replies; 6+ messages in thread
From: Igor Mammedov @ 2017-03-28 10:25 UTC (permalink / raw)
  To: Eduardo Habkost
  Cc: qemu-devel, Paolo Bonzini, Jiri Denemark, Collin L . Walling,
	Richard Henderson, Jason J . Herne

On Mon, 27 Mar 2017 11:48:14 -0300
Eduardo Habkost <ehabkost@redhat.com> wrote:

> Instead of passing a pointer to the feature property getter and
> setter functions, pass a FeatureWord enum so they can perform
> other actions related to the feature flag.
> 
> This will be used to add a new "user_features" field to keep
> track of features that were explicitly set by the user.
> 
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>

> ---
>  target/i386/cpu.c | 19 +++++++++++--------
>  1 file changed, 11 insertions(+), 8 deletions(-)
> 
> diff --git a/target/i386/cpu.c b/target/i386/cpu.c
> index 7aa762245a..feefa5b8a4 100644
> --- a/target/i386/cpu.c
> +++ b/target/i386/cpu.c
> @@ -3692,15 +3692,17 @@ static void x86_cpu_unrealizefn(DeviceState *dev, Error **errp)
>  }
>  
>  typedef struct BitProperty {
> -    uint32_t *ptr;
> +    FeatureWord w;
>      uint32_t mask;
>  } BitProperty;
>  
>  static void x86_cpu_get_bit_prop(Object *obj, Visitor *v, const char *name,
>                                   void *opaque, Error **errp)
>  {
> +    X86CPU *cpu = X86_CPU(obj);
>      BitProperty *fp = opaque;
> -    bool value = (*fp->ptr & fp->mask) == fp->mask;
> +    uint32_t f = cpu->env.features[fp->w];
> +    bool value = (f & fp->mask) == fp->mask;
>      visit_type_bool(v, name, &value, errp);
>  }
>  
> @@ -3708,6 +3710,7 @@ static void x86_cpu_set_bit_prop(Object *obj, Visitor *v, const char *name,
>                                   void *opaque, Error **errp)
>  {
>      DeviceState *dev = DEVICE(obj);
> +    X86CPU *cpu = X86_CPU(obj);
>      BitProperty *fp = opaque;
>      Error *local_err = NULL;
>      bool value;
> @@ -3724,9 +3727,9 @@ static void x86_cpu_set_bit_prop(Object *obj, Visitor *v, const char *name,
>      }
>  
>      if (value) {
> -        *fp->ptr |= fp->mask;
> +        cpu->env.features[fp->w] |= fp->mask;
>      } else {
> -        *fp->ptr &= ~fp->mask;
> +        cpu->env.features[fp->w] &= ~fp->mask;
>      }
>  }
>  
> @@ -3745,7 +3748,7 @@ static void x86_cpu_release_bit_prop(Object *obj, const char *name,
>   */
>  static void x86_cpu_register_bit_prop(X86CPU *cpu,
>                                        const char *prop_name,
> -                                      uint32_t *field,
> +                                      FeatureWord w,
>                                        int bitnr)
>  {
>      BitProperty *fp;
> @@ -3755,11 +3758,11 @@ static void x86_cpu_register_bit_prop(X86CPU *cpu,
>      op = object_property_find(OBJECT(cpu), prop_name, NULL);
>      if (op) {
>          fp = op->opaque;
> -        assert(fp->ptr == field);
> +        assert(fp->w == w);
>          fp->mask |= mask;
>      } else {
>          fp = g_new0(BitProperty, 1);
> -        fp->ptr = field;
> +        fp->w = w;
>          fp->mask = mask;
>          object_property_add(OBJECT(cpu), prop_name, "bool",
>                              x86_cpu_get_bit_prop,
> @@ -3787,7 +3790,7 @@ static void x86_cpu_register_feature_bit_props(X86CPU *cpu,
>      /* aliases don't use "|" delimiters anymore, they are registered
>       * manually using object_property_add_alias() */
>      assert(!strchr(name, '|'));
> -    x86_cpu_register_bit_prop(cpu, name, &cpu->env.features[w], bitnr);
> +    x86_cpu_register_bit_prop(cpu, name, w, bitnr);
>  }
>  
>  static GuestPanicInformation *x86_cpu_get_crash_info(CPUState *cs)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH for-2.9 v2 2/2] i386: Don't override -cpu options on -cpu host/max
  2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 2/2] i386: Don't override -cpu options on -cpu host/max Eduardo Habkost
@ 2017-03-28 10:31   ` Igor Mammedov
  0 siblings, 0 replies; 6+ messages in thread
From: Igor Mammedov @ 2017-03-28 10:31 UTC (permalink / raw)
  To: Eduardo Habkost
  Cc: qemu-devel, Paolo Bonzini, Jiri Denemark, Collin L . Walling,
	Richard Henderson, Jason J . Herne

On Mon, 27 Mar 2017 11:48:15 -0300
Eduardo Habkost <ehabkost@redhat.com> wrote:

> The existing code for "host" and "max" CPU models overrides every
> single feature in the CPU object at realize time, even the ones
> that were explicitly enabled or disabled by the user using
> "feat=on" or "feat=off", while features set using +feat/-feat are
> kept.
> 
> This means "-cpu host,+invtsc" works as expected, while
> "-cpu host,invtsc=on" doesn't.
> 
> This was a known bug, already documented in a comment inside
> x86_cpu_expand_features(). What makes this bug worse now is that
> libvirt 3.0.0 and newer now use "feat=on|off" instead of
> +feat/-feat when it detects a QEMU version that supports it (see
> libvirt commit d47db7b16dd5422c7e487c8c8ee5b181a2f9cd66).
> 
> Change the feature property getter/setter to set a
> env->user_features field, to keep track of features that were
> explicitly changed using QOM properties. Then make the
> max_features code not override user features when handling "-cpu
> host" and "-cpu max".
> 
> This will also allow us to remove the plus_features/minus_features
> hack in the future, but I plan to do that after 2.9.0 is
> released.
> 
> Reported-by: Jiri Denemark <jdenemar@redhat.com>
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>

> ---
> Changes v1 -> v2:
> * Don't clear existing bits on env->features (they are already
>   supposed to be all zeroes) (Igor)
> * Fix typo on comment (Igor)
> * Moved feature word getter/setter parameter changes to separate
>   patch (Igor)
> ---
>  target/i386/cpu.h |  2 ++
>  target/i386/cpu.c | 13 +++++++++----
>  2 files changed, 11 insertions(+), 4 deletions(-)
> 
> diff --git a/target/i386/cpu.h b/target/i386/cpu.h
> index 07401ad9fe..c4602ca80d 100644
> --- a/target/i386/cpu.h
> +++ b/target/i386/cpu.h
> @@ -1147,6 +1147,8 @@ typedef struct CPUX86State {
>      uint32_t cpuid_vendor3;
>      uint32_t cpuid_version;
>      FeatureWordArray features;
> +    /* Features that were explicitly enabled/disabled */
> +    FeatureWordArray user_features;
>      uint32_t cpuid_model[12];
>  
>      /* MTRRs */
> diff --git a/target/i386/cpu.c b/target/i386/cpu.c
> index feefa5b8a4..13c0985f11 100644
> --- a/target/i386/cpu.c
> +++ b/target/i386/cpu.c
> @@ -3373,15 +3373,19 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)
>      GList *l;
>      Error *local_err = NULL;
>  
> -    /*TODO: cpu->max_features incorrectly overwrites features
> -     * set using "feat=on|off". Once we fix this, we can convert
> +    /*TODO: Now cpu->max_features doesn't overwrite features
> +     * set using QOM properties, and we can convert
>       * plus_features & minus_features to global properties
>       * inside x86_cpu_parse_featurestr() too.
>       */
>      if (cpu->max_features) {
>          for (w = 0; w < FEATURE_WORDS; w++) {
> -            env->features[w] =
> -                x86_cpu_get_supported_feature_word(w, cpu->migratable);
> +            /* Override only features that weren't set explicitly
> +             * by the user.
> +             */
> +            env->features[w] |=
> +                x86_cpu_get_supported_feature_word(w, cpu->migratable) &
> +                ~env->user_features[w];
>          }
>      }
>  
> @@ -3731,6 +3735,7 @@ static void x86_cpu_set_bit_prop(Object *obj, Visitor *v, const char *name,
>      } else {
>          cpu->env.features[fp->w] &= ~fp->mask;
>      }
> +    cpu->env.user_features[fp->w] |= fp->mask;
>  }
>  
>  static void x86_cpu_release_bit_prop(Object *obj, const char *name,

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH for-2.9 v2 0/2] i386: Don't override -cpu options on -cpu host/max
  2017-03-27 14:48 [Qemu-devel] [PATCH for-2.9 v2 0/2] i386: Don't override -cpu options on -cpu host/max Eduardo Habkost
  2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 1/2] i386: Replace uint32_t* with FeatureWord on feature getter/setter Eduardo Habkost
  2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 2/2] i386: Don't override -cpu options on -cpu host/max Eduardo Habkost
@ 2017-03-28 12:46 ` Jiri Denemark
  2 siblings, 0 replies; 6+ messages in thread
From: Jiri Denemark @ 2017-03-28 12:46 UTC (permalink / raw)
  To: Eduardo Habkost
  Cc: qemu-devel, Paolo Bonzini, Igor Mammedov, Collin L . Walling,
	Richard Henderson, Jason J . Herne

On Mon, Mar 27, 2017 at 11:48:13 -0300, Eduardo Habkost wrote:
> The existing code for "host" and "max" CPU models overrides every
> single feature in the CPU object at realize time, even the ones
> that were explicitly enabled or disabled by the user using
> "feat=on" or "feat=off", while features set using +feat/-feat are
> kept.
> 
> This means "-cpu host,+invtsc" works as expected, while
> "-cpu host,invtsc=on" doesn't.
> 
> This was a known bug, already documented in a comment inside
> x86_cpu_expand_features(). What makes this bug worse now is that
> libvirt 3.0.0 and newer now use "feat=on|off" instead of
> +feat/-feat when it detects a QEMU version that supports it (see
> libvirt commit d47db7b16dd5422c7e487c8c8ee5b181a2f9cd66).
> 
> This series fixes the bug.

Thanks.

Tested-by: Jiri Denemark <jdenemar@redhat.com>

Jirka

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-03-28 12:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-27 14:48 [Qemu-devel] [PATCH for-2.9 v2 0/2] i386: Don't override -cpu options on -cpu host/max Eduardo Habkost
2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 1/2] i386: Replace uint32_t* with FeatureWord on feature getter/setter Eduardo Habkost
2017-03-28 10:25   ` Igor Mammedov
2017-03-27 14:48 ` [Qemu-devel] [PATCH for-2.9 v2 2/2] i386: Don't override -cpu options on -cpu host/max Eduardo Habkost
2017-03-28 10:31   ` Igor Mammedov
2017-03-28 12:46 ` [Qemu-devel] [PATCH for-2.9 v2 0/2] " Jiri Denemark

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.