Skip to contentSkip to conversation

hw: Fix abuse of QOM class internals modified by their instances

PATCH In review Philippe Mathieu-Daudé 3 participants 9 messages May 23, 2023

lore
Share & tools
lore
#0 lore raw

cover hw: Fix abuse of QOM class internals modified by their instances

Bernhard warned for QOM class abuse here [*]:

Quoted 5 lines

A realize method is supposed to modify a single instance only while we're modifying the behavior of whole classes here, i.e. will affect every instance of these classes. This goes against QOM design principles and will therefore be confusing for people who are familiar with QOM in particular and OOP in general.

This series fixes the cases I found while auditing.

[*] https://lore.kernel.org/qemu-devel/[email protected]/

Philippe Mathieu-Daudé (3):
hw/mips/jazz: Fix modifying QOM class internal state from instance hw/ppc/e500plat: Fix modifying QOM class internal state from instance hw/i2c/pmbus_device: Fix modifying QOM class internals from instance

 hw/i2c/pmbus_device.c | 17 ++++++++++-------
 hw/mips/jazz.c        | 41 +++++++++++++++++++++++------------------
 hw/ppc/e500plat.c     | 25 +++++++++++--------------
 3 files changed, 44 insertions(+), 39 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/3 hw/mips/jazz: Fix modifying QOM class internal state from instance

QOM object instance should not modify its class state (because all other objects instanciated from this class get affected).

Instead of modifying the MIPSCPUClass 'no_data_aborts' field in the instance machine_init() handler, set it in the machine class_init handler. Since 2 machines require this, share the common code in a new machine_class_ignore_data_abort() helper.

---
 hw/mips/jazz.c | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)
hw/mips/jazz.c +23 -18
index ca4426a92c..de2e827bf8 100644
@@ -128,7 +128,6 @@ static void mips_jazz_init(MachineState *machine,
     int bios_size, n, big_endian;
     Clock *cpuclk;
     MIPSCPU *cpu;
-    MIPSCPUClass *mcc;
     CPUMIPSState *env;
     qemu_irq *i8259;
     rc4030_dma *dmas;
@@ -177,23 +176,6 @@ static void mips_jazz_init(MachineState *machine,
     env = &cpu->env;
     qemu_register_reset(main_cpu_reset, cpu);
 
-    /*
-     * Chipset returns 0 in invalid reads and do not raise data exceptions.
-     * However, we can't simply add a global memory region to catch
-     * everything, as this would make all accesses including instruction
-     * accesses be ignored and not raise exceptions.
-     *
-     * NOTE: this behaviour of raising exceptions for bad instruction
-     * fetches but not bad data accesses was added in commit 54e755588cf1e9
-     * to restore behaviour broken by c658b94f6e8c206, but it is not clear
-     * whether the real hardware behaves this way. It is possible that
-     * real hardware ignores bad instruction fetches as well -- if so then
-     * we could replace this hijacking of CPU methods with a simple global
-     * memory region that catches all memory accesses, as we do on Malta.
-     */
-    mcc = MIPS_CPU_GET_CLASS(cpu);
-    mcc->no_data_aborts = true;
-
     /* allocate RAM */
     memory_region_add_subregion(address_space, 0, machine->ram);
 
@@ -414,6 +396,27 @@ void mips_pica61_init(MachineState *machine)
     mips_jazz_init(machine, JAZZ_PICA61);
 }
 
+static void machine_class_ignore_data_abort(MachineClass *mc)
+{
+    MIPSCPUClass *mcc = MIPS_CPU_CLASS(mc);
+
+    /*
+     * Chipset returns 0 in invalid reads and do not raise data exceptions.
+     * However, we can't simply add a global memory region to catch
+     * everything, as this would make all accesses including instruction
+     * accesses be ignored and not raise exceptions.
+     *
+     * NOTE: this behaviour of raising exceptions for bad instruction
+     * fetches but not bad data accesses was added in commit 54e755588cf1e9
+     * to restore behaviour broken by c658b94f6e8c206, but it is not clear
+     * whether the real hardware behaves this way. It is possible that
+     * real hardware ignores bad instruction fetches as well -- if so then
+     * we could replace this hijacking of CPU methods with a simple global
+     * memory region that catches all memory accesses, as we do on Malta.
+     */
+    mcc->no_data_aborts = true;
+}
+
 static void mips_magnum_class_init(ObjectClass *oc, void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
@@ -423,6 +426,7 @@ static void mips_magnum_class_init(ObjectClass *oc, void *data)
     mc->block_default_type = IF_SCSI;
     mc->default_cpu_type = MIPS_CPU_TYPE_NAME("R4000");
     mc->default_ram_id = "mips_jazz.ram";
+    machine_class_ignore_data_abort(mc);
 }
 
 static const TypeInfo mips_magnum_type = {
@@ -440,6 +444,7 @@ static void mips_pica61_class_init(ObjectClass *oc, void *data)
     mc->block_default_type = IF_SCSI;
     mc->default_cpu_type = MIPS_CPU_TYPE_NAME("R4000");
     mc->default_ram_id = "mips_jazz.ram";
+    machine_class_ignore_data_abort(mc);
 }
 
 static const TypeInfo mips_pica61_type = {
Signature · 1 line
2.38.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] hw/mips/jazz: Fix modifying QOM class internal state from instance

Quoted 8 lines · Philippe Mathieu-Daudé

QOM object instance should not modify its class state (because all other objects instanciated from this class get affected).

Instead of modifying the MIPSCPUClass 'no_data_aborts' field in the instance machine_init() handler, set it in the machine class_init handler. Since 2 machines require this, share the common code in a new machine_class_ignore_data_abort() helper.

Quoted 5 lines
---
  hw/mips/jazz.c | 41 +++++++++++++++++++++++------------------
  1 file changed, 23 insertions(+), 18 deletions(-)

r~

#3 lore raw

PATCH 2/3 hw/ppc/e500plat: Fix modifying QOM class internal state from instance

In reply to Philippe Mathieu-Daudé

QOM object instance should not modify its class state (because all other objects instanciated from this class get affected).

Instead of modifying the PPCE500MachineClass 'mpic_version' field in the instance machine_init() handler, set it in the machine class init handler (e500plat_machine_class_init).

---
 hw/ppc/e500plat.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)
