render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Pablo Greco 40546a
From 5101d214a63b31847f80048d4ee34b5e62176ddd Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <5101d214a63b31847f80048d4ee34b5e62176ddd@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:06:52 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: extract virCgroupV1(Set|Get)Memory*Limit
Pablo Greco 40546a
MIME-Version: 1.0
Pablo Greco 40546a
Content-Type: text/plain; charset=UTF-8
Pablo Greco 40546a
Content-Transfer-Encoding: 8bit
Pablo Greco 40546a
Pablo Greco 40546a
They all need virCgroupV1GetMemoryUnlimitedKB() so it's easier to
Pablo Greco 40546a
move them in one commit.
Pablo Greco 40546a
Pablo Greco 40546a
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit 900c58b7f963d5054921e31bbd0b6005155b284a)
Pablo Greco 40546a
Pablo Greco 40546a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Message-Id: <c85e4973bb1718a5c9a36f971f9544b46b213183.1561993100.git.phrdina@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 src/util/vircgroup.c        | 128 ++-------------------------
Pablo Greco 40546a
 src/util/vircgroupbackend.h |  30 +++++++
Pablo Greco 40546a
 src/util/vircgrouppriv.h    |   6 ++
Pablo Greco 40546a
 src/util/vircgroupv1.c      | 168 ++++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 4 files changed, 211 insertions(+), 121 deletions(-)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
