Skip to contentSkip to conversation

qom: Make composition-tree parenting mandatory

RFC PATCH Superseded Alexander Graf 9 participants 295 messages Jul 11 – Jul 20 Page 7 of 7

Subsystems (11)
lore
Share & tools
lore
#266 lore raw

RFC PATCH v2 109/137 irq: Rename qemu_allocate_irq*() and friends to *_orphan()

In reply to Alexander Graf

Mechanical rename of the IRQ allocation helpers that create an IRQState via object_new() with no QOM parent:

  qemu_allocate_irq   -> qemu_allocate_irq_orphan
  qemu_allocate_irqs  -> qemu_allocate_irqs_orphan
  qemu_extend_irqs    -> qemu_extend_irqs_orphan
  qemu_irq_invert     -> qemu_irq_invert_orphan

The next commit reintroduces the well-known names with a mandatory (owner, name, ...) prefix, and later commits convert every caller per subdirectory before deleting the *_orphan() variants.

Coccinelle: scripts/coccinelle/qom-parent/irq-rename.cocci

  git ls-files '*.c' '*.h' | grep -v '^subprojects/' |
    xargs sed -i -e 's/\bqemu_allocate_irqs\b/qemu_allocate_irqs_orphan/g' \
                 -e 's/\bqemu_allocate_irq\b/qemu_allocate_irq_orphan/g' \
                 -e 's/\bqemu_extend_irqs\b/qemu_extend_irqs_orphan/g' \
                 -e 's/\bqemu_irq_invert\b/qemu_irq_invert_orphan/g'

AI-used-for: code (refactoring)

---
 hw/alpha/typhoon.c                             |  4 ++--
 hw/arm/armsse.c                                |  4 ++--
 hw/arm/exynos4_boards.c                        |  2 +-
 hw/arm/mps2-tz.c                               |  2 +-
 hw/arm/omap1.c                                 | 14 +++++++-------
 hw/arm/realview.c                              |  2 +-
 hw/arm/stellaris.c                             | 10 +++++-----
 hw/char/diva-gsp.c                             |  2 +-
 hw/core/gpio.c                                 |  2 +-
 hw/core/irq.c                                  | 14 +++++++-------
 hw/display/macfb.c                             |  2 +-
 hw/dma/omap_dma.c                              |  4 ++--
 hw/i386/kvm/i8259.c                            |  2 +-
 hw/i386/microvm.c                              |  2 +-
 hw/i386/pc.c                                   |  4 ++--
 hw/i386/pc_piix.c                              |  2 +-
 hw/i386/x86-cpu.c                              |  2 +-
 hw/i386/xen/xen-hvm.c                          |  2 +-
 hw/ide/ahci.c                                  |  2 +-
 hw/ide/pci.c                                   |  2 +-
 hw/intc/sh_intc.c                              |  2 +-
 hw/isa/i82378.c                                |  2 +-
 hw/isa/lpc_ich9.c                              |  2 +-
 hw/m68k/mcf5206.c                              |  2 +-
 hw/m68k/q800.c                                 |  4 ++--
 hw/mips/mips_int.c                             |  2 +-
 hw/net/can/can_kvaser_pci.c                    |  2 +-
 hw/pci-host/pnv_phb3.c                         |  2 +-
 hw/pci-host/pnv_phb3_msi.c                     |  2 +-
 hw/pci-host/pnv_phb4.c                         |  2 +-
 hw/pci/pci.c                                   |  2 +-
 hw/ppc/e500.c                                  |  2 +-
 hw/ppc/pnv_lpc.c                               |  2 +-
 hw/ppc/pnv_psi.c                               |  4 ++--
 hw/ppc/spapr_irq.c                             |  2 +-
 hw/riscv/sifive_u.c                            |  2 +-
 hw/scsi/esp-pci.c                              |  2 +-
 hw/sh4/sh7750.c                                |  2 +-
 hw/sparc/sun4m.c                               |  4 ++--
 hw/sparc64/sun4u.c                             |  2 +-
 hw/timer/arm_timer.c                           |  4 ++--
 hw/usb/vt82c686-uhci-pci.c                     |  2 +-
 hw/xen/xen-pvh-common.c                        |  4 ++--
 hw/xtensa/mx_pic.c                             |  2 +-
 hw/xtensa/pic_cpu.c                            |  4 ++--
 include/hw/core/irq.h                          |  8 ++++----
 scripts/coccinelle/qom-parent/irq-rename.cocci | 15 +++++++++++++++
 system/qtest.c                                 |  2 +-
 48 files changed, 91 insertions(+), 76 deletions(-)
 create mode 100644 scripts/coccinelle/qom-parent/irq-rename.cocci
hw/alpha/typhoon.c +2 -2
index af39f2659b..671e8f89d9 100644
@@ -849,8 +849,8 @@ PCIBus *typhoon_init(Object *parent, MemoryRegion *ram, qemu_irq *p_isa_irq,
         }
     }
 
-    *p_isa_irq = qemu_allocate_irq(typhoon_set_isa_irq, s, 0);
-    *p_rtc_irq = qemu_allocate_irq(typhoon_set_timer_irq, s, 0);
+    *p_isa_irq = qemu_allocate_irq_orphan(typhoon_set_isa_irq, s, 0);
+    *p_rtc_irq = qemu_allocate_irq_orphan(typhoon_set_timer_irq, s, 0);
 
     /* Main memory region, 0x00.0000.0000.  Real hardware supports 32GB,
        but the address space hole reserved at this point is 8TB.  */
hw/arm/armsse.c +2 -2
index 81895ad370..93f6715224 100644
@@ -693,7 +693,7 @@ static void armsse_forward_sec_resp_cfg(ARMSSE *s)
     DeviceState *dev_splitter = DEVICE(&s->sec_resp_splitter);
 
     qdev_init_gpio_out_named(dev, &s->sec_resp_cfg, "sec_resp_cfg", 1);
-    s->sec_resp_cfg_in = qemu_allocate_irq(irq_status_forwarder,
+    s->sec_resp_cfg_in = qemu_allocate_irq_orphan(irq_status_forwarder,
                                            s->sec_resp_cfg, 1);
     qdev_connect_gpio_out(dev_splitter, 2, s->sec_resp_cfg_in);
 }
@@ -1137,7 +1137,7 @@ static void armsse_realize(DeviceState *dev, Error **errp)
     sysbus_mmio_map(sbd_secctl, 0, 0x50080000);
     sysbus_mmio_map(sbd_secctl, 1, 0x40080000);
 
-    s->nsc_cfg_in = qemu_allocate_irq(nsccfg_handler, s, 1);
+    s->nsc_cfg_in = qemu_allocate_irq_orphan(nsccfg_handler, s, 1);
     qdev_connect_gpio_out_named(dev_secctl, "nsc_cfg", 0, s->nsc_cfg_in);
 
     /* The sec_resp_cfg output from the security controller must be split into
hw/arm/exynos4_boards.c +1 -1
index 8430317815..537a50f8cc 100644
@@ -146,7 +146,7 @@ static void smdkc210_init(MachineState *machine)
                                                       EXYNOS4_BOARD_SMDKC210);
 
     lan9215_init(SMDK_LAN9118_BASE_ADDR,
-            qemu_irq_invert(s->soc.irq_table[exynos4210_get_irq(37, 1)]));
+            qemu_irq_invert_orphan(s->soc.irq_table[exynos4210_get_irq(37, 1)]));
     arm_load_kernel(s->soc.cpu[0], machine, &exynos4_board_binfo);
 }
 
hw/arm/mps2-tz.c +1 -1
index a57915e208..654eaedd97 100644
@@ -1209,7 +1209,7 @@ static void mps2tz_common_init(MachineState *machine)
          * Connect the line from the SCC so that we can remap when the
          * guest updates that register.
          */
-        mms->remap_irq = qemu_allocate_irq(remap_irq_fn, mms, 0);
+        mms->remap_irq = qemu_allocate_irq_orphan(remap_irq_fn, mms, 0);
         qdev_connect_gpio_out_named(DEVICE(&mms->scc), "remap", 0,
                                     mms->remap_irq);
     }
hw/arm/omap1.c +7 -7
index 474a3902fe..53e52ef34e 100644
@@ -139,7 +139,7 @@ static void omap_timer_clk_update(void *opaque, int line, int on)
 static void omap_timer_clk_setup(struct omap_mpu_timer_s *timer)
 {
     omap_clk_adduser(timer->clk,
-                    qemu_allocate_irq(omap_timer_clk_update, timer, 0));
+                    qemu_allocate_irq_orphan(omap_timer_clk_update, timer, 0));
     timer->rate = omap_clk_getrate(timer->clk);
 }
 
@@ -2083,14 +2083,14 @@ static struct omap_mpuio_s *omap_mpuio_init(Object *owner,
 
     s->irq = gpio_int;
     s->kbd_irq = kbd_int;
-    s->in = qemu_allocate_irqs(omap_mpuio_set, s, 16);
+    s->in = qemu_allocate_irqs_orphan(omap_mpuio_set, s, 16);
     omap_mpuio_reset(s);
 
     memory_region_init_io(&s->iomem, owner, &omap_mpuio_ops, s,
                           "omap-mpuio", 0x800);
     memory_region_add_subregion(memory, base, &s->iomem);
 
-    omap_clk_adduser(clk, qemu_allocate_irq(omap_mpuio_onoff, s, 0));
+    omap_clk_adduser(clk, qemu_allocate_irq_orphan(omap_mpuio_onoff, s, 0));
 
     return s;
 }
@@ -2363,7 +2363,7 @@ static struct omap_pwl_s *omap_pwl_init(Object *owner,
                           "omap-pwl", 0x800);
     memory_region_add_subregion(system_memory, base, &s->iomem);
 
-    omap_clk_adduser(clk, qemu_allocate_irq(omap_pwl_clk_update, s, 0));
+    omap_clk_adduser(clk, qemu_allocate_irq_orphan(omap_pwl_clk_update, s, 0));
     return s;
 }
 
@@ -3426,8 +3426,8 @@ static void omap_mcbsp_i2s_start(void *opaque, int line, int level)
 void omap_mcbsp_i2s_attach(struct omap_mcbsp_s *s, I2SCodec *slave)
 {
     s->codec = slave;
-    slave->rx_swallow = qemu_allocate_irq(omap_mcbsp_i2s_swallow, s, 0);
-    slave->tx_start = qemu_allocate_irq(omap_mcbsp_i2s_start, s, 0);
+    slave->rx_swallow = qemu_allocate_irq_orphan(omap_mcbsp_i2s_swallow, s, 0);
+    slave->tx_start = qemu_allocate_irq_orphan(omap_mcbsp_i2s_start, s, 0);
 }
 
 /* LED Pulse Generators */
@@ -3579,7 +3579,7 @@ static struct omap_lpg_s *omap_lpg_init(Object *owner,
                           "omap-lpg", 0x800);
     memory_region_add_subregion(system_memory, base, &s->iomem);
 
-    omap_clk_adduser(clk, qemu_allocate_irq(omap_lpg_clk_update, s, 0));
+    omap_clk_adduser(clk, qemu_allocate_irq_orphan(omap_lpg_clk_update, s, 0));
 
     return s;
 }
hw/arm/realview.c +1 -1
index 73bfffa6e6..cd63f39f42 100644
@@ -276,7 +276,7 @@ static void realview_init(MachineState *machine,
 
     split_irq_from_named(OBJECT(machine), dev, "card-inserted",
                    qdev_get_gpio_in(sysctl, ARM_SYSCTL_GPIO_MMC_CARDIN),
-                   qemu_irq_invert(qdev_get_gpio_in(gpio2, 0)));
+                   qemu_irq_invert_orphan(qdev_get_gpio_in(gpio2, 0)));
 
     dinfo = drive_get(IF_SD, 0, 0);
     if (dinfo) {
hw/arm/stellaris.c +5 -5
index 1bc428e063..a5d037094a 100644
@@ -1358,15 +1358,15 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
         sysbus_realize(SYS_BUS_DEVICE(gpad), &error_fatal);
 
         qdev_connect_gpio_out(gpad, 0,
-                              qemu_irq_invert(gpio_in[GPIO_E][0])); /* up */
+                              qemu_irq_invert_orphan(gpio_in[GPIO_E][0])); /* up */
         qdev_connect_gpio_out(gpad, 1,
-                              qemu_irq_invert(gpio_in[GPIO_E][1])); /* down */
+                              qemu_irq_invert_orphan(gpio_in[GPIO_E][1])); /* down */
         qdev_connect_gpio_out(gpad, 2,
-                              qemu_irq_invert(gpio_in[GPIO_E][2])); /* left */
+                              qemu_irq_invert_orphan(gpio_in[GPIO_E][2])); /* left */
         qdev_connect_gpio_out(gpad, 3,
-                              qemu_irq_invert(gpio_in[GPIO_E][3])); /* right */
+                              qemu_irq_invert_orphan(gpio_in[GPIO_E][3])); /* right */
         qdev_connect_gpio_out(gpad, 4,
-                              qemu_irq_invert(gpio_in[GPIO_F][1])); /* select */
+                              qemu_irq_invert_orphan(gpio_in[GPIO_F][1])); /* select */
     }
     for (i = 0; i < 7; i++) {
         if (board->dc4 & (1 << i)) {
hw/char/diva-gsp.c +1 -1
index 53fd0fe2a1..bf1da1faee 100644
@@ -126,7 +126,7 @@ static void diva_pci_realize(PCIDevice *dev, Error **errp)
     pci->dev.config[PCI_INTERRUPT_PIN] = 1;
     memory_region_init(&pci->membar, OBJECT(pci), "serial_ports", 4096);
     pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &pci->membar);
-    pci->irqs = qemu_allocate_irqs(multi_serial_irq_mux, pci, di.nports);
+    pci->irqs = qemu_allocate_irqs_orphan(multi_serial_irq_mux, pci, di.nports);
 
     for (i = 0; i < di.nports; i++) {
         s = pci->state + i;
hw/core/gpio.c +1 -1
index 37cda5ce4d..ef8ef8f25a 100644
@@ -49,7 +49,7 @@ void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
     NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
 
     assert(gpio_list->num_out == 0 || !name);
-    gpio_list->in = qemu_extend_irqs(gpio_list->in, gpio_list->num_in, handler,
+    gpio_list->in = qemu_extend_irqs_orphan(gpio_list->in, gpio_list->num_in, handler,
                                      opaque, n);
 
     if (!name) {
hw/core/irq.c +7 -7
index 1b610e75e1..f8fdeb7e02 100644
@@ -68,7 +68,7 @@ void qemu_init_irqs(IRQState irq[], size_t count,
     }
 }
 
-qemu_irq *qemu_extend_irqs(qemu_irq *old, int n_old, qemu_irq_handler handler,
+qemu_irq *qemu_extend_irqs_orphan(qemu_irq *old, int n_old, qemu_irq_handler handler,
                            void *opaque, int n)
 {
     qemu_irq *s;
@@ -79,17 +79,17 @@ qemu_irq *qemu_extend_irqs(qemu_irq *old, int n_old, qemu_irq_handler handler,
     }
     s = old ? g_renew(qemu_irq, old, n + n_old) : g_new(qemu_irq, n);
     for (i = n_old; i < n + n_old; i++) {
-        s[i] = qemu_allocate_irq(handler, opaque, i);
+        s[i] = qemu_allocate_irq_orphan(handler, opaque, i);
     }
     return s;
 }
 
-qemu_irq *qemu_allocate_irqs(qemu_irq_handler handler, void *opaque, int n)
+qemu_irq *qemu_allocate_irqs_orphan(qemu_irq_handler handler, void *opaque, int n)
 {
-    return qemu_extend_irqs(NULL, 0, handler, opaque, n);
+    return qemu_extend_irqs_orphan(NULL, 0, handler, opaque, n);
 }
 
-qemu_irq qemu_allocate_irq(qemu_irq_handler handler, void *opaque, int n)
+qemu_irq qemu_allocate_irq_orphan(qemu_irq_handler handler, void *opaque, int n)
 {
     IRQState *irq = IRQ(object_new(TYPE_IRQ));
     init_irq_fields(irq, handler, opaque, n);
@@ -117,11 +117,11 @@ static void qemu_notirq(void *opaque, int line, int level)
     qemu_set_irq(irq, !level);
 }
 
-qemu_irq qemu_irq_invert(qemu_irq irq)
+qemu_irq qemu_irq_invert_orphan(qemu_irq irq)
 {
     /* The default state for IRQs is low, so raise the output now.  */
     qemu_irq_raise(irq);
-    return qemu_allocate_irq(qemu_notirq, irq, 0);
+    return qemu_allocate_irq_orphan(qemu_notirq, irq, 0);
 }
 
 void qemu_irq_set_observer(qemu_irq *gpio_in, qemu_irq_handler handler, int n)
hw/display/macfb.c +1 -1
index f40a7ed9f5..de5c83884e 100644
@@ -737,7 +737,7 @@ static void macfb_nubus_realize(DeviceState *dev, Error **errp)
     memory_region_add_subregion(&nd->slot_mem, DAFB_BASE, &ms->mem_ctrl);
     memory_region_add_subregion(&nd->slot_mem, VIDEO_BASE, &ms->mem_vram);
 
-    ms->irq = qemu_allocate_irq(macfb_nubus_set_irq, s, 0);
+    ms->irq = qemu_allocate_irq_orphan(macfb_nubus_set_irq, s, 0);
 }
 
 static void macfb_nubus_unrealize(DeviceState *dev)
hw/dma/omap_dma.c +2 -2
index b130ba2480..7a2a3310f9 100644
@@ -1109,7 +1109,7 @@ struct soc_dma_s *omap_dma_init(Object *owner, hwaddr base, qemu_irq *irqs,
     s->dma->freq = omap_clk_getrate(clk);
     s->dma->transfer_fn = omap_dma_transfer_generic;
     s->dma->setup_fn = omap_dma_transfer_setup;
-    s->dma->drq = qemu_allocate_irqs(omap_dma_request, s, 32);
+    s->dma->drq = qemu_allocate_irqs_orphan(omap_dma_request, s, 32);
     s->dma->opaque = s;
 
     while (num_irqs --)
@@ -1123,7 +1123,7 @@ struct soc_dma_s *omap_dma_init(Object *owner, hwaddr base, qemu_irq *irqs,
         s->dma->ch[i].opaque = &s->ch[i];
     }
 
-    omap_clk_adduser(s->clk, qemu_allocate_irq(omap_dma_clk_update, s, 0));
+    omap_clk_adduser(s->clk, qemu_allocate_irq_orphan(omap_dma_clk_update, s, 0));
     omap_dma_reset(s->dma);
     omap_dma_clk_update(s, 0, 1);
 
hw/i386/kvm/i8259.c +1 -1
index 5fb11d3613..7ef7a0b487 100644
@@ -136,7 +136,7 @@ qemu_irq *kvm_i8259_init(Object *parent, ISABus *bus)
     i8259_init_chip(parent, TYPE_KVM_I8259, bus, true);
     i8259_init_chip(parent, TYPE_KVM_I8259, bus, false);
 
-    return qemu_allocate_irqs(kvm_pic_set_irq, NULL, ISA_NUM_IRQS);
+    return qemu_allocate_irqs_orphan(kvm_pic_set_irq, NULL, ISA_NUM_IRQS);
 }
 
 static void kvm_i8259_class_init(ObjectClass *klass, const void *data)
hw/i386/microvm.c +1 -1
index 751e3962fa..07dbe3a18b 100644
@@ -168,7 +168,7 @@ static void microvm_devices_init(MicrovmMachineState *mms)
     /* Core components */
     ioapics = microvm_ioapics(mms);
     gsi_state = g_malloc0(sizeof(*gsi_state));
-    x86ms->gsi = qemu_allocate_irqs(gsi_handler, gsi_state,
+    x86ms->gsi = qemu_allocate_irqs_orphan(gsi_handler, gsi_state,
                                     IOAPIC_NUM_PINS * ioapics);
 
     isa_bus = isa_bus_new_bridge(OBJECT(mms), get_system_memory(), get_system_io(),
hw/i386/pc.c +2 -2
index 8057058ff0..249fa699c7 100644
@@ -200,7 +200,7 @@ GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled)
     if (kvm_ioapic_in_kernel()) {
         kvm_pc_setup_irq_routing(pci_enabled);
     }
-    *irqs = qemu_allocate_irqs(gsi_handler, s, IOAPIC_NUM_PINS);
+    *irqs = qemu_allocate_irqs_orphan(gsi_handler, s, IOAPIC_NUM_PINS);
 
     return s;
 }
@@ -1028,7 +1028,7 @@ static void pc_superio_init(Object *parent, ISABus *isa_bus, bool create_fdctrl,
     }
     port92 = isa_create_simple(parent, "port92", isa_bus, TYPE_PORT92);
 
-    a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
+    a20_line = qemu_allocate_irqs_orphan(handle_a20_line_change, first_cpu, 2);
     qdev_connect_gpio_out_named(DEVICE(i8042),
                                 I8042_A20_LINE, 0, a20_line[0]);
     qdev_connect_gpio_out_named(DEVICE(port92),
hw/i386/pc_piix.c +1 -1
index 0b8d7d88b9..f76d684d6b 100644
@@ -299,7 +299,7 @@ static void pc_init1(MachineState *machine, const char *pci_type)
     pc_nic_init(pcmc, isa_bus, pcms->pcibus);
 
     if (piix4_pm) {
-        smi_irq = qemu_allocate_irq(pc_acpi_smi_interrupt, first_cpu, 0);
+        smi_irq = qemu_allocate_irq_orphan(pc_acpi_smi_interrupt, first_cpu, 0);
 
         qdev_connect_gpio_out_named(DEVICE(piix4_pm), "smi-irq", 0, smi_irq);
         pcms->smbus = I2C_BUS(qdev_get_child_bus(DEVICE(piix4_pm), "i2c"));
hw/i386/x86-cpu.c +1 -1
index 95e08e3c2a..6d010291cf 100644
@@ -63,7 +63,7 @@ static void pic_irq_request(void *opaque, int irq, int level)
 
 qemu_irq x86_allocate_cpu_irq(void)
 {
-    return qemu_allocate_irq(pic_irq_request, NULL, 0);
+    return qemu_allocate_irq_orphan(pic_irq_request, NULL, 0);
 }
 
 int cpu_get_pic_interrupt(CPUX86State *env)
hw/i386/xen/xen-hvm.c +1 -1
index 01d851ce3e..e87c8237ea 100644
@@ -114,7 +114,7 @@ static void xen_set_irq(void *opaque, int irq, int level)
 
 qemu_irq *xen_interrupt_controller_init(void)
 {
-    return qemu_allocate_irqs(xen_set_irq, NULL, 16);
+    return qemu_allocate_irqs_orphan(xen_set_irq, NULL, 16);
 }
 
 /* Memory Ops */
hw/ide/ahci.c +1 -1
index 749f0efa1d..ac1cf26bab 100644
@@ -1594,7 +1594,7 @@ void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as)
     assert(s->ports > 0);
     s->dev = g_new0(AHCIDevice, s->ports);
     ahci_reg_init(s);
-    irqs = qemu_allocate_irqs(ahci_irq_set, s, s->ports);
+    irqs = qemu_allocate_irqs_orphan(ahci_irq_set, s, s->ports);
     for (i = 0; i < s->ports; i++) {
         AHCIDevice *ad = &s->dev[i];
 
hw/ide/pci.c +1 -1
index 7ce1ae67ab..c808469008 100644
@@ -606,7 +606,7 @@ void bmdma_init(IDEBus *bus, BMDMAState *bm, PCIIDEState *d)
     bm->dma.ops = &bmdma_ops;
     bus->dma = &bm->dma;
     bm->irq = bus->irq;
-    bus->irq = qemu_allocate_irq(bmdma_irq, bm, 0);
+    bus->irq = qemu_allocate_irq_orphan(bmdma_irq, bm, 0);
     bm->bus = bus;
     bm->pci_dev = d;
 }
hw/intc/sh_intc.c +1 -1
index 37feb6a813..63f4eee2aa 100644
@@ -403,7 +403,7 @@ int sh_intc_init(Object *owner, MemoryRegion *sysmem,
     for (i = 0; i < nr_sources; i++) {
         desc->sources[i].parent = desc;
     }
-    desc->irqs = qemu_allocate_irqs(sh_intc_set_irq, desc, nr_sources);
+    desc->irqs = qemu_allocate_irqs_orphan(sh_intc_set_irq, desc, nr_sources);
     memory_region_init_io(&desc->iomem, owner, &sh_intc_ops, desc, "intc",
                           0x100000000ULL);
     j = 0;
hw/isa/i82378.c +1 -1
index 995d1eaa53..4f0be992e0 100644
@@ -95,7 +95,7 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
 
     /* 2 82C59 (irq) */
     s->isa_irqs_in = i8259_init(OBJECT(pci), isabus,
-                                qemu_allocate_irq(i82378_request_out0_irq,
+                                qemu_allocate_irq_orphan(i82378_request_out0_irq,
                                                   s, 0));
     isa_bus_register_input_irqs(isabus, s->isa_irqs_in);
 
hw/isa/lpc_ich9.c +1 -1
index edf9783ec8..7cb82519fb 100644
@@ -429,7 +429,7 @@ static void ich9_lpc_pm_init(ICH9LPCState *lpc)
     qemu_irq sci_irq;
     FWCfgState *fw_cfg = fw_cfg_find();
 
-    sci_irq = qemu_allocate_irq(ich9_set_sci, lpc, 0);
+    sci_irq = qemu_allocate_irq_orphan(ich9_set_sci, lpc, 0);
     ich9_pm_init(PCI_DEVICE(lpc), &lpc->pm, sci_irq);
 
     if (lpc->smi_host_features && fw_cfg) {
hw/m68k/mcf5206.c +1 -1
index ee39a6ac30..cbb361fae9 100644
@@ -593,7 +593,7 @@ static void mcf5206_mbar_realize(DeviceState *dev, Error **errp)
                           "mbar", 0x00001000);
     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
 
-    s->pic = qemu_allocate_irqs(m5206_mbar_set_irq, s, 14);
+    s->pic = qemu_allocate_irqs_orphan(m5206_mbar_set_irq, s, 14);
     m5206_timer_init(&s->timer[0], s->pic[9]);
     m5206_timer_init(&s->timer[1], s->pic[10]);
     s->uart[0] = mcf_uart_create(OBJECT(s), s->pic[12], serial_hd(0));
hw/m68k/q800.c +2 -2
index 954cdf8489..177fff0289 100644
@@ -474,11 +474,11 @@ static void q800_machine_init(MachineState *machine)
     sysbus_realize(sysbus, &error_fatal);
     /* SCSI and SCSI data IRQs are negative edge triggered */
     sysbus_connect_irq(sysbus, 0,
-                       qemu_irq_invert(
+                       qemu_irq_invert_orphan(
                            qdev_get_gpio_in(DEVICE(&m->via2),
                                                    VIA2_IRQ_SCSI_BIT)));
     sysbus_connect_irq(sysbus, 1,
-                       qemu_irq_invert(
+                       qemu_irq_invert_orphan(
                            qdev_get_gpio_in(DEVICE(&m->via2),
                                                    VIA2_IRQ_SCSI_DATA_BIT)));
     memory_region_add_subregion(&m->macio, ESP_BASE - IO_BASE,
hw/mips/mips_int.c +1 -1
index 26e7dd6d5f..814d07319f 100644
@@ -56,7 +56,7 @@ void cpu_mips_irq_init_cpu(MIPSCPU *cpu)
     qemu_irq *qi;
     int i;
 
-    qi = qemu_allocate_irqs(cpu_mips_irq_request, cpu, 8);
+    qi = qemu_allocate_irqs_orphan(cpu_mips_irq_request, cpu, 8);
     for (i = 0; i < 8; i++) {
         env->irq[i] = qi[i];
     }
hw/net/can/can_kvaser_pci.c +1 -1
index f2912b206d..69fc66de14 100644
@@ -224,7 +224,7 @@ static void kvaser_pci_realize(PCIDevice *pci_dev, Error **errp)
     pci_conf = pci_dev->config;
     pci_conf[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
 
-    d->irq = qemu_allocate_irq(kvaser_pci_irq_handler, d, 0);
+    d->irq = qemu_allocate_irq_orphan(kvaser_pci_irq_handler, d, 0);
 
     can_sja_init(s, d->irq);
 
hw/pci-host/pnv_phb3.c +1 -1
index 4ffdb4ce31..63da48f5af 100644
@@ -1054,7 +1054,7 @@ static void pnv_phb3_realize(DeviceState *dev, Error **errp)
         ics_set_irq_type(&phb->lsis, i, true);
     }
 
-    phb->qirqs = qemu_allocate_irqs(ics_set_irq, &phb->lsis, phb->lsis.nr_irqs);
+    phb->qirqs = qemu_allocate_irqs_orphan(ics_set_irq, &phb->lsis, phb->lsis.nr_irqs);
 
     /* MSI sources */
     object_property_set_link(OBJECT(&phb->msis), "phb", OBJECT(phb),
hw/pci-host/pnv_phb3_msi.c +1 -1
index 66ba7b7913..fa5f61ea2b 100644
@@ -267,7 +267,7 @@ static void phb3_msi_realize(DeviceState *dev, Error **errp)
         return;
     }
 
-    msi->qirqs = qemu_allocate_irqs(phb3_msi_set_irq, msi, ics->nr_irqs);
+    msi->qirqs = qemu_allocate_irqs_orphan(phb3_msi_set_irq, msi, ics->nr_irqs);
 }
 
 static void phb3_msi_instance_init(Object *obj)
hw/pci-host/pnv_phb4.c +1 -1
index 705a5bcf07..7a1988a2b2 100644
@@ -1605,7 +1605,7 @@ static void pnv_phb4_realize(DeviceState *dev, Error **errp)
 
     pnv_phb4_update_xsrc(phb);
 
-    phb->qirqs = qemu_allocate_irqs(xive_source_set_irq, xsrc, xsrc->nr_irqs);
+    phb->qirqs = qemu_allocate_irqs_orphan(xive_source_set_irq, xsrc, xsrc->nr_irqs);
 
     pnv_phb4_xscom_realize(phb);
 }
hw/pci/pci.c +1 -1
index 5ef9dfa8b7..9ba7bfd977 100644
@@ -1869,7 +1869,7 @@ qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
     int intx = pci_intx(pci_dev);
     assert(0 <= intx && intx < PCI_NUM_PINS);
 
-    return qemu_allocate_irq(pci_irq_handler, pci_dev, intx);
+    return qemu_allocate_irq_orphan(pci_irq_handler, pci_dev, intx);
 }
 
 void pci_set_irq(PCIDevice *pci_dev, int level)
hw/ppc/e500.c +1 -1
index 21e656e87f..62692f5776 100644
@@ -1089,7 +1089,7 @@ void ppce500_init(MachineState *machine)
                                     sysbus_mmio_get_region(s, 0));
 
         /* Power Off GPIO at Pin 0 */
-        poweroff_irq = qemu_allocate_irq(ppce500_power_off, NULL, 0);
+        poweroff_irq = qemu_allocate_irq_orphan(ppce500_power_off, NULL, 0);
         qdev_connect_gpio_out(dev, 0, poweroff_irq);
     }
 
hw/ppc/pnv_lpc.c +1 -1
index 6ae228088f..e89defb321 100644
@@ -993,7 +993,7 @@ ISABus *pnv_lpc_isa_create(PnvLpcController *lpc, bool use_cpld, Error **errp)
     }
 
     /* POWER has a 17th irq, QEMU only implements the 16 regular device irqs */
-    irqs = qemu_allocate_irqs(handler, lpc, ISA_NUM_IRQS);
+    irqs = qemu_allocate_irqs_orphan(handler, lpc, ISA_NUM_IRQS);
 
     isa_bus_register_input_irqs(isa_bus, irqs);
 
hw/ppc/pnv_psi.c +2 -2
index e8701c6100..1ff33596ac 100644
@@ -509,7 +509,7 @@ static void pnv_psi_power8_realize(DeviceState *dev, Error **errp)
 
     qdev_init_gpio_in(dev, pnv_psi_power8_set_irq, ics->nr_irqs);
 
-    psi->qirqs = qemu_allocate_irqs(ics_set_irq, ics, ics->nr_irqs);
+    psi->qirqs = qemu_allocate_irqs_orphan(ics_set_irq, ics, ics->nr_irqs);
 
     /* XSCOM region for PSI registers */
     pnv_xscom_region_init(&psi->xscom_regs, OBJECT(dev), &pnv_psi_xscom_ops,
@@ -872,7 +872,7 @@ static void pnv_psi_power9_realize(DeviceState *dev, Error **errp)
         xive_source_irq_set_lsi(xsrc, i);
     }
 
-    psi->qirqs = qemu_allocate_irqs(xive_source_set_irq, xsrc, xsrc->nr_irqs);
+    psi->qirqs = qemu_allocate_irqs_orphan(xive_source_set_irq, xsrc, xsrc->nr_irqs);
 
     qdev_init_gpio_in(dev, pnv_psi_power9_set_irq, xsrc->nr_irqs);
 
hw/ppc/spapr_irq.c +1 -1
index b9e5f5d710..a05edb78bc 100644
@@ -339,7 +339,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
         spapr_xive_hcall_init(spapr);
     }
 
-    spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
+    spapr->qirqs = qemu_allocate_irqs_orphan(spapr_set_irq, spapr,
                                       SPAPR_NR_XIRQS + SPAPR_IRQ_NR_IPIS);
 
     /*
hw/riscv/sifive_u.c +1 -1
index 9d3f5c8f59..3df9fafb12 100644
@@ -484,7 +484,7 @@ static void sifive_u_machine_init(MachineState *machine)
 
     /* register gpio-restart */
     qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
-                          qemu_allocate_irq(sifive_u_machine_reset, NULL, 0));
+                          qemu_allocate_irq_orphan(sifive_u_machine_reset, NULL, 0));
 
     /* load/create device tree */
     if (machine->dtb) {
hw/scsi/esp-pci.c +1 -1
index b31ccdbecc..4c02b23a28 100644
@@ -407,7 +407,7 @@ static void esp_pci_scsi_realize(PCIDevice *dev, Error **errp)
                           "esp-io", 0x80);
 
     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &pci->io);
-    s->irq = qemu_allocate_irq(esp_irq_handler, pci, 0);
+    s->irq = qemu_allocate_irq_orphan(esp_irq_handler, pci, 0);
 
     scsi_bus_init(&s->bus, sizeof(s->bus), d, &esp_pci_scsi_info);
 }
hw/sh4/sh7750.c +1 -1
index ea55a3a9cc..307daa523d 100644
@@ -848,5 +848,5 @@ SH7750State *sh7750_init(Object *parent, SuperHCPU *cpu,
 qemu_irq sh7750_irl(SH7750State *s)
 {
     sh_intc_toggle_source(&s->intc.sources[IRL], 1, 0); /* enable */
-    return qemu_allocate_irq(sh_intc_set_irl, &s->intc.sources[IRL], 0);
+    return qemu_allocate_irq_orphan(sh_intc_set_irl, &s->intc.sources[IRL], 0);
 }
hw/sparc/sun4m.c +2 -2
index a0d0213d0b..0a35b20e17 100644
@@ -1008,7 +1008,7 @@ static void sun4m_hw_init(MachineState *machine)
 
     if (hwdef->apc_base) {
         apc_init(OBJECT(machine), hwdef->apc_base,
-                 qemu_allocate_irq(cpu_halt_signal, NULL, 0));
+                 qemu_allocate_irq_orphan(cpu_halt_signal, NULL, 0));
     }
 
     if (hwdef->fd_base) {
@@ -1018,7 +1018,7 @@ static void sun4m_hw_init(MachineState *machine)
         sun4m_fdctrl_init(OBJECT(machine), slavio_irq[22], hwdef->fd_base, fd,
                           &fdc_tc);
     } else {
-        fdc_tc = qemu_allocate_irq(dummy_fdc_tc, NULL, 0);
+        fdc_tc = qemu_allocate_irq_orphan(dummy_fdc_tc, NULL, 0);
     }
 
     slavio_misc_init(OBJECT(machine), hwdef->slavio_base, hwdef->aux1_base,
hw/sparc64/sun4u.c +1 -1
index dbf49ef43d..389436fd3d 100644
@@ -311,7 +311,7 @@ static void ebus_realize(PCIDevice *pci_dev, Error **errp)
     }
 
     /* ISA bus */
-    s->isa_irqs_in = qemu_allocate_irqs(ebus_isa_irq_handler, s, ISA_NUM_IRQS);
+    s->isa_irqs_in = qemu_allocate_irqs_orphan(ebus_isa_irq_handler, s, ISA_NUM_IRQS);
     isa_bus_register_input_irqs(s->isa_bus, s->isa_irqs_in);
     qdev_init_gpio_out_named(DEVICE(s), s->isa_irqs_out, "isa-irq",
                              ISA_NUM_IRQS);
hw/timer/arm_timer.c +2 -2
index fb70333c53..1c29db12a2 100644
@@ -305,8 +305,8 @@ static void sp804_realize(DeviceState *dev, Error **errp)
 
     s->timer[0] = arm_timer_init(s->freq0);
     s->timer[1] = arm_timer_init(s->freq1);
-    s->timer[0]->irq = qemu_allocate_irq(sp804_set_irq, s, 0);
-    s->timer[1]->irq = qemu_allocate_irq(sp804_set_irq, s, 1);
+    s->timer[0]->irq = qemu_allocate_irq_orphan(sp804_set_irq, s, 0);
+    s->timer[1]->irq = qemu_allocate_irq_orphan(sp804_set_irq, s, 1);
 }
 
 /* Integrator/CP timer module.  */
hw/usb/vt82c686-uhci-pci.c +1 -1
index 97c09a51cf..b58b61e609 100644
@@ -23,7 +23,7 @@ static void usb_uhci_vt82c686b_realize(PCIDevice *dev, Error **errp)
 
     usb_uhci_common_realize(dev, errp);
     object_unref(s->irq);
