Skip to contentSkip to conversation

Machine type compatible properties

PATCH In review Maxim Davydov 5 participants 32 messages Mar 28, 2022 – Apr 21, 2022 Networking

lore
Share & tools
lore
#0 lore raw

cover Machine type compatible properties

We need to be able to check machine type after its definition. It's necessary when using complicated inheritance of compatible features. For instance, this tool can help to find bugs in the machine type definition if the name of the device has been changed. Also, this tool was created to help with MTs of other projects such as vz branches.

Maxim Davydov (9):
qmp: Add dump machine type compatible properties
pci: add null-pointer check
mem: appropriate handling getting mem region
msmouse: add appropriate unregister handler
wctablet: add appropriate unregister handler
chardev: add appropriate getting address
colo-compare: safe finalization
qom: add command to print initial properties
scripts: printing machine type compat properties

 chardev/char-socket.c       |   9 ++
 chardev/msmouse.c           |   4 +-
 chardev/wctablet.c          |   4 +-
 hw/core/machine-qmp-cmds.c  |  25 +++-
 hw/i386/sgx-epc.c           |   5 +-
 hw/mem/nvdimm.c             |   6 +
 hw/mem/pc-dimm.c            |   5 +
 hw/pci-host/i440fx.c        |  17 ++-
 hw/pci-host/q35.c           |  17 ++-
 net/colo-compare.c          |  25 ++--
 qapi/machine.json           |  58 +++++++-
 qapi/qom.json               |  69 +++++++++
 qom/qom-qmp-cmds.c          | 121 ++++++++++++++++
 scripts/print_MT.py         | 274 ++++++++++++++++++++++++++++++++++++
 tests/qtest/fuzz/qos_fuzz.c |   2 +-
 15 files changed, 613 insertions(+), 28 deletions(-)
 create mode 100755 scripts/print_MT.py
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/9 qmp: Add dump machine type compatible properties

This patch adds the ability to get all the compat_props of the corresponding supported machines for their comparison.

Example:
{ "execute" : "query-machines", "arguments" : { "is-full" : true } }

---
 hw/core/machine-qmp-cmds.c  | 25 +++++++++++++++-
 qapi/machine.json           | 58 +++++++++++++++++++++++++++++++++++--
 tests/qtest/fuzz/qos_fuzz.c |  2 +-
 3 files changed, 81 insertions(+), 4 deletions(-)
hw/core/machine-qmp-cmds.c +24 -1
index 4f4ab30f8c..8f3206ba8d 100644
@@ -74,7 +74,8 @@ CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
     return head;
 }
 
-MachineInfoList *qmp_query_machines(Error **errp)
+MachineInfoList *qmp_query_machines(bool has_is_full, bool is_full,
+                                    Error **errp)
 {
     GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
     MachineInfoList *mach_list = NULL;
@@ -107,6 +108,28 @@ MachineInfoList *qmp_query_machines(Error **errp)
             info->default_ram_id = g_strdup(mc->default_ram_id);
             info->has_default_ram_id = true;
         }
+        if (has_is_full && is_full && mc->compat_props) {
+            int i;
+            info->compat_props = NULL;
+            info->has_compat_props = true;
+
+            for (i = 0; i < mc->compat_props->len; i++) {
+                GlobalProperty *mt_prop = g_ptr_array_index(mc->compat_props,
+                                                            i);
+                ObjectClass *klass = object_class_by_name(mt_prop->driver);
+                CompatProperty *prop;
+
+                prop = g_malloc0(sizeof(*prop));
+                if (klass && object_class_is_abstract(klass)) {
+                    prop->abstract = true;
+                }
+                prop->driver = g_strdup(mt_prop->driver);
+                prop->property = g_strdup(mt_prop->property);
+                prop->value = g_strdup(mt_prop->value);
+
+                QAPI_LIST_PREPEND(info->compat_props, prop);
+            }
+        }
 
         QAPI_LIST_PREPEND(mach_list, info);
     }
qapi/machine.json +56 -2

86 lines not on this page · raw message

tests/qtest/fuzz/qos_fuzz.c +1 -1
index 7a244c951e..3f9c1ede6e 100644
@@ -47,7 +47,7 @@ static void qos_set_machines_devices_available(void)
     MachineInfoList *mach_info;
     ObjectTypeInfoList *type_info;
 
-    mach_info = qmp_query_machines(&error_abort);
+    mach_info = qmp_query_machines(false, false, &error_abort);
     machines_apply_to_node(mach_info);
     qapi_free_MachineInfoList(mach_info);
 
Signature · 1 line
2.31.1
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH v1 1/9] qmp: Add dump machine type compatible properties

Quoted 28 lines · Maxim Davydov

This patch adds the ability to get all the compat_props of the corresponding supported machines for their comparison.

Example:
{ "execute" : "query-machines", "arguments" : { "is-full" : true } }

---
  hw/core/machine-qmp-cmds.c  | 25 +++++++++++++++-
  qapi/machine.json           | 58 +++++++++++++++++++++++++++++++++++--
  tests/qtest/fuzz/qos_fuzz.c |  2 +-
  3 files changed, 81 insertions(+), 4 deletions(-)
hw/core/machine-qmp-cmds.c +2 -1
index 4f4ab30f8c..8f3206ba8d 100644
@@ -74,7 +74,8 @@ CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
      return head;
  }
 
-MachineInfoList *qmp_query_machines(Error **errp)
+MachineInfoList *qmp_query_machines(bool has_is_full, bool is_full,
+                                    Error **errp)
  {
      GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
      MachineInfoList *mach_list = NULL;

hw/core/machine-qmp-cmds.c MachineInfoList *qmp_query_machines(Error **errp) · line 111

Quoted 4 lines
hw/core/machine-qmp-cmds.c +1 -0
@@ -107,6 +108,28 @@ MachineInfoList *qmp_query_machines(Error **errp)
              info->default_ram_id = g_strdup(mc->default_ram_id);
              info->has_default_ram_id = true;
          }
+        if (has_is_full && is_full && mc->compat_props) {

is_full is guaranteed to be zero when has_is_full is zero. So, it's enough to write:

   if (is_full && mc->compat_props) {
Quoted 71 lines
hw/core/machine-qmp-cmds.c +21 -0
+            int i;
+            info->compat_props = NULL;
+            info->has_compat_props = true;
+
+            for (i = 0; i < mc->compat_props->len; i++) {
+                GlobalProperty *mt_prop = g_ptr_array_index(mc->compat_props,
+                                                            i);
+                ObjectClass *klass = object_class_by_name(mt_prop->driver);
+                CompatProperty *prop;
+
+                prop = g_malloc0(sizeof(*prop));
+                if (klass && object_class_is_abstract(klass)) {
+                    prop->abstract = true;
+                }
+                prop->driver = g_strdup(mt_prop->driver);
+                prop->property = g_strdup(mt_prop->property);
+                prop->value = g_strdup(mt_prop->value);
+
+                QAPI_LIST_PREPEND(info->compat_props, prop);
+            }
+        }
 
          QAPI_LIST_PREPEND(mach_list, info);
      }
qapi/machine.json +25 -0
index 42fc68403d..16e961477c 100644
@@ -130,6 +130,28 @@
  ##
  { 'command': 'query-cpus-fast', 'returns': [ 'CpuInfoFast' ] }
 
+##
+# @CompatProperty:
+#
+# Machine type compatible property. It's based on GlobalProperty and created
+# for machine type compat properties (see scripts)
+#
+# @driver: name of the driver that has GlobalProperty
+#
+# @abstract: Bool value that shows that property is belonged to abstract class
+#
+# @property: global property name
+#
+# @value: global property value
+#
+# Since: 7.0
+##
+{ 'struct': 'CompatProperty',
+  'data': { 'driver': 'str',
+            'abstract': 'bool',
+            'property': 'str',
+            'value': 'str' } }
+
  ##
  # @MachineInfo:
  #
@@ -158,6 +180,9 @@
  #
  # @default-ram-id: the default ID of initial RAM memory backend (since 5.2)
  #
+# @compat-props: List of compatible properties that defines machine type
+#                (since 7.0)
+#
  # Since: 1.2
  ##
  { 'struct': 'MachineInfo',
@@ -165,18 +190,47 @@
              '*is-default': 'bool', 'cpu-max': 'int',
              'hotpluggable-cpus': 'bool',  'numa-mem-supported': 'bool',
              'deprecated': 'bool', '*default-cpu-type': 'str',

qapi/machine.json line 201

Quoted 10 lines
qapi/machine.json +3 -1
-            '*default-ram-id': 'str' } }
+            '*default-ram-id': 'str', '*compat-props': ['CompatProperty'] } }
 
  ##
  # @query-machines:
  #
  # Return a list of supported machines
  #
+# @is-full: if true return will contain information about machine type
+#           compatible properties (since 7.0)

Should be 7.1.

Also, maybe call it "compat-props" to be consistent with output and with documentation?

Quoted 41 lines
qapi/machine.json +28 -1
+#
  # Returns: a list of MachineInfo
  #
  # Since: 1.2
+#
+# Example:
+#
+# -> { "execute" : "query-machines", "arguments" : { "is-full" : true } }
+# <- { "return": [
+#          {
+#              "hotpluggable-cpus": true,
+#              "name": "pc-q35-6.2",
+#              "compat-props": [
+#                  {
+#                      "abstract": false,
+#                      "driver": "virtio-mem",
+#                      "property": "unplugged-inaccessible",
+#                      "value": "off"
+#                   }
+#               ],
+#               "numa-mem-supported": false,
+#               "default-cpu-type": "qemu64-x86_64-cpu",
+#               "cpu-max": 288,
+#               "deprecated": false,
+#               "default-ram-id": "pc.ram"
+#           },
+#           ...
+#    }
  ##
-{ 'command': 'query-machines', 'returns': ['MachineInfo'] }
+{ 'command': 'query-machines',
+  'data': { '*is-full': 'bool' },
+  'returns': ['MachineInfo'] }
 
  ##
  # @CurrentMachineParams:
tests/qtest/fuzz/qos_fuzz.c +0 -0
index 7a244c951e..3f9c1ede6e 100644

tests/qtest/fuzz/qos_fuzz.c static void qos_set_machines_devices_available(void) · line 53

Quoted 8 lines
tests/qtest/fuzz/qos_fuzz.c +1 -1
@@ -47,7 +47,7 @@ static void qos_set_machines_devices_available(void)
      MachineInfoList *mach_info;
      ObjectTypeInfoList *type_info;
 
-    mach_info = qmp_query_machines(&error_abort);
+    mach_info = qmp_query_machines(false, false, &error_abort);
      machines_apply_to_node(mach_info);
      qapi_free_MachineInfoList(mach_info);
 

weak:

Signature · 2 lines
Best regards,
Vladimir
#3 lore raw

Re: [PATCH v1 1/9] qmp: Add dump machine type compatible properties

Quoted 176 lines · Vladimir Sementsov-Ogievskiy
Quoted 27 lines · Maxim Davydov

This patch adds the ability to get all the compat_props of the corresponding supported machines for their comparison.

Example:
{ "execute" : "query-machines", "arguments" : { "is-full" : true } }

---
  hw/core/machine-qmp-cmds.c  | 25 +++++++++++++++- qapi/machine.json           | 58 +++++++++++++++++++++++++++++++++++-- tests/qtest/fuzz/qos_fuzz.c |  2 +-

  3 files changed, 81 insertions(+), 4 deletions(-)
hw/core/machine-qmp-cmds.c +2 -0
index 4f4ab30f8c..8f3206ba8d 100644
@@ -74,7 +74,8 @@ CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
       return head;
   }
   -MachineInfoList *qmp_query_machines(Error **errp)
+MachineInfoList *qmp_query_machines(bool has_is_full, bool is_full,
+                                    Error **errp)
   {
       GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
       MachineInfoList *mach_list = NULL;

hw/core/machine-qmp-cmds.c MachineInfoList *qmp_query_machines(Error **errp) · line 111

Quoted 4 lines
hw/core/machine-qmp-cmds.c +1 -0
@@ -107,6 +108,28 @@ MachineInfoList *qmp_query_machines(Error **errp)
               info->default_ram_id = g_strdup(mc->default_ram_id);
               info->has_default_ram_id = true;
           }
+        if (has_is_full && is_full && mc->compat_props) {

is_full is guaranteed to be zero when has_is_full is zero. So, it's enough to write:

   if (is_full && mc->compat_props) {

Quoted 74 lines
hw/core/machine-qmp-cmds.c +21 -0
+            int i;
+            info->compat_props = NULL;
+            info->has_compat_props = true;
+
+            for (i = 0; i < mc->compat_props->len; i++) {
+                GlobalProperty *mt_prop = 
 g_ptr_array_index(mc->compat_props,
+                                                            i);
+                ObjectClass *klass = 
 object_class_by_name(mt_prop->driver);
+                CompatProperty *prop;
+
+                prop = g_malloc0(sizeof(*prop));
+                if (klass && object_class_is_abstract(klass)) {
+                    prop->abstract = true;
+                }
+                prop->driver = g_strdup(mt_prop->driver);
+                prop->property = g_strdup(mt_prop->property);
+                prop->value = g_strdup(mt_prop->value);
+
+                QAPI_LIST_PREPEND(info->compat_props, prop);
+            }
+        }
             QAPI_LIST_PREPEND(mach_list, info);

      }

qapi/machine.json +0 -0
index 42fc68403d..16e961477c 100644
@@ -130,6 +130,28 @@
   ##
   { 'command': 'query-cpus-fast', 'returns': [ 'CpuInfoFast' ] }

  +##

qapi/machine.json +3 -0
+# @CompatProperty:
+#
+# Machine type compatible property. It's based on GlobalProperty and 

created

qapi/machine.json +5 -0
+# for machine type compat properties (see scripts)
+#
+# @driver: name of the driver that has GlobalProperty
+#
+# @abstract: Bool value that shows that property is belonged to 

abstract class

qapi/machine.json +13 -0
+#
+# @property: global property name
+#
+# @value: global property value
+#
+# Since: 7.0
+##
+{ 'struct': 'CompatProperty',
+  'data': { 'driver': 'str',
+            'abstract': 'bool',
+            'property': 'str',
+            'value': 'str' } }
+

  ##
  # @MachineInfo:
  #

qapi/machine.json +1 -0
@@ -158,6 +180,9 @@
   #
   # @default-ram-id: the default ID of initial RAM memory backend (since 5.2)
   #
+# @compat-props: List of compatible properties that defines machine 

type

qapi/machine.json +2 -0
+#                (since 7.0)
+#

  # Since: 1.2
  ##
  { 'struct': 'MachineInfo',

qapi/machine.json +0 -0
@@ -165,18 +190,47 @@
               '*is-default': 'bool', 'cpu-max': 'int',
               'hotpluggable-cpus': 'bool', 'numa-mem-supported': 'bool',
               'deprecated': 'bool', '*default-cpu-type': 'str',

qapi/machine.json line 201

Quoted 10 lines
qapi/machine.json +3 -1
-            '*default-ram-id': 'str' } }
+            '*default-ram-id': 'str', '*compat-props': 
 ['CompatProperty'] } }
     ##
   # @query-machines:
   #
   # Return a list of supported machines
   #
+# @is-full: if true return will contain information about machine type
+#           compatible properties (since 7.0)

Should be 7.1.

Also, maybe call it "compat-props" to be consistent with output and with documentation?

Quoted 41 lines
qapi/machine.json +28 -1
+#
   # Returns: a list of MachineInfo
   #
   # Since: 1.2
+#
+# Example:
+#
+# -> { "execute" : "query-machines", "arguments" : { "is-full" : 
 true } }
+# <- { "return": [
+#          {
+#              "hotpluggable-cpus": true,
+#              "name": "pc-q35-6.2",
+#              "compat-props": [
+#                  {
+#                      "abstract": false,
+#                      "driver": "virtio-mem",
+#                      "property": "unplugged-inaccessible",
+#                      "value": "off"
+#                   }
+#               ],
+#               "numa-mem-supported": false,
+#               "default-cpu-type": "qemu64-x86_64-cpu",
+#               "cpu-max": 288,
+#               "deprecated": false,
+#               "default-ram-id": "pc.ram"
+#           },
+#           ...
+#    }
   ##
-{ 'command': 'query-machines', 'returns': ['MachineInfo'] }
+{ 'command': 'query-machines',
+  'data': { '*is-full': 'bool' },
+  'returns': ['MachineInfo'] }
     ##
   # @CurrentMachineParams:
tests/qtest/fuzz/qos_fuzz.c +0 -0
index 7a244c951e..3f9c1ede6e 100644

tests/qtest/fuzz/qos_fuzz.c static void qos_set_machines_devices_available(void) · line 52

Quoted 6 lines
tests/qtest/fuzz/qos_fuzz.c +1 -0
@@ -47,7 +47,7 @@ static void qos_set_machines_devices_available(void)
       MachineInfoList *mach_info;
       ObjectTypeInfoList *type_info;
   -    mach_info = qmp_query_machines(&error_abort);
+    mach_info = qmp_query_machines(false, false, &error_abort);
       machines_apply_to_node(mach_info);
       qapi_free_MachineInfoList(mach_info);

weak:

Quoted 2 lines

Thank you for the review. Yes, the name "compat-props" seems more appropriate than "is-full". It will be changed in the next version

Signature · 2 lines
Best regards,
Maxim Davydov
#4 lore raw

PATCH 2/9 pci: add null-pointer check

In reply to Maxim Davydov

Call pci_bus_get_w64_range can fail with the segmentation fault. For example, this can happen during attempt to get pci-hole64-end immediately after initialization.

---
 hw/pci-host/i440fx.c | 17 +++++++++++------
 hw/pci-host/q35.c    | 17 +++++++++++------
 2 files changed, 22 insertions(+), 12 deletions(-)
hw/pci-host/i440fx.c +11 -6
index e08716142b..71a114e551 100644
@@ -158,10 +158,12 @@ static uint64_t i440fx_pcihost_get_pci_hole64_start_value(Object *obj)
     PCIHostState *h = PCI_HOST_BRIDGE(obj);
     I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
     Range w64;
-    uint64_t value;
+    uint64_t value = 0;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    }
     if (!value && s->pci_hole64_fix) {
         value = pc_pci_hole64_start();
     }
@@ -191,10 +193,13 @@ static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
     I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
     uint64_t hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
     Range w64;
-    uint64_t value, hole64_end;
+    uint64_t value = 0;
+    uint64_t hole64_end;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    }
     hole64_end = ROUND_UP(hole64_start + s->pci_hole64_size, 1ULL << 30);
     if (s->pci_hole64_fix && value < hole64_end) {
         value = hole64_end;
hw/pci-host/q35.c +11 -6
index ab5a47aff5..d679fd85ef 100644
@@ -124,10 +124,12 @@ static uint64_t q35_host_get_pci_hole64_start_value(Object *obj)
     PCIHostState *h = PCI_HOST_BRIDGE(obj);
     Q35PCIHost *s = Q35_HOST_DEVICE(obj);
     Range w64;
-    uint64_t value;
+    uint64_t value = 0;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    }
     if (!value && s->pci_hole64_fix) {
         value = pc_pci_hole64_start();
     }
@@ -157,10 +159,13 @@ static void q35_host_get_pci_hole64_end(Object *obj, Visitor *v,
     Q35PCIHost *s = Q35_HOST_DEVICE(obj);
     uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
     Range w64;
-    uint64_t value, hole64_end;
+    uint64_t value = 0;
+    uint64_t hole64_end;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    }
     hole64_end = ROUND_UP(hole64_start + s->mch.pci_hole64_size, 1ULL << 30);
     if (s->pci_hole64_fix && value < hole64_end) {
         value = hole64_end;
Signature · 1 line
2.31.1
#5 lore raw

Re: [PATCH v1 2/9] pci: add null-pointer check

Quoted 3 lines · Maxim Davydov

Call pci_bus_get_w64_range can fail with the segmentation fault. For example, this can happen during attempt to get pci-hole64-end immediately after initialization.

So, immediately after initialization, h->bus is NULL?

The significant bit is, is the value which we calculate without h->bus is correct or not? That should be covered by commit message.

Quoted 81 lines
---
  hw/pci-host/i440fx.c | 17 +++++++++++------
  hw/pci-host/q35.c    | 17 +++++++++++------
  2 files changed, 22 insertions(+), 12 deletions(-)
hw/pci-host/i440fx.c +11 -6
index e08716142b..71a114e551 100644
@@ -158,10 +158,12 @@ static uint64_t i440fx_pcihost_get_pci_hole64_start_value(Object *obj)
      PCIHostState *h = PCI_HOST_BRIDGE(obj);
      I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
      Range w64;
-    uint64_t value;
+    uint64_t value = 0;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    }
      if (!value && s->pci_hole64_fix) {
          value = pc_pci_hole64_start();
      }
@@ -191,10 +193,13 @@ static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
      I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
      uint64_t hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
      Range w64;
-    uint64_t value, hole64_end;
+    uint64_t value = 0;
+    uint64_t hole64_end;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    }
      hole64_end = ROUND_UP(hole64_start + s->pci_hole64_size, 1ULL << 30);
      if (s->pci_hole64_fix && value < hole64_end) {
          value = hole64_end;
hw/pci-host/q35.c +11 -6
index ab5a47aff5..d679fd85ef 100644
@@ -124,10 +124,12 @@ static uint64_t q35_host_get_pci_hole64_start_value(Object *obj)
      PCIHostState *h = PCI_HOST_BRIDGE(obj);
      Q35PCIHost *s = Q35_HOST_DEVICE(obj);
      Range w64;
-    uint64_t value;
+    uint64_t value = 0;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    }
      if (!value && s->pci_hole64_fix) {
          value = pc_pci_hole64_start();
      }
@@ -157,10 +159,13 @@ static void q35_host_get_pci_hole64_end(Object *obj, Visitor *v,
      Q35PCIHost *s = Q35_HOST_DEVICE(obj);
      uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
      Range w64;
-    uint64_t value, hole64_end;
+    uint64_t value = 0;
+    uint64_t hole64_end;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    }
      hole64_end = ROUND_UP(hole64_start + s->mch.pci_hole64_size, 1ULL << 30);
      if (s->pci_hole64_fix && value < hole64_end) {
          value = hole64_end;
Signature · 2 lines
Best regards,
Vladimir
#6 lore raw

Re: [PATCH v1 2/9] pci: add null-pointer check

Quoted 8 lines · Vladimir Sementsov-Ogievskiy
Quoted 3 lines · Maxim Davydov

Call pci_bus_get_w64_range can fail with the segmentation fault. For example, this can happen during attempt to get pci-hole64-end immediately after initialization.

So, immediately after initialization, h->bus is NULL?

The significant bit is, is the value which we calculate without h->bus is correct or not? That should be covered by commit message.

For example, object_new_with_class() returns only initialized object (after calling instance_init). It means that pci_root_bus_new() in q35_host_realize() hasn't been called for returned object and pci->bus == NULL. So, if then we try to call q35_host_get_pci_hole64_end() it will fail with segmentation fault in the pci_for_each_device_under_bus() (d = bus->devices[devfn], but bus == NULL). Similarly for i440fx. I'm not sure that it's the correct behavior. To reproduce this situation, run "{'execute' : 'query-init-properties'}" or qmp_query_init_properties() from 8th patch of this series without applying fixes for pci-host.
After this fix, the behavior is the similar as if range_is_empty(&w64) == True, but without SEGFAULT. Although, we can check flag DeviceState.realized to detect unrealized device.

Quoted 84 lines
Quoted 82 lines

---
  hw/pci-host/i440fx.c | 17 +++++++++++------
  hw/pci-host/q35.c    | 17 +++++++++++------

  2 files changed, 22 insertions(+), 12 deletions(-)
hw/pci-host/i440fx.c +5 -2
index e08716142b..71a114e551 100644
@@ -158,10 +158,12 @@ static uint64_t 
 i440fx_pcihost_get_pci_hole64_start_value(Object *obj)
       PCIHostState *h = PCI_HOST_BRIDGE(obj);
       I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
       Range w64;
-    uint64_t value;
+    uint64_t value = 0;
   -    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    }
       if (!value && s->pci_hole64_fix) {
           value = pc_pci_hole64_start();

      }

hw/pci-host/i440fx.c +6 -2
@@ -191,10 +193,13 @@ static void 
 i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
       I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
       uint64_t hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
       Range w64;
-    uint64_t value, hole64_end;
+    uint64_t value = 0;
+    uint64_t hole64_end;
   -    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    }
       hole64_end = ROUND_UP(hole64_start + s->pci_hole64_size, 1ULL << 30);
       if (s->pci_hole64_fix && value < hole64_end) {

          value = hole64_end;

hw/pci-host/q35.c +5 -2
index ab5a47aff5..d679fd85ef 100644
@@ -124,10 +124,12 @@ static uint64_t 
 q35_host_get_pci_hole64_start_value(Object *obj)
       PCIHostState *h = PCI_HOST_BRIDGE(obj);
       Q35PCIHost *s = Q35_HOST_DEVICE(obj);
       Range w64;
-    uint64_t value;
+    uint64_t value = 0;
   -    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    }
       if (!value && s->pci_hole64_fix) {
           value = pc_pci_hole64_start();

      }

hw/pci-host/q35.c +6 -2
@@ -157,10 +159,13 @@ static void q35_host_get_pci_hole64_end(Object 
 *obj, Visitor *v,
       Q35PCIHost *s = Q35_HOST_DEVICE(obj);
       uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
       Range w64;
-    uint64_t value, hole64_end;
+    uint64_t value = 0;
+    uint64_t hole64_end;
   -    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    }
       hole64_end = ROUND_UP(hole64_start + s->mch.pci_hole64_size, 1ULL << 30);
       if (s->pci_hole64_fix && value < hole64_end) {

          value = hole64_end;

Signature · 2 lines
Best regards,
Maxim Davydov
#7 lore raw

Re: [PATCH v1 2/9] pci: add null-pointer check

In reply to Maxim Davydov
Quoted 4 lines · On Tue, 29 Mar 2022 00:15:32 +0300 Maxim Davydov <[email protected]>…

Call pci_bus_get_w64_range can fail with the segmentation fault. For example, this can happen during attempt to get pci-hole64-end

" immediately after initialization"

this too vague, pls provide a better description
and is possible a reproducer.

Quoted 81 lines
---
 hw/pci-host/i440fx.c | 17 +++++++++++------
 hw/pci-host/q35.c    | 17 +++++++++++------
 2 files changed, 22 insertions(+), 12 deletions(-)
hw/pci-host/i440fx.c +11 -6
index e08716142b..71a114e551 100644
@@ -158,10 +158,12 @@ static uint64_t i440fx_pcihost_get_pci_hole64_start_value(Object *obj)
     PCIHostState *h = PCI_HOST_BRIDGE(obj);
     I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
     Range w64;
-    uint64_t value;
+    uint64_t value = 0;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    }
     if (!value && s->pci_hole64_fix) {
         value = pc_pci_hole64_start();
     }
@@ -191,10 +193,13 @@ static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
     I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
     uint64_t hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
     Range w64;
-    uint64_t value, hole64_end;
+    uint64_t value = 0;
+    uint64_t hole64_end;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    }
     hole64_end = ROUND_UP(hole64_start + s->pci_hole64_size, 1ULL << 30);
     if (s->pci_hole64_fix && value < hole64_end) {
         value = hole64_end;
hw/pci-host/q35.c +11 -6
index ab5a47aff5..d679fd85ef 100644
@@ -124,10 +124,12 @@ static uint64_t q35_host_get_pci_hole64_start_value(Object *obj)
     PCIHostState *h = PCI_HOST_BRIDGE(obj);
     Q35PCIHost *s = Q35_HOST_DEVICE(obj);
     Range w64;
-    uint64_t value;
+    uint64_t value = 0;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_lob(&w64);
+    }
     if (!value && s->pci_hole64_fix) {
         value = pc_pci_hole64_start();
     }
@@ -157,10 +159,13 @@ static void q35_host_get_pci_hole64_end(Object *obj, Visitor *v,
     Q35PCIHost *s = Q35_HOST_DEVICE(obj);
     uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
     Range w64;
-    uint64_t value, hole64_end;
+    uint64_t value = 0;
+    uint64_t hole64_end;
 
-    pci_bus_get_w64_range(h->bus, &w64);
-    value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    if (h->bus) {
+        pci_bus_get_w64_range(h->bus, &w64);
+        value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
+    }
     hole64_end = ROUND_UP(hole64_start + s->mch.pci_hole64_size, 1ULL << 30);
     if (s->pci_hole64_fix && value < hole64_end) {
         value = hole64_end;
#8 lore raw

PATCH 3/9 mem: appropriate handling getting mem region

In reply to Maxim Davydov

Attempt to get memory region if the device doesn't have hostmem may not be an error. This can be happen immediately after initialization (getting value without default one).

---
 hw/i386/sgx-epc.c | 5 ++++-
 hw/mem/nvdimm.c   | 6 ++++++
 hw/mem/pc-dimm.c  | 5 +++++
 3 files changed, 15 insertions(+), 1 deletion(-)
hw/i386/sgx-epc.c +4 -1
index d664829d35..1a4c8acdcc 100644
@@ -121,9 +121,12 @@ static MemoryRegion *sgx_epc_md_get_memory_region(MemoryDeviceState *md,
 {
     SGXEPCDevice *epc = SGX_EPC(md);
     HostMemoryBackend *hostmem;
+    DeviceState *dev = DEVICE(epc);
 
     if (!epc->hostmem) {
-        error_setg(errp, "'" SGX_EPC_MEMDEV_PROP "' property must be set");
+        if (dev->realized) {
+            error_setg(errp, "'" SGX_EPC_MEMDEV_PROP "' property must be set");
+        }
         return NULL;
     }
 
hw/mem/nvdimm.c +6 -0
index 7c7d777781..61e77e5476 100644
@@ -166,9 +166,15 @@ static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md,
                                                  Error **errp)
 {
     NVDIMMDevice *nvdimm = NVDIMM(md);
+    PCDIMMDevice *dimm = PC_DIMM(nvdimm);
     Error *local_err = NULL;
 
     if (!nvdimm->nvdimm_mr) {
+        /* Not error if we try get memory region after init */
+        if (!dimm->hostmem) {
+            return NULL;
+        }
+
         nvdimm_prepare_memory_region(nvdimm, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
hw/mem/pc-dimm.c +5 -0
index f27e1a11ba..6fd74de97f 100644
@@ -240,6 +240,11 @@ static void pc_dimm_md_set_addr(MemoryDeviceState *md, uint64_t addr,
 static MemoryRegion *pc_dimm_md_get_memory_region(MemoryDeviceState *md,
                                                   Error **errp)
 {
+    PCDIMMDevice *dimm = PC_DIMM(md);
+    /* Not error if we try get memory region after init */
+    if (!dimm->hostmem) {
+        return NULL;
+    }
     return pc_dimm_get_memory_region(PC_DIMM(md), errp);
 }
 
Signature · 1 line
2.31.1
#9 lore raw

Re: [PATCH v1 3/9] mem: appropriate handling getting mem region

Quoted 18 lines · Maxim Davydov

Attempt to get memory region if the device doesn't have hostmem may not be an error. This can be happen immediately after initialization (getting value without default one).

---
  hw/i386/sgx-epc.c | 5 ++++-
  hw/mem/nvdimm.c   | 6 ++++++
  hw/mem/pc-dimm.c  | 5 +++++
  3 files changed, 15 insertions(+), 1 deletion(-)
hw/i386/sgx-epc.c +0 -0
index d664829d35..1a4c8acdcc 100644
@@ -121,9 +121,12 @@ static MemoryRegion *sgx_epc_md_get_memory_region(MemoryDeviceState *md,
  {
      SGXEPCDevice *epc = SGX_EPC(md);

hw/i386/sgx-epc.c static MemoryRegion *sgx_epc_md_get_memory_region(MemoryDeviceState *md, · line 131

Quoted 10 lines
hw/i386/sgx-epc.c +4 -1
      HostMemoryBackend *hostmem;
+    DeviceState *dev = DEVICE(epc);
 
      if (!epc->hostmem) {
-        error_setg(errp, "'" SGX_EPC_MEMDEV_PROP "' property must be set");
+        if (dev->realized) {
+            error_setg(errp, "'" SGX_EPC_MEMDEV_PROP "' property must be set");
+        }
          return NULL;
      }

I can't judge, is it really and error or not.

But the way you change the logic is not correct, as you change the semantics:

Old semantics: on error return NULL and set errp, on success return non-NULL and not set errp

New semantics: on error return NULL and set errp, on success return anything (may be NULL) and not set errp.

Callers are not prepared to this. For example, look at memory_device_unplug:
it does

  mr = mdc->get_memory_region(md, &error_abort);

assume it returns NULL, which is not an error (so we don't crash on error_abort)

and then pass mr to memory_region_del_subregion(), which in turn access mr->container, which will crash if mr is NULL.

Most probably the situation I describe is not possible, but I just want to illustrate the idea.

Moreover, in QEMU functions which has "Error **errp" argument and return pointer are recommended to return NULL on failure and nonNULL on success. In other words, return value of function with "Error **errp" argument should report success/failure information. And having NULL as possible success return value is not recommended, as it's ambiguous and leads to bugs (see big comment at start of include/qapi/error.h).

So, if it's really needed to change the semantics in such not-recommended way, you should check that all callers are OK with it and also describe new semantics in a comment near get_memory_region declaration. But better is deal with returned error as it is.. What is an exact problem you trying to solve with this commit?

Quoted 37 lines
hw/mem/nvdimm.c +6 -0
index 7c7d777781..61e77e5476 100644
@@ -166,9 +166,15 @@ static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md,
                                                   Error **errp)
  {
      NVDIMMDevice *nvdimm = NVDIMM(md);
+    PCDIMMDevice *dimm = PC_DIMM(nvdimm);
      Error *local_err = NULL;
 
      if (!nvdimm->nvdimm_mr) {
+        /* Not error if we try get memory region after init */
+        if (!dimm->hostmem) {
+            return NULL;
+        }
+
          nvdimm_prepare_memory_region(nvdimm, &local_err);
          if (local_err) {
              error_propagate(errp, local_err);
hw/mem/pc-dimm.c +5 -0
index f27e1a11ba..6fd74de97f 100644
@@ -240,6 +240,11 @@ static void pc_dimm_md_set_addr(MemoryDeviceState *md, uint64_t addr,
  static MemoryRegion *pc_dimm_md_get_memory_region(MemoryDeviceState *md,
                                                    Error **errp)
  {
+    PCDIMMDevice *dimm = PC_DIMM(md);
+    /* Not error if we try get memory region after init */
+    if (!dimm->hostmem) {
+        return NULL;
+    }
      return pc_dimm_get_memory_region(PC_DIMM(md), errp);
  }
 
Signature · 2 lines
Best regards,
Vladimir
#10 lore raw

Re: [PATCH v1 3/9] mem: appropriate handling getting mem region

Quoted 47 lines · Vladimir Sementsov-Ogievskiy
Quoted 29 lines · Maxim Davydov

Attempt to get memory region if the device doesn't have hostmem may not be an error. This can be happen immediately after initialization (getting value without default one).

---
  hw/i386/sgx-epc.c | 5 ++++-
  hw/mem/nvdimm.c   | 6 ++++++
  hw/mem/pc-dimm.c  | 5 +++++

  3 files changed, 15 insertions(+), 1 deletion(-)
hw/i386/sgx-epc.c +4 -1
index d664829d35..1a4c8acdcc 100644
@@ -121,9 +121,12 @@ static MemoryRegion 
 *sgx_epc_md_get_memory_region(MemoryDeviceState *md,
   {
       SGXEPCDevice *epc = SGX_EPC(md);
       HostMemoryBackend *hostmem;
+    DeviceState *dev = DEVICE(epc);
         if (!epc->hostmem) {
-        error_setg(errp, "'" SGX_EPC_MEMDEV_PROP "' property must be 
 set");
+        if (dev->realized) {
+            error_setg(errp, "'" SGX_EPC_MEMDEV_PROP "' property 
 must be set");
+        }
           return NULL;
Quoted 1 line

      }

I can't judge, is it really and error or not.

But the way you change the logic is not correct, as you change the semantics:

Old semantics: on error return NULL and set errp, on success return non-NULL and not set errp

New semantics: on error return NULL and set errp, on success return anything (may be NULL) and not set errp.

Callers are not prepared to this. For example, look at memory_device_unplug:
it does

  mr = mdc->get_memory_region(md, &error_abort);

assume it returns NULL, which is not an error (so we don't crash on error_abort)

Quoted 7 lines

and then pass mr  to memory_region_del_subregion(), which in turn access mr->container, which will crash if mr is NULL.

Most probably the situation I describe is not possible, but I just want to illustrate the idea.

Moreover, in QEMU functions which has "Error **errp" argument and return pointer are recommended to return NULL on failure and nonNULL on success. In other words, return value of function with "Error **errp" argument should report success/failure information. And having NULL as possible success return value is not recommended, as it's ambiguous and leads to bugs (see big comment at start of include/qapi/error.h).

So, if it's really needed to change the semantics in such not-recommended way, you should check that all callers are OK with it and also describe new semantics in a comment near get_memory_region declaration. But better is deal with returned error as it is.. What is an exact problem you trying to solve with this commit?

I tried to solve the problem with errors from request MemoryRegion (via *md_get_memory_region()) that was called immediately after object_new_with_class(). But it does seem to change the semantics. Perhaps better solution would be to ignore these errors or to add an exception to handle the object properties correctly.

Quoted 39 lines
Quoted 37 lines

  diff --git a/hw/mem/nvdimm.c b/hw/mem/nvdimm.c
index 7c7d777781..61e77e5476 100644

hw/mem/nvdimm.c +6 -0
@@ -166,9 +166,15 @@ static MemoryRegion 
 *nvdimm_md_get_memory_region(MemoryDeviceState *md,
                                                    Error **errp)
   {
       NVDIMMDevice *nvdimm = NVDIMM(md);
+    PCDIMMDevice *dimm = PC_DIMM(nvdimm);
       Error *local_err = NULL;
         if (!nvdimm->nvdimm_mr) {
+        /* Not error if we try get memory region after init */
+        if (!dimm->hostmem) {
+            return NULL;
+        }
+
           nvdimm_prepare_memory_region(nvdimm, &local_err);
           if (local_err) {
               error_propagate(errp, local_err);
hw/mem/pc-dimm.c +5 -0
index f27e1a11ba..6fd74de97f 100644
@@ -240,6 +240,11 @@ static void 
 pc_dimm_md_set_addr(MemoryDeviceState *md, uint64_t addr,
   static MemoryRegion *pc_dimm_md_get_memory_region(MemoryDeviceState *md,
                                                     Error **errp)
   {
+    PCDIMMDevice *dimm = PC_DIMM(md);
+    /* Not error if we try get memory region after init */
+    if (!dimm->hostmem) {
+        return NULL;
+    }
       return pc_dimm_get_memory_region(PC_DIMM(md), errp);
   }
Signature · 2 lines
Best regards,
Maxim Davydov
#11 lore raw

Re: [PATCH v1 3/9] mem: appropriate handling getting mem region

In reply to Maxim Davydov
Quoted 3 lines · On Tue, 29 Mar 2022 00:15:33 +0300 Maxim Davydov <[email protected]>…

Attempt to get memory region if the device doesn't have hostmem may not be an error. This can be happen immediately after initialization (getting value without default one).

Above statement begs for expanded explanation
Pls rephrase and explain why it's needed and how it will be used.

Quoted 62 lines
---
 hw/i386/sgx-epc.c | 5 ++++-
 hw/mem/nvdimm.c   | 6 ++++++
 hw/mem/pc-dimm.c  | 5 +++++
 3 files changed, 15 insertions(+), 1 deletion(-)
hw/i386/sgx-epc.c +4 -1
index d664829d35..1a4c8acdcc 100644
@@ -121,9 +121,12 @@ static MemoryRegion *sgx_epc_md_get_memory_region(MemoryDeviceState *md,
 {
     SGXEPCDevice *epc = SGX_EPC(md);
     HostMemoryBackend *hostmem;
+    DeviceState *dev = DEVICE(epc);
 
     if (!epc->hostmem) {
-        error_setg(errp, "'" SGX_EPC_MEMDEV_PROP "' property must be set");
+        if (dev->realized) {
+            error_setg(errp, "'" SGX_EPC_MEMDEV_PROP "' property must be set");
+        }
         return NULL;
     }
 
hw/mem/nvdimm.c +6 -0
index 7c7d777781..61e77e5476 100644
@@ -166,9 +166,15 @@ static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md,
                                                  Error **errp)
 {
     NVDIMMDevice *nvdimm = NVDIMM(md);
+    PCDIMMDevice *dimm = PC_DIMM(nvdimm);
     Error *local_err = NULL;
 
     if (!nvdimm->nvdimm_mr) {
+        /* Not error if we try get memory region after init */
+        if (!dimm->hostmem) {
+            return NULL;
+        }
+
         nvdimm_prepare_memory_region(nvdimm, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
hw/mem/pc-dimm.c +5 -0
index f27e1a11ba..6fd74de97f 100644
@@ -240,6 +240,11 @@ static void pc_dimm_md_set_addr(MemoryDeviceState *md, uint64_t addr,
 static MemoryRegion *pc_dimm_md_get_memory_region(MemoryDeviceState *md,
                                                   Error **errp)
 {
+    PCDIMMDevice *dimm = PC_DIMM(md);
+    /* Not error if we try get memory region after init */
+    if (!dimm->hostmem) {
+        return NULL;
+    }
     return pc_dimm_get_memory_region(PC_DIMM(md), errp);
 }
 
#12 lore raw

PATCH 4/9 msmouse: add appropriate unregister handler

In reply to Maxim Davydov

Attempt to finalize msmouse after initalization brings to segmentation fault in QTAILQ_REMOVE.

---
 chardev/msmouse.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/msmouse.c +3 -1
index eb9231dcdb..2cc1b16561 100644
@@ -146,7 +146,9 @@ static void char_msmouse_finalize(Object *obj)
 {
     MouseChardev *mouse = MOUSE_CHARDEV(obj);
 
-    qemu_input_handler_unregister(mouse->hs);
+    if (mouse->hs) {
+        qemu_input_handler_unregister(mouse->hs);
+    }
 }
 
 static QemuInputHandler msmouse_handler = {
Signature · 1 line
2.31.1
Reviewed-by on v1 4/9
#13 lore raw

Re: [PATCH v1 4/9] msmouse: add appropriate unregister handler

Quoted 5 lines · Maxim Davydov

Attempt to finalize msmouse after initalization brings to segmentation fault in QTAILQ_REMOVE.

Quoted 22 lines
---
 chardev/msmouse.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/msmouse.c +3 -2
index eb9231dcdb..2cc1b16561 100644
@@ -146,7 +146,9 @@ static void char_msmouse_finalize(Object *obj)
 {
     MouseChardev *mouse = MOUSE_CHARDEV(obj);
 
-    qemu_input_handler_unregister(mouse->hs);
+    if (mouse->hs) {
+        qemu_input_handler_unregister(mouse->hs);
+    }
 }
 
 static QemuInputHandler msmouse_handler = {
--

2.31.1

#14 lore raw

PATCH 5/9 wctablet: add appropriate unregister handler

In reply to Maxim Davydov

Attempt to finalize msmouse after initalization brings to segmentation fault in QTAILQ_REMOVE.

---
 chardev/wctablet.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/wctablet.c +3 -1
index e8b292c43c..43bdf6b608 100644
@@ -319,7 +319,9 @@ static void wctablet_chr_finalize(Object *obj)
 {
     TabletChardev *tablet = WCTABLET_CHARDEV(obj);
 
-    qemu_input_handler_unregister(tablet->hs);
+    if (tablet->hs) {
+        qemu_input_handler_unregister(tablet->hs);
+    }
 }
 
 static void wctablet_chr_open(Chardev *chr,
Signature · 1 line
2.31.1
Reviewed-by on v1 5/9
#15 lore raw

Re: [PATCH v1 5/9] wctablet: add appropriate unregister handler

Quoted 5 lines · Maxim Davydov

Attempt to finalize msmouse after initalization brings to segmentation fault in QTAILQ_REMOVE.

Quoted 22 lines
---
 chardev/wctablet.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
chardev/wctablet.c +3 -2
index e8b292c43c..43bdf6b608 100644
@@ -319,7 +319,9 @@ static void wctablet_chr_finalize(Object *obj)
 {
     TabletChardev *tablet = WCTABLET_CHARDEV(obj);
 
-    qemu_input_handler_unregister(tablet->hs);
+    if (tablet->hs) {
+        qemu_input_handler_unregister(tablet->hs);
+    }
 }
 
 static void wctablet_chr_open(Chardev *chr,
--

2.31.1

#16 lore raw

PATCH 6/9 chardev: add appropriate getting address

In reply to Maxim Davydov

Attempt to get address after initialization shouldn't fail on assert in the qapi automatically generated code. As a possible solution, it can return null type.

---
 chardev/char-socket.c | 9 +++++++++
 1 file changed, 9 insertions(+)
chardev/char-socket.c +9 -0
index fab2d791d4..f851e3346b 100644
@@ -33,6 +33,7 @@
 #include "qapi/clone-visitor.h"
 #include "qapi/qapi-visit-sockets.h"
 #include "qemu/yank.h"
+#include "qapi/qmp/qnull.h"
 
 #include "chardev/char-io.h"
 #include "chardev/char-socket.h"
@@ -1509,6 +1510,14 @@ char_socket_get_addr(Object *obj, Visitor *v, const char *name,
 {
     SocketChardev *s = SOCKET_CHARDEV(obj);
 
+    QNull *null = NULL;
+
+    /* Return NULL type if getting addr was called after init */
+    if (!s->addr) {
+        visit_type_null(v, NULL, &null, errp);
+        return;
+    }
+
     visit_type_SocketAddress(v, name, &s->addr, errp);
 }
 
Signature · 1 line
2.31.1
#17 lore raw

Re: [PATCH v1 6/9] chardev: add appropriate getting address

Quoted 3 lines · Maxim Davydov

Attempt to get address after initialization shouldn't fail on assert in the qapi automatically generated code. As a possible solution, it can return null type.

But at some point this address appears? May be we try to query it too early, or we need some more initialization steps?

Isn't it better to report failure, when we try to query things that are not yet initialized?

Quoted 33 lines
---
  chardev/char-socket.c | 9 +++++++++
  1 file changed, 9 insertions(+)
chardev/char-socket.c +9 -0
index fab2d791d4..f851e3346b 100644
@@ -33,6 +33,7 @@
  #include "qapi/clone-visitor.h"
  #include "qapi/qapi-visit-sockets.h"
  #include "qemu/yank.h"
+#include "qapi/qmp/qnull.h"
 
  #include "chardev/char-io.h"
  #include "chardev/char-socket.h"
@@ -1509,6 +1510,14 @@ char_socket_get_addr(Object *obj, Visitor *v, const char *name,
  {
      SocketChardev *s = SOCKET_CHARDEV(obj);
 
+    QNull *null = NULL;
+
+    /* Return NULL type if getting addr was called after init */
+    if (!s->addr) {
+        visit_type_null(v, NULL, &null, errp);
+        return;
+    }
+
      visit_type_SocketAddress(v, name, &s->addr, errp);
  }
 
Signature · 2 lines
Best regards,
Vladimir
#18 lore raw

Re: [PATCH v1 6/9] chardev: add appropriate getting address

Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Quoted 3 lines · Maxim Davydov

Attempt to get address after initialization shouldn't fail on assert in the qapi automatically generated code. As a possible solution, it can return null type.

But at some point this address appears? May be we try to query it too early, or we need some more initialization steps?

For example, query address after object_new_with_class(). Without the patch it triggers assert(). I tried to implement the same solution as in hw/ppc/spapr_drc.c:prop_get_fdt

Quoted 2 lines

Isn't it better to report failure, when we try to query things that are not yet initialized?

Yes, maybe it should set errp after visit_type_null. And it should be a common error for unrealized devices to fix the same problem with MemoryRegion, etc.

Quoted 34 lines
Quoted 32 lines

---
  chardev/char-socket.c | 9 +++++++++

  1 file changed, 9 insertions(+)
chardev/char-socket.c +8 -0
index fab2d791d4..f851e3346b 100644
@@ -33,6 +33,7 @@
   #include "qapi/clone-visitor.h"
   #include "qapi/qapi-visit-sockets.h"
   #include "qemu/yank.h"
+#include "qapi/qmp/qnull.h"
     #include "chardev/char-io.h"
   #include "chardev/char-socket.h"
@@ -1509,6 +1510,14 @@ char_socket_get_addr(Object *obj, Visitor *v, 
 const char *name,
   {
       SocketChardev *s = SOCKET_CHARDEV(obj);
   +    QNull *null = NULL;
+
+    /* Return NULL type if getting addr was called after init */
+    if (!s->addr) {
+        visit_type_null(v, NULL, &null, errp);
+        return;
+    }
+
       visit_type_SocketAddress(v, name, &s->addr, errp);
   }
Signature · 2 lines
Best regards,
Maxim Davydov
#19 lore raw

PATCH 7/9 colo-compare: safe finalization

In reply to Maxim Davydov

Fixes some possible issues with finalization. For example, finalization immediately after instance_init fails on the assert.

---
 net/colo-compare.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
net/colo-compare.c +16 -9
index 62554b5b3c..81d8de0aaa 100644
@@ -1426,7 +1426,7 @@ static void colo_compare_finalize(Object *obj)
             break;
         }
     }
-    if (QTAILQ_EMPTY(&net_compares)) {
+    if (QTAILQ_EMPTY(&net_compares) && colo_compare_active) {
         colo_compare_active = false;
         qemu_mutex_destroy(&event_mtx);
         qemu_cond_destroy(&event_complete_cond);
@@ -1442,19 +1442,26 @@ static void colo_compare_finalize(Object *obj)
 
     colo_compare_timer_del(s);
 
-    qemu_bh_delete(s->event_bh);
+    if (s->event_bh) {
+        qemu_bh_delete(s->event_bh);
+    }
 
-    AioContext *ctx = iothread_get_aio_context(s->iothread);
-    aio_context_acquire(ctx);
-    AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
-    if (s->notify_dev) {
-        AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
+    if (s->iothread) {
+        AioContext *ctx = iothread_get_aio_context(s->iothread);
+        aio_context_acquire(ctx);
+        AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
+        if (s->notify_dev) {
+            AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
+        }
+        aio_context_release(ctx);
     }
-    aio_context_release(ctx);
 
     /* Release all unhandled packets after compare thead exited */
     g_queue_foreach(&s->conn_list, colo_flush_packets, s);
-    AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
+    /* Without colo_compare_complete done == false without packets */
+    if (!g_queue_is_empty(&s->out_sendco.send_list)) {
+        AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
+    }
 
     g_queue_clear(&s->conn_list);
     g_queue_clear(&s->out_sendco.send_list);
Signature · 1 line
2.31.1
#20 lore raw

Re: [PATCH v1 7/9] colo-compare: safe finalization

Quoted 43 lines · Maxim Davydov

Fixes some possible issues with finalization. For example, finalization immediately after instance_init fails on the assert.

---
  net/colo-compare.c | 25 ++++++++++++++++---------
  1 file changed, 16 insertions(+), 9 deletions(-)
net/colo-compare.c +12 -7
index 62554b5b3c..81d8de0aaa 100644
@@ -1426,7 +1426,7 @@ static void colo_compare_finalize(Object *obj)
              break;
          }
      }
-    if (QTAILQ_EMPTY(&net_compares)) {
+    if (QTAILQ_EMPTY(&net_compares) && colo_compare_active) {
          colo_compare_active = false;
          qemu_mutex_destroy(&event_mtx);
          qemu_cond_destroy(&event_complete_cond);
@@ -1442,19 +1442,26 @@ static void colo_compare_finalize(Object *obj)
 
      colo_compare_timer_del(s);
 
-    qemu_bh_delete(s->event_bh);
+    if (s->event_bh) {
+        qemu_bh_delete(s->event_bh);
+    }
 
-    AioContext *ctx = iothread_get_aio_context(s->iothread);
-    aio_context_acquire(ctx);
-    AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
-    if (s->notify_dev) {
-        AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
+    if (s->iothread) {
+        AioContext *ctx = iothread_get_aio_context(s->iothread);
+        aio_context_acquire(ctx);
+        AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
+        if (s->notify_dev) {
+            AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
+        }
+        aio_context_release(ctx);

net/colo-compare.c static void colo_compare_finalize(Object *obj) · line 1464

Quoted 10 lines
net/colo-compare.c +4 -2
      }
-    aio_context_release(ctx);
 
      /* Release all unhandled packets after compare thead exited */
      g_queue_foreach(&s->conn_list, colo_flush_packets, s);
-    AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
+    /* Without colo_compare_complete done == false without packets */
+    if (!g_queue_is_empty(&s->out_sendco.send_list)) {
+        AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
+    }

I think, would be good to add more description for this last change. It's not as obvious as previous two changes.

Quoted 3 lines
net/colo-compare.c +0 -0
      g_queue_clear(&s->conn_list);
      g_queue_clear(&s->out_sendco.send_list);
Signature · 2 lines
Best regards,
Vladimir
#21 lore raw

Re: [PATCH v1 7/9] colo-compare: safe finalization

The main problem that if we call object_new_with_class() and then object_unref(), it fails. First of all, this is due to the fact that finalize expects that net/colo-compare.c:colo_compare_complete() has been called before.

Quoted 9 lines · Vladimir Sementsov-Ogievskiy
Quoted 8 lines · Maxim Davydov

Fixes some possible issues with finalization. For example, finalization immediately after instance_init fails on the assert.

---
  net/colo-compare.c | 25 ++++++++++++++++---------

  1 file changed, 16 insertions(+), 9 deletions(-)
Quoted 9 lines

net/colo-compare.c static void colo_compare_finalize(Object *obj) · line 1429

Quoted 9 lines
net/colo-compare.c +0 -1
index 62554b5b3c..81d8de0aaa 100644
@@ -1426,7 +1426,7 @@ static void colo_compare_finalize(Object *obj)
               break;
           }
       }
-    if (QTAILQ_EMPTY(&net_compares)) {

if colo_compare_active == false, event_mtx and event_complete_cond didn't inited in colo_compare_complete()

Quoted 7 lines

net/colo-compare.c static void colo_compare_finalize(Object *obj) · line 1443

Quoted 7 lines
net/colo-compare.c +1 -0
+    if (QTAILQ_EMPTY(&net_compares) && colo_compare_active) {
           colo_compare_active = false;
           qemu_mutex_destroy(&event_mtx);
           qemu_cond_destroy(&event_complete_cond);
@@ -1442,19 +1442,26 @@ static void colo_compare_finalize(Object *obj)
         colo_compare_timer_del(s);
   -    qemu_bh_delete(s->event_bh);

s->event_bh wasn't allocated in colo_compare_iothread() in colo_compare_complete()

Quoted 8 lines

net/colo-compare.c static void colo_compare_finalize(Object *obj) · line 1451

Quoted 8 lines
net/colo-compare.c +3 -4
+    if (s->event_bh) {
+        qemu_bh_delete(s->event_bh);
+    }
   -    AioContext *ctx = iothread_get_aio_context(s->iothread);
-    aio_context_acquire(ctx);
-    AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
-    if (s->notify_dev) {
-        AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);

s->iothread == NULL after .instance_init (it can be detected in colo_compare_complete(), if it has been called)

Quoted 5 lines
Quoted 5 lines
net/colo-compare.c +5 -0
+    if (s->iothread) {
+        AioContext *ctx = iothread_get_aio_context(s->iothread);
+        aio_context_acquire(ctx);
+        AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
+        if (s->notify_dev) {
Quoted 8 lines

net/colo-compare.c static void colo_compare_finalize(Object *obj) · line 1457

Quoted 8 lines
net/colo-compare.c +3 -2
+            AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
+        }
+        aio_context_release(ctx);
       }
-    aio_context_release(ctx);
         /* Release all unhandled packets after compare thead exited */
       g_queue_foreach(&s->conn_list, colo_flush_packets, s);
-    AIO_WAIT_WHILE(NULL, !s->out_sendco.done);

In normal situation, it flushes all packets and sets s->out_sendco.done = true via compare_chr_send (we wait this event). But s->conn_list isn't initialized, s->out_sendco.done == false and won't become true. So, it's infinite waiting.

Quoted 11 lines

net/colo-compare.c static void colo_compare_finalize(Object *obj) · line 1464

Quoted 4 lines
net/colo-compare.c +4 -0
+    /* Without colo_compare_complete done == false without packets */
+    if (!g_queue_is_empty(&s->out_sendco.send_list)) {
+        AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
+    }

I think, would be good to add more description for this last change. It's not as obvious as previous two changes.

Quoted 2 lines
net/colo-compare.c +0 -0
 g_queue_clear(&s->conn_list);
       g_queue_clear(&s->out_sendco.send_list);
Signature · 2 lines
Best regards,
Maxim Davydov
#22 lore raw

PATCH 8/9 qom: add command to print initial properties

In reply to Maxim Davydov

The command "query-init-properties" is needed to get values of properties after initialization (not only default value). It makes sense, for example, when working with x86_64-cpu.
All machine types (and x-remote-object, because its init uses machime type's infrastructure) should be skipped, because only the one instance can be correctly initialized.

---
 qapi/qom.json      |  69 ++++++++++++++++++++++++++
 qom/qom-qmp-cmds.c | 121 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)
qapi/qom.json +69 -0
index eeb5395ff3..1eedc441eb 100644
@@ -949,3 +949,72 @@
 ##
 { 'command': 'object-del', 'data': {'id': 'str'},
   'allow-preconfig': true }
+
+##
+# @InitValue:
+#
+# Not all objects have default values but they have "initial" values.
+#
+# @name: property name
+#
+# @value: Current value (default or after initialization. It makes sence,
+#         for example, for x86-cpus)
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'InitValue',
+  'data': { 'name': 'str',
+            '*value': 'any' } }
+
+##
+# @ClassProperties:
+#
+# Initial values of properties that are owned by the class
+#
+# @classname: name of the class that owns appropriate properties
+#
+# @classprops: List of class properties
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'ClassProperties',
+  'data': { 'classname': 'str',
+            '*classprops': [ 'InitValue' ] } }
+
+##
+# @InitProps:
+#
+# List of properties and their values that are available after class
+# initialization. So it important to know default value of the property
+# even if it doesn't have "QObject *defval"
+#
+# @name: Object name
+#
+# @props: List of properties
+#
+# Notes: a value in each property was defval if it's available
+#        otherwise it's obtained via "(ObjectPropertyAccessor*) get"
+#        immediately after initialization of device object.
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'InitProps',
+  'data': { 'name': 'str',
+            'props': [ 'ClassProperties' ] } }
+
+##
+# @query-init-properties:
+#
+# Returns list of all objects (except all types related with machine type)
+# with all properties and their "default" values that  will be available
+# after initialization. The main purpose of this command is to be used to
+# build table with all machine-type-specific properties
+#
+# Since: 7.0
+#
+##
+{ 'command': 'query-init-properties',
+  'returns': [ 'InitProps' ] }
qom/qom-qmp-cmds.c +121 -0

130 lines not on this page · raw message

Signature · 1 line
2.31.1
#23 lore raw

Re: [PATCH v1 8/9] qom: add command to print initial properties

Quoted 23 lines · Maxim Davydov

The command "query-init-properties" is needed to get values of properties after initialization (not only default value). It makes sense, for example, when working with x86_64-cpu.
All machine types (and x-remote-object, because its init uses machime type's infrastructure) should be skipped, because only the one instance can be correctly initialized.

---
  qapi/qom.json      |  69 ++++++++++++++++++++++++++
  qom/qom-qmp-cmds.c | 121 +++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 190 insertions(+)
qapi/qom.json +2 -0
index eeb5395ff3..1eedc441eb 100644
@@ -949,3 +949,72 @@
  ##
  { 'command': 'object-del', 'data': {'id': 'str'},
    'allow-preconfig': true }
+
+##

qapi/qom.json line 963

Quoted 10 lines
qapi/qom.json +10 -0
+# @InitValue:
+#
+# Not all objects have default values but they have "initial" values.
+#
+# @name: property name
+#
+# @value: Current value (default or after initialization. It makes sence,
+#         for example, for x86-cpus)
+#
+# Since: 7.0

7.1 (here and below)

Quoted 6 lines
qapi/qom.json +6 -0
+#
+##
+{ 'struct': 'InitValue',
+  'data': { 'name': 'str',
+            '*value': 'any' } }
+

[..]

Quoted 37 lines
qom/qom-qmp-cmds.c +22 -0
index 2d6f41ecc7..c1bb3f1f8b 100644
@@ -27,6 +27,7 @@
  #include "qemu/cutils.h"
  #include "qom/object_interfaces.h"
  #include "qom/qom-qobject.h"
+#include "hw/boards.h"
 
  ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
  {
@@ -235,3 +236,123 @@ void qmp_object_del(const char *id, Error **errp)
  {
      user_creatable_del(id, errp);
  }
+
+static void query_object_prop(InitValueList **props_list, ObjectProperty *prop,
+                              Object *obj, Error **errp)
+{
+    InitValue *prop_info = NULL;
+
+    /* Skip inconsiderable properties */
+    if (strcmp(prop->name, "type") == 0 ||
+        strcmp(prop->name, "realized") == 0 ||
+        strcmp(prop->name, "hotpluggable") == 0 ||
+        strcmp(prop->name, "hotplugged") == 0 ||
+        strcmp(prop->name, "parent_bus") == 0) {
+        return;
+    }
+
+    prop_info = g_malloc0(sizeof(*prop_info));
+    prop_info->name = g_strdup(prop->name);
+    prop_info->value = NULL;
+    if (prop->defval) {
+        prop_info->value = qobject_ref(prop->defval);
+    } else if (prop->get) {

qom/qom-qmp-cmds.c void qmp_object_del(const char *id, Error **errp) · line 269

Quoted 10 lines
qom/qom-qmp-cmds.c +10 -0
+        /*
+         * crash-information in x86-cpu uses errp to return current state.
+         * So, after requesting this property it returns  GenericError:
+         * "No crash occured"
+         */
+        if (strcmp(prop->name, "crash-information") != 0) {
+            prop_info->value = object_property_get_qobject(obj, prop->name,
+                                                           errp);
+        }
+    }

Hmmm. Should we instead call prop->get() when is is available, and only if not use prep->defval?

Quoted 37 lines
qom/qom-qmp-cmds.c +37 -0
+    prop_info->has_value = !!prop_info->value;
+
+    QAPI_LIST_PREPEND(*props_list, prop_info);
+}
+
+typedef struct QIPData {
+    InitPropsList **dev_list;
+    Error **errp;
+} QIPData;
+
+static void query_init_properties_tramp(gpointer list_data, gpointer opaque)
+{
+    ObjectClass *k = list_data;
+    Object *obj;
+    ObjectClass *parent;
+    GHashTableIter iter;
+
+    QIPData *data = opaque;
+    ClassPropertiesList *class_props_list = NULL;
+    InitProps *dev_info;
+
+    /* Only one machine can be initialized correctly (it's already happened) */
+    if (object_class_dynamic_cast(k, TYPE_MACHINE)) {
+        return;
+    }
+
+    const char *klass_name = object_class_get_name(k);
+    /*
+     * Uses machine type infrastructure with notifiers. It causes immediate
+     * notify and SEGSEGV during remote_object_machine_done
+     */
+    if (strcmp(klass_name, "x-remote-object") == 0) {
+        return;
+    }
+
+    dev_info = g_malloc0(sizeof(*dev_info));
+    dev_info->name = g_strdup(klass_name);

qom/qom-qmp-cmds.c void qmp_object_del(const char *id, Error **errp) · line 316

Quoted 10 lines
qom/qom-qmp-cmds.c +10 -0
+
+    obj = object_new_with_class(k);
+
+    /*
+     * Part of ObjectPropertyIterator infrastructure, but we need more precise
+     * control of current class to dump appropriate features
+     * This part was taken out from loop because first initialization differ
+     * from other reinitializations
+     */
+    parent = object_get_class(obj);

hmm.. obj = object_new_with_class(k); parent = object_get_class(obj);.. Looks for me like parent should be equal to k. Or object_ API is rather unobvious.

Quoted 42 lines
qom/qom-qmp-cmds.c +42 -0
+    g_hash_table_iter_init(&iter, obj->properties);
+    const char *prop_owner_name = object_get_typename(obj);
+    do {
+        InitValueList *prop_list = NULL;
+        ClassProperties *class_data;
+
+        gpointer key, val;
+        while (g_hash_table_iter_next(&iter, &key, &val)) {
+            query_object_prop(&prop_list, (ObjectProperty *)val, obj,
+                              data->errp);
+        }
+        class_data = g_malloc0(sizeof(*class_data));
+        class_data->classname = g_strdup(prop_owner_name);
+        class_data->classprops = prop_list;
+        class_data->has_classprops = !!prop_list;
+
+        QAPI_LIST_PREPEND(class_props_list, class_data);
+
+        if (!parent) {
+            break;
+        }
+        g_hash_table_iter_init(&iter, parent->properties);
+        prop_owner_name = object_class_get_name(parent);
+        parent = object_class_get_parent(parent);
+    } while (true);
+    dev_info->props = class_props_list;
+    object_unref(OBJECT(obj));
+
+    QAPI_LIST_PREPEND(*(data->dev_list), dev_info);
+}
+
+InitPropsList *qmp_query_init_properties(Error **errp)
+{
+    GSList *typename_list = object_class_get_list(TYPE_OBJECT, false);
+
+    InitPropsList *dev_list = NULL;
+    QIPData data = { &dev_list, errp };
+    g_slist_foreach(typename_list, query_init_properties_tramp, &data);
+    g_slist_free(typename_list);
+
+    return dev_list;
+}
Signature · 2 lines
Best regards,
Vladimir
#24 lore raw

Re: [PATCH v1 8/9] qom: add command to print initial properties

Quoted 90 lines · Vladimir Sementsov-Ogievskiy
Quoted 31 lines · Maxim Davydov

The command "query-init-properties" is needed to get values of properties after initialization (not only default value). It makes sense, for example, when working with x86_64-cpu.
All machine types (and x-remote-object, because its init uses machime type's infrastructure) should be skipped, because only the one instance can be correctly initialized.

---
  qapi/qom.json      |  69 ++++++++++++++++++++++++++ qom/qom-qmp-cmds.c | 121 +++++++++++++++++++++++++++++++++++++++++++++

  2 files changed, 190 insertions(+)
qapi/qom.json +9 -0
index eeb5395ff3..1eedc441eb 100644
@@ -949,3 +949,72 @@
   ##
   { 'command': 'object-del', 'data': {'id': 'str'},
     'allow-preconfig': true }
+
+##
+# @InitValue:
+#
+# Not all objects have default values but they have "initial" values.
+#
+# @name: property name
+#
+# @value: Current value (default or after initialization. It makes 

sence,

qapi/qom.json line 963

Quoted 3 lines
qapi/qom.json +3 -0
+#         for example, for x86-cpus)
+#
+# Since: 7.0

7.1 (here and below)

Quoted 6 lines
qapi/qom.json +6 -0
+#
+##
+{ 'struct': 'InitValue',
+  'data': { 'name': 'str',
+            '*value': 'any' } }
+

[..]

Quoted 43 lines
qom/qom-qmp-cmds.c +3 -0
index 2d6f41ecc7..c1bb3f1f8b 100644
@@ -27,6 +27,7 @@
   #include "qemu/cutils.h"
   #include "qom/object_interfaces.h"
   #include "qom/qom-qobject.h"
+#include "hw/boards.h"
     ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
   {
@@ -235,3 +236,123 @@ void qmp_object_del(const char *id, Error **errp)
   {
       user_creatable_del(id, errp);
   }
+
+static void query_object_prop(InitValueList **props_list, 

ObjectProperty *prop,
+                              Object *obj, Error **errp)

qom/qom-qmp-cmds.c +20 -0
+{
+    InitValue *prop_info = NULL;
+
+    /* Skip inconsiderable properties */
+    if (strcmp(prop->name, "type") == 0 ||
+        strcmp(prop->name, "realized") == 0 ||
+        strcmp(prop->name, "hotpluggable") == 0 ||
+        strcmp(prop->name, "hotplugged") == 0 ||
+        strcmp(prop->name, "parent_bus") == 0) {
+        return;
+    }
+
+    prop_info = g_malloc0(sizeof(*prop_info));
+    prop_info->name = g_strdup(prop->name);
+    prop_info->value = NULL;
+    if (prop->defval) {
+        prop_info->value = qobject_ref(prop->defval);
+    } else if (prop->get) {
+        /*
+         * crash-information in x86-cpu uses errp to return current 

state.
+         * So, after requesting this property it returns GenericError:
+         * "No crash occured"
+         */

Quoted 8 lines
Quoted 6 lines

+        if (strcmp(prop->name, "crash-information") != 0) { +            prop_info->value = object_property_get_qobject(obj, prop->name,
+ errp);
+        }
+    }

Hmmm. Should we instead call prop->get() when is is available, and only if not use prep->defval?

default properties more rare and sometimes can give more information (if the device developer thought that there should be a default value). And I think that if prop->get() isn't available, prop->defval() isn't too.

Quoted 47 lines
Quoted 47 lines

+    prop_info->has_value = !!prop_info->value;

qom/qom-qmp-cmds.c +10 -0
+
+    QAPI_LIST_PREPEND(*props_list, prop_info);
+}
+
+typedef struct QIPData {
+    InitPropsList **dev_list;
+    Error **errp;
+} QIPData;
+
+static void query_init_properties_tramp(gpointer list_data, gpointer 

opaque)

qom/qom-qmp-cmds.c +11 -0
+{
+    ObjectClass *k = list_data;
+    Object *obj;
+    ObjectClass *parent;
+    GHashTableIter iter;
+
+    QIPData *data = opaque;
+    ClassPropertiesList *class_props_list = NULL;
+    InitProps *dev_info;
+
+    /* Only one machine can be initialized correctly (it's already 

happened) */
+    if (object_class_dynamic_cast(k, TYPE_MACHINE)) { +        return;
+    }
+
+    const char *klass_name = object_class_get_name(k); +    /*
+     * Uses machine type infrastructure with notifiers. It causes immediate
+     * notify and SEGSEGV during remote_object_machine_done +     */
+    if (strcmp(klass_name, "x-remote-object") == 0) { +        return;
+    }

qom/qom-qmp-cmds.c +8 -0
+
+    dev_info = g_malloc0(sizeof(*dev_info));
+    dev_info->name = g_strdup(klass_name);
+
+    obj = object_new_with_class(k);
+
+    /*
+     * Part of ObjectPropertyIterator infrastructure, but we need 

more precise

Quoted 8 lines
Quoted 6 lines

+     * control of current class to dump appropriate features +     * This part was taken out from loop because first initialization differ
+     * from other reinitializations
+     */
+    parent = object_get_class(obj);

hmm.. obj = object_new_with_class(k); parent = object_get_class(obj);.. Looks for me like parent should be equal to k. Or object_ API is rather unobvious.

I'll change it)

Quoted 45 lines
Quoted 43 lines

+    g_hash_table_iter_init(&iter, obj->properties); +    const char *prop_owner_name = object_get_typename(obj); +    do {
+        InitValueList *prop_list = NULL;
+        ClassProperties *class_data;

qom/qom-qmp-cmds.c +37 -0
+
+        gpointer key, val;
+        while (g_hash_table_iter_next(&iter, &key, &val)) {
+            query_object_prop(&prop_list, (ObjectProperty *)val, obj,
+                              data->errp);
+        }
+        class_data = g_malloc0(sizeof(*class_data));
+        class_data->classname = g_strdup(prop_owner_name);
+        class_data->classprops = prop_list;
+        class_data->has_classprops = !!prop_list;
+
+        QAPI_LIST_PREPEND(class_props_list, class_data);
+
+        if (!parent) {
+            break;
+        }
+        g_hash_table_iter_init(&iter, parent->properties);
+        prop_owner_name = object_class_get_name(parent);
+        parent = object_class_get_parent(parent);
+    } while (true);
+    dev_info->props = class_props_list;
+    object_unref(OBJECT(obj));
+
+    QAPI_LIST_PREPEND(*(data->dev_list), dev_info);
+}
+
+InitPropsList *qmp_query_init_properties(Error **errp)
+{
+    GSList *typename_list = object_class_get_list(TYPE_OBJECT, false);
+
+    InitPropsList *dev_list = NULL;
+    QIPData data = { &dev_list, errp };
+    g_slist_foreach(typename_list, query_init_properties_tramp, &data);
+    g_slist_free(typename_list);
+
+    return dev_list;
+}
Signature · 2 lines
Best regards,
Maxim Davydov
#25 lore raw

Re: [PATCH v1 8/9] qom: add command to print initial properties

In reply to Maxim Davydov
Quoted 6 lines · On Tue, 29 Mar 2022 00:15:38 +0300 Maxim Davydov <[email protected]>…

The command "query-init-properties" is needed to get values of properties after initialization (not only default value). It makes sense, for example, when working with x86_64-cpu.
All machine types (and x-remote-object, because its init uses machime type's infrastructure) should be skipped, because only the one instance can be correctly initialized.

It might be obvious to you but I couldn't parse above commit message at all. Pls rephrase and explain in more detail what you are trying to achieve.

Quoted 220 lines
---
 qapi/qom.json      |  69 ++++++++++++++++++++++++++
 qom/qom-qmp-cmds.c | 121 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 190 insertions(+)
qapi/qom.json +69 -0
index eeb5395ff3..1eedc441eb 100644
@@ -949,3 +949,72 @@
 ##
 { 'command': 'object-del', 'data': {'id': 'str'},
   'allow-preconfig': true }
+
+##
+# @InitValue:
+#
+# Not all objects have default values but they have "initial" values.
+#
+# @name: property name
+#
+# @value: Current value (default or after initialization. It makes sence,
+#         for example, for x86-cpus)
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'InitValue',
+  'data': { 'name': 'str',
+            '*value': 'any' } }
+
+##
+# @ClassProperties:
+#
+# Initial values of properties that are owned by the class
+#
+# @classname: name of the class that owns appropriate properties
+#
+# @classprops: List of class properties
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'ClassProperties',
+  'data': { 'classname': 'str',
+            '*classprops': [ 'InitValue' ] } }
+
+##
+# @InitProps:
+#
+# List of properties and their values that are available after class
+# initialization. So it important to know default value of the property
+# even if it doesn't have "QObject *defval"
+#
+# @name: Object name
+#
+# @props: List of properties
+#
+# Notes: a value in each property was defval if it's available
+#        otherwise it's obtained via "(ObjectPropertyAccessor*) get"
+#        immediately after initialization of device object.
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'InitProps',
+  'data': { 'name': 'str',
+            'props': [ 'ClassProperties' ] } }
+
+##
+# @query-init-properties:
+#
+# Returns list of all objects (except all types related with machine type)
+# with all properties and their "default" values that  will be available
+# after initialization. The main purpose of this command is to be used to
+# build table with all machine-type-specific properties
+#
+# Since: 7.0
+#
+##
+{ 'command': 'query-init-properties',
+  'returns': [ 'InitProps' ] }
qom/qom-qmp-cmds.c +121 -0
index 2d6f41ecc7..c1bb3f1f8b 100644
@@ -27,6 +27,7 @@
 #include "qemu/cutils.h"
 #include "qom/object_interfaces.h"
 #include "qom/qom-qobject.h"
+#include "hw/boards.h"
 
 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
 {
@@ -235,3 +236,123 @@ void qmp_object_del(const char *id, Error **errp)
 {
     user_creatable_del(id, errp);
 }
+
+static void query_object_prop(InitValueList **props_list, ObjectProperty *prop,
+                              Object *obj, Error **errp)
+{
+    InitValue *prop_info = NULL;
+
+    /* Skip inconsiderable properties */
+    if (strcmp(prop->name, "type") == 0 ||
+        strcmp(prop->name, "realized") == 0 ||
+        strcmp(prop->name, "hotpluggable") == 0 ||
+        strcmp(prop->name, "hotplugged") == 0 ||
+        strcmp(prop->name, "parent_bus") == 0) {
+        return;
+    }
+
+    prop_info = g_malloc0(sizeof(*prop_info));
+    prop_info->name = g_strdup(prop->name);
+    prop_info->value = NULL;
+    if (prop->defval) {
+        prop_info->value = qobject_ref(prop->defval);
+    } else if (prop->get) {
+        /*
+         * crash-information in x86-cpu uses errp to return current state.
+         * So, after requesting this property it returns  GenericError:
+         * "No crash occured"
+         */
+        if (strcmp(prop->name, "crash-information") != 0) {
+            prop_info->value = object_property_get_qobject(obj, prop->name,
+                                                           errp);
+        }
+    }
+    prop_info->has_value = !!prop_info->value;
+
+    QAPI_LIST_PREPEND(*props_list, prop_info);
+}
+
+typedef struct QIPData {
+    InitPropsList **dev_list;
+    Error **errp;
+} QIPData;
+
+static void query_init_properties_tramp(gpointer list_data, gpointer opaque)
+{
+    ObjectClass *k = list_data;
+    Object *obj;
+    ObjectClass *parent;
+    GHashTableIter iter;
+
+    QIPData *data = opaque;
+    ClassPropertiesList *class_props_list = NULL;
+    InitProps *dev_info;
+
+    /* Only one machine can be initialized correctly (it's already happened) */
+    if (object_class_dynamic_cast(k, TYPE_MACHINE)) {
+        return;
+    }
+
+    const char *klass_name = object_class_get_name(k);
+    /*
+     * Uses machine type infrastructure with notifiers. It causes immediate
+     * notify and SEGSEGV during remote_object_machine_done
+     */
+    if (strcmp(klass_name, "x-remote-object") == 0) {
+        return;
+    }
+
+    dev_info = g_malloc0(sizeof(*dev_info));
+    dev_info->name = g_strdup(klass_name);
+
+    obj = object_new_with_class(k);
+
+    /*
+     * Part of ObjectPropertyIterator infrastructure, but we need more precise
+     * control of current class to dump appropriate features
+     * This part was taken out from loop because first initialization differ
+     * from other reinitializations
+     */
+    parent = object_get_class(obj);
+    g_hash_table_iter_init(&iter, obj->properties);
+    const char *prop_owner_name = object_get_typename(obj);
+    do {
+        InitValueList *prop_list = NULL;
+        ClassProperties *class_data;
+
+        gpointer key, val;
+        while (g_hash_table_iter_next(&iter, &key, &val)) {
+            query_object_prop(&prop_list, (ObjectProperty *)val, obj,
+                              data->errp);
+        }
+        class_data = g_malloc0(sizeof(*class_data));
+        class_data->classname = g_strdup(prop_owner_name);
+        class_data->classprops = prop_list;
+        class_data->has_classprops = !!prop_list;
+
+        QAPI_LIST_PREPEND(class_props_list, class_data);
+
+        if (!parent) {
+            break;
+        }
+        g_hash_table_iter_init(&iter, parent->properties);
+        prop_owner_name = object_class_get_name(parent);
+        parent = object_class_get_parent(parent);
+    } while (true);
+    dev_info->props = class_props_list;
+    object_unref(OBJECT(obj));
+
+    QAPI_LIST_PREPEND(*(data->dev_list), dev_info);
+}
+
+InitPropsList *qmp_query_init_properties(Error **errp)
+{
+    GSList *typename_list = object_class_get_list(TYPE_OBJECT, false);
+
+    InitPropsList *dev_list = NULL;
+    QIPData data = { &dev_list, errp };
+    g_slist_foreach(typename_list, query_init_properties_tramp, &data);
+    g_slist_free(typename_list);
+
+    return dev_list;
+}
#26 lore raw

Re: [PATCH v1 8/9] qom: add command to print initial properties

Quoted 11 lines · Igor Mammedov
Quoted 6 lines · On Tue, 29 Mar 2022 00:15:38 +0300 Maxim Davydov <[email protected]>…

The command "query-init-properties" is needed to get values of properties after initialization (not only default value). It makes sense, for example, when working with x86_64-cpu.
All machine types (and x-remote-object, because its init uses machime type's infrastructure) should be skipped, because only the one instance can be correctly initialized.

It might be obvious to you but I couldn't parse above commit message at all. Pls rephrase and explain in more detail what you are trying to achieve.

I want to dump all "default" object properties to compare it with compat_props of MachineState. It means that I need values from ObjectProperty even it doesn't have default value. For many devices it can give useless information. But, for example, x86_64-cpu sets "real" default values for specific model only during initialization. x86_cpu_properties[] can't give information about kvm default features.

Quoted 220 lines
Quoted 220 lines
---
  qapi/qom.json      |  69 ++++++++++++++++++++++++++
  qom/qom-qmp-cmds.c | 121 +++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 190 insertions(+)
qapi/qom.json +69 -0
index eeb5395ff3..1eedc441eb 100644
@@ -949,3 +949,72 @@
  ##
  { 'command': 'object-del', 'data': {'id': 'str'},
    'allow-preconfig': true }
+
+##
+# @InitValue:
+#
+# Not all objects have default values but they have "initial" values.
+#
+# @name: property name
+#
+# @value: Current value (default or after initialization. It makes sence,
+#         for example, for x86-cpus)
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'InitValue',
+  'data': { 'name': 'str',
+            '*value': 'any' } }
+
+##
+# @ClassProperties:
+#
+# Initial values of properties that are owned by the class
+#
+# @classname: name of the class that owns appropriate properties
+#
+# @classprops: List of class properties
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'ClassProperties',
+  'data': { 'classname': 'str',
+            '*classprops': [ 'InitValue' ] } }
+
+##
+# @InitProps:
+#
+# List of properties and their values that are available after class
+# initialization. So it important to know default value of the property
+# even if it doesn't have "QObject *defval"
+#
+# @name: Object name
+#
+# @props: List of properties
+#
+# Notes: a value in each property was defval if it's available
+#        otherwise it's obtained via "(ObjectPropertyAccessor*) get"
+#        immediately after initialization of device object.
+#
+# Since: 7.0
+#
+##
+{ 'struct': 'InitProps',
+  'data': { 'name': 'str',
+            'props': [ 'ClassProperties' ] } }
+
+##
+# @query-init-properties:
+#
+# Returns list of all objects (except all types related with machine type)
+# with all properties and their "default" values that  will be available
+# after initialization. The main purpose of this command is to be used to
+# build table with all machine-type-specific properties
+#
+# Since: 7.0
+#
+##
+{ 'command': 'query-init-properties',
+  'returns': [ 'InitProps' ] }
qom/qom-qmp-cmds.c +121 -0
index 2d6f41ecc7..c1bb3f1f8b 100644
@@ -27,6 +27,7 @@
  #include "qemu/cutils.h"
  #include "qom/object_interfaces.h"
  #include "qom/qom-qobject.h"
+#include "hw/boards.h"
 
  ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
  {
@@ -235,3 +236,123 @@ void qmp_object_del(const char *id, Error **errp)
  {
      user_creatable_del(id, errp);
  }
+
+static void query_object_prop(InitValueList **props_list, ObjectProperty *prop,
+                              Object *obj, Error **errp)
+{
+    InitValue *prop_info = NULL;
+
+    /* Skip inconsiderable properties */
+    if (strcmp(prop->name, "type") == 0 ||
+        strcmp(prop->name, "realized") == 0 ||
+        strcmp(prop->name, "hotpluggable") == 0 ||
+        strcmp(prop->name, "hotplugged") == 0 ||
+        strcmp(prop->name, "parent_bus") == 0) {
+        return;
+    }
+
+    prop_info = g_malloc0(sizeof(*prop_info));
+    prop_info->name = g_strdup(prop->name);
+    prop_info->value = NULL;
+    if (prop->defval) {
+        prop_info->value = qobject_ref(prop->defval);
+    } else if (prop->get) {
+        /*
+         * crash-information in x86-cpu uses errp to return current state.
+         * So, after requesting this property it returns  GenericError:
+         * "No crash occured"
+         */
+        if (strcmp(prop->name, "crash-information") != 0) {
+            prop_info->value = object_property_get_qobject(obj, prop->name,
+                                                           errp);
+        }
+    }
+    prop_info->has_value = !!prop_info->value;
+
+    QAPI_LIST_PREPEND(*props_list, prop_info);
+}
+
+typedef struct QIPData {
+    InitPropsList **dev_list;
+    Error **errp;
+} QIPData;
+
+static void query_init_properties_tramp(gpointer list_data, gpointer opaque)
+{
+    ObjectClass *k = list_data;
+    Object *obj;
+    ObjectClass *parent;
+    GHashTableIter iter;
+
+    QIPData *data = opaque;
+    ClassPropertiesList *class_props_list = NULL;
+    InitProps *dev_info;
+
+    /* Only one machine can be initialized correctly (it's already happened) */
+    if (object_class_dynamic_cast(k, TYPE_MACHINE)) {
+        return;
+    }
+
+    const char *klass_name = object_class_get_name(k);
+    /*
+     * Uses machine type infrastructure with notifiers. It causes immediate
+     * notify and SEGSEGV during remote_object_machine_done
+     */
+    if (strcmp(klass_name, "x-remote-object") == 0) {
+        return;
+    }
+
+    dev_info = g_malloc0(sizeof(*dev_info));
+    dev_info->name = g_strdup(klass_name);
+
+    obj = object_new_with_class(k);
+
+    /*
+     * Part of ObjectPropertyIterator infrastructure, but we need more precise
+     * control of current class to dump appropriate features
+     * This part was taken out from loop because first initialization differ
+     * from other reinitializations
+     */
+    parent = object_get_class(obj);
+    g_hash_table_iter_init(&iter, obj->properties);
+    const char *prop_owner_name = object_get_typename(obj);
+    do {
+        InitValueList *prop_list = NULL;
+        ClassProperties *class_data;
+
+        gpointer key, val;
+        while (g_hash_table_iter_next(&iter, &key, &val)) {
+            query_object_prop(&prop_list, (ObjectProperty *)val, obj,
+                              data->errp);
+        }
+        class_data = g_malloc0(sizeof(*class_data));
+        class_data->classname = g_strdup(prop_owner_name);
+        class_data->classprops = prop_list;
+        class_data->has_classprops = !!prop_list;
+
+        QAPI_LIST_PREPEND(class_props_list, class_data);
+
+        if (!parent) {
+            break;
+        }
+        g_hash_table_iter_init(&iter, parent->properties);
+        prop_owner_name = object_class_get_name(parent);
+        parent = object_class_get_parent(parent);
+    } while (true);
+    dev_info->props = class_props_list;
+    object_unref(OBJECT(obj));
+
+    QAPI_LIST_PREPEND(*(data->dev_list), dev_info);
+}
+
+InitPropsList *qmp_query_init_properties(Error **errp)
+{
+    GSList *typename_list = object_class_get_list(TYPE_OBJECT, false);
+
+    InitPropsList *dev_list = NULL;
+    QIPData data = { &dev_list, errp };
+    g_slist_foreach(typename_list, query_init_properties_tramp, &data);
+    g_slist_free(typename_list);
+
+    return dev_list;
+}
Signature · 2 lines
Best regards,
Maxim Davydov
#27 lore raw

PATCH 9/9 scripts: printing machine type compat properties

In reply to Maxim Davydov

This script makes the information that can be obtained from query-init-properties and query-machines easy to read.

Note: some init values from the devices can't be available like properties from virtio-9p when configure has --disable-virtfs. Such values are replaced by "DEFAULT". Another exception is properties of abstract classes. The default value of the abstract class property is common value of all child classes. But if the values of the child classes are different the default value will be "BASED_ON_CHILD" (for example, old x86_64-cpu can have unsupported feature).

Example:

1) virsh qemu-monitor-command VM --pretty \
'{"execute" : "query-init-properties"}' > init_props.json

2) virsh qemu-monitor-command VM --pretty \
'{"execute" : "query-machines", "arguments" : {"is-full" : true}}' \
> compat_props.json

3) scripts/print_MT.py --MT_compat_props compat_props.json\
--init_props init_props.json --mt pc-q35-7.0 pc-q35-6.1

Output:

╒═══════════════════════════════════╤══════════════╤══════════════╕
│           property_name           │  pc-q35-7.0  │  pc-q35-6.1  │
╞═══════════════════════════════════╪══════════════╪══════════════╡
│   ICH9-LPC-x-keep-pci-slot-hpc    │     True     │    False     │
├───────────────────────────────────┼──────────────┼──────────────┤
│          nvme-ns-shared           │     True     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│ vhost-user-vsock-device-seqpacket │     auto     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│ virtio-mem-unplugged-inaccessible │     auto     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-build   │    14393     │    0x1bbc    │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-major   │      10      │    0x0006    │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-minor   │      0       │    0x0001    │
╘═══════════════════════════════════╧══════════════╧══════════════╛
---
 scripts/print_MT.py | 274 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 274 insertions(+)
 create mode 100755 scripts/print_MT.py
scripts/print_MT.py new file +274 -0
new file mode 100755
index 0000000000..8be13be8d7
@@ -0,0 +1,274 @@
+#! /usr/bin/python3
+#
+# Script for printing machine type compatible features. It uses two JSON files
+# that should be generated by qmp-init-properties and query-machines.
+#
+# Copyright (c) 2022 Maxim Davydov <[email protected]>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+import pandas as pd
+import json
+from tabulate import tabulate
+from argparse import ArgumentParser
+
+
+# used for aliases and other names that can be changed
+aliases = { "e1000-82540em": "e1000" }
+
+
+def get_major(mt):
+    splited = mt.split('.')
+    if (len(splited) >= 2):
+        return int(mt.split('.')[1])
+    else:
+        return 0
+
+
+def get_prefix(mt):
+    splited = mt.split('.')
+    if (len(splited) >= 1):
+        return mt.split('.')[0]
+    else:
+        return mt
+
+
+def get_mt_sequence(mt_data):
+    mt_list = [mt['name'] for mt in mt_data['return']]
+    mt_list.remove('none')
+
+    mt_list.sort(key=get_major, reverse=True)
+    mt_list.sort(key=get_prefix, reverse=True)
+
+    return mt_list
+
+
+def get_req_props(defval_data, prop_set, abstr_class_to_features):
+    req_prop_values = dict()
+    req_abstr_prop_values = dict()
+
+    for device in defval_data['return']:
+        # Skip cpu devices that will break all default values for cpus
+        if device['name'] == 'base-x86_64-cpu':
+            continue
+        if device['name'] == 'max-x86_64-cpu':
+            continue
+
+        # some features in mt set as one absract class
+        # but this features are owned by another class
+        device_props_owners = dict()
+        for props_class in device['props']:
+            if not 'classprops' in props_class: # for example, Object class
+                continue
+
+            for prop in props_class['classprops']:
+                if not 'value' in prop:
+                    continue
+
+                prop_name = device['name'] + '-' + prop['name']
+                device_props_owners[prop['name']] = prop['value']
+                if prop_name in prop_set:
+                    req_prop_values[prop_name] = prop['value']
+
+        for props_class in device['props']:
+            if not props_class['classname'] in abstr_class_to_features:
+                continue
+
+            for req_prop in abstr_class_to_features[props_class['classname']]:
+                if not req_prop in device_props_owners:
+                    continue
+
+                prop_value = device_props_owners[req_prop]
+                prop_name = props_class['classname'] + '-' + req_prop
+                if req_abstr_prop_values.setdefault(prop_name, prop_value) \
+                    != prop_value:
+                    req_abstr_prop_values[prop_name] = 'BASED_ON_CHILD'
+
+    return req_prop_values, req_abstr_prop_values
+
+
+def make_definition_table(mt_to_compat_props, prop_set,
+                          req_props, req_abstr_props, is_full):
+    mt_table = dict()
+    for prop in sorted(prop_set):
+        if not is_full:
+            values = set()
+            for mt in mt_to_compat_props:
+                if prop in mt_to_compat_props[mt]:
+                    values.add(mt_to_compat_props[mt][prop])
+                else:
+                    if prop in req_props:
+                        values.add(req_props[prop])
+                    else:
+                        values.add('DEFAULT')
+            # Skip the property if its value is the same for
+            # all required machines
+            if len(values) == 1:
+                continue
+
+        mt_table.setdefault('property_name', []).append(prop)
+        for mt in mt_to_compat_props:
+            if prop in mt_to_compat_props[mt]:
+                value = mt_to_compat_props[mt][prop]
+                mt_table.setdefault(mt, []).append(value)
+            else:
+                if prop in req_props:
+                    mt_table.setdefault(mt, []).append(req_props[prop])
+                else:
+                    value = req_abstr_props.get(prop, 'DEFAULT')
+                    mt_table.setdefault(mt, []).append(value)
+
+    return mt_table
+
+
+def get_standard_form(value):
+    if type(value) is str:
+        out = value.upper()
+        if out.isnumeric():
+            return int(out)
+        if out == 'TRUE':
+            return True
+        if out == 'FALSE':
+            return False
+
+    return value
+
+
+def get_features(mt_data, defval_data, mts, is_full):
+    prop_set = set()
+    abstr_prop_set = set()
+    mt_to_compat_props = dict()
+    # It will be used for searching appropriate feature (sometimes class name
+    # in machine type definition and real class name are different)
+    abstr_class_to_features = dict()
+
+    for mt in mt_data['return']:
+        if mt['name'] == 'none':
+            continue
+
+        if not mt['name'] in mts:
+            continue
+
+        mt_to_compat_props[mt['name']] = dict()
+        for prop in mt['compat-props']:
+            driver_name = aliases.get(prop['driver'], prop['driver'])
+            prop_name = prop['driver'] + '-' + prop['property']
+            real_name = driver_name + '-' + prop['property']
+            # value is always string
+            prop_val  = get_standard_form(prop['value'])
+            if prop['abstract']:
+                mt_to_compat_props[mt['name']][real_name] = prop_val
+                abstr_prop_set.add(real_name)
+                abstr_class_to_features.setdefault(driver_name,
+                                                   set()).add(prop['property'])
+            else:
+                mt_to_compat_props[mt['name']][real_name] = prop_val
+                prop_set.add(real_name)
+
+    req_props, req_abstr_props = get_req_props(defval_data, prop_set,
+                                               abstr_class_to_features)
+
+    # join sets for global sorting by name
+    prop_set.update(abstr_prop_set)
+    mt_table = make_definition_table(mt_to_compat_props, prop_set, req_props,
+                                     req_abstr_props, is_full)
+    # to save mt sequence
+    df = pd.DataFrame({'property_name': mt_table['property_name']})
+    for mt in mts:
+        if not mt in mt_table:
+            print('Error: {0} no found'.format(mt))
+            continue
+        df[mt] = mt_table[mt]
+
+    return df
+
+
+def main():
+    parser = ArgumentParser(description='''Print definition of machine
+                                           type (compatible features)''')
+    parser.add_argument('--MT_compat_props', type=str, required=True,
+                        help='''Path to JSON file with current machine type
+                                definition. It must be generated via
+                                query-machines with is-full option.''')
+    parser.add_argument('--init_props', type=str, required=True,
+                        help='''Path to JSON file with initial features. It
+                                must be generated via
+                                query-init-properties.''')
+    parser.add_argument('--format', type=str,
+                        choices=['table', 'csv', 'html', 'json'],
+                        default='table', help='Format of the output file')
+    parser.add_argument('--file', type=str,
+                        help='''Path to output file. It must be set with csv
+                                and html formats.''')
+    parser.add_argument('--all', action='store_true',
+                        help='''Print all available machine types (list of
+                                machine types will be ignored''')
+    parser.add_argument('--mt', nargs="*", type=str,
+                        help='List of machine types that will be compared')
+    parser.add_argument('--full', action='store_true',
+                        help='''Print all defined properties (by default,
+                                only properties with different values are
+                                printed)''')
+
+    args = parser.parse_args()
+
+    if args.all == 0 and args.mt == None:
+        print('Enter the list of required machine types (list of all '\
+              'machine types : qemu-system-x86_64 --machine help)')
+        return
+
+    with open(args.MT_compat_props) as mt_json_file:
+        mt_data = json.load(mt_json_file)
+
+    with open(args.init_props) as defval_json_file:
+        defval_data = json.load(defval_json_file)
+
+    if args.all:
+        df = get_features(mt_data, defval_data, get_mt_sequence(mt_data),
+                          args.full)
+    else:
+        df = get_features(mt_data, defval_data, args.mt, args.full)
+
+    if args.format == 'csv':
+        if args.file == None:
+            print('Error: csv format requires path to output file')
+            return
+        df.to_csv(args.file)
+    elif args.format == 'html':
+        if args.file == None:
+            print('Error: html format requires path to output file')
+            return
+        with open(args.file, 'w') as output_html:
+            output_html.write(df.to_html(justify='center', col_space='400px',
+                                         index=False))
+    elif args.format == 'json':
+        json_table = df.to_json()
+        if args.file == None:
+            print(json_table)
+        else:
+            with open(args.file, 'w') as output_json:
+                output_json.write(json_table)
+    elif args.format == 'table':
+        table = tabulate(df, showindex=False, stralign='center',
+                         tablefmt='fancy_grid', headers='keys')
+        if args.file == None:
+            print(table)
+        else:
+            with open(args.file, 'w') as output_table:
+                output_table.write(table)
+
+
+if __name__ == '__main__':
+    main()
Signature · 1 line
2.31.1
#28 lore raw

Re: [PATCH v1 9/9] scripts: printing machine type compat properties

Quoted 54 lines · Maxim Davydov

This script makes the information that can be obtained from query-init-properties and query-machines easy to read.

Note: some init values from the devices can't be available like properties from virtio-9p when configure has --disable-virtfs. Such values are replaced by "DEFAULT". Another exception is properties of abstract classes. The default value of the abstract class property is common value of all child classes. But if the values of the child classes are different the default value will be "BASED_ON_CHILD" (for example, old x86_64-cpu can have unsupported feature).

Example:

1) virsh qemu-monitor-command VM --pretty \
'{"execute" : "query-init-properties"}' > init_props.json

2) virsh qemu-monitor-command VM --pretty \
'{"execute" : "query-machines", "arguments" : {"is-full" : true}}' \
> compat_props.json

3) scripts/print_MT.py --MT_compat_props compat_props.json\
--init_props init_props.json --mt pc-q35-7.0 pc-q35-6.1

Output:

╒═══════════════════════════════════╤══════════════╤══════════════╕
│           property_name           │  pc-q35-7.0  │  pc-q35-6.1  │
╞═══════════════════════════════════╪══════════════╪══════════════╡
│   ICH9-LPC-x-keep-pci-slot-hpc    │     True     │    False     │
├───────────────────────────────────┼──────────────┼──────────────┤
│          nvme-ns-shared           │     True     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│ vhost-user-vsock-device-seqpacket │     auto     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│ virtio-mem-unplugged-inaccessible │     auto     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-build   │    14393     │    0x1bbc    │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-major   │      10      │    0x0006    │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-minor   │      0       │    0x0001    │
╘═══════════════════════════════════╧══════════════╧══════════════╛
---
  scripts/print_MT.py | 274 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 274 insertions(+)

create mode 100755 scripts/print_MT.py

scripts/print_MT.py new file +0 -0
new file mode 100755
index 0000000000..8be13be8d7

scripts/print_MT.py line 1

Quoted 1 line
scripts/print_MT.py new file +1 -0
@@ -0,0 +1,274 @@
+#! /usr/bin/python3

Standard shebang line for python3 is:

#!/usr/bin/env python3

Quoted 24 lines
scripts/print_MT.py new file +24 -0
+#
+# Script for printing machine type compatible features. It uses two JSON files
+# that should be generated by qmp-init-properties and query-machines.
+#
+# Copyright (c) 2022 Maxim Davydov <[email protected]>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+import pandas as pd
+import json
+from tabulate import tabulate
+from argparse import ArgumentParser

scripts/print_MT.py line 35

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+
+
+# used for aliases and other names that can be changed
+aliases = { "e1000-82540em": "e1000" }
+
+
+def get_major(mt):
+    splited = mt.split('.')
+    if (len(splited) >= 2):
+        return int(mt.split('.')[1])

why to call split() again? You may use splited[1]

scripts/print_MT.py line 42

Quoted 7 lines
scripts/print_MT.py new file +7 -0
+    else:
+        return 0
+
+
+def get_prefix(mt):
+    splited = mt.split('.')
+    if (len(splited) >= 1):

In python you don't need braces around if condition:

if len(splited) >= 1:

is the same thing.

scripts/print_MT.py line 45

Quoted 3 lines
scripts/print_MT.py new file +3 -0
+        return mt.split('.')[0]
+    else:
+        return mt

seems, split() never return empty list, so len is always >=1.

You can do simply

return mt.split(',', 1)[0]

scripts/print_MT.py line 53

Quoted 8 lines
scripts/print_MT.py new file +8 -0
+
+
+def get_mt_sequence(mt_data):
+    mt_list = [mt['name'] for mt in mt_data['return']]
+    mt_list.remove('none')
+
+    mt_list.sort(key=get_major, reverse=True)
+    mt_list.sort(key=get_prefix, reverse=True)

Aha. You may use one sort() call, if use tuple as key. Something like this should work:

def parse_mt(mt):
spl = mt.split('.')
if len(spl) >= 2:

    return spl[0], spl[1]

else:
return mt, 0

...

mt_list.sort(key=parse_mt, ...)

scripts/print_MT.py line 60

Quoted 7 lines
scripts/print_MT.py new file +7 -0
+
+    return mt_list
+
+
+def get_req_props(defval_data, prop_set, abstr_class_to_features):
+    req_prop_values = dict()
+    req_abstr_prop_values = dict()

{} construction is native way to create empty dict:

req_prop_values = {}

scripts/print_MT.py line 70

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+
+    for device in defval_data['return']:
+        # Skip cpu devices that will break all default values for cpus
+        if device['name'] == 'base-x86_64-cpu':
+            continue
+        if device['name'] == 'max-x86_64-cpu':
+            continue
+
+        # some features in mt set as one absract class
+        # but this features are owned by another class

Hmm. That all hard for me to review, because I don't know the whole model of machine types.. Don't we have a documentation somewhere? Which objects, classes, abstart classes and properties we have and what that all mean.

scripts/print_MT.py line 73

Quoted 3 lines
scripts/print_MT.py new file +3 -0
+        device_props_owners = dict()
+        for props_class in device['props']:
+            if not 'classprops' in props_class: # for example, Object class

More pythonic is:

if 'classprops' not in props_class:

Quoted 144 lines
scripts/print_MT.py new file +144 -0
+                continue
+
+            for prop in props_class['classprops']:
+                if not 'value' in prop:
+                    continue
+
+                prop_name = device['name'] + '-' + prop['name']
+                device_props_owners[prop['name']] = prop['value']
+                if prop_name in prop_set:
+                    req_prop_values[prop_name] = prop['value']
+
+        for props_class in device['props']:
+            if not props_class['classname'] in abstr_class_to_features:
+                continue
+
+            for req_prop in abstr_class_to_features[props_class['classname']]:
+                if not req_prop in device_props_owners:
+                    continue
+
+                prop_value = device_props_owners[req_prop]
+                prop_name = props_class['classname'] + '-' + req_prop
+                if req_abstr_prop_values.setdefault(prop_name, prop_value) \
+                    != prop_value:
+                    req_abstr_prop_values[prop_name] = 'BASED_ON_CHILD'
+
+    return req_prop_values, req_abstr_prop_values
+
+
+def make_definition_table(mt_to_compat_props, prop_set,
+                          req_props, req_abstr_props, is_full):
+    mt_table = dict()
+    for prop in sorted(prop_set):
+        if not is_full:
+            values = set()
+            for mt in mt_to_compat_props:
+                if prop in mt_to_compat_props[mt]:
+                    values.add(mt_to_compat_props[mt][prop])
+                else:
+                    if prop in req_props:
+                        values.add(req_props[prop])
+                    else:
+                        values.add('DEFAULT')
+            # Skip the property if its value is the same for
+            # all required machines
+            if len(values) == 1:
+                continue
+
+        mt_table.setdefault('property_name', []).append(prop)
+        for mt in mt_to_compat_props:
+            if prop in mt_to_compat_props[mt]:
+                value = mt_to_compat_props[mt][prop]
+                mt_table.setdefault(mt, []).append(value)
+            else:
+                if prop in req_props:
+                    mt_table.setdefault(mt, []).append(req_props[prop])
+                else:
+                    value = req_abstr_props.get(prop, 'DEFAULT')
+                    mt_table.setdefault(mt, []).append(value)
+
+    return mt_table
+
+
+def get_standard_form(value):
+    if type(value) is str:
+        out = value.upper()
+        if out.isnumeric():
+            return int(out)
+        if out == 'TRUE':
+            return True
+        if out == 'FALSE':
+            return False
+
+    return value
+
+
+def get_features(mt_data, defval_data, mts, is_full):
+    prop_set = set()
+    abstr_prop_set = set()
+    mt_to_compat_props = dict()
+    # It will be used for searching appropriate feature (sometimes class name
+    # in machine type definition and real class name are different)
+    abstr_class_to_features = dict()
+
+    for mt in mt_data['return']:
+        if mt['name'] == 'none':
+            continue
+
+        if not mt['name'] in mts:
+            continue
+
+        mt_to_compat_props[mt['name']] = dict()
+        for prop in mt['compat-props']:
+            driver_name = aliases.get(prop['driver'], prop['driver'])
+            prop_name = prop['driver'] + '-' + prop['property']
+            real_name = driver_name + '-' + prop['property']
+            # value is always string
+            prop_val  = get_standard_form(prop['value'])
+            if prop['abstract']:
+                mt_to_compat_props[mt['name']][real_name] = prop_val
+                abstr_prop_set.add(real_name)
+                abstr_class_to_features.setdefault(driver_name,
+                                                   set()).add(prop['property'])
+            else:
+                mt_to_compat_props[mt['name']][real_name] = prop_val
+                prop_set.add(real_name)
+
+    req_props, req_abstr_props = get_req_props(defval_data, prop_set,
+                                               abstr_class_to_features)
+
+    # join sets for global sorting by name
+    prop_set.update(abstr_prop_set)
+    mt_table = make_definition_table(mt_to_compat_props, prop_set, req_props,
+                                     req_abstr_props, is_full)
+    # to save mt sequence
+    df = pd.DataFrame({'property_name': mt_table['property_name']})
+    for mt in mts:
+        if not mt in mt_table:
+            print('Error: {0} no found'.format(mt))
+            continue
+        df[mt] = mt_table[mt]
+
+    return df
+
+
+def main():
+    parser = ArgumentParser(description='''Print definition of machine
+                                           type (compatible features)''')
+    parser.add_argument('--MT_compat_props', type=str, required=True,
+                        help='''Path to JSON file with current machine type
+                                definition. It must be generated via
+                                query-machines with is-full option.''')
+    parser.add_argument('--init_props', type=str, required=True,
+                        help='''Path to JSON file with initial features. It
+                                must be generated via
+                                query-init-properties.''')
+    parser.add_argument('--format', type=str,
+                        choices=['table', 'csv', 'html', 'json'],
+                        default='table', help='Format of the output file')
+    parser.add_argument('--file', type=str,
+                        help='''Path to output file. It must be set with csv
+                                and html formats.''')
+    parser.add_argument('--all', action='store_true',
+                        help='''Print all available machine types (list of
+                                machine types will be ignored''')

scripts/print_MT.py line 227

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+    parser.add_argument('--mt', nargs="*", type=str,
+                        help='List of machine types that will be compared')
+    parser.add_argument('--full', action='store_true',
+                        help='''Print all defined properties (by default,
+                                only properties with different values are
+                                printed)''')
+
+    args = parser.parse_args()
+
+    if args.all == 0 and args.mt == None:

Don't compare boolean value with zero, use it as is (I'm about args.all, but comparing mt with None doesn't make real sense here too):

if not(args.all or args.mt):

Quoted 5 lines
scripts/print_MT.py new file +5 -0
+        print('Enter the list of required machine types (list of all '\
+              'machine types : qemu-system-x86_64 --machine help)')
+        return
+
+    with open(args.MT_compat_props) as mt_json_file:

scripts/print_MT.py line 242

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+        mt_data = json.load(mt_json_file)
+
+    with open(args.init_props) as defval_json_file:
+        defval_data = json.load(defval_json_file)
+
+    if args.all:
+        df = get_features(mt_data, defval_data, get_mt_sequence(mt_data),
+                          args.full)
+    else:
+        df = get_features(mt_data, defval_data, args.mt, args.full)

I'd write it like this:

mt = args.mt or get_mt_sequence(mt_data)
df = get_feature(..., mt, args.full)

Quoted 18 lines
scripts/print_MT.py new file +18 -0
+
+    if args.format == 'csv':
+        if args.file == None:
+            print('Error: csv format requires path to output file')
+            return
+        df.to_csv(args.file)
+    elif args.format == 'html':
+        if args.file == None:
+            print('Error: html format requires path to output file')
+            return
+        with open(args.file, 'w') as output_html:
+            output_html.write(df.to_html(justify='center', col_space='400px',
+                                         index=False))
+    elif args.format == 'json':
+        json_table = df.to_json()
+        if args.file == None:
+            print(json_table)
+        else:

scripts/print_MT.py line 270

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+            with open(args.file, 'w') as output_json:
+                output_json.write(json_table)
+    elif args.format == 'table':
+        table = tabulate(df, showindex=False, stralign='center',
+                         tablefmt='fancy_grid', headers='keys')
+        if args.file == None:
+            print(table)
+        else:
+            with open(args.file, 'w') as output_table:
+                output_table.write(table)

For me this looks like extra logic.

Why to restrict printing csv/html directly to stdout? It's native to use output redirection to save output to some file. I think you can simply drop --file argument always print to stdout.

Or, if you still want this option, it's better to prepare the whole output in string variable, and then handle it once:

if ...
elif ...
elif ...

...

if args.file:
with open(...) as f:
f.write(output)
else:
print(output)

Quoted 4 lines
scripts/print_MT.py new file +4 -0
+
+
+if __name__ == '__main__':
+    main()
Signature · 2 lines
Best regards,
Vladimir
#29 lore raw

Re: [PATCH v1 9/9] scripts: printing machine type compat properties

Quoted 18 lines · Vladimir Sementsov-Ogievskiy
Quoted 16 lines · Maxim Davydov

This script makes the information that can be obtained from query-init-properties and query-machines easy to read.

Note: some init values from the devices can't be available like properties from virtio-9p when configure has --disable-virtfs. Such values are replaced by "DEFAULT". Another exception is properties of abstract classes. The default value of the abstract class property is common value of all child classes. But if the values of the child classes are different the default value will be "BASED_ON_CHILD" (for example, old x86_64-cpu can have unsupported feature).

Example:

1) virsh qemu-monitor-command VM --pretty \
'{"execute" : "query-init-properties"}' > init_props.json

Quoted 7 lines
Quoted 7 lines

2) virsh qemu-monitor-command VM --pretty \
'{"execute" : "query-machines", "arguments" : {"is-full" : true}}' \
> compat_props.json

3) scripts/print_MT.py --MT_compat_props compat_props.json\
--init_props init_props.json --mt pc-q35-7.0 pc-q35-6.1

Being able to parse existing JSON files is nice, but have you also considered baking the QMP querying directly into this script?

Quoted 381 lines
Quoted 31 lines

Output:

╒═══════════════════════════════════╤══════════════╤══════════════╕
│           property_name           │  pc-q35-7.0  │  pc-q35-6.1  │
╞═══════════════════════════════════╪══════════════╪══════════════╡
│   ICH9-LPC-x-keep-pci-slot-hpc    │     True     │    False     │
├───────────────────────────────────┼──────────────┼──────────────┤
│          nvme-ns-shared           │     True     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│ vhost-user-vsock-device-seqpacket │     auto     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│ virtio-mem-unplugged-inaccessible │     auto     │     off      │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-build   │    14393     │    0x1bbc    │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-major   │      10      │    0x0006    │
├───────────────────────────────────┼──────────────┼──────────────┤
│  x86_64-cpu-hv-version-id-minor   │      0       │    0x0001    │
╘═══════════════════════════════════╧══════════════╧══════════════╛
---
  scripts/print_MT.py | 274 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 274 insertions(+)

create mode 100755 scripts/print_MT.py

scripts/print_MT.py new file +0 -0
new file mode 100755
index 0000000000..8be13be8d7

scripts/print_MT.py line 1

Quoted 1 line
scripts/print_MT.py new file +1 -0
@@ -0,0 +1,274 @@
+#! /usr/bin/python3

Standard shebang line for python3 is:

#!/usr/bin/env python3

Quoted 24 lines
scripts/print_MT.py new file +24 -0
+#
+# Script for printing machine type compatible features. It uses two JSON files
+# that should be generated by qmp-init-properties and query-machines.
+#
+# Copyright (c) 2022 Maxim Davydov <[email protected]>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+import pandas as pd
+import json
+from tabulate import tabulate
+from argparse import ArgumentParser

scripts/print_MT.py line 35

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+
+
+# used for aliases and other names that can be changed
+aliases = { "e1000-82540em": "e1000" }
+
+
+def get_major(mt):
+    splited = mt.split('.')
+    if (len(splited) >= 2):
+        return int(mt.split('.')[1])

why to call split() again? You may use splited[1]

scripts/print_MT.py line 42

Quoted 7 lines
scripts/print_MT.py new file +7 -0
+    else:
+        return 0
+
+
+def get_prefix(mt):
+    splited = mt.split('.')
+    if (len(splited) >= 1):

In python you don't need braces around if condition:

    if len(splited) >= 1:

is the same thing.

scripts/print_MT.py line 45

Quoted 3 lines
scripts/print_MT.py new file +3 -0
+        return mt.split('.')[0]
+    else:
+        return mt

seems, split() never return empty list, so len is always >=1.

You can do simply

return mt.split(',', 1)[0]

scripts/print_MT.py line 53

Quoted 8 lines
scripts/print_MT.py new file +8 -0
+
+
+def get_mt_sequence(mt_data):
+    mt_list = [mt['name'] for mt in mt_data['return']]
+    mt_list.remove('none')
+
+    mt_list.sort(key=get_major, reverse=True)
+    mt_list.sort(key=get_prefix, reverse=True)

Aha. You may use one sort() call, if use tuple as key. Something like this should work:

def parse_mt(mt):

   spl = mt.split('.')
   if len(spl) >= 2:
     return spl[0], spl[1]
   else:
     return mt, 0

...

mt_list.sort(key=parse_mt, ...)

scripts/print_MT.py line 60

Quoted 7 lines
scripts/print_MT.py new file +7 -0
+
+    return mt_list
+
+
+def get_req_props(defval_data, prop_set, abstr_class_to_features):
+    req_prop_values = dict()
+    req_abstr_prop_values = dict()

{} construction is native way to create empty dict:

   req_prop_values = {}

scripts/print_MT.py line 70

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+
+    for device in defval_data['return']:
+        # Skip cpu devices that will break all default values for cpus
+        if device['name'] == 'base-x86_64-cpu':
+            continue
+        if device['name'] == 'max-x86_64-cpu':
+            continue
+
+        # some features in mt set as one absract class
+        # but this features are owned by another class

Hmm. That all hard for me to review, because I don't know the whole model of machine types.. Don't we have a documentation somewhere? Which objects, classes, abstart classes and properties we have and what that all mean.

scripts/print_MT.py line 73

Quoted 3 lines
scripts/print_MT.py new file +3 -0
+        device_props_owners = dict()
+        for props_class in device['props']:
+            if not 'classprops' in props_class: # for example, Object class

More pythonic is:

if 'classprops' not in props_class:

Quoted 144 lines
scripts/print_MT.py new file +144 -0
+                continue
+
+            for prop in props_class['classprops']:
+                if not 'value' in prop:
+                    continue
+
+                prop_name = device['name'] + '-' + prop['name']
+                device_props_owners[prop['name']] = prop['value']
+                if prop_name in prop_set:
+                    req_prop_values[prop_name] = prop['value']
+
+        for props_class in device['props']:
+            if not props_class['classname'] in abstr_class_to_features:
+                continue
+
+            for req_prop in abstr_class_to_features[props_class['classname']]:
+                if not req_prop in device_props_owners:
+                    continue
+
+                prop_value = device_props_owners[req_prop]
+                prop_name = props_class['classname'] + '-' + req_prop
+                if req_abstr_prop_values.setdefault(prop_name, prop_value) \
+                    != prop_value:
+                    req_abstr_prop_values[prop_name] = 'BASED_ON_CHILD'
+
+    return req_prop_values, req_abstr_prop_values
+
+
+def make_definition_table(mt_to_compat_props, prop_set,
+                          req_props, req_abstr_props, is_full):
+    mt_table = dict()
+    for prop in sorted(prop_set):
+        if not is_full:
+            values = set()
+            for mt in mt_to_compat_props:
+                if prop in mt_to_compat_props[mt]:
+                    values.add(mt_to_compat_props[mt][prop])
+                else:
+                    if prop in req_props:
+                        values.add(req_props[prop])
+                    else:
+                        values.add('DEFAULT')
+            # Skip the property if its value is the same for
+            # all required machines
+            if len(values) == 1:
+                continue
+
+        mt_table.setdefault('property_name', []).append(prop)
+        for mt in mt_to_compat_props:
+            if prop in mt_to_compat_props[mt]:
+                value = mt_to_compat_props[mt][prop]
+                mt_table.setdefault(mt, []).append(value)
+            else:
+                if prop in req_props:
+                    mt_table.setdefault(mt, []).append(req_props[prop])
+                else:
+                    value = req_abstr_props.get(prop, 'DEFAULT')
+                    mt_table.setdefault(mt, []).append(value)
+
+    return mt_table
+
+
+def get_standard_form(value):
+    if type(value) is str:
+        out = value.upper()
+        if out.isnumeric():
+            return int(out)
+        if out == 'TRUE':
+            return True
+        if out == 'FALSE':
+            return False
+
+    return value
+
+
+def get_features(mt_data, defval_data, mts, is_full):
+    prop_set = set()
+    abstr_prop_set = set()
+    mt_to_compat_props = dict()
+    # It will be used for searching appropriate feature (sometimes class name
+    # in machine type definition and real class name are different)
+    abstr_class_to_features = dict()
+
+    for mt in mt_data['return']:
+        if mt['name'] == 'none':
+            continue
+
+        if not mt['name'] in mts:
+            continue
+
+        mt_to_compat_props[mt['name']] = dict()
+        for prop in mt['compat-props']:
+            driver_name = aliases.get(prop['driver'], prop['driver'])
+            prop_name = prop['driver'] + '-' + prop['property']
+            real_name = driver_name + '-' + prop['property']
+            # value is always string
+            prop_val  = get_standard_form(prop['value'])
+            if prop['abstract']:
+                mt_to_compat_props[mt['name']][real_name] = prop_val
+                abstr_prop_set.add(real_name)
+                abstr_class_to_features.setdefault(driver_name,
+                                                   set()).add(prop['property'])
+            else:
+                mt_to_compat_props[mt['name']][real_name] = prop_val
+                prop_set.add(real_name)
+
+    req_props, req_abstr_props = get_req_props(defval_data, prop_set,
+                                               abstr_class_to_features)
+
+    # join sets for global sorting by name
+    prop_set.update(abstr_prop_set)
+    mt_table = make_definition_table(mt_to_compat_props, prop_set, req_props,
+                                     req_abstr_props, is_full)
+    # to save mt sequence
+    df = pd.DataFrame({'property_name': mt_table['property_name']})
+    for mt in mts:
+        if not mt in mt_table:
+            print('Error: {0} no found'.format(mt))
+            continue
+        df[mt] = mt_table[mt]
+
+    return df
+
+
+def main():
+    parser = ArgumentParser(description='''Print definition of machine
+                                           type (compatible features)''')
+    parser.add_argument('--MT_compat_props', type=str, required=True,
+                        help='''Path to JSON file with current machine type
+                                definition. It must be generated via
+                                query-machines with is-full option.''')
+    parser.add_argument('--init_props', type=str, required=True,
+                        help='''Path to JSON file with initial features. It
+                                must be generated via
+                                query-init-properties.''')
+    parser.add_argument('--format', type=str,
+                        choices=['table', 'csv', 'html', 'json'],
+                        default='table', help='Format of the output file')
+    parser.add_argument('--file', type=str,
+                        help='''Path to output file. It must be set with csv
+                                and html formats.''')
+    parser.add_argument('--all', action='store_true',
+                        help='''Print all available machine types (list of
+                                machine types will be ignored''')

scripts/print_MT.py line 227

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+    parser.add_argument('--mt', nargs="*", type=str,
+                        help='List of machine types that will be compared')
+    parser.add_argument('--full', action='store_true',
+                        help='''Print all defined properties (by default,
+                                only properties with different values are
+                                printed)''')
+
+    args = parser.parse_args()
+
+    if args.all == 0 and args.mt == None:

Don't compare boolean value with zero, use it as is (I'm about args.all, but comparing mt with None doesn't make real sense here too):

if not(args.all or args.mt):

Quoted 5 lines
scripts/print_MT.py new file +5 -0
+        print('Enter the list of required machine types (list of all '\
+              'machine types : qemu-system-x86_64 --machine help)')
+        return
+
+    with open(args.MT_compat_props) as mt_json_file:

scripts/print_MT.py line 242

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+        mt_data = json.load(mt_json_file)
+
+    with open(args.init_props) as defval_json_file:
+        defval_data = json.load(defval_json_file)
+
+    if args.all:
+        df = get_features(mt_data, defval_data, get_mt_sequence(mt_data),
+                          args.full)
+    else:
+        df = get_features(mt_data, defval_data, args.mt, args.full)

I'd write it like this:

mt = args.mt or get_mt_sequence(mt_data)
df = get_feature(..., mt, args.full)

Quoted 18 lines
scripts/print_MT.py new file +18 -0
+
+    if args.format == 'csv':
+        if args.file == None:
+            print('Error: csv format requires path to output file')
+            return
+        df.to_csv(args.file)
+    elif args.format == 'html':
+        if args.file == None:
+            print('Error: html format requires path to output file')
+            return
+        with open(args.file, 'w') as output_html:
+            output_html.write(df.to_html(justify='center', col_space='400px',
+                                         index=False))
+    elif args.format == 'json':
+        json_table = df.to_json()
+        if args.file == None:
+            print(json_table)
+        else:

scripts/print_MT.py line 270

Quoted 10 lines
scripts/print_MT.py new file +10 -0
+            with open(args.file, 'w') as output_json:
+                output_json.write(json_table)
+    elif args.format == 'table':
+        table = tabulate(df, showindex=False, stralign='center',
+                         tablefmt='fancy_grid', headers='keys')
+        if args.file == None:
+            print(table)
+        else:
+            with open(args.file, 'w') as output_table:
+                output_table.write(table)

For me this looks like extra logic.

Why to restrict printing csv/html directly to stdout? It's native to use output redirection to save output to some file. I think you can simply drop --file argument always print to stdout.

Or, if you still want this option, it's better to prepare the whole output in string variable, and then handle it once:

if ...
elif ...
elif ...

...

if args.file:

   with open(...) as f:
     f.write(output)

else:
print(output)

Quoted 10 lines

scripts/print_MT.py line 274

Quoted 4 lines
scripts/print_MT.py new file +4 -0
+
+
+if __name__ == '__main__':
+    main()

--
Best regards,
Vladimir

I trust Vladimir's review on python scripting otherwise.

--js

#30 lore raw
In reply to Maxim Davydov
Quoted 5 lines · On Tue, 29 Mar 2022 00:15:30 +0300 Maxim Davydov <[email protected]>…

We need to be able to check machine type after its definition. It's necessary when using complicated inheritance of compatible features. For instance, this tool can help to find bugs in the machine type definition if the name of the device has been changed. Also, this tool was created to help with MTs of other projects such as vz branches.

a comment to all patches where "after initialization" is mentioned. Pls rewrite commit messages so that it would be clear what that means and describe in detail why current code needs fixing.

Quoted 30 lines

Maxim Davydov (9):
qmp: Add dump machine type compatible properties
pci: add null-pointer check
mem: appropriate handling getting mem region
msmouse: add appropriate unregister handler
wctablet: add appropriate unregister handler
chardev: add appropriate getting address
colo-compare: safe finalization
qom: add command to print initial properties
scripts: printing machine type compat properties

 chardev/char-socket.c       |   9 ++
 chardev/msmouse.c           |   4 +-
 chardev/wctablet.c          |   4 +-
 hw/core/machine-qmp-cmds.c  |  25 +++-
 hw/i386/sgx-epc.c           |   5 +-
 hw/mem/nvdimm.c             |   6 +
 hw/mem/pc-dimm.c            |   5 +
 hw/pci-host/i440fx.c        |  17 ++-
 hw/pci-host/q35.c           |  17 ++-
 net/colo-compare.c          |  25 ++--
 qapi/machine.json           |  58 +++++++-
 qapi/qom.json               |  69 +++++++++
 qom/qom-qmp-cmds.c          | 121 ++++++++++++++++
 scripts/print_MT.py         | 274 ++++++++++++++++++++++++++++++++++++
 tests/qtest/fuzz/qos_fuzz.c |   2 +-
 15 files changed, 613 insertions(+), 28 deletions(-)
 create mode 100755 scripts/print_MT.py
#31 lore raw
In reply to Maxim Davydov
Quoted 5 lines · Maxim Davydov

We need to be able to check machine type after its definition. It's necessary when using complicated inheritance of compatible features. For instance, this tool can help to find bugs in the machine type definition if the name of the device has been changed. Also, this tool was created to help with MTs of other projects such as vz branches.

We discussed verbally two points to change:

1. Instead of workarounds to avoid crashes, which are unsafe (we avoid crash, but is data that we generate this way valid or not?), let's require to call QEMU process with such options so everything that needed is initialized and we avoid crashes and still get valid information

2. We want not "default" values, but "current" ones. I.e., we start QEMU with same options like in production, and we clearly get all real values of machine properties.

Quoted 30 lines

Maxim Davydov (9):
qmp: Add dump machine type compatible properties
pci: add null-pointer check
mem: appropriate handling getting mem region
msmouse: add appropriate unregister handler
wctablet: add appropriate unregister handler
chardev: add appropriate getting address
colo-compare: safe finalization
qom: add command to print initial properties
scripts: printing machine type compat properties

  chardev/char-socket.c       |   9 ++
  chardev/msmouse.c           |   4 +-
  chardev/wctablet.c          |   4 +-
  hw/core/machine-qmp-cmds.c  |  25 +++-
  hw/i386/sgx-epc.c           |   5 +-
  hw/mem/nvdimm.c             |   6 +
  hw/mem/pc-dimm.c            |   5 +
  hw/pci-host/i440fx.c        |  17 ++-
  hw/pci-host/q35.c           |  17 ++-
  net/colo-compare.c          |  25 ++--
  qapi/machine.json           |  58 +++++++-
  qapi/qom.json               |  69 +++++++++
  qom/qom-qmp-cmds.c          | 121 ++++++++++++++++
  scripts/print_MT.py         | 274 ++++++++++++++++++++++++++++++++++++
  tests/qtest/fuzz/qos_fuzz.c |   2 +-
  15 files changed, 613 insertions(+), 28 deletions(-)

create mode 100755 scripts/print_MT.py

Signature · 2 lines
Best regards,
Vladimir