Pablo Greco 40546a
index ed7252243c..6708eef0da 100644
Pablo Greco 40546a
--- a/src/util/vircgroup.c
Pablo Greco 40546a
+++ b/src/util/vircgroup.c
Pablo Greco 40546a
@@ -634,7 +634,7 @@ virCgroupMakeGroup(virCgroupPtr parent,
Pablo Greco 40546a
  *
Pablo Greco 40546a
  * Returns 0 on success, -1 on error
Pablo Greco 40546a
  */
Pablo Greco 40546a
-static int
Pablo Greco 40546a
+int
Pablo Greco 40546a
 virCgroupNew(pid_t pid,
Pablo Greco 40546a
              const char *path,
Pablo Greco 40546a
              virCgroupPtr parent,
Pablo Greco 40546a
@@ -1537,51 +1537,6 @@ virCgroupGetBlkioDeviceWeight(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-/*
Pablo Greco 40546a
- * Retrieve the "memory.limit_in_bytes" value from the memory controller
Pablo Greco 40546a
- * root dir. This value cannot be modified by userspace and therefore
Pablo Greco 40546a
- * is the maximum limit value supported by cgroups on the local system.
Pablo Greco 40546a
- * Returns this value scaled to KB or falls back to the original
Pablo Greco 40546a
- * VIR_DOMAIN_MEMORY_PARAM_UNLIMITED. Either way, remember the return
Pablo Greco 40546a
- * value to avoid unnecessary cgroup filesystem access.
Pablo Greco 40546a
- */
Pablo Greco 40546a
-static unsigned long long int virCgroupMemoryUnlimitedKB;
Pablo Greco 40546a
-static virOnceControl virCgroupMemoryOnce = VIR_ONCE_CONTROL_INITIALIZER;
Pablo Greco 40546a
-
Pablo Greco 40546a
-static void
Pablo Greco 40546a
-virCgroupMemoryOnceInit(void)
Pablo Greco 40546a
-{
Pablo Greco 40546a
-    virCgroupPtr group;
Pablo Greco 40546a
-    unsigned long long int mem_unlimited = 0ULL;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virCgroupNew(-1, "/", NULL, -1, &group) < 0)
Pablo Greco 40546a
-        goto cleanup;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (!virCgroupHasController(group, VIR_CGROUP_CONTROLLER_MEMORY))
Pablo Greco 40546a
-        goto cleanup;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    ignore_value(virCgroupGetValueU64(group,
Pablo Greco 40546a
-                                      VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
-                                      "memory.limit_in_bytes",
Pablo Greco 40546a
-                                      &mem_unlimited));
Pablo Greco 40546a
- cleanup:
Pablo Greco 40546a
-    virCgroupFree(&group);
Pablo Greco 40546a
-    virCgroupMemoryUnlimitedKB = mem_unlimited >> 10;
Pablo Greco 40546a
-}
Pablo Greco 40546a
-
Pablo Greco 40546a
-static unsigned long long int
Pablo Greco 40546a
-virCgroupGetMemoryUnlimitedKB(void)
Pablo Greco 40546a
-{
Pablo Greco 40546a
-    if (virOnce(&virCgroupMemoryOnce, virCgroupMemoryOnceInit) < 0)
Pablo Greco 40546a
-        VIR_DEBUG("Init failed, will fall back to defaults.");
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virCgroupMemoryUnlimitedKB)
Pablo Greco 40546a
-        return virCgroupMemoryUnlimitedKB;
Pablo Greco 40546a
-    else
Pablo Greco 40546a
-        return VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
-}
Pablo Greco 40546a
-
Pablo Greco 40546a
-
Pablo Greco 40546a
 /**
Pablo Greco 40546a
  * virCgroupSetMemory:
Pablo Greco 40546a
  *
Pablo Greco 40546a
@@ -1652,7 +1607,7 @@ virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb)
Pablo Greco 40546a
 int
Pablo Greco 40546a
 virCgroupSetMemoryHardLimit(virCgroupPtr group, unsigned long long kb)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    return virCgroupSetMemory(group, kb);
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, setMemoryHardLimit, -1, kb);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1667,18 +1622,7 @@ virCgroupSetMemoryHardLimit(virCgroupPtr group, unsigned long long kb)
Pablo Greco 40546a
 int
Pablo Greco 40546a
 virCgroupGetMemoryHardLimit(virCgroupPtr group, unsigned long long *kb)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    long long unsigned int limit_in_bytes;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virCgroupGetValueU64(group,
Pablo Greco 40546a
-                             VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
-                             "memory.limit_in_bytes", &limit_in_bytes) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    *kb = limit_in_bytes >> 10;
Pablo Greco 40546a
-    if (*kb >= virCgroupGetMemoryUnlimitedKB())
Pablo Greco 40546a
-        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    return 0;
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, getMemoryHardLimit, -1, kb);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1693,25 +1637,7 @@ virCgroupGetMemoryHardLimit(virCgroupPtr group, unsigned long long *kb)
Pablo Greco 40546a
 int
Pablo Greco 40546a
 virCgroupSetMemorySoftLimit(virCgroupPtr group, unsigned long long kb)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (kb > maxkb) {
Pablo Greco 40546a
-        virReportError(VIR_ERR_INVALID_ARG,
Pablo Greco 40546a
-                       _("Memory '%llu' must be less than %llu"),
Pablo Greco 40546a
-                       kb, maxkb);
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (kb == maxkb)
Pablo Greco 40546a
-        return virCgroupSetValueI64(group,
Pablo Greco 40546a
-                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
-                                    "memory.soft_limit_in_bytes",
Pablo Greco 40546a
-                                    -1);
Pablo Greco 40546a
-    else
Pablo Greco 40546a
-        return virCgroupSetValueU64(group,
Pablo Greco 40546a
-                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
-                                    "memory.soft_limit_in_bytes",
Pablo Greco 40546a
-                                    kb << 10);
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, setMemorySoftLimit, -1, kb);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1726,18 +1652,7 @@ virCgroupSetMemorySoftLimit(virCgroupPtr group, unsigned long long kb)
Pablo Greco 40546a
 int
Pablo Greco 40546a
 virCgroupGetMemorySoftLimit(virCgroupPtr group, unsigned long long *kb)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    long long unsigned int limit_in_bytes;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virCgroupGetValueU64(group,
Pablo Greco 40546a
-                             VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
-                             "memory.soft_limit_in_bytes", &limit_in_bytes) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    *kb = limit_in_bytes >> 10;
Pablo Greco 40546a
-    if (*kb >= virCgroupGetMemoryUnlimitedKB())
Pablo Greco 40546a
-        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    return 0;
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, getMemorySoftLimit, -1, kb);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1752,25 +1667,7 @@ virCgroupGetMemorySoftLimit(virCgroupPtr group, unsigned long long *kb)
Pablo Greco 40546a
 int
Pablo Greco 40546a
 virCgroupSetMemSwapHardLimit(virCgroupPtr group, unsigned long long kb)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (kb > maxkb) {
Pablo Greco 40546a
-        virReportError(VIR_ERR_INVALID_ARG,
Pablo Greco 40546a
-                       _("Memory '%llu' must be less than %llu"),
Pablo Greco 40546a
-                       kb, maxkb);
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (kb == maxkb)
Pablo Greco 40546a
-        return virCgroupSetValueI64(group,
Pablo Greco 40546a
-                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
-                                    "memory.memsw.limit_in_bytes",
Pablo Greco 40546a
-                                    -1);
Pablo Greco 40546a
-    else
Pablo Greco 40546a
-        return virCgroupSetValueU64(group,
Pablo Greco 40546a
-                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
-                                    "memory.memsw.limit_in_bytes",
Pablo Greco 40546a
-                                    kb << 10);
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, setMemSwapHardLimit, -1, kb);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1785,18 +1682,7 @@ virCgroupSetMemSwapHardLimit(virCgroupPtr group, unsigned long long kb)
Pablo Greco 40546a
 int
Pablo Greco 40546a
 virCgroupGetMemSwapHardLimit(virCgroupPtr group, unsigned long long *kb)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    long long unsigned int limit_in_bytes;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virCgroupGetValueU64(group,
Pablo Greco 40546a
-                             VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
-                             "memory.memsw.limit_in_bytes", &limit_in_bytes) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    *kb = limit_in_bytes >> 10;
Pablo Greco 40546a
-    if (*kb >= virCgroupGetMemoryUnlimitedKB())
Pablo Greco 40546a
-        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    return 0;
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, getMemSwapHardLimit, -1, kb);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
Pablo Greco 40546a
index 8bdfc5c835..cdbca4b907 100644
Pablo Greco 40546a
--- a/src/util/vircgroupbackend.h
Pablo Greco 40546a
+++ b/src/util/vircgroupbackend.h
Pablo Greco 40546a
@@ -227,6 +227,30 @@ typedef int
Pablo Greco 40546a
 (*virCgroupGetMemoryUsageCB)(virCgroupPtr group,
Pablo Greco 40546a
                              unsigned long *kb);
Pablo Greco 40546a
 
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupSetMemoryHardLimitCB)(virCgroupPtr group,
Pablo Greco 40546a
+                                 unsigned long long kb);
Pablo Greco 40546a
+
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupGetMemoryHardLimitCB)(virCgroupPtr group,
Pablo Greco 40546a
+                                 unsigned long long *kb);
Pablo Greco 40546a
+
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupSetMemorySoftLimitCB)(virCgroupPtr group,
Pablo Greco 40546a
+                                 unsigned long long kb);
Pablo Greco 40546a
+
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupGetMemorySoftLimitCB)(virCgroupPtr group,
Pablo Greco 40546a
+                                 unsigned long long *kb);
Pablo Greco 40546a
+
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupSetMemSwapHardLimitCB)(virCgroupPtr group,
Pablo Greco 40546a
+                                  unsigned long long kb);
Pablo Greco 40546a
+
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupGetMemSwapHardLimitCB)(virCgroupPtr group,
Pablo Greco 40546a
+                                  unsigned long long *kb);
Pablo Greco 40546a
+
Pablo Greco 40546a
 struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupBackendType type;
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -269,6 +293,12 @@ struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupSetMemoryCB setMemory;
Pablo Greco 40546a
     virCgroupGetMemoryStatCB getMemoryStat;
Pablo Greco 40546a
     virCgroupGetMemoryUsageCB getMemoryUsage;
Pablo Greco 40546a
+    virCgroupSetMemoryHardLimitCB setMemoryHardLimit;
Pablo Greco 40546a
+    virCgroupGetMemoryHardLimitCB getMemoryHardLimit;
Pablo Greco 40546a
+    virCgroupSetMemorySoftLimitCB setMemorySoftLimit;
Pablo Greco 40546a
+    virCgroupGetMemorySoftLimitCB getMemorySoftLimit;
Pablo Greco 40546a
+    virCgroupSetMemSwapHardLimitCB setMemSwapHardLimit;
Pablo Greco 40546a
+    virCgroupGetMemSwapHardLimitCB getMemSwapHardLimit;
Pablo Greco 40546a
 };
Pablo Greco 40546a
 typedef struct _virCgroupBackend virCgroupBackend;
Pablo Greco 40546a
 typedef virCgroupBackend *virCgroupBackendPtr;
Pablo Greco 40546a
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
Pablo Greco 40546a
index 3a968c1ce2..7b985280e1 100644
Pablo Greco 40546a
--- a/src/util/vircgrouppriv.h
Pablo Greco 40546a
+++ b/src/util/vircgrouppriv.h
Pablo Greco 40546a
@@ -88,6 +88,12 @@ int virCgroupGetValueForBlkDev(virCgroupPtr group,
Pablo Greco 40546a
                                const char *path,
Pablo Greco 40546a
                                char **value);
Pablo Greco 40546a
 
Pablo Greco 40546a
+int virCgroupNew(pid_t pid,
Pablo Greco 40546a
+                 const char *path,
Pablo Greco 40546a
+                 virCgroupPtr parent,
Pablo Greco 40546a
+                 int controllers,
Pablo Greco 40546a
+                 virCgroupPtr *group);
Pablo Greco 40546a
+
Pablo Greco 40546a
 int virCgroupNewPartition(const char *path,
Pablo Greco 40546a
                           bool create,
Pablo Greco 40546a
                           int controllers,
Pablo Greco 40546a
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
Pablo Greco 40546a
index 4f1d79a758..779288fb97 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv1.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv1.c
Pablo Greco 40546a
@@ -1378,6 +1378,51 @@ virCgroupV1GetBlkioDeviceWriteBps(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+/*
Pablo Greco 40546a
+ * Retrieve the "memory.limit_in_bytes" value from the memory controller
Pablo Greco 40546a
+ * root dir. This value cannot be modified by userspace and therefore
Pablo Greco 40546a
+ * is the maximum limit value supported by cgroups on the local system.
Pablo Greco 40546a
+ * Returns this value scaled to KB or falls back to the original
Pablo Greco 40546a
+ * VIR_DOMAIN_MEMORY_PARAM_UNLIMITED. Either way, remember the return
Pablo Greco 40546a
+ * value to avoid unnecessary cgroup filesystem access.
Pablo Greco 40546a
+ */
Pablo Greco 40546a
+static unsigned long long int virCgroupV1MemoryUnlimitedKB;
Pablo Greco 40546a
+static virOnceControl virCgroupV1MemoryOnce = VIR_ONCE_CONTROL_INITIALIZER;
Pablo Greco 40546a
+
Pablo Greco 40546a
+static void
Pablo Greco 40546a
+virCgroupV1MemoryOnceInit(void)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    virCgroupPtr group;
Pablo Greco 40546a
+    unsigned long long int mem_unlimited = 0ULL;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupNew(-1, "/", NULL, -1, &group) < 0)
Pablo Greco 40546a
+        goto cleanup;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (!virCgroupV1HasController(group, VIR_CGROUP_CONTROLLER_MEMORY))
Pablo Greco 40546a
+        goto cleanup;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    ignore_value(virCgroupGetValueU64(group,
Pablo Greco 40546a
+                                      VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                                      "memory.limit_in_bytes",
Pablo Greco 40546a
+                                      &mem_unlimited));
Pablo Greco 40546a
+ cleanup:
Pablo Greco 40546a
+    virCgroupFree(&group);
Pablo Greco 40546a
+    virCgroupV1MemoryUnlimitedKB = mem_unlimited >> 10;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+static unsigned long long int
Pablo Greco 40546a
+virCgroupV1GetMemoryUnlimitedKB(void)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    if (virOnce(&virCgroupV1MemoryOnce, virCgroupV1MemoryOnceInit) < 0)
Pablo Greco 40546a
+        VIR_DEBUG("Init failed, will fall back to defaults.");
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupV1MemoryUnlimitedKB)
Pablo Greco 40546a
+        return virCgroupV1MemoryUnlimitedKB;
Pablo Greco 40546a
+    else
Pablo Greco 40546a
+        return VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 static int
Pablo Greco 40546a
 virCgroupV1SetMemory(virCgroupPtr group,
Pablo Greco 40546a
                      unsigned long long kb)
