render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
7548c0
From 205289d2792aacf68ed2cb8563d1860bd36137a0 Mon Sep 17 00:00:00 2001
7548c0
Message-Id: <205289d2792aacf68ed2cb8563d1860bd36137a0@dist-git>
7548c0
From: Pavel Hrdina <phrdina@redhat.com>
7548c0
Date: Fri, 19 Feb 2021 13:33:55 +0100
7548c0
Subject: [PATCH] vircgroup: use DBus call to systemd for some APIs
7548c0
MIME-Version: 1.0
7548c0
Content-Type: text/plain; charset=UTF-8
7548c0
Content-Transfer-Encoding: 8bit
7548c0
7548c0
When running on host with systemd we register VMs with machined.
7548c0
In this case systemd creates the root VM cgroup for us. This has some
7548c0
implications where one of them is that systemd owns all files inside
7548c0
the root VM cgroup and we should not touch them.
7548c0
7548c0
If we change any value in file that systemd knows about it will be
7548c0
changed to what systemd thinks it should be when executing
7548c0
`systemctl daemon-reload`.
7548c0
7548c0
These are the APIs that we need to call using systemd because they set
7548c0
limits that are proportional to sibling cgroups.
7548c0
7548c0
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
7548c0
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
7548c0
(cherry picked from commit 9c1693eff427661616ce1bd2795688f87288a412)
7548c0
7548c0
Conflicts:
7548c0
    src/util/vircgroup.c
7548c0
        - missing upstream g_autofree rewrite
7548c0
        - missing upstream glib dbus rewrite, hence the ugly macro
7548c0
          instead of a function
7548c0
7548c0
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
7548c0
7548c0
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
7548c0
Message-Id: <5d22d307112333f1da565cb642ea9001a7b8b55b.1613737828.git.phrdina@redhat.com>
7548c0
Reviewed-by: Ján Tomko <jtomko@redhat.com>
7548c0
---
7548c0
 src/util/vircgroup.c     | 11 ++++++++++
7548c0
 src/util/vircgrouppriv.h | 25 +++++++++++++++++++++++
7548c0
 src/util/vircgroupv1.c   | 44 +++++++++++++++++++++++++++-------------
7548c0
 src/util/vircgroupv2.c   | 44 +++++++++++++++++++++++++++-------------
7548c0
 tests/Makefile.am        |  1 +
7548c0
 5 files changed, 97 insertions(+), 28 deletions(-)
7548c0
7548c0
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
7548c0
index a45c2e7f2f..10b934291c 100644
7548c0
--- a/src/util/vircgroup.c
7548c0
+++ b/src/util/vircgroup.c
7548c0
@@ -1027,6 +1027,10 @@ virCgroupNewDetectMachine(const char *name,
7548c0
         }
7548c0
     }
7548c0
 
7548c0
+    (*group)->unitName = virSystemdGetMachineUnitByPID(pid);
7548c0
+    if (virSystemdHasMachined() == 0 && !(*group)->unitName)
7548c0
+        return -1;
7548c0
+
7548c0
     return 0;
7548c0
 }
7548c0
 
7548c0
@@ -1146,6 +1150,12 @@ virCgroupNewMachineSystemd(const char *name,
7548c0
         return -1;
7548c0
     }
7548c0
 
