diff mbox series

[v1,1/5] pc: Properly handle unplug of virtio based memory devices

Message ID 20230613150210.449406-2-david@redhat.com (mailing list archive)
State New, archived
Headers show
Series virtio-mem: Device unplug support | expand

Commit Message

David Hildenbrand June 13, 2023, 3:02 p.m. UTC
While we fence unplug requests from the outside, the VM can still
trigger unplug of virtio based memory devices, for example, in Linux
doing:
    # echo 0 > /sys/bus/pci/slots/3/power

While doing that is not really expected to work without harming the
guest OS (e.g., removing a virtio-mem device while it still provides
memory), let's make sure that we properly handle it on the QEMU side.

We'll support unplugging of virtio-mem devices in some configurations
next.

Signed-off-by: David Hildenbrand <david@redhat.com>
---
 hw/i386/pc.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index fc52772fdd..fdd7062929 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -1559,7 +1559,25 @@  static void pc_virtio_md_pci_unplug_request(HotplugHandler *hotplug_dev,
 static void pc_virtio_md_pci_unplug(HotplugHandler *hotplug_dev,
                                     DeviceState *dev, Error **errp)
 {
-    /* We don't support hot unplug of virtio based memory devices */
+    HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
+    Error *local_err = NULL;
+
+    /* Unplug the memory device while it is still realized. */
+    memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
+
+    if (hotplug_dev2) {
+        hotplug_handler_unplug(hotplug_dev2, dev, &local_err);
+        if (local_err) {
+            /* Not expected to fail ... but still try to recover. */
+            memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
+            error_propagate(errp, local_err);
+            return;
+        }
+    } else {
+        /* Very unexpected, but let's just try to do the right thing. */
+        warn_report("Unexpected unplug of virtio based memory device");
+        qdev_unrealize(dev);
+    }
 }
 
 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,