qemu/include/hw
Markus Armbruster b69c3c21a5 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>
2020-05-15 07:08:14 +02:00
..
acpi Drop more @errp parameters after previous commit 2020-05-15 07:08:14 +02:00
adc include: Make headers more self-contained 2019-08-16 13:31:51 +02:00
arm hw/arm/virt: Introduce a RAS machine option 2020-05-14 15:03:09 +01:00
audio Include hw/qdev-properties.h less 2019-08-16 13:31:53 +02:00
block block: Support providing LCHS from user 2019-10-31 11:47:11 -04:00
char hw/char/cadence_uart: add clock support 2020-04-30 15:35:41 +01:00
core exec: Add block comments for watchpoint routines 2020-05-11 11:14:02 +01:00
cpu Include hw/qdev-properties.h less 2019-08-16 13:31:53 +02:00
cris etraxfs: remove PROP_PTR usage 2020-01-07 17:24:29 +04:00
display hw/arm/bcm283x: Correct the license text 2020-03-23 17:22:30 +00:00
dma hw/arm/bcm283x: Correct the license text 2020-03-23 17:22:30 +00:00
firmware machine: Refactor smp-related call chains to pass MachineState 2019-07-05 17:07:36 -03:00
gpio nrf51: Fix last GPIO CNF address 2020-04-30 11:52:27 +01:00
hyperv
i2c hw/arm/nrf51: Add NRF51_PERIPHERAL_SIZE definition 2020-05-11 11:05:11 +01:00
i386 hw: add compat machines for 5.1 2020-05-06 10:12:16 -04:00
ide hw/ide: Move MAX_IDE_DEVS define to hw/ide/internal.h 2020-03-17 12:22:36 -04:00
input hppa: add emulation of LASI PS2 controllers 2020-01-27 10:49:51 -08:00
intc hw/arm/bcm283x: Correct the license text 2020-03-23 17:22:30 +00:00
ipack Include hw/qdev-properties.h less 2019-08-16 13:31:53 +02:00
ipmi ipmi: Add support to customize OEM functions 2019-12-17 10:39:47 +11:00
isa acpi: add ISADeviceClass->build_aml() 2020-05-04 10:25:02 -04:00
kvm
lm32
m68k m68k: Add NeXTcube machine 2019-09-07 08:31:51 +02:00
mem nvdimm: Use configurable ACPI IO base and size 2020-05-04 10:25:02 -04:00
mips Include hw/irq.h a lot less 2019-08-16 13:31:52 +02:00
misc aspeed: Support AST2600A1 silicon revision 2020-05-11 11:00:26 +01:00
net hw/net: Add Smartfusion2 emac block 2020-04-30 11:52:28 +01:00
nubus hw/m68k: add Nubus support 2019-10-28 19:06:47 +01:00
nvram fw_cfg: Migrate ACPI table mr sizes separately 2020-04-13 06:55:54 -04:00
pci pcie_root_port: Add hotplug disabling option 2020-03-08 09:18:29 -04:00
pci-bridge
pci-host hw/pci-host/q35: Remove unused includes 2020-03-09 15:59:31 +01:00
ppc Drop more @errp parameters after previous commit 2020-05-15 07:08:14 +02:00
rdma
riscv hw/riscv: Add optional symbol callback ptr to riscv_load_firmware() 2020-04-29 13:16:38 -07:00
rtc rtc: add RTC_ISA_BASE 2020-05-04 10:25:02 -04:00
s390x qdev: Unrealize must not fail 2020-05-15 07:08:14 +02:00
scsi qdev: Unrealize must not fail 2020-05-15 07:08:14 +02:00
sd hw/arm/allwinner: add SD/MMC host controller 2020-03-12 16:27:33 +00:00
semihosting semihosting: add qemu_semihosting_console_inc for SYS_READC 2020-01-09 11:41:29 +00:00
sh4 Include hw/irq.h a lot less 2019-08-16 13:31:52 +02:00
southbridge hw/ide: Do ide_drive_get() within pci_ide_create_devs() 2020-03-17 12:22:36 -04:00
sparc Include hw/qdev-properties.h less 2019-08-16 13:31:53 +02:00
ssi aspeed/smc: Add AST2600 timings registers 2019-12-16 10:46:34 +00:00
timer hw/timer/nrf51_timer: Display timer ID in trace events 2020-05-11 11:05:52 +01:00
tricore Include hw/irq.h a lot less 2019-08-16 13:31:52 +02:00
unicore32
usb hw/usb: Add basic i.MX USB Phy support 2020-03-17 11:23:14 +00:00
vfio vfio: Turn the container error into an Error handle 2019-10-04 18:49:18 +02:00
virtio fix vhost_user_blk_watch crash 2020-03-29 09:52:13 -04:00
watchdog watchdog/aspeed: Fix AST2600 frequency behaviour 2019-12-16 10:46:34 +00:00
xen qdev: Unrealize must not fail 2020-05-15 07:08:14 +02:00
xtensa Include hw/irq.h a lot less 2019-08-16 13:31:52 +02:00
boards.h hw: add compat machines for 5.1 2020-05-06 10:12:16 -04:00
clock.h hw/core/clock-vmstate: define a vmstate entry for clock state 2020-04-30 15:35:40 +01:00
elf_ops.h elf_ops: Don't try to g_mapped_file_unref(NULL) 2020-05-04 14:35:23 +02:00
empty_slot.h include: Make headers more self-contained 2019-08-16 13:31:51 +02:00
fw-path-provider.h
hotplug.h
hw.h Include hw/hw.h exactly where needed 2019-08-16 13:31:52 +02:00
ide.h hw/ide: Move MAX_IDE_DEVS define to hw/ide/internal.h 2020-03-17 12:22:36 -04:00
irq.h Revert "irq: introduce qemu_irq_proxy()" 2019-11-05 23:33:12 +01:00
loader-fit.h
loader.h hw/core/loader: Let load_elf() populate a field with CPU-specific flags 2020-01-29 19:28:52 +01:00
nmi.h hw/nmi: Fix the NMI() macro, based on INTERFACE_CHECK() 2020-02-28 14:57:19 -05:00
or-irq.h hw/core/or-irq: Increase limit of or-lines to 48 2020-01-23 16:34:15 +00:00
pcmcia.h Include hw/qdev-properties.h less 2019-08-16 13:31:53 +02:00
platform-bus.h
ptimer.h ptimer: Remove old ptimer_init_with_bh() API 2019-11-11 13:44:16 +00:00
qdev-clock.h qdev-clock: introduce an init array to ease the device construction 2020-04-30 15:35:40 +01:00
qdev-core.h qdev: Unrealize must not fail 2020-05-15 07:08:14 +02:00
qdev-dma.h
qdev-properties.h multifd: Add multifd-compression parameter 2020-02-28 09:24:43 +01:00
register.h hw/core/register: Add register_init_block8 helper. 2020-05-05 13:37:51 -07:00
registerfields.h hw/registerfields.h: Add 8bit and 16bit register macros 2020-03-19 17:15:19 +01:00
resettable.h hw/core: deprecate old reset functions and introduce new ones 2020-01-30 16:02:04 +00:00
stream.h hw/core: stream: Add an end-of-packet flag 2020-05-14 13:44:35 +02:00
sysbus.h sysbus: remove outdated comment 2020-01-07 16:06:59 +04:00
usb.h qdev: Unrealize must not fail 2020-05-15 07:08:14 +02:00
vmstate-if.h vmstate: add qom interface to get id 2020-01-06 18:41:32 +04:00