7548c0
+    (*group)->unitName = virSystemdGetMachineUnitByPID(pidleader);
7548c0
+    if (!(*group)->unitName) {
7548c0
+        virCgroupFree(group);
7548c0
+        return -1;
7548c0
+    }
7548c0
+
7548c0
     if (virCgroupAddProcess(*group, pidleader) < 0) {
7548c0
         virErrorPtr saved;
7548c0
 
7548c0
@@ -3553,6 +3563,7 @@ virCgroupFree(virCgroupPtr *group)
7548c0
 
7548c0
     VIR_FREE((*group)->unified.mountPoint);
7548c0
     VIR_FREE((*group)->unified.placement);
7548c0
+    VIR_FREE((*group)->unitName);
7548c0
 
7548c0
     VIR_FREE((*group)->path);
7548c0
     VIR_FREE(*group);
7548c0
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
7548c0
index f2a80aeb82..b4a9e0b379 100644
7548c0
--- a/src/util/vircgrouppriv.h
7548c0
+++ b/src/util/vircgrouppriv.h
7548c0
@@ -27,6 +27,7 @@
7548c0
 
7548c0
 #include "vircgroup.h"
7548c0
 #include "vircgroupbackend.h"
7548c0
+#include "virdbus.h"
7548c0
 
7548c0
 struct _virCgroupV1Controller {
7548c0
     int type;
7548c0
@@ -66,8 +67,32 @@ struct _virCgroup {
7548c0
 
7548c0
     virCgroupV1Controller legacy[VIR_CGROUP_CONTROLLER_LAST];
7548c0
     virCgroupV2Controller unified;
7548c0
+
7548c0
+    char *unitName;
7548c0
 };
7548c0
 
7548c0
+#define virCgroupSetValueDBus(unitName, key, ...) \
7548c0
+    ({ \
7548c0
+        int __ret = -1; \
7548c0
+        do { \
7548c0
+            DBusConnection *__conn; \
7548c0
+            if (!(__conn = virDBusGetSystemBus())) \
7548c0
+                break; \
7548c0
+            __ret = virDBusCallMethod(__conn, NULL, NULL, \
7548c0
+                                      "org.freedesktop.systemd1", \
7548c0
+                                      "/org/freedesktop/systemd1", \
7548c0
+                                      "org.freedesktop.systemd1.Manager", \
7548c0
+                                      "SetUnitProperties", \
7548c0
+                                      "sba(sv)", \
7548c0
+                                      unitName, \
7548c0
+                                      true, \
7548c0
+                                      1, \
7548c0
+                                      key, \
7548c0
+                                      __VA_ARGS__); \
7548c0
+        } while (0); \
7548c0
+        __ret; \
7548c0
+    })
7548c0
+
7548c0
 int virCgroupSetValueRaw(const char *path,
7548c0
                          const char *value);
7548c0
 
7548c0
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
7548c0
index c35088a3c4..7ec8f3a316 100644
7548c0
--- a/src/util/vircgroupv1.c
7548c0
+++ b/src/util/vircgroupv1.c
7548c0
@@ -931,7 +931,6 @@ virCgroupV1SetBlkioWeight(virCgroupPtr group,
7548c0
                           unsigned int weight)
7548c0
 {
7548c0
     g_autofree char *path = NULL;
7548c0
-    g_autofree char *value = NULL;
7548c0
 
7548c0
     if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
7548c0
                                     "blkio.bfq.weight", &path) < 0) {
7548c0
@@ -953,9 +952,14 @@ virCgroupV1SetBlkioWeight(virCgroupPtr group,
7548c0
         return -1;
7548c0
     }
7548c0
 
7548c0
-    value = g_strdup_printf("%u", weight);
7548c0
+    if (group->unitName) {
7548c0
+        return virCgroupSetValueDBus(group->unitName, "BlockIOWeight",
7548c0
+                                     "t", (unsigned long long) weight);
7548c0
+    } else {
7548c0
+        g_autofree char *value = g_strdup_printf("%u", weight);
7548c0
 
7548c0
-    return virCgroupSetValueRaw(path, value);
7548c0
+        return virCgroupSetValueRaw(path, value);
7548c0
+    }
7548c0
 }
7548c0
 
7548c0
 
7548c0
@@ -1188,15 +1192,8 @@ virCgroupV1SetBlkioDeviceWeight(virCgroupPtr group,
7548c0
                                 const char *devPath,
7548c0
                                 unsigned int weight)
7548c0
 {
7548c0
-    g_autofree char *str = NULL;
7548c0
-    g_autofree char *blkstr = NULL;
7548c0
     g_autofree char *path = NULL;
7548c0
 
7548c0
-    if (!(blkstr = virCgroupGetBlockDevString(devPath)))
7548c0
-        return -1;
7548c0
-
7548c0
-    str = g_strdup_printf("%s%d", blkstr, weight);
7548c0
-
7548c0
     if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
7548c0
                                     "blkio.weight_device", &path) < 0) {
7548c0
         return -1;
7548c0
@@ -1208,7 +1205,21 @@ virCgroupV1SetBlkioDeviceWeight(virCgroupPtr group,
7548c0
         return -1;
7548c0
     }
7548c0
 
7548c0
-    return virCgroupSetValueRaw(path, str);
7548c0
+    if (group->unitName) {
7548c0
+        return virCgroupSetValueDBus(group->unitName, "BlockIODeviceWeight",
7548c0
+                                     "a(st)",
7548c0
+                                     1, path, (unsigned long long) weight);
7548c0
+    } else {
7548c0
+        g_autofree char *str = NULL;
7548c0
+        g_autofree char *blkstr = NULL;
7548c0
+
7548c0
+        if (!(blkstr = virCgroupGetBlockDevString(devPath)))
7548c0
+            return -1;
7548c0
+
7548c0
+        str = g_strdup_printf("%s%d", blkstr, weight);
7548c0
+
7548c0
+        return virCgroupSetValueRaw(path, str);
7548c0
+    }
7548c0
 }
7548c0
 
7548c0
 
7548c0
@@ -1849,9 +1860,14 @@ static int
7548c0
 virCgroupV1SetCpuShares(virCgroupPtr group,
7548c0
                         unsigned long long shares)
7548c0
 {
7548c0
-    return virCgroupSetValueU64(group,
7548c0
-                                VIR_CGROUP_CONTROLLER_CPU,
7548c0
-                                "cpu.shares", shares);
7548c0
+    if (group->unitName) {
7548c0
+        return virCgroupSetValueDBus(group->unitName, "CPUShares",
7548c0
+                                     "t", shares);
7548c0
+    } else {
7548c0
+        return virCgroupSetValueU64(group,
7548c0
+                                    VIR_CGROUP_CONTROLLER_CPU,
7548c0
+                                    "cpu.shares", shares);
7548c0
+    }
7548c0
 }
