diff options
author | Markus Armbruster <armbru@redhat.com> | 2020-05-05 17:29:24 +0200 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2020-05-15 07:08:14 +0200 |
commit | b69c3c21a5d11075d42100d5cfe0a736593fae6b (patch) | |
tree | b34284892cb90e420c8ec9cb909686664052dcde /hw/usb | |
parent | 40c2281cc3342573bd72895997b5cfaddee36ef2 (diff) |
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
Diffstat (limited to 'hw/usb')
-rw-r--r-- | hw/usb/bus.c | 12 | ||||
-rw-r--r-- | hw/usb/ccid-card-emulated.c | 2 | ||||
-rw-r--r-- | hw/usb/ccid.h | 2 | ||||
-rw-r--r-- | hw/usb/dev-audio.c | 2 | ||||
-rw-r--r-- | hw/usb/dev-hid.c | 2 | ||||
-rw-r--r-- | hw/usb/dev-hub.c | 2 | ||||
-rw-r--r-- | hw/usb/dev-network.c | 2 | ||||
-rw-r--r-- | hw/usb/dev-smartcard-reader.c | 11 | ||||
-rw-r--r-- | hw/usb/dev-uas.c | 2 | ||||
-rw-r--r-- | hw/usb/dev-wacom.c | 2 | ||||
-rw-r--r-- | hw/usb/hcd-ehci-pci.c | 2 | ||||
-rw-r--r-- | hw/usb/hcd-ehci.c | 2 | ||||
-rw-r--r-- | hw/usb/hcd-ehci.h | 2 | ||||
-rw-r--r-- | hw/usb/host-libusb.c | 2 | ||||
-rw-r--r-- | hw/usb/redirect.c | 2 |
15 files changed, 22 insertions, 27 deletions
diff --git a/hw/usb/bus.c b/hw/usb/bus.c index 686f492112..fa07df98a2 100644 --- a/hw/usb/bus.c +++ b/hw/usb/bus.c @@ -14,7 +14,7 @@ static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); static char *usb_get_dev_path(DeviceState *dev); static char *usb_get_fw_dev_path(DeviceState *qdev); -static void usb_qdev_unrealize(DeviceState *qdev, Error **errp); +static void usb_qdev_unrealize(DeviceState *qdev); static Property usb_props[] = { DEFINE_PROP_STRING("port", USBDevice, port_path), @@ -130,12 +130,12 @@ USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr) return NULL; } -static void usb_device_unrealize(USBDevice *dev, Error **errp) +static void usb_device_unrealize(USBDevice *dev) { USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); if (klass->unrealize) { - klass->unrealize(dev, errp); + klass->unrealize(dev); } } @@ -265,14 +265,14 @@ static void usb_qdev_realize(DeviceState *qdev, Error **errp) if (dev->auto_attach) { usb_device_attach(dev, &local_err); if (local_err) { - usb_qdev_unrealize(qdev, NULL); + usb_qdev_unrealize(qdev); error_propagate(errp, local_err); return; } } } -static void usb_qdev_unrealize(DeviceState *qdev, Error **errp) +static void usb_qdev_unrealize(DeviceState *qdev) { USBDevice *dev = USB_DEVICE(qdev); USBDescString *s, *next; @@ -286,7 +286,7 @@ static void usb_qdev_unrealize(DeviceState *qdev, Error **errp) if (dev->attached) { usb_device_detach(dev); } - usb_device_unrealize(dev, errp); + usb_device_unrealize(dev); if (dev->port) { usb_release_port(dev); } diff --git a/hw/usb/ccid-card-emulated.c b/hw/usb/ccid-card-emulated.c index 3083124556..7d6105ef34 100644 --- a/hw/usb/ccid-card-emulated.c +++ b/hw/usb/ccid-card-emulated.c @@ -562,7 +562,7 @@ out1: qemu_mutex_destroy(&card->event_list_mutex); } -static void emulated_unrealize(CCIDCardState *base, Error **errp) +static void emulated_unrealize(CCIDCardState *base) { EmulatedState *card = EMULATED_CCID_CARD(base); VEvent *vevent = vevent_new(VEVENT_LAST, NULL, NULL); diff --git a/hw/usb/ccid.h b/hw/usb/ccid.h index bb2fdbfff3..531bf28fb0 100644 --- a/hw/usb/ccid.h +++ b/hw/usb/ccid.h @@ -36,7 +36,7 @@ typedef struct CCIDCardClass { const uint8_t *apdu, uint32_t len); void (*realize)(CCIDCardState *card, Error **errp); - void (*unrealize)(CCIDCardState *card, Error **errp); + void (*unrealize)(CCIDCardState *card); } CCIDCardClass; /* diff --git a/hw/usb/dev-audio.c b/hw/usb/dev-audio.c index 6fa213fad5..1371c44f48 100644 --- a/hw/usb/dev-audio.c +++ b/hw/usb/dev-audio.c @@ -923,7 +923,7 @@ static void usb_audio_handle_data(USBDevice *dev, USBPacket *p) } } -static void usb_audio_unrealize(USBDevice *dev, Error **errp) +static void usb_audio_unrealize(USBDevice *dev) { USBAudioState *s = USB_AUDIO(dev); diff --git a/hw/usb/dev-hid.c b/hw/usb/dev-hid.c index 67ec8b69ec..89f63b698b 100644 --- a/hw/usb/dev-hid.c +++ b/hw/usb/dev-hid.c @@ -699,7 +699,7 @@ static void usb_hid_handle_data(USBDevice *dev, USBPacket *p) } } -static void usb_hid_unrealize(USBDevice *dev, Error **errp) +static void usb_hid_unrealize(USBDevice *dev) { USBHIDState *us = USB_HID(dev); diff --git a/hw/usb/dev-hub.c b/hw/usb/dev-hub.c index 49a573b346..5f19dd9fb5 100644 --- a/hw/usb/dev-hub.c +++ b/hw/usb/dev-hub.c @@ -565,7 +565,7 @@ static void usb_hub_handle_data(USBDevice *dev, USBPacket *p) } } -static void usb_hub_unrealize(USBDevice *dev, Error **errp) +static void usb_hub_unrealize(USBDevice *dev) { USBHubState *s = (USBHubState *)dev; int i; diff --git a/hw/usb/dev-network.c b/hw/usb/dev-network.c index 720744488b..c69756709b 100644 --- a/hw/usb/dev-network.c +++ b/hw/usb/dev-network.c @@ -1326,7 +1326,7 @@ static void usbnet_cleanup(NetClientState *nc) s->nic = NULL; } -static void usb_net_unrealize(USBDevice *dev, Error **errp) +static void usb_net_unrealize(USBDevice *dev) { USBNetState *s = (USBNetState *) dev; diff --git a/hw/usb/dev-smartcard-reader.c b/hw/usb/dev-smartcard-reader.c index ef72738ced..ada18c1983 100644 --- a/hw/usb/dev-smartcard-reader.c +++ b/hw/usb/dev-smartcard-reader.c @@ -1146,7 +1146,7 @@ static void ccid_handle_data(USBDevice *dev, USBPacket *p) } } -static void ccid_unrealize(USBDevice *dev, Error **errp) +static void ccid_unrealize(USBDevice *dev) { USBCCIDState *s = USB_CCID_DEV(dev); @@ -1269,23 +1269,18 @@ void ccid_card_card_inserted(CCIDCardState *card) ccid_on_slot_change(s, true); } -static void ccid_card_unrealize(DeviceState *qdev, Error **errp) +static void ccid_card_unrealize(DeviceState *qdev) { CCIDCardState *card = CCID_CARD(qdev); CCIDCardClass *cc = CCID_CARD_GET_CLASS(card); USBDevice *dev = USB_DEVICE(qdev->parent_bus->parent); USBCCIDState *s = USB_CCID_DEV(dev); - Error *local_err = NULL; if (ccid_card_inserted(s)) { ccid_card_card_removed(card); } if (cc->unrealize) { - cc->unrealize(card, &local_err); - if (local_err != NULL) { - error_propagate(errp, local_err); - return; - } + cc->unrealize(card); } s->card = NULL; } diff --git a/hw/usb/dev-uas.c b/hw/usb/dev-uas.c index 11a8684cc2..a3a4d41c07 100644 --- a/hw/usb/dev-uas.c +++ b/hw/usb/dev-uas.c @@ -894,7 +894,7 @@ static void usb_uas_handle_data(USBDevice *dev, USBPacket *p) } } -static void usb_uas_unrealize(USBDevice *dev, Error **errp) +static void usb_uas_unrealize(USBDevice *dev) { UASDevice *uas = USB_UAS(dev); diff --git a/hw/usb/dev-wacom.c b/hw/usb/dev-wacom.c index 8ed57b3b44..8aba44b8bc 100644 --- a/hw/usb/dev-wacom.c +++ b/hw/usb/dev-wacom.c @@ -331,7 +331,7 @@ static void usb_wacom_handle_data(USBDevice *dev, USBPacket *p) } } -static void usb_wacom_unrealize(USBDevice *dev, Error **errp) +static void usb_wacom_unrealize(USBDevice *dev) { USBWacomState *s = (USBWacomState *) dev; diff --git a/hw/usb/hcd-ehci-pci.c b/hw/usb/hcd-ehci-pci.c index fc73a054c6..4c37c8e227 100644 --- a/hw/usb/hcd-ehci-pci.c +++ b/hw/usb/hcd-ehci-pci.c @@ -105,7 +105,7 @@ static void usb_ehci_pci_exit(PCIDevice *dev) EHCIPCIState *i = PCI_EHCI(dev); EHCIState *s = &i->ehci; - usb_ehci_unrealize(s, DEVICE(dev), NULL); + usb_ehci_unrealize(s, DEVICE(dev)); g_free(s->irq); s->irq = NULL; diff --git a/hw/usb/hcd-ehci.c b/hw/usb/hcd-ehci.c index 29d49c2d7e..1495e8f7fa 100644 --- a/hw/usb/hcd-ehci.c +++ b/hw/usb/hcd-ehci.c @@ -2522,7 +2522,7 @@ void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp) s->vmstate = qemu_add_vm_change_state_handler(usb_ehci_vm_state_change, s); } -void usb_ehci_unrealize(EHCIState *s, DeviceState *dev, Error **errp) +void usb_ehci_unrealize(EHCIState *s, DeviceState *dev) { trace_usb_ehci_unrealize(); diff --git a/hw/usb/hcd-ehci.h b/hw/usb/hcd-ehci.h index edb59311c4..57b38cfc05 100644 --- a/hw/usb/hcd-ehci.h +++ b/hw/usb/hcd-ehci.h @@ -324,7 +324,7 @@ extern const VMStateDescription vmstate_ehci; void usb_ehci_init(EHCIState *s, DeviceState *dev); void usb_ehci_finalize(EHCIState *s); void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp); -void usb_ehci_unrealize(EHCIState *s, DeviceState *dev, Error **errp); +void usb_ehci_unrealize(EHCIState *s, DeviceState *dev); void ehci_reset(void *opaque); #define TYPE_PCI_EHCI "pci-ehci-usb" diff --git a/hw/usb/host-libusb.c b/hw/usb/host-libusb.c index 78a5c2ba55..e28441379d 100644 --- a/hw/usb/host-libusb.c +++ b/hw/usb/host-libusb.c @@ -1104,7 +1104,7 @@ static void usb_host_instance_init(Object *obj) &udev->qdev); } -static void usb_host_unrealize(USBDevice *udev, Error **errp) +static void usb_host_unrealize(USBDevice *udev) { USBHostDevice *s = USB_HOST_DEVICE(udev); diff --git a/hw/usb/redirect.c b/hw/usb/redirect.c index e9a97feaed..417a60a2e6 100644 --- a/hw/usb/redirect.c +++ b/hw/usb/redirect.c @@ -1468,7 +1468,7 @@ static void usbredir_cleanup_device_queues(USBRedirDevice *dev) } } -static void usbredir_unrealize(USBDevice *udev, Error **errp) +static void usbredir_unrealize(USBDevice *udev) { USBRedirDevice *dev = USB_REDIRECT(udev); |