Pablo Greco e6a3ae
From 6a2ee1fd8d36ed8407b403a7307de1633462759c Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
Date: Wed, 9 Oct 2019 12:39:45 +0100
Pablo Greco e6a3ae
Subject: [PATCH 19/22] qdev/machine: Introduce hotplug_allowed hook
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
Message-id: <20191009123947.21505-4-peterx@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 91351
Pablo Greco e6a3ae
O-Subject: [RHEL-8.2.0 qemu-kvm PATCH 3/5] qdev/machine: Introduce hotplug_allowed hook
Pablo Greco e6a3ae
Bugzilla: 1738440
Pablo Greco e6a3ae
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Conflicts:
Pablo Greco e6a3ae
Pablo Greco e6a3ae
  hw/core/qdev.c: don't have 14405c274e86e ("qdev: Provide
Pablo Greco e6a3ae
    qdev_get_bus_hotplug_handler()")
Pablo Greco e6a3ae
Pablo Greco e6a3ae
  include/hw/boards: plenty of new things missing in
Pablo Greco e6a3ae
    MachineClass (kvm_type, numa_mem_supported, smp_parse)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
  include/hw/qdev-core.h: don't have 17cc0128da3 ("qdev: Let machine
Pablo Greco e6a3ae
    hotplug handler to override bus hotplug handler")
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Introduce this new per-machine hook to give any machine class a chance
Pablo Greco e6a3ae
to do a sanity check on the to-be-hotplugged device as a sanity test.
Pablo Greco e6a3ae
This will be used for x86 to try to detect some illegal configuration
Pablo Greco e6a3ae
of devices, e.g., possible conflictions between vfio-pci and x86
Pablo Greco e6a3ae
vIOMMU.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
Message-Id: <20190916080718.3299-3-peterx@redhat.com>
Pablo Greco e6a3ae
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Pablo Greco e6a3ae
(cherry picked from commit d2321d31ff98b75b652c2b1594f00a4cfd48102a)
Pablo Greco e6a3ae
Signed-off-by: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 hw/core/qdev.c         | 17 +++++++++++++++++
Pablo Greco e6a3ae
 include/hw/boards.h    |  9 +++++++++
Pablo Greco e6a3ae
 include/hw/qdev-core.h |  1 +
Pablo Greco e6a3ae
 qdev-monitor.c         |  7 +++++++
Pablo Greco e6a3ae
 4 files changed, 34 insertions(+)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/hw/core/qdev.c b/hw/core/qdev.c
Pablo Greco e6a3ae
index 24f1ae7..5971242 100644
Pablo Greco e6a3ae
--- a/hw/core/qdev.c
Pablo Greco e6a3ae
+++ b/hw/core/qdev.c
Pablo Greco e6a3ae
@@ -259,6 +259,23 @@ HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
Pablo Greco e6a3ae
     return NULL;
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
+bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    MachineState *machine;
Pablo Greco e6a3ae
+    MachineClass *mc;
Pablo Greco e6a3ae
+    Object *m_obj = qdev_get_machine();
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
Pablo Greco e6a3ae
+        machine = MACHINE(m_obj);
Pablo Greco e6a3ae
+        mc = MACHINE_GET_CLASS(machine);
Pablo Greco e6a3ae
+        if (mc->hotplug_allowed) {
Pablo Greco e6a3ae
+            return mc->hotplug_allowed(machine, dev, errp);
Pablo Greco e6a3ae
+        }
Pablo Greco e6a3ae
+    }
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    return true;
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
     HotplugHandler *hotplug_ctrl;
Pablo Greco e6a3ae
diff --git a/include/hw/boards.h b/include/hw/boards.h
Pablo Greco e6a3ae
index 9b4a69b..e568a3c 100644
Pablo Greco e6a3ae
--- a/include/hw/boards.h
Pablo Greco e6a3ae
+++ b/include/hw/boards.h
Pablo Greco e6a3ae
@@ -156,6 +156,13 @@ typedef struct {
Pablo Greco e6a3ae
  *    should instead use "unimplemented-device" for all memory ranges where
Pablo Greco e6a3ae
  *    the guest will attempt to probe for a device that QEMU doesn't
Pablo Greco e6a3ae
  *    implement and a stub device is required.
Pablo Greco e6a3ae
+ * @hotplug_allowed:
Pablo Greco e6a3ae
+ *    If the hook is provided, then it'll be called for each device
Pablo Greco e6a3ae
+ *    hotplug to check whether the device hotplug is allowed.  Return
Pablo Greco e6a3ae
+ *    true to grant allowance or false to reject the hotplug.  When
Pablo Greco e6a3ae
+ *    false is returned, an error must be set to show the reason of
Pablo Greco e6a3ae
+ *    the rejection.  If the hook is not provided, all hotplug will be
Pablo Greco e6a3ae
+ *    allowed.
Pablo Greco e6a3ae
  */
Pablo Greco e6a3ae
 struct MachineClass {
Pablo Greco e6a3ae
     /*< private >*/
Pablo Greco e6a3ae
@@ -210,6 +217,8 @@ struct MachineClass {
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     HotplugHandler *(*get_hotplug_handler)(MachineState *machine,
Pablo Greco e6a3ae
                                            DeviceState *dev);
Pablo Greco e6a3ae
+    bool (*hotplug_allowed)(MachineState *state, DeviceState *dev,
Pablo Greco e6a3ae
+                            Error **errp);
Pablo Greco e6a3ae
     CpuInstanceProperties (*cpu_index_to_instance_props)(MachineState *machine,
Pablo Greco e6a3ae
                                                          unsigned cpu_index);
Pablo Greco e6a3ae
     const CPUArchIdList *(*possible_cpu_arch_ids)(MachineState *machine);
Pablo Greco e6a3ae
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
Pablo Greco e6a3ae
index 9453588..b8d1cac 100644
Pablo Greco e6a3ae
--- a/include/hw/qdev-core.h
Pablo Greco e6a3ae
+++ b/include/hw/qdev-core.h
Pablo Greco e6a3ae
@@ -286,6 +286,7 @@ void qdev_init_nofail(DeviceState *dev);
Pablo Greco e6a3ae
 void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
Pablo Greco e6a3ae
                                  int required_for_version);
Pablo Greco e6a3ae
 HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);
Pablo Greco e6a3ae
+bool qdev_hotplug_allowed(DeviceState *dev, Error **errp);
Pablo Greco e6a3ae
 HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);
Pablo Greco e6a3ae
 void qdev_unplug(DeviceState *dev, Error **errp);
Pablo Greco e6a3ae
 void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
Pablo Greco e6a3ae
diff --git a/qdev-monitor.c b/qdev-monitor.c
Pablo Greco e6a3ae
index f439b83..70bce8f 100644
Pablo Greco e6a3ae
--- a/qdev-monitor.c
Pablo Greco e6a3ae
+++ b/qdev-monitor.c
Pablo Greco e6a3ae
@@ -606,6 +606,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
Pablo Greco e6a3ae
     /* create device */
Pablo Greco e6a3ae
     dev = DEVICE(object_new(driver));
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
+    /* Check whether the hotplug is allowed by the machine */
Pablo Greco e6a3ae
+    if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) {
Pablo Greco e6a3ae
+        /* Error must be set in the machine hook */
Pablo Greco e6a3ae
+        assert(err);
Pablo Greco e6a3ae
+        goto err_del_dev;
Pablo Greco e6a3ae
+    }
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
     if (bus) {
Pablo Greco e6a3ae
         qdev_set_parent_bus(dev, bus);
Pablo Greco e6a3ae
     } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae