7548c0
From 9cf56b5a0d1394fef10afdd763dc8005457bbaf5 Mon Sep 17 00:00:00 2001
7548c0
Message-Id: <9cf56b5a0d1394fef10afdd763dc8005457bbaf5@dist-git>
7548c0
From: Pavel Hrdina <phrdina@redhat.com>
7548c0
Date: Fri, 19 Feb 2021 13:33:51 +0100
7548c0
Subject: [PATCH] vircgroupv2: properly detect placement of running VM
7548c0
MIME-Version: 1.0
7548c0
Content-Type: text/plain; charset=UTF-8
7548c0
Content-Transfer-Encoding: 8bit
7548c0
7548c0
When libvirtd starts a VM it internally stores a path to the main
7548c0
cgroup. When we restart libvirtd we should get to the same state.
7548c0
7548c0
When we start a VM on host with systemd the cgroup is created for us and
7548c0
the process is already placed into that cgroup and we detect the path
7548c0
created by systemd using /proc/$PID/cgroup. After that we create
7548c0
sub-cgroups and move all threads there.
7548c0
7548c0
Once libvirtd is restarted we again detect the cgroup path using
7548c0
/proc/$PID/cgroup, but in this case we will get a different path because
7548c0
the main thread was moved to a "emulator" cgroup.
7548c0
7548c0
Instead of ignoring the "emulator" directory when validating cgroups
7548c0
remove it completely when detecting cgroup otherwise cgroups will not
7548c0
work properly when libvirtd is restarted.
7548c0
7548c0
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
7548c0
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
7548c0
(cherry picked from commit 902c6644a8ec292789d561b3188e576c37a86872)
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: <10fb6b61cbb4f9caf8e8ba7706ec01d1da41fc67.1613737828.git.phrdina@redhat.com>
7548c0
Reviewed-by: Ján Tomko <jtomko@redhat.com>
7548c0
---
7548c0
 src/util/vircgroupv2.c | 17 ++++++++++-------
7548c0
 1 file changed, 10 insertions(+), 7 deletions(-)
7548c0
7548c0
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
7548c0
index 92ae3ec839..4682a6a920 100644
7548c0
--- a/src/util/vircgroupv2.c
7548c0
+++ b/src/util/vircgroupv2.c
7548c0
@@ -121,12 +121,6 @@ virCgroupV2ValidateMachineGroup(virCgroupPtr group,
7548c0
     if (!(tmp = strrchr(group->unified.placement, '/')))
7548c0
         return false;
7548c0
 
7548c0
-    if (STREQ(tmp, "/emulator")) {
7548c0
-        *tmp = '\0';
7548c0
-
7548c0
-        if (!(tmp = strrchr(group->unified.placement, '/')))
7548c0
-            return false;
7548c0
-    }
7548c0
     tmp++;
7548c0
 
7548c0
     if (STRNEQ(tmp, partmachinename) &&
7548c0
@@ -197,6 +191,9 @@ virCgroupV2DetectPlacement(virCgroupPtr group,
7548c0
                            const char *controllers,
7548c0
                            const char *selfpath)
7548c0
 {
7548c0
+    g_autofree char *placement = g_strdup(selfpath);
7548c0
+    char *tmp = NULL;
7548c0
+
7548c0
     if (group->unified.placement)
7548c0
         return 0;
7548c0
 
7548c0
@@ -207,12 +204,18 @@ virCgroupV2DetectPlacement(virCgroupPtr group,
7548c0
     if (STRNEQ(controllers, ""))
7548c0
         return 0;
7548c0
 
7548c0
+    /* Running VM will have the main thread placed in emulator cgroup
7548c0
+     * but we need to get the main cgroup. */
7548c0
+    tmp = g_strrstr(placement, "/emulator");
7548c0
+    if (tmp)
7548c0
+        *tmp = '\0';
7548c0
+
7548c0
     /*
7548c0
      * selfpath == "/" + path="" -> "/"
7548c0
      * selfpath == "/libvirt.service" + path == "" -> "/libvirt.service"
7548c0
      * selfpath == "/libvirt.service" + path == "foo" -> "/libvirt.service/foo"
7548c0
      */
7548c0
-    group->unified.placement = g_strdup_printf("%s%s%s", selfpath,
7548c0
+    group->unified.placement = g_strdup_printf("%s%s%s", placement,
7548c0
                                                (STREQ(selfpath, "/") || STREQ(path, "") ? "" : "/"), path);
7548c0
 
7548c0
     return 0;
7548c0
-- 
7548c0
2.30.0
7548c0