-    s->irq = qemu_allocate_irq(uhci_isa_set_irq, s, 0);
+    s->irq = qemu_allocate_irq_orphan(uhci_isa_set_irq, s, 0);
 }
 
 static UHCIInfo uhci_info[] = {
hw/xen/xen-pvh-common.c +2 -2
index 1047aabd57..42514a599e 100644
@@ -170,7 +170,7 @@ static void xen_create_virtio_mmio_devices(XenPVHMachineState *s)
      */
     for (i = s->cfg.virtio_mmio_num - 1; i >= 0; i--) {
         hwaddr base = s->cfg.virtio_mmio.base + i * s->cfg.virtio_mmio.size;
-        qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL,
+        qemu_irq irq = qemu_allocate_irq_orphan(xen_set_irq, NULL,
                                          s->cfg.virtio_mmio_irq_base + i);
 
         sysbus_create_simple(OBJECT(s), "virtio-mmio[*]", "virtio-mmio",
@@ -260,7 +260,7 @@ static inline void xenpvh_gpex_init(XenPVHMachineState *s,
     assert(xpc->set_pci_intx_irq);
 
     for (i = 0; i < PCI_NUM_PINS; i++) {
-        qemu_irq irq = qemu_allocate_irq(xpc->set_pci_intx_irq, s, i);
+        qemu_irq irq = qemu_allocate_irq_orphan(xpc->set_pci_intx_irq, s, i);
 
         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq);
         gpex_set_irq_num(GPEX_HOST(dev), i, s->cfg.pci_intx_irq_base + i);
hw/xtensa/mx_pic.c +1 -1
index f349fb125c..9959bec5f2 100644
@@ -346,7 +346,7 @@ XtensaMxPic *xtensa_mx_pic_init(unsigned n_irq)
     XtensaMxPic *mx = g_new0(XtensaMxPic, 1);
 
     mx->n_irq = n_irq + 1;
-    mx->irq_inputs = qemu_allocate_irqs(xtensa_mx_pic_set_irq, mx,
+    mx->irq_inputs = qemu_allocate_irqs_orphan(xtensa_mx_pic_set_irq, mx,
                                         mx->n_irq);
     return mx;
 }
hw/xtensa/pic_cpu.c +2 -2
index 370a353f1e..c3163a8b6c 100644
@@ -102,7 +102,7 @@ void xtensa_irq_init(CPUXtensaState *env)
 {
     unsigned i;
 
-    env->irq_inputs = qemu_allocate_irqs(xtensa_set_irq, env,
+    env->irq_inputs = qemu_allocate_irqs_orphan(xtensa_set_irq, env,
                                          env->config->ninterrupt);
     if (xtensa_option_enabled(env->config, XTENSA_OPTION_TIMER_INTERRUPT)) {
         env->time_base = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -118,7 +118,7 @@ void xtensa_irq_init(CPUXtensaState *env)
 
         env->ext_irq_inputs[i] = env->irq_inputs[irq];
     }
-    env->runstall_irq = qemu_allocate_irq(xtensa_set_runstall, env, 0);
+    env->runstall_irq = qemu_allocate_irq_orphan(xtensa_set_runstall, env, 0);
 }
 
 qemu_irq *xtensa_get_extints(CPUXtensaState *env)
include/hw/core/irq.h +4 -4
index 299a4a9a8c..f984c02c14 100644
@@ -77,25 +77,25 @@ void qemu_init_irqs(IRQState irq[], size_t count,
 /* Returns an array of N IRQs. Each IRQ is assigned the argument handler and
  * opaque data.
  */
-qemu_irq *qemu_allocate_irqs(qemu_irq_handler handler, void *opaque, int n);
+qemu_irq *qemu_allocate_irqs_orphan(qemu_irq_handler handler, void *opaque, int n);
 
 /*
  * Allocates a single IRQ. The irq is assigned with a handler, an opaque
  * data and the interrupt number.
  */
-qemu_irq qemu_allocate_irq(qemu_irq_handler handler, void *opaque, int n);
+qemu_irq qemu_allocate_irq_orphan(qemu_irq_handler handler, void *opaque, int n);
 
 /* Extends an Array of IRQs. Old IRQs have their handlers and opaque data
  * preserved. New IRQs are assigned the argument handler and opaque data.
  */
-qemu_irq *qemu_extend_irqs(qemu_irq *old, int n_old, qemu_irq_handler handler,
+qemu_irq *qemu_extend_irqs_orphan(qemu_irq *old, int n_old, qemu_irq_handler handler,
                                 void *opaque, int n);
 
 void qemu_free_irqs(qemu_irq *s, int n);
 void qemu_free_irq(qemu_irq irq);
 
 /* Returns a new IRQ with opposite polarity.  */
-qemu_irq qemu_irq_invert(qemu_irq irq);
+qemu_irq qemu_irq_invert_orphan(qemu_irq irq);
 
 /* For internal use in qtest. */
 void qemu_irq_set_observer(qemu_irq *gpio_in, qemu_irq_handler handler, int n);
scripts/coccinelle/qom-parent/irq-rename.cocci new file +15 -0
new file mode 100644
index 0000000000..02df66cb6c
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// Rename the IRQ allocation helpers to *_orphan() so the parented
+// variants can take the well-known names.
+@@ @@
+- qemu_allocate_irqs
++ qemu_allocate_irqs_orphan
+@@ @@
+- qemu_allocate_irq
++ qemu_allocate_irq_orphan
+@@ @@
+- qemu_extend_irqs
++ qemu_extend_irqs_orphan
+@@ @@
+- qemu_irq_invert
++ qemu_irq_invert_orphan
system/qtest.c +1 -1
index cf30123917..f38feaac89 100644
@@ -346,7 +346,7 @@ void qtest_set_command_cb(bool (*pc_cb)(CharFrontend *chr, gchar **words))
 static void qtest_install_gpio_out_intercept(DeviceState *dev, const char *name, int n)
 {
     qemu_irq *disconnected = g_new0(qemu_irq, 1);
-    qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
+    qemu_irq icpt = qemu_allocate_irq_orphan(qtest_irq_handler,
                                       disconnected, n);
 
     *disconnected = qdev_intercept_gpio_out(dev, icpt, name, n);
Signature · 1 line
2.47.1
#267 lore raw

RFC PATCH v2 110/137 irq: Reintroduce qemu_allocate_irq*() with a mandatory owner and name

In reply to Alexander Graf

Add parented variants of the IRQ allocation helpers under the well-known names, taking (Object *owner, const char *name, ...) as their first two arguments:

qemu_allocate_irq(owner, name, handler, opaque, n) qemu_allocate_irqs(owner, name, handler, opaque, n) qemu_extend_irqs(owner, name, old, n_old, handler, opaque, n) qemu_irq_invert(owner, name, irq)

The new IRQ object is created via object_new_child() so it lands under @owner with a stable path. The array/extend variants suffix "[*]" so callers pass a bare name and get name[0], name[1], ...

qemu_free_irq() is taught to unparent when the IRQ has a QOM parent, so it works for both the *_orphan() and the parented allocation paths during the transition.

The *_orphan() variants stay for now; later commits convert every caller and delete them, at which point the /machine/unattached fallback in qdev_connect_gpio_out_named() also goes away.

AI-used-for: code (refactoring)

---
 hw/core/irq.c         | 45 ++++++++++++++++++++++++++++++++++++++++++-
 include/hw/core/irq.h | 15 +++++++++++++++
 2 files changed, 59 insertions(+), 1 deletion(-)
hw/core/irq.c +44 -1
index f8fdeb7e02..1ae53a74d3 100644
@@ -96,6 +96,38 @@ qemu_irq qemu_allocate_irq_orphan(qemu_irq_handler handler, void *opaque, int n)
     return irq;
 }
 
+qemu_irq qemu_allocate_irq(Object *owner, const char *name,
+                           qemu_irq_handler handler, void *opaque, int n)
+{
+    IRQState *irq = IRQ(object_new_child(owner, name, TYPE_IRQ));
+    init_irq_fields(irq, handler, opaque, n);
+    return irq;
+}
+
+qemu_irq *qemu_extend_irqs(Object *owner, const char *name,
+                           qemu_irq *old, int n_old,
+                           qemu_irq_handler handler, void *opaque, int n)
+{
+    qemu_irq *s;
+    int i;
+
+    if (!old) {
+        n_old = 0;
+    }
+    s = old ? g_renew(qemu_irq, old, n + n_old) : g_new(qemu_irq, n);
+    for (i = n_old; i < n + n_old; i++) {
+        g_autofree char *propname = g_strdup_printf("%s[%d]", name, i);
+        s[i] = qemu_allocate_irq(owner, propname, handler, opaque, i);
+    }
+    return s;
+}
+
+qemu_irq *qemu_allocate_irqs(Object *owner, const char *name,
+                             qemu_irq_handler handler, void *opaque, int n)
+{
+    return qemu_extend_irqs(owner, name, NULL, 0, handler, opaque, n);
+}
+
 void qemu_free_irqs(qemu_irq *s, int n)
 {
     int i;
@@ -107,7 +139,11 @@ void qemu_free_irqs(qemu_irq *s, int n)
 
 void qemu_free_irq(qemu_irq irq)
 {
-    object_unref(OBJECT(irq));
+    if (irq && OBJECT(irq)->parent) {
+        object_unparent(OBJECT(irq));
+    } else {
+        object_unref(OBJECT(irq));
+    }
 }
 
 static void qemu_notirq(void *opaque, int line, int level)
@@ -124,6 +160,13 @@ qemu_irq qemu_irq_invert_orphan(qemu_irq irq)
     return qemu_allocate_irq_orphan(qemu_notirq, irq, 0);
 }
 
+qemu_irq qemu_irq_invert(Object *owner, const char *name, qemu_irq irq)
+{
+    /* The default state for IRQs is low, so raise the output now.  */
+    qemu_irq_raise(irq);
+    return qemu_allocate_irq(owner, name, qemu_notirq, irq, 0);
+}
+
 void qemu_irq_set_observer(qemu_irq *gpio_in, qemu_irq_handler handler, int n)
 {
     int i;
include/hw/core/irq.h +15 -0
index f984c02c14..5ccccd2697 100644
@@ -85,6 +85,20 @@ qemu_irq *qemu_allocate_irqs_orphan(qemu_irq_handler handler, void *opaque, int
  */
 qemu_irq qemu_allocate_irq_orphan(qemu_irq_handler handler, void *opaque, int n);
 
+/*
+ * Same as the *_orphan() variants above, but the new IRQ objects are
+ * parented under @owner with property name @name (or @name[*] for the
+ * array/extend variants).  The reference is held by @owner; call
+ * qemu_free_irq() to unparent and free.
+ */
+qemu_irq qemu_allocate_irq(Object *owner, const char *name,
+                           qemu_irq_handler handler, void *opaque, int n);
+qemu_irq *qemu_allocate_irqs(Object *owner, const char *name,
+                             qemu_irq_handler handler, void *opaque, int n);
+qemu_irq *qemu_extend_irqs(Object *owner, const char *name,
+                           qemu_irq *old, int n_old,
+                           qemu_irq_handler handler, void *opaque, int n);
+
 /* Extends an Array of IRQs. Old IRQs have their handlers and opaque data
  * preserved. New IRQs are assigned the argument handler and opaque data.
  */
@@ -96,6 +110,7 @@ void qemu_free_irq(qemu_irq irq);
 
 /* Returns a new IRQ with opposite polarity.  */
 qemu_irq qemu_irq_invert_orphan(qemu_irq irq);
+qemu_irq qemu_irq_invert(Object *owner, const char *name, qemu_irq irq);
 
 /* For internal use in qtest. */
 void qemu_irq_set_observer(qemu_irq *gpio_in, qemu_irq_handler handler, int n);
Signature · 1 line
2.47.1
#268 lore raw

RFC PATCH v2 111/137 hw/pci, hw/usb, system: Give allocated IRQs an owner and name

In reply to Alexander Graf

Convert pci_allocate_irq() and the qtest interceptor to the parented qemu_allocate_irq() form so the IRQ objects land under the device that owns their lifetime instead of /machine/unattached.

hw/usb/vt82c686-uhci-pci.c drops the IRQ set up by the base UHCI class before installing its own; switch that from object_unref() to qemu_free_irq() so it unparents the now-parented IRQ before freeing it.

  hw/pci/pci.c pci_allocate_irq()  | OBJECT(pci_dev) | "intx-irq"
  system/qtest.c qtest intercept   | OBJECT(dev)     | "qtest-icpt[*]"
  hw/usb/vt82c686-uhci-pci.c       | OBJECT(s)       | "isa-irq"

AI-used-for: code (refactoring)

---
 hw/pci/pci.c               | 2 +-
 hw/usb/vt82c686-uhci-pci.c | 4 ++--
 system/qtest.c             | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
hw/pci/pci.c +1 -1
index 9ba7bfd977..d92092b233 100644
@@ -1869,7 +1869,7 @@ qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
     int intx = pci_intx(pci_dev);
     assert(0 <= intx && intx < PCI_NUM_PINS);
 
-    return qemu_allocate_irq_orphan(pci_irq_handler, pci_dev, intx);
+    return qemu_allocate_irq(OBJECT(pci_dev), "intx-irq", pci_irq_handler, pci_dev, intx);
 }
 
 void pci_set_irq(PCIDevice *pci_dev, int level)
hw/usb/vt82c686-uhci-pci.c +2 -2
index b58b61e609..d8c9e98061 100644
@@ -22,8 +22,8 @@ static void usb_uhci_vt82c686b_realize(PCIDevice *dev, Error **errp)
     pci_set_long(pci_conf + 0xc0, 0x00002000);
 
     usb_uhci_common_realize(dev, errp);
-    object_unref(s->irq);
-    s->irq = qemu_allocate_irq_orphan(uhci_isa_set_irq, s, 0);
+    qemu_free_irq(s->irq);
+    s->irq = qemu_allocate_irq(OBJECT(s), "isa-irq", uhci_isa_set_irq, s, 0);
 }
 
 static UHCIInfo uhci_info[] = {
system/qtest.c +1 -1
index f38feaac89..5ef99f9f23 100644
@@ -346,7 +346,7 @@ void qtest_set_command_cb(bool (*pc_cb)(CharFrontend *chr, gchar **words))
 static void qtest_install_gpio_out_intercept(DeviceState *dev, const char *name, int n)
 {
     qemu_irq *disconnected = g_new0(qemu_irq, 1);
-    qemu_irq icpt = qemu_allocate_irq_orphan(qtest_irq_handler,
+    qemu_irq icpt = qemu_allocate_irq(OBJECT(dev), "qtest-icpt[*]", qtest_irq_handler,
                                       disconnected, n);
 
     *disconnected = qdev_intercept_gpio_out(dev, icpt, name, n);
Signature · 1 line
2.47.1
#269 lore raw

RFC PATCH v2 112/137 hw/i386, hw/xen: Give allocated IRQs an owner and name

In reply to Alexander Graf

Convert the IRQ allocation calls in hw/i386 and the Xen accelerator to the parented form. Thread an Object *parent through the small helpers that had none:

pc_gsi_create() -> +Object *parent
x86_allocate_cpu_irq() -> +Object *parent
xen_interrupt_controller_init() -> +Object *parent

Per-site owner/name:

  pc.c pc_gsi_create()          | parent          | "gsi"
  pc.c a20 line                 | parent          | "a20"
  pc_piix.c smi                 | OBJECT(machine) | "smi"
  x86-cpu.c CPU IRQ             | parent          | "pic-cpu-irq"
  microvm.c gsi                 | OBJECT(mms)     | "gsi"
  kvm/i8259.c                   | parent          | "kvm-pic-irq"
  xen-hvm.c                     | parent          | "xen-irq"

AI-used-for: code (refactoring)

---
 hw/i386/isapc.c       |  2 +-
 hw/i386/kvm/i8259.c   |  2 +-
 hw/i386/microvm.c     |  4 ++--
 hw/i386/pc.c          | 10 +++++-----
 hw/i386/pc_piix.c     |  5 +++--
 hw/i386/pc_q35.c      |  2 +-
 hw/i386/x86-cpu.c     |  4 ++--
 hw/i386/xen/xen-hvm.c |  4 ++--
 include/hw/i386/pc.h  |  2 +-
 include/hw/i386/x86.h |  2 +-
 include/hw/xen/xen.h  |  2 +-
 stubs/xen-hw-stub.c   |  2 +-
 12 files changed, 21 insertions(+), 20 deletions(-)
hw/i386/isapc.c +1 -1
index e8d5767b80..c9f2f14123 100644
@@ -106,7 +106,7 @@ static void pc_init_isa(MachineState *machine)
         }
     }
 
-    gsi_state = pc_gsi_create(&x86ms->gsi, false);
+    gsi_state = pc_gsi_create(OBJECT(machine), &x86ms->gsi, false);
 
     isa_bus = isa_bus_new_bridge(OBJECT(machine), system_memory, system_io,
                           &error_abort);
hw/i386/kvm/i8259.c +1 -1
index 7ef7a0b487..db2847686b 100644
@@ -136,7 +136,7 @@ qemu_irq *kvm_i8259_init(Object *parent, ISABus *bus)
     i8259_init_chip(parent, TYPE_KVM_I8259, bus, true);
     i8259_init_chip(parent, TYPE_KVM_I8259, bus, false);
 
-    return qemu_allocate_irqs_orphan(kvm_pic_set_irq, NULL, ISA_NUM_IRQS);
+    return qemu_allocate_irqs(parent, "kvm-pic-irq", kvm_pic_set_irq, NULL, ISA_NUM_IRQS);
 }
 
 static void kvm_i8259_class_init(ObjectClass *klass, const void *data)
hw/i386/microvm.c +2 -2
index 07dbe3a18b..4c1a4d5661 100644
@@ -168,7 +168,7 @@ static void microvm_devices_init(MicrovmMachineState *mms)
     /* Core components */
     ioapics = microvm_ioapics(mms);
     gsi_state = g_malloc0(sizeof(*gsi_state));
-    x86ms->gsi = qemu_allocate_irqs_orphan(gsi_handler, gsi_state,
+    x86ms->gsi = qemu_allocate_irqs(OBJECT(mms), "gsi", gsi_handler, gsi_state,
                                     IOAPIC_NUM_PINS * ioapics);
 
     isa_bus = isa_bus_new_bridge(OBJECT(mms), get_system_memory(), get_system_io(),
@@ -252,7 +252,7 @@ static void microvm_devices_init(MicrovmMachineState *mms)
     if (x86ms->pic == ON_OFF_AUTO_ON || x86ms->pic == ON_OFF_AUTO_AUTO) {
         qemu_irq *i8259;
 
-        i8259 = i8259_init(OBJECT(mms), isa_bus, x86_allocate_cpu_irq());
+        i8259 = i8259_init(OBJECT(mms), isa_bus, x86_allocate_cpu_irq(OBJECT(mms)));
         for (i = 0; i < ISA_NUM_IRQS; i++) {
             gsi_state->i8259_irq[i] = i8259[i];
         }
hw/i386/pc.c +5 -5
index 249fa699c7..398371ff6f 100644
@@ -192,7 +192,7 @@ const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
  */
 #define PC_FW_DATA (0x20000 + 0x8000)
 
-GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled)
+GSIState *pc_gsi_create(Object *parent, qemu_irq **irqs, bool pci_enabled)
 {
     GSIState *s;
 
@@ -200,7 +200,7 @@ GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled)
     if (kvm_ioapic_in_kernel()) {
         kvm_pc_setup_irq_routing(pci_enabled);
     }
-    *irqs = qemu_allocate_irqs_orphan(gsi_handler, s, IOAPIC_NUM_PINS);
+    *irqs = qemu_allocate_irqs(parent, "gsi", gsi_handler, s, IOAPIC_NUM_PINS);
 
     return s;
 }
@@ -1028,7 +1028,7 @@ static void pc_superio_init(Object *parent, ISABus *isa_bus, bool create_fdctrl,
     }
     port92 = isa_create_simple(parent, "port92", isa_bus, TYPE_PORT92);
 
-    a20_line = qemu_allocate_irqs_orphan(handle_a20_line_change, first_cpu, 2);
+    a20_line = qemu_allocate_irqs(parent, "a20", handle_a20_line_change, first_cpu, 2);
     qdev_connect_gpio_out_named(DEVICE(i8042),
                                 I8042_A20_LINE, 0, a20_line[0]);
     qdev_connect_gpio_out_named(DEVICE(port92),
@@ -1167,9 +1167,9 @@ void pc_i8259_create(Object *parent, ISABus *isa_bus, qemu_irq *i8259_irqs)
     if (kvm_pic_in_kernel()) {
         i8259 = kvm_i8259_init(parent, isa_bus);
     } else if (xen_enabled()) {
-        i8259 = xen_interrupt_controller_init();
+        i8259 = xen_interrupt_controller_init(parent);
     } else {
-        i8259 = i8259_init(parent, isa_bus, x86_allocate_cpu_irq());
+        i8259 = i8259_init(parent, isa_bus, x86_allocate_cpu_irq(parent));
     }
 
     for (size_t i = 0; i < ISA_NUM_IRQS; i++) {
hw/i386/pc_piix.c +3 -2
index f76d684d6b..976c646822 100644
@@ -233,7 +233,7 @@ static void pc_init1(MachineState *machine, const char *pci_type)
         }
     }
 
-    gsi_state = pc_gsi_create(&x86ms->gsi, true);
+    gsi_state = pc_gsi_create(OBJECT(machine), &x86ms->gsi, true);
 
     pci_dev = pci_new_multifunction(OBJECT(machine), "south-bridge", -1,
                                     pcms->south_bridge);
@@ -299,7 +299,8 @@ static void pc_init1(MachineState *machine, const char *pci_type)
     pc_nic_init(pcmc, isa_bus, pcms->pcibus);
 
     if (piix4_pm) {
-        smi_irq = qemu_allocate_irq_orphan(pc_acpi_smi_interrupt, first_cpu, 0);
+        smi_irq = qemu_allocate_irq(OBJECT(machine), "smi",
+                                    pc_acpi_smi_interrupt, first_cpu, 0);
 
         qdev_connect_gpio_out_named(DEVICE(piix4_pm), "smi-irq", 0, smi_irq);
         pcms->smbus = I2C_BUS(qdev_get_child_bus(DEVICE(piix4_pm), "i2c"));
hw/i386/pc_q35.c +1 -1
index b2cdca8ade..b6a79d2e13 100644
@@ -233,7 +233,7 @@ static void pc_q35_init(MachineState *machine)
     pcms->pcibus = PCI_BUS(qdev_get_child_bus(DEVICE(phb), "pcie.0"));
 
     /* irq lines */
-    gsi_state = pc_gsi_create(&x86ms->gsi, true);
+    gsi_state = pc_gsi_create(OBJECT(machine), &x86ms->gsi, true);
 
     /* create ISA bus */
     lpc = pci_new_multifunction(OBJECT(machine), "lpc",
hw/i386/x86-cpu.c +2 -2
index 6d010291cf..f92acf2778 100644
@@ -61,9 +61,9 @@ static void pic_irq_request(void *opaque, int irq, int level)
     }
 }
 
-qemu_irq x86_allocate_cpu_irq(void)
+qemu_irq x86_allocate_cpu_irq(Object *parent)
 {
-    return qemu_allocate_irq_orphan(pic_irq_request, NULL, 0);
+    return qemu_allocate_irq(parent, "pic-cpu-irq", pic_irq_request, NULL, 0);
 }
 
 int cpu_get_pic_interrupt(CPUX86State *env)
hw/i386/xen/xen-hvm.c +2 -2
index e87c8237ea..e19c9723da 100644
@@ -112,9 +112,9 @@ static void xen_set_irq(void *opaque, int irq, int level)
     xen_set_isa_irq_level(xen_domid, irq, level);
 }
 
-qemu_irq *xen_interrupt_controller_init(void)
+qemu_irq *xen_interrupt_controller_init(Object *parent)
 {
-    return qemu_allocate_irqs_orphan(xen_set_irq, NULL, 16);
+    return qemu_allocate_irqs(parent, "xen-irq", xen_set_irq, NULL, 16);
 }
 
 /* Memory Ops */
include/hw/i386/pc.h +1 -1
index a059409243..15e4cca288 100644
@@ -126,7 +126,7 @@ OBJECT_DECLARE_TYPE(PCMachineState, PCMachineClass, PC_MACHINE)
 
 /* ioapic.c */
 
-GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled);
+GSIState *pc_gsi_create(Object *parent, qemu_irq **irqs, bool pci_enabled);
 
 /* pc.c */
 
include/hw/i386/x86.h +1 -1
index 9d5d7694d4..a21b5d1384 100644
@@ -145,7 +145,7 @@ typedef struct GSIState {
     qemu_irq ioapic2_irq[IOAPIC_NUM_PINS];
 } GSIState;
 
-qemu_irq x86_allocate_cpu_irq(void);
+qemu_irq x86_allocate_cpu_irq(Object *parent);
 void gsi_handler(void *opaque, int n, int level);
 void ioapic_init_gsi(GSIState *gsi_state, Object *parent);
 DeviceState *ioapic_init_secondary(Object *parent, GSIState *gsi_state);
include/hw/xen/xen.h +1 -1
index e94c6e5a31..83b7f6c3aa 100644
@@ -42,7 +42,7 @@ void xen_intx_set_irq(void *opaque, int irq_num, int level);
 void xen_hvm_inject_msi(uint64_t addr, uint32_t data);
 int xen_is_pirq_msi(uint32_t msi_data);
 
-qemu_irq *xen_interrupt_controller_init(void);
+qemu_irq *xen_interrupt_controller_init(Object *parent);
 
 void xen_register_framebuffer(struct MemoryRegion *mr);
 
stubs/xen-hw-stub.c +1 -1
index 6cf0e9a4c1..4ea666cac8 100644
@@ -29,7 +29,7 @@ int xen_is_pirq_msi(uint32_t msi_data)
     return 0;
 }
 
-qemu_irq *xen_interrupt_controller_init(void)
+qemu_irq *xen_interrupt_controller_init(Object *parent)
 {
     return NULL;
 }
Signature · 1 line
2.47.1
#270 lore raw

RFC PATCH v2 113/137 hw/arm: Give allocated IRQs an owner and name

In reply to Alexander Graf

Convert the IRQ allocation calls in hw/arm boards and the OMAP peripheral models to the parented form. Thread an Object *owner through the small helpers that had none:

omap_timer_clk_setup() -> +Object *owner omap_mcbsp_i2s_attach() -> +Object *owner (no callers, kept for API)

The OMAP init helpers are called multiple times from omap310_mpu_init() so their IRQ names use the auto-index [*] form.

AI-used-for: code (refactoring)

---
 hw/arm/armsse.c         |  5 +++--
 hw/arm/exynos4_boards.c |  3 ++-
 hw/arm/mps2-tz.c        |  3 ++-
 hw/arm/omap1.c          | 29 +++++++++++++++++------------
 hw/arm/realview.c       |  3 ++-
 hw/arm/stellaris.c      | 15 ++++++++++-----
 hw/dma/omap_dma.c       |  5 +++--
 hw/timer/arm_timer.c    |  6 ++++--
 include/hw/arm/omap.h   |  2 +-
 9 files changed, 44 insertions(+), 27 deletions(-)
hw/arm/armsse.c +3 -2
index 93f6715224..b25f058020 100644
@@ -693,7 +693,8 @@ static void armsse_forward_sec_resp_cfg(ARMSSE *s)
     DeviceState *dev_splitter = DEVICE(&s->sec_resp_splitter);
 
     qdev_init_gpio_out_named(dev, &s->sec_resp_cfg, "sec_resp_cfg", 1);
-    s->sec_resp_cfg_in = qemu_allocate_irq_orphan(irq_status_forwarder,
+    s->sec_resp_cfg_in = qemu_allocate_irq(OBJECT(s), "sec-resp-cfg-in",
+                                           irq_status_forwarder,
                                            s->sec_resp_cfg, 1);
     qdev_connect_gpio_out(dev_splitter, 2, s->sec_resp_cfg_in);
 }
@@ -1137,7 +1138,7 @@ static void armsse_realize(DeviceState *dev, Error **errp)
     sysbus_mmio_map(sbd_secctl, 0, 0x50080000);
     sysbus_mmio_map(sbd_secctl, 1, 0x40080000);
 
-    s->nsc_cfg_in = qemu_allocate_irq_orphan(nsccfg_handler, s, 1);
+    s->nsc_cfg_in = qemu_allocate_irq(OBJECT(dev), "nsc-cfg-in", nsccfg_handler, s, 1);
     qdev_connect_gpio_out_named(dev_secctl, "nsc_cfg", 0, s->nsc_cfg_in);
 
     /* The sec_resp_cfg output from the security controller must be split into
hw/arm/exynos4_boards.c +2 -1
index 537a50f8cc..4f5c99e301 100644
@@ -146,7 +146,8 @@ static void smdkc210_init(MachineState *machine)
                                                       EXYNOS4_BOARD_SMDKC210);
 
     lan9215_init(SMDK_LAN9118_BASE_ADDR,
-            qemu_irq_invert_orphan(s->soc.irq_table[exynos4210_get_irq(37, 1)]));
+            qemu_irq_invert(OBJECT(machine), "lan9118-irq-inv",
+                            s->soc.irq_table[exynos4210_get_irq(37, 1)]));
     arm_load_kernel(s->soc.cpu[0], machine, &exynos4_board_binfo);
 }
 
hw/arm/mps2-tz.c +2 -1
index 654eaedd97..ea94f1a6dd 100644
@@ -1209,7 +1209,8 @@ static void mps2tz_common_init(MachineState *machine)
          * Connect the line from the SCC so that we can remap when the
          * guest updates that register.
          */
-        mms->remap_irq = qemu_allocate_irq_orphan(remap_irq_fn, mms, 0);
+        mms->remap_irq = qemu_allocate_irq(OBJECT(machine), "remap-irq",
+                                           remap_irq_fn, mms, 0);
         qdev_connect_gpio_out_named(DEVICE(&mms->scc), "remap", 0,
                                     mms->remap_irq);
     }
hw/arm/omap1.c +17 -12
index 53e52ef34e..c193b41f5b 100644
@@ -136,10 +136,11 @@ static void omap_timer_clk_update(void *opaque, int line, int on)
     omap_timer_update(timer);
 }
 
-static void omap_timer_clk_setup(struct omap_mpu_timer_s *timer)
+static void omap_timer_clk_setup(Object *owner, struct omap_mpu_timer_s *timer)
 {
     omap_clk_adduser(timer->clk,
-                    qemu_allocate_irq_orphan(omap_timer_clk_update, timer, 0));
+                    qemu_allocate_irq(owner, "rate-irq[*]",
+                                      omap_timer_clk_update, timer, 0));
     timer->rate = omap_clk_getrate(timer->clk);
 }
 
@@ -232,7 +233,7 @@ static struct omap_mpu_timer_s *omap_mpu_timer_init(Object *owner,
     s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, omap_timer_tick, s);
     s->tick = qemu_bh_new(omap_timer_fire, s);
     omap_mpu_timer_reset(s);
-    omap_timer_clk_setup(s);
+    omap_timer_clk_setup(owner, s);
 
     memory_region_init_io(&s->iomem, owner, &omap_mpu_timer_ops, s,
                           "omap-mpu-timer", 0x100);
@@ -363,7 +364,7 @@ static struct omap_watchdog_timer_s *omap_wd_timer_init(Object *owner,
     s->timer.clk = clk;
     s->timer.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, omap_timer_tick, &s->timer);
     omap_wd_timer_reset(s);
-    omap_timer_clk_setup(&s->timer);
+    omap_timer_clk_setup(owner, &s->timer);
 
     memory_region_init_io(&s->iomem, owner, &omap_wd_timer_ops, s,
                           "omap-wd-timer", 0x100);
@@ -472,7 +473,7 @@ static struct omap_32khz_timer_s *omap_os_timer_init(Object *owner,
     s->timer.clk = clk;
     s->timer.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, omap_timer_tick, &s->timer);
     omap_os_timer_reset(s);
-    omap_timer_clk_setup(&s->timer);
+    omap_timer_clk_setup(owner, &s->timer);
 
     memory_region_init_io(&s->iomem, owner, &omap_os_timer_ops, s,
                           "omap-os-timer", 0x800);
@@ -2083,14 +2084,15 @@ static struct omap_mpuio_s *omap_mpuio_init(Object *owner,
 
     s->irq = gpio_int;
     s->kbd_irq = kbd_int;
-    s->in = qemu_allocate_irqs_orphan(omap_mpuio_set, s, 16);
+    s->in = qemu_allocate_irqs(owner, "mpuio-in", omap_mpuio_set, s, 16);
     omap_mpuio_reset(s);
 
     memory_region_init_io(&s->iomem, owner, &omap_mpuio_ops, s,
                           "omap-mpuio", 0x800);
     memory_region_add_subregion(memory, base, &s->iomem);
 
-    omap_clk_adduser(clk, qemu_allocate_irq_orphan(omap_mpuio_onoff, s, 0));
+    omap_clk_adduser(clk, qemu_allocate_irq(owner, "mpuio-clk-onoff[*]",
+                                            omap_mpuio_onoff, s, 0));
 
     return s;
 }
@@ -2363,7 +2365,8 @@ static struct omap_pwl_s *omap_pwl_init(Object *owner,
                           "omap-pwl", 0x800);
     memory_region_add_subregion(system_memory, base, &s->iomem);
 
-    omap_clk_adduser(clk, qemu_allocate_irq_orphan(omap_pwl_clk_update, s, 0));
+    omap_clk_adduser(clk, qemu_allocate_irq(owner, "pwl-clk-irq[*]",
+                                            omap_pwl_clk_update, s, 0));
     return s;
 }
 
@@ -3423,11 +3426,12 @@ static void omap_mcbsp_i2s_start(void *opaque, int line, int level)
     }
 }
 
-void omap_mcbsp_i2s_attach(struct omap_mcbsp_s *s, I2SCodec *slave)
+void omap_mcbsp_i2s_attach(Object *owner, struct omap_mcbsp_s *s, I2SCodec *slave)
 {
     s->codec = slave;
-    slave->rx_swallow = qemu_allocate_irq_orphan(omap_mcbsp_i2s_swallow, s, 0);
-    slave->tx_start = qemu_allocate_irq_orphan(omap_mcbsp_i2s_start, s, 0);
+    slave->rx_swallow = qemu_allocate_irq(owner, "rx-swallow[*]",
+                                          omap_mcbsp_i2s_swallow, s, 0);
+    slave->tx_start = qemu_allocate_irq(owner, "tx-start[*]", omap_mcbsp_i2s_start, s, 0);
 }
 
 /* LED Pulse Generators */
@@ -3579,7 +3583,8 @@ static struct omap_lpg_s *omap_lpg_init(Object *owner,
                           "omap-lpg", 0x800);
     memory_region_add_subregion(system_memory, base, &s->iomem);
 
-    omap_clk_adduser(clk, qemu_allocate_irq_orphan(omap_lpg_clk_update, s, 0));
+    omap_clk_adduser(clk, qemu_allocate_irq(owner, "lpg-clk-irq[*]",
+                                            omap_lpg_clk_update, s, 0));
 
     return s;
 }