7548c0
 
7548c0
 
7548c0
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
7548c0
index 4682a6a920..8fe4894a9e 100644
7548c0
--- a/src/util/vircgroupv2.c
7548c0
+++ b/src/util/vircgroupv2.c
7548c0
@@ -606,7 +606,6 @@ virCgroupV2SetBlkioWeight(virCgroupPtr group,
7548c0
                           unsigned int weight)
7548c0
 {
7548c0
     g_autofree char *path = NULL;
7548c0
-    g_autofree char *value = NULL;
7548c0
     const char *format = "%u";
7548c0
 
7548c0
     if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
7548c0
@@ -630,9 +629,14 @@ virCgroupV2SetBlkioWeight(virCgroupPtr group,
7548c0
         return -1;
7548c0
     }
7548c0
 
7548c0
-    value = g_strdup_printf(format, weight);
7548c0
+    if (group->unitName) {
7548c0
+        return virCgroupSetValueDBus(group->unitName, "IOWeight",
7548c0
+                                     "t", (unsigned long long) weight);
7548c0
+    } else {
7548c0
+        g_autofree char *value = g_strdup_printf(format, weight);
7548c0
 
7548c0
-    return virCgroupSetValueRaw(path, value);
7548c0
+        return virCgroupSetValueRaw(path, value);
7548c0
+    }
7548c0
 }
7548c0
 
7548c0
 
7548c0
@@ -817,13 +821,6 @@ virCgroupV2SetBlkioDeviceWeight(virCgroupPtr group,
7548c0
                                 unsigned int weight)
7548c0
 {
7548c0
     g_autofree char *path = NULL;
7548c0
-    g_autofree char *str = NULL;
7548c0
-    g_autofree char *blkstr = NULL;
7548c0
-
7548c0
-    if (!(blkstr = virCgroupGetBlockDevString(devPath)))
7548c0
-        return -1;
7548c0
-
7548c0
-    str = g_strdup_printf("%s%d", blkstr, weight);
7548c0
 
7548c0
     if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
7548c0
                                     "io.weight", &path) < 0) {
7548c0
@@ -836,7 +833,21 @@ virCgroupV2SetBlkioDeviceWeight(virCgroupPtr group,
7548c0
         return -1;
7548c0
     }
7548c0
 
7548c0
-    return virCgroupSetValueRaw(path, str);
7548c0
+    if (group->unitName) {
7548c0
+        return virCgroupSetValueDBus(group->unitName, "IODeviceWeight",
7548c0
+                                     "a(st)",
7548c0
+                                     1, path, (unsigned long long) weight);
7548c0
+    } else {
7548c0
+        g_autofree char *str = NULL;
7548c0
+        g_autofree char *blkstr = NULL;
7548c0
+
7548c0
+        if (!(blkstr = virCgroupGetBlockDevString(devPath)))
7548c0
+            return -1;
7548c0
+
7548c0
+        str = g_strdup_printf("%s%d", blkstr, weight);
7548c0
+
7548c0
+        return virCgroupSetValueRaw(path, str);
7548c0
+    }
7548c0
 }
7548c0
 
7548c0
 
7548c0
@@ -1455,9 +1466,14 @@ static int
7548c0
 virCgroupV2SetCpuShares(virCgroupPtr group,
7548c0
                         unsigned long long shares)
7548c0
 {
7548c0
-    return virCgroupSetValueU64(group,
7548c0
-                                VIR_CGROUP_CONTROLLER_CPU,
7548c0
-                                "cpu.weight", shares);
7548c0
+    if (group->unitName) {
7548c0
+        return virCgroupSetValueDBus(group->unitName, "CPUWeight",
7548c0
+                                     "t", shares);
7548c0
+    } else {
7548c0
+        return virCgroupSetValueU64(group,
7548c0
+                                    VIR_CGROUP_CONTROLLER_CPU,
7548c0
+                                    "cpu.weight", shares);
7548c0
+    }
7548c0
 }
7548c0
 
7548c0
 
7548c0
diff --git a/tests/Makefile.am b/tests/Makefile.am
7548c0
index f957c7d1ba..b030d0e8f6 100644
7548c0
--- a/tests/Makefile.am
7548c0
+++ b/tests/Makefile.am
7548c0
@@ -1188,6 +1188,7 @@ libvirportallocatormock_la_LIBADD = $(MOCKLIBS_LIBS)
7548c0
 
7548c0
 vircgrouptest_SOURCES = \
7548c0
 	vircgrouptest.c testutils.h testutils.c
7548c0
+vircgrouptest_CFLAGS = $(DBUS_CFLAGS) $(AM_CFLAGS)
7548c0
 vircgrouptest_LDADD = $(LDADDS)
7548c0
 
7548c0
 libvircgroupmock_la_SOURCES = \
7548c0
-- 
7548c0
2.30.0
7548c0