On 12/7/26 00:34, Alexander Graf wrote:
Sysbus is by far the largest producer of orphaned onboard devices:
of the ~1180 unparented device creation sites in the tree, well over
half go through sysbus_create_simple(), sysbus_create_varargs(), or
qdev_new_orphan()+sysbus_realize_and_unref() open-coded.
Apply the same rename-and-reintroduce treatment as qdev_new():
mechanically rename the existing helpers to *_orphan() and
reintroduce the short names with a leading (parent, id, ...) pair
that plumbs down to the new qdev_new(). The parented versions call
sysbus_realize() (not _and_unref) since the child<> property already
holds the sole reference.
The mechanical rename part is generated by
spatch --sp-file \
scripts/coccinelle/qom-parent/sysbus-create-orphan.cocci \
--in-place --include-headers --dir .
or equivalently
git ls-files '*.[ch]' '*.[ch].inc' '*.rst' '*.py' | \
grep -v '^subprojects/' | \
xargs sed -i \
's/\bsysbus_create_simple\b/sysbus_create_simple_orphan/g;
s/\bsysbus_create_varargs\b/sysbus_create_varargs_orphan/g'
Assisted-by: Kiro
Signed-off-by: Alexander Graf <[email protected]>
---
hw/arm/allwinner-h3.c | 16 ++++-----
hw/arm/collie.c | 2 +-
hw/arm/exynos4210.c | 14 ++++----
hw/arm/fsl-imx6.c | 2 +-
hw/arm/integratorcp.c | 18 +++++-----
hw/arm/mps2.c | 6 ++--
hw/arm/musicpal.c | 18 +++++-----
hw/arm/npcm7xx.c | 2 +-
hw/arm/realview.c | 24 ++++++-------
hw/arm/sbsa-ref.c | 6 ++--
hw/arm/stellaris.c | 8 ++---
hw/arm/strongarm.c | 10 +++---
hw/arm/versatilepb.c | 28 +++++++--------
hw/arm/vexpress.c | 20 +++++------
hw/arm/virt.c | 8 ++---
hw/arm/xilinx_zynq.c | 10 +++---
hw/core/sysbus.c | 35 ++++++++++++++++++-
hw/i386/kvm/clock.c | 2 +-
hw/i386/kvm/xen_evtchn.c | 2 +-
hw/i386/kvm/xen_gnttab.c | 2 +-
hw/i386/kvm/xen_overlay.c | 2 +-
hw/i386/kvm/xen_primary_console.c | 2 +-
hw/i386/kvm/xen_xenstore.c | 2 +-
hw/i386/microvm.c | 2 +-
hw/intc/apic_common.c | 2 +-
hw/loongarch/virt.c | 2 +-
hw/m68k/next-cube.c | 4 +--
hw/m68k/virt.c | 2 +-
hw/mips/jazz.c | 2 +-
hw/mips/loongson3_virt.c | 2 +-
hw/or1k/virt.c | 2 +-
hw/ppc/amigaone.c | 2 +-
hw/ppc/e500.c | 2 +-
hw/ppc/pegasos.c | 4 +--
hw/ppc/ppc440_bamboo.c | 2 +-
hw/ppc/sam460ex.c | 8 ++---
hw/riscv/virt.c | 4 +--
hw/sparc/sun4m.c | 2 +-
hw/vmapple/vmapple.c | 4 +--
hw/xen/xen-pvh-common.c | 2 +-
include/hw/core/sysbus.h | 32 ++++++++++++++---
.../qom-parent/sysbus-create-orphan.cocci | 18 ++++++++++
42 files changed, 206 insertions(+), 131 deletions(-)
create mode 100644 scripts/coccinelle/qom-parent/sysbus-create-orphan.cocci
diff --git a/include/hw/core/sysbus.h b/include/hw/core/sysbus.h
index f3c4259d29..7737c959c5 100644
--- a/include/hw/core/sysbus.h
+++ b/include/hw/core/sysbus.h
@@ -91,15 +91,39 @@ bool sysbus_realize_and_unref(SysBusDevice *dev, Error
**errp);
/* Call func for every dynamically created sysbus device in the system */
void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque);
-/* Legacy helper function for creating devices. */
-DeviceState *sysbus_create_varargs(const char *name,
+/**
+ * sysbus_create_varargs: Create, parent and realize a sysbus device
+ * @parent: the QOM parent (usually the machine or containing device)
+ * @id: child<> property name
+ * @type: sysbus device type to create
+ * @addr: MMIO region 0 address, or -1 for none
+ * @...: NULL-terminated list of qemu_irq to connect
+ *
+ * Create a sysbus device via qdev_new(@parent, @id, @type), realize
+ * it, optionally map MMIO region 0 at @addr, and connect the given
+ * IRQs. The returned device is owned by @parent.
+ */
+DeviceState *sysbus_create_varargs(Object *parent, const char *id,
+ const char *type, hwaddr addr, ...);
+
+static inline DeviceState *sysbus_create_simple(Object *parent,
Both are legacy. Updating the doc:
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
+ const char *id,
+ const char *type,
+ hwaddr addr,
+ qemu_irq irq)
+{
+ return sysbus_create_varargs(parent, id, type, addr, irq, NULL);
+}
+
+/* Legacy helper function for creating unparented devices. */
+DeviceState *sysbus_create_varargs_orphan(const char *name,
hwaddr addr, ...);