hw/arm/realview.c +2 -1
index cd63f39f42..99591e7d80 100644
@@ -276,7 +276,8 @@ static void realview_init(MachineState *machine,
 
     split_irq_from_named(OBJECT(machine), dev, "card-inserted",
                    qdev_get_gpio_in(sysctl, ARM_SYSCTL_GPIO_MMC_CARDIN),
-                   qemu_irq_invert_orphan(qdev_get_gpio_in(gpio2, 0)));
+                   qemu_irq_invert(OBJECT(machine), "mmc-cd-inv",
+                                   qdev_get_gpio_in(gpio2, 0)));
 
     dinfo = drive_get(IF_SD, 0, 0);
     if (dinfo) {
hw/arm/stellaris.c +10 -5
index a5d037094a..a0986dbfe8 100644
@@ -1358,15 +1358,20 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
         sysbus_realize(SYS_BUS_DEVICE(gpad), &error_fatal);
 
         qdev_connect_gpio_out(gpad, 0,
-                              qemu_irq_invert_orphan(gpio_in[GPIO_E][0])); /* up */
+                              qemu_irq_invert(OBJECT(ms), "gpio-in-irq[*]",
+                                              gpio_in[GPIO_E][0])); /* up */
         qdev_connect_gpio_out(gpad, 1,
-                              qemu_irq_invert_orphan(gpio_in[GPIO_E][1])); /* down */
+                              qemu_irq_invert(OBJECT(ms), "gpio-in-irq[*]",
+                                              gpio_in[GPIO_E][1])); /* down */
         qdev_connect_gpio_out(gpad, 2,
-                              qemu_irq_invert_orphan(gpio_in[GPIO_E][2])); /* left */
+                              qemu_irq_invert(OBJECT(ms), "gpio-in-irq[*]",
+                                              gpio_in[GPIO_E][2])); /* left */
         qdev_connect_gpio_out(gpad, 3,
-                              qemu_irq_invert_orphan(gpio_in[GPIO_E][3])); /* right */
+                              qemu_irq_invert(OBJECT(ms), "gpio-in-irq[*]",
+                                              gpio_in[GPIO_E][3])); /* right */
         qdev_connect_gpio_out(gpad, 4,
-                              qemu_irq_invert_orphan(gpio_in[GPIO_F][1])); /* select */
+                              qemu_irq_invert(OBJECT(ms), "gpio-in-irq[*]",
+                                              gpio_in[GPIO_F][1])); /* select */
     }
     for (i = 0; i < 7; i++) {
         if (board->dc4 & (1 << i)) {
hw/dma/omap_dma.c +3 -2
index 7a2a3310f9..7efb7a8b9a 100644
@@ -1109,7 +1109,7 @@ struct soc_dma_s *omap_dma_init(Object *owner, hwaddr base, qemu_irq *irqs,
     s->dma->freq = omap_clk_getrate(clk);
     s->dma->transfer_fn = omap_dma_transfer_generic;
     s->dma->setup_fn = omap_dma_transfer_setup;
-    s->dma->drq = qemu_allocate_irqs_orphan(omap_dma_request, s, 32);
+    s->dma->drq = qemu_allocate_irqs(owner, "drq", omap_dma_request, s, 32);
     s->dma->opaque = s;
 
     while (num_irqs --)
@@ -1123,7 +1123,8 @@ struct soc_dma_s *omap_dma_init(Object *owner, hwaddr base, qemu_irq *irqs,
         s->dma->ch[i].opaque = &s->ch[i];
     }
 
-    omap_clk_adduser(s->clk, qemu_allocate_irq_orphan(omap_dma_clk_update, s, 0));
+    omap_clk_adduser(s->clk, qemu_allocate_irq(owner, "clk-irq",
+                                               omap_dma_clk_update, s, 0));
     omap_dma_reset(s->dma);
     omap_dma_clk_update(s, 0, 1);
 
hw/timer/arm_timer.c +4 -2
index 1c29db12a2..42ccc99b1e 100644
@@ -305,8 +305,10 @@ static void sp804_realize(DeviceState *dev, Error **errp)
 
     s->timer[0] = arm_timer_init(s->freq0);
     s->timer[1] = arm_timer_init(s->freq1);
-    s->timer[0]->irq = qemu_allocate_irq_orphan(sp804_set_irq, s, 0);
-    s->timer[1]->irq = qemu_allocate_irq_orphan(sp804_set_irq, s, 1);
+    s->timer[0]->irq = qemu_allocate_irq(OBJECT(dev), "timer-irq[*]",
+                                         sp804_set_irq, s, 0);
+    s->timer[1]->irq = qemu_allocate_irq(OBJECT(dev), "timer-irq[*]",
+                                         sp804_set_irq, s, 1);
 }
 
 /* Integrator/CP timer module.  */
include/hw/arm/omap.h +1 -1
index 591abf752b..a4267ef30a 100644
@@ -291,7 +291,7 @@ struct I2SCodec {
     } in, out;
 };
 struct omap_mcbsp_s;
-void omap_mcbsp_i2s_attach(struct omap_mcbsp_s *s, I2SCodec *slave);
+void omap_mcbsp_i2s_attach(Object *owner, struct omap_mcbsp_s *s, I2SCodec *slave);
 
 /* omap_lcdc.c */
 struct omap_lcd_panel_s;
Signature · 1 line
2.47.1
#271 lore raw

RFC PATCH v2 114/137 hw/xtensa: Give allocated IRQs an owner and name

In reply to Alexander Graf

Thread an Object *owner through xtensa_mx_pic_init() and use the CPU object as owner for the per-CPU IRQ input arrays.

AI-used-for: code (refactoring)

---
 hw/xtensa/mx_pic.c         | 4 ++--
 hw/xtensa/pic_cpu.c        | 5 +++--
 hw/xtensa/xtfpga.c         | 2 +-
 include/hw/xtensa/mx_pic.h | 2 +-
 4 files changed, 7 insertions(+), 6 deletions(-)
hw/xtensa/mx_pic.c +2 -2
index 9959bec5f2..f698d592f0 100644
@@ -341,12 +341,12 @@ static void xtensa_mx_pic_set_irq(void *opaque, int irq, int active)
     }
 }
 
-XtensaMxPic *xtensa_mx_pic_init(unsigned n_irq)
+XtensaMxPic *xtensa_mx_pic_init(Object *owner, unsigned n_irq)
 {
     XtensaMxPic *mx = g_new0(XtensaMxPic, 1);
 
     mx->n_irq = n_irq + 1;
-    mx->irq_inputs = qemu_allocate_irqs_orphan(xtensa_mx_pic_set_irq, mx,
+    mx->irq_inputs = qemu_allocate_irqs(owner, "irq", xtensa_mx_pic_set_irq, mx,
                                         mx->n_irq);
     return mx;
 }
hw/xtensa/pic_cpu.c +3 -2
index c3163a8b6c..496c83a298 100644
@@ -100,9 +100,10 @@ static void xtensa_set_runstall(void *opaque, int irq, int active)
 
 void xtensa_irq_init(CPUXtensaState *env)
 {
+    Object *cpu = OBJECT(env_cpu(env));
     unsigned i;
 
-    env->irq_inputs = qemu_allocate_irqs_orphan(xtensa_set_irq, env,
+    env->irq_inputs = qemu_allocate_irqs(cpu, "irq-in", xtensa_set_irq, env,
                                          env->config->ninterrupt);
     if (xtensa_option_enabled(env->config, XTENSA_OPTION_TIMER_INTERRUPT)) {
         env->time_base = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
@@ -118,7 +119,7 @@ void xtensa_irq_init(CPUXtensaState *env)
 
         env->ext_irq_inputs[i] = env->irq_inputs[irq];
     }
-    env->runstall_irq = qemu_allocate_irq_orphan(xtensa_set_runstall, env, 0);
+    env->runstall_irq = qemu_allocate_irq(cpu, "runstall", xtensa_set_runstall, env, 0);
 }
 
 qemu_irq *xtensa_get_extints(CPUXtensaState *env)
hw/xtensa/xtfpga.c +1 -1
index f0c21180a2..6ec0822a96 100644
@@ -247,7 +247,7 @@ static void xtfpga_init(const XtfpgaBoardDesc *board, MachineState *machine)
     Object *mo = OBJECT(machine);
 
     if (smp_cpus > 1) {
-        mx_pic = xtensa_mx_pic_init(31);
+        mx_pic = xtensa_mx_pic_init(mo, 31);
         qemu_register_reset(xtensa_mx_pic_reset, mx_pic);
     }
     for (n = 0; n < smp_cpus; n++) {
include/hw/xtensa/mx_pic.h +1 -1
index 01d5ea884e..212cf91e78 100644
@@ -33,7 +33,7 @@
 struct XtensaMxPic;
 typedef struct XtensaMxPic XtensaMxPic;
 
-XtensaMxPic *xtensa_mx_pic_init(unsigned n_extint);
+XtensaMxPic *xtensa_mx_pic_init(Object *owner, unsigned n_extint);
 void xtensa_mx_pic_reset(void *opaque);
 MemoryRegion *xtensa_mx_pic_register_cpu(Object *owner, XtensaMxPic *mx,
                                          qemu_irq *irq,
Signature · 1 line
2.47.1
#272 lore raw

RFC PATCH v2 115/137 hw: Give allocated IRQs an owner and name

In reply to Alexander Graf

Convert the remaining IRQ allocation calls across hw/ to the parented form. Owner is the enclosing device for realize()-time allocations and the machine for board-init allocations. Names describe the pin's purpose; the auto-indexed [*] form is used where the same allocation runs in a loop.

AI-used-for: code (refactoring)

---
 hw/alpha/typhoon.c          | 4 ++--
 hw/char/diva-gsp.c          | 3 ++-
 hw/display/macfb.c          | 2 +-
 hw/ide/ahci.c               | 2 +-
 hw/ide/pci.c                | 2 +-
 hw/intc/sh_intc.c           | 2 +-
 hw/isa/i82378.c             | 3 ++-
 hw/isa/lpc_ich9.c           | 2 +-
 hw/m68k/mcf5206.c           | 2 +-
 hw/m68k/q800.c              | 4 ++--
 hw/mips/mips_int.c          | 2 +-
 hw/net/can/can_kvaser_pci.c | 2 +-
 hw/pci-host/pnv_phb3.c      | 3 ++-
 hw/pci-host/pnv_phb3_msi.c  | 3 ++-
 hw/pci-host/pnv_phb4.c      | 3 ++-
 hw/ppc/e500.c               | 3 ++-
 hw/ppc/pnv_lpc.c            | 2 +-
 hw/ppc/pnv_psi.c            | 6 ++++--
 hw/ppc/spapr_irq.c          | 2 +-
 hw/riscv/sifive_u.c         | 3 ++-
 hw/scsi/esp-pci.c           | 2 +-
 hw/sh4/sh7750.c             | 2 +-
 hw/sparc/sun4m.c            | 5 +++--
 hw/sparc64/sun4u.c          | 3 ++-
 hw/xen/xen-pvh-common.c     | 5 +++--
 25 files changed, 42 insertions(+), 30 deletions(-)
hw/alpha/typhoon.c +2 -2
index 671e8f89d9..bc2073bfa9 100644
@@ -849,8 +849,8 @@ PCIBus *typhoon_init(Object *parent, MemoryRegion *ram, qemu_irq *p_isa_irq,
         }
     }
 
-    *p_isa_irq = qemu_allocate_irq_orphan(typhoon_set_isa_irq, s, 0);
-    *p_rtc_irq = qemu_allocate_irq_orphan(typhoon_set_timer_irq, s, 0);
+    *p_isa_irq = qemu_allocate_irq(parent, "isa-irq", typhoon_set_isa_irq, s, 0);
+    *p_rtc_irq = qemu_allocate_irq(parent, "rtc-irq", typhoon_set_timer_irq, s, 0);
 
     /* Main memory region, 0x00.0000.0000.  Real hardware supports 32GB,
        but the address space hole reserved at this point is 8TB.  */
hw/char/diva-gsp.c +2 -1
index bf1da1faee..b70811f24f 100644
@@ -126,7 +126,8 @@ static void diva_pci_realize(PCIDevice *dev, Error **errp)
     pci->dev.config[PCI_INTERRUPT_PIN] = 1;
     memory_region_init(&pci->membar, OBJECT(pci), "serial_ports", 4096);
     pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &pci->membar);
-    pci->irqs = qemu_allocate_irqs_orphan(multi_serial_irq_mux, pci, di.nports);
+    pci->irqs = qemu_allocate_irqs(OBJECT(dev), "serial-irq",
+                                   multi_serial_irq_mux, pci, di.nports);
 
     for (i = 0; i < di.nports; i++) {
         s = pci->state + i;
hw/display/macfb.c +1 -1
index de5c83884e..b59523a0ac 100644
@@ -737,7 +737,7 @@ static void macfb_nubus_realize(DeviceState *dev, Error **errp)
     memory_region_add_subregion(&nd->slot_mem, DAFB_BASE, &ms->mem_ctrl);
     memory_region_add_subregion(&nd->slot_mem, VIDEO_BASE, &ms->mem_vram);
 
-    ms->irq = qemu_allocate_irq_orphan(macfb_nubus_set_irq, s, 0);
+    ms->irq = qemu_allocate_irq(OBJECT(dev), "nubus-irq", macfb_nubus_set_irq, s, 0);
 }
 
 static void macfb_nubus_unrealize(DeviceState *dev)
hw/ide/ahci.c +1 -1
index ac1cf26bab..b8f4e018b9 100644
@@ -1594,7 +1594,7 @@ void ahci_realize(AHCIState *s, DeviceState *qdev, AddressSpace *as)
     assert(s->ports > 0);
     s->dev = g_new0(AHCIDevice, s->ports);
     ahci_reg_init(s);
-    irqs = qemu_allocate_irqs_orphan(ahci_irq_set, s, s->ports);
+    irqs = qemu_allocate_irqs(OBJECT(qdev), "port-irq", ahci_irq_set, s, s->ports);
     for (i = 0; i < s->ports; i++) {
         AHCIDevice *ad = &s->dev[i];
 
hw/ide/pci.c +1 -1
index c808469008..56750b7b6f 100644
@@ -606,7 +606,7 @@ void bmdma_init(IDEBus *bus, BMDMAState *bm, PCIIDEState *d)
     bm->dma.ops = &bmdma_ops;
     bus->dma = &bm->dma;
     bm->irq = bus->irq;
-    bus->irq = qemu_allocate_irq_orphan(bmdma_irq, bm, 0);
+    bus->irq = qemu_allocate_irq(OBJECT(d), "bmdma-irq[*]", bmdma_irq, bm, 0);
     bm->bus = bus;
     bm->pci_dev = d;
 }
hw/intc/sh_intc.c +1 -1
index 63f4eee2aa..4eaaeefeea 100644
@@ -403,7 +403,7 @@ int sh_intc_init(Object *owner, MemoryRegion *sysmem,
     for (i = 0; i < nr_sources; i++) {
         desc->sources[i].parent = desc;
     }
-    desc->irqs = qemu_allocate_irqs_orphan(sh_intc_set_irq, desc, nr_sources);
+    desc->irqs = qemu_allocate_irqs(owner, "src-irq", sh_intc_set_irq, desc, nr_sources);
     memory_region_init_io(&desc->iomem, owner, &sh_intc_ops, desc, "intc",
                           0x100000000ULL);
     j = 0;
hw/isa/i82378.c +2 -1
index 4f0be992e0..263a0c0dcf 100644
@@ -95,7 +95,8 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
 
     /* 2 82C59 (irq) */
     s->isa_irqs_in = i8259_init(OBJECT(pci), isabus,
-                                qemu_allocate_irq_orphan(i82378_request_out0_irq,
+                                qemu_allocate_irq(OBJECT(dev), "out0-irq",
+                                                  i82378_request_out0_irq,
                                                   s, 0));
     isa_bus_register_input_irqs(isabus, s->isa_irqs_in);
 
hw/isa/lpc_ich9.c +1 -1
index 7cb82519fb..7b44423372 100644
@@ -429,7 +429,7 @@ static void ich9_lpc_pm_init(ICH9LPCState *lpc)
     qemu_irq sci_irq;
     FWCfgState *fw_cfg = fw_cfg_find();
 
-    sci_irq = qemu_allocate_irq_orphan(ich9_set_sci, lpc, 0);
+    sci_irq = qemu_allocate_irq(OBJECT(lpc), "sci-irq", ich9_set_sci, lpc, 0);
     ich9_pm_init(PCI_DEVICE(lpc), &lpc->pm, sci_irq);
 
     if (lpc->smi_host_features && fw_cfg) {
hw/m68k/mcf5206.c +1 -1
index cbb361fae9..ec70d482eb 100644
@@ -593,7 +593,7 @@ static void mcf5206_mbar_realize(DeviceState *dev, Error **errp)
                           "mbar", 0x00001000);
     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
 
-    s->pic = qemu_allocate_irqs_orphan(m5206_mbar_set_irq, s, 14);
+    s->pic = qemu_allocate_irqs(OBJECT(dev), "pic-irq", m5206_mbar_set_irq, s, 14);
     m5206_timer_init(&s->timer[0], s->pic[9]);
     m5206_timer_init(&s->timer[1], s->pic[10]);
     s->uart[0] = mcf_uart_create(OBJECT(s), s->pic[12], serial_hd(0));
hw/m68k/q800.c +2 -2
index 177fff0289..677dc4ad80 100644
@@ -474,11 +474,11 @@ static void q800_machine_init(MachineState *machine)
     sysbus_realize(sysbus, &error_fatal);
     /* SCSI and SCSI data IRQs are negative edge triggered */
     sysbus_connect_irq(sysbus, 0,
-                       qemu_irq_invert_orphan(
+                       qemu_irq_invert(OBJECT(machine), "scsi-irq-inv",
                            qdev_get_gpio_in(DEVICE(&m->via2),
                                                    VIA2_IRQ_SCSI_BIT)));
     sysbus_connect_irq(sysbus, 1,
-                       qemu_irq_invert_orphan(
+                       qemu_irq_invert(OBJECT(machine), "scsi-drq-inv",
                            qdev_get_gpio_in(DEVICE(&m->via2),
                                                    VIA2_IRQ_SCSI_DATA_BIT)));
     memory_region_add_subregion(&m->macio, ESP_BASE - IO_BASE,
hw/mips/mips_int.c +1 -1
index 814d07319f..c404063762 100644
@@ -56,7 +56,7 @@ void cpu_mips_irq_init_cpu(MIPSCPU *cpu)
     qemu_irq *qi;
     int i;
 
-    qi = qemu_allocate_irqs_orphan(cpu_mips_irq_request, cpu, 8);
+    qi = qemu_allocate_irqs(OBJECT(cpu), "cpu-irq", cpu_mips_irq_request, cpu, 8);
     for (i = 0; i < 8; i++) {
         env->irq[i] = qi[i];
     }
hw/net/can/can_kvaser_pci.c +1 -1
index 69fc66de14..95bae684d6 100644
@@ -224,7 +224,7 @@ static void kvaser_pci_realize(PCIDevice *pci_dev, Error **errp)
     pci_conf = pci_dev->config;
     pci_conf[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
 
-    d->irq = qemu_allocate_irq_orphan(kvaser_pci_irq_handler, d, 0);
+    d->irq = qemu_allocate_irq(OBJECT(s), "kvaser-irq", kvaser_pci_irq_handler, d, 0);
 
     can_sja_init(s, d->irq);
 
hw/pci-host/pnv_phb3.c +2 -1
index 63da48f5af..410a27a073 100644
@@ -1054,7 +1054,8 @@ static void pnv_phb3_realize(DeviceState *dev, Error **errp)
         ics_set_irq_type(&phb->lsis, i, true);
     }
 
-    phb->qirqs = qemu_allocate_irqs_orphan(ics_set_irq, &phb->lsis, phb->lsis.nr_irqs);
+    phb->qirqs = qemu_allocate_irqs(OBJECT(dev), "lsi-irq",
+                                    ics_set_irq, &phb->lsis, phb->lsis.nr_irqs);
 
     /* MSI sources */
     object_property_set_link(OBJECT(&phb->msis), "phb", OBJECT(phb),
hw/pci-host/pnv_phb3_msi.c +2 -1
index fa5f61ea2b..1bba60c616 100644
@@ -267,7 +267,8 @@ static void phb3_msi_realize(DeviceState *dev, Error **errp)
         return;
     }
 
-    msi->qirqs = qemu_allocate_irqs_orphan(phb3_msi_set_irq, msi, ics->nr_irqs);
+    msi->qirqs = qemu_allocate_irqs(OBJECT(dev), "msi-irq",
+                                    phb3_msi_set_irq, msi, ics->nr_irqs);
 }
 
 static void phb3_msi_instance_init(Object *obj)
hw/pci-host/pnv_phb4.c +2 -1
index 7a1988a2b2..9b567413c7 100644
@@ -1605,7 +1605,8 @@ static void pnv_phb4_realize(DeviceState *dev, Error **errp)
 
     pnv_phb4_update_xsrc(phb);
 
-    phb->qirqs = qemu_allocate_irqs_orphan(xive_source_set_irq, xsrc, xsrc->nr_irqs);
+    phb->qirqs = qemu_allocate_irqs(OBJECT(dev), "lsi-irq",
+                                    xive_source_set_irq, xsrc, xsrc->nr_irqs);
 
     pnv_phb4_xscom_realize(phb);
 }
hw/ppc/e500.c +2 -1
index 62692f5776..f3917c0755 100644
@@ -1089,7 +1089,8 @@ void ppce500_init(MachineState *machine)
                                     sysbus_mmio_get_region(s, 0));
 
         /* Power Off GPIO at Pin 0 */
-        poweroff_irq = qemu_allocate_irq_orphan(ppce500_power_off, NULL, 0);
+        poweroff_irq = qemu_allocate_irq(OBJECT(machine), "poweroff",
+                                         ppce500_power_off, NULL, 0);
         qdev_connect_gpio_out(dev, 0, poweroff_irq);
     }
 
hw/ppc/pnv_lpc.c +1 -1
index e89defb321..a13d633f64 100644
@@ -993,7 +993,7 @@ ISABus *pnv_lpc_isa_create(PnvLpcController *lpc, bool use_cpld, Error **errp)
     }
 
     /* POWER has a 17th irq, QEMU only implements the 16 regular device irqs */
-    irqs = qemu_allocate_irqs_orphan(handler, lpc, ISA_NUM_IRQS);
+    irqs = qemu_allocate_irqs(OBJECT(lpc), "isa-irq", handler, lpc, ISA_NUM_IRQS);
 
     isa_bus_register_input_irqs(isa_bus, irqs);
 
hw/ppc/pnv_psi.c +4 -2
index 1ff33596ac..1f2148a4d3 100644
@@ -509,7 +509,8 @@ static void pnv_psi_power8_realize(DeviceState *dev, Error **errp)
 
     qdev_init_gpio_in(dev, pnv_psi_power8_set_irq, ics->nr_irqs);
 
-    psi->qirqs = qemu_allocate_irqs_orphan(ics_set_irq, ics, ics->nr_irqs);
+    psi->qirqs = qemu_allocate_irqs(OBJECT(dev), "psi-irq",
+                                    ics_set_irq, ics, ics->nr_irqs);
 
     /* XSCOM region for PSI registers */
     pnv_xscom_region_init(&psi->xscom_regs, OBJECT(dev), &pnv_psi_xscom_ops,
@@ -872,7 +873,8 @@ static void pnv_psi_power9_realize(DeviceState *dev, Error **errp)
         xive_source_irq_set_lsi(xsrc, i);
     }
 
-    psi->qirqs = qemu_allocate_irqs_orphan(xive_source_set_irq, xsrc, xsrc->nr_irqs);
+    psi->qirqs = qemu_allocate_irqs(OBJECT(dev), "psi-irq",
+                                    xive_source_set_irq, xsrc, xsrc->nr_irqs);
 
     qdev_init_gpio_in(dev, pnv_psi_power9_set_irq, xsrc->nr_irqs);
 
hw/ppc/spapr_irq.c +1 -1
index a05edb78bc..82869ea03a 100644
@@ -339,7 +339,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
         spapr_xive_hcall_init(spapr);
     }
 
-    spapr->qirqs = qemu_allocate_irqs_orphan(spapr_set_irq, spapr,
+    spapr->qirqs = qemu_allocate_irqs(OBJECT(spapr), "irq", spapr_set_irq, spapr,
                                       SPAPR_NR_XIRQS + SPAPR_IRQ_NR_IPIS);
 
     /*
hw/riscv/sifive_u.c +2 -1
index 3df9fafb12..3f4a9b9b7b 100644
@@ -484,7 +484,8 @@ static void sifive_u_machine_init(MachineState *machine)
 
     /* register gpio-restart */
     qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10,
-                          qemu_allocate_irq_orphan(sifive_u_machine_reset, NULL, 0));
+                          qemu_allocate_irq(OBJECT(machine), "reset-irq",
+                                            sifive_u_machine_reset, NULL, 0));
 
     /* load/create device tree */
     if (machine->dtb) {
hw/scsi/esp-pci.c +1 -1
index 4c02b23a28..bfc61b8f3b 100644
@@ -407,7 +407,7 @@ static void esp_pci_scsi_realize(PCIDevice *dev, Error **errp)
                           "esp-io", 0x80);
 
     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &pci->io);
-    s->irq = qemu_allocate_irq_orphan(esp_irq_handler, pci, 0);
+    s->irq = qemu_allocate_irq(OBJECT(s), "esp-irq", esp_irq_handler, pci, 0);
 
     scsi_bus_init(&s->bus, sizeof(s->bus), d, &esp_pci_scsi_info);
 }
hw/sh4/sh7750.c +1 -1
index 307daa523d..a42b753a1d 100644
@@ -848,5 +848,5 @@ SH7750State *sh7750_init(Object *parent, SuperHCPU *cpu,
 qemu_irq sh7750_irl(SH7750State *s)
 {
     sh_intc_toggle_source(&s->intc.sources[IRL], 1, 0); /* enable */
-    return qemu_allocate_irq_orphan(sh_intc_set_irl, &s->intc.sources[IRL], 0);
+    return qemu_allocate_irq(OBJECT(s), "irl", sh_intc_set_irl, &s->intc.sources[IRL], 0);
 }
hw/sparc/sun4m.c +3 -2
index 0a35b20e17..51f5468a02 100644
@@ -1008,7 +1008,8 @@ static void sun4m_hw_init(MachineState *machine)
 
     if (hwdef->apc_base) {
         apc_init(OBJECT(machine), hwdef->apc_base,
-                 qemu_allocate_irq_orphan(cpu_halt_signal, NULL, 0));
+                 qemu_allocate_irq(OBJECT(machine), "cpu-halt",
+                                   cpu_halt_signal, NULL, 0));
     }
 
     if (hwdef->fd_base) {
@@ -1018,7 +1019,7 @@ static void sun4m_hw_init(MachineState *machine)
         sun4m_fdctrl_init(OBJECT(machine), slavio_irq[22], hwdef->fd_base, fd,
                           &fdc_tc);
     } else {
-        fdc_tc = qemu_allocate_irq_orphan(dummy_fdc_tc, NULL, 0);
+        fdc_tc = qemu_allocate_irq(OBJECT(machine), "fdc-tc", dummy_fdc_tc, NULL, 0);
     }
 
     slavio_misc_init(OBJECT(machine), hwdef->slavio_base, hwdef->aux1_base,
hw/sparc64/sun4u.c +2 -1
index 389436fd3d..7d84dc201f 100644
@@ -311,7 +311,8 @@ static void ebus_realize(PCIDevice *pci_dev, Error **errp)
     }
 
     /* ISA bus */
