From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Peter Xu" <peterx@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>
Subject: [PATCH v3 1/9] hw: eliminate qdev_try_new, isa_try_new & usb_try_new
Date: Fri, 15 Nov 2024 17:25:13 +0000 [thread overview]
Message-ID: <20241115172521.504102-2-berrange@redhat.com> (raw)
In-Reply-To: <20241115172521.504102-1-berrange@redhat.com>
These functions all return NULL rather than asserting, if the requested
type is not registered and also cannot be dynamically loaded.
In several cases their usage is pointless, since the caller then just
reports an error & exits anyway. Easier to just let qdev_new fail
with &error_fatal.
In other cases, the desired semantics are clearer to understand if the
caller directly checks module_object_class_by_name(), before calling
the regular qdev_new (or specialized equiv) function.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
hw/core/qdev.c | 9 ---------
hw/i386/pc.c | 22 ++++++++++------------
hw/isa/isa-bus.c | 5 -----
hw/s390x/s390-pci-bus.c | 6 +-----
hw/usb/bus.c | 6 ++----
include/hw/isa/isa.h | 1 -
include/hw/net/ne2000-isa.h | 16 ++++++----------
include/hw/qdev-core.h | 12 ------------
include/hw/usb.h | 5 -----
9 files changed, 19 insertions(+), 63 deletions(-)
diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 5f13111b77..960a704a96 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -149,15 +149,6 @@ DeviceState *qdev_new(const char *name)
return DEVICE(object_new(name));
}
-DeviceState *qdev_try_new(const char *name)
-{
- ObjectClass *oc = module_object_class_by_name(name);
- if (!oc) {
- return NULL;
- }
- return DEVICE(object_new_with_class(oc));
-}
-
static QTAILQ_HEAD(, DeviceListener) device_listeners
= QTAILQ_HEAD_INITIALIZER(device_listeners);
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index f9147fecbd..d668970bee 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -596,9 +596,11 @@ static gboolean pc_init_ne2k_isa(ISABus *bus, NICInfo *nd, Error **errp)
"maximum number of ISA NE2000 devices exceeded");
return false;
}
- isa_ne2000_init(bus, ne2000_io[nb_ne2k],
- ne2000_irq[nb_ne2k], nd);
- nb_ne2k++;
+ if (module_object_class_by_name(TYPE_ISA_NE2000)) {
+ isa_ne2000_init(bus, ne2000_io[nb_ne2k],
+ ne2000_irq[nb_ne2k], nd);
+ nb_ne2k++;
+ }
return true;
}
@@ -1087,7 +1089,7 @@ static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl,
int i;
DriveInfo *fd[MAX_FD];
qemu_irq *a20_line;
- ISADevice *i8042, *port92, *vmmouse;
+ ISADevice *i8042, *port92, *vmmouse = NULL;
serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
@@ -1117,9 +1119,9 @@ static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl,
i8042 = isa_create_simple(isa_bus, TYPE_I8042);
if (!no_vmport) {
isa_create_simple(isa_bus, TYPE_VMPORT);
- vmmouse = isa_try_new("vmmouse");
- } else {
- vmmouse = NULL;
+ if (module_object_class_by_name("vmmouse")) {
+ vmmouse = isa_new("vmmouse");
+ }
}
if (vmmouse) {
object_property_set_link(OBJECT(vmmouse), TYPE_I8042, OBJECT(i8042),
@@ -1163,11 +1165,7 @@ void pc_basic_device_init(struct PCMachineState *pcms,
if (pcms->hpet_enabled) {
qemu_irq rtc_irq;
- hpet = qdev_try_new(TYPE_HPET);
- if (!hpet) {
- error_report("couldn't create HPET device");
- exit(1);
- }
+ hpet = qdev_new(TYPE_HPET);
/*
* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-*,
* use IRQ16~23, IRQ8 and IRQ2. If the user has already set
diff --git a/hw/isa/isa-bus.c b/hw/isa/isa-bus.c
index f1e0f14007..8aaf44a3ef 100644
--- a/hw/isa/isa-bus.c
+++ b/hw/isa/isa-bus.c
@@ -158,11 +158,6 @@ ISADevice *isa_new(const char *name)
return ISA_DEVICE(qdev_new(name));
}
-ISADevice *isa_try_new(const char *name)
-{
- return ISA_DEVICE(qdev_try_new(name));
-}
-
ISADevice *isa_create_simple(ISABus *bus, const char *name)
{
ISADevice *dev;
diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c
index 40b2567aa7..558f17d3ba 100644
--- a/hw/s390x/s390-pci-bus.c
+++ b/hw/s390x/s390-pci-bus.c
@@ -922,11 +922,7 @@ static S390PCIBusDevice *s390_pci_device_new(S390pciState *s,
Error *local_err = NULL;
DeviceState *dev;
- dev = qdev_try_new(TYPE_S390_PCI_DEVICE);
- if (!dev) {
- error_setg(errp, "zPCI device could not be created");
- return NULL;
- }
+ dev = qdev_new(TYPE_S390_PCI_DEVICE);
if (!object_property_set_str(OBJECT(dev), "target", target, &local_err)) {
object_unparent(OBJECT(dev));
diff --git a/hw/usb/bus.c b/hw/usb/bus.c
index bfab2807d7..002f7737e4 100644
--- a/hw/usb/bus.c
+++ b/hw/usb/bus.c
@@ -394,7 +394,6 @@ void usb_claim_port(USBDevice *dev, Error **errp)
{
USBBus *bus = usb_bus_from_device(dev);
USBPort *port;
- USBDevice *hub;
assert(dev->port == NULL);
@@ -412,9 +411,8 @@ void usb_claim_port(USBDevice *dev, Error **errp)
} else {
if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
/* Create a new hub and chain it on */
- hub = usb_try_new("usb-hub");
- if (hub) {
- usb_realize_and_unref(hub, bus, NULL);
+ if (module_object_class_by_name("usb-hub")) {
+ usb_realize_and_unref(usb_new("usb-hub"), bus, NULL);
}
}
if (bus->nfree == 0) {
diff --git a/include/hw/isa/isa.h b/include/hw/isa/isa.h
index 40d6224a4e..8475120849 100644
--- a/include/hw/isa/isa.h
+++ b/include/hw/isa/isa.h
@@ -81,7 +81,6 @@ IsaDma *isa_bus_get_dma(ISABus *bus, int nchan);
*/
qemu_irq isa_bus_get_irq(ISABus *bus, unsigned irqnum);
ISADevice *isa_new(const char *name);
-ISADevice *isa_try_new(const char *name);
bool isa_realize_and_unref(ISADevice *dev, ISABus *bus, Error **errp);
ISADevice *isa_create_simple(ISABus *bus, const char *name);
diff --git a/include/hw/net/ne2000-isa.h b/include/hw/net/ne2000-isa.h
index 73bae10ad1..aeb3e2bfd5 100644
--- a/include/hw/net/ne2000-isa.h
+++ b/include/hw/net/ne2000-isa.h
@@ -20,17 +20,13 @@
static inline ISADevice *isa_ne2000_init(ISABus *bus, int base, int irq,
NICInfo *nd)
{
- ISADevice *d;
+ ISADevice *d = isa_new(TYPE_ISA_NE2000);
+ DeviceState *dev = DEVICE(d);
- d = isa_try_new(TYPE_ISA_NE2000);
- if (d) {
- DeviceState *dev = DEVICE(d);
-
- qdev_prop_set_uint32(dev, "iobase", base);
- qdev_prop_set_uint32(dev, "irq", irq);
- qdev_set_nic_properties(dev, nd);
- isa_realize_and_unref(d, bus, &error_fatal);
- }
+ qdev_prop_set_uint32(dev, "iobase", base);
+ qdev_prop_set_uint32(dev, "irq", irq);
+ qdev_set_nic_properties(dev, nd);
+ isa_realize_and_unref(d, bus, &error_fatal);
return d;
}
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 5be9844412..020417d027 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -443,18 +443,6 @@ compat_props_add(GPtrArray *arr,
*/
DeviceState *qdev_new(const char *name);
-/**
- * qdev_try_new: Try to create a device on the heap
- * @name: device type to create
- *
- * This is like qdev_new(), except it returns %NULL when type @name
- * does not exist, rather than asserting.
- *
- * Return: a derived DeviceState object with a reference count of 1 or
- * NULL if type @name does not exist.
- */
-DeviceState *qdev_try_new(const char *name);
-
/**
* qdev_is_realized() - check if device is realized
* @dev: The device to check.
diff --git a/include/hw/usb.h b/include/hw/usb.h
index d46d96779a..bb778cb844 100644
--- a/include/hw/usb.h
+++ b/include/hw/usb.h
@@ -584,11 +584,6 @@ static inline USBDevice *usb_new(const char *name)
return USB_DEVICE(qdev_new(name));
}
-static inline USBDevice *usb_try_new(const char *name)
-{
- return USB_DEVICE(qdev_try_new(name));
-}
-
static inline bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Error **errp)
{
return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp);
--
2.46.0
next prev parent reply other threads:[~2024-11-15 17:27 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-15 17:25 [PATCH v3 0/9] Require error handling for dynamically created objects Daniel P. Berrangé
2024-11-15 17:25 ` Daniel P. Berrangé [this message]
2024-11-15 17:54 ` [PATCH v3 1/9] hw: eliminate qdev_try_new, isa_try_new & usb_try_new Peter Xu
2024-11-15 18:34 ` Daniel P. Berrangé
2024-12-03 15:30 ` Markus Armbruster
2024-12-05 16:21 ` Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 2/9] qom: refactor checking abstract property when creating instances Daniel P. Berrangé
2024-11-15 17:54 ` Peter Xu
2024-11-15 17:25 ` [PATCH v3 3/9] qom: allow failure of object_new_with_class Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 4/9] qom: introduce object_new_dynamic() Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 5/9] convert code to object_new_dynamic() where appropriate Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 6/9] qom: enforce use of static, const string with object_new() Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 7/9] qom: introduce qdev_new_dynamic() Daniel P. Berrangé
2024-11-15 17:55 ` Peter Xu
2024-11-15 17:25 ` [PATCH v3 8/9] convert code to qdev_new_dynamic() where appropriate Daniel P. Berrangé
2024-11-15 17:25 ` [PATCH v3 9/9] hw: enforce use of static, const string with qdev_new() Daniel P. Berrangé
2024-11-15 17:55 ` Peter Xu
2024-12-04 11:07 ` [PATCH v3 0/9] Require error handling for dynamically created objects Markus Armbruster
2024-12-05 16:04 ` Daniel P. Berrangé
2024-12-06 8:25 ` Markus Armbruster
2024-12-06 10:57 ` Daniel P. Berrangé
2024-12-07 7:37 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241115172521.504102-2-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).