diff mbox series

[v4,23/24] virtio-pmem: hotplug support functions

Message ID 20180926094219.20322-24-david@redhat.com
State New
Headers show
Series memory-device: complete refactoring + virtio-pmem | expand

Commit Message

David Hildenbrand Sept. 26, 2018, 9:42 a.m. UTC
virtio-pmem devices will have to be hotplugged using the machine hotplug
handler just like other memory devices. Therefore, all machines that
want to support virtio-pmem will have to modify their machine hotplug
handler.

virtio-pmem devices are realized when their parent proxy device
(virtio-pmem-pci) is realized. Therefore, they are attached to a bus
without a hotplug handler. This makes things a lot easier, because
without a hotplug handler, we can directly get control over the device
in the machine hotplug handler (otherwise we would have to overwrite
control and pass control to the bus hotplug handler from the machine
hotplug handler).

As we have to implement support for each machine we want to support,
add a safety net ("pre_plugged") that catches if the pre_plug handler
was not called - if trying to realize it with a machine that does not
support it.

Otherwise creating and realizing virtio-pmem-pci along with virtio-pmem
would work, however the memory-device part would not properly get
hotplugged.

Signed-off-by: David Hildenbrand <david@redhat.com>
---
 hw/virtio/virtio-pmem.c         | 34 +++++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-pmem.h | 11 +++++++++++
 2 files changed, 45 insertions(+)
diff mbox series

Patch

diff --git a/hw/virtio/virtio-pmem.c b/hw/virtio/virtio-pmem.c
index 0f8b509f0f..893e92237b 100644
--- a/hw/virtio/virtio-pmem.c
+++ b/hw/virtio/virtio-pmem.c
@@ -112,6 +112,12 @@  static void virtio_pmem_realize(DeviceState *dev, Error **errp)
         return;
     }
 
+    /* pre_plug handler wasn't executed (e.g. from machine hotplug handler) */
+    if (!pmem->pre_plugged) {
+        error_setg(errp, "virtio-pmem is not compatible with the machine");
+        return;
+    }
+
     host_memory_backend_set_mapped(pmem->memdev, true);
     virtio_init(vdev, TYPE_VIRTIO_PMEM, VIRTIO_ID_PMEM,
                                           sizeof(struct virtio_pmem_config));
@@ -205,6 +211,34 @@  static void virtio_pmem_class_init(ObjectClass *klass, void *data)
     mdc->get_device_id = virtio_pmem_md_get_device_id;
 }
 
+void virtio_pmem_pre_plug(VirtIOPMEM *pmem, MachineState *ms, Error **errp)
+{
+    /*
+     * The proxy device (e.g. virtio-pmem-pci) has an hotplug handler and
+     * will attach the virtio-pmem device to its bus (parent_bus). This
+     * device will realize the virtio-mem device from its realize function,
+     * therefore when it is hotplugged itself. The proxy device bus
+     * therefore has no hotplug handler and we don't have to forward any
+     * calls.
+     */
+    if (!DEVICE(pmem)->parent_bus ||
+        DEVICE(pmem)->parent_bus->hotplug_handler) {
+        error_setg(errp, "virtio-pmem is not compatible with the proxy.");
+    }
+    memory_device_pre_plug(MEMORY_DEVICE(pmem), ms, NULL, errp);
+    pmem->pre_plugged = true;
+}
+
+void virtio_pmem_plug(VirtIOPMEM *pmem, MachineState *ms, Error **errp)
+{
+    memory_device_plug(MEMORY_DEVICE(pmem), ms);
+}
+
+void virtio_pmem_do_unplug(VirtIOPMEM *pmem, MachineState *ms)
+{
+    memory_device_unplug(MEMORY_DEVICE(pmem), ms);
+}
+
 static TypeInfo virtio_pmem_info = {
     .name          = TYPE_VIRTIO_PMEM,
     .parent        = TYPE_VIRTIO_DEVICE,
diff --git a/include/hw/virtio/virtio-pmem.h b/include/hw/virtio/virtio-pmem.h
index 11e549e0f1..9cc70e391d 100644
--- a/include/hw/virtio/virtio-pmem.h
+++ b/include/hw/virtio/virtio-pmem.h
@@ -31,10 +31,21 @@  typedef struct VirtIOPMEM {
     VirtQueue *rq_vq;
     uint64_t start;
     HostMemoryBackend *memdev;
+
+    /*
+     * Safety net to make sure we can catch trying to be realized on a
+     * machine that is not prepared to properly hotplug virtio-pmem from
+     * its machine hotplug handler.
+     */
+    bool pre_plugged;
 } VirtIOPMEM;
 
 struct virtio_pmem_config {
     uint64_t start;
     uint64_t size;
 };
+
+void virtio_pmem_pre_plug(VirtIOPMEM *pmem, MachineState *ms, Error **errp);
+void virtio_pmem_plug(VirtIOPMEM *pmem, MachineState *ms, Error **errp);
+void virtio_pmem_do_unplug(VirtIOPMEM *pmem, MachineState *ms);
 #endif