-    s->isa_irqs_in = qemu_allocate_irqs_orphan(ebus_isa_irq_handler, s, ISA_NUM_IRQS);
+    s->isa_irqs_in = qemu_allocate_irqs(OBJECT(s), "isa-irq-in",
+                                        ebus_isa_irq_handler, s, ISA_NUM_IRQS);
     isa_bus_register_input_irqs(s->isa_bus, s->isa_irqs_in);
     qdev_init_gpio_out_named(DEVICE(s), s->isa_irqs_out, "isa-irq",
                              ISA_NUM_IRQS);
hw/xen/xen-pvh-common.c +3 -2
index 42514a599e..76893c8fcf 100644
@@ -170,7 +170,7 @@ static void xen_create_virtio_mmio_devices(XenPVHMachineState *s)
      */
     for (i = s->cfg.virtio_mmio_num - 1; i >= 0; i--) {
         hwaddr base = s->cfg.virtio_mmio.base + i * s->cfg.virtio_mmio.size;
-        qemu_irq irq = qemu_allocate_irq_orphan(xen_set_irq, NULL,
+        qemu_irq irq = qemu_allocate_irq(OBJECT(s), "virtio-irq[*]", xen_set_irq, NULL,
                                          s->cfg.virtio_mmio_irq_base + i);
 
         sysbus_create_simple(OBJECT(s), "virtio-mmio[*]", "virtio-mmio",
@@ -260,7 +260,8 @@ static inline void xenpvh_gpex_init(XenPVHMachineState *s,
     assert(xpc->set_pci_intx_irq);
 
     for (i = 0; i < PCI_NUM_PINS; i++) {
-        qemu_irq irq = qemu_allocate_irq_orphan(xpc->set_pci_intx_irq, s, i);
+        qemu_irq irq = qemu_allocate_irq(OBJECT(s), "pci-intx-irq[*]",
+                                         xpc->set_pci_intx_irq, s, i);
 
         sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq);
         gpex_set_irq_num(GPEX_HOST(dev), i, s->cfg.pci_intx_irq_base + i);
Signature · 1 line
2.47.1
#273 lore raw

RFC PATCH v2 116/137 hw/core: Parent GPIO input IRQs and embedded IRQState via QOM

In reply to Alexander Graf

qdev_init_gpio_in_named_with_opaque() now uses qemu_extend_irqs() directly, so the input IRQ objects are parented under the device via object_new_child() and the manual object_property_add_child() loop goes away. device_finalize() no longer needs to free the IRQ objects explicitly since child<> property destruction does that; only the qemu_irq* array itself needs g_free().

Convert the six users of qemu_init_irq()/qemu_init_irqs() (which initialize an IRQState embedded in a device struct) to qemu_init_irq_child() so those IRQs also get a QOM parent.

qdev_connect_gpio_out_named() now asserts that its input pin has a parent instead of falling back to /machine/unattached; every qemu_irq that reaches it is parented after this and the preceding commits.

AI-used-for: code (refactoring)

---
 hw/core/gpio.c    | 24 ++++--------------------
 hw/core/qdev.c    |  3 ++-
 hw/ipack/ipack.c  | 11 +++++++++--
 hw/isa/vt82c686.c |  3 ++-
 hw/net/imx_fec.c  |  2 +-
 hw/net/lan9118.c  |  2 +-
 hw/ppc/pegasos.c  |  3 ++-
 hw/sh4/r2d.c      |  5 ++++-
 8 files changed, 25 insertions(+), 28 deletions(-)
hw/core/gpio.c +4 -20
index ef8ef8f25a..f76d604520 100644
@@ -45,24 +45,12 @@ void qdev_init_gpio_in_named_with_opaque(DeviceState *dev,
                                          void *opaque,
                                          const char *name, int n)
 {
-    int i;
     NamedGPIOList *gpio_list = qdev_get_named_gpio_list(dev, name);
 
     assert(gpio_list->num_out == 0 || !name);
-    gpio_list->in = qemu_extend_irqs_orphan(gpio_list->in, gpio_list->num_in, handler,
-                                     opaque, n);
-
-    if (!name) {
-        name = "unnamed-gpio-in";
-    }
-    for (i = gpio_list->num_in; i < gpio_list->num_in + n; i++) {
-        gchar *propname = g_strdup_printf("%s[%u]", name, i);
-
-        object_property_add_child(OBJECT(dev), propname,
-                                  OBJECT(gpio_list->in[i]));
-        g_free(propname);
-    }
-
+    gpio_list->in = qemu_extend_irqs(OBJECT(dev), name ?: "unnamed-gpio-in",
+                                     gpio_list->in, gpio_list->num_in,
+                                     handler, opaque, n);
     gpio_list->num_in += n;
 }
 
@@ -119,11 +107,7 @@ void qdev_connect_gpio_out_named(DeviceState *dev, const char *name, int n,
 {
     char *propname = g_strdup_printf("%s[%d]",
                                      name ? name : "unnamed-gpio-out", n);
-    if (input_pin && !OBJECT(input_pin)->parent) {
-        /* We need a name for object_property_set_link to work */
-        object_property_add_child(machine_get_container("unattached"),
-                                  "non-qdev-gpio[*]", OBJECT(input_pin));
-    }
+    g_assert(!input_pin || OBJECT(input_pin)->parent);
     object_property_set_link(OBJECT(dev), propname,
                              OBJECT(input_pin), &error_abort);
     g_free(propname);
hw/core/qdev.c +2 -1
index 89bc1e30c0..9a68fb426e 100644
@@ -698,7 +698,8 @@ static void device_finalize(Object *obj)
 
     QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
         QLIST_REMOVE(ngl, node);
-        qemu_free_irqs(ngl->in, ngl->num_in);
+        /* IRQs are child<> properties of dev; freed via child destruction */
+        g_free(ngl->in);
         g_free(ngl->name);
         g_free(ngl);
         /* ngl->out irqs are owned by the other end and should not be freed
hw/ipack/ipack.c +9 -2
index f2e5524fa8..dad0d486df 100644
@@ -55,18 +55,25 @@ static void ipack_device_realize(DeviceState *dev, Error **errp)
     }
     bus->free_slot = idev->slot + 1;
 
-    qemu_init_irqs(idev->irq, ARRAY_SIZE(idev->irq), bus->set_irq, idev);
+    for (int i = 0; i < ARRAY_SIZE(idev->irq); i++) {
+        qemu_init_irq_child(OBJECT(idev), "irq[*]", &idev->irq[i],
+                            bus->set_irq, idev, i);
+    }
 
     k->realize(dev, errp);
 }
 
 static void ipack_device_unrealize(DeviceState *dev)
 {
+    IPackDevice *idev = IPACK_DEVICE(dev);
     IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
 
     if (k->unrealize) {
         k->unrealize(dev);
-        return;
+    }
+
+    for (int i = 0; i < ARRAY_SIZE(idev->irq); i++) {
+        object_unparent(OBJECT(&idev->irq[i]));
     }
 }
 
hw/isa/vt82c686.c +2 -1
index 2337c3115b..f93bf49754 100644
@@ -722,7 +722,8 @@ static void via_isa_realize(PCIDevice *d, Error **errp)
 
     qdev_init_gpio_out_named(dev, &s->cpu_intr, "intr", 1);
     qdev_init_gpio_in_named(dev, via_isa_pirq, "pirq", PCI_NUM_PINS);
-    qemu_init_irq(&s->i8259_irq, via_isa_request_i8259_irq, s, 0);
+    qemu_init_irq_child(OBJECT(s), "i8259-irq", &s->i8259_irq,
+                        via_isa_request_i8259_irq, s, 0);
     isa_bus = isa_bus_new(dev, pci_address_space(d), pci_address_space_io(d),
                           errp);
 
hw/net/imx_fec.c +1 -1
index c177b7ff2b..38e0f64462 100644
@@ -1205,7 +1205,7 @@ static void imx_eth_realize(DeviceState *dev, Error **errp)
     sysbus_init_irq(sbd, &s->irq[0]);
     sysbus_init_irq(sbd, &s->irq[1]);
 
-    qemu_init_irq(&s->mii_irq, imx_phy_update_irq, s, 0);
+    qemu_init_irq_child(OBJECT(s), "mii-irq", &s->mii_irq, imx_phy_update_irq, s, 0);
     object_initialize_child(OBJECT(s), "mii", &s->mii, TYPE_LAN9118_PHY);
     if (!sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->mii), errp)) {
         return;
hw/net/lan9118.c +1 -1
index 91c3533702..2003280fc4 100644
@@ -1274,7 +1274,7 @@ static void lan9118_realize(DeviceState *dev, Error **errp)
     const MemoryRegionOps *mem_ops =
             s->mode_16bit ? &lan9118_16bit_mem_ops : &lan9118_mem_ops;
 
-    qemu_init_irq(&s->mii_irq, lan9118_update_irq, s, 0);
+    qemu_init_irq_child(OBJECT(s), "mii-irq", &s->mii_irq, lan9118_update_irq, s, 0);
     object_initialize_child(OBJECT(s), "mii", &s->mii, TYPE_LAN9118_PHY);
     if (!sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->mii), errp)) {
         return;
hw/ppc/pegasos.c +2 -1
index 426a54d8ee..845b6355e9 100644
@@ -134,7 +134,8 @@ static void pegasos2_setup_pci_irq(PegasosMachineState *pm)
                                                    TYPE_OR_IRQ, &error_fatal,
                                                    "num-lines", "2", NULL);
                 qdev_realize(DEVICE(ori), NULL, &error_fatal);
-                qemu_init_irq(&pm->pci_irqs[i], pegasos2_pci_irq, pm, i);
+                qemu_init_irq_child(OBJECT(pm), "pci-irq[*]", &pm->pci_irqs[i],
+                                    pegasos2_pci_irq, pm, i);
                 qdev_connect_gpio_out(DEVICE(ori), 0, &pm->pci_irqs[i]);
                 pm->mv_pirq[i] = qdev_get_gpio_in_named(pm->nb, "gpp", 12 + i);
                 pm->via_pirq[i] = qdev_get_gpio_in_named(pm->sb, "pirq", i);
hw/sh4/r2d.c +4 -1
index 8deda6e5f4..21e804f3d9 100644
@@ -200,7 +200,10 @@ static r2d_fpga_t *r2d_fpga_init(Object *owner,
     memory_region_init_io(&s->iomem, owner, &r2d_fpga_ops, s, "r2d-fpga", 0x40);
     memory_region_add_subregion(sysmem, base, &s->iomem);
 
-    qemu_init_irqs(s->irq, NR_IRQS, r2d_fpga_irq_set, s);
+    for (int i = 0; i < NR_IRQS; i++) {
+        qemu_init_irq_child(owner, "fpga-irq[*]", &s->irq[i],
+                            r2d_fpga_irq_set, s, i);
+    }
 
     return s;
 }
Signature · 1 line
2.47.1
#274 lore raw

RFC PATCH v2 117/137 irq: Delete the *_orphan() IRQ allocation variants

In reply to Alexander Graf

Every caller has been converted to the parented form; drop the transitional *_orphan() helpers.

AI-used-for: code (refactoring)

---
 hw/core/irq.c         | 35 -----------------------------------
 include/hw/core/irq.h | 20 +-------------------
 2 files changed, 1 insertion(+), 54 deletions(-)
hw/core/irq.c +0 -35
index 1ae53a74d3..459ff467ff 100644
@@ -68,34 +68,6 @@ void qemu_init_irqs(IRQState irq[], size_t count,
     }
 }
 
-qemu_irq *qemu_extend_irqs_orphan(qemu_irq *old, int n_old, qemu_irq_handler handler,
-                           void *opaque, int n)
-{
-    qemu_irq *s;
-    int i;
-
-    if (!old) {
-        n_old = 0;
-    }
-    s = old ? g_renew(qemu_irq, old, n + n_old) : g_new(qemu_irq, n);
-    for (i = n_old; i < n + n_old; i++) {
-        s[i] = qemu_allocate_irq_orphan(handler, opaque, i);
-    }
-    return s;
-}
-
-qemu_irq *qemu_allocate_irqs_orphan(qemu_irq_handler handler, void *opaque, int n)
-{
-    return qemu_extend_irqs_orphan(NULL, 0, handler, opaque, n);
-}
-
-qemu_irq qemu_allocate_irq_orphan(qemu_irq_handler handler, void *opaque, int n)
-{
-    IRQState *irq = IRQ(object_new(TYPE_IRQ));
-    init_irq_fields(irq, handler, opaque, n);
-    return irq;
-}
-
 qemu_irq qemu_allocate_irq(Object *owner, const char *name,
                            qemu_irq_handler handler, void *opaque, int n)
 {
@@ -153,13 +125,6 @@ static void qemu_notirq(void *opaque, int line, int level)
     qemu_set_irq(irq, !level);
 }
 
-qemu_irq qemu_irq_invert_orphan(qemu_irq irq)
-{
-    /* The default state for IRQs is low, so raise the output now.  */
-    qemu_irq_raise(irq);
-    return qemu_allocate_irq_orphan(qemu_notirq, irq, 0);
-}
-
 qemu_irq qemu_irq_invert(Object *owner, const char *name, qemu_irq irq)
 {
     /* The default state for IRQs is low, so raise the output now.  */
include/hw/core/irq.h +1 -19
index 5ccccd2697..89923cf4a8 100644
@@ -74,19 +74,8 @@ void qemu_init_irq_child(Object *parent, const char *propname,
 void qemu_init_irqs(IRQState irq[], size_t count,
                     qemu_irq_handler handler, void *opaque);
 
-/* Returns an array of N IRQs. Each IRQ is assigned the argument handler and
- * opaque data.
- */
-qemu_irq *qemu_allocate_irqs_orphan(qemu_irq_handler handler, void *opaque, int n);
-
 /*
- * Allocates a single IRQ. The irq is assigned with a handler, an opaque
- * data and the interrupt number.
- */
-qemu_irq qemu_allocate_irq_orphan(qemu_irq_handler handler, void *opaque, int n);
-
-/*
- * Same as the *_orphan() variants above, but the new IRQ objects are
+ * Allocate IRQ objects with an explicit QOM parent.  The new objects are
  * parented under @owner with property name @name (or @name[*] for the
  * array/extend variants).  The reference is held by @owner; call
  * qemu_free_irq() to unparent and free.
@@ -99,17 +88,10 @@ qemu_irq *qemu_extend_irqs(Object *owner, const char *name,
                            qemu_irq *old, int n_old,
                            qemu_irq_handler handler, void *opaque, int n);
 
-/* Extends an Array of IRQs. Old IRQs have their handlers and opaque data
- * preserved. New IRQs are assigned the argument handler and opaque data.
- */
-qemu_irq *qemu_extend_irqs_orphan(qemu_irq *old, int n_old, qemu_irq_handler handler,
-                                void *opaque, int n);
-
 void qemu_free_irqs(qemu_irq *s, int n);
 void qemu_free_irq(qemu_irq irq);
 
 /* Returns a new IRQ with opposite polarity.  */
-qemu_irq qemu_irq_invert_orphan(qemu_irq irq);
 qemu_irq qemu_irq_invert(Object *owner, const char *name, qemu_irq irq);
 
 /* For internal use in qtest. */
Signature · 1 line
2.47.1
#275 lore raw

RFC PATCH v2 118/137 sysbus: Parent main-system-bus directly under /machine

In reply to Alexander Graf

The main system bus is the only object deliberately placed under /machine/unattached; every other resident is an accidental orphan. Promoting it to /machine/sysbus makes /machine/unattached a pure bug indicator that later commits can turn into a hard error.

The canonical path changes from /machine/unattached/sysbus to /machine/sysbus. qom-list users that hard-code the old path need to be updated; the hmp/qmp qom-tree output naturally follows.

AI-used-for: code (refactoring)

---
 system/vl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
system/vl.c +1 -1
index 061cbdf860..b4434839df 100644
@@ -2211,7 +2211,7 @@ static void qemu_create_machine(QDict *qdict)
     object_property_add_child(object_get_root(), "machine",
                               OBJECT(current_machine));
     qemu_create_machine_containers(OBJECT(current_machine));
-    object_property_add_child(machine_get_container("unattached"),
+    object_property_add_child(OBJECT(current_machine),
                               "sysbus", OBJECT(sysbus_get_default()));
 
     if (machine_class->minimum_page_bits) {
Signature · 1 line
2.47.1
#276 lore raw

RFC PATCH v2 119/137 accel: Parent the accelerator singleton under /machine

In reply to Alexander Graf

do_configure_accelerator() runs after qemu_create_machine(), so current_machine is available. Give the AccelState singleton a canonical path at /machine/accel and drop the floating caller reference; on init failure, unparent instead of unref.

AI-used-for: code (refactoring)

---
 accel/accel-system.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
accel/accel-system.c +3 -1
index 9176665202..987c982c16 100644
@@ -41,12 +41,14 @@ int accel_init_machine(AccelState *accel, MachineState *ms)
     AccelClass *acc = ACCEL_GET_CLASS(accel);
     int ret;
     ms->accelerator = accel;
+    object_property_add_child(OBJECT(ms), "accel", OBJECT(accel));
+    object_unref(OBJECT(accel));
     *(acc->allowed) = true;
     ret = acc->init_machine(accel, ms);
     if (ret < 0) {
         ms->accelerator = NULL;
         *(acc->allowed) = false;
-        object_unref(OBJECT(accel));
+        object_unparent(OBJECT(accel));
     } else {
         object_set_accelerator_compat_props(acc->compat_props);
     }
Signature · 1 line
2.47.1
#277 lore raw

RFC PATCH v2 120/137 hw/intc/mips_gic: Register reset handler at realize, not instance_init

In reply to Alexander Graf

mips_gic_init() calls qemu_register_reset(gic_reset, s), but there is no matching qemu_unregister_reset() in an instance_finalize. This leaks a reset handler every time the type is introspected via device-list-properties (object_new + object_unref). The following patch makes each legacy reset handler visible in the composition tree under /machine/reset-container/legacy-reset[*], which turns this pre-existing leak into a device-introspect-test failure.

Move the registration to mips_gic_realize(), matching every other in-tree interrupt controller.

AI-used-for: code (refactoring)

---
 hw/intc/mips_gic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/mips_gic.c +1 -1
index 4777c70d6b..c0f1501bcc 100644
@@ -394,7 +394,6 @@ static void mips_gic_init(Object *obj)
     memory_region_init_io(&s->mr, OBJECT(s), &gic_ops, s,
                           "mips-gic", GIC_ADDRSPACE_SZ);
     sysbus_init_mmio(sbd, &s->mr);
-    qemu_register_reset(gic_reset, s);
 }
 
 static void mips_gic_realize(DeviceState *dev, Error **errp)
@@ -430,6 +429,7 @@ static void mips_gic_realize(DeviceState *dev, Error **errp)
     for (i = 0; i < s->num_irq; i++) {
         s->irq_state[i].irq = qdev_get_gpio_in(dev, i);
     }
+    qemu_register_reset(gic_reset, s);
 }
 
 static const Property mips_gic_properties[] = {
Signature · 1 line
2.47.1
#278 lore raw

RFC PATCH v2 121/137 hw/core/reset: Give the root reset container and legacy shims a QOM path

In reply to Alexander Graf

The root ResettableContainer and the LegacyReset shim objects are long-lived singletons held only by static pointers and refcounts. Give them a canonical path in the composition tree so qom-tree/qom-list can enumerate them and so device_set_realized() can require a QOM parent for every object.

/machine/reset-container (ResettableContainer)
/machine/reset-container/legacy-reset[N] (LegacyReset shims)

get_root_reset_container() is first reached from configure_accelerators() or machine_run_board_init(), both of which run after qemu_create_machine(), so qdev_get_machine() is available. qemu_unregister_reset() switches from object_unref() to object_unparent() to release the child<> ref.

AI-used-for: code (refactoring)

---
 hw/core/reset.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
hw/core/reset.c +9 -4
index e7230b49b7..34c1ab84d9 100644
@@ -27,6 +27,7 @@
 #include "system/reset.h"
 #include "hw/core/resettable.h"
 #include "hw/core/resetcontainer.h"
+#include "hw/core/qdev.h"
 
 /*
  * Return a pointer to the singleton container that holds all the Resettable
@@ -38,7 +39,9 @@ static ResettableContainer *get_root_reset_container(void)
 
     if (!root_reset_container) {
         root_reset_container =
-            RESETTABLE_CONTAINER(object_new(TYPE_RESETTABLE_CONTAINER));
+            RESETTABLE_CONTAINER(object_new_child(qdev_get_machine(),
+                                          "reset-container",
+                                          TYPE_RESETTABLE_CONTAINER));
     }
     return root_reset_container;
 }
@@ -94,7 +97,8 @@ static void legacy_reset_class_init(ObjectClass *klass, const void *data)
 
 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
 {
-    Object *obj = object_new(TYPE_LEGACY_RESET);
+    Object *obj = object_new_child(OBJECT(get_root_reset_container()),
+                                   "legacy-reset[*]", TYPE_LEGACY_RESET);
     LegacyReset *lr = LEGACY_RESET(obj);
 
     lr->func = func;
@@ -104,7 +108,8 @@ void qemu_register_reset(QEMUResetHandler *func, void *opaque)
 
 void qemu_register_reset_nosnapshotload(QEMUResetHandler *func, void *opaque)
 {
-    Object *obj = object_new(TYPE_LEGACY_RESET);
+    Object *obj = object_new_child(OBJECT(get_root_reset_container()),
+                                   "legacy-reset[*]", TYPE_LEGACY_RESET);
     LegacyReset *lr = LEGACY_RESET(obj);
 
     lr->func = func;
@@ -156,7 +161,7 @@ void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
 
     if (obj) {
         qemu_unregister_resettable(obj);
-        object_unref(obj);
+        object_unparent(obj);
     }
 }
 
Signature · 1 line
2.47.1
#279 lore raw

RFC PATCH v2 122/137 system: Parent per-MR helper objects under their owner

In reply to Alexander Graf

RamBlockAttributes and MemoryRegionPortioList objects are long-lived helpers whose lifetime is already tied to a MemoryRegion or its owner. Give each a canonical path under that owner instead of leaving them as floating refcounted objects:

<mr-path>/attributes (RamBlockAttributes)
<owner-path>/portio-list[N] (MemoryRegionPortioList)

The destroy paths switch from object_unref() to object_unparent() now that the parent holds the reference.

RamDiscardManager is deliberately left as-is: it is unref'd from memory_region_finalize() itself, so making it a child<> of the MR would introduce a use-after-free during MR teardown. It can move under a different owner in a follow-up once the RDM lifecycle is untangled from MR finalize.

AI-used-for: code (refactoring)

---
 system/ioport.c               | 5 +++--
 system/ram-block-attributes.c | 7 ++++---
 2 files changed, 7 insertions(+), 5 deletions(-)
system/ioport.c +3 -2
index 0415b3cd60..77ab77d7be 100644
@@ -145,7 +145,7 @@ void portio_list_destroy(PortioList *piolist)
     for (i = 0; i < piolist->nr; ++i) {
         mrpio = container_of(piolist->regions[i], MemoryRegionPortioList, mr);
         object_unparent(OBJECT(&mrpio->mr));
-        object_unref(mrpio);
+        object_unparent(OBJECT(mrpio));
     }
     g_free(piolist->regions);
 }
@@ -228,7 +228,8 @@ static void portio_list_add_1(PortioList *piolist,
 
     /* Copy the sub-list and null-terminate it.  */
     mrpio = MEMORY_REGION_PORTIO_LIST(
-                object_new(TYPE_MEMORY_REGION_PORTIO_LIST));
+                object_new_child(piolist->owner, "portio-list[*]",
+                                 TYPE_MEMORY_REGION_PORTIO_LIST));
     mrpio->portio_opaque = piolist->opaque;
     mrpio->ports = g_new0(MemoryRegionPortio, count + 1);
     memcpy(mrpio->ports, pio_init, sizeof(MemoryRegionPortio) * count);
system/ram-block-attributes.c +4 -3
index 59ec7a28eb..0967bed6d3 100644
@@ -182,12 +182,13 @@ RamBlockAttributes *ram_block_attributes_create(RAMBlock *ram_block)
     RamBlockAttributes *attr;
     MemoryRegion *mr = ram_block->mr;
 
-    attr = RAM_BLOCK_ATTRIBUTES(object_new(TYPE_RAM_BLOCK_ATTRIBUTES));
+    attr = RAM_BLOCK_ATTRIBUTES(object_new_child(OBJECT(mr), "attributes",
+                                                 TYPE_RAM_BLOCK_ATTRIBUTES));
 
     attr->ram_block = ram_block;
 
     if (memory_region_add_ram_discard_source(mr, RAM_DISCARD_SOURCE(attr))) {
-        object_unref(OBJECT(attr));
+        object_unparent(OBJECT(attr));
         return NULL;
     }
     attr->bitmap_size = DIV_ROUND_UP(int128_get64(mr->size), block_size);
@@ -202,7 +203,7 @@ void ram_block_attributes_destroy(RamBlockAttributes *attr)
 
     g_free(attr->bitmap);
     memory_region_del_ram_discard_source(attr->ram_block->mr, RAM_DISCARD_SOURCE(attr));
-    object_unref(OBJECT(attr));
+    object_unparent(OBJECT(attr));
 }
 
 static void ram_block_attributes_init(Object *obj)
Signature · 1 line
2.47.1
#280 lore raw

RFC PATCH v2 123/137 hw/virtio, hw/display: Parent per-device helper objects

In reply to Alexander Graf

The virtio-mem system-reset shim, the vhost-user-gpu backend, and its socket chardev live exactly as long as their enclosing virtio device. Parent them under it so they show up in the composition tree instead of floating with only a refcount.

The unrealize/finalize/error paths switch from object_unref() to object_unparent(), and vhost_user_gpu_instance_finalize() drops its now-redundant unref: the vhost-backend is a child<> of the GPU and is released automatically when the GPU is finalized.

AI-used-for: code (refactoring)

---
 hw/display/vhost-user-gpu.c | 12 ++++++------
 hw/virtio/virtio-mem.c      |  5 +++--
 2 files changed, 9 insertions(+), 8 deletions(-)
hw/display/vhost-user-gpu.c +6 -6
index 57360898ca..c2c150bdcf 100644
@@ -439,7 +439,8 @@ vhost_user_gpu_do_set_socket(VhostUserGPU *g, Error **errp)
         return false;
     }
 
-    chr = CHARDEV(object_new(TYPE_CHARDEV_SOCKET));
+    chr = CHARDEV(object_new_child(OBJECT(g), "vhost-chardev",
+                                   TYPE_CHARDEV_SOCKET));
     if (!chr || qemu_chr_add_client(chr, sv[0]) == -1) {
         error_setg(errp, "Failed to make socket chardev");
         goto err;
@@ -462,7 +463,7 @@ err:
     close(sv[0]);
     close(sv[1]);
     if (chr) {
-        object_unref(OBJECT(chr));
+        object_unparent(OBJECT(chr));
     }
     return false;
 }
@@ -584,7 +585,8 @@ vhost_user_gpu_instance_init(Object *obj)
 {
     VhostUserGPU *g = VHOST_USER_GPU(obj);
 
-    g->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND));
+    g->vhost = VHOST_USER_BACKEND(object_new_child(obj, "vhost-backend",
+                                                   TYPE_VHOST_USER_BACKEND));
     object_property_add_alias(obj, "chardev",
                               OBJECT(g->vhost), "chardev");
 }
@@ -592,9 +594,7 @@ vhost_user_gpu_instance_init(Object *obj)
 static void
 vhost_user_gpu_instance_finalize(Object *obj)
 {
-    VhostUserGPU *g = VHOST_USER_GPU(obj);
-
-    object_unref(OBJECT(g->vhost));
+    /* g->vhost is a child<> of obj and released with it */
 }
 
 static void
hw/virtio/virtio-mem.c +3 -2
index 35e03ed759..c45cbdbe32 100644
@@ -1011,7 +1011,8 @@ static void virtio_mem_device_realize(DeviceState *dev, Error **errp)
      * we need a dedicated reset object that only gets called during
      * qemu_devices_reset().
      */
-    obj = object_new(TYPE_VIRTIO_MEM_SYSTEM_RESET);
+    obj = object_new_child(OBJECT(vmem), "system-reset",
+                           TYPE_VIRTIO_MEM_SYSTEM_RESET);
     vmem->system_reset = VIRTIO_MEM_SYSTEM_RESET(obj);
     vmem->system_reset->vmem = vmem;
     qemu_register_resettable(obj);
@@ -1023,7 +1024,7 @@ static void virtio_mem_device_unrealize(DeviceState *dev)
     VirtIOMEM *vmem = VIRTIO_MEM(dev);
 
     qemu_unregister_resettable(OBJECT(vmem->system_reset));
-    object_unref(OBJECT(vmem->system_reset));
+    object_unparent(OBJECT(vmem->system_reset));
 
     if (vmem->early_migration) {
         vmstate_unregister(VMSTATE_IF(vmem), &vmstate_virtio_mem_device_early,
Signature · 1 line
2.47.1
#281 lore raw

RFC PATCH v2 124/137 hw/i386/amd_iommu: Parent internally-created AMDVI-PCI under the IOMMU

In reply to Alexander Graf

When the AMD IOMMU creates its own PCI companion device (no pci-id supplied by the user), parent it under the sysbus IOMMU so it has a canonical path at <iommu>/pci. The user-supplied path already parents via -device.

AI-used-for: code (refactoring)

---
 hw/i386/amd_iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/i386/amd_iommu.c +2 -1
index 90252c52af..e3394bad6e 100644
@@ -2698,7 +2698,8 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
         s->pci = AMD_IOMMU_PCI(pdev);
         dc->vmsd = &vmstate_amdvi_sysbus_migratable;
     } else {
-        s->pci = AMD_IOMMU_PCI(object_new(TYPE_AMD_IOMMU_PCI));
+        s->pci = AMD_IOMMU_PCI(object_new_child(OBJECT(s), "pci",
+                                                TYPE_AMD_IOMMU_PCI));
         /* This device should take care of IOMMU PCI properties */
         if (!qdev_realize(DEVICE(s->pci), &bus->qbus, errp)) {
             return;
Signature · 1 line
2.47.1
#282 lore raw

RFC PATCH v2 125/137 hw/s390x, hw/remote: Parent per-devfn IOMMU objects

In reply to Alexander Graf

Give the per-devfn S390 PCI IOMMU object a canonical path under the S390 PCI host state. s390_pci_iommu_free() already calls object_unparent(), which was previously a no-op; it now performs the actual release.

For the multi-process remote IOMMU, replace the raw object_new(TYPE_MEMORY_REGION) with a proper memory_region_init() under the RemoteIommu object so the MR gets an owner like every other MR in the tree.

AI-used-for: code (refactoring)

---
 hw/remote/iommu.c       | 7 ++++---
 hw/s390x/s390-pci-bus.c | 9 +++++++--
 2 files changed, 11 insertions(+), 5 deletions(-)
hw/remote/iommu.c +4 -3
index 3e0758a21e..52ee9765e6 100644
@@ -52,8 +52,9 @@ static AddressSpace *remote_iommu_find_add_as(PCIBus *pci_bus,
     }
 
     if (!elem->mr) {
-        elem->mr = MEMORY_REGION(object_new(TYPE_MEMORY_REGION));
-        memory_region_set_size(elem->mr, UINT64_MAX);
+        g_autofree char *name = g_strdup_printf("mr-devfn-%02x", devfn);
+        elem->mr = g_new0(MemoryRegion, 1);
+        memory_region_init(elem->mr, OBJECT(iommu), name, UINT64_MAX);
         address_space_init(&elem->as, elem->mr, NULL);
     }
 
@@ -75,7 +76,7 @@ void remote_iommu_unplug_dev(PCIDevice *pci_dev)
 
     address_space_destroy(&elem->as);
 
-    object_unref(elem->mr);
+    object_unparent(OBJECT(elem->mr));
 
     elem->mr = NULL;
 }
hw/s390x/s390-pci-bus.c +7 -2
index 4f0a8a787b..da8406cc4e 100644
@@ -618,7 +618,13 @@ static S390PCIIOMMU *s390_pci_get_iommu(S390pciState *s, PCIBus *bus,
 
     iommu = table->iommu[PCI_SLOT(devfn)];
     if (!iommu) {
-        iommu = S390_PCI_IOMMU(object_new(TYPE_S390_PCI_IOMMU));
+        char *ch_name = g_strdup_printf("iommu[%02x:%02x.%01x]",
+                                        pci_bus_num(bus),
+                                        PCI_SLOT(devfn),
+                                        PCI_FUNC(devfn));
+        iommu = S390_PCI_IOMMU(object_new_child(OBJECT(s), ch_name,
+                                                TYPE_S390_PCI_IOMMU));
+        g_free(ch_name);
 
         char *mr_name = g_strdup_printf("iommu-root-%02x:%02x.%01x",
                                         pci_bus_num(bus),
@@ -798,7 +804,6 @@ static void s390_pci_iommu_free(S390pciState *s, PCIBus *bus, int32_t devfn)
     address_space_destroy(&iommu->as);
     object_unparent(OBJECT(&iommu->mr));
     object_unparent(OBJECT(iommu));
-    object_unref(OBJECT(iommu));
 }
 
 S390PCIGroup *s390_group_create(int id, int host_id)
Signature · 1 line
2.47.1
#283 lore raw

RFC PATCH v2 126/137 hw/pci-host/raven: Parent the OR-IRQ under the host bridge

In reply to Alexander Graf

The PReP raven host bridge creates an or-irq gate to fan PCI interrupts into IRQ 15. Parent it under the host bridge so it gets a canonical path.

AI-used-for: code (refactoring)

---
 hw/pci-host/raven.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/pci-host/raven.c +2 -1
index b6ab686ff5..62d78efde5 100644
@@ -249,7 +249,8 @@ static void raven_pcihost_realizefn(DeviceState *d, Error **errp)
      * According to PReP specification section 6.1.6 "System Interrupt
      * Assignments", all PCI interrupts are routed via IRQ 15
      */
-    s->or_irq = OR_IRQ(object_new(TYPE_OR_IRQ));
+    s->or_irq = OR_IRQ(object_new_child(OBJECT(s), "or-irq",
+                                        TYPE_OR_IRQ));
     object_property_set_int(OBJECT(s->or_irq), "num-lines", PCI_NUM_PINS,
                             &error_fatal);
     qdev_realize(DEVICE(s->or_irq), NULL, &error_fatal);
Signature · 1 line
2.47.1
#284 lore raw

RFC PATCH v2 127/137 hw/arm/mps2: Parent the OR-IRQ gates under the machine

In reply to Alexander Graf

The mps2 board creates several TYPE_OR_IRQ gates for UART overflow, per-UART tx/rx, and SPI interrupts. Parent them under the machine so they get canonical paths. The 282/325 pair sit on mutually-exclusive switch arms, so a single "uart-orgate" name is sufficient; the loop-created gates use "[*]" auto-indexing.

AI-used-for: code (refactoring)

---
 hw/arm/mps2.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/arm/mps2.c +5 -4
index 1dec4a5d4d..1032f47cb8 100644
@@ -279,7 +279,7 @@ static void mps2_common_init(MachineState *machine)
         Object *orgate;
         DeviceState *orgate_dev;
 
-        orgate = object_new(TYPE_OR_IRQ);
+        orgate = object_new_child(OBJECT(mms), "uart-orgate", TYPE_OR_IRQ);
         object_property_set_int(orgate, "num-lines", 6, &error_fatal);
         qdev_realize(DEVICE(orgate), NULL, &error_fatal);
         orgate_dev = DEVICE(orgate);
@@ -322,7 +322,7 @@ static void mps2_common_init(MachineState *machine)
         Object *orgate;
         DeviceState *orgate_dev;
 
-        orgate = object_new(TYPE_OR_IRQ);
+        orgate = object_new_child(OBJECT(mms), "uart-orgate", TYPE_OR_IRQ);
         object_property_set_int(orgate, "num-lines", 10, &error_fatal);
         qdev_realize(DEVICE(orgate), NULL, &error_fatal);
         orgate_dev = DEVICE(orgate);
@@ -338,7 +338,8 @@ static void mps2_common_init(MachineState *machine)
             DeviceState *txrx_orgate_dev, *dev;
             SysBusDevice *s;
 
-            txrx_orgate = object_new(TYPE_OR_IRQ);
+            txrx_orgate = object_new_child(OBJECT(mms), "uart-txrx-orgate[*]",
+                                           TYPE_OR_IRQ);
             object_property_set_int(txrx_orgate, "num-lines", 2, &error_fatal);
             qdev_realize(DEVICE(txrx_orgate), NULL, &error_fatal);
             txrx_orgate_dev = DEVICE(txrx_orgate);
@@ -430,7 +431,7 @@ static void mps2_common_init(MachineState *machine)
         Object *orgate;
         int j;
 
-        orgate = object_new(TYPE_OR_IRQ);
+        orgate = object_new_child(OBJECT(mms), "spi-orgate[*]", TYPE_OR_IRQ);
         object_property_set_int(orgate, "num-lines", 2, &error_fatal);
         orgate_dev = DEVICE(orgate);
         qdev_realize(orgate_dev, NULL, &error_fatal);
Signature · 1 line
2.47.1
#285 lore raw

RFC PATCH v2 128/137 hw/arm/digic: Parent the DIGIC SoC under the machine

In reply to Alexander Graf

Bring digic in line with cubieboard/orangepi/bpim2u/imx6ul/imx7, which all parent their SoC as /machine/soc.

AI-used-for: code (refactoring)

---
 hw/arm/digic_boards.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/arm/digic_boards.c +2 -1
index 92b66f34f7..bdc5f7324b 100644
@@ -50,7 +50,8 @@ typedef struct DigicBoard {
 static void digic4_board_init(MachineState *machine, DigicBoard *board)
 {
     Error *err = NULL;
-    DigicState *s = DIGIC(object_new(TYPE_DIGIC));
+    DigicState *s = DIGIC(object_new_child(OBJECT(machine), "soc",
+                                           TYPE_DIGIC));
     MachineClass *mc = MACHINE_GET_CLASS(machine);
 
     if (machine->ram_size != mc->default_ram_size) {
Signature · 1 line
2.47.1
#286 lore raw

RFC PATCH v2 129/137 hw/microblaze: Parent the CPU under the machine

In reply to Alexander Graf

Both petalogix boards create exactly one MicroBlaze CPU via object_new(); parent it as /machine/cpu[0] like every other single-CPU board.

AI-used-for: code (refactoring)

---
 hw/microblaze/petalogix_ml605_mmu.c      | 3 ++-
 hw/microblaze/petalogix_s3adsp1800_mmu.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
hw/microblaze/petalogix_ml605_mmu.c +2 -1
index 26470b0251..cfc76bf518 100644
@@ -82,7 +82,8 @@ petalogix_ml605_init(MachineState *machine)
     qemu_irq irq[32];
 
     /* init CPUs */
-    cpu = MICROBLAZE_CPU(object_new(TYPE_MICROBLAZE_CPU));
+    cpu = MICROBLAZE_CPU(object_new_child(OBJECT(machine), "cpu[0]",
+                                          TYPE_MICROBLAZE_CPU));
     object_property_set_str(OBJECT(cpu), "version", "8.10.a", &error_abort);
     /* Use FPU but don't use floating point conversion and square
      * root instructions
hw/microblaze/petalogix_s3adsp1800_mmu.c +2 -1
index 83bf325b2b..82e486e716 100644
@@ -85,7 +85,8 @@ petalogix_s3adsp1800_init(MachineState *machine)
     MemoryRegion *sysmem = get_system_memory();
     EndianMode endianness = psms->endianness;
 
-    cpu = MICROBLAZE_CPU(object_new(TYPE_MICROBLAZE_CPU));
+    cpu = MICROBLAZE_CPU(object_new_child(OBJECT(machine), "cpu[0]",
+                                          TYPE_MICROBLAZE_CPU));
     object_property_set_str(OBJECT(cpu), "version", "7.10.d", &error_abort);
     object_property_set_bool(OBJECT(cpu), "little-endian",
                              endianness == ENDIAN_MODE_LITTLE, &error_abort);
Signature · 1 line
2.47.1
#287 lore raw

RFC PATCH v2 130/137 hw: Pair object_initialize_child() with plain realize()

In reply to Alexander Graf

A number of composite devices and boards create embedded children via object_initialize_child() (or object_new_with_props()) and then realize them via *_realize_and_unref(). The child<> property already holds the sole reference; the extra unref drops it and the object survives only because bus_add_child() takes a reference of its own. Use plain sysbus_realize()/qdev_realize() so the parent's child<> keeps the reference it is documented to hold.

AI-used-for: code (refactoring)

---
 hw/arm/imx8mm-evk.c          | 2 +-
 hw/arm/imx8mp-evk.c          | 2 +-
 hw/arm/mps2.c                | 2 +-
 hw/display/sm501.c           | 2 +-
 hw/m68k/q800.c               | 6 +++---
 hw/misc/macio/macio.c        | 2 +-
 hw/net/imx_fec.c             | 2 +-
 hw/net/lan9118.c             | 2 +-
 hw/nubus/nubus-virtio-mmio.c | 2 +-
 hw/pci-host/mv64361.c        | 2 +-
 hw/xen/xen-pvh-common.c      | 2 +-
 11 files changed, 13 insertions(+), 13 deletions(-)
hw/arm/imx8mm-evk.c +1 -1
index 72fa1930ae..84a1ae6aa4 100644
@@ -80,7 +80,7 @@ static void imx8mm_evk_init(MachineState *machine)
     s = FSL_IMX8MM(object_new_with_props(TYPE_FSL_IMX8MM, OBJECT(machine),
                                          "soc", &error_fatal, NULL));
     object_property_set_uint(OBJECT(s), "fec1-phy-num", 1, &error_fatal);
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(s), &error_fatal);
+    sysbus_realize(SYS_BUS_DEVICE(s), &error_fatal);
 
     memory_region_add_subregion(get_system_memory(), FSL_IMX8MM_RAM_START,
                                 machine->ram);
hw/arm/imx8mp-evk.c +1 -1
index 6116e6e9dc..776aa22b9a 100644
@@ -93,7 +93,7 @@ static void imx8mp_evk_init(MachineState *machine)
         object_property_set_link(OBJECT(&s->soc), bus_name,
                                  OBJECT(s->canbus[i]), &error_fatal);
     }
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->soc), &error_fatal);
+    sysbus_realize(SYS_BUS_DEVICE(&s->soc), &error_fatal);
 
     memory_region_add_subregion(get_system_memory(), FSL_IMX8MP_RAM_START,
                                 machine->ram);
hw/arm/mps2.c +1 -1
index 1032f47cb8..3de11b19a9 100644
@@ -379,7 +379,7 @@ static void mps2_common_init(MachineState *machine)
                                 TYPE_CMSDK_APB_TIMER);
         sbd = SYS_BUS_DEVICE(&mms->timer[i]);
         qdev_connect_clock_in(DEVICE(&mms->timer[i]), "pclk", mms->sysclk);
-        sysbus_realize_and_unref(sbd, &error_fatal);
+        sysbus_realize(sbd, &error_fatal);
         sysbus_mmio_map(sbd, 0, base);
         sysbus_connect_irq(sbd, 0, qdev_get_gpio_in(armv7m, irqno));
     }
hw/display/sm501.c +1 -1
index 56075a0303..4256ee0318 100644
@@ -2091,7 +2091,7 @@ static void sm501_realize_sysbus(DeviceState *dev, Error **errp)
     sysbus_init_mmio(sbd, &s->state.mmio_region);
 
     /* bridge to usb host emulation module */
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->ohci), &error_fatal);
+    sysbus_realize(SYS_BUS_DEVICE(&s->ohci), &error_fatal);
     memory_region_add_subregion(&s->state.mmio_region, SM501_USB_HOST,
                        sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->ohci), 0));
     sysbus_pass_irq(sbd, SYS_BUS_DEVICE(&s->ohci));
hw/m68k/q800.c +3 -3
index 677dc4ad80..9fc186737b 100644
@@ -324,14 +324,14 @@ static void q800_machine_init(MachineState *machine)
     object_initialize_child(OBJECT(machine), "djmemc", &m->djmemc,
                             TYPE_DJMEMC);
     sysbus = SYS_BUS_DEVICE(&m->djmemc);
-    sysbus_realize_and_unref(sysbus, &error_fatal);
+    sysbus_realize(sysbus, &error_fatal);
     memory_region_add_subregion(&m->macio, DJMEMC_BASE - IO_BASE,
                                 sysbus_mmio_get_region(sysbus, 0));
 
     /* IOSB subsystem */
     object_initialize_child(OBJECT(machine), "iosb", &m->iosb, TYPE_IOSB);
     sysbus = SYS_BUS_DEVICE(&m->iosb);
-    sysbus_realize_and_unref(sysbus, &error_fatal);
+    sysbus_realize(sysbus, &error_fatal);
     memory_region_add_subregion(&m->macio, IOSB_BASE - IO_BASE,
                                 sysbus_mmio_get_region(sysbus, 0));
 
@@ -497,7 +497,7 @@ static void q800_machine_init(MachineState *machine)
         qdev_prop_set_string(DEVICE(&m->asc), "audiodev", machine->audiodev);
     }
     sysbus = SYS_BUS_DEVICE(&m->asc);
-    sysbus_realize_and_unref(sysbus, &error_fatal);
+    sysbus_realize(sysbus, &error_fatal);
     memory_region_add_subregion(&m->macio, ASC_BASE - IO_BASE,
                                 sysbus_mmio_get_region(sysbus, 0));
     sysbus_connect_irq(sysbus, 0, qdev_get_gpio_in(DEVICE(&m->glue),
hw/misc/macio/macio.c +1 -1
index 2813705c03..a26babfbc2 100644
@@ -274,7 +274,7 @@ static void macio_newworld_realize(PCIDevice *d, Error **errp)
     /* OpenPIC */
     qdev_prop_set_uint32(pic_dev, "model", OPENPIC_MODEL_KEYLARGO);
     sbd = SYS_BUS_DEVICE(&ns->pic);
-    sysbus_realize_and_unref(sbd, &error_fatal);
+    sysbus_realize(sbd, &error_fatal);
     memory_region_add_subregion(&s->bar, 0x40000,
                                 sysbus_mmio_get_region(sbd, 0));
 
hw/net/imx_fec.c +1 -1
index 38e0f64462..fddb0bbba3 100644
@@ -1207,7 +1207,7 @@ static void imx_eth_realize(DeviceState *dev, Error **errp)
 
     qemu_init_irq_child(OBJECT(s), "mii-irq", &s->mii_irq, imx_phy_update_irq, s, 0);
     object_initialize_child(OBJECT(s), "mii", &s->mii, TYPE_LAN9118_PHY);
-    if (!sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->mii), errp)) {
+    if (!sysbus_realize(SYS_BUS_DEVICE(&s->mii), errp)) {
         return;
     }
     qdev_connect_gpio_out(DEVICE(&s->mii), 0, &s->mii_irq);
hw/net/lan9118.c +1 -1
index 2003280fc4..fa425bb5bb 100644
@@ -1276,7 +1276,7 @@ static void lan9118_realize(DeviceState *dev, Error **errp)
 
     qemu_init_irq_child(OBJECT(s), "mii-irq", &s->mii_irq, lan9118_update_irq, s, 0);
     object_initialize_child(OBJECT(s), "mii", &s->mii, TYPE_LAN9118_PHY);
-    if (!sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->mii), errp)) {
+    if (!sysbus_realize(SYS_BUS_DEVICE(&s->mii), errp)) {
         return;
     }
     qdev_connect_gpio_out(DEVICE(&s->mii), 0, &s->mii_irq);
hw/nubus/nubus-virtio-mmio.c +1 -1
index 63aeca5b12..29008ea6c2 100644
@@ -51,7 +51,7 @@ static void nubus_virtio_mmio_realize(DeviceState *dev, Error **errp)
     for (i = 0; i < NUBUS_VIRTIO_MMIO_NUM_DEVICES; i++) {
         sbd = SYS_BUS_DEVICE(&s->virtio_mmio[i]);
         qdev_prop_set_bit(DEVICE(sbd), "force-legacy", false);
-        if (!sysbus_realize_and_unref(sbd, errp)) {
+        if (!sysbus_realize(sbd, errp)) {
             return;
         }
 
hw/pci-host/mv64361.c +1 -1
index 3ef51fef1b..8c3dadd490 100644
@@ -874,7 +874,7 @@ static void mv64361_realize(DeviceState *dev, Error **errp)
                                 TYPE_MV64361_PCI);
         DeviceState *pci = DEVICE(&s->pci[i]);
         qdev_prop_set_uint8(pci, "index", i);
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(pci), &error_fatal);
+        sysbus_realize(SYS_BUS_DEVICE(pci), &error_fatal);
     }
     sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->cpu_irq);
     qdev_init_gpio_in_named(dev, mv64361_gpp_irq, "gpp", 32);
hw/xen/xen-pvh-common.c +1 -1
index 76893c8fcf..9ced68afea 100644
@@ -230,7 +230,7 @@ static inline void xenpvh_gpex_init(XenPVHMachineState *s,
     object_initialize_child(OBJECT(s), "gpex", &s->pci.gpex,
                             TYPE_GPEX_HOST);
     dev = DEVICE(&s->pci.gpex);
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+    sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
 
     ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
     memory_region_add_subregion(sysmem, s->cfg.pci_ecam.base, ecam_reg);
Signature · 1 line
2.47.1
#288 lore raw

RFC PATCH v2 131/137 hw: Parent board-created CPUs under the machine

In reply to Alexander Graf

A number of boards create CPUs via object_new(cpu_type) followed by property setting and qdev_realize(NULL), rather than through cpu_create(). These CPUs currently fall into /machine/unattached via the device_set_realized() fallback. Parent them explicitly under the machine (or the CPS/SoC container that owns them) as "cpu[*]", matching the naming convention used by cpu_create().

The subsequent object_unref() and qdev_realize_and_unref() calls are converted to plain qdev_realize() (or dropped) since the parent's child<> property now holds the reference. Error paths switch to object_unparent().

The transient introspection object_new() in hw/arm/virt.c machvirt_init() (probing pa_bits) is deliberately left as-is; it is unref'd immediately without being realized.

hw/arm: integratorcp mps3r realview sbsa-ref versatilepb
vexpress virt xilinx-zynq
hw/hexagon: hexagon_dsp virt
hw/i386: x86-common xen-pvh
hw/loongarch: virt

  hw/mips:      cps
  hw/ppc:       e500 spapr (cpu-core)
  hw/riscv:     cps microblaze-v-generic
  hw/s390x:     s390-virtio-ccw
  hw/sparc:     leon3 sun4m
  hw/sparc64:   sparc64

Hot-plugged CPUs (via -device / device_add) continue to be parented under /machine/peripheral by the user's id=; only cold-boot board-created CPUs move under /machine/cpu[*].

AI-used-for: code (refactoring)

---
 hw/arm/integratorcp.c           |  3 ++-
 hw/arm/mps3r.c                  |  4 ++--
 hw/arm/realview.c               |  3 ++-
 hw/arm/sbsa-ref.c               |  5 +++--
 hw/arm/versatilepb.c            |  3 ++-
 hw/arm/vexpress.c               |  3 ++-
 hw/arm/virt.c                   |  5 +++--
 hw/arm/xilinx_zynq.c            |  3 ++-
 hw/hexagon/hexagon_dsp.c        |  5 +++--
 hw/hexagon/virt.c               |  5 +++--
 hw/i386/x86-common.c            |  9 ++++-----
 hw/i386/xen/xen-pvh.c           |  5 ++---
 hw/loongarch/virt.c             |  6 ++++--
 hw/mips/cps.c                   |  5 +++--
 hw/ppc/e500.c                   |  5 +++--
 hw/ppc/spapr.c                  |  5 +++--
 hw/riscv/cps.c                  |  5 +++--
 hw/riscv/microblaze-v-generic.c |  3 ++-
 hw/s390x/s390-virtio-ccw.c      | 16 +++++++---------
 hw/sparc/leon3.c                |  3 ++-
 hw/sparc/sun4m.c                |  5 +++--
 hw/sparc64/sparc64.c            |  3 ++-
 22 files changed, 62 insertions(+), 47 deletions(-)
hw/arm/integratorcp.c +2 -1
index 81c0bce8f9..30a6a940c5 100644
@@ -598,7 +598,8 @@ static void integratorcp_init(MachineState *machine)
     DriveInfo *dinfo;
     int i;
 
-    cpuobj = object_new(machine->cpu_type);
+    cpuobj = object_new_child(OBJECT(machine), "cpu[0]",
+                                        machine->cpu_type);
 
     /* By default ARM1176 CPUs have EL3 enabled.  This board does not
      * currently support EL3 so the CPU EL3 property is disabled before
hw/arm/mps3r.c +2 -2
index d6c29c1172..dd434eaefd 100644
@@ -388,13 +388,13 @@ static void mps3r_common_init(MachineState *machine)
         memory_region_add_subregion_overlap(&mms->cpu_sysmem[i], 0,
                                             &mms->sysmem_alias[i], -1);
 
-        mms->cpu[i] = object_new(machine->cpu_type);
+        mms->cpu[i] = object_new_child(OBJECT(mms), "cpu[*]",
+                                        machine->cpu_type);
         object_property_set_link(mms->cpu[i], "memory",
                                  OBJECT(&mms->cpu_sysmem[i]), &error_abort);
         object_property_set_int(mms->cpu[i], "reset-cbar",
                                 PERIPHBASE, &error_abort);
         qdev_realize(DEVICE(mms->cpu[i]), NULL, &error_fatal);
-        object_unref(mms->cpu[i]);
 
         /* Per-CPU RAM */
         memory_region_init_ram(&mms->cpu_ram[i], OBJECT(mms), ramname,
hw/arm/realview.c +2 -1
index 99591e7d80..f938367131 100644
@@ -119,7 +119,8 @@ static void realview_init(MachineState *machine,
     }
 
     for (n = 0; n < smp_cpus; n++) {
-        Object *cpuobj = object_new(machine->cpu_type);
+        Object *cpuobj = object_new_child(OBJECT(machine), "cpu[*]",
+                                        machine->cpu_type);
 
         /* By default A9,A15 and ARM1176 CPUs have EL3 enabled.  This board
          * does not currently support EL3 so the CPU EL3 property is disabled
hw/arm/sbsa-ref.c +3 -2
index 1df8efd207..eeb55b5334 100644
@@ -783,7 +783,9 @@ static void sbsa_ref_init(MachineState *machine)
             break;
         }
 
-        cpuobj = object_new(possible_cpus->cpus[n].type);
+        g_autofree char *cpu_name = g_strdup_printf("cpu[%d]", n);
+        cpuobj = object_new_child(OBJECT(sms), cpu_name,
+                                  possible_cpus->cpus[n].type);
         object_property_set_int(cpuobj, "mp-affinity",
                                 possible_cpus->cpus[n].arch_id, NULL);
 
@@ -808,7 +810,6 @@ static void sbsa_ref_init(MachineState *machine)
                                  OBJECT(secure_sysmem), &error_abort);
 
         qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
-        object_unref(cpuobj);
     }
 
     memory_region_add_subregion(sysmem, sbsa_ref_memmap[SBSA_MEM].base,
hw/arm/versatilepb.c +2 -1
index f68c4765c6..8d93fff207 100644
@@ -208,7 +208,8 @@ static void versatile_init(MachineState *machine, int board_id)
         exit(1);
     }
 
-    cpuobj = object_new(machine->cpu_type);
+    cpuobj = object_new_child(OBJECT(machine), "cpu[0]",
+                                        machine->cpu_type);
 
     /* By default ARM1176 CPUs have EL3 enabled.  This board does not
      * currently support EL3 so the CPU EL3 property is disabled before
hw/arm/vexpress.c +2 -1
index f877ccf330..986525b9ce 100644
@@ -219,7 +219,8 @@ static void init_cpus(MachineState *ms, const char *cpu_type,
 
     /* Create the actual CPUs */
     for (n = 0; n < smp_cpus; n++) {
-        Object *cpuobj = object_new(cpu_type);
+        Object *cpuobj = object_new_child(qdev_get_machine(), "cpu[*]",
+                                        cpu_type);
 
         if (!secure) {
             object_property_set_bool(cpuobj, "has_el3", false, NULL);
hw/arm/virt.c +3 -2
index beb2d481ce..f4f2643b49 100644
@@ -3039,7 +3039,9 @@ static void machvirt_init(MachineState *machine)
             break;
         }
 
-        cpuobj = object_new(possible_cpus->cpus[n].type);
+        g_autofree char *cpu_name = g_strdup_printf("cpu[%d]", n);
+        cpuobj = object_new_child(OBJECT(vms), cpu_name,
+                                  possible_cpus->cpus[n].type);
         object_property_set_int(cpuobj, "mp-affinity",
                                 possible_cpus->cpus[n].arch_id, NULL);
 
@@ -3139,7 +3141,6 @@ static void machvirt_init(MachineState *machine)
         }
 
         qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
-        object_unref(cpuobj);
     }
 
     /* Now we've created the CPUs we can see if they have the hypvirt timer */
hw/arm/xilinx_zynq.c +2 -1
index 441289e63f..a2dcdd422d 100644
@@ -222,7 +222,8 @@ static void zynq_init(MachineState *machine)
     }
 
     for (n = 0; n < smp_cpus; n++) {
-        Object *cpuobj = object_new(machine->cpu_type);
+        Object *cpuobj = object_new_child(OBJECT(machine), "cpu[*]",
+                                        machine->cpu_type);
 
         object_property_set_int(cpuobj, "midr", ZYNQ_BOARD_MIDR,
                                 &error_fatal);
hw/hexagon/hexagon_dsp.c +3 -2
index 53dfe6741e..d78a987dca 100644
@@ -143,7 +143,8 @@ static void hexagon_common_init(MachineState *machine, Rev_t rev,
     sysbus_realize(SYS_BUS_DEVICE(tlb_dev), &error_fatal);
 
     for (int i = 0; i < machine->smp.cpus; i++) {
-        HexagonCPU *cpu = HEXAGON_CPU(object_new(machine->cpu_type));
+        HexagonCPU *cpu = HEXAGON_CPU(object_new_child(OBJECT(machine), "cpu[*]",
+                                        machine->cpu_type));
         qemu_register_reset(do_cpu_reset, cpu);
 
         /*
@@ -158,7 +159,7 @@ static void hexagon_common_init(MachineState *machine, Rev_t rev,
                                  OBJECT(glob_regs_dev), &error_fatal);
         object_property_set_link(OBJECT(cpu), "tlb",
                                  OBJECT(tlb_dev), &error_fatal);
-        qdev_realize_and_unref(DEVICE(cpu), NULL, &error_fatal);
+        qdev_realize(DEVICE(cpu), NULL, &error_fatal);
     }
 
     rom_add_blob_fixed_as("config_table.rom", &m_cfg->cfgtable,
hw/hexagon/virt.c +3 -2
index fb23cda19f..48f1e35dd3 100644
@@ -280,7 +280,8 @@ static void virt_init(MachineState *ms)
 
     cpu0 = NULL;
     for (int i = 0; i < ms->smp.cpus; i++) {
-        HexagonCPU *cpu = HEXAGON_CPU(object_new(ms->cpu_type));
+        HexagonCPU *cpu = HEXAGON_CPU(object_new_child(OBJECT(ms), "cpu[*]",
+                                        ms->cpu_type));
         qemu_register_reset(do_cpu_reset, cpu);
 
         if (i == 0) {
@@ -300,7 +301,7 @@ static void virt_init(MachineState *ms)
         object_property_set_link(OBJECT(cpu), "tlb",
                                  OBJECT(tlb_dev), &error_fatal);
 
-        qdev_realize_and_unref(DEVICE(cpu), NULL, &error_fatal);
+        qdev_realize(DEVICE(cpu), NULL, &error_fatal);
     }
     fdt_add_cpu_nodes(vms);
     clk_phandle = fdt_add_clocks(vms);
hw/i386/x86-common.c +4 -5
index 151a67ee5f..5bbfee737e 100644
@@ -55,15 +55,14 @@ static size_t pvh_start_addr;
 
 static void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **errp)
 {
-    Object *cpu = object_new(MACHINE(x86ms)->cpu_type);
+    Object *cpu = object_new_child(OBJECT(x86ms), "cpu[*]",
+                                        MACHINE(x86ms)->cpu_type);
 
     if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) {
-        goto out;
+        object_unparent(cpu);
+        return;
     }
     qdev_realize(DEVICE(cpu), NULL, errp);
-
-out:
-    object_unref(cpu);
 }
 
 void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
hw/i386/xen/xen-pvh.c +2 -3
index ab90c83a83..7aef48ad61 100644
@@ -29,12 +29,11 @@ struct XenPVHx86State {
 static DeviceState *xen_pvh_cpu_new(MachineState *ms,
                                     int64_t apic_id)
 {
-    Object *cpu = object_new(ms->cpu_type);
+    Object *cpu = object_new_child(OBJECT(ms), "cpu[*]",
+                                        ms->cpu_type);
 
-    object_property_add_child(OBJECT(ms), "cpu[*]", cpu);
     object_property_set_uint(cpu, "apic-id", apic_id, &error_fatal);
     qdev_realize(DEVICE(cpu), NULL, &error_fatal);
-    object_unref(cpu);
 
     return DEVICE(cpu);
 }
hw/loongarch/virt.c +4 -2
index 09c39140bd..8f0725f384 100644
@@ -948,13 +948,15 @@ static void virt_init(MachineState *machine)
     /* Init CPUs */
     mc->possible_cpu_arch_ids(machine);
     for (i = 0; i < machine->smp.cpus; i++) {
-        cpuobj = object_new(machine->cpu_type);
+        g_autofree char *cpu_name = g_strdup_printf("cpu[%d]", i);
+        cpuobj = object_new_child(OBJECT(machine), cpu_name,
+                                  machine->cpu_type);
         if (cpuobj == NULL) {
             error_report("Fail to create object with type %s ",
                          machine->cpu_type);
             exit(EXIT_FAILURE);
         }
-        qdev_realize_and_unref(DEVICE(cpuobj), NULL, &error_fatal);
+        qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
     }
     virt_check_dmsi(machine);
     fw_cfg_add_memory(machine);
hw/mips/cps.c +3 -2
index 620ee972f8..b997608373 100644
@@ -74,7 +74,8 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
     }
 
     for (int i = 0; i < s->num_vp; i++) {
-        MIPSCPU *cpu = MIPS_CPU(object_new(s->cpu_type));
+        MIPSCPU *cpu = MIPS_CPU(object_new_child(OBJECT(s), "cpu[*]",
+                                        s->cpu_type));
         CPUMIPSState *env = &cpu->env;
 
         object_property_set_bool(OBJECT(cpu), "big-endian", s->cpu_is_bigendian,
@@ -87,7 +88,7 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
         /* All cores use the same clock tree */
         qdev_connect_clock_in(DEVICE(cpu), "clk-in", s->clock);
 
-        if (!qdev_realize_and_unref(DEVICE(cpu), NULL, errp)) {
+        if (!qdev_realize(DEVICE(cpu), NULL, errp)) {
             return;
         }
 
hw/ppc/e500.c +3 -2
index f3917c0755..a0eb25e956 100644
@@ -941,7 +941,8 @@ void ppce500_init(MachineState *machine)
         PowerPCCPU *cpu;
         CPUState *cs;
 
-        cpu = POWERPC_CPU(object_new(machine->cpu_type));
+        cpu = POWERPC_CPU(object_new_child(OBJECT(machine), "cpu[*]",
+                                        machine->cpu_type));
         env = &cpu->env;
         cs = CPU(cpu);
 
@@ -956,7 +957,7 @@ void ppce500_init(MachineState *machine)
          */
         object_property_set_bool(OBJECT(cs), "start-powered-off", i != 0,
                                  &error_abort);
-        qdev_realize_and_unref(DEVICE(cs), NULL, &error_fatal);
+        qdev_realize(DEVICE(cs), NULL, &error_fatal);
 
         if (!firstenv) {
             firstenv = env;
hw/ppc/spapr.c +3 -2
index 9b6b5e7257..2719044414 100644
@@ -2776,7 +2776,9 @@ static void spapr_init_cpus(SpaprMachineState *spapr)
         }
 
         if (i < boot_cores_nr) {
-            Object *core  = object_new(type);
+            g_autofree char *core_name = g_strdup_printf("cpu-core[%d]", i);
+            Object *core  = object_new_child(OBJECT(spapr), core_name,
+                                             type);
             int nr_threads = smp_threads;
 
             /* Handle the partially filled core for older machine types */
@@ -2790,7 +2792,6 @@ static void spapr_init_cpus(SpaprMachineState *spapr)
                                     &error_fatal);
             qdev_realize(DEVICE(core), NULL, &error_fatal);
 
-            object_unref(core);
         }
     }
 }
hw/riscv/cps.c +3 -2
index b6b30e05e1..2bf506b61a 100644
@@ -63,13 +63,14 @@ static void riscv_cps_realize(DeviceState *dev, Error **errp)
     int harts_in_cluster = s->num_hart * s->num_core;
     int num_of_clusters = s->num_vp / harts_in_cluster;
     for (i = 0; i < s->num_vp; i++) {
-        cpu = RISCV_CPU(object_new(s->cpu_type));
+        cpu = RISCV_CPU(object_new_child(OBJECT(s), "cpu[*]",
+                                        s->cpu_type));
 
         /* All VPs are halted on reset. Leave powering up to CPC. */
         object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
                                  &error_abort);
 
-        if (!qdev_realize_and_unref(DEVICE(cpu), NULL, errp)) {
+        if (!qdev_realize(DEVICE(cpu), NULL, errp)) {
             return;
         }
 
hw/riscv/microblaze-v-generic.c +2 -1
index 0a83932405..f97ce402e6 100644
@@ -65,7 +65,8 @@ static void mb_v_generic_init(MachineState *machine)
     qemu_irq irq[32];
     MemoryRegion *sysmem = get_system_memory();
 
-    cpu = RISCV_CPU(object_new(machine->cpu_type));
+    cpu = RISCV_CPU(object_new_child(OBJECT(machine), "cpu[0]",
+                                        machine->cpu_type));
     object_property_set_bool(OBJECT(cpu), "h", false, NULL);
     object_property_set_bool(OBJECT(cpu), "d", false, NULL);
     qdev_realize(DEVICE(cpu), NULL, &error_abort);
hw/s390x/s390-virtio-ccw.c +7 -9
index 037cb6eb5e..0625d4a8df 100644
@@ -56,20 +56,18 @@ static Error *pv_mig_blocker;
 static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id,
                               Error **errp)
 {
-    S390CPU *cpu = S390_CPU(object_new(typename));
-    S390CPU *ret = NULL;
+    S390CPU *cpu = S390_CPU(object_new_child(qdev_get_machine(), "cpu[*]",
+                                        typename));
 
     if (!object_property_set_int(OBJECT(cpu), "core-id", core_id, errp)) {
-        goto out;
+        object_unparent(OBJECT(cpu));
+        return NULL;
     }
     if (!qdev_realize(DEVICE(cpu), NULL, errp)) {
-        goto out;
+        object_unparent(OBJECT(cpu));
+        return NULL;
     }
-    ret = cpu;
-
-out:
-    object_unref(OBJECT(cpu));
-    return ret;
+    return cpu;
 }
 
 static void s390_init_cpus(MachineState *machine)
hw/sparc/leon3.c +2 -1
index f9ef2758e1..a2781bcc32 100644
@@ -278,7 +278,8 @@ static void leon3_generic_hw_init(MachineState *machine)
 
     for (i = 0; i < machine->smp.cpus; i++) {
         /* Init CPU */
-        cpu = SPARC_CPU(object_new(machine->cpu_type));
+        cpu = SPARC_CPU(object_new_child(OBJECT(machine), "cpu[*]",
+                                        machine->cpu_type));
         qdev_init_gpio_in_named(DEVICE(cpu), leon3_start_cpu, "start_cpu", 1);
         qdev_init_gpio_in_named(DEVICE(cpu), leon3_set_pil_in, "pil", 1);
         qdev_realize(DEVICE(cpu), NULL, &error_fatal);
hw/sparc/sun4m.c +3 -2
index 51f5468a02..1ae9d834ca 100644
@@ -787,7 +787,8 @@ static DeviceState *cpu_devinit(const char *cpu_type, unsigned int id,
     CPUSPARCState *env;
     DeviceState *cpudev;
 
-    cpu = SPARC_CPU(object_new(cpu_type));
+    cpu = SPARC_CPU(object_new_child(qdev_get_machine(), "cpu[*]",
+                                        cpu_type));
     env = &cpu->env;
     cpudev = DEVICE(cpu);
 
@@ -795,7 +796,7 @@ static DeviceState *cpu_devinit(const char *cpu_type, unsigned int id,
     object_property_set_bool(OBJECT(cpu), "start-powered-off", id != 0,
                              &error_abort);
     qdev_init_gpio_in_named(cpudev, cpu_set_irq, "pil", MAX_PILS);
-    qdev_realize_and_unref(cpudev, NULL, &error_fatal);
+    qdev_realize(cpudev, NULL, &error_fatal);
     cpu_sparc_set_id(env, id);
     env->prom_addr = prom_addr;
     return cpudev;
hw/sparc64/sparc64.c +2 -1
index 16f4aa052b..9092c4193e 100644
@@ -272,7 +272,8 @@ SPARCCPU *sparc64_cpu_devinit(const char *cpu_type, uint64_t prom_addr)
     uint32_t  stick_frequency = 100 * 1000000;
     uint32_t hstick_frequency = 100 * 1000000;
 
-    cpu = SPARC_CPU(object_new(cpu_type));
+    cpu = SPARC_CPU(object_new_child(qdev_get_machine(), "cpu[0]",
+                                        cpu_type));
     qdev_init_gpio_in_named(DEVICE(cpu), sparc64_cpu_set_ivec_irq,
                             "ivec-irq", IVEC_MAX);
     qdev_realize(DEVICE(cpu), NULL, &error_fatal);
Signature · 1 line
2.47.1
#289 lore raw

RFC PATCH v2 132/137 hw/pci-host/versatile: Use object_initialize_child() for pci_dev

In reply to Alexander Graf

The embedded versatile_pci_host device was initialized via object_initialize() without ever being parented, so it landed in /machine/unattached at realize time. Switch to object_initialize_child() so it becomes a proper child<> of the host bridge.

AI-used-for: code (refactoring)

---
 hw/pci-host/versatile.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/pci-host/versatile.c +2 -1
index b27531f7fb..b749bcf1a1 100644
@@ -410,7 +410,8 @@ static void pci_vpb_realize(DeviceState *dev, Error **errp)
                       PCI_DEVFN(11, 0), TYPE_PCI_BUS);
     h->bus = &s->pci_bus;
 
-    object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_VERSATILE_PCI_HOST);
+    object_initialize_child(OBJECT(s), "pci-host", &s->pci_dev,
+                            TYPE_VERSATILE_PCI_HOST);
 
     for (i = 0; i < 4; i++) {
         sysbus_init_irq(sbd, &s->irq[i]);
Signature · 1 line
2.47.1
#290 lore raw

RFC PATCH v2 133/137 docs, qapi, scripts, iotests: Update /machine/unattached path references

In reply to Alexander Graf

Board-created devices no longer land in /machine/unattached and the next commit removes the container entirely. Update the in-tree consumers of the old paths:

scripts/arm_processor_error.py, scripts/qmp_helper.py
Search for CPUs directly under /machine.
docs/devel/s390-cpu-topology.rst, docs/system/cpu-hotplug.rst qapi/{block-core,block,machine,qom}.json
Refresh example qom-path strings.
tests/qemu-iotests/common.filter
_filter_qom_path() now normalizes any /machine/... device path so the reference outputs are stable across board naming changes.
tests/qemu-iotests/{172,186}.out
Regenerated against the new filter.

AI-used-for: code (refactoring)

---
 docs/devel/s390-cpu-topology.rst |  2 +-
 docs/system/cpu-hotplug.rst      |  4 +-
 qapi/block-core.json             | 12 +++---
 qapi/block.json                  |  2 +-
 qapi/machine.json                | 12 +++---
 qapi/qom.json                    |  4 +-
 scripts/arm_processor_error.py   |  3 +-
 scripts/qmp_helper.py            |  2 +-
 tests/qemu-iotests/172.out       | 72 ++++++++++++++++----------------
 tests/qemu-iotests/186.out       | 10 ++---
 tests/qemu-iotests/common.filter |  2 +-
 11 files changed, 62 insertions(+), 63 deletions(-)
docs/devel/s390-cpu-topology.rst +1 -1
index 48313b92d4..26be230969 100644
@@ -40,7 +40,7 @@ modifiers for all configured vCPUs.
       },
       "cpu-state": "operating",
       "entitlement": "medium",
-      "qom-path": "/machine/unattached/device[0]",
+      "qom-path": "/machine/cpu[0]",
       "cpu-index": 0,
       "target": "s390x"
     },
docs/system/cpu-hotplug.rst +2 -2
index cc50937c36..f838faba6f 100644
@@ -47,7 +47,7 @@ vCPU hotplug
                       "socket-id": 0,
                       "thread-id": 0
                   },
-                  "qom-path": "/machine/unattached/device[0]",
+                  "qom-path": "/machine/cpu[0]",
                   "type": "IvyBridge-IBRS-x86_64-cpu",
                   "vcpus-count": 1
               }
@@ -95,7 +95,7 @@ vCPU hotplug
                       "socket-id": 0,
                       "thread-id": 0
                   },
-                  "qom-path": "/machine/unattached/device[0]",
+                  "qom-path": "/machine/cpu[0]",
                   "target": "x86_64",
                   "thread-id": 28957
               },
qapi/block-core.json +6 -6
index 1f87b07850..28487f7cae 100644
@@ -928,7 +928,7 @@
 #                 "device":"ide1-cd0",
 #                 "locked":false,
 #                 "removable":true,
-#                 "qdev": "/machine/unattached/device[23]",
+#                 "qdev": "/machine/ide[1]/ide.1/cd0",
 #                 "tray_open": false,
 #                 "type":"unknown"
 #              },
@@ -936,7 +936,7 @@
 #                 "device":"floppy0",
 #                 "locked":false,
 #                 "removable":true,
-#                 "qdev": "/machine/unattached/device[20]",
+#                 "qdev": "/machine/fdc/floppy[0]",
 #                 "type":"unknown"
 #              },
 #              {
@@ -1310,7 +1310,7 @@
 #                    "account_invalid":true,
 #                    "account_failed":false
 #                 },
-#                 "qdev": "/machine/unattached/device[23]"
+#                 "qdev": "/machine/ide[0]/ide.0/hd0"
 #              },
 #              {
 #                 "device":"ide1-cd0",
@@ -1329,7 +1329,7 @@
 #                    "account_invalid":false,
 #                    "account_failed":false
 #                 },
-#                 "qdev": "/machine/unattached/device[24]"
+#                 "qdev": "/machine/ide[1]/ide.1/cd0"
 #              },
 #              {
 #                 "device":"floppy0",
@@ -1348,7 +1348,7 @@
 #                    "account_invalid":false,
 #                    "account_failed":false
 #                 },
-#                 "qdev": "/machine/unattached/device[16]"
+#                 "qdev": "/machine/fdc/floppy[0]"
 #              },
 #              {
 #                 "device":"sd0",
@@ -5750,7 +5750,7 @@
 # .. qmp-example::
 #
 #     <- { "event": "BLOCK_IO_ERROR",
-#          "data": { "qom-path": "/machine/unattached/device[0]",
+#          "data": { "qom-path": "/machine/ide[0]/ide.0/hd1",
 #                    "device": "ide0-hd1",
 #                    "node-name": "#block212",
 #                    "operation": "write",
qapi/block.json +1 -1
index 46955bbb3e..cb177180e7 100644
@@ -404,7 +404,7 @@
 #
 #     <- { "event": "DEVICE_TRAY_MOVED",
 #          "data": { "device": "ide1-cd0",
-#                    "id": "/machine/unattached/device[22]",
+#                    "id": "/machine/ide[1]/ide.1/cd0",
 #                    "tray-open": true
 #          },
 #          "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
qapi/machine.json +6 -6
index 9b2248038f..2d39d650ce 100644
@@ -121,7 +121,7 @@
 #                     "thread-id": 0,
 #                     "socket-id": 0
 #                 },
-#                 "qom-path": "/machine/unattached/device[0]",
+#                 "qom-path": "/machine/cpu[0]",
 #                 "target":"x86_64",
 #                 "cpu-index": 0
 #             },
@@ -132,7 +132,7 @@
 #                     "thread-id": 0,
 #                     "socket-id": 1
 #                 },
-#                 "qom-path": "/machine/unattached/device[2]",
+#                 "qom-path": "/machine/cpu[1]",
 #                 "target":"x86_64",
 #                 "cpu-index": 1
 #             }
@@ -1028,7 +1028,7 @@
 #          { "props": { "core-id": 8 }, "type": "POWER8-spapr-cpu-core",
 #            "vcpus-count": 1 },
 #          { "props": { "core-id": 0 }, "type": "POWER8-spapr-cpu-core",
-#            "vcpus-count": 1, "qom-path": "/machine/unattached/device[0]"}
+#            "vcpus-count": 1, "qom-path": "/machine/cpu[0]"}
 #        ]}
 #
 # .. qmp-example::
@@ -1043,7 +1043,7 @@
 #             "props": {"core-id": 0, "socket-id": 1, "thread-id": 0}
 #          },
 #          {
-#             "qom-path": "/machine/unattached/device[0]",
+#             "qom-path": "/machine/cpu[0]",
 #             "type": "qemu64-x86_64-cpu", "vcpus-count": 1,
 #             "props": {"core-id": 0, "socket-id": 0, "thread-id": 0}
 #          }
@@ -1062,7 +1062,7 @@
 #             "props": { "core-id": 1 }
 #          },
 #          {
-#             "qom-path": "/machine/unattached/device[0]",
+#             "qom-path": "/machine/cpu[0]",
 #             "type": "qemu-s390x-cpu", "vcpus-count": 1,
 #             "props": { "core-id": 0 }
 #          }
@@ -1617,7 +1617,7 @@
 #
 #     <- { "event": "MEMORY_DEVICE_SIZE_CHANGE",
 #          "data": { "id": "vm0", "size": 1073741824,
-#                    "qom-path": "/machine/unattached/device[2]" },
+#                    "qom-path": "/machine/peripheral/vm0" },
 #          "timestamp": { "seconds": 1588168529, "microseconds": 201316 } }
 ##
 { 'event': 'MEMORY_DEVICE_SIZE_CHANGE',
qapi/qom.json +2 -2
index d96cc5aa21..62f9fd5492 100644
@@ -135,7 +135,7 @@
 #    :title: Use absolute path
 #
 #     -> { "execute": "qom-get",
-#          "arguments": { "path": "/machine/unattached/device[0]",
+#          "arguments": { "path": "/machine/cpu[0]",
 #                         "property": "hotplugged" } }
 #     <- { "return": false }
 #
@@ -143,7 +143,7 @@
 #    :title: Use partial path
 #
 #     -> { "execute": "qom-get",
-#          "arguments": { "path": "unattached/sysbus",
+#          "arguments": { "path": "machine/sysbus",
 #                         "property": "type" } }
 #     <- { "return": "System" }
 ##
scripts/arm_processor_error.py +1 -2
index 73d069f070..2636837ce3 100644
@@ -244,8 +244,7 @@ def send_cper(self, args):
         qmp_cmd.set_args(args)
 
         is_cpu_type = re.compile(r"^([\w+]+\-)?arm\-cpu$")
-        cpus = qmp_cmd.search_qom("/machine/unattached/device",
-                                  "type", is_cpu_type)
+        cpus = qmp_cmd.search_qom("/machine/cpu", "type", is_cpu_type)
 
         cper = {}
         pei = {}
scripts/qmp_helper.py +1 -1
index 521612fb94..b7e9ad86b9 100755
@@ -601,7 +601,7 @@ def search_qom(self, path, prop, regex):
         """
         Return a list of devices that match path array like:
 
-            /machine/unattached/device
+            /machine/cpu
             /machine/peripheral-anon/device
             ...
         """
tests/qemu-iotests/172.out +36 -36

266 lines not on this page · raw message

tests/qemu-iotests/186.out +5 -5
index 01530040e5..a2fdb0a915 100644
@@ -404,7 +404,7 @@ Testing: -drive if=floppy
 QEMU X.Y.Z monitor - type 'help' for more information
 (qemu) info block
 floppy0: [not inserted]
-    Attached to:      /machine/unattached/device[N]
+    Attached to:      PATH
     Removable device: not locked, tray closed
 (qemu) quit
 
@@ -412,7 +412,7 @@ Testing: -drive if=floppy,driver=null-co,read-zeroes=on
 QEMU X.Y.Z monitor - type 'help' for more information
 (qemu) info block
 floppy0 (NODE_NAME): json:{"read-zeroes": "on", "driver": "null-co"} (null-co)
-    Attached to:      /machine/unattached/device[N]
+    Attached to:      PATH
     Removable device: not locked, tray closed
     Cache mode:       writeback
 (qemu) quit
@@ -421,7 +421,7 @@ Testing: -drive if=ide,driver=null-co,read-zeroes=on
 QEMU X.Y.Z monitor - type 'help' for more information
 (qemu) info block
 ide0-hd0 (NODE_NAME): json:{"read-zeroes": "on", "driver": "null-co"} (null-co)
-    Attached to:      /machine/unattached/device[N]
+    Attached to:      PATH
     Cache mode:       writeback
 (qemu) quit
 
@@ -429,7 +429,7 @@ Testing: -drive if=ide,media=cdrom
 QEMU X.Y.Z monitor - type 'help' for more information
 (qemu) info block
 ide0-cd0: [not inserted]
-    Attached to:      /machine/unattached/device[N]
+    Attached to:      PATH
     Removable device: not locked, tray closed
 (qemu) quit
 
@@ -437,7 +437,7 @@ Testing: -drive if=ide,driver=null-co,read-zeroes=on,media=cdrom
 QEMU X.Y.Z monitor - type 'help' for more information
 (qemu) info block
 ide0-cd0 (NODE_NAME): json:{"read-zeroes": "on", "driver": "null-co"} (null-co, read-only)
-    Attached to:      /machine/unattached/device[N]
+    Attached to:      PATH
     Removable device: not locked, tray closed
     Cache mode:       writeback
 (qemu) quit
tests/qemu-iotests/common.filter +1 -1
index ce4213cb97..9a5994a3a5 100644
@@ -37,7 +37,7 @@ _filter_generated_node_ids()
 
 _filter_qom_path()
 {
-    gsed -e '/Attached to:/s/\device[[0-9]\+\]/device[N]/g'
+    gsed -e '/Attached to:/s#/machine/.*#PATH#'
 }
 
 # replace occurrences of the actual TEST_DIR value with TEST_DIR
Signature · 1 line
2.47.1
#291 lore raw

RFC PATCH v2 134/137 qom: Delete /machine/unattached and error on unparented realize

In reply to Alexander Graf

With every board-created device, memory region, IRQ, and singleton now given an explicit QOM parent, the /machine/unattached container serves no legitimate purpose. Remove it and turn the device_set_realized() fallback into a hard error, so that any future orphan is caught at realize time rather than silently adopted into an anonymous bucket.

hw/core/qdev.c error out on !obj->parent instead of adopting
hw/core/qdev-user.c drop the fake unattached container
system/vl.c drop "unattached" from machine container list
hw/i386/pc.c search /machine recursively for the FDC instead
of the (unattached, peripheral, peripheral-anon) container triple
tests/unit/ parent test devices explicitly under /machine

After this commit, info qom-tree no longer shows /machine/unattached on any target/machine combination, and git grep '"unattached"' returns zero hits.

AI-used-for: code (refactoring)

---
 hw/core/qdev-user.c                 |  6 +-----
 hw/core/qdev.c                      | 20 ++++----------------
 hw/i386/pc.c                        | 11 +----------
 include/hw/core/qdev.h              |  2 +-
 system/vl.c                         |  1 -
 tests/unit/test-qdev-global-props.c | 17 +++++++----------
 tests/unit/test-qdev.c              | 12 ++++--------
 7 files changed, 18 insertions(+), 51 deletions(-)
hw/core/qdev-user.c +1 -5
index 051f8fbd02..592a8369a3 100644
@@ -11,9 +11,5 @@
 
 void qdev_create_fake_machine(void)
 {
-    Object *fake_machine_obj;
-
-    fake_machine_obj = object_property_add_new_container(object_get_root(),
-                                                         "machine");
-    object_property_add_new_container(fake_machine_obj, "unattached");
+    object_property_add_new_container(object_get_root(), "machine");
 }
hw/core/qdev.c +4 -16
index 9a68fb426e..df3ba02839 100644
@@ -486,8 +486,6 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
     BusState *bus;
     NamedClockList *ncl;
     Error *local_err = NULL;
-    bool unattached_parent = false;
-    static int unattached_count;
 
     if (dev->hotplugged && !dc->hotpluggable) {
         error_setg(errp, "Device '%s' does not support hotplugging",
@@ -501,12 +499,10 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
         }
 
         if (!obj->parent) {
-            gchar *name = g_strdup_printf("device[%d]", unattached_count++);
-
-            object_property_add_child(machine_get_container("unattached"),
-                                      name, obj);
-            unattached_parent = true;
-            g_free(name);
+            error_setg(errp, "device type '%s' has no QOM parent; "
+                       "use qdev_new() with a parent",
+                       object_get_typename(obj));
+            return;
         }
 
         hotplug_ctrl = qdev_get_hotplug_handler(dev);
@@ -634,14 +630,6 @@ post_realize_fail:
 
 fail:
     error_propagate(errp, local_err);
-    if (unattached_parent) {
-        /*
-         * Beware, this doesn't just revert
-         * object_property_add_child(), it also runs bus_remove()!
-         */
-        object_unparent(OBJECT(dev));
-        unattached_count--;
-    }
 }
 
 static bool device_get_hotpluggable(Object *obj, Error **errp)
hw/i386/pc.c +1 -10
index 398371ff6f..bab8ee453f 100644
@@ -368,24 +368,15 @@ static int check_fdc(Object *obj, void *opaque)
     return 0;
 }
 
-static const char * const fdc_container_path[] = {
-    "unattached", "peripheral", "peripheral-anon"
-};
-
 /*
  * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
  * and ACPI objects.
  */
 static ISADevice *pc_find_fdc0(void)
 {
-    int i;
-    Object *container;
     CheckFdcState state = { 0 };
 
-    for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
-        container = machine_get_container(fdc_container_path[i]);
-        object_child_foreach(container, check_fdc, &state);
-    }
+    object_child_foreach_recursive(qdev_get_machine(), check_fdc, &state);
 
     if (state.multiple) {
         warn_report("multiple floppy disk controllers with "
include/hw/core/qdev.h +1 -1
index 9220e9e3b1..eded1b715d 100644
@@ -462,7 +462,7 @@ static inline bool qdev_is_realized(DeviceState *dev)
  * initialization.
  * @dev must not be plugged into a bus already.
  * If @bus, plug @dev into @bus.  This takes a reference to @dev.
- * If @dev has no QOM parent, make one up, taking another reference.
+ * @dev must already have a QOM parent (see qdev_new()).
  *
  * If you created @dev via object_new() directly (holding a floating
  * reference), you probably want to use qdev_realize_and_unref() instead.
system/vl.c +0 -1
index b4434839df..e51399d389 100644
@@ -2192,7 +2192,6 @@ static void parse_memory_options(void)
 static void qemu_create_machine_containers(Object *machine)
 {
     static const char *const containers[] = {
-        "unattached",
         "peripheral",
         "peripheral-anon",
     };
tests/unit/test-qdev-global-props.c +7 -10
index 8ea362cbb9..a941774b9d 100644
@@ -78,25 +78,22 @@ static const TypeInfo subclass_type = {
  * inherit the global states of the parent process) will try to create qdev
  * and realize the device.
  *
- * Realization of such anonymous qdev (with no parent object) requires both
- * the machine object and its "unattached" container to be at least present.
+ * Realization requires that the device already has a QOM parent.
  */
 static void test_init_machine(void)
 {
     /* This is a fake machine - it doesn't need to be a machine object */
-    Object *machine = object_property_add_new_container(
+    object_property_add_new_container(
         object_get_root(), "machine");
 
-    /* This container must exist for anonymous qdevs to realize() */
-    object_property_add_new_container(machine, "unattached");
-}
+    }
 
 /* Test simple static property setting to default value */
 static void test_static_prop_subprocess(void)
 {
     MyType *mt;
 
-    mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
+    mt = STATIC_TYPE(object_new_child(qdev_get_machine(), "dev[*]", TYPE_STATIC_PROPS));
     qdev_realize(DEVICE(mt), NULL, &error_fatal);
 
     g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
@@ -131,7 +128,7 @@ static void test_static_globalprop_subprocess(void)
 
     register_global_properties(props);
 
-    mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
+    mt = STATIC_TYPE(object_new_child(qdev_get_machine(), "dev[*]", TYPE_STATIC_PROPS));
     qdev_realize(DEVICE(mt), NULL, &error_fatal);
 
     g_assert_cmpuint(mt->prop1, ==, 200);
@@ -249,7 +246,7 @@ static void test_dynamic_globalprop_subprocess(void)
 
     register_global_properties(props);
 
-    mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
+    mt = DYNAMIC_TYPE(object_new_child(qdev_get_machine(), "dev[*]", TYPE_DYNAMIC_PROPS));
     qdev_realize(DEVICE(mt), NULL, &error_fatal);
 
     g_assert_cmpuint(mt->prop1, ==, 101);
@@ -295,7 +292,7 @@ static void test_subclass_global_props(void)
 
     register_global_properties(props);
 
-    mt = STATIC_TYPE(object_new(TYPE_SUBCLASS));
+    mt = STATIC_TYPE(object_new_child(qdev_get_machine(), "dev[*]", TYPE_SUBCLASS));
     qdev_realize(DEVICE(mt), NULL, &error_fatal);
 
     g_assert_cmpuint(mt->prop1, ==, 102);
tests/unit/test-qdev.c +4 -8
index 20eae38e03..eafffb7175 100644
@@ -44,24 +44,21 @@ static const TypeInfo my_dev_type_info = {
 /*
  * Initialize a fake machine, being prepared for future tests.
  *
- * Realization of anonymous qdev (with no parent object) requires both
- * the machine object and its "unattached" container to be at least present.
+ * Realization requires that the device already has a QOM parent.
  */
 static void test_init_machine(void)
 {
     /* This is a fake machine - it doesn't need to be a machine object */
-    Object *machine = object_property_add_new_container(
+    object_property_add_new_container(
         object_get_root(), "machine");
 
-    /* This container must exist for anonymous qdevs to realize() */
-    object_property_add_new_container(machine, "unattached");
-}
+    }
 
 static void test_qdev_free_properties(void)
 {
     MyDev *mt;
 
-    mt = STATIC_TYPE(object_new(TYPE_MY_DEV));
+    mt = STATIC_TYPE(object_new_child(qdev_get_machine(), "dev[*]", TYPE_MY_DEV));
     object_set_props(OBJECT(mt), &error_fatal,
                      "string", "something",
                      "array-u32", "12,13",
@@ -75,7 +72,6 @@ static void test_qdev_free_properties(void)
     g_assert_cmpuint(mt->prop_array_u32[1], ==, 13);
 
     object_unparent(OBJECT(mt));
-    object_unref(mt);
 }
 
 
Signature · 1 line
2.47.1
#292 lore raw

RFC PATCH v2 135/137 tests/qtest/qom-test: Add smp-max init-time ceiling test

In reply to Alexander Graf

Add a slow-mode subtest that boots aarch64/virt and ppc64/pseries at their maximum -smp count and asserts that qtest_init() returns within 15 seconds. This guards against quadratic-time init regressions when many child objects are created under a single parent, such as the "[*]" auto-index property scan that made pseries take ~10 s to launch before qemu_extend_irqs() switched to explicit indices.

AI-used-for: code (refactoring)

---
 tests/qtest/qom-test.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
tests/qtest/qom-test.c +35 -0
index cf4c6b5add..0bfb3233ac 100644
@@ -238,8 +238,32 @@ static void test_qom_qtests(void)
     qtest_quit(qts);
 }
 
+/*
+ * Guard against O(n^2) init-time regressions when many child objects are
+ * created under one parent (e.g. board-created CPUs).  Launch the machine
+ * at its maximum SMP count and assert an upper bound on time-to-monitor.
+ * The bound is generous (15 s) so this only fires when init cost blows up
+ * quadratically, not on ordinary CI jitter.
+ */
+static void test_smp_max(gconstpointer data)
+{
+    const char *args = data;
+    gint64 t0, dt;
+    QTestState *qts;
+
+    t0 = g_get_monotonic_time();
+    qts = qtest_initf("%s", args);
+    dt = g_get_monotonic_time() - t0;
+
+    g_test_message("smp-max init time: %" G_GINT64_FORMAT " ms", dt / 1000);
+    g_assert_cmpint(dt, <, 15 * G_USEC_PER_SEC);
+
+    qtest_quit(qts);
+}
+
 int main(int argc, char **argv)
 {
+    const char *arch = qtest_get_arch();
     char *v_env = getenv("V");
 
     if (v_env) {
@@ -251,5 +275,16 @@ int main(int argc, char **argv)
     qtest_cb_for_every_machine(add_machine_test_case, g_test_quick());
     qtest_add_func("qom/qom-qtests", test_qom_qtests);
 
+    if (g_test_slow()) {
+        if (g_str_equal(arch, "aarch64")) {
+            qtest_add_data_func("qom/smp-max/virt",
+                                "-M virt,gic-version=max -smp 512",
+                                test_smp_max);
+        } else if (g_str_equal(arch, "ppc64")) {
+            qtest_add_data_func("qom/smp-max/pseries",
+                                "-M pseries -smp 512", test_smp_max);
+        }
+    }
+
     return g_test_run();
 }
Signature · 1 line
2.47.1
#293 lore raw

RFC PATCH v2 136/137 docs/devel/qom: Document the composition-tree parenting contract

In reply to Alexander Graf

Add a section to the QOM developer documentation explaining that every object must have a composition-tree parent before use, how to pick the parent (creator owns lifetime), how to name children, the reference-count contract for parented creators, and where the contract is enforced (device_set_realized, memory_region_do_init, qdev_connect_gpio_out_named).

AI-used-for: code (refactoring)

---
 docs/devel/qom.rst | 96 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
docs/devel/qom.rst +96 -0
index 935bb1004a..ffcb3333b5 100644
@@ -27,6 +27,102 @@ that make up a QEMU "machine". You can view this tree by running
 objects created by the machine itself as well those created due to
 user configuration.
 
+Composition-tree parenting contract
+====================================
+
+Every QOM object that participates in the running system must have a
+composition-tree parent before it is used.  The parent gives the object
+a stable, canonical path such as ``/machine/soc/uart[0]`` that is
+visible in ``info qom-tree`` and addressable via ``qom-get`` and
+``qom-set``.  Objects that reach ``realize`` (for devices), that are
+wired to another object (for IRQ lines), or that are given a name (for
+memory regions) without a parent are programming errors.
+
+Creating parented objects
+-------------------------
+
+The standard creation helpers all take the composition-tree parent and
+the child name as their first two arguments:
+
+.. code-block:: c
+
+    Object      *object_new_child(Object *parent, const char *id,
+                                  const char *typename);
+    DeviceState *qdev_new(Object *parent, const char *id,
+                          const char *typename);
+    DeviceState *sysbus_create_simple(Object *parent, const char *id,
+                                      const char *type, hwaddr, qemu_irq);
+    PCIDevice   *pci_new(Object *parent, const char *id, ...);
+    ISADevice   *isa_new(Object *parent, const char *id, ...);
+    /* i2c_slave_new(), ssi_create_peripheral(), usb_new(),
+       cpu_create() etc. follow the same pattern. */
+
+``object_new()`` remains available as the primitive allocator for
+transient introspection objects and for ``qdev_device_add()``, which
+parents the device via ``qdev_set_id()``.  All other callers should use
+one of the parented helpers above, or ``object_initialize_child()`` for
+objects that are embedded by value in the parent's instance struct.
+
+Choosing the parent
+-------------------
+
+The QOM parent is the object that owns the child's lifetime.  In
+practice this is the object whose code is creating the child:
+
+* Board code (``MachineClass.init``) parents onboard devices to the
+  machine: ``OBJECT(machine)``.
+* A composite device's ``DeviceClass.realize`` parents the sub-devices
+  it creates to itself: ``OBJECT(dev)``.
+* An SoC container likewise parents the devices it creates to the SoC
+  object.
+
+The bus a device is plugged into is *not* its composition-tree parent.
+A device may sit on a bus owned by ``B`` without being a child of
+``B``; the bus relationship is expressed through the ``parent_bus``
+link, not the ``child<>`` property.
+
+Choosing the child name
+-----------------------
+
+* A single instance takes a bare noun: ``"uart"``, ``"gic"``, ``"rtc"``.
+* A fixed number of identical instances take an explicit index:
+  ``"uart[0]"``, ``"uart[1]"``.
+* When the caller does not care about the index, ``"uart[*]"`` lets
+  QOM allocate the next free slot in the ``uart[]`` array.
+* Address-based names such as ``"eth@0x30000000"`` should only be used
+  when the address is genuinely the identity of the device.
+
+Child names share the parent's property namespace, so avoid names that
+collide with class-defined properties of the parent (in particular,
+machine class properties visible on ``/machine``).
+
+Reference-count contract
+------------------------
+
+``object_new_child()``, ``qdev_new()`` and the per-bus creators return
+with the object's single reference held by the parent's ``child<>``
+property.  The caller does *not* hold a reference of its own and must
+therefore pair creation with plain ``qdev_realize()`` rather than
+``qdev_realize_and_unref()``.  The object is finalized when the parent
+drops its ``child<>`` property, typically via ``object_unparent()``.
+
+Enforcement
+-----------
+
+The parenting contract is enforced at the points where an unparented
+object would first become visible or entangled with another object:
+
+* ``device_set_realized()`` fails with an ``Error`` if the device has
+  no composition-tree parent.
+* ``memory_region_do_init()`` asserts that a memory region given a
+  non-empty name also has an owner.
+* ``qdev_connect_gpio_out_named()`` asserts that the outbound IRQ
+  line being connected already has a composition-tree parent.
+
+There is no ``/machine/unattached`` fallback container; an object that
+would previously have landed there now triggers one of the errors
+above instead.
+
 Creating a QOM class
 ====================
 
Signature · 1 line
2.47.1
#294 lore raw

RFC PATCH v2 137/137 MAINTAINERS: Add scripts/coccinelle/qom-parent/ under QOM

In reply to Alexander Graf

The Coccinelle scripts used to perform the tree-wide *_orphan() renames are stored under scripts/coccinelle/qom-parent/. Add the directory to the QOM MAINTAINERS entry.

AI-used-for: code (refactoring)

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index ecb8cfdc41..1f0586e71c 100644
@@ -3639,6 +3639,7 @@ S: Supported
 F: docs/devel/qom.rst
 F: include/qom/
 F: qapi/qom.json
+F: scripts/coccinelle/qom-parent/
 F: scripts/coccinelle/qom-parent-type.cocci
 F: scripts/qom-cast-macro-clean-cocci-gen.py
 F: qom/
Signature · 1 line
2.47.1