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

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