Blob Blame History Raw
From 9cf56b5a0d1394fef10afdd763dc8005457bbaf5 Mon Sep 17 00:00:00 2001
Message-Id: <9cf56b5a0d1394fef10afdd763dc8005457bbaf5@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Fri, 19 Feb 2021 13:33:51 +0100
Subject: [PATCH] vircgroupv2: properly detect placement of running VM
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When libvirtd starts a VM it internally stores a path to the main
cgroup. When we restart libvirtd we should get to the same state.

When we start a VM on host with systemd the cgroup is created for us and
the process is already placed into that cgroup and we detect the path
created by systemd using /proc/$PID/cgroup. After that we create
sub-cgroups and move all threads there.

Once libvirtd is restarted we again detect the cgroup path using
/proc/$PID/cgroup, but in this case we will get a different path because
the main thread was moved to a "emulator" cgroup.

Instead of ignoring the "emulator" directory when validating cgroups
remove it completely when detecting cgroup otherwise cgroups will not
work properly when libvirtd is restarted.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
(cherry picked from commit 902c6644a8ec292789d561b3188e576c37a86872)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <10fb6b61cbb4f9caf8e8ba7706ec01d1da41fc67.1613737828.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroupv2.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
index 92ae3ec839..4682a6a920 100644
--- a/src/util/vircgroupv2.c
+++ b/src/util/vircgroupv2.c
@@ -121,12 +121,6 @@ virCgroupV2ValidateMachineGroup(virCgroupPtr group,
     if (!(tmp = strrchr(group->unified.placement, '/')))
         return false;
 
-    if (STREQ(tmp, "/emulator")) {
-        *tmp = '\0';
-
-        if (!(tmp = strrchr(group->unified.placement, '/')))
-            return false;
-    }
     tmp++;
 
     if (STRNEQ(tmp, partmachinename) &&
@@ -197,6 +191,9 @@ virCgroupV2DetectPlacement(virCgroupPtr group,
                            const char *controllers,
                            const char *selfpath)
 {
+    g_autofree char *placement = g_strdup(selfpath);
+    char *tmp = NULL;
+
     if (group->unified.placement)
         return 0;
 
@@ -207,12 +204,18 @@ virCgroupV2DetectPlacement(virCgroupPtr group,
     if (STRNEQ(controllers, ""))
         return 0;
 
+    /* Running VM will have the main thread placed in emulator cgroup
+     * but we need to get the main cgroup. */
+    tmp = g_strrstr(placement, "/emulator");
+    if (tmp)
+        *tmp = '\0';
+
     /*
      * selfpath == "/" + path="" -> "/"
      * selfpath == "/libvirt.service" + path == "" -> "/libvirt.service"
      * selfpath == "/libvirt.service" + path == "foo" -> "/libvirt.service/foo"
      */
-    group->unified.placement = g_strdup_printf("%s%s%s", selfpath,
+    group->unified.placement = g_strdup_printf("%s%s%s", placement,
                                                (STREQ(selfpath, "/") || STREQ(path, "") ? "" : "/"), path);
 
     return 0;
-- 
2.30.0