Blame SOURCES/libvirt-qemu-Extract-logic-to-determine-the-mlock-limit-size-for-VFIO.patch

38f2fd
From ed413584e982b24442ab860874e307f50d7fdb10 Mon Sep 17 00:00:00 2001
38f2fd
Message-Id: <ed413584e982b24442ab860874e307f50d7fdb10@dist-git>
38f2fd
From: Peter Krempa <pkrempa@redhat.com>
38f2fd
Date: Thu, 12 Nov 2015 08:40:42 +0100
38f2fd
Subject: [PATCH] qemu: Extract logic to determine the mlock limit size for
38f2fd
 VFIO
38f2fd
38f2fd
https://bugzilla.redhat.com/show_bug.cgi?id=1280420
38f2fd
38f2fd
New function qemuDomainGetMlockLimitBytes will now handle the
38f2fd
calculation so that it unifies the logic to one place and allows later
38f2fd
reuse.
38f2fd
38f2fd
(cherry picked from commit fbc58cfcaeffdd4a350cf6abd67da6006f01b148)
38f2fd
38f2fd
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
38f2fd
---
38f2fd
 src/qemu/qemu_command.c | 18 ++----------------
38f2fd
 src/qemu/qemu_domain.c  | 27 +++++++++++++++++++++++++++
38f2fd
 src/qemu/qemu_domain.h  |  2 ++
38f2fd
 src/qemu/qemu_hotplug.c | 17 ++---------------
38f2fd
 4 files changed, 33 insertions(+), 31 deletions(-)
38f2fd
38f2fd
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
38f2fd
index 9c80e0c..7f8a31b 100644
38f2fd
--- a/src/qemu/qemu_command.c
38f2fd
+++ b/src/qemu/qemu_command.c
38f2fd
@@ -11146,22 +11146,8 @@ qemuBuildCommandLine(virConnectPtr conn,
38f2fd
             goto error;
38f2fd
     }
38f2fd
 
38f2fd
-    if (mlock) {
38f2fd
-        unsigned long long memKB;
38f2fd
-
38f2fd
-        /* VFIO requires all of the guest's memory to be
38f2fd
-         * locked resident, plus some amount for IO
38f2fd
-         * space. Alex Williamson suggested adding 1GiB for IO
38f2fd
-         * space just to be safe (some finer tuning might be
38f2fd
-         * nice, though).
38f2fd
-         */
38f2fd
-        if (virMemoryLimitIsSet(def->mem.hard_limit))
38f2fd
-            memKB = def->mem.hard_limit;
38f2fd
-        else
38f2fd
-            memKB = virDomainDefGetMemoryActual(def) + 1024 * 1024;
38f2fd
-
38f2fd
-        virCommandSetMaxMemLock(cmd, memKB * 1024);
38f2fd
-    }
38f2fd
+    if (mlock)
38f2fd
+        virCommandSetMaxMemLock(cmd, qemuDomainGetMlockLimitBytes(def));
38f2fd
 
38f2fd
     if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MSG_TIMESTAMP) &&
38f2fd
         cfg->logTimestamp)
38f2fd
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
38f2fd
index 1528cfd..944817d 100644
38f2fd
--- a/src/qemu/qemu_domain.c
38f2fd
+++ b/src/qemu/qemu_domain.c
38f2fd
@@ -3422,3 +3422,30 @@ qemuDomainUpdateCurrentMemorySize(virQEMUDriverPtr driver,
38f2fd
 
38f2fd
     return 0;
38f2fd
 }
