76daa3
From 9fa3c9fc6dfcde76d80db1aa601b2d577f72ceec Mon Sep 17 00:00:00 2001
76daa3
From: Bandan Das <bsd@redhat.com>
76daa3
Date: Tue, 3 Dec 2013 20:05:13 +0100
76daa3
Subject: vfio: cap number of devices that can be assigned
76daa3
76daa3
RH-Author: Bandan Das <bsd@redhat.com>
76daa3
Message-id: <1386101113-31560-3-git-send-email-bsd@redhat.com>
76daa3
Patchwork-id: 55984
76daa3
O-Subject: [PATCH RHEL7 qemu-kvm v2 2/2] vfio: cap number of devices that can be assigned
76daa3
Bugzilla: 678368
76daa3
RH-Acked-by: Alex Williamson <alex.williamson@redhat.com>
76daa3
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>
76daa3
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
76daa3
76daa3
Go through all groups to get count of total number of devices
76daa3
active to enforce limit
76daa3
76daa3
Reasoning from Alex for the limit(32) - Assuming 3 slots per
76daa3
device, with 125 slots (number of memory slots for RHEL 7),
76daa3
we can support almost 40 devices and still have few slots left
76daa3
for other uses. Stepping down a bit, the number 32 arbitrarily
76daa3
matches the number of slots on a PCI bus and is also a nice power
76daa3
of two.
76daa3
76daa3
Signed-off-by: Bandan Das <bsd@redhat.com>
76daa3
(cherry picked from commit 5c749a1960fbbf1924b8cf285bdb5a8f78d27ab7)
76daa3
76daa3
Rebase notes (2.8.0):
76daa3
- removed return value for vfio_realize (commit 1a22aca)
76daa3
76daa3
Merged patches (2.9.0):
76daa3
- 17eb774 vfio: Use error_setg when reporting max assigned device overshoot
76daa3
76daa3
(cherry picked from commit a2f327758bac334f69c26977befbe3e4e499f758)
76daa3
---
76daa3
 hw/vfio/pci.c | 15 ++++++++++++++-
76daa3
 1 file changed, 14 insertions(+), 1 deletion(-)
76daa3
76daa3
diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
76daa3
index 03a3d01..756cad4 100644
76daa3
--- a/hw/vfio/pci.c
76daa3
+++ b/hw/vfio/pci.c
76daa3
@@ -34,6 +34,7 @@
76daa3
 #include "qapi/error.h"
76daa3
 
76daa3
 #define MSIX_CAP_LENGTH 12
76daa3
+#define MAX_DEV_ASSIGN_CMDLINE 32
76daa3
 
76daa3
 static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
76daa3
 static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
76daa3
@@ -2619,7 +2620,19 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
76daa3
     ssize_t len;
76daa3
     struct stat st;
76daa3
     int groupid;
76daa3
-    int i, ret;
76daa3
+    int ret, i = 0;
76daa3
+
76daa3
+    QLIST_FOREACH(group, &vfio_group_list, next) {
76daa3
+        QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
76daa3
+            i++;
76daa3
+        }
76daa3
+    }
76daa3
+
76daa3
+    if (i >= MAX_DEV_ASSIGN_CMDLINE) {
76daa3
+        error_setg(errp, "Maximum supported vfio devices (%d) "
76daa3
+                     "already attached", MAX_DEV_ASSIGN_CMDLINE);
76daa3
+        return;
76daa3
+    }
76daa3
 
76daa3
     if (!vdev->vbasedev.sysfsdev) {
76daa3
         if (!(~vdev->host.domain || ~vdev->host.bus ||
76daa3
-- 
76daa3
1.8.3.1
76daa3