Pablo Greco 40546a
@@ -1494,6 +1539,123 @@ virCgroupV1GetMemoryUsage(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1SetMemoryHardLimit(virCgroupPtr group,
Pablo Greco 40546a
+                              unsigned long long kb)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    return virCgroupV1SetMemory(group, kb);
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1GetMemoryHardLimit(virCgroupPtr group,
Pablo Greco 40546a
+                              unsigned long long *kb)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    long long unsigned int limit_in_bytes;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupGetValueU64(group,
Pablo Greco 40546a
+                             VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                             "memory.limit_in_bytes", &limit_in_bytes) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    *kb = limit_in_bytes >> 10;
Pablo Greco 40546a
+    if (*kb >= virCgroupV1GetMemoryUnlimitedKB())
Pablo Greco 40546a
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1SetMemorySoftLimit(virCgroupPtr group,
Pablo Greco 40546a
+                              unsigned long long kb)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (kb > maxkb) {
Pablo Greco 40546a
+        virReportError(VIR_ERR_INVALID_ARG,
Pablo Greco 40546a
+                       _("Memory '%llu' must be less than %llu"),
Pablo Greco 40546a
+                       kb, maxkb);
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (kb == maxkb)
Pablo Greco 40546a
+        return virCgroupSetValueI64(group,
Pablo Greco 40546a
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                                    "memory.soft_limit_in_bytes",
Pablo Greco 40546a
+                                    -1);
Pablo Greco 40546a
+    else
Pablo Greco 40546a
+        return virCgroupSetValueU64(group,
Pablo Greco 40546a
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                                    "memory.soft_limit_in_bytes",
Pablo Greco 40546a
+                                    kb << 10);
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1GetMemorySoftLimit(virCgroupPtr group,
Pablo Greco 40546a
+                              unsigned long long *kb)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    long long unsigned int limit_in_bytes;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupGetValueU64(group,
Pablo Greco 40546a
+                             VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                             "memory.soft_limit_in_bytes", &limit_in_bytes) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    *kb = limit_in_bytes >> 10;
Pablo Greco 40546a
+    if (*kb >= virCgroupV1GetMemoryUnlimitedKB())
Pablo Greco 40546a
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1SetMemSwapHardLimit(virCgroupPtr group,
Pablo Greco 40546a
+                               unsigned long long kb)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (kb > maxkb) {
Pablo Greco 40546a
+        virReportError(VIR_ERR_INVALID_ARG,
Pablo Greco 40546a
+                       _("Memory '%llu' must be less than %llu"),
Pablo Greco 40546a
+                       kb, maxkb);
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (kb == maxkb)
Pablo Greco 40546a
+        return virCgroupSetValueI64(group,
Pablo Greco 40546a
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                                    "memory.memsw.limit_in_bytes",
Pablo Greco 40546a
+                                    -1);
Pablo Greco 40546a
+    else
Pablo Greco 40546a
+        return virCgroupSetValueU64(group,
Pablo Greco 40546a
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                                    "memory.memsw.limit_in_bytes",
Pablo Greco 40546a
+                                    kb << 10);
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1GetMemSwapHardLimit(virCgroupPtr group,
Pablo Greco 40546a
+                               unsigned long long *kb)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    long long unsigned int limit_in_bytes;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupGetValueU64(group,
Pablo Greco 40546a
+                             VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                             "memory.memsw.limit_in_bytes", &limit_in_bytes) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    *kb = limit_in_bytes >> 10;
Pablo Greco 40546a
+    if (*kb >= virCgroupV1GetMemoryUnlimitedKB())
Pablo Greco 40546a
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1534,6 +1696,12 @@ virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .setMemory = virCgroupV1SetMemory,
Pablo Greco 40546a
     .getMemoryStat = virCgroupV1GetMemoryStat,
Pablo Greco 40546a
     .getMemoryUsage = virCgroupV1GetMemoryUsage,
Pablo Greco 40546a
+    .setMemoryHardLimit = virCgroupV1SetMemoryHardLimit,
Pablo Greco 40546a
+    .getMemoryHardLimit = virCgroupV1GetMemoryHardLimit,
Pablo Greco 40546a
+    .setMemorySoftLimit = virCgroupV1SetMemorySoftLimit,
Pablo Greco 40546a
+    .getMemorySoftLimit = virCgroupV1GetMemorySoftLimit,
Pablo Greco 40546a
+    .setMemSwapHardLimit = virCgroupV1SetMemSwapHardLimit,
Pablo Greco 40546a
+    .getMemSwapHardLimit = virCgroupV1GetMemSwapHardLimit,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a