38f2fd
+
38f2fd
+
38f2fd
+/**
38f2fd
+ * qemuDomainGetMlockLimitBytes:
38f2fd
+ *
38f2fd
+ * @def: domain definition
38f2fd
+ *
38f2fd
+ * Returns the size of the memory in bytes that needs to be set as
38f2fd
+ * RLIMIT_MEMLOCK for purpose of VFIO device passthrough.
38f2fd
+ * If a mem.hard_limit is set, then that value is preferred; otherwise, the
38f2fd
+ * value returned may depend upon the architecture or devices present.
38f2fd
+ */
38f2fd
+unsigned long long
38f2fd
+qemuDomainGetMlockLimitBytes(virDomainDefPtr def)
38f2fd
+{
38f2fd
+    unsigned long long memKB;
38f2fd
+
38f2fd
+    /* VFIO requires all of the guest's memory to be locked resident, plus some
38f2fd
+     * amount for IO space. Alex Williamson suggested adding 1GiB for IO space
38f2fd
+     * just to be safe (some finer tuning might be nice, though). */
38f2fd
+    if (virMemoryLimitIsSet(def->mem.hard_limit))
38f2fd
+        memKB = def->mem.hard_limit;
38f2fd
+    else
38f2fd
+        memKB = virDomainDefGetMemoryActual(def) + 1024 * 1024;
38f2fd
+
38f2fd
+    return memKB << 10;
38f2fd
+}
38f2fd
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
38f2fd
index e283b58..54e7cd9 100644
38f2fd
--- a/src/qemu/qemu_domain.h
38f2fd
+++ b/src/qemu/qemu_domain.h
38f2fd
@@ -475,4 +475,6 @@ bool qemuDomainMachineIsS390CCW(const virDomainDef *def);
38f2fd
 int qemuDomainUpdateCurrentMemorySize(virQEMUDriverPtr driver,
38f2fd
                                       virDomainObjPtr vm);
38f2fd
 
38f2fd
+unsigned long long qemuDomainGetMlockLimitBytes(virDomainDefPtr def);
38f2fd
+
38f2fd
 #endif /* __QEMU_DOMAIN_H__ */
38f2fd
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
38f2fd
index bd96abf..d96f572 100644
38f2fd
--- a/src/qemu/qemu_hotplug.c
38f2fd
+++ b/src/qemu/qemu_hotplug.c
38f2fd
@@ -1254,7 +1254,6 @@ qemuDomainAttachHostPCIDevice(virQEMUDriverPtr driver,
38f2fd
     bool teardowncgroup = false;
38f2fd
     bool teardownlabel = false;
38f2fd
     int backend;
38f2fd
-    unsigned long long memKB;
38f2fd
     virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
38f2fd
     unsigned int flags = 0;
38f2fd
 
38f2fd
@@ -1279,20 +1278,8 @@ qemuDomainAttachHostPCIDevice(virQEMUDriverPtr driver,
38f2fd
             goto error;
38f2fd
         }
38f2fd
 
38f2fd
-        /* VFIO requires all of the guest's memory to be locked
38f2fd
-         * resident (plus an additional 1GiB to cover IO space). During
38f2fd
-         * hotplug, the guest's memory may already be locked, but it
38f2fd
-         * doesn't hurt to "change" the limit to the same value.
38f2fd
-         * NB: the domain's memory tuning parameters are stored as
38f2fd
-         * Kibibytes, but virProcessSetMaxMemLock expects the value in
38f2fd
-         * bytes.
38f2fd
-         */
38f2fd
-        if (virMemoryLimitIsSet(vm->def->mem.hard_limit))
38f2fd
-            memKB = vm->def->mem.hard_limit;
38f2fd
-        else
38f2fd
-            memKB = virDomainDefGetMemoryActual(vm->def) + (1024 * 1024);
38f2fd
-
38f2fd
-        virProcessSetMaxMemLock(vm->pid, memKB * 1024);
38f2fd
+        /* setup memory locking limits, that are necessary for VFIO */
38f2fd
+        virProcessSetMaxMemLock(vm->pid, qemuDomainGetMlockLimitBytes(vm->def));
38f2fd
         break;
38f2fd
 
38f2fd
     default:
38f2fd
-- 
38f2fd
2.6.3
38f2fd