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

902636
From de433da59448eaad4ac1b902d07d57b57f922aff Mon Sep 17 00:00:00 2001
26ba25
From: Bandan Das <bsd@redhat.com>
26ba25
Date: Tue, 3 Dec 2013 20:05:13 +0100
26ba25
Subject: vfio: cap number of devices that can be assigned
26ba25
26ba25
RH-Author: Bandan Das <bsd@redhat.com>
26ba25
Message-id: <1386101113-31560-3-git-send-email-bsd@redhat.com>
26ba25
Patchwork-id: 55984
26ba25
O-Subject: [PATCH RHEL7 qemu-kvm v2 2/2] vfio: cap number of devices that can be assigned
26ba25
Bugzilla: 678368
26ba25
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>
26ba25
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>
26ba25
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
26ba25
26ba25
Go through all groups to get count of total number of devices
26ba25
active to enforce limit
26ba25
26ba25
Reasoning from Alex for the limit(32) - Assuming 3 slots per
26ba25
device, with 125 slots (number of memory slots for RHEL 7),
26ba25
we can support almost 40 devices and still have few slots left
26ba25
for other uses. Stepping down a bit, the number 32 arbitrarily
26ba25
matches the number of slots on a PCI bus and is also a nice power
26ba25
of two.
26ba25
26ba25
Signed-off-by: Bandan Das <bsd@redhat.com>
26ba25
26ba25
Rebase notes (2.8.0):
26ba25
- removed return value for vfio_realize (commit 1a22aca)
26ba25
26ba25
Merged patches (2.9.0):
26ba25
- 17eb774 vfio: Use error_setg when reporting max assigned device overshoot
26ba25
902636
 Merged patches (4.1.0-rc3):
902636
- 2b89558 vfio: increase the cap on number of assigned devices to 64
902636
26ba25
(cherry picked from commit 9fa3c9fc6dfcde76d80db1aa601b2d577f72ceec)
26ba25
(cherry picked from commit 3cb35556dc7d994f203d732fe952f95fcdb03c0a)
902636
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
902636
 hw/vfio/pci.c | 29 ++++++++++++++++++++++++++++-
902636
 hw/vfio/pci.h |  1 +
902636
 2 files changed, 29 insertions(+), 1 deletion(-)
26ba25
26ba25
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
902636
index c8534d3035..309535f306 100644
26ba25
--- a/hw/vfio/pci.c
26ba25
+++ b/hw/vfio/pci.c
902636
@@ -47,6 +47,9 @@
26ba25
 
902636
 #define TYPE_VFIO_PCI_NOHOTPLUG "vfio-pci-nohotplug"
26ba25
 
902636
+/* RHEL only: Set once for the first assigned dev */
902636
+static uint16_t device_limit;
902636
+
26ba25
 static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
26ba25
 static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
902636
 
902636
@@ -2722,9 +2725,30 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
26ba25
     ssize_t len;
26ba25
     struct stat st;
26ba25
     int groupid;
26ba25
-    int i, ret;
26ba25
+    int ret, i = 0;
902636
     bool is_mdev;
902636
 
902636
+    if (device_limit && device_limit != vdev->assigned_device_limit) {
902636
+            error_setg(errp, "Assigned device limit has been redefined. "
902636
+                       "Old:%d, New:%d",
902636
+                       device_limit, vdev->assigned_device_limit);
902636
+            return;
902636
+    } else {
902636
+        device_limit = vdev->assigned_device_limit;
902636
+    }
26ba25
+
26ba25
+    QLIST_FOREACH(group, &vfio_group_list, next) {
26ba25
+        QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
26ba25
+            i++;
26ba25
+        }
26ba25
+    }
26ba25
+
902636
+    if (i >= vdev->assigned_device_limit) {
26ba25
+        error_setg(errp, "Maximum supported vfio devices (%d) "
902636
+                     "already attached", vdev->assigned_device_limit);
26ba25
+        return;
26ba25
+    }
902636
+
26ba25
     if (!vdev->vbasedev.sysfsdev) {
26ba25
         if (!(~vdev->host.domain || ~vdev->host.bus ||
902636
               ~vdev->host.slot || ~vdev->host.function)) {
902636
@@ -3167,6 +3191,9 @@ static Property vfio_pci_dev_properties[] = {
902636
     DEFINE_PROP_BOOL("x-no-kvm-msix", VFIOPCIDevice, no_kvm_msix, false),
902636
     DEFINE_PROP_BOOL("x-no-geforce-quirks", VFIOPCIDevice,
902636
                      no_geforce_quirks, false),
902636
+    /* RHEL only */
902636
+    DEFINE_PROP_UINT16("x-assigned-device-limit", VFIOPCIDevice,
902636
+                       assigned_device_limit, 64),
902636
     DEFINE_PROP_BOOL("x-no-kvm-ioeventfd", VFIOPCIDevice, no_kvm_ioeventfd,
902636
                      false),
902636
     DEFINE_PROP_BOOL("x-no-vfio-ioeventfd", VFIOPCIDevice, no_vfio_ioeventfd,
902636
diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h
902636
index 35626cd63e..0cd4803aee 100644
902636
--- a/hw/vfio/pci.h
902636
+++ b/hw/vfio/pci.h
902636
@@ -135,6 +135,7 @@ typedef struct VFIOPCIDevice {
902636
     EventNotifier err_notifier;
902636
     EventNotifier req_notifier;
902636
     int (*resetfn)(struct VFIOPCIDevice *);
902636
+    uint16_t assigned_device_limit;
902636
     uint32_t vendor_id;
902636
     uint32_t device_id;
902636
     uint32_t sub_vendor_id;
26ba25
-- 
902636
2.21.0
26ba25