c313de
From a756729403915485dbd3cbdf4f252806318c227c Mon Sep 17 00:00:00 2001
c313de
Message-Id: <a756729403915485dbd3cbdf4f252806318c227c@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Mon, 1 Jul 2019 17:06:37 +0200
c313de
Subject: [PATCH] vircgroup: extract virCgroupV1AddTask
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
(cherry picked from commit 064024e70afd6ad08993444b3839eb3ba67426f2)
c313de
c313de
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
Message-Id: <558f1a90ce9b0fc5a9688c5a44bf85dd523c37e5.1561993100.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/util/vircgroup.c        | 56 ++++---------------------------------
c313de
 src/util/vircgroupbackend.h | 20 +++++++++++++
c313de
 src/util/vircgrouppriv.h    |  5 ++++
c313de
 src/util/vircgroupv1.c      | 31 ++++++++++++++++++++
c313de
 4 files changed, 62 insertions(+), 50 deletions(-)
c313de
c313de
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c313de
index 33d34d6eda..a5076cb554 100644
c313de
--- a/src/util/vircgroup.c
c313de
+++ b/src/util/vircgroup.c
c313de
@@ -544,7 +544,7 @@ virCgroupSetValueU64(virCgroupPtr group,
c313de
 }
c313de
 
c313de
 
c313de
-static int
c313de
+int
c313de
 virCgroupSetValueI64(virCgroupPtr group,
c313de
                      int controller,
c313de
                      const char *key,
c313de
@@ -672,50 +672,6 @@ virCgroupNew(pid_t pid,
c313de
 }
c313de
 
c313de
 
c313de
-typedef enum {
c313de
-    /* Adds a whole process with all threads to specific cgroup except
c313de
-     * to systemd named controller. */
c313de
-    VIR_CGROUP_TASK_PROCESS = 1 << 0,
c313de
-
c313de
-    /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd
c313de
-     * named controller. */
c313de
-    VIR_CGROUP_TASK_SYSTEMD = 1 << 1,
c313de
-
c313de
-    /* Moves only specific thread into cgroup except to systemd
c313de
-     * named controller. */
c313de
-    VIR_CGROUP_TASK_THREAD = 1 << 2,
c313de
-} virCgroupTaskFlags;
c313de
-
c313de
-
c313de
-static int
c313de
-virCgroupAddTaskInternal(virCgroupPtr group,
c313de
-                         pid_t pid,
c313de
-                         unsigned int flags)
c313de
-{
c313de
-    int ret = -1;
c313de
-    size_t i;
c313de
-
c313de
-    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c313de
-        /* Skip over controllers not mounted */
c313de
-        if (!group->controllers[i].mountPoint)
c313de
-            continue;
c313de
-
c313de
-        /* We must never add tasks in systemd's hierarchy
c313de
-         * unless we're intentionally trying to move a
c313de
-         * task into a systemd machine scope */
c313de
-        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD &&
c313de
-            !(flags & VIR_CGROUP_TASK_SYSTEMD))
c313de
-            continue;
c313de
-
c313de
-        if (virCgroupSetValueI64(group, i, "tasks", pid) < 0)
c313de
-            goto cleanup;
c313de
-    }
c313de
-
c313de
-    ret = 0;
c313de
- cleanup:
c313de
-    return ret;
c313de
-}
c313de
-
c313de
 /**
c313de
  * virCgroupAddProcess:
c313de
  *
c313de
@@ -730,7 +686,7 @@ virCgroupAddTaskInternal(virCgroupPtr group,
c313de
 int
c313de
 virCgroupAddProcess(virCgroupPtr group, pid_t pid)
c313de
 {
c313de
-    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_PROCESS);
c313de
+    return group->backend->addTask(group, pid, VIR_CGROUP_TASK_PROCESS);
c313de
 }
c313de
 
c313de
 /**
c313de
@@ -747,9 +703,9 @@ virCgroupAddProcess(virCgroupPtr group, pid_t pid)
c313de
 int
c313de
 virCgroupAddMachineProcess(virCgroupPtr group, pid_t pid)
c313de
 {
c313de
-    return virCgroupAddTaskInternal(group, pid,
c313de
-                                    VIR_CGROUP_TASK_PROCESS |
c313de
-                                    VIR_CGROUP_TASK_SYSTEMD);
c313de
+    return group->backend->addTask(group, pid,
c313de
+                                   VIR_CGROUP_TASK_PROCESS |
c313de
+                                   VIR_CGROUP_TASK_SYSTEMD);
c313de
 }
c313de
 
c313de
 /**
c313de
@@ -767,7 +723,7 @@ int
c313de
 virCgroupAddThread(virCgroupPtr group,
c313de
                    pid_t pid)
c313de
 {
c313de
-    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_THREAD);
c313de
+    return group->backend->addTask(group, pid, VIR_CGROUP_TASK_THREAD);
c313de
 }
c313de
 
c313de
 
c313de
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
c313de
index 1f28c51c49..bff1f8938e 100644
c313de
--- a/src/util/vircgroupbackend.h
c313de
+++ b/src/util/vircgroupbackend.h
c313de
@@ -35,6 +35,20 @@ typedef enum {
c313de
                                        */
c313de
 } virCgroupBackendFlags;
c313de
 
c313de
+typedef enum {
c313de
+    /* Adds a whole process with all threads to specific cgroup except
c313de
+     * to systemd named controller. */
c313de
+    VIR_CGROUP_TASK_PROCESS = 1 << 0,
c313de
+
c313de
+    /* Same as VIR_CGROUP_TASK_PROCESS but it also adds the task to systemd
c313de
+     * named controller. */
c313de
+    VIR_CGROUP_TASK_SYSTEMD = 1 << 1,
c313de
+
c313de
+    /* Moves only specific thread into cgroup except to systemd
c313de
+     * named controller. */
c313de
+    VIR_CGROUP_TASK_THREAD = 1 << 2,
c313de
+} virCgroupBackendTaskFlags;
c313de
+
c313de
 typedef enum {
c313de
     VIR_CGROUP_BACKEND_TYPE_V1 = 0,
c313de
     VIR_CGROUP_BACKEND_TYPE_LAST,
c313de
@@ -103,6 +117,11 @@ typedef int
c313de
 typedef int
c313de
 (*virCgroupRemoveCB)(virCgroupPtr group);
c313de
 
c313de
+typedef int
c313de
+(*virCgroupAddTaskCB)(virCgroupPtr group,
c313de
+                      pid_t pid,
c313de
+                      unsigned int flags);
c313de
+
c313de
 struct _virCgroupBackend {
c313de
     virCgroupBackendType type;
c313de
 
c313de
@@ -121,6 +140,7 @@ struct _virCgroupBackend {
c313de
     virCgroupPathOfControllerCB pathOfController;
c313de
     virCgroupMakeGroupCB makeGroup;
c313de
     virCgroupRemoveCB remove;
c313de
+    virCgroupAddTaskCB addTask;
c313de
 };
c313de
 typedef struct _virCgroupBackend virCgroupBackend;
c313de
 typedef virCgroupBackend *virCgroupBackendPtr;
c313de
diff --git a/src/util/vircgrouppriv.h b/src/util/vircgrouppriv.h
c313de
index a760b9bcfd..01714370be 100644
c313de
--- a/src/util/vircgrouppriv.h
c313de
+++ b/src/util/vircgrouppriv.h
c313de
@@ -73,6 +73,11 @@ int virCgroupGetValueU64(virCgroupPtr group,
c313de
                          const char *key,
c313de
                          unsigned long long int *value);
c313de
 
c313de
+int virCgroupSetValueI64(virCgroupPtr group,
c313de
+                         int controller,
c313de
+                         const char *key,
c313de
+                         long long int value);
c313de
+
c313de
 int virCgroupPartitionEscape(char **path);
c313de
 
c313de
 int virCgroupNewPartition(const char *path,
c313de
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
c313de
index 4d4b51094a..fda2c43eef 100644
c313de
--- a/src/util/vircgroupv1.c
c313de
+++ b/src/util/vircgroupv1.c
c313de
@@ -706,6 +706,36 @@ virCgroupV1Remove(virCgroupPtr group)
c313de
 }
c313de
 
c313de
 
c313de
+static int
c313de
+virCgroupV1AddTask(virCgroupPtr group,
c313de
+                   pid_t pid,
c313de
+                   unsigned int flags)
c313de
+{
c313de
+    int ret = -1;
c313de
+    size_t i;
c313de
+
c313de
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
c313de
+        /* Skip over controllers not mounted */
c313de
+        if (!group->controllers[i].mountPoint)
c313de
+            continue;
c313de
+
c313de
+        /* We must never add tasks in systemd's hierarchy
c313de
+         * unless we're intentionally trying to move a
c313de
+         * task into a systemd machine scope */
c313de
+        if (i == VIR_CGROUP_CONTROLLER_SYSTEMD &&
c313de
+            !(flags & VIR_CGROUP_TASK_SYSTEMD))
c313de
+            continue;
c313de
+
c313de
+        if (virCgroupSetValueI64(group, i, "tasks", pid) < 0)
c313de
+            goto cleanup;
c313de
+    }
c313de
+
c313de
+    ret = 0;
c313de
+ cleanup:
c313de
+    return ret;
c313de
+}
c313de
+
c313de
+
c313de
 virCgroupBackend virCgroupV1Backend = {
c313de
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
c313de
 
c313de
@@ -723,6 +753,7 @@ virCgroupBackend virCgroupV1Backend = {
c313de
     .pathOfController = virCgroupV1PathOfController,
c313de
     .makeGroup = virCgroupV1MakeGroup,
c313de
     .remove = virCgroupV1Remove,
c313de
+    .addTask = virCgroupV1AddTask,
c313de
 };
c313de
 
c313de
 
c313de
-- 
c313de
2.22.0
c313de