hw/ppc/e500plat.c +11 -14
index 3032bd3f6d..c3b0ed01cf 100644
@@ -30,18 +30,6 @@ static void e500plat_fixup_devtree(void *fdt)
                      sizeof(compatible));
 }
 
-static void e500plat_init(MachineState *machine)
-{
-    PPCE500MachineClass *pmc = PPCE500_MACHINE_GET_CLASS(machine);
-    /* Older KVM versions don't support EPR which breaks guests when we announce
-       MPIC variants that support EPR. Revert to an older one for those */
-    if (kvm_enabled() && !kvmppc_has_cap_epr()) {
-        pmc->mpic_version = OPENPIC_MODEL_FSL_MPIC_20;
-    }
-
-    ppce500_init(machine);
-}
-
 static void e500plat_machine_device_plug_cb(HotplugHandler *hotplug_dev,
                                             DeviceState *dev, Error **errp)
 {
@@ -81,7 +69,6 @@ static void e500plat_machine_class_init(ObjectClass *oc, void *data)
     pmc->pci_first_slot = 0x1;
     pmc->pci_nr_slots = PCI_SLOT_MAX - 1;
     pmc->fixup_devtree = e500plat_fixup_devtree;
-    pmc->mpic_version = OPENPIC_MODEL_FSL_MPIC_42;
     pmc->has_mpc8xxx_gpio = true;
     pmc->has_esdhc = true;
     pmc->platform_bus_base = 0xf00000000ULL;
@@ -94,8 +81,18 @@ static void e500plat_machine_class_init(ObjectClass *oc, void *data)
     pmc->pci_mmio_bus_base = 0xE0000000ULL;
     pmc->spin_base = 0xFEF000000ULL;
 
+    if (kvm_enabled() && !kvmppc_has_cap_epr()) {
+        /*
+         * Older KVM versions don't support EPR which breaks guests when
+         * we announce MPIC variants that support EPR. Revert to an older
+         * one for those.
+         */
+        pmc->mpic_version = OPENPIC_MODEL_FSL_MPIC_20;
+    } else {
+        pmc->mpic_version = OPENPIC_MODEL_FSL_MPIC_42;
+    }
+
     mc->desc = "generic paravirt e500 platform";
-    mc->init = e500plat_init;
     mc->max_cpus = 32;
     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("e500v2_v30");
     mc->default_ram_id = "mpc8544ds.ram";
Signature · 1 line
2.38.1
#4 lore raw

Re: [PATCH 2/3] hw/ppc/e500plat: Fix modifying QOM class internal state from instance

Hi Philippe,

Quoted 21 lines · Philippe Mathieu-Daudé

QOM object instance should not modify its class state (because all other objects instanciated from this class get affected).

Instead of modifying the PPCE500MachineClass 'mpic_version' field in the instance machine_init() handler, set it in the machine class init handler (e500plat_machine_class_init).

---
  hw/ppc/e500plat.c | 25 +++++++++++--------------
  1 file changed, 11 insertions(+), 14 deletions(-)
hw/ppc/e500plat.c +0 -0
index 3032bd3f6d..c3b0ed01cf 100644
@@ -30,18 +30,6 @@ static void e500plat_fixup_devtree(void *fdt)
                       sizeof(compatible));
  }
 

