]> xenbits.xensource.com Git - libvirt.git/commitdiff
vircgroupv2: move task into cgroup before enabling controllers
authorPavel Hrdina <phrdina@redhat.com>
Wed, 4 Nov 2020 18:55:44 +0000 (19:55 +0100)
committerPavel Hrdina <phrdina@redhat.com>
Wed, 10 Feb 2021 12:37:12 +0000 (13:37 +0100)
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>
src/util/vircgroup.c
src/util/vircgroupbackend.h
src/util/vircgroupv1.c
src/util/vircgroupv2.c

index 7a0e14eb731f8dde0c23897ac8d8d7da8f07f7cb..44a30648761c710c30f910f8eb1e7247b5f24d3f 100644 (file)
@@ -694,13 +694,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;
         }
@@ -970,7 +971,7 @@ virCgroupNewPartition(const char *path,
         return -1;
 
     if (parent) {
-        if (virCgroupMakeGroup(parent, newGroup, create, VIR_CGROUP_NONE) < 0)
+        if (virCgroupMakeGroup(parent, newGroup, create, -1, VIR_CGROUP_NONE) < 0)
             return -1;
     }
 
@@ -1033,7 +1034,7 @@ virCgroupNewDomainPartition(virCgroupPtr partition,
      * a group for driver, is to avoid overhead to track
      * cumulative usage that we don't need.
      */
-    if (virCgroupMakeGroup(partition, newGroup, true,
+    if (virCgroupMakeGroup(partition, newGroup, true, -1,
                            VIR_CGROUP_MEM_HIERACHY) < 0) {
         return -1;
     }
@@ -1090,7 +1091,7 @@ virCgroupNewThread(virCgroupPtr domain,
     if (virCgroupNewFromParent(domain, name, controllers, &newGroup) < 0)
         return -1;
 
-    if (virCgroupMakeGroup(domain, newGroup, create, VIR_CGROUP_THREAD) < 0)
+    if (virCgroupMakeGroup(domain, newGroup, create, -1, VIR_CGROUP_THREAD) < 0)
         return -1;
 
     *group = g_steal_pointer(&newGroup);
@@ -1192,7 +1193,7 @@ virCgroupEnableMissingControllers(char *path,
                                    &tmp) < 0)
             return -1;
 
-        if (virCgroupMakeGroup(parent, tmp, true, VIR_CGROUP_SYSTEMD) < 0)
+        if (virCgroupMakeGroup(parent, tmp, true, -1, VIR_CGROUP_SYSTEMD) < 0)
             return -1;
 
         virCgroupFree(parent);
index 4ca2e38af21d663185774de1275026b457a6c438..497dde56252cfb19c756be52c68a6634ba01d960 100644 (file)
@@ -122,6 +122,7 @@ typedef int
 (*virCgroupMakeGroupCB)(virCgroupPtr parent,
                         virCgroupPtr group,
                         bool create,
+                        pid_t pid,
                         unsigned int flags);
 
 typedef int
index f4dd20fd73aac658c41815a27171b34fe5dd8778..51ada12f6eeb8c045309e6fdf37165e7f6b0112f 100644 (file)
@@ -622,6 +622,7 @@ static int
 virCgroupV1MakeGroup(virCgroupPtr parent,
                      virCgroupPtr group,
                      bool create,
+                     pid_t pid G_GNUC_UNUSED,
                      unsigned int flags)
 {
     size_t i;
index a61c633a1f9d279010653c4a2ee843a7bc0f1a89..2f82842cde49229920fef9b0ab8bf4d6d2865e0c 100644 (file)
@@ -406,10 +406,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;
@@ -455,6 +462,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;