render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
459f93
From 2595c7716b19214b2729b41b86656f96a2cd18bc Mon Sep 17 00:00:00 2001
459f93
Message-Id: <2595c7716b19214b2729b41b86656f96a2cd18bc@dist-git>
459f93
From: Jiri Denemark <jdenemar@redhat.com>
459f93
Date: Wed, 22 Jun 2022 15:21:30 +0200
459f93
Subject: [PATCH] qemu: Add qemuDomainSetMaxMemLock helper
459f93
MIME-Version: 1.0
459f93
Content-Type: text/plain; charset=UTF-8
459f93
Content-Transfer-Encoding: 8bit
459f93
459f93
qemuDomainAdjustMaxMemLock combined computing the desired limit with
459f93
applying it. This patch separates the code to apply a memory locking
459f93
limit to a new qemuDomainSetMaxMemLock helper for better reusability.
459f93
459f93
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
459f93
Reviewed-by: Ján Tomko <jtomko@redhat.com>
459f93
(cherry picked from commit dff51c7f5760ded8235076f55d082fe4363f2f78)
459f93
459f93
https://bugzilla.redhat.com/show_bug.cgi?id=2089433
459f93
459f93
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
459f93
---
459f93
 src/qemu/qemu_domain.c | 95 ++++++++++++++++++++++++++----------------
459f93
 src/qemu/qemu_domain.h |  3 ++
459f93
 2 files changed, 61 insertions(+), 37 deletions(-)
459f93
459f93
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
459f93
index ee7d310903..a81789f194 100644
459f93
--- a/src/qemu/qemu_domain.c
459f93
+++ b/src/qemu/qemu_domain.c
459f93
@@ -9261,6 +9261,61 @@ qemuDomainGetMemLockLimitBytes(virDomainDef *def,
459f93
 }
459f93
 
459f93
 
459f93
+/**
459f93
+ * qemuDomainSetMaxMemLock:
459f93
+ * @vm: domain
459f93
+ * @limit: the desired memory locking limit
459f93
+ * @origPtr: where to store (or load from) the original value of the limit
459f93
+ *
459f93
+ * Set the memory locking limit for @vm unless it's already big enough. If
459f93
+ * @origPtr is non-NULL, the original value of the limit will be store there
459f93
+ * and can be restored by calling this function with @limit == 0.
459f93
+ *
459f93
+ * Returns: 0 on success, -1 otherwise.
459f93
+ */
459f93
+int
459f93
+qemuDomainSetMaxMemLock(virDomainObj *vm,
459f93
+                        unsigned long long limit,
459f93
+                        unsigned long long *origPtr)
459f93
+{
459f93
+    unsigned long long current = 0;
459f93
+
459f93
+    if (virProcessGetMaxMemLock(vm->pid, &current) < 0)
459f93
+        return -1;
459f93
+
459f93
+    if (limit > 0) {
459f93
+        VIR_DEBUG("Requested memory lock limit: %llu", limit);
459f93
+        /* If the limit is already high enough, we can assume
459f93
+         * that some external process is taking care of managing
459f93
+         * process limits and we shouldn't do anything ourselves:
459f93
+         * we're probably running in a containerized environment
459f93
+         * where we don't have enough privilege anyway */
459f93
+        if (current >= limit) {
459f93
+            VIR_DEBUG("Current limit %llu is big enough", current);
459f93
+            return 0;
459f93
+        }
459f93
+
459f93
+        /* If this is the first time adjusting the limit, save the current
459f93
+         * value so that we can restore it once memory locking is no longer
459f93
+         * required */
459f93
+        if (origPtr && *origPtr == 0)
459f93
+            *origPtr = current;
459f93
+    } else {
459f93
+        /* Once memory locking is no longer required, we can restore the
459f93
+         * original, usually very low, limit. But only if we actually stored
459f93
+         * the original limit before. */
459f93
+        if (!origPtr || *origPtr == 0)
459f93
+            return 0;
459f93
+
459f93
+        limit = *origPtr;
459f93
+        *origPtr = 0;
459f93
+        VIR_DEBUG("Resetting memory lock limit back to %llu", limit);
459f93
+    }
459f93
+
459f93
+    return virProcessSetMaxMemLock(vm->pid, limit);
459f93
+}
459f93
+
459f93
+
459f93
 /**
459f93
  * qemuDomainAdjustMaxMemLock:
459f93
  * @vm: domain
459f93
@@ -9282,43 +9337,9 @@ int
459f93
 qemuDomainAdjustMaxMemLock(virDomainObj *vm,
459f93
                            bool forceVFIO)
459f93
 {
459f93
-    qemuDomainObjPrivate *priv = vm->privateData;
459f93
-    unsigned long long currentMemLock = 0;
459f93
-    unsigned long long desiredMemLock = 0;
459f93
-
459f93
-    desiredMemLock = qemuDomainGetMemLockLimitBytes(vm->def, forceVFIO);
459f93
-    if (virProcessGetMaxMemLock(vm->pid, &currentMemLock) < 0)
459f93
-        return -1;
459f93
-
459f93
-    if (desiredMemLock > 0) {
459f93
-        if (currentMemLock < desiredMemLock) {
459f93
-            /* If this is the first time adjusting the limit, save the current
459f93
-             * value so that we can restore it once memory locking is no longer
459f93
-             * required */
459f93
-            if (priv->originalMemlock == 0) {
459f93
-                priv->originalMemlock = currentMemLock;
459f93
-            }
459f93
-        } else {
459f93
-            /* If the limit is already high enough, we can assume
459f93
-             * that some external process is taking care of managing
459f93
-             * process limits and we shouldn't do anything ourselves:
459f93
-             * we're probably running in a containerized environment
459f93
-             * where we don't have enough privilege anyway */
459f93
-            desiredMemLock = 0;
459f93
-        }
459f93
-    } else {
459f93
-        /* Once memory locking is no longer required, we can restore the
459f93
-         * original, usually very low, limit */
459f93
-        desiredMemLock = priv->originalMemlock;
459f93
-        priv->originalMemlock = 0;
459f93
-    }
459f93
-
459f93
-    if (desiredMemLock > 0 &&
459f93
-        virProcessSetMaxMemLock(vm->pid, desiredMemLock) < 0) {
459f93
-        return -1;
459f93
-    }
459f93
-
459f93
-    return 0;
459f93
+    return qemuDomainSetMaxMemLock(vm,
459f93
+                                   qemuDomainGetMemLockLimitBytes(vm->def, forceVFIO),
459f93
+                                   &QEMU_DOMAIN_PRIVATE(vm)->originalMemlock);
459f93
 }
459f93
 
459f93
 
459f93
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
459f93
index e9497d20de..6d1d23439a 100644
459f93
--- a/src/qemu/qemu_domain.h
459f93
+++ b/src/qemu/qemu_domain.h
459f93
@@ -789,6 +789,9 @@ int qemuDomainAdjustMaxMemLock(virDomainObj *vm,
459f93
                                bool forceVFIO);
459f93
 int qemuDomainAdjustMaxMemLockHostdev(virDomainObj *vm,
459f93
                                       virDomainHostdevDef *hostdev);
459f93
+int qemuDomainSetMaxMemLock(virDomainObj *vm,
459f93
+                            unsigned long long limit,
459f93
+                            unsigned long long *origPtr);
459f93
 
459f93
 int qemuDomainDefValidateMemoryHotplug(const virDomainDef *def,
459f93
                                        const virDomainMemoryDef *mem);
459f93
-- 
459f93
2.35.1
459f93