Skip to contentSkip to conversation

Fix definition of classes with wrong parent

PATCH In review Zhenzhong Duan 3 participants 10 messages Jun 6, 2025 – Jun 9, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover Fix definition of classes with wrong parent

Hi,

This fix definition of some classes with wrong parent. Used below script to get a list, hoping all are addressed.

find "$directory" -type d \( -name "roms" -o -name "build" \) -prune -o -type f \( -name "*.c" -o -name "*.h" \) -print | while read -r file; do

    awk '/^(struct |typedef struct )/ { 
        first = $0;
        getline second; 
        getline third; 
        if (first ~ /[Cc]lass/ && !(second ~ /Class/) && !(third ~ /Class/)) {
            print FILENAME ":" NR-2 ": " first; 
            print FILENAME ":" NR-1 ": " second; 
            print FILENAME ":" NR ": " third;
        }
    }' "$file"

done

patch4/5 are more like cleanups instead of fixes as those class_size are not initialized so class definition doesn't take effect, so no Fixes tag for them.

Thanks
Zhenzhong

Changelog:
v4:
- add comments 'this isn't catastrophic only because sizeof(VirtIODevice) > sizeof(VirtioDeviceClass).' to patch1/2/3 (Markus)
- collect RB for patch5

v3:
- s/Suggested-by/Reported-by on patch2 (David)
- add Closes: tag (David)
- collect RB

v2:
- add more fixes per David

Zhenzhong Duan (5):
virtio-mem: Fix definition of VirtIOMEMClass
virtio-pmem: Fix definition of VirtIOPMEMClass hw/gpio/aspeed: Fix definition of AspeedGPIOClass hw/char/sh_serial: Remove dummy definition of SH_SERIAL class hw/riscv/riscv-iommu: Remove definition of RISCVIOMMU[Pci|Sys]Class

 include/hw/gpio/aspeed_gpio.h   | 2 +-
 include/hw/riscv/iommu.h        | 6 ++----
 include/hw/virtio/virtio-mem.h  | 2 +-
 include/hw/virtio/virtio-pmem.h | 2 +-
 hw/char/sh_serial.c             | 4 +---
 hw/riscv/riscv-iommu-pci.c      | 6 ------
 hw/riscv/riscv-iommu-sys.c      | 6 ------
 7 files changed, 6 insertions(+), 22 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/5 virtio-mem: Fix definition of VirtIOMEMClass

Parent of VirtIOMEMClass is VirtioDeviceClass rather than VirtIODevice. This isn't catastrophic only because sizeof(VirtIODevice) > sizeof(VirtioDeviceClass).

Fixes 910b25766b33 ("virtio-mem: Paravirtualized memory hot(un)plug")
---
 include/hw/virtio/virtio-mem.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/virtio/virtio-mem.h +1 -1
