[libvirt] [PATCH] qemu: Increase RLIMIT_MEMLOCK when memoryBacking/locked is used

Jiri Denemark jdenemar at redhat.com
Fri May 17 13:03:16 UTC 2013


If a domain is configured to have all its memory locked, we need to
increase RLIMIT_MEMLOCK so that QEMU is actually allowed to lock the
memory.
---
 src/qemu/qemu_command.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index c268a3a..8e2de09 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -6440,6 +6440,7 @@ qemuBuildCommandLine(virConnectPtr conn,
     int spice = 0;
     int usbcontroller = 0;
     bool usblegacy = false;
+    bool mlock;
     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
@@ -6551,6 +6552,7 @@ qemuBuildCommandLine(virConnectPtr conn,
         virCommandAddArgFormat(cmd, "mlock=%s",
                                def->mem.locked ? "on" : "off");
     }
+    mlock = def->mem.locked;
 
     virCommandAddArg(cmd, "-smp");
     if (!(smp = qemuBuildSmpArgStr(def, qemuCaps)))
@@ -8191,22 +8193,13 @@ 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);
+                mlock = true;
             }
 
             if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE)) {
@@ -8425,6 +8418,24 @@ qemuBuildCommandLine(virConnectPtr conn,
         goto error;
     }
 
+    if (mlock) {
+        unsigned long long memKB;
+        /* 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).
+         *
+         * If memory hard_limit is configured, we can use it directly as
+         * there is no sense to set MEMLOCK limit beyond it. Also we can
+         * safely use it instead of any magically computed value.
+         */
+        if (def->mem.hard_limit)
+            memKB = def->mem.hard_limit;
+        else
+            memKB = def->mem.max_balloon + (1024 * 1024);
+        virCommandSetMaxMemLock(cmd, memKB * 1024);
+    }
+
     virObjectUnref(cfg);
     return cmd;
 
-- 
1.8.2.1




More information about the libvir-list mailing list