hw/ppc/e500plat.c static void e500plat_fixup_devtree(void *fdt) · line 42

Quoted 10 lines
hw/ppc/e500plat.c +0 -10
-static void e500plat_init(MachineState *machine)
-{
-    PPCE500MachineClass *pmc = PPCE500_MACHINE_GET_CLASS(machine);
-    /* Older KVM versions don't support EPR which breaks guests when we announce
-       MPIC variants that support EPR. Revert to an older one for those */
-    if (kvm_enabled() && !kvmppc_has_cap_epr()) {
-        pmc->mpic_version = OPENPIC_MODEL_FSL_MPIC_20;
-    }
-
-    ppce500_init(machine);

Won't this drop the call to ppce500_init(machine)?

Quoted 20 lines
hw/ppc/e500plat.c +3 -3
-}
-
  static void e500plat_machine_device_plug_cb(HotplugHandler *hotplug_dev,
                                              DeviceState *dev, Error **errp)
  {
@@ -81,7 +69,6 @@ static void e500plat_machine_class_init(ObjectClass *oc, void *data)
      pmc->pci_first_slot = 0x1;
      pmc->pci_nr_slots = PCI_SLOT_MAX - 1;
      pmc->fixup_devtree = e500plat_fixup_devtree;
-    pmc->mpic_version = OPENPIC_MODEL_FSL_MPIC_42;
      pmc->has_mpc8xxx_gpio = true;
      pmc->has_esdhc = true;
      pmc->platform_bus_base = 0xf00000000ULL;
@@ -94,8 +81,18 @@ static void e500plat_machine_class_init(ObjectClass *oc, void *data)
      pmc->pci_mmio_bus_base = 0xE0000000ULL;
      pmc->spin_base = 0xFEF000000ULL;
 
+    if (kvm_enabled() && !kvmppc_has_cap_epr()) {
+        /*
+         * Older KVM versions don't support EPR which breaks guests when

hw/ppc/e500plat.c static void e500plat_machine_class_init(ObjectClass *oc, void *data) · line 98

Quoted 10 lines
hw/ppc/e500plat.c +8 -1
+         * we announce MPIC variants that support EPR. Revert to an older
+         * one for those.
+         */
+        pmc->mpic_version = OPENPIC_MODEL_FSL_MPIC_20;
+    } else {
+        pmc->mpic_version = OPENPIC_MODEL_FSL_MPIC_42;
+    }
+
      mc->desc = "generic paravirt e500 platform";
-    mc->init = e500plat_init;

I suppose best would be to just put it in here instead of e500plat_init?

Alex

Quoted 3 lines
hw/ppc/e500plat.c +0 -0
      mc->max_cpus = 32;
      mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("e500v2_v30");
      mc->default_ram_id = "mpc8544ds.ram";
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] hw/ppc/e500plat: Fix modifying QOM class internal state from instance

In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

QOM object instance should not modify its class state (because all other objects instanciated from this class get affected).

Instead of modifying the PPCE500MachineClass 'mpic_version' field in the instance machine_init() handler, set it in the machine class init handler (e500plat_machine_class_init).

Quoted 5 lines
---
  hw/ppc/e500plat.c | 25 +++++++++++--------------
  1 file changed, 11 insertions(+), 14 deletions(-)

r~

#6 lore raw

PATCH 3/3 hw/i2c/pmbus_device: Fix modifying QOM class internals from instance

In reply to Philippe Mathieu-Daudé

QOM object instance should not modify its class state (because all other objects instanciated from this class get affected).

Instead of modifying the PMBusDeviceClass 'device_num_pages' field the first time a instance is initialized (in pmbus_pages_alloc), introduce a new pmbus_pages_num() helper which returns the page number from the class without modifying the class state.

The code logic become slighly simplified.

---
 hw/i2c/pmbus_device.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
hw/i2c/pmbus_device.c +10 -7
index 44fe4eddbb..8bc9d5108a 100644
@@ -190,15 +190,18 @@ static void pmbus_quick_cmd(SMBusDevice *smd, uint8_t read)
     }
 }
 
+static uint8_t pmbus_pages_num(PMBusDevice *pmdev)
+{
+    const PMBusDeviceClass *k = PMBUS_DEVICE_GET_CLASS(pmdev);
+
+    /* some PMBus devices don't use the PAGE command, so they get 1 page */
+    return k->device_num_pages ? : 1;
+}
+
 static void pmbus_pages_alloc(PMBusDevice *pmdev)
 {
-    /* some PMBus devices don't use the PAGE command, so they get 1 page */
-    PMBusDeviceClass *k = PMBUS_DEVICE_GET_CLASS(pmdev);
-    if (k->device_num_pages == 0) {
-        k->device_num_pages = 1;
-    }
-    pmdev->num_pages = k->device_num_pages;
-    pmdev->pages = g_new0(PMBusPage, k->device_num_pages);
+    pmdev->num_pages = pmbus_pages_num(pmdev);
+    pmdev->pages = g_new0(PMBusPage, pmdev->num_pages);
 }
 
 void pmbus_check_limits(PMBusDevice *pmdev)
Signature · 1 line
2.38.1
Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] hw/i2c/pmbus_device: Fix modifying QOM class internals from instance

Quoted 8 lines · Philippe Mathieu-Daudé

QOM object instance should not modify its class state (because all other objects instanciated from this class get affected).

Instead of modifying the PMBusDeviceClass 'device_num_pages' field the first time a instance is initialized (in pmbus_pages_alloc), introduce a new pmbus_pages_num() helper which returns the page number from the class without modifying the class state.

Quoted 7 lines

The code logic become slighly simplified.

---
  hw/i2c/pmbus_device.c | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)

r~

#8 lore raw
In reply to Philippe Mathieu-Daudé

(posted too quickly)

Quoted 9 lines · Philippe Mathieu-Daudé

Bernhard warned for QOM class abuse here [*]:

Quoted 5 lines

A realize method is supposed to modify a single instance only while we're modifying the behavior of whole classes here, i.e. will affect every instance of these classes. This goes against QOM design principles and will therefore be confusing for people who are familiar with QOM in particular and OOP in general.

This series fixes the cases I found while auditing.

Audited but not yet fixed:

- accel/xen
xen_init() sets 'default_ram_id'
-> need to figure migration compatibility (because using shared
pc_machine_class_init and DEFINE_PC_MACHINE), will be posted separately.

- hw/core/bus
qbus_init_internal() increments 'automatic_ids'
-> need more thought.

- hw/core/
machine_parse_smp_config() sets 'smp_props.has_clusters' -> dubious

- hw/remote/
remote_object_init() and probe_pci_info() set 'vendor_id', 'nr_devs' -> need more thought.

- qom/object/
object_dynamic_cast_assert() populates object_cast_cache[] -> QOM internal, likely OK.

- softmmu/vl
configure_blockdev() overwrites 'machine_class->block_default_type' -> dubious

- gdbstub
ppc_gdb_gen_spr_xml() sets 'gdb_num_sprs' and 'gdb_spr_xml' -> dubious, likely fixable (maybe like patch #3 of this series)

- tests/unit/
smp_parse_test() set 'smp_props.prefer_sockets'
-> dubious, probably tolerable.

Quoted 1 line

[*] https://lore.kernel.org/qemu-devel/[email protected]/