index bc4f787772..e0ab31b45a 100644
@@ -134,7 +134,7 @@ struct VirtioMemSystemReset {
 
 struct VirtIOMEMClass {
     /* private */
-    VirtIODevice parent;
+    VirtioDeviceClass parent_class;
 
     /* public */
     void (*fill_device_info)(const VirtIOMEM *vmen, VirtioMEMDeviceInfo *vi);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 2/5 virtio-pmem: Fix definition of VirtIOPMEMClass

In reply to Zhenzhong Duan

VirtIOPMEMClass's parent is VirtioDeviceClass rather than VirtIODevice. This isn't catastrophic only because sizeof(VirtIODevice) > sizeof(VirtioDeviceClass).

---
 include/hw/virtio/virtio-pmem.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/virtio/virtio-pmem.h +1 -1
index fc4fd1f7fe..9cce600d0b 100644
@@ -36,7 +36,7 @@ struct VirtIOPMEM {
 
 struct VirtIOPMEMClass {
     /* private */
-    VirtIODevice parent;
+    VirtioDeviceClass parent_class;
 
     /* public */
     void (*fill_device_info)(const VirtIOPMEM *pmem, VirtioPMEMDeviceInfo *vi);
Signature · 1 line
2.34.1
#3 lore raw

PATCH v4 3/5 hw/gpio/aspeed: Fix definition of AspeedGPIOClass

In reply to Zhenzhong Duan

AspeedGPIOClass's parent is SysBusDeviceClass rather than SysBusDevice. This isn't catastrophic only because sizeof(SysBusDevice) > sizeof(SysBusDeviceClass).

---
 include/hw/gpio/aspeed_gpio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/gpio/aspeed_gpio.h +1 -1
index e1e6c54333..e6b2fe71b5 100644
@@ -70,7 +70,7 @@ typedef struct AspeedGPIOReg {
 } AspeedGPIOReg;
 
 struct AspeedGPIOClass {
-    SysBusDevice parent_obj;
+    SysBusDeviceClass parent_class;
     const GPIOSetProperties *props;
     uint32_t nr_gpio_pins;
     uint32_t nr_gpio_sets;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v4 4/5 hw/char/sh_serial: Remove dummy definition of SH_SERIAL class

In reply to Zhenzhong Duan

SH_SERIAL is declared with OBJECT_DECLARE_SIMPLE_TYPE but defined with OBJECT_DEFINE_TYPE, SHSerialStateClass is also a dummy class which missed its parent.

Change to use OBJECT_DEFINE_SIMPLE_TYPE and remove SHSerialStateClass.

---
 hw/char/sh_serial.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/char/sh_serial.c +1 -3
index 6abd80386f..8ccc2234ba 100644
@@ -78,9 +78,7 @@ struct SHSerialState {
     qemu_irq bri;
 };
 
-typedef struct {} SHSerialStateClass;
-
-OBJECT_DEFINE_TYPE(SHSerialState, sh_serial, SH_SERIAL, SYS_BUS_DEVICE)
+OBJECT_DEFINE_SIMPLE_TYPE(SHSerialState, sh_serial, SH_SERIAL, SYS_BUS_DEVICE)
 
 static void sh_serial_clear_fifo(SHSerialState *s)
 {
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v4 4/5] hw/char/sh_serial: Remove dummy definition of SH_SERIAL class

Quoted 18 lines · Zhenzhong Duan

SH_SERIAL is declared with OBJECT_DECLARE_SIMPLE_TYPE but defined with OBJECT_DEFINE_TYPE, SHSerialStateClass is also a dummy class which missed its parent.

Change to use OBJECT_DEFINE_SIMPLE_TYPE and remove SHSerialStateClass.

---
  hw/char/sh_serial.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
hw/char/sh_serial.c +0 -0
index 6abd80386f..8ccc2234ba 100644

hw/char/sh_serial.c struct SHSerialState { · line 81

Quoted 7 lines
hw/char/sh_serial.c +1 -3
@@ -78,9 +78,7 @@ struct SHSerialState {
      qemu_irq bri;
  };
 
-typedef struct {} SHSerialStateClass;
-
-OBJECT_DEFINE_TYPE(SHSerialState, sh_serial, SH_SERIAL, SYS_BUS_DEVICE)
+OBJECT_DEFINE_SIMPLE_TYPE(SHSerialState, sh_serial, SH_SERIAL, SYS_BUS_DEVICE)

I'm surprised this is the first time we use OBJECT_DEFINE_SIMPLE_TYPE.

#6 lore raw

Re: [PATCH v4 4/5] hw/char/sh_serial: Remove dummy definition of SH_SERIAL class

Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Zhenzhong Duan

SH_SERIAL is declared with OBJECT_DECLARE_SIMPLE_TYPE but defined with OBJECT_DEFINE_TYPE, SHSerialStateClass is also a dummy class which missed its parent.

Change to use OBJECT_DEFINE_SIMPLE_TYPE and remove SHSerialStateClass.

---
  hw/char/sh_serial.c | 4 +---

  1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 14 lines

hw/char/sh_serial.c struct SHSerialState { · line 81

Quoted 12 lines
hw/char/sh_serial.c +1 -3
index 6abd80386f..8ccc2234ba 100644
@@ -78,9 +78,7 @@ struct SHSerialState {
       qemu_irq bri;
   };
-typedef struct {} SHSerialStateClass;
-
-OBJECT_DEFINE_TYPE(SHSerialState, sh_serial, SH_SERIAL, SYS_BUS_DEVICE)
+OBJECT_DEFINE_SIMPLE_TYPE(SHSerialState, sh_serial, SH_SERIAL, 
 SYS_BUS_DEVICE)

I'm surprised this is the first time we use OBJECT_DEFINE_SIMPLE_TYPE.

Actually I already posted a patch cleaning that...:

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

Also I now notice I never replied to Peter's questions. I'll and queue the series, along with yours.

Regards,

Phil.

#7 lore raw

RE: [PATCH v4 4/5] hw/char/sh_serial: Remove dummy definition of SH_SERIAL class

Quoted 37 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Subject: Re: [PATCH v4 4/5] hw/char/sh_serial: Remove dummy definition of SH_SERIAL class

Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Zhenzhong Duan

SH_SERIAL is declared with OBJECT_DECLARE_SIMPLE_TYPE but defined with OBJECT_DEFINE_TYPE, SHSerialStateClass is also a dummy class which missed its parent.

Change to use OBJECT_DEFINE_SIMPLE_TYPE and remove SHSerialStateClass.

msg00586.html

---
  hw/char/sh_serial.c | 4 +---

  1 file changed, 1 insertion(+), 3 deletions(-)
hw/char/sh_serial.c +0 -0
index 6abd80386f..8ccc2234ba 100644

hw/char/sh_serial.c struct SHSerialState { · line 81

Quoted 7 lines
hw/char/sh_serial.c +1 -3
@@ -78,9 +78,7 @@ struct SHSerialState {
       qemu_irq bri;
   };
-typedef struct {} SHSerialStateClass;
-
-OBJECT_DEFINE_TYPE(SHSerialState, sh_serial, SH_SERIAL, SYS_BUS_DEVICE)
+OBJECT_DEFINE_SIMPLE_TYPE(SHSerialState, sh_serial, SH_SERIAL,
 SYS_BUS_DEVICE)

I'm surprised this is the first time we use OBJECT_DEFINE_SIMPLE_TYPE.

Ah, yes.

Quoted 5 lines

Actually I already posted a patch cleaning that...:

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

Good to see.

Quoted 3 lines

Also I now notice I never replied to Peter's questions. I'll and queue the series, along with yours.

Thanks.

BRs,
Zhenzhong

#8 lore raw

PATCH v4 5/5 hw/riscv/riscv-iommu: Remove definition of RISCVIOMMU[Pci|Sys]Class

In reply to Zhenzhong Duan

RISCVIOMMUPciClass and RISCVIOMMUSysClass are defined with missed parent class, class_init on them may corrupt their parent class fields.

It's lucky that parent_realize and parent_phases are not initialized or used until now, so just remove the definitions. They can be added back when really necessary.

---
 include/hw/riscv/iommu.h   | 6 ++----
 hw/riscv/riscv-iommu-pci.c | 6 ------
 hw/riscv/riscv-iommu-sys.c | 6 ------
 3 files changed, 2 insertions(+), 16 deletions(-)
include/hw/riscv/iommu.h +2 -4
index b03339d75c..8a8acfc3f0 100644
@@ -30,14 +30,12 @@ typedef struct RISCVIOMMUState RISCVIOMMUState;
 typedef struct RISCVIOMMUSpace RISCVIOMMUSpace;
 
 #define TYPE_RISCV_IOMMU_PCI "riscv-iommu-pci"
-OBJECT_DECLARE_TYPE(RISCVIOMMUStatePci, RISCVIOMMUPciClass, RISCV_IOMMU_PCI)
+OBJECT_DECLARE_SIMPLE_TYPE(RISCVIOMMUStatePci, RISCV_IOMMU_PCI)
 typedef struct RISCVIOMMUStatePci RISCVIOMMUStatePci;
-typedef struct RISCVIOMMUPciClass RISCVIOMMUPciClass;
 
 #define TYPE_RISCV_IOMMU_SYS "riscv-iommu-device"
-OBJECT_DECLARE_TYPE(RISCVIOMMUStateSys, RISCVIOMMUSysClass, RISCV_IOMMU_SYS)
+OBJECT_DECLARE_SIMPLE_TYPE(RISCVIOMMUStateSys, RISCV_IOMMU_SYS)
 typedef struct RISCVIOMMUStateSys RISCVIOMMUStateSys;
-typedef struct RISCVIOMMUSysClass RISCVIOMMUSysClass;
 
 #define FDT_IRQ_TYPE_EDGE_LOW 1
 
hw/riscv/riscv-iommu-pci.c +0 -6
index 1f44eef74e..cdb4a7a8f0 100644
@@ -68,12 +68,6 @@ typedef struct RISCVIOMMUStatePci {
     RISCVIOMMUState  iommu;   /* common IOMMU state */
 } RISCVIOMMUStatePci;
 
-struct RISCVIOMMUPciClass {
-    /*< public >*/
-    DeviceRealize parent_realize;
-    ResettablePhases parent_phases;
-};
-
 /* interrupt delivery callback */
 static void riscv_iommu_pci_notify(RISCVIOMMUState *iommu, unsigned vector)
 {
hw/riscv/riscv-iommu-sys.c +0 -6
index 74e76b94a5..e34d00aef6 100644
@@ -53,12 +53,6 @@ struct RISCVIOMMUStateSys {
     uint8_t *msix_pba;
 };
 
-struct RISCVIOMMUSysClass {
-    /*< public >*/
-    DeviceRealize parent_realize;
-    ResettablePhases parent_phases;
-};
-
 static uint64_t msix_table_mmio_read(void *opaque, hwaddr addr,
                                      unsigned size)
 {
Signature · 1 line
2.34.1
Acked-by on v4 5/5
#9 lore raw

Re: [PATCH v4 5/5] hw/riscv/riscv-iommu: Remove definition of RISCVIOMMU[Pci|Sys]Class

Quoted 11 lines · Zhenzhong Duan

RISCVIOMMUPciClass and RISCVIOMMUSysClass are defined with missed parent class, class_init on them may corrupt their parent class fields.

It's lucky that parent_realize and parent_phases are not initialized or used until now, so just remove the definitions. They can be added back when really necessary.

Alistair

Quoted 65 lines
---
 include/hw/riscv/iommu.h   | 6 ++----
 hw/riscv/riscv-iommu-pci.c | 6 ------
 hw/riscv/riscv-iommu-sys.c | 6 ------
 3 files changed, 2 insertions(+), 16 deletions(-)
include/hw/riscv/iommu.h +2 -4
index b03339d75c..8a8acfc3f0 100644
@@ -30,14 +30,12 @@ typedef struct RISCVIOMMUState RISCVIOMMUState;
 typedef struct RISCVIOMMUSpace RISCVIOMMUSpace;
 
 #define TYPE_RISCV_IOMMU_PCI "riscv-iommu-pci"
-OBJECT_DECLARE_TYPE(RISCVIOMMUStatePci, RISCVIOMMUPciClass, RISCV_IOMMU_PCI)
+OBJECT_DECLARE_SIMPLE_TYPE(RISCVIOMMUStatePci, RISCV_IOMMU_PCI)
 typedef struct RISCVIOMMUStatePci RISCVIOMMUStatePci;
-typedef struct RISCVIOMMUPciClass RISCVIOMMUPciClass;
 
 #define TYPE_RISCV_IOMMU_SYS "riscv-iommu-device"
-OBJECT_DECLARE_TYPE(RISCVIOMMUStateSys, RISCVIOMMUSysClass, RISCV_IOMMU_SYS)
+OBJECT_DECLARE_SIMPLE_TYPE(RISCVIOMMUStateSys, RISCV_IOMMU_SYS)
 typedef struct RISCVIOMMUStateSys RISCVIOMMUStateSys;
-typedef struct RISCVIOMMUSysClass RISCVIOMMUSysClass;
 
 #define FDT_IRQ_TYPE_EDGE_LOW 1
 
hw/riscv/riscv-iommu-pci.c +0 -6
index 1f44eef74e..cdb4a7a8f0 100644
@@ -68,12 +68,6 @@ typedef struct RISCVIOMMUStatePci {
     RISCVIOMMUState  iommu;   /* common IOMMU state */
 } RISCVIOMMUStatePci;
 
-struct RISCVIOMMUPciClass {
-    /*< public >*/
-    DeviceRealize parent_realize;
-    ResettablePhases parent_phases;
-};
-
 /* interrupt delivery callback */
 static void riscv_iommu_pci_notify(RISCVIOMMUState *iommu, unsigned vector)
 {
hw/riscv/riscv-iommu-sys.c +0 -7
index 74e76b94a5..e34d00aef6 100644
@@ -53,12 +53,6 @@ struct RISCVIOMMUStateSys {
     uint8_t *msix_pba;
 };
 
-struct RISCVIOMMUSysClass {
-    /*< public >*/
-    DeviceRealize parent_realize;
-    ResettablePhases parent_phases;
-};
-
 static uint64_t msix_table_mmio_read(void *opaque, hwaddr addr,
                                      unsigned size)
 {
--

2.34.1