thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/0012-vfio-cap-number-of-devices-that-can-be-assigned.patch

7f1c5b
From 0804844e4755377be6d2ebad578794ad9f4f3f31 Mon Sep 17 00:00:00 2001
a83cc2
From: Bandan Das <bsd@redhat.com>
a83cc2
Date: Tue, 3 Dec 2013 20:05:13 +0100
a83cc2
Subject: vfio: cap number of devices that can be assigned
a83cc2
a83cc2
RH-Author: Bandan Das <bsd@redhat.com>
a83cc2
Message-id: <1386101113-31560-3-git-send-email-bsd@redhat.com>
a83cc2
Patchwork-id: 55984
a83cc2
O-Subject: [PATCH RHEL7 qemu-kvm v2 2/2] vfio: cap number of devices that can be assigned
a83cc2
Bugzilla: 678368
a83cc2
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>
a83cc2
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>
a83cc2
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
a83cc2
a83cc2
Go through all groups to get count of total number of devices
a83cc2
active to enforce limit
a83cc2
a83cc2
Reasoning from Alex for the limit(32) - Assuming 3 slots per
a83cc2
device, with 125 slots (number of memory slots for RHEL 7),
a83cc2
we can support almost 40 devices and still have few slots left
a83cc2
for other uses. Stepping down a bit, the number 32 arbitrarily
a83cc2
matches the number of slots on a PCI bus and is also a nice power
a83cc2
of two.
a83cc2
77609c
Count of slots increased to 509 later so we could increase limit
77609c
to 64 as some usecases require more than 32 devices.
77609c
a83cc2
Signed-off-by: Bandan Das <bsd@redhat.com>
a83cc2
---
a83cc2
 hw/vfio/pci.c | 29 ++++++++++++++++++++++++++++-
a83cc2
 hw/vfio/pci.h |  1 +
a83cc2
 2 files changed, 29 insertions(+), 1 deletion(-)
a83cc2
a83cc2
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
7f1c5b
index 939dcc3d4a..acbc6673ce 100644
a83cc2
--- a/hw/vfio/pci.c
a83cc2
+++ b/hw/vfio/pci.c
7f1c5b
@@ -48,6 +48,9 @@
7f1c5b
 /* Protected by BQL */
7f1c5b
 static KVMRouteChange vfio_route_change;
a83cc2
 
a83cc2
+/* RHEL only: Set once for the first assigned dev */
a83cc2
+static uint16_t device_limit;
a83cc2
+
a83cc2
 static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
a83cc2
 static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
7f1c5b
 static void vfio_msi_disable_common(VFIOPCIDevice *vdev);
7f1c5b
@@ -2854,9 +2857,30 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
a83cc2
     ssize_t len;
a83cc2
     struct stat st;
a83cc2
     int groupid;
a83cc2
-    int i, ret;
a83cc2
+    int ret, i = 0;
a83cc2
     bool is_mdev;
a83cc2
 
a83cc2
+    if (device_limit && device_limit != vdev->assigned_device_limit) {
a83cc2
+            error_setg(errp, "Assigned device limit has been redefined. "
a83cc2
+                       "Old:%d, New:%d",
a83cc2
+                       device_limit, vdev->assigned_device_limit);
a83cc2
+            return;
a83cc2
+    } else {
a83cc2
+        device_limit = vdev->assigned_device_limit;
a83cc2
+    }
a83cc2
+
a83cc2
+    QLIST_FOREACH(group, &vfio_group_list, next) {
a83cc2
+        QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
a83cc2
+            i++;
a83cc2
+        }
a83cc2
+    }
a83cc2
+
a83cc2
+    if (i >= vdev->assigned_device_limit) {
a83cc2
+        error_setg(errp, "Maximum supported vfio devices (%d) "
a83cc2
+                     "already attached", vdev->assigned_device_limit);
a83cc2
+        return;
a83cc2
+    }
a83cc2
+
7f1c5b
     if (!vbasedev->sysfsdev) {
a83cc2
         if (!(~vdev->host.domain || ~vdev->host.bus ||
a83cc2
               ~vdev->host.slot || ~vdev->host.function)) {
7f1c5b
@@ -3293,6 +3317,9 @@ static Property vfio_pci_dev_properties[] = {
a83cc2
     DEFINE_PROP_BOOL("x-no-kvm-msix", VFIOPCIDevice, no_kvm_msix, false),
a83cc2
     DEFINE_PROP_BOOL("x-no-geforce-quirks", VFIOPCIDevice,
a83cc2
                      no_geforce_quirks, false),
a83cc2
+    /* RHEL only */
a83cc2
+    DEFINE_PROP_UINT16("x-assigned-device-limit", VFIOPCIDevice,
a83cc2
+                       assigned_device_limit, 64),
a83cc2
     DEFINE_PROP_BOOL("x-no-kvm-ioeventfd", VFIOPCIDevice, no_kvm_ioeventfd,
a83cc2
                      false),
a83cc2
     DEFINE_PROP_BOOL("x-no-vfio-ioeventfd", VFIOPCIDevice, no_vfio_ioeventfd,
a83cc2
diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h
7f1c5b
index 7c236a52f4..7b7d036a8f 100644
a83cc2
--- a/hw/vfio/pci.h
a83cc2
+++ b/hw/vfio/pci.h
7f1c5b
@@ -140,6 +140,7 @@ struct VFIOPCIDevice {
a83cc2
     EventNotifier err_notifier;
a83cc2
     EventNotifier req_notifier;
a83cc2
     int (*resetfn)(struct VFIOPCIDevice *);
a83cc2
+    uint16_t assigned_device_limit;
a83cc2
     uint32_t vendor_id;
a83cc2
     uint32_t device_id;
a83cc2
     uint32_t sub_vendor_id;
a83cc2
-- 
29b115
2.31.1
a83cc2