Blob Blame History Raw
From 41a7547b32786b1a84c8ee7bad0c4cf9559ea4b9 Mon Sep 17 00:00:00 2001
Message-Id: <41a7547b32786b1a84c8ee7bad0c4cf9559ea4b9@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Fri, 19 Feb 2021 13:33:57 +0100
Subject: [PATCH] vircgroupv2: move task into cgroup before enabling
 controllers
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When we create a new child cgroup and the parent cgroup has any process
attached to it enabling controllers for the child cgroup fails with
error. We need to move the process into the child cgroup first before
enabling any controllers.

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

Conflicts:
    src/util/vircgroup.c
        - missing upstream g_autofree rewrite

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

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <85d34403caacb571cb78539d5c4f56eee9484d57.1613737828.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroup.c        | 13 +++++++------
 src/util/vircgroupbackend.h |  1 +
 src/util/vircgroupv1.c      |  1 +
 src/util/vircgroupv2.c      | 13 +++++++++++++
 4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 10b934291c..8f5bcd94f4 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -622,13 +622,14 @@ static int
 virCgroupMakeGroup(virCgroupPtr parent,
                    virCgroupPtr group,
                    bool create,
+                   pid_t pid,
                    unsigned int flags)
 {
     size_t i;
 
     for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
         if (group->backends[i] &&
-            group->backends[i]->makeGroup(parent, group, create, flags) < 0) {
+            group->backends[i]->makeGroup(parent, group, create, pid, flags) < 0) {
             virCgroupRemove(group);
             return -1;
         }
@@ -857,8 +858,8 @@ virCgroupNewPartition(const char *path,
         goto cleanup;
 
     if (parent) {
-        if (virCgroupMakeGroup(parent, *group, create, VIR_CGROUP_NONE) < 0)
-            goto cleanup;
+        if (virCgroupMakeGroup(parent, *group, create, -1, VIR_CGROUP_NONE) < 0)
+            return -1;
     }
 
     ret = 0;
@@ -924,7 +925,7 @@ virCgroupNewDomainPartition(virCgroupPtr partition,
      * a group for driver, is to avoid overhead to track
      * cumulative usage that we don't need.
      */
-    if (virCgroupMakeGroup(partition, *group, create,
+    if (virCgroupMakeGroup(partition, *group, create, -1,
                            VIR_CGROUP_MEM_HIERACHY) < 0) {
         virCgroupFree(group);
         return -1;
@@ -978,7 +979,7 @@ virCgroupNewThread(virCgroupPtr domain,
     if (virCgroupNew(-1, name, domain, controllers, group) < 0)
         return -1;
 
-    if (virCgroupMakeGroup(domain, *group, create, VIR_CGROUP_THREAD) < 0) {
+    if (virCgroupMakeGroup(domain, *group, create, -1, VIR_CGROUP_THREAD) < 0) {
         virCgroupFree(group);
         return -1;
     }
@@ -1065,7 +1066,7 @@ virCgroupEnableMissingControllers(char *path,
                          &tmp) < 0)
             goto cleanup;
 
-        if (virCgroupMakeGroup(parent, tmp, true, VIR_CGROUP_SYSTEMD) < 0) {
+        if (virCgroupMakeGroup(parent, tmp, true, -1, VIR_CGROUP_SYSTEMD) < 0) {
             virCgroupFree(&tmp);
             goto cleanup;
         }
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index e12a2e8b9d..ac7b3ae517 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -119,6 +119,7 @@ typedef int
 (*virCgroupMakeGroupCB)(virCgroupPtr parent,
                         virCgroupPtr group,
                         bool create,
+                        pid_t pid,
                         unsigned int flags);
 
 typedef int
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index 09165ece4d..eb2b611cee 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -601,6 +601,7 @@ static int
 virCgroupV1MakeGroup(virCgroupPtr parent,
                      virCgroupPtr group,
                      bool create,
+                     pid_t pid G_GNUC_UNUSED,
                      unsigned int flags)
 {
     size_t i;
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
index 8fe4894a9e..5e19ed8332 100644
--- a/src/util/vircgroupv2.c
+++ b/src/util/vircgroupv2.c
@@ -398,10 +398,17 @@ virCgroupV2EnableController(virCgroupPtr group,
 }
 
 
+static int
+virCgroupV2AddTask(virCgroupPtr group,
+                   pid_t pid,
+                   unsigned int flags);
+
+
 static int
 virCgroupV2MakeGroup(virCgroupPtr parent,
                      virCgroupPtr group,
                      bool create,
+                     pid_t pid,
                      unsigned int flags)
 {
     g_autofree char *path = NULL;
@@ -449,6 +456,12 @@ virCgroupV2MakeGroup(virCgroupPtr parent,
             }
         } else {
             size_t i;
+
+            if (pid > 0) {
+                if (virCgroupV2AddTask(group, pid, VIR_CGROUP_TASK_PROCESS) < 0)
+                    return -1;
+            }
+
             for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
                 int rc;
 
-- 
2.30.0