render / rpms / libvirt

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