Blame SOURCES/libvirt-vircgroupv2-move-task-into-cgroup-before-enabling-controllers.patch

8c03ec
From 41a7547b32786b1a84c8ee7bad0c4cf9559ea4b9 Mon Sep 17 00:00:00 2001
8c03ec
Message-Id: <41a7547b32786b1a84c8ee7bad0c4cf9559ea4b9@dist-git>
8c03ec
From: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Date: Fri, 19 Feb 2021 13:33:57 +0100
8c03ec
Subject: [PATCH] vircgroupv2: move task into cgroup before enabling
8c03ec
 controllers
8c03ec
MIME-Version: 1.0
8c03ec
Content-Type: text/plain; charset=UTF-8
8c03ec
Content-Transfer-Encoding: 8bit
8c03ec
8c03ec
When we create a new child cgroup and the parent cgroup has any process
8c03ec
attached to it enabling controllers for the child cgroup fails with
8c03ec
error. We need to move the process into the child cgroup first before
8c03ec
enabling any controllers.
8c03ec
8c03ec
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
8c03ec
(cherry picked from commit 382fa15cde538cba3888a89b301fd3d9a0ce69ea)
8c03ec
8c03ec
Conflicts:
8c03ec
    src/util/vircgroup.c
8c03ec
        - missing upstream g_autofree rewrite
8c03ec
8c03ec
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
8c03ec
8c03ec
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Message-Id: <85d34403caacb571cb78539d5c4f56eee9484d57.1613737828.git.phrdina@redhat.com>
8c03ec
Reviewed-by: Ján Tomko <jtomko@redhat.com>
8c03ec
---
8c03ec
 src/util/vircgroup.c        | 13 +++++++------
8c03ec
 src/util/vircgroupbackend.h |  1 +
8c03ec
 src/util/vircgroupv1.c      |  1 +
8c03ec
 src/util/vircgroupv2.c      | 13 +++++++++++++
8c03ec
 4 files changed, 22 insertions(+), 6 deletions(-)
8c03ec
8c03ec
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
8c03ec
index 10b934291c..8f5bcd94f4 100644
8c03ec
--- a/src/util/vircgroup.c
8c03ec
+++ b/src/util/vircgroup.c
8c03ec
@@ -622,13 +622,14 @@ static int
8c03ec
 virCgroupMakeGroup(virCgroupPtr parent,
8c03ec
                    virCgroupPtr group,
8c03ec
                    bool create,
8c03ec
+                   pid_t pid,
8c03ec
                    unsigned int flags)
8c03ec
 {
8c03ec
     size_t i;
8c03ec
 
8c03ec
     for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
8c03ec
         if (group->backends[i] &&
8c03ec
-            group->backends[i]->makeGroup(parent, group, create, flags) < 0) {
8c03ec
+            group->backends[i]->makeGroup(parent, group, create, pid, flags) < 0) {
8c03ec
             virCgroupRemove(group);
8c03ec
             return -1;
8c03ec
         }
8c03ec
@@ -857,8 +858,8 @@ virCgroupNewPartition(const char *path,
8c03ec
         goto cleanup;
8c03ec
 
8c03ec
     if (parent) {
8c03ec
-        if (virCgroupMakeGroup(parent, *group, create, VIR_CGROUP_NONE) < 0)
8c03ec
-            goto cleanup;
8c03ec
+        if (virCgroupMakeGroup(parent, *group, create, -1, VIR_CGROUP_NONE) < 0)
8c03ec
+            return -1;
8c03ec
     }
8c03ec
 
8c03ec
     ret = 0;
8c03ec
@@ -924,7 +925,7 @@ virCgroupNewDomainPartition(virCgroupPtr partition,
8c03ec
      * a group for driver, is to avoid overhead to track
8c03ec
      * cumulative usage that we don't need.
8c03ec
      */
8c03ec
-    if (virCgroupMakeGroup(partition, *group, create,
8c03ec
+    if (virCgroupMakeGroup(partition, *group, create, -1,
8c03ec
                            VIR_CGROUP_MEM_HIERACHY) < 0) {
8c03ec
         virCgroupFree(group);
8c03ec
         return -1;
8c03ec
@@ -978,7 +979,7 @@ virCgroupNewThread(virCgroupPtr domain,
8c03ec
     if (virCgroupNew(-1, name, domain, controllers, group) < 0)
8c03ec
         return -1;
8c03ec
 
8c03ec
-    if (virCgroupMakeGroup(domain, *group, create, VIR_CGROUP_THREAD) < 0) {
8c03ec
+    if (virCgroupMakeGroup(domain, *group, create, -1, VIR_CGROUP_THREAD) < 0) {
8c03ec
         virCgroupFree(group);
8c03ec
         return -1;
8c03ec
     }
8c03ec
@@ -1065,7 +1066,7 @@ virCgroupEnableMissingControllers(char *path,
8c03ec
                          &tmp) < 0)
8c03ec
             goto cleanup;
8c03ec
 
8c03ec
-        if (virCgroupMakeGroup(parent, tmp, true, VIR_CGROUP_SYSTEMD) < 0) {
8c03ec
+        if (virCgroupMakeGroup(parent, tmp, true, -1, VIR_CGROUP_SYSTEMD) < 0) {
8c03ec
             virCgroupFree(&tmp);
8c03ec
             goto cleanup;
8c03ec
         }
8c03ec
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
8c03ec
index e12a2e8b9d..ac7b3ae517 100644
8c03ec
--- a/src/util/vircgroupbackend.h
8c03ec
+++ b/src/util/vircgroupbackend.h
8c03ec
@@ -119,6 +119,7 @@ typedef int
8c03ec
 (*virCgroupMakeGroupCB)(virCgroupPtr parent,
8c03ec
                         virCgroupPtr group,
8c03ec
                         bool create,
8c03ec
+                        pid_t pid,
8c03ec
                         unsigned int flags);
8c03ec
 
8c03ec
 typedef int
8c03ec
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
8c03ec
index 09165ece4d..eb2b611cee 100644
8c03ec
--- a/src/util/vircgroupv1.c
8c03ec
+++ b/src/util/vircgroupv1.c
8c03ec
@@ -601,6 +601,7 @@ static int
8c03ec
 virCgroupV1MakeGroup(virCgroupPtr parent,
8c03ec
                      virCgroupPtr group,
8c03ec
                      bool create,
8c03ec
+                     pid_t pid G_GNUC_UNUSED,
8c03ec
                      unsigned int flags)
8c03ec
 {
8c03ec
     size_t i;
8c03ec
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
8c03ec
index 8fe4894a9e..5e19ed8332 100644
8c03ec
--- a/src/util/vircgroupv2.c
8c03ec
+++ b/src/util/vircgroupv2.c
8c03ec
@@ -398,10 +398,17 @@ virCgroupV2EnableController(virCgroupPtr group,
8c03ec
 }
8c03ec
 
8c03ec
 
8c03ec
+static int
8c03ec
+virCgroupV2AddTask(virCgroupPtr group,
8c03ec
+                   pid_t pid,
8c03ec
+                   unsigned int flags);
8c03ec
+
8c03ec
+
8c03ec
 static int
8c03ec
 virCgroupV2MakeGroup(virCgroupPtr parent,
8c03ec
                      virCgroupPtr group,
8c03ec
                      bool create,
8c03ec
+                     pid_t pid,
8c03ec
                      unsigned int flags)
8c03ec
 {
8c03ec
     g_autofree char *path = NULL;
8c03ec
@@ -449,6 +456,12 @@ virCgroupV2MakeGroup(virCgroupPtr parent,
8c03ec
             }
8c03ec
         } else {
8c03ec
             size_t i;
8c03ec
+
8c03ec
+            if (pid > 0) {
8c03ec
+                if (virCgroupV2AddTask(group, pid, VIR_CGROUP_TASK_PROCESS) < 0)
8c03ec
+                    return -1;
8c03ec
+            }
8c03ec
+
8c03ec
             for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
8c03ec
                 int rc;
8c03ec
 
8c03ec
-- 
8c03ec
2.30.0
8c03ec