Pablo Greco e6a3ae
From f117f5fb216e45796a32579c03673c1d79164037 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:46 +0100
Pablo Greco e6a3ae
Subject: [PATCH 20/22] pc/q35: Disallow vfio-pci hotplug without VT-d caching
Pablo Greco e6a3ae
 mode
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
Message-id: <20191009123947.21505-5-peterx@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 91352
Pablo Greco e6a3ae
O-Subject: [RHEL-8.2.0 qemu-kvm PATCH 4/5] pc/q35: Disallow vfio-pci hotplug without VT-d caching mode
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/i386/pc.c: context differs on quite a few places in
Pablo Greco e6a3ae
    pc_machine_class_init(), but none of them is really relevant to
Pablo Greco e6a3ae
    current change.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Instead of bailing out when trying to hotplug a vfio-pci device with
Pablo Greco e6a3ae
below configuration:
Pablo Greco e6a3ae
Pablo Greco e6a3ae
  -device intel-iommu,caching-mode=off
Pablo Greco e6a3ae
Pablo Greco e6a3ae
With this we can return a warning message to the user via QMP/HMP and
Pablo Greco e6a3ae
the VM will continue to work after failing the hotplug:
Pablo Greco e6a3ae
Pablo Greco e6a3ae
  (qemu) device_add vfio-pci,bus=root.3,host=05:00.0,id=vfio1
Pablo Greco e6a3ae
  Error: Device assignment is not allowed without enabling caching-mode=on for Intel IOMMU.
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-4-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 c6cbc29d36fe8df078776ed715c37cebac582238)
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/i386/pc.c | 21 +++++++++++++++++++++
Pablo Greco e6a3ae
 1 file changed, 21 insertions(+)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/hw/i386/pc.c b/hw/i386/pc.c
Pablo Greco e6a3ae
index 9e1e6ae..d6c4050 100644
Pablo Greco e6a3ae
--- a/hw/i386/pc.c
Pablo Greco e6a3ae
+++ b/hw/i386/pc.c
Pablo Greco e6a3ae
@@ -2340,6 +2340,26 @@ static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    X86IOMMUState *iommu = x86_iommu_get_default();
Pablo Greco e6a3ae
+    IntelIOMMUState *intel_iommu;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    if (iommu &&
Pablo Greco e6a3ae
+        object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) &&
Pablo Greco e6a3ae
+        object_dynamic_cast((Object *)dev, "vfio-pci")) {
Pablo Greco e6a3ae
+        intel_iommu = INTEL_IOMMU_DEVICE(iommu);
Pablo Greco e6a3ae
+        if (!intel_iommu->caching_mode) {
Pablo Greco e6a3ae
+            error_setg(errp, "Device assignment is not allowed without "
Pablo Greco e6a3ae
+                       "enabling caching-mode=on for Intel IOMMU.");
Pablo Greco e6a3ae
+            return false;
Pablo Greco e6a3ae
+        }
Pablo Greco e6a3ae
+    }
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    return true;
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
 static void pc_machine_class_init(ObjectClass *oc, void *data)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
     MachineClass *mc = MACHINE_CLASS(oc);
Pablo Greco e6a3ae
@@ -2369,6 +2389,7 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
Pablo Greco e6a3ae
      */
Pablo Greco e6a3ae
     mc->async_pf_vmexit_disable = true;
Pablo Greco e6a3ae
     mc->get_hotplug_handler = pc_get_hotpug_handler;
Pablo Greco e6a3ae
+    mc->hotplug_allowed = pc_hotplug_allowed;
Pablo Greco e6a3ae
     mc->cpu_index_to_instance_props = pc_cpu_index_to_props;
Pablo Greco e6a3ae
     mc->get_default_cpu_node_id = pc_get_default_cpu_node_id;
Pablo Greco e6a3ae
     mc->possible_cpu_arch_ids = pc_possible_cpu_arch_ids;
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae