]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
qemu: Use qemuDomainMemoryLimit when computing memory for VFIO
authorJiri Denemark <jdenemar@redhat.com>
Fri, 28 Jun 2013 14:54:38 +0000 (16:54 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Mon, 8 Jul 2013 10:35:27 +0000 (12:35 +0200)
src/qemu/qemu_command.c
src/qemu/qemu_domain.c
src/qemu/qemu_hotplug.c

index ba93233745ac3e0015c6d1e81a796712f8ea4f56..a9aa6702f20f2e18bcc04cfc2ad6c32f2f03fde9 100644 (file)
@@ -6683,6 +6683,7 @@ qemuBuildCommandLine(virConnectPtr conn,
     int spice = 0;
     int usbcontroller = 0;
     bool usblegacy = false;
+    bool mlock = false;
     int contOrder[] = {
         /* We don't add an explicit IDE or FD controller because the
          * provided PIIX4 device already includes one. It isn't possible to
@@ -8337,22 +8338,15 @@ qemuBuildCommandLine(virConnectPtr conn,
 
             if (hostdev->source.subsys.u.pci.backend
                 == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
-                unsigned long long memKB;
-
                 if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                    _("VFIO PCI device assignment is not "
                                      "supported by this version of qemu"));
                     goto error;
                 }
-                /* VFIO requires all of the guest's memory to be
-                 * locked resident, plus some amount for IO
-                 * space. Alex Williamson suggested adding 1GiB for IO
-                 * space just to be safe (some finer tuning might be
-                 * nice, though).
-                 */
-                memKB = def->mem.max_balloon + (1024 * 1024);
-                virCommandSetMaxMemLock(cmd, memKB * 1024);
+                /* VFIO requires all of the guest's memory to be locked
+                 * resident */
+                mlock = true;
             }
 
             if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
@@ -8572,6 +8566,9 @@ qemuBuildCommandLine(virConnectPtr conn,
         goto error;
     }
 
+    if (mlock)
+        virCommandSetMaxMemLock(cmd, qemuDomainMemoryLimit(def) * 1024);
+
     virObjectUnref(cfg);
     return cmd;
 
index 92cf4b64d9b40ee693eeeae419fa987e31899fd5..28b8ee48f4007b02c4934e32538bfe829ee7357c 100644 (file)
@@ -2199,6 +2199,9 @@ qemuDomainMemoryLimit(virDomainDefPtr def)
          *     cache per each disk) + F
          * where k = 0.5 and F = 200MB.  The cache for disks is important as
          * kernel cache on the host side counts into the RSS limit.
+         * Moreover, VFIO requires some amount for IO space. Alex Williamson
+         * suggested adding 1GiB for IO space just to be safe (some finer
+         * tuning might be nice, though).
          *
          * Technically, the disk cache does not have to be included in
          * RLIMIT_MEMLOCK but it doesn't hurt as it's just an upper limit and
@@ -2210,6 +2213,18 @@ qemuDomainMemoryLimit(virDomainDefPtr def)
         mem *= 1.5;
         mem += def->ndisks * 32768;
         mem += 204800;
+
+        for (i = 0; i < def->nhostdevs; i++) {
+            virDomainHostdevDefPtr hostdev = def->hostdevs[i];
+            if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
+                hostdev->source.subsys.type ==
+                    VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
+                hostdev->source.subsys.u.pci.backend ==
+                    VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
+                mem += 1024 * 1024;
+                break;
+            }
+        }
     }
 
     return mem;
index 46875ad6c99c02d584cadac6914cb6f5ea677289..a350059126ffcef3ece6b46f9fd420972a518906 100644 (file)
@@ -1054,23 +1054,21 @@ int qemuDomainAttachHostPciDevice(virQEMUDriverPtr driver,
 
     if (hostdev->source.subsys.u.pci.backend
         == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
-        unsigned long long memKB;
-
         if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("VFIO PCI device assignment is not "
                              "supported by this version of qemu"));
             goto error;
         }
-        /* VFIO requires all of the guest's memory to be locked
-         * resident, plus some amount for IO space. Alex Williamson
-         * suggested adding 1GiB for IO space just to be safe (some
-         * finer tuning might be nice, though).
-         * In this case, the guest's memory may already be locked, but
-         * it doesn't hurt to "change" the limit to the same value.
+
+        /* VFIO requires all of the guest's memory to be locked resident.
+         * In this case, the guest's memory may already be locked, but it
+         * doesn't hurt to "change" the limit to the same value.
          */
-        memKB = vm->def->mem.max_balloon + (1024 * 1024);
-        virProcessSetMaxMemLock(vm->pid, memKB * 1024);
+        vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
+        virProcessSetMaxMemLock(vm->pid,
+                                qemuDomainMemoryLimit(vm->def) * 1024);
+        vm->def->hostdevs[vm->def->nhostdevs--] = NULL;
     }
 
     if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {