render / rpms / libvirt

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